Repository navigation
Keep run files in one state folder per PR, with a lock - #13
Conversation
catalan-adobe
left a comment
There was a problem hiding this comment.
Approach
What it does: Moves review artifacts from the caller’s working directory into one state folder per PR. A local lock prevents overlapping runs; GitHub still determines which rounds were posted.
Fit with the repo: The state change fits Task 1 of docs/superpowers/plans/2026-11-26-review-tracking-pr3-local-worktrees.md. The concern is the new dependency table: 3c (shared worktrees) needs only 3a (state), not 3b (reviewer guards). The base design explains why those guards matter: a worktree shares “refs, objects, stash and config with my working clone.” This PR still uses a throwaway clone, but its plan permits shipping 3c without the guards.
Strongest case for the approach as built: lib/state.sh owns the folder and lock lifecycle without changing review or round-history logic. The PR says the lock “complements the exit-6 check,” and the code preserves that distinction. Stacked PR #16 already uses the state folder and pr.json.
Alternatives: Keep the split, but make enabling shared worktrees depend on #14. That costs merge ordering, not implementation rework. As written, either PR can land first, leaving a window where the reviewer runs against a user’s clone without the planned guards.
Questions for the author: Can the plan make #14 a prerequisite for enabling shared worktrees? With that gate explicit, I’d call this Sound.
Missing context: None.
Approach: Questionable
Strengths
gh-pr-review:1533–1559finalizes the directory only after the post succeeds or is independently confirmed. An ambiguous post keeps itsrun-…artifacts rather than presenting them as a posted round.lib/config.sh:18–25distinguishes an invalid config from a missing setting and preserves meaningful values such asfalseand0.tests/flow.sh:304–323checks the user-visible lock behavior: a refused run makes no model call or post, and--checkleaves the live lock alone.
Issues
🚨 Critical (Must Fix Before Merge)
(none found)
⚠️ Important (Should Fix)
-
[lib/state.sh:42] — Two processes can both take over the same stale lock. Both can read the dead PID before either removes the directory. After the first process creates its replacement lock and starts reviewing, the second can delete that live replacement based on its earlier observation. Atomic PID publication does not protect this check-and-delete sequence.
- Use a locking primitive that releases ownership on process death, or serialize stale-lock recovery and revalidate ownership under that serialization. Add a two-contender test that asserts exactly one process enters the review.
-
[lib/state.sh:97] — Replacing
mktemp -dwith plainmkdirloses the owner-only permissions on each run. With umask022and a traversable state root, another local user can read the private checkout, PR discussion, and unposted--instructions. The old run directory was0700regardless of that umask.- Create the tool-owned PR and run directories with owner-only permissions from the outset, including when
--checkis the first command to create state. Test under umask022.
- Create the tool-owned PR and run directories with owner-only permissions from the outset, including when
-
[lib/state.sh:7] — A relative
GH_PR_REVIEW_STATEchanges meaning during the run. WithGH_PR_REVIEW_STATE=./state, the lock and clone are created relative to the launch directory. Aftercd "$REVIEW_DIR"atgh-pr-review:487, context writes resolve inside the checkout, the latercd "$REVIEW_DIR"normally fails, and the exit trap looks in the wrong place for the lock.- Resolve the override against the launch directory once, before deriving any state paths, or reject a relative override before creating anything.
💡 Minor (Nice to Have)
- [gh-pr-review:1561] — A fetch or context failure after run creation but before the first
session:message at line 1119 leaves arun-…directory without reporting its path. Report the final session path on every exit after directory creation, preserving the exit status and lock cleanup. - [gh-pr-review:447] — Ctrl-D at the interactive loop-stop prompt makes the preceding
readfail underset -e, skipping bothloop_check_saveand exit 3. Handle EOF as the default “No,” as thenothing-newprompt already does at line 429. - [lib/state.sh:80] — The
pr.jsonhelpers accept two concatenated JSON objects:pr_json_getcan return two values for one key, andpr_json_setrewrites both objects without reporting corruption. Validate exactly one object before reading or updating, asconfig_getalready does. - [lib/state.sh:122] — Saving a diagnosis truncates the previous
loop-check.mdbeforeloop_textsucceeds. A failed or interrupted--checkcan therefore erase the last complete diagnosis. Write to a unique sibling temporary file and rename it only on success; this also keeps concurrent unlocked checks from mixing their output.
Recommendations
Prioritize a contention test with two actual claimants and an end-to-end test using a non-default state root. The existing happy-path tests pass without exercising either failure mode. I ran tests/state.sh and tests/post-decision.sh successfully; tests/flow.sh did not finish within the 240-second review timeout.
Assessment
Ready to merge? No
Reasoning: The normal post and preview lifecycle follows the plan, but stale recovery can admit two reviewers, and the directory change regresses artifact privacy. Those issues and the relative-path failure should be fixed before later PRs rely on this state layer.
Review generated by gh pr-review · model: azure-openai-direct/gpt-6.2-astra
|
Thanks for the review. All items are addressed; each fix has a test that failed before it (except the docs change, and two lock tests noted below). Approach question (make #14 a prerequisite for shared worktrees): yes. Important
Minor
All test files pass on macOS (bash 5) and ubuntu:24.04; |
catalan-adobe
left a comment
There was a problem hiding this comment.
Round 2 · since round 1
6 fixed · 1 still open · 1 new (1 in the new commits) · open blocking: 1 (was 3) · loop: early
Approach
What it does: Keeps review artifacts in one private folder per PR and locks that folder during a review. GitHub remains the source of truth for posted rounds.
Fit with the repo: lib/state.sh owns the folder and lock lifecycle, as Task 1 of docs/superpowers/plans/2026-11-26-review-tracking-pr3-local-worktrees.md specifies. The revised plan resolves the round 1 concern: shared worktrees cannot become the default until both review passes use read-only git and the tripwire. The author confirms the merge order: “this PR and #14, then #16.”
Strongest case for the approach as built: Users can find a PR’s runs regardless of where they launched the command, without changing how either pass reviews code. This PR still uses a throwaway clone.
Alternatives: Keep the split. Combining state, guards, and worktrees would make a larger change without improving safety over the explicit merge order. I’d revisit that choice if #16 enabled shared worktrees before the guards.
Questions for the author: None.
Missing context: None.
Approach: Sound
Strengths
lib/state.sh:46andlib/state.sh:153restore owner-only access to the PR and run directories. The flow tests cover both a review and a--checkthat creates the state directory first.gh-pr-review:479–488centralizes final-path reporting and lock release.tests/flow.sh:241–245checks the early discussion-fetch failure against the actual directory left on disk, the exit code, and the absence of the lock.
Issues
🚨 Critical (Must Fix Before Merge)
(none found)
⚠️ Important (Should Fix)
- F1 · [lib/state.sh:78] — A slow, live process can still lose its takeover lock. Suppose run A has rechecked the dead PID and is delayed before the removal at line 85. After five seconds, run B can remove A’s
lock.takeoverbased solely on its age, replace the stale primary lock, and start reviewing. When A resumes, line 85 removes B’s live lock, and A can enter too. Unlike the residual case described in the discussion, this requires neither contender to crash nor an abandoned takeover lock.- Don’t revoke takeover ownership based on elapsed time alone. Use a process-owned lock, or refuse the takeover with a recovery message when ownership cannot be established safely. Increasing the timeout only moves the race.
💡 Minor (Nice to Have)
- [lib/state.sh:181] — If
loop-check.mdalready exists as a directory,mv "$tmp" "$file"succeeds by putting the temporary file inside it. I verified that the save emits no warning and leavesloop-check.md/loop-check.md.<suffix>instead of a readable diagnosis. The previous direct write warned in this case. Reject a directory destination and use the existing warning and cleanup path.
Fixed since the last round (6)
- F2 — PR and run directories are now
0700, including when--checkcreates state first. - F3 — A relative
GH_PR_REVIEW_STATEis pinned to the launch directory before any checkout or state path is derived. - F4 — The
EXIThandler reports the retained run directory even when the discussion fetch fails before the model runs. - F5 — Ctrl-D at the loop-stop prompt now means “No,” saves the diagnosis, and exits 3.
- F6 — Both
pr.jsonhelpers reject concatenated objects, and the setter leaves the invalid file unchanged. - F7 — A failed diagnosis render no longer truncates the previous
loop-check.md; replacement happens only after rendering succeeds.
Recommendations
Extend the contention test with an explicit synchronization point after stale-PID revalidation. Hold the first claimant there past the takeover timeout and assert that a second claimant cannot enter while the first still owns recovery. tests/state.sh, tests/post-decision.sh, and the selected state and loop-stop cases from tests/flow.sh passed on this checkout.
Assessment
Ready to merge? No
Reasoning: Six of the seven earlier findings are fixed. The remaining race still breaks the PR’s central guarantee: two live runs on this machine can both acquire the same PR’s lock.
Review generated by gh pr-review · model: azure-openai-direct/gpt-6.2-astra
|
Thanks for round 2, and for moving the approach to Sound. You were right that the remaining window was wider than I described: a slow but live run could lose its takeover lock to elapsed time. Both items are fixed, each with tests that failed on the code before. This branch also merges Important
Minor
All test files pass on macOS (bash 5) and ubuntu:24.04, and |
catalan-adobe
left a comment
There was a problem hiding this comment.
Round 3 · since round 2
2 fixed · 3 new (1 in the new commits) · open blocking: 1 (was 1) · loop: converging · full review (the base branch was merged in with conflicts)
Approach
Unchanged since round 2: The lock-recovery and diagnosis fixes stay in lib/state.sh, and shared worktrees still depend on both review passes having read-only Git and the tripwire.
Approach: Sound
Strengths
lib/state.sh:96–130no longer treats elapsed time as proof that a takeover owner died.tests/state.sh:222–253pauses the first claimant after PID revalidation and checks that a second claimant cannot enter—the interleaving that mattered for F1.lib/state.sh:222–229preserves the previous diagnosis if rendering or replacement fails.tests/state.sh:338–345also checks that a directory namedloop-check.mdis left empty, not merely that the command warns.tests/flow.sh:256–260checks the final session path, exit status, and lock cleanup together on an early failure, before either model pass runs.
Issues
🚨 Critical (Must Fix Before Merge)
(none found)
⚠️ Important (Should Fix)
- [lib/state.sh:63] — The printed lock-recovery command does not shell-quote the path. With a state root such as
$HOME/Projects Archive/reviews, pasting the suggested command passes$HOME/Projectsas a separate operand torm -rf. It can delete an unrelated directory while leaving the lock intact.- Print the path as one shell-escaped argument, for example with
printf 'rm -rf -- %q\n' "$1". Test the resulting argument list with a path containing spaces, without runningrm.
- Print the path as one shell-escaped argument, for example with
💡 Minor (Nice to Have)
- [lib/state.sh:13] — A relative
XDG_STATE_HOMEstill changes meaning aftercd. This is distinct from the fixedGH_PR_REVIEW_STATEoverride: with that override unset andXDG_STATE_HOME=./state, the stubbed flow wrotecontext/pr-body.mdinside the checkout, exited 1 at the secondcd, and left the original lock behind.- Ignore a non-absolute
XDG_STATE_HOMEand use the default, as the XDG specification prescribes, or reject it before creating state.
- Ignore a non-absolute
- [gh-pr-review:1530] — The oversized-review warning points to a
round.jsonpath that disappears after a successful post. In the no-room-for-marker flow, stderr reportedrun-…/round.json, but the file ended up atround-1/round.json. This is the case where the user needs the local record because GitHub does not have it.- Refer to
round.jsonin the finalSession:directory, or defer printing the absolute record path until after the rename.
- Refer to
Fixed since the last round (2)
- F1 — A live takeover owner is no longer evicted by age; an unverifiable takeover is refused rather than removed.
- F8 — A directory at
loop-check.mdnow triggers a warning and temporary-file cleanup without writing inside it.
Recommendations
Keep the contention tests. For the remaining fixes, test the paths users actually consume: the recovery instruction must identify exactly one operand, and a record path advertised for later inspection must still exist after finalization. tests/state.sh, tests/post-decision.sh, and the targeted stubbed flows described above were run on this checkout.
Assessment
Ready to merge? Yes with fixes
Reasoning: Both findings from round 2 are fixed. The remaining merge concern is the copyable recovery command: a valid state path containing a space can turn removal of one lock into deletion of unrelated files.
Review generated by gh pr-review · model: azure-openai-direct/gpt-6.2-astra
|
Thanks for round 3. All three items are fixed, each with a test that failed before its fix.
All test files pass on macOS (bash 5) and ubuntu:24.04; |
Conflicts: - README.md: #13's lock description and #14's tripwire/ entry, with this branch's review-worktree paragraph, pr.json facts, repo/ (--fresh only) and warnings.txt (a reset dirty worktree, or a change in the clone only). - tests/flow.sh: the stub pi keeps both pi.pwd and #14's pi.evil; run, run_no_y and run_tty run $ext_bash (#15) with --fresh; run_local runs $ext_bash too; the local-clone cases come before #14's real-git --fresh cases.
PR 3a of review tracking: one state folder per PR, with a lock. Task 1 of
docs/superpowers/plans/2026-11-26-review-tracking-pr3-local-worktrees.md. This PR also adds the plan's "Changes after PRs #8, #9 and #10" section, which splits PR 3 into four smaller PRs: 3a, 3b (#14, a read-only reviewer), 3c (local clones and review worktrees) and 3d (gc, docs, a real run).What changes
Run files live in one state folder per PR:
${GH_PR_REVIEW_STATE:-${XDG_STATE_HOME:-~/.local/state}/gh-pr-review}/<host>/<owner>/<repo>/<N>/. Before, they went intopr-review-sessions/under whatever folder the command ran from. Old folders are left alone.Run folder names:
run-<UTC time>while the run works.round-<K>after a post, where K is the.roundof the posted record (the number left after the final re-read).preview-<time>for a completed--no-postrun.run-…. Ifround-<K>already exists, it keepsrun-…with a warning.A per-PR lock (
lock/with apidfile) once the loop check decides to run. A second run of the same PR is refused while that PID lives; a dead PID's lock is taken over with a note.--checkandroundsdon't lock. The lock covers this machine; Record each review round, and stop review loops that don't converge #8's exit-6 check still covers other machines and accounts.loop-check.mdin the PR's state folder whenever the loop check stops a run or--checkreports a stop.lib/config.shreads~/.config/gh-pr-review/config.json(config_get).lib/state.shaddspr.jsonget/set. Nothing uses either yet; 3c will.Path safety:
pr_state_dirrefuses empty,.,..or/-containing parts, so a crafted URL can't put the folder outside the state root.Also, before review: host, owner and repo are lowercased in the state path, so
Owner/Repoandowner/reposhare one folder and lock. The lock's pid is written atomically, and a lock folder without a pid yet gets up to 2 s before it counts as stale. The config file must be a JSON object, and an empty-string value gives the default.Tests
tests/state.sh(43 checks): config, paths, the lock (taken, refused, taken over, released on a failing exit),pr.json, run folder names,loop-check.md.tests/flow.shnow uses a state folder under the stub home. New checks:round-1/,preview-…/, exit 6 keepingrun-…, a live lock refusing a second run, a stale lock taken over,--checktaking no lock, andloop-check.mdon a stop.All tests pass on macOS (bash 5 and 3.2) and Ubuntu 24.04, and shellcheck and shfmt are clean. Diff: 7 files, +368 −18, plus the plan section (+39).