🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(agent-runtime): harden classifyLLMError so it never masks the original provider error (#13774)
Arvin Xud6f11f80b638
diff --git a/src/server/modules/AgentRuntime/__tests__/llmErrorClassification.test.ts b/src/server/modules/AgentRuntime/__tests__/llmErrorClassification.test.tsindex b2025e329b2..c639cf741f2 100644--- a/src/server/modules/AgentRuntime/__tests__/llmErrorClassification.test.ts+++ b/src/server/modules/AgentRuntime/__tests__/llmErrorClassification.test.ts@@ -49,4 +49,86 @@ describe('classifyLLMError', () => {it('should default unknown errors to retry', () => {expect(classifyLLMError(new Error('unexpected upstream issue')).kind).toBe('retry');});++ describe('non-string code / errorType defensive handling', () => {+ // Regression: real-world provider errors sometimes carry numeric `code`+ // (HTTP status) or a structured object in the error fields. Earlier versions+ // called `.trim()` on these and threw TypeError, which masked the original+ // provider error behind "e.trim is not a function".++ it('does not throw when error.code is a number', () => {+ const result = classifyLLMError({ code: 429, message: 'rate limit' });+ expect(result.message).toBe('rate limit');+ // Classifier should still land on a valid kind, not crash.+ expect(['retry', 'stop']).toContain(result.kind);+ });++ it('does not throw when errorType is an object', () => {+ const result = classifyLLMError({+ errorType: { nested: 'structured error' },+ message: 'upstream returned structured type',+ });+ expect(result.message).toBe('upstream returned structured type');+ expect(['retry', 'stop']).toContain(result.kind);+ });++ it('does not throw when nested error.code is a number (OpenAI SDK shape)', () => {+ const result = classifyLLMError({+ error: { error: { code: 402, message: 'payment required' } },+ errorType: 'ProviderBizError',+ });+ expect(result.message).toBe('payment required');+ expect(['retry', 'stop']).toContain(result.kind);+ });++ // Regression: some third-party proxies surface HTTP status ONLY as a+ // numeric `code` (no `status`/`statusCode`, no status digits in the+ // message). Previously these fell through to `retry`, causing wasteful+ // retry loops on permanent auth/permission failures.++ it('treats numeric code=401 as stop when no status field is present', () => {+ const result = classifyLLMError({ code: 401, message: 'upstream refused' });+ expect(result.kind).toBe('stop');+ });++ it('treats numeric code=403 as stop when no status field is present', () => {+ const result = classifyLLMError({ code: 403, message: 'upstream refused' });+ expect(result.kind).toBe('stop');+ });++ it('treats numeric code=429 as retry when no status field is present', () => {+ const result = classifyLLMError({ code: 429, message: 'upstream refused' });+ expect(result.kind).toBe('retry');+ });++ it('treats nested numeric code as stop (proxy-wrapped auth failure)', () => {+ const result = classifyLLMError({+ error: { error: { code: 401, message: 'proxy refused upstream' } },+ });+ expect(result.kind).toBe('stop');+ });++ it('prefers explicit status over numeric code fallback', () => {+ // status says 500 (retry), code says 401 (stop) — status wins.+ const result = classifyLLMError({ code: 401, message: 'oops', status: 500 });+ expect(result.kind).toBe('retry');+ });++ it('preserves the original error message when normalizeSignal itself would throw', () => {+ // Force a normalizeSignal crash by making `.toLowerCase()` blow up on a+ // non-string message (via a Proxy that throws on Symbol.toPrimitive).+ const hostile = new Proxy(+ {},+ {+ get: () => {+ throw new Error('property access explodes');+ },+ },+ );+ const result = classifyLLMError(hostile);+ // Falls back to 'stop' when classifier throws; message is best-effort.+ expect(result.kind).toBe('stop');+ expect(typeof result.message).toBe('string');+ });+ });});diff --git a/src/server/modules/AgentRuntime/llmErrorClassification.ts b/src/server/modules/AgentRuntime/llmErrorClassification.tsindex 828df331df6..cdec3251e9e 100644--- a/src/server/modules/AgentRuntime/llmErrorClassification.ts+++ b/src/server/modules/AgentRuntime/llmErrorClassification.ts@@ -64,8 +64,8 @@ const STOP_KEYWORDS = [const hasAnyKeyword = (text: string, keywords: string[]) =>keywords.some((keyword) => text.includes(keyword));-const normalizeCode = (value?: string) => {- if (!value) return;+const normalizeCode = (value?: unknown): string | undefined => {+ if (typeof value !== 'string' || !value) return;return value.trim()@@ -73,7 +73,11 @@ const normalizeCode = (value?: string) => {.replaceAll(/[\s-]+/g, '_');};-const normalizeErrorType = (value?: string) => value?.trim();+const normalizeErrorType = (value?: unknown): string | undefined => {+ if (typeof value !== 'string') return undefined;+ const trimmed = value.trim();+ return trimmed || undefined;+};const tryExtractStatus = (message: string) => {const matches = message.match(/\b([45]\d{2})\b/);@@ -83,6 +87,17 @@ const tryExtractStatus = (message: string) => {return Number.isNaN(status) ? undefined : status;};+// Some providers (notably bare HTTP proxies) only surface the HTTP status as a+// numeric `code` on the error object, with no `status`/`statusCode`. Treat+// those numeric codes as status so classifyKind can still map 401/403 to stop+// and 429/5xx to retry without falling through to message-keyword matching.+const numericStatusFromCode = (...codes: unknown[]): number | undefined => {+ for (const code of codes) {+ if (typeof code === 'number' && Number.isFinite(code)) return code;+ }+ return undefined;+};+const normalizeSignal = (error: unknown): LLMErrorSignal => {if (typeof error === 'string') {const message = error.toLowerCase();@@ -91,11 +106,11 @@ const normalizeSignal = (error: unknown): LLMErrorSignal => {if (error instanceof Error) {const raw = error as Error & {- code?: string;- errorType?: string;+ code?: unknown;+ errorType?: unknown;status?: number;statusCode?: number;- type?: string;+ type?: unknown;};const message = (raw.message || raw.name || 'unknown error').toLowerCase();@@ -108,26 +123,26 @@ const normalizeSignal = (error: unknown): LLMErrorSignal => {? raw.status: typeof raw.statusCode === 'number'? raw.statusCode- : tryExtractStatus(message),+ : (numericStatusFromCode(raw.code) ?? tryExtractStatus(message)),};}if (error && typeof error === 'object') {const raw = error as {- code?: string;+ code?: unknown;error?: {- code?: string;- error?: { code?: string; message?: string; status?: number; type?: string };- errorType?: string;+ code?: unknown;+ error?: { code?: unknown; message?: string; status?: number; type?: unknown };+ errorType?: unknown;message?: string;status?: number;- type?: string;+ type?: unknown;};- errorType?: string;+ errorType?: unknown;message?: string;status?: number;statusCode?: number;- type?: string;+ type?: unknown;};const nested = raw.error;const nestedError = nested?.error;@@ -153,7 +168,8 @@ const normalizeSignal = (error: unknown): LLMErrorSignal => {? nested.status: typeof nestedError?.status === 'number'? nestedError.status- : tryExtractStatus(message),+ : (numericStatusFromCode(raw.code, nested?.code, nestedError?.code) ??+ tryExtractStatus(message)),};}@@ -198,14 +214,47 @@ const classifyKind = ({ code, errorType, message, status }: LLMErrorSignal): LLMreturn 'retry';};+/**+ * Extract a human-readable message for the fallback path without relying on+ * normalizeSignal (which might be the thing that just threw).+ */+const bestEffortMessage = (error: unknown): string => {+ try {+ if (error instanceof Error && typeof error.message === 'string' && error.message.length > 0) {+ return error.message;+ }+ if (typeof error === 'string' && error.length > 0) return error;+ if (error && typeof error === 'object') {+ const e = error as { message?: unknown; error?: { message?: unknown } };+ if (typeof e.message === 'string' && e.message.length > 0) return e.message;+ const nested = e.error?.message;+ if (typeof nested === 'string' && nested.length > 0) return nested;+ }+ } catch {+ // Property access itself can throw (e.g. hostile Proxy). Fall through to default.+ }+ return 'unknown error';+};+export const classifyLLMError = (error: unknown): ClassifiedLLMError => {- const signal = normalizeSignal(error);+ // Defensive: a classifier that throws would mask the original provider error+ // behind the classifier's own TypeError (e.g. `e.trim is not a function`),+ // making prod debugging impossible. If anything below throws, fall back to a+ // conservative "stop" decision that preserves the original error message.+ try {+ const signal = normalizeSignal(error);- return {- code: signal.code || signal.errorType,- kind: classifyKind(signal),- message: signal.message,- };+ return {+ code: signal.code || signal.errorType,+ kind: classifyKind(signal),+ message: signal.message,+ };+ } catch (classificationError) {+ return {+ kind: 'stop',+ message: bestEffortMessage(error),+ };+ }};export type { ClassifiedLLMError, LLMErrorKind };