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(tests): a test that swaps a module out does not reach it (#4338)
Raghav Chamadiya06f4198b1ca5
diff --git a/docs/architecture/test-intelligence.md b/docs/architecture/test-intelligence.mdindex 379368d8b..a8c30746e 100644--- a/docs/architecture/test-intelligence.md+++ b/docs/architecture/test-intelligence.md@@ -133,12 +133,20 @@ per run, and caches it. The per-change walk reads the same edge tableThe reverse walks are written once and run over the stored graph or over agraph held in memory (`build_walk_maps` on the NetworkX graph or its node-linkJSON), and both return the same rows. `reach_states` gives each file a typed-state: `reached`, `unreached`, `new`, `no_signal` or `type_only`. A file is+state: `reached`, `unreached`, `new`, `no_signal`, `type_only` or+`mocked_only`. A file is`unreached` only when the call walk, the helper and barrel hop and the wholeimport closure all miss it, and a test the change adds or edits that is namedfor the file or imports it, or the file's own inline tests (Rust`#[cfg(test)]`, a Go `_test.go` beside it), count as reaching it.+A Python test that swaps a module object out (`patch("pkg.helpers")`,+`patch.object(pkg, "helpers")`, `monkeypatch.setattr`, a `sys.modules` entry)+does not reach that module through the swap. A file only such tests reach is+`mocked_only`, read from the test files' text at walk time, never stored.+Patching a name inside a module, a run-time target, or a patch with `wraps`,+`side_effect` or `autospec` keeps the reach.+## Measured map internals- `.coverage` is opened read-only as SQLite. Repowise decodes coverage.py'sdiff --git a/packages/core/src/repowise/core/analysis/actions/build.py b/packages/core/src/repowise/core/analysis/actions/build.pyindex 937760f6a..bb702969a 100644--- a/packages/core/src/repowise/core/analysis/actions/build.py+++ b/packages/core/src/repowise/core/analysis/actions/build.py@@ -52,7 +52,8 @@ Row shapes (the field names are the SQL columns):``partial``.``test_map``One mapping of path to ``total`` (test files that reach it in the code- graph) and ``via`` (the walk), for :func:`reach_paths`; see+ graph) and ``via`` (the walk, or ``mocked-only`` with a total of 0 when+ only tests replacing the file reach it), for :func:`reach_paths`; see``repowise.core.analysis.test_reachability.any_tests_reaching``."""diff --git a/packages/core/src/repowise/core/analysis/actions/facts.py b/packages/core/src/repowise/core/analysis/actions/facts.pyindex d1578abc1..d46ae05b0 100644--- a/packages/core/src/repowise/core/analysis/actions/facts.py+++ b/packages/core/src/repowise/core/analysis/actions/facts.py@@ -18,6 +18,11 @@ from repowise.core.analysis.health.fix_first import FixItemCoverageStatus = Literal["measured", "stale", "unknown"]+#: ``via`` of a file only tests that replace it with a double reach: zero tests+#: run it, which is not the claim that none reaches it.+MOCKED_ONLY_VIA = "mocked-only"++@dataclass(frozen=True, slots=True)class LeadFinding:"""A file's strongest code-shape finding: the thing an edit can fix."""@@ -50,7 +55,8 @@ class FileFacts:#: Test files the code graph says reach this file; ``None`` when not looked#: up (only fragile files with no measured coverage are) or the map failed.tests_reaching: int | None = None- #: Which walk found them (``call-graph`` or ``import-graph``).+ #: Which walk found them (``call-graph`` or ``import-graph``), or+ #: :data:`MOCKED_ONLY_VIA` when every test reaching it replaces it.tests_reaching_via: str | None = Nonediff --git a/packages/core/src/repowise/core/analysis/actions/rules/code.py b/packages/core/src/repowise/core/analysis/actions/rules/code.pyindex e860bc4e0..ae776cfa4 100644--- a/packages/core/src/repowise/core/analysis/actions/rules/code.py+++ b/packages/core/src/repowise/core/analysis/actions/rules/code.py@@ -8,7 +8,7 @@ from collections import defaultdictfrom datetime import datetimefrom ..context import HISTORY_TOO_SHORT, RepoContext-from ..facts import FileFacts, RepoFacts+from ..facts import MOCKED_ONLY_VIA, FileFacts, RepoFactsfrom ..model import (SEVERITY_VALUE,Action,@@ -327,6 +327,8 @@ def _coverage_fact(f: FileFacts, facts: RepoFacts) -> WhyFact:def _reach_fact(f: FileFacts) -> WhyFact:+ if f.tests_reaching_via == MOCKED_ONLY_VIA:+ return WhyFact("tests that reach it", "Only tests that replace it reach it", "inferred")if not f.tests_reaching:return WhyFact("tests that reach it", "None in the code graph", "inferred")via = (f.tests_reaching_via or "code graph").replace("-", " ")diff --git a/packages/core/src/repowise/core/analysis/health/mocks/lexicon.py b/packages/core/src/repowise/core/analysis/health/mocks/lexicon.pyindex 86c0669af..283edd334 100644--- a/packages/core/src/repowise/core/analysis/health/mocks/lexicon.py+++ b/packages/core/src/repowise/core/analysis/health/mocks/lexicon.py@@ -71,6 +71,16 @@ class MockDialect:#: Over-inclusive costs time; under-inclusive counts nothing at all.file_markers: frozenset[str] = frozenset()+ #: Callee -> how it names the binding it replaces: ``dotted`` as one+ #: ``"pkg.mod.name"`` string, ``object`` as ``(obj, "name")``,+ #: ``dotted_or_object`` either way, ``modules`` as ``sys.modules`` keys.+ #: Callees are spelled with any :attr:`guard_roots` prefix dropped.+ binding_patches: dict[str, str] = field(default_factory=dict)++ #: Keywords that leave the original reachable through the double, or may:+ #: a patch carrying one never counts as replacing the binding.+ passthrough_keywords: frozenset[str] = frozenset()+_PYTHON = MockDialect(# ``unittest.mock`` members with no mock token in their names.@@ -85,6 +95,14 @@ _PYTHON = MockDialect(config_attributes=frozenset({"return_value", "side_effect"}),test_name_prefixes=("test",),file_markers=frozenset({"patch"}),+ binding_patches={+ "patch": "dotted",+ "patch.object": "object",+ "patch.dict": "modules",+ "monkeypatch.setattr": "dotted_or_object",+ "monkeypatch.setitem": "modules",+ },+ passthrough_keywords=frozenset({"wraps", "side_effect", "autospec"}),)diff --git a/packages/core/src/repowise/core/analysis/test_reach/__init__.py b/packages/core/src/repowise/core/analysis/test_reach/__init__.pyindex 5e351af84..5fb31d027 100644--- a/packages/core/src/repowise/core/analysis/test_reach/__init__.py+++ b/packages/core/src/repowise/core/analysis/test_reach/__init__.py@@ -160,6 +160,7 @@ from .db import (cached_files_reached_by_tests,cached_hub_files,cached_test_files,+ cached_test_masks,call_graph_from_db,clear_test_map_cache,dependency_path,@@ -185,6 +186,7 @@ from .forward import (from .hubs import call_hub_files, hub_filesfrom .inline import go_inline_tests, inline_tests, rust_has_inline_testsfrom .maps import ImportGraph, WalkMaps, build_walk_maps+from .mocks import replaced_files, replaced_modulesfrom .model import (DEFAULT_CALL_DEPTH,DEFAULT_MAX_DEPTH,@@ -223,6 +225,7 @@ __all__ = ["cached_files_reached_by_tests","cached_hub_files","cached_test_files",+ "cached_test_masks","call_graph_from_db","call_graph_from_graph","call_hub_files",@@ -246,6 +249,8 @@ __all__ = ["reach_states","reach_states_from_db","reaching_by_tier",+ "replaced_files",+ "replaced_modules","rust_has_inline_tests","tests_matching_by_name","tests_reaching",diff --git a/packages/core/src/repowise/core/analysis/test_reach/db.py b/packages/core/src/repowise/core/analysis/test_reach/db.pyindex a09db5dc5..329ed3f15 100644--- a/packages/core/src/repowise/core/analysis/test_reach/db.py+++ b/packages/core/src/repowise/core/analysis/test_reach/db.py@@ -8,9 +8,10 @@ rows.from __future__ import annotations+import asyncioimport jsonfrom collections.abc import Collection, Mapping-from pathlib import PurePosixPath+from pathlib import Path, PurePosixPathfrom typing import Any, TypeVarfrom sqlalchemy import select, text@@ -20,11 +21,12 @@ from repowise.core.analysis.dead_code.file_reachability import BARREL_FILENAMESfrom repowise.core.analysis.execution_graph import ExecutionGraphIndex, file_of_symbolfrom repowise.core.ingestion.models import EXECUTION_EDGE_TYPES, FILE_DEPENDENCY_EDGE_TYPESfrom repowise.core.persistence.index_cache import IndexStampCache-from repowise.core.persistence.models import GraphNode+from repowise.core.persistence.models import GraphNode, Repositoryfrom .forward import CallGraphView, files_reached_by_testsfrom .hubs import call_hub_filesfrom .maps import LEGACY_CONFTEST_HINT, ImportGraph, Need, Walk+from .mocks import replaced_filesfrom .model import (DEFAULT_CALL_DEPTH,DEFAULT_MAX_DEPTH,@@ -180,13 +182,55 @@ async def cached_files_reached_by_tests(session: AsyncSession, repo_id: str) ->return _cache.remember(key, frozenset(files_reached_by_tests(view, test_files)))+async def cached_test_masks(session: AsyncSession, repo_id: str) -> dict[str, frozenset[str]]:+ """:func:`~.mocks.replaced_files` for the stored test files, read from the checkout.++ Read once per index state, like the graph it pairs with. No checkout on+ disk means no masks, so every test keeps its reach.+ """+ key = await _cache.key(session, repo_id, "masks")+ if key in _cache:+ return _cache.get(key)+ root = (+ await session.execute(select(Repository.local_path).where(Repository.id == repo_id))+ ).scalar_one_or_none()+ tests = [t for t in await cached_test_files(session, repo_id) if t.endswith(".py")]+ texts = await asyncio.to_thread(_read_texts, root, tests) if root else {}+ files = await _file_paths(session, repo_id) if texts else set()+ return _cache.remember(key, replaced_files(texts, files))+++def _read_texts(root: str, paths: list[str]) -> dict[str, str]:+ """``{path: text}`` for the files under *root* that read; none when *root* is gone."""+ base = Path(root)+ if not base.is_dir():+ return {}+ out: dict[str, str] = {}+ for path in paths:+ try:+ out[path] = (base / path).read_text(encoding="utf-8", errors="replace")+ except OSError:+ continue+ return out+++async def _file_paths(session: AsyncSession, repo_id: str) -> set[str]:+ res = await session.execute(+ select(GraphNode.node_id).where(+ GraphNode.repository_id == repo_id, GraphNode.node_type == "file"+ )+ )+ return {row[0] for row in res.all()}++async def any_tests_reaching(session: AsyncSession, repo_id: str, targets: Collection[str]-) -> dict[str, ReachedBy]:- """The targets some test reaches, and how; a target it leaves out is unreached.+) -> dict[str, ReachRow]:+ """The targets some test reaches, or only tests replacing them reach; others are unreached.- The ``reached`` rows of :func:`reach_states_from_db`: the call and import- tiers :func:`tests_reaching_by_tier` runs at its defaults (so the two never+ The ``reached`` and ``mocked_only`` rows of :func:`reach_states_from_db`,+ masked by :func:`cached_test_masks`: the call and import tiers+ :func:`tests_reaching_by_tier` runs at its defaults (so the two neverdisagree about a file it answers), the helper and barrel hop, then thewhole import closure, so absence is a claim. Cached per index state."""@@ -195,8 +239,9 @@ async def any_tests_reaching(if key in _cache:return _cache.get(key)test_files = set(await cached_test_files(session, repo_id))- rows = await drive(states_walk(list(seeds), test_files), session, repo_id)- found = {path: row.reached for path, row in rows.items() if row.reached is not None}+ masks = await cached_test_masks(session, repo_id)+ rows = await drive(states_walk(list(seeds), test_files, masks=masks), session, repo_id)+ found = {path: row for path, row in rows.items() if row.state in ("reached", "mocked_only")}return _cache.remember(key, found)@@ -207,10 +252,13 @@ async def reach_states_from_db(*,changed_tests: Collection[str] = frozenset(),contents: Mapping[str, str | bytes] | None = None,+ masks: Mapping[str, Collection[str]] | None = None,) -> dict[str, ReachRow]:""":func:`~.walk.reach_states` over the stored graph."""test_files = set(await cached_test_files(session, repo_id))- walk = states_walk(list(paths), test_files, changed_tests=changed_tests, contents=contents)+ walk = states_walk(+ list(paths), test_files, changed_tests=changed_tests, contents=contents, masks=masks+ )return await drive(walk, session, repo_id)diff --git a/packages/core/src/repowise/core/analysis/test_reach/mocks.py b/packages/core/src/repowise/core/analysis/test_reach/mocks.pynew file mode 100644index 000000000..0b1f85f2b--- /dev/null+++ b/packages/core/src/repowise/core/analysis/test_reach/mocks.py@@ -0,0 +1,182 @@+"""Which repository modules a test replaces outright, read from the test's own text.++A test that swaps a module object for a double runs the double, not the module,+so its route into that module is no evidence that a test exercises it. Python+only: the patch forms come from the mock lexicon+(:attr:`~repowise.core.analysis.health.mocks.lexicon.MockDialect.binding_patches`).++A patch replaces one binding. Only a binding that *is* a module masks it:+``patch("pkg.helpers")``, ``patch.object(pkg, "helpers")``, a ``sys.modules``+entry. ``patch("pkg.mod.func")`` replaces ``func`` inside ``pkg.mod``, and the+test still runs the rest of ``pkg.mod`` (patching where a name is used is how+a module is tested in isolation), so it masks nothing. Ceiling: a binding that+names a module through a consumer's own import (``patch("app.cli.helpers")``+after ``from app import helpers``) is not traced to that module; resolving it+through the consumer's import edges is the upgrade path.++The answer is per test file, not per test function: a file that also imports+the module it replaces keeps its reach, since another test in it may run the+real one. Ceiling: a file mixing both without importing the module still masks+it; reading each test function's own patches is the upgrade path.++Abstain-first: a target built at run time, an alias the file binds more than+once, a relative import, a ``patch`` not imported from the mock library, or a+patch carrying a pass-through keyword (``wraps``, ``side_effect``,+``autospec``) masks nothing, so the test keeps its reach.+"""++from __future__ import annotations++import ast+from collections.abc import Collection, Mapping++from repowise.core.analysis.health.mocks.lexicon import MockDialect, mock_dialect+from repowise.core.ingestion.languages.python_modules import build_python_module_index+++def replaced_modules(path: str, text: str | bytes) -> frozenset[str]:+ """Dotted names of the module bindings the test at *path* replaces outright.++ Every name is a candidate: whether it is a module is+ :func:`replaced_files`'s question, answered against the repository's files.+ """+ dialect = mock_dialect("python")+ if dialect is None or not path.endswith(".py"):+ return frozenset()+ if isinstance(text, bytes):+ text = text.decode("utf-8", errors="replace")+ if not any(marker in text for marker in dialect.file_markers):+ return frozenset()+ try:+ tree = ast.parse(text)+ except (SyntaxError, ValueError, RecursionError, MemoryError):+ return frozenset()+ aliases = _import_aliases(tree)+ found: set[str] = set()+ for node in ast.walk(tree):+ if isinstance(node, ast.Call):+ found |= _patched(node, dialect, aliases)+ return frozenset(found - _imported_modules(tree))+++def replaced_files(+ texts: Mapping[str, str | bytes], files: Collection[str]+) -> dict[str, frozenset[str]]:+ """``{test: the repository files it replaces outright}``, for the tests in *texts*.++ *files* are the repository's paths; a replaced name masks a file only when+ it is exactly that file's dotted module name. Tests replacing none are absent.+ """+ index = build_python_module_index({f for f in files if f.endswith(".py")})+ out: dict[str, frozenset[str]] = {}+ for test, text in texts.items():+ hits = {index[name] for name in replaced_modules(test, text) if name in index}+ hits.discard(test)+ if hits:+ out[test] = frozenset(hits)+ return out+++def _import_aliases(tree: ast.AST) -> dict[str, str]:+ """``{local name: dotted module}`` for names the file binds once, by an absolute import."""+ seen: dict[str, set[str | None]] = {}+ for node in ast.walk(tree):+ if isinstance(node, ast.Import):+ for a in node.names:+ if a.asname:+ seen.setdefault(a.asname, set()).add(a.name)+ else:+ root = a.name.split(".", 1)[0]+ seen.setdefault(root, set()).add(root)+ elif isinstance(node, ast.ImportFrom):+ for a in node.names:+ local = a.asname or a.name+ dotted = f"{node.module}.{a.name}" if node.module and not node.level else None+ seen.setdefault(local, set()).add(dotted)+ elif isinstance(node, ast.Name) and isinstance(node.ctx, ast.Store):+ seen.setdefault(node.id, set()).add(None)+ elif isinstance(node, ast.arg):+ seen.setdefault(node.arg, set()).add(None)+ elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)) or (+ isinstance(node, ast.ExceptHandler) and node.name+ ):+ seen.setdefault(node.name, set()).add(None)+ return {name: next(iter(v)) for name, v in seen.items() if len(v) == 1 and None not in v}+++def _imported_modules(tree: ast.AST) -> set[str]:+ """Every dotted name the file imports: modules, and ``from`` names under them."""+ out: set[str] = set()+ for node in ast.walk(tree):+ if isinstance(node, ast.Import):+ out.update(a.name for a in node.names)+ elif isinstance(node, ast.ImportFrom) and node.module and not node.level:+ out.add(node.module)+ out.update(f"{node.module}.{a.name}" for a in node.names)+ return out+++def _dotted(node: ast.expr, aliases: Mapping[str, str] | None = None) -> str | None:+ """``a.b.c`` for a plain name chain; with *aliases*, its root resolved through them."""+ parts: list[str] = []+ while isinstance(node, ast.Attribute):+ parts.append(node.attr)+ node = node.value+ if not isinstance(node, ast.Name):+ return None+ root = node.id if aliases is None else aliases.get(node.id)+ if root is None:+ return None+ return ".".join([root, *reversed(parts)])+++def _callee(call: ast.Call, dialect: MockDialect, aliases: Mapping[str, str]) -> str | None:+ """The callee as :attr:`MockDialect.binding_patches` spells it, guard roots dropped.++ A callee whose root the receiver table does not name must be imported from+ a guard root (``from unittest.mock import patch``, ``import mock``).+ """+ name = _dotted(call.func)+ if name is None:+ return None+ root = name.split(".", 1)[0]+ if root not in dialect.receiver_methods:+ origin = aliases.get(root)+ if origin is None or origin.split(".", 1)[0] not in dialect.guard_roots:+ return None+ name = _dotted(call.func, aliases) or name+ parts = name.split(".")+ while len(parts) > 1 and parts[0] in dialect.guard_roots:+ parts.pop(0)+ return ".".join(parts)+++def _literal(node: ast.expr | None) -> str | None:+ if isinstance(node, ast.Constant) and isinstance(node.value, str):+ return node.value+ return None+++def _patched(call: ast.Call, dialect: MockDialect, aliases: Mapping[str, str]) -> set[str]:+ """The dotted bindings one call replaces, or nothing when any part is unproven."""+ form = dialect.binding_patches.get(_callee(call, dialect, aliases) or "")+ if form is None or any(k.arg in dialect.passthrough_keywords for k in call.keywords):+ return set()+ args = call.args+ if form == "dotted_or_object":+ form = "dotted" if args and _literal(args[0]) is not None else "object"+ if form == "dotted":+ target = _literal(args[0]) if args else None+ return {target} if target else set()+ if form == "object":+ owner = _dotted(args[0], aliases) if len(args) >= 2 else None+ attr = _literal(args[1]) if len(args) >= 2 else None+ return {f"{owner}.{attr}"} if owner and attr else set()+ # ``modules``: entries of ``sys.modules``, by key.+ if not args or not (_literal(args[0]) == "sys.modules" or _dotted(args[0]) == "sys.modules"):+ return set()+ if len(args) >= 2 and isinstance(args[1], ast.Dict):+ keys = [_literal(k) for k in args[1].keys]+ return set() if None in keys else {k for k in keys if k}+ key = _literal(args[1]) if len(args) >= 2 else None+ return {key} if key else set()diff --git a/packages/core/src/repowise/core/analysis/test_reach/model.py b/packages/core/src/repowise/core/analysis/test_reach/model.pyindex b3748e2d7..09f2ab90b 100644--- a/packages/core/src/repowise/core/analysis/test_reach/model.py+++ b/packages/core/src/repowise/core/analysis/test_reach/model.py@@ -45,8 +45,10 @@ ReachedVia = Literal["call-graph", "import-graph", "name-match", "changed-test",# closure all miss it, so "no test reaches this" is a claim;# ``new`` the index has never seen the file;# ``no_signal`` the index holds no tests, so silence proves nothing;-# ``type_only`` it declares only types: there is nothing for a test to run.-ReachState = Literal["reached", "unreached", "new", "no_signal", "type_only"]+# ``type_only`` it declares only types: there is nothing for a test to run;+# ``mocked_only`` every test that reaches it replaces it with a double, so+# none is shown to run it, and nothing says none could.+ReachState = Literal["reached", "unreached", "new", "no_signal", "type_only", "mocked_only"]class ReachDistance(NamedTuple):@@ -87,10 +89,12 @@ class ReachedBy:@dataclass(frozen=True)class ReachRow:- """One file's :data:`ReachState`, with the evidence when it is ``reached``."""+ """One file's :data:`ReachState`, with the evidence when it is ``reached`` or ``mocked_only``."""state: ReachStatereached: ReachedBy | None = None+ # ``mocked_only``: the tests that reach the file and replace it.+ mocked_by: tuple[str, ...] = ()def capped(diff --git a/packages/core/src/repowise/core/analysis/test_reach/walk.py b/packages/core/src/repowise/core/analysis/test_reach/walk.pyindex 538c5ccb8..41a888742 100644--- a/packages/core/src/repowise/core/analysis/test_reach/walk.py+++ b/packages/core/src/repowise/core/analysis/test_reach/walk.py@@ -10,7 +10,9 @@ from __future__ import annotationsimport sysfrom collections.abc import Collection, Mapping+from dataclasses import replacefrom pathlib import PurePosixPath+from typing import TypeVarfrom repowise.core.analysis.dead_code.file_reachability import BARREL_FILENAMESfrom repowise.core.analysis.execution_graph import file_of_symbol@@ -22,6 +24,7 @@ from .maps import Need, Walk, WalkMaps, runfrom .model import (DEFAULT_CALL_DEPTH,DEFAULT_MAX_DEPTH,+ MAX_TESTS_PER_TARGET,TRANSITIVE_HOPS,ReachDistance,ReachedBy,@@ -40,6 +43,8 @@ CLOSURE_DEPTH = sys.maxsize# over shared ancestors; a seed bitset per node is the upgrade path.CLOSURE_SEED_CAP = 200+_V = TypeVar("_V")+def call_walk(seeds: list[str],@@ -384,17 +389,22 @@ def states_walk(*,changed_tests: Collection[str] = frozenset(),contents: Mapping[str, str | bytes] | None = None,+ masks: Mapping[str, Collection[str]] | None = None,) -> Walk[dict[str, ReachRow]]:"""A :class:`~.model.ReachRow` per path, the negative claim made only when it holds.In order: ``type_only`` (from *contents*); ``reached`` by a test the change- adds or edits (*changed_tests*) that is named for the path or imports it,- then by the path's own inline tests; ``new`` for a path the index lacks;- ``no_signal`` when the index holds no test; ``reached`` by the call walk,+ adds or edits (*changed_tests*) that is named for the path or imports it+ and does not replace it, then by the path's own inline tests; ``new`` for+ a path the index lacks; ``no_signal`` when the index holds no test; ``reached`` by the call walk,the import walk or the helper and barrel hop; ``reached`` through the whole- import closure; else ``unreached``.+ import closure; ``mocked_only`` when every test those found replaces the+ path (*masks*, ``{test: files it replaces}``, :func:`~.mocks.replaced_files`);+ else ``unreached``. A path whose walked tiers find only replacing tests+ goes on to the import closure, so a test that runs it is still found."""contents = contents or {}+ mocked: dict[str, set[str]] = {}rows: dict[str, ReachRow] = {}rest: list[str] = []# Grouped once by directory, so each Go file scans only its own folder's tests.@@ -417,6 +427,10 @@ def states_walk(rows[path] = ReachRow("type_only")continuenamed = [t for t in touched if t in imported_by.get(path, ()) or names_test_for(t, path)]+ replacing = [t for t in named if masks and path in masks.get(t, ())]+ if replacing:+ mocked.setdefault(path, set()).update(replacing)+ named = [t for t in named if t not in replacing]if named:rows[path] = ReachRow("reached", capped(path, named, "changed-test", changed=named))continue@@ -432,15 +446,54 @@ def states_walk(elif not test_files:rows[path] = ReachRow("no_signal")rest = [p for p in rest if p not in rows]- found = yield from tier_walk(rest, test_files, helpers=True)+ found = _unmasked((yield from tier_walk(rest, test_files, helpers=True)), masks, mocked)left = [p for p in rest if p not in found]- found |= yield from closure_walk(left, test_files)+ found |= _unmasked((yield from closure_walk(left, test_files)), masks, mocked)for path in rest:hit = found.get(path)- rows[path] = ReachRow("reached", hit) if hit else ReachRow("unreached")+ if hit:+ rows[path] = ReachRow("reached", hit)+ elif path in mocked:+ rows[path] = ReachRow("mocked_only", mocked_by=tuple(sorted(mocked[path])))+ else:+ rows[path] = ReachRow("unreached")return {path: rows[path] for path in dict.fromkeys(paths)}+def _unmasked(+ found: dict[str, ReachedBy],+ masks: Mapping[str, Collection[str]] | None,+ mocked: dict[str, set[str]],+) -> dict[str, ReachedBy]:+ """*found* without the tests that replace each target; *mocked* gains the ones dropped.++ A target every test of which replaces it is left out. The rest keep their+ rank order, so the cap still keeps the closest tests.+ """+ if not masks:+ return found+ out: dict[str, ReachedBy] = {}+ for target, by in found.items():+ tests = by.all_tests if by.all_tests is not None else tuple(by.tests)+ replaced = [t for t in tests if target in masks.get(t, ())]+ if not replaced:+ out[target] = by+ continue+ mocked.setdefault(target, set()).update(replaced)+ kept = tuple(t for t in tests if t not in replaced)+ if kept:+ out[target] = replace(+ by,+ tests=list(kept[:MAX_TESTS_PER_TARGET]),+ total=len(kept),+ all_tests=kept,+ reach=_only(by.reach, kept),+ entries=_only(by.entries, kept),+ transitive=by.transitive - set(replaced),+ )+ return out++def _touched_imports(paths: list[str], touched: list[str]) -> Walk[dict[str, set[str]]]:"""``{path: the touched tests importing it}``, one hop; nothing to ask without touched tests."""if not touched or not paths:@@ -453,22 +506,32 @@ def _touched_imports(paths: list[str], touched: list[str]) -> Walk[dict[str, setreturn out+def _only(by_test: Mapping[str, _V] | None, tests: Collection[str]) -> dict[str, _V] | None:+ return None if by_test is None else {t: v for t, v in by_test.items() if t in tests}++def reach_states(maps: WalkMaps,paths: Collection[str],*,changed_tests: Collection[str] = frozenset(),contents: Mapping[str, str | bytes] | None = None,+ masks: Mapping[str, Collection[str]] | None = None,) -> dict[str, ReachRow]:""":func:`states_walk` over *maps*: whether tests reach each path, as a typed claim.*changed_tests* are test files the change adds or edits, indexed or not.*contents* maps a path to its bytes, for the type-only and Rust inline- checks; a path without them skips both.+ checks; a path without them skips both. *masks* maps a test to the files+ it replaces outright (:func:`~.mocks.replaced_files`)."""return run(states_walk(- list(paths), maps.test_files, changed_tests=changed_tests, contents=contents+ list(paths),+ maps.test_files,+ changed_tests=changed_tests,+ contents=contents,+ masks=masks,),maps,)diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/actions.py b/packages/core/src/repowise/core/persistence/crud/analysis/actions.pyindex 788155e3a..86078382e 100644--- a/packages/core/src/repowise/core/persistence/crud/analysis/actions.py+++ b/packages/core/src/repowise/core/persistence/crud/analysis/actions.py@@ -51,7 +51,7 @@ from repowise.core.analysis.actions.build import (with_leads,with_test_reach,)-from repowise.core.analysis.actions.facts import FileFacts+from repowise.core.analysis.actions.facts import MOCKED_ONLY_VIA, FileFactsfrom repowise.core.analysis.actions.rules.code import FIX_FIRST_ACTIONSfrom repowise.core.analysis.actions.rules.hygiene import PUBLIC_ENV_KIND, SECRET_KINDSfrom repowise.core.analysis.dead_code.risk_factors import REVIEW_ONLY_KINDS@@ -296,7 +296,11 @@ async def _test_map(session: AsyncSession, repo_id: str, files: dict[str, FileFapaths = reach_paths(files)if not paths:return {}- return {"files": with_test_reach(files, await any_tests_reaching(session, repo_id, paths))}+ rows = await any_tests_reaching(session, repo_id, paths)+ reaching = {+ path: row.reached or {"total": 0, "via": MOCKED_ONLY_VIA} for path, row in rows.items()+ }+ return {"files": with_test_reach(files, reaching)}async def _fix_first(session: AsyncSession, repo_id: str) -> dict[str, Any]:diff --git a/tests/unit/analysis/test_actions_rules.py b/tests/unit/analysis/test_actions_rules.pyindex 039b47dec..2fc4d5602 100644--- a/tests/unit/analysis/test_actions_rules.py+++ b/tests/unit/analysis/test_actions_rules.py@@ -838,6 +838,14 @@ def test_fragile_no_test_reaches_says_add_tests() -> None:assert (reach.value, reach.basis) == ("None in the code graph", "inferred")+def test_fragile_reached_only_by_replacing_tests_gives_that_reason_not_none() -> None:+ f = _file("src/a.py", tests_reaching=0, tests_reaching_via="mocked-only")+ (a,) = _run(code.fragile_file, _facts([f])).actions+ assert a.title.startswith("Add tests around `src/a.py`")+ reach = next(w for w in a.why if w.label == "tests that reach it")+ assert (reach.value, reach.basis) == ("Only tests that replace it reach it", "inferred")++def test_fragile_unknown_reach_is_not_zero() -> None:"""A failed test-map walk claims neither "add tests" nor "tests reach it"."""lead = LeadFinding("complex_method", "high", "run", 10, "")diff --git a/tests/unit/analysis/test_test_reach_mocks.py b/tests/unit/analysis/test_test_reach_mocks.pynew file mode 100644index 000000000..1f3b9fcc3--- /dev/null+++ b/tests/unit/analysis/test_test_reach_mocks.py@@ -0,0 +1,151 @@+"""Which modules a Python test replaces outright, from its text, and the reach state it leaves."""++from __future__ import annotations++import networkx as nx+import pytest++from repowise.core.analysis.test_reach import (+ build_walk_maps,+ reach_states,+ replaced_files,+ replaced_modules,+)++_T = "tests/test_x.py"+++ ("source", "expected"),+ [+ ('from unittest.mock import patch\n@patch("app.helpers")\ndef test(m): ...\n', {"app.helpers"}),+ ('from unittest import mock\nwith mock.patch("app.helpers"): ...\n', {"app.helpers"}),+ ('import unittest.mock\nunittest.mock.patch("app.helpers")\n', {"app.helpers"}),+ ('from app import pkg\nfrom unittest.mock import patch\npatch.object(pkg, "helpers")\n', {"app.pkg.helpers"}),+ ('import app.pkg as p\nfrom unittest import mock\nmock.patch.object(p, "helpers")\n', {"app.pkg.helpers"}),+ ('def test(monkeypatch):\n monkeypatch.setattr("app.helpers", None)\n', {"app.helpers"}),+ ('import app\ndef test(monkeypatch):\n monkeypatch.setattr(app, "helpers", None)\n', {"app.helpers"}),+ ('import sys\nfrom unittest.mock import patch\npatch.dict(sys.modules, {"app.helpers": None})\n', {"app.helpers"}),+ ('import sys\ndef test(monkeypatch):\n monkeypatch.setitem(sys.modules, "app.helpers", None)\n', {"app.helpers"}),+ ],+)+def test_each_patch_form_names_the_binding_it_replaces(source: str, expected: set[str]) -> None:+ assert replaced_modules(_T, source) == expected+++ "source",+ [+ # The original stays reachable through the double, or may.+ 'from unittest.mock import patch\npatch("app.helpers", wraps=object())\n',+ 'from unittest.mock import patch\npatch("app.helpers", side_effect=lambda: 1)\n',+ 'from unittest.mock import patch\npatch("app.helpers", autospec=True)\n',+ # Targets built at run time.+ 'from unittest.mock import patch\nname = "app"\npatch(f"{name}.helpers")\n',+ 'from unittest.mock import patch\npatch("app" + ".helpers")\n',+ 'from unittest.mock import patch\nT = "app.helpers"\npatch(T)\n',+ # An owner the file does not bind once by an absolute import.+ 'def test(monkeypatch, mod):\n monkeypatch.setattr(mod, "helpers", None)\n',+ 'from . import pkg\nfrom unittest.mock import patch\npatch.object(pkg, "helpers")\n',+ 'import app\napp = object()\nfrom unittest.mock import patch\npatch.object(app, "helpers")\n',+ # Not a patch, not ``sys.modules``, not a fixture this lexicon knows.+ 'def test(client):\n client.patch("/app.helpers")\n',+ 'def test(monkeypatch):\n monkeypatch.setitem(CONFIG, "app.helpers", None)\n',+ 'def test(mp):\n mp.setattr("app.helpers", None)\n',+ 'import sys\nfrom unittest.mock import patch\nkey = "a"\npatch.dict(sys.modules, {key: None})\n',+ # A ``patch`` the file does not import from the mock library.+ 'def patch(x): ...\npatch("app.helpers")\n',+ 'from requests import patch\npatch("app.helpers")\n',+ 'import app\nfrom unittest.mock import patch\ndef app(): ...\npatch.object(app, "helpers")\n',+ # The file imports the module itself, so another test in it may run it.+ 'import app.helpers\nfrom unittest.mock import patch\npatch("app.helpers")\n',+ 'from app.helpers import run\nfrom unittest.mock import patch\npatch("app.helpers")\n',+ # Unparseable.+ 'from unittest.mock import patch\npatch("app.helpers"\n',+ ],+)+def test_anything_unproven_replaces_nothing(source: str) -> None:+ assert replaced_modules(_T, source) == frozenset()+++def test_only_python_test_files_are_read() -> None:+ assert replaced_modules("tests/x.test.ts", 'patch("app.helpers")') == frozenset()+ assert replaced_modules(_T, b'from unittest.mock import patch\npatch("app.helpers")\n') == {+ "app.helpers"+ }+++def test_a_patch_masks_a_file_only_when_its_binding_is_that_module() -> None:+ files = {"app/__init__.py", "app/helpers.py", "app/core.py", _T}+ texts = {+ # A function inside core.py: the test still runs the rest of core.py.+ _T: 'from unittest.mock import patch\npatch("app.core.run")\npatch("app.helpers")\n',+ "tests/test_y.py": 'from unittest.mock import patch\npatch("app.core.run")\n',+ "tests/test_z.py": 'from unittest.mock import patch\npatch("other.lib")\n',+ }+ assert replaced_files(texts, files) == {_T: frozenset({"app/helpers.py"})}+++# ---------------------------------------------------------------------------+# The ``mocked_only`` state+# ---------------------------------------------------------------------------+++def _graph(tests: dict[str, str], *, imports: tuple[tuple[str, str], ...] = ()) -> nx.MultiDiGraph:+ """Each test calls ``run`` in the file it maps to; *imports* adds file edges."""+ g = nx.MultiDiGraph()+ for path in {*tests, *tests.values(), "src/types.py", *(p for e in imports for p in e)}:+ g.add_node(path, node_type="file", is_test=path.startswith("tests/"))+ for test, source in tests.items():+ g.add_edge(test, f"{test}::test_it", edge_type="defines")+ g.add_edge(source, f"{source}::run", edge_type="defines")+ g.add_edge(f"{test}::test_it", f"{source}::run", edge_type="calls")+ for src, dst in imports:+ g.add_edge(src, dst, edge_type="imports")+ return g+++def test_a_file_only_replacing_tests_reach_is_mocked_only_not_unreached() -> None:+ maps = build_walk_maps(_graph({"tests/test_a.py": "src/a.py", "tests/test_b.py": "src/b.py"}))+ masks = {"tests/test_a.py": frozenset({"src/a.py"})}+ rows = reach_states(maps, ["src/a.py", "src/b.py"], masks=masks)+ assert rows["src/a.py"].state == "mocked_only"+ assert rows["src/a.py"].reached is None+ assert rows["src/a.py"].mocked_by == ("tests/test_a.py",)+ assert rows["src/b.py"].state == "reached"+ # Without masks the same walk reads both as reached.+ assert reach_states(maps, ["src/a.py"])["src/a.py"].state == "reached"+++def test_a_masked_tier_falls_through_to_a_test_that_runs_the_file() -> None:+ maps = build_walk_maps(+ _graph({"tests/test_a.py": "src/a.py"}, imports=(("tests/test_c.py", "src/a.py"),))+ )+ rows = reach_states(maps, ["src/a.py"], masks={"tests/test_a.py": {"src/a.py"}})+ assert rows["src/a.py"].state == "reached"+ assert rows["src/a.py"].reached.tests == ["tests/test_c.py"]+ assert rows["src/a.py"].reached.via == "import-graph"+++def test_a_replacing_test_is_dropped_from_a_tier_another_test_answers() -> None:+ g = _graph({"tests/test_a.py": "src/a.py", "tests/test_b.py": "src/a.py"})+ rows = reach_states(build_walk_maps(g), ["src/a.py"], masks={"tests/test_a.py": {"src/a.py"}})+ reached = rows["src/a.py"].reached+ assert rows["src/a.py"].state == "reached"+ assert (reached.tests, reached.total, reached.via) == (["tests/test_b.py"], 1, "call-graph")+ assert set(reached.reach) == {"tests/test_b.py"}+++def test_type_only_outranks_mocked_only_and_a_replacing_changed_test_is_no_evidence() -> None:+ maps = build_walk_maps(_graph({"tests/test_a.py": "src/a.py", "tests/test_t.py": "src/types.py"}))+ masks = {"tests/test_a.py": {"src/a.py"}, "tests/test_t.py": {"src/types.py"}}+ rows = reach_states(+ maps,+ ["src/a.py", "src/types.py"],+ masks=masks,+ changed_tests=["tests/test_a.py"],+ contents={"src/types.py": "from typing import Protocol\nclass P(Protocol): ...\n"},+ )+ assert rows["src/types.py"].state == "type_only"+ assert rows["src/a.py"].state == "mocked_only"+ assert rows["src/a.py"].mocked_by == ("tests/test_a.py",)diff --git a/tests/unit/persistence/test_actions_loader.py b/tests/unit/persistence/test_actions_loader.pyindex d2e15b436..2d6d1aa7e 100644--- a/tests/unit/persistence/test_actions_loader.py+++ b/tests/unit/persistence/test_actions_loader.py@@ -366,6 +366,20 @@ async def test_a_test_reaching_through_a_helper_turns_add_tests_into_simplify(asassert facts.files["src/core.py"].tests_reaching == 1+async def test_a_file_only_replacing_tests_reach_reads_mocked_only(async_session, monkeypatch) -> None:+ from repowise.core.analysis.test_reachability import ReachRow+ from repowise.core.persistence.crud.analysis import actions as loader++ rid = await _seed(async_session)++ async def mocked(_session, _repo_id, paths):+ return {p: ReachRow("mocked_only", mocked_by=("tests/test_core.py",)) for p in paths}++ monkeypatch.setattr(loader, "any_tests_reaching", mocked)+ core = (await loader.load_repo_facts(async_session, rid)).files["src/core.py"]+ assert (core.tests_reaching, core.tests_reaching_via) == (0, "mocked-only")++async def test_a_failed_test_map_walk_is_unknown_not_zero(async_session, monkeypatch) -> None:from repowise.core.persistence.crud.analysis import actions as loaderdiff --git a/tests/unit/persistence/test_test_reachability_walk.py b/tests/unit/persistence/test_test_reachability_walk.pyindex 905bcf1c8..1f364b0ec 100644--- a/tests/unit/persistence/test_test_reachability_walk.py+++ b/tests/unit/persistence/test_test_reachability_walk.py@@ -721,11 +721,73 @@ async def test_any_reach_passes_through_the_package_entry_tests_import(tmp_path,clear_test_map_cache()found = await any_tests_reaching(async_session, repo.id, ["src/core/Ky.ts", "src/retry.ts"])- assert found["src/core/Ky.ts"].tests == ["test/x.test.ts"]- assert found["src/core/Ky.ts"].via == "import-graph"+ assert found["src/core/Ky.ts"].reached.tests == ["test/x.test.ts"]+ assert found["src/core/Ky.ts"].reached.via == "import-graph"# The helper hop stops at pool.ts (source imports it too), but the import# closure does not: a test importing pool.ts runs retry.ts, so it is not unreached.- assert found["src/retry.ts"].entries == {"test/x.test.ts": frozenset({"src/pool.ts"})}+ assert found["src/retry.ts"].reached.entries == {"test/x.test.ts": frozenset({"src/pool.ts"})}+++_SWAPPED = {+ "app/__init__.py": "",+ "app/core.py": "from app import util\n\ndef run():\n return util.helper()\n",+ "app/util.py": "def helper():\n return 1\n",+ "tests/test_core.py": (+ "from unittest.mock import patch\n\nfrom app.core import run\n\n"+ '@patch("app.util")\ndef test_run(m):\n assert run()\n'+ ),+ "tests/test_util.py": "from app.util import helper\n\ndef test_helper():\n assert helper()\n",+}+++async def test_any_reach_reads_which_tests_replace_a_module_from_the_checkout(+ tmp_path, async_session+):+ from repowise.core.analysis.test_reachability import (+ any_tests_reaching,+ cached_test_masks,+ clear_test_map_cache,+ )+ from repowise.core.persistence.crud.graph import batch_upsert_graph_edges+ from repowise.core.pipeline.persist import _edge_row, persist_graph_nodes+ from tests.unit.analysis.test_public_api_flag import build_fixture++ both = tmp_path / "both"+ both.mkdir()+ builder = build_fixture(both, _SWAPPED)+ repo = await insert_repo(async_session, local_path=str(both))+ await persist_graph_nodes(async_session, repo.id, builder, ep_scores={})+ edges = [_edge_row(u, v, d) for u, v, d in builder.graph().edges(data=True)]+ await batch_upsert_graph_edges(async_session, repo.id, edges)+ clear_test_map_cache()++ assert await cached_test_masks(async_session, repo.id) == {+ "tests/test_core.py": frozenset({"app/util.py"})+ }+ found = await any_tests_reaching(async_session, repo.id, ["app/core.py", "app/util.py"])+ assert found["app/core.py"].reached.tests == ["tests/test_core.py"]+ assert found["app/util.py"].reached.tests == ["tests/test_util.py"]++ # Without the other test, util.py is reached by a replacing test alone.+ alone = tmp_path / "alone"+ alone.mkdir()+ builder = build_fixture(alone, {k: v for k, v in _SWAPPED.items() if k != "tests/test_util.py"})+ repo = await insert_repo(async_session, name="swapped-only", local_path=str(alone))+ await persist_graph_nodes(async_session, repo.id, builder, ep_scores={})+ edges = [_edge_row(u, v, d) for u, v, d in builder.graph().edges(data=True)]+ await batch_upsert_graph_edges(async_session, repo.id, edges)+ clear_test_map_cache()+ found = await any_tests_reaching(async_session, repo.id, ["app/util.py"])+ assert found["app/util.py"].state == "mocked_only"+ assert found["app/util.py"].mocked_by == ("tests/test_core.py",)++ # No checkout on disk: no masks, so the test keeps its reach.+ repo = await insert_repo(async_session, name="elsewhere", local_path=str(tmp_path / "gone"))+ await persist_graph_nodes(async_session, repo.id, builder, ep_scores={})+ await batch_upsert_graph_edges(async_session, repo.id, edges)+ clear_test_map_cache()+ found = await any_tests_reaching(async_session, repo.id, ["app/util.py"])+ assert found["app/util.py"].state == "reached"async def test_reached_files_are_walked_once_per_index_state(async_session, monkeypatch):@@ -894,6 +956,7 @@ async def test_the_stored_walk_and_the_in_memory_walk_return_the_same_rows(tmp_preach_states,reach_states_from_db,reaching_by_tier,+ replaced_files,)from repowise.core.persistence.crud.graph import batch_upsert_graph_edgesfrom repowise.core.pipeline.persist import _edge_row, persist_graph_nodes@@ -923,6 +986,17 @@ async def test_the_stored_walk_and_the_in_memory_walk_return_the_same_rows(tmp_passert in_memory["app/types.py"].state == "type_only"assert {in_memory[p].state for p in in_memory} >= {"reached", "unreached", "new"}+ # Both tests swap ``app.util`` out, so only replacing tests reach it.+ swap = 'from unittest.mock import patch\n\n@patch("app.util")\ndef test_swapped(m): ...\n'+ masks = replaced_files(dict.fromkeys(["tests/test_core.py", "tests/test_pkg.py"], swap), _PARITY)+ assert masks == {t: {"app/util.py"} for t in ("tests/test_core.py", "tests/test_pkg.py")}+ in_memory = reach_states(maps, asked, masks=masks, **evidence)+ assert await reach_states_from_db(async_session, repo.id, asked, masks=masks, **evidence) == (+ in_memory+ )+ assert in_memory["app/util.py"].state == "mocked_only"+ assert in_memory["app/core.py"].state == "reached"+async def test_stored_call_hubs_match_the_in_memory_ones(async_session):"""Distinct reliable callers, tests included; a name-guessed call does not count."""