Skip to content

fix(core): preserve no-argument tool calls that stream an empty arguments string - #6250

Merged
wenshao merged 6 commits into
QwenLM:mainfrom
tomsen02:fix/streaming-empty-args-tool-call
Jul 6, 2026
Merged

wenshao merged 6 commits into
QwenLM:mainfrom
tomsen02:fix/streaming-empty-args-tool-call

Conversation

@tomsen02

@tomsen02 tomsen02 commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does

Makes the streaming tool-call parser emit tool calls whose arguments buffer is empty at stream end (as args: {}) instead of silently dropping them, matching the non-streaming conversion path. Rewrites the unit test that encoded the old drop behavior, and adds regression tests at parser level and converter chunk level.

Why it's needed

For tools that take no parameters, some OpenAI-compatible providers stream arguments: "" (or omit the field entirely) and never send an argument fragment — llama.cpp's server, for example, omits arguments when empty and never synthesizes "{}". Today the streaming parser drops such calls wholesale (if (meta?.name && buffer.trim())), while the non-streaming path keeps them with args: {}. When the turn contains only that tool call, the whole turn looks empty and the client raises InvalidStreamError: Model stream ended with empty response text., triggering pointless retries. Qwen Code's own built-in no-argument tools (task_list, cron_list, enter_plan_mode) are in the blast radius.

Fixes #6249

Reviewer Test Plan

How to verify

  1. cd packages/core && npx vitest run src/core/openaiContentGenerator/ — includes the new regression tests: the parser emits an empty-args call (the old test asserted the drop), and the converter chunk path delivers a functionCall with args: {}.
  2. On main, the new parser test should return no-argument tool calls with empty args when buffer is empty fails with expected [] to have a length of 1; on this branch it passes. Full npm run preflight is green.
  3. Optional end-to-end: point the CLI at a mock OpenAI-compatible SSE server that streams {name: "task_list", arguments: ""} + finish_reason: "tool_calls" (script shared in Streaming tool calls with an empty arguments string are silently dropped, causing "Model stream ended with empty response text" retry loops #6249). Before: API error + identical retry requests. After: the tool call surfaces and executes, the conversation completes.

Evidence (Before & After)

Same mock server (streams a no-arg task_list call, llama.cpp-style), same prompt:

Before (v0.19.5):

$ qwen -p "list my tasks"
[API Error: Model stream ended with empty response text.]   (exit 1)

# mock server log — the tool result is never sent back, the client just retries:
request #1 (first round, replies with no-arg tool call)
request #2 (first round, replies with no-arg tool call)
request #3 (first round, replies with no-arg tool call)

After (this branch):

$ npm start -- -p "list my tasks"
Your task list is empty — no active tasks right now.   (exit 0)

# mock server log — the call survives, executes, and the conversation completes:
request #4 (first round, replies with no-arg tool call)
request #5 (contains tool result → replies with final text)

Tested on

OS Status
🍏 macOS ✅
🪟 Windows ⚠️
🐧 Linux ⚠️

Risk & Scope

Low risk, narrow scope: one guard relaxed in streamingToolCallParser.ts (production code), everything else is tests. Behavior changes only for tool calls whose arguments buffer is empty at stream end — previously dropped (then surfacing as a fake "empty response" error with retries), now emitted with args: {} like the non-streaming path. Tool calls with a missing/unknown name are still filtered out, and truncation detection (hasIncompleteToolCalls) is unaffected since an empty buffer has depth 0. No API/config surface changes.

Linked Issues

中文说明

本 PR 做了什么:流式工具调用解析器在收流结束、参数缓冲为空时,不再整体丢弃该工具调用,而是以 args: {} 正常发出,与非流式转换路径(converter.ts)行为对齐。同时把原先固化"丢弃"行为的单测改写为新语义,并在 parser 层和 converter chunk 层补充回归测试。

为什么需要:对无参数工具,部分 OpenAI 兼容服务端会以 arguments: "" 收尾且不再发送任何参数分片(如 llama.cpp 在参数为空时会完全省略该字段)。此时整轮回复看起来既无文本也无工具调用,客户端抛出 Model stream ended with empty response text 并无意义地自动重试。Qwen Code 内置的无参工具(task_list、cron_list、enter_plan_mode)均在影响范围内。

风险与范围:低风险、窄范围——生产代码仅放宽一个判定条件;无名称的工具调用仍会被过滤;截断检测不受影响;无 API/配置面变化。

…ents string

For tools that take no parameters, some OpenAI-compatible providers
stream `arguments: ""` (or omit the field entirely) and never send an
argument fragment. The streaming parser dropped such calls wholesale
(`meta?.name && buffer.trim()`), while the non-streaming path keeps
them with `args: {}` — so a turn containing only that call looked
empty and geminiChat raised "Model stream ended with empty response
text", triggering pointless retries.

Align the streaming parser with the non-streaming path: emit the call
with empty args when the buffer is empty at stream end. Rewrite the
unit test that encoded the drop, and add regression coverage at parser
and converter chunk level.

@doudouOUC doudouOUC left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] buffer.trim() as occupancy signal is now inconsistent with the new empty-buffer-is-valid semantics.

getCompletedToolCalls() correctly treats empty buffers as valid no-arg tool calls, but three other methods still use buffer.trim() to decide whether a slot is "occupied":

  1. addChunk() line ~92 — collision guard existingBuffer.trim() && short-circuits to false for no-arg calls. A second tool call arriving at the same index silently overwrites the first.
  2. findNextAvailableIndex() line ~334 — !buffer.trim() returns a valid no-arg call's slot as "available."
  3. findMostRecentIncompleteIndex() line ~377 — !buffer.trim() classifies a valid no-arg call as "incomplete," potentially misrouting continuation chunks.

This is dormant for standard providers (OpenAI, Anthropic-via-proxy, DashScope assign unique indices per tool call), but would trigger with buggy providers that reuse indices — exactly the llama.cpp-style servers this PR targets.

Suggested fix: Replace buffer.trim() / !buffer.trim() with meta?.name / !meta?.name as the occupancy signal in all three locations:

// addChunk() line ~92:
if (existingMeta?.name && existingDepth === 0 && existingMeta?.id && existingMeta.id !== id)

// findNextAvailableIndex() line ~334:
if (!meta?.name || depth > 0 || !meta?.id)

// findMostRecentIncompleteIndex() line ~377:
if (meta?.id && (depth > 0 || (meta?.name && buffer.trim())))

And add a regression test sending two tool calls at the same index where the first has no arguments.

— qwen3.7-max via Qwen Code /review

for (const [index, buffer] of this.buffers.entries()) {
const meta = this.toolCallMeta.get(index);
if (meta?.name && buffer.trim()) {
if (meta?.name) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] The guard relaxation here (meta?.name only) correctly preserves empty-buffer no-arg tool calls at emission time, but three buffer.trim() checks in unchanged addChunk() code still use "has buffer content" as a proxy for "this slot is occupied by a real tool call." Now that empty buffer is a valid terminal state, those proxies break in multi-tool-call scenarios:

  1. Line 93 (collision detection): existingBuffer.trim() is falsy for an empty-buffer entry, so when a second tool call arrives at the same index, the collision guard is skipped and the first call's metadata is silently overwritten.
  2. Line 149 (replay guard): currentBuffer.trim() is falsy, so a replayed chunk for a completed no-arg call bypasses the guard and corrupts the buffer.
  3. Lines 121 & 345 (continuation routing + findMostRecentIncompleteIndex): !existingBuffer.trim() classifies a completed no-arg call as "incomplete," so continuation chunks from other tool calls get misrouted to it.

None of these paths are covered by the new tests (they require multi-tool-call interleaving). The fix for all three is the same — replace buffer.trim() with a completion signal that accounts for empty-buffer entries, e.g. meta?.name && meta?.id && depth === 0.

— qwen3.7-max via Qwen Code /review

parser.addChunk(0, ' ', 'call_1', 'function1');

const completed = parser.getCompletedToolCalls();
expect(completed).toHaveLength(1);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] This test uses weaker assertions than the empty-buffer test above (line 398). The empty-buffer test uses expect(completed).toEqual([{ id: 'call_1', name: 'function1', args: {}, index: 0 }]) (full shape), while this one only checks toHaveLength(1) and completed[0].args. It would not catch metadata corruption or wrong-index routing.

Suggested change
expect(completed).toHaveLength(1);
it('should return empty args for whitespace-only argument buffers', () => {
parser.addChunk(0, ' ', 'call_1', 'function1');
const completed = parser.getCompletedToolCalls();
expect(completed).toEqual([
{ id: 'call_1', name: 'function1', args: {}, index: 0 },
]);
});

— qwen3.7-max via Qwen Code /review

@wenshao

wenshao commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

✅ Maintainer verification — local build + real-binary E2E

I built this PR locally and verified it end-to-end with a real binary. The fix resolves the reported failure and is protected by load-bearing regression tests. LGTM.

Scope reviewed: head 2a02b5d · 3 files (streamingToolCallParser.ts + 2 test files). The single production change relaxes the emit guard from if (meta?.name && buffer.trim()) to if (meta?.name), wrapping the JSON parse in if (buffer.trim()) so an empty buffer legally yields args: {} — matching the non-streaming path in converter.ts (if (toolCall.function.arguments) … else {}).

Why the old behavior broke a whole turn: when the dropped call was the turn's only content, hasToolCall stayed false and geminiChat.ts threw InvalidStreamError('Model stream ended with empty response text.', 'NO_RESPONSE_TEXT'), which the client then retried.

1. Real-binary end-to-end (the actual user-facing symptom)

Two bundles built from identical source except that one guard line, running the real qwen 0.19.5 binary headless against a fake OpenAI-compatible SSE server that streams a no-argument cron_list call the llama.cpp way — the opener carries arguments:"" and no argument fragment ever follows.

before (pre-PR guard) after (this PR)
result [API Error: Model stream ended with empty response text.] You have no scheduled cron tasks right now.
exit code 1 0
requests the model received 3 — all round-1, hasToolResult:false (retries) 2 — round-1 → round-2 carrying the tool result (messages 2→4)

Every request had stream:true, so the streaming path (where the bug lives) was exercised — the non-streaming path in converter.ts was already correct. After the fix the no-arg call surfaces, cron_list executes, and the turn completes cleanly.

2. Code-level regression guard (mutation A/B)

At head all three new/rewritten tests pass. Reverting only the guard back to && buffer.trim() flips exactly those three (nothing else), with the precise assertions the PR predicts:

So the tests genuinely pin the fix — a future regression would be caught.

Minor nit (non-blocking)

The JSDoc above getCompletedToolCalls() (line ~247) still reads "Only returns tool calls with both name metadata and non-empty buffers." — inaccurate after this change. Worth a one-line update for future readers, but not a blocker.

Verdict

✅ Correct, narrowly scoped, matches the non-streaming path, and regression-protected. No behavior change for tool calls that stream real arguments, nor for name-less / truncated calls (hasIncompleteToolCalls is unaffected — an empty buffer has depth 0). Recommend merge.

Verification: worktree at PR head, npm ci + npm run build, two esbuild bundles (fix vs guard-reverted); targeted vitest suites + one-line mutation A/B; real-binary headless E2E vs a fake SSE server. Screenshots are from the real runs (E2E text is verbatim; the mutation card reproduces the real vitest ✓/✗ and assertion messages).

🇨🇳 中文说明(完整对应)

✅ 维护者验证 —— 本地构建 + 真实二进制端到端

我在本地构建了该 PR 并用真实二进制做了端到端验证。修复确实解决了所报告的故障,且有承重的回归测试保护。建议合并(LGTM)。

审阅范围: head 2a02b5d,共 3 个文件(streamingToolCallParser.ts + 2 个测试文件)。唯一的生产代码改动,是把发出工具调用的判定从 if (meta?.name && buffer.trim()) 放宽为 if (meta?.name),并把 JSON 解析包进 if (buffer.trim())——这样空缓冲区就合法地产出 args: {},与 converter.ts 里非流式路径的行为一致(if (toolCall.function.arguments) … else {})。

为什么旧行为会毁掉一整轮: 当被丢弃的调用是这一轮唯一的内容时,hasToolCall 仍为 false,于是 geminiChat.ts 抛出 InvalidStreamError('Model stream ended with empty response text.', 'NO_RESPONSE_TEXT'),客户端随即进行无意义的重试。

1. 真实二进制端到端(真正的用户可见症状)

用除那一行判定外完全相同的源码构建了两个 bundle,让真实的 qwen 0.19.5 二进制以无头模式打一个伪 OpenAI 兼容 SSE 服务器:它以 llama.cpp 的方式流式返回一个无参数的 cron_list 调用——开头分片带 arguments:"",之后永远不再发送任何参数分片。

(见上方英文处第一张截图)

修复前(PR 之前的判定) 修复后(本 PR)
结果 [API Error: Model stream ended with empty response text.] You have no scheduled cron tasks right now.
退出码 1 0
模型收到的请求 3 次——全部是第一轮、hasToolResult:false(重试) 2 次——第一轮 → 第二轮携带工具结果(messages 2→4)

每个请求都是 stream:true,因此走的正是有 bug 的流式路径(非流式路径本就正确)。修复后,无参调用得以浮现,cron_list 执行,整轮干净完成。

2. 代码层回归保护(变异 A/B)

在 head 上三个新增/改写的测试全部通过。只把那行判定改回 && buffer.trim(),就恰好让这三个测试失败(其余全绿),且失败信息与 PR 预期完全一致:

(见上方英文处第二张截图)

说明这些测试真正"钉住"了修复——将来若回归会被抓到。

小瑕疵(不阻塞)

getCompletedToolCalls() 上方的 JSDoc(约第 247 行)仍写着 "Only returns tool calls with both name metadata and non-empty buffers."——改动后已不准确。建议顺手更新一行,但不是合并阻塞项。

结论

✅ 修复正确、范围收敛、与非流式路径对齐、且有回归保护。对流式返回真实参数的工具调用、以及无名称/被截断的调用均无行为变化(hasIncompleteToolCalls 不受影响——空缓冲区深度为 0)。建议合并。

验证方式:在 PR head 建 worktree,npm ci + npm run build,构建两个 esbuild bundle(修复版 vs 判定回退版);跑定向 vitest 套件 + 一行变异 A/B;真实二进制无头 E2E 打伪 SSE 服务器。截图取自真实运行(E2E 文本逐字照录;变异卡片复现真实的 vitest ✓/✗ 与断言信息)。

wenshao
wenshao previously approved these changes Jul 5, 2026
@wenshao

wenshao commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

Hi @tomsen-ai — thanks for picking this up. The bug is real, the fix direction is right, and the regression tests look good.

Before merging, could you take another pass at @doudouOUC's review comment above? The concern is that this PR relaxes the completion gate in getCompletedToolCalls(), but three other methods in the same parser still use buffer.trim() as a slot-occupancy signal — which is now inconsistent with the "empty buffer is a legal no-arg call" semantics:

  1. addChunk() collision guard (~line 92) — existingBuffer.trim() && short-circuits to false for a no-arg call, so a second tool call landing on the same index silently overwrites the first.
  2. findNextAvailableIndex() (~line 334) — !buffer.trim() returns a valid no-arg slot as "available."
  3. findMostRecentIncompleteIndex() (~line 377) — !buffer.trim() classifies a valid no-arg call as "incomplete," potentially misrouting continuation chunks.

This is dormant on OpenAI / Anthropic / DashScope (unique index per tool call), but the PR description explicitly calls out llama.cpp-style servers — exactly the class of provider most likely to reuse indices.

Suggested fix (from the review): switch the occupancy signal from buffer.trim() / !buffer.trim() to meta?.name / !meta?.name in those three spots, and add a regression test that sends two tool calls at the same index where the first has no arguments.

Happy to take this over if you're busy — just say the word.

@wenshao
wenshao dismissed their stale review July 5, 2026 15:58

request changes

… tool calls

Follow-up to review feedback: after empty buffers became a legal end
state for no-argument tool calls, three parser methods still used
buffer.trim() to decide whether an index slot was occupied. A provider
reusing indices could then silently overwrite a completed no-argument
call (addChunk collision guard, findNextAvailableIndex) or append stray
continuation chunks to it (findMostRecentIncompleteIndex).

Switch the occupancy signal in all three places to the name metadata,
keeping the JSON-completeness check for non-empty buffers. Add
regression tests for both corruption paths and update the stale
getCompletedToolCalls JSDoc.

Co-Authored-By: Claude Fable 5 <[email protected]>
@tomsen02
tomsen02 dismissed a stale review via 0d2b91d July 6, 2026 02:00
@tomsen02

tomsen02 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @wenshao for the thorough verification, and @doudouOUC for catching the occupancy inconsistency — the concern is real, and it's addressed in 0d2b91d.

All three spots now use name metadata as the occupancy signal, with two deviations from the literal snippets in the review, both needed for the fix to actually take effect:

  1. addChunk() collision guard / findNextAvailableIndex() — swapping the outer condition alone isn't enough: an empty buffer then falls into JSON.parse('')'s catch branch ("not complete JSON → reuse the slot") and the overwrite still happens. The parse is now gated on buffer.trim(), mirroring the emit path: an empty buffer with name metadata is already complete.

  2. findMostRecentIncompleteIndex() — the suggested condition (depth > 0 || (meta?.name && buffer.trim())) would classify every named call with a complete non-empty buffer as "incomplete" and bypass the parse check below it. I used the form matching the review's intent instead: (depth > 0 || (!buffer.trim() && !meta?.name)) — an empty buffer only counts as incomplete while no name has arrived yet.

One buffer.trim() occupancy use is intentionally left as-is: the ID-less continuation routing in addChunk() (~line 121). At that point an empty buffer must stay "continuable" — for the llama.cpp-style flow, the first fragment of a with-arguments call can also be an empty string, and its argument fragments then arrive ID-less at the same index; re-routing them away would break argument accumulation. The "empty buffer = complete no-arg call" interpretation is only safe where the call can no longer receive its own fragments (relocation targets and stray-chunk routing), which is exactly the three fixed spots.

Regression tests added for both corruption paths:

  • two tool calls at the same index where the first has no arguments (the requested test) — it fails unless both the collision guard and findNextAvailableIndex() are fixed;
  • an ID-less continuation chunk arriving at a completed index is routed to the actual incomplete call, not appended to a completed no-argument call.

Mutation check: reverting only streamingToolCallParser.ts to the previous commit flips exactly these two tests (the other 61 stay green; converter suite 146/146 unaffected).

Also fixed the stale getCompletedToolCalls() JSDoc from the maintainer review.

@wenshao wenshao left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Critical] streamingToolCallParser.ts:130 — Missing !meta?.name guard in no-ID continuation routing

The PR correctly updated three of four sites that used !buffer.trim() as the "incomplete call" signal to also check !meta?.name. But the inline continuation branch in addChunk() at line ~130 was missed:

// Line 130 — NOT updated (still uses old guard)
if (existingDepth > 0 || !existingBuffer.trim()) {
  actualIndex = index;

Compare with the correctly updated findMostRecentIncompleteIndex (line 383):

if (meta?.id && (depth > 0 || (!buffer.trim() && !meta?.name))) {

A continuation chunk (no ID) arriving at the index of a completed no-arg call enters the !existingBuffer.trim() branch and is appended to the completed call, corrupting its buffer. The in-progress call that should receive the chunk is starved. Two tool calls corrupted simultaneously.

Fix — mirror the name-guard from findMostRecentIncompleteIndex:

const existingMeta = this.toolCallMeta.get(index);
if (existingDepth > 0 || (!existingBuffer.trim() && !existingMeta?.name)) {
  actualIndex = index;

Test gap — The existing test "should not route continuation chunks to a completed no-argument tool call" sends the continuation at index 2 (a completed call with non-empty buffer → JSON.parse branch → findMostRecentIncompleteIndex), never exercising this still-broken inline path. A test that would catch this:

parser.addChunk(0, '{"key":', 'call_1', 'function1');
parser.addChunk(1, '', 'call_2', 'no_arg_function');
parser.addChunk(1, '"value"}'); // continuation at no-arg call's OWN index
// Assert: no_arg_function still has args: {}, function1 gets {key: "value"}

— qwen3.7-max via Qwen Code /review

@wenshao

wenshao commented Jul 6, 2026 •

Copy link
Copy Markdown
Collaborator

Suggestions — commit 096eea5

File Issue Suggested fix
streamingToolCallParser.ts:95,167,379 "Is this slot complete?" check is implemented inline in three locations (collision detection, replay guard, findNextAvailableIndex) with slightly different formulations — the line 130 oversight is a direct consequence of this duplication Extract a private isSlotComplete(index: number): boolean helper and call it from all three sites. The helper would return `meta?.name && depth === 0 && (!buffer.trim()

— qwen3.7-max via Qwen Code /review

tomsen02 and others added 2 commits July 6, 2026 12:03
…ical empty-opener shape

Review follow-up on the no-ID continuation routing at addChunk. Mid-stream,
an empty buffer with name metadata is formally undecidable between "completed
no-argument call" and "canonical opener awaiting its first argument fragment"
(every OpenAI-compatible provider opens with arguments:"" and streams
fragments ID-less at the same index). Routing must favor the canonical shape,
so the guard stays; a new test pins that shape, which the suite previously
did not cover.

The corruption concern from review is instead bounded at emit time: a buffer
polluted by a stray fragment can parse or repair to a non-object value, which
now collapses to {} so a polluted no-argument call still emits empty args.

Co-Authored-By: Claude Fable 5 <[email protected]>
… argument collapse

Review follow-up: a stray fragment that happens to parse as a valid JSON
object is indistinguishable from real arguments at emit time, so log both
the non-object collapse and empty-buffer emissions to aid diagnosis.

Co-Authored-By: Claude Fable 5 <[email protected]>
@tomsen02

tomsen02 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the careful second pass. Before changing anything I ran an empirical A/B on the Critical, and the result is that line ~130 cannot take the name-guard — but the concerns behind both findings are real, and a5db830 + f1efd7f address them at the layer where they are addressable. Evidence below.

1. The suggested edit does not change the routing outcome

With the suggested condition, an ID-less chunk arriving at a completed no-arg call's index skips the first branch — but then enters the else, where JSON.parse('') unconditionally throws, and the catch sets actualIndex = index. Same destination as today. I applied the suggested edit verbatim and ran the proposed test against it: it fails identically — the fragment is still appended to no_arg_function (whose args then repair to the bare string 'value', more on that in §3).

2. The version that would change routing breaks the canonical streaming shape

To make the proposed test pass, an empty buffer with name metadata must be treated as complete and routed away via findMostRecentIncompleteIndex(). But mid-stream, that exact state — empty buffer + name — is what every OpenAI-compatible provider produces after the opener: the first delta carries id + name + arguments: "", and argument fragments follow at the same index without an ID. I implemented the route-away version and the canonical shape immediately misroutes:

parser.addChunk(0, '', 'call_1', 'function1'); // canonical opener
parser.addChunk(0, '{"x":');                   // ID-less fragment — misrouted under route-away
parser.addChunk(0, '1}');

Notably, the existing suite never pinned this shape — which I suspect is why route-away looked viable. a5db830 adds the lock test.

The underlying issue: at this code point, "completed no-argument call" and "opener awaiting its first fragment" are formally indistinguishable; only stream end resolves them. Routing has to favor the canonical case — the pathological one (a provider reusing a completed call's index for another call's ID-less fragment) requires the provider to already be violating index semantics, at which point fragment ownership is unknowable under any policy.

3. What I did take from the Critical: bounding the damage

The A/B surfaced a real secondary bug in the pathological case: the polluted buffer '"value"}' jsonrepairs to the bare string "value", which was emitted as args: 'value' — violating the Record<string, unknown> contract downstream. a5db830 collapses non-object parse/repair results to {} at emit, so a polluted no-argument call still emits args: {}. The first assertion of the proposed test now holds under any routing; the second (recovering the fragment to function1) is unreachable, since the information "which call owns this fragment" no longer exists once the index is reused.

4. Replay guard at line ~157

Same structure as §1: for an empty buffer, the suggested condition enters the guard, JSON.parse('') throws, and the catch falls through to append — behavior unchanged. A version that actually ignored known-ID chunks when meta?.name is set would risk discarding legitimate argument fragments from providers that repeat the tool-call ID on continuations after an empty opener (during issue triage we observed Kimi's coding endpoint stream arguments: "" followed by a "{}" fragment — if such a continuation carries the ID, a name-based ignore drops real arguments). Today a replayed empty opener appends '' — a no-op — and a full replay of a completed with-args call is already caught by the existing parse check. I'd leave this one as-is.

5. Valid-object pollution (latest suggestion)

Agreed — a stray fragment that parses as a valid JSON object is indistinguishable from real arguments at emit time, and logging is the only detector. f1efd7f adds debug logging for both the non-object collapse and empty-buffer emissions.

Happy to adjust if there's a routing policy I've missed — but I couldn't find an edit at line ~130 that helps the pathological case without breaking the canonical one, and the canonical one is every provider's every multi-fragment tool call.

…t tool calls

Review follow-up: after empty buffers became a legal completed state, a
replayed opener (duplicate ID, QwenLM#5107 lineage) could overwrite a completed
no-argument call's name metadata, since the replay guard only engaged on
non-empty buffers.

Swallowing every known-ID chunk at that state would drop ID-bearing
argument fragments for providers whose opener streams empty arguments, so
the guard uses the protocol shape as discriminator: a chunk carrying a
name but no argument content is an opener replay and is ignored; a chunk
with argument content is a continuation and appends. Regression tests
cover both directions.

Co-Authored-By: Claude Fable 5 <[email protected]>
@tomsen02

tomsen02 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Good catch this round — the invariant is right and I've adopted it, with one scope adjustment grounded in the delta protocol. Details:

The finding is real. After empty buffers became a legal completed state, a replayed opener (duplicate ID — the #5107 pathology this guard was built for) could indeed overwrite a completed no-argument call's meta.name, since the guard only engaged on non-empty buffers. Your regression test is correct and is now in the suite.

But the proposed swallow drops legitimate fragments. I applied the suggested implementation verbatim: it passes its own regression test and the existing suite — and silently swallows ID-bearing argument fragments after an empty opener:

parser.addChunk(0, '', 'call_1', 'function1'); // canonical empty opener
parser.addChunk(0, '{"x":1}', 'call_1');       // known-ID continuation — swallowed, args lost

Providers that repeat the tool-call ID on argument fragments exist, and under the swallow their with-args calls execute with {}. The suite had no coverage in either direction.

The protocol itself offers the discriminator. An opener-shaped replay carries a name and no argument content; a continuation carries argument content. 096eea5 scopes the guard accordingly: known-ID chunk at an empty-buffer/named slot is ignored only when it has a name and no argument payload; anything carrying content appends. Both directions are pinned by regression tests (yours verbatim, plus an anti-swallow lock).

Residual ambiguity, stated for the record: a known-ID chunk carrying both a name and argument content is indistinguishable between a name-repeating provider's fragment and a content-carrying duplicate opener. The guard appends — consistent with its original design, which also appends to unparseable buffers — and the emit-time non-object collapse plus the debug logging from f1efd7f bound and surface the residual case.

…ion scan

Review follow-up: pin the null and array branches of the emit-time
non-object collapse, and exercise findNextAvailableIndex scanning past
multiple occupied no-argument slots during collision relocation.

Co-Authored-By: Claude Fable 5 <[email protected]>
@tomsen02

tomsen02 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Both coverage gaps addressed in 852e763: the null and array branches of the emit-time collapse are now pinned, and the relocation test stacks two no-argument calls so findNextAvailableIndex actually iterates past occupied empty-buffer slots (verified the loop body executes). 70/70 parser tests green.

@tomsen02

tomsen02 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

@wenshao All review rounds are now addressed — 096eea5 adopts the replay-guard invariant with the protocol-scoped implementation (opener-shaped replays ignored, ID-bearing argument continuations preserved), and 852e763 closes the remaining coverage gaps. 70/70 parser + 146/146 converter tests green at head. Ready for your re-review whenever convenient.

// Check if this tool call is incomplete. An empty buffer counts only
// while no name has arrived: with name metadata it is a complete
// no-argument call, and stray chunks must not be appended to it
if (meta?.id && (depth > 0 || (!buffer.trim() && !meta?.name))) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Critical] Same !meta?.name guard applied here but missed at line 130 (no-ID continuation fast-path)

The PR correctly updated three of four sites that used !buffer.trim() as the "incomplete call" signal to also check !meta?.name — here, findNextAvailableIndex (~line 379), and the replay guard (~line 167). But the no-ID continuation fast-path at line 130 was not updated:

// Line 130 (unchanged by this PR, not in diff):
if (existingDepth > 0 || !existingBuffer.trim()) {
  actualIndex = index;

A completed no-argument call has depth=0, buffer="", so !existingBuffer.trim() short-circuits to actualIndex = index. A stray no-ID continuation chunk arriving at that index gets appended to the completed call instead of being routed through findMostRecentIncompleteIndex. The non-object collapse guard in getCompletedToolCalls catches non-object pollution, but a stray chunk forming a valid JSON object (e.g. {"x":1}) passes through undetected — emitting wrong args for the no-arg call while the intended target call stalls with truncated args.

The canonical-shape test (should route ID-less argument fragments to a call whose opener streamed empty arguments) still passes with this fix because findMostRecentIncompleteIndex correctly handles the routing.

Suggested fix (at line 130):

const existingMeta = this.toolCallMeta.get(index);
if (existingDepth > 0 || (!existingBuffer.trim() && !existingMeta?.name)) {
  actualIndex = index;

— qwen3.7-max via Qwen Code /review

@wenshao

wenshao commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

@qwen-code /triage

@wenshao

wenshao commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

Maintainer verification — real end-to-end test on Linux 🐧

I built this PR locally and drove it end-to-end through a mock OpenAI‑compatible SSE server (llama.cpp wire shape) under tmux, plus parser‑level and unit‑level differential checks. Summary: the fix does what it claims, and it is verified on Linux — which fills the ⚠️ Linux box in the PR's own test matrix.

Layer BASE (main e5ec601) PR #6250
Live CLI E2E (mock provider) [API Error: Model stream ended with empty response text.], exit 1, 3 identical retries tool executes, final text, exit 0, 2 requests
Standalone parser harness 3 / 8 8 / 8
New unit tests vs old code 9 fail (incl. the headline test) 547 / 547 green
Fix in shipped bundle chunk absent present
Standing [Critical] (line 129) non‑object args "value" emitted collapses to {} (strictly safer than main)

1) Live E2E — the exact symptom from #6249, reproduced and fixed

Mock server streams a single no‑argument task_list call the llama.cpp way (one delta carrying id+name+arguments:"", no argument fragment) then finish_reason:"tool_calls". Same server, same prompt, only the parser differs.

BEFORE — main (fix not applied): the call is dropped → the turn looks empty → API error → identical retries (the tool result is never sent back), then exit 1.

$ qwen -p "list my tasks"
[API Error: Model stream ended with empty response text.]

>>> CLI exit code: 1

---- requests the mock provider received this run ----
request #1 | hasToolResult=false | messages=[system,user]
request #2 | hasToolResult=false | messages=[system,user]
request #3 | hasToolResult=false | messages=[system,user]

AFTER — PR #6250 (fix applied): the no‑arg call survives with args:{}, executes, the result comes back (request #2 now carries assistant,tool), the model replies, exit 0.

$ qwen -p "list my tasks"
Your task list is empty — no active tasks right now.

>>> CLI exit code: 0

---- requests the mock provider received this run ----
request #1 | hasToolResult=false | messages=[system,user]
request #2 | hasToolResult=true  | messages=[system,user,assistant,tool]

Run with an isolated HOME so only env‑var auth is active (OPENAI_BASE_URL→mock, OPENAI_MODEL, OPENAI_API_KEY), QWEN_CODE_ENABLE_AGENT_TEAM=1 to register task_list, mock served inside tmux. The request #N lines are the mock's own request log — on BASE the client re‑asks 3× with no tool result ever sent back; on the PR it round‑trips exactly once and finishes.

2) Standalone parser harness (outside vitest, on the real source)

The issue's deterministic repro plus the corruption edge cases, driven through the compiled parser via tsx:

BASE (e5ec6019c): 3/8      PR #6250: 8/8
FAIL→PASS  llama.cpp no-arg call (id+name, arguments:"") → []  vs  [{args:{}}]
FAIL→PASS  no-arg call not overwritten when a later call reuses its index
FAIL→PASS  null buffer collapses to {}            (main emitted args=null)
FAIL→PASS  array buffer collapses to {}           (main emitted args=[1,2,3])
FAIL→PASS  replayed opener doesn't mutate a completed no-arg call
 PASS→PASS canonical opener (arguments:"") + ID-less fragments → {x:1}   (non-vacuous)
 PASS→PASS ID-bearing fragment after empty opener appends (continuation)  (non-vacuous)
 PASS→PASS truncated call (depth>0) still flagged incomplete              (non-vacuous)

The 3 shared‑pass rows confirm the harness isn't vacuously failing — it's precisely the no‑argument behaviors that changed.

3) Unit before/after (the PR's own claim)

Reverting only the production file to main while keeping the new tests: the headline test fails with expected [] to deeply equal [ { id: 'call_1', … } ], and 9 new tests fail total (all the no‑arg behaviors). On the PR branch the full src/core/openaiContentGenerator/ suite is 547/547 green (15 files). Confirmed the fix is compiled into the shipped bundle (dist/chunks/…) on the PR and absent from a BASE rebuild.

4) Re‑verification of my earlier [Critical] (line 129, name‑guard in no‑ID continuation routing)

I re‑ran my exact scenario on the current HEAD and compared against main:

scenario:  addChunk(0,'{"key":','call_1','function1')   // in-progress at idx 0
           addChunk(1,'',       'call_2','no_arg')        // completed no-arg at idx 1
           addChunk(1,'"value"}')                          // stray ID-less fragment at idx 1

MAIN   → call_2 args = "value"   (non-object string emitted!)   call_1 = {key:null}, hasIncomplete=true
PR     → call_2 args = {}        (emit-time collapse sanitizes) call_1 = {key:null}, hasIncomplete=true

Findings, stated plainly:

  • The no‑argument call is not corrupted on the PR. The emit‑time non‑object→{} collapse (commit a5db830) turns what main emits as a bare string "value" into a safe {}. The PR is strictly safer than main in this exact scenario — so this is not a regression the PR introduces.
  • My originally‑suggested line‑129 guard is a no‑op here. I applied it verbatim and got the identical result (call_2={}, call_1={key:null}): the empty‑buffer else branch falls through JSON.parse('')→catch→actualIndex = index, so the fragment still lands on idx 1 regardless. The guard doesn't reroute; the emit‑collapse is what actually protects the no‑arg call.
  • The starved in‑progress call is detected, not silently delivered. hasIncompleteToolCalls() returns true (depth > 0), which overrides finish_reason → "length" (wasOutputTruncated=true) in converter.ts, so the partial {key:null} is treated as truncation rather than passed off as a finished call.
  • The trigger requires a provider to stream a continuation fragment under a different tool_call index than its opener — not the canonical OpenAI‑compatible wire shape.

Net: my [Critical]'s worry ("no‑arg call corrupted") does not materialize — the no‑arg call is safer than before, and the residual (a starved sibling under a non‑canonical stream) is truncation‑flagged and no worse than main. I'm satisfied this is adequately handled and no longer consider it merge‑blocking.

Notes / caveats

  • CI: Test (ubuntu‑latest, Node 22.x) is green; the win/mac matrix shows skipped on the latest (test‑only) commit; qwen-code-ci-bot has since APPROVED.
  • Local build caveat (not this PR): a full tsc --build here fails only in gitWorktreeService.ts because the installed [email protected] lags the required ^3.36 (allowUnsafeHooksPath missing from its unsafe typings). This is present on main too, is type‑only, and the esbuild bundle is unaffected. I patched the stale .d.ts locally to build — flagging only for transparency.

Recommendation

Verified — the fix correctly resolves #6249 end‑to‑end on Linux, with no regression in the pathological path my [Critical] targeted (in fact an improvement over main). Good to merge from my side.

中文版本(点击展开)

维护者验证 —— Linux 本地真实端到端测试 🐧

我在本地拉取并构建了本 PR,用一个模拟 OpenAI 兼容的 SSE 服务器(llama.cpp 的报文形状),在 tmux 中把 CLI 完整跑通,另外补充了解析器层和单测层的差分验证。结论:修复达到了预期效果,并已在 Linux 上验证通过 —— 正好补上了 PR 自带测试矩阵里 Linux 的 ⚠️。

层次 BASE(main e5ec601) PR #6250
CLI 真实 E2E(模拟 provider) [API Error: Model stream ended with empty response text.],退出码 1,3 次相同重试 工具执行、返回最终文本,退出码 0,2 次请求
独立解析器 harness 3 / 8 8 / 8
新单测 vs 旧代码 9 条失败(含标志性用例) 547 / 547 全绿
修复是否进入发布产物 chunk 无 有
我提的 [Critical](line 129) 发出非对象参数 "value" 折叠为 {}(严格比 main 更安全)

1)真实 E2E —— 复现并修复 #6249 的确切症状

模拟服务器以 llama.cpp 方式流式返回一个无参 task_list 调用(一个 delta 携带 id+name+arguments:"",不再发送任何参数分片),随后 finish_reason:"tool_calls"。相同服务器、相同提示词,仅解析器不同。

修复前 —— main: 调用被整体丢弃 → 整轮看起来为空 → API 报错 → 相同请求不断重试(工具结果从未回传),最终退出码 1。(见上方英文 BEFORE 代码块)

修复后 —— PR #6250: 无参调用以 args:{} 存活、被执行,结果回传(request #2 此时带上了 assistant,tool),模型给出最终回复,退出码 0。(见上方英文 AFTER 代码块)

使用隔离的 HOME,使得仅环境变量鉴权生效(OPENAI_BASE_URL→模拟服务器、OPENAI_MODEL、OPENAI_API_KEY),QWEN_CODE_ENABLE_AGENT_TEAM=1 用于注册 task_list,模拟服务器在 tmux 内部监听。request #N 是模拟服务器自身的请求日志 —— BASE 上客户端重试 3 次且始终没有回传工具结果;PR 上恰好一次往返即完成。

2)独立解析器 harness(脱离 vitest,直接跑真实源码)

将 issue 的确定性复现 + 若干损坏边界,用 tsx 驱动编译后的解析器:BASE 3/8,PR 8/8。其中标志性一条:llama.cpp 无参调用在 main 上返回 [](丢弃),在 PR 上返回 [{args:{}}]。有 3 条在两侧都通过(规范 opener + 分片、带 ID 的续传分片、depth>0 截断检测),证明该 harness 不是"无脑全红",变化的正是无参这一类行为。

3)单测前后对比(PR 自身声明)

仅把生产文件回退到 main、保留新测试:标志性用例以 expected [] to deeply equal [ { id: 'call_1', … } ] 失败,共 9 条新测试失败(全部为无参相关行为)。在 PR 分支上,src/core/openaiContentGenerator/ 全量 547/547 通过(15 个文件)。并确认修复已编译进 PR 的发布产物(dist/chunks/…),而在 BASE 重新打包中不存在。

4)对我此前 [Critical](line 129,无 ID 续传路由缺少 name 守卫)的复验

我在当前 HEAD 上重跑了我给出的确切场景,并与 main 对比:

场景:  addChunk(0,'{"key":','call_1','function1')   // idx 0 进行中
        addChunk(1,'',       'call_2','no_arg')        // idx 1 已完成的无参调用
        addChunk(1,'"value"}')                          // 无 ID 的游离分片落在 idx 1

MAIN → call_2 args = "value"(发出非对象字符串!)  call_1 = {key:null},hasIncomplete=true
PR   → call_2 args = {}     (emit 时折叠净化)      call_1 = {key:null},hasIncomplete=true

结论,直说:

  • 无参调用在 PR 上并未被损坏。 emit 时"非对象→{}"折叠(commit a5db830)把 main 发出的裸字符串 "value" 变成安全的 {}。在这一确切场景里,PR 严格比 main 更安全 —— 因此这不是 PR 引入的回归。
  • 我最初建议的 line‑129 守卫在这里其实是空操作。 我原样应用后得到完全相同的结果(call_2={}、call_1={key:null}):空缓冲的 else 分支经 JSON.parse('')→catch→actualIndex = index,分片仍然落到 idx 1。守卫并不能改路由;真正保护无参调用的是 emit 折叠。
  • 被"饿死"的进行中调用是被检测到的,不会静默交付。 hasIncompleteToolCalls() 返回 true(depth>0),在 converter.ts 中把 finish_reason 覆写为 "length"(wasOutputTruncated=true),因此残缺的 {key:null} 被当作截断处理,而非当作完成的调用交付。
  • 触发它需要 provider 用不同于 opener 的 tool_call index 来流式发送续传分片 —— 并非规范的 OpenAI 兼容报文形状。

综上: 我 [Critical] 担心的"无参调用被损坏"并未发生 —— 无参调用比之前更安全,剩余情形(非规范流下同批次的兄弟调用被饿死)已被截断标记,且不比 main 更差。我认为已被妥善处理,不再视为阻塞合并的问题。

备注 / 说明

  • CI: Test (ubuntu‑latest, Node 22.x) 绿;win/mac 矩阵在最新(仅测试)提交上显示 skipped;qwen-code-ci-bot 之后已 APPROVED。
  • 本地构建说明(与本 PR 无关): 本机完整 tsc --build 仅在 gitWorktreeService.ts 处失败,原因是已安装的 [email protected] 落后于所需的 ^3.36(其 unsafe 类型缺少 allowUnsafeHooksPath)。此问题在 main 上同样存在,仅是类型层面,esbuild 打包不受影响。我在本地给过期的 .d.ts 打了补丁以完成构建 —— 仅作透明说明。

建议

已验证 —— 该修复在 Linux 上端到端正确解决了 #6249,且在我 [Critical] 针对的病态路径上没有回归(反而优于 main)。就我这边而言,可以合并。

@wenshao
wenshao added this pull request to the merge queue Jul 6, 2026
Merged via the queue into QwenLM:main with commit ac123bb Jul 6, 2026
42 checks passed
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.

Streaming tool calls with an empty arguments string are silently dropped, causing "Model stream ended with empty response text" retry loops

3 participants