DISCLAIMER: This fork contains experimental, AI-assisted modifications ("AI slop") created in an attempt to keep long-running sessions from crashing due to memory exhaustion.
- Not a Complete Solution: This is not a proper architectural fix for the memory leak issues present in OpenCode. It addresses only approximately 20% of the observable leak surface by applying defensive bounds to queues and in-memory caches.
- LLM Usage Acknowledgment: I am fully aware that project maintainers have explicitly requested contributors not to submit LLM-generated code or fixes. This repository exists strictly as a personal workaround to keep the tool operational on limited-resource machines, not as an official upstream recommendation or PR candidate.
- Unresolved Root Causes: Deep runtime leaks—including EventTarget listener accumulation inside the bundled AI-SDK Effect runtime, Bun libuv conversion overhead, and V8 heap retention under heavy subagent workloads—are not fixed by these changes.
-
LSP Diagnostic and File Buffers (
packages/opencode/src/lsp/client.ts)- Implemented a
BoundedMapstructure with LRU key eviction. - Restricted diagnostic state retention to
MAX_DIAGNOSTICS_FILES = 5000. - Restricted open document text cache to
MAX_OPEN_FILES = 1000.
- Implemented a
-
SSE Event Stream Buffer Bounding (
packages/opencode/src/server/routes/instance/httpapi/handlers/event.ts)- Replaced unbounded event queue allocation (
Queue.unbounded<EventV2.Payload>()) with a sliding buffer (Queue.sliding<EventV2.Payload>(1000)). - Drops stale progress events under severe backpressure to prevent workers from growing to 9+ GB RSS when clients disconnect or process events slowly (references upstream issue #31087).
- Replaced unbounded event queue allocation (
-
Instance State Cache Bounding (
packages/opencode/src/effect/instance-state.ts)- Capped the per-directory active instance cache at
capacity: 100instead ofNumber.POSITIVE_INFINITY. Idle entries are evicted and re-instantiated on demand.
- Capped the per-directory active instance cache at
-
TUI and Event Queue Clamping (
packages/opencode/src/server/shared/tui-control.ts,packages/opencode/src/util/queue.ts)- Bounded internal
AsyncQueueallocations to 1,000 items with shift-based eviction on overflow.
- Bounded internal
For the official OpenCode repository, documentation, and installation instructions, visit: https://github.com/anomalyco/opencode