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): an add never waits on the relay lock [H21]

Corey Olson15c8db92733f
diff --git a/src/relay-local-first.js b/src/relay-local-first.js
index 60e6ca60..e96781a9 100644
--- a/src/relay-local-first.js
+++ b/src/relay-local-first.js
@@ -33,13 +33,15 @@ export function senderInBackground(env = process.env) {
}
/**
- * The machine sender's files: its claim, its short log, and one note per board it serves.
+ * The machine sender's files: its claim, its short log, one note per board it serves, and the marker of a sender
+ * being started.
*
- * @returns {{ directory: string, pid: string, log: string, boards: string }}
+ * @returns {{ directory: string, pid: string, log: string, boards: string, starting: string }}
*/
export function senderFiles() {
const directory = join(pullboardHome(), 'relay-sender');
- return { directory, pid: join(directory, 'sender.pid'), log: join(directory, 'sender.log'), boards: join(directory, 'boards') };
+ return { directory, pid: join(directory, 'sender.pid'), log: join(directory, 'sender.log'), boards: join(directory, 'boards'),
+ starting: join(directory, 'sender.starting') };
}
// Budget: a sender refreshes its claim every few seconds while it runs; a claim this old belongs to a process that
@@ -67,6 +69,23 @@ export function senderHolding() {
} catch { return 0; }
}
+// Budget: a sender starts and claims its place well within this; an older start marker belongs to one that died.
+const STARTING_STALE_MS = 10_000;
+
+/**
+ * Mark that a sender is starting, so the several nudges of one burst of commands start one sender, not one each.
+ * True when this caller should start it.
+ */
+function startingSender() {
+ const marker = senderFiles().starting;
+ mkdirSync(senderFiles().directory, { recursive: true, mode: 0o700 });
+ try { writeFileSync(marker, `${process.pid}\n`, { flag: 'wx', mode: 0o600 }); return true; }
+ catch (error) { if (error.code !== 'EEXIST') throw error; }
+ try { if (Date.now() - statSync(marker).mtimeMs < STARTING_STALE_MS) return false; } catch { /* Gone: start one. */ }
+ rmSync(marker, { force: true });
+ try { writeFileSync(marker, `${process.pid}\n`, { flag: 'wx', mode: 0o600 }); return true; } catch { return false; }
+}
+
/** The note a board keeps in the sender's folder, named by its link file. */
export function boardNote(file) {
return join(senderFiles().boards, createHash('sha256').update(file).digest('hex').slice(0, 32) + '.json');
@@ -89,7 +108,7 @@ export function nudgeRelaySender(root, file) {
writeFileSync(temporary, JSON.stringify({ root, file, due: Date.now() }) + '\n', { flag: 'wx', mode: 0o600 });
renameSync(temporary, note);
} finally { rmSync(temporary, { force: true }); }
- if (senderHolding()) return;
+ if (senderHolding() || !startingSender()) return;
const env = { ...process.env };
delete env.PULLBOARD_RELAY_TOKEN; // The sender works only with this machine's own saved credentials.
const child = spawn(process.execPath, [WORKER], { cwd: root, detached: true, stdio: 'ignore', env });
diff --git a/src/relay-sender-worker.js b/src/relay-sender-worker.js
index 8384c3ec..30790544 100644
--- a/src/relay-sender-worker.js
+++ b/src/relay-sender-worker.js
@@ -63,6 +63,7 @@ async function claim() {
const holder = senderHolding();
if (holder && holder !== process.pid) return false;
writeFileSync(files.pid, `${process.pid}\n`, { mode: 0o600 });
+ rmSync(files.starting, { force: true }); // Started: the next nudge sees this claim instead.
writeFileSync(files.log, '', { mode: 0o600 }); // Each sender keeps only its own short story.
io.say(`claimed by ${process.pid}`);
return true;
diff --git a/src/relay.js b/src/relay.js
index 82b93c25..3b83cec6 100644
--- a/src/relay.js
+++ b/src/relay.js
@@ -1445,6 +1445,10 @@ export async function relayRevoke(root, id, io) {
export async function relayCommandReceipt(root, command) {
if (!command) return null;
const file = linkFile(root);
+ // Most commands have no interrupted result to recover: they read that without the relay lock, so an add never
+ // waits on the sender holding it (#1013).
+ const peek = loadLink(file);
+ if (peek && !peek.unlinking && !peek.recovered) return null;
return locked(file, async () => {
const state = loadLink(file);
if (!state) {
@@ -1480,6 +1484,8 @@ export async function relayCommandReceipt(root, command) {
/** Consume the exact cached result after its original CLI outcome has been formatted. */
export async function relayCommandReceiptReported(root, moveId) {
const file = linkFile(root);
+ const peek = loadLink(file);
+ if (peek && peek.recovered?.move?.id !== moveId) return;
return locked(file, async () => {
const state = loadLink(file);
if (state?.recovered?.move?.id === moveId) {
diff --git a/test/relay-local-first.test.js b/test/relay-local-first.test.js
index ab2a6ccb..2beadfde 100644
--- a/test/relay-local-first.test.js
+++ b/test/relay-local-first.test.js
@@ -270,10 +270,10 @@ test('a conflict found on sync is refused at replay with a code naming the winne
assert.ok(refused.earlier.some(move => move.move === `claim #${contested} by ${winner}`), 'the refusal names the winning claim and its agent');
// The agent whose claim lost is told on its next command, once.
- const told = await runFixtureChildAsync(process.execPath, [CLI, 'status', '--json'], { cwd: here.path, env: box.env });
+ const told = await runFixtureChildAsync(process.execPath, [CLI, 'status'], { cwd: here.path, env: box.env });
assert.equal(told.status, 0, told.context);
assert.match(told.stderr, new RegExp(`\\[RELAY_OUTBOX_REFUSED\\] the queued move claim #${contested} by ${here.id} .*\\[HELD\\].*won by sequence \\d+ claim #${contested} by ${winner}`, 'u'));
- const again = await runFixtureChildAsync(process.execPath, [CLI, 'status', '--json'], { cwd: here.path, env: box.env });
+ const again = await runFixtureChildAsync(process.execPath, [CLI, 'status'], { cwd: here.path, env: box.env });
assert.doesNotMatch(again.stderr, /RELAY_OUTBOX_REFUSED/u, 'the notice is said once');
for (const machine of [box, other]) {