Skip to content

Commit 2da6ea2

Browse files
awschmederedelauna
andauthored
fix: streaming tool-call argument loss in NativeToolCallParser (Zoo-Code-Org#695) (Zoo-Code-Org#700)
* fix: streaming tool-call argument loss in NativeToolCallParser (Zoo-Code-Org#695) * chore: remove agent-generated changeset file per policy * test: add direct coverage for finalizeRawChunks() in NativeToolCallParser * fix: address PR Zoo-Code-Org#700 review feedback for tool-call streaming reassembly - Guard finalize results with not.toBeNull() in parallel-index and single-chunk tests so a null result fails instead of passing silently - Add reverse-ordering test (name -> buffered args -> id) covering the start-gate id requirement - Use name !== undefined recording plus a nameSeen flag in the start-gate as a defensive guard against an empty tool name - Clear rawChunkTracker in processFinishReason so finalizeRawChunks is a safe no-op; add a regression test asserting no double tool_call_end - Remove unrelated ask_followup_question wording change from PR scope - Remove prs/fix-toolcall-dropped-leading-deltas.md from the diff * fix: handle provider-emitted tool_call_end chunks during streaming Task.ts had no stream-level case for tool_call_end, so end chunks emitted by providers on finish_reason: "tool_calls" were silently dropped; tool calls only finalized at stream end via finalizeRawChunks(). Add a tool_call_end case so tools finalize and present during streaming, and extract the triplicated finalize/present logic into a shared idempotent helper. Correct the NativeToolCallParser test drive helper to finalize via finalizeRawChunks() (matching production) instead of processFinishReason(). * test: cover Task.finalizeStreamingToolCallById streaming finalization Add a focused spec that invokes the real Task.prototype.finalizeStreamingToolCallById via .call() with mocked presentAssistantMessage and NativeToolCallParser, covering the success, null-finalize (malformed JSON), untracked-id no-op, and idempotent re-finalize paths. Closes the codecov/patch gap on the new helper. * test: adapt tool-call streaming coverage to scoped parser state * test: type finalizeStreamingToolCallById fixtures as FinalizeStub * fix(task): defer finalized tool execution until history persists * refactor(parser): cut PR Zoo-Code-Org#700 to parser-only streaming scope Restore Task.ts persistence coordination and stream-time tool_call_end finalization to main; retain only the Zoo-Code-Org#695 parser fixes with their focused tests. --------- Co-authored-by: Elliott de Launay <[email protected]>
1 parent e277ab9 commit 2da6ea2

2 files changed

Lines changed: 351 additions & 19 deletions

File tree

‎src/core/assistant-message/NativeToolCallParser.ts‎

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,17 @@ export class NativeToolCallParser {
6161
// Raw chunk tracking state (keyed by index from one API stream)
6262
private static rawChunkTrackersByScope = new WeakMap<
6363
object,
64-
Map<number, { id: string; name: string; hasStarted: boolean; deltaBuffer: string[] }>
64+
Map<
65+
number,
66+
{
67+
id?: string
68+
name: string
69+
// Track whether the provider sent a name, including an empty name.
70+
nameSeen: boolean
71+
hasStarted: boolean
72+
deltaBuffer: string[]
73+
}
74+
>
6575
>()
6676

6777
public static createScope(): object {
@@ -124,49 +134,54 @@ export class NativeToolCallParser {
124134

125135
let tracked = rawChunkTracker.get(index)
126136

127-
// Initialize new tool call tracking when we receive an id
128-
if (id && !tracked) {
137+
// Create the tracker on first sight of this index, independent of whether
138+
// an id has arrived yet. Keying the lifecycle by index (not id) ensures any
139+
// `arguments` that stream before the id is known are buffered rather than dropped.
140+
if (!tracked) {
129141
tracked = {
130142
id,
131143
name: name || "",
144+
nameSeen: name !== undefined,
132145
hasStarted: false,
133146
deltaBuffer: [],
134147
}
135148
rawChunkTracker.set(index, tracked)
136149
}
137150

138-
if (!tracked) {
139-
return events
151+
// Record id and name as they arrive (they may come in separate chunks).
152+
if (id) {
153+
tracked.id = id
140154
}
141-
142-
// Update name if present in chunk and not yet set
143-
if (name) {
155+
if (name !== undefined) {
144156
tracked.name = name
157+
tracked.nameSeen = true
145158
}
146159

147-
// Emit start event when we have the name
148-
if (!tracked.hasStarted && tracked.name) {
160+
// Emit start event only once both id and name are known. Using a local
161+
// non-null id keeps emitted events typed as id: string.
162+
if (!tracked.hasStarted && tracked.id && tracked.nameSeen) {
163+
const startedId = tracked.id
149164
events.push({
150165
type: "tool_call_start",
151-
id: tracked.id,
166+
id: startedId,
152167
name: tracked.name,
153168
})
154169
tracked.hasStarted = true
155170

156-
// Flush buffered deltas
171+
// Flush buffered deltas accumulated during the pre-start window.
157172
for (const bufferedDelta of tracked.deltaBuffer) {
158173
events.push({
159174
type: "tool_call_delta",
160-
id: tracked.id,
175+
id: startedId,
161176
delta: bufferedDelta,
162177
})
163178
}
164179
tracked.deltaBuffer = []
165180
}
166181

167-
// Emit delta event for argument chunks
182+
// Emit delta event for argument chunks, buffering until start is emitted.
168183
if (args) {
169-
if (tracked.hasStarted) {
184+
if (tracked.hasStarted && tracked.id) {
170185
events.push({
171186
type: "tool_call_delta",
172187
id: tracked.id,
@@ -190,7 +205,7 @@ export class NativeToolCallParser {
190205

191206
if (rawChunkTracker) {
192207
for (const [, tracked] of rawChunkTracker.entries()) {
193-
if (tracked.hasStarted) {
208+
if (tracked.hasStarted && tracked.id) {
194209
events.push({
195210
type: "tool_call_end",
196211
id: tracked.id,

0 commit comments

Comments
 (0)