Skip to content

fix(opencode): read the 2.x session_v2 + session_message generation - #1436

Open
ozymandiashh wants to merge 2 commits into
getagentseal:mainfrom
ozymandiashh:fix/1293-opencode-v2-sessions
Open

ozymandiashh wants to merge 2 commits into
getagentseal:mainfrom
ozymandiashh:fix/1293-opencode-v2-sessions

Conversation

@ozymandiashh

Copy link
Copy Markdown
Collaborator

Summary

OpenCode 2.x (mainline since 2.0.3, per #1293) writes sessions to session_v2 and messages to session_message (tagged by a type column, ordered by seq, payload JSON in data). The legacy session/message/part tables freeze at upgrade, so every post-upgrade session was invisible to CodeBurn.

  • src/providers/sqlite-session-parser.ts branches per database on sqlite_master: when the v2 tables exist they win and the legacy tables are ignored entirely — the generations are never joined, exactly as ejwill laid out in how can see the usage of the opencode v2? #1293. Otherwise the legacy path is byte-for-byte unchanged (kilo-code, which shares this parser, is unaffected: it has no v2 tables).
  • v2 payloads normalize into the shared message/part shape, so tokens (same {input, output, reasoning, cache:{read, write}} object), cost, model refs ({id, providerID}provider/model), tools, bash commands, user attribution, the parent_id child-session walk, and the session-level cost/token rollup fallback (same columns on session_v2) all behave exactly as on legacy.
  • docs/providers/opencode.md documents the two generations and the branch rule.

Test plan

  • 5 new tests in tests/providers/opencode.test.ts: v2-only discovery+parse (tokens/cost/model/tools/user message), non-user/assistant type filtering (compaction/idle/model-switched), child-session walk, upgraded DB with both generations (v2 wins, frozen legacy session invisible), and the session_v2 rollup fallback.
  • All 46 pre-existing opencode tests unchanged and passing; full suite 4113 passed / 0 failed.

Fixes #1293.

@github-actions

Copy link
Copy Markdown

Closing for now: you already have 5 pull requests open (#1339, #1347, #1352, #1364, #1434), and we review at most 5 per contributor at a time. Reopen this one when one of them is merged or closed. Issues are always welcome.

@dcaldr

dcaldr commented Sep 15, 2026

Copy link
Copy Markdown

Isnt this close contraproductive?

@iamtoruk

Copy link
Copy Markdown
Member

Fair question. The cap is not about this PR, it is about fairness across contributors: when one person has five PRs waiting, new ones from them go to the back of the queue instead of the front. Issues and draft PRs are always fine. A sixth ready PR is not, until one of the five lands.

The part that is on us: those five were open because review is slow, not because they are bad. I am going through them today. #1434 just merged, so this one reopens now and the OpenCode 2.x fix goes in next.

@iamtoruk iamtoruk reopened this Sep 15, 2026
OpenCode 2.x (mainline since 2.0.3) writes sessions to session_v2 and
messages to session_message (tagged by `type`, ordered by `seq`, payload
JSON in `data`), while the legacy session/message/part tables freeze at
upgrade — so every post-upgrade session was invisible.

Branch per database on sqlite_master: when the v2 tables exist they win
and the legacy tables are ignored (the generations are never joined);
otherwise the legacy path is untouched. v2 payloads normalize into the
shared message/part shape, so tokens, cost, model refs, tools, bash
commands, user attribution, child-session walks and session-level token
rollups all behave exactly as before.

Fixes getagentseal#1293 (schema and generation rules confirmed there by ejwill).

@iamtoruk iamtoruk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Verified against a real 1.x database: export is byte-identical before and after, $109.91 / 4,009 calls / 177 sessions. Built a 2.x fixture from ejwill's DDL and the v2.0.3 payload schemas; the branch reads it correctly, tokens, cost, model, tools and bash all land. Generations never joined. Good work.

Two things before this lands.

Compaction messages carry cost and tokens in 2.0.3 (CompactionUsage in packages/schema/src/session-message.ts). The type !== 'assistant' filter drops them, and the "skips non-user/assistant" test pins that as correct. On 1.x these were role: assistant and we counted them, so this silently undercounts every compacted 2.x session. My fixture lost $0.42 and 181k tokens on one turn. Treat compaction like assistant for cost and tokens, and flip that test.

Needs a CHANGELOG bullet under Unreleased / Fixed.

Unrelated note for #1362: the v2 queries add two more time_archived IS NULL, so that fix now has five places to strip, not three.

Rebased cleanly onto main, pushed as pr1436-rebased if you want to start from that.

…ngelog

Compaction rows carry their own CompactionUsage in 2.0.3 and counted as
assistant messages on 1.x; dropping them undercounted every compacted
2.x session (iamtoruk's fixture lost $0.42 / 181k tokens on one turn).
A `running` compaction has no usage and still yields nothing.
@ozymandiashh
ozymandiashh force-pushed the fix/1293-opencode-v2-sessions branch from 8157a89 to da55154 Compare September 15, 2026 17:30
@ozymandiashh

Copy link
Copy Markdown
Collaborator Author

Both addressed in da55154, on top of your pr1436-rebased:

  • compaction rows now normalize like assistant messages, so CompactionUsage (cost + tokens, model ref included) lands as it did on 1.x; a running compaction carries no usage and still yields nothing. The pinning test is flipped — it now asserts the usage is counted (181k tokens / $0.0042 on the fixture) alongside a running-compaction skip.
  • CHANGELOG bullet under Unreleased / Fixed.

opencode + kilo-code suites green (57/57), typecheck clean.

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.

how can see the usage of the opencode v2?

3 participants