fix(token-usage): Pi 会话补充上下文用量与百分比(卡片页脚) - #898
Conversation
foldPiLine 此前只累计 input/output/cache 用于成本统计,从不生成 latestContextUsage,导致 pi 会话的回复卡片缺失「上下文 N/M (P%)」 页脚(codex/claude 均正常)。 - foldPiLine 按 claude 同口径(input+cacheRead+cacheWrite)计算最新 一搏的上下文占用并写入 latestContextUsage - 窗口信息从 ~/.pi/agent/models.json 读取(mtime+size 缓存),模型 id 兼容 provider/model 与 model:variant 形态;找不到配置时降级为仅 显示 usedTokens,与 claude 行为一致 - __resetSessionUsageCachesForTest 顺带重置 piModelsCache 配套单测:无 models.json 降级、有 models.json 出百分比、变体模型 id 归一化匹配。
|
自动化初步复审发现两处建议修正后再合入的问题(最终以维护者审阅为准):
建议新增两类回归测试:
补充核对结果: |
- foldPiLine: prefer usage.totalTokens (Pi 0.84 native口径), fallback to input+output+cacheRead+cacheWrite (previous sum omitted output) - models.json resolution keyed by provider+model; bare-id fallback only when the id is unambiguous across providers (different windows → degrade safely) - tests: 3 existing Pi tests updated to include output; +3 new regression tests (totalTokens preference, provider-qualified window hit, ambiguous bare id) Verified: tsc --noEmit clean; vitest 59/59; pnpm build clean. Co-Authored-By: Claude <noreply@anthropic.com>
|
两处复审意见已修正并推送(33b67128):
验证: |
背景
pi 会话在 botmux 回复卡片上不显示「上下文 N/M (P%)」页脚,codex/claude 均正常。根因:
foldPiLine只累计 input/output/cacheRead/cacheWrite 用于成本统计,从不生成latestContextUsage,因此getSessionUsageSnapshot().context恒为 null。改动
src/core/cost-calculator.tsfoldPiLine按 claude 同口径(input + cacheRead + cacheWrite)计算最新一搏的上下文占用并写入agg.latestContextUsage;空 usage 的合成记录不覆盖上次测量windowTokens/percentUsed)从~/.pi/agent/models.json读取(mtime+size 缓存,避免逐行重读)。模型 id 兼容provider/model与model:variant形态;models.json 缺失/不可解析时降级为仅usedTokens(无百分比),与 claude 行为一致__resetSessionUsageCachesForTest顺带重置piModelsCachetest/cost-calculator.test.ts:新增 3 条 pi 上下文单测(无 models.json 降级 / 有 models.json 出百分比 / 变体模型 id 归一化)验证
test/cost-calculator.test.ts56 通过;test/cost-calculator-cache.test.ts+ bounded-widen 49 通过tsc --noEmit通过备注
pi 的 JSONL 只含每搏增量 usage,无窗口字段,百分比口径为「最近一搏 prompt 侧 tokens / 模型 contextWindow」,与卡片页脚「最新上下文」语义一致。