Skip to content

fix(opencode): derive per-model stats from step-finish parts#31138

Open
Hexecu wants to merge 3 commits into
anomalyco:devfrom
Hexecu:fix/stats-models-fork-cost
Open

fix(opencode): derive per-model stats from step-finish parts#31138
Hexecu wants to merge 3 commits into
anomalyco:devfrom
Hexecu:fix/stats-models-fork-cost

Conversation

@Hexecu
Copy link
Copy Markdown

@Hexecu Hexecu commented Jun 6, 2026

Issue for this PR

Closes #31032

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

What does this PR do?

opencode stats --models builds its per-model cost and token breakdown by summing message.info.cost and message.info.tokens for each assistant message. Forked sessions copy those message fields verbatim, so the pre-fork history of every fork is counted again in the per-model breakdown.

This changes the per-model aggregation to derive cost and tokens from each assistant message's step-finish parts instead. For a normal session the sum of a message's step-finish part costs/tokens equals message.info by construction (the same usage value is written to both), so the reported numbers are unchanged. Forked sessions zero out their cloned step-finish parts, so deriving from parts stops the breakdown from double-counting pre-fork usage.

This is the per-model breakdown half of #31032. It builds on #31136, which excludes pre-fork usage from the session-level cost/tokens totals by zeroing the cloned step-finish parts on fork. This PR is stacked on #31136, so its diff currently also contains that commit; it should be merged after #31136.

How did you verify your code works?

  • Extended the fork cost test in packages/opencode/test/session/session.test.ts to assert that a forked session's cloned step-finish parts carry zero cost and tokens (the values the new aggregation reads), while the parent session keeps its original part cost. bun test test/session/session.test.ts passes (8 tests).
  • bun typecheck passes for the opencode package and across the monorepo (22/22).

Checklist

  • I have performed a self-review of my code
  • I have made corresponding changes to the documentation if needed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Session fork double-counts pre-fork API costs in opencode stats and cost reporting

1 participant