fix(opencode): Support incremental step usage, reasoning tokens, and model pricing - #91
Merged
Merged
Conversation
…model pricing - Add incremental flag to UsageUpdatedEvent for step-level usage deltas - Intercept step_finish in OpenCode parser, folding reasoning tokens into output - Discard dummy cost: 0 to allow CodeDeck pricing table fallback - Atomically accumulate incremental usage in daemon and persist to SQLite - Normalize model IDs in pricing.ts (strip provider prefixes/display labels) - Add pricing entries for Alibaba Qwen models and Claude cached rates - Fix Claude parser cache creation token drop - Add backfill script for recovering historical session metrics - Add comprehensive test coverage across parser, pricing, events, and daemon
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
This PR addresses the issue where OpenCode sessions reported
Tokens: 0and unpriced$0.00?costs in CodeDeck metrics, and fixes associated pricing and parser regressions across the runtime.Root Causes
step_finish:src/drivers/opencode/parser.tsexplicitly discardedstep_finishevents where OpenCode reports step tokens and costs.cost: 0for models missing from its localmodels.json(such asalibaba-token-plan), which suppressed CodeDeck's fallback pricing.alibaba-token-plan/qwen3.8-maxoropencode/claude-sonnet-4-6) failed static lookup inMODEL_PRICES.src/drivers/claude/parser.tsused??between read and creation tokens, dropping cache creation tokens when read tokens were present.Key Changes
incremental?: booleantoUsageUpdatedEvent.step_finishevents.tokens.reasoningintooutputTokens(standard provider billing for o-series/Qwen/DeepSeek).tokens.cache.readtocachedTokens.cost: 0toundefinedto allow static table fallback while preserving positive reported costs.incremental: true.updateSessionFromEvent):incremental: true, atomically addsinputTokens,outputTokens,cachedTokens, andcostto existing session usage in SQLite.Session.modelif reported in the usage event.sourceKeyinsession-runtime.tsprevents double-counting during daemon restarts and process reattach.resolveModelPrice()to normalize model IDs by stripping provider prefixes, display labels, and providing case-insensitive resolution.qwen3.8-max,qwen3.8-flash,qwen-plus,qwen-turbo) toMODEL_PRICES.cachedrates for Claude models (0.1x of input rate).cache_read_input_tokens + cache_creation_input_tokens.scripts/backfill-opencode-usage.tsto retroactively recover tokens and costs from historical.ndjsonsession logs.Testing & Verification
step_finishparsing, reasoning folding, incremental flag, and root/part fallbacks intests/opencode-parser.test.ts.tests/pricing.test.ts.tests/usage-daemon.test.ts.tests/events.test.ts.npm run build) completed with 0 errors.