mirror of
https://github.com/morluto/rea
synced 2026-10-27 14:42:18 -04:00
fix(cache): scope Evidence display refresh to matching snapshot bindings
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -13,6 +13,8 @@ import { rebindEvidenceSubjectTargetPath } from "../../domain/evidence.js";
|
||||
import { err, ok, type Result } from "../../domain/result.js";
|
||||
import {
|
||||
analysisQueryId,
|
||||
evidenceMatchesEntry,
|
||||
evidenceMatchesWorkflowEntry,
|
||||
createAnalysisSnapshotEntry,
|
||||
createAnalysisSnapshotWorkflowEntry,
|
||||
snapshotBinding,
|
||||
@@ -35,6 +37,8 @@ import type {
|
||||
import { OFFICIAL_TOOL_CONTRACTS } from "../../contracts/officialToolContracts.js";
|
||||
import { compareUnicodeCodePoints } from "../../domain/unicodeCodePointOrder.js";
|
||||
|
||||
import type { EvidenceDisplayReplacement } from "../investigation/EvidenceLedger.js";
|
||||
|
||||
const RELATIONSHIP_OPERATIONS: ReadonlySet<AnalysisOperation> = new Set([
|
||||
"procedure_callers",
|
||||
"procedure_callees",
|
||||
@@ -148,6 +152,59 @@ export class AnalysisSnapshotCache {
|
||||
return imported;
|
||||
}
|
||||
|
||||
/** Refresh only display bindings backed by the replaced authenticated records. */
|
||||
refreshEvidenceDisplay(
|
||||
replacements: readonly EvidenceDisplayReplacement[],
|
||||
): void {
|
||||
if (this.#target === undefined || this.#binding === undefined) return;
|
||||
const header = { target: this.#target, binding: this.#binding };
|
||||
for (const { previous, current } of replacements) {
|
||||
if (
|
||||
current.subject === null ||
|
||||
previous.subject?.local_path === current.subject.local_path
|
||||
)
|
||||
continue;
|
||||
const queryId = analysisQueryId(
|
||||
header.target,
|
||||
header.binding,
|
||||
previous.operation,
|
||||
previous.parameters,
|
||||
);
|
||||
const entry = this.#entries.get(queryId);
|
||||
if (
|
||||
entry !== undefined &&
|
||||
entry.execution.subject !== null &&
|
||||
evidenceMatchesEntry(previous, entry, header)
|
||||
)
|
||||
this.#entries.set(queryId, {
|
||||
...entry,
|
||||
execution: {
|
||||
...entry.execution,
|
||||
subject: {
|
||||
...entry.execution.subject,
|
||||
path: current.subject.local_path,
|
||||
},
|
||||
},
|
||||
});
|
||||
const workflow = this.#workflowEntries.get(queryId);
|
||||
if (
|
||||
workflow !== undefined &&
|
||||
workflow.execution.subject !== null &&
|
||||
evidenceMatchesWorkflowEntry(previous, workflow, header)
|
||||
)
|
||||
this.#workflowEntries.set(queryId, {
|
||||
...workflow,
|
||||
execution: {
|
||||
...workflow.execution,
|
||||
subject: {
|
||||
...workflow.execution.subject,
|
||||
path: current.subject.local_path,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/** Build a complete snapshot for an active target. */
|
||||
export(
|
||||
target: BinaryTarget | undefined,
|
||||
|
||||
@@ -150,8 +150,8 @@ export abstract class BinarySessionRecords {
|
||||
): Result<number, EvidenceIntegrityError | AnalysisInputError> {
|
||||
const imported = this.#records.mergeEvidenceBundle(bundle);
|
||||
if (!imported.ok) return imported;
|
||||
if (imported.value.metadataChanged) this.invalidateSnapshot();
|
||||
else if (imported.value.changed) this.#emitSnapshotChanged();
|
||||
this.#snapshot.refreshEvidenceDisplay(imported.value.replacements);
|
||||
if (imported.value.changed) this.#emitSnapshotChanged();
|
||||
return ok(imported.value.recordsAdded);
|
||||
}
|
||||
|
||||
@@ -238,7 +238,9 @@ export abstract class BinarySessionRecords {
|
||||
: { target: active.target, profile },
|
||||
(bundle) => {
|
||||
const imported = this.#records.mergeEvidenceBundle(bundle);
|
||||
return imported.ok ? ok(imported.value.recordsAdded) : imported;
|
||||
if (!imported.ok) return imported;
|
||||
this.#snapshot.refreshEvidenceDisplay(imported.value.replacements);
|
||||
return ok(imported.value.recordsAdded);
|
||||
},
|
||||
);
|
||||
if (imported.ok) this.#emitSnapshotChanged();
|
||||
|
||||
Reference in New Issue
Block a user