Skip to content

Make make test changeset-aware (only run tests affected by diff from base branch) #1973

Closed

Description

@jwbron

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, or modulegraph may 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:

  1. 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 of HEAD.
  2. Base branch — $(BASE_BRANCH), defaulting to origin/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 a make test invocation 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, root conftest.py) so we don't trust a baseline whose environment no longer matches.
  • make test-record-good exposes 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-good to main'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 test on 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

  1. Resolve the baseline per Baseline selection above.
  2. Compute the changed file set: git diff --name-only $BASELINE...HEAD plus uncommitted changes.
  3. Build a module-level import graph across gateway/, orchestrator/, shared/, sandbox/, and the tests/ trees (AST-parse each .py file, resolve imports to repo modules).
  4. Compute the transitive reverse closure of the changed modules — every module that imports a changed module, directly or through a chain.
  5. Select the test files whose module is in that closure, or that themselves import (transitively) a changed module.
  6. Run only those test files via pytest, preserving PYTEST_ARGS.
  7. If the run passes and we ran the full suite (via fallback or make test-all), update .egg/last-known-good to HEAD.

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.cfg test config, fixtures under shared/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 for importlib, __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-good points to a sha that isn't reachable from HEAD (branch was rebased/reset past the LKG).

Optional refinements (later)

  • Layer pytest-testmon or 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-good if valid, else $(BASE_BRANCH).
  • make test-all → current behavior (run everything); updates LKG on pass.
  • make test-record-good → manually set LKG to HEAD (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 of gateway/tests/.
  • Changes to shared/ modules correctly pull in tests across gateway/, orchestrator/, and sandbox/ that import them.
  • Editing conftest.py, Makefile, or lockfiles triggers the full suite.
  • make test-all runs the full suite unconditionally and refreshes .egg/last-known-good on pass.
  • A successful make test-all followed 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 main rewrites .egg/last-known-good to main'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 from HEAD.
  • 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

  1. added
    p1
    on Apr 24, 2026
  2. added 19 commits that reference this issue on Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    p1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions