xonecas.io
64875
Mc

mini-coder / commit 12a627c

fix: prefer explicit reasoning stream over GPT commentary

xonecas2026-03-14 12a627ca7a92ec336cc60d35108d1af66902af73 parent b90fd10

3 files changed

TODO.md+1−0
@@ -10,6 +10,7 @@ The issue is not fixed, it's just suppressed.
1010 This seems like an issue on how we are parsing stream events, and not recognzing tool calls from other events.
1111
1212 neither pi or opencode have this bug with the same provider, and opencode uses the same sdks as we do
13+use the `gh` tool to clone and inspect how they avoid this issue
1314
1415 Relevant reasoning output:
1516
src/llm-api/turn-execution.test.ts+38−0
@@ -151,6 +151,44 @@ describe("mapFullStreamToTurnEvents", () => {
151151 ]);
152152 });
153153
154+ test("prefers explicit reasoning events over duplicate openai commentary text in the same step", async () => {
155+ const events = await collectEvents([
156+ { type: "start-step" },
157+ { type: "reasoning-start", id: "rs-1" },
158+ { type: "reasoning-delta", id: "rs-1", delta: "real reasoning" },
159+ { type: "reasoning-end", id: "rs-1" },
160+ {
161+ type: "text-start",
162+ id: "msg-commentary",
163+ providerMetadata: {
164+ openai: { itemId: "msg-commentary", phase: "commentary" },
165+ },
166+ },
167+ {
168+ type: "text-delta",
169+ id: "msg-commentary",
170+ text: "to=functions.shell json{...}",
171+ },
172+ { type: "text-end", id: "msg-commentary" },
173+ {
174+ type: "tool-call",
175+ toolCallId: "call-4",
176+ toolName: "shell",
177+ input: { command: "echo hi" },
178+ },
179+ ]);
180+
181+ expect(events).toEqual([
182+ { type: "reasoning-delta", delta: "real reasoning" },
183+ {
184+ type: "tool-call-start",
185+ toolCallId: "call-4",
186+ toolName: "shell",
187+ args: { command: "echo hi" },
188+ },
189+ ]);
190+ });
191+
154192 test("keeps unphased text deltas visible", async () => {
155193 const events = await collectEvents([
156194 { type: "text-delta", id: "msg-plain", text: "visible" },
src/llm-api/turn-execution.ts+16−3
@@ -166,10 +166,22 @@ function extractTextDelta(chunk: StreamTextChunk): string {
166166
167167 class StreamTextPhaseTracker {
168168 private phaseByTextPartId = new Map<string, "commentary" | "final_answer">();
169+ private sawExplicitReasoningThisStep = false;
169170
170171 route(chunk: StreamTextChunk): StreamTextRoute {
171172 const textPartId = normalizeTextPartId(chunk.id);
172173 switch (chunk.type) {
174+ case "start-step": {
175+ this.sawExplicitReasoningThisStep = false;
176+ return "text";
177+ }
178+ case "reasoning-start":
179+ case "reasoning-delta":
180+ case "reasoning-end":
181+ case "reasoning": {
182+ this.sawExplicitReasoningThisStep = true;
183+ return "text";
184+ }
173185 case "text-start": {
174186 const phase = getOpenAITextPhase(chunk);
175187 if (textPartId && phase) this.phaseByTextPartId.set(textPartId, phase);
@@ -181,9 +193,10 @@ class StreamTextPhaseTracker {
181193 }
182194 case "text-delta": {
183195 if (!textPartId) return "text";
184- return this.phaseByTextPartId.get(textPartId) === "commentary"
185- ? "reasoning"
186- : "text";
196+ if (this.phaseByTextPartId.get(textPartId) !== "commentary") {
197+ return "text";
198+ }
199+ return this.sawExplicitReasoningThisStep ? "skip" : "reasoning";
187200 }
188201 default:
189202 return "text";