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:
@@ -20,11 +20,13 @@ import {
|
||||
canonicalJsonParts,
|
||||
} from "../domain/jsonSerialization.js";
|
||||
import { readJsonFile, writeTextParts } from "./JsonFiles.js";
|
||||
import { RegularFileCleanupFailure } from "./RegularFileRead.js";
|
||||
|
||||
type EvidenceReadFailure =
|
||||
| EvidenceFileError
|
||||
| EvidenceIntegrityError
|
||||
| AnalysisResourceConstraintError;
|
||||
| AnalysisResourceConstraintError
|
||||
| AnalysisCancelledError;
|
||||
type EvidenceWriteFailure =
|
||||
| EvidenceFileError
|
||||
| EvidenceIntegrityError
|
||||
@@ -39,18 +41,38 @@ export const readEvidenceBundle = (
|
||||
/** Read an authenticated immutable snapshot for a subsequent session import. */
|
||||
export const readImmutableEvidenceBundle = (
|
||||
path: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<EvidenceBundle, EvidenceReadFailure>> =>
|
||||
readBundle(path, (input) => {
|
||||
const bundle = parseEvidenceBundle(input);
|
||||
return createImmutableEvidenceBundle(bundle.records, bundle.unknowns);
|
||||
});
|
||||
readBundle(
|
||||
path,
|
||||
(input) => {
|
||||
const bundle = parseEvidenceBundle(input);
|
||||
return createImmutableEvidenceBundle(bundle.records, bundle.unknowns);
|
||||
},
|
||||
signal,
|
||||
);
|
||||
|
||||
const readBundle = async (
|
||||
path: string,
|
||||
parseBundle: (input: unknown) => EvidenceBundle,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<EvidenceBundle, EvidenceReadFailure>> => {
|
||||
const loaded = await readJsonFile(path);
|
||||
if (signal?.aborted)
|
||||
return err(new AnalysisCancelledError("import_evidence_bundle"));
|
||||
const loaded = await readJsonFile(path, signal).catch((cause: unknown) => {
|
||||
// A failed close transfers a live handle owner; preserve that failure.
|
||||
if (
|
||||
signal?.aborted === true &&
|
||||
!(cause instanceof RegularFileCleanupFailure)
|
||||
)
|
||||
return err(
|
||||
new AnalysisCancelledError("import_evidence_bundle", { cause }),
|
||||
);
|
||||
throw cause;
|
||||
});
|
||||
if (!loaded.ok) return loaded;
|
||||
if (signal?.aborted)
|
||||
return err(new AnalysisCancelledError("import_evidence_bundle"));
|
||||
let bundle: EvidenceBundle;
|
||||
try {
|
||||
bundle = parseBundle(loaded.value);
|
||||
|
||||
@@ -35,11 +35,14 @@ type TextWriteResult<Failure = EvidenceFileError | AnalysisCancelledError> =
|
||||
/** Read JSON data from a regular file at the caller-supplied path. */
|
||||
export const readJsonFile = async (
|
||||
path: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<
|
||||
Result<unknown, EvidenceFileError | AnalysisResourceConstraintError>
|
||||
> =>
|
||||
readJsonFileInput(path, (selected) =>
|
||||
readJsonInputFile(selected, "read_evidence_file"),
|
||||
readJsonFileInput(
|
||||
path,
|
||||
(selected) => readJsonInputFile(selected, "read_evidence_file", signal),
|
||||
signal,
|
||||
);
|
||||
|
||||
/** Read selected JSON members through the same regular-file error boundary. */
|
||||
|
||||
Reference in New Issue
Block a user