feat(opencode): report context window usage#4572
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
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.
Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.
❌ 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 e75e143. Configure here.
ApprovabilityVerdict: Needs human review This PR introduces new capability by emitting 'thread.token-usage.updated' events and calling a new API endpoint (provider.list) during session initialization. While additive and well-tested, new features warrant human review. You can customize Macroscope's approvability policy. Learn more. |

What Changed
thread.token-usage.updatedevent from OpenCode assistant message updates.provider.list()when a session starts, while keeping that lookup best-effort.Why
OpenCode sessions already expose per-message token usage and model context limits, but the adapter did not forward them into T3 Code's existing context-window pipeline. As a result, the existing context-window meter had no usage data for OpenCode threads. This keeps the change inside the provider adapter and reuses the current contracts, ingestion, and UI.
UI Changes
No UI components or styling changed. The existing context-window meter now receives usage snapshots for OpenCode sessions; screenshots and interaction video are not applicable to this adapter-only change.
Validation
pnpm exec vp test run apps/server/src/provider/Layers/OpenCodeAdapter.test.ts(32 passed)git diff --checkpassedpnpm exec vp run --filter t3 typecheckpassed (with unrelated existing suggestions only)Checklist
Note
Low Risk
Adapter-only event emission with defensive parsing and best-effort provider lookup; no auth, persistence, or UI logic changes.
Overview
OpenCode threads can now drive the existing context-window meter by forwarding token usage into the same
thread.token-usage.updatedpipeline other providers already use.On session start, the adapter calls
provider.list()once and builds a per-model context-window map (failures log a warning and continue with no limits). Onmessage.updatedfor assistant messages with token data, it emits a normalizedThreadTokenUsageSnapshot—input totals include cache read/write, plus output and reasoning, with optionalmaxTokenswhen a limit was found.Tests extend the runtime mock with
provider.listand add cases for full cache breakdown and usage without cache details.Reviewed by Cursor Bugbot for commit dd20934. Bugbot is set up for automated code reviews on this repo. Configure here.
Note
Report context window usage in thread token-usage events from OpenCode adapter
makeOpenCodeAdaptercallsprovider.listto build a slug-to-context-window map (providerID/modelID→ max tokens); failures log a warning and use an empty map.message.updatedevent, token usage (input, cache read/write, output, reasoning) is aggregated and emitted as athread.token-usage.updatedevent, withmaxTokenspopulated from the context window map when available.normalizeOpenCodeTokenUsageandcollectOpenCodeModelContextWindowshelpers in OpenCodeAdapter.ts to handle aggregation and provider inventory parsing.Macroscope summarized dd20934.