Fail CI when a change adds a real-clock wait to a test or an `asyncio` primitive to backend-neutral code
- Closes #13947
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, onscripts/_ratchet.py, the base-ref comparison extracted fromscripts/check_complexity.py. Per file undertests/, 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, privateREADINESS_WAIT_TIMEOUTdefinitions, or ports read from a closed socket.# real-wait: <reason>exempts a wait that is about real time. It runs in pre-commit and thecomplexityCI 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 usereserve_loopback_port()from the newtests/_ports.py, moved out oftests/conftest.py. - R7: ruff
TID251bansasyncio.sleep,Event,to_thread,wait_for,timeout,Queue,SemaphoreandCondition, each message naming the anyio replacement. Each existing library use carries# noqa: TID251 (<reason>), whichRUF100removes once the line is converted. Tests, examples, scripts and clai2 are exempt per file, and.github/scripts/test_lint_exemptions.pychecks that theTypedDictandasyncio.Lockbans still hold there.tests/test_examples.pylints docs examples without the asyncio bans, and with every other entry. - The guidance matches the gates:
agent_docs/concurrency.md, the harnessagent_docs/concurrency.mdand thepushing-commits-to-the-reposkill point atREADINESS_WAIT_TIMEOUTintests/_timeouts.py, and thetests/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 (itsrunning.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.Eventandasyncio.sleepuses. reserve_loopback_port()now also backs the OAuth callback tests intests/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 unchangedscripts/test_check_complexity.pyon 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-ignoresentry: it owns its asyncio loop (asyncio.runincli/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. gatherandcreate_taskstay unbanned:agent_docs/concurrency.mdrecommends each for specific uses, and neither has a drop-in anyio swap.- No
lax no coverratchet: 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_porthelpers insrc/pydantic_ai_harness/integration_tests/(localstack, kubernetes) stay as they are.
PR decisions
- 2026-11-19 · Waiter list includes
.result(timeout=): #10495 addedrunning.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 intests/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_TIMEOUTtoo (2 harness modules). Source: #13947. Session: 762e5599-0298-4091-b4dc-fe3b1297793f - 2026-11-19 · Ban list leaves out
gatherandcreate_task:agent_docs/concurrency.mdrecommends both for specific uses. Source: #13947. Session: 762e5599-0298-4091-b4dc-fe3b1297793f - 2026-11-19 · The port helper moves to a public
reserve_loopback_portintests/_ports.py, mirroringtests/_timeouts.py. Source: gates-mgr. Session: 762e5599-0298-4091-b4dc-fe3b1297793f - 2026-11-19 · R1 runs as a step in the existing
complexityCI 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 andtests/test_examples.py), thetests/AGENTS.mdreadiness bullet naming function-local definitions too, and thecognitive complexityjob header naming both gates. Source: douwebot's approval on this PR. Session: 5dbcce7a-cc39-4099-adae-bab2f6391004 - 2026-11-19 · No
lax no coverrule: it is not in #13947's gate list, #9022 and #11162 fixed flakes by adding it, and the coverageexclude_linesdocuments it. Source: gates-mgr. Session: 762e5599-0298-4091-b4dc-fe3b1297793f - 2026-11-19 · R7 baseline: per-line
# noqa: TID251 (<reason>)in library code, in theE402reason format ofmessages.py, with no per-file cap, as.github/scripts/issue_pr_attention_monitor.pyandrender_sandbox/_command_runner.pyalready do;per-file-ignoresplus 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-ignoresentry, 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
returninside 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-ignoresnever 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
complexityCI job, sharing a base-ref runner extracted fromscripts/check_complexity.py. - 32 private
READINESS_WAIT_TIMEOUTdefinitions move to the sharedtests/_timeouts.pyconstant, and 4 probe-then-close ports use the newtests/_ports.pyreserve_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>), whichRUF100removes as lines convert; tests, examples, scripts and clai2 are exempt per file. .github/scripts/test_lint_exemptions.pyverifies theTypedDictandasyncio.Lockbans still hold in the exempted files.- Readiness-wait guidance in
agent_docs/concurrency.md, the harnessagent_docs/concurrency.md, thepushing-commits-to-the-reposkill andtests/AGENTS.mdnow point attests/_timeouts.pyand document the gate.