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(impacted-tests): run tests the graph cannot see into with every subset, and --explain (#3217)
Raghav Chamadiyad9ebaf3707af
diff --git a/docs/reference/CLI_REFERENCE.md b/docs/reference/CLI_REFERENCE.mdindex 39fc00e05..0ed342fd4 100644--- a/docs/reference/CLI_REFERENCE.md+++ b/docs/reference/CLI_REFERENCE.md@@ -1069,6 +1069,7 @@ line up.| `--staged` | Diff the staged changes (`git diff --cached`); the default with no range outside CI (in CI: the pull request's change) || `--format` | `table` (default), `json` (full report plus the selection), `list` (test ids one per line), or `args` (one line of runner arguments, or `:all`; reasons on stderr) || `--runner` | For `--format args`: `auto` (default; `files` when mixed), `pytest` (node ids or files), `go` (package dirs), `jest` (files; pass with `--runTestsByPath`), `files` |+| `--explain` | A test file or node id: say why it was or was not selected (the changed file, the evidence and the import route, or the rule that runs it). Added to `--format json` as `explain` |```bashrepowise impacted-tests # staged changes@@ -1077,6 +1078,7 @@ repowise impacted-tests main..HEAD # a plain rangerepowise impacted-tests abc123 # a single commitrepowise impacted-tests main..HEAD --format list | xargs pytestrepowise impacted-tests main...HEAD --format args --runner pytest+repowise impacted-tests main...HEAD --explain tests/unit/test_api.py````--format args` exits `0` whether it selects a subset or everything, and `2`@@ -1085,7 +1087,8 @@ in the config. `--format json` adds `indexed_commit`, `map_current` and aper-file `selected.basis`: `full-run`, `no-tests-needed`, `test-tree`,`test-package`, `conftest`, `helper-importers`, `deleted-test`, `coverage`,`changed-test`, `call-graph`, `import-graph`, `filename-pattern`, `unknown`,-or `none` (no index). When it runs everything:+or `none` (no index), and a per-test `selected.why`: the first changed file and+evidence that selected it, or why it runs with every subset. When it runs everything:[CI](../start/CI.md#selecting-the-tests-a-change-needs).---diff --git a/docs/start/CI.md b/docs/start/CI.mdindex 86b4f9dce..715cc091a 100644--- a/docs/start/CI.md+++ b/docs/start/CI.md@@ -257,11 +257,12 @@ a per-test coverage map (`repowise coverage add`) makes it more precise.- It also runs everything for: a missing or unreadable index; a per-test map at its row cap; a deleted file no known test used.- An index built at an older commit than the change's base is used as is. Files changed in between whose imports or module-name strings moved (any change beyond whitespace, outside Python and JavaScript/TypeScript) add the tests reaching them. It runs everything instead when that commit is not in the clone, or more than 1,000 files changed in between, or one of them is a lockfile, manifest, build or test config or `.repowise/config.yaml`. CI config and test data changed in between no longer force a full run: neither is imported.- Only `docs/` and root README-like files (README, CHANGELOG, LICENSE, CONTRIBUTING) are skipped, and only when no code names them (a test that reads `README.md` names it); an empty line means only those changed.-- Tests the graph cannot see into (not indexed, or importing nothing it resolves, such as a test that only runs a subprocess) run with every subset. A Python module named like a test but outside every test directory is not a test when the nearest pytest config's `testpaths` or `python_files` leaves it out, as a bare `pytest` does: it is selected through like any other code. The rule follows a bare `pytest` run, so a test that CI only names explicitly on the command line outside `testpaths` is not seen as one: list it in `tests.always_run`.+- Tests the graph cannot see into run with every subset, and the reasons say how many and why: a test not indexed or importing nothing it resolves; a Python, JavaScript or TypeScript test that lists and reads files under a source directory (the working directory, a module's location, or its own location climbed out of the test directory; never its fixtures, sibling data or a temporary directory); one that runs the project's own command, `python -m` module, code importing the project, or package `bin` in a child process. The indexer finds them once from each test's source and stores the reason, so selection reads it without opening test files. An index built before this check runs everything until `repowise update` checks its tests. A Python module named like a test but outside every test directory is not a test when the nearest pytest config's `testpaths` or `python_files` leaves it out, as a bare `pytest` does: it is selected through like any other code. The rule follows a bare `pytest` run, so a test that CI only names explicitly on the command line outside `testpaths` is not seen as one: list it in `tests.always_run`.- A changed `conftest.py`, test package `__init__.py` or imported test helper selects the tests under or importing it, not everything.- Without a range, in CI it reads the pull request's change; locally, the staged changes.- `tests.full_run_on` (gitignore patterns) adds run-everything paths; `tests.always_run` is appended to every selection as written.-- Ceiling: on the JVM and .NET, a same-package test linked only by an unresolved call is missed. A test that reaches the change only through a subprocess, a plugin or registry loaded by a string name, or a directory it globs, while importing something else, is missed too: list such tests in `tests.always_run`, and run the full suite on the default branch.+- `--explain <test>` says why one test file was or was not selected: the first changed file that reached it, the evidence (coverage, call graph, import graph) and the import route from the test to that file when there is one, or the rule that runs it. With `--format json` the same answer is added as `explain`, and `selected.why` holds it for every selected test.+- Ceiling: on the JVM and .NET, a same-package test linked only by an unresolved call is missed. So is a test that reaches the change through a plugin or registry loaded by a string name, a tree walk or child process done inside a shared helper it calls, or one in another language: list such tests in `tests.always_run`, and run the full suite on the default branch.**What to expect.** The subset is only as small as the code is loosely coupled.On tightly coupled code the graph alone picks most tests (over 80% of testdiff --git a/packages/cli/src/repowise/cli/commands/impacted_tests_cmd.py b/packages/cli/src/repowise/cli/commands/impacted_tests_cmd.pyindex 130e5dd0f..07a46312c 100644--- a/packages/cli/src/repowise/cli/commands/impacted_tests_cmd.py+++ b/packages/cli/src/repowise/cli/commands/impacted_tests_cmd.py@@ -25,6 +25,7 @@ Examples:repowise impacted-tests abc123 # a single commitrepowise impacted-tests main..HEAD --format list | xargs pytestrepowise impacted-tests main...HEAD --format args --runner pytest+ repowise impacted-tests main...HEAD --explain tests/unit/test_api.py"""from __future__ import annotations@@ -34,6 +35,7 @@ from pathlib import Pathfrom typing import TYPE_CHECKING, NamedTupleimport click+import structlogfrom rich.table import Tablefrom repowise.cli.ci import (@@ -58,6 +60,8 @@ from repowise.core.analysis.test_selection import RUNNERSif TYPE_CHECKING:from repowise.core.pytest_roots import PytestRoots+log = structlog.get_logger(__name__)+# The whole reverse-import closure: a test importing a module that imports the# changed file runs it too. No depth limit: the walk ends when no node gains a# new seed, so a cut-off can never drop a test.@@ -106,8 +110,22 @@ def _resolve_repo_path(path: str | None, fmt: str):help="Who --format args is for: pytest (node ids and files), go (package dirs), ""jest (files), files, or auto (from the selected test files; mixed means files).",)+ "--explain",+ "explain",+ default=None,+ metavar="TEST",+ help="Say why one test file (or node id) was or was not selected: the changed file and "+ "the route that reached it, or the rule that runs it. With --format json it is added "+ "to the report as 'explain'.",+)def impacted_tests_command(- revspec: str | None, repo: str | None, staged: bool, fmt: str, runner: str+ revspec: str | None,+ repo: str | None,+ staged: bool,+ fmt: str,+ runner: str,+ explain: str | None,) -> None:"""Print the tests whose coverage intersects a change's changed lines."""if revspec and staged:@@ -119,23 +137,27 @@ def impacted_tests_command(repo_path = _resolve_repo_path(repo, fmt)try:- change, config = _read_change(repo_path, revspec, staged, fmt)+ change, config = _read_change(repo_path, revspec, staged, fmt, bool(explain))except CannotEvaluateError as exc:cannot_evaluate(fmt, exc.code, str(exc))+ explain = explain.replace("\\", "/").removeprefix("./") if explain else Nonecheckout = _read_checkout(repo_path)- result = run_async(_collect(repo_path, change, checkout.roots, config))+ result = run_async(_collect(repo_path, change, checkout.roots, config, explain))result["diff"] = change.labelif config is not None:result["selection"] = _select(repo_path, change, result, config, checkout)- _render(result, fmt, runner)+ if explain and fmt != "json":+ _render_explain(result, explain)+ return+ _render(result, fmt, runner, explain)-def _read_change(repo_path, revspec: str | None, staged: bool, fmt: str):+def _read_change(repo_path, revspec: str | None, staged: bool, fmt: str, explain: bool = False):"""``(change set, selection config or None)``, or :class:`CannotEvaluateError`."""from repowise.core.ci.base import in_ci- config = _selection_config(repo_path) if fmt in ("json", "args") else None+ config = _selection_config(repo_path) if explain or fmt in ("json", "args") else Noneif revspec is None and not staged and in_ci():# In CI the change to test is the pull request's, not the staged index.revspec = ci_revspec(str(repo_root(str(repo_path))), None)@@ -173,11 +195,18 @@ def _read_checkout(repo_path) -> _Checkout:return _Checkout(tracked, texts, roots)-async def _collect(repo_path, change, roots: PytestRoots | None = None, config=None) -> dict:+async def _collect(+ repo_path,+ change,+ roots: PytestRoots | None = None,+ config=None,+ explain: str | None = None,+) -> dict:"""Resolve the change's files to impacted tests + labelled fallbacks.With a selection *config*, files changed since the index was built are- walked too, so selection can add the tests reaching them.+ walked too, so selection can add the tests reaching them. With *explain*,+ the import route from that test to a changed file is looked up as well."""from repowise.core.persistence.crud import (get_health_metrics,@@ -212,20 +241,50 @@ async def _collect(repo_path, change, roots: PytestRoots | None = None, config=Nsession, repo_id, _query_lines(change, measured), repo_keys, out, roots, routes)await _place_tests(session, repo_id, out)+ if explain:+ await _explain_route(session, repo_id, explain, change, out)return out+async def _explain_route(session, repo_id: str, test: str, change, out: dict) -> None:+ """The dependency route from *test* to a changed file, for ``--explain`` only."""+ from repowise.core.analysis.test_reachability import dependency_path++ changed = [*change.files, *change.deleted]+ try:+ out["explain_route"] = await dependency_path(+ session, repo_id, test.split("::", 1)[0], changed+ )+ except Exception as exc: # the explanation still stands without its route+ log.debug("explain_route_failed", test=test, error=str(exc))+ out["explain_route"] = []++async def _place_tests(session, repo_id: str, out: dict) -> None:- """Record the tests the graph can see into, which selection needs to find the rest."""- from repowise.core.analysis.test_reachability import placed_test_files+ """Record the tests the graph can see into, and those the indexer found it cannot."""+ from repowise.core.analysis.test_reachability import (+ always_run_test_files,+ placed_test_files,+ unscanned_test_files,+ )if out["graph_error"] is not None:returntry:out["placed_tests"] = await placed_test_files(session, repo_id)+ out["always_run_tests"] = await always_run_test_files(session, repo_id)+ unscanned = sorted(await unscanned_test_files(session, repo_id) & out["placed_tests"])except Exception as exc:out["graph_error"] = f"{type(exc).__name__}: {exc}"+ return+ # An index older than the check cannot say which tests walk the tree, so+ # it is out of date like any other; unplaced tests run anyway.+ if unscanned and out["index_problem"] is None:+ out["index_problem"] = (+ f"The index has not checked {len(unscanned)} test file(s) for walking the source "+ f"tree or running the project (e.g. {unscanned[0]}); run `repowise update`."+ )def _indexed_commit(repo_path, row_commit: str | None, out: dict) -> None:@@ -315,6 +374,8 @@ def _empty_result(changed_files: int) -> dict:"gap": None,"graph_error": None,"placed_tests": None,+ "always_run_tests": {},+ "explain_route": [],"helper_importers": {},"changed_files": changed_files,"covered": {}, # test_id -> {test_file, source_files: [...]}@@ -553,6 +614,7 @@ def _select(repo_path, change, result: dict, config, checkout: _Checkout):doc_readers=doc_readers(docs, _texts(root, sources)) if docs else {},plugin_loader=plugin_loader(checkout.pytest_texts),unplaced_tests=[] if placed is None else [t for t in known_tests if t not in placed],+ always_run_tests=result["always_run_tests"],))@@ -584,9 +646,9 @@ def _machine_test_ids(result: dict) -> list[str]:return [i for i in ids if not (i in seen or seen.add(i))]-def _render(result: dict, fmt: str, runner: str = "auto") -> None:+def _render(result: dict, fmt: str, runner: str = "auto", explain: str | None = None) -> None:if fmt in ("json", "args"):- _render_selection(result, fmt, runner)+ _render_selection(result, fmt, runner, explain)returnif fmt == "list":@@ -608,7 +670,23 @@ def _render(result: dict, fmt: str, runner: str = "auto") -> None:_render_table(result)-def _render_selection(result: dict, fmt: str, runner: str) -> None:+def _explanation(result: dict, test: str) -> dict:+ """``--explain``: the selection's own record for *test*, plus the route behind it."""+ from repowise.core.analysis.test_selection import explain_test, selected_by_change++ selected, lines = explain_test(result["selection"], test)+ route = result["explain_route"] if selected_by_change(result["selection"], test) else []+ if len(route) > 1:+ lines.append("Route: " + " -> ".join(route))+ return {"test": test, "selected": selected, "lines": lines, "route": route}+++def _render_explain(result: dict, test: str) -> None:+ for line in _explanation(result, test)["lines"]:+ click.echo(line)+++def _render_selection(result: dict, fmt: str, runner: str, explain: str | None = None) -> None:"""``--format args`` (one line, reasons on stderr) or ``--format json``."""from repowise.core.analysis.test_selection import format_args, resolve_runner, runner_args@@ -628,6 +706,7 @@ def _render_selection(result: dict, fmt: str, runner: str) -> None:selected = selection.to_dict()run_all, reasons = selected.pop("run_all"), selected.pop("reasons")+ extra = {"explain": _explanation(result, explain)} if explain else {}emit_json({"diff": result["diff"],@@ -652,6 +731,7 @@ def _render_selection(result: dict, fmt: str, runner: str) -> None:"selected": selected,"runner": resolved,"args": args,+ **extra,})diff --git a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.pyindex a05e4fd96..f8a4126ed 100644--- a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py+++ b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py@@ -1668,8 +1668,13 @@ def parser_changed(repo_path: Path) -> bool:row with the running ``parser_fingerprint()``. An unstamped row, or a storethat cannot be read, is **not** a change, for the reason:func:`health_analyzer_changed` gives: the next commit's widen stamps it.++ Also true when a test file was never checked for walking the source tree+ (an index built before that check): selection would run everything until+ the re-parse stamps it, so a quiet repo must not take the shortcut."""from repowise.cli.helpers import get_db_url_for_repo+ from repowise.core.analysis.test_reachability import unscanned_test_filesfrom repowise.core.ingestion.parse_cache import parser_fingerprintfrom repowise.core.persistence import (create_engine,@@ -1678,22 +1683,25 @@ def parser_changed(repo_path: Path) -> bool:get_session,)- async def _stored() -> str | None:+ async def _stored() -> tuple[str | None, bool]:engine = create_engine(get_db_url_for_repo(repo_path))try:sf = create_session_factory(engine)async with get_session(sf) as session:repo = await get_repository_by_path(session, str(repo_path))- return repo.graph_edges_parser_fingerprint if repo is not None else None+ if repo is None:+ return None, False+ unscanned = bool(await unscanned_test_files(session, repo.id))+ return repo.graph_edges_parser_fingerprint, unscannedfinally:await engine.dispose()try:- stored = run_async(_stored())+ stored, unscanned = run_async(_stored())except Exception as exc:log.debug("parser_change_check_failed", error=str(exc))return False- return stored is not None and stored != parser_fingerprint()+ return unscanned or (stored is not None and stored != parser_fingerprint())def health_analyzer_changed(state: dict) -> bool:diff --git a/packages/core/alembic/versions/0097_graph_node_always_run_reason.py b/packages/core/alembic/versions/0097_graph_node_always_run_reason.pynew file mode 100644index 000000000..53b320847--- /dev/null+++ b/packages/core/alembic/versions/0097_graph_node_always_run_reason.py@@ -0,0 +1,36 @@+"""graph_nodes: why a test file runs with every selected subset.++``always_run_reason`` is set at index time on a test file that lists and reads+files under a source directory, or runs the project's own command or module in+a child process. The dependency graph cannot say what such a test exercises,+so test selection runs it with every subset. NULL everywhere else; rows stored+before the column fill in on the next index or update.++Local SQLite stores get the column from ``init_db``'s additive reconciler; this+migration covers managed Postgres.++Revision ID: 0097+Revises: 0096+Create Date: 2026-10-10+"""++from __future__ import annotations++from collections.abc import Sequence++import sqlalchemy as sa+from alembic import op++# revision identifiers+revision: str = "0097"+down_revision: str | None = "0096"+branch_labels: str | Sequence[str] | None = None+depends_on: str | Sequence[str] | None = None+++def upgrade() -> None:+ op.add_column("graph_nodes", sa.Column("always_run_reason", sa.Text(), nullable=True))+++def downgrade() -> None:+ op.drop_column("graph_nodes", "always_run_reason")diff --git a/packages/core/src/repowise/core/analysis/test_reachability.py b/packages/core/src/repowise/core/analysis/test_reachability.pyindex d644a5b26..02df2f611 100644--- a/packages/core/src/repowise/core/analysis/test_reachability.py+++ b/packages/core/src/repowise/core/analysis/test_reachability.py@@ -443,6 +443,75 @@ async def placed_test_files(session: AsyncSession, repo_id: str) -> set[str]:return {row[0] for row in rows}+async def always_run_test_files(session: AsyncSession, repo_id: str) -> dict[str, str]:+ """``{test file: why}`` for the tests the indexer found the graph cannot see into.++ Stored per file at index time (``repowise.core.ingestion.always_run``), so+ this is one indexed read, never a scan of the test files.+ """+ res = await session.execute(+ select(GraphNode.node_id, GraphNode.always_run_reason).where(+ GraphNode.repository_id == repo_id,+ GraphNode.always_run_reason.is_not(None),+ GraphNode.always_run_reason != "",+ )+ )+ return {node_id: reason for node_id, reason in res.all()}+++async def unscanned_test_files(session: AsyncSession, repo_id: str) -> set[str]:+ """Judged test files the indexer never stamped (an index built before the check).++ A stamped test holds ``""`` when it is ordinary, so ``NULL`` means unknown,+ not "nothing found": such a test may walk the tree unseen.+ """+ from ..ingestion.always_run import is_judged_test++ res = await session.execute(+ select(GraphNode.node_id).where(+ GraphNode.repository_id == repo_id,+ GraphNode.node_type == "file",+ GraphNode.is_test == True, # noqa: E712+ GraphNode.always_run_reason.is_(None),+ )+ )+ return {node_id for (node_id,) in res.all() if is_judged_test(node_id)}+++async def dependency_path(+ session: AsyncSession, repo_id: str, source: str, targets: Collection[str], max_depth: int = 64+) -> list[str]:+ """The shortest file-dependency route from *source* to any of *targets*, ends included.++ ``[]`` when none is found within *max_depth* hops. Breadth-first, one ``IN``+ query per level, over the same edges the import walk follows+ (``FILE_DEPENDENCY_EDGE_TYPES``, every resolution origin). Ceiling: a test+ found through the call walk alone may have no such route; following call+ edges with ``UNRELIABLE_CALL_ORIGINS`` excluded is the upgrade path.+ """+ goals = set(targets)+ parent: dict[str, str | None] = {source: None}+ frontier = [source]+ for _ in range(max_depth):+ if not frontier or goals & parent.keys():+ break+ level, frontier = frontier, []+ for dependent, dependency in await _edges_from(+ session, repo_id, level, sorted(FILE_DEPENDENCY_EDGE_TYPES)+ ):+ if dependency not in parent:+ parent[dependency] = dependent+ frontier.append(dependency)+ hit = goals & parent.keys()+ if not hit:+ return []+ route, node = [], min(hit)+ while node is not None:+ route.append(node)+ node = parent[node]+ return route[::-1]++async def tests_reaching(session: AsyncSession,repo_id: str,diff --git a/packages/core/src/repowise/core/analysis/test_selection.py b/packages/core/src/repowise/core/analysis/test_selection.pyindex bfc6cf2a0..7d7425668 100644--- a/packages/core/src/repowise/core/analysis/test_selection.py+++ b/packages/core/src/repowise/core/analysis/test_selection.py@@ -28,13 +28,16 @@ is chosen when any of these hold:Otherwise the subset is the covering tests, the tests the graph shows reachingthe changed files (a changed test, the call graph, the import graph) and-``tests.always_run``, plus every test the graph cannot see into (not indexed,-or with no resolved edge), plus the tests reaching files whose imports moved-between the indexed commit and the base (:func:`plan_gap`). A test package's-``__init__.py`` or a ``conftest.py`` (changed, deleted, or on a route to a-changed file) stands for every test under its directory. A helper module tests-import stands for the tests that import it, directly or through other helpers-(basis ``helper-importers``), which are the files that run it: Python runs the+``tests.always_run``, plus every test the graph cannot see into: one not+indexed or with no resolved edge, and one the indexer found listing and reading+files under a source directory or running the project's own command or module+in a child process (it imports one module, but exercises far more). The tests+reaching files whose imports moved between the indexed commit and the base+(:func:`plan_gap`) run too. A test package's ``__init__.py`` or a+``conftest.py`` (changed, deleted, or on a route to a changed file) stands for+every test under its directory. A helper module tests import stands for the+tests that import it, directly or through other helpers (basis+``helper-importers``), which are the files that run it: Python runs thepackage file for each module in it, and pytest loads a conftest for each testat or below it. Only documentation (``docs/`` and the root README, CHANGELOG,LICENSE and the like, never code) that no code names is skipped without a test.@@ -234,7 +237,9 @@ class Selection:``no-tests-needed``, ``deleted-test``, ``test-tree``, ``test-package``,``conftest``, ``helper-importers``, ``coverage``,``changed-test``, ``call-graph``, ``import-graph``, ``filename-pattern``,- ``unknown``, or ``none`` when nothing was asked (no index).+ ``unknown``, or ``none`` when nothing was asked (no index). ``why`` says,+ per selected test file, what put it in: the changed file and evidence that+ reached it, or the reason it runs with every subset."""run_all: bool@@ -245,6 +250,7 @@ class Selection:always_run: tuple[str, ...] = ()skipped_files: tuple[str, ...] = ()basis: Mapping[str, str] = field(default_factory=dict)+ why: Mapping[str, str] = field(default_factory=dict)def to_dict(self) -> dict[str, Any]:return {@@ -256,6 +262,7 @@ class Selection:"always_run": list(self.always_run),"skipped_files": list(self.skipped_files),"basis": dict(self.basis),+ "why": dict(self.why),}@@ -410,7 +417,9 @@ class SelectionInput:expands to. *doc_readers* maps a changed doc to a file naming it(:func:`doc_readers`); *plugin_loader* is a file loading pytest plugins byname (:func:`plugin_loader`); *unplaced_tests* are tests the graph cannot- see into (not indexed, or with no resolved edge), which every subset runs.+ see into (not indexed, or with no resolved edge), and *always_run_tests*+ maps each test the indexer found walking the source tree or running the+ project in a child process to why; every subset runs both."""changed: Collection[str]@@ -433,6 +442,7 @@ class SelectionInput:# :func:`plan_gap` for *index_gap*, with the targets whose rows *tiers* also# holds; None when the caller did not trace it, so any code there runs all.gap: GapPlan | None = None+ always_run_tests: Mapping[str, str] = field(default_factory=dict)@dataclass@@ -456,30 +466,93 @@ def select_tests(inp: SelectionInput) -> Selection:evidence = _Evidence.of(inp, deleted)per_file, file_reasons = _tests_per_file(triage.code, evidence)traced = bool(triage.code and inp.gap and inp.gap.targets)- gap_tests, route_reasons = (- _gap_route_tests(inp.gap.targets, paths, evidence) if traced else ([], [])+ gap_tests, route_reasons, gap_why = (+ _gap_route_tests(inp.gap.targets, paths, evidence) if traced else ([], [], {}))if inp.index_available:run_all += file_reasons + route_reasonsbasis = {**triage.basis, **{path: per_file[path][1] for path in triage.code}}- # The graph cannot say what an unplaced test reaches, so it always runs.- unplaced = [(t, t) for t in inp.unplaced_tests] if triage.code else []+ # The graph cannot say what these tests reach, so they always run.+ always = _always_running(inp) if triage.code else {}tests, test_files = _runnable(- [*(t for path in triage.code for t in per_file[path][0]), *gap_tests, *unplaced]+ [+ *(t for path in triage.code for t in per_file[path][0]),+ *gap_tests,+ *((t, t) for t in always),+ ])return Selection(run_all=bool(run_all),- reasons=tuple(run_all + _notes(inp, triage.skipped, bool(unplaced), traced)),+ reasons=tuple(run_all + _notes(inp, triage.skipped, always, traced)),tests=tests,test_files=test_files,packages=_go_packages(triage.code, deleted, inp.go_test_dirs),always_run=inp.config.always_run,skipped_files=tuple(triage.skipped),basis=basis,+ why={**gap_why, **_why(triage.code, per_file, evidence), **always},)+_UNPLACED_REASON = "the graph has no edge from it into the repository's code"+_EVERY_SUBSET = "runs with every subset: "+++def _always_running(inp: SelectionInput) -> dict[str, str]:+ """``{test file: why it runs with every subset}``; a detected reason wins.++ A detected test the checkout no longer has is left out, like any test.+ """+ known = set(inp.known_tests)+ out = {t: f"{_EVERY_SUBSET}{_UNPLACED_REASON}" for t in inp.unplaced_tests}+ for test, reason in sorted(inp.always_run_tests.items()):+ if not known or test in known:+ out[test] = f"{_EVERY_SUBSET}{reason}"+ return out+++def _why(+ code: list[str], per_file: Mapping[str, tuple[list[_TestRef], str]], ev: _Evidence+) -> dict[str, str]:+ """``{test file: the first changed file that selected it, and the evidence}``.++ Only the first: a test several changed files reach names the first of them+ in sorted order.+ """+ out: dict[str, str] = {}+ for path in code:+ tests, basis = per_file[path]+ vias = {f: via for f, via in ev.inferred.get(path, ())}+ covered = {f for _, f in ev.covered.get(path, ())}+ for _, test_file in tests:+ if test_file and test_file not in out:+ via = "coverage" if test_file in covered else vias.get(test_file, basis)+ out[test_file] = f"{path} changed ({via})"+ return out+++def selected_by_change(selection: Selection, test: str) -> bool:+ """Whether a changed file (not a rule) put *test* in the selection."""+ why = selection.why.get(test.split("::", 1)[0])+ return bool(why) and not why.startswith(_EVERY_SUBSET)+++def explain_test(selection: Selection, test: str) -> tuple[bool, list[str]]:+ """Whether *test* (a file or node id) runs for this selection, and why, in plain lines."""+ test_file = test.split("::", 1)[0]+ if selection.run_all:+ return True, ["Every test runs:", *selection.reasons]+ if why := selection.why.get(test_file):+ return True, [f"Selected: {why}."]+ if test in selection.always_run or test_file in selection.always_run:+ return True, ["Selected: it is listed in tests.always_run."]+ return False, [+ "Not selected: no changed file reaches it through coverage, the call graph or "+ "the import graph, and it is not a test that runs with every subset."+ ]++def _triage(paths: list[str], config: TestSelectionConfig, doc_readers: Mapping[str, str]) -> _Triage:@@ -536,7 +609,9 @@ def _index_reasons(inp: SelectionInput, *, has_code: bool) -> list[str]:return out-def _notes(inp: SelectionInput, skipped: list[str], unplaced: bool, traced: bool) -> list[str]:+def _notes(+ inp: SelectionInput, skipped: list[str], always: Mapping[str, str], traced: bool+) -> list[str]:"""Reasons that explain the selection without forcing a full run."""out = []if traced:@@ -545,11 +620,21 @@ def _notes(inp: SelectionInput, skipped: list[str], unplaced: bool, traced: boolf"The index predates {rewired} changed file(s) outside this change; ""the tests reaching them run too.")- if unplaced:- out.append(- f"{len(inp.unplaced_tests)} test file(s) the graph cannot see into run with "- f"every selection (e.g. {sorted(inp.unplaced_tests)[0]})."- )+ if always:+ detected = sorted(t for t in always if t in inp.always_run_tests)+ unseen = len(always) - len(detected)+ if unseen:+ example = sorted(t for t in always if t not in inp.always_run_tests)[0]+ out.append(+ f"{unseen} test file(s) the graph cannot see into run with every selection "+ f"(e.g. {example})."+ )+ if detected:+ out.append(+ f"{len(detected)} test file(s) that list source files or run the project in "+ f"a child process run with every selection (e.g. {detected[0]}: "+ f"{inp.always_run_tests[detected[0]]})."+ )if not inp.map_current and inp.tiers.get("covered"):out.append("The per-test map was measured at another commit, so covering tests "@@ -700,15 +785,19 @@ def _files_under(init: str, files: Collection[str]) -> list[str]:def _gap_route_tests(targets: Collection[str], paths: list[str], ev: _Evidence-) -> tuple[list[_TestRef], list[str]]:- """The tests reaching each gap target, and run-all reasons found on those routes."""+) -> tuple[list[_TestRef], list[str], dict[str, str]]:+ """The tests reaching each gap target, run-all reasons found on those routes, and why each runs."""tests: list[_TestRef] = []reasons: list[str] = []+ why: dict[str, str] = {}for path in sorted(set(targets) - set(paths)):- found, _, why = _file_tests(path, ev, route_only=True)+ found, _, route_reasons = _file_tests(path, ev, route_only=True)tests += found- reasons += [f"Changed after the index was built: {r}" for r in why]- return tests, reasons+ reasons += [f"Changed after the index was built: {r}" for r in route_reasons]+ for _, test_file in found:+ if test_file:+ why.setdefault(test_file, f"{path} changed after the index was built")+ return tests, reasons, why_TestRef = tuple[str, str | None] # (test id or file, the file it lives in)diff --git a/packages/core/src/repowise/core/ingestion/always_run.py b/packages/core/src/repowise/core/ingestion/always_run.pynew file mode 100644index 000000000..1aaa5bc30--- /dev/null+++ b/packages/core/src/repowise/core/ingestion/always_run.py@@ -0,0 +1,588 @@+"""Tests the dependency graph cannot see into, found from their source text.++A test's import edges say which files it exercises, so test selection can skip+it when none of those files changed. Two shapes of test break that promise+while still importing something the graph resolves:++* a **tree-walking** test lists a source directory (``rglob``, ``os.walk``,+ ``readdirSync``, a glob, ``git ls-files``) and reads what it finds, so it+ depends on every file under that root, including files added later;+* a **self-running** test starts the project's own command or module in a+ child process (a console script, ``sys.executable -m <own module>``, code+ passed to ``-c`` that imports the project, the package's ``bin``), so the+ code it exercises is reached through a process, not an import.++:func:`always_run_reason` names which, or returns ``None``. It reads only+the text it is given, so the indexer runs it once per test file while the+bytes are in memory and stores the answer on the file's graph node; selection+reads that stored answer and runs such tests with every subset.++Precision matters as much as recall here: every detected test joins every+subset. A walk only counts when its root is the source tree: the working+directory (``process.cwd()``), a module's ``__file__``, or the test's own+location (``__file__``, ``import.meta.url``, ``__dirname``) climbed out of the+test directory (``parents[2]``, ``".."``). The test's own directory counts only+when it is not a test directory (a test beside the code it checks). The root+may arrive directly, through a variable, or through a local function called+with one; a root naming fixtures, test data, snapshots or a temporary+directory never counts. The file read must sit in the walk's own function or+block, or in a block naming the walk's function or variable. Ceiling: the+check is textual and per file, so a walk done by a shared helper the test+calls (``list_tracked_files()``) is not seen; detecting walks in helpers and+marking their importers is the upgrade path. Python, JavaScript and+TypeScript only.+"""++from __future__ import annotations++import json+import re+from collections.abc import Callable, Iterable, Mapping+from dataclasses import dataclass+from pathlib import PurePosixPath+from typing import Any++_PY = ".py"+_JS = (".js", ".jsx", ".mjs", ".cjs", ".ts", ".tsx", ".mts", ".cts")+++@dataclass(frozen=True)+class _Lang:+ # Plain substrings every match of the pattern below contains: a test+ # holding none of them is passed over without running a regex.+ walk_tokens: tuple[str, ...]+ read_tokens: tuple[str, ...]+ spawn_tokens: tuple[str, ...]+ walk: re.Pattern[str]+ read: re.Pattern[str]+ # A root that is the source tree wherever the test sits.+ anchor: re.Pattern[str]+ # The test's own file or directory, and what climbs out of it from the+ # file (``climb_file``) or from a name already bound to its directory.+ local: re.Pattern[str]+ climb_file: re.Pattern[str]+ climb_dir: re.Pattern[str]+ spawn: re.Pattern[str]+ assign: re.Pattern[str]+ loop: re.Pattern[str]+ func: re.Pattern[str]+++_PY_LANG = _Lang(+ walk_tokens=("glob(", "walk(", "listdir(", "scandir(", "iterdir(", "ls-files"),+ read_tokens=("read_text(", "read_bytes(", "open(", "parse(", "tokenize."),+ spawn_tokens=("subprocess", "os.system(", "os.exec", "pexpect"),+ walk=re.compile(+ r"\.rglob\(|\bos\.walk\(|\.glob\(|\bglob\.i?glob\(|\bos\.listdir\(|\bos\.scandir\("+ r"|\.iterdir\(|ls-files"+ ),+ read=re.compile(r"read_text\(|read_bytes\(|\bopen\(|\bast\.parse\(|\btokenize\."),+ anchor=re.compile(r"\w\.__file__\b|\bPath\.cwd\(\)|\bos\.getcwd\(\)"),+ local=re.compile(r"(?<![\w.])__file__\b"),+ climb_file=re.compile(r"parents\[|\.parent\s*\.parent\b|[\"']\.\.[\"'/\\]|dirname\(.*dirname\("),+ climb_dir=re.compile(r"\.parent\b|parents\[|[\"']\.\.[\"'/\\]|dirname\("),+ spawn=re.compile(+ r"\bsubprocess\.(?:run|Popen|call|check_call|check_output)\b|create_subprocess_(?:exec|shell)"+ r"|\bos\.system\(|\bos\.exec[lv]p?e?\(|\bpexpect\.spawn"+ r"|from subprocess import"+ ),+ assign=re.compile(r"^\s*([A-Za-z_]\w*)\s*(?::[^=\n]+)?=(?!=)\s*(.*)$"),+ loop=re.compile(r"^\s*for\s+([A-Za-z_]\w*)\s+in\s+(.*)$"),+ func=re.compile(r"^\s*(?:async\s+)?def\s+([A-Za-z_]\w*)\s*\("),+)++_JS_LANG = _Lang(+ walk_tokens=("readdir", "opendir", "glob", "fg(", "fg.sync(", "ls-files"),+ read_tokens=("readFile", "createReadStream"),+ spawn_tokens=("spawn", "exec", "fork", "Bun.spawn"),+ walk=re.compile(+ r"\breaddir(?:Sync)?\s*\(|\bopendir(?:Sync)?\s*\(|\bglob(?:Sync)?\s*\(|\bfg(?:\.sync)?\s*\("+ r"|\bfs\.glob\b|ls-files"+ ),+ read=re.compile(r"\breadFile(?:Sync)?\s*\(|\bcreateReadStream\s*\("),+ anchor=re.compile(r"\bprocess\.cwd\(\)"),+ local=re.compile(r"\bimport\.meta\.(?:url|dirname|filename)\b|\b__dirname\b|\b__filename\b"),+ climb_file=re.compile(r"[\"'`]\.\.[\"'`/\\]|dirname\(.*dirname\("),+ climb_dir=re.compile(r"[\"'`]\.\.[\"'`/\\]|dirname\("),+ spawn=re.compile(+ r"\b(?:spawn|spawnSync|exec|execSync|execFile|execFileSync|fork)\s*\("+ r"|\bexeca(?:Sync|Node)?\s*\(|\bBun\.spawn"+ ),+ assign=re.compile(+ r"^\s*(?:export\s+)?(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*(?::[^=\n]+)?=(?!=)\s*(.*)$"+ ),+ loop=re.compile(r"^\s*for\s*\(\s*(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s+of\s+(.*)$"),+ func=re.compile(+ r"^\s*(?:export\s+)?(?:async\s+)?function\s*\*?\s*([A-Za-z_$][\w$]*)\s*\("+ r"|^\s*(?:export\s+)?(?:const|let)\s+([A-Za-z_$][\w$]*)\s*=\s*(?:async\s*)?"+ r"(?:\([^)]*\)|[A-Za-z_$][\w$]*)\s*(?::[^=]+)?=>"+ ),+)++# A JS spawn call only counts where a process API is imported: a bare+# ``exec(`` is also ``RegExp.prototype.exec``.+_JS_PROCESS_TOKENS = ("child_process", "execa", "Bun.spawn")+_IDENT = re.compile(r"[A-Za-z_$][\w$]*")++# A root naming any of these is a test's own material, not the source tree.+_LOCAL_ROOT = re.compile(+ r"fixture|testdata|test_data|__snapshots__|snapshot|golden|mkdtemp|tmp_path|tmpdir"+ r"|tmp_dir|tempdir|temp_dir|\btmp\b|tmpRoot|tempRoot|os\.tmpdir|gettempdir",+ re.IGNORECASE,+)++# An anchor used to load a module (``createRequire(import.meta.url)``) roots+# an import, not a directory.+_MODULE_LOAD = re.compile(r"\bcreateRequire\b|\brequire\(|\bimport\(|\bimportlib\b")++# String literals on one line. An escape and a plain character never match the+# same backslash, so an unclosed quote cannot backtrack exponentially.+_STRING = re.compile(r"""(?P<q>["'`])((?:\\.|(?!(?P=q))[^\\\n])*)(?P=q)""")+# A string opening a call or a list, where a command name sits; a command name+# elsewhere (``tmp_path / "repowise"``) is a path or a message.+_LEADING_STRING = re.compile(r"""[\[(]\s*(?P<q>["'`])((?:\\.|(?!(?P=q))[^\\\n])*)(?P=q)""")+# ``import a.b`` / ``from a.b import c`` inside a code string handed to ``-c``.+_CODE_IMPORT = re.compile(r"(?:^|[;\n\s])(?:from|import)\s+([A-Za-z_][\w.]*)")+_DOTTED = re.compile(r"[A-Za-z_]\w*(?:\.[A-Za-z_]\w*)+")+_WORD = r"(?<![\w$.]){}(?![\w$])"+++@dataclass(frozen=True)+class OwnCode:+ """What the repository installs: launcher names, ``bin`` targets, Python modules.++ *commands* are console scripts (``[project.scripts]``) and package ``bin``+ names; *bin_files* the files those ``bin`` entries point at; *modules* every+ dotted name a Python file can be imported under, whatever its source root+ (``a/b/c.py`` gives ``c``, ``b.c`` and ``a.b.c``).+ """++ commands: frozenset[str] = frozenset()+ bin_files: frozenset[str] = frozenset()+ modules: frozenset[str] = frozenset()+++def own_code(+ paths: Iterable[str],+ read: Callable[[str], str | None],+ console_scripts: Iterable[str] = (),+) -> OwnCode:+ """:class:`OwnCode` from the repository's production files and its manifests.++ *paths* leaves out tests: a test module run in a child process is still a+ test, not the project's own code. *console_scripts* are the launcher names+ the traverser already read from every ``pyproject.toml``+ (``FileTraverser.console_script_names``), so those files are not read+ again. *read* returns a file's text or ``None``; only ``package.json``+ files are read, so a caller holding the bytes passes them.+ """+ paths = list(paths)+ commands: set[str] = set(console_scripts)+ bins: set[str] = set()+ for path in paths:+ if PurePosixPath(path).name != "package.json" or "node_modules/" in path:+ continue+ if text := read(path):+ _add_package_bins(path, text, commands, bins)+ return OwnCode(frozenset(commands), frozenset(bins), _module_names(paths))+++def _module_names(paths: list[str]) -> frozenset[str]:+ names: set[str] = set()+ for path in paths:+ if not path.endswith(_PY):+ continue+ parts = path[: -len(_PY)].split("/")+ if parts[-1] in ("__init__", "__main__"):+ parts.pop()+ names.update(".".join(parts[i:]) for i in range(len(parts)))+ names.discard("")+ return frozenset(names)+++def _add_package_bins(path: str, text: str, commands: set[str], bins: set[str]) -> None:+ """One ``package.json``'s ``bin`` names and the repo paths they point at."""+ try:+ manifest = json.loads(text)+ except ValueError:+ return+ if not isinstance(manifest, dict):+ return+ entries = manifest.get("bin")+ if isinstance(entries, str) and isinstance(manifest.get("name"), str):+ entries = {manifest["name"].rsplit("/", 1)[-1]: entries}+ if not isinstance(entries, dict):+ return+ root = PurePosixPath(path).parent+ for command, target in entries.items():+ if isinstance(command, str) and isinstance(target, str):+ commands.add(command)+ bins.add(str(root / target.removeprefix("./")).removeprefix("./"))+++def is_judged_test(path: str) -> bool:+ """A runnable test in a language :func:`always_run_reason` reads."""+ from ..analysis.test_selection import is_runnable_test++ return path.endswith((_PY, *_JS)) and is_runnable_test(path)+++def _beside_code(path: str) -> bool:+ """Whether the test sits in a code directory, not a test directory."""+ from ..test_paths import is_test_related_path++ return not is_test_related_path(str(PurePosixPath(path).parent / "__init__.py"))+++def always_run_reason(path: str, text: str, own: OwnCode) -> str | None:+ """Why the test at *path* must run with every subset, or ``None``."""+ if path.endswith(_PY):+ lang = _PY_LANG+ elif path.endswith(_JS):+ lang = _JS_LANG+ else:+ return None+ if _walks_source(text, lang, local_is_source=_beside_code(path)):+ return "it lists and reads files under a source directory"+ if command := _runs_own_code(text, lang, own, python=path.endswith(_PY)):+ return f"it runs {command} in a child process"+ return None+++def _walks_source(text: str, lang: _Lang, *, local_is_source: bool = False) -> bool:+ if not (_has(text, lang.walk_tokens) and _has(text, lang.read_tokens)):+ return False+ if not (lang.walk.search(text) and lang.read.search(text)):+ return False+ lines = text.splitlines()+ bindings = _bindings(lines, lang)+ rooted = _Rooted(lang, bindings, local_is_source)+ scopes = _Scopes(lines, lang)+ for i, line in enumerate(lines):+ if not (_has(line, lang.walk_tokens) and (walk := lang.walk.search(line))):+ continue+ if any(m.start() < walk.start() < m.end() for m in _STRING.finditer(line)):+ continue # code quoted inside a string+ func = scopes.function(i)+ anchored = rooted.holds(_expression(line, lines, i + 1)) or bool(+ func and _called_rooted(lines, func[2], rooted, lang)+ )+ if anchored and _reads_near(lines, i, func, scopes, lang):+ return True+ return False+++def _reads_near(+ lines: list[str], at: int, func: tuple[int, int, str] | None, scopes: _Scopes, lang: _Lang+) -> bool:+ """A file read in the walk's own scope, or in a block naming its function or variable."""+ own = (func[0], func[1]) if func else scopes.block(at)+ if _reads_in(lines, own, scopes, lang):+ return True+ names = {func[2]} if func else set()+ if match := lang.assign.match(lines[at]):+ names.add(match.group(1))+ if not names:+ return False+ mention = _words(names)+ blocks = {scopes.block(i) for i, line in enumerate(lines) if i != at and mention.search(line)}+ return any(_reads_in(lines, block, scopes, lang) for block in blocks)+++def _reads_in(lines: list[str], span: tuple[int, int], scopes: _Scopes, lang: _Lang) -> bool:+ """A read in *span*, or in a local function *span* calls (one step)."""+ text = "\n".join(lines[span[0] : span[1]])+ if lang.read.search(text):+ return True+ for name in set(_IDENT.findall(text)):+ body = scopes.body(name)+ if body and body != span and lang.read.search("\n".join(lines[body[0] : body[1]])):+ return True+ return False+++class _Scopes:+ """Function spans and top-level blocks, by indentation."""++ def __init__(self, lines: list[str], lang: _Lang) -> None:+ self._funcs: list[tuple[int, int, str]] = []+ for i, line in enumerate(lines):+ if match := lang.func.match(line):+ name = next(g for g in match.groups() if g)+ self._funcs.append((i, _span_end(lines, i), name))+ self._block_of: list[int] = []+ self._starts: list[int] = []+ decorated = False # a decorator and the definition under it are one block+ for i, line in enumerate(lines):+ top = line[:1] not in ("", " ", "\t", ")", "]", "}")+ if (top and not decorated) or not self._starts:+ self._starts.append(i)+ if top:+ decorated = line.startswith("@")+ self._block_of.append(len(self._starts) - 1)+ self._starts.append(len(lines))++ def body(self, name: str) -> tuple[int, int] | None:+ return next(((a, b) for a, b, n in self._funcs if n == name), None)++ def function(self, at: int) -> tuple[int, int, str] | None:+ """The innermost function whose span holds line *at*."""+ for k in range(len(self._funcs) - 1, -1, -1):+ start, end, name = self._funcs[k]+ if start <= at < end:+ return start, end, name+ return None++ def block(self, at: int) -> tuple[int, int]:+ b = self._block_of[at]+ return self._starts[b], self._starts[b + 1]+++def _span_end(lines: list[str], start: int) -> int:+ """The line after a definition's body: the next one indented no deeper than it."""+ indent = _indent(lines[start])+ for j in range(start + 1, len(lines)):+ stripped = lines[j].strip()+ if stripped and _indent(lines[j]) <= indent:+ # A closing bracket at the definition's own indent still belongs to it.+ return j + 1 if stripped[0] in ")]}" else j+ return len(lines)+++def _indent(line: str) -> int:+ return len(line) - len(line.lstrip())+++def _has(text: str, tokens: tuple[str, ...]) -> bool:+ return any(token in text for token in tokens)+++# Assignments kept per name: the last few, so a name rebound a thousand times+# in one file costs a fixed amount per lookup.+_BINDINGS_PER_NAME = 3+++def _bindings(lines: list[str], lang: _Lang) -> dict[str, str]:+ """``{name: the last expressions it is assigned or looped from}``."""+ out: dict[str, list[str]] = {}+ for i, line in enumerate(lines):+ match = lang.assign.match(line) or lang.loop.match(line)+ if match and not _MODULE_LOAD.search(line):+ kept = out.setdefault(match.group(1), [])+ kept.append(_expression(match.group(2), lines, i + 1))+ del kept[:-_BINDINGS_PER_NAME]+ return {name: " ".join(exprs) for name, exprs in out.items()}+++def _expression(head: str, lines: list[str], nxt: int, cap: int = 6) -> str:+ """*head* plus the lines that close its brackets or continue it (``? a : b``)."""+ parts = [head]+ depth = _depth(head)+ while (+ nxt < len(lines)+ and len(parts) <= cap+ and (depth > 0 or lines[nxt].lstrip().startswith(_CONTINUATION))+ ):+ parts.append(lines[nxt])+ depth += _depth(lines[nxt])+ nxt += 1+ return " ".join(parts)+++_CONTINUATION = ("?", ":", ".", "|", "&", "+")+++def _depth(text: str) -> int:+ return sum(text.count(c) for c in "([{") - sum(text.count(c) for c in ")]}")+++class _Rooted:+ """Names bound to the source tree, and names bound to the test's own directory.++ Both are found once, to a fixed point. A name of the second kind counts as+ the source tree only where the expression climbs out of it, or when the+ test sits beside the code (*local_is_source*).+ """++ def __init__(self, lang: _Lang, bindings: dict[str, str], local_is_source: bool) -> None:+ self._lang = lang+ self._local_is_source = local_is_source+ self._names: re.Pattern[str] | None = None+ self._locals: re.Pattern[str] | None = None+ rooted: set[str] = set()+ local: set[str] = set()+ pending = {n: e for n, e in bindings.items() if not _LOCAL_ROOT.search(e)}+ while True:+ fresh = {n for n, e in pending.items() if self.holds(e)}+ fresh_local = {n for n, e in pending.items() if n not in fresh and self._is_local(e)}+ if not (fresh or fresh_local):+ break+ rooted |= fresh+ local |= fresh_local+ pending = {n: e for n, e in pending.items() if n not in fresh | fresh_local}+ self._names = _words(rooted) if rooted else None+ self._locals = _words(local) if local else None++ def holds(self, expr: str) -> bool:+ if _LOCAL_ROOT.search(expr):+ return False+ lang = self._lang+ if lang.anchor.search(expr) or (self._names and self._names.search(expr)):+ return True+ if lang.local.search(expr) and (self._local_is_source or lang.climb_file.search(expr)):+ return True+ return bool(+ self._locals+ and self._locals.search(expr)+ and (self._local_is_source or lang.climb_dir.search(expr))+ )++ def _is_local(self, expr: str) -> bool:+ return bool(self._lang.local.search(expr) or (self._locals and self._locals.search(expr)))+++def _words(names: Iterable[str]) -> re.Pattern[str]:+ alternation = "|".join(sorted(map(re.escape, names), key=len, reverse=True))+ return re.compile(_WORD.format(f"(?:{alternation})"))+++def _called_rooted(lines: list[str], func: str, rooted: _Rooted, lang: _Lang) -> bool:+ call = re.compile(_WORD.format(re.escape(func)) + r"\s*\(")+ return any(+ call.search(line) and not lang.func.match(line) and rooted.holds(_expression(line, lines, i + 1))+ for i, line in enumerate(lines)+ )+++def _runs_own_code(text: str, lang: _Lang, own: OwnCode, *, python: bool) -> str | None:+ """The own command, module or file a child process in *text* is started with."""+ if not _has(text, lang.spawn_tokens if python else _JS_PROCESS_TOKENS):+ return None+ lines: list[str] | None = None+ seen: set[str] = set()+ for match in lang.spawn.finditer(text):+ if lines is None:+ lines = text.splitlines()+ known = {**_function_bodies(lines, lang), **_bindings(lines, lang)}+ call = _call_region(text, match.end())+ if call in seen: # ``run(cmd)`` a thousand times reads ``cmd`` once+ continue+ seen.add(call)+ if hit := _own_reference(_expand(call, known), own, python=python):+ return hit+ return None+++# Most text one spawn call is expanded to: its arguments, then the names and+# local helpers they mention, two steps back.+_REGION_CAP = 4096+++def _expand(region: str, known: Mapping[str, str]) -> str:+ """*region* plus what its names are bound to: ``run(cmd)`` with ``cmd = [...]``."""+ expanded: set[str] = set()+ for _ in range(2):+ refs = (set(_IDENT.findall(region)) & known.keys()) - expanded+ expanded |= refs+ region = " ".join([region, *(known[r] for r in sorted(refs))])[:_REGION_CAP]+ return region+++def _call_region(text: str, start: int, cap: int = 600) -> str:+ """The text of a call's arguments: up to its closing bracket, at most *cap* characters."""+ depth = 0+ end = min(len(text), start + cap)+ for i in range(start, end):+ ch = text[i]+ if ch in "([{":+ depth += 1+ elif ch in ")]}":+ depth -= 1+ if depth <= 0:+ return text[start : i + 1]+ return text[start:end]+++def _function_bodies(lines: list[str], lang: _Lang, cap: int = 30) -> dict[str, str]:+ out: dict[str, str] = {}+ for i, line in enumerate(lines):+ if match := lang.func.match(line):+ out[next(g for g in match.groups() if g)] = " ".join(lines[i : i + cap])+ return out+++def _own_reference(region: str, own: OwnCode, *, python: bool) -> str | None:+ """The first own command, ``bin`` file or module the strings in *region* name."""+ if own.commands:+ for match in _LEADING_STRING.finditer(region):+ if (head := match.group(2).strip().split(" ", 1)[0]) in own.commands:+ return f"the project's own command `{head}`"+ values = [m.group(2).strip() for m in _STRING.finditer(region)]+ dash_m, dash_c = "-m" in values, "-c" in values+ for value in values:+ head = value.split(" ", 1)[0]+ if not python:+ if (bin_file := _bin_file(head, own)) is not None:+ return f"the project's own bin `{bin_file}`"+ continue+ if dash_m and _DOTTED.fullmatch(value) and value in own.modules:+ return f"`python -m {value}`"+ if dash_c:+ for module in _CODE_IMPORT.findall(value):+ if "." in module and module in own.modules:+ return f"code importing `{module}`"+ return None+++def _bin_file(value: str, own: OwnCode) -> str | None:+ """The ``bin`` file *value* names by its repo path, or by a path of two or more parts.++ A bare file name only matches a ``bin`` at the repository root: ``cli.js``+ is not every package's ``bin/cli.js``.+ """+ value = value.removeprefix("./")+ if not value:+ return None+ return next(+ (b for b in own.bin_files if b == value or ("/" in value and b.endswith(f"/{value}"))),+ None,+ )+++def stamp_always_run(+ graph: Any,+ parsed_files: Mapping[str, Any],+ source_map: dict[str, bytes] | None,+ console_scripts: Iterable[str] = (),+) -> int:+ """Set ``always_run_reason`` on each judged test's graph node; returns how many have one.++ A test read and found ordinary gets ``""``; ``None`` (never stamped) is+ left for a test that could not be read, which selection treats as not yet+ scanned. Reads the bytes the pipeline already holds (*source_map*),+ falling back to disk only for a file the map lacks, and only for test+ files and manifests.+ """+ from .source_text import source_text++ def read(path: str) -> str | None:+ info = getattr(parsed_files.get(path), "file_info", None)+ return source_text(path, getattr(info, "abs_path", path), source_map)++ tests = [+ p+ for p, parsed in parsed_files.items()+ if parsed.file_info.is_test and is_judged_test(p)+ ]+ if not tests:+ return 0+ production = [p for p, pf in parsed_files.items() if not pf.file_info.is_test]+ own = own_code(production, read, console_scripts)+ stamped = 0+ for path in tests:+ node = graph.nodes.get(path)+ if node is None or (text := read(path)) is None:+ continue+ node["always_run_reason"] = always_run_reason(path, text, own) or ""+ stamped += bool(node["always_run_reason"])+ return stampeddiff --git a/packages/core/src/repowise/core/ingestion/graph/_rehydrate.py b/packages/core/src/repowise/core/ingestion/graph/_rehydrate.pyindex 7ed2a9758..09360d775 100644--- a/packages/core/src/repowise/core/ingestion/graph/_rehydrate.py+++ b/packages/core/src/repowise/core/ingestion/graph/_rehydrate.py@@ -37,6 +37,7 @@ _NODE_ATTR_KEYS = ("is_test","is_entry_point","is_reachability_root",+ "always_run_reason","kind","name","qualified_name",diff --git a/packages/core/src/repowise/core/ingestion/graph/builder.py b/packages/core/src/repowise/core/ingestion/graph/builder.pyindex 2883a695a..6ac47fbbc 100644--- a/packages/core/src/repowise/core/ingestion/graph/builder.py+++ b/packages/core/src/repowise/core/ingestion/graph/builder.py@@ -65,6 +65,9 @@ class GraphBuilder(MetricsMixin, ResolveMixin, EdgesMixin, SerializeMixin, Rehyd# warmups that scan file text read it instead of re-opening every# file. Empty means "not supplied": every reader falls back to disk.self._source_map: dict[str, bytes] = {}+ # Launcher names the traverser read from pyproject.toml; set by the+ # pipelines that walk the repo, empty for a builder fed files directly.+ self.console_scripts: frozenset[str] = frozenset()self._built = False# Resolver-built DotNetProjectIndex, stashed by build() for the# dynamic-hints phase to reuse (see build()).@@ -406,6 +409,17 @@ class GraphBuilder(MetricsMixin, ResolveMixin, EdgesMixin, SerializeMixin, Rehydrun_warmups(self._parsed_files, ctx, progress=progress)+ # Tests the import graph cannot see into, from the bytes already held,+ # so selection reads a stored fact and never re-reads test files.+ from ..always_run import stamp_always_run++ try:+ stamp_always_run(+ self._graph, self._parsed_files, self._source_map, self.console_scripts+ )+ except Exception as exc: # a detector bug must not fail the index+ log.warning("always_run_detection_failed", error=str(exc))+# --- Phase 1: Resolve file-level imports ---import_targets: dict[str, set[str]] = {} # file → set of imported filesdiff --git a/packages/core/src/repowise/core/ingestion/traverser.py b/packages/core/src/repowise/core/ingestion/traverser.pyindex 4da78fb8d..02866316d 100644--- a/packages/core/src/repowise/core/ingestion/traverser.py+++ b/packages/core/src/repowise/core/ingestion/traverser.py@@ -450,6 +450,11 @@ class FileTraverser:def _console_script_names(self) -> frozenset[str]:return self._console_script_tables().names+ @property+ def console_script_names(self) -> frozenset[str]:+ """Launcher names the repo's ``pyproject.toml`` files declare, read once per walk."""+ return self._console_script_tables().names+@propertydef _console_script_modules(self) -> frozenset[str]:return self._console_script_tables().modulesdiff --git a/packages/core/src/repowise/core/persistence/crud/graph.py b/packages/core/src/repowise/core/persistence/crud/graph.pyindex b79e5345d..f37a67476 100644--- a/packages/core/src/repowise/core/persistence/crud/graph.py+++ b/packages/core/src/repowise/core/persistence/crud/graph.py@@ -60,6 +60,7 @@ _NODE_FIELDS = ("is_test","is_entry_point","is_reachability_root",+ "always_run_reason","pagerank","betweenness","betweenness_commit",@@ -484,6 +485,7 @@ async def get_all_graph_nodes("is_test": row.is_test,"is_entry_point": row.is_entry_point,"is_reachability_root": row.is_reachability_root,+ "always_run_reason": row.always_run_reason,"kind": row.kind,"name": row.name,"qualified_name": row.qualified_name,diff --git a/packages/core/src/repowise/core/persistence/models.py b/packages/core/src/repowise/core/persistence/models.pyindex d9d38e0fa..9b8e0fde5 100644--- a/packages/core/src/repowise/core/persistence/models.py+++ b/packages/core/src/repowise/core/persistence/models.py@@ -290,6 +290,10 @@ class GraphNode(Base):is_entry_point: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)# Dead-code exempt: reached from outside the graph. Every entry point is one.is_reachability_root: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=false())+ # Why a test file runs with every selected subset (it walks the source tree+ # or runs the project in a child process). "" on a test checked and found+ # ordinary; NULL on a test never checked (an older index) and on non-tests.+ always_run_reason: Mapped[str | None] = mapped_column(Text, nullable=True)pagerank: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)betweenness: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)# The commit ``betweenness`` was last actually computed at. Betweenness isdiff --git a/packages/core/src/repowise/core/pipeline/incremental.py b/packages/core/src/repowise/core/pipeline/incremental.pyindex b769da00d..15fc58911 100644--- a/packages/core/src/repowise/core/pipeline/incremental.py+++ b/packages/core/src/repowise/core/pipeline/incremental.py@@ -136,6 +136,7 @@ def build_repo_graph(include_submodules=include_submodules,include_nested_repos=include_nested_repos,)+ graph_builder.console_scripts = traverser.console_script_names# Carry the walk's skip record to the dead-code analyzer. This report is# persisted repo-wide, so an update that could not see the skipped source# files would re-derive and write back the verdicts init had clamped.diff --git a/packages/core/src/repowise/core/pipeline/persist.py b/packages/core/src/repowise/core/pipeline/persist.pyindex 15a4dec62..b81481419 100644--- a/packages/core/src/repowise/core/pipeline/persist.py+++ b/packages/core/src/repowise/core/pipeline/persist.py@@ -459,6 +459,7 @@ async def persist_graph_nodes("is_test": data.get("is_test", False),"is_entry_point": data.get("is_entry_point", False),"is_reachability_root": is_reachability_root(data),+ "always_run_reason": data.get("always_run_reason"),# Files draw from the file-level metric tables; symbols fall# back to the symbol subgraph (calls + heritage) so that the# per-symbol UI panel shows real centrality instead of 0.diff --git a/packages/core/src/repowise/core/pipeline/phases/ingestion.py b/packages/core/src/repowise/core/pipeline/phases/ingestion.pyindex 850cef5c7..190838257 100644--- a/packages/core/src/repowise/core/pipeline/phases/ingestion.py+++ b/packages/core/src/repowise/core/pipeline/phases/ingestion.py@@ -520,6 +520,7 @@ async def _run_ingestion(include_submodules=include_submodules,include_nested_repos=include_nested_repos,)+ graph_builder.console_scripts = traverser.console_script_namesloop = asyncio.get_running_loop()diff --git a/tests/unit/analysis/test_test_selection.py b/tests/unit/analysis/test_test_selection.pyindex 60048494a..966baf51c 100644--- a/tests/unit/analysis/test_test_selection.py+++ b/tests/unit/analysis/test_test_selection.py@@ -774,6 +774,50 @@ def test_a_test_the_graph_cannot_see_into_runs_with_every_subset() -> None:assert _select(["docs/guide.md"], unplaced_tests=["tests/test_cli.py"]).tests == ()+def test_a_test_found_walking_the_tree_runs_with_every_subset_and_says_why() -> None:+ tiers = _tiers(inferred=[_inferred("src/a.py", "tests/test_a.py", "import-graph")])+ lint = {"tests/test_no_copies.py": "it lists and reads files under a source directory"}+ sel = _select(+ ["src/a.py"],+ tiers,+ always_run_tests={**lint, "tests/test_gone.py": "it runs `tool` in a child process"},+ known_tests=["tests/test_a.py", "tests/test_no_copies.py"],+ )+ assert not sel.run_all+ # A detected test the checkout no longer has is left out.+ assert sel.tests == ("tests/test_a.py", "tests/test_no_copies.py")+ assert (+ "1 test file(s) that list source files or run the project in a child process run "+ "with every selection (e.g. tests/test_no_copies.py: it lists and reads files under "+ "a source directory)." in sel.reasons+ )+ assert sel.why == {+ "tests/test_a.py": "src/a.py changed (import-graph)",+ "tests/test_no_copies.py": (+ "runs with every subset: it lists and reads files under a source directory"+ ),+ }+++def test_explain_says_what_selected_a_test_or_that_nothing_did() -> None:+ from repowise.core.analysis.test_selection import explain_test++ tiers = _tiers(inferred=[_inferred("src/a.py", "tests/test_a.py", "call-graph")])+ sel = _select(["src/a.py"], tiers, config=TestSelectionConfig(always_run=("tests/smoke",)))+ assert explain_test(sel, "tests/test_a.py::test_one") == (+ True,+ ["Selected: src/a.py changed (call-graph)."],+ )+ assert explain_test(sel, "tests/smoke")[0] is True+ selected, lines = explain_test(sel, "tests/test_b.py")+ assert not selected and lines[0].startswith("Not selected: no changed file reaches it")+ full = _select(["uv.lock"])+ assert explain_test(full, "tests/test_b.py") == (+ True,+ ["Every test runs:", "uv.lock changed: dependencies can change any test."],+ )++def test_a_test_named_module_pytest_does_not_collect_is_not_runnable() -> None:"""``core/test_paths.py`` is test-shaped, but a bare pytest never collects it."""config = '[tool.pytest.ini_options]\ntestpaths = ["tests"]\n'diff --git a/tests/unit/cli/test_impacted_tests_cmd.py b/tests/unit/cli/test_impacted_tests_cmd.pyindex 6750df027..6df2d6fab 100644--- a/tests/unit/cli/test_impacted_tests_cmd.py+++ b/tests/unit/cli/test_impacted_tests_cmd.py@@ -42,7 +42,15 @@ async def _index(root: Path, head_commit: str) -> None:async with async_sessionmaker(engine, expire_on_commit=False)() as session:session.add(Repository(id="r1", name="r", local_path=local_path, head_commit=head_commit))for path, is_test in (("src/a.py", False), ("src/b.py", False), ("tests/test_a.py", True)):- node = GraphNode(repository_id="r1", node_id=path, node_type="file", is_test=is_test)+ # A test the indexer checked and found ordinary carries "".+ reason = "" if is_test else None+ node = GraphNode(+ repository_id="r1",+ node_id=path,+ node_type="file",+ is_test=is_test,+ always_run_reason=reason,+ )session.add(node)session.add(GraphEdge(@@ -303,7 +311,15 @@ async def _add_to_index(root: Path, test: str, source: str) -> None:db = root / ".repowise" / "wiki.db"engine = create_async_engine(f"sqlite+aiosqlite:///{db.as_posix()}")async with async_sessionmaker(engine, expire_on_commit=False)() as session:- session.add(GraphNode(repository_id="r1", node_id=test, node_type="file", is_test=True))+ session.add(+ GraphNode(+ repository_id="r1",+ node_id=test,+ node_type="file",+ is_test=True,+ always_run_reason="",+ )+ )session.add(GraphEdge(repository_id="r1", source_node_id=test, target_node_id=source, edge_type="imports"@@ -330,3 +346,88 @@ def test_only_files_whose_imports_moved_since_the_index_add_their_tests(result = _run(repo, "main...feat", "--format", "args", "--runner", "pytest")assert result.exit_code == 0, result.outputassert result.stdout == expected+++def _add_test(repo, path: str, imports: str, reason: str | None = "") -> None:+ """Land test *path* on main, index it there importing *imports*, rebase the change."""+ from sqlalchemy import update++ from repowise.core.persistence.models import GraphEdge, GraphNode, Repository++ _git(repo, "switch", "-q", "main")+ _write(repo, {path: "def test_x():\n pass\n"})+ _git(repo, "add", "-A")+ _git(repo, "commit", "-qm", f"add {path}")+ head = _git(repo, "rev-parse", "HEAD")+ _git(repo, "switch", "-q", "feat")+ _git(repo, "rebase", "-q", "main")++ async def add() -> None:+ db = (repo / ".repowise" / "wiki.db").as_posix()+ engine = create_async_engine(f"sqlite+aiosqlite:///{db}")+ async with async_sessionmaker(engine, expire_on_commit=False)() as session:+ node = GraphNode(+ repository_id="r1",+ node_id=path,+ node_type="file",+ is_test=True,+ always_run_reason=reason,+ )+ edge = GraphEdge(+ repository_id="r1", source_node_id=path, target_node_id=imports, edge_type="imports"+ )+ session.add_all([node, edge])+ await session.execute(update(Repository).values(head_commit=head))+ await session.commit()+ await engine.dispose()++ asyncio.run(add())+++def test_a_test_the_indexer_found_walking_the_tree_runs_with_every_subset(repo) -> None:+ walks = "it lists and reads files under a source directory"+ _add_test(repo, "tests/test_lint.py", "src/b.py", walks)+ _add_test(repo, "tests/test_b.py", "src/b.py")+ result = _run(repo, "main...feat", "--format", "args", "--runner", "pytest")+ assert result.exit_code == 0, result.output+ # src/a.py changed: tests/test_b.py stays out, the tree walker comes in.+ assert result.stdout == "tests/test_a.py tests/test_lint.py\n"+ assert (+ "1 test file(s) that list source files or run the project in a child process run with "+ "every selection (e.g. tests/test_lint.py: it lists and reads files under a source "+ "directory)." in _err(result)+ )+++def test_an_index_that_never_checked_its_tests_runs_everything(repo) -> None:+ _add_test(repo, "tests/test_b.py", "src/b.py", reason=None)+ result = _run(repo, "main...feat", "--format", "args")+ assert result.stdout == ":all\n"+ assert (+ "The index has not checked 1 test file(s) for walking the source tree or running "+ "the project (e.g. tests/test_b.py); run `repowise update`." in _err(result)+ )+++def test_explain_names_the_changed_file_and_the_route(repo) -> None:+ _add_test(repo, "tests/test_b.py", "src/b.py")+ result = _run(repo, "main...feat", "--explain", "tests/test_a.py")+ assert result.exit_code == 0, result.output+ assert result.stdout.splitlines() == [+ "Selected: src/a.py changed (import-graph).",+ "Route: tests/test_a.py -> src/a.py",+ ]+ missed = _run(repo, "main...feat", "--explain", "./tests/test_b.py")+ assert missed.stdout.startswith("Not selected: no changed file reaches it")+++def test_explain_joins_the_json_report(repo) -> None:+ result = _run(repo, "main...feat", "--format", "json", "--explain", "tests/test_a.py")+ data = json.loads(result.stdout)+ assert data["explain"] == {+ "test": "tests/test_a.py",+ "selected": True,+ "lines": ["Selected: src/a.py changed (import-graph).", "Route: tests/test_a.py -> src/a.py"],+ "route": ["tests/test_a.py", "src/a.py"],+ }+ assert data["selected"]["why"] == {"tests/test_a.py": "src/a.py changed (import-graph)"}diff --git a/tests/unit/ingestion/test_always_run.py b/tests/unit/ingestion/test_always_run.pynew file mode 100644index 000000000..2d4534496--- /dev/null+++ b/tests/unit/ingestion/test_always_run.py@@ -0,0 +1,300 @@+"""Tests the graph cannot see into: tree walks and self-runs, found from source text."""++from __future__ import annotations++import time++import pytest++from repowise.core.ingestion.always_run import (+ OwnCode,+ always_run_reason,+ own_code,+ stamp_always_run,+)++_OWN = OwnCode(+ commands=frozenset({"tool", "tool-hook"}),+ bin_files=frozenset({"bin/tool.mjs"}),+ modules=frozenset({"tool", "tool.cli", "tool.cli.main", "cli.main", "main"}),+)++_WALK = "it lists and reads files under a source directory"+++def _reason(path: str, text: str) -> str | None:+ return always_run_reason(path, text, _OWN)+++ "text",+ [+ # A lint test: anchored root, rglob, then a parse of each file.+ "import ast, pathlib\n"+ "_PACKAGES = pathlib.Path(__file__).resolve().parents[2] / 'packages'\n"+ "def test_no_copies():\n"+ " for p in _PACKAGES.rglob('*.py'):\n"+ " ast.parse(p.read_text())\n",+ # The root comes through a module's ``__file__`` and a loop variable.+ "from pathlib import Path\nimport tool\n"+ "ROOT = Path(tool.__file__).parent\n"+ "SCAN = [ROOT / 'a', ROOT / 'b']\n"+ "def test_x():\n"+ " for entry in SCAN:\n"+ " for f in sorted(entry.glob('*.j2')):\n"+ " assert 'x' not in f.read_text()\n",+ # ``os.walk`` from the working directory.+ "import os\n"+ "def test_x():\n"+ " for d, _, names in os.walk(os.getcwd()):\n"+ " open(os.path.join(d, names[0]))\n",+ ],+)+def test_a_python_test_walking_the_source_tree_is_detected(text) -> None:+ assert _reason("tests/test_lint.py", text) == _WALK+++ "text",+ [+ # Its own fixtures directory.+ "from pathlib import Path\n"+ "FIXTURES = Path(__file__).parent / 'fixtures'\n"+ "def test_x():\n"+ " for p in FIXTURES.glob('*.json'):\n"+ " p.read_text()\n",+ # A temporary directory it filled itself.+ "def test_x(tmp_path):\n"+ " for p in tmp_path.iterdir():\n"+ " p.read_text()\n",+ # Lists the tree but never reads a file.+ "from pathlib import Path\n"+ "ROOT = Path(__file__).parent\n"+ "def test_x():\n"+ " assert list(ROOT.glob('*.py'))\n",+ ],+)+def test_a_python_test_walking_its_own_material_is_not(text) -> None:+ assert _reason("tests/test_x.py", text) is None+++ "text",+ [+ # Its own directory: sibling case files, not the source tree.+ "from pathlib import Path\n"+ "HERE = Path(__file__).parent\n"+ "def test_cases():\n"+ " for p in HERE.glob('case_*.txt'):\n"+ " p.read_text()\n",+ # ``git ls-files`` in a repository the test built in a temp dir.+ "import subprocess\n"+ "def test_x(tmp_path):\n"+ " out = subprocess.run(['git', 'ls-files'], cwd=tmp_path).stdout\n"+ " open(tmp_path / out.split()[0])\n",+ # The walk is anchored, but the only read is in an unrelated test.+ "from pathlib import Path\n"+ "ROOT = Path(__file__).parents[2]\n"+ "def test_count():\n"+ " assert len(list(ROOT.rglob('*.py'))) > 10\n"+ "\n"+ "def test_other(tmp_path):\n"+ " (tmp_path / 'a').write_text('x')\n"+ " assert open(tmp_path / 'a').read() == 'x'\n",+ ],+)+def test_walks_that_are_not_the_source_tree_or_read_nothing_from_it(text) -> None:+ assert _reason("tests/unit/test_x.py", text) is None+++def test_a_test_beside_the_code_may_walk_its_own_directory() -> None:+ text = (+ "import { readdirSync, readFileSync } from 'node:fs';\n"+ "const dir = new URL('./', import.meta.url);\n"+ "it('guards', () => {\n"+ " for (const f of readdirSync(dir)) readFileSync(new URL(f, dir), 'utf8');\n"+ "});\n"+ )+ assert _reason("src/tools/boundary.test.ts", text) == _WALK+ assert _reason("test/tools/boundary.test.ts", text) is None+++def test_a_large_cli_test_file_is_judged_fast() -> None:+ lines = ["import subprocess"]+ lines += [f"cmd = ['git', 'log', '-{i}']" for i in range(2000)]+ lines += ["def test_x():"] + [f" subprocess.run(cmd, cwd='d{i}')" for i in range(1000)]+ start = time.perf_counter()+ assert _reason("tests/test_cli_big.py", "\n".join(lines)) is None+ assert time.perf_counter() - start < 2+++def test_a_walk_inside_a_helper_counts_when_the_helper_is_given_a_source_root() -> None:+ text = (+ "import fs from 'node:fs';\nimport path from 'node:path';\n"+ "async function collect(dir) {\n"+ " const entries = await fs.promises.readdir(dir, { withFileTypes: true });\n"+ " return entries.map((e) => path.join(dir, e.name));\n"+ "}\n"+ "it('guards callsites', async () => {\n"+ " const root = process.cwd();\n"+ " const files = await collect(path.join(root, 'src'));\n"+ " for (const f of files) fs.readFileSync(f, 'utf8');\n"+ "});\n"+ )+ assert _reason("src/gateway/callsites.guard.test.ts", text) == _WALK+++def test_a_typescript_test_listing_a_temp_dir_is_not_a_walk() -> None:+ text = (+ "import fs from 'node:fs/promises';\nimport os from 'node:os';\n"+ "const here = new URL('.', import.meta.url);\n"+ "it('cleans up', async () => {\n"+ " const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'x-'));\n"+ " expect(await fs.readdir(dir)).toEqual([]);\n"+ " await fs.readFile(path.join(dir, 'a'));\n"+ "});\n"+ )+ assert _reason("src/media/store.test.ts", text) is None+++ ("text", "expected"),+ [+ (+ "import subprocess, sys\n"+ "def test_x():\n"+ " subprocess.run([sys.executable, '-m', 'tool.cli.main', 'status'])\n",+ "it runs `python -m tool.cli.main` in a child process",+ ),+ (+ "import subprocess\n"+ "def test_x():\n"+ " subprocess.run(['tool', 'init'], check=True)\n",+ "it runs the project's own command `tool` in a child process",+ ),+ ],+)+def test_a_python_test_running_the_project_in_a_child_process_is_detected(text, expected) -> None:+ assert _reason("tests/test_cli_smoke.py", text) == expected+++def test_helper_built_code_strings_name_the_module_they_import() -> None:+ text = (+ "import subprocess, sys\n"+ "def _cmd():\n"+ " return [sys.executable, '-c', 'from tool.cli import main; main()']\n"+ "def test_x():\n"+ " cmd = _cmd()\n"+ " subprocess.run(cmd, input='{}')\n"+ )+ assert _reason("tests/test_hook.py", text) == "it runs code importing `tool.cli` in a child process"+++ "text",+ [+ # Runs git; the project's name only appears as a directory.+ "import subprocess\n"+ "def test_x(tmp_path):\n"+ " repo = tmp_path / 'tool'\n"+ " subprocess.run(['git', 'init'], cwd=repo)\n",+ # A monkeypatch target string is not a ``-m`` module.+ "import subprocess\n"+ "def test_x(monkeypatch):\n"+ " monkeypatch.setattr('tool.cli.main', None)\n"+ " subprocess.run(['git', 'status'])\n",+ # Mentions the command in a message, never runs it.+ "import subprocess\n"+ "def test_x():\n"+ " r = subprocess.run(['git', 'log'])\n"+ " assert 'tool init' in r.stdout\n",+ ],+)+def test_a_child_process_that_does_not_run_the_project_is_not_detected(text) -> None:+ assert _reason("tests/test_git.py", text) is None+++def test_a_typescript_test_spawning_the_package_bin_is_detected() -> None:+ text = (+ "import { spawnSync } from 'node:child_process';\n"+ "const args = process.env.X\n"+ " ? ['dist/index.js']\n"+ " : ['bin/tool.mjs', '--help'];\n"+ "it('runs', () => { spawnSync(process.execPath, args); });\n"+ )+ assert _reason("test/launcher.e2e.test.ts", text) == (+ "it runs the project's own bin `bin/tool.mjs` in a child process"+ )+++def test_a_bare_file_name_is_not_another_packages_bin() -> None:+ text = (+ "import { spawnSync } from 'node:child_process';\n"+ "it('runs', () => { spawnSync('node', ['tool.mjs']); });\n"+ )+ assert _reason("pkg/a.test.ts", text) is None+++def test_a_regex_exec_is_not_a_child_process() -> None:+ text = "it('x', () => { const m = /tool (\\d+)/.exec('tool 1'); expect(m).toBeTruthy(); });\n"+ assert _reason("src/a.test.ts", text) is None+++def test_other_languages_are_not_judged() -> None:+ assert _reason("pkg/a_test.go", 'exec.Command("tool")') is None+++def test_an_unclosed_string_full_of_backslashes_is_fast() -> None:+ text = "import subprocess\nsubprocess.run(['x', '" + "\\\\" * 5000 + "\n"+ start = time.perf_counter()+ assert _reason("tests/test_win.py", text) is None+ assert time.perf_counter() - start < 1+++def test_own_code_takes_console_scripts_and_reads_bins_and_production_modules() -> None:+ texts = {+ "web/package.json": '{"name": "@acme/web", "bin": {"web-tool": "./bin/run.mjs"}}',+ "cli/package.json": '{"name": "@acme/cli", "bin": "cli.js"}',+ }+ paths = [*texts, "pyproject.toml", "src/tool/cli/main.py", "src/tool/__init__.py"]+ # pyproject.toml is never read: its launchers come from the traverser.+ own = own_code(paths, texts.__getitem__, ["tool"])+ assert own.commands == {"tool", "web-tool", "cli"}+ assert own.bin_files == {"web/bin/run.mjs", "cli/cli.js"}+ assert own.modules == {"src.tool.cli.main", "tool.cli.main", "cli.main", "main", "src.tool", "tool"}+++def test_stamp_sets_the_reason_on_test_nodes_only() -> None:+ import networkx as nx++ from repowise.core.ingestion.models import FileInfo, ParsedFile++ lint = (+ "import pathlib\nROOT = pathlib.Path(__file__).parents[1]\n"+ "def test_x():\n [p.read_text() for p in ROOT.rglob('*.py')]\n"+ )+ sources = {"tests/test_lint.py": lint, "tests/test_a.py": "def test_a():\n pass\n"}+ sources["src/walker.py"] = lint+ graph = nx.DiGraph()+ parsed = {}+ for path, text in sources.items():+ graph.add_node(path, node_type="file")+ info = FileInfo(+ path=path,+ abs_path=path,+ language="python",+ size_bytes=len(text),+ git_hash="",+ last_modified=None,+ is_test=path.startswith("tests/"),+ is_config=False,+ is_api_contract=False,+ is_entry_point=False,+ )+ parsed[path] = ParsedFile(file_info=info, symbols=[], imports=[], exports=[])+ stamped = stamp_always_run(graph, parsed, {p: t.encode() for p, t in sources.items()})+ assert stamped == 1+ assert graph.nodes["tests/test_lint.py"]["always_run_reason"] == _WALK+ # Scanned and ordinary is "", apart from never scanned (None).+ assert graph.nodes["tests/test_a.py"]["always_run_reason"] == ""+ assert "always_run_reason" not in graph.nodes["src/walker.py"]