Speed up CI by fixing the slowest tests, balancing shards by measured duration, and guarding test cost

Merged
DO
2mo ago
+2678-690

CI's critical path on main is about 13 minutes. In run 37943388487, the slowest job (3.11 all-extras, shard 1/3) spent:

  • about 4 minutes before its first test ran: every xdist worker collected all ~29k tests, then threw most of them away;
  • 7.5 minutes running tests, while its two sibling shards needed 3.5–4.5, because pytest-split was splitting by test count (no durations file);
  • most of the remaining time in a few hundred tests that launched python -m ... subprocesses, waited out real timeouts, used inputs far larger than needed, or started a Prefect server per module.

This PR fixes those causes and adds guards so the suite doesn't drift back.

Faster tests

Times are summed across the 25 test jobs of the run above. No test was dropped, and every touched test keeps its assertions or gets stronger ones.

AreaBeforeCauseFix
tests/clai2/test_worktrees.py, test_cli_edges.py, test_terminal.py, test_resume_integration.py~2.4k seach test launched python -m pydantic_clai2new run_cli fixture (tests/clai2/cli_runner.py) runs the CLI in-process, and launch() uses it; one installed-entry-point smoke test still launches a subprocess
tests/test_web_fetch.py budget tests~580 sinputs of 9–18 MB to cross the real complexity budgetspatch the budget down 100× and shrink the input with it; the same 16 deliberately broken versions of the code fail as before
tests/test_examples.py~985 sone example's logfire.configure() left the console exporter on for the rest of the worker, and each span cost ~67 ms through pytest-examples' print mockLOGFIRE_CONSOLE=false in the examples env
Prefect tests (tests/durable_exec, harness durable matrix and docs)~450 s of setupup to five prefect_test_harness starts per worker, 15–25 s eachone session-scoped prefect_test_server fixture in the prefect xdist group
Mongo, Redis, SQLite, file-search, ACP, gh_aw, stream-parity, benchmarks, realtime~900 soversized inputs, per-row commits, stdio subprocesses, pure-Python YAML parsing of ~900 cassettes, 8192-tool benchmark sizes in every job, a 2 s wait in the WebSocket cassette replayer on session.closesmaller inputs or patched limits, one transaction, in-memory ACP wire, libyaml CSafeLoader, large benchmark sizes only under CodSpeed, a replayer fix that also delivers the final session.closed usage
Real timeouts and GC waits (test_local_workspace, Copilot OAuth, plugin loader, herdr, streaming GC)~300 swaiting out 2–5 s timers; gc.collect() walking the whole worker heappatched or injected timers, plus a young_gc fixture that freezes pre-existing objects

The clai2 reload scenarios still need one fresh interpreter each, since a reload would corrupt the test process's module identities. They are marked subprocess and slow; forking them from one preloaded process was tried and was slower under coverage.

Two small library changes make hard-coded waits patchable without changing behavior: pydantic_clai2.plugins.loader.FAILED_SESSION_END_TIMEOUT and pydantic_clai2.builtin_plugins.herdr.TITLE_POLL_INTERVAL.

CI structure

  • Shards are balanced by measured time, from the latest main run.
    • A new test-durations-baseline job fetches the per-test durations the shards of the latest main run uploaded, once per run, so every shard splits from the same file.
    • .github/scripts/shard_durations.py select assigns whole test modules to shards and passes --ignore for the other shards' modules. Each shard's workers therefore collect only their own modules. A module the script doesn't know about runs in every shard rather than none.
    • Without a baseline (the first run after merge), modules are weighted by source size.
  • Per-test cost shows up on PRs. On the 3.12 all-extras shards, the job summary lists new tests over 1 s and tests at least 2× (and 1 s) slower than on main.

Guards

tests/cost_guards.py, loaded from tests/conftest.py:

  • Python subprocesses need a reason. A test that launches a Python interpreter (sys.executable, python*, a venv console script, uv run python, multiprocessing spawn/forkserver) fails unless it is marked @pytest.mark.subprocess(reason=...). Shared (module/session) fixtures are exempt. A marker that no longer covers any launch fails the session as stale, so markers don't outlive their reason.
  • Per-test time budget. With PYTEST_TEST_BUDGET_SECONDS set, a test whose setup plus call exceeds the budget fails unless it is marked @pytest.mark.slow(reason=...). Setup of fixtures shared beyond one test is excluded. CI sets 8 s on the 3.13 all-extras shards, which run without coverage or BlockBuster. scripts/list_over_budget_tests.py <run-id> lists offenders from a run's logs.
  • Heavy imports stay out of collection. Ruff TID253 bans module-level imports of the heaviest optional dependencies under tests/, based on measured import cost. A collection-time summary names the slowest modules to collect.
  • Guidance where tests get written and reviewed. A "Test cost" section in tests/AGENTS.md, a clarification in AGENTS.md that "integration tests" means in-process code paths with recorded requests (not process spawns or real waits), and a matching "test cost" rule for the PR review bot.

Follow-ups (not in this PR)

  • Lower the timeout-minutes of the sharded jobs once a few main runs show the new durations.
  • Collect 3.11 coverage only for the 3.11-specific branches and extras, which could drop its third shard.
  • #10046 halves import pydantic_ai time, which speeds up every remaining subprocess test.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
  • PR title is fit for the release changelog.

🤖 Generated with Claude Code