Skip to content

OpenCode provider ignores SQLite DB when legacy JSON files exist (Windows, v0.9.15) #621

Closed

Description

@morpheus9393

Summary

CodeBurn v0.9.15 on Windows only reads the legacy file-based JSON sessions from OpenCode and completely ignores the current SQLite database (opencode.db), even when both exist side-by-side. This causes the dashboard to show only a tiny fraction of actual usage (in my case: $3.31 / 12 sessions instead of the real ~$2,617 / 71 models).

Environment

  • OS: Windows 11
  • Node: v24.13.0 (node:sqlite available and working)
  • CodeBurn: v0.9.15 (installed globally via npm install -g codeburn)
  • OpenCode: current (sessions stored in SQLite opencode.db, 1.5 GB, modified today)

Data layout on disk

~\.local\share\opencode\
├── opencode.db                          ← 1.5 GB SQLite, current (modified today 20:41)
├── opencode - Copy.db                   ← backup
├── opencode - Copy (2).db               ← backup
└── storage\
    ├── session\*.json                   ← 13 legacy JSON files (Jan 22 – Feb 13 2026, ~5 KB total)
    └── message\*.json                    ← 797 legacy message files (same period)

Symptom

codeburn report --provider opencode -p all shows:

  • $3.31 cost, 549 calls, 12 sessions
  • Last data point: 2026-02-13
  • Only the 13 legacy JSON sessions are visible

The 1.5 GB SQLite DB (containing all usage from Feb through today, ~$2,617 across 71 models including gpt-5.5 at ~$1,000) is completely ignored.

For comparison, tokscale reading the same machine shows the correct numbers from the SQLite DB:

  • $2,616.89 across 71 models, last data point today (2026-07-05)
  • Top model: gpt-5.5 at $1,000

Root cause (found in bundled dist/main.js)

In src/providers/opencode.ts, createOpenCodeProvider → discoverSessions():

async discoverSessions() {
  const fileSessions = await discoverOpenCodeFileSessions(resolvedDataDir, "opencode");
  if (fileSessions.length > 0) return fileSessions;   // ← short-circuits, never falls back to SQLite
  return discoverSqliteSessions(sqliteConfig);
}

The comment above it says:

"OpenCode 1.1+ stores sessions as file-based JSON; older builds used a SQLite DB. Prefer file-based when present, otherwise fall back to the DB so pre-migration installs keep reporting."

This assumption is backwards for real-world OpenCode installs on Windows. OpenCode migrated from file-based JSON (legacy, storage/session/*.json) to SQLite (opencode.db) at some point. After the migration, the legacy JSON files are left behind on disk but are no longer written to. The current data lives exclusively in opencode.db.

Because the discovery short-circuits on fileSessions.length > 0, any install that still has the old storage/session/ directory (even with just a handful of stale files from January) will never read the SQLite DB. The SQLite fallback only fires when the legacy directory is completely empty/absent — which is not the case for users who upgraded OpenCode in place.

Expected behavior

CodeBurn should read both sources and merge them (dedup by session ID), the way tokscale does (opencode.db or/and storage/message/ legacy). At minimum, it should prefer the SQLite DB when it exists and is larger/newer than the legacy files, rather than short-circuiting on any legacy file presence.

Suggested fix

Either:

  1. Merge both sources (dedup by session ID), so legacy + SQLite are both counted; or
  2. Prefer SQLite when present and only fall back to legacy files when no opencode*.db exists; or
  3. At minimum, compare mtime/size and prefer the source that is actively being written to.

Reproduction

  1. Install OpenCode on Windows, use it for a while (creates storage/session/*.json)
  2. Upgrade OpenCode in place (it migrates to opencode.db SQLite, leaves legacy files behind)
  3. Continue using OpenCode (data now flows into opencode.db only)
  4. npm install -g codeburn and run codeburn report --provider opencode -p all
  5. Observe: only the stale legacy JSON sessions are shown; current SQLite data is missing

Related

Activity

  1. NihalJain commented on Jul 5, 2026

    @NihalJain
    Contributor

    do you plan to provide a fix for this? if not i can hit this up? although i can try a fix and test only on mac, i believe this issue exists for mac too.

  2. morpheus9393 commented on Jul 5, 2026

    @morpheus9393
    ContributorAuthor

    Opened PR #622 with the fix. Verified on Windows 11 against a real 1.5 GB opencode.db + 13 stale legacy JSON files: before patch .31/12 sessions, after patch ,182.91/470 sessions (matches tokscale's independent SQLite reading). The bug is not Windows-specific — any in-place OpenCode upgrade that leaves legacy storage/session/ files behind will hit the same short-circuit on Mac too.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions