fix(session): serialize snapshot lifecycle with admitted requests

This commit is contained in:
morluto
2026-10-09 04:37:43 +08:00
parent 8d39eae6b9
commit e44cb45908
11 changed files with 587 additions and 148 deletions
+11 -23
View File
@@ -1,9 +1,8 @@
import { writeAnalysisSnapshot } from "../application/binary/AnalysisSnapshotFiles.js";
import { ok } from "../domain/result.js";
import {
reportLifecycleEnd,
reportLifecycleStart,
} from "./lifecycleProgress.js";
import { ok } from "../domain/result.js";
import { mcpProgressReporter } from "./mcpProgress.js";
import type { LifecycleToolRegistration } from "./registerSessionTools.js";
import { logToolExecution } from "./toolLogging.js";
@@ -22,7 +21,8 @@ export const registerCloseLifecycleTool = ({
toolRegistrationOptions(closeContract),
async (input, context) => {
const progress = mcpProgressReporter(context);
if (input.snapshot_path === undefined) {
const snapshotPath = input.snapshot_path;
if (snapshotPath === undefined) {
await reportLifecycleStart(progress, closeContract.name);
const closed = await logToolExecution(logger, closeContract.name, () =>
session.close({ progress }),
@@ -30,33 +30,21 @@ export const registerCloseLifecycleTool = ({
await reportLifecycleEnd(progress, closeContract.name, closed.ok);
return toCallToolResult(closed, closeContract);
}
const snapshot = session.exportAnalysisSnapshot();
if (!snapshot.ok) return toCallToolResult(snapshot, closeContract);
const written = await writeAnalysisSnapshot(
snapshot.value,
input.snapshot_path,
input.overwrite,
);
if (!written.ok) return toCallToolResult(written, closeContract);
await reportLifecycleStart(
progress,
closeContract.name,
"snapshot written; closing provider",
"saving snapshot; closing provider",
);
const closed = await logToolExecution(logger, closeContract.name, () =>
session.close({ progress }),
session.closeWithSnapshot(snapshotPath, input.overwrite, {
progress,
}),
);
await reportLifecycleEnd(progress, closeContract.name, closed.ok);
return closed.ok
? toCallToolResult(
ok({
path: written.value.path,
bytes: written.value.bytes,
entries: snapshot.value.entries.length,
}),
closeContract,
)
: toCallToolResult(closed, closeContract);
return toCallToolResult(
closed.ok ? ok({ ...closed.value }) : closed,
closeContract,
);
},
);
};