Skip to content

fix(aws): record cancellations and sync partial usage - #6628

Open
ifoxgege wants to merge 1 commit into
QuantumNous:mainfrom
ifoxgege:codex/fix-aws-cancel-accounting
Open

fix(aws): record cancellations and sync partial usage#6628
ifoxgege wants to merge 1 commit into
QuantumNous:mainfrom
ifoxgege:codex/fix-aws-cancel-accounting

Conversation

@ifoxgege

@ifoxgege ifoxgege commented Aug 3, 2026

Copy link
Copy Markdown

⚠️ 提交说明 / PR Notice

Important

  • 本 PR 的代码与说明由 AI 协助完成,并由提交者发起,请维护者重点复核取消计费语义。

📝 变更描述 / Description

这是 #6589 的后续修复,补齐 AWS Bedrock 流取消后的日志和部分 usage 结算:

  • AWS 自定义 EventStream 循环现在写入 RelayInfo.StreamStatus,区分 client_gonetimeoutdoneeof,从而与公共 StreamScannerHandler 的日志行为一致。
  • Claude 流在拿不到最终 message_delta 时,会根据已累计文本更新部分 CompletionTokens,并重新构建 BillingUsage,避免继续采用 message_start 中常见的 output_tokens=1
  • BuildMessageDeltaPatchUsage 会在上游缺失输出 usage 时,从已归一化的语义 usage 补齐 OutputTokens;输入与 cache 字段仍保持独立语义。
  • 回归测试覆盖客户端取消状态、多 Token 部分输出和高 cache 占比下的 usage 保留。

实际复现中,Claude 原生流在客户端收到约 50 KB 后断开,旧逻辑仍以 output_tokens=1 结算;该修改确保部分输出计数同步到最终计费 usage,同时保留 Bedrock 上游取消。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 Issues 和 PRs,未发现覆盖 AWS client_gone 与取消后 stale BillingUsage 的重复修改。
  • Bug fix 说明: 本修改修复 fix(aws): cancel Bedrock requests on client disconnect #6589 合入后仍可复现的 AWS 取消日志缺失与部分输出少计费问题。
  • 变更理解: 已追踪 AWS EventStream、Claude finalizer、BillingUsage 选择与日志序列化链路。
  • 范围聚焦: 仅修改 AWS/Claude 流取消状态、usage 映射及对应测试。
  • 本地验证: 已运行并通过下列测试和独立构建。
  • 安全合规: 代码中无敏感凭据。

📸 运行证明 / Proof of Work

go test ./relay/channel/aws ./relay/channel/claude ./service
ok github.com/QuantumNous/new-api/relay/channel/aws
ok github.com/QuantumNous/new-api/relay/channel/claude
ok github.com/QuantumNous/new-api/service

cd relaykit && GOWORK=off go build ./...
PASS

cd relaykit && GOWORK=off go test ./...
PASS

新增断言确保:

  1. 客户端取消后 StreamStatus.EndReason == client_gone
  2. 部分输出的 BillingUsage.ClaudeUsage.OutputTokens 与本地累计的 CompletionTokens 一致且大于 1;
  3. 高 cache 样本仍保持净输入、cache read、cache creation 三类字段独立。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant