Fix flaky `SystemReminders` caplog tests that counted other loggers' records

Merged
DS
1mo ago
+13-8

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 asyncio Task was destroyed but it is pending! record. On main, 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.py passes 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 -

2026-10-29 · pre-push review 1 of 3 at 76cba8f130dea2b39668b27d3ff159d756d1816a · iter -

2026-10-29 · pre-push review 1 of 3 at 76cba8f130dea2b39668b27d3ff159d756d1816a · iter -

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 -

2026-10-29 · pre-push review 2 of 3 at 2d1cbc199b21aadde38c204125c649a203e01d04 · iter -

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 -

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.