A crashed merge no longer blocks every other merge, and a one-call count of PRs waiting to merge (GST-3549) (#4712)

When `gstack-ship-queue land --pin` dies in the middle of a merge, the
next merge now takes over its lock by itself (the process has exited,
its machine restarted, or 90 minutes passed) and logs it; a merge that
is still running is never interrupted.

New `gstack-ship-queue count` says how many finished PRs are waiting to
merge, and the docs now say how to drain that queue and how to time a
revert drill.

Nothing to do: the next `land --pin` uses the new lock on its own.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Review-Finding: p1/specialist-batched#1 reader=security
Review-Finding: p1/specialist-batched#2 reader=testing
Review-Finding: p1/red-team#2 reader=red-team
Review-Finding: p1/red-team#3 reader=red-team
Review-Finding: p2/outside-voice#1 reader=outside-voice
Review-Finding: p2/outside-voice#3 reader=outside-voice
Review-Finding: p2/outside-voice#4 reader=outside-voice
Review-Finding: p2/red-team#1 reader=red-team
Review-Finding: p2/adversarial-native#1 reader=adversarial-native
Review-Finding: p2/outside-voice#5 reader=outside-voice
Review-Finding: p2/outside-voice#6 reader=outside-voice
Review-Finding: p2/specialist-batched#2 reader=testing
This commit is contained in:
Garry Tanandcapy-ai[bot] authored and GitHub committed 2026-11-22 18:37:39 +00:00
1 parent d3aed00dbe
commit 2ab2aad705
13 files changed
+696 -22

No files matched your search

+227 -1
View File
@@ -10,7 +10,10 @@ import { afterEach, describe, expect, test } from 'bun:test';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { EXIT } from '../../lib/headless-artifacts';
import { acquireLandLock, LAND_LOCK_REF, readLock, writeLock, type LandLock, type LockOwner } from '../lib/ship-queue-land-lock';
import { spawn, spawnSync } from 'node:child_process';
import * as os from 'node:os';
import { acquireLandLock, DIRECT_LOCK_TTL_MS, judgeOwner, LAND_LOCK_REF, readLock, thisProcess, writeLock, type LandLock, type LockOwner, type LockProcess } from '../lib/ship-queue-land-lock';
import { bunIsolationArgs } from '../../lib/bun-isolation';
import { BASE_POLICY, cleanup, makeFixtureRepo, type FixtureRepo } from '../../test/helpers/restamp-fixture';
import { flakyGit } from '../../test/helpers/flaky-git';
import { coordinatorClone } from '../../test/helpers/ship-queue-fixture';
@@ -214,6 +217,229 @@ describe('the lock module', () => {
}, 60_000);
});
/** A real process as a lock records it: its group and kernel start time, with this machine's boot id and pid namespace. */
function procOf(pid: number, self: LockProcess, boot = self.boot): LockProcess {
const stat = fs.readFileSync(`/proc/${pid}/stat`, 'utf8');
const f = stat.slice(stat.lastIndexOf(')') + 2).split(' ');
return { boot, pidns: self.pidns, pid, pgid: Number(f[2]), start: Number(f[19]), mark: `mark-${pid}-${Date.now()}` };
}
/** A detached shell running script; resolves once /proc shows it leading its own process group. */
async function group(script: string) {
const child = spawn('sh', ['-c', script], { detached: true, stdio: 'ignore' });
const exited = new Promise<void>(resolve => child.on('exit', () => resolve()));
const leads = () => { try { const s = fs.readFileSync(`/proc/${child.pid}/stat`, 'utf8'); return Number(s.slice(s.lastIndexOf(')') + 2).split(' ')[2]) === child.pid; } catch { return false; } };
while (!leads()) await Bun.sleep(5);
return { pid: child.pid!, exited, kill: () => { try { process.kill(-child.pid!, 'SIGKILL'); } catch { /* gone */ } } };
}
describe.skipIf(process.platform !== 'linux')('a direct land\'s lock cannot go stale (process-scoped locks)', () => {
const pin = (n: string) => `land-pin:${n.repeat(40)}`;
const marksDirs: string[] = [];
afterEach(() => { while (marksDirs.length) fs.rmSync(marksDirs.pop()!, { recursive: true, force: true }); });
const newMarks = () => { const d = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-land-marks-')); marksDirs.push(d); return d; };
const takeDirect = (root: string, pr: string, o: { proc: LockProcess | null; marks: string; now?: () => Date; breakLock?: boolean; journal?: string; releasesHold?: boolean }) => acquireLandLock({
root, holder: `cox.local:${o.proc?.pid ?? 1}`, owner: { pr }, journal: o.journal ?? pin(pr.slice(-1)), now: o.now ?? at(0), phase: () => 'merging', breakLock: o.breakLock, releasesHold: o.releasesHold,
processScoped: { ttlMs: DIRECT_LOCK_TTL_MS, proc: o.proc, marks: o.marks },
});
test('a lock whose land process has exited is taken over without --break-lock, and the takeover is logged in the output and in the new lock', async () => {
const self = thisProcess()!;
const repo = bareRepo();
const marks = newMarks();
const dead = await group('sleep 30');
const proc = procOf(dead.pid, self);
dead.kill();
await dead.exited;
withRepoEnv(repo, () => {
expect(failureOf(takeDirect(repo.work, '4670', { proc, marks }))).toBeNull();
expect(readLock(repo.work)).toMatchObject({ state: 'held', lock: { pr: '4670', proc: { pid: dead.pid }, expires_at: new Date(T0 + DIRECT_LOCK_TTL_MS).toISOString() } });
const judged = readLock(repo.work).sha;
const next = takeDirect(repo.work, '4671', { proc: { ...self, mark: 'next' }, marks, now: at(1) });
expect(failureOf(next)).toBeNull();
expect(next.lines.join('\n')).toMatch(new RegExp(`^LOCK_TAKEOVER: #4670's land lock \\(holder cox.local:${dead.pid}, phase merging, since .*\\): its land \\(process ${dead.pid}\\) and every process it started have exited$`, 'm'));
const now = readLock(repo.work);
expect(now).toMatchObject({ state: 'held', lock: { pr: '4671', took_over: { pr: '4670', sha: judged, holder: `cox.local:${dead.pid}` } } });
expect(repo.git('log', '-1', '--format=%s', LAND_LOCK_REF)).toContain('(took over #4670: its land');
});
}, 60_000);
test('a lock whose land is still running is refused, --break-lock included, even when the land\'s first process exited and left a child running', async () => {
const self = thisProcess()!;
const repo = bareRepo();
const marks = newMarks();
const live = await group('sleep 60');
const parent = await group('sleep 60 & sleep 0.5; exit 0');
try {
const parentProc = procOf(parent.pid, self);
await parent.exited;
withRepoEnv(repo, () => {
expect(failureOf(takeDirect(repo.work, '4670', { proc: procOf(live.pid, self), marks }))).toBeNull();
for (const breakLock of [false, true]) {
const refused = failureOf(takeDirect(repo.work, '4671', { proc: { ...self, mark: 'b' }, marks, now: at(500), breakLock }));
expect(refused).toMatchObject({ code: 'QUEUE_LAND_LOCKED' });
expect(refused!.message).toContain(`#4670's land is still running: process ${live.pid} of its land`);
expect(refused!.fix).toContain(`stop process ${live.pid}`);
}
live.kill();
writeLock(repo.work, readLock(repo.work).sha, { released: true, by: 'test', at: new Date(T0).toISOString() });
expect(failureOf(takeDirect(repo.work, '4672', { proc: parentProc, marks }))).toBeNull();
const orphaned = failureOf(takeDirect(repo.work, '4673', { proc: { ...self, mark: 'c' }, marks, now: at(1) }));
expect(orphaned!.message).toMatch(/#4672's land is still running: process \d+ of its land/);
expect(orphaned!.message).not.toContain(`process ${parent.pid} of`);
});
} finally { live.kill(); parent.kill(); }
}, 60_000);
test('a lock from another machine is refused until it expires, naming the expiry and the fix, then taken over; one this machine wrote before it restarted is taken over at once', () => {
const self = thisProcess()!;
const repo = bareRepo();
const marks = newMarks();
withRepoEnv(repo, () => {
const elsewhere: LockProcess = { ...self, boot: 'another-boot', pid: 1640, pgid: 1640, start: 1, mark: 'elsewhere' };
expect(failureOf(takeDirect(repo.work, '4670', { proc: elsewhere, marks: newMarks() }))).toBeNull();
const early = failureOf(takeDirect(repo.work, '4671', { proc: { ...self, mark: 'x' }, marks, now: at(89) }));
expect(early).toMatchObject({ code: 'QUEUE_LAND_LOCKED' });
expect(early!.message).toContain('its land ran on another machine, so this one cannot check whether it still runs');
expect(early!.fix).toContain(`the lock expires at ${new Date(T0 + DIRECT_LOCK_TTL_MS).toISOString()} (in 1 min)`);
expect(early!.fix).toContain('--break-lock');
const expired = takeDirect(repo.work, '4671', { proc: { ...self, mark: 'y' }, marks, now: at(90) });
expect(failureOf(expired)).toBeNull();
expect(expired.lines.join('\n')).toContain(`LOCK_TAKEOVER: #4670's land lock (holder cox.local:1640, phase merging, since ${new Date(T0).toISOString()}): it expired at ${new Date(T0 + DIRECT_LOCK_TTL_MS).toISOString()}`);
const before: LockProcess = { ...self, boot: 'before-restart', pid: 1639, pgid: 1639, start: 1, mark: 'restarted' };
writeLock(repo.work, readLock(repo.work).sha, { released: true, by: 'test', at: new Date(T0).toISOString() });
expect(failureOf(takeDirect(repo.work, '4672', { proc: before, marks }))).toBeNull();
expect(fs.existsSync(path.join(marks, 'restarted.json'))).toBe(true);
const rebooted = takeDirect(repo.work, '4673', { proc: { ...self, mark: 'z' }, marks, now: at(1) });
expect(failureOf(rebooted)).toBeNull();
expect(rebooted.lines.join('\n')).toContain('this machine wrote it before it last restarted, so its land is gone');
expect(fs.existsSync(path.join(marks, 'restarted.json'))).toBe(false);
});
}, 60_000);
test('a lock from an older gstack (no process, no expiry) expires 90 minutes after its last phase; a deliberate hold refuses even after it expires', () => {
const self = thisProcess()!;
const repo = bareRepo();
const marks = newMarks();
withRepoEnv(repo, () => {
const old: LandLock = { pr: '4670', holder: 'cox.local:74326', started_at: new Date(T0).toISOString(), phase_at: new Date(T0).toISOString(), phase: 'merging', journal: pin('4') };
writeLock(repo.work, readLock(repo.work).sha, old);
expect(failureOf(takeDirect(repo.work, '4671', { proc: { ...self, mark: 'p' }, marks, now: at(30) }))!.message).toContain('it records no land process (an older gstack wrote it)');
expect(failureOf(takeDirect(repo.work, '4671', { proc: { ...self, mark: 'q' }, marks, now: at(91) }))).toBeNull();
const holder: LockProcess = { ...self, boot: 'held-boot', pid: 77, pgid: 77, start: 1, mark: 'held' };
writeLock(repo.work, readLock(repo.work).sha, { released: true, by: 'test', at: new Date(T0).toISOString() });
const tampered = takeDirect(repo.work, '4672', { proc: holder, marks });
if (!('held' in tampered)) throw new Error('lock not taken');
tampered.held.hold('land.merged_checks changed this checkout\'s git directory while landing #4672');
const refused = failureOf(takeDirect(repo.work, '4673', { proc: { ...self, mark: 'r' }, marks, now: at(500) }));
expect(refused!.message).toContain('its land left it held on purpose: land.merged_checks changed');
expect(refused!.fix).toContain(`gstack-ship-queue land --pr 4672 --pin ${'2'.repeat(40)}`);
});
}, 60_000);
test('a hold set while land.merged_checks run keeps a dead land\'s lock refused; cleared, the lock is taken over', async () => {
const self = thisProcess()!;
const repo = bareRepo();
const marks = newMarks();
const dead = await group('sleep 30');
const proc = procOf(dead.pid, self);
dead.kill();
await dead.exited;
withRepoEnv(repo, () => {
const got = takeDirect(repo.work, '4670', { proc, marks });
if (!('held' in got)) throw new Error('lock not taken');
expect(got.held.hold('land.merged_checks were running when it stopped')).toBe(true);
const refused = failureOf(takeDirect(repo.work, '4671', { proc: { ...self, mark: 'h1' }, marks, now: at(1) }));
expect(refused!.message).toContain('its land left it held on purpose: land.merged_checks were running when it stopped');
expect(refused!.fix).toContain('or pass --break-lock');
const retry = failureOf(takeDirect(repo.work, '4670', { proc: { ...self, mark: 'h0' }, marks, now: at(1) }));
expect(retry!.message).toBe("#4670's earlier land left the lock held on purpose: land.merged_checks were running when it stopped");
expect(failureOf(takeDirect(repo.work, '4670', { proc: { ...self, mark: 'h0' }, marks, now: at(1), releasesHold: true }))).toBeNull();
writeLock(repo.work, readLock(repo.work).sha, { released: true, by: 'test', at: new Date(T0).toISOString() });
const again = takeDirect(repo.work, '4672', { proc, marks });
if (!('held' in again)) throw new Error('lock not taken');
expect(again.held.hold('land.merged_checks were running when it stopped')).toBe(true);
expect(again.held.hold(null)).toBe(true);
expect(failureOf(takeDirect(repo.work, '4671', { proc: { ...self, mark: 'h2' }, marks, now: at(1) }))).toBeNull();
const unwritable = path.join(marks, 'not-a-dir');
fs.writeFileSync(unwritable, '');
writeLock(repo.work, readLock(repo.work).sha, { released: true, by: 'test', at: new Date(T0).toISOString() });
const blind = takeDirect(repo.work, '4673', { proc: { ...self, mark: 'h3' }, marks: unwritable });
if (!('held' in blind)) throw new Error('lock not taken');
expect(blind.held.hold('land.merged_checks were running when it stopped')).toBe(false);
});
}, 60_000);
test('a mark file proves a restart only for this machine and a different boot: another pid namespace or another machine\'s mark is unknown', () => {
const self = thisProcess()!;
const marks = newMarks();
const lock = (proc: LockProcess): LandLock => ({ pr: '4670', holder: 'cox.local:9', started_at: new Date(T0).toISOString(), phase: 'merging', journal: pin('4'), proc });
const otherNs: LockProcess = { ...self, pidns: 'pid:[1]', pid: 9, pgid: 9, start: 1, mark: 'ns' };
const machine = `${os.hostname()}/${(() => { try { return fs.readFileSync('/etc/machine-id', 'utf8').trim(); } catch { return ''; } })()}`;
fs.writeFileSync(path.join(marks, 'ns.json'), JSON.stringify({ machine, boot: self.boot }));
expect(judgeOwner(lock(otherNs), self, marks).state).toBe('unknown');
const heldElsewhere: LockProcess = { ...self, boot: 'their-boot', pid: 9, pgid: 9, start: 1, mark: 'held-elsewhere' };
fs.writeFileSync(path.join(marks, 'held-elsewhere.json'), JSON.stringify({ machine: 'another-host/another-id', boot: 'their-boot', hold: 'land.merged_checks were running when it stopped' }));
expect(judgeOwner(lock(heldElsewhere), self, marks).state).toBe('unknown');
const otherMachine: LockProcess = { ...self, boot: 'their-boot', pid: 9, pgid: 9, start: 1, mark: 'theirs' };
fs.writeFileSync(path.join(marks, 'theirs.json'), JSON.stringify({ machine: 'another-host/another-id', boot: 'their-boot' }));
expect(judgeOwner(lock(otherMachine), self, marks).state).toBe('unknown');
});
test('a rerun in the dead holder\'s own process group is not refused because of its own siblings (a | cat beside it)', () => {
const dir = newMarks();
const judge = path.join(dir, 'judge.ts');
fs.writeFileSync(judge, `import * as fs from 'node:fs';
import { judgeOwner, thisProcess } from ${JSON.stringify(path.resolve(import.meta.dir, '../lib/ship-queue-land-lock.ts'))};
const self = thisProcess()!;
const stat = fs.readFileSync(process.argv[2]!, 'utf8');
const f = stat.slice(stat.lastIndexOf(')') + 2).split(' ');
const proc = { ...self, pid: Number(stat.split(' ')[0]), pgid: Number(f[2]), start: Number(f[19]), mark: 'none' };
console.log(judgeOwner({ pr: '1', holder: 'h', started_at: '', phase: 'merging', journal: 'land-pin:1', proc }, self, process.argv[3]!).state);
`);
const bun = [process.execPath, ...bunIsolationArgs()].map(a => `'${a}'`).join(' ');
const script = `sleep 0.5 & cat /proc/$!/stat > '${dir}/holder.stat'; wait; ${bun} '${judge}' '${dir}/holder.stat' '${dir}' | cat`;
const r = spawnSync('setsid', ['sh', '-c', script], { encoding: 'utf8', timeout: 30_000 });
expect(r.stdout.trim(), r.stderr).toBe('gone');
}, 60_000);
test('a stalled takeover never replaces a newer owner\'s lock, and a taken-over land can neither move nor release the lock it lost', async () => {
const self = thisProcess()!;
const repo = bareRepo();
const other = coordinatorClone(repo);
const marks = newMarks();
const dead = await group('sleep 30');
const deadProc = procOf(dead.pid, self);
dead.kill();
await dead.exited;
withRepoEnv(repo, () => {
const lost = takeDirect(repo.work, '4670', { proc: deadProc, marks });
if (!('held' in lost)) throw new Error('lock not taken');
let fresh = '';
const stalled = takeDirect(repo.work, '4671', {
proc: { ...self, mark: 'stalled' }, marks,
now: () => {
if (!fresh) {
const seen = readLock(other);
const w = writeLock(other, seen.sha, { pr: '4672', holder: 'fresh:1', started_at: new Date(T0).toISOString(), phase: 'merging', journal: pin('7'), proc: { ...self, mark: 'fresh' }, expires_at: new Date(T0 + DIRECT_LOCK_TTL_MS).toISOString() });
expect(w.ok).toBe(true);
fresh = w.sha;
}
return new Date(T0 + 60_000);
},
});
expect(failureOf(stalled)).toMatchObject({ code: 'QUEUE_LAND_LOCKED', message: 'the land lock moved under this run (another lander took it)' });
expect(repo.git('--git-dir', repo.origin, 'rev-parse', LAND_LOCK_REF)).toBe(fresh);
expect(lost.held.set('merging')).toMatchObject({ code: 'QUEUE_LAND_LOCKED' });
expect(lost.held.release()).toBe(false);
expect(readLock(repo.work)).toMatchObject({ state: 'held', lock: { pr: '4672', holder: 'fresh:1' } });
});
}, 60_000);
});
const POLICY = { ...TRAIN_POLICY, release_tool: null, release_outputs: [], changelog_fragments: { dir: 'changelog.d' }, min_gstack_version: '1.91.336.0' };
function stampedTrain(prs = ['1', '2']) {
const fx = trainFixture(POLICY, 'gstack-land-lock-'); fixtures.push(fx); wrapLand(fx);