Skip to content

[PR] Fix: text stream swallowed when gateway sends empty array tool_calls:[], client receives empty stream (#4466) | 修复:网关发空数组 tool_calls:[] 时文本流被吞、客户端收到空流(#4466) - #4470

Merged
ranxianglei merged 2 commits into
masterfrom
2026-10-24_empty-tool-calls
Oct 24, 2026

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Fixes #4466

现象(人话)

腾讯 CodeBuddy 网关(copilot.tencent.com)在每个普通文本块里都带一个空数组 "tool_calls": []。代理把它当真值 → 文本块全被归类成「工具调用帧」→ 客户端可见字节被扣住等工具结算(永远不会来)→ 客户端收到空流(「Model returned no valid response」)。不走代理一切正常。

根因

src/loop/adapter-openai.ts:528 裸真值判断:

const frameToolCalls = delta?.tool_calls as ... | undefined;
if (frameToolCalls) absorbToolCallDeltas(frameToolCalls);

[] 在 JS 里是真值。同文件 filterRealToolFragments 早已用 !tcs || tcs.length === 0 → keep —— 两处判定不一致。

修复

空数组当缺失(与 filterRealToolFragments 同一谓词):

const frameToolCalls = Array.isArray(delta?.tool_calls) && delta.tool_calls.length > 0 ? ... : undefined;

非空 tool_calls 行为完全不变(真工具流照旧吸收/重放)。

测试(both-direction #3617)

tests/issue4466-empty-tool-calls.test.ts:

  • ✅ 带 tool_calls:[] 的文本块 → 文本事件 + 客户端可见字节都带 HELLO(修复前此测试红,已做负控制验证);
  • ✅ 真工具流(分片 arguments)照旧吸收重放;
  • ✅ 混合流(空数组文本块 + 真工具调用)两者都活。

相邻面 758/758(toolargs-adversarial / absorb-emission / issue1881 / issue1502 / host-usage-postfold / issue1455)+ typecheck 干净。完整 CI 以 push 后的 test lanes 为准。

config surface

无。

影响面

所有 OpenAI 兼容车道(/bili/ 重写 + cert-MITM);报告者实测 0.1.203/0.1.204/0.1.205/master 全带此代码路径,非 203/204 回归,是一直存在的接线 bug。

Some OpenAI-compatible gateways (Tencent CodeBuddy, copilot.tencent.com)
stamp "tool_calls": [] on every chunk of a normal text response. The
bare truthiness check classified each text frame as a tool-call frame,
withholding its client-visible bytes for a tool settle that never comes
— the client received an empty stream ("Model returned no valid
response"). An empty array is now treated as absent (same predicate as
filterRealToolFragments); non-empty tool_calls behavior is unchanged.
@ranxianglei ranxianglei changed the title 修复:网关发空数组 tool_calls:[] 时文本流被吞、客户端收到空流(#4466) [PR] Fix: text stream swallowed when gateway sends empty array tool_calls:[], client receives empty stream (#4466) | 修复:网关发空数组 tool_calls:[] 时文本流被吞、客户端收到空流(#4466) Oct 24, 2026
@github-actions

github-actions Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-24_empty-tool-calls (89ea820)

Latest build — install this exact version

npm install -g billion-context@0.0.5374-pr.4470.2362e7e

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Recent builds (commit → version)

commit version build
89ea820 billion-context@0.0.5374-pr.4470.2362e7e build
1c45013 billion-context@0.0.5370-pr.4470.1ff90b6 build

Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr4470.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

查重:重复——三重撞车。 这个修复已经有三个 open PR 在跑同一个机制:

条目 时间 基线 CI(当前 head) 测试文件
issue #3926 + PR #3954(最早) 10-20 master@1cec7add(已落后) head d2e5321 全绿(30 success/14 skipped),但基线旧 tests/issue3926-empty-tool-calls.test.ts
PR #4469(#4466 的 daemon 修复) 10-24 01:43 master@e5183631(落后) head e749375 有 40 个 cancelled run,无完整绿 tests/issue4466-empty-toolcalls.test.ts
PR #4470(本 PR) 10-24 02:29 master@76254c95(最新) in_progress(25 success/13 skipped/12 running,暂无失败) tests/issue4466-empty-tool-calls.test.ts

三者症状相同(网关每块带 tool_calls: [] → 文本流被吞、客户端空流)、机制相同(src/loop/adapter-openai.ts:528 裸真值判定把空数组当工具帧)、根因相同、修法同为「长度 0 视为缺失」的一行谓词。按先来后到,保留最早的 #3954;后到的 #4469/#4470 应并入。按规矩我不执行关闭,由 owner 定夺:建议 rebase #3954 到当前 master 重跑 CI 后合入,#4469/#4470 关为 duplicate(或 owner 若更想要新基线,把 #4470 的测试文件 cherry-pick 进 #3954 分支也行——谓词本身三版语义等价)。注意三个 PR 的回归测试文件名互不相同,全合并会出现三份重叠测试。

标签: bug / scope/wiring / p1 / duplicate —— wire 帧误分类导致受影响网关经代理完全拿不到正文(可用性停摆,p1);与 #3954/#3926、#4469 同症状同机制,打 duplicate 待 owner 收拢。客户端标签不打:CodeBuddy 是上游网关不是 agent 客户端,且修复覆盖所有 OpenAI 兼容车道。

本地独立验证进行中(根因代码路径复核 + 定向测试 + 负控制),完整八问 review 结论随后补一条。

@ranxianglei ranxianglei added bug Something isn't working duplicate This issue or pull request already exists p1 scope/wiring labels Oct 24, 2026
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

PR #4470 独立复审完成(重读 diff、重走代码路径、本地定向验证 + 负控制复做,未照抄 issue/上游分析)。

先说方向关(① 审原 issue):#4466 的诊断在 master 代码上独立复核成立——src/loop/adapter-openai.ts:528 裸真值判断确实把 [] 当工具帧;同文件 :34 filterRealToolFragments 已是 !tcs || tcs.length === 0 → keep,两处谓词不一致属实。故障链我逐段走过:文本帧 {content:"HELLO", tool_calls:[]} 进 absorb 分支 → 字节存入 rawToolChunks 等结算、发出的 text 事件不带 raw(无 raw 的 text 事件不转发客户端)→ finish 时 settleToolCalls() 因 pending.size === 0 早退(空数组吸收不进任何东西)→ 缓冲块永不重放,#1881 prose 救援也在 settle 内、早退之后,同样不触发 → 客户端可见字节为零。修的是机制本身(帧分类谓词),不是治标。

八问:

  1. 是什么问题:网关在每个普通文本块上盖空数组 tool_calls:[] 时,代理把整条文本流误判成「工具调用帧」,扣住全部客户端可见字节等一个永远不会来的工具结算 → 客户端收到空流("Model returned no valid response")。
  2. 什么原因导致的:JS 里 [] 是真值,adapter-openai.ts:528 裸真值判断把空数组归类为非空工具帧;与同文件 filterRealToolFragments 的长度感知谓词不一致。
  3. 影响范围与严重程度:所有 OpenAI 兼容车道(/bili/ 重写 + cert-MITM);触发条件 = 上游网关对文本块盖空 tool_calls 数组(CodeBuddy 每块都盖,WorkBuddy 家族 MITM/streaming proxy: First-round text is silently discarded when an upstream chunk carries an empty tool_calls array | MITM/流式代理: 上游 chunk 携带空 tool_calls 数组时第一轮文本被静默丢弃 #3926 同症状)。严重度 p1:完全不可用。非版本回归——该代码路径 0.1.203/204/205/master 全在。
  4. 怎么修复的:谓词收敛,length-0 当缺失(Array.isArray(x) && x.length > 0 ? x : undefined),与 filterRealToolFragments 同一判定。非空 tool_calls 流的行为逐字节不变。
  5. 是否有回归问题:逐项清点过既有行为——① 真工具流(分片 arguments 吸收/重放):不变,test 2 + absorb-emission / toolargs-adversarial / issue1502 / fix-1039 钉住;② 缺失 tool_calls:Array.isArray(undefined) = false,走原文本分支,不变;③ 纯占位块(无 content/reasoning、只有 []):旧码缓冲后 settle 丢弃、新码直接跳过,两者客户端可见均为 0 字节,净等价;④ finish/hadToolCalls/结算逻辑:空数组本来就不进 pending,不变;⑤ wire 顺序:混合流保持原始顺序(test 3 钉住)。高危模式逐项过:无语义漂移、无 fail-fast 变吞错、无阈值/时序/输出格式变化。
  6. 是否破坏缓存:不涉及。改动只在 parse 期帧分类;非空路径转发字节不变;空数组路径此前发给客户端 0 字节、现在正确发文本(这正是修复),前缀缓存接缝不受影响。
  7. 是否改了设计方向:不涉及。单行谓词收敛,架构/边界/数据流未动。
  8. 是否引入新配置:不涉及(config surface:无,与 PR 声明一致)。

行为变更披露(旧→新):tool_calls: [] 帧——旧:归为工具帧,文本字节被永久扣留(客户端空流);新:当缺失,文本即时转发。其余任何输入形状行为零变化。

测试有效性(④):真入口 createOpenaiAdapter().parseStream,断言有牙(text 拼接精确相等、raw 字节含 HELLO、toolCalls deepEqual)。负控制我独立复做:还原 master 谓词 → test 1 红、test 2/3 绿;恢复修复 → 3/3 绿。both-direction(#3617)成立。

diff 整洁度(⑤/⑤b):2 文件 +135/-1——一行谓词 + 讲「为什么」的注释(带 issue 引用)+ 自包含测试文件。无无关改动、无死代码、命名与注释风格合仓库惯例。过。

验证证据:

  • 本地定向:新测试 3/3;相邻 8 个文件 770/770(toolargs-adversarial / absorb-emission / issue1881×2 / issue1502 / host-usage-postfold / issue1455 / fix-1039);typecheck 干净。
  • 远程 CI(head 1c45013,base = 当前 master tip 76254c9,三者中最新):12 个 test shard 全绿(ubuntu/windows × node 22/24)、static-gates 全绿、guard 作业全绿(kernel-guard / lane-guard / config-surface-guard / version-guard / lock-sync 等);e2e/real-client 车道按三层 CI(feat!: remove image pre-compression and the image_full tool (#3266) #3295)设计跳过(PR 只跑 CORE 矩阵,ci:full 标签或 cron 才扩全量)。合入前若想补 e2e 覆盖,打 ci:full 标签即可。

处置建议(重复链,细节见首条回复):#3954(10-20,最早)/ #4469(今晨 01:43)/ #4470(本 PR)是同一机制的三个 PR,修法语义等价。按先来后到建议保留最早的 #3954(rebase 到当前 master 刷一轮 CI 即可,其 head CI 曾全绿但 base 已陈旧);#4469/#4470 打 duplicate 关闭。备选:本 PR base 最新、测试最完整,可把它这个测试文件 cherry-pick 进 #3954 分支后一并收掉。关不关由你定,我不自行关闭。

判定:修复内容本身可合(单行谓词收敛 + 双向回归钉 + CI 全绿);但作为三重重复之一,建议按上面方案收敛为一个 PR 后再合,不要在三个里任选一个单独合入。

@ranxianglei ranxianglei added the backport merge onto the patch train (bugfix-release/**) via the backport bot — set BEFORE merging label Oct 24, 2026
@ranxianglei
ranxianglei merged commit b11dd2a into master Oct 24, 2026
23 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

Backported to bugfix-release/v0.1.206-train (cherry-pick) and CI dispatched on the train head. Patch release will ship it from the tag-based train — master's unreleased work stays out.

@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.783-master.b11dd2a (rolling master tag).

npm install -g billion-context@0.0.783-master.b11dd2a  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

ranxianglei pushed a commit to Chrockution/billion-context that referenced this pull request Oct 24, 2026
… the train (ranxianglei#4474)

A conflicted backport leaves only a PR comment; CI green on the train
head says nothing about fixes that are ABSENT. Before opening the ship
PR, reconcile every backport-labeled merge since the train's base tag
(merge-commit ranges + squash-shape subjects) against the cherry-pick
-x trailers on the train; any no-show renders as a prominent warning
section at the top of the ship PR body. Never fails the run — the owner
may ship without the straggler, but must see it first.

Verified end-to-end against the live repo: stale train (missing the
ranxianglei#4470 pick) -> flags {ranxianglei#4473, ranxianglei#4470}; real train (pick present) -> flags
{ranxianglei#4473} only, both directions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport merge onto the patch train (bugfix-release/**) via the backport bot — set BEFORE merging bug Something isn't working duplicate This issue or pull request already exists p1 scope/wiring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAI adapter drops all content when upstream sends an empty tool_calls array

1 participant