Skip to content

fix(query): weight seed scoring by file type so captions don't drown out code - #4601

Closed
ayushcodes10 wants to merge 2 commits into
Graphify-Labs:v8from
ayushcodes10:fix-445-query-seed-scoring
Closed

ayushcodes10 wants to merge 2 commits into
Graphify-Labs:v8from
ayushcodes10:fix-445-query-seed-scoring

Conversation

@ayushcodes10

Copy link
Copy Markdown
Contributor

Fixes #445.

Seed selection for a /graphify query question ran every candidate node through the same substring/prefix/exact label tiers regardless of file_type, so an image or document node with a long, verbose, human-written caption could match more of the query's words than a short, precise code symbol ever would, purely by being wordier - and win the BFS seed slot a code-centric question actually needed. Nothing in _score_query/_score_nodes weighed a node's type at all (confirmed by grep - no file_type reference anywhere in the scoring path).

Each node's final score, and its own per-term singleton score (used by _pick_seeds to guarantee at least one seed per query word), is now scaled by a small per-file-type factor, close to the one the issue itself proposed: unchanged (1.0) for code, concept and rationale nodes, down-weighted for document/paper (0.7) and image (0.4). A query that only matches a document or image still finds it - a lone nonzero match is never zeroed out, only out-ranked when a comparable code match exists too.

This narrows, rather than eliminates, the gap described in the issue: when a wordy caption matches several query terms as plain substrings and a code symbol only matches one, the raw term count can still win even after the weighting, since term-coverage scaling (#1602) only ever applied to the exact/prefix tiers, not the substring/source/rationale/attribute tiers. The case this fixes is the common one: a code symbol whose own name already contains the query words (so it already wins the tiered, coverage-scaled score outright) was previously undone only by an incidental tie with a shorter image/doc caption on the untied substring tier, with the existing "shorter label wins ties" rule handing it to the caption. That tie no longer happens. Degree and incoming-edge-confidence weighting, both mentioned in the issue as further signals, are left for a follow-up - this PR is scoped to the confirmed, zero-existing-coverage gap (no file_type weighting at all).

Verified against the issue's own repro shape (image captions containing the query's words outscoring a code node matching fewer, but more specific, terms) using a minimal constructed graph; confirmed via the safe git show+file-swap method that the new regression tests fail on pre-fix code and pass after the fix, then confirmed byte-identical restoration. Full suite and python3 -m tools.skillgen --check are clean.

Seed selection for a question ran every candidate through the same
substring, prefix and exact label tiers regardless of file type, so
an image or document node with a long, verbose, human written
caption could rack up more matched query words than a short, precise
code symbol ever would, purely by being wordier, and win the BFS
seed slot a code centric question actually needed. Nothing in the
scorer weighed a node's type at all.

Each node's final score, and its own per term singleton score used
to guarantee at least one seed per query word, is now scaled by a
small per file type factor: unchanged for code, concept and
rationale nodes, down weighted for document, paper and image nodes.
A query that only matches a document or image still finds it, since
a lone nonzero match is never zeroed out, only out ranked when a
comparable code match exists too.

This narrows, rather than eliminates, the gap: when a wordy caption
matches several query terms as plain substrings and a code symbol
only matches one, the raw term count can still win even after the
weighting, since coverage scaling only ever applied to the exact and
prefix tiers. The common case this fixes is a code symbol whose own
name already contains the query words, where it already won the
tiered score outright, undone only by an incidental tie with a
shorter caption on the untiered substring/source tiers; that case
now resolves correctly. Degree and incoming edge confidence, both
mentioned as further signals, are left for a follow up.
Covers a tied substring match where only the file type weighting
decides the winner, since equal raw scores would otherwise fall to
the existing shorter label tie break and hand it to a short caption.
Also checks document and paper nodes are down weighted the same way
as image, that an existing code labeled graph scores identically to
one with no file type set at all, and that a node stays findable
when it is the only match regardless of its type.
@github-actions

Copy link
Copy Markdown

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

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

Not checked: tests were not run; formal verification proved 0 of 1 changed function(s) (1 not verified).

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

Not verified on this run: \_score\_query (vacuous: never exercised).


Graphify review — findings

Adds a _FILE_TYPE_WEIGHT multiplier in _score_query that scales down document (0.7), paper (0.7) and image (0.4) node scores, so verbose captions stop outranking short code symbols just by matching more query words. The weight applies to both the per-term singleton seeds and the final node score, and code, concept and rationale nodes keep their existing scores; a doc or image that is the only match still surfaces. Because the multiplier sits on top of the exact-label tiers, an exact match on a down-weighted node can now fall below a weaker, non-exact code match.

Worth a look

  • File-type multiplier can demote exact image/document/paper label matches below non-exact code matches — graphify/serve.py:759 · Escalate · medium
    • NOT verified (no proof, no reproducing execution) — needs human review
Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 753 functions depend on the 399 functions this change touches.

Health — grade A; 10 existing hotspot(s) in the area this change touches (pre-existing, not introduced here):

  • dispatch_command() — 5 callers, 134 callees (high)
  • main() — 110 callers, 3 callees (high)
  • _query_graph_text() — 48 callers, 6 callees (high)
  • _score_query() — 17 callers, 7 callees (high)
  • _run_cli() — 11 callers, 8 callees (high)
  • _subgraph_to_text() — 25 callers, 3 callees (high)
  • run_benchmark() — 20 callers, 3 callees (high)
  • _query_terms() — 18 callers, 3 callees (high)
  • …and 2 more

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

Test selection

Test selection

16 of 436 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_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, changed-test
  • 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

Could not verify: Could not verify \_score\_query.

The verifier did not have enough to check \_score\_query, 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 47 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly TypeError — names the real obstacle, not a sampling gap)

@ayushcodes10

Copy link
Copy Markdown
Contributor Author

Closing: upstream v8 independently shipped a more general fix for the same root cause (issue #445) after this PR was opened, since hardened twice: a50ad2b introduces a per-node _query_path_weight/_QUERY_DEMOTE_WEIGHT mechanism (gated by demote_docs) that demotes test/doc/config/sourceless nodes for the query endpoint, 0caa1c3 adds a toggle to disable it for doc-first corpora, and 01c0a85 exposes that toggle via MCP. Mechanically rebasing this PR's static _FILE_TYPE_WEIGHT multiplier on top would double-apply demotion and fight that newer, toggleable design, so there is nothing left to add on the document/paper side. One real gap remains, upstream's mechanism has no handling at all for image captions (only document/paper), which this PR also covered. Opening a small follow-up PR to add that narrow piece on top of the current mechanism instead.

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.

Query seed-node selection matches image/doc captions, dominating BFS start for cross-cutting code queries

1 participant