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.

feat(perf): promote plans on facts that settle their prerequisites (#2504)

Raghav Chamadiya35c262e2fa7d
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/defer_in_loop.py b/packages/core/src/repowise/core/analysis/health/biomarkers/defer_in_loop.py
index a5b7a9b29..ac7f33aeb 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/defer_in_loop.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/defer_in_loop.py
@@ -37,7 +37,7 @@ class DeferInLoopDetector:
function_name=hit.function,
line_start=hit.line,
line_end=hit.line,
- details={},
+ details=hit.loop_facts(),
reason=(
"a deferred call inside a loop runs only when the "
"enclosing function returns, so the resource stays held "
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/goroutine_in_unbounded_loop.py b/packages/core/src/repowise/core/analysis/health/biomarkers/goroutine_in_unbounded_loop.py
index 6abb0abd3..da3ab18e9 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/goroutine_in_unbounded_loop.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/goroutine_in_unbounded_loop.py
@@ -32,7 +32,7 @@ class GoroutineInUnboundedLoopDetector:
function_name=hit.function,
line_start=hit.line,
line_end=hit.line,
- details={},
+ details=hit.loop_facts(),
reason=(
"a goroutine is spawned per loop element with no concurrency "
"bound; use a worker pool or a bounded errgroup/semaphore"
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 977ea32ec..155931483 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
@@ -96,6 +96,7 @@ class IoInLoopDetector:
"cross_function": True,
"path": list(hit.path),
"resolution_basis": hit.resolution_basis,
+ **hit.loop_facts(),
},
reason=(
f"{phrasing} is reached once per loop iteration through "
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/json_parse_in_loop.py b/packages/core/src/repowise/core/analysis/health/biomarkers/json_parse_in_loop.py
index 1823ae1d1..cb563b23f 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/json_parse_in_loop.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/json_parse_in_loop.py
@@ -32,7 +32,7 @@ class JsonParseInLoopDetector:
function_name=hit.function,
line_start=hit.line,
line_end=hit.line,
- details={},
+ details=hit.loop_facts(),
reason=(
"JSON.parse/stringify runs every loop iteration; hoist the "
"serialization or use structuredClone for deep copies"
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/list_insert_zero_in_loop.py b/packages/core/src/repowise/core/analysis/health/biomarkers/list_insert_zero_in_loop.py
index 6afdb572a..454598fb4 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/list_insert_zero_in_loop.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/list_insert_zero_in_loop.py
@@ -31,7 +31,7 @@ class ListInsertZeroInLoopDetector:
function_name=hit.function,
line_start=hit.line,
line_end=hit.line,
- details={},
+ details=hit.loop_facts(),
reason=(
"insert(0, ...) in a loop shifts the whole list each pass (O(n^2)); "
"use collections.deque.appendleft or append-then-reverse"
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/lock_in_loop.py b/packages/core/src/repowise/core/analysis/health/biomarkers/lock_in_loop.py
index f45e45b96..cfa0c500b 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/lock_in_loop.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/lock_in_loop.py
@@ -36,7 +36,7 @@ class LockInLoopDetector:
function_name=hit.function,
line_start=hit.line,
line_end=hit.line,
- details={},
+ details=hit.loop_facts(),
reason=(
"a lock is acquired every loop iteration; hoist the "
"lock outside the loop or batch the critical section"
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/membership_test_against_list_in_loop.py b/packages/core/src/repowise/core/analysis/health/biomarkers/membership_test_against_list_in_loop.py
index e19dfd3e8..d79523546 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/membership_test_against_list_in_loop.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/membership_test_against_list_in_loop.py
@@ -35,7 +35,7 @@ class MembershipTestAgainstListInLoopDetector:
function_name=hit.function,
line_start=hit.line,
line_end=hit.line,
- details={},
+ details=hit.loop_facts(),
reason=(
"membership tested against a list inside a loop (O(n·m)); "
"use a set for O(1) lookups"
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/pd_concat_in_loop.py b/packages/core/src/repowise/core/analysis/health/biomarkers/pd_concat_in_loop.py
index 1bd5aa7f4..5b265a8ce 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/pd_concat_in_loop.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/pd_concat_in_loop.py
@@ -32,7 +32,7 @@ class PdConcatInLoopDetector:
function_name=hit.function,
line_start=hit.line,
line_end=hit.line,
- details={},
+ details=hit.loop_facts(),
reason=(
"pd.concat in a loop copies the whole frame each pass (O(n^2)); "
"collect the chunks in a list and concat once after the loop"
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/regex_compile_in_loop.py b/packages/core/src/repowise/core/analysis/health/biomarkers/regex_compile_in_loop.py
index 4ae1ee81b..a40927cdb 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/regex_compile_in_loop.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/regex_compile_in_loop.py
@@ -38,7 +38,7 @@ class RegexCompileInLoopDetector:
function_name=hit.function,
line_start=hit.line,
line_end=hit.line,
- details={},
+ details=hit.loop_facts(),
reason=(
"a regex with a static pattern is recompiled every loop "
"iteration; compile it once outside the loop and reuse it"
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/resource_construction_in_loop.py b/packages/core/src/repowise/core/analysis/health/biomarkers/resource_construction_in_loop.py
index 59a947698..8c91a5cfa 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/resource_construction_in_loop.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/resource_construction_in_loop.py
@@ -37,7 +37,7 @@ class ResourceConstructionInLoopDetector:
function_name=hit.function,
line_start=hit.line,
line_end=hit.line,
- details={},
+ details=hit.loop_facts(),
reason=(
"a heavy client / connection is constructed each loop "
"iteration; hoist and reuse a single instance"
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/string_concat_in_loop.py b/packages/core/src/repowise/core/analysis/health/biomarkers/string_concat_in_loop.py
index 427187aa0..c29ee3b07 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/string_concat_in_loop.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/string_concat_in_loop.py
@@ -35,7 +35,7 @@ class StringConcatInLoopDetector:
function_name=hit.function,
line_start=hit.line,
line_end=hit.line,
- details={},
+ details=hit.loop_facts(),
reason="string built by += in a loop; use a buffer / join for linear cost",
)
)
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/models.py b/packages/core/src/repowise/core/analysis/health/complexity/models.py
index 7bcb5b0fb..59ceb5dea 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/models.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/models.py
@@ -7,10 +7,11 @@ the health engine; see the per-class docstrings for the downstream reader.
from __future__ import annotations
from dataclasses import dataclass, field
-from typing import TYPE_CHECKING
+from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from ..asserts.oracle_reach import OracleReach
+ from ..perf.loop_facts import LoopFacts
@dataclass
@@ -271,12 +272,12 @@ class PerfHit:
# proof is unavailable (no dialect, guard trip, non-convergence) or the loop
# genuinely carries a dependence. The biomarker sharpens its message when set.
promoted: bool = False
- # Innermost loop walks its data in chunks (same-function hits only).
- chunked: bool = False
+ # What the innermost enclosing loop proves (same-function hits only).
+ loop: LoopFacts | None = None
- def loop_facts(self) -> dict[str, bool]:
+ def loop_facts(self) -> dict[str, Any]:
"""Loop facts for ``details``; absent when unset so old findings are unchanged."""
- return {"chunked_iteration": True} if self.chunked else {}
+ return self.loop.as_details() if self.loop is not None else {}
@dataclass(frozen=True)
@@ -322,6 +323,9 @@ class PerfFnFacts:
nested_loop_line: int = 0
blocking_sink_kind: str | None = None
blocking_sink_line: int = 0
+ # ``(call_line, magnitude)`` for loop-nested calls whose loop magnitude is known,
+ # so a cross-function hit reports the trip count of the loop that pays it.
+ loop_call_magnitudes: tuple[tuple[int, str], ...] = ()
@dataclass
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
index 863e369c1..6059807e3 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.py
@@ -24,6 +24,7 @@ from typing import TYPE_CHECKING
from ..perf.dialects import PERF_DIALECTS
from ..perf.dialects.base import BasePerfDialect as BasePerfDialectClass
from ..perf.io_boundaries import collect_io_names
+from ..perf.loop_facts import LoopFacts
from .ast_utils import _dart_signature_sibling, _find_name
from .languages import LanguageNodeMap
from .models import PerfFnFacts, PerfHit
@@ -153,18 +154,24 @@ def _enclosing_loop_iterables(
return names
-def _in_chunked_loop(
+def _enclosing_loops(
node: Node, dialect: BasePerfDialect, loop_kinds: frozenset[str], fn_kinds: frozenset[str]
-) -> bool:
- """Whether the innermost enclosing loop chunks; walked up only for rare sink hits."""
+) -> list[Node]:
+ """The data-dependent loops around *node* in its function, innermost first;
+ walked up only for hits."""
+ loops: list[Node] = []
cur = node.parent
for _ in range(64):
if cur is None or cur.type in fn_kinds:
- return False
- if cur.type in loop_kinds and cur.is_named:
- return dialect.is_chunked_loop(cur)
+ break
+ if cur.type in loop_kinds and cur.is_named and not dialect.is_constant_loop(cur):
+ loops.append(cur)
cur = cur.parent
- return False
+ return loops
+
+
+def _overrides(dialect: BasePerfDialect, hook: str) -> bool:
+ return getattr(type(dialect), hook) is not getattr(BasePerfDialectClass, hook)
def _is_block_loop_body_scope(
@@ -245,8 +252,46 @@ def _collect_perf_hits(
async_fn_kinds = lmap.async_function_kinds
# Block-iteration loops (Ruby ``items.each do … end``): only pay for the
# per-call-node hook when the dialect actually overrides it.
- do_block_loop = type(dialect).block_loop_body is not BasePerfDialectClass.block_loop_body
- do_chunked = type(dialect).is_chunked_loop is not BasePerfDialectClass.is_chunked_loop
+ do_block_loop = _overrides(dialect, "block_loop_body")
+ do_chunked = _overrides(dialect, "is_chunked_loop")
+ do_magnitude = _overrides(dialect, "loop_magnitude")
+ do_batch = _overrides(dialect, "batch_form")
+ do_bound = _overrides(dialect, "concurrency_bound")
+ do_loop_facts = do_chunked or do_magnitude or do_batch or do_bound
+
+ def probe(call: Node) -> str | None:
+ return dialect.sink_kind(
+ dialect.callee_root_name(call) or "",
+ dialect.callee_method_name(call) or "",
+ awaited=_is_awaited(call),
+ is_attribute=dialect.callee_is_attribute(call),
+ io_names=io_names,
+ has_db_import=has_db_import,
+ )
+
+ def magnitude(loops: list[Node]) -> str:
+ """Grows if any enclosing loop grows; bounded only if every one is, since a
+ retry loop inside a loop over rows still runs once per row."""
+ found = {dialect.loop_magnitude(loop, probe) or "unknown" for loop in loops}
+ if "grows_with_data" in found:
+ return "grows_with_data"
+ return "bounded" if found == {"bounded"} else "unknown"
+
+ def loop_facts(node: Node, sink: bool) -> LoopFacts | None:
+ """Facts of *node*'s innermost loop; batch and bound only mean something at a sink."""
+ if not do_loop_facts:
+ return None
+ loops = _enclosing_loops(node, dialect, loop_kinds, fn_kinds)
+ if not loops:
+ return None
+ loop = loops[0]
+ facts = LoopFacts(
+ chunked=do_chunked and dialect.is_chunked_loop(loop),
+ magnitude=magnitude(loops) if do_magnitude else "unknown",
+ batch=dialect.batch_form(node, loop, probe) if sink and do_batch else None,
+ concurrency_bound=dialect.concurrency_bound(node, loop) if sink and do_bound else None,
+ )
+ return facts if facts != LoopFacts() else None
hits: list[PerfHit] = []
# Per-enclosing-function accumulators keyed by the function's start line
@@ -262,6 +307,8 @@ def _collect_perf_hits(
fn_acc: dict[
int, tuple[str | None, dict[str, int], dict[str, int], list[str | None], list]
] = {}
+ # func_start -> {call_line: magnitude} for the loop-call targets above.
+ call_magnitudes: dict[int, dict[int, str]] = {}
def _acc(
func_start: int, func_name: str | None
@@ -372,12 +419,9 @@ def _collect_perf_hits(
)
if kind is not None:
if loop_depth >= 1:
- chunked = do_chunked and _in_chunked_loop(node, dialect, loop_kinds, fn_kinds)
+ facts = loop_facts(node, sink=True)
hits.append(
- PerfHit(
- "io_in_loop", line, next_func, kind, func_start=next_start,
- chunked=chunked,
- )
+ PerfHit("io_in_loop", line, next_func, kind, func_start=next_start, loop=facts)
)
if do_serial_await and awaited:
# An *awaited* sink in a loop body is additionally a
@@ -388,7 +432,7 @@ def _collect_perf_hits(
hits.append(
PerfHit(
"serial_await_in_loop", line, next_func, kind,
- func_start=next_start, chunked=chunked,
+ func_start=next_start, loop=facts,
)
)
if do_nested_io and loop_depth >= 2 and outer_iter:
@@ -402,7 +446,7 @@ def _collect_perf_hits(
hits.append(
PerfHit(
"nested_loop_with_io", line, next_func, kind,
- func_start=next_start, chunked=chunked,
+ func_start=next_start, loop=facts,
)
)
else:
@@ -446,7 +490,12 @@ def _collect_perf_hits(
else None
)
if marker is not None:
- hits.append(PerfHit(marker, line, next_func, "", func_start=next_start))
+ hits.append(
+ PerfHit(
+ marker, line, next_func, "", func_start=next_start,
+ loop=loop_facts(node, sink=False),
+ )
+ )
elif method:
# A loop-nested call to a non-sink helper: a candidate
# entry for cross-function reachability (PR4). Keep the
@@ -454,6 +503,12 @@ def _collect_perf_hits(
targets = _acc(next_start, next_func)[0]
if method not in targets:
targets[method] = line
+ if do_magnitude:
+ facts = loop_facts(node, sink=False)
+ if facts is not None and facts.magnitude != "unknown":
+ call_magnitudes.setdefault(next_start, {})[line] = (
+ facts.magnitude
+ )
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.
@@ -491,6 +546,7 @@ def _collect_perf_hits(
next_func,
"",
func_start=next_start,
+ loop=loop_facts(node, sink=False),
)
)
elif do_loop_stmt_marker:
@@ -503,6 +559,7 @@ def _collect_perf_hits(
next_func,
"",
func_start=next_start,
+ loop=loop_facts(node, sink=False),
)
)
@@ -587,6 +644,7 @@ def _collect_perf_hits(
nested_loop_line=misc[0],
blocking_sink_kind=misc[1],
blocking_sink_line=misc[2],
+ loop_call_magnitudes=tuple(sorted(call_magnitudes.get(start, {}).items())),
)
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)
diff --git a/packages/core/src/repowise/core/analysis/health/engine.py b/packages/core/src/repowise/core/analysis/health/engine.py
index 59bf6eba5..99bfeb9b4 100644
--- a/packages/core/src/repowise/core/analysis/health/engine.py
+++ b/packages/core/src/repowise/core/analysis/health/engine.py
@@ -111,6 +111,9 @@ log = structlog.get_logger(__name__)
# and a declining call's arguments are not scanned, so an assertion passed as an
# argument still does not stand in for the header's oracle.
#
+# v23: loop-shaped perf findings carry the loop's magnitude, the bulk form of a
+# per-key sink and any concurrency bound around it; a v22 store has none.
+#
# v22: perf findings in a chunked loop carry ``chunked_iteration``, and
# ``unbounded_read_reduced_in_memory`` is a new marker; a v21 store has neither.
#
@@ -244,7 +247,7 @@ log = structlog.get_logger(__name__)
# forms. Files that were counted untested and are not become tested, which
# moves untested-hotspot findings and the scores that carry them, on every
# language with a prefix or spec convention rather than Ruby alone.
-HEALTH_ANALYZER_VERSION = 22
+HEALTH_ANALYZER_VERSION = 23
def walked_functions(
diff --git a/packages/core/src/repowise/core/analysis/health/perf/actionability.py b/packages/core/src/repowise/core/analysis/health/perf/actionability.py
index 41a7cffc1..27e0b2c0b 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/actionability.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/actionability.py
@@ -26,7 +26,6 @@ FixStrategy = Literal[
"parallelize_independent_awaits",
"replace_membership_collection",
"buffer_string_accumulation",
- "hoist_loop_invariant_resource",
"batch_or_prefetch_io",
"shrink_lock_scope",
"push_reduction_into_query",
@@ -44,9 +43,20 @@ class PerformanceFix:
strategy: FixStrategy
safety: FixSafety
rationale: str
+ # The concrete construct the edit uses (a bulk call, a bound), when one was found.
+ api: str | None = None
def as_dict(self) -> dict[str, str]:
- return {"strategy": self.strategy, "safety": self.safety, "rationale": self.rationale}
+ out = {"strategy": self.strategy, "safety": self.safety, "rationale": self.rationale}
+ if self.api:
+ out["api"] = self.api
+ return out
+
+
+def _shared(details: list[dict[str, Any]], key: str) -> Any:
+ """The one value every detail carries under *key*, else ``None``."""
+ values = {detail.get(key) for detail in details}
+ return values.pop() if len(values) == 1 else None
@dataclass(frozen=True, slots=True)
@@ -116,6 +126,19 @@ def assess_fix(
if not all(detail.get("dataflow_verified") for detail in details):
return FixAssessment(None, ("loop_carried_dependence_proof",))
if boundary in CONCURRENCY_SENSITIVE_BOUNDARIES:
+ # A small fixed trip count is not a bound: that is what a retry loop
+ # looks like, and retries must stay sequential.
+ bound = _shared(details, "concurrency_bound")
+ if bound:
+ return FixAssessment(
+ PerformanceFix(
+ "parallelize_independent_awaits",
+ "proven",
+ f"Iterations are independent and each await already runs under {bound}.",
+ bound,
+ ),
+ (),
+ )
# Independence is proven; nothing here bounds the fan-out.
return FixAssessment(
PerformanceFix(
@@ -173,6 +196,29 @@ def assess_fix(
# Filesystem and subprocess repetition is real, but there is no
# batch or prefetch operation to point the caller at.
return FixAssessment(None, ("batch_operation_for_boundary",))
+ form = _shared(details, "batch_form")
+ if form:
+ if _shared(details, "batch_equivalent") is True:
+ return FixAssessment(
+ PerformanceFix(
+ "batch_or_prefetch_io",
+ "proven",
+ f"Every call filters on the loop's own key, so {form} covers the same "
+ "rows, and nothing else in the loop can observe the difference.",
+ form,
+ ),
+ (),
+ )
+ return FixAssessment(
+ PerformanceFix(
+ "batch_or_prefetch_io",
+ "advisory",
+ f"Every call filters on the loop's own key, so {form} is the bulk form; "
+ "the per-key call limits, orders or shares the loop with other I/O.",
+ form,
+ ),
+ ("result_equivalence",),
+ )
return FixAssessment(
PerformanceFix(
"batch_or_prefetch_io",
@@ -198,16 +244,9 @@ def assess_fix(
("shared_state_ordering",),
)
if marker == "resource_construction_in_loop":
- if not all(detail.get("resource_invariant") is True for detail in details):
- return FixAssessment(None, ("loop_invariant_construction_proof",))
- return FixAssessment(
- PerformanceFix(
- "hoist_loop_invariant_resource",
- "proven",
- "Dataflow proves construction arguments and lifetime are loop invariant.",
- ),
- (),
- )
+ # Hoisting needs per-argument dataflow plus a guard against attribute
+ # mutation (``Client(token=self.token)``); neither exists, so no strategy.
+ return FixAssessment(None, ("loop_invariant_construction_proof",))
return FixAssessment(None, ("supported_strategy_for_marker",))
diff --git a/packages/core/src/repowise/core/analysis/health/perf/crossfn.py b/packages/core/src/repowise/core/analysis/health/perf/crossfn.py
index 2f0b01e9b..4649461e3 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/crossfn.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/crossfn.py
@@ -88,4 +88,5 @@ def collect_crossfn_io_in_loop(
kind=CROSSFN_KIND,
index=index,
max_depth=max_depth,
+ carry_loop=True,
)
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 f93327b21..07a20c07b 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
@@ -66,11 +66,15 @@ pillar depends on.
from __future__ import annotations
-from typing import TYPE_CHECKING
+from typing import TYPE_CHECKING, ClassVar
if TYPE_CHECKING:
+ from collections.abc import Iterator
+
from tree_sitter import Node
+ from ..loop_facts import BatchForm, LoopMagnitude, SinkProbe
+
# Node types whose callee is a member access (``x.foo()``) rather than a bare
# identifier call. Covers Python ``attribute``, TS ``member_expression``, C++/
# Rust ``field_expression``, Go ``selector_expression``. Languages that need
@@ -275,6 +279,98 @@ class BasePerfDialect:
"""True when this loop walks its data a chunk at a time (already batched)."""
return False
+ # -- promotion facts (perf/loop_facts.py); ``None`` means "not settled" ----
+
+ # The grammar's ``await <expr>`` node, and the key-hop node kinds with the
+ # field naming their receiver (``r.id`` / ``r["id"]``).
+ await_kind: str = ""
+ key_hops: ClassVar[dict[str, str]] = {}
+ # Statements that bind a name (kind -> field holding the target), nodes that
+ # run their child conditionally, statements that leave an iteration early,
+ # and scopes whose body does not run as part of the loop.
+ binding_fields: ClassVar[dict[str, str]] = {}
+ branch_kinds: frozenset[str] = frozenset()
+ exit_kinds: frozenset[str] = frozenset()
+ scope_kinds: frozenset[str] = frozenset()
+
+ def loop_magnitude(self, loop: Node, probe: SinkProbe) -> LoopMagnitude | None:
+ """Whether the loop's iterable grows with data or has a fixed small bound."""
+ return None
+
+ def batch_form(self, sink: Node, loop: Node, probe: SinkProbe) -> BatchForm | None:
+ """The bulk counterpart of *sink* when its only varying key is the loop's element."""
+ return None
+
+ def concurrency_bound(self, sink: Node, loop: Node) -> str | None:
+ """The semaphore or limiter *sink* already runs under inside the loop body."""
+ return None
+
+ @staticmethod
+ def _walk(node: Node, prune: frozenset[str] = frozenset()) -> Iterator[Node]:
+ """*node*'s subtree, not descending below node kinds in *prune*."""
+ stack = [node]
+ while stack:
+ cur = stack.pop()
+ yield cur
+ if cur.type not in prune or cur == node:
+ stack.extend(cur.children)
+
+ @staticmethod
+ def _grows(magnitude: LoopMagnitude | None) -> LoopMagnitude | None:
+ """A projection of a growing source grows; nothing else carries through it."""
+ return magnitude if magnitude == "grows_with_data" else None
+
+ @staticmethod
+ def _within(container: Node, node: Node) -> bool:
+ return container.start_byte <= node.start_byte and node.end_byte <= container.end_byte
+
+ def _is_key_of(self, node: Node, ref: Node) -> bool:
+ """*node* is the loop element *ref* itself, or one hop into it (``r.id``, ``r["id"]``)."""
+ if node == ref:
+ return True
+ field = self.key_hops.get(node.type)
+ return field is not None and node.child_by_field_name(field) == ref
+
+ def _reads_loop_local(self, sink: Node, body: Node) -> bool:
+ """*sink* reads a name the loop body binds, so its key is not the element alone."""
+ bound: set[bytes | None] = set()
+ for node in self._walk(body, prune=self.scope_kinds):
+ field = self.binding_fields.get(node.type)
+ target = node.child_by_field_name(field) if field else None
+ # ``rec.x = ...`` / ``seen[k] = ...`` mutate a value; they bind no name.
+ if target is not None and target.type not in self.key_hops:
+ bound.update(n.text for n in (target, *target.children) if n.type == "identifier")
+ return any(n.type == "identifier" and n.text in bound for n in self._walk(sink))
+
+ def _unconditional(self, sink: Node, body: Node) -> bool:
+ """*sink* runs on every iteration: no branch around it, no early exit in the body."""
+ cur = sink.parent
+ while cur is not None and cur != body:
+ if cur.type in self.branch_kinds:
+ return False
+ cur = cur.parent
+ return not any(n.type in self.exit_kinds for n in self._walk(body, prune=self.scope_kinds))
+
+ def _only_io_in_body(self, sink: Node, body: Node, probe: SinkProbe) -> bool:
+ """*sink* is the loop body's only I/O sink and its only await.
+
+ Any other one could write what the batched read would have read first.
+ Ceiling: a helper call that does I/O out of sight is not seen.
+ """
+ own = sink
+ while own.parent is not None and own.parent.type == "parenthesized_expression":
+ own = own.parent
+ if own.parent is not None and own.parent.type == self.await_kind:
+ own = own.parent
+ for node in self._walk(body):
+ if self._within(own, node):
+ continue
+ if node.type == self.await_kind and node.is_named:
+ return False
+ if node.child_by_field_name("function") is not None and probe(node) is not None:
+ return False
+ return True
+
def is_string_concat(self, node: Node) -> bool:
"""True if *node* is a ``+=`` accumulation onto a string."""
if not self.aug_assign_kinds or node.type not in self.aug_assign_kinds:
diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/python.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/python.py
index 2a02cb294..47bd9a101 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/python.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/python.py
@@ -8,13 +8,17 @@ zero Python behavior — the defect golden + perf suite lock that.
from __future__ import annotations
-from typing import TYPE_CHECKING
+import re
+from typing import TYPE_CHECKING, ClassVar
+from ..loop_facts import BatchForm
from .base import BasePerfDialect
if TYPE_CHECKING:
from tree_sitter import Node
+ from ..loop_facts import LoopMagnitude, SinkProbe
+
# Network round-trip verbs (shared across languages that name HTTP methods).
HTTP_VERBS: frozenset[str] = frozenset(
{"get", "post", "put", "delete", "patch", "head", "options", "request", "send", "stream"}
@@ -161,6 +165,27 @@ _PY_NONLIST_RHS_KINDS: frozenset[str] = frozenset(
)
_PY_NONLIST_BUILTINS: frozenset[str] = frozenset({"set", "dict", "frozenset"})
+# Promotion facts (perf/loop_facts.py). Calls and attributes that read a query
+# result out without changing how many rows it holds.
+_PY_RESULT_PROJECTIONS: frozenset[str] = frozenset(
+ {"all", "scalars", "fetchall", "json", "data", "rows"}
+)
+_PY_BOUND_NAME_RE = re.compile(r"(?i)retr|attempt")
+_PY_MUTATE_METHODS: frozenset[str] = frozenset({"append", "extend", "add", "insert"})
+# A per-key call that limits or orders its rows is not the read one IN query makes.
+_PY_PER_KEY_LIMITS: frozenset[str] = frozenset(
+ {
+ "limit", "single", "maybe_single", "first", "one", "scalar_one",
+ "scalar_one_or_none", "order", "order_by", "range", "offset",
+ }
+)
+_PY_ONE_ROW: frozenset[str] = _PY_PER_KEY_LIMITS | {"scalar"}
+_PY_READ_CAPS: frozenset[str] = _PY_ONE_ROW - {"order", "order_by", "offset"}
+_PY_WRITES: frozenset[str] = frozenset({"update", "insert", "upsert", "values"})
+_PY_LOOP_EXITS: frozenset[str] = frozenset({"break_statement", "return_statement"})
+_PY_SCOPES: frozenset[str] = frozenset({"function_definition", "lambda", "class_definition"})
+_PY_SEM_NAME_RE = re.compile(r"(?i)(sem|semaphore|limiter|limit)$")
+
class PythonPerfDialect(BasePerfDialect):
language = "python"
@@ -665,5 +690,235 @@ class PythonPerfDialect(BasePerfDialect):
return fn.text.decode("utf-8", "replace") in _PY_NONLIST_BUILTINS
return False
+ # -- promotion facts (perf/loop_facts.py) ---------------------------------
+
+ await_kind = "await"
+ key_hops: ClassVar[dict[str, str]] = {"attribute": "object", "subscript": "value"}
+ binding_fields: ClassVar[dict[str, str]] = {
+ "assignment": "left",
+ "augmented_assignment": "left",
+ "for_statement": "left",
+ "named_expression": "name",
+ }
+ branch_kinds = frozenset(
+ {
+ "if_statement", "try_statement", "match_statement", "while_statement",
+ "conditional_expression", "boolean_operator",
+ }
+ )
+ exit_kinds = frozenset(
+ {"break_statement", "continue_statement", "return_statement", "raise_statement"}
+ )
+ scope_kinds = _PY_SCOPES
+
+ def loop_magnitude(self, loop: Node, probe: SinkProbe) -> LoopMagnitude | None:
+ if loop.type != "for_statement":
+ return None
+ return self._magnitude(loop.child_by_field_name("right"), loop, probe, hop=True)
+
+ def _magnitude(
+ self, expr: Node | None, loop: Node, probe: SinkProbe, *, hop: bool
+ ) -> LoopMagnitude | None:
+ """Grows (a query result or a directory listing), bounded, or ``None``.
+
+ *hop* allows one step through the name's last assignment before the
+ loop; a parameter has none and stays unknown on purpose.
+ """
+ if expr is None:
+ return None
+ if expr.type in ("await", "parenthesized_expression"):
+ inner = next((c for c in expr.children if c.is_named), None)
+ return self._magnitude(inner, loop, probe, hop=hop)
+ if expr.type == "identifier" and hop and expr.text:
+ rhs = self._reaching_rhs(loop, expr.text)
+ return self._magnitude(rhs, loop, probe, hop=False)
+ if expr.type == "subscript":
+ return self._slice_magnitude(expr)
+ if expr.type == "boolean_operator" and any(c.type == "or" for c in expr.children):
+ # ``(await q.execute()).data or []``: the fallback is empty, the read is not.
+ left = expr.child_by_field_name("left")
+ return self._grows(self._magnitude(left, loop, probe, hop=hop))
+ if expr.type == "attribute":
+ attr = expr.child_by_field_name("attribute")
+ if attr is None or (attr.text or b"").decode() not in _PY_RESULT_PROJECTIONS:
+ return None
+ return self._grows(self._magnitude(expr.child_by_field_name("object"), loop, probe, hop=hop))
+ return self._call_magnitude(expr, loop, probe, hop=hop) if expr.type == "call" else None
+
+ def _call_magnitude(
+ self, expr: Node, loop: Node, probe: SinkProbe, *, hop: bool
+ ) -> LoopMagnitude | None:
+ method = self.callee_method_name(expr) or ""
+ member = self.callee_is_attribute(expr)
+ if method == "range" and not member:
+ return self._range_magnitude(expr, loop, probe, hop=hop)
+ if probe(expr) in ("db", "network"):
+ # A read capped in the query (``.limit(n)``) is as large as its cap.
+ capped = any(
+ n.type == "call" and self.callee_method_name(n) in _PY_READ_CAPS
+ for n in self._walk(expr)
+ )
+ return None if capped else "grows_with_data"
+ if member and method in _PY_RESULT_PROJECTIONS:
+ receiver = expr.child_by_field_name("function").child_by_field_name("object")
+ return self._grows(self._magnitude(receiver, loop, probe, hop=hop))
+ return None
+
+ def _range_magnitude(
+ self, call: Node, loop: Node, probe: SinkProbe, *, hop: bool
+ ) -> LoopMagnitude | None:
+ """``range(len(rows))`` grows with ``rows``; ``range(MAX_RETRIES)`` is bounded."""
+ args = [c for c in call.child_by_field_name("arguments").children if c.is_named]
+ if not args:
+ return None
+ stop = args[1] if len(args) > 1 else args[0]
+ if stop.type == "call" and self.callee_method_name(stop) == "len":
+ inner = [c for c in stop.child_by_field_name("arguments").children if c.is_named]
+ if len(inner) == 1:
+ return self._grows(self._magnitude(inner[0], loop, probe, hop=hop))
+ return None
+ path = self._dotted_path(stop) if stop.type in ("identifier", "attribute") else None
+ last = path.rsplit(".", 1)[-1] if path else ""
+ if (stop.type == "identifier" and last.isupper() and len(last) > 1) or (
+ last and _PY_BOUND_NAME_RE.search(last)
+ ):
+ return "bounded"
+ return None
+
+ def _slice_magnitude(self, subscript: Node) -> LoopMagnitude | None:
+ """``xs[:5]`` / ``xs[:LIMIT]`` / ``xs[i:i + 4]``: a constant-width slice."""
+ piece = subscript.child_by_field_name("subscript")
+ if piece is None or piece.type != "slice" or sum(c.type == ":" for c in piece.children) != 1:
+ return None
+ colon = next(i for i, c in enumerate(piece.children) if c.type == ":")
+ start = next((c for c in piece.children[:colon] if c.is_named), None)
+ stop = next((c for c in piece.children[colon + 1 :] if c.is_named), None)
+ if stop is None:
+ return None
+ if start is None:
+ text = (stop.text or b"").decode()
+ constant = stop.type == "integer" or (
+ stop.type == "identifier" and text.isupper() and len(text) > 1
+ )
+ return "bounded" if constant else None
+ if stop.type == "binary_operator":
+ left, right = stop.child_by_field_name("left"), stop.child_by_field_name("right")
+ same_start = left is not None and self._dotted_path(left) == self._dotted_path(start)
+ if same_start and right is not None and right.type == "integer":
+ return "bounded"
+ return None
+
+ def _reaching_rhs(self, loop: Node, name: bytes) -> Node | None:
+ """The right side of the last ``name = ...`` before *loop* in its function."""
+ scope = loop.parent
+ while scope is not None and scope.type != "function_definition":
+ scope = scope.parent
+ if scope is None:
+ return None
+ last: Node | None = None
+ for node in self._walk(scope, prune=_PY_SCOPES):
+ if node.type != "assignment" or node.end_byte > loop.start_byte:
+ continue
+ left = node.child_by_field_name("left")
+ named = left is not None and left.type == "identifier" and left.text == name
+ if named and (last is None or node.start_byte > last.start_byte):
+ last = node
+ return last.child_by_field_name("right") if last is not None else None
+
+ def batch_form(self, sink: Node, loop: Node, probe: SinkProbe) -> BatchForm | None:
+ target = loop.child_by_field_name("left")
+ if loop.type != "for_statement" or target is None or target.type != "identifier":
+ return None
+ body = self.loop_body(loop) or loop
+ iterable = self.loop_iterable_name(loop)
+ for node in self._walk(body):
+ if (
+ node.type == "call"
+ and self.callee_is_attribute(node)
+ and self.callee_root_name(node) == iterable
+ and self.callee_method_name(node) in _PY_MUTATE_METHODS
+ ):
+ return None # a worklist: its keys are not known before the loop
+ refs = [n for n in self._walk(sink) if n.type == "identifier" and n.text == target.text]
+ methods = {self.callee_method_name(n) for n in self._walk(sink) if n.type == "call"}
+ if len(refs) != 1 or methods & _PY_WRITES or self._reads_loop_local(sink, body):
+ return None
+ call = self._keyed_filter(sink, refs[0], methods)
+ if call is None:
+ return None
+ equivalent = (
+ not methods & _PY_PER_KEY_LIMITS
+ and not self._limited_downstream(sink, body)
+ and self._only_io_in_body(sink, body, probe)
+ # Batched, a delete also removes rows a skipped iteration would have left.
+ and ("delete" not in methods or self._unconditional(sink, body))
+ )
+ return BatchForm(call, equivalent)
+
+ def _limited_downstream(self, sink: Node, body: Node) -> bool:
+ """The result is cut to one row after the call (``(await q).first()``,
+ ``res = await q`` then ``res.scalar_one()``), which one IN query does not do."""
+ names: set[bytes | None] = set()
+ cur = sink
+ while cur.parent is not None and cur.parent != body:
+ cur = cur.parent
+ if cur.type == "call" and self.callee_method_name(cur) in _PY_ONE_ROW:
+ return True
+ left = cur.child_by_field_name("left") if cur.type == "assignment" else None
+ if left is not None and left.type == "identifier":
+ names.add(left.text)
+ return any(
+ n.type == "call"
+ and self.callee_method_name(n) in _PY_ONE_ROW
+ and (self.callee_root_name(n) or "").encode() in names
+ for n in self._walk(body)
+ )
+
+ def _keyed_filter(self, sink: Node, ref: Node, methods: set[str | None]) -> str | None:
+ """The bulk filter when *sink* selects or deletes by equality on *ref*."""
+ for node in self._walk(sink):
+ if node.type == "call" and self.callee_method_name(node) == "eq":
+ # supabase-py: ``.table(t).select(...).eq("col", key)``
+ args = [c for c in node.child_by_field_name("arguments").children if c.is_named]
+ if (
+ len(args) == 2
+ and args[0].type == "string"
+ and self._is_key_of(args[1], ref)
+ and ("select" in methods) != ("delete" in methods)
+ ):
+ column = next(
+ (c.text.decode() for c in args[0].children if c.type == "string_content"),
+ None,
+ )
+ return f'.in_("{column}", keys)' if column else None
+ if node.type == "comparison_operator" and any(c.type == "==" for c in node.children):
+ # SQLAlchemy: ``.where(M.col == key)`` / ``.filter(M.col == key)``
+ sides = [c for c in node.children if c.is_named]
+ if len(sides) != 2:
+ continue
+ for column, key in (sides, sides[::-1]):
+ path = self._dotted_path(column)
+ if path and "." in path and self._is_key_of(key, ref):
+ return f"{path}.in_(keys)"
+ return None
+
+ def concurrency_bound(self, sink: Node, loop: Node) -> str | None:
+ body = self.loop_body(loop) or loop
+ if any(n.type in _PY_LOOP_EXITS for n in self._walk(body, prune=_PY_SCOPES)):
+ return None # fanned out, it would run the iterations the exit skips
+ cur = sink.parent
+ while cur is not None and cur != loop:
+ if cur.type == "with_statement" and any(c.type == "async" for c in cur.children):
+ clause = next((c for c in cur.children if c.type == "with_clause"), None)
+ for item in clause.children if clause is not None else ():
+ value = item.child_by_field_name("value") if item.type == "with_item" else None
+ # A name, not a constructor: ``async with Semaphore(5)`` in the
+ # loop makes a fresh bound per iteration, which bounds nothing.
+ path = self._dotted_path(value) if value is not None else None
+ if path and _PY_SEM_NAME_RE.search(path.rsplit(".", 1)[-1]):
+ return path
+ cur = cur.parent
+ return None
+
DIALECT = PythonPerfDialect()
diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/ts_js.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/ts_js.py
index c86cf6280..a6023803c 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/ts_js.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/ts_js.py
@@ -8,14 +8,17 @@ and the TS branches of the walker (``_has_async_modifier`` and the
from __future__ import annotations
-from typing import TYPE_CHECKING
+from typing import TYPE_CHECKING, ClassVar
+from ..loop_facts import BatchForm
from .base import BasePerfDialect
from .python import HTTP_VERBS
if TYPE_CHECKING:
from tree_sitter import Node
+ from ..loop_facts import LoopMagnitude, SinkProbe
+
# TypeScript / JavaScript. Only DISTINCTIVE method names are trusted without
# import resolution: bare create/update/delete/count/exec collide hard with
# Map/Set/Array/RegExp and are pure noise. Generic fs/subprocess verbs are only
@@ -326,6 +329,227 @@ class TsJsPerfDialect(BasePerfDialect):
return None
return ctor.text.decode("utf-8", "replace").split(".")[-1]
+ # -- chunked-loop detection ------------------------------------------------
+
+ def is_chunked_loop(self, node: Node) -> bool:
+ """True when the loop already walks its data one chunk at a time.
+
+ Two idioms: a C-style counter with a non-1 step (``i += CHUNK``), and a
+ ``for...of`` over a ``chunk(xs, n)`` / lodash ``_.chunk(xs, n)`` call.
+ """
+ if node.type == "for_statement":
+ inc = node.child_by_field_name("increment")
+ if inc is None or inc.type != "augmented_assignment_expression":
+ return False
+ if not any(c.type == "+=" for c in inc.children):
+ return False
+ right = inc.child_by_field_name("right")
+ if right is None:
+ return False
+ if right.type == "number" and right.text is not None:
+ try:
+ return float(right.text.decode("utf-8", "replace")) != 1
+ except ValueError:
+ return False
+ return right.type == "identifier" # a named step is not a literal 1
+ if node.type in self._ITERATION_LOOP_KINDS:
+ right = node.child_by_field_name("right")
+ if right is None or right.type != "call_expression":
+ return False
+ return self.callee_method_name(right) == "chunk"
+ return False
+
+ # -- promotion facts (perf/loop_facts.py) ----------------------------------
+
+ #: Filesystem listing calls whose result count scales with data on disk.
+ #: A function/lambda scope: the boundary for the reaching-assignment walk
+ #: (below it) and the "not a loop exit" skip for break/return/throw inside
+ #: a nested closure (a callback's own ``return`` doesn't exit the loop).
+ _FN_KINDS: frozenset[str] = frozenset(
+ {"function_declaration", "function_expression", "arrow_function", "method_definition"}
+ )
+ _MUTATOR_METHODS: frozenset[str] = frozenset({"push", "unshift", "splice"})
+ await_kind = "await_expression"
+ key_hops: ClassVar[dict[str, str]] = {
+ "member_expression": "object",
+ "subscript_expression": "object",
+ }
+ binding_fields: ClassVar[dict[str, str]] = {
+ "variable_declarator": "name",
+ "assignment_expression": "left",
+ "augmented_assignment_expression": "left",
+ "for_in_statement": "left",
+ }
+ branch_kinds = frozenset(
+ {
+ "if_statement", "try_statement", "switch_statement", "while_statement",
+ "do_statement", "ternary_expression", "binary_expression",
+ }
+ )
+ exit_kinds = frozenset(
+ {"break_statement", "continue_statement", "return_statement", "throw_statement"}
+ )
+ scope_kinds = _FN_KINDS
+ _PRISMA_BATCH_METHODS: frozenset[str] = frozenset({"findUnique", "findFirst", "delete"})
+
+ @staticmethod
+ def _unwrap(node: Node) -> Node:
+ """Peel await / parens: ``(await x()).y`` needs both hops stripped to
+ reach the call ``x()`` itself."""
+ cur = node
+ while cur.type in ("await_expression", "parenthesized_expression"):
+ inner = next((c for c in cur.children if c.is_named), None)
+ if inner is None:
+ return cur
+ cur = inner
+ return cur
+
+ def _magnitude_of_expr(
+ self, expr: Node | None, probe: SinkProbe, depth: int = 0
+ ) -> LoopMagnitude | None:
+ """Whether *expr* is provably a growing / bounded source, else None."""
+ if expr is None or depth > 4:
+ return None
+ node = self._unwrap(expr)
+ if node.type == "binary_expression" and any(c.type in ("||", "??") for c in node.children):
+ # ``(await q).data ?? []``: the fallback is empty, the read is not.
+ return self._grows(self._magnitude_of_expr(node.child_by_field_name("left"), probe, depth + 1))
+ if node.type == "call_expression":
+ method = self.callee_method_name(node) or ""
+ if method == "slice":
+ return "bounded" if self._slice_is_bounded(node) else None
+ if probe(node) in ("db", "network"):
+ # A read capped in the query (``take: n``) is as large as its cap.
+ capped = any(
+ n.type == "pair" and (n.child_by_field_name("key") or n).text == b"take"
+ for n in self._walk(node)
+ )
+ return None if capped else "grows_with_data"
+ if method == "json":
+ # ``fetch(url).json()`` / ``(await sink()).json()`` — a
+ # projection of whatever the receiver resolves to.
+ fn = node.child_by_field_name("function")
+ obj = fn.child_by_field_name("object") if fn is not None else None
+ return self._grows(self._magnitude_of_expr(obj, probe, depth + 1))
+ return None
+ if node.type == "member_expression":
+ prop = node.child_by_field_name("property")
+ if prop is not None and prop.text == b"data":
+ inner = self._magnitude_of_expr(node.child_by_field_name("object"), probe, depth + 1)
+ return self._grows(inner)
+ return None
+
+ @staticmethod
+ def _slice_is_bounded(node: Node) -> bool:
+ """``.slice(a, N)`` where N is an integer literal or an ALL_CAPS named
+ constant — a constant-width read, not the whole collection."""
+ args = node.child_by_field_name("arguments")
+ named = [c for c in args.children if c.is_named] if args is not None else []
+ if len(named) != 2:
+ return False
+ end = named[1]
+ if end.type == "number":
+ return True
+ return end.type == "identifier" and bool(end.text) and end.text.decode(
+ "utf-8", "replace"
+ ).isupper()
+
+ def _reaching_assignment(self, loop: Node, name: bytes) -> Node | None:
+ """The value of the last ``name = ...`` before *loop* in its function."""
+ scope = loop.parent
+ while scope is not None and scope.type not in self._FN_KINDS:
+ scope = scope.parent
+ last: tuple[int, Node] | None = None
+ for node in self._walk(scope or loop.parent or loop, prune=self._FN_KINDS):
+ if node.end_byte > loop.start_byte:
+ continue
+ if node.type == "variable_declarator":
+ target, value = node.child_by_field_name("name"), node.child_by_field_name("value")
+ elif node.type == "assignment_expression":
+ target, value = node.child_by_field_name("left"), node.child_by_field_name("right")
+ else:
+ continue
+ named = target is not None and target.type == "identifier" and target.text == name
+ if named and value is not None and (last is None or node.start_byte > last[0]):
+ last = (node.start_byte, value)
+ return last[1] if last is not None else None
+
+ def loop_magnitude(self, loop: Node, probe: SinkProbe) -> LoopMagnitude | None:
+ if not self.is_iteration_loop(loop):
+ return None
+ right = loop.child_by_field_name("right")
+ magnitude = self._magnitude_of_expr(right, probe)
+ if magnitude is None and right is not None and right.type == "identifier" and right.text:
+ return self._magnitude_of_expr(self._reaching_assignment(loop, right.text), probe)
+ return magnitude
+
+ # No ``concurrency_bound``: a limiter wraps its await in a closure
+ # (``limit(() => call())``), and the walker does not count a closure as the
+ # loop body, so no hit could reach it.
+
+ def batch_form(self, sink: Node, loop: Node, probe: SinkProbe) -> BatchForm | None:
+ """Prisma ``m.findUnique/findFirst/delete({ where: { f: key } })`` -> its bulk form."""
+ target = loop.child_by_field_name("left")
+ fn = sink.child_by_field_name("function")
+ if (
+ not self.is_iteration_loop(loop)
+ or target is None
+ or target.type != "identifier"
+ or fn is None
+ or fn.type != "member_expression"
+ ):
+ return None
+ receiver, method = fn.child_by_field_name("object"), self.callee_method_name(sink)
+ if receiver is None or method not in self._PRISMA_BATCH_METHODS:
+ return None
+ body = self.loop_body(loop) or loop
+ iterable = self.loop_iterable_name(loop)
+ for node in self._walk(body):
+ if (
+ node.type == "call_expression"
+ and self.callee_root_name(node) == iterable
+ and self.callee_method_name(node) in self._MUTATOR_METHODS
+ ):
+ return None # a worklist: its keys are not known before the loop
+ refs = [n for n in self._walk(sink) if n.type == "identifier" and n.text == target.text]
+ field = self._where_key(sink, refs[0]) if len(refs) == 1 else None
+ if field is None or self._reads_loop_local(sink, body):
+ return None
+ text = (receiver.text or b"").decode()
+ only_io = self._only_io_in_body(sink, body, probe)
+ if method == "delete":
+ # Batched, a delete also removes rows a skipped iteration would have left.
+ return BatchForm(
+ f"{text}.deleteMany({{ where: {{ {field}: {{ in: keys }} }} }})",
+ only_io and self._unconditional(sink, body),
+ )
+ return BatchForm(
+ f"{text}.findMany({{ where: {{ {field}: {{ in: keys }} }} }})",
+ only_io and method == "findUnique",
+ )
+
+ def _where_key(self, sink: Node, ref: Node) -> str | None:
+ """The field of a one-field ``where`` whose value is the loop element *ref*."""
+ args = sink.child_by_field_name("arguments")
+ options = next((c for c in args.children if c.is_named), None) if args else None
+ if options is None or options.type != "object":
+ return None
+ where = next(
+ (
+ pair.child_by_field_name("value")
+ for pair in options.children
+ if pair.type == "pair" and (pair.child_by_field_name("key") or pair).text == b"where"
+ ),
+ None,
+ )
+ pairs = [c for c in where.children if c.type == "pair"] if where is not None else []
+ if where is None or where.type != "object" or len(pairs) != 1:
+ return None
+ key, value = pairs[0].child_by_field_name("key"), pairs[0].child_by_field_name("value")
+ if key is None or value is None or not self._is_key_of(value, ref):
+ return None
+ return (key.text or b"").decode() or None
+
def list_bound_names(self, root: Node) -> frozenset[str]:
"""Names bound to an array literal (``const arr = [...]``)."""
names: set[str] = set()
diff --git a/packages/core/src/repowise/core/analysis/health/perf/loop_facts.py b/packages/core/src/repowise/core/analysis/health/perf/loop_facts.py
new file mode 100644
index 000000000..97bcc0336
--- /dev/null
+++ b/packages/core/src/repowise/core/analysis/health/perf/loop_facts.py
@@ -0,0 +1,60 @@
+"""What a loop around a performance hit proves about the fix.
+
+A hit says the cost is paid per iteration. These facts say how many iterations
+to expect and whether the obvious fix is provably the same program: a bulk
+counterpart that takes every key at once, or a bound the fan-out would run
+under. Each is read off the syntax of one function by a language dialect, and
+each answers ``None`` or ``unknown`` when that syntax does not settle it.
+"""
+
+from __future__ import annotations
+
+from collections.abc import Callable
+from dataclasses import dataclass
+from typing import TYPE_CHECKING, Any, Literal
+
+if TYPE_CHECKING:
+ from tree_sitter import Node
+
+LoopMagnitude = Literal["grows_with_data", "bounded", "unknown"]
+
+# The boundary kind of a call node when it is an I/O sink, else ``None``.
+SinkProbe = Callable[["Node"], str | None]
+
+
+@dataclass(frozen=True, slots=True)
+class BatchForm:
+ """The bulk counterpart of a per-key sink, written as the code would call it.
+
+ ``equivalent`` is the result-equivalence proof: batching returns the rows
+ the loop would have read, one key at a time, and nothing else in the loop
+ can observe the difference.
+ """
+
+ call: str
+ equivalent: bool
+
+
+@dataclass(frozen=True, slots=True)
+class LoopFacts:
+ chunked: bool = False
+ magnitude: LoopMagnitude = "unknown"
+ batch: BatchForm | None = None
+ concurrency_bound: str | None = None
+
+ def as_details(self) -> dict[str, Any]:
+ """Finding ``details`` keys; defaults are omitted so old findings are unchanged."""
+ details: dict[str, Any] = {}
+ if self.chunked:
+ details["chunked_iteration"] = True
+ if self.magnitude != "unknown":
+ details["loop_magnitude"] = self.magnitude
+ if self.batch is not None:
+ details["batch_form"] = self.batch.call
+ details["batch_equivalent"] = self.batch.equivalent
+ if self.concurrency_bound:
+ details["concurrency_bound"] = self.concurrency_bound
+ return details
+
+
+__all__ = ["BatchForm", "LoopFacts", "LoopMagnitude", "SinkProbe"]
diff --git a/packages/core/src/repowise/core/analysis/health/perf/opportunities.py b/packages/core/src/repowise/core/analysis/health/perf/opportunities.py
index 85586c881..22342e581 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/opportunities.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/opportunities.py
@@ -53,6 +53,7 @@ from .opportunity_rank import (
dominant_marker,
exposure,
leverage,
+ loop_magnitude,
rank_factors,
rank_sort_key,
weakest_provenance,
@@ -164,6 +165,7 @@ def _assemble(key: Any, members: list[Any], cap: int) -> PerformanceOpportunity:
cross_function=key_is_cross_function(key),
)
acted = actionability(assessment, evidence_confidence)
+ magnitude = loop_magnitude(marker, [facts.details for facts in members])
factors = rank_factors(
marker=marker,
boundary=boundary,
@@ -171,6 +173,7 @@ def _assemble(key: Any, members: list[Any], cap: int) -> PerformanceOpportunity:
reachable=reachable,
site_count=len(sites),
provenance=provenance,
+ magnitude=magnitude,
)
return PerformanceOpportunity(
opportunity_id=stable_id(key),
@@ -199,6 +202,7 @@ def _assemble(key: Any, members: list[Any], cap: int) -> PerformanceOpportunity:
"amplification": amplification(marker),
"leverage": leverage(len(sites)),
"change_risk": change_risk(len(files)),
+ "loop_magnitude": magnitude,
},
actionability_state=acted.state,
actionability_reason=acted.reason,
@@ -214,6 +218,7 @@ def _assemble(key: Any, members: list[Any], cap: int) -> PerformanceOpportunity:
"entry_reachability": reachable,
"affected_call_sites": len(sites),
"provenance": provenance,
+ "loop_magnitude": magnitude,
},
),
fix=acted.fix,
diff --git a/packages/core/src/repowise/core/analysis/health/perf/opportunity_rank.py b/packages/core/src/repowise/core/analysis/health/perf/opportunity_rank.py
index 7688cf980..03aaa8bc5 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/opportunity_rank.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/opportunity_rank.py
@@ -70,6 +70,9 @@ CROSS_FUNCTION_POINTS = 1
the loop, so it earns a point that an intra-function hit does not."""
CONTEXT_POINTS = {"production": 3, "tooling": 2, "test": 1, "unknown": 1}
+MAGNITUDE_POINTS = {"grows_with_data": 2, "unknown": 1, "n/a": 1, "bounded": 0}
+"""A loop over a query result is paid again as the data grows; a retry loop or a
+constant-width slice is not. Unknown sits between, so a guess never outranks a fact."""
PROVENANCE_POINTS = {"call-site": 3, "direct": 3, "reliable-edge": 2, "name-fallback": 0}
AMPLIFICATION = {
@@ -129,6 +132,21 @@ def amplification(marker: str) -> str:
return "per_iteration" if marker in MULTIPLIER_POINTS else "unknown"
+def loop_magnitude(marker: str, details: list[dict[str, Any]]) -> str:
+ """Whether the loop's trip count grows with data, read off every member.
+
+ One member proven to grow is enough to say the cause grows; ``bounded``
+ needs every member. Markers that are not paid per iteration have no loop
+ to measure.
+ """
+ if amplification(marker) not in {"per_iteration", "quadratic"}:
+ return "n/a"
+ values = {detail.get("loop_magnitude", "unknown") for detail in details}
+ if "grows_with_data" in values:
+ return "grows_with_data"
+ return "bounded" if values == {"bounded"} else "unknown"
+
+
def exposure(reachable: bool | None) -> str:
"""How closely an entry point reaches this group.
@@ -177,6 +195,7 @@ def rank_factors(
reachable: bool | None,
site_count: int,
provenance: str,
+ magnitude: str,
) -> dict[str, int]:
"""The additive rank terms, published verbatim on every opportunity.
@@ -190,6 +209,7 @@ def rank_factors(
"entry_reachability": 3 if reachable is True else 0,
"affected_call_sites": min(8, int(log2(site_count + 1) * 2)),
"provenance": PROVENANCE_POINTS.get(provenance, 0),
+ "loop_magnitude": MAGNITUDE_POINTS[magnitude],
}
@@ -230,6 +250,7 @@ __all__ = [
"BOUNDARY_POINTS",
"CONTEXT_POINTS",
"CROSS_FUNCTION_POINTS",
+ "MAGNITUDE_POINTS",
"MULTIPLIER_POINTS",
"PROVENANCE_POINTS",
"UNKNOWN_MULTIPLIER_POINTS",
@@ -239,6 +260,7 @@ __all__ = [
"dominant_marker",
"exposure",
"leverage",
+ "loop_magnitude",
"observation_rank",
"rank_factors",
"rank_sort_key",
diff --git a/packages/core/src/repowise/core/analysis/health/perf/sink_reach.py b/packages/core/src/repowise/core/analysis/health/perf/sink_reach.py
index 4ace05652..baa243044 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/sink_reach.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/sink_reach.py
@@ -51,6 +51,7 @@ def collect_sink_reaching_hits(
index: CallGraphIndex | None = None,
max_depth: int = 3,
carry_func_start: bool = False,
+ carry_loop: bool = False,
) -> dict[str, list[PerfHit]]:
"""Hits for entry callees that reach a bare I/O sink, keyed by owning file.
@@ -62,6 +63,8 @@ def collect_sink_reaching_hits(
callers disagree today: the lock pass carries it and the loop pass does not.
Preserved rather than unified, since making them agree is a behaviour change
and this is a de-duplication.
+
+ ``carry_loop`` attaches the owning loop's magnitude, which only the loop pass has.
"""
walked_list = list(walked)
if graph is None or not walked_list:
@@ -118,6 +121,7 @@ def collect_sink_reaching_hits(
entries=entries,
kind=kind,
carry_func_start=carry_func_start,
+ carry_loop=carry_loop,
)
if hits:
out.setdefault(path, []).extend(hits)
@@ -134,8 +138,10 @@ def _hits_for_function(
entries: EntrySelector,
kind: str,
carry_func_start: bool,
+ carry_loop: bool,
) -> list[PerfHit]:
from ..complexity import PerfHit
+ from .loop_facts import LoopFacts
a_sid = index.resolve_function(path, fact.func_start)
if a_sid is None:
@@ -145,6 +151,7 @@ def _hits_for_function(
return []
extra: dict[str, Any] = {"func_start": fact.func_start} if carry_func_start else {}
+ magnitudes = dict(fact.loop_call_magnitudes) if carry_loop else {}
hits: list[PerfHit] = []
seen: set[str] = set()
for target_name, call_line in entries(fact):
@@ -170,6 +177,9 @@ def _hits_for_function(
detail=sink_kind.get(info.sink, ""),
path=(a_sid, *chain),
resolution_basis=basis,
+ loop=LoopFacts(magnitude=magnitudes[call_line])
+ if call_line in magnitudes
+ else None,
**extra,
)
)
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/performance_fix.py b/packages/core/src/repowise/core/analysis/health/refactoring/performance_fix.py
index 7d7808858..49057c80b 100644
--- a/packages/core/src/repowise/core/analysis/health/refactoring/performance_fix.py
+++ b/packages/core/src/repowise/core/analysis/health/refactoring/performance_fix.py
@@ -26,13 +26,24 @@ _STEPS: dict[str, tuple[str | None, str]] = {
),
"replace_membership_collection": (None, "Build a set once before the loop and probe it"),
"buffer_string_accumulation": (None, "Accumulate into a list and join once after the loop"),
- "hoist_loop_invariant_resource": (None, "Construct the resource once before the loop"),
"shrink_lock_scope": ("Move the I/O in {sink} out of the locked section", ""),
"push_reduction_into_query": (
None,
"Select one row per key in the query instead of reducing all rows here",
),
}
+# When the fix names the construct it uses, the step says it instead.
+_API_STEPS: dict[str, tuple[str | None, str]] = {
+ "batch_or_prefetch_io": (
+ None,
+ "Collect the keys before the loop and make one call with {api} in place of the "
+ "per-key filter",
+ ),
+ "parallelize_independent_awaits": (
+ None,
+ "Run the independent awaits concurrently, each still inside {api}",
+ ),
+}
@dataclass(frozen=True, slots=True)
@@ -54,9 +65,11 @@ def fix_steps(
intervention: str | None,
sink: str | None,
locations: list[dict[str, object]],
+ api: str | None = None,
) -> list[dict[str, object]]:
"""Ordered edits for one plan; mechanical only when the strategy is proven."""
- intro, per_site = _STEPS.get(strategy, (None, ""))
+ intro, per_site = (_API_STEPS if api else _STEPS).get(strategy, (None, ""))
+ per_site = per_site.format(api=api) if api else per_site
applicability: Applicability = "mechanical" if safety == "proven" else "judgment"
steps: list[dict[str, object]] = []
if intro:
@@ -139,7 +152,9 @@ def _suggestion(
}
for item in opportunity.evidence
]
- steps = fix_steps(fix.strategy, fix.safety, intervention, opportunity.terminal_sink, locations)
+ steps = fix_steps(
+ fix.strategy, fix.safety, intervention, opportunity.terminal_sink, locations, fix.api
+ )
mechanical = sum(step["applicability"] == "mechanical" for step in steps)
return RefactoringSuggestion(
refactoring_type="performance_fix",
@@ -151,6 +166,7 @@ def _suggestion(
"opportunity_id": opportunity.opportunity_id,
"strategy": fix.strategy,
"safety": fix.safety,
+ "api": fix.api,
"intervention_symbol": intervention,
"affected_locations": locations,
"affected_locations_total": opportunity.affected_call_sites_total,
diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/performance.py b/packages/core/src/repowise/core/persistence/crud/analysis/performance.py
index e4a20816c..4cb9be9d2 100644
--- a/packages/core/src/repowise/core/persistence/crud/analysis/performance.py
+++ b/packages/core/src/repowise/core/persistence/crud/analysis/performance.py
@@ -84,6 +84,7 @@ def opportunity_details(
"rank_factors": dict(opportunity.rank_factors),
"why_ranked": [dict(entry) for entry in opportunity.why_ranked],
"fix_rationale": opportunity.fix.rationale if opportunity.fix else None,
+ **({"fix_api": opportunity.fix.api} if opportunity.fix and opportunity.fix.api else {}),
"siblings": [dict(entry) for entry in opportunity.siblings],
}
diff --git a/packages/server/src/repowise/server/services/performance_health.py b/packages/server/src/repowise/server/services/performance_health.py
index 3065c236e..1ca4d1bef 100644
--- a/packages/server/src/repowise/server/services/performance_health.py
+++ b/packages/server/src/repowise/server/services/performance_health.py
@@ -551,6 +551,7 @@ class PerformanceHealthService:
"strategy": fix_strategy,
"safety": row.fix_safety,
"rationale": details.get("fix_rationale") or "",
+ **({"api": details["fix_api"]} if details.get("fix_api") else {}),
},
}
diff --git a/packages/types/src/health.ts b/packages/types/src/health.ts
index 22e8c4a7a..f38b90ccf 100644
--- a/packages/types/src/health.ts
+++ b/packages/types/src/health.ts
@@ -331,6 +331,8 @@ export interface PerformanceOpportunityFix {
strategy: string;
safety: "proven" | "advisory";
rationale: string;
+ /** The concrete construct the edit uses (a bulk call, a bound), when one was found. */
+ api?: string;
}
export interface PerformanceOpportunityEvidence {
@@ -355,6 +357,13 @@ export interface PerformanceWhyRanked {
points: number;
}
+/**
+ * Whether the loop's trip count grows with data, read off every member of the
+ * group. `n/a` is for markers whose amplification is not per_iteration or
+ * quadratic, so there is no loop to measure.
+ */
+export type PerformanceLoopMagnitude = "grows_with_data" | "bounded" | "unknown" | "n/a";
+
/**
* The facets that are not published anywhere else on the row.
* `confidence` stays evidence confidence and `fix.safety` stays fix safety,
@@ -366,6 +375,8 @@ export interface PerformanceOpportunityFacets {
amplification: string;
leverage: string;
change_risk: string;
+ /** Absent on rows stored before the fact existed. */
+ loop_magnitude?: PerformanceLoopMagnitude;
}
/**
diff --git a/packages/ui/__tests__/health/fixtures/performance-wire-contract.json b/packages/ui/__tests__/health/fixtures/performance-wire-contract.json
index 71044b3af..3c43fc35a 100644
--- a/packages/ui/__tests__/health/fixtures/performance-wire-contract.json
+++ b/packages/ui/__tests__/health/fixtures/performance-wire-contract.json
@@ -70,7 +70,8 @@
"amplification",
"change_risk",
"exposure",
- "leverage"
+ "leverage",
+ "loop_magnitude"
],
"evidence": [
"biomarker_type",
@@ -89,6 +90,7 @@
"value"
],
"fix": [
+ "api",
"rationale",
"safety",
"strategy"
diff --git a/packages/ui/__tests__/health/fixtures/performance.ts b/packages/ui/__tests__/health/fixtures/performance.ts
index 6f06a460d..0b3c97b75 100644
--- a/packages/ui/__tests__/health/fixtures/performance.ts
+++ b/packages/ui/__tests__/health/fixtures/performance.ts
@@ -83,6 +83,7 @@ export function opportunity(
amplification: "per_iteration",
leverage: "local",
change_risk: "moderate",
+ loop_magnitude: "grows_with_data",
},
actionability_state: "advisory",
actionability_reason: "strategy_requires_validation",
@@ -142,6 +143,7 @@ export function page(overrides: Partial<PerformanceOpportunityPage> = {}): Perfo
strategy: "parallelize_independent_awaits",
safety: "proven",
rationale: "Dataflow proves the iterations carry no dependence.",
+ api: "self._sem",
},
}),
opportunity({
diff --git a/packages/ui/__tests__/health/performance-view.test.tsx b/packages/ui/__tests__/health/performance-view.test.tsx
index bfbe570ba..030de201d 100644
--- a/packages/ui/__tests__/health/performance-view.test.tsx
+++ b/packages/ui/__tests__/health/performance-view.test.tsx
@@ -212,6 +212,28 @@ describe("PerformanceView drawer", () => {
expect(within(panel).getByText(/How reliably the call path resolved/)).toBeTruthy();
});
+ it("shows the loop magnitude facet and the fix's concrete api", async () => {
+ render(<PerformanceView adapter={adapter()} />);
+ await openFirstRow();
+ const panel = await screen.findByRole("dialog");
+ expect(within(panel).getByText("Loop magnitude")).toBeTruthy();
+ expect(within(panel).getByText("Grows with data")).toBeTruthy();
+ expect(within(panel).getByText("self._sem")).toBeTruthy();
+ });
+
+ it("hides the loop magnitude field for a n/a magnitude", async () => {
+ const getDetail = vi.fn(async () =>
+ resolvedDetail({
+ facets: { ...opportunity().facets, loop_magnitude: "n/a" },
+ }),
+ );
+ render(<PerformanceView adapter={adapter({ getPerformanceOpportunity: getDetail })} />);
+ await openFirstRow();
+ await waitFor(() => expect(getDetail).toHaveBeenCalled());
+ const panel = await screen.findByRole("dialog");
+ expect(within(panel).queryByText("Loop magnitude")).toBeNull();
+ });
+
it("carries the exact drill-down an agent should call", async () => {
render(<PerformanceView adapter={adapter()} />);
await openFirstRow();
diff --git a/packages/ui/src/health/performance/drawer.tsx b/packages/ui/src/health/performance/drawer.tsx
index 31afde20d..7013ebb43 100644
--- a/packages/ui/src/health/performance/drawer.tsx
+++ b/packages/ui/src/health/performance/drawer.tsx
@@ -253,8 +253,14 @@ function PlanSection({
<p className="mt-1 text-sm text-[var(--color-text-tertiary)]">{presentation.detail}</p>
{opportunity.fix ? (
<p className="mt-2 text-sm text-[var(--color-text-secondary)]">
- Proposed intervention: {humanizeToken(opportunity.fix.strategy).toLowerCase()}.{" "}
- {opportunity.fix.rationale}
+ Proposed intervention: {humanizeToken(opportunity.fix.strategy).toLowerCase()}
+ {opportunity.fix.api ? (
+ <>
+ {" "}
+ (<code className="font-mono text-xs">{opportunity.fix.api}</code>)
+ </>
+ ) : null}
+ . {opportunity.fix.rationale}
</p>
) : null}
{steps && steps.length > 0 ? <PlanSteps steps={steps} /> : null}
@@ -386,6 +392,12 @@ export function OpportunityDrawer({
<Field label="Exposure" value={humanizeToken(current.facets.exposure)} />
<Field label="Leverage" value={humanizeToken(current.facets.leverage)} />
<Field label="Change risk" value={humanizeToken(current.facets.change_risk)} />
+ {current.facets.loop_magnitude && current.facets.loop_magnitude !== "n/a" ? (
+ <Field
+ label="Loop magnitude"
+ value={humanizeToken(current.facets.loop_magnitude)}
+ />
+ ) : null}
</dl>
{current.siblings && current.siblings.length > 0 ? (
<SiblingsNote siblings={current.siblings} onSelect={onSelectById} />
diff --git a/tests/fixtures/perf_corpus/golden_opportunities.json b/tests/fixtures/perf_corpus/golden_opportunities.json
index 59f8c0d04..d24821eb4 100644
--- a/tests/fixtures/perf_corpus/golden_opportunities.json
+++ b/tests/fixtures/perf_corpus/golden_opportunities.json
@@ -5,23 +5,102 @@
"actionability_state": "plan_ready",
"affected_call_sites_total": 1,
"affected_files_total": 1,
- "biomarker_type": "resource_construction_in_loop",
+ "biomarker_type": "io_in_loop",
"biomarker_types": [
- "resource_construction_in_loop"
+ "io_in_loop"
+ ],
+ "boundary_kind": "db",
+ "confidence": "high",
+ "evidence": [
+ {
+ "biomarker_type": "io_in_loop",
+ "file_path": "src/app/owners.py",
+ "finding_id": "finding_fda81ef13ef4cf19ba17",
+ "function_name": "load_owners",
+ "line_end": 22,
+ "line_start": 20,
+ "path": [],
+ "provenance": "direct",
+ "reason": "batch_form_equivalent: io_in_loop at src/app/owners.py:20 repeats work per iteration."
+ }
+ ],
+ "evidence_truncated": false,
+ "execution_context": "production",
+ "facets": {
+ "actionability_confidence": "high",
+ "amplification": "per_iteration",
+ "change_risk": "contained",
+ "exposure": "unknown",
+ "leverage": "isolated",
+ "loop_magnitude": "grows_with_data"
+ },
+ "fix": {
+ "api": ".in_(\"repo_id\", keys)",
+ "rationale": "Every call filters on the loop's own key, so .in_(\"repo_id\", keys) covers the same rows, and nothing else in the loop can observe the difference.",
+ "safety": "proven",
+ "strategy": "batch_or_prefetch_io"
+ },
+ "intervention_symbol": null,
+ "observations_total": 1,
+ "opportunity_id": "perf2_e1734c647c3b26b6dfc4",
+ "performance_model_version": 2,
+ "prerequisites": [],
+ "provenance": "direct",
+ "rank_factors": {
+ "affected_call_sites": 2,
+ "boundary_kind": 4,
+ "entry_reachability": 0,
+ "execution_context": 3,
+ "loop_magnitude": 2,
+ "multiplier_shape": 4,
+ "provenance": 3
+ },
+ "rank_score": 18,
+ "reliable_entry_reachability": null,
+ "resource_fingerprints": [],
+ "shared_path_suffix": [],
+ "siblings": [],
+ "terminal_sink": null,
+ "why_ranked": [
+ {
+ "factor": "boundary_kind",
+ "points": 4,
+ "value": "db"
+ },
+ {
+ "factor": "multiplier_shape",
+ "points": 4,
+ "value": "io_in_loop"
+ },
+ {
+ "factor": "execution_context",
+ "points": 3,
+ "value": "production"
+ }
+ ]
+ },
+ {
+ "actionability_reason": "proven_strategy",
+ "actionability_state": "plan_ready",
+ "affected_call_sites_total": 1,
+ "affected_files_total": 1,
+ "biomarker_type": "serial_await_in_loop",
+ "biomarker_types": [
+ "serial_await_in_loop"
],
"boundary_kind": "network",
"confidence": "high",
"evidence": [
{
- "biomarker_type": "resource_construction_in_loop",
- "file_path": "src/app/clients.py",
- "finding_id": "finding_7598efa2a35d26832a2e",
- "function_name": "each",
- "line_end": 172,
- "line_start": 170,
+ "biomarker_type": "serial_await_in_loop",
+ "file_path": "src/app/fetch.py",
+ "finding_id": "finding_763ec1cb433e3ef63614",
+ "function_name": "fetch_each",
+ "line_end": 42,
+ "line_start": 40,
"path": [],
"provenance": "direct",
- "reason": "resource_construction: resource_construction_in_loop at src/app/clients.py:170 repeats work per iteration."
+ "reason": "bounded_fan_out: serial_await_in_loop at src/app/fetch.py:40 repeats work per iteration."
}
],
"evidence_truncated": false,
@@ -31,16 +110,18 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "unknown",
- "leverage": "isolated"
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
},
"fix": {
- "rationale": "Dataflow proves construction arguments and lifetime are loop invariant.",
+ "api": "self._sem",
+ "rationale": "Iterations are independent and each await already runs under self._sem.",
"safety": "proven",
- "strategy": "hoist_loop_invariant_resource"
+ "strategy": "parallelize_independent_awaits"
},
"intervention_symbol": null,
"observations_total": 1,
- "opportunity_id": "perf2_386a99aa2876c0dd31bd",
+ "opportunity_id": "perf2_cc802c5285ea0c31b02a",
"performance_model_version": 2,
"prerequisites": [],
"provenance": "direct",
@@ -49,10 +130,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 16,
+ "rank_score": 17,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [],
@@ -67,7 +149,7 @@
{
"factor": "multiplier_shape",
"points": 4,
- "value": "resource_construction_in_loop"
+ "value": "serial_await_in_loop"
},
{
"factor": "execution_context",
@@ -126,7 +208,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "entry_reachable",
- "leverage": "local"
+ "leverage": "local",
+ "loop_magnitude": "unknown"
},
"fix": {
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
@@ -147,10 +230,11 @@
"boundary_kind": 4,
"entry_reachability": 3,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 20,
+ "rank_score": 21,
"reliable_entry_reachability": true,
"resource_fingerprints": [],
"shared_path_suffix": [
@@ -227,7 +311,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "entry_reachable",
- "leverage": "local"
+ "leverage": "local",
+ "loop_magnitude": "unknown"
},
"fix": {
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
@@ -248,10 +333,11 @@
"boundary_kind": 4,
"entry_reachability": 3,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 20,
+ "rank_score": 21,
"reliable_entry_reachability": true,
"resource_fingerprints": [],
"shared_path_suffix": [
@@ -329,7 +415,8 @@
"amplification": "quadratic",
"change_risk": "contained",
"exposure": "unknown",
- "leverage": "local"
+ "leverage": "local",
+ "loop_magnitude": "unknown"
},
"fix": {
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
@@ -350,10 +437,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 6,
"provenance": 3
},
- "rank_score": 19,
+ "rank_score": 20,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [
@@ -430,7 +518,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "unknown",
- "leverage": "local"
+ "leverage": "local",
+ "loop_magnitude": "unknown"
},
"fix": {
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
@@ -451,10 +540,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 17,
+ "rank_score": 18,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [
@@ -515,7 +605,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "unknown",
- "leverage": "isolated"
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
},
"fix": {
"rationale": "I/O under the lock is proven, but shared-state ordering must be validated before moving it.",
@@ -535,10 +626,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 5,
"provenance": 3
},
- "rank_score": 17,
+ "rank_score": 18,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [
@@ -596,7 +688,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "unknown",
- "leverage": "isolated"
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
},
"fix": {
"rationale": "Iteration independence is proven; the concurrency the fan-out would create is not bounded by anything this group can see.",
@@ -616,10 +709,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 16,
+ "rank_score": 17,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [],
@@ -677,7 +771,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "not_entry_reachable",
- "leverage": "isolated"
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
},
"fix": {
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
@@ -698,10 +793,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 16,
+ "rank_score": 17,
"reliable_entry_reachability": false,
"resource_fingerprints": [],
"shared_path_suffix": [
@@ -762,7 +858,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "not_entry_reachable",
- "leverage": "isolated"
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
},
"fix": {
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
@@ -783,10 +880,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 16,
+ "rank_score": 17,
"reliable_entry_reachability": false,
"resource_fingerprints": [],
"shared_path_suffix": [
@@ -847,7 +945,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "not_entry_reachable",
- "leverage": "isolated"
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
},
"fix": {
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
@@ -868,10 +967,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 16,
+ "rank_score": 17,
"reliable_entry_reachability": false,
"resource_fingerprints": [],
"shared_path_suffix": [
@@ -932,7 +1032,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "unknown",
- "leverage": "isolated"
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
},
"fix": {
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
@@ -953,10 +1054,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 16,
+ "rank_score": 17,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [
@@ -983,6 +1085,87 @@
}
]
},
+ {
+ "actionability_reason": "strategy_requires_validation",
+ "actionability_state": "advisory",
+ "affected_call_sites_total": 1,
+ "affected_files_total": 1,
+ "biomarker_type": "io_in_loop",
+ "biomarker_types": [
+ "io_in_loop"
+ ],
+ "boundary_kind": "db",
+ "confidence": "high",
+ "evidence": [
+ {
+ "biomarker_type": "io_in_loop",
+ "file_path": "src/app/commits.py",
+ "finding_id": "finding_cdc3430a0de872c130b3",
+ "function_name": "first_commit_each",
+ "line_end": 32,
+ "line_start": 30,
+ "path": [],
+ "provenance": "direct",
+ "reason": "batch_form_limited: io_in_loop at src/app/commits.py:30 repeats work per iteration."
+ }
+ ],
+ "evidence_truncated": false,
+ "execution_context": "production",
+ "facets": {
+ "actionability_confidence": "medium",
+ "amplification": "per_iteration",
+ "change_risk": "contained",
+ "exposure": "unknown",
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
+ },
+ "fix": {
+ "api": ".in_(\"repo_id\", keys)",
+ "rationale": "Every call filters on the loop's own key, so .in_(\"repo_id\", keys) is the bulk form; the per-key call limits, orders or shares the loop with other I/O.",
+ "safety": "advisory",
+ "strategy": "batch_or_prefetch_io"
+ },
+ "intervention_symbol": null,
+ "observations_total": 1,
+ "opportunity_id": "perf2_820a695aba4fe3a6496a",
+ "performance_model_version": 2,
+ "prerequisites": [
+ "result_equivalence"
+ ],
+ "provenance": "direct",
+ "rank_factors": {
+ "affected_call_sites": 2,
+ "boundary_kind": 4,
+ "entry_reachability": 0,
+ "execution_context": 3,
+ "loop_magnitude": 1,
+ "multiplier_shape": 4,
+ "provenance": 3
+ },
+ "rank_score": 17,
+ "reliable_entry_reachability": null,
+ "resource_fingerprints": [],
+ "shared_path_suffix": [],
+ "siblings": [],
+ "terminal_sink": null,
+ "why_ranked": [
+ {
+ "factor": "boundary_kind",
+ "points": 4,
+ "value": "db"
+ },
+ {
+ "factor": "multiplier_shape",
+ "points": 4,
+ "value": "io_in_loop"
+ },
+ {
+ "factor": "execution_context",
+ "points": 3,
+ "value": "production"
+ }
+ ]
+ },
{
"actionability_reason": "strategy_requires_validation",
"actionability_state": "advisory",
@@ -1016,7 +1199,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "unknown",
- "leverage": "isolated"
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
},
"fix": {
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
@@ -1037,10 +1221,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 16,
+ "rank_score": 17,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [
@@ -1100,7 +1285,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "unknown",
- "leverage": "isolated"
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
},
"fix": {
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
@@ -1121,10 +1307,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 2,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 15,
+ "rank_score": 16,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [
@@ -1185,7 +1372,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "unknown",
- "leverage": "isolated"
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
},
"fix": {
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
@@ -1206,10 +1394,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 2
},
- "rank_score": 15,
+ "rank_score": 16,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [
@@ -1236,6 +1425,87 @@
}
]
},
+ {
+ "actionability_reason": "strategy_requires_validation",
+ "actionability_state": "advisory",
+ "affected_call_sites_total": 1,
+ "affected_files_total": 1,
+ "biomarker_type": "io_in_loop",
+ "biomarker_types": [
+ "io_in_loop"
+ ],
+ "boundary_kind": "network",
+ "confidence": "high",
+ "evidence": [
+ {
+ "biomarker_type": "io_in_loop",
+ "file_path": "src/app/retry.py",
+ "finding_id": "finding_1bdd58a5f59d1771055a",
+ "function_name": "post_with_retry",
+ "line_end": 52,
+ "line_start": 50,
+ "path": [],
+ "provenance": "direct",
+ "reason": "retry_loop: io_in_loop at src/app/retry.py:50 repeats work per iteration."
+ }
+ ],
+ "evidence_truncated": false,
+ "execution_context": "production",
+ "facets": {
+ "actionability_confidence": "medium",
+ "amplification": "per_iteration",
+ "change_risk": "contained",
+ "exposure": "unknown",
+ "leverage": "isolated",
+ "loop_magnitude": "bounded"
+ },
+ "fix": {
+ "rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
+ "safety": "advisory",
+ "strategy": "batch_or_prefetch_io"
+ },
+ "intervention_symbol": null,
+ "observations_total": 1,
+ "opportunity_id": "perf2_dc58f1a585a816ec37e9",
+ "performance_model_version": 2,
+ "prerequisites": [
+ "batch_api_contract",
+ "result_equivalence"
+ ],
+ "provenance": "direct",
+ "rank_factors": {
+ "affected_call_sites": 2,
+ "boundary_kind": 4,
+ "entry_reachability": 0,
+ "execution_context": 3,
+ "loop_magnitude": 0,
+ "multiplier_shape": 4,
+ "provenance": 3
+ },
+ "rank_score": 16,
+ "reliable_entry_reachability": null,
+ "resource_fingerprints": [],
+ "shared_path_suffix": [],
+ "siblings": [],
+ "terminal_sink": null,
+ "why_ranked": [
+ {
+ "factor": "boundary_kind",
+ "points": 4,
+ "value": "network"
+ },
+ {
+ "factor": "multiplier_shape",
+ "points": 4,
+ "value": "io_in_loop"
+ },
+ {
+ "factor": "execution_context",
+ "points": 3,
+ "value": "production"
+ }
+ ]
+ },
{
"actionability_reason": "strategy_requires_validation",
"actionability_state": "advisory",
@@ -1270,7 +1540,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "unknown",
- "leverage": "isolated"
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
},
"fix": {
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
@@ -1291,10 +1562,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 1,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 14,
+ "rank_score": 15,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [
@@ -1355,7 +1627,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "unknown",
- "leverage": "isolated"
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
},
"fix": {
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
@@ -1377,10 +1650,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 0
},
- "rank_score": 13,
+ "rank_score": 14,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [
@@ -1438,7 +1712,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "unknown",
- "leverage": "isolated"
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
},
"fix": {
"rationale": "The collection is proven list-backed; element hashability and ordering/identity use still require validation.",
@@ -1459,10 +1734,11 @@
"boundary_kind": 0,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 3,
"provenance": 3
},
- "rank_score": 11,
+ "rank_score": 12,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [],
@@ -1517,7 +1793,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "unknown",
- "leverage": "isolated"
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
},
"fix": {
"rationale": "Repeated string accumulation is proven; intermediate accumulator observations still require validation.",
@@ -1537,10 +1814,11 @@
"boundary_kind": 0,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 3,
"provenance": 3
},
- "rank_score": 11,
+ "rank_score": 12,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [],
@@ -1614,7 +1892,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "unknown",
- "leverage": "local"
+ "leverage": "local",
+ "loop_magnitude": "unknown"
},
"fix": null,
"intervention_symbol": "src/app/store.py::flush_all",
@@ -1630,10 +1909,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 5,
"provenance": 3
},
- "rank_score": 18,
+ "rank_score": 19,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [
@@ -1740,7 +2020,8 @@
"amplification": "per_iteration",
"change_risk": "moderate",
"exposure": "unknown",
- "leverage": "shared"
+ "leverage": "shared",
+ "loop_magnitude": "unknown"
},
"fix": null,
"intervention_symbol": "src/app/parse.py::_get_query",
@@ -1756,10 +2037,11 @@
"boundary_kind": 2,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 16,
+ "rank_score": 17,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [
@@ -1801,7 +2083,7 @@
{
"biomarker_type": "resource_construction_in_loop",
"file_path": "src/app/clients.py",
- "finding_id": "finding_82ac8251d294a1dabcf9",
+ "finding_id": "finding_36449d699224e623c1f6",
"function_name": "each_varying",
"line_end": 182,
"line_start": 180,
@@ -1817,7 +2099,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "unknown",
- "leverage": "isolated"
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
},
"fix": null,
"intervention_symbol": null,
@@ -1833,10 +2116,87 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 16,
+ "rank_score": 17,
+ "reliable_entry_reachability": null,
+ "resource_fingerprints": [],
+ "shared_path_suffix": [],
+ "siblings": [],
+ "terminal_sink": null,
+ "why_ranked": [
+ {
+ "factor": "boundary_kind",
+ "points": 4,
+ "value": "network"
+ },
+ {
+ "factor": "multiplier_shape",
+ "points": 4,
+ "value": "resource_construction_in_loop"
+ },
+ {
+ "factor": "execution_context",
+ "points": 3,
+ "value": "production"
+ }
+ ]
+ },
+ {
+ "actionability_reason": "no_supported_strategy",
+ "actionability_state": "investigate",
+ "affected_call_sites_total": 1,
+ "affected_files_total": 1,
+ "biomarker_type": "resource_construction_in_loop",
+ "biomarker_types": [
+ "resource_construction_in_loop"
+ ],
+ "boundary_kind": "network",
+ "confidence": "high",
+ "evidence": [
+ {
+ "biomarker_type": "resource_construction_in_loop",
+ "file_path": "src/app/clients.py",
+ "finding_id": "finding_17256c41ed2a1893e6ce",
+ "function_name": "each",
+ "line_end": 172,
+ "line_start": 170,
+ "path": [],
+ "provenance": "direct",
+ "reason": "resource_construction: resource_construction_in_loop at src/app/clients.py:170 repeats work per iteration."
+ }
+ ],
+ "evidence_truncated": false,
+ "execution_context": "production",
+ "facets": {
+ "actionability_confidence": "low",
+ "amplification": "per_iteration",
+ "change_risk": "contained",
+ "exposure": "unknown",
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
+ },
+ "fix": null,
+ "intervention_symbol": null,
+ "observations_total": 1,
+ "opportunity_id": "perf2_386a99aa2876c0dd31bd",
+ "performance_model_version": 2,
+ "prerequisites": [
+ "loop_invariant_construction_proof"
+ ],
+ "provenance": "direct",
+ "rank_factors": {
+ "affected_call_sites": 2,
+ "boundary_kind": 4,
+ "entry_reachability": 0,
+ "execution_context": 3,
+ "loop_magnitude": 1,
+ "multiplier_shape": 4,
+ "provenance": 3
+ },
+ "rank_score": 17,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [],
@@ -1891,7 +2251,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "unknown",
- "leverage": "isolated"
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
},
"fix": null,
"intervention_symbol": null,
@@ -1907,10 +2268,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 16,
+ "rank_score": 17,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [],
@@ -1968,7 +2330,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "unknown",
- "leverage": "isolated"
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
},
"fix": null,
"intervention_symbol": "src/app/walker.py::scan",
@@ -1984,10 +2347,11 @@
"boundary_kind": 2,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 14,
+ "rank_score": 15,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [
@@ -2048,7 +2412,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "unknown",
- "leverage": "isolated"
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
},
"fix": null,
"intervention_symbol": "src/app/walker.py::index",
@@ -2064,10 +2429,11 @@
"boundary_kind": 2,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 14,
+ "rank_score": 15,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [
@@ -2125,7 +2491,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "unknown",
- "leverage": "isolated"
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
},
"fix": null,
"intervention_symbol": null,
@@ -2141,10 +2508,11 @@
"boundary_kind": 2,
"entry_reachability": 0,
"execution_context": 1,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 12,
+ "rank_score": 13,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [],
@@ -2199,7 +2567,8 @@
"amplification": "per_iteration",
"change_risk": "contained",
"exposure": "unknown",
- "leverage": "isolated"
+ "leverage": "isolated",
+ "loop_magnitude": "unknown"
},
"fix": null,
"intervention_symbol": null,
@@ -2215,10 +2584,11 @@
"boundary_kind": 2,
"entry_reachability": 0,
"execution_context": 1,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 12,
+ "rank_score": 13,
"reliable_entry_reachability": null,
"resource_fingerprints": [],
"shared_path_suffix": [],
diff --git a/tests/fixtures/perf_corpus/golden_plans.json b/tests/fixtures/perf_corpus/golden_plans.json
index e3b421e3c..431c262d8 100644
--- a/tests/fixtures/perf_corpus/golden_plans.json
+++ b/tests/fixtures/perf_corpus/golden_plans.json
@@ -5,15 +5,86 @@
"call_sites": 1,
"file_count": 1,
"files": [
- "src/app/clients.py"
+ "src/app/owners.py"
]
},
"confidence": "high",
"evidence": {
"affected_files_total": 1,
- "biomarker_type": "resource_construction_in_loop",
+ "biomarker_type": "io_in_loop",
+ "biomarker_types": [
+ "io_in_loop"
+ ],
+ "boundary_kind": "db",
+ "execution_context": "production",
+ "observations_total": 1,
+ "provenance": "direct",
+ "rank_factors": {
+ "affected_call_sites": 2,
+ "boundary_kind": 4,
+ "entry_reachability": 0,
+ "execution_context": 3,
+ "loop_magnitude": 2,
+ "multiplier_shape": 4,
+ "provenance": 3
+ },
+ "rank_score": 18,
+ "rationale": "Every call filters on the loop's own key, so .in_(\"repo_id\", keys) covers the same rows, and nothing else in the loop can observe the difference.",
+ "reliable_entry_reachability": null
+ },
+ "file_path": "src/app/owners.py",
+ "impact_delta": 0.0,
+ "line_end": 22,
+ "line_start": 20,
+ "plan": {
+ "affected_locations": [
+ {
+ "file_path": "src/app/owners.py",
+ "function_name": "load_owners",
+ "line_end": 22,
+ "line_start": 20
+ }
+ ],
+ "affected_locations_total": 1,
+ "api": ".in_(\"repo_id\", keys)",
+ "evidence_truncated": false,
+ "intervention_symbol": null,
+ "judgment_steps": 0,
+ "mechanical_steps": 1,
+ "opportunity_id": "perf2_e1734c647c3b26b6dfc4",
+ "paths": [],
+ "paths_total": 1,
+ "safety": "proven",
+ "steps": [
+ {
+ "action": "Collect the keys before the loop and make one call with .in_(\"repo_id\", keys) in place of the per-key filter",
+ "applicability": "mechanical",
+ "file_path": "src/app/owners.py",
+ "line": 20,
+ "order": 1,
+ "symbol": "load_owners"
+ }
+ ],
+ "strategy": "batch_or_prefetch_io"
+ },
+ "refactoring_type": "performance_fix",
+ "source_biomarker": "io_in_loop",
+ "target_symbol": "load_owners"
+ },
+ {
+ "blast_radius": {
+ "call_sites": 1,
+ "file_count": 1,
+ "files": [
+ "src/app/fetch.py"
+ ]
+ },
+ "confidence": "high",
+ "evidence": {
+ "affected_files_total": 1,
+ "biomarker_type": "serial_await_in_loop",
"biomarker_types": [
- "resource_construction_in_loop"
+ "serial_await_in_loop"
],
"boundary_kind": "network",
"execution_context": "production",
@@ -24,50 +95,52 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 16,
- "rationale": "Dataflow proves construction arguments and lifetime are loop invariant.",
+ "rank_score": 17,
+ "rationale": "Iterations are independent and each await already runs under self._sem.",
"reliable_entry_reachability": null
},
- "file_path": "src/app/clients.py",
+ "file_path": "src/app/fetch.py",
"impact_delta": 0.0,
- "line_end": 172,
- "line_start": 170,
+ "line_end": 42,
+ "line_start": 40,
"plan": {
"affected_locations": [
{
- "file_path": "src/app/clients.py",
- "function_name": "each",
- "line_end": 172,
- "line_start": 170
+ "file_path": "src/app/fetch.py",
+ "function_name": "fetch_each",
+ "line_end": 42,
+ "line_start": 40
}
],
"affected_locations_total": 1,
+ "api": "self._sem",
"evidence_truncated": false,
"intervention_symbol": null,
"judgment_steps": 0,
"mechanical_steps": 1,
- "opportunity_id": "perf2_386a99aa2876c0dd31bd",
+ "opportunity_id": "perf2_cc802c5285ea0c31b02a",
"paths": [],
"paths_total": 1,
"safety": "proven",
"steps": [
{
- "action": "Construct the resource once before the loop",
+ "action": "Run the independent awaits concurrently, each still inside self._sem",
"applicability": "mechanical",
- "file_path": "src/app/clients.py",
- "line": 170,
+ "file_path": "src/app/fetch.py",
+ "line": 40,
"order": 1,
- "symbol": "each"
+ "symbol": "fetch_each"
}
],
- "strategy": "hoist_loop_invariant_resource"
+ "strategy": "parallelize_independent_awaits"
},
"refactoring_type": "performance_fix",
- "source_biomarker": "resource_construction_in_loop",
- "target_symbol": "each"
+ "source_biomarker": "serial_await_in_loop",
+ "target_symbol": "fetch_each"
},
{
"blast_radius": {
@@ -93,10 +166,11 @@
"boundary_kind": 4,
"entry_reachability": 3,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 20,
+ "rank_score": 21,
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
"reliable_entry_reachability": true
},
@@ -120,6 +194,7 @@
}
],
"affected_locations_total": 2,
+ "api": null,
"evidence_truncated": false,
"intervention_symbol": "src/app/entry.py::_hit",
"judgment_steps": 3,
@@ -195,10 +270,11 @@
"boundary_kind": 4,
"entry_reachability": 3,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 20,
+ "rank_score": 21,
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
"reliable_entry_reachability": true
},
@@ -222,6 +298,7 @@
}
],
"affected_locations_total": 2,
+ "api": null,
"evidence_truncated": false,
"intervention_symbol": "src/app/repo.py::fetch_page",
"judgment_steps": 3,
@@ -298,10 +375,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 6,
"provenance": 3
},
- "rank_score": 19,
+ "rank_score": 20,
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
"reliable_entry_reachability": null
},
@@ -325,6 +403,7 @@
}
],
"affected_locations_total": 2,
+ "api": null,
"evidence_truncated": false,
"intervention_symbol": "src/app/reindex.py::_write",
"judgment_steps": 3,
@@ -400,10 +479,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 17,
+ "rank_score": 18,
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
"reliable_entry_reachability": null
},
@@ -427,6 +507,7 @@
}
],
"affected_locations_total": 2,
+ "api": null,
"evidence_truncated": false,
"intervention_symbol": "src/app/sync.py::_load",
"judgment_steps": 3,
@@ -502,10 +583,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 5,
"provenance": 3
},
- "rank_score": 17,
+ "rank_score": 18,
"rationale": "I/O under the lock is proven, but shared-state ordering must be validated before moving it.",
"reliable_entry_reachability": null
},
@@ -523,6 +605,7 @@
}
],
"affected_locations_total": 1,
+ "api": null,
"evidence_truncated": false,
"intervention_symbol": "src/app/cache.py::refresh",
"judgment_steps": 1,
@@ -576,10 +659,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 16,
+ "rank_score": 17,
"rationale": "Iteration independence is proven; the concurrency the fan-out would create is not bounded by anything this group can see.",
"reliable_entry_reachability": null
},
@@ -597,6 +681,7 @@
}
],
"affected_locations_total": 1,
+ "api": null,
"evidence_truncated": false,
"intervention_symbol": null,
"judgment_steps": 1,
@@ -645,10 +730,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 16,
+ "rank_score": 17,
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
"reliable_entry_reachability": false
},
@@ -666,6 +752,7 @@
}
],
"affected_locations_total": 1,
+ "api": null,
"evidence_truncated": false,
"intervention_symbol": "src/app/billing.py::handle",
"judgment_steps": 2,
@@ -727,10 +814,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 16,
+ "rank_score": 17,
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
"reliable_entry_reachability": false
},
@@ -748,6 +836,7 @@
}
],
"affected_locations_total": 1,
+ "api": null,
"evidence_truncated": false,
"intervention_symbol": "src/app/orders.py::handle",
"judgment_steps": 2,
@@ -809,10 +898,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 16,
+ "rank_score": 17,
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
"reliable_entry_reachability": false
},
@@ -830,6 +920,7 @@
}
],
"affected_locations_total": 1,
+ "api": null,
"evidence_truncated": false,
"intervention_symbol": "src/app/reports.py::handle",
"judgment_steps": 2,
@@ -891,10 +982,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 16,
+ "rank_score": 17,
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
"reliable_entry_reachability": null
},
@@ -912,6 +1004,7 @@
}
],
"affected_locations_total": 1,
+ "api": null,
"evidence_truncated": false,
"intervention_symbol": "src/app/job.py::run",
"judgment_steps": 2,
@@ -949,6 +1042,77 @@
"source_biomarker": "io_in_loop",
"target_symbol": "src/app/job.py::run"
},
+ {
+ "blast_radius": {
+ "call_sites": 1,
+ "file_count": 1,
+ "files": [
+ "src/app/commits.py"
+ ]
+ },
+ "confidence": "medium",
+ "evidence": {
+ "affected_files_total": 1,
+ "biomarker_type": "io_in_loop",
+ "biomarker_types": [
+ "io_in_loop"
+ ],
+ "boundary_kind": "db",
+ "execution_context": "production",
+ "observations_total": 1,
+ "provenance": "direct",
+ "rank_factors": {
+ "affected_call_sites": 2,
+ "boundary_kind": 4,
+ "entry_reachability": 0,
+ "execution_context": 3,
+ "loop_magnitude": 1,
+ "multiplier_shape": 4,
+ "provenance": 3
+ },
+ "rank_score": 17,
+ "rationale": "Every call filters on the loop's own key, so .in_(\"repo_id\", keys) is the bulk form; the per-key call limits, orders or shares the loop with other I/O.",
+ "reliable_entry_reachability": null
+ },
+ "file_path": "src/app/commits.py",
+ "impact_delta": 0.0,
+ "line_end": 32,
+ "line_start": 30,
+ "plan": {
+ "affected_locations": [
+ {
+ "file_path": "src/app/commits.py",
+ "function_name": "first_commit_each",
+ "line_end": 32,
+ "line_start": 30
+ }
+ ],
+ "affected_locations_total": 1,
+ "api": ".in_(\"repo_id\", keys)",
+ "evidence_truncated": false,
+ "intervention_symbol": null,
+ "judgment_steps": 1,
+ "mechanical_steps": 0,
+ "opportunity_id": "perf2_820a695aba4fe3a6496a",
+ "paths": [],
+ "paths_total": 1,
+ "safety": "advisory",
+ "steps": [
+ {
+ "action": "Collect the keys before the loop and make one call with .in_(\"repo_id\", keys) in place of the per-key filter",
+ "applicability": "judgment",
+ "file_path": "src/app/commits.py",
+ "line": 30,
+ "order": 1,
+ "symbol": "first_commit_each"
+ }
+ ],
+ "strategy": "batch_or_prefetch_io"
+ },
+ "refactoring_type": "performance_fix",
+ "source_biomarker": "io_in_loop",
+ "target_symbol": "first_commit_each"
+ },
{
"blast_radius": {
"call_sites": 1,
@@ -973,10 +1137,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 16,
+ "rank_score": 17,
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
"reliable_entry_reachability": null
},
@@ -994,6 +1159,7 @@
}
],
"affected_locations_total": 1,
+ "api": null,
"evidence_truncated": false,
"intervention_symbol": null,
"judgment_steps": 2,
@@ -1054,10 +1220,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 2,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 15,
+ "rank_score": 16,
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
"reliable_entry_reachability": null
},
@@ -1075,6 +1242,7 @@
}
],
"affected_locations_total": 1,
+ "api": null,
"evidence_truncated": false,
"intervention_symbol": "scripts/backfill.py::run",
"judgment_steps": 2,
@@ -1136,10 +1304,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 2
},
- "rank_score": 15,
+ "rank_score": 16,
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
"reliable_entry_reachability": null
},
@@ -1157,6 +1326,7 @@
}
],
"affected_locations_total": 1,
+ "api": null,
"evidence_truncated": false,
"intervention_symbol": "src/app/edge.py::run",
"judgment_steps": 2,
@@ -1194,6 +1364,85 @@
"source_biomarker": "io_in_loop",
"target_symbol": "src/app/edge.py::run"
},
+ {
+ "blast_radius": {
+ "call_sites": 1,
+ "file_count": 1,
+ "files": [
+ "src/app/retry.py"
+ ]
+ },
+ "confidence": "medium",
+ "evidence": {
+ "affected_files_total": 1,
+ "biomarker_type": "io_in_loop",
+ "biomarker_types": [
+ "io_in_loop"
+ ],
+ "boundary_kind": "network",
+ "execution_context": "production",
+ "observations_total": 1,
+ "provenance": "direct",
+ "rank_factors": {
+ "affected_call_sites": 2,
+ "boundary_kind": 4,
+ "entry_reachability": 0,
+ "execution_context": 3,
+ "loop_magnitude": 0,
+ "multiplier_shape": 4,
+ "provenance": 3
+ },
+ "rank_score": 16,
+ "rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
+ "reliable_entry_reachability": null
+ },
+ "file_path": "src/app/retry.py",
+ "impact_delta": 0.0,
+ "line_end": 52,
+ "line_start": 50,
+ "plan": {
+ "affected_locations": [
+ {
+ "file_path": "src/app/retry.py",
+ "function_name": "post_with_retry",
+ "line_end": 52,
+ "line_start": 50
+ }
+ ],
+ "affected_locations_total": 1,
+ "api": null,
+ "evidence_truncated": false,
+ "intervention_symbol": null,
+ "judgment_steps": 2,
+ "mechanical_steps": 0,
+ "opportunity_id": "perf2_dc58f1a585a816ec37e9",
+ "paths": [],
+ "paths_total": 1,
+ "safety": "advisory",
+ "steps": [
+ {
+ "action": "Add a form of the shared sink that takes every key at once",
+ "applicability": "judgment",
+ "file_path": null,
+ "line": null,
+ "order": 1,
+ "symbol": null
+ },
+ {
+ "action": "Collect the keys before the loop and call the batched form once",
+ "applicability": "judgment",
+ "file_path": "src/app/retry.py",
+ "line": 50,
+ "order": 2,
+ "symbol": "post_with_retry"
+ }
+ ],
+ "strategy": "batch_or_prefetch_io"
+ },
+ "refactoring_type": "performance_fix",
+ "source_biomarker": "io_in_loop",
+ "target_symbol": "post_with_retry"
+ },
{
"blast_radius": {
"call_sites": 1,
@@ -1218,10 +1467,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 1,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 3
},
- "rank_score": 14,
+ "rank_score": 15,
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
"reliable_entry_reachability": null
},
@@ -1239,6 +1489,7 @@
}
],
"affected_locations_total": 1,
+ "api": null,
"evidence_truncated": false,
"intervention_symbol": "tests/test_job.py::run",
"judgment_steps": 2,
@@ -1300,10 +1551,11 @@
"boundary_kind": 4,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 4,
"provenance": 0
},
- "rank_score": 13,
+ "rank_score": 14,
"rationale": "The shared I/O sink is proven; no concrete batch API or result-equivalence proof is available.",
"reliable_entry_reachability": null
},
@@ -1321,6 +1573,7 @@
}
],
"affected_locations_total": 1,
+ "api": null,
"evidence_truncated": false,
"intervention_symbol": "src/app/guess.py::run",
"judgment_steps": 2,
@@ -1382,10 +1635,11 @@
"boundary_kind": 0,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 3,
"provenance": 3
},
- "rank_score": 11,
+ "rank_score": 12,
"rationale": "The collection is proven list-backed; element hashability and ordering/identity use still require validation.",
"reliable_entry_reachability": null
},
@@ -1403,6 +1657,7 @@
}
],
"affected_locations_total": 1,
+ "api": null,
"evidence_truncated": false,
"intervention_symbol": null,
"judgment_steps": 1,
@@ -1451,10 +1706,11 @@
"boundary_kind": 0,
"entry_reachability": 0,
"execution_context": 3,
+ "loop_magnitude": 1,
"multiplier_shape": 3,
"provenance": 3
},
- "rank_score": 11,
+ "rank_score": 12,
"rationale": "Repeated string accumulation is proven; intermediate accumulator observations still require validation.",
"reliable_entry_reachability": null
},
@@ -1472,6 +1728,7 @@
}
],
"affected_locations_total": 1,
+ "api": null,
"evidence_truncated": false,
"intervention_symbol": null,
"judgment_steps": 1,
diff --git a/tests/fixtures/perf_corpus/manifest.json b/tests/fixtures/perf_corpus/manifest.json
index 5f7687bf6..8b43bc5ea 100644
--- a/tests/fixtures/perf_corpus/manifest.json
+++ b/tests/fixtures/perf_corpus/manifest.json
@@ -24,7 +24,11 @@
"cases": [
"generic_infra_sink",
"same_file_db_helper",
- "async_serial_awaits"
+ "async_serial_awaits",
+ "batch_form_equivalent",
+ "batch_form_limited",
+ "bounded_fan_out",
+ "retry_loop"
],
"note": "Database boundary through a shared session/query helper."
},
diff --git a/tests/fixtures/perf_corpus/observations.json b/tests/fixtures/perf_corpus/observations.json
index b2418fd39..5c9cfab63 100644
--- a/tests/fixtures/perf_corpus/observations.json
+++ b/tests/fixtures/perf_corpus/observations.json
@@ -288,9 +288,7 @@
"biomarker_type": "resource_construction_in_loop",
"boundary_kind": "network",
"case": "resource_construction",
- "extra": {
- "resource_invariant": true
- },
+ "extra": {},
"file_path": "src/app/clients.py",
"finding_ref": "resource_construction-16",
"function_name": "each",
@@ -305,9 +303,7 @@
"biomarker_type": "resource_construction_in_loop",
"boundary_kind": "network",
"case": "resource_construction",
- "extra": {
- "resource_invariant": false
- },
+ "extra": {},
"file_path": "src/app/clients.py",
"finding_ref": "resource_construction-17",
"function_name": "each_varying",
@@ -606,6 +602,78 @@
"reason": "sink_without_caller: io_in_loop at src/app/lone.py:9 repeats work per iteration.",
"reliable_entry_reachability": null,
"resolution_basis": "call-site"
+ },
+ {
+ "biomarker_type": "io_in_loop",
+ "boundary_kind": "db",
+ "case": "batch_form_equivalent",
+ "extra": {
+ "batch_equivalent": true,
+ "batch_form": ".in_(\"repo_id\", keys)",
+ "loop_magnitude": "grows_with_data"
+ },
+ "file_path": "src/app/owners.py",
+ "finding_ref": "batch_form_equivalent-34",
+ "function_name": "load_owners",
+ "line_end": 22,
+ "line_start": 20,
+ "path": [],
+ "reason": "batch_form_equivalent: io_in_loop at src/app/owners.py:20 repeats work per iteration.",
+ "reliable_entry_reachability": null,
+ "resolution_basis": "direct"
+ },
+ {
+ "biomarker_type": "io_in_loop",
+ "boundary_kind": "db",
+ "case": "batch_form_limited",
+ "extra": {
+ "batch_equivalent": false,
+ "batch_form": ".in_(\"repo_id\", keys)"
+ },
+ "file_path": "src/app/commits.py",
+ "finding_ref": "batch_form_limited-35",
+ "function_name": "first_commit_each",
+ "line_end": 32,
+ "line_start": 30,
+ "path": [],
+ "reason": "batch_form_limited: io_in_loop at src/app/commits.py:30 repeats work per iteration.",
+ "reliable_entry_reachability": null,
+ "resolution_basis": "direct"
+ },
+ {
+ "biomarker_type": "serial_await_in_loop",
+ "boundary_kind": "network",
+ "case": "bounded_fan_out",
+ "extra": {
+ "concurrency_bound": "self._sem",
+ "dataflow_verified": true
+ },
+ "file_path": "src/app/fetch.py",
+ "finding_ref": "bounded_fan_out-36",
+ "function_name": "fetch_each",
+ "line_end": 42,
+ "line_start": 40,
+ "path": [],
+ "reason": "bounded_fan_out: serial_await_in_loop at src/app/fetch.py:40 repeats work per iteration.",
+ "reliable_entry_reachability": null,
+ "resolution_basis": "direct"
+ },
+ {
+ "biomarker_type": "io_in_loop",
+ "boundary_kind": "network",
+ "case": "retry_loop",
+ "extra": {
+ "loop_magnitude": "bounded"
+ },
+ "file_path": "src/app/retry.py",
+ "finding_ref": "retry_loop-37",
+ "function_name": "post_with_retry",
+ "line_end": 52,
+ "line_start": 50,
+ "path": [],
+ "reason": "retry_loop: io_in_loop at src/app/retry.py:50 repeats work per iteration.",
+ "reliable_entry_reachability": null,
+ "resolution_basis": "direct"
}
]
}
diff --git a/tests/unit/health/test_model_version_coupling.py b/tests/unit/health/test_model_version_coupling.py
index 1ab4cda19..08f259995 100644
--- a/tests/unit/health/test_model_version_coupling.py
+++ b/tests/unit/health/test_model_version_coupling.py
@@ -22,7 +22,7 @@ from repowise.core.analysis.health.refactoring.identity import (
# The three stamps as they stand together. Moving either identity model means
# moving the analyzer with it, and updating this tuple in the same commit.
-_STAMPS = (22, 2, 2)
+_STAMPS = (23, 2, 2)
def test_the_identity_models_and_the_analyzer_stamp_are_pinned_together() -> None:
diff --git a/tests/unit/health/test_perf_corpus_golden.py b/tests/unit/health/test_perf_corpus_golden.py
index dcc6f3d98..7a0d2f8db 100644
--- a/tests/unit/health/test_perf_corpus_golden.py
+++ b/tests/unit/health/test_perf_corpus_golden.py
@@ -139,7 +139,12 @@ def test_linking_stamps_the_id_the_builder_derives() -> None:
("distinct_lock_owners", 1, {None}),
("membership_scan", 1, {"replace_membership_collection"}),
("string_accumulation", 1, {"buffer_string_accumulation"}),
- ("resource_construction", 2, {"hoist_loop_invariant_resource", None}),
+ # No hoisting proof exists, so neither construction site gets a strategy.
+ ("resource_construction", 2, {None}),
+ ("batch_form_equivalent", 1, {"batch_or_prefetch_io"}),
+ ("batch_form_limited", 1, {"batch_or_prefetch_io"}),
+ ("bounded_fan_out", 1, {"parallelize_independent_awaits"}),
+ ("retry_loop", 1, {"batch_or_prefetch_io"}),
# Execution context is an identity input: one shape, three contexts.
("context_split", 3, {"batch_or_prefetch_io"}),
# io_in_loop and nested_loop_with_io share one cross-function identity
@@ -242,3 +247,29 @@ def test_reachability_aggregates_any_true_over_all_false() -> None:
item = build_performance_opportunities(rows_for("reachability_states"))[0]
assert item.reliable_entry_reachability is True
assert item.observations_total == 2
+
+
+ ("case", "state", "prerequisites", "api"),
+ [
+ # The loop's own key filters the call, so the bulk form reads the same rows.
+ ("batch_form_equivalent", "plan_ready", (), '.in_("repo_id", keys)'),
+ # The bulk form exists, but a per-key limit means it may not read the same rows.
+ ("batch_form_limited", "advisory", ("result_equivalence",), '.in_("repo_id", keys)'),
+ ("bounded_fan_out", "plan_ready", (), "self._sem"),
+ ("retry_loop", "advisory", ("batch_api_contract", "result_equivalence"), None),
+ ],
+)
+def test_promotion_facts_clear_exactly_their_prerequisite(case, state, prerequisites, api) -> None:
+ (item,) = build_performance_opportunities(rows_for(case))
+ assert item.actionability_state == state
+ assert item.prerequisites == prerequisites
+ assert item.fix is not None and item.fix.api == api
+
+
+def test_a_loop_that_grows_with_data_outranks_a_bounded_one() -> None:
+ (grows,) = build_performance_opportunities(rows_for("batch_form_equivalent"))
+ (bounded,) = build_performance_opportunities(rows_for("retry_loop"))
+ assert grows.facets["loop_magnitude"] == "grows_with_data"
+ assert bounded.facets["loop_magnitude"] == "bounded"
+ assert grows.rank_factors["loop_magnitude"] > bounded.rank_factors["loop_magnitude"]
diff --git a/tests/unit/health/test_perf_crossfn.py b/tests/unit/health/test_perf_crossfn.py
index 157d18e3f..847db4259 100644
--- a/tests/unit/health/test_perf_crossfn.py
+++ b/tests/unit/health/test_perf_crossfn.py
@@ -531,3 +531,26 @@ def test_crossfn_finding_scores_performance_not_defect():
assert scores["defect"] == 10.0
assert scores["performance"] < 10.0
assert all(d == 0.0 for d in deductions)
+
+
+def test_crossfn_hit_carries_the_owning_loops_magnitude(tmp_path):
+ """The loop that pays for the helper's sink is in the caller, so its trip
+ count travels with the cross-function hit into the finding."""
+ source = _SCHEDULER.replace(
+ " for repo in repos:",
+ " repos = self.session.execute(select(Repo)).scalars().all()\n"
+ " for repo in repos:",
+ )
+ walked, graph = _build(tmp_path, {"scheduler.py": source})
+ (hit,) = [h for hs in collect_crossfn_io_in_loop(walked, graph).values() for h in hs]
+ assert hit.loop is not None and hit.loop.magnitude == "grows_with_data"
+ ctx = FileContext(
+ file_path="scheduler.py",
+ language="python",
+ nloc=20,
+ has_test_file=False,
+ module="scheduler",
+ perf_hits=[hit],
+ )
+ (finding,) = IoInLoopDetector().detect(ctx)
+ assert finding.details["loop_magnitude"] == "grows_with_data"
diff --git a/tests/unit/health/test_perf_io_in_loop.py b/tests/unit/health/test_perf_io_in_loop.py
index ef2274f71..5045f8cff 100644
--- a/tests/unit/health/test_perf_io_in_loop.py
+++ b/tests/unit/health/test_perf_io_in_loop.py
@@ -295,6 +295,6 @@ def test_a_sink_in_a_chunked_loop_carries_the_fact(header: str, chunked: bool):
fc = walk_file("f.py", "python", source)
loop_hits = [h for h in fc.perf_hits if h.kind in {"io_in_loop", "serial_await_in_loop"}]
assert loop_hits
- assert all(h.chunked is chunked for h in loop_hits)
+ assert all((h.loop is not None and h.loop.chunked) is chunked for h in loop_hits)
finding = IoInLoopDetector().detect(_ctx(loop_hits))[0]
assert finding.details.get("chunked_iteration", False) is chunked
diff --git a/tests/unit/health/test_perf_loop_facts_python.py b/tests/unit/health/test_perf_loop_facts_python.py
new file mode 100644
index 000000000..ec5432203
--- /dev/null
+++ b/tests/unit/health/test_perf_loop_facts_python.py
@@ -0,0 +1,458 @@
+"""Unit tests for the Python promotion-facts hooks (perf/dialects/python.py).
+
+Covers ``loop_magnitude`` / ``batch_form`` / ``concurrency_bound`` through the
+real walker (``walk_file`` -> ``FileComplexity.perf_hits[i].loop``), mirroring
+``test_perf_io_in_loop.py``'s inline-fixture style. Every positive rule is
+paired with the refusal(s) that protect precision (SPEC.md "Dialect hooks").
+"""
+
+from __future__ import annotations
+
+from repowise.core.analysis.health.complexity import walk_file
+from repowise.core.analysis.health.perf.loop_facts import LoopFacts
+
+
+def _hits(src: bytes, kind: str = "io_in_loop") -> list:
+ fc = walk_file("f.py", "python", src)
+ return [h for h in fc.perf_hits if h.kind == kind]
+
+
+def _loop(src: bytes, kind: str = "io_in_loop", index: int = 0) -> LoopFacts | None:
+ hits = _hits(src, kind)
+ assert hits, f"no {kind!r} hit produced"
+ return hits[index].loop
+
+
+# ---------------------------------------------------------------------------
+# loop_magnitude
+# ---------------------------------------------------------------------------
+
+
+def test_magnitude_grows_via_lexical_reaching_assignment():
+ """``rows = await session.execute(...)`` then ``for r in rows`` -> grows (G1)."""
+ facts = _loop(
+ b"from sqlalchemy import select\n"
+ b"async def f(session):\n"
+ b" rows = await session.execute(select(1))\n"
+ b" for r in rows:\n"
+ b" await session.execute(select(r))\n"
+ )
+ assert facts.magnitude == "grows_with_data"
+
+
+def test_magnitude_grows_via_projection_chain():
+ """The iterable itself is a ``.scalars().all()`` projection of a sink call."""
+ facts = _loop(
+ b"from sqlalchemy import select\n"
+ b"async def f(session):\n"
+ b" for r in session.execute(select(1)).scalars().all():\n"
+ b" await session.execute(select(r))\n"
+ )
+ assert facts.magnitude == "grows_with_data"
+
+
+def test_magnitude_range_len_of_grown_source_grows():
+ """``range(len(rows))`` resolved through ``rows`` the same way as G1."""
+ facts = _loop(
+ b"from sqlalchemy import select\n"
+ b"async def f(session):\n"
+ b" rows = await session.execute(select(1))\n"
+ b" for i in range(len(rows)):\n"
+ b" await session.execute(select(rows))\n"
+ )
+ assert facts.magnitude == "grows_with_data"
+
+
+def test_a_directory_listing_is_as_unknown_as_the_path_it_is_given():
+ facts = _loop(
+ b"import os\n"
+ b"from sqlalchemy import select\n"
+ b"async def f(session):\n"
+ b" for root_, dirs, files in os.walk('/tmp'):\n"
+ b" await session.execute(select(root_))\n"
+ )
+ assert facts is None or facts.magnitude == "unknown"
+
+
+def test_a_read_capped_in_the_query_does_not_grow():
+ facts = _loop(
+ b"def f(sb, repo_id, max_snaps):\n"
+ b" rows = sb.table('s').select('*').eq('repo_id', repo_id).limit(max_snaps).execute().data\n"
+ b" for row in rows:\n"
+ b" sb.table('t').delete().eq('id', row['id']).execute()\n"
+ )
+ assert facts.magnitude == "unknown"
+
+
+def test_magnitude_range_all_caps_constant_bounded():
+ """B1: ``range(N)`` where N is an ALL_CAPS identifier."""
+ facts = _loop(
+ b"from sqlalchemy import select\n"
+ b"async def f(session, MAX_RETRIES):\n"
+ b" for i in range(MAX_RETRIES):\n"
+ b" await session.execute(select(i))\n"
+ )
+ assert facts.magnitude == "bounded"
+
+
+def test_magnitude_range_attempts_name_bounded():
+ """B1: the last dotted segment matches ``retr|attempt`` (case-insensitive)."""
+ facts = _loop(
+ b"from sqlalchemy import select\n"
+ b"async def f(session, cfg):\n"
+ b" for i in range(cfg.max_attempts):\n"
+ b" await session.execute(select(i))\n"
+ )
+ assert facts.magnitude == "bounded"
+
+
+def test_magnitude_slice_constant_upper_bound_bounded():
+ """B1: ``xs[:5]``."""
+ facts = _loop(
+ b"from sqlalchemy import select\n"
+ b"async def f(session, xs):\n"
+ b" for x in xs[:5]:\n"
+ b" await session.execute(select(x))\n"
+ )
+ assert facts.magnitude == "bounded"
+
+
+def test_magnitude_slice_named_constant_bounded():
+ """B1: ``xs[:LIMIT]``."""
+ facts = _loop(
+ b"from sqlalchemy import select\n"
+ b"async def f(session, xs, LIMIT):\n"
+ b" for x in xs[:LIMIT]:\n"
+ b" await session.execute(select(x))\n"
+ )
+ assert facts.magnitude == "bounded"
+
+
+def test_magnitude_slice_variable_width_int_literal_bounded():
+ """B1: ``xs[a:a+4]`` — same start name, integer-literal width."""
+ facts = _loop(
+ b"from sqlalchemy import select\n"
+ b"async def f(session, xs, a):\n"
+ b" for x in xs[a:a+4]:\n"
+ b" await session.execute(select(x))\n"
+ )
+ assert facts.magnitude == "bounded"
+
+
+def test_magnitude_parameter_iterable_is_unknown():
+ """Parameters are unknown on purpose — no reaching assignment to resolve."""
+ facts = _loop(
+ b"from sqlalchemy import select\n"
+ b"async def f(session, repos):\n"
+ b" for r in repos:\n"
+ b" await session.execute(select(r))\n"
+ )
+ assert facts is None # unknown magnitude + no batch/bound -> no LoopFacts at all
+
+
+# ---------------------------------------------------------------------------
+# batch_form
+# ---------------------------------------------------------------------------
+
+
+def test_batch_supabase_select_eq_equivalent():
+ facts = _loop(
+ b"async def f(client, repos):\n"
+ b" for r in repos:\n"
+ b" await client.table('t').select('*').eq('repo_id', r.id).execute()\n"
+ )
+ assert facts.batch.call == '.in_("repo_id", keys)'
+ assert facts.batch.equivalent is True
+
+
+def test_batch_supabase_delete_eq_equivalent():
+ facts = _loop(
+ b"async def f(client, repos):\n"
+ b" for r in repos:\n"
+ b" await client.table('t').delete().eq('repo_id', r.id).execute()\n"
+ )
+ assert facts.batch.call == '.in_("repo_id", keys)'
+ assert facts.batch.equivalent is True
+
+
+def test_batch_sqlalchemy_execute_where_equality():
+ facts = _loop(
+ b"from sqlalchemy import select\n"
+ b"async def f(session, ids):\n"
+ b" for i in ids:\n"
+ b" await session.execute(select(Model).where(Model.id == i))\n"
+ )
+ assert facts.batch.call == "Model.id.in_(keys)"
+ assert facts.batch.equivalent is True
+
+
+def test_batch_sqlalchemy_query_filter_first_not_equivalent():
+ """``.first()`` is itself a per-key limiting verb -> form returned, not equivalent."""
+ facts = _loop(
+ b"from sqlalchemy import select\n"
+ b"def f(session, ids):\n"
+ b" for i in ids:\n"
+ b" session.query(Model).filter(Model.id == i).first()\n"
+ )
+ assert facts.batch.call == "Model.id.in_(keys)"
+ assert facts.batch.equivalent is False
+
+
+# -- refusals ----------------------------------------------------------------
+
+
+def test_batch_none_when_loop_var_used_twice_in_chain():
+ """Two ``.eq()`` calls each reference the loop var -> not a single-key chain."""
+ facts = _loop(
+ b"async def f(client, repos):\n"
+ b" for r in repos:\n"
+ b" await client.table('t').select('*')"
+ b".eq('repo_id', r.id).eq('name', r.name).execute()\n"
+ )
+ assert facts is None
+
+
+def test_batch_supabase_limit_makes_not_equivalent():
+ facts = _loop(
+ b"async def f(client, repos):\n"
+ b" for r in repos:\n"
+ b" await client.table('t').select('*')"
+ b".eq('repo_id', r.id).limit(1).execute()\n"
+ )
+ assert facts.batch.equivalent is False
+
+
+def test_batch_supabase_single_makes_not_equivalent():
+ facts = _loop(
+ b"async def f(client, repos):\n"
+ b" for r in repos:\n"
+ b" await client.table('t').select('*')"
+ b".eq('repo_id', r.id).single().execute()\n"
+ )
+ assert facts.batch.equivalent is False
+
+
+def test_batch_second_sink_in_body_makes_not_equivalent():
+ hits = _hits(
+ b"from sqlalchemy import select\n"
+ b"async def f(client, repos, session):\n"
+ b" for r in repos:\n"
+ b" session.execute(select(r))\n"
+ b" await client.table('t').select('*').eq('repo_id', r.id).execute()\n"
+ )
+ supabase_hit = next(h for h in hits if h.loop is not None)
+ assert supabase_hit.loop.batch.equivalent is False
+
+
+def test_batch_delete_with_break_not_equivalent():
+ """An early exit could skip work the batched delete would still do."""
+ facts = _loop(
+ b"async def f(client, repos):\n"
+ b" for r in repos:\n"
+ b" if r.id == 0:\n"
+ b" break\n"
+ b" await client.table('t').delete().eq('repo_id', r.id).execute()\n"
+ )
+ assert facts.batch.call == '.in_("repo_id", keys)'
+ assert facts.batch.equivalent is False
+
+
+def test_batch_none_when_iterable_appended_in_body():
+ facts = _loop(
+ b"async def f(client, repos):\n"
+ b" for r in repos:\n"
+ b" repos.append(r)\n"
+ b" await client.table('t').select('*').eq('repo_id', r.id).execute()\n"
+ )
+ assert facts is None
+
+
+def test_batch_none_for_while_loop():
+ facts = _loop(
+ b"async def f(client, repos):\n"
+ b" r = repos.pop()\n"
+ b" while r is not None:\n"
+ b" await client.table('t').select('*').eq('repo_id', r.id).execute()\n"
+ b" r = repos.pop() if repos else None\n"
+ )
+ assert facts is None
+
+
+# ---------------------------------------------------------------------------
+# concurrency_bound
+# ---------------------------------------------------------------------------
+
+
+def test_concurrency_bound_async_with_self_attribute():
+ facts = _loop(
+ b"from sqlalchemy import select\n"
+ b"async def f(self, items, session):\n"
+ b" for x in items:\n"
+ b" async with self._sem:\n"
+ b" await session.execute(select(x))\n"
+ )
+ assert facts.concurrency_bound == "self._sem"
+
+
+def test_a_semaphore_built_inside_the_loop_bounds_nothing():
+ facts = _loop(
+ b"import asyncio\n"
+ b"from sqlalchemy import select\n"
+ b"async def f(items, session):\n"
+ b" for x in items:\n"
+ b" async with asyncio.Semaphore(5):\n"
+ b" await session.execute(select(x))\n"
+ )
+ assert facts is None or facts.concurrency_bound is None
+
+
+def test_concurrency_bound_none_when_loop_has_break():
+ """Spec amendment: an early-exit loop refuses concurrency_bound — fan-out
+ under the semaphore would run work the sequential loop skips past the exit."""
+ facts = _loop(
+ b"from sqlalchemy import select\n"
+ b"async def f(self, items, session):\n"
+ b" for x in items:\n"
+ b" if x.bad:\n"
+ b" break\n"
+ b" async with self._sem:\n"
+ b" await session.execute(select(x))\n"
+ )
+ assert facts is None
+
+
+def test_concurrency_bound_none_when_loop_has_return():
+ facts = _loop(
+ b"from sqlalchemy import select\n"
+ b"async def f(self, items, session):\n"
+ b" for x in items:\n"
+ b" if x.bad:\n"
+ b" return None\n"
+ b" async with self._sem:\n"
+ b" await session.execute(select(x))\n"
+ )
+ assert facts is None
+
+
+def test_concurrency_bound_none_for_sync_with():
+ """Only an ``async with`` counts — a plain ``with`` never yields to other tasks."""
+ facts = _loop(
+ b"from sqlalchemy import select\n"
+ b"async def f(self, items, session):\n"
+ b" for x in items:\n"
+ b" with self._sem:\n"
+ b" await session.execute(select(x))\n"
+ )
+ assert facts is None
+
+
+def test_batch_none_when_the_key_is_computed_from_the_element():
+ facts = _loop(
+ b"async def f(client, repos):\n"
+ b" for r in repos:\n"
+ b" await client.table('t').select('*').eq('id', slug(r)).execute()\n"
+ )
+ assert facts is None or facts.batch is None
+
+
+def test_a_sync_network_write_in_the_body_breaks_equivalence():
+ facts = _loop(
+ b"import requests\n"
+ b"def f(client, repos):\n"
+ b" for r in repos:\n"
+ b" client.table('t').select('*').eq('id', r.id).execute()\n"
+ b" requests.post('https://x', json={})\n"
+ )
+ assert facts.batch is not None and facts.batch.equivalent is False
+
+
+def test_magnitude_grows_through_a_result_attribute():
+ facts = _loop(
+ b"def f(client):\n"
+ b" res = client.table('repos').select('*').execute()\n"
+ b" for row in res.data:\n"
+ b" client.table('t').select('*').eq('id', row['id']).execute()\n"
+ )
+ assert facts.magnitude == "grows_with_data"
+
+
+def test_a_retry_loop_inside_a_loop_over_rows_is_not_bounded():
+ facts = _loop(
+ b"async def f(session):\n"
+ b" rows = (await session.execute(q)).scalars().all()\n"
+ b" for row in rows:\n"
+ b" for attempt in range(MAX_RETRIES):\n"
+ b" await session.execute(select(row))\n"
+ )
+ assert facts.magnitude == "grows_with_data"
+
+
+def test_a_delete_behind_a_continue_is_not_equivalent():
+ """Collected up front, the keys would include the records the loop skips."""
+ facts = _loop(
+ b"from sqlalchemy import delete\n"
+ b"async def f(session, rows):\n"
+ b" for rec in rows:\n"
+ b" if not rec.files:\n"
+ b" continue\n"
+ b" await session.execute(delete(Link).where(Link.decision_id == rec.id))\n"
+ )
+ assert facts.batch is not None and facts.batch.equivalent is False
+
+
+def test_a_result_cut_to_one_row_after_the_call_is_not_equivalent():
+ facts = _loop(
+ b"from sqlalchemy import select\n"
+ b"async def f(session, targets):\n"
+ b" for t in targets:\n"
+ b" res = await session.execute(select(Meta).where(Meta.path == t))\n"
+ b" meta = res.scalar_one_or_none()\n"
+ )
+ assert facts.batch is not None and facts.batch.equivalent is False
+
+
+def test_no_batch_form_when_the_filter_also_reads_a_loop_local():
+ """The key is (src, kind), not the element: IN on one column is the wrong query."""
+ facts = _loop(
+ b"from sqlalchemy import select\n"
+ b"async def f(session, edges):\n"
+ b" for edge in edges:\n"
+ b" src = remap(edge)\n"
+ b" await session.execute(select(E).where(E.src == src, E.kind == edge.kind))\n"
+ )
+ assert facts is None or facts.batch is None
+
+
+def test_a_parenthesised_await_is_still_the_sinks_own():
+ facts = _loop(
+ b"async def f(supabase, payload, installation_id):\n"
+ b" for repo in payload.get('repositories_removed', []):\n"
+ b" await (\n"
+ b" supabase.table('app_repos').delete()\n"
+ b" .eq('installation_id', installation_id).eq('repo_id', repo['id']).execute()\n"
+ b" )\n"
+ )
+ assert facts.batch is not None
+ assert facts.batch.call == '.in_("repo_id", keys)'
+ assert facts.batch.equivalent is True
+
+
+def test_writing_through_the_element_does_not_make_it_loop_local():
+ facts = _loop(
+ b"from sqlalchemy import select\n"
+ b"async def f(session, targets, out):\n"
+ b" for t in targets:\n"
+ b" res = await session.execute(select(Meta).where(Meta.path == t))\n"
+ b" out[t] = res\n"
+ )
+ assert facts.batch is not None and facts.batch.call == "Meta.path.in_(keys)"
+
+
+def test_magnitude_sees_through_an_empty_fallback():
+ facts = _loop(
+ b"def f(sb):\n"
+ b" rows = (sb.table('repos').select('*').execute()).data or []\n"
+ b" for row in rows:\n"
+ b" sb.table('t').select('*').eq('id', row['id']).execute()\n"
+ )
+ assert facts.magnitude == "grows_with_data"
diff --git a/tests/unit/health/test_perf_loop_facts_ts.py b/tests/unit/health/test_perf_loop_facts_ts.py
new file mode 100644
index 000000000..b817ec90f
--- /dev/null
+++ b/tests/unit/health/test_perf_loop_facts_ts.py
@@ -0,0 +1,258 @@
+"""Unit tests for the TS/JS promotion-fact hooks (``loop_magnitude`` /
+``batch_form`` / ``is_chunked_loop``) on ``TsJsPerfDialect``.
+
+Goes through the real walker (``walk_file``) rather than calling the dialect
+methods directly, so the tests also cover the sink-detection gate each hook
+depends on (a hit only carries ``.loop`` facts when its call is first
+classified as an I/O sink). Precision over recall: every ambiguous case
+asserts ``None`` / ``"unknown"``, not a guess.
+"""
+
+from __future__ import annotations
+
+import pytest
+
+from repowise.core.analysis.health.complexity import walk_file
+
+
+def _hits(src: str, kind: str | None = None):
+ fc = walk_file("f.ts", "typescript", src.encode())
+ hits = fc.perf_hits
+ if kind is not None:
+ hits = [h for h in hits if h.kind == kind]
+ return hits
+
+
+def _io_hits(src: str):
+ return _hits(src, "io_in_loop")
+
+
+# ---------------------------------------------------------------------------
+# batch_form — Prisma
+# ---------------------------------------------------------------------------
+
+
+def test_prisma_find_unique_batches_equivalent():
+ src = """
+async function f(users) {
+ for (const u of users) {
+ await prisma.user.findUnique({ where: { id: u.id } });
+ }
+}
+"""
+ hits = _io_hits(src)
+ loop = hits[0].loop
+ assert loop is not None and loop.batch is not None
+ assert loop.batch.call == "prisma.user.findMany({ where: { id: { in: keys } } })"
+ assert loop.batch.equivalent is True
+
+
+def test_prisma_find_first_not_equivalent():
+ src = """
+async function f(users) {
+ for (const u of users) {
+ await prisma.user.findFirst({ where: { id: u.id } });
+ }
+}
+"""
+ hits = _io_hits(src)
+ loop = hits[0].loop
+ assert loop is not None and loop.batch is not None
+ assert loop.batch.call == "prisma.user.findMany({ where: { id: { in: keys } } })"
+ assert loop.batch.equivalent is False
+
+
+ "body",
+ [
+ # The element is a bystander; the filter value is a constant.
+ "await prisma.user.findUnique({ where: { id: ROOT_ID }, include: { x: u.flag } });",
+ # The element feeds two arguments, so no single key column exists.
+ "await prisma.user.findUnique({ where: { id: u.id }, select: { [u.col]: true } });",
+ ],
+)
+def test_no_batch_form_unless_the_element_is_the_filter_value(body):
+ src = f"async function f(users) {{ for (const u of users) {{ {body} }} }}"
+ assert all(h.loop is None or h.loop.batch is None for h in _io_hits(src))
+
+
+def test_no_batch_form_for_a_cursor_loop_or_a_mutated_iterable():
+ cursor = """
+async function f(users) {
+ for (let i = 0; i < users.length; i++) {
+ await prisma.user.findUnique({ where: { id: users[i].id } });
+ }
+}
+"""
+ worklist = """
+async function f(users) {
+ for (const u of users) {
+ await prisma.user.findUnique({ where: { id: u.id } });
+ users.push(u);
+ }
+}
+"""
+ for src in (cursor, worklist):
+ assert all(h.loop is None or h.loop.batch is None for h in _io_hits(src))
+
+
+def test_prisma_delete_batches_equivalent():
+ # Bare ``delete`` (unlike ``deleteMany``) is not a distinctive-enough verb
+ # to trust without import resolution (it collides with Map/Set/cache
+ # ``.delete()``), so this fixture imports the client the same way
+ # ``collect_io_names`` resolves any other db client — the sink then fires
+ # via the ``awaited`` arm of ``sink_kind`` regardless of the method name.
+ src = """
+import { prisma } from "@prisma/client";
+async function f(users) {
+ for (const u of users) {
+ await prisma.user.delete({ where: { id: u.id } });
+ }
+}
+"""
+ hits = _io_hits(src)
+ loop = hits[0].loop
+ assert loop is not None and loop.batch is not None
+ assert loop.batch.call == "prisma.user.deleteMany({ where: { id: { in: keys } } })"
+ assert loop.batch.equivalent is True
+
+
+def test_prisma_delete_with_break_not_equivalent():
+ src = """
+import { prisma } from "@prisma/client";
+async function f(users) {
+ for (const u of users) {
+ if (!u.id) { break; }
+ await prisma.user.delete({ where: { id: u.id } });
+ }
+}
+"""
+ hits = _io_hits(src)
+ loop = hits[0].loop
+ assert loop is not None and loop.batch is not None
+ assert loop.batch.equivalent is False
+
+
+# ---------------------------------------------------------------------------
+# loop_magnitude
+# ---------------------------------------------------------------------------
+
+
+def test_magnitude_grows_with_prisma_iterable():
+ src = """
+async function f() {
+ const users = await prisma.user.findMany();
+ for (const u of users) {
+ await log(u.id);
+ await prisma.post.deleteMany({ where: { userId: u.id } });
+ }
+}
+"""
+ hits = _io_hits(src)
+ kinds = {h.detail for h in hits}
+ assert "db" in kinds
+ hit = next(h for h in hits if h.detail == "db")
+ assert hit.loop is not None
+ assert hit.loop.magnitude == "grows_with_data"
+
+
+def test_magnitude_grows_with_fetch_json_one_hop():
+ src = """
+async function f() {
+ const rows = (await fetch(url)).json();
+ for (const r of rows) {
+ await prisma.post.deleteMany({ where: { userId: r.id } });
+ }
+}
+"""
+ hits = _io_hits(src)
+ assert hits, "expected an io_in_loop hit"
+ assert hits[0].loop is not None
+ assert hits[0].loop.magnitude == "grows_with_data"
+
+
+def test_a_directory_listing_is_as_unknown_as_the_path_it_is_given():
+ src = """
+function f(dir) {
+ const files = fs.readdirSync(dir);
+ for (const name of files) {
+ prisma.file.deleteMany({ where: { name: name } });
+ }
+}
+"""
+ hits = _io_hits(src)
+ assert hits[0].loop is None or hits[0].loop.magnitude == "unknown"
+
+
+def test_magnitude_bounded_via_slice():
+ src = """
+async function f(items, LIMIT) {
+ for (const x of items.slice(0, 5)) {
+ await prisma.post.deleteMany({ where: { userId: x.id } });
+ }
+}
+"""
+ hits = _io_hits(src)
+ assert hits[0].loop is not None
+ assert hits[0].loop.magnitude == "bounded"
+
+
+def test_magnitude_unknown_for_parameter():
+ src = """
+async function f(items) {
+ for (const x of items) {
+ await prisma.post.deleteMany({ where: { userId: x.id } });
+ }
+}
+"""
+ hits = _io_hits(src)
+ loop = hits[0].loop
+ # ``items`` is a bare parameter — no grows/bounded proof, so either no
+ # loop facts at all or an explicit "unknown" magnitude.
+ assert loop is None or loop.magnitude == "unknown"
+
+
+# ---------------------------------------------------------------------------
+# is_chunked_loop
+# ---------------------------------------------------------------------------
+
+
+def test_chunked_c_style_step():
+ src = """
+function f(ids, n) {
+ for (let i = 0; i < n; i += 50) {
+ prisma.user.deleteMany({ where: { id: { in: ids.slice(i, i + 50) } } });
+ }
+}
+"""
+ hits = _io_hits(src)
+ assert hits
+ assert hits[0].loop is not None
+ assert hits[0].loop.chunked is True
+
+
+def test_chunked_lodash_chunk_helper():
+ src = """
+async function f(ids) {
+ for (const batch of _.chunk(ids, 50)) {
+ await prisma.user.deleteMany({ where: { id: { in: batch } } });
+ }
+}
+"""
+ hits = _io_hits(src)
+ assert hits
+ assert hits[0].loop is not None
+ assert hits[0].loop.chunked is True
+
+
+def test_not_chunked_step_one():
+ src = """
+function f(ids, n) {
+ for (let i = 0; i < n; i += 1) {
+ prisma.user.deleteMany({ where: { id: ids[i] } });
+ }
+}
+"""
+ hits = _io_hits(src)
+ assert hits
+ assert hits[0].loop is None or hits[0].loop.chunked is False
diff --git a/tests/unit/server/test_performance_opportunities.py b/tests/unit/server/test_performance_opportunities.py
index 5fd11617b..d420ef09b 100644
--- a/tests/unit/server/test_performance_opportunities.py
+++ b/tests/unit/server/test_performance_opportunities.py
@@ -275,6 +275,7 @@ async def test_detail_carries_the_facets_and_evidence_for_one_cause(
"amplification",
"leverage",
"change_risk",
+ "loop_magnitude",
}
assert body["evidence_total"] == 2
assert body["evidence_emitted"] == 1
@@ -370,3 +371,28 @@ async def test_a_file_with_no_cause_scopes_to_an_empty_queue(app, client):
page = await _page(client, repo_id, context="all", file_paths="src/nothing-here.py")
assert page["total"] == 0
assert page["items"] == []
+
+
+async def test_the_bulk_call_a_plan_names_is_served(app, client: AsyncClient) -> None:
+ finding = HealthFindingData(
+ biomarker_type="io_in_loop",
+ severity=Severity.MEDIUM,
+ file_path="src/owners.py",
+ function_name="load",
+ line_start=12,
+ line_end=12,
+ details={
+ "boundary_kind": "db",
+ "batch_form": '.in_("repo_id", keys)',
+ "batch_equivalent": True,
+ },
+ health_impact=0.0,
+ reason="Database work repeats for every loop iteration.",
+ dimension="performance",
+ )
+ repo_id, opportunity_id = await _seed(app, client, [finding])
+ body = (
+ await client.get(f"/api/repos/{repo_id}/health/performance-opportunities/{opportunity_id}")
+ ).json()
+ assert body["actionability_state"] == "plan_ready"
+ assert body["fix"]["api"] == '.in_("repo_id", keys)'