Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

fix(rules): index committed proof coverage on writes [S1,A1,C7]

Corey Olsonf8a2bcf26369
diff --git a/src/proof-counters.js b/src/proof-counters.js
index b5e212e3..b065db9f 100644
--- a/src/proof-counters.js
+++ b/src/proof-counters.js
@@ -2,9 +2,10 @@ import { NATIVE_FLAKE_ENGINE } from './flake-engine.js';
/** Rebuildable, local proof projections maintained with the event that produced them [R1,R2]. */
import { moveFlowItem } from './stats-flow.js';
import { roleOf } from './stats-flow.js';
+import { appendRuleProof } from './rule-proof-index.js';
export const PROOF_COUNTER_PREFIX = 'local:proof-stats:';
-const PREFIX = `${PROOF_COUNTER_PREFIX}v1:`;
+const PREFIX = `${PROOF_COUNTER_PREFIX}v2:`;
/** Encode non-finite timestamps with external paths, so user detail objects cannot collide with markers. */
function encode(value) {
@@ -139,6 +140,7 @@ export function appendProofCounter(board, event) {
if (keepCatch(event, detail)) {
put(board, `catch:${event.event_id}`, { event_id: event.event_id, event_at: event.event_at, event_by: event.event_by, event_kind: event.event_kind, item_id: event.item_id, detail });
}
+ appendRuleProof(board, event, detail, key => proofCounter(board, key), (key, value) => put(board, key, value));
put(board, 'summary', summary);
}
diff --git a/src/rule-proof-counts.js b/src/rule-proof-counts.js
index d0df7521..5627ca35 100644
--- a/src/rule-proof-counts.js
+++ b/src/rule-proof-counts.js
@@ -1,24 +1,40 @@
-/** Counts distinguish named rule gates from committed trunk execution evidence [S1,A1,A2]. */
-import { createHash } from 'node:crypto';
-import { readFileSync } from 'node:fs';
+/** Read indexed rule proof without Git subprocesses or replaying raw gate history [S1,A1,A2,C7]. */
+import { repoInfo } from './git.js';
+import { trunkRevisionReader } from './trunk-revision.js';
import {
- mainCheckout,
- tryGit,
-} from './git.js';
-import { literalShellWords } from './literal-shell-words.js';
+ currentProofCounters,
+ proofCounter,
+ proofCounterRows,
+} from './proof-counters.js';
+import { proofCoverageFingerprint } from './rule-proof-coverage.js';
import { receiptFields } from './gate-run-receipt.js';
-
const WEEK = 7 * 86_400_000;
const FILE = /^[A-Za-z0-9_.-]+(?:\/[A-Za-z0-9_.-]+)*\.(?:[cm]?js|cts)$/u;
-/** Read schema-valid recorded runs only; old or incomplete receipts cannot prove a row. */
-function runs(board) {
- return board.db.prepare("SELECT event_id, event_by, event_detail FROM event WHERE event_kind = 'gate-run' ORDER BY event_id").all().flatMap(row => {
- try {
+/** Read only native red receipt candidates in bounded batches when an older projection cannot answer. */
+function legacyRedRuns(board) {
+ const read = board.db.prepare(`SELECT event_id,event_detail FROM event WHERE event_id>? AND event_kind='gate-run'
+ AND event_by='coordinator' AND json_extract(CASE WHEN json_valid(event_detail) THEN event_detail ELSE '{}' END,'$.run.green')=0
+ ORDER BY event_id LIMIT 256`);
+ const runs = [];
+ let cursor = 0;
+ while (true) {
+ const batch = read.all(cursor);
+ if (!batch.length) return runs;
+ for (const row of batch) {
+ cursor = row.event_id;
const run = receiptFields(JSON.parse(row.event_detail).run);
- return run ? [{ ...run, event: row.event_id, by: row.event_by }] : [];
- } catch { return []; }
- });
+ if (run && !run.green && run.head === run.main) runs.push({ event: row.event_id, kind: run.kind, at: run.at,
+ files: [...new Set(run.failures.map(failure => failure.file))] });
+ }
+ }
+}
+
+/** Readers never warm indexes; only red history has a legacy fallback, and green coverage stays unknown. */
+function redRuns(board) {
+ try { if (currentProofCounters(board)) return proofCounterRows(board, 'rule-red'); }
+ catch { /* An unreadable disposable projection cannot erase authoritative red history. */ }
+ return legacyRedRuns(board);
}
/** Only explicit test-file declarations identify a machine gate; review and opaque commands do not. */
@@ -27,90 +43,33 @@ function rowFiles(row) {
return files.length && files.every(file => FILE.test(file) && !file.split('/').includes('..')) ? files : [];
}
-/** Parse one literal Node test selection without pretending filtered, sharded or unknown options ran the file. */
-function selectedFiles(args, tracked, canonical) {
- const files = [];
- for (let at = 0; at < args.length; at += 1) {
- const arg = args[at];
- if (/^--test-concurrency=\d+$/u.test(arg)) continue;
- if (arg === '--test-concurrency') { if (!/^\d+$/u.test(args[++at] ?? '')) return []; continue; }
- if (arg.startsWith('-')) return [];
- const file = arg.replace(/^\.\//u, '');
- if (!FILE.test(file) || !tracked.has(file)) return [];
- files.push(file);
- }
- // The canonical runner deliberately selects tracked .test.js/.test.mjs entry points, not helper modules.
- if (!files.length) return canonical ? [...tracked]
- .filter(file => !file.split('/').some(part => part === 'node_modules' || part.startsWith('.')))
- .filter(file => /(?:^|\/)[^/]+\.test\.(?:js|mjs)$/u.test(file)) : [];
- return files;
-}
-
-/** Known external tools leave the parent shell's directory/environment intact; shell builtins do not. */
-function keepsShellContext(words) {
- return words && (words[0] === 'node' || words[0] === process.execPath
- || words[0] === 'npm' && (words[1] === 'test' && words.length === 2 || words[1] === 'run' && words.length === 3));
-}
-
-/** Resolve supported configured test commands at the receipt's committed source; opaque commands prove no file. */
-function includedFiles(root, commit, command, tracked, depth = 0) {
- if (depth > 4) return [];
- const words = literalShellWords(command);
- if (!words) {
- // A green AND chain proves its selected commands only if no earlier part changed the shell's cwd/environment.
- const parts = command.split(' && ');
- return parts.length > 1 && parts.every(part => keepsShellContext(literalShellWords(part)))
- ? parts.flatMap(part => includedFiles(root, commit, part, tracked, depth + 1)) : [];
- }
- if (words[0] === 'npm' && (words[1] === 'test' && words.length === 2 || words[1] === 'run' && words.length === 3)) {
- try {
- const source = tryGit(root, ['show', `${commit}:package.json`]);
- const manifest = JSON.parse(source.stdout);
- if (Object.hasOwn(manifest, 'workspaces') || tracked.has('.npmrc')) return [];
- const script = manifest.scripts?.[words[1] === 'test' ? 'test' : words[2]];
- return typeof script === 'string' ? includedFiles(root, commit, script, tracked, depth + 1) : [];
- } catch { return []; }
- }
- if (words[0] !== 'node' && words[0] !== process.execPath) return [];
- if (words[1] === '--test') return selectedFiles(words.slice(2), tracked, false);
- if (words[1]?.replace(/^\.\//u, '') === 'bin/run-tests.js' && tracked.has('bin/run-tests.js')) {
- const source = tryGit(root, ['show', `${commit}:bin/run-tests.js`]);
- if (source.status !== 0 || source.stdout !== readFileSync(new URL('../bin/run-tests.js', import.meta.url), 'utf8')) return [];
- return selectedFiles(words.slice(2), tracked, true);
- }
- return [];
-}
-
-/** Count the board's approved Spec rows against current trunk gates and independently recent trunk failures. */
+/** Current immutable source tuples prove coverage; recent indexed reds remain an independent count. */
export function ruleProofCounts(root, board, rows, { now = board.clock.now().getTime() } = {}) {
const approved = rows.filter(row => row.status === 'approved');
const gated = approved.filter(row => String(row.gate ?? '').trim());
const counts = { approved: approved.length, gated: gated.length, ungated: approved.length - gated.length, proven: 0, unproven: gated.length, contradicted: 0 };
if (!gated.length) return counts;
- const history = runs(board).filter(run => run.by === 'coordinator' && run.head === run.main && ['gate', 'land'].includes(run.kind));
- const failures = new Set(history.filter(run => !run.green && Date.parse(run.at) <= now && Date.parse(run.at) >= now - WEEK).flatMap(run => run.failures.map(failure => failure.file)));
+ const failures = new Set(redRuns(board).filter(run => ['gate', 'land'].includes(run.kind)
+ && Date.parse(run.at) <= now && Date.parse(run.at) >= now - WEEK).flatMap(run => run.files));
counts.contradicted = gated.filter(row => rowFiles(row).some(file => failures.has(file))).length;
- const trunk = mainCheckout(root);
- if (!trunk?.commit || !trunk.branch) return counts;
try {
- const config = JSON.parse(tryGit(root, ['show', `${trunk.commit}:pullboard.json`]).stdout);
- if (typeof config.gate !== 'string' || !config.gate.trim()) return counts;
- const command = createHash('sha256').update(config.gate).digest('hex');
- const tree = tryGit(root, ['rev-parse', `${trunk.commit}^{tree}`]).stdout.trim();
- if (!history.some(run => run.green && run.head === trunk.commit && run.tree === tree && run.command === command)) return counts;
- const tracked = new Set(tryGit(root, ['ls-tree', '-rz', trunk.commit]).stdout.split('\0')
- .filter(entry => /^(?:100644|100755) blob /u.test(entry)).map(entry => entry.slice(entry.indexOf('\t') + 1)));
- const included = new Set(includedFiles(root, trunk.commit, config.gate, tracked));
+ if (!currentProofCounters(board)) return counts;
+ const head = trunkRevisionReader(repoInfo(root))();
+ if (!head) return counts;
+ const fingerprint = proofCoverageFingerprint();
+ const source = proofCounter(board, `rule-green:${head}:${fingerprint}`);
+ if (!source || source.head !== head || source.fingerprint !== fingerprint) return counts;
+ const included = new Set(source.files);
counts.proven = gated.filter(row => { const files = rowFiles(row); return files.length > 0 && files.every(file => included.has(file)); }).length;
counts.unproven = gated.length - counts.proven;
- } catch { /* Missing committed inputs leave named gates unproven, never green. */ }
+ } catch { /* Unknown legacy coverage remains unproven; a read never re-derives it with Git. */ }
return counts;
}
-/** The time-dependent receipt window is a cache input, with an expiry even when no board event changes. */
+/** Indexed red timestamps expire independently of green proof and board mutation. */
export function ruleProofWindow(board, now) {
const at = Date.parse(now);
- const candidates = runs(board).filter(run => !run.green && run.by === 'coordinator' && run.head === run.main);
+ const candidates = redRuns(board);
const active = candidates.filter(run => Date.parse(run.at) <= at && Date.parse(run.at) >= at - WEEK);
const boundaries = candidates.flatMap(run => [Date.parse(run.at), Date.parse(run.at) + WEEK + 1]).filter(time => time > at);
return { events: active.map(run => run.event), nextExpiry: boundaries.length ? new Date(boundaries.reduce((a, b) => Math.min(a, b))).toISOString() : null };
diff --git a/src/rule-proof-coverage.js b/src/rule-proof-coverage.js
new file mode 100644
index 00000000..f9272eaf
--- /dev/null
+++ b/src/rule-proof-coverage.js
@@ -0,0 +1,86 @@
+/** Committed rule coverage is derived on the event writer, never by status or API readers [S1,C7]. */
+import { createHash } from 'node:crypto';
+import { readFileSync } from 'node:fs';
+import { tryGit } from './git.js';
+import { literalShellWords } from './literal-shell-words.js';
+const FILE = /^[A-Za-z0-9_.-]+(?:\/[A-Za-z0-9_.-]+)*\.(?:[cm]?js|cts)$/u;
+
+/** Bind cached interpretation to the exact installed parser, canonical runner and runtime command identity. */
+export function proofCoverageFingerprint() {
+ const hash = createHash('sha256').update(JSON.stringify([process.execPath, process.version]));
+ for (const name of ['./rule-proof-coverage.js', './literal-shell-words.js', '../bin/run-tests.js']) {
+ hash.update(name).update(readFileSync(new URL(name, import.meta.url)));
+ }
+ return hash.digest('hex');
+}
+
+/** Parse one literal Node test selection without pretending filtered, sharded or unknown options ran the file. */
+function selectedFiles(args, tracked, canonical) {
+ const files = [];
+ for (let at = 0; at < args.length; at += 1) {
+ const arg = args[at];
+ if (/^--test-concurrency=\d+$/u.test(arg)) continue;
+ if (arg === '--test-concurrency') { if (!/^\d+$/u.test(args[++at] ?? '')) return []; continue; }
+ if (arg.startsWith('-')) return [];
+ const file = arg.replace(/^\.\//u, '');
+ if (!FILE.test(file) || !tracked.has(file)) return [];
+ files.push(file);
+ }
+ // The canonical runner deliberately selects tracked .test.js/.test.mjs entry points, not helper modules.
+ if (!files.length) return canonical ? [...tracked]
+ .filter(file => !file.split('/').some(part => part === 'node_modules' || part.startsWith('.')))
+ .filter(file => /(?:^|\/)[^/]+\.test\.(?:js|mjs)$/u.test(file)) : [];
+ return files;
+}
+
+/** Known external tools leave the parent shell's directory/environment intact; shell builtins do not. */
+function keepsShellContext(words) {
+ return words && (words[0] === 'node' || words[0] === process.execPath
+ || words[0] === 'npm' && (words[1] === 'test' && words.length === 2 || words[1] === 'run' && words.length === 3));
+}
+
+/** Resolve supported configured test commands at the receipt's committed source; opaque commands prove no file. */
+function includedFiles(root, commit, command, tracked, depth = 0) {
+ if (depth > 4) return [];
+ const words = literalShellWords(command);
+ if (!words) {
+ // A green AND chain proves its selected commands only if no earlier part changed the shell's cwd/environment.
+ const parts = command.split(' && ');
+ return parts.length > 1 && parts.every(part => keepsShellContext(literalShellWords(part)))
+ ? parts.flatMap(part => includedFiles(root, commit, part, tracked, depth + 1)) : [];
+ }
+ if (words[0] === 'npm' && (words[1] === 'test' && words.length === 2 || words[1] === 'run' && words.length === 3)) {
+ try {
+ const source = tryGit(root, ['show', `${commit}:package.json`]);
+ const manifest = JSON.parse(source.stdout);
+ if (Object.hasOwn(manifest, 'workspaces') || tracked.has('.npmrc')) return [];
+ const script = manifest.scripts?.[words[1] === 'test' ? 'test' : words[2]];
+ return typeof script === 'string' ? includedFiles(root, commit, script, tracked, depth + 1) : [];
+ } catch { return []; }
+ }
+ if (words[0] !== 'node' && words[0] !== process.execPath) return [];
+ if (words[1] === '--test') return selectedFiles(words.slice(2), tracked, false);
+ if (words[1]?.replace(/^\.\//u, '') === 'bin/run-tests.js' && tracked.has('bin/run-tests.js')) {
+ const source = tryGit(root, ['show', `${commit}:bin/run-tests.js`]);
+ if (source.status !== 0 || source.stdout !== readFileSync(new URL('../bin/run-tests.js', import.meta.url), 'utf8')) return [];
+ return selectedFiles(words.slice(2), tracked, true);
+ }
+ return [];
+}
+
+/** Resolve one immutable commit's policy/tree/coverage once; incomplete source remains unknown. */
+export function committedRuleCoverage(root, head) {
+ try {
+ const configResult = tryGit(root, ['show', `${head}:pullboard.json`]);
+ if (configResult.status !== 0) return null;
+ const config = JSON.parse(configResult.stdout);
+ if (typeof config.gate !== 'string' || !config.gate.trim()) return null;
+ const tree = tryGit(root, ['rev-parse', `${head}^{tree}`]);
+ const entries = tryGit(root, ['ls-tree', '-rz', head]);
+ if (tree.status !== 0 || entries.status !== 0) return null;
+ const tracked = new Set(entries.stdout.split('\0').filter(entry => /^(?:100644|100755) blob /u.test(entry))
+ .map(entry => entry.slice(entry.indexOf('\t') + 1)));
+ return { head, tree: tree.stdout.trim(), command: createHash('sha256').update(config.gate).digest('hex'),
+ fingerprint: proofCoverageFingerprint(), files: [...new Set(includedFiles(root, head, config.gate, tracked))] };
+ } catch { return null; }
+}
diff --git a/src/rule-proof-index.js b/src/rule-proof-index.js
new file mode 100644
index 00000000..8aaabdf3
--- /dev/null
+++ b/src/rule-proof-index.js
@@ -0,0 +1,43 @@
+/** Validated coordinator run facts and immutable source interpretation live only in local proof projections [S1,R2,C7]. */
+import { realpathSync } from 'node:fs';
+import { join } from 'node:path';
+import { repoInfo } from './git.js';
+import { receiptFields } from './gate-run-receipt.js';
+import {
+ committedRuleCoverage,
+ proofCoverageFingerprint,
+} from './rule-proof-coverage.js';
+
+/** Use the registered coordinator's repository only when it owns this exact board file. */
+function sourceRoot(board) {
+ const root = board.db.prepare("SELECT agent_path FROM agent WHERE agent_id='coordinator'").get()?.agent_path;
+ const file = board.db.prepare('PRAGMA database_list').all().find(row => row.name === 'main')?.file;
+ if (!root || !file) return null;
+ try {
+ return realpathSync(join(repoInfo(root).commonDir, 'pullboard', 'board.sqlite')) === realpathSync(file) ? root : null;
+ } catch { return null; }
+}
+
+/** Fold one already-ordered native event; malformed/legacy receipts never gain proof by indexing. */
+export function appendRuleProof(board, event, detail, get, put) {
+ if (event.event_kind !== 'gate-run' || event.event_by !== 'coordinator') return;
+ const run = receiptFields(detail.run);
+ if (!run || run.head !== run.main) return;
+ if (!run.green) {
+ put(`rule-red:${event.event_id}`, { event: event.event_id, kind: run.kind, at: run.at,
+ files: [...new Set(run.failures.map(failure => failure.file))] });
+ return;
+ }
+ if (!['gate', 'land'].includes(run.kind)) return;
+ const root = sourceRoot(board);
+ if (!root) return;
+ const fingerprint = proofCoverageFingerprint();
+ const key = `rule-source:${run.head}:${fingerprint}:${encodeURIComponent(root)}`;
+ let source = get(key);
+ if (!source) {
+ source = committedRuleCoverage(root, run.head);
+ if (source) put(key, source);
+ }
+ if (!source || source.head !== run.head || source.tree !== run.tree || source.command !== run.command || source.fingerprint !== fingerprint) return;
+ put(`rule-green:${run.head}:${fingerprint}`, { ...source, event: event.event_id });
+}