Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

refactor(health): split complexity walker into focused modules (#557)

Raghav Chamadiya4da360a2ef14
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/error_handling.py b/packages/core/src/repowise/core/analysis/health/biomarkers/error_handling.py
index f027d62db..9505ed5da 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/error_handling.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/error_handling.py
@@ -14,7 +14,7 @@ ships in its own ``error_handling`` category with an advisory 0.5 cap and
a floored 0.5 weight, and is excluded from the defect-calibration roster.
Detection happens in the complexity walker's whole-tree pass (see
-``complexity.walker._collect_error_handling``); this detector just lifts
+``complexity.error_handling._collect_error_handling``); this detector just lifts
the pre-collected hits into findings. Precision-first: unsupported
languages and parse failures yield zero hits, never a false positive.
"""
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/io_in_loop.py b/packages/core/src/repowise/core/analysis/health/biomarkers/io_in_loop.py
index 2bab43fe7..796ded42d 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/io_in_loop.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/io_in_loop.py
@@ -16,7 +16,7 @@ Two cases share this detector and the same ``performance`` budget:
This is a *high-precision, low-recall* signal by design (measured 79% on the
Phase-0 gate, CI 68-87). The precision comes from three constraints enforced
-upstream in the walker's perf pass (``complexity.walker._collect_perf_hits``)
+upstream in the walker's perf pass (``complexity.perf_walk._collect_perf_hits``)
and the boundary classifier (``perf.io_boundaries``):
1. **Loop-body scoping** — only calls under a loop's ``body`` field count; a
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/nested_loop_with_io.py b/packages/core/src/repowise/core/analysis/health/biomarkers/nested_loop_with_io.py
index 1acd875e1..c718e70b1 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/nested_loop_with_io.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/nested_loop_with_io.py
@@ -7,7 +7,7 @@ almost never incidental, so this rides alongside ``io_in_loop`` un-gated (it doe
NOT need the centrality gate the bare O(n^2) marker does).
A ``performance`` dimension signal. This detector lifts the pre-collected,
-loop-depth-scoped hits (``complexity.walker._collect_perf_hits``, emitted only at
+loop-depth-scoped hits (``complexity.perf_walk._collect_perf_hits``, emitted only at
``loop_depth >= 2``) into findings — unsupported languages yield nothing.
"""
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/README.md b/packages/core/src/repowise/core/analysis/health/complexity/README.md
index dbbdd3aaa..6736c8441 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/README.md
+++ b/packages/core/src/repowise/core/analysis/health/complexity/README.md
@@ -2,6 +2,27 @@
Tree-sitter AST walker. Single AST pass per file computes:
+> **Module layout.** `walker.py` is a thin orchestrator: it parses the source
+> once and drives the passes, each of which lives in its own module.
+>
+> | Module | Responsibility |
+> |--------|----------------|
+> | `walker.py` | Orchestration (`walk_file` / `walk_file_complexity`) + public re-exports |
+> | `models.py` | Output dataclasses (`FunctionComplexity`, `ClassComplexity`, `PerfHit`, ...) |
+> | `ast_utils.py` | Name/text helpers, function-node collection, parameter counting |
+> | `nloc.py` | Non-blank / non-comment line counting |
+> | `cyclomatic.py` | The CCN / cognitive / max-nesting engine (`_walk_function_body`) |
+> | `assertions.py` | Assertion-block detection (test-quality smells) |
+> | `error_handling.py` | Error-handling anti-pattern detection (`_collect_error_handling`) |
+> | `perf_walk.py` | Performance-risk pass (`_collect_perf_hits`) |
+> | `class_analysis.py` | Class-level LCOM4 / god-class metrics (`_compute_lcom4`) |
+> | `languages.py` | Per-language tree-sitter `LanguageNodeMap` registry (extension point) |
+>
+> The package `__init__` re-exports the dataclasses plus `walk_file` /
+> `walk_file_complexity`, so importers use
+> `from repowise.core.analysis.health.complexity import ...` and are unaffected
+> by the internal split.
+
- **CCN** — McCabe cyclomatic complexity. Counts branching constructs
(`if`, `for`, `while`, `case`, `catch`) plus boolean operators
(`&&`, `||`).
@@ -121,7 +142,7 @@ fields on its `LanguageNodeMap` (all default to empty = opt-out):
field reads and method calls).
The receiver and member-name children are extracted by a generic
-field-name probe (`walker._self_member_name`), which tries the
+field-name probe (`class_analysis._self_member_name`), which tries the
`object`/`value`/`argument`/`expression` (receiver) and
`property`/`attribute`/`field`/`name` (member) fields and then falls back
to positional children — so most tree-sitter grammars need only the
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/assertions.py b/packages/core/src/repowise/core/analysis/health/complexity/assertions.py
new file mode 100644
index 000000000..246e35d86
--- /dev/null
+++ b/packages/core/src/repowise/core/analysis/health/complexity/assertions.py
@@ -0,0 +1,131 @@
+"""Assertion-block detection (test-quality smells).
+
+Finds runs of ≥2 consecutive assertion statements within a function body,
+recorded as ``(start_line, end_line, count)``. Opt-in per language via the
+``LanguageNodeMap`` ``assert_kinds`` / ``assert_call_kinds`` fields; a language
+that maps neither produces nothing (never a false positive). Consumed by the
+``large_assertion_block`` / ``duplicated_assertion_block`` biomarkers.
+"""
+
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+from .ast_utils import _IDENTIFIER_SUFFIX
+from .languages import LanguageNodeMap
+
+if TYPE_CHECKING:
+ from tree_sitter import Node
+
+# Callee-name prefixes that mark a call as a test assertion. Matched
+# case-insensitively against every identifier in the call's callee chain,
+# so ``assertEqual`` / ``assert_eq`` / ``Assert.assertTrue`` / ``expect``
+# all qualify. Deliberately small — these two prefixes cover xUnit-family
+# (``assert*``) and the BDD/expect family (``expect(...)``).
+_ASSERT_CALL_PREFIXES = ("assert", "expect")
+_EXPRESSION_STATEMENT = "expression_statement"
+_AWAIT_WRAPPER_KINDS = ("await_expression", "await", "parenthesized_expression")
+
+
+def _callee_matches_assert(call_node: Node) -> bool:
+ """True if any identifier in *call_node*'s callee chain is assert-ish.
+
+ Only the callee (the ``function`` / ``macro`` field) is inspected, not
+ the arguments — so ``foo(assertion)`` does not match while
+ ``expect(x).toBe(y)`` and ``self.assertEqual(...)`` do.
+ """
+ callee = call_node.child_by_field_name("function") or call_node.child_by_field_name("macro")
+ # Fallback when no ``function``/``macro`` field is exposed: the first
+ # named child is usually the callee.
+ roots = [callee] if callee is not None else [c for c in call_node.children if c.is_named][:1]
+ stack: list[Node] = list(roots)
+ while stack:
+ node = stack.pop()
+ if node.type.endswith(_IDENTIFIER_SUFFIX) and node.text is not None:
+ name = node.text.decode("utf-8", errors="replace").lower()
+ if any(name.startswith(p) for p in _ASSERT_CALL_PREFIXES):
+ return True
+ for child in node.children:
+ stack.append(child)
+ return False
+
+
+def _find_assert_call(stmt: Node, kinds: frozenset[str]) -> Node | None:
+ """Find an assertion-call node that is *stmt*'s own expression.
+
+ Searches direct named children and one level deeper (to see through
+ ``await`` / parenthesis wrappers) — but no further, so a call buried in
+ an argument or a nested block is not mistaken for the statement's
+ expression.
+ """
+ for child in stmt.children:
+ if not child.is_named:
+ continue
+ if child.type in kinds:
+ return child
+ if child.type in _AWAIT_WRAPPER_KINDS:
+ for gc in child.children:
+ if gc.is_named and gc.type in kinds:
+ return gc
+ return None
+
+
+def _is_assertion_statement(stmt: Node, lmap: LanguageNodeMap) -> bool:
+ """True if *stmt* is a test assertion (bare ``assert`` or assert call)."""
+ if stmt.type in lmap.assert_kinds:
+ return True
+ if not lmap.assert_call_kinds:
+ return False
+ # Some grammars (Kotlin) have no ``expression_statement`` wrapper — the
+ # call node sits directly in the statement list. Match it as the
+ # statement itself. (Wrapper languages never hit this: their call nodes
+ # only ever appear as the single child of an ``expression_statement``,
+ # so they can't form a run of ≥2 at this level.)
+ if stmt.type in lmap.assert_call_kinds:
+ return _callee_matches_assert(stmt)
+ if stmt.type != _EXPRESSION_STATEMENT:
+ return False
+ call = _find_assert_call(stmt, lmap.assert_call_kinds)
+ return call is not None and _callee_matches_assert(call)
+
+
+def _collect_assertion_blocks(body_node: Node, lmap: LanguageNodeMap) -> list[tuple[int, int, int]]:
+ """Runs of ≥2 consecutive assertion statements within a function body.
+
+ Each run is recorded as ``(start_line, end_line, count)``. Runs are
+ found per statement-list (a block's direct children), so an assertion
+ sequence broken by a non-assertion statement starts a new run. Nested
+ function bodies are skipped — their assertions belong to them.
+ """
+ if not lmap.assert_kinds and not lmap.assert_call_kinds:
+ return []
+ blocks: list[tuple[int, int, int]] = []
+
+ def _scan_siblings(parent: Node) -> None:
+ run_start = 0
+ run_end = 0
+ run_count = 0
+ for child in parent.children:
+ if not child.is_named:
+ continue
+ if _is_assertion_statement(child, lmap):
+ if run_count == 0:
+ run_start = child.start_point[0] + 1
+ run_end = child.end_point[0] + 1
+ run_count += 1
+ else:
+ if run_count >= 2:
+ blocks.append((run_start, run_end, run_count))
+ run_count = 0
+ if run_count >= 2:
+ blocks.append((run_start, run_end, run_count))
+
+ def _visit(node: Node) -> None:
+ _scan_siblings(node)
+ for child in node.children:
+ if child.type in lmap.function_kinds:
+ continue # nested fn — walked as its own entry
+ _visit(child)
+
+ _visit(body_node)
+ return blocks
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/ast_utils.py b/packages/core/src/repowise/core/analysis/health/complexity/ast_utils.py
new file mode 100644
index 000000000..7ea09accb
--- /dev/null
+++ b/packages/core/src/repowise/core/analysis/health/complexity/ast_utils.py
@@ -0,0 +1,165 @@
+"""Shared tree-sitter AST helpers used across the walker passes.
+
+Name/text extraction, function-entry naming (including lambdas assigned to
+a variable or passed as a callback), top-level function-node collection, and
+parameter counting. These are the cross-pass primitives; the metric passes
+(cyclomatic, assertions, error-handling, perf, class-analysis) build on them.
+"""
+
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+from .languages import LanguageNodeMap
+
+if TYPE_CHECKING:
+ from tree_sitter import Node
+
+# Suffix shared by tree-sitter identifier node types (``identifier`` /
+# ``field_identifier`` / ``property_identifier`` / ``type_identifier`` ...).
+# Used by both the assertion-callee scan and the class self-member probe.
+_IDENTIFIER_SUFFIX = "identifier"
+
+# Leaf node types that carry a declared name at the bottom of a C/C++
+# ``declarator`` chain.
+_DECLARATOR_NAME_KINDS = frozenset(
+ {"identifier", "field_identifier", "type_identifier", "qualified_identifier"}
+)
+
+
+def _find_name(node: Node) -> str:
+ """Best-effort: return the text of the first identifier child."""
+ # Search a couple of common field names first.
+ for field_name in ("name", "identifier"):
+ child = node.child_by_field_name(field_name)
+ if child is not None and child.text is not None:
+ return child.text.decode("utf-8", errors="replace")
+ # C / C++: the function name is not a direct child but nested inside a
+ # ``declarator`` chain (``function_definition → function_declarator →
+ # field_identifier``). Languages with a ``name`` field never reach here.
+ decl = node.child_by_field_name("declarator")
+ hops = 0
+ while decl is not None and hops < 6:
+ if decl.type in _DECLARATOR_NAME_KINDS and decl.text is not None:
+ return decl.text.decode("utf-8", errors="replace")
+ decl = decl.child_by_field_name("declarator") or decl.child_by_field_name("name")
+ hops += 1
+ for child in node.children:
+ if (
+ child.type in ("identifier", "property_identifier", "field_identifier")
+ and child.text is not None
+ ):
+ return child.text.decode("utf-8", errors="replace")
+ return "<anonymous>"
+
+
+def _node_text(node: Node) -> str:
+ return (node.text or b"").decode("utf-8", errors="replace")
+
+
+def _find_assigned_lambda_name(node: Node) -> str | None:
+ parent = node.parent
+ while parent is not None:
+ if parent.type == "variable_declarator":
+ name = parent.child_by_field_name("name")
+ if name is not None and name.text is not None:
+ return _node_text(name)
+ if parent.type in {"assignment_expression", "assignment_pattern"}:
+ left = parent.child_by_field_name("left")
+ if left is None:
+ left = next((child for child in parent.children if child is not node), None)
+ if left is not None and left.text is not None:
+ return _node_text(left)
+ if parent.type not in {"parenthesized_expression", "as_expression", "satisfies_expression"}:
+ return None
+ parent = parent.parent
+ return None
+
+
+def _find_call_callback_callee(node: Node) -> str | None:
+ parent = node.parent
+ while parent is not None and parent.type in {
+ "parenthesized_expression",
+ "as_expression",
+ "satisfies_expression",
+ }:
+ parent = parent.parent
+ if parent is None or parent.type != "arguments":
+ return None
+ call = parent.parent
+ if call is None or call.type != "call_expression":
+ return None
+ callee = call.child_by_field_name("function")
+ if callee is None or callee.text is None:
+ return None
+ return " ".join(_node_text(callee).split())
+
+
+_TEST_SUITE_CALLBACK_CALLEES = frozenset({"describe", "context", "suite"})
+
+
+def _is_test_suite_callback(node: Node, lmap: LanguageNodeMap) -> bool:
+ if node.type not in lmap.lambda_kinds:
+ return False
+ callee_text = _find_call_callback_callee(node)
+ if callee_text is None:
+ return False
+ return any(part in _TEST_SUITE_CALLBACK_CALLEES for part in callee_text.split("."))
+
+
+def _find_function_entry_name(node: Node, lmap: LanguageNodeMap) -> str:
+ if node.type not in lmap.lambda_kinds:
+ return _find_name(node)
+ assigned = _find_assigned_lambda_name(node)
+ if assigned:
+ return assigned
+ if callee := _find_call_callback_callee(node):
+ return f"{callee} callback"
+ return f"<anonymous@{node.start_point[0] + 1}>"
+
+
+def _collect_function_nodes(root: Node, lmap: LanguageNodeMap) -> list[Node]:
+ """All function / method definition nodes in the file.
+
+ Iterative pre-order traversal. We descend into class / module
+ bodies but do **not** recurse below a function or lambda. Lambdas found
+ before any function boundary are module-level executable units (for
+ example route callbacks) and get their own entry; lambdas inside an
+ already-collected function still roll up into that function.
+ """
+ out: list[Node] = []
+ stack: list[Node] = [root]
+ while stack:
+ node = stack.pop()
+ if node.type in lmap.lambda_kinds and _is_test_suite_callback(node, lmap):
+ stack.extend(node.children)
+ continue
+ if node.type in lmap.function_kinds or node.type in lmap.lambda_kinds:
+ out.append(node)
+ continue
+ for child in node.children:
+ stack.append(child)
+ return out
+
+
+def _count_parameters(fn_node: Node) -> int:
+ """Best-effort parameter-list size for *fn_node*.
+
+ Looks at tree-sitter ``parameters`` / ``parameter_list`` / ``parameters_list`` fields and counts non-punctuation
+ children. Returns 0 when no parameter list is found.
+ """
+ params = fn_node.child_by_field_name("parameters")
+ if params is None:
+ for child in fn_node.children:
+ if child.type in ("parameters", "parameter_list", "formal_parameters"):
+ params = child
+ break
+ if params is None:
+ return 0
+ count = 0
+ for child in params.children:
+ if child.type in ("(", ")", ",", "self", "cls", ":", "*", "**"):
+ continue
+ if child.is_named:
+ count += 1
+ return count
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/class_analysis.py b/packages/core/src/repowise/core/analysis/health/complexity/class_analysis.py
new file mode 100644
index 000000000..37e1690fd
--- /dev/null
+++ b/packages/core/src/repowise/core/analysis/health/complexity/class_analysis.py
@@ -0,0 +1,244 @@
+"""Class-level analysis (LCOM4 / god-class).
+
+``_collect_classes`` builds a ``ClassComplexity`` per class-like node for
+languages that opt in (``class_kinds`` non-empty). ``_compute_lcom4`` is the
+cohesion metric: connected components over the methods, where two methods are
+linked if they share an instance member or one calls the other. The safety
+valve returns ``1`` ("no signal") when no instance-member references are
+detected, so an unmapped language never produces a false ``low_cohesion`` hit.
+"""
+
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+from .ast_utils import _IDENTIFIER_SUFFIX, _find_name
+from .languages import LanguageNodeMap
+from .models import ClassComplexity, FunctionComplexity
+from .nloc import _count_nloc
+
+if TYPE_CHECKING:
+ from tree_sitter import Node
+
+_PROP_FIELD_NAMES = ("property", "attribute", "field", "name")
+# ``expression`` is C#'s receiver field on ``member_access_expression`` (its
+# ``this`` token is unnamed, so the positional fallback would pick the member).
+_OBJECT_FIELD_NAMES = ("object", "value", "argument", "operand", "expression")
+
+
+def _class_name(node: Node) -> str:
+ """Best-effort class/impl name.
+
+ Tries the ``name`` field (most class grammars), then ``type`` (Rust's
+ ``impl T`` exposes the implemented type there), then the generic
+ identifier scan.
+ """
+ for field_name in ("name", "type"):
+ child = node.child_by_field_name(field_name)
+ if child is not None and child.text is not None:
+ return child.text.decode("utf-8", errors="replace")
+ return _find_name(node)
+
+
+def _collect_class_nodes(root: Node, lmap: LanguageNodeMap) -> list[Node]:
+ """All class-like grouping nodes in the file (pre-order).
+
+ Descends through the whole tree so nested classes are found too;
+ each becomes its own ``ClassComplexity``.
+ """
+ out: list[Node] = []
+ stack: list[Node] = [root]
+ while stack:
+ node = stack.pop()
+ # ``is_named`` filters out keyword tokens that share a type name
+ # with an expression node (e.g. the ``class`` keyword vs a
+ # ``class`` expression in tree-sitter-typescript).
+ if node.type in lmap.class_kinds and node.is_named:
+ out.append(node)
+ for child in node.children:
+ stack.append(child)
+ return out
+
+
+def _collect_class_methods(class_node: Node, lmap: LanguageNodeMap) -> list[Node]:
+ """Direct method nodes of *class_node*.
+
+ Stops at nested classes (their methods belong to the inner class) and
+ does not descend into a method body (nested local defs roll up into
+ the method, mirroring ``_collect_function_nodes``).
+ """
+ methods: list[Node] = []
+ stack: list[Node] = list(class_node.children)
+ while stack:
+ node = stack.pop()
+ if node.type in lmap.class_kinds:
+ continue # nested class — its methods are not ours
+ if node.type in lmap.function_kinds:
+ methods.append(node)
+ continue # don't descend into the method body
+ for child in node.children:
+ stack.append(child)
+ return methods
+
+
+def _self_member_name(node: Node, lmap: LanguageNodeMap) -> str | None:
+ """Extract ``member`` from a ``self.member`` / ``this.member`` access.
+
+ Returns the member name when the receiver token is one of the
+ language's ``self_identifiers``; otherwise ``None`` (so ``other.x`` and
+ ``a.b.c``'s outer hops are ignored — only direct instance access
+ counts toward cohesion).
+ """
+ obj: Node | None = None
+ for field_name in _OBJECT_FIELD_NAMES:
+ obj = node.child_by_field_name(field_name)
+ if obj is not None:
+ break
+ if obj is None:
+ obj = next((c for c in node.children if c.is_named), None)
+
+ prop: Node | None = None
+ for field_name in _PROP_FIELD_NAMES:
+ prop = node.child_by_field_name(field_name)
+ if prop is not None:
+ break
+ if prop is None:
+ named = [c for c in node.children if c.is_named]
+ prop = next(
+ (c for c in reversed(named) if c.type.endswith(_IDENTIFIER_SUFFIX)),
+ None,
+ )
+
+ if obj is None or prop is None or obj is prop:
+ return None
+ if obj.text is None or prop.text is None:
+ return None
+ if obj.text.decode("utf-8", errors="replace") not in lmap.self_identifiers:
+ return None
+ return prop.text.decode("utf-8", errors="replace")
+
+
+def _collect_self_members(method_node: Node, lmap: LanguageNodeMap) -> set[str]:
+ """Set of instance-member names referenced by *method_node*.
+
+ Walks the method body (descending through nested functions/lambdas,
+ which close over the same instance) but stops at nested class
+ definitions. Both field reads and method calls reduce to a member
+ name here — both are evidence two methods touch the same thing.
+ """
+ members: set[str] = set()
+ if not lmap.self_identifiers or not lmap.member_access_kinds:
+ return members
+ stack: list[Node] = list(method_node.children)
+ while stack:
+ node = stack.pop()
+ if node.type in lmap.class_kinds:
+ continue # nested class has its own self
+ if node.type in lmap.member_access_kinds:
+ name = _self_member_name(node, lmap)
+ if name:
+ members.add(name)
+ for child in node.children:
+ stack.append(child)
+ return members
+
+
+def _compute_lcom4(
+ method_nodes: list[Node],
+ method_fcs: list[FunctionComplexity],
+ lmap: LanguageNodeMap,
+) -> tuple[int, int]:
+ """Return ``(lcom4, field_count)`` for a class.
+
+ LCOM4 = number of connected components over the methods, where two
+ methods are connected if they share an instance member or one calls
+ the other (a call shows up as a reference to the callee's name).
+
+ **Safety valve:** if no instance-member references are detected at all
+ (a pure-static class, or — importantly — a language whose
+ member-access node type we have not mapped), return ``1`` rather than
+ ``len(methods)``. This prevents ``low_cohesion`` from false-firing on
+ an unverified language: a missing mapping yields "no signal", never a
+ spurious high-LCOM hit.
+ """
+ n = len(method_nodes)
+ if n == 0:
+ return 1, 0
+
+ members_per_method: list[set[str]] = [
+ _collect_self_members(node, lmap) for node in method_nodes
+ ]
+ total_refs = sum(len(m) for m in members_per_method)
+ method_names = {fc.name for fc in method_fcs}
+ all_members: set[str] = set().union(*members_per_method) if members_per_method else set()
+ field_count = len(all_members - method_names)
+
+ if total_refs == 0:
+ return 1, field_count
+
+ # Union-find over method indices.
+ parent = list(range(n))
+
+ def _find(i: int) -> int:
+ while parent[i] != i:
+ parent[i] = parent[parent[i]]
+ i = parent[i]
+ return i
+
+ def _union(a: int, b: int) -> None:
+ ra, rb = _find(a), _find(b)
+ if ra != rb:
+ parent[ra] = rb
+
+ name_to_idx = {fc.name: i for i, fc in enumerate(method_fcs)}
+ # Bucket method indices by each member they reference; the method that
+ # *defines* that name (a callee) joins the bucket too, so call edges and
+ # shared-field edges are both captured in one pass.
+ buckets: dict[str, list[int]] = {}
+ for i, members in enumerate(members_per_method):
+ for m in members:
+ buckets.setdefault(m, []).append(i)
+ for member, idxs in buckets.items():
+ group = list(idxs)
+ callee = name_to_idx.get(member)
+ if callee is not None:
+ group.append(callee)
+ first = group[0]
+ for other in group[1:]:
+ _union(first, other)
+
+ components = {_find(i) for i in range(n)}
+ return len(components), field_count
+
+
+def _collect_classes(
+ root: Node,
+ lmap: LanguageNodeMap,
+ source: bytes,
+ fc_by_node_id: dict[int, FunctionComplexity],
+) -> list[ClassComplexity]:
+ """Build ``ClassComplexity`` for every class-like node in the file."""
+ if not lmap.class_kinds:
+ return []
+ classes: list[ClassComplexity] = []
+ for class_node in _collect_class_nodes(root, lmap):
+ method_nodes = _collect_class_methods(class_node, lmap)
+ method_fcs = [fc_by_node_id[m.id] for m in method_nodes if m.id in fc_by_node_id]
+ # Keep nodes and FCs aligned (a method missing from the function
+ # pass — unusual — drops out of both).
+ aligned_nodes = [m for m in method_nodes if m.id in fc_by_node_id]
+ lcom4, field_count = _compute_lcom4(aligned_nodes, method_fcs, lmap)
+ classes.append(
+ ClassComplexity(
+ name=_class_name(class_node),
+ start_line=class_node.start_point[0] + 1,
+ end_line=class_node.end_point[0] + 1,
+ method_count=len(method_fcs),
+ total_nloc=_count_nloc(class_node, source),
+ methods=method_fcs,
+ lcom4=lcom4,
+ max_method_ccn=max((fc.ccn for fc in method_fcs), default=0),
+ field_count=field_count,
+ )
+ )
+ return classes
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/cyclomatic.py b/packages/core/src/repowise/core/analysis/health/complexity/cyclomatic.py
new file mode 100644
index 000000000..022cca279
--- /dev/null
+++ b/packages/core/src/repowise/core/analysis/health/complexity/cyclomatic.py
@@ -0,0 +1,295 @@
+"""The CCN / cognitive / max-nesting engine.
+
+``_walk_function_body`` is the recursive per-function walk: it accumulates
+McCabe cyclomatic complexity, SonarSource cognitive complexity, max nesting
+depth, ``bumps`` (independent heavy branches), and a side-channel of compound
+boolean conditions. The helpers above it classify branch / loop / case / catch
+nodes and pull condition subtrees out for the boolean-operator tally.
+"""
+
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+from .languages import LanguageNodeMap
+from .models import ConditionComplexity
+
+if TYPE_CHECKING:
+ from tree_sitter import Node
+
+
+def _is_boolean_operator(node: Node, lmap: LanguageNodeMap) -> bool:
+ """True if this node represents a logical ``&&`` / ``||`` operator."""
+ if node.type in lmap.boolean_operator_kinds:
+ return True
+ if node.type in lmap.boolean_operator_text_kinds:
+ # The operator child carries the literal token text.
+ for child in node.children:
+ if child.text is None:
+ continue
+ tok = child.text
+ if tok in (b"&&", b"||", b"and", b"or"):
+ return True
+ return False
+
+
+_BODY_FIELD_NAMES = (
+ "body",
+ "consequence",
+ "alternative",
+ "else_clause",
+ "block",
+)
+
+
+def _count_boolean_ops_in_condition(node: Node, lmap: LanguageNodeMap) -> int:
+ """Count ``&&`` / ``||`` / ``and`` / ``or`` operators in a condition.
+
+ Walks the subtree rooted at *node* but does not descend into nested
+ function bodies (lambdas / closures used as condition values are
+ rare and would skew the count).
+ """
+ if node is None:
+ return 0
+ count = 0
+ stack: list[Node] = [node]
+ while stack:
+ cur = stack.pop()
+ if cur is not node and cur.type in lmap.function_kinds:
+ continue
+ if _is_boolean_operator(cur, lmap):
+ count += 1
+ for child in cur.children:
+ stack.append(child)
+ return count
+
+
+def _enclosing_construct(node: Node, lmap: LanguageNodeMap) -> str:
+ if node.type in lmap.loop_kinds:
+ return "for" if "for" in node.type else "while"
+ if node.type in lmap.case_kinds:
+ return "case"
+ if node.type in lmap.catch_kinds:
+ return "catch"
+ if node.type in lmap.branch_kinds:
+ if "ternary" in node.type or "conditional" in node.type:
+ return "ternary"
+ return "if"
+ return "if"
+
+
+def _condition_subtrees(node: Node) -> list[Node]:
+ """Best-effort: pull the *condition* parts out of a branch/loop node.
+
+ Prefers the tree-sitter ``condition`` named field where exposed
+ (Python, TS, Java, Rust, Go all use it for most branch shapes).
+ Falls back to all direct children except recognised body fields and
+ syntactic punctuation.
+ """
+ cond = node.child_by_field_name("condition")
+ if cond is not None:
+ return [cond]
+ # Switch case value (TS, Java)
+ value = node.child_by_field_name("value")
+ if value is not None and "case" in node.type:
+ return [value]
+ # Fallback: direct children minus bodies / blocks.
+ body_nodes: set[int] = set()
+ for fname in _BODY_FIELD_NAMES:
+ child = node.child_by_field_name(fname)
+ if child is not None:
+ body_nodes.add(child.id)
+ out: list[Node] = []
+ for child in node.children:
+ if child.id in body_nodes:
+ continue
+ if not child.is_named:
+ continue
+ if child.type in ("block", "compound_statement", "statement_block"):
+ continue
+ out.append(child)
+ return out
+
+
+def _collect_case_children(node: Node, lmap: LanguageNodeMap) -> list[Node]:
+ """Collect all case/arm nodes from a switch/match node.
+
+ In Rust, ``match_expression`` contains a ``match_block`` which in
+ turn holds the ``match_arm`` nodes. Other languages may place cases
+ directly under the switch node. This helper handles both layouts.
+ """
+ cases: list[Node] = []
+ for child in node.children:
+ if child.type in lmap.case_kinds:
+ cases.append(child)
+ else:
+ # Descend one level into intermediate wrapper nodes (e.g.
+ # ``match_block``) that are not themselves control flow.
+ for grandchild in child.children:
+ if grandchild.type in lmap.case_kinds:
+ cases.append(grandchild)
+ return cases
+
+
+def _is_flat_match(node: Node, lmap: LanguageNodeMap) -> bool:
+ """Return True if *node* is a match/switch with only simple arms.
+
+ A "flat" match has arms whose bodies are single expressions without
+ nested control flow (no ``if``, ``match``, ``for``, ``while``,
+ ``loop``, ``if_let``, ``while_let`` expressions, and no ``block``
+ with multiple statements). Flat matches contribute 1 CCN point for
+ the match keyword itself but do NOT count each arm individually.
+ """
+ complex_types = lmap.branch_kinds | lmap.loop_kinds | lmap.switch_kinds
+ cases = _collect_case_children(node, lmap)
+ if not cases:
+ return False
+ return all(not _subtree_contains_complex(arm, complex_types) for arm in cases)
+
+
+def _subtree_contains_complex(arm_node: Node, complex_types: frozenset[str]) -> bool:
+ """Return True if *arm_node*'s subtree contains complex control flow.
+
+ A ``block`` with more than one statement is also considered complex.
+ """
+ stack: list[Node] = list(arm_node.children)
+ while stack:
+ cur = stack.pop()
+ if cur.type in complex_types:
+ return True
+ # A block with multiple named children (statements) is complex.
+ if cur.type == "block":
+ named_children = [c for c in cur.children if c.is_named]
+ if len(named_children) > 1:
+ return True
+ for child in cur.children:
+ stack.append(child)
+ return False
+
+
+def _walk_function_body(
+ body_node: Node,
+ lmap: LanguageNodeMap,
+) -> tuple[int, int, int, int, list[ConditionComplexity]]:
+ """Recursive AST walk. Returns (ccn, max_nesting, cognitive, bumps,
+ complex_conditions).
+
+ Starts CCN at 1 (the entry path). Nested function bodies are
+ skipped — they will (or already did) produce their own
+ ``FunctionComplexity``.
+
+ ``bumps`` counts how many *direct* children of the function body
+ contain nested control flow that reaches a depth of ≥ 2. A function
+ with several heavy independent branches is "bumpy" in
+ CodeScene/SonarSource terminology.
+
+ ``complex_conditions`` is an additive side-channel — collected for
+ every branch/loop/case construct encountered. The CCN / cognitive
+ accumulation logic is unchanged.
+ """
+
+ ccn = 1
+ max_nesting = 0
+ cognitive = 0
+ bumps = 0
+ conditions: list[ConditionComplexity] = []
+
+ # Track match_expression nodes identified as "flat" so their arms
+ # are not individually counted as branch points.
+ flat_match_ids: set[int] = set()
+
+ def _recurse(node: Node, depth: int) -> None:
+ nonlocal ccn, max_nesting, cognitive
+
+ # Don't descend into nested function bodies — they're walked
+ # separately at the top level. Lambdas / arrow functions DO
+ # contribute to the enclosing function's complexity.
+ if node.type in lmap.function_kinds:
+ return
+
+ nesting_increment = 0
+ ccn_increment = 0
+
+ # Check if this is a case/arm inside a flat match — skip it.
+ # In Rust the parent chain is match_arm → match_block → match_expression,
+ # so we check both parent and grandparent.
+ _parent = node.parent
+ is_flat_match_arm = False
+ if (
+ node.type in lmap.case_kinds
+ and _parent is not None
+ and (
+ _parent.id in flat_match_ids
+ or (_parent.parent is not None and _parent.parent.id in flat_match_ids)
+ )
+ ):
+ is_flat_match_arm = True
+
+ if is_flat_match_arm:
+ # Flat match arms: no CCN increment, no nesting increment.
+ pass
+ elif (
+ node.type in lmap.branch_kinds
+ or node.type in lmap.loop_kinds
+ or node.type in lmap.case_kinds
+ or node.type in lmap.catch_kinds
+ ):
+ ccn_increment = 1
+ nesting_increment = 1
+ # Side-channel: count compound boolean ops in this
+ # construct's condition. Does not affect ccn/cognitive
+ # (boolean operators are still tallied independently by
+ # the regular recursion below).
+ op_count = 0
+ for sub in _condition_subtrees(node):
+ op_count += _count_boolean_ops_in_condition(sub, lmap)
+ if op_count > 0:
+ conditions.append(
+ ConditionComplexity(
+ line=node.start_point[0] + 1,
+ operator_count=op_count,
+ enclosing_construct=_enclosing_construct(node, lmap),
+ )
+ )
+ elif node.type in lmap.try_kinds:
+ # TRY opens a nesting level but does not branch on its own.
+ nesting_increment = 1
+ elif node.type in lmap.switch_kinds:
+ # Detect flat match: all arms are simple single-expression arms.
+ if _is_flat_match(node, lmap):
+ flat_match_ids.add(node.id)
+ # Flat match: count 1 CCN point for the match itself,
+ # open a nesting level, but arms won't be counted.
+ ccn_increment = 1
+ # Switch opens nesting; each case contributes its own +1.
+ nesting_increment = 1
+ elif _is_boolean_operator(node, lmap):
+ ccn_increment = 1
+
+ ccn += ccn_increment
+ new_depth = depth + nesting_increment
+ if nesting_increment:
+ # SonarSource cognitive: each nesting node adds (1 + depth).
+ cognitive += 1 + depth
+ elif ccn_increment:
+ # Flat +1 for boolean operators (no nesting impact).
+ cognitive += 1
+
+ if new_depth > max_nesting:
+ max_nesting = new_depth
+
+ for child in node.children:
+ _recurse(child, new_depth)
+
+ for child in body_node.children:
+ # Per-child peak depth: temporarily swap max_nesting out so we
+ # can read just this child's contribution, then restore.
+ outer_max = max_nesting
+ max_nesting = 0
+ _recurse(child, 0)
+ child_peak = max_nesting
+ max_nesting = max(outer_max, child_peak)
+ if child_peak >= 2:
+ bumps += 1
+
+ return ccn, max_nesting, cognitive, bumps, conditions
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/error_handling.py b/packages/core/src/repowise/core/analysis/health/complexity/error_handling.py
new file mode 100644
index 000000000..62d08d6a6
--- /dev/null
+++ b/packages/core/src/repowise/core/analysis/health/complexity/error_handling.py
@@ -0,0 +1,160 @@
+"""Error-handling anti-pattern detection (``error_handling`` biomarker).
+
+Ported from the bench-validated detector (24/24 fixtures across 11
+languages). Precision-first: every detector targets the unambiguous
+shape and degrades to "no signal" rather than guessing.
+
+``_collect_error_handling`` is a whole-tree pass emitting one
+``ErrorHandlingHit`` per swallowed catch / bare except / Rust panic-unwrap /
+Go err-swallow it finds, anywhere in the file (not just function bodies).
+"""
+
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+from .languages import LanguageNodeMap
+from .models import ErrorHandlingHit
+
+if TYPE_CHECKING:
+ from tree_sitter import Node
+
+# Block-like body node types of a catch/except clause.
+_EH_BLOCK_KINDS = frozenset({"block", "statement_block", "compound_statement"})
+# Statement node types that count as "no real handling" inside a catch body.
+_EH_TRIVIAL_STMT = frozenset({"comment", "pass_statement", "line_comment", "block_comment"})
+# Rust: each of these is a latent panic-on-error.
+_RUST_UNWRAP_METHODS = frozenset({"unwrap", "expect", "unwrap_unchecked"})
+_RUST_PANIC_MACROS = frozenset({"panic", "unreachable", "todo", "unimplemented"})
+
+
+def _eh_text(node: Node) -> str:
+ return (node.text or b"").decode("utf-8", errors="replace")
+
+
+def _eh_named(node: Node) -> list[Node]:
+ return [c for c in node.children if c.is_named]
+
+
+def _eh_find_body_block(clause: Node) -> Node | None:
+ """The block-like child of a catch/except clause (its handler body)."""
+ for c in clause.children:
+ if c.type in _EH_BLOCK_KINDS:
+ return c
+ # Kotlin catch_block / some grammars nest the block one level down.
+ for c in clause.children:
+ for g in c.children:
+ if g.type in _EH_BLOCK_KINDS:
+ return g
+ return None
+
+
+def _eh_is_trivial_stmt(stmt: Node, language: str) -> bool:
+ if stmt.type in _EH_TRIVIAL_STMT:
+ return True
+ if language == "python" and stmt.type == "expression_statement":
+ inner = _eh_named(stmt)
+ if not inner:
+ return True
+ # ``...`` or a docstring as the entire statement.
+ if inner[0].type in ("ellipsis", "string"):
+ return True
+ return False
+
+
+def _eh_body_is_swallowed(block: Node, language: str) -> bool:
+ real = [c for c in _eh_named(block) if not _eh_is_trivial_stmt(c, language)]
+ return len(real) == 0
+
+
+def _eh_is_bare_except(clause: Node) -> bool:
+ """Python ``except:`` / ``except Exception:`` / ``except BaseException:``."""
+ kids = [c for c in clause.children if c.type != "comment"]
+ after = [c for c in kids if c.type not in ("except", ":") and c.type not in _EH_BLOCK_KINDS]
+ if not after:
+ return True # bare ``except:``
+ # ``except Exception:`` / ``except BaseException:`` (single catch-all
+ # identifier — a tuple of specific types or an ``as`` binding on a
+ # specific type does not match).
+ first = after[0]
+ return first.type == "identifier" and _eh_text(first) in ("Exception", "BaseException")
+
+
+def _eh_rust_hit(node: Node) -> bool:
+ """True when *node* is an unwrap/expect call or a panic-family macro."""
+ if node.type == "call_expression":
+ fn = node.child_by_field_name("function")
+ if fn is not None and fn.type == "field_expression":
+ fld = fn.child_by_field_name("field")
+ return fld is not None and _eh_text(fld) in _RUST_UNWRAP_METHODS
+ return False
+ if node.type == "macro_invocation":
+ mac = node.child_by_field_name("macro")
+ return mac is not None and _eh_text(mac) in _RUST_PANIC_MACROS
+ return False
+
+
+def _eh_go_cond_is_err_check(cond_text: str) -> bool:
+ t = cond_text.replace(" ", "")
+ return "err!=nil" in t or "err==nil" in t
+
+
+def _eh_go_hit(node: Node) -> bool:
+ """Go: empty ``if err != nil {}`` or blank-identifier discard of a call."""
+ if node.type == "if_statement":
+ cond = node.child_by_field_name("condition")
+ cons = node.child_by_field_name("consequence")
+ return (
+ cond is not None
+ and cons is not None
+ and _eh_go_cond_is_err_check(_eh_text(cond))
+ and len(_eh_named(cons)) == 0
+ )
+ if node.type in ("short_var_declaration", "assignment_statement"):
+ left = node.child_by_field_name("left")
+ right = node.child_by_field_name("right")
+ if left is None or right is None:
+ return False
+ left_kids = _eh_named(left)
+ has_blank = any(c.type == "blank_identifier" or _eh_text(c) == "_" for c in left_kids)
+ right_is_call = any(c.type == "call_expression" for c in _eh_named(right)) or (
+ right.type == "expression_list"
+ and any(c.type == "call_expression" for c in _eh_named(right))
+ )
+ # Multi-return discard: ≥2 LHS targets, a call on the RHS, a blank present.
+ return has_blank and len(left_kids) >= 2 and right_is_call
+ return False
+
+
+def _collect_error_handling(
+ root: Node, language: str, lmap: LanguageNodeMap
+) -> list[ErrorHandlingHit]:
+ """Whole-tree pass: every error-handling anti-pattern with its line.
+
+ Catch-clause shapes reuse the ``LanguageNodeMap`` catch kinds (Python
+ ``except_clause``; JS/TS/Java/C++/C# ``catch_clause``; Kotlin
+ ``catch_block``); Rust and Go have no catch nodes and use their own
+ recognizers. Module-level code is covered too — anti-patterns are not
+ confined to function bodies.
+ """
+ hits: list[ErrorHandlingHit] = []
+ catch_kinds = lmap.catch_kinds
+ is_python = language == "python"
+ is_rust = language == "rust"
+ is_go = language == "go"
+ stack: list[Node] = [root]
+ while stack:
+ node = stack.pop()
+ if catch_kinds and node.type in catch_kinds:
+ block = _eh_find_body_block(node)
+ if block is not None and _eh_body_is_swallowed(block, language):
+ hits.append(ErrorHandlingHit("swallowed_catch", node.start_point[0] + 1))
+ if is_python and _eh_is_bare_except(node):
+ hits.append(ErrorHandlingHit("bare_except", node.start_point[0] + 1))
+ elif is_rust and _eh_rust_hit(node):
+ hits.append(ErrorHandlingHit("unsafe_unwrap", node.start_point[0] + 1))
+ elif is_go and _eh_go_hit(node):
+ hits.append(ErrorHandlingHit("go_swallow", node.start_point[0] + 1))
+ stack.extend(node.children)
+ hits.sort(key=lambda h: h.line)
+ return hits
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/models.py b/packages/core/src/repowise/core/analysis/health/complexity/models.py
new file mode 100644
index 000000000..92023276b
--- /dev/null
+++ b/packages/core/src/repowise/core/analysis/health/complexity/models.py
@@ -0,0 +1,248 @@
+"""Walker output schema — the dataclasses ``walk_file`` produces.
+
+Pure data, no logic. Each type is consumed by a specific biomarker or by
+the health engine; see the per-class docstrings for the downstream reader.
+"""
+
+from __future__ import annotations
+
+from dataclasses import dataclass, field
+
+
+@dataclass
+class ConditionComplexity:
+ """One control-flow condition with its boolean-operator count.
+
+ Emitted by the walker as a side-channel to ``FunctionComplexity``;
+ does not affect CCN or cognitive complexity. Consumed by the
+ ``complex_conditional`` biomarker.
+ """
+
+ line: int # 1-indexed start line of the enclosing construct
+ operator_count: int
+ enclosing_construct: str # "if" | "while" | "for" | "ternary" | "case"
+
+
+@dataclass
+class FunctionComplexity:
+ """Per-function metrics produced by the walker."""
+
+ name: str
+ start_line: int # 1-indexed
+ end_line: int # 1-indexed
+ ccn: int
+ max_nesting: int
+ cognitive: int
+ nloc: int # non-blank lines inside the body
+ # Number of top-level body sub-blocks whose internal nesting reached
+ # ≥ 2 — used by ``bumpy_road``. A flat function has 0 bumps.
+ bumps: int = 0
+ # Number of declared parameters on the function signature — used by
+ # ``primitive_obsession``. Counted via the tree-sitter ``parameters``
+ # field; 0 when the language lacks an explicit list or extraction fails.
+ param_count: int = 0
+ # Per-condition boolean-operator counts collected during the walk.
+ # Empty when no branch/loop carries compound boolean expressions.
+ complex_conditions: list[ConditionComplexity] = None # type: ignore[assignment]
+ # Runs of ≥2 consecutive assertion statements within the body, each
+ # ``(start_line, end_line, count)``. Populated only for languages whose
+ # ``LanguageNodeMap`` opts into assertion detection (``assert_kinds`` /
+ # ``assert_call_kinds``). Consumed by the test-quality biomarkers.
+ assertion_blocks: list[tuple[int, int, int]] = None # type: ignore[assignment]
+
+ def __post_init__(self) -> None:
+ if self.complex_conditions is None:
+ self.complex_conditions = []
+ if self.assertion_blocks is None:
+ self.assertion_blocks = []
+
+
+@dataclass
+class ClassComplexity:
+ """Per-class aggregate metrics produced by the walker.
+
+ Emitted only for languages whose ``LanguageNodeMap`` opts into
+ class-level analysis (``class_kinds`` non-empty). Consumed by the
+ ``low_cohesion`` (LCOM4) and ``god_class`` biomarkers.
+
+ ``lcom4`` is the LCOM4 cohesion metric — the number of connected
+ components in the graph whose nodes are the class's methods and whose
+ edges link methods that share an instance field or call one another.
+ ``1`` means a fully cohesive class (or "no signal": see the safety
+ valve in ``_compute_lcom4``). Higher values mean the class splinters
+ into unrelated method clusters.
+ """
+
+ name: str
+ start_line: int # 1-indexed
+ end_line: int # 1-indexed
+ method_count: int
+ total_nloc: int
+ methods: list[FunctionComplexity]
+ lcom4: int = 1
+ max_method_ccn: int = 0
+ field_count: int = 0
+
+
+@dataclass(frozen=True)
+class ErrorHandlingHit:
+ """One error-handling anti-pattern occurrence in a file.
+
+ Collected by the walker's whole-tree pass (see
+ ``_collect_error_handling``) and consumed by the ``error_handling``
+ biomarker. ``kind`` is one of:
+
+ - ``swallowed_catch`` — a catch/except whose body has no real handling
+ (empty, or only ``pass`` / ``...`` / a docstring / comments).
+ - ``bare_except`` — Python ``except:`` / ``except Exception:`` /
+ ``except BaseException:`` (catch-all) regardless of body.
+ - ``unsafe_unwrap`` — Rust ``.unwrap()`` / ``.expect()`` /
+ ``.unwrap_unchecked()`` calls and ``panic!`` / ``unreachable!`` /
+ ``todo!`` / ``unimplemented!`` macros (latent panic-on-error).
+ - ``go_swallow`` — Go empty ``if err != nil {}`` block, or a
+ blank-identifier discard of a multi-return call's value.
+ """
+
+ kind: str
+ line: int # 1-indexed
+
+
+@dataclass(frozen=True)
+class PerfHit:
+ """One performance-risk occurrence in a file (the ``performance`` dimension).
+
+ Collected by the walker's whole-tree perf pass (see
+ ``_collect_perf_hits``) and lifted into findings by the perf biomarkers.
+ Precision-first: every hit is loop-body-scoped and execution-sink-gated so
+ an unsupported language / parse failure / builder-only call yields nothing.
+
+ ``kind`` is one of:
+
+ - ``io_in_loop`` — an execution sink at an I/O boundary (db / network /
+ filesystem / subprocess) inside a real, data-dependent loop body. The
+ boundary kind is carried in ``detail``.
+ - ``string_concat_in_loop`` — string accumulation (``+=`` onto a string)
+ inside a loop, instead of a buffer / ``join``.
+ - ``blocking_sync_in_async`` — a known blocking sync call (``time.sleep`` /
+ sync ``requests`` / ``subprocess`` / ``os.system`` / bare ``open``) inside
+ an ``async def``, not awaited. ``detail`` carries the offending API.
+ - ``resource_construction_in_loop`` — a heavy I/O client / connection
+ (``sqlite3.connect`` / ``httpx.Client`` / ``boto3.client`` / ``new
+ PrismaClient``) constructed each iteration instead of hoisted.
+ - ``lock_in_loop`` — a lock acquired each iteration (``lock.acquire`` /
+ ``mu.Lock`` / ``synchronized`` / ``lock(x){}``); a contention signal that
+ activates the ``lock`` I/O boundary kind.
+ - ``serial_await_in_loop`` — an awaited I/O sink in a loop body (the
+ missed-``gather`` / ``Promise.all`` shape). ``detail`` carries the
+ boundary kind. Rides alongside ``io_in_loop`` as an advisory co-signal.
+ - ``membership_test_against_list_in_loop`` — ``x in big_list`` (or
+ ``big_list.includes(x)``) inside a loop where the right operand is a known
+ list -> O(n·m); a set would make each probe O(1).
+
+ Phase-7b markers (``func_start``-carrying so the centrality gate can resolve
+ the enclosing function to a graph symbol node):
+
+ - ``nested_loop_with_io`` — an I/O sink in the inner body of a nested loop
+ (``loop_depth >= 2``) -> O(n·m) round-trips. Rides alongside ``io_in_loop``;
+ the nesting itself raises confidence, so it is NOT centrality-gated.
+ - ``nested_loop_quadratic`` — a data-dependent loop nested inside another
+ (O(n^2) shape). Emitted as a *candidate*; the centrality gate keeps it
+ ONLY in a hot/central function (the gate is the precision fix).
+ - ``hot_path_sync_io`` — a blocking (non-awaited) I/O sink at ``loop_depth 0``.
+ Emitted as a *candidate*; survives only in a top-centrality function.
+ - ``blocking_io_under_lock`` — an I/O sink reached while a block-scoped lock
+ (C# ``lock``/Java ``synchronized``) is held. Same-function (sink lexically
+ under the lock) is emitted here; the cross-function case is added by
+ ``perf.gated.collect_blocking_io_under_lock``.
+
+ The Phase-7a markers above are emitted via the dialect ``loop_call_marker`` /
+ ``loop_stmt_marker`` hooks (and the inline awaited-sink path for
+ ``serial_await_in_loop``), so each is per-language opt-in.
+ """
+
+ kind: str
+ line: int # 1-indexed
+ function: str | None = None
+ detail: str = ""
+ # 1-indexed ``def`` line of the enclosing function (0 = module scope). Lets
+ # the centrality gate (Phase 7b) resolve a candidate hit to its graph symbol
+ # node without a separate lookup. 0 for hits where it is irrelevant.
+ func_start: int = 0
+ # Reachability path for a cross-function hit: the resolved symbol-node
+ # chain ``A -> B -> ... -> sink`` (PR4). Empty for same-function hits —
+ # its emptiness is what distinguishes the two cases downstream.
+ path: tuple[str, ...] = ()
+
+
+@dataclass(frozen=True)
+class PerfFnFacts:
+ """Per-function facts the cross-function perf bridge needs (PR4).
+
+ Collected on the same whole-tree perf pass as ``PerfHit``. Two signals:
+
+ - ``loop_call_targets`` — the rightmost names of calls made inside a loop
+ body that are NOT themselves a direct I/O sink (those are already a
+ same-function ``io_in_loop`` hit). Each is a candidate helper whose body
+ might reach a sink transitively — the entry points of the reachability
+ walk.
+ - ``bare_sink_kind`` — the boundary kind of an I/O sink this function
+ executes at ``loop_depth 0`` (its own top level, not nested in a loop),
+ else ``None``. A function with a bare sink is a *reachability target*:
+ when a loop in another function calls into it, that sink runs once per
+ iteration. The ``loop_depth 0`` requirement is deliberate — a sink the
+ function runs inside its own loop is already counted there.
+
+ ``func_start`` is the 1-indexed line of the enclosing function's ``def``
+ (0 for module scope, which maps to the synthetic ``::__module__`` node);
+ the bridge resolves it to a graph symbol node by line containment.
+
+ ``lock_call_targets`` is the Phase-7b analogue of ``loop_call_targets``: the
+ rightmost names of non-sink calls made while a block-scoped lock is held.
+ Each is a candidate helper whose body might reach a sink transitively —
+ the cross-function ``blocking_io_under_lock`` entry points.
+ """
+
+ function: str | None
+ func_start: int
+ # ``(callee_name, call_line)`` pairs for the loop-nested non-sink calls.
+ loop_call_targets: tuple[tuple[str, int], ...]
+ bare_sink_kind: str | None
+ # ``(callee_name, call_line)`` pairs for non-sink calls under a held lock.
+ lock_call_targets: tuple[tuple[str, int], ...] = ()
+ # Phase-7b centrality-gated facts the engine turns into hits only for a hot
+ # function. ``nested_loop_line`` is a representative data-dependent
+ # nested-loop site (0 = none -> ``nested_loop_quadratic``);
+ # ``blocking_sink_{kind,line}`` is the first non-awaited loop_depth-0 I/O
+ # sink (None/0 = none -> ``hot_path_sync_io``).
+ nested_loop_line: int = 0
+ blocking_sink_kind: str | None = None
+ blocking_sink_line: int = 0
+
+
+@dataclass
+class FileComplexity:
+ """Walker output for one file: per-function and per-class metrics.
+
+ ``walk_file`` returns this; ``walk_file_complexity`` is the
+ backward-compatible thin wrapper that returns only ``functions``.
+ """
+
+ functions: list[FunctionComplexity]
+ classes: list[ClassComplexity]
+ file_nloc: int = 0
+ # Error-handling anti-pattern occurrences (whole-tree pass). Empty when
+ # the language is unsupported or parsing failed — "no signal", never a
+ # false positive.
+ error_handling_hits: list[ErrorHandlingHit] = field(default_factory=list)
+ # Performance-risk occurrences (whole-tree perf pass). Empty when the
+ # language opts out of the perf pass (no ``call_kinds``) or parsing failed.
+ perf_hits: list[PerfHit] = field(default_factory=list)
+ # Names imported from an I/O-typed library in this file, mapped to their
+ # boundary kind (db / network / filesystem / subprocess / lock). The
+ # per-file import bridge; PR4's cross-function reachability consumes it.
+ io_boundary_names: dict[str, str] = field(default_factory=dict)
+ # Per-function facts feeding PR4's cross-function N+1 detection: which
+ # callees each function invokes inside a loop body, and whether the
+ # function holds a bare (non-loop) I/O sink. Empty when the language opts
+ # out of the perf pass. Consumed by ``perf.crossfn``, not by a biomarker.
+ perf_fn_facts: list[PerfFnFacts] = field(default_factory=list)
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/nloc.py b/packages/core/src/repowise/core/analysis/health/complexity/nloc.py
new file mode 100644
index 000000000..b88377e44
--- /dev/null
+++ b/packages/core/src/repowise/core/analysis/health/complexity/nloc.py
@@ -0,0 +1,61 @@
+"""Non-blank / non-comment line counting over tree-sitter nodes and raw bytes.
+
+``_count_nloc`` measures a single node's span; ``_count_file_nloc`` is the
+no-tree fallback (used when parsing is unavailable); ``_count_file_nloc_tree``
+excludes comment-only lines using the parsed tree.
+"""
+
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+if TYPE_CHECKING:
+ from tree_sitter import Node
+
+
+def _count_nloc(node: Node, source: bytes) -> int:
+ """Return the count of non-blank lines spanned by *node*."""
+ start = node.start_point[0]
+ end = node.end_point[0]
+ if end < start:
+ return 0
+ try:
+ snippet = source[node.start_byte : node.end_byte].decode("utf-8", errors="replace")
+ except Exception:
+ return end - start + 1
+ return sum(1 for line in snippet.splitlines() if line.strip())
+
+
+def _count_file_nloc(source: bytes) -> int:
+ """Count non-blank lines in *source* bytes (plain fallback, no tree)."""
+ try:
+ text = source.decode("utf-8", errors="replace")
+ except Exception:
+ return 0
+ return sum(1 for line in text.splitlines() if line.strip())
+
+
+def _count_file_nloc_tree(root_node: Node, source: bytes) -> int:
+ """Count lines that have at least one non-comment token.
+
+ Lines where all content is inside comment nodes are excluded; lines
+ with real code plus a trailing comment still count.
+ """
+ try:
+ lines = source.decode("utf-8", errors="replace").splitlines()
+ except Exception:
+ return 0
+ code_lines: set[int] = set()
+ stack = [root_node]
+ while stack:
+ node = stack.pop()
+ if "comment" in node.type:
+ continue
+ if not node.children and node.start_byte < node.end_byte:
+ for line in range(node.start_point[0], node.end_point[0] + 1):
+ if line < len(lines) and lines[line].strip():
+ code_lines.add(line)
+ else:
+ for child in node.children:
+ stack.append(child)
+ return len(code_lines)
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.py b/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.py
new file mode 100644
index 000000000..3789f7fec
--- /dev/null
+++ b/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.py
@@ -0,0 +1,457 @@
+"""Performance-risk detection (the ``performance`` health dimension).
+
+io_in_loop / string_concat_in_loop / blocking_sync_in_async and the Phase-7
+loop-level / centrality-gated markers.
+
+One whole-tree pass mirroring ``_collect_error_handling`` but carrying the
+per-node context the perf signal needs: loop depth, in-async, lock depth, and
+the enclosing function name. Two non-negotiable refinements (Phase-0 gate: they
+took precision from 49% to 79%) are baked in:
+ 1. Loop-BODY scoping — only calls under a loop node's ``body`` field run
+ per-iteration; a call in the ``for x in <iterable>`` header runs once.
+ 2. Constant-bound-loop skip — ``for _ in range(<int literals>)`` and loops
+ over literal / ALL_CAPS-named-constant collections are not data-dependent.
+
+The pass is language-agnostic: it looks up ``PERF_DIALECTS[language]`` and
+drives the DFS off the dialect's predicates, early-outing when none is
+registered.
+"""
+
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+from ..perf.dialects import PERF_DIALECTS
+from ..perf.io_boundaries import collect_io_names
+from .languages import LanguageNodeMap
+from .models import PerfFnFacts, PerfHit
+
+if TYPE_CHECKING:
+ from tree_sitter import Node
+
+ from ..perf.dialects.base import BasePerfDialect
+
+# Marker kinds emitted through each dialect hook. The walker consults a
+# dialect's ``markers`` set against these to decide whether to invoke the hook
+# at all, so a dialect that lists none of a hook's kinds never pays for it.
+_LOOP_CALL_MARKER_KINDS = frozenset(
+ {
+ "regex_compile_in_loop",
+ "resource_construction_in_loop",
+ "lock_in_loop",
+ "membership_test_against_list_in_loop",
+ # Phase 7d — language-specific call markers.
+ "list_insert_zero_in_loop",
+ "pd_concat_in_loop",
+ "json_parse_in_loop",
+ }
+)
+_LOOP_STMT_MARKER_KINDS = frozenset(
+ {
+ "defer_in_loop",
+ "resource_construction_in_loop",
+ "lock_in_loop",
+ "membership_test_against_list_in_loop",
+ # Phase 7d — language-specific statement markers.
+ "goroutine_in_unbounded_loop",
+ }
+)
+# Markers for a call that is its OWN iteration construct (``.reduce``), a perf
+# smell at any loop depth — emitted via ``dialect.bare_call_marker``.
+_BARE_CALL_MARKER_KINDS = frozenset({"array_spread_in_reduce"})
+# Markers fired on a loop whose ITERABLE is itself a slow call (``df.iterrows()``)
+# — the call lives in the loop header, so the body markers miss it. Emitted via
+# ``dialect.loop_iterable_call_marker`` against the loop node.
+_LOOP_ITERABLE_CALL_MARKER_KINDS = frozenset({"pandas_iterrows_in_loop"})
+
+# Boundary kinds that are *unambiguously blocking* when called synchronously, so
+# a loop_depth-0 occurrence in a hot function is a ``hot_path_sync_io`` candidate.
+# Limited to ``subprocess`` + ``filesystem`` on purpose:
+# * ``db`` — in an async codebase DB access is awaited (non-blocking), and the
+# un-awaited DB calls a static pass sees are almost all result *materializers*
+# (``result.scalars().all()`` / ``.scalar_one()``) on an already-awaited
+# result, not a round-trip (a probe-confirmed FP generator).
+# * ``network`` — async HTTP is awaited too; the walker's ``awaited`` test only
+# inspects the immediate parent, so a wrapped/chained await
+# (``(await client.GetAsync(u)).X``) can read as non-awaited and FP. Every
+# hot-path TP on the OSS corpus was subprocess/fs (0 network), so excluding
+# it costs no measured precision and removes that FP class. (A wrapper-aware
+# ``awaited`` test could re-admit sync ``requests`` — tracked as a follow-up.)
+# subprocess (``subprocess.run`` / ``os.system``) and filesystem (bare ``open``)
+# are synchronous by construction, with no await semantics to misread.
+_HOT_PATH_SINK_KINDS = frozenset({"subprocess", "filesystem"})
+
+# Block node kinds that form the *body* of a lock construct (C# ``lock (x) {…}``
+# / Java ``synchronized (x) {…}``). Only the body runs with the lock held, so
+# ``lock_depth`` is raised for the body child only — a sink in the lock-object
+# expression (``synchronized(repo.find(id)){…}``) runs BEFORE the lock is taken.
+_LOCK_BODY_KINDS = frozenset({"block", "statement_block", "compound_statement"})
+
+
+def _perf_func_name(node: Node) -> str | None:
+ nm = node.child_by_field_name("name")
+ if nm is not None and nm.text:
+ return nm.text.decode("utf-8", "replace")
+ return None
+
+
+def _enclosing_loop_iterables(
+ node: Node, dialect: BasePerfDialect, loop_kinds: frozenset[str], fn_kinds: frozenset[str]
+) -> set[str]:
+ """Names of the collections every enclosing loop (up to the function bound)
+ iterates over — the lookup the same-collection ``nested_loop_quadratic``
+ shape gate compares the inner loop's iterable against."""
+ names: set[str] = set()
+ cur = node.parent
+ for _ in range(64):
+ if cur is None or cur.type in fn_kinds:
+ break
+ if cur.type in loop_kinds:
+ nm = dialect.loop_iterable_name(cur)
+ if nm:
+ names.add(nm)
+ cur = cur.parent
+ return names
+
+
+def _collect_perf_hits(
+ root: Node, language: str, lmap: LanguageNodeMap
+) -> tuple[list[PerfHit], dict[str, str], list[PerfFnFacts]]:
+ """Whole-tree perf pass → ``(hits, io_boundary_names, fn_facts)``.
+
+ Iterative DFS carrying ``(node, loop_depth, in_async, func_name,
+ func_start, lock_depth)`` — the proven Phase-0 shape, extended with the
+ enclosing function's start line (so per-function facts key to a graph symbol
+ node) and a block-scoped ``lock_depth`` (so an I/O sink under a held lock is
+ flagged). Loop-body scoping and constant-loop skipping are applied so only
+ genuinely per-iteration calls are flagged. Returns nothing for languages
+ that opt out of the perf pass (empty ``call_kinds``).
+
+ Alongside the same-function ``hits``, it accumulates ``fn_facts`` (one
+ :class:`PerfFnFacts` per enclosing function that has any loop-nested call
+ or a bare sink) — the input to PR4's cross-function reachability.
+ """
+ call_kinds = lmap.call_kinds
+ dialect = PERF_DIALECTS.get(language)
+ if not call_kinds or dialect is None:
+ return [], {}, []
+
+ io_names = collect_io_names(root, language)
+ has_db_import = any(k == "db" for k in io_names.values())
+ markers = dialect.markers
+ do_string_concat = "string_concat_in_loop" in markers
+ do_blocking = "blocking_sync_in_async" in markers
+ do_loop_call_marker = bool(markers & _LOOP_CALL_MARKER_KINDS)
+ do_loop_stmt_marker = bool(markers & _LOOP_STMT_MARKER_KINDS)
+ do_bare_call_marker = bool(markers & _BARE_CALL_MARKER_KINDS)
+ do_loop_iterable_call_marker = bool(markers & _LOOP_ITERABLE_CALL_MARKER_KINDS)
+ do_serial_await = "serial_await_in_loop" in markers
+ # Phase 7b markers.
+ do_nested_io = "nested_loop_with_io" in markers
+ do_nested_quadratic = "nested_loop_quadratic" in markers
+ do_hot_path = "hot_path_sync_io" in markers
+ do_lock_io = "blocking_io_under_lock" in markers
+ # ``list_names`` is the precision gate for the membership marker; compute it
+ # once per file only when this dialect can emit that marker, then thread it
+ # to the loop-marker hooks (which ignore it for every other marker).
+ list_names = (
+ dialect.list_bound_names(root)
+ if "membership_test_against_list_in_loop" in markers
+ else frozenset()
+ )
+ loop_kinds = lmap.loop_kinds
+ fn_kinds = lmap.function_kinds
+ lambda_kinds = lmap.lambda_kinds
+ async_fn_kinds = lmap.async_function_kinds
+
+ hits: list[PerfHit] = []
+ # Per-enclosing-function accumulators keyed by the function's start line
+ # (0 = module scope): ``(name, loop_targets{name->line}, lock_targets{...},
+ # [bare_sink], misc)``. loop/lock targets feed the cross-function
+ # reachability passes; the bare-sink slot makes the function a reachability
+ # target. ``misc`` carries the Phase-7b *centrality-gated* facts the engine
+ # turns into hits only for hot functions (kept out of ``hits`` so the raw
+ # walker output stays the same high-precision same-function set):
+ # misc[0] = nested_loop_line (a data-dependent loop nested in another)
+ # misc[1] = blocking_sink_kind (first non-awaited loop_depth-0 sink kind)
+ # misc[2] = blocking_sink_line
+ fn_acc: dict[
+ int, tuple[str | None, dict[str, int], dict[str, int], list[str | None], list]
+ ] = {}
+
+ def _acc(
+ func_start: int, func_name: str | None
+ ) -> tuple[dict[str, int], dict[str, int], list[str | None], list]:
+ entry = fn_acc.get(func_start)
+ if entry is None:
+ entry = (func_name, {}, {}, [None], [0, None, 0])
+ fn_acc[func_start] = entry
+ return entry[1], entry[2], entry[3], entry[4]
+
+ # (node, loop_depth, in_async, func_name, func_start, lock_depth, outer_iter)
+ # ``outer_iter`` is whether the OUTERMOST enclosing loop iterates a collection
+ # (vs a ``while``/cursor) — the precision gate for ``nested_loop_with_io``.
+ stack: list[tuple[Node, int, bool, str | None, int, int, bool]] = [
+ (root, 0, False, None, 0, 0, True)
+ ]
+ while stack:
+ node, loop_depth, in_async, func_name, func_start, lock_depth, outer_iter = stack.pop()
+ t = node.type
+
+ is_loop = t in loop_kinds
+ if is_loop and dialect.is_constant_loop(node):
+ is_loop = False
+
+ entering_fn = t in fn_kinds or t in lambda_kinds
+ is_async_fn = t in async_fn_kinds or (entering_fn and dialect.is_async_fn(node))
+ next_async = True if is_async_fn else (False if entering_fn else in_async)
+ next_func = func_name
+ next_start = func_start
+ if t in fn_kinds:
+ next_func = _perf_func_name(node) or func_name
+ next_start = node.start_point[0] + 1
+
+ # A data-dependent loop nested inside another (``loop_depth`` only counts
+ # non-constant loops) is an O(n^2) shape. Record the site as a fact; the
+ # engine emits ``nested_loop_quadratic`` only when the function is hot
+ # (centrality gate), so the noisy-everywhere shape never ships ungated.
+ if is_loop and loop_depth >= 1 and do_nested_quadratic:
+ # Shape gate (Phase-7d): raw nesting depth was ~3-20% precision in
+ # every language. Fire only on the SAME-COLLECTION shape — the inner
+ # loop iterates the same named collection as an enclosing loop
+ # (all-pairs O(n^2)) — which all four Phase-7c labelers converged on.
+ nm = dialect.loop_iterable_name(node)
+ if nm and nm in _enclosing_loop_iterables(node, dialect, loop_kinds, fn_kinds):
+ misc = _acc(next_start, next_func)[3]
+ if misc[0] == 0:
+ misc[0] = node.start_point[0] + 1
+
+ # A loop whose ITERABLE is itself a slow call (``for _, r in
+ # df.iterrows()``): the call sits in the loop header (runs once), so the
+ # body call markers never see it. Fire on the loop node itself, at any
+ # nesting depth — ``iterrows`` is O(n)-boxing slow on its own.
+ if is_loop and do_loop_iterable_call_marker:
+ icm = dialect.loop_iterable_call_marker(node)
+ if icm is not None:
+ hits.append(
+ PerfHit(icm, node.start_point[0] + 1, next_func, "", func_start=next_start)
+ )
+
+ if t in call_kinds:
+ method = dialect.callee_method_name(node) or ""
+ root_name = dialect.callee_root_name(node) or ""
+ parent = node.parent
+ awaited = parent is not None and "await" in parent.type
+ line = node.start_point[0] + 1
+ if do_bare_call_marker:
+ # A call that is its own iteration construct (``.reduce`` with an
+ # accumulator spread) — a perf smell at any loop depth.
+ bare = dialect.bare_call_marker(root_name, method, node)
+ if bare is not None:
+ hits.append(PerfHit(bare, line, next_func, "", func_start=next_start))
+ kind = dialect.sink_kind(
+ root_name,
+ method,
+ awaited=awaited,
+ is_attribute=dialect.callee_is_attribute(node),
+ io_names=io_names,
+ has_db_import=has_db_import,
+ )
+ if kind is not None:
+ if loop_depth >= 1:
+ hits.append(PerfHit("io_in_loop", line, next_func, kind, func_start=next_start))
+ if do_serial_await and awaited:
+ # An *awaited* sink in a loop body is additionally a
+ # missed-concurrency candidate (a serial round-trip that
+ # a ``gather`` / ``Promise.all`` could fan out). Advisory
+ # co-signal alongside ``io_in_loop``; ``detail`` carries
+ # the boundary kind for the finding.
+ hits.append(
+ PerfHit(
+ "serial_await_in_loop", line, next_func, kind, func_start=next_start
+ )
+ )
+ if do_nested_io and loop_depth >= 2 and outer_iter:
+ # A sink in the inner body of a NESTED loop -> O(n·m)
+ # round-trips. Nesting raises confidence, so it ships
+ # un-gated alongside ``io_in_loop`` — but only when the
+ # OUTER loop iterates a collection (``outer_iter``): a
+ # ``while`` pagination cursor wrapping an inner ``for ...
+ # of chunk`` is ``io_in_loop``, not a nested explosion
+ # (Phase-7c TS while-cursor FP).
+ hits.append(
+ PerfHit(
+ "nested_loop_with_io", line, next_func, kind, func_start=next_start
+ )
+ )
+ else:
+ # A sink at loop_depth 0 makes this function a reachability
+ # target: a loop elsewhere calling into it runs the sink N
+ # times (cross-function N+1).
+ _lt, _kt, sink_slot, misc = _acc(next_start, next_func)
+ if sink_slot[0] is None:
+ sink_slot[0] = kind
+ if (
+ do_hot_path
+ and not awaited
+ and misc[1] is None
+ and kind in _HOT_PATH_SINK_KINDS
+ ):
+ # An inherently-blocking (non-awaited subprocess / fs /
+ # sync-network) sink outside any loop. Noisy everywhere,
+ # so record it as a fact; the engine emits
+ # ``hot_path_sync_io`` only for a hot, request-reachable
+ # function (centrality gate). ``db`` is excluded — see
+ # ``_HOT_PATH_SINK_KINDS``.
+ misc[1] = kind
+ misc[2] = line
+ if do_lock_io and lock_depth >= 1:
+ # A sink reached while a block-scoped lock is held: the
+ # round-trip runs under the lock, serializing every thread.
+ hits.append(
+ PerfHit(
+ "blocking_io_under_lock", line, next_func, kind, func_start=next_start
+ )
+ )
+ else:
+ if loop_depth >= 1:
+ marker = (
+ dialect.loop_call_marker(root_name, method, node, list_names)
+ if do_loop_call_marker
+ else None
+ )
+ if marker is not None:
+ hits.append(PerfHit(marker, line, next_func, "", func_start=next_start))
+ elif method:
+ # A loop-nested call to a non-sink helper: a candidate
+ # entry for cross-function reachability (PR4). Keep the
+ # first line we see the helper called at, for the finding.
+ targets = _acc(next_start, next_func)[0]
+ if method not in targets:
+ targets[method] = line
+ if do_lock_io and lock_depth >= 1 and method:
+ # A non-sink call under a held lock: a candidate entry for the
+ # cross-function ``blocking_io_under_lock`` reachability pass.
+ lock_targets = _acc(next_start, next_func)[1]
+ if method not in lock_targets:
+ lock_targets[method] = line
+ if do_blocking and in_async and not awaited:
+ api = dialect.blocking_sync_api(root_name, method)
+ if api is not None:
+ hits.append(
+ PerfHit(
+ "blocking_sync_in_async", line, next_func, api, func_start=next_start
+ )
+ )
+ else:
+ if do_blocking and in_async:
+ # A non-call member read that blocks in async (C# ``task.Result``).
+ mem = dialect.async_blocking_member(node)
+ if mem is not None:
+ hits.append(
+ PerfHit(
+ "blocking_sync_in_async",
+ node.start_point[0] + 1,
+ next_func,
+ mem,
+ func_start=next_start,
+ )
+ )
+ if loop_depth >= 1:
+ if do_string_concat and dialect.is_string_concat(node):
+ hits.append(
+ PerfHit(
+ "string_concat_in_loop",
+ node.start_point[0] + 1,
+ next_func,
+ "",
+ func_start=next_start,
+ )
+ )
+ elif do_loop_stmt_marker:
+ sm = dialect.loop_stmt_marker(node, list_names)
+ if sm is not None:
+ hits.append(
+ PerfHit(
+ sm,
+ node.start_point[0] + 1,
+ next_func,
+ "",
+ func_start=next_start,
+ )
+ )
+
+ # A block-scoped lock (``lock``/``synchronized``) opens a held region.
+ # Only its BLOCK body runs with the lock held — a sink in the lock-object
+ # expression (``synchronized(repo.find(id)){…}``) runs before the lock is
+ # taken — so ``lock_depth`` is raised per-child, for the body block only.
+ entering_lock = do_lock_io and dialect.is_lock_scope(node)
+
+ if is_loop:
+ # The outermost loop in a nest fixes ``outer_iter``; deeper loops
+ # inherit it. Set when entering the first loop (loop_depth 0 -> 1).
+ next_outer_iter = outer_iter if loop_depth >= 1 else dialect.is_iteration_loop(node)
+ # Only the loop BODY runs per-iteration; the ``for x in <iterable>``
+ # header / ``while <cond>`` condition runs once.
+ body = node.child_by_field_name("body")
+ if body is not None:
+ # NB: tree-sitter Node wrappers are not singletons, so compare
+ # with ``==`` (identity by tree + byte range), never ``is``.
+ for c in node.children:
+ cd = loop_depth + 1 if c == body else loop_depth
+ stack.append(
+ (c, cd, next_async, next_func, next_start, lock_depth, next_outer_iter)
+ )
+ else:
+ for c in node.children:
+ stack.append(
+ (
+ c,
+ loop_depth + 1,
+ next_async,
+ next_func,
+ next_start,
+ lock_depth,
+ next_outer_iter,
+ )
+ )
+ elif entering_lock:
+ # Raise lock_depth for the body block only (not the lock-object expr).
+ for c in node.children:
+ cl = lock_depth + 1 if c.type in _LOCK_BODY_KINDS else lock_depth
+ stack.append((c, loop_depth, next_async, next_func, next_start, cl, outer_iter))
+ else:
+ for c in node.children:
+ stack.append(
+ (c, loop_depth, next_async, next_func, next_start, lock_depth, outer_iter)
+ )
+
+ # Dedup chained sinks: ``result.scalars().all()`` parses as two call nodes
+ # on one line (the ``.scalars()`` sink and the ``.all()`` materializer) —
+ # one logical query, one finding. Collapse per (kind, line, function).
+ seen: set[tuple[str, int, str | None]] = set()
+ deduped: list[PerfHit] = []
+ for h in hits:
+ key = (h.kind, h.line, h.function)
+ if key in seen:
+ continue
+ seen.add(key)
+ deduped.append(h)
+ deduped.sort(key=lambda h: (h.line, h.kind))
+
+ fn_facts = [
+ PerfFnFacts(
+ function=name,
+ func_start=start,
+ loop_call_targets=tuple(sorted(loop_targets.items())),
+ bare_sink_kind=sink[0],
+ lock_call_targets=tuple(sorted(lock_targets.items())),
+ nested_loop_line=misc[0],
+ blocking_sink_kind=misc[1],
+ blocking_sink_line=misc[2],
+ )
+ for start, (name, loop_targets, lock_targets, sink, misc) in fn_acc.items()
+ if (loop_targets or lock_targets or sink[0] is not None or misc[0] or misc[1] is not None)
+ ]
+ fn_facts.sort(key=lambda f: f.func_start)
+ return deduped, io_names, fn_facts
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/walker.py b/packages/core/src/repowise/core/analysis/health/complexity/walker.py
index 35e9c6a7f..606564500 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/walker.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/walker.py
@@ -17,1645 +17,65 @@ Anonymous functions (lambdas, arrow functions, closures) recurse for
their containing function's metrics when they are nested in a named
function. Module-level lambdas, such as route callbacks, produce their
own ``FunctionComplexity`` row.
+
+This module is the orchestrator: ``walk_file`` parses the source once and
+drives the individual passes, each of which lives in its own sibling module:
+
+- ``models`` — the output dataclasses
+- ``ast_utils`` — name/text helpers, function-node collection, params
+- ``nloc`` — non-blank / non-comment line counting
+- ``cyclomatic`` — the CCN / cognitive / nesting engine
+- ``assertions`` — assertion-block detection (test-quality)
+- ``error_handling`` — error-handling anti-patterns
+- ``perf_walk`` — the performance-risk pass
+- ``class_analysis`` — class-level LCOM4 / god-class metrics
"""
from __future__ import annotations
-from dataclasses import dataclass, field
-from typing import TYPE_CHECKING
-
import structlog
-from ..perf.dialects import PERF_DIALECTS
-from ..perf.io_boundaries import collect_io_names
-from .languages import LanguageNodeMap, get_language_map
-
-if TYPE_CHECKING:
- from tree_sitter import Node
-
- from ..perf.dialects.base import BasePerfDialect
-
-log = structlog.get_logger(__name__)
-
-
-@dataclass
-class ConditionComplexity:
- """One control-flow condition with its boolean-operator count.
-
- Emitted by the walker as a side-channel to ``FunctionComplexity``;
- does not affect CCN or cognitive complexity. Consumed by the
- ``complex_conditional`` biomarker.
- """
-
- line: int # 1-indexed start line of the enclosing construct
- operator_count: int
- enclosing_construct: str # "if" | "while" | "for" | "ternary" | "case"
-
-
-@dataclass
-class FunctionComplexity:
- """Per-function metrics produced by the walker."""
-
- name: str
- start_line: int # 1-indexed
- end_line: int # 1-indexed
- ccn: int
- max_nesting: int
- cognitive: int
- nloc: int # non-blank lines inside the body
- # Number of top-level body sub-blocks whose internal nesting reached
- # ≥ 2 — used by ``bumpy_road``. A flat function has 0 bumps.
- bumps: int = 0
- # Number of declared parameters on the function signature — used by
- # ``primitive_obsession``. Counted via the tree-sitter ``parameters``
- # field; 0 when the language lacks an explicit list or extraction fails.
- param_count: int = 0
- # Per-condition boolean-operator counts collected during the walk.
- # Empty when no branch/loop carries compound boolean expressions.
- complex_conditions: list[ConditionComplexity] = None # type: ignore[assignment]
- # Runs of ≥2 consecutive assertion statements within the body, each
- # ``(start_line, end_line, count)``. Populated only for languages whose
- # ``LanguageNodeMap`` opts into assertion detection (``assert_kinds`` /
- # ``assert_call_kinds``). Consumed by the test-quality biomarkers.
- assertion_blocks: list[tuple[int, int, int]] = None # type: ignore[assignment]
-
- def __post_init__(self) -> None:
- if self.complex_conditions is None:
- self.complex_conditions = []
- if self.assertion_blocks is None:
- self.assertion_blocks = []
-
-
-@dataclass
-class ClassComplexity:
- """Per-class aggregate metrics produced by the walker.
-
- Emitted only for languages whose ``LanguageNodeMap`` opts into
- class-level analysis (``class_kinds`` non-empty). Consumed by the
- ``low_cohesion`` (LCOM4) and ``god_class`` biomarkers.
-
- ``lcom4`` is the LCOM4 cohesion metric — the number of connected
- components in the graph whose nodes are the class's methods and whose
- edges link methods that share an instance field or call one another.
- ``1`` means a fully cohesive class (or "no signal": see the safety
- valve in ``_compute_lcom4``). Higher values mean the class splinters
- into unrelated method clusters.
- """
-
- name: str
- start_line: int # 1-indexed
- end_line: int # 1-indexed
- method_count: int
- total_nloc: int
- methods: list[FunctionComplexity]
- lcom4: int = 1
- max_method_ccn: int = 0
- field_count: int = 0
-
-
-@dataclass(frozen=True)
-class ErrorHandlingHit:
- """One error-handling anti-pattern occurrence in a file.
-
- Collected by the walker's whole-tree pass (see
- ``_collect_error_handling``) and consumed by the ``error_handling``
- biomarker. ``kind`` is one of:
-
- - ``swallowed_catch`` — a catch/except whose body has no real handling
- (empty, or only ``pass`` / ``...`` / a docstring / comments).
- - ``bare_except`` — Python ``except:`` / ``except Exception:`` /
- ``except BaseException:`` (catch-all) regardless of body.
- - ``unsafe_unwrap`` — Rust ``.unwrap()`` / ``.expect()`` /
- ``.unwrap_unchecked()`` calls and ``panic!`` / ``unreachable!`` /
- ``todo!`` / ``unimplemented!`` macros (latent panic-on-error).
- - ``go_swallow`` — Go empty ``if err != nil {}`` block, or a
- blank-identifier discard of a multi-return call's value.
- """
-
- kind: str
- line: int # 1-indexed
-
-
-@dataclass(frozen=True)
-class PerfHit:
- """One performance-risk occurrence in a file (the ``performance`` dimension).
-
- Collected by the walker's whole-tree perf pass (see
- ``_collect_perf_hits``) and lifted into findings by the perf biomarkers.
- Precision-first: every hit is loop-body-scoped and execution-sink-gated so
- an unsupported language / parse failure / builder-only call yields nothing.
-
- ``kind`` is one of:
-
- - ``io_in_loop`` — an execution sink at an I/O boundary (db / network /
- filesystem / subprocess) inside a real, data-dependent loop body. The
- boundary kind is carried in ``detail``.
- - ``string_concat_in_loop`` — string accumulation (``+=`` onto a string)
- inside a loop, instead of a buffer / ``join``.
- - ``blocking_sync_in_async`` — a known blocking sync call (``time.sleep`` /
- sync ``requests`` / ``subprocess`` / ``os.system`` / bare ``open``) inside
- an ``async def``, not awaited. ``detail`` carries the offending API.
- - ``resource_construction_in_loop`` — a heavy I/O client / connection
- (``sqlite3.connect`` / ``httpx.Client`` / ``boto3.client`` / ``new
- PrismaClient``) constructed each iteration instead of hoisted.
- - ``lock_in_loop`` — a lock acquired each iteration (``lock.acquire`` /
- ``mu.Lock`` / ``synchronized`` / ``lock(x){}``); a contention signal that
- activates the ``lock`` I/O boundary kind.
- - ``serial_await_in_loop`` — an awaited I/O sink in a loop body (the
- missed-``gather`` / ``Promise.all`` shape). ``detail`` carries the
- boundary kind. Rides alongside ``io_in_loop`` as an advisory co-signal.
- - ``membership_test_against_list_in_loop`` — ``x in big_list`` (or
- ``big_list.includes(x)``) inside a loop where the right operand is a known
- list -> O(n·m); a set would make each probe O(1).
-
- Phase-7b markers (``func_start``-carrying so the centrality gate can resolve
- the enclosing function to a graph symbol node):
-
- - ``nested_loop_with_io`` — an I/O sink in the inner body of a nested loop
- (``loop_depth >= 2``) -> O(n·m) round-trips. Rides alongside ``io_in_loop``;
- the nesting itself raises confidence, so it is NOT centrality-gated.
- - ``nested_loop_quadratic`` — a data-dependent loop nested inside another
- (O(n^2) shape). Emitted as a *candidate*; the centrality gate keeps it
- ONLY in a hot/central function (the gate is the precision fix).
- - ``hot_path_sync_io`` — a blocking (non-awaited) I/O sink at ``loop_depth 0``.
- Emitted as a *candidate*; survives only in a top-centrality function.
- - ``blocking_io_under_lock`` — an I/O sink reached while a block-scoped lock
- (C# ``lock``/Java ``synchronized``) is held. Same-function (sink lexically
- under the lock) is emitted here; the cross-function case is added by
- ``perf.gated.collect_blocking_io_under_lock``.
-
- The Phase-7a markers above are emitted via the dialect ``loop_call_marker`` /
- ``loop_stmt_marker`` hooks (and the inline awaited-sink path for
- ``serial_await_in_loop``), so each is per-language opt-in.
- """
-
- kind: str
- line: int # 1-indexed
- function: str | None = None
- detail: str = ""
- # 1-indexed ``def`` line of the enclosing function (0 = module scope). Lets
- # the centrality gate (Phase 7b) resolve a candidate hit to its graph symbol
- # node without a separate lookup. 0 for hits where it is irrelevant.
- func_start: int = 0
- # Reachability path for a cross-function hit: the resolved symbol-node
- # chain ``A -> B -> ... -> sink`` (PR4). Empty for same-function hits —
- # its emptiness is what distinguishes the two cases downstream.
- path: tuple[str, ...] = ()
-
-
-@dataclass(frozen=True)
-class PerfFnFacts:
- """Per-function facts the cross-function perf bridge needs (PR4).
-
- Collected on the same whole-tree perf pass as ``PerfHit``. Two signals:
-
- - ``loop_call_targets`` — the rightmost names of calls made inside a loop
- body that are NOT themselves a direct I/O sink (those are already a
- same-function ``io_in_loop`` hit). Each is a candidate helper whose body
- might reach a sink transitively — the entry points of the reachability
- walk.
- - ``bare_sink_kind`` — the boundary kind of an I/O sink this function
- executes at ``loop_depth 0`` (its own top level, not nested in a loop),
- else ``None``. A function with a bare sink is a *reachability target*:
- when a loop in another function calls into it, that sink runs once per
- iteration. The ``loop_depth 0`` requirement is deliberate — a sink the
- function runs inside its own loop is already counted there.
-
- ``func_start`` is the 1-indexed line of the enclosing function's ``def``
- (0 for module scope, which maps to the synthetic ``::__module__`` node);
- the bridge resolves it to a graph symbol node by line containment.
-
- ``lock_call_targets`` is the Phase-7b analogue of ``loop_call_targets``: the
- rightmost names of non-sink calls made while a block-scoped lock is held.
- Each is a candidate helper whose body might reach a sink transitively —
- the cross-function ``blocking_io_under_lock`` entry points.
- """
-
- function: str | None
- func_start: int
- # ``(callee_name, call_line)`` pairs for the loop-nested non-sink calls.
- loop_call_targets: tuple[tuple[str, int], ...]
- bare_sink_kind: str | None
- # ``(callee_name, call_line)`` pairs for non-sink calls under a held lock.
- lock_call_targets: tuple[tuple[str, int], ...] = ()
- # Phase-7b centrality-gated facts the engine turns into hits only for a hot
- # function. ``nested_loop_line`` is a representative data-dependent
- # nested-loop site (0 = none -> ``nested_loop_quadratic``);
- # ``blocking_sink_{kind,line}`` is the first non-awaited loop_depth-0 I/O
- # sink (None/0 = none -> ``hot_path_sync_io``).
- nested_loop_line: int = 0
- blocking_sink_kind: str | None = None
- blocking_sink_line: int = 0
-
-
-@dataclass
-class FileComplexity:
- """Walker output for one file: per-function and per-class metrics.
-
- ``walk_file`` returns this; ``walk_file_complexity`` is the
- backward-compatible thin wrapper that returns only ``functions``.
- """
-
- functions: list[FunctionComplexity]
- classes: list[ClassComplexity]
- file_nloc: int = 0
- # Error-handling anti-pattern occurrences (whole-tree pass). Empty when
- # the language is unsupported or parsing failed — "no signal", never a
- # false positive.
- error_handling_hits: list[ErrorHandlingHit] = field(default_factory=list)
- # Performance-risk occurrences (whole-tree perf pass). Empty when the
- # language opts out of the perf pass (no ``call_kinds``) or parsing failed.
- perf_hits: list[PerfHit] = field(default_factory=list)
- # Names imported from an I/O-typed library in this file, mapped to their
- # boundary kind (db / network / filesystem / subprocess / lock). The
- # per-file import bridge; PR4's cross-function reachability consumes it.
- io_boundary_names: dict[str, str] = field(default_factory=dict)
- # Per-function facts feeding PR4's cross-function N+1 detection: which
- # callees each function invokes inside a loop body, and whether the
- # function holds a bare (non-loop) I/O sink. Empty when the language opts
- # out of the perf pass. Consumed by ``perf.crossfn``, not by a biomarker.
- perf_fn_facts: list[PerfFnFacts] = field(default_factory=list)
-
-
-# Leaf node types that carry a declared name at the bottom of a C/C++
-# ``declarator`` chain.
-_DECLARATOR_NAME_KINDS = frozenset(
- {"identifier", "field_identifier", "type_identifier", "qualified_identifier"}
-)
-
-
-def _find_name(node: Node) -> str:
- """Best-effort: return the text of the first identifier child."""
- # Search a couple of common field names first.
- for field_name in ("name", "identifier"):
- child = node.child_by_field_name(field_name)
- if child is not None and child.text is not None:
- return child.text.decode("utf-8", errors="replace")
- # C / C++: the function name is not a direct child but nested inside a
- # ``declarator`` chain (``function_definition → function_declarator →
- # field_identifier``). Languages with a ``name`` field never reach here.
- decl = node.child_by_field_name("declarator")
- hops = 0
- while decl is not None and hops < 6:
- if decl.type in _DECLARATOR_NAME_KINDS and decl.text is not None:
- return decl.text.decode("utf-8", errors="replace")
- decl = decl.child_by_field_name("declarator") or decl.child_by_field_name("name")
- hops += 1
- for child in node.children:
- if (
- child.type in ("identifier", "property_identifier", "field_identifier")
- and child.text is not None
- ):
- return child.text.decode("utf-8", errors="replace")
- return "<anonymous>"
-
-
-def _node_text(node: Node) -> str:
- return (node.text or b"").decode("utf-8", errors="replace")
-
-
-def _find_assigned_lambda_name(node: Node) -> str | None:
- parent = node.parent
- while parent is not None:
- if parent.type == "variable_declarator":
- name = parent.child_by_field_name("name")
- if name is not None and name.text is not None:
- return _node_text(name)
- if parent.type in {"assignment_expression", "assignment_pattern"}:
- left = parent.child_by_field_name("left")
- if left is None:
- left = next((child for child in parent.children if child is not node), None)
- if left is not None and left.text is not None:
- return _node_text(left)
- if parent.type not in {"parenthesized_expression", "as_expression", "satisfies_expression"}:
- return None
- parent = parent.parent
- return None
-
-
-def _find_call_callback_callee(node: Node) -> str | None:
- parent = node.parent
- while parent is not None and parent.type in {
- "parenthesized_expression",
- "as_expression",
- "satisfies_expression",
- }:
- parent = parent.parent
- if parent is None or parent.type != "arguments":
- return None
- call = parent.parent
- if call is None or call.type != "call_expression":
- return None
- callee = call.child_by_field_name("function")
- if callee is None or callee.text is None:
- return None
- return " ".join(_node_text(callee).split())
-
-
-_TEST_SUITE_CALLBACK_CALLEES = frozenset({"describe", "context", "suite"})
-
-
-def _is_test_suite_callback(node: Node, lmap: LanguageNodeMap) -> bool:
- if node.type not in lmap.lambda_kinds:
- return False
- callee_text = _find_call_callback_callee(node)
- if callee_text is None:
- return False
- return any(part in _TEST_SUITE_CALLBACK_CALLEES for part in callee_text.split("."))
-
-
-def _find_function_entry_name(node: Node, lmap: LanguageNodeMap) -> str:
- if node.type not in lmap.lambda_kinds:
- return _find_name(node)
- assigned = _find_assigned_lambda_name(node)
- if assigned:
- return assigned
- if callee := _find_call_callback_callee(node):
- return f"{callee} callback"
- return f"<anonymous@{node.start_point[0] + 1}>"
-
-
-def _count_nloc(node: Node, source: bytes) -> int:
- """Return the count of non-blank lines spanned by *node*."""
- start = node.start_point[0]
- end = node.end_point[0]
- if end < start:
- return 0
- try:
- snippet = source[node.start_byte : node.end_byte].decode("utf-8", errors="replace")
- except Exception:
- return end - start + 1
- return sum(1 for line in snippet.splitlines() if line.strip())
-
-
-def _count_file_nloc(source: bytes) -> int:
- """Count non-blank lines in *source* bytes (plain fallback, no tree)."""
- try:
- text = source.decode("utf-8", errors="replace")
- except Exception:
- return 0
- return sum(1 for line in text.splitlines() if line.strip())
-
-
-def _count_file_nloc_tree(root_node: Node, source: bytes) -> int:
- """Count lines that have at least one non-comment token.
-
- Lines where all content is inside comment nodes are excluded; lines
- with real code plus a trailing comment still count.
- """
- try:
- lines = source.decode("utf-8", errors="replace").splitlines()
- except Exception:
- return 0
- code_lines: set[int] = set()
- stack = [root_node]
- while stack:
- node = stack.pop()
- if "comment" in node.type:
- continue
- if not node.children and node.start_byte < node.end_byte:
- for line in range(node.start_point[0], node.end_point[0] + 1):
- if line < len(lines) and lines[line].strip():
- code_lines.add(line)
- else:
- for child in node.children:
- stack.append(child)
- return len(code_lines)
-
-
-def _is_boolean_operator(node: Node, lmap: LanguageNodeMap) -> bool:
- """True if this node represents a logical ``&&`` / ``||`` operator."""
- if node.type in lmap.boolean_operator_kinds:
- return True
- if node.type in lmap.boolean_operator_text_kinds:
- # The operator child carries the literal token text.
- for child in node.children:
- if child.text is None:
- continue
- tok = child.text
- if tok in (b"&&", b"||", b"and", b"or"):
- return True
- return False
-
-
-_BODY_FIELD_NAMES = (
- "body",
- "consequence",
- "alternative",
- "else_clause",
- "block",
-)
-
-
-def _count_boolean_ops_in_condition(node: Node, lmap: LanguageNodeMap) -> int:
- """Count ``&&`` / ``||`` / ``and`` / ``or`` operators in a condition.
-
- Walks the subtree rooted at *node* but does not descend into nested
- function bodies (lambdas / closures used as condition values are
- rare and would skew the count).
- """
- if node is None:
- return 0
- count = 0
- stack: list[Node] = [node]
- while stack:
- cur = stack.pop()
- if cur is not node and cur.type in lmap.function_kinds:
- continue
- if _is_boolean_operator(cur, lmap):
- count += 1
- for child in cur.children:
- stack.append(child)
- return count
-
-
-def _enclosing_construct(node: Node, lmap: LanguageNodeMap) -> str:
- if node.type in lmap.loop_kinds:
- return "for" if "for" in node.type else "while"
- if node.type in lmap.case_kinds:
- return "case"
- if node.type in lmap.catch_kinds:
- return "catch"
- if node.type in lmap.branch_kinds:
- if "ternary" in node.type or "conditional" in node.type:
- return "ternary"
- return "if"
- return "if"
-
-
-def _condition_subtrees(node: Node) -> list[Node]:
- """Best-effort: pull the *condition* parts out of a branch/loop node.
-
- Prefers the tree-sitter ``condition`` named field where exposed
- (Python, TS, Java, Rust, Go all use it for most branch shapes).
- Falls back to all direct children except recognised body fields and
- syntactic punctuation.
- """
- cond = node.child_by_field_name("condition")
- if cond is not None:
- return [cond]
- # Switch case value (TS, Java)
- value = node.child_by_field_name("value")
- if value is not None and "case" in node.type:
- return [value]
- # Fallback: direct children minus bodies / blocks.
- body_nodes: set[int] = set()
- for fname in _BODY_FIELD_NAMES:
- child = node.child_by_field_name(fname)
- if child is not None:
- body_nodes.add(child.id)
- out: list[Node] = []
- for child in node.children:
- if child.id in body_nodes:
- continue
- if not child.is_named:
- continue
- if child.type in ("block", "compound_statement", "statement_block"):
- continue
- out.append(child)
- return out
-
-
-def _collect_case_children(node: Node, lmap: LanguageNodeMap) -> list[Node]:
- """Collect all case/arm nodes from a switch/match node.
-
- In Rust, ``match_expression`` contains a ``match_block`` which in
- turn holds the ``match_arm`` nodes. Other languages may place cases
- directly under the switch node. This helper handles both layouts.
- """
- cases: list[Node] = []
- for child in node.children:
- if child.type in lmap.case_kinds:
- cases.append(child)
- else:
- # Descend one level into intermediate wrapper nodes (e.g.
- # ``match_block``) that are not themselves control flow.
- for grandchild in child.children:
- if grandchild.type in lmap.case_kinds:
- cases.append(grandchild)
- return cases
-
-
-def _is_flat_match(node: Node, lmap: LanguageNodeMap) -> bool:
- """Return True if *node* is a match/switch with only simple arms.
-
- A "flat" match has arms whose bodies are single expressions without
- nested control flow (no ``if``, ``match``, ``for``, ``while``,
- ``loop``, ``if_let``, ``while_let`` expressions, and no ``block``
- with multiple statements). Flat matches contribute 1 CCN point for
- the match keyword itself but do NOT count each arm individually.
- """
- complex_types = lmap.branch_kinds | lmap.loop_kinds | lmap.switch_kinds
- cases = _collect_case_children(node, lmap)
- if not cases:
- return False
- return all(not _subtree_contains_complex(arm, complex_types) for arm in cases)
-
-
-def _subtree_contains_complex(arm_node: Node, complex_types: frozenset[str]) -> bool:
- """Return True if *arm_node*'s subtree contains complex control flow.
-
- A ``block`` with more than one statement is also considered complex.
- """
- stack: list[Node] = list(arm_node.children)
- while stack:
- cur = stack.pop()
- if cur.type in complex_types:
- return True
- # A block with multiple named children (statements) is complex.
- if cur.type == "block":
- named_children = [c for c in cur.children if c.is_named]
- if len(named_children) > 1:
- return True
- for child in cur.children:
- stack.append(child)
- return False
-
-
-def _walk_function_body(
- body_node: Node,
- lmap: LanguageNodeMap,
-) -> tuple[int, int, int, int, list[ConditionComplexity]]:
- """Recursive AST walk. Returns (ccn, max_nesting, cognitive, bumps,
- complex_conditions).
-
- Starts CCN at 1 (the entry path). Nested function bodies are
- skipped — they will (or already did) produce their own
- ``FunctionComplexity``.
-
- ``bumps`` counts how many *direct* children of the function body
- contain nested control flow that reaches a depth of ≥ 2. A function
- with several heavy independent branches is "bumpy" in
- CodeScene/SonarSource terminology.
-
- ``complex_conditions`` is an additive side-channel — collected for
- every branch/loop/case construct encountered. The CCN / cognitive
- accumulation logic is unchanged.
- """
-
- ccn = 1
- max_nesting = 0
- cognitive = 0
- bumps = 0
- conditions: list[ConditionComplexity] = []
-
- # Track match_expression nodes identified as "flat" so their arms
- # are not individually counted as branch points.
- flat_match_ids: set[int] = set()
-
- def _recurse(node: Node, depth: int) -> None:
- nonlocal ccn, max_nesting, cognitive
-
- # Don't descend into nested function bodies — they're walked
- # separately at the top level. Lambdas / arrow functions DO
- # contribute to the enclosing function's complexity.
- if node.type in lmap.function_kinds:
- return
-
- nesting_increment = 0
- ccn_increment = 0
-
- # Check if this is a case/arm inside a flat match — skip it.
- # In Rust the parent chain is match_arm → match_block → match_expression,
- # so we check both parent and grandparent.
- _parent = node.parent
- is_flat_match_arm = False
- if (
- node.type in lmap.case_kinds
- and _parent is not None
- and (
- _parent.id in flat_match_ids
- or (_parent.parent is not None and _parent.parent.id in flat_match_ids)
- )
- ):
- is_flat_match_arm = True
-
- if is_flat_match_arm:
- # Flat match arms: no CCN increment, no nesting increment.
- pass
- elif (
- node.type in lmap.branch_kinds
- or node.type in lmap.loop_kinds
- or node.type in lmap.case_kinds
- or node.type in lmap.catch_kinds
- ):
- ccn_increment = 1
- nesting_increment = 1
- # Side-channel: count compound boolean ops in this
- # construct's condition. Does not affect ccn/cognitive
- # (boolean operators are still tallied independently by
- # the regular recursion below).
- op_count = 0
- for sub in _condition_subtrees(node):
- op_count += _count_boolean_ops_in_condition(sub, lmap)
- if op_count > 0:
- conditions.append(
- ConditionComplexity(
- line=node.start_point[0] + 1,
- operator_count=op_count,
- enclosing_construct=_enclosing_construct(node, lmap),
- )
- )
- elif node.type in lmap.try_kinds:
- # TRY opens a nesting level but does not branch on its own.
- nesting_increment = 1
- elif node.type in lmap.switch_kinds:
- # Detect flat match: all arms are simple single-expression arms.
- if _is_flat_match(node, lmap):
- flat_match_ids.add(node.id)
- # Flat match: count 1 CCN point for the match itself,
- # open a nesting level, but arms won't be counted.
- ccn_increment = 1
- # Switch opens nesting; each case contributes its own +1.
- nesting_increment = 1
- elif _is_boolean_operator(node, lmap):
- ccn_increment = 1
-
- ccn += ccn_increment
- new_depth = depth + nesting_increment
- if nesting_increment:
- # SonarSource cognitive: each nesting node adds (1 + depth).
- cognitive += 1 + depth
- elif ccn_increment:
- # Flat +1 for boolean operators (no nesting impact).
- cognitive += 1
-
- if new_depth > max_nesting:
- max_nesting = new_depth
-
- for child in node.children:
- _recurse(child, new_depth)
-
- for child in body_node.children:
- # Per-child peak depth: temporarily swap max_nesting out so we
- # can read just this child's contribution, then restore.
- outer_max = max_nesting
- max_nesting = 0
- _recurse(child, 0)
- child_peak = max_nesting
- max_nesting = max(outer_max, child_peak)
- if child_peak >= 2:
- bumps += 1
-
- return ccn, max_nesting, cognitive, bumps, conditions
-
-
-# ----------------------------------------------------------------------
-# Assertion-block detection (test-quality smells)
-# ----------------------------------------------------------------------
-
-# Callee-name prefixes that mark a call as a test assertion. Matched
-# case-insensitively against every identifier in the call's callee chain,
-# so ``assertEqual`` / ``assert_eq`` / ``Assert.assertTrue`` / ``expect``
-# all qualify. Deliberately small — these two prefixes cover xUnit-family
-# (``assert*``) and the BDD/expect family (``expect(...)``).
-_ASSERT_CALL_PREFIXES = ("assert", "expect")
-_EXPRESSION_STATEMENT = "expression_statement"
-_AWAIT_WRAPPER_KINDS = ("await_expression", "await", "parenthesized_expression")
-
-
-def _callee_matches_assert(call_node: Node) -> bool:
- """True if any identifier in *call_node*'s callee chain is assert-ish.
-
- Only the callee (the ``function`` / ``macro`` field) is inspected, not
- the arguments — so ``foo(assertion)`` does not match while
- ``expect(x).toBe(y)`` and ``self.assertEqual(...)`` do.
- """
- callee = call_node.child_by_field_name("function") or call_node.child_by_field_name("macro")
- # Fallback when no ``function``/``macro`` field is exposed: the first
- # named child is usually the callee.
- roots = [callee] if callee is not None else [c for c in call_node.children if c.is_named][:1]
- stack: list[Node] = list(roots)
- while stack:
- node = stack.pop()
- if node.type.endswith(_IDENTIFIER_SUFFIX) and node.text is not None:
- name = node.text.decode("utf-8", errors="replace").lower()
- if any(name.startswith(p) for p in _ASSERT_CALL_PREFIXES):
- return True
- for child in node.children:
- stack.append(child)
- return False
-
-
-def _find_assert_call(stmt: Node, kinds: frozenset[str]) -> Node | None:
- """Find an assertion-call node that is *stmt*'s own expression.
-
- Searches direct named children and one level deeper (to see through
- ``await`` / parenthesis wrappers) — but no further, so a call buried in
- an argument or a nested block is not mistaken for the statement's
- expression.
- """
- for child in stmt.children:
- if not child.is_named:
- continue
- if child.type in kinds:
- return child
- if child.type in _AWAIT_WRAPPER_KINDS:
- for gc in child.children:
- if gc.is_named and gc.type in kinds:
- return gc
- return None
-
-
-def _is_assertion_statement(stmt: Node, lmap: LanguageNodeMap) -> bool:
- """True if *stmt* is a test assertion (bare ``assert`` or assert call)."""
- if stmt.type in lmap.assert_kinds:
- return True
- if not lmap.assert_call_kinds:
- return False
- # Some grammars (Kotlin) have no ``expression_statement`` wrapper — the
- # call node sits directly in the statement list. Match it as the
- # statement itself. (Wrapper languages never hit this: their call nodes
- # only ever appear as the single child of an ``expression_statement``,
- # so they can't form a run of ≥2 at this level.)
- if stmt.type in lmap.assert_call_kinds:
- return _callee_matches_assert(stmt)
- if stmt.type != _EXPRESSION_STATEMENT:
- return False
- call = _find_assert_call(stmt, lmap.assert_call_kinds)
- return call is not None and _callee_matches_assert(call)
-
-
-def _collect_assertion_blocks(body_node: Node, lmap: LanguageNodeMap) -> list[tuple[int, int, int]]:
- """Runs of ≥2 consecutive assertion statements within a function body.
-
- Each run is recorded as ``(start_line, end_line, count)``. Runs are
- found per statement-list (a block's direct children), so an assertion
- sequence broken by a non-assertion statement starts a new run. Nested
- function bodies are skipped — their assertions belong to them.
- """
- if not lmap.assert_kinds and not lmap.assert_call_kinds:
- return []
- blocks: list[tuple[int, int, int]] = []
-
- def _scan_siblings(parent: Node) -> None:
- run_start = 0
- run_end = 0
- run_count = 0
- for child in parent.children:
- if not child.is_named:
- continue
- if _is_assertion_statement(child, lmap):
- if run_count == 0:
- run_start = child.start_point[0] + 1
- run_end = child.end_point[0] + 1
- run_count += 1
- else:
- if run_count >= 2:
- blocks.append((run_start, run_end, run_count))
- run_count = 0
- if run_count >= 2:
- blocks.append((run_start, run_end, run_count))
-
- def _visit(node: Node) -> None:
- _scan_siblings(node)
- for child in node.children:
- if child.type in lmap.function_kinds:
- continue # nested fn — walked as its own entry
- _visit(child)
-
- _visit(body_node)
- return blocks
-
-
-def _collect_function_nodes(root: Node, lmap: LanguageNodeMap) -> list[Node]:
- """All function / method definition nodes in the file.
-
- Iterative pre-order traversal. We descend into class / module
- bodies but do **not** recurse below a function or lambda. Lambdas found
- before any function boundary are module-level executable units (for
- example route callbacks) and get their own entry; lambdas inside an
- already-collected function still roll up into that function.
- """
- out: list[Node] = []
- stack: list[Node] = [root]
- while stack:
- node = stack.pop()
- if node.type in lmap.lambda_kinds and _is_test_suite_callback(node, lmap):
- stack.extend(node.children)
- continue
- if node.type in lmap.function_kinds or node.type in lmap.lambda_kinds:
- out.append(node)
- continue
- for child in node.children:
- stack.append(child)
- return out
-
-
-# ----------------------------------------------------------------------
-# Error-handling anti-pattern detection (error_handling biomarker)
-# ----------------------------------------------------------------------
-# Ported from the bench-validated detector (24/24 fixtures across 11
-# languages). Precision-first: every detector targets the unambiguous
-# shape and degrades to "no signal" rather than guessing.
-
-# Block-like body node types of a catch/except clause.
-_EH_BLOCK_KINDS = frozenset({"block", "statement_block", "compound_statement"})
-# Statement node types that count as "no real handling" inside a catch body.
-_EH_TRIVIAL_STMT = frozenset({"comment", "pass_statement", "line_comment", "block_comment"})
-# Rust: each of these is a latent panic-on-error.
-_RUST_UNWRAP_METHODS = frozenset({"unwrap", "expect", "unwrap_unchecked"})
-_RUST_PANIC_MACROS = frozenset({"panic", "unreachable", "todo", "unimplemented"})
-
-
-def _eh_text(node: Node) -> str:
- return (node.text or b"").decode("utf-8", errors="replace")
-
-
-def _eh_named(node: Node) -> list[Node]:
- return [c for c in node.children if c.is_named]
-
-
-def _eh_find_body_block(clause: Node) -> Node | None:
- """The block-like child of a catch/except clause (its handler body)."""
- for c in clause.children:
- if c.type in _EH_BLOCK_KINDS:
- return c
- # Kotlin catch_block / some grammars nest the block one level down.
- for c in clause.children:
- for g in c.children:
- if g.type in _EH_BLOCK_KINDS:
- return g
- return None
-
-
-def _eh_is_trivial_stmt(stmt: Node, language: str) -> bool:
- if stmt.type in _EH_TRIVIAL_STMT:
- return True
- if language == "python" and stmt.type == "expression_statement":
- inner = _eh_named(stmt)
- if not inner:
- return True
- # ``...`` or a docstring as the entire statement.
- if inner[0].type in ("ellipsis", "string"):
- return True
- return False
-
-
-def _eh_body_is_swallowed(block: Node, language: str) -> bool:
- real = [c for c in _eh_named(block) if not _eh_is_trivial_stmt(c, language)]
- return len(real) == 0
-
-
-def _eh_is_bare_except(clause: Node) -> bool:
- """Python ``except:`` / ``except Exception:`` / ``except BaseException:``."""
- kids = [c for c in clause.children if c.type != "comment"]
- after = [c for c in kids if c.type not in ("except", ":") and c.type not in _EH_BLOCK_KINDS]
- if not after:
- return True # bare ``except:``
- # ``except Exception:`` / ``except BaseException:`` (single catch-all
- # identifier — a tuple of specific types or an ``as`` binding on a
- # specific type does not match).
- first = after[0]
- return first.type == "identifier" and _eh_text(first) in ("Exception", "BaseException")
-
-
-def _eh_rust_hit(node: Node) -> bool:
- """True when *node* is an unwrap/expect call or a panic-family macro."""
- if node.type == "call_expression":
- fn = node.child_by_field_name("function")
- if fn is not None and fn.type == "field_expression":
- fld = fn.child_by_field_name("field")
- return fld is not None and _eh_text(fld) in _RUST_UNWRAP_METHODS
- return False
- if node.type == "macro_invocation":
- mac = node.child_by_field_name("macro")
- return mac is not None and _eh_text(mac) in _RUST_PANIC_MACROS
- return False
-
-
-def _eh_go_cond_is_err_check(cond_text: str) -> bool:
- t = cond_text.replace(" ", "")
- return "err!=nil" in t or "err==nil" in t
-
-
-def _eh_go_hit(node: Node) -> bool:
- """Go: empty ``if err != nil {}`` or blank-identifier discard of a call."""
- if node.type == "if_statement":
- cond = node.child_by_field_name("condition")
- cons = node.child_by_field_name("consequence")
- return (
- cond is not None
- and cons is not None
- and _eh_go_cond_is_err_check(_eh_text(cond))
- and len(_eh_named(cons)) == 0
- )
- if node.type in ("short_var_declaration", "assignment_statement"):
- left = node.child_by_field_name("left")
- right = node.child_by_field_name("right")
- if left is None or right is None:
- return False
- left_kids = _eh_named(left)
- has_blank = any(c.type == "blank_identifier" or _eh_text(c) == "_" for c in left_kids)
- right_is_call = any(c.type == "call_expression" for c in _eh_named(right)) or (
- right.type == "expression_list"
- and any(c.type == "call_expression" for c in _eh_named(right))
- )
- # Multi-return discard: ≥2 LHS targets, a call on the RHS, a blank present.
- return has_blank and len(left_kids) >= 2 and right_is_call
- return False
-
-
-def _collect_error_handling(
- root: Node, language: str, lmap: LanguageNodeMap
-) -> list[ErrorHandlingHit]:
- """Whole-tree pass: every error-handling anti-pattern with its line.
-
- Catch-clause shapes reuse the ``LanguageNodeMap`` catch kinds (Python
- ``except_clause``; JS/TS/Java/C++/C# ``catch_clause``; Kotlin
- ``catch_block``); Rust and Go have no catch nodes and use their own
- recognizers. Module-level code is covered too — anti-patterns are not
- confined to function bodies.
- """
- hits: list[ErrorHandlingHit] = []
- catch_kinds = lmap.catch_kinds
- is_python = language == "python"
- is_rust = language == "rust"
- is_go = language == "go"
- stack: list[Node] = [root]
- while stack:
- node = stack.pop()
- if catch_kinds and node.type in catch_kinds:
- block = _eh_find_body_block(node)
- if block is not None and _eh_body_is_swallowed(block, language):
- hits.append(ErrorHandlingHit("swallowed_catch", node.start_point[0] + 1))
- if is_python and _eh_is_bare_except(node):
- hits.append(ErrorHandlingHit("bare_except", node.start_point[0] + 1))
- elif is_rust and _eh_rust_hit(node):
- hits.append(ErrorHandlingHit("unsafe_unwrap", node.start_point[0] + 1))
- elif is_go and _eh_go_hit(node):
- hits.append(ErrorHandlingHit("go_swallow", node.start_point[0] + 1))
- stack.extend(node.children)
- hits.sort(key=lambda h: h.line)
- return hits
-
-
-# ----------------------------------------------------------------------
-# Performance-risk detection (io_in_loop / string_concat_in_loop /
-# blocking_sync_in_async — the ``performance`` health dimension)
-# ----------------------------------------------------------------------
-# One whole-tree pass mirroring ``_collect_error_handling`` but carrying the
-# per-node context the perf signal needs: loop depth, in-async, and the
-# enclosing function name. Two non-negotiable refinements (Phase-0 gate: they
-# took precision from 49% to 79%) are baked in:
-# 1. Loop-BODY scoping — only calls under a loop node's ``body`` field run
-# per-iteration; a call in the ``for x in <iterable>`` header runs once.
-# 2. Constant-bound-loop skip — ``for _ in range(<int literals>)`` and loops
-# over literal / ALL_CAPS-named-constant collections are not data-dependent.
-
-# Marker kinds emitted through each dialect hook. The walker consults a
-# dialect's ``markers`` set against these to decide whether to invoke the hook
-# at all, so a dialect that lists none of a hook's kinds never pays for it.
-_LOOP_CALL_MARKER_KINDS = frozenset(
- {
- "regex_compile_in_loop",
- "resource_construction_in_loop",
- "lock_in_loop",
- "membership_test_against_list_in_loop",
- # Phase 7d — language-specific call markers.
- "list_insert_zero_in_loop",
- "pd_concat_in_loop",
- "json_parse_in_loop",
- }
+from .assertions import _collect_assertion_blocks
+from .ast_utils import (
+ _collect_function_nodes,
+ _count_parameters,
+ _find_function_entry_name,
)
-_LOOP_STMT_MARKER_KINDS = frozenset(
- {
- "defer_in_loop",
- "resource_construction_in_loop",
- "lock_in_loop",
- "membership_test_against_list_in_loop",
- # Phase 7d — language-specific statement markers.
- "goroutine_in_unbounded_loop",
- }
+from .class_analysis import _collect_classes
+from .cyclomatic import _walk_function_body
+from .error_handling import _collect_error_handling
+from .languages import get_language_map
+
+# Re-exported so the package façade (``__init__``) and downstream consumers
+# keep importing the output schema from ``complexity.walker`` unchanged.
+from .models import (
+ ClassComplexity,
+ ConditionComplexity,
+ ErrorHandlingHit,
+ FileComplexity,
+ FunctionComplexity,
+ PerfFnFacts,
+ PerfHit,
)
-# Markers for a call that is its OWN iteration construct (``.reduce``), a perf
-# smell at any loop depth — emitted via ``dialect.bare_call_marker``.
-_BARE_CALL_MARKER_KINDS = frozenset({"array_spread_in_reduce"})
-# Markers fired on a loop whose ITERABLE is itself a slow call (``df.iterrows()``)
-# — the call lives in the loop header, so the body markers miss it. Emitted via
-# ``dialect.loop_iterable_call_marker`` against the loop node.
-_LOOP_ITERABLE_CALL_MARKER_KINDS = frozenset({"pandas_iterrows_in_loop"})
-
-# Boundary kinds that are *unambiguously blocking* when called synchronously, so
-# a loop_depth-0 occurrence in a hot function is a ``hot_path_sync_io`` candidate.
-# Limited to ``subprocess`` + ``filesystem`` on purpose:
-# * ``db`` — in an async codebase DB access is awaited (non-blocking), and the
-# un-awaited DB calls a static pass sees are almost all result *materializers*
-# (``result.scalars().all()`` / ``.scalar_one()``) on an already-awaited
-# result, not a round-trip (a probe-confirmed FP generator).
-# * ``network`` — async HTTP is awaited too; the walker's ``awaited`` test only
-# inspects the immediate parent, so a wrapped/chained await
-# (``(await client.GetAsync(u)).X``) can read as non-awaited and FP. Every
-# hot-path TP on the OSS corpus was subprocess/fs (0 network), so excluding
-# it costs no measured precision and removes that FP class. (A wrapper-aware
-# ``awaited`` test could re-admit sync ``requests`` — tracked as a follow-up.)
-# subprocess (``subprocess.run`` / ``os.system``) and filesystem (bare ``open``)
-# are synchronous by construction, with no await semantics to misread.
-_HOT_PATH_SINK_KINDS = frozenset({"subprocess", "filesystem"})
-
-# Block node kinds that form the *body* of a lock construct (C# ``lock (x) {…}``
-# / Java ``synchronized (x) {…}``). Only the body runs with the lock held, so
-# ``lock_depth`` is raised for the body child only — a sink in the lock-object
-# expression (``synchronized(repo.find(id)){…}``) runs BEFORE the lock is taken.
-_LOCK_BODY_KINDS = frozenset({"block", "statement_block", "compound_statement"})
-
-
-def _perf_func_name(node: Node) -> str | None:
- nm = node.child_by_field_name("name")
- if nm is not None and nm.text:
- return nm.text.decode("utf-8", "replace")
- return None
-
-
-def _enclosing_loop_iterables(
- node: Node, dialect: BasePerfDialect, loop_kinds: frozenset[str], fn_kinds: frozenset[str]
-) -> set[str]:
- """Names of the collections every enclosing loop (up to the function bound)
- iterates over — the lookup the same-collection ``nested_loop_quadratic``
- shape gate compares the inner loop's iterable against."""
- names: set[str] = set()
- cur = node.parent
- for _ in range(64):
- if cur is None or cur.type in fn_kinds:
- break
- if cur.type in loop_kinds:
- nm = dialect.loop_iterable_name(cur)
- if nm:
- names.add(nm)
- cur = cur.parent
- return names
-
-
-def _collect_perf_hits(
- root: Node, language: str, lmap: LanguageNodeMap
-) -> tuple[list[PerfHit], dict[str, str], list[PerfFnFacts]]:
- """Whole-tree perf pass → ``(hits, io_boundary_names, fn_facts)``.
-
- Iterative DFS carrying ``(node, loop_depth, in_async, func_name,
- func_start, lock_depth)`` — the proven Phase-0 shape, extended with the
- enclosing function's start line (so per-function facts key to a graph symbol
- node) and a block-scoped ``lock_depth`` (so an I/O sink under a held lock is
- flagged). Loop-body scoping and constant-loop skipping are applied so only
- genuinely per-iteration calls are flagged. Returns nothing for languages
- that opt out of the perf pass (empty ``call_kinds``).
- Alongside the same-function ``hits``, it accumulates ``fn_facts`` (one
- :class:`PerfFnFacts` per enclosing function that has any loop-nested call
- or a bare sink) — the input to PR4's cross-function reachability.
- """
- call_kinds = lmap.call_kinds
- dialect = PERF_DIALECTS.get(language)
- if not call_kinds or dialect is None:
- return [], {}, []
-
- io_names = collect_io_names(root, language)
- has_db_import = any(k == "db" for k in io_names.values())
- markers = dialect.markers
- do_string_concat = "string_concat_in_loop" in markers
- do_blocking = "blocking_sync_in_async" in markers
- do_loop_call_marker = bool(markers & _LOOP_CALL_MARKER_KINDS)
- do_loop_stmt_marker = bool(markers & _LOOP_STMT_MARKER_KINDS)
- do_bare_call_marker = bool(markers & _BARE_CALL_MARKER_KINDS)
- do_loop_iterable_call_marker = bool(markers & _LOOP_ITERABLE_CALL_MARKER_KINDS)
- do_serial_await = "serial_await_in_loop" in markers
- # Phase 7b markers.
- do_nested_io = "nested_loop_with_io" in markers
- do_nested_quadratic = "nested_loop_quadratic" in markers
- do_hot_path = "hot_path_sync_io" in markers
- do_lock_io = "blocking_io_under_lock" in markers
- # ``list_names`` is the precision gate for the membership marker; compute it
- # once per file only when this dialect can emit that marker, then thread it
- # to the loop-marker hooks (which ignore it for every other marker).
- list_names = (
- dialect.list_bound_names(root)
- if "membership_test_against_list_in_loop" in markers
- else frozenset()
- )
- loop_kinds = lmap.loop_kinds
- fn_kinds = lmap.function_kinds
- lambda_kinds = lmap.lambda_kinds
- async_fn_kinds = lmap.async_function_kinds
-
- hits: list[PerfHit] = []
- # Per-enclosing-function accumulators keyed by the function's start line
- # (0 = module scope): ``(name, loop_targets{name->line}, lock_targets{...},
- # [bare_sink], misc)``. loop/lock targets feed the cross-function
- # reachability passes; the bare-sink slot makes the function a reachability
- # target. ``misc`` carries the Phase-7b *centrality-gated* facts the engine
- # turns into hits only for hot functions (kept out of ``hits`` so the raw
- # walker output stays the same high-precision same-function set):
- # misc[0] = nested_loop_line (a data-dependent loop nested in another)
- # misc[1] = blocking_sink_kind (first non-awaited loop_depth-0 sink kind)
- # misc[2] = blocking_sink_line
- fn_acc: dict[
- int, tuple[str | None, dict[str, int], dict[str, int], list[str | None], list]
- ] = {}
-
- def _acc(
- func_start: int, func_name: str | None
- ) -> tuple[dict[str, int], dict[str, int], list[str | None], list]:
- entry = fn_acc.get(func_start)
- if entry is None:
- entry = (func_name, {}, {}, [None], [0, None, 0])
- fn_acc[func_start] = entry
- return entry[1], entry[2], entry[3], entry[4]
-
- # (node, loop_depth, in_async, func_name, func_start, lock_depth, outer_iter)
- # ``outer_iter`` is whether the OUTERMOST enclosing loop iterates a collection
- # (vs a ``while``/cursor) — the precision gate for ``nested_loop_with_io``.
- stack: list[tuple[Node, int, bool, str | None, int, int, bool]] = [
- (root, 0, False, None, 0, 0, True)
- ]
- while stack:
- node, loop_depth, in_async, func_name, func_start, lock_depth, outer_iter = stack.pop()
- t = node.type
-
- is_loop = t in loop_kinds
- if is_loop and dialect.is_constant_loop(node):
- is_loop = False
-
- entering_fn = t in fn_kinds or t in lambda_kinds
- is_async_fn = t in async_fn_kinds or (entering_fn and dialect.is_async_fn(node))
- next_async = True if is_async_fn else (False if entering_fn else in_async)
- next_func = func_name
- next_start = func_start
- if t in fn_kinds:
- next_func = _perf_func_name(node) or func_name
- next_start = node.start_point[0] + 1
-
- # A data-dependent loop nested inside another (``loop_depth`` only counts
- # non-constant loops) is an O(n^2) shape. Record the site as a fact; the
- # engine emits ``nested_loop_quadratic`` only when the function is hot
- # (centrality gate), so the noisy-everywhere shape never ships ungated.
- if is_loop and loop_depth >= 1 and do_nested_quadratic:
- # Shape gate (Phase-7d): raw nesting depth was ~3-20% precision in
- # every language. Fire only on the SAME-COLLECTION shape — the inner
- # loop iterates the same named collection as an enclosing loop
- # (all-pairs O(n^2)) — which all four Phase-7c labelers converged on.
- nm = dialect.loop_iterable_name(node)
- if nm and nm in _enclosing_loop_iterables(node, dialect, loop_kinds, fn_kinds):
- misc = _acc(next_start, next_func)[3]
- if misc[0] == 0:
- misc[0] = node.start_point[0] + 1
-
- # A loop whose ITERABLE is itself a slow call (``for _, r in
- # df.iterrows()``): the call sits in the loop header (runs once), so the
- # body call markers never see it. Fire on the loop node itself, at any
- # nesting depth — ``iterrows`` is O(n)-boxing slow on its own.
- if is_loop and do_loop_iterable_call_marker:
- icm = dialect.loop_iterable_call_marker(node)
- if icm is not None:
- hits.append(
- PerfHit(icm, node.start_point[0] + 1, next_func, "", func_start=next_start)
- )
-
- if t in call_kinds:
- method = dialect.callee_method_name(node) or ""
- root_name = dialect.callee_root_name(node) or ""
- parent = node.parent
- awaited = parent is not None and "await" in parent.type
- line = node.start_point[0] + 1
- if do_bare_call_marker:
- # A call that is its own iteration construct (``.reduce`` with an
- # accumulator spread) — a perf smell at any loop depth.
- bare = dialect.bare_call_marker(root_name, method, node)
- if bare is not None:
- hits.append(PerfHit(bare, line, next_func, "", func_start=next_start))
- kind = dialect.sink_kind(
- root_name,
- method,
- awaited=awaited,
- is_attribute=dialect.callee_is_attribute(node),
- io_names=io_names,
- has_db_import=has_db_import,
- )
- if kind is not None:
- if loop_depth >= 1:
- hits.append(PerfHit("io_in_loop", line, next_func, kind, func_start=next_start))
- if do_serial_await and awaited:
- # An *awaited* sink in a loop body is additionally a
- # missed-concurrency candidate (a serial round-trip that
- # a ``gather`` / ``Promise.all`` could fan out). Advisory
- # co-signal alongside ``io_in_loop``; ``detail`` carries
- # the boundary kind for the finding.
- hits.append(
- PerfHit(
- "serial_await_in_loop", line, next_func, kind, func_start=next_start
- )
- )
- if do_nested_io and loop_depth >= 2 and outer_iter:
- # A sink in the inner body of a NESTED loop -> O(n·m)
- # round-trips. Nesting raises confidence, so it ships
- # un-gated alongside ``io_in_loop`` — but only when the
- # OUTER loop iterates a collection (``outer_iter``): a
- # ``while`` pagination cursor wrapping an inner ``for ...
- # of chunk`` is ``io_in_loop``, not a nested explosion
- # (Phase-7c TS while-cursor FP).
- hits.append(
- PerfHit(
- "nested_loop_with_io", line, next_func, kind, func_start=next_start
- )
- )
- else:
- # A sink at loop_depth 0 makes this function a reachability
- # target: a loop elsewhere calling into it runs the sink N
- # times (cross-function N+1).
- _lt, _kt, sink_slot, misc = _acc(next_start, next_func)
- if sink_slot[0] is None:
- sink_slot[0] = kind
- if (
- do_hot_path
- and not awaited
- and misc[1] is None
- and kind in _HOT_PATH_SINK_KINDS
- ):
- # An inherently-blocking (non-awaited subprocess / fs /
- # sync-network) sink outside any loop. Noisy everywhere,
- # so record it as a fact; the engine emits
- # ``hot_path_sync_io`` only for a hot, request-reachable
- # function (centrality gate). ``db`` is excluded — see
- # ``_HOT_PATH_SINK_KINDS``.
- misc[1] = kind
- misc[2] = line
- if do_lock_io and lock_depth >= 1:
- # A sink reached while a block-scoped lock is held: the
- # round-trip runs under the lock, serializing every thread.
- hits.append(
- PerfHit(
- "blocking_io_under_lock", line, next_func, kind, func_start=next_start
- )
- )
- else:
- if loop_depth >= 1:
- marker = (
- dialect.loop_call_marker(root_name, method, node, list_names)
- if do_loop_call_marker
- else None
- )
- if marker is not None:
- hits.append(PerfHit(marker, line, next_func, "", func_start=next_start))
- elif method:
- # A loop-nested call to a non-sink helper: a candidate
- # entry for cross-function reachability (PR4). Keep the
- # first line we see the helper called at, for the finding.
- targets = _acc(next_start, next_func)[0]
- if method not in targets:
- targets[method] = line
- if do_lock_io and lock_depth >= 1 and method:
- # A non-sink call under a held lock: a candidate entry for the
- # cross-function ``blocking_io_under_lock`` reachability pass.
- lock_targets = _acc(next_start, next_func)[1]
- if method not in lock_targets:
- lock_targets[method] = line
- if do_blocking and in_async and not awaited:
- api = dialect.blocking_sync_api(root_name, method)
- if api is not None:
- hits.append(
- PerfHit(
- "blocking_sync_in_async", line, next_func, api, func_start=next_start
- )
- )
- else:
- if do_blocking and in_async:
- # A non-call member read that blocks in async (C# ``task.Result``).
- mem = dialect.async_blocking_member(node)
- if mem is not None:
- hits.append(
- PerfHit(
- "blocking_sync_in_async",
- node.start_point[0] + 1,
- next_func,
- mem,
- func_start=next_start,
- )
- )
- if loop_depth >= 1:
- if do_string_concat and dialect.is_string_concat(node):
- hits.append(
- PerfHit(
- "string_concat_in_loop",
- node.start_point[0] + 1,
- next_func,
- "",
- func_start=next_start,
- )
- )
- elif do_loop_stmt_marker:
- sm = dialect.loop_stmt_marker(node, list_names)
- if sm is not None:
- hits.append(
- PerfHit(
- sm,
- node.start_point[0] + 1,
- next_func,
- "",
- func_start=next_start,
- )
- )
-
- # A block-scoped lock (``lock``/``synchronized``) opens a held region.
- # Only its BLOCK body runs with the lock held — a sink in the lock-object
- # expression (``synchronized(repo.find(id)){…}``) runs before the lock is
- # taken — so ``lock_depth`` is raised per-child, for the body block only.
- entering_lock = do_lock_io and dialect.is_lock_scope(node)
-
- if is_loop:
- # The outermost loop in a nest fixes ``outer_iter``; deeper loops
- # inherit it. Set when entering the first loop (loop_depth 0 -> 1).
- next_outer_iter = outer_iter if loop_depth >= 1 else dialect.is_iteration_loop(node)
- # Only the loop BODY runs per-iteration; the ``for x in <iterable>``
- # header / ``while <cond>`` condition runs once.
- body = node.child_by_field_name("body")
- if body is not None:
- # NB: tree-sitter Node wrappers are not singletons, so compare
- # with ``==`` (identity by tree + byte range), never ``is``.
- for c in node.children:
- cd = loop_depth + 1 if c == body else loop_depth
- stack.append(
- (c, cd, next_async, next_func, next_start, lock_depth, next_outer_iter)
- )
- else:
- for c in node.children:
- stack.append(
- (
- c,
- loop_depth + 1,
- next_async,
- next_func,
- next_start,
- lock_depth,
- next_outer_iter,
- )
- )
- elif entering_lock:
- # Raise lock_depth for the body block only (not the lock-object expr).
- for c in node.children:
- cl = lock_depth + 1 if c.type in _LOCK_BODY_KINDS else lock_depth
- stack.append((c, loop_depth, next_async, next_func, next_start, cl, outer_iter))
- else:
- for c in node.children:
- stack.append(
- (c, loop_depth, next_async, next_func, next_start, lock_depth, outer_iter)
- )
-
- # Dedup chained sinks: ``result.scalars().all()`` parses as two call nodes
- # on one line (the ``.scalars()`` sink and the ``.all()`` materializer) —
- # one logical query, one finding. Collapse per (kind, line, function).
- seen: set[tuple[str, int, str | None]] = set()
- deduped: list[PerfHit] = []
- for h in hits:
- key = (h.kind, h.line, h.function)
- if key in seen:
- continue
- seen.add(key)
- deduped.append(h)
- deduped.sort(key=lambda h: (h.line, h.kind))
-
- fn_facts = [
- PerfFnFacts(
- function=name,
- func_start=start,
- loop_call_targets=tuple(sorted(loop_targets.items())),
- bare_sink_kind=sink[0],
- lock_call_targets=tuple(sorted(lock_targets.items())),
- nested_loop_line=misc[0],
- blocking_sink_kind=misc[1],
- blocking_sink_line=misc[2],
- )
- for start, (name, loop_targets, lock_targets, sink, misc) in fn_acc.items()
- if (loop_targets or lock_targets or sink[0] is not None or misc[0] or misc[1] is not None)
- ]
- fn_facts.sort(key=lambda f: f.func_start)
- return deduped, io_names, fn_facts
-
-
-# ----------------------------------------------------------------------
-# Class-level analysis (LCOM4 / god-class)
-# ----------------------------------------------------------------------
-
-_PROP_FIELD_NAMES = ("property", "attribute", "field", "name")
-# ``expression`` is C#'s receiver field on ``member_access_expression`` (its
-# ``this`` token is unnamed, so the positional fallback would pick the member).
-_OBJECT_FIELD_NAMES = ("object", "value", "argument", "operand", "expression")
-_IDENTIFIER_SUFFIX = "identifier"
-
-
-def _class_name(node: Node) -> str:
- """Best-effort class/impl name.
-
- Tries the ``name`` field (most class grammars), then ``type`` (Rust's
- ``impl T`` exposes the implemented type there), then the generic
- identifier scan.
- """
- for field_name in ("name", "type"):
- child = node.child_by_field_name(field_name)
- if child is not None and child.text is not None:
- return child.text.decode("utf-8", errors="replace")
- return _find_name(node)
-
-
-def _collect_class_nodes(root: Node, lmap: LanguageNodeMap) -> list[Node]:
- """All class-like grouping nodes in the file (pre-order).
-
- Descends through the whole tree so nested classes are found too;
- each becomes its own ``ClassComplexity``.
- """
- out: list[Node] = []
- stack: list[Node] = [root]
- while stack:
- node = stack.pop()
- # ``is_named`` filters out keyword tokens that share a type name
- # with an expression node (e.g. the ``class`` keyword vs a
- # ``class`` expression in tree-sitter-typescript).
- if node.type in lmap.class_kinds and node.is_named:
- out.append(node)
- for child in node.children:
- stack.append(child)
- return out
-
-
-def _collect_class_methods(class_node: Node, lmap: LanguageNodeMap) -> list[Node]:
- """Direct method nodes of *class_node*.
-
- Stops at nested classes (their methods belong to the inner class) and
- does not descend into a method body (nested local defs roll up into
- the method, mirroring ``_collect_function_nodes``).
- """
- methods: list[Node] = []
- stack: list[Node] = list(class_node.children)
- while stack:
- node = stack.pop()
- if node.type in lmap.class_kinds:
- continue # nested class — its methods are not ours
- if node.type in lmap.function_kinds:
- methods.append(node)
- continue # don't descend into the method body
- for child in node.children:
- stack.append(child)
- return methods
+# ``_count_file_nloc`` is re-exported for ``tests/unit/health/test_file_nloc.py``,
+# which imports it directly from this module.
+from .nloc import _count_file_nloc, _count_file_nloc_tree, _count_nloc
+from .perf_walk import _collect_perf_hits
+
+__all__ = [
+ "ClassComplexity",
+ "ConditionComplexity",
+ "ErrorHandlingHit",
+ "FileComplexity",
+ "FunctionComplexity",
+ "PerfFnFacts",
+ "PerfHit",
+ "walk_file",
+ "walk_file_complexity",
+]
-
-def _self_member_name(node: Node, lmap: LanguageNodeMap) -> str | None:
- """Extract ``member`` from a ``self.member`` / ``this.member`` access.
-
- Returns the member name when the receiver token is one of the
- language's ``self_identifiers``; otherwise ``None`` (so ``other.x`` and
- ``a.b.c``'s outer hops are ignored — only direct instance access
- counts toward cohesion).
- """
- obj: Node | None = None
- for field_name in _OBJECT_FIELD_NAMES:
- obj = node.child_by_field_name(field_name)
- if obj is not None:
- break
- if obj is None:
- obj = next((c for c in node.children if c.is_named), None)
-
- prop: Node | None = None
- for field_name in _PROP_FIELD_NAMES:
- prop = node.child_by_field_name(field_name)
- if prop is not None:
- break
- if prop is None:
- named = [c for c in node.children if c.is_named]
- prop = next(
- (c for c in reversed(named) if c.type.endswith(_IDENTIFIER_SUFFIX)),
- None,
- )
-
- if obj is None or prop is None or obj is prop:
- return None
- if obj.text is None or prop.text is None:
- return None
- if obj.text.decode("utf-8", errors="replace") not in lmap.self_identifiers:
- return None
- return prop.text.decode("utf-8", errors="replace")
-
-
-def _collect_self_members(method_node: Node, lmap: LanguageNodeMap) -> set[str]:
- """Set of instance-member names referenced by *method_node*.
-
- Walks the method body (descending through nested functions/lambdas,
- which close over the same instance) but stops at nested class
- definitions. Both field reads and method calls reduce to a member
- name here — both are evidence two methods touch the same thing.
- """
- members: set[str] = set()
- if not lmap.self_identifiers or not lmap.member_access_kinds:
- return members
- stack: list[Node] = list(method_node.children)
- while stack:
- node = stack.pop()
- if node.type in lmap.class_kinds:
- continue # nested class has its own self
- if node.type in lmap.member_access_kinds:
- name = _self_member_name(node, lmap)
- if name:
- members.add(name)
- for child in node.children:
- stack.append(child)
- return members
-
-
-def _compute_lcom4(
- method_nodes: list[Node],
- method_fcs: list[FunctionComplexity],
- lmap: LanguageNodeMap,
-) -> tuple[int, int]:
- """Return ``(lcom4, field_count)`` for a class.
-
- LCOM4 = number of connected components over the methods, where two
- methods are connected if they share an instance member or one calls
- the other (a call shows up as a reference to the callee's name).
-
- **Safety valve:** if no instance-member references are detected at all
- (a pure-static class, or — importantly — a language whose
- member-access node type we have not mapped), return ``1`` rather than
- ``len(methods)``. This prevents ``low_cohesion`` from false-firing on
- an unverified language: a missing mapping yields "no signal", never a
- spurious high-LCOM hit.
- """
- n = len(method_nodes)
- if n == 0:
- return 1, 0
-
- members_per_method: list[set[str]] = [
- _collect_self_members(node, lmap) for node in method_nodes
- ]
- total_refs = sum(len(m) for m in members_per_method)
- method_names = {fc.name for fc in method_fcs}
- all_members: set[str] = set().union(*members_per_method) if members_per_method else set()
- field_count = len(all_members - method_names)
-
- if total_refs == 0:
- return 1, field_count
-
- # Union-find over method indices.
- parent = list(range(n))
-
- def _find(i: int) -> int:
- while parent[i] != i:
- parent[i] = parent[parent[i]]
- i = parent[i]
- return i
-
- def _union(a: int, b: int) -> None:
- ra, rb = _find(a), _find(b)
- if ra != rb:
- parent[ra] = rb
-
- name_to_idx = {fc.name: i for i, fc in enumerate(method_fcs)}
- # Bucket method indices by each member they reference; the method that
- # *defines* that name (a callee) joins the bucket too, so call edges and
- # shared-field edges are both captured in one pass.
- buckets: dict[str, list[int]] = {}
- for i, members in enumerate(members_per_method):
- for m in members:
- buckets.setdefault(m, []).append(i)
- for member, idxs in buckets.items():
- group = list(idxs)
- callee = name_to_idx.get(member)
- if callee is not None:
- group.append(callee)
- first = group[0]
- for other in group[1:]:
- _union(first, other)
-
- components = {_find(i) for i in range(n)}
- return len(components), field_count
-
-
-def _collect_classes(
- root: Node,
- lmap: LanguageNodeMap,
- source: bytes,
- fc_by_node_id: dict[int, FunctionComplexity],
-) -> list[ClassComplexity]:
- """Build ``ClassComplexity`` for every class-like node in the file."""
- if not lmap.class_kinds:
- return []
- classes: list[ClassComplexity] = []
- for class_node in _collect_class_nodes(root, lmap):
- method_nodes = _collect_class_methods(class_node, lmap)
- method_fcs = [fc_by_node_id[m.id] for m in method_nodes if m.id in fc_by_node_id]
- # Keep nodes and FCs aligned (a method missing from the function
- # pass — unusual — drops out of both).
- aligned_nodes = [m for m in method_nodes if m.id in fc_by_node_id]
- lcom4, field_count = _compute_lcom4(aligned_nodes, method_fcs, lmap)
- classes.append(
- ClassComplexity(
- name=_class_name(class_node),
- start_line=class_node.start_point[0] + 1,
- end_line=class_node.end_point[0] + 1,
- method_count=len(method_fcs),
- total_nloc=_count_nloc(class_node, source),
- methods=method_fcs,
- lcom4=lcom4,
- max_method_ccn=max((fc.ccn for fc in method_fcs), default=0),
- field_count=field_count,
- )
- )
- return classes
+log = structlog.get_logger(__name__)
def walk_file(
@@ -1743,26 +163,3 @@ def walk_file_complexity(
Prefer ``walk_file`` when class-level metrics are also needed.
"""
return walk_file(abs_path, language, source).functions
-
-
-def _count_parameters(fn_node: Node) -> int:
- """Best-effort parameter-list size for *fn_node*.
-
- Looks at tree-sitter ``parameters`` / ``parameter_list`` / ``parameters_list`` fields and counts non-punctuation
- children. Returns 0 when no parameter list is found.
- """
- params = fn_node.child_by_field_name("parameters")
- if params is None:
- for child in fn_node.children:
- if child.type in ("parameters", "parameter_list", "formal_parameters"):
- params = child
- break
- if params is None:
- return 0
- count = 0
- for child in params.children:
- if child.type in ("(", ")", ",", "self", "cls", ":", "*", "**"):
- continue
- if child.is_named:
- count += 1
- return count
diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/base.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/base.py
index e1cfd6b47..4bbe26f6f 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/base.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/base.py
@@ -1,6 +1,6 @@
"""The ``PerfDialect`` plugin contract + the ``PERF_DIALECTS`` registry.
-The performance pass (``complexity/walker.py::_collect_perf_hits``) is
+The performance pass (``complexity/perf_walk.py::_collect_perf_hits``) is
language-agnostic: every language difference lives in a ``PerfDialect``. This
mirrors the per-language plugin idiom the rest of the pipeline already uses
(``resolvers/``, ``extractors/bindings/``, ``heritage/``, ``framework_edges/``,