@e2e-dev/desktop 0.0.1-nightly-20261123030205 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -3
- package/dist/.build.json +1 -1
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +5 -1
- package/dist/engine.d.ts +5 -5
- package/dist/engine.js +3 -2
- package/dist/keys.d.ts +2 -1
- package/dist/keys.js +17 -1
- package/dist/launch-arguments.d.ts +2 -0
- package/dist/launch-arguments.js +5 -0
- package/dist/nodes.d.ts +20 -2
- package/dist/nodes.js +65 -23
- package/dist/options.d.ts +3 -3
- package/dist/options.js +4 -3
- package/dist/surface.d.ts +35 -12
- package/dist/surface.js +215 -63
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -9,7 +9,9 @@ target unchanged.
|
|
|
9
9
|
|
|
10
10
|
> [!NOTE]
|
|
11
11
|
> This is a prototype, and it runs on macOS only. A config with a desktop
|
|
12
|
-
> target still loads elsewhere,
|
|
12
|
+
> target still loads elsewhere, and its other targets run there when you pick
|
|
13
|
+
> them with `--target`. A run that includes the desktop target stops before
|
|
14
|
+
> any test starts.
|
|
13
15
|
|
|
14
16
|
## Install
|
|
15
17
|
|
|
@@ -34,7 +36,7 @@ export default {
|
|
|
34
36
|
} satisfies E2EConfig;
|
|
35
37
|
```
|
|
36
38
|
|
|
37
|
-
`app.bundleId` names an installed app instead (`'com.apple.
|
|
39
|
+
`app.bundleId` names an installed app instead (`'com.apple.calculator'`, or an
|
|
38
40
|
app name), and `app.launchArguments` are passed to every launch.
|
|
39
41
|
|
|
40
42
|
```ts title="tests/sign-in.e2e.ts"
|
|
@@ -58,7 +60,9 @@ test('signs in', async ({ app, screen }) => {
|
|
|
58
60
|
driven, or quit. Every test ends with the app quit.
|
|
59
61
|
- The app gets only `HOME`, `USER`, `LOGNAME`, `SHELL`, `PATH`, `TMPDIR`,
|
|
60
62
|
`LANG`, `LC_*`, and `__CF_USER_TEXT_ENCODING` of the run's environment, and
|
|
61
|
-
`E2E_DESKTOP_LAUNCH`, the token the engine finds its copy by.
|
|
63
|
+
`E2E_DESKTOP_LAUNCH`, the token the engine finds its copy by. An app
|
|
64
|
+
launched by `bundleId` also gets `AGENT_DESKTOP_HOME`, the path of the
|
|
65
|
+
worker's private agent-desktop directory.
|
|
62
66
|
- An observation is one agent-desktop snapshot of the app's window. Roles map
|
|
63
67
|
onto the ARIA names other engines use (`textbox`, `checkbox`, `combobox`,
|
|
64
68
|
`button`); the developer id (`AXIdentifier`) is the node's test id, so
|
package/dist/.build.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":"0.0.1
|
|
1
|
+
{"version":"0.0.1"}
|
package/dist/cli.d.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* | agent-desktop result | Code |
|
|
6
6
|
* | ------------------------------------------------------------- | ------------------------- |
|
|
7
7
|
* | `STALE_REF`, `SNAPSHOT_NOT_FOUND`, `ELEMENT_NOT_FOUND`, sent nothing | NODE_STALE (retryable) |
|
|
8
|
+
* | `APP_UNRESPONSIVE` that `details.retryable` says is safe to retry | NODE_STALE (retryable) |
|
|
8
9
|
* | `ACTION_NOT_SUPPORTED`, `PLATFORM_NOT_SUPPORTED`, `POLICY_DENIED` | UNSUPPORTED_CAPABILITY |
|
|
9
10
|
* | `APP_NOT_FOUND`, `WINDOW_NOT_FOUND` | INVALID_STATE |
|
|
10
11
|
* | `AMBIGUOUS_TARGET`, `ACTION_FAILED`, `TIMEOUT`, sent nothing | NOT_ACTIONABLE |
|
|
@@ -49,6 +50,8 @@ export interface CliCall {
|
|
|
49
50
|
readonly timeoutMs: number;
|
|
50
51
|
/** Names the command in an error message. */
|
|
51
52
|
readonly label: string;
|
|
53
|
+
/** The command's `TMPDIR`, in place of the run's: where it writes files of its own that a killed process leaves behind. */
|
|
54
|
+
readonly tmpdir?: string;
|
|
52
55
|
}
|
|
53
56
|
/** A failure agent-desktop reported, before translation, for a caller that tries another way when nothing was sent. */
|
|
54
57
|
export declare class AgentDesktopFailure extends Error {
|
package/dist/cli.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* | agent-desktop result | Code |
|
|
6
6
|
* | ------------------------------------------------------------- | ------------------------- |
|
|
7
7
|
* | `STALE_REF`, `SNAPSHOT_NOT_FOUND`, `ELEMENT_NOT_FOUND`, sent nothing | NODE_STALE (retryable) |
|
|
8
|
+
* | `APP_UNRESPONSIVE` that `details.retryable` says is safe to retry | NODE_STALE (retryable) |
|
|
8
9
|
* | `ACTION_NOT_SUPPORTED`, `PLATFORM_NOT_SUPPORTED`, `POLICY_DENIED` | UNSUPPORTED_CAPABILITY |
|
|
9
10
|
* | `APP_NOT_FOUND`, `WINDOW_NOT_FOUND` | INVALID_STATE |
|
|
10
11
|
* | `AMBIGUOUS_TARGET`, `ACTION_FAILED`, `TIMEOUT`, sent nothing | NOT_ACTIONABLE |
|
|
@@ -83,7 +84,7 @@ export class AgentDesktopCli {
|
|
|
83
84
|
spawn(call) {
|
|
84
85
|
return new Promise((resolve, reject) => {
|
|
85
86
|
const child = spawn(this.executable, call.args, {
|
|
86
|
-
env: this.env,
|
|
87
|
+
env: call.tmpdir === undefined ? this.env : { ...this.env, TMPDIR: call.tmpdir },
|
|
87
88
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
88
89
|
windowsHide: true,
|
|
89
90
|
});
|
|
@@ -154,6 +155,9 @@ export function translate(cause, label, input) {
|
|
|
154
155
|
return new EngineError('ACTION_MAY_HAVE_COMMITTED', `${text} (the input may have reached the app)`, { retryable: false, cause });
|
|
155
156
|
if (STALE_CODES.has(cause.code))
|
|
156
157
|
return new EngineError('NODE_STALE', text, { retryable: true, cause });
|
|
158
|
+
// A read agent-desktop says is safe to try again (the window list changed while it read): read again, as a stale one.
|
|
159
|
+
if (cause.code === 'APP_UNRESPONSIVE' && cause.details.retryable === true)
|
|
160
|
+
return new EngineError('NODE_STALE', text, { retryable: true, cause });
|
|
157
161
|
if (UNSUPPORTED_CODES.has(cause.code))
|
|
158
162
|
return new EngineError('UNSUPPORTED_CAPABILITY', text, { retryable: false, cause });
|
|
159
163
|
if (GONE_CODES.has(cause.code))
|
package/dist/engine.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* package imports the contract from `@e2e-dev/core/engine` and contributes the
|
|
6
6
|
* `desktop` fixture the way the browser engine contributes `browser`.
|
|
7
7
|
*/
|
|
8
|
-
import { type EngineHandle } from '@e2e-dev/core/engine';
|
|
8
|
+
import { type EngineHandle, type EngineIsolation } from '@e2e-dev/core/engine';
|
|
9
9
|
import type { DesktopFixtures } from './fixtures.ts';
|
|
10
10
|
import { type DesktopOptions } from './options.ts';
|
|
11
11
|
/**
|
|
@@ -13,13 +13,13 @@ import { type DesktopOptions } from './options.ts';
|
|
|
13
13
|
* names this package: a project sees `@e2e-dev/core` only through its
|
|
14
14
|
* dependencies, and TypeScript cannot write that path in a declaration.
|
|
15
15
|
*/
|
|
16
|
-
export interface DesktopEngine extends EngineHandle<DesktopFixtures> {
|
|
16
|
+
export interface DesktopEngine<Platform extends string = string, Isolation extends EngineIsolation = EngineIsolation> extends EngineHandle<DesktopFixtures, Platform, Isolation> {
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
19
|
* Creates one desktop engine: it drives the target's macOS app on this
|
|
20
20
|
* machine, one worker at a time. A test launches the app with `app.open()`;
|
|
21
21
|
* every test ends with the app quit. A config that names it loads on any OS,
|
|
22
|
-
*
|
|
23
|
-
* on any OS but macOS.
|
|
22
|
+
* and its other targets run elsewhere when `--target` picks them; a run that
|
|
23
|
+
* includes this target stops before any test starts on any OS but macOS.
|
|
24
24
|
*/
|
|
25
|
-
export declare function desktop(options?: DesktopOptions): DesktopEngine
|
|
25
|
+
export declare function desktop(options?: DesktopOptions): DesktopEngine<'macos', 'attempt'>;
|
package/dist/engine.js
CHANGED
|
@@ -23,8 +23,8 @@ const HEADED_POINTER_ACTIONS = ['tap', 'doubleTap', 'secondaryTap', 'hover'];
|
|
|
23
23
|
* Creates one desktop engine: it drives the target's macOS app on this
|
|
24
24
|
* machine, one worker at a time. A test launches the app with `app.open()`;
|
|
25
25
|
* every test ends with the app quit. A config that names it loads on any OS,
|
|
26
|
-
*
|
|
27
|
-
* on any OS but macOS.
|
|
26
|
+
* and its other targets run elsewhere when `--target` picks them; a run that
|
|
27
|
+
* includes this target stops before any test starts on any OS but macOS.
|
|
28
28
|
*/
|
|
29
29
|
export function desktop(options = {}) {
|
|
30
30
|
const resolved = resolveOptions(options);
|
|
@@ -37,6 +37,7 @@ export function desktop(options = {}) {
|
|
|
37
37
|
spiVersion: 1,
|
|
38
38
|
platform,
|
|
39
39
|
workers: 1,
|
|
40
|
+
launchArgumentValues: true,
|
|
40
41
|
prepare: (info) => {
|
|
41
42
|
requireMacOS();
|
|
42
43
|
return surface.prepare(info);
|
package/dist/keys.d.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* The combo agent-desktop's `press` takes for one contract key: `Enter` is
|
|
4
4
|
* `return`, `ControlOrMeta+a` is `cmd+a`.
|
|
5
5
|
* A key agent-desktop has no name for is `UNSUPPORTED_CAPABILITY`, and so is
|
|
6
|
-
* `+`, which its combo syntax uses as the separator
|
|
6
|
+
* `+`, which its combo syntax uses as the separator, and a shortcut it
|
|
7
|
+
* refuses (`REFUSED_SHORTCUTS`).
|
|
7
8
|
*/
|
|
8
9
|
export declare function toCombo(key: string): string;
|
package/dist/keys.js
CHANGED
|
@@ -54,11 +54,23 @@ function modifierName(modifier) {
|
|
|
54
54
|
return 'cmd';
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* The shortcuts agent-desktop refuses to press unless forced, which the
|
|
59
|
+
* engine never is: on macOS they quit, log out, force-quit, or lock the
|
|
60
|
+
* session. It refuses any combo with one's key and at least its modifiers,
|
|
61
|
+
* so `cmd+q` stands for `cmd+shift+q` and `ctrl+cmd+q` too.
|
|
62
|
+
*/
|
|
63
|
+
const REFUSED_SHORTCUTS = [
|
|
64
|
+
{ key: 'q', modifiers: ['cmd'] },
|
|
65
|
+
{ key: 'escape', modifiers: ['cmd', 'alt'] },
|
|
66
|
+
{ key: 'backspace', modifiers: ['cmd', 'shift'] },
|
|
67
|
+
];
|
|
57
68
|
/**
|
|
58
69
|
* The combo agent-desktop's `press` takes for one contract key: `Enter` is
|
|
59
70
|
* `return`, `ControlOrMeta+a` is `cmd+a`.
|
|
60
71
|
* A key agent-desktop has no name for is `UNSUPPORTED_CAPABILITY`, and so is
|
|
61
|
-
* `+`, which its combo syntax uses as the separator
|
|
72
|
+
* `+`, which its combo syntax uses as the separator, and a shortcut it
|
|
73
|
+
* refuses (`REFUSED_SHORTCUTS`).
|
|
62
74
|
*/
|
|
63
75
|
export function toCombo(key) {
|
|
64
76
|
const parsed = parseKey(key);
|
|
@@ -82,5 +94,9 @@ export function toCombo(key) {
|
|
|
82
94
|
// A capital letter with other modifiers loses its case in agent-desktop's combo, so Shift says it.
|
|
83
95
|
if (parsed.key.kind === 'char' && /^[A-Z]$/.test(parsed.key.char) && modifiers.length > 0 && !modifiers.includes('shift'))
|
|
84
96
|
modifiers.push('shift');
|
|
97
|
+
const pressed = name.toLowerCase();
|
|
98
|
+
if (REFUSED_SHORTCUTS.some((shortcut) => shortcut.key === pressed && shortcut.modifiers.every((modifier) => modifiers.includes(modifier)))) {
|
|
99
|
+
throw new EngineError('UNSUPPORTED_CAPABILITY', `agent-desktop refuses to press "${key}", a shortcut that quits, logs out, force-quits, or locks the session; end the app with desktop.closeApp()`, { retryable: false });
|
|
100
|
+
}
|
|
85
101
|
return [...modifiers, name].join('+');
|
|
86
102
|
}
|
package/dist/nodes.d.ts
CHANGED
|
@@ -70,6 +70,10 @@ export interface NodeBinding {
|
|
|
70
70
|
* place is the same one only if no twin around it came or went.
|
|
71
71
|
*/
|
|
72
72
|
readonly likeness: string;
|
|
73
|
+
/** The developer id (`AXIdentifier`), which a look-alike found again by its place must still carry. */
|
|
74
|
+
readonly testId?: string;
|
|
75
|
+
/** Read with `-i`, whose ids and look-alike counts a check of the same control must read the same way to compare. */
|
|
76
|
+
readonly interactive: boolean;
|
|
73
77
|
}
|
|
74
78
|
/** A snapshot as the engine keeps it: the tree the harness sees, and a binding for every node in it. */
|
|
75
79
|
export interface ProjectedScreen {
|
|
@@ -81,6 +85,8 @@ export interface ProjectedScreen {
|
|
|
81
85
|
/** Rects of the secure fields, in the viewport's points, for masking. Undefined when a secure field has no bounds to mask. */
|
|
82
86
|
readonly secureRects: readonly Rect[] | undefined;
|
|
83
87
|
readonly truncated: boolean;
|
|
88
|
+
/** The read ran out of time (`complete: false`), which the next read may not; a tree cut only at its depth limit is cut the same way every time. */
|
|
89
|
+
readonly cutShort: boolean;
|
|
84
90
|
readonly windowId?: string;
|
|
85
91
|
readonly title?: string;
|
|
86
92
|
/** The first scrollable node, which the observation root's swipe scrolls. */
|
|
@@ -96,6 +102,18 @@ export interface ProjectedScreen {
|
|
|
96
102
|
* The window is always `root`.
|
|
97
103
|
* Wrappers nothing reads (anonymous groups) are dropped
|
|
98
104
|
* and their children lifted, so the model reads the controls, not the
|
|
99
|
-
* scaffolding, and the window's title-bar buttons are left out.
|
|
105
|
+
* scaffolding, and the window's title-bar buttons are left out. Every node
|
|
106
|
+
* of a `minimized` window is hidden: accessibility still reads its controls,
|
|
107
|
+
* but nothing of it is on screen. `interactive` marks the bindings of a read
|
|
108
|
+
* agent-desktop pruned (`-i`), whose tree, and so whose ids and look-alike
|
|
109
|
+
* counts, differ from a whole read's. `onLookAlike` hears each identity a
|
|
110
|
+
* look-alike sibling made shared, for a caller that must retire it, keyed by
|
|
111
|
+
* the id the parent held before (`priorIdOf` looks it up without handing one
|
|
112
|
+
* out), since a read cut short gives that parent a fresh one.
|
|
100
113
|
*/
|
|
101
|
-
export declare function projectSnapshot(raw: RawSnapshot, idFor: (identity: string | undefined) => string
|
|
114
|
+
export declare function projectSnapshot(raw: RawSnapshot, idFor: (identity: string | undefined) => string, { minimized, interactive, onLookAlike, priorIdOf, }?: {
|
|
115
|
+
readonly minimized?: boolean;
|
|
116
|
+
readonly interactive?: boolean;
|
|
117
|
+
readonly onLookAlike?: (identity: string) => void;
|
|
118
|
+
readonly priorIdOf?: (identity: string) => string | undefined;
|
|
119
|
+
}): ProjectedScreen;
|
package/dist/nodes.js
CHANGED
|
@@ -83,8 +83,6 @@ const CHECKABLE_ROLES = new Set(['checkbox', 'switch', 'radiobutton']);
|
|
|
83
83
|
const INPUT_ROLES = new Set(['textfield', 'combobox', 'slider', 'incrementor', 'datefield', 'timefield', 'colorwell']);
|
|
84
84
|
/** Roles agent-desktop reads text into `value`, which the contract calls `text`. */
|
|
85
85
|
const TEXT_ROLES = new Set(['statictext', 'heading']);
|
|
86
|
-
/** Roles a swipe on the observation root scrolls, in preference order. */
|
|
87
|
-
const SCROLLABLE_ROLES = new Set(['scrollarea', 'list', 'listbox', 'table', 'outline', 'grid', 'webarea']);
|
|
88
86
|
const STATE_NAMES = ['focused', 'disabled', 'secure', 'expanded', 'selected', 'hidden', 'pressed'];
|
|
89
87
|
/**
|
|
90
88
|
* A label: a whitespace-only one is no label. Nothing is cut here: `locate`
|
|
@@ -173,16 +171,25 @@ function identityOf(node, named = false) {
|
|
|
173
171
|
* The window is always `root`.
|
|
174
172
|
* Wrappers nothing reads (anonymous groups) are dropped
|
|
175
173
|
* and their children lifted, so the model reads the controls, not the
|
|
176
|
-
* scaffolding, and the window's title-bar buttons are left out.
|
|
174
|
+
* scaffolding, and the window's title-bar buttons are left out. Every node
|
|
175
|
+
* of a `minimized` window is hidden: accessibility still reads its controls,
|
|
176
|
+
* but nothing of it is on screen. `interactive` marks the bindings of a read
|
|
177
|
+
* agent-desktop pruned (`-i`), whose tree, and so whose ids and look-alike
|
|
178
|
+
* counts, differ from a whole read's. `onLookAlike` hears each identity a
|
|
179
|
+
* look-alike sibling made shared, for a caller that must retire it, keyed by
|
|
180
|
+
* the id the parent held before (`priorIdOf` looks it up without handing one
|
|
181
|
+
* out), since a read cut short gives that parent a fresh one.
|
|
177
182
|
*/
|
|
178
|
-
export function projectSnapshot(raw, idFor) {
|
|
183
|
+
export function projectSnapshot(raw, idFor, { minimized = false, interactive = false, onLookAlike, priorIdOf, } = {}) {
|
|
179
184
|
const window = raw.tree.bounds ?? { x: 0, y: 0, width: 0, height: 0 };
|
|
180
185
|
const titleBar = titleBarButtons(raw.tree);
|
|
181
186
|
const bindings = new Map();
|
|
182
187
|
const secureRects = [];
|
|
183
188
|
let unmaskable = false;
|
|
184
189
|
let scrollTarget;
|
|
185
|
-
|
|
190
|
+
// `hiddenAbove`: an ancestor is hidden, a wrapper left out included, so nothing under it is on screen; the resolver reads each node's own state only.
|
|
191
|
+
// `prior`: the id the node held before, which a cut gave it a fresh one in place of; look-alikes under it are reported against that id, which their identities are kept under.
|
|
192
|
+
const project = (node, id, stable, likeness, hiddenAbove, prior) => {
|
|
186
193
|
const rawStates = new Set(node.states ?? []);
|
|
187
194
|
const role = ROLE_MAP[node.role] ?? (node.role === 'unknown' ? undefined : node.role);
|
|
188
195
|
const secure = rawStates.has('secure');
|
|
@@ -190,7 +197,10 @@ export function projectSnapshot(raw, idFor) {
|
|
|
190
197
|
for (const state of STATE_NAMES)
|
|
191
198
|
if (rawStates.has(state))
|
|
192
199
|
states[state] = true;
|
|
193
|
-
if (rawStates.has('offscreen'))
|
|
200
|
+
if (rawStates.has('offscreen') || minimized || hiddenAbove)
|
|
201
|
+
states.hidden = true;
|
|
202
|
+
// A control collapsed to no width or height draws nothing, and agent-desktop will not act on it. Only a leaf: a container with no box can still hold drawn children, listed or cut off at the depth limit.
|
|
203
|
+
if (id !== ROOT_ID && node.bounds !== undefined && (node.bounds.width <= 0 || node.bounds.height <= 0) && (node.children ?? []).length === 0 && (node.children_count ?? 0) === 0)
|
|
194
204
|
states.hidden = true;
|
|
195
205
|
// A mixed checkbox is neither checked nor unchecked: no state, so check() and uncheck() always reach the control.
|
|
196
206
|
if (CHECKABLE_ROLES.has(node.role) && !rawStates.has('indeterminate'))
|
|
@@ -201,6 +211,12 @@ export function projectSnapshot(raw, idFor) {
|
|
|
201
211
|
if (node.available_actions?.includes('Expand') === true || node.available_actions?.includes('Collapse') === true)
|
|
202
212
|
states.expanded = rawStates.has('expanded');
|
|
203
213
|
}
|
|
214
|
+
// agent-desktop marks a text field whose value cannot be set `readonly`, and offers SetValue on one whose value can. A combo box takes text only when its value can be set; a pop-up menu takes none, which is not read-only, so it gets no state, as on a device.
|
|
215
|
+
const settable = node.available_actions?.includes('SetValue') === true;
|
|
216
|
+
if (node.role === 'textfield' && (settable || rawStates.has('readonly')))
|
|
217
|
+
states.readonly = !settable;
|
|
218
|
+
else if (node.role === 'combobox' && settable)
|
|
219
|
+
states.readonly = false;
|
|
204
220
|
const isText = TEXT_ROLES.has(node.role);
|
|
205
221
|
const name = label(node.name ?? (isText ? node.value : undefined));
|
|
206
222
|
// A control shows its name (a button's title) as its text, as on a device; an input's name is a label, not what it holds.
|
|
@@ -228,7 +244,8 @@ export function projectSnapshot(raw, idFor) {
|
|
|
228
244
|
attributes.description = description;
|
|
229
245
|
if (node.native_id !== undefined)
|
|
230
246
|
attributes['native-id-kind'] = node.native_id.kind;
|
|
231
|
-
|
|
247
|
+
// A swipe on the window scrolls the first node agent-desktop offers Scroll, which it scrolls only then and only while visible and enabled: a list with no scroll mechanism, or a pane off screen, would refuse it.
|
|
248
|
+
if (scrollTarget === undefined && node.ref_id !== undefined && node.available_actions?.includes('Scroll') === true && states.hidden !== true && states.disabled !== true)
|
|
232
249
|
scrollTarget = id;
|
|
233
250
|
bindings.set(id, {
|
|
234
251
|
id,
|
|
@@ -241,18 +258,30 @@ export function projectSnapshot(raw, idFor) {
|
|
|
241
258
|
...(rect === undefined ? {} : { rect }),
|
|
242
259
|
stable,
|
|
243
260
|
likeness,
|
|
261
|
+
interactive,
|
|
262
|
+
...(node.native_id === undefined || node.native_id.value === '' ? {} : { testId: node.native_id.value }),
|
|
244
263
|
});
|
|
245
264
|
const kept = [];
|
|
246
|
-
const
|
|
247
|
-
|
|
265
|
+
const underHidden = new Set();
|
|
266
|
+
// Whether a wrapper left out had children the read cut off: its unread children are this node's, and one could be a twin of a listed one.
|
|
267
|
+
let wrapperCut = false;
|
|
268
|
+
const collect = (child, throughHidden) => {
|
|
269
|
+
if (transparent(child)) {
|
|
270
|
+
if (childrenCut(child))
|
|
271
|
+
wrapperCut = true;
|
|
272
|
+
const hiddenWrapper = throughHidden || child.states?.includes('hidden') === true || child.states?.includes('offscreen') === true;
|
|
248
273
|
for (const grandchild of child.children ?? [])
|
|
249
|
-
collect(grandchild);
|
|
250
|
-
|
|
274
|
+
collect(grandchild, hiddenWrapper);
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
251
277
|
kept.push(child);
|
|
278
|
+
if (throughHidden)
|
|
279
|
+
underHidden.add(child);
|
|
280
|
+
}
|
|
252
281
|
};
|
|
253
282
|
for (const child of node.children ?? [])
|
|
254
283
|
if (id !== ROOT_ID || !titleBar.has(child))
|
|
255
|
-
collect(child);
|
|
284
|
+
collect(child, false);
|
|
256
285
|
const tally = (named) => {
|
|
257
286
|
const counts = new Map();
|
|
258
287
|
for (const child of kept)
|
|
@@ -265,10 +294,21 @@ export function projectSnapshot(raw, idFor) {
|
|
|
265
294
|
// look-alike that still matches has no identity: if the first of two
|
|
266
295
|
// "Delete" buttons goes, the second must not take over its id, so each
|
|
267
296
|
// look-alike gets a new id every snapshot.
|
|
297
|
+
// A node whose children the read cut off lists only some of them: one that looks unique may have a twin the read did not reach, so none takes an id.
|
|
298
|
+
const cut = childrenCut(node) || wrapperCut;
|
|
268
299
|
const children = kept.map((child) => {
|
|
269
|
-
const
|
|
270
|
-
|
|
271
|
-
|
|
300
|
+
const seen = plain.get(identityOf(child)) === 1 ? identityOf(child) : named.get(identityOf(child, true)) === 1 ? identityOf(child, true) : undefined;
|
|
301
|
+
const identity = cut ? undefined : seen;
|
|
302
|
+
// An identity two siblings share may pass on to neither, even where their names tell them apart now.
|
|
303
|
+
if (prior !== undefined && (plain.get(identityOf(child)) ?? 0) > 1)
|
|
304
|
+
onLookAlike?.(`${prior}/${identityOf(child)}`);
|
|
305
|
+
if (prior !== undefined && (named.get(identityOf(child, true)) ?? 0) > 1)
|
|
306
|
+
onLookAlike?.(`${prior}/${identityOf(child, true)}`);
|
|
307
|
+
// A child's id is built on its parent's: under a look-alike, whose id is new every snapshot, so is the child's, and no identity is kept for it.
|
|
308
|
+
const childStable = stable && identity !== undefined;
|
|
309
|
+
const childId = idFor(childStable ? `${id}/${identity}` : undefined);
|
|
310
|
+
const childPrior = childStable ? childId : prior !== undefined && seen !== undefined ? priorIdOf?.(`${prior}/${seen}`) : undefined;
|
|
311
|
+
return project(child, childId, childStable, `${likeness}/${plain.get(identityOf(child)) ?? 1}`, states.hidden === true || underHidden.has(child), childPrior);
|
|
272
312
|
});
|
|
273
313
|
return {
|
|
274
314
|
ref: { id, revision: '' },
|
|
@@ -285,7 +325,8 @@ export function projectSnapshot(raw, idFor) {
|
|
|
285
325
|
...(children.length === 0 ? {} : { children }),
|
|
286
326
|
};
|
|
287
327
|
};
|
|
288
|
-
const root = project(raw.tree, ROOT_ID, true, '');
|
|
328
|
+
const root = project(raw.tree, ROOT_ID, true, '', false, ROOT_ID);
|
|
329
|
+
const title = label(raw.tree.name) ?? label(raw.window?.title);
|
|
289
330
|
return {
|
|
290
331
|
root,
|
|
291
332
|
bindings,
|
|
@@ -293,8 +334,10 @@ export function projectSnapshot(raw, idFor) {
|
|
|
293
334
|
viewport: { width: window.width, height: window.height },
|
|
294
335
|
secureRects: unmaskable ? undefined : secureRects,
|
|
295
336
|
truncated: raw.complete === false || raw.truncated === true || hasTruncatedSubtree(raw.tree),
|
|
337
|
+
cutShort: raw.complete === false || raw.truncated === true,
|
|
296
338
|
...(raw.window === undefined ? {} : { windowId: raw.window.id }),
|
|
297
|
-
|
|
339
|
+
// The window's accessibility root carries its title; the window list's, which agent-desktop copies here, is the app's name without Screen Recording permission.
|
|
340
|
+
...(title === undefined ? {} : { title }),
|
|
298
341
|
...(scrollTarget === undefined ? {} : { scrollTarget }),
|
|
299
342
|
};
|
|
300
343
|
}
|
|
@@ -303,11 +346,10 @@ function headingLevel(node) {
|
|
|
303
346
|
const match = /\blevel (\d)\b/i.exec(node.description ?? '');
|
|
304
347
|
return match === null ? {} : { level: Number(match[1]) };
|
|
305
348
|
}
|
|
349
|
+
/** Whether the read listed only some of a node's children: agent-desktop flags the node, or counts more children than it listed at the depth limit. */
|
|
350
|
+
function childrenCut(node) {
|
|
351
|
+
return node.subtree_truncated === true || (node.children_count !== undefined && node.children_count > (node.children?.length ?? 0));
|
|
352
|
+
}
|
|
306
353
|
function hasTruncatedSubtree(node) {
|
|
307
|
-
|
|
308
|
-
return true;
|
|
309
|
-
// At the depth limit agent-desktop counts the children it did not list.
|
|
310
|
-
if (node.children_count !== undefined && node.children_count > (node.children?.length ?? 0))
|
|
311
|
-
return true;
|
|
312
|
-
return (node.children ?? []).some(hasTruncatedSubtree);
|
|
354
|
+
return childrenCut(node) || (node.children ?? []).some(hasTruncatedSubtree);
|
|
313
355
|
}
|
package/dist/options.d.ts
CHANGED
|
@@ -20,8 +20,8 @@ export interface DesktopOptions {
|
|
|
20
20
|
readonly binary?: string | undefined;
|
|
21
21
|
/**
|
|
22
22
|
* What an observation captures. `full` (default) includes static text, so
|
|
23
|
-
* judgments
|
|
24
|
-
*
|
|
23
|
+
* judgments can read labels; `interactive` keeps only the controls
|
|
24
|
+
* agent-desktop gives a ref. Locators read the whole window either way.
|
|
25
25
|
*/
|
|
26
26
|
readonly snapshot?: 'full' | 'interactive' | undefined;
|
|
27
27
|
/**
|
|
@@ -42,7 +42,7 @@ export interface DesktopOptions {
|
|
|
42
42
|
readonly settle?: number | false | undefined;
|
|
43
43
|
/**
|
|
44
44
|
* How many levels of a window's accessibility tree a read goes down, from
|
|
45
|
-
* 1 to
|
|
45
|
+
* 1 to 50. A read cut short at this depth cannot prove a control absent or
|
|
46
46
|
* the only one, so a locator on such a window reads again until its
|
|
47
47
|
* timeout: raise it for an app nested deeper. Default 24.
|
|
48
48
|
*/
|
package/dist/options.js
CHANGED
|
@@ -8,11 +8,12 @@ const OPTION_KEYS = Object.keys({ binary: true, snapshot: true, headed: true, se
|
|
|
8
8
|
const DEFAULT_SETTLE_MS = 150;
|
|
9
9
|
/** How deep a read goes by default: deeper than the controls of the apps tried, and quick to read. */
|
|
10
10
|
const DEFAULT_MAX_DEPTH = 24;
|
|
11
|
-
/** The deepest read agent-desktop takes
|
|
12
|
-
const MAX_DEPTH_LIMIT =
|
|
11
|
+
/** The deepest read agent-desktop takes: its macOS reader stops at 50 levels and refuses a deeper `--max-depth`. */
|
|
12
|
+
const MAX_DEPTH_LIMIT = 50;
|
|
13
13
|
/** Checks the option bag the way every config option is checked: unknown keys and wrong types fail the config load. */
|
|
14
14
|
export function resolveOptions(options) {
|
|
15
|
-
|
|
15
|
+
// Only a missing bag defaults: a null one (from JSON an agent wrote for e2e mcp) is a config error, not the defaults.
|
|
16
|
+
const bag = options === undefined ? {} : options;
|
|
16
17
|
if (typeof bag !== 'object' || bag === null)
|
|
17
18
|
throw new ConfigurationError('INVALID_CONFIG', 'desktop() options must be an object');
|
|
18
19
|
rejectUnknownKeys('desktop() options', bag, OPTION_KEYS);
|
package/dist/surface.d.ts
CHANGED
|
@@ -47,7 +47,7 @@ export declare class DesktopSurface {
|
|
|
47
47
|
private lifecycle;
|
|
48
48
|
/** The window a test chose with `desktop.useWindow`, which every read and command then uses until the app quits. */
|
|
49
49
|
private chosenWindow;
|
|
50
|
-
/** The window of the newest screen handed to the harness, by observe or locate: the one a point is measured in. */
|
|
50
|
+
/** The window of the newest screen handed to the harness, by observe or locate: the one a point is measured in, the agent's as core hit-tests it, against the newest observation. */
|
|
51
51
|
private pointWindow;
|
|
52
52
|
/** The newest binding of every node id an observation or a locate handed out, newest last. */
|
|
53
53
|
private readonly bindings;
|
|
@@ -135,6 +135,15 @@ export declare class DesktopSurface {
|
|
|
135
135
|
private windowReadable;
|
|
136
136
|
/** The windows of the app the engine launched, for the `desktop` fixture; none while it is not running. */
|
|
137
137
|
windows(signal: AbortSignal, timeoutMs: number): Promise<readonly DesktopWindow[]>;
|
|
138
|
+
/**
|
|
139
|
+
* A window's title. Without Screen Recording permission macOS hides the
|
|
140
|
+
* title the window list reads, and agent-desktop lists the app's name in
|
|
141
|
+
* its place; the window's accessibility root still carries the title, so
|
|
142
|
+
* a title that reads as the app's name is read from there, unless
|
|
143
|
+
* accessibility cannot read the window. A read that fails leaves the title
|
|
144
|
+
* as listed.
|
|
145
|
+
*/
|
|
146
|
+
private titleOf;
|
|
138
147
|
/**
|
|
139
148
|
* `desktop.useWindow()`: reads and acts in the window `id` names from now
|
|
140
149
|
* on, until the app quits, rather than the focused one. With no id, goes
|
|
@@ -160,7 +169,9 @@ export declare class DesktopSurface {
|
|
|
160
169
|
* first: one that exited on its own, or whose id the OS gave to another
|
|
161
170
|
* process since, is not running any more, and nothing is killed. Until the
|
|
162
171
|
* process is known to be gone, the engine keeps it as its own, so a quit
|
|
163
|
-
* that failed is tried again.
|
|
172
|
+
* that failed is tried again. A failed launch that turns out to have left
|
|
173
|
+
* no process is reported, so the user can look for a copy by hand, unless
|
|
174
|
+
* `reportMissing` is false, as before a new launch.
|
|
164
175
|
*/
|
|
165
176
|
private quitNow;
|
|
166
177
|
/**
|
|
@@ -178,10 +189,11 @@ export declare class DesktopSurface {
|
|
|
178
189
|
private idFor;
|
|
179
190
|
/**
|
|
180
191
|
* Projects a snapshot of one window, its node identities scoped to that
|
|
181
|
-
* window. An identity of that window
|
|
182
|
-
* a control that went, or now has a look-alike sibling, never
|
|
183
|
-
* on to another.
|
|
184
|
-
*
|
|
192
|
+
* window. An identity of that window a complete snapshot no longer has is
|
|
193
|
+
* retired: a control that went, or now has a look-alike sibling, never
|
|
194
|
+
* passes its id on to another. A snapshot cut short retires only the
|
|
195
|
+
* identities it saw a look-alike take. Another window's identities stay, so
|
|
196
|
+
* its controls keep their ids when it is read again.
|
|
185
197
|
*/
|
|
186
198
|
private projectWindow;
|
|
187
199
|
/**
|
|
@@ -192,15 +204,23 @@ export declare class DesktopSurface {
|
|
|
192
204
|
* read that only places secure fields: it reads every node even with
|
|
193
205
|
* `snapshot: 'interactive'`, which leaves out a secure node with no action
|
|
194
206
|
* of its own, and hands out no ids, so the ids of the screen the harness
|
|
195
|
-
* holds stay as they are.
|
|
207
|
+
* holds stay as they are. `whole` reads every node too, with ids, for a
|
|
208
|
+
* locator: a label left out of an interactive read would match nothing,
|
|
209
|
+
* and a negative assertion on it pass. Undefined, an empty screen, before `app.open()`
|
|
196
210
|
* and while the running app has no window; a copy of the app the engine
|
|
197
211
|
* did not launch is never read. A read that fails is no empty screen,
|
|
198
212
|
* which would let a negative assertion pass: an app that quit is
|
|
199
213
|
* `INVALID_STATE`, as is a chosen window that closed, and windows none of
|
|
200
|
-
* which could be read
|
|
201
|
-
* again until its deadline.
|
|
214
|
+
* which could be read, or none listed while the screen is locked, are a
|
|
215
|
+
* retryable `NODE_STALE`, so a locator reads again until its deadline.
|
|
202
216
|
*/
|
|
203
217
|
private capture;
|
|
218
|
+
/**
|
|
219
|
+
* Throws a retryable `NODE_STALE` while the Mac's screen is locked: macOS
|
|
220
|
+
* then lists none of the app's windows, which must not read as a window
|
|
221
|
+
* that closed or an app with none open.
|
|
222
|
+
*/
|
|
223
|
+
private unlessLocked;
|
|
204
224
|
/**
|
|
205
225
|
* Throws unless the process the engine launched still runs: its id alone
|
|
206
226
|
* does not show that, since another process can take the id over once the
|
|
@@ -242,7 +262,7 @@ export declare class DesktopSurface {
|
|
|
242
262
|
/**
|
|
243
263
|
* Whether the control a binding names holds keyboard focus in a fresh read
|
|
244
264
|
* of its window. A control with an identity keeps its id; a look-alike has
|
|
245
|
-
* a new id in every read, so it is found by role and place, with as many
|
|
265
|
+
* a new id in every read, so it is found by role, name, developer id, and place, with as many
|
|
246
266
|
* twins around it as before: a twin that went could have let another take
|
|
247
267
|
* its place.
|
|
248
268
|
*/
|
|
@@ -278,7 +298,7 @@ export declare class DesktopSurface {
|
|
|
278
298
|
typeText(text: string, options: {
|
|
279
299
|
readonly replace: boolean;
|
|
280
300
|
}, context: OperationContext): Promise<void>;
|
|
281
|
-
/** One key to whatever holds focus in the app
|
|
301
|
+
/** One key to whatever holds focus in the app's window: agent-desktop brings that window forward (`headed`, which declares this) and sends the key to it. */
|
|
282
302
|
pressKey(key: string, context: OperationContext): Promise<void>;
|
|
283
303
|
/**
|
|
284
304
|
* Sends one key combo to the launched process's window. agent-desktop
|
|
@@ -295,7 +315,10 @@ export declare class DesktopSurface {
|
|
|
295
315
|
* come from the snapshot taken just before, and the window is read again
|
|
296
316
|
* after the capture: a secure field without bounds, a partial snapshot, or
|
|
297
317
|
* a field that moved in between withholds the image rather than ship it
|
|
298
|
-
* unproven.
|
|
318
|
+
* unproven. That is a refusal of policy, `POLICY_DENIED`, as on the mobile
|
|
319
|
+
* engine: the image could hold a credential. A window with no secure field
|
|
320
|
+
* that closed, changed size, or was read in part because the read ran out
|
|
321
|
+
* of time is a capture that failed, and the next one may work.
|
|
299
322
|
*/
|
|
300
323
|
private pixelsFor;
|
|
301
324
|
/** agent-desktop's own wait inside one command: the operation's budget less a margin, so it answers before it is killed. */
|
package/dist/surface.js
CHANGED
|
@@ -16,6 +16,7 @@ import { ConfigurationError, EngineError, OBSERVED_NAME_LIMIT, OBSERVED_TEXT_LIM
|
|
|
16
16
|
import { AGENT_DESKTOP_VERSION, installPinned, pinnedExecutable, pinnedInstalled, resolveExecutable } from './binary.js';
|
|
17
17
|
import { AgentDesktopCli, AgentDesktopFailure, translate } from './cli.js';
|
|
18
18
|
import { toCombo } from './keys.js';
|
|
19
|
+
import { launchArgumentList } from './launch-arguments.js';
|
|
19
20
|
import { ROOT_ID, projectSnapshot } from './nodes.js';
|
|
20
21
|
import { encodePng, windowImage } from './png.js';
|
|
21
22
|
const execFileAsync = promisify(execFile);
|
|
@@ -53,9 +54,11 @@ const UNREADABLE = new Error('no window of the app could be read');
|
|
|
53
54
|
const UNLISTED = new Error('the window list did not find the running app');
|
|
54
55
|
/** The cause of a read of an app that quit since it launched. */
|
|
55
56
|
const QUIT = new Error('the app quit');
|
|
57
|
+
/** The cause of a screenshot withheld because its secure fields could not be shown masked, which the harness reads as masking it could not prove. */
|
|
58
|
+
const UNMASKED = new Error('the secure fields of the screenshot could not be placed');
|
|
56
59
|
/** The prefix of every worker's private agent-desktop state directory, which a later run sweeps once its worker is gone. */
|
|
57
60
|
const STATE_PREFIX = 'e2e-agent-desktop-';
|
|
58
|
-
/** The file in a state directory that names the process that owns it. */
|
|
61
|
+
/** The file in a state directory that names the process that owns it: its id, and on the next line when it started, which tells it from a later process the OS gives the same id. */
|
|
59
62
|
const OWNER_FILE = 'owner';
|
|
60
63
|
/** What the owner file holds once its worker gave the directory up: a later sweep takes it, though the process that wrote it may still run another worker. */
|
|
61
64
|
const ABANDONED = 'abandoned';
|
|
@@ -67,7 +70,8 @@ const ORPHAN_AGE_MS = 24 * 60 * 60 * 1000;
|
|
|
67
70
|
* The variables of the run's environment the app under test and
|
|
68
71
|
* agent-desktop get, and nothing else: the run's secrets, model keys, and CI
|
|
69
72
|
* tokens never reach a process the runner does not control. `LC_*` passes
|
|
70
|
-
* too.
|
|
73
|
+
* too. agent-desktop also gets `AGENT_DESKTOP_HOME`, its private state
|
|
74
|
+
* directory, and an app it launches by bundle id inherits that path.
|
|
71
75
|
*/
|
|
72
76
|
const INHERITED_ENV = new Set(['HOME', 'USER', 'LOGNAME', 'SHELL', 'PATH', 'TMPDIR', 'LANG', '__CF_USER_TEXT_ENCODING']);
|
|
73
77
|
/** Launch arguments every launch gets first: no window or document the app saved at its last quit comes back. */
|
|
@@ -91,6 +95,10 @@ function sleep(ms, signal) {
|
|
|
91
95
|
signal.addEventListener('abort', done, { once: true });
|
|
92
96
|
});
|
|
93
97
|
}
|
|
98
|
+
/** A read that ran out of time, which proves nothing absent: a retryable `NODE_STALE` that says what it could not tell. */
|
|
99
|
+
function partialRead(what) {
|
|
100
|
+
return new EngineError('NODE_STALE', `the window was read only in part, as agent-desktop ran out of time, so ${what} cannot be told`, { retryable: true, cause: UNREADABLE });
|
|
101
|
+
}
|
|
94
102
|
function notActionable(text) {
|
|
95
103
|
return new EngineError('NOT_ACTIONABLE', text, { retryable: false });
|
|
96
104
|
}
|
|
@@ -190,7 +198,7 @@ export class DesktopSurface {
|
|
|
190
198
|
lifecycle = Promise.resolve();
|
|
191
199
|
/** The window a test chose with `desktop.useWindow`, which every read and command then uses until the app quits. */
|
|
192
200
|
chosenWindow;
|
|
193
|
-
/** The window of the newest screen handed to the harness, by observe or locate: the one a point is measured in. */
|
|
201
|
+
/** The window of the newest screen handed to the harness, by observe or locate: the one a point is measured in, the agent's as core hit-tests it, against the newest observation. */
|
|
194
202
|
pointWindow;
|
|
195
203
|
/** The newest binding of every node id an observation or a locate handed out, newest last. */
|
|
196
204
|
bindings = new Map();
|
|
@@ -231,7 +239,8 @@ export class DesktopSurface {
|
|
|
231
239
|
await sweepStateDirectories();
|
|
232
240
|
// A private state directory per worker, named for the process that owns it so a later sweep can tell it was left behind.
|
|
233
241
|
this.home = await mkdtemp(path.join(tmpdir(), STATE_PREFIX));
|
|
234
|
-
await
|
|
242
|
+
const started = await startedAt(process.pid, info.signal);
|
|
243
|
+
await writeFile(path.join(this.home, OWNER_FILE), started === undefined ? String(process.pid) : `${process.pid}\n${started}`);
|
|
235
244
|
// agent-desktop starts the app it launches with its own environment, so it gets the trimmed one too.
|
|
236
245
|
this.cli = new AgentDesktopCli(executable, { ...this.appEnv, AGENT_DESKTOP_HOME: this.home });
|
|
237
246
|
const status = await this.run(['status'], { label: 'agent-desktop status', input: false, signal: info.signal, timeoutMs: 15_000 });
|
|
@@ -353,7 +362,8 @@ export class DesktopSurface {
|
|
|
353
362
|
const target = this.launchTarget;
|
|
354
363
|
if (target === undefined)
|
|
355
364
|
throw unsupported("launching the app needs the target's app.bundleId or app.appPath");
|
|
356
|
-
|
|
365
|
+
// A failed launch that left no process is nothing to clean up before a new one.
|
|
366
|
+
await this.quitNow(operation.signal, operation.timeoutMs, false);
|
|
357
367
|
// Marks the process this launch starts, so it is told apart from every other copy by evidence, never by timing.
|
|
358
368
|
const launch = { target, token: randomUUID(), before: await this.runningCopies(target, operation) };
|
|
359
369
|
// Written before anything is sent, so the app stays findable by its token if this worker is killed mid-launch or never finds its process.
|
|
@@ -405,7 +415,7 @@ export class DesktopSurface {
|
|
|
405
415
|
const { target, token } = launch;
|
|
406
416
|
// In the background unless `headed` or the run's `--headed`: otherwise the app never takes the keyboard from whoever is at the Mac.
|
|
407
417
|
const args = ['launch', target, '--no-attach', ...(this.foreground ? ['--activate'] : []), '--timeout', String(this.toolBudget(operation)), `--env=${LAUNCH_TOKEN}=${token}`];
|
|
408
|
-
for (const argument of [...FRESH_STATE_ARGUMENTS, ...(this.app
|
|
418
|
+
for (const argument of [...FRESH_STATE_ARGUMENTS, ...launchArgumentList(this.app)])
|
|
409
419
|
args.push(`--arg=${argument}`);
|
|
410
420
|
let launched;
|
|
411
421
|
try {
|
|
@@ -446,7 +456,7 @@ export class DesktopSurface {
|
|
|
446
456
|
if (operation.timeoutMs <= 0)
|
|
447
457
|
throw new EngineError('OPERATION_TIMEOUT', `opening ${bundle}: the operation's budget ran out before it was sent`, { retryable: false, cause: NOT_SENT });
|
|
448
458
|
// `-g` keeps the app in the background unless `headed` or the run's `--headed`; `open` hands the app its own environment, so it runs with the trimmed one.
|
|
449
|
-
const args = [...(this.foreground ? [] : ['-g']), '-n', '--env', `${LAUNCH_TOKEN}=${launch.token}`, bundle, '--args', ...FRESH_STATE_ARGUMENTS, ...(this.app
|
|
459
|
+
const args = [...(this.foreground ? [] : ['-g']), '-n', '--env', `${LAUNCH_TOKEN}=${launch.token}`, bundle, '--args', ...FRESH_STATE_ARGUMENTS, ...launchArgumentList(this.app)];
|
|
450
460
|
try {
|
|
451
461
|
await execFileAsync('open', args, { signal: operation.signal, timeout: operation.timeoutMs, env: this.appEnv });
|
|
452
462
|
}
|
|
@@ -548,9 +558,11 @@ export class DesktopSurface {
|
|
|
548
558
|
break;
|
|
549
559
|
}
|
|
550
560
|
// A window not readable yet, an app the window list missed, or an app that quit, is what this wait looks at below.
|
|
551
|
-
|
|
561
|
+
// So is a window replaced while it was read (agent-desktop's root went stale), a retryable read of a window that was there.
|
|
562
|
+
const stale = cause instanceof EngineError && cause.code === 'NODE_STALE' && cause.retryable;
|
|
563
|
+
if (!(cause instanceof EngineError && (stale || cause.cause === QUIT || cause.cause === UNLISTED)))
|
|
552
564
|
throw cause;
|
|
553
|
-
if (cause.cause
|
|
565
|
+
if (stale && cause.cause !== UNLISTED)
|
|
554
566
|
sawWindow = true;
|
|
555
567
|
}
|
|
556
568
|
if (screen !== undefined)
|
|
@@ -572,7 +584,7 @@ export class DesktopSurface {
|
|
|
572
584
|
if (budgetBound)
|
|
573
585
|
throw new EngineError('OPERATION_TIMEOUT', `${this.appName ?? 'the app'} launched, but the open ran out of time before its window could be read`, { retryable: false });
|
|
574
586
|
// A locked screen hides every window from accessibility, and can list none or time out: neither read tells it from an app with no window, so macOS is asked.
|
|
575
|
-
if ((await screenLocked(operation.signal)) === true) {
|
|
587
|
+
if ((await screenLocked(operation.signal, operation.timeoutMs)) === true) {
|
|
576
588
|
throw new EngineError('ENGINE_FAILURE', `${this.appName ?? 'the app'} launched, but the Mac's screen is locked, so no window can be read through accessibility; unlock it and run again`, { retryable: false });
|
|
577
589
|
}
|
|
578
590
|
if (!sawWindow && listedNone && !this.foreground) {
|
|
@@ -586,8 +598,29 @@ export class DesktopSurface {
|
|
|
586
598
|
}
|
|
587
599
|
/** The windows of the app the engine launched, for the `desktop` fixture; none while it is not running. */
|
|
588
600
|
async windows(signal, timeoutMs) {
|
|
601
|
+
const deadline = Date.now() + timeoutMs;
|
|
589
602
|
const windows = await this.ownWindows({ signal, timeoutMs });
|
|
590
|
-
|
|
603
|
+
const listed = [];
|
|
604
|
+
for (const window of windows)
|
|
605
|
+
listed.push({ id: window.id, title: await this.titleOf(window, signal, deadline), focused: window.is_focused === true });
|
|
606
|
+
return listed;
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
609
|
+
* A window's title. Without Screen Recording permission macOS hides the
|
|
610
|
+
* title the window list reads, and agent-desktop lists the app's name in
|
|
611
|
+
* its place; the window's accessibility root still carries the title, so
|
|
612
|
+
* a title that reads as the app's name is read from there, unless
|
|
613
|
+
* accessibility cannot read the window. A read that fails leaves the title
|
|
614
|
+
* as listed.
|
|
615
|
+
*/
|
|
616
|
+
async titleOf(window, signal, deadline) {
|
|
617
|
+
const listed = window.title ?? '';
|
|
618
|
+
if (window.app_name === undefined || listed !== window.app_name || window.accessible === false)
|
|
619
|
+
return listed;
|
|
620
|
+
const timeoutMs = deadline - Date.now();
|
|
621
|
+
const raw = await this.run(['snapshot', '--window-id', window.id, '--max-depth', '1', '--timeout-ms', String(this.toolBudget({ timeoutMs }))], { label: 'read window title', input: false, signal, timeoutMs }).catch(() => undefined);
|
|
622
|
+
const title = raw?.tree.name;
|
|
623
|
+
return title === undefined || title.trim() === '' ? listed : title;
|
|
591
624
|
}
|
|
592
625
|
/**
|
|
593
626
|
* `desktop.useWindow()`: reads and acts in the window `id` names from now
|
|
@@ -633,9 +666,11 @@ export class DesktopSurface {
|
|
|
633
666
|
* first: one that exited on its own, or whose id the OS gave to another
|
|
634
667
|
* process since, is not running any more, and nothing is killed. Until the
|
|
635
668
|
* process is known to be gone, the engine keeps it as its own, so a quit
|
|
636
|
-
* that failed is tried again.
|
|
669
|
+
* that failed is tried again. A failed launch that turns out to have left
|
|
670
|
+
* no process is reported, so the user can look for a copy by hand, unless
|
|
671
|
+
* `reportMissing` is false, as before a new launch.
|
|
637
672
|
*/
|
|
638
|
-
async quitNow(signal, timeoutMs) {
|
|
673
|
+
async quitNow(signal, timeoutMs, reportMissing = true) {
|
|
639
674
|
const deadline = Date.now() + timeoutMs;
|
|
640
675
|
const pending = this.pending;
|
|
641
676
|
if (this.pid === undefined && pending !== undefined) {
|
|
@@ -673,7 +708,7 @@ export class DesktopSurface {
|
|
|
673
708
|
this.pending = undefined;
|
|
674
709
|
if (this.home !== undefined)
|
|
675
710
|
await rm(path.join(this.home, APP_FILE), { force: true }).catch(() => undefined);
|
|
676
|
-
if (carriers.length > 0)
|
|
711
|
+
if (carriers.length > 0 || !reportMissing)
|
|
677
712
|
return;
|
|
678
713
|
throw new EngineError('ENGINE_FAILURE', `a launch of ${pending.target} failed, and no process it started was found; if the app is running, quit it by hand`, { retryable: false });
|
|
679
714
|
}
|
|
@@ -745,23 +780,28 @@ export class DesktopSurface {
|
|
|
745
780
|
}
|
|
746
781
|
/**
|
|
747
782
|
* Projects a snapshot of one window, its node identities scoped to that
|
|
748
|
-
* window. An identity of that window
|
|
749
|
-
* a control that went, or now has a look-alike sibling, never
|
|
750
|
-
* on to another.
|
|
751
|
-
*
|
|
783
|
+
* window. An identity of that window a complete snapshot no longer has is
|
|
784
|
+
* retired: a control that went, or now has a look-alike sibling, never
|
|
785
|
+
* passes its id on to another. A snapshot cut short retires only the
|
|
786
|
+
* identities it saw a look-alike take. Another window's identities stay, so
|
|
787
|
+
* its controls keep their ids when it is read again.
|
|
752
788
|
*/
|
|
753
|
-
projectWindow(raw) {
|
|
754
|
-
|
|
789
|
+
projectWindow(raw, minimized, interactive) {
|
|
790
|
+
// An interactive read (-i) prunes the tree, a whole one does not: each keeps its own identities, so one never retires the other's ids.
|
|
791
|
+
const scope = `${raw.window?.id ?? ''}|${interactive ? 'i' : 'w'}|`;
|
|
755
792
|
const used = new Set();
|
|
793
|
+
const shared = new Set();
|
|
756
794
|
const screen = projectSnapshot(raw, (identity) => {
|
|
757
795
|
if (identity === undefined)
|
|
758
796
|
return this.idFor(undefined);
|
|
759
|
-
const scoped = `${
|
|
797
|
+
const scoped = `${scope}${identity}`;
|
|
760
798
|
used.add(scoped);
|
|
761
799
|
return this.idFor(scoped);
|
|
762
|
-
});
|
|
800
|
+
}, { minimized, interactive, onLookAlike: (identity) => shared.add(`${scope}${identity}`), priorIdOf: (identity) => this.ids.get(`${scope}${identity}`) });
|
|
801
|
+
// A read that ran out of time saw only part of the window: what it did not reach may still be there, so only an identity it saw a twin take retires.
|
|
802
|
+
const partial = raw.complete === false || raw.truncated === true;
|
|
763
803
|
for (const identity of this.ids.keys())
|
|
764
|
-
if (identity.startsWith(
|
|
804
|
+
if (identity.startsWith(scope) && !used.has(identity) && (!partial || shared.has(identity)))
|
|
765
805
|
this.ids.delete(identity);
|
|
766
806
|
return screen;
|
|
767
807
|
}
|
|
@@ -774,40 +814,54 @@ export class DesktopSurface {
|
|
|
774
814
|
* read that only places secure fields: it reads every node even with
|
|
775
815
|
* `snapshot: 'interactive'`, which leaves out a secure node with no action
|
|
776
816
|
* of its own, and hands out no ids, so the ids of the screen the harness
|
|
777
|
-
* holds stay as they are.
|
|
817
|
+
* holds stay as they are. `whole` reads every node too, with ids, for a
|
|
818
|
+
* locator: a label left out of an interactive read would match nothing,
|
|
819
|
+
* and a negative assertion on it pass. Undefined, an empty screen, before `app.open()`
|
|
778
820
|
* and while the running app has no window; a copy of the app the engine
|
|
779
821
|
* did not launch is never read. A read that fails is no empty screen,
|
|
780
822
|
* which would let a negative assertion pass: an app that quit is
|
|
781
823
|
* `INVALID_STATE`, as is a chosen window that closed, and windows none of
|
|
782
|
-
* which could be read
|
|
783
|
-
* again until its deadline.
|
|
824
|
+
* which could be read, or none listed while the screen is locked, are a
|
|
825
|
+
* retryable `NODE_STALE`, so a locator reads again until its deadline.
|
|
784
826
|
*/
|
|
785
|
-
async capture(operation, windowId = this.chosenWindow, forMask = false) {
|
|
827
|
+
async capture(operation, windowId = this.chosenWindow, forMask = false, whole = false) {
|
|
786
828
|
const windows = await this.ownWindows(operation, true);
|
|
787
829
|
const chosen = windowId !== undefined && windowId === this.chosenWindow;
|
|
788
|
-
if (chosen && !windows.some((window) => window.id === windowId))
|
|
830
|
+
if (chosen && !windows.some((window) => window.id === windowId)) {
|
|
831
|
+
await this.unlessLocked(operation);
|
|
789
832
|
throw this.chosenClosed();
|
|
790
|
-
|
|
833
|
+
}
|
|
834
|
+
if (windows.length === 0 && this.pid !== undefined) {
|
|
791
835
|
await this.requireRunning(operation);
|
|
836
|
+
await this.unlessLocked(operation);
|
|
837
|
+
}
|
|
838
|
+
// A window the list no longer has is closed: its identities go, so windows opened and closed one after another do not pile them up.
|
|
839
|
+
const open = new Set(windows.map((window) => window.id));
|
|
840
|
+
for (const identity of this.ids.keys())
|
|
841
|
+
if (!open.has(identity.slice(0, identity.indexOf('|'))))
|
|
842
|
+
this.ids.delete(identity);
|
|
792
843
|
const candidates = windowId === undefined
|
|
793
844
|
? [...windows.filter((window) => window.is_focused === true), ...windows.filter((window) => window.is_focused !== true && window.visible !== false), ...windows.filter((window) => window.is_focused !== true && window.visible === false)]
|
|
794
845
|
: windows.filter((window) => window.id === windowId);
|
|
795
846
|
for (const window of candidates) {
|
|
796
847
|
const args = ['snapshot', '--window-id', window.id, '--include-bounds', '--max-depth', String(this.options.maxDepth), '--timeout-ms', String(this.toolBudget(operation))];
|
|
797
|
-
|
|
848
|
+
const interactive = this.options.interactiveOnly && !forMask && !whole;
|
|
849
|
+
if (interactive)
|
|
798
850
|
args.push('-i');
|
|
799
851
|
try {
|
|
800
852
|
const raw = await this.run(args, { label: 'snapshot', input: false, signal: operation.signal, timeoutMs: operation.timeoutMs, raw: true });
|
|
801
853
|
if (!forMask)
|
|
802
|
-
return this.projectWindow(raw);
|
|
854
|
+
return this.projectWindow(raw, window.minimized === true, interactive);
|
|
803
855
|
let scratch = 0;
|
|
804
856
|
return projectSnapshot(raw, () => `mask${(scratch += 1)}`);
|
|
805
857
|
}
|
|
806
858
|
catch (cause) {
|
|
807
859
|
// A panel accessibility cannot read, or a window that just closed: try the next one.
|
|
808
860
|
if (cause instanceof AgentDesktopFailure && NO_WINDOW_CODES.has(cause.code)) {
|
|
809
|
-
if (chosen)
|
|
861
|
+
if (chosen) {
|
|
862
|
+
await this.unlessLocked(operation);
|
|
810
863
|
throw this.chosenClosed();
|
|
864
|
+
}
|
|
811
865
|
continue;
|
|
812
866
|
}
|
|
813
867
|
if (cause instanceof AgentDesktopFailure && cause.details.kind === 'window_without_accessibility_element')
|
|
@@ -819,6 +873,16 @@ export class DesktopSurface {
|
|
|
819
873
|
return undefined;
|
|
820
874
|
throw new EngineError('NODE_STALE', `no window of ${this.appName ?? 'the app'} could be read through accessibility (on macOS the screen may be locked), or each closed while it was read`, { retryable: true, cause: UNREADABLE });
|
|
821
875
|
}
|
|
876
|
+
/**
|
|
877
|
+
* Throws a retryable `NODE_STALE` while the Mac's screen is locked: macOS
|
|
878
|
+
* then lists none of the app's windows, which must not read as a window
|
|
879
|
+
* that closed or an app with none open.
|
|
880
|
+
*/
|
|
881
|
+
async unlessLocked(operation) {
|
|
882
|
+
if ((await screenLocked(operation.signal, operation.timeoutMs)) !== true)
|
|
883
|
+
return;
|
|
884
|
+
throw new EngineError('NODE_STALE', `the Mac's screen is locked, so no window of ${this.appName ?? 'the app'} can be read through accessibility; reading again`, { retryable: true, cause: UNREADABLE });
|
|
885
|
+
}
|
|
822
886
|
/**
|
|
823
887
|
* Throws unless the process the engine launched still runs: its id alone
|
|
824
888
|
* does not show that, since another process can take the id over once the
|
|
@@ -888,16 +952,18 @@ export class DesktopSurface {
|
|
|
888
952
|
if (screen === undefined) {
|
|
889
953
|
return { root: { ref: { id: ROOT_ID, revision: '' }, role: 'window' }, viewport: { width: 0, height: 0 } };
|
|
890
954
|
}
|
|
891
|
-
this.pointWindow = screen.windowId;
|
|
892
955
|
this.remember(screen.bindings.values());
|
|
893
956
|
let withheld = false;
|
|
894
957
|
const pixels = options?.pixels === true ? await this.pixelsFor(screen, operation).catch((cause) => {
|
|
895
|
-
|
|
958
|
+
// Out of time or cancelled is no screenshot at all, and the observation with it.
|
|
959
|
+
if (cause instanceof EngineError && (cause.code === 'OPERATION_TIMEOUT' || cause.code === 'CANCELLED'))
|
|
896
960
|
throw cause;
|
|
897
|
-
|
|
961
|
+
// A capture that failed (no Screen Recording permission) is no screenshot, as on the mobile engine, not masking that could not be proven.
|
|
962
|
+
withheld = cause instanceof Error && cause.cause === UNMASKED;
|
|
898
963
|
return undefined;
|
|
899
964
|
}) : undefined;
|
|
900
|
-
|
|
965
|
+
this.pointWindow = screen.windowId;
|
|
966
|
+
// Pixels withheld for their secure fields still report the regions masked, none, so the harness reads masking it could not prove (MASKING_UNPROVEN), not a missing screenshot.
|
|
901
967
|
return {
|
|
902
968
|
...this.snapshotOf(screen),
|
|
903
969
|
...(pixels === undefined ? (withheld ? { maskedRegionCount: 0 } : {}) : { pixels: pixels.pixels, maskedRegionCount: pixels.masked }),
|
|
@@ -919,7 +985,8 @@ export class DesktopSurface {
|
|
|
919
985
|
throw unsupported("macOS accessibility reports no placeholder to match; a field with no label is named by its placeholder, so find it with getByRole('textbox', name) or getByLabel(name)");
|
|
920
986
|
}
|
|
921
987
|
const operation = withDeadline(context);
|
|
922
|
-
|
|
988
|
+
// A locator reads the whole window whatever `snapshot` says, as on the mobile engine: the interactive read leaves out static text.
|
|
989
|
+
const screen = await this.capture(operation, this.chosenWindow, false, true);
|
|
923
990
|
if (screen === undefined)
|
|
924
991
|
return [];
|
|
925
992
|
const sink = this.attempt?.screen;
|
|
@@ -980,13 +1047,22 @@ export class DesktopSurface {
|
|
|
980
1047
|
async perform(ref, action, context) {
|
|
981
1048
|
const operation = withDeadline(context);
|
|
982
1049
|
if (ref.id === ROOT_ID) {
|
|
1050
|
+
// The window is in view unless it is minimized: toHaveScreenshot on a window locator scrolls it into view first.
|
|
1051
|
+
if (action.kind === 'scrollIntoView') {
|
|
1052
|
+
const shown = await this.capture(operation);
|
|
1053
|
+
if (shown === undefined || shown.root.states?.hidden === true)
|
|
1054
|
+
throw notActionable('the window is not on screen (minimized, or closed), so it cannot be brought into view');
|
|
1055
|
+
return;
|
|
1056
|
+
}
|
|
983
1057
|
if (action.kind !== 'swipe')
|
|
984
|
-
throw notActionable(`the window takes swipe only, not ${action.kind}; act on a node`);
|
|
1058
|
+
throw notActionable(`the window takes swipe and scrollIntoView only, not ${action.kind}; act on a node`);
|
|
985
1059
|
// The root is the window as it is now: read it again, since a locate or a restart may have moved on from the last observation.
|
|
986
1060
|
const screen = await this.capture(operation);
|
|
987
1061
|
if (screen !== undefined)
|
|
988
1062
|
this.remember(screen.bindings.values());
|
|
989
1063
|
const target = screen?.scrollTarget;
|
|
1064
|
+
if (target === undefined && screen?.cutShort === true)
|
|
1065
|
+
throw partialRead('what in it scrolls');
|
|
990
1066
|
if (target === undefined)
|
|
991
1067
|
throw notActionable('the window has nothing that scrolls');
|
|
992
1068
|
await this.scroll(this.binding({ id: target, revision: '' }), action.direction, action.momentum, operation);
|
|
@@ -1016,6 +1092,9 @@ export class DesktopSurface {
|
|
|
1016
1092
|
await this.refCommand(action.kind, binding, operation);
|
|
1017
1093
|
break;
|
|
1018
1094
|
case 'scrollIntoView':
|
|
1095
|
+
// Static text gets no ref to scroll to; agent-desktop marks a node outside the window offscreen, read as hidden, so one not hidden is in view already.
|
|
1096
|
+
if (binding.ref === undefined && binding.states?.hidden !== true)
|
|
1097
|
+
break;
|
|
1019
1098
|
await this.refCommand('scroll-to', binding, operation);
|
|
1020
1099
|
break;
|
|
1021
1100
|
case 'hover':
|
|
@@ -1032,12 +1111,16 @@ export class DesktopSurface {
|
|
|
1032
1111
|
throw notActionable(`node ${binding.id} was read in no window to press a key into`);
|
|
1033
1112
|
await this.refCommand('focus', binding, operation);
|
|
1034
1113
|
try {
|
|
1035
|
-
// The key goes to the process, which hands it to its key window: with
|
|
1036
|
-
|
|
1114
|
+
// The key goes to the process, which hands it to its key window: with another window that could be it, the control's must be the one shown focused.
|
|
1115
|
+
// A window on another Space, which agent-desktop lists as not visible, is still the control's: press --headed brings it forward.
|
|
1116
|
+
const windows = await this.ownWindows(operation);
|
|
1037
1117
|
const home = windows.find((candidate) => candidate.id === window);
|
|
1038
1118
|
if (home === undefined)
|
|
1039
1119
|
throw notActionable(`the window node ${binding.id} was read in is gone`);
|
|
1040
|
-
if (
|
|
1120
|
+
if (home.minimized === true)
|
|
1121
|
+
throw notActionable(`the window node ${binding.id} was read in is minimized, so no key reaches it`);
|
|
1122
|
+
const rivals = windows.filter((candidate) => candidate.id !== window && candidate.visible !== false && candidate.minimized !== true);
|
|
1123
|
+
if (rivals.length > 0 && home.is_focused !== true) {
|
|
1041
1124
|
throw notActionable(`node ${binding.id} is in a window that cannot be shown to be the app's key window, so the key could land in another window`);
|
|
1042
1125
|
}
|
|
1043
1126
|
// Focusing can hand focus on (a dialog it opens): the key goes only when the control still holds it.
|
|
@@ -1096,14 +1179,20 @@ export class DesktopSurface {
|
|
|
1096
1179
|
/**
|
|
1097
1180
|
* Whether the control a binding names holds keyboard focus in a fresh read
|
|
1098
1181
|
* of its window. A control with an identity keeps its id; a look-alike has
|
|
1099
|
-
* a new id in every read, so it is found by role and place, with as many
|
|
1182
|
+
* a new id in every read, so it is found by role, name, developer id, and place, with as many
|
|
1100
1183
|
* twins around it as before: a twin that went could have let another take
|
|
1101
1184
|
* its place.
|
|
1102
1185
|
*/
|
|
1103
1186
|
async holdsFocus(binding, window, operation) {
|
|
1104
|
-
|
|
1187
|
+
// Read as the binding was: a locator's binding comes from a whole read, an observation's from an interactive one.
|
|
1188
|
+
const now = await this.capture(operation, window, false, !binding.interactive);
|
|
1189
|
+
// A read that ran out of time gives every control a fresh id, so it cannot tell which one holds focus: that is no lost focus.
|
|
1190
|
+
if (now?.cutShort === true)
|
|
1191
|
+
throw partialRead('which control holds focus');
|
|
1105
1192
|
return [...(now?.bindings.values() ?? [])].some((candidate) => candidate.states?.focused === true &&
|
|
1106
|
-
(binding.stable
|
|
1193
|
+
(binding.stable
|
|
1194
|
+
? candidate.id === binding.id
|
|
1195
|
+
: candidate.role === binding.role && candidate.name === binding.name && candidate.testId === binding.testId && sameRect(candidate.rect, binding.rect) && candidate.likeness === binding.likeness));
|
|
1107
1196
|
}
|
|
1108
1197
|
/**
|
|
1109
1198
|
* Chooses an option of a list or dropdown with agent-desktop's `select`,
|
|
@@ -1186,6 +1275,9 @@ export class DesktopSurface {
|
|
|
1186
1275
|
// A menu takes a moment to close after Escape. The last look decides: one that could not tell leaves it unknown.
|
|
1187
1276
|
const until = Date.now() + Math.min(MENU_CLOSE_MS, operation.timeoutMs);
|
|
1188
1277
|
for (;;) {
|
|
1278
|
+
// Cancelled, each look fails at once and the wait would never yield: it cannot tell any more.
|
|
1279
|
+
if (operation.signal.aborted)
|
|
1280
|
+
return 'unconfirmed';
|
|
1189
1281
|
const open = await menus();
|
|
1190
1282
|
if (open === 0)
|
|
1191
1283
|
return 'closed';
|
|
@@ -1244,6 +1336,9 @@ export class DesktopSurface {
|
|
|
1244
1336
|
const operation = withDeadline(context);
|
|
1245
1337
|
const screen = await this.capture(operation);
|
|
1246
1338
|
const focused = screen === undefined ? undefined : [...screen.bindings.values()].find((binding) => binding.ref !== undefined && binding.states?.focused === true);
|
|
1339
|
+
// A read that ran out of time may stop short of the focused field: no proof that none has focus.
|
|
1340
|
+
if (focused === undefined && screen?.cutShort === true)
|
|
1341
|
+
throw partialRead('which field has keyboard focus');
|
|
1247
1342
|
if (focused === undefined)
|
|
1248
1343
|
throw notActionable('no field has keyboard focus; tap or focus the field first');
|
|
1249
1344
|
// agent-desktop types only into a control that offers TypeText; a list or button with focus would take the select-all and delete as commands.
|
|
@@ -1271,7 +1366,7 @@ export class DesktopSurface {
|
|
|
1271
1366
|
await type().catch((cause) => Promise.reject(translate(cause, 'type', true)));
|
|
1272
1367
|
await this.settle(operation);
|
|
1273
1368
|
}
|
|
1274
|
-
/** One key to whatever holds focus in the app
|
|
1369
|
+
/** One key to whatever holds focus in the app's window: agent-desktop brings that window forward (`headed`, which declares this) and sends the key to it. */
|
|
1275
1370
|
async pressKey(key, context) {
|
|
1276
1371
|
const operation = withDeadline(context);
|
|
1277
1372
|
const combo = toCombo(key);
|
|
@@ -1327,30 +1422,54 @@ export class DesktopSurface {
|
|
|
1327
1422
|
* come from the snapshot taken just before, and the window is read again
|
|
1328
1423
|
* after the capture: a secure field without bounds, a partial snapshot, or
|
|
1329
1424
|
* a field that moved in between withholds the image rather than ship it
|
|
1330
|
-
* unproven.
|
|
1425
|
+
* unproven. That is a refusal of policy, `POLICY_DENIED`, as on the mobile
|
|
1426
|
+
* engine: the image could hold a credential. A window with no secure field
|
|
1427
|
+
* that closed, changed size, or was read in part because the read ran out
|
|
1428
|
+
* of time is a capture that failed, and the next one may work.
|
|
1331
1429
|
*/
|
|
1332
1430
|
async pixelsFor(observed, operation) {
|
|
1333
1431
|
if (observed.windowId === undefined)
|
|
1334
1432
|
throw notActionable('the observed surface has no window to capture');
|
|
1433
|
+
const unmasked = (reason) => new ConfigurationError('POLICY_DENIED', `${reason}, so the screenshot is withheld`, { cause: UNMASKED });
|
|
1335
1434
|
// The secure fields come from a whole read of the window, never an interactive one, which can leave a secure node out.
|
|
1336
1435
|
const screen = this.options.interactiveOnly ? await this.capture(operation, observed.windowId, true) : observed;
|
|
1337
1436
|
if (screen?.windowId === undefined)
|
|
1338
|
-
throw new EngineError('ENGINE_FAILURE', 'the window
|
|
1437
|
+
throw new EngineError('ENGINE_FAILURE', 'the window closed before it could be captured', { retryable: false });
|
|
1438
|
+
// The image goes out beside the observed tree: a window that changed size since that read would pair it with the wrong viewport and boxes.
|
|
1439
|
+
if (screen.viewport.width !== observed.viewport.width || screen.viewport.height !== observed.viewport.height) {
|
|
1440
|
+
throw new EngineError('ENGINE_FAILURE', 'the window changed size between the read and the capture', { retryable: false });
|
|
1441
|
+
}
|
|
1339
1442
|
const secure = screen.secureRects;
|
|
1340
|
-
// A refusal of policy, as on the mobile engine: the image could hold a credential, so it never leaves the engine.
|
|
1341
1443
|
if (secure === undefined)
|
|
1342
|
-
throw
|
|
1444
|
+
throw unmasked('a secure field on screen has no bounds to mask');
|
|
1343
1445
|
// A snapshot cut short can leave out a secure field, which would then go unmasked.
|
|
1344
|
-
if (screen.truncated)
|
|
1345
|
-
|
|
1446
|
+
if (screen.truncated) {
|
|
1447
|
+
// One that ran out of time and saw no secure field may read whole next time: a capture that failed, not a refusal.
|
|
1448
|
+
if (screen.cutShort && secure.length === 0)
|
|
1449
|
+
throw new EngineError('ENGINE_FAILURE', 'the window was read only in part, as agent-desktop ran out of time, so it cannot be shown to hold no secure field', { retryable: false });
|
|
1450
|
+
throw unmasked(screen.cutShort
|
|
1451
|
+
? 'the snapshot did not read the whole window, so a secure field may be missing from it'
|
|
1452
|
+
: `the window is deeper than desktop({ maxDepth }) reads (${this.options.maxDepth} levels), so a secure field may be missing from the read; raise maxDepth`);
|
|
1453
|
+
}
|
|
1346
1454
|
const directory = await mkdtemp(path.join(this.home ?? tmpdir(), 'shot-'));
|
|
1347
1455
|
try {
|
|
1348
1456
|
const file = path.join(directory, 'window.png');
|
|
1349
|
-
|
|
1457
|
+
// agent-desktop captures to a file of its own under TMPDIR first, unmasked, which a process killed mid-capture leaves behind: here it is under this directory, removed below.
|
|
1458
|
+
const shot = await this.run(['screenshot', '--window-id', screen.windowId, file], { label: 'screenshot', input: false, signal: operation.signal, timeoutMs: operation.timeoutMs, tmpdir: directory });
|
|
1350
1459
|
// The snapshot and the capture are two commands: read the window again, and withhold the image if a secure field moved, came, or went between them.
|
|
1351
|
-
const after = await this.capture(operation, screen.windowId, true)
|
|
1460
|
+
const after = await this.capture(operation, screen.windowId, true).catch((cause) => {
|
|
1461
|
+
if (cause instanceof EngineError && (cause.code === 'OPERATION_TIMEOUT' || cause.code === 'CANCELLED'))
|
|
1462
|
+
throw cause;
|
|
1463
|
+
// The image holds a secure field the read after it could not place: that is masking not proven, not a capture that failed.
|
|
1464
|
+
if (secure.length > 0)
|
|
1465
|
+
throw unmasked('the window could not be read again after the capture, so its secure fields cannot be placed');
|
|
1466
|
+
throw cause;
|
|
1467
|
+
});
|
|
1352
1468
|
if (after === undefined || after.windowId !== screen.windowId || after.truncated || !sameRects(after.secureRects, secure) || after.viewport.width !== screen.viewport.width || after.viewport.height !== screen.viewport.height) {
|
|
1353
|
-
|
|
1469
|
+
// Only a secure field, seen in either read or possibly below the depth a read stops at, makes this a matter of masking; a read that ran out of time is tried again.
|
|
1470
|
+
if (secure.length > 0 || (after !== undefined && ((after.truncated && !after.cutShort) || after.secureRects?.length !== 0)))
|
|
1471
|
+
throw unmasked('the window changed while it was captured, so its secure fields cannot be placed');
|
|
1472
|
+
throw new EngineError('ENGINE_FAILURE', 'the window closed or changed size while it was captured', { retryable: false });
|
|
1354
1473
|
}
|
|
1355
1474
|
const scale = typeof shot.scale_factor === 'number' && shot.scale_factor > 0 ? shot.scale_factor : 1;
|
|
1356
1475
|
const raw = new Uint8Array(await readFile(file));
|
|
@@ -1358,7 +1477,10 @@ export class DesktopSurface {
|
|
|
1358
1477
|
const rects = secure.map((rect) => ({ x: rect.x * scale, y: rect.y * scale, width: rect.width * scale, height: rect.height * scale }));
|
|
1359
1478
|
const image = windowImage(raw, expected, rects);
|
|
1360
1479
|
if (image === undefined) {
|
|
1361
|
-
|
|
1480
|
+
const mismatch = `the window capture does not match the window's ${expected.width}x${expected.height} pixels`;
|
|
1481
|
+
if (rects.length > 0)
|
|
1482
|
+
throw unmasked(`${mismatch}, so its secure fields cannot be placed`);
|
|
1483
|
+
throw new EngineError('ENGINE_FAILURE', mismatch, { retryable: false });
|
|
1362
1484
|
}
|
|
1363
1485
|
return { pixels: { data: encodePng(image), mediaType: 'image/png', width: image.width, height: image.height, scale }, masked: rects.length };
|
|
1364
1486
|
}
|
|
@@ -1384,7 +1506,7 @@ export class DesktopSurface {
|
|
|
1384
1506
|
if (call.timeoutMs <= 0)
|
|
1385
1507
|
throw new EngineError('OPERATION_TIMEOUT', `${call.label}: the operation's budget ran out before it was sent`, { retryable: false, cause: NOT_SENT });
|
|
1386
1508
|
try {
|
|
1387
|
-
return await cli.run({ args, input: call.input, signal: call.signal, timeoutMs: call.timeoutMs, label: call.label, ...(call.stdin === undefined ? {} : { stdin: call.stdin }) });
|
|
1509
|
+
return await cli.run({ args, input: call.input, signal: call.signal, timeoutMs: call.timeoutMs, label: call.label, ...(call.stdin === undefined ? {} : { stdin: call.stdin }), ...(call.tmpdir === undefined ? {} : { tmpdir: call.tmpdir }) });
|
|
1388
1510
|
}
|
|
1389
1511
|
catch (cause) {
|
|
1390
1512
|
if (call.raw === true && cause instanceof AgentDesktopFailure)
|
|
@@ -1511,8 +1633,10 @@ async function sweepStateDirectories() {
|
|
|
1511
1633
|
if (!(await ownDirectory(directory)))
|
|
1512
1634
|
continue;
|
|
1513
1635
|
const ownerText = (await readFile(path.join(directory, OWNER_FILE), 'utf8').catch(() => '')).trim();
|
|
1514
|
-
const
|
|
1515
|
-
const
|
|
1636
|
+
const [ownerId = '', ownerStarted] = ownerText.split('\n');
|
|
1637
|
+
const owner = Number(ownerId);
|
|
1638
|
+
const stale = ownerText === ABANDONED ||
|
|
1639
|
+
(Number.isInteger(owner) && owner > 0 ? !(await ownerRuns(owner, ownerStarted)) : await stat(directory).then((info) => Date.now() - info.mtimeMs > ORPHAN_AGE_MS, () => false));
|
|
1516
1640
|
if (!stale)
|
|
1517
1641
|
continue;
|
|
1518
1642
|
// A record cut short by the kill that left it names no process: the directory still goes.
|
|
@@ -1541,6 +1665,19 @@ async function sweepStateDirectories() {
|
|
|
1541
1665
|
await (settled ? rm(directory, { recursive: true, force: true }) : keepOnlyRecord(directory)).catch(() => undefined);
|
|
1542
1666
|
}
|
|
1543
1667
|
}
|
|
1668
|
+
/**
|
|
1669
|
+
* Whether the worker process an owner file names still runs: one with its id
|
|
1670
|
+
* that started when the file says. A start time that cannot be read counts
|
|
1671
|
+
* as the same process, so a directory in use is never taken.
|
|
1672
|
+
*/
|
|
1673
|
+
async function ownerRuns(pid, started) {
|
|
1674
|
+
if (!alive(pid))
|
|
1675
|
+
return false;
|
|
1676
|
+
if (started === undefined)
|
|
1677
|
+
return true;
|
|
1678
|
+
const now = await startedAt(pid, AbortSignal.timeout(5_000));
|
|
1679
|
+
return now === undefined || now === started;
|
|
1680
|
+
}
|
|
1544
1681
|
/** Whether `directory` is a real directory (not a symlink) that belongs to the user this process runs as. */
|
|
1545
1682
|
async function ownDirectory(directory) {
|
|
1546
1683
|
const info = await lstat(directory).catch(() => undefined);
|
|
@@ -1585,11 +1722,25 @@ async function killed(pids, until, signal) {
|
|
|
1585
1722
|
/**
|
|
1586
1723
|
* Whether macOS reports the console session's screen locked (ioreg's
|
|
1587
1724
|
* `CGSSessionScreenIsLocked`); undefined when that cannot be told, off macOS
|
|
1588
|
-
* or when ioreg fails.
|
|
1725
|
+
* or when ioreg fails. The question runs within the operation's budget: a
|
|
1726
|
+
* cancellation or a budget that ran out ends the operation, never passes as
|
|
1727
|
+
* an unknown answer that lets a read go on as if unlocked.
|
|
1589
1728
|
*/
|
|
1590
|
-
async function screenLocked(signal) {
|
|
1591
|
-
|
|
1592
|
-
|
|
1729
|
+
async function screenLocked(signal, timeoutMs) {
|
|
1730
|
+
if (timeoutMs <= 0)
|
|
1731
|
+
throw new EngineError('OPERATION_TIMEOUT', "asking macOS whether the screen is locked: the operation's budget ran out", { retryable: false });
|
|
1732
|
+
let stdout;
|
|
1733
|
+
try {
|
|
1734
|
+
({ stdout } = await execFileAsync('ioreg', ['-n', 'Root', '-d1', '-a'], { signal, timeout: timeoutMs, maxBuffer: 16 * 1024 * 1024 }));
|
|
1735
|
+
}
|
|
1736
|
+
catch (cause) {
|
|
1737
|
+
if (signal.aborted)
|
|
1738
|
+
throw new EngineError('CANCELLED', 'asking macOS whether the screen is locked was cancelled', { retryable: false, cause });
|
|
1739
|
+
if (cause.killed === true)
|
|
1740
|
+
throw new EngineError('OPERATION_TIMEOUT', 'asking macOS whether the screen is locked ran out of time', { retryable: false, cause });
|
|
1741
|
+
return undefined;
|
|
1742
|
+
}
|
|
1743
|
+
const match = /<key>CGSSessionScreenIsLocked<\/key>\s*<(true|false)\/>/.exec(stdout);
|
|
1593
1744
|
return match === null ? undefined : match[1] === 'true';
|
|
1594
1745
|
}
|
|
1595
1746
|
/**
|
|
@@ -1598,7 +1749,8 @@ async function screenLocked(signal) {
|
|
|
1598
1749
|
* running or `ps` failed.
|
|
1599
1750
|
*/
|
|
1600
1751
|
async function startedAt(pid, signal) {
|
|
1601
|
-
|
|
1752
|
+
// `lstart` is local time: every reader must print it in one zone, or a run with another TZ takes a live process for a later one.
|
|
1753
|
+
const { stdout } = await execFileAsync('ps', ['-o', 'lstart=', '-p', String(pid)], { signal, env: { ...process.env, LC_ALL: 'C', TZ: 'UTC' } }).catch(() => ({ stdout: '' }));
|
|
1602
1754
|
const text = stdout.trim();
|
|
1603
1755
|
return text === '' ? undefined : text;
|
|
1604
1756
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e2e-dev/desktop",
|
|
3
|
-
"version": "0.0.1
|
|
3
|
+
"version": "0.0.1",
|
|
4
4
|
"description": "Desktop engine for e2e agentic end-to-end testing, built on agent-desktop: native apps, a prototype tested on macOS",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"e2e",
|
|
@@ -46,8 +46,8 @@
|
|
|
46
46
|
"@napi-rs/image": "1.16.0"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
49
|
-
"@e2e-dev/core": "0.25.0
|
|
50
|
-
"e2e": "0.25.0
|
|
49
|
+
"@e2e-dev/core": ">=0.25.0 <1",
|
|
50
|
+
"e2e": ">=0.25.0 <1"
|
|
51
51
|
},
|
|
52
52
|
"peerDependenciesMeta": {
|
|
53
53
|
"e2e": {
|
|
@@ -55,9 +55,9 @@
|
|
|
55
55
|
}
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@e2e-dev/core": "0.25.0
|
|
58
|
+
"@e2e-dev/core": "0.25.0",
|
|
59
59
|
"@types/node": "26.6.6",
|
|
60
|
-
"e2e": "0.25.0
|
|
60
|
+
"e2e": "0.25.0",
|
|
61
61
|
"typescript": "7.0.2",
|
|
62
62
|
"vitest": "5.1.1"
|
|
63
63
|
},
|