Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(drivers): cache the next newline while scanning statuses; keep overload detail in later reports

Saim Shafique690b3f3560cd
diff --git a/server/drivers/engine-errors.test.ts b/server/drivers/engine-errors.test.ts
index ea58913f5b..e09ba09874 100644
--- a/server/drivers/engine-errors.test.ts
+++ b/server/drivers/engine-errors.test.ts
@@ -287,6 +287,12 @@ describe("classifyError reads an explicit HTTP status before body text", () => {
["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" }],
+ // A later 5xx report keeps the overloaded reason when the detail sits inside the
+ // status parentheses or on the line after the report.
+ ["API Error: 410: resource gone\n(status 503 overloaded)", { transient: true, reason: "overloaded" }],
+ ["API Error: 410: resource gone\nHTTP 503 Service Unavailable\noverloaded: capacity exceeded", { transient: true, reason: "overloaded" }],
+ // Many quoted statuses on one line still leave the later report's verdict alone.
+ ["API Error: 410: resource gone" + " quoted HTTP 409".repeat(50) + " Error: HTTP 503 Service Unavailable", { transient: true, reason: "server_error" }],
// ...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" }],
diff --git a/server/drivers/retry.ts b/server/drivers/retry.ts
index 0631e26982..fba0e8339a 100644
--- a/server/drivers/retry.ts
+++ b/server/drivers/retry.ts
@@ -120,7 +120,8 @@ function opensOwnReport(
/** 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 — unless the report body says
- * overloaded, which keeps that reason, matching the report on its own. */
+ * overloaded, which keeps that reason, matching the report on its own. The body is the report itself: its
+ * status form, the rest of its line, and the line after it, where a provider puts the detail. */
function transientStatusVerdict(
status: number,
provider: { openAi?: boolean },
@@ -153,20 +154,23 @@ function httpStatusVerdict(text: string, provider: { openAi?: boolean }): ErrorC
let prevEnd = rest.lastIndex;
let lineStart = text.lastIndexOf("\n", match!.index - 1) + 1;
let lineHasMatch = true;
+ let nextNewline = text.indexOf("\n", prevEnd);
let later: RegExpExecArray | null;
while ((later = rest.exec(text)) !== null) {
- let newline = text.indexOf("\n", prevEnd);
- while (newline !== -1 && newline < later.index) {
- lineStart = newline + 1;
+ if (nextNewline !== -1 && nextNewline < prevEnd) nextNewline = text.indexOf("\n", prevEnd);
+ while (nextNewline !== -1 && nextNewline < later.index) {
+ lineStart = nextNewline + 1;
lineHasMatch = false;
- newline = text.indexOf("\n", lineStart);
+ nextNewline = text.indexOf("\n", lineStart);
}
if (opensOwnReport(text, later, prevEnd, lineStart, lineHasMatch)) {
- const lineEnd = text.indexOf("\n", later.index);
+ const matchEnd = later.index + later[0].length;
+ const lineEnd = text.indexOf("\n", matchEnd);
+ const nextLineEnd = lineEnd === -1 ? -1 : text.indexOf("\n", lineEnd + 1);
const verdict = transientStatusVerdict(
Number(later[1] ?? later[2] ?? later[3]),
provider,
- text.slice(later.index + later[0].length, lineEnd === -1 ? undefined : lineEnd),
+ text.slice(later.index, nextLineEnd === -1 ? undefined : nextLineEnd),
);
if (verdict) return verdict;
}