Skip to content

Commit 079bb34

Browse files
authored
feat(cua-computer): recording family with host-owned resource handles (#124035)
* feat(cua-computer): add recording resource handles * test(agents): split computer tool coverage
1 parent 6d9ea63 commit 079bb34

30 files changed

Lines changed: 1846 additions & 591 deletions

‎docs/nodes/computer-use.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Providers with the v2 window/element family can additionally expose `list_apps`,
3838

3939
The CUA provider also exposes the v2 browser family: `get_browser_state`, `browser_prepare`, `browser_navigate`, `browser_click`, `browser_type`, `browser_dialog`, `browser_set_input_files`, `browser_download`, and `browser_pointer`. Bind a discovered native browser window with `get_browser_state`, then use the returned opaque `browserRef`, `pageRef`, observation, and element references. These references belong to one Computer Use execution and driver generation; navigation invalidates page-element observations, and a driver restart invalidates the complete browser reference set.
4040

41+
CUA additionally exposes `get_recording_state`, `start_recording`, `stop_recording`, and `replay_trajectory`. Recording and browser file operations use opaque `openclaw:computer-resource` handles. The node creates and validates the underlying files and directories; agent actions never accept native paths, output roots, or helper executable paths. Handles belong to one Computer Use execution and cannot be reused by another execution.
42+
4143
Modifier keys ride the `text` field on click and scroll actions (`shift`, `ctrl`, `alt`, `cmd`). After an input action the tool returns a fresh screenshot so the model can observe the result. If more than one computer-capable node is connected, pass `node` explicitly.
4244

4345
Screenshots are kept **model-only**: they are never auto-delivered to the chat channel. Treat all on-screen content as untrusted input; the tool warns the model not to follow on-screen instructions that conflict with the user's request.
@@ -54,7 +56,7 @@ The app waits until the private socket accepts connections before advertising CU
5456

5557
The embedded CUA daemon runs in unrestricted mode because bounded CUA grants require exact launch-time resources and cannot represent OpenClaw's runtime-discovered windows and elements. OpenClaw command arming, pairing approval, and tool policy are the authoritative authorization gate, identical to the shipped Peekaboo fulfiller. The app owns the daemon and its macOS TCC identity, and the daemon accepts local connections only through an owner-only socket directory.
5658

57-
The CUA descriptor advertises window, element, and browser targets; background and foreground delivery; and image, accessibility, and browser observations. Peekaboo remains the default in this release and advertises only the action families its native adapter implements.
59+
The CUA descriptor advertises window, element, and browser targets; background and foreground delivery; image, accessibility, and browser observations; and recording. Peekaboo remains the default in this release and does not advertise recording.
5860

5961
#### Browser profiles
6062

@@ -105,7 +107,7 @@ The bundled `cua-computer` plugin provides an experimental fulfiller for Windows
105107

106108
OpenClaw checks the SDK package version, the selected OS/CPU package version, regular-file identity, and the pinned SHA-256 digest of the native library and Node runtime. A clean check prints `no findings`. If it reports a `COMPUTER_DRIVER_*` error, reinstall or update OpenClaw on this node host and run the check again. Do not download a standalone `cua-driver` executable or add one to `PATH`; Windows and Linux use the npm-installed in-process SDK.
107109

108-
3. Start `openclaw node run` from the interactive desktop session. The plugin repeats the artifact verification at startup before it imports native code, creates its configured SDK runtime lazily, then creates separate fixed window- and desktop-scoped trusted sessions for node-host command execution. `escalate_scope` reads the already-desktop session state, so the window identity remains immutable. It closes both sessions and shuts down the runtime when the command host stops or restarts.
110+
3. Start `openclaw node run` from the interactive desktop session. The plugin repeats the artifact verification at startup before it imports native code, creates its configured SDK runtime lazily, then creates separate fixed window- and desktop-scoped trusted sessions for each provider execution. `escalate_scope` reads the already-desktop session state, so the window identity remains immutable. Completion, cancellation, Gateway disconnect, provider switching, local Stop, and command-host shutdown all close that exact execution, finalize or discard its recording resources, close both sessions, and shut down its runtime.
109111

110112
4. Add `computer.act` to the Gateway allowlist. This plugin registers `computer.act` as a dangerous plugin node command, so enabling the plugin alone is not enough; the operator must opt in explicitly:
111113

@@ -181,6 +183,7 @@ On macOS, default-on means a paired gateway can drive pointer and keyboard input
181183

182184
- Every layer (tool policy, gateway command policy, pairing, node-app setting, and platform permissions) must agree. On macOS that includes **Allow Computer Control**, Accessibility, and Screen Recording; the native Peekaboo path also requires Event Posting. Actions execute while those durable controls remain enabled; there is no per-action confirmation.
183185
- The macOS fulfiller posts text one grapheme at a time, so cancellation, disconnect, pause, disable, or endpoint replacement stops it before the next grapheme. The experimental CUA Driver fulfiller passes node cancellation to the SDK for each call.
186+
- CUA recording, replay, browser upload, and browser download paths are node-owned. The model receives only opaque execution-scoped resource handles; traversal, absolute paths, symlink escapes, and helper selection are rejected before driver dispatch.
184187
- Screenshots are model-only and never auto-sent to chat (issue [#44759](https://github.com/openclaw/openclaw/issues/44759)).
185188
- Treat screen content as untrusted; it can carry prompt injection.
186189

‎extensions/cua-computer/src/action-targets.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414

1515
export type CuaComputerActParams = {
1616
action: ComputerActParams["action"];
17+
executionId?: string;
1718
displayFrameId?: string;
1819
x?: number;
1920
y?: number;
@@ -51,8 +52,11 @@ export type CuaComputerActParams = {
5152
dialogAction?: "inspect" | "accept" | "dismiss";
5253
dialogRef?: string;
5354
promptText?: string;
54-
files?: string[];
55-
destinationRoot?: string;
55+
resourceHandle?: string;
56+
resourceHandles?: string[];
57+
recordVideo?: boolean;
58+
delayMs?: number;
59+
stopOnError?: boolean;
5660
pointerAction?: "hover" | "right_click" | "double_click" | "scroll" | "drag";
5761
destinationElementRef?: string;
5862
toX?: number;

‎extensions/cua-computer/src/browser-actions.test.ts‎

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1-
import { describe, expect, it } from "vitest";
1+
import fs from "node:fs/promises";
2+
import os from "node:os";
3+
import path from "node:path";
4+
import { afterEach, describe, expect, it } from "vitest";
25
import { driver, execution } from "./commands.test-helpers.js";
36
import {
47
CUA_DRIVER_CONTRACT_FIXTURES,
58
cuaToolResult,
69
} from "./cua-driver-contract.test-fixtures.js";
710
import type { CuaToolResult } from "./driver-client.js";
811

12+
const tempRoots: string[] = [];
13+
14+
afterEach(async () => {
15+
await Promise.all(
16+
tempRoots.splice(0).map(async (root) => await fs.rm(root, { recursive: true, force: true })),
17+
);
18+
});
19+
920
describe("cua-computer browser actions", () => {
1021
it("maps every browser action to the pinned driver tool contract", async () => {
22+
const resourceRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-cua-browser-"));
23+
tempRoots.push(resourceRoot);
1124
const { session, callTool } = driver();
25+
let downloadedFile = "";
1226
callTool.mockImplementation(async (name, args) => {
1327
switch (name) {
1428
case "list_windows":
@@ -29,6 +43,8 @@ describe("cua-computer browser actions", () => {
2943
case "browser_set_input_files":
3044
return cuaToolResult(CUA_DRIVER_CONTRACT_FIXTURES.browserFiles);
3145
case "browser_download":
46+
downloadedFile = path.join(String(args.destination_root), "download.txt");
47+
await fs.writeFile(downloadedFile, "download");
3248
return cuaToolResult(CUA_DRIVER_CONTRACT_FIXTURES.browserDownload);
3349
case "browser_click":
3450
case "browser_type":
@@ -44,7 +60,7 @@ describe("cua-computer browser actions", () => {
4460
return cuaToolResult({});
4561
}
4662
});
47-
const computer = await execution(session);
63+
const computer = await execution(session, { resourceRoot });
4864
const listed = JSON.parse(await computer.act('{"action":"list_windows"}')) as {
4965
details: { windows: Array<{ windowRef: string }> };
5066
};
@@ -119,24 +135,29 @@ describe("cua-computer browser actions", () => {
119135
),
120136
) as { details: { dialogRef: string } };
121137
expect(dialog.details.dialogRef).toMatch(/^cua:v2:dialog:/);
122-
await computer.act(
138+
const downloadJson = await computer.act(
123139
JSON.stringify({
124-
action: "browser_set_input_files",
140+
action: "browser_download",
125141
browserRef,
126142
pageRef,
127143
observationId,
128-
elementRef: secondElement,
129-
files: ["/tmp/input.txt"],
144+
elementRef: firstElement,
130145
}),
131146
);
147+
expect(downloadJson).not.toContain(resourceRoot);
148+
const download = JSON.parse(downloadJson) as {
149+
details: { fileResourceHandles: string[]; resourceHandle: string };
150+
};
151+
expect(download.details.resourceHandle).toMatch(/^openclaw:computer-resource:v1:/u);
152+
expect(download.details.fileResourceHandles[0]).toMatch(/^openclaw:computer-resource:v1:/u);
132153
await computer.act(
133154
JSON.stringify({
134-
action: "browser_download",
155+
action: "browser_set_input_files",
135156
browserRef,
136157
pageRef,
137158
observationId,
138-
elementRef: firstElement,
139-
destinationRoot: "/tmp/downloads",
159+
elementRef: secondElement,
160+
resourceHandles: download.details.fileResourceHandles,
140161
}),
141162
);
142163
await computer.act(
@@ -214,22 +235,22 @@ describe("cua-computer browser actions", () => {
214235
undefined,
215236
],
216237
[
217-
"browser_set_input_files",
238+
"browser_download",
218239
{
219240
target_id: "native-browser-target-1",
220241
tab_id: "native-page-1",
221-
ref: "p7:1",
222-
files: ["/tmp/input.txt"],
242+
ref: "p7:0",
243+
destination_root: path.dirname(downloadedFile),
223244
},
224245
undefined,
225246
],
226247
[
227-
"browser_download",
248+
"browser_set_input_files",
228249
{
229250
target_id: "native-browser-target-1",
230251
tab_id: "native-page-1",
231-
ref: "p7:0",
232-
destination_root: "/tmp/downloads",
252+
ref: "p7:1",
253+
files: [downloadedFile],
233254
},
234255
undefined,
235256
],

‎extensions/cua-computer/src/browser-actions.ts‎

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { browserElement, browserTarget, requireWindowTarget } from "./action-targets.js";
22
import type { CuaComputerActParams } from "./action-targets.js";
3-
import type { CuaDriverSession } from "./driver-client.js";
3+
import type { CuaDriverSession, CuaToolResult } from "./driver-client.js";
44
import {
55
browserBinding,
66
browserDialogEnvelope,
77
browserObservation,
88
browserToolEnvelope,
99
callWindowTool,
1010
} from "./driver-result.js";
11+
import type { CuaExecutionResources } from "./execution-resources.js";
1112
import {
1213
clearDialogRef,
1314
invalidateBrowserObservation,
@@ -21,6 +22,7 @@ import {
2122
export async function handleBrowserAct(
2223
driver: CuaDriverSession,
2324
state: CuaFrameState,
25+
resources: CuaExecutionResources,
2426
input: CuaComputerActParams,
2527
signal?: AbortSignal,
2628
): Promise<string | undefined> {
@@ -174,36 +176,66 @@ export async function handleBrowserAct(
174176
case "browser_set_input_files": {
175177
const target = browserTarget(driver, state, input);
176178
const ref = browserElement(state, input, target)!;
177-
const result = await callWindowTool(
178-
driver,
179-
state,
180-
"browser_set_input_files",
181-
{
182-
target_id: target.targetId,
183-
tab_id: target.tabId,
184-
ref,
185-
files: input.files,
186-
},
187-
signal,
188-
);
179+
const files = await resources.resolveFiles(input.resourceHandles ?? []);
180+
let result: CuaToolResult;
181+
try {
182+
result = await callWindowTool(
183+
driver,
184+
state,
185+
"browser_set_input_files",
186+
{
187+
target_id: target.targetId,
188+
tab_id: target.tabId,
189+
ref,
190+
files,
191+
},
192+
signal,
193+
);
194+
} catch (error) {
195+
signal?.throwIfAborted();
196+
throw new Error(
197+
"COMPUTER_DRIVER_ERROR: browser_set_input_files failed; inspect node logs and resource state before retrying",
198+
{ cause: error },
199+
);
200+
}
189201
return JSON.stringify(browserToolEnvelope(result, "browser_set_input_files"));
190202
}
191203
case "browser_download": {
192204
const target = browserTarget(driver, state, input);
193205
const ref = browserElement(state, input, target)!;
194-
const result = await callWindowTool(
195-
driver,
196-
state,
197-
"browser_download",
198-
{
199-
target_id: target.targetId,
200-
tab_id: target.tabId,
201-
ref,
202-
destination_root: input.destinationRoot,
206+
const resource = await resources.createDirectory("browser-download");
207+
let result: CuaToolResult;
208+
try {
209+
result = await callWindowTool(
210+
driver,
211+
state,
212+
"browser_download",
213+
{
214+
target_id: target.targetId,
215+
tab_id: target.tabId,
216+
ref,
217+
destination_root: resource.path,
218+
},
219+
signal,
220+
);
221+
} catch (error) {
222+
await resources.discard(resource.handle).catch(() => {});
223+
signal?.throwIfAborted();
224+
throw new Error(
225+
"COMPUTER_DRIVER_ERROR: browser_download failed; inspect node logs and resource state before retrying",
226+
{ cause: error },
227+
);
228+
}
229+
const envelope = browserToolEnvelope(result, "browser_download");
230+
const fileResourceHandles = await resources.captureFiles(resource.handle);
231+
return JSON.stringify({
232+
...envelope,
233+
details: {
234+
...envelope.details,
235+
resourceHandle: resource.handle,
236+
fileResourceHandles,
203237
},
204-
signal,
205-
);
206-
return JSON.stringify(browserToolEnvelope(result, "browser_download"));
238+
});
207239
}
208240
case "browser_pointer": {
209241
const target = browserTarget(driver, state, input);

‎extensions/cua-computer/src/commands.test-helpers.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { randomUUID } from "node:crypto";
12
import { vi } from "vitest";
23
import { createCuaComputerProvider } from "./commands.js";
34
import type { CuaDriverSession, CuaToolResult } from "./driver-client.js";
@@ -133,12 +134,16 @@ export function driver(
133134
};
134135
}
135136

136-
export async function execution(session: CuaDriverSession) {
137+
export async function execution(
138+
session: CuaDriverSession,
139+
options: { resourceRoot?: string } = {},
140+
) {
137141
return await createCuaComputerProvider({
138142
platform: "linux",
139143
driver: session,
140144
imageProcessor: {
141145
encode: vi.fn(async () => ({ data: Buffer.from("jpeg"), width: 100, height: 50 })),
142146
},
143-
}).openExecution({});
147+
resourceRoot: options.resourceRoot,
148+
}).openExecution({ executionId: randomUUID() });
144149
}

‎extensions/cua-computer/src/commands.test.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,16 @@ describe("cua-computer provider", () => {
6666
"browser_download",
6767
"browser_pointer",
6868
"escalate_scope",
69+
"get_recording_state",
70+
"start_recording",
71+
"stop_recording",
72+
"replay_trajectory",
6973
"invoke_menu",
7074
],
7175
targets: ["screen", "window", "element", "browser"],
7276
deliveryModes: ["background", "foreground"],
7377
observations: ["image", "accessibility", "browser"],
74-
features: { recording: false, agentCursor: false, multiDisplay: false },
78+
features: { recording: true, agentCursor: false, multiDisplay: false },
7579
});
7680
});
7781

@@ -82,6 +86,14 @@ describe("cua-computer provider", () => {
8286
expect(actions).not.toContain("left_mouse_down");
8387
expect(actions).not.toContain("left_mouse_up");
8488
expect(actions).toContain("get_window_state");
89+
expect(actions).toEqual(
90+
expect.arrayContaining([
91+
"get_recording_state",
92+
"start_recording",
93+
"stop_recording",
94+
"replay_trajectory",
95+
]),
96+
);
8597
});
8698

8799
it("advertises the macOS mapping only with a valid atomic app-provided endpoint", () => {
@@ -97,7 +109,7 @@ describe("cua-computer provider", () => {
97109
expect(provider.capabilities().actions).toContain("get_window_state");
98110
expect(provider.capabilities().actions).not.toContain("left_mouse_down");
99111
expect(provider.capabilities().features).toEqual({
100-
recording: false,
112+
recording: true,
101113
agentCursor: false,
102114
multiDisplay: false,
103115
});
@@ -140,7 +152,7 @@ describe("cua-computer provider", () => {
140152
imageProcessor: {
141153
encode: vi.fn(async () => ({ data: Buffer.from("png"), width: 100, height: 50 })),
142154
},
143-
}).openExecution({});
155+
}).openExecution({ executionId: "123e4567-e89b-42d3-a456-426614174000" });
144156
const screen = JSON.parse(await computer.snapshot('{"format":"png","maxWidth":100}')) as {
145157
displayFrameId: string;
146158
width: number;
@@ -307,7 +319,9 @@ describe("cua-computer provider", () => {
307319
});
308320
expect(createDriver).not.toHaveBeenCalled();
309321

310-
const computer = await provider.openExecution({});
322+
const computer = await provider.openExecution({
323+
executionId: "123e4567-e89b-42d3-a456-426614174000",
324+
});
311325
await computer.snapshot('{"format":"png","maxWidth":100}');
312326
expect(createDriver).toHaveBeenCalledOnce();
313327

0 commit comments

Comments
 (0)