[PR #6456] fix: prevent unbounded memory growth in long-running sessions #11914

Closed
opened 2026-02-16 18:16:51 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/6456

State: closed
Merged: No


Summary

  • Fix multiple sources of memory accumulation causing infinite growth in long-running OpenCode sessions
  • Add configurable limits and automatic cleanup mechanisms across 5 subsystems
  • Maintain full backward compatibility with existing behavior

Problem

During extended sessions (hundreds of tool calls), OpenCode accumulates memory without bounds due to:

  1. Full message history materialized every loop iteration
  2. FileTime read cache growing indefinitely per session
  3. Tool outputs and attachments retained even after compaction
  4. Instance cache never releasing idle projects
  5. LSP diagnostics accumulating for all opened files

Solution

Phase 1: Quick Wins (Low Risk)

Change File Impact
Skip attachments for compacted parts message-v2.ts Reduces model message size
Cap queued callbacks at 10 prompt.ts Prevents callback accumulation
LRU cache with 500-entry limit time.ts Bounds file tracking memory
Memory config options config.ts Makes limits configurable

Phase 2: Structural Improvements (Medium Risk)

Change File Impact
2000 message safety limit message-v2.ts Hard cap on history size
30-minute idle timeout instance.ts Auto-cleanup idle instances
1000-file diagnostics limit client.ts Bounds LSP cache size

Testing

  • Added test/file/time.test.ts with 4 unit tests for LRU eviction
  • All 371 existing tests pass
  • Typechecks pass across all 12 packages

Files Changed

  • packages/opencode/src/file/time.ts - LRU cache implementation
  • packages/opencode/src/session/prompt.ts - Callback limits
  • packages/opencode/src/session/message-v2.ts - Attachment skipping & message limit
  • packages/opencode/src/project/instance.ts - Idle timeout cleanup
  • packages/opencode/src/lsp/client.ts - Diagnostics cache limit
  • packages/opencode/src/config/config.ts - Memory config schema
  • packages/opencode/test/file/time.test.ts - New tests (created)

Config Options Added

memory: {
  fileCacheLimit: 500,        // Max file entries per session
  maxQueuedCallbacks: 10      // Max queued loop callbacks
}

Breaking Changes

None - all changes are additive with sensible defaults matching previous implicit behavior.

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/6456 **State:** closed **Merged:** No --- ## Summary - Fix multiple sources of memory accumulation causing infinite growth in long-running OpenCode sessions - Add configurable limits and automatic cleanup mechanisms across 5 subsystems - Maintain full backward compatibility with existing behavior ## Problem During extended sessions (hundreds of tool calls), OpenCode accumulates memory without bounds due to: 1. Full message history materialized every loop iteration 2. FileTime read cache growing indefinitely per session 3. Tool outputs and attachments retained even after compaction 4. Instance cache never releasing idle projects 5. LSP diagnostics accumulating for all opened files ## Solution ### Phase 1: Quick Wins (Low Risk) | Change | File | Impact | |--------|------|--------| | Skip attachments for compacted parts | `message-v2.ts` | Reduces model message size | | Cap queued callbacks at 10 | `prompt.ts` | Prevents callback accumulation | | LRU cache with 500-entry limit | `time.ts` | Bounds file tracking memory | | Memory config options | `config.ts` | Makes limits configurable | ### Phase 2: Structural Improvements (Medium Risk) | Change | File | Impact | |--------|------|--------| | 2000 message safety limit | `message-v2.ts` | Hard cap on history size | | 30-minute idle timeout | `instance.ts` | Auto-cleanup idle instances | | 1000-file diagnostics limit | `client.ts` | Bounds LSP cache size | ## Testing - Added `test/file/time.test.ts` with 4 unit tests for LRU eviction - All 371 existing tests pass - Typechecks pass across all 12 packages ## Files Changed - `packages/opencode/src/file/time.ts` - LRU cache implementation - `packages/opencode/src/session/prompt.ts` - Callback limits - `packages/opencode/src/session/message-v2.ts` - Attachment skipping & message limit - `packages/opencode/src/project/instance.ts` - Idle timeout cleanup - `packages/opencode/src/lsp/client.ts` - Diagnostics cache limit - `packages/opencode/src/config/config.ts` - Memory config schema - `packages/opencode/test/file/time.test.ts` - New tests (created) ## Config Options Added ```typescript memory: { fileCacheLimit: 500, // Max file entries per session maxQueuedCallbacks: 10 // Max queued loop callbacks } ``` ## Breaking Changes None - all changes are additive with sensible defaults matching previous implicit behavior.
yindo added the pull-request label 2026-02-16 18:16:51 -05:00
yindo closed this issue 2026-02-16 18:16:51 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#11914