Skip to content

fix(session): read opencode session list from SQLite, not subprocess - #905

Merged
njbrake merged 1 commit into
mainfrom
memleak
May 5, 2026
Merged

njbrake merged 1 commit into
mainfrom
memleak

Conversation

@njbrake

@njbrake njbrake commented May 5, 2026

Copy link
Copy Markdown
Collaborator

Note: this PR was drafted by Claude via back-and-forth with @njbrake. The reasoning and decisions are his; the prose is Claude's.

Description

Fixes #904. Memory leak under aoe non-sandbox mode with opencode: ~4.5MB files like /tmp/.f9ef7ff75feb77fe-00000000.so accumulate over time. At 60s steady-state polling that's ~270MB/hour per active session, ~2GB in an 8-hour day.

Root cause (verified empirically). opencode is built with bun --compile and bundles the OpenTUI Zig native renderer as an embedded bun:ffi library. Bun extracts that library to /tmp/.<hash>-NNNNNNNN.so on every process startup and never unlinks it on exit. Every invocation of every opencode subcommand (--version, --help, session list) leaks exactly one tempfile. This is a known, currently unresolved upstream bug: anomalyco/opencode#6523, anomalyco/opencode#16996, anomalyco/opencode#13479.

Why aoe and not other agents. claude is also Bun-compiled but ships no embedded FFI native lib, so its subcommands leak zero. opencode is the only aoe-supported tool that embeds a Bun-FFI native lib that the runtime extracts on startup.

Why aoe and not opencode-by-itself. aoe's session-resume poller (src/session/capture.rs:try_capture_opencode_session_id, src/session/poller.rs:48-51) spawns opencode session list --format json every poll tick. Steady-state interval is 60s. Each poll spawns a fresh subprocess, each subprocess leaks one ~4.5MB file.

Why sandbox mode is unaffected. The sandbox path routes through docker exec opencode session list; leaks land in the container's ephemeral /tmp and disappear with the container.

The fix. Read opencode's local SQLite store at $XDG_DATA_HOME/opencode/opencode.db directly. The session table exposes id, directory, time_updated, which is exactly what select_opencode_session needs. Same row shape (synthesized to match the CLI JSON), same filtering pipeline, no subprocess, no /tmp pressure, ~100x faster per poll.

The fallback to the subprocess path is preserved, but only fires on infrastructure errors (DB missing, locked, schema drift); a readable DB with no matching session is authoritative and does not fall back, because the subprocess would just leak for the same empty answer.

Sandbox path (try_capture_opencode_session_id_in_container) is intentionally unchanged.

PR Type

  • New Feature
  • Bug Fix
  • Refactor
  • Documentation
  • Infrastructure / CI

Checklist

  • I understand the code I am submitting
  • New and existing tests pass
  • Documentation was updated where necessary
  • For UI changes: included screenshot or recording

How tested

  • 6 new unit tests in src/session/capture.rs covering: most-recent match, exclusion set, launch-time floor, missing DB, schema mismatch, and the critical "readable DB with no match must NOT fall back" contract.
  • cargo fmt --check, cargo clippy --all-targets -D warnings, cargo test --lib all pass locally.
  • Verified empirically against the real opencode binary that the SQLite path leaks zero /tmp/.<hash>-*.so files across 60s of real SessionPoller polling and 3 minutes of full aoe TUI runtime, where the unfixed subprocess path leaks 1:1 with invocations.

Tradeoffs

  • Adds rusqlite = { version = "0.32", features = ["bundled"] }. bundled ships its own libsqlite3, no new system dependency. Adds roughly 250KB to the release binary.
  • Couples to opencode's current session table schema. If opencode renames or drops the columns we read, we degrade to the subprocess path with a one-shot tracing::warn! log; we do not panic.

AI Usage

  • No AI was used
  • AI was used for drafting/refactoring
  • This is fully AI-generated

AI Model/Tool used: Claude Code (Opus 4.7, 1M context)

Any Additional AI Details you'd like to share:
Used Claude Code to investigate the issue, locate the upstream root cause, design the fix, and write the implementation and tests. Schema decisions, fallback semantics (specifically the "no match is authoritative" contract), the choice to leave sandbox mode unchanged, and the bundled-rusqlite tradeoff were all reviewed and approved by me before the code was written.

  • I am an AI Agent filling out this form (check box if true)

opencode is built with bun --compile and bundles the OpenTUI Zig native
renderer as an embedded bun:ffi library. Bun extracts that library to
/tmp/.<hash>-NNNNNNNN.so on every process startup and never unlinks it
on exit. aoe's 60s session-resume poller spawned `opencode session list`
each tick, which turned the per-process leak into unbounded growth
(~270MB/hour per active session, ~2GB in an 8-hour day).

Read opencode's local SQLite store at $XDG_DATA_HOME/opencode/opencode.db
directly. Same row shape, same filtering pipeline, no subprocess, no
/tmp pressure. Fall back to the original subprocess path only when the
DB is genuinely unreadable (missing, locked, schema drift); a readable
DB with no matching session is authoritative and does not fall back,
since the subprocess would just leak for the same empty answer.

Sandbox mode is unchanged. Container /tmp is ephemeral, so the leak is
invisible to users there, and shelling sqlite3 into the container would
be brittle.

Fixes #904
Refs anomalyco/opencode#6523, anomalyco/opencode#16996

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@njbrake
njbrake merged commit 67624b8 into main May 5, 2026
13 checks passed
@njbrake
njbrake deleted the memleak branch May 5, 2026 01:13
Comment thread Cargo.toml
# system libsqlite3 dependency. Used to read session state without spawning
# `opencode session list`, which leaks a 4.5MB /tmp/.<hash>.so per invocation
# via opencode's bun:ffi OpenTUI extract path (see anomalyco/opencode#6523).
rusqlite = { version = "0.32", features = ["bundled"] }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hermes can also benefit from it instead of using sqlite3 CLI.

njbrake pushed a commit that referenced this pull request May 5, 2026
#908)

* fix(session): read Hermes sessions via rusqlite instead of sqlite3 CLI

Replace the subprocess call to sqlite3 with a direct rusqlite read,
matching the pattern established for opencode in PR #905. This removes
the last consumer of the sqlite3 CLI binary in the codebase, so drop
it from CI dependencies as well.

The container path (python3 -c via docker exec) is unchanged since
python3 is guaranteed by Hermes's runtime.

Closes #906

* fix(session): propagate row errors instead of silently dropping

Align with opencode pattern: use explicit error propagation per row
instead of filter_map(.ok()).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory leak that seems to be specific to opencode in aoe

2 participants