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(core): import into initialized repos [A7]
Corey Olsoneb31acda58b2
diff --git a/src/exchange.js b/src/exchange.jsindex 3711c74a..00a806e6 100644--- a/src/exchange.js+++ b/src/exchange.js@@ -86,6 +86,37 @@ function validateDocument(db, document, names) {}}+/** True only for the coordinator registration and join event that `pullboard init` creates. */+function hasOnlyInitCoordinator(db, names) {+ if (!names.includes('sqlite_sequence')) return false;+ const agents = db.prepare('SELECT * FROM agent').all();+ const events = db.prepare('SELECT * FROM event').all();+ const sequence = db.prepare('SELECT name, seq FROM sqlite_sequence ORDER BY name').all();+ if (agents.length !== 1 || events.length !== 1 || sequence.length !== 1) return false;+ const [agent] = agents;+ const [event] = events;+ if (+ agent.agent_id !== 'coordinator'+ || agent.agent_lane !== 'coordinator'+ || agent.agent_route !== 'strong'+ || agent.agent_last_shout_id !== 0+ || !agent.agent_path+ || event.event_by !== 'coordinator'+ || event.event_kind !== 'join'+ || event.item_id !== null+ || sequence[0].name !== 'event'+ || sequence[0].seq !== 1+ ) return false;+ try {+ if (JSON.stringify(JSON.parse(event.event_detail)) !== JSON.stringify({ lane: 'coordinator' })) return false;+ } catch {+ return false;+ }+ return names+ .filter((table) => table !== 'agent' && table !== 'event' && table !== 'sqlite_sequence')+ .every((table) => !db.prepare(`SELECT 1 FROM ${identifier(table)} LIMIT 1`).get());+}+/*** Restore a v1 export only into an empty board, atomically, while briefly allowing historical item* states to be inserted before restoring the board's declared lifecycle triggers.@@ -100,11 +131,15 @@ export function importBoard(board, document) {db.exec('PRAGMA defer_foreign_keys = ON; BEGIN IMMEDIATE');try {const occupied = names.filter((table) => db.prepare(`SELECT 1 FROM ${identifier(table)} LIMIT 1`).get());- if (occupied.length) {+ const initCoordinator = occupied.length > 0 && hasOnlyInitCoordinator(db, names);+ if (occupied.length && !initCoordinator) {throw new Refused('IMPORT_NOT_EMPTY', `board tables already have rows (${occupied.join(', ')}); import into a repo with an empty board`);}const triggers = db.prepare("SELECT name FROM sqlite_master WHERE type = 'trigger' AND name LIKE 'machine_%'").all();for (const { name } of triggers) db.exec(`DROP TRIGGER ${identifier(name)}`);+ if (initCoordinator) {+ db.exec('DELETE FROM event; DELETE FROM agent; DELETE FROM sqlite_sequence');+ }for (const table of names.filter((name) => name !== 'sqlite_sequence')) {const columns = columnsOf(db, table);const sql = `INSERT INTO ${identifier(table)} (${columns.map(identifier).join(', ')}) VALUES (${columns.map(() => '?').join(', ')})`;diff --git a/test/exchange.test.js b/test/exchange.test.jsindex f058bb67..9544b388 100644--- a/test/exchange.test.js+++ b/test/exchange.test.js@@ -5,13 +5,13 @@ import assert from 'node:assert/strict';import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';import { spawnSync } from 'node:child_process';import { tmpdir } from 'node:os';-import { join } from 'node:path';+import { join, resolve } from 'node:path';import { test } from 'node:test';import * as store from '../src/board.js';-import { main } from '../src/cli.js';import { exportBoard, importBoard } from '../src/exchange.js';const SHA = 'a'.repeat(40);+const BIN = resolve(import.meta.dirname, '../bin/pullboard.js');/** The deterministic frozen criterion used by the fixture. */const FREEZE = (item) => ({ text: item.item_title, digest: `digest:${item.item_title}` });@@ -149,6 +149,7 @@ test('import refuses a board with rows and leaves it unchanged [A7]', () => {const target = store.openBoard(join(directory, 'target.sqlite'));try {store.register(target, { lane: 'coordinator', path: '/target' });+ store.addItem(target, { by: 'coordinator', lane: 'web', title: 'real work makes this board nonempty' });const before = exportBoard(target);assert.throws(() => importBoard(target, exportBoard(source)), /IMPORT_NOT_EMPTY.*empty board/);assert.deepEqual(exportBoard(target), before);@@ -189,21 +190,44 @@ test('export and import CLI commands exchange a versioned document between realconst source = store.openBoard(join(sourceRoot, '.git', 'pullboard', 'board.sqlite'));store.register(source, { lane: 'coordinator', path: sourceRoot });store.closeBoard(source);- let exported = '';- let errors = '';- const streams = (cwd) => ({ cwd, stdout: { write: (text) => { exported += text; } }, stderr: { write: (text) => { errors += text; } } });+ const env = {+ ...process.env,+ GIT_CONFIG_GLOBAL: '/dev/null',+ GIT_CONFIG_NOSYSTEM: '1',+ GIT_AUTHOR_NAME: 'Exchange Test',+ GIT_AUTHOR_EMAIL: '[email protected]',+ GIT_COMMITTER_NAME: 'Exchange Test',+ GIT_COMMITTER_EMAIL: '[email protected]',+ PULLBOARD_HOME: join(directory, 'home'),+ };+ const cli = (cwd, ...args) => spawnSync(process.execPath, [BIN, ...args], { cwd, env, encoding: 'utf8' });try {- assert.equal(await main(['export'], streams(sourceRoot)), 0, errors);- const document = JSON.parse(exported);+ const initialized = cli(targetRoot, 'init');+ assert.equal(initialized.status, 0, initialized.stderr || initialized.stdout);+ assert.match(initialized.stdout, /coordinator/i);++ const exported = cli(sourceRoot, 'export', '--json');+ assert.equal(exported.status, 0, exported.stderr || exported.stdout);+ const document = JSON.parse(exported.stdout);writeFileSync(join(directory, 'board.json'), JSON.stringify(document));- exported = '';- assert.equal(await main(['import', join(directory, 'board.json')], streams(targetRoot)), 0, errors);- const imported = store.openBoard(join(targetRoot, '.git', 'pullboard', 'board.sqlite'));+ const imported = cli(targetRoot, 'import', join(directory, 'board.json'));+ assert.equal(imported.status, 0, imported.stderr || imported.stdout);+ assert.match(imported.stdout, /imported version 1 board tables/);++ const reexported = cli(targetRoot, 'export', '--json');+ assert.equal(reexported.status, 0, reexported.stderr || reexported.stdout);+ assert.deepEqual(JSON.parse(reexported.stdout), document, 'a board initialized by pullboard can receive the full export');++ const importedBoard = store.openBoard(join(targetRoot, '.git', 'pullboard', 'board.sqlite'));try {- assert.deepEqual(JSON.parse(JSON.stringify(exportBoard(imported))), document);+ assert.deepEqual(JSON.parse(JSON.stringify(exportBoard(importedBoard))), document);} finally {- store.closeBoard(imported);+ store.closeBoard(importedBoard);}+ const status = cli(targetRoot, 'status');+ assert.equal(status.status, 0, status.stderr || status.stdout);+ const listed = cli(targetRoot, 'list', '--all');+ assert.equal(listed.status, 0, listed.stderr || listed.stdout);} finally {rmSync(directory, { recursive: true, force: true });}