Skip to content

fix(server): retain OpenCode V2 per-turn token usage - #9937

Open
saphid wants to merge 3 commits into
pingdotgg:t3code/codex-turn-mappingfrom
saphid:fix/v2-opencode-usage-20260905
Open

fix(server): retain OpenCode V2 per-turn token usage#9937
saphid wants to merge 3 commits into
pingdotgg:t3code/codex-turn-mappingfrom
saphid:fix/v2-opencode-usage-20260905

Conversation

@saphid

@saphid saphid commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

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-finish parts 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.turnTokenUsage with 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; hasSubagents is set when a task subagent is projected.

A new adapter test covers duplicate step replay, inflated assistant message.updated tokens, 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 415ed0f73b97f1655b6282492f81d0b2bba3a9cc at cd32bd8c5d3e9ef9f068426ea576815a0b76d24e. 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 reviewed aad785db62e4769c55aa915f5fc2a9ee0cadeb92 head; 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 OpenCodeAdapterV2

  • Adds OpenCodeTurnUsageAccumulator to track processed step IDs and aggregate input, cached input, cache creation, output, and reasoning tokens per provider turn
  • Deduplicates step-finish events by step ID; each new step emits a running provider-turn update with partial usage, while duplicate events are ignored
  • Finalized turns carry complete or partial aggregated usage before their terminal update; turns with no recorded steps report unavailable usage
  • Tracks a hasSubagents marker on turns that emit subagent activity and includes it in the token-usage payload
  • Adds an effect-based regression test verifying dedup, root/child accumulation, and subagent reporting
  • Risk: turns without step-finish events now report unavailable usage instead of zero; consumers of TurnTokenUsage in OpenCodeAdapterV2.ts must handle the unavailable case

Macroscope summarized aad785d.

@github-actions github-actions Bot added vouch:trusted PR author is trusted by repo permissions or the VOUCHED list. size:M 30-99 changed lines (additions + deletions). labels Sep 5, 2026
@macroscopeapp

macroscopeapp Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: 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.

@cursor

cursor Bot commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Bugbot is paused — on-demand spend limit reached

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M 30-99 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.

1 participant