From d7a30d169a95a46db14f98d76cd069be02b1687e Mon Sep 17 00:00:00 2001 From: DoubleGate Date: Sat, 14 Nov 2026 05:58:12 -0500 Subject: [PATCH 1/4] ci: sync the Antigravity PR reviewer with the current template Re-installs antigravity-pr-review (template b8501ed) over a copy that was an unmodified older template version, so nothing repo-specific is lost. The headline fix: the UTF-8 scrub after byte caps was a no-op on glibc, because GNU `iconv -c` exits 1 whenever it drops bytes and the old code discarded its output, so agy could be handed a split UTF-8 sequence. It now falls back to python3 and is shared by the prompt and review-output paths. Also: the shallow merge-base fetches carry the token header, the README (installed as .github/AGY-REVIEWER.md) lists the real file set, and every template fix since this copy was installed comes along. Co-Authored-By: Claude Opus 5.5 --- .github/AGY-REVIEWER.md | 329 ++++++++++++ .github/workflows/antigravity-review.yml | 149 ++++- scripts/_agy_comment_body.sh | 104 +++- scripts/_agy_print.sh | 22 +- scripts/agy-review-selftest.sh | 271 +++++++++- scripts/agy-review.sh | 656 +++++++++++++++++++---- 6 files changed, 1406 insertions(+), 125 deletions(-) create mode 100644 .github/AGY-REVIEWER.md diff --git a/.github/AGY-REVIEWER.md b/.github/AGY-REVIEWER.md new file mode 100644 index 00000000..a0e220a2 --- /dev/null +++ b/.github/AGY-REVIEWER.md @@ -0,0 +1,329 @@ +# Antigravity PR Review + +A drop-in replacement for the retired **Gemini Code Assist for GitHub** consumer +app, rebuilt to run on your **Google AI Ultra** subscription with **no paid, +pay-as-you-go API usage**. + +It auto-reviews pull requests (on open, on every push, and on an `/agy-review` comment) using +the **Antigravity CLI (`agy`)** running on a **self-hosted runner** on your own +machine. Because `agy` authenticates with your Google OAuth session, every +review draws on your Ultra rate limits instead of a metered Gemini API key. + +--- + +## Why this is free under Ultra (the one idea that matters) + +| Path | Auth | Cost | +|------|------|------| +| Gemini **API key** / `run-gemini-cli` / Antigravity **SDK** (`run-agy-sdk`) | API key | **metered, pay-as-you-go** | +| Antigravity **CLI** (`agy`) | Google **OAuth** | **your Ultra rate limits** | + +`agy` supports OAuth sign-in with "generous rate limits on Google AI Pro and +Ultra plans." So the whole trick is to build the reviewer around the **`agy` +CLI's cached OAuth session** — never around an API key. That also means the job +must run **on a machine where `agy` is already logged in** (your desktop), which +is why this uses a self-hosted runner rather than GitHub's cloud runners. + +> **ToS caveat, stated plainly:** Google deliberately moved GitHub reviews to its +> enterprise/API product. Automating a *consumer* Ultra subscription for CI-style +> workloads is a gray area — the realistic downside is rate-limiting. This template +> reviews on open, reopen, **every push** (`synchronize`) and on demand; remove +> `synchronize` from the workflow if you need to keep volume lower. + +--- + +## How it works + +``` +GitHub PR event ──▶ self-hosted runner (YOUR machine, systemd --user) + │ gh pr diff ─▶ build adversarial-reviewer prompt (+ style guide) + │ agy --print (PTY-wrapped, OAuth/Ultra) ─▶ review text + └─ gh api ─▶ one comment per PR, edited in place (earlier rounds archived) +``` + +Files in this template: + +| File | Goes where | Purpose | +|------|-----------|---------| +| `.github/workflows/antigravity-review.yml` | **copy → target repo** | trigger + job definition | +| `scripts/agy-review.sh` | **copy → target repo** | the reviewer (diff → prompt → `agy` → comment) | +| `scripts/_agy_print.sh` | **copy → target repo** | PTY fallback helper (only if `unbuffer` is absent) | +| `scripts/_agy_comment_body.sh` | **copy → target repo** | comment-body and archive format; `agy-review.sh` sources it unconditionally | +| `scripts/agy-review-selftest.sh` | **copy → target repo** | offline self-test of the reviewer; run it in the target's CI | +| `.github/actionlint.yaml` | **copy → target repo** (only if absent) | declares the self-hosted `agy` label for actionlint | +| `README.md` (this file) | **copy → `.github/AGY-REVIEWER.md`** | operations doc: setup, troubleshooting, security model | +| `agy-review.md.example` | **copy → `.github/agy-review.md`** in target repo | the review style guide (optional) | +| `scripts/provision-runner.sh`, `scripts/deprovision-runner.sh` | template-local | stand up and tear down a per-repo runner | +| `systemd/github-agy-runner@.service` | `~/.config/systemd/user/` | runs each runner in your session (template-local) | +| `gitignore.snippet` | *optional append* | precautionary ignores (see below) — never copied over an existing `.gitignore` | +| `.gitignore` | template-local | this repo's own hygiene — **do not** copy into target repos | + +> **Copy into a target repo** (what `install-into-repo.sh` does): the workflow, the four +> scripts `agy-review.sh`, `_agy_print.sh`, `_agy_comment_body.sh` and +> `agy-review-selftest.sh`, this README as `.github/AGY-REVIEWER.md`, +> `.github/actionlint.yaml` when the target has none, optionally `.github/agy-review.md`, +> and one appended `.gitignore` line (`/.agy-review-work/`). Everything else +> (`systemd/`, the provision/deprovision scripts, this repo's `.gitignore`, `*.example`, +> `gitignore.snippet`) stays here. + +--- + +## One-time host setup (do this once per machine) + +### 1. Prerequisites + +```fish +# agy must be installed and logged in (it already is on this box): +agy -p "reply with the single word: ok" # should print: ok + +# Tools the reviewer uses (all already present here): +# gh jq sqlite3 col script + unbuffer (from `expect`) +pacman -Q github-cli jq sqlite expect util-linux >/dev/null # verify; install any missing with paru/pacman +``` + +If `agy -p` prints nothing, fix auth first (run `agy` interactively once to +complete the Google sign-in) — the reviewer can't work until that one-liner does. + +### 2. Register a self-hosted runner (label `agy`) per repo + +Personal `github.com//*` repos support only **repo-level** runners, so you +run **one instance per repo** on this one machine, each in `~/actions-runner/`. +Download the runner once (get the current version + checksum from **repo → +Settings → Actions → Runners → New self-hosted runner → Linux/x64**), unpack a +copy per repo, then configure each with a freshly minted token: + +```fish +# one-time download (pick the version GitHub shows you) +set VER 2.336.0 +# per repo: +for pair in "RustySNES:rustysnes" "RustyN64:rustyn64" + set repo (string split ":" $pair)[1]; set name (string split ":" $pair)[2] + mkdir -p ~/actions-runner/$name + tar xzf ~/Downloads/actions-runner-linux-x64-$VER.tar.gz -C ~/actions-runner/$name + set token (gh api -X POST repos/doublegate/$repo/actions/runners/registration-token --jq .token) + cd ~/actions-runner/$name + ./config.sh --url https://github.com/doublegate/$repo --token $token \ + --labels agy --name (hostname)-$name --work _work --unattended --replace +end +``` + +Do **not** use the bundled `svc.sh` (it makes a *system* service that can't read +your keyring). Use the `--user` template unit below. + +### 3. Run each runner as a `systemd --user` instance + +```fish +mkdir -p ~/.config/systemd/user +cp systemd/github-agy-runner@.service ~/.config/systemd/user/ +systemctl --user daemon-reload +systemctl --user enable --now github-agy-runner@rustysnes github-agy-runner@rustyn64 +systemctl --user is-active github-agy-runner@rustysnes github-agy-runner@rustyn64 +``` + +Keyring note: the unit intentionally does **not** enable linger — it runs inside +your unlocked graphical session so `agy`'s OAuth token in the keyring is +readable. Reviews process while you're logged in; they queue otherwise. + +--- + +## Per-repo setup + +### Quickest: the installer + +```fish +set TPL ~/Code/Local_Only-Projects/antigravity-pr-review +$TPL/install-into-repo.sh /path/to/existing/repo --with-style-guide +``` + +It copies the workflow + `scripts/` (and, with `--with-style-guide`, a starter +`.github/agy-review.md`), leaves the target's `.gitignore` untouched, and prints +the `git add/commit/push` to finish. Then jump to **Triggering it** below. + +### Or by hand + +1. Copy `.github/workflows/antigravity-review.yml` and the `scripts/` directory + into the target repo (keep the paths). +2. `chmod +x scripts/*.sh` and commit (git preserves the executable bit). +3. Optionally copy `agy-review.md.example` → **`.github/agy-review.md`** and + tailor the rules. The dedicated filename avoids clashing with an existing + `GEMINI.md`/`AGENTS.md`. Point elsewhere with the `STYLE_GUIDE` env var. +4. Ensure the repo's **Settings → Actions** allows workflows and self-hosted + runners (default for private repos you own). + +That's it — no repository secrets. The job uses the built-in `GITHUB_TOKEN` to +post comments and your local `agy` session for the model. + +### About `.gitignore` (append, don't overwrite) + +The reviewer works in temporary directories (`mktemp` / `$RUNNER_TEMP`) for +everything **except** one case: when a PR's diff is too large to inline in the +argv prompt, it writes the diff to a gitignored working-tree scratch dir, +`.agy-review-work/`, so agy can read it in full with its file tools instead of +the review being truncated to the execve arg-size ceiling. The file and the +(now-empty) dir are removed by the script's `EXIT` trap. Because that path is in +the working tree, a target repo **does** need `/.agy-review-work/` in its +`.gitignore` — otherwise the transient `.patch` shows as untracked pollution on +large-diff PRs. Still **never** copy this template's own `.gitignore` over a +project's. + +**Append** the ignore entries idempotently: + +```fish +# from the target repo root +grep -qxF '/.agy-review-work/' .gitignore 2>/dev/null + or cat $TPL/gitignore.snippet >> .gitignore # TPL: your checkout of this template +``` + +--- + +## Triggering it + +- **Automatic:** open, reopen, or **push to** a PR → a review is posted within a + minute or two. This fires from the workflow on the **PR branch**, so the very PR + that adds the workflow is itself a live test, and every follow-up push re-reviews + (`synchronize`). Auto-runs only for PRs authored by a trusted user (see the + security model) so a fork's code never auto-executes on the runner. +- **On demand:** comment **`/agy-review`** on any PR to (re)run it. + +### One comment per PR, appended to — never replaced + +Each run **edits** the existing review comment: the newest round goes on top and +every earlier round is folded into a collapsed `
` block beneath it. The +thread stays clean and **nothing is destroyed**. + +This replaced a delete-and-repost design, and the reason is worth keeping. That +version posted a fresh comment and deleted the previous one, so a round nobody had +read before the next push was **gone**, with nothing on the PR indicating it had +ever existed. Unlike a CodeRabbit or Copilot review thread, an unaddressed +Antigravity finding left no trace at all. It was caught on a real PR where two +consecutive rounds each raised a blocking issue and only the second survived — a +clean comment list is not evidence that nothing was raised. + +The script no longer issues a `DELETE` at all; `agy-review-selftest.sh` asserts +that, so the behaviour cannot come back unnoticed. The archive is bounded by +`MAX_BODY_BYTES` (default 60000, under GitHub's 65536 limit); when rounds must be +dropped to fit, the oldest go first **and the body says how many were dropped**, +because a silent truncation would look exactly like a PR reviewed only once. + +> **Default-branch rule:** GitHub runs `issue_comment` workflows from the copy on +> the **default branch**. So the `/agy-review` comment trigger only works after +> the workflow is merged to `main`. The `opened` trigger works from the PR branch +> immediately. +> +> The same rule governs the **scripts**: the workflow checks out the default branch +> to run them, so a change to `agy-review.sh` (the archive behaviour above included) +> has **no effect on any PR until it is merged**, not even on the PR that makes it. + +--- + +## Security model (read this if your repo is public) + +GitHub, [OWASP](https://cheatsheetseries.owasp.org/cheatsheets/GitHub_Actions_Security_Cheat_Sheet.html), +and [Wiz](https://www.wiz.io/blog/github-actions-security-guide) all warn that +**self-hosted runners should almost never back public repos** — runners are +non-ephemeral, so untrusted code from a fork PR can persistently compromise the +host. This template applies the standard mitigations, but the residual risk is +yours to accept: + +- **Required: approval for every outside contributor.** For a `pull_request` event + GitHub runs the workflow file **from the PR's own branch**, so a fork can edit or + delete any `if:` in it and point a job at your runner. Only GitHub's approval gate + stops that. Set **Settings → Actions → General → Fork pull request workflows → + Require approval for all outside collaborators** (API: + `gh api -X PUT repos/OWNER/REPO/actions/permissions/fork-pr-contributor-approval + -f approval_policy=all_external_contributors`). The default, + `first_time_contributors`, runs a returning contributor's fork PR without asking. + Never approve a fork PR's workflows unless you have read its `.github/` changes. +- **Same-repository gate on auto-runs:** the `pull_request` job (open/reopen/push, + i.e. `synchronize`) is written to run only when the PR head is a branch **in this + repository** (`github.event.pull_request.head.repo.full_name == github.repository`). + That keeps an unmodified workflow off fork PRs, but it is not a security boundary, + for the reason above. +- **Write-access gate on comments:** `/agy-review` from a commenter whose + `author_association` is `OWNER`/`MEMBER`/`COLLABORATOR` passes the workflow's + cheap pre-filter, but that is not the permission check (a Triage-only user also + reports as COLLABORATOR). The authoritative check is `agy_has_write_access` in + `scripts/agy-review.sh`, which asks the permissions API for the commenter and + fails closed; the script also re-checks that the PR is not from a fork, because + the `issue_comment` payload has no head-repo field for the `if:` to gate on. +- **First-install bootstrap:** on a repository whose default branch has no + reviewer yet, the workflow runs the PR head's scripts once, only for a same-repo + branch whose author has write access *at run time*. Remove the bootstrap steps + once the reviewer is on the default branch (they also trip CodeQL's + `actions/untrusted-checkout`). +- **Least surface in the job:** review-only prompt, `--sandbox`, no repo secrets + (built-in `GITHUB_TOKEN` only), temp files cleaned via an `EXIT` trap. + +To reduce the remaining exposure, run each job on a **clean, disposable host** (a VM or +container image recreated per job). Re-registering a runner on the same host is not enough, +because the host's state survives. Making the repository private does not remove the risk either: +if fork workflows are enabled for a private repository, anyone who can fork it can still reach the +runner, so disable fork workflows there or gate them the same way. + +--- + +## Configuration (workflow `env:`) + +| Variable | Default | Meaning | +|----------|---------|---------| +| `AGY_MODEL` | *(agy default)* | e.g. `gemini-3-pro`; run `agy models` to see IDs | +| `AGY_EFFORT` | `high` | reasoning effort: `low` / `medium` / `high` | +| `AGY_PRINT_TIMEOUT` | `5m` | base wait for a single `agy --print`; raised for a large diff (next row). Setting it pins it and turns the raise off | +| `AGY_TIMEOUT_SECONDS_PER_MIB` | `900` | extra seconds per MiB of diff. A diff handed over as a file costs agy 6-8 minutes almost whatever its size, so this is deliberately steep (240 left a 426 KB diff at 397 s, too little) | +| `AGY_PRINT_TIMEOUT_MAX_SECONDS` | `1800` (workflow: `900`) | ceiling on one attempt after the raise | +| `AGY_RETRIES` | `3` (workflow: `2`) | attempts; a retry is for a transient backend error, so more of them at the same budget only repeat a timeout | +| `AGY_DIFF_MODE` | `auto` | how the diff reaches agy: `inline` (in the `--print` prompt), `file` (written to a file agy reads with its tools), or `auto` (inline if it fits the arg-size budget, else file — so **large PRs are never truncated**) | +| `MAX_DIFF_BYTES` | `5000000` | sanity cap on a pathological diff (5 MB). Not the arg-size limit — a large diff goes to agy as a file, not an argv value | +| `MAX_PROMPT_BYTES` | `125000` | the inline/file threshold in `auto` mode, and a hard backstop on the **inlined** prompt. agy takes an inlined prompt as a `--print` argv value, and a single `execve` argument cannot exceed `MAX_ARG_STRLEN` (128 KiB); over it `execve` fails with E2BIG before agy starts. Clamped below 128 KiB | +| `STYLE_GUIDE` | `.github/agy-review.md` | repo-relative style guide, loaded if present | + +--- + +## Troubleshooting + +- **Review comment never appears / job succeeds but empty:** the classic + Antigravity **issue #76** — `agy -p` drops stdout when it isn't attached to a + TTY. This template runs `agy` under a PTY (`unbuffer`, or `scripts/_agy_print.sh` + if `unbuffer` is absent). If output is still empty the job fails loudly and + dumps `agy`'s stderr; confirm `agy -p "hi"` works for the runner's user. +- **`agy: command not found` in the job:** the systemd unit's `PATH` must include + `~/.local/bin`. Confirm with `systemctl --user show-environment`. +- **Auth errors / no output:** the keyring is probably locked (you're logged out, + or the runner started before the session unlocked). Log in, then + `systemctl --user restart github-agy-runner`. +- **"agy hit its print timeout ... on N of N attempt(s)":** the review needed + more time than the budget, which is a size problem, not an outage: agy did run + (an expired login prints its OAuth flow instead, and a backend error is logged + as one). A diff over the inline budget goes to agy as a file, and that costs + 6-8 minutes at `AGY_EFFORT: high` almost whatever its size. Raise + `AGY_TIMEOUT_SECONDS_PER_MIB` or `AGY_PRINT_TIMEOUT_MAX_SECONDS` in the + workflow's `env:` and keep the job's `timeout-minutes` above the worst case + (the selftest checks it). For a `pull_request` run GitHub takes the workflow + from the PR branch, so an `env:` change takes effect on the PR's next push; a + script change only after it reaches the default branch. +- **Job never starts:** no runner is online/idle with the `agy` label, or the + machine is asleep. Check `systemctl --user status github-agy-runner`. +- **Review fails on a very large PR (`gh pr diff failed`):** GitHub's API refuses + any diff over **20,000 lines** with HTTP 406, so `gh pr diff` cannot fetch it at + all. This is separate from — and upstream of — the arg-size handling + (`AGY_DIFF_MODE`): that decides how a diff *already in hand* reaches agy, but a + 406 means there is no diff in hand. The script now detects the 406 specifically + (a genuine auth/network/bad-PR failure is still fatal) and falls back to + computing the diff locally: it fetches `refs/pull//head` and the base branch + into private `refs/agy/*`, takes `git merge-base`, and `git diff`s them. It + fetches objects but **never checks them out** — the working tree is untouched and + the PR content is never executed — and the refs are removed on exit. The + recovered diff then flows through the normal inline/file path, so a 67k-line PR + reviews the same as a 50-line one. The fetch authenticates via `http.extraheader` + (Actions `GITHUB_TOKEN`), falling back to git's ambient credentials for a hand + run, so it does not depend on `persist-credentials`. + +--- + +## Roadmap / possible enhancements + +- **Inline line comments** (map findings to diff hunks via `gh api ... /reviews`) + instead of a single summary comment. +- **Quota guard:** skip auto-review for docs-only or tiny diffs; a daily cap. +- **`/agy-review `** to pass an ad-hoc instruction (e.g. `security only`). +- **Label gate:** only auto-review PRs carrying a `needs-review` label. diff --git a/.github/workflows/antigravity-review.yml b/.github/workflows/antigravity-review.yml index 4a5abbbc..6e7dde4a 100644 --- a/.github/workflows/antigravity-review.yml +++ b/.github/workflows/antigravity-review.yml @@ -3,7 +3,8 @@ # so it draws on your Google AI Ultra limits instead of a paid API key. # # Requires a self-hosted runner registered with the label `agy` on a machine -# where `agy` is logged in. See ../../README.md for the full setup. +# where `agy` is logged in. See the template README (installed into target repos +# as .github/AGY-REVIEWER.md) for the full setup. name: Antigravity PR Review on: @@ -18,8 +19,9 @@ on: permissions: contents: read + # Creating and editing a PR's timeline comment needs Issues OR Pull requests write; + # pull-requests alone is enough, so `issues: write` is not granted (least privilege). pull-requests: write - issues: write jobs: review: @@ -31,11 +33,16 @@ jobs: # collaborators" setting is belt-and-suspenders, not the gate — its default only # covers *first-time* contributors, so a returning outside contributor would # otherwise get a runner. - # - issue_comment: only `/agy-review` from someone with write access - # (OWNER/MEMBER/COLLABORATOR), so a stranger cannot trigger execution by - # commenting. `scripts/agy-review.sh` re-checks the fork status defensively, - # because the `issue_comment` payload carries no head-repo field for the `if:` - # to gate on — a trusted commenter is not the same as a trusted diff. + # - issue_comment: only `/agy-review`, and the association test below is a CHEAP + # PRE-FILTER, not the permission gate. `author_association` answers "what is this + # person's relationship to the repo", not "may they run code on my workstation": + # a Triage-only user reports as COLLABORATOR with no write access whatsoever. + # The real decision is `agy_has_write_access` in `scripts/agy-review.sh`, which + # asks the permissions API and fails closed. Keeping the coarse test here only + # avoids booting a runner for an obvious stranger; do not treat it as the gate. + # That script also re-checks fork status, because the `issue_comment` payload + # carries no head-repo field for the `if:` to gate on — a trusted commenter is + # not the same as a trusted diff. if: >- (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository) || @@ -55,8 +62,14 @@ jobs: runs-on: [self-hosted, agy] # Bounded harder than the hosted jobs, not softer: this runs on the # maintainer's own hardware, so a hung run holds a real machine rather than - # a disposable VM. Observed runtime is 1-3 minutes. - timeout-minutes: 30 + # a disposable VM. Observed runtime is 1-3 minutes for a small diff and 6-8 + # for a large one handed over as a file. The cap must still exceed the + # script's own worst case -- lock wait + every retry at its maximum timeout + + # backoff -- or the job is killed mid-retry and posts nothing. With the + # settings below that is 300 + 2 x 900 + 15 = 2115 s, under 45 minutes with + # room for checkout and the diff fetch; the selftest checks this sum against + # this value. + timeout-minutes: 45 steps: # Check out the DEFAULT BRANCH, never the PR head. This job runs the checked-out # `scripts/agy-review.sh` on a self-hosted runner with a token in the environment, @@ -86,12 +99,110 @@ jobs: # supplies the token per command via `http.extraheader` rather than persisting it. persist-credentials: false + # BOOTSTRAP ONLY. On the very first install in a repo the scripts exist on the PR + # branch but not yet on the default branch, so the run step below chmods files that + # are not there and the job dies before it can review anything. Observed on + # CyberChef-MCP PR #41, and predicted by Copilot review on that same PR. + # + # This step checks out and then EXECUTES code from the PR head on a self-hosted runner, + # which is normally the thing you must never do. Three properties are what make it + # defensible here, and all three must hold — if you are editing this step, they are the + # invariant, not the YAML: + # + # 1. It fires only when the default branch has no reviewer at all (`hashFiles` empty), + # i.e. exactly once in a repository's life, and never again after that PR merges. + # 2. The head must be a branch in THIS repository, so a fork PR can never reach it. + # 3. The PR author must hold write access AT RUN TIME, verified against the permissions + # API by the step below — not inferred from `author_association`, and not merely + # implied by their having been able to push the branch at some point in the past. + # This is what closes the revoked-collaborator gap: a stale branch pushed by someone + # whose access was later removed no longer bootstraps. + # + # Worth stating plainly, because it is what bounds the exposure: `pull_request` runs the + # workflow definition from the PR branch. Anyone who can satisfy (2) and (3) can already + # rewrite THIS FILE on their branch and have it execute on the runner. The bootstrap + # therefore grants no capability that the event type has not already granted to the same + # set of people. It is not a reason to relax any of the three conditions. + - name: Check bootstrap author still has write access + id: agy_bootstrap_auth + if: >- + hashFiles('scripts/agy-review.sh') == '' && + github.event.pull_request.head.repo.full_name == github.repository + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR_AUTHOR: ${{ github.event.pull_request.user.login }} + run: | + set -eu + # Fails closed: an API error, an unknown login, or any role below `write` leaves + # `writer` unset, and the checkout below does not run. + # + # The failure REASON is surfaced, not swallowed. Denying silently makes "this person + # has no write access" (intended, one PR affected) indistinguishable from "the token + # lost its scope" (the bootstrap is dead for everyone) — an outage that reads as a + # decision. Redacted first: this log is public. + if perm="$(gh api "repos/${GITHUB_REPOSITORY}/collaborators/${PR_AUTHOR}/permission" \ + --jq '.permission // empty' 2>perm_err.txt)"; then + : + else + reason="$(tr '\n' ' ' < perm_err.txt \ + | sed -E 's/(gh[pousr]|github_pat)_[A-Za-z0-9_]+//g' \ + | cut -c1-300)" + echo "::warning::Reviewer bootstrap permission lookup failed for ${PR_AUTHOR}: ${reason}" + perm="" + fi + rm -f perm_err.txt + case "$perm" in + admin|maintain|write) + echo "writer=true" >> "$GITHUB_OUTPUT" ;; + *) + echo "::warning::Skipping reviewer bootstrap: ${PR_AUTHOR} has permission '${perm:-none}', write required." ;; + esac + + - name: Bootstrap reviewer from PR head (first install only) + id: agy_bootstrap + if: steps.agy_bootstrap_auth.outputs.writer == 'true' + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + with: + ref: ${{ github.event.pull_request.head.sha }} + fetch-depth: 1 + persist-credentials: false + path: .agy-bootstrap + + - name: Install bootstrapped reviewer + if: steps.agy_bootstrap.outcome == 'success' + run: | + set -eu + echo "::notice::Default branch has no reviewer yet; bootstrapping from the PR head." + mkdir -p scripts + cp .agy-bootstrap/scripts/agy-review.sh \ + .agy-bootstrap/scripts/_agy_print.sh \ + .agy-bootstrap/scripts/_agy_comment_body.sh scripts/ + # The style guide is read from the workspace too, and is equally absent on a first install. + if [ -f .agy-bootstrap/.github/agy-review.md ] && [ ! -f .github/agy-review.md ]; then + mkdir -p .github && cp .agy-bootstrap/.github/agy-review.md .github/ + fi + rm -rf .agy-bootstrap + - name: Run Antigravity review env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # --- optional overrides (uncomment to change) --- # AGY_MODEL: gemini-3-pro # default: agy's configured model AGY_EFFORT: high # low|medium|high + # A large diff goes to agy as a file, and reading it at high effort costs 6-8 + # minutes almost whatever its size (1.15 MB: 465 s; 426 KB timed out three times at + # 397 s under the old 240 s/MiB scaling). The scaling is now 900 s/MiB (the + # script's AGY_TIMEOUT_SECONDS_PER_MIB), so 900 s here is reached from ~650 KB. + AGY_PRINT_TIMEOUT_MAX_SECONDS: "900" # per-attempt cap (default 1800) + # Also set here, not only as the script's default: a pull_request run takes + # this file from the PR branch but the script from the default branch, so a + # repository whose default branch still has the old script would otherwise + # keep scaling at 240 s/MiB. + AGY_TIMEOUT_SECONDS_PER_MIB: "900" # budget per MiB of diff (script default 900) + # Two attempts, not three: a retry is for a transient backend error; a third + # attempt at the same budget only repeats a timeout. + AGY_RETRIES: "2" + AGY_LOCK_WAIT: "300" # wait for another review's lock (default 600) # AGY_DIFF_MODE: auto # auto|inline|file — how the diff reaches agy. `auto` # # (default) inlines a small diff and hands a large one to # # agy as a file, so PR size never truncates the review. @@ -108,14 +219,16 @@ jobs: # chmods a file the default branch does not have yet. Observed exactly # once, on the PR that introduced `_agy_comment_body.sh`. # - # The two required files are chmod'd unconditionally; anything added later - # is chmod'd only if present, so the workflow stays compatible with both - # the old and the new script set. A genuinely missing required file still - # fails loudly -- `agy-review.sh` sources it and dies -- rather than being - # papered over here. + # Only the two files that are EXECUTED need the bit, and both are listed below: + # `agy-review.sh` runs directly, and `_agy_print.sh` is exec'd through + # `script -qfec` as the PTY fallback. + # + # `_agy_comment_body.sh` is deliberately NOT listed: it is sourced, never + # executed, so it never needed the bit. It used to be chmod'ed anyway, by a loop + # that iterated a single item -- the loop and that redundant entry are what were + # removed here, not the scripts themselves. + # + # A genuinely missing required file still fails loudly: `agy-review.sh` sources + # it and dies, rather than being papered over here. chmod +x scripts/agy-review.sh scripts/_agy_print.sh - for opt in scripts/_agy_comment_body.sh; do - [ -f "$opt" ] && chmod +x "$opt" - done - true scripts/agy-review.sh diff --git a/scripts/_agy_comment_body.sh b/scripts/_agy_comment_body.sh index 5668f9ce..84519914 100755 --- a/scripts/_agy_comment_body.sh +++ b/scripts/_agy_comment_body.sh @@ -25,6 +25,18 @@ AGY_ARCHIVE_START='' AGY_ARCHIVE_END='' +# Marks where the archive SECTION begins -- i.e. the `
` wrapper, which sits OUTSIDE +# the start/end sentinels. +# +# Why a third sentinel rather than reusing AGY_ARCHIVE_START for both jobs: the two boundaries +# are not the same boundary. `agy_body_head` needs "where does the newest round stop", which is +# before the wrapper; `agy_body_archive` needs "where do the accumulated rounds begin", which is +# after it. Conflating them put the wrapper inside the extracted region, so every run captured +# the previous wrapper and nested a new one around it -- the archive grew a fresh `
` +# layer per round and `agy_drop_oldest_round` then tore holes in it. Measured on a real PR at +# round six: seven "Earlier review rounds" wrappers, 15 `
` against 13 `
`. +AGY_ARCHIVE_SECTION='' + # The newest round of a comment body: everything after the marker, before the archive. # # `awk` matching WHOLE LINES rather than `sed` with a pattern, because a review body @@ -32,7 +44,12 @@ AGY_ARCHIVE_END='' # match a line that merely mentions one. agy_body_head() { local marker="$1" - awk -v s="$AGY_ARCHIVE_START" '$0 == s { exit } { print }' \ + # Stops at the SECTION sentinel, not the start sentinel: the `
` wrapper lives + # between the two, and capturing it here would append a stray opener to the newest round. + # Falls back to the start sentinel so a body written before the section sentinel existed + # still splits correctly. + awk -v s="$AGY_ARCHIVE_START" -v sec="$AGY_ARCHIVE_SECTION" \ + '$0 == sec { exit } $0 == s { exit } { print }' \ | grep -v -F -x "$marker" || true } @@ -41,10 +58,89 @@ agy_body_head() { # The `$0 == e` test comes FIRST so a body whose archive is empty yields nothing rather than # emitting its own end sentinel. agy_body_archive() { - awk -v s="$AGY_ARCHIVE_START" -v e="$AGY_ARCHIVE_END" ' + # Emits only whole rounds: inside the sentinels, output starts at the FIRST round mark. + # + # That skip is also the migration path. Bodies written before the wrapper moved out have a + # `
`/`` pair and possibly a "N earlier round(s) dropped" note sitting + # inside the sentinels ahead of the first round; starting at the round mark discards them, + # so a corrupted archive heals on its first pass instead of needing a manual edit. The + # matching stray `
` is dropped by the tail filter below for the same reason. + awk -v s="$AGY_ARCHIVE_START" -v e="$AGY_ARCHIVE_END" -v r="$AGY_ROUND_MARK" ' $0 == e { inside = 0 } - inside { print } - $0 == s { inside = 1 }' || true + inside { buf[n++] = $0; if (first < 0 && $0 == r) first = n - 1 } + $0 == s { inside = 1 } + BEGIN { first = -1; n = 0 } + END { + # With round marks present, start at the first one: that discards a wrapper captured + # by an older layout. With NONE present the archive predates round marks entirely, so + # emit it verbatim -- dropping it would silently destroy the review history, which is + # a worse failure than the nesting this guards against. + start = (first >= 0) ? first : 0 + for (i = start; i < n; i++) print buf[i] + }' \ + | awk 'BEGIN { n = 0 } + { line[n++] = $0 } + END { + # Trim a trailing wrapper close (plus any blank lines before it) left by a + # pre-migration body. A legitimate round always ends with its own
+ # followed by nothing, so only ONE trailing close is ever removed here. + while (n > 0 && line[n-1] == "") n-- + if (n > 0 && line[n-1] == "
") { + closes = 0; opens = 0 + for (i = 0; i < n; i++) { + if (line[i] ~ /^
/) opens++ + if (line[i] == "
") closes++ + } + if (closes > opens) n-- + } + for (i = 0; i < n; i++) print line[i] + }' || true +} + +# Prefix of the hidden line that records when a review was posted. The comment head carries +# it so that, when the round is archived on the next run, the summary can say when it was +# REVIEWED; the time of archiving says nothing useful. +AGY_REVIEWED_AT_PREFIX='$/, "", s); print s; exit }' \ + <<< "$head")" + if [ -n "$when" ]; then + label="Round reviewed at $when" + else + label="Round archived at $fallback" + fi + printf '%s\n' "$AGY_ROUND_MARK" + printf '
\n%s\n\n' "$label" + printf '%s\n' "$head" + printf '\n
\n' + [ -n "$archive" ] || return 0 + # Here-string for the same reason: `grep -q` stops at the first match, and the resulting + # SIGPIPE would make the test false for a large archive and wrap it again on every run. + if grep -qxF -- "$AGY_ROUND_MARK" <<< "$archive"; then + printf '%s\n' "$archive" + else + printf '%s\n' "$AGY_ROUND_MARK" + printf '
\nEarlier rounds (archived before round markers)\n\n' + printf '%s\n' "$archive" + printf '\n
\n' + fi } # Delimits one archived round. Emitted by the caller ahead of each round's `
`. diff --git a/scripts/_agy_print.sh b/scripts/_agy_print.sh index df4cbab3..cdc47187 100755 --- a/scripts/_agy_print.sh +++ b/scripts/_agy_print.sh @@ -7,5 +7,25 @@ # (agy-review.sh passes it through `script -qfec` so agy runs attached to a PTY, # working around agy issue #76 where `-p` drops stdout on a non-TTY.) set -euo pipefail + +# `$# -eq 0` rather than `[ -z "${1:-}" ]`: both avoid the bare `$1` that aborts under `set -u` +# with "$1: unbound variable", but only the arity test distinguishes "no argument" from "an empty +# argument". `_agy_print.sh ""` is a caller passing a bad path, and it should get the readability +# error below rather than a usage message implying it passed nothing. This is the script a person +# is most likely to invoke by hand while debugging a review, so the diagnostics matter. +if [ $# -eq 0 ]; then + printf 'usage: %s [agy flags...]\n' "${0##*/}" >&2 + printf ' Reads the prompt from and execs `agy --print` with it.\n' >&2 + printf ' Normally invoked by agy-review.sh through `script -qfec`, not directly.\n' >&2 + exit 2 +fi +if [ ! -r "$1" ]; then + printf '%s: prompt file not readable: %s\n' "${0##*/}" "$1" >&2 + exit 2 +fi prompt_file="$1"; shift -exec "${AGY_BIN:-agy}" "$@" --print "$(cat "$prompt_file")" +# `$(&2; exit 1; } printf '%s\n' "$blk" | bash -n - 2>/dev/null \ @@ -76,7 +89,7 @@ FILTER="$(extract_filter)" # The script must actually USE the shared helper, or these checks test a file nothing runs. grep -q '_agy_comment_body.sh' "$SCRIPT_DIR/agy-review.sh" \ || { echo "FAIL: agy-review.sh does not source _agy_comment_body.sh" >&2; exit 1; } -for fn in agy_body_head agy_body_archive agy_drop_oldest_round; do +for fn in agy_body_head agy_body_archive agy_drop_oldest_round agy_assemble_archive; do grep -q "$fn" "$SCRIPT_DIR/agy-review.sh" \ || { echo "FAIL: agy-review.sh does not call $fn" >&2; exit 1; } done @@ -213,7 +226,9 @@ check "a
INSIDE a round is not mistaken for a round boundary" \ # The writer must actually EMIT the sentinel, or every archive is legacy-shaped and the trim # silently never fires -- the comment would then grow until the edit fails. -if grep -q 'AGY_ROUND_MARK' "$SCRIPT_DIR/agy-review.sh"; then +# It does so through agy_assemble_archive, whose output is checked to start with the mark. +if grep -q 'agy_assemble_archive' "$SCRIPT_DIR/agy-review.sh" \ + && [ "$(agy_assemble_archive X "" T | head -1)" = "$AGY_ROUND_MARK" ]; then echo " ok agy-review.sh emits the round sentinel" else echo " FAIL agy-review.sh never emits AGY_ROUND_MARK; the archive would never trim" @@ -254,6 +269,8 @@ trap 'rm -rf "$TMPD"' EXIT check "a bare backend 503 is caught" "MATCH" \ "$(se 'Error: Eligibility check failed: UNAVAILABLE (code 503): The service is currently unavailable.')" check "RESOURCE_EXHAUSTED is caught" "MATCH" "$(se 'Error: RESOURCE_EXHAUSTED')" +check "a bare truncated-output tool notice is caught" "MATCH" \ + "$(se '_The output was truncated. To view the remaining content, please use the `ContentOffset` parameter with the exact line range._')" # The false positives the anchoring exists to avoid. A genuine review may quote a 503 while # reviewing retry logic, and aborting on that would be the very failure the OAuth guard's design @@ -274,6 +291,69 @@ check "a heading MENTIONING an error mid-line is not caught" "NOMATCH" \ check "a leading-whitespace error is still caught" "MATCH" \ "$(se ' Error: UNAVAILABLE (code 503)')" +# REGRESSION, observed on CyberChef-MCP PR #72. The guard used to enumerate known backend +# signatures (UNAVAILABLE, RESOURCE_EXHAUSTED, DEADLINE_EXCEEDED, code 4xx/5xx...). A timeout is +# none of those, so agy posted this as its entire review and the `review` check went GREEN -- +# reproducing the exact failure the guard exists to prevent, one signature later. +check "a TIMEOUT error is caught (was not, before the guard stopped enumerating)" "MATCH" \ + "$(se 'Error: timeout waiting for response')" + +# The general form: any leading `Error:` is a failure, whatever follows it. Enumerating failure +# modes only ever catches the ones already seen, so the guard no longer tries. +check "an unfamiliar error signature is caught" "MATCH" \ + "$(se 'Error: something nobody has seen before')" + +# REGRESSION, observed on VeridianOS PR #5: agy under `unbuffer` printed the auth failure in +# lowercase, the case-sensitive guard missed it, and the error was posted as the review under a +# green check. Any capitalisation of the word is the same failure. +# --- truncation: executed, not grepped (CodeRabbit review, VeridianOS PR #5) ----- +# A diff cut by MAX_DIFF_BYTES, a prompt cut by MAX_PROMPT_BYTES, and a review cut to fit the comment +# limit must each leave the model -- or the reader -- a notice. These run the real helpers. +printf '0123456789abcdefghij' > "$TMPD/diff" +check "the diff cap cuts an oversized diff" "cut:5" \ + "$(agy_cut_file "$TMPD/diff" 5 && echo "cut:$(wc -c < "$TMPD/diff")")" +check "the diff cap leaves a small diff alone" "kept" \ + "$(agy_cut_file "$TMPD/diff" 50 && echo cut || echo kept)" +{ printf 'PROMPT\n--- UNIFIED DIFF ---\n'; cat "$TMPD/diff"; agy_diff_truncation_notice 5; } > "$TMPD/prompt" +check "a truncated diff is disclosed in the prompt" "yes" \ + "$(grep -q 'NOTE: TRUNCATED DIFF' "$TMPD/prompt" && grep -q 'first 5 bytes' "$TMPD/prompt" && echo yes || echo no)" +head -c 2000 /dev/zero | tr '\0' 'x' > "$TMPD/bigprompt" +agy_diff_truncation_notice 5 >> "$TMPD/bigprompt" +agy_cap_prompt "$TMPD/bigprompt" 600 +check "the prompt cap stays within its budget" "yes" \ + "$([ "$(wc -c < "$TMPD/bigprompt")" -le 600 ] && echo yes || echo no)" +check "the prompt cap's own notice survives the cut" "yes" \ + "$(tail -n 4 "$TMPD/bigprompt" | grep -q 'NOTE: TRUNCATED PROMPT' && echo yes || echo no)" +head -c 70000 /dev/zero | tr '\0' 'r' > "$TMPD/round" +agy_cap_round "$TMPD/round" 1000 +check "an oversized review round is capped" "yes" \ + "$([ "$(wc -c < "$TMPD/round")" -le 1000 ] && echo yes || echo no)" +check "a capped review round says it was truncated" "yes" \ + "$(grep -q 'Review truncated' "$TMPD/round" && echo yes || echo no)" +# --- UTF-8 scrub: one helper for the prompt AND the review output, executed ------- +# The review-output path once had iconv only while the prompt path also fell back to python3 +# (agy review, switch2-hub-patcher PR #1). Both now call agy_scrub_utf8; exercise each sanitizer. +printf 'abc\xe2\x82' > "$TMPD/split" # a euro sign cut after two of its three bytes +agy_scrub_utf8 "$TMPD/split" +check "a split UTF-8 sequence is scrubbed" "abc" "$(cat "$TMPD/split")" +mkdir -p "$TMPD/noiconv" +for t in python3 mv rm; do ln -sf "$(command -v "$t")" "$TMPD/noiconv/$t"; done +printf 'abc\xe2\x82' > "$TMPD/split" +( PATH="$TMPD/noiconv"; agy_scrub_utf8 "$TMPD/split" ) +check "the python3 fallback scrubs when iconv is absent" "abc" "$(cat "$TMPD/split")" +printf '\xe2\x82' > "$TMPD/allbad" # no valid byte: every sanitizer yields nothing +agy_scrub_utf8 "$TMPD/allbad" +check "a sanitizer's empty result never replaces the file" "2" "$(wc -c < "$TMPD/allbad" | tr -d ' ')" +check "both byte-cap paths call the shared scrub" "2" \ + "$(grep -c '^ *agy_scrub_utf8 "\$' "$SCRIPT_DIR/agy-review.sh")" +# A failed jq step in the archive lookup must be logged, not swallowed. +check "archive jq failures are logged" "no" \ + "$(grep -n 'SELECT_OURS_JQ" 2>/dev/null' "$SCRIPT_DIR/agy-review.sh" >/dev/null && echo yes || echo no)" + +check "a lowercase error (agy under unbuffer) is caught" "MATCH" \ + "$(se 'error: Eligibility check failed: PERMISSION_DENIED (code 403): Request had insufficient authentication scopes.. Please log out (/logout) and log back in (/login).')" +check "an all-caps ERROR: is caught" "MATCH" "$(se 'ERROR: backend unavailable')" + # The size cap is what separates "the error IS the whole capture" from "a review mentions one". long="Error: UNAVAILABLE (code 503) $(head -c 3000 /dev/zero | tr '\0' 'x')" check "a long capture opening with an error is not caught" "NOMATCH" "$(se "$long")" @@ -290,6 +370,187 @@ check "outage + a review -> do not fail" "1" \ check "no outage + no review -> not THIS guard's job" "1" \ "$(backend_outage_should_fail 0 "$TMPD/empty"; echo $?)" + +# --- duration parser --------------------------------------------------------------------------- +# The point is the REJECTIONS. A value that is not a duration must be refused, not fed into an +# arithmetic expansion: `$(( 1x + 60 ))` is a SYNTAX ERROR that takes the whole script down under +# `set -e`, which would turn a mis-set variable into a reviewer that never runs. +check "duration: bare seconds" "90" "$(duration_to_seconds 90)" +check "duration: explicit seconds" "90" "$(duration_to_seconds 90s)" +check "duration: minutes" "300" "$(duration_to_seconds 5m)" +check "duration: hours" "3600" "$(duration_to_seconds 1h)" +check "duration: leading zero minutes" "480" "$(duration_to_seconds 08m)" +check "duration: leading zero seconds" "9" "$(duration_to_seconds 09s)" +# 10, not 8. That IS the fix: without `10#` bash reads the leading zero as octal, so this would +# silently mean 8 seconds -- a wrong answer rather than an error, which is the worse of the two. +check "duration: leading zero bare" "10" "$(duration_to_seconds 010)" +for bad in m s "" 1m2s 5x -3 " 5m" 5M; do + check "duration: rejects '$bad'" "1" "$(duration_to_seconds "$bad" >/dev/null 2>&1; echo $?)" +done + +# --- numeric env validation --------------------------------------------------------------------- +# These values reach `$(( ... ))`. Two separate ways that goes wrong, and digits-only catches only +# one of them -- which is exactly how the octal case survived the first version of this guard. +nne() { # run the real function, echo what the variable became + local v="$1"; local T="$v" + normalise_numeric_env T 240 >/dev/null 2>&1 || echo "CRASHED" + printf '%s' "$T" +} +check "numeric env: plain integer passes through" "300" "$(nne 300)" +check "numeric env: zero is a legal setting" "0" "$(nne 0)" +# 9, not a crash and not 8: `09` is all digits, so the digits-only check admits it, and bash then +# reads the leading zero as OCTAL. This is the case the digits-only check alone did NOT cover. +check "numeric env: leading zero canonicalises" "9" "$(nne 09)" +check "numeric env: many leading zeros" "8" "$(nne 0008)" +check "numeric env: empty falls back" "240" "$(nne "")" +check "numeric env: non-numeric falls back" "240" "$(nne 30s)" +check "numeric env: negative falls back" "240" "$(nne -1800)" +# Bash arithmetic recursively expands variable CONTENTS as a name, so a value that names another +# variable silently evaluates to that one's value. It must never reach `$(( ))` at all. +check "numeric env: a variable name falls back" "240" "$(nne a_name)" +# Asserted on STDERR, not on the returned value: reaching `$(( ))` with a non-numeric value emits +# a bash arithmetic error there, so an empty stderr is positive evidence that it never got that +# far. Mutation-checked -- deleting the digits-only case above makes THIS check fail with +# "value too great for base", which is what a guard's test is supposed to do. +# +# The previous version of this check grepped for a command-substitution payload and was VACUOUS: +# it passed under a deliberately broken guard, because the helper's own `2>&1 >/dev/null` had +# already swallowed the evidence. Kept as a note because a test that cannot fail is worse than +# no test -- it reads as coverage. +check "numeric env: a non-numeric value never reaches arithmetic" "" \ + "$( { T=a_name; normalise_numeric_env T 240; } 2>&1 >/dev/null || true )" +# It assigns THROUGH a caller-supplied name, so its own locals must not collide with one. With +# plain `name`/`default`/`val` locals these two silently no-op -- the function reads and writes its +# own local and the caller's variable is never touched. Verified as a real failure before fixing. +name=09; normalise_numeric_env name 240 +check "numeric env: a caller variable named 'name' is not shadowed" "9" "$name" +val=07; normalise_numeric_env val 240 +check "numeric env: a caller variable named 'val' is not shadowed" "7" "$val" +default=08; normalise_numeric_env default 240 +check "numeric env: a caller variable named 'default' is not shadowed" "8" "$default" + +# --- diff-size scaling ------------------------------------------------------------------------- +# The argument is stripped of whitespace BEFORE validation, so a padded `wc` count still scales. +# Validating first would fall back to 0 and silently disable the scaling -- a no-op is worse here +# than the crash, because the symptom is the timeout this whole feature exists to prevent. +sc() { AGY_PRINT_TIMEOUT="5m"; scale_timeout_for_diff "$1"; printf '%s' "$AGY_PRINT_TIMEOUT"; } +AGY_PRINT_TIMEOUT_EXPLICIT=""; AGY_TIMEOUT_SECONDS_PER_MIB=240; AGY_PRINT_TIMEOUT_MAX_SECONDS=1800 +check "scaling: 1.6 MB gets a real raise" "670s" "$(sc 1619782)" +check "scaling: a padded count still scales" "670s" "$(sc ' 1619782 ')" +check "scaling: a huge diff hits the ceiling" "1800s" "$(sc 20000000)" +check "scaling: no argument leaves the base" "5m" "$(sc '')" +# The default (900 s/MiB): the 426 KB diff that timed out three times at 397 s under 240. +AGY_TIMEOUT_SECONDS_PER_MIB="$(sed -n 's/^AGY_TIMEOUT_SECONDS_PER_MIB="${AGY_TIMEOUT_SECONDS_PER_MIB:-\([0-9]*\)}".*/\1/p' "$SCRIPT_DIR/agy-review.sh")" +check "scaling: the default gives a 426 KB diff 665 s" "665s" "$(sc 425786)" +AGY_TIMEOUT_SECONDS_PER_MIB=240 +check "scaling: junk leaves the base" "5m" "$(sc junk)" +AGY_PRINT_TIMEOUT_EXPLICIT=set +check "scaling: an explicit timeout is not touched" "5m" "$(sc 20000000)" +AGY_PRINT_TIMEOUT_EXPLICIT="" + +# And the value it produces must survive the arithmetic it exists to feed. +T=09; normalise_numeric_env T 240 +# 9. Feeding the RAW `09` to the same expansion dies "value too great for base" under `set -e`, +# which is the crash this canonicalisation exists to prevent -- asserted directly below. +check "numeric env: canonical value is arithmetic-safe" "9" \ + "$(bash -c 'set -e; echo $(( 1048576 * '"$T"' / 1048576 ))' 2>/dev/null || echo CRASHED)" +check "numeric env: the RAW value would have crashed" "CRASHED" \ + "$(bash -c 'set -e; echo $(( 1048576 * 09 / 1048576 ))' 2>/dev/null || echo CRASHED)" + +# --- the 406 diff-limit classifier ------------------------------------------------------- +# GitHub's two "too big for the API" refusals must BOTH reach the local-diff fallback, and the +# log must name the limit that fired. Matching only the lines variant failed a wide-but-shallow +# PR outright; the label is what a reader triages from. Anything else is a real failure. +dl() { printf '%s\n' "$1" > "$TMPD/derr"; diff_limit_hit "$TMPD/derr" || printf 'NO-FALLBACK'; } +check "406, lines variant: falls back, named 20,000-line" "20,000-line" \ + "$(dl 'could not find pull request diff: HTTP 406: Sorry, the diff exceeded the maximum number of lines (20000)')" +check "406, files variant: falls back, named 300-file" "300-file" \ + "$(dl 'could not find pull request diff: HTTP 406: Sorry, the diff exceeded the maximum number of files (300)')" +check "an unrelated gh error does not fall back" "NO-FALLBACK" \ + "$(dl 'could not find pull request diff: HTTP 404: Not Found')" + +# --- the print-timeout guard ------------------------------------------------------------- +# agy exits 0 on its own --print-timeout and prints a notice, alone or after a partial review. +# `have_text` passed that, so a truncated review was posted as the whole one (2026-09-28). +pt() { printf '%s' "$1" > "$TMPD/cap"; print_timeout_present "$TMPD/cap" && echo MATCH || echo NOMATCH; } +check "a bare timeout notice is caught" "MATCH" \ + "$(pt '[agy] print timeout after 5m0s with turn in progress; returning partial output')" +check "a partial review ending in the notice is caught" "MATCH" \ + "$(pt "$(printf '## Review\n\nSome findings\n[agy] print timeout after 5m0s with turn in progress; returning partial output\n')")" +check "a review quoting the notice mid-line is not caught" "NOMATCH" \ + "$(pt 'The log showed [agy] print timeout after 5m0s once.')" +check "an empty capture is not a timeout" "NOMATCH" "$(pt '')" + +# --- no `exec` may redirect to /dev/null ------------------------------------------------- +# A legacy (markerless) archive behind a new round: trimming once must drop the LEGACY +# history and keep the new round. Assembled bare, the only marker was the new round's, so the +# cut landed on it and discarded everything while reporting one round dropped. +legacy_combined="$(agy_assemble_archive "NEW" "$(printf '
\nLEGACY\n
')" "T0")" +check "a legacy archive becomes one droppable round behind the new one" "NEW" \ + "$(printf '%s\n' "$legacy_combined" | agy_drop_oldest_round | grep -x 'NEW\|LEGACY')" + +# The archived summary says when the round was REVIEWED (from the hidden line in its head), +# falling back to an honest "archived at" for a head that predates the line. +check "an archived round keeps its reviewed-at time" "1" \ + "$(agy_assemble_archive "$(printf '%s2026-10-06 12:34 UTC -->\nBODY' "$AGY_REVIEWED_AT_PREFIX")" "" "LATER" \ + | grep -c -F 'Round reviewed at 2026-10-06 12:34 UTC')" +check "a head without the line is labelled archived-at" "1" \ + "$(agy_assemble_archive "BODY" "" "LATER" | grep -c -F 'Round archived at LATER')" +if grep -q 'AGY_REVIEWED_AT_PREFIX' "$SCRIPT_DIR/agy-review.sh"; then + echo " ok the posted head records its reviewed-at time" +else + echo " FAIL agy-review.sh no longer writes the reviewed-at line" + fails=$((fails + 1)) +fi + +# The job timeout must exceed the script's worst case: lock wait + every attempt at its cap + +# the linear backoff between attempts (delay x 1 + delay x 2 + ...). Read from the workflow +# and the script's defaults, so changing either without the other fails here. +wf="$SCRIPT_DIR/../.github/workflows/antigravity-review.yml" +wf_val() { sed -n "s/^[[:space:]]*$1:[[:space:]]*[\"']\{0,1\}\([0-9][0-9]*\).*/\1/p;T;q" "$wf"; } +sh_default() { sed -n "s/^$1=\"\${$1:-\([0-9][0-9]*\)}\".*/\1/p;T;q" "$SCRIPT_DIR/agy-review.sh"; } +job_min="$(wf_val timeout-minutes)" +max_s="$(wf_val AGY_PRINT_TIMEOUT_MAX_SECONDS)"; [ -n "$max_s" ] || max_s="$(sh_default AGY_PRINT_TIMEOUT_MAX_SECONDS)" +lock_s="$(wf_val AGY_LOCK_WAIT)"; [ -n "$lock_s" ] || lock_s="$(sh_default AGY_LOCK_WAIT)" +retries="$(wf_val AGY_RETRIES)"; [ -n "$retries" ] || retries="$(sh_default AGY_RETRIES)" +delay="$(wf_val AGY_RETRY_DELAY)"; [ -n "$delay" ] || delay="$(sh_default AGY_RETRY_DELAY)" +if [ -z "$job_min" ] || [ -z "$max_s" ] || [ -z "$lock_s" ] || [ -z "$retries" ] \ + || [ -z "$delay" ]; then + echo " FAIL could not read the timeout budget (job=$job_min max=$max_s lock=$lock_s retries=$retries delay=$delay)" + fails=$((fails + 1)) +else + worst=$(( lock_s + retries * max_s + delay * retries * (retries - 1) / 2 )) + if [ "$worst" -lt $(( job_min * 60 - 120 )) ]; then + echo " ok worst case ${worst}s fits the ${job_min}-minute job timeout with 2 minutes to spare" + else + echo " FAIL worst case ${worst}s does not fit the ${job_min}-minute job timeout" + fails=$((fails + 1)) + fi +fi + +# Both helpers run under the caller's `set -euo pipefail`. A head or archive far larger than a +# pipe buffer must neither abort them nor be misread (an early-exiting reader used to SIGPIPE +# the writer). 300 KB of filler around the markers. +big="$(head -c 300000 /dev/zero | tr '\0' 'x' | fold -w 100)" +check "a huge head still yields its reviewed-at time under pipefail" "Round reviewed at T1" \ + "$( (set -euo pipefail + agy_assemble_archive "$(printf '%sT1 -->\n%s' "$AGY_REVIEWED_AT_PREFIX" "$big")" "" "X") \ + | grep -o 'Round reviewed at T1' | head -1)" +check "a huge marked archive is not wrapped again under pipefail" "2" \ + "$( (set -euo pipefail + agy_assemble_archive "NEW" "$(printf '%s\nOLD\n%s' "$AGY_ROUND_MARK" "$big")" "X") \ + | grep -c -x -F "$AGY_ROUND_MARK")" + +# A redirection on a bare `exec` applies to the rest of the script. `exec 9>&- 2>/dev/null` +# after the retry loop sent every later `log` line (stderr) to /dev/null, so a posted review, an +# updated one and every failure looked identical in CI: nothing logged, green check. +if grep -nE '^[[:space:]]*exec[[:space:]][^#]*>[[:space:]]*/dev/null' "$SCRIPT_DIR/agy-review.sh" >/dev/null; then + echo " FAIL an \`exec\` redirects to /dev/null (silences every later log line)" + fails=$((fails + 1)) +else + echo " ok no \`exec\` redirects to /dev/null" +fi + if [ "$fails" -ne 0 ]; then echo "$fails check(s) failed" >&2 exit 1 diff --git a/scripts/agy-review.sh b/scripts/agy-review.sh index 656e760a..72aa558a 100755 --- a/scripts/agy-review.sh +++ b/scripts/agy-review.sh @@ -10,7 +10,8 @@ # Flow: resolve PR -> `gh pr diff` -> build an adversarial-reviewer prompt # (+ repo style guide) -> `agy --print` under a PTY -> post via `gh pr comment`. # -# See ../README.md for setup, the issue #76 PTY workaround, and the ToS caveat. +# See README.md (installed into target repos as .github/AGY-REVIEWER.md) for setup, the issue #76 +# PTY workaround, and the ToS caveat. set -euo pipefail # Helpers are defined FIRST: the configuration block below calls log() from the MAX_PROMPT_BYTES @@ -42,9 +43,35 @@ log() { printf '[agy-review] %s\n' "$*" >&2; } # general helper used throughout the script -- the marker is a comment, so its position # changes nothing about where the function is defined. have_text() { [ -s "$1" ] && grep -q '[^[:space:]]' "$1"; } -AGY_ERROR_RE='^[[:space:]]*Error:.*(Eligibility check failed|UNAVAILABLE|unavailable|RESOURCE_EXHAUSTED|INTERNAL|DEADLINE_EXCEEDED|code [45][0-9]{2})' -# Captures longer than this are assumed to be real reviews even if they open with an error line: -# a genuine review is thousands of bytes, a bare backend error is a couple of hundred. +# Any `Error:` on line 1 of a short capture -- NOT an enumeration of known backend messages. +# +# This was previously a list: Eligibility check failed | UNAVAILABLE | RESOURCE_EXHAUSTED | +# INTERNAL | DEADLINE_EXCEEDED | code 4xx/5xx. That list is unmaintainable by construction, +# and it failed exactly as you would expect. Observed on CyberChef-MCP PR #72, where agy posted +# this as its entire review and the `review` check went GREEN: +# +# ## Antigravity review (Gemini via Ultra) +# +# Error: timeout waiting for response +# +# A timeout is not in the list, so the guard let it through -- reproducing the precise failure +# the guard exists to prevent, one signature later. Enumerating failure modes only ever catches +# the ones already seen. +# +# Inverted: treat ANY leading `Error:` as a failure. The false-positive protection does not come +# from the message text at all, and never did -- it comes from the two conditions below, which +# are unchanged: the error must be on LINE 1, and the whole capture must be under +# AGY_ERROR_MAX_BYTES. The load-bearing half is the LINE 1 anchor: a review does not *open* +# with a bare "Error:" line, however short it is. +# Case-insensitive on the word itself: under `unbuffer` agy 1.2.x prints its eligibility failure +# as lowercase `error: Eligibility check failed: PERMISSION_DENIED (code 403) ...`, and the +# case-sensitive `Error:` let it through. Observed on VeridianOS PR #5: that line was posted as +# the entire review and the `review` check went green in 14 seconds. +AGY_ERROR_RE='^[[:space:]]*[Ee][Rr][Rr][Oo][Rr]:' +# Secondary safety net, not the primary discriminator. A backend failure IS the whole capture and +# is a couple of hundred bytes; the cap only ensures that if a review ever did open with an +# "Error:" line, length alone keeps it from being discarded. Reviews shorter than this exist, so +# the cap is deliberately generous rather than a claim about minimum review length. AGY_ERROR_MAX_BYTES="${AGY_ERROR_MAX_BYTES:-2000}" # `head -n 1`, not `head -n 5`. grep matches line-by-line, so scanning five lines # means "any of the first five lines is an error line" -- which would discard a short, @@ -52,10 +79,16 @@ AGY_ERROR_MAX_BYTES="${AGY_ERROR_MAX_BYTES:-2000}" # UNAVAILABLE` on line 3. That is the false positive the anchoring was supposed to # prevent, reintroduced by the very check meant to enforce it. The invariant is that a # backend failure IS the whole capture, so only the first line can carry it. +# A tool notice from agy's own file viewer, returned as the whole answer when the model stopped +# after a truncated read instead of writing a review. Observed on VeridianOS PR #15: the posted +# "review" was only `_The output was truncated. To view the remaining content, please use the +# ContentOffset parameter ..._`, under a green check. Same rules as the error line: line 1 of a +# short capture. +AGY_TOOL_NOTICE_RE='^[[:space:]_*]*The output was truncated\.' service_error_present() { [ -s "$1" ] || return 1 [ "$(wc -c < "$1")" -le "$AGY_ERROR_MAX_BYTES" ] || return 1 - head -n 1 "$1" | grep -qE "$AGY_ERROR_RE" + head -n 1 "$1" | grep -qE "$AGY_ERROR_RE|$AGY_TOOL_NOTICE_RE" } # True when the run must FAIL rather than post: the backend errored at least once and no review @@ -70,6 +103,16 @@ backend_outage_should_fail() { [ "${1:-0}" -gt 0 ] || return 1 ! have_text "$2" } + +# agy's own --print-timeout marker. When the turn is still running at the timeout, agy does NOT +# fail: it exits 0 and prints `[agy] print timeout after 5m0s with turn in progress; returning +# partial output`, alone or after whatever it had written so far. That capture is non-empty, so +# `have_text` passed it and it was posted AS the review -- a truncated review, or a bare timeout +# notice, under a green check. Measured 2026-09-28 (agy 1.2.12): a 10 s timeout on a long prompt +# exits 0 with exactly that line as the whole 78-byte capture. Anchored at a line start so a +# review that merely quotes the message mid-sentence is not discarded. +AGY_TIMEOUT_RE='^\[agy\] print timeout after ' +print_timeout_present() { [ -s "$1" ] && grep -qE "$AGY_TIMEOUT_RE" "$1"; } # <<< SELFTEST-EXTRACT # Guarding against a leak of agy's interactive Google OAuth login flow. When agy's cached # session lapses on the runner, `--print` emits the login prompt (a live OAuth URL + "paste the @@ -115,7 +158,60 @@ AGY_BIN="${AGY_BIN:-agy}" command -v "$AGY_BIN" >/dev/null 2>&1 || AGY_BIN="$HOME/.local/bin/agy" AGY_MODEL="${AGY_MODEL:-}" # empty = agy's configured default (Gemini 3.x Pro) AGY_EFFORT="${AGY_EFFORT:-high}" # low|medium|high +# Base timeout for a normal review. Scaled up for a large diff further down -- see +# `scale_timeout_for_diff`. Set AGY_PRINT_TIMEOUT explicitly to pin it and skip the scaling. +AGY_PRINT_TIMEOUT_EXPLICIT="${AGY_PRINT_TIMEOUT:+set}" AGY_PRINT_TIMEOUT="${AGY_PRINT_TIMEOUT:-5m}" +# Seconds of budget per MiB of diff handed to agy, on top of the base. Reading a 1.6 MB patch and +# reasoning over it is not work a five-minute budget can absorb, and the failure is indistinguishable +# from a backend outage: `Error: timeout waiting for response`, three times, with no review. +# +# 900, not the 240 this started with: a diff handed over as a FILE costs agy 6-8 minutes almost +# whatever its size (reading it through its tools at high effort is mostly fixed cost). Measured on +# VeridianOS: a 1.15 MB diff took 465 s, and a 426 KB one timed out three times at 300 + 240 x 0.41 +# = 397 s. At 900 s/MiB the 426 KB diff gets 665 s and anything over ~650 KB the cap. +AGY_TIMEOUT_SECONDS_PER_MIB="${AGY_TIMEOUT_SECONDS_PER_MIB:-900}" +AGY_PRINT_TIMEOUT_MAX_SECONDS="${AGY_PRINT_TIMEOUT_MAX_SECONDS:-1800}" + +# Both of the above reach `$(( ... ))`, so both are validated before they get there. +# >>> SELFTEST-EXTRACT: numeric env validation +# Validate and canonicalise a numeric setting that will reach an arithmetic expansion. +# +# @param $1 name of the variable to validate, assigned in place. +# @param $2 default to fall back to when the value is not a non-negative decimal integer. +# +# Two hazards, and the honest status of each is different -- so they are recorded separately +# rather than under one "unsafe input" heading. +# +# 1. Bash arithmetic recursively expands variable CONTENTS as a name, so `$(( V ))` with V=a and +# a=5 yields 5, at any depth. Contents that are a command substitution are NOT executed on +# bash 5.3 (measured here): they reach the parser as a literal and are refused with +# "arithmetic syntax error: operand expected". This was reported in review as a CWE-78, and an +# earlier version of this comment asserted the execution -- WRONGLY, from a nested-quoting +# artefact in the test that ran it. It does not reproduce. What is left is still worth +# refusing: a value naming another variable silently means something other than what it says. +# 2. Digits-only is not sufficient on its own. `09` is all digits, and bash reads the leading +# zero as OCTAL: +# $(( 1000000 * 09 / 1048576 )) -> value too great for base +# So a *valid* setting takes the script down under `set -e`. That one is demonstrated, not +# theoretical, and is the reason this function exists. Canonicalising to base 10 here, once, +# means no downstream `$(( ))` has to remember `10#`. +# The locals are `_nne_`-prefixed because this function assigns THROUGH A NAME the caller supplies. +# Plain `name`/`val` locals shadow a caller variable of the same name, and the failure is silent -- +# `normalise_numeric_env val 240` reads and writes the local, leaving the caller's `val` untouched. +# Verified: with unprefixed locals, `val=07; normalise_numeric_env val 240` leaves val as `07`. +normalise_numeric_env() { + local _nne_name="$1" _nne_default="$2" _nne_val="${!1}" + case "$_nne_val" in + ""|*[!0-9]*) + log "$_nne_name ('$_nne_val') is not a non-negative integer; using the default ($_nne_default)" + printf -v "$_nne_name" '%s' "$_nne_default" ;; + *) printf -v "$_nne_name" '%s' "$(( 10#$_nne_val ))" ;; # digits-only, verified above + esac +} +# <<< SELFTEST-EXTRACT +normalise_numeric_env AGY_TIMEOUT_SECONDS_PER_MIB 900 +normalise_numeric_env AGY_PRINT_TIMEOUT_MAX_SECONDS 1800 AGY_DIFF_MODE="${AGY_DIFF_MODE:-auto}" # auto|inline|file. A diff is passed to agy either inlined # in the --print prompt, or written to a FILE agy reads with # its own tools. `auto` inlines a diff that fits under the @@ -130,9 +226,15 @@ MAX_PROMPT_BYTES="${MAX_PROMPT_BYTES:-125000}" # hard ceiling on the INLINED pro # Linux). Over it, execve fails with E2BIG before agy even # starts. In `auto` mode this is the inline/file threshold; # it also backstops the assembled prompt in every mode. -ARG_SIZE_CEILING=128000 # hard cap: a configured MAX_PROMPT_BYTES above the +ARG_SIZE_CEILING=120000 # hard cap: a configured MAX_PROMPT_BYTES above the # MAX_ARG_STRLEN-derived safe bound would defeat the guard # and re-expose E2BIG, so clamp any override down to it. + # 120000, not 128000: MAX_ARG_STRLEN is 131072, and the + # prompt is not the only thing execve must fit - argv[0], + # the other flags and the whole environment count against + # the limit too. A ceiling 3 KB under it left no room for + # them, so a large environment could still hit E2BIG on a + # prompt the guard had just declared safe. # Require a POSITIVE integer at or below the ceiling. The `-gt 0` half is load-bearing, not # cosmetic: a negative override (e.g. MAX_PROMPT_BYTES=-1) satisfies `-le "$ARG_SIZE_CEILING"`, # so without it the clamp is skipped and the `head -c "$MAX_PROMPT_BYTES"` prompt cap below runs @@ -151,8 +253,14 @@ STYLE_GUIDE="${STYLE_GUIDE:-.github/agy-review.md}" # repo-relative; loaded if LOG="${RUNNER_TEMP:-/tmp}/agy-review-${GITHUB_RUN_ID:-$$}.log" AGY_LOCK="${AGY_LOCK:-$HOME/.gemini/antigravity-cli/.agy-review.lock}" AGY_LOCK_WAIT="${AGY_LOCK_WAIT:-600}" # seconds to wait for the agy lock before proceeding +normalise_numeric_env AGY_LOCK_WAIT 600 # flock -w rejects a non-numeric value and aborts AGY_RETRIES="${AGY_RETRIES:-3}" # attempts to get a usable agy response AGY_RETRY_DELAY="${AGY_RETRY_DELAY:-15}" # base backoff seconds between retries (grows per attempt) +# Both reach (( )) arithmetic: `08` would abort on an octal parse and `0` retries would skip every +# attempt and report "no review output after 0 attempt(s)". Same validation as the other numerics. +normalise_numeric_env AGY_RETRIES 3 +normalise_numeric_env AGY_RETRY_DELAY 15 +[ "$AGY_RETRIES" -ge 1 ] || AGY_RETRIES=1 MARKER="" # The comment body's format -- sentinels plus the split/trim helpers -- lives in a sourceable @@ -174,6 +282,18 @@ MAX_BODY_BYTES="${MAX_BODY_BYTES:-60000}" # (an HTML comment, invisible when rendered) in a PR comment and have this bot edit it. Only # ever touch our own bot's comments. `first` picks the OLDEST match, so if duplicates exist # from an older version of this script, the canonical thread is the one that keeps growing. +# +# COUPLING, stated because it is invisible otherwise: the `github-actions[bot]` login pin assumes +# the review is posted with `GITHUB_TOKEN` from Actions, which is the only path the workflow has. +# Move this to a GitHub App or a PAT and the filter stops finding its own comments — it will post +# a fresh review every round instead of editing one, and the archive stops accumulating. +# +# Do not "fix" that in advance by dropping the login and matching on `.user.type == "Bot"` plus the +# marker. Under a PAT the comment's type is `"User"`, not `"Bot"`, so that clause fails in the same +# scenario; and the marker is an HTML comment, so any bot that quotes or summarises a PR comment +# carries it along, which would let this script PATCH over ANOTHER bot's comment. Losing a review +# that way is worse than posting a duplicate. Change this filter when the auth changes, not before, +# and match on whatever identity the new credential actually presents. # >>> SELFTEST-EXTRACT: ours-comment filter SELECT_OURS_JQ='[ .[] | select(.user.type == "Bot" and .user.login == "github-actions[bot]") @@ -185,6 +305,57 @@ readonly SELECT_OURS_JQ REPO="${GITHUB_REPOSITORY:?GITHUB_REPOSITORY not set}" +# Does $1 actually hold write access to this repository? +# +# `author_association` is NOT a permission check, which is the trap this replaces. It is a +# relationship label: a user granted only the *Triage* role reports as COLLABORATOR while +# holding no write access at all, and OWNER/MEMBER describe org membership rather than +# repository rights. On a self-hosted runner — someone's actual workstation — "may this +# person schedule execution here" has to be answered by the permissions API, not inferred +# from a label that was never meant to answer it. +# +# Fails CLOSED: any API error, missing token, or unrecognised role denies. A reviewer that +# stops running is a visible annoyance; one that runs for the wrong person is not. +# +# But fail-closed makes the error TEXT load-bearing, which is why stderr is captured and logged +# rather than sent to /dev/null. Denying and swallowing the reason makes two very different +# situations look identical in the log: "this person genuinely has no write access" (working as +# intended, one user affected) and "the token lost its scope / the API is rate-limited" (the +# reviewer is now dead for EVERYONE and nothing says so). The first is a decision; the second is +# an outage wearing a decision's clothes, and it would sit undiagnosed until someone noticed +# reviews had quietly stopped. +agy_has_write_access() { + _login="${1:-}" + [ -n "$_login" ] || { log "no login to check for write access"; return 1; } + _perm_err="$(mktemp)" + # URL-encode the login before it becomes a path segment, the same way + # base_ref is handled below. GitHub logins are restricted in practice, but + # this value arrives from event payload rather than from us, and an unexpected + # character would corrupt the path and fail into the error branch as if the + # user simply had no write access - a silent wrong answer on a permission + # check, which is the worst place to have one. + _login_enc="$(jq -rn --arg v "$_login" '$v|@uri')" + if _perm="$(gh api "repos/${REPO}/collaborators/${_login_enc}/permission" \ + --jq '.permission // empty' 2>"$_perm_err")"; then + rm -f "$_perm_err" + else + # Redacted before logging. gh does not echo credentials in its error text today, but this + # log lands in a PUBLIC Actions log, and "today" is not a property worth betting a token on. + log "permission lookup for '${_login}' FAILED: $(tr '\n' ' ' < "$_perm_err" \ + | sed -E 's/(gh[pousr]|github_pat)_[A-Za-z0-9_]+//g' \ + | cut -c1-400)" + log "denying by default. A 404 here is an ordinary non-collaborator; a 401/403 or a rate" + log "limit means the token is the problem and EVERY review will skip until it is fixed." + rm -f "$_perm_err" + return 1 + fi + case "$_perm" in + admin|maintain|write) return 0 ;; + *) log "user '${_login}' has repository permission '${_perm:-}'; write required" + return 1 ;; + esac +} + # --- resolve the PR number from the triggering event -------------------------- case "${GITHUB_EVENT_NAME:-}" in pull_request|pull_request_target) @@ -193,20 +364,20 @@ case "${GITHUB_EVENT_NAME:-}" in issue_comment) is_pr="$(jq -r '.issue.pull_request // empty' "$GITHUB_EVENT_PATH")" body="$(jq -r '.comment.body // ""' "$GITHUB_EVENT_PATH")" - assoc="$(jq -r '.comment.author_association // ""' "$GITHUB_EVENT_PATH")" + commenter="$(jq -r '.comment.user.login // ""' "$GITHUB_EVENT_PATH")" [ -n "$is_pr" ] || { log "comment is not on a PR; skipping"; exit 0; } case "$body" in /agy-review*) : ;; *) log "comment is not an /agy-review command; skipping"; exit 0 ;; esac - # Defense in depth: the workflow `if:` already gates on author_association, but this - # script is also runnable by hand and by any future caller, so re-check here that the - # commenter has write access. A stranger's `/agy-review` must never schedule an agy - # run on the self-hosted host — losing this to a workflow-only gate would be silent. - case "$assoc" in - OWNER|MEMBER|COLLABORATOR) : ;; - *) log "comment author association '$assoc' lacks write access; skipping"; exit 0 ;; - esac + # THE authoritative permission gate, not defence in depth. The workflow `if:` can only + # filter on `author_association`, which the payload carries but which does not answer the + # question being asked (see `agy_has_write_access`). Treat that `if:` as a cheap + # pre-filter that avoids booting a runner for obvious strangers, and settle the actual + # decision here, where an API call is possible. A stranger's `/agy-review` must never + # schedule an agy run on the self-hosted host. + agy_has_write_access "$commenter" \ + || { log "commenter '${commenter}' lacks write access; skipping"; exit 0; } PR="$(jq -r '.issue.number' "$GITHUB_EVENT_PATH")" ;; *) @@ -214,11 +385,18 @@ case "${GITHUB_EVENT_NAME:-}" in [ -n "$PR" ] || { log "unknown event; pass a PR number as \$1"; exit 1; } ;; esac +# $PR is interpolated into API paths, refspecs (`refs/agy/pr-${PR}`) and a `gh` invocation, so it +# is validated once here rather than trusted at each of those sites. From an event payload it is a +# jq-parsed number and safe; from the `*)` branch above it is `$1`, i.e. whatever a hand-run or a +# future caller passed. "Practically safe" is a property of today's callers, not of the variable. +case "$PR" in + ''|*[!0-9]*) log "invalid PR number '${PR}'; expected digits only"; exit 1 ;; +esac log "reviewing ${REPO}#${PR}" # Remove every temp file on exit. Pre-declared so the trap is safe under `set -u` even if the # script exits before a given file is created. -diff_file= diff_err= meta_file= prompt_file= out_file= raw= body_file= agy_diff_file= agy_work_dir= +diff_file= diff_err= meta_file= prompt_file= out_file= raw= body_file= agy_diff_file= agy_work_dir= jq_err= prior_body_file= archived_file= api_err= # Set to 1 if agy printed its interactive OAuth login flow instead of a review (lapsed session); # gates the no-post abort below. Pre-declared so `${auth_failed:-0}` is set-u-safe on every path. auth_failed=0 @@ -227,13 +405,53 @@ auth_failed=0 service_errors=0 # Set when the large-diff fallback below creates refs/agy/* so the trap can remove them. agy_refs_created= +# Set by the AGY_DRY_RUN block so cleanup KEEPS the two artefacts dry-run exists to expose. The +# whole point of dry run is "let me look at the assembled prompt", and the EXIT trap fires on the +# `exit 0` at the end of that block -- so it deleted the prompt on the way out, every time. +# Everything else is still removed; only these two are spared, and their paths are printed. +keep_artifacts= cleanup() { - rm -f "$diff_file" "$diff_err" "$meta_file" "$prompt_file" "$out_file" "$raw" "$body_file" "$agy_diff_file" + # Built as an argv ARRAY rather than as `rm -f ${v:+"$v"} ...`. + # + # The problem being solved is the EMPTY operand: `rm -f ""` is silent and exits 0 on GNU + # coreutils -- which is why an unconditional `rm -f "$unset_var"` never surfaced on the Linux + # runner -- but BSD/macOS `rm` writes "No such file or directory" to stderr for it. + # + # A note for the next reader, because this was raised in review and the intuition is wrong: + # `rm -f ${v:+"$v"}` does NOT word-split. Bash honours the quotes inside the `:+` alternate + # word, so a path with a space or a glob character survives as one operand. Verified: + # + # v='a*b'; rm -f ${v:+"$v"} -> + rm -f 'a*b' (siblings axxb, ayb untouched) + # v='a file'; rm -f ${v:+"$v"} -> one operand (siblings a, file untouched) + # + # The array is used anyway, for a reason the expansion form genuinely does not cover: it lets + # `--` terminate option parsing, so a temp path that begins with `-` is treated as a path + # rather than as flags. An array element is also one word by construction, which does not + # depend on knowing that `:+` quoting rule. + local keep_set="${keep_artifacts:-}" + local -a doomed=() + local f + # `${v:-}` on every name, even though all of them are pre-declared at the top of the script and + # cannot be unset here. Belt-and-braces on purpose: this is an EXIT trap, so if a future edit + # ever moves the trap above the pre-declarations, the failure would be an `unbound variable` + # abort DURING cleanup -- temp files left behind, and a confusing error masking the real exit + # cause. The pre-declaration stays and is still the actual guarantee; this is the cheap second + # line for a function that only ever runs while something else is going wrong. + for f in "${diff_file:-}" "${diff_err:-}" "${meta_file:-}" "${out_file:-}" "${raw:-}" "${body_file:-}" \ + "${jq_err:-}" "${prior_body_file:-}" "${archived_file:-}" "${api_err:-}"; do + [ -n "$f" ] && doomed+=("$f") + done + if [ -z "$keep_set" ]; then + for f in "${prompt_file:-}" "${agy_diff_file:-}"; do + [ -n "$f" ] && doomed+=("$f") + done + fi + [ ${#doomed[@]} -gt 0 ] && rm -f -- "${doomed[@]}" # Remove the gitignored diff-handoff scratch dir once its file is gone. `rmdir` only unlinks an # empty dir, so a concurrent run's file (a different $$) is never clobbered; a non-empty dir is # gitignored and harmless if left behind. - [ -n "$agy_work_dir" ] && rmdir "$agy_work_dir" 2>/dev/null || true - if [ -n "$agy_refs_created" ] && [ -n "${PR:-}" ]; then + [ -n "${agy_work_dir:-}" ] && [ -z "$keep_set" ] && rmdir "${agy_work_dir}" 2>/dev/null || true + if [ -n "${agy_refs_created:-}" ] && [ -n "${PR:-}" ]; then git update-ref -d "refs/agy/pr-${PR}" 2>/dev/null || true git update-ref -d "refs/agy/base-${PR}" 2>/dev/null || true fi @@ -281,7 +499,8 @@ esac # read-only bytes that become prompt text and are never executed. So the fallback # does not widen the trust boundary the workflow already sets: whatever governs # whether a given PR's diff is allowed to reach agy at all (the workflow `if:` -# author-association gate; see the trust model at the agy invocation below) is +# pre-filter plus `agy_has_write_access` above; see the trust model at the agy +# invocation below) is # unchanged, and this only changes HOW an already-permitted diff is obtained when # it is too large for the API. `refs/agy/*` are private namespaces (cannot clobber # a real branch) and are removed on exit. Auth goes through `http.extraheader` for @@ -290,28 +509,36 @@ esac # ambient credential helper. # ($diff_err was allocated alongside $diff_file / $meta_file above, so the cleanup # trap never references it before it exists.) +# >>> SELFTEST-EXTRACT: diff-limit classifier +# GitHub refuses an oversized diff TWO ways, with different wording: over +# 20,000 lines, and over 300 FILES. Both are HTTP 406 and both mean the same +# thing here -- the PR is too big for the API, not that anything went wrong -- +# so both must reach the local fallback. Matching only the `lines` variant made +# a wide-but-shallow PR -- hundreds of files, well under the line limit, as a +# bulk regeneration of test baselines produces -- fail the review outright +# instead of falling back. +# +# On a match, print the limit that actually fired and succeed. Reporting +# "20,000-line" for a file-count refusal is the same class of misleading triage +# signal that made this bug look like a runner auth failure in the first place. +# A function, not inline, so the self-test executes the real matcher and label +# against both GitHub messages rather than grepping for its text. +diff_limit_hit() { + grep -qiE 'diff exceeded the maximum number of (lines|files)' "$1" || return 1 + if grep -qi 'maximum number of files' "$1"; then + printf '300-file' + else + printf '20,000-line' + fi +} +# <<< SELFTEST-EXTRACT if ! gh pr diff "$PR" --repo "$REPO" > "$diff_file" 2>"$diff_err"; then - # GitHub refuses an oversized diff TWO ways, with different wording: over - # 20,000 lines, and over 300 FILES. Both are HTTP 406 and both mean the same - # thing here -- the PR is too big for the API, not that anything went wrong -- - # so both must reach the local fallback. Matching only the `lines` variant made - # a wide-but-shallow PR -- hundreds of files, well under the line limit, as a - # bulk regeneration of test baselines produces -- fail the review outright - # instead of falling back. - if grep -qiE 'diff exceeded the maximum number of (lines|files)' "$diff_err"; then + if hit="$(diff_limit_hit "$diff_err")"; then base_ref="$(jq -r '.baseRefName // empty' "$meta_file")" if [ -z "$base_ref" ] || [ "$base_ref" = "null" ]; then log "diff exceeds the API limit and the base branch is unknown; cannot fall back" exit 1 fi - # Name the limit that actually fired. Reporting "20,000-line" for a - # file-count refusal is the same class of misleading triage signal that - # made this bug look like a runner auth failure in the first place. - if grep -qi 'maximum number of files' "$diff_err"; then - hit="300-file" - else - hit="20,000-line" - fi log "diff exceeds GitHub's ${hit} API limit; falling back to a local git diff" pr_ref="refs/agy/pr-${PR}" base_local="refs/agy/base-${PR}" @@ -320,9 +547,32 @@ if ! gh pr diff "$PR" --repo "$REPO" > "$diff_file" 2>"$diff_err"; then # `bearer` is what Actions' GITHUB_TOKEN accepts; a personal token from # `gh auth token` is rejected ("remote: invalid credentials"), so a hand-run # falls through to git's ambient credentials. Neither path persists anything. + # + # The header is supplied through GIT_CONFIG_* (git >= 2.31), NOT `git -c`. Identical + # config, different exposure: `git -c "http.extraheader=...bearer $TOKEN"` puts the token + # in the process's argv, and `/proc//cmdline` is WORLD-READABLE on Linux — any + # concurrent job or any local process can read it for the lifetime of the fetch. This + # runner is a shared workstation, so that window is real. `/proc//environ` is + # restricted to the same UID, so moving the secret from argv to the environment is what + # actually closes it. Do not "simplify" this back to `git -c`. + # Every fetch on this path goes through agy_fetch so they all carry the header: the + # follow-up merge-base fetches below once omitted it, which fails on a private repo + # (agy review, switch2-hub-patcher PR #1). Token first, then git's ambient credentials. + agy_fetch() { + if [ -n "${GH_TOKEN:-}" ] \ + && GIT_CONFIG_COUNT=1 \ + GIT_CONFIG_KEY_0="http.extraheader" \ + GIT_CONFIG_VALUE_0="AUTHORIZATION: bearer ${GH_TOKEN}" \ + git fetch --no-tags --quiet "$@" 2>/dev/null; then + return 0 + fi + git fetch --no-tags --quiet "$@" 2>/dev/null + } if [ -n "${GH_TOKEN:-}" ] \ - && git -c "http.extraheader=AUTHORIZATION: bearer ${GH_TOKEN}" fetch --no-tags --quiet \ - origin "${fetch_refspecs[@]}" 2>/dev/null; then + && GIT_CONFIG_COUNT=1 \ + GIT_CONFIG_KEY_0="http.extraheader" \ + GIT_CONFIG_VALUE_0="AUTHORIZATION: bearer ${GH_TOKEN}" \ + git fetch --no-tags --quiet origin "${fetch_refspecs[@]}" 2>/dev/null; then : elif git fetch --no-tags --quiet origin "${fetch_refspecs[@]}"; then log "fetched PR refs using git's ambient credentials (token header not accepted)" @@ -343,20 +593,39 @@ if ! gh pr diff "$PR" --repo "$REPO" > "$diff_file" 2>"$diff_err"; then merge_base="$(git merge-base "$base_local" "$pr_ref" 2>/dev/null || true)" if [ -z "$merge_base" ]; then head_sha="$(jq -r '.headRefOid // empty' "$meta_file")" - # Percent-encode the branch name for the URL path. NOT for the `/` in a - # `/` branch -- GitHub's compare endpoint accepts those - # raw, verified against a real slashed branch, returning the same SHA - # either way. It is for `%` and `#`, which git permits in a ref name and - # which a URL does not survive: `%` starts an escape and `#` truncates the - # path at the fragment. Both would fail silently into the `|| true`. + # Percent-encode the branch name for the URL path. `@uri` encodes EVERY reserved + # character, `/` included -- `ci/foo` becomes `ci%2Ffoo`, verified with + # `jq -rn --arg v 'ci/foo' '$v|@uri'`. GitHub's compare endpoint resolves the + # escaped form back to the branch, so a `/` name works either + # way and the encoding is harmless there. + # + # It is NOT harmless to skip: `%` and `#` are legal in a git ref name and a raw + # URL does not survive them -- `%` starts an escape sequence and `#` truncates + # the path at the fragment. Either would fail silently into the `|| true` below, + # which is exactly the class of bug this whole path exists to avoid. base_enc="$(jq -rn --arg v "$base_ref" '$v|@uri')" + compare_err="$(mktemp)" api_base="$(gh api "repos/${REPO}/compare/${base_enc}...${head_sha}" \ - --jq '.merge_base_commit.sha' 2>/dev/null || true)" + --jq '.merge_base_commit.sha' 2>"$compare_err" || true)" + [ -n "$api_base" ] || log "compare API gave no merge base ($(head -c 300 "$compare_err" | tr '\n' ' '))" + rm -f "$compare_err" if [ -n "$api_base" ] && [ "$api_base" != "null" ]; then - if git fetch --no-tags --quiet origin "$api_base" 2>/dev/null \ - || git fetch --no-tags --quiet --deepen=250 origin "${fetch_refspecs[@]}" 2>/dev/null; then - merge_base="$(git merge-base "$base_local" "$pr_ref" 2>/dev/null || echo "$api_base")" - log "shallow clone: merge base ${merge_base} resolved via the compare API" + if agy_fetch origin "$api_base" \ + || agy_fetch --deepen=250 origin "${fetch_refspecs[@]}"; then + merge_base="$(git merge-base "$base_local" "$pr_ref" 2>/dev/null || true)" + # Fall back to the API's merge base only if that commit is actually + # here. When the SHA fetch fails and `--deepen=250` succeeds without + # reaching it, the object is absent, and using it anyway surfaced later + # as the generic "local git diff failed" -- the wrong diagnosis. + if [ -z "$merge_base" ]; then + if git cat-file -e "${api_base}^{commit}" 2>/dev/null; then + merge_base="$api_base" + else + log "compare API merge base ${api_base} is not in the local clone after both fetches" + fi + fi + [ -z "$merge_base" ] \ + || log "shallow clone: merge base ${merge_base} resolved via the compare API" fi fi fi @@ -375,8 +644,78 @@ fi if ! have_text "$diff_file"; then log "empty diff; nothing to review"; exit 0; fi truncated="" -if [ "$(wc -c < "$diff_file")" -gt "$MAX_DIFF_BYTES" ]; then - head -c "$MAX_DIFF_BYTES" "$diff_file" > "$diff_file.cut" && mv "$diff_file.cut" "$diff_file" +# `head -c` is a BYTE cut and can slice a multi-byte UTF-8 sequence in half. That is accepted, not +# overlooked: the UTF-8 scrub further down (`iconv -c`, python3 fallback) removes any partial +# sequence before the prompt reaches agy, so the split never escapes this file. +# +# A line-aware cut would avoid creating the split, but cannot honour a byte ceiling — and the +# ceiling is the actual constraint, since this bounds what is handed to a model with a hard context +# limit. One minified-asset line in a diff can exceed the whole budget by itself, so "cut at a line +# boundary" degenerates to either overshooting the cap or emitting nothing. Exact where the limit is +# real, tolerant where the damage is already handled. +# --- truncation helpers --------------------------------------------------------- +# Every byte cap in this script goes through these, so the self-test can EXECUTE them rather than +# grep for their text (CodeRabbit review, VeridianOS PR #5). A model that is handed a cut input must +# be told so, and a notice appended before a later cut can be cut away -- so each cap that can hide +# content writes its own notice AFTER cutting. Notices are plain ASCII: character count = bytes. +# >>> SELFTEST-EXTRACT: truncation helpers +# Cut file $1 to at most $2 bytes. Succeeds only when it actually cut. +agy_cut_file() { + [ "$(wc -c < "$1")" -gt "$2" ] || return 1 + head -c "$2" "$1" > "$1.cut" && mv "$1.cut" "$1" +} +# Prompt text telling the model the diff it was given stops after $1 bytes. +agy_diff_truncation_notice() { + printf '\n--- NOTE: TRUNCATED DIFF ---\n' + printf 'The diff above is only the first %s bytes; everything after that was cut and you have NOT\n' "$1" + printf 'seen it. Say at the top of your review that it covers a truncated diff, and do not state or\n' + printf 'imply that the whole change was reviewed.\n' +} +# Cap prompt file $1 at $2 bytes, keeping room for a notice written after the cut. +agy_cap_prompt() { + local notice=$'\n\n--- NOTE: TRUNCATED PROMPT ---\nThe prompt, including the diff, was cut at the argument-size limit; you have NOT seen\nthe rest. Say at the top of your review that it covers a truncated diff, and do not state or\nimply that the whole change was reviewed.\n' + agy_cut_file "$1" "$(( $2 - ${#notice} ))" || return 1 + printf '%s' "$notice" >> "$1" +} +# Cap review output $1 at $2 bytes so the posted comment fits GitHub's limit, saying what was cut. +agy_cap_round() { + local size notice + size="$(wc -c < "$1")" + [ "$size" -gt "$2" ] || return 1 + notice="$(printf '\n\n> **Review truncated:** about %s bytes of this round were omitted to stay under GitHub'"'"'s comment size limit.' "$(( size - $2 + 200 ))")" + # -1: the trailing newline written below is part of the budget too. + agy_cut_file "$1" "$(( $2 - ${#notice} - 1 ))" || return 1 + printf '%s\n' "$notice" >> "$1" +} +# Drop invalid/partial UTF-8 sequences from file $1 in place (a byte cut can split one). Every byte +# cap that feeds agy or a PR comment calls this, so the two paths cannot drift apart again (the +# review-output path once had iconv only; agy review, switch2-hub-patcher PR #1). iconv first +# (glibc and macOS ship it), then python3 -- also when iconv is present but fails. That case is the +# common one, not an edge: GNU `iconv -c` writes the cleaned bytes but EXITS 1 whenever it dropped +# anything ("incomplete character or shift sequence at end of buffer"), i.e. on exactly the byte-cut +# input this exists for. The old iconv-only branches discarded that output, so on glibc hosts the +# scrub was a no-op whenever it mattered (measured: glibc 2.44). A sanitizer that +# exits 0 with ZERO bytes is a malfunction, not a result: replace the file only with non-empty +# output, otherwise keep the original. A failed mv is fatal under `set -e`; the function returns +# 0 when no sanitizer succeeded, so a host with neither tool proceeds with the original bytes. +agy_scrub_utf8() { + if command -v iconv >/dev/null 2>&1 \ + && iconv -c -f UTF-8 -t UTF-8 "$1" > "$1.utf8" 2>/dev/null && [ -s "$1.utf8" ]; then + mv "$1.utf8" "$1"; return + fi + rm -f "$1.utf8" + if command -v python3 >/dev/null 2>&1 \ + && python3 -c 'import sys; sys.stdout.buffer.write(open(sys.argv[1],"rb").read().decode("utf-8","ignore").encode("utf-8"))' \ + "$1" > "$1.utf8" 2>/dev/null && [ -s "$1.utf8" ]; then + mv "$1.utf8" "$1"; return + fi + rm -f "$1.utf8" +} +# <<< SELFTEST-EXTRACT + +diff_truncated= +if agy_cut_file "$diff_file" "$MAX_DIFF_BYTES"; then + diff_truncated=1 truncated=$'\n\n> Note: the diff exceeded '"${MAX_DIFF_BYTES}"$' bytes and was truncated for this review.' log "diff truncated to the ${MAX_DIFF_BYTES}-byte sanity cap" fi @@ -454,7 +793,7 @@ if [ "$use_file" = "1" ]; then cp "$diff_file" "$agy_diff_file" { printf '\n--- UNIFIED DIFF (in a file) ---\n' - printf 'The full unified diff for this PR is in the file at the absolute path `%s`\n' "$agy_diff_file" + printf 'The unified diff for this PR is in the file at the absolute path `%s`\n' "$agy_diff_file" printf '(it is too large to inline). Read that file IN FULL with your file-reading tool first, then\n' printf 'produce the review above from its actual contents. Do not review from the PR title alone.\n' } >> "$prompt_file" @@ -466,43 +805,38 @@ else { printf '\n--- UNIFIED DIFF ---\n'; cat "$diff_file"; } >> "$prompt_file" log "diff is ${diff_bytes} bytes; inlined into the prompt" fi +# The sanity cap above may have cut the diff. The note in `$truncated` used to reach only the +# posted comment, so the model was told it had the whole diff and could present a partial review +# as complete. Say so in the prompt itself, whichever way the diff was delivered. +if [ -n "$diff_truncated" ]; then + agy_diff_truncation_notice "$MAX_DIFF_BYTES" >> "$prompt_file" +fi # --- guard the argv size (E2BIG) ----------------------------------------------- # agy takes the prompt as a --print VALUE, so the whole prompt is one execve argument # and must stay under MAX_ARG_STRLEN (128 KiB). MAX_DIFF_BYTES bounds the diff, but the # boilerplate + style guide ride on top, so cap the assembled prompt as a hard backstop. -if [ "$(wc -c < "$prompt_file")" -gt "$MAX_PROMPT_BYTES" ]; then - head -c "$MAX_PROMPT_BYTES" "$prompt_file" > "$prompt_file.cut" && mv "$prompt_file.cut" "$prompt_file" +# +# Cutting the tail also cuts whatever notice was appended there (the TRUNCATED DIFF note above), +# and the model would never learn that its input was cut. So the cut leaves room for a notice of +# its own, written AFTER the cut where nothing can remove it (agy review on VeridianOS PR #5). +# The notice is plain ASCII, so its character count is its byte count. +if agy_cap_prompt "$prompt_file" "$MAX_PROMPT_BYTES"; then truncated+=$'\n\n> Note: the review prompt was capped to '"${MAX_PROMPT_BYTES}"$' bytes (execve arg-size limit).' log "prompt capped to ${MAX_PROMPT_BYTES} bytes (execve arg-size ceiling)" fi # Byte truncation (here or in the MAX_DIFF_BYTES cap above) can slice a multi-byte UTF-8 sequence. # agy is a Rust binary and std::env::args() PANICS on a non-UTF-8 argument, which would reintroduce # an instant startup failure -- exactly the class of bug this guard exists to prevent. Drop any -# invalid/partial sequences with iconv (glibc + macOS ship it), and fall back to python3 (present on -# every CI runner) if iconv is absent, so a no-iconv host can't leave a split sequence in the prompt. -# Explicit branches, not `&& mv || rm`: that idiom masks an mv failure and would then feed agy the -# original (possibly split) bytes. A successful sanitize must replace the file; a failed mv is fatal -# (set -e); a failed/absent sanitizer leaves the original and we proceed (it may already be clean, and -# any residual invalid byte now surfaces via the captured stderr rather than a silent instant crash). -# The `[ -s ... ]` guard is deliberate: the prompt is always non-empty here (boilerplate at -# minimum), so a sanitizer that exits 0 but emits ZERO bytes is a malfunction, and mv-ing that -# empty file over $prompt_file would wipe the prompt and hand agy nothing. Replace only on a -# non-empty result; otherwise discard it and proceed with the original (already likely clean). -if command -v iconv >/dev/null 2>&1; then - if iconv -c -f UTF-8 -t UTF-8 "$prompt_file" > "$prompt_file.utf8" 2>/dev/null && [ -s "$prompt_file.utf8" ]; then - mv "$prompt_file.utf8" "$prompt_file" - else - rm -f "$prompt_file.utf8" - fi -elif command -v python3 >/dev/null 2>&1; then - if python3 -c 'import sys; sys.stdout.buffer.write(open(sys.argv[1],"rb").read().decode("utf-8","ignore").encode("utf-8"))' \ - "$prompt_file" > "$prompt_file.utf8" 2>/dev/null && [ -s "$prompt_file.utf8" ]; then - mv "$prompt_file.utf8" "$prompt_file" - else - rm -f "$prompt_file.utf8" - fi -fi +# invalid/partial sequences with `agy_scrub_utf8` (iconv, then python3), so a host without iconv can't +# leave a split sequence in the prompt. Explicit branches inside it, not `&& mv || rm`: that idiom +# masks an mv failure and would then feed agy the original (possibly split) bytes. A successful +# sanitize must replace the file; a failed mv is fatal (set -e); a failed/absent sanitizer leaves the +# original and we proceed (it may already be clean, and any residual invalid byte now surfaces via the +# captured stderr rather than a silent instant crash). Its non-empty guard matters here: the prompt is +# always non-empty (boilerplate at minimum), so mv-ing an empty sanitizer result over $prompt_file +# would wipe the prompt and hand agy nothing. +agy_scrub_utf8 "$prompt_file" # Escape hatch for verifying the diff-acquisition and prompt-assembly path (including # the large-PR API-limit fallback and the inline/file decision) without spending an agy @@ -515,9 +849,93 @@ if [ -n "${AGY_DRY_RUN:-}" ]; then log "prompt: $(wc -c < "$prompt_file") bytes (diff inlined, $(wc -c < "$diff_file") bytes)" fi cat "$prompt_file" + # Keep the prompt (and the on-disk diff handoff, if used) so they can actually be inspected -- + # see `keep_artifacts` at the cleanup trap. Paths go to stderr so a `> prompt.txt` redirect of + # stdout still captures only the prompt. + keep_artifacts=1 + log "kept for inspection: $prompt_file${agy_diff_file:+ and $agy_diff_file}" + log "(remove them yourself; every other temp file was cleaned up as usual)" exit 0 fi +# Scale the timeout with the size of the diff agy actually has to read. +# +# A fixed 5m is right for an ordinary PR and hopeless for a release merge. Observed on +# CyberChef-MCP#83 -- 323 files, 39,856 lines, 1.6 MB handed off as a file -- where agy hit the +# 5m ceiling on all three attempts, twice in a row, at 5m01s each time. The job then failed with +# `Error: timeout waiting for response`, which is INDISTINGUISHABLE from a backend outage: the +# guard did its job and refused to post a fake review, but nothing told the reader that the cause +# was diff size rather than an outage. +# +# Deliberately keyed on the diff handed to agy, not on the PR's file count: what costs time is the +# bytes it must read and reason over. +# +# An explicit AGY_PRINT_TIMEOUT wins, so a caller can still pin it. + +# >>> SELFTEST-EXTRACT: duration parser +# Parse a duration ("90", "90s", "5m", "1h") to seconds. Echoes nothing and returns 1 if the value +# is not one of those forms -- deliberately, so a caller can decide, rather than feeding a +# non-numeric token into an arithmetic expansion where `$(( 1x + 60 ))` is a SYNTAX ERROR that +# takes the whole script down under `set -e`. +duration_to_seconds() { + local v="$1" n unit + case "$v" in + *[!0-9smh]*|"") return 1 ;; # stray characters, or empty + *h) n="${v%h}"; unit=3600 ;; + *m) n="${v%m}"; unit=60 ;; + *s) n="${v%s}"; unit=1 ;; + *) n="$v"; unit=1 ;; + esac + case "$n" in ""|*[!0-9]*) return 1 ;; esac # "m" alone, or "1m2s" + # `10#` forces base 10. Without it bash reads a leading zero as OCTAL, so a perfectly valid + # `08m` dies with "value too great for base" -- and `010s` would silently mean 8 seconds. + printf '%s\n' $(( 10#$n * unit )) +} +# <<< SELFTEST-EXTRACT + +# >>> SELFTEST-EXTRACT: diff-size scaling +# Kept below the duration-parser block, not beside the other constants: SELFTEST-EXTRACT ranges +# end at the first closing marker, so a block declared around this one would be truncated at the +# parser's `<<<` and silently extract without the function under test. +readonly BYTES_PER_MIB=$(( 1024 * 1024 )) + +# Raise --print-timeout in proportion to the diff agy has to read. +# +# @param $1 size of the diff handed to agy, in bytes. Passed explicitly rather than read from the +# enclosing scope, so the function's inputs are visible at the call site. +scale_timeout_for_diff() { + local bytes="${1:-0}" # explicit default rather than leaning on `$(( ))` treating "" as 0 + # Whitespace is stripped BEFORE validating, not after: some `wc` implementations pad their count, + # and " 1619782 " is not digits-only, so validating first would quietly fall back to 0 and + # disable the scaling entirely -- a silent no-op, which is worse than the crash being guarded + # against. Then the same guard the settings get, so a non-numeric value falls back rather than + # becoming a syntax error in the expansion below. + bytes="${bytes//[[:space:]]/}" + normalise_numeric_env bytes 0 + [ -z "$AGY_PRINT_TIMEOUT_EXPLICIT" ] || { log "AGY_PRINT_TIMEOUT set explicitly ($AGY_PRINT_TIMEOUT); not scaling"; return 0; } + + # Computed from BYTES, not from truncated whole MiB: `mib = bytes / 1048576` in integer + # arithmetic gives a 1.99 MiB diff exactly one MiB of extra budget, which is the wrong side to + # round on for the case this exists to fix. + local extra=$(( bytes * AGY_TIMEOUT_SECONDS_PER_MIB / BYTES_PER_MIB )) + [ "$extra" -gt 0 ] || return 0 # rounds to nothing: keep the base budget untouched + + local base_s + if ! base_s="$(duration_to_seconds "$AGY_PRINT_TIMEOUT")"; then + log "AGY_PRINT_TIMEOUT ('$AGY_PRINT_TIMEOUT') is not a recognised duration; leaving it alone" + return 0 + fi + + local scaled=$(( base_s + extra )) + # Ceiling, so a pathological diff cannot pin the self-hosted runner for an hour. + [ "$scaled" -gt "$AGY_PRINT_TIMEOUT_MAX_SECONDS" ] && scaled="$AGY_PRINT_TIMEOUT_MAX_SECONDS" + + AGY_PRINT_TIMEOUT="${scaled}s" + log "diff is ${bytes} bytes; raised --print-timeout to ${AGY_PRINT_TIMEOUT} (base ${base_s}s + ${extra}s)" +} +# <<< SELFTEST-EXTRACT +scale_timeout_for_diff "$diff_bytes" + # --- run agy headless, under a PTY (works around agy issue #76: -p drops -------- # stdout when stdout is not a TTY, e.g. piped/redirected/subprocess) --------- flags=( --print-timeout "$AGY_PRINT_TIMEOUT" --sandbox --dangerously-skip-permissions ) @@ -629,6 +1047,15 @@ for (( attempt=1; attempt<=AGY_RETRIES; attempt++ )); do service_errors=$(( ${service_errors:-0} + 1 )) fi + # A timed-out turn is a partial review at best. Discard it and retry: posting half a review as + # if it were the whole one is the failure the service-error guard exists to prevent, one cause + # later. Counted like backend errors, so the final log line names the real cause. + if print_timeout_present "$out_file"; then + log "agy hit its print timeout (${AGY_PRINT_TIMEOUT}) before finishing (attempt ${attempt}/${AGY_RETRIES}); discarding the partial output" + : > "$out_file" + print_timeouts=$(( ${print_timeouts:-0} + 1 )) + fi + have_text "$out_file" && break if [ "$attempt" -lt "$AGY_RETRIES" ]; then delay=$(( AGY_RETRY_DELAY * attempt )) @@ -636,7 +1063,12 @@ for (( attempt=1; attempt<=AGY_RETRIES; attempt++ )); do sleep "$delay" fi done -exec 9>&- 2>/dev/null || true # release the agy lock so the next queued job proceeds +# Release the agy lock so the next queued job proceeds. NO redirection on this `exec`: a bare +# `exec` applies its redirections to the rest of the SCRIPT, so the `2>/dev/null` that used to be +# here sent every later `log` line (they write to stderr) to /dev/null -- "posted", "updated", +# and every failure cause alike. A healthy run and a silent crash looked identical in CI for as +# long as that line existed (found 2026-09-28 on RustyNES #563). Closing an fd needs no guard. +exec 9>&- # Lapsed-auth abort takes precedence over the generic empty-output path: it is a specific, # actionable cause (re-auth agy on the runner), not a transient backend blip, and we already @@ -658,6 +1090,9 @@ if backend_outage_should_fail "${service_errors:-0}" "$out_file"; then fi if ! have_text "$out_file"; then + if [ "${print_timeouts:-0}" -gt 0 ]; then + log "agy hit its print timeout (${AGY_PRINT_TIMEOUT}) on ${print_timeouts} of ${AGY_RETRIES} attempt(s) and produced no complete review. Raise AGY_PRINT_TIMEOUT for this repository's diffs, then re-run with '/agy-review'." + fi log "no review output after ${AGY_RETRIES} attempt(s). Check $LOG and confirm 'agy -p \"hi\"' works for this user." # Surface agy's stderr into the job log. RUNNER_TEMP is wiped between jobs, so a bare # `exit 1` otherwise leaves the real cause invisible in CI (E2BIG, auth, backend, ...). @@ -675,9 +1110,17 @@ if ! have_text "$out_file"; then fi # --- assemble the comment body ------------------------------------------------- +# MAX_BODY_BYTES bounded only the archive, so a review longer than GitHub's comment limit failed to +# post at all (CodeRabbit review, VeridianOS PR #5). Bound the new round too, leaving room for the +# framing and some archive, and say what was cut. A byte cut can split a UTF-8 sequence: scrub it. +if agy_cap_round "$out_file" "$(( MAX_BODY_BYTES - 4096 ))"; then + log "review output capped to fit the ${MAX_BODY_BYTES}-byte comment budget" + agy_scrub_utf8 "$out_file" +fi body_file="$(mktemp)" { printf '%s\n' "$MARKER" + printf '%s%s -->\n' "$AGY_REVIEWED_AT_PREFIX" "$(date -u +'%Y-%m-%d %H:%M UTC')" printf '## Antigravity review (Gemini via Ultra)\n\n' cat "$out_file" printf '%s' "$truncated" @@ -708,17 +1151,38 @@ fi # costs only the archive, never the review. prior_id="" prior_body_file="$(mktemp)" -if prior_json="$(gh api "repos/${REPO}/issues/${PR}/comments" --paginate 2>/dev/null)"; then - prior_id="$(printf '%s' "$prior_json" | jq -r --arg marker "$MARKER" "$SELECT_OURS_JQ" 2>/dev/null || true)" +# gh's stderr from the calls below. Each still falls back to a fresh post, but says WHY (expired +# token, rate limit, 404), which a bare `2>/dev/null` threw away (agy review, VeridianOS PR #5). +api_err="$(mktemp)" +# `--paginate` emits one top-level JSON array PER PAGE, concatenated — not one merged array. +# jq then runs the filter once per array, so `[ ... ] | first | .id` yields one id per page +# that matches rather than one id overall. On a thread short enough to fit a single page that +# is invisible; past 30 comments, if a fallback POST has ever left a second marked comment +# behind, `prior_id` becomes multi-line, `--argjson id` rejects it, and the in-place edit +# silently degrades into a duplicate post — exactly on the long threads where the archive +# matters most. `jq -s add` merges the pages into the single array the filter assumes. +# (`gh api --slurp` does this too, but only on gh >= 2.42; this works on any version.) +if prior_json="$(gh api "repos/${REPO}/issues/${PR}/comments" --paginate 2>"$api_err" \ + | jq -s 'add // []' 2>/dev/null)"; then + # Both jq steps still fall back to a fresh post (losing the archive, never the review), but the + # failure is LOGGED: swallowing it made a broken filter indistinguishable from a first review, + # and the archive would vanish with nothing to say why (agy review on VeridianOS PR #5). + jq_err="$(mktemp)" + if ! prior_id="$(printf '%s' "$prior_json" | jq -r --arg marker "$MARKER" "$SELECT_OURS_JQ" 2>"$jq_err")"; then + log "warning: finding the previous review comment failed (jq: $(head -c 300 "$jq_err" | tr '\n' ' ')); posting a fresh review without the archive" + prior_id="" + fi if [ -n "$prior_id" ] && [ "$prior_id" != "null" ]; then - printf '%s' "$prior_json" \ - | jq -r --argjson id "$prior_id" '.[] | select(.id == $id) | .body' > "$prior_body_file" 2>/dev/null \ - || : > "$prior_body_file" + if ! printf '%s' "$prior_json" \ + | jq -r --argjson id "$prior_id" '.[] | select(.id == $id) | .body' > "$prior_body_file" 2>"$jq_err"; then + log "warning: reading the previous review comment failed (jq: $(head -c 300 "$jq_err" | tr '\n' ' ')); posting a fresh review without the archive" + : > "$prior_body_file" + fi else prior_id="" fi else - log "warning: could not list PR comments; posting a fresh review without the archive" + log "warning: could not list PR comments ($(head -c 300 "$api_err" | tr '\n' ' ')); posting a fresh review without the archive" fi if [ -n "$prior_id" ] && [ -s "$prior_body_file" ]; then @@ -730,14 +1194,8 @@ if [ -n "$prior_id" ] && [ -s "$prior_body_file" ]; then prior_archive="$(agy_body_archive < "$prior_body_file")" archived_file="$(mktemp)" - { - printf '%s\n' "$AGY_ROUND_MARK" - printf '
\nRound reviewed at %s\n\n' \ - "$(date -u +'%Y-%m-%d %H:%M UTC')" - printf '%s\n' "$prior_head" - printf '\n
\n' - printf '%s\n' "$prior_archive" - } > "$archived_file" + agy_assemble_archive "$prior_head" "$prior_archive" "$(date -u +'%Y-%m-%d %H:%M UTC')" \ + > "$archived_file" # Drop the oldest rounds until the whole comment fits, and SAY SO. A silent truncation # here would look identical to "there were never any earlier rounds", which is the exact @@ -754,14 +1212,18 @@ if [ -n "$prior_id" ] && [ -s "$prior_body_file" ]; then done { - printf '\n%s\n' "$AGY_ARCHIVE_START" + # Order matters. The `
` wrapper and the dropped-round note sit OUTSIDE the + # start/end sentinels, so `agy_body_archive` extracts only the rounds themselves. With + # the wrapper inside, every run re-captured it and nested another layer around it. + printf '\n%s\n' "$AGY_ARCHIVE_SECTION" if [ "$dropped" -gt 0 ]; then printf '%d earlier round(s) dropped to stay under GitHub'"'"'s comment size limit.\n\n' "$dropped" fi printf '
\nEarlier review rounds (newest first)\n\n' + printf '%s\n' "$AGY_ARCHIVE_START" cat "$archived_file" - printf '\n
\n' printf '%s\n' "$AGY_ARCHIVE_END" + printf '\n
\n' } >> "$body_file" rm -f "$archived_file" @@ -781,12 +1243,12 @@ if [ -n "$prior_id" ] && [ -s "$prior_body_file" ]; then # JSON string by construction, so no shell quoting or `-F` type-coercion can # reinterpret a body that happens to look like a number or a boolean. if jq -n --rawfile b "$body_file" '{body: $b}' \ - | gh api -X PATCH "repos/${REPO}/issues/comments/${prior_id}" --input - >/dev/null 2>&1; then + | gh api -X PATCH "repos/${REPO}/issues/comments/${prior_id}" --input - >/dev/null 2>"$api_err"; then log "updated review comment ${prior_id} on ${REPO}#${PR} (earlier rounds archived in place)" rm -f "$prior_body_file" exit 0 fi - log "warning: could not edit comment ${prior_id}; posting a fresh review instead" + log "warning: could not edit comment ${prior_id} ($(head -c 300 "$api_err" | tr '\n' ' ')); posting a fresh review instead" fi rm -f "$prior_body_file" From 905542f4b06658501dce9f74457198789ad9972e Mon Sep 17 00:00:00 2001 From: DoubleGate Date: Sat, 14 Nov 2026 06:01:53 -0500 Subject: [PATCH 2/4] ci: pick up template 629a97e (fork-gate comment, single-job guard) The workflow comment no longer calls the outside-contributor approval setting "belt-and-suspenders": for a pull_request event a fork controls the workflow file, so that setting is the gate. The self-test now fails if the workflow ever has more than one timeout-minutes, which would make its budget check read the wrong value. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/antigravity-review.yml | 11 +++++++---- scripts/agy-review-selftest.sh | 6 ++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/.github/workflows/antigravity-review.yml b/.github/workflows/antigravity-review.yml index 6e7dde4a..7eb94334 100644 --- a/.github/workflows/antigravity-review.yml +++ b/.github/workflows/antigravity-review.yml @@ -29,10 +29,13 @@ jobs: # and spends the owner's Google AI Ultra quota, so "who can start a run" is the # primary control): # - pull_request: only from a branch in THIS repo. A FORK PR must never schedule - # work on the self-hosted host. GitHub's "require approval for outside - # collaborators" setting is belt-and-suspenders, not the gate — its default only - # covers *first-time* contributors, so a returning outside contributor would - # otherwise get a runner. + # work on the self-hosted host. This `if:` alone cannot ensure that: for a + # pull_request event GitHub runs the PR's own copy of this file, so a fork can + # edit the condition away, or add a workflow of its own aimed at the `agy` label. + # The gate for forks is the repository setting that requires approval for ALL + # external contributors (approval_policy = all_external_contributors, see + # .github/AGY-REVIEWER.md); GitHub's default covers only first-time contributors. + # (Adopted from VeridianOS, whose copy corrected this.) # - issue_comment: only `/agy-review`, and the association test below is a CHEAP # PRE-FILTER, not the permission gate. `author_association` answers "what is this # person's relationship to the repo", not "may they run code on my workstation": diff --git a/scripts/agy-review-selftest.sh b/scripts/agy-review-selftest.sh index a63e4aea..0646f466 100755 --- a/scripts/agy-review-selftest.sh +++ b/scripts/agy-review-selftest.sh @@ -509,6 +509,12 @@ fi wf="$SCRIPT_DIR/../.github/workflows/antigravity-review.yml" wf_val() { sed -n "s/^[[:space:]]*$1:[[:space:]]*[\"']\{0,1\}\([0-9][0-9]*\).*/\1/p;T;q" "$wf"; } sh_default() { sed -n "s/^$1=\"\${$1:-\([0-9][0-9]*\)}\".*/\1/p;T;q" "$SCRIPT_DIR/agy-review.sh"; } +# wf_val takes the first match: right while the workflow has one job. A second +# timeout-minutes would make it read the wrong one (adopted from VeridianOS, PR #6 review). +if [ "$(grep -c '^[[:space:]]*timeout-minutes:' "$wf")" -gt 1 ]; then + echo " FAIL more than one timeout-minutes in the workflow: wf_val needs the review job's" + fails=$((fails + 1)) +fi job_min="$(wf_val timeout-minutes)" max_s="$(wf_val AGY_PRINT_TIMEOUT_MAX_SECONDS)"; [ -n "$max_s" ] || max_s="$(sh_default AGY_PRINT_TIMEOUT_MAX_SECONDS)" lock_s="$(wf_val AGY_LOCK_WAIT)"; [ -n "$lock_s" ] || lock_s="$(sh_default AGY_LOCK_WAIT)" From 45022ad49e4e2c15cc544fc601a1ee7c366e2d3c Mon Sep 17 00:00:00 2001 From: DoubleGate Date: Sat, 14 Nov 2026 06:21:49 -0500 Subject: [PATCH 3/4] ci: drop the reviewer's first-install bootstrap steps The reviewer is already on this repository's default branch, so the three bootstrap steps (a PR-head checkout and install that only fire while the default branch has no reviewer) can never run here. Keeping them leaves a PR-head checkout path in the workflow, which CodeQL flags as actions/untrusted-checkout (high). The comment and the AGY-REVIEWER.md note match RustyNES, VeridianOS and switch2-hub-patcher. Co-Authored-By: Claude Opus 5.5 --- .github/AGY-REVIEWER.md | 10 +-- .github/workflows/antigravity-review.yml | 88 ++---------------------- 2 files changed, 9 insertions(+), 89 deletions(-) diff --git a/.github/AGY-REVIEWER.md b/.github/AGY-REVIEWER.md index a0e220a2..c930d7b9 100644 --- a/.github/AGY-REVIEWER.md +++ b/.github/AGY-REVIEWER.md @@ -246,11 +246,11 @@ yours to accept: `scripts/agy-review.sh`, which asks the permissions API for the commenter and fails closed; the script also re-checks that the PR is not from a fork, because the `issue_comment` payload has no head-repo field for the `if:` to gate on. -- **First-install bootstrap:** on a repository whose default branch has no - reviewer yet, the workflow runs the PR head's scripts once, only for a same-repo - branch whose author has write access *at run time*. Remove the bootstrap steps - once the reviewer is on the default branch (they also trip CodeQL's - `actions/untrusted-checkout`). +- **First-install bootstrap:** the checked-in workflow has no bootstrap steps. On a + repository whose default branch has no reviewer yet, add the guarded bootstrap + steps first. They run the PR head's scripts once, only for a same-repo branch + whose author has write access *at run time*. Remove them once the reviewer is on + the default branch (they also trip CodeQL's `actions/untrusted-checkout`). - **Least surface in the job:** review-only prompt, `--sandbox`, no repo secrets (built-in `GITHUB_TOKEN` only), temp files cleaned via an `EXIT` trap. diff --git a/.github/workflows/antigravity-review.yml b/.github/workflows/antigravity-review.yml index 7eb94334..60a245de 100644 --- a/.github/workflows/antigravity-review.yml +++ b/.github/workflows/antigravity-review.yml @@ -102,90 +102,10 @@ jobs: # supplies the token per command via `http.extraheader` rather than persisting it. persist-credentials: false - # BOOTSTRAP ONLY. On the very first install in a repo the scripts exist on the PR - # branch but not yet on the default branch, so the run step below chmods files that - # are not there and the job dies before it can review anything. Observed on - # CyberChef-MCP PR #41, and predicted by Copilot review on that same PR. - # - # This step checks out and then EXECUTES code from the PR head on a self-hosted runner, - # which is normally the thing you must never do. Three properties are what make it - # defensible here, and all three must hold — if you are editing this step, they are the - # invariant, not the YAML: - # - # 1. It fires only when the default branch has no reviewer at all (`hashFiles` empty), - # i.e. exactly once in a repository's life, and never again after that PR merges. - # 2. The head must be a branch in THIS repository, so a fork PR can never reach it. - # 3. The PR author must hold write access AT RUN TIME, verified against the permissions - # API by the step below — not inferred from `author_association`, and not merely - # implied by their having been able to push the branch at some point in the past. - # This is what closes the revoked-collaborator gap: a stale branch pushed by someone - # whose access was later removed no longer bootstraps. - # - # Worth stating plainly, because it is what bounds the exposure: `pull_request` runs the - # workflow definition from the PR branch. Anyone who can satisfy (2) and (3) can already - # rewrite THIS FILE on their branch and have it execute on the runner. The bootstrap - # therefore grants no capability that the event type has not already granted to the same - # set of people. It is not a reason to relax any of the three conditions. - - name: Check bootstrap author still has write access - id: agy_bootstrap_auth - if: >- - hashFiles('scripts/agy-review.sh') == '' && - github.event.pull_request.head.repo.full_name == github.repository - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - PR_AUTHOR: ${{ github.event.pull_request.user.login }} - run: | - set -eu - # Fails closed: an API error, an unknown login, or any role below `write` leaves - # `writer` unset, and the checkout below does not run. - # - # The failure REASON is surfaced, not swallowed. Denying silently makes "this person - # has no write access" (intended, one PR affected) indistinguishable from "the token - # lost its scope" (the bootstrap is dead for everyone) — an outage that reads as a - # decision. Redacted first: this log is public. - if perm="$(gh api "repos/${GITHUB_REPOSITORY}/collaborators/${PR_AUTHOR}/permission" \ - --jq '.permission // empty' 2>perm_err.txt)"; then - : - else - reason="$(tr '\n' ' ' < perm_err.txt \ - | sed -E 's/(gh[pousr]|github_pat)_[A-Za-z0-9_]+//g' \ - | cut -c1-300)" - echo "::warning::Reviewer bootstrap permission lookup failed for ${PR_AUTHOR}: ${reason}" - perm="" - fi - rm -f perm_err.txt - case "$perm" in - admin|maintain|write) - echo "writer=true" >> "$GITHUB_OUTPUT" ;; - *) - echo "::warning::Skipping reviewer bootstrap: ${PR_AUTHOR} has permission '${perm:-none}', write required." ;; - esac - - - name: Bootstrap reviewer from PR head (first install only) - id: agy_bootstrap - if: steps.agy_bootstrap_auth.outputs.writer == 'true' - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 - with: - ref: ${{ github.event.pull_request.head.sha }} - fetch-depth: 1 - persist-credentials: false - path: .agy-bootstrap - - - name: Install bootstrapped reviewer - if: steps.agy_bootstrap.outcome == 'success' - run: | - set -eu - echo "::notice::Default branch has no reviewer yet; bootstrapping from the PR head." - mkdir -p scripts - cp .agy-bootstrap/scripts/agy-review.sh \ - .agy-bootstrap/scripts/_agy_print.sh \ - .agy-bootstrap/scripts/_agy_comment_body.sh scripts/ - # The style guide is read from the workspace too, and is equally absent on a first install. - if [ -f .agy-bootstrap/.github/agy-review.md ] && [ ! -f .github/agy-review.md ]; then - mkdir -p .github && cp .agy-bootstrap/.github/agy-review.md .github/ - fi - rm -rf .agy-bootstrap - + # The template's first-install BOOTSTRAP steps are deliberately omitted here: the reviewer + # is already on this repository's default branch, so they could never fire, and their + # PR-head checkout trips CodeQL's actions/untrusted-checkout (high) on every scan. + # Re-add them only for a repository that has never had the reviewer on its default branch. - name: Run Antigravity review env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 80a85bc525470ad900e5983ed67144570c88dc24 Mon Sep 17 00:00:00 2001 From: DoubleGate Date: Sat, 14 Nov 2026 07:07:36 -0500 Subject: [PATCH 4/4] ci: pick up template 07bd8bd (review-round fixes) From the Antigravity reviews of these template-sync PRs; each fix has a self-test check that failed before it: - agy_body_archive no longer drops a clean round's closing
when the round quotes a standalone
or uses
. - agy_cap_prompt checks the size first, so a prompt that already fits is not cut and the model is not told its diff was truncated. - agy_cut_file refuses a non-positive size rather than handing GNU `head -c -N` a negative count. - The self-test budget uses a pinned AGY_PRINT_TIMEOUT when one is set. - MAX_PROMPT_BYTES defaults to 120000 (its ceiling), ending the spurious clamp warning on every run. Co-Authored-By: Claude Opus 5.5 --- .github/AGY-REVIEWER.md | 2 +- .github/workflows/antigravity-review.yml | 2 +- scripts/_agy_comment_body.sh | 29 ++++----- scripts/agy-review-selftest.sh | 83 +++++++++++++++++++----- scripts/agy-review.sh | 11 +++- 5 files changed, 87 insertions(+), 40 deletions(-) diff --git a/.github/AGY-REVIEWER.md b/.github/AGY-REVIEWER.md index c930d7b9..b556eb18 100644 --- a/.github/AGY-REVIEWER.md +++ b/.github/AGY-REVIEWER.md @@ -274,7 +274,7 @@ runner, so disable fork workflows there or gate them the same way. | `AGY_RETRIES` | `3` (workflow: `2`) | attempts; a retry is for a transient backend error, so more of them at the same budget only repeat a timeout | | `AGY_DIFF_MODE` | `auto` | how the diff reaches agy: `inline` (in the `--print` prompt), `file` (written to a file agy reads with its tools), or `auto` (inline if it fits the arg-size budget, else file — so **large PRs are never truncated**) | | `MAX_DIFF_BYTES` | `5000000` | sanity cap on a pathological diff (5 MB). Not the arg-size limit — a large diff goes to agy as a file, not an argv value | -| `MAX_PROMPT_BYTES` | `125000` | the inline/file threshold in `auto` mode, and a hard backstop on the **inlined** prompt. agy takes an inlined prompt as a `--print` argv value, and a single `execve` argument cannot exceed `MAX_ARG_STRLEN` (128 KiB); over it `execve` fails with E2BIG before agy starts. Clamped below 128 KiB | +| `MAX_PROMPT_BYTES` | `120000` | the inline/file threshold in `auto` mode, and a hard backstop on the **inlined** prompt. agy takes an inlined prompt as a `--print` argv value, and a single `execve` argument cannot exceed `MAX_ARG_STRLEN` (128 KiB); over it `execve` fails with E2BIG before agy starts. Clamped below 128 KiB | | `STYLE_GUIDE` | `.github/agy-review.md` | repo-relative style guide, loaded if present | --- diff --git a/.github/workflows/antigravity-review.yml b/.github/workflows/antigravity-review.yml index 60a245de..c4d94d7a 100644 --- a/.github/workflows/antigravity-review.yml +++ b/.github/workflows/antigravity-review.yml @@ -132,7 +132,7 @@ jobs: # MAX_DIFF_BYTES: "5000000" # sanity cap on a pathological diff (5 MB). Do NOT lower # # this to bound the review: it truncates the diff BEFORE the # # file handoff, so a low value defeats the large-PR path. - # MAX_PROMPT_BYTES: "125000" # inline/file threshold + hard backstop on the argv prompt + # MAX_PROMPT_BYTES: "120000" # inline/file threshold + hard backstop on the argv prompt # STYLE_GUIDE: .github/agy-review.md # style guide, loaded if present run: | # The workflow and the scripts come from DIFFERENT REFS: for a `pull_request` diff --git a/scripts/_agy_comment_body.sh b/scripts/_agy_comment_body.sh index 84519914..83d5b302 100755 --- a/scripts/_agy_comment_body.sh +++ b/scripts/_agy_comment_body.sh @@ -76,25 +76,18 @@ agy_body_archive() { # emit it verbatim -- dropping it would silently destroy the review history, which is # a worse failure than the nesting this guards against. start = (first >= 0) ? first : 0 + # Trim a trailing wrapper close (plus blank lines before it) ONLY when an older wrapper + # was actually skipped above (first > 0): that is the one case with a stray close to + # remove. The trim used to run on every archive, guarded by a whole-archive count of + # `^
` opens against `
` closes, and a CLEAN archive whose round quotes + # a standalone `
` or uses `
` tipped that count and lost a + # legitimate closing tag (agy reviews of SLAC #24 and Rusty2600 #53). + if (first > 0) { + while (n > start && buf[n-1] == "") n-- + if (n > start && buf[n-1] == "
") n-- + } for (i = start; i < n; i++) print buf[i] - }' \ - | awk 'BEGIN { n = 0 } - { line[n++] = $0 } - END { - # Trim a trailing wrapper close (plus any blank lines before it) left by a - # pre-migration body. A legitimate round always ends with its own
- # followed by nothing, so only ONE trailing close is ever removed here. - while (n > 0 && line[n-1] == "") n-- - if (n > 0 && line[n-1] == "
") { - closes = 0; opens = 0 - for (i = 0; i < n; i++) { - if (line[i] ~ /^
/) opens++ - if (line[i] == "
") closes++ - } - if (closes > opens) n-- - } - for (i = 0; i < n; i++) print line[i] - }' || true + }' || true } # Prefix of the hidden line that records when a review was posted. The comment head carries diff --git a/scripts/agy-review-selftest.sh b/scripts/agy-review-selftest.sh index 0646f466..36a62857 100755 --- a/scripts/agy-review-selftest.sh +++ b/scripts/agy-review-selftest.sh @@ -198,6 +198,22 @@ check "a body with no archive yields an empty archive" "" \ check "the head never carries the marker into the archive" "" \ "$(printf '%s\nX\n' "$MARKER" | agy_body_head "$MARKER" | grep -F -x "$MARKER" || true)" +# REGRESSION (agy reviews of SLAC #24 and Rusty2600 #53): the pre-migration tail trim used to run +# on EVERY archive and drop the last `` whenever closes outnumbered `^
` opens. +# A clean archive whose round QUOTES a standalone `
` (a code block, a review of this very +# script) or uses `
` tips that count and lost a legitimate closing tag. +clean_quote="$(printf '%s\n
\nRound reviewed at X\n\nreview text\n```html\n
\n```\n\n
' "$AGY_ROUND_MARK")" +check "a clean archive quoting a standalone keeps its closing tag" "$clean_quote" \ + "$(printf '%s\n%s\n%s\n%s\n' "$MARKER" "$AGY_ARCHIVE_START" "$clean_quote" "$AGY_ARCHIVE_END" | agy_body_archive)" +clean_open="$(printf '%s\n
\nRound reviewed at X\n\n
\nlog\nL\n
\n\n
' "$AGY_ROUND_MARK")" +check "a clean archive with a
block keeps its closing tag" "$clean_open" \ + "$(printf '%s\n%s\n%s\n%s\n' "$MARKER" "$AGY_ARCHIVE_START" "$clean_open" "$AGY_ARCHIVE_END" | agy_body_archive)" +# The migration path must still heal: a pre-migration wrapper captured inside the sentinels ahead +# of the first round mark, with its stray trailing close, comes out as the bare rounds. +legacy_wrap="$(printf '
\nEarlier review rounds\n\n%s\n
' "$clean_quote")" +check "a pre-migration wrapper and its stray close are still removed" "$clean_quote" \ + "$(printf '%s\n%s\n%s\n%s\n' "$MARKER" "$AGY_ARCHIVE_START" "$legacy_wrap" "$AGY_ARCHIVE_END" | agy_body_archive)" + # Trimming must terminate: with no `
` left, dropping fails rather than looping. check "dropping from an empty archive fails rather than spinning" "1" \ "$(printf 'no rounds here\n' | agy_drop_oldest_round >/dev/null 2>&1; echo $?)" @@ -330,6 +346,18 @@ check "an oversized review round is capped" "yes" \ "$([ "$(wc -c < "$TMPD/round")" -le 1000 ] && echo yes || echo no)" check "a capped review round says it was truncated" "yes" \ "$(grep -q 'Review truncated' "$TMPD/round" && echo yes || echo no)" +# REGRESSION (agy reviews of SLAC #24 and RustyNES_MiSTer #78): agy_cap_prompt cut at the budget +# MINUS its notice before checking the size, so a prompt that already fit -- anywhere in the last +# ~260 bytes below the cap -- was cut and falsely told the model its diff was truncated. +head -c 590 /dev/zero | tr '\0' 'f' > "$TMPD/fits" +check "a prompt that fits the cap is not cut" "kept:590" \ + "$(agy_cap_prompt "$TMPD/fits" 600 && echo "cut:$(wc -c < "$TMPD/fits")" || echo "kept:$(wc -c < "$TMPD/fits")")" +# A non-positive cut size must be refused, never handed to GNU `head -c -N` (all-but-last-N bytes). +printf '0123456789' > "$TMPD/neg" +check "a non-positive cut size is refused and the file is untouched" "kept:10" \ + "$(agy_cut_file "$TMPD/neg" -5 && echo "cut:$(wc -c < "$TMPD/neg")" || echo "kept:$(wc -c < "$TMPD/neg")")" +check "a cap smaller than its own notice never cuts with a negative size" "kept:10" \ + "$(agy_cap_prompt "$TMPD/neg" 50 && echo "cut:$(wc -c < "$TMPD/neg")" || echo "kept:$(wc -c < "$TMPD/neg")")" # --- UTF-8 scrub: one helper for the prompt AND the review output, executed ------- # The review-output path once had iconv only while the prompt path also fell back to python3 # (agy review, switch2-hub-patcher PR #1). Both now call agy_scrub_utf8; exercise each sanitizer. @@ -515,24 +543,43 @@ if [ "$(grep -c '^[[:space:]]*timeout-minutes:' "$wf")" -gt 1 ]; then echo " FAIL more than one timeout-minutes in the workflow: wf_val needs the review job's" fails=$((fails + 1)) fi -job_min="$(wf_val timeout-minutes)" -max_s="$(wf_val AGY_PRINT_TIMEOUT_MAX_SECONDS)"; [ -n "$max_s" ] || max_s="$(sh_default AGY_PRINT_TIMEOUT_MAX_SECONDS)" -lock_s="$(wf_val AGY_LOCK_WAIT)"; [ -n "$lock_s" ] || lock_s="$(sh_default AGY_LOCK_WAIT)" -retries="$(wf_val AGY_RETRIES)"; [ -n "$retries" ] || retries="$(sh_default AGY_RETRIES)" -delay="$(wf_val AGY_RETRY_DELAY)"; [ -n "$delay" ] || delay="$(sh_default AGY_RETRY_DELAY)" -if [ -z "$job_min" ] || [ -z "$max_s" ] || [ -z "$lock_s" ] || [ -z "$retries" ] \ - || [ -z "$delay" ]; then - echo " FAIL could not read the timeout budget (job=$job_min max=$max_s lock=$lock_s retries=$retries delay=$delay)" - fails=$((fails + 1)) -else - worst=$(( lock_s + retries * max_s + delay * retries * (retries - 1) / 2 )) - if [ "$worst" -lt $(( job_min * 60 - 120 )) ]; then - echo " ok worst case ${worst}s fits the ${job_min}-minute job timeout with 2 minutes to spare" - else - echo " FAIL worst case ${worst}s does not fit the ${job_min}-minute job timeout" - fails=$((fails + 1)) - fi -fi +# A workflow that PINS AGY_PRINT_TIMEOUT bypasses the per-MiB scaling and its cap entirely +# (agy-review.sh: AGY_PRINT_TIMEOUT_EXPLICIT), so every attempt waits the pinned value; the budget +# must use it, or raising the pin past the cap could exceed timeout-minutes unnoticed (agy review +# of RustyNES_MiSTer #78). The value is a duration ("15m"), so wf_val's digits-only read won't do. +wf_pin() { sed -n "s/^[[:space:]]*AGY_PRINT_TIMEOUT:[[:space:]]*[\"']\{0,1\}\([0-9][0-9]*[smh]\{0,1\}\).*/\1/p;T;q" "$wf"; } +# budget_verdict: "fits" or "exceeds"; empty when the budget cannot be read. Uses $wf. +budget_verdict() { + local job_min max_s lock_s retries delay pin per worst + job_min="$(wf_val timeout-minutes)" + max_s="$(wf_val AGY_PRINT_TIMEOUT_MAX_SECONDS)"; [ -n "$max_s" ] || max_s="$(sh_default AGY_PRINT_TIMEOUT_MAX_SECONDS)" + lock_s="$(wf_val AGY_LOCK_WAIT)"; [ -n "$lock_s" ] || lock_s="$(sh_default AGY_LOCK_WAIT)" + retries="$(wf_val AGY_RETRIES)"; [ -n "$retries" ] || retries="$(sh_default AGY_RETRIES)" + delay="$(wf_val AGY_RETRY_DELAY)"; [ -n "$delay" ] || delay="$(sh_default AGY_RETRY_DELAY)" + pin="$(wf_pin)" + per="$max_s" + if [ -n "$pin" ]; then per="$(duration_to_seconds "$pin")" || return 0; fi + [ -n "$job_min" ] && [ -n "$per" ] && [ -n "$lock_s" ] && [ -n "$retries" ] && [ -n "$delay" ] || return 0 + worst=$(( lock_s + retries * per + delay * retries * (retries - 1) / 2 )) + if [ "$worst" -lt $(( job_min * 60 - 120 )) ]; then echo "fits ${worst}s ${job_min}m"; else echo "exceeds ${worst}s ${job_min}m"; fi +} +verdict="$(budget_verdict)" +case "$verdict" in + fits*) echo " ok worst case ${verdict#fits } (job timeout) fits with 2 minutes to spare" ;; + exceeds*) echo " FAIL worst case ${verdict#exceeds } does not fit the job timeout"; fails=$((fails + 1)) ;; + *) echo " FAIL could not read the timeout budget from $wf"; fails=$((fails + 1)) ;; +esac +# The pin must count: the same workflow with a 50-minute pin has to be reported as exceeding. +real_wf="$wf" +sed 's/^\([[:space:]]*\)AGY_EFFORT:.*/&\n\1AGY_PRINT_TIMEOUT: 50m/' "$real_wf" > "$TMPD/pinned.yml" +wf="$TMPD/pinned.yml" +check "a pinned AGY_PRINT_TIMEOUT counts in the timeout budget" "exceeds" "$(budget_verdict | cut -d' ' -f1)" +wf="$real_wf" +# The prompt cap's default must sit at or below its own ceiling; above it, every run logged a +# spurious "invalid ... clamping" warning and overrode its own default (agy review of Rusty2600 #53). +ceiling="$(sed -n 's/^ARG_SIZE_CEILING=\([0-9][0-9]*\).*/\1/p' "$SCRIPT_DIR/agy-review.sh")" +check "the MAX_PROMPT_BYTES default is within ARG_SIZE_CEILING" "yes" \ + "$([ "$(sh_default MAX_PROMPT_BYTES)" -le "${ceiling:-0}" ] 2>/dev/null && echo yes || echo no)" # Both helpers run under the caller's `set -euo pipefail`. A head or archive far larger than a # pipe buffer must neither abort them nor be misread (an early-exiting reader used to SIGPIPE diff --git a/scripts/agy-review.sh b/scripts/agy-review.sh index 72aa558a..6a1dd631 100755 --- a/scripts/agy-review.sh +++ b/scripts/agy-review.sh @@ -220,7 +220,7 @@ AGY_DIFF_MODE="${AGY_DIFF_MODE:-auto}" # auto|inline|file. A diff is passed MAX_DIFF_BYTES="${MAX_DIFF_BYTES:-5000000}" # sanity cap on a pathological diff (5 MB). No longer the # arg-size limit -- a large diff goes to agy as a file, not # as an argv value -- just a guard against a runaway diff. -MAX_PROMPT_BYTES="${MAX_PROMPT_BYTES:-125000}" # hard ceiling on the INLINED prompt: agy takes it as a +MAX_PROMPT_BYTES="${MAX_PROMPT_BYTES:-120000}" # hard ceiling on the INLINED prompt: agy takes it as a # --print VALUE (not stdin), and a single execve argument # cannot exceed MAX_ARG_STRLEN (PAGE_SIZE*32 = 128 KiB on # Linux). Over it, execve fails with E2BIG before agy even @@ -659,8 +659,11 @@ truncated="" # be told so, and a notice appended before a later cut can be cut away -- so each cap that can hide # content writes its own notice AFTER cutting. Notices are plain ASCII: character count = bytes. # >>> SELFTEST-EXTRACT: truncation helpers -# Cut file $1 to at most $2 bytes. Succeeds only when it actually cut. +# Cut file $1 to at most $2 bytes. Succeeds only when it actually cut. A non-positive size is +# refused, never handed to GNU `head -c -N`, which prints all-but-the-last-N bytes (a tiny +# MAX_BODY_BYTES or MAX_PROMPT_BYTES minus a notice gets there; agy review of SLAC #24). agy_cut_file() { + [ "$2" -gt 0 ] 2>/dev/null || return 1 [ "$(wc -c < "$1")" -gt "$2" ] || return 1 head -c "$2" "$1" > "$1.cut" && mv "$1.cut" "$1" } @@ -674,6 +677,10 @@ agy_diff_truncation_notice() { # Cap prompt file $1 at $2 bytes, keeping room for a notice written after the cut. agy_cap_prompt() { local notice=$'\n\n--- NOTE: TRUNCATED PROMPT ---\nThe prompt, including the diff, was cut at the argument-size limit; you have NOT seen\nthe rest. Say at the top of your review that it covers a truncated diff, and do not state or\nimply that the whole change was reviewed.\n' + # Size FIRST, like agy_cap_round: cutting to the budget minus the notice before asking whether + # the prompt even exceeds the budget cut prompts that already fit, and told the model a whole + # diff was truncated (agy reviews of SLAC #24 and RustyNES_MiSTer #78). + [ "$(wc -c < "$1")" -gt "$2" ] || return 1 agy_cut_file "$1" "$(( $2 - ${#notice} ))" || return 1 printf '%s' "$notice" >> "$1" }