Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
bf79c2f
Add a read-only git wrapper, the reviewer's git environment and a wor…
catalan-adobe Nov 28, 2026
79b0410
Run the reviewer with a read-only git and a worktree tripwire
catalan-adobe Nov 28, 2026
37661b4
Run the reviewer's git without optional locks
catalan-adobe Nov 28, 2026
7d4e732
Skip more global options in git-ro to find the subcommand
catalan-adobe Nov 28, 2026
72f928b
Show every untracked file and submodule change in the tripwire's status
catalan-adobe Nov 28, 2026
7b6863b
Stop with exit 4 when the tripwire can't read the review checkout
catalan-adobe Nov 28, 2026
5cb9d43
Keep the reviewer's git diff from refreshing the index
catalan-adobe Nov 28, 2026
d9e5016
Turn off core.fsmonitor for the reviewer's git
catalan-adobe Nov 28, 2026
064414a
Refuse to run the reviewer with a git that ignores GIT_CONFIG_COUNT
catalan-adobe Nov 28, 2026
59436c9
Say what the reviewer's sandbox disables, and that pi keeps its gh ac…
catalan-adobe Nov 28, 2026
716f4c8
Keep the tripwire's clone warning in the session
catalan-adobe Nov 28, 2026
55324a2
Isolate git config before the flow test's real-git fixture
catalan-adobe Nov 28, 2026
2c5a029
Refuse git describe --dirty and --broken in the reviewer's git: they …
catalan-adobe Nov 28, 2026
e0d727a
Drop the settings a parent git passes on in the reviewer's sandbox
catalan-adobe Nov 28, 2026
4d54f56
Refuse abbreviated git describe --dirty and --broken in the reviewer'…
catalan-adobe Nov 28, 2026
1b82798
Stop the run when the tripwire's snapshot can't be saved
catalan-adobe Nov 28, 2026
3d23e0f
Clear inherited git config before the tests' real-git fixtures
catalan-adobe Nov 28, 2026
5b66bbb
Probe for environment config without GIT_CONFIG or a parent git's set…
catalan-adobe Nov 28, 2026
b7f56a2
Name both causes of exit 4 in --help
catalan-adobe Nov 28, 2026
0bb8d07
Merge remote-tracking branch 'origin/main' into read-only-reviewer
catalan-adobe Nov 28, 2026
211ca3f
Unset GIT_TEMPLATE_DIR in the real-git test fixtures
catalan-adobe Nov 28, 2026
ee51859
Pass the snapshot failure cause to unreadable_checkout
catalan-adobe Nov 28, 2026
3b80f5a
Skip the pattern --match and --exclude take in git-ro's describe check
catalan-adobe Nov 28, 2026
29e8eae
Merge remote-tracking branch 'origin/main' into read-only-reviewer
catalan-adobe Nov 28, 2026
b0a2a54
Leave the run folder to the exit handler on exit 4, and find run fold…
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ jobs:
- name: Install shfmt
run: sudo apt-get update && sudo apt-get install -y shfmt
- name: shellcheck
run: shellcheck gh-pr-review lib/*.sh tests/*.sh
run: shellcheck gh-pr-review lib/*.sh lib/git-ro tests/*.sh
- name: shfmt
run: shfmt -d gh-pr-review lib/*.sh tests/*.sh
run: shfmt -d gh-pr-review lib/*.sh lib/git-ro tests/*.sh
- name: tests
run: for t in tests/*.sh; do bash "$t" || exit 1; done
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ gh pr-review https://github.com/owner/repo/pull/42
| What | Why | Get it |
| --- | --- | --- |
| `gh` (authenticated) | PR metadata, posting comments, this extension | `gh auth login` |
| `git`, `jq` | clone + JSON plumbing | `brew install jq` |
| `git` 2.31+, `jq` | clone + JSON plumbing (the reviewer's read-only git needs `GIT_CONFIG_COUNT`, git 2.31) | `brew install jq` |
| Node 22+ | runtime for pi | https://nodejs.org |
| `pi` | headless review agent | `npm install -g @earendil-works/pi-coding-agent` |
| GitHub Copilot subscription | model access | run `pi`, then `/login` → GitHub Copilot |
Expand Down Expand Up @@ -132,6 +132,8 @@ A run folder holds:
- `round.json`: the round record (see below); `posted.md`: the exact body posted
- `context/`: the PR description, the discussion and the inline review threads both passes read
- `repo/`: the checkout
- `tripwire/`: the snapshots of the checkout around each model call; `warnings.txt`, when there is one: a change in
the clone only (its refs, HEAD or stash) while the reviewer ran

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`.
Expand Down Expand Up @@ -335,6 +337,18 @@ record a preview, and exits with code 1. Another run's review on the same
commit, by the same account, does not count. Run
`gh pr-review rounds <PR>` before retrying, so the round is not posted twice.

The reviewer (pi) works read-only. Its `git` is a wrapper that runs only the
commands that read (`show`, `diff`, `log`, `range-diff`, `status`, …) and
refuses the rest. Git hooks, credential helpers and prompts are disabled. pi
keeps its authenticated `gh` access (with `--user`, that account's token), so
the wrapper is a guard rail against mistakes, not credential isolation. A
snapshot of the review checkout (its HEAD and `git status`) is taken before
and after each model call: if the reviewer changed it anyway, the run stops
before posting, says what changed, keeps the session folder, and exits with
code 4. A checkout the snapshot can't read (its HEAD or `git status` fails),
or a snapshot that can't be saved in the session folder, stops the run the
same way: a change would go unseen.

`gh pr-review <PR> --check` runs the check alone and exits 0 (run), 3 (stop)
or 5 (no new commits); `--check --json` prints the decision as JSON for
scripts. `gh pr-review rounds <PR>` shows the same decision under the history.
Expand All @@ -344,6 +358,7 @@ scripts. `gh pr-review rounds <PR>` shows the same decision under the history.
| 0 | Done |
| 1 | Error |
| 3 | Stopped by the loop check |
| 4 | The reviewer changed the review checkout (its HEAD or its files), or it could not be read or its snapshot saved, so a change could not be seen; nothing posted, the session is kept |
| 5 | No new commits since the last round |
| 6 | History changed while the review ran (someone posted another round, or a round went away); nothing posted, the session is kept |

Expand Down
52 changes: 47 additions & 5 deletions gh-pr-review
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
# --doctor Check all prerequisites and exit
# -h, --help Show this help
#
# Exit codes: 0 done, 1 error, 3 stopped by the loop check, 5 no new commits since the last round,
# 6 the rounds changed while the review ran (nothing posted, the session is kept).
# Exit codes: 0 done, 1 error, 3 stopped by the loop check, 4 the reviewer changed the review checkout, or it could
# not be read or its snapshot saved, so a change could not be seen (nothing posted, the session is kept), 5 no new
# commits since the last round, 6 the rounds changed while the review ran (nothing posted, the session is kept).
#
# Examples:
# gh pr-review https://github.com/owner/repo/pull/42
Expand Down Expand Up @@ -241,16 +242,47 @@ write_pr_context() {
}

# ── passes ──────────────────────────────────────────────────────────────────────────
# One headless pi call: prompt file in, model output file out. A failed call stops the run.
run_pi() {
if ! pi "${PI_ARGS[@]}" "$(cat "$1")" >"$2"; then
# One headless pi call: prompt file in, model output file out. A failed call stops the run. pi runs in the git
# sandbox (lib/guard.sh), between two snapshots of the review checkout: a reviewer that changed it stops the run with
# exit 4, before anything is posted. So does a checkout a snapshot can't read, or a snapshot that can't be saved: a
# change would go unseen. A change in the clone only is a warning, kept in the session's warnings.txt.
run_pi() { # <prompt-file> <output-file>
local snap code=0 changed=0
snap="${SESSION_DIR}/tripwire/$(basename "$2" .md)"
pi_bin "${SESSION_DIR}/bin"
tripwire_snapshot "$REVIEW_DIR" "$REVIEW_DIR" "$snap/before" || unreadable_checkout "$snap/before" $?
pi_env "${SESSION_DIR}/bin" pi ${PI_ARGS[@]+"${PI_ARGS[@]}"} "$(cat "$1")" >"$2" || code=$?
tripwire_snapshot "$REVIEW_DIR" "$REVIEW_DIR" "$snap/after" || unreadable_checkout "$snap/after" $?
tripwire_check "$snap/before" "$snap/after" "${SESSION_DIR}/warnings.txt" || changed=$?
if [[ $changed -ne 0 ]]; then
echo "" >&2
echo "Not posted (exit 4): the reviewer changed the review checkout, which must stay read-only." >&2
echo " The checkout is left as it is." >&2
exit 4
fi
if [[ $code -ne 0 ]]; then
echo "" >&2
echo "Error: pi exited with a non-zero status." >&2
echo " Check the session directory for details: ${SESSION_DIR}" >&2
exit 1
fi
}

# Stops the run with exit 4, so a change by the reviewer could not be seen. <cause> is tripwire_snapshot's exit code:
# 1, the review checkout could not be read (its snapshot says why); 2, the snapshot could not be saved.
unreadable_checkout() { # <snapshot-dir> <cause>
echo "" >&2
if [[ "$2" == 1 ]]; then
echo "Not posted (exit 4): the review checkout could not be read, so a change by the reviewer could not be seen:" >&2
sed 's/^/ /' "$1/worktree.txt" >&2 || true
else
echo "Not posted (exit 4): the snapshot of the review checkout could not be saved, so a change by the reviewer" \
"could not be seen." >&2
fi
echo " The checkout is left as it is." >&2
exit 4
}

# The run's extra guidance as a prompt section. Empty in, empty out.
instructions_section() {
[[ -z "$INSTRUCTIONS" ]] && return 0
Expand Down Expand Up @@ -321,6 +353,16 @@ run_preflight() {
"run: npm install -g @earendil-works/pi-coding-agent (requires Node 22+)"
fi

# The reviewer's read-only git turns credential helpers and hooks off through GIT_CONFIG_COUNT (lib/guard.sh).
if command -v git &>/dev/null; then
if git_env_config; then
pf_ok "git reads config from the environment (2.31 or later)"
else
pf_fail "this git ignores GIT_CONFIG_COUNT, so the reviewer's git can't be made read-only" \
"install git 2.31 or later"
fi
fi

if [[ "$MODEL" == github-copilot/* ]]; then
if command -v jq &>/dev/null && [[ -f "$PI_AUTH_FILE" ]] &&
jq -e 'has("github-copilot")' "$PI_AUTH_FILE" &>/dev/null; then
Expand Down
56 changes: 56 additions & 0 deletions lib/git-ro
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bash
# git for the reviewer: pi finds it as `git` first on its PATH (pi_env in lib/guard.sh). It runs the real git
# (GH_PR_REVIEW_REAL_GIT) for the commands that only read, and refuses the rest. A guard rail, not a wall: the
# tripwire around each pi call catches what gets past it.
set -euo pipefail

real=${GH_PR_REVIEW_REAL_GIT:-}
if [[ -z "$real" || ! -x "$real" ]]; then
echo "git-ro: GH_PR_REVIEW_REAL_GIT does not name a git to run" >&2
exit 1
fi

# The subcommand: the first argument after the global options. Any other option before it is refused.
i=1 sub=""
while [[ $i -le $# ]]; do
case "${!i}" in
-C | -c | --git-dir | --work-tree) i=$((i + 2)) ;;
--no-pager | -P | -p | --paginate | --no-optional-locks | --literal-pathspecs | --no-replace-objects | \
--git-dir=* | --work-tree=*)
i=$((i + 1))
;;
*)
sub=${!i}
break
;;
esac
done

# describe --dirty and --broken refresh the index, and no setting turns that off (pi_env does for status and diff).
# Git takes any unique prefix of an option name (--dir, --brok=x): refused too, and so is an ambiguous --d. The
# pattern after --match or --exclude (or their prefixes --m, --exc) is no option: `--match --br` only reads.
if [[ "$sub" == describe ]]; then
value=false
for arg in "${@:i+1}"; do
if [[ $value == true ]]; then
value=false
continue
fi
name=${arg%%=*}
if [[ $arg != *=* && ((${#name} -gt 2 && --match == "$name"*) || (${#name} -gt 4 && --exclude == "$name"*)) ]]; then
value=true
elif [[ ${#name} -gt 2 && (--dirty == "$name"* || --broken == "$name"*) ]]; then
echo "git describe --dirty and --broken are not allowed in a read-only review: they write the index" >&2
exit 1
fi
done
fi

case "$sub" in
show | diff | log | blame | grep | ls-files | ls-tree | cat-file | rev-parse | rev-list | merge-base | range-diff | \
for-each-ref | describe | shortlog | status | help | version | --version | --help)
exec "$real" "$@"
;;
esac
echo "git ${sub:-(no subcommand)} is not allowed in a read-only review" >&2
exit 1
99 changes: 99 additions & 0 deletions lib/guard.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env bash
# The reviewer's git sandbox: a read-only git on pi's PATH, no git credential helper or prompt, no hooks, and a
# tripwire on the review worktree around each pi call. A guard rail, not credential isolation: pi keeps its gh access.
# Functions only.

# Puts lib/git-ro in <dir> as `git`, for pi_env.
pi_bin() { # <dir>
mkdir -p "$1"
cp "$(dirname "${BASH_SOURCE[0]}")/git-ro" "$1/git"
chmod +x "$1/git"
}

# 0 when git reads config from the environment (GIT_CONFIG_COUNT: git 2.31 and later). pi_env turns credentials and
# hooks off that way; an older git ignores it. The probe runs without GIT_CONFIG (`git config` would read that file
# only) and without a parent git's -c settings (GIT_CONFIG_PARAMETERS, which win).
git_env_config() {
[[ $(env -u GIT_CONFIG -u GIT_CONFIG_PARAMETERS GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=gh-pr-review.probe \
GIT_CONFIG_VALUE_0=ok git config --get gh-pr-review.probe 2>/dev/null) == ok ]]
}

# Runs a command (pi) in the sandbox: the wrapper in <bin-dir> first on PATH, the real git (the git on PATH now) in
# GH_PR_REVIEW_REAL_GIT, no credential helper, no hooks (core.fsmonitor included), no password prompt, no ssh, no
# optional locks and no index refresh in `git diff`: `git status` and `git diff` do not rewrite the index. Drops the
# settings a parent git passes on (`git -c ...` running an alias), which would win over these. Refuses (1) with a git
# that ignores GIT_CONFIG_COUNT.
pi_env() { # <bin-dir> <command> [args...]
local bin=$1 real
shift
real=$(command -v git) || {
echo "Error: git not found" >&2
return 1
}
git_env_config || {
echo "Error: this git ignores GIT_CONFIG_COUNT, so the reviewer's git can't be made read-only:" \
"install git 2.31 or later" >&2
return 1
}
env -u GIT_CONFIG_PARAMETERS PATH="$bin:$PATH" GH_PR_REVIEW_REAL_GIT="$real" \
GIT_CONFIG_COUNT=4 GIT_CONFIG_KEY_0=credential.helper GIT_CONFIG_VALUE_0= \
GIT_CONFIG_KEY_1=core.hooksPath GIT_CONFIG_VALUE_1=/dev/null \
GIT_CONFIG_KEY_2=diff.autoRefreshIndex GIT_CONFIG_VALUE_2=false \
GIT_CONFIG_KEY_3=core.fsmonitor GIT_CONFIG_VALUE_3=false \
GIT_TERMINAL_PROMPT=0 GIT_ASKPASS=false SSH_ASKPASS=false GIT_SSH_COMMAND=false \
GIT_OPTIONAL_LOCKS=0 "$@"
}

# What the reviewer must not change. worktree.txt: the review worktree's HEAD (commit and branch) and status, with
# every untracked file and every submodule whatever the config says (status.showUntrackedFiles, diff.ignoreSubmodules,
# submodule.<name>.ignore). clone.txt: the clone's refs but refs/pr-review/, its HEAD and its stash. Errors go in the
# files. 1 when the worktree's HEAD or status can't be read (a detached HEAD is fine): two such snapshots would compare
# equal, so the worktree can't be checked. 2 when <out-dir> or a file in it can't be written, even in part: no
# snapshot. Each step is checked: a caller's `|| ...` turns errexit off in here.
tripwire_snapshot() { # <clone> <worktree> <out-dir>
local g=(--no-optional-locks -c core.hooksPath=/dev/null -c core.fsmonitor=false) ok=0 head branch status clone code=0
head=$(git -C "$2" "${g[@]}" rev-parse --verify HEAD 2>&1) || ok=1
branch=$(git -C "$2" "${g[@]}" symbolic-ref -q HEAD 2>&1) || code=$?
[[ $code -le 1 ]] || ok=1
status=$(git -C "$2" "${g[@]}" status --porcelain --untracked-files=all --ignore-submodules=none 2>&1) || {
status+="${status:+$'\n'}status failed: $?"
ok=1
}
clone=$(
{ git -C "$1" "${g[@]}" for-each-ref --format='%(objectname) %(refname)' 2>&1 || echo "for-each-ref failed"; } |
{ grep -v ' refs/pr-review/' || true; }
echo "HEAD $(git -C "$1" "${g[@]}" rev-parse HEAD 2>&1) $(git -C "$1" "${g[@]}" symbolic-ref -q HEAD 2>&1)"
git -C "$1" "${g[@]}" stash list 2>&1 || echo "stash list failed: $?"
)
if ! mkdir -p "$3" || ! printf 'HEAD %s\nbranch %s\n%s' "$head" "$branch" "${status:+$status$'\n'}" >"$3/worktree.txt" ||
! printf '%s\n' "$clone" >"$3/clone.txt"; then
echo "Error: could not save the tripwire's snapshot in $3" >&2
return 2
fi
return "$ok"
}

# 0 when nothing changed. 4 when the worktree changed: prints what did. A change in the clone only (a commit or a
# branch I make while pi runs) is no stop: 0, with a warning, also added to <warnings-file> when given.
tripwire_check() { # <before-dir> <after-dir> [warnings-file]
local clone=same
cmp -s "$1/clone.txt" "$2/clone.txt" || clone=changed
if ! cmp -s "$1/worktree.txt" "$2/worktree.txt"; then
echo "The reviewer changed the review worktree (its HEAD or its files):" >&2
{ diff -u "$1/worktree.txt" "$2/worktree.txt" || true; } | tail -n +3 >&2
if [[ $clone == changed ]]; then
echo "and the clone's refs, HEAD or stash:" >&2
{ diff -u "$1/clone.txt" "$2/clone.txt" || true; } | tail -n +3 >&2
fi
return 4
fi
if [[ $clone == changed ]]; then
{
echo "Warning: the clone's refs, HEAD or stash changed while the reviewer ran (the review worktree did not):"
{ diff -u "$1/clone.txt" "$2/clone.txt" || true; } | tail -n +3
} >"$2/warning.txt"
cat "$2/warning.txt" >&2
[[ -z "${3:-}" ]] || cat "$2/warning.txt" >>"$3"
fi
return 0
}
Loading