Skip to content

fix(graph): a query rooted elsewhere never re-roots the graph (#438) - #857

Draft
hr1juldey wants to merge 1 commit into
trailhq:mainfrom
hr1juldey:fix/438-ask-root-freshness-wipe
Draft

hr1juldey wants to merge 1 commit into
trailhq:mainfrom
hr1juldey:fix/438-ask-root-freshness-wipe

Conversation

@hr1juldey

Copy link
Copy Markdown

Fixes #438.

Root cause

The pre-query freshness check anchored on the query's root. With no positional, queryRoot falls back to cwd, the fingerprint probe compares cwd's file set against the graph, everything "changed", and the rebuild re-rooted the graph on an unrelated directory — 0 nodes written over a valid graph, cards intact, exit 0.

Fix

  • buildGraph records its absolute source root on the graph (meta.root).
  • ensureFreshGraph refuses to rebuild when the query root is not the recorded root: it answers from the graph as-is and prints graph was built for <root>; pass it as the argument, or use --no-refresh to answer as-is.
  • Same-directory is judged by realpath, so the hub layout from the issue (<entry> symlinks into sibling repos) passes no matter which path spelling arrives.
  • Two deliberate exemptions, both covered by tests: a freshly seeded worktree copy still repairs to its own checkout (that rebuild is seeding's point), and graphs without meta.root keep the old behavior byte-for-byte, so an upgrade changes nothing.

Tests (test/graph-refresh.test.ts)

Stranger-root query → refreshed:false, note names the recorded root, node set untouched · symlink alias of the true root → clean, no note · true root with a real edit → refreshes as before · old graph without the field → old (re-rooting) behavior asserted honestly as unchanged.

…q#438)

'graft ask "..."' without the repo-root positional anchored the
freshness check on cwd, so the pre-query rebuild re-rooted the graph on
an unrelated directory and wrote 0 nodes over a valid graph — behind
surviving per-file cards, exit 0, the worst failure mode there is.

- buildGraph now records its source root on the graph (meta.root).
- ensureFreshGraph refuses to rebuild when the query's root is not the
  recorded root: it answers from the graph as-is and says what root
  built it. Same directory is judged by realpath, so a symlinked entry
  (hub/entry -> ../repo) and the repo's own path are one root.
- The freshly seeded worktree copy is the deliberate exception — its
  repair rebuild to THIS checkout is the point of seeding — and graphs
  without a recorded root keep the old behavior exactly, so an upgrade
  changes nothing for them.
@github-actions

Copy link
Copy Markdown
Contributor

🌱 graft blast radius

1 area changed → 4 areas can be affected. 12 dependent symbols, depth 2.
Tests: 1 area updated its tests.
Tag: @anirudhkumar-nanonets — 4 of 5 areas · @shhdwi — 5 of 5 areas · @qoole — Graph Refresh CLI

flowchart TB
  A0(("Pull Request Review<br/>4 symbols"))
  A1(("Graph Refresh CLI<br/>3 symbols"))
  A2(("CLI Engine Integration<br/>3 symbols"))
  A3(("MCP Server Tools<br/>2 symbols"))
  classDef reached fill:#D9EDF3,stroke:#3AA7C9,stroke-width:1.5px,color:#0E313C;
  class A0,A1,A2,A3 reached;
Loading
Can be affected Symbols Nearest hop Reached from
Pull Request Review 4 src/app/review.ts:L45-L99 reviewPullRequest — calls, depth 1 Graph Building
Graph Refresh CLI 3 src/graph/refresh-run.ts:L18-L21 main — calls, depth 1 Graph Building
CLI Engine Integration 3 src/cli.ts:L280-L290 refreshBefore — calls, depth 1 Graph Building
MCP Server Tools 2 src/mcp/tools.ts:L233-L262 callTool — calls, depth 1 Graph Building
Who knows this code — 3 people across 5 areas
Area Who knows it
Graph Building · changed @anirudhkumar-nanonets — 25 commits, last 16d ago · @shhdwi — 8 commits, last 22d ago
Pull Request Review · affected @anirudhkumar-nanonets — 11 commits, last 1mo ago · @shhdwi — 2 commits, last 22d ago
Graph Refresh CLI · affected @shhdwi — 6 commits, last 22d ago · @qoole — 3 commits, last 9d ago
CLI Engine Integration · affected @anirudhkumar-nanonets — 77 commits, last yesterday · @shhdwi — 52 commits, last 22d ago
MCP Server Tools · affected @shhdwi — 21 commits, last 22d ago · @anirudhkumar-nanonets — 14 commits, last 22d ago

Ownership is git history over each area's own files, weighted towards recent work (120-day half-life). Merge commits and bots are dropped, and you are dropped from your own PR. A name with no @ has no GitHub handle in its commit email — tag them by hand, or add a .mailmap entry. A suggestion from history, not a CODEOWNERS rule.

All 12 dependent symbols, grouped by area

Pull Request Review — 4 symbols in 4 files

  • src/app/review.ts:L45-L99 — reviewPullRequest (calls, depth 1)
    55: await buildGraph(checkout.dir);
  • src/app/review-process.ts:L178-L182 — childReviewer (references, depth 2)
  • src/app/review-worker.ts:L67-L87 — run (calls, depth 2)
  • src/app/server.ts:L31-L37 — AppSeams (references, depth 2)

Graph Refresh CLI — 3 symbols in 2 files

  • src/graph/refresh-run.ts:L18-L21 — main (calls, depth 1)
    20: await buildGraph(dir, { contextDir, graphOnly: true, onlyDirs, extensions });
  • src/graph/refresh-run.ts:L1-L33 — refresh-run.ts (calls, depth 2)
    9: import { buildGraph } from "./build.js";
  • src/graph/workspace-cli.ts:L65-L108 — buildChild (calls, depth 2)

CLI Engine Integration — 3 symbols in 2 files

  • src/cli.ts:L280-L290 — refreshBefore (calls, depth 1)
    287: : await ensureFreshGraph(root, { contextDir: globalDir, disabled });
  • src/engine.ts:L104-L115 — graph (calls, depth 1)
    105: return buildGraph(dir, {
  • src/cli.ts:L1-L3499 — cli.ts (calls, depth 2)
    35: import { ensureFreshChildren, ensureFreshGraph, refreshNote } from "./graph/refresh.js";

MCP Server Tools — 2 symbols in 2 files

  • src/mcp/tools.ts:L233-L262 — callTool (calls, depth 1)
    252: : await ensureFreshGraph(root, { contextDir: dirOverride });
  • src/mcp/server.ts:L101-L256 — startMcpServer (calls, depth 2)
Test signal per changed area — 1 ✓

Reached = a node under a test path has a resolved edge into the changed symbol. It undercounts anything called indirectly — through a CLI, a spawned process or a dynamic import — so read a low ratio as “look here”, never as a coverage gate.

  • ✓ Graph Building — 2 of 3 reached · 1 test file changed here: test/graph-refresh.test.ts
    • not reached: sameDir
53 test suites also reference this code

65 symbols, kept out of the diagram and the table so they cannot crowd out the areas a reviewer has to look at.

  • test/ask-index.test.ts
  • test/ask.test.ts
  • test/claude-hooks.test.ts
  • test/claude-prompt-private.test.ts
  • test/container-extract.test.ts
  • test/context-only-dir.test.ts
  • test/context.test.ts
  • test/covers.test.ts
  • test/depth-parity.ts
  • test/generic-extract.test.ts
  • test/graph-bash.test.ts
  • test/graph-elixir.test.ts
  • test/graph-extensions.test.ts
  • test/graph-follow-submodules.test.ts
  • test/graph-go.test.ts
  • test/graph-imported-call-mcp.test.ts
  • test/graph-incremental.test.ts
  • test/graph-invariants.test.ts
  • test/graph-java.test.ts
  • test/graph-jsx.test.ts
  • …33 more

graft blast · origin/main...HEAD · depth 2 · 4 changed files

Open the interactive graph → — click an area to see its dependent symbols at file:line.

This branch has not been deployed

No deployments
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.

ask without the repo-root positional silently wipes the graph: freshness check anchors on cwd and rebuilds 0 nodes (cards survive)

1 participant