Skip to content

Commit b85b28f

Browse files
authored
fix(opencode): count usage from completed v2 compaction rows (ccusage#2050)
* fix(opencode): count usage from completed v2 compaction rows The OpenCode v2 loader skipped every session_message row whose type was not "assistant". That filter came from the original v2 support (ccusage#1668), which ignored non-assistant rows generically to drop user and marker rows; it was not added to avoid double-counting compactions. OpenCode records a completed compaction as its own session_message row whose tokens and cost are the usage of the compaction request itself, not the size of the resulting context. That usage is billed separately from assistant steps: compaction.ts publishes it as a UsageRecorded event with source "compaction", while assistant usage comes from Step.Ended. Skipping these rows therefore understated tokens and cost in every report, for example 12 instead of 174 tokens in the issue's repro. Accept "compaction" rows alongside "assistant" rows. They go through the same path, so message-ID deduplication across source roots and fork-copy exclusion by seq still apply, and session aggregates stay a fallback only for sessions without message usage. Running compactions carry no usage and failed ones record no model, so the existing parser still drops both. Closes ccusage#2013 * fix(opencode): keep session totals for compaction-only sessions The previous commit made compaction rows mark their session as covered by message usage, which suppresses the cumulative session aggregate fallback in unbounded session reports. OpenCode's session counters are the sum of every Step.Ended, every Step.Failed with usage, and every UsageRecorded event, and compactions publish UsageRecorded, so the aggregate already includes compaction usage. Revert deletes message rows without lowering the counters, so a session can be left with only compaction rows. Suppressing the aggregate for such a session dropped its assistant usage, which main still reported through the fallback. Adding the aggregate on top of the compaction rows would have double-counted the compaction instead. Keep compaction rows out of the coverage set and load them separately. Session aggregates fill sessions that have no assistant or legacy message usage, and compaction rows then count only for sessions that no aggregate covered. Period and date-bounded reports never use the aggregates, so they still count every completed compaction. * test(opencode): cover compaction usage in CLI reports rust/adapters/AGENTS.md asks for CLI JSON assertions and table snapshots for every report mode an adapter change affects. Run the ccusage binary against an OpenCode v2 database with a mixed assistant and compaction session and a compaction-only session, assert the daily, weekly, monthly, and session JSON totals, and snapshot each table. * fix(opencode): count only completed v2 compaction rows Compaction rows were accepted by type alone. Running and failed compactions stayed out only because the existing parser drops rows without a model, and that was incidental. OpenCode's failed compaction still records usage: compaction.ts publishes Compaction.Failed with the cost and tokens of every call already billed, and the CompactionFailed schema keeps those fields while omitting the model. A failed row that gained a model, from another OpenCode version or a provider that records one, would have been counted, even though attributing failed compactions is outside this change. Pass the row kind to the parser and accept a compaction only when its status is "completed". Group the row fields into a SessionMessageRow so the parser signature stays within clippy's argument limit.
1 parent cd7b35d commit b85b28f

8 files changed

Lines changed: 543 additions & 21 deletions

‎docs/guide/opencode/index.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ OpenCode supports subagent sessions. The session report displays:
7070

7171
When OpenCode v2 forks a session, it copies the parent's history into the fork. ccusage counts only the usage the fork generates itself, so copied turns are not counted twice in session, daily, weekly, or monthly reports. If the fork's parent session or boundary message is no longer in the database, the fork keeps all of its rows.
7272

73+
## Compaction Usage
74+
75+
OpenCode v2 records each context compaction as its own message, with the tokens and cost of the compaction request itself. ccusage counts completed compactions alongside assistant turns in every report, using the compaction's recorded model, timestamp, and cost. Running compactions carry no usage yet, and failed compactions record no model to attribute their usage to, so neither is counted.
76+
77+
A session report without a date range uses OpenCode's cumulative session totals for a session whose only remaining usage rows are compactions, such as after a revert removed its assistant turns. Those totals already include the compaction usage, so the compaction rows are not added on top.
78+
7379
## Claude Agent Provider
7480

7581
The `claude-agent` provider runs Claude Code under the hood, so each turn is also saved in Claude Code's own logs. In [unified reports](/guide/all-reports) such as `ccusage daily`, ccusage skips the OpenCode copy of turns the Claude source already counts, so they are priced from the Claude Code logs. OpenCode stores only a model alias such as `opus`, while Claude Code records the exact model, so its costs are accurate.

0 commit comments

Comments
 (0)