Use `anyio.Event` for core run handoffs

Merged
KL
2mo ago
+20-9

Why we make these changes

Related to #7112. Core run handoffs and immediate event dispatch use one-shot asyncio.Event signals. Use anyio.Event for these signals as one step toward Trio support. Task creation and cancellation remain separate work.

New public surface

None.

User-visible behavior

pydantic_ai_slim/pydantic_ai/agent/__init__.py :: Agent.run()
 └─ pydantic_ai_slim/pydantic_ai/agent/__init__.py :: _run_lifecycle_hooks()
-   └─ asyncio.Event() for run readiness and completion
+   └─ anyio.Event() for run readiness and completion

The same change covers model-stream handoff and immediate event settlement. Asyncio behavior is unchanged. This PR does not enable end-to-end Trio runs.

Verification

  • Six focused existing tests passed, including run-handoff cancellation and immediate-event settlement.
  • Pyright on the three changed files, Ruff, and pre-commit passed locally.
  • Tests remain on asyncio, as planned for the incremental migration.
  • CI passed on e0af2d8, including coverage and strict-no-cover. A covered CI cell runs one Absurd test with the database URL unset to exercise its skip path reliably.

What changes for existing users

Nothing under asyncio. The changed signals can run on either AnyIO backend when the surrounding task lifecycle supports it.

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. (Not applicable.)
  • PR title is fit for the release changelog.

AI Disclaimer

This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.