import type { BinaryTarget } from "../../domain/binaryTarget.js"; import { createEvidence, type Evidence } from "../../domain/evidence.js"; import type { EvidenceBundle } from "../../domain/evidenceBundle.js"; import type { EvidenceIntegrityError } from "../../domain/evidenceErrors.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; import type { UnknownRegistryError } from "../../domain/unknownRegistryError.js"; import type { RecordUnknownInput, ResidualUnknown, UnknownStatus, UpdateUnknownInput, } from "../../domain/residualUnknown.js"; import type { Result } from "../../domain/result.js"; import { EvidenceLedger, type EvidenceImportDelta } from "./EvidenceLedger.js"; import { UNKNOWN_REGISTRY_PROVIDER, unknownEvidenceLinks, unknownMutationEvidence, } from "./UnknownEvidence.js"; import type { EvidenceReader, EvidenceWriter, EvidenceUnknownWriter, UnknownRegistryPort, } from "./InvestigationRecordPort.js"; /** One investigation's validated record owner, independent of binary execution. */ export class InvestigationRecords implements EvidenceReader, EvidenceWriter, EvidenceUnknownWriter, UnknownRegistryPort { readonly #evidence = new EvidenceLedger(); /** Record immutable Evidence idempotently; reject conflicting content. */ recordEvidence( evidence: Evidence, ): Result<"added" | "duplicate", EvidenceIntegrityError> { return this.#evidence.record(evidence); } /** Check one semantic Evidence ID without exposing stored values. */ hasEvidence(evidenceId: string): boolean { return this.#evidence.has(evidenceId); } /** Return a detached Evidence value for one semantic ID. */ evidenceById(evidenceId: string): Evidence | undefined { return this.#evidence.get(evidenceId); } /** Export a detached deterministic bundle of Evidence and Unknown revisions. */ exportEvidenceBundle(): EvidenceBundle { return this.#evidence.export(); } /** Atomically merge a bundle and report changes for the owner's post-commit observers. */ mergeEvidenceBundle( bundle: unknown, ): Result { return this.#evidence.import(bundle); } /** Clear records when the owning runtime applies its existing lifecycle policy. */ clear(): void { this.#evidence.clear(); } /** Record an Unknown with an explicitly supplied Evidence subject, if any. */ recordUnknown( input: RecordUnknownInput, target?: BinaryTarget, ): Result { const recorded = this.#evidence.recordUnknown( input, unknownMutationEvidence(target, input), ); return recorded; } /** Commit derived Evidence and its Unknown atomically without an implicit subject. */ recordEvidenceWithUnknown( evidence: Evidence, input: RecordUnknownInput, ): Result { const recorded = this.#evidence.recordWithUnknown( evidence, input, unknownMutationEvidence(undefined, input), ); return recorded; } /** Update an optimistic revision with an explicitly supplied Evidence subject. */ updateUnknown( input: UpdateUnknownInput, target?: BinaryTarget, ): Result { const evidence = createEvidence(target, UNKNOWN_REGISTRY_PROVIDER, { predicateType: "rea.residual-unknown-mutation", operation: "update_unknown", parameters: { unknown_id: input.unknown_id, expected_revision: input.expected_revision, }, result: { action: "update", status: input.status }, confidence: "derived", authority: "analyst-inference", evidenceLinks: unknownEvidenceLinks(input), limitations: [ "Registry mutation evidence records analyst intent, not proof of the answer.", ], }); const updated = this.#evidence.updateUnknown(input, evidence); return updated; } /** Return detached current Unknown revisions matching the selected filters. */ listUnknowns( filters: { readonly status?: UnknownStatus; readonly severity?: ResidualUnknown["severity"]; readonly domain?: string; } = {}, ): ResidualUnknown[] { return this.#evidence.listUnknowns(filters); } /** Verify registry consistency without asserting the answer is true. */ verifyUnknownResolution(unknownId: string): Result< { readonly valid: boolean; readonly truthVerified: boolean; readonly unknown: ResidualUnknown; }, UnknownRegistryError > { return this.#evidence.verifyUnknownResolution(unknownId); } }