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): judge a loop's size by the columns of the tables it reads (#4236)
Raghav Chamadiya39c98ca126e2
diff --git a/docs/layers/PERFORMANCE.md b/docs/layers/PERFORMANCE.mdindex 0dba20fca..7a1b7c55a 100644--- a/docs/layers/PERFORMANCE.md+++ b/docs/layers/PERFORMANCE.md@@ -69,7 +69,7 @@ Each opportunity gets one state:| `plan_ready` | A fix strategy whose transformation is proven safe || `advisory` | A strategy with a prerequisite Repowise cannot prove, such as bounded concurrency before parallelizing awaits against a database || `investigate` | No supported strategy; a person has to decide |-| `expected` | The repetition is real and nothing should change, such as deleting N files, or the function is switched off by a constant flag (reason `gated_off`) or runs once per deploy, boot or incident (reason `cold_path`), or its loop runs a fixed number of times (reason `bounded_loop`) |+| `expected` | The repetition is real and nothing should change, such as deleting N files, or the function is switched off by a constant flag (reason `gated_off`) or runs once per deploy, boot or incident (reason `cold_path`), or its loop runs a fixed number of times (reason `bounded_loop`) or grows only by reading tables that do not grow with the repository (reason `small_table`) |A plan lists its edits, one step per call site and loop, each naming the call itrepeats and the kind of I/O that call does (database, network, file system or@@ -87,6 +87,13 @@ stays unknown and is never treated as bounded. A loop that stops at the first cathat answers, or feeds each call's result into the next, gets no batch advice(reason `loop_not_batchable`).+A loop that grows only by reading SQLAlchemy tables is judged by those models' declared+columns. In a schema keyed by files, commits, graph nodes or symbols, a table with+such a key (or a foreign key to one) grows with the repository and one without+(registered repositories, decision records) is small, so the loop is bounded and+names the tables (`loop_small_tables`). Elsewhere, a registry table name alone+leaves the loop unknown.+## The default queueThe default queue holds production opportunities in the `plan_ready` anddiff --git a/packages/core/src/repowise/core/analysis/change_health/service.py b/packages/core/src/repowise/core/analysis/change_health/service.pyindex 942f19974..16e4e5765 100644--- a/packages/core/src/repowise/core/analysis/change_health/service.py+++ b/packages/core/src/repowise/core/analysis/change_health/service.py@@ -697,6 +697,8 @@ _EXPECTED_TEXT: dict[str | None, str] = {"gated_off": "Nothing to change while a constant-False flag switches this function off.","cold_path": "Runs once per deploy, boot or incident; not worth scheduling.","bounded_loop": "Nothing to change: the loop runs a fixed number of times.",+ "small_table": "Nothing to change: the loop walks a table that does not grow with the "+ "repository.",None: _INHERENT,}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 a77d466bb..196fa028f 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@@ -27,6 +27,7 @@ from typing import TYPE_CHECKING, NamedTuplefrom ..perf.dialects import PERF_DIALECTSfrom ..perf.dialects.base import BasePerfDialect as BasePerfDialectClassfrom ..perf.loop_facts import BatchForm, LoopFacts, LoopSource, combined_magnitude+from ..perf.table_scale import small_table_candidatefrom .ast_utils import (_dart_signature_sibling,_find_function_entry_name,@@ -688,13 +689,15 @@ class _PerfWalk:loop = loops[0]do_magnitude = self.do_magnitudechunked = self.do_chunked and dialect.is_chunked_loop(loop)- magnitude = (- combined_magnitude([self.source(lp) for lp in loops]) if do_magnitude else "unknown"- )+ sources = [self.source(lp) for lp in loops] if do_magnitude else []+ magnitude = combined_magnitude(sources) if do_magnitude else "unknown"+ read_tables, small_magnitude = small_table_candidate(sources)batch, bound = self.sink_facts(node, loop, sink)facts = LoopFacts(chunked=chunked,magnitude=magnitude,+ read_tables=read_tables,+ small_magnitude=small_magnitude,batch=batch,concurrency_bound=bound,# Only a native loop header names its key; a block loop (Rubydiff --git a/packages/core/src/repowise/core/analysis/health/crossfn_perf.py b/packages/core/src/repowise/core/analysis/health/crossfn_perf.pyindex 003ab4bcb..73b4842dd 100644--- a/packages/core/src/repowise/core/analysis/health/crossfn_perf.py+++ b/packages/core/src/repowise/core/analysis/health/crossfn_perf.py@@ -19,6 +19,8 @@ from .perf import (collect_crossfn_io_in_loop,)from .perf.lazy_load import collect_lazy_loads+from .perf.orm_models import build_model_index+from .perf.table_scale import settle_small_table_loopsfrom .perf.unbounded_reduction import collect_unbounded_reductionsfrom .source_reader import SourceReader@@ -61,7 +63,13 @@ class CrossFunctionPerfStage:with timed(timings, "analysis.health.unbounded_reduction"):collect_unbounded_reductions(walked, read_source=self._read_source)with timed(timings, "analysis.health.lazy_load"):- collect_lazy_loads(walked, self._parsed_files, read_source=self._read_source)+ index = build_model_index(self._parsed_files, self._read_source)+ collect_lazy_loads(walked, self._parsed_files, read_source=self._read_source, index=index)+ with timed(timings, "analysis.health.small_tables"):+ try:+ settle_small_table_loops(walked, index)+ except Exception as exc: # the loops keep the magnitude the walk gave them+ log.debug("health_small_tables_failed", error=str(exc))def apply_crossfn(self, walked: list[tuple[Any, FileComplexity]]) -> None:"""Append cross-function and centrality-gated perf hits to each file, in place.diff --git a/packages/core/src/repowise/core/analysis/health/engine.py b/packages/core/src/repowise/core/analysis/health/engine.pyindex ea51af273..940ca780d 100644--- a/packages/core/src/repowise/core/analysis/health/engine.py+++ b/packages/core/src/repowise/core/analysis/health/engine.py@@ -98,6 +98,8 @@ log = structlog.get_logger(__name__)# Not a licence to move a calibrated scoring weight — those are frozen# independently of this stamp.#+# v41 (also): a loop that grows only by reading tables that do not grow with+# the repository is bounded (``perf.table_scale``), so its causes leave the queue.# v41 (also): ``docs`` / ``docs_src`` count as a docs root only at the# repository root or beside a docs-site generator's config, so a product's# ``app/docs/`` route is production and stored ``code_origin`` is re-read.diff --git a/packages/core/src/repowise/core/analysis/health/perf/actionability.py b/packages/core/src/repowise/core/analysis/health/perf/actionability.pyindex 78c2045fc..de18bd5cd 100644--- a/packages/core/src/repowise/core/analysis/health/perf/actionability.py+++ b/packages/core/src/repowise/core/analysis/health/perf/actionability.py@@ -29,11 +29,13 @@ OpportunityConfidence = Literal["high", "medium", "low"]# by its nature or for a reason :func:`expected_reason` reads off the group# (``gated_off``: a constant-false flag switches the function off;# ``cold_path``: it runs once per deploy, boot or incident;-# ``bounded_loop``: the loop runs a fixed number of times).+# ``bounded_loop``: the loop runs a fixed number of times; ``small_table``: it+# grows only by reading tables that do not grow with the repository).ActionabilityState = Literal["plan_ready", "advisory", "investigate", "expected"]#: Reasons :func:`expected_reason` gives that the default queue counts under-#: their own name, in precedence order; ``bounded_loop`` counts as ``expected``.+#: their own name, in precedence order; ``bounded_loop`` and ``small_table``+#: count as ``expected``.EXPECTED_REASONS: tuple[str, ...] = ("gated_off", "cold_path")# Refusals that are facts about the code, not missing proofs: nothing to investigate.@@ -313,8 +315,9 @@ def expected_reason(members: Sequence[Any], magnitude: str | None = None) -> strits own file switches off. ``cold_path``: every loop owner is named for amigration, startup, shutdown or crash recovery (:mod:`.cold_paths`), so itruns once per deploy, boot or incident. ``bounded_loop``: the group's loop- *magnitude* is proven bounded, so its cost does not grow. Reasons are- checked in precedence order.+ *magnitude* is proven bounded, so its cost does not grow; ``small_table``+ when a member is bounded because its loop reads only small tables+ (:mod:`.table_scale`). Reasons are checked in precedence order."""if not members:return None@@ -322,7 +325,10 @@ def expected_reason(members: Sequence[Any], magnitude: str | None = None) -> strreturn "gated_off"if all(is_cold_path(facts.file_path, facts.function_name) for facts in members):return "cold_path"- return "bounded_loop" if magnitude == "bounded" else None+ if magnitude != "bounded":+ return None+ small = any(facts.details.get("loop_small_tables") for facts in members)+ return "small_table" if small else "bounded_loop"def actionability(diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/iterables.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/iterables.pyindex 0af43c283..90a1d2e6a 100644--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/iterables.py+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/iterables.py@@ -11,11 +11,12 @@ stays ``unknown``, which no reader takes as bounded.from __future__ import annotations-from dataclasses import dataclass, field+from dataclasses import dataclass, field, replacefrom typing import TYPE_CHECKING, ClassVarfrom .. import loop_lexicon as lexfrom ..loop_facts import LoopSource+from ..table_scale import registry_sourcefrom .base import BasePerfDialectif TYPE_CHECKING:@@ -36,6 +37,12 @@ def text_of(node: Node | None) -> str:return (node.text or b"").decode("utf-8", "replace") if node is not None else ""+# Names a loop's header is followed through to the reads that feed it.+_TABLE_HOPS = 3+# What a loop walks when its rows are a read's: a header that mixes a read with+# anything else (``[*rows, *helper()]``) resolves to none of these.+_READ_SOURCES = frozenset({"query", "config_table", "rare_subset"})+# The reading recurses once per link of a header's call chain; a header nested# deeper than this (a generated chain hundreds of calls long) stays unknown._MAX_HEADER_DEPTH = 120@@ -127,6 +134,8 @@ class IterableSources(BasePerfDialect):conditional_kinds: frozenset[str] = frozenset()# ``None``: a dialect whose own syntax rules size a read (Python, TS).read_grows: LoopMagnitude | None = "grows_with_data"+ # Call nodes of a dialect that does not read calls through :meth:`call_parts`.+ read_call_kinds: frozenset[str] = frozenset()# Declarations whose ``body`` holds more file-level items (a C++ namespace),# and class-like declarations whose members a bare name in a method may read.file_container_kinds: frozenset[str] = frozenset()@@ -192,9 +201,10 @@ class IterableSources(BasePerfDialect):return parts[1].textreturn None- def reads_config_table(self, call: Node, nodes: list[Node]) -> bool:- """The read names only registry or configuration tables."""- return False+ def read_tables(self, call: Node, nodes: list[Node]) -> tuple[str, ...]:+ """The tables or ORM models the read *call* names, read off it and the+ values *nodes* its names are bound to; ``()`` when it names none."""+ return ()def is_cap(self, node: Node, loop: Node) -> bool:"""*node* caps a read at a fixed count (``Limit(10)``, ``setMaxResults(5)``)."""@@ -237,7 +247,61 @@ class IterableSources(BasePerfDialect):iterates, magnitude = self._iterable(node, loop, probe, hop=True)if iterates == "unknown" and magnitude is None:return None- return LoopSource(iterates, magnitude or "unknown")+ return self.with_read_tables(LoopSource(iterates, magnitude or "unknown"), node, loop, probe)++ def with_read_tables(+ self, source: LoopSource, expr: Node | None, loop: Node, probe: SinkProbe+ ) -> LoopSource:+ """*source*, with the tables its growth reads when it grows by walking a read."""+ if source.magnitude != "grows_with_data" or source.iterates not in _READ_SOURCES:+ return source+ return registry_source(replace(source, tables=self.growth_tables(expr, loop, probe)))++ def growth_tables(+ self, expr: Node | None, loop: Node, probe: SinkProbe+ ) -> tuple[str, ...] | None:+ """The tables named by the reads *expr* walks, followed through names bound+ before *loop*: ``()`` when one read names none, ``None`` with no read."""+ level = [expr] if expr is not None else []+ seen: set[bytes | None] = set()+ tables: set[str] = set()+ for _ in range(_TABLE_HOPS):+ found = self._read_level(level, loop, probe, seen, tables)+ if found is None:+ return ()+ level = found+ return tuple(sorted(tables)) or None++ def _read_level(+ self,+ level: list[Node],+ loop: Node,+ probe: SinkProbe,+ seen: set[bytes | None],+ tables: set[str],+ ) -> list[Node] | None:+ """Add the tables of the reads under *level* to *tables*, and return the+ values of the names it reads first; ``None`` when a read names no table."""+ values: list[Node] = []+ stack = list(level)+ while stack:+ node = stack.pop()+ if self._is_read(node, probe):+ named = self.read_tables(node, self._query_parts(node, loop)[0])+ if not named:+ return None+ tables.update(named)+ elif node.type in self.name_kinds:+ if node.text not in seen:+ seen.add(node.text)+ values.extend(filter(None, [self.reaching_value(loop, node.text or b"")]))+ elif node.type not in _BLOCKS:+ stack.extend(node.named_children)+ return values++ def _is_read(self, node: Node, probe: SinkProbe) -> bool:+ is_call = node.type in self.read_call_kinds or self.call_parts(node) is not None+ return is_call and (probe(node) in ("db", "network") or self.query_chain(node))def _unwrap(self, node: Node) -> Node:while node.type in self.wrapper_kinds:@@ -385,7 +449,8 @@ class IterableSources(BasePerfDialect):"""Rows of one key, a capped read, a rare state, or rows that grow; thenames the call reads may be bound to their statement before the loop."""nodes, text = self._query_parts(call, loop)- if self.reads_config_table(call, nodes):+ tables = self.read_tables(call, nodes)+ if tables and all(lex.is_config_table(t) for t in tables):return "config_table", Noneif lex.is_key_lookup(text):return "caller_key", "bounded"diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/python_sources.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/python_sources.pyindex 7a947e9a7..10202fe7a 100644--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/python_sources.py+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/python_sources.py@@ -43,6 +43,8 @@ if TYPE_CHECKING:class PythonLoopSources:"""Loop sources and magnitudes. Mixed into :class:`~.python.PythonPerfDialect`."""+ read_call_kinds = frozenset({"call"})+def loop_source(self, loop: Node, probe: SinkProbe) -> LoopSource | None:if loop.type != "for_statement":return None@@ -51,7 +53,7 @@ class PythonLoopSources:iterates, claim = self._source(right, loop, probe, hop=True)if iterates == "unknown" and baseline == "unknown":return None- return LoopSource(iterates, claim or baseline)+ return self.with_read_tables(LoopSource(iterates, claim or baseline), right, loop, probe)def _source(self, expr: Node | None, loop: Node, probe: SinkProbe, *, hop: bool) -> Source:"""What *expr* walks, and the magnitude that settles when it differs from@@ -161,39 +163,21 @@ class PythonLoopSources:return self._source(inner[0], loop, probe, hop=hop)return "unknown", None- def reads_config_table(self, call: Node, nodes: list[Node]) -> bool:- """Every SQL text and selected model names a registry or configuration table."""+ def read_tables(self, call: Node, nodes: list[Node]) -> tuple[str, ...]:+ """The tables its SQL texts read and every model-like name in the statement+ (``select(Repository)``, ``File.repository_id`` in a filter); ``()`` when a+ SQL text names no table or more than its FROM / JOIN list shows."""walked = [n for part in nodes for n in self._walk(part)]sql = [n.text or b""for n in walkedif n.type == "string" and _PY_SQL_LITERAL_RE.match(_text(n))]- models = self._query_models(walked)- return bool(sql or models) and (- all(lex.is_config_sql(text) for text in sql)- and all(lex.is_config_model(model) for model in models)- )-- def _query_models(self, walked: list[Node]) -> list[str]:- return [- model- for n in walked- if n.type == "call" and self.callee_method_name(n) in ("select", "query")- for model in self._selected_models(n)- ]-- def _selected_models(self, select: Node) -> list[str]:- """``select(Repository)`` / ``query(Repository.id, Repository.path)`` -> the model- names; ``func.count()`` and other expressions name none."""- args = select.child_by_field_name("arguments")- models: list[str] = []- for arg in (c for c in args.children if c.is_named) if args is not None else ():- path = self._dotted_path(arg) or ""- head = next((seg for seg in path.split(".") if _PY_MODEL_NAME_RE.match(seg)), None)- if head is not None:- models.append(head)- return models+ tables = [lex.read_sql_tables(text) for text in sql]+ if not all(tables):+ return ()+ models = [_text(n) for n in walked if n.type == "identifier" and _PY_MODEL_NAME_RE.match(_text(n))]+ return tuple(dict.fromkeys([*(t for found in tables for t in found), *models]))def _literal_constant(self, expr: Node, name: bytes) -> bool:"""``self._TABLES`` where the file binds ``_TABLES`` once, to a literal collection."""diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/ts_js.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/ts_js.pyindex 5aeede7ec..dea7aa386 100644--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/ts_js.py+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/ts_js.py@@ -324,6 +324,7 @@ def _sole_where_pair(call: Node) -> tuple[Node, Node] | None:class TsJsPerfDialect(TsJsMarkerHooks, IterableSources):language = "typescript"+ read_call_kinds = frozenset({"call_expression"})markers = frozenset({"io_in_loop",@@ -526,7 +527,8 @@ class TsJsPerfDialect(TsJsMarkerHooks, IterableSources):iterates, claim = self._source(right, loop, probe, hop=True)if iterates == "unknown" and baseline is None:return None- return LoopSource(iterates, claim or baseline or "unknown")+ source = LoopSource(iterates, claim or baseline or "unknown")+ return self.with_read_tables(source, right, loop, probe)def _expr_magnitude(self, expr: Node | None, loop: Node, probe: SinkProbe@@ -618,17 +620,18 @@ class TsJsPerfDialect(TsJsMarkerHooks, IterableSources):return self._source(first, loop, probe, hop=hop)return self._source(receiver, loop, probe, hop=hop)- def reads_config_table(self, call: Node, nodes: list[Node]) -> bool:- """Every table the read names is a registry (``FROM repos``), or with no SQL,- the delegate does (``prisma.repository.findMany()``)."""- sql = [n.text or b"" for n in self._walk(call) if n.type in ("string", "template_string")]- sql = [text for text in sql if lex.sql_tables(text)]- if sql:- return all(lex.is_config_sql(text) for text in sql)+ def read_tables(self, call: Node, nodes: list[Node]) -> tuple[str, ...]:+ """The tables the read's SQL names (``FROM repos``), or with no SQL, its+ delegate's model (``prisma.repository.findMany()`` -> ``Repository``)."""+ texts = [n.text or b"" for n in self._walk(call) if n.type in ("string", "template_string")]+ texts = [text for text in texts if lex.sql_tables(text)]+ if texts:+ tables = [lex.read_sql_tables(text) for text in texts]+ return tuple(dict.fromkeys(t for found in tables for t in found)) if all(tables) else ()model = _callee_object(call)prop = model.child_by_field_name("property") if model is not None else Nonedelegate = _text(prop) if prop is not None and model.type == "member_expression" else ""- return bool(delegate) and lex.is_config_model(delegate[:1].upper() + delegate[1:])+ return (delegate[:1].upper() + delegate[1:],) if delegate else ()def _awaited_wrapper_call(self, closure: Node) -> Node | None:"""The awaited call *closure* is passed to (``await limit(() => f(x))``).diff --git a/packages/core/src/repowise/core/analysis/health/perf/lazy_load.py b/packages/core/src/repowise/core/analysis/health/perf/lazy_load.pyindex 2a5819942..be1a61157 100644--- a/packages/core/src/repowise/core/analysis/health/perf/lazy_load.py+++ b/packages/core/src/repowise/core/analysis/health/perf/lazy_load.py@@ -97,13 +97,16 @@ def collect_lazy_loads(walked: Iterable[tuple[Any, FileComplexity]],parsed_files: Iterable[Any],read_source: SourceReader | None = None,+ index: ModelIndex | None = None,) -> None:"""Append hits to each walked file's ``perf_hits``; one file failing skips only it.- The Java repository/mapper pass rides this ORM entry point."""+ The Java repository/mapper pass rides this ORM entry point. *index* is the+ repo's model index when the caller already built it."""# Both passes read both inputs, so a one-shot iterable is materialized once.walked, parsed_files = list(walked), list(parsed_files)reader = read_source or _disk_read- index = build_model_index(parsed_files, reader)+ if index is None:+ index = build_model_index(parsed_files, reader)for pf, fcx in walked:try:fcx.perf_hits.extend(_file_hits(pf, reader, index))diff --git a/packages/core/src/repowise/core/analysis/health/perf/loop_facts.py b/packages/core/src/repowise/core/analysis/health/perf/loop_facts.pyindex d1e2219b9..6591d8a46 100644--- a/packages/core/src/repowise/core/analysis/health/perf/loop_facts.py+++ b/packages/core/src/repowise/core/analysis/health/perf/loop_facts.py@@ -54,6 +54,10 @@ class LoopSource:iterates: LoopIterates = "unknown"magnitude: LoopMagnitude = "unknown"+ # The tables or ORM models named by the reads that make it grow: empty when+ # a read names none, ``None`` when no read was found (:mod:`.table_scale`+ # judges their size once every file is read).+ tables: tuple[str, ...] | None = None@dataclass(frozen=True, slots=True)@@ -80,6 +84,13 @@ class LoopFacts:# What the innermost loop walks, and why its calls cannot be one batch.iterates: LoopIterates = "unknown"not_batchable: NotBatchable | None = None+ # Every growing enclosing loop grows only by reading these tables, and+ # ``small_magnitude`` is the magnitude if none of them scales with the repo+ # (:func:`.table_scale.settle_small_table_loops`); ``small_tables`` is set+ # once that holds.+ read_tables: tuple[str, ...] = ()+ small_magnitude: LoopMagnitude = "unknown"+ small_tables: tuple[str, ...] = ()def as_details(self) -> dict[str, Any]:"""Finding ``details`` keys; defaults are omitted so old findings are unchanged."""@@ -99,6 +110,8 @@ class LoopFacts:details["loop_iterates"] = self.iteratesif self.not_batchable:details["loop_not_batchable"] = self.not_batchable+ if self.small_tables:+ details["loop_small_tables"] = list(self.small_tables)return detailsdiff --git a/packages/core/src/repowise/core/analysis/health/perf/loop_lexicon.py b/packages/core/src/repowise/core/analysis/health/perf/loop_lexicon.pyindex d6031f4f4..71ace3bb6 100644--- a/packages/core/src/repowise/core/analysis/health/perf/loop_lexicon.py+++ b/packages/core/src/repowise/core/analysis/health/perf/loop_lexicon.py@@ -117,9 +117,11 @@ def is_config_name(name: str, *, callee: bool = False) -> bool:return len(ws) >= 2 and ws[-1] in _CONFIG_LISTED and ws[-2] in singulars-def is_config_model(name: str) -> bool:- """An ORM model (last dotted segment) that is a registry or configuration table."""- return name.rsplit(".", 1)[-1] in _CONFIG_MODELS+def is_config_table(name: str) -> bool:+ """An ORM model (last dotted segment) or SQL table named for a registry or+ configuration table."""+ last = name.rsplit(".", 1)[-1]+ return last in _CONFIG_MODELS or last.lower() in _CONFIG_TABLESdef sql_tables(sql: bytes) -> list[str]:@@ -127,9 +129,16 @@ def sql_tables(sql: bytes) -> list[str]:return [m.group(1).decode().rsplit(".", 1)[-1].lower() for m in _SQL_FROM.finditer(sql)]-def is_config_sql(sql: bytes) -> bool:- tables = sql_tables(sql)- return bool(tables) and all(t in _CONFIG_TABLES for t in tables)+# ``FROM a, b`` / ``FROM a x, b`` and a subquery read tables :func:`sql_tables` misses.+_SQL_HIDDEN_TABLES = re.compile(+ rb"\bFROM\s+[\w.\"`\[\]]+(?:\s+(?:AS\s+)?\w+)?\s*,|\(\s*SELECT\b", re.IGNORECASE+)+++def read_sql_tables(sql: bytes) -> list[str]:+ """:func:`sql_tables`, or ``[]`` when the text reads tables its FROM / JOIN+ list does not name one by one."""+ return [] if _SQL_HIDDEN_TABLES.search(sql) else sql_tables(sql)def is_key_lookup(text: bytes) -> bool:@@ -186,13 +195,13 @@ def names_repo_root(name: str) -> bool:__all__ = ["has_sql_limit",- "is_config_model","is_config_name",- "is_config_sql",+ "is_config_table","is_key_lookup","is_parent_key_filter","is_rare_filter","names_repo_root",+ "read_sql_tables","sql_tables","words",]diff --git a/packages/core/src/repowise/core/analysis/health/perf/orm_models.py b/packages/core/src/repowise/core/analysis/health/perf/orm_models.pyindex e27da3e40..939f4dfc5 100644--- a/packages/core/src/repowise/core/analysis/health/perf/orm_models.py+++ b/packages/core/src/repowise/core/analysis/health/perf/orm_models.py@@ -32,6 +32,9 @@ _RELATION_HINTS: tuple[bytes, ...] = (# A manager or queryset class that eager-loads is found by these, even with no relation._MANAGER_HINTS: tuple[bytes, ...] = (b"get_queryset",)+# A file declaring SQLAlchemy columns is read for its tables' columns alone.+_COLUMN_HINTS: tuple[bytes, ...] = (b"mapped_column(", b"Column(")+_COLUMN_CALLS = frozenset({"mapped_column", "Column"})_EAGER_QUERYSET_CALLS = frozenset({"select_related", "prefetch_related"})_SA_LAZY_TRUE_STRINGS = frozenset({"select", "dynamic"})@@ -62,6 +65,11 @@ class ModelIndex:# Models whose ``objects`` manager (or queryset) class, defined in this repo, has a# ``get_queryset`` that select_related / prefetch_related: the caller's chain hides it.custom_managers: frozenset[str] = frozenset()+ # SQLAlchemy models by class name: their column names, ``__tablename__`` and the+ # tables (or models) their ``ForeignKey`` columns point at, lower-cased.+ columns: dict[str, frozenset[str]] = field(default_factory=dict)+ table_names: dict[str, str] = field(default_factory=dict)+ foreign_tables: dict[str, frozenset[str]] = field(default_factory=dict)@dataclass@@ -70,6 +78,9 @@ class _RawClass:bases: list[str] = field(default_factory=list)manager_names: set[str] = field(default_factory=set)eager_queryset: bool = False+ columns: set[str] = field(default_factory=set)+ table: str | None = None+ foreign_tables: set[str] = field(default_factory=set)# -- literal readers -----------------------------------------------------------@@ -329,6 +340,47 @@ def _django_relation_from_assignment(return attr, relation, pending+# -- SQLAlchemy columns ------------------------------------------------------------+++def _foreign_target(call: Node) -> str | None:+ """``ForeignKey("repositories.id")`` -> ``repositories``; ``ForeignKey(Repo.id)`` -> ``repo``."""+ positional, _ = _call_args(call)+ arg = positional[0] if positional else None+ text = _string_content(arg)+ if text is None and arg is not None and arg.type == "attribute":+ text = _node_text(arg)+ parts = (text or "").split(".")+ return parts[-2].lower() if len(parts) >= 2 and parts[-2] else None+++def _record_column(stmt: Node, rc: _RawClass) -> None:+ """``__tablename__ = "t"``, ``name = Column(...)``, ``name: Mapped[str] =+ mapped_column(...)`` or a bare ``name: Mapped[str]`` into *rc*."""+ attr = ident(stmt.child_by_field_name("left"))+ right = stmt.child_by_field_name("right")+ if attr == "__tablename__":+ rc.table = _string_content(right) or rc.table+ elif attr is None:+ return+ elif right is None:+ if _mapped_info(stmt.child_by_field_name("type"))[0]:+ rc.columns.add(attr)+ elif right.type == "call" and _callee_last_segment(right) in _COLUMN_CALLS:+ positional, _ = _call_args(right)+ rc.columns.add((_string_content(positional[0]) if positional else None) or attr)+ rc.foreign_tables.update(_foreign_targets(right))+++def _foreign_targets(column: Node) -> set[str]:+ return {+ target+ for node in walk(column)+ if node.type == "call" and _callee_last_segment(node) == "ForeignKey"+ if (target := _foreign_target(node))+ }++# -- per-file scan + cross-file merge ---------------------------------------------@@ -366,6 +418,8 @@ def _scan_class_body(node: Node, name: str, is_django_file: bool, pending: list)else raw_stmt)result = None+ if stmt.type == "assignment":+ _record_column(stmt, rc)if stmt.type == "assignment" and (names := _manager_names(stmt)):rc.manager_names |= nameselif stmt.type == "assignment":@@ -409,6 +463,9 @@ def _scan_file(existing.bases.extend(rc.bases)existing.manager_names |= rc.manager_namesexisting.eager_queryset |= rc.eager_queryset+ existing.columns |= rc.columns+ existing.table = existing.table or rc.table+ existing.foreign_tables |= rc.foreign_tablesreturn classes@@ -455,19 +512,31 @@ def _resolve_inherited(def build_model_index(parsed_files: Iterable[Any], read_source: SourceReader) -> ModelIndex:raw: dict[str, list[_RawClass]] = {}+ schema: dict[str, list[_RawClass]] = {}pending: list[tuple[str, str, Relation]] = []for pf in parsed_files:info = pf.file_infoif info.language != "python":continuesource = read_source(info.abs_path)- if source is None or not any(h in source for h in _RELATION_HINTS + _MANAGER_HINTS):+ if source is None:+ continue+ # Only a file with relations or managers feeds the lazy-load index; one that+ # only declares columns feeds the schema.+ related = any(h in source for h in _RELATION_HINTS + _MANAGER_HINTS)+ if not related and not any(h in source for h in _COLUMN_HINTS):continue- classes = _scan_file(info.abs_path, info.language, source, pending)+ try:+ classes = _scan_file(info.abs_path, info.language, source, pending)+ except Exception: # one unreadable model file costs its own classes only+ classes = Noneif classes is None:continuefor name, rc in classes.items():- raw.setdefault(name, []).append(rc)+ if related:+ raw.setdefault(name, []).append(rc)+ if rc.columns or rc.table:+ schema.setdefault(name, []).append(rc)for target, attr, relation in pending:for rc in raw.get(target, ()):rc.relations.setdefault(attr, relation)@@ -478,9 +547,39 @@ def build_model_index(parsed_files: Iterable[Any], read_source: SourceReader) ->relations=resolved,models=frozenset(merged),custom_managers=_with_custom_manager(raw, merged),+ **_schema_maps(schema),)+def _inherited(+ name: str, own: dict[str, set[str]], bases: dict[str, set[str]], seen: set[str]+) -> set[str]:+ """What *name* and the bases it reaches declare in *own*; cycle-safe."""+ seen.add(name)+ found = set(own.get(name, ()))+ for base in bases.get(name, ()):+ if base not in seen:+ found |= _inherited(base, own, bases, seen)+ return found+++def _schema_maps(schema: dict[str, list[_RawClass]]) -> dict[str, Any]:+ """The column, table-name and foreign-key maps of :class:`ModelIndex`. Two+ classes of one name pool what they declare, and a class takes what the+ bases (mixins) the schema holds declare: more key columns only ever make a+ table read as larger, never smaller."""+ bases = {n: {b for rc in rcs for b in rc.bases if b != n} for n, rcs in schema.items()}++ columns = {n: {c for rc in rcs for c in rc.columns} for n, rcs in schema.items()}+ foreign = {n: {t for rc in rcs for t in rc.foreign_tables} for n, rcs in schema.items()}+ tables = {n: next((rc.table for rc in rcs if rc.table), None) for n, rcs in schema.items()}+ return {+ "columns": {n: frozenset(_inherited(n, columns, bases, set())) for n in schema},+ "table_names": {n: t for n, t in tables.items() if t},+ "foreign_tables": {n: frozenset(_inherited(n, foreign, bases, set())) for n in schema},+ }++def _with_custom_manager(raw: dict[str, list[_RawClass]], merged: _Merged) -> frozenset[str]:"""Models whose ``objects`` class eager-loads in ``get_queryset``, or inherit one."""eager = {name for name, rcs in raw.items() if any(rc.eager_queryset for rc in rcs)}diff --git a/packages/core/src/repowise/core/analysis/health/perf/table_scale.py b/packages/core/src/repowise/core/analysis/health/perf/table_scale.pynew file mode 100644index 000000000..ff7dc325f--- /dev/null+++ b/packages/core/src/repowise/core/analysis/health/perf/table_scale.py@@ -0,0 +1,198 @@+"""Whether the tables a loop reads grow with the repository, and what that does+to the loop's magnitude.++A loop over rows grows with the data only when the table it reads does. In a+schema keyed by code artefacts (files, commits, graph nodes, symbols), a table+that carries one of those keys, or a foreign key to a table that does, grows+with the repository; one that carries none (registered repositories, decision+records, conversations) stays small however often a loop walks it. A loop that+grows only by reading small tables is bounded, and says which tables+(``loop_small_tables``). Nothing here ever makes a loop grow.++Ceiling: the key vocabulary names code artefacts, so the column rule applies+only to a schema whose tables carry keys of at least two of those families.+Elsewhere, and for a table whose columns the repo does not declare, only a+registry name (:func:`.loop_lexicon.is_config_table`) speaks, and it leaves+the loop unmeasured rather than bounded: a name says what is walked, not how+many rows. Upgrade path: per-domain key vocabularies, or row counts read from+a live database.+"""++from __future__ import annotations++from dataclasses import dataclass, replace+from typing import TYPE_CHECKING, Any, Literal++from . import loop_lexicon as lex+from .loop_facts import LoopSource, combined_magnitude++if TYPE_CHECKING:+ from collections.abc import Iterable++ from .loop_facts import LoopMagnitude+ from .orm_models import ModelIndex++TableClass = Literal["repo_scale", "small", "registry"]++# Key columns by family: exact names, then suffixes (``source_node_id``).+_KEY_FAMILIES: dict[str, tuple[frozenset[str], tuple[str, ...]]] = {+ "file": (+ frozenset({"file_path", "target_path", "source_path", "document_path"}),+ ("_file_path",),+ ),+ # A change keyed by its head or base commit is one row per change, not per repo.+ "commit": (frozenset({"sha", "commit_sha", "head_sha", "base_sha"}), ("_commit_sha",)),+ "symbol": (frozenset({"node_id", "symbol_id", "symbol"}), ("_node_id", "_symbol_id")),+}+_MIN_FAMILIES = 2+# Columns that may key a row per file, commit or symbol under another spelling:+# a table carrying one is of unknown size, never small.+_MAYBE_KEYS = frozenset(+ {+ "path",+ "file",+ "filename",+ "file_name",+ "rel_path",+ "relative_path",+ "file_id",+ "hash",+ "commit_hash",+ "commit_id",+ "revision",+ "symbol_name",+ "qualified_name",+ "module_path",+ }+)+++def _family(column: str) -> str | None:+ name = column.lower()+ for family, (exact, suffixes) in _KEY_FAMILIES.items():+ if name in exact or name.endswith(suffixes):+ return family+ return None+++@dataclass(frozen=True, slots=True)+class TableScales:+ """Lower-cased model and table names -> ``repo_scale``, ``small`` or ``None``+ (declared, but its size cannot be told)."""++ scale: dict[str, TableClass | None]++ def classify(self, name: str) -> TableClass | None:+ key = name.rsplit(".", 1)[-1].lower()+ if key in self.scale:+ return self.scale[key]+ return "registry" if lex.is_config_table(name) else None+++def _closure(seed: set[str], foreign: dict[str, set[str | None]]) -> set[str]:+ """*seed* and every table whose foreign keys reach one in it."""+ found, changed = set(seed), True+ while changed:+ changed = False+ for name, targets in foreign.items():+ if name not in found and found.intersection(targets):+ found.add(name)+ changed = True+ return found+++def table_scales(index: ModelIndex) -> TableScales:+ """Each declared model's class, read once per repo; empty off a code-artefact schema.++ A child of a growing table grows with it. A table with no column read, a+ column that may be a key under another name, or a foreign key to a table+ the repo does not declare is of unknown size, and so is a child of one."""+ keyed = {+ name: {f for column in columns if (f := _family(column))}+ for name, columns in index.columns.items()+ }+ if len(set().union(*keyed.values())) < _MIN_FAMILIES:+ return TableScales({})+ aliases = {name.lower(): name for name in index.columns}+ aliases.update({table.lower(): name for name, table in index.table_names.items()})+ # ``None`` stands for a target the repo does not declare.+ foreign = {n: {aliases.get(t) for t in targets} for n, targets in index.foreign_tables.items()}+ grows = _closure({n for n, families in keyed.items() if families}, foreign)+ unsure = {+ n+ for n, columns in index.columns.items()+ if not columns or {c.lower() for c in columns} & _MAYBE_KEYS or None in foreign.get(n, ())+ }+ unknown = _closure(unsure, foreign) - grows+ return TableScales(+ {+ alias: "repo_scale" if name in grows else None if name in unknown else "small"+ for alias, name in aliases.items()+ }+ )+++def registry_source(source: LoopSource) -> LoopSource:+ """A registry-named loop whose growth rests on a read that names no table:+ the name alone proves no growth, so the loop is unmeasured."""+ if (+ source.iterates == "config_table"+ and source.magnitude == "grows_with_data"+ and source.tables == ()+ ):+ return replace(source, magnitude="unknown")+ return source+++def small_table_candidate(sources: list[LoopSource]) -> tuple[tuple[str, ...], LoopMagnitude]:+ """``(tables, magnitude if they are small)`` when every growing loop grows only+ by reading named tables; ``((), "unknown")`` otherwise."""+ growing = [s for s in sources if s.magnitude == "grows_with_data"]+ if not growing or not all(s.tables for s in growing):+ return (), "unknown"+ rest = [s for s in sources if s.magnitude != "grows_with_data"]+ tables = tuple(sorted({t for s in growing for t in s.tables or ()}))+ return tables, combined_magnitude([*rest, LoopSource(magnitude="bounded")])+++def _verdict(tables: tuple[str, ...], scales: TableScales) -> TableClass | None:+ """``small`` when every table is, ``registry`` when each is small or only+ registry-named, else ``None`` (one grows, or one is unknown)."""+ found = {scales.classify(t) for t in tables}+ if None in found or "repo_scale" in found:+ return None+ return "small" if found == {"small"} else "registry"+++def settle_small_table_loops(walked: Iterable[tuple[Any, Any]], index: ModelIndex) -> None:+ """Re-judge, in place, every hit whose loop grows only by reading tables.++ The schema is read from Python models only, so a loop in another language+ is judged by registry names alone: a TS ``prisma.repository`` is not the+ Python ``Repository``."""+ scales, names_only = table_scales(index), TableScales({})+ for pf, fcx in walked:+ if any(h.loop is not None and h.loop.read_tables for h in fcx.perf_hits):+ judge = scales if pf.file_info.language == "python" else names_only+ fcx.perf_hits = [_settled(hit, judge) for hit in fcx.perf_hits]+++def _settled(hit: Any, scales: TableScales) -> Any:+ loop = hit.loop+ verdict = _verdict(loop.read_tables, scales) if loop is not None and loop.read_tables else None+ if verdict is None:+ return hit+ if verdict == "registry":+ return replace(hit, loop=replace(loop, magnitude="unknown"))+ small = replace(loop, magnitude=loop.small_magnitude, small_tables=loop.read_tables)+ return replace(hit, loop=small)+++__all__ = [+ "TableClass",+ "TableScales",+ "registry_source",+ "settle_small_table_loops",+ "small_table_candidate",+ "table_scales",+]diff --git a/packages/core/src/repowise/core/analysis/health/walk_cache.py b/packages/core/src/repowise/core/analysis/health/walk_cache.pyindex b06f68683..53d8dd63d 100644--- a/packages/core/src/repowise/core/analysis/health/walk_cache.py+++ b/packages/core/src/repowise/core/analysis/health/walk_cache.py@@ -29,7 +29,7 @@ log = structlog.get_logger(__name__)_CACHE_FILENAME = "health_walk_cache.pkl"#: Bump when the walker's output for the same bytes changes.-_CACHE_VERSION = 1+_CACHE_VERSION = 2class HealthWalkCache:diff --git a/tests/unit/health/test_perf_table_scale.py b/tests/unit/health/test_perf_table_scale.pynew file mode 100644index 000000000..8129dd685--- /dev/null+++ b/tests/unit/health/test_perf_table_scale.py@@ -0,0 +1,273 @@+"""Whether the tables a loop reads grow with the repository (``perf.table_scale``).++Models are declared in one file, the loop is walked in another, and the+post-walk pass judges the loop with the repo's whole schema, as on an index.+"""++from __future__ import annotations++import textwrap+from dataclasses import dataclass+from pathlib import Path++import pytest++from repowise.core.analysis.health.complexity import walk_file+from repowise.core.analysis.health.perf.actionability import expected_reason+from repowise.core.analysis.health.perf.orm_models import build_model_index+from repowise.core.analysis.health.perf.table_scale import (+ settle_small_table_loops,+ table_scales,+)++_CODE_SCHEMA = """+from sqlalchemy import Column, ForeignKey, String+from sqlalchemy.orm import Mapped, mapped_column++class Repository(Base):+ __tablename__ = "repositories"+ id: Mapped[str] = mapped_column(String, primary_key=True)+ local_path: Mapped[str] = mapped_column(String)+ head_commit: Mapped[str] = mapped_column(String)++class Decision(Base):+ __tablename__ = "decisions"+ id: Mapped[str] = mapped_column(String, primary_key=True)+ repository_id: Mapped[str] = mapped_column(String, ForeignKey("repositories.id"))++class GraphNode(Base):+ __tablename__ = "graph_nodes"+ node_id: Mapped[str] = mapped_column(String)+ file_path: Mapped[str] = mapped_column(String)++class GitCommit(Base):+ __tablename__ = "git_commits"+ sha = Column(String)++class Page(Base):+ __tablename__ = "wiki_pages"+ target_path: Mapped[str] = mapped_column(String)++class PageVersion(Base):+ __tablename__ = "wiki_page_versions"+ page_id: Mapped[str] = mapped_column(String, ForeignKey("wiki_pages.id"))+"""++# One key family only (a workflow ``node_id``): the column rule does not apply.+_APP_SCHEMA = """+from sqlalchemy import ForeignKey, String+from sqlalchemy.orm import Mapped, mapped_column++class Repository(Base):+ __tablename__ = "repositories"+ id: Mapped[str] = mapped_column(String, primary_key=True)++class Order(Base):+ __tablename__ = "orders"+ id: Mapped[str] = mapped_column(String, primary_key=True)++class NodeRun(Base):+ __tablename__ = "node_runs"+ node_id: Mapped[str] = mapped_column(String)+"""++_LOOP = """+from sqlalchemy import select+async def check(session):+ result = await session.execute(select(MODEL))+ rows = result.scalars().all()+ for row in rows:+ await session.execute(select(Page).where(Page.repository_id == row.id))+"""+++@dataclass+class _FileInfo:+ path: str+ abs_path: str+ language: str+++@dataclass+class _ParsedFile:+ file_info: _FileInfo+++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")+++def _index(tmp_path: Path, schema: str):+ path = tmp_path / "models.py"+ path.write_text(textwrap.dedent(schema), encoding="utf-8")+ pf = _ParsedFile(_FileInfo(path="models.py", abs_path=str(path), language="python"))+ return build_model_index([pf], lambda p: Path(p).read_bytes())+++def _settled_loop(tmp_path: Path, schema: str, src: str):+ _require_python()+ fcx = walk_file("loop.py", "python", textwrap.dedent(src).encode())+ pf = _ParsedFile(_FileInfo(path="loop.py", abs_path="loop.py", language="python"))+ settle_small_table_loops([(pf, fcx)], _index(tmp_path, schema))+ return next(h for h in fcx.perf_hits if h.kind == "io_in_loop").loop+++def test_tables_with_a_code_artefact_key_or_a_parent_that_has_one_grow(tmp_path: Path) -> None:+ _require_python()+ scales = table_scales(_index(tmp_path, _CODE_SCHEMA))+ for name in ("GraphNode", "graph_nodes", "GitCommit", "Page", "PageVersion"):+ assert scales.classify(name) == "repo_scale", name+ for name in ("Repository", "repositories", "Decision"):+ assert scales.classify(name) == "small", name+++def test_a_schema_with_one_key_family_falls_back_to_registry_names(tmp_path: Path) -> None:+ _require_python()+ scales = table_scales(_index(tmp_path, _APP_SCHEMA))+ assert scales.classify("Order") is None+ assert scales.classify("NodeRun") is None+ assert scales.classify("Repository") == "registry"+++_UNSURE_TABLES = """+class Upload(Base):+ __tablename__ = "uploads"+ path: Mapped[str] = mapped_column(String)++class Bare(Base):+ __tablename__ = "bare"++class Child(Base):+ __tablename__ = "children"+ other_id: Mapped[str] = mapped_column(String, ForeignKey("elsewhere.id"))++class Grandchild(Base):+ __tablename__ = "grandchildren"+ child_id: Mapped[str] = mapped_column(String, ForeignKey("children.id"))+"""+++def test_a_table_whose_size_cannot_be_told_is_never_small(tmp_path: Path) -> None:+ # A key under another spelling, no column read, or a parent the repo does not declare.+ _require_python()+ scales = table_scales(_index(tmp_path, _CODE_SCHEMA + _UNSURE_TABLES))+ for name in ("Upload", "Bare", "Child", "Grandchild"):+ assert scales.classify(name) is None, name+++def test_a_typescript_loop_is_judged_by_registry_names_alone(tmp_path: Path) -> None:+ _require_python()+ src = """+ async function f(prisma) {+ const rows = await prisma.repository.findMany();+ for (const r of rows) { await prisma.page.findMany({ where: { repoId: r.id } }); }+ }+ """+ fcx = walk_file("loop.ts", "typescript", textwrap.dedent(src).encode())+ pf = _ParsedFile(_FileInfo(path="loop.ts", abs_path="loop.ts", language="typescript"))+ settle_small_table_loops([(pf, fcx)], _index(tmp_path, _CODE_SCHEMA))+ facts = next(h for h in fcx.perf_hits if h.kind == "io_in_loop").loop+ assert facts.magnitude == "unknown"+ assert "loop_small_tables" not in facts.as_details()+++def test_a_comma_join_in_raw_sql_names_no_tables_one_by_one(tmp_path: Path) -> None:+ src = """+ from sqlalchemy import text+ async def check(session):+ result = await session.execute(text("SELECT r.id FROM repositories r, graph_nodes n"))+ rows = result.all()+ for row in rows:+ await session.execute(select(Page).where(Page.repository_id == row.id))+ """+ assert _settled_loop(tmp_path, _CODE_SCHEMA, src).magnitude == "grows_with_data"+++def test_a_filter_on_a_growing_model_keeps_the_loop_growing(tmp_path: Path) -> None:+ src = _LOOP.replace("select(MODEL)", "select(Repository).where(Repository.name == GraphNode.repo_name)")+ assert _settled_loop(tmp_path, _CODE_SCHEMA, src).magnitude == "grows_with_data"+++def test_a_loop_over_a_small_table_is_bounded_and_names_it(tmp_path: Path) -> None:+ facts = _settled_loop(tmp_path, _CODE_SCHEMA, _LOOP.replace("MODEL", "Repository"))+ assert facts.magnitude == "bounded"+ assert facts.as_details()["loop_small_tables"] == ["Repository"]+++def test_a_loop_over_a_table_that_grows_with_the_repo_still_grows(tmp_path: Path) -> None:+ facts = _settled_loop(tmp_path, _CODE_SCHEMA, _LOOP.replace("MODEL", "PageVersion"))+ assert facts.magnitude == "grows_with_data"+ assert "loop_small_tables" not in facts.as_details()+++def test_a_join_to_a_growing_table_keeps_the_loop_growing(tmp_path: Path) -> None:+ src = _LOOP.replace("select(MODEL)", "select(Repository).join(GraphNode)")+ assert _settled_loop(tmp_path, _CODE_SCHEMA, src).magnitude == "grows_with_data"+++def test_outside_a_code_schema_only_a_registry_name_speaks_and_it_proves_no_size(+ tmp_path: Path,+) -> None:+ assert (+ _settled_loop(tmp_path, _APP_SCHEMA, _LOOP.replace("MODEL", "Order")).magnitude+ == "grows_with_data"+ )+ registry = _settled_loop(tmp_path, _APP_SCHEMA, _LOOP.replace("MODEL", "Repository"))+ assert registry.magnitude == "unknown"+ assert "loop_small_tables" not in registry.as_details()+++def test_an_outer_loop_that_grows_by_itself_keeps_the_hit_growing(tmp_path: Path) -> None:+ src = """+ import os+ from sqlalchemy import select+ async def check(session, root):+ for path in os.walk(root):+ result = await session.execute(select(Repository))+ rows = result.scalars().all()+ for row in rows:+ await session.execute(select(Page).where(Page.repository_id == row.id))+ """+ assert _settled_loop(tmp_path, _CODE_SCHEMA, src).magnitude != "bounded"+++def test_a_registry_named_loop_over_a_read_that_names_no_table_is_unmeasured() -> None:+ _require_python()+ src = """+ from sqlalchemy import select+ async def check(session, stmt):+ result = await session.execute(stmt)+ repos = result.scalars().all()+ for repo in repos:+ await session.execute(select(Page).where(Page.repository_id == repo.id))+ """+ fcx = walk_file("loop.py", "python", textwrap.dedent(src).encode())+ facts = next(h for h in fcx.perf_hits if h.kind == "io_in_loop").loop+ assert facts.iterates == "config_table"+ assert facts.magnitude == "unknown"+++def test_column_only_model_files_do_not_widen_the_lazy_load_index(tmp_path: Path) -> None:+ _require_python()+ index = _index(tmp_path, _APP_SCHEMA.replace("ForeignKey, ", ""))+ assert index.models == frozenset()+ assert "Order" in index.columns+++@dataclass+class _Member:+ details: dict+ file_path: str = "app/store.py"+ function_name: str = "check"+++def test_a_bounded_group_over_small_tables_says_so() -> None:+ small = _Member({"loop_magnitude": "bounded", "loop_small_tables": ["Repository"]})+ plain = _Member({"loop_magnitude": "bounded"})+ assert expected_reason([small, plain], "bounded") == "small_table"+ assert expected_reason([plain], "bounded") == "bounded_loop"