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(land): guard abandoned workers and retain repair moves [Q3,B3]
Corey Olson12c372740ea3
diff --git a/src/land.js b/src/land.jsindex c0c63dff..e30ba1f2 100644--- a/src/land.js+++ b/src/land.js@@ -690,7 +690,7 @@ export async function land(options) {adopt: options.adopt, fence: lease.fence };if (batchOptions.maximum === null) delete batchOptions.maximum;return await runLandingWorker(batchOptions, { write, checkLease, checkLeaseNow: lease.assertCurrent,- onWait: options.onWait, say: options.say, signal: controller.signal });+ bindWorker: lease.bindWorker, onWait: options.onWait, say: options.say, signal: controller.signal });} catch (error) { failed = true; throw error; }finally {try { await lease.release(); }diff --git a/src/landing-lease.js b/src/landing-lease.jsindex 4854d97e..d0ebac53 100644--- a/src/landing-lease.js+++ b/src/landing-lease.js@@ -27,6 +27,7 @@ function validEntry(entry) {&& entry.branch.length <= 256 && typeof entry.label === 'string' && entry.label.length > 0 && entry.label.length <= 160&& !/[\r\n]/u.test(entry.label) && Number.isSafeInteger(entry.pid) && entry.pid > 0&& (entry.lineage === null || typeof entry.lineage === 'string' && entry.lineage.length > 0 && entry.lineage.length <= 64)+ && (entry.guardianPid === undefined || Number.isSafeInteger(entry.guardianPid) && entry.guardianPid > 0)&& Number.isFinite(Date.parse(entry.until)) && Number.isFinite(Date.parse(entry.since));}@@ -62,9 +63,9 @@ function save(board, state, action, token) {}/** Enqueue, renew, inspect ownership or release through one deterministic ordered operation. */-export function landingLeaseMove(board, { agentId, action, token, branch, pid, generation }) {+export function landingLeaseMove(board, { agentId, action, token, branch, pid, generation, guardianPid }) {if (agentId !== COORDINATOR) throw new Refused('COORDINATOR_ONLY', 'only the coordinator takes a landing lease; ask your coordinator to run pullboard land');- if (!['request', 'renew', 'release'].includes(action) || !/^[a-f0-9-]{36}$/u.test(token ?? '')) {+ if (!['request', 'renew', 'release', 'bind'].includes(action) || !/^[a-f0-9-]{36}$/u.test(token ?? '')) {throw new Refused('LAND_LEASE_REQUEST', 'a landing lease needs its command identity and a supported action; run pullboard land');}return atomic(board, () => {@@ -74,9 +75,12 @@ export function landingLeaseMove(board, { agentId, action, token, branch, pid, gif (action === 'release') {if (state.holder?.token === token && state.holder.generation === generation) state.holder = null;state.line = state.line.filter(entry => entry.token !== token);- } else if (action === 'renew') {+ } else if (action === 'renew' || action === 'bind') {const holder = assertLandingLease(board, { token, branch, generation });- state.holder = { ...holder, until: new Date(at.getTime() + LANDING_LEASE_MS).toISOString() };+ if (action === 'bind' && (!Number.isSafeInteger(guardianPid) || guardianPid < 1 || holder.guardianPid && holder.guardianPid !== guardianPid)) {+ throw new Refused('LAND_LEASE_REQUEST', 'this landing already names its owned guardian; start a new pullboard land command');+ }+ state.holder = { ...holder, ...(action === 'bind' ? { guardianPid } : {}), until: new Date(at.getTime() + LANDING_LEASE_MS).toISOString() };} else {requireConfirmedLanding(board);if (state.holder && Date.parse(state.holder.until) <= at.getTime()) state.holder = null;@@ -84,7 +88,8 @@ export function landingLeaseMove(board, { agentId, action, token, branch, pid, gconst old = state.holder?.token === token ? state.holder : state.line.find(entry => entry.token === token);const holder = coordinatorHolder(board);const entry = { token, branch, pid, label: `${holder?.label ?? 'coordinator'} landing ${token.slice(0, 8)}`,- lineage: holder?.lineage ?? null, since: old?.since ?? at.toISOString(), until: new Date(at.getTime() + LANDING_LEASE_MS).toISOString() };+ lineage: holder?.lineage ?? null, ...(old?.guardianPid ? { guardianPid: old.guardianPid } : {}),+ since: old?.since ?? at.toISOString(), until: new Date(at.getTime() + LANDING_LEASE_MS).toISOString() };if (!validEntry(entry)) throw new Refused('LAND_LEASE_REQUEST', 'a landing request needs its trunk and process identity; run pullboard land');if (old && (old.branch !== branch || old.pid !== pid || old.lineage !== entry.lineage)) {throw new Refused('LAND_LEASE_REQUEST', 'this landing identity already belongs to another invocation; start a new pullboard land command');diff --git a/src/landing-resource.js b/src/landing-resource.jsindex 19852599..8a9cd604 100644--- a/src/landing-resource.js+++ b/src/landing-resource.js@@ -8,7 +8,14 @@ import { Refused } from './refused.js';function deadLocalOwner(holder) {if (!holder || holder.lineage !== null) return false;try { process.kill(holder.pid, 0); return false; }- catch (error) { return error.code === 'ESRCH'; }+ catch (error) {+ if (error.code !== 'ESRCH') return false;+ if (holder.guardianPid) {+ try { process.kill(holder.guardianPid, 0); return false; }+ catch (guardianError) { return guardianError.code === 'ESRCH'; }+ }+ return true;+ }}/** Return a generation-bound lease whose renewal failures prevent later landing writes. */@@ -27,6 +34,7 @@ export async function takeLandingResource({ board, write, branch, onWait, onLostif (lost) throw lost;try {const result = await write('landingLeaseMove', [{ ...parameters, action: 'renew', generation }]);+ if (lost) throw lost;assertLandingLease(board, { token, branch, generation }); // A delayed recovered receipt is not a fresh grant.deadline = Date.parse(result.holder.until);return result;@@ -55,6 +63,14 @@ export async function takeLandingResource({ board, write, branch, onWait, onLost}, 100);watchdog.unref();return { name: 'landing', scope: 'board', fence: { token, branch, generation }, assertHeld,+ /** Record the guardian before it starts physical work, so dead-parent recovery waits for its drain. */+ async bindWorker(guardianPid) {+ await assertHeld();+ if (lost) throw lost;+ const result = await write('landingLeaseMove', [{ ...parameters, action: 'bind', generation, guardianPid }]);+ if (lost) throw lost;+ deadline = Date.parse(result.holder.until);+ },/** Check the current wall-time fence without adding an await before a physical action. */assertCurrent() { return assertLandingLease(board, { token, branch, generation }); },/** End renewals before releasing; a stale generation never clears its successor. */diff --git a/src/landing-worker.js b/src/landing-worker.jsindex 06faff56..23a7293a 100644--- a/src/landing-worker.js+++ b/src/landing-worker.js@@ -9,6 +9,9 @@ import { parseProcessCensus } from './process-census.js';const FILE = fileURLToPath(import.meta.url);+/** Preserve one typed refusal prefix while passing it through more than one process. */+function messageText(error) { return String(error.message).replace(/^\[[A-Z][A-Z0-9_]*\] /u, ''); }+/** Stop only the process group this invocation created; never signal another landing's processes. */function stop(child) {if (!child.pid) return;@@ -32,19 +35,20 @@ function stop(child) {}/** Run blocking batch work under a live parent that owns all canonical writes and lease renewal. */-export async function runLandingWorker(options, { write, checkLease, checkLeaseNow, onWait, say, signal }) {- const child = spawn(process.execPath, [FILE], { cwd: options.root, env: process.env, detached: true, stdio: ['ignore', 'ignore', 'pipe', 'ipc'] });+export async function runLandingWorker(options, { write, checkLease, checkLeaseNow, bindWorker, onWait, say, signal }) {+ const child = spawn(process.execPath, [FILE, 'guardian'], { cwd: options.root, env: process.env, detached: true, stdio: ['ignore', 'ignore', 'pipe', 'ipc'] });+ const inFlight = new Set();let result;let failure;let stderr = '';child.stderr.setEncoding('utf8').on('data', part => { stderr = (stderr + part).slice(-65536); });/** A failed authority check stops the whole owned group before another command can adopt its worktree. */- const abort = () => { failure ??= signal.reason; stop(child); };+ const abort = () => { failure ??= signal.reason; if (child.exitCode === null && child.signalCode === null) stop(child); };signal.addEventListener('abort', abort, { once: true });/** Reply only over this owned child's channel, preserving the original typed refusal. */const answer = (id, value, error) => {if (!child.connected) return;- child.send({ type: 'answer', id, value, error: error ? { code: error.code ?? 'LAND_WORKER', message: error.message } : null }, () => {});+ child.send({ type: 'answer', id, value, error: error ? { code: error.code ?? 'LAND_WORKER', message: messageText(error) } : null }, () => {});};child.on('message', message => {if (message.type === 'say') { say(message.text); return; }@@ -53,20 +57,25 @@ export async function runLandingWorker(options, { write, checkLease, checkLeaseNif (message.type !== 'call') return;/** Permit only the batch worker's narrow protocol, retaining all authority in this parent. */const act = async () => {+ if (signal.aborted) throw signal.reason;if (message.operation === 'check') { await checkLease(); checkLeaseNow(); return null; }if (message.operation === 'wait') return await onWait?.(message.args[0]);- if (!['recordLandingBatch', 'finishLandingBatch', 'recordLandingFlake', 'recordFlakeObservation', 'recordGateRun'].includes(message.operation)) {+ if (!['recordLandingBatch', 'finishLandingBatch', 'recordLandingFlake', 'recordFlakeObservation', 'recordGateRun', 'reopen', 'appendFact'].includes(message.operation)) {throw new Refused('LAND_WORKER', 'the landing worker requested an unsupported operation; stop it and inspect pullboard resume');}return await write(message.operation, message.args);};- void act().then(value => answer(message.id, value), error => answer(message.id, null, error));+ const pending = act().then(value => answer(message.id, value), error => answer(message.id, null, error));+ inFlight.add(pending);+ void pending.finally(() => inFlight.delete(pending));});try {- const terminal = new Promise((resolveClose, reject) => {- child.once('error', reject);+ const terminal = new Promise(resolveClose => {+ child.once('error', error => { failure = error; });child.once('close', (code, terminatedBy) => resolveClose({ code, signal: terminatedBy }));});+ await new Promise((resolveSpawn, reject) => { child.once('spawn', resolveSpawn); child.once('error', reject); });+ await bindWorker(child.pid);if (signal.aborted) abort();else child.send({ type: 'start', options });const status = await terminal;@@ -75,7 +84,8 @@ export async function runLandingWorker(options, { write, checkLease, checkLeaseNreturn result;} finally {signal.removeEventListener('abort', abort);- stop(child);+ if (child.exitCode === null && child.signalCode === null) stop(child);+ await Promise.allSettled(inFlight);}}@@ -116,7 +126,7 @@ async function workerMain(options) {pipe.finish({ type: 'result', value: result });} catch (error) {process.exitCode = 1;- pipe.finish({ type: 'failure', code: error.code ?? 'LAND_WORKER', message: error.message });+ pipe.finish({ type: 'failure', code: error.code ?? 'LAND_WORKER', message: messageText(error) });} finally { store.closeBoard(board); }}@@ -127,6 +137,45 @@ function assertWorkerFence(board, fence) {}import { assertLandingLease } from './landing-lease.js';-if (process.argv[1] === FILE && process.send) process.once('message', message => {- if (message.type === 'start') void workerMain(message.options);-});+/** A nonblocking guardian survives CLI death and stops its own whole group, even while the worker is synchronous. */+function guardianMain() {+ let worker;+ let finished = false;+ let closed = false;+ let pending = 0;+ /** Flush result and refusal messages before ending this private channel normally. */+ const finish = () => {+ if (!closed || pending) return;+ finished = true;+ if (process.connected) process.disconnect();+ };+ process.on('message', message => {+ if (message.type === 'start' && !worker) {+ // The worker stays in this guardian's detached group; its gates inherit that group too.+ worker = spawn(process.execPath, [FILE, 'worker'], { cwd: message.options.root, env: process.env,+ stdio: ['ignore', 'ignore', 'inherit', 'ipc'] });+ worker.on('message', value => {+ if (!process.connected) return;+ pending += 1;+ process.send(value, () => { pending -= 1; finish(); });+ });+ worker.once('error', error => {+ if (process.connected) process.send({ type: 'failure', code: 'LAND_WORKER', message: error.message });+ });+ worker.once('close', (code, signal) => {+ closed = true;+ process.exitCode = code ?? (signal ? 1 : 0);+ finish();+ });+ worker.send(message);+ } else if (worker?.connected) worker.send(message, () => {});+ });+ process.once('disconnect', () => { if (!finished) stop({ pid: process.pid }); });+}++if (process.argv[1] === FILE && process.send) {+ if (process.argv[2] === 'guardian') guardianMain();+ else if (process.argv[2] === 'worker') process.once('message', message => {+ if (message.type === 'start') void workerMain(message.options);+ });+}diff --git a/test/landing-lease.test.js b/test/landing-lease.test.jsindex 21b4ef24..f67f77fd 100644--- a/test/landing-lease.test.js+++ b/test/landing-lease.test.js@@ -34,6 +34,9 @@ test('landing admission is FIFO and stale releases cannot free a successor [Q1,Qassert.equal(store.landingLeaseMove(board, second).position, 1);assert.equal(store.landingLeaseMove(board, third).position, 2);const fence = { ...first, generation: acquired.generation };+ store.landingLeaseMove(board, { ...fence, action: 'bind', guardianPid: process.pid });+ assert.equal(store.landingLeaseState(board).holder.guardianPid, process.pid);+ assert.throws(() => store.landingLeaseMove(board, { ...fence, action: 'bind', guardianPid: process.pid + 1 }), error => error.code === 'LAND_LEASE_REQUEST');assert.throws(() => guardLandingWrite(board), error => error.code === 'LAND_LEASE_LOST');assert.throws(() => assertLandingLease(board, { ...fence, branch: 'refs/heads/other' }), error => error.code === 'LAND_LEASE_LOST');store.landingLeaseMove(board, { ...fence, action: 'release' });diff --git a/test/landing-queue-fixture.js b/test/landing-queue-fixture.jsindex 1b5df68e..6850c481 100644--- a/test/landing-queue-fixture.js+++ b/test/landing-queue-fixture.js@@ -1,6 +1,6 @@/** Public, real-Git landing contention fixture; no private drill data is used [B3,R3,Q1,Q2,E8]. */import assert from 'node:assert/strict';-import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';+import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';import { join } from 'node:path';import * as store from '../src/board.js';@@ -18,6 +18,12 @@ function readBoard(box, action) {finally { store.closeBoard(board); }}+/** Observe only a recorded fixture PID; no process is signalled by this read. */+function alive(pid) {+ try { process.kill(pid, 0); return true; }+ catch (error) { if (error.code === 'ESRCH') return false; throw error; }+}+/** Build two independent items and accept the higher id first through actual CLI checks. */function acceptedPair(helpers, box, staleAfterFirst) {const entries = [1, 2].map(number => {@@ -50,16 +56,17 @@ function acceptedPair(helpers, box, staleAfterFirst) {}/** Prove named waiting, exclusion and verdict order with two real overlapping land commands. */-export async function landingQueueCase(t, helpers, { staleAfterFirst = false } = {}) {+export async function landingQueueCase(t, helpers, { staleAfterFirst = false, killParent = false } = {}) {const sandbox = helpers.sandbox();const arm = join(sandbox.dir, 'landing-armed');const entered = join(sandbox.dir, 'landing-entered');const release = join(sandbox.dir, 'landing-release');+ const gatePid = join(sandbox.dir, 'landing-gate-pid');const gateFile = join(sandbox.dir, 'landing-queue-gate.cjs');writeFileSync(gateFile, ["const fs = require('node:fs'); const assert = require('node:assert/strict');",- `const [arm,entered,release] = ${JSON.stringify([arm, entered, release])};`,- "if (fs.existsSync(arm)) { fs.writeFileSync(entered, process.cwd()); const end = Date.now() + 20000; while (!fs.existsSync(release) && Date.now() < end) Atomics.wait(new Int32Array(new SharedArrayBuffer(4)),0,0,20); assert.ok(fs.existsSync(release), 'owning test releases the gate'); }",+ `const [arm,entered,release,gatePid] = ${JSON.stringify([arm, entered, release, gatePid])};`,+ "if (fs.existsSync(arm)) { fs.writeFileSync(gatePid, String(process.pid)); fs.writeFileSync(entered, process.cwd()); const end = Date.now() + 20000; while (!fs.existsSync(release) && Date.now() < end) Atomics.wait(new Int32Array(new SharedArrayBuffer(4)),0,0,20); assert.ok(fs.existsSync(release), 'owning test releases the gate'); }","const files = fs.existsSync('web') ? fs.readdirSync('web').filter(file => /^queue-\\d+\\.txt$/.test(file)) : [];","for (const file of files) assert.match(fs.readFileSync('web/' + file,'utf8'), /^works/);","console.log('TAP version 13\\nok 1 - landed queue files keep their behavior\\n1..1\\n# tests 1\\n# pass 1\\n# fail 0');",@@ -102,6 +109,24 @@ export async function landingQueueCase(t, helpers, { staleAfterFirst = false } =await helpers.waitFor(() => Date.parse(readBoard(box, board => store.landingLeaseState(board).holder?.until)) > Date.parse(holder.until),'the independent parent renews while the worker is blocked inside its real synchronous gate');assert.equal(readBoard(box, board => store.landingBatches(board).at(-1).state), 'gating');+ if (killParent) {+ const blockedGatePid = Number(readFileSync(gatePid, 'utf8'));+ assert.ok(Number.isSafeInteger(holder.guardianPid));+ first.kill('SIGKILL'); // Kill only the CLI, leaving its separately grouped guardian responsible for the blocked gate.+ assert.equal((await first.closed).signal, 'SIGKILL');+ await helpers.waitFor(() => !alive(holder.guardianPid) && !alive(blockedGatePid), 'the guardian and blocked gate stop without releasing their marker');+ assert.equal(existsSync(release), false, 'the owning test did not let the abandoned gate proceed');+ const waiting = await second.closed;+ assert.equal(waiting.code, 1);+ assert.match(second.stderrText, /LAND_ADOPT/u, 'a queued ordinary land preserves explicit interrupted-batch adoption');+ rmSync(arm); // Future gates may run, but removing arm cannot unblock the old gate's release loop.+ const adopted = JSON.parse(run(box, box.repo, 'land', '--adopt', '--json'));+ assert.equal(adopted.batch.id, firstBatch.id);+ assert.deepEqual(adopted.landed, [entries[1].id]);+ run(box, box.repo, 'land', '--max', '1');+ assert.deepEqual(readBoard(box, board => store.landingBatches(board).flatMap(batch => batch.items.map(entry => entry.id))), [entries[1].id, entries[0].id]);+ return;+ }writeFileSync(release, 'released');const results = await Promise.all(children.map(child => child.closed));if (staleAfterFirst) {diff --git a/test/team.test.js b/test/team.test.jsindex fe82ab2b..ef2612e2 100644--- a/test/team.test.js+++ b/test/team.test.js@@ -37,6 +37,9 @@ test('a trunk lands one batch at a time [B3,R3,Q1,Q2,E8]', { timeout: 120_000 },test('a queued landing rechecks freshness after its holder moves main [E8,Q1,Q2]', { timeout: 120_000 }, // budget: real private Git/CLI and deterministic gate barrier; no speed target.async t => landingQueueCase(t, helpers, { staleAfterFirst: true }));+test('an abandoned landing drains its blocked gate before adoption [Q1,Q3,B3]', { timeout: 120_000 }, // budget: private CLI death, owned-process cleanup, interrupted-batch adoption and Git; no speed target.+ async t => landingQueueCase(t, helpers, { killParent: true }));+/** Open a fixture's independent board and always close it after its observation or public move. */function withBoard(root, action) {const board = store.openBoard(join(root, '.git', 'pullboard', 'board.sqlite'));