xonecas.io
64875
Mc

mini-coder / commit 95704e3

fix: suppress phantom tool-start stream events

xonecas2026-03-15 95704e3f701ee62a0acdb59e3ad994b4fb8e022c parent 5a42081

4 files changed

TODO.md+3−0
@@ -13,6 +13,8 @@ This seems like an issue on how we are parsing stream events, and not recognzing
1313 neither pi or opencode have this bug with the same provider, and opencode uses the same sdks as we do
1414 use the `gh` tool to clone and inspect how they avoid this issue
1515
16+Expectation: Gpt models don't produce corrupted output and correctly display reasoning
17+
1618 Relevant reasoning output:
1719
1820 ```
@@ -114,6 +116,7 @@ with assistant metadata to=functions.shell from UI (not visible in message maybe
114116 ```
115117
116118 - Models in general struggle with our read/write tools, use the db to investigate their behaviour with out tools and let's refactor them with the goal of reducintg errors.
119+- UI hangs for a short time before reasoning blocks. Reasoning blocks lost their italic styling.
117120
118121 ---
119122
src/llm-api/turn-execution.test.ts+40−0
@@ -90,6 +90,46 @@ describe("mapFullStreamToTurnEvents", () => {
9090 });
9191 });
9292
93+ test("suppresses placeholder tool-input-start chunks until the real tool call arrives", async () => {
94+ const events = await collectEvents([
95+ { type: "tool-input-start", toolName: "shell", input: {} },
96+ {
97+ type: "tool-input-delta",
98+ toolName: "shell",
99+ delta: '{"command":"echo hi"}',
100+ },
101+ { type: "tool-input-end", toolName: "shell" },
102+ {
103+ type: "tool-call",
104+ toolCallId: "call-2b",
105+ toolName: "shell",
106+ input: { command: "echo hi" },
107+ },
108+ {
109+ type: "tool-result",
110+ toolCallId: "call-2b",
111+ toolName: "shell",
112+ output: "hi",
113+ },
114+ ]);
115+
116+ expect(events).toEqual([
117+ {
118+ type: "tool-call-start",
119+ toolCallId: "call-2b",
120+ toolName: "shell",
121+ args: { command: "echo hi" },
122+ },
123+ {
124+ type: "tool-result",
125+ toolCallId: "call-2b",
126+ toolName: "shell",
127+ result: "hi",
128+ isError: false,
129+ },
130+ ]);
131+ });
132+
93133 test("routes openai commentary text deltas into reasoning events", async () => {
94134 const events = await collectEvents([
95135 {
src/llm-api/turn-execution.ts+80−16
@@ -1,6 +1,8 @@
11 import type { FlexibleSchema, StepResult } from "ai";
22 import { dynamicTool, jsonSchema, type streamText } from "ai";
33 import {
4+ extractToolArgs,
5+ hasRenderableToolArgs,
46 mapStreamChunkToTurnEvent,
57 shouldLogStreamChunk,
68 } from "./turn-stream-events.ts";
@@ -114,7 +116,6 @@ interface StreamTextChunk {
114116 [key: string]: unknown;
115117 }
116118
117-const TOOL_START_CHUNK_TYPES = new Set(["tool-input-start", "tool-call"]);
118119 const TOOL_RESULT_CHUNK_TYPES = new Set(["tool-result", "tool-error"]);
119120
120121 function normalizeToolCallId(raw: unknown): string | null {
@@ -217,18 +218,45 @@ function mapCommentaryChunkToTurnEvent(
217218 class StreamToolCallTracker {
218219 private syntheticCount = 0;
219220 private pendingByTool = new Map<string, string[]>();
221+ private deferredStartsByTool = new Map<string, number>();
220222
221- assign(chunk: StreamToolChunk): StreamToolChunk {
223+ prepare(chunk: StreamToolChunk): {
224+ chunk: StreamToolChunk;
225+ suppressTurnEvent: boolean;
226+ } {
222227 const type = chunk.type;
223- if (!type) return chunk;
228+ if (!type) {
229+ return { chunk, suppressTurnEvent: false };
230+ }
231+
232+ if (type === "tool-input-start") {
233+ const toolName = normalizeToolName(chunk.toolName);
234+ const toolCallId = normalizeToolCallId(chunk.toolCallId);
235+ const args = extractToolArgs(chunk);
236+ if (!hasRenderableToolArgs(args)) {
237+ if (!toolCallId) {
238+ this.trackDeferredStart(toolName);
239+ return { chunk, suppressTurnEvent: true };
240+ }
241+ return { chunk, suppressTurnEvent: false };
242+ }
243+ return {
244+ chunk: this.trackRenderableStart(chunk, toolName, toolCallId),
245+ suppressTurnEvent: false,
246+ };
247+ }
224248
225- if (TOOL_START_CHUNK_TYPES.has(type)) {
249+ if (type === "tool-call") {
226250 const toolName = normalizeToolName(chunk.toolName);
227- const toolCallId =
228- normalizeToolCallId(chunk.toolCallId) ?? this.nextSyntheticToolCallId();
229- this.trackStart(toolName, toolCallId);
230- if (toolCallId === chunk.toolCallId) return chunk;
231- return { ...chunk, toolCallId };
251+ this.consumeDeferredStart(toolName);
252+ return {
253+ chunk: this.trackRenderableStart(
254+ chunk,
255+ toolName,
256+ normalizeToolCallId(chunk.toolCallId),
257+ ),
258+ suppressTurnEvent: false,
259+ };
232260 }
233261
234262 if (TOOL_RESULT_CHUNK_TYPES.has(type)) {
@@ -236,14 +264,28 @@ class StreamToolCallTracker {
236264 const existingToolCallId = normalizeToolCallId(chunk.toolCallId);
237265 if (existingToolCallId) {
238266 this.consumeTracked(toolName, existingToolCallId);
239- return chunk;
267+ return { chunk, suppressTurnEvent: false };
240268 }
241- const toolCallId =
269+ const nextToolCallId =
242270 this.consumeNextTracked(toolName) ?? this.nextSyntheticToolCallId();
243- return { ...chunk, toolCallId };
271+ return {
272+ chunk: { ...chunk, toolCallId: nextToolCallId },
273+ suppressTurnEvent: false,
274+ };
244275 }
245276
246- return chunk;
277+ return { chunk, suppressTurnEvent: false };
278+ }
279+
280+ private trackRenderableStart(
281+ chunk: StreamToolChunk,
282+ toolName: string,
283+ existingToolCallId: string | null,
284+ ): StreamToolChunk {
285+ const toolCallId = existingToolCallId ?? this.nextSyntheticToolCallId();
286+ this.trackStart(toolName, toolCallId);
287+ if (toolCallId === chunk.toolCallId) return chunk;
288+ return { ...chunk, toolCallId };
247289 }
248290
249291 private nextSyntheticToolCallId(): string {
@@ -257,6 +299,23 @@ class StreamToolCallTracker {
257299 this.pendingByTool.set(toolName, pending);
258300 }
259301
302+ private trackDeferredStart(toolName: string): void {
303+ this.deferredStartsByTool.set(
304+ toolName,
305+ (this.deferredStartsByTool.get(toolName) ?? 0) + 1,
306+ );
307+ }
308+
309+ private consumeDeferredStart(toolName: string): void {
310+ const count = this.deferredStartsByTool.get(toolName) ?? 0;
311+ if (count <= 0) return;
312+ if (count === 1) {
313+ this.deferredStartsByTool.delete(toolName);
314+ return;
315+ }
316+ this.deferredStartsByTool.set(toolName, count - 1);
317+ }
318+
260319 private consumeTracked(toolName: string, toolCallId: string): void {
261320 const pending = this.pendingByTool.get(toolName);
262321 if (!pending || pending.length === 0) return;
@@ -284,12 +343,17 @@ export async function* mapFullStreamToTurnEvents(
284343 const toolCallTracker = new StreamToolCallTracker();
285344 const textPhaseTracker = new StreamTextPhaseTracker();
286345 for await (const originalChunk of stream) {
287- const chunk = toolCallTracker.assign(originalChunk);
346+ const prepared = toolCallTracker.prepare(originalChunk);
347+ const chunk = prepared.chunk;
288348 const route = textPhaseTracker.route(chunk);
289- if (route !== "skip" && shouldLogStreamChunk(chunk)) {
349+ if (
350+ !prepared.suppressTurnEvent &&
351+ route !== "skip" &&
352+ shouldLogStreamChunk(chunk)
353+ ) {
290354 opts.onChunk?.(chunk);
291355 }
292- if (route === "skip") continue;
356+ if (prepared.suppressTurnEvent || route === "skip") continue;
293357 const event =
294358 route === "reasoning"
295359 ? mapCommentaryChunkToTurnEvent(chunk)
src/llm-api/turn-stream-events.ts+2−2
@@ -11,11 +11,11 @@ export function shouldLogStreamChunk(c: StreamChunk): boolean {
1111 );
1212 }
1313
14-function extractToolArgs(c: StreamChunk): unknown {
14+export function extractToolArgs(c: StreamChunk): unknown {
1515 return c.input ?? c.args;
1616 }
1717
18-function hasRenderableToolArgs(args: unknown): boolean {
18+export function hasRenderableToolArgs(args: unknown): boolean {
1919 if (args === null || args === undefined) return false;
2020 if (typeof args === "string") return args.trim().length > 0;
2121 if (Array.isArray(args)) return args.length > 0;