Repository navigation
fix: read OpenCode v2 sessions from session_v2/session_message tables - #184
Closed
3intermute wants to merge 1 commit into
Closed
3intermute wants to merge 1 commit into
3intermute wants to merge 1 commit into
Conversation
OpenCode v2 replaced the legacy schema: sessions now live in session_v2
(with token totals on the row) and messages in session_message. The old
query failed with 'no such table: session', which run_query() swallowed,
so OpenCode v2 users silently saw zero sessions.
- Query session_v2 joined to project; read tokens from the row instead
of json_extract aggregation over the message table (one sqlite3 fork
per refresh instead of two, no scan of message blobs).
- Extract model/provider from the session_v2.model JSON blob
({id,providerID}) so the TUI keeps rendering 'provider/model'.
- Filter parent_id IS NULL: 84% of rows in a long-lived DB are subagent
child sessions that never map to a user-launched process.
- Turn count from session_message type='assistant' via the covering
index (session_id, type, seq).
Verified against a live 2.7GB WAL DB (OpenCode 2.2.5): query runs in
~27ms and all running opencode TUI processes are matched by cwd.
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.
Problem
OpenCode v2 replaced the SQLite schema: sessions moved from
session/messagetosession_v2/session_message(sameopencode.db). The collector's query fails withno such table: session;run_query()returnsNoneand the failure is swallowed, so on OpenCode v2 abtop silently shows zero sessions — no error in the TUI. v0.5.5 (latest release) and currentmainare both affected.This bit the wider ecosystem the same way (ccusage #1644, tokscale #1155).
Fix (one file,
src/collector/opencode.rs)session_v2joined toproject; read token totals from the row (tokens_input/tokens_output/tokens_cache_read/tokens_cache_write) instead ofjson_extractaggregation over the message table. Onesqlite3fork per refresh instead of two, and no scan of message blobs.session_v2.modelis a JSON blob ({"id":"glm-5.3","providerID":"zai-key2",...}), not a plain string — extract$.id/$.providerIDso the TUI keeps renderingprovider/model; both still go throughsanitize_db_field.parent_id IS NULL: in a long-lived DB 84% ofsession_v2rows (4599/5474 here) are subagent child sessions that never map to a user-launched process — without the filter they flood the top-20 and break PID claiming.session_messagerows withtype='assistant', served by the covering index(session_id, type, seq).PID↔cwd matching, status derivation, and sanitization are unchanged.
Verification
On a live 2.7GB WAL DB (OpenCode 2.2.5, actively written):
EXPLAIN QUERY PLANconfirms the covering index; outer scan is ~880 parent rows).abtop --jsonbefore:"sessions": []. After: all 7 running opencode TUI processes matched by cwd, correctzai-key2/glm-5.3model strings, tokens/cache/turn counts populated.cargo test(222 passed) andcargo clippy -- -D warningsclean.Note: context-% for OpenCode still over-reports for long sessions because it divides cumulative lifetime tokens by the window — pre-existing behavior, unchanged here.