fix(serve): a name spelled exactly wins a lookup tie that is only case

Since #2779 the Go extractor keeps `Run` and `run` apart, but node lookup
folds case: both land in the exact tier and every lookup is refused as a
same-file tie, even one spelled exactly like one of them. The get_node and
get_neighbors tools also lowercased the query before resolving it. A
case-only pair could be reached only by its opaque id.

When the exact tier holds names that are equal up to case and exactly one
label is written as queried, that node is the exact tier and the others
move to the front of prefix. A query in neither spelling, or one spelling
that two files share, is still refused. get_node and get_neighbors pass
the label as written; the tiers fold case on their own.

(cherry picked from commit 507c5f351a)
This commit is contained in:
nothariharan authored and safishamsi committed 2026-10-30 22:43:06 +00:00
1 parent e677fb8093
commit 0d251fe577
3 files changed
+93 -2

No files matched your search

+17 -2
View File
@@ -1892,6 +1892,21 @@ def _find_node_tiers(
if members:
return [], members, [], []
# The tiers fold case, so in a case-sensitive language `Run` and `run` (two
# symbols the extractor keeps apart, #2779) tie in `exact` and every lookup is
# refused, even one spelled exactly like one of them. When the tie is only
# case and exactly one label is written as queried, it wins; the others stay
# reachable one tier down. Mutually exclusive with the `Type.Member` block
# above (that fires only when `exact` is empty), so order does not matter.
if len(exact) > 1:
def _bare(nid: str) -> str:
return str(G.nodes[nid].get("label") or "").strip().lstrip(".").removesuffix("()")
wanted = str(label).strip().lstrip(".").removesuffix("()")
same_case = [nid for nid in exact if _bare(nid) == wanted]
if len(same_case) == 1 and all(_bare(nid).lower() == wanted.lower() for nid in exact):
prefix = [nid for nid in exact if nid != same_case[0]] + prefix
exact = same_case
if source_exact:
query_basename = _strip_diacritics(Path(label).name).lower()
preferred = []
@@ -2712,7 +2727,7 @@ def _build_server(graph_path: str):
raw = _node_arg(arguments)
if not raw:
return "Provide a node label or id (accepted keys: label, node_id, id)."
label = raw.lower()
label = raw
nid, err = _resolve_single_node(G, label)
if err:
return err
@@ -2745,7 +2760,7 @@ def _build_server(graph_path: str):
raw = _node_arg(arguments)
if not raw:
return "Provide a node label or id (accepted keys: label, node_id, id)."
label = raw.lower()
label = raw
rel_filter = arguments.get("relation_filter", "").lower()
nid, err = _resolve_single_node(G, label)
if err:
+47
View File
@@ -2088,3 +2088,50 @@ def test_query_graph_text_surfaces_parallel_relationships(tmp_path):
text = _query_graph_text(G, "applySeed", token_budget=2000)
assert "--contains " in text, text
assert "--calls " in text, text
# --- a tie only by case goes to the exact spelling ---------------------------
def _case_pair_graph() -> nx.Graph:
# Go keeps `CheckHealth` and `checkHealth` apart (#2779): an exported method
# and its unexported helper on one type, in one file.
G = nx.Graph()
G.add_node("api_server_checkhealth", label=".CheckHealth()",
source_file="api/server.go", source_location="L9")
G.add_node("api_server_checkhealth_efb2ee", label=".checkHealth()",
source_file="api/server.go", source_location="L13")
G.add_edge("api_server_checkhealth", "api_server_checkhealth_efb2ee", relation="calls")
return G
@pytest.mark.parametrize("query, expected", [
("CheckHealth", "api_server_checkhealth"),
("checkHealth", "api_server_checkhealth_efb2ee"),
(".checkHealth()", "api_server_checkhealth_efb2ee"),
])
def test_a_name_spelled_exactly_wins_a_tie_that_is_only_case(query, expected):
from graphify.serve import _resolve_single_node
nid, err = _resolve_single_node(_case_pair_graph(), query)
assert err is None, err
assert nid == expected
def test_a_case_tie_neither_spelling_matches_stays_ambiguous():
from graphify.serve import _resolve_single_node
nid, err = _resolve_single_node(_case_pair_graph(), "checkhealth")
assert nid is None
assert err.startswith("Ambiguous:"), err
def test_an_exact_spelling_does_not_break_a_tie_between_two_files():
# Same spelling in two files is a real ambiguity: case cannot settle it.
from graphify.serve import _resolve_single_node
G = _case_pair_graph()
G.add_node("web_router_checkhealth", label=".CheckHealth()",
source_file="web/router.go", source_location="L4")
nid, err = _resolve_single_node(G, "CheckHealth")
assert nid is None
assert err.startswith("Ambiguous:"), err
+29
View File
@@ -433,6 +433,35 @@ def test_get_node_and_get_neighbors_agree_on_ambiguous_label(tmp_path):
assert "Node: unique_helper" in unique, unique
def test_get_node_resolves_a_name_spelled_exactly_when_the_tie_is_only_case(tmp_path):
"""Go keeps `CheckHealth` and `checkHealth` apart (#2779); the tools lowercased the
query before resolving it, so neither could be reached by name."""
graph = {
"directed": True,
"nodes": [
{"id": "srv_checkhealth", "label": ".CheckHealth()", "community": 0,
"source_file": "api/server.go", "source_location": "L9"},
{"id": "srv_checkhealth_efb2ee", "label": ".checkHealth()", "community": 0,
"source_file": "api/server.go", "source_location": "L13"},
],
"edges": [
{"source": "srv_checkhealth", "target": "srv_checkhealth_efb2ee",
"relation": "calls", "confidence": "EXTRACTED"},
],
}
p = tmp_path / "graph.json"
p.write_text(json.dumps(graph), encoding="utf-8")
app = serve_mod._build_http_app(str(p), json_response=True)
with _client(app) as client:
headers = _init_session(client)
node = _call_tool(client, headers, "get_node", {"label": "checkHealth"}, rid=2)
assert "ID: srv_checkhealth_efb2ee" in node, node
neighbors = _call_tool(client, headers, "get_neighbors", {"label": "CheckHealth"}, rid=3)
assert neighbors.startswith("Neighbors of .CheckHealth()"), neighbors
folded = _call_tool(client, headers, "get_node", {"label": "checkhealth"}, rid=4)
assert folded.startswith("Ambiguous:"), folded
# --- graph_stats build commit (#3354) ----------------------------------------
_STATS_NO_COMMIT = (