graphify icon indicating copy to clipboard operation
graphify copied to clipboard

explain silently caps connections at 20 and never points to affected, which returns the complete set

Open jandudekcz opened this issue 3 months ago • 5 comments

Version: graphifyy 0.9.32 (pip), Windows 11, Python 3.12.9 Graph: 8,988 nodes / 21,571 edges, code-only corpus (JS/JSX + SQL), built with graphify update .

Summary

explain prints at most 20 connections. The cap is hardcoded and no flag lifts it. That alone would be fine — the header does say Connections (159) and a ... and 139 more line is printed — but in practice agents read the visible list as the answer and conclude "nothing else imports this". On a real service that is a 20-of-159 view.

The fix that already exists in the tool is affected, which has no cap and returns the complete set. Nothing in --help, the README, or the explain output points from one to the other, so it is only found by reading cli.py.

Reproduction

$ graphify explain "payroll.service.js" | grep -E "^Connections|and .* more"
Connections (159):
  ... and 139 more

$ graphify explain "payroll.service.js" | grep -cE "^  (-->|<--)"
20

$ graphify explain "payroll.service.js" --top-k-edges 200 | grep -cE "^  (-->|<--)"
20

--top-k-edges is accepted without error but changes nothing — it is parsed for serve/inspector (cli.py:1995), not for explain.

The Grouped by file section has the same cap:

$ graphify explain "payroll.service.js" | grep -A25 "Grouped by file" | grep -c connection
20

Source, cli.py:1394:

if connections:
    print(f"\nConnections ({len(connections)}):")
    connections.sort(key=lambda c: G.degree(c[1]), reverse=True)
    for direction, nb, edata in connections[:20]:

Note the sort key: G.degree(neighbor) — incoming and outgoing edges are pooled into one list and then cut. Because a busy service imports far more than imports it, the outgoing side dominates the surviving 20 and the incoming side is what gets dropped.

Concretely, payroll.service.js has 5 production consumers. Exactly one of them (payroll-closure.service.js) survives the cap; payroll.controller.js, payslip.service.js, profitability.service.js and mobile.service.js are all cut, while outgoing imports like settings.service.js and leave-balances.service.js are kept. For a "who depends on this" question that is the wrong half of the list.

affected returns the complete set

$ graphify affected "payroll.service.js" --relation imports_from --depth 1 | grep -c "^- "
21

$ grep -rlE "from ['\"].*payroll\.service" apps/api --include=*.js | wc -l
21

Exact match with grep — paths and line numbers included, no truncation.

Why this bites agents specifically

The Claude Code hook installed by graphify claude install pushes query as the mandatory first step, and the read-guard text tells the model to propagate the rule to subagents. So the agent path is: hook fires → query or explain → agent reads 20 lines → answers. Nothing in that chain surfaces affected.

I hit this with a real subagent on a 56-consumer service. It ran explain, saw 20 entries, and under-reported dependants by roughly half. Only after being told about affected did it get the complete list. Same task, same graph — the difference was entirely which command it knew about.

Suggestions (independent, any subset helps)

  1. Point at affected from the truncation line. Cheapest fix, no behaviour change: ... and 139 more — run 'graphify affected "payroll.service.js" --relation imports_from --depth 1' for the full list
  2. Honour a limit flag on explain (--limit N / --all). --top-k-edges is currently accepted-but-ignored here, which reads as a bug on its own; either wire it up or reject it for this subcommand.
  3. Cap each direction separately. Pooling in- and out-edges before [:20] lets the outgoing side crowd out the incoming one, which is the half that answers "who uses this". Something like 10 in / 10 out — or truncating per direction — would keep the visible slice representative.
  4. Mention affected in the agent-facing guidance (hook-guard text / skill SKILL.md), since that is where the command choice actually gets made.

Happy to send a PR for (1) if the wording is agreed — it is a one-line change and would have saved this entire investigation.

Related

  • #2384 — query ranking; same underlying theme of agents getting a confidently-wrong subset
  • #2145 — hook-guard doesn't cover subagent spawns; that is where this misread happened for me

jandudekcz avatar Aug 03 '26 10:08 jandudekcz

Opened a PR for suggestion (1): https://github.com/Graphify-Labs/graphify/pull/2423

Extends the explain truncation line to point at graphify affected "…" --depth 1. Cap and sort order are unchanged; happy to follow up on --limit / per-direction caps if useful.

nothariharan avatar Aug 03 '26 13:08 nothariharan

Thanks for the fast turnaround — (1) is exactly the shape I had in mind, and adding test_explain_truncation_points_to_affected is the part that makes it stick.

One correction to my own report, since it affects how you weigh (3).

I claimed the pooled sort systematically favours out-edges and buries the incoming side. Re-measuring on the current graph (payroll.service.js, 182 connections after I added dynamic vi.mock/await import edges locally):

visible 20:  16 out / 4 in   → 20% incoming
full graph: 138 out / 44 in  → 24% incoming

So the visible slice is roughly proportional to the underlying mix — the cap shrinks both sides rather than skewing one. My earlier "4 of 5 production consumers are cut" observation was true as a count, but it follows from 20/182 being a small window, not from a direction bias. Per-direction caps would therefore buy less than I implied; the honest framing is that any 20-of-182 window under-represents whichever side you happen to care about. Treat (3) as low priority unless someone has a case where the skew is real.

That makes (2) --limit/--all the more useful follow-up in my view, and it also resolves the smaller oddity that --top-k-edges is currently accepted on explain without doing anything (it parses for serve). Either wiring it up or rejecting it there would remove a false affordance — I lost some time assuming it applied.

On (4): worth noting the installed hook-guard text still says "MANDATORY: you MUST run graphify query". On my repo (8.9k nodes, code-only) query is the weakest of the three commands — related to #2384 — so that guidance steers agents toward the command least likely to answer them, and away from explain/affected. Updating that string is probably higher leverage for agent behaviour than anything left in (2)/(3).

For what it's worth, the ordering that ended up working here after measuring: affected for "who uses this", explain for orientation, plain grep for locating a symbol, and query not at all.

jandudekcz avatar Aug 03 '26 14:08 jandudekcz

Thanks for the remeasure — that correction is useful.

agree on (3): if the visible 20 is roughly the same in/out mix as the full set, then per-direction caps are solving a skew that isnt really there. the real issue is just "any 20-of-N window is tiny", so treating (3) as low priority makes sense.

for follow-ups i would keep this PR scoped to (1) only. next useful slice is (2): a real --limit / --all on explain, and either wiring or rejecting --top-k-edges there so it stops looking like a working flag.

(4) is a separate change in my view — updating the hook-guard "MUST run graphify query" text is probably higher leverage for agent behaviour than the remaining explain UX, especially on large code-only graphs where affected/explain are the better tools. happy to take that as a follow-up and keep it away from this PR so (1) stays easy to land.

your ordering after measuring also matches what this PR is trying to surface: affected for "who uses this", explain for orientation.

nothariharan avatar Aug 03 '26 14:08 nothariharan

Another data point, posted by Devin (Cognition's coding agent) on behalf of the electrumnz/KeeceTakeOffApp maintainer.

graphifyy 0.9.64. On a degree-35 function node, explain printed 20 connections and collapsed the rest into ... and 15 more plus a per-file tally. The rationale_for edge — the docstring explaining the function's fail direction, arguably the single most useful edge on the node — was in the truncated tail and surfaced only as <-- keece-backend/app/tasks/inbound_sweeper.py: 1 connection. An agent reading that output concluded the rationale wasn't in the graph, which was wrong and cost a detour into grep.

Two small things would have avoided it: a --limit N|all flag, and printing rationale_for edges first (or never truncating them), since there is usually exactly one and it is the highest-value line.

electrumnz avatar Sep 21 '26 02:09 electrumnz

This looks fixed on the v8 branch by 25452a63 (fix(explain,query): say how to get the full answer when output is cut). Could a maintainer confirm and close?

omsenjalia avatar Oct 18 '26 09:10 omsenjalia