feat(opencode): surface context-window usage and session cost on the meter - #8659
feat(opencode): surface context-window usage and session cost on the meter#8659ardevdevts wants to merge 5 commits into
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:
Comment |
ApprovabilityVerdict: Not approved Macroscope's review found this PR not approvable — This PR introduces a cross-layer OpenCode metering capability that changes the production event pump, shared usage contract, persisted activity stream, and user-facing context meter. Because it automatically adds token/cost tracking and catalog lookups to existing sessions, the change warrants human review. Notes:
You can add or adjust custom eligibility rules. Learn more. |
Problem
OpenCode sessions never reported token usage, so the context-window meter stayed empty for them while Codex and Claude sessions render usage.
Change
OpenCodeAdapternow emitsthread.token-usage.updated: per-message counts from assistantmessage.updatedevents folded with cumulative session totals and USD cost fromsession.updatedevents. Emissions are deduped over every input that shapes the snapshot, so unchanged session replays don't spam the event log while real changes (including output-only growth mid-stream) re-emit.maxTokens) is resolved once per model from theprovider.listcatalog; a transient catalog failure is retried on the nextsession.updatedinstead of giving up for the session's lifetime.costonThreadTokenUsageSnapshot; the web context-window meter renders a Cost row when a provider reports it.Note: Claude's
total_cost_usdcurrently lands on theturn.completedpayload and is not wired into the meter — left as-is for now.Verification
Focused tests pass (OpenCodeAdapter 92, ContextWindowMeter + contextWindow 11) covering the mapping, dedupe, late/retried catalog resolution, zero-token handling, and model switches; targeted typecheck is clean.
Model: glm-5.3-flash · harness: opencode
Note
Surface context-window usage and session cost on the
ContextWindowMeterOpenCodeAdapternow emitsthread.token-usage.updatedevents on assistant message and session updates, carrying normalized token counts (input, output, reasoning, cacheRead, cacheWrite), optionalmaxTokensresolved viaclient.provider.list(), and optional cumulative session cost.normalizeOpenCodeTokenCountsandnormalizeOpenCodeTokenUsageclamp non-finite/negative/undefined values to zero and build aThreadTokenUsageSnapshot. Emissions are deduped by a composite key; zero-token assistant messages no longer clobber prior per-message usage.resolveOpenCodeMaxTokenscaches per-model context-window size, drops the value on model switch, times out after 5s, and retries on subsequent session updates.costfield toThreadTokenUsageSnapshot. ContextWindowMeter.tsx renders a Cost row in the popover whenusage.cost > 0, formatted via newformatContextWindowCost(2 decimals, or 4 for sub-cent amounts).session.updatedandmessage.updatedhandlers inmakeOpenCodeAdapternow perform catalog lookups and emit usage events on every relevant update; catalog failures leavemaxTokensunresolved until a later update succeeds.Macroscope summarized 1f71e67.
Note
Medium Risk
Changes the OpenCode event pump (catalog calls on
session.updated) and a shared usage contract/UI path; behavior is heavily tested but incorrect token or cost mapping would affect metering accuracy.Overview
OpenCode sessions now drive the same context-window meter as other providers by emitting
thread.token-usage.updatedfrom assistantmessage.updatedandsession.updatedevents, with normalization helpers that clamp bad token payloads and map OpenCode’s split counts intoThreadTokenUsageSnapshot(used vs cumulative totals, optional breakdown fields).maxTokensis looked up once per model viaprovider.liston the event path (short timeout, retry on later session updates). Model switches clear stale limits; emissions are deduped unless usage, cost, or capacity actually changes. Zero-token assistant messages no longer wipe the last real per-message usage.UI/contracts:
ThreadTokenUsageSnapshotgains optionalcost(USD); the context-window meter popover shows a Cost row when the provider reports it (formatContextWindowCost). Tests cover mapping, dedupe, catalog failure/retry, model switches, and the new meter row.Reviewed by Cursor Bugbot for commit 1f71e67. Bugbot is set up for automated code reviews on this repo. Configure here.