Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,29 @@ describe('StreamingToolCallParser', () => {
});
});

it('routes id-less continuation chunks to a slot claimed by a colliding opener delta', () => {
parser.addChunk(0, '{"a":1}', 'call_1', 'function1');
Comment on lines +1015 to +1016

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The added guard's block direction is covered by the third test, does not let a brand-new tool-call id adopt a remap slot that already has an id: after call_2 claims the 0->1 remap with its own id, a third call reusing index 0 with a fresh id asserts third.actualIndex !== 1, which fails if the !toolCallMeta.get(remap)?.id guard is removed. This first test intentionally covers the record path; the third covers the adopt-guard path.


// The provider reuses index 0 for a second tool call whose id and name
// arrive together on an empty opener delta (the standard OpenAI streaming
// shape: function: { name, arguments: '' }).
const opener = parser.addChunk(0, '', 'call_2', 'function2');
expect(opener.actualIndex).toBe(1);

// The following id-less argument chunk must land on call_2's slot, not on
// a fresh orphan slot that would drop the arguments and get the call
// flagged as malformed.
const continuation = parser.addChunk(0, '{"b":2}');
expect(continuation.actualIndex).toBe(1);

expect(parser.getCompletedToolCalls()).toContainEqual({
id: 'call_2',
name: 'function2',
args: { b: 2 },
index: 1,
});
});

it('should handle rapid tool call switching at same index', () => {
// Rapid switching between different tool calls at index 0
parser.addChunk(0, '{"step1":', 'call_1', 'function1');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,14 @@ export class StreamingToolCallParser {
if (this.idToIndexMap.has(id)) {
// We've seen this ID before, use the existing mapped index
actualIndex = this.idToIndexMap.get(id)!;
} else if (this.pendingIndexRemaps.has(index)) {
// Some providers stream name or arguments before the stable ID.
} else if (
this.pendingIndexRemaps.has(index) &&
!this.toolCallMeta.get(this.pendingIndexRemaps.get(index)!)?.id
) {
// Some providers stream name or arguments before the stable ID. Only
// adopt the remapped slot while it has not yet been claimed by an id:
// once it has one, the remap only exists to route later id-less
// continuation chunks, so a brand-new id must not hijack that slot.
actualIndex = this.pendingIndexRemaps.get(index)!;
this.pendingIndexRemaps.delete(index);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed — removed in ef62d64. You're right that the common-path re-registration re-creates the mapping on the adoption path (actualIndex !== index always holds and isKnownId is false there, so the "ignoring replay chunk" early return isn't reached), so the delete had no runtime effect. I left a comment where it was so the remap's intended lifetime — kept alive for later id-less continuations — reads straight through and nobody re-adds it.

this.idToIndexMap.set(id, actualIndex);
Expand Down Expand Up @@ -206,7 +212,7 @@ export class StreamingToolCallParser {
} else {
this.namelessToolCallIndices.delete(actualIndex);
}
if (!meta.id && actualIndex !== index) {
if (actualIndex !== index) {
this.pendingIndexRemaps.set(index, actualIndex);
}
return { actualIndex, complete: false };
Expand Down Expand Up @@ -236,7 +242,7 @@ export class StreamingToolCallParser {
meta.name = validName;
}
}
if (!meta.id && actualIndex !== index) {
if (actualIndex !== index) {
this.pendingIndexRemaps.set(index, actualIndex);
}
Comment on lines 249 to 254

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks — I looked at this closely and verified it empirically, and I'm going to keep the unconditional overwrite. The suggested guard (!pendingIndexRemaps.has(index)) keeps the first remap, but an id-less continuation is supposed to belong to the most recent opener at that provider index (the sequential-completion assumption the whole collision path relies on). Keeping the first mapping pins the remap at call_2's slot, so a continuation after call_3's opener is misrouted to call_2.

I added a regression test for exactly the well-formed three-call case (6b62711): call_1@0, call_2 opener→slot 1 with its args, call_3 opener→slot 2, then an id-less {"c":3} that must land on slot 2. It passes with the overwrite and fails with the suggested guard (the continuation lands on slot 1 — the findMostRecentIncompleteIndex fallback doesn't recover it). So the guard would trade the pre-existing 3-call interleaved-corruption case (which it only half-fixes — it moves the corruption from call_2 to call_3) for a regression in the well-formed 3-call case. The interleaved-malformed 3-call case is genuinely pre-existing and out of this PR's 2-call scope.


Expand Down
Loading