Skip to content

fix(opencode): usage totals include what OpenCode 2 spends on compaction - #30565

Open
MarcoDotIO wants to merge 3 commits into
pingdotgg:mainfrom
MarcoDotIO:fix/prov-compaction-usage
Open

MarcoDotIO wants to merge 3 commits into
pingdotgg:mainfrom
MarcoDotIO:fix/prov-compaction-usage

Conversation

@MarcoDotIO

Copy link
Copy Markdown
Contributor

fix(opencode): usage totals include OpenCode 2 compaction spend

Problem

OpenCode 2 records the spend of a compaction request (automatic or /compact) on that compaction's own row in session_message (type = 'compaction', with model, tokens and cost in data). Assistant replies are separate type = 'assistant' rows. T3's OpenCode usage reader (readOpenCodeUsage in packages/provider-opencode/src/server/usage.ts, reached through openCodeUsageReader.scan for the usage page) selected only type = 'assistant' rows from session_message. So T3 left every OpenCode 2 compaction out of its token and cost totals, while OpenCode's own stats counts it.

Evidence from OpenCode's source:

  • packages/core/src/session/message-updater.ts writes model, cost and tokens onto the compaction message for session.compaction.ended, and cost/tokens for session.compaction.failed.
  • packages/core/src/session/stats.ts adds compaction usage (session.usage.recorded with source = 'compaction') to its assistant-message totals, so the two do not overlap.
  • The Compaction.Ended schema in @opencode/schema (2.0.28, the version T3 pins) carries model, cost and tokens.

OpenCode 1.x stores a compaction as an ordinary assistant message, which T3 already counts, so this is a gap only for OpenCode 2.

Change

The session_message predicate now selects type IN ('assistant', 'compaction'). The existing message parser already reads the compaction row's shape (model.id, tokens, cost, time.created). Rows without tokens (a running compaction) or without a model (a failed compaction) still produce no record, as before.

Scope and approval

A one-line fix to an obvious undercount in an existing reader, with a focused test, so it needs no prior issue under CONTRIBUTING.md. It changes only which rows the usage scan reads. It changes no UI text or layout; the usage page shows larger, correct totals for OpenCode 2 users whose sessions were compacted.

Open PR #27679 also edits the session_message query in this file (to skip huge user rows). The two changes touch neighbouring lines and should combine without conflict, but whichever lands second needs a small merge.

Verification

Added counts OpenCode 2 compaction requests alongside assistant replies to packages/provider-opencode/src/server/usage.test.ts. It builds an OpenCode 2 style session_message table with a user row, an assistant row, a completed compaction (with model, tokens and cost) and a running compaction (no tokens).

Before the fix (upstream/main):

- Expected
+ Received
    [
      "opencode:assistant-1",
      "big-pickle",
      0.02,
    ],
-   [
-     "opencode:compaction-1",
-     "big-pickle",
-     0.5,
-   ],
  ]
 Test Files  1 failed (1)
      Tests  1 failed | 5 passed (6)

After the fix:

 Test Files  1 passed (1)
      Tests  6 passed (6)

Also ran vp lint and vp fmt --check on packages/provider-opencode/src/server, and tsc --noEmit in packages/provider-opencode. All clean. The commit hook's knip check passed.

Not checked: a real OpenCode 2 database. The table layout and row shape come from OpenCode's packages/core/src/session/sql.ts and message-updater.ts, and from the pinned @opencode/schema types.

Proof: a regression test that fails on main; the row shape was checked against the pinned @opencode/schema 2.0.28 (CompactionUsage on the compaction message). Reviewed with GPT-6.1 Sol through the Codex CLI.

Created with Claude Opus 5.5 in the Claude Code desktop app.

🤖 Generated with Claude Code

OpenCode 2 stores a compaction request's model, tokens and cost on its
`compaction` row in `session_message`, separate from assistant replies.
The usage reader selected only `assistant` rows, so every compaction was
missing from T3's usage totals while OpenCode's own stats count it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added vouch:unvouched Author is not yet trusted in the VOUCHED list. size:XS 0-9 changed lines (additions + deletions). labels Nov 4, 2026
@macroscopeapp

macroscopeapp Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved at a9c6137

Macroscope's review found this PR approvable — OpenCode usage totals now include tokens and costs recorded on compaction messages. The change only broadens a read-only history query and preserves existing parsing, filtering, and deduplication safeguards.

You can add or adjust custom eligibility rules. Learn more.

@coderabbitai

coderabbitai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

readOpenCodeUsage now selects compaction messages alongside assistant messages. A test checks that completed compactions produce usage records with their model, cost, and token totals, while running compactions do not.

Changes: OpenCode usage records

Layer / File(s) Summary
Message selection and test coverage
packages/provider-opencode/src/server/usage.ts, packages/provider-opencode/src/server/usage.test.ts
The session_message query now selects assistant and compaction messages. The test checks usage records for the assistant reply and completed compaction, and verifies that the running compaction is not returned.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Suggested reviewers: juliusmarminge

Merge Risk: Minimal · up to 0a91a

OpenCode usage totals now include compaction spend recorded by OpenCode 2. The change is small and tested, and no actionable merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: OpenCode 2 compaction spend is now included in the usage totals. It is specific, follows the conventional commit form, and matches the raw summary, which says the read…
Description check Passed The description follows the template. It has Problem, Change, Scope, and Verification sections. The Problem section explains the undercount and the affected reader. The Change section explains the pre…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: OpenCode 2 compaction spend is now included in the usage totals. It is specific, follows the conventional commit form, and matches the raw summary, which says the reader now selects both assistant and compaction rows from session_message.

Full details: Description check

Explanation

The description follows the template. It has Problem, Change, Scope, and Verification sections. The Problem section explains the undercount and the affected reader. The Change section explains the predicate change and why running and failed compactions stay uncounted. The Scope section explains why no prior issue is needed, and it links the related open PR #27679. The Verification section gives the new test name, before and after results, the lint, format, and typecheck runs, and what was not checked. The description also names the model and harness, as the template asks. The only gap is that the Scope section does not link an issue or a maintainer request. The template allows a short justification for small obvious fixes instead, and the author gave one.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between ce57f49 and 0a91a8a.

Files selected for processing: 2

  • packages/provider-opencode/src/server/usage.test.ts
  • packages/provider-opencode/src/server/usage.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.

# Conflicts:
#	packages/provider-opencode/src/server/usage.test.ts
@maria-rcks

Copy link
Copy Markdown
Collaborator

Note

Written by claude-opus-5-5 on behalf of Maria

Merged current main into the branch (merge commit, no rebase). The only conflict was in usage.test.ts, where main added the "projects both SQLite formats" test next to this PR's compaction test; both are kept. Main's new projected read (json_object of tokens/cost/model/time and the role filter) still passes compaction rows, since they carry no role, and the compaction test passes against it. vp run check:changed passes on a Blacksmith testbox (format, lint, knip, typecheck, 2 test files).

This branch has not been deployed

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

Labels

size:XS 0-9 changed lines (additions + deletions). vouch:unvouched Author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants