feat(evidence): generate verifier completion ledgers
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import canonicalize from "canonicalize";
|
||||
import { z } from "zod";
|
||||
|
||||
const evidenceIdSchema = z.string().regex(/^ev_[a-f0-9]{64}$/u);
|
||||
const claimIdSchema = z.string().regex(/^[A-Za-z][A-Za-z0-9._-]{0,99}$/u);
|
||||
|
||||
/** Terminal outcome recorded for one verifier claim. */
|
||||
export const evidenceCompletionStatusSchema = z.enum([
|
||||
"pass",
|
||||
"fail",
|
||||
"unsupported",
|
||||
"truncated",
|
||||
"unknown",
|
||||
]);
|
||||
|
||||
/** Strict evidence-linked result for one verifier claim. */
|
||||
export const evidenceCompletionRecordSchema = z.strictObject({
|
||||
claim_id: claimIdSchema,
|
||||
status: evidenceCompletionStatusSchema,
|
||||
evidence_ids: z.array(evidenceIdSchema).min(1).max(100),
|
||||
});
|
||||
|
||||
const completionSummarySchema = z.strictObject({
|
||||
total: z.number().int().min(1).max(10_000),
|
||||
pass: z.number().int().nonnegative(),
|
||||
fail: z.number().int().nonnegative(),
|
||||
unsupported: z.number().int().nonnegative(),
|
||||
truncated: z.number().int().nonnegative(),
|
||||
unknown: z.number().int().nonnegative(),
|
||||
complete: z.boolean(),
|
||||
});
|
||||
|
||||
const completionLedgerObjectSchema = z.strictObject({
|
||||
schema_version: z.literal(1),
|
||||
ledger_id: z.string().regex(/^ecl_[a-f0-9]{64}$/u),
|
||||
records: z.array(evidenceCompletionRecordSchema).min(1).max(10_000),
|
||||
summary: completionSummarySchema,
|
||||
});
|
||||
|
||||
export type EvidenceCompletionStatus = z.infer<
|
||||
typeof evidenceCompletionStatusSchema
|
||||
>;
|
||||
/** Readonly verifier result accepted by the canonical ledger builder. */
|
||||
export interface EvidenceCompletionRecordInput {
|
||||
readonly claim_id: string;
|
||||
readonly status: EvidenceCompletionStatus;
|
||||
readonly evidence_ids: readonly string[];
|
||||
}
|
||||
export type EvidenceCompletionRecord = z.infer<
|
||||
typeof evidenceCompletionRecordSchema
|
||||
>;
|
||||
type EvidenceCompletionLedgerValue = z.infer<
|
||||
typeof completionLedgerObjectSchema
|
||||
>;
|
||||
|
||||
const compareIdentifiers = (left: string, right: string): number =>
|
||||
left < right ? -1 : left > right ? 1 : 0;
|
||||
|
||||
const isSortedUnique = (values: readonly string[]): boolean =>
|
||||
values.every(
|
||||
(value, index) =>
|
||||
index === 0 || compareIdentifiers(values[index - 1] ?? "", value) < 0,
|
||||
);
|
||||
|
||||
const summarize = (
|
||||
records: readonly EvidenceCompletionRecord[],
|
||||
): EvidenceCompletionLedger["summary"] => {
|
||||
const counts: Record<EvidenceCompletionStatus, number> = {
|
||||
pass: 0,
|
||||
fail: 0,
|
||||
unsupported: 0,
|
||||
truncated: 0,
|
||||
unknown: 0,
|
||||
};
|
||||
for (const record of records) counts[record.status] += 1;
|
||||
return {
|
||||
total: records.length,
|
||||
...counts,
|
||||
complete: counts.pass === records.length,
|
||||
};
|
||||
};
|
||||
|
||||
const canonicalJson = (value: unknown): string => {
|
||||
const serialized = canonicalize(value);
|
||||
if (serialized === undefined)
|
||||
throw new TypeError(
|
||||
"RFC 8785 canonicalization rejected the completion ledger",
|
||||
);
|
||||
return serialized;
|
||||
};
|
||||
|
||||
const computeLedgerId = (
|
||||
records: readonly EvidenceCompletionRecord[],
|
||||
): string =>
|
||||
`ecl_${createHash("sha256")
|
||||
.update(canonicalJson({ schema_version: 1, records }))
|
||||
.digest("hex")}`;
|
||||
|
||||
const ledgerIssues = (
|
||||
ledger: EvidenceCompletionLedgerValue,
|
||||
context: z.RefinementCtx,
|
||||
): void => {
|
||||
if (!isSortedUnique(ledger.records.map((record) => record.claim_id)))
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["records"],
|
||||
message: "Completion records must be sorted by unique claim ID",
|
||||
});
|
||||
for (const [index, record] of ledger.records.entries())
|
||||
if (!isSortedUnique(record.evidence_ids))
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["records", index, "evidence_ids"],
|
||||
message: "Evidence IDs must be sorted and unique",
|
||||
});
|
||||
if (
|
||||
JSON.stringify(summarize(ledger.records)) !== JSON.stringify(ledger.summary)
|
||||
)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["summary"],
|
||||
message: "Completion summary does not match its records",
|
||||
});
|
||||
if (computeLedgerId(ledger.records) !== ledger.ledger_id)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["ledger_id"],
|
||||
message: "Completion ledger identifier does not match its records",
|
||||
});
|
||||
};
|
||||
|
||||
/** Strict path-free Evidence v2 completion ledger. */
|
||||
export const evidenceCompletionLedgerSchema =
|
||||
completionLedgerObjectSchema.superRefine(ledgerIssues);
|
||||
|
||||
export type EvidenceCompletionLedger = z.infer<
|
||||
typeof evidenceCompletionLedgerSchema
|
||||
>;
|
||||
|
||||
/** Build a canonical path-independent ledger from verifier claim outcomes. */
|
||||
export const createEvidenceCompletionLedger = (
|
||||
input: readonly EvidenceCompletionRecordInput[],
|
||||
): EvidenceCompletionLedger => {
|
||||
const records = input
|
||||
.map((record) => evidenceCompletionRecordSchema.parse(record))
|
||||
.map((record) => ({
|
||||
...record,
|
||||
evidence_ids: [...record.evidence_ids].sort(compareIdentifiers),
|
||||
}))
|
||||
.sort((left, right) => compareIdentifiers(left.claim_id, right.claim_id));
|
||||
if (!isSortedUnique(records.map((record) => record.claim_id)))
|
||||
throw new TypeError("Completion claim IDs must be unique");
|
||||
for (const record of records)
|
||||
if (!isSortedUnique(record.evidence_ids))
|
||||
throw new TypeError("Completion Evidence IDs must be unique per claim");
|
||||
return evidenceCompletionLedgerSchema.parse({
|
||||
schema_version: 1,
|
||||
ledger_id: computeLedgerId(records),
|
||||
records,
|
||||
summary: summarize(records),
|
||||
});
|
||||
};
|
||||
|
||||
/** Parse a completion ledger and reject non-canonical or tampered content. */
|
||||
export const parseEvidenceCompletionLedger = (
|
||||
input: unknown,
|
||||
): EvidenceCompletionLedger => evidenceCompletionLedgerSchema.parse(input);
|
||||
Reference in New Issue
Block a user