fix: prevent tool_call_end events for unstarted tool calls #11093
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related GitHub Issue
Closes: #11071
Roo Code Task Context (Optional)
N/A
Description
This PR fixes the root cause of GLM4.5 (and other models) getting stuck in infinite file read loops when used via LM Studio or OpenAI-compatible endpoints.
Root Cause Analysis:
The
NativeToolCallParsermaintains two tracking systems:rawChunkTracker- tracks tool calls by stream index as chunks arrivestreamingToolCalls- tracks active tool calls by ID aftertool_call_startis emittedWhen a model sends a tool call ID without a name (common with GLM4.5), the tool call is tracked in
rawChunkTrackerbut never "started" (notool_call_startevent emitted, not added tostreamingToolCalls).The bug was in
processFinishReason(): it emittedtool_call_endfor ALL tracked calls inrawChunkTracker, including those that never hadtool_call_startemitted. This causedfinalizeStreamingToolCall()to fail with "Attempting to finalize unknown tool call" because the call was never instreamingToolCalls.The Fix:
Added a
hasStartedcheck inprocessFinishReason()to only emittool_call_endevents for tool calls that actually had theirtool_call_startevent emitted. Tool calls without names are now skipped with a diagnostic warning.Test Procedure
NativeToolCallParser.spec.ts:tool_call_endis emitted only for started tool callstool_call_endeventsRun tests:
All 17 tests pass.
Pre-Submission Checklist
Screenshots / Videos
N/A - Backend fix, no UI changes.
Documentation Updates
Additional Notes
This fix addresses the symptoms described in issue #11071 where users saw repeated console warnings:
The fix ensures proper synchronization between the dual tracking systems by respecting the
hasStartedflag that indicates whether a tool call lifecycle was properly initiated.Important
Fixes
NativeToolCallParserto preventtool_call_endevents for unstarted tool calls, adding checks and tests for correct behavior.processFinishReason()inNativeToolCallParser.tsto emittool_call_endonly for tool calls withhasStartedtrue.NativeToolCallParser.spec.tsto verify correcttool_call_endemission and logging behavior.This description was created by
for 24e8d92. You can customize this summary. It will automatically update as commits are pushed.