perf(snapshot): restore large legacy caches within live memory bounds
Stream legacy snapshot cache rows independently, authenticate complete Evidence once, and restore exact immutable payloads without assembling duplicate full graphs. Preserve the existing file format, provenance, resource admission, cancellation and cache export isolation. Verified through real Linux SDK MCP import of the original 83 MB Ghidra snapshot and full 41 MB bundle hash, actual Windows CLI/MCP PE recovery and the installed Windows large-file decoder boundary, focused tests and static checks. Fixes #1680. Refs #1065.
This commit is contained in:
@@ -18,6 +18,7 @@ import {
|
||||
snapshotMatchesTarget,
|
||||
snapshotTarget,
|
||||
parseAnalysisSnapshot,
|
||||
immutableAnalysisSnapshot,
|
||||
type AnalysisSnapshot,
|
||||
type AnalysisSnapshotEntry,
|
||||
type AnalysisSnapshotWorkflowEntry,
|
||||
@@ -107,6 +108,7 @@ export class AnalysisSnapshotCache {
|
||||
|
||||
/** Merge already-validated entries and return the new-entry count. */
|
||||
stage(snapshot: AnalysisSnapshot): number {
|
||||
snapshot = immutableAnalysisSnapshot(snapshot);
|
||||
if (
|
||||
(this.#target !== undefined &&
|
||||
JSON.stringify(this.#target) !== JSON.stringify(snapshot.target)) ||
|
||||
@@ -123,12 +125,12 @@ export class AnalysisSnapshotCache {
|
||||
for (const entry of snapshot.entries) {
|
||||
if (!this.#entries.has(entry.query_id) && !this.#hasCapacity()) continue;
|
||||
if (!this.#entries.has(entry.query_id)) imported += 1;
|
||||
this.#entries.set(entry.query_id, structuredClone(entry));
|
||||
this.#entries.set(entry.query_id, entry);
|
||||
}
|
||||
for (const entry of snapshot.workflow_entries) {
|
||||
if (!this.#workflowEntries.has(entry.query_id) && !this.#hasCapacity())
|
||||
continue;
|
||||
this.#workflowEntries.set(entry.query_id, structuredClone(entry));
|
||||
this.#workflowEntries.set(entry.query_id, entry);
|
||||
}
|
||||
return imported;
|
||||
}
|
||||
@@ -186,7 +188,7 @@ export class AnalysisSnapshotCache {
|
||||
): Result<number, AnalysisError> {
|
||||
let validated: AnalysisSnapshot;
|
||||
try {
|
||||
validated = parseAnalysisSnapshot(snapshot);
|
||||
validated = immutableAnalysisSnapshot(snapshot);
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
new EvidenceIntegrityError("Analysis snapshot validation failed", {
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { setImmediate } from "node:timers/promises";
|
||||
|
||||
import {
|
||||
parseAnalysisSnapshot,
|
||||
prepareSnapshotEntry,
|
||||
parsePreparedAnalysisSnapshot,
|
||||
type PreparedSnapshotEntry,
|
||||
serializeAnalysisSnapshot,
|
||||
type AnalysisSnapshot,
|
||||
} from "../../domain/analysisSnapshot.js";
|
||||
@@ -9,27 +13,106 @@ import {
|
||||
} from "../../domain/evidenceErrors.js";
|
||||
import { err, ok, type Result } from "../../domain/result.js";
|
||||
import type { AnalysisResourceConstraintError } from "../../domain/analysisErrorCore.js";
|
||||
import { readJsonFile, writeTextFile } from "../JsonFiles.js";
|
||||
import { AnalysisCancelledError } from "../../domain/analysisErrorCore.js";
|
||||
import { readJsonObjectMembersFile, writeTextFile } from "../JsonFiles.js";
|
||||
import type { JsonObjectMemberReader } from "../JsonInputFile.js";
|
||||
|
||||
type SnapshotFailure = EvidenceFileError | EvidenceIntegrityError;
|
||||
|
||||
/** Read and validate an analysis snapshot from the caller's path. */
|
||||
export const readAnalysisSnapshot = async (
|
||||
path: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<
|
||||
Result<AnalysisSnapshot, SnapshotFailure | AnalysisResourceConstraintError>
|
||||
Result<
|
||||
AnalysisSnapshot,
|
||||
SnapshotFailure | AnalysisResourceConstraintError | AnalysisCancelledError
|
||||
>
|
||||
> => {
|
||||
const loaded = await readJsonFile(path);
|
||||
if (!loaded.ok) return loaded;
|
||||
const entries = snapshotEntryReader("provider");
|
||||
const workflows = snapshotEntryReader("workflow");
|
||||
const header: Record<string, unknown> = {};
|
||||
const metadata = new Map<string, JsonObjectMemberReader>(
|
||||
["target", "binding", "evidence_bundle"].map((name) => [
|
||||
name,
|
||||
{
|
||||
mode: "value",
|
||||
begin: () => {
|
||||
delete header[name];
|
||||
},
|
||||
accept: (value) => {
|
||||
header[name] = value;
|
||||
},
|
||||
reject: (cause) => {
|
||||
throw cause;
|
||||
},
|
||||
},
|
||||
]),
|
||||
);
|
||||
try {
|
||||
return ok(parseAnalysisSnapshot(loaded.value));
|
||||
const loaded = await readJsonObjectMembersFile(
|
||||
path,
|
||||
[
|
||||
new Map([
|
||||
["entries", entries.reader],
|
||||
["workflow_entries", workflows.reader],
|
||||
]),
|
||||
metadata,
|
||||
],
|
||||
signal,
|
||||
);
|
||||
if (!loaded.ok) return loaded;
|
||||
signal?.throwIfAborted();
|
||||
const snapshot = parsePreparedAnalysisSnapshot(
|
||||
header,
|
||||
entries.finish(),
|
||||
workflows.finish(),
|
||||
);
|
||||
// Deliver queued cancellation before handing a complete cache to the session.
|
||||
await setImmediate();
|
||||
signal?.throwIfAborted();
|
||||
return ok(snapshot);
|
||||
} catch (cause: unknown) {
|
||||
if (signal?.aborted === true)
|
||||
return err(new AnalysisCancelledError("read_evidence_file", { cause }));
|
||||
return err(
|
||||
new EvidenceIntegrityError(snapshotValidationMessage(cause), { cause }),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const snapshotEntryReader = (kind: "provider" | "workflow") => {
|
||||
let seen = false;
|
||||
let entries: PreparedSnapshotEntry[] = [];
|
||||
let failure: { readonly cause: unknown } | undefined;
|
||||
const reader: JsonObjectMemberReader = {
|
||||
mode: "array",
|
||||
begin: () => {
|
||||
seen = true;
|
||||
entries = [];
|
||||
failure = undefined;
|
||||
},
|
||||
accept: (value) => {
|
||||
if (failure === undefined)
|
||||
entries.push(prepareSnapshotEntry(value, kind));
|
||||
},
|
||||
reject: (cause) => {
|
||||
failure ??= { cause };
|
||||
},
|
||||
};
|
||||
return {
|
||||
reader,
|
||||
finish: (): PreparedSnapshotEntry[] => {
|
||||
if (!seen)
|
||||
throw new TypeError(
|
||||
`Analysis snapshot ${kind === "provider" ? "entries" : "workflow_entries"} is missing`,
|
||||
);
|
||||
if (failure !== undefined) throw failure.cause;
|
||||
return entries;
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
/** Atomically write a deterministic analysis snapshot to the caller's path. */
|
||||
export const writeAnalysisSnapshot = async (
|
||||
snapshot: AnalysisSnapshot,
|
||||
|
||||
Reference in New Issue
Block a user