Skip to content

fix(mcp): enforce deterministic get_node resolution & graceful list_p… - #3920

Closed
A-P-Shukla wants to merge 5 commits into
Graphify-Labs:v8from
A-P-Shukla:fix-mcp-ambiguity
Closed

A-P-Shukla wants to merge 5 commits into
Graphify-Labs:v8from
A-P-Shukla:fix-mcp-ambiguity

Conversation

@A-P-Shukla

@A-P-Shukla A-P-Shukla commented Sep 29, 2026 •

Copy link
Copy Markdown

…rs handling (#3917)

What does this PR do?

Fixes two MCP failure modes: get_node now returns only exact-match results, listing candidates as JSON when ambiguous and returning {"error": "no exact match found"} otherwise. list_prs now checks for a local .git marker before calling gh when no repo= is provided, and reports the resulting error as an MCP tool error.

Repository detection checks .git relative to the current working directory. Pass repo= when running outside a directory containing that marker.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor
  • Security fix

Verification & Invariants

  • Read the CONTRIBUTING.md guide.
  • Reproduced the failure conditions in regression tests and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test (if bug fix) or isolated boundary test.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

How was this tested?

uv run --extra mcp pytest tests/test_prs.py tests/test_serve_http.py tests/test_serve.py -q
# 237 passed, 1 skipped

uv run graphify update .
git diff --check

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. (No skill source fragments changed.)
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages.

@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @A-P-Shukla. 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.

Worth a look — the grounded gate found no coupling regressions or blocking issues, but 1 advisory finding(s) below merit a look before merge.

Formal verification. 1 change(s) tested, no difference found (not proven).


Graphify review — findings

Tightens get_node to resolve only exact label/ID matches; an ambiguous label returns a JSON list of {path, line} candidates, and a miss or empty argument returns {"error": "no exact match found"} instead of falling back to prefix or substring guesses. fetch_prs and _detect_default_branch now fail fast with "could not detect repository; pass repo=" when no repo is given and the working directory has no .git, before calling gh. list_prs moves base-branch detection inside its error handling, so that failure comes back as an MCP tool error rather than escaping the handler.

Worth a look

  • Repo detection rejects valid repository subdirectories — graphify/prs.py:186 · Escalate · high
    • agreed by 2 of 2 members but NOT verified (no proof, no reproducing execution) — consensus is not a verdict; needs human review
Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 671 functions depend on the 305 functions this change touches.

Health — this change adds coupling hotspots:

  • new: main() — 98 callers, 3 callees
  • new: _query_graph_text() — 27 callers, 10 callees
  • new: dispatch_command() — 2 callers, 125 callees
  • new: _score_query() — 15 callers, 7 callees
  • new: _query_terms() — 20 callers, 3 callees
  • new: run_benchmark() — 16 callers, 3 callees
  • new: _load_graph() — 16 callers, 3 callees
  • new: fetch_prs() — 7 callers, 5 callees
  • …and 18 more — each is listed as a finding

Verification — 671 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: 486 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

10 of 304 test file(s) selected (3%) via static blast radius.

  • tests/test_benchmark.py — impact
  • tests/test_benchmark_raw_graph.py — impact
  • tests/test_file_label_disambiguation.py — impact
  • tests/test_prs.py — impact, changed-test
  • 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, changed-test
  • tests/test_terraform.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.

Formal verification

No difference found (not proven): No behavior difference found in \_detect\_default\_branch (not a proof).

The verifier ran both versions of \_detect\_default\_branch on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.

Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.

Note: An input the sampler did not try could still differ.

Could not verify: Could not verify fetch\_prs.

The verifier did not have enough to check fetch\_prs, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: not verifiable: all 200 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly RuntimeError — names the real obstacle, not a sampling gap)

Could not verify: Could not verify \_build\_server.

The verifier did not have enough to check \_build\_server, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: not verifiable: all 23 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous

· 1 grounded finding(s) anchored inline below; 25 more finding(s) on lines outside this diff (see the check run).

Comment thread graphify/prs.py Resolved
@safishamsi

Copy link
Copy Markdown
Member

Thanks for this @A-P-Shukla — the diagnosis of #3917 is right and the list_prs half is solid: _require_repo_context before any gh call, moving _detect_default_branch inside the try (that unhandled traceback was a real bug), and gh invoked as a list-form argv (arg-safe). That part is landable.

The get_node half needs rework before it can land, because it re-diverges get_node from get_neighbors:

  1. Keep get_node on the shared resolver. get_node and get_neighbors must answer identically for the same lookup (the shared _resolve_single_node -> find_node_ambiguity path; see the invariant note in serve.py). This PR gives get_node its own len(matches) > 1 check, so a label matching several nodes in the same file (a file node + its members, overloads) is now reported ambiguous by get_node but still resolves cleanly in get_neighbors. Please put the fix in the shared resolver so both tools get it, and reuse find_node_ambiguity (which distinguishes cross-file ambiguity from same-file precedence) rather than a raw count.

  2. Keep the text contract. The ambiguous / not-found paths now return json.dumps([...]) / {"error": ...}, while the happy path and every sibling tool return human-readable text. Please return the existing text form (the Ambiguous: ... retry with path::symbol or node id guidance and No node matching '<label>' found.), including the node id and retry hint that the JSON form drops. If a structured error is wanted, raise ToolError like list_prs does — consistently across both lookup tools.

  3. Fix the tests. test_resolve_single_node_shared_by_get_node_and_get_neighbors now documents a broken invariant, and test_get_node_and_get_neighbors_agree_on_ambiguous_label was rewritten to show them disagreeing. After (1), these should assert agreement again.

  4. Complaint v3: Codex and OpenClaw support #2 (noisy query_graph) is out of scope here — could you open a separate follow-up so it isn't lost?

The core goal (no silent wrong match on a near-miss) is right; the fix just needs to live in the shared path and keep the tool outputs consistent. Happy to re-review once that's in. Marking changes-requested.

@A-P-Shukla

Copy link
Copy Markdown
Author

@safishamsi All review points addressed and verified:

1. Shared Resolver Invariant Restored

  • Removed custom ambiguity/counting logic from get_node.
  • Shifted strict exact matching into _resolve_single_node, so both get_node and get_neighbors use the same resolver path.
  • Reused find_node_ambiguity to preserve same-file precedence, such as class/file-node and overload collisions, while rejecting exact matches that span multiple files.

2. Text Contract & Error Handling

  • Removed the JSON error and candidate payloads.
  • Ambiguous lookups return standardized human-readable text containing candidate paths, node IDs, and path::symbol retry hints.
  • Supplied labels or IDs that have no exact match return No node matching '<label>' found. through the shared resolver.
  • Successful lookups retain each tool’s purpose-specific output; ambiguity and not-found handling are shared.

3. Test Suite & Invariants

  • Preserved test_resolve_single_node_shared_by_get_node_and_get_neighbors.
  • Updated test_get_node_and_get_neighbors_agree_on_ambiguous_label to assert identical ambiguity text from both tools.
  • Added regression coverage for same-file precedence, exact-only matching, and nested .git repository resolution in list_prs.

Validation Run

  • Directly affected suites (test_prs.py, test_serve.py, and test_serve_http.py): 240 passed, 1 skipped.
  • Diagnostics and git diff --check: clean.
  • The full repository suite also includes unrelated platform and optional-dependency failures.

Note: The existing successor-first output ordering in get_neighbors can still hide incoming edges under constrained token budgets. That behavior remains outside this PR’s scope.

Ready for re-review.

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