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(board): fields and proof hold on every write, not just entry [M3]
Corey Olsonda093502acf3
diff --git a/src/machine.js b/src/machine.jsindex b3657319..69e50a72 100644--- a/src/machine.js+++ b/src/machine.js@@ -81,8 +81,8 @@ export const STATES = [{id: 'verified', final: true, means: 'another agent accepted it at the submitted commit', requires: ['item_verified_by', 'item_commit'],proof: {- says: 'an ACCEPT at the submitted commit, on the frozen criterion, by an agent that did not build it',- sql: "EXISTS (SELECT 1 FROM verdict v WHERE v.item_id = NEW.item_id AND v.verdict_decision = 'ACCEPT' AND v.verdict_commit = NEW.item_commit AND v.verdict_digest = NEW.item_frozen_digest AND v.verdict_by <> NEW.item_built_by)",+ says: 'an ACCEPT at the submitted commit, on the frozen criterion, by the agent it names as verifier, who did not build it',+ sql: "EXISTS (SELECT 1 FROM verdict v WHERE v.item_id = NEW.item_id AND v.verdict_decision = 'ACCEPT' AND v.verdict_commit = NEW.item_commit AND v.verdict_digest = NEW.item_frozen_digest AND v.verdict_by = NEW.item_verified_by AND v.verdict_by <> NEW.item_built_by)",},},{ id: 'withdrawn', final: true, means: 'nobody should build it; the reason stays on it', requires: ['item_withdrawn_reason'] },@@ -378,12 +378,19 @@ export function machineProblems(machine = MACHINE) {*/const sqlText = (text) => `'${text.replaceAll("'", "''")}'`;+/**+ * The characters JavaScript's trim removes, as code points. A field made only of them is blank in+ * the board file, exactly as it is to the commands that trim what an agent typed.+ */+export const BLANKS = [9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279];+/*** The SQLite triggers that make the board file itself refuse what the declaration does not allow- * (M3): an item that starts anywhere but the start, a state or a move nobody declared, a state- * entered without its fields, a final state entered without its proof, and an edit to a record the- * board keeps for good. All of it comes from the declaration, so the board reinstalls any trigger- * that differs when the declaration changes.+ * (M3): an item that starts anywhere but the start, a state or a move nobody declared, an item left+ * in a state without that state's fields or proof, and an edit to a record the board keeps for good.+ * A state's fields and proof are checked on every write that leaves an item in it, a move in or an+ * edit while it stays, so no field that makes the proof can be rewritten after it. All of it comes+ * from the declaration, so the board reinstalls any trigger that differs when the declaration changes.** The triggers stop a status written by hand. They cannot stop an agent that forges a verdict row* under some name and then moves the item; that forgery stays in the ledger under the name it used.@@ -397,11 +404,13 @@ export function storeTriggers(machine = MACHINE) {const states = machine.states.map((state) => state.id);const moves = [...new Set(machine.moves.flatMap((move) => move.from.filter((from) => from !== move.to).map((from) => `${from}>${move.to}`)))];const declared = `OLD.item_status || '>' || NEW.item_status IN (${moves.map(sqlText).join(', ')})`;- const entering = (state) => `NEW.item_status = ${sqlText(state)} AND OLD.item_status <> NEW.item_status AND ${declared}`;+ const holds = (state) => `NEW.item_status = ${sqlText(state)} AND (OLD.item_status = NEW.item_status OR ${declared})`;+ const onWrite = 'BEFORE UPDATE ON item';const trigger = (name, on, body) => ({ name, sql: `CREATE TRIGGER ${name} ${on}\n${body}` });// Each write meets at most one trigger: an unknown state, else an undeclared move, else missing// fields, else a missing proof. SQLite fires triggers in no promised order, so their conditions- // exclude each other and the refusal never depends on which one ran first.+ // exclude each other and the refusal never depends on which one ran first. A blank field is one+ // that BLANKS trims to nothing.const triggers = [trigger('machine_item_start', 'BEFORE INSERT ON item', `WHEN NEW.item_status <> ${sqlText(machine.initial)}\n${refuse('UNDECLARED_MOVE', `an item starts ${machine.initial}`)}`),trigger('machine_item_state', onStatus, `WHEN NEW.item_status NOT IN (${states.map(sqlText).join(', ')})\n${refuse('UNDECLARED_STATE', `an item is ${states.join(', ')}`)}`),@@ -411,13 +420,14 @@ export function storeTriggers(machine = MACHINE) {`WHEN NEW.item_status IN (${states.map(sqlText).join(', ')}) AND OLD.item_status <> NEW.item_status AND NOT ${declared}\n${refuse('UNDECLARED_MOVE', `the moves are ${moves.join(', ')}`)}`,),];- const missingOf = (state) => state.requires.map((field) => `coalesce(NEW.${field}, '') = ''`).join(' OR ');+ const blank = `char(${BLANKS.join(', ')})`;+ const missingOf = (state) => state.requires.map((field) => `trim(coalesce(NEW.${field}, ''), ${blank}) = ''`).join(' OR ');for (const state of machine.states.filter((entry) => entry.requires.length)) {- triggers.push(trigger(`machine_fields_${state.id}`, onStatus, `WHEN ${entering(state.id)} AND (${missingOf(state)})\n${refuse('MISSING_FIELD', `${state.id} needs ${state.requires.join(', ')}`)}`));+ triggers.push(trigger(`machine_fields_${state.id}`, onWrite, `WHEN ${holds(state.id)} AND (${missingOf(state)})\n${refuse('MISSING_FIELD', `${state.id} needs ${state.requires.join(', ')}`)}`));}for (const state of machine.states.filter((entry) => entry.proof)) {const complete = state.requires.length ? ` AND NOT (${missingOf(state)})` : '';- triggers.push(trigger(`machine_proof_${state.id}`, onStatus, `WHEN ${entering(state.id)}${complete} AND NOT ${state.proof.sql}\n${refuse('NOT_PROVEN', `${state.id} needs ${state.proof.says}`)}`));+ triggers.push(trigger(`machine_proof_${state.id}`, onWrite, `WHEN ${holds(state.id)}${complete} AND NOT ${state.proof.sql}\n${refuse('NOT_PROVEN', `${state.id} needs ${state.proof.says}`)}`));}for (const [table, kept] of Object.entries(machine.records)) {for (const change of kept === 'append' ? ['update', 'delete'] : ['delete']) {diff --git a/test/machine.test.js b/test/machine.test.jsindex d9bbea07..c9da762a 100644--- a/test/machine.test.js+++ b/test/machine.test.js@@ -9,7 +9,7 @@ import { join } from 'node:path';import { DatabaseSync } from 'node:sqlite';import { test } from 'node:test';import * as store from '../src/board.js';-import { IN_STATE, MACHINE, effectiveGuards, machineProblems, storeTriggers } from '../src/machine.js';+import { BLANKS, IN_STATE, MACHINE, effectiveGuards, machineProblems, storeTriggers } from '../src/machine.js';/*** Refusals that are not about an item's lifecycle, so no move declares them: how a command was@@ -639,3 +639,56 @@ test('the triggers come from the declaration, so a changed declaration changes tfor (const state of MACHINE.states.filter((entry) => entry.requires.length)) assert.ok(names.includes(`machine_fields_${state.id}`), `${state.id} has a fields trigger`);assert.ok(names.includes('machine_proof_verified'));});++test("a verified item's proof cannot be rewritten afterwards: its commit, builder, verifier or criterion [M3]", () => {+ const lab = boardOnDisk();+ try {+ const id = submittedItem(lab.board);+ verdictOn(lab.board, id, 'web-2', 'ACCEPT');+ const raw = lab.raw();+ const refused = (sql) => assert.throws(() => raw.prepare(sql).run(id), /NOT_PROVEN/, sql);+ refused(`UPDATE item SET item_commit = '${SHA_B}' WHERE item_id = ?`);+ refused("UPDATE item SET item_built_by = 'web-2' WHERE item_id = ?");+ refused("UPDATE item SET item_verified_by = 'web-1' WHERE item_id = ?");+ refused("UPDATE item SET item_frozen_digest = 'digest:Another criterion' WHERE item_id = ?");+ refused(`UPDATE item SET item_status = 'verified', item_commit = '${SHA_B}' WHERE item_id = ?`);+ raw.prepare(`UPDATE item SET item_merged_commit = '${SHA_A}' WHERE item_id = ?`).run(id);+ assert.deepEqual([store.getItem(lab.board, id).item_commit, store.getItem(lab.board, id).item_merged_commit], [SHA_A, SHA_A]);+ } finally {+ lab.done();+ }+});++test('a field a state needs cannot be blanked, on the way in or while the item stays, with any whitespace [M3]', () => {+ const lab = boardOnDisk();+ try {+ const { board } = lab;+ const withdrawn = store.addItem(board, { by: 'coordinator', lane: 'web', title: 'Old idea' });+ store.withdraw(board, withdrawn, { agentId: 'coordinator', reason: 'superseded' });+ const fresh = store.addItem(board, { by: 'coordinator', lane: 'web', title: 'Another' });+ const claimed = store.addItem(board, { by: 'coordinator', lane: 'web', title: 'Page' });+ store.claim(board, claimed, { agentId: 'web-2', lane: 'web', leaseMs: 7_200_000, freeze });+ const built = submittedItem(board);+ const raw = lab.raw();+ const refused = (sql, args) => assert.throws(() => raw.prepare(sql).run(...args), /MISSING_FIELD/, `${sql} ${JSON.stringify(args)}`);+ refused("UPDATE item SET item_withdrawn_reason = '' WHERE item_id = ?", [withdrawn]);+ refused('UPDATE item SET item_withdrawn_reason = NULL WHERE item_id = ?', [withdrawn]);+ refused("UPDATE item SET item_status = 'withdrawn', item_withdrawn_reason = '' WHERE item_id = ?", [withdrawn]);+ for (const blank of [' ', '\t\n', ' ', ' ', '', ' ']) {+ refused("UPDATE item SET item_status = 'withdrawn', item_withdrawn_reason = ? WHERE item_id = ?", [blank, fresh]);+ }+ refused('UPDATE item SET item_owner = NULL WHERE item_id = ?', [claimed]);+ refused("UPDATE item SET item_commit = '' WHERE item_id = ?", [built]);+ assert.equal(store.getItem(board, withdrawn).item_withdrawn_reason, 'superseded');+ assert.equal(store.getItem(board, fresh).item_status, 'open');+ assert.throws(() => store.withdraw(board, fresh, { agentId: 'coordinator', reason: ' ' }), /NOTE_REQUIRED/, 'the command trims the same way');+ } finally {+ lab.done();+ }+});++test("a blank field in the board file is exactly what JavaScript's trim removes [M3]", () => {+ const trimmed = [];+ for (let code = 0; code <= 0xffff; code += 1) if (String.fromCharCode(code).trim() === '') trimmed.push(code);+ assert.deepEqual(BLANKS, trimmed);+});