perf(snapshot): avoid redundant result copies and serialization
This commit is contained in:
@@ -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