xonecas.io
64875
Mc

mini-coder / commit 687661c

fix(llm-api): dedupe tool start events by tool call id

xonecas2026-03-14 687661c56ad5ec03a4ce93060e8fd86c67dbac9b parent c60f67d

2 files changed

src/llm-api/turn.test.ts+73−0
@@ -4,6 +4,7 @@ import {
44 annotateAnthropicCacheBreakpoints,
55 applyContextPruning,
66 compactToolResultPayloads,
7+ consumePendingToolInputStart,
78 getMessageDiagnostics,
89 getReasoningDeltaFromStreamChunk,
910 isOpenAIGPT,
@@ -121,6 +122,78 @@ describe("getReasoningDeltaFromStreamChunk", () => {
121122 });
122123 });
123124
125+describe("consumePendingToolInputStart", () => {
126+ test("consumes one queued start by id", () => {
127+ const pendingById = new Set<string>(["call_1"]);
128+ const pendingByName = new Map<string, number>([["read", 2]]);
129+
130+ expect(
131+ consumePendingToolInputStart(
132+ pendingById,
133+ pendingByName,
134+ "call_1",
135+ "read",
136+ ),
137+ ).toBe(true);
138+ expect(pendingById.has("call_1")).toBe(false);
139+ expect(pendingByName.get("read")).toBe(2);
140+ });
141+
142+ test("falls back to name only when id is missing", () => {
143+ const pendingById = new Set<string>();
144+ const pendingByName = new Map<string, number>([["read", 2]]);
145+
146+ expect(
147+ consumePendingToolInputStart(pendingById, pendingByName, "", "read"),
148+ ).toBe(true);
149+ expect(pendingByName.get("read")).toBe(1);
150+ expect(
151+ consumePendingToolInputStart(pendingById, pendingByName, "", "read"),
152+ ).toBe(true);
153+ expect(pendingByName.has("read")).toBe(false);
154+ });
155+
156+ test("does not consume by name when an id is present but unmatched", () => {
157+ const pendingById = new Set<string>(["call_a", "call_b"]);
158+ const pendingByName = new Map<string, number>([["read", 2]]);
159+
160+ expect(
161+ consumePendingToolInputStart(
162+ pendingById,
163+ pendingByName,
164+ "call_missing",
165+ "read",
166+ ),
167+ ).toBe(false);
168+ expect(pendingById.size).toBe(2);
169+ expect(pendingByName.get("read")).toBe(2);
170+ });
171+
172+ test("handles interleaved same-name calls without cross-consuming", () => {
173+ const pendingById = new Set<string>(["call_a", "call_b"]);
174+ const pendingByName = new Map<string, number>();
175+
176+ // tool-call for B arrives before A; both should dedupe correctly by id.
177+ expect(
178+ consumePendingToolInputStart(
179+ pendingById,
180+ pendingByName,
181+ "call_b",
182+ "read",
183+ ),
184+ ).toBe(true);
185+ expect(
186+ consumePendingToolInputStart(
187+ pendingById,
188+ pendingByName,
189+ "call_a",
190+ "read",
191+ ),
192+ ).toBe(true);
193+ expect(pendingById.size).toBe(0);
194+ });
195+});
196+
124197 describe("sanitizeGeminiToolMessages", () => {
125198 const healthyGeminiToolTurn = (userContent: string): CoreMessage[] =>
126199 [
src/llm-api/turn.ts+65−2
@@ -275,6 +275,29 @@ export function getReasoningDeltaFromStreamChunk(
275275 return "";
276276 }
277277
278+export function consumePendingToolInputStart(
279+ pendingToolStartsById: Set<string>,
280+ pendingToolStartsByName: Map<string, number>,
281+ toolCallId: string,
282+ toolName: string,
283+): boolean {
284+ if (toolCallId.length > 0) {
285+ if (!pendingToolStartsById.has(toolCallId)) return false;
286+ pendingToolStartsById.delete(toolCallId);
287+ return true;
288+ }
289+
290+ if (toolName.length === 0) return false;
291+ const pending = pendingToolStartsByName.get(toolName) ?? 0;
292+ if (pending <= 0) return false;
293+ if (pending === 1) {
294+ pendingToolStartsByName.delete(toolName);
295+ } else {
296+ pendingToolStartsByName.set(toolName, pending - 1);
297+ }
298+ return true;
299+}
300+
278301 type StreamTextResult = ReturnType<typeof streamText>;
279302 type StreamTextResultFull = StreamTextResult & {
280303 fullStream: AsyncIterable<StreamChunk>;
@@ -1093,6 +1116,8 @@ export async function* runTurn(options: {
10931116 // awaited, causing an unhandled rejection that crashes the app.
10941117 // We catch it here to mark it as handled (awaiting it later will still throw).
10951118 result.response.catch(() => {});
1119+ const pendingToolStartsById = new Set<string>();
1120+ const pendingToolStartsByName = new Map<string, number>();
10961121
10971122 // Stream events
10981123 for await (const chunk of result.fullStream) {
@@ -1135,11 +1160,49 @@ export async function* runTurn(options: {
11351160 break;
11361161 }
11371162
1163+ case "tool-input-start": {
1164+ const toolName =
1165+ typeof c.toolName === "string" && c.toolName.length > 0
1166+ ? c.toolName
1167+ : "";
1168+ const toolCallId = String(c.toolCallId ?? "");
1169+ if (toolCallId.length > 0) {
1170+ pendingToolStartsById.add(toolCallId);
1171+ } else if (toolName.length > 0) {
1172+ pendingToolStartsByName.set(
1173+ toolName,
1174+ (pendingToolStartsByName.get(toolName) ?? 0) + 1,
1175+ );
1176+ } else {
1177+ break;
1178+ }
1179+ yield {
1180+ type: "tool-call-start",
1181+ toolCallId,
1182+ toolName,
1183+ // tool-input-start usually arrives before full args are available.
1184+ args: c.input ?? c.args,
1185+ };
1186+ break;
1187+ }
1188+
11381189 case "tool-call": {
1190+ const toolName = String(c.toolName ?? "");
1191+ const toolCallId = String(c.toolCallId ?? "");
1192+ if (
1193+ consumePendingToolInputStart(
1194+ pendingToolStartsById,
1195+ pendingToolStartsByName,
1196+ toolCallId,
1197+ toolName,
1198+ )
1199+ ) {
1200+ break;
1201+ }
11391202 yield {
11401203 type: "tool-call-start",
1141- toolCallId: String(c.toolCallId ?? ""),
1142- toolName: String(c.toolName ?? ""),
1204+ toolCallId,
1205+ toolName,
11431206 // AI SDK v6: property is `input`, not `args`
11441207 args: c.input ?? c.args,
11451208 };