diff --git a/AGENTS.md b/AGENTS.md index 8a9317a..a8f270b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -6,13 +6,16 @@ Phosphor is a Kotlin Multiplatform rendering library that translates cognitive s ### Build & Test ```bash -./gradlew build # Build the project -./gradlew allTests # Run all tests -./gradlew jvmTest # Run JVM-specific tests (fastest, primary gate) +./gradlew :phosphor-lumos:jvmTest --tests "*OrbStepper*" # The local pass: one module, narrowed (see Testing) +./gradlew jvmTest # Every module's JVM tests (CI's JVM job runs this) ./gradlew iosSimulatorArm64Test # Run iOS tests (Apple Silicon simulator) -./gradlew wasmJsBrowserTest # Run WASM browser tests +./gradlew jsTest wasmJsTest # JS and Wasm browser tests (CI's Web job runs this) +./gradlew build # Everything; not part of the loop ``` +The whole-repo commands are what CI runs on every push. Locally, run one narrow pass at +the end of the work (see "Testing" and "Ticket Delivery Loop"). + ### Code Quality ```bash ./gradlew ktlintFormat # Auto-format code @@ -34,6 +37,12 @@ unreleased version, so the number to release is the one already in `gradle.properties` and the tag goes on a commit from before the next bump. Never bump to release. +Agents never push a tag, and never run `scripts/bump-version.sh` unless the ticket +*is* the bump. Don't run `publishToMavenLocal` unless the ticket asks for it: Socket's +build resolves `mavenLocal()` on this Mac, so a local publish silently changes what +every Socket workspace compiles against. A ticket that needs a release is a reason to +ask (see "When you need Miley"). + ## Changelog There is no `CHANGELOG.md`. Do not create one, and do not add a changelog entry @@ -80,6 +89,215 @@ and every PR after it inherited the failure. dependencies left. The task deletes the whole file in that case, and the remaining `format-util` entry still failed CI. +## Ticket Delivery Loop + +Tickets are worked by many agents at once, each in its own Conductor workspace, and Miley is not +watching any one of them. So an agent carries its ticket all the way to a green PR, and speaks up +only when a person is actually needed. If you were started some other way (a plain CLI session) +and your prompt says otherwise, the prompt wins. + +**You are finished** when there is a PR against `main` with all four CI checks green +(`JVM, Android, Lint`, `Web (JS, Wasm)`, `Dokka`, `iOS Compile And Test`). Until then, the only +thing that pauses the loop is a question to Miley, asked with Conductor's question tool (see +"When you need Miley"). Ending your turn any other way (CI still running, a red check you haven't +read, a question left in your closing message) strands the ticket, because nobody else is +watching it. + +Recon and spike tickets end differently. They have no PR, and their write-up goes on the Linear +ticket as comments, never into the repo (drafting it in `.context/` first is fine). They are +finished when the findings are posted and the ticket's STOP-gate question, if it has one, is +asked. `PHO-*` tickets and their comments sync to the public GitHub repo, so keep anything +private to Socket out of them. + +### The loop + +1. **Read the ticket.** It lives on Linear as `PHO-`. Its "synced to a corresponding GitHub + issue" line gives the GitHub number that commits and the PR need. Read the files you will + touch and their tests (see "Before You Change Anything"). +2. **Do the work, with tests, without building.** Builds are the scarce resource when a dozen + agents share one machine. Check your work by reading code, and build once, at the end. +3. **Validate once, narrowly.** Run `./gradlew ::ktlintFormat` for each module you + touched, plus the narrowest test task that covers the change (see "Testing"). If you changed a + dependency, regenerate `kotlin-js-store/` too (see "Dependencies and the Yarn Lockfiles"). CI + runs every target on every push. The local pass is there to catch the obvious before a + round trip, not to prove every target. If the machine refuses a local build for lack of + memory, don't retry and don't work around it. Push, let CI verify, and say so under + Verification in the PR. +4. **Commit and push** the workspace branch, following "Commit and PR conventions". +5. **Open the PR** with `gh pr create --base main`, ready for review rather than draft. If + `gh pr create` errors or hangs, run `gh pr list --head "$(git branch --show-current)"` before + you retry. GitHub sometimes creates the PR and then drops the connection, and a retry opens a + duplicate. +6. **Wait on CI** with `scripts/pr-checks.sh`. Run it in the foreground (in Claude Code, with the + Bash tool's `timeout` at its `600000` maximum). It polls quietly, returns within nine minutes, + and its exit code says what to do next: + + | Exit | Meaning | Next | + |---|---|---| + | `0` | Every required check passed, and every other check has finished | Read any red it lists as "not required", then step 8 | + | `1` | A required check is red, or can no longer run | Step 7 | + | `2` | No open PR for this branch, or a `gh` error | Step 5, or read the error | + | `3` | The PR conflicts with `main`, so CI cannot run | Rebase (below), then step 6 | + | `8` | Still running | Run it again | + + The required set is read from GitHub: today that is the four checks above, from the + "Protect Main" ruleset. Don't end your turn while the script returns `8`. Spend the wait on + work that doesn't depend on the result, like the PR body or the visual evidence. +7. **Fix and go again.** Read the failed job's log with + `gh api repos/{owner}/{repo}/actions/jobs//logs --allow-escape-sequences` (the script + prints the exact command). `gh run view --log-failed` refuses until the whole run has finished, + and the script reports a red as soon as it happens. Decide whether the failure is your diff, + `main`, or the infrastructure (see "Reading a red check"). Fix it, re-run the narrow local + check that covers the fix, push, and go back to step 6. A push re-runs every job, so don't + also re-run jobs by hand. +8. **Finish.** If the work changed along the way, update the PR body to match. If the ticket's + acceptance is visual, ask for the verdict now (see "Visual evidence"). Then close with a short + message: the PR link, the script's verdict line, what was verified locally and what only in + CI, and anything a person still owes. Don't merge; Miley merges. The ruleset also blocks a + merge while a review thread is unresolved, so answer review comments rather than leaving them. + If you are asked to merge, use `gh pr merge --squash`, which keeps `main`'s one-commit-per-PR + history. + +**Rebasing.** When `main` moves and the PR conflicts, run +`git fetch origin main && git rebase origin/main`. Use `origin/main`, never the local `main`, +which is shared across workspaces and stale. Resolve the conflicts, and check that +`git diff --check origin/main` reports no leftover conflict markers. If the conflict is in +`kotlin-js-store/`, don't merge the lockfiles by hand: take `origin/main`'s and regenerate. If it +is in `docs/lumos/llms.txt` or `skills/lumos/`, take `origin/main`'s and regenerate with +`./gradlew :phosphor-lumos:generateAgentDocs`. Re-run the narrow local check, then +`git push --force-with-lease`. + +**Know when to stop pushing.** If the same required check is still red after three fix attempts, +or it is red and you can't tie it to your diff or to a known infrastructure failure, stop and ask +(see "When you need Miley"). + +### Visual evidence + +Unit tests can't see motion, colour or a seam, so a change to palette, projection, particle, +form, look or surface code comes with evidence a person can judge: a preview the `jvmTest` +harness records (under `build/lumos-previews/` or `build/lumos-surface*/`), a golden render, or a +clip of a demo such as `LookDemo`. Produce it as part of the work and save it under `.context/`. +Don't commit it: binary test assets need approval (see "Safety Boundaries"). Name it in the PR +under Verification, and attach it to the PR or the ticket where you can. + +When the ticket's acceptance is visual ("pick dense or fade from the clips", a look that has to +read right), the verdict is a step reserved for a person. Don't decide it yourself, and don't hold +the PR for it. Get the PR green, then ask Miley once, with the PR link and the evidence's path, +and your recommendation first. + +### Reading a red check + +Not every red is the diff. Before you change code for a failure, check whether `main` shows the +same red (`gh run list --workflow ci.yml --branch main --limit 3`). If it does, the break isn't +yours. Rebase if a newer `main` commit fixes it. Otherwise don't fix it inside your PR: say so in +the PR and ask Miley, who may already have it in hand. Then compare the failure with the table +below. Re-run an infrastructure failure once: `gh run rerun --failed`, or +`--job ` for a single job. GitHub refuses both while any job in the run is still going. A +second identical failure is no longer a flake. Never edit code to make an infrastructure failure +go away, and never re-run a code failure hoping it passes. + +Here infrastructure failures are rare. In the 50 failed `ci.yml` runs up to 0.10.0, every red +job was code, the branch's own or inherited from a red `main`. The two flaky signatures were +timeouts in the tests themselves, and both are fixed. So read every red as code until its log +says otherwise. Nine of those PRs were merged on a red check, and each one broke `main` for every +branch after it. + +| What you see | Job | What it is | What to do | +|---|---|---|---| +| `Name contains illegal characters: ","` from `compileTestKotlinIosSimulatorArm64` | iOS | Code. A backtick test name Kotlin/Native rejects (see "Testing"); the JVM accepts it. The most common red here | Rename the test. Before pushing, grep new `commonTest` backtick names for `[,.;:/\\<>\[\]]` | +| `Couldn't resolve link for …`, then `❌ Dokka link resolution warnings found:` | Dokka | Code. A KDoc `[link]` to a symbol Dokka can't see (a parameter, a private member, an unimported name) | Fix the link, or make it a code span. Never a flake | +| ``Lock file was changed. Run the `kotlinUpgradeYarnLock` task`` at `:kotlinStoreYarnLock` (or `:kotlinWasmStoreYarnLock`) | Web | Code. `kotlin-js-store/` is out of date | If `main` is green, your dependency change caused it: regenerate and commit the lockfiles (see "Dependencies and the Yarn Lockfiles"). If `main` is red the same way, don't regenerate on your branch; it gets fixed on `main` (#168), then you rebase | +| `KtLint found code style violations`, often `Exceeded max line length (120) (cannot be auto-corrected)` | JVM, Android, Lint | Code | Run `ktlintFormat`, then wrap the lines it can't fix by hand. Run `ktlintCheck` before you push again; it is cheap | +| `e: …` (`Argument type mismatch`, `'val' cannot be reassigned`, …) in every job at once | All four | Code. Usually a signature that changed under a dependent module, often after a rebase | Fix it, then run the narrow test of every module that depends on the one you changed | +| A JS test fails with `AssertionError` or `Expected <…>` on an exact `Float` result, and passes on JVM and Wasm | Web | Code. Kotlin/JS `Float` is a double at runtime | Compare with a tolerance, or compute in `Double` | +| `The bundle is N KB gzipped, over the M KB budget` | Web | Code. `:phosphor-lumos-dom` grew past `checkBundleSize` | Shrink what reaches the bundle. Raising the budget is Miley's call, so ask | +| A browser test `… [js, browser, ChromeHeadless…] FAILED` (or `wasmJs`), followed by a bare `Error` with no assertion text | Web | A mocha timeout, not an assertion. Was a flake in `FormCatalogueTest` and `SignalGeneratorTest`, fixed by `karma.config.d/mocha-timeout.js` | Re-run once if the module has `karma.config.d/mocha-timeout.js` and the test is untouched by your diff. If the module has none, or your diff made the test slower, add the config or speed the test up. `useMocha { timeout }` does nothing on Wasm | +| A job runs far past its timeout and ends `##[error]The operation was canceled.`, the last task line `> Task :phosphor-lumos-compose:jvmTest` (or `:phosphor-lumos-surface:jvmTest`) | JVM, Android, Lint | Code. A Compose test deadlocked (#135) | Drive the scene with `SteppedScene`, not an `ImageComposeScene` waiting on a `delay`. Don't re-run | +| Jobs "fail" within seconds with zero steps (`pr-checks.sh` prints `never started`), every workflow at once, `main` included | Any | Not seen here yet: an Actions billing block, or no runner | Only Miley can fix billing, so ask. Re-run once it is fixed | + +To grep a finished job's log, even while the rest of the run is going, use +`gh api repos/{owner}/{repo}/actions/jobs//logs --allow-escape-sequences | grep -nE 'FAILED|What went wrong|^e: |##\[error\]'`. + +### When you need Miley + +Ask with Conductor's question tool: `AskUserQuestion` from the Conductor MCP server +(`mcp__conductor__AskUserQuestion` in Claude Code). It notifies Miley and holds your session until +the answer arrives. A question left in your closing message is easy to miss among a dozen +workspaces that all look finished. Without a Conductor tool (a plain CLI session), put the +question first in your closing message and stop. + +**Ask when:** +- the ticket is ambiguous or contradicts the code, and the readings lead to materially different + work; +- the step is destructive, or involves credentials, signing keys, billing, or legal or privacy + risk; +- something only Miley can change is in the way: Actions billing, the "Protect Main" ruleset, + repository secrets, the GPG signing key or Maven Central credentials, or a release (pushing a + `v*` tag, or a version bump the ticket isn't); +- the ticket reserves a step for a person, such as a visual verdict, a STOP gate, or a review it + names Miley for. If that step doesn't block the PR, ask once the PR is green, so the question + can carry the PR link; +- the loop is stuck: the same required check is red after three fix attempts, or a red you can't + attribute. + +**Don't ask** for permission to run the loop (committing, pushing, opening the PR, fixing CI, +re-running an infrastructure failure). Don't ask Miley to choose between options when one is +clearly better: pick it, and record the choice and the reason under "Deviations from the ticket" +in the PR. And don't ask whether there's anything else to do. + +**How to ask:** +- Make each question stand alone. Miley may be answering from another workspace with none of your + context. State what you found in a sentence or two, with the `file:line` or the job link, and + what each answer would lead you to do. +- Offer the options as choices, with your recommendation first and labelled "(Recommended)". +- Put every open decision into one call (the tool takes up to four questions) rather than asking + them one at a time. +- Before you ask, finish and push everything that doesn't depend on the answer, so the question + is the only thing left. + +### Commit and PR conventions + +These apply from the first commit, because a pushed commit can't be fixed without a force-push. +Conductor's own create-PR prompt only fires on its button, so an agent that opens its PR with +`gh` never sees it. These rules are the ones to follow. + +- **Titles** carry both ticket IDs, then the task label the ticket gives, if any: + `PHO-/# : `, for example + `PHO-67/#123 T15 · A: Avatar — a face as a GazingForm, …`. Commits and the PR use the same + shape. A ticket with no synced GitHub issue drops the `/#`. +- **Commit and PR bodies** end with `Authored by Claude (), not by the committer.` (or your + own agent's name), followed by any attribution trailer your harness adds. Every commit's git + author is Miley, so the body is the only place that shows who wrote it. +- **The PR body** opens with `Closes #` when the PR completes the issue, or + `Refs #` when it doesn't. Then it covers what was wrong, the change, deviations from the + ticket and why, and Verification: what ran locally, what was left to CI and why, the visual + evidence and where it is, and anything a person still owes (a visual verdict, say). +- No changelog entry, in the PR or anywhere else (see "Changelog"). + +Work without a ticket drops the IDs and keeps the rest. + +## Git Worktree Hazards (Conductor) + +This repo is commonly checked out as multiple Conductor workspaces, which are +git worktrees sharing a single `.git` directory. Some refs and state are +**not** per-worktree and collide across concurrently running workspaces: + +- **Never use `git stash`.** `refs/stash` is a single shared ref across all + worktrees of the same repo. A concurrent stash from another workspace can + interleave with yours and mix unrelated changes. Use `git commit` to a + throwaway/WIP commit (and `git reset --soft HEAD^` to undo) instead of + stashing. +- Be cautious with other shared, non-worktree-local git state (e.g. global + config, hooks, `git gc`/`git prune` — these operate on the shared + `.git` object store) when other workspaces may be active concurrently. +- Conductor names your branch, and may rename it mid-session. That is expected. + Keep working on whatever branch is checked out, and don't rename it yourself. +- A workspace is cut from `origin/main` once and then drifts as other agents' + PRs merge. Answer "is X on main?" from `origin/main` (`git fetch origin main`, + then `git grep origin/main`), not from your working tree. +- Act only on your own branch, PR, workflow runs and Actions caches. Other + workspaces' are someone else's work in progress. + ## Architecture at a Glance Phosphor is a layered rendering pipeline: cognitive signals flow in, visible characters flow out. Each layer depends only on the layer below it — signal code never imports from palette, field code never imports from render. @@ -147,7 +365,7 @@ one-line meaning in `LumosCatalog`, then a regenerate. The examples they print a - Read the file you're modifying. Read its tests. - Understand the layer boundary you're in. The pipeline flows strictly downward: Signal → Field → Palette → Render → Surface Adapter. -- If touching particle physics (`field/`), run the demo to visually verify — unit tests alone can't catch motion regressions. +- If touching particle physics (`field/`), palette or projection code, produce visual evidence — unit tests alone can't catch motion regressions. See "Visual evidence" under "Ticket Delivery Loop". - If adding a new EmitterEffect, follow the sealed class pattern in `emitter/EmitterEffect.kt`. ## Kotlin Conventions @@ -170,14 +388,52 @@ one-line meaning in `LumosCatalog`, then a regenerate. The examples they print a ## Testing -- **Primary gate:** `./gradlew jvmTest` — run this after every change +CI is the full gate: its four jobs run every target on every push. Locally, run **one** narrow +pass when the work is done, not after every change. Many agents share a 16 GB Mac, and a +whole-repo `jvmTest` in every workspace after every edit is what stalls it. + +**Where tests live.** `commonTest` runs on every target the module has, so a test there is also +run by the Web and iOS jobs. `jvmTest` holds what needs the JVM: the Compose scene and Skia pixel +harnesses in `:phosphor-lumos-compose` and `:phosphor-lumos-surface`, the previews they record +under `build/lumos-previews/` and `build/lumos-surface*/`, and `AgentDocsTest` in +`:phosphor-lumos-cli`. `:phosphor-trace` also has `jsTest`, `webTest` and `iosTest`. + +| Module | Narrow local task | Also run in CI by | +|---|---|---| +| `:phosphor-core` | `:phosphor-core:jvmTest` | JVM, Web, iOS; Android compile | +| `:phosphor-lumos` | `:phosphor-lumos:jvmTest` | JVM, Web, iOS; Android compile | +| `:phosphor-lumos-cli` | `:phosphor-lumos-cli:jvmTest` (JVM is its only target) | JVM | +| `:phosphor-lumos-compose` | `:phosphor-lumos-compose:jvmTest` | JVM, Web, iOS; Android compile | +| `:phosphor-lumos-surface` | `:phosphor-lumos-surface:jvmTest` | JVM, Web, iOS; Android compile | +| `:phosphor-lumos-dom` | `:phosphor-lumos-dom:jsTest`, plus `:phosphor-lumos-dom:checkBundleSize` if the bundle can grow | Web | +| `:phosphor-trace` | `:phosphor-trace:jvmTest` | JVM, Web, iOS | + +**Narrowing.** Add `--tests "*FooTest*"` when you know which tests cover the change, and +`--offline` when dependencies haven't changed. The traps: + +- `:phosphor-lumos-dom` has no JVM target, so `jvmTest` silently skips it. Run its `jsTest`. +- A `--tests` filter that matches nothing in a module fails that module's task. Give each module + its own filter, in separate invocations if need be. +- A narrow JVM run proves nothing about the other targets. These only fail in CI: + - backtick test names in `commonTest` must also compile for Kotlin/Native, which rejects some + characters that the JVM accepts. Leave out `, . ; : / \ < > [ ]` (use `—` to separate + clauses). Only the iOS job catches this (#165); + - Kotlin/JS `Float` is a double at runtime, so a test asserting an exact `Float` result can pass + on JVM and Wasm and fail on JS. Compare with a tolerance; + - Dokka fails on any `Couldn't resolve link` in KDoc, which no test task checks. +- Compose tests in `jvmTest` drive their scene with `SteppedScene`. An `ImageComposeScene` left + waiting on a `delay` deadlocked `:phosphor-lumos-compose:jvmTest` for an hour at a time (#135). + +**What to test:** + - Test luminance mapping boundaries: `charForLuminance(0.0f)` returns space, `charForLuminance(1.0f)` returns the brightest character - Test color ramp interpolation at 0.0, 0.5, and 1.0 - Test particle spawn/decay lifecycle to completion - Test 3D projection against known camera→screen coordinate pairs - Visual regression tests: render a known CellBuffer to string and compare against golden files in `testdata/` -- Backtick test names in `commonTest` must also compile for Kotlin/Native, which rejects some characters that the JVM accepts. Leave out `, . ; : / \ < > [ ]` (use `—` to separate clauses). `jvmTest` won't catch this. Only the iOS job will (#165). -- `jvmTest` is the fast local gate, but it isn't the whole check. All four CI jobs (JVM, Web, iOS, Dokka) are required to merge into `main` + +All four CI jobs (`JVM, Android, Lint`, `Web (JS, Wasm)`, `Dokka`, `iOS Compile And Test`) must +be green before a PR merges into `main`. ## Safety Boundaries @@ -189,14 +445,18 @@ one-line meaning in `LumosCatalog`, then a regenerate. The examples they print a - **Never** change a dependency without regenerating and committing `kotlin-js-store/` — see [Dependencies and the Yarn Lockfiles](#dependencies-and-the-yarn-lockfiles) - **Never** merge on a red Web job that failed on `kotlinStoreYarnLock` / `kotlinWasmStoreYarnLock`; it is not a flake - **Never** push a `v*` tag without `scripts/release-preflight.sh` passing on that exact commit — Maven Central is permanent -- **Always** run `./gradlew jvmTest` before considering work complete -- **Always** run `./gradlew ktlintFormat` before committing -- **Always** verify demo output visually when changing palette or projection code +- **Never** push a `v*` tag, bump the version unless the ticket is the bump, or run `publishToMavenLocal` unless the ticket asks — see [Release](#release) +- **Never** merge your own PR; Miley merges +- **Always** run one narrow local test pass before pushing, and consider work complete only when every CI check is green — see [Testing](#testing) and [Ticket Delivery Loop](#ticket-delivery-loop) +- **Always** run `./gradlew ktlintFormat` (or `::ktlintFormat` per module touched) before committing +- **Always** produce visual evidence when changing palette, projection or particle code — see "Visual evidence" ## Permissions - **File read:** any file in the repository - **File write:** `src/`, `test/`, `docs/`, config files -- **Git:** branch creation, commits (with review) -- **Build/test:** full access to `./gradlew` commands -- **External:** no network requests — this is an offline rendering library +- **Git:** branch creation, commits, pushing the workspace branch (`--force-with-lease` after a rebase), and opening and updating that branch's PR +- **Build/test:** `./gradlew` commands, within the one-narrow-pass budget in [Testing](#testing) +- **GitHub:** `gh` for your own branch, PR, workflow runs (read logs, re-run an infrastructure failure) and Actions caches. Not other workspaces' PRs, branch protection, workflow files, secrets or releases +- **Linear:** read the ticket being worked, and comment on it where the ticket asks +- **Library code:** no network requests — this is an offline rendering library. That rule is about what the library does at runtime, not about the agent's tools above diff --git a/scripts/pr-checks.sh b/scripts/pr-checks.sh new file mode 100755 index 0000000..c100b29 --- /dev/null +++ b/scripts/pr-checks.sh @@ -0,0 +1,249 @@ +#!/usr/bin/env bash +# Wait on a PR's CI and report only what needs attention. +# +# Built for agents. `gh pr checks --watch` reprints its whole table every interval and cannot +# outlast the Bash tool's 10-minute cap, so this polls quietly inside a time budget and prints one +# short report. Run it after every push and act on the exit code (AGENTS.md, "Ticket Delivery Loop"). +# +# Usage: scripts/pr-checks.sh [--budget ] [--fail-on required|any] [] +# +# --budget seconds to wait before giving up with exit 8 (default 540, under the 10-minute cap) +# --fail-on which red check ends the wait early: a `required` one, or `any` (default: required) +# defaults to the PR for the current branch +# +# Exit codes: +# 0 every required check passed or skipped, and every other check has finished +# 1 a check that --fail-on covers failed, was cancelled, or can no longer run; reported as soon +# as it happens +# 2 usage error, gh error, or no open PR +# 3 the PR conflicts with its base branch, so CI cannot run until you rebase +# 8 still pending when the budget ran out; run it again +# +# Required checks are read from the base branch's protection rule, so a check added to the rule +# is waited on without editing this script. With no rule to read, every check counts as required. + +set -uo pipefail + +budget=540 +interval=30 +# main has no protection rule, so every check counts as required (see below) and `required` +# already ends the wait on the first red. +fail_on=required +# How long to wait for a fresh push to become the PR head before reading the old head's checks. +push_grace=90 +target="" + +usage() { + awk 'NR == 1 { next } /^#/ { sub(/^# ?/, ""); print; next } { exit }' "$0" +} + +while [ $# -gt 0 ]; do + case "$1" in + --budget) + [ $# -ge 2 ] || { usage >&2; exit 2; } + budget="$2" + shift 2 + ;; + --fail-on) + [ $# -ge 2 ] || { usage >&2; exit 2; } + fail_on="$2" + shift 2 + ;; + -h | --help) + usage + exit 0 + ;; + -*) + usage >&2 + exit 2 + ;; + *) + target="$1" + shift + ;; + esac +done + +case "$budget" in + '' | *[!0-9]*) + echo "--budget takes whole seconds, got '$budget'" >&2 + exit 2 + ;; +esac + +case "$fail_on" in + required | any) ;; + *) + echo "--fail-on takes required or any, got '$fail_on'" >&2 + exit 2 + ;; +esac + +for tool in gh jq git; do + command -v "$tool" >/dev/null || { echo "$tool is not installed" >&2; exit 2; } +done + +if ! pr_json=$(gh pr view ${target:+"$target"} --json number,url,state,baseRefName 2>&1); then + echo "No PR found${target:+ for '$target'}: $pr_json" >&2 + echo "Open one first: gh pr create --base main" >&2 + exit 2 +fi + +number=$(jq -r .number <<<"$pr_json") +url=$(jq -r .url <<<"$pr_json") +state=$(jq -r .state <<<"$pr_json") +base=$(jq -r .baseRefName <<<"$pr_json") + +if [ "$state" != OPEN ]; then + echo "PR #$number is $state: $url" + [ "$state" = MERGED ] && exit 0 + exit 2 +fi + +# With no explicit target the PR is this branch's, so its head should become the local HEAD. +local_sha="" +[ -z "$target" ] && local_sha=$(git rev-parse HEAD 2>/dev/null || true) + +# With no rule to read (unprotected, or no access), fall back to whichever registered checks +# GitHub marks required, and failing that to every registered check. Test the exit status, not the +# output: on a 404 `gh api --jq` still prints the error body to stdout. +required_from_rule=0 +if required=$(gh api "repos/{owner}/{repo}/branches/$base/protection/required_status_checks" \ + --jq '.contexts[]' 2>/dev/null) && [ -n "$required" ]; then + required_from_rule=1 +fi + +started=$(date +%s) +deadline=$((started + budget)) +head_sha="" +report="" +never_ran=0 + +while :; do + now=$(date +%s) + view=$(gh pr view "$number" --json mergeable,headRefOid 2>/dev/null || echo '{}') + mergeable=$(jq -r '.mergeable // "UNKNOWN"' <<<"$view") + head_sha=$(jq -r '.headRefOid // ""' <<<"$view") + + if [ "$mergeable" = CONFLICTING ]; then + echo "PR #$number @ ${head_sha:0:9}: $url" + echo "CONFLICT PR #$number conflicts with $base, so CI will not run on this head." + echo "Rebase onto origin/$base, re-run your local validation, then push with --force-with-lease." + exit 3 + fi + + # Right after a push GitHub can still report the previous head, whose checks are all finished. + if [ -n "$local_sha" ] && [ "$head_sha" != "$local_sha" ] \ + && [ "$now" -lt $((started + push_grace)) ] && [ "$now" -lt "$deadline" ]; then + sleep 10 + continue + fi + + checks=$(gh pr checks "$number" --json name,bucket,link 2>/dev/null || true) + jq -e 'type == "array"' >/dev/null 2>&1 <<<"$checks" || checks='[]' + + if [ "$required_from_rule" = 0 ]; then + required=$(gh pr checks "$number" --required --json name --jq '.[].name' 2>/dev/null) || required="" + [ -n "$required" ] || required=$(jq -r '.[].name' <<<"$checks") + fi + required_json=$(printf '%s\n' "$required" | jq -R . | jq -s 'map(select(length > 0))') + + report=$(jq -r --argjson req "$required_json" ' + def kind: .name as $n | if ($req | any(. == $n)) then "required" else "not required" end; + def job: (.link // "") | (capture("/runs/(?[0-9]+)/job/(?[0-9]+)") // {}) + | if .job then "run \(.run) job \(.job)" else "" end; + (map(.name)) as $seen + | if length == 0 and ($req | length) == 0 then ["PENDING\tno checks registered for this head yet\t"] + else + map(select(.bucket == "fail" or .bucket == "cancel") + | "\(if .bucket == "fail" then "FAIL" else "CANCEL" end)\t\(.name) (\(kind))\t\(job)") + + map(select(.bucket == "pending") | "PENDING\t\(.name) (\(kind))\t") + + ($req - $seen | map("PENDING\t\(.) (required)\tnot registered yet")) + end + | .[]' <<<"$checks") + + required_failed=$(grep -cE '^(FAIL|CANCEL).*\(required\)' <<<"$report" || true) + any_failed=$(grep -cE '^(FAIL|CANCEL)' <<<"$report" || true) + blocking_failed=$required_failed + [ "$fail_on" = any ] && blocking_failed=$any_failed + anything_pending=$(grep -c '^PENDING' <<<"$report" || true) + unregistered=$(grep -c 'not registered yet$' <<<"$report" || true) + now=$(date +%s) + + if [ "$blocking_failed" -gt 0 ] || [ "$anything_pending" -eq 0 ] || [ "$now" -ge "$deadline" ]; then + break + fi + + # A required check that has not registered once every run for this head has finished, one of + # them unsuccessfully, never will: a gate job upstream of it failed or never got a runner. The + # failed run is the tell; "all finished" alone can be a fast workflow beating a slow one's start. + if [ "$unregistered" -gt 0 ] && [ "$unregistered" -eq "$anything_pending" ] && [ -n "$head_sha" ]; then + runs=$(gh run list --commit "$head_sha" --json status,conclusion --jq ' + if length == 0 then "none" + elif any(.[]; .status != "completed") then "open" + elif any(.[]; .conclusion != "success" and .conclusion != "skipped" and .conclusion != "neutral") then "dead" + else "clean" end' 2>/dev/null || echo none) + if [ "$runs" = dead ]; then + never_ran=1 + break + fi + fi + + remaining=$((deadline - now)) + sleep $((remaining < interval ? remaining : interval)) +done + +waited=$(($(date +%s) - started)) +echo "PR #$number @ ${head_sha:0:9}: $url" +if [ -n "$local_sha" ] && [ -n "$head_sha" ] && [ "$head_sha" != "$local_sha" ]; then + echo "note: local HEAD ${local_sha:0:9} is not the PR head, so these results are for ${head_sha:0:9}. Push first?" +fi +[ -n "$report" ] && printf '%s\n' "$report" | column -t -s "$(printf '\t')" + +# A job that "failed" with zero steps never got a runner: an Actions billing block or a runner +# outage, which no change to the diff can fix (AGENTS.md, "Reading a red check"). +for job_id in $(sed -nE 's/^(FAIL|CANCEL).*job ([0-9]+)$/\2/p' <<<"$report"); do + steps=$(gh api "repos/{owner}/{repo}/actions/jobs/$job_id" --jq '.steps | length' 2>/dev/null || true) + if [ "$steps" = 0 ]; then + echo "note: job $job_id never started (0 steps). That is Actions billing or runner allocation, not the diff." + fi +done + +if [ "$blocking_failed" -gt 0 ]; then + # Point at the job that actually failed. Under --fail-on any, a red check that is not required is + # usually what a required aggregator is about to report, and the aggregator's own log only names it. + first_job="" + [ "$fail_on" = any ] \ + && first_job=$(grep -E '^(FAIL|CANCEL).*\(not required\)' <<<"$report" | sed -nE 's/.*job ([0-9]+)$/\1/p' | head -1) + [ -n "$first_job" ] \ + || first_job=$(grep -E '^(FAIL|CANCEL)' <<<"$report" | sed -nE 's/.*job ([0-9]+)$/\1/p' | head -1) + if [ "$required_failed" -gt 0 ]; then + echo "verdict: a required check is red after ${waited}s." + else + echo "verdict: a check is red after ${waited}s, and --fail-on is any." + fi + # `gh run view --log-failed` refuses until the whole run finishes; the jobs API serves one + # finished job's log while the rest of the run is still going. + if [ -n "$first_job" ]; then + echo "Read it: gh api repos/{owner}/{repo}/actions/jobs/$first_job/logs --allow-escape-sequences | grep -nE 'FAILED|What went wrong|^e: |##\[error\]'" + fi + exit 1 +fi + +if [ "$never_ran" = 1 ]; then + echo "verdict: every run for this head has finished, but the required checks marked 'not registered yet' never ran. Read the red jobs above." + exit 1 +fi + +if [ "$anything_pending" -gt 0 ]; then + echo "verdict: still running after ${waited}s. Run scripts/pr-checks.sh again." + exit 8 +fi + +required_count=$(jq 'length' <<<"$required_json") +if [ "$any_failed" -gt 0 ]; then + echo "verdict: all $required_count required checks passed; $any_failed check(s) that are not required are red, listed above." +else + echo "verdict: all $required_count required checks passed, and nothing else is red." +fi +exit 0