feat: warn when a query borrows another git worktree's index#312
Open
shinju4n wants to merge 1 commit into
Open
feat: warn when a query borrows another git worktree's index#312shinju4n wants to merge 1 commit into
shinju4n wants to merge 1 commit into
Conversation
When a worktree is nested inside the main checkout (e.g. tools that place worktrees under gitignored paths like .claude/worktrees/<name>/), the nearest-.codegraph walk resolves UP to the main checkout's index. Queries then silently return the main branch's code instead of the worktree being edited — symbols changed only in the worktree are invisible. Add detectWorktreeIndexMismatch() (src/sync/worktree.ts) which compares the caller's git working-tree root against the resolved index root via 'git rev-parse --show-toplevel', and surface a non-fatal warning in both 'codegraph status' (CLI, incl. --json) and the codegraph_status MCP tool. Detection is best-effort: no git / not a repo / index in a plain ancestor all report no mismatch, so existing non-worktree and monorepo-subdir layouts are unaffected. Refs colbymchenry#155 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.
feat: warn when a query borrows another git worktree's index
Refs #155
Problem
CodeGraph resolves a project's index by walking up to the nearest
.codegraph/(
findNearestCodeGraphRoot). That walk is unaware of git worktrees.When a worktree is created inside the main checkout — which is exactly what
several agent tools do (e.g. placing worktrees under gitignored paths like
.claude/worktrees/<name>/) — a command run from the worktree walks up andsilently resolves the main checkout's index. Every query then returns
results from the main tree's code (usually a different branch) rather than the
worktree the user is actually editing. Symbols added or changed only in the
worktree are invisible, and nothing tells the user this is happening.
This is the silent footgun behind #155. (Note: a single shared index cannot
be correct for multiple worktrees at once — each can be on a different branch —
so the right primitive is to make the situation visible, then let users opt into
a worktree-local index.)
What this PR does
Adds detection + a non-fatal warning, scoped intentionally small:
src/sync/worktree.ts—detectWorktreeIndexMismatch(startPath, indexRoot)compares the caller's git working-tree root against the resolved index root via
git rev-parse --show-toplevel. Returnsnull(no warning) when: not a gitrepo / git unavailable, the index lives in the caller's own tree, or the index
root is a plain (non-worktree) ancestor directory. So non-worktree and
monorepo-subdir layouts are unaffected.
codegraph status(CLI) — prints the warning, and adds aworktreeMismatchfield to--jsonoutput.codegraph_status(MCP tool) — prepends the warning to the status text.Example (
statusrun inside a nested worktree):Tests
__tests__/worktree-detection.test.ts— drives realgit worktree add(no mocking, matching the existing
mcp-roots.test.tsstyle):gitWorktreeRootreports each tree distinctlytscbuild passes. Verified end-to-end with the bundled FTS5 Node runtime:the warning fires in a worktree and is absent in the main checkout.
Deliberately out of scope (happy to follow up per your preference)
(the natural next step once the detection primitive lands).
I kept the surface area minimal to make review easy — glad to extend in whatever
direction you'd prefer for #155.