Repository navigation
feat(agent-server): reclaim Docker runtime caches and disable core dumps - #5263
Conversation
… cache Each Docker conversation keeps a private $HOME/.cache (uv alone is ~1.4 GB) under runtime-data/<id>/persistence, and nothing removed it short of deleting the conversation, so self-hosted boxes filled their disks. - Prune persistence/.cache after a runtime stops (idle eviction or registry.stop) and once at startup, unless the persisted status is RUNNING. The cache is renamed out of the bind mount under the registry lock and deleted afterwards, so a concurrent resume never sees a half-deleted cache. Workspace, history and the manifest are untouched, so resume keeps working. - Add conversation_shared_cache_dir: an existing absolute host directory bind-mounted into every container as UV_CACHE_DIR. Off by default; single-tenant only. Fixes #5260
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
Coverage Report •
|
||||||||||||||||||||||||||||||
…background Review follow-up for #5260. - Drop the persisted-status check from the prune. It only ever runs once the container is gone (stop, idle eviction, or startup after stale containers are removed), so a RUNNING status on disk is stale there and only pinned ~1.4 GB per crashed or force-stopped conversation. Live runs stay protected by idle eviction, which skips RUNNING conversations. - Detach caches synchronously but delete them in background tasks, so startup with a large backlog, DELETE .../runtime and shutdown no longer wait on rmtree. Shutdown cancels pending deletions; the next start sweeps whatever they left behind.
…e dumps Prune persistence/.npm alongside .cache: npm ignores XDG and keeps its cache in ~/.npm, which the uv-only prune never touched (17 GB across 111 runtimes on the OSS VM). The shared cache now sets XDG_CACHE_HOME, which uv, pip and yarn follow, plus npm_config_cache, instead of UV_CACHE_DIR alone. Conversation containers run with --ulimit core=0: crashing processes were writing 1-2 GB core dumps into the bind-mounted workspace.
# Conflicts: # openhands-agent-server/openhands/agent_server/docker_runtime/registry.py
agent_server/storage/ owns reclaiming disk the server stores per conversation, independent of the runtime mode: - Trash: the only delete path. One rename into <root>/.trash, emptied in the background, swept on the next start. Each entry is tried once per run, so an undeletable one (safe_rmtree reports failure instead of raising) no longer spins. - Reclaimer: on_stop / start / shutdown, reclaiming by Tier, with the move done inside the adapter's idle() guard. - StorageAdapter protocol; DockerStorageAdapter implements it for runtime-data/<id>. Its idle() also refuses runtimes being deleted, which the registry's own prune did not check. Caches set aside by earlier builds (.cache-pruned-*) are moved into the trash at startup. Behaviour is otherwise unchanged.
- Final[...] on the registry's and selectors' module constants. - DockerStorageAdapter is a frozen dataclass; no __future__ import, the registry annotation is quoted instead. - Drop the .cache-pruned-* migration and StorageAdapter.leftovers(): only unreleased builds of this branch ever created those dirs. - contextlib.suppress for UUID parsing (scoped to a _parse_id helper) and Trash._entries. - Document what idle() means for a Docker runtime.
…escribe DockerStorageAdapter -> DockerRuntimeStorage and the protocol StorageAdapter -> ReclaimableStorage: they describe a runtime mode's stored conversations for the Reclaimer, which now calls it storage. Per review.
Caches are now dropped whenever a container stops, so sharing them saved little disk while weakening isolation between conversations and adding a single-tenant-only setting nobody uses. Removes conversation_shared_cache_dir and its XDG_CACHE_HOME / npm_config_cache mount. Per review.
There was a problem hiding this comment.
LGTM
Few questions
-
if we run two agent servers on the same machine with shared persistence dir, are we checking which one owns the conversation prior to reclaiming space? maybe we don't need to over engineer here but seems like they'll step over each if that's the case
-
path based cleanup means this won't work for remote sandboxes. i suppose we're ok with that as we assume any remote sandbox should run an agent server which will perform the cleanup there
-
when the trash is shutdown deletion still occurs in a separate thread
await asyncio.to_thread(_remove, entry); maybe we should document that in the docstring more concretely as its not a true deletion interrupt
| inside it are not in use. | ||
| """ | ||
| registry = self.registry | ||
| async with registry._lock: |
There was a problem hiding this comment.
I think we're leaking internal locks in this class
Maybe the registry can expose explicit methods like registry.runtime_idle(conversation_id) that wrap this logic
There was a problem hiding this comment.
Good point, fixed in a4bb58b. The lock logic moved to a public DockerConversationRegistry.runtime_idle(conversation_id) context manager; DockerRuntimeStorage.idle() now only delegates to it, the same way LocalWorktreeStorage.idle() delegates to the service. #5429 still reads registry._service in on_retired; I will give that a public registry method there too.
| try: | ||
| last_active = state.stat().st_mtime | ||
| except OSError: | ||
| last_active = runtime_dir.stat().st_mtime |
There was a problem hiding this comment.
If runtime dir is deleted in the middle of execution could runtime_dir.stat() also throw an error?
There was a problem hiding this comment.
Yes. The base_state.json stat was guarded, but the fallback runtime_dir.stat() was not: a DELETE removing the dir between the is_dir() check and the stat raised FileNotFoundError, and since runtimes() builds the whole list it aborted the pass (and, from Reclaimer.start(), server startup). Fixed in a4bb58b: that runtime is skipped. test_startup_prune_skips_runtime_deleted_mid_listing reproduces the race and fails without the fix.
|
|
||
|
|
||
| class Reclaimer: | ||
| """Frees what a runtime mode stores per conversation, by tier. |
There was a problem hiding this comment.
@OpenHands what is a runtime mode? Investigate the codebase deeply. I thought it would be only docker or local but local is in the same place so I guess only docker, but you need to correct me if it means something else or there are more modes.
There was a problem hiding this comment.
I'm on it! enyst can track my progress at all-hands.dev
There was a problem hiding this comment.
"runtime mode" is the value of Config.conversation_runtime, the Literal["local", "docker"] at openhands-agent-server/openhands/agent_server/config.py:367, selected once in create_conversation_registry() (conversation_registry.py:84). That factory returns the registry — the runtime-mode adapter — that the rest of the server programs against:
conversation_runtime == "docker"→DockerConversationRegistry(a subclass ofConversationRegistry) is the only registry that constructs aReclaimertoday (docker_runtime/registry.py:85), backed byDockerRuntimeStorage, the onlyReclaimableStorageimplementation.- anything else — only
"local"— → the baseConversationRegistry, which has no reclaimer and no storage adapter.
So your guess needs one correction. Local mode is not "in the same place" in the sense this module cares about: a host-local conversation has no per-conversation sandbox $HOME. The agent runs in-process on the host and uses the host's own $HOME/.cache / .npm, so there is nothing per-conversation to reclaim — which is exactly why local has no ReclaimableStorage implementation. The per-conversation storage this module targets (runtime-data/<id>/persistence, the sandbox's own $HOME) exists only in docker mode.
That's also why the abstraction is a Protocol rather than docker-only: the per-conversation things that do pile up in local mode are the git worktrees under conversation_worktree_root, and the follow-up #5433 is the same storage layer for those. So today there are two runtime modes, only one of which has reclaimable per-conversation storage; the generic ReclaimableStorage/Reclaimer shape is there so the layer isn't tied to Docker.
This reply was authored by an AI agent (OpenHands).
There was a problem hiding this comment.
I've investigated the codebase and answered the reviewer's question directly on the thread.
Answer to the comment
I found and confirmed the facts, then posted a reply to enyst on PR #5263 (thread r4166906195):
"Runtime mode" is the value of Config.conversation_runtime — a Literal["local", "docker"] at openhands-agent-server/openhands/agent_server/config.py:367. It's selected exactly once, in create_conversation_registry() (conversation_registry.py:84), which returns the registry (the runtime-mode adapter) and:
"docker"→DockerConversationRegistry, the only registry that constructs aReclaimer(backed byDockerRuntimeStorage, the onlyReclaimableStorageimplementation).- anything else (only
"local") → the baseConversationRegistry, which has no reclaimer and no storage adapter.
The key correction to the reviewer's mental model: local mode is not "in the same place." A host-local conversation runs in-process on the host and uses the host's own $HOME/.cache / .npm, so there's no per-conversation sandbox $HOME to reclaim — which is exactly why local has no ReclaimableStorage implementation. The per-conversation dir this module targets (runtime-data/<id>/persistence) only exists in docker mode.
I also explained why the abstraction is a Protocol rather than docker-specific: the per-conversation things that do pile up in local mode are the git worktrees under conversation_worktree_root, and follow-up #5433 reuses the same ReclaimableStorage/Reclaimer layer for those. So today there are two modes, one of which has reclaimable per-conversation storage, and the generic shape is future-proofing so the layer isn't tied to Docker.
I left the thread unresolved since it's a pure question and the reviewer may want to follow up or confirm before I mark it resolved.
# Conflicts: # openhands-agent-server/openhands/agent_server/docker_runtime/registry.py # tests/agent_server/docker_runtime/test_registry.py
… runtimes deleted mid-listing
|
🧹 PR Artifact Cleanup Queued The |
|
@malhotra5 thanks for the review! On your three questions:
Also merged main (f10ef3e). That picks up #5403, which fixes the |
HUMAN:
Docker runtimes kept every conversation's package caches forever and wrote 1–2 GB core dumps into the workspace, which repeatedly filled the OSS VM's disk. This adds a small storage module that drops those caches when a container stops and blocks core dumps. An earlier version of this ran on the OSS VM and freed about 44 GB.
AGENT:
Stack: #5263 (this) → #5407 → #5429 → #5433
Why
With
conversation_runtime="docker", every conversation keeps its own$HOMEunderruntime-data/<id>/persistence. Package caches pile up there and nothing removes them short of deleting the conversation, which also deletes its history. Measured on the OSS VM:.npmwere 17.3 GB across 111 runtimescorefiles into the workspace (8.2 GB across 8 runtimes)The disk filled and crashed the server seven times between 2026-09-28 and 2026-09-30.
Summary
agent_server/storage/, shared by every runtime mode:Trash: the only delete path. One rename into<root>/.trash, emptied in the background and swept on the next start. Each entry is tried once per run, so an undeletable one cannot spin (safe_rmtreereports failure instead of raising).Tier: what may be dropped.StoredRuntime/ReclaimableStorage: how a runtime mode stores conversations.Reclaimer:start/on_stop/shutdown. It moves files only inside the storage'sidle()guard.DockerRuntimeStorageforruntime-data/<id>. The registry now only callsreclaimer.on_stop()/start()/shutdown().idle()also refuses a runtime that is being deleted; the old prune did not check that.persistence/.cacheandpersistence/.npmare dropped when a container stops (idle eviction,DELETE …/runtime, shutdown), and for every runtime at startup. The workspace (including.venv), the rest ofpersistence/, the manifest and the conversation history are untouched, so resume keeps working.--ulimit core=0on every conversation container.The follow-ups build on this: dependencies under a disk budget (#5407), retiring old runtimes (#5429), and the same storage layer for host-local worktrees (#5433).
Issue Number
#5260
How to Test
The 7 failures are the pre-existing
canvas_extensions/test_canvas_extension_backend.pyones (macOS), identical without this change, diffed by test id.The tests run on a real filesystem.
tests/agent_server/storage/test_reclaimer.pydrives the reclaimer through a minimal fake storage, showing the core has no Docker knowledge. Mutation-checked, each makes its test fail when removed:_deletingcheckEnd to end against real Docker (Docker 29.2.1,
ghcr.io/openhands/agent-server:1.44.1-python), driving the realDockerConversationRegistry: realdocker run,/health, and uv inside the container. The same result on the top of the stack (#5433).Script
On the OSS VM (the pre-refactor implementation of the same behaviour,
72f6942): the first startup freed ~26 GB of uv caches and the next one 18 GB of npm caches; 108 idle stops pruned with 0 failures overnight; no core dump has been written since. The refactored code is not on the VM yet.Video/Screenshots
N/A (server-side).
Type
Notes
$HOME/.cacheand$HOME/.npmonly hold rebuildable package caches.🐳 Agent Server images for this PR — GHCR package, pull/run commands, and all pushed tags (click to expand)
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdkpython-node-runtimepython-node-runtimepython-node-runtimegolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:5e1ddaa-pythonRun
All tags pushed for this build
About Multi-Architecture Support
5e1ddaa-python) is a multi-arch manifest supporting both amd64 and arm645e1ddaa-python-amd64) are also available if needed