Repository navigation
Description
TL;DR
The hardcoded STREAM_IDLE_TIMEOUT_MS = 30000 aborts upstream requests whose first-token/prefill stall exceeds 30s. The official CLI has no idle timeout at all (verified in command-code@1.50.0 deobfuscated source) and completes 12+ minute stalls fine. With reasoning models (e.g. z-ai/glm-5.3-flash at high effort), >30s stalls are normal, so the watchdog kills healthy requests and triggers a 429 retry loop that re-sends the full prefill every time. Proposal: make both timeouts configurable via env vars (CC_STREAM_IDLE_MS / CC_NONSTREAM_IDLE_MS), keeping current defaults unchanged, following the existing CC_* env-var convention (CC_USE_PROVIDER_MODELS, CC_EMPTY_SYSTEM_PLACEHOLDER). No protocol changes, no fingerprint changes, watchdog semantics preserved.
现象
模型:z-ai/glm-5.3-flash,thinking effort: high(Go 套餐,/alpha/generate 协议)。
同一个账号、同一种协议,两条路径对比(各约 60 次请求的真实会话统计):
| 路径 | 结果 |
|---|---|
直接使用 /alpha/generate 协议的客户端(无中间层) |
55 次请求,0 次错误,其中最长一次流内停顿 713 秒后正常完成 |
| 经本 proxy 转发 | 58 次请求,47 次错误(429 Response timeout / terminated / zero output tokens) |
失败分类(反代路径):7 次 Empty response from upstream (zero output tokens)、2 次 429 Response timeout - request timed out、1 次 429 try reducing context length(连续超时话术,即连续 3 次超时后的提醒)、其余为连接被掐断(terminated)。其中 429 系列确定来自 proxy 自身的 30s 空闲看门狗;terminated 部分叠加了我方 nginx 60s proxy_read_timeout 的因素(已在我侧调大解决),但根因相同——上游停顿 >30s 就会被判定为异常。
所有失败请求的日志特征一致:elapsedMs ≈ 30000+、bytesReceived = 0(或首轮 reasoning 事件后停顿)。也就是说只要上游开始持续吐流,30s 空闲计时器不会触发(我也观察到 574s/607s 的长思考正常完成);触发的是 prefill/首 token/思考段切换造成的 >30s 停顿——推理模型下这是正常现象,不是死连接。
对官方 CLI 行为的验证(why 30s is too aggressive)
反编译 command-code@1.50.0(npm 官方包)的传输层:
// createCommandApiClient / send():
const a = e.timeout ? setTimeout(() => i.abort(), e.timeout) : nullCLI 源码中所有 createApiClient({ baseUrl }) 调用点都没有传 timeout,即官方 CLI 对 /alpha/generate:
- 请求阶段:无总超时、无空闲看门狗,无限等待
- 读流阶段:无每 chunk 空闲限制,同样无限等待
而实测官方 CLI 路径下 700+ 秒的停顿可以正常完成。因此 30s 看门狗会把官方 CLI 认为完全健康、并且最终能成功返回的请求提前 abort。
附带成本:重试放大
被 abort 后 proxy 返回 429 + retry_after: 5,SDK 自动重试 = 完整重发整个上下文。对长上下文会话,这意味着每次误杀都重付一次全量 prefill 的 token/算力,短时间内产生重复大请求——这对用户配额和服务端都是额外负担(从风控视角看,"反复重发同一个大 prefill"的模式比一次 12 分钟的长等待更显眼)。
建议(最小改动)
把两个常量改为环境变量可配,默认值保持 30s/90s 不变(不改变现有用户的默认行为),命名沿用仓库已有的 CC_* 环境变量约定(如 CC_USE_PROVIDER_MODELS、CC_EMPTY_SYSTEM_PLACEHOLDER):
-const STREAM_IDLE_TIMEOUT_MS = 30000; // 30s — 流式无新数据中断
-const NONSTREAM_IDLE_TIMEOUT_MS = 90000; // 90s — 非流式超时更宽容
+const STREAM_IDLE_TIMEOUT_MS = Number.parseInt(process.env.CC_STREAM_IDLE_MS ?? '', 10) || 30000;
+const NONSTREAM_IDLE_TIMEOUT_MS = Number.parseInt(process.env.CC_NONSTREAM_IDLE_MS ?? '', 10) || 90000;看门狗本身不建议移除(防假计费、防连接悬挂的价值都在),只是把阈值交给部署者——推理模型用户可调大,普通场景维持现状。
我本地验证过把流式空闲调到 300s 后,之前必现的 429 循环完全消失,且看门狗语义(Zero-Output Guard、连续超时提示、Retry-After)保持不变。
与 master 最新改动的关系(487f219f)
我确认 master 最新提交 487f219f(2026-09-08)已经:
- 在 Anthropic 端点加入了 15s 静默后的
event: pingkeepalive(很好,正击中下游首字节超时问题); - 修复了 finish 事件缺 usage 时的假 zero-output 429。
但两个空闲超时常量在当前 master 的 proxy.mjs 中仍是硬编码 30s/90s,OpenAI /v1/chat/completions 流式路径也没有对应的 keepalive 注释——本 issue 的诉求不受上述改动影响。
可选的后续增强(不阻塞本 issue)
- 把 15s keepalive ping 同样注入 OpenAI SSE 路径(SSE 注释帧
: keep-alive即可),让 proxy 前面的 nginx/CDN 的 read timeout 不再掐断长停顿的流。这是社区同类项目的常见做法(如 Nir-Bhay/commandcode-cursor-proxy 内置 15s ping)。 - (更远期)
config.json+ env 双通道可配,与emptySystemPlaceholder的做法一致。
Activity
perf/fix: 下游背压 + 客户端僵死保护 + NDJSON 行切分 O(n²)/定时器滞留(issue #20)
MAXeaglet commentedon Sep 10, 2026
Update: this is now implemented in
99ab485(pushed tomaster/release/test). Both timeouts are configurable via env vars with defaults unchanged (30 s / 90 s), exactly as you proposed — no protocol or fingerprint changes, watchdog semantics preserved.Invalid values (empty /
0/ negative / non-numeric) fall back to the defaults, so a typo can't accidentally disable the watchdog. The effective values are printed at startup:Why this became a ~6-line change
Your proposal was the right shape from the start; what made it cheap was a refactor that landed in
88a1872for an unrelated reason. The four read loops previously inlined the timer:Every chunk allocated a fresh
setTimeout, and whenreaderwon the race the timer was never cleared — roughly 225 B retained per pending timer, so steady-state retention scaled asthroughput × window × chunks-per-response × 225 B(at 50 rps × 2000 chunks × 30 s ≈ 644 MB, and the non-stream window is 3× longer). That was fixed by consolidating all four sites into a single factory usingtimer.refresh(), which also preserved your log signature's cause — the timeout still measures only thereader.read()wait and resets on each chunk:So this issue turned into reading two env vars instead of touching four call sites.
Verification
stream 30000ms / nonstream 90000ms— nothing changed for existing deploymentsCC_STREAM_IDLE_MS=123456→stream 123456ms;CC_NONSTREAM_IDLE_MS=654321→nonstream 654321ms""/0/-5/abc→ all fall back to30000429+Stream idle timeoutlog), including when set purely via the env var[DONE]and usage intact, slow client (20 ms/chunk) and 30 concurrent streams complete normallyOn the substance of your report
I don't have anything to add against your evidence, and I'd rather record it here than let it get lost: the deobfuscated
command-code@1.50.0showing everycreateApiClient({ baseUrl })call site passes notimeout, plus the 700+ second stall completing, is a much stronger basis than my own reasoning was. Your controlled comparison (same account, same/alpha/generateprotocol: 55 requests / 0 errors direct vs 58 requests / 47 errors through the proxy) is what makes it conclusive rather than circumstantial.The retry-amplification point deserves emphasis: a false kill doesn't just fail one request, it returns
429 + retry_afterand the SDK resends the entire context, so on long sessions each false kill re-pays a full prefill. That's now called out in the README's new "Upstream Idle Timeouts" section alongside theelapsedMs ≈ 30000+bytesReceived = 0signature you identified, so the next person hitting it can self-diagnose and raise the limit.One deliberate non-change: the 30 s default stays. I agree with your framing that the official CLI's behaviour is "no timeout at all", but this proxy also serves as a guard against genuinely dead connections, and changing a default is a behaviour change I didn't want to bundle silently. If you think 30 s is indefensible as a default for reasoning models — not just annoying — say so and I'll move it; that's a maintainer call, not a technical one.
Thanks for the report — making the timeouts single-sourced was possible because your writeup was precise about where the watchdog lives and what it measures.
feat: 上游读空闲超时可配置(CC_STREAM_IDLE_MS / CC_NONSTREAM_IDLE_MS)— issue #19
Merge upstream/master (99ab485) into local master
MAXeaglet commentedon Oct 9, 2026
已支持环境变量配置:可通过 \CC_STREAM_IDLE_MS\(流式空闲超时,默认 30000ms)与 \CC_NONSTREAM_IDLE_MS\(非流式超时,默认 90000ms)自定义超时时间,长思考模型调大即可。