Skip to content

fix(cli): resolve the graph from the primary checkout in linked worktrees (#2008)#2064

Open
HerenderKumar wants to merge 1 commit into
Graphify-Labs:v8from
HerenderKumar:worktree-graph-fallback
Open

fix(cli): resolve the graph from the primary checkout in linked worktrees (#2008)#2064
HerenderKumar wants to merge 1 commit into
Graphify-Labs:v8from
HerenderKumar:worktree-graph-fallback

Conversation

@HerenderKumar

Copy link
Copy Markdown
Contributor

Summary

Fixes #2008. The read commands (query / explain / path / affected) resolve their default graph to <GRAPHIFY_OUT>/graph.json relative to CWD. Run from a linked git worktree — increasingly the default for AI-agent task isolation (Claude Code etc.) — that path does not exist, so the command fails hard with Graph file not found and callers have to wrap every invocation in a git rev-parse --git-common-dir shim.

Fix

cli.py::_default_graph_path() — used only by the read commands; writers resolve through graphify.paths — gains a worktree fallback: when the local graph is absent and CWD is inside a linked worktree (--git-dir != --git-common-dir, the same absolute compare the commit/post-checkout hooks already use for #1809), resolve to the primary checkout's graph.

Read-only and surgical by construction:

  • writers (extract / build) never touch this helper — a write is never redirected;
  • an explicit --graph still overrides;
  • an absolute GRAPHIFY_OUT short-circuits (already shared across worktrees);
  • an absent primary graph keeps the original "run extract" error;
  • the two git rev-parse probes run only on the cold path (no local graph), so the common case is untouched.

Testing

New tests/test_worktree_graph_path.py (real git worktree, no grammar deps):

  • resolves the primary checkout's graph from a linked worktree (red→green verified against origin/v8);
  • a worktree that built its own graph keeps using it (local wins);
  • the primary checkout is unaffected (local default preserved);
  • outside a git repo the resolver never raises.

tests/test_explain_cli.py + tests/test_affected_cli.py pass unchanged.

…rees (Graphify-Labs#2008)

Read commands (query/explain/path/affected) resolve their default graph to
<GRAPHIFY_OUT>/graph.json relative to CWD. Run from a linked git worktree —
increasingly the default for AI-agent task isolation — that path does not
exist, so the command failed hard with "graph file not found" and callers had
to wrap every invocation in a `git rev-parse --git-common-dir` shim.

When the local graph is absent and CWD is inside a linked worktree (git-dir !=
git-common-dir, the same absolute compare the commit/checkout hooks already
use), fall back to the primary checkout's graph. Read-only by construction:
writers resolve through graphify.paths, an explicit --graph still wins, an
absolute GRAPHIFY_OUT short-circuits, and an absent primary graph keeps the
original "run extract" error. The git probe runs only on the cold path (no
local graph), leaving the common case untouched.
Comment thread graphify/cli.py
return str(local)


def _worktree_primary_graph() -> str | None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice helper function. Would it be worth adding a short docstring explaining why graph.json is the default output path?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks !!! Actually both new functions already carry docstrings, If I mention specifically _default_graph_path (lines 85–93) and _worktree_primary_graph (lines 104–113). _default_graph_path explains why <GRAPHIFY_OUT>/graph.json is the default and how the worktree fallback kicks in, and _worktree_primary_graph documents the git-dir vs. git-common-dir check and every case where it returns None. Btw I am happy to extend further if any part seems less explanatory.

are run from a linked git worktree that has no ``graphify-out/`` of its own.

Agent workflows (Claude Code etc.) increasingly run every task in its own
worktree, where ``graphify explain``/``query``/``path``/``affected`` used to fail

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Path here improves readability. Is there any scenario where _GRAPHIFY_OUT could be missing or invalid? If so, should that be handled explicitly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks !!! Agreed on Path. On _GRAPHIFY_OUT — it isn't a value that gets passed in while the program runs. It's a fixed setting loaded once at the top of the file (line 14), so it's always a valid string (defaults to graphify-out). If it were ever wrong, the program wouldn't even start — it'd fail right at import, not inside this function. The only tricky case is when it's set to an absolute path, and that's already handled at line 114 (the os.path.isabs check). So there's nothing extra to handle here. That said, I'm happy to add a check if you'd still like one just to be safe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auto-resolve graph location from git worktrees (git-common-dir)

2 participants