Skip to content

fix: parse OpenCode sessions from SQLite storage (opencode.db)#170

Open
felipetruman wants to merge 1 commit into
microsoft:mainfrom
felipetruman:fix/opencode-sqlite-storage
Open

fix: parse OpenCode sessions from SQLite storage (opencode.db)#170
felipetruman wants to merge 1 commit into
microsoft:mainfrom
felipetruman:fix/opencode-sqlite-storage

Conversation

@felipetruman

@felipetruman felipetruman commented Jul 2, 2026

Copy link
Copy Markdown

Description

Problem

Newer OpenCode versions migrate session storage from the legacy JSON file layout (storage/session/global/*.json + storage/message/ + storage/part/) into a single SQLite database at ~/.local/share/opencode/opencode.db. After that migration the storage/ tree is left nearly empty, so parseOpenCodeSessions silently reports zero OpenCode sessions on any machine running a current OpenCode build.

On my machine this meant 1,143 real OpenCode sessions were invisible to the coach while Claude Code and Codex sessions were picked up fine.

Fix

  • parser-opencode.ts: read opencode.db via the node:sqlite builtin (Node >= 22.5). The db stores the same JSON payloads as the legacy files in data columns, minus the id fields, which live in dedicated columns — the reader re-attaches them (message.id/session_id, part.id/message_id/session_id) and surfaces the nested model.modelID as the flat modelID the parser expects. Session rows are plain columns and are mapped to the existing OcSession shape.
  • The existing per-session assembly logic is extracted into assembleOpenCodeSession() and shared by both the legacy JSON path and the new SQLite path, so request building, token accounting, and devcontainer detection stay identical.
  • node:sqlite is loaded lazily via process.getBuiltinModule and the SQLite source is skipped gracefully when the builtin is unavailable (e.g. older extension hosts). The db is opened read-only and failures fall back to whatever was already parsed, mirroring readJsonSafe's tolerance.
  • Legacy JSON sessions win on id collisions, so partially-migrated installs don't double count.
  • findOpenCodeDirs now also qualifies an install where only opencode.db exists and the storage/ dir is already gone.

Testing

  • New test seeds a real opencode.db (via node:sqlite) with a user → assistant exchange and asserts sessions, requests, token counts, flat model id, and workspace root are parsed; skips when the builtin is unavailable.
  • Full suite: 1214 tests pass. tsc --noEmit clean. ESLint: no new errors or warnings in parser-opencode.ts.
  • Verified against a real ~750 MB opencode.db: 0 → 1,143 sessions parsed, with Claude Code and Codex counts unchanged.

Related Issues

Fixes #84

Checklist

  • npm run check passes (typecheck + lint + spellcheck + knip + tests) — 1214 tests, cspell 0 issues, knip clean, tsc --noEmit clean, ESLint 0 errors
  • Changes are covered by tests (new SQLite-layout test seeding a real opencode.db)
  • Documentation updated (parser header comment documents both storage layouts)

Newer OpenCode versions migrate the JSON file layout into a single
opencode.db SQLite database, which left the parser reading zero
sessions. Read the db via the node:sqlite builtin (lazy-loaded and
skipped gracefully when unavailable), re-attaching the column-stored
ids to the JSON payloads and reusing the existing session assembly.
Legacy JSON sessions still win on id collisions.
Copilot AI review requested due to automatic review settings July 2, 2026 15:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@felipetruman

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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.

feat(opencode): add SQLite parser so OpenCode sessions load in VS Code

2 participants