From eeca98bb2bb77412eb727b8a40e7224e2c04d43a Mon Sep 17 00:00:00 2001 From: 2SinceJuly <202121119+2SinceJuly@users.noreply.github.com> Date: Wed, 11 Nov 2026 13:51:11 +0800 Subject: [PATCH] fix(providers): attribute staged snapshot and unknown-mutation Evidence Address the open review findings on session provider provenance: - importAnalysisSnapshot merged the re-attributed bundle into the ledger but staged the snapshot's own bundle, so AnalysisSnapshotCache rebuilt replay bindings from records the merge had replaced. Hand the merged bundle back to the stage so replays bind to the attributed evidence. - recordResult recorded an attributed record and then serialized the caller's copy, so successful MCP responses omitted provider_selection while the ledger and exports carried it. Read the recorded Evidence back before delivery, as the direct analysis path already does. - recordUnknown and updateUnknown synthesized unknown-mutation Evidence without the active session's provider selection, so those exported records were unattributed. Thread the binding's selection through to unknownMutationEvidence, which is where the record is produced. --- .../binary/AnalysisSnapshotCache.ts | 19 +++++-- .../binary/BinarySessionRecords.ts | 18 +++++-- .../investigation/InvestigationRecords.ts | 22 +++++--- .../investigation/UnknownEvidence.ts | 51 +++++++++++-------- .../registerApplicationTools/helpers.ts | 5 +- 5 files changed, 79 insertions(+), 36 deletions(-) diff --git a/src/application/binary/AnalysisSnapshotCache.ts b/src/application/binary/AnalysisSnapshotCache.ts index 560f34d1d51..d7edae1b79d 100644 --- a/src/application/binary/AnalysisSnapshotCache.ts +++ b/src/application/binary/AnalysisSnapshotCache.ts @@ -1,7 +1,10 @@ import type { BinaryTarget } from "../../domain/binaryTargetTypes.js"; import type { AnalysisProfileCommitment } from "../../domain/analysisProfile.js"; import type { JsonValue } from "../../domain/jsonValue.js"; -import type { EvidenceBundle } from "../../domain/evidenceBundle.js"; +import { + isImmutableEvidenceBundle, + type EvidenceBundle, +} from "../../domain/evidenceBundle.js"; import { EvidenceIntegrityError, AnalysisSnapshotMismatchError, @@ -434,7 +437,9 @@ export class AnalysisSnapshotCache { readonly profile: AnalysisProfileCommitment; } | undefined, - mergeEvidence: (bundle: EvidenceBundle) => Result, + mergeEvidence: ( + bundle: EvidenceBundle, + ) => Result, ): Result { let validated: AnalysisSnapshot; try { @@ -461,7 +466,15 @@ export class AnalysisSnapshotCache { ); } const importedEvidence = mergeEvidence(validated.evidence_bundle); - return importedEvidence.ok ? ok(this.stage(validated)) : importedEvidence; + if (!importedEvidence.ok) return importedEvidence; + // The stage's replay bindings are rebuilt from the records it is given. + // Staging the snapshot's own bundle would bind replays to the evidence + // identity the merge replaced with an attributed record. + const bundle = importedEvidence.value; + const staged = isImmutableEvidenceBundle(bundle) + ? immutableAnalysisSnapshot({ ...validated, evidence_bundle: bundle }) + : validated; + return ok(this.stage(staged)); } /** Return canonical entries for persistence. */ diff --git a/src/application/binary/BinarySessionRecords.ts b/src/application/binary/BinarySessionRecords.ts index 596ed31d81f..6ff28a78b1e 100644 --- a/src/application/binary/BinarySessionRecords.ts +++ b/src/application/binary/BinarySessionRecords.ts @@ -391,7 +391,7 @@ export abstract class BinarySessionRecords { const imported = this.#records.mergeEvidenceBundle(importable.value); if (!imported.ok) return imported; this.#snapshot.refreshEvidenceMetadata(imported.value.replacements); - return ok(imported.value.recordsAdded); + return ok(importable.value); }, ); if (imported.ok) this.#emitSnapshotChanged(); @@ -554,8 +554,12 @@ export abstract class BinarySessionRecords { ): Result { if (this.#investigationDisposed) return err(new AnalysisCancelledError("record_unknown")); - const target = this.activeAnalysisBinding()?.target; - const recorded = this.#records.recordUnknown(input, target); + const binding = this.activeAnalysisBinding(); + const recorded = this.#records.recordUnknown( + input, + binding?.target, + binding?.providerSelection, + ); if (recorded.ok) this.#emitSnapshotChanged(); return recorded; } @@ -576,8 +580,12 @@ export abstract class BinarySessionRecords { ): Result { if (this.#investigationDisposed) return err(new AnalysisCancelledError("update_unknown")); - const target = this.activeAnalysisBinding()?.target; - const updated = this.#records.updateUnknown(input, target); + const binding = this.activeAnalysisBinding(); + const updated = this.#records.updateUnknown( + input, + binding?.target, + binding?.providerSelection, + ); if (updated.ok) this.#emitSnapshotChanged(); return updated; } diff --git a/src/application/investigation/InvestigationRecords.ts b/src/application/investigation/InvestigationRecords.ts index 9e26b13369f..1a68d34bf53 100644 --- a/src/application/investigation/InvestigationRecords.ts +++ b/src/application/investigation/InvestigationRecords.ts @@ -1,5 +1,8 @@ import type { BinaryTarget } from "../../domain/binaryTargetTypes.js"; -import type { Evidence } from "../../domain/evidence.js"; +import type { + Evidence, + ProviderSelectionProvenance, +} from "../../domain/evidence.js"; import type { EvidenceBundle } from "../../domain/evidenceBundle.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; import type { UnknownRegistryError } from "../../domain/unknownRegistryError.js"; @@ -134,6 +137,7 @@ export class InvestigationRecords recordUnknown( input: RecordUnknownInput, target?: BinaryTarget, + providerSelection?: ProviderSelectionProvenance, ): Result { const admitted = admitInvestigationAuthentication( input, @@ -143,7 +147,11 @@ export class InvestigationRecords if (!admitted.ok) return admitted; return this.#evidence.recordUnknown( input, - unknownMutationEvidence(target, { operation: "record_unknown", input }), + unknownMutationEvidence( + target, + { operation: "record_unknown", input }, + providerSelection, + ), ); } @@ -172,6 +180,7 @@ export class InvestigationRecords updateUnknown( input: UpdateUnknownInput, target?: BinaryTarget, + providerSelection?: ProviderSelectionProvenance, ): Result { const admitted = admitInvestigationAuthentication( input, @@ -179,10 +188,11 @@ export class InvestigationRecords this.retentionPolicy, ); if (!admitted.ok) return admitted; - const evidence = unknownMutationEvidence(target, { - operation: "update_unknown", - input, - }); + const evidence = unknownMutationEvidence( + target, + { operation: "update_unknown", input }, + providerSelection, + ); return this.#evidence.updateUnknown(input, evidence); } diff --git a/src/application/investigation/UnknownEvidence.ts b/src/application/investigation/UnknownEvidence.ts index b1d4955e755..410bb6a2bb3 100644 --- a/src/application/investigation/UnknownEvidence.ts +++ b/src/application/investigation/UnknownEvidence.ts @@ -1,5 +1,10 @@ import type { BinaryTarget } from "../../domain/binaryTargetTypes.js"; -import { createEvidence, type Evidence } from "../../domain/evidence.js"; +import { + createEvidence, + withEvidenceProviderSelection, + type Evidence, + type ProviderSelectionProvenance, +} from "../../domain/evidence.js"; import type { RecordUnknownInput, UpdateUnknownInput, @@ -24,27 +29,31 @@ type UnknownMutation = export const unknownMutationEvidence = ( target: BinaryTarget | undefined, mutation: UnknownMutation, + providerSelection?: ProviderSelectionProvenance, ): Evidence => - createEvidence(target, UNKNOWN_REGISTRY_PROVIDER, { - predicateType: "rea.residual-unknown-mutation", - operation: mutation.operation, - parameters: { ...mutation.input }, - result: - mutation.operation === "record_unknown" - ? { - action: "record", - question: mutation.input.question, - required_authority: mutation.input.required_authority, - required_confidence: mutation.input.required_confidence, - } - : { action: "update", status: mutation.input.status }, - confidence: "derived", - authority: "analyst-inference", - evidenceLinks: unknownEvidenceLinks(mutation.input), - limitations: [ - "Registry mutation evidence records analyst intent, not proof of the answer.", - ], - }); + withEvidenceProviderSelection( + createEvidence(target, UNKNOWN_REGISTRY_PROVIDER, { + predicateType: "rea.residual-unknown-mutation", + operation: mutation.operation, + parameters: { ...mutation.input }, + result: + mutation.operation === "record_unknown" + ? { + action: "record", + question: mutation.input.question, + required_authority: mutation.input.required_authority, + required_confidence: mutation.input.required_confidence, + } + : { action: "update", status: mutation.input.status }, + confidence: "derived", + authority: "analyst-inference", + evidenceLinks: unknownEvidenceLinks(mutation.input), + limitations: [ + "Registry mutation evidence records analyst intent, not proof of the answer.", + ], + }), + providerSelection, + ); /** Deduplicate evidence links carried by an unknown mutation. */ const unknownEvidenceLinks = ( diff --git a/src/server/registerApplicationTools/helpers.ts b/src/server/registerApplicationTools/helpers.ts index 9313a45220d..336d31f449e 100644 --- a/src/server/registerApplicationTools/helpers.ts +++ b/src/server/registerApplicationTools/helpers.ts @@ -25,7 +25,10 @@ export const recordResult = ( unknownKind?: UnknownKind, ) => { const recorded = recordApplicationResult(options, evidence, unknownKind); - return options.delivery.toEvidenceToolResult(evidence, contract, recorded); + // Recording stamps the active session's provider selection, and serializing + // the caller's own copy would omit it while the ledger and exports carry it. + const retained = options.evidenceById?.(evidence.evidence_id) ?? evidence; + return options.delivery.toEvidenceToolResult(retained, contract, recorded); }; /**