Repository navigation
新增跨会话用量统计接口 /v1/usage/stats - #209
Merged
Merged
Conversation
GET /v1/usage/stats?days=N&tz=IANA 汇总所有工作区会话的 token 用量:按天、按模型、 总计、连续天数、最长会话与费用;/v1/health 增加 capabilities.usage_stats: 1。 - input 沿用 ai.Usage.PromptTokens(含缓存读写),cacheRead/cacheWrite 为其子集, total = input + output,与 UsageSummary 和 /usage 卡片一致。 - 回复按 sent_at 分日,缺失时取前一条消息时间,再退回会话创建时间;消息无用量的旧会话 把元数据合计记在最后活动日。 - 子代理转录会话与父会话 SubUsage 只计一次;分支/复制的同 id 回复去重。 - 每个会话文件的用量条目按路径、大小、修改时间缓存,统计不获取会话锁。 - 费用逻辑抽到 config.UsageCost,kn 的 /usage 卡片与后端共用。
用量统计不持有会话锁读取会话文件。Windows 上 os.Open 不带 FILE_SHARE_DELETE, 统计读取期间回合保存会话的改名替换会失败(Access is denied)。改用 openShared 以 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE 打开(做法同 permissions/readfile_windows.go),其他平台仍是 os.Open;补充 Windows 上持有 读取句柄时保存仍成功的测试。
Windows 上只要有句柄开着会话文件,MoveFileEx 替换就会失败,带 FILE_SHARE_DELETE 也一样,上一版的测试在 Windows CI 上因此失败。 - 用量统计以共享方式打开会话文件,从同一句柄取大小与修改时间后一次读完原始字节 并立即关闭,再从内存解析;句柄只在读文件的那一下开着。 - 新增 renameRetry(同 permissions/store.go):会话文件替换与 SetCWD 的会话目录移动 (及回滚)在 Windows 上重试约四秒,也能等过杀毒软件、索引服务对新文件的短暂占用。 - Windows 测试改为:持有共享句柄时在 goroutine 里保存,约 100ms 后关闭句柄,断言保存 成功且下一次统计看到新的总量。
This was referenced Nov 15, 2026
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.
Closes #208
内容
/v1/health增加capabilities.usage_stats。GET /v1/usage/stats?days=N&tz=IANA:汇总所有工作区会话最近 N 天(默认 371,范围 1–1100)的用量,按tz分日(默认进程本地时区,非法时区返回 400)。返回总量、按天明细(含各模型)、按模型明细(含费用)、活跃天数、当前与最长连续活跃天数、最常用模型、最长会话。字段说明见docs/liveagent-backend.md。统计口径
input即请求的完整输入,已包含缓存读写;cacheRead、cacheWrite是其中的部分,total = input + output。与 kn/usage卡片、usage.updated一致。SentAt分日;缺SentAt时取之前最近一条有时间的消息,再退到会话创建/更新时间。SubUsage,避免重复;只有旧版本没有子代理转录时才用SubUsage。子代理用量计入父会话。/usage卡片的计价逻辑抽到config.UsageCost共用;订阅计费的 provider 不计价;没有任何可计价用量时为null。性能与并发
FILE_SHARE_DELETE打开会话文件,统计读取期间回合保存(改名覆盖)不会失败。测试
go test ./internal/session ./internal/backend:跨时区分日、days范围、缺SentAt的时间回退、连续天数、子代理不重复计、SubUsage回退、分支去重、旧会话、缓存失效、HTTP 参数校验、费用 number/null、订阅 provider 不计价。TestUsageStatsReaderDoesNotBlockSaves仅在 Windows 上运行(本地 macOS 跳过);GOOS=windows go vet与go test -c通过。go run ./cmd/k-brain-vet ./...(UI 锁分析)通过。已知限制