Fix flaky `SystemReminders` caplog tests that counted other loggers' records
This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.
No issue: this fixes a flake that removed #10489 from the merge queue (job log).
TestLLMReminder::test_remembers_a_bounded_number_of_runs failed assert len(caplog.records) == 7 with assert 9 == 7. The two extra records were ERROR asyncio Task was destroyed but it is pending!. They came from tasks that another test on the same xdist worker leaked, and that were garbage-collected while this test ran. caplog.at_level(..., logger='pydantic_ai_harness.system_reminders') only sets the level. caplog.records still holds every logger's records, so any record from an unrelated logger during the call phase breaks the count.
The fix is test-only. A new _reminder_records(caplog) helper keeps only records from the pydantic_ai_harness.system_reminders logger tree, which is the logger the tests already pass to caplog.at_level. tests/clai2/test_sessions.py filters caplog.records by record.name the same way. All eight caplog assertions in the file that read every record now use the helper. I used a prefix instead of the exact module name so the two "no warning" assertions (== []) still catch a warning if the logger moves within the package.
New public surface: none.
Verification (local):
- To make the flake deterministic, I used a throwaway pytest plugin (not committed). In every test that takes
caplog, it creates a task on a fresh event loop, closes the loop, and garbage-collects the task during the call phase. That emits the real asyncioTask was destroyed but it is pending!record. Onmain, all eight caplog tests fail (too many values to unpack,assert 5 == 4,assert 8 == 7,Left contains one more item). With this change, all eight pass and still capture the asyncio record. tests/harness/system_reminders/test_system_reminders.pypasses without the injection.
What changes for existing users: nothing.
The leaked tasks in the CI log are _run_durable_on_agent_loop.<locals>.run() and _AgentLoop.retire.<locals>.arrange_shutdown.<locals>.clean_up() in pydantic_ai_harness/aws_lambda/_bridge.py. This PR does not change them.
PR decisions
2026-10-29 · filter caplog by exact module logger name · iter -
- Decision: Helper _reminder_records filters caplog.records by record.name == 'pydantic_ai_harness.system_reminders._capability'; all 8 all-records assertions use it; the now-tautological record.name assert is dropped
- Why: Only repo precedent for logger-filtering caplog is the exact record.name comparison in tests/clai2/test_sessions.py; _capability is the package's only logger
- Source: https://github.com/pydantic/pydantic-ai/actions/runs/40924710275/job/123627142729
- Session: 08ec0097-2b58-4258-a652-f497dc9a7b12
2026-10-29 · foreign-logger injection not committed · iter -
- Decision: Repro uses a scratch pytest plugin that GCs a real pending asyncio task in each caplog test's call phase; it stays out of the commit
- Why: No repo precedent for committing a log-injection regression test (searched tests/ for foreign-logger emitters)
- Source: https://github.com/pydantic/pydantic-ai/actions/runs/40924710275/job/123627142729
- Session: 08ec0097-2b58-4258-a652-f497dc9a7b12
2026-10-29 · pre-push review 1 of 3 at 76cba8f130dea2b39668b27d3ff159d756d1816a · iter -
- Decision: reserved: dispatching
- Why: tree 9d42deda85b4dda0034bae3ad6ecb2867022332c; scope 3809c1b861036197de4a06ae3ee38e27e5455100..76cba8f130dea2b39668b27d3ff159d756d1816a (full branch); first review
- Source: https://github.com/pydantic/pydantic-ai/tree/fix-system-reminders-caplog-flake
- Session: 08ec0097-2b58-4258-a652-f497dc9a7b12
2026-10-29 · pre-push review 1 of 3 at 76cba8f130dea2b39668b27d3ff159d756d1816a · iter -
- Decision: remediated: exact-name filter made the two == [] no-warning assertions vacuous if the logger is renamed; filter now matches the package logger tree
- Why: reviewed tree 9d42deda85b4dda0034bae3ad6ecb2867022332c; scope 3809c1b861036197de4a06ae3ee38e27e5455100..76cba8f130dea2b39668b27d3ff159d756d1816a
- Source: https://github.com/pydantic/pydantic-ai/tree/fix-system-reminders-caplog-flake
- Session: 08ec0097-2b58-4258-a652-f497dc9a7b12
2026-10-29 · filter caplog by the package logger prefix · iter -
- Decision: _reminder_records keeps records whose name starts with 'pydantic_ai_harness.system_reminders', the logger caplog.at_level targets; the exact record.name assert stays
- Why: An exact module-name filter lets the == [] assertions pass vacuously after a logger rename; importing _capability for its logger would break the tests/harness no-private-imports rule
- Source: https://github.com/pydantic/pydantic-ai/tree/fix-system-reminders-caplog-flake
- Session: 08ec0097-2b58-4258-a652-f497dc9a7b12
- Supersedes: filter caplog by exact module logger name
2026-10-29 · pre-push review 2 of 3 at 2d1cbc199b21aadde38c204125c649a203e01d04 · iter -
- Decision: reserved: dispatching
- Why: tree 5b4b7cb0ed4c6528af6341c1be25e747fc451272; follow-up scope 76cba8f130dea2b39668b27d3ff159d756d1816a..2d1cbc199b21aadde38c204125c649a203e01d04; verifies review 1 finding
- Source: https://github.com/pydantic/pydantic-ai/tree/fix-system-reminders-caplog-flake
- Session: 08ec0097-2b58-4258-a652-f497dc9a7b12
2026-10-29 · pre-push review 2 of 3 at 2d1cbc199b21aadde38c204125c649a203e01d04 · iter -
- Decision: dismissed with evidence: rename-vacuity finding; no further review on unchanged tree
- Why: reviewed tree 5b4b7cb0ed4c6528af6341c1be25e747fc451272; scope 76cba8f..2d1cbc1; probe renaming the logger outside the tree fails 6 of 8 caplog tests at the shared helper, so the two == [] checks cannot go vacuous silently
- Source: https://github.com/pydantic/pydantic-ai/tree/fix-system-reminders-caplog-flake
- Session: 08ec0097-2b58-4258-a652-f497dc9a7b12
2026-10-29 · keep the package-prefix literal, no private-module import · iter -
- Decision: Filter and caplog.at_level keep the 'pydantic_ai_harness.system_reminders' literal rather than importing _capability for name
- Why: A rename outside the tree fails the 6 positive caplog tests through the same helper (probe: 6 failed, 2 passed), so the gap surfaces; switching would widen the diff to all 8 at_level lines plus a private-module import
- Source: https://github.com/pydantic/pydantic-ai/tree/fix-system-reminders-caplog-flake
- Session: 08ec0097-2b58-4258-a652-f497dc9a7b12
2026-10-29 · pushed 84ebce92624ef16c17de751a8f26b1c780e7ed5f · iter -
- Decision: Commit-message reword of 2d1cbc1; same tree 5b4b7cb0ed4c6528af6341c1be25e747fc451272, so the review 2 verdict carries
- Why: Tree equality to the reviewed anchor 2d1cbc199b21aadde38c204125c649a203e01d04
- Source: https://github.com/pydantic/pydantic-ai/tree/fix-system-reminders-caplog-flake
- Session: 08ec0097-2b58-4258-a652-f497dc9a7b12
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.