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
fix(health): item history holds its statement through the loop [A6]
Corey Olson5967fb3eb110
diff --git a/src/health.js b/src/health.jsindex da2f09e6..fbdb2c7c 100644--- a/src/health.js+++ b/src/health.js@@ -352,17 +352,36 @@ function releaseOverdue(db, root, now, deadlineMs, history) {});}-/** Each item's latest event of every kind, its latest lifecycle move and its first merge record. */-function itemHistory(db) {+/**+ * Each item's latest event of every kind, its latest lifecycle move and its first merge record.+ *+ * The statement is held for the whole loop and touched after it (#874): Node 22.13's node:sqlite+ * finalizes an unreferenced statement when garbage collection runs mid-iterate, which crashed+ * resume with "statement has been finalized" on a large board. Exported for that regression test.+ */+export function itemHistory(db) {const history = new Map();- for (const event of db.prepare('SELECT event_id, event_at, event_by, event_kind, item_id, event_detail FROM event WHERE item_id IS NOT NULL ORDER BY event_id').iterate()) {+ const statement = db.prepare('SELECT event_id, event_at, event_by, event_kind, item_id, event_detail FROM event WHERE item_id IS NOT NULL ORDER BY event_id');+ try { readItemHistory(statement.iterate(), history); }+ finally { keepAlive(statement); }+ return history;+}++/** Keep a value reachable until this call, so no optimizer treats it as dead early. */+function keepAlive(value) {+ if (value === KEEP_ALIVE_SENTINEL) throw new Error('unreachable');+}+const KEEP_ALIVE_SENTINEL = Symbol('keep alive');++/** Fold item events, oldest first, into each item's history entry. */+function readItemHistory(events, history) {+ for (const event of events) {let entry = history.get(event.item_id);if (!entry) history.set(event.item_id, entry = { last: new Map(), lifecycle: null, firstMerged: null });entry.last.set(event.event_kind, event);if (LIFECYCLE.has(event.event_kind)) entry.lifecycle = event;if (event.event_kind === 'merged' && !entry.firstMerged) entry.firstMerged = event.event_at;}- return history;}/** Decisions to the coordinator or the person, and the person's requests, that nobody has closed. */diff --git a/test/health.test.js b/test/health.test.jsindex 9b5a568d..8ddaa685 100644--- a/test/health.test.js+++ b/test/health.test.js@@ -7,14 +7,15 @@ import assert from 'node:assert/strict';import { createHash } from 'node:crypto';import { mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';-import { join } from 'node:path';+import { join, resolve } from 'node:path';import { test } from 'node:test';import { DatabaseSync } from 'node:sqlite';+import { pathToFileURL } from 'node:url';import * as store from '../src/board.js';import { main } from '../src/cli.js';import { boardHealth } from '../src/health.js';import { JSON_SHAPES } from '../src/json.js';-import { runFixtureGit } from './fixture-child.js';+import { runFixtureChild, runFixtureGit } from './fixture-child.js';const HOUR = 3_600_000;const NOW = Date.parse('2026-10-17T12:00:00.000Z');@@ -277,3 +278,24 @@ test('health on a healthy board says nothing is stuck [A6]', async (t) => {assert.equal((await cli(box.root, ['health'])).stdout, 'nothing is stuck\n');assert.deepEqual(boardDigest(box.file), before, 'health changes nothing on a healthy board');});++test('item history survives garbage collection mid-read [A6]', () => {+ // A stand-in database whose statement lives only while something references it, as node:sqlite's+ // does on Node 22.13, with garbage collection forced while the history is read (#874).+ const health = pathToFileURL(resolve(import.meta.dirname, '../src/health.js')).href;+ const script = `+ import { DatabaseSync } from 'node:sqlite';+ import { itemHistory } from ${JSON.stringify(health)};+ const real = new DatabaseSync(':memory:');+ real.exec('CREATE TABLE event (event_id INTEGER PRIMARY KEY, event_at TEXT, event_by TEXT, event_kind TEXT, item_id INTEGER, event_detail TEXT)');+ const insert = real.prepare('INSERT INTO event (event_at, event_by, event_kind, item_id, event_detail) VALUES (?, ?, ?, ?, ?)');+ for (let i = 0; i < 20000; i += 1) insert.run('2026-10-18T00:00:00.000Z', 'agent', i % 3 ? 'claim' : 'submit', i % 50, '{}');+ // Defined outside any scope that holds the statement, so only the code under test can keep it alive.+ const collecting = (rows) => { let read = 0; return { [Symbol.iterator]() { return this; }, next() { read += 1; if (read % 500 === 0) globalThis.gc(); return rows.next(); } }; };+ const db = { prepare: (sql) => { const statement = real.prepare(sql); return { iterate: () => collecting(statement.iterate()) }; } };+ process.stdout.write(String(itemHistory(db).size));+ `;+ const child = runFixtureChild(process.execPath, ['--expose-gc', '--no-warnings', '--input-type=module', '-e', script], { encoding: 'utf8' });+ assert.equal(child.status, 0, `reading item history must not lose its statement to garbage collection: ${child.stderr}`);+ assert.equal(child.stdout, '50', 'every item with events has a history entry');+});