Skip to content

feat(query): a natural-language question leads with its best-matching file (#4480) - #4481

Open
devkancheti4-design wants to merge 1 commit into
Graphify-Labs:v8from
devkancheti4-design:right-file-first
Open

devkancheti4-design wants to merge 1 commit into
Graphify-Labs:v8from
devkancheti4-design:right-file-first

Conversation

@devkancheti4-design

Copy link
Copy Markdown
Contributor

What does this PR do?

Closes #4480. On a natural-language question, the query now starts from the file that matches the question best, instead of the single best-scoring node. The renderer prints the start nodes first, so this is the file an agent reads first.

_lead_by_file(G, ranked, terms) runs on questions of three or more terms:

  • it sums the scores of the 50 best-scored candidates per file;
  • the best node of the best file becomes the first start node, and the other seeds from _pick_seeds keep their order behind it;
  • a test file (graphify.paths._is_test_path) leads only when the question names tests (test, pytest, fixture, spec, …), or when only test files match;
  • ties break by path, so the choice is deterministic.

Short queries return None and are unchanged, so an identifier lookup keeps its exact match first. Explicit --seed values never reach this code. _pick_seeds, _score_query and _subgraph_to_text are untouched.

Measured

SWE-bench Lite: 285 issues across 12 repos, graphs from graphify update, default BFS depth 2, 2,000-token budget. Each count is how often the fix's changed file is printed first, in the first 3 files, and in the first 5:

by title by title + 400 characters
v8 (0.9.91) 78 / 115 / 131 67 / 99 / 122
this PR 88 / 121 / 136 80 / 117 / 140
per-file sum alone 73 / 120 / 135 45 / 104 / 128
test rule alone 83 / 115 / 132 76 / 100 / 122

Neither part works alone. Test files gather many matching nodes, so summing without the test rule makes the first file worse. The test rule without the sum fixes the first file but not the top five.

The constants (50 candidates, 3 terms) were chosen on django and sympy. On the 10 other repos alone, right file first goes 24 → 27 by title and 20 → 29 by title + 400.

Type of change

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

Verification & Invariants

Invariants:

  • a query of fewer than three terms renders exactly as before;

  • the lead is always a node the ranking already scored, so a question that matches nothing still says so;

  • the lead is deterministic: ties break by path;

  • no generated or persisted state changes; it is query-time only.

  • Read the CONTRIBUTING.md guide.

  • Reproduced the issue and identified the invariant.

  • Made the smallest fix necessary.

  • Added isolated boundary tests and end-to-end tests.

  • Kept the PR description synchronized with the final implementation.

  • Documented any limitations / unsupported cases explicitly:

    • lexical, like the rest of query scoring, so a file that shares no words with the question can't lead;
    • the start list grows by at most one node (the lead) when _pick_seeds hadn't already picked it;
    • in 94 of the 207 title misses on v8, the fixed file isn't among the 50 best-scored nodes at all, so no reordering can help those.

How was this tested?

uv run python -m pytest -q tests/test_query_lead_file.py            # 10 passed
uv run python -m pytest -q tests -k "query or reflect or learning or memory or lesson or mcp or serve or ingest or save_result or benchmark"
                                                                    # 799 passed, 9 skipped
uv run python -m pytest -q tests                                    # 7825 passed, 185 skipped; the 7 failures are in
                                                                    # tests/test_ext_tagged_id_clash.py and fail the same way on v8 locally
uv run ruff check graphify/serve.py tests/test_query_lead_file.py   # clean

The new tests cover:

  • the file with the best summed score leads over the single best node;
  • a short query keeps its exact match first;
  • a test file doesn't lead unless the question names tests, and still leads when only tests match;
  • ties break by path;
  • nodes without a file, or outside the graph, are skipped;
  • only the 50 best-scored candidates count;
  • end to end, through _query_graph_text: a 4-term question leads with the best file, a 2-term query is unchanged, and a question about tests leads with the test.

With the lead switched off, the first end-to-end test fails.

Graphify-specific checklist

  • No skill fragments changed.
  • AST/structural extraction untouched; the lead is deterministic.
  • No shell or Python interpolation.
  • No API keys or local graph data included.
  • AI authorship disclosed in the commit message.

🤖 Generated with Claude Code

… file

On a question of three or more terms, the single best-scoring node is often
a test, or a same-named helper in another file, while the file the question
is about matches it through several nodes at once. `_lead_by_file` sums the
scores of the 50 best-scored candidates per file and puts the best node of
the best file first among the start nodes; the other seeds keep their order.
A test file leads only when the question names tests or nothing else
matches. Short queries (identifier lookups) are unchanged.

SWE-bench Lite, 285 issues across 12 repos, default BFS depth 2, 2,000-token
budget, fixed file printed first: 78 -> 88 asked by the issue title, 67 -> 80
by title plus 400 characters (first five files: 131 -> 136, 122 -> 140).
The constants were chosen on django and sympy; on the 10 other repos alone:
24 -> 27 and 20 -> 29.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @devkancheti4-design. 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.

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Oct 15, 2026

@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; formal verification proved 0 of 1 changed function(s) (1 sampled, not proven).

Formal verification. PR-changed functions: 1/1 verified (0 proven, 1 may-equivalent, 0 distinguished) · 0 not verified.


Graphify review — findings

Adds file-aware lead selection to natural-language queries: with 3+ search terms, _query_graph_text now starts from the best node of the file whose top-50 scored candidates have the highest summed score, instead of the single best-scoring node. The other seeds keep their order behind it. Test files are skipped as the lead unless the question mentions tests or only test files match, and ties break by path so the result is deterministic. Short queries (under 3 terms) skip this and keep their exact match first, so identifier lookups behave as before. The commit comment reports SWE-bench Lite gains (fixed file printed first: 78 → 88 by title, 67 → 80 by title plus 400 characters), with the constants tuned on django and sympy. New tests cover the ranking, test-file handling, tie-breaks and the candidate cutoff.

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

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 648 functions depend on the 170 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: _query_graph_text() — 51 callers, 7 callees

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

Test selection

Test selection

17 of 423 test file(s) selected (4%) via static blast radius.

  • tests/test_benchmark.py — impact
  • tests/test_benchmark_raw_graph.py — impact
  • tests/test_benchmark_real_query.py — impact
  • tests/test_explain_same_file_tie.py — impact
  • tests/test_file_label_disambiguation.py — impact
  • tests/test_mcp_graph_view_cache.py — impact
  • tests/test_path_cli.py — impact
  • tests/test_query_induced_edges.py — impact
  • tests/test_query_lead_file.py — impact, changed-test
  • tests/test_query_mcp_direction.py — impact
  • tests/test_query_names_its_graph.py — impact
  • tests/test_query_seed_order.py — impact
  • tests/test_query_seeds.py — impact
  • tests/test_serve.py — impact
  • tests/test_serve_http.py — impact
  • tests/test_terraform.py — impact
  • tests/test_truncation_full_answer.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 \_query\_graph\_text (not a proof).

The verifier ran both versions of \_query\_graph\_text 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.

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

@devkancheti4-design

Copy link
Copy Markdown
Contributor Author

@safishamsi Devieswar here (Model D; this PR, #4470 and #4478).

I'd like to give Graphify Model D's core, written natively into Graphify under its Apache-2.0 license, with no dependency on Model D:

  1. Tests after code. An independent evaluation found Graphify's first answer was a test file in 50 of 120 real bug fixes. Putting code before its tests in Graphify's own recorded answers lifts right-file-first from 25 to 41 of 120. Model D ships this rule.
  2. Lessons pinned to lines. save-result lessons keep the exact line they came from, and reflect re-finds it after the code changes, instead of dropping the lesson.
  3. This PR, feat(query): --compact writes the same answer in ~1.7x fewer tokens (#4469) #4470 and feat(query): --learned starts from nodes that past useful answers to a similar question cited (#4477) #4478: file first, learned seeds, compact output.

If the metrics convince you, I'd like the title of core performance engineer on Graphify. I can start with (1) as a PR this week, so you can judge the work first.

The evaluation, with its scripts and raw rows: https://optimal-context-mapping-site.vercel.app/independent/

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor Pull request from a returning community contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: query: lead with the file that matches a natural-language question best, not the single best-scoring node

1 participant