Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
23680a4
docs: update the PR 3 plan for #8-#10 and split it into four PRs
catalan-adobe Nov 28, 2026
c41c9d1
Add the config file reader and the per-PR state folder with its lock
catalan-adobe Nov 28, 2026
52573ab
Keep run files in one state folder per PR, with a lock
catalan-adobe Nov 28, 2026
14c4ab5
Lowercase host, owner and repo in the PR state folder
catalan-adobe Nov 28, 2026
c104eb8
Wait for a lock's pid file before taking the lock over, and write it …
catalan-adobe Nov 28, 2026
d53ffc7
Require a JSON object config file and give the default for an empty s…
catalan-adobe Nov 28, 2026
49ef71c
Print non-string pr.json values as compact JSON
catalan-adobe Nov 28, 2026
cfff879
Make shared review worktrees depend on the read-only reviewer too
catalan-adobe Nov 28, 2026
1a8a69d
Serialize taking over a stale lock and check it again before replacin…
catalan-adobe Nov 28, 2026
a770ff6
Make the PR and run folders owner-only whatever the umask
catalan-adobe Nov 28, 2026
9867867
Resolve a relative GH_PR_REVIEW_STATE against the launch folder once
catalan-adobe Nov 28, 2026
0ba1c66
Name the run folder on every exit once it exists
catalan-adobe Nov 28, 2026
f824b34
Take end of input at the loop-stop prompt as no
catalan-adobe Nov 28, 2026
5332a06
Refuse a pr.json that is not exactly one JSON object
catalan-adobe Nov 28, 2026
01059be
Write loop-check.md to a temporary file and move it in once complete
catalan-adobe Nov 28, 2026
b364f1a
Establish lock ownership by PID only, and refuse locks whose owner is…
catalan-adobe Nov 28, 2026
69ad063
Refuse a loop-check.md that is not a regular file
catalan-adobe Nov 28, 2026
5cc1ea5
Merge remote-tracking branch 'origin/main' into review-state-folder
catalan-adobe Nov 28, 2026
72e5921
Shell-quote the lock path in the printed rm command
catalan-adobe Nov 28, 2026
e7ef746
Ignore a relative XDG_STATE_HOME
catalan-adobe Nov 28, 2026
b4ddcfa
Point the no-marker warning at the final run folder
catalan-adobe Nov 28, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,18 @@ point; this PR branches inside the JSON exporter instead ...

## Artifacts

Each run writes its files to `./pr-review-sessions/pr-review-XXXXX/` in
your current directory, so you can inspect or re-post later:
Each PR has one state folder, `<state>/<host>/<owner>/<repo>/<N>/`, where `<state>` is `$GH_PR_REVIEW_STATE` (a
relative path is taken from the folder the command runs in), else `${XDG_STATE_HOME:-~/.local/state}/gh-pr-review`.
The PR's folder and its run folders are readable by you only (0700), whatever the umask:

- `round-<K>/`: the files of the run that posted round K; `preview-<time>/`: a `--no-post` run; `run-<time>/`: a
run that is still working, or that ended without posting (an error, exit 6). The run prints its folder.
- `loop-check.md`: the last loop diagnosis that stopped a run (or `--check`)
- `lock/`: held while a review of the PR runs, so a second one on this machine is refused; a lock left by a killed
run is taken over, by one run at a time (`lock.takeover/`, held for that moment only)
- `pr.json`: facts about the PR kept between runs

A run folder holds:

- `owner-prompt.md` and `approach.md`: the approach pass
- `prompt.md` and `review.md`: the code review (absent after a Rethink); `review-body.md` is the reply
Expand All @@ -119,7 +129,8 @@ your current directory, so you can inspect or re-post later:
- `context/`: the PR description, the discussion and the inline review threads both passes read
- `repo/`: the checkout

Add `pr-review-sessions/` to `.gitignore` if you run it inside a repo.
Earlier versions wrote to `./pr-review-sessions/`; those folders are left alone. The optional config file is
`${XDG_CONFIG_HOME:-~/.config}/gh-pr-review/config.json`.

## Round history

Expand All @@ -131,7 +142,7 @@ the approach and "Ready to merge?" verdicts, and the findings (severity,
record is saved as `round.json`. The record on GitHub holds only what the
review already says: a title or path the posted review does not show is left
out of the marker, and the `--instructions` text and the prompts are never in
it. `round.json` in the session folder is not filtered: it keeps everything the
it. `round.json` in the run folder is not filtered: it keeps everything the
model returned, so inspect it before sharing it.

Not every review that looks generated is a round. Anyone who can comment on
Expand Down Expand Up @@ -288,7 +299,7 @@ Rounds are numbered in the order GitHub received them. Just before posting,
the tool reads the rounds again and compares them, by GitHub review ID, with
the rounds it started from: if someone posted one while this review ran (or a
round was deleted, or edited so it is no longer one), it says who, posts
nothing, keeps the session folder, and exits with code 6, in a terminal too:
nothing, keeps its `run-<time>` folder, and exits with code 6, in a terminal too:
run the review again to review on top of the rounds as they are now. If
the rounds can't be read again (one retry), it posts nothing either, keeps
the session, and exits with code 1. Two runs that post at the very same
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,44 @@ Expected: all `ok`, no lint output.
`--fresh` on one large repo. No review text.
- [ ] **Step 5: Check.**
- [ ] **Step 6: Commit.** `git commit -m "Document local review worktrees and check them on a real PR"`

---

## Changes after PRs #8, #9 and #10

Written after #8, #9 and #10 merged. Where this section and the tasks above disagree, this section wins.

**Four PRs instead of one.** The tasks above would make one PR well past the loop check's "too big" threshold (800
lines or 25 files), the flag #8 and #9 carried through 7 and 4 rounds. They ship as four PRs:

| PR | Tasks | Needs |
|---|---|---|
| 3a: one state folder per PR, with a lock | 1 | `main` |
| 3b: a read-only reviewer | 4 | `main` |
| 3c: local clones, review worktrees and private refs | 2, 3 | 3a and 3b |
| 3d: `gh pr-review gc`, docs and a real run | 5, 6 | 3c |

3a and 3b are independent. 3b guards today's throwaway clone (`tripwire_snapshot <repo> <repo>`); 3c passes the clone
and the review worktree. 3c needs both: a review worktree shares refs, objects, stash and config with the user's clone,
so it is not the default until both pi passes run with the read-only git and the tripwire (3b) and the state folder and
lock (3a) are in.

**Task 1 (3a).**
- Today the run folder is `$(pwd)/pr-review-sessions/pr-review-XXXXX`, under whatever folder the command runs from.
Old folders are left alone.
- After a post, the run folder is renamed `round-<K>`, where K is the `.round` of the record that was posted. That is
the number left after the final re-read, not `LOOP.next_round`. A run that exits 6 (a round landed during the
review), fails to post, or can't confirm its post keeps `run-…`.
- The lock is per PR, on this machine. It complements the exit-6 check, which catches rounds from other machines and
accounts. `--check` and `rounds` don't take it.

**Task 3 (3c).**
- `fetch_pr` keeps #9's rules: a SHA is validated with `delta_object_id` before any git call, `--end-of-options` comes
before refs, and credentials come from `GIT_AUTH`.
- `pick_delta` reads the previous head and base from `refs/pr-review/<N>/round-<K-1>` and `…-base` first, then from
the record (`head_sha`, `base_sha`). If the base is still unknown, #9's rule applies: the round is `full`.
- `mark_round` uses the posted record's round.
- The deepen-by-200 fallback for a missing merge-base stays in `--fresh` only. A local clone has its full history.

**Task 4 (3b).** `git-ro` allows the commands the round-2 prompts ask the reviewer to run (`diff`, `range-diff`,
`log`, `show`, `diff --stat`). The approach pass runs in the same sandbox.
48 changes: 40 additions & 8 deletions gh-pr-review
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
# GH_PR_REVIEW_MODEL Default model override (same as --model)
# GH_PR_REVIEW_PI_EXTENSION Path to a pi extension to load during the review
# GH_PR_REVIEW_USER Default gh account (same as --user)
# GH_PR_REVIEW_STATE State folder (default: ${XDG_STATE_HOME:-~/.local/state}/gh-pr-review)
# GITHUB_TOKEN For private repos (gh auth is usually sufficient)

set -euo pipefail
Expand All @@ -50,6 +51,8 @@ for LIB_FILE in "$LIB_DIR"/*.sh; do
# shellcheck source=/dev/null
source "$LIB_FILE"
done
# A relative state folder is the launch folder's, for the whole run.
state_root_pin

# ── subcommands ─────────────────────────────────────────────────────────────
# A known first word is a subcommand; anything else is a PR to review.
Expand Down Expand Up @@ -348,6 +351,8 @@ fi

# ── resolve owner/repo/PR number ─────────────────────────────────────────────
resolve_target "$TARGET"
# The PR's state folder: its runs, its lock, pr.json and the last loop check (lib/state.sh).
PR_STATE_DIR=$(pr_state_dir "$GH_HOST_NAME" "$OWNER" "$REPO" "$PR_NUM") || exit 1

# ── fetch PR metadata ────────────────────────────────────────────────────────
echo "🔍 PR #${PR_NUM} — ${OWNER}/${REPO}"
Expand Down Expand Up @@ -406,6 +411,7 @@ ID_FLOOR=$(jq -c -L "$LIB_DIR" 'include "records"; id_floor' <<<"$RECORDS")

if [[ "$CHECK_ONLY" == "true" ]]; then
if [[ "$CHECK_JSON" == "true" ]]; then echo "$LOOP" >&3; else loop_text "$LOOP"; fi
[[ "$LOOP_DECISION" != stop ]] || loop_check_save "$LOOP"
case "$LOOP_DECISION" in
stop) exit 3 ;;
nothing-new) exit 5 ;;
Expand Down Expand Up @@ -438,13 +444,16 @@ stop)
if [[ "$FORCE_ROUND" == "true" ]]; then
FORCED=true
elif [[ -t 0 && "$YES" == "false" ]]; then
read -r -p "Run round ${ROUND} anyway? [y/N] " LOOP_ANSWER
# End of input (Ctrl-D) is the default, No.
read -r -p "Run round ${ROUND} anyway? [y/N] " LOOP_ANSWER || LOOP_ANSWER=""
[[ "$LOOP_ANSWER" == [Yy]* ]] || {
loop_check_save "$LOOP"
echo "Stopped by the loop check."
exit 3
}
FORCED=true
else
loop_check_save "$LOOP"
echo "Stopped by the loop check (exit 3). --force-round runs anyway." >&2
exit 3
fi
Expand All @@ -462,9 +471,21 @@ echo " round: ${ROUND}"
echo ""

# ── clone + checkout ─────────────────────────────────────────────────────────
SESSIONS_DIR="$(pwd)/pr-review-sessions"
mkdir -p "$SESSIONS_DIR"
SESSION_DIR=$(mktemp -d "${SESSIONS_DIR}/pr-review-XXXXX")
# One run per PR at a time on this machine; the run works in <PR_STATE_DIR>/run-<time>, renamed when it ends.
pr_lock || exit 1
SESSION_DIR=$(run_dir_new) || exit 1
# From here on, every exit names the run folder, by its final name once it has one (on stderr after a failure), and
# releases the lock. The exit status stays.
run_exit() {
local rc=$?
if [[ $rc == 0 ]]; then
echo " Session: ${SESSION_DIR}" || true
else
echo " Session: ${SESSION_DIR}" >&2 || true
fi
pr_unlock || true
}
trap run_exit EXIT
REVIEW_DIR="${SESSION_DIR}/repo"
PROMPT_FILE="${SESSION_DIR}/prompt.md"
OWNER_PROMPT_FILE="${SESSION_DIR}/owner-prompt.md"
Expand Down Expand Up @@ -1160,7 +1181,7 @@ else
# Only a Rethink may post the approach alone; an empty code review is a failed run.
if [[ -z "${CODE_REVIEW//[[:space:]]/}" ]]; then
echo "Error: the code review came back empty — pi may have failed or timed out." >&2
echo " Nothing was posted. Session: ${SESSION_DIR}" >&2
echo " Nothing was posted." >&2
echo " Try another model: --model github-copilot/gpt-5.5" >&2
exit 1
fi
Expand Down Expand Up @@ -1384,6 +1405,7 @@ posted_record() {
fi
}

POSTED=false
if [[ "$POST_TO_PR" == "true" ]] && [[ -n "$REVIEW" ]]; then
if [[ -z "$DECISION" && "${APPROACH_VERDICT:-}" == rethink ]]; then
DECISION=request-changes
Expand Down Expand Up @@ -1440,7 +1462,6 @@ if [[ "$POST_TO_PR" == "true" ]] && [[ -n "$REVIEW" ]]; then
# of it. Read the history again first; when that read fails twice, nothing is posted either.
if ! NOW_RECORDS=$(rounds_now); then
echo "Error: could not read the PR's rounds again, so the history could not be verified; nothing was posted." >&2
echo " Session: ${SESSION_DIR}" >&2
exit 1
fi
# The same rounds, by GitHub review ID and in order: a new round, a round gone or one that is no longer a round (its
Expand All @@ -1456,7 +1477,7 @@ if [[ "$POST_TO_PR" == "true" ]] && [[ -n "$REVIEW" ]]; then
else
echo "Not posted (exit 6): the PR's rounds changed while this review ran (a round was removed or is no longer one)." >&2
fi
echo " Run the review again to review on top of them. Session: ${SESSION_DIR}" >&2
echo " Run the review again to review on top of them." >&2
exit 6
fi

Expand Down Expand Up @@ -1529,7 +1550,6 @@ if [[ "$POST_TO_PR" == "true" ]] && [[ -n "$REVIEW" ]]; then
else
echo "Error: could not confirm whether the review was posted; run" \
"\`gh pr-review rounds https://${GH_HOST_NAME}/${OWNER}/${REPO}/pull/${PR_NUM}\` before retrying." >&2
echo " Session: ${SESSION_DIR}" >&2
exit 1
fi
fi
Expand All @@ -1538,4 +1558,16 @@ if [[ "$POST_TO_PR" == "true" ]] && [[ -n "$REVIEW" ]]; then
fi
printf '%s\n' "$COMMENT" >"${SESSION_DIR}/posted.md"
echo "✓ Posted → https://${GH_HOST_NAME}/${OWNER}/${REPO}/pull/${PR_NUM}"
POSTED=true
fi

# ── run folder ───────────────────────────────────────────────────────────────
# Its final name: round-<K> after a post, K the posted record's round; preview-<time> for --no-post. Any other run
# keeps run-<time>. run_exit prints the path it ends with.
cd "$PR_STATE_DIR"
if [[ "$POSTED" == true ]]; then
K=$(jq -er '.round | numbers' "${RECORD_FILE:-}" 2>/dev/null) || K=$ROUND
SESSION_DIR=$(run_dir_finish "$SESSION_DIR" "$K")
elif [[ "$POST_TO_PR" == false ]]; then
SESSION_DIR=$(run_dir_finish "$SESSION_DIR" preview)
fi
26 changes: 26 additions & 0 deletions lib/config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
# The optional config file. Functions only.

# Prints the config file's path: ${XDG_CONFIG_HOME:-$HOME/.config}/gh-pr-review/config.json.
config_file() {
echo "${XDG_CONFIG_HOME:-$HOME/.config}/gh-pr-review/config.json"
}

# Prints the value at <jq-path> in the config file (a string raw, anything else as compact JSON), or <default> when
# the file is missing or the value is missing, null or "". A file that is not one JSON object is an error that names it.
config_get() { # <jq-path> <default>
local file
file=$(config_file)
if [[ ! -e "$file" ]]; then
printf '%s\n' "$2"
return 0
fi
if ! jq -se 'length == 1 and (.[0] | type) == "object"' "$file" >/dev/null 2>&1; then
echo "Error: invalid config file (not one JSON object): $file" >&2
return 1
fi
jq -rc --arg d "$2" "($1) as \$v | if \$v == null or \$v == \"\" then \$d else \$v end" "$file" 2>/dev/null || {
echo "Error: could not read $1 from the config file: $file" >&2
return 1
}
}
Loading