Skip to content

feat(extract): preserve declaration end lines with source provenance - #4968

Open
xiongjianxu wants to merge 13 commits into
Graphify-Labs:v8from
xiongjianxu:fix/declaration-end-line
Open

xiongjianxu wants to merge 13 commits into
Graphify-Labs:v8from
xiongjianxu:fix/declaration-end-line

Conversation

@xiongjianxu

@xiongjianxu xiongjianxu commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

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 existing source_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 query NODE lines, MCP get_node and graphify explain print the range as L42-L58 when 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.

Condition Mean tokens / run Model turns Source-read calls Runs that re-read
start line only (current v8) 94.6K 60 21 8 / 12
range printed (this PR) 81.1K 52 14 2 / 12

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_location beside the implementation's definition_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 v8 013a6461 (0.9.118) on 2026-11-24, in the same local macOS environment for both runs:

  • Full suite on plain v8 and on this branch: the branch's failures are a subset of v8's (23 vs 24; the extra one is a network-dependent uv install test) and the 133 setup errors are identical (local setuptools too old to build a wheel). No new failures.
  • tests/test_declaration_end_line.py: 53 passed, including the new range-printing case.
  • 249 related test files (extractors for each language, dedup, build, query/serve, global graph): 4,975 passed (279 files) after the 0.9.118 merge; the only 2 failures are the hash-seed query tests, which spawn a subprocess that cannot import anyio in this local environment and fail the same way on plain v8.
  • python -m ruff check ., python -m tools.skillgen --check and git diff --check: passed.

Closes #4964

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Nov 3, 2026
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
@xiongjianxu
xiongjianxu force-pushed the fix/declaration-end-line branch from 4911d8e to c3e2bc6 Compare November 3, 2026 02:48

@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; 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-safety
  • tests/test_4874_jvm_resolution.py — impact, full-run-safety
  • tests/test_4909_reference_binding_scale.py — impact, full-run-safety
  • tests/test_affected_cli.py — full-run-safety
  • tests/test_affected_defines.py — full-run-safety
  • tests/test_affected_member_seed.py — full-run-safety
  • tests/test_affected_overrides.py — full-run-safety
  • tests/test_affected_path_file_node.py — full-run-safety
  • tests/test_affected_seed_reference_forms.py — full-run-safety
  • tests/test_agents_platform.py — full-run-safety
  • tests/test_alias_folding.py — impact, full-run-safety
  • tests/test_analyze.py — impact, full-run-safety
  • tests/test_anthropic_custom_endpoint.py — full-run-safety
  • tests/test_antigravity_install.py — full-run-safety
  • tests/test_apm_fallback_version.py — full-run-safety
  • tests/test_architecture_doc.py — full-run-safety
  • tests/test_astro_extraction.py — impact, full-run-safety
  • tests/test_astro_import_ids.py — impact, full-run-safety
  • tests/test_atomic_canvas_export.py — full-run-safety
  • tests/test_atomic_version_stamp.py — full-run-safety
  • tests/test_atomic_writes.py — impact, full-run-safety
  • tests/test_backend_env_isolation.py — full-run-safety
  • tests/test_backend_extras.py — full-run-safety
  • tests/test_backend_opt_in.py — impact, full-run-safety
  • tests/test_benchmark.py — impact, full-run-safety
  • tests/test_benchmark_raw_graph.py — impact, full-run-safety
  • tests/test_benchmark_real_query.py — impact, full-run-safety
  • tests/test_binary_media_words.py — impact, full-run-safety
  • tests/test_blade_extractor.py — full-run-safety
  • tests/test_build.py — impact, full-run-safety
  • tests/test_build_located_semantic_identity.py — impact, full-run-safety
  • tests/test_build_merge_dedup_scope.py — impact, full-run-safety
  • tests/test_build_merge_hyperedges_and_prune.py — impact, full-run-safety
  • tests/test_build_merge_shrink_guard.py — impact, full-run-safety
  • tests/test_builtin_global_type_refs.py — full-run-safety
  • tests/test_c_include_resolution.py — impact, full-run-safety
  • tests/test_cache.py — impact, full-run-safety
  • tests/test_cache_long_paths.py — impact, full-run-safety
  • tests/test_cache_stale_import_target.py — impact, full-run-safety
  • tests/test_cache_windows_case_4925.py — impact, full-run-safety
  • tests/test_callflow_html.py — full-run-safety
  • tests/test_cargo_introspect.py — full-run-safety
  • tests/test_cargo_missing_manifest.py — full-run-safety
  • tests/test_carried_hyperedge_remap.py — impact, full-run-safety
  • tests/test_case_only_decl_ids_4904.py — full-run-safety
  • tests/test_case_sensitive_resolution.py — full-run-safety
  • tests/test_charmap_encoding.py — impact, full-run-safety
  • tests/test_chunking.py — impact, full-run-safety
  • tests/test_cjs_module_extension.py — impact, full-run-safety
  • tests/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)

· 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):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Health regression — 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.

@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 review View in Graphify

Nothing blocking · 9 possible issues to check · Merge confidence 5/5

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

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.
Comment thread graphify/extractors/objc.py Outdated
Comment on lines +635 to +638
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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2  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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@YotamPeled

Copy link
Copy Markdown

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 source_end_location from the graph. Rust is the one language in our set that this PR leaves in the start-only form; for us it would be worth a follow-up.

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.
@xiongjianxu

Copy link
Copy Markdown
Contributor Author

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:

  • Several functions on one line (function a(){...}function b(){...}const c=()=>3;): each declaration gets L1/L1.
  • Declarations without a body: a Java interface method whose signature spans two lines gets L2-L3, a one-line one L4, and an abstract method L7-L8; the C# interface and abstract methods behave the same way.

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.

Comment thread graphify/serve.py Resolved
_node_location takes the site to read, so a merged header declaration
shows its implementation as L<start>-L<end> too.

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.

AST declaration nodes expose a start line but no matching declaration end line

2 participants