perf(server): idle agent sessions free their memory after 2 minutes #41174
main fromrefs/pull/41174/head·merged by juliusmarminge View on GitHubRebuilds #35711 on top of #37585 (merged).
An agent session stayed alive for 30 minutes after its turn ended. Each idle Claude session keeps its CLI process (about 140 MB, measured in #35711). On macOS each one also kept running the ps -A descendant poll while doing nothing.
- Idle sessions are released after 2 minutes when the provider resumes natively (
capabilities.turns.supportsNativeResume: Claude, Codex, OpenCode, Cursor). A follow-up resumes the same provider conversation; the cost is starting the provider process again, about a second. A running turn, pending approvals and questions, background work and delegated children still keep a session alive, as before. - Providers without native resume keep 30 minutes: every ACP-based driver (ACP registry, Grok, Kiro, fx, Antigravity), Pi and Muse. A release would replace their conversation with a summary. #35711 listed four drivers by name; this reads the capability instead, so Antigravity, Pi and Muse, which also lack native resume, keep 30 minutes too.
- The shared Claude poll covers only working CLIs. A CLI joins it at launch, when a prompt is queued and on an assistant message (a turn it starts itself), and leaves at the turn's result after one last read. The read on session close and the one at CLI exit are unchanged, so the cleanup from #34505 still finds a job that left the process group during a turn.
Known gap, as in #35711: a job whose child leaves the CLI's process tree after the turn ended is found only if it is still in the tree at close.
Tests#
- New: a session with native resume is released at 2 minutes, one without at 30 (
ProviderSessionManager.test.ts). The 2-minute case fails on the base branch. - Ported from #35711: a job started during a turn is still stopped when the CLI exits later, and no
psruns between the end of the turn and the exit. Fails on the base branch (one read per second while idle). - Extended: one shared poll reads once for every working CLI; a settled turn reads once more and then drops out of polls.
vp run check:changedpasses.
Measured#
~/mem-bench harness on cups, the same as in #37585: a fake Anthropic API with the real claude 2.1.330 CLI, and 20 Claude threads that each answered once. XDG_RUNTIME_DIR was unset so the server takes the no-agent-scope path a Mac takes. The window is 60 s, measured 20 s after the last turn.
| 20 quiet threads | 0.1.8 | main | #37585 | this PR |
|---|---|---|---|---|
| server CPU | 0.27% | 26.1% | 2.2% | 0.45% |
| server wakeups/s | 6.8 | 208.1 | 30.2 | 10.3 |
| server RSS peak | 471 MB | 615 MB | 527 MB | 517 MB |
| heap after GC | 177 MB | 201 MB | 200 MB | 200.5 MB |
Twelve of the 20 CLIs had already been released when the window ended (2 minutes after their turns); 8 were still running. The CPU and wakeup drop comes from the idle poll, not the releases: none of the 20 was polled after its turn.
Claude Opus 5.5 via Claude Code (T3 Code), from Theo's #35711.
🤖 Generated with Claude Code
Finishing Touches
Generate unit tests (beta)