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(relay): a relink turns the old link off with a receipt [E12]

Corey Olsonbc82fc989034
diff --git a/relay/auth.js b/relay/auth.js
index 952a5e52..f4a8e2af 100644
--- a/relay/auth.js
+++ b/relay/auth.js
@@ -45,6 +45,7 @@ export function createRelayAuth({ database, github, now = Date.now, sessionTTL =
CREATE TABLE IF NOT EXISTS relay_users (id TEXT PRIMARY KEY, login TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS relay_boards (id TEXT PRIMARY KEY, repository TEXT NOT NULL, repository_id TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS relay_cleanup (board TEXT PRIMARY KEY);
+ CREATE TABLE IF NOT EXISTS relay_board_off (board TEXT PRIMARY KEY, repository_id TEXT NOT NULL, replaced_by TEXT NOT NULL, user_id TEXT NOT NULL, at INTEGER NOT NULL);
CREATE TABLE IF NOT EXISTS relay_deleted_credentials (hash TEXT PRIMARY KEY, board TEXT NOT NULL, expires INTEGER NOT NULL);
CREATE TABLE IF NOT EXISTS relay_credentials (id TEXT PRIMARY KEY, hash TEXT UNIQUE NOT NULL, kind TEXT NOT NULL, user_id TEXT NOT NULL REFERENCES relay_users(id), board TEXT REFERENCES relay_boards(id), agent TEXT, expires INTEGER NOT NULL, revoked INTEGER NOT NULL DEFAULT 0);
CREATE TABLE IF NOT EXISTS relay_action_grants (hash TEXT PRIMARY KEY, request TEXT UNIQUE NOT NULL,
@@ -518,13 +519,24 @@ export function createRelayAuth({ database, github, now = Date.now, sessionTTL =
if (db.prepare('SELECT 1 FROM relay_cleanup WHERE board=?').get(board)) throw new Refused('RELAY_CLEANUP', 'finish pending board cleanup before linking again');
const existing = db.prepare('SELECT * FROM relay_boards WHERE id=?').get(board);
if (existing && existing.repository_id !== String(repo.id)) throw new Refused('BOARD_LINK_CONFLICT', 'use a new board identifier for a different repository');
- const other = db.prepare('SELECT id FROM relay_boards WHERE repository_id=? AND id<>? ORDER BY linked_at DESC, id').all(String(repo.id), board)
- .find((row) => row.id !== replaces && !db.prepare('SELECT 1 FROM relay_cleanup WHERE board=?').get(row.id));
+ // A repository has one active link: an off board, with its receipt, or one under cleanup no longer counts [E12].
+ const active = db.prepare("SELECT id FROM relay_boards WHERE repository_id=? AND id<>? AND id NOT IN (SELECT board FROM relay_board_off) AND id NOT IN (SELECT board FROM relay_cleanup) ORDER BY linked_at DESC, id").all(String(repo.id), board);
+ if (replaces !== null && replaces !== undefined && !active.some((entry) => entry.id === replaces)) {
+ throw new Refused('BOARD_REPLACE', `board ${replaces} is not this repository's active board, so this link replaces nothing; run pullboard relay on again to join the repository's board`);
+ }
+ const other = active.find((entry) => entry.id !== replaces);
if (other) throw new Refused('BOARD_REPO_LINKED', `${repo.name} already has board ${other.id}, and a repository has one team board; pull pullboard.json from main, which names that board, then run pullboard relay on again to join it`);
+ // Relinking after relay off turns the old link off in the same transaction, with its receipt [E12].
+ if (replaces) db.prepare('INSERT INTO relay_board_off (board,repository_id,replaced_by,user_id,at) VALUES (?,?,?,?,?)').run(replaces, String(repo.id), board, row.user_id, now());
db.prepare('INSERT INTO relay_boards (id,repository,repository_id,linked_at,storage_account) VALUES (?,?,?,?,?) ON CONFLICT(id) DO UPDATE SET repository=excluded.repository,storage_account=COALESCE(relay_boards.storage_account,excluded.storage_account)').run(board, repo.name, String(repo.id), now(), row.user_id);
permissions.delete(row.user_id + ':' + board);
});
- return { id: board, repository: repo.name, repositoryID: String(repo.id) };
+ return { id: board, repository: repo.name, repositoryID: String(repo.id), ...(replaces ? { replaced: replaces } : {}) };
+ },
+ /** The receipt that turned a board's link off when a relink replaced it, or null [E12]. */
+ boardOff(board) {
+ open();
+ return db.prepare('SELECT board,replaced_by AS replacedBy,user_id AS userId,at FROM relay_board_off WHERE board=?').get(board) ?? null;
},
/** Internal maintenance inventory; HTTP callers must still pass the normal authorization boundary. */
linkedBoards() {
diff --git a/src/next-step-defaults.js b/src/next-step-defaults.js
index 253f6466..fba740eb 100644
--- a/src/next-step-defaults.js
+++ b/src/next-step-defaults.js
@@ -201,6 +201,7 @@ export const DEFAULT_NEXT_STEPS = Object.freeze({
"BAR_COVERAGE": "record whole-file duration evidence, the named-check duration and any install duration in a coverage JSON file, then run pullboard bar sign <id> --coverage-file <path> --note \"...\"",
"NOTHING_FREE": "wait for work: pullboard next --wait 9 (minutes)",
"NOT_COMMITTED": "commit the files the message names, then run the command again",
+ "BOARD_REPLACE": "run pullboard relay on again; a relink replaces only the repository's active board",
"BOARD_REPO_LINKED": "pull pullboard.json from main, which names the repository's board, then run pullboard relay on again to join it",
"GITHUB_UNAVAILABLE": "run any pullboard command again once GitHub answers; the new device keeps waiting",
"RELAY_JOIN_NOT_EMPTY": "keep this board, then run pullboard init and pullboard relay on in a fresh clone to join the team board",
diff --git a/src/relay-handover.js b/src/relay-handover.js
index c166b52d..d59aa3e1 100644
--- a/src/relay-handover.js
+++ b/src/relay-handover.js
@@ -125,7 +125,7 @@ export async function handOverBoardKeys(state, key, { send, githubKeys }, io) {
const statement = entry.statement;
let body;
try {
- if (statement?.board !== state.board || statement.repository !== state.repository || statement.login !== entry.login || !(statement.expires > Date.now())) {
+ if (statement?.board !== state.board || statement.repository.toLowerCase() !== String(state.repository).toLowerCase() || statement.login !== entry.login || !(statement.expires > Date.now())) {
throw new Refused('RELAY_KEY_VOUCH', 'the key request names another board, repository or person; run pullboard relay on again in the clone');
}
if (!await vouched(statement, entry.signature, entry.login, githubKeys)) {
diff --git a/test/team.test.js b/test/team.test.js
index 2ff532e9..84a79c86 100644
--- a/test/team.test.js
+++ b/test/team.test.js
@@ -773,6 +773,24 @@ test('a clone of a linked repo joins its board; a member device hands over the k
assert.ok((await box.cli('export')).document.tables.item.some((item) => item.item_title === 'From bob’s new computer'), 'one board, one order: alice reads bob’s move');
});
+test('a relink turns the old link off with its receipt, so a repository keeps one active board [E12]', { timeout: 120_000 }, async (t) => { // budget: hang ceiling for real relay calls; not a speed target.
+ const box = await teamBoard(t, null);
+ const old = JSON.parse(readFileSync(box.linkFile, 'utf8')).board;
+ const alice = await personSession(box, 'alice');
+ const next = randomBytes(16).toString('hex');
+ await assert.rejects(box.auth.linkBoard(alice, next, 'fixture/repository'), { code: 'BOARD_REPO_LINKED' }, 'a second board without replaces is refused');
+ await assert.rejects(box.auth.linkBoard(alice, next, 'fixture/repository', Infinity, 'e'.repeat(32)), { code: 'BOARD_REPLACE' }, 'replaces must name the active board');
+ const linked = await box.auth.linkBoard(alice, next, 'fixture/repository', Infinity, old);
+ assert.equal(linked.replaced, old, 'the link reports the board it turned off');
+ const receipt = box.auth.boardOff(old);
+ assert.equal(receipt?.replacedBy, next, 'the old link is off, with a receipt naming its replacement');
+ assert.equal(box.auth.boardOff(next), null, 'the new link is the active one');
+ await assert.rejects(box.auth.linkBoard(alice, randomBytes(16).toString('hex'), 'fixture/repository', Infinity, old), { code: 'BOARD_REPLACE' },
+ 'an off board cannot be replaced again, so no caller can stack links');
+ await assert.rejects(box.auth.linkBoard(alice, randomBytes(16).toString('hex'), 'fixture/repository'), { code: 'BOARD_REPO_LINKED' },
+ 'the replacement is the one active board');
+});
+
/** Every file under a directory, so a test can prove no secret was written anywhere in it. */
function filesUnder(directory) {
if (!existsSync(directory)) return [];