diff --git a/.github/workflows/ratchet.yml b/.github/workflows/ratchet.yml new file mode 100644 index 0000000..a2ae698 --- /dev/null +++ b/.github/workflows/ratchet.yml @@ -0,0 +1,28 @@ +name: Only Prism is implemented +# Non-Prism code may only shrink: scripts/check-prism-ratchet.mjs measures the tree against the +# commit's first parent (a pull request's merge commit has the base branch there). It reads two +# commits and nothing installed, so it runs on every change. +on: + push: + branches: [main, 'feat/**'] + pull_request: + workflow_dispatch: +permissions: + contents: read +concurrency: + group: ratchet-${{ github.ref }} + cancel-in-progress: true +jobs: + ratchet: + runs-on: ubuntu-24.04 + timeout-minutes: 5 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 + with: + # The ceiling is the first parent, so the checkout needs that one commit more. + fetch-depth: 2 + persist-credentials: false + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 + with: + node-version: '24' + - run: node scripts/check-prism-ratchet.mjs diff --git a/product/contracts.json b/product/contracts.json index 2eaa8b8..b52d679 100644 --- a/product/contracts.json +++ b/product/contracts.json @@ -2132,6 +2132,10 @@ "path": ".github/workflows/prism.yml", "sha256": "4fe595efda75eabd6d81db25b87a44d0d3fa186deb37b1c50b040095f488d093" }, + { + "path": ".github/workflows/ratchet.yml", + "sha256": "d687028f7cc906b64af52c1b3439f79d0ac39b9c11bee88a0ffbde93203ed7cc" + }, { "path": ".github/workflows/source-authoring.yml", "sha256": "28ddee0d1feebc5bf0eae419c1e3c30b570d3e08d1c6e064bfcaf65ddafef374" @@ -2948,6 +2952,14 @@ "path": "scripts/check-model.mjs", "sha256": "7aacb22c351ee9b032b5ee4236a26f85aad3504e1fac34d77b11af4116bfe297" }, + { + "path": "scripts/check-prism-ratchet-tests.mjs", + "sha256": "0775b2c3b0005b48a304de758215979b1676d41a588543d915b7b5f9c1b295e4" + }, + { + "path": "scripts/check-prism-ratchet.mjs", + "sha256": "47936c9c3d64e6f21f298a8ce8325160386b35579f288f0c68b4df2137a7fca5" + }, { "path": "scripts/check-product.mjs", "sha256": "63f6d356456b89c13a17b017b0a6d4fb2b2ba43e126b2d98b5dd059fabf83829" @@ -3209,5 +3221,5 @@ "sha256": "02263c2d4bf1a1913a673466554c19593b5a6c39b22a2da32ce4375675868347" } ], - "sourceDigest": "sha256:1de8da497644d607610fb331cfa7d3056c8b8846886889e85cc6b3916b4587ff" + "sourceDigest": "sha256:6db33466ecb20b1303e4022fd3289ecca405c17f26a300fc7df3b0c21b87b3cd" } diff --git a/scripts/check-prism-ratchet-tests.mjs b/scripts/check-prism-ratchet-tests.mjs new file mode 100644 index 0000000..0681ea5 --- /dev/null +++ b/scripts/check-prism-ratchet-tests.mjs @@ -0,0 +1,33 @@ +// The ratchet's planted defects: each must be refused, and what Prism or comments add must not. +import assert from 'node:assert/strict'; +import { test } from 'node:test'; +import { countLines, language, measure, refusals } from './check-prism-ratchet.mjs'; + +test('counts code, not comments or blank lines', () => { + assert.equal(countLines('// why\n/* a\n b */\nconst a = 1;\n\n/* x */ let b;\n', language('a.js')), 2); + assert.equal(countLines('#!/bin/sh\n# why\necho hi\n', language('a.sh')), 1); + assert.equal(countLines('\n
a
\n', language('a.html')), 1); +}); + +test('Prism, data and the generated model are not counted', () => { + const m = measure([['app/src/Stitch.lex.tex', 'def a := 1\n'], ['product/facets.json', '{}\n'], ['src/layer1-uor/generated-model.js', 'x()\n'], ['src/a.js', 'a()\n'], ['src/a.constructor', 'x\n']]); + assert.deepEqual([...m.keys()], ['src/a.js']); +}); + +test('refuses a new non-Prism file', () => { + const refused = refusals(measure([['src/a.js', 'a()\nb()\n']]), measure([['src/a.js', 'a()\n'], ['src/b.js', 'b()\n']])); + assert.equal(refused.length, 1); + assert.match(refused[0], /src\/b\.js/); +}); + +test('refuses net growth and names what grew', () => { + const refused = refusals(measure([['src/a.js', 'a()\nb()\n'], ['src/c.js', 'c()\n']]), measure([['src/a.js', 'a()\n'], ['src/c.js', 'c()\nd()\ne()\nf()\n']])); + assert.equal(refused.length, 1); + assert.match(refused[0], /grew by 2 lines \(3 to 5\); grown: src\/c\.js \+3\./); +}); + +test('admits shrinking, growth paid for by deletion, and comments', () => { + const before = measure([['src/a.js', 'a()\nb()\n'], ['src/c.js', 'c()\n']]); + assert.deepEqual(refusals(before, measure([['src/a.js', 'a()\nb()\nd()\n']])), []); + assert.deepEqual(refusals(before, measure([['src/a.js', '// why\n\na()\nb()\n'], ['src/c.js', 'c()\n']])), []); +}); diff --git a/scripts/check-prism-ratchet.mjs b/scripts/check-prism-ratchet.mjs new file mode 100644 index 0000000..e6e8e27 --- /dev/null +++ b/scripts/check-prism-ratchet.mjs @@ -0,0 +1,130 @@ +// Only Prism is implemented: the shrink-only ratchet of UOR-Foundation/template's audit-prism. +// +// Stitch's behavior belongs in its Prism model (app/src/*.lex.tex). Every other language that +// executes, hand-written JavaScript, Rust, Python, shell, CSS or HTML, may only shrink. The +// ceiling is the commit's first parent, measured rather than recorded: a pull request's merge +// commit has the base branch as first parent and main has the previous main, so a removal lowers +// it as it merges. Refused: a non-Prism file the parent lacks, and net growth in non-Prism lines +// (non-blank, outside comments, so explaining a decision is free). A shallow clone that hides +// the parent fails. The ratchet starts at the commit that installs it. +// +// node scripts/check-prism-ratchet.mjs +import { execFileSync } from 'node:child_process'; +import { existsSync, lstatSync, readFileSync } from 'node:fs'; +import { fileURLToPath } from 'node:url'; + +const root = fileURLToPath(new URL('../', import.meta.url)); +const SELF = 'scripts/check-prism-ratchet.mjs'; + +// The model check (`scripts/check-model.mjs`, in `npm test`) refuses this file unless it is +// exactly what the Prism model generates, so it is Prism output. +export const PRISM_OUTPUTS = new Set(['src/layer1-uor/generated-model.js']); + +const C = ['//', '/*', '*/'], HASH = ['#'], HTML = [null, ''], LEAN = ['--', '/-', '-/']; +const LANGUAGES = { + js: C, mjs: C, cjs: C, jsx: C, ts: C, tsx: C, mts: C, cts: C, rs: C, css: C, scss: C, c: C, h: C, cpp: C, + html: HTML, py: HASH, sh: HASH, bash: HASH, awk: HASH, lean: LEAN, +}; + +export function language(path) { + const name = path.split('/').pop(); + const dot = name.lastIndexOf('.'); + const extension = name.slice(dot + 1); + return dot > 0 && Object.hasOwn(LANGUAGES, extension) ? LANGUAGES[extension] : undefined; +} + +/** Non-blank lines outside comments. */ +export function countLines(text, [line, open, close]) { + let inBlock = false, count = 0; + for (const raw of text.split('\n')) { + let rest = raw.trim(); + if (inBlock) { + const end = rest.indexOf(close); + if (end < 0) continue; + inBlock = false; + rest = rest.slice(end + close.length).trim(); + } + while (open && rest.startsWith(open)) { + const end = rest.indexOf(close, open.length); + if (end < 0) { inBlock = true; rest = ''; break; } + rest = rest.slice(end + close.length).trim(); + } + if (!rest || (line && rest.startsWith(line))) continue; + count++; + } + return count; +} + +export function measure(entries) { + const files = new Map(); + for (const [path, text] of entries) { + const syntax = language(path); + if (syntax && !PRISM_OUTPUTS.has(path)) files.set(path, countLines(text, syntax)); + } + return files; +} + +const total = (files) => [...files.values()].reduce((a, b) => a + b, 0); + +export function refusals(ceiling, current) { + const out = []; + const added = [...current.keys()].filter((path) => !ceiling.has(path)); + if (added.length) { + out.push(`${added.length} new non-Prism file(s): ${added.slice(0, 20).join(', ')}${added.length > 20 ? ', ...' : ''}. Author the behavior in the Prism model (app/src/*.lex.tex).`); + } + const now = total(current), before = total(ceiling); + if (now > before) { + const grown = [...current].map(([path, n]) => [path, n - (ceiling.get(path) ?? 0)]).filter(([, d]) => d > 0) + .sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0])).slice(0, 10).map(([p, d]) => `${p} +${d}`); + out.push(`non-Prism implementation grew by ${now - before} lines (${before} to ${now}); grown: ${grown.join(', ')}. It may only shrink: move the logic into the Prism model, or delete as much as you add.`); + } + return out; +} + +const git = (...args) => execFileSync('git', args, { cwd: root, maxBuffer: 1 << 30 }); + +function worktree() { + const entries = []; + for (const path of git('ls-files', '-z', '--cached').toString().split('\0')) { + if (!path || !language(path)) continue; + const full = root + path; + if (!existsSync(full) || lstatSync(full).isSymbolicLink() || !lstatSync(full).isFile()) continue; + entries.push([path, readFileSync(full, 'utf8')]); + } + return measure(entries); +} + +function commit(id) { + const entries = []; + for (const entry of git('ls-tree', '-r', '-z', id).toString().split('\0')) { + const tab = entry.indexOf('\t'); + if (tab < 0) continue; + const [mode, type, oid] = entry.slice(0, tab).split(' '), path = entry.slice(tab + 1); + if (mode === '120000' || type !== 'blob' || !language(path)) continue; + entries.push([path, git('cat-file', 'blob', oid).toString('utf8')]); + } + return measure(entries); +} + +if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) { + const current = worktree(); + let parent; + try { parent = git('rev-parse', '--verify', '-q', 'HEAD^1^{commit}').toString().trim(); } catch { parent = null; } + if (!parent) { + if (git('rev-parse', '--is-shallow-repository').toString().startsWith('true')) { + console.error('prism-ratchet: the clone is shallow, so the parent commit, the ratchet\'s ceiling, cannot be read; fetch at least depth 2'); + process.exit(1); + } + console.log(`prism-ratchet: a root commit; non-Prism implementation is ${total(current)} lines in ${current.size} files`); + } else if (!git('ls-tree', '--name-only', parent, SELF).toString().trim()) { + console.log(`prism-ratchet: installed here; the parent ran no ratchet, so this commit sets the ceiling: ${total(current)} lines in ${current.size} files`); + } else { + const ceiling = commit(parent); + const refused = refusals(ceiling, current); + if (refused.length) { + console.error(`prism-ratchet: only Prism may be implemented here, measured against ${parent.slice(0, 12)}:\n - ${refused.join('\n - ')}`); + process.exit(1); + } + console.log(`prism-ratchet: non-Prism implementation is ${total(current)} lines in ${current.size} files (${total(ceiling)} lines, ${ceiling.size} files in the parent); it only shrinks`); + } +}