fix(server): retain OpenCode V2 per-turn token usage - #9937
Open
saphid wants to merge 3 commits into
Open
Conversation
Contributor
ApprovabilityVerdict: Not approved Macroscope's review found this PR not approvable — The adapter now changes production token accounting and emits usage data used by provider-turn analytics and usage pricing. The scope is focused and tested, but the billing/usage impact warrants human review. You can add or adjust custom eligibility rules. Learn more. |
…apping' into landing/pr-9937
Contributor
Bugbot is paused — on-demand spend limit reachedBugbot uses usage-based billing for this team and has hit its on-demand spend limit. A team admin can raise the spend limit in the Cursor dashboard, or wait for the next billing cycle to continue. |
…refresh/v2-pr-9937
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.
OpenCode V2 drops per-turn token usage even though the provider emits it in step-finish parts. Accumulate unique step IDs, publish live partial usage, and finalize with complete/partial/unavailable status. Keep child-session usage separate from the root and report the presence of subagents.
Normalize the separate cache/reasoning counts into T3’s inclusive input/output totals. This matches the pinned OpenCode v1.15.13 usage conversion. Ignore assistant message usage because the processor repeats each step’s totals in message updates, including the terminal update.
Validation: 27 focused adapter/testkit tests, patched server typecheck, and targeted lint pass on V2 base
2ac9bfed. The regression covers multiple root steps, duplicate part replay, repeated assistant updates, and separate child usage. The checked provider transcript also repeats a step with nonzero reasoning tokens. No live provider call was made.Targets #2829; addresses audit F21. Direct Claude Opus 5 high review was attempted but exited 1 before model execution because OAuth expired; no Claude review is claimed.
Implemented with Codex workers and reviewed with GPT-6 in Codex/T3.
Note
Medium Risk
Changes how token usage is computed and emitted on provider turns (billing/analytics surface), but logic is isolated to OpenCode V2 with a targeted regression test.
Overview
Restores per-turn token usage for OpenCode V2 by aggregating native
step-finishparts instead of relying on assistant message totals (which duplicate step counts).The adapter tracks a per-turn usage accumulator keyed by unique step IDs, updates
providerTurn.turnTokenUsagewith partial totals as new steps arrive, and sets complete or partial on turn finalization (or unavailable when no steps were seen). Cache read/write and reasoning tokens are folded into T3’s inclusive input/output fields, matching OpenCode’s usage conversion. Root and child (subagent) sessions keep separate usage;hasSubagentsis set when a task subagent is projected.A new adapter test covers duplicate step replay, inflated assistant
message.updatedtokens, multi-step root turns, and isolated child-session usage.Reviewed by Cursor Bugbot for commit 463f82c. Bugbot is set up for automated code reviews on this repo. Configure here.
Current-base verification
Updated to exact V2 base
415ed0f73b97f1655b6282492f81d0b2bba3a9ccatcd32bd8c5d3e9ef9f068426ea576815a0b76d24e. All 22 focused OpenCode V2 adapter tests pass, together with the server typecheck, targeted lint, and diff check. The contribution files are byte-for-byte identical to the previously reviewedaad785db62e4769c55aa915f5fc2a9ee0cadeb92head; this merge only incorporates the current target branch. There are no review threads. Earlier verification remains revision-bound with its original scope and limitations; no live external OpenCode server was used. Fresh independent Claude review remains unavailable because its OAuth credentials are expired. Refreshed and source-reviewed in the Codex harness; exact worker model metadata was not exposed to this session.Note
Fix per-turn token usage accounting in
OpenCodeAdapterV2OpenCodeTurnUsageAccumulatorto track processed step IDs and aggregate input, cached input, cache creation, output, and reasoning tokens per provider turncompleteorpartialaggregated usage before their terminal update; turns with no recorded steps report unavailable usagehasSubagentsmarker on turns that emit subagent activity and includes it in the token-usage payloadTurnTokenUsagein OpenCodeAdapterV2.ts must handle the unavailable caseMacroscope summarized aad785d.