Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(health): one perf plan step per call site, naming the call it batches (#3250)
Raghav Chamadiya323fc636645d
diff --git a/docs/architecture/code-health.md b/docs/architecture/code-health.mdindex 4f1fb9d6e..ab0a43021 100644--- a/docs/architecture/code-health.md+++ b/docs/architecture/code-health.md@@ -716,10 +716,13 @@ by `rank_score`; actionability only breaks ties. Excluded reasons are counted inagainst a DB or network client is advisory with a `bounded_concurrency`prerequisite. A loop already walking chunks is `loop_already_chunked`. When`io_in_loop` and `serial_await_in_loop` fire on one call they name each other in-`siblings`. Plans list their edits (`mechanical` only when proven) and validation-tests (coverage, call graph, import graph, then `via: "name-match"`). An-opportunity links to a `performance_fix` refactoring plan only by exact-`opportunity_id`.+`siblings`. Plans list their edits (`mechanical` only when proven): one step per+call site and loop, however many markers saw it, each naming the call it repeats+(`details.sink_call`, or the helper a cross-function path enters). A step that adds+a bulk form to the sink comes first only when every site reaches that one sink.+Plans also list validation tests (coverage, call graph, import graph, then+`via: "name-match"`). An opportunity links to a `performance_fix` refactoring+plan only by exact `opportunity_id`.**Dataflow promotion.** `serial_await_in_loop` and `nested_loop_quadratic` arepromoted to `dataflow_verified: true` when a def/use pass proves iterationsdiff --git a/packages/core/src/repowise/core/analysis/health/complexity/models.py b/packages/core/src/repowise/core/analysis/health/complexity/models.pyindex f661bea75..6eacb2005 100644--- a/packages/core/src/repowise/core/analysis/health/complexity/models.py+++ b/packages/core/src/repowise/core/analysis/health/complexity/models.py@@ -316,12 +316,20 @@ class PerfHit:# Distinct direct callers of the enclosing function, set only on the# centrality-gated ``hot_path_sync_io`` hit; 0 everywhere else.callers: int = 0+ # The per-iteration call as written at ``line`` (``session.get``), so a plan+ # can name what to batch; empty where the detector kept no call text.+ sink_call: str = ""def loop_facts(self) -> dict[str, Any]:- """Loop facts for ``details``; absent when unset so old findings are unchanged."""+ """Loop facts and the call text for ``details``.++ Each key is absent when unset, so old findings are unchanged.+ """facts = self.loop.as_details() if self.loop is not None else {}if self.loop_line:facts["loop_line"] = self.loop_line+ if self.sink_call:+ facts["sink_call"] = self.sink_callreturn factsdiff --git a/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.py b/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.pyindex 78246336a..126ae0ef6 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@@ -327,6 +327,48 @@ def _loop_key_feeds(call: Node, loop: Node) -> bool:return bool(_loop_bound_names(loop) & _identifiers(call))+_SINK_CALL_MAX = 60+++_STRING_QUOTES = frozenset("'\"`")+++def _sink_call_text(call: Node, method: str, dialect: BasePerfDialect) -> str:+ """The callee as written with its arguments dropped, else its method name.++ ``session.get`` stays as is and ``conn.execute(sql).fetchone`` reads+ ``conn.execute().fetchone``. A callee that opens with a parenthesis, runs+ long or holds a string literal (its quotes defeat the paren scan) gives the+ method only, as does a dialect with no callee text.+ """+ text = dialect.callee_text(call)+ if len(text) > 4 * _SINK_CALL_MAX or not _STRING_QUOTES.isdisjoint(text):+ return method+ callee = _strip_call_args(text)+ return callee if _quotable(callee) else method+++def _strip_call_args(text: str) -> str:+ """``conn.execute(sql).fetchone`` -> ``conn.execute().fetchone``, whitespace dropped."""+ if "(" not in text:+ return "".join(text.split())+ kept: list[str] = []+ depth = 0+ for char in text:+ if char == "(":+ kept.append("()" if depth == 0 else "")+ depth += 1+ elif char == ")":+ depth = max(depth - 1, 0)+ elif depth == 0 and not char.isspace():+ kept.append(char)+ return "".join(kept)+++def _quotable(callee: str) -> bool:+ return bool(callee) and not callee.startswith("(") and len(callee) <= _SINK_CALL_MAX++def _key_unused(hit: PerfHit) -> bool:return hit.loop is not None and hit.loop.key_unused@@ -634,8 +676,12 @@ def _collect_perf_hits(if kind is not None:if loop_depth >= 1:facts = loop_facts(call_node, sink=True, per_call=True)+ call = _sink_call_text(call_node, method, dialect)hits.append(- PerfHit("io_in_loop", line, next_func, kind, func_start=next_start, loop=facts)+ PerfHit(+ "io_in_loop", line, next_func, kind,+ func_start=next_start, loop=facts, sink_call=call,+ ))if do_serial_await and awaited:# An *awaited* sink in a loop body is additionally a@@ -646,7 +692,7 @@ def _collect_perf_hits(hits.append(PerfHit("serial_await_in_loop", line, next_func, kind,- func_start=next_start, loop=facts,+ func_start=next_start, loop=facts, sink_call=call,))if do_nested_io and loop_depth >= 2 and outer_iter:@@ -660,7 +706,7 @@ def _collect_perf_hits(hits.append(PerfHit("nested_loop_with_io", line, next_func, kind,- func_start=next_start, loop=facts,+ func_start=next_start, loop=facts, sink_call=call,))else: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.pyindex a8f1a4967..c750bf6e7 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@@ -155,6 +155,11 @@ class BasePerfDialect:txt = (node.text or b"").decode("utf-8", "replace")return txt.split(".")[0] if txt else None+ def callee_text(self, call_node: Node) -> str:+ """The callee as written, arguments included (``session.get``); empty if unknown."""+ fn = call_node.child_by_field_name("function")+ return (fn.text or b"").decode("utf-8", "replace") if fn is not None else ""+def callee_method_name(self, call_node: Node) -> str | None:"""Rightmost member of the callee (``x.execute`` -> 'execute')."""fn = call_node.child_by_field_name("function")diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/java.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/java.pyindex 65d814b13..d4d58c691 100644--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/java.py+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/java.py@@ -260,6 +260,15 @@ class JavaPerfDialect(BasePerfDialect):return name.text.decode("utf-8", "replace")return None+ def callee_text(self, call_node: Node) -> str:+ # ``method_invocation`` splits the callee into ``object`` and ``name``.+ name = call_node.child_by_field_name("name")+ if call_node.type != "method_invocation" or name is None or not name.text:+ return ""+ obj = call_node.child_by_field_name("object")+ method = name.text.decode("utf-8", "replace")+ return f"{obj.text.decode('utf-8', 'replace')}.{method}" if obj and obj.text else method+def callee_root_name(self, call_node: Node) -> str | None:if call_node.type == "object_creation_expression":t = call_node.child_by_field_name("type")diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/kotlin.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/kotlin.pyindex 2794df692..f1d2c0984 100644--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/kotlin.py+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/kotlin.py@@ -239,6 +239,9 @@ class KotlinPerfDialect(BasePerfDialect):return _decode(fn)return None+ def callee_text(self, call_node: Node) -> str:+ return _decode(_callee(call_node)) or ""+def callee_root_name(self, call_node: Node) -> str | None:node = _callee(call_node)# Walk to the bottom of a ``a.b().c()`` chain — the leftmost receiver isdiff --git a/packages/core/src/repowise/core/analysis/health/perf/facts.py b/packages/core/src/repowise/core/analysis/health/perf/facts.pyindex 83c998b93..f4f3e9309 100644--- a/packages/core/src/repowise/core/analysis/health/perf/facts.py+++ b/packages/core/src/repowise/core/analysis/health/perf/facts.py@@ -150,6 +150,7 @@ def evidence_row(facts: ObservationFacts) -> dict[str, Any]:# The loop around the call, where a reader starts; absent when the# walk stored none, so older evidence reads unchanged.**({"loop_line": loop} if (loop := facts.details.get("loop_line")) else {}),+ **({"sink_call": call} if (call := facts.details.get("sink_call")) else {}),}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.pyindex cfe074933..f7b96068e 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@@ -4,6 +4,7 @@ from __future__ import annotationsfrom collections.abc import Iterablefrom dataclasses import dataclass+from typing import Anyfrom repowise.core.analysis.execution_graph import file_of_symbol@@ -14,15 +15,16 @@ from .models import RefactoringSuggestionfrom .preconditions import Applicability# Per strategy: an optional step at the intervention, then one step per call-# site. Templates name the edit; the locations are the evidence's own.+# site. Templates name the edit; the locations are the evidence's own, and+# ``{call}`` names the call each site repeats._STEPS: dict[str, tuple[str | None, str]] = {"batch_or_prefetch_io": ("Add a form of {sink} that takes every key at once",- "Collect the keys before the loop and call the batched form once",+ "Collect the keys before the loop and replace the per-key {call} with one batched call",),"parallelize_independent_awaits": (None,- "Run the independent awaits concurrently, with a bound on how many at once",+ "Run the independent {call}s concurrently, with a bound on how many at once",),"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"),@@ -37,11 +39,11 @@ _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 call",+ "per-key {call}",),"parallelize_independent_awaits": (None,- "Run the independent awaits concurrently, each still inside {api}",+ "Run the independent {call}s concurrently, each still inside {api}",),}@@ -67,13 +69,18 @@ def fix_steps(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."""+ """Ordered edits for one plan; mechanical only when the strategy is proven.++ *sink* is the sink every site shares, or ``None``: a bulk form added to one+ member's sink would leave the other sites' calls per-key.+ """intro, per_site = (_API_STEPS if api else _STEPS).get(strategy, (None, ""))- per_site = per_site.format(api=api) if api else per_siteapplicability: Applicability = "mechanical" if safety == "proven" else "judgment"steps: list[dict[str, object]] = []- if intro:- target = intervention or sink+ target = intervention or sink+ # With nothing to name, a site step says the whole edit; a strategy with no+ # site step keeps its intro whatever it can name.+ if intro and (target or not per_site):steps.append({"action": intro.format(sink=(target or "the per-key call").rsplit("::", 1)[-1]),@@ -86,7 +93,7 @@ def fix_steps(if per_site:steps.extend({- "action": per_site,+ "action": per_site.format(api=api, call=_call_phrase(location.get("call"))),"symbol": location.get("function_name"),"file_path": location.get("file_path"),"line": location.get("line_start"),@@ -98,6 +105,10 @@ def fix_steps(return [{"order": index, **step} for index, step in enumerate(steps, 1)]+def _call_phrase(call: object) -> str:+ return f"{call} call" if call else "call"++_EFFORT_BANDS = ((1, "S"), (3, "M"), (8, "L"))@@ -139,6 +150,54 @@ def _intervention(opportunity: PerformanceOpportunity, fix: PerformanceFix) -> sreturn opportunity.intervention_symbol+def _site_locations(evidence: Iterable[dict[str, Any]]) -> list[dict[str, Any]]:+ """One location per call site and loop, whatever number of markers observed it.++ ``io_in_loop`` and ``nested_loop_with_io`` both fire on one call; that is one+ edit, not two.+ """+ sites: dict[tuple[Any, Any, Any], dict[str, Any]] = {}+ for item in evidence:+ key = (item["file_path"], item.get("line_start"), item.get("loop_line"))+ site = sites.setdefault(+ key,+ {+ "file_path": item["file_path"],+ "function_name": item.get("function_name"),+ "line_start": item.get("line_start"),+ "line_end": item.get("line_end"),+ **({"loop_line": item["loop_line"]} if item.get("loop_line") else {}),+ },+ )+ if "call" not in site and (call := _site_call(item)):+ site["call"] = call+ return list(sites.values())+++def _site_call(item: dict[str, Any]) -> str | None:+ """The call the site repeats: the detector's text, else the helper hop.++ A cross-function path starts at the loop's own function (``path[0]``), so+ ``path[1]`` is the helper the loop calls at this site.+ """+ path = item.get("path") or ()+ hop = path[1] if len(path) > 1 and isinstance(path[1], str) else None+ return item.get("sink_call") or (hop.rsplit("::", 1)[-1] if hop else None)+++def _shared_sink(opportunity: PerformanceOpportunity) -> str | None:+ """The sink every site reaches, or ``None`` when a site has none or they differ.++ Evidence is capped; past the cap only the members' resolved sinks are known,+ so a pathless member beyond it goes unseen (ceiling: carry a has-path count).+ """+ sinks = {item["path"][-1] if item.get("path") else None for item in opportunity.evidence}+ sink = sinks.pop() if len(sinks) == 1 else None+ if opportunity.evidence_truncated and sink != opportunity.terminal_sink:+ return None+ return sink++def _suggestion(opportunity: PerformanceOpportunity, fix: PerformanceFix, confidence: str) -> RefactoringSuggestion:@@ -146,16 +205,7 @@ def _suggestion(intervention = _intervention(opportunity, fix)target_file = file_of_symbol(intervention) if intervention else anchor["file_path"]in_anchor_file = target_file == anchor["file_path"]- locations = [- {- "file_path": item["file_path"],- "function_name": item.get("function_name"),- "line_start": item.get("line_start"),- "line_end": item.get("line_end"),- **({"loop_line": item["loop_line"]} if item.get("loop_line") else {}),- }- for item in opportunity.evidence- ]+ locations = _site_locations(opportunity.evidence)# A bulk form belongs on the per-key callee: the shared helper, else the one# sink the loop reaches. The loop's own function is where the keys are# collected, never what gains the bulk form.@@ -164,7 +214,7 @@ def _suggestion(fix.strategy,fix.safety,intervention if fix.strategy != "batch_or_prefetch_io" or batched_on_helper else None,- opportunity.terminal_sink,+ _shared_sink(opportunity),locations,fix.api,)diff --git a/tests/fixtures/perf_corpus/golden_plans.json b/tests/fixtures/perf_corpus/golden_plans.jsonindex fa844560b..5c358f4b5 100644--- a/tests/fixtures/perf_corpus/golden_plans.json+++ b/tests/fixtures/perf_corpus/golden_plans.json@@ -52,6 +52,7 @@"line_start": 42},{+ "call": "save","file_path": "src/app/jobs.py","function_name": "drain","line_end": 44,@@ -62,7 +63,7 @@"api": null,"evidence_truncated": false,"intervention_symbol": "src/app/jobs.py::drain",- "judgment_steps": 4,+ "judgment_steps": 3,"mechanical_steps": 0,"opportunity_id": "perf3_d41b04897cfde2a9eed6","paths": [@@ -75,35 +76,27 @@"safety": "advisory","steps": [{- "action": "Add a form of save that takes every key at once",- "applicability": "judgment",- "file_path": "src/app/db.py",- "line": null,- "order": 1,- "symbol": "src/app/db.py::save"- },- {- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key call with one batched call","applicability": "judgment","file_path": "src/app/jobs.py","line": 40,- "order": 2,+ "order": 1,"symbol": "drain"},{- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key call with one batched call","applicability": "judgment","file_path": "src/app/jobs.py","line": 42,- "order": 3,+ "order": 2,"symbol": "drain"},{- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key save call with one batched call","applicability": "judgment","file_path": "src/app/jobs.py","line": 44,- "order": 4,+ "order": 3,"symbol": "drain"}],@@ -152,18 +145,21 @@"plan": {"affected_locations": [{+ "call": "_move","file_path": "src/app/migrate.py","function_name": "apply","line_end": 587,"line_start": 587},{+ "call": "_fold_one","file_path": "src/app/migrate.py","function_name": "apply","line_end": 590,"line_start": 590},{+ "call": "_write_alias","file_path": "src/app/migrate.py","function_name": "apply","line_end": 591,@@ -174,7 +170,7 @@"api": null,"evidence_truncated": false,"intervention_symbol": "src/app/migrate.py::apply",- "judgment_steps": 4,+ "judgment_steps": 3,"mechanical_steps": 0,"opportunity_id": "perf3_b03c60fe41fa017130c6","paths": [@@ -195,35 +191,27 @@"safety": "advisory","steps": [{- "action": "Add a form of the per-key call 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",+ "action": "Collect the keys before the loop and replace the per-key _move call with one batched call","applicability": "judgment","file_path": "src/app/migrate.py","line": 587,- "order": 2,+ "order": 1,"symbol": "apply"},{- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key _fold_one call with one batched call","applicability": "judgment","file_path": "src/app/migrate.py","line": 590,- "order": 3,+ "order": 2,"symbol": "apply"},{- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key _write_alias call with one batched call","applicability": "judgment","file_path": "src/app/migrate.py","line": 591,- "order": 4,+ "order": 3,"symbol": "apply"}],@@ -272,12 +260,14 @@"plan": {"affected_locations": [{+ "call": "_hit","file_path": "src/app/entry.py","function_name": "a","line_end": 252,"line_start": 250},{+ "call": "_hit","file_path": "src/app/entry.py","function_name": "b","line_end": 257,@@ -315,7 +305,7 @@"symbol": "src/app/entry.py::_hit"},{- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key _hit call with one batched call","applicability": "judgment","file_path": "src/app/entry.py","line": 250,@@ -323,7 +313,7 @@"symbol": "a"},{- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key _hit call with one batched call","applicability": "judgment","file_path": "src/app/entry.py","line": 255,@@ -376,12 +366,14 @@"plan": {"affected_locations": [{+ "call": "fetch_page","file_path": "src/app/api.py","function_name": "list_users","line_end": 32,"line_start": 30},{+ "call": "fetch_page","file_path": "src/app/api.py","function_name": "list_orders","line_end": 33,@@ -419,7 +411,7 @@"symbol": "src/app/repo.py::fetch_page"},{- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key fetch_page call with one batched call","applicability": "judgment","file_path": "src/app/api.py","line": 30,@@ -427,7 +419,7 @@"symbol": "list_users"},{- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key fetch_page call with one batched call","applicability": "judgment","file_path": "src/app/api.py","line": 31,@@ -481,12 +473,14 @@"plan": {"affected_locations": [{+ "call": "_write","file_path": "src/app/reindex.py","function_name": "one","line_end": 212,"line_start": 210},{+ "call": "_write","file_path": "src/app/reindex.py","function_name": "many","line_end": 217,@@ -524,7 +518,7 @@"symbol": "src/app/reindex.py::_write"},{- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key _write call with one batched call","applicability": "judgment","file_path": "src/app/reindex.py","line": 210,@@ -532,7 +526,7 @@"symbol": "one"},{- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key _write call with one batched call","applicability": "judgment","file_path": "src/app/reindex.py","line": 215,@@ -656,12 +650,14 @@"plan": {"affected_locations": [{+ "call": "_load","file_path": "src/app/sync.py","function_name": "sync_one","line_end": 72,"line_start": 70},{+ "call": "_load","file_path": "src/app/sync.py","function_name": "sync_all","line_end": 73,@@ -699,7 +695,7 @@"symbol": "src/app/sync.py::_load"},{- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key _load call with one batched call","applicability": "judgment","file_path": "src/app/sync.py","line": 70,@@ -707,7 +703,7 @@"symbol": "sync_one"},{- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key _load call with one batched call","applicability": "judgment","file_path": "src/app/sync.py","line": 71,@@ -760,6 +756,7 @@"plan": {"affected_locations": [{+ "call": "write","file_path": "src/app/cache.py","function_name": "refresh","line_end": 112,@@ -854,7 +851,7 @@"safety": "proven","steps": [{- "action": "Run the independent awaits concurrently, each still inside self._sem",+ "action": "Run the independent calls concurrently, each still inside self._sem","applicability": "mechanical","file_path": "src/app/fetch.py","line": 40,@@ -996,7 +993,7 @@"safety": "advisory","steps": [{- "action": "Run the independent awaits concurrently, with a bound on how many at once",+ "action": "Run the independent calls concurrently, with a bound on how many at once","applicability": "judgment","file_path": "src/app/fanout.py","line": 90,@@ -1049,6 +1046,7 @@"plan": {"affected_locations": [{+ "call": "get_session","file_path": "src/app/job.py","function_name": "run","line_end": 202,@@ -1080,7 +1078,7 @@"symbol": "src/app/db.py::get_session"},{- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key get_session call with one batched call","applicability": "judgment","file_path": "src/app/job.py","line": 200,@@ -1163,7 +1161,7 @@"symbol": "src/app/db.py::only_sink"},{- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key call with one batched call","applicability": "judgment","file_path": "src/app/lone.py","line": 9,@@ -1340,6 +1338,7 @@"plan": {"affected_locations": [{+ "call": "get_session","file_path": "src/app/orders.py","function_name": "handle","line_end": 12,@@ -1371,7 +1370,7 @@"symbol": "src/app/db.py::get_session"},{- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key get_session call with one batched call","applicability": "judgment","file_path": "src/app/orders.py","line": 10,@@ -1424,6 +1423,7 @@"plan": {"affected_locations": [{+ "call": "get_session","file_path": "src/app/reports.py","function_name": "handle","line_end": 14,@@ -1455,7 +1455,7 @@"symbol": "src/app/db.py::get_session"},{- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key get_session call with one batched call","applicability": "judgment","file_path": "src/app/reports.py","line": 12,@@ -1508,6 +1508,7 @@"plan": {"affected_locations": [{+ "call": "get_session","file_path": "src/app/billing.py","function_name": "handle","line_end": 13,@@ -1539,7 +1540,7 @@"symbol": "src/app/db.py::get_session"},{- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key get_session call with one batched call","applicability": "judgment","file_path": "src/app/billing.py","line": 11,@@ -1602,7 +1603,7 @@"api": null,"evidence_truncated": false,"intervention_symbol": "src/app/retry.py::post_with_retry",- "judgment_steps": 2,+ "judgment_steps": 1,"mechanical_steps": 0,"opportunity_id": "perf3_bd36a05060a6d02a1245","paths": [],@@ -1610,19 +1611,11 @@"safety": "advisory","steps": [{- "action": "Add a form of the per-key call 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",+ "action": "Collect the keys before the loop and replace the per-key call with one batched call","applicability": "judgment","file_path": "src/app/retry.py","line": 50,- "order": 2,+ "order": 1,"symbol": "post_with_retry"}],@@ -1671,6 +1664,7 @@"plan": {"affected_locations": [{+ "call": "get_session","file_path": "scripts/backfill.py","function_name": "run","line_end": 202,@@ -1702,7 +1696,7 @@"symbol": "src/app/db.py::get_session"},{- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key get_session call with one batched call","applicability": "judgment","file_path": "scripts/backfill.py","line": 200,@@ -1755,6 +1749,7 @@"plan": {"affected_locations": [{+ "call": "reliable_sink","file_path": "src/app/edge.py","function_name": "run","line_end": 232,@@ -1786,7 +1781,7 @@"symbol": "src/app/db.py::reliable_sink"},{- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key reliable_sink call with one batched call","applicability": "judgment","file_path": "src/app/edge.py","line": 230,@@ -1839,6 +1834,7 @@"plan": {"affected_locations": [{+ "call": "guessed_sink","file_path": "src/app/guess.py","function_name": "run","line_end": 242,@@ -1870,7 +1866,7 @@"symbol": "src/app/db.py::guessed_sink"},{- "action": "Collect the keys before the loop and call the batched form once",+ "action": "Collect the keys before the loop and replace the per-key guessed_sink call with one batched call","applicability": "judgment","file_path": "src/app/guess.py","line": 240,diff --git a/tests/unit/health/test_perf_plan_steps.py b/tests/unit/health/test_perf_plan_steps.pynew file mode 100644index 000000000..61eb093a5--- /dev/null+++ b/tests/unit/health/test_perf_plan_steps.py@@ -0,0 +1,140 @@+"""Performance plan steps: one per call site, each naming the call it batches.++Two markers observing one call used to become two identical steps, and the+intro step added a bulk form to whichever member's sink happened to resolve,+even when the loop's own N+1 was a different call.+"""++from __future__ import annotations++from repowise.core.analysis.health.complexity import walk_file+from repowise.core.analysis.health.perf.opportunities import build_performance_opportunities+from repowise.core.analysis.health.refactoring.performance_fix import (+ performance_fix_suggestions,+)+from tests.unit.health.test_perf_opportunities import _finding++_EVOLUTION = "core/decisions/evolution.py"+++def _steps(rows) -> list[dict]:+ opportunities = build_performance_opportunities(rows)+ assert len(opportunities) == 1+ return performance_fix_suggestions(opportunities)[0].plan["steps"]+++def _hits(source: str):+ return walk_file("t.py", "python", source.encode()).perf_hits+++def test_the_walker_stores_the_call_each_iteration_makes() -> None:+ src = (+ "from sqlalchemy import select\n"+ "async def sync(session, ids, groups):\n"+ " for group in groups:\n"+ " for key in ids:\n"+ " await session.get(Record, key)\n"+ " conn.execute(select(key)).fetchone()\n"+ )+ calls = {(h.kind, h.line): h.loop_facts().get("sink_call") for h in _hits(src)}+ assert calls[("io_in_loop", 5)] == "session.get"+ assert calls[("nested_loop_with_io", 5)] == "session.get"+ assert calls[("io_in_loop", 6)] == "conn.execute().fetchone"+++def test_one_site_seen_twice_and_a_helper_sink_make_one_step_per_site() -> None:+ """The evolution.py shape: two markers on ``session.get`` at one line, and a+ helper call lower in the same loop whose path ends in ``upsert_decision_edge``."""+ rows = [+ _finding(_EVOLUTION, 471, loop_line=467, sink_call="session.get"),+ _finding(+ _EVOLUTION, 471, marker="nested_loop_with_io", loop_line=467, sink_call="session.get"+ ),+ _finding(+ _EVOLUTION,+ 522,+ loop_line=467,+ call_path=(f"{_EVOLUTION}::run", "core/graph.py::upsert_decision_edge"),+ ),+ ]+ steps = _steps(rows)++ assert [(s["line"], s["loop_line"]) for s in steps] == [(471, 467), (522, 467)]+ assert not any("upsert_decision_edge" in str(s["symbol"]) for s in steps)+ assert "session.get" in steps[0]["action"]+ assert "upsert_decision_edge" in steps[1]["action"]+ assert all(s["symbol"] for s in steps)+++def test_sites_that_reach_one_sink_keep_the_bulk_form_step_and_every_site() -> None:+ sink = ("shared.py::load", "git.py::head")+ rows = [+ _finding("a.py", 10, loop_line=8, call_path=("a.py::run", *sink)),+ _finding("b.py", 20, loop_line=18, call_path=("b.py::run", *sink)),+ _finding("b.py", 25, loop_line=18, call_path=("b.py::run", *sink)),+ ]+ steps = _steps(rows)++ assert steps[0]["symbol"] == "shared.py::load"+ assert steps[0]["line"] is None+ assert [(s["file_path"], s["line"]) for s in steps[1:]] == [+ ("a.py", 10),+ ("b.py", 20),+ ("b.py", 25),+ ]+ assert all("load call" in s["action"] for s in steps[1:])+++def test_direct_calls_with_no_sink_get_no_unnamed_bulk_form_step() -> None:+ rows = [+ _finding("jobs.py", 12, loop_line=10, sink_call="session.execute"),+ _finding("jobs.py", 15, loop_line=10, sink_call="session.commit"),+ ]+ steps = _steps(rows)++ assert [s["line"] for s in steps] == [12, 15]+ assert all(s["symbol"] == "run" for s in steps)+ assert "session.execute" in steps[0]["action"]+ assert "session.commit" in steps[1]["action"]+++def test_a_sink_past_the_evidence_cap_still_withholds_the_bulk_form_step() -> None:+ rows = [+ _finding("a.py", line, loop_line=5, call_path=("a.py::run", "db.py::query"))+ for line in range(10, 90, 10)+ ]+ rows.append(_finding("a.py", 95, loop_line=5, call_path=("a.py::run", "db.py::other")))+ opportunities = build_performance_opportunities(rows)+ assert len(opportunities) == 1 and opportunities[0].evidence_truncated+ steps = performance_fix_suggestions(opportunities)[0].plan["steps"]++ assert all(s["line"] for s in steps)+++def test_a_callee_holding_a_string_literal_gives_its_method() -> None:+ src = (+ "from sqlalchemy import select\n"+ "def sync(session, ids):\n"+ " for key in ids:\n"+ " getattr(session, 'get(')(key).execute(select(key))\n"+ )+ calls = {h.loop_facts().get("sink_call") for h in _hits(src) if h.kind == "io_in_loop"}+ assert calls == {"execute"}+++def test_java_and_kotlin_name_the_receiver() -> None:+ java = (+ "class A { void f(java.sql.Connection conn, java.util.List<String> ids) throws Exception {\n"+ " for (String id : ids) { conn.prepareStatement(id).executeQuery(); }\n"+ "} }\n"+ )+ kotlin = (+ "import java.sql.Connection\n"+ "fun f(conn: Connection, ids: List<String>) {\n"+ " for (id in ids) { conn.prepareStatement(id).executeQuery() }\n"+ "}\n"+ )+ for path, language, src in (("A.java", "java", java), ("A.kt", "kotlin", kotlin)):+ hits = walk_file(path, language, src.encode()).perf_hits+ calls = {h.loop_facts().get("sink_call") for h in hits if h.kind == "io_in_loop"}+ assert calls == {"conn.prepareStatement().executeQuery"}, language