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.
This commit is contained in:
2SinceJuly committed 2026-11-11 13:51:11 +08:00
1 parent a47748ac17
commit eeca98bb2b
5 files changed
+79 -36

No files matched your search

@@ -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<number, AnalysisError>,
mergeEvidence: (
bundle: EvidenceBundle,
) => Result<EvidenceBundle | unknown, AnalysisError>,
): Result<number, AnalysisError> {
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. */
+13 -5
View File
@@ -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<ResidualUnknown, AnalysisError> {
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<ResidualUnknown, AnalysisError> {
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;
}
@@ -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<ResidualUnknown, AnalysisError> {
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<ResidualUnknown, AnalysisError> {
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);
}
@@ -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 = (
@@ -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);
};
/**