fix(mcp): cancel evidence imports before session mutation

Propagate request cancellation through immutable JSON bundle reading and
check it before committing session state. Preserve failed-close ownership.

Fixes #1746

Assisted-by: Suika (gensokyo-skills:suika-scatter-gather-planning)
This commit is contained in:
Ysopwnable
2026-10-11 15:59:21 +08:00
committed by GitHub
parent 953a0ce5bd
commit e5699d23b1
5 changed files with 235 additions and 12 deletions
@@ -8,7 +8,7 @@ import {
} from "node:fs/promises";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { describe, expect, it, vi } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
@@ -19,6 +19,8 @@ import {
} from "../../../src/application/EvidenceBundleFiles.js";
import { compareEvidenceBundlesCommand } from "../../../src/application/EvidenceBundleCommands.js";
import { EvidenceLedger } from "../../../src/application/investigation/EvidenceLedger.js";
import { RegularFileCleanupFailure } from "../../../src/application/RegularFileRead.js";
import { OwnedFileHandle } from "../../../src/filesystem/OwnedFileHandle.js";
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import { createEvidence } from "../../../src/domain/evidence.js";
import { writeTextParts } from "../../../src/application/JsonFiles.js";
@@ -36,6 +38,56 @@ const bundle = (result = true) =>
),
]);
it("returns import cancellation before inspecting a selected path", async () => {
const root = await createTestTempDirectory("rea-import-pre-cancel-");
const controller = new AbortController();
controller.abort(new Error("cancel before import"));
expect(
await readImmutableEvidenceBundle(
join(root, "missing.json"),
controller.signal,
),
).toMatchObject({
ok: false,
error: {
_tag: "AnalysisCancelledError",
operation: "import_evidence_bundle",
},
});
expect(await readdir(root)).toEqual([]);
});
it("preserves the failed-close owner when import is also cancelled", async () => {
const root = await createTestTempDirectory("rea-import-cancel-cleanup-");
const path = join(root, "bundle.json");
await writeFile(path, JSON.stringify(bundle()));
const controller = new AbortController();
const cleanupCause = new Error("fixture close failure");
const close = vi
.spyOn(OwnedFileHandle.prototype, "close")
.mockImplementationOnce(async () => {
controller.abort(new Error("cancel during cleanup"));
throw cleanupCause;
});
let failure: unknown;
try {
try {
await readImmutableEvidenceBundle(path, controller.signal);
} catch (cause: unknown) {
failure = cause;
}
expect(failure).toBeInstanceOf(RegularFileCleanupFailure);
if (!(failure instanceof RegularFileCleanupFailure))
throw new Error("Expected the retained file handle owner");
expect(failure.cleanupCause).toBe(cleanupCause);
} finally {
close.mockRestore();
if (failure instanceof RegularFileCleanupFailure)
await failure.owner.close();
}
expect(await readEvidenceBundle(path)).toMatchObject({ ok: true });
});
describe("evidence bundle export cancellation", () => {
it("does not inspect the path or advance a source cancelled before writing", async () => {
const root = await createTestTempDirectory("rea-export-pre-cancel-");