Skip to content

fix: 流的结局上桌——access log 补 proxy_stream 行 - #102

Merged
YoungSx merged 1 commit into
mainfrom
diagnose-llm-proxy-stream-drops
Sep 7, 2026
Merged

fix: 流的结局上桌——access log 补 proxy_stream 行#102
YoungSx merged 1 commit into
mainfrom
diagnose-llm-proxy-stream-drops

Conversation

@YoungSx

@YoungSx YoungSx commented Sep 6, 2026

Copy link
Copy Markdown
Owner

背景

线上诊断「LLM 反代总断流」的直接教训。两轮 wrangler tail(82 事件)证实:

  • gh.shangxin.me 路由 60 请求中 37 个 exceededCpu,cpuTime 全部钉在 10ms = 免费版限额签名。凶手是 ClaudeBot 爬 GitHub 大 HTML 页,而该路由开了 bodyRewrite,HTMLRewriter 在免费版 10ms CPU 预算内根本改不动整页。超时开多大都救不了——路由四段超时只管墙钟/CPU 等待,不管 HTMLRewriter 的 CPU 计费。
  • 库的四段超时与 SSE 豁免设计本身没问题,文档也没缺口;真正的缺陷在 observability.ts。

本 PR 修什么

1. 流的结局落地(logsReceiver)

ProxyEvent.stream 早就把 StreamReport(first_chunk_timeout/idle_timeout/upstream_reset/client_closed/complete + bytes + durationMs)以 Promise 形式交给了 host,但 logsReceiver 从不消费——流被掐断在日志里永远读作 outcome:"ok"、status 200,这正是断流查无可查的原因。

现在:流结束时补一行结构化 JSON:

{"message":"proxy_stream","routeId":"bifrost","requestId":"...","upstream":"...","path":"/v1/chat","outcome":"idle_timeout","bytes":512,"durationMs":9800}

与请求行靠 requestId 对上。设计上仍然两行而非合并一行:请求行按库的既有约定在 body 被读之前打,合并意味着把每条 LLM 流的日志 hold 到分钟级。

2. 免费版 CPU 限额写进 README(Body rewriting 一节)

把这次诊断的签名与判据沉淀下来:cpuTime 钉死 10ms 的 exceededCpu = 免费版;CPU 是按请求终止(官方文档对 memory 才写 isolate 级行为,此处措辞已对齐);SSE 豁免改写所以 LLM 流不吃这笔账。

验证

  • npm run check 全绿(246 + 26 + 101 测试)
  • 新增 3 个测试:proxy_stream 行落地、无流时不打、report reject 不带走请求行

部署侧建议(不在本 PR,需线上决策)

  1. gh.shangxin.me 关 bodyRewrite(免费版改不动 GitHub 量级页面),或封 ClaudeBot,或升级付费计划。
  2. LLM 路由断流的直接归因:tail 窗口内未捕获任何 POST 流式请求,下一轮复现时再抓,proxy_stream 行上线后即可见。

🤖 Generated with Claude Code

线上诊断的直接教训:access log 只在 body 被读之前打一行,流被
deadline 掐断、被上游重置、被客户端挂断,全都落在 ProxyEvent.stream
的 report 里,而 logsReceiver 从不消费它——断流在日志里读作
outcome:"ok"。现在流结束时补一行 proxy_stream(outcome/bytes/
durationMs),与请求行靠 requestId 对上。

CPU 限额段落进 Body rewriting:免费版 10ms 是 CPU 时间不是墙钟,
GitHub 量级的页面在 body 流完之前就击杀;签名是 cpuTime 钉死 10ms
的 exceededCpu。SSE 豁免改写所以 LLM 流不吃这笔账。

Co-Authored-By: Claude <noreply@anthropic.com>
@YoungSx
YoungSx merged commit 334bccf into main Sep 7, 2026
1 check passed
@YoungSx
YoungSx deleted the diagnose-llm-proxy-stream-drops branch September 7, 2026 01:07
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