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(init): init --yes never waits on its own commit's hook [I1,I2]

Corey Olson4b743b4cb7b2
diff --git a/src/agent-session.js b/src/agent-session.js
index f8ad03b2..1c3a46d3 100644
--- a/src/agent-session.js
+++ b/src/agent-session.js
@@ -13,9 +13,35 @@ import {
join,
resolve,
} from 'node:path';
-import { takeResource } from './resources.js';
+import {
+ holdsLease,
+ takeResource,
+} from './resources.js';
import { Refused } from './refused.js';
+/**
+ * A command that holds this checkout passes its lease to the processes it starts, its Git hooks among them, so they act
+ * under it instead of queueing behind their own parent (#906: init --yes committed, and its pre-commit hook waited on
+ * init forever).
+ */
+export const CHECKOUT_SESSION_TOKEN_ENV = 'PULLBOARD_CHECKOUT_SESSION_TOKEN';
+/** How long a hook waits for a checkout another command holds before it names the holder and stops (#906). */
+export const HOOK_CHECKOUT_WAIT_MS = 30_000;
+
+/** Take the checkout's lease, act under the one a parent command passed down, or stop after waitLimitMs naming the holder. */
+async function checkoutLease(info, name, agent, waitLimitMs) {
+ const inherited = process.env[CHECKOUT_SESSION_TOKEN_ENV];
+ if (inherited && holdsLease({ name, scope: 'repo', root: info.root, token: inherited })) return { token: inherited, release() {} };
+ const startedAt = Date.now();
+ const onWait = waitLimitMs ? (state) => {
+ if (Date.now() - startedAt < waitLimitMs) return;
+ const holder = state?.holders?.[0];
+ throw new Refused('CHECKOUT_BUSY', `this checkout is in use by ${holder ? `${holder.agent} (since ${holder.since})` : 'another Pullboard command'}, `
+ + `and this hook waited ${Math.round(waitLimitMs / 1000)} s for it; let that command finish, or stop it if it is stuck, then commit again`);
+ } : undefined;
+ return takeResource({ name, capacity: 1, scope: 'repo', root: info.root, agent, ...(onWait ? { onWait } : {}) });
+}
+
/** Keep each worktree's binding in its own Git directory rather than the shared common directory. */
export function checkoutSessionFile(info) {
return join(info.gitDir, 'pullboard-checkout-session.json');
@@ -41,10 +67,10 @@ export function localSessionMatches(info, { agent, digest }) {
}
/** Serialize local binding changes, record an explicit takeover, and replace complete JSON atomically. */
-export async function bindLocalSession(info, { agent, digest, takeover = false, recordTakeover, keepLease = false }) {
+export async function bindLocalSession(info, { agent, digest, takeover = false, recordTakeover, keepLease = false, waitLimitMs = 0 }) {
const file = checkoutSessionFile(info);
const name = 'checkout-session:' + createHash('sha256').update(info.gitDir).digest('hex');
- const lease = await takeResource({ name, capacity: 1, scope: 'repo', root: info.root, agent });
+ const lease = await checkoutLease(info, name, agent, waitLimitMs);
let retained = false;
/** Atomically replace the complete local record, keeping identifiers out of durable state. */
function persist(identity) {
@@ -62,7 +88,17 @@ export async function bindLocalSession(info, { agent, digest, takeover = false,
if (!previous || takeover || previous.agent !== agent) persist(agent);
if (keepLease) {
retained = true;
- return { release: lease.release, updateAgent: persist };
+ // Processes this command starts while it holds the checkout inherit the lease; the previous value returns with it.
+ const previous = process.env[CHECKOUT_SESSION_TOKEN_ENV];
+ process.env[CHECKOUT_SESSION_TOKEN_ENV] = lease.token;
+ const release = () => {
+ if (process.env[CHECKOUT_SESSION_TOKEN_ENV] === lease.token) {
+ if (previous === undefined) delete process.env[CHECKOUT_SESSION_TOKEN_ENV];
+ else process.env[CHECKOUT_SESSION_TOKEN_ENV] = previous;
+ }
+ lease.release();
+ };
+ return { release, updateAgent: persist };
}
} finally { if (!retained) lease.release(); }
}
diff --git a/src/cli.js b/src/cli.js
index 0786a893..2cf33203 100644
--- a/src/cli.js
+++ b/src/cli.js
@@ -75,6 +75,7 @@ import {
} from './person.js';
import {
bindLocalSession,
+ HOOK_CHECKOUT_WAIT_MS,
localSessionMatches,
localSessionSeats,
} from './agent-session.js';
@@ -1031,7 +1032,7 @@ async function bindCheckoutSession(io, positionals, values) {
CHECKOUT_BOARD_ONLY.set(io, command);
return;
}
- const lease = await bindLocalSession(info, { agent: agent ?? 'unjoined agent', digest, takeover: command === 'takeover', keepLease: !['run', 'next'].includes(command),
+ 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);
diff --git a/src/next-steps.js b/src/next-steps.js
index 38248095..fa5403c1 100644
--- a/src/next-steps.js
+++ b/src/next-steps.js
@@ -203,6 +203,7 @@ export const NEXT_STEPS = Object.freeze({
BROWSER_SLOT_REQUIRED: 'release the gate lease and take it again with browser: true',
CANNOT_SIGN: 'sign only approved rows: pullboard spec signoff <approved ids>',
CHECKOUT_SESSION: 'run pullboard takeover from the agent session that owns this checkout',
+ CHECKOUT_BUSY: 'let the named command finish, or stop it if it is stuck, then commit again',
CHECK_SYNTAX: 'close the quote the shell names, then set the check again: inside single quotes write an apostrophe as \'\\\'\' or match it with ., and test with sh -n -c "<command>"',
CHECK_ACCEPT_PERSON_ONLY: 'ask the coordinator to bring the check to the person: pullboard shout coordinator "#<id> needs the person to accept its check" --decision',
CHECK_ACCEPT_STALE: 'read the current check with pullboard show <id>, then the person accepts it again: pullboard check accept <id>',
diff --git a/src/resources.js b/src/resources.js
index 58440f62..8c02b713 100644
--- a/src/resources.js
+++ b/src/resources.js
@@ -872,6 +872,16 @@ export function inheritedGateSlot({ root = process.cwd(), browser = false, named
};
}
+/** Whether a live process holds this exact lease token, so a child it started may act under it (#906). */
+export function holdsLease({ name, scope = 'repo', root = process.cwd(), token }) {
+ if (typeof token !== 'string' || !token) return false;
+ const db = open(databaseFile(scope, root), scope === 'machine');
+ try {
+ const holder = update(db, () => db.prepare('SELECT pid FROM holder WHERE name = ? AND token = ?').get(name, token));
+ return Boolean(holder) && alive(holder.pid);
+ } finally { db.close(); }
+}
+
/** Raise live capacity atomically; a decrease waits for an empty queue without evicting holders. */
export function setResourceCapacity({ name, capacity, scope = 'machine', root = process.cwd(), persist = () => {} }) {
if (typeof name !== 'string' || !name.trim() || name.trim() !== name) throw new Refused('BAD_RESOURCE_NAME', 'resource name must be nonempty trimmed text');
diff --git a/test/newcomer.test.js b/test/newcomer.test.js
index 7ac2cfba..dffb7f44 100644
--- a/test/newcomer.test.js
+++ b/test/newcomer.test.js
@@ -1,11 +1,16 @@
/** First-run guidance on private Git repos, including unrelated files setup must not stage [I1,I2]. */
import assert from 'node:assert/strict';
-import { runFixtureExecFile as execFileSync, runFixtureChild as spawnSync, runFixtureChild, runFixtureGit } from './fixture-child.js';
+import { runFixtureExecFile as execFileSync, runFixtureChild as spawnSync, runFixtureChild, runFixtureChildAsync, runFixtureGit } from './fixture-child.js';
import { mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { test } from 'node:test';
import { AGENT_SHELL_MARKERS } from '../src/person.js';
+import {
+ bindLocalSession,
+ CHECKOUT_SESSION_TOKEN_ENV,
+} from '../src/agent-session.js';
+import { repoInfo } from '../src/git.js';
const BIN = resolve(import.meta.dirname, '../bin/pullboard.js');
@@ -173,3 +178,56 @@ test('bare pullboard starts with the tour and init pointer; explicit help and JS
assert.equal(typeof document.help, 'string');
assert.ok(document.help.includes('pullboard init'));
});
+
+/** A directory whose pullboard is this checkout's, so init's Git hooks run the code under test (#906). */
+function checkoutPullboard(root) {
+ const bin = join(root, '.this-pullboard');
+ mkdirSync(bin);
+ symlinkSync(BIN, join(bin, 'pullboard'));
+ return bin;
+}
+
+test('init --yes commits its setup and exits in every agent session [I1,I2]', { timeout: 600_000 }, async (t) => { // budget: hang ceiling for eight inits, each about 2 s.
+ for (const marker of AGENT_SHELL_MARKERS) {
+ const box = project(t);
+ for (const name of [...AGENT_SHELL_MARKERS, CHECKOUT_SESSION_TOKEN_ENV]) delete box.env[name];
+ box.env[marker] = '1';
+ Object.assign(box.env, { PATH: checkoutPullboard(box.root) + ':' + box.env.PATH, PULLBOARD_MACHINE_HOME: join(box.root, '.private-machine'), PULLBOARD_VIEW_BACKGROUND: '0' });
+ box.git('config', 'user.name', 'Newcomer');
+ box.git('config', 'user.email', '[email protected]');
+ box.git('commit', '-q', '--allow-empty', '-m', 'initial');
+ // 0.9.1 hung here for good: the pre-commit hook of init's own commit waited on init's checkout lease (#906).
+ const running = runFixtureChildAsync(process.execPath, [BIN, 'init', '--yes', '--model', 'Test Model'], { cwd: box.root, env: box.env, detached: true });
+ let timer;
+ const result = await Promise.race([running, new Promise((done) => { timer = setTimeout(() => done(null), 60_000); })]); // budget: hang ceiling; init --yes takes about 2 s.
+ clearTimeout(timer);
+ if (!result) { try { process.kill(-running.child.pid, 'SIGKILL'); } catch { /* the group already ended */ } }
+ assert.ok(result, `init --yes with ${marker} set never exited`);
+ assert.equal(result.status, 0, result.context);
+ assert.equal(String(box.git('log', '--format=%s', '-1')).trim(), 'chore: set up Pullboard', `${marker}: init committed its setup`);
+ }
+});
+
+test('a command started under a held checkout acts under it, and a hook that must wait names the holder and stops [I1,I2]', async (t) => {
+ const box = project(t);
+ box.git('config', 'user.name', 'Newcomer');
+ box.git('config', 'user.email', '[email protected]');
+ box.git('commit', '-q', '--allow-empty', '-m', 'initial');
+ const info = repoInfo(box.root);
+ const before = process.env[CHECKOUT_SESSION_TOKEN_ENV];
+ delete process.env[CHECKOUT_SESSION_TOKEN_ENV];
+ const holder = await bindLocalSession(info, { agent: 'holding-agent', digest: 'a'.repeat(64), keepLease: true });
+ try {
+ // A child of the holding command (its env carries the lease) proceeds at once instead of queueing behind it.
+ assert.ok(process.env[CHECKOUT_SESSION_TOKEN_ENV], 'the holding command passes its lease to what it starts');
+ await bindLocalSession(info, { agent: 'holding-agent', digest: 'a'.repeat(64) });
+ // An unrelated command waits, and a hook gives up after its limit with the holder named.
+ delete process.env[CHECKOUT_SESSION_TOKEN_ENV];
+ await assert.rejects(bindLocalSession(info, { agent: 'holding-agent', digest: 'a'.repeat(64), waitLimitMs: 300 }),
+ (error) => error.code === 'CHECKOUT_BUSY' && /in use by holding-agent \(since /u.test(error.message) && /then commit again/u.test(error.message));
+ } finally {
+ holder.release();
+ if (before === undefined) delete process.env[CHECKOUT_SESSION_TOKEN_ENV];
+ else process.env[CHECKOUT_SESSION_TOKEN_ENV] = before;
+ }
+});