Fix flaky `test_engine_definition` budget failures by starting `node` once per module
This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.
- Closes #12622
Why
The first node start on a fresh CI runner can take several seconds. In tests/harness/gh_aw/test_engine_definition.py, whichever tests each xdist worker runs first pay for it, and on the 3.13 budget leg they go over PYTEST_TEST_BUDGET_SECONDS (8s).
A module-scoped autouse fixture now starts node once before the module's tests. The per-test budget does not count shared fixture setup (tests/cost_guards.py :: pytest_fixture_setup), so the one-time cost no longer lands on a test.
Root cause
Three failing runs show the same shape: four of the module's earliest node-launching tests, with near-identical inflated times, while every other test in the module takes 2.9s or less.
| Run | Shard | The four slow tests |
|---|---|---|
| PR #12572 | 2/2 | 7.96s to 8.49s |
main | 1/2 | 8.02s to 8.76s |
| merge queue | 1/2 | 10.17s to 10.82s |
One of the four in every run is test_harness_rejects_a_runtime_without_protected_capture, which starts node and exits before any Python child, so the shared cost is node's first start. The only other tests that start node are in tests/ui_roundtrip/, which collects after tests/harness/. Why a first start is slow on the runner (cold disk reads, most likely) is not verified.
Reproduction
A node shim first on PATH forces a cold first start: every launch waits until 10s after the first launch, then execs the real node. This shows the mechanism: the cold start fails whichever test starts node first, as each xdist worker's first node test did in CI. Same six nodes, serial, PYTEST_TEST_BUDGET_SECONDS=8:
| Result | Where the 10s lands | |
|---|---|---|
| Without the fix | 1 failed, 5 passed | test_harness_rejects_a_runtime_without_protected_capture call: 9.93s, over budget |
| With the fix | 6 passed | started_node setup: 10.54s, not counted |
Without the fix, test_wrapper_imports_a_custom_agent_once[custom_agent:agent] run first | 1 failed, 3 passed | that node's call: 10.26s, over budget |
Shim and command
#!/bin/sh
marker=/path/to/coldnode/deadline
if mkdir "$marker.lock" 2>/dev/null; then echo $(($(date +%s) + 10)) > "$marker"; fi
while [ ! -s "$marker" ]; do sleep 0.05; done
deadline=$(cat "$marker")
while [ "$(date +%s)" -lt "$deadline" ]; do sleep 0.1; done
exec /path/to/real/node "$@"
PATH=/path/to/coldnode/bin:$PATH PYTEST_TEST_BUDGET_SECONDS=8 uv run pytest \
'tests/harness/gh_aw/test_engine_definition.py::test_harness_rejects_a_runtime_without_protected_capture' \
'tests/harness/gh_aw/test_engine_definition.py::test_wrapper_imports_a_custom_agent_once' \
'tests/harness/gh_aw/test_engine_definition.py::test_wrapper_imports_json_and_yaml_agent_specs' \
'tests/harness/gh_aw/test_engine_definition.py::test_interpreter_fallback_resolves_python3_on_path' \
-p no:randomly
Remove the marker and its .lock directory between runs.
What changes for existing users
Nothing. Test-only change in the failing test's module.
The fixture is autouse, so it also runs before the module's tests that never start node. Deliberate: it costs one warm node start per worker in uncounted setup.
Sibling not folded in
tests/harness/gh_aw/test_logfire_isolation.py imports launch and starts node too, so it could absorb the same first start if a worker runs it before this module. It has not failed in the runs above, so this PR leaves it alone.
PR decisions
2026-11-06 · Warm node in a module fixture, not a slow marker · iter -
- Decision: Start node once in a module-scoped autouse fixture; no @pytest.mark.slow
- Why: The 7-10s is node's first start on a fresh runner, charged to whichever tests run first; later node tests take 1-2.5s, so the cost is not inherent to any test
- Source: https://github.com/pydantic/pydantic-ai/actions/runs/42032254666
- Session: 22b5708d-6883-4836-bb67-2641f6788bec
2026-11-06 · pre-push review 1 of 3 at d46a7c70ae8eb90c810a1a4a05b93370c3fee6a2 · iter -
- Decision: no code findings; body remediated (repro shows the mechanism; autouse scope noted); docstring and env-warmup findings skipped with evidence
- Why: tree 1a0e9768ee5519b12ae2808b2f0c51ca76f07ad6; scope e2332baaba..d46a7c70ae; with-fix run passed after dispatch
- Source: https://github.com/pydantic/pydantic-ai/tree/fix-flaky-gh-aw-engine-definition-budget
- Session: 22b5708d-6883-4836-bb67-2641f6788bec
2026-11-06 · Decline shared conftest fixture for test_logfire_isolation · iter -
- Decision: Keep the fixture module-scoped in the failing module; sibling reported to the manager, not folded
- Why: Flake fixes may change only the failing test's module; the sibling has not failed in CI; local forced repro inconclusive (its tests run ~30s here regardless)
- Source: https://github.com/pydantic/pydantic-ai/pull/12659#discussion_r4487631811
- Session: 22b5708d-6883-4836-bb67-2641f6788bec
2026-11-06 · exempt main merge at 20fb0594b2 · iter -
- Decision: Pushed without re-review: target-branch merge, no conflicts
- Why: git diff main...d46a7c70ae equals git diff main...20fb0594b2 with main fetched; chains from reviewed d46a7c70ae; review budget 1 of 3 used
- Source: https://github.com/pydantic/pydantic-ai/pull/12659
- Session: 22b5708d-6883-4836-bb67-2641f6788bec
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
- PR title is fit for the release changelog.