xonecas.io
64875
Mc

mini-coder / commit e0e7fe9

fix: add diagnostic logging for silent stream termination

Session mn0lde5v-szm11 showed a turn completing normally but missing
an entire step's tool-call result and step-finish. No error was logged.
The stream ended silently mid-step, returning the user to the prompt.

Add logging at three levels to catch this next time:
- Tool execution: log start/end/error of every tool execute call
- Step tracking: include stepNumber in step finish logs
- Stream completion: track lastChunkType in turn complete log
- AI SDK onError: log errors the SDK handles internally (was no-op)

xonecas2026-03-21 e0e7fe98c8fd3e443855098160d3272d3ce95fbe parent 9843cea

3 files changed

src/llm-api/turn-execution.ts+13−1
@@ -6,6 +6,7 @@
66 // some providers (OpenAI) emit alongside tool calls, preventing duplicate output.
77 import type { FlexibleSchema, StepResult } from "ai";
88 import { dynamicTool, jsonSchema, type streamText } from "ai";
9+import { logApiEvent } from "../logging/context.ts";
910 import { normalizeUnknownError } from "./error-utils.ts";
1011 import { isRecord } from "./history/shared.ts";
1112 import { isAnthropicModelFamily } from "./model-routing.ts";
@@ -51,9 +52,16 @@ function toCoreTool(def: ToolDef): ReturnType<typeof dynamicTool> {
5152 description: def.description,
5253 inputSchema: schema,
5354 execute: async (input: unknown) => {
55+ logApiEvent("tool execute start", { tool: def.name });
5456 try {
55- return await def.execute(input);
57+ const result = await def.execute(input);
58+ logApiEvent("tool execute end", { tool: def.name });
59+ return result;
5660 } catch (err) {
61+ logApiEvent("tool execute error", {
62+ tool: def.name,
63+ error: String(err),
64+ });
5765 throw normalizeUnknownError(err);
5866 }
5967 },
@@ -102,6 +110,7 @@ export function annotateToolCaching(
102110
103111 export function createTurnStateTracker(opts: {
104112 onStepLog: (entry: {
113+ stepNumber: number;
105114 finishReason: string | null | undefined;
106115 usage: unknown;
107116 }) => void;
@@ -110,10 +119,13 @@ export function createTurnStateTracker(opts: {
110119 let outputTokens = 0;
111120 let contextTokens = 0;
112121 let partialMessages: CoreMessage[] = [];
122+ let stepCount = 0;
113123
114124 return {
115125 onStepFinish: (step: StepResult<ToolSet>) => {
126+ stepCount++;
116127 opts.onStepLog({
128+ stepNumber: stepCount,
117129 finishReason: step.finishReason,
118130 usage: step.usage,
119131 });
src/llm-api/turn-request.ts+4−1
@@ -1,5 +1,6 @@
11 import type { streamText } from "ai";
22
3+import { logApiEvent } from "../logging/context.ts";
34 import { getMaxOutputTokens } from "./model-info.ts";
45 import { isAnthropicModelFamily } from "./model-routing.ts";
56 import type { ThinkingEffort } from "./provider-options.ts";
@@ -133,9 +134,11 @@ export function buildStreamTextRequest(
133134 }
134135 : {}),
135136 ...(input.signal ? { abortSignal: input.signal } : {}),
136- onError: () => {
137+ onError: ({ error }) => {
137138 // The AI SDK logs errors to stderr by default. We surface failures through
138139 // streamed turn events so CLI output stays compact and consistent.
140+ // Log the error so we can debug silent stream terminations.
141+ logApiEvent("streamText onError", { error: String(error) });
139142 },
140143 } as StreamTextOptions;
141144 }
src/llm-api/turn.ts+3−0
@@ -106,9 +106,11 @@ export async function* runTurn(options: {
106106 ) as StreamTextResultFull;
107107 result.response.catch(() => {});
108108
109+ let lastChunkType: string | undefined;
109110 for await (const event of mapFullStreamToTurnEvents(result.fullStream, {
110111 stepPruneQueue,
111112 onChunk: (streamChunk) => {
113+ lastChunkType = streamChunk.type;
112114 if (
113115 streamChunk.type === "tool-call" ||
114116 streamChunk.type === "tool-result"
@@ -127,6 +129,7 @@ export async function* runTurn(options: {
127129
128130 const finalState = turnState.getState();
129131 logApiEvent("turn complete", {
132+ lastChunkType,
130133 newMessagesCount: finalState.partialMessages.length,
131134 inputTokens: finalState.inputTokens,
132135 outputTokens: finalState.outputTokens,