Repository navigation
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (6)
Included review availability: Your plan provides up to 10 included reviews per hour; 2 remain after this review. 📝 WalkthroughWalkthroughOpenCode usage parsing now preserves the full cache-read token count and includes it when reported totals are lower than the combined token counters. The OpenCode usage schema version changes from 2 to 3. A cache-heavy SQLite reproduction test checks parsed sessions and usage projections. Persisted states with an older schema reset to current defaults while retaining the scan enabled flag. Related parser, materialization, and normalization tests update their expectations. Priority: ➖ Normal Severity of issue fixed: Medium Merge Risk: ⚪ Minimal · up to Cache-read usage is carried through the relevant totals and projections, while outdated cached state is reset for rescanning without disabling usage tracking. No actionable merge risk remains. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
ℹ️ No critical issues — the cache-read fix is correct and well tested. One coverage gap worth a look.
Reviewed changes
- Cache-read counting —
opencode-usage-row-parsing.tsnow takes the fullcache.read(dropping the oldMath.min(cache.read, input)cap) and computestotalTokens = Math.max(tokens.total, input + output + reasoning + cache.read). This matches OpenCode's own semantics:inputis the non-cached input and cache reads are additive. - Schema bump —
OPENCODE_USAGE_SCHEMA_VERSION2 → 3 invalidates stale v2 caches so existing undercounted files are rebuilt from source. - Enabled preservation — the schema-mismatch branch of
persisted-state-normalization.tsnow carriesscanState.enabledforward while discarding stale analytics, matching the codex/claude readers. - Regression coverage — a new session-table reproduction test drives the corrected counters through the parser plus daily/session/model/project projections;
scanner.test.tsandstore.test.tstotals were updated.
ℹ️ Cache-write tokens are still dropped from the session-table path
The parser now counts cache reads, but tokens_cache_write is never read and the primary session-table path hardcodes cache.write: 0, so cache writes stay out of the total there. OpenCode's own total includes cache write, so for Anthropic/Bedrock sessions — where cache creation is additive — Orca still undercounts, and the session path and message path can disagree for the same usage.
Technical details
# Cache write is excluded from the session-table total
## Affected sites
- `src/main/opencode-usage/opencode-usage-row-queries.ts:102-114` — `selectSessionUsageRows` synthesizes `data.tokens.total = tokens_input + tokens_output + tokens_reasoning` and hardcodes `cache: { read: tokens_cache_read, write: 0 }`. It never selects `tokens_cache_write`.
- `src/main/opencode-usage/opencode-usage-row-parsing.ts:85-88` — the fallback sum `input + output + reasoning + cachedInputTokens` omits cache write; because the session path's synthesized `total` also omits it, `Math.max` resolves to the cache-read-only sum.
- `src/main/opencode-usage/types.ts` — no `cachedWriteTokens` field exists on any projection, so there is nowhere to surface it even if read.
## Evidence (upstream OpenCode)
- `packages/opencode/src/session/session.ts` `getUsage`: `input = inputTokens - cacheRead - cacheWrite`; `total = usage.totalTokens` (AI SDK total, which includes cache read and cache write). So OpenCode's own total = `input + output + reasoning + cache.read + cache.write`.
- `packages/core/src/session/sql.ts`: the `session` table has a `tokens_cache_write` column (alongside `tokens_cache_read`).
## Why it matters
The message-blob path (`opencode-usage-row-queries.ts:149-163`, raw `data`) keeps OpenCode's `total`, which already includes cache write. The session-table path is preferred whenever session rows have usage, so the two paths produce different totals for identical usage. For Anthropic-backed OpenCode sessions, cache-creation tokens are real volume that stays unreported.
## Required outcome
Decide whether cache write belongs in OpenCode totals. If yes, read `tokens_cache_write` (guarded by `columnExists`, since older DBs may lack the column) and add it to the synthesized `total`; adding a `cachedWriteTokens` projection field is optional. If the intent is to fix only the cache-read undercount for now, a short comment noting the deliberate omission would keep the next reader from re-deriving this.
## Open questions for the human
- Is cache write intentionally out of scope for this PR (title says cache-read), or should it land here for parity with the Claude reader, whose total includes cache write?DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

ELI5
OpenCode reports cache-read tokens separately from new input. Orca was limiting cache reads to the input count and leaving them out of the total, so long cache-heavy sessions showed only a small fraction of the tokens actually served.
What Changed
Why
On the local OpenCode database, the old calculation reported 98,275,738 tokens while the corrected calculation reports 2,184,439,781. Recorded cost remains unchanged. Recomputing from the source database is safer than trying to repair old aggregates because the previous cache omitted information needed for an exact migration.
Linked Issue
Fixes #21440
Visual Proof
N/A — this changes the usage data source and persistence logic; no UI layout or interaction changed.
Testing
pnpm exec vitest run src/main/opencode-usage/cache-read-reproduction.test.ts src/main/opencode-usage/scanner.test.ts src/main/opencode-usage/store.test.ts(27 tests)pnpm tcpnpm run check:code-quality:changedgit diff --checkReview
The change is isolated to OpenCode usage parsing, cache invalidation, and regression tests. It does not add IPC or wire fields, change remote execution, or alter provider pricing.
Agent skill upstream boundary
Notes
Cross-platform behavior is unchanged: the parser consumes the same SQLite fields and JSON values on macOS, Linux, Windows, and remote hosts. Existing v2 caches are rebuilt from the source database.
Checklist
pnpm lint,pnpm test, andpnpm buildare left to CI; focused type, test, quality, and diff checks pass locally.