fix(snapshot): bind replayed subjects to the selected target path
Rebind display paths only for matching artifact identities in primitive and composed replay. Preserve semantic Evidence IDs, payloads, and historical snapshot provenance. Classify snapshot filesystem workflows as boundary tests. Validation: cache, direct workflow, MCP replay, and Evidence tests; combined checks and temporary-directory hygiene. Provider interactions are simulated for replay verification.
This commit is contained in:
@@ -115,6 +115,63 @@ it("exports independent nested query, workflow and Evidence payloads", () => {
|
||||
expect(serializeAnalysisSnapshot(fresh.value)).toBe(expected);
|
||||
});
|
||||
|
||||
it("rebinds replayed target subjects while preserving child artifact subjects", () => {
|
||||
const cache = new AnalysisSnapshotCache();
|
||||
const target = ANALYSIS_SNAPSHOT_TARGET;
|
||||
const profile = ANALYSIS_SNAPSHOT_PROFILE;
|
||||
const relocatedTarget = { ...target, path: "/tmp/relocated-app" };
|
||||
const parameters = { address: "0x1000" };
|
||||
const locations = [{ kind: "address" as const, address: "0x1000" }];
|
||||
cache.record({
|
||||
target,
|
||||
profile,
|
||||
operation: "procedure_info",
|
||||
parameters,
|
||||
execution: createAnalysisExecution(
|
||||
{ name: "main" },
|
||||
ANALYSIS_SNAPSHOT_PROVIDER,
|
||||
{
|
||||
subject: target,
|
||||
rawResult: { provider: "raw" },
|
||||
locations,
|
||||
analysisProfile: profile,
|
||||
},
|
||||
),
|
||||
});
|
||||
|
||||
const replayedTarget = cache.lookup(
|
||||
relocatedTarget,
|
||||
profile,
|
||||
"procedure_info",
|
||||
parameters,
|
||||
);
|
||||
expect(replayedTarget?.subject?.path).toBe(relocatedTarget.path);
|
||||
expect(replayedTarget?.rawResult).toEqual({ provider: "raw" });
|
||||
expect(replayedTarget?.locations).toEqual(locations);
|
||||
|
||||
const childSubject = {
|
||||
path: "/tmp/child.zip",
|
||||
sha256: "b".repeat(64),
|
||||
format: "zip" as const,
|
||||
};
|
||||
const childParameters = { address: "0x2000" };
|
||||
cache.record({
|
||||
target,
|
||||
profile,
|
||||
operation: "procedure_info",
|
||||
parameters: childParameters,
|
||||
execution: createAnalysisExecution(
|
||||
{ name: "child" },
|
||||
ANALYSIS_SNAPSHOT_PROVIDER,
|
||||
{ subject: childSubject, analysisProfile: profile },
|
||||
),
|
||||
});
|
||||
expect(
|
||||
cache.lookup(relocatedTarget, profile, "procedure_info", childParameters)
|
||||
?.subject,
|
||||
).toMatchObject(childSubject);
|
||||
});
|
||||
|
||||
describe("analysis snapshot cache capacity", () => {
|
||||
it("shares capacity across query kinds while allowing replacements and fresh partitions", () => {
|
||||
const cache = new AnalysisSnapshotCache();
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
} from "../../domain/evidenceErrors.js";
|
||||
import { NoBinaryOpenError } from "../../domain/configurationErrors.js";
|
||||
import type { AnalysisError } from "../../domain/analysisErrorBase.js";
|
||||
import { rebindEvidenceSubjectTargetPath } from "../../domain/evidence.js";
|
||||
import { err, ok, type Result } from "../../domain/result.js";
|
||||
import {
|
||||
analysisQueryId,
|
||||
@@ -277,7 +278,21 @@ export class AnalysisSnapshotCache {
|
||||
);
|
||||
const cached = this.#entries.get(queryId);
|
||||
if (cached === undefined) return undefined;
|
||||
const subject = cached.execution.subject;
|
||||
const cachedSubject = cached.execution.subject;
|
||||
const subject =
|
||||
cachedSubject === null
|
||||
? null
|
||||
: rebindEvidenceSubjectTargetPath(
|
||||
{
|
||||
path: cachedSubject.path,
|
||||
sha256: cachedSubject.sha256,
|
||||
format: cachedSubject.format,
|
||||
...(cachedSubject.architecture === null
|
||||
? {}
|
||||
: { architecture: cachedSubject.architecture }),
|
||||
},
|
||||
target,
|
||||
);
|
||||
return structuredClone({
|
||||
result: cached.execution.result,
|
||||
rawResult: cached.execution.raw_result,
|
||||
@@ -288,21 +303,7 @@ export class AnalysisSnapshotCache {
|
||||
"Loaded from a local REA analysis snapshot; this call did not re-run the provider.",
|
||||
],
|
||||
locations: cached.execution.locations,
|
||||
subject:
|
||||
subject === null
|
||||
? null
|
||||
: subject.architecture === null
|
||||
? {
|
||||
path: subject.path,
|
||||
sha256: subject.sha256,
|
||||
format: subject.format,
|
||||
}
|
||||
: {
|
||||
path: subject.path,
|
||||
sha256: subject.sha256,
|
||||
format: subject.format,
|
||||
architecture: subject.architecture,
|
||||
},
|
||||
subject,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
} from "./evidenceBundle.js";
|
||||
import {
|
||||
evidenceLocationSchema,
|
||||
rebindEvidenceToTargetPath,
|
||||
type Evidence,
|
||||
type EvidenceLocation,
|
||||
type EvidenceSubjectTarget,
|
||||
@@ -411,7 +412,7 @@ export const snapshotEvidenceForQuery = (
|
||||
(candidate) => candidate.query_id === queryId,
|
||||
);
|
||||
if (entry === undefined && workflowEntry === undefined) return undefined;
|
||||
return snapshot.evidence_bundle.records.find(
|
||||
const evidence = snapshot.evidence_bundle.records.find(
|
||||
(record) =>
|
||||
record.subject?.digest.sha256 === target.sha256 &&
|
||||
record.operation === operation &&
|
||||
@@ -425,6 +426,9 @@ export const snapshotEvidenceForQuery = (
|
||||
(workflowEntry !== undefined &&
|
||||
evidenceMatchesWorkflowEntry(record, workflowEntry, snapshot))),
|
||||
);
|
||||
return evidence === undefined
|
||||
? undefined
|
||||
: rebindEvidenceToTargetPath(evidence, target);
|
||||
};
|
||||
|
||||
/** Create one exact replay record for a derived application workflow result. */
|
||||
|
||||
+51
-1
@@ -1,3 +1,4 @@
|
||||
import { basename } from "node:path";
|
||||
import { z } from "zod";
|
||||
|
||||
import { digestCanonicalValue } from "./canonicalDigest.js";
|
||||
@@ -182,6 +183,18 @@ export interface EvidenceSubjectTarget {
|
||||
readonly format: z.infer<typeof subjectSchema>["format"];
|
||||
readonly architecture?: BinaryArchitecture;
|
||||
}
|
||||
|
||||
/** Rebind a matching immutable artifact subject to the caller's selected path. */
|
||||
export const rebindEvidenceSubjectTargetPath = (
|
||||
subject: EvidenceSubjectTarget,
|
||||
target: EvidenceSubjectTarget,
|
||||
): EvidenceSubjectTarget =>
|
||||
subject.sha256 === target.sha256 &&
|
||||
subject.format === target.format &&
|
||||
(subject.architecture ?? null) === (target.architecture ?? null)
|
||||
? { ...subject, path: target.path }
|
||||
: subject;
|
||||
|
||||
type EvidenceAuthority = z.infer<typeof evidenceAuthoritySchema>;
|
||||
type ExecutionEnvironment = z.infer<typeof executionEnvironmentSchema>;
|
||||
|
||||
@@ -252,6 +265,43 @@ export const parseEvidence = (input: unknown): Evidence => {
|
||||
export const immutableEvidence = (input: unknown): Evidence =>
|
||||
rememberImmutableEvidence(parseEvidence(input));
|
||||
|
||||
/** Rebind display path metadata when an authenticated record names this target. */
|
||||
export const rebindEvidenceToTargetPath = (
|
||||
evidence: Evidence,
|
||||
target: EvidenceSubjectTarget,
|
||||
): Evidence => {
|
||||
const subject = evidence.subject;
|
||||
if (subject === null) return evidence;
|
||||
const rebound = rebindEvidenceSubjectTargetPath(
|
||||
{
|
||||
path: subject.local_path,
|
||||
sha256: subject.digest.sha256,
|
||||
format: subject.format,
|
||||
...(subject.architecture === null
|
||||
? {}
|
||||
: { architecture: subject.architecture }),
|
||||
},
|
||||
target,
|
||||
);
|
||||
if (rebound.path === subject.local_path) return evidence;
|
||||
const reboundSubject = subjectSchema.parse({
|
||||
...subject,
|
||||
name: basename(target.path) || "artifact",
|
||||
local_path: target.path,
|
||||
});
|
||||
const reboundEvidence = { ...evidence, subject: reboundSubject };
|
||||
if (!isImmutableEvidence(evidence)) return immutableEvidence(reboundEvidence);
|
||||
|
||||
// The original authenticated Evidence owns immutable payloads. Only the
|
||||
// small display subject and envelope are new, so preserve those payloads
|
||||
// without reparsing a potentially large analysis result.
|
||||
Object.freeze(reboundSubject.digest);
|
||||
Object.freeze(reboundSubject);
|
||||
Object.freeze(reboundEvidence);
|
||||
immutableEvidenceSnapshots.set(reboundEvidence, reboundEvidence);
|
||||
return reboundEvidence;
|
||||
};
|
||||
|
||||
/** Recognize authenticated Evidence whose complete reachable JSON data is immutable. */
|
||||
export const isImmutableEvidence = (evidence: Evidence): boolean =>
|
||||
immutableEvidenceSnapshots.has(evidence);
|
||||
@@ -316,7 +366,7 @@ const normalizeEvidenceObservation = (
|
||||
target === undefined
|
||||
? null
|
||||
: {
|
||||
name: target.path.split("/").at(-1) || "artifact",
|
||||
name: basename(target.path) || "artifact",
|
||||
digest: { sha256: target.sha256 },
|
||||
format: target.format,
|
||||
architecture: target.architecture ?? null,
|
||||
|
||||
Reference in New Issue
Block a user