Repository navigation
fix(lane): 流式 usage 逐帧重复块改为逐字段取最大值,修复 input 被放大 30–1400x (issue #105) - #115
Merged
Merged
Conversation
此前 readSSE 把每个 SSE 帧的 usage 用 Usage.Merge 朴素 += 累加。当上游在 多个帧里重复发送同一份完整 usage(OpenAI include_usage 下常见),或中继把 usage 每帧重发时,input 会被放大 30–1400 倍,客户端据此误触发上下文压缩。 但流式 usage 是累计快照而非 delta,且 Anthropic 把 input(message_start) 与 output(message_delta) 拆到两帧——直接「替换 last-wins」会丢掉 input。故新增 Usage.MergeMax:逐字段取最大值。这样重复整块→数值不变、跨帧拆分→两字段都 保留、累计增长→取最新。Merge(累加)仍保留给 recovery 合并两次真实请求。 新增 usage_test.go 覆盖三种情形。
Owner
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.
解决的问题
issue #105:流式响应里重复的 usage 块被逐帧相加,input 被放大 30–1400 倍,客户端每轮都误触发上下文压缩。
根因
readSSE(internal/lane/http.go) 把每个 SSE 帧的 usage 用Usage.Merge朴素+=累加。流式 usage 是累计快照(不是 delta),当上游在多个帧重复发送同一份完整 usage(OpenAIinclude_usage下常见),或中继每帧重发时,累加就把同一数值加了很多遍。改动
Usage.MergeMax:逐字段取最大值而非相加。readSSE改用MergeMax。为什么不是「替换 last-wins」:Anthropic 把 input(
message_start) 与 output(message_delta) 拆到两个不同帧,直接替换会丢掉 input。MergeMax同时正确覆盖三种情形:重复整块→数值不变;跨帧拆分→两字段都保留;累计增长→取最新。Merge(累加)保留给 recovery 路径合并两次独立真实请求的用量。测试
新增
usage_test.go覆盖上述三种情形;go build ./...与go test ./internal/lane/...全部通过。Closes #105