fix: route crit comment --json bulk to alt review file by reply ID#424
Merged
tomasz-tomczyk merged 1 commit intomainfrom May 2, 2026
Merged
fix: route crit comment --json bulk to alt review file by reply ID#424tomasz-tomczyk merged 1 commit intomainfrom
tomasz-tomczyk merged 1 commit intomainfrom
Conversation
When crit comment --json runs from a cwd whose resolved review file
doesn't contain the comment IDs being replied to, the bulk write
silently lands in the wrong review (or fails). The singular
crit comment --reply-to path already scans ~/.crit/reviews/ for the
ID via findReviewFileByCommentID; this extends the same fallback to
the bulk path.
A bulk call now writes to a single target file, chosen by:
- no reply_to → cwd-resolved primary (unchanged)
- all reply IDs in primary → primary (unchanged)
- all reply IDs in one alt review file → entire bulk redirects
there (new comments ride along), with a one-line stderr note
- reply IDs split across files → rejected with "bulk targets
multiple review files" so the caller can split the call
Also makes findReviewFileByCommentID return the canonical
"not found in any review file" error when ~/.crit/reviews/ doesn't
exist, instead of a raw os.ReadDir error.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #424 +/- ##
==========================================
+ Coverage 67.27% 67.43% +0.16%
==========================================
Files 26 26
Lines 9689 9747 +58
==========================================
+ Hits 6518 6573 +55
- Misses 2658 2660 +2
- Partials 513 514 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
5 tasks
tomasz-tomczyk
added a commit
that referenced
this pull request
May 2, 2026
* refactor: split github.go into github / comment_cli / review_file github.go had grown to ~1774 lines mixing three unrelated concerns: gh-CLI / PR push-pull, the comment-authoring CLI, and review-file I/O. PR #424's bulk-routing fallback made the entanglement obvious. Move whole functions, no behavior changes, no signature changes: - review_file.go: resolveReviewPath, resolveReviewPathFromDaemon, pickReviewPath, resolveReviewPathFromSessions, writeCritJSON, loadCritJSON, saveCritJSON, clearCritJSON, findReviewFileByCommentID, reviewFileContainsComment, cjContainsCommentID - comment_cli.go: appendComment[Scoped], readAnchorFromDisk, appendReply, addCommentToCritJSON[Scoped], addReplyToCritJSON, BulkCommentEntry (incl. UnmarshalJSON), processBulkEntry and helpers, parseLineSpec, bulkAddCommentsToCritJSON[Scoped], resolveBulkTarget, addReviewCommentToCritJSON[Scoped], addFileCommentToCritJSON[Scoped], appendReviewComment[Scoped], appendFileComment[Scoped] - github.go: keeps only the gh-CLI / PR sync surface (detectPR, fetchPRComments, mergeGHComments*, createGHReview, push/pull plumbing, truncateStr). Tests stay in github_test.go for this commit — they all live in package main and continue to compile and pass against the moved functions. Splitting the 2700-line test file along the same lines is mechanical follow-up work and not required for the refactor to be useful. go build ./... and go test ./... both pass. * fix: route crit pull --pr to review file matching the PR's branch When a user runs `crit pull <pr>` from a worktree whose cwd-resolved review file is for a different branch than the named PR, comments were silently merged into the wrong review file — same class of cwd-vs-intent mismatch that PR #424 fixed for `crit comment`. The trigger is narrow: only kicks in when the user passed an explicit PR number, didn't override --output, and the existing review file's "branch" field disagrees with the PR's headRefName. Under those conditions we scan ~/.crit/reviews/ for exactly one review file matching the PR's branch and route to it, with a stderr note. If zero or multiple match, we fall back to the cwd-resolved path (today's behavior). Adds findReviewFileByBranch helper to review_file.go alongside findReviewFileByCommentID and a redirectReviewPathForPR shim in main.go that wires the fetch+match together. * fix: route crit push --pr to review file matching the PR's branch Mirrors the previous commit for `crit pull`. When a user runs `crit push <pr>` from a worktree whose cwd-resolved review file is for a different branch than the named PR, we'd silently post the wrong comments to that PR — strictly worse than the pull case because pushing creates side effects on GitHub that are visible to reviewers. Trigger conditions are identical to the pull fix: explicit PR number, no --output override, existing review file's branch disagrees with the PR's headRefName, and exactly one alt review file matches the PR's branch. Reuses redirectReviewPathForPR. * fix: log Note when multiple review files match PR branch in redirect When a multi-worktree user runs crit pull/push with --pr and several review files share the PR's head branch, findReviewFileByBranch returned a generic error and redirectReviewPathForPR swallowed it — leaving the user with the cwd file and no signal of why. Distinguish "no match" (silent fallback) from "ambiguous" (stderr Note) via sentinel errors. The Note tells the user to pass --output to disambiguate. * fix: reroute crit pull/push --pr when cwd review file missing Previously the gate in runPull/runPush required cj.Branch != "" before attempting to redirect to a branch-matching review file. When the cwd review file didn't exist, cj.Branch was empty and the redirect never fired — the user got a fresh wrong-location file silently. For runPull: drop the cj.Branch != "" gate; pass empty cwdBranch through to redirectReviewPathForPR, which now skips its cwd-vs-PR-branch early-out when cwdBranch is empty (no false-positive risk without cwd state). For runPush: tolerate ENOENT on the initial review file read so an explicit --pr from a clean checkout can find the right file by branch via the redirect. Only fail with "no review file found" if the redirect also misses. * test: cover findReviewFileByBranch + redirectReviewPathForPR Table-driven tests for both helpers using the existing fetchPRByNumberFn test seam (via withFetchPRByNumber) and a temp HOME for isolated ~/.crit/reviews/ fixtures. Covers all redirect branches: cwd-matches-PR, unique-alt, no-alt, ambiguous-alt (asserts the new stderr Note), fetch error, empty HeadRefName, and empty cwdBranch (Fix 1: redirect must fire even when the cwd review file is missing). Also covers the four findReviewFileByBranch outcomes (single, none, ambiguous, exclude). * docs: address second-pass review notes - review_file.go: document gh-CLI repo-scoping assumption that keeps cross-repo branch-name collisions from being a real-world hazard. - main.go: extend runPull comment to note that a corrupt cwd file also produces empty cj.Branch and triggers branch-based redirect. - .golangci.yml: update stale gocyclo exclusion to reference the *Scoped function name (post-#428 wrapper deletion). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When
crit comment --jsonruns from a cwd whose resolved review file doesn't contain the comment IDs being replied to, the bulk write silently lands in the wrong review file (or no-ops on missing IDs). The singularcrit comment --reply-topath already scans~/.crit/reviews/for the ID viafindReviewFileByCommentID; this extends the same defensive fallback to--json.A bulk call now writes to a single target file, chosen by:
reply_toentries → cwd-resolved primary (unchanged)~/.crit/reviews/→ entire bulk redirects there (new comments ride along), with a one-line stderr notebulk targets multiple review files: ...so the caller can split the callAlso makes
findReviewFileByCommentIDreturn the canonicalnot found in any review fileerror when~/.crit/reviews/doesn't exist, instead of a rawos.ReadDirerror.Test plan
go test ./...(full suite) — passgofmt -l .— cleangolangci-lint run ./...— 0 issues~/.crit/reviews/directory🤖 Generated with Claude Code