diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 7f8fad8..6a44c44 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -66,6 +66,21 @@ https://semver.org/spec/v2.0.0.html[Semantic Versioning]. ==== Added +* `scripts/registry/receipt-from-run.mjs` (`just receipt-from-run`, `just + receipt-pin`) moves the receipt of Mathesis to a green Proof Policy Gate run + on `main`. It writes one new current receipt per leg. It supersedes the old one + with a stated reason, in the shape of the earlier superseded receipts: no + digest, manifest or axioms (owner ruling 2026-11-17). Each receipt's fields + come only from the run's job log + (PASS line, file SHA-256, axiom report, checker versions, Guix commit) and + from git (manifests from the pinned `proof-inputs-sha256.sh`). It refuses: + a run that is not a green push on `main`, a log SHA-256 that git does not + hold, an audit that inspected nothing, a report in an earlier gate's form (one + that audits no definition, audit finding 31, or a Rocq report without the + `coqchk -o` kernel check of #107), and a move backwards. Nothing is + written until the canon reader and `check-proof-receipt.sh` accept the + result. The script never writes a grade. Tests: + `tests/registry/receipt-from-run.test.mjs`. * The 27 `hyperpolymath/resource-algebra` claims move their pin to `a17ffb6` (2026-11-17), whose receipt describes `47de4a8`, the merge of hyperpolymath/resource-algebra#36. F-5 now names `maps-disagree-at-positive`, diff --git a/Justfile b/Justfile index b9034c1..aa668dc 100644 --- a/Justfile +++ b/Justfile @@ -780,6 +780,20 @@ registry-check: receipt-check: bun scripts/registry/claims-registry.mjs receipt +# PROOF-POLICY §8.4 rules 2, 4 and 5: a new current receipt per leg, from the +# run's log and git, and the old one superseded because WHY. It writes only what +# the canon reader and the git checker accept, and never a grade. Add --dry-run +# to look first. Needs MATHESIS_STANDARDS (as above) and origin/main with its +# history. Example: just receipt-from-run 44105371764 "whose gate audits definitions" --dry-run +# Move the receipt of Mathesis to green Proof Policy Gate run RUN on main +receipt-from-run run why *flags: + bun scripts/registry/receipt-from-run.mjs receipt --run {{run}} --why {{quote(why)}} {{flags}} + +# COMMIT must hold the new receipt. Then regenerate the views: just registry +# Pin the claims of the receipt of Mathesis to COMMIT and that file's SHA-256 +receipt-pin commit *flags: + bun scripts/registry/receipt-from-run.mjs pin --at {{commit}} {{flags}} + # Append each change of the theory shelf to docs/theory/ledger.jsonl, the only # way the ledger changes (needs MATHESIS_STANDARDS, as above). A note tells the # story: just theory-ledger "THY-2 admitted after its Lean leg landed" diff --git a/docs/status/PROOF-STATUS.adoc b/docs/status/PROOF-STATUS.adoc index 74a64da..0c8d6e3 100644 --- a/docs/status/PROOF-STATUS.adoc +++ b/docs/status/PROOF-STATUS.adoc @@ -285,6 +285,17 @@ guix time-machine -C build/channels.scm -- shell -m build/manifest.scm -- just c # The planted controls: each must fail the gate bash tests/proofs/gate_controls_test.sh agda + +# Move the receipt to a green Proof Policy Gate run on main, then pin the +# claims to the commit that holds it (scripts/registry/receipt-from-run.mjs). +# Look first; the second run writes the receipt, and nothing that the canon +# reader and the git checker refuse. Commit it, then pin the claims to that +# commit and regenerate the registry view. +just receipt-from-run RUN "why the new receipt replaces the old one" --dry-run +just receipt-from-run RUN "why the new receipt replaces the old one" +git commit -S -m "proofs: move the receipt to run RUN" verification/receipts/theory_praxis.deed +just receipt-pin "$(git rev-parse HEAD)" +just registry ---- == Changelog diff --git a/docs/status/ROADMAP.adoc b/docs/status/ROADMAP.adoc index 5253987..2483aa9 100644 --- a/docs/status/ROADMAP.adoc +++ b/docs/status/ROADMAP.adoc @@ -365,3 +365,13 @@ scheduled census, a view of what each repository owes the source of generated Agda; proofs in Agda, Rocq and Lean; three pull requests; the laws of the integer instance proved in the second pull request. +* Receipt moves (2026-11-17), through the selector: +** A superseded receipt keeps the shape of the earlier ones: `:run`, +`:commit`, `:checker`, `:invocation`, `:state` and `:reason`. It keeps no +digest, manifest or axioms. Git keeps them at the receipt's earlier commit. +** The run-to-receipt logic lives here for now +(`scripts/registry/receipt-from-run.mjs`). +** Next action, outside this repository: port its general parts (run check, +log reading, manifest, supersession) to the shared §8.5.1 receipt generator +in `metadatastician/standards`, which the owner ruled on 2026-11-16 should +come first. Mathesis then keeps only its lane names and pins that generator. diff --git a/scripts/registry/receipt-from-run.mjs b/scripts/registry/receipt-from-run.mjs new file mode 100644 index 0000000..0dff25d --- /dev/null +++ b/scripts/registry/receipt-from-run.mjs @@ -0,0 +1,621 @@ +// SPDX-License-Identifier: MPL-2.0 +// SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell +// +// Move the proof receipt of Mathesis to a green run of the Proof Policy Gate. +// +// PROOF-POLICY §8.4 rules 2 and 4 say a receipt moves only after CI has earned +// it, and §8.4 rule 5 keeps the lineage on the receipt. This script does that +// move for verification/receipts/theory_praxis.deed (§8.5.1, standards +// e5a39d7). It does not decide what the run proved; it copies only what the +// run's own record shows and what git holds: +// +// * the run: a completed, successful push run of +// .github/workflows/agda-proof-policy.yml on main, and its job "Agda, Rocq +// and Lean in the pinned Guix" (GitHub's REST API, or --evidence DIR); +// * the job log: for every graded root, the gate's PASS line, the file's +// SHA-256 and the gate's axiom report, plus the checker versions and the +// Guix commit. A graded root whose report is not a non-empty "none" is +// refused: the script never writes an (axiom …) it cannot see; +// * git: each manifest is written by the canon digest script +// (standards scripts/proof-inputs-sha256.sh, pinned by its SHA-256), and +// every file the log names must have the SHA-256 the log printed. +// +// Then it writes one new current receipt per leg, marks the old one +// superseded with the reason you give (in the shape of the earlier superseded +// receipts: no digest, manifest or axioms), and moves :source-commit. Before it +// writes anything, the result must pass the pinned canon reader (which derives +// each grade and refuses a recorded :pwg the receipts do not support) and the +// shared git checker (§8.5.3). The script never writes a grade. +// +// bun scripts/registry/receipt-from-run.mjs receipt --run ID --why TEXT +// [--commit SHA] [--evidence DIR | --save DIR] [--dry-run] +// bun scripts/registry/receipt-from-run.mjs pin --at COMMIT +// +// `receipt` needs MATHESIS_STANDARDS (as claims-registry.mjs) and origin/main +// with its history. `pin` moves each claims.jsonl row that pins this receipt to +// COMMIT, which must hold the new receipt, and to that file's SHA-256. +import {readFile, writeFile, mkdir, mkdtemp, rm} from 'node:fs/promises'; +import {spawnSync} from 'node:child_process'; +import {tmpdir} from 'node:os'; +import {createHash} from 'node:crypto'; +import {fileURLToPath} from 'node:url'; +import path from 'node:path'; +import {loadReader, checkerScript, findTool, CHECKER} from './claims-registry.mjs'; +import {jcs} from './theory-ledger.mjs'; +import {HOME} from './theory.mjs'; + +const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..'); +export const DEED = 'verification/receipts/theory_praxis.deed'; +export const CLAIMS = 'docs/atlas/claims/claims.jsonl'; +export const WORKFLOW = '.github/workflows/agda-proof-policy.yml'; +export const JOB = 'Agda, Rocq and Lean in the pinned Guix'; + +// The receipt's prover names, the gate's names for them, and where their +// proofs live. `config` lists the files of §8.5.1 that a run of that prover +// can read besides the proofs; any that exist at the commit join the manifest. +export const PROVERS = { + agda: {gate: 'agda', dir: 'verification/proofs/agda', label: 'agda', config: n => n.endsWith('.agda-lib') || n === 'libraries'}, + rocq: {gate: 'coq', dir: 'verification/proofs/coq', label: 'coqc', config: n => ['_CoqProject', 'dune', 'dune-project'].includes(n)}, + lean: {gate: 'lean4', dir: 'verification/proofs/lean4', label: 'lean', config: n => ['lean-toolchain', 'lakefile.lean', 'lakefile.toml', 'lake-manifest.json'].includes(n)}, +}; + +// The audit report the gate prints for a module that passed, per gate prover: +// only the forms of the fixed gate (hyperpolymath/mathesis#107), which audits +// definitions as well as theorems, and checks each Rocq library with the kernel +// checker (coqchk -o). The earlier gate's reports ("N theorem(s)" for Rocq, +// "N declared theorem(s)" for Lean) inspected no definition, so a receipt from +// them could again carry vacuous axiom evidence (audit finding 31), and a Rocq +// report without coqchk lacks the kernel check; all are refused. The counts +// must add up to at least one: an audit that inspected nothing is no evidence. +// For Agda the count is the first-party modules of the import cone, and the +// names follow it. +export const NOTES = { + agda: /^postulates: none in the (\d+) first-party module\(s\) of the import cone: (.*)$/, + coq: /^coqchk -o: no axiom, type-in-type, unsafe \(co\)fixpoint or assumed positivity in the library or its import cone; Print Assumptions: closed under the global context for (\d+) declaration\(s\):/, + lean4: /^axioms: none for (\d+) theorem\(s\) and (\d+) definition\(s\) with a source range$/, +}; +const EARLIER = { + coq: /^Print Assumptions: closed under the global context for \d+ (?:theorem|declaration)\(s\)|^no theorem declared$/, + lean4: /^axioms: none for \d+ declared theorem\(s\)$/, +}; + +const HEX40 = /^[0-9a-f]{40}$/; +const sha256 = data => createHash('sha256').update(data).digest('hex'); +const fail = m => { throw new Error(m); }; + +// ── The run ────────────────────────────────────────────────────────────────── + +// Check that `run` (GET /actions/runs/ID) and `jobs` (GET …/jobs) describe a +// green push run of the gate on main, at `commit` when one is given. Returns +// {commit, job}. +export function checkRun(run, jobs, commit = null) { + const no = m => fail(`run ${run?.id}: ${m}`); + if (run?.repository?.full_name && run.repository.full_name !== HOME) no(`is a run of ${run.repository.full_name}, not ${HOME}`); + if (String(run?.path ?? '').split('@')[0] !== WORKFLOW) no(`is a run of ${run?.path}, not ${WORKFLOW}`); + if (run.event !== 'push' || run.head_branch !== 'main') no(`is a ${run.event} run on ${run.head_branch}, not a push run on main`); + if (run.status !== 'completed' || run.conclusion !== 'success') no(`is ${run.status}/${run.conclusion}, not completed/success`); + if (!HEX40.test(run.head_sha ?? '')) no('names no full head commit'); + if (commit !== null && run.head_sha !== commit) no(`ran on ${run.head_sha}, not ${commit}`); + const named = (jobs?.jobs ?? []).filter(j => j.name === JOB); + if (named.length !== 1) no(`has ${named.length} jobs named "${JOB}", not one`); + const [job] = named; + if (job.status !== 'completed' || job.conclusion !== 'success') no(`job ${job.id} is ${job.status}/${job.conclusion}`); + if (job.head_sha !== run.head_sha) no(`job ${job.id} ran on ${job.head_sha}, not ${run.head_sha}`); + if (job.run_attempt !== undefined && run.run_attempt !== undefined && job.run_attempt !== run.run_attempt) { + no(`job ${job.id} is from attempt ${job.run_attempt}, not the run's attempt ${run.run_attempt}`); + } + // The job must be this run's, in this repository: its URL names both, and the + // receipt's :run is that URL. + const url = /^https:\/\/github\.com\/([^/]+\/[^/]+)\/actions\/runs\/(\d+)\/job\/\d+$/.exec(job.html_url ?? '') ?? no(`job ${job.id} has no job URL`); + if (url[1] !== HOME || url[2] !== String(run.id)) no(`job ${job.id}'s URL is not a job of run ${run.id} in ${HOME}: ${job.html_url}`); + if (String(job.run_id) !== String(run.id)) no(`job ${job.id} belongs to run ${job.run_id}, not run ${run.id}`); + return {commit: run.head_sha, job}; +} + +// ── The log ────────────────────────────────────────────────────────────────── + +// Read a job log into {gates, versions, guix}. `gates[gateProver]` maps each +// module "/" to {status, sha256, note}. The gate runs twice (its +// lane, then the golden path); every run must end in RESULT: PASS and all runs +// must agree, or the log is refused. +export function parseGateLog(text) { + const lines = text.split(/\r?\n/).map(l => l + .replace(/^\uFEFF/, '') + .replace(/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(?:\.\d+)?Z ?/, '') + .replace(/\x1b\[[0-9;]*[A-Za-z]/g, '')); + const runs = []; + let cur = null, last = null; + for (const line of lines) { + let m; + if ((m = /^=== (agda|coq|lean4|idris2) proof check ===$/.exec(line))) { + cur = {prover: m[1], modules: new Map(), result: null, coverage: false}; + runs.push(cur); + last = null; + continue; + } + if (!cur) continue; + if (/^=== manifest coverage /.test(line)) { cur.coverage = true; last = null; continue; } + if ((m = /^RESULT: (PASS|FAIL)\b/.exec(line))) { cur.result = m[1]; cur = null; last = null; continue; } + if (cur.coverage) continue; + if ((m = /^ {2}(\S+)\s+(\S+)\s+(PASS|FAIL|PASS -- UNEXPECTED.*|fail \(quarantined, expected\))$/.exec(line))) { + last = {status: m[3], sha256: null, note: null}; + // A gate run lists each module once; a second entry would overwrite the + // first and could hide a stale SHA-256, so the log is refused. + if (cur.modules.has(`${m[1]}/${m[2]}`)) fail(`the ${cur.prover} gate run in the log lists ${m[1]}/${m[2]} twice`); + cur.modules.set(`${m[1]}/${m[2]}`, last); + continue; + } + if (last && (m = /^ {8}sha256 ([0-9a-f]{64})$/.exec(line))) { + // One SHA-256 per module: a second would overwrite the first. + if (last.sha256 !== null) fail(`the ${cur.prover} gate run in the log prints two SHA-256s for one module`); + last.sha256 = m[1]; + continue; + } + if (last && last.sha256 && last.note === null && /^ {8}\S/.test(line)) { last.note = line.trim(); continue; } + } + const gates = {}; + for (const r of runs) { + if (r.result !== 'PASS') fail(`the ${r.prover} gate run in the log ends in ${r.result ?? 'no RESULT line'}, not RESULT: PASS`); + const seen = gates[r.prover]; + if (!seen) { gates[r.prover] = r.modules; continue; } + const key = mods => JSON.stringify([...mods].sort(([a], [b]) => (a < b ? -1 : a > b))); + if (key(seen) !== key(r.modules)) fail(`the ${r.prover} gate ran more than once in the log, and the runs disagree`); + } + const one = (re, what) => { + const found = new Set(lines.map(l => re.exec(l)?.[1]).filter(Boolean)); + if (found.size !== 1) fail(`the log shows ${found.size === 0 ? 'no' : `${found.size} different`} ${what}`); + return [...found][0]; + }; + const versions = { + agda: one(/^Agda version (\S+)$/, 'Agda version'), + rocq: one(/^The (?:Coq Proof Assistant|Rocq Prover), version (\S+)/, 'Rocq version'), + lean: one(/^Lean \(version ([^,\s]+),/, 'Lean version'), + }; + const guix = one(/^\s+commit: ([0-9a-f]{40})$/, 'Guix channel commit'); + return {gates, versions, guix}; +} + +// ── The deed, as spans ─────────────────────────────────────────────────────── + +// Read the deed's lists and atoms with their byte offsets, so that an edit +// touches only the bytes it means to and every comment and layout survives. +// This locates spans only; the canon reader decides whether the result is valid. +export function parseSpans(text) { + const top = [], stack = []; + const push = node => (stack.length ? stack.at(-1).items : top).push(node); + let i = 0; + while (i < text.length) { + const c = text[i]; + if (c === ';') { while (i < text.length && text[i] !== '\n') i++; continue; } + if (c === '(') { const n = {type: 'list', start: i, end: -1, items: []}; push(n); stack.push(n); i++; continue; } + if (c === ')') { const n = stack.pop() ?? fail(`unbalanced ) at offset ${i}`); n.end = i + 1; i++; continue; } + if (/\s/.test(c)) { i++; continue; } + const s = i; + const q = /^#?[A-Za-z0-9]*"/.exec(text.slice(i, i + 8)); + if (q && (c === '"' || c === '#')) { + i += q[0].length; + while (i < text.length && text[i] !== '"') i += text[i] === '\\' ? 2 : 1; + if (i >= text.length) fail(`unterminated string at offset ${s}`); + i++; + const raw = text.slice(s + q[0].length, i - 1); + push({type: 'atom', start: s, end: i, text: text.slice(s, i), value: raw.replace(/\\(.)/g, '$1')}); + continue; + } + while (i < text.length && !/[\s();"]/.test(text[i])) i++; + push({type: 'atom', start: s, end: i, text: text.slice(s, i), value: text.slice(s, i)}); + } + if (stack.length) fail('unbalanced ( in the deed'); + return top; +} +const headOf = n => (n.type === 'list' && n.items[0]?.type === 'atom' ? n.items[0].value : null); +const children = (n, head) => n.items.filter(x => headOf(x) === head); +const keyAtom = (n, key) => n.items.find(x => x.type === 'atom' && x.value === `:${key}`); +const valueAtom = (n, key) => { const i = n.items.indexOf(keyAtom(n, key)); return i < 0 ? null : n.items[i + 1]; }; +const value = (n, key) => valueAtom(n, key)?.value ?? null; +const column = (text, offset) => offset - (text.lastIndexOf('\n', offset - 1) + 1); +const quote = s => { + if (/["\\\n\r\t]/.test(s) || s.trim() === '') fail(`refusing a deed string with a quote, backslash, control character or nothing in it: ${JSON.stringify(s)}`); + return `"${s}"`; +}; + +// The legs of the deed with a current receipt: [{claim, prover, leg, receipt}]. +export function currentReceipts(text) { + const deed = parseSpans(text).find(n => headOf(n) === 'praxis-deed') ?? fail('no (praxis-deed …) in the receipt'); + if (children(deed, 'echo').length) fail('the receipt has (echo …) records; moving them is not this script\'s job'); + const out = []; + for (const claim of children(deed, 'claim')) { + for (const leg of children(claim, 'leg')) { + const cur = children(leg, 'receipt').filter(r => value(r, 'state') === 'current'); + if (cur.length > 1) fail(`claim ${value(claim, 'id')}: the ${value(leg, 'prover')} leg has ${cur.length} current receipts`); + if (cur.length) out.push({claim: value(claim, 'id'), prover: value(leg, 'prover'), leg, receipt: cur[0]}); + } + } + return {deed, legs: out}; +} + +// The text of a new current receipt, laid out as the existing ones are. +export function receiptText({run, commit, checker, invocation, digest, manifest}, fieldIndent) { + const f = ' '.repeat(fieldIndent); + const files = manifest.map(m => `${f} (file :path ${quote(m.path)} :sha256 ${quote(m.sha256)})`).join('\n'); + return [ + `(receipt :run ${quote(run)}`, + `${f}:commit ${quote(commit)}`, + `${f}:checker ${quote(checker)}`, + `${f}:invocation ${quote(invocation)}`, + `${f}:inputs-sha256 ${quote(digest)}`, + `${f}:state current`, + `${f}(axioms)`, + `${f}(inputs-manifest`, + `${files}))`, + ].join('\n'); +} + +// Whether text[start, end) holds a comment (a ";" outside a string). +const hasComment = (text, start, end) => { + for (let i = start, inString = false; i < end; i++) { + if (inString) { if (text[i] === '\\') i++; else if (text[i] === '"') inString = false; } else if (text[i] === '"') inString = true; else if (text[i] === ';') return true; + } + return false; +}; + +// The old current receipt as a superseded one, in the shape of the receipts +// superseded before it (owner ruling 2026-11-17): :run, :commit, :checker and +// :invocation as they were written, then :state superseded and :reason. Its +// :inputs-sha256, (axioms …) and (inputs-manifest …) are dropped; §8.5.1 lets a +// receipt that is not current omit them, and git keeps them at the receipt's +// earlier commit. A comment inside it would be lost, so it is refused. +export function supersededText(text, receipt, fieldIndent, reason) { + if (hasComment(text, receipt.start, receipt.end)) fail(`the receipt at offset ${receipt.start} holds a comment, which superseding it would drop`); + const f = ' '.repeat(fieldIndent); + const field = k => valueAtom(receipt, k)?.text ?? fail(`the receipt at offset ${receipt.start} has no :${k}`); + return [ + `(receipt :run ${field('run')}`, + `${f}:commit ${field('commit')}`, + `${f}:checker ${field('checker')}`, + `${f}:invocation ${field('invocation')}`, + `${f}:state superseded`, + `${f}:reason ${quote(reason)})`, + ].join('\n'); +} + +// Apply the move to the deed text: a new current receipt before each old one, +// the old one superseded with `reason` (supersededText), :source-commit moved, +// and the old run, commit and Guix named in the header comment replaced by the +// new ones. `receipts` maps "claim prover" to the fields of receiptText. +// Returns {text, header}, where `header` lists the header replacements made. +export function rewriteDeed(text, {commit, runId, receipts, reason, guix}) { + const {deed, legs} = currentReceipts(text); + const old = value(deed, 'source-commit'); + if (old === commit) fail(`the receipt already describes ${commit}`); + const edits = []; + const runs = new Set(), guixes = new Set(); + for (const {claim, prover, receipt} of legs) { + const fields = receipts.get(`${claim} ${prover}`) ?? fail(`no new receipt for claim ${claim}, ${prover} leg`); + if (value(receipt, 'commit') !== old) fail(`claim ${claim}, ${prover} leg: the current receipt is at ${value(receipt, 'commit')}, not the :source-commit ${old}`); + runs.add(/\/actions\/runs\/(\d+)/.exec(value(receipt, 'run'))?.[1] ?? fail(`claim ${claim}, ${prover} leg: the current :run is not a CI run`)); + const g = /\(Guix ([0-9a-f]{7,40})\)/.exec(value(receipt, 'checker'))?.[1]; + if (g) guixes.add(g); + const indent = column(text, receipt.start); + const fieldIndent = column(text, keyAtom(receipt, 'commit').start); + edits.push({at: receipt.start, del: receipt.end - receipt.start, + ins: receiptText(fields, fieldIndent) + '\n' + ' '.repeat(indent) + supersededText(text, receipt, fieldIndent, reason)}); + } + if (legs.length === 0) fail('the receipt has no current receipt to move'); + if (receipts.size !== legs.length) fail(`${receipts.size} new receipts for ${legs.length} legs`); + const src = valueAtom(deed, 'source-commit'); + edits.push({at: src.start, del: src.end - src.start, ins: `"${commit}"`}); + // The header comment names the current run, commit and Guix: move those words. + const header = []; + const swaps = [...runs].map(r => [r, String(runId)]); + swaps.push([old, commit], [old.slice(0, 7), commit.slice(0, 7)]); + for (const g of guixes) if (g !== guix.slice(0, g.length)) swaps.push([`Guix ${g}`, `Guix ${guix.slice(0, g.length)}`]); + let head = text.slice(0, deed.start); + for (const [from, to] of swaps) { + const re = new RegExp(`(? ${to} (${n}x)`); } + } + let out = text; + for (const e of edits.sort((a, b) => b.at - a.at)) out = out.slice(0, e.at) + e.ins + out.slice(e.at + e.del); + return {text: head + out.slice(deed.start), header}; +} + +// ── Inputs: what each prover's run read at the commit (§8.5.1) ────────────── + +// The input paths of each receipt prover, from the log and the tree at the +// commit. Agda: the first-party files of the import cones of the graded +// roots, as the gate printed them. Rocq and Lean: every module the gate +// compiled. Each: the config files of PROVERS that exist. `files` is the set of +// paths at the commit; `roots` maps each receipt prover to its graded roots +// (paths). Throws if a graded root did not pass with a non-empty no-axiom +// report, or if a cone's file count differs from the gate's. +export function inputPaths(gates, files, roots) { + const out = {}, notes = {}; + for (const [prover, spec] of Object.entries(PROVERS)) { + if (!roots[prover]?.length) continue; + const mods = gates[spec.gate] ?? fail(`the log has no ${spec.gate} gate run`); + const set = new Set(); + for (const r of roots[prover]) { + const m = mods.get(r) ?? fail(`the ${spec.gate} gate in the log did not check ${r}`); + if (m.status !== 'PASS') fail(`the ${spec.gate} gate reports ${r} as ${m.status}`); + if (!m.sha256) fail(`the ${spec.gate} gate printed no sha256 for ${r}`); + const n = NOTES[spec.gate].exec(m.note ?? ''); + if (!n && EARLIER[spec.gate]?.test(m.note ?? '')) { + fail(`the ${spec.gate} gate's report for ${r} is in the form of an earlier gate, which #107 replaced (it audits no definition, or runs no kernel check): ${JSON.stringify(m.note)}`); + } + if (!n) fail(`the ${spec.gate} gate's report for ${r} is not a no-axiom report: ${JSON.stringify(m.note)}`); + if (Number(n[1]) + Number(spec.gate === 'lean4' ? n[2] : 0) < 1) fail(`the ${spec.gate} gate's audit of ${r} inspected nothing: ${JSON.stringify(m.note)}`); + notes[r] = m.note; + if (prover === 'agda') { + const cone = n[2].trim().split(/\s+/).map(name => `${spec.dir}/${name.replaceAll('.', '/')}.agda`).filter(f => files.has(f)); + if (cone.length !== Number(n[1])) fail(`the gate counts ${n[1]} first-party modules in the cone of ${r}, but ${cone.length} of its names are files at the commit`); + cone.forEach(f => set.add(f)); + } + } + // Rocq and Lean: every module the gate compiled. A quarantined module that + // failed as expected compiled nothing, so it is not an input. + if (prover !== 'agda') for (const [k, m] of mods) if (m.status === 'PASS') set.add(k); + // Agda looks for its library files in the directory it runs in and in + // every parent; the other provers read their config beside the proofs. + const dirs = new Set([spec.dir]); + if (prover === 'agda') for (let d = spec.dir; d !== '.'; d = path.posix.dirname(d)) dirs.add(path.posix.dirname(d)); + for (const f of files) if (dirs.has(path.posix.dirname(f)) && spec.config(path.posix.basename(f))) set.add(f); + out[prover] = [...set]; + } + return {paths: out, notes}; +} + +// ── Git and the pinned canon scripts ───────────────────────────────────────── + +const run = (cmd, args, opts = {}) => spawnSync(findTool(cmd), args, {encoding: 'utf8', maxBuffer: 1 << 26, ...opts}); +const git = (repo, ...args) => { + const r = run('git', ['-C', repo, ...args]); + if (r.status !== 0) fail(`git ${args.join(' ')}: ${(r.stderr || r.error?.message || '').trim()}`); + return r.stdout; +}; +const isAncestor = (repo, a, b) => run('git', ['-C', repo, 'merge-base', '--is-ancestor', a, b]).status === 0; + +// The manifest of PATHS at COMMIT, from the pinned canon digest script. +export async function canonManifest(repo, commit, paths, standardsDir = process.env.MATHESIS_STANDARDS) { + await checkerScript(standardsDir); // checks the reader, the checker and the digest script against their pins + const script = path.resolve(standardsDir, CHECKER.digest); + const r = run('bash', [script, '-C', repo, '--manifest', commit, ...paths]); + if (r.status !== 0) fail(`proof-inputs-sha256.sh at ${commit}: ${(r.stdout + r.stderr).trim()}`); + const manifest = r.stdout.trimEnd().split('\n').map(l => { + const m = /^([0-9a-f]{64}) {2}(.+)$/.exec(l) ?? fail(`unexpected manifest line: ${l}`); + return {sha256: m[1], path: m[2]}; + }); + return {manifest, digest: sha256(r.stdout)}; +} + +// ── Evidence: the run, its jobs and the log ────────────────────────────────── + +// A GitHub id goes into an API path only as a positive integer, as one encoded +// segment, so no input can steer the request to another path. +export function idSegment(id, what) { + const text = String(id); + const n = /^[1-9]\d*$/.test(text) ? Number.parseInt(text, 10) : Number.NaN; + if (!Number.isSafeInteger(n)) fail(`${what} is not a positive integer id: ${JSON.stringify(id)}`); + return n.toString(10); +} + +// The only three requests the script makes, each checked against an allowlist +// of origin and path before it is sent. +const API = 'https://api.github.com'; +const API_PATHS = { + run: id => `/repos/${HOME}/actions/runs/${id}`, + jobs: id => `/repos/${HOME}/actions/runs/${id}/jobs`, + logs: id => `/repos/${HOME}/actions/jobs/${id}/logs`, +}; +const API_PATH = new RegExp(`^/repos/${HOME}/actions/(?:runs/[1-9][0-9]*(?:/jobs)?|jobs/[1-9][0-9]*/logs)$`); +export function apiUrl(kind, id) { + const build = API_PATHS[kind] ?? fail(`no API request of kind ${kind}`); + const url = new URL(build(idSegment(id, `the ${kind} id`)), API); + if (url.origin !== API || !API_PATH.test(url.pathname)) fail(`refusing to request ${url.href}`); + if (kind === 'jobs') { url.searchParams.set('filter', 'latest'); url.searchParams.set('per_page', '100'); } + return url; +} + +async function fetchEvidence(runId) { + const plain = {Accept: 'application/vnd.github+json', 'User-Agent': 'mathesis-receipt-from-run'}; + const auth = process.env.GITHUB_TOKEN ? {...plain, Authorization: `Bearer ${process.env.GITHUB_TOKEN}`} : plain; + // GitHub answers a log request with a redirect to a signed storage URL. It is + // followed here by hand, over HTTPS and without the token, so the token is + // only ever sent to api.github.com. + const get = async url => { + let res = await fetch(url, {headers: auth, redirect: 'manual'}); + if (res.status >= 300 && res.status < 400) { + const to = new URL(res.headers.get('location') ?? fail(`GET ${url}: HTTP ${res.status} with no Location`), url); + if (to.protocol !== 'https:') fail(`GET ${url}: redirected to a URL that is not HTTPS`); + res = await fetch(to, {headers: plain, redirect: 'error'}); + } + if (!res.ok) fail(`GET ${url}: HTTP ${res.status}`); + return res; + }; + const runJson = await (await get(apiUrl('run', runId))).json(); + const jobsJson = await (await get(apiUrl('jobs', runId))).json(); + const job = (jobsJson.jobs ?? []).find(j => j.name === JOB); + const log = job ? await (await get(apiUrl('logs', job.id))).text() : ''; + return {run: runJson, jobs: jobsJson, log}; +} + +async function readEvidence(dir) { + return { + run: JSON.parse(await readFile(path.join(dir, 'run.json'), 'utf8')), + jobs: JSON.parse(await readFile(path.join(dir, 'jobs.json'), 'utf8')), + log: await readFile(path.join(dir, 'job.log'), 'utf8'), + }; +} + +async function saveEvidence(dir, ev) { + await mkdir(dir, {recursive: true}); + await writeFile(path.join(dir, 'run.json'), JSON.stringify(ev.run, null, 2) + '\n'); + await writeFile(path.join(dir, 'jobs.json'), JSON.stringify(ev.jobs, null, 2) + '\n'); + await writeFile(path.join(dir, 'job.log'), ev.log); +} + +// ── The move ───────────────────────────────────────────────────────────────── + +// Plan and check the move. Returns {text, report}; writes nothing. `repo` is +// a clone of Mathesis whose `branch` holds the run's commit. +export async function planMove({repo = root, deedPath = DEED, evidence, why, commit = null, branch = 'origin/main', standardsDir = process.env.MATHESIS_STANDARDS}) { + if (!why || !why.trim()) fail('--why is required: say why the new receipt replaces the old one'); + const {commit: at, job} = checkRun(evidence.run, evidence.jobs, commit); + const log = parseGateLog(evidence.log); + const text = await readFile(path.join(repo, deedPath), 'utf8'); + const {legs, deed} = currentReceipts(text); + const old = value(deed, 'source-commit'); + if (old === at) fail(`the receipt already describes ${at}`); + if (!isAncestor(repo, old, at)) fail(`${at} does not descend from the receipt's :source-commit ${old}; a receipt moves forward only`); + if (!isAncestor(repo, at, branch)) fail(`${at} is not on ${branch}`); + const channels = git(repo, 'show', `${at}:build/channels.scm`); + if (!channels.includes(`(commit "${log.guix}")`)) fail(`the log's Guix commit ${log.guix} is not the one build/channels.scm pins at ${at}`); + + // The graded roots of each prover, from the invocations of the current receipts. + const roots = {}; + for (const l of legs) { + const spec = PROVERS[l.prover] ?? fail(`claim ${l.claim}: no gate lane for prover ${l.prover}`); + const file = value(l.receipt, 'invocation').trim().split(/\s+/).at(-1); + (roots[l.prover] ??= new Set()).add(`${spec.dir}/${file}`); + } + for (const p of Object.keys(roots)) roots[p] = [...roots[p]]; + const files = new Set(git(repo, 'ls-tree', '-r', '--name-only', '--full-tree', at).split('\n').filter(Boolean)); + const {paths, notes} = inputPaths(log.gates, files, roots); + + // Every SHA-256 the log printed, for every gated module of every prover and + // not only those in a manifest, must be the file's at the commit: the log + // must be a log of this commit. + const logged = new Map(); + for (const mods of Object.values(log.gates)) { + for (const [p, m] of mods) { + if (!m.sha256) continue; + if (logged.has(p) && logged.get(p) !== m.sha256) fail(`${p}: the log prints two SHA-256s for it`); + logged.set(p, m.sha256); + } + } + if (logged.size === 0) fail('the log printed no SHA-256 for any module'); + for (const f of (await canonManifest(repo, at, [...logged.keys()], standardsDir)).manifest) { + if (logged.get(f.path) !== f.sha256) fail(`${f.path}: the log printed sha256 ${logged.get(f.path)}, git has ${f.sha256} at ${at}`); + } + // One manifest per prover (§8.5.1: one digest for one commit and one prover). + const inputs = {}; + for (const [prover, list] of Object.entries(paths)) inputs[prover] = await canonManifest(repo, at, list, standardsDir); + const checker = p => `${PROVERS[p].label} ${log.versions[p]} (Guix ${log.guix.slice(0, 7)})`; + const receipts = new Map(legs.map(l => [`${l.claim} ${l.prover}`, { + run: job.html_url, commit: at, checker: checker(l.prover), invocation: value(l.receipt, 'invocation'), + digest: inputs[l.prover].digest, manifest: inputs[l.prover].manifest, + }])); + const reason = `replaced by the CI receipt of run ${evidence.run.id} on ${at.slice(0, 7)}, ${why.trim()}`; + const moved = rewriteDeed(text, {commit: at, runId: evidence.run.id, receipts, reason, guix: log.guix}); + const out = moved.text; + + // The canon reader must accept the result, and derives each grade. + const readProofReceipt = await loadReader(standardsDir); + const record = readProofReceipt(out, path.basename(deedPath)); + // The shared git checker (§8.5.3) must pass on the result. + const work = await mkdtemp(path.join(tmpdir(), 'mathesis-receipt-')); + try { + const file = path.join(work, path.basename(deedPath)); + await writeFile(file, out); + const r = run('bash', [await checkerScript(standardsDir), '--branch', branch, file, repo]); + if (r.status !== 0) fail(`check-proof-receipt.sh refuses the new receipt (exit ${r.status}):\n${(r.stdout + r.stderr).split('\n').filter(l => !l.startsWith('ok ')).join('\n')}`); + } finally { + await rm(work, {recursive: true, force: true}); + } + + const oldPaths = p => [...new Set(legs.filter(l => l.prover === p).flatMap(l => + children(children(l.receipt, 'inputs-manifest')[0] ?? {items: []}, 'file').map(f => value(f, 'path'))))]; + const report = [ + `run ${job.html_url}`, + `commit ${at} (was ${old})`, + `toolchain Guix ${log.guix}; ${Object.keys(PROVERS).map(checker).join('; ')}`, + ...Object.entries(inputs).map(([p, m]) => { + const before = new Set(oldPaths(p)), now = new Set(m.manifest.map(f => f.path)); + const delta = [...now].filter(x => !before.has(x)).map(x => `+${x}`).concat([...before].filter(x => !now.has(x)).map(x => `-${x}`)); + return `inputs ${p}: ${m.digest} (${m.manifest.length} files${delta.length ? `; ${delta.join(' ')}` : '; same paths as before'})`; + }), + ...Object.entries(notes).map(([r, n]) => `audit ${r}: ${n}`), + `header ${moved.header.join('; ') || 'no change'}`, + `reason ${reason}`, + `reader accepts; derived: ${record.claims.map(c => `${c.id} ${c.pwg}`).join(', ')}`, + `checker check-proof-receipt.sh --branch ${branch}: every check holds`, + ]; + return {text: out, report, record}; +} + +// ── The claim pins ─────────────────────────────────────────────────────────── + +// Move every claims.jsonl row that pins the receipt at `deedPath` to `at`, with +// the SHA-256 of that file at `at`. Each claim pinned must be in that receipt, +// and the canon reader must accept it. Returns {text, moved, notOnMain}. +export async function planPin({repo = root, claimsPath = CLAIMS, deedPath = DEED, at, branch = 'origin/main', standardsDir = process.env.MATHESIS_STANDARDS}) { + const commit = git(repo, 'rev-parse', '--verify', `${at}^{commit}`).trim(); + const receipt = git(repo, 'show', `${commit}:${deedPath}`); + const readProofReceipt = await loadReader(standardsDir); + const ids = new Set(readProofReceipt(receipt, path.basename(deedPath)).claims.map(c => c.id)); + const digest = sha256(receipt); + const text = await readFile(path.join(repo, claimsPath), 'utf8'); + if (!text.endsWith('\n')) fail(`${claimsPath}: the last line has no LF`); + const moved = []; + const lines = text.slice(0, -1).split('\n').map(line => { + const row = JSON.parse(line); + if (row.repo !== HOME || row.receipt?.path !== deedPath) return line; + if (!ids.has(row.claim)) fail(`${row.id} pins ${deedPath}, but the receipt at ${commit} has no claim ${row.claim}`); + moved.push(`${row.claim}: ${row.source_commit.slice(0, 7)} -> ${commit.slice(0, 7)}`); + return jcs({...row, source_commit: commit, receipt: {...row.receipt, sha256: digest}}); + }); + if (moved.length === 0) fail(`no row of ${claimsPath} pins ${deedPath}`); + return {text: lines.join('\n') + '\n', moved, digest, commit, notOnMain: !isAncestor(repo, commit, branch)}; +} + +// ── Entry point ────────────────────────────────────────────────────────────── + +function flags(args, spec) { + const out = {}; + for (let i = 0; i < args.length; i++) { + const a = args[i]; + if (!a.startsWith('--') || !(a.slice(2) in spec)) fail(`unknown argument ${a}`); + const k = a.slice(2); + if (spec[k] === 'bool') out[k] = true; + else if (i + 1 < args.length) out[k] = args[++i]; + else fail(`${a} needs a value`); + } + return out; +} + +async function main() { + const [cmd, ...args] = process.argv.slice(2); + if (cmd === 'receipt') { + const o = flags(args, {run: 'v', why: 'v', commit: 'v', evidence: 'v', save: 'v', 'dry-run': 'bool'}); + if (!/^\d+$/.test(o.run ?? '')) fail('--run needs the numeric id of a Proof Policy Gate run'); + if (o.commit && !HEX40.test(o.commit)) fail('--commit needs a full 40-character commit'); + const evidence = o.evidence ? await readEvidence(path.resolve(o.evidence)) : await fetchEvidence(o.run); + if (String(evidence.run.id) !== o.run) fail(`the evidence is for run ${evidence.run.id}, not ${o.run}`); + if (o.save) await saveEvidence(path.resolve(o.save), evidence); + const {text, report} = await planMove({evidence, why: o.why, commit: o.commit ?? null}); + console.log(report.join('\n')); + if (o['dry-run']) { console.log(`DRY RUN: ${DEED} not written`); return; } + await writeFile(path.join(root, DEED), text); + console.log(`WROTE ${DEED}. Commit it, then pin the claims to that commit: bun scripts/registry/receipt-from-run.mjs pin --at `); + return; + } + if (cmd === 'pin') { + const o = flags(args, {at: 'v', 'dry-run': 'bool'}); + if (!o.at) fail('--at needs the commit that holds the new receipt'); + const {text, moved, digest, commit, notOnMain} = await planPin({at: o.at}); + console.log(`receipt ${DEED} at ${commit}: sha256 ${digest}`); + console.log(`moved ${moved.join(', ')}`); + if (notOnMain) console.log(`NOTE ${commit} is not on origin/main; the registry fetches it by its id while GitHub keeps it (a PR head ref does)`); + if (o['dry-run']) { console.log(`DRY RUN: ${CLAIMS} not written`); return; } + await writeFile(path.join(root, CLAIMS), text); + console.log(`WROTE ${CLAIMS}. Then regenerate the views: bun scripts/registry/claims-registry.mjs build`); + return; + } + console.error('usage: receipt-from-run.mjs receipt --run ID --why TEXT [--commit SHA] [--evidence DIR | --save DIR] [--dry-run]\n' + + ' receipt-from-run.mjs pin --at COMMIT [--dry-run]'); + process.exit(2); +} + +if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + main().catch(e => { console.error('ERROR: ' + e.message); process.exit(1); }); +} diff --git a/tests/registry/receipt-from-run.test.mjs b/tests/registry/receipt-from-run.test.mjs new file mode 100644 index 0000000..3e8e86b --- /dev/null +++ b/tests/registry/receipt-from-run.test.mjs @@ -0,0 +1,340 @@ +// SPDX-License-Identifier: MPL-2.0 +// SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell +// +// Hermetic tests of scripts/registry/receipt-from-run.mjs. Each end-to-end +// test builds a small git repository with Agda, Rocq and Lean proofs, a +// receipt at its first commit and a gate log for its second, then moves the +// receipt. Like claims-registry.test.mjs, they need MATHESIS_STANDARDS: the +// result must pass the pinned canon reader and the shared git checker. +import {test, expect, beforeAll} from 'bun:test'; +import {mkdtempSync, mkdirSync, writeFileSync, readFileSync} from 'node:fs'; +import {spawnSync} from 'node:child_process'; +import {createHash} from 'node:crypto'; +import {tmpdir} from 'node:os'; +import path from 'node:path'; +import {checkRun, parseGateLog, parseSpans, inputPaths, currentReceipts, canonManifest, planMove, planPin, idSegment, apiUrl, JOB, WORKFLOW, DEED, CLAIMS} + from '../../scripts/registry/receipt-from-run.mjs'; +import {findTool} from '../../scripts/registry/claims-registry.mjs'; +import {jcs} from '../../scripts/registry/theory-ledger.mjs'; + +const sha = s => createHash('sha256').update(s).digest('hex'); +const GUIX = '8edb9da7052607f17f09007933275b43978fcf9d'; +const git = (dir, ...args) => { + const r = spawnSync(findTool('git'), ['-C', dir, '-c', 'user.name=t', '-c', 'user.email=t@t', '-c', 'commit.gpgsign=false', ...args], {encoding: 'utf8'}); + if (r.status !== 0) throw new Error(`git ${args.join(' ')}: ${r.stderr}`); + return r.stdout.trim(); +}; +const put = (dir, file, body) => { mkdirSync(path.dirname(path.join(dir, file)), {recursive: true}); writeFileSync(path.join(dir, file), body); }; + +// ── A gate log in the fixed gate's format ──────────────────────────────────── + +const AGDA_NOTE = (n, names) => `postulates: none in the ${n} first-party module(s) of the import cone: ${names} `; +const COQCHK = 'coqchk -o: no axiom, type-in-type, unsafe (co)fixpoint or assumed positivity in the library or its import cone; '; +const ROCQ_NOTE = `${COQCHK}Print Assumptions: closed under the global context for 3 declaration(s): Not t u `; +const LEAN_NOTE = 'axioms: none for 1 theorem(s) and 4 definition(s) with a source range'; +// mods: [root, rel, file text, note]; a null file text is a quarantined module +// that failed as expected, which the gate prints with no SHA-256. +const gateRun = (prover, root, mods) => [ + `=== ${prover} proof check ===`, + ...mods.flatMap(([r, rel, body, note]) => body === null + ? [` ${r.padEnd(30)} ${rel.padEnd(24)} fail (quarantined, expected)`, ` reason: ${note}`] + : [` ${r.padEnd(30)} ${rel.padEnd(24)} PASS`, ` sha256 ${sha(body)}`, ` ${note}`]), + '', `=== manifest coverage (${root}) ===`, ` all ${mods.length} file(s) accounted for`, '', + 'RESULT: PASS -- gated modules compile; quarantined modules still fail as recorded']; +const logFor = (files, {guix = GUIX, notes = {}, quarantined = false} = {}) => { + const A = 'verification/proofs/agda', C = 'verification/proofs/coq', L = 'verification/proofs/lean4'; + const lines = [ + '##[group]Run guix time-machine -C build/channels.scm -- describe', + ' guix 8edb9da', ' repository URL: https://codeberg.org/guix/guix.git', ' branch: master', ` commit: ${guix}`, + 'Agda version 2.7.0.1', 'The Coq Proof Assistant, version 8.18.0', 'Lean (version 4.29.0, x86_64-unknown-linux-gnu, Release)', + ]; + const once = () => [ + ...gateRun('agda', A, [ + [A, 'B.agda', files[`${A}/B.agda`], notes.B ?? AGDA_NOTE(1, 'Agda.Primitive B')], + [A, 'Top.agda', files[`${A}/Top.agda`], notes.Top ?? AGDA_NOTE(2, 'Agda.Builtin.Nat Agda.Primitive B Top')], + // Gated, but outside every graded cone: its SHA-256 is checked, and it is in no manifest. + [A, 'Unused.agda', files[`${A}/Unused.agda`], AGDA_NOTE(1, 'Agda.Primitive Unused')], + ]), + ...gateRun('coq', C, [[C, 'Top.v', files[`${C}/Top.v`], notes.v ?? ROCQ_NOTE], + ...(quarantined ? [[C, 'Old.v', null, ' an open obligation, kept to show it still fails']] : [])]), + ...gateRun('lean4', L, [[L, 'Top.lean', files[`${L}/Top.lean`], notes.lean ?? LEAN_NOTE]]), + ]; + // The gate runs in its lane and again in the golden path. + return [...lines, ...once(), 'PASS: agda clean control passes', ...once()] + // As GitHub serves it: a timestamp on every line, colour codes on some. + .map((l, i) => `2026-11-17T15:16:${String(i % 60).padStart(2, '0')}.0000000Z ${i % 2 ? '' : '\x1b[36;1m'}${l}${i % 2 ? '' : '\x1b[0m'}`) + .join('\n') + '\n'; +}; + +// ── A repository with a receipt at c1 and a run at c2 ──────────────────────── + +const RUN0 = 'https://github.com/hyperpolymath/mathesis/actions/runs/6000/job/60'; +const deedAt = async (repo, c1, inputs) => { + const rec = (p, inv) => { + const m = inputs[p]; + return ` (receipt :run "${RUN0}"\n :commit "${c1}"\n :checker "${p} (Guix 8edb9da)"\n` + + ` :invocation "${inv}"\n :inputs-sha256 "${m.digest}"\n :state current\n` + + ' (axioms)\n (inputs-manifest\n' + + m.manifest.map(f => ` (file :path "${f.path}" :sha256 "${f.sha256}")`).join('\n') + '))'; + }; + return ';; SPDX-License-Identifier: CC-BY-SA-4.0\n' + + `;; The current receipts come from run\n;; 6000 on main ${c1.slice(0, 7)}; a comment with ( and ) and "quotes".\n` + + '(praxis-deed\n :schema-version "1.0.0"\n :canonical-name "fixture-receipt"\n :profile proof-receipt\n' + + ` :repository "hyperpolymath/mathesis"\n :source-commit "${c1}"\n` + + ' (claim :id "T-1" :pwg PWG-1 :foundation-diverse #f\n' + + ' (leg :prover agda :route source :token proven :state AGDA-VERIFIED\n' + rec('agda', 'agda --safe --without-K --cubical-compatible Top.agda') + ')\n' + + ' (leg :prover rocq :route re-proved :token proven :state RECONCILED ; a comment ) here\n' + rec('rocq', 'coqc Top.v') + ')\n' + + ' (leg :prover lean :route re-proved :token proven :state RECONCILED\n' + rec('lean', 'lean Top.lean') + ')))\n'; +}; + +const PATHS = { + agda: ['verification/proofs/agda/B.agda', 'verification/proofs/agda/Top.agda'], + rocq: ['verification/proofs/coq/Top.v'], + lean: ['verification/proofs/lean4/Top.lean', 'verification/proofs/lean4/lean-toolchain'], +}; + +async function fixtureRepo() { + const repo = mkdtempSync(path.join(tmpdir(), 'receipt-from-run-')); + git(repo, 'init', '-q', '-b', 'main'); + const files = { + 'build/channels.scm': `(list (channel (name 'guix) (commit "${GUIX}")))\n`, + 'verification/proofs/agda/B.agda': 'module B where\n', + 'verification/proofs/agda/Top.agda': 'module Top where\nopen import B\n', + 'verification/proofs/agda/Unused.agda': 'module Unused where\n', + 'verification/proofs/coq/Top.v': 'Theorem t : True. exact I. Qed.\n', + 'verification/proofs/lean4/Top.lean': 'theorem t : True := trivial\n', + 'verification/proofs/lean4/lean-toolchain': 'leanprover/lean4:v4.29.0\n', + }; + for (const [f, b] of Object.entries(files)) put(repo, f, b); + git(repo, 'add', '-A'); git(repo, 'commit', '-q', '-m', 'c1'); + const c1 = git(repo, 'rev-parse', 'HEAD'); + const inputs = {}; + for (const [p, list] of Object.entries(PATHS)) inputs[p] = await canonManifest(repo, c1, list); + put(repo, DEED, await deedAt(repo, c1, inputs)); + files['verification/proofs/agda/Top.agda'] = 'module Top where\nopen import B\n-- a new lemma\n'; + files['verification/proofs/lean4/Top.lean'] = 'theorem t : True := trivial\ntheorem u : True := trivial\n'; + for (const f of ['verification/proofs/agda/Top.agda', 'verification/proofs/lean4/Top.lean']) put(repo, f, files[f]); + git(repo, 'add', '-A'); git(repo, 'commit', '-q', '-m', 'c2'); + const c2 = git(repo, 'rev-parse', 'HEAD'); + return {repo, files, c1, c2, inputs}; +} + +const evidenceFor = (c2, log, {run = {}, job = {}} = {}) => ({ + run: {id: 7000, path: WORKFLOW, event: 'push', head_branch: 'main', status: 'completed', conclusion: 'success', head_sha: c2, run_attempt: 1, ...run}, + jobs: {jobs: [{id: 70, run_id: 7000, name: JOB, status: 'completed', conclusion: 'success', head_sha: c2, run_attempt: 1, + html_url: 'https://github.com/hyperpolymath/mathesis/actions/runs/7000/job/70', ...job}]}, + log, +}); + +let fx; +beforeAll(async () => { fx = await fixtureRepo(); }); + +// ── The run ────────────────────────────────────────────────────────────────── + +test('a green push run of the gate on main is accepted; anything else is refused', () => { + const ev = evidenceFor('c'.repeat(40), ''); + expect(checkRun(ev.run, ev.jobs).commit).toBe('c'.repeat(40)); + const no = (run, job = {}, commit = null) => { const e = evidenceFor('c'.repeat(40), '', {run, job}); return () => checkRun(e.run, e.jobs, commit); }; + expect(no({event: 'pull_request', head_branch: 'feature'})).toThrow(/not a push run on main/); + expect(no({conclusion: 'failure'})).toThrow(/not completed\/success/); + expect(no({path: '.github/workflows/other.yml'})).toThrow(/not .github\/workflows\/agda-proof-policy.yml/); + expect(no({}, {}, 'd'.repeat(40))).toThrow(/ran on c+, not d+/); + expect(no({}, {name: 'another job'})).toThrow(/has 0 jobs named/); + expect(no({}, {conclusion: 'cancelled'})).toThrow(/completed\/cancelled/); + expect(no({}, {run_attempt: 2})).toThrow(/attempt 2/); + // The job must be this run's, in this repository. + expect(no({}, {run_id: 7001})).toThrow(/belongs to run 7001, not run 7000/); + expect(no({}, {html_url: 'https://github.com/hyperpolymath/mathesis/actions/runs/7001/job/70'})).toThrow(/is not a job of run 7000/); + expect(no({}, {html_url: 'https://github.com/someone/else/actions/runs/7000/job/70'})).toThrow(/is not a job of run 7000 in hyperpolymath\/mathesis/); + expect(no({}, {html_url: 'https://example.com/x'})).toThrow(/has no job URL/); +}); + +// ── The log ────────────────────────────────────────────────────────────────── + +test('the log is read through its timestamps and colour codes, and both gate runs must agree', () => { + const {gates, versions, guix} = parseGateLog(logFor(fx.files)); + expect(versions).toEqual({agda: '2.7.0.1', rocq: '8.18.0', lean: '4.29.0'}); + expect(guix).toBe(GUIX); + const top = gates.agda.get('verification/proofs/agda/Top.agda'); + expect(top).toEqual({status: 'PASS', sha256: sha(fx.files['verification/proofs/agda/Top.agda']), note: AGDA_NOTE(2, 'Agda.Builtin.Nat Agda.Primitive B Top').trim()}); + const text = logFor(fx.files); + const i = text.lastIndexOf(sha(fx.files['verification/proofs/coq/Top.v'])); + expect(() => parseGateLog(text.slice(0, i) + '0'.repeat(64) + text.slice(i + 64))).toThrow(/ran more than once in the log, and the runs disagree/); + expect(() => parseGateLog(text.replace(/RESULT: PASS[^\n]*\n(?![\s\S]*RESULT: PASS)/, 'RESULT: FAIL (1 failure(s))\n'))).toThrow(/ends in FAIL/); + expect(() => parseGateLog(text + 'Lean (version 4.30.0, x86_64-unknown-linux-gnu, Release)\n')).toThrow(/2 different Lean version/); + expect(() => parseGateLog(text.replace(/Agda version 2\.7\.0\.1/g, ''))).toThrow(/no Agda version/); + expect(() => parseGateLog(text + ` commit: ${'e'.repeat(40)}\n`)).toThrow(/2 different Guix channel commit/); + // A module listed twice in one gate run, a stale SHA-256 first, would be overwritten + // by the second entry; the log is refused instead. + const twice = text.split('\n').flatMap(l => /Unused\.agda +PASS/.test(l) + ? [l, ` sha256 ${'0'.repeat(64)}`, ` ${AGDA_NOTE(1, 'Agda.Primitive Unused').trim()}`, l] : [l]).join('\n'); + expect(() => parseGateLog(twice)).toThrow(/lists verification\/proofs\/agda\/Unused\.agda twice/); + // Nor may one module carry two SHA-256 lines, a stale one first. + const twoHashes = text.split('\n').flatMap(l => /Unused\.agda +PASS/.test(l) ? [l, ` sha256 ${'0'.repeat(64)}`] : [l]).join('\n'); + expect(() => parseGateLog(twoHashes)).toThrow(/prints two SHA-256s for one module/); +}); + +test('only a non-empty no-axiom report of the fixed gate is evidence for a graded root', () => { + const files = new Set(Object.keys(fx.files)); + const roots = {agda: ['verification/proofs/agda/Top.agda'], rocq: ['verification/proofs/coq/Top.v'], lean: ['verification/proofs/lean4/Top.lean']}; + const read = notes => parseGateLog(logFor(fx.files, {notes})).gates; + const {paths} = inputPaths(read({}), files, roots); + // A quarantined module that failed as expected is not an input; a gated module outside the cones is not either. + expect(inputPaths(parseGateLog(logFor(fx.files, {quarantined: true})).gates, files, roots).paths.rocq).toEqual(['verification/proofs/coq/Top.v']); + expect(paths).toEqual({agda: ['verification/proofs/agda/B.agda', 'verification/proofs/agda/Top.agda'], + rocq: ['verification/proofs/coq/Top.v'], lean: ['verification/proofs/lean4/Top.lean', 'verification/proofs/lean4/lean-toolchain']}); + // The earlier gate's reports audit no definition (audit finding 31). + expect(() => inputPaths(read({lean: 'axioms: none for 2 declared theorem(s)'}), files, roots)).toThrow(/form of an earlier gate/); + expect(() => inputPaths(read({v: 'Print Assumptions: closed under the global context for 1 theorem(s): t '}), files, roots)).toThrow(/form of an earlier gate/); + // The intermediate form of #107 audits definitions but runs no kernel check (coqchk). + expect(() => inputPaths(read({v: 'Print Assumptions: closed under the global context for 3 declaration(s): Not t u '}), files, roots)).toThrow(/form of an earlier gate/); + expect(() => inputPaths(read({lean: 'axioms: none for 0 theorem(s) and 0 definition(s) with a source range'}), files, roots)).toThrow(/inspected nothing/); + expect(() => inputPaths(read({v: `${COQCHK}Print Assumptions: closed under the global context for 0 declaration(s): `}), files, roots)).toThrow(/inspected nothing/); + expect(() => inputPaths(read({lean: 'axioms: Classical.choice'}), files, roots)).toThrow(/not a no-axiom report/); + expect(() => inputPaths(read({Top: AGDA_NOTE(3, 'Agda.Primitive B Top')}), files, roots)).toThrow(/counts 3 first-party modules/); + expect(() => inputPaths(read({}), files, {...roots, agda: ['verification/proofs/agda/Missing.agda']})).toThrow(/did not check/); +}); + +test('an id goes into an API path only as a positive integer', () => { + expect(idSegment('44105371764', 'run')).toBe('44105371764'); + expect(idSegment(134116153255, 'job')).toBe('134116153255'); + for (const bad of ['1/../../x', '../users', '%2e%2e', '-1', '0', '1e3', 1.5, '', ' 7', null, '99999999999999999999']) { + expect(() => idSegment(bad, 'run')).toThrow(/is not a positive integer id/); + expect(() => apiUrl('logs', bad)).toThrow(/is not a positive integer id/); + } + const api = 'https://api.github.com/repos/hyperpolymath/mathesis/actions'; + expect(apiUrl('run', '44105371764').href).toBe(`${api}/runs/44105371764`); + expect(apiUrl('jobs', 44105371764).href).toBe(`${api}/runs/44105371764/jobs?filter=latest&per_page=100`); + expect(apiUrl('logs', 134116153255).href).toBe(`${api}/jobs/134116153255/logs`); + expect(() => apiUrl('artifacts', 1)).toThrow(/no API request of kind/); +}); + +test('the span reader is not misled by parentheses and quotes in comments', () => { + const text = ';; ( a "comment" )\n(a :k "v ; not a comment )" ; ) comment\n (b #u5"x" #f))\n'; + const [a] = parseSpans(text); + expect(text.slice(a.start, a.end)).toBe(text.slice(text.indexOf('(a'), text.lastIndexOf(')') + 1)); + expect(a.items.map(x => x.type === 'list' ? 'list' : x.value)).toEqual(['a', ':k', 'v ; not a comment )', 'list']); + expect(() => parseSpans('(a "b)')).toThrow(/unterminated string/); + expect(() => parseSpans('(a))')).toThrow(/unbalanced \)/); +}); + +// ── The move, end to end ───────────────────────────────────────────────────── + +const WHY = 'whose gate audits definitions as well'; + +test('the move writes one new current receipt per leg and supersedes the old one; the reader and the git checker accept it', async () => { + const before = readFileSync(path.join(fx.repo, DEED), 'utf8'); + const {text, report, record} = await planMove({repo: fx.repo, evidence: evidenceFor(fx.c2, logFor(fx.files)), why: WHY, branch: 'main'}); + expect(readFileSync(path.join(fx.repo, DEED), 'utf8')).toBe(before); // planning writes nothing + expect(record.source_commit).toBe(fx.c2); + expect(record.claims.map(c => [c.id, c.pwg])).toEqual([['T-1', 'PWG-1']]); + for (const leg of record.claims[0].legs) { + const [cur, old] = leg.receipts; + expect(cur).toMatchObject({state: 'current', commit: fx.c2, run: 'https://github.com/hyperpolymath/mathesis/actions/runs/7000/job/70', axioms: []}); + expect(old).toMatchObject({state: 'superseded', commit: fx.c1, run: RUN0, + reason: `replaced by the CI receipt of run 7000 on ${fx.c2.slice(0, 7)}, ${WHY}`}); + // In the shape of the earlier superseded receipts (owner ruling 2026-11-17). + expect(old['inputs-sha256']).toBeUndefined(); + expect(old.inputs_manifest).toBeNull(); + expect(old.axioms).toBeNull(); + expect(leg.receipts.length).toBe(2); + } + expect(text).toContain(` :invocation "coqc Top.v"\n :state superseded\n` + + ` :reason "replaced by the CI receipt of run 7000 on ${fx.c2.slice(0, 7)}, ${WHY}")`); + const lean = record.claims[0].legs.find(l => l.prover === 'lean').receipts[0]; + expect(lean.checker).toBe('lean 4.29.0 (Guix 8edb9da)'); + expect(lean.inputs_manifest.map(f => f.path)).toEqual(PATHS.lean); + expect(lean['inputs-sha256']).not.toBe(fx.inputs.lean.digest); // Top.lean changed between c1 and c2 + const rocq = record.claims[0].legs.find(l => l.prover === 'rocq').receipts[0]; + expect(rocq['inputs-sha256']).toBe(fx.inputs.rocq.digest); // Top.v did not + // The header comment and every comment survive; the header names the new run and commit. + expect(text).toContain(`;; 7000 on main ${fx.c2.slice(0, 7)}; a comment with ( and ) and "quotes".`); + expect(text).toContain('; a comment ) here'); + expect(report.join('\n')).toMatch(/checker {2}check-proof-receipt\.sh --branch main: every check holds/); + // Moving it again to the same commit is refused. + writeFileSync(path.join(fx.repo, DEED), text); + try { + await expect(planMove({repo: fx.repo, evidence: evidenceFor(fx.c2, logFor(fx.files)), why: WHY, branch: 'main'})).rejects.toThrow(/already describes/); + } finally { + writeFileSync(path.join(fx.repo, DEED), before); + } +}); + +test('a green run with a quarantined module that failed as expected is accepted, and the module is in no manifest', async () => { + const {record} = await planMove({repo: fx.repo, evidence: evidenceFor(fx.c2, logFor(fx.files, {quarantined: true})), why: WHY, branch: 'main'}); + expect(record.claims[0].pwg).toBe('PWG-1'); + const rocq = record.claims[0].legs.find(l => l.prover === 'rocq').receipts[0]; + expect(rocq.inputs_manifest.map(m => m.path)).toEqual(['verification/proofs/coq/Top.v']); +}); + +test('the move is refused when the log, the tree or the history disagree', async () => { + const move = (opts = {}) => planMove({repo: fx.repo, evidence: evidenceFor(fx.c2, logFor(fx.files)), why: WHY, branch: 'main', ...opts}); + await expect(move({why: ' '})).rejects.toThrow(/--why is required/); + await expect(move({why: 'with a "quote"'})).rejects.toThrow(/refusing a deed string/); + // The log printed a SHA-256 that git does not have at the commit. + const stale = {...fx.files, 'verification/proofs/agda/B.agda': 'module B where\n-- not what was committed\n'}; + await expect(move({evidence: evidenceFor(fx.c2, logFor(stale))})).rejects.toThrow(/the log printed sha256 .*, git has/); + // A logged SHA-256 outside every manifest must hold too. + const unused = {...fx.files, 'verification/proofs/agda/Unused.agda': 'module Unused where\n-- not what was committed\n'}; + await expect(move({evidence: evidenceFor(fx.c2, logFor(unused))})).rejects.toThrow(/Unused\.agda: the log printed sha256 .*, git has/); + // One path given two SHA-256s by two provers' gates is refused, not overwritten. + const clash = logFor(fx.files).split('\n').flatMap(l => /Top\.v +PASS/.test(l) + ? [` ${'verification/proofs/agda'.padEnd(30)} ${'B.agda'.padEnd(24)} PASS`, ` sha256 ${'0'.repeat(64)}`, ` ${ROCQ_NOTE.trim()}`, l] : [l]).join('\n'); + await expect(move({evidence: evidenceFor(fx.c2, clash)})).rejects.toThrow(/B\.agda: the log prints two SHA-256s for it/); + // The Guix of the log is not the one the commit pins. + await expect(move({evidence: evidenceFor(fx.c2, logFor(fx.files, {guix: 'f'.repeat(40)}))})).rejects.toThrow(/is not the one build\/channels.scm pins/); + // A receipt moves forward only. + await expect(move({evidence: evidenceFor(fx.c1, logFor(fx.files))})).rejects.toThrow(/already describes/); + const side = git(fx.repo, 'commit-tree', `${fx.c2}^{tree}`, '-m', 'a root commit with no history'); // same tree, unrelated history + await expect(move({evidence: evidenceFor(side, logFor(fx.files))})).rejects.toThrow(/does not descend from/); + // The run's commit must be on the branch. + git(fx.repo, 'branch', '-f', 'old-main', fx.c1); + await expect(move({branch: 'old-main'})).rejects.toThrow(/is not on old-main/); + // Superseding drops a receipt's fields after :invocation, so a comment inside it is refused, not dropped. + const file = path.join(fx.repo, DEED), before = readFileSync(file, 'utf8'); + writeFileSync(file, before.replace(' (axioms)\n', ' (axioms) ; seen by hand\n')); + try { + await expect(move()).rejects.toThrow(/holds a comment, which superseding it would drop/); + } finally { + writeFileSync(file, before); + } + // Every current receipt must describe the :source-commit; a mixed receipt is not moved. + const parts = before.split(`:commit "${fx.c1}"`); + writeFileSync(file, parts.slice(0, 3).join(`:commit "${fx.c1}"`) + `:commit "${'b'.repeat(40)}"` + parts.slice(3).join(`:commit "${fx.c1}"`)); + try { + await expect(move()).rejects.toThrow(/lean leg: the current receipt is at b+, not the :source-commit/); + } finally { + writeFileSync(file, before); + } +}); + +// ── The claim pins ─────────────────────────────────────────────────────────── + +test('pin moves each row that pins the receipt to the commit that holds it, with its digest', async () => { + const {repo} = fx; + const {text} = await planMove({repo, evidence: evidenceFor(fx.c2, logFor(fx.files)), why: WHY, branch: 'main'}); + const row = (claim, extra = {}) => JSON.stringify({claim, id: `hyperpolymath/mathesis#${claim}`, receipt: {path: DEED, sha256: '0'.repeat(64)}, + repo: 'hyperpolymath/mathesis', source_commit: fx.c1, ...extra}); + const other = JSON.stringify({claim: 'X-1', id: 'o/r#X-1', receipt: null, repo: 'o/r', source_commit: fx.c1}); + put(repo, DEED, text); + put(repo, CLAIMS, [row('T-1'), other].join('\n') + '\n'); + git(repo, 'add', '-A'); git(repo, 'commit', '-q', '-m', 'c3: the new receipt'); + const c3 = git(repo, 'rev-parse', 'HEAD'); + const p = await planPin({repo, at: c3, branch: 'main'}); + expect(p.digest).toBe(sha(text)); + expect(p.notOnMain).toBe(false); + const [moved, kept] = p.text.trimEnd().split('\n'); + expect(JSON.parse(moved)).toMatchObject({source_commit: c3, receipt: {path: DEED, sha256: sha(text)}}); + expect(moved).toBe(jcs(JSON.parse(moved))); // I-JSON, serialised by JCS, as claims.jsonl is + expect(kept).toBe(other); // a row of another receipt is left byte for byte + put(repo, CLAIMS, [row('T-1'), row('T-9')].join('\n') + '\n'); + await expect(planPin({repo, at: c3, branch: 'main'})).rejects.toThrow(/has no claim T-9/); + await expect(planPin({repo, at: fx.c1, branch: 'main'})).rejects.toThrow(/git show/); // c1 holds no receipt +}); + +test('the committed receipt of Mathesis is readable by the span reader, with one current receipt per leg', () => { + const text = readFileSync(new URL(`../../${DEED}`, import.meta.url), 'utf8'); + const {legs} = currentReceipts(text); + expect(legs.length).toBeGreaterThan(0); + expect(new Set(legs.map(l => `${l.claim} ${l.prover}`)).size).toBe(legs.length); +});