Skip to content

Memory stays unbounded: ACP session map + compacted tool outputs #4315

@riatzukiza

Description

@riatzukiza

Description

Two separate memory leaks keep OpenCode RSS growing without bound:

  1. ACP session manager never frees entries. ACPSessionManager (packages/opencode/src/acp/session.ts:9-72) pushes every new ACP session into a private Map and there is no delete or lifecycle hook tied to cancel. Each ACP IDE reconnect adds another entry plus async subscriptions from setupEventSubscriptions (acp/agent.ts:62-335). The repro script packages/opencode/script/memory/acp-session-leak.ts shows RSS climbing from ~80 MB to ~96 MB after 20k synthetic sessions even with forced GC; heap snapshots (logs/acp-session-leak-*.heapsnapshot) retain all session objects.

  2. Tool compaction does not clear payloads. SessionCompaction.prune only stamps part.state.time.compacted, but the underlying state.output / attachments stay in storage. Every SessionPrompt.prompt reloads the full tool history via MessageV2.stream()/toModelMessage, so sessions with thousands of big tool outputs are re-materialized on every turn. Users report >10 GB RSS after a day because "cleared" tool results are still parsed into JS objects.

Both leaks are captured in spec/memory-leaks.md (benchmark table + repro steps). We need lifecycle cleanup for ACP sessions/subscriptions and real data eviction when compaction marks tool outputs as old.

OpenCode version

v1.0.65

Steps to reproduce

  1. ACP leak: NODE_OPTIONS=--expose-gc npx tsx packages/opencode/script/memory/acp-session-leak.ts → observe RSS + session map size continuing to grow; inspect logs/acp-session-leak-*.heapsnapshot in Chrome DevTools.
  2. Tool-output leak: run a long chat session that repeatedly invokes read/edit/bash on large files (or script fake tool parts) and watch process.memoryUsage() / heap snapshots. Even after compaction UI shows "Old tool result content cleared", the backing JSON still contains every output string which gets reloaded on each prompt.

Screenshot and/or share link

N/A

Operating System

Linux (container)

Terminal

tmux + bash inside Codex CLI

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions