From a02a19fcae394d513a0b14ddec0ae2d52388cfb3 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Thu, 12 Nov 2026 06:14:15 +0000 Subject: [PATCH] fix(computer-use): bound wait reads by the deadline and wait out missing Linux/Windows windows --- docs/site/content/docs/cli/computer-use.mdx | 2 +- skill-guides/orca-computer-use.md | 2 +- src/cli/computer/computer-wait.test.ts | 16 +++--- src/cli/computer/computer-wait.ts | 50 +++++++++++++------ src/cli/discovery/command-scoped-flag-help.ts | 2 +- src/cli/discovery/root-help-text-primary.ts | 2 +- src/cli/skills/bundled-skill-guides.ts | 2 +- .../desktop-script-provider-bridge.ts | 2 +- 8 files changed, 52 insertions(+), 26 deletions(-) diff --git a/docs/site/content/docs/cli/computer-use.mdx b/docs/site/content/docs/cli/computer-use.mdx index b8c8f00a211..4f6b8f31ae7 100644 --- a/docs/site/content/docs/cli/computer-use.mdx +++ b/docs/site/content/docs/cli/computer-use.mdx @@ -49,7 +49,7 @@ orca computer wait --app com.apple.TextEdit --enabled "Continue" --json orca computer wait --app com.apple.TextEdit --window-title "Save As" --json ``` -`--text` matches element lines the same way as `get-app-state --query`. `--enabled` holds once elements containing the text exist and none of them is disabled. `--window-title` checks the titles of all the app's windows. `wait` only reads the app and keeps checking while the app or window doesn't exist yet. `--enabled` needs a runtime whose provider marks disabled elements (`supports.observation.disabledState` in `capabilities`). +`--text` matches element lines the same way as `get-app-state --query`. `--enabled` holds once elements containing the text exist and none of them is disabled. `--window-title` checks the window titles `list-windows` reports (on Windows, only the main window's). `wait` only reads the app and keeps checking while the app or window doesn't exist yet. `--enabled` needs a runtime whose provider marks disabled elements (`supports.observation.disabledState` in `capabilities`). ## Multi-window apps diff --git a/skill-guides/orca-computer-use.md b/skill-guides/orca-computer-use.md index b7aacdc6fb4..c500bf72a44 100644 --- a/skill-guides/orca-computer-use.md +++ b/skill-guides/orca-computer-use.md @@ -58,7 +58,7 @@ ORCA computer wait --app --window-title "Save As" --json - `--text `: an element line contains the text (case-insensitive, like `--query`). - `--enabled `: an element contains the text and none of the elements containing it is disabled. Use text only the intended control shows; a label that repeats the button text also has to be enabled. -- `--window-title `: one of the app's windows has the text in its title. Then target that window with its `--window-id` or `--window-index`. +- `--window-title `: one of the app's windows has the text in its title, as `list-windows` reports them. Then target that window with its `--window-id` or `--window-index`. On Windows, `list-windows` reports only the app's main window, so wait for a dialog with `--text` instead. `wait` only reads the app; it never acts, raises or restores a window. The app or window may be missing at first; `wait` keeps checking until it appears. Element indexes in the output come from a fresh tree, so you can act on them next. diff --git a/src/cli/computer/computer-wait.test.ts b/src/cli/computer/computer-wait.test.ts index 516bcf99af0..96ede0e5102 100644 --- a/src/cli/computer/computer-wait.test.ts +++ b/src/cli/computer/computer-wait.test.ts @@ -48,11 +48,11 @@ describe('orca computer wait', () => { await run('--text', 'saved') expect(callMock).toHaveBeenCalledTimes(3) - expect(callMock).toHaveBeenLastCalledWith('computer.getAppState', { - session: 'manual', - app: 'TextEdit', - noScreenshot: true - }) + expect(callMock).toHaveBeenLastCalledWith( + 'computer.getAppState', + { session: 'manual', app: 'TextEdit', noScreenshot: true }, + expect.anything() + ) const output = JSON.parse(vi.mocked(console.log).mock.calls[0][0]) expect(output.result.wait).toMatchObject({ kind: 'text', value: 'saved' }) expect(output.result.snapshot.treeText).toContain('1 text All changes saved') @@ -106,7 +106,11 @@ describe('orca computer wait', () => { await run('--window-title', 'save as') - expect(callMock).toHaveBeenLastCalledWith('computer.listWindows', { app: 'TextEdit' }) + expect(callMock).toHaveBeenLastCalledWith( + 'computer.listWindows', + { app: 'TextEdit' }, + expect.anything() + ) const output = JSON.parse(vi.mocked(console.log).mock.calls[0][0]) expect(output.result.windows.map((window: { title: string }) => window.title)).toEqual([ 'Save As' diff --git a/src/cli/computer/computer-wait.ts b/src/cli/computer/computer-wait.ts index a9fd9f239a0..aa5eaa7bc13 100644 --- a/src/cli/computer/computer-wait.ts +++ b/src/cli/computer/computer-wait.ts @@ -30,10 +30,15 @@ export type ComputerWaitSnapshotResult = ComputerSnapshotResult & { wait: Comput export type ComputerWaitWindowsResult = ComputerListWindowsResult & { wait: ComputerWaitSummary } const POLL_INTERVAL_MS = 250 +// Why: a read started near the deadline still gets long enough to answer for a large window. +const MIN_READ_TIMEOUT_MS = 2_000 // Why: the app or window may not exist yet; waiting for it to appear is part of the wait. const NOT_YET_CODES = new Set(['app_not_found', 'window_not_found', 'window_stale']) -type Poll = { call: () => Promise>; check: (result: T) => string | null } +type Poll = { + call: (timeoutMs: number) => Promise> + check: (result: T) => string | null +} // Polls the same reads every provider and runtime version serves, so the wait needs no host support. export async function waitForComputerSnapshot( @@ -46,11 +51,12 @@ export async function waitForComputerSnapshot( await assertDisabledStateSupported(client) } const { response, elapsedMs } = await pollUntil(condition, timeoutMs, { - call: () => - client.call('computer.getAppState', { - ...params, - noScreenshot: true - }), + call: (readTimeoutMs) => + client.call( + 'computer.getAppState', + { ...params, noScreenshot: true }, + { timeoutMs: readTimeoutMs } + ), check: (result) => snapshotMiss(condition, result.snapshot.treeText) }) const { snapshot } = response.result @@ -74,11 +80,13 @@ export async function waitForComputerWindowTitle( timeoutMs: number ): Promise> { const { response, elapsedMs } = await pollUntil(condition, timeoutMs, { - call: () => client.call('computer.listWindows', { app }), - check: (result) => - matchingWindows(result.windows, condition.value).length > 0 - ? null - : `its windows are ${result.windows.map((window) => `"${window.title}"`).join(', ') || 'none'}` + call: (readTimeoutMs) => + client.call( + 'computer.listWindows', + { app }, + { timeoutMs: readTimeoutMs } + ), + check: (result) => windowTitleMiss(result.windows, condition.value) }) return { ...response, @@ -111,6 +119,16 @@ function snapshotMiss(condition: ComputerTreeWaitCondition, treeText: string): s .join(' | ')}` } +function windowTitleMiss(windows: ComputerWindowListWindow[], title: string): string | null { + if (matchingWindows(windows, title).length > 0) { + return null + } + if (windows.length > 0 && windows.every((window) => window.title === '')) { + return 'every window title is empty (on macOS, window titles need the Screen Recording permission)' + } + return `its windows are ${windows.map((window) => `"${window.title}"`).join(', ') || 'none'}` +} + function matchingWindows( windows: ComputerWindowListWindow[], title: string @@ -139,7 +157,8 @@ async function pollUntil( for (;;) { let miss: string try { - const response = await poll.call() + const remainingMs = timeoutMs - (Date.now() - startedAt) + const response = await poll.call(Math.max(remainingMs, MIN_READ_TIMEOUT_MS)) const result = poll.check(response.result) if (result === null) { return { response, elapsedMs: Date.now() - startedAt } @@ -147,10 +166,13 @@ async function pollUntil( miss = result } catch (error) { const code = cliErrorCode(error) - if (!code || !NOT_YET_CODES.has(code)) { + if (code === 'runtime_timeout' && Date.now() - startedAt >= timeoutMs) { + miss = 'the last read did not answer before the deadline' + } else if (code && NOT_YET_CODES.has(code)) { + miss = `${code}: ${error instanceof Error ? error.message : String(error)}` + } else { throw error } - miss = `${code}: ${error instanceof Error ? error.message : String(error)}` } const remainingMs = timeoutMs - (Date.now() - startedAt) if (remainingMs <= 0) { diff --git a/src/cli/discovery/command-scoped-flag-help.ts b/src/cli/discovery/command-scoped-flag-help.ts index 1152a065a74..b23764dd9f4 100644 --- a/src/cli/discovery/command-scoped-flag-help.ts +++ b/src/cli/discovery/command-scoped-flag-help.ts @@ -27,7 +27,7 @@ const COMMAND_SCOPED_FLAG_HELP: Record> = { enabled: '--enabled Wait until elements containing this text exist and none is disabled', 'window-title': - "--window-title Wait until one of the app's windows has this in its title", + "--window-title Wait until one of the app's windows has this in its title", timeout: `--timeout How long to wait before failing with wait_timeout (default ${COMPUTER_WAIT_DEFAULT_TIMEOUT_MS})` }, 'reference find': { diff --git a/src/cli/discovery/root-help-text-primary.ts b/src/cli/discovery/root-help-text-primary.ts index a406ffa2f8a..d073961bd86 100644 --- a/src/cli/discovery/root-help-text-primary.ts +++ b/src/cli/discovery/root-help-text-primary.ts @@ -186,7 +186,7 @@ export const ROOT_HELP_TEXT_PRIMARY = [ ' computer list-apps List running apps available to computer-use', ' computer list-windows List visible windows for a target app', ' computer get-app-state Capture a compact accessibility snapshot of an app', - ' computer wait Wait until an app shows text, enables a control, or opens a window', + ' computer wait Wait until an app shows text, enables a control, or opens a window', ' computer click Click an app element or window coordinate', ' computer perform-secondary-action Run an advertised accessibility action', ' computer scroll Scroll an app element', diff --git a/src/cli/skills/bundled-skill-guides.ts b/src/cli/skills/bundled-skill-guides.ts index fc4a2d3be9b..e5d991da436 100644 --- a/src/cli/skills/bundled-skill-guides.ts +++ b/src/cli/skills/bundled-skill-guides.ts @@ -1077,7 +1077,7 @@ const ORCA_COMPUTER_USE_MARKDOWN = [ "", "- `--text `: an element line contains the text (case-insensitive, like `--query`).", "- `--enabled `: an element contains the text and none of the elements containing it is disabled. Use text only the intended control shows; a label that repeats the button text also has to be enabled.", - "- `--window-title `: one of the app's windows has the text in its title. Then target that window with its `--window-id` or `--window-index`.", + "- `--window-title `: one of the app's windows has the text in its title, as `list-windows` reports them. Then target that window with its `--window-id` or `--window-index`. On Windows, `list-windows` reports only the app's main window, so wait for a dialog with `--text` instead.", "", "`wait` only reads the app; it never acts, raises or restores a window. The app or window may be missing at first; `wait` keeps checking until it appears. Element indexes in the output come from a fresh tree, so you can act on them next.", "", diff --git a/src/main/computer/desktop-script/desktop-script-provider-bridge.ts b/src/main/computer/desktop-script/desktop-script-provider-bridge.ts index 1e75632529b..75cc71d6938 100644 --- a/src/main/computer/desktop-script/desktop-script-provider-bridge.ts +++ b/src/main/computer/desktop-script/desktop-script-provider-bridge.ts @@ -220,7 +220,7 @@ export function mapBridgeError(message: string): RuntimeClientError { return new RuntimeClientError('screenshot_failed', text) } if ( - /window_not_found|No top-level(?: AT-SPI| UI Automation)? window|has no (?:on-screen |accessibility )?window|could not match accessibility window|unknown window(?:_index| id)?/i.test( + /window_not_found|windowNotFound|No top-level(?: AT-SPI| UI Automation)? window|has no (?:on-screen |accessibility )?window|could not match accessibility window|unknown window(?:_index| id)?/i.test( text ) ) {