xonecas.io
64875
Mc

mini-coder / commit f1363bc

fix: prevent fatal crashes on malformed grep/glob tool results

This fixes an issue where if an error or unexpected output was returned from the grep or glob tools during execution, the CLI would crash attempting to evaluate 'r.matches.length' or 'r.files.length'. The stream event parsing now detects the inner 'isError' state, and the rendering logic now uses optional chaining alongside Array.isArray to fail gracefully.

Sean2026-03-01 f1363bc6e41f07dd2501ecb900936a965d4d9982 parent 2910efe

2 files changed

src/cli/tool-render.ts+65−57
@@ -148,21 +148,25 @@ export function renderToolResultInline(
148148 }
149149
150150 if (toolName === "glob") {
151- const r = result as { files: string[]; truncated: boolean };
152- const n = r.files.length;
153- writeln(
154- `${indent}${G.info} ${c.dim(n === 0 ? "no matches" : `${n} file${n === 1 ? "" : "s"}${r.truncated ? " (capped)" : ""}`)}`,
155- );
156- return;
151+ const r = result as { files?: string[]; truncated?: boolean };
152+ if (Array.isArray(r?.files)) {
153+ const n = r.files.length;
154+ writeln(
155+ `${indent}${G.info} ${c.dim(n === 0 ? "no matches" : `${n} file${n === 1 ? "" : "s"}${r.truncated ? " (capped)" : ""}`)}`,
156+ );
157+ return;
158+ }
157159 }
158160
159161 if (toolName === "grep") {
160- const r = result as { matches: unknown[]; truncated: boolean };
161- const n = r.matches.length;
162- writeln(
163- `${indent}${G.info} ${c.dim(n === 0 ? "no matches" : `${n} match${n === 1 ? "" : "es"}${r.truncated ? " (capped)" : ""}`)}`,
164- );
165- return;
162+ const r = result as { matches?: unknown[]; truncated?: boolean };
163+ if (Array.isArray(r?.matches)) {
164+ const n = r.matches.length;
165+ writeln(
166+ `${indent}${G.info} ${c.dim(n === 0 ? "no matches" : `${n} match${n === 1 ? "" : "es"}${r.truncated ? " (capped)" : ""}`)}`,
167+ );
168+ return;
169+ }
166170 }
167171
168172 if (toolName === "read") {
@@ -247,65 +251,69 @@ export function renderToolResult(
247251 }
248252
249253 if (toolName === "glob") {
250- const r = result as { files: string[]; truncated: boolean };
251- const files = r.files;
252- if (files.length === 0) {
253- writeln(` ${G.info} ${c.dim("no matches")}`);
254- return;
255- }
254+ const r = result as { files?: string[]; truncated?: boolean };
255+ if (Array.isArray(r?.files)) {
256+ const files = r.files;
257+ if (files.length === 0) {
258+ writeln(` ${G.info} ${c.dim("no matches")}`);
259+ return;
260+ }
256261
257- // Show up to 12 files, grouped by top-level directory for readability
258- const show = files.slice(0, 12);
259- const rest = files.length - show.length;
260-
261- // Find common prefix to strip for cleaner display
262- const prefix = commonPrefix(show);
263- for (const f of show) {
264- const rel =
265- prefix && f.startsWith(prefix)
266- ? c.dim(prefix) + f.slice(prefix.length)
267- : f;
268- writeln(` ${c.dim("·")} ${rel}`);
262+ // Show up to 12 files, grouped by top-level directory for readability
263+ const show = files.slice(0, 12);
264+ const rest = files.length - show.length;
265+
266+ // Find common prefix to strip for cleaner display
267+ const prefix = commonPrefix(show);
268+ for (const f of show) {
269+ const rel =
270+ prefix && f.startsWith(prefix)
271+ ? c.dim(prefix) + f.slice(prefix.length)
272+ : f;
273+ writeln(` ${c.dim("·")} ${rel}`);
274+ }
275+ if (rest > 0) writeln(` ${c.dim(` +${rest} more`)}`);
276+ if (r.truncated) writeln(` ${c.dim(" (results capped)")}`);
277+ return;
269278 }
270- if (rest > 0) writeln(` ${c.dim(` +${rest} more`)}`);
271- if (r.truncated) writeln(` ${c.dim(" (results capped)")}`);
272- return;
273279 }
274280
275281 if (toolName === "grep") {
276282 const r = result as {
277- matches: Array<{
283+ matches?: Array<{
278284 file: string;
279285 line: number;
280286 text: string;
281287 context?: Array<{ line: number; text: string; isMatch: boolean }>;
282288 }>;
283- truncated: boolean;
289+ truncated?: boolean;
284290 };
285- if (r.matches.length === 0) {
286- writeln(` ${G.info} ${c.dim("no matches")}`);
287- return;
288- }
291+ if (Array.isArray(r?.matches)) {
292+ if (r.matches.length === 0) {
293+ writeln(` ${G.info} ${c.dim("no matches")}`);
294+ return;
295+ }
289296
290- // Deduplicate by file, show file:line and match text
291- const seen = new Set<string>();
292- let shown = 0;
293- for (const m of r.matches) {
294- if (shown >= 10) break;
295- const key = m.file;
296- const fileLabel = seen.has(key)
297- ? c.dim(" ".repeat(m.file.length + 1))
298- : c.dim(`${m.file}:`);
299- seen.add(key);
300- writeln(
301- ` ${fileLabel}${c.dim(String(m.line))} ${stripHashlinePrefix(m.text).trim()}`,
302- );
303- shown++;
297+ // Deduplicate by file, show file:line and match text
298+ const seen = new Set<string>();
299+ let shown = 0;
300+ for (const m of r.matches) {
301+ if (shown >= 10) break;
302+ const key = m.file;
303+ const fileLabel = seen.has(key)
304+ ? c.dim(" ".repeat(m.file.length + 1))
305+ : c.dim(`${m.file}:`);
306+ seen.add(key);
307+ writeln(
308+ ` ${fileLabel}${c.dim(String(m.line))} ${stripHashlinePrefix(m.text).trim()}`,
309+ );
310+ shown++;
311+ }
312+ const rest = r.matches.length - shown;
313+ if (rest > 0) writeln(` ${c.dim(` +${rest} more`)}`);
314+ if (r.truncated) writeln(` ${c.dim(" (results capped)")}`);
315+ return;
304316 }
305- const rest = r.matches.length - shown;
306- if (rest > 0) writeln(` ${c.dim(` +${rest} more`)}`);
307- if (r.truncated) writeln(` ${c.dim(" (results capped)")}`);
308- return;
309317 }
310318
311319 if (toolName === "read") {
src/llm-api/turn.ts+1−1
@@ -213,7 +213,7 @@ export async function* runTurn(options: {
213213 // AI SDK v6: property is `output`, not `result`
214214 result:
215215 "output" in c ? c.output : "result" in c ? c.result : undefined,
216- isError: false,
216+ isError: "isError" in c ? Boolean(c.isError) : false,
217217 };
218218 break;
219219 }