Skip to content

fix(explain): refuse a name that several nodes of one file carry - #4320

Closed
alexwrite wants to merge 2 commits into
Graphify-Labs:v8from
alexwrite:fix/explain-same-file-tie
Closed

alexwrite wants to merge 2 commits into
Graphify-Labs:v8from
alexwrite:fix/explain-same-file-tie

Conversation

@alexwrite

Copy link
Copy Markdown
Contributor

What does this PR do?

explain and the MCP get_node / get_neighbors tools refuse a name only when its winning match tier spans several source files. When several nodes of one file carry the name, the tie is broken by graph iteration order and one node is printed as the answer, with nothing saying a choice was made. path already refuses that case (#3913, #3935) and lists the node ids.

Two shapes seen on real graphs:

  • Two classes of one module each define a method with the same name (WebSocketRequestHandler.vmsg() and WebSocketServer.vmsg() in one Python file). explain ".vmsg()" printed one of them.
  • Per-file import stubs carry no source_file, so every stub of EntityManagerInterface counts as the same "file". explain "EntityManagerInterface" printed the stub of one test file, with that stub's degree.

Measured on three real PHP/JS/Python graphs (4.8k to 36.5k nodes), on 1,500 node labels drawn at random from each: 4, 12 and 22 labels resolved this way, silently.

Change. One helper, _tier_refusal, now decides whether the winning tier names a single node. _resolve_single_node (explain, get_node, get_neighbors) and _resolve_path_endpoint (path, shortest_path) both call it, so the four lookups refuse the same names with the same messages. A plain file path keeps its exception: its tier is the file node followed by its members, which is precedence, not a tie.

The explain CLI now resolves through _resolve_single_node instead of its own copy of the cross-file check. Exit codes are unchanged here (a refusal still exits 1, a miss still exits 0); distinct exit codes are a separate PR.

Type of change

  • Bug fix

Verification & Invariants

Invariant: a lookup by name answers with a node only when the name designates exactly one node; otherwise it lists the candidates and their ids. The answer never depends on node order in graph.json.

Persisted state: none. Read-only commands.

  • Read the CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

How was this tested?

# new tests against unmodified v8 (b4e6307)
uv run pytest tests/test_explain_same_file_tie.py -q
5 failed, 2 passed        (the 2 passing are guards: node id and file path still resolve)

# with this PR
uv run pytest tests/test_explain_same_file_tie.py tests/test_explain_cli.py tests/test_path_cli.py tests/test_serve.py -q
203 passed

uv run pytest tests/ -q
7172 passed, 14 skipped

uv run ruff check .
All checks passed!

uv run pyright
607 errors, identical set (file + message) to v8 b4e6307; none in the changed code

Linux only; no filesystem, path or process code is touched.

Limitations

  • A prefix or substring match that lands on one node still answers, as before: the printed Node: line shows which node it is. Only a tie is refused.
  • find_node_ambiguity is kept as is (public name) but is no longer called by explain.

explain and the get_node / get_neighbors MCP tools refused a name only
when its winning match tier spanned several source files. Within one file
(two classes each defining .send(), or per-file import stubs that carry no
source file) the tie was broken by graph iteration order and one node was
presented as the answer. path already refused that case with the node ids.

Route all of them through one _tier_refusal helper, so explain, get_node,
get_neighbors and the path endpoints refuse the same lookups. A plain file
path keeps resolving to its file node.

Co-Authored-By: Claude <[email protected]>
@alexwrite
alexwrite requested a review from safishamsi as a code owner October 11, 2026 10:01
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @alexwrite. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic.

@graphify-labs graphify-labs Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Graphify reviewed this change.

No issues found by static checks — no coupling regressions or blocking issues in the code graph. That is not the same as safe to merge: see what was not checked.

Not checked: tests were not run; no formal proof of the changed code.


Graphify review — findings

Makes explain, get_node and get_neighbors refuse a label whose winning match tier holds several nodes in the same file (or several sourceless stubs), instead of returning whichever one graph iteration order puts first. All three now go through _resolve_single_node, which shares a new _tier_refusal check with the path endpoints, so every lookup refuses the same names. A plain file-path query still resolves to the file node, a node ID still picks one twin, and explain exits 1 on ambiguity and 0 on no match.

No blocking issues surfaced. 3 lower-confidence candidates did not survive cross-model review.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 692 functions depend on the 218 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: main() — 104 callers, 3 callees
  • worse: _find_node_tiers() — 4 callers, 5 callees

Verification — 692 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 631 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

38 of 368 test file(s) selected (10%) via static blast radius.

  • tests/test_affected_cli.py — impact
  • tests/test_agents_platform.py — impact
  • tests/test_benchmark.py — impact
  • tests/test_benchmark_raw_graph.py — impact
  • tests/test_cloud_cta.py — impact
  • tests/test_codebuddy.py — impact
  • tests/test_dedup_shrink_refuses_force_write.py — impact
  • tests/test_devin.py — impact
  • tests/test_explain_cli.py — impact
  • tests/test_explain_same_file_tie.py — impact, changed-test
  • tests/test_extract_cli.py — impact
  • tests/test_file_label_disambiguation.py — impact
  • tests/test_global_add_tag_inference.py — impact
  • tests/test_god_nodes_cli.py — impact
  • tests/test_hollow_chunks_arm_shrink_guard.py — impact
  • tests/test_hook_guard_token_match.py — impact
  • tests/test_hook_out_of_project_paths.py — impact
  • tests/test_hook_strict.py — impact
  • tests/test_incomplete_build_guard.py — impact
  • tests/test_install.py — impact
  • tests/test_install_references.py — impact
  • tests/test_mcp_graph_view_cache.py — impact
  • tests/test_merge_chunks_validation.py — impact
  • tests/test_multigraph_diagnostics.py — impact
  • tests/test_no_dedup_flag.py — impact
  • tests/test_opencode_plugin_refresh.py — impact
  • tests/test_partial_cache.py — impact
  • tests/test_path_cli.py — impact
  • tests/test_query_cli.py — impact
  • tests/test_query_induced_edges.py — impact
  • tests/test_query_mcp_direction.py — impact
  • tests/test_query_names_its_graph.py — impact
  • tests/test_serve.py — impact
  • tests/test_serve_http.py — impact
  • tests/test_skill_auto_refresh.py — impact
  • tests/test_stale_prune.py — impact
  • tests/test_terraform.py — impact
  • tests/test_unverified_semantic_shrink.py — impact

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

· 2 more finding(s) on lines outside this diff (see the check run).

The wording of a refusal among sourceless stubs depends on how stubs are
grouped (one shared empty source, or one rival each), which is not what
this test is about.

Co-Authored-By: Claude <[email protected]>
safishamsi pushed a commit that referenced this pull request Oct 11, 2026
@safishamsi

Copy link
Copy Markdown
Member

Landed in 0.9.86 (now on PyPI) via an authorship-preserving cherry-pick, so your commit authorship is retained. Thank you, @alexwrite!

@safishamsi safishamsi closed this Oct 11, 2026
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.

2 participants