Skip to content

feat(opencode): switch Go meters to the official usage API - #1097

Merged
robinebers merged 2 commits into
mainfrom
feat/opencode-official-usage-api
Aug 13, 2026
Merged

feat(opencode): switch Go meters to the official usage API#1097
robinebers merged 2 commits into
mainfrom
feat/opencode-official-usage-api

Conversation

@robinebers

Copy link
Copy Markdown
Owner

TL;DR

OpenCode Go Session / Weekly / Monthly now come from GET https://opencode.ai/zen/go/v1/usage — the same account-wide percents as the OpenCode dashboard. Local SQLite dollar-cap math is gone. Spend tiles and the usage trend still come from this Mac's logs.

What was happening

  • Go meters were reconstructed from local opencode-go message costs against hardcoded $12 / $30 / $60 caps.
  • That only saw this machine, so the card drifted from OpenCode's own dashboard.
  • OpenCode shipped the official usage endpoint in anomalyco/opencode#16513.

What this changes

  • New OpenCodeUsageClient calls /zen/go/v1/usage with the local opencode-go key as Authorization: Bearer.
  • Mapper turns { usage: { rolling, weekly, monthly } } into .percent meters (widget IDs and placement unchanged).
  • 401 → key rejected. 403 EntitlementError → no Go subscription (Zen spend tiles still work). Generic 403 / transport failures fail loudly.
  • Deletes OpenCodeGoWindows and the local window math. Scanner is tiles + trend only.
  • Docs updated (docs/providers/opencode.md, local HTTP API unit is percent).

Heads-up

  • Meters are account-wide percents, not reconstructed dollars. Local spend tiles can still undercount if you use OpenCode on other machines.
  • A Go key with no subscription and no local hosted usage surfaces "No OpenCode Go subscription on this key" instead of empty $0 caps.

Tests

  • OpenCode provider / mapper / scanner tests updated; 41 OpenCode tests passed locally.
  • ./script/build_and_run.sh verify — dev build launched; live Go key returned Session ~1% / Weekly 0% / Monthly ~1%.

Made with Cursor

Local SQLite dollar-cap math only saw this Mac and drifted from the
dashboard. The new /zen/go/v1/usage endpoint is account-wide percents.

Co-authored-by: Cursor <cursoragent@cursor.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 5aae1f0. Configure here.

Comment thread Sources/OpenUsage/Providers/OpenCode/OpenCodeUsageScanner.swift
Wall-clock now-30d sat later the same day than start-of-day 30 days ago,
so morning rows on the oldest day never left SQLite.

Co-authored-by: Cursor <cursoragent@cursor.com>
@robinebers robinebers added the gate-passed Passed the automated PR gatekeeper checks (gatekeeper skips it on later runs) label Aug 12, 2026 — with Cursor
@robinebers
robinebers merged commit 064819b into main Aug 13, 2026
3 checks passed
@robinebers
robinebers deleted the feat/opencode-official-usage-api branch August 13, 2026 07:45
@ardjo-s

ardjo-s commented Aug 13, 2026

Copy link
Copy Markdown

Confirming the production usage payload that this PR consumes.

GET https://opencode.ai/zen/go/v1/usage with a Go API key as Authorization: Bearer returns { usage: { rolling, weekly, monthly } }. Each window has status, percent (used — same meaning as the OpenCode dashboard “X% used”), and resetsAt.

Filed the remaining 0.7.8 user-facing bug (local SQLite cost 0 → Session/Weekly/Monthly stuck at 100% left) as #1102. This PR is the fix; 1095 was the original proposal. Latest GitHub release is still 0.7.8, so a follow-up cut would get this to people still on the dollar-cap meters.

kaanmertkoc pushed a commit to kaanmertkoc/openusage that referenced this pull request Aug 14, 2026
…s#1097)

* feat(opencode): read Go plan windows from the official usage API

Local SQLite dollar-cap math only saw this Mac and drifted from the
dashboard. The new /zen/go/v1/usage endpoint is account-wide percents.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(opencode): align SQLite scan cutoff with the calendar tile window

Wall-clock now-30d sat later the same day than start-of-day 30 days ago,
so morning rows on the oldest day never left SQLite.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
(cherry picked from commit 064819b)
mstallone added a commit to mstallone/runway that referenced this pull request Aug 14, 2026
**TL;DR** — The OpenCode Session / Weekly / Monthly meters now come from
OpenCode's official usage endpoint (`GET
https://opencode.ai/zen/go/v1/usage`, authorized with the local
`opencode-go` key) instead of being reconstructed from local SQLite logs
with hardcoded dollar caps and window math. Port of upstream openusage
robinebers#1097.

**What was happening**
- The Go plan meters were estimated locally: `OpenCodeGoWindows`
reimplemented the rolling 5-hour session, ISO-week, and anchored-month
windows over this machine's logs, bounded by hardcoded dollar caps.
- That undercounted account usage from other machines, drifted from
OpenCode's own accounting, and broke whenever OpenCode changed plan
caps.

**What this changes**
- New `OpenCodeUsageClient` calls the official usage API; the mapper
turns `{ usage: { rolling, weekly, monthly } }` into three percent
meters with reset countdowns — the same numbers the OpenCode dashboard
shows, account-wide.
- The provider distinguishes a rejected key (401), a valid key with no
Go subscription (403 `EntitlementError` → meters hidden, spend tiles
stay), and transport errors. If the local database is unreadable but the
API works, the meters still render and the failure is logged.
- `OpenCodeGoWindows` (147 lines of window math) and its tests are
deleted; the scanner now only feeds the spend tiles and trend from local
logs.
- Metric IDs (`opencode.session/weekly/monthly`) are unchanged, so
layout customizations, pins, and defaults carry over; the widgets switch
from bounded-dollar meters to percent meters. Docs updated
(`docs/providers/opencode.md`, `docs/local-http-api.md`).

**Heads-up**
- Runway-specific adaptations from upstream: no telemetry
`ErrorCategory` (Runway removed PostHog), and the scanner keeps Runway's
`DayKeyCache` day-key memoization.
- The local HTTP API's `session`/`weekly`/`monthly` units change from
`usd` (estimated) to `percent` — anything scripted against those units
will see the new unit field.

**Tests**
- `swift test` — full suite, 1772 tests, 0 failures (42 OpenCode tests,
ported from upstream with the Runway adaptations above).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs gate-passed Passed the automated PR gatekeeper checks (gatekeeper skips it on later runs) provider tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants