Repository navigation
Conversation
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]>
| # 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"] } |
Collaborator
There was a problem hiding this comment.
hermes can also benefit from it instead of using sqlite3 CLI.
6 of 13 tasks
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()).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #904. Memory leak under aoe non-sandbox mode with opencode: ~4.5MB files like
/tmp/.f9ef7ff75feb77fe-00000000.soaccumulate 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 --compileand bundles the OpenTUI Zig native renderer as an embeddedbun:ffilibrary. Bun extracts that library to/tmp/.<hash>-NNNNNNNN.soon 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.
claudeis 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) spawnsopencode session list --format jsonevery 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/tmpand disappear with the container.The fix. Read opencode's local SQLite store at
$XDG_DATA_HOME/opencode/opencode.dbdirectly. Thesessiontable exposesid,directory,time_updated, which is exactly whatselect_opencode_sessionneeds. Same row shape (synthesized to match the CLI JSON), same filtering pipeline, no subprocess, no/tmppressure, ~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
Checklist
How tested
src/session/capture.rscovering: 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 --liball pass locally./tmp/.<hash>-*.sofiles across 60s of realSessionPollerpolling and 3 minutes of fullaoeTUI runtime, where the unfixed subprocess path leaks 1:1 with invocations.Tradeoffs
rusqlite = { version = "0.32", features = ["bundled"] }.bundledships its own libsqlite3, no new system dependency. Adds roughly 250KB to the release binary.sessiontable schema. If opencode renames or drops the columns we read, we degrade to the subprocess path with a one-shottracing::warn!log; we do not panic.AI Usage
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.