Skip to content

feat: add OpenCode usage statistics - #7221

Open
tris203 wants to merge 15 commits into
pingdotgg:mainfrom
tris203:t3code/show-opencode-usage-stats
Open

feat: add OpenCode usage statistics#7221
tris203 wants to merge 15 commits into
pingdotgg:mainfrom
tris203:t3code/show-opencode-usage-stats

Conversation

@tris203

@tris203 tris203 commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

What changed

The Usage page previously only counted Claude Code and Codex. This adds OpenCode as a third scanned provider.

  • Server: OpenCode moved its transcripts into a SQLite store (~/.local/share/opencode/opencode.db), so instead of the JSONL file walk the other providers use, its scan is a single read-only query against the message table via node:sqlite. The window filter is pushed into SQL (time_updated), and only completed assistant messages with token usage and a known model are projected into usage records. cost is trusted when positive (OpenCode prices its own curated/subscription models that LiteLLM doesn't know); zero-cost rows fall back to LiteLLM like Codex. The message id is the dedupe key.
  • Contracts: UsageProviderKind gains "opencode".
  • Scan cache: bumped to v3 — v2 entries were never parsed for OpenCode and must not be reused.
  • Web: OpenCode joins PROVIDER_PRESENTATION (violet #8b5cf6, OpenCodeIcon); the chart test expectation gains the zero-value opencode band that PROVIDER_ORDER now produces.
  • Mobile: OpenCode added to provider order, labels, and theme-aware colors.

Why

Anyone driving agents through OpenCode currently sees an empty or understated Usage page. Scanning the provider's own transcript store (rather than T3's orchestration projections) keeps usage complete even for sessions never driven through T3 Code — the same ccusage-style approach the existing providers use.

Before / after

image

Verification

  • usageTranscripts.test.ts gains focused coverage for the OpenCode message projection (88 new lines); all 26 usage tests pass.
  • Scoped typecheck and lint clean for the touched packages.

Built with kimi-k3 via OpenCode.


Note

Medium Risk
New SQLite/worker read path and changed merge dedupe behavior affect reported totals; scan cache v3 invalidates prior entries. Contract v6 adds a provider but remains merge-compatible with v4+.

Overview
Adds OpenCode as a fourth usage provider end-to-end: contract UsageProviderKind and v6 contract version, mobile/web chart labels and violet styling, and docs.

On the server, usage is read from OpenCode’s SQLite store (~/.local/share/opencode/opencode.db, overridable via OPENCODE_DB / XDG_DATA_HOME) instead of JSONL walks. A read-only worker query pulls completed assistant usage from legacy message and current session_message tables; rows map through parseOpenCodeUsageRow (completion time, token/cost rules, message id dedupe). OpenCode skips the per-file scan cache (window-sensitive); scan cache bumps to v3 for the expanded provider set.

Reliability: JSONL listing now surfaces hadReadError; failed transcript reads return null and sources can be partial or failed instead of silently counting zero. Multi-environment merge claimSources prefers healthier sources (ok over partial; failed/missing don’t claim) so a bad duplicate scan doesn’t hide good data.

Reviewed by Cursor Bugbot for commit 8e7316b. Bugbot is set up for automated code reviews on this repo. Configure here.

Note

Add OpenCode usage provider with SQLite worker reader and source-health merge ranking

  • Adds opencode as a new usage provider across server, web, mobile, and contracts; the server reads OpenCode usage from its SQLite database via a worker thread in readOpenCodeRecords, honoring OPENCODE_DB and XDG defaults via resolveOpenCodeDatabasePath.
  • Introduces parseOpenCodeUsageRow to project scalar rows into UsageRecord, capping reasoning tokens to output, dropping zero-token rows, and trusting positive reported costs.
  • listTranscriptFiles now returns hadReadError so readSummary can mark JSONL sources partial when directory traversal or file reads fail, with a user-facing message.
  • Merge logic in claimSources now ranks sources (ok > partial > missing/failed) so a failed source no longer hides a healthy duplicate; ownedContribution excludes failed/missing sources from session and bucket attribution.
  • Risk: USAGE_SCAN_CACHE_VERSION bumps 2→3 and USAGE_CONTRACT_VERSION bumps 5→6, causing a one-time cold rescan and requiring client/server contract renegotiation; OpenCode reads bypass the mtime/size cache and always fetch fresh, which may add overhead on large databases.

Macroscope summarized 8e7316b.

@coderabbitai

coderabbitai Bot commented Aug 16, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 2d6cc8c9-622d-4e86-87ca-e47fc0d72b30

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added vouch:unvouched PR author is not yet trusted in the VOUCHED list. size:L 100-499 changed lines (additions + deletions). labels Aug 16, 2026
Comment thread packages/contracts/src/usage.ts Outdated
Comment thread apps/server/src/usage/UsageService.ts Outdated

@macroscopeapp macroscopeapp Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One finding: adding a third usage provider makes the hardcoded colSpan on the time-breakdown empty state in UsagePage.tsx under-span the table. Details inline.

Posted via Macroscope — UI Consistency

Comment thread apps/web/src/components/usage/usageProviders.ts
Comment thread apps/server/src/usage/UsageService.ts
Comment thread apps/server/src/usage/UsageService.ts Outdated
@macroscopeapp

macroscopeapp Bot commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Not approved

Macroscope's review found this PR not approvable — This adds OpenCode usage reporting through a new SQLite worker integration and propagates the provider through server, contract, web, and mobile layers. It also changes existing transcript failure handling and multi-environment merge ownership, creating broader runtime impact than a small isolated usage update.

You can add or adjust custom eligibility rules. Learn more.

Comment thread apps/server/src/usage/usageTranscriptReader.ts Outdated
Comment thread apps/server/src/usage/usageTranscriptReader.ts
Comment thread apps/server/src/usage/UsageService.ts
Comment thread apps/server/src/usage/usageTranscriptReader.ts Outdated
Comment thread apps/server/src/usage/usageTranscriptReader.ts Outdated
Comment thread apps/server/src/usage/UsageService.ts
@t3dotgg t3dotgg added vouch:trusted PR author is trusted by repo permissions or the VOUCHED list. and removed vouch:unvouched PR author is not yet trusted in the VOUCHED list. labels Aug 24, 2026
@tris203
tris203 force-pushed the t3code/show-opencode-usage-stats branch from 2ce7e4f to 557dbb7 Compare August 24, 2026 18:36
Comment thread apps/server/src/usage/UsageService.ts
Comment thread apps/server/src/usage/usageTranscriptReader.ts Outdated
tris203 added 10 commits August 28, 2026 13:21
- Read OpenCode SQLite transcripts and report token and cost usage
- Display OpenCode usage across web and mobile clients
…rsion

A failed OpenCode SQLite open/query was flattened to zero usage and
reported as status ok; propagate the null so the source reports failed.
Adding the opencode provider kind also changes the wire schema, so bump
USAGE_CONTRACT_VERSION to 5 for older clients to detect.
…s it

The resolver invented a macOS Application Support branch and an
OPENCODE_DATA_HOME override; the CLI resolves its data home through
xdg-basedir on every platform (XDG_DATA_HOME or ~/.local/share/opencode)
and the only documented override is OPENCODE_DB, which can be absolute or
name a database inside the data home. Mirror that, including the
channel-suffixed database caveat for dev installs.
@tris203
tris203 force-pushed the t3code/show-opencode-usage-stats branch from 13d3eb0 to a1cf597 Compare August 28, 2026 12:26
Comment thread apps/server/src/usage/UsageService.ts
@github-actions github-actions Bot added size:XL 500-999 changed lines (additions + deletions). and removed size:L 100-499 changed lines (additions + deletions). labels Aug 28, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

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 high 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.

Want fixes drafted automatically? Bugbot Autofix can create code changes for findings. A team admin can enable Autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 42cdcfe. Configure here.

Comment thread apps/server/src/usage/usageTranscriptReader.ts Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XL 500-999 changed lines (additions + deletions). vouch:trusted PR author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants