Skip to content

Commit c63cc24

Browse files
committed
fix(browser): share shipped navigation limits across all drivers
1 parent 9139680 commit c63cc24

11 files changed

Lines changed: 177 additions & 21 deletions

‎extensions/browser/src/browser-tool.test.ts‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2486,6 +2486,28 @@ describe("browser tool url alias support", () => {
24862486
});
24872487
});
24882488

2489+
it.each([
2490+
{ requestedTimeoutMs: 10, expectedTimeoutMs: 1_000 },
2491+
{ requestedTimeoutMs: 180_000, expectedTimeoutMs: 120_000 },
2492+
{ requestedTimeoutMs: Number.MAX_SAFE_INTEGER, expectedTimeoutMs: 120_000 },
2493+
])(
2494+
"normalizes host navigation timeout $requestedTimeoutMs before browser dispatch",
2495+
async ({ requestedTimeoutMs, expectedTimeoutMs }) => {
2496+
await createBrowserTool().execute?.("call-1", {
2497+
action: "navigate",
2498+
target: "host",
2499+
url: "https://example.com/slow",
2500+
targetId: "tab-1",
2501+
timeoutMs: requestedTimeoutMs,
2502+
});
2503+
2504+
expect(browserActionsMocks.browserNavigate).toHaveBeenCalledWith(
2505+
undefined,
2506+
expect.objectContaining({ timeoutMs: expectedTimeoutMs }),
2507+
);
2508+
},
2509+
);
2510+
24892511
it("preserves an explicit navigation timeout across node and Gateway watchdogs", async () => {
24902512
mockSingleBrowserProxyNode();
24912513
gatewayMocks.callGatewayTool
@@ -2528,6 +2550,54 @@ describe("browser tool url alias support", () => {
25282550
});
25292551
});
25302552

2553+
it.each([
2554+
{ requestedTimeoutMs: 10, expectedTimeoutMs: 1_000 },
2555+
{ requestedTimeoutMs: 180_000, expectedTimeoutMs: 120_000 },
2556+
{ requestedTimeoutMs: Number.MAX_SAFE_INTEGER, expectedTimeoutMs: 120_000 },
2557+
])(
2558+
"keeps normalized node navigation timeout $requestedTimeoutMs inside nested watchdogs",
2559+
async ({ requestedTimeoutMs, expectedTimeoutMs }) => {
2560+
mockSingleBrowserProxyNode();
2561+
gatewayMocks.callGatewayTool
2562+
.mockResolvedValueOnce({
2563+
ok: true,
2564+
payload: {
2565+
result: { ok: true, targetId: "tab-1", url: "https://example.com/slow" },
2566+
},
2567+
})
2568+
.mockResolvedValueOnce({
2569+
ok: true,
2570+
payload: {
2571+
result: {
2572+
ok: true,
2573+
format: "ai",
2574+
targetId: "tab-1",
2575+
url: "https://example.com/slow",
2576+
snapshot: "slow page",
2577+
},
2578+
},
2579+
});
2580+
2581+
await createBrowserTool().execute?.("call-1", {
2582+
action: "navigate",
2583+
target: "node",
2584+
url: "https://example.com/slow",
2585+
targetId: "tab-1",
2586+
timeoutMs: requestedTimeoutMs,
2587+
});
2588+
2589+
const { options, request } = nodeInvokeCall(0);
2590+
expect(options.timeoutMs).toBe(expectedTimeoutMs + 10_000);
2591+
expect(request.timeoutMs).toBe(expectedTimeoutMs + 5_000);
2592+
expect(request.params?.timeoutMs).toBe(expectedTimeoutMs);
2593+
expect(request.params?.body).toEqual({
2594+
url: "https://example.com/slow",
2595+
targetId: "tab-1",
2596+
timeoutMs: expectedTimeoutMs,
2597+
});
2598+
},
2599+
);
2600+
25312601
it("returns inline page state after navigate", async () => {
25322602
browserActionsMocks.browserNavigate.mockResolvedValueOnce({
25332603
ok: true,

‎extensions/browser/src/browser-tool.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import {
6969
validateJsonSchemaValue,
7070
} from "./browser-tool.runtime.js";
7171
import { appendNavigatedPageState, executeSnapshotAction } from "./browser-tool.snapshot.js";
72+
import { resolveBrowserNavigationTimeoutMs } from "./browser/act-policy.js";
7273
import { DEFAULT_BROWSER_SCREENSHOT_TIMEOUT_MS } from "./browser/constants.js";
7374
import { parseBrowserNavigationUrl } from "./browser/navigation-guard.js";
7475
import { normalizeBrowserScreenshot } from "./browser/screenshot.js";
@@ -846,6 +847,10 @@ export function createBrowserTool(opts?: {
846847
case "navigate": {
847848
const targetUrl = readTargetUrlParam(params);
848849
const targetId = readStringParam(params, "targetId");
850+
const timeoutMs =
851+
requestedTimeoutMs === undefined
852+
? undefined
853+
: resolveBrowserNavigationTimeoutMs(requestedTimeoutMs);
849854
const result = proxyRequest
850855
? await proxyRequest({
851856
method: "POST",
@@ -854,14 +859,14 @@ export function createBrowserTool(opts?: {
854859
body: {
855860
url: targetUrl,
856861
targetId,
857-
timeoutMs: requestedTimeoutMs,
862+
timeoutMs,
858863
},
859-
timeoutMs: requestedTimeoutMs,
864+
timeoutMs,
860865
})
861866
: await browserToolDeps.browserNavigate(baseUrl, {
862867
url: targetUrl,
863868
targetId,
864-
timeoutMs: requestedTimeoutMs,
869+
timeoutMs,
865870
profile,
866871
});
867872
const navigatedTargetId =

‎extensions/browser/src/browser/act-policy.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ export const BROWSER_ACTION_TRANSPORT_SLACK_MS = 5_000;
3636
/** Post-action window that keeps navigation policy interception active. */
3737
export const BROWSER_ACTION_NAVIGATION_GRACE_MS = 250;
3838

39+
/** Keep navigation timeouts consistent across transports and browser backends. */
40+
export function resolveBrowserNavigationTimeoutMs(timeoutMs?: number): number {
41+
return Math.min(120_000, resolveTimerTimeoutMs(timeoutMs, 20_000, 1_000));
42+
}
43+
3944
export function normalizeActBoundedNonNegativeMs(
4045
value: number | undefined,
4146
fieldName: string,

‎extensions/browser/src/browser/chrome-mcp-actions.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
} from "openclaw/plugin-sdk/number-runtime";
99
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
1010
import { resolvePreferredOpenClawTmpDir } from "../infra/tmp-openclaw-dir.js";
11+
import { resolveBrowserNavigationTimeoutMs } from "./act-policy.js";
1112
import {
12-
CHROME_MCP_NAVIGATE_TIMEOUT_MS,
1313
rethrowChromeMcpDocumentError,
1414
type ChromeMcpOperationOptions,
1515
type ChromeMcpProfileOptions,
@@ -104,7 +104,7 @@ export async function navigateChromeMcpPage(params: {
104104
timeoutMs?: number;
105105
signal?: AbortSignal;
106106
}): Promise<{ url: string }> {
107-
const resolvedTimeoutMs = params.timeoutMs ?? CHROME_MCP_NAVIGATE_TIMEOUT_MS;
107+
const resolvedTimeoutMs = resolveBrowserNavigationTimeoutMs(params.timeoutMs);
108108
const callTimeoutMs = resolveChromeMcpNavigateCallTimeoutMs(resolvedTimeoutMs);
109109
return await withChromeMcpTarget({ ...params, timeoutMs: callTimeoutMs }, async (target) => {
110110
await callTool(

‎extensions/browser/src/browser/chrome-mcp.test.ts‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2881,12 +2881,35 @@ describe("chrome MCP page parsing", () => {
28812881
});
28822882

28832883
const callToolMock = session.client["callTool"] as unknown as ToolCallMock;
2884-
const navigateCall = callToolMock.mock.calls.find(
2885-
([call]) => call.name === "navigate_page",
2886-
)?.[0];
2887-
expect(navigateCall?.arguments?.timeout).toBe(20_000);
2884+
const navigateCall = callToolMock.mock.calls.find(([call]) => call.name === "navigate_page");
2885+
expect(navigateCall?.[0].arguments?.timeout).toBe(20_000);
2886+
expect(navigateCall?.[2]?.timeout).toBe(25_000);
28882887
});
28892888

2889+
it.each([
2890+
{ requestedTimeoutMs: 10, expectedTimeoutMs: 1_000 },
2891+
{ requestedTimeoutMs: 180_000, expectedTimeoutMs: 120_000 },
2892+
{ requestedTimeoutMs: Number.MAX_SAFE_INTEGER, expectedTimeoutMs: 120_000 },
2893+
])(
2894+
"normalizes Chrome MCP navigation timeout $requestedTimeoutMs before SDK watchdog grace",
2895+
async ({ requestedTimeoutMs, expectedTimeoutMs }) => {
2896+
const session = createFakeSession();
2897+
setChromeMcpSessionFactoryForTest(async () => session);
2898+
2899+
await navigateChromeMcpPage({
2900+
profileName: "chrome-live",
2901+
targetId: FAKE_TARGET_1,
2902+
url: "https://example.com",
2903+
timeoutMs: requestedTimeoutMs,
2904+
});
2905+
2906+
const callToolMock = session.client["callTool"] as unknown as ToolCallMock;
2907+
const navigateCall = callToolMock.mock.calls.find(([call]) => call.name === "navigate_page");
2908+
expect(navigateCall?.[0].arguments?.timeout).toBe(expectedTimeoutMs);
2909+
expect(navigateCall?.[2]?.timeout).toBe(expectedTimeoutMs + 5_000);
2910+
},
2911+
);
2912+
28902913
it("caps the navigate_page safety-net timeout", () => {
28912914
expect(resolveChromeMcpNavigateCallTimeoutMs(10_000)).toBe(15_000);
28922915
expect(resolveChromeMcpNavigateCallTimeoutMs(Number.MAX_VALUE)).toBe(MAX_TIMER_TIMEOUT_MS);

‎extensions/browser/src/browser/client-actions-core.navigation.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,29 @@ describe("browser navigation client actions", () => {
4545
});
4646
});
4747

48+
it.each([
49+
{ requestedTimeoutMs: 10, expectedTimeoutMs: 1_000 },
50+
{ requestedTimeoutMs: 180_000, expectedTimeoutMs: 120_000 },
51+
{ requestedTimeoutMs: Number.MAX_SAFE_INTEGER, expectedTimeoutMs: 120_000 },
52+
])(
53+
"normalizes navigation timeout $requestedTimeoutMs before arming its transport watchdog",
54+
async ({ requestedTimeoutMs, expectedTimeoutMs }) => {
55+
await browserNavigate(undefined, {
56+
url: "https://example.com/slow",
57+
targetId: "tab-1",
58+
timeoutMs: requestedTimeoutMs,
59+
});
60+
61+
const request = lastNavigationRequest();
62+
expect(request.options.timeoutMs).toBe(expectedTimeoutMs + 5_000);
63+
expect(JSON.parse(request.options.body ?? "{}")).toEqual({
64+
url: "https://example.com/slow",
65+
targetId: "tab-1",
66+
timeoutMs: expectedTimeoutMs,
67+
});
68+
},
69+
);
70+
4871
it("preserves the existing navigation watchdog when no timeout is requested", async () => {
4972
await browserNavigate(undefined, { url: "https://example.com" });
5073

‎extensions/browser/src/browser/client-actions-core.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import {
1313
BROWSER_ACTION_TRANSPORT_SLACK_MS,
1414
resolveBrowserActRequestTimeoutMs,
15+
resolveBrowserNavigationTimeoutMs,
1516
} from "./act-policy.js";
1617
import type {
1718
BrowserActionOk,
@@ -84,14 +85,14 @@ export async function browserNavigate(
8485
},
8586
): Promise<BrowserActionTabResult> {
8687
const q = buildProfileQuery(opts.profile);
88+
const timeoutMs =
89+
opts.timeoutMs === undefined ? undefined : resolveBrowserNavigationTimeoutMs(opts.timeoutMs);
8790
return await fetchBrowserJson<BrowserActionTabResult>(withBaseUrl(baseUrl, `/navigate${q}`), {
8891
method: "POST",
8992
headers: { "Content-Type": "application/json" },
90-
body: JSON.stringify({ url: opts.url, targetId: opts.targetId, timeoutMs: opts.timeoutMs }),
93+
body: JSON.stringify({ url: opts.url, targetId: opts.targetId, timeoutMs }),
9194
timeoutMs:
92-
opts.timeoutMs === undefined
93-
? 20_000
94-
: resolveBrowserOperationRequestTimeoutMs(opts.timeoutMs),
95+
timeoutMs === undefined ? 20_000 : resolveBrowserOperationRequestTimeoutMs(timeoutMs),
9596
});
9697
}
9798

‎extensions/browser/src/browser/pw-tools-core.snapshot.navigate-guard.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,32 @@ describe("pw-tools-core.snapshot navigate guard", () => {
8686
expect(result.url).toBe("https://example.com");
8787
});
8888

89+
it.each([
90+
{ requestedTimeoutMs: undefined, expectedTimeoutMs: 20_000 },
91+
{ requestedTimeoutMs: 180_000, expectedTimeoutMs: 120_000 },
92+
{ requestedTimeoutMs: Number.MAX_SAFE_INTEGER, expectedTimeoutMs: 120_000 },
93+
])(
94+
"applies the shipped navigation timeout contract to Playwright timeout $requestedTimeoutMs",
95+
async ({ requestedTimeoutMs, expectedTimeoutMs }) => {
96+
const goto = vi.fn(async () => {});
97+
setPwToolsCoreCurrentPage({
98+
goto,
99+
url: vi.fn(() => "https://example.com"),
100+
});
101+
102+
await mod.navigateViaPlaywright({
103+
cdpUrl: "http://127.0.0.1:18792",
104+
url: "https://example.com",
105+
timeoutMs: requestedTimeoutMs,
106+
});
107+
108+
expect(goto).toHaveBeenCalledWith("https://example.com", { timeout: expectedTimeoutMs });
109+
expect(getPwToolsCoreSessionMocks().gotoPageWithNavigationGuard).toHaveBeenCalledWith(
110+
expect.objectContaining({ timeoutMs: expectedTimeoutMs }),
111+
);
112+
},
113+
);
114+
89115
it("returns managed download metadata when navigation starts an attachment download", async () => {
90116
const download = {
91117
url: "https://example.com/export.csv",

‎extensions/browser/src/browser/pw-tools-core.snapshot.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
1111
import type { Frame, Page } from "playwright-core";
1212
import type { SsrFPolicy } from "../infra/net/ssrf.js";
13-
import { ACT_MAX_VIEWPORT_DIMENSION } from "./act-policy.js";
13+
import { ACT_MAX_VIEWPORT_DIMENSION, resolveBrowserNavigationTimeoutMs } from "./act-policy.js";
1414
import { type AriaSnapshotNode, formatAriaSnapshot, type RawAXNode } from "./cdp.js";
1515
import type { BrowserDownloadResult } from "./download-types.js";
1616
import {
@@ -56,10 +56,6 @@ function resolveSnapshotTimeoutMs(timeoutMs: number | undefined): number {
5656
return resolveBoundedTimeoutMs(timeoutMs, 5_000, 500, 60_000);
5757
}
5858

59-
function resolveNavigationTimeoutMs(timeoutMs: number | undefined): number {
60-
return resolveBoundedTimeoutMs(timeoutMs, 20_000, 1000, 120_000);
61-
}
62-
6359
function resolveViewportDimension(value: unknown, label: "width" | "height"): number {
6460
const dimension = resolveIntegerOption(value, 1, { min: 1 });
6561
if (dimension > ACT_MAX_VIEWPORT_DIMENSION) {
@@ -509,7 +505,7 @@ export async function navigateViaPlaywright(opts: {
509505
url,
510506
...navigationPolicy,
511507
});
512-
const timeout = resolveNavigationTimeoutMs(opts.timeoutMs);
508+
const timeout = resolveBrowserNavigationTimeoutMs(opts.timeoutMs);
513509
let page = await getPageForTargetId(opts);
514510
let pageState = ensurePageState(page);
515511
const navigate = async () =>

‎extensions/browser/src/browser/routes/agent.snapshot.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import path from "node:path";
88
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
99
import { getImageMetadata } from "../../media/media-services.js";
1010
import { ensureMediaDir, saveMediaBuffer } from "../../media/store.js";
11+
import { resolveBrowserNavigationTimeoutMs } from "../act-policy.js";
1112
import {
1213
captureScreenshot,
1314
getMainFrameDocumentIdentityViaCdp,
@@ -331,7 +332,11 @@ export function registerBrowserAgentSnapshotRoutes(
331332
}
332333
let timeoutMs: number | undefined;
333334
try {
334-
timeoutMs = readRouteTimerTimeoutMs(body.timeoutMs);
335+
const requestedTimeoutMs = readRouteTimerTimeoutMs(body.timeoutMs);
336+
timeoutMs =
337+
requestedTimeoutMs === undefined
338+
? undefined
339+
: resolveBrowserNavigationTimeoutMs(requestedTimeoutMs);
335340
} catch (err) {
336341
return jsonError(res, 400, String(err instanceof Error ? err.message : err));
337342
}

0 commit comments

Comments
 (0)