Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(proxy): stop the stream gate from dropping chunks at the exact cap (#5006)
billythegod4325b0851672
diff --git a/packages/anthropic-compat-proxy/src/server.test.ts b/packages/anthropic-compat-proxy/src/server.test.tsindex 6114b9f7cf..854f291cc4 100644--- a/packages/anthropic-compat-proxy/src/server.test.ts+++ b/packages/anthropic-compat-proxy/src/server.test.ts@@ -4289,3 +4289,25 @@ describe('streaming response validity gate (#2242)', () => {await expect(post(proxy.url, { model: 'test-model', stream: true })).rejects.toThrow();});});++describe('anthropic-compat-proxy stream gate pending-buffer boundary', () => {+ it('commits every gated byte when the pending buffer lands exactly on the gate cap', async () => {+ // 回归:入队条件曾是 `pendingBytes < CAP` 而拒收条件是 `> CAP` —— 累计恰好+ // 落在 64KiB(回环读常见块大小)时, 后续 chunk 只计数不入队; 事件标记在这些+ // chunk 里到达并提交后, 它们被永久跳过, 客户端收到中间有缺口的 200 SSE。+ const pad = 'x'.repeat(64 * 1024);+ const marker = 'event: message_start\ndata: {"type":"message_start"}\n\n';+ const upstream = await startFakeUpstream((_idx, _body, res) => {+ res.writeHead(200, { 'content-type': 'text/event-stream' });+ res.write(pad);+ res.write(marker);+ res.end();+ });+ upstreamClose = upstream.close;+ proxy = await createAnthropicCompatProxy({ upstream: upstream.url });+ const result = await post(proxy.url, { model: 'test-model', stream: true });++ expect(result.status).toBe(200);+ expect(result.text).toBe(pad + marker);+ });+});diff --git a/packages/anthropic-compat-proxy/src/server.ts b/packages/anthropic-compat-proxy/src/server.tsindex aa201e7e30..42075bd5be 100644--- a/packages/anthropic-compat-proxy/src/server.ts+++ b/packages/anthropic-compat-proxy/src/server.ts@@ -1638,12 +1638,13 @@ function forward(}if (!streamGateCommitted) {// 门控中(未提交):积累待发字节并判定是否可提交。- if (pendingBytes < STREAM_GATE_PENDING_CAP_BYTES) {- pendingChunks.push(chunk);- pendingBytes += chunk.length;- } else {- pendingBytes += chunk.length;- }+ // 无条件入缓冲:曾用 `pendingBytes < CAP` 做入队条件, 累计恰好落在 CAP+ // (64KiB, 回环读的常见块大小)时, 后续 chunk 只计数不入队 —— 一旦事件+ // 标记在这些 chunk 里到达并提交, 这些字节被永久跳过, 客户端收到中间+ // 有缺口的 200 SSE。任何把 pendingBytes 推过上限的 chunk 都会在该+ // chunk 内提交或按 502 拒收, 缓冲仍有界(至多 CAP + 单 chunk 溢出)。+ pendingChunks.push(chunk);+ pendingBytes += chunk.length;if (!isSse) {if (canInferSse) {pendingText += chunk.toString('utf8');