Repository navigation
fix(claude): avoid estimating input before message_delta - #7976
zhu1025960209-web wants to merge 1 commit into
Conversation
AI-assisted change with synthetic regression fixtures. Preserve reported usage, cache categories, output fallback, and existing message_delta completion semantics.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
WalkthroughThe Claude stream finalizer now fills missing input usage with estimated prompt tokens only after the stream is done. Tests cover cache usage, interrupted streams, and zero-frame aborts. ChangesClaude stream usage
Priority: ➖ Normal Estimated code review effort: 2 (Simple) | ~10 minutes Change: Bug fix Possibly related issues
Suggested reviewers: Merge Risk: ⚪ Minimal · up to
|
Agent
0.154.0-alpha.6.2。gpt-6-astra。Links
User request
Out of scope — refuse
Open gate — do not open unless all are satisfied
.agents/github/ISSUE.md提交后,被机器人以“未使用标准模板”关闭,并非技术问题已解决;保留原报告,不重复提交。go buildor tests passed: yes,见下方 RED/GREEN 与回归结果。Kind
Issue facts
7fb6e57a的合成回归中,在message_delta前中断且输入/cache 无证据时,finalizer 把请求估算补成普通输入,并进入 canonical usage。POST /v1/messages,未发送 HTTP;使用合成模型标识和 SSE 字符串;倍率/数据库不参与目标数量计算,前端与部署升级不适用。完整适用字段与未验证边界见 Claude 流在 message_delta 前中断时仍将整请求估算记为输入用量 #7975。Change
缺失输入仅在
claudeInfo.Done为 true 时沿用请求估算。未 Done 时不补普通输入;已有 input/cache、已收到输出的 fallback、工具输出开销及 Done 的原有兼容行为保留。finalize/canonical 从修正后的本地 usage 生成,不会在该路径重新补入请求估值。Research
Duplicate / prior art
usage==nil;fix: skip Claude prompt estimate for zero-frame aborts #6820(Open)仅处理异常零帧。本补丁覆盖已收部分输出且 adapter 已构造非 nil usage 的路径。fix(claude): preserve explicit zero usage and cache-only billing #7816(Merged)修复 cache-only;其已合并逻辑不改。Docs and code
d0322047仅作结构导航,不替代当前源码。README.zh_CN.md确认 Messages/stream/usage 支持;遵循.agents/rules/billing.md。FinalizeClaudeStreamBillingUsage→CanonicalUsage→effectiveBillingUsage;服务层仅整个 usage 为 nil 时另行估输入,非 nil 零 usage 不会触发该回退。Alternatives considered
Files
relay/channel/claude/relay-claude.gorelay/channel/claude/relay_claude_test.goBehavior
Done仍在任意message_delta后为 true,不代表 transport 成功;wire billing snapshot、图片计数、定价、固定价/工具附加费及历史账务不改,不声称所有断流免费。Verification
GOWORK=off go test ./relay/channel/claude -run 'TestClaudeStreamFinalUsageEstimatesOnlyUncountedUsage|TestClaudeStreamFallbackCountsInterruptedToolUse' -count=1 -v:目标三类 RED,错误输入为 1200/1200/100;一个生产条件修改后同命令 GREEN,9 个表用例及工具用例通过。GOWORK=off go test ./relay/channel/claude -count=1:全包通过;提交前再次复核 3.660s。GOWORK=off go test ./service -run 'TestCalculateTextQuotaSummary|TestUsageFromOpenAIBillingUsage|TestUsageBillingPathForLog|TestClaudeCacheOnlyUsageRemainsBillable|TestPostTextConsumeQuotaLogsMissingUsageAsStructuredContent' -count=1 -v:22 项顶层测试通过。relaykit、tokenkit分别执行GOWORK=off go test ./... -count=1:通过;三个模块分别go build ./...、go vet ./...:退出 0。GOWORK=off go test ./... -count=1:44 包通过、3 包失败;controller 的 SQLite 临时文件清理锁、service affinity 计数串扰、HTTP/2 socket 错误文本断言。官方 HEAD overlay 复现前两类,HTTP/2 单独重测通过;不声称 Windows 全 root 通过。51986f9371d9c5c4c63578c8d34e09798c74e6d4,Go1.26.10 linux/amd64;root、relaykit、tokenkit 分别执行GOWORK=off go test ./... -count=1全部通过。验证 workflow 位于 fork 独立分支,不在本 PR diff 中。gofmt -l两文件无输出,git diff --check退出 0。message_delta前;未调用真实供应商。1.27.1,Ubuntu Linux amd64 / Go1.26.10;目标回归无 DB/供应商。既有 service 回归使用本地合成 SQLite/miniredis fixture,不是生产结算。Risks
Scope check
Summary by CodeRabbit