fix(path): refuse an endpoint whose words the graph doesn't know

`graphify path` fell back to the top of the score ranking for any name that
matched no tier, so `zzqqxx_no_such_symbol_9137` resolved to
`no_such_symbol_helper()` (three shared `_`-split tokens) and printed its route
with exit 0, while `explain` on the same name said "No node matching".
Nothing that exists could have answered that request, and the route reads as
if it had.

The fallback now only runs when every word of the query appears in some
node's name or id. That keeps the partial names it is there for
(`draft-generator` -> `draft-generator.ts`, `Reject-everything judge` ->
`Degenerate Reject-Everything Judge`, a label the caller is still typing) and
turns a name made of unknown words into the miss `explain` already reports,
which the CLI surfaces as exit 4.

Fixes #5079

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Houyun SunandClaude committed 2026-11-08 10:50:32 +08:00
1 parent 6c8002eca4
commit a084c3dd67
2 files changed
+96 -1

No files matched your search

+45 -1
View File
@@ -2256,6 +2256,11 @@ def _resolve_path_endpoint(
symbols of one file were split by graph order behind a score-tie warning,
and `path::symbol` cannot separate them — only the node id can.
A name made of words the graph doesn't know is a miss, not the top of a
weak ranking (#5079): `explain` already answers "No node matching" for it,
and routing from the closest-sounding label instead prints a path through
a symbol nobody asked about.
Returns ``(node_id, scored, None)``, where ``scored`` is empty unless the
score fallback picked the node; ``(None, [], message)`` when ambiguous; and
``(None, [], None)`` when nothing matches.
@@ -2285,12 +2290,51 @@ def _resolve_path_endpoint(
no_member = _no_member_match_message(G, query)
if no_member:
return None, [], no_member
scored = _score_nodes(G, [t.lower() for t in query.split()])
terms = [t.lower() for t in query.split()]
scored = _score_nodes(G, terms)
if not scored:
return None, [], None
if not _query_uses_known_words(G, terms):
return None, [], None
return _pick_scored_endpoint(G, scored, query), scored, None
def _query_uses_known_words(G: nx.Graph, terms: list[str]) -> bool:
"""Whether every word of the query occurs in some node's name.
Scoring ranks by token overlap, so a made-up name still finds candidates:
`zzqqxx_no_such_symbol_9137` matches `no_such_symbol_helper()` on `no`,
`symbol` and `helper`, and the route through that symbol prints as if it
were the one asked for (#5079). The scored fallback is only honest when
the query is at least partly in the graph's vocabulary, so each token has
to occur somewhere — then the query shares a word with what it routes
through (`Rejection judge` -> `Rejection Summary`, its tokens once said
whole). A token that appears nowhere (`zzqqxx`, `9137`) means the name the
caller asked for isn't in the graph, which is what `explain` reports too.
Looser than `explain`'s resolution on purpose: there any substring hit is
a "near miss" worth refusing with suggestions, but `path` answers with a
route rather than a name, and a caller still typing (`draft-generator` for
`draft-generator.ts`) needs the score fallback's ranking.
One pass per token, each stopping at the first node that carries it; the
vocabulary is `_search_tokens` pieces of labels and ids, the same split the
scorer uses.
"""
tokens = list(dict.fromkeys(tok for t in terms for tok in _search_tokens(t)))
if not tokens:
return False
# A query is usually rejected on its first unknown word (~a fifth of a score
# pass on a 60k-node graph), and nothing is memoised on a shared graph.
for token in tokens:
for nid, d in G.nodes(data=True):
if token in _search_tokens(nid) or token in _search_tokens(d.get("label") or ""):
break
else:
return False
return True
def _path_search_graph(G: nx.Graph, undirected: bool) -> nx.Graph:
"""The sorted, materialized graph `_shortest_path_text` searches.
+51
View File
@@ -560,3 +560,54 @@ def test_path_scoped_endpoint_honors_file_qualifier(monkeypatch, tmp_path, capsy
gp = _cross_file_symbol_graph(tmp_path)
out = _run(monkeypatch, gp, "src/billing/route.ts::readRoute", "chargeCard", capsys)
assert "readRoute() --calls [EXTRACTED]--> chargeCard()" in out
def _no_such_node_graph(tmp_path):
"""#5079: `zzqqxx_no_such_symbol_9137` names nothing, but `no_such_symbol_helper()`
shares three of its `_`-split tokens and wins the score fallback, so `path` used
to print the helper's route and exit 0 while `explain` said "No node matching"."""
return _write_nodes(tmp_path, [
("src_a_ts", "a.ts", "src/a.ts", "L1"),
("src_a_ts_parse", "parse()", "src/a.ts", "L1"),
("src_b_ts", "b.ts", "src/b.ts", "L1"),
("src_b_ts_helper", "no_such_symbol_helper()", "src/b.ts", "L1"),
("src_b_ts_format", "format()", "src/b.ts", "L1"),
("src_c_ts", "c.ts", "src/c.ts", "L1"),
("src_c_ts_format", "format()", "src/c.ts", "L1"),
], [
("src_a_ts", "src_a_ts_parse", "contains"),
("src_a_ts_parse", "src_b_ts_helper", "calls"),
("src_b_ts", "src_b_ts_helper", "contains"),
("src_b_ts", "src_b_ts_format", "contains"),
("src_c_ts", "src_c_ts_format", "contains"),
("src_b_ts_helper", "src_b_ts_format", "calls"),
])
_NO_SUCH_NODE = "zzqqxx_no_such_symbol_9137"
def test_path_refuses_endpoint_that_names_no_node(monkeypatch, tmp_path, capsys):
"""An endpoint matching no node name at all must fail closed, like `explain`,
instead of routing from whichever node happened to share some tokens (#5079)."""
gp = _no_such_node_graph(tmp_path)
monkeypatch.setattr(mainmod, "_check_skill_version", lambda _: None)
monkeypatch.setattr(mainmod.sys, "argv",
["graphify", "path", _NO_SUCH_NODE, "src/b.ts::format()", "--graph", str(gp)])
with pytest.raises(SystemExit) as exc_info:
mainmod.main()
assert exc_info.value.code == EXIT_NOT_FOUND
captured = capsys.readouterr()
assert "Shortest path" not in captured.out
assert _NO_SUCH_NODE in captured.err
assert "no_such_symbol_helper" not in captured.out
def test_shortest_path_tool_refuses_endpoint_that_names_no_node(tmp_path):
"""The MCP tool shares the resolver, so a bare name it cannot place is a miss too."""
from graphify.serve import _shortest_path_text
raw = json.loads(_no_such_node_graph(tmp_path).read_text())
G = json_graph.node_link_graph(raw, edges="links")
out = _shortest_path_text(G, {"source": _NO_SUCH_NODE, "target": "src/b.ts::format()"})
assert "Shortest path" not in out
assert f"No node matching source '{_NO_SUCH_NODE}' found." in out