Skip to content

feat(opencode): report context window usage#4572

Open
tris203 wants to merge 2 commits into
pingdotgg:mainfrom
tris203:opencode_context
Open

feat(opencode): report context window usage#4572
tris203 wants to merge 2 commits into
pingdotgg:mainfrom
tris203:opencode_context

Conversation

@tris203

@tris203 tris203 commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

What Changed

  • Emit the provider-neutral thread.token-usage.updated event from OpenCode assistant message updates.
  • Include input, cache read/write, output, and reasoning tokens in the active context snapshot.
  • Load each OpenCode model's context limit from provider.list() when a session starts, while keeping that lookup best-effort.
  • Add focused adapter coverage for model-limit lookup and normalized usage emission.

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)
  • Targeted formatting and lint passed for the changed files
  • git diff --check passed
  • pnpm exec vp run --filter t3 typecheck passed (with unrelated existing suggestions only)
  • GitHub CI passed: Check, Test, Mobile Native Static Analysis, and Release Smoke

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes (not applicable: no UI code changed)
  • I included a video for animation/interaction changes (not applicable: no animation or interaction changed)

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.updated pipeline 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). On message.updated for assistant messages with token data, it emits a normalized ThreadTokenUsageSnapshot—input totals include cache read/write, plus output and reasoning, with optional maxTokens when a limit was found.

Tests extend the runtime mock with provider.list and 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

  • At session start, makeOpenCodeAdapter calls provider.list to build a slug-to-context-window map (providerID/modelID → max tokens); failures log a warning and use an empty map.
  • On each assistant message.updated event, token usage (input, cache read/write, output, reasoning) is aggregated and emitted as a thread.token-usage.updated event, with maxTokens populated from the context window map when available.
  • Adds normalizeOpenCodeTokenUsage and collectOpenCodeModelContextWindows helpers in OpenCodeAdapter.ts to handle aggregation and provider inventory parsing.
  • New tests in OpenCodeAdapter.test.ts cover both the normal case (with cache details) and the missing-cache-fields edge case.

Macroscope summarized dd20934.

@coderabbitai

coderabbitai Bot commented Jul 26, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: fe1b5f8e-f952-466a-9db0-98835562988b

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added vouch:unvouched PR author is not yet trusted in the VOUCHED list. size:L 100-499 changed lines (additions + deletions). labels Jul 26, 2026
Comment thread apps/server/src/provider/Layers/OpenCodeAdapter.ts

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.

Fix All in Cursor

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

Comment thread apps/server/src/provider/Layers/OpenCodeAdapter.ts Outdated
Comment thread apps/server/src/provider/Layers/OpenCodeAdapter.ts Outdated
@macroscopeapp

macroscopeapp Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Approvability

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

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

Labels

size:L 100-499 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant