Skip to content

fix(coding-agent): exported session header counts all usage - #12763

Open
acmerfight wants to merge 2 commits into
earendil-works:mainfrom
acmerfight:fix/12762-export-usage-totals
Open

acmerfight wants to merge 2 commits into
earendil-works:mainfrom
acmerfight:fix/12762-export-usage-totals

Conversation

@acmerfight

Copy link
Copy Markdown
Contributor

Closes #12762.

Problem. The header of an exported session adds up usage from assistant messages only. /session (getSessionStats) and the footer also add usage entries (cache warming), tool result usage, and compaction and branch summary usage, which #6671 and #9668 started recording. For a session with one reply, one cache warming request and one compaction, /session showed 7510 tokens and $0.170 while the exported page showed 1100 tokens and $0.100. Cache warming is on by default, so most longer sessions are affected. /export, /share and pi --export all use this page.

Fix. computeStats in template.js moves the token and cost accumulation into a local addUsage and calls it for every usage /session counts: assistant messages, tool results, usage entries, compactions and branch summaries. The fallback to the component sum for sessions written before cost.total existed (#9980) applies to all of them. Message and entry counts are unchanged.

Validation on main e00d10b5c:

  • New test in test/export-html-cost.test.ts: runs the viewer's computeStats on an assistant reply, a tool result, a usage entry, a compaction and a branch summary, plus entries without usage. It fails without the fix (expected 1000 to be 7800). The existing Calculated cost for top open models on OpenRouter is off by 2-3x almost always #9980 test now shares the same helper and still passes.
  • End to end (throwaway test, not committed): a session written with SessionManager, exported with exportFromFile, and the viewer's computeStats run on the embedded entries. Before: 1100 tokens, $0.100 against 7510 tokens, $0.170 from the /session sum. After: both 7510 tokens, $0.170.
  • npm run check passes. ./test.sh after npm run build: the coding-agent suite passes (3338 tests). The only failures are unrelated to this change: one Chord test (route-admission.test.ts, which also fails when run alone), one pi-ai model catalog test (zai-coding-plan-models.test.ts, on the generated model prices) and the pi-env tests (they need cargo, which is not installed here).

🤖 Generated with Claude Code

The export viewer's computeStats added usage from assistant messages
only. /session and the footer also add usage entries (cache warming),
tool result usage, and compaction and branch summary usage, so the
exported header showed fewer tokens and a lower cost: 1100 tokens and
$0.100 against 7510 tokens and $0.170 for a session with one reply, one
cache warming request and one compaction.

The accumulation moves into addUsage, called for each of those sources.
The component-sum fallback for old sessions (earendil-works#9980) applies to all.

Closes earendil-works#12762.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exported session header shows fewer tokens and a lower cost than /session: cache warming, compaction and tool result usage are left out

1 participant