feat(reconstruction): add obligation ledgers
This commit is contained in:
@@ -0,0 +1,515 @@
|
||||
import type { Evidence } from "../domain/evidence.js";
|
||||
import {
|
||||
parseEvidenceBundle,
|
||||
type EvidenceBundle,
|
||||
} from "../domain/evidenceBundle.js";
|
||||
import {
|
||||
reconstructionObligationLedgerSchema,
|
||||
type ReconstructionObligation,
|
||||
type ReconstructionObligationLedger,
|
||||
type ReconstructionObligationManifest,
|
||||
} from "../domain/reconstructionObligationLedgerSchemas.js";
|
||||
import type { ReconstructionObligationCandidate } from "./ReconstructionObligationCandidates.js";
|
||||
import {
|
||||
evidenceAuthoritySupportsProof,
|
||||
evidenceSupportsOriginalAuthority,
|
||||
proofAuthoritySatisfies,
|
||||
} from "./ReconstructionObligationAuthority.js";
|
||||
import {
|
||||
addObligationDiagnostic as addDiagnostic,
|
||||
contradictionDiagnostics,
|
||||
groupManifestBindings,
|
||||
groupManifestContradictions,
|
||||
hasObligationDiagnostic as hasDiagnostic,
|
||||
uniqueObligationDiagnostics as uniqueDiagnostics,
|
||||
} from "./ReconstructionObligationLedgerDiagnostics.js";
|
||||
import {
|
||||
originalCaseDiagnostics,
|
||||
originalCasesFor,
|
||||
} from "./ReconstructionObligationOriginalCases.js";
|
||||
import {
|
||||
digestObligationLedgerValue,
|
||||
obligationEdgeOrder,
|
||||
obligationLedgerEvidenceLinks,
|
||||
obligationLedgerStatus,
|
||||
obligationReports,
|
||||
summarizeObligations,
|
||||
} from "./ReconstructionObligationLedgerProjection.js";
|
||||
|
||||
type Diagnostic = ReconstructionObligation["diagnostics"][number];
|
||||
type Binding = NonNullable<ReconstructionObligation["binding"]>;
|
||||
|
||||
interface EvaluationContext {
|
||||
readonly candidates: ReadonlyMap<string, ReconstructionObligationCandidate>;
|
||||
readonly evidence: ReadonlyMap<string, Evidence>;
|
||||
readonly bindings: ReadonlyMap<string, readonly Binding[]>;
|
||||
readonly contradictions: ReadonlyMap<string, readonly string[]>;
|
||||
}
|
||||
|
||||
/** Evaluate candidates and manifest bindings into one canonical fail-closed ledger. */
|
||||
export const evaluateReconstructionObligationLedger = ({
|
||||
candidates: candidatesInput,
|
||||
bundle: bundleInput,
|
||||
manifest,
|
||||
maxObligations,
|
||||
generationLimitations,
|
||||
}: {
|
||||
readonly candidates: readonly ReconstructionObligationCandidate[];
|
||||
readonly bundle: EvidenceBundle;
|
||||
readonly manifest: ReconstructionObligationManifest;
|
||||
readonly maxObligations: number;
|
||||
readonly generationLimitations: readonly string[];
|
||||
}): ReconstructionObligationLedger => {
|
||||
const bundle = parseEvidenceBundle(bundleInput);
|
||||
const allCandidates = [...candidatesInput].sort((left, right) =>
|
||||
left.obligation_id.localeCompare(right.obligation_id),
|
||||
);
|
||||
const orphanManifestIds = findOrphanManifestIds(allCandidates, manifest);
|
||||
const candidates = allCandidates.slice(0, maxObligations);
|
||||
const omittedCount = allCandidates.length - candidates.length;
|
||||
const context = createEvaluationContext(candidates, bundle, manifest);
|
||||
const preliminary = candidates.map((candidate) =>
|
||||
evaluateObligation(candidate, context),
|
||||
);
|
||||
const obligations = applyDependencyDiagnostics(preliminary, context);
|
||||
const summary = summarizeObligations(obligations);
|
||||
const reports = obligationReports(obligations);
|
||||
const ownershipGraph = obligations
|
||||
.flatMap((obligation) =>
|
||||
obligation.binding === null
|
||||
? []
|
||||
: [
|
||||
{
|
||||
obligation_id: obligation.obligation_id,
|
||||
module_path: obligation.binding.owner.module_path,
|
||||
symbol: obligation.binding.owner.symbol,
|
||||
},
|
||||
],
|
||||
)
|
||||
.sort(obligationEdgeOrder);
|
||||
const dependencyGraph = obligations
|
||||
.flatMap((obligation) =>
|
||||
obligation.dependency_obligation_ids.map((dependencyId) => ({
|
||||
obligation_id: obligation.obligation_id,
|
||||
depends_on_obligation_id: dependencyId,
|
||||
})),
|
||||
)
|
||||
.sort(obligationEdgeOrder);
|
||||
const evidenceLinks = obligationLedgerEvidenceLinks(obligations, manifest);
|
||||
const limitations = ledgerLimitations(
|
||||
generationLimitations,
|
||||
allCandidates.length,
|
||||
omittedCount,
|
||||
orphanManifestIds,
|
||||
);
|
||||
const coverage = coverageFor(
|
||||
omittedCount,
|
||||
maxObligations,
|
||||
limitations.length > 0,
|
||||
);
|
||||
const semantic = {
|
||||
coverage,
|
||||
summary,
|
||||
reports,
|
||||
ownership_graph: ownershipGraph,
|
||||
dependency_graph: dependencyGraph,
|
||||
obligations,
|
||||
evidence_links: evidenceLinks,
|
||||
limitations,
|
||||
};
|
||||
const evaluatedStatus = obligationLedgerStatus(obligations, omittedCount);
|
||||
const status =
|
||||
evaluatedStatus === "failed"
|
||||
? evaluatedStatus
|
||||
: limitations.length > 0
|
||||
? "unknown"
|
||||
: evaluatedStatus;
|
||||
const closureDigest = digestObligationLedgerValue({
|
||||
schema: "rea.reconstruction-obligation-closure/v1",
|
||||
status,
|
||||
...semantic,
|
||||
});
|
||||
return reconstructionObligationLedgerSchema.parse({
|
||||
schema: "ReconstructionObligationLedger",
|
||||
schema_version: 1,
|
||||
ledger_id: `rol_${digestObligationLedgerValue({
|
||||
schema: "rea.reconstruction-obligation-ledger/v1",
|
||||
evidence_ids: bundle.records.map(({ evidence_id: id }) => id),
|
||||
obligation_ids: obligations.map(({ obligation_id: id }) => id),
|
||||
closure_digest: closureDigest,
|
||||
})}`,
|
||||
closure_digest: closureDigest,
|
||||
status,
|
||||
...semantic,
|
||||
});
|
||||
};
|
||||
|
||||
const createEvaluationContext = (
|
||||
candidates: readonly ReconstructionObligationCandidate[],
|
||||
bundle: EvidenceBundle,
|
||||
manifest: ReconstructionObligationManifest,
|
||||
): EvaluationContext => ({
|
||||
candidates: new Map(
|
||||
candidates.map((candidate) => [candidate.obligation_id, candidate]),
|
||||
),
|
||||
evidence: new Map(
|
||||
bundle.records.map((evidence) => [evidence.evidence_id, evidence]),
|
||||
),
|
||||
bindings: groupManifestBindings(manifest),
|
||||
contradictions: groupManifestContradictions(manifest),
|
||||
});
|
||||
|
||||
const findOrphanManifestIds = (
|
||||
candidates: readonly ReconstructionObligationCandidate[],
|
||||
manifest: ReconstructionObligationManifest,
|
||||
): string[] => {
|
||||
const candidateIds = new Set(
|
||||
candidates.map(({ obligation_id: obligationId }) => obligationId),
|
||||
);
|
||||
return [
|
||||
...new Set(
|
||||
[
|
||||
...manifest.bindings.map(
|
||||
({ obligation_id: obligationId }) => obligationId,
|
||||
),
|
||||
...manifest.contradictions.map(
|
||||
({ obligation_id: obligationId }) => obligationId,
|
||||
),
|
||||
].filter((obligationId) => !candidateIds.has(obligationId)),
|
||||
),
|
||||
].sort();
|
||||
};
|
||||
|
||||
const ledgerLimitations = (
|
||||
generationLimitations: readonly string[],
|
||||
candidateCount: number,
|
||||
omittedCount: number,
|
||||
orphanManifestIds: readonly string[],
|
||||
): string[] =>
|
||||
[
|
||||
...new Set([
|
||||
...generationLimitations,
|
||||
...(candidateCount === 0
|
||||
? [
|
||||
"No reconstruction obligations were derived; closure cannot be claimed.",
|
||||
]
|
||||
: []),
|
||||
...(omittedCount > 0
|
||||
? [
|
||||
`${String(omittedCount)} obligation candidates were omitted by max_obligations; closure is unknown.`,
|
||||
]
|
||||
: []),
|
||||
...(orphanManifestIds.length > 0
|
||||
? [
|
||||
`Manifest entries reference unknown obligations: ${orphanManifestIds.join(", ")}.`,
|
||||
]
|
||||
: []),
|
||||
]),
|
||||
].sort();
|
||||
|
||||
const coverageFor = (
|
||||
omittedCount: number,
|
||||
maxObligations: number,
|
||||
incomplete: boolean,
|
||||
): ReconstructionObligationLedger["coverage"] => ({
|
||||
status: incomplete ? "partial" : "complete",
|
||||
truncated: omittedCount > 0,
|
||||
omitted_count: omittedCount,
|
||||
max_obligations: maxObligations,
|
||||
});
|
||||
|
||||
const evaluateObligation = (
|
||||
candidate: ReconstructionObligationCandidate,
|
||||
context: EvaluationContext,
|
||||
): ReconstructionObligation => {
|
||||
const bindings = context.bindings.get(candidate.obligation_id) ?? [];
|
||||
const binding = bindings.length === 1 ? (bindings[0] ?? null) : null;
|
||||
const diagnostics: Diagnostic[] = [];
|
||||
if (candidate.definitionCount > 1)
|
||||
addDiagnostic(
|
||||
diagnostics,
|
||||
"duplicate-definition",
|
||||
"Multiple candidate definitions share this obligation ID.",
|
||||
);
|
||||
if (bindings.length === 0)
|
||||
addDiagnostic(
|
||||
diagnostics,
|
||||
"missing-owner",
|
||||
"No reconstruction manifest binding owns this obligation.",
|
||||
);
|
||||
if (bindings.length > 1)
|
||||
addDiagnostic(
|
||||
diagnostics,
|
||||
"ambiguous-owner",
|
||||
"Multiple reconstruction manifest bindings claim this obligation.",
|
||||
);
|
||||
if (binding !== null)
|
||||
evaluateBinding(candidate, binding, context, diagnostics);
|
||||
diagnostics.push(
|
||||
...originalCaseDiagnostics(candidate, binding, context.evidence),
|
||||
);
|
||||
evaluateAuthority(candidate, binding, context, diagnostics);
|
||||
evaluateDisposition(candidate, diagnostics);
|
||||
diagnostics.push(
|
||||
...contradictionDiagnostics(
|
||||
candidate.obligation_id,
|
||||
context.contradictions,
|
||||
context.evidence,
|
||||
),
|
||||
);
|
||||
for (const unknownId of candidate.residual_unknown_ids)
|
||||
addDiagnostic(
|
||||
diagnostics,
|
||||
"residual-unknown",
|
||||
`Residual unknown remains visible: ${unknownId}.`,
|
||||
);
|
||||
for (const unavailable of candidate.unavailable_authority)
|
||||
addDiagnostic(diagnostics, "unavailable-authority", unavailable);
|
||||
return {
|
||||
obligation_id: candidate.obligation_id,
|
||||
obligation_version: candidate.obligation_version,
|
||||
title: candidate.title,
|
||||
origin: candidate.origin,
|
||||
application_layer: candidate.application_layer,
|
||||
family: candidate.family,
|
||||
target: candidate.target,
|
||||
authority_references: candidate.authority_references,
|
||||
source_state: candidate.source_state,
|
||||
observed_cases: originalCasesFor(candidate, binding),
|
||||
required: candidate.required,
|
||||
required_case_kinds: candidate.required_case_kinds,
|
||||
required_original_authority: candidate.required_original_authority,
|
||||
required_fixture_authority: candidate.required_fixture_authority,
|
||||
required_verifier_authority: candidate.required_verifier_authority,
|
||||
requires_parser_type: candidate.requires_parser_type,
|
||||
dependency_obligation_ids: candidate.dependency_obligation_ids,
|
||||
residual_unknown_ids: candidate.residual_unknown_ids,
|
||||
unavailable_authority: candidate.unavailable_authority,
|
||||
required_next_evidence: candidate.required_next_evidence,
|
||||
binding,
|
||||
status: obligationStatus(candidate, binding, diagnostics),
|
||||
diagnostics: uniqueDiagnostics(diagnostics),
|
||||
};
|
||||
};
|
||||
|
||||
const evaluateBinding = (
|
||||
candidate: ReconstructionObligationCandidate,
|
||||
binding: Binding,
|
||||
context: EvaluationContext,
|
||||
diagnostics: Diagnostic[],
|
||||
): void => {
|
||||
if (candidate.requires_parser_type && binding.parser_type === null)
|
||||
addDiagnostic(
|
||||
diagnostics,
|
||||
"missing-parser-type",
|
||||
"This boundary requires an explicit parser, schema, and domain type.",
|
||||
);
|
||||
for (const caseKind of candidate.required_case_kinds) {
|
||||
const fixtures = binding.fixtures.filter(
|
||||
({ case_kind: fixtureCase }) => fixtureCase === caseKind,
|
||||
);
|
||||
if (fixtures.length === 0) {
|
||||
addDiagnostic(
|
||||
diagnostics,
|
||||
"missing-case",
|
||||
`Required ${caseKind} fixture is missing.`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
!fixtures.some(
|
||||
(fixture) =>
|
||||
proofAuthoritySatisfies(
|
||||
fixture.authority,
|
||||
candidate.required_fixture_authority,
|
||||
) &&
|
||||
fixture.evidence_ids.every((id) => {
|
||||
const evidence = context.evidence.get(id);
|
||||
return (
|
||||
evidence !== undefined &&
|
||||
evidenceAuthoritySupportsProof(evidence, fixture.authority)
|
||||
);
|
||||
}),
|
||||
)
|
||||
)
|
||||
addDiagnostic(
|
||||
diagnostics,
|
||||
"weak-fixture-authority",
|
||||
`${caseKind} fixture lacks ${candidate.required_fixture_authority} authority or authenticated Evidence.`,
|
||||
);
|
||||
}
|
||||
evaluateVerifier(candidate, binding, context, diagnostics);
|
||||
};
|
||||
|
||||
const evaluateVerifier = (
|
||||
candidate: ReconstructionObligationCandidate,
|
||||
binding: Binding,
|
||||
context: EvaluationContext,
|
||||
diagnostics: Diagnostic[],
|
||||
): void => {
|
||||
const verifier = binding.verifier;
|
||||
if (verifier === null) {
|
||||
addDiagnostic(
|
||||
diagnostics,
|
||||
"missing-verifier",
|
||||
"No verifier claim is bound to this obligation.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (!verifier.enumerated_obligation_ids.includes(candidate.obligation_id))
|
||||
addDiagnostic(
|
||||
diagnostics,
|
||||
"verifier-does-not-enumerate",
|
||||
"The verifier claim does not enumerate this obligation ID.",
|
||||
);
|
||||
const resultEvidence = context.evidence.get(verifier.result_evidence_id);
|
||||
if (resultEvidence === undefined)
|
||||
addDiagnostic(
|
||||
diagnostics,
|
||||
"missing-verifier-result",
|
||||
"The latest verifier result Evidence is absent from the bundle.",
|
||||
);
|
||||
if (
|
||||
!proofAuthoritySatisfies(
|
||||
verifier.authority,
|
||||
candidate.required_verifier_authority,
|
||||
) ||
|
||||
(resultEvidence !== undefined &&
|
||||
!evidenceAuthoritySupportsProof(resultEvidence, verifier.authority))
|
||||
)
|
||||
addDiagnostic(
|
||||
diagnostics,
|
||||
"weak-verifier-authority",
|
||||
`Verifier authority does not satisfy ${candidate.required_verifier_authority}.`,
|
||||
);
|
||||
if (verifier.status !== "pass")
|
||||
addDiagnostic(
|
||||
diagnostics,
|
||||
"verifier-failed",
|
||||
`Latest verifier status is ${verifier.status}.`,
|
||||
);
|
||||
};
|
||||
|
||||
const evaluateAuthority = (
|
||||
candidate: ReconstructionObligationCandidate,
|
||||
binding: Binding | null,
|
||||
context: EvaluationContext,
|
||||
diagnostics: Diagnostic[],
|
||||
): void => {
|
||||
const referenceSatisfied = candidate.authority_references.some(
|
||||
(reference) => {
|
||||
const evidence = context.evidence.get(reference.evidence_id);
|
||||
return (
|
||||
evidence !== undefined &&
|
||||
(candidate.required_original_authority === "static"
|
||||
? ["candidate", "observed"].includes(reference.state)
|
||||
: reference.state === "observed") &&
|
||||
evidenceSupportsOriginalAuthority(
|
||||
evidence,
|
||||
candidate.required_original_authority,
|
||||
)
|
||||
);
|
||||
},
|
||||
);
|
||||
const caseSatisfied = originalCasesFor(candidate, binding).some(
|
||||
({ evidence_id: evidenceId }) => {
|
||||
const evidence = context.evidence.get(evidenceId);
|
||||
return (
|
||||
evidence !== undefined &&
|
||||
evidenceSupportsOriginalAuthority(
|
||||
evidence,
|
||||
candidate.required_original_authority,
|
||||
)
|
||||
);
|
||||
},
|
||||
);
|
||||
const satisfied = referenceSatisfied || caseSatisfied;
|
||||
if (!satisfied)
|
||||
addDiagnostic(
|
||||
diagnostics,
|
||||
"static-only",
|
||||
`No ${candidate.required_original_authority} authority characterizes the original finite behavior.`,
|
||||
);
|
||||
};
|
||||
|
||||
const evaluateDisposition = (
|
||||
candidate: ReconstructionObligationCandidate,
|
||||
diagnostics: Diagnostic[],
|
||||
): void => {
|
||||
if (candidate.disposition === "blocked")
|
||||
addDiagnostic(
|
||||
diagnostics,
|
||||
"blocked",
|
||||
"Reviewed disposition marks this obligation blocked.",
|
||||
);
|
||||
if (candidate.disposition === "out-of-scope")
|
||||
addDiagnostic(
|
||||
diagnostics,
|
||||
"out-of-scope",
|
||||
"Reviewed disposition marks this obligation out of scope; it remains visible.",
|
||||
);
|
||||
};
|
||||
|
||||
const applyDependencyDiagnostics = (
|
||||
obligations: readonly ReconstructionObligation[],
|
||||
context: EvaluationContext,
|
||||
): ReconstructionObligation[] => {
|
||||
const statuses = new Map(
|
||||
obligations.map((obligation) => [
|
||||
obligation.obligation_id,
|
||||
obligation.status,
|
||||
]),
|
||||
);
|
||||
return obligations.map((obligation) => {
|
||||
const diagnostics = [...obligation.diagnostics];
|
||||
for (const dependencyId of obligation.dependency_obligation_ids) {
|
||||
if (!context.candidates.has(dependencyId))
|
||||
addDiagnostic(
|
||||
diagnostics,
|
||||
"dependency-missing",
|
||||
`Dependency obligation is missing: ${dependencyId}.`,
|
||||
);
|
||||
else if (statuses.get(dependencyId) !== "verified")
|
||||
addDiagnostic(
|
||||
diagnostics,
|
||||
"dependency-open",
|
||||
`Dependency obligation is not verified: ${dependencyId}.`,
|
||||
);
|
||||
}
|
||||
const normalized = uniqueDiagnostics(diagnostics);
|
||||
return {
|
||||
...obligation,
|
||||
status: dependencyStatus(obligation.status, normalized),
|
||||
diagnostics: normalized,
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
const obligationStatus = (
|
||||
candidate: ReconstructionObligationCandidate,
|
||||
binding: Binding | null,
|
||||
diagnostics: readonly Diagnostic[],
|
||||
): ReconstructionObligation["status"] => {
|
||||
if (hasDiagnostic(diagnostics, "out-of-scope")) return "out-of-scope";
|
||||
if (hasDiagnostic(diagnostics, "blocked")) return "blocked";
|
||||
if (hasDiagnostic(diagnostics, "contradiction")) return "contradicted";
|
||||
if (
|
||||
hasDiagnostic(diagnostics, "duplicate-definition") ||
|
||||
hasDiagnostic(diagnostics, "residual-unknown") ||
|
||||
hasDiagnostic(diagnostics, "unavailable-authority")
|
||||
)
|
||||
return "unknown";
|
||||
if (binding === null) return "unowned";
|
||||
if (diagnostics.length === 0) return "verified";
|
||||
return "implemented";
|
||||
};
|
||||
|
||||
const dependencyStatus = (
|
||||
status: ReconstructionObligation["status"],
|
||||
diagnostics: readonly Diagnostic[],
|
||||
): ReconstructionObligation["status"] =>
|
||||
(hasDiagnostic(diagnostics, "dependency-missing") ||
|
||||
hasDiagnostic(diagnostics, "dependency-open")) &&
|
||||
!["contradicted", "out-of-scope"].includes(status)
|
||||
? "blocked"
|
||||
: status;
|
||||
Reference in New Issue
Block a user