fix(token-usage): 修复 Pi 会话 token 入账 - #718
Merged
deepcoldy merged 1 commit intoAug 3, 2026
Merged
Conversation
为 Pi transcript 建立独立解析路径,确保未缓存输入与缓存读写按账本语义累计。 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
deepcoldy
approved these changes
Aug 3, 2026
deepcoldy
left a comment
Owner
There was a problem hiding this comment.
复审结论:✅ Approve,无 blocker(仅提交 review,不合码)。
重点核对:
- Pi 的逐行求和语义正确。 当前安装的
@earendil-works/pi-ai@0.80.6明确定义input/output/cacheRead/cacheWrite为互斥计量桶,totalTokens = input + output + cacheRead + cacheWrite;Pi 自己的 session stats 也是逐条 assistant message 累加这四项,不是累计快照取最后值。 reasoning不应额外入账。 Pi 的官方Usage类型注明 reasoning/thinking 是output的子集,output已包含 reasoning。若现在再加到 output 或单独参与总量会双算;账本没有 reasoning 桶不影响准确性。- 无 message.id 时增量读取仍不会重算。 本机 299 条真实 Pi assistant usage 记录的 id 全是 null;准确一次性依赖
scanJsonlFromOffset的 durable offset。未换行尾记录只折进 preview clone,补换行后才进入 durable state。我额外构造了“无 id + pending tail → 补换行 → 再 append”实盘,三次读取结果保持 exact-once。 - 当前 master 集成安全。 PR 分支落后共同祖先 431 个提交,但 GitHub 判定 MERGEABLE,trial merge 无冲突;合并后的
cost-calculator新字段也能自动兼容,Pi 仍只新增自己的 dialect 分支,Claude/Codex/CoCo/generic 路径未改变。
实际验证:
- PR head
3e2322ac:pnpm build✅ pnpm vitest run --project unit test/cost-calculator.test.ts: 43/43 ✅- 真实 Pi transcript 端到端(151 条 assistant usage): parser=
in 17,064,777 / out 48,360,与独立手算逐位一致 ✅ - 无 id 增量/尾行用例: PASS ✅
- 当前 master
7ef0e472trial merge 后:pnpm build✅ - 合并后 cost calculator + usage ledger + Dashboard token row: 88/88 ✅
- PR 旧基线全量 unit: 9,812/9,815;仅 3 个既有时区断言失败(scheduler/schedule-card-model),与本 PR 文件和调用路径无关。
非 blocker 建议:以后可把“Pi id=null + 增量 pending-tail”手工用例固化成单测;当前新增单测使用了字符串 id,和真实 Pi 日志不完全一致,但实现已通过上述真实/增量验证。
|
🚀 Released in v3.9.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
input、output、cacheRead与cacheWrite。影响范围
Test plan
pnpm vitest run test/cost-calculator.test.ts test/cost-calculator-cache.test.ts test/usage-ledger.test.ts(89/89)pnpm buildpnpm switch:here && pnpm daemon:restart6ffe0522…已写入两条 usage ledger 增量;累计值与 transcript 手工汇总一致🤖 Generated with Claude Code