Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

fix(bots): read the cause a failure wraps before calling it permanent

tournierjc4d1ad06e9884
diff --git a/src/bots/bot-turn.test.ts b/src/bots/bot-turn.test.ts
index e80af598..a98c4b4e 100644
--- a/src/bots/bot-turn.test.ts
+++ b/src/bots/bot-turn.test.ts
@@ -127,6 +127,15 @@ describe('runBotTurn step-budget continuation', () => {
// ship long leader-bot turns straight to the DLQ instead of retrying.
expect(classifyFailure(new Error('The operation timed out.'))).toBe('provider_timeout');
expect(classifyFailure(new Error('Request timed out after 60000ms'))).toBe('provider_timeout');
+ // The SDK hides a cut stream behind its own wrapper and keeps the reason in `cause`: the live
+ // failure (`Failed to process successful response`, 6 runs in four days) was classified
+ // unknown_error — no retry, DLQ, needs-you — because only the message was read.
+ const wrapped = (cause: unknown) =>
+ Object.assign(new Error('Failed to process successful response'), { cause });
+ expect(classifyFailure(wrapped(new Error('terminated')))).toBe('provider_timeout');
+ expect(classifyFailure(wrapped(Object.assign(new Error('socket hang up'), { code: 'ECONNRESET' })))).toBe('provider_timeout');
+ // A 2xx nobody could parse is a provider hiccup, not a permanent condition.
+ expect(classifyFailure(wrapped(new SyntaxError('Unexpected end of JSON input')))).toBe('provider_empty');
// Still-permanent things stay permanent.
expect(classifyFailure(new Error('401 unauthorized'))).toBe('provider_auth');
expect(classifyFailure(new Error('permission denied by policy'))).toBe('permission_denied');
diff --git a/src/bots/bot-turn.ts b/src/bots/bot-turn.ts
index 59946bf1..6627c189 100644
--- a/src/bots/bot-turn.ts
+++ b/src/bots/bot-turn.ts
@@ -547,9 +547,32 @@ export function resolveTurnLimits(manifest: BotManifest, overrides?: Partial<Bot
};
}
+/**
+ * The text a failure is classified on: the message, and what the error wraps.
+ *
+ * The AI SDK reports a cut stream as « Failed to process successful response » and keeps the real
+ * reason in `cause` — an ECONNRESET, a body cut mid-read, a JSON parse error. Reading only the
+ * message sent every one of those to unknown_error: no retry, DLQ, needs-you. A network blip then
+ * looked like a permanent fault, and the owner was told a bot had failed when it had merely lost
+ * its socket.
+ */
+function failureText(err: unknown): string {
+ const parts: string[] = [];
+ const seen = new Set<unknown>();
+ let current: unknown = err;
+ for (let depth = 0; current != null && depth < 5 && !seen.has(current); depth++) {
+ seen.add(current);
+ const wrapped = current as { message?: unknown; code?: unknown; cause?: unknown };
+ if (typeof wrapped.message === 'string') parts.push(wrapped.message);
+ if (typeof wrapped.code === 'string') parts.push(wrapped.code);
+ current = wrapped.cause;
+ }
+ return parts.join(' | ').toLowerCase();
+}
+
/** Map a provider/tool error to a typed reason code (retry vs permanent). */
export function classifyFailure(err: unknown): string {
- let msg = String((err as { message?: unknown })?.message ?? err ?? '').toLowerCase();
+ let msg = failureText(err);
// Retry wrappers ("Failed after 3 attempts. Last error: …") carry the
// real cause after the colon — classify that, not the wrapper.
const inner = /last error:\s*(.+)$/s.exec(msg);
@@ -566,6 +589,10 @@ export function classifyFailure(err: unknown): string {
// The SDK's "No output generated" means the stream ended before any step:
// a provider hiccup, not a permanent condition.
if (/no output generated/.test(msg)) return 'provider_empty';
+ // The SDK's wrapper around a 2xx response it could not read or parse. Nothing
+ // permanent lives there — a cut stream, a truncated body, an HTML page — and the
+ // cause above already claimed the network cases; whatever is left is still a retry.
+ if (/failed to process successful response/.test(msg)) return 'provider_empty';
if (/no llm providers available/.test(msg)) return 'provider_unavailable';
if (/permission denied|blocked command|no permission/.test(msg)) return 'permission_denied';
if (/api key|unauthorized|401|authentication/.test(msg)) return 'provider_auth';