fix(opencode): merge legacy file sessions and SQLite DB instead of short-circuiting#622
Conversation
…ort-circuiting The OpenCode provider's discoverSessions() short-circuited on legacy file-based JSON sessions (storage/session/*.json) and never fell back to the SQLite DB (opencode.db) when any legacy file existed. This caused migrated installs (which keep stale legacy JSON files from before the OpenCode SQLite migration) to report only a tiny fraction of actual usage — the SQLite DB was silently ignored. Replace the either/or short-circuit with a merge of both sources. Per- message dedup via seenKeys (keyed by provider:sessionId:messageId) prevents double-counting sessions that exist in both stores. Fixes getagentseal#621
|
Nice catch, and the diagnosis is right. I traced the parser path to sanity-check the merge approach and it holds up:
Two things worth tightening before merge:
Out of scope for this PR, but the header comment in |
ozymandiashh
left a comment
There was a problem hiding this comment.
Approving on correctness. I traced the parser path (graceful [] when opencode.db is absent, parserDedup shared across sources so the file pass dedups the SQLite pass) and CI is green. The both-stores regression test and the ID-stability note above remain worth a follow-up, but neither blocks the fix.
Summary
Fixes #621
The OpenCode provider's
discoverSessions()short-circuited on legacy file-based JSON sessions (storage/session/*.json) and never fell back to the SQLite DB (opencode.db) when any legacy file existed. This caused migrated installs — which keep stale legacy JSON files from before the OpenCode SQLite migration alongside the now-current SQLite DB — to report only a tiny fraction of actual usage, with the SQLite DB silently ignored.Root cause
The comment said "Prefer file-based when present, otherwise fall back to the DB so pre-migration installs keep reporting." But the real-world migration direction is the opposite: OpenCode migrated from file-based JSON to SQLite. After an in-place upgrade, the legacy
storage/session/directory is left behind with stale files, and all new data flows intoopencode.db. The short-circuit fires on the stale legacy files and the SQLite DB is never read.Fix
Merge both sources instead of either/or:
Per-message dedup is already handled in
createSessionParserviaseenKeys(keyed by${provider}:${sessionId}:${messageId}), so sessions that exist in both stores are not double-counted.Verification (Windows 11, Node v24.13.0)
Before fix (v0.9.15,
codeburn report --provider opencode -p all):After fix (patched dist, same command):
Cross-checked against
tokscalereading the sameopencode.db: $2,616.89 across 71 models — consistent with the patched CodeBurn output (the small delta is explained by CodeBurn counting a few legacy JSON sessions that tokscale deduplicates differently).Why this also helps Mac users
The bug is not Windows-specific. Any OpenCode install that was upgraded in place (on any OS) and retains the
storage/session/directory with even one legacy JSON file will hit the same short-circuit. The issue title mentions Windows because that's where it was reported, but the fix applies to all platforms.Checklist
src/providers/opencode.ts(not just the bundled dist)opencode.db+ 13 legacy JSON files