Skip to content

#17 Fix: OpenCode Go plugin β€” schema migration, QuickJS float bug, and stale cache#18

Merged
symonbaikov merged 7 commits into
openusage-community:mainfrom
rizkyfaza20:feat/fix-opencode-go-sync
Jun 23, 2026
Merged

#17 Fix: OpenCode Go plugin β€” schema migration, QuickJS float bug, and stale cache#18
symonbaikov merged 7 commits into
openusage-community:mainfrom
rizkyfaza20:feat/fix-opencode-go-sync

Conversation

@rizkyfaza20

Copy link
Copy Markdown

#17 Opencode Go Subscription Issue

Problem

The OpenCode Go plugin had two silent failures that made it show wrong or zero usage:

  1. Schema mismatch β€” OpenCode migrated per-message cost to session.cost in May 2026. The plugin still queried message.data.cost, which OpenCode no longer populates. Query threw "no such column" errors β†’ plugin showed "No usage data".

  2. QuickJS float truncation β€” sqlite3 -json serializes REAL values with 18-decimal precision (e.g. 0.105931999999999998). QuickJS's JSON.parse silently floors these to integers, making all fractional costs disappear. Every cost < 1.0 became 0.

  3. Stale cache on panel reopen β€” The panel only probed on mount or on a 5-minute timer. If the panel was hidden, no re-probe fired until the user waited for the interval. The display showed yesterday's data.


Changes

plugins/opencode-go/plugin.js

  • Switch message β†’ session table, data β†’ model column, $.model.providerID β†’ $.providerID
  • CAST(cost AS TEXT) β€” sqlite3 outputs cost as a clean string that Number() parses correctly in QuickJS
  • clampPercent: Math.round(...*10)/10 β†’ Math.floor(...) to match OpenCode server calculation
  • HISTORY_EXISTS_SQL and HISTORY_ROWS_SQL rewritten for the new schema

plugins/opencode-go/plugin.test.js

  • Updated SQL assertions for model column, $.providerID, time_updated, cost > 0
  • Monthly expected value 4.5 β†’ 4 (Math.floor alignment)

src/App.tsx

  • Added visibilitychange listener that triggers handleRefreshAll() when the panel opens β€” no more stale cache on reopen

src/hooks/use-probe-events.ts

  • Accept batchId === "background" (reserved for future Rust background reprobe)

…fresh on panel open

- Switch from message to session table (cost moved in OpenCode May 2026 migration)
- Fix column: data -> model, JSON path: $.model.providerID -> $.providerID
- CAST(cost AS TEXT) for QuickJS compat (sqlite3 -json REAL values get
  18-decimal precision that QuickJS silently floors to integers)
- clampPercent: Math.round -> Math.floor (match OpenCode server behavior)
- Trigger refresh on panel visibility change (no more stale cache on reopen)
- Accept background batchId in probe events (reserved for future Rust timer)
@symonbaikov

Copy link
Copy Markdown
Collaborator

Thanks for the PR the fix addresses real issues and the existing test suite passes. A few things need to be addressed before merge:

  1. Update provider docs
    docs/providers/opencode-go.md still shows the old SQL (message, data, $.role). Please sync it with the new session / model / cost > 0 schema.
  2. Add regression tests
    Since this is a bug fix, please add tests for the two main failure modes:
    • CAST(cost AS TEXT) so fractional costs like 0.1059… are not truncated to 0 by QuickJS.
    • visibilitychange triggering handleRefreshAll in App.test.tsx.
  3. Avoid magic string
    In src/hooks/use-probe-events.ts, extract "background" into a named constant.
  4. Confirm time_updated units
    The switch to time_updated AS createdMs assumes the value is in milliseconds. If OpenCode stores it as seconds, all time windows will break. Please verify and document.
  5. Bump plugin version
    plugin.json is still 0.0.1 but plugin behavior changed. Consider bumping it.
    Once these are addressed and CI is green, this should be good to merge.

- Update SQL examples to use session table, model column, CAST(cost AS TEXT)
- Update detection text from 'assistant messages' to 'sessions with cost data'
- Update usage scope from 'assistant spend' to 'session spend'
…ility refresh

- plugin.test.js: verify fractional cost 0.7 parses to used=5 (not 0)
- App.test.tsx: verify visibilitychange triggers startBatch
- App.tsx: use startBatch directly (bypass handleRefreshAll cooldown)
@rizkyfaza20

Copy link
Copy Markdown
Author

Hi @symonbaikov , its have been added, for documentation and also for the regression test, please let me know if you have more question

@symonbaikov

Copy link
Copy Markdown
Collaborator

Everything works! Thank you very much for your work!

@symonbaikov symonbaikov merged commit f7fb8df into openusage-community:main Jun 23, 2026
4 checks passed
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.

2 participants