fix: resolve project root from caller cwd git toplevel, not script anchor (#328)#329
Conversation
…chor (#328) map_orchestrator.py's main() previously anchored cwd exclusively to Path(__file__).resolve().parents[2], which silently operates on the main clone when invoked via absolute path from a git worktree that lacks its own .map/scripts/ copy. New resolution priority (highest first): 1. CLAUDE_PROJECT_DIR env var (explicit operator intent) 2. git rev-parse --show-toplevel from the caller's cwd (handles git worktrees: the worktree root is correct, not the main clone) 3. Path(__file__).resolve().parents[2] — script-anchored fallback (legacy behaviour when caller's cwd is not a git repo) Emits an INFO message to stderr when the resolved root differs from the script anchor so cross-checkout usage is auditable but not fatal. Tests added (TestCwdIndependence): - test_claude_project_dir_takes_priority_over_script_anchor - test_git_toplevel_from_caller_cwd_takes_priority_over_script_anchor Existing three tests still pass (non-git caller cwd → falls back to script-anchored root, preserving the prior-fix behaviour). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015J78vXZPNJcBCwUKYHVEWi
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughProject root resolution in map_orchestrator.py's main() was reworked across three parallel copies (deployed script, templates package, jinja template). Instead of always chdir'ing to the script-anchored root, root is now resolved via CLAUDE_PROJECT_DIR, then git rev-parse --show-toplevel from the caller's cwd, then script fallback, with logging on mismatch. Two subprocess integration tests were added. ChangesProject root resolution fix
Estimated code review effort: 3 (Moderate) | ~25 minutes Possibly related issues
Poem
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
Fixes #328.
map_orchestrator.py'smain()previously anchoredcwdexclusively toPath(__file__).resolve().parents[2]. When invoked via an absolute path from a git worktree that lacks its own.map/scripts/copy, this silently changed directory to the main clone's root — causingget_branch_name()to return the wrong branch and all.map/<branch>/reads/writes to operate on an unrelated workflow.Root cause
Fix
New resolution priority (highest first):
CLAUDE_PROJECT_DIRenv var (explicit operator intent)git -C <caller_cwd> rev-parse --show-toplevel— uses the caller's actual cwd so a worktree invocation anchors to the worktree root, not the main clonePath(__file__).resolve().parents[2]— script-anchored fallback when caller's cwd is not a git repo (preserves the old non-worktree behaviour)Emits an
INFOmessage to stderr when the resolved root differs from the script anchor so cross-checkout usage is auditable but non-fatal.Tests
Two new regression tests added to
TestCwdIndependence:test_claude_project_dir_takes_priority_over_script_anchor—CLAUDE_PROJECT_DIRwins over the script-anchored roottest_git_toplevel_from_caller_cwd_takes_priority_over_script_anchor— git toplevel from the caller's cwd wins whenCLAUDE_PROJECT_DIRis unset (the worktree scenario from the issue)All 3 existing
TestCwdIndependencetests continue to pass: they use a non-gitproject_b, so git resolution fails and the code correctly falls back to the script-anchored root.Generated by Claude Code
Summary by CodeRabbit
Bug Fixes
Tests