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
feat(perf): plans that claim only what they can back, and an over-fetch detector (#2495)
Raghav Chamadiya875ce948e54e
diff --git a/docs/agent/MCP_TOOLS.md b/docs/agent/MCP_TOOLS.mdindex d77d5d56b..00cc484ae 100644--- a/docs/agent/MCP_TOOLS.md+++ b/docs/agent/MCP_TOOLS.md@@ -1192,7 +1192,10 @@ get_health(only=["kpis"], limit=0) # headline numbers, no rowA bare `get_health()` carries `performance_directive`: one bounded lead withits status (`plan_ready` / `advisory` / `investigate` / `clear` / `unavailable`),up to three `why_ranked` facets, the exact plan state, and a structured-`next_action`. Performance findings carry `health_impact: 0` by construction, so+`next_action`, and, when a plan is stored, its `validation` basis. An opportunity+by id adds `plan_steps`, `validation` (tests and commands) and `siblings`: other+causes observed on the same lines. A rejected filter value is echoed with its+accepted values in `ignored_arguments`. Performance findings carry `health_impact: 0` by construction, sothey never competed for the main `directive` and the dashboard used to reportcounts and nothing to act on. `clear` means no supported pattern surfaced, whichis not a claim about how the code runs; `unavailable` means this index has notdiff --git a/docs/layers/CODE_HEALTH.md b/docs/layers/CODE_HEALTH.mdindex 2fb66988f..c2531a315 100644--- a/docs/layers/CODE_HEALTH.md+++ b/docs/layers/CODE_HEALTH.md@@ -534,6 +534,27 @@ findings are usually batchable; filesystem ones often are not, since deleting Nfiles genuinely needs N unlinks. The finding still tells you where the timegoes.+A few more things keep the plans honest:++- **Proven means the transformation, not the runtime.** Parallelizing awaits+ against a database or network client is advisory with a+ `bounded_concurrency` prerequisite, however clean the dataflow. A loop that+ already walks its input in chunks (`range(0, n, CHUNK)`, `batched(...)`) is+ reported as `loop_already_chunked` rather than told to batch.+- **One line, one problem.** When `io_in_loop` and `serial_await_in_loop` fire+ on the same call, each names the other in `siblings`, and batching is queued+ right before the parallelize variant.+- **Plans carry steps and validation.** Each performance plan lists its edits+ (`mechanical` only when the strategy is proven) and the tests that validate+ it: coverage, then call graph, then import graph, then a test named for the+ file (`via: "name-match"`).++**Over-fetch.** `unbounded_read_reduced_in_memory` (Python, advisory) flags a+query with no limit or aggregate whose rows are then deduplicated per key in+code, in the same function or one same-file helper. It is the one shape here+that is not a loop around I/O: the query runs once and returns too much, and+the fix is to select one row per key in the database.+Methodology and raw data:[perf-detection](https://github.com/repowise-dev/repowise-bench/tree/master/perf-detection).diff --git a/packages/core/src/repowise/core/analysis/change_health/perf_delta.py b/packages/core/src/repowise/core/analysis/change_health/perf_delta.pyindex a0a048276..c618377a0 100644--- a/packages/core/src/repowise/core/analysis/change_health/perf_delta.py+++ b/packages/core/src/repowise/core/analysis/change_health/perf_delta.py@@ -12,7 +12,6 @@ from typing import Anyfrom ..health import HealthFindingDatafrom ..health.perf.opportunities import build_performance_opportunities-from ..health.perf.opportunity_rank import rank_sort_key#: Supporting evidence rows kept per surfaced opportunity.EVIDENCE_CAP = 3@@ -55,7 +54,7 @@ def opportunities_for(findings: list[HealthFindingData]) -> list[PerfOpportunityif not rows:return []built = build_performance_opportunities(rows, evidence_limit=max(len(rows), EVIDENCE_CAP))- return [_view(o) for o in sorted(built, key=rank_sort_key)]+ return [_view(o) for o in built]def index_by_finding(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.pyindex cf5c7c1f3..977ea32ec 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@@ -69,7 +69,11 @@ class IoInLoopDetector:function_name=hit.function,line_start=hit.line,line_end=hit.line,- details={"boundary_kind": hit.detail, "cross_function": False},+ details={+ "boundary_kind": hit.detail,+ "cross_function": False,+ **hit.loop_facts(),+ },reason=f"{phrasing} runs once per loop iteration (N+1 / IO-in-loop)",))diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/nested_loop_with_io.py b/packages/core/src/repowise/core/analysis/health/biomarkers/nested_loop_with_io.pyindex c718e70b1..d1a01a2c7 100644--- a/packages/core/src/repowise/core/analysis/health/biomarkers/nested_loop_with_io.py+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/nested_loop_with_io.py@@ -44,7 +44,7 @@ class NestedLoopWithIoDetector:function_name=hit.function,line_start=hit.line,line_end=hit.line,- details={"boundary_kind": hit.detail},+ details={"boundary_kind": hit.detail, **hit.loop_facts()},reason=(f"{phrasing} runs inside a nested loop (O(n·m) round-trips); ""batch the inner query or restructure the loops"diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/registry.py b/packages/core/src/repowise/core/analysis/health/biomarkers/registry.pyindex e104456f5..3ed2e8fbc 100644--- a/packages/core/src/repowise/core/analysis/health/biomarkers/registry.py+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/registry.py@@ -62,6 +62,7 @@ from .sql_high_complexity import SqlHighComplexityDetectorfrom .sql_select_star import SqlSelectStarDetectorfrom .sql_update_delete_without_where import SqlUpdateDeleteWithoutWhereDetectorfrom .string_concat_in_loop import StringConcatInLoopDetector+from .unbounded_read_reduced_in_memory import UnboundedReadReducedInMemoryDetectorfrom .untested_hotspot import UntestedHotspotDetector_DETECTOR_FACTORIES: list[type[Biomarker]] = [@@ -107,6 +108,7 @@ _DETECTOR_FACTORIES: list[type[Biomarker]] = [LockInLoopDetector, # type: ignore[list-item]SerialAwaitInLoopDetector, # type: ignore[list-item]MembershipTestAgainstListInLoopDetector, # type: ignore[list-item]+ UnboundedReadReducedInMemoryDetector, # type: ignore[list-item]# Phase 7b — centrality-gated moat markers.NestedLoopWithIoDetector, # type: ignore[list-item]NestedLoopQuadraticDetector, # type: ignore[list-item]diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/serial_await_in_loop.py b/packages/core/src/repowise/core/analysis/health/biomarkers/serial_await_in_loop.pyindex 5dc32a8f8..3efa0e122 100644--- a/packages/core/src/repowise/core/analysis/health/biomarkers/serial_await_in_loop.py+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/serial_await_in_loop.py@@ -41,13 +41,17 @@ class SerialAwaitInLoopDetector:if hit.promoted:# Dataflow proved the loop carries no data dependence between# iterations: assert the fan-out instead of hedging on it.- details = {"boundary_kind": hit.detail, "dataflow_verified": True}+ details = {+ "boundary_kind": hit.detail,+ "dataflow_verified": True,+ **hit.loop_facts(),+ }reason = (f"{phrasing} is awaited serially in a loop whose iterations ""carry no data dependence; fan out with gather / Promise.all")else:- details = {"boundary_kind": hit.detail}+ details = {"boundary_kind": hit.detail, **hit.loop_facts()}reason = (f"{phrasing} is awaited serially in a loop; if the ""iterations are independent, fan out with gather / Promise.all"diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/unbounded_read_reduced_in_memory.py b/packages/core/src/repowise/core/analysis/health/biomarkers/unbounded_read_reduced_in_memory.pynew file mode 100644index 000000000..e4534ed8c--- /dev/null+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/unbounded_read_reduced_in_memory.py@@ -0,0 +1,46 @@+"""Unbounded read reduced to one row per key in code.++Lifts ``perf.unbounded_reduction`` hits. ``PerfHit.path`` names the same-file+helper that does the reducing, when it is not the reading function itself.+"""++from __future__ import annotations++from ..models import Severity+from .base import BiomarkerResult, FileContext++_KIND = "unbounded_read_reduced_in_memory"+_FIX = "select one row per key in the query (DISTINCT ON, a window function, or a view)"+++class UnboundedReadReducedInMemoryDetector:+ name = _KIND+ category = "performance"++ def detect(self, ctx: FileContext) -> list[BiomarkerResult]:+ out: list[BiomarkerResult] = []+ for hit in ctx.perf_hits:+ if hit.kind != _KIND:+ continue+ where = f"in {hit.path[0]}" if hit.path else "here"+ out.append(+ BiomarkerResult(+ biomarker_type=self.name,+ severity=Severity.LOW,+ function_name=hit.function,+ line_start=hit.line,+ line_end=hit.line,+ details={+ "boundary_kind": hit.detail,+ **({"reduced_in": hit.path[0]} if hit.path else {}),+ },+ reason=(+ f"every row of an unbounded read is fetched, then reduced to one "+ f"per key {where}; {_FIX}"+ ),+ )+ )+ return out+++BIOMARKER = UnboundedReadReducedInMemoryDetector()diff --git a/packages/core/src/repowise/core/analysis/health/complexity/models.py b/packages/core/src/repowise/core/analysis/health/complexity/models.pyindex f134cdf97..7bcb5b0fb 100644--- a/packages/core/src/repowise/core/analysis/health/complexity/models.py+++ b/packages/core/src/repowise/core/analysis/health/complexity/models.py@@ -271,6 +271,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++ def loop_facts(self) -> dict[str, bool]:+ """Loop facts for ``details``; absent when unset so old findings are unchanged."""+ return {"chunked_iteration": True} if self.chunked else {}@dataclass(frozen=True)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.pyindex d4c64a1b8..863e369c1 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@@ -153,6 +153,20 @@ def _enclosing_loop_iterables(return names+def _in_chunked_loop(+ 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."""+ 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)+ cur = cur.parent+ return False++def _is_block_loop_body_scope(node: Node, dialect: BasePerfDialect, call_kinds: frozenset[str]) -> bool:@@ -232,6 +246,7 @@ def _collect_perf_hits(# 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_loophits: list[PerfHit] = []# Per-enclosing-function accumulators keyed by the function's start line@@ -357,7 +372,13 @@ def _collect_perf_hits()if kind is not None:if loop_depth >= 1:- hits.append(PerfHit("io_in_loop", line, next_func, kind, func_start=next_start))+ chunked = do_chunked and _in_chunked_loop(node, dialect, loop_kinds, fn_kinds)+ hits.append(+ PerfHit(+ "io_in_loop", line, next_func, kind, func_start=next_start,+ chunked=chunked,+ )+ )if do_serial_await and awaited:# An *awaited* sink in a loop body is additionally a# missed-concurrency candidate (a serial round-trip that@@ -366,7 +387,8 @@ def _collect_perf_hits(# the boundary kind for the finding.hits.append(PerfHit(- "serial_await_in_loop", line, next_func, kind, func_start=next_start+ "serial_await_in_loop", line, next_func, kind,+ func_start=next_start, chunked=chunked,))if do_nested_io and loop_depth >= 2 and outer_iter:@@ -379,7 +401,8 @@ def _collect_perf_hits(# (Phase-7c TS while-cursor FP).hits.append(PerfHit(- "nested_loop_with_io", line, next_func, kind, func_start=next_start+ "nested_loop_with_io", line, next_func, kind,+ func_start=next_start, chunked=chunked,))else:diff --git a/packages/core/src/repowise/core/analysis/health/engine.py b/packages/core/src/repowise/core/analysis/health/engine.pyindex 9eff27ef5..59bf6eba5 100644--- a/packages/core/src/repowise/core/analysis/health/engine.py+++ b/packages/core/src/repowise/core/analysis/health/engine.py@@ -21,7 +21,6 @@ import asyncioimport osimport timefrom datetime import UTC, datetime-from pathlib import Pathfrom typing import Anyimport structlog@@ -37,6 +36,7 @@ from ...ingestion.git_indexer.function_blame import (from ...ingestion.package_roots import module_for as _module_forfrom ...ingestion.package_roots import package_roots_from_paths as _package_rootsfrom ...ingestion.package_roots import scan_package_roots as _scan_package_roots+from ...test_paths import paired_test_namesfrom ..graph_view import HasEdge, ImportEdgeViewfrom ..test_reachability import files_reached_by_testsfrom .asserts.lexicon import AssertVocabulary@@ -58,6 +58,7 @@ from .perf import (collect_crossfn_io_in_loop,link_performance_findings,)+from .perf.unbounded_reduction import collect_unbounded_reductionsfrom .refactoring import (PerformancePlanPolicy,RefactoringContext,@@ -110,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.#+# 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.+## v21 moves two stored counts, both of which were order-dependent in the same# way. ``assertion_count`` rises on such a header. ``mock_setup_count`` falls on# it, because ``mock_walk._count_body_setup`` skips a statement@@ -240,7 +244,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 = 21+HEALTH_ANALYZER_VERSION = 22def walked_functions(@@ -417,48 +421,9 @@ def _path_basenames(all_paths: set[str]) -> set[str]:return {p.rsplit("/", 1)[-1] for p in all_paths}-_PASCAL_UNIT_SUFFIXES = frozenset({".pas", ".pp", ".dpr", ".dpk", ".lpr"})--def _has_paired_test_file(rel_path: str, path_basenames: set[str]) -> bool:- """Heuristic: does any other file look like a test for *rel_path*?-- Cheap and conservative — looks for common test-file naming- conventions paired with the same basename. *path_basenames* is the- precomputed ``_path_basenames`` set for the analyzed file list.- """- p = Path(rel_path)- stem = p.stem- test_suffix = ".exs" if p.suffix == ".ex" else p.suffix- candidates = {- f"test_{stem}{test_suffix}",- f"{stem}_test{test_suffix}",- f"{stem}_spec{test_suffix}",- f"{stem}.test.ts",- f"{stem}.test.tsx",- f"{stem}.test.js",- f"{stem}.test.mts",- f"{stem}.test.cts",- f"{stem}.spec.ts",- f"{stem}.spec.js",- f"{stem}.spec.mts",- f"{stem}.spec.cts",- }- if p.suffix.lower() in _PASCAL_UNIT_SUFFIXES:- # Delphi/FPC's lowercase "u" unit-name prefix (uFoo.pas) has no- # test-file convention of its own; real-world projects pair it with- # a standalone console test program named Test<Foo>.dpr (the "u" is- # dropped, the extension is .dpr since a runnable test program is a- # project file, not a unit). Only a lowercase "u" is stripped -- a- # stem that merely starts with capital "U" (Utils.pas) is a- # different word, not this naming convention. Confirmed against a- # real ~150-file Delphi codebase: uKeymap.pas <-> TestKeymap.dpr,- # uANSIParser.pas <-> TestANSIParser.dpr, uConsoleBuffer.pas <->- # TestConsoleBuffer.dpr, etc. -- src/tools/Test*.dpr, not next to- # the unit.- pascal_stem = stem[1:] if stem[:1] == "u" else stem- candidates.add(f"Test{pascal_stem}.dpr")- return not candidates.isdisjoint(path_basenames)+ """Whether any analyzed file is named like a test for *rel_path*."""+ return not paired_test_names(rel_path).isdisjoint(path_basenames)class HealthAnalyzer:@@ -712,6 +677,8 @@ class HealthAnalyzer:# centrality-gated nested-loop hits are present to promote).with timed(timings, "analysis.health.promotions"):apply_perf_promotions(walked, dataflow=dataflow_cache)+ with timed(timings, "analysis.health.unbounded_reduction"):+ collect_unbounded_reductions(walked, read_source=self.read_source)timings_evaluate = timed(timings, "analysis.health.evaluate")timings_evaluate.__enter__()@@ -774,7 +741,6 @@ class HealthAnalyzer:suggestions.extend(performance_fix_suggestions(opportunities,- nloc_by_file={metric.file_path: metric.nloc for metric in metrics},min_confidence=refactoring_min_confidence,))@@ -982,7 +948,6 @@ class HealthAnalyzer:suggestions.extend(performance_fix_suggestions(opportunities,- nloc_by_file={metric.file_path: metric.nloc for metric in metrics},min_confidence=refactoring_min_confidence,))diff --git a/packages/core/src/repowise/core/analysis/health/perf/actionability.py b/packages/core/src/repowise/core/analysis/health/perf/actionability.pyindex fd25c5752..41a7cffc1 100644--- a/packages/core/src/repowise/core/analysis/health/perf/actionability.py+++ b/packages/core/src/repowise/core/analysis/health/perf/actionability.py@@ -9,7 +9,7 @@ Three separate questions live here and must not collapse into one label:* evidence confidence, :func:`provenance_confidence`, asks how reliably thecall path was resolved;* fix safety, :attr:`PerformanceFix.safety`, asks how strongly the specific- transformation is proven;+ transformation is proven, not whether the runtime can absorb it;* actionability, :func:`actionability`, asks what to do with the group now, anddemotes a proven strategy whose evidence is weak."""@@ -29,11 +29,15 @@ FixStrategy = Literal["hoist_loop_invariant_resource","batch_or_prefetch_io","shrink_lock_scope",+ "push_reduction_into_query",]BATCHABLE_MARKERS = frozenset({"io_in_loop", "nested_loop_with_io"})BATCHABLE_BOUNDARIES = frozenset({"db", "network"})+# Fanning out N awaits here spends a pool, rate limit or statement timeout.+CONCURRENCY_SENSITIVE_BOUNDARIES = frozenset({"db", "network"})+@dataclass(frozen=True, slots=True)class PerformanceFix:@@ -52,11 +56,13 @@ class FixAssessment:Prerequisites are stable machine tokens rather than prose: a caller rendersthem, and a new detector fact is expected to clear one by name. They arepopulated whether or not a fix was returned, because an offered advisory- strategy has open questions too.+ strategy has open questions too. ``refusal`` names why no strategy was+ offered when the reason is a fact about the code, not a missing proof."""fix: PerformanceFix | Noneprerequisites: tuple[str, ...]+ refusal: str = "no_supported_strategy"@dataclass(frozen=True, slots=True)@@ -109,6 +115,17 @@ def assess_fix(if marker == "serial_await_in_loop":if not all(detail.get("dataflow_verified") for detail in details):return FixAssessment(None, ("loop_carried_dependence_proof",))+ if boundary in CONCURRENCY_SENSITIVE_BOUNDARIES:+ # Independence is proven; nothing here bounds the fan-out.+ return FixAssessment(+ PerformanceFix(+ "parallelize_independent_awaits",+ "advisory",+ "Iteration independence is proven; the concurrency the fan-out "+ "would create is not bounded by anything this group can see.",+ ),+ ("bounded_concurrency",),+ )return FixAssessment(PerformanceFix("parallelize_independent_awaits",@@ -137,7 +154,21 @@ def assess_fix(),("accumulator_not_observed",),)+ if marker == "unbounded_read_reduced_in_memory":+ return FixAssessment(+ PerformanceFix(+ "push_reduction_into_query",+ "advisory",+ "The read is proven unbounded and the per-key selection proven "+ "Python-side; whether the query layer can express that "+ "selection (DISTINCT ON / a window function / a view) is not.",+ ),+ ("query_supports_group_selection",),+ )if set(markers) <= BATCHABLE_MARKERS:+ if details and all(detail.get("chunked_iteration") for detail in details):+ # The loop is already the batch; "batch this" repeats advice taken.+ return FixAssessment(None, (), refusal="loop_already_chunked")if boundary not in BATCHABLE_BOUNDARIES:# Filesystem and subprocess repetition is real, but there is no# batch or prefetch operation to point the caller at.@@ -193,7 +224,7 @@ def actionability(fix = assessment.fixif fix is None:return Actionability(- "investigate", "no_supported_strategy", "low", assessment.prerequisites, None+ "investigate", assessment.refusal, "low", assessment.prerequisites, None)if evidence_confidence == "low":# A transformation proven against a path we could not resolve is notdiff --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 e1873b583..f93327b21 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@@ -271,6 +271,10 @@ class BasePerfDialect:"""return None+ def is_chunked_loop(self, node: Node) -> bool:+ """True when this loop walks its data a chunk at a time (already batched)."""+ return False+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:@@ -436,6 +440,14 @@ class BasePerfDialect:"""return None+ def unbounded_read_bound_methods(self) -> frozenset[str]:+ """Method names anywhere in a chain that prove a DB read is bounded.++ Default empty: a language that does not override this never scans+ for ``unbounded_read_reduced_in_memory`` (v1 is Python-only).+ """+ return frozenset()+def is_lock_scope(self, node: Node) -> bool:"""True if *node* opens a block-scoped held-lock region.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.pyindex ab98d10fe..2a02cb294 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@@ -41,6 +41,11 @@ PY_DB_AMBIGUOUS: frozenset[str] = frozenset({"all", "first", "one", "one_or_nonePY_SUBPROC_METHODS: frozenset[str] = frozenset({"run", "call", "check_call", "check_output", "Popen"})+# Any of these anywhere in a read's call chain proves it is bounded, gating+# ``unbounded_read_reduced_in_memory`` (analysis.health.perf.unbounded_reduction).+PY_UNBOUNDED_READ_BOUND_METHODS: frozenset[str] = frozenset(+ {"limit", "range", "single", "maybe_single", "first", "count", "head", "aggregate"}+)# Filesystem round-trips, in two strata for the same reason the DB verbs are# stratified: how much evidence the name alone carries.@@ -321,6 +326,30 @@ class PythonPerfDialect(BasePerfDialect):return first.text.decode("utf-8", "replace")return None+ # ``itertools.batched``, ``more_itertools.chunked`` and hand-rolled peers.+ _CHUNKING_CALLS: frozenset[str] = frozenset(+ {"batched", "chunked", "ichunked", "chunks", "iter_chunks", "grouper"}+ )++ def is_chunked_loop(self, node: Node) -> bool:+ """``range(start, stop, step)`` with a non-unit step, or a chunking helper."""+ if node.type != "for_statement":+ return False+ right = node.child_by_field_name("right")+ if right is None or right.type != "call":+ return False+ fn = right.child_by_field_name("function")+ if fn is None or fn.text is None:+ return False+ name = fn.text.decode("utf-8", "replace").rsplit(".", 1)[-1]+ if name in self._CHUNKING_CALLS:+ return True+ if name != "range":+ return False+ args = right.child_by_field_name("arguments")+ named = [c for c in args.children if c.is_named] if args is not None else []+ return len(named) == 3 and named[2].text not in (b"1", b"-1")+def is_string_concat(self, node: Node) -> bool:"""``s += "x"`` accumulation — but skip an accumulator that is *reset*each iteration of an enclosing loop.@@ -368,6 +397,9 @@ class PythonPerfDialect(BasePerfDialect):stack.extend(n.children)return False+ def unbounded_read_bound_methods(self) -> frozenset[str]:+ return PY_UNBOUNDED_READ_BOUND_METHODS+def blocking_sync_api(self, root: str, method: str) -> str | None:"""The offending API name if ``root.method`` is a known blocking sync call.diff --git a/packages/core/src/repowise/core/analysis/health/perf/opportunities.py b/packages/core/src/repowise/core/analysis/health/perf/opportunities.pyindex 33924a19d..85586c881 100644--- a/packages/core/src/repowise/core/analysis/health/perf/opportunities.py+++ b/packages/core/src/repowise/core/analysis/health/perf/opportunities.py@@ -58,6 +58,7 @@ from .opportunity_rank import (weakest_provenance,why_ranked,)+from .siblings import link_siblings@dataclass(frozen=True, slots=True)@@ -96,6 +97,8 @@ class PerformanceOpportunity:rank_factors: dict[str, int]why_ranked: tuple[dict[str, Any], ...]fix: PerformanceFix | None+ # Other causes observed on the same lines; see :mod:`.siblings`.+ siblings: tuple[dict[str, Any], ...] = ()def as_dict(self) -> dict[str, Any]:return {@@ -125,6 +128,7 @@ class PerformanceOpportunity:"rank_factors": dict(self.rank_factors),"why_ranked": [dict(entry) for entry in self.why_ranked],"fix": self.fix.as_dict() if self.fix else None,+ "siblings": [dict(entry) for entry in self.siblings],}@@ -219,13 +223,15 @@ def _assemble(key: Any, members: list[Any], cap: int) -> PerformanceOpportunity:def build_performance_opportunities(findings: list[Any], *, evidence_limit: int = 8) -> list[PerformanceOpportunity]:- """Group and rank performance rows in one deterministic pass."""+ """Group, link, and rank performance rows in one deterministic pass."""cap = max(0, evidence_limit)- opportunities = [- _assemble(key, members, cap) for key, members in group_observations(findings).items()- ]- opportunities.sort(key=rank_sort_key)- return opportunities+ opportunities: list[PerformanceOpportunity] = []+ sites: dict[str, set[Any]] = {}+ for key, members in group_observations(findings).items():+ opportunity = _assemble(key, members, cap)+ opportunities.append(opportunity)+ sites[opportunity.opportunity_id] = {facts.site for facts in members}+ return link_siblings(opportunities, sites, rank_sort_key)__all__ = [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.pyindex bf64d6632..7688cf980 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@@ -40,6 +40,7 @@ MULTIPLIER_POINTS = {"resource_construction_in_loop": 4,"goroutine_in_unbounded_loop": 4,"hot_path_sync_io": 4,+ "unbounded_read_reduced_in_memory": 4,# In-loop CPU or allocation: real, and orders below a round-trip."membership_test_against_list_in_loop": 3,"string_concat_in_loop": 3,@@ -76,6 +77,9 @@ AMPLIFICATION = {"nested_loop_quadratic": "quadratic","hot_path_sync_io": "per_call","blocking_sync_in_async": "per_call",+ # Runs once (not per iteration); its cost scales with the unbounded row+ # count the read transfers and decodes, not with a loop trip count.+ "unbounded_read_reduced_in_memory": "per_call",}"""Marker to the repetition shape its evidence supports.@@ -96,7 +100,7 @@ _LEVERAGE_BANDS = ((1, "isolated"), (3, "local"), (9, "shared"))_CHANGE_RISK_BANDS = ((1, "contained"), (4, "moderate"))-def _band(value: int, bands: tuple[tuple[int, str], ...], beyond: str) -> str:+def band(value: int, bands: tuple[tuple[int, str], ...], beyond: str) -> str:for ceiling, label in bands:if value <= ceiling:return label@@ -139,7 +143,7 @@ def exposure(reachable: bool | None) -> str:def leverage(call_sites: int) -> str:"""How many places one intervention would settle."""- return _band(call_sites, _LEVERAGE_BANDS, "broad")+ return band(call_sites, _LEVERAGE_BANDS, "broad")def change_risk(affected_files: int) -> str:@@ -148,7 +152,7 @@ def change_risk(affected_files: int) -> str:Structural reach only. Churn and hotspot history live in the serving layerand are not read here, so nothing in this module costs a query."""- return _band(affected_files, _CHANGE_RISK_BANDS, "wide")+ return band(affected_files, _CHANGE_RISK_BANDS, "wide")def observation_rank(marker: str | None, boundary: str | None, cross_function: bool) -> int:@@ -230,6 +234,7 @@ __all__ = ["PROVENANCE_POINTS","UNKNOWN_MULTIPLIER_POINTS","amplification",+ "band","change_risk","dominant_marker","exposure",diff --git a/packages/core/src/repowise/core/analysis/health/perf/reduction_idioms.py b/packages/core/src/repowise/core/analysis/health/perf/reduction_idioms.pynew file mode 100644index 000000000..ec0836ba1--- /dev/null+++ b/packages/core/src/repowise/core/analysis/health/perf/reduction_idioms.py@@ -0,0 +1,153 @@+"""Loop bodies that keep one row per key: the in-code half of an over-fetch.++Three first-wins shapes, each keyed by something read off the loop row:+``d.setdefault(key, row)``, ``if key not in d: d[key] = row``, and+``if key in seen: continue`` followed by ``seen.add(key)``. A bare+``d[row.id] = row`` is a 1:1 projection, not a reduction, and never matches.+"""++from __future__ import annotations++from collections.abc import Iterator+from typing import TYPE_CHECKING++from ..complexity.ast_utils import _node_text++if TYPE_CHECKING:+ from tree_sitter import Node+++def walk(node: Node, stop_at: frozenset[str] = frozenset()) -> Iterator[Node]:+ """Pre-order walk that does not enter a nested scope of a *stop_at* kind."""+ stack = [node]+ while stack:+ current = stack.pop()+ yield current+ if current is node or current.type not in stop_at:+ stack.extend(current.children)+++def ident(node: Node | None) -> str | None:+ """The name when *node* is a bare identifier, else ``None``."""+ return _node_text(node) if node is not None and node.type == "identifier" else None+++def field_text(node: Node, field: str) -> str:+ child = node.child_by_field_name(field)+ return _node_text(child) if child is not None else ""+++def references(node: Node, names: frozenset[str]) -> bool:+ return any(ident(n) in names for n in walk(node))+++def _method_call(node: Node) -> tuple[str | None, str, list[Node]] | None:+ """``recv.method(args)`` as ``(recv name, method, named args)``."""+ if node.type == "expression_statement":+ node = next((c for c in node.children if c.is_named), node)+ function = node.child_by_field_name("function") if node.type == "call" else None+ if function is None or function.type != "attribute":+ return None+ args = node.child_by_field_name("arguments")+ named = [a for a in args.children if a.is_named] if args is not None else []+ return ident(function.child_by_field_name("object")), field_text(function, "attribute"), named+++def _membership(stmt: Node) -> tuple[str, Node, str, Node] | None:+ """``if key [not] in name: ...`` as ``(operator, key, name, consequence)``."""+ if stmt.type != "if_statement":+ return None+ condition = stmt.child_by_field_name("condition")+ consequence = stmt.child_by_field_name("consequence")+ if condition is None or consequence is None or condition.type != "comparison_operator":+ return None+ operator = next((c.type for c in condition.children if c.type in ("in", "not in")), None)+ named = [c for c in condition.children if c.is_named]+ container = ident(named[-1]) if len(named) >= 2 else None+ if operator is None or container is None:+ return None+ return operator, named[0], container, consequence+++def _row_names(for_stmt: Node) -> frozenset[str]:+ left = for_stmt.child_by_field_name("left")+ return frozenset(filter(None, (ident(n) for n in walk(left)))) if left else frozenset()+++def _derived(stmts: list[Node], rows: frozenset[str]) -> frozenset[str]:+ """*rows* plus locals assigned from them (``rid = row["repo_id"]``), one hop."""+ derived = set(rows)+ for stmt in stmts:+ node = next((c for c in stmt.children if c.is_named), stmt)+ if node.type != "assignment":+ continue+ name = ident(node.child_by_field_name("left"))+ right = node.child_by_field_name("right")+ if name and right is not None and references(right, frozenset(derived)):+ derived.add(name)+ return frozenset(derived)+++def _keeps_first_via_setdefault(stmt: Node, rows: frozenset[str], keys: frozenset[str]) -> bool:+ call = _method_call(stmt)+ if call is None or call[1] != "setdefault" or len(call[2]) < 2:+ return False+ key, value = call[2][0], call[2][1]+ return ident(value) in rows and references(key, keys)+++def _stores_row(consequence: Node, container: str, rows: frozenset[str]) -> bool:+ """``container[...] = row`` (or a field of it), not a computed value."""+ for node in walk(consequence):+ left = node.child_by_field_name("left") if node.type == "assignment" else None+ right = node.child_by_field_name("right") if left is not None else None+ if (+ left is not None+ and right is not None+ and left.type == "subscript"+ and ident(left.child_by_field_name("value")) == container+ and right.type in ("identifier", "attribute", "subscript")+ and references(right, rows)+ ):+ return True+ return False+++def _adds_to(stmts: list[Node], container: str, keys: frozenset[str]) -> bool:+ calls = (_method_call(n) for stmt in stmts for n in walk(stmt) if n.type == "call")+ return any(+ call and call[0] == container and call[1] == "add" and call[2]+ and references(call[2][0], keys)+ for call in calls+ )+++def _keeps_first_via_guard(+ stmt: Node, rest: list[Node], rows: frozenset[str], keys: frozenset[str]+) -> bool:+ guard = _membership(stmt)+ if guard is None or not references(guard[1], keys):+ return False+ operator, _key, container, consequence = guard+ if operator == "not in":+ return _stores_row(consequence, container, rows)+ skips = any(n.type == "continue_statement" for n in walk(consequence))+ return skips and _adds_to(rest, container, keys)+++def reduces_per_key(for_stmt: Node) -> bool:+ """Whether this loop keeps one row per key of what it iterates."""+ body = for_stmt.child_by_field_name("body")+ rows = _row_names(for_stmt)+ if body is None or not rows:+ return False+ stmts = [c for c in body.children if c.is_named]+ keys = _derived(stmts, rows)+ return any(+ _keeps_first_via_setdefault(stmt, rows, keys)+ or _keeps_first_via_guard(stmt, stmts[index + 1 :], rows, keys)+ for index, stmt in enumerate(stmts)+ )+++__all__ = ["field_text", "ident", "reduces_per_key", "references", "walk"]diff --git a/packages/core/src/repowise/core/analysis/health/perf/siblings.py b/packages/core/src/repowise/core/analysis/health/perf/siblings.pynew file mode 100644index 000000000..6b38e1c1b--- /dev/null+++ b/packages/core/src/repowise/core/analysis/health/perf/siblings.py@@ -0,0 +1,105 @@+"""Opportunities that observe the same lines, and which fix leads among them.++One awaited query in a loop is both ``io_in_loop`` and ``serial_await_in_loop``.+They are one problem with two remedies, so each names the other, and a weaker+remedy is queued directly after the stronger one instead of wherever its own+actionability would put it.+"""++from __future__ import annotations++from collections import defaultdict+from collections.abc import Callable, Iterable+from dataclasses import replace+from typing import Any, TypeVar++T = TypeVar("T")++STRATEGY_PREFERENCE = ("batch_or_prefetch_io", "parallelize_independent_awaits")+"""Earlier wins: batching removes round-trips, parallelizing only overlaps them."""+++def _strategy(item: Any) -> str | None:+ return item.fix.strategy if item.fix else None+++def _preference(item: Any) -> int | None:+ strategy = _strategy(item)+ return STRATEGY_PREFERENCE.index(strategy) if strategy in STRATEGY_PREFERENCE else None+++def _relation(own: int | None, other: int | None) -> str:+ if own is None or other is None or own == other:+ return "same_site"+ return "preferred" if other < own else "alternative"+++def link_siblings(+ items: list[T], sites: dict[str, set[Any]], sort_key: Callable[[T], Any]+) -> list[T]:+ """Attach ``siblings`` to every item sharing a site, and order the queue.++ *sites* maps each opportunity id to every site its members observed, not+ only the capped evidence, so a cause is never missed for being row nine.+ """+ by_site: dict[Any, set[str]] = defaultdict(set)+ for opportunity_id, members in sites.items():+ for site in members:+ by_site[site].add(opportunity_id)+ by_id = {item.opportunity_id: item for item in items}+ ordered = sorted(items, key=sort_key)+ position = {item.opportunity_id: index for index, item in enumerate(ordered)}++ linked: dict[str, T] = {}+ leader: dict[str, str] = {}+ for item in ordered:+ oid = item.opportunity_id+ others = sorted(+ {o for site in sites.get(oid, ()) for o in by_site[site]} - {oid},+ key=position.__getitem__,+ )+ own = _preference(item)+ siblings = tuple(+ {+ "opportunity_id": other,+ "biomarker_type": by_id[other].biomarker_type,+ "strategy": _strategy(by_id[other]),+ "relation": _relation(own, _preference(by_id[other])),+ }+ for other in others+ )+ linked[oid] = replace(item, siblings=siblings) if siblings else item+ preferred = next((s["opportunity_id"] for s in siblings if s["relation"] == "preferred"), None)+ if preferred is not None:+ leader[oid] = preferred+ return _queue([linked[item.opportunity_id] for item in ordered], leader)+++def _queue(ordered: Iterable[T], leader: dict[str, str]) -> list[T]:+ """Rank order, with each superseded item moved to just after its leader."""+ followers: dict[str, list[T]] = defaultdict(list)+ for item in ordered:+ if item.opportunity_id in leader:+ followers[leader[item.opportunity_id]].append(item)+ out: list[T] = []+ placed: set[str] = set()++ def place(item: T) -> None:+ if item.opportunity_id in placed:+ return+ placed.add(item.opportunity_id)+ out.append(item)+ for follower in followers.get(item.opportunity_id, ()):+ place(follower)++ for item in ordered:+ if item.opportunity_id not in leader:+ place(item)+ for item in ordered:+ # A leader cycle cannot form (preference is a strict order), but never+ # drop an item if one did.+ place(item)+ return out+++__all__ = ["STRATEGY_PREFERENCE", "link_siblings"]diff --git a/packages/core/src/repowise/core/analysis/health/perf/unbounded_reduction.py b/packages/core/src/repowise/core/analysis/health/perf/unbounded_reduction.pynew file mode 100644index 000000000..aadc94783--- /dev/null+++ b/packages/core/src/repowise/core/analysis/health/perf/unbounded_reduction.py@@ -0,0 +1,277 @@+"""An unbounded read whose rows are then reduced to one per key in code.++A query with no ``limit``/``range``/``single`` in its chain, run once, whose+result a loop deduplicates per key, pays to transfer every row to keep a+handful. The loop may sit in the same function or in one same-file helper the+result is passed to. Python only: the dialect names the bounding methods, and+every other dialect names none.+"""++from __future__ import annotations++from dataclasses import dataclass+from pathlib import Path+from typing import TYPE_CHECKING, Any++import structlog++from ..complexity.ast_utils import _collect_function_nodes, _find_function_entry_name+from ..complexity.models import PerfHit+from .dialects import PERF_DIALECTS+from .io_boundaries import collect_io_names+from .reduction_idioms import field_text, ident, reduces_per_key, walk++if TYPE_CHECKING:+ from collections.abc import Iterable++ from tree_sitter import Node++ from ..complexity.models import FileComplexity+ from ..source_reader import SourceReader+ from .dialects.base import BasePerfDialect++log = structlog.get_logger(__name__)++_KIND = "unbounded_read_reduced_in_memory"+_LOOPS = ("for_statement", "while_statement")+# Accessors that unwrap a result into its rows (``res.data``, ``res.scalars().all()``).+_RESULT_ACCESSORS = frozenset({"data", "scalars", "all", "fetchall"})+++@dataclass(frozen=True, slots=True)+class _FileScan:+ dialect: BasePerfDialect+ io_names: dict[str, str]+ bound_methods: frozenset[str]+ functions: dict[str, Node]+ scopes: frozenset[str]++ def is_db_read(self, call: Node) -> bool:+ return (+ self.dialect.sink_kind(+ self.dialect.callee_root_name(call) or "",+ self.dialect.callee_method_name(call) or "",+ awaited=False,+ is_attribute=self.dialect.callee_is_attribute(call),+ io_names=self.io_names,+ has_db_import="db" in self.io_names.values(),+ )+ == "db"+ )++ def is_bounded(self, stmt: Node) -> bool:+ chain = stmt.child_by_field_name("right")+ return chain is not None and any(+ n.type == "call" and self.dialect.callee_method_name(n) in self.bound_methods+ for n in walk(chain)+ )+++def collect_unbounded_reductions(+ walked: Iterable[tuple[Any, FileComplexity]],+ read_source: SourceReader | None = None,+) -> None:+ """Append hits to each file's ``perf_hits``; one file failing skips only it."""+ reader = read_source or _disk_read+ for pf, fcx in walked:+ try:+ fcx.perf_hits.extend(_file_hits(pf, reader))+ except Exception as exc:+ log.debug("unbounded_reduction_failed", path=pf.file_info.abs_path, error=str(exc))+++def _disk_read(abs_path: str) -> bytes | None:+ try:+ return Path(abs_path).read_bytes()+ except OSError:+ return None+++def _file_hits(pf: Any, read_source: SourceReader) -> list[PerfHit]:+ language = pf.file_info.language+ dialect = PERF_DIALECTS.get(language)+ bound_methods = dialect.unbounded_read_bound_methods() if dialect else frozenset()+ source = read_source(pf.file_info.abs_path) if bound_methods else None+ if dialect is None or source is None:+ return []+ from ..dataflow.parsing import parse_source # deferred: dataflow imports perf++ parsed = parse_source(pf.file_info.abs_path, language, source)+ if parsed is None:+ return []+ root, lmap = parsed+ named = [(fn, _find_function_entry_name(fn, lmap)) for fn in _collect_function_nodes(root, lmap)]+ scan = _FileScan(+ dialect=dialect,+ io_names=collect_io_names(root, language),+ bound_methods=bound_methods,+ functions={name: fn for fn, name in reversed(named) if name},+ scopes=lmap.function_kinds | lmap.lambda_kinds,+ )+ return [hit for fn, name in named for hit in _function_hits(fn, name, scan)]+++def _function_hits(fn: Node, name: str | None, scan: _FileScan) -> list[PerfHit]:+ body = fn.child_by_field_name("body")+ if body is None:+ return []+ hits: list[PerfHit] = []+ seen: set[tuple[int, int]] = set()+ for call in walk(body, scan.scopes):+ if call.type != "call" or not scan.is_db_read(call):+ continue+ site = _read_site(call, fn)+ # A chain holds several sink calls; its statement is the unit.+ if site is None or (site[0].start_byte, site[0].end_byte) in seen:+ continue+ stmt, var = site+ seen.add((stmt.start_byte, stmt.end_byte))+ reducer = None if scan.is_bounded(stmt) else _reducer(stmt, var, fn, scan)+ if reducer is not None:+ hits.append(+ PerfHit(+ _KIND,+ stmt.start_point[0] + 1,+ name,+ "db",+ func_start=fn.start_point[0] + 1,+ path=(reducer,) if reducer else (),+ )+ )+ return hits+++def _in_loop_body(node: Node, fn: Node) -> bool:+ cur = node+ while cur.parent is not None and cur != fn:+ if cur.parent.type in _LOOPS and cur == cur.parent.child_by_field_name("body"):+ return True+ cur = cur.parent+ return False+++def _read_site(call: Node, fn: Node) -> tuple[Node, str | None] | None:+ """The statement a read runs in once: a loop header, or ``name = read``."""+ if _in_loop_body(call, fn):+ return None+ cur = call+ while cur.parent is not None and cur != fn:+ parent = cur.parent+ if cur == parent.child_by_field_name("right"):+ if parent.type == "for_statement":+ return parent, None+ if parent.type == "assignment":+ name = ident(parent.child_by_field_name("left"))+ return (parent, name) if name else None+ cur = parent+ return None+++def _reducer(stmt: Node, var: str | None, fn: Node, scan: _FileScan) -> str | None:+ """``""`` when *fn* reduces the rows itself, a helper's name, or ``None``."""+ if var is None:+ return "" if reduces_per_key(stmt) else None+ if _rebound_after(fn, var, stmt.end_byte, scan.scopes):+ return None+ loop = _consuming_loop(fn, var, scan.scopes)+ if loop is not None and reduces_per_key(loop):+ return ""+ return _reducing_helper(fn, var, scan)+++def _rebound_after(fn: Node, var: str, after: int, scopes: frozenset[str]) -> bool:+ """A name reused for other data later must not read as the query result."""+ return any(+ n.type == "assignment"+ and n.start_byte > after+ and ident(n.child_by_field_name("left")) == var+ for n in walk(fn, scopes)+ )+++def _unwrap(node: Node) -> Node:+ """Peel parentheses and ``rows or []`` down to the rows."""+ while node.type in ("parenthesized_expression", "boolean_operator"):+ inner = (+ node.child_by_field_name("left")+ if node.type == "boolean_operator"+ else next((c for c in node.children if c.is_named), None)+ )+ if inner is None:+ break+ node = inner+ return node+++def _is_rows_of(node: Node, var: str) -> bool:+ """``var``, ``var.data``, ``var.scalars().all()`` and similar."""+ node = _unwrap(node)+ while ident(node) is None:+ target = node.child_by_field_name("function") if node.type == "call" else node+ if target is None or target.type != "attribute":+ return False+ if field_text(target, "attribute") not in _RESULT_ACCESSORS:+ return False+ inner = target.child_by_field_name("object")+ if inner is None:+ return False+ node = inner+ return ident(node) == var+++def _consuming_loop(fn: Node, var: str, scopes: frozenset[str]) -> Node | None:+ return next(+ (+ n+ for n in walk(fn, scopes)+ if n.type == "for_statement"+ and (iterable := n.child_by_field_name("right")) is not None+ and _is_rows_of(iterable, var)+ and not _in_loop_body(n, fn)+ ),+ None,+ )+++def _param_names(fn: Node) -> list[str]:+ params = fn.child_by_field_name("parameters")+ names = []+ for param in params.children if params is not None else ():+ name = ident(param) or ident(param.child_by_field_name("name"))+ name = name or next(filter(None, (ident(c) for c in param.children)), None)+ if param.is_named and name:+ names.append(name)+ return names+++def _bound_param(args: Node, var: str, helper: Node) -> str | None:+ """The helper parameter that receives *var*'s rows at this call."""+ params = _param_names(helper)+ position = 0+ for arg in (a for a in args.children if a.is_named):+ if arg.type == "keyword_argument":+ value = arg.child_by_field_name("value")+ if value is not None and _is_rows_of(value, var):+ return field_text(arg, "name") or None+ continue+ if _is_rows_of(arg, var):+ return params[position] if position < len(params) else None+ position += 1+ return None+++def _reducing_helper(fn: Node, var: str, scan: _FileScan) -> str | None:+ for call in walk(fn, scan.scopes):+ name = ident(call.child_by_field_name("function")) if call.type == "call" else None+ helper = scan.functions.get(name) if name else None+ args = call.child_by_field_name("arguments")+ if helper is None or helper == fn or args is None:+ continue+ param = _bound_param(args, var, helper)+ loop = _consuming_loop(helper, param, scan.scopes) if param else None+ if loop is not None and reduces_per_key(loop):+ return name+ return None+++__all__ = ["collect_unbounded_reductions"]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 07b520e04..7d7808858 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@@ -2,14 +2,37 @@from __future__ import annotations-from collections.abc import Iterable, Mapping+from collections.abc import Iterablefrom dataclasses import dataclassfrom repowise.core.analysis.execution_graph import file_of_symbol+from ..perf.actionability import PerformanceFixfrom ..perf.opportunities import PerformanceOpportunity+from ..perf.opportunity_rank import bandfrom .models import RefactoringSuggestion-from .registry import effort_bucket+from .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.+_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",+ ),+ "parallelize_independent_awaits": (+ None,+ "Run the independent awaits 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"),+ "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",+ ),+}@dataclass(frozen=True, slots=True)@@ -25,10 +48,53 @@ class PerformancePlanPolicy:min_confidence: str | None = None+def fix_steps(+ strategy: str,+ safety: str,+ intervention: str | None,+ sink: str | None,+ locations: list[dict[str, object]],+) -> list[dict[str, object]]:+ """Ordered edits for one plan; mechanical only when the strategy is proven."""+ intro, per_site = _STEPS.get(strategy, (None, ""))+ applicability: Applicability = "mechanical" if safety == "proven" else "judgment"+ steps: list[dict[str, object]] = []+ if intro:+ target = intervention or sink+ steps.append(+ {+ "action": intro.format(sink=(target or "the shared sink").rsplit("::", 1)[-1]),+ "symbol": target,+ "file_path": file_of_symbol(target) if target else None,+ "line": None,+ "applicability": "judgment",+ }+ )+ if per_site:+ steps.extend(+ {+ "action": per_site,+ "symbol": location.get("function_name"),+ "file_path": location.get("file_path"),+ "line": location.get("line_start"),+ "applicability": applicability,+ }+ for location in locations+ )+ return [{"order": index, **step} for index, step in enumerate(steps, 1)]+++_EFFORT_BANDS = ((1, "S"), (3, "M"), (8, "L"))+++def edit_effort(sites: int) -> str:+ """Effort from how many places change, not the size of the file they sit in."""+ return band(sites, _EFFORT_BANDS, "XL")++def performance_fix_suggestions(opportunities: Iterable[PerformanceOpportunity],*,- nloc_by_file: Mapping[str, int] | None = None,min_confidence: str | None = None,) -> list[RefactoringSuggestion]:"""Convert opportunities with a safe strategy into one closed plan type.@@ -36,83 +102,92 @@ def performance_fix_suggestions(This is intentionally a repo-level service, not a per-file detector: onecross-function cause can address observations in many caller files."""- nloc_by_file = nloc_by_file or {}confidence_order = {"low": 0, "medium": 1, "high": 2}floor = confidence_order.get((min_confidence or "").lower(), 0)out: list[RefactoringSuggestion] = []for opportunity in opportunities:fix = opportunity.fix- if fix is None or not opportunity.evidence:- continue- confidence = "high" if fix.safety == "proven" else "medium"- if confidence_order[confidence] < floor:+ confidence = "high" if fix and fix.safety == "proven" else "medium"+ if fix is None or not opportunity.evidence or confidence_order[confidence] < floor:continue- anchor = opportunity.evidence[0]- intervention = opportunity.intervention_symbol- if fix.strategy == "shrink_lock_scope" and anchor.get("path"):- # The lock owner is the first node in the proven lock-to-I/O path;- # editing a shared downstream sink would not shorten its critical- # section. Opportunity construction only emits this plan when all- # grouped paths have the same owner.- intervention = anchor["path"][0]- target_file = file_of_symbol(intervention) if intervention else 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"),- }- for item in opportunity.evidence- ]- paths = [item["path"] for item in opportunity.evidence if item.get("path")]- out.append(- RefactoringSuggestion(- refactoring_type="performance_fix",- file_path=target_file,- target_symbol=intervention or str(anchor.get("function_name") or target_file),- line_start=(- anchor.get("line_start") if target_file == anchor["file_path"] else None- ),- line_end=(anchor.get("line_end") if target_file == anchor["file_path"] else None),- plan={- "opportunity_id": opportunity.opportunity_id,- "strategy": fix.strategy,- "safety": fix.safety,- "intervention_symbol": intervention,- "affected_locations": locations,- "affected_locations_total": opportunity.affected_call_sites_total,- "paths": paths,- "paths_total": opportunity.observations_total,- "evidence_truncated": opportunity.evidence_truncated,- },- evidence={- "biomarker_type": opportunity.biomarker_type,- "biomarker_types": list(opportunity.biomarker_types),- "boundary_kind": opportunity.boundary_kind,- "execution_context": opportunity.execution_context,- "provenance": opportunity.provenance,- "reliable_entry_reachability": opportunity.reliable_entry_reachability,- "rank_score": opportunity.rank_score,- "rank_factors": opportunity.rank_factors,- "rationale": fix.rationale,- "observations_total": opportunity.observations_total,- "affected_files_total": opportunity.affected_files_total,- },- # Performance is a separate score dimension and its findings- # intentionally carry zero defect-health impact.- impact_delta=0.0,- effort_bucket=effort_bucket(int(nloc_by_file.get(target_file, 0))),- blast_radius={- "files": sorted({item["file_path"] for item in opportunity.evidence}),- "file_count": opportunity.affected_files_total,- "call_sites": opportunity.affected_call_sites_total,- },- confidence=confidence,- source_biomarker=opportunity.biomarker_type,- )- )+ out.append(_suggestion(opportunity, fix, confidence))return out-__all__ = ["PerformancePlanPolicy", "performance_fix_suggestions"]+def _intervention(opportunity: PerformanceOpportunity, fix: PerformanceFix) -> str | None:+ """Where the edit lands. A lock fix edits the lock owner, the first node of+ its proven path, never the shared sink downstream of it."""+ anchor = opportunity.evidence[0]+ if fix.strategy == "shrink_lock_scope" and anchor.get("path"):+ return anchor["path"][0]+ return opportunity.intervention_symbol+++def _suggestion(+ opportunity: PerformanceOpportunity, fix: PerformanceFix, confidence: str+) -> RefactoringSuggestion:+ anchor = opportunity.evidence[0]+ 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"),+ }+ for item in opportunity.evidence+ ]+ steps = fix_steps(fix.strategy, fix.safety, intervention, opportunity.terminal_sink, locations)+ mechanical = sum(step["applicability"] == "mechanical" for step in steps)+ return RefactoringSuggestion(+ refactoring_type="performance_fix",+ file_path=target_file,+ target_symbol=intervention or str(anchor.get("function_name") or target_file),+ line_start=anchor.get("line_start") if in_anchor_file else None,+ line_end=anchor.get("line_end") if in_anchor_file else None,+ plan={+ "opportunity_id": opportunity.opportunity_id,+ "strategy": fix.strategy,+ "safety": fix.safety,+ "intervention_symbol": intervention,+ "affected_locations": locations,+ "affected_locations_total": opportunity.affected_call_sites_total,+ "paths": [item["path"] for item in opportunity.evidence if item.get("path")],+ "paths_total": opportunity.observations_total,+ "evidence_truncated": opportunity.evidence_truncated,+ "steps": steps,+ "mechanical_steps": mechanical,+ "judgment_steps": len(steps) - mechanical,+ },+ evidence={+ "biomarker_type": opportunity.biomarker_type,+ "biomarker_types": list(opportunity.biomarker_types),+ "boundary_kind": opportunity.boundary_kind,+ "execution_context": opportunity.execution_context,+ "provenance": opportunity.provenance,+ "reliable_entry_reachability": opportunity.reliable_entry_reachability,+ "rank_score": opportunity.rank_score,+ "rank_factors": opportunity.rank_factors,+ "rationale": fix.rationale,+ "observations_total": opportunity.observations_total,+ "affected_files_total": opportunity.affected_files_total,+ },+ # Performance is a separate score dimension and its findings+ # intentionally carry zero defect-health impact.+ impact_delta=0.0,+ effort_bucket=edit_effort(+ opportunity.affected_call_sites_total + (len(steps) > len(locations))+ ),+ blast_radius={+ "files": sorted({item["file_path"] for item in opportunity.evidence}),+ "file_count": opportunity.affected_files_total,+ "call_sites": opportunity.affected_call_sites_total,+ },+ confidence=confidence,+ source_biomarker=opportunity.biomarker_type,+ )+++__all__ = ["PerformancePlanPolicy", "edit_effort", "fix_steps", "performance_fix_suggestions"]diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py b/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.pyindex 2a822e2cb..7a27ca17a 100644--- a/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py+++ b/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py@@ -17,13 +17,18 @@ from typing import Any, Literalfrom sqlalchemy.ext.asyncio import AsyncSessionfrom repowise.core.analysis.health.grading import TARGET_SCORE-from repowise.core.analysis.test_reachability import ReachedBy, tests_reaching_by_tier+from repowise.core.analysis.test_reachability import (+ ReachedBy,+ load_test_files,+ tests_matching_by_name,+ tests_reaching_by_tier,+)from .models import RefactoringSuggestionRecommendationView = Literal["canonical", "file_spread"]ValidationBasis = Literal["measured", "inferred", "mixed", "unknown"]-ValidationVia = Literal["coverage", "call-graph", "import-graph", "mixed"]+ValidationVia = Literal["coverage", "call-graph", "import-graph", "name-match", "mixed"]DEFAULT_TEST_LIMIT = 12# Public because the opportunity rank charges the same work and the same@@ -503,12 +508,13 @@ def _priority_components(cost = EFFORT_COST.get(suggestion.effort_bucket, 3.0)provenance = str((suggestion.evidence or {}).get("provenance") or "")weak_graph = 1.0 if provenance in _WEAK_PROVENANCE else 0.0- validation_risk = {- "measured": 0.0,- "mixed": 0.5,- "inferred": 0.75,- "unknown": 1.5,- }[validation.basis]+ # A test named for the file, with no edge proving it runs it, is weaker+ # evidence than a graph walk; price it between inferred and unknown.+ validation_risk = (+ 1.0+ if validation.via == "name-match"+ else {"measured": 0.0, "mixed": 0.5, "inferred": 0.75, "unknown": 1.5}[validation.basis]+ )risk = surface_confidence_risk(surface, suggestion.confidence) + weak_graph + validation_risk# Benefit multiplies rather than offsets: leverage (the host file's# deficit and dependents) scales a real gain, and scales nothing when@@ -652,6 +658,11 @@ async def hydrate_recommendations(if unansweredelse {})+ unreached = sorted(unanswered - inferred.keys())+ if unreached:+ inferred.update(+ tests_matching_by_name(unreached, await load_test_files(session, repository_id))+ )validations = {index: build_validation_plan(suggestion, measured, inferred, test_limit=test_limit)for index, suggestion in enumerate(suggestions)diff --git a/packages/core/src/repowise/core/analysis/health/scoring.py b/packages/core/src/repowise/core/analysis/health/scoring.pyindex ef4b84f16..4d02874c0 100644--- a/packages/core/src/repowise/core/analysis/health/scoring.py+++ b/packages/core/src/repowise/core/analysis/health/scoring.py@@ -262,6 +262,7 @@ _BIOMARKER_DIMENSIONS: dict[str, set[str]] = {"resource_construction_in_loop": {"performance"},"lock_in_loop": {"performance"},"serial_await_in_loop": {"performance"},+ "unbounded_read_reduced_in_memory": {"performance"},"membership_test_against_list_in_loop": {"performance"},# Phase 7b centrality-gated moat markers - performance-only."nested_loop_with_io": {"performance"},@@ -429,6 +430,8 @@ _PERFORMANCE_WEIGHT_MULTIPLIER: dict[str, float] = {# SQL comma-join with no predicate: high-precision by AST shape, advisory# weight pending a corpus spot-check like every new perf marker."sql_cartesian_join": 0.6,+ # Advisory pending a corpus precision gate, like every new perf marker.+ "unbounded_read_reduced_in_memory": 0.4,}# All perf biomarkers share one ``performance`` category, so the single cap@@ -454,6 +457,7 @@ _PERFORMANCE_CATEGORY: dict[str, str] = {"array_spread_in_reduce": "performance","goroutine_in_unbounded_loop": "performance","sql_cartesian_join": "performance",+ "unbounded_read_reduced_in_memory": "performance",}# One bounded performance category cap. 2.0 is a deliberately conservative@@ -488,6 +492,7 @@ _PERFORMANCE_HOME: frozenset[str] = frozenset("array_spread_in_reduce","goroutine_in_unbounded_loop","sql_cartesian_join",+ "unbounded_read_reduced_in_memory",})diff --git a/packages/core/src/repowise/core/analysis/test_reachability.py b/packages/core/src/repowise/core/analysis/test_reachability.pyindex 6ca9f19cd..9fdf8c1c8 100644--- a/packages/core/src/repowise/core/analysis/test_reachability.py+++ b/packages/core/src/repowise/core/analysis/test_reachability.py@@ -162,6 +162,7 @@ from repowise.core.analysis.execution_graph import ()from repowise.core.ingestion.models import EXECUTION_EDGE_TYPES, FILE_DEPENDENCY_EDGE_TYPESfrom repowise.core.persistence.models import GraphNode+from repowise.core.test_paths import paired_test_names# How many call hops a test may take and still be said to reach a file. The walk# saturates here; see the Depth section above.@@ -181,7 +182,8 @@ MAX_TESTS_PER_TARGET = 50# Which tier answered. The CLI prints this so a reader can tell "this test runs# into the file" from the weaker "this test imports it".-ReachedVia = Literal["call-graph", "import-graph"]+# ``name-match`` is weaker still: a test named for the file, with no edge.+ReachedVia = Literal["call-graph", "import-graph", "name-match"]__all__ = ["DEFAULT_CALL_DEPTH",@@ -194,6 +196,7 @@ __all__ = ["call_graph_from_graph","files_reached_by_tests","load_test_files",+ "tests_matching_by_name","tests_reaching","tests_reaching_by_tier",]@@ -574,3 +577,25 @@ async def _edges_into(params,)return [(s, t) for s, t in rows]+++def tests_matching_by_name(targets: list[str], test_files: Collection[str]) -> dict[str, ReachedBy]:+ """Tests named for each target by convention: the fallback when no edge answered.++ An unresolved graph (dynamic client, unindexed framework) leaves+ ``tests/test_x.py`` with no edge to ``x.py``; "unknown" there argues+ against a change a test guards.+ """+ by_name: dict[str, list[str]] = {}+ for path in test_files:+ by_name.setdefault(path.rsplit("/", 1)[-1], []).append(path)+ out: dict[str, ReachedBy] = {}+ for target in targets:+ found = sorted(+ path for name in paired_test_names(target) for path in by_name.get(name, ())+ )+ if found:+ out[target] = ReachedBy(+ found[:MAX_TESTS_PER_TARGET], "name-match", len(found), tuple(found)+ )+ return outdiff --git a/packages/core/src/repowise/core/persistence/crud/analysis/performance.py b/packages/core/src/repowise/core/persistence/crud/analysis/performance.pyindex 88554a9a6..e4a20816c 100644--- a/packages/core/src/repowise/core/persistence/crud/analysis/performance.py+++ b/packages/core/src/repowise/core/persistence/crud/analysis/performance.py@@ -20,7 +20,6 @@ from sqlalchemy import delete, func, select, updatefrom sqlalchemy.ext.asyncio import AsyncSessionfrom ...models import (- HealthFileMetric,HealthFinding,PerformanceOpportunity,PerformanceSummary,@@ -63,13 +62,17 @@ _OBSERVATION_COLUMNS = ()-def opportunity_details(opportunity: OpportunityModel) -> dict[str, Any]:+def opportunity_details(+ opportunity: OpportunityModel, plan: dict[str, Any] | None = None+) -> dict[str, Any]:"""The explanatory half of one opportunity, the half no query filters on.Everything a column already holds is deliberately absent, so the row and- the payload cannot drift into disagreeing about the same fact.+ the payload cannot drift into disagreeing about the same fact. *plan* is+ the stored plan's validation and economics, resolved once at finalize."""return {+ **({"plan": plan} if plan else {}),"biomarker_types": list(opportunity.biomarker_types),"shared_path_suffix": list(opportunity.shared_path_suffix),"resource_fingerprints": list(opportunity.resource_fingerprints),@@ -81,6 +84,7 @@ def opportunity_details(opportunity: OpportunityModel) -> dict[str, Any]:"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,+ "siblings": [dict(entry) for entry in opportunity.siblings],}@@ -90,6 +94,7 @@ def _row_kwargs(position: int,plan_state: str,analyzed_commit: str | None,+ plan: dict[str, Any] | None = None,) -> dict[str, Any]:fix = opportunity.fixreturn {@@ -112,7 +117,7 @@ def _row_kwargs("observations_total": opportunity.observations_total,"affected_call_sites_total": opportunity.affected_call_sites_total,"affected_files_total": opportunity.affected_files_total,- "details_json": json.dumps(opportunity_details(opportunity), separators=(",", ":")),+ "details_json": json.dumps(opportunity_details(opportunity, plan), separators=(",", ":")),"analyzed_commit": analyzed_commit,}@@ -125,7 +130,11 @@ def _intervention_file(opportunity: OpportunityModel) -> str:return opportunity.evidence[0]["file_path"] if opportunity.evidence else ""-def _summary_payload(opportunities: list[OpportunityModel], plan_states: dict[str, str]) -> dict:+def _summary_payload(+ opportunities: list[OpportunityModel],+ plan_states: dict[str, str],+ plans: dict[str, dict[str, Any]] | None = None,+) -> dict:"""The compact current headline, written once and read by primary key."""counts: dict[str, int] = {}contexts: dict[str, int] = {}@@ -159,6 +168,7 @@ def _summary_payload(opportunities: list[OpportunityModel], plan_states: dict[st"why_ranked": [dict(entry) for entry in lead.why_ranked],"prerequisites": list(lead.prerequisites),"actionability_reason": lead.actionability_reason,+ **({"plan": plan} if (plan := (plans or {}).get(lead.opportunity_id)) else {}),},}@@ -197,9 +207,11 @@ async def finalize_performance_opportunities(opportunities = build_performance_opportunities(rows, evidence_limit=_EVIDENCE_LIMIT)await _restamp_findings(session, rows)- plan_states = await _replace_plans(session, repository_id, opportunities, policy)- await _reconcile_opportunities(session, repository_id, opportunities, plan_states, analyzed_commit)- await _write_summary(session, repository_id, opportunities, plan_states, analyzed_commit)+ plan_states, plans = await _replace_plans(session, repository_id, opportunities, policy)+ await _reconcile_opportunities(+ session, repository_id, opportunities, plan_states, analyzed_commit, plans+ )+ await _write_summary(session, repository_id, opportunities, plan_states, analyzed_commit, plans)return len(opportunities)@@ -234,8 +246,8 @@ async def _replace_plans(repository_id: str,opportunities: list[OpportunityModel],policy: PerformancePlanPolicy,-) -> dict[str, str]:- """Write the authoritative plans and report each opportunity's plan state.+) -> tuple[dict[str, str], dict[str, dict[str, Any]]]:+ """Write the authoritative plans; report each opportunity's plan state and facts.Plans are generated here rather than from an analysis report because thereport sees one run's findings while this sees the merged stored set, and a@@ -255,27 +267,45 @@ async def _replace_plans(for item in opportunities}if not policy.enabled:- return states+ return states, {}- nloc_by_file = dict(- (- await session.execute(- select(HealthFileMetric.file_path, HealthFileMetric.nloc).where(- HealthFileMetric.repository_id == repository_id- )- )- ).all()- )suggestions = performance_fix_suggestions(- opportunities,- nloc_by_file=nloc_by_file,- min_confidence=policy.min_confidence,+ opportunities, min_confidence=policy.min_confidence)+ rows = []for suggestion in suggestions:- session.add(RefactoringSuggestion(**_refactoring_row_kwargs(suggestion, repository_id)))+ row = RefactoringSuggestion(**_refactoring_row_kwargs(suggestion, repository_id))+ session.add(row)+ rows.append(row)states[suggestion.plan["opportunity_id"]] = "available"await session.flush()- return states+ return states, await _plan_facts(session, repository_id, rows)+++async def _plan_facts(+ session: AsyncSession, repository_id: str, rows: list[Any]+) -> dict[str, dict[str, Any]]:+ """Validation and economics per plan, resolved once here rather than per read.++ Serving one plan cannot afford the test-reachability walk, and without this+ it fell back to an empty profile that read as "no tests" for every plan.+ """+ from ....analysis.health.refactoring.recommendations import hydrate_recommendations++ if not rows:+ return {}+ facts: dict[str, dict[str, Any]] = {}+ for recommendation in await hydrate_recommendations(session, repository_id, rows):+ suggestion = recommendation.suggestion+ facts[suggestion.plan["opportunity_id"]] = {+ "steps": (suggestion.plan or {}).get("steps", []),+ "effort_bucket": suggestion.effort_bucket,+ "benefit": recommendation.benefit,+ "cost": recommendation.cost,+ "risk": recommendation.risk,+ "validation": recommendation.validation.as_dict(),+ }+ return factsasync def _reconcile_opportunities(@@ -284,6 +314,7 @@ async def _reconcile_opportunities(opportunities: list[OpportunityModel],plan_states: dict[str, str],analyzed_commit: str | None,+ plans: dict[str, dict[str, Any]] | None = None,) -> None:"""Update, insert, and resolve, in one pass over the stored rows."""stored = {@@ -308,6 +339,7 @@ async def _reconcile_opportunities(position=position,plan_state=plan_states.get(item.opportunity_id, "no_safe_plan"),analyzed_commit=analyzed_commit,+ plan=(plans or {}).get(item.opportunity_id),)row = stored.get(key)if row is None:@@ -336,11 +368,14 @@ async def _write_summary(opportunities: list[OpportunityModel],plan_states: dict[str, str],analyzed_commit: str | None,+ plans: dict[str, dict[str, Any]] | None = None,) -> None:from ....analysis.health.perf.opportunities import PERFORMANCE_MODEL_VERSION- payload = json.dumps(_summary_payload(opportunities, plan_states), separators=(",", ":"))+ payload = json.dumps(+ _summary_payload(opportunities, plan_states, plans), separators=(",", ":")+ )row = await session.get(PerformanceSummary, repository_id)if row is None:row = PerformanceSummary(repository_id=repository_id)diff --git a/packages/core/src/repowise/core/test_paths.py b/packages/core/src/repowise/core/test_paths.pyindex e1faba020..9332f16c5 100644--- a/packages/core/src/repowise/core/test_paths.py+++ b/packages/core/src/repowise/core/test_paths.py@@ -43,7 +43,7 @@ from __future__ import annotationsimport refrom dataclasses import dataclassfrom functools import cache-from pathlib import PurePosixPath+from pathlib import PurePath, PurePosixPath# Directory segments that mark every file beneath them as test material,# whatever the filename. ``__test__`` is the Jest variant of ``__tests__``.@@ -310,3 +310,32 @@ def is_test_to_production_pair(against another file, with no node to read a stored flag from."""return is_test_related_path(code_path, code_language) ^ is_test_related_path(partner_path)+++_PASCAL_UNIT_SUFFIXES = frozenset({".pas", ".pp", ".dpr", ".dpk", ".lpr"})+++def paired_test_names(rel_path: str) -> frozenset[str]:+ """Filenames a test for *rel_path* would conventionally carry, any directory."""+ p = PurePath(rel_path)+ stem = p.stem+ test_suffix = ".exs" if p.suffix == ".ex" else p.suffix+ names = {+ f"test_{stem}{test_suffix}",+ f"{stem}_test{test_suffix}",+ f"{stem}_spec{test_suffix}",+ f"{stem}.test.ts",+ f"{stem}.test.tsx",+ f"{stem}.test.js",+ f"{stem}.test.mts",+ f"{stem}.test.cts",+ f"{stem}.spec.ts",+ f"{stem}.spec.js",+ f"{stem}.spec.mts",+ f"{stem}.spec.cts",+ }+ if p.suffix.lower() in _PASCAL_UNIT_SUFFIXES:+ # Delphi pairs ``uFoo.pas`` with a ``TestFoo.dpr`` program; only a+ # lowercase ``u`` is the unit prefix (``Utils.pas`` keeps its U).+ names.add(f"Test{stem[1:] if stem[:1] == 'u' else stem}.dpr")+ return frozenset(names)diff --git a/packages/server/src/repowise/server/mcp_server/tool_health.py b/packages/server/src/repowise/server/mcp_server/tool_health.pyindex 6c1afc0be..901aad1ae 100644--- a/packages/server/src/repowise/server/mcp_server/tool_health.py+++ b/packages/server/src/repowise/server/mcp_server/tool_health.py@@ -1585,7 +1585,7 @@ async def get_health(refactoring_view: ``diversified`` (default)|``canonical``|``file_spread``; _type/_confidence/_effort filter.performance_view/_context/_boundary/_confidence/_sort: queue- projection and filters; the facets list them.+ projection and filters; a rejected value lists the accepted.scope / counts: default ``all``/``everything``. ``production`` dropstest files; ``code_shape`` drops the git-derived half of thescore and its findings.diff --git a/packages/server/src/repowise/server/services/performance_health.py b/packages/server/src/repowise/server/services/performance_health.pyindex c29a80ad9..3065c236e 100644--- a/packages/server/src/repowise/server/services/performance_health.py+++ b/packages/server/src/repowise/server/services/performance_health.py@@ -171,7 +171,8 @@ def parse_query(return Noneif value in allowed:return value- ignored[name] = value+ # Name the vocabulary, so a rejected value is recoverable from the reply.+ ignored[name] = f"{value} (accepted: {', '.join(allowed)})"return Noneresolved_context: PerformanceContext = DEFAULT_CONTEXT@@ -181,7 +182,9 @@ def parse_query(else:# Named, so the caller learns the value was not understood, then# treated as absent like every other unrecognized filter.- ignored["performance_context"] = context+ ignored["performance_context"] = (+ f"{context} (accepted: {', '.join((*CANONICAL_CONTEXTS, 'all'))})"+ )return (PerformanceQuery(context=resolved_context,@@ -432,6 +435,7 @@ class PerformanceHealthService:"plan_state": lead["plan_state"],"plan_reason": _PLAN_REASONS[lead["plan_state"]],"prerequisites": lead["prerequisites"],+ **_plan_brief(lead.get("plan"), economics=False),"next_action": {"tool": "get_health","arguments": {"opportunity_id": lead["opportunity_id"]},@@ -539,6 +543,8 @@ class PerformanceHealthService:"actionability_reason": details.get("actionability_reason"),"prerequisites": details.get("prerequisites", []),"rank_factors": details.get("rank_factors", {}),+ "siblings": details.get("siblings", []),+ **_plan_brief(details.get("plan")),"fix": Noneif fix_strategy is Noneelse {@@ -632,6 +638,21 @@ def evidence_block(return block+def _plan_brief(plan: dict[str, Any] | None, *, economics: bool = True) -> dict[str, Any]:+ """The stored plan's validation (and steps, economics), or nothing on an older store."""+ if not plan:+ return {}+ validation = plan.get("validation") or {}+ keys = ("basis", "via", "total", "tests", "commands") if economics else ("basis", "via", "total")+ brief: dict[str, Any] = {"validation": {key: validation.get(key) for key in keys}}+ if economics:+ brief["plan_steps"] = plan.get("steps", [])+ brief["plan_economics"] = {+ key: plan.get(key) for key in ("effort_bucket", "benefit", "cost", "risk")+ }+ return brief++def _title(lead: dict[str, Any]) -> str:"""A human cause, not a marker name."""symbol = lead.get("intervention_symbol") or lead.get("terminal_sink") or lead["file_path"]diff --git a/packages/server/src/repowise/server/services/refactoring_health.py b/packages/server/src/repowise/server/services/refactoring_health.pyindex 7489feb11..ae584eddb 100644--- a/packages/server/src/repowise/server/services/refactoring_health.py+++ b/packages/server/src/repowise/server/services/refactoring_health.py@@ -423,7 +423,10 @@ class RefactoringHealthService:[rehydrate_suggestion(row)],metric_by_path=metric_by_path,centrality=centrality,- validations={0: _stored_validation(owner, row.public_id)},+ validations={+ 0: _stored_validation(owner, row.public_id)+ or await self._performance_validation(row)+ },)payload = built[0].as_dict() if built else _plan_payload(row)payload["id"] = row.public_id or row.id@@ -440,6 +443,27 @@ class RefactoringHealthService:}return result+ async def _performance_validation(self, row: Any) -> Any:+ """A performance plan's profile, stored on its opportunity at finalize.++ Performance plans are never refactoring steps, so the step lookup above+ cannot find them, and an empty fallback reports every one as untested.+ """+ from repowise.core.persistence.crud.analysis.performance import (+ get_performance_opportunity,+ )++ if row.refactoring_type != "performance_fix":+ return None+ opportunity_id = _loads(row.plan_json).get("opportunity_id")+ if not opportunity_id:+ return None+ owner = await get_performance_opportunity(+ self._session, self._repository_id, opportunity_id+ )+ profile = (_loads(owner.details_json).get("plan") or {}).get("validation") if owner else None+ return _validation_from_profile(profile) if profile else None+async def _rank_inputs(self, file_path: str) -> tuple[dict[str, Any], dict[str, float]]:"""The two rank inputs for one file, as seeks rather than repo reads.@@ -613,11 +637,6 @@ def _stored_validation(owner: Any, public_id: str | None) -> Any:Test reachability is a graph walk over the whole unanswered set; it belongsat index time, and this reads its result rather than repeating it."""- from repowise.core.analysis.health.refactoring.recommendations import (- ValidationPlan,- ValidationTarget,- )-if owner is None or not public_id:return Nonedetails = _loads(owner.details_json)@@ -635,8 +654,16 @@ def _stored_validation(owner: Any, public_id: str | None) -> Any:(p for p in (details.get("validation_profiles") or []) if p.get("id") == wanted),None,)- if profile is None:- return None+ return _validation_from_profile(profile) if profile is not None else None+++def _validation_from_profile(profile: dict[str, Any]) -> Any:+ """Rebuild a stored validation profile into the plan dataclass."""+ from repowise.core.analysis.health.refactoring.recommendations import (+ ValidationPlan,+ ValidationTarget,+ )+target_fields = set(ValidationTarget.__dataclass_fields__)values = {key: valuediff --git a/packages/types/src/health.ts b/packages/types/src/health.tsindex add68e3b4..22e8c4a7a 100644--- a/packages/types/src/health.ts+++ b/packages/types/src/health.ts@@ -17,6 +17,7 @@import type { C4IoKind } from "./external-systems.js";import type { Paginated } from "./pagination.js";+import type { StepClassification, ValidationBasis, ValidationVia } from "./refactoring.js";/** Finding severity used across the health surface. */export type HealthSeverity = "low" | "medium" | "high" | "critical";@@ -367,6 +368,47 @@ export interface PerformanceOpportunityFacets {change_risk: string;}+/**+ * Another cause observed on the same source lines. `relation` is from this+ * opportunity's own view: `preferred` means the sibling's fix is the+ * stronger one, `alternative` means this one is, `same_site` is no+ * preference either way.+ */+export interface PerformanceOpportunitySibling {+ opportunity_id: string;+ biomarker_type: string;+ strategy: string | null;+ relation: "preferred" | "alternative" | "same_site";+}++/** How to validate a stored plan. Shares its vocabulary with refactoring's+ * `RecommendationValidation`, trimmed to the fields the queue materializes. */+export interface PerformanceOpportunityValidation {+ basis: ValidationBasis;+ via: ValidationVia | null;+ total: number;+ tests: string[];+ commands: string[];+}++/** One ordered edit in a stored plan. */+export interface PerformanceOpportunityPlanStep {+ order: number;+ action: string;+ symbol: string | null;+ file_path: string | null;+ line: number | null;+ applicability: StepClassification;+}++/** Ranking inputs behind a plan, not a cost/benefit ledger. */+export interface PerformanceOpportunityPlanEconomics {+ effort_bucket: string;+ benefit: number;+ cost: number;+ risk: number;+}+export interface PerformanceOpportunity {opportunity_id: string;/** Ids are stable within a model version and never translated across one. */@@ -407,6 +449,15 @@ export interface PerformanceOpportunity {plan_id: string | null;plan_status: PerformancePlanStatus;plan_reason: string;+ /** Other causes flagged on the same lines. Always present (may be empty) on+ * new stores; absent on payloads from an older store. */+ siblings?: PerformanceOpportunitySibling[];+ /** How to validate the stored plan. Absent when there is no stored plan. */+ validation?: PerformanceOpportunityValidation;+ /** Ordered edits for the stored plan. Absent when there is no stored plan. */+ plan_steps?: PerformanceOpportunityPlanStep[];+ /** Ranking inputs behind the stored plan, not a verdict. */+ plan_economics?: PerformanceOpportunityPlanEconomics;}/** Whether a quoted id still names something this index can resolve. */diff --git a/packages/types/src/refactoring.ts b/packages/types/src/refactoring.tsindex 982525224..74e614fe5 100644--- a/packages/types/src/refactoring.ts+++ b/packages/types/src/refactoring.ts@@ -14,7 +14,7 @@ export type RefactoringType =export type EffortBucket = "S" | "M" | "L" | "XL";export type Confidence = "low" | "medium" | "high";export type ValidationBasis = "measured" | "inferred" | "mixed" | "unknown";-export type ValidationVia = "coverage" | "call-graph" | "import-graph" | "mixed";+export type ValidationVia = "coverage" | "call-graph" | "import-graph" | "name-match" | "mixed";export interface RecommendationValidationTarget {file_path: string;diff --git a/packages/ui/__tests__/health/fixtures/performance.ts b/packages/ui/__tests__/health/fixtures/performance.tsindex 18ed7dc1d..6f06a460d 100644--- a/packages/ui/__tests__/health/fixtures/performance.ts+++ b/packages/ui/__tests__/health/fixtures/performance.ts@@ -3,6 +3,7 @@ import type {PerformanceOpportunity,PerformanceOpportunityDetail,PerformanceOpportunityPage,+ PerformanceOpportunitySibling,} from "@repowise-dev/types/health";import type { PerformanceViewAdapter } from "../../../src/health/performance/adapter";@@ -15,6 +16,18 @@ import type { PerformanceViewAdapter } from "../../../src/health/performance/ada* and a live index only offers whatever it happens to hold.*/+export function sibling(+ overrides: Partial<PerformanceOpportunitySibling> = {},+): PerformanceOpportunitySibling {+ return {+ opportunity_id: "perf2_sibling",+ biomarker_type: "nested_loop_with_io",+ strategy: "batch_or_prefetch_io",+ relation: "preferred",+ ...overrides,+ };+}+export function opportunity(overrides: Partial<PerformanceOpportunity> = {},): PerformanceOpportunity {diff --git a/packages/ui/__tests__/health/performance-presentation.test.ts b/packages/ui/__tests__/health/performance-presentation.test.tsindex beead6de3..05e5ccbe5 100644--- a/packages/ui/__tests__/health/performance-presentation.test.ts+++ b/packages/ui/__tests__/health/performance-presentation.test.ts@@ -6,9 +6,11 @@ import {agentHandoffCall,boundaryLabel,facetValueLabel,+ humanizeToken,opportunityEvidenceLine,opportunityTitle,planPresentation,+ siblingFixLabel,whyRankedLabel,} from "../../src/health/performance/presentation";import { contiguousSections } from "../../src/health/performance/queue";@@ -95,6 +97,33 @@ describe("performance presentation", () => {it("quotes the opportunity id in the agent drill-down", () => {expect(agentHandoffCall("perf2_abc")).toBe('get_health(opportunity_id="perf2_abc")');});++ it("prefers a sibling's strategy over its biomarker for the fix label", () => {+ expect(+ siblingFixLabel({+ opportunity_id: "perf2_sib",+ biomarker_type: "nested_loop_with_io",+ strategy: "batch_or_prefetch_io",+ relation: "preferred",+ }),+ ).toBe("Batch or prefetch io");+ expect(+ siblingFixLabel({+ opportunity_id: "perf2_sib",+ biomarker_type: "nested_loop_with_io",+ strategy: null,+ relation: "same_site",+ }),+ ).toBe("Nested loop with io");+ });++ it("reads a new actionability reason and prerequisite tokens honestly", () => {+ expect(humanizeToken("loop_already_chunked")).toBe("Loop already chunked");+ expect(humanizeToken("bounded_concurrency")).toBe("Bounded concurrency");+ expect(humanizeToken("query_supports_group_selection")).toBe(+ "Query supports group selection",+ );+ });});describe("queue sections", () => {diff --git a/packages/ui/__tests__/health/performance-view.test.tsx b/packages/ui/__tests__/health/performance-view.test.tsxindex c9eb1b14f..d9ddd48a6 100644--- a/packages/ui/__tests__/health/performance-view.test.tsx+++ b/packages/ui/__tests__/health/performance-view.test.tsx@@ -3,7 +3,14 @@ import { beforeAll, describe, expect, it, vi } from "vitest";import type { RefactoringPlan } from "@repowise-dev/types/refactoring";import { PerformanceView } from "../../src/health/performance-view";-import { adapter, legacyPage, opportunity, page, resolvedDetail } from "./fixtures/performance";+import {+ adapter,+ legacyPage,+ opportunity,+ page,+ resolvedDetail,+ sibling,+} from "./fixtures/performance";beforeAll(() => {Element.prototype.scrollIntoView = vi.fn();@@ -432,6 +439,228 @@ describe("PerformanceView drawer", () => {});});+describe("PerformanceView drawer siblings", () => {+ it("names a preferred sibling's fix as the stronger one", async () => {+ const withSiblings = page({+ items: [+ opportunity({+ opportunity_id: "perf2_planready",+ siblings: [+ sibling({+ opportunity_id: "perf2_preferred",+ relation: "preferred",+ strategy: "batch_or_prefetch_io",+ }),+ sibling({+ opportunity_id: "perf2_alt",+ relation: "alternative",+ biomarker_type: "serial_await_in_loop",+ strategy: null,+ }),+ ],+ }),+ ],+ });+ render(+ <PerformanceView+ adapter={adapter({ getPerformanceOpportunities: async () => withSiblings })}+ />,+ );+ await openFirstRow();+ const panel = await screen.findByRole("dialog");+ expect(within(panel).getByText(/Batch or prefetch io is the stronger fix here/)).toBeTruthy();+ expect(within(panel).getByText(/Also flagged on these lines/)).toBeTruthy();+ expect(within(panel).getByRole("button", { name: "Serial await in loop" })).toBeTruthy();+ });++ it("opens a sibling cause from the drawer, the same way a shared link would", async () => {+ const withSiblings = page({+ items: [+ opportunity({+ opportunity_id: "perf2_planready",+ siblings: [+ sibling({+ opportunity_id: "perf2_alt",+ relation: "same_site",+ biomarker_type: "serial_await_in_loop",+ strategy: null,+ }),+ ],+ }),+ ],+ });+ const getDetail = vi.fn(async (id: string) =>+ resolvedDetail({+ opportunity_id: id,+ siblings: id === "perf2_planready" ? (withSiblings.items[0]!.siblings ?? []) : [],+ }),+ );+ const onOpenOpportunityChange = vi.fn();+ render(+ <PerformanceView+ adapter={adapter({+ getPerformanceOpportunities: async () => withSiblings,+ getPerformanceOpportunity: getDetail,+ })}+ onOpenOpportunityChange={onOpenOpportunityChange}+ />,+ );+ await openFirstRow();+ const panel = await screen.findByRole("dialog");+ await waitFor(() =>+ expect(within(panel).getByRole("button", { name: "Serial await in loop" })).toBeTruthy(),+ );+ fireEvent.click(within(panel).getByRole("button", { name: "Serial await in loop" }));+ await waitFor(() =>+ expect(getDetail).toHaveBeenCalledWith("perf2_alt", { evidenceLimit: 8 }),+ );+ expect(onOpenOpportunityChange).toHaveBeenLastCalledWith("perf2_alt");+ });++ it("renders no siblings note when the opportunity carries none", async () => {+ render(<PerformanceView adapter={adapter()} />);+ await openFirstRow();+ const panel = await screen.findByRole("dialog");+ expect(within(panel).queryByText(/Also flagged on these lines/)).toBeNull();+ expect(within(panel).queryByText(/is the stronger fix here/)).toBeNull();+ });+});++describe("PerformanceView drawer plan steps and validation", () => {+ it("renders ordered plan steps and marks only the mechanical ones", async () => {+ const withSteps = page({+ items: [+ opportunity({+ opportunity_id: "perf2_planready",+ plan_steps: [+ {+ order: 2,+ action: "Batch the second call",+ symbol: "src/b.py::run",+ file_path: "src/b.py",+ line: 20,+ applicability: "judgment",+ },+ {+ order: 1,+ action: "Batch the first call",+ symbol: "src/a.py::run",+ file_path: "src/a.py",+ line: 10,+ applicability: "mechanical",+ },+ ],+ }),+ ],+ });+ render(+ <PerformanceView adapter={adapter({ getPerformanceOpportunities: async () => withSteps })} />,+ );+ await openFirstRow();+ const panel = await screen.findByRole("dialog");+ const firstStepText = within(panel).getByText(/Batch the first call/).closest("li");+ const secondStepText = within(panel).getByText(/Batch the second call/).closest("li");+ expect(firstStepText?.textContent).toContain("mechanical");+ expect(secondStepText?.textContent).not.toContain("mechanical");+ const ol = firstStepText?.closest("ol");+ const liTexts = Array.from(ol?.querySelectorAll("li") ?? []).map((li) => li.textContent ?? "");+ expect(liTexts[0]).toContain("Batch the first call");+ expect(liTexts[1]).toContain("Batch the second call");+ });++ it("renders the stored validation through the shared validation summary", async () => {+ const withValidation = page({+ items: [+ opportunity({+ opportunity_id: "perf2_planready",+ validation: {+ basis: "measured",+ via: "call-graph",+ total: 2,+ tests: ["tests/test_a.py"],+ commands: ["pytest tests/test_a.py"],+ },+ }),+ ],+ });+ render(+ <PerformanceView+ adapter={adapter({ getPerformanceOpportunities: async () => withValidation })}+ />,+ );+ await openFirstRow();+ const panel = await screen.findByRole("dialog");+ expect(within(panel).getByText(/Measured coverage/)).toBeTruthy();+ expect(within(panel).getByText(/via call graph/)).toBeTruthy();+ expect(within(panel).getByText("tests/test_a.py")).toBeTruthy();+ });++ it("reads a name-match validation honestly", async () => {+ const withValidation = page({+ items: [+ opportunity({+ opportunity_id: "perf2_planready",+ validation: {+ basis: "inferred",+ via: "name-match",+ total: 1,+ tests: ["tests/test_b.py"],+ commands: [],+ },+ }),+ ],+ });+ render(+ <PerformanceView+ adapter={adapter({ getPerformanceOpportunities: async () => withValidation })}+ />,+ );+ await openFirstRow();+ const panel = await screen.findByRole("dialog");+ expect(within(panel).getByText(/via name match/)).toBeTruthy();+ });++ it("says validation is unknown honestly rather than silently skipping it", async () => {+ const withValidation = page({+ items: [+ opportunity({+ opportunity_id: "perf2_planready",+ validation: { basis: "unknown", via: null, total: 0, tests: [], commands: [] },+ }),+ ],+ });+ render(+ <PerformanceView+ adapter={adapter({ getPerformanceOpportunities: async () => withValidation })}+ />,+ );+ await openFirstRow();+ const panel = await screen.findByRole("dialog");+ expect(within(panel).getByText("Validation gap")).toBeTruthy();+ expect(within(panel).getByText(/Treat this as explicit validation work/)).toBeTruthy();+ });++ it("renders no validation block when the opportunity carries none", async () => {+ render(<PerformanceView adapter={adapter()} />);+ await openFirstRow();+ const panel = await screen.findByRole("dialog");+ expect(within(panel).queryByText(/guarding test/)).toBeNull();+ expect(within(panel).queryByText(/Validation gap/)).toBeNull();+ });+});++describe("PerformanceView sort control", () => {+ it("asks the server to sort by the chosen vocabulary", async () => {+ const load = vi.fn(async () => page());+ render(<PerformanceView adapter={adapter({ getPerformanceOpportunities: load })} />);+ await rows();+ fireEvent.change(screen.getByLabelText("Sort"), { target: { value: "leverage" } });+ await waitFor(() =>+ expect(load).toHaveBeenLastCalledWith(expect.objectContaining({ sort: "leverage" })),+ );+ });+});+describe("PerformanceView compatibility", () => {it("collapses production and tooling only when the server predates the vocabulary", async () => {const load = vi.fn(async () => legacyPage());diff --git a/packages/ui/src/health/biomarker-glossary.ts b/packages/ui/src/health/biomarker-glossary.tsindex 8822b216a..b6d343a4c 100644--- a/packages/ui/src/health/biomarker-glossary.ts+++ b/packages/ui/src/health/biomarker-glossary.ts@@ -290,7 +290,13 @@ export const BIOMARKER_GLOSSARY: Record<string, BiomarkerInfo> = {label: "Serial await in loop",category: "performance",description:- "An awaited I/O round-trip run one-at-a-time inside a loop. When the iterations are independent, fan them out with gather / Promise.all / Task.WhenAll for concurrent execution. Advisory — a static analyzer cannot prove the iterations are independent.",+ "An awaited I/O round-trip run one-at-a-time inside a loop. When the iterations are independent, fan them out with gather / Promise.all / Task.WhenAll for concurrent execution. Advisory: independence may be unproven, and against a database or network client a fan-out also needs a bound on concurrency.",+ },+ unbounded_read_reduced_in_memory: {+ label: "Unbounded read reduced in memory",+ category: "performance",+ description:+ "A database read with no limit/range/single bound, run once, whose result a loop then dedups down to one row per key (setdefault, a seen-set, a not-in guard). The table can grow without bound while the code still pays to transfer and decode every row. Move the selection into the query: DISTINCT ON, a window function, or a view.",},membership_test_against_list_in_loop: {label: "List membership in loop",@@ -429,6 +435,7 @@ export const PERFORMANCE_HOME_BIOMARKERS: ReadonlySet<string> = new Set(["array_spread_in_reduce","goroutine_in_unbounded_loop","sql_cartesian_join",+ "unbounded_read_reduced_in_memory",]);/**diff --git a/packages/ui/src/health/performance-view.tsx b/packages/ui/src/health/performance-view.tsxindex d60a64968..250b4549c 100644--- a/packages/ui/src/health/performance-view.tsx+++ b/packages/ui/src/health/performance-view.tsx@@ -21,7 +21,13 @@ import {import type { PerformanceViewAdapter } from "./performance/adapter";import { capabilitiesOf } from "./performance/capabilities";import { OpportunityDrawer } from "./performance/drawer";-import { ContextHint, ContextTabs, QueueFilters, ScopeLine } from "./performance/filters";+import {+ ContextHint,+ ContextTabs,+ QueueFilters,+ ScopeLine,+ SortControl,+} from "./performance/filters";import { LegacyPerformanceFindings } from "./performance/legacy";import { OpportunityQueue } from "./performance/queue";import {@@ -86,8 +92,19 @@ export function PerformanceView({const [selected, setSelected] = useState<PerformanceOpportunity | null>(null);const select = (next: PerformanceOpportunity | null) => {setSelected(next);+ setInternalOpenId(null);onOpenOpportunityChange?.(next?.opportunity_id ?? null);};+ // A sibling link inside the drawer opens another opportunity by id, the+ // same way a shared link does. The host may not feed `openOpportunityId`+ // back synchronously (or at all, if it does not track the URL), so this+ // state carries the request until the fetch below resolves it.+ const [internalOpenId, setInternalOpenId] = useState<string | null>(null);+ const openById = (opportunityId: string) => {+ setInternalOpenId(opportunityId);+ onOpenOpportunityChange?.(opportunityId);+ };+ const effectiveOpenId = openOpportunityId ?? internalOpenId;// The handoff carries the verified plan when the drawer proved one, so a// plan-ready row hands over the ready payload rather than an instruction to// re-derive it.@@ -126,9 +143,7 @@ export function PerformanceView({// them. The row shape and the detail shape share their fields, so what comes// back drives the same drawer a click does.const pendingId =- openOpportunityId && openOpportunityId !== selected?.opportunity_id- ? openOpportunityId- : null;+ effectiveOpenId && effectiveOpenId !== selected?.opportunity_id ? effectiveOpenId : null;const { data: linked, error: linkError } = useSWR<PerformanceOpportunityDetail>(pendingId && adapter.getPerformanceOpportunity? `performance-opportunity-link:${adapter.cacheKey}:${pendingId}`@@ -238,6 +253,11 @@ export function PerformanceView({/><ContextHint context={filters.context} />+ <SortControl+ value={filters.sort}+ onChange={(sort) => apply(withFilter(filters, "sort", sort))}+ />+{capabilities.serverFacets ? (<QueueFiltersfilters={filters}@@ -307,6 +327,7 @@ export function PerformanceView({planEnabled={capabilities.planById}onClose={() => select(null)}onAgentHandoff={(opportunity, plan) => setPromptFor({ opportunity, plan })}+ onSelectById={openById}/><AiPromptModaldiff --git a/packages/ui/src/health/performance/drawer.tsx b/packages/ui/src/health/performance/drawer.tsxindex 8b32bb012..31afde20d 100644--- a/packages/ui/src/health/performance/drawer.tsx+++ b/packages/ui/src/health/performance/drawer.tsx@@ -5,13 +5,17 @@ import { Sparkles } from "lucide-react";import type {PerformanceOpportunity,PerformanceOpportunityDetail,+ PerformanceOpportunityPlanStep,+ PerformanceOpportunitySibling,+ PerformanceOpportunityValidation,PerformanceModelState,} from "@repowise-dev/types/health";-import type { RefactoringPlan } from "@repowise-dev/types/refactoring";+import type { RecommendationValidation, RefactoringPlan } from "@repowise-dev/types/refactoring";import { AdaptivePanel } from "../../shared/adaptive-panel";import { ProvenancePathList } from "../../shared/provenance-path-list";import { performancePlanDetail } from "../../refactoring/types";+import { ValidationSummary } from "../../refactoring/validation-summary";import type { PerformanceViewAdapter } from "./adapter";import { RawObservations } from "./evidence";import {@@ -25,9 +29,29 @@ import {opportunityEvidenceLine,opportunityTitle,planPresentation,+ siblingFixLabel,whyRankedLabel,} from "./presentation";+/** The queue's small validation shape, adapted onto the refactoring surface's+ * reader rather than duplicated. The fields it doesn't materialize (per-target+ * breakdown, affected paths) read as empty, not missing. */+function toRecommendationValidation(+ validation: PerformanceOpportunityValidation,+): RecommendationValidation {+ return {+ basis: validation.basis,+ via: validation.via,+ total: validation.total,+ tests: validation.tests,+ truncated: false,+ affected_files: [],+ affected_symbols: [],+ commands: validation.commands,+ targets: [],+ };+}+/*** One opportunity in depth.*@@ -115,6 +139,95 @@ function useVerifiedPlan(return { planId, data, error, isLoading, matches, verified: matches ? data! : null };}+/** One sibling's fix, as a link when the drawer can open another opportunity,+ * plain text otherwise (an agent-facing or read-only host, say). */+function SiblingLink({+ sibling,+ label,+ onSelect,+}: {+ sibling: PerformanceOpportunitySibling;+ label: string;+ onSelect?: ((opportunityId: string) => void) | undefined;+}) {+ if (!onSelect) {+ return <span className="font-medium text-[var(--color-text-secondary)]">{label}</span>;+ }+ return (+ <button+ type="button"+ onClick={() => onSelect(sibling.opportunity_id)}+ className="rounded font-medium text-[var(--color-accent-primary)] underline-offset-2 hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent-primary)]"+ >+ {label}+ </button>+ );+}++/** Other causes on the same lines, one quiet line. A `preferred` sibling says+ * plainly that its fix is the stronger one; the rest are just named. */+function SiblingsNote({+ siblings,+ onSelect,+}: {+ siblings: PerformanceOpportunitySibling[];+ onSelect?: ((opportunityId: string) => void) | undefined;+}) {+ const preferred = siblings.filter((sibling) => sibling.relation === "preferred");+ const rest = siblings.filter((sibling) => sibling.relation !== "preferred");+ return (+ <p className="mt-3 text-xs text-[var(--color-text-tertiary)]">+ {preferred.map((sibling) => (+ <span key={sibling.opportunity_id} className="block">+ {siblingFixLabel(sibling)} is the stronger fix here; see{" "}+ <SiblingLink sibling={sibling} label="this cause" onSelect={onSelect} />.+ </span>+ ))}+ {rest.length > 0 ? (+ <span className="block">+ Also flagged on these lines:{" "}+ {rest.map((sibling, index) => (+ <span key={sibling.opportunity_id}>+ {index > 0 ? ", " : ""}+ <SiblingLink+ sibling={sibling}+ label={humanizeToken(sibling.biomarker_type)}+ onSelect={onSelect}+ />+ </span>+ ))}+ .+ </span>+ ) : null}+ </p>+ );+}++/** The plan's edits in order; only the rarer mechanical ones are marked. */+function PlanSteps({ steps }: { steps: PerformanceOpportunityPlanStep[] }) {+ return (+ <ol className="mt-3 space-y-1.5">+ {[...steps]+ .sort((a, b) => a.order - b.order)+ .map((step) => (+ <li key={step.order} className="text-sm text-[var(--color-text-secondary)]">+ <span className="tabular-nums text-[var(--color-text-tertiary)]">{step.order}.</span>{" "}+ {step.action}+ {step.applicability === "mechanical" ? (+ <span className="ml-1.5 text-xs text-[var(--color-text-tertiary)]">mechanical</span>+ ) : null}+ {step.file_path ? (+ <span className="ml-1.5 break-all font-mono text-xs text-[var(--color-text-tertiary)]">+ {step.file_path}+ {step.line ? `:${step.line}` : ""}+ </span>+ ) : null}+ </li>+ ))}+ </ol>+ );+}+function PlanSection({opportunity,adapter,@@ -132,6 +245,7 @@ function PlanSection({planId && matches? adapter.refactoringPlanHref?.(planId, opportunity.opportunity_id): undefined;+ const steps = opportunity.plan_steps;return (<Section title="Plan">@@ -143,6 +257,15 @@ function PlanSection({{opportunity.fix.rationale}</p>) : null}+ {steps && steps.length > 0 ? <PlanSteps steps={steps} /> : null}+ {opportunity.validation ? (+ <div className="mt-3">+ <ValidationSummary+ validation={toRecommendationValidation(opportunity.validation)}+ fileHref={adapter.fileHref}+ />+ </div>+ ) : null}{planId && !enabled ? (<p className="mt-2 text-xs text-[var(--color-text-tertiary)]">This host cannot open the stored plan. Its id is{" "}@@ -182,6 +305,7 @@ export function OpportunityDrawer({planEnabled,onClose,onAgentHandoff,+ onSelectById,}: {/** The row that was inspected, held so the panel can render before the fetch. */opportunity: PerformanceOpportunity | null;@@ -190,6 +314,8 @@ export function OpportunityDrawer({planEnabled: boolean;onClose: () => void;onAgentHandoff: (opportunity: PerformanceOpportunity, plan: RefactoringPlan | null) => void;+ /** Open a sibling cause by id, replacing the drawer's contents in place. */+ onSelectById?: ((opportunityId: string) => void) | undefined;}) {const id = opportunity?.opportunity_id ?? null;const { data: detail } = useSWR<PerformanceOpportunityDetail>(@@ -261,6 +387,9 @@ export function OpportunityDrawer({<Field label="Leverage" value={humanizeToken(current.facets.leverage)} /><Field label="Change risk" value={humanizeToken(current.facets.change_risk)} /></dl>+ {current.siblings && current.siblings.length > 0 ? (+ <SiblingsNote siblings={current.siblings} onSelect={onSelectById} />+ ) : null}</Section><Section title="Whether it can be changed">diff --git a/packages/ui/src/health/performance/filters.tsx b/packages/ui/src/health/performance/filters.tsxindex 48a6c92c9..137908a71 100644--- a/packages/ui/src/health/performance/filters.tsx+++ b/packages/ui/src/health/performance/filters.tsx@@ -21,6 +21,7 @@ import {narrowingCount,type NarrowingKey,type PerformanceFilterState,+ type PerformanceSort,} from "./query";/**@@ -204,6 +205,31 @@ export function ScopeLine({);}+const SORT_OPTIONS: { value: PerformanceSort; label: string }[] = [+ { value: "rank", label: "Rank" },+ { value: "leverage", label: "Call sites" },+ { value: "observations", label: "Observations" },+];++/** How the queue is ordered. A separate axis from the narrowing filters, so+ * it gets its own control rather than sharing theirs. */+export function SortControl({+ value,+ onChange,+}: {+ value: PerformanceSort;+ onChange: (value: PerformanceSort) => void;+}) {+ return (+ <FilterSelect+ label="Sort"+ value={value}+ onChange={(next) => onChange(next as PerformanceSort)}+ options={SORT_OPTIONS}+ />+ );+}+export function ContextHint({ context }: { context: PerformanceContextFilter }) {if (context === "all") return null;return (diff --git a/packages/ui/src/health/performance/presentation.ts b/packages/ui/src/health/performance/presentation.tsindex f1bf664b2..feb7ae8c1 100644--- a/packages/ui/src/health/performance/presentation.ts+++ b/packages/ui/src/health/performance/presentation.ts@@ -5,6 +5,7 @@ import {type PerformanceFacetKey,type PerformanceOpportunity,type PerformanceOpportunityConfidence,+ type PerformanceOpportunitySibling,type PerformanceWhyRanked,} from "@repowise-dev/types/health";import type { C4IoKind } from "@repowise-dev/types/external-systems";@@ -205,3 +206,8 @@ export function planPresentation(opportunity: PerformanceOpportunity): PlanPreseexport function agentHandoffCall(opportunityId: string): string {return `get_health(opportunity_id="${opportunityId}")`;}++/** A sibling's fix, in words, for the drawer's "also flagged" line. */+export function siblingFixLabel(sibling: PerformanceOpportunitySibling): string {+ return humanizeToken(sibling.strategy ?? sibling.biomarker_type);+}diff --git a/tests/fixtures/perf_corpus/golden_opportunities.json b/tests/fixtures/perf_corpus/golden_opportunities.jsonindex e79deae34..59f8c0d04 100644--- a/tests/fixtures/perf_corpus/golden_opportunities.json+++ b/tests/fixtures/perf_corpus/golden_opportunities.json@@ -1,80 +1,5 @@{"opportunities": [- {- "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": "serial_await_in_loop",- "file_path": "src/app/fanout.py",- "finding_id": "finding_28eb6a8735fdf18585c6",- "function_name": "gather_all",- "line_end": 92,- "line_start": 90,- "path": [],- "provenance": "direct",- "reason": "async_serial_awaits: serial_await_in_loop at src/app/fanout.py:90 repeats work per iteration."- }- ],- "evidence_truncated": false,- "execution_context": "production",- "facets": {- "actionability_confidence": "high",- "amplification": "per_iteration",- "change_risk": "contained",- "exposure": "unknown",- "leverage": "isolated"- },- "fix": {- "rationale": "Dataflow proves that every observed loop carries no cross-iteration dependence.",- "safety": "proven",- "strategy": "parallelize_independent_awaits"- },- "intervention_symbol": null,- "observations_total": 1,- "opportunity_id": "perf2_182b2a7c053167959285",- "performance_model_version": 2,- "prerequisites": [],- "provenance": "direct",- "rank_factors": {- "affected_call_sites": 2,- "boundary_kind": 4,- "entry_reachability": 0,- "execution_context": 3,- "multiplier_shape": 4,- "provenance": 3- },- "rank_score": 16,- "reliable_entry_reachability": null,- "resource_fingerprints": [],- "shared_path_suffix": [],- "terminal_sink": null,- "why_ranked": [- {- "factor": "boundary_kind",- "points": 4,- "value": "network"- },- {- "factor": "multiplier_shape",- "points": 4,- "value": "serial_await_in_loop"- },- {- "factor": "execution_context",- "points": 3,- "value": "production"- }- ]- },{"actionability_reason": "proven_strategy","actionability_state": "plan_ready",@@ -131,6 +56,7 @@"reliable_entry_reachability": null,"resource_fingerprints": [],"shared_path_suffix": [],+ "siblings": [],"terminal_sink": null,"why_ranked": [{@@ -231,6 +157,7 @@"src/app/entry.py::_hit","src/app/db.py::reach_sink"],+ "siblings": [],"terminal_sink": "src/app/db.py::reach_sink","why_ranked": [{@@ -331,6 +258,7 @@"src/app/repo.py::fetch_page","src/app/db.py::execute"],+ "siblings": [],"terminal_sink": "src/app/db.py::execute","why_ranked": [{@@ -432,6 +360,7 @@"src/app/reindex.py::_write","src/app/db.py::upsert"],+ "siblings": [],"terminal_sink": "src/app/db.py::upsert","why_ranked": [{@@ -532,6 +461,7 @@"src/app/sync.py::_load","src/app/db.py::query"],+ "siblings": [],"terminal_sink": "src/app/db.py::query","why_ranked": [{@@ -615,6 +545,7 @@"src/app/cache.py::refresh","src/app/db.py::write"],+ "siblings": [],"terminal_sink": "src/app/db.py::write","why_ranked": [{@@ -634,6 +565,84 @@}]},+ {+ "actionability_reason": "strategy_requires_validation",+ "actionability_state": "advisory",+ "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": "serial_await_in_loop",+ "file_path": "src/app/fanout.py",+ "finding_id": "finding_28eb6a8735fdf18585c6",+ "function_name": "gather_all",+ "line_end": 92,+ "line_start": 90,+ "path": [],+ "provenance": "direct",+ "reason": "async_serial_awaits: serial_await_in_loop at src/app/fanout.py:90 repeats work per iteration."+ }+ ],+ "evidence_truncated": false,+ "execution_context": "production",+ "facets": {+ "actionability_confidence": "medium",+ "amplification": "per_iteration",+ "change_risk": "contained",+ "exposure": "unknown",+ "leverage": "isolated"+ },+ "fix": {+ "rationale": "Iteration independence is proven; the concurrency the fan-out would create is not bounded by anything this group can see.",+ "safety": "advisory",+ "strategy": "parallelize_independent_awaits"+ },+ "intervention_symbol": null,+ "observations_total": 1,+ "opportunity_id": "perf2_182b2a7c053167959285",+ "performance_model_version": 2,+ "prerequisites": [+ "bounded_concurrency"+ ],+ "provenance": "direct",+ "rank_factors": {+ "affected_call_sites": 2,+ "boundary_kind": 4,+ "entry_reachability": 0,+ "execution_context": 3,+ "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": "serial_await_in_loop"+ },+ {+ "factor": "execution_context",+ "points": 3,+ "value": "production"+ }+ ]+ },{"actionability_reason": "strategy_requires_validation","actionability_state": "advisory",@@ -699,6 +708,7 @@"src/app/billing.py::handle","src/app/db.py::get_session"],+ "siblings": [],"terminal_sink": "src/app/db.py::get_session","why_ranked": [{@@ -783,6 +793,7 @@"src/app/orders.py::handle","src/app/db.py::get_session"],+ "siblings": [],"terminal_sink": "src/app/db.py::get_session","why_ranked": [{@@ -867,6 +878,7 @@"src/app/reports.py::handle","src/app/db.py::get_session"],+ "siblings": [],"terminal_sink": "src/app/db.py::get_session","why_ranked": [{@@ -951,6 +963,7 @@"src/app/job.py::run","src/app/db.py::get_session"],+ "siblings": [],"terminal_sink": "src/app/db.py::get_session","why_ranked": [{@@ -1033,6 +1046,7 @@"shared_path_suffix": ["src/app/db.py::only_sink"],+ "siblings": [],"terminal_sink": null,"why_ranked": [{@@ -1117,6 +1131,7 @@"scripts/backfill.py::run","src/app/db.py::get_session"],+ "siblings": [],"terminal_sink": "src/app/db.py::get_session","why_ranked": [{@@ -1201,6 +1216,7 @@"src/app/edge.py::run","src/app/db.py::reliable_sink"],+ "siblings": [],"terminal_sink": "src/app/db.py::reliable_sink","why_ranked": [{@@ -1285,6 +1301,7 @@"tests/test_job.py::run","src/app/db.py::get_session"],+ "siblings": [],"terminal_sink": "src/app/db.py::get_session","why_ranked": [{@@ -1370,6 +1387,7 @@"src/app/guess.py::run","src/app/db.py::guessed_sink"],+ "siblings": [],"terminal_sink": "src/app/db.py::guessed_sink","why_ranked": [{@@ -1448,6 +1466,7 @@"reliable_entry_reachability": null,"resource_fingerprints": [],"shared_path_suffix": [],+ "siblings": [],"terminal_sink": null,"why_ranked": [{@@ -1525,6 +1544,7 @@"reliable_entry_reachability": null,"resource_fingerprints": [],"shared_path_suffix": [],+ "siblings": [],"terminal_sink": null,"why_ranked": [{@@ -1620,6 +1640,7 @@"src/app/store.py::flush_all","src/app/store.py::flush"],+ "siblings": [],"terminal_sink": "src/app/store.py::flush","why_ranked": [{@@ -1745,6 +1766,7 @@"src/app/parse.py::_get_query","src/app/parse.py::_load_compiled_query"],+ "siblings": [],"terminal_sink": "src/app/parse.py::_load_compiled_query","why_ranked": [{@@ -1818,6 +1840,7 @@"reliable_entry_reachability": null,"resource_fingerprints": [],"shared_path_suffix": [],+ "siblings": [],"terminal_sink": null,"why_ranked": [{@@ -1891,6 +1914,7 @@"reliable_entry_reachability": null,"resource_fingerprints": [],"shared_path_suffix": [],+ "siblings": [],"terminal_sink": null,"why_ranked": [{@@ -1970,6 +1994,7 @@"src/app/walker.py::scan","src/app/fsio.py::read_text"],+ "siblings": [],"terminal_sink": "src/app/fsio.py::read_text","why_ranked": [{@@ -2049,6 +2074,7 @@"src/app/walker.py::index","src/app/fsio.py::read_text"],+ "siblings": [],"terminal_sink": "src/app/fsio.py::read_text","why_ranked": [{@@ -2122,6 +2148,7 @@"reliable_entry_reachability": null,"resource_fingerprints": [],"shared_path_suffix": [],+ "siblings": [],"terminal_sink": null,"why_ranked": [{@@ -2195,6 +2222,7 @@"reliable_entry_reachability": null,"resource_fingerprints": [],"shared_path_suffix": [],+ "siblings": [],"terminal_sink": null,"why_ranked": [{diff --git a/tests/fixtures/perf_corpus/golden_plans.json b/tests/fixtures/perf_corpus/golden_plans.jsonindex 15399bc8e..e3b421e3c 100644--- a/tests/fixtures/perf_corpus/golden_plans.json+++ b/tests/fixtures/perf_corpus/golden_plans.json@@ -1,62 +1,5 @@{"plans": [- {- "blast_radius": {- "call_sites": 1,- "file_count": 1,- "files": [- "src/app/fanout.py"- ]- },- "confidence": "high",- "evidence": {- "affected_files_total": 1,- "biomarker_type": "serial_await_in_loop",- "biomarker_types": [- "serial_await_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,- "multiplier_shape": 4,- "provenance": 3- },- "rank_score": 16,- "rationale": "Dataflow proves that every observed loop carries no cross-iteration dependence.",- "reliable_entry_reachability": null- },- "file_path": "src/app/fanout.py",- "impact_delta": 0.0,- "line_end": 92,- "line_start": 90,- "plan": {- "affected_locations": [- {- "file_path": "src/app/fanout.py",- "function_name": "gather_all",- "line_end": 92,- "line_start": 90- }- ],- "affected_locations_total": 1,- "evidence_truncated": false,- "intervention_symbol": null,- "opportunity_id": "perf2_182b2a7c053167959285",- "paths": [],- "paths_total": 1,- "safety": "proven",- "strategy": "parallelize_independent_awaits"- },- "refactoring_type": "performance_fix",- "source_biomarker": "serial_await_in_loop",- "target_symbol": "gather_all"- },{"blast_radius": {"call_sites": 1,@@ -104,10 +47,22 @@"affected_locations_total": 1,"evidence_truncated": false,"intervention_symbol": null,+ "judgment_steps": 0,+ "mechanical_steps": 1,"opportunity_id": "perf2_386a99aa2876c0dd31bd","paths": [],"paths_total": 1,"safety": "proven",+ "steps": [+ {+ "action": "Construct the resource once before the loop",+ "applicability": "mechanical",+ "file_path": "src/app/clients.py",+ "line": 170,+ "order": 1,+ "symbol": "each"+ }+ ],"strategy": "hoist_loop_invariant_resource"},"refactoring_type": "performance_fix",@@ -167,6 +122,8 @@"affected_locations_total": 2,"evidence_truncated": false,"intervention_symbol": "src/app/entry.py::_hit",+ "judgment_steps": 3,+ "mechanical_steps": 0,"opportunity_id": "perf2_0673587e7abbea99fc0b","paths": [[@@ -182,6 +139,32 @@],"paths_total": 2,"safety": "advisory",+ "steps": [+ {+ "action": "Add a form of _hit that takes every key at once",+ "applicability": "judgment",+ "file_path": "src/app/entry.py",+ "line": null,+ "order": 1,+ "symbol": "src/app/entry.py::_hit"+ },+ {+ "action": "Collect the keys before the loop and call the batched form once",+ "applicability": "judgment",+ "file_path": "src/app/entry.py",+ "line": 250,+ "order": 2,+ "symbol": "a"+ },+ {+ "action": "Collect the keys before the loop and call the batched form once",+ "applicability": "judgment",+ "file_path": "src/app/entry.py",+ "line": 255,+ "order": 3,+ "symbol": "b"+ }+ ],"strategy": "batch_or_prefetch_io"},"refactoring_type": "performance_fix",@@ -241,6 +224,8 @@"affected_locations_total": 2,"evidence_truncated": false,"intervention_symbol": "src/app/repo.py::fetch_page",+ "judgment_steps": 3,+ "mechanical_steps": 0,"opportunity_id": "perf2_89348be6c2ba0311a0e8","paths": [[@@ -256,6 +241,32 @@],"paths_total": 2,"safety": "advisory",+ "steps": [+ {+ "action": "Add a form of fetch_page that takes every key at once",+ "applicability": "judgment",+ "file_path": "src/app/repo.py",+ "line": null,+ "order": 1,+ "symbol": "src/app/repo.py::fetch_page"+ },+ {+ "action": "Collect the keys before the loop and call the batched form once",+ "applicability": "judgment",+ "file_path": "src/app/api.py",+ "line": 30,+ "order": 2,+ "symbol": "list_users"+ },+ {+ "action": "Collect the keys before the loop and call the batched form once",+ "applicability": "judgment",+ "file_path": "src/app/api.py",+ "line": 31,+ "order": 3,+ "symbol": "list_orders"+ }+ ],"strategy": "batch_or_prefetch_io"},"refactoring_type": "performance_fix",@@ -316,6 +327,8 @@"affected_locations_total": 2,"evidence_truncated": false,"intervention_symbol": "src/app/reindex.py::_write",+ "judgment_steps": 3,+ "mechanical_steps": 0,"opportunity_id": "perf2_3d62fcebb00bdf408f2c","paths": [[@@ -331,6 +344,32 @@],"paths_total": 2,"safety": "advisory",+ "steps": [+ {+ "action": "Add a form of _write that takes every key at once",+ "applicability": "judgment",+ "file_path": "src/app/reindex.py",+ "line": null,+ "order": 1,+ "symbol": "src/app/reindex.py::_write"+ },+ {+ "action": "Collect the keys before the loop and call the batched form once",+ "applicability": "judgment",+ "file_path": "src/app/reindex.py",+ "line": 210,+ "order": 2,+ "symbol": "one"+ },+ {+ "action": "Collect the keys before the loop and call the batched form once",+ "applicability": "judgment",+ "file_path": "src/app/reindex.py",+ "line": 215,+ "order": 3,+ "symbol": "many"+ }+ ],"strategy": "batch_or_prefetch_io"},"refactoring_type": "performance_fix",@@ -390,6 +429,8 @@"affected_locations_total": 2,"evidence_truncated": false,"intervention_symbol": "src/app/sync.py::_load",+ "judgment_steps": 3,+ "mechanical_steps": 0,"opportunity_id": "perf2_af29fc10103a555c8652","paths": [[@@ -405,6 +446,32 @@],"paths_total": 2,"safety": "advisory",+ "steps": [+ {+ "action": "Add a form of _load that takes every key at once",+ "applicability": "judgment",+ "file_path": "src/app/sync.py",+ "line": null,+ "order": 1,+ "symbol": "src/app/sync.py::_load"+ },+ {+ "action": "Collect the keys before the loop and call the batched form once",+ "applicability": "judgment",+ "file_path": "src/app/sync.py",+ "line": 70,+ "order": 2,+ "symbol": "sync_one"+ },+ {+ "action": "Collect the keys before the loop and call the batched form once",+ "applicability": "judgment",+ "file_path": "src/app/sync.py",+ "line": 71,+ "order": 3,+ "symbol": "sync_all"+ }+ ],"strategy": "batch_or_prefetch_io"},"refactoring_type": "performance_fix",@@ -458,6 +525,8 @@"affected_locations_total": 1,"evidence_truncated": false,"intervention_symbol": "src/app/cache.py::refresh",+ "judgment_steps": 1,+ "mechanical_steps": 0,"opportunity_id": "perf2_9dfe577bc9c8b799355e","paths": [[@@ -467,12 +536,91 @@],"paths_total": 1,"safety": "advisory",+ "steps": [+ {+ "action": "Move the I/O in refresh out of the locked section",+ "applicability": "judgment",+ "file_path": "src/app/cache.py",+ "line": null,+ "order": 1,+ "symbol": "src/app/cache.py::refresh"+ }+ ],"strategy": "shrink_lock_scope"},"refactoring_type": "performance_fix","source_biomarker": "blocking_io_under_lock","target_symbol": "src/app/cache.py::refresh"},+ {+ "blast_radius": {+ "call_sites": 1,+ "file_count": 1,+ "files": [+ "src/app/fanout.py"+ ]+ },+ "confidence": "medium",+ "evidence": {+ "affected_files_total": 1,+ "biomarker_type": "serial_await_in_loop",+ "biomarker_types": [+ "serial_await_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,+ "multiplier_shape": 4,+ "provenance": 3+ },+ "rank_score": 16,+ "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+ },+ "file_path": "src/app/fanout.py",+ "impact_delta": 0.0,+ "line_end": 92,+ "line_start": 90,+ "plan": {+ "affected_locations": [+ {+ "file_path": "src/app/fanout.py",+ "function_name": "gather_all",+ "line_end": 92,+ "line_start": 90+ }+ ],+ "affected_locations_total": 1,+ "evidence_truncated": false,+ "intervention_symbol": null,+ "judgment_steps": 1,+ "mechanical_steps": 0,+ "opportunity_id": "perf2_182b2a7c053167959285",+ "paths": [],+ "paths_total": 1,+ "safety": "advisory",+ "steps": [+ {+ "action": "Run the independent awaits concurrently, with a bound on how many at once",+ "applicability": "judgment",+ "file_path": "src/app/fanout.py",+ "line": 90,+ "order": 1,+ "symbol": "gather_all"+ }+ ],+ "strategy": "parallelize_independent_awaits"+ },+ "refactoring_type": "performance_fix",+ "source_biomarker": "serial_await_in_loop",+ "target_symbol": "gather_all"+ },{"blast_radius": {"call_sites": 1,@@ -520,6 +668,8 @@"affected_locations_total": 1,"evidence_truncated": false,"intervention_symbol": "src/app/billing.py::handle",+ "judgment_steps": 2,+ "mechanical_steps": 0,"opportunity_id": "perf2_3d4056f8405c9313d004","paths": [[@@ -529,6 +679,24 @@],"paths_total": 1,"safety": "advisory",+ "steps": [+ {+ "action": "Add a form of handle that takes every key at once",+ "applicability": "judgment",+ "file_path": "src/app/billing.py",+ "line": null,+ "order": 1,+ "symbol": "src/app/billing.py::handle"+ },+ {+ "action": "Collect the keys before the loop and call the batched form once",+ "applicability": "judgment",+ "file_path": "src/app/billing.py",+ "line": 11,+ "order": 2,+ "symbol": "handle"+ }+ ],"strategy": "batch_or_prefetch_io"},"refactoring_type": "performance_fix",@@ -582,6 +750,8 @@"affected_locations_total": 1,"evidence_truncated": false,"intervention_symbol": "src/app/orders.py::handle",+ "judgment_steps": 2,+ "mechanical_steps": 0,"opportunity_id": "perf2_5abe869788cb2cebf6cf","paths": [[@@ -591,6 +761,24 @@],"paths_total": 1,"safety": "advisory",+ "steps": [+ {+ "action": "Add a form of handle that takes every key at once",+ "applicability": "judgment",+ "file_path": "src/app/orders.py",+ "line": null,+ "order": 1,+ "symbol": "src/app/orders.py::handle"+ },+ {+ "action": "Collect the keys before the loop and call the batched form once",+ "applicability": "judgment",+ "file_path": "src/app/orders.py",+ "line": 10,+ "order": 2,+ "symbol": "handle"+ }+ ],"strategy": "batch_or_prefetch_io"},"refactoring_type": "performance_fix",@@ -644,6 +832,8 @@"affected_locations_total": 1,"evidence_truncated": false,"intervention_symbol": "src/app/reports.py::handle",+ "judgment_steps": 2,+ "mechanical_steps": 0,"opportunity_id": "perf2_7d6b729c3f43753626f2","paths": [[@@ -653,6 +843,24 @@],"paths_total": 1,"safety": "advisory",+ "steps": [+ {+ "action": "Add a form of handle that takes every key at once",+ "applicability": "judgment",+ "file_path": "src/app/reports.py",+ "line": null,+ "order": 1,+ "symbol": "src/app/reports.py::handle"+ },+ {+ "action": "Collect the keys before the loop and call the batched form once",+ "applicability": "judgment",+ "file_path": "src/app/reports.py",+ "line": 12,+ "order": 2,+ "symbol": "handle"+ }+ ],"strategy": "batch_or_prefetch_io"},"refactoring_type": "performance_fix",@@ -706,6 +914,8 @@"affected_locations_total": 1,"evidence_truncated": false,"intervention_symbol": "src/app/job.py::run",+ "judgment_steps": 2,+ "mechanical_steps": 0,"opportunity_id": "perf2_811bb33b8d8f9e744638","paths": [[@@ -715,6 +925,24 @@],"paths_total": 1,"safety": "advisory",+ "steps": [+ {+ "action": "Add a form of run that takes every key at once",+ "applicability": "judgment",+ "file_path": "src/app/job.py",+ "line": null,+ "order": 1,+ "symbol": "src/app/job.py::run"+ },+ {+ "action": "Collect the keys before the loop and call the batched form once",+ "applicability": "judgment",+ "file_path": "src/app/job.py",+ "line": 200,+ "order": 2,+ "symbol": "run"+ }+ ],"strategy": "batch_or_prefetch_io"},"refactoring_type": "performance_fix",@@ -768,6 +996,8 @@"affected_locations_total": 1,"evidence_truncated": false,"intervention_symbol": null,+ "judgment_steps": 2,+ "mechanical_steps": 0,"opportunity_id": "perf2_f349f0c0ac6827ebfd86","paths": [[@@ -776,6 +1006,24 @@],"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/lone.py",+ "line": 9,+ "order": 2,+ "symbol": "run"+ }+ ],"strategy": "batch_or_prefetch_io"},"refactoring_type": "performance_fix",@@ -829,6 +1077,8 @@"affected_locations_total": 1,"evidence_truncated": false,"intervention_symbol": "scripts/backfill.py::run",+ "judgment_steps": 2,+ "mechanical_steps": 0,"opportunity_id": "perf2_626e6b4848d036aede65","paths": [[@@ -838,6 +1088,24 @@],"paths_total": 1,"safety": "advisory",+ "steps": [+ {+ "action": "Add a form of run that takes every key at once",+ "applicability": "judgment",+ "file_path": "scripts/backfill.py",+ "line": null,+ "order": 1,+ "symbol": "scripts/backfill.py::run"+ },+ {+ "action": "Collect the keys before the loop and call the batched form once",+ "applicability": "judgment",+ "file_path": "scripts/backfill.py",+ "line": 200,+ "order": 2,+ "symbol": "run"+ }+ ],"strategy": "batch_or_prefetch_io"},"refactoring_type": "performance_fix",@@ -891,6 +1159,8 @@"affected_locations_total": 1,"evidence_truncated": false,"intervention_symbol": "src/app/edge.py::run",+ "judgment_steps": 2,+ "mechanical_steps": 0,"opportunity_id": "perf2_cf3653d0149e79f7a1fb","paths": [[@@ -900,6 +1170,24 @@],"paths_total": 1,"safety": "advisory",+ "steps": [+ {+ "action": "Add a form of run that takes every key at once",+ "applicability": "judgment",+ "file_path": "src/app/edge.py",+ "line": null,+ "order": 1,+ "symbol": "src/app/edge.py::run"+ },+ {+ "action": "Collect the keys before the loop and call the batched form once",+ "applicability": "judgment",+ "file_path": "src/app/edge.py",+ "line": 230,+ "order": 2,+ "symbol": "run"+ }+ ],"strategy": "batch_or_prefetch_io"},"refactoring_type": "performance_fix",@@ -953,6 +1241,8 @@"affected_locations_total": 1,"evidence_truncated": false,"intervention_symbol": "tests/test_job.py::run",+ "judgment_steps": 2,+ "mechanical_steps": 0,"opportunity_id": "perf2_b17b10884d21aeeb4df0","paths": [[@@ -962,6 +1252,24 @@],"paths_total": 1,"safety": "advisory",+ "steps": [+ {+ "action": "Add a form of run that takes every key at once",+ "applicability": "judgment",+ "file_path": "tests/test_job.py",+ "line": null,+ "order": 1,+ "symbol": "tests/test_job.py::run"+ },+ {+ "action": "Collect the keys before the loop and call the batched form once",+ "applicability": "judgment",+ "file_path": "tests/test_job.py",+ "line": 200,+ "order": 2,+ "symbol": "run"+ }+ ],"strategy": "batch_or_prefetch_io"},"refactoring_type": "performance_fix",@@ -1015,6 +1323,8 @@"affected_locations_total": 1,"evidence_truncated": false,"intervention_symbol": "src/app/guess.py::run",+ "judgment_steps": 2,+ "mechanical_steps": 0,"opportunity_id": "perf2_a817f9001be1d323f8a2","paths": [[@@ -1024,6 +1334,24 @@],"paths_total": 1,"safety": "advisory",+ "steps": [+ {+ "action": "Add a form of run that takes every key at once",+ "applicability": "judgment",+ "file_path": "src/app/guess.py",+ "line": null,+ "order": 1,+ "symbol": "src/app/guess.py::run"+ },+ {+ "action": "Collect the keys before the loop and call the batched form once",+ "applicability": "judgment",+ "file_path": "src/app/guess.py",+ "line": 240,+ "order": 2,+ "symbol": "run"+ }+ ],"strategy": "batch_or_prefetch_io"},"refactoring_type": "performance_fix",@@ -1077,10 +1405,22 @@"affected_locations_total": 1,"evidence_truncated": false,"intervention_symbol": null,+ "judgment_steps": 1,+ "mechanical_steps": 0,"opportunity_id": "perf2_6b8f8be1ab10062f0a19","paths": [],"paths_total": 1,"safety": "advisory",+ "steps": [+ {+ "action": "Build a set once before the loop and probe it",+ "applicability": "judgment",+ "file_path": "src/app/filters.py",+ "line": 150,+ "order": 1,+ "symbol": "select"+ }+ ],"strategy": "replace_membership_collection"},"refactoring_type": "performance_fix",@@ -1134,10 +1474,22 @@"affected_locations_total": 1,"evidence_truncated": false,"intervention_symbol": null,+ "judgment_steps": 1,+ "mechanical_steps": 0,"opportunity_id": "perf2_e82f5ad0e9c24fe3d1a1","paths": [],"paths_total": 1,"safety": "advisory",+ "steps": [+ {+ "action": "Accumulate into a list and join once after the loop",+ "applicability": "judgment",+ "file_path": "src/app/render.py",+ "line": 160,+ "order": 1,+ "symbol": "to_csv"+ }+ ],"strategy": "buffer_string_accumulation"},"refactoring_type": "performance_fix",diff --git a/tests/unit/health/test_model_version_coupling.py b/tests/unit/health/test_model_version_coupling.pyindex 704de1747..1ab4cda19 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 = (21, 2, 2)+_STAMPS = (22, 2, 2)def test_the_identity_models_and_the_analyzer_stamp_are_pinned_together() -> None:diff --git a/tests/unit/health/test_paired_test_basenames.py b/tests/unit/health/test_paired_test_basenames.pyindex e659ad428..5a456b643 100644--- a/tests/unit/health/test_paired_test_basenames.py+++ b/tests/unit/health/test_paired_test_basenames.py@@ -150,3 +150,18 @@ class TestPascalPairing:assert _has_paired_test_file("src/user.cr", _path_basenames(crystal))# Same stem, dot form, must NOT count for Ruby's underscore layout.assert not _has_paired_test_file("lib/user.rb", _path_basenames({"spec/user.spec.rb"}))+++def test_name_match_tier_finds_tests_named_for_a_target_anywhere():+ from repowise.core.analysis.test_reachability import tests_matching_by_name++ tests = {"tests/unit/test_jira_links.py", "tests/other/test_jira_links.py", "web/a.test.ts"}+ found = tests_matching_by_name(["app/jira_links.py", "web/a.ts", "app/none.py"], tests)++ assert found["app/jira_links.py"].tests == [+ "tests/other/test_jira_links.py",+ "tests/unit/test_jira_links.py",+ ]+ assert found["app/jira_links.py"].via == "name-match"+ assert found["web/a.ts"].total == 1+ assert "app/none.py" not in founddiff --git a/tests/unit/health/test_perf_io_in_loop.py b/tests/unit/health/test_perf_io_in_loop.pyindex 90cf3db0b..ef2274f71 100644--- a/tests/unit/health/test_perf_io_in_loop.py+++ b/tests/unit/health/test_perf_io_in_loop.py@@ -275,3 +275,26 @@ def test_perf_findings_score_performance_not_defect():assert scores["performance"] < 10.0# Every perf finding carries 0 defect-pillar impact.assert all(d == 0.0 for d in deductions)+++ ("header", "chunked"),+ [+ ("for start in range(0, len(ids), CHUNK):", True),+ ("for batch in itertools.batched(ids, 50):", True),+ ("for start in range(0, len(ids), 1):", False),+ ("for i in ids:", False),+ ],+)+def test_a_sink_in_a_chunked_loop_carries_the_fact(header: str, chunked: bool):+ source = (+ "async def f(client, ids):\n"+ f" {header}\n"+ " await client.table('t').select('*').execute()\n"+ ).encode()+ 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)+ finding = IoInLoopDetector().detect(_ctx(loop_hits))[0]+ assert finding.details.get("chunked_iteration", False) is chunkeddiff --git a/tests/unit/health/test_perf_opportunities.py b/tests/unit/health/test_perf_opportunities.pyindex fe1ae5c43..f613c108a 100644--- a/tests/unit/health/test_perf_opportunities.py+++ b/tests/unit/health/test_perf_opportunities.py@@ -106,7 +106,7 @@ def test_strategy_preconditions_distinguish_proven_advisory_and_no_plan():"async.py",5,marker="serial_await_in_loop",- boundary="network",+ boundary="filesystem",dataflow_verified=True,)]@@ -234,7 +234,7 @@ def test_performance_fix_plan_carries_closed_strategy_and_true_totals():],evidence_limit=1,)[0]- plans = performance_fix_suggestions([opportunity], nloc_by_file={"shared.py": 80})+ plans = performance_fix_suggestions([opportunity])assert len(plans) == 1plan = plans[0]assert plan.refactoring_type == "performance_fix"@@ -245,6 +245,8 @@ def test_performance_fix_plan_carries_closed_strategy_and_true_totals():assert plan.plan["affected_locations_total"] == 2assert plan.plan["paths_total"] == 2assert plan.plan["evidence_truncated"] is True+ # Two call sites plus the batched form: effort counts edits, not file size.+ assert plan.effort_bucket == "M"assert performance_fix_suggestions([opportunity], min_confidence="high") == []@@ -290,7 +292,7 @@ def test_a_reliable_path_keeps_the_proven_strategy_and_its_plan_confidence():"async.py",5,marker="serial_await_in_loop",- boundary="network",+ boundary="filesystem",dataflow_verified=True,)]@@ -321,3 +323,67 @@ def test_a_cross_function_group_always_shares_its_last_two_path_nodes():opportunity.intervention_symbol,opportunity.terminal_sink,)+++def test_a_fan_out_at_a_pooled_boundary_is_advisory_until_something_bounds_it():+ opportunity = build_performance_opportunities(+ [_finding("async.py", 5, marker="serial_await_in_loop", dataflow_verified=True)]+ )[0]++ assert opportunity.fix and opportunity.fix.strategy == "parallelize_independent_awaits"+ assert opportunity.fix.safety == "advisory"+ assert opportunity.prerequisites == ("bounded_concurrency",)+ assert opportunity.actionability_state == "advisory"+++def test_a_loop_that_already_chunks_is_not_told_to_batch():+ opportunity = build_performance_opportunities(+ [_finding("sync.py", 5, chunked_iteration=True)]+ )[0]++ assert opportunity.fix is None+ assert opportunity.actionability_state == "investigate"+ assert opportunity.actionability_reason == "loop_already_chunked"+++def test_two_causes_on_one_line_name_each_other_and_the_stronger_fix_leads():+ rows = [+ _finding("jobs.py", 5, marker="serial_await_in_loop", dataflow_verified=True),+ _finding("jobs.py", 5),+ _finding("other.py", 9, marker="serial_await_in_loop", dataflow_verified=True),+ ]+ opportunities = build_performance_opportunities(rows)+ by_marker = {+ (item.biomarker_type, item.evidence[0]["file_path"]): item for item in opportunities+ }+ batch = by_marker[("io_in_loop", "jobs.py")]+ parallel = by_marker[("serial_await_in_loop", "jobs.py")]++ assert [s["relation"] for s in batch.siblings] == ["alternative"]+ assert parallel.siblings[0]["opportunity_id"] == batch.opportunity_id+ assert parallel.siblings[0]["relation"] == "preferred"+ assert by_marker[("serial_await_in_loop", "other.py")].siblings == ()+ order = [item.opportunity_id for item in opportunities]+ assert order.index(parallel.opportunity_id) == order.index(batch.opportunity_id) + 1+++def test_a_plan_lists_its_edits_and_marks_only_proven_ones_mechanical():+ advisory = build_performance_opportunities(+ [_finding("a.py", 2, call_path=("a.py::run", "db.py::fetch"))]+ )+ proven = build_performance_opportunities(+ [+ _finding(+ "f.py", 3, marker="serial_await_in_loop", boundary="filesystem",+ dataflow_verified=True,+ )+ ]+ )+ batch_plan = performance_fix_suggestions(advisory)[0].plan+ parallel_plan = performance_fix_suggestions(proven)[0].plan++ assert [step["order"] for step in batch_plan["steps"]] == [1, 2]+ assert batch_plan["steps"][0]["symbol"] == "a.py::run"+ assert batch_plan["steps"][1]["line"] == 2+ assert (batch_plan["mechanical_steps"], batch_plan["judgment_steps"]) == (0, 2)+ assert (parallel_plan["mechanical_steps"], parallel_plan["judgment_steps"]) == (1, 0)diff --git a/tests/unit/health/test_perf_unbounded_reduction.py b/tests/unit/health/test_perf_unbounded_reduction.pynew file mode 100644index 000000000..978ce0e9f--- /dev/null+++ b/tests/unit/health/test_perf_unbounded_reduction.py@@ -0,0 +1,211 @@+"""Unit tests for ``unbounded_read_reduced_in_memory``.++Positive: same-function supabase chain + setdefault, SQLAlchemy+``scalars().all()`` + seen-set, and the one-hop same-file-helper case (the+motivating shape: a query in one function reduced by its helper). Negative:+bounded reads, 1:1 projection, query-in-a-loop, unconsumed result, and a+helper defined in another file.+"""++from __future__ import annotations++import textwrap+from dataclasses import dataclass+from pathlib import Path++import pytest++from repowise.core.analysis.health.complexity import FileComplexity+from repowise.core.analysis.health.perf.unbounded_reduction import collect_unbounded_reductions++_KIND = "unbounded_read_reduced_in_memory"+++def _require_python() -> None:+ try:+ from repowise.core.ingestion.parser import _get_language+ except Exception:+ pytest.skip("tree-sitter language pack missing for python")+ if _get_language("python") is None:+ pytest.skip("tree-sitter language pack missing for python")+++@dataclass+class _FileInfo:+ path: str+ abs_path: str+ language: str+++@dataclass+class _ParsedFile:+ file_info: _FileInfo+++def _hits(tmp_path: Path, name: str, src: str) -> list:+ _require_python()+ p = tmp_path / name+ p.write_text(textwrap.dedent(src), encoding="utf-8")+ pf = _ParsedFile(_FileInfo(path=name, abs_path=str(p), language="python"))+ fcx = FileComplexity(functions=[], classes=[])+ collect_unbounded_reductions([(pf, fcx)])+ return [h for h in fcx.perf_hits if h.kind == _KIND]+++# -- positive ----------------------------------------------------------------+++def test_same_function_supabase_setdefault(tmp_path: Path):+ hits = _hits(+ tmp_path,+ "a.py",+ """+ def get_repos(supabase, repo_ids):+ builds = (+ supabase.table("builds")+ .select("*")+ .in_("repo_id", repo_ids)+ .eq("status", "ready")+ .order("completed_at", desc=True)+ .execute()+ )+ latest_by_repo = {}+ for build in builds.data:+ rid = build["repo_id"]+ latest_by_repo.setdefault(rid, build)+ return latest_by_repo+ """,+ )+ assert len(hits) == 1+ assert hits[0].function == "get_repos"+ assert hits[0].path == ()+++def test_sqlalchemy_scalars_all_seen_set(tmp_path: Path):+ hits = _hits(+ tmp_path,+ "b.py",+ """+ def latest_per_repo(session):+ rows = session.execute(select(Build)).scalars().all()+ seen = set()+ out = []+ for row in rows:+ rid = row.repo_id+ if rid in seen:+ continue+ seen.add(rid)+ out.append(row)+ return out+ """,+ )+ assert len(hits) == 1+ assert hits[0].function == "latest_per_repo"+++def test_one_hop_same_file_helper(tmp_path: Path):+ hits = _hits(+ tmp_path,+ "repos.py",+ """+ def list_latest_builds(supabase, repo_ids):+ builds = (+ supabase.table("builds")+ .select("*")+ .in_("repo_id", repo_ids)+ .eq("status", "ready")+ .execute()+ )+ return _latest_per_branch(builds.data)+++ def _latest_per_branch(builds):+ latest_by_repo = {}+ for build in builds:+ rid = build["repo_id"]+ latest_by_repo.setdefault(rid, build)+ return latest_by_repo+ """,+ )+ assert len(hits) == 1+ assert hits[0].function == "list_latest_builds"+ assert hits[0].path == ("_latest_per_branch",)+++# -- negative ------------------------------------------------------------+++def test_limit_bounds_the_read(tmp_path: Path):+ hits = _hits(+ tmp_path,+ "c.py",+ """+ def f(supabase):+ builds = supabase.table("builds").select("*").eq("status", "ready").limit(50).execute()+ out = {}+ for row in builds.data:+ out.setdefault(row["id"], row)+ return out+ """,+ )+ assert hits == []+++def test_bare_one_to_one_projection_does_not_fire(tmp_path: Path):+ hits = _hits(+ tmp_path,+ "d.py",+ """+ def f(supabase):+ builds = supabase.table("builds").select("*").execute()+ out = {}+ for row in builds.data:+ out[row.id] = row+ return out+ """,+ )+ assert hits == []+++def test_query_inside_a_loop_does_not_fire(tmp_path: Path):+ hits = _hits(+ tmp_path,+ "e.py",+ """+ def f(supabase, ids):+ out = {}+ for rid in ids:+ builds = supabase.table("builds").select("*").eq("repo_id", rid).execute()+ out[rid] = builds.data+ return out+ """,+ )+ assert hits == []+++def test_result_only_returned_does_not_fire(tmp_path: Path):+ hits = _hits(+ tmp_path,+ "f.py",+ """+ def f(supabase):+ builds = supabase.table("builds").select("*").execute()+ return builds.data+ """,+ )+ assert hits == []+++def test_different_file_helper_does_not_fire(tmp_path: Path):+ hits = _hits(+ tmp_path,+ "g.py",+ """+ from other_module import process_builds++ def f(supabase):+ builds = supabase.table("builds").select("*").execute()+ return process_builds(builds.data)+ """,+ )+ assert hits == []diff --git a/tests/unit/server/mcp/test_performance_levels.py b/tests/unit/server/mcp/test_performance_levels.pyindex 8b23114d2..399de83ef 100644--- a/tests/unit/server/mcp/test_performance_levels.py+++ b/tests/unit/server/mcp/test_performance_levels.py@@ -16,7 +16,7 @@ from sqlalchemy import eventfrom sqlalchemy.engine import Enginefrom repowise.core.persistence.crud import finalize_performance_opportunities-from repowise.core.persistence.models import HealthFinding+from repowise.core.persistence.models import GraphNode, HealthFinding_CALLERS = ("src/a.py", "src/b.py", "src/c.py", "src/d.py", "src/e.py")@@ -90,7 +90,10 @@ async def test_a_bare_dashboard_leads_with_something_to_do(setup_mcp, materializresult = await get_health()directive = result["performance_directive"]- assert directive["status"] == "plan_ready"+ # A fan-out at a db boundary is advisory until something bounds it.+ assert directive["status"] == "advisory"+ assert directive["prerequisites"] == ["bounded_concurrency"]+ assert directive["validation"]["basis"] in {"measured", "inferred", "mixed", "unknown"}assert directive["opportunity_id"].startswith("perf")assert directive["plan_state"] == "available"assert directive["next_action"] == {@@ -146,7 +149,7 @@ async def test_the_summary_rolls_up_and_names_the_next_call(setup_mcp, materialiresult = await get_health(include=["performance"], only=["performance_summary"])summary = result["performance_summary"]assert summary["status"] == "current"- assert summary["actionability"]["plan_ready"] == 1+ assert summary["actionability"].get("plan_ready", 0) == 0assert summary["with_plan_total"] == 2assert summary["analyzed_commit"] == "c" * 40assert "get_health" in summary["next_call"]@@ -195,7 +198,9 @@ async def test_an_unrecognized_filter_value_is_named_not_silently_empty(performance_context="staging",)assert result["performance_opportunities_total"] == 2- assert result["ignored_arguments"] == {"performance_context": "staging"}+ assert result["ignored_arguments"] == {+ "performance_context": "staging (accepted: production, tooling, test, unknown, all)"+ }@pytest.mark.asyncio@@ -235,7 +240,9 @@ async def test_one_id_returns_the_cause_its_plan_and_its_rank_rationale(# The plan address space is the refactoring layer's content identity.assert result["plan_reference"].startswith("refac2_")assert result["confidence"] == "high"- assert result["fix"]["safety"] == "proven"+ assert result["fix"]["safety"] == "advisory"+ assert [step["order"] for step in result["plan_steps"]] == [1, 2, 3, 4, 5]+ assert result["validation"]["commands"]assert result["facets"]["leverage"] == "shared"assert result["why_ranked"]assert len(json.dumps(result)) <= 20_000@@ -452,3 +459,27 @@ async def test_rest_and_the_agent_surface_project_the_same_rows(for item in agent["performance_opportunities"]if item["plan_status"] == "available")+++async def test_a_plan_is_validated_by_a_test_named_for_its_file_when_no_edge_reaches_it(+ setup_mcp, session, health_data+):+ """The plan selector used to rebuild validation from nothing: always unknown."""+ from repowise.server.mcp_server import get_health++ session.add(GraphNode(repository_id=health_data, node_id="tests/test_a.py", is_test=True))+ for index, caller in enumerate(_CALLERS, start=10):+ session.add(_row(health_data, caller, index))+ await session.flush()+ await finalize_performance_opportunities(session, health_data, analyzed_commit="c" * 40)+ await session.commit()++ lead = (await get_health())["performance_directive"]+ assert lead["validation"]["via"] == "name-match"+ opportunity = await get_health(opportunity_id=lead["opportunity_id"])+ plan = (await get_health(plan_id=opportunity["plan_reference"]))["plan"]++ assert "tests/test_a.py" in opportunity["validation"]["tests"]+ assert plan["validation"]["via"] == "name-match"+ assert "pytest tests/test_a.py" in plan["validation"]["commands"]diff --git a/tests/unit/server/test_performance_opportunities.py b/tests/unit/server/test_performance_opportunities.pyindex 8e7ff04d9..5fd11617b 100644--- a/tests/unit/server/test_performance_opportunities.py+++ b/tests/unit/server/test_performance_opportunities.py@@ -222,8 +222,8 @@ async def test_an_unrecognized_filter_value_is_reported_not_read_as_no_data(assert body["total"] == 1assert body["items"][0]["execution_context"] == "production"assert body["ignored_arguments"] == {- "performance_context": "staging",- "performance_boundary": "pigeon",+ "performance_context": "staging (accepted: production, tooling, test, unknown, all)",+ "performance_boundary": "pigeon (accepted: db, network, filesystem, subprocess, lock, none)",}