Description
I tweeted Dax the issue on twitter earlier but didnt get a reply. I managed to half solve the issue in my opencode installation but it is not truly solved. here is the diagnosis made by my agent.
OpenCode v2: session DB accumulates huge free-page bloat after upgrade (WSL2 RAM blow-up)
TL;DR
After upgrading from OpenCode 1.19.5 → 2.x, the global session database
(~/.local/share/opencode/opencode.db) grew to 5.86 GiB, of which
~4.33 GiB (69%) was free pages (freelist_count = 1,060,053 of
page_count = 1,536,800). The DB is in auto_vacuum=0 mode and the v2
migration/prune paths never run VACUUM (SQLite's built-in file-compaction
command), so the file never shrank.
Result: starting the CLI in WSL2 jumped the VM from ~2 GB to ~7 GB (its memory
cap). Native Windows with a small DB was unaffected. VACUUM INTO reduced the
file to 1.81 GiB with no data loss.
Important: the manual VACUUM only fixes the current file. Because the
application never compacts automatically, a future migration, prune, or usage
peak can raise the high-water mark and reproduce the same problem. This needs an
application-level fix (or periodic local maintenance), not just a one-time
cleanup.
Environment
- OpenCode CLI: log first shows 2.2.7 on 2026-11-11; later 2.2.5 / 2.3.0.
Previously 1.19.5.
- OS: WSL2 Ubuntu (ext4), Windows 11 host with 15.6 GB RAM.
- WSL2 total memory during the issue: 7.5 GiB (default ~50% of host).
- Desktop app (separate install, same Windows user): 1.14.48, DB = 9.5 MB;
no issue.
Symptom
Opening opencode in WSL caused WSL RAM to jump from ~2 GB to ~7 GB
immediately, in every repository. The DB is global, so the repo did not matter.
With ten windows open it was previously light; the change happened around
2026-11-11.
An initial red herring: the umbrella workspace contained ~625k files
(node_modules, venvs, site-packages). That contributes file-watcher/IO
pressure but was not the 6 GB.
Root cause
The v2 SQLite session database had grown to 5.86 GiB, but only ~1.8 GiB was
live data. The rest was free pages left behind by v2 schema migrations and the
7-day session prune, with no follow-up VACUUM.
Database stats (before compaction)
size_bytes 6,294,732,800 (5.86 GiB)
page_size 4096
page_count 1,536,800
freelist_count 1,060,053 (~4.33 GiB free = 69%)
auto_vacuum 0
journal_mode wal
Live data was approximately 1.8 GiB:
session_message 0.73 GB
part 0.69 GB
message 0.35 GB
Row counts (original and compacted DB are identical):
session 251
session_v2 299
session_message 39,715
message 33,156
part 138,830
Why this happened on upgrade
The migration table shows a v2 schema batch completed at
2026-11-11T14:49:05Z, which is exactly when the log first shows
cli starting version=2.2.7. The batch included destructive / table-rebuild
migrations such as:
20261024175301_drop_session_allowance — ALTER TABLE session_v2 DROP COLUMN
20261025123711_blob — new blob / blob_ref
20261025154125_drop_session_pending — DROP TABLE session_pending
20261029002625_session_part — new session_part
20261029073502_session_time_busy
20261029165540_schedule_checks
20261030054400_job — creates job, migrates rows out of kv, then
DELETE FROM kv
Earlier v2 migrations are even more destructive, e.g.:
DELETE FROM session_message; DELETE FROM event; DELETE FROM event_sequence;
DROP TABLE permission; / recreate
DROP TABLE credential; / recreate
- rebuild
workspace / project_directory through __new_* + DROP + RENAME
In addition, the log shows a retention job running repeatedly from Nov 11:
message=cleanup prune=7.days
Both migration drops/rebuilds and the prune delete many pages. SQLite does not
shrink the file when rows are deleted or tables are dropped when
auto_vacuum=0. The file stays at its high-water mark.
The missing maintenance step
The compiled connection setup in @opencode/storage/Database sets:
PRAGMA journal_mode = WAL
PRAGMA synchronous = NORMAL
PRAGMA busy_timeout = 5000
PRAGMA cache_size = -64000
PRAGMA wal_checkpoint(PASSIVE)
It does not set auto_vacuum, and the migration runner applies migrations
inside transactions but never runs VACUUM / PRAGMA incremental_vacuum after
them. The 7-day prune likewise deletes rows without compacting.
So the design is internally inconsistent: the app intentionally prunes data to
save space, but the file can never shrink.
Why it looked like a sudden regression
Timeline from the log:
- 2026-10-30 — CLI on 1.19.5.
- 2026-11-11 14:49:05 — first
cli starting version=2.2.7; the v2 migration
batch completed at the same timestamp; cleanup prune=7.days starts running.
- 2026-11-13 — v2.3.0 released.
- 2026-11-16 — issue diagnosed; DB compacted.
Before the v2 line, the DB was smaller and the 1.x startup path was lighter, so
many concurrent windows were fine. The v2 upgrade ran destructive migrations and
retention against a multi-GB DB and left ~4.3 GiB of free pages behind.
Why WSL showed it more than the desktop
The desktop and WSL CLI use separate data directories:
- Desktop:
C:\Users\<user>\.local\share\opencode\opencode.db — 9.5 MB.
- WSL:
~/.local/share/opencode/opencode.db — was 5.86 GiB.
The desktop DB had only 9 sessions; the WSL DB had 251 sessions. The desktop
also runs an older version with the smaller/older schema.
WSL2 additionally counts file cache toward the VM's memory and does not return
it quickly unless autoMemoryReclaim is enabled. That amplifies the symptom,
but it is not the root cause. After compacting the DB, WSL startup dropped from
~7 GB to ~4 GB — the remaining cost is consistent with ~1.8 GiB of live session
data being loaded/decoded plus file cache/VM overhead.
Recurrence risk
This is not a one-time cleanup problem. The manual VACUUM only removes the
current 4.3 GiB of free pages; it does not change the app's behavior. The same
pattern can return because:
- A future migration that drops/rebuilds tables can again free a large number
of pages and raise the file's high-water mark.
cleanup prune=7.days deletes rows; without compaction, the file keeps the
size of the busiest period.
- A peak in live data (a very large tool output, many long sessions, etc.)
permanently raises the file size, even after the data is pruned.
SQLite reuses free pages, so the file normally stabilizes at the largest
live-data size seen rather than growing every day. But that ceiling can move up
again. A long-lived installation will therefore likely regress without either an
application-level fix or periodic local maintenance.
For impacted users, a one-time user-performed VACUUM should be described as
mitigation, not as the solution.
Recovery performed (no data lost)
-
Backed up the original DB to opencode.db.bak.
-
Ran VACUUM INTO a new file:
VACUUM INTO '/home/<user>/.local/share/opencode/opencode.compact.db'
-
Verified PRAGMA integrity_check = ok; identical row counts in both DBs
(251 sessions, 39,715 session_messages, 33,156 messages, 138,830 parts).
-
Swapped the compact DB into place and removed stale -wal / -shm.
-
Result: 6.29 GB → 1.94 GB, freelist_count = 0.
-
Added WSL settings: memory=6GB and
[experimental] autoMemoryReclaim=gradual (note: autoMemoryReclaim is an
unknown key under [wsl2]).
-
Added watcher ignores for node_modules, venv, .venv, site-packages,
caches, data/, output/, .git/ (secondary optimization only).
Suggested fixes
- Set
PRAGMA auto_vacuum=INCREMENTAL when creating the DB (requires a
one-time VACUUM), or run VACUUM after migrations that drop/rebuild
tables.
- Compact after the 7-day prune.
cleanup prune=7.days deletes rows;
follow it with PRAGMA incremental_vacuum or a scheduled VACUUM.
- Add a DB health check. If
freelist_count / page_count exceeds a
threshold (e.g. 20–25%), compact in the background and log it.
- Add maintenance commands:
opencode db stats and
opencode db vacuum (and possibly opencode db prune).
- Log DB size / freelist ratio at startup so this is visible before it
becomes a multi-GB problem.
- Bound startup loading. The new v2 session model appears to load a large
portion of the live data. Consider profiling startup RSS and lazy-loading
session parts; keep old (message, part) and new (session_message,
session_part) representations from being retained indefinitely.
- Keep large tool outputs / blobs bounded.
part was 0.69 GB across
138k rows; a retention or size cap (plus compaction) would keep the live
working set small.
- Document WSL settings.
[experimental] autoMemoryReclaim=gradual plus a
reasonable memory= cap prevents WSL from holding stale page cache.
- Treat compaction as ongoing maintenance. Free pages are reused, so the
file can look stable and then jump after a migration or usage peak. Provide a
documented periodic check/compaction path for long-lived installs (or run it
automatically), so users do not have to discover freelist_count themselves.
Appendix: quick repro / verification
PRAGMA page_size;
PRAGMA page_count;
PRAGMA freelist_count;
PRAGMA auto_vacuum;
PRAGMA journal_mode;
SELECT name, sum(pgsize) AS bytes
FROM dbstat
GROUP BY name
ORDER BY bytes DESC
LIMIT 10;
A DB with millions of free pages and auto_vacuum=0 will not shrink on its own.
VACUUM INTO produces a compact copy; VACUUM rewrites in place (both need
temporary disk space).
Note on terminology: VACUUM is a standard SQLite command and auto_vacuum is a
standard SQLite PRAGMA — neither is an OpenCode-specific API. The OpenCode DB
layer already uses SQLite (WAL, synchronous, busy_timeout, cache_size,
wal_checkpoint), so enabling compaction does not require a new dependency.
If useful, I can provide the compacted DB stats, the original migration list,
or a small standalone maintenance script that checks freelist_count/page_count
and vacuums when needed.
Plugins
No response
OpenCode version
No response
Steps to reproduce
No response
Screenshot
No response
Operating System
No response
Terminal
No response