Repository navigation
feat(extract): preserve declaration end lines with source provenance - #4968
xiongjianxu wants to merge 13 commits into
Conversation
|
Thanks for the pull request, @xiongjianxu. 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. |
Preserve inclusive declaration and definition ranges through AST extraction, provenance-aware merges, build, export and global graphs. Retire older AST caches so cached records cannot omit ranges. Refs Graphify-Labs#4964
4911d8e to
c3e2bc6
Compare
There was a problem hiding this comment.
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; no formal proof of the changed code.
Graphify review — findings
Adds metadata.end_line to source-backed AST declarations, a 1-based inclusive end of the declaration alongside the existing start-only source_location. The shared engine's type, function and declaration hooks plus the Go and Objective-C extractors fill it in; file nodes, reference stubs and synthetic owners omit it. A merged header declaration keeps its own range, with the separate definition site getting metadata.definition_end_line. Dedup now backfills end_line from a duplicate only when the source_file and source_location match, so ranges never get copied across different declarations. Bumps the AST cache schema to 38 so extractions cached before declaration ranges existed re-run, and replaces the long per-version changelog comment on _AST_CACHE_SCHEMA with the bare number.
Review partial — this diff was larger than one review pass covers, so later files were not reviewed; some findings may be missing.
Analysis details — impact, health, verification
Impact & health
Graphify review
Impact — 6264 functions depend on the 865 functions this change touches.
Health — this change adds coupling hotspots:
- worse:
extract()— 1118 callers, 66 callees - worse:
to_json()— 98 callers, 13 callees - worse:
_extract_generic()— 20 callers, 54 callees - worse:
build()— 93 callers, 6 callees - worse:
extract_objc()— 27 callers, 14 callees - worse:
extract_go()— 19 callers, 14 callees - worse:
_merge_decl_def_classes()— 5 callers, 3 callees - new:
test_objc_declaration_and_definition_ranges_survive_pipeline()— 0 callers, 6 callees
Verification — 6264 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: 4104 function(s) in the blast radius were not formally verified this run
Test selection
Test selection
502 of 502 test file(s) selected (100%) via static blast radius.
Escalated to a full run for safety — the selection is not trustworthy on its own (see below). CI should run the whole suite.
tests/pi.test.ts— full-run-safetytests/test_4874_jvm_resolution.py— impact, full-run-safetytests/test_4909_reference_binding_scale.py— impact, full-run-safetytests/test_affected_cli.py— full-run-safetytests/test_affected_defines.py— full-run-safetytests/test_affected_member_seed.py— full-run-safetytests/test_affected_overrides.py— full-run-safetytests/test_affected_path_file_node.py— full-run-safetytests/test_affected_seed_reference_forms.py— full-run-safetytests/test_agents_platform.py— full-run-safetytests/test_alias_folding.py— impact, full-run-safetytests/test_analyze.py— impact, full-run-safetytests/test_anthropic_custom_endpoint.py— full-run-safetytests/test_antigravity_install.py— full-run-safetytests/test_apm_fallback_version.py— full-run-safetytests/test_architecture_doc.py— full-run-safetytests/test_astro_extraction.py— impact, full-run-safetytests/test_astro_import_ids.py— impact, full-run-safetytests/test_atomic_canvas_export.py— full-run-safetytests/test_atomic_version_stamp.py— full-run-safetytests/test_atomic_writes.py— impact, full-run-safetytests/test_backend_env_isolation.py— full-run-safetytests/test_backend_extras.py— full-run-safetytests/test_backend_opt_in.py— impact, full-run-safetytests/test_benchmark.py— impact, full-run-safetytests/test_benchmark_raw_graph.py— impact, full-run-safetytests/test_benchmark_real_query.py— impact, full-run-safetytests/test_binary_media_words.py— impact, full-run-safetytests/test_blade_extractor.py— full-run-safetytests/test_build.py— impact, full-run-safetytests/test_build_located_semantic_identity.py— impact, full-run-safetytests/test_build_merge_dedup_scope.py— impact, full-run-safetytests/test_build_merge_hyperedges_and_prune.py— impact, full-run-safetytests/test_build_merge_shrink_guard.py— impact, full-run-safetytests/test_builtin_global_type_refs.py— full-run-safetytests/test_c_include_resolution.py— impact, full-run-safetytests/test_cache.py— impact, full-run-safetytests/test_cache_long_paths.py— impact, full-run-safetytests/test_cache_stale_import_target.py— impact, full-run-safetytests/test_cache_windows_case_4925.py— impact, full-run-safetytests/test_callflow_html.py— full-run-safetytests/test_cargo_introspect.py— full-run-safetytests/test_cargo_missing_manifest.py— full-run-safetytests/test_carried_hyperedge_remap.py— impact, full-run-safetytests/test_case_only_decl_ids_4904.py— full-run-safetytests/test_case_sensitive_resolution.py— full-run-safetytests/test_charmap_encoding.py— impact, full-run-safetytests/test_chunking.py— impact, full-run-safetytests/test_cjs_module_extension.py— impact, full-run-safetytests/test_claude_cli_backend.py— impact, full-run-safety- … and 452 more
non-code file(s) changed (
ARCHITECTURE.md) → running the full suite for safety (a code graph can't see config/fixture/data deps)
changed code file(s) with no mapped test (
ARCHITECTURE.md) — a coverage gap or a missing link — running the full suite rather than only the selected tests
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.
Docs that may be stale (advisory)
graphify/skill-devin.md§ Step 6 - Generate Obsidian vault (opt-in) + HTML (lines 594-675): references changed symbolsadd_nodetools/skillgen/expected/graphify__skill-devin.md§ Step 6 - Generate Obsidian vault (opt-in) + HTML (lines 594-675): references changed symbolsadd_nodetools/skillgen/fragments/core/devin.md§ Step 6 - Generate Obsidian vault (opt-in) + HTML (lines 594-675): references changed symbolsadd_node
· 1 grounded finding(s) anchored inline below; 7 more finding(s) on lines outside this diff (see the check run).
| assert "end_line" not in owner.get("metadata", {}) | ||
|
|
||
|
|
||
| def test_objc_declaration_and_definition_ranges_survive_pipeline(tmp_path, monkeypatch): |
There was a problem hiding this comment.
test_objc_declaration_and_definition_ranges_survive_pipeline()
fans out to 6 callees (efferent coupling).
Grounded coupling-delta finding (deterministic), not an LLM guess.
…erges Prefer the implementation record's existing definition provenance over its class-extension declaration site. Keep declaration ranges separate and omit unknown definition ends. Refs Graphify-Labs#4964
Keep v8's C++ member id salting, type_path and qualified-definition branches, and pass ast_node through them. AST cache schema moves to 49 on top of v8's 48.
query NODE lines, MCP get_node and CLI explain print L<start>-L<end> when a declaration records metadata.end_line, so an agent can read the declaration without guessing a window. One-line declarations and nodes without a range keep the start-only form.
There was a problem hiding this comment.
Graphify review 
9 possible issues to check · High impact
The change adds inclusive declaration end-line metadata to extracted nodes and displays source ranges in graph output.
9 possible issues we couldn't confirm
- A partially recorded implementation definition is replaced by the source site, which may be the class extension the code is trying to avoid ·
graphify/extractors/resolution.py:4246 - When the implementation has no end line, the declaration's end line is deleted, even though the PR title says it should be preserved ·
graphify/extractors/resolution.py:4249 - NS_ENUM/NS_OPTIONS enums are emitted without an end line, unlike typedef enums ·
graphify/extractors/objc.py:275 - +6 more in Graphify
They don't change the score.
What this review covered
- We didn't review 1 file that isn't source code, such as docs, config, images, or generated or third-party code.
- 10 of 10 changed files inspected. This is scope, not a correctness guarantee.
Code graph · 7605 functions depend on this change
- 9 functions became more coupled. Largest:
extract()(1354 callers),build()(103 callers),_subgraph_to_text()(29 callers). - 621 of 621 test files are affected by this change.
- Gate, test list and every flagged function: in the Graphify check.
Reviewed 6054d20 · 10 earlier findings not re-checked · Full report in the Graphify check
source_end_location and definition_end_location sit next to source_location and definition_location in the same L<n> form, instead of as integers under metadata.
AST cache schema moves to 50 on top of v8's 49.
AST cache schema moves to 52 on top of v8's 51.
Keep v8's underscore-salted ObjC method ids and pass ast_node through them. AST cache schema moves to 54 on top of v8's 53.
| impl_stem = _objc_category_base_stem(stem) if _objc_is_category(node) else stem | ||
| impl_nid = _make_id(impl_stem, name) | ||
| if impl_nid not in seen_ids: | ||
| add_node(impl_nid, name, line, _CALLABLE_CLASS) | ||
| add_edge(file_nid, impl_nid, "contains", line) | ||
| add_node(impl_nid, name, line, _CALLABLE_CLASS, ast_node=node) | ||
| add_edge(file_nid, impl_nid, "contains", line) |
There was a problem hiding this comment.
Objective-C classes get duplicate containment edges
A file containing both @interface and @implementation for a class now emits two contains edges from the file to the same class node.
Emit the implementation contains edge only when it is not already present in graphify/extractors/objc.py.
Evidence · checked against the code
Expected: The extraction result has one file-to-class contains edge.
Actually: Both AST branches append a contains edge to the same class ID, producing two edges.
- Independent source review: Checked the declaration/implementation merge and its edge deduplication. That cleanup runs only when the merge drops duplicate nodes; a same-file interface and implementation share the one node created by
add_node, so this cleanup does not eliminate the two edges. Existing Objective-C tests cover extraction behavior but do not assert containment-edge uniqueness.
Prompt to fix with AI
In graphify/extractors/objc.py, extract_objc (lines 635-638):
Problem: Objective-C classes get duplicate containment edges
Expected: The extraction result has one file-to-class `contains` edge.
Actually: Both AST branches append a `contains` edge to the same class ID, producing two edges.
Fix it at the cause, and add a test that fails without the fix.
Graphify · Checked against the code · View in Graphify
There was a problem hiding this comment.
Fixed in e57142b: the @implementation still records its definition site, but the contains edge is added only when the node is new. Covered by test_objc_interface_and_implementation_in_one_file_keep_one_contains_edge.
…implementation in one file add_node runs for the @implementation to record its definition site, but the contains edge is added only when the node is new.
|
We need this as well. We use graphify's graph to cut source files down to single functions, and since the graph gives only the line where a declaration starts, we compute the end line ourselves from the same tree-sitter grammars. With the end defined as in this PR (the inclusive last line of the declaration's node), we checked 3,891 functions in 24 apps (TypeScript, TSX, JavaScript, Python, Go, Java, C#, PHP, Ruby, Rust): in every case the lines from start to end hold exactly that declaration in the syntax tree, and the cut pieces put back together equal the file byte for byte. We have read this branch but not run it. If it is merged we would drop our own computation and read Two cases that needed care on our side, in case they are useful as tests: several functions that start on the same line (minified JavaScript, 342 of 422 functions in our sample), and declarations without a body (interface and abstract method signatures in Java and C#). |
…ess ones Minified JavaScript with several functions on one line, and Java/C# interface and abstract methods whose signature spans lines.
|
Thanks, that is useful to have checked against a corpus this size. Both cases already hold on this branch, and they are now pinned as tests in dfe1bf8:
Rust uses its own extractor, which this PR does not change, so it keeps the start-only form for now. It would fit a separate follow-up. |
_node_location takes the site to read, so a merged header declaration shows its implementation as L<start>-L<end> too.
AST declaration nodes expose a starting line but no ending line. Add optional
source_end_location, the 1-based inclusive end line derived from the declaration AST, in the same form as the existingsource_location("L42"/"L58"), which is unchanged. An exclusive tree-sitter end at column zero maps to the preceding line.Language coverage. Languages extracted by the shared engine get ranges from its declaration paths: TypeScript, JavaScript, Python, Java, Kotlin, C#, C, C++, PHP, Ruby, Scala, Swift and Lua. The dedicated Go and Objective-C extractors are covered too. Languages with their own separate extractors, such as Rust and Dart, are not changed here and keep the start-only form.
graphify queryNODE lines, MCPget_nodeandgraphify explainprint the range asL42-L58when a declaration records one. One-line declarations and nodes without a known range keep the start-only form. Skill and agent instruction text is unchanged.Measured effect
The motivation is in #4964. Measured with Claude Code subagents (Sonnet) on a graph of graphify's own
graphify/package: 6 questions, each about one 30-90 line function in a 3,000-13,000 line file, 2 runs per question per condition (12 runs each), with no change to the agent's instructions.v8)With the range printed, the agent usually reads the function once; total input tokens per run fall by about 14%, almost entirely from the turns it no longer spends re-reading. All answers were correct in every condition. Token counts are raw (no prompt-cache discount, so the cost saving is smaller), and the sample is small and limited to one model and one host.
Details
For Objective-C, header/implementation merges retain the declaration range and store
definition_end_locationbeside the implementation'sdefinition_file/definition_location. Duplicate records backfill a range only when the corresponding file/start pair matches. C++ initializers and pure-virtual specifiers are included without extending one declarator into the next.Objective-C header merging preserves an existing complete implementation site when a class extension supplies the source declaration. A missing implementation end does not borrow the declaration end.
File nodes, reference stubs, synthetic owners and Objective-C macro enums reconstructed from isolated fragments omit unknown ranges. AST cache schema advances from upstream's 53 to 54, invalidating results that predate declaration ranges.
Both fields survive build, export and global-graph merging. Existing graphs need extraction with the new code to obtain ranges; merging alone does not calculate missing ranges.
Validation
Against v8
340268a7(0.9.112), merged on 2026-11-18, then re-merged with v8013a6461(0.9.118) on 2026-11-24, in the same local macOS environment for both runs:v8and on this branch: the branch's failures are a subset ofv8's (23 vs 24; the extra one is a network-dependentuvinstall test) and the 133 setup errors are identical (localsetuptoolstoo old to build a wheel). No new failures.tests/test_declaration_end_line.py: 53 passed, including the new range-printing case.anyioin this local environment and fail the same way on plain v8.python -m ruff check .,python -m tools.skillgen --checkandgit diff --check: passed.Closes #4964