perf(snapshot): avoid redundant result copies and serialization
This commit is contained in:
@@ -13,6 +13,7 @@ import { createAnalysisExecution } from "../AnalysisProvider.js";
|
||||
import {
|
||||
createAnalysisSnapshotWorkflowEntry,
|
||||
parseAnalysisSnapshot,
|
||||
serializeAnalysisSnapshot,
|
||||
snapshotBinding,
|
||||
snapshotTarget,
|
||||
} from "../../domain/analysisSnapshot.js";
|
||||
@@ -30,6 +31,89 @@ import {
|
||||
isSnapshotCacheable,
|
||||
} from "./AnalysisSnapshotCache.js";
|
||||
|
||||
it("exports independent nested query, workflow and Evidence payloads", () => {
|
||||
const cache = new AnalysisSnapshotCache();
|
||||
const target = ANALYSIS_SNAPSHOT_TARGET;
|
||||
const profile = ANALYSIS_SNAPSHOT_PROFILE;
|
||||
const result = { nested: [{ text: "ret" }], name: "main" };
|
||||
const parameters = { address: "0x1000", document: "fixture" };
|
||||
const workflowProfile = workflowAnalysisProfile(profile);
|
||||
cache.record({
|
||||
target,
|
||||
profile,
|
||||
operation: "procedure_info",
|
||||
parameters,
|
||||
execution: createAnalysisExecution(result, ANALYSIS_SNAPSHOT_PROVIDER, {
|
||||
subject: target,
|
||||
analysisProfile: profile,
|
||||
rawResult: result,
|
||||
}),
|
||||
});
|
||||
cache.recordWorkflow({
|
||||
target,
|
||||
profile,
|
||||
operation: "analyze_function",
|
||||
parameters,
|
||||
execution: {
|
||||
...createAnalysisExecution(result, REA_WORKFLOW_PROVIDER, {
|
||||
subject: target,
|
||||
rawResult: result,
|
||||
}),
|
||||
analysisProfile: workflowProfile,
|
||||
},
|
||||
});
|
||||
const bundle = createEvidenceBundle([
|
||||
createEvidence(target, ANALYSIS_SNAPSHOT_PROVIDER, {
|
||||
operation: "procedure_info",
|
||||
parameters,
|
||||
result,
|
||||
rawResult: result,
|
||||
analysisProfile: profile,
|
||||
}),
|
||||
createEvidence(target, REA_WORKFLOW_PROVIDER, {
|
||||
operation: "analyze_function",
|
||||
parameters,
|
||||
result,
|
||||
rawResult: result,
|
||||
analysisProfile: workflowProfile,
|
||||
}),
|
||||
]);
|
||||
const exported = cache.export(target, profile, bundle);
|
||||
if (!exported.ok) throw exported.error;
|
||||
const expected = serializeAnalysisSnapshot(exported.value);
|
||||
const changed = JSON.stringify(exported.value).replaceAll(
|
||||
'"ret"',
|
||||
'"changed"',
|
||||
);
|
||||
// Mutate the returned graph itself, including every duplicated JSON result.
|
||||
for (const payload of [
|
||||
...exported.value.entries.flatMap(({ execution }) => [
|
||||
execution.result,
|
||||
execution.raw_result,
|
||||
]),
|
||||
...(exported.value.workflow_entries ?? []).flatMap(({ execution }) => [
|
||||
execution.result,
|
||||
execution.raw_result,
|
||||
]),
|
||||
...exported.value.evidence_bundle.records.flatMap((record) => [
|
||||
record.normalized_result,
|
||||
record.raw_result,
|
||||
]),
|
||||
]) {
|
||||
if (
|
||||
typeof payload !== "object" ||
|
||||
payload === null ||
|
||||
Array.isArray(payload)
|
||||
)
|
||||
throw new Error("Expected nested fixture result");
|
||||
Reflect.set(payload, "nested", [{ text: "changed" }]);
|
||||
}
|
||||
expect(JSON.stringify(exported.value)).toBe(changed);
|
||||
const fresh = cache.export(target, profile, bundle);
|
||||
if (!fresh.ok) throw fresh.error;
|
||||
expect(serializeAnalysisSnapshot(fresh.value)).toBe(expected);
|
||||
});
|
||||
|
||||
describe("analysis snapshot cache capacity", () => {
|
||||
it("shares capacity across query kinds while allowing replacements and fresh partitions", () => {
|
||||
const cache = new AnalysisSnapshotCache();
|
||||
|
||||
@@ -143,16 +143,21 @@ export class AnalysisSnapshotCache {
|
||||
try {
|
||||
const snapshotTargetIdentity = snapshotTarget(target);
|
||||
const binding = snapshotBinding(profile);
|
||||
const retainedEvidence = structuredClone(evidenceBundle);
|
||||
const entries = [...this.#entries.values()].sort((left, right) =>
|
||||
left.query_id.localeCompare(right.query_id),
|
||||
);
|
||||
const workflows = [...this.#workflowEntries.values()].sort(
|
||||
(left, right) => left.query_id.localeCompare(right.query_id),
|
||||
);
|
||||
// Parsing owns the returned JSON and metadata; pre-cloning the same
|
||||
// payloads here only adds another full materialization.
|
||||
return ok(
|
||||
parseAnalysisSnapshot({
|
||||
target: snapshotTargetIdentity,
|
||||
binding,
|
||||
entries: this.entries(),
|
||||
...(this.workflowEntries().length === 0
|
||||
? {}
|
||||
: { workflow_entries: this.workflowEntries() }),
|
||||
evidence_bundle: retainedEvidence,
|
||||
entries,
|
||||
...(workflows.length === 0 ? {} : { workflow_entries: workflows }),
|
||||
evidence_bundle: evidenceBundle,
|
||||
}),
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
|
||||
@@ -138,6 +138,72 @@ describe("analysis snapshot contract", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("analysis snapshot canonical ordering and scope", () => {
|
||||
it("rejects reordered queries and foreign-target records while retaining legacy Evidence", () => {
|
||||
const { snapshot } = snapshotWithHistoricalEvidence();
|
||||
const first = snapshot.entries[0];
|
||||
if (first === undefined) throw new Error("Missing fixture query");
|
||||
const parameters = { procedure: "other" };
|
||||
const second = {
|
||||
...first,
|
||||
parameters,
|
||||
query_id: analysisQueryId(
|
||||
snapshot.target,
|
||||
snapshot.binding,
|
||||
first.operation,
|
||||
parameters,
|
||||
),
|
||||
};
|
||||
const entries = [first, second].sort((left, right) =>
|
||||
left.query_id.localeCompare(right.query_id),
|
||||
);
|
||||
const canonical = { ...snapshot, entries };
|
||||
expect(parseAnalysisSnapshot(canonical).entries).toEqual([first]);
|
||||
expect(() =>
|
||||
parseAnalysisSnapshot({ ...canonical, entries: [...entries].reverse() }),
|
||||
).toThrow(/entries are not canonical/u);
|
||||
const workflows = entries.map((entry) => ({
|
||||
...entry,
|
||||
execution: {
|
||||
...entry.execution,
|
||||
analysis_profile: ANALYSIS_SNAPSHOT_PROFILE,
|
||||
},
|
||||
}));
|
||||
expect(
|
||||
parseAnalysisSnapshot({
|
||||
...snapshot,
|
||||
entries: [],
|
||||
workflow_entries: workflows,
|
||||
}).workflow_entries,
|
||||
).toEqual([]);
|
||||
expect(() =>
|
||||
parseAnalysisSnapshot({
|
||||
...snapshot,
|
||||
entries: [],
|
||||
workflow_entries: [...workflows].reverse(),
|
||||
}),
|
||||
).toThrow(/workflow entries are not canonical/u);
|
||||
const foreign = createEvidence(
|
||||
{ ...ANALYSIS_SNAPSHOT_TARGET, sha256: "b".repeat(64) },
|
||||
ANALYSIS_SNAPSHOT_PROVIDER,
|
||||
{
|
||||
operation: "legacy_query",
|
||||
parameters: {},
|
||||
result: null,
|
||||
},
|
||||
);
|
||||
expect(() =>
|
||||
parseAnalysisSnapshot({
|
||||
...snapshot,
|
||||
evidence_bundle: createEvidenceBundle([
|
||||
...snapshot.evidence_bundle.records,
|
||||
foreign,
|
||||
]),
|
||||
}),
|
||||
).toThrow(/another target/u);
|
||||
});
|
||||
});
|
||||
|
||||
describe("analysis snapshot Evidence binding", () => {
|
||||
it("finds only Evidence committed to the exact binding and profile", () => {
|
||||
const { current, snapshot } = snapshotWithHistoricalEvidence();
|
||||
|
||||
@@ -372,10 +372,15 @@ export const createAnalysisSnapshotEntry = (input: {
|
||||
export const parseAnalysisSnapshot = (input: unknown): AnalysisSnapshot => {
|
||||
const parsed = analysisSnapshotSchema.parse(input);
|
||||
parseEvidenceBundle(parsed.evidence_bundle);
|
||||
const scopedEvidence = evidenceBundleForTarget(
|
||||
parsed.evidence_bundle,
|
||||
parsed.target.sha256,
|
||||
);
|
||||
// The validated canonical bundle is filtered, never extended. Equal counts
|
||||
// establish that no record or unknown was removed without serializing results.
|
||||
if (
|
||||
JSON.stringify(
|
||||
evidenceBundleForTarget(parsed.evidence_bundle, parsed.target.sha256),
|
||||
) !== JSON.stringify(parsed.evidence_bundle)
|
||||
scopedEvidence.records.length !== parsed.evidence_bundle.records.length ||
|
||||
scopedEvidence.unknowns.length !== parsed.evidence_bundle.unknowns.length
|
||||
)
|
||||
throw new TypeError(
|
||||
"Analysis snapshot evidence contains records for another target",
|
||||
@@ -448,17 +453,11 @@ export const parseAnalysisSnapshot = (input: unknown): AnalysisSnapshot => {
|
||||
}
|
||||
boundWorkflowEntries.push(entry);
|
||||
}
|
||||
const sorted = [...parsed.entries].sort((left, right) =>
|
||||
left.query_id.localeCompare(right.query_id),
|
||||
);
|
||||
if (JSON.stringify(parsed.entries) !== JSON.stringify(sorted))
|
||||
if (!hasCanonicalQueryOrder(parsed.entries))
|
||||
throw new TypeError("Analysis snapshot entries are not canonical");
|
||||
const sortedWorkflows = [...(parsed.workflow_entries ?? [])].sort(
|
||||
(left, right) => left.query_id.localeCompare(right.query_id),
|
||||
);
|
||||
if (
|
||||
parsed.workflow_entries !== undefined &&
|
||||
JSON.stringify(parsed.workflow_entries) !== JSON.stringify(sortedWorkflows)
|
||||
!hasCanonicalQueryOrder(parsed.workflow_entries)
|
||||
)
|
||||
throw new TypeError("Analysis snapshot workflow entries are not canonical");
|
||||
return {
|
||||
@@ -470,6 +469,18 @@ export const parseAnalysisSnapshot = (input: unknown): AnalysisSnapshot => {
|
||||
};
|
||||
};
|
||||
|
||||
const hasCanonicalQueryOrder = (
|
||||
entries: readonly { readonly query_id: string }[],
|
||||
): boolean => {
|
||||
let previous: string | undefined;
|
||||
for (const { query_id: queryId } of entries) {
|
||||
if (previous !== undefined && previous.localeCompare(queryId) > 0)
|
||||
return false;
|
||||
previous = queryId;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
/** Check that a cached provider execution is represented by bundled Evidence. */
|
||||
interface EvidenceIndex {
|
||||
readonly matching: ReadonlySet<string>;
|
||||
|
||||
Reference in New Issue
Block a user