Skip to content

fix(opencode): support OpenCode 2 session_message schema and credential table - #1186

Closed
arpankanwer wants to merge 1 commit into
robinebers:mainfrom
arpankanwer:fix/opencode2-session-message-compat
Closed

arpankanwer wants to merge 1 commit into
robinebers:mainfrom
arpankanwer:fix/opencode2-session-message-compat

Conversation

@arpankanwer

@arpankanwer arpankanwer commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Approved issue

Fixes #1124

TL;DR

OpenUsage failed on OpenCode 2 beta — "Couldn't read OpenCode's local database" and no Go meters — because the scanner still hit the v1 message table / auth.json only. This unions both message + session_message tables and falls back from auth.json to the SQLite credential table.

What was happening

OpenCode 2 (1.18.x beta, current stable) changed local storage:

  • message (fields role, providerID, modelID, tokens.total) → session_message (columns type, seq, data with type='assistant', JSON model.providerID, model.id, tokens.input/output/reasoning/cache.* — no total)
  • ~/.local/share/opencode/auth.json → SQLite credential table (integration_id='opencode-go', value JSON {"key":"sk-..."})

OpenUsage only queried FROM message with $.role / $.providerID / $.tokens.total and only read auth.json, so on a v2 DB every scan threw no such table: message or returned empty, and goAPIKey() returned nil.

What this changes

  • OpenCodeUsageScanner: dataSQL/probeSQL now UNION ALL both tables with coalesced JSON paths
  • OpenCodeAuthStore: try auth.json first, then fallback to credential table

Tests

swift build → Build complete
swift test --filter OpenCode → 40 passed

…al table

OpenCode 2 (1.18.x/beta) moved local storage:
- message table (role/providerID/modelID/tokens.total) -> session_message
  table (type=assistant, model.providerID, model.id,
  tokens.input/output/reasoning/cache.* without total)
- auth.json {"opencode-go":{"key":"sk-..."}} -> SQLite credential
  table (integration_id='opencode-go', value JSON {"key":"sk-..."})

Previously OpenUsage queried only 'message' with old JSON paths and
only read auth.json, so on OpenCode 2 it showed
"Couldn't read OpenCode's local database" and no Go meters.

Fix:
- Scanner: UNION ALL message + session_message with coalesced JSON
  paths (providerID, modelID, tokens) so either schema or both
  during migration works. probe and data queries updated.
- AuthStore: try auth.json first, then fallback to credential table
  across all opencode*.db files (opencode-go then generic sk-%),
  injected via SQLiteAccessing for testability, backwards compatible.

Resolves robinebers#1124

Test: swift build, swift test --filter OpenCode (40 passed),
swift test full (1225 passed), manual sqlite verification on
~/.local/share/opencode/opencode.db (46716 union vs 24893 v1-only)
@github-actions

Copy link
Copy Markdown

Thanks for your interest in contributing to OpenUsage!

External pull requests must reference an open issue that:

  1. Has been approved by a maintainer with the approved label.
  2. Is assigned to the pull request author (@arpankanwer).

Please discuss the change on an issue first, wait for a maintainer to approve and assign it to you, then reopen this pull request with Fixes #123 in its description.

Read the contribution guidelines.

@arpankanwer

arpankanwer commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor Author

@robinebers Thanks for assigning #1124! Could you please reopen #1186? Latest policy check is now passing, so it should stay open. I'll mark it Ready after.

@arpankanwer

Copy link
Copy Markdown
Contributor Author

Superseded by #1243.

This PR could not be reopened: GitHub blocks it while the head branch has been force-pushed after close (state cannot be changed. The fix/opencode2-session-message-compat branch was force-pushed or recreated.). The replacement branch carries the same change, rebased onto current main, and #1243 links Fixes #1124 the same way.

Nothing to review here — please look at #1243 instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenCode 2 (session_message schema) not detected - local database unreadable + missing auth.json

2 participants