fix(io): retain cleanup ownership and honor cancellation

Keep failed JSON read handles in the existing server resource scope and fail admission until cleanup succeeds. Thread cancellation through every process CLI input read and replace timer-driven cancellation checks with a shared first-read gate.

Classify authenticated standalone Evidence as wrong-kind input while preserving tampered-record integrity failures and actionable messages across ledger, CLI, and MCP consumers. Record unrelated CS_RESTRICT process neighbors as unverified without weakening related-process ownership checks. Update stale ADB fixtures for the actual shell protocol.

Validated focused CLI, MCP, filesystem, ownership, and error projection tests; static, docs, and duplication gates pass. The host-level CS_RESTRICT load reproduction was not run. Refs #2050, #2035, #2064.
This commit is contained in:
morluto
2026-10-12 14:12:44 +08:00
parent e4532b5376
commit d8c76b6296
32 changed files with 1007 additions and 318 deletions
@@ -1,5 +1,5 @@
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { open, writeFile, type FileHandle } from "node:fs/promises";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it, onTestFinished, vi } from "vitest";
@@ -13,6 +13,7 @@ import { createServer } from "../../../src/server/createServer.js";
import { ToolResultDelivery } from "../../../src/server/toolResult.js";
import { createTestBinarySession } from "../../fixtures/binarySession.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { holdFirstFileRead } from "../../support/holdFirstFileRead.js";
it("leaves the session unchanged after bundle comparison cancellation and permits a retry", async () => {
const root = await createTestTempDirectory("rea-mcp-compare-cancel-");
@@ -58,8 +59,10 @@ it("leaves the session unchanged after bundle comparison cancellation and permit
version: "1",
});
const controller = new AbortController();
let releaseRead: (() => void) | undefined;
onTestFinished(async () => {
controller.abort();
releaseRead?.();
vi.restoreAllMocks();
await client.close();
await server.close();
@@ -70,8 +73,8 @@ it("leaves the session unchanged after bundle comparison cancellation and permit
await server.connect(serverTransport);
await client.connect(clientTransport);
const reader = await holdFirstRead(leftPath);
onTestFinished(reader.release);
const reader = await holdFirstFileRead(leftPath);
releaseRead = reader.release;
let resolveDispatched: (() => void) | undefined;
const dispatched = new Promise<void>((resolve) => {
@@ -162,56 +165,3 @@ it("leaves the session unchanged after bundle comparison cancellation and permit
]),
);
});
const holdFirstRead = async (path: string) => {
// Hold the first streamed read after it reaches the operating system.
const probe = await open(path, "r");
const identity = await probe.stat();
const fileHandlePrototype = Object.getPrototypeOf(probe) as {
read(
buffer: Buffer,
offset: number,
length: number,
position: number | null,
): Promise<{ readonly bytesRead: number; readonly buffer: Buffer }>;
};
await probe.close();
let totalReadBytes = 0;
let readCount = 0;
let resolveFirstRead: (() => void) | undefined;
const firstRead = new Promise<void>((resolve) => {
resolveFirstRead = resolve;
});
let releaseRead: (() => void) | undefined;
const readGate = new Promise<void>((resolve) => {
releaseRead = resolve;
});
const read = fileHandlePrototype.read;
vi.spyOn(fileHandlePrototype, "read").mockImplementation(async function (
this: FileHandle,
...args: [Buffer, number, number, number | null]
) {
const current = await this.stat();
const isTarget =
current.dev === identity.dev && current.ino === identity.ino;
const result = await read.call(this, ...args);
if (!isTarget) return result;
totalReadBytes += result.bytesRead;
readCount += 1;
if (readCount === 1) {
resolveFirstRead?.();
await readGate;
}
return result;
});
return {
firstRead,
release: () => releaseRead?.(),
get readCount() {
return readCount;
},
get totalReadBytes() {
return totalReadBytes;
},
};
};