Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/ratchet.yml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 13 additions & 1 deletion product/contracts.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions scripts/check-prism-ratchet-tests.mjs
Original file line number Diff line number Diff line change
@@ -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 x -->\n<p>a</p>\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']])), []);
});
130 changes: 130 additions & 0 deletions scripts/check-prism-ratchet.mjs
Original file line number Diff line number Diff line change
@@ -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`);
}
}
Loading