fix(dashboard): 修正 Sessions 页 total_tokens 漏算缓存 token 的聚合口径#240
Merged
Conversation
- src/coding/proxy/logging/db.py: query_recent_sessions / query_session_profile 两条按 session_key 分组的聚合 SQL 中, 将 total_tokens 由 SUM(input + output) 修正为 SUM(input + output + cache_creation + cache_read), 与同文件 query_total_tokens_by_vendor / query_usage 已使用的全口径对齐, 消除 Sessions 页与 Overview 页之间的统计口径分裂; - tests/test_session_aware.py: test_query_recent_sessions_basic / test_query_session_profile_found 中追加 cache_creation_tokens 与 cache_read_tokens 入参及完整口径断言, 锁定回归; - docs/issue.md: 追加 Issue 档案, 记录 SQL 聚合层 SSOT 缺失导致的口径漂移, 沉淀向 usage_log 增列后必须审计所有 SUM 表达式的防范经验. 🤖 Generated with [Claude Code](https://github.com/claude), [CodeX](https://openai.com), [Gemini](https://github.com/apps/gemini-code-assist) Co-Authored-By: Aurelius Huang<threefish.ai@gmail.com>
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.
摘要
修复 Dashboard Sessions 页
Tokens列遗漏cache_creation/cache_read两类 token 的统计 Bug,使其与 Overview 页的全口径(input + output + cache_creation + cache_read)保持一致。背景
Anthropic Prompt Cache 在长会话中常常贡献远超 input/output 的命中量,原口径
SUM(input + output)导致 Sessions 页总量被显著低估,并与 Overview 页的卡片、Token 时序图统计口径分裂。Schema 与log()入参均已完整记录四类 token,问题完全收敛在两条按session_key分组的聚合 SQL。变更
src/coding/proxy/logging/db.py:query_recent_sessions/query_session_profile两条聚合 SQL 的total_tokens改为SUM(input + output + cache_creation + cache_read),复用同文件query_total_tokens_by_vendor(第 584 行)已验证的全口径,无前端 / API 结构变动。tests/test_session_aware.py:在test_query_recent_sessions_basic/test_query_session_profile_found中追加cache_creation_tokens/cache_read_tokens入参与完整口径断言,锁定回归。docs/issue.md:追加 Issue 档案,沉淀「SQL 聚合层保持单一权威定义」「usage_log增列后必须审计所有SUM表达式」两条防范经验。验证
uv run pytest tests/test_session_aware.py -q→ 28 项通过uv run pytest -q→ 全量 1450 项通过,无副作用grep "SUM(input_tokens + output_tokens)"全仓库已无残留实例