mirror of
https://github.com/garrytan/gstack.git
synced 2026-12-03 05:13:49 +01:00
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:
1 parent
78ee2966d3
commit
1cb381b048
8 files changed
+201
-34
No files matched your search
@@ -108,7 +108,7 @@ function changedGitPaths(before: Map<string, GitEntry>, after: Map<string, GitEn
|
||||
return [...new Set([...before.keys(), ...after.keys()])].filter(p => !same(before.get(p), after.get(p))).sort();
|
||||
}
|
||||
|
||||
export const LAND_EXPLANATION = '{ merged_checks, docs_only_skip? } or null, read from the base revision only: under merge: direct, when the base moved past a pin\'s merge base, `land --pin` builds the pin merged onto the base tip in a fresh clone outside the checkout (its own .git, a copy of the checkout\'s node_modules) and runs each merged_checks command there (at most 16, 512 characters each, 15 minutes each); it lands only when every one exits 0, and refuses on a conflict (QUEUE_LAND_NOT_CLEAN) or a failure (QUEUE_LAND_MERGED_CHECKS). They are repo commands: land runs them only with --allow-repo-commands (else REPO_COMMANDS_NOT_ALLOWED), never under --dry-run, with PATH and locale as the only environment and a throwaway HOME. null lands a stale base unchecked with a STALE_BASE: warn line. docs_only_skip names merged_checks commands land skips for a pin lib/docs-only.ts calls human docs only (docs/pregate.md#docs-only) and needs min_gstack_version at its own introducing release. merge_commit_paths lists git glob pathspecs (at most 64, 256 characters each, no `..`): a PR that changes one lands as a merge commit instead of a squash, so an eval results PR keeps the commits its run receipts point to (docs/writing-prs.md#the-commit-on-main); it needs min_gstack_version at its own introducing release, and with it merged_checks may be left out. merged_ci { workflow, profile } runs CI on the tree that would land, after any merged_checks pass: land pushes the merged commit to the scratch branch gstack-land/<pr>, dispatches the workflow there, waits up to 45 minutes, and lands only when lib/run-evidence.ts accepts that run under the named run profile of the base\'s .gstack/pregate.json with the merged tree as its tested tree (a red run refuses with QUEUE_LAND_MERGED_CHECKS; a run that cannot start, finish or be proved refuses with QUEUE_NOT_READY); it runs on CI, so it needs no --allow-repo-commands, never runs under --dry-run, and the branch is deleted whatever the outcome; with it merged_checks may be left out, and an older gstack ignores it with a warning. An unknown key under land is a warning, ignored.';
|
||||
export const LAND_EXPLANATION = '{ merged_checks, docs_only_skip? } or null, read from the base revision only: under merge: direct, when the base moved past a pin\'s merge base, `land --pin` builds the pin merged onto the base tip in a fresh clone outside the checkout (its own .git, a copy of the checkout\'s node_modules) and runs each merged_checks command there (at most 16, 512 characters each, 15 minutes each); it lands only when every one exits 0, and refuses on a conflict (QUEUE_LAND_NOT_CLEAN) or a failure (QUEUE_LAND_MERGED_CHECKS). They are repo commands: land runs them only with --allow-repo-commands (else REPO_COMMANDS_NOT_ALLOWED), never under --dry-run, with PATH and locale as the only environment and a throwaway HOME. null lands a stale base unchecked with a STALE_BASE: warn line. docs_only_skip names merged_checks commands land skips for a pin lib/docs-only.ts calls human docs only (docs/pregate.md#docs-only) and needs min_gstack_version at its own introducing release. merge_commit_paths lists git glob pathspecs (at most 64, 256 characters each, no `..`): a PR that changes one lands as a merge commit instead of a squash, so an eval results PR keeps the commits its run receipts point to (docs/writing-prs.md#the-commit-on-main); it needs min_gstack_version at its own introducing release, and with it merged_checks may be left out. merged_ci { workflow, profile } runs CI on the tree that would land, after any merged_checks pass: land pushes the merged commit to the scratch branch gstack-land/<pr>, dispatches the workflow there, waits up to 45 minutes, and lands only when lib/run-evidence.ts accepts that run (an earlier land\'s workflow_dispatch run on gstack-land/<pr> whose head commit has the same merged tree is reused instead of a new dispatch: waited on while it runs, refused when red) under the named run profile of the base\'s .gstack/pregate.json with the merged tree as its tested tree (a red run refuses with QUEUE_LAND_MERGED_CHECKS; a run that cannot start, finish or be proved refuses with QUEUE_NOT_READY); it runs on CI, so it needs no --allow-repo-commands, never runs under --dry-run, and the branch is deleted whatever the outcome; with it merged_checks may be left out, and an older gstack ignores it with a warning. An unknown key under land is a warning, ignored.';
|
||||
|
||||
/** The keys this gstack reads under land; any other is a warning (lib/ship-policy.ts policyWarnings), never an error. */
|
||||
export const LAND_KEYS: readonly string[] = ['merged_checks', 'docs_only_skip', 'merge_commit_paths', 'merged_ci'];
|
||||
|
||||
+54
-21
@@ -3,9 +3,15 @@
|
||||
* merge base, the pin's own pull_request runs never tested the merged tree, and land.merged_checks runs only what land's
|
||||
* machine can afford. So land pushes the merged commit to a scratch branch, gstack-land/<pr>, dispatches the policy's
|
||||
* workflow there, waits for that run, and lands only when lib/run-evidence.ts verifyRun accepts it under the named run
|
||||
* profile from the base's .gstack/pregate.json, with the merged commit as its head and the merged tree as its tested
|
||||
* tree. The scratch branch is deleted whatever the outcome. The run executes on CI runners, so no repo command runs on
|
||||
* land's machine and it needs no --allow-repo-commands.
|
||||
* profile from the base's .gstack/pregate.json, with the run's head as its head and the merged tree as its tested tree.
|
||||
* Each land builds a new merged commit, so before dispatching land looks for a run an earlier land started (any
|
||||
* workflow_dispatch run on gstack-land/<pr>, or one at this merged commit) whose head commit has the merged tree: the
|
||||
* newest one that was not cancelled decides. A finished green run that verifyRun accepts lands with no dispatch, a run
|
||||
* still going is waited on, and a red one refuses; only a finished green run that no longer proves the tree (an expired
|
||||
* artifact, past the profile's max age) falls through to a fresh dispatch. GitHub keeps a run and its record of the
|
||||
* branch after the branch is deleted, and serves its head commit by sha, so the scratch branch is deleted whatever the
|
||||
* outcome. The run executes on CI runners, so no repo command runs on land's machine and it needs no
|
||||
* --allow-repo-commands.
|
||||
*/
|
||||
import { git, type QueueFailure } from '../crew/lib/ship-queue-gate';
|
||||
import { repoSlug } from '../crew/lib/ship-queue-train-github';
|
||||
@@ -23,36 +29,60 @@ export interface CiDeps {
|
||||
dispatch(workflow: string, branch: string): string | null;
|
||||
/** The workflow's workflow_dispatch runs at the commit, newest first. */
|
||||
runsAt(workflow: string, commit: string): ApiRun[];
|
||||
/** The workflow's workflow_dispatch runs on the branch, newest first, including runs on a branch since deleted. */
|
||||
runsOn(workflow: string, branch: string): ApiRun[];
|
||||
run(id: number): ApiRun;
|
||||
verify(id: number): { ok: true; head: string; tree: string } | { ok: false; detail: string };
|
||||
sleep(ms: number): void;
|
||||
now(): number;
|
||||
url(id: number): string;
|
||||
}
|
||||
export type CiResult = { ok: true; line: string } | { ok: false; red: boolean; detail: string };
|
||||
/** `run`: the run the refusal is about: a red run, or one still going that the next land on the same merged tree waits on. */
|
||||
export type CiResult = { ok: true; line: string } | { ok: false; red: boolean; detail: string; run?: number };
|
||||
type CiInput = { pr: string; commit: string; tree: string; ci: MergedCi; timeoutMs?: number };
|
||||
|
||||
/** One dispatch of `ci.workflow` on the merged commit and its verdict; red when the run itself failed, else land could not run or prove it. */
|
||||
export function runMergedCi(deps: CiDeps, o: { pr: string; commit: string; tree: string; ci: MergedCi; timeoutMs?: number }): CiResult {
|
||||
/** The newest workflow_dispatch run of `ci.workflow` on the scratch branch or at the merged commit whose head commit has the merged tree, unless it was cancelled. */
|
||||
function earlierRun(deps: CiDeps, o: CiInput, branch: string): ApiRun | undefined {
|
||||
const runs = [...deps.runsAt(o.ci.workflow, o.commit), ...deps.runsOn(o.ci.workflow, branch).filter(r => r.head_branch === branch)];
|
||||
return runs.filter(r => r.event === 'workflow_dispatch' && r.head_commit?.tree_id === o.tree).sort((a, b) => b.id - a.id).find(r => r.conclusion !== 'cancelled');
|
||||
}
|
||||
|
||||
/** Wait from `started` for `first` (or the run that appears at the merged commit) to finish, then its verdict. */
|
||||
function settle(deps: CiDeps, o: CiInput, branch: string, first: ApiRun | undefined, started: number): CiResult {
|
||||
let run = first;
|
||||
for (;;) {
|
||||
run = run ? deps.run(run.id) : deps.runsAt(o.ci.workflow, o.commit)[0];
|
||||
if (run?.status === 'completed') break;
|
||||
if (deps.now() - started >= (o.timeoutMs ?? CI_TIMEOUT_MS)) {
|
||||
if (!run) return { ok: false, red: false, detail: `no ${o.ci.workflow} run appeared on ${branch} at ${o.commit.slice(0, 12)}` };
|
||||
return { ok: false, red: false, run: run.id, detail: `${o.ci.workflow} run ${deps.url(run.id)} is still ${run.status} after ${Math.round((deps.now() - started) / 60_000)} minutes; it tests the merged tree ${o.tree.slice(0, 12)}, so the next land onto the same base waits on this run instead of dispatching another` };
|
||||
}
|
||||
deps.sleep(pollDelayMs(MIN_POLL_MS));
|
||||
}
|
||||
const at = `${o.ci.workflow} run ${deps.url(run.id)} on the merged tree ${o.tree.slice(0, 12)}`;
|
||||
if (run.conclusion !== 'success') return { ok: false, red: true, run: run.id, detail: `${at} concluded ${run.conclusion ?? 'unknown'}` };
|
||||
const v = deps.verify(run.id);
|
||||
if (!v.ok) return { ok: false, red: false, detail: `${at} passed, but it does not prove the tree under ${o.ci.profile}: ${v.detail}` };
|
||||
if (v.head !== run.head_sha || v.tree !== o.tree) return { ok: false, red: false, detail: `${at} tested ${v.head.slice(0, 12)} (tree ${v.tree.slice(0, 12)}), not the merged commit ${o.commit.slice(0, 12)} or a commit with its tree` };
|
||||
const earlier = run.head_sha === o.commit ? '' : `; run by an earlier land at ${run.head_sha.slice(0, 12)}, not dispatched again`;
|
||||
return { ok: true, line: `MERGED_CI: pass ${at} (profile ${o.ci.profile}${earlier})` };
|
||||
}
|
||||
|
||||
/** The verdict of `ci.workflow` on the merged tree: an earlier land's run on that tree when there is one, else one new dispatch on the merged commit; red when the run itself failed, else land could not run or prove it. */
|
||||
export function runMergedCi(deps: CiDeps, o: CiInput): CiResult {
|
||||
const branch = landBranch(o.pr);
|
||||
const earlier = earlierRun(deps, o, branch);
|
||||
if (earlier) {
|
||||
const r = settle(deps, o, branch, earlier, deps.now());
|
||||
if (r.ok || earlier.status !== 'completed' || earlier.conclusion !== 'success') { deps.remove(branch); return r; }
|
||||
}
|
||||
const pushed = deps.push(o.commit, branch);
|
||||
if (pushed) return { ok: false, red: false, detail: `cannot push the merged commit ${o.commit.slice(0, 12)} to ${branch}: ${pushed}` };
|
||||
try {
|
||||
const started = deps.now();
|
||||
const sent = deps.dispatch(o.ci.workflow, branch);
|
||||
if (sent) return { ok: false, red: false, detail: `gh workflow run ${o.ci.workflow} --ref ${branch} failed: ${sent}` };
|
||||
let run: ApiRun | undefined;
|
||||
for (;;) {
|
||||
run = run ? deps.run(run.id) : deps.runsAt(o.ci.workflow, o.commit)[0];
|
||||
if (run?.status === 'completed') break;
|
||||
if (deps.now() - started >= (o.timeoutMs ?? CI_TIMEOUT_MS)) return { ok: false, red: false, detail: run ? `${o.ci.workflow} run ${deps.url(run.id)} is still ${run.status} after ${Math.round((deps.now() - started) / 60_000)} minutes` : `no ${o.ci.workflow} run appeared on ${branch} at ${o.commit.slice(0, 12)}` };
|
||||
deps.sleep(pollDelayMs(MIN_POLL_MS));
|
||||
}
|
||||
const at = `${o.ci.workflow} run ${deps.url(run.id)} on the merged tree ${o.tree.slice(0, 12)}`;
|
||||
if (run.conclusion !== 'success') return { ok: false, red: true, detail: `${at} concluded ${run.conclusion ?? 'unknown'}` };
|
||||
const v = deps.verify(run.id);
|
||||
if (!v.ok) return { ok: false, red: false, detail: `${at} passed, but it does not prove the tree under ${o.ci.profile}: ${v.detail}` };
|
||||
if (v.head !== o.commit || v.tree !== o.tree) return { ok: false, red: false, detail: `${at} tested ${v.head.slice(0, 12)} (tree ${v.tree.slice(0, 12)}), not the merged commit ${o.commit.slice(0, 12)}` };
|
||||
return { ok: true, line: `MERGED_CI: pass ${at} (profile ${o.ci.profile})` };
|
||||
return settle(deps, o, branch, undefined, started);
|
||||
} finally {
|
||||
deps.remove(branch);
|
||||
}
|
||||
@@ -73,6 +103,7 @@ export function ghCiDeps(ctx: QueueContext, remote: string, tip: string, ci: Mer
|
||||
remove: branch => { git(root, ['push', '-q', remote, `:refs/heads/${branch}`]); },
|
||||
dispatch: (workflow, branch) => { const r = ctx.gh(['workflow', 'run', workflow, '--ref', branch]); return r.ok ? null : (r.err ?? r.out).trim().slice(0, 160) || 'no output'; },
|
||||
runsAt: (workflow, commit) => { try { return api<{ workflow_runs?: ApiRun[] }>(`repos/${slug}/actions/workflows/${workflow}/runs?head_sha=${commit}&event=workflow_dispatch&per_page=20`).workflow_runs ?? []; } catch { return []; } },
|
||||
runsOn: (workflow, branch) => { try { return api<{ workflow_runs?: ApiRun[] }>(`repos/${slug}/actions/workflows/${workflow}/runs?branch=${encodeURIComponent(branch)}&event=workflow_dispatch&per_page=20`).workflow_runs ?? []; } catch { return []; } },
|
||||
run: id => { try { return api<ApiRun>(`repos/${slug}/actions/runs/${id}`); } catch { return { id, run_attempt: 1, status: 'unknown', conclusion: null, event: 'workflow_dispatch', head_sha: '' }; } },
|
||||
verify: id => {
|
||||
const v = verifyRun(evidence, { repo: slug, runRef: String(id), profileName: ci.profile, profile: profile!, now: new Date() });
|
||||
@@ -90,6 +121,8 @@ export function landMergedCi(ctx: QueueContext, o: { remote: string; tip: string
|
||||
if ('error' in deps) return { failure: { code: 'QUEUE_NOT_READY', message: `${o.moved}, and land cannot run land.merged_ci: ${deps.error}; nothing merged`, fix: 'fix what the message names, then rerun land' } };
|
||||
const r = runMergedCi(deps, o);
|
||||
if (r.ok) return { line: r.line };
|
||||
if (r.red) return { failure: { code: 'QUEUE_LAND_MERGED_CHECKS', message: `${o.moved}; on the pin merged onto it, ${r.detail}`, fix: `read the failed job in that run; a flaky case gets one rerun of land; a real failure goes back to the lane, which merges the base into the branch, fixes it, runs /ship and gstack-ship-queue ready --pr ${o.pr} at the new head` } };
|
||||
return { failure: { code: 'QUEUE_NOT_READY', message: `${o.moved}, and land.merged_ci could not run or prove CI on the pin merged onto it: ${r.detail}; nothing merged`, fix: `fix what the message names, then rerun gstack-ship-queue land --pr ${o.pr} --pin ${o.pin}` } };
|
||||
const rerun = `gstack-ship-queue land --pr ${o.pr} --pin ${o.pin}`;
|
||||
if (r.red) return { failure: { code: 'QUEUE_LAND_MERGED_CHECKS', message: `${o.moved}; on the pin merged onto it, ${r.detail}`, fix: `read the failed job in that run; for a flaky case rerun the run once (gh run rerun ${r.run}), then rerun ${rerun}, which waits on its new attempt (a rerun of land alone finds the same red run and refuses again); a real failure goes back to the lane, which merges the base into the branch, fixes it, runs /ship and gstack-ship-queue ready --pr ${o.pr} at the new head` } };
|
||||
if (r.run !== undefined) return { failure: { code: 'QUEUE_NOT_READY', message: `${o.moved}, and land.merged_ci timed out waiting for CI on the pin merged onto it: ${r.detail}; nothing merged`, fix: `rerun ${rerun} at any time: while origin/main stays at ${o.tip.slice(0, 12)} it waits up to 45 more minutes on run ${deps.url(r.run)} (or lands at once when it already passed) instead of dispatching a new one` } };
|
||||
return { failure: { code: 'QUEUE_NOT_READY', message: `${o.moved}, and land.merged_ci could not run or prove CI on the pin merged onto it: ${r.detail}; nothing merged`, fix: `fix what the message names, then rerun ${rerun}` } };
|
||||
}
|
||||
@@ -64,6 +64,7 @@ export interface ApiRun {
|
||||
id: number; run_attempt: number; status: string; conclusion: string | null; event: string; head_sha: string;
|
||||
path?: string; created_at?: string; run_started_at?: string; updated_at?: string;
|
||||
repository?: { full_name: string } | null; head_repository?: { full_name: string } | null; head_branch?: string | null;
|
||||
head_commit?: { tree_id?: string } | null;
|
||||
}
|
||||
export interface ApiStep { number: number; name: string; conclusion: string | null; started_at?: string | null; completed_at?: string | null }
|
||||
export interface ApiJob { id?: number; name: string; conclusion: string | null; started_at?: string | null; completed_at?: string | null; steps?: ApiStep[] }
|
||||
|
||||
Reference in new issue
Block a user