Skip to content

Commit 85edbc3

Browse files
committed
Merge remote-tracking branch 'origin/main' into pr-123235-prep
2 parents 8d5ae29 + 28092cb commit 85edbc3

8 files changed

Lines changed: 95 additions & 3 deletions

‎src/agents/embedded-agent-helpers/provider-runtime-failure.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ function expectNotFailoverSample(sample: string) {
1717
}
1818

1919
describe("classifyProviderRuntimeFailureKind", () => {
20+
it("classifies complete HTML after an HTTP reason phrase as upstream_html", () => {
21+
const raw = "HTTP 502 Bad Gateway\n\n<!doctype html><html><body>down</body></html>";
22+
23+
expect(classifyProviderRuntimeFailureKind(raw)).toBe("upstream_html");
24+
});
25+
2026
it("classifies generic resource-exhausted codes as rate_limit", () => {
2127
expect(
2228
classifyProviderRuntimeFailureKind({

‎src/agents/embedded-agent-helpers/provider-runtime-failure.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { normalizeLowercaseStringOrEmpty } from "@openclaw/normalization-core/string-coerce";
22
import { extractLeadingHttpStatus } from "../../shared/assistant-error-format.js";
3+
import { extractHttpResponseBody } from "../../shared/http-error-response.js";
34
import { classifyOAuthRefreshFailure } from "../auth-profiles/oauth-refresh-failure.js";
45
import { formatExecDeniedUserMessage } from "../exec-approval-result.js";
56
import {
@@ -70,7 +71,7 @@ function isHtmlErrorResponse(raw: string, status?: number): boolean {
7071
if (typeof inferred !== "number" || inferred < 400) {
7172
return false;
7273
}
73-
const rest = extractLeadingHttpStatus(candidate)?.rest ?? candidate;
74+
const rest = extractHttpResponseBody(extractLeadingHttpStatus(candidate))?.body ?? candidate;
7475
return HTML_BODY_RE.test(rest) && HTML_CLOSE_RE.test(rest);
7576
}
7677
function isCloudflareChallengeResponse(message: string): boolean {

‎src/shared/assistant-error-format.html.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ describe("isCloudflareOrHtmlErrorPage", () => {
1717
expect(isCloudflareOrHtmlErrorPage(htmlError)).toBe(true);
1818
});
1919

20+
it("detects complete 5xx HTML pages after an HTTP reason phrase", () => {
21+
const htmlError = "HTTP 502 Bad Gateway\n\n<!doctype html><html><body>down</body></html>";
22+
expect(isCloudflareOrHtmlErrorPage(htmlError)).toBe(true);
23+
});
24+
25+
it("does not flag partial HTML after an HTTP reason phrase", () => {
26+
const partialHtml = "HTTP 502 Bad Gateway\n\n<!doctype html><html><body>down";
27+
expect(isCloudflareOrHtmlErrorPage(partialHtml)).toBe(false);
28+
});
29+
2030
it("detects standalone Cloudflare challenge HTML pages", () => {
2131
// HTML challenge pages are provider transport failures, not model text.
2232
const htmlError = `<!DOCTYPE html>

‎src/shared/assistant-error-format.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ describe("extractErrorHttpStatus", () => {
8181
});
8282

8383
describe("HTTP status consumers", () => {
84+
it("does not return raw HTML after an HTTP reason phrase", () => {
85+
const raw = [
86+
"HTTP 502 Bad Gateway",
87+
"",
88+
"<!doctype html><html><body><h1>502</h1></body></html>",
89+
].join("\n");
90+
91+
expect(formatRawAssistantErrorForUi(raw)).toBe(
92+
"The AI service is temporarily unavailable (HTTP 502). Please try again in a moment.",
93+
);
94+
});
95+
8496
it("formats only status lines inside the HTTP range", () => {
8597
expect(formatRawAssistantErrorForUi("100 Continue")).toBe("HTTP 100: Continue");
8698
expect(formatRawAssistantErrorForUi("599 Provider Error")).toBe("HTTP 599: Provider Error");

‎src/shared/assistant-error-format.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Assistant error formatting helpers normalize assistant-visible error payloads.
22
import { truncateUtf16Safe } from "@openclaw/normalization-core/utf16-slice";
3+
import { extractHttpResponseBody } from "./http-error-response.js";
34
const ERROR_PAYLOAD_PREFIX_RE =
45
/^(?:error|(?:[a-z][\w-]*\s+)?api\s*error|apierror|openai\s*error|anthropic\s*error|gateway\s*error|codex\s*error)(?:\s+\d{3})?[:\s-]+/i;
56
const HTTP_STATUS_DELIMITER_RE = /(?:\s*:\s*|\s+)/;
@@ -160,7 +161,7 @@ export function isCloudflareOrHtmlErrorPage(raw: string): boolean {
160161
return true;
161162
}
162163

163-
const status = extractLeadingHttpStatus(trimmed);
164+
const status = extractHttpResponseBody(extractLeadingHttpStatus(trimmed));
164165
if (!status || status.code < 500) {
165166
return false;
166167
}
@@ -170,7 +171,7 @@ export function isCloudflareOrHtmlErrorPage(raw: string): boolean {
170171
}
171172

172173
return (
173-
status.code < 600 && HTML_ERROR_PREFIX_RE.test(status.rest) && HTML_CLOSE_RE.test(status.rest)
174+
status.code < 600 && HTML_ERROR_PREFIX_RE.test(status.body) && HTML_CLOSE_RE.test(status.body)
174175
);
175176
}
176177

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { describe, expect, it } from "vitest";
2+
import { extractHttpResponseBody } from "./http-error-response.js";
3+
4+
describe("extractHttpResponseBody", () => {
5+
it.each([
6+
{
7+
name: "direct HTML body",
8+
rest: "<html><body>down</body></html>",
9+
expected: "<html><body>down</body></html>",
10+
},
11+
{
12+
name: "HTTP reason phrase before HTML",
13+
rest: "Bad Gateway\n\n<!doctype html><html><body>down</body></html>",
14+
expected: "<!doctype html><html><body>down</body></html>",
15+
},
16+
{
17+
name: "non-HTML status text",
18+
rest: "Service Unavailable",
19+
expected: "Service Unavailable",
20+
},
21+
])("extracts $name", ({ rest, expected }) => {
22+
expect(extractHttpResponseBody({ code: 502, rest })).toEqual({
23+
code: 502,
24+
body: expected,
25+
});
26+
});
27+
28+
it("preserves a missing status", () => {
29+
expect(extractHttpResponseBody(null)).toBeNull();
30+
});
31+
});

‎src/shared/http-error-response.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const HTML_ERROR_PREFIX_RE = /^\s*(?:<!doctype\s+html\b|<html\b)/i;
2+
3+
export function extractHttpResponseBody(
4+
status: { code: number; rest: string } | null,
5+
): { code: number; body: string } | null {
6+
if (!status) {
7+
return null;
8+
}
9+
if (HTML_ERROR_PREFIX_RE.test(status.rest)) {
10+
return { code: status.code, body: status.rest };
11+
}
12+
const lineBreak = status.rest.indexOf("\n");
13+
return {
14+
code: status.code,
15+
body: lineBreak === -1 ? status.rest : status.rest.slice(lineBreak + 1).trimStart(),
16+
};
17+
}

‎src/tui/tui-formatters.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,25 @@ import {
1212
isolateRtlRenderedLine,
1313
isTerminalSafeAutocompleteValue,
1414
isCommandMarkedMessage,
15+
resolveFinalAssistantText,
1516
sanitizeMarkdownSource,
1617
sanitizeRenderableLine,
1718
sanitizeRenderableText,
1819
} from "./tui-formatters.js";
1920

21+
describe("resolveFinalAssistantText", () => {
22+
it("hides complete HTML error pages after an HTTP reason phrase", () => {
23+
const raw = "HTTP 502 Bad Gateway\n\n<!doctype html><html><body>down</body></html>";
24+
25+
const rendered = resolveFinalAssistantText({ errorMessage: raw });
26+
27+
expect(rendered).toBe(
28+
"The AI service is temporarily unavailable (HTTP 502). Please try again in a moment.",
29+
);
30+
expect(rendered).not.toContain("<html>");
31+
});
32+
});
33+
2034
describe("formatTuiFooter", () => {
2135
it("shows session modes and the process delivery mode in one compact summary", () => {
2236
expect(

0 commit comments

Comments
 (0)