Add `tests/refactor_capture.py`, an opt-in pytest plugin that proves a refactor keeps streamed events
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 onPYTHONPATHthe 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, onStreamedResponseand every subclass that overrides it, plus any error and the responseget()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_aifrom the same file. - An agent run over a canary stream at startup stops the run with a
UsageErrorif 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_captureand--refactor-capture, records per test (including a subclass defined after the capture started, and aFallbackModelstream 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,
FallbackModeland 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 varREFACTOR_CAPTURE_DIR; records carrykind(streamnow) - Why: R4 adds a wire capture to the same plugin; a stream-only name would then mislead, and
kindlets 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 archivethe base commit's workspace packages, run head's tests and plugin twice, base sources ahead onPYTHONPATHfor the base run - Why: No copy step and no second venv; same tests both sides isolate the library diff;
source.jsonmakes the comparator refuse two captures that importedpydantic_aifrom 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) throughStreamedResponse.__aiter__; a canary stream at configure raisesUsageErrorif unrecorded - Why: Drops the
_event_iteratorread 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 -
- Decision: Call
get()per recorded stream at test end, not when_get_event_iteratoris exhausted - Why: At exhaustion
__aiter__hasn't set_finished, so every recorded state readincomplete(the #12302 script had the same flaw); the canary check caught it - Source: https://github.com/pydantic/pydantic-ai/pull/12302#pullrequestreview-5745500067 (douwebot, finding 1)
- Session: b2893d43-23ea-4f93-a849-d07dce6e229c
2026-11-03 · pre-push review 1 of 3 at 77f6ef01a47e5c35cd2a0aaa8b94e958776e7123 · iter -
- Decision: reserved; outcome follows
- Why: tree aab80389e24ef812f00adcd071b41f1ec6690c14; scope full branch 6b3df9a7ba0d3f36197df1b7ba6b59bf631cf879..77f6ef01a47e5c35cd2a0aaa8b94e958776e7123; first review
- Source: https://github.com/pydantic/pydantic-ai/tree/stream-event-capture-plugin
- Session: b2893d43-23ea-4f93-a849-d07dce6e229c
2026-11-03 · pre-push review 1 of 3 at 77f6ef01a47e5c35cd2a0aaa8b94e958776e7123 · iter -
- Decision: remediated: hook moved to the consumer-facing
__aiter__output on every overriding subclass;normalizedtested; missing capture returns 2; class renamedRefactorCapture. Dismissed thepragma: no coverfinding: strict-no-cover'sBLOCK_OPENINGSexempts executeddef/iflines. First launch failed on CLI arg parsing before any review ran - Why: tree aab80389e24ef812f00adcd071b41f1ec6690c14; scope 6b3df9a7ba..77f6ef01a4 full branch
- Source: https://github.com/pydantic/pydantic-ai/tree/stream-event-capture-plugin
- Session: b2893d43-23ea-4f93-a849-d07dce6e229c
2026-11-03 · record what consumers see, through the public aiter · iter -
- Decision: Wrap the iterator
__aiter__returns, onStreamedResponseand 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_iteratorhook missedPartEndEvent/FinalResultEventand 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 -
- Decision: reserved; outcome follows
- Why: tree 1e330047dfe444a50b821a61a8eb019403eb63f1; follow-up scope 77f6ef01a4..ed834b548f03ea5909ef4c5a1dfbc79ba285953f (last reviewed tree aab80389e2), full branch 6b3df9a7ba0d3f36197df1b7ba6b59bf631cf879..ed834b548f03ea5909ef4c5a1dfbc79ba285953f as context
- Source: https://github.com/pydantic/pydantic-ai/tree/stream-event-capture-plugin
- Session: b2893d43-23ea-4f93-a849-d07dce6e229c
2026-11-03 · pre-push review 2 of 3 at ed834b548f03ea5909ef4c5a1dfbc79ba285953f · iter -
- Decision: remediated: hand the recording out only in place of the iterator it wraps (
request_stream_until_first_eventstores a peeking stream over it), with aFallbackModeltest;mainfails when either pytest run fails; test module skips under a capture; documented the__init_subclass__/late-__aiter__escape instead of checking for it - Why: tree 1e330047dfe444a50b821a61a8eb019403eb63f1; follow-up scope 77f6ef01a4..ed834b548f, full branch 6b3df9a7ba..ed834b548f as context; prior findings 1, 2, 4-7 verified fixed
- Source: https://github.com/pydantic/pydantic-ai/tree/stream-event-capture-plugin
- Session: b2893d43-23ea-4f93-a849-d07dce6e229c
2026-11-03 · pre-push review 3 of 3 at 188f5b9b397462c23dc31d7fe7d0f23b1e60352d · iter -
- Decision: reserved; outcome follows
- Why: tree 6f0e9594f9f5d4a051ccf9ab0f17dc036728059c; follow-up scope ed834b548f03ea5909ef4c5a1dfbc79ba285953f..188f5b9b397462c23dc31d7fe7d0f23b1e60352d (last reviewed tree 1e330047df), full branch 6b3df9a7ba0d3f36197df1b7ba6b59bf631cf879..188f5b9b397462c23dc31d7fe7d0f23b1e60352d as context
- Source: https://github.com/pydantic/pydantic-ai/tree/stream-event-capture-plugin
- Session: b2893d43-23ea-4f93-a849-d07dce6e229c
2026-11-03 · pre-push review 3 of 3 at 188f5b9b397462c23dc31d7fe7d0f23b1e60352d · iter -
- Decision: remediated, budget exhausted:
comparerefuses a capture with no records; env gate moved intorefactor_capture.register, which inner test sessions call through their conftest (no pragma); module docstring says why no recording applies; rewrapped lines over 120 - Why: tree 6f0e9594f9f5d4a051ccf9ab0f17dc036728059c; follow-up scope ed834b548f..188f5b9b39; review-2 findings 1-3 verified fixed, 4 accepted as documented
- Source: https://github.com/pydantic/pydantic-ai/tree/stream-event-capture-plugin
- Session: b2893d43-23ea-4f93-a849-d07dce6e229c
2026-11-03 · tests/AGENTS.md usage note · iter -
- Decision: Add "Proving a refactor keeps stream events" to
tests/AGENTS.mdverbatim 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.pystays as onmain; pytest loads the plugin only through-p tests.refactor_capture --refactor-capture=DIR, whichmainpasses; theREFACTOR_CAPTURE_DIRenv var is gone - Why: Importing the plugin from conftest tripped CodSpeed's loaded-dependencies guard, and listing it in
affects_benchmarkswould touch CODEOWNERS-owned.github/workflows/; David chose option 1: default and benchmark runs never import the plugin, and--refactor-capturewithout-pfails 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.