Shared node resolution for the `explain` CLI command and the get_node / get_neighbors MCP tools. Returns ``(node_id, None)`` when *label* resolves to a single winner via the tiered `_find_node` ranking, or ``(None, message)`` when there is no match or the winning tier holds several
(G: nx.Graph, label: str)
| 2617 | |
| 2618 | |
| 2619 | def _resolve_single_node(G: nx.Graph, label: str) -> tuple[str | None, str | None]: |
| 2620 | """Shared node resolution for the `explain` CLI command and the get_node / |
| 2621 | get_neighbors MCP tools. |
| 2622 | |
| 2623 | Returns ``(node_id, None)`` when *label* resolves to a single winner via the |
| 2624 | tiered `_find_node` ranking, or ``(None, message)`` when there is no match or |
| 2625 | the winning tier holds several nodes, in different files or in one, as the |
| 2626 | `path` endpoints refuse them. Routing both tools through this keeps get_node |
| 2627 | from silently returning a `G.nodes()` iteration-order match for a hub name |
| 2628 | while get_neighbors reports the same lookup as ambiguous (#ADR-0001). |
| 2629 | |
| 2630 | A prefix match still resolves: the caller started typing the real name |
| 2631 | (`Email` -> `EmailController`). A substring match does not: the query sits |
| 2632 | in the middle of an unrelated label (`MailController` -> `EmailController`), |
| 2633 | which is a search hit, so it comes back as a near miss with suggestions |
| 2634 | instead of a different node presented as the answer (#3917). |
| 2635 | |
| 2636 | A node id names exactly one node, so it resolves before any ranking: the |
| 2637 | refusals tell the caller to retry with the id, and an id that tokenizes |
| 2638 | like a source path must not lose to that file's node (#5343). |
| 2639 | """ |
| 2640 | if label in G: |
| 2641 | return label, None |
| 2642 | source_exact, exact, prefix, substring = _find_node_tiers(G, label) |
| 2643 | if not (source_exact or exact or prefix): |
| 2644 | if not substring: |
| 2645 | return None, f"No node matching '{label}' found." |
| 2646 | return None, _near_miss_message(G, label, substring) |
| 2647 | tier = source_exact or exact or prefix |
| 2648 | refusal = _tier_refusal(G, label, tier, file_query=bool(source_exact) and "::" not in label) |
| 2649 | if refusal: |
| 2650 | return None, refusal |
| 2651 | return tier[0], None |
| 2652 | |
| 2653 | |
| 2654 | def _query_name_refusal(G: nx.Graph, label: str) -> str | None: |