Return the memory freed by loading saved tasks to the OS when `clai2` opens its task owner

Merged
DO
17d ago
+46-3

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
main595 MB
this PR386 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_users checks 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_trimmed checks that a C library without malloc_trim and 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 -

2026-11-12 · pre-push review 2 of 3 at c497aadad9 · iter -

2026-11-12 · pre-push review 2 of 3 at c497aadad9 · 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.

🤖 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.