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
feat(core): add read-only board doctor [A6]
Corey Olsonebc679de4f88
diff --git a/src/cli.js b/src/cli.jsindex 7efc7b16..0f1cc497 100644--- a/src/cli.js+++ b/src/cli.js@@ -46,6 +46,7 @@ import { commandOutput } from './json.js';import { forgetProject, registerProject } from './projects.js';import { citedTestFiles, rowEvidence, rowStage } from './evidence.js';import { serveView } from './serve.js';+import { doctorProblems } from './doctor.js';const PACKAGE = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));export const VERSION = PACKAGE.version;@@ -80,6 +81,7 @@ Work$PULLBOARD_ATTEMPT, $PULLBOARD_TIER); green work is submitted, red escalated[--agent-light "..."] [--agent-mid "..."] [--agent-strong "..."] a command per tierpullboard list [lane] [--all] [--route light|mid|strong] open and active items; --all adds closed ones+ pullboard doctor check board integrity without changing itpullboard show <id> [--history] an item, the criterion frozen at claim, its verdicts: the latest in full,earlier ones as one line; --history prints every note in fullpullboard next [--wait <minutes>] claim the next item in your lane that is free to start@@ -884,6 +886,17 @@ function readCommands(io, { first, values }) {}return 0;},+ doctor: () => {+ const ctx = context(io);+ const problems = doctorProblems(ctx.file, ctx.info.root, tryGit);+ io.result?.({ problems });+ if (!problems.length) {+ io.say('board is clean');+ return 0;+ }+ for (const problem of problems) io.say(`problem: ${problem.message}; repair: ${problem.next}`);+ return 1;+ },inbox: () => {const ctx = context(io);const shouts = withBoard(ctx, (board) => store.inbox(board, whoAmI(ctx, board).id));diff --git a/src/doctor.js b/src/doctor.jsnew file mode 100644index 00000000..4b578afb--- /dev/null+++ b/src/doctor.js@@ -0,0 +1,131 @@+/** Read-only integrity checks for a pullboard board file (A6). */+import { existsSync } from 'node:fs';+import { DatabaseSync } from 'node:sqlite';+import { BLANKS, STATES, storeTriggers } from './machine.js';++const SCHEMA_VERSION = 1;+const blankCharacters = new Set(BLANKS.map((point) => String.fromCodePoint(point)));++/**+ * Check a board and its git pins without opening it through the migrator, which repairs boards on+ * open. `doctor` must report the evidence it sees and leave that evidence untouched.+ *+ * @param {string} file+ * @param {string} root+ * @param {(root: string, args: string[]) => { status: number, stdout: string }} tryGit+ * @returns {{ code: string, message: string, next: string }[]}+ */+export function doctorProblems(file, root, tryGit) {+ if (!existsSync(file)) return [finding('BOARD_MISSING', 'board file is missing', 'run pullboard status to create a new board')];+ const db = new DatabaseSync(file, { readOnly: true });+ try {+ return [+ ...versionProblems(db),+ ...triggerProblems(db),+ ...itemProblems(db),+ ...pinProblems(db, root, tryGit),+ ...verdictProblems(db, root, tryGit),+ ];+ } finally {+ db.close();+ }+}++/**+ * Make one stable finding for both text and JSON output.+ *+ * @param {string} code+ * @param {string} message+ * @param {string} next+ * @returns {{ code: string, message: string, next: string }}+ */+const finding = (code, message, next) => ({ code, message, next });++/**+ * Report a schema version this executable cannot safely use.+ *+ * @param {DatabaseSync} db+ * @returns {{ code: string, message: string, next: string }[]}+ */+function versionProblems(db) {+ const version = db.prepare('PRAGMA user_version').get().user_version;+ if (version === SCHEMA_VERSION) return [];+ const repair = version < SCHEMA_VERSION+ ? 'run pullboard status to upgrade this board'+ : 'use a pullboard version that supports this board schema';+ return [finding('SCHEMA_VERSION', `schema version is ${version}; this pullboard expects ${SCHEMA_VERSION}`, repair)];+}++/**+ * Report missing, changed or undeclared lifecycle triggers and how the normal opener repairs them.+ *+ * @param {DatabaseSync} db+ * @returns {{ code: string, message: string, next: string }[]}+ */+function triggerProblems(db) {+ const expected = storeTriggers();+ const actual = new Map(db.prepare("SELECT name, sql FROM sqlite_master WHERE type = 'trigger' AND name LIKE 'machine%'").all().map((row) => [row.name, row.sql]));+ const problems = [];+ for (const trigger of expected) {+ const sql = actual.get(trigger.name);+ if (sql === undefined) problems.push(finding('TRIGGER_MISSING', `board trigger ${trigger.name} is missing`, 'run pullboard status'));+ else if (sql !== trigger.sql) problems.push(finding('TRIGGER_CHANGED', `board trigger ${trigger.name} has changed`, 'run pullboard status'));+ }+ for (const name of actual.keys()) {+ if (!expected.some((trigger) => trigger.name === name)) problems.push(finding('TRIGGER_UNEXPECTED', `board trigger ${name} is unexpected`, 'run pullboard status'));+ }+ return problems;+}++/**+ * Report item rows missing fields required by their current state.+ *+ * @param {DatabaseSync} db+ * @returns {{ code: string, message: string, next: string }[]}+ */+function itemProblems(db) {+ const required = new Map(STATES.map((state) => [state.id, state.requires]));+ const problems = [];+ for (const item of db.prepare('SELECT * FROM item ORDER BY item_id').all()) {+ const fields = required.get(item.item_status) ?? [];+ const missing = fields.filter((field) => [...String(item[field] ?? '')].every((character) => blankCharacters.has(character)));+ if (missing.length) problems.push(finding('ITEM_FIELDS', `item #${item.item_id} (${item.item_status}) is missing ${missing.join(', ')}`, 'restore a consistent board backup, then run pullboard doctor'));+ }+ return problems;+}++/**+ * Report submitted or verified items whose saved commit pin no longer resolves.+ *+ * @param {DatabaseSync} db+ * @param {string} root+ * @param {(root: string, args: string[]) => { status: number, stdout: string }} tryGit+ * @returns {{ code: string, message: string, next: string }[]}+ */+function pinProblems(db, root, tryGit) {+ const items = db.prepare("SELECT item_id, item_commit FROM item WHERE item_status IN ('submitted', 'verified') ORDER BY item_id").all();+ return items.flatMap((item) => {+ const commit = item.item_commit ?? '';+ const pin = `refs/pullboard/items/${item.item_id}/${commit.slice(0, 12)}`;+ const found = commit && tryGit(root, ['rev-parse', '--verify', '--quiet', `${pin}^{commit}`]);+ if (found?.status === 0 && found.stdout === commit) return [];+ return [finding('ITEM_PIN_MISSING', `item #${item.item_id} has no pin for ${commit || 'its missing commit'} at ${pin}`, `fetch the commit if needed, then run git update-ref ${pin} ${commit || '<commit>'}`)];+ });+}++/**+ * Report verdict receipts whose commit object is no longer in the repository.+ *+ * @param {DatabaseSync} db+ * @param {string} root+ * @param {(root: string, args: string[]) => { status: number, stdout: string }} tryGit+ * @returns {string[]}+ */+function verdictProblems(db, root, tryGit) {+ return db.prepare('SELECT verdict_id, item_id, verdict_commit FROM verdict ORDER BY verdict_id').all().flatMap((verdict) => {+ const found = tryGit(root, ['rev-parse', '--verify', '--quiet', `${verdict.verdict_commit}^{commit}`]);+ if (found.status === 0 && found.stdout === verdict.verdict_commit) return [];+ const pin = `refs/pullboard/items/${verdict.item_id}/${verdict.verdict_commit.slice(0, 12)}`;+ return [finding('VERDICT_COMMIT_MISSING', `verdict #${verdict.verdict_id} for item #${verdict.item_id} names missing commit ${verdict.verdict_commit}`, `fetch the commit if needed, then run git update-ref ${pin} ${verdict.verdict_commit}`)];+ });+}diff --git a/test/doctor.test.js b/test/doctor.test.jsnew file mode 100644index 00000000..56df2c6a--- /dev/null+++ b/test/doctor.test.js@@ -0,0 +1,157 @@+/** Integrity checks use throwaway git repositories and real board databases (A6). */+import assert from 'node:assert/strict';+import { execFileSync, spawnSync } from 'node:child_process';+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join, resolve } from 'node:path';+import { after, test } from 'node:test';+import { DatabaseSync } from 'node:sqlite';++const BIN = resolve(import.meta.dirname, '../bin/pullboard.js');+const sandboxes = [];++after(() => {+ for (const dir of sandboxes) rmSync(dir, { recursive: true, force: true });+});++/**+ * Create a committed repo with a board and isolated git identity for one integrity scenario.+ */+function boardBox() {+ const dir = mkdtempSync(join(tmpdir(), 'pullboard-doctor-'));+ sandboxes.push(dir);+ const root = join(dir, 'repo');+ mkdirSync(root);+ const env = {+ ...process.env,+ GIT_CONFIG_GLOBAL: '/dev/null',+ GIT_CONFIG_NOSYSTEM: '1',+ GIT_AUTHOR_NAME: 'Doctor Test',+ GIT_AUTHOR_EMAIL: '[email protected]',+ GIT_COMMITTER_NAME: 'Doctor Test',+ GIT_COMMITTER_EMAIL: '[email protected]',+ };+ const git = (...args) => execFileSync('git', args, { cwd: root, env, encoding: 'utf8' }).trim();+ const run = (...args) => spawnSync(process.execPath, [BIN, ...args], { cwd: root, env, encoding: 'utf8' });+ git('init', '-q', '-b', 'main');+ writeFileSync(join(root, 'pullboard.json'), JSON.stringify({ gate: 'true', lanes: {} }, null, 2));+ git('add', '-A');+ git('commit', '-q', '-m', 'chore: setup');+ assert.equal(run('status').status, 0);+ return { dir, root, env, git, run, dbFile: join(root, '.git', 'pullboard', 'board.sqlite') };+}++/**+ * Open the test board directly so a fixture can model corruption that normal triggers prevent.+ */+function mutate(box, action) {+ const db = new DatabaseSync(box.dbFile);+ try {+ action(db);+ } finally {+ db.close();+ }+}++/**+ * Add a valid open row, then return its integer id for a corruption test.+ */+function addItem(box) {+ let id;+ mutate(box, (db) => {+ db.prepare('INSERT INTO item (item_lane, item_title, item_created_by, item_created_at, item_updated_at) VALUES (?, ?, ?, ?, ?)')+ .run('web', 'Doctor fixture', 'coordinator', '2026-10-07T00:00:00.000Z', '2026-10-07T00:00:00.000Z');+ id = Number(db.prepare('SELECT last_insert_rowid() AS id').get().id);+ });+ return id;+}++test('doctor reports a clean board in one line and leaves it unchanged [A6]', () => {+ const box = boardBox();+ const before = readFileSync(box.dbFile);+ const result = box.run('doctor');+ assert.equal(result.status, 0);+ assert.equal(result.stdout, 'board is clean\n');+ assert.equal(result.stderr, '');+ assert.deepEqual(readFileSync(box.dbFile), before);+});++test('doctor finds a missing lifecycle trigger without reinstalling it [A6]', () => {+ const box = boardBox();+ mutate(box, (db) => db.exec('DROP TRIGGER machine_item_start'));+ const result = box.run('doctor');+ assert.equal(result.status, 1);+ assert.match(result.stdout, /machine_item_start is missing; repair: run pullboard status/);+ const db = new DatabaseSync(box.dbFile, { readOnly: true });+ try {+ assert.equal(db.prepare("SELECT count(*) AS n FROM sqlite_master WHERE type='trigger' AND name='machine_item_start'").get().n, 0);+ } finally {+ db.close();+ }+});++test('doctor reports every required field missing from an item state [A6]', () => {+ const box = boardBox();+ const id = addItem(box);+ mutate(box, (db) => {+ db.exec('DROP TRIGGER machine_item_move; DROP TRIGGER machine_fields_claimed');+ db.prepare("UPDATE item SET item_status='claimed', item_owner='', item_lease_until='', item_frozen_digest='' WHERE item_id=?").run(id);+ });+ const result = box.run('doctor');+ assert.equal(result.status, 1);+ assert.match(result.stdout, new RegExp(`item #${id} \\(claimed\\) is missing item_owner, item_lease_until, item_frozen_digest`));+ assert.match(result.stdout, /restore a consistent board backup, then run pullboard doctor/);+});++test('doctor reports a submitted item whose git pin is gone [A6]', () => {+ const box = boardBox();+ const id = addItem(box);+ const verifiedId = addItem(box);+ writeFileSync(join(box.root, 'proof.txt'), 'commit for the submitted item\n');+ box.git('add', '-A');+ box.git('commit', '-q', '-m', 'docs: make a fixture commit');+ const commit = box.git('rev-parse', 'HEAD');+ mutate(box, (db) => {+ db.exec('DROP TRIGGER machine_item_move; DROP TRIGGER machine_fields_submitted; DROP TRIGGER machine_fields_verified; DROP TRIGGER machine_proof_verified');+ db.prepare("UPDATE item SET item_status='submitted', item_built_by='web-1', item_commit=?, item_frozen_digest='fixture-digest' WHERE item_id=?").run(commit, id);+ db.prepare("UPDATE item SET item_status='verified', item_verified_by='review-1', item_commit=? WHERE item_id=?").run(commit, verifiedId);+ });+ const result = box.run('doctor');+ assert.equal(result.status, 1);+ assert.match(result.stdout, new RegExp(`item #${id} has no pin for ${commit}`));+ assert.match(result.stdout, new RegExp(`git update-ref refs/pullboard/items/${id}/${commit.slice(0, 12)} ${commit}`));+ assert.match(result.stdout, new RegExp(`item #${verifiedId} has no pin for ${commit}`));+});++test('doctor reports verdicts whose commit object is gone [A6]', () => {+ const box = boardBox();+ const id = addItem(box);+ const missing = 'f'.repeat(40);+ mutate(box, (db) => {+ db.prepare(`INSERT INTO verdict (item_id, verdict_by, verdict_decision, verdict_reason, verdict_note,+ verdict_commit, verdict_digest, verdict_head, verdict_at) VALUES (?, 'review-1', 'REJECT', 'OTHER', 'fixture', ?, 'digest', 'head', '2026-10-07T00:00:00.000Z')`)+ .run(id, missing);+ });+ const result = box.run('doctor');+ assert.equal(result.status, 1);+ assert.match(result.stdout, new RegExp(`verdict #1 for item #${id} names missing commit ${missing}`));+ assert.match(result.stdout, new RegExp(`git update-ref refs/pullboard/items/${id}/${missing.slice(0, 12)} ${missing}`));+});++test('doctor reports older and newer schema versions without migrating either [A6]', () => {+ const box = boardBox();+ mutate(box, (db) => db.exec('PRAGMA user_version = 0'));+ const older = box.run('doctor');+ assert.equal(older.status, 1);+ assert.match(older.stdout, /schema version is 0; this pullboard expects 1; repair: run pullboard status to upgrade this board/);+ mutate(box, (db) => db.exec('PRAGMA user_version = 2'));+ const newer = box.run('doctor');+ assert.equal(newer.status, 1);+ assert.match(newer.stdout, /schema version is 2; this pullboard expects 1; repair: use a pullboard version that supports this board schema/);+ const db = new DatabaseSync(box.dbFile, { readOnly: true });+ try {+ assert.equal(db.prepare('PRAGMA user_version').get().user_version, 2);+ } finally {+ db.close();+ }+});