Skip to content

fix(kotlin): silent import edge drop due to grammar node type mismatch - #2531

Closed
mustaqeem66 wants to merge 1 commit into
Graphify-Labs:v8from
mustaqeem66:fix/kotlin-imports-silent-drop
Closed

mustaqeem66 wants to merge 1 commit into
Graphify-Labs:v8from
mustaqeem66:fix/kotlin-imports-silent-drop

Conversation

@mustaqeem66

@mustaqeem66 mustaqeem66 commented Aug 7, 2026 •

Copy link
Copy Markdown

Summary

This PR fixes a silent data loss bug where 100% of Kotlin import edges were dropped during extraction, reducing cross-file dependency recall from ~45% to 0% for Kotlin code.

Root Cause Analysis

The bundled tree_sitter_kotlin grammar (PyPI tree_sitter_kotlin v1.1.0) emits import node types with qualified_identifier children. However:

  1. _KOTLIN_CONFIG only declared import_types=frozenset({"import_header"}) — the import node type was never matched, so _import_kotlin was never invoked.

  2. _import_kotlin fallback only checked for identifier children, but the grammar produces qualified_identifier (e.g., kotlinx.coroutines.delay → qualified_identifier with children identifier, ., identifier, ., identifier).

This meant zero import edges were produced for any Kotlin file using the current grammar.

Changes Made

1. Updated _KOTLIN_CONFIG (graphify/extract.py:891)

# Before
import_types=frozenset({"import_header"}),

# After  
import_types=frozenset({"import_header", "import"}),

Accepts both grammar generations (older forks emit import_header, current PyPI grammar emits import).

2. Enhanced _import_kotlin fallback (graphify/extract.py:628-629)

# Before
if child.type == "identifier":
    raw = _read_text(child, source)

# After
if child.type in ("identifier", "qualified_identifier"):
    raw = _read_text(child, source).split(".")[-1].strip()
  • Handles both identifier and qualified_identifier child types
  • Extracts the last segment from qualified paths (e.g., kotlinx.coroutines.delay → delay)

Reproducer

Before fix:

$ graphify extract . --code-only
# 4 nodes, 2 edges (only contains edges)

After fix:

$ graphify extract . --code-only  
# 4 nodes, 3 edges (contains + imports edge from Ledger.kt → Money.kt)

Measured Impact

Tested on a 416-file Kotlin/Spring modular monolith:

Metric Before (0.9.35) After (this fix)
Kotlin imports edges 0 1,322
Cross-file dep recall 45.3% (232/512) 94.7% (485/512)
Nodes 8,097 11,752
Edges 11,193 16,158

The 45.3% baseline came entirely from calls/references heuristics. The dependencies missed were the most critical ones — shared value types (Money, SkuId, Currency) and constructor-injected repositories that define Spring application architecture.

Testing

  • All 20 existing Kotlin tests pass
  • All 318 language tests pass
  • 567 cross-language/build/merge tests pass
  • Manual verification with minimal reproducer confirms imports edge creation

Related Issues

This is the Kotlin analogue of:

But with total loss rather than partial — not a single Kotlin import edge survived.

Follow-up

Wildcard imports (import com.example.*) currently resolve to "*" and produce no edge. This is a separate issue tracked independently.


Closes #2526

_KOTLIN_CONFIG declared import_types=frozenset({'import_header'}) but the
bundled tree_sitter_kotlin grammar emits 'import' nodes with
qualified_identifier children. This caused _import_kotlin to never be
invoked, silently dropping 100% of Kotlin import edges.

Fix: accept both 'import_header' and 'import' node types, and handle
qualified_identifier in the fallback by extracting the last path segment.

Impact: Cross-file dependency recall improves from ~45% to ~95% on real
Kotlin/Spring codebases (1,322 imports edges vs 0 before).

Closes Graphify-Labs#2526

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


Graphify review — findings

This PR modifies the Kotlin import handling in graphify/extract.py. The changes update the fallback branch of _import_kotlin to also match qualified_identifier child nodes (in addition to identifier) and to take the last dot-separated segment, and they add "import" to the set of recognized import node types in _KOTLIN_CONFIG. A blank line between two config definitions was also removed. Note: the diff only touches _import_kotlin and _KOTLIN_CONFIG, which differs from the long list of changed symbols provided—reviewers may want to confirm the stated surface area matches the actual diff.

Worth a look

  • Kotlin import fallback strips full package path to last segment, breaking import target id — graphify/extract.py:628 · 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 — 1282 functions depend on the 189 functions this change touches.

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

  • extract() — 370 callers, 39 callees (high)
  • _rebuild_code() — 93 callers, 52 callees (high)
  • extract_xaml() — 19 callers, 17 callees (high)
  • dispatch_command() — 2 callers, 118 callees (high)
  • _get_extractor() — 24 callers, 6 callees (high)
  • run_pipeline() — 8 callers, 13 callees (high)
  • collect_files() — 17 callers, 6 callees (high)
  • extract_vue() — 10 callers, 6 callees (high)
  • …and 2 more

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

· 1 grounded finding(s) anchored inline below.

Comment thread graphify/extract.py
Comment on lines +629 to 631
raw = _read_text(child, source).split(".")[-1].strip()
tgt_nid = _make_id(raw)
edges.append({

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Kotlin import fallback strips full package path to last segment, breaking import target id — agreed by 2 of 2 members but NOT verified (no proof, no reproducing execution) — consensus is not a verdict; needs human review

Graphify suggests a fix:

Suggested change
raw = _read_text(child, source).split(".")[-1].strip()
tgt_nid = _make_id(raw)
edges.append({
raw = _read_text(child, source).strip()
if not raw:
break
tgt_nid = _make_id(raw)
edges.append({

safishamsi added a commit that referenced this pull request Aug 8, 2026
, #2526, #2550, #2551)

TypeScript (#2553, #2552, thanks @Earthfreedom):
- _resolve_typescript_member_calls matched a receiver type by name alone
  and emitted EXTRACTED, so a third-party import could bind to an unrelated
  local class of the same name. It now requires the matched type to be
  same-file or imported by the caller's file, and tiers table-inferred
  receivers to INFERRED.
- calls inside a callback passed to another call (const h = wrapper(arrow))
  were never walked; the callback body is now walked and attributed to the
  declaration, through the same import-gated resolution so it cannot
  fabricate edges. The #2553 gate lands with #2552 by design.

Kotlin (#2526, #2550, #2551; adapts PR #2531, thanks @mustaqeem66;
reports from @spaceBrownie and @thomasrengot-hub):
- match the bundled tree-sitter-kotlin 1.1.0 import node and resolve each
  import to the real target node (imports were silently dropped), so
  genuine calls promote to EXTRACTED via import evidence;
- a fully-qualified call com.example.Foo.bar() now produces a calls edge;
- a file the grammar cannot fully parse (e.g. one-line class C { val x })
  now warns instead of silently extracting nothing, and declarations
  recovered inside an error span keep their enclosing class.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.37 (graphifyy==0.9.37). Your import node-type fix is the basis of the #2526 fix; adapted with target resolution (so import edges point at the real node instead of dangling), wildcard/alias handling, and the FQ-call and one-line-body fixes (#2550, #2551). Credited in the release notes. Thanks @mustaqeem66!

@safishamsi safishamsi closed this Aug 8, 2026
PerryLin-0420 pushed a commit to PerryLin-0420/graphify-decouple that referenced this pull request Aug 13, 2026
…Labs#2553, Graphify-Labs#2552, Graphify-Labs#2526, Graphify-Labs#2550, Graphify-Labs#2551)

TypeScript (Graphify-Labs#2553, Graphify-Labs#2552, thanks @Earthfreedom):
- _resolve_typescript_member_calls matched a receiver type by name alone
  and emitted EXTRACTED, so a third-party import could bind to an unrelated
  local class of the same name. It now requires the matched type to be
  same-file or imported by the caller's file, and tiers table-inferred
  receivers to INFERRED.
- calls inside a callback passed to another call (const h = wrapper(arrow))
  were never walked; the callback body is now walked and attributed to the
  declaration, through the same import-gated resolution so it cannot
  fabricate edges. The Graphify-Labs#2553 gate lands with Graphify-Labs#2552 by design.

Kotlin (Graphify-Labs#2526, Graphify-Labs#2550, Graphify-Labs#2551; adapts PR Graphify-Labs#2531, thanks @mustaqeem66;
reports from @spaceBrownie and @thomasrengot-hub):
- match the bundled tree-sitter-kotlin 1.1.0 import node and resolve each
  import to the real target node (imports were silently dropped), so
  genuine calls promote to EXTRACTED via import evidence;
- a fully-qualified call com.example.Foo.bar() now produces a calls edge;
- a file the grammar cannot fully parse (e.g. one-line class C { val x })
  now warns instead of silently extracting nothing, and declarations
  recovered inside an error span keep their enclosing class.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants