Skip to content

feat(review): fall back to the next reviewer backend, ending with Claude Code - #679

Merged
amiddavid merged 2 commits into
mainfrom
feat/review-backend-fallback
Oct 23, 2026
Merged

amiddavid merged 2 commits into
mainfrom
feat/review-backend-fallback

Conversation

@amiddavid

Copy link
Copy Markdown
Collaborator

Summary

  • When a reviewer's backend is not set up, or it fails before that reviewer posts, the next backend runs that reviewer.
  • Default order: glm, then bob, then claude. claude is the user's own Claude Code, so it is the Anthropic fallback.
  • Since feat(review): add a GLM reviewer backend and make it the default #675, a colleague without a GLM key file got a failed review at once. Now their review falls back to Bob.

What changed

  • scripts/offload-review.sh:
    • Each reviewer tries the chosen backend, then each name in OFFLOAD_FALLBACK (default bob claude).
    • A reviewer never posts twice. The next backend runs only while the PR has no review of that reviewer, round and head.
    • A backend that is not set up is skipped without a run, through a new optional backend_ready.
    • Each backend has its own worktree, review-pr<N>-<backend>-r<1|2>, so the session files of 2 backends never mix.
    • In round 2, each reviewer starts with the backend that wrote its round-1 review, so it resumes that session.
    • The last line lists the backend of each reviewer, for example (reviewer 1: glm reviewer 2: bob).
    • --no-fallback, or OFFLOAD_FALLBACK="", turns it off. Use it to compare one backend with another.
    • Worktree creation takes a lock. With fallback, both reviewers can create a worktree at the same moment.
  • backends/claude.sh (new): runs claude -p with the user's own login. OFFLOAD_CLAUDE_MODEL picks the model.
  • backends/glm.sh, backends/bob.sh: add backend_ready.
  • SKILL.md: documents the fallback and the claude backend.

How it was tested

The real script ran in a scratch git repo, with a fake gh and 3 fake backends. Each fake backend can be marked "not set up" or "fails". No model was called.

Case Backends run Result
glm works glm ×2 exit 0, both reviews from glm
glm not set up bob ×2 (glm skipped, no run) exit 0, both from bob
glm fails glm ×2, then bob ×2 exit 0, both from bob
glm and bob fail glm, bob, claude, ×2 each exit 0, both from claude
all fail all 3, ×2 each exit 1, names each backend and its review.log
--no-fallback, glm fails glm ×2 only exit 1
round 2 after a bob round 1, glm now working bob ×2, resume=1 exit 0. Round 2 kept bob and resumed its session.
--cleanup removed all 6 worktrees

bash -n passes on the script and all 3 backends.

🤖 Generated with Claude Code

…ude Code

When GLM was made the default (#675), a colleague without a GLM key
file got a failed review at once: there was no fallback.

- Each reviewer now tries the backends in order: the chosen one, then
  OFFLOAD_FALLBACK (default "bob claude"). So the default order is
  glm, then bob, then claude.
- The next backend runs only when the reviewer has not posted a review
  of this round and head. A reviewer never posts twice.
- A backend may define backend_ready. One that is not set up (no key
  file, no bob command) is skipped without a run.
- Each backend has its own worktree, review-pr<N>-<backend>-r<1|2>, so
  its session files never mix with another backend's.
- In round 2 each reviewer starts with the backend that wrote its
  round-1 review, so it resumes that session.
- The marker names the backend that wrote the review, and the last line
  lists the backend of each reviewer.
- --no-fallback (or OFFLOAD_FALLBACK="") turns it off, to compare one
  backend with another.
- New backend claude.sh: the user's own Claude Code, with
  OFFLOAD_CLAUDE_MODEL to pick the model.
- Worktree creation takes a lock, because with fallback the two
  reviewers can create worktrees at the same moment.

Assisted-By: Claude
Signed-off-by: DAVID AMID <[email protected]>

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.309 offload-review skill, @amiddavid's session fresh 1 of 2 1 of 2 f067b21ec176 blocking

Verdict: blocking

Findings

  1. scripts/offload-review.sh:184-187 — posted_now can say "not posted" when the review is posted, so the next backend posts a second review
    • What fails: a reviewer posts its review, then posted_now checks the PR. grep -q exits as soon as it finds the marker. If gh is still writing review bodies at that moment, it gets SIGPIPE and exits 141. set -o pipefail (line 43) makes posted_now return 141. run_reviewer reads that as "not posted" and runs the next backend for the same reviewer. That backend posts a second review of the same reviewer, round and head. The script then exits 0 and reports success. This breaks the rule "a reviewer never posts twice", and it costs a full extra review run.
    • Proof: the mechanism, on this machine with /bin/bash 3.2.57 and the default grep:
      set -euo pipefail
      posted_now() { { printf 'match early\n'; head -c 200000 /dev/zero | tr '\0' 'x'; } | grep -q 'match early'; }
      if posted_now; then echo posted; else echo "not posted (rc=$?)"; fi
      Output: posted: NO (rc=141). The match was found; the pipeline still failed. In the real call, gh api --paginate writes each review body as its own output, so any bytes after the marker (the rest of that review, later reviews, a next page) give gh a write that fails after grep exits. It is a race: it needs enough text after the marker to span a write. Review bodies of a few KB, and more rounds, make it more likely. The same fetch at line 316 is safe, because command substitution reads all output before grep runs.
    • Fix: read the whole output first, then grep, in the style of posted() at line 123:
      posted_now() {
        local now
        now=$(gh api --paginate "repos/$repo/pulls/$pr/reviews" --jq '.[].body')
        grep -qE "<!-- offload-review reviewer=$1 round=$round sha=$sha backend=[a-z0-9_-]+ verdict=(approve|blocking)" <<<"$now"
      }

Non-blocking

  • scripts/offload-review.sh:208 — exit code 4 means "backend not set up" only by contract. attempt also returns backend_run's own exit code. A reviewer tool that itself exits 4 is reported as "not set up", with no review.log path in the message. Pick an exit code the tools cannot return, or have attempt wrap the tool's code.
  • scripts/offload-review.sh:191-198 and 329-330 — the marker greps scan whole review bodies. From round 2 on, a review may quote another reviewer's marker line. The quote then matches too, and tail -1 can pick the quoted backend. Effect: round N+1 starts with the wrong backend first, or the summary line names the wrong backend. Low harm: the review still posts, only the resume order can be wrong. Anchoring the grep to line 1 of a body would need a different fetch; at least keep this in mind.
  • scripts/offload-review/backends/glm.sh:3 and bob.sh:3, and SKILL.md "Add a backend" — these still say a backend defines "one optional" function. backend_ready is a second optional one now. Update the three headers so a new backend author learns about backend_ready.
  • The new fallback logic (chain order, posted_now, worktree lock) has no tests. The repo has no shell test harness at all, so this matches current practice. The pure parts (chain_for, the marker parsing) could be tested if a harness ever appears.

What I checked

  • The full diff of head f067b21 against base 40868e3: SKILL.md, offload-review.sh, and the three backends, line by line.
  • The whole current scripts/offload-review.sh, backends/glm.sh, backends/bob.sh, backends/claude.sh, SKILL.md, review-prompt.md, and docs/design.md.
  • Every call site of wt_of after its argument order changed from <reviewer> to <backend> <reviewer>: all consistent (lines 167, 210, 288, 320).
  • The fallback invariants the PR promises: dedup of the chain (line 71-72), per-reviewer fallback, "never posts twice" via posted_now, round-2 resume order via chain_for. The first three hold; the posted_now race above breaks the last two under the conditions given.
  • Session survival across rounds: git clean -e .glm-cc/ -e .claude-review-session (line 174) keeps the glm and claude session state; bob keeps its sessions outside the worktree. The changed-files allowlist (line 325) covers both new names.
  • The cleanup path (lines 98-111) with the new per-backend worktree names, and the claude.sh cleanup guards on the session id.
  • Bash semantics on this machine, with tests, not by reading alone: empty-array expansion under set -u in chain_for (safe in bash 3.2.57 and 5.2), and the grep -q + pipefail race in finding 1 (reproduced, rc=141).
  • Did not run the script itself, any Go build, or any remote-box test: live tests are not approved, and this PR touches no Go code.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.309 offload-review skill, @amiddavid's session fresh 2 of 2 1 of 2 f067b21ec176 blocking

Verdict: blocking

Findings

  1. scripts/offload-review.sh:185 — posted_now can call a posted review "not posted", so the next backend runs and posts a duplicate review.
    • What fails: gh api ... | grep -q runs under set -o pipefail (line 43). grep -q exits at the first match and closes the pipe. If gh api writes after that, it dies of a broken pipe (SIGPIPE, exit 141). A failed gh api call gives the same result. pipefail then makes the whole pipeline non-zero, so posted_now reports false even though the review is on the PR. run_reviewer (lines 281-290) records a failure and starts the next backend. That backend writes and posts a second review of the same reviewer, round and head. This breaks the invariant "a reviewer never posts twice" (SKILL.md) and spends a second full model run, with no error reported.
    • Proof: the mechanism, run on this machine:
      $ bash -c 'set -uo pipefail
      prod() { printf "%s\n" "MARKER"; sleep 0.3; python3 -c "print(\"x\"*200000)"; }
      if prod | grep -q "MARKER"; then echo MATCH; else echo "FALSE NEGATIVE rc=$?"; fi'
      FALSE NEGATIVE rc=1
      
      grep -q matched, the producer then hit the closed pipe, and pipefail turned the match into a false negative. Code trace: line 43 sets pipefail; lines 184-187 pipe gh api into grep -q; line 281 if posted_now "$r" takes the false branch; lines 285-290 continue the loop over chain_for. Honest trigger analysis: the API lists reviews oldest first (checked on PR #675), so the marker usually sits near the end of the stream and the pipe drains. The realistic doors are (a) any transient gh api failure at exactly this call, and (b) more than the 64 KiB pipe buffer of review text after the marker line — two long reviews posted by the two parallel reviewers can approach that. Both are silent.
    • Fix: buffer the answer before grepping, and fail safe on a gh error — an uncertain answer must stop the chain, not start another backend. The final check (lines 316-331) still reports a truly missing review with exit 1:
      posted_now() {
        local now
        now=$(gh api --paginate "repos/$repo/pulls/$pr/reviews" --jq '.[].body') \
          || { echo "posted_now: gh api failed; assuming posted, not trying the next backend" >&2; return 0; }
        grep -qE "<!-- offload-review reviewer=$1 round=$round sha=$sha backend=[a-z0-9_-]+ verdict=(approve|blocking)" <<<"$now"
      }
      This also matches posted() at line 123, which greps a saved string.

Non-blocking

  • scripts/offload-review.sh:280 — attempt "$r" "$b" || rc=$? disables set -e and the ERR trap for the whole of attempt. Verified on this machine: a false inside a function called in a || list does not stop the function. Before this PR, the reviewer function ran backgrounded, where set -e applied: a mid-function failure aborted the run with the trap message. Now a failure in git merge-base (line 230) or while writing .review-prompt.md (lines 232-269) is swallowed, and the backend runs on a broken prompt (for example git diff ..HEAD after a failed merge-base). I found no likely trigger — the fetch at line 156 makes a merge-base failure implausible — but the safety net is gone. Consider set -e explicitly inside the subshell at line 205.
  • Doc drift around backend_ready. SKILL.md "Add a backend" (line 165) still says "three functions, and one optional one" and omits backend_ready; backends/glm.sh:3 and backends/bob.sh:3 say "one optional one" too, while backends/claude.sh:3-8 documents two. Also the script header (line 35) still says exit 1 means "a reviewer failed", while SKILL.md now says "No backend posted a review for a reviewer".
  • scripts/offload-review.sh:288 — the failure message names review.log even when none exists. When ensure_wt fails (line 209), the attempt exits before any log is written in that worktree, so the path in tried points at nothing.
  • backends/claude.sh runs with the user's own Claude Code settings. That is its stated purpose, but it also means the reviewer's traffic goes through whatever endpoint the user's settings define — here, Context Guru, a lossy context-management proxy. The glm backend isolates its config for exactly this reason. Worth a sentence in the backend header, so a user who sees odd review quality knows where to look.
  • scripts/offload-review.sh:319 — the final audit checks only worktrees of backends in this run's chain. A worktree left by an earlier run with a different --backend is skipped even if its prompt names this round and head. Only an audit hole; the reviewer that ran there was checked by that run.

What I checked

  • Read CLAUDE.md, docs/design.md, the full diff (40868e38..HEAD), the whole current scripts/offload-review.sh, all three files in scripts/offload-review/backends/, and the changed parts of .claude/skills/offload-review/SKILL.md.
  • Traced every call site of the new wt_of <backend> <reviewer> argument order; the cleanup glob and its backend-name parsing; the ensure_wt lock (creation, 60 s timeout, release on failure) and the git clean excludes (.glm-cc/, .claude-review-session); chain_for (prev-backend extraction from markers, dedup, round-2 ordering); attempt (ready check, worktree, prompt build, session resume, exit-code-4 protocol); run_reviewer (posted check, tried messages); the final verification loop (worktree selection by prompt grep, changed-files allowlist, HEAD check, used report); the round/todo/--same-head logic (unchanged by this PR).
  • Checked the claude backend for safety: the session-id regex guard before find -delete, the rm guards, sid file handling across git clean, and --maxdepth 2 against Claude Code's projects layout.
  • Checked the SKILL.md claims against the code: default order glm→bob→claude, per-reviewer fallback, "never posts twice" (broken by finding 1), round-2 resume of the round-1 backend, --no-fallback, exit-code table, cleanup list.
  • Ran no Go builds or tests (not approved). Ran two local shell simulations only; they touch no repo code. Confirmed the GitHub reviews API order on PR #675 (a different, merged PR; I did not read this PR's reviews or comments).
  • Found no test infrastructure for this script in the repo, so I do not count missing tests as a finding.

Found by both GLM reviewers of #679, round 1. Under pipefail,
`gh api ... | grep -q` fails with SIGPIPE when gh writes after grep has
matched. A posted review then reads as missing, and the next backend
posts a second review of the same reviewer.

posted_now now saves the answer, then greps it, as posted() does. A gh
failure counts as posted, so an uncertain answer stops the chain. The
final check still reports a review that is really missing.

Assisted-By: Claude
Signed-off-by: DAVID AMID <[email protected]>
@amiddavid

Copy link
Copy Markdown
Collaborator Author

Author reply to both reviewers, round 1 (head f067b21ec176).

Reviewer 1, finding 1, and reviewer 2, finding 1 (the same bug): posted_now can read a posted review as missing. Fixed in ed20f9da65ba.

  • Agreed on the cause. Under pipefail, gh api ... | grep -q fails with SIGPIPE when gh writes after grep has matched. The next backend would then post a second review.
  • posted_now now saves the answer, then greps it, as posted() does.
  • A gh failure now counts as posted (reviewer 2's point). An uncertain answer stops the chain. The final check still reports a review that is really missing, with exit 1.
  • Checked with reviewer 2's reproduction: the old form read "NOT posted (rc=1)" after a match. The new form reads "posted".
  • No other | grep -q is left in the script or the backends.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.309 offload-review skill, @amiddavid's session resumed 1 of 2 2 of 2 ed20f9da65ba approve

Verdict: approve

Findings

None. The one blocking finding of round 1 is fixed, and I verified the fix myself (below).

Status of the round-1 findings

  1. Mine, finding 1, and reviewer 2, finding 1 (the same bug): posted_now could read a posted review as missing, so the next backend would post a duplicate. Fixed in ed20f9da.
    • The fix matches what both reviews proposed: posted_now saves gh's whole answer in a variable, then greps it (scripts/offload-review.sh:188-193). No pipe into grep -q is left.
    • I verified the mechanism myself, on this machine with /bin/bash 3.2.57:
      • The old form, on a match with the producer still writing: posted: NO (rc=141) (round 1).
      • The new form, same producer: posted: yes.
      • A gh failure: prints "could not read the PR's reviews; not trying another backend" and returns 0, so the chain stops.
    • I checked the "counts as posted" choice for false success. It cannot hide a missing review. The final check (lines 321-336) fetches the reviews again and prints "Reviewer r did not post its review" with exit 1. If gh fails there too, set -e ends the script with exit 1. Both paths report the problem.
    • I checked the author's claim "No other | grep -q is left in the script or the backends": true. The remaining grep -q calls (lines 123, 192, 328, and bob.sh:25) all read a saved string, a here-string, or a file. None is on the right of a pipe.
  2. My non-blocking notes and reviewer 2's non-blocking notes: not fixed, not rejected. The commit changes only posted_now. These notes stay open for a later change; none blocks this PR:
    • Exit code 4 from a reviewer tool reads as "not set up" (mine).
    • Marker greps scan whole review bodies, so a quoted marker can mislead chain_for (mine; harmless here: this round's run picked glm, the backend I used in round 1, as intended).
    • Doc drift: backend_ready missing from the "Add a backend" docs and from the glm.sh and bob.sh headers (mine and reviewer 2's).
    • attempt "$r" "$b" || rc=$? disables set -e inside the attempt (reviewer 2; I checked their reasoning and agree, and agree no likely trigger exists).
    • The failure message names review.log even when none exists (reviewer 2).
    • The claude backend runs through the user's own endpoint (reviewer 2; stated purpose of the backend).

Non-blocking

  • scripts/offload-review.sh:282 — when posted_now returns 0 because gh failed, and an earlier backend was tried, run_reviewer prints "posted with $b". The message before it says the reviews could not be read, so the operator is not misled. A clearer wording would say "unknown".

What I checked

  • Last review: round 1, f067b21, 2026-10-23T10:20:56Z.
  • New commits read: 1 (ed20f9da fix(review): read the PR's reviews whole before grep in posted_now). Full diff read; only posted_now and its comment changed.
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from the three files .review-in-reviews.txt, .review-in-comments.txt, .review-in-inline.txt).
  • New items since my last review, and what I did about each:
    • Reviewer 2's round-1 review (10:21:55): read in full. Its blocking finding is the same bug as mine; fixed. Its non-blocking notes are listed above with a status.
    • Author reply (10:23:24): read in full. It states the fix, the "gh failure counts as posted" choice, and the "no other | grep -q" claim. I verified each claim myself; all three hold.
    • Commit ed20f9da: reviewed as a first review would. The change is small and correct; the commit message follows the repo rules (conventional title, Signed-off-by, Assisted-By).
  • Ran local shell simulations of the new posted_now (match path and gh-failure path) with /bin/bash 3.2.57, the shell this machine runs the script with. No Go builds, no remote-box tests: live tests are not approved.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.309 offload-review skill, @amiddavid's session resumed 2 of 2 2 of 2 ed20f9da65ba approve

Verdict: approve

Findings

None.

Earlier findings

  • My finding 1 and reviewer 1's finding 1 (the same bug): posted_now could read a posted review as missing, so the next backend posted a duplicate. Fixed in ed20f9da65ba.
    • The new form saves the whole answer with command substitution, then greps it. grep runs only after gh finished, so no broken pipe. Verified with the round-1 reproduction, reshaped for the new form: the match now reads "MATCH (fix works)" where the old form read "FALSE NEGATIVE".
    • A gh failure now returns 0, so an uncertain answer stops the chain instead of starting another backend. Verified that path too: it returns 0. The final check (lines 316-333 in the new file) still reports a truly missing review with exit 1, and a failure of that fetch aborts the script under set -e. The fail direction is now safe in every case I can reach.
    • The author's claim "no other | grep -q is left" holds: every remaining grep -q in the script and the backends reads a saved string or a file, never a live pipe.

Non-blocking

The round-1 non-blocking notes are still open. None blocks the merge; the author did not reply to them, and this round's budget is used.

  • Doc drift, flagged by both reviewers: backend_ready is missing from SKILL.md "Add a backend" and from the glm.sh:3 and bob.sh:3 headers; the script header (line 35) still words exit 1 as "a reviewer failed".
  • Reviewer 1's note on exit code 4: a reviewer tool that itself exits 4 reads as "not set up". Agree after checking attempt — the code passes backend_run's status through.
  • Reviewer 1's note on quoted markers in chain_for and the summary line: agreed as low harm; the grep pins reviewer=$r, so a wrong backend needs a quoted marker of the same reviewer with a different backend.
  • My round-1 notes that stay: attempt ... || rc=$? disables set -e inside the attempt; the failure message at line 288 can name a review.log that does not exist; the claude backend inherits the user's endpoint; the final audit skips worktrees of backends outside this run's chain.

What I checked

  • Last review: round 1, f067b21, 2026-10-23T10:21:55Z.
  • New commits read: 1 — ed20f9da "fix(review): read the PR's reviews whole before grep in posted_now". Read the full diff and the changed function in the file around it.
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from the three saved files; the inline file is empty).
  • New items since my last review, and what I did:
    • Commit ed20f9da: reviewed line by line. It changes only posted_now. Checked both exit paths (answer read whole; gh failure returns 0), the local now on its own line, and that the error message goes to stderr. No regression found: posted_now is used only by run_reviewer, and the final verification still catches a missing review.
    • Author's comment at 10:23:24: checked each claim. The fix matches the description; the "no other | grep -q" claim verified by grep over the script and the backends.
    • Reviewer 1's round-1 review (posted at 10:20:56, before my round-1 review; I read it now for the first time): its finding 1 is the same bug as mine, now fixed. Its non-blocking notes checked as described above; I do not repeat them as findings.
  • Ran no Go builds or tests (not approved). Ran two local shell simulations of the fixed function only; they touch no repo code.

@amiddavid
amiddavid merged commit c79d9ff into main Oct 23, 2026
4 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant