mirror of
https://github.com/garrytan/gstack.git
synced 2026-10-31 14:06:50 +01:00
* feat(ship-queue): screens read from the run, train watch/forget/prune --stale, verify --post-merge, ready --dispatch-ci, --stack-tests Operating merge trains on 2026-10-26 showed the gaps this closes. A dispatched screen's verdict is read from the run's gate-binding and report-verdict artifacts (lib/ship-queue-screen.ts) and recorded in the READY receipt: a RED screen refuses ready under either choice and is never re-dispatched at the same merge SHA (QUEUE_SCREEN_RED); a run that concluded success with no slice is NOT-RUN (QUEUE_SCREEN_NOT_RUN), never followed, and counted as unscreened by train build. When refs/pull/<n>/merge lags or is missing the screen is planned on a scratch merge pushed as screen/<pr>-<head12> (#3465); QUEUE_SCREEN_STALE_REF means a real conflict only. train watch --train is the resumable follow command; train prune --stale settles a merging/green train whose PRs all merged or closed elsewhere; train forget removes one record with an exact lease on refs/notes/gstack-trains; verify --post-merge <sha> watches main's push workflows until they settle; ready --dispatch-ci <wf,…> dispatches the free workflows on a conflicting PR, waits, and clears the matching requires-remote pregate lanes with the run URLs; train build --dry-run --stack-tests runs each PR's new test files on its integrated prefix before anything is dispatched (QUEUE_TRAIN_STACK_RED). Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * Regenerate gstack-doctor-components.sh for the four new QUEUE_* result codes The committed Bash render was four case lines behind its generator, so test/doctor-components.test.ts failed atc75ec3a. Output of bun run gen:skill-docs; no hand edits. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * Restore the exact stderr line count in the POLICY_SOURCE_UNAVAILABLE test RH-15: toBeGreaterThan(0) after a toMatch is vacuous; the refusal is one deterministic line, so toBe(1) holds and is restored. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * docs(ship-policy): a lagging merge ref is a scratch merge, not QUEUE_SCREEN_STALE_REF Three passages still described the pre-scratch-merge refusal; they now match screenMerge: a missing or lagging refs/pull/<n>/merge gets a screen/<pr>-<head12> scratch merge, and QUEUE_SCREEN_STALE_REF is a real conflict with the base only. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * stack-tests: drop the dead detail clause and assert the worktree is gone r.detail === 'pass' can never hold when r.ok is false; the dry-run test now checks git worktree list instead of a state-dir name that was never written. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * 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> * verify --post-merge: honour commit statuses in the settled and red verdicts settled and red were computed from check runs only while ci.green also counted commit statuses, so a pending status was called settled and a failed status was reported as 'red: ' with no name. Pending statuses now keep the verdict pending; a red status with no failed check run is named as such. The fixture gains a statuses knob to drive both. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * train watch: reject a malformed --free-timeout as usage instead of taking the default Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * train forget: restore the local notes ref on a rejected lease and refuse every in-flight state forgetNotes removed the note locally before the leased push, so a rejected lease left a local notes ref without the note that the next plain persist pushed without a lease. It now snapshots the ref and restores it on rejection, reporting failed removals separately. forget refused only merging/gate-pending/free-pending; built, pushed, free-green, dispatch-unknown and dispatch-absent are in flight too and are refused the same way. CHANGELOG and ship-policy say so. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * train build --dry-run: never persist or push the read screen verdict admitSlots recorded a verdict that differed from the receipt and pushed refs/notes/gstack-trains before the dryRun branch printed 'nothing written, pushed or dispatched'. It now takes dryRun and skips the persist, matching trainReady's guard. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * stack-tests: only bun test files count as new tests, passed as paths TEST_FILE matched every .ts under test/, so a PR whose only added file was a helper or fixture ran 'bun test test/helpers/x.ts', which exits 1 (the filter matches no test file) and was reported QUEUE_TRAIN_STACK_RED. A bare positional is also a substring filter that picks up unrelated files. The rule is now bun's own (.test, _test_, .spec, _spec_ in the name) and each file is passed as ./<path>, which bun treats as the exact file. The fixture PR 3 now adds a helper and is still 'no new test files'. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * screen: page the run's artifact list before picking the newest binding One page of 100 artifacts can miss gate-binding/report-verdict on a broad or rerun screen (three artifacts per slice plus reruns), which read as 'bound no verdict' and bought a fresh screen. newestArtifact now follows page= until a short page. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * stack-tests: match bun's _test and _spec suffixes too Bun discovers a file whose name ends in .test, _test, .spec or _spec before the extension. The previous rule missed the underscore forms (b_test.ts, e_spec.ts), so a PR adding only such files got no stack check, and it matched names bun ignores. PR 1 in the fixture now adds test/pr-1_test.ts to prove the underscore form is run. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * docs: ship-policy and troubleshooting match the shipped train commands Step 14.5 documentation audit: the stack-tests naming rule is bun's own (.test, _test, .spec, _spec); prune --stale deletes every merged or closed PR's screen/<pr>-* branch; verify --post-merge takes the full SHA and counts commit statuses; train watch --free-timeout and ready --dispatch-ci --timeout defaults; PENDING/UNKNOWN/INCOMPLETE screen verdicts; QUEUE_SCREEN_STALE_REF's Meaning no longer contradicts its Fix; one sentence each for the new QUEUE_NOT_READY, QUEUE_TRAIN_UNTRUSTED and QUEUE_TRAIN_PENDING triggers. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * changelog: prune --stale scope, post-merge commit statuses, STALE_REF vs POLICY_SOURCE_UNAVAILABLE Three details the documentation audit found wrong against the code; the entry stays under [Unreleased] (stamp-at-merge). Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * docs: RED screens are not seated; QUEUE_PROOF_MISSING and POLICY_SOURCE_UNAVAILABLE name the train-command causes Step 14.5 re-audit: ship-policy.md says train build does not seat a RED screen and its NEXT names the build without that PR; troubleshooting.md lists train forget, train watch, verify --screen and the scratch-branch push under QUEUE_PROOF_MISSING, and the origin/<base> fetch under POLICY_SOURCE_UNAVAILABLE. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * test(host-conformance): delete the named scope.md in the C3 broken-link case A test repair the remote gate requires, not a review finding. The case deleted the first .md readdirSync listed under gstack-qa/sections, and on the remote-gate VM's ext4 that was a file setup --status does not count, so the case was deterministically red there (#3479) while green locally and on Actions. The same one-line change ships in #3471 (fd468eb); this copy carries no CHANGELOG line so the two merge cleanly. Refs #3479 Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * v1.91.263.0 stamp Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --------- Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
123 lines
7.6 KiB
TypeScript
123 lines
7.6 KiB
TypeScript
/**
|
|
* GitHub reads and writes for merge trains, all through the queue's `gh`
|
|
* seam (QueueContext.gh) so tests answer them from a fixture: PR identity,
|
|
* workflow runs found by branch or by eval-bg nonce, run artifacts and the
|
|
* full-gate check run. Dispatch itself goes through scripts/eval-bg.ts.
|
|
*/
|
|
import * as fs from 'node:fs';
|
|
import * as os from 'node:os';
|
|
import * as path from 'node:path';
|
|
import type { Exec, RunSummary } from '../scripts/eval-bg';
|
|
import type { QueueContext } from './ship-queue';
|
|
|
|
export interface PrInfo { headRefOid: string; isCrossRepository: boolean; state: string; headRefName: string; body: string }
|
|
export interface RunInfo { databaseId: number; status: string; conclusion: string | null; url: string; headSha: string; event: string; displayTitle: string }
|
|
export interface CheckInfo { status: string; conclusion: string | null }
|
|
|
|
function json<T>(r: { ok: boolean; out: string }): T | null {
|
|
if (!r.ok) return null;
|
|
try { return JSON.parse(r.out) as T; } catch { return null; }
|
|
}
|
|
|
|
/** eval-bg's process seam bound to the queue's gh (only `gh` is ever routed through it). */
|
|
export function execFor(ctx: QueueContext): Exec {
|
|
return (command, args) => {
|
|
if (command !== 'gh') return { status: null, stdout: '' };
|
|
const r = ctx.gh(args);
|
|
return { status: r.ok ? 0 : 1, stdout: r.out };
|
|
};
|
|
}
|
|
|
|
export function repoSlug(ctx: QueueContext): string | null {
|
|
const r = json<{ nameWithOwner?: string }>(ctx.gh(['repo', 'view', '--json', 'nameWithOwner']));
|
|
return r?.nameWithOwner && /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(r.nameWithOwner) ? r.nameWithOwner : null;
|
|
}
|
|
|
|
export function prInfo(ctx: QueueContext, pr: string): PrInfo | { error: string } {
|
|
const r = ctx.gh(['pr', 'view', pr, '--json', 'headRefOid,isCrossRepository,state,headRefName,body']);
|
|
const p = json<Partial<PrInfo>>(r);
|
|
if (!p?.headRefOid) return { error: `cannot read PR #${pr} through gh (${(r.err ?? r.out).trim().slice(0, 120) || 'no headRefOid'})` };
|
|
return { headRefOid: p.headRefOid, isCrossRepository: p.isCrossRepository !== false, state: p.state ?? 'OPEN', headRefName: p.headRefName ?? '', body: p.body ?? '' };
|
|
}
|
|
|
|
const RUN_FIELDS = 'databaseId,status,conclusion,url,headSha,event,displayTitle';
|
|
|
|
/** Newest workflow_dispatch run of a workflow on one branch; null when none; 'unreadable' when gh failed. */
|
|
export function newestBranchRun(ctx: QueueContext, slug: string, workflow: string, branch: string): RunInfo | null | 'unreadable' {
|
|
const runs = json<RunInfo[]>(ctx.gh(['run', 'list', '--repo', slug, '--workflow', workflow, '--branch', branch, '--event', 'workflow_dispatch', '--limit', '20', '--json', RUN_FIELDS]));
|
|
if (!Array.isArray(runs)) return 'unreadable';
|
|
return runs.filter(r => r.event === 'workflow_dispatch').sort((a, b) => b.databaseId - a.databaseId)[0] ?? null;
|
|
}
|
|
|
|
export function runView(ctx: QueueContext, slug: string, id: number): RunInfo | null {
|
|
return json<RunInfo>(ctx.gh(['run', 'view', String(id), '--repo', slug, '--json', RUN_FIELDS]));
|
|
}
|
|
|
|
export function asRun(summary: RunSummary): { id: number; url: string } {
|
|
return { id: summary.databaseId, url: summary.url };
|
|
}
|
|
|
|
/**
|
|
* One JSON artifact of a run: the named file wherever the artifact keeps it
|
|
* (a multi-path upload keeps its directories, so report-verdict-a1 holds
|
|
* paid-report/collector-outcomes.json), else the first .json file at its
|
|
* root; null when it is missing or unreadable.
|
|
*/
|
|
export function downloadJson(ctx: QueueContext, slug: string, run: number, name: string, scratch: string, file?: string): unknown {
|
|
const dir = path.join(scratch, `${run}-${name}`);
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
try {
|
|
if (!ctx.gh(['run', 'download', String(run), '--repo', slug, '-n', name, '-D', dir]).ok) return null;
|
|
const pick = file
|
|
? (fs.readdirSync(dir, { recursive: true }) as string[]).filter(f => path.basename(f) === file).sort((a, b) => a.length - b.length)[0]
|
|
: fs.readdirSync(dir).find(f => f.endsWith('.json'));
|
|
return pick && fs.existsSync(path.join(dir, pick)) ? JSON.parse(fs.readFileSync(path.join(dir, pick), 'utf8')) : null;
|
|
} catch { return null; } finally { fs.rmSync(dir, { recursive: true, force: true }); }
|
|
}
|
|
|
|
/**
|
|
* A finished screen run that planned no cases is not a screen: when the merge
|
|
* ref it was dispatched for was superseded, evals.yml skips plan-slices and
|
|
* concludes success (GSTA-95 on #3431). A screen run uploads no train record,
|
|
* so the evidence is the plan itself: no `paid-plan` artifact, or a manifest
|
|
* with nothing planned. `hollow` carries the reason; `live` is a screen that
|
|
* planned cases or is still running; `unknown` names what could not be read
|
|
* (the caller decides, and says, how it counts).
|
|
*/
|
|
export type ScreenPlan = { kind: 'hollow'; reason: string } | { kind: 'live' } | { kind: 'unknown'; reason: string };
|
|
export function hollowScreen(ctx: QueueContext, slug: string, run: number): ScreenPlan {
|
|
if (!Number.isSafeInteger(run) || run <= 0) return { kind: 'unknown', reason: `screen run id ${String(run).slice(0, 40)} is not a run number` };
|
|
const view = runView(ctx, slug, run);
|
|
if (!view) return { kind: 'unknown', reason: `run ${run} is unreadable` };
|
|
if (view.status !== 'completed') return { kind: 'live' };
|
|
const list = json<{ artifacts?: Array<{ name?: string; expired?: boolean }> }>(ctx.gh(['api', `repos/${slug}/actions/runs/${run}/artifacts?name=${PLAN_ARTIFACT}&per_page=10`]));
|
|
if (!Array.isArray(list?.artifacts)) return { kind: 'unknown', reason: `run ${run}'s artifact list is unreadable` };
|
|
const plan = list.artifacts.find(a => a.name === PLAN_ARTIFACT);
|
|
if (!plan) return { kind: 'hollow', reason: `run ${run} planned no cases (no ${PLAN_ARTIFACT} artifact; concluded ${view.conclusion ?? 'unknown'})` };
|
|
if (plan.expired) return { kind: 'unknown', reason: `run ${run}'s ${PLAN_ARTIFACT} artifact has expired` };
|
|
const scratch = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-screen-'));
|
|
let manifest: { entries?: Array<{ status?: unknown }> } | null;
|
|
try { manifest = downloadJson(ctx, slug, run, PLAN_ARTIFACT, scratch, 'manifest.json') as typeof manifest; } finally { fs.rmSync(scratch, { recursive: true, force: true }); }
|
|
if (!Array.isArray(manifest?.entries)) return { kind: 'unknown', reason: `run ${run}'s ${PLAN_ARTIFACT} manifest.json is unreadable` };
|
|
const planned = manifest.entries.filter(e => e?.status === 'planned').length;
|
|
return planned === 0 ? { kind: 'hollow', reason: `run ${run} planned no cases (an empty plan manifest; concluded ${view.conclusion ?? 'unknown'})` } : { kind: 'live' };
|
|
}
|
|
|
|
/** The plan manifest every evals.yml run that planned anything uploads (plan-slices); a superseded screen never reaches that job. */
|
|
const PLAN_ARTIFACT = 'paid-plan';
|
|
|
|
/** The named check run on a commit (the newest when several); null when absent or unreadable. */
|
|
export function checkRun(ctx: QueueContext, sha: string, name: string): CheckInfo | null {
|
|
const r = json<{ check_runs?: Array<{ name?: string; status?: string; conclusion?: string | null }> }>(
|
|
ctx.gh(['api', `repos/{owner}/{repo}/commits/${sha}/check-runs?check_name=${encodeURIComponent(name)}`]));
|
|
const hit = r?.check_runs?.find(c => c.name === name);
|
|
return hit ? { status: hit.status ?? 'unknown', conclusion: hit.conclusion ?? null } : null;
|
|
}
|
|
|
|
/** Whether a branch exists on the remote (ls-remote, never a fetch). */
|
|
export function remoteBranchSha(git: (args: string[]) => { ok: boolean; out: string }, remote: string, branch: string): string | null {
|
|
const r = git(['ls-remote', remote, `refs/heads/${branch}`]);
|
|
return r.ok ? (r.out.split(/\s/)[0] || null) : null;
|
|
}
|