Skip to content

Commit c65d19d

Browse files
committed
fix(evidence): preserve JSON ownership across metadata rebinding
1 parent 31fc976 commit c65d19d

2 files changed

Lines changed: 68 additions & 10 deletions

File tree

‎src/domain/evidence.test.ts‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@ import {
1313
parseEvidence,
1414
immutableEvidence,
1515
withEvidenceCallerInputPath,
16+
rebindEvidenceToTargetPath,
1617
} from "./evidence.js";
17-
import { MAX_JSON_DEPTH, type JsonValue } from "./jsonValue.js";
18+
import {
19+
MAX_JSON_DEPTH,
20+
jsonValueSchema,
21+
type JsonValue,
22+
} from "./jsonValue.js";
23+
import { AnalysisResourceConstraintError } from "./analysisErrorCore.js";
24+
import { projectAnalysisError } from "./analysisErrorProjection.js";
1825
import { createEvidenceBundle } from "./evidenceBundle.js";
1926
import { freezeJsonSnapshot } from "./immutableJson.js";
2027

@@ -183,6 +190,49 @@ it("retains verbatim caller input without changing semantic identity or copying
183190
expect(() => withEvidenceCallerInputPath(second, "invalid\0path")).toThrow();
184191
});
185192

193+
it.each(["caller selection", "subject display"])(
194+
"preserves sealed JSON ownership when rebinding %s metadata",
195+
(metadata) => {
196+
const original = createEvidence(
197+
TARGET,
198+
PROVIDER,
199+
{
200+
operation: "inspect",
201+
parameters: {},
202+
result: { rows: [{ value: "observed" }] },
203+
rawResult: { source: ["provider"] },
204+
},
205+
{ immutable: true },
206+
);
207+
const rebound =
208+
metadata === "caller selection"
209+
? withEvidenceCallerInputPath(original, "./selected/../alias")
210+
: rebindEvidenceToTargetPath(original, {
211+
...TARGET,
212+
path: "/tmp/selected-alias",
213+
});
214+
const projected = projectAnalysisError(
215+
new AnalysisResourceConstraintError(
216+
"inspect",
217+
"memory",
218+
"low heap",
219+
null,
220+
{
221+
partialObservation: rebound,
222+
},
223+
),
224+
);
225+
expect(rebound).not.toBe(original);
226+
expect(rebound.evidence_id).toBe(original.evidence_id);
227+
expect(rebound.normalized_result).toBe(original.normalized_result);
228+
expect(rebound.raw_result).toBe(original.raw_result);
229+
expect(projected.details?.partial_observation).toBe(rebound);
230+
expect(jsonValueSchema.parse(rebound)).toBe(rebound);
231+
expect(original.caller_input_path).toBeUndefined();
232+
expect(original.subject?.local_path).toBe(TARGET.path);
233+
},
234+
);
235+
186236
it("applies the JSON depth boundary to owned immutable results", () => {
187237
let result: JsonValue = 1;
188238
for (let index = 0; index <= MAX_JSON_DEPTH; index += 1)

‎src/domain/evidence.ts‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
freezeJsonSnapshot,
2727
freezeOwnedJsonTreeSnapshot,
2828
canBorrowParsedJsonSnapshot,
29+
immutableJsonSnapshotMember,
2930
isImmutableJsonSnapshot,
3031
} from "./immutableJson.js";
3132
import { digestSchema } from "./../domain/digests.js";
@@ -355,11 +356,7 @@ export const rebindEvidenceToTargetPath = (
355356
// The original authenticated Evidence owns immutable payloads. Only the
356357
// small display subject and envelope are new, so preserve those payloads
357358
// without reparsing a potentially large analysis result.
358-
Object.freeze(reboundSubject.digest);
359-
Object.freeze(reboundSubject);
360-
Object.freeze(reboundEvidence);
361-
immutableEvidenceSnapshots.set(reboundEvidence, reboundEvidence);
362-
return reboundEvidence;
359+
return rememberReboundEvidence(evidence, reboundEvidence);
363360
};
364361

365362
/** Bind the exact selected request path without changing semantic identity or stored history. */
@@ -372,12 +369,23 @@ export const withEvidenceCallerInputPath = (
372369
if (owned.caller_input_path === callerInputPath) return owned;
373370
// The authenticated source owns its immutable payloads. Only this small
374371
// envelope changes, so replay does not copy a document-sized result.
375-
const rebound = Object.freeze({
372+
const rebound = {
376373
...owned,
377374
caller_input_path: callerInputPath,
378-
});
379-
immutableEvidenceSnapshots.set(rebound, rebound);
380-
return rebound;
375+
};
376+
return rememberReboundEvidence(owned, rebound);
377+
};
378+
379+
/** Carry the source's sealing proof before sealing only the changed envelope. */
380+
const rememberReboundEvidence = (
381+
source: Evidence,
382+
rebound: Evidence,
383+
): Evidence => {
384+
// Each own member belongs to an authenticated immutable source. Register
385+
// these shared roots so sealing the envelope never walks their payloads.
386+
for (const key of Object.keys(source))
387+
immutableJsonSnapshotMember(source, key);
388+
return rememberImmutableEvidence(rebound);
381389
};
382390

383391
/** Recognize authenticated Evidence whose complete reachable JSON data is immutable. */

0 commit comments

Comments
 (0)