Repository navigation
Description
Problem
make test currently runs the full unit-test suite across tests/, gateway/tests/, orchestrator/tests/, and shared/tests/ regardless of what changed. For small/targeted changes this is slow and wastes CI time, which also slows the inner feedback loop for agents iterating in the SDLC pipeline.
Goals
- Correctness first: run every test that could be affected by the diff. Never skip a test that exercises a changed code path.
- Minimal waste: avoid running tests that provably cannot be affected.
- Keep the full-suite behavior available (e.g.
make test-all) so CI/release flows are unaffected.
Proposal
Not set in stone. The design below is a starting proposal — the refine phase should do its own exploration and push back on any of it. Specifically worth re-evaluating:
- Selection mechanism: static reverse import graph (below) vs coverage-based (
pytest-testmon) vs a hybrid. Each has a different correctness/cost profile — the right answer isn't obvious.- Existing tooling:
grimp,pydeps,importlab, ormodulegraphmay already solve the graph-building step; no reason to hand-roll if one fits.- LKG storage: tracked file (current proposal) vs git ref vs git notes vs a non-tracked sidecar. Each has different merge/rebase/parallel-branch semantics.
- LKG update mechanism: auto-commit vs amend vs sidecar file (avoids touching history entirely).
- Merge behavior: rewrite to main HEAD (current proposal) vs strip on merge vs leave branch-local values alone.
- Dynamic import handling: the grep-based widening is a blunt instrument; consider whether a runtime/import-hook approach during a single seed test run would be more accurate.
The acceptance criteria and goals (correctness-first, minimal waste, full-suite escape hatch, per-branch isolation) are the load-bearing constraints; everything else is negotiable.
Use a reverse import-graph built from static analysis of the repo, rather than directory-based path mapping. The graph is cheap to rebuild per invocation and doesn't require prior test runs or stored state.
Baseline selection
The diff is computed against a baseline that is presumed green. We pick the baseline in this order:
- Last-known-good (LKG) — the sha stored in a tracked file (e.g.
.egg/last-known-good) on the working branch, pointing to the most recent commit at which the full suite passed on this branch. Used only if the sha exists in the repo and is an ancestor ofHEAD. - Base branch —
$(BASE_BRANCH), defaulting toorigin/main.
LKG is preferred when available because it's typically further along than the base branch (the agent/developer has been iterating on this branch), giving a smaller and more accurate changed set. Both baselines are safe because both are presumed green.
LKG file storage
- Stored as a tracked file (
.egg/last-known-good) whose contents are a single commit sha. Because it's tracked, parallel branches each carry their own LKG and never stomp each other — no global working-tree state to coordinate. - Updated by committing a new sha to the file whenever the full suite passes — either
make test-all, or amake testinvocation that degraded to a full run via a conservative fallback. The update is a small auto-commit on the branch (e.g.chore: bump LKG to <sha>), or amended onto the commit that triggered the run when running locally — implementation detail to settle during design. - Never updated on a partial (changeset-aware) pass, since a partial pass doesn't prove the full suite is green.
- Invalidated (file content cleared or marker written) when any fallback trigger changes between LKG and
HEAD(lockfiles,Makefile, config, rootconftest.py) so we don't trust a baseline whose environment no longer matches. make test-record-goodexposes a manual override.
Merge behavior
Branch-local LKG values must not leak into main's history as meaningful state. On merge, the LKG file's content is replaced with the current main HEAD sha (the cleaner of the two options considered — the alternative was stripping the file entirely on merge, but that would mean re-bootstrapping LKG on every branch cut from main). Practically:
- Pre-merge hook / merge-queue step rewrites
.egg/last-known-goodtomain's pre-merge HEAD sha as part of the merge commit. - Branches cut from the new main inherit that LKG, which is a safe baseline (main is presumed green at that sha) and is typically close enough to main HEAD that the first
make teston a fresh branch gets a useful narrow diff. - This keeps
main's LKG history as a clean sequence of main-shas rather than a noisy log of per-branch iteration points.
Algorithm
- Resolve the baseline per Baseline selection above.
- Compute the changed file set:
git diff --name-only $BASELINE...HEADplus uncommitted changes. - Build a module-level import graph across
gateway/,orchestrator/,shared/,sandbox/, and thetests/trees (AST-parse each.pyfile, resolve imports to repo modules). - Compute the transitive reverse closure of the changed modules — every module that imports a changed module, directly or through a chain.
- Select the test files whose module is in that closure, or that themselves import (transitively) a changed module.
- Run only those test files via
pytest, preservingPYTEST_ARGS. - If the run passes and we ran the full suite (via fallback or
make test-all), update.egg/last-known-goodtoHEAD.
Conservative fallbacks (always run full suite when…)
- Any of these change:
Makefile,pyproject.toml,uv.lock/lockfiles,conftest.py(root or package-level),pytest.ini/setup.cfgtest config, fixtures undershared/tests/used repo-wide. - A changed file can't be mapped to a module (non-Python source used by tests, config/schema files, fixture data) — unless we add an explicit allowlist of known-safe paths.
- Dynamic imports /
importlib/ entry-point plugins touch a changed module. We should grep forimportlib,__import__, and known plugin-registration patterns during graph construction and widen selection when matched. - The diff is empty or the baseline can't be resolved.
.egg/last-known-goodpoints to a sha that isn't reachable fromHEAD(branch was rebased/reset past the LKG).
Optional refinements (later)
- Layer
pytest-testmonor a coverage-derived mapping on top of the static graph to further narrow within a selected test file (run only affected test functions). Only worth doing if the static approach still selects too much. - Cache the import graph keyed by tree hash for repeat invocations.
UX
make test→ changeset-aware against the LKG sha in.egg/last-known-goodif valid, else$(BASE_BRANCH).make test-all→ current behavior (run everything); updates LKG on pass.make test-record-good→ manually set LKG toHEAD(escape hatch).- Log the selection decision: which baseline was used (LKG sha or base branch), number of changed files, size of affected set, and which fallback (if any) triggered a full run. This makes it debuggable when someone expects a test to run and it doesn't.
Out of scope
- Integration/E2E/security test targets — those stay as-is.
- Pre-commit / lint changes.
Acceptance criteria
- Branch touching only a leaf module in
gateway/runs just the tests transitively importing it, not all ofgateway/tests/. - Changes to
shared/modules correctly pull in tests acrossgateway/,orchestrator/, andsandbox/that import them. - Editing
conftest.py,Makefile, or lockfiles triggers the full suite. make test-allruns the full suite unconditionally and refreshes.egg/last-known-goodon pass.- A successful
make test-allfollowed by an unrelated one-file edit runs only the tests affected by that edit (diff is vs LKG, not base branch). - Two branches iterating in parallel each advance their own LKG without interfering.
- On merge, the merge commit on
mainrewrites.egg/last-known-goodtomain's pre-merge HEAD sha. - LKG is invalidated (fallback to full suite) when a fallback-trigger file changes between LKG and
HEAD, or when LKG is no longer reachable fromHEAD. - Selection logic has its own unit tests (synthetic repo fixture + expected selected set) — this is the kind of thing that silently regresses.
- Documented where the test commands are described.
Activity
refine(#1973): analysis for changeset-aware `make test`
refine(#1973): address reviewer_refine non-blocking nits
refine(#1973): align analysis Open Questions with contract IDs
architect: plan analysis for #1973 changeset-aware make test
plan(#1973): changeset-aware `make test` via grimp + sidecar LKG
plan(#1973): address reviewer_plan NACK — 3 blocking + 10 non-blocking
plan(#1973): address reviewer_plan v2 NACK — R1 + R2 + R6/R7/R10/R14/R5
plan(#1973): adopt reviewer_plan v3 ACK polish items
Fix #1973: add grimp dep and gitignore sidecars (foundation)
docs(#1973): add testing guide for changeset-aware make test
Fix #1973: add scripts/select_tests.py (changeset-aware selector)
implement(#1973): foundation — grimp dev dep + sidecar gitignore
docs(#1973): add testing guide for changeset-aware make test
implement(#1973): scripts/select_tests.py — changeset-aware test sele…
implement(#1973): Makefile narrow-default + CI full-suite switch
implement(#1973): address reviewer_contract polish — reverse_closure …
implement(#1973): address reviewer_code NACK — sys.path + PYTEST_ARGS…
implement(#1973): thread repo_root through sidecar I/O (tester NACK)
test(#1973): add tests/tools/test_select_tests_*.py for changeset-awa…
Make `make test` changeset-aware via grimp + sidecar LKG (#2042)
Make `make test` changeset-aware via grimp + sidecar LKG (#2042)