fix(cache): scope Evidence display refresh to matching snapshot bindings

This commit is contained in:
morluto committed 2026-10-24 01:47:30 +08:00
1 parent f8b65e7872
commit 8aa40e9a61
11 files changed
+327 -113

No files matched your search

@@ -30,6 +30,7 @@ import {
SNAPSHOT_CACHE_ENTRY_CEILING,
isSnapshotCacheable,
} from "./AnalysisSnapshotCache.js";
import { EvidenceLedger } from "../investigation/EvidenceLedger.js";
import { enhancedOutputSchemas } from "../../contracts/toolOutputSchemaGroups.js";
it("exports independent nested query, workflow and Evidence payloads", () => {
@@ -116,6 +117,120 @@ it("exports independent nested query, workflow and Evidence payloads", () => {
expect(serializeAnalysisSnapshot(fresh.value)).toBe(expected);
});
it.each(["provider", "workflow"] as const)(
"refreshes only the authenticated %s display dependency and preserves earlier snapshots",
(kind) => {
const cache = new AnalysisSnapshotCache();
const ledger = new EvidenceLedger();
const target = ANALYSIS_SNAPSHOT_TARGET;
const profile = ANALYSIS_SNAPSHOT_PROFILE;
const parameters = { address: "0x1000", document: "fixture" };
const operation =
kind === "provider" ? "procedure_info" : "analyze_function";
const provider =
kind === "provider" ? ANALYSIS_SNAPSHOT_PROVIDER : REA_WORKFLOW_PROVIDER;
const evidenceProfile =
kind === "provider" ? profile : workflowAnalysisProfile(profile);
const dependencies = [1, 2].map((source) =>
createEvidence(target, provider, {
operation: "source",
parameters: { source },
result: source,
}),
);
for (const dependency of dependencies)
expect(ledger.record(dependency).ok).toBe(true);
const evidence = createEvidence(target, provider, {
operation,
parameters,
evidenceLinks: dependencies.map(({ evidence_id }) => evidence_id),
result: "original",
analysisProfile: evidenceProfile,
confidence: kind === "provider" ? "observed" : "derived",
});
const execution = createAnalysisExecution("original", provider, {
subject: target,
analysisProfile: evidenceProfile,
});
if (kind === "provider")
cache.record({ target, profile, operation, parameters, execution });
else
cache.recordWorkflow({
target,
profile,
operation,
parameters,
execution: { ...execution, analysisProfile: evidenceProfile },
});
expect(ledger.record(evidence).ok).toBe(true);
const before = cache.export(target, profile, ledger.export());
if (!before.ok) throw before.error;
const nameOnly = {
...evidence,
subject:
evidence.subject === null
? null
: { ...evidence.subject, name: "display-only-name" },
};
const nameImport = ledger.import(createEvidenceBundle([nameOnly]));
if (!nameImport.ok) throw nameImport.error;
cache.refreshEvidenceDisplay(nameImport.value.replacements);
expect(cache.entries()).toEqual(before.value.entries);
expect(cache.workflowEntries()).toEqual(before.value.workflow_entries);
// A different observation for this exact query is not the entry's dependency.
const alternative = createEvidence(target, provider, {
operation,
parameters,
result: "alternative",
analysisProfile: evidenceProfile,
confidence: kind === "provider" ? "observed" : "derived",
});
expect(ledger.record(alternative).ok).toBe(true);
const relocate = (record: typeof evidence) => {
if (record.subject === null) throw new Error("Expected fixture subject");
return {
...record,
subject: {
...record.subject,
local_path: "/relocated/fixture",
name: "renamed",
},
};
};
const foreign = ledger.import(
createEvidenceBundle([relocate(alternative)]),
);
if (!foreign.ok) throw foreign.error;
cache.refreshEvidenceDisplay(foreign.value.replacements);
expect(cache.entries()).toEqual(before.value.entries);
expect(cache.workflowEntries()).toEqual(before.value.workflow_entries);
// A result may cite several observations; updating their envelopes does not change its payload.
const dependentImport = ledger.import(
createEvidenceBundle(dependencies.map(relocate)),
);
if (!dependentImport.ok) throw dependentImport.error;
cache.refreshEvidenceDisplay(dependentImport.value.replacements);
expect(cache.entries()).toEqual(before.value.entries);
expect(cache.workflowEntries()).toEqual(before.value.workflow_entries);
const relocated = relocate(evidence);
const imported = ledger.import(createEvidenceBundle([relocated]));
if (!imported.ok) throw imported.error;
cache.refreshEvidenceDisplay(imported.value.replacements);
const after = cache.export(target, profile, ledger.export());
if (!after.ok) throw after.error;
const entries =
kind === "provider" ? after.value.entries : after.value.workflow_entries;
expect(entries[0]?.execution.subject?.path).toBe("/relocated/fixture");
expect(entries[0]?.execution.result).toBe("original");
expect(after.value.evidence_bundle.records).toContainEqual(relocated);
const originalEntries =
kind === "provider"
? before.value.entries
: before.value.workflow_entries;
expect(originalEntries[0]?.execution.subject?.path).toBe(target.path);
},
);
it("counts newly retained direct and workflow bindings when staging", () => {
const source = new AnalysisSnapshotCache();
const target = ANALYSIS_SNAPSHOT_TARGET;