perf(evidence): reuse authenticated unknown mutation snapshots

This commit is contained in:
morluto
2026-10-13 12:46:06 +08:00
parent b045df100e
commit 042dcaa1ee
3 changed files with 57 additions and 17 deletions
@@ -78,6 +78,51 @@ const update = (
}); });
describe("residual unknown registry reads and identity", () => { describe("residual unknown registry reads and identity", () => {
it("seals mutation Evidence without freezing caller-owned inputs", () => {
const store = ledger();
const recordMutation = mutation("mutable-record-mutation");
const recorded = store.recordUnknown(
input("Does record mutation evidence stay stable?"),
recordMutation,
);
if (!recorded.ok) throw recorded.error;
const updateMutation = mutation("mutable-update-mutation");
const updated = store.updateUnknown(update(recorded.value), updateMutation);
if (!updated.ok) throw updated.error;
const atomicEvidence = evidence("mutable-atomic-evidence");
const atomicMutation = mutation("mutable-atomic-mutation");
const atomicallyRecorded = store.recordWithUnknown(
atomicEvidence,
input("Does atomic evidence stay stable?"),
atomicMutation,
);
if (!atomicallyRecorded.ok) throw atomicallyRecorded.error;
for (const callerEvidence of [
recordMutation,
updateMutation,
atomicEvidence,
atomicMutation,
]) {
expect(Object.isFrozen(callerEvidence)).toBe(false);
const retained = store.forAnalysis(callerEvidence.evidence_id);
if (retained === undefined) throw new Error("Missing retained Evidence");
const originalResult = structuredClone(retained.normalized_result);
expect(Object.isFrozen(retained)).toBe(true);
Reflect.set(callerEvidence, "operation", "caller mutation");
expect(
Reflect.set(
callerEvidence.normalized_result as object,
"label",
"caller nested mutation",
),
).toBe(true);
expect(retained.operation).not.toBe("caller mutation");
expect(retained.normalized_result).toEqual(originalResult);
}
});
it("merges record-only bundles without changing retained unknown history", () => { it("merges record-only bundles without changing retained unknown history", () => {
const store = ledger(); const store = ledger();
const created = store.recordUnknown( const created = store.recordUnknown(
@@ -11,11 +11,7 @@ import {
import { AnalysisInputError } from "../../domain/analysisErrorCore.js"; import { AnalysisInputError } from "../../domain/analysisErrorCore.js";
import { EvidenceIntegrityError } from "../../domain/evidenceErrors.js"; import { EvidenceIntegrityError } from "../../domain/evidenceErrors.js";
import { UnknownRegistryError } from "../../domain/unknownRegistryError.js"; import { UnknownRegistryError } from "../../domain/unknownRegistryError.js";
import { import { immutableEvidence, type Evidence } from "../../domain/evidence.js";
immutableEvidence,
parseEvidence,
type Evidence,
} from "../../domain/evidence.js";
import { import {
createResidualUnknown, createResidualUnknown,
updateResidualUnknown, updateResidualUnknown,
@@ -220,7 +216,7 @@ export class EvidenceLedger {
): Result<ResidualUnknown, EvidenceIntegrityError | UnknownRegistryError> { ): Result<ResidualUnknown, EvidenceIntegrityError | UnknownRegistryError> {
let parsedMutation: Evidence; let parsedMutation: Evidence;
try { try {
parsedMutation = parseEvidence(mutationEvidence); parsedMutation = immutableEvidence(mutationEvidence);
} catch (cause: unknown) { } catch (cause: unknown) {
return err( return err(
new EvidenceIntegrityError("Unknown mutation evidence is invalid", { new EvidenceIntegrityError("Unknown mutation evidence is invalid", {
@@ -255,8 +251,8 @@ export class EvidenceLedger {
let evidence: Evidence; let evidence: Evidence;
let mutation: Evidence; let mutation: Evidence;
try { try {
evidence = parseEvidence(evidenceInput); evidence = immutableEvidence(evidenceInput);
mutation = parseEvidence(mutationInput); mutation = immutableEvidence(mutationInput);
} catch (cause: unknown) { } catch (cause: unknown) {
return err( return err(
new EvidenceIntegrityError("Atomic Evidence validation failed", { new EvidenceIntegrityError("Atomic Evidence validation failed", {
@@ -394,7 +390,7 @@ export class EvidenceLedger {
): Result<ResidualUnknown, EvidenceIntegrityError | UnknownRegistryError> { ): Result<ResidualUnknown, EvidenceIntegrityError | UnknownRegistryError> {
let mutationEvidence: Evidence; let mutationEvidence: Evidence;
try { try {
mutationEvidence = parseEvidence(mutationEvidenceInput); mutationEvidence = immutableEvidence(mutationEvidenceInput);
} catch (cause: unknown) { } catch (cause: unknown) {
return err( return err(
new EvidenceIntegrityError("Unknown mutation evidence is invalid", { new EvidenceIntegrityError("Unknown mutation evidence is invalid", {
+7 -8
View File
@@ -281,7 +281,7 @@ it("pages filtered unknown heads through the public MCP contract", async () => {
supporting_evidence_ids: [], supporting_evidence_ids: [],
contradicting_evidence_ids: [], contradicting_evidence_ids: [],
}); });
expect(result.ok).toBe(true); if (!result.ok) throw result.error;
} }
const defaultPage = structured( const defaultPage = structured(
@@ -372,12 +372,11 @@ it("pages filtered unknown heads through the public MCP contract", async () => {
.result as typeof defaultPage; .result as typeof defaultPage;
expect(first).toMatchObject({ total: 3, offset: 0, next_offset: 2 }); expect(first).toMatchObject({ total: 3, offset: 0, next_offset: 2 });
expect(first.items).toHaveLength(2); expect(first.items).toHaveLength(2);
const second = structured( const secondResponse = await client.callTool({
await client.callTool({ ...request,
...request, arguments: { ...request.arguments, offset: first.next_offset ?? 0 },
arguments: { ...request.arguments, offset: first.next_offset ?? 0 }, });
}), const second = structured(secondResponse).result as typeof defaultPage;
).result as typeof defaultPage;
expect(second).toMatchObject({ total: 3, offset: 2, next_offset: null }); expect(second).toMatchObject({ total: 3, offset: 2, next_offset: null });
expect(second.items).toHaveLength(1); expect(second.items).toHaveLength(1);
expect( expect(
@@ -389,7 +388,7 @@ it("pages filtered unknown heads through the public MCP contract", async () => {
); );
expect( expect(
toolContract("list_unknowns").outputSchema.safeParse( toolContract("list_unknowns").outputSchema.safeParse(
(await client.callTool(request)).structuredContent, secondResponse.structuredContent,
).success, ).success,
).toBe(true); ).toBe(true);