Skip to content

Commit 84a52b3

Browse files
authored
fix(browser): budget sequential action watchdogs (#104659)
* fix(browser): budget sequential action watchdogs * chore: leave browser release note to release flow
1 parent 81a8405 commit 84a52b3

13 files changed

Lines changed: 524 additions & 76 deletions

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
resolveRuntimeImageSanitization,
3030
wrapExternalContent,
3131
} from "./browser-tool.runtime.js";
32+
import { resolveBrowserActRequestTimeoutMs } from "./browser/act-policy.js";
3233
import {
3334
DEFAULT_BROWSER_ACTION_TIMEOUT_MS,
3435
DEFAULT_BROWSER_DOWNLOAD_TIMEOUT_MS,
@@ -47,7 +48,6 @@ const browserToolActionDeps = {
4748
imageResultFromFile,
4849
};
4950

50-
const BROWSER_ACT_REQUEST_TIMEOUT_SLACK_MS = 5_000;
5151
const BROWSER_DOWNLOAD_REQUEST_TIMEOUT_SLACK_MS = 5_000;
5252

5353
type BrowserActRequest = Parameters<typeof browserAct>[1];
@@ -128,20 +128,7 @@ function withConfiguredActTimeout(
128128
}
129129

130130
function resolveActProxyTimeoutMs(request: BrowserActRequest): number | undefined {
131-
const candidateTimeouts: number[] = [];
132-
const explicitTimeout = normalizePositiveTimeoutMs(
133-
(request as BrowserActRequestWithTimeout).timeoutMs,
134-
);
135-
if (explicitTimeout !== undefined) {
136-
candidateTimeouts.push(explicitTimeout + BROWSER_ACT_REQUEST_TIMEOUT_SLACK_MS);
137-
}
138-
if (request.kind === "wait") {
139-
const waitDuration = normalizeNonNegativeDurationMs(request.timeMs);
140-
if (waitDuration !== undefined) {
141-
candidateTimeouts.push(waitDuration + BROWSER_ACT_REQUEST_TIMEOUT_SLACK_MS);
142-
}
143-
}
144-
return candidateTimeouts.length ? Math.max(...candidateTimeouts) : undefined;
131+
return resolveBrowserActRequestTimeoutMs(request);
145132
}
146133

147134
export const testing = {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,14 @@ describe("browser tool schema", () => {
4646
expect(properties.kind.enum).toContain("scrollIntoView");
4747
expect(requestProperties.kind.enum).toContain("scrollIntoView");
4848
});
49+
50+
it("exposes batch actions on nested and flattened act params", () => {
51+
const properties = BrowserToolSchema.properties as BrowserSchemaRecord;
52+
const requestProperties = properties.request.properties as BrowserSchemaRecord;
53+
54+
expect(properties.kind.enum).toContain("batch");
55+
expect(properties.actions).toBeDefined();
56+
expect(requestProperties.kind.enum).toContain("batch");
57+
expect(requestProperties.actions).toBeDefined();
58+
});
4959
});

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { Type } from "typebox";
1515
import { ACT_MAX_VIEWPORT_DIMENSION } from "./browser/act-policy.js";
1616

1717
const BROWSER_ACT_KINDS = [
18+
"batch",
1819
"click",
1920
"clickCoords",
2021
"type",
@@ -72,6 +73,9 @@ const BrowserActSchema = Type.Object({
7273
// Common fields
7374
targetId: Type.Optional(Type.String({ description: TAB_REFERENCE_DESCRIPTION })),
7475
ref: Type.Optional(Type.String()),
76+
// batch - permissive children keep the provider schema flat; runtime validates each action.
77+
actions: Type.Optional(Type.Array(Type.Object({}, { additionalProperties: true }))),
78+
stopOnError: Type.Optional(Type.Boolean()),
7579
// click
7680
doubleClick: Type.Optional(Type.Boolean()),
7781
button: Type.Optional(Type.String()),
@@ -149,6 +153,8 @@ export const BrowserToolSchema = Type.Object({
149153
promptText: Type.Optional(Type.String()),
150154
// Legacy flattened act params (preferred: request={...})
151155
kind: Type.Optional(stringEnum(BROWSER_ACT_KINDS)),
156+
actions: Type.Optional(Type.Array(Type.Object({}, { additionalProperties: true }))),
157+
stopOnError: Type.Optional(Type.Boolean()),
152158
doubleClick: Type.Optional(Type.Boolean()),
153159
button: Type.Optional(Type.String()),
154160
modifiers: Type.Optional(Type.Array(Type.String())),

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

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ function nodeInvokeCall(callIndex: number): {
474474
request: {
475475
nodeId?: string;
476476
command?: string;
477+
timeoutMs?: number;
477478
params?: {
478479
method?: string;
479480
path?: string;
@@ -491,6 +492,7 @@ function nodeInvokeCall(callIndex: number): {
491492
const request = mockCallArg<{
492493
nodeId?: string;
493494
command?: string;
495+
timeoutMs?: number;
494496
params?: {
495497
method?: string;
496498
path?: string;
@@ -1909,14 +1911,20 @@ describe("browser tool act compatibility", () => {
19091911
await tool.execute?.("call-1", {
19101912
action: "act",
19111913
target: "node",
1912-
request: { kind: "wait", timeMs: 20_000 },
1914+
request: { kind: "wait", timeMs: 20_000, text: "ready" },
19131915
});
19141916

19151917
const { options, request } = lastNodeInvokeCall();
1916-
expect(options.timeoutMs).toBe(55_000);
1918+
expect(options.timeoutMs).toBe(75_000);
1919+
expect(request.timeoutMs).toBe(75_000);
19171920
expect(request.params?.path).toBe("/act");
1918-
expect(request.params?.body).toEqual({ kind: "wait", timeMs: 20_000, timeoutMs: 45_000 });
1919-
expect(request.params?.timeoutMs).toBe(45_000 + 5_000);
1921+
expect(request.params?.body).toEqual({
1922+
kind: "wait",
1923+
timeMs: 20_000,
1924+
text: "ready",
1925+
timeoutMs: 45_000,
1926+
});
1927+
expect(request.params?.timeoutMs).toBe(70_000);
19201928
});
19211929

19221930
it("honors string act request timeouts when sizing node proxy calls", async () => {
@@ -1925,18 +1933,47 @@ describe("browser tool act compatibility", () => {
19251933
await tool.execute?.("call-1", {
19261934
action: "act",
19271935
target: "node",
1928-
request: { kind: "wait", timeMs: "20000", timeoutMs: "45000" },
1936+
request: { kind: "wait", timeMs: "20000", text: "ready", timeoutMs: "45000" },
19291937
});
19301938

19311939
const { options, request } = lastNodeInvokeCall();
1932-
expect(options.timeoutMs).toBe(55_000);
1940+
expect(options.timeoutMs).toBe(75_000);
19331941
expect(request.params?.path).toBe("/act");
19341942
expect(request.params?.body).toEqual({
19351943
kind: "wait",
19361944
timeMs: "20000",
1945+
text: "ready",
19371946
timeoutMs: "45000",
19381947
});
1939-
expect(request.params?.timeoutMs).toBe(50_000);
1948+
expect(request.params?.timeoutMs).toBe(70_000);
1949+
});
1950+
1951+
it("sizes node proxy calls for recursively nested batch execution", async () => {
1952+
mockSingleBrowserProxyNode();
1953+
const tool = createBrowserTool();
1954+
1955+
await tool.execute?.("call-1", {
1956+
action: "act",
1957+
target: "node",
1958+
request: {
1959+
kind: "batch",
1960+
actions: [
1961+
{ kind: "wait", timeMs: 30_000 },
1962+
{
1963+
kind: "batch",
1964+
actions: [
1965+
{ kind: "wait", timeMs: 30_000 },
1966+
{ kind: "wait", timeMs: 30_000 },
1967+
],
1968+
},
1969+
],
1970+
},
1971+
});
1972+
1973+
const { options, request } = lastNodeInvokeCall();
1974+
expect(request.params?.timeoutMs).toBe(95_000);
1975+
expect(request.timeoutMs).toBe(100_000);
1976+
expect(options.timeoutMs).toBe(100_000);
19401977
});
19411978

19421979
it("rejects fractional act request timeouts before node proxy calls", async () => {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ const SCREENSHOT_SHARE_UNAVAILABLE =
198198

199199
const LEGACY_BROWSER_ACT_REQUEST_KEYS = [
200200
"kind",
201+
"actions",
202+
"stopOnError",
201203
"targetId",
202204
"ref",
203205
"doubleClick",
@@ -381,6 +383,9 @@ async function callBrowserProxy(params: {
381383
{
382384
nodeId: params.nodeId,
383385
command: "browser.proxy",
386+
// node.invoke owns a separate watchdog from browser.proxy. Keep both
387+
// bounded, with enough outer slack for the proxy result to cross back.
388+
timeoutMs: gatewayTimeoutMs,
384389
params: {
385390
method: params.method,
386391
path: params.path,
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
import { MAX_TIMER_TIMEOUT_MS } from "openclaw/plugin-sdk/number-runtime";
2+
import { describe, expect, it } from "vitest";
3+
import {
4+
resolveBrowserActExecutionBudgetMs,
5+
resolveBrowserActRequestTimeoutMs,
6+
} from "./act-policy.js";
7+
import type { BrowserActRequest } from "./client-actions.types.js";
8+
9+
describe("browser action execution budgets", () => {
10+
it("sums the delay and every sequential wait condition", () => {
11+
expect(
12+
resolveBrowserActExecutionBudgetMs({
13+
kind: "wait",
14+
timeMs: 10_000,
15+
text: "ready",
16+
textGone: "loading",
17+
selector: "#result",
18+
url: "**/done",
19+
loadState: "networkidle",
20+
fn: "() => true",
21+
timeoutMs: 20_000,
22+
}),
23+
).toBe(130_000);
24+
});
25+
26+
it("normalizes each condition timeout before multiplication", () => {
27+
const wait = {
28+
kind: "wait",
29+
text: "ready",
30+
textGone: "loading",
31+
selector: "#result",
32+
url: "**/done",
33+
loadState: "load",
34+
fn: "() => true",
35+
} as const;
36+
37+
expect(resolveBrowserActExecutionBudgetMs({ ...wait, timeoutMs: 1 })).toBe(3_000);
38+
expect(resolveBrowserActExecutionBudgetMs({ ...wait, timeoutMs: Number.MAX_VALUE })).toBe(
39+
720_000,
40+
);
41+
});
42+
43+
it("matches runtime normalization for whitespace-only wait conditions", () => {
44+
expect(
45+
resolveBrowserActExecutionBudgetMs({
46+
kind: "wait",
47+
timeMs: 0,
48+
text: " ",
49+
textGone: " ",
50+
selector: "",
51+
url: " ",
52+
fn: " ",
53+
}),
54+
).toBe(40_000);
55+
expect(resolveBrowserActRequestTimeoutMs({ kind: "wait", timeMs: 0 })).toBe(5_000);
56+
});
57+
58+
it("recursively sums nested batch children in execution order", () => {
59+
expect(
60+
resolveBrowserActExecutionBudgetMs({
61+
kind: "batch",
62+
actions: [
63+
{ kind: "wait", timeMs: 30_000 },
64+
{
65+
kind: "batch",
66+
actions: [
67+
{ kind: "wait", timeMs: 30_000 },
68+
{ kind: "wait", timeMs: 30_000 },
69+
],
70+
},
71+
],
72+
}),
73+
).toBe(90_000);
74+
});
75+
76+
it("keeps leaf defaults and adds outer slack once", () => {
77+
expect(resolveBrowserActExecutionBudgetMs({ kind: "click", ref: "1" })).toBe(60_000);
78+
expect(resolveBrowserActRequestTimeoutMs({ kind: "click", ref: "1", timeoutMs: 45_000 })).toBe(
79+
50_250,
80+
);
81+
});
82+
83+
it("budgets sequential leaf phases and bounded delays", () => {
84+
expect(
85+
resolveBrowserActExecutionBudgetMs({
86+
kind: "fill",
87+
fields: [
88+
{ ref: "first", type: "text" },
89+
{ ref: "second", type: "text" },
90+
],
91+
timeoutMs: 20_000,
92+
}),
93+
).toBe(40_500);
94+
expect(
95+
resolveBrowserActExecutionBudgetMs({
96+
kind: "type",
97+
ref: "field",
98+
text: "value",
99+
slowly: true,
100+
submit: true,
101+
timeoutMs: 20_000,
102+
}),
103+
).toBe(60_250);
104+
expect(
105+
resolveBrowserActExecutionBudgetMs({
106+
kind: "click",
107+
ref: "button",
108+
delayMs: 5_000,
109+
timeoutMs: 20_000,
110+
}),
111+
).toBe(45_250);
112+
expect(
113+
resolveBrowserActExecutionBudgetMs({
114+
kind: "clickCoords",
115+
x: 10,
116+
y: 20,
117+
doubleClick: true,
118+
delayMs: 5_000,
119+
timeoutMs: 20_000,
120+
}),
121+
).toBe(35_250);
122+
});
123+
124+
it("preserves the prior whole-request floor for batches", () => {
125+
expect(
126+
resolveBrowserActExecutionBudgetMs({
127+
kind: "batch",
128+
actions: [
129+
{
130+
kind: "fill",
131+
fields: [
132+
{ ref: "first", type: "text" },
133+
{ ref: "second", type: "text" },
134+
],
135+
timeoutMs: 20_000,
136+
},
137+
],
138+
}),
139+
).toBe(60_000);
140+
});
141+
142+
it("budgets the wider scrollIntoView locator timeout", () => {
143+
expect(
144+
resolveBrowserActExecutionBudgetMs({
145+
kind: "batch",
146+
actions: Array.from({ length: 4 }, () => ({
147+
kind: "scrollIntoView" as const,
148+
ref: "result",
149+
})),
150+
}),
151+
).toBe(81_000);
152+
expect(
153+
resolveBrowserActExecutionBudgetMs({
154+
kind: "scrollIntoView",
155+
ref: "result",
156+
timeoutMs: 120_000,
157+
}),
158+
).toBe(120_250);
159+
});
160+
161+
it("leaves malformed model batch validation to the browser route", () => {
162+
expect(
163+
resolveBrowserActExecutionBudgetMs({
164+
kind: "batch",
165+
requests: [],
166+
actions: [{ kind: "wait", selector: 1 }, { kind: "unknown" }, null],
167+
} as unknown as BrowserActRequest),
168+
).toBe(60_000);
169+
});
170+
171+
it("saturates aggregate and slack arithmetic at the timer limit", () => {
172+
const actions: BrowserActRequest[] = Array.from({ length: 3_000 }, () => ({
173+
kind: "wait",
174+
text: "ready",
175+
textGone: "loading",
176+
selector: "#result",
177+
url: "**/done",
178+
loadState: "load",
179+
fn: "() => true",
180+
timeoutMs: 120_000,
181+
}));
182+
const batch = { kind: "batch", actions } as const;
183+
184+
expect(resolveBrowserActExecutionBudgetMs(batch)).toBe(MAX_TIMER_TIMEOUT_MS);
185+
expect(resolveBrowserActRequestTimeoutMs(batch)).toBe(MAX_TIMER_TIMEOUT_MS);
186+
});
187+
});

0 commit comments

Comments
 (0)