/** * Candidate sentences and their verification (.agents/features/day-sentence.md P8). * * An agent may propose a sentence; only a person may say it was checked * (phase0-workspace.md D4). A candidate is a library entry without `publicDomain`, * `verifiedBy` and `verifiedAt`, and with a `check`: where to read it. It is held to * every library rule but verification, so the author is never asked to verify a * line that would fail anyway. * * `verifyCandidates` is what `pnpm library:verify --by ` does. It is the * author's command. **Agents never run it.** */ import { validateEntry, validateLibrary, type Problem } from './validator.js'; /** A file's worth of entries: its name (`biblical.json`) and what it holds. */ export type Files = Readonly>; const isRecord = (x: unknown): x is Record => typeof x === 'object' && x !== null && !Array.isArray(x); const idOf = (x: unknown): string => (isRecord(x) && typeof x['id'] === 'string' ? x['id'] : '(no id)'); /** The library entry a candidate becomes once a person has checked it. */ export function asVerified(candidate: unknown, by: string, day: string): unknown { if (!isRecord(candidate) || !isRecord(candidate['source'])) return candidate; const { check: _check, ...entry } = candidate; return { ...entry, source: { ...candidate['source'], publicDomain: true, verifiedBy: by, verifiedAt: day } }; } /** Every reason a candidate could not become a library entry, even once verified. */ export function validateCandidate(candidate: unknown): Problem[] { const id = idOf(candidate); const problems: Problem[] = []; const check = isRecord(candidate) ? candidate['check'] : undefined; if (!isRecord(check) || typeof check['url'] !== 'string' || !/^https:\/\//.test(check['url']) || typeof check['where'] !== 'string' || typeof check['publicDomain'] !== 'string') { problems.push({ id, rule: 'schema', message: 'a candidate needs check.url (https), check.where and check.publicDomain, so the person verifying knows where to read it' }); } const source = isRecord(candidate) ? candidate['source'] : undefined; for (const field of ['publicDomain', 'verifiedBy', 'verifiedAt']) { if (isRecord(source) && field in source) { problems.push({ id, rule: 'schema', message: `a candidate must not carry source.${field}: only the person who verifies it adds that` }); } } return [...problems, ...validateEntry(asVerified(candidate, 'candidate', '2000-01-01'))]; } /** Every candidate, and ids unique across the candidates and the library together. */ export function validateCandidates(candidates: readonly unknown[], library: readonly unknown[]): Problem[] { const problems = candidates.flatMap(validateCandidate); const inLibrary = new Set(library.map(idOf)); const seen = new Set(); for (const c of candidates) { const id = idOf(c); if (inLibrary.has(id)) problems.push({ id, rule: 'duplicate-id', message: `id '${id}' is already in the library` }); else if (seen.has(id)) problems.push({ id, rule: 'duplicate-id', message: `id '${id}' is proposed more than once` }); seen.add(id); } return problems; } export type VerifyResult = | { readonly ok: true; readonly library: Files; readonly candidates: Files; readonly moved: readonly string[] } | { readonly ok: false; readonly problems: readonly string[] }; /** * Moves the candidates named by `ids` into the library, as checked by `by` on `day`. * Each lands in the library file named like its candidate file. Refuses, and moves * nothing, if an id is unknown, the name is empty, or the library would fail. */ export function verifyCandidates(p: { readonly candidates: Files; readonly library: Files; readonly ids: readonly string[]; readonly by: string; readonly day: string; }): VerifyResult { const by = p.by.trim(); if (by === '' || by.toLowerCase() === 'candidate') return { ok: false, problems: ['--by needs the name of the person who checked the sentences'] }; if (!/^\d{4}-\d{2}-\d{2}$/.test(p.day)) return { ok: false, problems: [`not a day: '${p.day}'`] }; if (p.ids.length === 0) return { ok: false, problems: ['name at least one candidate id'] }; const wanted = new Set(p.ids); const library: Record = Object.fromEntries(Object.entries(p.library).map(([f, e]) => [f, [...e]])); const candidates: Record = {}; const moved: string[] = []; for (const [file, entries] of Object.entries(p.candidates)) { candidates[file] = []; for (const c of entries) { const id = idOf(c); if (wanted.has(id) && !moved.includes(id)) { (library[file] ??= []).push(asVerified(c, by, p.day)); moved.push(id); } else { candidates[file].push(c); } } } const unknown = p.ids.filter((id) => !moved.includes(id)); if (unknown.length > 0) return { ok: false, problems: unknown.map((id) => `no candidate '${id}'`) }; const problems = validateLibrary(Object.values(library).flat()).map((x) => `${x.id} [${x.rule}] ${x.message}`); if (problems.length > 0) return { ok: false, problems }; return { ok: true, library, candidates, moved }; }