xonecas.io
64875
Mc

mini-coder / commit 01f2bea

fix: cap api log serialization, recover from API errors in input loop

- Sanitize error objects before logging to strip requestBodyValues,
  responseBody, and responseHeaders (prevents 170MB+ api.log from
  unbounded JSON.stringify of RetryError objects with full conversation)
- Cap individual log entries at 8KB
- Wrap processUserInput() calls in input-loop with try/catch so
  transient API errors (502, ECONNRESET, rate limits) no longer crash
  the process
- Add ECONNRESET and socket-closed detection to error parser with
  actionable hint

xonecas2026-03-13 01f2bea7046a33e2cea86051a2e299f282db3aaa parent a33d3bc

4 files changed

src/cli/error-parse.test.ts+20−0
@@ -112,6 +112,26 @@ test("parseAppError - ECONNREFUSED message", () => {
112112 });
113113 });
114114
115+test("parseAppError - ECONNRESET code", () => {
116+ const err = Object.assign(new Error("Connection reset"), {
117+ code: "ECONNRESET",
118+ });
119+ expect(parseAppError(err)).toEqual({
120+ headline: "Connection lost",
121+ hint: "The server closed the connection — retry or switch model with /model",
122+ });
123+});
124+
125+test("parseAppError - socket closed unexpectedly message", () => {
126+ const err = new Error(
127+ "The socket connection was closed unexpectedly. For more information, pass `verbose: true`",
128+ );
129+ expect(parseAppError(err)).toEqual({
130+ headline: "Connection lost",
131+ hint: "The server closed the connection — retry or switch model with /model",
132+ });
133+});
134+
115135 test("parseAppError - fallback", () => {
116136 const err = new Error("Something went wrong\nSome more details");
117137 expect(parseAppError(err)).toEqual({
src/cli/error-parse.ts+11−0
@@ -73,6 +73,17 @@ export function parseAppError(err: unknown): {
7373 };
7474 }
7575
76+ if (
77+ code === "ECONNRESET" ||
78+ message.includes("ECONNRESET") ||
79+ message.includes("socket connection was closed unexpectedly")
80+ ) {
81+ return {
82+ headline: "Connection lost",
83+ hint: "The server closed the connection — retry or switch model with /model",
84+ };
85+ }
86+
7687 const firstLine = message.split("\n")[0]?.trim() || "Unknown error";
7788 return { headline: firstLine };
7889 }
src/cli/input-loop.ts+10−2
@@ -77,7 +77,11 @@ export async function runInputLoop(opts: InputLoopOptions): Promise<void> {
7777 if (result.type === "inject-user-message") {
7878 const { text: resolvedText, images: refImages } =
7979 await resolveFileRefs(result.text, cwd);
80- await runner.processUserInput(resolvedText, refImages);
80+ try {
81+ await runner.processUserInput(resolvedText, refImages);
82+ } catch {
83+ // Error already rendered by stream-render; continue the loop.
84+ }
8185 }
8286 continue;
8387 }
@@ -106,7 +110,11 @@ export async function runInputLoop(opts: InputLoopOptions): Promise<void> {
106110 const allImages = [...(input.images || []), ...refImages];
107111
108112 if (!runner.ralphMode) {
109- await runner.processUserInput(resolvedText, allImages);
113+ try {
114+ await runner.processUserInput(resolvedText, allImages);
115+ } catch {
116+ // Error already rendered by stream-render; continue the loop.
117+ }
110118 continue;
111119 }
112120
src/llm-api/api-log.ts+45−1
@@ -4,6 +4,10 @@ import { join } from "node:path";
44
55 let writer: ReturnType<ReturnType<typeof Bun.file>["writer"]> | null = null;
66
7+/** Cap individual log entries to prevent unbounded serialization (e.g. RetryError
8+ * objects that embed the full requestBodyValues / conversation history). */
9+const MAX_ENTRY_BYTES = 8 * 1024;
10+
711 export function initApiLog(): void {
812 if (writer) return;
913
@@ -16,6 +20,41 @@ export function initApiLog(): void {
1620 writer = Bun.file(logPath).writer();
1721 }
1822
23+function isObject(v: unknown): v is Record<string, unknown> {
24+ return typeof v === "object" && v !== null;
25+}
26+
27+/**
28+ * Strip large/opaque fields from error objects before serialization.
29+ * AI SDK errors embed full `requestBodyValues` (the entire conversation) and
30+ * `responseBody` (full HTML error pages) — serializing these can produce
31+ * tens of megabytes per error and block the event loop on flush.
32+ */
33+function sanitizeForLog(data: unknown): unknown {
34+ if (!isObject(data)) return data;
35+
36+ const DROP_KEYS = new Set([
37+ "requestBodyValues",
38+ "responseBody",
39+ "responseHeaders",
40+ "stack",
41+ ]);
42+
43+ const result: Record<string, unknown> = {};
44+ for (const key in data) {
45+ if (DROP_KEYS.has(key)) continue;
46+ const value = data[key];
47+ if (key === "errors" && Array.isArray(value)) {
48+ result[key] = value.map((e) => sanitizeForLog(e));
49+ } else if (key === "lastError" && isObject(value)) {
50+ result[key] = sanitizeForLog(value);
51+ } else {
52+ result[key] = value;
53+ }
54+ }
55+ return result;
56+}
57+
1958 export function logApiEvent(event: string, data?: unknown): void {
2059 if (!writer) return;
2160
@@ -24,7 +63,12 @@ export function logApiEvent(event: string, data?: unknown): void {
2463
2564 if (data !== undefined) {
2665 try {
27- entry += JSON.stringify(data, null, 2)
66+ const safe = sanitizeForLog(data);
67+ let serialized = JSON.stringify(safe, null, 2);
68+ if (Buffer.byteLength(serialized, "utf8") > MAX_ENTRY_BYTES) {
69+ serialized = `${serialized.slice(0, MAX_ENTRY_BYTES)}\n …truncated`;
70+ }
71+ entry += serialized
2872 .split("\n")
2973 .map((line) => ` ${line}`)
3074 .join("\n");