feat(reconstruction): add obligation ledgers

This commit is contained in:
morluto
2026-07-24 13:17:56 +00:00
parent 38c883bbc8
commit 9d48ca788c
14 changed files with 2581 additions and 0 deletions
@@ -0,0 +1,53 @@
import type { McpServer } from "@modelcontextprotocol/server";
import {
buildReconstructionObligationLedgerEvidenceValidated,
resolveReconstructionObligationLedgerRequest,
} from "../../application/ReconstructionObligationLedgerService.js";
import { applicationToolContract } from "../../contracts/applicationToolContracts.js";
import { reconstructionObligationLedgerInputSchema } from "../../domain/reconstructionObligationLedgerSchemas.js";
import { logToolExecution } from "../toolLogging.js";
import { safeParseToolInput } from "../toolInputValidation.js";
import { toolRegistrationOptions } from "../toolRegistrationOptions.js";
import { toCallToolResult } from "../toolResult.js";
import { recordResult, recordSources } from "./helpers.js";
import type { ApplicationToolRegistration } from "./types.js";
const contract = applicationToolContract(
"build_reconstruction_obligation_ledger",
);
/** Register conservative reconstruction-obligation generation and closure. */
export const registerReconstructionObligationLedgerTool = (
server: McpServer,
options: ApplicationToolRegistration,
): void => {
server.registerTool(
contract.name,
toolRegistrationOptions(contract),
async (input) => {
const parsed = safeParseToolInput(
reconstructionObligationLedgerInputSchema,
input,
contract.name,
);
if (!parsed.ok) return toCallToolResult(parsed, contract);
const resolved = resolveReconstructionObligationLedgerRequest(
parsed.value,
);
if (!resolved.ok) return toCallToolResult(resolved, contract);
const result = await logToolExecution(options.logger, contract.name, () =>
Promise.resolve(
buildReconstructionObligationLedgerEvidenceValidated(resolved.value),
),
);
if (!result.ok) return toCallToolResult(result, contract);
const recorded = recordSources(
options.recordEvidence,
resolved.value.evidence_bundle.records,
);
if (!recorded.ok) return toCallToolResult(recorded, contract);
return recordResult(options, contract, result.value);
},
);
};
@@ -0,0 +1,69 @@
import {
ResourceNotFoundError,
ResourceTemplate,
type McpServer,
} from "@modelcontextprotocol/server";
import type { BinarySessionPort } from "../application/BinarySessionPort.js";
import { reconstructionObligationLedgerPageSchema } from "../domain/reconstructionObligationLedgerSchemas.js";
/** Expose retained obligation-ledger pages as typed Evidence resources. */
export const registerReconstructionObligationLedgerResource = (
server: McpServer,
session: BinarySessionPort,
): void => {
server.registerResource(
"reconstruction-obligation-ledger-page",
new ResourceTemplate(
"rea://evidence/{evidenceId}/reconstruction-obligation-ledger",
{
list: undefined,
complete: {
evidenceId: (prefix) =>
session
.exportEvidenceBundle()
.records.filter(
({ predicate_type: predicateType }) =>
predicateType === "rea.reconstruction-obligation-ledger/v1",
)
.map(({ evidence_id: evidenceId }) => evidenceId)
.filter((evidenceId) => evidenceId.startsWith(prefix)),
},
},
),
{
title: "Reconstruction obligation ledger page",
description:
"One deterministic Evidence v2-backed obligation page with full-ledger closure digest and summaries.",
mimeType: "application/json",
},
(uri, variables) => {
const evidenceId = variables.evidenceId;
if (typeof evidenceId !== "string")
throw new ResourceNotFoundError(uri.href);
const evidence = session.evidenceById(evidenceId);
if (
evidence === undefined ||
evidence.predicate_type !== "rea.reconstruction-obligation-ledger/v1"
)
throw new ResourceNotFoundError(uri.href);
const page = reconstructionObligationLedgerPageSchema.safeParse(
evidence.normalized_result,
);
if (!page.success) throw new ResourceNotFoundError(uri.href);
return {
contents: [
{
uri: uri.href,
mimeType: "application/json",
text: JSON.stringify(
{ evidence_id: evidence.evidence_id, page: page.data },
null,
2,
),
},
],
};
},
);
};