Add `tests/refactor_capture.py`, an opt-in pytest plugin that proves a refactor keeps streamed events

Merged
DS
1mo ago
+618-0

This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.

Part of #10983.

Why. #12302 proved its stream refactor with an out-of-tree capture script, and douwebot blocked committing it: no type checking or tests, silently broken by a change to the private internals it patched, and a copy-out step to run it against the base commit. The coming stream refactors in #10983 need the same proof, so this lands it as gated test tooling.

New public surface. None. Test tooling only: tests/refactor_capture.py and its tests. Pytest loads the plugin only through -p tests.refactor_capture --refactor-capture=DIR, which the command below passes, so default and benchmark runs never import it.

What it does.

uv run python -m tests.refactor_capture "$(git merge-base HEAD upstream/main)" <test nodes>
  • git archives the base commit's workspace packages and runs the nodes twice, base sources ahead on PYTHONPATH the first time. Tests, plugin and environment come from the working tree both times, so nothing is copied and only library code differs.
  • Records the events __aiter__ hands consumers, on StreamedResponse and every subclass that overrides it, plus any error and the response get() builds when the test ends. Generated ids and timestamps are normalized.
  • Compares per test node and exits non-zero on any difference or failed pytest run. It refuses two captures that imported pydantic_ai from the same file.
  • An agent run over a canary stream at startup stops the run with a UsageError if the stream goes unrecorded.
  • Records carry kind: 'stream', so a wire capture can add its own kind to the same files and comparator.

Verification.

  • tests/test_refactor_capture.py: requires both -p tests.refactor_capture and --refactor-capture, records per test (including a subclass defined after the capture started, and a FallbackModel stream whose first event is peeked), stops the run on an unrecorded canary, refuses to append, comparator flags a changed event and a missing node and passes identical captures, normalization.
  • Demo against the commit before #12302, over 17 streaming test nodes (streams from 9 model modules, FallbackModel and the continuation composite): 41 streams and 188 events per side, 0 differing. The committed tool reproduces #12302's proof.

Usage note: a new "Proving a refactor keeps stream events" section in tests/AGENTS.md.

What changes for existing users. Nothing.

PR decisions

2026-11-03 · name the plugin for both captures · iter -

  • Decision: Module tests/refactor_capture.py, env var REFACTOR_CAPTURE_DIR; records carry kind (stream now)
  • Why: R4 adds a wire capture to the same plugin; a stream-only name would then mislead, and kind lets its records share the files and the comparator
  • Source: https://github.com/pydantic/pydantic-ai/pull/12302#pullrequestreview-5745500067 (douwebot, finding 1)
  • Session: b2893d43-23ea-4f93-a849-d07dce6e229c

2026-11-03 · capture base without copying the plugin · iter -

  • Decision: One command: git archive the base commit's workspace packages, run head's tests and plugin twice, base sources ahead on PYTHONPATH for the base run
  • Why: No copy step and no second venv; same tests both sides isolate the library diff; source.json makes the comparator refuse two captures that imported pydantic_ai from the same file, so a failed overlay can't pass as identical
  • Source: https://github.com/pydantic/pydantic-ai/pull/12302#pullrequestreview-5745500067 (douwebot, finding 1)
  • Session: b2893d43-23ea-4f93-a849-d07dce6e229c

2026-11-03 · hook point and loud failure · iter -

  • Decision: Hook only _get_event_iterator (the documented override point) through StreamedResponse.__aiter__; a canary stream at configure raises UsageError if unrecorded
  • Why: Drops the _event_iterator read douwebot flagged; a hook-point change stops the run instead of recording nothing
  • Source: https://github.com/pydantic/pydantic-ai/pull/12302#pullrequestreview-5745500067 (douwebot, finding 1)
  • Session: b2893d43-23ea-4f93-a849-d07dce6e229c

2026-11-03 · record the response when the test ends · iter -

2026-11-03 · pre-push review 1 of 3 at 77f6ef01a47e5c35cd2a0aaa8b94e958776e7123 · iter -

2026-11-03 · pre-push review 1 of 3 at 77f6ef01a47e5c35cd2a0aaa8b94e958776e7123 · iter -

2026-11-03 · record what consumers see, through the public aiter · iter -

  • Decision: Wrap the iterator __aiter__ returns, on StreamedResponse and every subclass that overrides it (existing ones at start, later ones through __init_subclass__); the canary is an agent run; the hook calls no private method
  • Why: R18 refactors StreamedResponse.__aiter__ under the same E proof, and the raw _get_event_iterator hook missed PartEndEvent/FinalResultEvent and the continuation composite's own stitching
  • Source: https://github.com/pydantic/pydantic-ai/tree/stream-event-capture-plugin
  • Session: b2893d43-23ea-4f93-a849-d07dce6e229c
  • Supersedes: hook point and loud failure

2026-11-03 · pre-push review 2 of 3 at ed834b548f03ea5909ef4c5a1dfbc79ba285953f · iter -

2026-11-03 · pre-push review 2 of 3 at ed834b548f03ea5909ef4c5a1dfbc79ba285953f · iter -

2026-11-03 · pre-push review 3 of 3 at 188f5b9b397462c23dc31d7fe7d0f23b1e60352d · iter -

2026-11-03 · pre-push review 3 of 3 at 188f5b9b397462c23dc31d7fe7d0f23b1e60352d · iter -

2026-11-03 · tests/AGENTS.md usage note · iter -

  • Decision: Add "Proving a refactor keeps stream events" to tests/AGENTS.md verbatim as proposed
  • Why: David approved the wording as is; pushed under an exhausted pre-push review budget (3 of 3 used), so this head is unreviewed locally
  • Source: https://github.com/pydantic/pydantic-ai/pull/12355
  • Session: b2893d43-23ea-4f93-a849-d07dce6e229c

2026-11-03 · activate the capture with -p and an option · iter -

  • Decision: tests/conftest.py stays as on main; pytest loads the plugin only through -p tests.refactor_capture --refactor-capture=DIR, which main passes; the REFACTOR_CAPTURE_DIR env var is gone
  • Why: Importing the plugin from conftest tripped CodSpeed's loaded-dependencies guard, and listing it in affects_benchmarks would touch CODEOWNERS-owned .github/workflows/; David chose option 1: default and benchmark runs never import the plugin, and --refactor-capture without -p fails as an unknown option. Pushed under the exhausted review budget
  • Source: https://github.com/pydantic/pydantic-ai/actions/runs/41662722683
  • Session: b2893d43-23ea-4f93-a849-d07dce6e229c
  • Supersedes: name the plugin for both captures

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.