From e9bfd51266370f4cdc55e4e289746014cb787134 Mon Sep 17 00:00:00 2001 From: gensokyo-dev Date: Tue, 24 Nov 2026 09:25:17 +0000 Subject: [PATCH] perf(artifacts): keep the inspection's inventory Evidence sealed inspect_artifact nests the inventory_artifact Evidence in its result. The nested Evidence was created unsealed, so createArtifactInspection parsed and authenticated it again, and the result schema then parsed the whole embedded inventory a second time, producing an unsealed copy. When the session recorded that copy as a source, the ledger authenticated it a third time, recomputing its evidence_id over the complete inventory. The inventory Evidence is now created sealed. createArtifactInspection seals what it receives with immutableEvidence, which borrows a sealed snapshot, and checks each substep's Evidence by seal instead of parsing the inventory again, so the recorded source is that same snapshot. The advertised result schema is unchanged. Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> --- src/artifacts/ArtifactProvider.ts | 25 ++++++++++------ src/domain/artifactInspection.ts | 50 ++++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/src/artifacts/ArtifactProvider.ts b/src/artifacts/ArtifactProvider.ts index 84e544fbac..044ee01bf6 100644 --- a/src/artifacts/ArtifactProvider.ts +++ b/src/artifacts/ArtifactProvider.ts @@ -526,15 +526,22 @@ class ArtifactClient implements AnalysisClient { await this.inventory(inventoryParameters, options), ); const subject = subjectFor(this.target, inventory.manifest); - const inventoryEvidence = createEvidence(subject, ARTIFACT_GRAPH_PROVIDER, { - operation: "inventory_artifact", - parameters: inventoryParameters, - result: inventory, - rawResult: null, - limitations: inventory.limitations, - // The nested inventory already names every occurrence's logical path. - locations: [], - }); + // Sealed at creation, the nested Evidence is borrowed when the session + // records it as a source, rather than parsed and digested again. + const inventoryEvidence = createEvidence( + subject, + ARTIFACT_GRAPH_PROVIDER, + { + operation: "inventory_artifact", + parameters: inventoryParameters, + result: inventory, + rawResult: null, + limitations: inventory.limitations, + // The nested inventory already names every occurrence's logical path. + locations: [], + }, + { immutable: true }, + ); // The returned execution and its Evidence then reuse this owned report. const result = freezeJsonSnapshot( createArtifactInspection( diff --git a/src/domain/artifactInspection.ts b/src/domain/artifactInspection.ts index 670d974378..ccac8aeaf7 100644 --- a/src/domain/artifactInspection.ts +++ b/src/domain/artifactInspection.ts @@ -25,7 +25,12 @@ import { type StandaloneArtifactFormat, } from "./artifactFormatRecognition.js"; import { uniqueSorted } from "./canonicalOrdering.js"; -import { evidenceSchema, parseEvidence, type Evidence } from "./evidence.js"; +import { + evidenceSchema, + immutableEvidence, + isImmutableEvidence, + type Evidence, +} from "./evidence.js"; import { offsetText } from "./firmware/espImageStructures.js"; import { jsonValueSchema } from "./jsonValue.js"; import { prefixedDigestSchema } from "./../domain/digests.js"; @@ -89,6 +94,13 @@ const unexploredBranchSchema = z.strictObject({ }); /** Provider-neutral inspection result with its atomic source Evidence. */ +const inspectionSubstepFields = { + substep_id: prefixedDigestSchema("ais"), + operation: z.literal("inventory_artifact"), + status: z.literal("completed"), + evidence_id: evidenceIdSchema, +} as const; + export const artifactInspectionResultSchema = z.strictObject({ inspection_id: prefixedDigestSchema("ai"), subject: z.strictObject({ @@ -100,10 +112,7 @@ export const artifactInspectionResultSchema = z.strictObject({ substeps: z .array( z.strictObject({ - substep_id: prefixedDigestSchema("ais"), - operation: z.literal("inventory_artifact"), - status: z.literal("completed"), - evidence_id: evidenceIdSchema, + ...inspectionSubstepFields, evidence: evidenceSchema.safeExtend({ operation: z.literal("inventory_artifact"), normalized_result: artifactInventoryResultSchema, @@ -231,14 +240,41 @@ export const createArtifactInspection = ( "Inspection derives bounded static hypotheses and next probes; it does not execute the artifact or claim format-specific semantics.", ]), }; - return artifactInspectionResultSchema.parse({ + return constructedInspectionSchema.parse({ ...semantic, inspection_id: `ai_${digestCanonicalValue(semantic, "Artifact inspection")}`, }); }; +/** + * The result as constructed here: each substep holds the sealed inventory + * Evidence authenticated above. Checking it by seal keeps that snapshot + * instead of parsing and copying the complete inventory again, so a session + * that records it as a source borrows it too. + */ +const constructedInspectionSchema = artifactInspectionResultSchema.safeExtend({ + substeps: z + .array( + z.strictObject({ + ...inspectionSubstepFields, + evidence: z.custom< + z.infer< + typeof artifactInspectionResultSchema + >["substeps"][number]["evidence"] + >( + (value) => + isImmutableEvidence(value as Evidence) && + (value as Evidence).operation === "inventory_artifact", + "Expected the sealed inventory_artifact Evidence", + ), + }), + ) + .min(1), +}); + const parseInventoryEvidence = (input: unknown): Evidence => { - const evidence = parseEvidence(input); + // Authenticate once and seal, borrowing an already sealed snapshot. + const evidence = immutableEvidence(input); if (evidence.operation !== "inventory_artifact") throw new TypeError( "Artifact inspection substep requires inventory_artifact Evidence",