Landing a PR reuses an earlier CI run on the same merged code instead of starting another (GST-3634) (#4760)

When main has moved, `gstack-ship-queue land --pin` now reuses an
earlier CI run on the same merged code: it lands at once on a green run,
waits on one still queued and refuses on a red one, instead of starting
a new run on every try.

Why: GitHub's macOS runner queue has been slower than land's 45-minute
wait, so lands kept timing out while the run they started finished green
unused.

Nothing to do. Rerunning a timed-out land now waits on the run it named.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Review-Finding: p1/specialist-batched#1 reader=testing
Review-Finding: p1/specialist-batched#2 reader=testing
This commit is contained in:
Garry Tanandcapy-ai[bot] authored and GitHub committed 2026-11-24 00:53:39 +00:00
1 parent 78ee2966d3
commit 1cb381b048
8 files changed
+201 -34

No files matched your search

+109 -6
View File
@@ -16,15 +16,16 @@ const COMMIT = 'c'.repeat(40), TREE = 't'.repeat(40);
const CI = { workflow: 'free-tests.yml', profile: 'ci-reuse/free-tests' };
type Calls = string[];
function fake(o: { runs?: ApiRun[][]; verify?: ReturnType<CiDeps['verify']>; push?: string; dispatch?: string } = {}): { deps: CiDeps; calls: Calls } {
function fake(o: { runs?: ApiRun[][]; verify?: ReturnType<CiDeps['verify']>; push?: string; dispatch?: string; earlier?: ApiRun[]; atCommit?: ApiRun[] } = {}): { deps: CiDeps; calls: Calls } {
const calls: Calls = [];
let clock = 0, polls = 0;
let clock = 0, polls = 0, sent = false;
const runs = o.runs ?? [[{ id: 7, run_attempt: 1, status: 'completed', conclusion: 'success', event: 'workflow_dispatch', head_sha: COMMIT }]];
const deps: CiDeps = {
push: (c, b) => { calls.push(`push ${c.slice(0, 4)} ${b}`); return o.push ?? null; },
remove: b => { calls.push(`remove ${b}`); },
dispatch: (w, b) => { calls.push(`dispatch ${w} ${b}`); return o.dispatch ?? null; },
runsAt: () => runs[Math.min(polls++, runs.length - 1)] ?? [],
dispatch: (w, b) => { calls.push(`dispatch ${w} ${b}`); sent = true; return o.dispatch ?? null; },
runsAt: () => (sent ? runs[Math.min(polls++, runs.length - 1)] ?? [] : o.atCommit ?? []),
runsOn: (w, b) => { calls.push(`runsOn ${w} ${b}`); return o.earlier ?? []; },
run: id => { const r = runs[Math.min(polls++, runs.length - 1)]?.[0]; return r ?? { id, run_attempt: 1, status: 'queued', conclusion: null, event: 'workflow_dispatch', head_sha: COMMIT }; },
verify: () => o.verify ?? { ok: true, head: COMMIT, tree: TREE },
sleep: ms => { clock += ms; },
@@ -34,6 +35,7 @@ function fake(o: { runs?: ApiRun[][]; verify?: ReturnType<CiDeps['verify']>; pus
return { deps, calls };
}
const run = (deps: CiDeps, timeoutMs?: number) => runMergedCi(deps, { pr: '42', commit: COMMIT, tree: TREE, ci: CI, timeoutMs });
const noLookup = (calls: Calls) => calls.filter(c => !c.startsWith('runsOn '));
const queued: ApiRun = { id: 7, run_attempt: 1, status: 'in_progress', conclusion: null, event: 'workflow_dispatch', head_sha: COMMIT };
describe('land.merged_ci', () => {
@@ -41,7 +43,7 @@ describe('land.merged_ci', () => {
const { deps, calls } = fake({ runs: [[], [queued], [queued], [{ ...queued, status: 'completed', conclusion: 'success' }]] });
const r = run(deps);
expect(r).toEqual({ ok: true, line: 'MERGED_CI: pass free-tests.yml run https://example.invalid/runs/7 on the merged tree tttttttttttt (profile ci-reuse/free-tests)' });
expect(calls).toEqual(['push cccc gstack-land/42', 'dispatch free-tests.yml gstack-land/42', 'remove gstack-land/42']);
expect(noLookup(calls)).toEqual(['push cccc gstack-land/42', 'dispatch free-tests.yml gstack-land/42', 'remove gstack-land/42']);
});
test('a red run refuses as red, naming its conclusion, and the branch is still deleted', () => {
@@ -74,7 +76,7 @@ describe('land.merged_ci', () => {
expect(sent.calls.at(-1)).toBe('remove gstack-land/42');
const pushed = fake({ push: 'rejected' });
expect(run(pushed.deps)).toMatchObject({ ok: false, red: false });
expect(pushed.calls).toEqual(['push cccc gstack-land/42']);
expect(noLookup(pushed.calls)).toEqual(['push cccc gstack-land/42']);
});
test('the policy key: { workflow, profile } only, a workflow file name and a run profile name; it may stand without merged_checks', () => {
@@ -91,6 +93,107 @@ describe('land.merged_ci', () => {
});
});
describe('land.merged_ci reuses an earlier run on the merged tree (GST-3634)', () => {
const OLD = 'a'.repeat(40), OTHER = 'o'.repeat(40);
const earlier = (over: Partial<ApiRun>): ApiRun => ({ id: 9, run_attempt: 1, status: 'completed', conclusion: 'success', event: 'workflow_dispatch', head_sha: OLD, head_branch: 'gstack-land/42', head_commit: { tree_id: TREE }, ...over });
const provesOld = { ok: true as const, head: OLD, tree: TREE };
test('a finished green run an earlier land dispatched on gstack-land/<pr>, at another commit with the merged tree, lands with no push and no new dispatch', () => {
const green = earlier({});
const { deps, calls } = fake({ earlier: [green], runs: [[green]], verify: provesOld });
const r = run(deps);
expect(r.ok).toBe(true);
expect(r.ok ? r.line : '').toBe('MERGED_CI: pass free-tests.yml run https://example.invalid/runs/9 on the merged tree tttttttttttt (profile ci-reuse/free-tests; run by an earlier land at aaaaaaaaaaaa, not dispatched again)');
expect(calls.some(c => c.startsWith('push ') || c.startsWith('dispatch '))).toBe(false);
expect(calls).toContain('runsOn free-tests.yml gstack-land/42');
});
test('a run still in progress on the merged tree is adopted and waited on, not duplicated', () => {
const going = earlier({ status: 'queued', conclusion: null });
const { deps, calls } = fake({ earlier: [going], runs: [[going], [{ ...going, status: 'in_progress' }], [{ ...going, status: 'completed', conclusion: 'success' }]], verify: provesOld });
expect(run(deps).ok).toBe(true);
expect(calls.some(c => c.startsWith('push ') || c.startsWith('dispatch '))).toBe(false);
});
test('a red run on the merged tree refuses as red with no new dispatch, naming the run', () => {
const red = earlier({ conclusion: 'failure' });
const { deps, calls } = fake({ earlier: [red], runs: [[red]], verify: provesOld });
const r = run(deps);
expect(r).toMatchObject({ ok: false, red: true, run: 9 });
expect(r.ok ? '' : r.detail).toContain('runs/9 on the merged tree tttttttttttt concluded failure');
expect(calls.some(c => c.startsWith('dispatch '))).toBe(false);
});
test('the match is on the tree, never the commit: runs on other trees and cancelled runs are skipped, and the newest live run on the tree decides', () => {
const otherTree = fake({ earlier: [earlier({ id: 11, head_commit: { tree_id: OTHER } })] });
expect(run(otherTree.deps).ok).toBe(true);
expect(otherTree.calls).toContain('dispatch free-tests.yml gstack-land/42');
const cancelledOnly = fake({ earlier: [earlier({ conclusion: 'cancelled' })] });
run(cancelledOnly.deps);
expect(cancelledOnly.calls).toContain('dispatch free-tests.yml gstack-land/42');
const sameCommitOtherTree = fake({ earlier: [earlier({ head_sha: COMMIT, head_commit: { tree_id: OTHER } })] });
run(sameCommitOtherTree.deps);
expect(sameCommitOtherTree.calls).toContain('dispatch free-tests.yml gstack-land/42');
const green = earlier({ id: 45249277829 });
const pr4753 = fake({ earlier: [earlier({ id: 45268196043, head_sha: 'b'.repeat(40), conclusion: 'cancelled' }), green], runs: [[green]], verify: provesOld });
const r = run(pr4753.deps);
expect(r.ok ? r.line : r.detail).toContain('runs/45249277829');
expect(pr4753.calls.some(c => c.startsWith('dispatch '))).toBe(false);
});
test('a finished green run that no longer proves the tree (an expired artifact, past max age) is no reason to refuse: land dispatches a fresh run', () => {
let verified = 0;
const green = earlier({});
const { deps, calls } = fake({ earlier: [green], runs: [[green], [], [queued], [{ ...queued, status: 'completed', conclusion: 'success' }]] });
let post = 0;
const runAt = deps.runsAt;
deps.runsAt = (w, c) => { if (calls.some(x => x.startsWith('dispatch '))) post++; return runAt(w, c); };
deps.verify = () => (verified++ === 0 ? { ok: false, detail: 'run_stale: finished 30 hours ago' } : { ok: true, head: COMMIT, tree: TREE });
expect(run(deps).ok).toBe(true);
expect(calls).toContain('dispatch free-tests.yml gstack-land/42');
expect(post).toBe(2);
});
test('a run found at the merged commit itself counts, with no branch listing needed', () => {
const green = earlier({ head_sha: COMMIT, head_branch: 'gstack-land/42' });
const { deps, calls } = fake({ atCommit: [green], runs: [[green]] });
expect(run(deps).ok).toBe(true);
expect(calls.some(c => c.startsWith('push ') || c.startsWith('dispatch '))).toBe(false);
});
test('an adopted run that finishes green but cannot be proved refuses without dispatching', () => {
const going = earlier({ status: 'in_progress', conclusion: null });
const { deps, calls } = fake({ earlier: [going], runs: [[going], [{ ...going, status: 'completed', conclusion: 'success' }]], verify: { ok: false, detail: 'artifact_missing: tested-tree' } });
const r = run(deps);
expect(r).toMatchObject({ ok: false, red: false });
expect(r.ok ? '' : r.detail).toContain('does not prove the tree');
expect(calls.some(c => c.startsWith('push ') || c.startsWith('dispatch '))).toBe(false);
});
test('an adopted run that times out names that run and does not dispatch', () => {
const going = earlier({ status: 'queued', conclusion: null });
const { deps, calls } = fake({ earlier: [going], runs: [[going]], verify: provesOld });
expect(run(deps, 60_000)).toMatchObject({ ok: false, red: false, run: 9 });
expect(calls.some(c => c.startsWith('push ') || c.startsWith('dispatch '))).toBe(false);
expect(calls.at(-1)).toBe('remove gstack-land/42');
});
test('a timed-out wait names the run the next land adopts, and that land adopts it after the scratch branch was deleted', () => {
const going: ApiRun = { ...queued, id: 77, head_branch: 'gstack-land/42', head_commit: { tree_id: TREE } };
const first = fake({ runs: [[going]] });
const r1 = run(first.deps, 60_000);
expect(r1).toMatchObject({ ok: false, red: false, run: 77 });
expect(r1.ok ? '' : r1.detail).toContain('runs/77 is still in_progress after');
expect(r1.ok ? '' : r1.detail).toContain('the next land onto the same base waits on this run instead of dispatching another');
expect(first.calls.at(-1)).toBe('remove gstack-land/42');
const done = { ...going, status: 'completed', conclusion: 'success' };
const second = fake({ earlier: [done], runs: [[done]] });
const r2 = run(second.deps);
expect(r2.ok ? r2.line : r2.detail).toContain('runs/77 on the merged tree tttttttttttt');
expect(second.calls.some(c => c.startsWith('push ') || c.startsWith('dispatch '))).toBe(false);
});
});
describe('queue git hooks', () => {
test.skipIf(process.platform === 'win32')('a queue git call runs the checkout\'s hooks until land turns them off, so wave and train pushes keep the managed pre-push guard', () => {
const gate = JSON.stringify(path.join(import.meta.dir, '..', 'crew', 'lib', 'ship-queue-gate.ts'));