opencode-copilot-chat icon indicating copy to clipboard operation
opencode-copilot-chat copied to clipboard

Wire SQLite reader into getSummary() to fix usage accuracy drift (#23 root cause)

Open ltmoerdani opened this issue 5 months ago • 1 comments

Context

PR #55 shipped session-level cost tracking on top of the extension's local estimate (buildSummaryFromTracked). That estimate is fine for per-session display. The subscription totals in the status bar tooltip (today / weekly / monthly) still use it, though, and the number can drift from what OpenCode actually bills. Two things the extension can't see: server-side cache affinity, and any usage from the CLI or TUI.

Issue #23 reported exactly this drift (weekly 9% to 15% too low, monthly 15% too low). It got closed as COMPLETED through PR #50, but PR #50 only added manual usage targets. The underlying estimate never changed.

Current state (verified on main post-PR #55)

goUsageTracker.ts already has most of the pieces. They just aren't wired in:

  • readOpenCodeHistory() (line 228) reads ~/.local/share/opencode/opencode.db, returns HistoryRow[] with { createdMs, cost }. SQL matches the OpenUsage plugin.
  • buildSummaryFromRows() (line 343) builds a UsageSummary from those rows using the correct anchored-month formula.
  • hasSQLiteData (line 424) is the only caller of readOpenCodeHistory, and itself has no callers.
  • getSummary() (line 337) only calls buildSummaryFromTracked(...). Never the SQLite path.

Proposal

  1. Make SQLite the primary source. getSummary() calls readOpenCodeHistory() first; if rows come back, use buildSummaryFromRows(). Fall back to tracked only when the DB is missing (user never ran the CLI).
  2. Merge tracked entries as enrichment, with dedup by timestamp window so the two sources don't double-count.
  3. Adopt anchoredMonthBounds from OpenUsage so the monthly window lines up with the dashboard. Current code uses calendar month, which accounts for part of the 15% drift on its own.
  4. Drop hasSQLiteData unless it earns a real caller during the wire-up. Keep the code path if someone wants it as a feature flag, otherwise delete.

Why this matters

Users treat the status bar as the source of truth for spend. A 9% to 15% drift, especially near the subscription cap, quietly trains them to distrust it. Manual targets move that burden onto the user; the default should be correct on its own.

The reader, SQL, and buildSummaryFromRows already exist. What's left is the call site swap, dedup logic, and the anchored month formula.

References

  • Issue #23 (closed COMPLETED via PR #50 workaround)
  • PR #50 (manual targets, not a root fix)
  • PR #55 (session tracking; unrelated to subscription totals, but it introduced the (est) marker pattern worth reusing here)
  • OpenUsage plugin: https://github.com/robinebers/openusage/blob/main/docs/providers/opencode-go.md
  • Repo memory note: opencode-go-usage-accuracy.md (TODO #1-#4, Jun 2026)

ltmoerdani avatar Jun 26 '26 11:06 ltmoerdani

OpenUsage just removed the support for Opencode because the lack of API: https://github.com/robinebers/openusage/issues/565

The only real fix here i think is use oAuth to get the most recent data, and failback to a local estimate that should run in parallel when the web scrap / private api breaks. Then we can keep fixing over time until breaks again. Or they publish a public API for that.

Wallacy avatar Jun 26 '26 13:06 Wallacy