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.

test(stats): require incremental proof parity [R1,R2,C7]

Corey Olsonf55eca4153cf
diff --git a/test/fixtures/proof-stats-legacy.js b/test/fixtures/proof-stats-legacy.js
new file mode 100644
index 00000000..fcbc3bac
--- /dev/null
+++ b/test/fixtures/proof-stats-legacy.js
@@ -0,0 +1,105 @@
+/** Public proof numbers derived from the append-only event log [R1,R2]. */
+import { events } from '../../src/board.js';
+import { Refused } from '../../src/refused.js';
+import { flowStats } from '../../src/stats-flow.js';
+import { barStats } from '../../src/bar-corrections.js';
+import { caughtFrom } from '../../src/caught.js';
+
+/** Parse an inclusive UTC date boundary, refusing malformed or impossible dates. */
+export function sinceDate(value) {
+ if (value === undefined || value === null) return null;
+ const text = String(value);
+ const match = /^(\d{4}-\d{2}-\d{2})(?:T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?Z)?$/u.exec(text);
+ const milliseconds = match ? Date.parse(text) : NaN;
+ if (!Number.isFinite(milliseconds) || new Date(milliseconds).toISOString().slice(0, 10) !== match?.[1]) {
+ throw new Refused('BAD_SINCE', 'the since date is invalid; use --since YYYY-MM-DD or an ISO UTC timestamp, for example --since 2026-10-08');
+ }
+ return new Date(milliseconds).toISOString();
+}
+
+/** Count moves in a window while auditing merges against their complete earlier history. */
+function countsFrom(history, boundary) {
+ const selected = history.filter((event) => !boundary || Date.parse(event.event_at) >= Date.parse(boundary));
+ const selectedIds = new Set(selected.map((event) => event.event_id));
+ let submissions = 0;
+ let rejections = 0;
+ const merged = new Set();
+ const withoutAccept = new Set();
+ const submitted = new Map();
+ const accepted = new Map();
+ for (const event of history) {
+ const detail = JSON.parse(event.event_detail);
+ if (event.event_kind === 'submit') {
+ submitted.set(event.item_id, detail.commit);
+ accepted.delete(event.item_id);
+ if (selectedIds.has(event.event_id)) submissions += 1;
+ } else if (event.event_kind === 'accept') {
+ if (detail.commit && detail.commit === submitted.get(event.item_id)) accepted.set(event.item_id, detail.commit);
+ } else if (event.event_kind === 'reject') {
+ accepted.delete(event.item_id);
+ if (selectedIds.has(event.event_id)) rejections += 1;
+ } else if (event.event_kind === 'merged' && selectedIds.has(event.event_id)) {
+ merged.add(event.item_id);
+ if (!accepted.has(event.item_id)) withoutAccept.add(event.item_id);
+ }
+ }
+ const dates = selected.map((event) => event.event_at).sort((a, b) => Date.parse(a) - Date.parse(b));
+ return {
+ since: boundary,
+ submissions,
+ rejections,
+ rejectionShare: submissions ? rejections / submissions : 0,
+ merged: merged.size,
+ mergedWithoutAccept: withoutAccept.size,
+ firstEventAt: dates[0] ?? null,
+ lastEventAt: dates.at(-1) ?? null,
+ };
+}
+
+/** Count lifecycle moves from one immutable read of the event history. */
+export function eventCounts(board, { since } = {}) {
+ return countsFrom(events(board), sinceDate(since));
+}
+
+/** Attribute only recorded actor families, retaining earlier declarations across date windows. */
+function actorsFrom(history, boundary) {
+ const declared = new Map();
+ const actors = new Map();
+ const families = new Map();
+ for (const event of history) {
+ const id = event.event_by;
+ if (id === 'board' || id === 'person') continue;
+ const detail = JSON.parse(event.event_detail);
+ if (Object.hasOwn(detail, 'family')) {
+ declared.set(id, typeof detail.family === 'string' && detail.family.trim() ? detail.family : 'unknown');
+ }
+ if (boundary && Date.parse(event.event_at) < Date.parse(boundary)) continue;
+ const name = declared.get(id) ?? 'unknown';
+ if (!actors.has(id)) actors.set(id, { id, moves: 0, families: new Set() });
+ const actor = actors.get(id);
+ actor.moves += 1;
+ actor.families.add(name);
+ if (!families.has(name)) families.set(name, { name, agents: new Set(), moves: 0 });
+ const family = families.get(name);
+ family.moves += 1;
+ family.agents.add(id);
+ }
+ const agents = [...actors.keys()].sort().map((id) => {
+ const actor = actors.get(id);
+ return { ...actor, families: [...actor.families].sort() };
+ });
+ const groups = [...families.keys()].sort().map((name) => {
+ const family = families.get(name);
+ return { ...family, agents: family.agents.size };
+ });
+ return { agentCount: agents.length, familyCount: groups.length, agents, families: groups };
+}
+
+/** Assemble the public statistics shared by the CLI and local board state from one event snapshot. */
+export function proofStats(board, { since, now = Date.now() } = {}) {
+ const boundary = sinceDate(since);
+ const history = events(board);
+ const counts = countsFrom(history, boundary);
+ const verdicts = board.db.prepare('SELECT * FROM verdict ORDER BY verdict_id').all();
+ return { ...counts, ...actorsFrom(history, boundary), bars: barStats(history, verdicts, boundary), caught: caughtFrom(history, verdicts, boundary), flow: flowStats(history, boundary, now, counts) };
+}
diff --git a/test/stats-counts.test.js b/test/stats-counts.test.js
index aa409166..9decbd91 100644
--- a/test/stats-counts.test.js
+++ b/test/stats-counts.test.js
@@ -2,8 +2,10 @@
import assert from 'node:assert/strict';
import { test } from 'node:test';
import { submitFixture } from './board-submit-fixture.js';
-import { addItem, claim, closeBoard, getItem, merged, openBoard, register, verify } from '../src/board.js';
+import { addItem, atomic, claim, closeBoard, events, getItem, logEvent, merged, openBoard, register, verify } from '../src/board.js';
import { exportBoard, importBoard } from '../src/exchange.js';
+import { proofStats as legacyProofStats } from './fixtures/proof-stats-legacy.js';
+import { proofStats } from '../src/stats.js';
import { eventCounts } from '../src/stats.js';
const COMMIT_ONE = '1'.repeat(40);
@@ -187,3 +189,61 @@ test('[R1,R2] imported historical merges without a matching current acceptance r
assert.equal(eventCounts(staleBoard).mergedWithoutAccept, 1,
'a new submit invalidates a previously accepted commit even when the current row remains verified');
});
+
+
+test('[R1,R2,C7] proof stats counters match a full re-parse after every event', t => {
+ const f = fixture(t);
+ const { board } = f;
+ let parsed = [];
+ board.onProofStatsEvent = event => parsed.push(event.event_id);
+ let id;
+ /** Compare independent full-history results at the same observation time and several windows. */
+ const same = current => {
+ const now = Date.parse('2026-02-01T00:00:00Z');
+ for (const since of [undefined, '2026-01-04', '2026-01-20']) {
+ assert.deepEqual(proofStats(current, { since, now }), legacyProofStats(current, { since, now }));
+ }
+ };
+ const steps = [
+ () => { id = addAndClaim(f, 'Incrementally counted lifecycle'); },
+ () => submitAt(f, id, COMMIT_ONE, TREE_ONE, '2026-01-03T10:00:00Z'),
+ () => verdictAt(f, id, 'REJECT', COMMIT_ONE, '2026-01-04T10:00:00Z'),
+ () => { f.setTime('2026-01-04T11:00:00Z'); claim(board, id, { agentId: 'web-1', lane: 'web', leaseMs: 86_400_000, freeze: freezeCriterion }); },
+ () => submitAt(f, id, COMMIT_TWO, TREE_TWO, '2026-01-05T10:00:00Z'),
+ () => verdictAt(f, id, 'ACCEPT', COMMIT_TWO, '2026-01-06T10:00:00Z'),
+ () => { f.setTime('2026-01-07T10:00:00Z'); merged(board, id, { agentId: 'coordinator', commit: MAIN_MERGE }); },
+ () => logEvent(board, 'web-1', 'join', null, { lane: 'web', family: 'changed-family' }),
+ () => logEvent(board, 'coordinator', 'edit', id, { reason: 'the original bar was wrong', barCorrection: { fields: ['check'], reject: { verdict: 1 } } }),
+ () => logEvent(board, 'coordinator', 'reopen', id, { conflict: { files: ['src/example.js'] } }),
+ () => logEvent(board, 'coordinator', 'refreeze', id, {}),
+ () => logEvent(board, 'web-2', 'reject', id, { commit: COMMIT_ONE, reason: 'TEST_FAILURE' }),
+ () => logEvent(board, 'coordinator', 'merged', id, { commit: MAIN_MERGE }),
+ () => logEvent(board, 'coordinator', 'test-quarantine', null, { file: 'test/example.test.js', name: 'a historical flake', tree: TREE_ONE }),
+ () => logEvent(board, 'sentinel', 'health-failure', null, { subject: 'unresolved obligation', cause: 'refusal:STUCK' }),
+ ];
+ for (const step of steps) {
+ const previous = events(board).at(-1)?.event_id ?? 0;
+ parsed = [];
+ step();
+ const appended = events(board).filter(event => event.event_id > previous).map(event => event.event_id);
+ assert.deepEqual(parsed, appended, 'each appended event is decoded once into its counters');
+ same(board);
+ assert.deepEqual(parsed, appended, 'reading counters never reparses earlier event details');
+ }
+ const document = exportBoard(board);
+ assert.equal(document.tables.board_meta.some(row => row.meta_key.startsWith('local:proof-stats:')), false,
+ 'derived counters are not native exported state');
+ const restored = importedBoard(t, document);
+ same(restored);
+ const restoredParses = [];
+ restored.onProofStatsEvent = event => restoredParses.push(event.event_id);
+ logEvent(restored, 'web-1', 'shout', null, {});
+ assert.deepEqual(restoredParses, [events(restored).at(-1).event_id], 'restored counters continue with one new event');
+ same(restored);
+ const before = proofStats(board, { now: 1_770_000_000_000 });
+ assert.throws(() => atomic(board, () => {
+ logEvent(board, 'web-1', 'submit', id, { commit: 'f'.repeat(40) });
+ throw new Error('rollback fixture');
+ }), /rollback fixture/u);
+ assert.deepEqual(proofStats(board, { now: 1_770_000_000_000 }), before, 'rollback preserves the prior projection');
+});