anomalyco / anomalyco/opencode

[Bug]: sustained high CPU and memory growth in long-running web sessions with large SQLite history

Open
#46,341 2 comments 0 reactions 1 assignee View on GitHub

@nexxeln is already working on this.

Since Aug 31, 2026.

Dominant language
TypeScript
Stars
209k
Forks
27.5k
PR merge metrics
PR metrics pending

Description

Description

A long-running opencode web process can sustain very high CPU and memory usage while an agent session is active. The behavior appears to worsen as session history and SQLite event data grow.

This is not only a UI rendering issue. The backend appears to repeatedly process and persist large session/event data during agent iterations.

Observed behavior

  • OpenCode version: 1.18.15
  • Command: opencode web
  • Process lifetime: more than 2 days
  • CPU usage: approximately 180–210% sustained
  • RSS: approximately 1.5–1.7 GB
  • Physical memory footprint: tens of GB
  • SQLite database: approximately 66–71 GB
  • The process was actively running an agent loop involving tool calls and other background work.

After stopping the process and inspecting the database:

  • The event table occupied approximately 39.7 GB.
  • Approximately 2,142 event rows were associated with one aggregate.
  • Deleting sessions did not remove all associated event rows.
  • Orphaned event rows remained because event records were not fully tied to session deletion.
  • After removing orphaned events and vacuuming the database, the database reduced to approximately 248 KB.

Expected behavior

Opening or continuing a session should load only:

  • Session metadata.
  • The visible/recent message window.
  • A bounded summary/context window.

Older messages, tool outputs, attachments, and event history should be loaded lazily only when explicitly requested.

CPU and memory usage should remain bounded as session history grows. Large historical data should not be repeatedly hydrated or serialized during every agent iteration.

Suspected causes

These are hypotheses requiring profiling:

  1. Full or oversized session history is hydrated during prompt-loop iterations or compaction.
  2. Large tool outputs and serialized event payloads are retained inline in SQLite.
  3. Event records are written at high frequency during tool execution.
  4. Session deletion does not transactionally remove all related event records.
  5. Large SQLite pages/cache/mmap activity amplifies memory pressure and serialization cost.

Suggested fixes

  • Hydrate only the recent/required context for prompt execution.
  • Lazy-load historical messages and collapsed tool output.
  • Bound tool-output and event-payload sizes.
  • Store large historical output externally or in compressed cold storage.
  • Add transactional session/event deletion with orphan-event cleanup.
  • Prevent deletion of active sessions.
  • Add storage quotas and backpressure.
  • Add profiling/telemetry for prompt hydration, event serialization, SQLite writes, and cache usage.
  • Add a long-running soak test covering large tool output, compaction, child sessions, reconnects, and pruning.

Related issues

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.