From 49a0e1dd8bff2b029686c39aa1324af6caaccd95 Mon Sep 17 00:00:00 2001 From: garrytan Date: Mon, 26 Oct 2026 16:12:57 +0000 Subject: [PATCH] ready --dispatch-ci: fail closed on fork or unreadable PRs, surface gh errors, keep screen: after READY dispatchCi now uses prInfo (a fork head or an unreadable PR is refused instead of dispatched), reads gh's error text when the workflow run call fails, and calls runsAt once. The screen: line is inserted after the READY line instead of at index 1, which with --dispatch-ci put it before READY and after the first CI_DISPATCH line. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --- lib/ship-queue-dispatch-ci.ts | 19 +++++++++---------- lib/ship-queue-train-ready.ts | 2 +- test/ship-queue-train-ops.test.ts | 10 +++++++++- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/ship-queue-dispatch-ci.ts b/lib/ship-queue-dispatch-ci.ts index 1267763f7..0c496ab8c 100644 --- a/lib/ship-queue-dispatch-ci.ts +++ b/lib/ship-queue-dispatch-ci.ts @@ -17,7 +17,7 @@ import type { PregateFile } from './headless-artifacts'; import { PREGATE_OUT_REL, clearObligation, readPregateFile, writePregateFile } from './pregate/index'; import type { QueueContext } from './ship-queue'; import type { QueueFailure } from './ship-queue-gate'; -import { execFor, repoSlug, runView } from './ship-queue-train-github'; +import { prInfo, repoSlug, runView } from './ship-queue-train-github'; const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); @@ -32,25 +32,24 @@ function runsAt(ctx: QueueContext, workflow: string, branch: string, head: strin } export async function dispatchCi(ctx: QueueContext, o: DispatchCiOptions): Promise { - const view = ctx.gh(['pr', 'view', o.pr, '--json', 'headRefOid,headRefName,isCrossRepository']); - let info: { headRefOid?: string; headRefName?: string; isCrossRepository?: boolean } = {}; - try { info = view.ok ? JSON.parse(view.out) : {}; } catch {} - if (!info.headRefOid || !info.headRefName) return { lines: [], failure: { code: 'QUEUE_NOT_READY', message: `cannot read PR #${o.pr}'s head and branch through gh (${(view.err ?? view.out).trim().slice(0, 120)})` } }; + const info = prInfo(ctx, o.pr); + if ('error' in info) return { lines: [], failure: { code: 'QUEUE_NOT_READY', message: info.error } }; + if (!info.headRefName) return { lines: [], failure: { code: 'QUEUE_NOT_READY', message: `cannot read PR #${o.pr}'s branch through gh (no headRefName)` } }; if (info.isCrossRepository) return { lines: [], failure: { code: 'QUEUE_TRAIN_UNTRUSTED', message: `#${o.pr}'s head is on a fork; its workflows are not dispatched here (the coordinator decides)` } }; const head = info.headRefOid; const branch = info.headRefName; const slug = repoSlug(ctx); - const exec = execFor(ctx); const deadline = Date.now() + o.timeoutSec * 1000; const lines: string[] = []; const concluded = new Map(); for (const wf of o.workflows) { - const prior = runsAt(ctx, wf, branch, head).find(r => r.status === 'completed'); + const atHead = runsAt(ctx, wf, branch, head); + const prior = atHead.find(r => r.status === 'completed'); if (prior) { lines.push(`CI_DISPATCH: ${wf} not dispatched (run ${prior.databaseId} already concluded ${prior.conclusion ?? 'unknown'} at this head)`); concluded.set(wf, prior); continue; } - const live = runsAt(ctx, wf, branch, head)[0]; + const live = atHead[0]; if (!live) { - const d = exec('gh', ['workflow', 'run', wf, '--ref', branch]); - if (d.status !== 0) return { lines, failure: { code: 'QUEUE_NOT_READY', message: `gh workflow run ${wf} --ref ${branch} failed (${d.stdout.trim().slice(0, 120) || 'no output'})` } }; + const d = ctx.gh(['workflow', 'run', wf, '--ref', branch]); + if (!d.ok) return { lines, failure: { code: 'QUEUE_NOT_READY', message: `gh workflow run ${wf} --ref ${branch} failed (${(d.err ?? d.out).trim().replace(/\s+/g, ' ').slice(0, 120) || 'no output'})` } }; } let run: RunRow | undefined = live; for (;;) { diff --git a/lib/ship-queue-train-ready.ts b/lib/ship-queue-train-ready.ts index fe423e021..736b74809 100644 --- a/lib/ship-queue-train-ready.ts +++ b/lib/ship-queue-train-ready.ts @@ -208,7 +208,7 @@ export async function trainReady(ctx: QueueContext, o: ReadyOptions): Promise l.startsWith('READY ')) + 1, 0, screenLine(choice)); const now = nowIso(); const receipt: ReadyReceipt = { schema: 1, kind: 'ready', repo_key: env.repo.key, pr: o.pr, head, base_sha: baseSha, ready_at: prior?.ready_at ?? now, screen: choice, updated_at: now }; if (o.dryRun) return outOk([...lines, 'DRY-RUN: no READY receipt written, nothing dispatched'], `${Q} ready --pr ${o.pr} --${o.screen}${o.broad ? ' --broad' : ''}`, 'none'); diff --git a/test/ship-queue-train-ops.test.ts b/test/ship-queue-train-ops.test.ts index e6fef30c6..122f6cdc1 100644 --- a/test/ship-queue-train-ops.test.ts +++ b/test/ship-queue-train-ops.test.ts @@ -347,7 +347,7 @@ describe('verify --post-merge', () => { describe('ready --dispatch-ci', () => { // Value: protects=a PR that conflicts with the base (a CHANGELOG-only conflict after every stamp) gets its free workflows dispatched on the branch, waited for, and its requires-remote pregate lanes cleared with the run URLs in one command, replacing the four-step hand procedure; - // fails_when=the dispatch happens on a fork head, a red run clears a lane, a lane whose workflow was not dispatched is cleared, or ready still says ci: none; why_new=the ci-hint was advisory prose; seam=the gh shim's workflow run, run list and check runs; no_claim=GitHub's real dispatch latency + // fails_when=the dispatch happens on a fork head, a red run clears a lane, a lane whose workflow was not dispatched is cleared, ready still says ci: none, or the CI_DISPATCH lines push the screen: line in front of READY (identifiers first); why_new=the ci-hint was advisory prose; seam=the gh shim's workflow run, run list and check runs; no_claim=GitHub's real dispatch latency test('dispatches the named workflows on a conflicting PR, waits for their runs, clears the matching pregate lanes with the run URLs and then READYs; a fork head and a red run are refused', () => { const { fx, head, pregate } = readyFixture(); fx.write(d => { @@ -379,6 +379,14 @@ describe('ready --dispatch-ci', () => { const again = queue(fx, ['ready', '--pr', '1', '--pregate', pregate, '--no-screen', '--dispatch-ci', 'free-tests.yml', '--timeout', '5']); expect(dispatches(fx).length).toBe(2); expect(line(again.out, 'CI_DISPATCH:')).toBe('CI_DISPATCH: free-tests.yml not dispatched (run 100 already concluded success at this head)'); + const cleared = JSON.parse(fs.readFileSync(pregate, 'utf8')); + cleared.requires_remote[2].receipt = 'https://example.invalid/runs/7'; + fs.writeFileSync(pregate, JSON.stringify(cleared)); + const ok = queue(fx, ['ready', '--pr', '1', '--pregate', pregate, '--no-screen', '--dispatch-ci', 'free-tests.yml', '--timeout', '5']); + expect([ok.status, ok.err]).toEqual([EXIT.ok, '']); + const okLines = ok.out.split('\n'); + expect(okLines[0]).toBe('CI_DISPATCH: free-tests.yml not dispatched (run 100 already concluded success at this head)'); + expect(okLines[okLines.findIndex(l => l.startsWith('READY #1 ')) + 1]).toBe('screen: no-screen'); fx.write(d => { d.rules['free-tests.yml'] = { ci: 'free-tests', conclusion: 'failure', headSha: head }; d.ci[head] = []; d.runs.length = 0; }); const red = queue(fx, ['ready', '--pr', '1', '--pregate', pregate, '--no-screen', '--dispatch-ci', 'free-tests.yml', '--timeout', '5']); expect([red.status, line(red.out, 'CI_DISPATCH:')]).toEqual([EXIT.refused, 'CI_DISPATCH: free-tests.yml --ref pr-1 run 102 https://example.invalid/runs/102 failure']);