Repository navigation
feat(query): --compact writes the same answer in ~1.7x fewer tokens (#4469) - #4470
devkancheti4-design wants to merge 2 commits into
Conversation
A query answer repeats each node's full file path, and on an unclustered graph an empty community= field, on every NODE line: on SWE-bench Lite answers those two make up about half the characters. compact (CLI --compact, MCP query_graph compact=true) keeps the budget cut, the notices and the order exactly as they are and rewrites only the NODE lines that survive: each source file once, in the order of its first node, with its nodes beneath, the community most of them share on the FILE line, and empty fields left out. A file's own node is dropped when the FILE line already says everything it would. Over 600 answers (300 SWE-bench Lite issues x 2 queries, 12 repos, AST-only graphs): 1,588 -> 912 tokens (1.74x fewer), the same files in the same order in 600 of 600. On clustered graphs (5 repos, 48 answers): 1.48x, 48 of 48. Default output is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
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. |
There was a problem hiding this comment.
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 6 changed function(s) (3 sampled, not proven, 3 not verified).
Formal verification. PR-changed functions: 3/6 verified (0 proven, 3 may-equivalent, 0 distinguished) · 3 not verified (3 vacuous).
Not verified on this run: dispatch\_command (vacuous: never exercised), \_build\_server (vacuous: never exercised), \_render\_query (vacuous: never exercised).
Graphify review — findings
Adds an opt-in --compact flag to graphify query (and a matching compact argument on the MCP query tool) that renders the same answer in fewer tokens, with each source file written once and its nodes listed beneath. The budget cut, notices and ordering are still decided by the default path, and _compact_node_block rewrites only the surviving NODE lines, so a node with no community can inherit its file's shared community label and the grouped view may not match the default output field for field. With the flag off, output is unchanged, and a complete answer that is over budget reports the size of its compact form instead; the MCP argument is read straight from the request, so a string like "false" can turn compact mode on.
Worth a look
- Compact view silently gives community-less nodes the file's shared community —
graphify/serve.py:1360· Escalate · medium- 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 — 849 functions depend on the 277 functions this change touches.
Health — this change adds coupling hotspots:
- worse:
_query_graph_text()— 49 callers, 5 callees - worse:
_subgraph_to_text()— 31 callers, 3 callees
Verification — 849 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: 849 function(s) in the blast radius were not formally verified this run
Test selection
Test selection
47 of 423 test file(s) selected (11%) via static blast radius.
tests/test_affected_cli.py— impacttests/test_agents_platform.py— impacttests/test_benchmark.py— impacttests/test_benchmark_raw_graph.py— impacttests/test_benchmark_real_query.py— impacttests/test_cloud_cta.py— impacttests/test_codebuddy.py— impacttests/test_compact_graph_json.py— impacttests/test_dedup_shrink_refuses_force_write.py— impacttests/test_devin.py— impacttests/test_explain_cli.py— impacttests/test_explain_same_file_tie.py— impacttests/test_extract_cli.py— impacttests/test_file_label_disambiguation.py— impacttests/test_global_add_tag_inference.py— impacttests/test_god_nodes_cli.py— impacttests/test_hollow_chunks_arm_shrink_guard.py— impacttests/test_hook_guard_token_match.py— impacttests/test_hook_out_of_project_paths.py— impacttests/test_hook_strict.py— impacttests/test_incomplete_build_guard.py— impacttests/test_install.py— impacttests/test_install_references.py— impacttests/test_install_version_warning.py— impacttests/test_lookup_exit_codes.py— impacttests/test_mcp_graph_view_cache.py— impacttests/test_merge_chunks_validation.py— impacttests/test_multigraph_diagnostics.py— impacttests/test_no_dedup_flag.py— impacttests/test_opencode_plugin_refresh.py— impacttests/test_partial_cache.py— impacttests/test_path_cli.py— impacttests/test_query_cli.py— impacttests/test_query_compact.py— impact, changed-testtests/test_query_induced_edges.py— impacttests/test_query_mcp_direction.py— impacttests/test_query_names_its_graph.py— impacttests/test_query_seed_order.py— impacttests/test_query_seeds.py— impacttests/test_serve.py— impacttests/test_serve_http.py— impacttests/test_skill_auto_refresh.py— impacttests/test_skill_version_warning.py— impacttests/test_stale_prune.py— impacttests/test_terraform.py— impacttests/test_truncation_full_answer.py— impacttests/test_unverified_semantic_shrink.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 \_run\_cli (not a proof).
The verifier ran both versions of \_run\_cli 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 dispatch\_command.
The verifier did not have enough to check dispatch\_command, 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 40 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly SystemExit — 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 40 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly ValueError — names the real obstacle, not a sampling gap)
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.
Could not verify: Could not verify \_render\_query.
The verifier did not have enough to check \_render\_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 223 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)
No difference found (not proven): No behavior difference found in \_subgraph\_to\_text (not a proof).
The verifier ran both versions of \_subgraph\_to\_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 grounded finding(s) anchored inline below; 1 more finding(s) on lines outside this diff (see the check run).
|
|
||
|
|
||
| def _subgraph_to_text(G: nx.Graph, nodes: set[str], edges: list[tuple], token_budget: int = 2000, *, seeds: list[str] | None = None) -> str: | ||
| def _subgraph_to_text(G: nx.Graph, nodes: set[str], edges: list[tuple], token_budget: int = 2000, *, |
There was a problem hiding this comment.
_subgraph_to_text()
31 callers depend on it (afferent coupling).
Grounded coupling-delta finding (deterministic), not an LLM guess.
There was a problem hiding this comment.
Addressed in cf6f238. _subgraph_to_text is back to v8 exactly: no signature or body change, so its 31 callers are untouched. Compaction is now a separate pure pass, _compact_answer(text, token_budget), over the already rendered answer. Only _render_query calls it, and only when compact=True. If a NODE line doesn't parse, the answer comes back unchanged. The measurements are the same: 1.74x fewer tokens over 600 SWE-bench Lite answers, with the same files in the same order in 600 of 600.
… applied by _render_query _subgraph_to_text has 31 callers, so its signature and body are back to v8 exactly. _compact_answer rewrites an already rendered answer and only _render_query calls it, only when compact is asked for. If any NODE line does not parse, the answer is returned unchanged. Same measurements: 1.74x fewer tokens over 600 SWE-bench Lite answers, the same files in the same order in 600 of 600; 1.48x on clustered graphs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Reviewed and tests pass (10/10). Confirming the bot's "string The bot's other finding ("community-less nodes inherit the file's shared community") looks like a false positive: line 1349 sets No other blocking issues. |
What does this PR do?
Closes #4469. It adds
compacttographify query(CLI--compact, MCPquery_graphcompact: true): the same answer in about 1.7x fewer tokens.The budget cut, the truncation notices and the node order are decided exactly as today. Only the
NODElines that survive the cut are rewritten:FILEline, and a node names its own community only when it differs;community=, no location) are left out;FILEline already says everything it would. It stays when it carries a different community or a learning or attrs annotation.Default output is unchanged.
compactdefaults to false everywhere.Measured
300 SWE-bench Lite issues x 2 queries (the issue title, and the title plus 400 characters), 12 repos, default BFS depth 2, 2,000-token budget:
update --no-cluster)cluster-only), 5 reposTokens are characters divided by 4. "Same files, same order" compares the distinct
src=paths in printed order with theFILElines.Type of change
Verification & Invariants
Invariant: compact changes how the chosen nodes are written, never which nodes are chosen or in what file order.
_subgraph_to_text(31 callers) is untouched. Compaction is a separate pure pass,_compact_answer(text, token_budget), over the answer_subgraph_to_textalready rendered: same cut, same notices, same order. Only_render_querycalls it, and only whencompact=True. Every field it rewrites was already passed throughsanitize_label. If any NODE line does not parse, the answer is returned unchanged. A complete answer that was over budget only in its default form loses the over-budget notice once its compact form fits.NODElines keep the default.How was this tested?
The new tests check four things:
compact=Falsereturns the default answer;Graphify-specific checklist
This comes from the same SWE-bench Lite study of
graphify queryas the seed-order fix (#4358) and--seed(#4289). It covers which files the query finds, in what order, and at what token cost. I have a few more measured changes to the query path from that work. I'd be glad to go through them with you, or to share the evaluation harness if it would help you judge query changes in general.