Skip to content

Complexity metrics (cognitive/cyclomatic/Halstead) missing for 24 of 34 supported languages #1923

Closed

Description

@carlos-alm

Summary

codegraph complexity --file <f> --health -T --json silently returns functions: [] for any file in a language outside a small supported subset — not because those files lack functions (codegraph where --file extracts them correctly), but because COMPLEXITY_RULES/HALSTEAD_RULES (src/ast-analysis/rules/index.ts) and the native mirror (lang_rules/halstead_rules in crates/codegraph-core/src/ast_analysis/complexity.rs) simply have no entry for the language. This is the same root-cause shape as #1782 (Lua), just unfixed for the remaining languages.

Confirmed via direct repro (native engine) for a spot-check of 4 languages, all returning functions: 0 despite the fixtures having real functions:

$ codegraph complexity --file tests/benchmarks/resolution/fixtures/elixir/main.ex --health -T --json --engine native
functions: 0
$ codegraph complexity --file tests/benchmarks/resolution/fixtures/zig/main.zig --health -T --json --engine native
functions: 0
$ codegraph complexity --file tests/benchmarks/resolution/fixtures/gleam/main.gleam --health -T --json --engine native
functions: 0
$ codegraph complexity --file tests/benchmarks/resolution/fixtures/clojure/main.clj --health -T --json --engine native
functions: 0

Exact scope (post-#1782, i.e. after Lua is fixed)

LANGUAGE_REGISTRY has 36 entries (35 real languages + intentionally-excluded hcl, which has no function/branch concept and should stay excluded).

TS COMPLEXITY_RULES/HALSTEAD_RULES supported (11): javascript, typescript, tsx, python, go, rust, java, csharp, ruby, php, lua

TS missing (24): c, cpp, kotlin, swift, scala, bash, elixir, dart, zig, haskell, ocaml, ocaml-interface, fsharp, fsharp-signature, gleam, clojure, julia, r, erlang, solidity, objc, cuda, groovy, verilog

Native Rust lang_rules/halstead_rules supported (16, post-#1782): javascript, typescript, tsx, python, go, rust, java, csharp, ruby, php, c, cpp, kotlin, swift, scala, bash, lua

Native missing (17): dart, zig, haskell, ocaml, ocaml-interface, fsharp, fsharp-signature, gleam, clojure, julia, r, erlang, solidity, objc, cuda, groovy, verilog

So there are two tiers of follow-up work:

  1. 6 languages (c, cpp, kotlin, swift, scala, bash) already have a native LangRules/HalsteadRules definition (defined in complexity.rs but never wired into the WASM/TS side) — porting these to src/ast-analysis/rules/*.ts should be a comparatively mechanical translation of the existing Rust rule struct, plus wiring into COMPLEXITY_RULES/HALSTEAD_RULES.
  2. 17 languages have no complexity model on either engine and need the same from-scratch grammar investigation codegraph complexity --file returns empty functions[] for Lua files #1782 required for Lua: confirming real tree-sitter node types for branch/loop/logical-operator/function constructs (do not assume node names transfer from a similar-looking language), figuring out the if/elseif/else structural pattern (flat siblings vs nested vs alternative-field), and — notably — several of these are functional/pattern-matching languages (Haskell, OCaml, F#, Clojure, Erlang, Gleam) whose control flow doesn't map cleanly onto the existing branch/nesting/cyclomatic model built around imperative if/for/while constructs. Those will need real design thought (e.g. what counts as a "branch" for a case/pattern-match expression with N clauses, or for guards), not just a config port.

Suggested approach

Not a single PR — likely one PR per language or small batch, mirroring how dataflow rules were rolled out in batches (P5 Batch B1–B5, see src/ast-analysis/rules/b2.ts–b5.ts). Tier 1 (c/cpp/kotlin/swift/scala/bash) is lower-risk and could go first since the native rule shape already exists as a reference. Tier 2 languages should be tackled individually or in small groups given the design work needed for non-imperative control flow.

Scope note

Not fixed as part of #1782 — that issue was scoped specifically to Lua. Filing this as the tracking issue for the broader gap per repo scope-discipline convention (task instructions for #1782 explicitly called for filing a follow-up here rather than expanding that PR into a 24-language audit).

Activity

  1. added
    bugSomething isn't working
    follow-upDeferred work from PR reviews that needs tracking
    titan-auditIssues discovered during Titan audit
    on Jul 6, 2026
  2. carlos-alm commented on Jul 30, 2026

    @carlos-alm
    ContributorAuthor

    Progress update from a /fixer pass on this issue:

    Tier 1 (c, cpp, kotlin, swift, scala, bash) is already fully done — confirmed on current main: all six are wired into both COMPLEXITY_RULES/HALSTEAD_RULES (TS) and lang_rules()/halstead_rules() (native), matching this issue's own tier-1 description exactly.

    Attempted Dart next (first tier-2 language, chosen as the most mechanical/lowest-risk per this issue's suggested approach) — investigated tree-sitter-dart's grammar in depth (branch/loop/logical-operator/case node types, operator leaf shapes for Halstead, if_statement's alternative-field else-if pattern) and had complexity/Halstead rules mirrored in both engines and passing my own hand-verified test cases, but discovered a blocking, pre-existing bug before landing it: function_signature/method_signature never contains the function's own body — function_body is an adjacent sibling, not a child, for both top-level functions and class methods. This breaks the shared engine's function-scope tracking (confirmed for both the standalone DFS reference and the visitor-based batch path used during codegraph build), not just something my new rules could work around. Filed as #2182 with full evidence and a suggested fix direction, since it's foundational (likely also affects the already-existing dataflowDart config, unverified) rather than specific to complexity.

    Reverted the Dart wiring rather than land rules that would silently produce wrong data (every function reporting the trivial base complexity regardless of actual branching) — landing "supported" but broken is worse than not supporting it yet.

    Recommendation for whoever picks up Dart next: fix #2182 first, then the complexity/Halstead rules I derived (branch/case/logical-operator/optional-chain node types, Halstead operator/operand leaf sets) should be usable close to as-is — the grammar investigation is the expensive part and is now done, just not yet landed.

    No change to the remaining 16 languages' status.

  3. carlos-alm commented on Aug 3, 2026

    @carlos-alm
    ContributorAuthor

    Progress update from a follow-up /fixer pass on this issue:

    Objective-C (tier 2) is now done — merged in #2233. tree-sitter-objc extends tree-sitter-c: if/else/for/while/switch/case/logical-operator/conditional-expression node kinds are byte-identical to plain C (confirmed by parsing sample ObjC control flow and inspecting the S-expression), including the same else_clause wrapper (Pattern A). Wired COMPLEXITY_RULES/HALSTEAD_RULES (TS) and lang_rules()/halstead_rules() (native) for objc, extending C's rule shape with method_definition in functionNodes and catch_clause (from @try/@catch/@finally) as a branch/nesting node — the same treatment C++'s catch_clause already gets. message_expression/selector_expression are treated as compound Halstead operators, mirroring call_expression.

    While verifying native/WASM parity for this, found and fixed a real divergence: the native ObjC extractor's handle_method ran complexity/CFG analysis unconditionally on both method_declaration (an @interface/@protocol signature, which never has a body) and method_definition, producing a spurious trivial complexity entry that duplicated the real method entry under the same dotted name. Gated both on actual body presence and set bodyless accordingly on both engines. Verified native and WASM now produce byte-identical codegraph complexity --health --json output on a hand-built fixture covering if/else-if/else, classic and fast-enumeration for loops, switch/case, @try/@catch/@finally, ternary, and message sends.

    No change to tier-1 (c/cpp/kotlin/swift/scala/bash) or CUDA — all already confirmed done on current main.

    Remaining unattempted (16, minus Dart which is blocked on #2182 — do not attempt until that's fixed): zig, haskell, ocaml, ocaml-interface, fsharp, fsharp-signature, gleam, clojure, julia, r, erlang, solidity, groovy, verilog.

  4. carlos-alm commented on Aug 7, 2026

    @carlos-alm
    ContributorAuthor

    Progress update from a /fixer pass on this issue:

    R and Groovy (tier 2) are now done — PR #2313. Both confirmed via direct grammar investigation rather than assumption: R's if_statement carries consequence/alternative fields directly (Pattern C, no else_clause wrapper), has no switch statement (switch(x, ...) is an ordinary function call), and treats repeat as an unconditional loop (same as Rust's loop). Groovy extends tree-sitter-java's grammar almost byte-for-byte; closure is deliberately excluded from functionNodes since this grammar reuses it ambiguously for plain if/for/while/switch block bodies as well as real closures — including it would fabricate a spurious extra "function" per such body.

    Julia and Solidity turned out to need an engine enhancement, not a rules port — filed as #2312 rather than expanding this PR's scope. Julia wraps every operator (+, >, &&, ...) in one generic operator node type, indistinguishable except by .text, which the current classifyLogicalOp/is_logical_op node-type matching can't handle. Solidity wraps every statement/expression in a generic statement/expression node, and both the "then" and "else-if"/"else" branches of if_statement share the same field name body (no alternative field, no else_clause wrapper) — breaking both else-if detection patterns the engine currently supports.

    Remaining unattempted: dart (blocked on #2182), haskell, ocaml, ocaml-interface, fsharp, fsharp-signature, gleam, clojure, erlang (all flagged in #2312 as needing real design thought for pattern-match/guard "branch" semantics), verilog (no existing dataflow-rule investigation to build on, needs from-scratch grammar work), plus julia/solidity pending the #2312 engine fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfollow-upDeferred work from PR reviews that needs trackingtitan-auditIssues discovered during Titan audit

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions