Repository navigation
Commit b85b28f
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
File tree
- docs/guide/opencode
- rust
- adapters/opencode/src
- crates/ccusage/tests
- snapshots
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
73 | 79 | | |
74 | 80 | | |
75 | 81 | | |
| |||
0 commit comments