mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
fix(computer-use): bound wait reads by the deadline and wait out missing Linux/Windows windows
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ ORCA computer wait --app <app> --window-title "Save As" --json
|
||||
|
||||
- `--text <text>`: an element line contains the text (case-insensitive, like `--query`).
|
||||
- `--enabled <text>`: 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 <text>`: 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 <text>`: 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.
|
||||
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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<T> = { call: () => Promise<RuntimeRpcSuccess<T>>; check: (result: T) => string | null }
|
||||
type Poll<T> = {
|
||||
call: (timeoutMs: number) => Promise<RuntimeRpcSuccess<T>>
|
||||
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<ComputerSnapshotResult>('computer.getAppState', {
|
||||
...params,
|
||||
noScreenshot: true
|
||||
}),
|
||||
call: (readTimeoutMs) =>
|
||||
client.call<ComputerSnapshotResult>(
|
||||
'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<RuntimeRpcSuccess<ComputerWaitWindowsResult>> {
|
||||
const { response, elapsedMs } = await pollUntil(condition, timeoutMs, {
|
||||
call: () => client.call<ComputerListWindowsResult>('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<ComputerListWindowsResult>(
|
||||
'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<T>(
|
||||
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<T>(
|
||||
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) {
|
||||
|
||||
@@ -27,7 +27,7 @@ const COMMAND_SCOPED_FLAG_HELP: Record<string, Record<string, string>> = {
|
||||
enabled:
|
||||
'--enabled <text> Wait until elements containing this text exist and none is disabled',
|
||||
'window-title':
|
||||
"--window-title <text> Wait until one of the app's windows has this in its title",
|
||||
"--window-title <text> Wait until one of the app's windows has this in its title",
|
||||
timeout: `--timeout <ms> How long to wait before failing with wait_timeout (default ${COMPUTER_WAIT_DEFAULT_TIMEOUT_MS})`
|
||||
},
|
||||
'reference find': {
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -1077,7 +1077,7 @@ const ORCA_COMPUTER_USE_MARKDOWN = [
|
||||
"",
|
||||
"- `--text <text>`: an element line contains the text (case-insensitive, like `--query`).",
|
||||
"- `--enabled <text>`: 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 <text>`: 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 <text>`: 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.",
|
||||
"",
|
||||
|
||||
@@ -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
|
||||
)
|
||||
) {
|
||||
|
||||
Reference in New Issue
Block a user