Skip to content

Commit 15f24c4

Browse files
fix(browser): keep inspection state independent of error wording (#128625)
* fix(browser): preserve action error codes * fix(browser): retain legacy evaluation fallback * fix(browser): preserve unknown action-code presence * fix(browser): preserve error payload narrowing
1 parent b463f4b commit 15f24c4

12 files changed

Lines changed: 237 additions & 39 deletions

‎extensions/browser/src/browser-node-proxy.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ vi.mock("./browser-tool.runtime.js", () => runtimeMocks);
5050
vi.mock("./browser-proxy-upload.js", () => uploadMocks);
5151

5252
import { createBrowserNodeProxyRequest } from "./browser-node-proxy.js";
53+
import { BrowserServiceError } from "./browser/client-fetch.js";
5354

5455
function createSessionProxy() {
5556
return createBrowserNodeProxyRequest({
@@ -84,6 +85,38 @@ beforeEach(() => {
8485
});
8586

8687
describe("Browser node proxy nested watchdogs", () => {
88+
it.each([
89+
{
90+
code: "ACT_EVALUATE_DISABLED",
91+
expected: { code: "ACT_EVALUATE_DISABLED", unrecognizedCode: undefined },
92+
},
93+
{
94+
code: "ACT_FUTURE_CODE",
95+
expected: { code: undefined, unrecognizedCode: true },
96+
},
97+
])("preserves the bounded action-code state for $code", async ({ code, expected }) => {
98+
runtimeMocks.callGatewayTool.mockResolvedValueOnce({
99+
payload: {
100+
error: {
101+
status: 403,
102+
body: { error: "evaluation disabled", code },
103+
},
104+
},
105+
} as unknown as BrowserNodeResponse);
106+
107+
const error = await createSessionProxy()({ method: "POST", path: "/act" }).catch(
108+
(caught: unknown) => caught,
109+
);
110+
111+
expect(error).toBeInstanceOf(BrowserServiceError);
112+
expect(error).toMatchObject({
113+
name: "BrowserServiceError",
114+
message: "evaluation disabled",
115+
status: 403,
116+
...expected,
117+
});
118+
});
119+
87120
it("keeps a requested action inside separate node and Gateway watchdogs", async () => {
88121
const signal = new AbortController().signal;
89122

‎extensions/browser/src/browser-node-proxy.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export function createBrowserNodeProxyRequest(params: {
213213
const failure = parseBrowserProxyFailure(proxy);
214214
if (failure) {
215215
const { status, body } = failure.error;
216-
throw new BrowserServiceError(body.error, "reason" in body ? body : undefined, status);
216+
throw new BrowserServiceError(body.error, body, status);
217217
}
218218
if (!("result" in proxy)) {
219219
throw new Error("Browser proxy returned a failure without an error payload.");
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { describe, expect, it } from "vitest";
2+
import { createBrowserProxyFailure, parseBrowserProxyFailure } from "./browser-proxy-envelope.js";
3+
4+
describe("browser proxy error envelope", () => {
5+
it("preserves validated action error codes and drops unknown metadata", () => {
6+
const failure = createBrowserProxyFailure(403, {
7+
error: "evaluation disabled",
8+
code: "ACT_EVALUATE_DISABLED",
9+
untrusted: "drop me",
10+
});
11+
12+
expect(parseBrowserProxyFailure(failure)).toEqual({
13+
error: {
14+
status: 403,
15+
body: { error: "evaluation disabled", code: "ACT_EVALUATE_DISABLED" },
16+
},
17+
});
18+
});
19+
20+
it("preserves unknown action-code presence without forwarding its value", () => {
21+
const failure = createBrowserProxyFailure(403, {
22+
error: "evaluation disabled",
23+
code: "ACT_FUTURE_CODE",
24+
});
25+
26+
expect(parseBrowserProxyFailure(failure)).toEqual({
27+
error: {
28+
status: 403,
29+
body: { error: "evaluation disabled", unrecognizedCode: true },
30+
},
31+
});
32+
});
33+
});

‎extensions/browser/src/browser-proxy-envelope.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ResolvedBrowserProfile } from "./browser/config.js";
33
/**
44
* Browser node-proxy response envelope shared by the node host and Gateway.
55
*/
6-
import { parseBrowserErrorPayload, type BrowserNoDisplayErrorMetadata } from "./browser/errors.js";
6+
import { parseBrowserErrorPayload, type BrowserErrorPayload } from "./browser/errors.js";
77

88
/** Additive opt-in for structured browser route errors over node.invoke. */
99
export const BROWSER_PROXY_ERROR_ENVELOPE = "browser-v1" as const;
@@ -104,9 +104,7 @@ export function visitBrowserProxyFilePaths(
104104
}
105105
}
106106

107-
type BrowserProxyErrorBody =
108-
| { error: string }
109-
| ({ error: string } & BrowserNoDisplayErrorMetadata);
107+
type BrowserProxyErrorBody = BrowserErrorPayload;
110108

111109
export type BrowserProxySuccess = {
112110
result: unknown;

‎extensions/browser/src/browser/client-fetch.error-body-boundary.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ describe("fetchHttpJson error body boundary", () => {
156156
return;
157157
}
158158

159+
if (req.url === "/evaluate-disabled") {
160+
res.writeHead(403, { "Content-Type": "application/json" });
161+
res.end(JSON.stringify({ error: "evaluation disabled", code: "ACT_EVALUATE_DISABLED" }));
162+
return;
163+
}
164+
159165
res.writeHead(500, { "Content-Type": "text/plain" });
160166
let written = 0;
161167
let closed = false;
@@ -274,4 +280,17 @@ describe("fetchHttpJson error body boundary", () => {
274280
},
275281
});
276282
});
283+
284+
it("preserves validated browser action error codes over HTTP", async () => {
285+
const error = await fetchBrowserJson(`${baseUrl}/evaluate-disabled`).catch(
286+
(err: unknown) => err,
287+
);
288+
289+
expect(error).toMatchObject({
290+
name: "BrowserServiceError",
291+
message: "evaluation disabled",
292+
code: "ACT_EVALUATE_DISABLED",
293+
status: 403,
294+
});
295+
});
277296
});

‎extensions/browser/src/browser/client-fetch.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import { resolveBrowserConfig, resolveProfile } from "./config.js";
2525
import { resolveBrowserControlAuth } from "./control-auth.js";
2626
import {
2727
parseBrowserErrorPayload,
28+
type BrowserActErrorCode,
29+
type BrowserErrorPayload,
2830
type BrowserNoDisplayErrorMetadata,
2931
type BrowserNoDisplayErrorDetails,
3032
} from "./errors.js";
@@ -34,15 +36,19 @@ import { resolveBrowserRateLimitMessage } from "./rate-limit-message.js";
3436
// but returned an error response). Must NOT be wrapped with "Can't reach ..." messaging.
3537
export class BrowserServiceError extends Error {
3638
readonly status?: number;
39+
readonly code?: BrowserActErrorCode;
40+
readonly unrecognizedCode?: true;
3741
readonly reason?: BrowserNoDisplayErrorMetadata["reason"];
3842
readonly details?: BrowserNoDisplayErrorDetails;
3943

40-
constructor(message: string, metadata?: BrowserNoDisplayErrorMetadata, status?: number) {
44+
constructor(message: string, metadata?: BrowserErrorPayload, status?: number) {
4145
super(message);
4246
this.name = "BrowserServiceError";
4347
this.status = status;
44-
this.reason = metadata?.reason;
45-
this.details = metadata?.details;
48+
this.code = metadata?.code;
49+
this.unrecognizedCode = metadata?.unrecognizedCode;
50+
this.reason = metadata && "reason" in metadata ? metadata.reason : undefined;
51+
this.details = metadata && "details" in metadata ? metadata.details : undefined;
4652
}
4753
}
4854

@@ -56,7 +62,7 @@ function browserServiceErrorFromPayload(
5662
const modelHint = resolveBrowserServiceModelHint(message, status);
5763
return new BrowserServiceError(
5864
modelHint ? appendBrowserToolModelHint(message, modelHint) : message,
59-
parsed && "reason" in parsed ? parsed : undefined,
65+
parsed ?? undefined,
6066
status,
6167
);
6268
}

‎extensions/browser/src/browser/errors.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
11
// Browser tests cover errors plugin behavior.
22
import { describe, expect, it } from "vitest";
33
import {
4+
BROWSER_ACT_ERROR_CODES,
45
BROWSER_ERROR_REASONS,
56
BrowserProfileUnavailableError,
67
BrowserTabNotFoundError,
78
parseBrowserErrorPayload,
89
toBrowserErrorResponse,
910
} from "./errors.js";
1011

12+
describe("browser action errors", () => {
13+
it("preserves known codes and drops unknown route metadata", () => {
14+
expect(
15+
parseBrowserErrorPayload({
16+
error: "evaluation disabled",
17+
code: BROWSER_ACT_ERROR_CODES.evaluateDisabled,
18+
untrusted: "drop me",
19+
}),
20+
).toEqual({
21+
error: "evaluation disabled",
22+
code: BROWSER_ACT_ERROR_CODES.evaluateDisabled,
23+
});
24+
expect(parseBrowserErrorPayload({ error: "failure", code: "UNTRUSTED_CODE" })).toEqual({
25+
error: "failure",
26+
unrecognizedCode: true,
27+
});
28+
});
29+
});
30+
1131
describe("BrowserTabNotFoundError", () => {
1232
it("teaches agents that bare numbers are not stable tab targets", () => {
1333
const err = new BrowserTabNotFoundError({ input: "2" });

‎extensions/browser/src/browser/errors.ts‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,27 @@ export const BROWSER_ERROR_REASONS = {
1414
noDisplayForHeadedProfile: "no_display_for_headed_profile",
1515
} as const;
1616

17+
/** Stable machine-readable codes returned by browser action routes. */
18+
export const BROWSER_ACT_ERROR_CODES = {
19+
kindRequired: "ACT_KIND_REQUIRED",
20+
invalidRequest: "ACT_INVALID_REQUEST",
21+
selectorUnsupported: "ACT_SELECTOR_UNSUPPORTED",
22+
evaluateDisabled: "ACT_EVALUATE_DISABLED",
23+
unsupportedForExistingSession: "ACT_EXISTING_SESSION_UNSUPPORTED",
24+
targetIdMismatch: "ACT_TARGET_ID_MISMATCH",
25+
} as const;
26+
27+
export type BrowserActErrorCode =
28+
(typeof BROWSER_ACT_ERROR_CODES)[keyof typeof BROWSER_ACT_ERROR_CODES];
29+
30+
const BROWSER_ACT_ERROR_CODE_VALUES: ReadonlySet<string> = new Set(
31+
Object.values(BROWSER_ACT_ERROR_CODES),
32+
);
33+
34+
function isBrowserActErrorCode(value: unknown): value is BrowserActErrorCode {
35+
return typeof value === "string" && BROWSER_ACT_ERROR_CODE_VALUES.has(value);
36+
}
37+
1738
const NO_DISPLAY_HEADLESS_SOURCES = ["request", "env", "profile", "config", "default"] as const;
1839

1940
export type BrowserNoDisplayErrorDetails = {
@@ -30,7 +51,11 @@ export type BrowserNoDisplayErrorMetadata = {
3051

3152
type WithNoDisplayMetadata<T> = T | (T & BrowserNoDisplayErrorMetadata);
3253
export type BrowserErrorResponse = WithNoDisplayMetadata<{ status: number; message: string }>;
33-
type BrowserErrorPayload = WithNoDisplayMetadata<{ error: string }>;
54+
export type BrowserErrorPayload = WithNoDisplayMetadata<{
55+
error: string;
56+
code?: BrowserActErrorCode;
57+
unrecognizedCode?: true;
58+
}>;
3459

3560
/** Base browser error carrying an HTTP status code. */
3661
export class BrowserError extends Error {
@@ -186,11 +211,19 @@ export function parseBrowserErrorPayload(value: unknown): BrowserErrorPayload |
186211
if (typeof body.error !== "string" || body.error.length === 0) {
187212
return null;
188213
}
214+
const code = isBrowserActErrorCode(body.code) ? body.code : undefined;
215+
const unrecognizedCode =
216+
body.unrecognizedCode === true || (body.code !== undefined && !code) ? true : undefined;
217+
const actionCode: { code?: BrowserActErrorCode; unrecognizedCode?: true } = code
218+
? { code }
219+
: unrecognizedCode
220+
? { unrecognizedCode: true }
221+
: {};
189222
if (body.reason === BROWSER_ERROR_REASONS.noDisplayForHeadedProfile) {
190223
const details = parseNoDisplayDetails(body.details);
191224
if (details) {
192-
return { error: body.error, reason: body.reason, details };
225+
return { error: body.error, ...actionCode, reason: body.reason, details };
193226
}
194227
}
195-
return { error: body.error };
228+
return { error: body.error, ...actionCode };
196229
}

‎extensions/browser/src/browser/routes/agent.act.errors.ts‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BROWSER_ACT_ERROR_CODES, type BrowserActErrorCode } from "../errors.js";
12
/**
23
* Shared browser action error codes and messages.
34
*
@@ -6,23 +7,13 @@
67
*/
78
import type { BrowserResponse } from "./types.js";
89

9-
/** Stable machine-readable codes returned by browser action routes. */
10-
export const ACT_ERROR_CODES = {
11-
kindRequired: "ACT_KIND_REQUIRED",
12-
invalidRequest: "ACT_INVALID_REQUEST",
13-
selectorUnsupported: "ACT_SELECTOR_UNSUPPORTED",
14-
evaluateDisabled: "ACT_EVALUATE_DISABLED",
15-
unsupportedForExistingSession: "ACT_EXISTING_SESSION_UNSUPPORTED",
16-
targetIdMismatch: "ACT_TARGET_ID_MISMATCH",
17-
} as const;
18-
19-
type ActErrorCode = (typeof ACT_ERROR_CODES)[keyof typeof ACT_ERROR_CODES];
10+
export const ACT_ERROR_CODES = BROWSER_ACT_ERROR_CODES;
2011

2112
/** Send a browser action JSON error with a stable action error code. */
2213
export function jsonActError(
2314
res: BrowserResponse,
2415
status: number,
25-
code: ActErrorCode,
16+
code: BrowserActErrorCode,
2617
message: string,
2718
) {
2819
res.status(status).json({ error: message, code });

‎extensions/browser/src/gateway/browser-request.profile-from-body.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,30 @@ describe("browser.request profile selection", () => {
543543
});
544544
});
545545

546+
it.each([
547+
{
548+
name: "recognized action code",
549+
body: { error: "evaluation disabled", code: "ACT_EVALUATE_DISABLED" },
550+
details: { error: "evaluation disabled", code: "ACT_EVALUATE_DISABLED" },
551+
},
552+
{
553+
name: "unrecognized action code",
554+
body: { error: "evaluation disabled", code: "ACT_FUTURE_CODE" },
555+
details: { error: "evaluation disabled", unrecognizedCode: true },
556+
},
557+
])("preserves bounded $name state through the node proxy", async ({ body, details }) => {
558+
const { respond } = await runBrowserRequest(
559+
{ method: "POST", path: "/act" },
560+
{ ok: true, payload: { error: { status: 403, body } } },
561+
);
562+
563+
expect(firstRespondCall(respond)[2]).toEqual({
564+
code: "INVALID_REQUEST",
565+
message: "evaluation disabled",
566+
details,
567+
});
568+
});
569+
546570
it("returns UNAVAILABLE for an incomplete node file envelope", async () => {
547571
const { respond } = await runBrowserRequest(
548572
{ method: "POST", path: "/screenshot" },

0 commit comments

Comments
 (0)