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

+64
View File
@@ -0,0 +1,64 @@
/**
* `gstack-ship-queue count` (R119): how many of this repository's PRs are finished and waiting to land, cheap enough
* to run at every Captain wake. One gh call (one GraphQL query) reads the oldest 100 open PRs with each one's newest
* commit. A PR is finished when one of the authors opened it (garrytan, capy-ai and garrytan-agents unless --authors
* says otherwise), it targets the base, it is not a draft, GitHub does not report it conflicting, and the check
* rollup on its newest commit is SUCCESS. Its wait runs from that commit's date. More than DRAIN_COUNT finished,
* or one that has waited DRAIN_HOURS or more, prints a DRAIN line: the Captain starts a drain that same turn
* (docs/captain.md#drain-the-queue).
*/
import type { QueueContext, StepOutput } from './ship-queue';
export const DRAIN_COUNT = 6;
export const DRAIN_HOURS = 24;
export const DEFAULT_AUTHORS = ['garrytan', 'capy-ai', 'garrytan-agents'];
const SHOWN = 10;
export const COUNT_QUERY = 'query($owner:String!,$name:String!){repository(owner:$owner,name:$name){pullRequests(states:OPEN,first:100,orderBy:{field:CREATED_AT,direction:ASC}){totalCount nodes{number title isDraft mergeable baseRefName author{login} commits(last:1){nodes{commit{committedDate statusCheckRollup{state}}}}}}}}';
interface PrNode {
number: number; title: string; isDraft: boolean; mergeable: string; baseRefName: string; author: { login: string } | null;
commits: { nodes: Array<{ commit: { committedDate: string; statusCheckRollup: { state: string } | null } }> };
}
export interface FinishedPr { pr: number; title: string; hours: number }
/** A bot's login reads as `capy-ai`, `capy-ai[bot]` or `app/capy-ai` depending on the API; compare the bare name. */
const bare = (login: string) => login.toLowerCase().replace(/^app\//, '').replace(/\[bot\]$/, '');
/** The finished PRs among nodes, oldest wait first. */
export function finishedPrs(nodes: PrNode[], o: { base: string; authors: string[]; now: Date }): FinishedPr[] {
const authors = new Set(o.authors.map(bare));
const out: FinishedPr[] = [];
for (const n of nodes) {
const head = n.commits?.nodes?.[0]?.commit;
if (!head || n.isDraft || n.mergeable === 'CONFLICTING' || n.baseRefName !== o.base || !authors.has(bare(n.author?.login ?? ''))) continue;
if (head.statusCheckRollup?.state !== 'SUCCESS') continue;
const since = Date.parse(head.committedDate);
if (Number.isNaN(since)) continue;
out.push({ pr: n.number, title: n.title, hours: Math.max(0, Math.floor((o.now.getTime() - since) / 3_600_000)) });
}
return out.sort((a, b) => b.hours - a.hours || a.pr - b.pr);
}
export function queueCount(ctx: QueueContext, o: { authors?: string[]; now?: Date } = {}): StepOutput {
const authors = o.authors?.length ? o.authors : DEFAULT_AUTHORS;
const r = ctx.gh(['api', 'graphql', '-F', 'owner={owner}', '-F', 'name={repo}', '-f', `query=${COUNT_QUERY}`]);
let page: { totalCount: number; nodes: PrNode[] } | undefined;
try { page = JSON.parse(r.out)?.data?.repository?.pullRequests; } catch { /* reported below */ }
if (!r.ok || !page || !Array.isArray(page.nodes)) {
return { ok: false, lines: [], run: 'none', failure: { code: 'QUEUE_NOT_READY', message: `gh could not list the open PRs: ${(r.err || r.out).trim().split('\n')[0]?.slice(0, 200) || 'no data'}`, fix: 'check gh auth status and that this checkout\'s origin is the GitHub repository, then rerun count' } };
}
const finished = finishedPrs(page.nodes, { base: ctx.base, authors, now: o.now ?? new Date() });
const old = finished.filter(f => f.hours >= DRAIN_HOURS);
const oldest = finished[0];
const lines = [`QUEUE_COUNT: finished=${finished.length} waited_${DRAIN_HOURS}h=${old.length} oldest=${oldest ? `${oldest.hours}h #${oldest.pr}` : 'none'} open=${page.totalCount} base=${ctx.base} authors=${authors.join(',')}`];
if (page.totalCount > page.nodes.length) lines.push(`QUEUE_COUNT_PARTIAL: counted the oldest ${page.nodes.length} of ${page.totalCount} open PRs`);
for (const f of finished.slice(0, SHOWN)) lines.push(`FINISHED: #${f.pr} ${f.hours}h ${f.title}`);
if (finished.length > SHOWN) lines.push(`FINISHED: and ${finished.length - SHOWN} more`);
const why = [
...(finished.length > DRAIN_COUNT ? [`${finished.length} finished PRs are waiting (more than ${DRAIN_COUNT})`] : []),
...(old.length ? [`${old.length} ${old.length === 1 ? 'has' : 'have'} waited ${DRAIN_HOURS}h or more (oldest #${oldest!.pr}, ${oldest!.hours}h)`] : []),
];
if (why.length) lines.push(`DRAIN: ${why.join(' and ')}; start a drain this turn: land them, bundle them, or send each idle owner its next step (docs/captain.md#drain-the-queue)`);
return { ok: true, lines, run: 'none' };
}
+14 -5
View File
@@ -26,7 +26,7 @@ import { versionAtRef } from './restamp';
import type { ShipPolicy } from './ship-policy';
import { ciAtHead, loadBasePolicy, type QueueContext, type StepOutput } from './ship-queue';
import { git, queueHooksOff, type QueueFailure } from '../crew/lib/ship-queue-gate';
import { acquireLandLock, lockLabel, ownsLock, readLock, type HeldLandLock, type LandLock } from '../crew/lib/ship-queue-land-lock';
import { acquireLandLock, DIRECT_LOCK_TTL_MS, lockLabel, ownsLock, readLock, type HeldLandLock, type LandLock } from '../crew/lib/ship-queue-land-lock';
import * as os from 'node:os';
import { activeFreeze, loadReady, persist, rebuildFromNotes, repoIdentity, type FreezeRecord } from '../crew/lib/ship-queue-train-record';
import { bindShippedReceipt } from './ship-receipt-depth';
@@ -75,6 +75,8 @@ export function verifyPin(ctx: QueueContext, o: { pr: string; pin: string; accep
}
const isSha = (s: string | undefined) => /^[0-9a-f]{40,64}$/.test(s ?? '');
/** The hold a direct land sets while land.merged_checks run detached, outside its process group: a land that stops there needs a person. */
const CHECKS_HOLD = 'land.merged_checks were running when it stopped, so a check may still run and the checkout\'s git directory was never compared';
function view(ctx: QueueContext, pr: string): DirectPr | QueueFailure {
const r = ctx.gh(['pr', 'view', pr, '--json', 'state,baseRefName,headRefName,headRefOid,isCrossRepository,body,title,mergeable,mergeStateStatus,mergeCommit']);
@@ -122,7 +124,7 @@ function havePin(root: string, pr: string, pin: string, branch?: string): boolea
}
/** The merge commit's tree is the pin merged onto the merge commit's first parent: GitHub merged exactly the pin. A read that fails is "cannot verify", never a mismatch. */
function verifyMerge(ctx: QueueContext, root: string, pr: string, pin: string, merge: string, lines: string[], run: string, now: Date, stamped?: string | 'pin-parent', merged: { checks: string[]; on?: string; allow?: boolean } = { checks: [] }): StepOutput {
function verifyMerge(ctx: QueueContext, root: string, pr: string, pin: string, merge: string, lines: string[], run: string, now: Date, stamped?: string | 'pin-parent', merged: { checks: string[]; on?: string; allow?: boolean; hold?: (reason: string | null) => boolean } = { checks: [] }): StepOutput {
const cannot = (why: string): StepOutput => ({ ok: false, run, lines, failure: { code: 'QUEUE_NOT_READY', message: `#${pr} merged as ${merge.slice(0, 12)}, but land cannot verify it: ${why}`, fix: `fix the read named, then rerun gstack-ship-queue land --pr ${pr} --pin ${pin}; it goes straight to the tree check` } });
if (!git(root, ['fetch', '-q', 'origin', ctx.base]).ok) return cannot(`git fetch origin ${ctx.base} failed`);
if (!havePin(root, pr, pin)) return cannot(`the pin ${pin.slice(0, 12)} is not in this clone and refs/pull/${pr}/head did not fetch it`);
@@ -137,7 +139,9 @@ function verifyMerge(ctx: QueueContext, root: string, pr: string, pin: string, m
else if (!merged.checks.length) return unverified('the base policy lists no land.merged_checks to run on it');
else if (!merged.allow) return { ok: false, run, lines, failure: { code: 'REPO_COMMANDS_NOT_ALLOWED', message: `${onto}, so land must run land.merged_checks on the tree that landed; they are repo commands, so nothing ran`, fix: `rerun gstack-ship-queue land --pr ${pr} --pin ${pin} --allow-repo-commands` } };
else {
if (merged.hold && !merged.hold(CHECKS_HOLD)) return cannot('it could not record under the state root that land.merged_checks are about to run (make $GSTACK_STATE_ROOT/ship-queue/land-owners writable)');
const mc = runMergedChecks(root, parent, pin, merged.checks);
if (!changedCheckouts.has(root)) merged.hold?.(null);
if (!mc.ok) return mc.setup ? cannot(mc.detail) : unverified(`land.merged_checks did not pass on it: ${mc.detail}`);
lines.push(`MERGED_CHECKS: pass tree=${mc.tree} base=${parent} (after the merge, on the base GitHub merged onto)`);
}
@@ -176,7 +180,7 @@ export function landDirect(ctx: QueueContext, o: DirectOptions): StepOutput {
const journal = `land-pin:${o.pin}`;
const unlock = `LOCK: warn land.merged_checks changed the checkout's git directory, so land pushed no lock release and the land lock stays held; once every path it names is restored from a trusted copy, release the lock without running the checks: gstack-ship-queue land --pr ${o.pr} --pin ${o.pin} (without --allow-repo-commands)`;
const takeLock = () => acquireLandLock({
root, remote, holder: `${os.hostname()}:${process.pid}`, owner: { pr: o.pr }, journal, now, phase: () => 'merging', breakLock: o.breakLock,
root, remote, holder: `${os.hostname()}:${process.pid}`, owner: { pr: o.pr }, journal, now, phase: () => 'merging', breakLock: o.breakLock, processScoped: { ttlMs: DIRECT_LOCK_TTL_MS }, releasesHold: !o.allowRepoCommands,
takeover: (held: LandLock) => !held.train && held.pr === o.pr && held.journal.startsWith('land-pin:') ? `LOCK: taking over #${o.pr}'s land lock that an earlier land --pin run left (${held.journal}, since ${held.started_at})` : null,
});
if (info.state === 'MERGED' && info.headRefOid === o.pin && isSha(info.mergeCommit?.oid)) {
@@ -272,7 +276,9 @@ export function landDirect(ctx: QueueContext, o: DirectOptions): StepOutput {
if (ci) lines.push(`DRY-RUN: land.merged_ci not dispatched; a real land runs ${ci.workflow} on the pin merged onto ${tip.slice(0, 12)} and waits for it`);
} else if (checks.length && !o.allowRepoCommands) return fail(lines, { code: 'REPO_COMMANDS_NOT_ALLOWED', message: `${moved}, so land must run land.merged_checks on the pin merged onto it; they are repo commands (the merged tree's package.json decides what they run), so nothing ran and nothing merged`, fix: `rerun gstack-ship-queue land --pr ${o.pr} --pin ${o.pin} --allow-repo-commands` });
else {
if (checks.length && held && !held.hold(CHECKS_HOLD)) return fail(lines, { code: 'QUEUE_NOT_READY', message: `${moved}, and land could not record under the state root that land.merged_checks are about to run, so a land that stopped during them would look gone; nothing ran and nothing merged`, fix: 'make $GSTACK_STATE_ROOT/ship-queue/land-owners writable (or free disk space), then rerun land' });
const built = checks.length ? runMergedChecks(root, tip, o.pin, checks) : mergedCommit(root, tip, o.pin);
if (checks.length && !changedCheckouts.has(root)) held?.hold(null);
if (!built.ok) return refuse(built);
if (checks.length) lines.push(`MERGED_CHECKS: pass tree=${built.tree} base=${tip} pin=${o.pin.slice(0, 12)} checks: ${checks.join('; ')}`);
if (ci) {
@@ -311,11 +317,14 @@ export function landDirect(ctx: QueueContext, o: DirectOptions): StepOutput {
}
if (!isSha(merge) && m.ok) return fail(lines, { code: 'QUEUE_NOT_READY', message: `gh pr merge accepted #${o.pr}, but GitHub does not show it merged yet; nothing is verified`, fix: `rerun gstack-ship-queue land --pr ${o.pr} --pin ${o.pin} in a minute; once GitHub shows the merge it goes straight to the tree check` });
if (!isSha(merge)) return fail(lines, { code: 'QUEUE_NOT_READY', message: `gh pr merge refused: ${(m.err ?? m.out).trim().slice(0, 200) || 'the PR is not merged'}`, fix: `read the refusal; rerun land --pr ${o.pr} --pin ${o.pin} once it is fixed` });
return verifyMerge(ctx, root, o.pr, o.pin, merge!, lines, run, now(), release ? mergeBase : undefined, { checks, on: tip, allow: o.allowRepoCommands });
return verifyMerge(ctx, root, o.pr, o.pin, merge!, lines, run, now(), release ? mergeBase : undefined, { checks, on: tip, allow: o.allowRepoCommands, hold: held && (reason => held!.hold(reason)) });
};
let out: StepOutput;
let released = true;
try { out = locked(); } finally { if (held) released = !changedCheckouts.has(root) && held.release(); }
try { out = locked(); } finally {
if (held && changedCheckouts.has(root)) held.hold(`land.merged_checks changed this checkout's git directory while landing #${o.pr}`);
if (held) released = !changedCheckouts.has(root) && held.release();
}
if (changedCheckouts.has(root)) return { ...out, lines: [...out.lines, unlock] };
return released ? out : { ...out, lines: [...out.lines, `LOCK: warn the land lock release push failed, so the lock stays held; rerun gstack-ship-queue land --pr ${o.pr} --pin ${o.pin} to resume and release it`] };
}