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(relay): a takeover takes the coordinator before its receipt [E19]
Corey Olsondc0a2924283e
diff --git a/src/cli.js b/src/cli.jsindex 916a1636..6fcc25d7 100644--- a/src/cli.js+++ b/src/cli.js@@ -973,17 +973,8 @@ async function bindCheckoutSession(io, positionals, values) {return;}const lease = await bindLocalSession(info, { agent: agent ?? 'unjoined agent', digest, takeover: command === 'takeover', keepLease: !['run', 'next'].includes(command), waitLimitMs: command === 'hook' ? HOOK_CHECKOUT_WAIT_MS : 0,- /** Use an existing ordered shout so takeover has a receipt without changing the board format. */- recordTakeover: async () => {- const ctx = context(io);- await withBoard(ctx, async (board) => {- const me = whoAmI(ctx, board);- const message = { from: me.id, to: info.isMain ? 'person' : COORDINATOR,- text: `${me.displayName} took over this checkout in a new agent session.`, lanes: laneNames(ctx.config) };- if (relayLinked(info.root)) await relayOperation(info.root, 'shout', [message], io, undefined, () => store.shout(board, message));- else store.shout(board, message);- });- },+ /** Record the takeover; a linked main checkout takes the board's coordinator before its receipt shout, which the hold would refuse (#347). */+ recordTakeover: async () => (await import('./commands/takeover.js')).recordCheckoutTakeover(io, info),});if (agent) return { lease };/** Replace the temporary identity after a successful first join, retaining the winning session. */diff --git a/src/commands/takeover.js b/src/commands/takeover.jsindex c080d175..405ea87e 100644--- a/src/commands/takeover.js+++ b/src/commands/takeover.js@@ -1,4 +1,5 @@/** The pullboard takeover command, in its own module so an item adding another command never edits these lines. */+import * as store from '../board.js';import {context,ordered,@@ -6,8 +7,43 @@ import {withBoard,} from '../cli.js';import { COORDINATOR } from '../config.js';+import { laneNames } from '../lanes.js';import { relayLinked } from '../relay.js';import { relayMachineContext } from '../relay.js';+import { relayOperation } from '../relay.js';++/** The takeCoordinator a session already sent while taking this checkout over, by command invocation. */+const TAKEN = new WeakMap();++/** The coordinator move's arguments: this checkout's relay machine id names it in the hold. */+function takeArgs(root) {+ const machine = relayMachineContext(root)?.machine;+ return [{ agentId: COORDINATOR, ...(typeof machine === 'string' ? { machine } : {}) }];+}++/**+ * Record that a new agent session took this checkout over. In a linked main checkout the board's coordinator is+ * taken first, as an ordered move, so the receipt shout that follows is the holder's own and the hold the session+ * is taking never refuses it; a takeover never stops at the step the refusal tells it to run (#347).+ *+ * @param {any} io+ * @param {{ root: string, isMain: boolean }} info+ */+export async function recordCheckoutTakeover(io, info) {+ const ctx = context(io);+ await withBoard(ctx, async (board) => {+ const me = whoAmI(ctx, board);+ const linked = relayLinked(info.root);+ if (info.isMain && linked) {+ const args = takeArgs(info.root);+ TAKEN.set(io, await relayOperation(info.root, 'takeCoordinator', args, io, undefined, () => store.takeCoordinator(board, ...args)));+ }+ const message = { from: me.id, to: info.isMain ? 'person' : COORDINATOR,+ text: `${me.displayName} took over this checkout in a new agent session.`, lanes: laneNames(ctx.config) };+ if (linked) await relayOperation(info.root, 'shout', [message], io, undefined, () => store.shout(board, message));+ else store.shout(board, message);+ });+}/*** Run `pullboard takeover` and return its exit code. In an agent worktree it rebinds the checkout to this agent@@ -18,8 +54,7 @@ export async function run(io) {const ctx = context(io);const me = withBoard(ctx, (board) => whoAmI(ctx, board));if (ctx.info.isMain && relayLinked(ctx.info.root)) {- const machine = relayMachineContext(ctx.info.root)?.machine;- const taken = await withBoard(ctx, (board) => ordered(ctx, board, 'takeCoordinator', [{ agentId: COORDINATOR, ...(typeof machine === 'string' ? { machine } : {}) }]));+ const taken = TAKEN.get(io) ?? await withBoard(ctx, (board) => ordered(ctx, board, 'takeCoordinator', takeArgs(ctx.info.root)));io.result?.({ agent: me.id, path: ctx.info.root, coordinator: taken.holder, previous: taken.previous });io.say(`${me.displayName} now coordinates this board from ${taken.holder}${taken.previous && taken.previous !== taken.holder ? `, taking over from ${taken.previous}` : ''}`);return 0;diff --git a/test/team.test.js b/test/team.test.jsindex 3bae105a..ac0b51d0 100644--- a/test/team.test.js+++ b/test/team.test.js@@ -526,3 +526,22 @@ test('one coordinator per board across machines [E19]', { timeout: relayWorkBudg'Planned on the second machine after takeover', 'Still planned on the first machine']);}});++test('an agent session on a computer that does not hold the coordinator takes it over [E19]', { timeout: relayWorkBudgetMs(12) }, async t => { // budget: hang ceiling for two linked devices' real CLI and relay round trips; not a speed target.+ const box = await relayClientFixture(t);+ await box.link();+ const pairing = await box.cli('relay', 'pair');+ const other = await box.otherDeviceJoin(pairing.document.code);+ /** Run the second computer's real CLI from a named agent session. */+ const inSession = (...args) => relayFixtureChild(other.root, { ...other.env, CLAUDECODE: '1', CLAUDE_CODE_SESSION_ID: 'second-computer-session' },+ [resolve(import.meta.dirname, '../bin/pullboard.js'), ...args, '--json']);+ assert.equal((await box.cli('add', box.lane, 'Planned on the holding computer')).code, 0, 'the first computer holds the coordinator');+ const held = await inSession('add', box.lane, 'Planned before the takeover');+ assert.equal(held.document?.error?.code, 'COORDINATOR_HELD', 'the second computer is refused by name');+ assert.match(held.document.error.message, /pullboard takeover/u, 'the refusal names the takeover');+ const taken = await inSession('takeover');+ assert.equal(taken.code, 0, `the takeover the refusal names succeeds from the agent session: ${JSON.stringify(taken.document)}`);+ assert.equal(taken.document.coordinator, `computer ${other.machine.slice(-8)}`, 'the second computer now holds the coordinator');+ assert.equal((await inSession('add', box.lane, 'Planned after the takeover')).code, 0, 'the new holder coordinates');+ assert.equal((await box.cli('add', box.lane, 'Planned again on the first computer')).document?.error?.code, 'COORDINATOR_HELD', 'the earlier holder is refused');+});