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.
JavaScript agentic-codingai-agentsai-governanceautonomous-agentsclaude-codeclicode-reviewcodexcoding-agentsdeveloper-toolsgit-hooksllmlocal-firstmulti-agentsoftware-factoryspec-driven-developmentverificationvibe-coding
perf(core): catch up flow reports without rebuilding proof counts [R2]
Corey Olson79740f6c8e10
diff --git a/src/flow-report-counter.js b/src/flow-report-counter.jsnew file mode 100644index 00000000..805ca65f--- /dev/null+++ b/src/flow-report-counter.js@@ -0,0 +1,49 @@+/** Maintain an independently versioned report cursor so older writers require only tail catch-up [R1,R2]. */+import { moveFlowItem } from './stats-flow.js';+import {+ FLOW_REPORT_VERSION,+ advanceReport,+ affectedReportItems,+ indexReportItem,+ newReportPolicy,+ prepareReportMove,+ recordReport,+ recordReportPolicy,+ reportEvent,+} from './stats-flow-report.js';++/** An empty report has measured empty history; its cursor is independent of legacy proof writers. */+export function emptyReportCounter() { return { version: FLOW_REPORT_VERSION, last: 0, invalid: false }; }++/** Fold one event using the existing canonical lifecycle, without touching ordinary proof counters. */+export function appendReportCounter(event, detail, at, read, write) {+ const summary = read('report-summary') ?? emptyReportCounter();+ if (event.event_id <= summary.last) return;+ const reporting = reportEvent(event, detail);+ const policy = reporting ? read('report-policy') ?? newReportPolicy() : null;+ const before = reporting ? JSON.stringify(policy) : null;+ const key = `report-item:${event.item_id}`;+ const existing = event.item_id == null ? null : read(key);+ for (const id of reporting ? affectedReportItems(policy, event, detail) : []) {+ const item = read(`report-item:${id}`);+ if (!item) continue;+ advanceReport(item, policy, at);+ write(`report-item:${id}`, item);+ }+ if (reporting) recordReportPolicy(policy, event, detail, existing);+ if (event.item_id != null) {+ // Reload if a policy move just charged this same item's prior interval.+ const item = read(key) ?? { id: event.item_id, lane: 'unknown' };+ if (reporting) {+ advanceReport(item, policy, at);+ prepareReportMove(item, event, detail);+ moveFlowItem(item, event, detail, at);+ recordReport(item, event, detail);+ indexReportItem(policy, item);+ }+ if (reporting || !existing) write(key, item);+ }+ if (reporting && JSON.stringify(policy) !== before) write('report-policy', policy);+ summary.last = event.event_id;+ write('report-summary', summary);+}diff --git a/src/proof-counter-read.js b/src/proof-counter-read.jsindex 101c6d1d..4e13175d 100644--- a/src/proof-counter-read.js+++ b/src/proof-counter-read.js@@ -61,7 +61,10 @@ export function counterProofStats(board, boundary, now) {const creators = items.filter(item => item.creator !== undefined).map(item => ({ event_kind: 'add', item_id: item.id, event_by: item.creator }));const corrections = proofCounterRows(board, 'bar').map(row => decodedProofEvent({ ...row, event_kind: 'edit' }, row.detail));const verdicts = board.db.prepare('SELECT * FROM verdict ORDER BY verdict_id').all();- const flow = { reportPolicy: summary.reportVersion === FLOW_REPORT_VERSION && summary.reportLast === summary.last ? proofCounter(board, 'report-policy') : null, items: items.filter(item => item.id !== null && item.id !== undefined).map(item => item.flow),+ const report = proofCounter(board, 'report-summary');+ const reportCurrent = report?.version === FLOW_REPORT_VERSION && !report.invalid && report.last === summary.last;+ const flow = { reportPolicy: reportCurrent ? proofCounter(board, 'report-policy') : null,+ ...(reportCurrent ? { reportItems: proofCounterRows(board, 'report-item') } : {}), items: items.filter(item => item.id !== null && item.id !== undefined).map(item => item.flow),actors: actors.filter(actor => actor.id !== 'clock').map(actor => actor.flow), dailyEvents: proofCounterRows(board, 'daily') };return { ...counts, agentCount: attributed.agents.length, familyCount: attributed.families.length, ...attributed,bars: barStats([...creators, ...corrections], verdicts, boundary), caught: counterCaught(board, boundary),diff --git a/src/proof-counters.js b/src/proof-counters.jsindex bcfe8884..0f942acb 100644--- a/src/proof-counters.js+++ b/src/proof-counters.js@@ -1,11 +1,7 @@+import { appendReportCounter } from './flow-report-counter.js';+import { emptyReportCounter } from './flow-report-counter.js';import { reportEvent } from './stats-flow-report.js';-import { affectedReportItems } from './stats-flow-report.js';-import { indexReportItem } from './stats-flow-report.js';-import { prepareReportMove } from './stats-flow-report.js';import { newReportPolicy } from './stats-flow-report.js';-import { advanceReport } from './stats-flow-report.js';-import { recordReport } from './stats-flow-report.js';-import { recordReportPolicy } from './stats-flow-report.js';import { appendRigorCounter } from './rigor-counters.js';import { ensureRigorCounters } from './rigor-counters.js';import { NATIVE_FLAKE_ENGINE } from './flake-engine.js';@@ -67,7 +63,7 @@ export function clearProofCounters(board) {/** Start a bounded all-time aggregate; window-specific contributions live in separate event shards. */function emptySummary() {return { last: 0, submissions: 0, rejections: 0, merged: 0, mergedWithoutAccept: 0,- firstEventAt: null, lastEventAt: null, invalid: false, reportVersion: FLOW_REPORT_VERSION, reportLast: 0 };+ firstEventAt: null, lastEventAt: null, invalid: false };}/** Advance an item's acceptance audit separately from its flow lifecycle, whose rules differ. */@@ -118,6 +114,7 @@ function keepCatch(event, detail) {export function appendProofCounter(board, event) {appendRigorCounter(board, event);const summary = proofCounter(board, 'summary') ?? emptySummary();+ ensureReportCounters(board, summary.last);board.onProofStatsEvent?.(event);let detail;try { detail = JSON.parse(event.event_detail); }@@ -129,16 +126,7 @@ export function appendProofCounter(board, event) {if (summary.invalid) { put(board, 'summary', summary); return; }if (summary.firstEventAt === null || at < Date.parse(summary.firstEventAt)) summary.firstEventAt = event.event_at;if (summary.lastEventAt === null || at >= Date.parse(summary.lastEventAt)) summary.lastEventAt = event.event_at;- const reporting = reportEvent(event, detail);- const reportPolicy = reporting ? proofCounter(board, 'report-policy') ?? newReportPolicy() : null;- const beforePolicy = reporting ? JSON.stringify(reportPolicy) : null;- for (const id of reporting ? affectedReportItems(reportPolicy, event, detail) : []) {- const row = proofCounter(board, `item:${id}`);- if (!row) continue;- advanceReport(row.flow, reportPolicy, at);- put(board, `item:${id}`, row);- }- if (reporting) recordReportPolicy(reportPolicy, event, detail, event.item_id == null ? null : proofCounter(board, `item:${event.item_id}`)?.flow);+ appendReportCounter(event, detail, at, key => proofCounter(board, key), (key, value) => put(board, key, value));const key = `item:${event.item_id}`;const item = proofCounter(board, key) ?? { id: event.item_id, flow: { id: event.item_id, lane: 'unknown' } };const contribution = countItem(summary, item, event, detail);@@ -148,9 +136,7 @@ export function appendProofCounter(board, event) {if (event.event_kind === 'add' || event.event_kind === 'merged' && item.flow.mergeAt === undefined) {put(board, `daily:${event.event_id}`, { at, kind: event.event_kind === 'add' ? 'added' : 'merged' });}- if (reporting) { advanceReport(item.flow, reportPolicy, at); prepareReportMove(item.flow, event, detail); }- if (reporting) moveFlowItem(item.flow, event, detail, at);- if (reporting) { recordReport(item.flow, event, detail); indexReportItem(reportPolicy, item.flow); }+ if (reportEvent(event, detail)) moveFlowItem(item.flow, event, detail, at);put(board, key, item);} else if (['submit', 'accept', 'reject', 'merged'].includes(event.event_kind)) {// Legacy counts also audit a null item id, while flow deliberately excludes it.@@ -163,9 +149,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 });}- if (reporting && JSON.stringify(reportPolicy) !== beforePolicy) put(board, 'report-policy', reportPolicy);appendRuleProof(board, event, detail, key => proofCounter(board, key), (key, value) => put(board, key, value));- summary.reportLast = event.event_id;put(board, 'summary', summary);}@@ -179,12 +163,47 @@ function replayProofTail(board, cursor) {}}+/** Reset only the optional report when its representation changes; preserve established proof shards. */+function resetReportCounters(board) {+ board.db.prepare('DELETE FROM board_meta WHERE meta_key GLOB ?').run(`${PREFIX}report-*`);+ put(board, 'report-summary', emptyReportCounter());+ put(board, 'report-policy', newReportPolicy());+}++/** Catch up only missed report events after an older writer, without recounting other statistics. */+function ensureReportCounters(board, until) {+ let summary = proofCounter(board, 'report-summary');+ if (!summary || summary.version !== FLOW_REPORT_VERSION) {+ resetReportCounters(board);+ summary = proofCounter(board, 'report-summary');+ }+ if (summary.last >= until || summary.invalid) return;+ const read = board.db.prepare('SELECT * FROM event WHERE event_id>? AND event_id<=? ORDER BY event_id LIMIT 256');+ let cursor = summary.last;+ while (true) {+ const batch = read.all(cursor, until);+ if (!batch.length) return;+ for (const event of batch) {+ let detail;+ try { detail = JSON.parse(event.event_detail); } catch { detail = null; }+ const at = Date.parse(event.event_at);+ if (detail === null || !Number.isFinite(at)) {+ put(board, 'report-summary', { ...summary, invalid: true });+ return;+ }+ board.onFlowReportEvent?.(event);+ appendReportCounter(event, detail, at, key => proofCounter(board, key), (key, value) => put(board, key, value));+ cursor = event.event_id;+ }+ }+}+/** Rebuild from authoritative history inside the caller's existing write transaction. */export function rebuildProofCounters(board) {clearProofCounters(board);ensureRigorCounters(board);put(board, 'summary', emptySummary());- put(board, 'report-policy', newReportPolicy());+ resetReportCounters(board);replayProofTail(board, 0);}@@ -192,7 +211,8 @@ export function rebuildProofCounters(board) {export function ensureProofCounters(board) {ensureRigorCounters(board);const summary = proofCounter(board, 'summary');- if (!summary || summary.reportVersion !== FLOW_REPORT_VERSION || summary.reportLast !== summary.last) { rebuildProofCounters(board); return; }+ if (!summary) { rebuildProofCounters(board); return; }+ ensureReportCounters(board, summary.last);replayProofTail(board, summary.last);}diff --git a/src/stats-flow.js b/src/stats-flow.jsindex 56fc3bf0..edef7f06 100644--- a/src/stats-flow.js+++ b/src/stats-flow.js@@ -274,7 +274,7 @@ export function projectFlowStats(prepared, boundary, now, counts, { windowMinute...projectFlowWindow(prepared, windowMinutes, now),...(executionCapacity ? { executionCapacity } : {}),};- const report = projectReport(prepared.items, prepared.reportPolicy, now);+ const report = projectReport(prepared.reportItems ?? prepared.items, prepared.reportPolicy, now);return { report, ...(window === undefined ? {} : { window }), asOf: new Date(now).toISOString(), stages, queues: queueData.queues, unreviewed: queueData.unreviewed, daily: [...daily.values()].sort((a, b) => a.date.localeCompare(b.date)),submitsPerMerged: completedItems ? counts.submissions / completedItems : 0, activeAgents, bottleneck };}diff --git a/test/flow-report-cache.test.js b/test/flow-report-cache.test.jsindex 7b34b266..add81630 100644--- a/test/flow-report-cache.test.js+++ b/test/flow-report-cache.test.js@@ -50,10 +50,7 @@ test('flow report indexed projection matches canonical replay without hot raw hitest('old v2 counters retain fast metrics and explicitly unavailable reports until a writer rebuilds [R1,R2]', t => {const board = fixture(t);- const row = board.db.prepare('SELECT meta_value FROM board_meta WHERE meta_key=?').get('local:proof-stats:v2:summary');- const summary = JSON.parse(row.meta_value);- delete summary.data.reportVersion;- board.db.prepare('UPDATE board_meta SET meta_value=? WHERE meta_key=?').run(JSON.stringify(summary), 'local:proof-stats:v2:summary');+ board.db.prepare('DELETE FROM board_meta WHERE meta_key=?').run('local:proof-stats:v2:report-summary');const prepare = board.db.prepare.bind(board.db);const reads = [];board.db.prepare = sql => { reads.push(sql); return prepare(sql); };@@ -70,17 +67,23 @@ test('old v2 counters retain fast metrics and explicitly unavailable reports unttest('an older writer cannot make retained report capability look current [R1,R2,H16]', t => {const board = fixture(t);- const key = 'local:proof-stats:v2:summary';- const row = board.db.prepare('SELECT meta_value FROM board_meta WHERE meta_key=?').get(key);- const summary = JSON.parse(row.meta_value);- summary.data.reportLast = summary.data.last - 1;- board.db.prepare('UPDATE board_meta SET meta_value=? WHERE meta_key=?').run(JSON.stringify(summary), key);+ const reportKeys = 'local:proof-stats:v2:report-*';+ const retained = board.db.prepare('SELECT meta_key,meta_value FROM board_meta WHERE meta_key GLOB ?').all(reportKeys);+ store.addItem(board, { by: 'coordinator', lane: 'api', title: 'An older writer advances native history' });+ // Emulate an older writer's exact derived-state boundary: ordinary counters advance; report shards do not.+ board.db.prepare('DELETE FROM board_meta WHERE meta_key GLOB ?').run(reportKeys);+ for (const row of retained) board.db.prepare('INSERT INTO board_meta(meta_key,meta_value) VALUES (?,?)').run(row.meta_key, row.meta_value);const stale = proofStats(board);- assert.equal(stale.flow.queues.open.size, 1, 'existing native counters remain usable');+ assert.equal(stale.flow.queues.open.size, 2, 'existing native counters remain usable');assert.deepEqual(stale.flow.report, { available: false }, 'retained version alone cannot certify intervening older-writer events');const before = store.events(board);+ const proofEvents = [], reportEvents = [];+ board.onProofStatsEvent = event => proofEvents.push(event.event_id);+ board.onFlowReportEvent = event => reportEvents.push(event.event_id);ensureProofCounters(board);- assert.ok(proofStats(board).flow.report.items);+ assert.equal(proofStats(board).flow.report.items.length, 2);+ assert.deepEqual(proofEvents, [], 'report catch-up never rebuilds or recounts the existing proof statistics');+ assert.deepEqual(reportEvents, [before.at(-1).event_id], 'only the one missed native event is folded into the report');assert.deepEqual(store.events(board), before, 'repair changes derived counters only');});