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
refactor(board): extract shared schema ownership checks [A5,A6,C7]
Corey Olson8f4464aebf50
diff --git a/src/board-access.js b/src/board-access.jsindex e0381edc..a486fde8 100644--- a/src/board-access.js+++ b/src/board-access.js@@ -29,3 +29,17 @@ export function boardNotWritable(file, error, environment = process.env) {return new Refused('BOARD_NOT_WRITABLE',`Cannot write the board in "${folder}". Give the agent write access to that folder, the repo's .git/pullboard, or run the command outside the sandbox.`);}++/**+ * Replace SQLite's bare lock error with the board path and elapsed wait when the wait expires.+ *+ * @param {string} file+ * @param {number} startedAt+ * @param {Error & { errcode?: number }} error+ * @returns {Error}+ */+export function boardBusyError(file, startedAt, error) {+ const sqliteCode = Number.isInteger(error.errcode) ? error.errcode & 0xff : null;+ if (sqliteCode !== 5 && error.code !== 'SQLITE_BUSY' && !/database is locked/iu.test(error.message ?? '')) return error;+ return new Refused('BOARD_BUSY', `board file ${file} remained held by another process after ${Date.now() - startedAt}ms; wait for it to finish and retry`);+}diff --git a/src/board-schema.js b/src/board-schema.jsindex aa340696..dc32af7a 100644--- a/src/board-schema.js+++ b/src/board-schema.js@@ -2,6 +2,8 @@import { DatabaseSync } from 'node:sqlite';import { existsSync, realpathSync } from 'node:fs';import { basename, dirname, join, resolve } from 'node:path';+import { codeIsFromMain } from './git.js';+import { Refused } from './refused.js';const reported = new Set();@@ -61,3 +63,32 @@ export function reportSharedSchema(file, diagnostic, onDiagnostic) {reported.add(key);if (reported.size > 128) reported.delete(reported.values().next().value);}+++const declarations = new WeakMap();++/** Resolve schema ownership independently of caller hints, while permitting fresh private fixtures. */+export function sharedSchemaOwner(file, hint) {+ let owner = isRepositoryBoard(file) ? hint !== false && codeIsFromMain() : hint;+ if (file !== ':memory:' && owner === false && !existsSync(file)) {+ // Linked repositories need trunk to initialize their common board; a fresh private project+ // has no existing shared schema to replace.+ if (isRepositoryBoard(file) && existsSync(join(dirname(dirname(file)), 'worktrees'))) {+ throw new Refused('UNMERGED_SCHEMA', 'the shared board is missing; run pullboard status from the main checkout to initialize it before using this worktree');+ }+ owner = true;+ }+ return owner;+}++/** Cache the complete source declaration without running any DDL on the shared connection. */+export function expectedBoardSchema(schema, migrate, triggers) {+ if (!declarations.has(migrate)) {+ declarations.set(migrate, declaredBoardSchema(db => {+ db.exec(schema);+ migrate(db);+ for (const { sql } of triggers()) db.exec(sql);+ }));+ }+ return declarations.get(migrate);+}diff --git a/src/board.js b/src/board.jsindex 1a04e5ea..12874240 100644--- a/src/board.js+++ b/src/board.js@@ -21,11 +21,11 @@ import {existsSync,mkdirSync,} from 'node:fs';-import { dirname, join } from 'node:path';-import { codeIsFromMain } from './git.js';-import { isRepositoryBoard, declaredBoardSchema, sharedSchemaDiagnostic, reportSharedSchema } from './board-schema.js';+import { dirname } from 'node:path';+import { sharedSchemaOwner, expectedBoardSchema, sharedSchemaDiagnostic, reportSharedSchema } from './board-schema.js';import { DatabaseSync } from 'node:sqlite';-import { boardNotWritable } from './board-access.js';+import { boardNotWritable, boardBusyError } from './board-access.js';+export { boardBusyError } from './board-access.js';import {briefFiles,briefSections,@@ -142,20 +142,6 @@ export const EVENT_LOG_VERSION = 1;/** Maximum time a board connection waits for another process to release SQLite's lock. */export const BOARD_BUSY_TIMEOUT_MS = 10_000;-/**- * Replace SQLite's bare lock error with the board path and elapsed wait when the wait expires.- *- * @param {string} file- * @param {number} startedAt- * @param {Error & { errcode?: number }} error- * @returns {Error}- */-export function boardBusyError(file, startedAt, error) {- const sqliteCode = Number.isInteger(error.errcode) ? error.errcode & 0xff : null;- if (sqliteCode !== 5 && error.code !== 'SQLITE_BUSY' && !/database is locked/iu.test(error.message ?? '')) return error;- return new Refused('BOARD_BUSY', `board file ${file} remained held by another process after ${Date.now() - startedAt}ms; wait for it to finish and retry`);-}-const BRIEF_LIMIT = 8000;const REVIEW_RELEASE_COOLDOWN_MS = 60 * 60 * 1000;@@ -282,7 +268,7 @@ export function openBoardReadOnly(file) {* @returns {{ db: DatabaseSync, clock: { now: () => Date } }}*/export function openBoard(file, clock = systemClock, { isMain, onDiagnostic } = {}) {- if (isRepositoryBoard(file)) isMain = isMain !== false && codeIsFromMain();+ isMain = sharedSchemaOwner(file, isMain);if (file !== ':memory:' && isMain === false) {let boardVersion = 0;let logVersion = 0;@@ -309,14 +295,6 @@ export function openBoard(file, clock = systemClock, { isMain, onDiagnostic } =}}}- if (file !== ':memory:' && isMain === false && !existsSync(file)) {- // A fresh private fixture or single-checkout project has no shared schema to preserve.- // A linked repository must have its trunk initialize the common board.- if (isRepositoryBoard(file) && existsSync(join(dirname(dirname(file)), 'worktrees'))) {- throw new Refused('UNMERGED_SCHEMA', 'the shared board is missing; run pullboard status from the main checkout to initialize it before using this worktree');- }- isMain = true;- }if (file !== ':memory:') mkdirSync(dirname(file), { recursive: true });const startedAt = Date.now();let db;@@ -333,7 +311,7 @@ export function openBoard(file, clock = systemClock, { isMain, onDiagnostic } =} else {// Read compatibility before any local effect; persistent schema belongs to trunk alone.readEventLogVersion(db);- reportSharedSchema(file, sharedSchemaDiagnostic(db, expectedBoardSchema()), onDiagnostic);+ reportSharedSchema(file, sharedSchemaDiagnostic(db, expectedBoardSchema(SCHEMA, migrate, storeTriggers)), onDiagnostic);}guardMoves(board);return board;@@ -343,17 +321,6 @@ export function openBoard(file, clock = systemClock, { isMain, onDiagnostic } =}}-let expectedSchema;--/** Cache this source's complete declaration using a private in-memory database, without touching trunk. */-function expectedBoardSchema() {- return expectedSchema ??= declaredBoardSchema(db => {- db.exec(SCHEMA);- migrate(db);- for (const { sql } of storeTriggers()) db.exec(sql);- });-}-/*** 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 columndiff --git a/test/api.test.js b/test/api.test.jsindex 77d71c28..c24268ff 100644--- a/test/api.test.js+++ b/test/api.test.js@@ -552,7 +552,7 @@ test('[A5, A6] a future-schema worktree cannot migrate the shared board; main caconst guardedSource = join(unguarded, 'src/board.js');const guardedText = readFileSync(guardedSource, 'utf8');const unguardedText = guardedText.replace(- " if (isRepositoryBoard(file)) isMain = isMain !== false && codeIsFromMain();",+ " isMain = sharedSchemaOwner(file, isMain);",' isMain = true;',);assert.notEqual(unguardedText, guardedText, 'the mutation removes only the unmerged-schema guard');