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(board): the board refuses status writes outside moveItem [M1]

Corey Olsond1fc421f62bd
diff --git a/src/board.js b/src/board.js
index 94ac3b4b..ebffc218 100644
--- a/src/board.js
+++ b/src/board.js
@@ -137,9 +137,30 @@ export function openBoard(file, clock = systemClock) {
migrate(db);
const board = { db, clock };
guardStore(board);
+ guardMoves(board);
return board;
}
+/**
+ * Make this connection refuse any change to an item's status that does not come through moveItem
+ * (M1). A TEMP trigger, which lives only in this process's connection, lets the status column
+ * change only while moveItem holds a token in a TEMP table, so a status written anywhere else in
+ * this code, however it is spelled, is refused by SQLite itself. Recursive triggers stay on, so a
+ * REPLACE INTO, which deletes the old row first, meets the board file's own delete trigger. An
+ * agent's own connection never sees these TEMP objects; the board file's triggers (M3) govern it.
+ *
+ * @param {any} board
+ */
+function guardMoves(board) {
+ board.db.exec(`
+ PRAGMA recursive_triggers = ON;
+ CREATE TEMP TABLE IF NOT EXISTS moving (token INTEGER);
+ CREATE TEMP TRIGGER IF NOT EXISTS status_through_moves BEFORE UPDATE OF item_status ON main.item
+ WHEN NOT EXISTS (SELECT 1 FROM temp.moving)
+ BEGIN SELECT RAISE(ABORT, 'STATUS_OUTSIDE_MOVE: only moveItem in src/board.js changes an item''s status'); END;
+ `);
+}
+
/** The board's PRAGMA user_version once its triggers are in: losing one after that is news. */
const GUARDED = 1;
@@ -578,7 +599,12 @@ function moveItem(board, id, verb, { checks, set = () => ({}), before = () => {}
const requires = STATES.find((state) => state.id === move.to).requires;
const missing = requires.filter((field) => !String({ ...item, ...fields }[field] ?? '').trim());
if (missing.length) throw new Error(`move ${verb}: ${move.to} needs ${missing.join(', ')}`);
- setItem(board, id, fields);
+ board.db.exec('INSERT INTO temp.moving (token) VALUES (1)');
+ try {
+ setItem(board, id, fields);
+ } finally {
+ board.db.exec('DELETE FROM temp.moving');
+ }
return item;
}
diff --git a/test/machine.test.js b/test/machine.test.js
index 09c85a14..2007f7de 100644
--- a/test/machine.test.js
+++ b/test/machine.test.js
@@ -3,9 +3,10 @@
* failing on a broken copy, and in step with the refusals board.js and cli.js raise today.
*/
import assert from 'node:assert/strict';
-import { mkdtempSync, readdirSync, readFileSync, rmSync } from 'node:fs';
+import { mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
+import { pathToFileURL } from 'node:url';
import { DatabaseSync } from 'node:sqlite';
import { test } from 'node:test';
import * as store from '../src/board.js';
@@ -903,9 +904,12 @@ test('the two conditional guards behave as declared: a renewal passes a held lan
});
/**
- * Every place outside moveItem that writes an item's status: an item_status key in an object, or
- * an SQL SET of it. current() is allowed: it builds the reader's view of a lapsed claim and
- * never writes.
+ * Every place in src/ that could write an item's status outside moveItem, found in the source: an
+ * item_status key, quoted or computed from a literal, or any statement that updates or replaces
+ * item rows, in either case. This backs up the runtime guard, which refuses such a write on every
+ * path a test runs, for the paths no test reaches. Allowed: moveItem; setItem, the generic update
+ * moveItem writes through, which the guard watches; and current(), which builds the reader's view
+ * of a lapsed claim and never writes.
*
* @param {(file: string) => string} read
* @returns {string[]}
@@ -914,9 +918,9 @@ function statusWriters(read) {
const writers = [];
for (const file of readdirSync(new URL('../src/', import.meta.url)).filter((name) => name.endsWith('.js'))) {
const source = read(file);
- const allowed = file === 'board.js' ? ['moveItem', 'current'].map((name) => functionsIn(source).get(name)) : [];
+ const allowed = file === 'board.js' ? ['moveItem', 'setItem', 'current'].map((name) => functionsIn(source).get(name)) : [];
const spans = allowed.filter(Boolean).map((body) => [source.indexOf(body), source.indexOf(body) + body.length]);
- for (const match of source.matchAll(/\bitem_status\s*:|\bSET\b[^;`]*\bitem_status\s*=/g)) {
+ for (const match of source.matchAll(/(?:\[\s*)?(['"`]?)item_status\1(?:\s*\])?\s*:|\b(?:update|replace\s+into|insert\s+or\s+replace\s+into)\s+(?:main\.)?item\b/gi)) {
if (!spans.some(([start, end]) => match.index >= start && match.index < end)) {
const before = source.slice(0, match.index);
const owner = [...functionsIn(source).entries()].find(([, body]) => source.indexOf(body) <= match.index && match.index < source.indexOf(body) + body.length);
@@ -932,4 +936,52 @@ test('only moveItem writes an item\'s status, and a write anywhere else fails th
assert.match(functionsIn(fromDisk('board.js')).get('moveItem'), /item_status: move\.to/, 'moveItem is the writer');
const planted = editedSource('board.js', /set: \(found\) => \(\{ item_route: nextRoute\(found\.item_route\), item_owner: null, item_lease_until: null \}\),/, "set: (found) => ({ item_route: nextRoute(found.item_route), item_status: 'open', item_owner: null, item_lease_until: null }),");
assert.deepEqual(statusWriters(planted), ['board.js#escalate']);
+ const lowercase = editedSource('board.js', /^export function recordAttempt/m, "export function reopenByHand(board, id) {\n board.db.prepare(\"update item set item_status = 'open' where item_id = ?\").run(id);\n}\n\nexport function recordAttempt");
+ assert.deepEqual(statusWriters(lowercase), ['board.js#reopenByHand']);
+ const computed = editedSource('board.js', /^export function recordAttempt/m, "export function reopenByKey(board, id) {\n setItem(board, id, { ['item_status']: 'open' });\n}\n\nexport function recordAttempt");
+ assert.deepEqual(statusWriters(computed), ['board.js#reopenByKey']);
+});
+
+test('the board refuses a status written outside moveItem at run time, however it is spelled [M1]', () => {
+ const lab = boardOnDisk();
+ try {
+ const { board } = lab;
+ const id = store.addItem(board, { by: 'coordinator', lane: 'web', title: 'Page' });
+ store.claim(board, id, { agentId: 'web-1', lane: 'web', leaseMs: 7_200_000, freeze });
+ const column = ['item', 'status'].join('_');
+ for (const sql of ["update item set item_status = 'open' where item_id = ?", `UPDATE item SET ${column} = 'claimed' WHERE item_id = ?`]) {
+ assert.throws(() => board.db.prepare(sql).run(id), /STATUS_OUTSIDE_MOVE/, sql);
+ }
+ assert.throws(
+ () => board.db.prepare("REPLACE INTO item (item_id, item_lane, item_title, item_created_by, item_created_at, item_updated_at) VALUES (?, 'web', 'Page', 'x', 'now', 'now')").run(id),
+ /KEPT/,
+ 'a replace deletes the old row first, and the board keeps items',
+ );
+ assert.equal(store.getItem(board, id).item_status, 'claimed');
+ store.release(board, id, 'web-1');
+ assert.equal(store.getItem(board, id).item_status, 'open', 'a move through moveItem still writes');
+ } finally {
+ lab.done();
+ }
+});
+
+test('a helper added to the board that changes a status outside moveItem is refused when it runs [M1]', async () => {
+ const dir = mkdtempSync(join(tmpdir(), 'pullboard-planted-'));
+ try {
+ for (const file of readdirSync(new URL('../src/', import.meta.url)).filter((name) => name.endsWith('.js'))) {
+ const source = fromDisk(file);
+ writeFileSync(join(dir, file), file === 'board.js' ? `${source}\nexport function reopenByHand(board, id) {\n board.db.prepare("update item set item_status = 'open', item_owner = null, item_lease_until = null where item_id = ?").run(id);\n}\n` : source);
+ }
+ const copy = await import(pathToFileURL(join(dir, 'board.js')).href);
+ const board = copy.openBoard(':memory:');
+ copy.register(board, { lane: 'coordinator', path: '/repo' });
+ copy.register(board, { lane: 'web', path: '/repo-web-1' });
+ const id = copy.addItem(board, { by: 'coordinator', lane: 'web', title: 'Page' });
+ copy.claim(board, id, { agentId: 'web-1', lane: 'web', leaseMs: 7_200_000, freeze });
+ assert.throws(() => copy.reopenByHand(board, id), /STATUS_OUTSIDE_MOVE/);
+ assert.equal(copy.getItem(board, id).item_status, 'claimed');
+ copy.closeBoard(board);
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
});