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(core): derive schema trust from source [A5,A6]

Corey Olson7c77351abf59
diff --git a/src/api.js b/src/api.js
index 4a07a5fb..ec07e4a0 100644
--- a/src/api.js
+++ b/src/api.js
@@ -1,7 +1,8 @@
/** The local API v1: the CLI's moves, persistent board identities and live events (A2). */
import { randomBytes, timingSafeEqual } from 'node:crypto';
import { createServer } from 'node:http';
-import { join } from 'node:path';
+import { dirname, join } from 'node:path';
+import { fileURLToPath } from 'node:url';
import * as store from './board.js';
import { COORDINATOR, loadConfig } from './config.js';
import { repoInfo } from './git.js';
@@ -14,6 +15,7 @@ import { createApiHandler } from './api-http.js';
const ADDRESS = '127.0.0.1';
const VERSION = 1;
+const CODE_ROOT = dirname(dirname(fileURLToPath(import.meta.url)));
/** Read a registered board and release its SQLite connection on every path.
*
@@ -24,7 +26,7 @@ const VERSION = 1;
function withBoard(root, read, isMain) {
const info = repoInfo(root);
loadConfig(info.root);
- const board = store.openBoard(join(info.commonDir, 'pullboard', 'board.sqlite'), undefined, { isMain: isMain ?? info.isMain });
+ const board = store.openBoard(join(info.commonDir, 'pullboard', 'board.sqlite'), undefined, { isMain: repoInfo(CODE_ROOT).isMain && isMain !== false });
try { return read(board, info); } finally { store.closeBoard(board); }
}
@@ -146,7 +148,6 @@ async function executeMove(root, body, runCommand, isMain) {
const events = [];
const status = await runCommand(argv, {
cwd,
- codeIsMain: isMain,
stdout: { isTTY: false, write: (text) => { output += text; } },
stderr: { write: (text) => { diagnostics += text; } },
onEvent: (row) => { events.push(row); },
@@ -174,6 +175,7 @@ function createRequest(root, body, isMain) {
export function createLocalApiHandler({ secret, getPort, runCommand, projects = listApiProjects, isMain } = {}) {
if (typeof getPort !== 'function') throw new TypeError('createLocalApiHandler needs a bound-port getter');
if (typeof runCommand !== 'function') throw new TypeError('createLocalApiHandler needs the actual CLI entry point as runCommand');
+ const codeIsMain = repoInfo(CODE_ROOT).isMain && isMain !== false;
const key = Buffer.from(secret);
return createApiHandler({
authenticate: (req) => {
@@ -186,18 +188,18 @@ export function createLocalApiHandler({ secret, getPort, runCommand, projects =
if (req.headers.origin && req.headers.origin !== 'http://' + req.headers.host) throw new Refused('API_ORIGIN', 'use the API from its own origin');
return { local: true };
},
- boards: () => apiBoardListing(projects, isMain),
- board: (id) => findBoard(id, projects, isMain),
+ boards: () => apiBoardListing(projects, codeIsMain),
+ board: (id) => findBoard(id, projects, codeIsMain),
state: (board, who, seen) => {
- const state = projectState(board.root, { seen, isMain });
+ const state = projectState(board.root, { seen, isMain: codeIsMain });
state.board = board.id;
- state.requests = withBoard(board.root, (db) => store.openRequests(db), isMain);
+ state.requests = withBoard(board.root, (db) => store.openRequests(db), codeIsMain);
return state;
},
code: (board, ref) => codeAt(board.root, ref),
- events: (board, after) => afterEvents(board.root, after, isMain),
- move: (board, body) => executeMove(board.root, body, runCommand, isMain),
- request: (board, body) => createRequest(board.root, body, isMain),
+ events: (board, after) => afterEvents(board.root, after, codeIsMain),
+ move: (board, body) => executeMove(board.root, body, runCommand, codeIsMain),
+ request: (board, body) => createRequest(board.root, body, codeIsMain),
});
}
diff --git a/src/cli.js b/src/cli.js
index aeef32e3..a56e40ac 100644
--- a/src/cli.js
+++ b/src/cli.js
@@ -5,7 +5,7 @@
import { spawn, spawnSync } from 'node:child_process';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { basename, dirname, isAbsolute, join, relative, resolve } from 'node:path';
-import { pathToFileURL } from 'node:url';
+import { fileURLToPath, pathToFileURL } from 'node:url';
import { parseArgs } from 'node:util';
import * as store from './board.js';
import { CONFIG_FILE, COORDINATOR, loadConfig } from './config.js';
@@ -57,6 +57,7 @@ import { relayOff, relayOn, relayStatus, syncRelay } from './relay.js';
const PACKAGE = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
export const VERSION = PACKAGE.version;
+const CODE_ROOT = dirname(dirname(fileURLToPath(import.meta.url)));
export const HELP = `pullboard ${VERSION}: the local-first work board for teams of coding agents.
Nothing ships until a second agent verifies it.
@@ -348,9 +349,8 @@ function sayBrief(io, brief) {
*/
function context(io) {
const info = repoInfo(io.cwd);
- const allowSchemaUpgrade = io.codeIsMain ?? (process.env.PULLBOARD_CODE_IS_MAIN === undefined
- ? info.isMain
- : process.env.PULLBOARD_CODE_IS_MAIN === '1');
+ const code = repoInfo(CODE_ROOT);
+ const allowSchemaUpgrade = code.isMain;
const config = configHere(info);
const file = join(info.commonDir, 'pullboard', 'board.sqlite');
return { info, config, file, allowSchemaUpgrade, doctrine: loadDoctrine(info.root, config), io, clock: io.clock ?? store.systemClock };
@@ -732,7 +732,7 @@ async function serveHere(io, values) {
registerProject(mainRoot, new Date(), loadConfig(mainRoot));
const port = values.port === undefined ? 0 : Number(values.port);
if (!Number.isInteger(port) || port < 0 || port > 65535) throw new Refused('USAGE', '--port is a number from 0 to 65535; use 0 to choose a free one');
- const api = await serveApi({ port, runCommand: main, isMain: ctx.info.isMain });
+ const api = await serveApi({ port, runCommand: main, isMain: ctx.allowSchemaUpgrade });
io.result?.({ url: api.url, port: api.port });
io.say(`Pullboard API v1: ${api.url}`);
io.say('Only this machine can reach it, with its session secret. Ctrl-C stops it.');
@@ -1849,8 +1849,6 @@ export async function main(argv, streams) {
}
};
if (sync) await retry();
- if (streams?.codeIsMain !== undefined) io.codeIsMain = streams.codeIsMain;
- else if (process.env.PULLBOARD_CODE_IS_MAIN !== undefined) io.codeIsMain = process.env.PULLBOARD_CODE_IS_MAIN === '1';
const code = await runCommand(argv, io);
if (sync && code === 0) await retry();
io.flush(code);
diff --git a/test/api.test.js b/test/api.test.js
index c1c15b4e..3b212424 100644
--- a/test/api.test.js
+++ b/test/api.test.js
@@ -200,6 +200,19 @@ test('[A5, A6] a future-schema worktree cannot migrate the shared board; main ca
assert.equal(box.git(repo, 'rev-parse', '--path-format=absolute', '--git-common-dir'), box.git(linked, 'rev-parse', '--path-format=absolute', '--git-common-dir'));
assert.notEqual(box.git(repo, 'rev-parse', '--path-format=absolute', '--git-dir'), box.git(linked, 'rev-parse', '--path-format=absolute', '--git-dir'));
+ const olderAgain = new DatabaseSync(dbFile);
+ olderAgain.exec('ALTER TABLE item DROP COLUMN item_files; PRAGMA user_version = 1');
+ olderAgain.close();
+ const mainCodeFromWorktree = spawnSync(process.execPath, [join(repo, 'bin/pullboard.js'), 'join', 'app', '--family', 'codex', '--json'], {
+ cwd: linked, env: box.env, encoding: 'utf8',
+ });
+ assert.equal(mainCodeFromWorktree.status, 0, mainCodeFromWorktree.stderr + mainCodeFromWorktree.stdout);
+ const migratedFromWorktree = new DatabaseSync(dbFile, { readOnly: true });
+ assert.equal(migratedFromWorktree.prepare('PRAGMA user_version').get().user_version, 2,
+ 'the main checkout code may upgrade its shared board even when cwd is a linked worktree');
+ assert.ok(migratedFromWorktree.prepare('PRAGMA table_info(item)').all().some((column) => column.name === 'item_files'));
+ migratedFromWorktree.close();
+
const boardSource = join(linked, 'src/board.js');
const oldSource = readFileSync(boardSource, 'utf8');
const migration = " ['shout', 'shout_request_outcome', \"TEXT NOT NULL DEFAULT ''\"],\n ];";
@@ -214,6 +227,22 @@ test('[A5, A6] a future-schema worktree cannot migrate the shared board; main ca
box.git(linked, 'commit', '-q', '-m', 'feat: simulate an unmerged schema bump');
const before = readFileSync(dbFile);
+ const worktreeCodeFromMain = spawnSync(process.execPath, [join(linked, 'bin/pullboard.js'), 'status', '--json'], {
+ cwd: repo, env: box.env, encoding: 'utf8',
+ });
+ assert.equal(worktreeCodeFromMain.status, 1, worktreeCodeFromMain.stderr + worktreeCodeFromMain.stdout);
+ assert.equal(JSON.parse(worktreeCodeFromMain.stdout).error.code, 'UNMERGED_SCHEMA',
+ 'a future-schema worktree refuses migration even when invoked from the main checkout directory');
+ assert.deepEqual(readFileSync(dbFile), before, 'cwd cannot make worktree code migrate the shared board');
+
+ const envBypass = spawnSync(process.execPath, [join(linked, 'bin/pullboard.js'), 'status', '--json'], {
+ cwd: repo, env: { ...box.env, PULLBOARD_CODE_IS_MAIN: '1' }, encoding: 'utf8',
+ });
+ assert.equal(envBypass.status, 1, envBypass.stderr + envBypass.stdout);
+ assert.equal(JSON.parse(envBypass.stdout).error.code, 'UNMERGED_SCHEMA',
+ 'PULLBOARD_CODE_IS_MAIN cannot impersonate main-checkout source provenance');
+ assert.deepEqual(readFileSync(dbFile), before, 'the removed environment override cannot migrate the shared board');
+
const specCheck = runFixture(linked, 'spec', 'check', '--json');
assert.equal(specCheck.status, 0, specCheck.stderr + specCheck.stdout);
assert.deepEqual(readFileSync(dbFile), before, 'spec check reads the spec without opening the shared board');
@@ -223,7 +252,7 @@ test('[A5, A6] a future-schema worktree cannot migrate the shared board; main ca
import { serveApi } from './src/api.js';
const root = process.env.PULLBOARD_FIXTURE_REPO;
const project = { root, name: 'schema fixture', project: '', added: '2026-01-01T00:00:00.000Z' };
- const api = await serveApi({ secret: 'api-probe', runCommand: async () => 0, projects: () => [project], isMain: false });
+ const api = await serveApi({ secret: 'api-probe', runCommand: async () => 0, projects: () => [project], isMain: true });
try {
const listing = await fetch(api.url).then((response) => response.json());
console.log(JSON.stringify({