Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(health): refuse Extract Method spans across which a closure shares a local (#3274)
Raghav Chamadiyab57f4f55093d
diff --git a/docs/layers/REFACTORING.md b/docs/layers/REFACTORING.mdindex 13a21551f..fca58107f 100644--- a/docs/layers/REFACTORING.md+++ b/docs/layers/REFACTORING.md@@ -177,7 +177,8 @@ Most plans answer a health finding, so per-path marker rules ingenerated code comes back as a diff.- Extract Method offers a span only when it can show the extraction keeps behavior(every returned value written on every path, no state carried across loop- iterations). Spans it cannot prove are dropped, so it under-reports by design.+ iterations, no local a closure shares crossing the span's edge). Spans it+ cannot prove are dropped, so it under-reports by design.- Extract Method skips spans too small to matter and spans that would carry theoriginal finding into the helper. A component whose branching is mostly in itsmarkup gets no extraction.diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/defuse.py b/packages/core/src/repowise/core/analysis/health/dataflow/defuse.pyindex c2f5a69a9..2a716e8f4 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/defuse.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/defuse.py@@ -17,7 +17,7 @@ from __future__ import annotationsfrom dataclasses import dataclass, fieldfrom typing import TYPE_CHECKING-from .dialects.base import Occurrence+from .dialects.base import Captured, Occurrenceif TYPE_CHECKING:from tree_sitter import Node@@ -42,6 +42,8 @@ class Definition:line: int # 1-indexed#: Where on ``line`` a declared name starts to exist; see ``Occurrence``.declared_at: int | None = None+ #: The write declares the name (``Occurrence.declares``).+ declares: bool = False@dataclass@@ -59,16 +61,17 @@ class FunctionDefUse:``blocks`` is keyed by block id; ``definitions`` is every write site orderedby ``index`` (so ``definitions[i].index == i``); ``params`` is the parameter- occurrences seeded at the entry block. ``captured`` holds the reads made- inside nested closures, kept apart from the per-block uses because they- run when the closure is called, not where it is written; code that moves- statements (the Extract Method slicer) still has to count them.+ occurrences seeded at the entry block. ``captured`` is what nested+ closures read and write of this function's variables (see+ :class:`Captured`), kept apart from the per-block uses because a closure+ runs when it is called, not where it is written; code that moves+ statements (the Extract Method slicer) still has to count it."""blocks: dict[int, BlockDefUse]definitions: list[Definition]params: tuple[Occurrence, ...]- captured: tuple[Occurrence, ...] = ()+ captured: Captured = field(default_factory=Captured)def block(self, block_id: int) -> BlockDefUse | None:return self.blocks.get(block_id)@@ -99,6 +102,7 @@ def compute_def_use(index=counter,line=occ.line,declared_at=occ.declared_at,+ declares=occ.declares,)counter += 1bdu.defs.append(definition)@@ -122,8 +126,8 @@ def compute_def_use(_add_def(occ, block.id)bdu.uses.extend(sdu.uses)- captured: list[Occurrence] = []- dialect.collect_captured_reads(fn_node.child_by_field_name("body"), captured)+ captured = Captured()+ dialect.collect_captured(fn_node.child_by_field_name("body"), captured, lmap)return FunctionDefUse(- blocks=blocks, definitions=definitions, params=params, captured=tuple(captured)+ blocks=blocks, definitions=definitions, params=params, captured=captured)diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/base.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/base.pyindex a98395991..0c5a9b411 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/base.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/base.py@@ -40,7 +40,7 @@ precision-first contract the perf pillar depends on.from __future__ import annotations-from dataclasses import dataclass, replace+from dataclasses import dataclass, field, replacefrom typing import TYPE_CHECKING, Protocol, runtime_checkableif TYPE_CHECKING:@@ -67,6 +67,12 @@ class Occurrence:#: a read in the source. It keeps must-def proofs conservative; code that#: asks what a span actually reads skips it.echo: bool = False+ #: A write that creates a new binding of the name: a block-scoped+ #: declaration (set by :meth:`BaseDefUseDialect._declare`, multi-line+ #: declarators included, where ``declared_at`` stays None) or a loop+ #: header binder (:meth:`BaseDefUseDialect._loop_scoped`). Never a+ #: hoisting TS/JS ``var``.+ declares: bool = False@dataclass(frozen=True)@@ -81,6 +87,25 @@ class StatementDefUse:uses: tuple[Occurrence, ...]+@dataclass+class Captured:+ """What nested closures do to the enclosing function's variables, from+ :meth:`BaseDefUseDialect.collect_captured`.++ ``reads`` is liberal: every read except names the closure binds as+ parameters or writes before reading; it feeds Extract Method's IN/OUT+ liveness, where an extra name only adds a parameter or a return.+ ``shared`` and ``writes`` are strict: reads and assignments of the+ enclosing function's variables, leaving out every occurrence a+ closure-local binding covers (by scope range, not by name); they feed the+ gate that refuses a span a shared local crosses.+ """++ reads: list[Occurrence] = field(default_factory=list)+ shared: list[Occurrence] = field(default_factory=list)+ writes: list[Occurrence] = field(default_factory=list)++@runtime_checkableclass DefUseDialect(Protocol):"""The contract a per-language def/use dialect satisfies."""@@ -133,17 +158,29 @@ class BaseDefUseDialect:column=node.start_point[1],)- def _declare(self, defs: list[Occurrence], start: int, declarator: Node) -> None:+ def _declare(+ self, defs: list[Occurrence], start: int, declarator: Node, *, binds: bool = True+ ) -> None:"""Mark ``defs[start:]`` as names *declarator* declares.+ ``declared_at`` is set where the declarator ends on the def's own line;+ ``declares`` is set on every one of them when *binds* (False for a+ declaration that creates no block-scoped binding, a TS/JS ``var``).Call it right after the declarator's targets are collected and beforeits initializer is walked, so a write nested in the initializer is nottaken for a declaration."""end_row, end_col = declarator.end_pointfor i in range(start, len(defs)):- if defs[i].line == end_row + 1:- defs[i] = replace(defs[i], declared_at=end_col)+ at = end_col if defs[i].line == end_row + 1 else None+ defs[i] = replace(defs[i], declared_at=at, declares=binds)++ @staticmethod+ def _loop_scoped(defs: list[Occurrence], start: int) -> None:+ """Mark ``defs[start:]`` as a loop header's own binders, scoped to the+ loop. Only ``declares`` is set; ``declared_at`` keeps the line rule."""+ for i in range(start, len(defs)):+ defs[i] = replace(defs[i], declares=True)# -- read (use) collection ------------------------------------------------@@ -177,50 +214,135 @@ class BaseDefUseDialect:for child in node.named_children:self.collect_reads(child, out)- def collect_captured_reads(self, node: Node | None, out: list[Occurrence]) -> None:- """Append the reads made inside nested scopes under *node* to *out*.+ #: Whether a plain assignment inside a nested scope writes the enclosing+ #: function's variable (JS/TS, Go, Rust, C++ closures). Python's assignment+ #: makes the name the closure's own local instead. A C++ ``[=]`` or Rust+ #: ``move`` closure writes its own copy; its writes still count, which can+ #: only refuse a span.+ closure_writes_outer: bool = True+ #: Statements that make a name in a nested scope refer to an outer+ #: variable (Python ``nonlocal`` / ``global``).+ outer_decl_kinds: frozenset[str] = frozenset()++ def collect_captured(self, node: Node | None, out: Captured, lmap: LanguageNodeMap) -> None:+ """Collect into *out* what nested scopes under *node* do to the+ enclosing function's variables (see :class:`Captured`).:meth:`collect_reads` stops at a nested function or lambda because its- reads are not the statement's own. A closure still reads the enclosing- function's variables, though, so code that moves one of them has to- see those reads: a span whose closure reads a local needs it passed in,- and a span defining a local a later closure reads has to return it.- Left out: names the nested scope binds as its own parameters, its own- name (a nested ``def``), and locals it writes on a line before it first- reads them (``const t2 = v * 2``), which are its own variables. A local- written and read on one line is kept, which can only add a parameter or- a return, never drop one.+ reads are not the statement's own, but code that moves a closure, or+ the code around it, still has to see what the closure touches."""if node is None:returnif not self._is_scope_boundary(node):for child in node.named_children:- self.collect_captured_reads(child, out)+ self.collect_captured(child, out, lmap)return- writes, reads = self._closure_def_use(node)- bound = self._closure_bound_names(node) | _written_before_read(writes, reads)- out.extend(occ for occ in reads if occ.name not in bound)+ writes, direct, inner = self._closure_def_use(node, lmap)+ own_reads = [*direct, *inner.reads]+ bound = self._closure_bound_names(node) | _written_before_read(writes, own_reads)+ out.reads.extend(occ for occ in own_reads if occ.name not in bound)+ scopes, head_writes = self._closure_scopes(node, writes, lmap)+ out.shared.extend(o for o in (*direct, *inner.shared) if not _covered(o, scopes))+ out.writes.extend(+ w+ for w in (*writes, *head_writes, *inner.writes)+ if not w.declares and not _covered(w, scopes)+ )- def _closure_def_use(self, node: Node) -> tuple[list[Occurrence], list[Occurrence]]:- """Writes and reads inside nested scope *node*, deeper closures included.+ def _closure_def_use(+ self, node: Node, lmap: LanguageNodeMap+ ) -> tuple[list[Occurrence], list[Occurrence], Captured]:+ """The writes and direct reads inside nested scope *node*, and what+ deeper closures inside it capture.The body goes through the dialect's own walk, which tells a declarationfrom a read; a scope with no ``body`` field falls back to plain reads."""writes: list[Occurrence] = []reads: list[Occurrence] = []+ inner = Captured()body = node.child_by_field_name("body")process = getattr(self, "_process", None)if body is not None and process is not None:process(body, writes, reads)- self.collect_captured_reads(body, reads)- return writes, reads+ self.collect_captured(body, inner, lmap)+ return writes, reads, innername = node.child_by_field_name("name")for child in node.named_children:if name is None or child.id != name.id:self.collect_reads(child, reads)- self.collect_captured_reads(child, reads)- return writes, reads+ self.collect_captured(child, inner, lmap)+ return writes, reads, inner++ def _closure_scopes(+ self, node: Node, writes: list[Occurrence], lmap: LanguageNodeMap+ ) -> tuple[_Scopes, list[Occurrence]]:+ """Where each closure-local name of nested scope *node* is bound, and+ the loop-header assignments to outer names the body walk missed.++ A parameter and a Python local (a name assigned without ``nonlocal`` /+ ``global``) cover the whole scope; a block-scoped declaration covers+ its statement to the end of the enclosing block; a loop header binder+ covers its loop. Ranges, not names, so an inner ``let x`` does not hide+ a write to the outer ``x`` elsewhere. Ceiling: a TS/JS ``var`` in a+ closure is read as an outer variable, which can only refuse a span.+ """+ whole = (node.start_point, node.end_point)+ scopes: _Scopes = {name: [whole] for name in self._closure_bound_names(node)}+ loops, decls, outer = self._closure_parts(node, lmap)+ head_writes = self._loop_binder_scopes(loops, lmap, scopes)+ for decl in decls:+ _add_declaration_scope(decl, writes, lmap, scopes)+ if not self.closure_writes_outer:+ for w in (*writes, *head_writes):+ if w.name not in outer:+ scopes.setdefault(w.name, []).append(whole)+ head_writes = []+ for name in outer:+ scopes.pop(name, None)+ return scopes, head_writes++ def _closure_parts(+ self, node: Node, lmap: LanguageNodeMap+ ) -> tuple[list[Node], list[Node], set[str]]:+ """The loops and declaration statements of nested scope *node* (deeper+ scopes not entered), and the names it declares outer."""+ loops: list[Node] = []+ decls: list[Node] = []+ outer: set[str] = set()+ stack = list(node.named_children)+ while stack:+ cur = stack.pop()+ if self._is_scope_boundary(cur):+ continue+ if cur.type in self.outer_decl_kinds:+ outer |= {occ.name for occ in _leaf_names(cur, self.identifier_kinds)}+ elif cur.type in lmap.loop_kinds:+ loops.append(cur)+ elif cur.type in lmap.local_decl_kinds:+ decls.append(cur)+ stack.extend(cur.named_children)+ return loops, decls, outer++ def _loop_binder_scopes(+ self, loops: list[Node], lmap: LanguageNodeMap, scopes: _Scopes+ ) -> list[Occurrence]:+ """Scope each loop's own binders to the loop; return the header's+ assignments to names it does not bind (TS ``for (x of xs)``)."""+ head = getattr(self, "_head", None)+ if head is None:+ return []+ head_writes: list[Occurrence] = []+ for loop in loops:+ defs: list[Occurrence] = []+ head(loop, lmap, defs, [])+ for d in defs:+ if d.declares:+ scopes.setdefault(d.name, []).append((loop.start_point, loop.end_point))+ else:+ head_writes.append(d)+ return head_writesdef _closure_bound_names(self, node: Node) -> set[str]:"""Names a nested scope binds as its own parameters.@@ -310,6 +432,50 @@ class BaseDefUseDialect:uses.extend(echoes(inner_defs))+# Per closure-local name, the (start, end) points where that binding is in scope.+_Scopes = dict[str, list[tuple[tuple[int, int], tuple[int, int]]]]+++def _pos(occ: Occurrence) -> tuple[int, int]:+ return (occ.line - 1, occ.column)+++def _covered(occ: Occurrence, scopes: _Scopes) -> bool:+ """True when a closure-local binding is in scope at *occ*."""+ return any(lo <= _pos(occ) <= hi for lo, hi in scopes.get(occ.name, ()))+++def _add_declaration_scope(+ decl: Node, writes: list[Occurrence], lmap: LanguageNodeMap, scopes: _Scopes+) -> None:+ """Scope the names *decl* binds from the declaration to its block's end."""+ scope = (decl.start_point, _enclosing_end(decl, lmap))+ for w in writes:+ if w.declares and decl.start_point <= _pos(w) <= decl.end_point:+ scopes.setdefault(w.name, []).append(scope)+++def _enclosing_end(node: Node, lmap: LanguageNodeMap) -> tuple[int, int]:+ """End of the block or loop a declaration lives in (its scope's end)."""+ kinds = lmap.block_kinds | lmap.loop_kinds+ cur = node.parent+ while cur is not None and cur.type not in kinds:+ cur = cur.parent+ return (cur or node).end_point+++def _leaf_names(node: Node, kinds: frozenset[str]) -> list[Occurrence]:+ """Every identifier leaf under *node*."""+ out: list[Occurrence] = []+ stack = [node]+ while stack:+ cur = stack.pop()+ if cur.type in kinds and cur.text:+ out.append(Occurrence(cur.text.decode("utf-8", "replace"), cur.start_point[0] + 1))+ stack.extend(cur.named_children)+ return out++def echoes(defs: list[Occurrence]) -> list[Occurrence]:"""The may-def uses paired with *defs* (see :attr:`Occurrence.echo`)."""return [replace(occ, echo=True) for occ in defs]diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/cpp.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/cpp.pyindex 7e888d5e4..b0a292d93 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/cpp.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/cpp.py@@ -160,6 +160,7 @@ class CppDefUseDialect(BaseDefUseDialect):binder = self._binder_identifier(node.child_by_field_name("declarator"))if binder is not None:defs.append(self._occ(binder))+ self._loop_scoped(defs, len(defs) - 1)self._process(node.child_by_field_name("right"), defs, uses)elif t in lmap.loop_kinds: # for: init/cond/update; while / do: condself._process(node.child_by_field_name("initializer"), defs, uses)diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/go.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/go.pyindex cda3c96ad..d3ed7762c 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/go.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/go.py@@ -86,23 +86,30 @@ class GoDefUseDialect(BaseDefUseDialect):) -> None:if node.type in lmap.loop_kinds: # for_statement: range / clause / condfor child in node.named_children:- t = child.type- if t in lmap.block_kinds:- continue # the body lives in successor blocks- if t == _RANGE_CLAUSE:- self._targets(child.child_by_field_name("left"), defs, uses)- self._process(child.child_by_field_name("right"), defs, uses)- elif t == _FOR_CLAUSE:- self._process(child.child_by_field_name("initializer"), defs, uses)- self._process(child.child_by_field_name("condition"), defs, uses)- self._process(child.child_by_field_name("update"), defs, uses)- else: # bare condition (while-style ``for cond {}``)- self._process(child, defs, uses)- elif node.type in lmap.branch_kinds: # if_statement: init + condition+ if child.type not in lmap.block_kinds: # the body is in successor blocks+ self._loop_clause(child, defs, uses)+ return+ if node.type in lmap.branch_kinds: # if_statement: init + conditionself._process(node.child_by_field_name("initializer"), defs, uses)self._process(node.child_by_field_name("condition"), defs, uses)- else:- self._process(node, defs, uses)+ return+ self._process(node, defs, uses)++ def _loop_clause(self, child: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:+ """One header child of a ``for``: a range clause, a three-part clause,+ or a bare condition (while-style ``for cond {}``)."""+ if child.type == _RANGE_CLAUSE:+ start = len(defs)+ self._targets(child.child_by_field_name("left"), defs, uses)+ if any(tok.type == ":=" for tok in child.children):+ self._loop_scoped(defs, start)+ self._process(child.child_by_field_name("right"), defs, uses)+ return+ if child.type == _FOR_CLAUSE:+ for field_name in ("initializer", "condition", "update"):+ self._process(child.child_by_field_name(field_name), defs, uses)+ return+ self._process(child, defs, uses)# -- the unified expression / statement walk ------------------------------diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/java.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/java.pyindex 39f92235e..c6ab18205 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/java.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/java.py@@ -118,7 +118,9 @@ class JavaDefUseDialect(BaseDefUseDialect):) -> None:t = node.typeif t == _ENHANCED_FOR: # ``for (T v : xs)``+ start = len(defs)self._targets(node.child_by_field_name("name"), defs, uses)+ self._loop_scoped(defs, start)self._process(node.child_by_field_name("value"), defs, uses)elif t in lmap.loop_kinds: # for: init/cond/update; while / do: condself._process(node.child_by_field_name("init"), defs, uses)diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/python.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/python.pyindex 4f35a53d8..fc973bf6a 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/python.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/python.py@@ -49,6 +49,10 @@ class PythonDefUseDialect(BaseDefUseDialect):# A nested ``def`` is visible in the enclosing scope from its statement on.enclosing_binder_kinds = frozenset({"function_definition", "async_function_definition"})+ # Assignment makes a closure-local name; only ``nonlocal`` / ``global``+ # reach out of a nested scope.+ closure_writes_outer = False+ outer_decl_kinds = frozenset({"nonlocal_statement", "global_statement"})def _is_scope_boundary(self, node: Node) -> bool:return node.type in _SCOPE_BOUNDARIESdiff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/rust.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/rust.pyindex 9b87e32db..6d5ae4f0f 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/rust.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/rust.py@@ -151,7 +151,9 @@ class RustDefUseDialect(BaseDefUseDialect):if t in lmap.loop_kinds:pattern = node.child_by_field_name("pattern")if pattern is not None: # ``for PAT in expr`` -- binds every entry+ start = len(defs)self._targets(pattern, defs, uses)+ self._loop_scoped(defs, start)self._process(node.child_by_field_name("value"), defs, uses)else: # ``while cond`` / ``while let`` / bare ``loop`` (no head)self._condition(node.child_by_field_name("condition"), defs, uses)diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/ts_js.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/ts_js.pyindex 558657c1b..e52356f95 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/ts_js.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/ts_js.py@@ -33,6 +33,9 @@ _AUG_KINDS = frozenset({"augmented_assignment_expression"})_UPDATE_KINDS = frozenset({"update_expression"}) # ``x++`` / ``--x``_DECL_KINDS = frozenset({"lexical_declaration", "variable_declaration"})_DECLARATOR = "variable_declarator"+# ``var`` hoists to the function scope, so it creates no block-scoped binding.+_VAR_DECL = "variable_declaration"+_VAR = "var"# Destructuring patterns and their parts._ARRAY_PATTERN = "array_pattern"_OBJECT_PATTERN = "object_pattern"@@ -112,7 +115,11 @@ class TsJsDefUseDialect(BaseDefUseDialect):left = node.child_by_field_name("left")right = node.child_by_field_name("right")if left is not None or right is not None: # for-of / for-in binder+ start = len(defs)self._targets(left, defs, uses)+ kind = node.child_by_field_name("kind")+ if kind is not None and kind.type != _VAR: # ``const`` / ``let``+ self._loop_scoped(defs, start)self._process(right, defs, uses)return# C-style for / while / do: initializer + condition + increment.@@ -151,7 +158,7 @@ class TsJsDefUseDialect(BaseDefUseDialect):continuestart = len(defs)self._targets(declarator.child_by_field_name("name"), defs, uses)- self._declare(defs, start, declarator)+ self._declare(defs, start, declarator, binds=t != _VAR_DECL)self._process(declarator.child_by_field_name("value"), defs, uses)returnif t in self.member_access_kinds:diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/slice.py b/packages/core/src/repowise/core/analysis/health/dataflow/slice.pyindex e9e4c2b58..e410145e0 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/slice.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/slice.py@@ -28,9 +28,10 @@ this one slicer.from __future__ import annotations+from bisect import bisect_left, bisect_rightfrom collections import defaultdictfrom dataclasses import dataclass-from typing import TYPE_CHECKING+from typing import TYPE_CHECKING, NamedTuplefrom ..complexity.nloc import _code_line_numbers@@ -115,6 +116,7 @@ def find_extractions(analysis: FunctionAnalysis, lmap: LanguageNodeMap) -> list[declared_first = _declared_before_read(analysis.def_use)hoisted = _hoisted_bindings(def_lines, use_lines)decl_lines = _declaration_lines(analysis.def_use)+ shared = _closure_state(analysis.def_use, def_lines, use_lines)decision_kinds = (lmap.branch_kinds| lmap.loop_kinds@@ -210,6 +212,8 @@ def find_extractions(analysis: FunctionAnalysis, lmap: LanguageNodeMap) -> list[continueif nested_prefix[j + 1] > nested_prefix[i]:continue+ if shared is not None and _closure_state_crosses(shared, span, s, e, block, lmap):+ continueif loop is not None and not _loop_carry_free(span, loop, s, e, def_lines, use_lines, lmap):@@ -307,7 +311,7 @@ def _var_lines(def_use: FunctionDefUse) -> tuple[dict[str, list[int]], dict[str,Parameter definitions are included (seeded at the signature line), so aparameter naturally counts as "defined before" any body span. Reads inside- nested closures (``def_use.captured``) count as uses at their own line.+ nested closures (``def_use.captured.reads``) count as uses at their own line."""def_lines: dict[str, list[int]] = defaultdict(list)use_lines: dict[str, list[int]] = defaultdict(list)@@ -321,7 +325,7 @@ def _var_lines(def_use: FunctionDefUse) -> tuple[dict[str, list[int]], dict[str,use_lines[u.name].append(u.line)# A closure's read counts where the closure is written: lifting the code# around it moves the read with it. Only names this function binds matter.- for u in def_use.captured:+ for u in def_use.captured.reads:if u.name in def_lines:use_lines[u.name].append(u.line)for lines in def_lines.values():@@ -398,6 +402,138 @@ def _infer_in_out(return tuple(params), tuple(returns)+class _SharedState(NamedTuple):+ """Per variable a closure shares with the function, sorted line lists:+ the closures' reads and writes (``Captured.shared`` / ``.writes``), the+ function's own defs, all its references, and its binding declarations."""++ reads: dict[str, list[int]]+ writes: dict[str, list[int]]+ defs: dict[str, list[int]]+ refs: dict[str, list[int]]+ binds: dict[str, frozenset[int]]+++def _closure_state(+ def_use: FunctionDefUse,+ def_lines: dict[str, list[int]],+ use_lines: dict[str, list[int]],+) -> _SharedState | None:+ """The closure-shared locals of a function, or None when it has none+ (then the gate costs nothing per span)."""+ reads: dict[str, list[int]] = defaultdict(list)+ writes: dict[str, list[int]] = defaultdict(list)+ for out, occurrences in (+ (reads, def_use.captured.shared),+ (writes, def_use.captured.writes),+ ):+ for occ in occurrences:+ if occ.name in def_lines: # a name this function binds+ out[occ.name].append(occ.line)+ if not reads and not writes:+ return None+ names = set(reads) | set(writes)+ return _SharedState(+ reads={var: sorted(lines) for var, lines in reads.items()},+ writes={var: sorted(lines) for var, lines in writes.items()},+ defs={var: def_lines.get(var, []) for var in names},+ refs={var: sorted((*def_lines.get(var, ()), *use_lines.get(var, ()))) for var in names},+ binds=_declaration_lines(def_use, binding=True),+ )+++def _count_in(lines: list[int], s: int, e: int) -> int:+ return bisect_right(lines, e) - bisect_left(lines, s)+++def _closure_state_crosses(+ st: _SharedState, span: list[Node], s: int, e: int, block: Node, lmap: LanguageNodeMap+) -> bool:+ """True when a local a closure shares crosses the span boundary.++ A closure runs when it is called, not where it is written, so line+ liveness cannot place its reads and writes; an IN/OUT signature copies a+ value where the closure needs the variable itself. Refused:++ - a closure in the span writes a local anything outside the span refers+ to (code, or a closure there): the lifted closure writes the helper's+ copy. A counter declared in the span with its ``inc`` closure is+ refused this way when the count is read after the span;+ - a closure outside the span writes a local the span refers to: the span+ reads or writes a stale copy;+ - the span writes a local that a closure written above it reads: the+ closure runs later and sees the old value. Measured on hermes+ ``apps/desktop/src/lib/ansi.ts::parseAnsi``, whose ``pushText`` reads+ the ``bold`` / ``fg`` a span inside the loop sets.++ The last two are waived when the span's name is a binding of its own+ (:func:`_span_binds`, :func:`_span_only_rebinds`): the closure's variable+ is then another one.+ """+ for var in st.reads.keys() | st.writes.keys():+ writes = st.writes.get(var, [])+ reads = st.reads.get(var, [])+ refs = st.refs[var]+ w_in = _count_in(writes, s, e)+ if w_in and (+ w_in < len(writes)+ or _count_in(reads, s, e) < len(reads)+ or _count_in(refs, s, e) < len(refs)+ ):+ return True+ stale = w_in < len(writes) and _count_in(refs, s, e) > 0+ if stale and not _span_binds(st, var, span, block, lmap):+ return True+ overwritten = bool(reads) and reads[0] < s and _count_in(st.defs[var], s, e) > 0+ if overwritten and not _span_only_rebinds(st, var, s, e, block, lmap):+ return True+ return False+++def _declares_in(node: Node, lines: frozenset[int], lmap: LanguageNodeMap) -> bool:+ """True when *node* is a declaration statement holding one of *lines*."""+ lo, hi = node.start_point[0] + 1, node.end_point[0] + 1+ return node.type in lmap.local_decl_kinds and any(lo <= ln <= hi for ln in lines)+++def _declared_above(binds: frozenset[int], block: Node, s: int, lmap: LanguageNodeMap) -> bool:+ """True when a statement of *block* above the span already declares the+ name: a re-declaration in the same scope (Go's ``v, err := h()``) is an+ assignment to it, not a new binding."""+ return any(+ node.end_point[0] + 1 < s and _declares_in(node, binds, lmap)+ for node in block.named_children+ )+++def _span_binds(+ st: _SharedState, var: str, span: list[Node], block: Node, lmap: LanguageNodeMap+) -> bool:+ """True when one of the span's own top-level statements declares a new+ binding of *var*, so every reference to it in the span is to that one.++ Top-level only: a declaration in a nested block shadows just that block.+ """+ binds = st.binds.get(var, frozenset())+ s = span[0].start_point[0] + 1+ return (+ bool(binds)+ and not _declared_above(binds, block, s, lmap)+ and any(_declares_in(node, binds, lmap) for node in span)+ )+++def _span_only_rebinds(+ st: _SharedState, var: str, s: int, e: int, block: Node, lmap: LanguageNodeMap+) -> bool:+ """True when every write the span makes to *var* creates a new binding+ (a declaration or a loop binder, at any depth), so none of them reaches+ the variable a closure outside the span reads."""+ binds = st.binds.get(var, frozenset())+ defs = st.defs[var][bisect_left(st.defs[var], s) : bisect_right(st.defs[var], e)]+ return all(ln in binds for ln in defs) and not _declared_above(binds, block, s, lmap)++def _holds_a_named_nested_function(span: list[Node], lmap: LanguageNodeMap) -> bool:"""True when the span contains a nested function that binds its own name.@@ -462,12 +598,16 @@ def _hoisted_bindings(return sorted(hoisted)-def _declaration_lines(def_use: FunctionDefUse) -> dict[str, frozenset[int]]:+def _declaration_lines(+ def_use: FunctionDefUse, *, binding: bool = False+) -> dict[str, frozenset[int]]:"""Per variable, the lines that declare it (a ``let`` / ``var`` / ``:=`` /- typed local, as the dialect marks with ``declared_at``)."""+ typed local, as the dialect marks with ``declared_at``). With *binding*,+ the lines that create a new binding instead (``declares``: multi-line+ declarators and loop binders included, a TS/JS ``var`` not)."""lines: dict[str, set[int]] = defaultdict(set)for d in def_use.definitions:- if d.declared_at is not None:+ if (d.declares if binding else d.declared_at is not None):lines[d.var].add(d.line)return {var: frozenset(found) for var, found in lines.items()}diff --git a/tests/unit/health/test_dataflow_langs.py b/tests/unit/health/test_dataflow_langs.pyindex 2060e0dfb..f53c183dd 100644--- a/tests/unit/health/test_dataflow_langs.py+++ b/tests/unit/health/test_dataflow_langs.py@@ -1734,7 +1734,7 @@ def test_ts_closure_parameter_is_not_a_read_of_the_outer_name():assert spansassert all("x" in e.params for e in spans) # the condition reads the outer xfn = _first("typescript", src)- captured = {u.name for u in fn.def_use.captured}+ captured = {u.name for u in fn.def_use.captured.reads}assert "x" not in captured and "items" not in captured@@ -1794,7 +1794,7 @@ def test_ts_closure_parameter_is_not_a_read_of_the_outer_name():)def test_closure_reads_are_captured_per_language(language, src, captured, not_captured):fn = _first(language, src)- names = {u.name for u in fn.def_use.captured}+ names = {u.name for u in fn.def_use.captured.reads}assert captured <= namesassert not (not_captured & names)@@ -1815,7 +1815,7 @@ def test_a_closure_local_shadowing_an_outer_name_is_not_a_capture():}"""fn = _first("typescript", src)- assert "t2" not in {u.name for u in fn.def_use.captured}+ assert "t2" not in {u.name for u in fn.def_use.captured.reads}# == Spans a helper cannot carry =================================================diff --git a/tests/unit/health/test_extract_method_live_out.py b/tests/unit/health/test_extract_method_live_out.pynew file mode 100644index 000000000..efe2a0f9d--- /dev/null+++ b/tests/unit/health/test_extract_method_live_out.py@@ -0,0 +1,633 @@+"""Extract Method returns every live-out write, or refuses the span.++A variable the span writes and the code after it reads has to come back as the+helper's return. Line liveness sees plain, conditional, ``x++`` and ``x += 1``+writes; it cannot place what a closure does, because a closure runs when it is+called, not where it is written. Two shapes leaked through before: a closure in+the span assigning an outer local (the helper's copy is written instead), and a+span writing a local that a closure written above it reads (hermes+``apps/desktop/src/lib/ansi.ts::parseAnsi``: ``pushText`` reads the ``bold`` /+``fg`` a span inside the loop sets, and the plan returned neither).++Each fixture marks the writes of the watched variable ``W``, the reads after+them ``R`` and, where one exists, the closure that reads it ``C``. A span that+covers a write, ends before a read and does not carry the reading closure along+must return the variable.+"""++from __future__ import annotations++import textwrap+from unittest import mock++import pytest++from repowise.core.analysis.health.complexity.languages import get_language_map+from repowise.core.analysis.health.dataflow import slice as slicer+from repowise.core.analysis.health.dataflow.analyze import analyze_file+++def _marked(src: str, mark: str) -> list[int]:+ return [i + 1 for i, line in enumerate(src.splitlines()) if line.rstrip().endswith(mark)]+++def _spans(language: str, ext: str, src: str):+ from repowise.core.ingestion.parser import _get_language++ if _get_language(language) is None:+ pytest.skip(f"tree-sitter language pack missing for {language}")+ res = analyze_file(f"m.{ext}", language, src.encode(), flagged_only=False)+ if not res.functions:+ pytest.skip(f"tree-sitter language pack missing for {language}")+ # The fixtures are small, so most spans hold nearly the whole body; the+ # body-share worth gate would hide the soundness gate under test.+ with mock.patch.object(slicer, "_MAX_BODY_SHARE", float("inf")):+ return slicer.find_extractions(res.functions[0], get_language_map(language))+++def _leaks(language: str, ext: str, var: str, src: str) -> tuple[list, list]:+ """(spans that drop the live-out *var*, spans that return it)."""+ src = textwrap.dedent(src)+ writes, reads, closures = _marked(src, "W"), _marked(src, "R"), _marked(src, "C")+ leaking, returning = [], []+ for x in _spans(language, ext, src):+ if not any(x.start_line <= w <= x.end_line for w in writes):+ continue+ if not any(r > x.end_line for r in reads):+ continue+ if any(x.start_line <= c <= x.end_line for c in closures):+ continue # the reading closure moves with the write+ (returning if var in x.returns else leaking).append(x)+ return leaking, returning+++# Plain writes line liveness already sees: conditional, increment, augmented.+_RETURNED = [+ (+ "python",+ "py",+ """+def f(a, b, c):+ x = 0+ y = a + b+ if y > c:+ x += 1 # W+ z = y * 2+ print(z)+ print(a)+ print(b)+ return x # R+""",+ ),+ (+ "typescript",+ "ts",+ """+function f(a: number, b: number, c: number): number {+ let x = 0;+ const y = a + b;+ if (y > c) {+ x++; // W+ }+ const z = y * 2;+ log(z);+ log(a);+ log(b);+ return x; // R+}+""",+ ),+ (+ "go",+ "go",+ """+package m+func f(a int, b int, c int) int {+ x := 0+ y := a + b+ if y > c {+ x++ // W+ }+ z := y * 2+ log(z)+ log(a)+ log(b)+ return x // R+}+""",+ ),+ (+ "java",+ "java",+ """+class M {+ int f(int a, int b, int c) {+ int x = 0;+ int y = a + b;+ if (y > c) {+ x++; // W+ }+ int z = y * 2;+ log(z);+ log(a);+ log(b);+ return x; // R+ }+}+""",+ ),+ (+ "rust",+ "rs",+ """+fn f(a: i32, b: i32, c: i32) -> i32 {+ let mut x = 0;+ let y = a + b;+ if y > c {+ x += 1; // W+ }+ let z = y * 2;+ log(z);+ log(a);+ log(b);+ return x; // R+}+""",+ ),+ (+ "cpp",+ "cpp",+ """+int f(int a, int b, int c) {+ int x = 0;+ int y = a + b;+ if (y > c) {+ x++; // W+ }+ int z = y * 2;+ log(z);+ log(a);+ log(b);+ return x; // R+}+""",+ ),+]++[email protected](("language", "ext", "src"), _RETURNED, ids=[r[0] for r in _RETURNED])+def test_plain_live_out_write_is_returned(language: str, ext: str, src: str):+ leaking, returning = _leaks(language, ext, "x", src)+ assert leaking == []+ assert returning, "the span holding the write should still be offered, returning x"+++# A closure in the span assigns an outer local that is read after the span.+_CLOSURE_WRITES = [+ (+ "typescript",+ "ts",+ """+function f(a: number, b: number, c: number): number {+ let x = 0;+ let y = a + b;+ if (y > c) {+ y = c;+ }+ const bump = () => { x = y; }; // W+ bump();+ log(y);+ log(a);+ return x; // R+}+""",+ ),+ (+ "go",+ "go",+ """+package m+func f(a int, b int, c int) int {+ x := 0+ y := a + b+ if y > c {+ y = c+ }+ bump := func() { x = y } // W+ bump()+ log(y)+ log(a)+ return x // R+}+""",+ ),+ (+ "rust",+ "rs",+ """+fn f(a: i32, b: i32, c: i32) -> i32 {+ let mut x = 0;+ let mut y = a + b;+ if y > c {+ y = c;+ }+ let mut bump = || { x = y; }; // W+ bump();+ log(y);+ log(a);+ return x; // R+}+""",+ ),+ (+ "cpp",+ "cpp",+ """+int f(int a, int b, int c) {+ int x = 0;+ int y = a + b;+ if (y > c) {+ y = c;+ }+ auto bump = [&]() { x = y; }; // W+ bump();+ log(y);+ log(a);+ return x; // R+}+""",+ ),+ (+ # A block-local ``let x`` inside the closure does not make the+ # closure's later ``x = y`` its own: scopes are ranges, not names.+ "typescript",+ "ts",+ """+function f(a: number, b: number, c: number): number {+ let x = 0;+ let y = a + b;+ if (y > c) {+ y = c;+ }+ const bump = () => { if (y) { let x = 1; log(x); } x = y; }; // W+ bump();+ log(y);+ log(a);+ return x; // R+}+""",+ ),+ (+ # An awaiting span is refused like any other.+ "typescript",+ "ts",+ """+async function f(a: number, b: number, c: number): Promise<number> {+ let x = 0;+ let y = a + b;+ if (y > c) {+ y = await g(c);+ }+ const bump = () => { x = y; }; // W+ bump();+ log(y);+ log(a);+ return x; // R+}+""",+ ),+ (+ # The counter pattern: declared in the span with its closure, read after.+ "typescript",+ "ts",+ """+function f(a: number, b: number, c: number): number {+ log(a);+ let x = 0;+ const inc = () => { x++; }; // W+ if (a > b) {+ inc();+ }+ if (b > c) {+ inc();+ }+ log(b);+ log(c);+ return x; // R+}+""",+ ),+]+++ ("language", "ext", "src"),+ _CLOSURE_WRITES,+ ids=["typescript", "go", "rust", "cpp", "ts-inner-shadow", "ts-await", "ts-counter"],+)+def test_closure_write_in_span_is_refused(language: str, ext: str, src: str):+ # Returning ``x`` does not help either: the closure may run again later.+ leaking, returning = _leaks(language, ext, "x", src)+ assert leaking == []+ assert returning == []+++# The parseAnsi shape: a closure above the loop reads state a span sets.+_CLOSURE_READS = [+ (+ "python",+ "py",+ """+def f(codes, cleaned):+ out = []+ bold = False+ def push(t): # C+ out.append((bold, t))+ for c in codes:+ push(c)+ if c == 0:+ bold = False # W+ elif c == 1:+ bold = True # W+ print(c)+ print(cleaned)+ push(cleaned) # R+ return out+""",+ ),+ (+ "typescript",+ "ts",+ """+function parseAnsi(cleaned: string): string[] {+ const segments: string[] = [];+ let bold = false;+ const pushText = (t: string) => { // C+ segments.push(bold ? t : "");+ };+ let match: string[] | null;+ while ((match = next(cleaned)) !== null) {+ pushText(match[0]);+ if (match[2] === "m") {+ const code = Number(match[1]);+ if (code === 0) {+ bold = false; // W+ } else if (code === 1) {+ bold = true; // W+ }+ log(code);+ }+ log(match);+ }+ pushText(cleaned); // R+ return segments;+}+""",+ ),+ (+ # This repo's scala.py::expand_scala_import_clauses: ``_flush`` reads+ # (and through ``nonlocal`` resets) the ``trailer`` the loop sets.+ "python",+ "py",+ """+def f(children, src):+ clauses = []+ trailer = None+ def _flush(): # C+ nonlocal trailer+ if trailer is not None:+ clauses.append(trailer)+ trailer = None+ for child in children:+ if child.type == "identifier":+ trailer = child # W+ elif child.type == ",":+ _flush()+ print(src)+ print(child)+ _flush() # R+ return clauses+""",+ ),+ (+ "go",+ "go",+ """+package m+func f(codes []int) []string {+ out := []string{}+ bold := false+ push := func(t string) { // C+ if bold {+ out = append(out, t)+ }+ }+ for _, c := range codes {+ push("a")+ if c == 0 {+ bold = false // W+ } else if c == 1 {+ bold = true // W+ }+ log(c)+ }+ push("b") // R+ return out+}+""",+ ),+]+++ ("language", "ext", "src"), _CLOSURE_READS, ids=["python", "typescript", "python-nonlocal", "go"]+)+def test_write_read_by_an_earlier_closure_is_refused(language: str, ext: str, src: str):+ leaking, _ = _leaks(language, ext, "trailer" if "trailer" in src else "bold", src)+ assert leaking == []+++def test_closure_after_the_span_still_makes_a_return():+ """A closure written after the span reads the value the span left, so the+ ordinary OUT still covers it: line liveness counts its read where it sits."""+ src = """+function f(a: number, b: number, c: number): number[] {+ let x = 0;+ const y = a + b;+ if (y > c) {+ x = 1; // W+ } else {+ x = 2; // W+ }+ log(a);+ log(b);+ return [a, b].map(v => v + x); // R+}+"""+ leaking, returning = _leaks("typescript", "ts", "x", src)+ assert leaking == []+ assert returning+++# A span binding the name itself is waived only for a real new binding at its+# top level. Each fixture's closure reads the outer ``x``; the span writes ``x``.+_NOT_A_NEW_BINDING = [+ (+ # The span's ``let x`` is in a nested block; its ``x = b`` is the outer x.+ "typescript",+ "ts",+ """+function f(a: number, b: number): number {+ let x = 0;+ const show = () => log(x); // C+ if (a > b) {+ let x = a;+ log(x);+ }+ x = b; // W+ if (b > a) {+ log(a);+ }+ log(b);+ show(); // R+ return 0;+}+""",+ ),+ (+ # ``var`` hoists: the span's ``var x`` is the closure's x.+ "typescript",+ "ts",+ """+function f(xs: number[], b: number): number {+ var x = 0;+ const show = () => log(x); // C+ for (const a of xs) {+ log(a);+ var x = a; // W+ if (a > b) {+ x = b;+ }+ log(b);+ log(a);+ show(); // R+ }+ return 0;+}+""",+ ),+ (+ # Go ``v, err := h(b)`` in the block that declared ``err`` assigns it.+ "go",+ "go",+ """+package m+func f(a int, b int) int {+ err := g(a)+ report := func() { log(err) } // C+ log(a)+ v, err := h(b) // W+ if v > a {+ log(v)+ }+ log(b)+ log(a)+ report() // R+ return v+}+""",+ ),+]+++ ("language", "ext", "src"), _NOT_A_NEW_BINDING, ids=["ts-nested-let", "ts-var", "go-redeclare"]+)+def test_name_bound_elsewhere_is_not_the_spans_own(language: str, ext: str, src: str):+ var = "err" if language == "go" else "x"+ leaking, _ = _leaks(language, ext, var, src)+ assert leaking == []+++def test_a_real_shadowing_let_is_still_offered():+ """The ``var`` fixture with ``let``: the span's x is a new binding, so the+ closure's reads of the outer x do not refuse it."""+ src = textwrap.dedent(_NOT_A_NEW_BINDING[1][2]).replace("var x = a;", "let x = a;")+ writes = _marked(src, "W")+ assert any(x.start_line <= writes[0] <= x.end_line for x in _spans("typescript", "ts", src))+++def test_closure_loop_binder_is_not_an_outer_variable():+ """A closure's ``for (const x of ...)`` binds its own x, so a span writing+ the outer x is not refused for it."""+ src = textwrap.dedent(+ """+function f(xs: number[], a: number, b: number): number {+ let x = 0;+ const dump = () => { for (const x of xs) { log(x); } };+ if (a > b) {+ x = a; // W+ } else {+ x = b;+ }+ log(a);+ log(b);+ dump();+ return x;+}+"""+ )+ writes = _marked(src, "W")+ assert any(+ x.start_line <= writes[0] <= x.end_line and "x" in x.returns+ for x in _spans("typescript", "ts", src)+ )+++def test_spans_without_shared_closure_state_are_unchanged():+ """The gate is a no-op for a function no closure shares a local with: its+ offered spans are identical with the gate on and off."""+ from repowise.core.analysis.health.complexity.languages import get_language_map++ from .refactoring_corpus_fixture import CASE_FILES, source_for++ sources = [(lang, f"m.{ext}", textwrap.dedent(src).encode()) for lang, ext, src in _RETURNED]+ sources += [(lang, name, source_for(name)) for lang, name in CASE_FILES]+ checked = 0+ for language, name, data in sources:+ lmap = get_language_map(language)+ for fn in analyze_file(name, language, data, flagged_only=False).functions:+ if fn.def_use.captured.shared or fn.def_use.captured.writes:+ continue+ with mock.patch.object(slicer, "_MAX_BODY_SHARE", float("inf")):+ on = slicer.find_extractions(fn, lmap)+ with mock.patch.object(slicer, "_closure_state_crosses", lambda *a: False):+ off = slicer.find_extractions(fn, lmap)+ assert on == off, (name, fn.name)+ checked += 1+ assert checked+++def test_a_nested_loop_binder_of_the_same_name_is_still_offered():+ """openclaw ``setup-registry.ts``: a closure in the first loop reads that+ loop's ``id``; the span's own ``for (const id of ...)`` binds another."""+ src = textwrap.dedent(+ """+function f(xs: string[], ys: string[], out: string[]): void {+ for (const id of xs) {+ out.push(...ys.filter((y) => y === id));+ }+ const seen = new Set(ys);+ if (seen.size > 0) {+ for (const id of xs) { // W+ if (!seen.has(id)) {+ out.push(id);+ }+ }+ }+ log(out);+}+"""+ )+ writes = _marked(src, "W")+ assert any(x.start_line <= writes[0] <= x.end_line for x in _spans("typescript", "ts", src))