Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(drivers): scan each diagnostic line once, keep quoted statuses nested
Saim Shafique1bbad2584f53
diff --git a/server/drivers/engine-errors.test.ts b/server/drivers/engine-errors.test.tsindex 055f4f9ffe..ea58913f5b 100644--- a/server/drivers/engine-errors.test.ts+++ b/server/drivers/engine-errors.test.ts@@ -285,8 +285,11 @@ describe("classifyError reads an explicit HTTP status before body text", () => {["API Error: 410: resource gone\nHTTP 503 Service Unavailable", { transient: true, reason: "server_error" }],["API Error: 410: resource gone\nHTTP 429: too many requests", { transient: true, reason: "rate_limited" }],["API Error: 410: resource gone Error: HTTP 503 Service Unavailable", { transient: true, reason: "server_error" }],+ ["API Error: 410: resource gone\nxAI HTTP 503 Service Unavailable", { transient: true, reason: "server_error" }],+ ["API Error: 410: resource gone\nHTTP 529: overloaded", { transient: true, reason: "overloaded" }],// ...but a status quoted inside the same clause stays nested, even on a later line.["API Error: 410: resource gone\nDetails: the backend returned HTTP 503", { transient: false, reason: "invalid_request" }],+ ["API Error: 410: resource gone Error: proxy said HTTP 503", { transient: false, reason: "invalid_request" }],["API Error: 410: resource gone\nOpenAI HTTP 409: conflict", { transient: false, reason: "invalid_request" }],["xAI HTTP 429: too many requests", { transient: true, reason: "rate_limited" }],["(status 503 Service Unavailable)", { transient: true, reason: "server_error" }],@@ -303,6 +306,10 @@ describe("classifyError reads an explicit HTTP status before body text", () => {.toEqual({ transient: true, reason: "server_error" });expect(classifyError({ text: "OpenAI HTTP 409: conflict with the current resource state" }, { openAi: true })).toEqual({ transient: true, reason: "server_error" });+ expect(classifyError({ text: "API Error: 410: resource gone\nOpenAI HTTP 409: conflict" }, { openAi: true }))+ .toEqual({ transient: true, reason: "server_error" });+ expect(classifyError({ text: "API Error: 410: resource gone\nxAI HTTP 409: conflict" }, { openAi: true }))+ .toEqual({ transient: true, reason: "server_error" });expect(classifyError({ text: "API Error: 409 Conflict" })).toEqual({ transient: false, reason: "invalid_request" });expect(classifyError({ text: "OpenAI HTTP 409: max 500 tokens" }))diff --git a/server/drivers/retry.ts b/server/drivers/retry.tsindex 23c552fee3..0631e26982 100644--- a/server/drivers/retry.ts+++ b/server/drivers/retry.ts@@ -98,25 +98,40 @@ const messageOf = (err: FailureInput): string => {* it appears: a status quoted inside the provider's message never wins over it.* A status quoted from a nested backend error still must not shadow a transient failure the same string* reports in a later clause: a CLI stderr tail, or a chained cause (messageOf joins it after "Error: ").- * Such a later status only decides when it opens its own report: the start of a line, or right after the- * cause marker. A status quoted mid-clause ("proxy said HTTP 409") stays nested. */+ * Such a later status only decides when it opens its own report: the start of a line (a provider name+ * may precede it), or right after the cause marker. A status quoted mid-clause ("proxy said HTTP 409")+ * stays nested. */const HTTP_STATUS = /\(status (\d{3})\b[^)]*\)|\bHTTP (\d{3})\b|\bAPI Error:? (\d{3})\b/i;-/** Whether a status-form match opens its own error report instead of quoting one inside the current clause:- * it starts a line (a CLI stderr tail), or follows the "Error: " prefix messageOf adds for a chained cause. */-function opensOwnReport(text: string, match: RegExpExecArray, gapStart: number): boolean {- if (text.slice(gapStart, match.index).includes("Error: ")) return true;- const lineStart = text.lastIndexOf("\n", match.index - 1) + 1;- return text.slice(lineStart, match.index).trim() === "";+function opensOwnReport(+ text: string,+ match: RegExpExecArray,+ gapStart: number,+ lineStart: number,+ lineHasMatch: boolean,+): boolean {+ const gap = text.slice(gapStart, match.index);+ const causeAt = gap.lastIndexOf("Error: ");+ if (causeAt !== -1 && /^[\w-]*\s*$/.test(gap.slice(causeAt + "Error: ".length))) return true;+ if (lineHasMatch) return false;+ const prefix = text.slice(lineStart, match.index).trim();+ return prefix === "" || /^[A-Za-z][\w.-]*$/.test(prefix);}/** The transient verdict a status carries on its own, if any: 5xx reads server_error here because this runs- * before the body patterns, which would read the same number the same way. */-function transientStatusVerdict(status: number, provider: { openAi?: boolean }): ErrorClassification | undefined {+ * before the body patterns, which would read the same number the same way — unless the report body says+ * overloaded, which keeps that reason, matching the report on its own. */+function transientStatusVerdict(+ status: number,+ provider: { openAi?: boolean },+ body = "",+): ErrorClassification | undefined {if (status === 429) return { transient: true, reason: "rate_limited" };if (status === 408) return { transient: true, reason: "timeout" };if (status === 409 && provider.openAi) return { transient: true, reason: "server_error" };- if (status >= 500 && status < 600) return { transient: true, reason: "server_error" };+ if (status >= 500 && status < 600) {+ return { transient: true, reason: /\boverloaded\b|\bcapacity\b/i.test(body) ? "overloaded" : "server_error" };+ }return undefined;}@@ -136,13 +151,27 @@ function httpStatusVerdict(text: string, provider: { openAi?: boolean }): ErrorCconst rest = new RegExp(HTTP_STATUS.source, "gi");rest.lastIndex = match!.index + match![0].length;let prevEnd = rest.lastIndex;+ let lineStart = text.lastIndexOf("\n", match!.index - 1) + 1;+ let lineHasMatch = true;let later: RegExpExecArray | null;while ((later = rest.exec(text)) !== null) {- if (opensOwnReport(text, later, prevEnd)) {- const verdict = transientStatusVerdict(Number(later[1] ?? later[2] ?? later[3]), provider);+ let newline = text.indexOf("\n", prevEnd);+ while (newline !== -1 && newline < later.index) {+ lineStart = newline + 1;+ lineHasMatch = false;+ newline = text.indexOf("\n", lineStart);+ }+ if (opensOwnReport(text, later, prevEnd, lineStart, lineHasMatch)) {+ const lineEnd = text.indexOf("\n", later.index);+ const verdict = transientStatusVerdict(+ Number(later[1] ?? later[2] ?? later[3]),+ provider,+ text.slice(later.index + later[0].length, lineEnd === -1 ? undefined : lineEnd),+ );if (verdict) return verdict;}prevEnd = later.index + later[0].length;+ lineHasMatch = true;}return { transient: false, reason: "invalid_request" };}