Return the memory freed by loading saved tasks to the OS when `clai2` opens its task owner
No linked issue: follow-up to #13221, from an audit of clai2's memory use.
Why we make these changes
When clai2 opens its sub-agent task owner, it parses every saved task's file in full and then drops each settled task's history (#13221). glibc keeps the memory it freed for reuse instead of returning it to the OS. On a real 1.3 GB task directory, a resumed session sat at 595 MB RSS, and about 200 MB of that was freed but retained. TaskRouter.opened now calls malloc_trim(0) once after the owner opens.
This is the interim fix. The follow-up change gives each conversation its own task directory, so startup stops parsing other conversations' tasks at all.
New public surface
None.
User-visible behavior
Same resume of a real session, measured in a scratch process with copies of the maintainer's sessions.db and task directory:
| RSS after resume | |
|---|---|
main | 595 MB |
| this PR | 386 MB |
The trim runs only on Linux, and only where the C library has malloc_trim: on musl it is skipped, and other platforms' allocators already return freed memory or have no such call.
Verification
test_the_owner_opens_once_for_overlapping_userschecks that each open of the owner trims once and that a second session joining an already open owner doesn't.test_the_owner_opens_where_memory_cannot_be_trimmedchecks that a C library withoutmalloc_trimand a platform other than Linux open the owner as usual.
What changes for existing users
clai2 sessions use less memory after startup. Nothing else changes.
PR decisions
2026-11-12 · pre-push review 1 of 3 at e84e8887b3dfbf923edfb5eef611a9b97acd91bb · iter -
- Decision: remediated: inlined the single-use trim helper into TaskRouter.opened, tested through the router, FILE_MAP row updated
- Why: reviewed tree 0d229732b7; scope b8e111d1fa..e84e8887b3 (full branch); first review
- Source: https://github.com/pydantic/pydantic-ai/tree/clai2-trim-after-task-load
- Session: 3c952642-1824-455b-9e24-2786bcd67c8d
2026-11-12 · pre-push review 2 of 3 at c497aadad9 · iter -
- Decision: dispatched
- Why: follow-up scope e84e8887b3..c497aadad9
- Source: https://github.com/pydantic/pydantic-ai/tree/clai2-trim-after-task-load
- Session: 3c952642-1824-455b-9e24-2786bcd67c8d
2026-11-12 · pre-push review 2 of 3 at c497aadad9 · iter -
- Decision: no blocking findings; applied two test-only nits (comment wording, parametrized expectation)
- Why: reviewed tree 68f9d4efab; scope e84e8887b3..c497aadad9; later delta is test code only (exempt, chains from c497aadad9)
- Source: https://github.com/pydantic/pydantic-ai/tree/clai2-trim-after-task-load
- Session: 3c952642-1824-455b-9e24-2786bcd67c8d
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.
🤖 Generated with Claude Code
Summary by cubic
Returns the memory freed by loading saved tasks to the OS so resumed clai2 sessions use less memory.
When the task owner opens, parsing each saved task's full history and dropping the settled ones left glibc retaining roughly 200 MB of freed memory. TaskRouter.opened now calls malloc_trim(0) once after the owner opens, cutting RSS after resume from 595 MB to 386 MB on a real 1.3 GB task directory. The trim runs only on Linux with a C library that has malloc_trim; musl skips it, and other platforms' allocators already return freed memory or have no such call. The tests cover trimming once per owner open and owners opening normally where trimming isn't possible.