Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(perf): a read filtered by a key set is bounded by the keys (#3535)
Raghav Chamadiya70797068af66
diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/base.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/base.pyindex 744741739..584ff21d8 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@@ -891,6 +891,13 @@ class BasePerfDialect:"""return frozenset()+ def read_key_column(self, call: Node, anchor: Node) -> str | None:+ """The column *call* filters a read to a set of input keys on+ (``Model.col.in_(ids)``), so the read scales with the keys, not the+ table. Values a name holds are read as bound before *anchor*.+ Default ``None``: a language that names no key filter."""+ return None+# Functions that ARE a lock acquisition, and the header of an unbounded# retry loop in this language. A spin loop inside such a function is how the# lock gets taken (tryLock retry, CAS spin): there is nothing to hoist. Bothdiff --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 27ed8467b..669926a32 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@@ -189,6 +189,11 @@ _PY_PER_KEY_LIMITS: frozenset[str] = frozenset(_PY_ONE_ROW: frozenset[str] = _PY_PER_KEY_LIMITS | {"scalar"}_PY_READ_CAPS: frozenset[str] = _PY_ONE_ROW - {"order", "order_by", "offset"}_PY_WRITES: frozenset[str] = frozenset({"update", "insert", "upsert", "values"})+# Keys a query yields (``.in_(select(...))``) are as many as that table's rows.+_PY_KEY_READS: frozenset[str] = _PY_STATEMENT_BUILDERS | {+ "subquery", "scalar_subquery", "query", "cte", "alias", "union", "union_all",+ "execute", "table", "from_",+}_PY_SCOPES: frozenset[str] = frozenset({"function_definition", "lambda", "class_definition"})_PY_BUILTINS: frozenset[str] = frozenset(dir(builtins))_PY_SEM_NAME_RE = re.compile(r"(?i)(sem|semaphore|limiter|limit)$")@@ -299,6 +304,63 @@ def _text(node: Node) -> str:return (node.text or b"").decode("utf-8", "replace")+def _field_text(node: Node, field: str) -> str:+ child = node.child_by_field_name(field)+ return _text(child) if child is not None else ""+++def _value_refs(expr: Node) -> list[Node]:+ """The names and dotted paths *expr* reads values from: not a callee+ (``tuple`` in ``tuple(xs)``), a keyword, or an ALL_CAPS constant."""+ refs = []+ stack = [expr]+ while stack:+ node = stack.pop()+ if node.type not in ("identifier", "attribute"):+ stack.extend(node.children)+ continue+ parent = node.parent+ callee = parent is not None and parent.child_by_field_name("function") == node+ keyword = parent is not None and parent.type == "keyword_argument" and (+ parent.child_by_field_name("name") == node+ )+ last = node.child_by_field_name("attribute") if node.type == "attribute" else node+ constant = last is not None and _text(last).strip("_").isupper()+ if callee and node.type == "attribute":+ # ``self.ids.copy()`` still reads ``self.ids``.+ stack.extend(filter(None, [node.child_by_field_name("object")]))+ elif not (callee or keyword or constant):+ refs.append(node)+ return refs+++def _string_value(node: Node) -> str | None:+ return _text(node).strip("\"'") if node.type == "string" else None+++def _in_filter(call: Node, args: list[Node]) -> tuple[str, Node] | None:+ """``(column, keys)`` of ``Model.col.in_(keys)`` or PostgREST ``.in_("col", keys)``."""+ if len(args) == 2:+ column = _string_value(args[0])+ return (column, args[1]) if column else None+ fn = call.child_by_field_name("function")+ receiver = fn.child_by_field_name("object") if fn is not None else None+ if len(args) != 1 or receiver is None or receiver.type != "attribute":+ return None+ column = _field_text(receiver, "attribute")+ return (column, args[0]) if column else None+++def _fixed_set(value: Node | None) -> bool:+ """A non-empty literal of scalars; an empty one is a container filled later."""+ return (+ value is not None+ and value.type in _PY_CONSTANT_RHS+ and bool(value.named_children)+ and all(_scalar_literal(item) for item in value.named_children)+ )++_PY_SCALARS = frozenset({"string", "concatenated_string", "integer", "float", "true", "false", "none"})@@ -1210,30 +1272,47 @@ class PythonPerfDialect(IterableSources):return Nonedef is_cap(self, node: Node, loop: Node) -> bool:- """``.limit(n)`` and peers, a SQL string saying ``LIMIT n``, or ``.in_()`` over- one chunk of keys."""+ """``.limit(n)`` and peers, a SQL string saying ``LIMIT n``, or a filter to a+ set of keys (:meth:`read_key_column`), which is as large as the keys are."""if node.type == "string":return bool(_PY_SQL_LIMIT_RE.search(_PY_SQL_COMMENT_RE.sub(b"", node.text or b"")))method = self.callee_method_name(node) if node.type == "call" else Noneif method in _PY_READ_CAPS:return True- return method == "in_" and self._in_one_chunk(node, loop)+ return node.type == "call" and self.read_key_column(node, loop) is not None- def _in_one_chunk(self, node: Node, loop: Node) -> bool:- """``.in_(xs[i:i + N])``, or ``.in_(chunk)`` where a loop walks ``xs`` in chunks."""- args = node.child_by_field_name("arguments")- keys = next((c for c in args.children if c.is_named), None) if args else None- if keys is None:+ def read_key_column(self, call: Node, anchor: Node) -> str | None:+ """``Model.col.in_(keys)`` and PostgREST ``.in_("col", keys)`` -> ``col``;+ ``filter_by(id=k)`` and ``.eq("id", k)`` -> ``id``. Keys a query yields+ (``.in_(select(...))``, or a name bound to one) grow with that table."""+ method = self.callee_method_name(call)+ arg_list = call.child_by_field_name("arguments")+ args = arg_list.named_children if arg_list is not None else []+ if method == "filter_by":+ named = {_field_text(a, "name") for a in args if a.type == "keyword_argument"}+ return "id" if "id" in named else None+ if method == "eq":+ return "id" if len(args) == 2 and _string_value(args[0]) == "id" else None+ found = _in_filter(call, args) if method == "in_" else None+ if found is None or not self._handed_in(found[1], anchor):+ return None+ return found[0]++ def _handed_in(self, keys: Node, anchor: Node) -> bool:+ """*keys* holds values given to the code, not ones a query yields or a fixed+ set of states (``in_(("a", "b"))``, ``in_(SOURCES)``) whose rows grow."""+ values = [+ self.reaching_value(anchor, ref.text or b"") if ref.type == "identifier" else None+ for ref in _value_refs(keys)+ ]+ if all(_fixed_set(value) for value in values):return False- if keys.type == "subscript":- return self._slice_magnitude(keys) == "bounded"- outer = loop- while outer is not None and keys.type == "identifier":- target = outer.child_by_field_name("left") if outer.type == "for_statement" else None- if target is not None and target.text == keys.text and self.is_chunked_loop(outer):- return True- outer = outer.parent- return False+ parts = [keys, *filter(None, values)]+ return not any(+ n.type == "call" and self.callee_method_name(n) in _PY_KEY_READS+ for part in parts+ for n in self._walk(part)+ )def _range_magnitude(self, call: Node, loop: Node, probe: SinkProbe, *, hop: booldiff --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 b7adf2fcb..26f4d7473 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@@ -472,8 +472,9 @@ class TsJsPerfDialect(TsJsMarkerHooks, IterableSources):if method == "slice":return "bounded" if _slice_is_bounded(call) else Noneif probe(call) in ("db", "network"):- # A read capped in the query (``take: n``) is as large as its cap.- capped = any(_pair_key_is(n, b"take") for n in self._walk(call))+ # A read capped in the query (``take: n``) is as large as its cap, and+ # one filtered to a set of keys (``id: { in: ids }``) as large as the keys.+ capped = any(_pair_key_is(n, k) for n in self._walk(call) for k in (b"take", b"in"))return None if capped else "grows_with_data"if method == "json":# ``fetch(url).json()`` / ``(await sink()).json()`` — adiff --git a/packages/core/src/repowise/core/analysis/health/perf/reduction_idioms.py b/packages/core/src/repowise/core/analysis/health/perf/reduction_idioms.pyindex 50eda76ef..620d5b425 100644--- a/packages/core/src/repowise/core/analysis/health/perf/reduction_idioms.py+++ b/packages/core/src/repowise/core/analysis/health/perf/reduction_idioms.py@@ -88,12 +88,12 @@ def _derived(stmts: list[Node], rows: frozenset[str]) -> frozenset[str]:return frozenset(derived)-def _keeps_first_via_setdefault(stmt: Node, rows: frozenset[str], keys: frozenset[str]) -> bool:+def _setdefault_key(stmt: Node, rows: frozenset[str], keys: frozenset[str]) -> Node | None:call = _method_call(stmt)if call is None or call[1] != "setdefault" or len(call[2]) < 2:- return False+ return Nonekey, value = call[2][0], call[2][1]- return ident(value) in rows and references(key, keys)+ return key if ident(value) in rows and references(key, keys) else Nonedef _stores_row(consequence: Node, container: str, rows: frozenset[str]) -> bool:@@ -154,32 +154,103 @@ def _keeps_every_row(stmts: list[Node], values: frozenset[str]) -> bool:return False-def _keeps_first_via_guard(+def _guard_key(stmt: Node, rest: list[Node], rows: frozenset[str], keys: frozenset[str]-) -> bool:+) -> Node | None:guard = _membership(stmt)if guard is None or not references(guard[1], keys):- return False- operator, _key, container, consequence = guard+ return None+ operator, key, container, consequence = guardif operator == "not in":- return _stores_row(consequence, container, rows)+ return key if _stores_row(consequence, container, rows) else Noneskips = any(n.type == "continue_statement" for n in walk(consequence))- return skips and _adds_to(rest, container, keys)+ return key if skips and _adds_to(rest, container, keys) else None-def reduces_per_key(for_stmt: Node) -> bool:- """Whether this loop keeps one row per key of what it iterates."""+def reduction_key(for_stmt: Node) -> Node | None:+ """The key this loop keeps one row per, or ``None`` when it keeps no such row."""body = for_stmt.child_by_field_name("body")rows = _row_names(for_stmt)if body is None or not rows:- return False+ return Nonestmts = [c for c in body.children if c.is_named]keys = _derived(stmts, rows)- return not _keeps_every_row(stmts, keys) and 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)- )+ if _keeps_every_row(stmts, keys):+ return None+ for index, stmt in enumerate(stmts):+ key = _setdefault_key(stmt, rows, keys)+ if key is None:+ key = _guard_key(stmt, stmts[index + 1 :], rows, keys)+ if key is not None:+ return key+ return None-__all__ = ["field_text", "ident", "reduces_per_key", "references", "walk"]+def reduces_per_key(for_stmt: Node) -> bool:+ """Whether this loop keeps one row per key of what it iterates."""+ return reduction_key(for_stmt) is not None+++def key_fields(for_stmt: Node, key: Node) -> frozenset[str]:+ """The row fields *key* reads: ``row.repo_id``, ``row["repo_id"]`` or+ ``row.get("repo_id")``, also through one local (``rid = row["repo_id"]``).+ A name unpacked from the row (``for path, name in rows``) is its own field."""+ rows = _row_names(for_stmt)+ local = _locals(for_stmt)+ fields: set[str] = set()+ pending, hopped = [key], False+ while pending:+ for node in walk(pending.pop()):+ name = ident(node)+ parent = node.parent+ if name is None or parent is None:+ continue+ if parent.type == "attribute" and parent.child_by_field_name("attribute") == node:+ continue+ if name in rows:+ fields.add(_field_of(node, parent) or name)+ elif name in local and not hopped:+ pending.append(local[name])+ hopped = True+ return frozenset(fields)+++def _locals(for_stmt: Node) -> dict[str, Node]:+ """Each name the loop body assigns, to its first value."""+ body = for_stmt.child_by_field_name("body")+ local: dict[str, Node] = {}+ for stmt in body.named_children if body is not None else ():+ node = next((c for c in stmt.children if c.is_named), stmt)+ if node.type != "assignment":+ continue+ name, right = ident(node.child_by_field_name("left")), node.child_by_field_name("right")+ if name and right is not None:+ local.setdefault(name, right)+ return local+++def _field_of(row: Node, parent: Node) -> str | None:+ """``row.f`` / ``row["f"]`` / ``row.get("f")`` -> ``f``."""+ if parent.type == "subscript" and parent.child_by_field_name("value") == row:+ index = parent.child_by_field_name("subscript")+ return _node_text(index).strip("\"'") if index is not None and index.type == "string" else None+ if parent.type != "attribute" or parent.child_by_field_name("object") != row:+ return None+ field = field_text(parent, "attribute")+ call = parent.parent+ if field == "get" and call is not None and call.type == "call":+ args = call.child_by_field_name("arguments")+ first = next((a for a in args.named_children), None) if args is not None else None+ return _node_text(first).strip("\"'") if first is not None and first.type == "string" else None+ return field+++__all__ = [+ "field_text",+ "ident",+ "key_fields",+ "reduces_per_key",+ "reduction_key",+ "references",+ "walk",+]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.pyindex 7e25e0157..2219d1a4e 100644--- a/packages/core/src/repowise/core/analysis/health/perf/unbounded_reduction.py+++ b/packages/core/src/repowise/core/analysis/health/perf/unbounded_reduction.py@@ -3,8 +3,10 @@A query with no ``limit``/``range``/``single`` in its chain, run once, whoseresult a loop deduplicates per key, pays to transfer every row to keep ahandful. 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.+result is passed to. A read filtered to a set of input keys holds the rows of+those keys, so it counts only when the loop keeps one row per that same key.+Python only: the dialect names the bounding methods, and every other dialect+names none."""from __future__ import annotations@@ -19,7 +21,14 @@ from ..complexity.ast_utils import _collect_function_nodes, _find_function_entryfrom ..complexity.models import PerfHitfrom .dialects import PERF_DIALECTSfrom .io_boundaries import collect_io_names-from .reduction_idioms import field_text, ident, reduces_per_key, walk+from .reduction_idioms import (+ field_text,+ ident,+ key_fields,+ reduces_per_key,+ reduction_key,+ walk,+)if TYPE_CHECKING:from collections.abc import Iterable@@ -60,12 +69,33 @@ class _FileScan:== "db")- def is_bounded(self, stmt: Node) -> bool:+ def read_calls(self, stmt: Node) -> list[Node]:+ """The calls of the read, including a statement or result bound before it+ (``rows = session.execute(stmt)``, ``for e in res.scalars()``)."""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)- )+ if chain is None:+ return []+ parts = [chain]+ for name in _statement_names(chain):+ value = self.dialect.reaching_value(stmt, name)+ if value is not None:+ parts.append(value)+ return [n for part in parts for n in walk(part) if n.type == "call"]++ def is_bounded(self, calls: list[Node]) -> bool:+ return any(self.dialect.callee_method_name(n) in self.bound_methods for n in calls)++ def bounded_by_keys(self, stmt: Node, calls: list[Node], loop: Node) -> bool:+ """A read filtered to a set of input keys holds the rows of those keys.+ Keeping one row per that same key still drops every other row each key+ has (the latest build per repo), so only that shape stays unbounded."""+ keyed = {col for n in calls if (col := self.dialect.read_key_column(n, stmt))}+ if not keyed:+ return False+ key = reduction_key(loop)+ fields = key_fields(loop, key) if key is not None else frozenset()+ # ``id`` keys one row each, so reducing per id drops nothing.+ return not (len(fields) == 1 and fields <= keyed - {"id"})def collect_unbounded_reductions(@@ -116,7 +146,7 @@ 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] = []+ found_at: list[tuple[Node, str, Node]] = []seen: set[tuple[int, int]] = set()for call in walk(body, scan.scopes):if call.type != "call" or not scan.is_db_read(call):@@ -127,19 +157,25 @@ def _function_hits(fn: Node, name: str | None, scan: _FileScan) -> list[PerfHit]continuestmt, var = siteseen.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+ calls = scan.read_calls(stmt)+ found = None if scan.is_bounded(calls) else _reducer(stmt, var, fn, scan)+ if found is not None and not scan.bounded_by_keys(stmt, calls, found[1]):+ found_at.append((stmt, *found))+ # ``res = execute(...)`` then ``for r in res.scalars()`` is one read: the+ # loop header only unwraps it, so the assignment carries the hit.+ unwraps = {(loop.start_byte, loop.end_byte) for stmt, _, loop in found_at if loop != stmt}+ return [+ PerfHit(+ _KIND,+ stmt.start_point[0] + 1,+ name,+ "db",+ func_start=fn.start_point[0] + 1,+ path=(reducer,) if reducer else (),+ )+ for stmt, reducer, _ in found_at+ if (stmt.start_byte, stmt.end_byte) not in unwraps+ ]def _in_loop_body(node: Node, fn: Node) -> bool:@@ -168,15 +204,39 @@ def _read_site(call: Node, fn: Node) -> tuple[Node, str | None] | None: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``."""+def _statement_names(chain: Node) -> list[bytes]:+ """Names the read runs or unwraps: ``execute(stmt)``'s first argument and the+ receiver of a method call (``res.scalars()``). A name the query only+ compares against (``== repo.id``) is not the read."""+ names = []+ for node in walk(chain):+ parent = node.parent+ if node.type != "identifier" or not node.text or parent is None:+ continue+ receiver = (+ parent.type == "attribute"+ and parent.child_by_field_name("object") == node+ and parent.parent is not None+ and parent.parent.type == "call"+ )+ first_arg = parent.type == "argument_list" and parent.named_children[0] == node+ if receiver or first_arg:+ names.append(node.text)+ return names+++def _reducer(+ stmt: Node, var: str | None, fn: Node, scan: _FileScan+) -> tuple[str, Node] | None:+ """The reducing loop, named ``""`` when *fn* reduces the rows itself and by+ its helper's name otherwise, or ``None``."""if var is None:- return "" if reduces_per_key(stmt) else None+ return ("", stmt) if reduces_per_key(stmt) else Noneif _rebound_after(fn, var, stmt.end_byte, scan.scopes):return Noneloop = _consuming_loop(fn, var, scan.scopes)if loop is not None and reduces_per_key(loop):- return ""+ return "", loopreturn _reducing_helper(fn, var, scan)@@ -261,17 +321,17 @@ def _bound_param(args: Node, var: str, helper: Node) -> str | None:return None-def _reducing_helper(fn: Node, var: str, scan: _FileScan) -> str | None:+def _reducing_helper(fn: Node, var: str, scan: _FileScan) -> tuple[str, Node] | None:for call in walk(fn, scan.scopes):name = ident(call.child_by_field_name("function")) if call.type == "call" else Nonehelper = scan.functions.get(name) if name else Noneargs = call.child_by_field_name("arguments")- if helper is None or helper == fn or args is None:+ if name is None or helper is None or helper == fn or args is None:continueparam = _bound_param(args, var, helper)loop = _consuming_loop(helper, param, scan.scopes) if param else Noneif loop is not None and reduces_per_key(loop):- return name+ return name, loopreturn Nonediff --git a/tests/unit/health/test_perf_loop_facts_python.py b/tests/unit/health/test_perf_loop_facts_python.pyindex a3a2fd455..1f6f8583d 100644--- a/tests/unit/health/test_perf_loop_facts_python.py+++ b/tests/unit/health/test_perf_loop_facts_python.py@@ -644,6 +644,42 @@ def test_a_read_filtered_to_a_chunk_loops_element_does_not_grow():assert facts.magnitude != "grows_with_data"+def test_a_read_filtered_to_a_set_of_keys_does_not_grow():+ facts = _loop(+ b"from sqlalchemy import select\n"+ b"async def f(session, paths):\n"+ b" result = await session.execute(select(G).where(G.path.in_(paths)))\n"+ b" for g in result.scalars().all():\n"+ b" await session.execute(select(H).where(H.id == g.id))\n"+ )+ assert facts.magnitude != "grows_with_data"++[email protected]("values", ['("a", "b")', "SOURCES", "sources"])+def test_a_fixed_set_of_values_is_a_state_filter_that_grows(values):+ facts = _loop(+ b"from sqlalchemy import select\n"+ b"async def f(session):\n"+ b" sources = ('a', 'b')\n"+ b" result = await session.execute(select(G).where(G.source.in_(" + values.encode() + b")))\n"+ b" for g in result.scalars().all():\n"+ b" await session.execute(select(H).where(H.id == g.id))\n"+ )+ assert facts.magnitude == "grows_with_data"+++def test_keys_a_subquery_yields_still_grow():+ facts = _loop(+ b"from sqlalchemy import select\n"+ b"async def f(session):\n"+ b" ids = select(R.id).subquery()\n"+ b" result = await session.execute(select(G).where(G.repo_id.in_(ids)))\n"+ b" for g in result.scalars().all():\n"+ b" await session.execute(select(H).where(H.id == g.id))\n"+ )+ assert facts.magnitude == "grows_with_data"++def test_a_limit_on_a_statement_built_before_the_loop_caps_the_read():facts = _loop(b"from sqlalchemy import select\n"diff --git a/tests/unit/health/test_perf_loop_facts_ts.py b/tests/unit/health/test_perf_loop_facts_ts.pyindex 10362bba9..e65b30340 100644--- a/tests/unit/health/test_perf_loop_facts_ts.py+++ b/tests/unit/health/test_perf_loop_facts_ts.py@@ -156,6 +156,19 @@ async function f() {assert hit.loop.magnitude == "grows_with_data"+def test_a_prisma_read_filtered_to_a_set_of_keys_does_not_grow():+ src = """+async function f(ids) {+ const users = await prisma.user.findMany({ where: { id: { in: ids } } });+ for (const u of users) {+ await prisma.post.deleteMany({ where: { userId: u.id } });+ }+}+"""+ hit = next(h for h in _io_hits(src) if h.detail == "db")+ assert hit.loop is None or hit.loop.magnitude != "grows_with_data"++def test_magnitude_grows_with_fetch_json_one_hop():src = """async function f() {diff --git a/tests/unit/health/test_perf_unbounded_reduction.py b/tests/unit/health/test_perf_unbounded_reduction.pyindex 3a8b3170e..eeb0d5787 100644--- a/tests/unit/health/test_perf_unbounded_reduction.py+++ b/tests/unit/health/test_perf_unbounded_reduction.py@@ -132,9 +132,162 @@ def test_one_hop_same_file_helper(tmp_path: Path):assert hits[0].path == ("_latest_per_branch",)+def test_keyed_read_reduced_per_its_own_key_column(tmp_path: Path):+ # The keys bound which repos, not how many builds each has: keeping the+ # first per repo still reads every build of every repo.+ hits = _hits(+ tmp_path,+ "keyed_same.py",+ """+ from sqlalchemy import select++ async def latest(session, repo_ids):+ res = await session.execute(select(Build).where(Build.repo_id.in_(repo_ids)))+ out = {}+ for build in res.scalars().all():+ out.setdefault(build.repo_id, build)+ return out+ """,+ )+ assert len(hits) == 1+++def test_keys_a_subquery_yields_do_not_bound_the_read(tmp_path: Path):+ hits = _hits(+ tmp_path,+ "keyed_subquery.py",+ """+ from sqlalchemy import select++ async def f(session):+ live = select(Repo.id).where(Repo.active).subquery()+ res = await session.execute(select(Build).where(Build.repo_id.in_(live)))+ seen = set()+ for build in res.scalars().all():+ if build.branch in seen:+ continue+ seen.add(build.branch)+ return seen+ """,+ )+ assert len(hits) == 1+++def test_a_fixed_set_of_states_does_not_bound_the_read(tmp_path: Path):+ hits = _hits(+ tmp_path,+ "states.py",+ """+ from sqlalchemy import select++ def f(session):+ rows = session.execute(select(Build).where(Build.status.in_(("ready", "done"))))+ out = {}+ for build in rows.scalars().all():+ out.setdefault(build.repo_id, build)+ return out+ """,+ )+ assert len(hits) == 1+++def test_a_value_the_query_compares_against_does_not_bound_it(tmp_path: Path):+ hits = _hits(+ tmp_path,+ "compared.py",+ """+ from sqlalchemy import select++ async def f(session):+ repo = (await session.execute(select(Repo).limit(1))).scalar()+ res = await session.execute(select(Build).where(Build.repo_id == repo.id))+ out = {}+ for build in res.scalars().all():+ out.setdefault(build.branch, build)+ return out+ """,+ )+ assert len(hits) == 1++# -- negative ------------------------------------------------------------+def test_keyed_read_reduced_per_another_column(tmp_path: Path):+ # Edges into a few files, kept once per importer: the rows are the keys'.+ hits = _hits(+ tmp_path,+ "keyed_other.py",+ """+ from sqlalchemy import select++ async def files_importing(session, symbols):+ by_file: dict[str, list] = {}+ for node in symbols:+ by_file.setdefault(node.file_path, []).append(node)+ res = await session.execute(+ select(GraphEdge).where(GraphEdge.target_node_id.in_(list(by_file)))+ )+ seen = set()+ out = []+ for edge in sorted(res.scalars().all(), key=lambda e: e.source_node_id):+ importer = edge.source_node_id+ if importer in seen:+ continue+ seen.add(importer)+ out.append(importer)+ return out+ """,+ )+ assert hits == []+++def test_keyed_statement_built_before_the_read(tmp_path: Path):+ hits = _hits(+ tmp_path,+ "keyed_tuple.py",+ """+ from sqlalchemy import select++ async def attach(session, paths):+ stmt = select(Sym.symbol_id, Sym.file_path, Sym.name).where(Sym.file_path.in_(paths))+ rows = (await session.execute(stmt)).all()+ by_name = {}+ for symbol_id, file_path, name in rows:+ by_name.setdefault((file_path, name), symbol_id)+ return by_name+ """,+ )+ assert hits == []+++ "read",+ [+ 'supabase.table("builds").select("*").in_("id", ids).execute()',+ "session.query(Build).filter_by(id=build_id).all()",+ "session.execute(select(Build).limit(10)).scalars().all()",+ ],+)+def test_reads_bounded_by_primary_keys_or_a_cap(tmp_path: Path, read: str):+ hits = _hits(+ tmp_path,+ "keyed_id.py",+ f"""+ from sqlalchemy import select++ def f(session, supabase, ids, build_id):+ rows = {read}+ out = {{}}+ for row in rows:+ out.setdefault(row.id, row)+ return out+ """,+ )+ assert hits == []+++def test_limit_bounds_the_read(tmp_path: Path):hits = _hits(tmp_path,