Repository navigation
流式响应超过 256 KiB 时 usage(含 cached_tokens)丢失,缓存命中统计退化为未知 - #1
Merged
Merged
Conversation
Owner
|
Thanks for your submission, this issue has been approved for merging. |
Contributor
Author
|
Merged — thanks! The >256KiB stream shape that hides usage in the tail chunk is exactly the class the corpus covers; more tail-vs-header shapes are there if useful: |
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.
版本
commit
25b48e69cf472acc8fb1044bafd0a668d0c9e51e(main, 2026-09-08)现有行为
app.py的stream_body()(流式代理路径)只捕获 SSE 流的前 262144 字节:而 llama.cpp(以及 OpenAI 兼容流式协议)把
usage放在最后一个 chunk。因此当一次流式响应的 SSE 总字节超过 256 KiB 时,usage 行根本不在捕获文本里,response_text_cache_stats提取不到任何东西。实测(真实 app + 模拟 llama.cpp 上游,末 chunk 携带
usage.prompt_tokens_details.cached_tokens=80):/admin/api/messages记录cached_tokens=80, prompt_tokens=100, cache_hit=True(正确)cached_tokens=None, prompt_tokens=None,cache_hit字段缺失——prefix_cache_result事件同样报stats_available=False连带影响:
clear_binding_after_cache_miss()依赖cached_tokens非空,长流下永远不会清绑定;prefix_cache_reused_in_memory)预先置cache_hit=True,长流下没有响应数据能纠偏它。对长输出的 coding agent 场景(本项目的主要目标用法),这恰恰是最容易超过 256 KiB 的场景。
期望行为
无论流多长,最终 usage chunk(含
cached_tokens/prompt_tokens)都应被解析并合入消息记录与prefix_cache_result事件;头部截断只应影响response预览文本,不应影响统计。最小修改
在
stream_body()中额外维护一个滚动的尾部缓冲(64 KiB),流结束后若总长超过捕获窗口,则解析尾部并合并非空统计。补丁见附件(31 行,含注释):cached_tokens=80, prompt_tokens=100, cache_hit=Truepytest -q:10 passed复现 fixture
模拟上游返回三条 SSE:一条大 content delta(~300 KiB)、一条携带
usage的末 chunk、data: [DONE]。fixture 由 AgentMeasure 测试构造,无需安装额外工具即可用任意 mock 上游复现。