mirror of
https://github.com/morluto/rea.git
synced 2026-11-05 14:26:07 +00:00
build_reconstruction_obligation_ledger reported a bundle with duplicate record IDs, or a reviewed obligation citing Evidence absent from the bundle, as unreadable_output and pointed callers at rea doctor. Both are caller input mistakes. Throw dedicated TypeError subclasses at those checks and map them to workflow input errors, following the NonCanonicalEvidenceBundleError approach from #3219 so unrelated TypeErrors stay protocol failures. Fixes #4444
93 lines
3.5 KiB
TypeScript
93 lines
3.5 KiB
TypeScript
import { z } from "zod";
|
|
|
|
import { AnalysisProtocolError } from "../domain/analysisErrorCore.js";
|
|
import type { AnalysisError } from "../domain/analysisErrorBase.js";
|
|
import {
|
|
DuplicateEvidenceRecordError,
|
|
NonCanonicalEvidenceBundleError,
|
|
} from "../domain/evidenceBundle.js";
|
|
import { createEvidence, type Evidence } from "../domain/evidence.js";
|
|
import { jsonObjectSchema, jsonValueSchema } from "../domain/jsonValue.js";
|
|
import {
|
|
reconstructionObligationLedgerInputSchema,
|
|
reconstructionObligationLedgerSchema,
|
|
type ReconstructionObligationLedgerInput,
|
|
type ReconstructionObligationLedger,
|
|
} from "../domain/reconstructionObligationLedgerSchemas.js";
|
|
import { err, ok, type Result } from "../domain/result.js";
|
|
import { requestInputError, workflowInputError } from "./workflowInputError.js";
|
|
import { JAVASCRIPT_APPLICATION_WORKFLOW_PROVIDER } from "./InvestigationProviders.js";
|
|
import {
|
|
deriveReconstructionObligationCandidates,
|
|
MissingReviewEvidenceError,
|
|
} from "./ReconstructionObligationCandidates.js";
|
|
import { evaluateReconstructionObligationLedger } from "./ReconstructionObligationLedgerEvaluation.js";
|
|
|
|
const OPERATION = "build_reconstruction_obligation_ledger" as const;
|
|
|
|
/** Parse one portable ledger request for both CLI and MCP adapters. */
|
|
export const resolveReconstructionObligationLedgerRequest = (
|
|
input: unknown,
|
|
): Result<ReconstructionObligationLedgerInput, AnalysisError> => {
|
|
const parsed = reconstructionObligationLedgerInputSchema.safeParse(input);
|
|
return parsed.success
|
|
? ok(parsed.data)
|
|
: err(requestInputError(OPERATION, parsed.error, input));
|
|
};
|
|
|
|
/** Build the complete deterministic ledger and wrap it in portable Evidence. */
|
|
export const buildReconstructionObligationLedgerEvidenceValidated = (
|
|
input: ReconstructionObligationLedgerInput,
|
|
): Result<Evidence, AnalysisError> => {
|
|
try {
|
|
const generated = deriveReconstructionObligationCandidates(
|
|
input.evidence_bundle,
|
|
input.reviewed_obligations,
|
|
);
|
|
const ledger = evaluateReconstructionObligationLedger({
|
|
candidates: generated.candidates,
|
|
bundle: input.evidence_bundle,
|
|
manifest: input.manifest,
|
|
generationLimitations: generated.limitations,
|
|
});
|
|
return ok(createLedgerEvidence(input, ledger));
|
|
} catch (cause: unknown) {
|
|
if (
|
|
cause instanceof NonCanonicalEvidenceBundleError ||
|
|
cause instanceof DuplicateEvidenceRecordError ||
|
|
cause instanceof MissingReviewEvidenceError
|
|
)
|
|
return err(workflowInputError(OPERATION, cause));
|
|
if (cause instanceof z.ZodError)
|
|
return err(workflowInputError(OPERATION, cause));
|
|
return err(
|
|
new AnalysisProtocolError(
|
|
"Reconstruction obligation ledger generation failed",
|
|
{ cause },
|
|
),
|
|
);
|
|
}
|
|
};
|
|
|
|
const createLedgerEvidence = (
|
|
input: ReconstructionObligationLedgerInput,
|
|
ledger: ReconstructionObligationLedger,
|
|
): Evidence => {
|
|
const completeLedger = reconstructionObligationLedgerSchema.parse(ledger);
|
|
return createEvidence(undefined, JAVASCRIPT_APPLICATION_WORKFLOW_PROVIDER, {
|
|
predicateType: "rea.reconstruction-obligation-ledger",
|
|
operation: OPERATION,
|
|
parameters: jsonObjectSchema.parse({
|
|
ledger_id: completeLedger.ledger_id,
|
|
closure_digest: completeLedger.closure_digest,
|
|
}),
|
|
result: jsonValueSchema.parse(completeLedger),
|
|
rawResult: null,
|
|
confidence: "inferred",
|
|
authority: "analyst-inference",
|
|
environment: null,
|
|
limitations: completeLedger.limitations,
|
|
evidenceLinks: completeLedger.evidence_links,
|
|
});
|
|
};
|