Files
reverse-engineering-anything/src/application/ReconstructionObligationLedgerRequest.ts
T
d734e6424d fix(cli): advertise parsed JSON documents in --schema
`--schema` for JSON-input CLI commands showed only an opaque
`inputJson` string, so callers could not see the document the command
actually parses. The argument now carries the decoded document as its
contentSchema, built from the same contract each command or its service
parses with (the Evidence input frame, or the ledger and coverage
request schemas). `$schema` is stripped, and a schema JSON Schema cannot
represent is refused rather than advertised as {}.

Fixes #9195
Supersedes #9283

Squashed from #9283 (with morluto's validation test) and rebased onto
main; the coverage test now expects boundary_id's stable-identifier
pattern from #9919. #9214 by 1052326311 took the same approach.

Co-Authored-By: morluto <76467478+morluto@users.noreply.github.com>
Co-Authored-By: 1052326311 <65798732+1052326311@users.noreply.github.com>
Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
2026-11-14 14:16:39 +01:00

87 lines
2.8 KiB
TypeScript

import { parseAnalysisInput } from "./AnalysisInput.js";
import { z } from "zod";
import { AnalysisInputError } from "../domain/analysisErrorCore.js";
import type { AnalysisError } from "../domain/analysisErrorBase.js";
import type { Evidence } from "../domain/evidence.js";
import {
readEvidenceBundle,
type EvidenceBundle,
} from "../domain/evidenceBundle.js";
import {
reconstructionObligationLedgerInputSchema,
type ReviewedReconstructionObligation,
type ReconstructionObligationManifest,
} from "../domain/reconstructionObligationLedgerSchemas.js";
import { err, ok, type Result } from "../domain/result.js";
const OPERATION = "build_reconstruction_obligation_ledger";
/** The ledger parser admits the bundle itself after this envelope. */
export const reconstructionObligationLedgerRequestSchema =
reconstructionObligationLedgerInputSchema.extend({
evidence_bundle: z.unknown(),
});
/** Authenticated bundle and its single request-owned record index. */
export interface ReconstructionObligationSources {
readonly bundle: EvidenceBundle;
readonly records: ReadonlyMap<string, Evidence>;
}
export interface ResolvedReconstructionObligationRequest {
readonly sources: ReconstructionObligationSources;
readonly reviewedObligations: readonly ReviewedReconstructionObligation[];
readonly manifest: ReconstructionObligationManifest;
}
/** Authenticate the source bundle and bind every selected review reference. */
export const resolveReconstructionObligationLedgerRequest = (
input: unknown,
): Result<ResolvedReconstructionObligationRequest, AnalysisError> => {
const frame = parseAnalysisInput(
OPERATION,
input,
reconstructionObligationLedgerRequestSchema,
);
if (!frame.ok) return frame;
const bundle = readEvidenceBundle(frame.value.evidence_bundle, {
operation: OPERATION,
path: ["evidence_bundle"],
});
if (!bundle.ok) return bundle;
const records = new Map(
bundle.value.records.map((record) => [record.evidence_id, record]),
);
for (const [
index,
obligation,
] of frame.value.reviewed_obligations.entries()) {
for (const [
referenceIndex,
id,
] of obligation.review_evidence_ids.entries()) {
if (records.has(id)) continue;
const message = `Reviewed obligation Evidence is missing: ${id}`;
return err(
new AnalysisInputError(OPERATION, { userMessage: message }, [
{
path: [
"reviewed_obligations",
index,
"review_evidence_ids",
referenceIndex,
],
reason: "invalid_value",
message,
},
]),
);
}
}
return ok({
sources: { bundle: bundle.value, records },
reviewedObligations: frame.value.reviewed_obligations,
manifest: frame.value.manifest,
});
};