Fail CI when a change adds a real-clock wait to a test or an `asyncio` primitive to backend-neutral code

Merged
DS
16d ago
+1581-356

Why we make these changes

The no-real-timer rule for tests and the anyio-only rule for backend-neutral code live in prose and reviewer prompts, so nothing fails a change that adds a violation, and the same flake fixes keep coming back. This PR builds proposals R1 and R7 from #13947 as deterministic gates.

  • R1: scripts/check_test_waits.py, on scripts/_ratchet.py, the base-ref comparison extracted from scripts/check_complexity.py. Per file under tests/, a change may not raise the count of literal waits (sleep, fail_after, move_on_after, asyncio.timeout, asyncio.wait_for, .wait(), .join(), .result(timeout=)), loops that sleep, private READINESS_WAIT_TIMEOUT definitions, or ports read from a closed socket. # real-wait: <reason> exempts a wait that is about real time. It runs in pre-commit and the complexity CI job.
  • A one-time cleanup starts the last two rules at zero: 32 test modules import the shared READINESS_WAIT_TIMEOUT, and 4 probe-then-close ports use reserve_loopback_port() from the new tests/_ports.py, moved out of tests/conftest.py.
  • R7: ruff TID251 bans asyncio.sleep, Event, to_thread, wait_for, timeout, Queue, Semaphore and Condition, each message naming the anyio replacement. Each existing library use carries # noqa: TID251 (<reason>), which RUF100 removes once the line is converted. Tests, examples, scripts and clai2 are exempt per file, and .github/scripts/test_lint_exemptions.py checks that the TypedDict and asyncio.Lock bans still hold there. tests/test_examples.py lints docs examples without the asyncio bans, and with every other entry.
  • The guidance matches the gates: agent_docs/concurrency.md, the harness agent_docs/concurrency.md and the pushing-commits-to-the-repo skill point at READINESS_WAIT_TIMEOUT in tests/_timeouts.py, and the tests/AGENTS.md "Test cost" section names the gate, reserve_loopback_port() and the # real-wait: <reason> escape.

New public surface

None.

Verification

  • R1 fails on the merge commit of each introducing PR, replayed with --base <merge>^1: #10072, #10070, #10495 (its running.result(timeout=10)), #10746, #11570 and #10372.
  • R7 fails at the parents of #9529 and #9542 (core), and on #12222 and #13283 (harness), which added asyncio.Event and asyncio.sleep uses.
  • reserve_loopback_port() now also backs the OAuth callback tests in tests/providers/ that used a plain probe. Those tests pass on the Linux legs. No CI leg runs them on macOS or Windows.
  • scripts/test_check_test_waits.py, .github/scripts/test_lint_exemptions.py, and the unchanged scripts/test_check_complexity.py on the extracted runner.

What changes for existing users

Nothing: this changes CI, test and lint configuration only.

Scope decisions

  • clai2 stays exempt as one per-file-ignores entry: it owns its asyncio loop (asyncio.run in cli/cli.py), and agent runs need asyncio today; revisit with #7112. So the R7 replay does not fail on the clai2 sweep (#9535, #9536, #9537, #9539, #9547, #9549). A reviewer can flip clai2 to per-line noqa instead.
  • gather and create_task stay unbanned: agent_docs/concurrency.md recommends each for specific uses, and neither has a drop-in anyio swap.
  • No lax no cover ratchet: the pragma is the sanctioned remedy for timing-dependent coverage.
  • Docs examples may use the banned asyncio primitives, as examples/ may: they are application code, which runs on asyncio. They are not converted to anyio.
  • The ratchet caps the existing backlog under tests/ (1334 literal waits, 298 sleep loops); burning it down is follow-up work.
  • A test that hangs on a readiness wait now fails after the shared 300 s instead of its old private 5, 10 or 30 s; a wait that should succeed is unchanged.
  • R1 covers tests/ only. The two probe-then-close _free_port helpers in src/pydantic_ai_harness/integration_tests/ (localstack, kubernetes) stay as they are.
PR decisions
  • 2026-11-19 · Waiter list includes .result(timeout=): #10495 added running.result(timeout=10), which the replay misses otherwise. Source: #13609, confirmed by gates-mgr. Session: 762e5599-0298-4091-b4dc-fe3b1297793f
  • 2026-11-19 · sleep(0) is a checkpoint, not a literal wait; it still makes a loop a sleep loop. Source: #13947. Session: 762e5599-0298-4091-b4dc-fe3b1297793f
  • 2026-11-19 · The port rule flags a port used after its socket closes, not every bind((host, 0)): 5 of 12 such sites in tests/ keep the socket open and hand it on. Source: #13947. Session: 762e5599-0298-4091-b4dc-fe3b1297793f
  • 2026-11-19 · The readiness rule matches _READINESS_WAIT_TIMEOUT too (2 harness modules). Source: #13947. Session: 762e5599-0298-4091-b4dc-fe3b1297793f
  • 2026-11-19 · Ban list leaves out gather and create_task: agent_docs/concurrency.md recommends both for specific uses. Source: #13947. Session: 762e5599-0298-4091-b4dc-fe3b1297793f
  • 2026-11-19 · The port helper moves to a public reserve_loopback_port in tests/_ports.py, mirroring tests/_timeouts.py. Source: gates-mgr. Session: 762e5599-0298-4091-b4dc-fe3b1297793f
  • 2026-11-19 · R1 runs as a step in the existing complexity CI job, so no new required check. Source: gates-mgr. Session: 762e5599-0298-4091-b4dc-fe3b1297793f
  • 2026-11-19 · Instruction-text edits stay out of this PR until a maintainer signs off; the reviewer-prompt real-timer clauses stay, since the gate does not see timers inside the code under test. Source: gates-mgr. Session: 762e5599-0298-4091-b4dc-fe3b1297793f
  • 2026-11-19 · The instruction-text edits land in this PR, approved by David, so the gate and the guidance agree at merge. Source: douwebot's finding on this PR. Session: 5dbcce7a-cc39-4099-adae-bab2f6391004
  • 2026-11-19 · Three optional douwebot findings are left for later: one source for the asyncio ban set (now in pyproject.toml, the guard test and tests/test_examples.py), the tests/AGENTS.md readiness bullet naming function-local definitions too, and the cognitive complexity job header naming both gates. Source: douwebot's approval on this PR. Session: 5dbcce7a-cc39-4099-adae-bab2f6391004
  • 2026-11-19 · No lax no cover rule: it is not in #13947's gate list, #9022 and #11162 fixed flakes by adding it, and the coverage exclude_lines documents it. Source: gates-mgr. Session: 762e5599-0298-4091-b4dc-fe3b1297793f
  • 2026-11-19 · R7 baseline: per-line # noqa: TID251 (<reason>) in library code, in the E402 reason format of messages.py, with no per-file cap, as .github/scripts/issue_pr_attention_monitor.py and render_sandbox/_command_runner.py already do; per-file-ignores plus the guard test for non-library paths, tests/ included. Source: gates-mgr. Session: 762e5599-0298-4091-b4dc-fe3b1297793f
  • 2026-11-19 · clai2 is one per-file-ignores entry, the status quo, with a revisit point: agent runs need asyncio today (Douwe on #13217). Source: #13217. Session: 762e5599-0298-4091-b4dc-fe3b1297793f
  • 2026-11-19 · A return inside a probe socket's block counts only when it reads the port; annotated and unpacked port names are tracked. Source: macroscope findings on this PR. Session: 5dbcce7a-cc39-4099-adae-bab2f6391004
  • 2026-11-19 · A port name returned or yielded after the probe socket's block counts as an escape. A waiter delay whose name shadows a module constant stays counted: no test module has that shape. Source: macroscope findings on this PR. Session: 5dbcce7a-cc39-4099-adae-bab2f6391004
  • 2026-11-19 · Docs examples lint without the asyncio bans: pytest-examples lints them from stdin, so per-file-ignores never reach them, and they failed CI on the new bans. Source: https://github.com/pydantic/pydantic-ai/actions/runs/44461474875. Session: 5dbcce7a-cc39-4099-adae-bab2f6391004
  • 2026-11-19 · Three local pre-push reviews ran; their findings (move credit for the hard rules, the port-rebind scope, docstring accuracy) are fixed. Source: https://github.com/pydantic/pydantic-ai/tree/gates-test-waits-asyncio-lint. Session: 762e5599-0298-4091-b4dc-fe3b1297793f

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.

Summary by cubic

Adds two deterministic CI gates so the same flake fixes stop coming back. R1 fails a change that adds a real-clock wait, private readiness timeout, or probed port to a test under tests/, ratcheting against the base ref with no committed baseline. R7 bans asyncio.sleep, Event, to_thread, wait_for, timeout, Queue, Semaphore and Condition from backend-neutral library code via ruff TID251, with each message naming its anyio replacement. No behavior changes for users: only CI, test and lint configuration.

  • R1 runs in pre-commit and the existing complexity CI job, sharing a base-ref runner extracted from scripts/check_complexity.py.
  • 32 private READINESS_WAIT_TIMEOUT definitions move to the shared tests/_timeouts.py constant, and 4 probe-then-close ports use the new tests/_ports.py reserve_loopback_port(), so both hard rules start at zero; the probe rule also flags a port returned or yielded after its socket closes.
  • Existing library uses of the banned primitives carry per-line # noqa: TID251 (<reason>), which RUF100 removes as lines convert; tests, examples, scripts and clai2 are exempt per file.
  • .github/scripts/test_lint_exemptions.py verifies the TypedDict and asyncio.Lock bans still hold in the exempted files.
  • Readiness-wait guidance in agent_docs/concurrency.md, the harness agent_docs/concurrency.md, the pushing-commits-to-the-repo skill and tests/AGENTS.md now point at tests/_timeouts.py and document the gate.