diff --git a/.github/workflows/computer-e2e.yml b/.github/workflows/computer-e2e.yml index f26cfe41eec..ad98347c1b9 100644 --- a/.github/workflows/computer-e2e.yml +++ b/.github/workflows/computer-e2e.yml @@ -101,7 +101,6 @@ jobs: src/cli/output/command-result-output.test.ts src/cli/computer/computer-state-formatting.test.ts src/cli/index.test.ts - src/cli/runtime/envelope-schema.test.ts src/shared/remote-runtime/remote-runtime-client.test.ts - run: pnpm verify:computer-native - run: pnpm build:cli diff --git a/.github/workflows/node-server-tests.yml b/.github/workflows/node-server-tests.yml index bf9d65525a1..a5c5dc91b43 100644 --- a/.github/workflows/node-server-tests.yml +++ b/.github/workflows/node-server-tests.yml @@ -227,7 +227,15 @@ jobs: with: arches: ${{ (inputs.build_template && matrix.os == 'windows-2022') && 'x64 arm64' || (runner.arch == 'ARM64' && 'arm64' || 'x64') }} - uses: actions/download-artifact@v8 + id: mobile-web-artifact if: needs.changes.outputs.qualification != 'false' + continue-on-error: true + with: + name: orcad-mobile-web-bundle + path: out/mobile-web + - name: Retry mobile web artifact download + if: steps.mobile-web-artifact.outcome == 'failure' + uses: actions/download-artifact@v8 with: name: orcad-mobile-web-bundle path: out/mobile-web diff --git a/.github/workflows/release-cut.yml b/.github/workflows/release-cut.yml index 7ad27fefb3c..31b23e40ac3 100644 --- a/.github/workflows/release-cut.yml +++ b/.github/workflows/release-cut.yml @@ -1121,7 +1121,9 @@ jobs: eb_cache_path: | ~\AppData\Local\electron\Cache ~\AppData\Local\electron-builder\Cache - - os: ubuntu-latest + # Why: ubuntu-latest moved to the amd64v3 Ubuntu 26.04 image, whose GCC + # defaults to -march=x86-64-v3 and ships AVX in node-pty (#41637). + - os: ubuntu-24.04 platform: linux-x64 release_command: node "$(if [ -f config/scripts/native-build/node-addons/ensure-native-runtime.mjs ]; then printf %s config/scripts/native-build/node-addons/ensure-native-runtime.mjs; else printf %s config/scripts/ensure-native-runtime.mjs; fi)" --runtime=electron && pnpm exec electron-builder --config config/electron-builder.config.cjs --linux AppImage deb rpm --x64 --publish always -c.publish.releaseType=draft unpacked_dir: dist/linux-unpacked diff --git a/.husky/pre-commit b/.husky/pre-commit index 26fc3643d41..980f430682f 100644 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,2 +1,2 @@ #!/usr/bin/env sh -pnpm exec lint-staged +lint-staged diff --git a/config/benchmarks/computer-use/README.md b/config/benchmarks/computer-use/README.md index 52d324d192d..619ed6826b8 100644 --- a/config/benchmarks/computer-use/README.md +++ b/config/benchmarks/computer-use/README.md @@ -64,8 +64,7 @@ click. Interference probes remain separate from task latency comparisons. Explicit-target typing is a native capability probe. Orca's public CLI/RPC accepts an element target for text (`type-text --element-index`) on providers that report -`actions.typeTextElement` (macOS and Linux); Windows types only at the focused -field. Click-then-type tasks send no element with the text, like `type-text` +`actions.typeTextElement` (macOS, Linux and Windows). Click-then-type tasks send no element with the text, like `type-text` without the index, although this benchmark still bypasses public transports. The Linux Electron job also records restoration stages in `focus-probe/results.json`. diff --git a/config/build-plugins/sandboxed-script-bundles.ts b/config/build-plugins/sandboxed-script-bundles.ts index 9c297cf96f1..15741dde22e 100644 --- a/config/build-plugins/sandboxed-script-bundles.ts +++ b/config/build-plugins/sandboxed-script-bundles.ts @@ -18,8 +18,8 @@ const GUEST_PRELOADS = [ { entry: 'src/preload/browser-extension-preload.ts', fileName: 'browser-extension-preload.js' }, { entry: 'src/preload/browser-guest.ts', fileName: 'browser-guest-preload.js' }, { - entry: 'src/preload/browser-passkey-request.ts', - fileName: 'browser-passkey-request-preload.js' + entry: 'src/preload/browser-system-dialogs.ts', + fileName: 'browser-system-dialogs-preload.js' }, { entry: 'src/preload/doc-preview-link.ts', fileName: 'doc-preview-link-preload.js' }, { entry: 'src/preload/plugin-frame-guest.ts', fileName: 'plugin-frame-guest-preload.js' } diff --git a/config/knip.json b/config/knip.json index d00eeaddb1b..b1d8191b540 100644 --- a/config/knip.json +++ b/config/knip.json @@ -4,7 +4,7 @@ "src/main/index.ts", "src/preload/index.ts", "src/preload/browser-guest.ts", - "src/preload/browser-passkey-request.ts", + "src/preload/browser-system-dialogs.ts", "src/main/daemon/daemon-entry.ts", "src/main/plugins/workers/plugin-host-entry.ts", "src/main/computer/computer-sidecar.ts", diff --git a/config/patches/lint-staged@16.4.0.patch b/config/patches/lint-staged@16.4.0.patch index cd5bcf57740..e0e933f1de3 100644 --- a/config/patches/lint-staged@16.4.0.patch +++ b/config/patches/lint-staged@16.4.0.patch @@ -1,5 +1,5 @@ diff --git a/lib/gitWorkflow.js b/lib/gitWorkflow.js -index 3a32d6a9cd65c7ecb6b3cbccfce5a15c96530435..363513128bf7302eab73ea775102bb8a633a3b8b 100644 +index 3a32d6a9cd65c7ecb6b3cbccfce5a15c96530435..f91b1931224ce59df5cf9880248d4fad93e74f03 100644 --- a/lib/gitWorkflow.js +++ b/lib/gitWorkflow.js @@ -50,6 +50,9 @@ export const STASH = 'lint-staged automatic backup' @@ -39,7 +39,66 @@ index 3a32d6a9cd65c7ecb6b3cbccfce5a15c96530435..363513128bf7302eab73ea775102bb8a /** Print stash list with short hash and subject */ const stashes = await this.execGit(['stash', 'list', '--format="%h %s"', '-z']) .then(parseGitZOutput) -@@ -270,11 +286,14 @@ export class GitWorkflow { +@@ -142,11 +158,44 @@ export class GitWorkflow { + */ + async getDeletedFiles() { + debugLog('Getting deleted files...') +- const lsFiles = await this.execGit(['ls-files', '--deleted']) +- const deletedFiles = lsFiles +- .split('\n') +- .filter(Boolean) +- .map((file) => path.resolve(this.topLevelDir, file)) ++ // Orca patch: `ls-files --deleted` lstat()s every tracked file one at a time, which takes tens ++ // of seconds when the stat cache is cold. `diff-files` reads the index with parallel lstat ++ // (preload) instead. Like `ls-files --deleted`, it must not trust an fsmonitor (a stale one ++ // would hide a deletion), so the monitor is turned off for this read-only query. It does ++ // trust the assume-unchanged bit, so those entries (lowercase `ls-files -v` tag; `s` is also ++ // skip-worktree, which `--deleted` skips) are added as candidates. A candidate counts only ++ // if lstat() fails, which is the `--deleted` rule. ++ const [diffOutput, taggedOutput] = await Promise.all([ ++ this.execGit([ ++ '-c', ++ 'core.fsmonitor=false', ++ 'diff-files', ++ '--name-only', ++ '--diff-filter=D', ++ '--no-renames', ++ '--no-relative', ++ '--ignore-submodules=none', ++ '-z', ++ ]), ++ this.execGit(['ls-files', '-v', '-z']), ++ ]) ++ const assumeUnchanged = parseGitZOutput(taggedOutput).flatMap((line) => ++ /^[a-rt-z] /.test(line) ? [line.slice(2)] : [] ++ ) ++ const candidates = new Set( ++ [...parseGitZOutput(diffOutput), ...assumeUnchanged].map((file) => ++ path.resolve(this.topLevelDir, file) ++ ) ++ ) ++ const isGone = await Promise.all( ++ [...candidates].map((file) => ++ fs.lstat(file).then( ++ () => false, ++ () => true ++ ) ++ ) ++ ) ++ const deletedFiles = [...candidates].filter((_, i) => isGone[i]) + debugLog('Found deleted files:', deletedFiles) + return deletedFiles + } +@@ -194,7 +243,7 @@ export class GitWorkflow { + */ + async getUnstagedFiles({ onlyPartial = false } = {}) { + debugLog('Getting partially staged files...') +- const status = await this.execGit(['status', '-z']) ++ const status = await this.execGit(['status', '-z', '--untracked-files=no']) + /** + * See https://git-scm.com/docs/git-status#_short_format + * Entries returned in machine format are separated by a NUL character. +@@ -270,11 +319,14 @@ export class GitWorkflow { } else { /** Save stash of all changes, keeping all files as-is */ const stashHash = await this.execGit(['stash', 'create']) @@ -56,7 +115,7 @@ index 3a32d6a9cd65c7ecb6b3cbccfce5a15c96530435..363513128bf7302eab73ea775102bb8a debugLog(task.title) } } catch (error) { -@@ -425,7 +444,11 @@ export class GitWorkflow { +@@ -425,7 +477,11 @@ export class GitWorkflow { async cleanup(ctx) { try { debugLog('Dropping backup stash...') @@ -111,6 +170,22 @@ index 993f8d81cac48132b5ae2ebdc8ca452c7217caad..2dc2291bc82451eacc5babfc8ce40805 > git stash list --format="%h %s" ${hash} On main: lint-staged automatic backup +diff --git a/lib/searchConfigs.js b/lib/searchConfigs.js +index 77ffe8f2423bb24ed80e2e23b57847a69c137195..822ff82c189cf712029e5e5870e5b3d9266f74a2 100644 +--- a/lib/searchConfigs.js ++++ b/lib/searchConfigs.js +@@ -36,8 +36,9 @@ const listConfigFilesFromGit = async ({ cwd, topLevelDir }) => + [ + ...EXEC_GIT, + '--cached', // show all tracked files +- '--others', // show untracked files +- '--exclude-standard', // apply standard git exclusions (.gitignore, etc.) ++ // Orca patch: no `--others`. Listing untracked files walks the whole worktree, which takes ++ // seconds on a cold stat cache. Untracked configs in `cwd` and above are still found by ++ // `listConfigFilesFromFs`; a nested config must be tracked or staged to apply. + '--', + ...CONFIG_PATHSPEC, + ], diff --git a/lib/state.js b/lib/state.js index da30e6f639d31307ebf503691aa991bb93948f8a..4a38799fa7773a8e8629c7c3a6b72287e6b22f4b 100644 --- a/lib/state.js diff --git a/config/react-compiler-bailout-baseline.txt b/config/react-compiler-bailout-baseline.txt index 62b0b0c36d6..e2f3ac9e4a3 100644 --- a/config/react-compiler-bailout-baseline.txt +++ b/config/react-compiler-bailout-baseline.txt @@ -65,7 +65,6 @@ 2 src/renderer/src/components/browser-pane/passwords/BrowserPasswordFillDropdown.tsx 1 src/renderer/src/components/browser-pane/stream-remote/use-remote-browser-page-input.ts 4 src/renderer/src/components/browser-pane/stream-remote/use-remote-browser-page-lifecycle.ts -1 src/renderer/src/components/browser-pane/stream-remote/use-remote-browser-page-navigation.ts 2 src/renderer/src/components/browser-pane/stream-remote/use-remote-browser-page-wheel.ts 3 src/renderer/src/components/browser-pane/workspace-doc/doc-preview-directory-access.tsx 1 src/renderer/src/components/browser-pane/workspace-doc/doc-preview-overflow-menu.tsx @@ -402,7 +401,6 @@ 1 src/renderer/src/components/settings/browser/extensions/BrowserExtensionsSection.tsx 1 src/renderer/src/components/settings/browser/extensions/BrowserExtensionsWebStoreInstall.tsx 1 src/renderer/src/components/settings/browser/profiles/BrowserNewProfileDialog.tsx -1 src/renderer/src/components/settings/chat/ChatNamingSetting.tsx 1 src/renderer/src/components/settings/cli/install/CliSection.tsx 3 src/renderer/src/components/settings/cli/install/WslCliRegistration.tsx 2 src/renderer/src/components/settings/cli/install/use-cli-registration-actions.ts @@ -510,8 +508,6 @@ 1 src/renderer/src/components/sidebar/workspace-board/viewport/WorkspaceKanbanLaneCardList.tsx 1 src/renderer/src/components/sidebar/workspace-board/viewport/WorkspaceKanbanLaneGrid.tsx 3 src/renderer/src/components/sidebar/workspace-board/viewport/use-workspace-kanban-column-resize.ts -1 src/renderer/src/components/sidebar/workspace-card/activity/WorktreeCardAgents.tsx -1 src/renderer/src/components/sidebar/workspace-card/activity/useWorktreeAgentRows.ts 3 src/renderer/src/components/sidebar/workspace-card/activity/worktree-card-compact-agent-row.tsx 2 src/renderer/src/components/sidebar/workspace-card/activity/worktree-card-compact-agents.tsx 1 src/renderer/src/components/sidebar/workspace-card/identity/AutoRenameFailedDialog.tsx @@ -704,6 +700,7 @@ 6 src/renderer/src/hooks/useVirtualizedScrollAnchor.ts 1 src/renderer/src/lib/agent-session/launch/inventory/host-chat-program-acquisition.ts 1 src/renderer/src/lib/files/search/quick-open-file-history.ts +1 src/renderer/src/lib/list-interaction/use-windowed-command-virtualizer.ts 2 src/renderer/src/runtime/agent-orchestration/worker-auto-close.ts 1 src/renderer/src/runtime/native-chat/naming/structured-chat-name-observation.ts 1 src/renderer/src/store/plugins/appearance/plugin-icon-themes.ts diff --git a/config/react-compiler-uncompiled-files.txt b/config/react-compiler-uncompiled-files.txt index c1570bdeeab..fcb82cc9f86 100644 --- a/config/react-compiler-uncompiled-files.txt +++ b/config/react-compiler-uncompiled-files.txt @@ -33,6 +33,8 @@ src/renderer/src/components/native-chat/tools/runs/use-native-chat-tool-run-wind src/renderer/src/components/right-sidebar/file-explorer/panel/FileExplorerVirtualRows.tsx src/renderer/src/components/right-sidebar/file-explorer/tree/FileExplorerFilesTreePane.tsx src/renderer/src/components/sidebar/worktree-list/viewport/VirtualizedWorktreeViewport.tsx +src/renderer/src/components/automations/authoring/WorkspaceCombobox.tsx +src/renderer/src/components/skills/installation/SkillInstallWorkspaceCombobox.tsx # The workbench subtree relies on the shell re-rendering on every app render. src/renderer/src/app-shell/AppWorkspaceShell.tsx diff --git a/config/reliability-gates.jsonc b/config/reliability-gates.jsonc index 0e009d6c046..33e9b4f22e9 100644 --- a/config/reliability-gates.jsonc +++ b/config/reliability-gates.jsonc @@ -3608,7 +3608,7 @@ "providers": ["local", "daemon", "ssh"], "coveredPlatforms": ["linux"], "coveredProviders": [], - "coverageNotes": "Audit4362 intentionally deletes the imported stale-waiter retry and manually assembled OSC9999 mobile-republish cases, plus the mocked missing-worktree cleanup suite (4 authored / 5 finite cases in three suites). Lost alarms include repeated unread worker_done pointers, working-to-waiting mobile status publication, missing-worktree filtering, and scoped daemon shutdown with unrelated duplicate identities. No replacement coverage or new platform proof is claimed. Audit3994 retires two authored callbacks imported by the shared construction test driver: split OSC 9999 carry and rendererless status ownership, plus OpenCode permission text taking priority in status and tui-idle refusal. Remaining imports and selectors keep their scope. No equivalent replacement or fresh gate execution is claimed. Audit3417 composes root-adopted3415v2/independent3416v2: intentionally removes117authored/173potential expanded registrations from12semantic sources, deleting4whole conventional suites and3registration modules and retaining4authored/5expanded cases, without equivalent replacement or fresh execution. The whole main-window attach-services suite is removed: runtime/window attach, notifier wiring, reload/teardown and permission-handler mock orchestration alarms are intentionally lost. Prune its exact deleted command selectors, testFiles entries and notifier assertionRef where present; complete prior selectors/ref are archived. Unrelated dated receipts remain exact and retain their original scope; no fresh reduced-selector, native-window or remote execution is claimed. Audit3403 composes root-adopted3393/3394,3400/3401 and3398/3399: intentionally retires167 authored registrations/196 potential expanded cases across93 whole suites without equivalent replacement or fresh execution. Retained65 registrations/79 potential cases include one conditional missing-input failure sentinel; potential counts are not executions. Historical notes and complete dated receipts retain their exact original scopes. The retained runtime registration driver loses three complete imported fragments: changed-working-label title facts and suppressed-frame tracked snapshots; refusing SSH GitLab MR-base lookup before local Git/glab calls; and missing folder-workspace path creation/update refusal before spawn/store mutation. These alarms are intentionally lost. Other imported fragments and separately rooted dated receipts are unchanged; they are not equivalent replacement coverage. Audit2271: Linux coverage is limited to the unchanged serve-signal-exit-diagnostic supervisor selector and the late-handoff-after-child-exit assertion recorded below. It does not cover desktop promotion, renderer continuity, persistent PTYs or native updater behavior. No current local, daemon or SSH provider execution is established; coveredProviders is empty. Historical macOS/provider scope remains only in preserved prior accounts and archived receipts. Audit2252 removes the entire handassembled serve promotion/renderer hydration suite. Retired alarms: queued and repeated activation coalescing, duplicate serve suppression, unavailable-provider diagnostic, and zero resume/replacement accounting on independently supplied renderer state. The fixture never joined actual daemon-to-renderer delivery. These exact alarms have no equivalent replacement assigned. Remaining gate selectors keep their existing narrower scope; historical dated receipts remain verbatim and do not validate this changed selector set. No fresh runtime, native/provider, live remote/mobile, cross-platform or performance result is claimed. Archived removed active entries verbatim: commands: \"pnpm exec vitest run --config config/vitest.config.ts src/renderer/src/lib/serve-desktop-promotion-session-continuity.test.ts\"\ntestFiles: \"src/renderer/src/lib/serve-desktop-promotion-session-continuity.test.ts\"\nassertionRefs: {\n \"file\": \"src/renderer/src/lib/serve-desktop-promotion-session-continuity.test.ts\",\n \"assertions\": [\n \"a forced desktop relaunch reaching the headless lock owner opens exactly one window, and only after the persistent provider settles\",\n \"a duplicate `orca serve` launch never promotes the headless owner\",\n \"the renderer promoted inside the serve process resumes zero agents while daemon panes survive, before and after those panes rebind their PTYs\",\n \"no replacement resume tab, startup command, or automatic-resume claim is created, and every surviving session record is left untouched\"\n ]\n } Historical prior account (preserved verbatim): Audit2133 current-parent preparation: Current reduced sources/selectors intentionally retire the alarms listed in knownGaps; prior dated runs and replacement claims do not validate the changed set. Historical prior account (preserved verbatim): Audit2095 current-parent preparation2126: Current reduced sources/selectors intentionally retire the alarms listed in knownGaps; prior dated runs and replacement claims do not validate the changed set. Historical prior account (preserved verbatim): Audit2090 (sources2088/2089): Current reduced suites intentionally retire the alarms listed in knownGaps; prior dated execution and replacement claims do not validate changed source bytes or selectors. Historical prior account (preserved verbatim): Audit2086 (sources2082/2083): Current reduced suites intentionally retire the alarms listed in knownGaps; prior dated execution and replacement claims do not validate changed source bytes or selectors. Historical prior account (preserved verbatim): Audit2076 (sources2073/2074): Intentionally retire these low-value alarms without replacement: src/main/runtime/orca-runtime-tests/terminal-handles-part-02.spec.ts: recovers a moved SSH pane through the tab it now sits in, not the one its lease froze; reports a plain expired orphan; does not report a pane whose only recent lease was superseded; does not report a pane whose only recent lease had its relay id recycled; picks the eligible orphan over a superseded predecessor that matches first; collects the same tabs the per-tab reader reports, in one sweep of the leases; reports no candidate when no lease names any of the worktree tabs. src/main/runtime/orca-runtime-tests/runtime-availability.spec.ts: starts unavailable with no authoritative window; reports the configured Windows terminal shell on status; reports floating workspace availability from settings on status; polls floating tabs with targeted PTY liveness and no repo/provider inventory; hydrates persisted tabs when the store cannot report repos; advertises browser screencast only when a renderer window is available; routes mobile Codex reset consumption through the account mutation coordinator; maps a definite pre-provider rejection into an authoritative current snapshot. Existing unrelated evidenceRuns retain their original scope; unchanged dated receipt objects do not validate changed source bytes or reduced selectors. No fresh gate execution or historical import-closure equivalence is claimed. Frozen current literal-import graph reaches these changed test bytes; unresolved local and computed edges remain debt, and current reachability is not historical execution or equivalent replacement evidence. Historical prior account (preserved verbatim): Audit2020: Retire CLI supervisor outcome, local-open promotion and paired-open projection alarms; launch retains only the two referenced stream-framing cases. Remaining CLI refusal/timeout cases do not replace headless-promotion coverage. Preserve both independent dated receipts at their original commands. Reduced selectors have no fresh result or equivalent replacement claim. Historical prior account (preserved verbatim): Audit1589 removes mock-only CLI forwarding, canned provisioning/catalog/error projections and private inventories without equivalent replacement coverage. Retained cases exercise actual local markdown/paste/reconciliation, stream framing/backpressure/output-drain, temporary directory cleanup and persisted handoff with injected host/provider/process boundaries. No real Jira, provisioning, remote launch, native pipe/process-tree authority or deployed provider catalog proof. Assertion references use the surviving handoff registration titles; changed-selector historical receipts are retired and unaffected receipts remain unchanged. Historical scope before this audit: Deterministic unit coverage exercises activation gating, single-instance ownership, local/remote CLI status, headless binding persistence, local daemon identity, SSH identity transfer, the promoted renderer's agent-resume accounting, and the macOS serve update handoff from staged installer through atomic bundle replacement and target-version readiness. Supervisor settlement coverage prevents a late handoff-completion failure from rearming termination after the replacement child exits. A macOS Electron journey covers headless promotion and persistent PTY identity. A disposable locally signed Electron canary exercised real ShipIt and a temporary LaunchAgent with the compiled production supervisor; full packaged Orca and Linux/Windows serve updates remain uncollected. Audit1383: src/main/runtime/orca-runtime-tests/worktree-native-chat-setup.spec.ts reduced. Lose command-list/receipt/default/background activation matrices whose provider spawn returns canned IDs. Retained test proves actual tab allocation separation, not terminal startup. No equivalent replacement coverage is claimed. Existing exact unaffected receipts and their platform/provider/budget claims remain scoped to unchanged selectors. Changed optional selectors have no new execution result. Audit1401: src/main/runtime/orca-runtime-tests/mobile-session-tabs-duplicate-pty-repair.spec.ts removed. Loses listing-on-already-repaired-data alarm; it never established runtime repair invocation. src/main/runtime/orca-runtime-tests/mobile-session-tabs-split-agent-tab.spec.ts removed. Loses actual mobile split sibling shell versus recorded-agent resume command alarms; no equivalent coverage asserted. src/main/runtime/orca-runtime-tests/worktree-native-chat-setup.spec.ts removed. Loses separate setup/startup tab result in that manually assembled path. src/main/runtime/orca-runtime.test.ts reduced. Driver registration/import of a whole-deleted selected spec is removed; directly authored cases remain unchanged. No extra authored removal credit. No equivalent replacement coverage is claimed. Existing exact unaffected receipts remain unchanged and apply only to their unchanged selectors. Changed optional selectors have no new execution result.", + "coverageNotes": "Audit4891 removes two isolated serve activation gate cases for coalescing/ready state and blocked-request cancellation/counts. Their exact alarms are intentionally lost. Retain the newly added queued foreground-over-background priority case, which exercises a new request-selection decision; it does not replace blocked/provider/native-window coverage. Historical receipts remain verbatim. Audit4362 intentionally deletes the imported stale-waiter retry and manually assembled OSC9999 mobile-republish cases, plus the mocked missing-worktree cleanup suite (4 authored / 5 finite cases in three suites). Lost alarms include repeated unread worker_done pointers, working-to-waiting mobile status publication, missing-worktree filtering, and scoped daemon shutdown with unrelated duplicate identities. No replacement coverage or new platform proof is claimed. Audit3994 retires two authored callbacks imported by the shared construction test driver: split OSC 9999 carry and rendererless status ownership, plus OpenCode permission text taking priority in status and tui-idle refusal. Remaining imports and selectors keep their scope. No equivalent replacement or fresh gate execution is claimed. Audit3417 composes root-adopted3415v2/independent3416v2: intentionally removes117authored/173potential expanded registrations from12semantic sources, deleting4whole conventional suites and3registration modules and retaining4authored/5expanded cases, without equivalent replacement or fresh execution. The whole main-window attach-services suite is removed: runtime/window attach, notifier wiring, reload/teardown and permission-handler mock orchestration alarms are intentionally lost. Prune its exact deleted command selectors, testFiles entries and notifier assertionRef where present; complete prior selectors/ref are archived. Unrelated dated receipts remain exact and retain their original scope; no fresh reduced-selector, native-window or remote execution is claimed. Audit3403 composes root-adopted3393/3394,3400/3401 and3398/3399: intentionally retires167 authored registrations/196 potential expanded cases across93 whole suites without equivalent replacement or fresh execution. Retained65 registrations/79 potential cases include one conditional missing-input failure sentinel; potential counts are not executions. Historical notes and complete dated receipts retain their exact original scopes. The retained runtime registration driver loses three complete imported fragments: changed-working-label title facts and suppressed-frame tracked snapshots; refusing SSH GitLab MR-base lookup before local Git/glab calls; and missing folder-workspace path creation/update refusal before spawn/store mutation. These alarms are intentionally lost. Other imported fragments and separately rooted dated receipts are unchanged; they are not equivalent replacement coverage. Audit2271: Linux coverage is limited to the unchanged serve-signal-exit-diagnostic supervisor selector and the late-handoff-after-child-exit assertion recorded below. It does not cover desktop promotion, renderer continuity, persistent PTYs or native updater behavior. No current local, daemon or SSH provider execution is established; coveredProviders is empty. Historical macOS/provider scope remains only in preserved prior accounts and archived receipts. Audit2252 removes the entire handassembled serve promotion/renderer hydration suite. Retired alarms: queued and repeated activation coalescing, duplicate serve suppression, unavailable-provider diagnostic, and zero resume/replacement accounting on independently supplied renderer state. The fixture never joined actual daemon-to-renderer delivery. These exact alarms have no equivalent replacement assigned. Remaining gate selectors keep their existing narrower scope; historical dated receipts remain verbatim and do not validate this changed selector set. No fresh runtime, native/provider, live remote/mobile, cross-platform or performance result is claimed. Archived removed active entries verbatim: commands: \"pnpm exec vitest run --config config/vitest.config.ts src/renderer/src/lib/serve-desktop-promotion-session-continuity.test.ts\"\ntestFiles: \"src/renderer/src/lib/serve-desktop-promotion-session-continuity.test.ts\"\nassertionRefs: {\n \"file\": \"src/renderer/src/lib/serve-desktop-promotion-session-continuity.test.ts\",\n \"assertions\": [\n \"a forced desktop relaunch reaching the headless lock owner opens exactly one window, and only after the persistent provider settles\",\n \"a duplicate `orca serve` launch never promotes the headless owner\",\n \"the renderer promoted inside the serve process resumes zero agents while daemon panes survive, before and after those panes rebind their PTYs\",\n \"no replacement resume tab, startup command, or automatic-resume claim is created, and every surviving session record is left untouched\"\n ]\n } Historical prior account (preserved verbatim): Audit2133 current-parent preparation: Current reduced sources/selectors intentionally retire the alarms listed in knownGaps; prior dated runs and replacement claims do not validate the changed set. Historical prior account (preserved verbatim): Audit2095 current-parent preparation2126: Current reduced sources/selectors intentionally retire the alarms listed in knownGaps; prior dated runs and replacement claims do not validate the changed set. Historical prior account (preserved verbatim): Audit2090 (sources2088/2089): Current reduced suites intentionally retire the alarms listed in knownGaps; prior dated execution and replacement claims do not validate changed source bytes or selectors. Historical prior account (preserved verbatim): Audit2086 (sources2082/2083): Current reduced suites intentionally retire the alarms listed in knownGaps; prior dated execution and replacement claims do not validate changed source bytes or selectors. Historical prior account (preserved verbatim): Audit2076 (sources2073/2074): Intentionally retire these low-value alarms without replacement: src/main/runtime/orca-runtime-tests/terminal-handles-part-02.spec.ts: recovers a moved SSH pane through the tab it now sits in, not the one its lease froze; reports a plain expired orphan; does not report a pane whose only recent lease was superseded; does not report a pane whose only recent lease had its relay id recycled; picks the eligible orphan over a superseded predecessor that matches first; collects the same tabs the per-tab reader reports, in one sweep of the leases; reports no candidate when no lease names any of the worktree tabs. src/main/runtime/orca-runtime-tests/runtime-availability.spec.ts: starts unavailable with no authoritative window; reports the configured Windows terminal shell on status; reports floating workspace availability from settings on status; polls floating tabs with targeted PTY liveness and no repo/provider inventory; hydrates persisted tabs when the store cannot report repos; advertises browser screencast only when a renderer window is available; routes mobile Codex reset consumption through the account mutation coordinator; maps a definite pre-provider rejection into an authoritative current snapshot. Existing unrelated evidenceRuns retain their original scope; unchanged dated receipt objects do not validate changed source bytes or reduced selectors. No fresh gate execution or historical import-closure equivalence is claimed. Frozen current literal-import graph reaches these changed test bytes; unresolved local and computed edges remain debt, and current reachability is not historical execution or equivalent replacement evidence. Historical prior account (preserved verbatim): Audit2020: Retire CLI supervisor outcome, local-open promotion and paired-open projection alarms; launch retains only the two referenced stream-framing cases. Remaining CLI refusal/timeout cases do not replace headless-promotion coverage. Preserve both independent dated receipts at their original commands. Reduced selectors have no fresh result or equivalent replacement claim. Historical prior account (preserved verbatim): Audit1589 removes mock-only CLI forwarding, canned provisioning/catalog/error projections and private inventories without equivalent replacement coverage. Retained cases exercise actual local markdown/paste/reconciliation, stream framing/backpressure/output-drain, temporary directory cleanup and persisted handoff with injected host/provider/process boundaries. No real Jira, provisioning, remote launch, native pipe/process-tree authority or deployed provider catalog proof. Assertion references use the surviving handoff registration titles; changed-selector historical receipts are retired and unaffected receipts remain unchanged. Historical scope before this audit: Deterministic unit coverage exercises activation gating, single-instance ownership, local/remote CLI status, headless binding persistence, local daemon identity, SSH identity transfer, the promoted renderer's agent-resume accounting, and the macOS serve update handoff from staged installer through atomic bundle replacement and target-version readiness. Supervisor settlement coverage prevents a late handoff-completion failure from rearming termination after the replacement child exits. A macOS Electron journey covers headless promotion and persistent PTY identity. A disposable locally signed Electron canary exercised real ShipIt and a temporary LaunchAgent with the compiled production supervisor; full packaged Orca and Linux/Windows serve updates remain uncollected. Audit1383: src/main/runtime/orca-runtime-tests/worktree-native-chat-setup.spec.ts reduced. Lose command-list/receipt/default/background activation matrices whose provider spawn returns canned IDs. Retained test proves actual tab allocation separation, not terminal startup. No equivalent replacement coverage is claimed. Existing exact unaffected receipts and their platform/provider/budget claims remain scoped to unchanged selectors. Changed optional selectors have no new execution result. Audit1401: src/main/runtime/orca-runtime-tests/mobile-session-tabs-duplicate-pty-repair.spec.ts removed. Loses listing-on-already-repaired-data alarm; it never established runtime repair invocation. src/main/runtime/orca-runtime-tests/mobile-session-tabs-split-agent-tab.spec.ts removed. Loses actual mobile split sibling shell versus recorded-agent resume command alarms; no equivalent coverage asserted. src/main/runtime/orca-runtime-tests/worktree-native-chat-setup.spec.ts removed. Loses separate setup/startup tab result in that manually assembled path. src/main/runtime/orca-runtime.test.ts reduced. Driver registration/import of a whole-deleted selected spec is removed; directly authored cases remain unchanged. No extra authored removal credit. No equivalent replacement coverage is claimed. Existing exact unaffected receipts remain unchanged and apply only to their unchanged selectors. Changed optional selectors have no new execution result.", "motivatingLinks": [ "https://github.com/stablyai/orca/issues/8457", "https://github.com/stablyai/orca/issues/9563" @@ -3666,10 +3666,7 @@ }, { "file": "src/main/startup/serve-launch/serve-desktop-activation.test.ts", - "assertions": [ - "early activation requests coalesce until the persistent provider is ready", - "a blocked provider drops pending activation and never opens a window" - ] + "assertions": ["lets a queued user activation keep the foreground over a background open"] }, { "file": "tests/e2e/headless-serve-desktop-activation.spec.ts", diff --git a/config/scripts/locale-catalog/locale-ko-key-overrides.json b/config/scripts/locale-catalog/locale-ko-key-overrides.json index c5808babd64..4b700a64caf 100644 --- a/config/scripts/locale-catalog/locale-ko-key-overrides.json +++ b/config/scripts/locale-catalog/locale-ko-key-overrides.json @@ -1742,12 +1742,6 @@ "auto.components.settings.ExperimentalPane.agentHibernation.toggleLabel": { "ko": "에이전트 최대 절전 전환" }, - "auto.components.settings.ExperimentalPane.newWorktreeCardStyle.copy": { - "ko": "업데이트된 워크트리 카드 레이아웃, 메타데이터 배치, 카드 표시 메뉴 옵션 및 상태 표시를 미리 봅니다." - }, - "auto.components.settings.ExperimentalPane.newWorktreeCardStyle.description": { - "ko": "업데이트된 워크트리 카드 레이아웃, 메타데이터 배치, 카드 표시 메뉴 옵션 및 상태 표시를 미리 봅니다." - }, "auto.components.settings.EphemeralVmsPane.recipes": { "ko": "레시피" }, @@ -2585,9 +2579,6 @@ "auto.components.settings.experimental.search.agentHibernation.title": { "ko": "에이전트 최대 절전" }, - "auto.components.settings.experimental.search.newWorktreeCardStyle.description": { - "ko": "업데이트된 워크트리 카드 레이아웃, 메타데이터 배치, 카드 표시 메뉴 옵션 및 상태 표시를 미리 봅니다." - }, "auto.components.settings.general.search.12ecc640a8": { "ko": "mru" }, diff --git a/config/scripts/runtime-verification/desktop-transport/verify-computer-native.mjs b/config/scripts/runtime-verification/desktop-transport/verify-computer-native.mjs index 4c8e6a3ecbb..9c07d8a1e74 100644 --- a/config/scripts/runtime-verification/desktop-transport/verify-computer-native.mjs +++ b/config/scripts/runtime-verification/desktop-transport/verify-computer-native.mjs @@ -75,6 +75,32 @@ const checks = [ ], enabled: process.platform === 'win32' }, + { + name: 'Windows text entry tests', + command: 'powershell.exe', + args: [ + '-NoLogo', + '-NoProfile', + '-ExecutionPolicy', + 'Bypass', + '-File', + 'native/computer-use-windows/runtime-text-entry.test.ps1' + ], + enabled: process.platform === 'win32' + }, + { + name: 'Windows native control tests', + command: 'powershell.exe', + args: [ + '-NoLogo', + '-NoProfile', + '-ExecutionPolicy', + 'Bypass', + '-File', + 'native/computer-use-windows/runtime-native-controls.test.ps1' + ], + enabled: process.platform === 'win32' + }, { name: 'macOS helper app bundle and signature', run: verifyMacOSHelperApp, diff --git a/config/workspace-record-read-baseline.txt b/config/workspace-record-read-baseline.txt index 3658cbbf35f..ff68d13d62d 100644 --- a/config/workspace-record-read-baseline.txt +++ b/config/workspace-record-read-baseline.txt @@ -56,7 +56,7 @@ 5 src/renderer/src/store/repos/repo-catalog-merge.ts # store writer 5 src/renderer/src/store/repos/repo-removal.ts # store writer 6 src/renderer/src/store/slices/diff-comments/diff-comment-persistence.ts # store writer -12 src/renderer/src/store/slices/workspace-attention/workspace-unread-flag.ts # store writer +10 src/renderer/src/store/slices/workspace-attention/workspace-unread-flag.ts # store writer 2 src/renderer/src/store/slices/worktrees/create/created-worktree-state-merge.ts # store writer 1 src/renderer/src/store/slices/worktrees/listing/detected-worktree-meta.ts # P5b-2 must clear: feeds an existing row-type cast 3 src/renderer/src/store/slices/worktrees/listing/fetch-detected-worktrees.ts # store writer diff --git a/docs/site/content/docs/cli/computer-use.mdx b/docs/site/content/docs/cli/computer-use.mdx index 55abc66c7ce..d1e4cae7a4b 100644 --- a/docs/site/content/docs/cli/computer-use.mdx +++ b/docs/site/content/docs/cli/computer-use.mdx @@ -71,6 +71,16 @@ orca computer get-app-state --app com.microsoft.edgemac --json App names work when unambiguous (`--app Spotify`). Use `--app pid:` only when bundle ID and name both collide. +## Starting an app + +`list-apps` shows only running apps. To start a closed one without pulling you away from what you're doing, use `launch-app`: + +``` +orca computer launch-app TextEdit --json +``` + +It asks macOS and Windows to start the app in the background and leaves an app that's already running alone. Some apps still come forward on their own, so the result's `delivery` says whether the app ended up in front. `--activate` brings it forward instead. Linux doesn't support it yet. + ## Available actions ``` @@ -90,9 +100,9 @@ Prefer semantic actions (`click`, `set-value`, `perform-secondary-action`) over `hotkey` and `press-key` refuse shortcuts that quit or close the app, log out, lock the screen, force-quit or end the session, such as `Cmd+Q`, `Shift+Cmd+Q`, `Ctrl+Cmd+Q` and `Option+Cmd+Esc` on macOS, `Alt+F4` and `Win+L` on Windows, and `Ctrl+Q`, `Alt+F4`, `Super+L` and `Ctrl+Alt+Delete` on Linux. The error code is `system_shortcut_blocked`. Pass `--allow-system-shortcut` to send one anyway. -`type-text --element-index ` inserts the text at that field's caret without focusing it or bringing its window forward. It works on macOS and Linux; on Windows, click the field, confirm it is the focused text receiver, and run `type-text` without the index. +`type-text --element-index ` inserts the text at that field's caret without focusing it or bringing its window forward. It works on macOS, Linux and Windows. On macOS, and for a Windows field written through its UI Automation value, a field that doesn't report its caret gets the text at the end. An app can still bring its own window forward in response, so check `action.delivery`. -On macOS, actions run in the background: the target app isn't brought forward, your frontmost app and key window stay put, and the pointer doesn't move, so you can keep working while an agent drives another window. Each action result reports `action.delivery` (`background` or `foreground`). Pass `--restore-window` only when an app ignores background input, such as drags in Chromium or Electron apps or games; it brings the app forward and uses the real pointer and keyboard. The bundled skill tells agents never to add it on their own: they ask you first, and error messages say the same. Popup menus and web `` controls are set with `set-value` instead of a click. On Linux, element clicks through accessibility actions, `set-value`, `type-text --element-index` and secondary actions run in the background; actions that need the real pointer or the focused window are refused unless you pass `--restore-window`. On Wayland they are refused even with it, because synthetic input never reaches native windows. On Windows, element clicks, `set-value` (including sliders), scrolls and secondary actions (`Invoke`, `Toggle`, `Select`, `Expand`, `Collapse`, `ScrollToVisible`) go through UI Automation when the control supports them, without moving the pointer or sending keys. `type-text --element-index` also works there for text boxes. Some apps still bring their own window forward when driven this way (WPF apps do for most of these), so `action.delivery` reports `foreground` whenever the target window came forward. Actions that need the real mouse, such as coordinate clicks, right-clicks and drags, are refused unless you pass `--restore-window`; then only the target window comes forward and the pointer goes back where it was. Keyboard input (`type-text` without an index, `press-key`, `hotkey`, `paste-text`) is refused the same way while the window isn't in front. When accessibility targeting fails, fall back carefully to coordinates: @@ -116,7 +126,7 @@ On Linux and Windows, action payloads also pass briefly through a local operatio ## Screenshots -`get-app-state` returns an accessibility tree and, by default, a screenshot. With `--json`, the image bytes are written to disk and the path is returned in `screenshot.path` rather than embedded in the response. Pass `--no-screenshot` when pixels aren't needed (faster, smaller payload). On macOS, a window that is minimized, hidden or on another Space can't be read; bring it on screen, or pass `--restore-window` to have Orca bring it forward. +`get-app-state` returns an accessibility tree and, by default, a screenshot. With `--json`, the image bytes are written to disk and the path is returned in `screenshot.path` rather than embedded in the response. Pass `--no-screenshot` when pixels aren't needed (faster, smaller payload). On macOS, a window that is minimized or hidden has no pixels, so the screenshot fails while the accessibility tree still comes back; a window on another Space is captured. On Wayland, screenshots work on KDE Plasma, Hyprland and Sway. On Hyprland and Sway they need `grim`, GdkPixbuf, and `hyprctl` or `swaymsg`; Orca finds the target window by process ID and title and captures its region, so overlapping windows can show in the image. diff --git a/docs/site/content/docs/cli/reference.mdx b/docs/site/content/docs/cli/reference.mdx index e8314c890ff..a92cd0df8e1 100644 --- a/docs/site/content/docs/cli/reference.mdx +++ b/docs/site/content/docs/cli/reference.mdx @@ -39,6 +39,8 @@ orca open --json orca status --json ``` +`orca open` starts Orca without taking keyboard focus from the app you're using. If an Orca window is already open, it does nothing. + Use `--json` when another tool will parse the result. Human-readable output is for quick terminal checks. ## Selectors @@ -306,6 +308,7 @@ Use `orca computer` for native desktop apps outside the built-in browser: ```bash orca computer permissions --json orca computer list-apps --json +orca computer launch-app Safari --json orca computer get-app-state --app com.apple.Safari --json orca computer click --app com.apple.Safari --element-index 12 --json orca computer paste-text --app com.apple.Safari --text "hello" --json diff --git a/mobile/modules/orca-gzip-utf8-codec/android/src/test/java/expo/modules/orcagziputf8codec/BoundedGunzipTest.kt b/mobile/modules/orca-gzip-utf8-codec/android/src/test/java/expo/modules/orcagziputf8codec/BoundedGunzipTest.kt index a55caf16d6f..7afe5a4ae18 100644 --- a/mobile/modules/orca-gzip-utf8-codec/android/src/test/java/expo/modules/orcagziputf8codec/BoundedGunzipTest.kt +++ b/mobile/modules/orca-gzip-utf8-codec/android/src/test/java/expo/modules/orcagziputf8codec/BoundedGunzipTest.kt @@ -6,7 +6,6 @@ import org.junit.Assert.assertEquals import org.junit.Test class BoundedGunzipTest { - // Shared with the Swift checks and the JS tests, which pin fflate's answer to the same inputs. private val vectors = JSONArray(File("../tests/gunzip-vectors.json").readText()) @Test diff --git a/mobile/modules/orca-gzip-utf8-codec/tests/GzipUtf8CodecChecks.swift b/mobile/modules/orca-gzip-utf8-codec/tests/GzipUtf8CodecChecks.swift index 1910ae7748e..08967de0d19 100644 --- a/mobile/modules/orca-gzip-utf8-codec/tests/GzipUtf8CodecChecks.swift +++ b/mobile/modules/orca-gzip-utf8-codec/tests/GzipUtf8CodecChecks.swift @@ -26,11 +26,10 @@ import Foundation } static func main() throws { - // Shared with the Kotlin tests and the JS tests, which pin fflate's answer to the same inputs. let url = URL(fileURLWithPath: "tests/gunzip-vectors.json") let vectors = try JSONSerialization.jsonObject(with: Data(contentsOf: url)) as! [[String: Any]] for vector in vectors where [ - "one member", "reaches the limit", "bomb", "bad CRC", "trailing data", "truncated body" + "one member", "reaches the limit", "bad CRC", "trailing data", "truncated body" ].contains(vector["name"] as? String ?? "") { let name = vector["name"] as! String let expected = (vector["native"] as? String).map { bytes(hex: $0) } diff --git a/mobile/modules/orca-gzip-utf8-codec/tests/gunzip-vectors.json b/mobile/modules/orca-gzip-utf8-codec/tests/gunzip-vectors.json index bd1f6f5e321..0692e2e52fe 100644 --- a/mobile/modules/orca-gzip-utf8-codec/tests/gunzip-vectors.json +++ b/mobile/modules/orca-gzip-utf8-codec/tests/gunzip-vectors.json @@ -6,13 +6,6 @@ "native": "77696e646f7720303a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720313a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720323a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720333a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720343a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720353a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a", "js": "77696e646f7720303a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720313a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720323a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720333a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720343a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720353a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a" }, - { - "name": "empty member", - "gzipHex": "1f8b080000000000001303000000000000000000", - "outputLimit": 1, - "native": "", - "js": "" - }, { "name": "reaches the limit", "gzipHex": "1f8b08000000000000132bcfcc4bc92f5730b052a8562a482cc950b2524a2c2e4e2d29d64fd4cb2a56d251cacf56b22a292a4dd5513ab70728f968cee40ff3673629d5729543741a92add3886c9dc664eb34215ba729e93a01177932565c010000", @@ -20,13 +13,6 @@ "native": "77696e646f7720303a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720313a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720323a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720333a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720343a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f772035", "js": "77696e646f7720303a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720313a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720323a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720333a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720343a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f772035" }, - { - "name": "bomb", - "gzipHex": "1f8b0800000000000013edc13101000000c2a0f54f6d085fa000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003e031cea38a700001000", - "outputLimit": 1025, - "native": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", - "js": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" - }, { "name": "name, comment and header CRC", "gzipHex": "1f8b081a000000000003612e6a73006e6f74650071bc2bcfcc4bc92f5730b052a8562a482cc950b2524a2c2e4e2d29d64fd4cb2a56d251cacf56b22a292a4dd5513ab70728f968cee40ff3673629d5729543741a92add3886c9dc664eb34215ba729e93a01177932565c010000", @@ -69,13 +55,6 @@ "native": null, "js": "77696e646f7720303a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720313a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720323a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720333a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720343a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720353a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a" }, - { - "name": "second member", - "gzipHex": "1f8b08000000000000132bcfcc4bc92f5730b052a8562a482cc950b2524a2c2e4e2d29d64fd4cb2a56d251cacf56b22a292a4dd5513ab70728f968cee40ff3673629d5729543741a92add3886c9dc664eb34215ba729e93a01177932565c0100001f8b08000000000000132bcfcc4bc92f5730b052a8562a482cc950b2524a2c2e4e2d29d64fd4cb2a56d251cacf56b22a292a4dd5513ab70728f968cee40ff3673629d5729543741a92add3886c9dc664eb34215ba729e93a01177932565c010000", - "outputLimit": 697, - "native": null, - "js": "77696e646f7720303a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720313a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720323a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720333a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720343a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a77696e646f7720353a207b2270617468223a226173736574732f612e6a73222c226f6b223a747275652c22cebc223a22e29c93f09f9982227d0a" - }, { "name": "truncated body", "gzipHex": "1f8b08000000000000132bcfcc4bc92f5730b052a8562a482cc950b2524a", @@ -97,20 +76,6 @@ "native": null, "js": null }, - { - "name": "unknown method", - "gzipHex": "1f8b09000000000000132bcfcc4bc92f5730b052a8562a482cc950b2524a2c2e4e2d29d64fd4cb2a56d251cacf56b22a292a4dd5513ab70728f968cee40ff3673629d5729543741a92add3886c9dc664eb34215ba729e93a01177932565c010000", - "outputLimit": 349, - "native": null, - "js": null - }, - { - "name": "header only", - "gzipHex": "1f8b0800000000000013", - "outputLimit": 349, - "native": null, - "js": null - }, { "name": "empty input", "gzipHex": "", diff --git a/mobile/src/mobile-web-shell/bridge-host.test.ts b/mobile/src/mobile-web-shell/bridge-host.test.ts index dd954750fc9..1f87bfe56a2 100644 --- a/mobile/src/mobile-web-shell/bridge-host.test.ts +++ b/mobile/src/mobile-web-shell/bridge-host.test.ts @@ -1,21 +1,9 @@ import { describe, expect, it } from 'vitest' import { harness, ID, OTHER, subscribeFrame, type Harness } from './bridge-host-test-harness' -import { BRIDGE_MAX_UNACKED_BYTES, BRIDGE_MAX_UNACKED_FRAMES } from './bridge-host-subscriptions' -import { - bridgeId, - clientFrame, - createFakeRpcClient, - flushBridge, - rpcSuccess -} from './bridge-host-test-fakes' -import { - BRIDGE_MAX_MESSAGE_BYTES, - BRIDGE_MAX_PENDING_DESKTOP_REQUESTS, - BRIDGE_MAX_REPLY_BYTES -} from './bridge/bridge-caps' -import type { BridgeHostMessage } from './bridge/bridge-envelope' -import { BridgeReplyAssembler } from './bridge/bridge-reply-chunking' +import { BRIDGE_MAX_UNACKED_FRAMES } from './bridge-host-subscriptions' +import { clientFrame, createFakeRpcClient, flushBridge, rpcSuccess } from './bridge-host-test-fakes' +import { BRIDGE_MAX_MESSAGE_BYTES } from './bridge/bridge-caps' describe('requests', () => { it('answers a synchronous throw from the client and frees the slot', () => { @@ -54,46 +42,6 @@ describe('requests', () => { }) }) - it('refuses a subscription id as a request id, because one ledger answers for both', () => { - const bridge = harness({ ready: true }) - bridge.host.receive(subscribeFrame(ID)) - expect(bridge.client.streams).toHaveLength(1) - bridge.host.receive(clientFrame({ type: 'request', id: ID, method: 'status.get' })) - expect(bridge.client.requests).toHaveLength(0) - expect(bridge.last()).toMatchObject({ - type: 'error', - error: { message: expect.stringContaining('already in flight') } - }) - }) - - it('holds the cap against a page that closes between batches', async () => { - const bridge = harness({ ready: true }) - const fill = (offset: number): void => { - for (let index = 0; index < BRIDGE_MAX_PENDING_DESKTOP_REQUESTS; index += 1) { - bridge.host.receive( - clientFrame({ type: 'request', id: bridgeId(offset + index), method: 'status.get' }) - ) - } - } - fill(0) - expect(bridge.client.requests).toHaveLength(BRIDGE_MAX_PENDING_DESKTOP_REQUESTS) - expect(bridge.frames().filter((frame) => frame.type !== 'init')).toEqual([]) - // Desktop calls survive page close because sendRequest has no cancel; keep their slots occupied instead of resetting the cap. - bridge.host.receive(clientFrame({ type: 'close' })) - bridge.host.receive(clientFrame({ type: 'ready' })) - fill(100) - expect(bridge.client.requests).toHaveLength(BRIDGE_MAX_PENDING_DESKTOP_REQUESTS) - expect(bridge.frames().filter((frame) => frame.type === 'error')).toHaveLength( - BRIDGE_MAX_PENDING_DESKTOP_REQUESTS - ) - for (const request of bridge.client.requests) { - request.resolve(rpcSuccess('wire-1', 'ok')) - } - await flushBridge() - fill(200) - expect(bridge.client.requests).toHaveLength(BRIDGE_MAX_PENDING_DESKTOP_REQUESTS * 2) - }) - it('stops answering a cancelled request without pretending the desktop stopped running it', async () => { const bridge = harness({ ready: true }) bridge.host.receive(clientFrame({ type: 'request', id: ID, method: 'status.get' })) @@ -104,35 +52,6 @@ describe('requests', () => { }) }) -describe('replies too big for one frame', () => { - it('chunks and reassembles to the same payload', async () => { - const bridge = harness({ ready: true }) - bridge.host.receive(clientFrame({ type: 'request', id: ID, method: 'worktree.list' })) - const payload = rpcSuccess('wire-1', 'y'.repeat(BRIDGE_MAX_MESSAGE_BYTES * 2)) - bridge.client.requests[0]?.resolve(payload) - await flushBridge() - const replies = bridge.frames() - expect(replies.length).toBeGreaterThan(1) - const assembler = new BridgeReplyAssembler() - const assembled = replies.map((frame) => - frame.type === 'reply' ? assembler.accept(frame) : { status: 'pending' as const } - ) - expect(assembled.at(-1)).toEqual({ status: 'complete', payload }) - }) - - it('aborts the request over the reply ceiling rather than truncating an answer', async () => { - const bridge = harness({ ready: true }) - bridge.host.receive(clientFrame({ type: 'request', id: ID, method: 'worktree.list' })) - bridge.client.requests[0]?.resolve(rpcSuccess('wire-1', 'y'.repeat(BRIDGE_MAX_REPLY_BYTES + 1))) - await flushBridge() - const frame = bridge.last() - expect(frame.type === 'error' && frame.error).toMatchObject({ - category: 'BridgeReplyUndeliverableError', - isRpcDeliveryUnknown: false - }) - }) -}) - describe('subscriptions', () => { it('unsubscribes on cancel, says so, and delivers nothing after', () => { const bridge = harness({ ready: true }) @@ -179,18 +98,6 @@ describe('backpressure', () => { } } - it('sends exactly the unacked frame window and then ends with overflow', () => { - const bridge = harness({ ready: true }) - bridge.host.receive(subscribeFrame(ID, WINDOWED)) - fill(bridge, BRIDGE_MAX_UNACKED_FRAMES) - expect(bridge.frames().filter((frame) => frame.type === 'event')).toHaveLength( - BRIDGE_MAX_UNACKED_FRAMES - ) - fill(bridge, 1) - expect(bridge.last()).toEqual({ v: 1, type: 'end', id: ID, reason: 'overflow' }) - expect(bridge.client.streams[0]?.unsubscribes).toBe(1) - }) - it('acks only up to the seq it was given', () => { const bridge = harness({ ready: true }) bridge.host.receive(subscribeFrame(ID, WINDOWED)) @@ -204,58 +111,6 @@ describe('backpressure', () => { expect(bridge.last()).toEqual({ v: 1, type: 'end', id: ID, reason: 'overflow' }) }) - it('ends on the unacked byte window well before the frame window is reached', () => { - const bridge = harness({ ready: true }) - bridge.host.receive(subscribeFrame(ID, WINDOWED)) - const chunk = 'z'.repeat(BRIDGE_MAX_MESSAGE_BYTES - 1024) - const ended = (): boolean => (bridge.posted.at(-1) ?? '').includes('"type":"end"') - for (let index = 0; index < BRIDGE_MAX_UNACKED_FRAMES && !ended(); index += 1) { - bridge.client.streams[0]?.emit(chunk) - } - const events = bridge.posted.filter((json) => JSON.parse(json).type === 'event') - expect(events.length).toBeGreaterThan(0) - expect(events.length).toBeLessThan(BRIDGE_MAX_UNACKED_FRAMES) - const eventBytes = events.reduce( - (total, json) => total + new TextEncoder().encode(json).byteLength, - 0 - ) - expect(eventBytes).toBeLessThanOrEqual(BRIDGE_MAX_UNACKED_BYTES) - const nextBytes = new TextEncoder().encode( - JSON.stringify({ v: 1, type: 'event', id: ID, seq: events.length + 1, payload: chunk }) - ).byteLength - expect(eventBytes + nextBytes).toBeGreaterThan(BRIDGE_MAX_UNACKED_BYTES) - expect(bridge.last()).toEqual({ v: 1, type: 'end', id: ID, reason: 'overflow' }) - }) - - it('reopens the byte window on ack, not just the frame window', () => { - const bridge = harness({ ready: true }) - bridge.host.receive(subscribeFrame(ID, WINDOWED)) - const chunk = 'z'.repeat(BRIDGE_MAX_MESSAGE_BYTES - 1024) - bridge.client.streams[0]?.emit(chunk) - const first = bridge.posted.find((json) => JSON.parse(json).type === 'event') - expect(first).toBeDefined() - if (first === undefined) { - throw new Error('no event posted') - } - const frameBytes = new TextEncoder().encode(first).byteLength - const fits = Math.floor(BRIDGE_MAX_UNACKED_BYTES / frameBytes) - const events = (): BridgeHostMessage[] => bridge.frames().filter((f) => f.type === 'event') - const emit = (times: number): void => { - for (let index = 0; index < times; index += 1) { - bridge.client.streams[0]?.emit(chunk) - } - } - emit(fits - 1) - expect(events()).toHaveLength(fits) - bridge.host.receive(clientFrame({ type: 'ack', id: ID, seq: fits })) - emit(fits) - expect(frameBytes * fits * 2).toBeGreaterThan(BRIDGE_MAX_UNACKED_BYTES) - // Keep the frame-count window open so only released acknowledgement bytes can unblock the batch. - expect(fits * 2).toBeLessThan(BRIDGE_MAX_UNACKED_FRAMES) - expect(events()).toHaveLength(fits * 2) - expect(bridge.frames().some((frame) => frame.type === 'end')).toBe(false) - }) - it('keeps each stream on its own window', () => { const bridge = harness({ ready: true }) bridge.host.receive(subscribeFrame(ID, WINDOWED)) @@ -269,42 +124,6 @@ describe('backpressure', () => { }) describe('teardown', () => { - it('rejects every pending as delivery-unknown, ends every stream, and refuses later frames', async () => { - const bridge = harness({ ready: true }) - bridge.host.receive(clientFrame({ type: 'request', id: ID, method: 'status.get' })) - bridge.host.receive(subscribeFrame(OTHER)) - bridge.host.dispose() - expect(bridge.frames().filter((frame) => frame.type !== 'init')).toEqual([ - { - v: 1, - type: 'error', - id: ID, - error: { - category: 'BridgeHostDisposedError', - code: 'bridge_host_disposed', - message: 'the page bridge was torn down before this request answered', - isRpcDeliveryUnknown: true - } - }, - { v: 1, type: 'end', id: OTHER, reason: 'closed' } - ]) - expect(bridge.client.streams[0]?.unsubscribes).toBe(1) - bridge.client.requests[0]?.resolve(rpcSuccess('wire-1', 'ok')) - bridge.client.streams[0]?.emit({ chunk: 'a' }) - bridge.host.receive(clientFrame({ type: 'request', id: bridgeId(9), method: 'status.get' })) - bridge.host.receive(subscribeFrame(bridgeId(10))) - bridge.host.receive(clientFrame({ type: 'notify', name: 'foreground' })) - bridge.host.receive(clientFrame({ type: 'ready' })) - await flushBridge() - expect(bridge.frames()).toHaveLength(3) - expect(bridge.client.requests).toHaveLength(1) - expect(bridge.client.streams).toHaveLength(1) - expect(bridge.client.foregroundCalls).toEqual([]) - expect(bridge.diagnostics).toEqual( - Array.from({ length: 4 }, () => ({ kind: 'frame-after-dispose' })) - ) - }) - it('settles what the page owned on close without answering a page that said goodbye', async () => { const bridge = harness({ ready: true }) bridge.host.receive(clientFrame({ type: 'request', id: ID, method: 'status.get' })) @@ -332,22 +151,6 @@ describe('teardown', () => { expect(bridge.diagnostics).toEqual([]) }) - it('forwards no straggler from the document that said goodbye', () => { - const bridge = harness() - bridge.host.receive(clientFrame({ type: 'close' })) - // Refuse stragglers from the closed document rather than answering into its replacement. - bridge.host.receive(clientFrame({ type: 'request', id: ID, method: 'status.get' })) - bridge.host.receive(subscribeFrame(OTHER)) - bridge.host.receive(clientFrame({ type: 'notify', name: 'foreground' })) - expect(bridge.client.requests).toHaveLength(0) - expect(bridge.client.streams).toHaveLength(0) - expect(bridge.client.foregroundCalls).toEqual([]) - expect(bridge.posted).toHaveLength(0) - expect(bridge.diagnostics).toEqual( - Array.from({ length: 3 }, () => ({ kind: 'frame-after-close' })) - ) - }) - it('posts nothing into a view that belongs to no document yet', () => { const bridge = harness() bridge.host.receive(clientFrame({ type: 'close' })) diff --git a/mobile/src/session/agent-launch/use-mobile-new-tab-agent-options.test.tsx b/mobile/src/session/agent-launch/use-mobile-new-tab-agent-options.test.tsx index 87a94023e7d..1c2e3c1d99d 100644 --- a/mobile/src/session/agent-launch/use-mobile-new-tab-agent-options.test.tsx +++ b/mobile/src/session/agent-launch/use-mobile-new-tab-agent-options.test.tsx @@ -139,4 +139,19 @@ describe('useMobileNewTabAgentOptions', () => { expect(drawer.options()).toEqual([]) drawer.unmount() }) + it('keeps a stale-sheet automatic refresh distinct from an explicit user refresh', async () => { + const client = createClient({ agents: () => claude }) + const first = mountDrawer(queryClient) + await first.render({ client, open: true }) + first.unmount() + vi.setSystemTime(1_000_001) + const reopened = mountDrawer(queryClient) + await reopened.render({ client, open: true }) + expect( + client.sendRequest.mock.calls + .filter(([method]) => method === 'preflight.refreshAgents') + .map(([, params]) => params) + ).toEqual([expect.not.objectContaining({ userInitiated: true })]) + reopened.unmount() + }) }) diff --git a/mobile/src/source-control/review/select-opened-session-diff.test.ts b/mobile/src/source-control/review/select-opened-session-diff.test.ts new file mode 100644 index 00000000000..c9b6ab4a7b9 --- /dev/null +++ b/mobile/src/source-control/review/select-opened-session-diff.test.ts @@ -0,0 +1,73 @@ +import { describe, expect, it, vi } from 'vitest' +import type { RpcResponse } from '../../transport/rpc/rpc-wire-types' +import { selectOpenedSessionDiff } from './select-opened-session-diff' + +function ok(result: unknown): RpcResponse { + return { id: 'rpc', ok: true, result, _meta: { runtimeId: 'runtime-1' } } +} + +describe('selectOpenedSessionDiff', () => { + it('selects the opened diff for this phone once the desktop lists it', async () => { + vi.useFakeTimers() + try { + let lists = 0 + const sendRequest = vi.fn(async (method: string, _params?: unknown) => { + if (method === 'session.tabs.list') { + lists += 1 + return ok({ + tabs: + lists === 1 + ? [] + : [ + { + id: 'staged', + type: 'file', + mode: 'diff', + relativePath: 'a.ts', + diffSource: 'staged' + }, + { id: 'edit', type: 'file', mode: 'edit', relativePath: 'a.ts' }, + { id: 'branch', type: 'file', mode: 'diff', relativePath: 'a.ts' }, + { + id: 'unstaged', + type: 'file', + mode: 'diff', + relativePath: 'a.ts', + diffSource: 'unstaged' + } + ] + }) + } + return ok({ activeTabId: 'unstaged' }) + }) + const selecting = selectOpenedSessionDiff( + { sendRequest }, + { + worktreeId: 'wt-1', + relativePath: 'a.ts', + staged: false + }, + () => true + ) + await vi.runAllTimersAsync() + await selecting + + expect( + sendRequest.mock.calls.filter(([method]) => method === 'session.tabs.activate') + ).toEqual([ + [ + 'session.tabs.activate', + { + worktree: 'id:wt-1', + tabId: 'unstaged', + notifyClients: false, + navigation: 'caller', + intent: 'user' + } + ] + ]) + } finally { + vi.useRealTimers() + } + }) +}) diff --git a/mobile/src/source-control/review/select-opened-session-diff.ts b/mobile/src/source-control/review/select-opened-session-diff.ts new file mode 100644 index 00000000000..5e0af0bedf3 --- /dev/null +++ b/mobile/src/source-control/review/select-opened-session-diff.ts @@ -0,0 +1,45 @@ +import { sessionFileTabListRead } from '../../files/session-documents/mobile-source-file-open-operations' +import { activateMobileSessionTab } from '../../session/tabs/mobile-session-tab-activation' + +// The desktop publishes the tab after files.openDiff replies; a few short polls cover that hop. +const TAB_POLL_DELAYS_MS = [0, 300, 600, 900] as const + +/** Selects the diff tab "Open in session" opened for this phone only; the desktop keeps its own view (#33052). */ +export async function selectOpenedSessionDiff( + client: Parameters[0], + target: { worktreeId: string; relativePath: string; staged: boolean }, + isCurrent: () => boolean +): Promise { + const worktree = `id:${target.worktreeId}` + const source = target.staged ? 'staged' : 'unstaged' + for (const delayMs of TAB_POLL_DELAYS_MS) { + if (delayMs > 0) { + await new Promise((resolve) => setTimeout(resolve, delayMs)) + } + if (!isCurrent()) { + return + } + const listed = sessionFileTabListRead.interpret( + await sessionFileTabListRead.request(client, { worktree }) + ) + const tab = listed.accepted + ? listed.value?.tabs.find( + (candidate) => + candidate.mode === 'diff' && + candidate.relativePath === target.relativePath && + // Branch and commit diffs list no source; they are never this working-tree diff. + candidate.diffSource === source + ) + : undefined + if (tab) { + await activateMobileSessionTab(client, { + worktree, + tabId: tab.id, + notifyClients: false, + navigation: 'caller', + intent: 'user' + }) + return + } + } +} diff --git a/mobile/src/source-control/review/use-mobile-diff-review-interactions.ts b/mobile/src/source-control/review/use-mobile-diff-review-interactions.ts index d7616151ece..906c7a419f9 100644 --- a/mobile/src/source-control/review/use-mobile-diff-review-interactions.ts +++ b/mobile/src/source-control/review/use-mobile-diff-review-interactions.ts @@ -1,4 +1,4 @@ -import { useRef, type Dispatch, type SetStateAction } from 'react' +import { useEffect, useRef, type Dispatch, type SetStateAction } from 'react' import { translate } from '../../i18n/mobile-i18n' import type { ConnectionState } from '../../transport/connection/connection-state' import type { RpcClient } from '../../transport/rpc/rpc-client-contract' @@ -8,6 +8,7 @@ import type { ReviewSheet, ReviewSheetIntents } from './mobile-diff-review-sheet import type { MobileReviewTerminalTab } from './review-terminal-reply-schema' import type { MobileDiffReviewNoteDraft } from './use-mobile-diff-review-note-draft' import { sourceFileDiffOpenRun } from '../../files/session-documents/mobile-source-file-open-operations' +import { selectOpenedSessionDiff } from './select-opened-session-diff' import { refusedRpcMessageOrFallback } from '../../transport/rpc/rpc-refusal-message' import { useMobileDiffReviewCommentActions } from './use-mobile-diff-review-comment-actions' import { useMobileDiffReviewGitActions } from './use-mobile-diff-review-git-actions' @@ -73,6 +74,14 @@ export function useMobileDiffReviewInteractions(input: InteractionInput) { // Bumped by every file the reader picks, so a slow save or stage never overrides a newer pick. const navigationRef = useRef(0) + // Open in session waits for the desktop's tab; leaving first must not replace the route the user went to. + const mountedRef = useRef(true) + useEffect(() => { + mountedRef.current = true + return () => { + mountedRef.current = false + } + }, []) const { clearSavedNote, @@ -169,23 +178,38 @@ export function useMobileDiffReviewInteractions(input: InteractionInput) { ) { return } - const response = await sourceFileDiffOpenRun.request(client, { - worktree: `id:${worktreeId}`, + const target = { + worktreeId, relativePath: currentItem.filePath, staged: currentItem.scope === 'staged' - }) - try { - sourceFileDiffOpenRun.interpret(response) - } catch (error) { - setActionError( - refusedRpcMessageOrFallback( - error, - translate('session.useDiffReviewInteractions.openFailed', 'Unable to open in session') - ) - ) - return } - onOpenSession() + setBusyAction('open-in-session') + try { + const response = await sourceFileDiffOpenRun.request(client, { + worktree: `id:${worktreeId}`, + relativePath: target.relativePath, + staged: target.staged + }) + try { + sourceFileDiffOpenRun.interpret(response) + } catch (error) { + setActionError( + refusedRpcMessageOrFallback( + error, + translate('session.useDiffReviewInteractions.openFailed', 'Unable to open in session') + ) + ) + return + } + const isCurrent = () => mountedRef.current + // Best effort: the session still opens, on this phone's previous tab, if selecting the diff fails. + await selectOpenedSessionDiff(client, target, isCurrent).catch(() => {}) + if (isCurrent()) { + onOpenSession() + } + } finally { + setBusyAction(null) + } }, openSendSheet, retryAction: connectionRetryAction({ diff --git a/native/computer-use-linux/runtime.py b/native/computer-use-linux/runtime.py index 498fb998bb8..4e9639339b7 100644 --- a/native/computer-use-linux/runtime.py +++ b/native/computer-use-linux/runtime.py @@ -156,20 +156,93 @@ class GeometryReader: finally: Atspi.set_timeout(ATSPI_METHOD_TIMEOUT_MS, ATSPI_STARTUP_TIMEOUT_MS) - def rect(self, node): - return screen_rect(node, self.read) - def screen_rect(node, read=attempt): + return extents_rect(node, Atspi.CoordType.SCREEN, read) + + +def extents_rect(node, coord_type, read=attempt): component = read(node.get_component_iface) if component is None: return None - rect = read(lambda: Atspi.Component.get_extents(component, Atspi.CoordType.SCREEN)) + rect = read(lambda: Atspi.Component.get_extents(component, coord_type)) if rect is None or rect.width <= 0 or rect.height <= 0: return None return Rect(float(rect.x), float(rect.y), float(rect.width), float(rect.height)) +UNREAD = object() + + +class Placement: + """Screen and window-relative rectangles for one window's nodes. + + GTK 4 reports SCREEN extents of [0, 0, w, h] for nearly every node, so its rectangles come from WINDOW + extents plus the X11 client origin (agent-desktop probes/linux/FINDINGS.md L23-37). + """ + + def __init__(self, app, window): + self.app, self.window = app, window + self.base = self._origin = UNREAD + self.window_coords = toolkit_kind(app) == "gtk4" + + @property + def origin(self): + if self._origin is UNREAD: + self._origin = (gtk4_client_origin(self.app, self.window) + if self.window_coords and not wayland_session() else None) + return self._origin + + def screen(self, node, read=attempt): + if not self.window_coords: + return screen_rect(node, read) + rect = extents_rect(node, Atspi.CoordType.WINDOW, read) if self.origin is not None else None + return None if rect is None else Rect(rect.x + self.origin[0], rect.y + self.origin[1], rect.width, rect.height) + + def bounds(self): + if self.window_coords and wayland_session(): + # Why: Wayland capture finds the window through the compositor and only checks its size. + return screen_rect(self.window) + return self.screen(self.window) + + def frame(self, node, read=attempt): + coord_type = Atspi.CoordType.WINDOW if self.window_coords else Atspi.CoordType.SCREEN + if self.base is UNREAD: + self.base = extents_rect(self.window, coord_type, read) + rect = self.base if node is self.window else extents_rect(node, coord_type, read) + if rect is None or self.base is None: + return None + return Rect(rect.x - self.base.x, rect.y - self.base.y, rect.width, rect.height) + + +# Why per operation: list-windows places every window of an app, and each lookup is a subprocess. +_x11_windows_by_pid = {} + + +def gtk4_client_origin(app, window): + xdotool = shutil.which("xdotool") + if not xdotool: + return None + pid = pid_of(app) + if pid not in _x11_windows_by_pid: + ids = x11_window_ids(app, xdotool) or [] + _x11_windows_by_pid[pid] = [(value, x11_window_name(xdotool, value) if len(ids) > 1 else None) for value in ids] + windows = _x11_windows_by_pid[pid] + if len(windows) > 1: + title = name_of(window) + windows = [item for item in windows if item[1] == title] + return x11_client_origin(int(windows[0][0])) if len(windows) == 1 else None + + +def x11_window_name(xdotool, window_id): + try: + result = subprocess.run([xdotool, "getwindowname", window_id], check=False, capture_output=True, + text=True, timeout=2) + except (OSError, subprocess.TimeoutExpired): + return None + return result.stdout.rstrip("\n") if result.returncode == 0 else None + + def desktop_apps(): for _, app in children(desktop_root()): if name_of(app): @@ -323,36 +396,91 @@ def restore_pointer_window(app, window): restore_window(app, window) if not wait_window_focused(window): raise RuntimeError("window_not_focused: window restoration did not focus the target window; no pointer input was sent") - return screen_rect(window) + + +X11_WINDOW = ctypes.c_ulong +X11_ERROR_HANDLER = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p) +# Why kept: Xlib's default handler exits the process on a BadWindow from a window that closed mid-read. +_ignore_x11_errors = X11_ERROR_HANDLER(lambda _display, _event: 0) + + +@contextmanager +def x11_display(): + """libX11 and an open display, or (None, None) when no X11 display answers.""" + try: + x = ctypes.CDLL("libX11.so.6") + except OSError: + yield None, None + return + pointer, window, integer = ctypes.c_void_p, X11_WINDOW, ctypes.c_int + for name, result, args in ( + ("XOpenDisplay", pointer, [ctypes.c_char_p]), + ("XCloseDisplay", integer, [pointer]), + ("XSetErrorHandler", X11_ERROR_HANDLER, [X11_ERROR_HANDLER]), + ("XDefaultRootWindow", window, [pointer]), + ("XQueryPointer", integer, [pointer, window, ctypes.POINTER(window), ctypes.POINTER(window), + *[ctypes.POINTER(integer)] * 4, ctypes.POINTER(ctypes.c_uint)]), + ("XTranslateCoordinates", integer, [pointer, window, window, integer, integer, ctypes.POINTER(integer), + ctypes.POINTER(integer), ctypes.POINTER(window)]), + ("XInternAtom", ctypes.c_ulong, [pointer, ctypes.c_char_p, integer]), + ("XGetWindowProperty", integer, [pointer, window, ctypes.c_ulong, ctypes.c_long, ctypes.c_long, integer, + ctypes.c_ulong, ctypes.POINTER(ctypes.c_ulong), ctypes.POINTER(integer), + ctypes.POINTER(ctypes.c_ulong), ctypes.POINTER(ctypes.c_ulong), + ctypes.POINTER(ctypes.POINTER(ctypes.c_long))]), + ("XFree", integer, [pointer]), + ): + fn = getattr(x, name) + fn.restype, fn.argtypes = result, args + display = x.XOpenDisplay(None) + if not display: + yield None, None + return + previous = x.XSetErrorHandler(_ignore_x11_errors) + try: + yield x, display + finally: + x.XCloseDisplay(display) + x.XSetErrorHandler(previous) def x11_pointer_state(): """The X11 pointer's root position (None off the default screen) and modifier mask, or None with no X11 display.""" - try: - x = ctypes.CDLL("libX11.so.6") - except OSError: - return None - pointer, window, integer = ctypes.c_void_p, ctypes.c_ulong, ctypes.c_int - x.XOpenDisplay.restype, x.XOpenDisplay.argtypes = pointer, [ctypes.c_char_p] - x.XDefaultRootWindow.restype, x.XDefaultRootWindow.argtypes = window, [pointer] - x.XQueryPointer.restype = integer - x.XQueryPointer.argtypes = [pointer, window, ctypes.POINTER(window), ctypes.POINTER(window), - *[ctypes.POINTER(integer)] * 4, ctypes.POINTER(ctypes.c_uint)] - x.XCloseDisplay.argtypes = [pointer] - display = x.XOpenDisplay(None) - if not display: - return None - try: - root, child = window(), window() - root_x, root_y, window_x, window_y, mask = integer(), integer(), integer(), integer(), ctypes.c_uint() + with x11_display() as (x, display): + if display is None: + return None + root, child = X11_WINDOW(), X11_WINDOW() + root_x, root_y, window_x, window_y = (ctypes.c_int() for _ in range(4)) + mask = ctypes.c_uint() # Why keep the mask on False: that only means the pointer is on another screen; the modifiers are still valid. if not x.XQueryPointer(display, x.XDefaultRootWindow(display), ctypes.byref(root), ctypes.byref(child), ctypes.byref(root_x), ctypes.byref(root_y), ctypes.byref(window_x), ctypes.byref(window_y), ctypes.byref(mask)): return None, None, mask.value return root_x.value, root_y.value, mask.value - finally: - x.XCloseDisplay(display) + + +def x11_client_origin(window_id): + """Where the visible area of an X11 client window starts on the root: its origin plus its client-side shadow.""" + with x11_display() as (x, display): + if display is None: + return None + root_x, root_y, child = ctypes.c_int(), ctypes.c_int(), X11_WINDOW() + if not x.XTranslateCoordinates(display, window_id, x.XDefaultRootWindow(display), 0, 0, + ctypes.byref(root_x), ctypes.byref(root_y), ctypes.byref(child)): + return None + shadow = (0, 0) + atom = x.XInternAtom(display, b"_GTK_FRAME_EXTENTS", 1) + actual_type, actual_format = ctypes.c_ulong(), ctypes.c_int() + count, remaining, data = ctypes.c_ulong(), ctypes.c_ulong(), ctypes.POINTER(ctypes.c_long)() + if atom and x.XGetWindowProperty(display, window_id, atom, 0, 4, 0, 6, ctypes.byref(actual_type), + ctypes.byref(actual_format), ctypes.byref(count), ctypes.byref(remaining), + ctypes.byref(data)) == 0 and data: + try: + if actual_format.value == 32 and count.value == 4: + shadow = (int(data[0]), int(data[2])) # left, right, top, bottom + finally: + x.XFree(data) + return root_x.value + shadow[0], root_y.value + shadow[1] @contextmanager @@ -1293,7 +1421,9 @@ def app_json(app): def window_json(app, index, window): - bounds = screen_rect(window) + bounds = Placement(app, window).bounds() + # Why the SCREEN fallback: a GTK 4 window with no known origin still has a true size, just no position. + size = bounds or screen_rect(window) showing = has_state(window, Atspi.StateType.SHOWING) return { "index": index, @@ -1302,8 +1432,8 @@ def window_json(app, index, window): "title": name_of(window), "x": round(bounds.x) if bounds else None, "y": round(bounds.y) if bounds else None, - "width": max(0, round(bounds.width if bounds else 0)), - "height": max(0, round(bounds.height if bounds else 0)), + "width": max(0, round(size.width if size else 0)), + "height": max(0, round(size.height if size else 0)), "isMinimized": not showing, "isOffscreen": not showing, "screenIndex": None, @@ -1317,7 +1447,8 @@ def make_snapshot(query, include_screenshot, window_id=None, window_index=None, if restore: restore_window(app, window) window_index, window = choose_window(app, window_id, window_index) - bounds = screen_rect(window) + placement = Placement(app, window) + bounds = placement.bounds() tree_root, root_path = window, [window_index] if root is not None: tree_root = find_element(app, root) @@ -1336,12 +1467,13 @@ def make_snapshot(query, include_screenshot, window_id=None, window_index=None, app_info = app_json(app) window_title = name_of(window) screenshot = capture_png(bounds, app, window) if include_screenshot else None + if include_screenshot and bounds is None and placement.window_coords: + screenshot = screenshot_failure("GTK 4 screenshot skipped: Orca can't tell where this window sits on screen; " + "use --no-screenshot and trust the tree") geometry = GeometryReader() for item in records: node = geometry_nodes[id(item)] - rect = bounds if node is window else geometry.rect(node) - if rect is not None and bounds is not None: - rect = Rect(rect.x - bounds.x, rect.y - bounds.y, rect.width, rect.height) + rect = placement.frame(node, geometry.read) item["frame"] = rect.to_json() if rect else None return { "snapshotId": str(uuid.uuid4()), @@ -1401,7 +1533,7 @@ def handshake_response(): "pressKey": True, "hotkey": has_hotkey, "pasteText": has_clipboard, - "scroll": not is_wayland, + "scroll": True, "drag": not is_wayland, "setValue": True, "performAction": True, @@ -1424,10 +1556,11 @@ def all_nodes(root): result = [] def walk(node): - if len(result) >= MAX_NODES: - return result.append(node) for _, child in children(node): + # Why check before each child: every lookup is an AT-SPI round trip, and a list can hold thousands. + if len(result) >= MAX_NODES: + return walk(child) walk(root) @@ -1556,8 +1689,12 @@ def activate_focused_entry(app, window): "verification": {"state": "unverified", "reason": "accessibility_action_unasserted"}} -def screen_point(window_rect, saved_element=None, x=None, y=None, node=None): - rect = screen_rect(node) if node is not None else None +def screen_point(placed, saved_element=None, x=None, y=None, node=None): + if placed.window_coords and placed.origin is None: + raise RuntimeError("unsupported capability: Orca can't tell where this GTK 4 window sits on screen, so " + "pointer input would miss; nothing was sent. Use an element's accessibility action or set-value") + window_rect = placed.screen(placed.window) + rect = placed.screen(node) if node is not None else None if rect is not None: return rect.x + rect.width / 2, rect.y + rect.height / 2 if saved_element is not None: @@ -1567,8 +1704,9 @@ def screen_point(window_rect, saved_element=None, x=None, y=None, node=None): return window_rect.x + float(x), window_rect.y + float(y) -def window_point(window_rect, saved_element=None, x=None, y=None, node=None): - point = screen_point(window_rect, saved_element, x, y, node) +def window_point(placed, saved_element=None, x=None, y=None, node=None): + point = screen_point(placed, saved_element, x, y, node) + window_rect = placed.screen(placed.window) if (window_rect is None or not window_rect.x <= point[0] < window_rect.x + window_rect.width or not window_rect.y <= point[1] < window_rect.y + window_rect.height): raise RuntimeError("coordinate action target lies outside the target window, so the pointer would land on " @@ -1696,6 +1834,241 @@ def drag_between(start, end): Atspi.generate_mouse_event(round(x), round(y), "b1r") +SCROLL_DIRECTIONS = {"up": ("vertical", -1), "down": ("vertical", 1), "left": ("horizontal", -1), "right": ("horizontal", 1)} +DOCUMENT_ROLES = {"document web", "document frame", "scroll pane"} +MAX_SCROLL_BAR_SIBLINGS = 8 +MAX_SCROLL_TO_PAGES = 10 +SCROLL_SETTLE_SECONDS = 1 + + +def scroll_direction(raw): + direction = str(raw or "").strip().lower() + if not direction: + raise RuntimeError("direction is required") + if direction not in SCROLL_DIRECTIONS: + raise RuntimeError(f"unsupported scroll direction: {direction}") + return SCROLL_DIRECTIONS[direction] + + +def scroll_without_pointer(window, node, placed, x, y, direction, pages): + """Scroll through scroll bar values or Component.ScrollTo, which move no pointer and raise no window + (agent-desktop actions/scroll/scroll_bus.rs; FINDINGS L4-22).""" + axis, sign = scroll_direction(direction) + pages = require_positive_number(1 if pages is None else pages, "pages") + if node is None and x is not None and y is not None: + node = node_at(window, placed, float(x), float(y), [MAX_NODES]) + at_limit, budget = None, [MAX_NODES] + # Why innermost first: like a wheel, the nearest view that can move takes the scroll, and one at its end hands it out. + for container in ancestors_to(node, window): + bar = scroll_bar(container, axis) + if bar is not None: + action = write_scroll_bar(container, bar, axis, sign, pages) + elif float(pages).is_integer(): + action = scroll_to_pages(container, placed, axis, sign, int(pages), budget) + else: + continue + if action is not None and action["verification"].get("reason") != "suspected_noop": + return action + at_limit = at_limit or action + return at_limit + + +def ancestors_to(node, window): + found, current = [], node + for _ in range(MAX_DEPTH): + if current is None: + break + found.append(current) + if current is window or current == window: + break + current = attempt(lambda current=current: current.get_parent()) + return found + + +def node_at(window, placed, x, y, budget): + """The deepest node whose window-relative frame holds the point, preferring later (topmost) siblings.""" + node = window + for _ in range(MAX_DEPTH): + hit = None + count = int(attempt(lambda node=node: node.get_child_count(), 0) or 0) + for index in range(count - 1, -1, -1): + budget[0] -= 1 + if budget[0] < 0: + return node + child = attempt(lambda node=node, index=index: node.get_child_at_index(index)) + frame = placed.frame(child) if child is not None else None + if frame is not None and frame.x <= x < frame.x + frame.width and frame.y <= y < frame.y + frame.height: + hit = child + break + if hit is None: + return node + node = hit + return node + + +def scroll_bar(container, axis): + vertical, horizontal = (getattr(Atspi.StateType, name, None) for name in ("VERTICAL", "HORIZONTAL")) + # Why capped: scroll bars sit beside one viewport child, and reading every row of a big list costs the app memory. + if int(attempt(lambda: container.get_child_count(), 0) or 0) > MAX_SCROLL_BAR_SIBLINGS: + return None + for _, child in children(container): + if role_of(child).lower() != "scroll bar": + continue + if vertical is not None and has_state(child, vertical): + matches = axis == "vertical" + elif horizontal is not None and has_state(child, horizontal): + matches = axis == "horizontal" + else: + rect = screen_rect(child) + matches = rect is not None and (rect.height > rect.width) == (axis == "vertical") + value = attempt(lambda child=child: child.get_value_iface()) if matches else None + if value is None: + continue + low, high, current = (attempt(lambda read=read: read(value)) for read in ( + Atspi.Value.get_minimum_value, Atspi.Value.get_maximum_value, Atspi.Value.get_current_value)) + if None not in (low, high, current) and high > low: + return value, float(low), float(high), float(current) + return None + + +def write_scroll_bar(container, bar, axis, sign, pages): + value, low, high, current = bar + viewport = screen_rect(container) + if viewport is None: + return None + # Why the viewport length: toolkit adjustments count pixels, so one page is one viewport (scroll_plan.rs). + page = viewport.height if axis == "vertical" else viewport.width + wanted = min(high, max(low, current + sign * pages * page)) + action = {"path": "accessibility", "actionName": "scrollBarValue", "fallbackReason": None} + if abs(wanted - current) < 0.5: + action["verification"] = {"state": "unverified", "reason": "suspected_noop"} + return action + if not attempt(lambda: Atspi.Value.set_current_value(value, wanted), False): + return None + readback = attempt(lambda: Atspi.Value.get_current_value(value)) + action["verification"] = ({"state": "verified", "property": "value"} + if readback is not None and abs(float(readback) - wanted) < 0.5 + else {"state": "unverified", "reason": "value_mismatch"}) + return action + + +def scroll_to_pages(container, placed, axis, sign, pages, budget): + edges = {("vertical", 1): "TOP_EDGE", ("vertical", -1): "BOTTOM_EDGE", + ("horizontal", 1): "LEFT_EDGE", ("horizontal", -1): "RIGHT_EDGE"} + scroll_type = getattr(getattr(Atspi, "ScrollType", None), edges[(axis, sign)], None) + if scroll_type is None: + return None + moved = accepted = 0 + at_end = False + for _ in range(min(pages, MAX_SCROLL_TO_PAGES)): + viewport = placed.frame(container) + if viewport is None: + break + start, length = (viewport.y, viewport.height) if axis == "vertical" else (viewport.x, viewport.width) + item = item_past_edge(container, placed, axis, sign, start, start + length, MAX_DEPTH, budget) + at_end = item is None and budget[0] >= 0 + component = attempt(lambda: item.get_component_iface()) if item is not None else None + before = placed.frame(item) if component is not None else None + if before is None or not attempt(lambda: Atspi.Component.scroll_to(component, scroll_type), False): + break + accepted += 1 + if not wait_item_moved(item, placed, axis, before): + break + moved += 1 + if not accepted: + # Why only documents: a document with nothing past its edge is at its end, while other nodes may not scroll. + if at_end and role_of(container).lower() in DOCUMENT_ROLES: + return {"path": "accessibility", "actionName": "scrollTo", "fallbackReason": None, + "verification": {"state": "unverified", "reason": "suspected_noop"}} + return None + # Why an action after any page: once the document moved, falling back to the pointer would scroll it twice. + verification = ({"state": "unverified", "reason": "accessibility_action_unasserted"} if moved + else {"state": "unverified", "reason": "suspected_noop"}) + return {"path": "accessibility", "actionName": "scrollTo", "fallbackReason": None, "verification": verification} + + +def item_past_edge(node, placed, axis, sign, viewport_start, viewport_end, depth, budget): + """The item that should lead the next page: the first one cut by or beyond the viewport edge we scroll toward.""" + if depth <= 0: + return None + edge = viewport_end if sign > 0 else viewport_start + count = int(attempt(lambda: node.get_child_count(), 0) or 0) + for index in (range(count) if sign > 0 else range(count - 1, -1, -1)): + # Why a shared budget: each child is an AT-SPI round trip, and a document can hold thousands. + budget[0] -= 1 + if budget[0] < 0: + return None + child = attempt(lambda index=index: node.get_child_at_index(index)) + frame = placed.frame(child) if child is not None else None + if frame is None: + continue + start, end = (frame.y, frame.y + frame.height) if axis == "vertical" else (frame.x, frame.x + frame.width) + if (start >= edge - 1) if sign > 0 else (end <= edge + 1): + return child + if start < edge < end: + inner = item_past_edge(child, placed, axis, sign, viewport_start, viewport_end, depth - 1, budget) + # Why the cut item itself: its hidden part leads the next page, unless it already fills the viewport. + leads = start > viewport_start + 1 if sign > 0 else end < viewport_end - 1 + if inner is not None or leads: + return inner if inner is not None else child + return None + + +def wait_item_moved(item, placed, axis, before): + """Wait until ScrollTo moved the item, so the next page is planned from settled geometry.""" + deadline = time.monotonic() + SCROLL_SETTLE_SECONDS + was = before.y if axis == "vertical" else before.x + while True: + frame = placed.frame(item) + now = None if frame is None else frame.y if axis == "vertical" else frame.x + if now is not None and abs(now - was) > 1: + return True + if time.monotonic() >= deadline: + return False + time.sleep(0.05) + + +def select_in_parent(app, node): + """Select a row through its container's Selection, proving the index names this row first (select_index.rs).""" + parent = attempt(lambda: node.get_parent()) if node is not None else None + selection = attempt(lambda: parent.get_selection_iface()) if parent is not None else None + index = attempt(lambda: node.get_index_in_parent()) if selection is not None else None + if index is None or index < 0 or not selection_indices_proven(app, parent, selection): + return None + if attempt(lambda: parent.get_child_at_index(index)) != node: + raise RuntimeError("stale element: the selection index no longer names this element; run get-app-state again and use a fresh element index") + multiselectable = getattr(Atspi.StateType, "MULTISELECTABLE", None) + # Why clear first: a plain click replaces the selection, while SelectChild adds to a multi-select list's. + if (multiselectable is not None and has_state(parent, multiselectable) + and not attempt(lambda: Atspi.Selection.clear_selection(selection), False)): + return None + if not attempt(lambda: Atspi.Selection.select_child(selection, index), False): + return None + attempt(lambda: node.clear_cache()) + verified = has_state(node, Atspi.StateType.SELECTED) + return {"path": "accessibility", "actionName": "selectChild", "fallbackReason": None, + "verification": {"state": "verified", "property": "selection"} if verified + else {"state": "unverified", "reason": "accessibility_action_unasserted"}} + + +def selection_indices_proven(app, parent, selection): + # Why: GTK 4 Selection counts item widgets, which can differ from its accessible children (select_index.rs). + if toolkit_kind(app) in {"gtk3", "qt", "chromium", "gecko"}: + return True + count = int(attempt(lambda: parent.get_child_count(), 0) or 0) + + def answer(index): + try: + Atspi.Selection.is_child_selected(selection, index) + return "answered" + except Exception as error: + # Why only InvalidArgs: a timeout or lost connection proves nothing about the index space. + return "refused" if "InvalidArgs" in str(error) else "unknown" + + # Why counted: the index spaces match exactly when the last child answers and the one past it is refused. + return count > 0 and answer(count - 1) == "answered" and answer(count) == "refused" + + def is_enter_key(raw): return key_name(raw) in {"Return", "KP_Enter"} @@ -2125,6 +2498,7 @@ def set_value(node, value): def run_operation(operation): + _x11_windows_by_pid.clear() tool = operation.get("tool") include_screenshot = not bool(operation.get("noScreenshot")) if tool == "handshake": @@ -2154,11 +2528,11 @@ def run_operation(operation): action = None def pointer_input(what, hint, points, send): - points(screen_rect(window)) # Why first: a stale or off-window target is not a reason to ask for a raise. + points(Placement(app, window)) # Why first: a stale or off-window target is not a reason to ask for a raise. require_pointer_route(operation, what, hint) - restored = restore_pointer_window(app, window) + restore_pointer_window(app, window) with pointer_put_back(): - send(*points(restored)) + send(*points(Placement(app, window))) if tool == "click": preferred = preferred_action(node) @@ -2168,7 +2542,8 @@ def run_operation(operation): else 1 ) has_modifiers = bool(str(operation.get("modifiers") or "").strip()) - semantic = not has_modifiers and operation.get("mouse_button", "left") == "left" and click_count <= 1 and preferred is not None + simple = not has_modifiers and operation.get("mouse_button", "left") == "left" and click_count <= 1 + semantic = simple and preferred is not None if semantic: names = action_names(node) name = (names[preferred] if preferred < len(names) else "") or "action" @@ -2176,9 +2551,11 @@ def run_operation(operation): if semantic and perform_action(node, preferred): action = {"path": "accessibility", "actionName": name, "fallbackReason": None} else: + action = select_in_parent(app, node) if simple else None + if action is None: pointer_input( "this click", "Click an element by --element-index that has an accessibility action, or use set-value.", - lambda bounds: [window_point(bounds, saved, operation.get("x"), operation.get("y"), node)], + lambda placed: [window_point(placed, saved, operation.get("x"), operation.get("y"), node)], lambda point: click_at(*point, operation.get("mouse_button", "left"), click_count, operation.get("modifiers")), ) action = {"path": "synthetic", "actionName": None, "fallbackReason": "actionUnsupported"} @@ -2196,20 +2573,23 @@ def run_operation(operation): raise RuntimeError(f'{operation.get("action", "")} is not a valid secondary action') action = {"path": "accessibility", "actionName": label, "fallbackReason": None} elif tool == "scroll": - pointer_input( - "scrolling", "", - lambda bounds: [window_point(bounds, saved, operation.get("x"), operation.get("y"), node)], - lambda point: scroll_at(*point, operation.get("direction"), operation.get("pages")), - ) - action = {"path": "synthetic", "actionName": "scroll", "fallbackReason": None} + action = scroll_without_pointer(window, node, Placement(app, window), operation.get("x"), operation.get("y"), + operation.get("direction"), operation.get("pages")) + if action is None: + pointer_input( + "scrolling", "This view exposes no scroll bar or scrollable document to scroll in the background.", + lambda placed: [window_point(placed, saved, operation.get("x"), operation.get("y"), node)], + lambda point: scroll_at(*point, operation.get("direction"), operation.get("pages")), + ) + action = {"path": "synthetic", "actionName": "scroll", "fallbackReason": None} elif tool == "drag": from_node = find_element(app, operation.get("fromElement")) to_node = find_element(app, operation.get("toElement")) pointer_input( "dragging", "", - lambda bounds: [ - window_point(bounds, operation.get("fromElement"), operation.get("from_x"), operation.get("from_y"), from_node), - window_point(bounds, operation.get("toElement"), operation.get("to_x"), operation.get("to_y"), to_node), + lambda placed: [ + window_point(placed, operation.get("fromElement"), operation.get("from_x"), operation.get("from_y"), from_node), + window_point(placed, operation.get("toElement"), operation.get("to_x"), operation.get("to_y"), to_node), ], drag_between, ) diff --git a/native/computer-use-linux/runtime_render_test.py b/native/computer-use-linux/runtime_render_test.py index 5e613d58896..aa2a9448a8b 100644 --- a/native/computer-use-linux/runtime_render_test.py +++ b/native/computer-use-linux/runtime_render_test.py @@ -22,6 +22,9 @@ class FakeStateType: SENSITIVE = 8 HAS_POPUP = 9 MULTI_LINE = 10 + VERTICAL = 11 + HORIZONTAL = 12 + MULTISELECTABLE = 13 class FakeStateSet: @@ -101,6 +104,15 @@ class FakeAccessible: self.name = name self.value = value self.children = list(children) + self.parent = None + for child in self.children: + child.parent = self + + def get_parent(self): + return self.parent + + def get_index_in_parent(self): + return self.parent.children.index(self) def get_child_count(self): return len(self.children) @@ -161,7 +173,7 @@ class SnapshotFixture(ExitStack): (runtime.time, "monotonic", lambda: self.clock), (runtime.Atspi, "set_timeout", self.set_timeout), (runtime.Atspi, "Component", types.SimpleNamespace(get_extents=self.extents)), - (runtime.Atspi, "CoordType", types.SimpleNamespace(SCREEN=0)), + (runtime.Atspi, "CoordType", types.SimpleNamespace(SCREEN=0, WINDOW=1)), (runtime, "capture_png", self.capture), (runtime, "restore_window", lambda *_: self.restore()), (runtime, "x11_pointer_state", lambda: self.pointer), @@ -673,6 +685,160 @@ class RuntimeRenderTest(unittest.TestCase): self.assertEqual(effects, []) keys.assert_not_called() + def test_gtk4_rectangles_come_from_window_extents_and_the_x11_client_origin(self): + button = FakeAccessible("push button", "Open") + window = FakeAccessible("frame", "Files", children=[button]) + app = FakeAccessible("application", "Files", children=[window]) + app.get_toolkit_name, app.get_toolkit_version = (lambda: "GTK"), (lambda: "4.14.5") + # Why these numbers: GTK 4 answers SCREEN with [0, 0, w, h] (agent-desktop FINDINGS L23-37). + extents = {(window, 0): (0, 0, 800, 600), (window, 1): (0, 0, 800, 600), + (button, 0): (0, 0, 64, 44), (button, 1): (16, 299, 64, 44)} + for node in (window, button): + node.get_component_iface = lambda node=node: node + def get_extents(node, coord_type): + x, y, width, height = extents[(node, coord_type)] + return types.SimpleNamespace(x=x, y=y, width=width, height=height) + for origin, wayland in (((1000, 500), False), (None, False), (None, True)): + with self.subTest(origin=origin, wayland=wayland), \ + patch.object(runtime.Atspi, "Component", types.SimpleNamespace(get_extents=get_extents), create=True), \ + patch.object(runtime.Atspi, "CoordType", types.SimpleNamespace(SCREEN=0, WINDOW=1), create=True), \ + patch.object(runtime, "gtk4_client_origin", lambda *_: origin), \ + patch.object(runtime, "wayland_session", lambda: wayland): + placed = runtime.Placement(app, window) + self.assertEqual(placed.frame(button).to_json(), {"x": 16, "y": 299, "width": 64, "height": 44}) + if origin: + self.assertEqual(runtime.window_point(placed, {"index": 1}, node=button), (1048, 821)) + else: + with self.assertRaisesRegex(RuntimeError, "GTK 4 window"): + runtime.window_point(placed, {"index": 1}, node=button) + + def test_background_scroll_pages_a_scroll_bar_and_hands_the_end_to_the_outer_view(self): + def view(name, value, high, children=()): + bar = FakeAccessible("scroll bar", name + " bar") + bar.position = value + bar.high = high + bar.get_state_set = lambda: FakeStateSet(FakeStateType.VERTICAL) + bar.get_value_iface = lambda: bar + pane = FakeAccessible("scroll pane", name, children=[bar, *children]) + return pane, bar + row = FakeAccessible("list item", "Row") + inner, inner_bar = view("inner", 300, 300, [row]) + outer, outer_bar = view("outer", 0, 2000, [inner]) + writes = [] + def set_value(bar, value): + writes.append((bar.name, value)) + bar.position = value + return True + value_api = types.SimpleNamespace(get_minimum_value=lambda bar: 0, get_maximum_value=lambda bar: bar.high, + get_current_value=lambda bar: bar.position, set_current_value=set_value) + with SnapshotFixture(FakeAccessible("frame", "Editor", children=[outer])) as fixture, \ + patch.object(runtime.Atspi, "Value", value_api, create=True): + fixture.bounds[outer] = runtime.Rect(100, 200, 300, 250) + element = next(item for item in runtime.make_snapshot("Fixture", False)["elements"] if item["name"] == "Row") + result = runtime.run_operation({"tool": "scroll", "app": "Fixture", "element": element, "direction": "down", + "pages": 2, "noScreenshot": True}) + self.assertEqual(writes, [("outer bar", 500)]) + self.assertEqual(result["action"]["verification"], {"state": "verified", "property": "value"}) + self.assertEqual(result["action"]["delivery"], "background") + runtime.run_operation({"tool": "scroll", "app": "Fixture", "element": element, "direction": "up", + "pages": 0.5, "noScreenshot": True}) + self.assertEqual(writes[-1], ("inner bar", 300 - 0.5 * fixture.bounds[inner].height)) + + def test_background_scroll_pages_a_document_with_scroll_to(self): + items = [FakeAccessible("paragraph", f"Item {index}") for index in range(6)] + document = FakeAccessible("document web", "Page", children=items) + scrolled = [] + offset = [0] + with SnapshotFixture(FakeAccessible("frame", "Browser", children=[document])) as fixture: + fixture.bounds[document] = runtime.Rect(0, 100, 400, 300) + def place(): + for index, item in enumerate(items): + fixture.bounds[item] = runtime.Rect(0, 100 + index * 120 - offset[0], 400, 120) + place() + def scroll_to(item, edge): + scrolled.append((item.name, edge)) + offset[0] += fixture.bounds[item].y - 100 + place() + return True + with patch.object(runtime.Atspi.Component, "scroll_to", scroll_to, create=True), \ + patch.object(runtime.Atspi, "ScrollType", types.SimpleNamespace(TOP_EDGE="top", BOTTOM_EDGE="bottom"), create=True): + result = runtime.run_operation({"tool": "scroll", "app": "Fixture", "x": 50, "y": 50, + "direction": "down", "pages": 3, "noScreenshot": True}) + # Why Item 2: it spans 340-460 and the viewport ends at 400, so its hidden half leads the next page. + # The third page finds nothing past the document's end, and the two pages already moved still count. + self.assertEqual(scrolled, [("Item 2", "top"), ("Item 4", "top")]) + self.assertEqual(result["action"]["actionName"], "scrollTo") + + def test_background_scroll_moves_the_inner_scroller_before_the_page(self): + inner_items = [FakeAccessible("list item", f"Message {index}") for index in range(4)] + log = FakeAccessible("panel", "Chat log", children=inner_items) + page_items = [FakeAccessible("paragraph", f"Section {index}") for index in range(4)] + document = FakeAccessible("document web", "Page", children=[log, *page_items]) + scrolled = [] + with SnapshotFixture(FakeAccessible("frame", "Browser", children=[document])) as fixture: + fixture.bounds[fixture.root] = runtime.Rect(0, 0, 400, 400) + fixture.bounds[document] = runtime.Rect(0, 0, 400, 400) + fixture.bounds[log] = runtime.Rect(0, 0, 400, 100) + for index, item in enumerate(inner_items): + fixture.bounds[item] = runtime.Rect(0, index * 60, 400, 60) + for index, item in enumerate(page_items): + fixture.bounds[item] = runtime.Rect(0, 100 + index * 150, 400, 150) + element = next(item for item in runtime.make_snapshot("Fixture", False)["elements"] if item["name"] == "Message 0") + def scroll_to(item, _): + scrolled.append(item.name) + fixture.bounds[item] = runtime.Rect(0, 0, 400, 60) + return True + with patch.object(runtime.Atspi.Component, "scroll_to", scroll_to, create=True), \ + patch.object(runtime.Atspi, "ScrollType", types.SimpleNamespace(TOP_EDGE="top"), create=True): + runtime.run_operation({"tool": "scroll", "app": "Fixture", "element": element, "direction": "down", + "pages": 1, "noScreenshot": True}) + # Why Message 1: it spans 60-120 and the chat log's own box ends at 100, so the log scrolls, not the page. + self.assertEqual(scrolled, ["Message 1"]) + + def test_click_without_an_action_selects_the_row_only_when_its_index_is_proven(self): + cases = ( + ("Qt", "6.6", None, "selected"), + ("GTK", "4.14", (True, False), "selected"), + ("GTK", "4.14", (True, True), "refused"), + ("GTK", "4.14", (True, "timeout"), "refused"), + ("Qt", "6.6", "replaced", "stale"), + ("GTK", "3.24", "multiple", "selected"), + ) + for toolkit, version, counted, expected in cases: + rows = [FakeAccessible("list item", name) for name in ("Mouse", "Keyboard")] + listing = FakeAccessible("list", "Devices", children=rows) + selected = ["Mouse"] if counted == "multiple" else [] + listing.get_state_set = lambda: FakeStateSet(*([FakeStateType.MULTISELECTABLE] if counted == "multiple" else [])) + listing.get_selection_iface = lambda: listing + if counted == "replaced": + rows[1].get_index_in_parent = lambda: 0 + for row in rows: + row.get_state_set = lambda row=row: FakeStateSet(*([FakeStateType.SELECTED] if row.name in selected else [])) + def is_child_selected(_, index): + answer = (counted if isinstance(counted, tuple) else (True, False))[index - len(rows) + 1] + if answer is not True: + raise RuntimeError("Timeout was reached" if answer == "timeout" else "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs") + return False + selection = types.SimpleNamespace( + select_child=lambda _, index: selected.append(rows[index].name) or True, + clear_selection=lambda _: selected.clear() or True, + is_child_selected=is_child_selected) + with self.subTest(toolkit=toolkit, counted=counted), \ + SnapshotFixture(FakeAccessible("frame", "Settings", children=[listing])) as fixture, \ + patch.object(runtime.Atspi, "Selection", selection, create=True), \ + patch.object(runtime, "gtk4_client_origin", lambda *_: (0, 0)): + fixture.app.get_toolkit_name, fixture.app.get_toolkit_version = (lambda: toolkit), (lambda: version) + element = next(item for item in runtime.make_snapshot("Fixture", False)["elements"] if item["name"] == "Keyboard") + request = {"tool": "click", "app": "Fixture", "element": element, "noScreenshot": True} + if expected == "selected": + result = runtime.run_operation(request) + self.assertEqual(selected, ["Keyboard"]) + self.assertEqual(result["action"]["verification"], {"state": "verified", "property": "selection"}) + else: + with self.assertRaisesRegex(RuntimeError, "background_unavailable" if expected == "refused" else "stale element"): + runtime.run_operation(request) + self.assertEqual(selected, []) + def test_text_entry_inserts_into_the_focused_field_of_a_background_window(self): cases = ( ("type_text", "🚀 café", (6, 11), "Hello 🚀 café", 12), diff --git a/native/computer-use-macos/Sources/OrcaComputerUseMacOS/main.swift b/native/computer-use-macos/Sources/OrcaComputerUseMacOS/main.swift index 32fd7405f3b..6118b0cdbad 100644 --- a/native/computer-use-macos/Sources/OrcaComputerUseMacOS/main.swift +++ b/native/computer-use-macos/Sources/OrcaComputerUseMacOS/main.swift @@ -156,6 +156,7 @@ struct Snapshot { let windowBounds: CGRect let windowId: CGWindowID let windowLayer: Int + let windowVisibility: WindowVisibility let treeText: String let focusedElementId: Int? let screenshot: ScreenshotPayload? @@ -173,6 +174,7 @@ struct Snapshot { func showsSameState(as other: Snapshot) -> Bool { treeText == other.treeText && windowBounds == other.windowBounds && appWindowIds == other.appWindowIds + && windowVisibility == other.windowVisibility } func withoutScreenshotPayload() -> Snapshot { @@ -183,6 +185,7 @@ struct Snapshot { windowBounds: windowBounds, windowId: windowId, windowLayer: windowLayer, + windowVisibility: windowVisibility, treeText: treeText, focusedElementId: focusedElementId, screenshot: nil, @@ -212,6 +215,31 @@ struct CapturedImage { let engine: String } +/// Where an observed window is; only an on-screen one can take background pointer or key input. +enum WindowVisibility { + case onScreen + case minimized + case appHidden + case otherSpace + + var reason: String { + switch self { + case .onScreen: "is on screen" + case .minimized: "is minimized" + case .appHidden: "belongs to a hidden app" + case .otherSpace: "is on another Space" + } + } +} + +/// A window an agent can observe: on screen, or off screen with an accessibility element it can still read. +private struct ObservableWindow { + let candidate: WindowCandidate + /// Off-screen windows carry the element found for them; on-screen ones are matched later. + let element: AXUIElement? + let visibility: WindowVisibility +} + enum ScreenshotStatus { case captured case skipped @@ -224,9 +252,25 @@ private struct CachedSnapshotEntry { let createdAt: Date } +private struct OtherSpaceLookups { + var elements: [CGWindowID: AXUIElement] = [:] + var misses: [CGWindowID: Date] = [:] +} + +/// The app's observable windows, plus other-Space windows that exist but accessibility can't read yet. +private struct WindowInventory { + let windows: [ObservableWindow] + let unreadableOtherSpace: Set + /// Every window the app has, closed-but-kept ones included. + let windowIds: Set +} + final class Provider { private var snapshots: [String: Snapshot] = [:] private var snapshotEntries: [CachedSnapshotEntry] = [] + /// Per app, window elements already read (valid while the window lives) for when a window moves to another Space, + /// and when a remote-token lookup last missed, since a window becomes findable only once some client reads it. + private var otherSpaceLookups: [pid_t: OtherSpaceLookups] = [:] init() { // Why: the system-wide element sets the wait for every AX call in this process, so a hung app costs one bounded timeout whatever the macOS default. @@ -239,53 +283,65 @@ final class Provider { return providerHandshake() case "listApps": return ["apps": listApps().map(renderListedApp)] + case "launchApp": + return try launchApp(params: params) case "listWindows": return try listWindows(params: params) case "getAppState": + let params = isForegroundRequest(params) ? try recoverRequestedWindow(params: params) : params return try renderSnapshot(observe(params: params)) case "click": - return try actionResult(params: params) { try click(params: params) } + return try actionResult(params: params) { try click(params: $0) } case "performSecondaryAction": - return try actionResult(params: params) { try performSecondaryAction(params: params) } + return try actionResult(params: params) { try performSecondaryAction(params: $0) } case "setValue": - return try actionResult(params: params) { try setValue(params: params) } + return try actionResult(params: params) { try setValue(params: $0) } case "typeText": - return try actionResult(params: params) { try typeText(params: params) } + return try actionResult(params: params) { try typeText(params: $0) } case "pressKey": - return try actionResult(params: params) { try pressKey(params: params) } + return try actionResult(params: params) { try pressKey(params: $0) } case "hotkey": - return try actionResult(params: params) { try hotkey(params: params) } + return try actionResult(params: params) { try hotkey(params: $0) } case "pasteText": - return try actionResult(params: params) { try pasteText(params: params) } + return try actionResult(params: params) { try pasteText(params: $0) } case "scroll": - return try actionResult(params: params) { try scroll(params: params) } + return try actionResult(params: params) { try scroll(params: $0) } case "drag": - return try actionResult(params: params) { try drag(params: params) } + return try actionResult(params: params) { try drag(params: $0) } default: throw ProviderError.coded("invalid_argument", "unknown method '\(method)'") } } - private func actionResult(params: [String: JSONValue], action runAction: () throws -> [String: Any]) throws -> [String: Any] { - var action = try runAction() - // Why: only --restore-window activates the app or posts to the HID tap; every other route leaves the user's desktop alone. - action["delivery"] = isForegroundRequest(params) ? "foreground" : "background" + private func actionResult(params requestParams: [String: JSONValue], action runAction: ([String: JSONValue]) throws -> [String: Any]) throws -> [String: Any] { + let app = try resolveApp(try requiredString(requestParams, "app")) + // Why: the main run loop keeps NSRunningApplication current while requests run on connection threads. + let frontBefore = ForegroundDelivery.Observation( + frontmost: app.app.isActive, + focusedWindowID: app.app.isActive ? focusedWindowId(of: app) : nil + ) + // Why: raise once, before the action's reads, so the pre and post observations never raise again. + let params = isForegroundRequest(requestParams) ? try recoverRequestedWindow(params: requestParams) : requestParams + var action = try runAction(params) // Why: the action's currentSnapshot just cached this target's tree from before the action; // a subtree read can't be compared with the whole-window read that follows. let before = action["path"] as? String == "accessibility" && action["verification"] == nil ? (try? cachedSnapshot(params: params)).flatMap { $0.scope == nil ? $0 : nil } : nil + let snapshot: Snapshot do { let after = try observe(params: params) - guard let before, after.showsSameState(as: before) else { - return renderActionResult(action: action, snapshot: after) + if let before, after.showsSameState(as: before) { + if let changed = try observeLateChange(before: before, params: params) { + snapshot = changed + } else { + // Why: an AX call can succeed and do nothing, as AXPress does on many web controls (#31758). + action["verification"] = unverifiedAction(reason: "suspected_noop") + snapshot = after + } + } else { + snapshot = after } - if let changed = try observeLateChange(before: before, params: params) { - return renderActionResult(action: action, snapshot: changed) - } - // Why: an AX call can succeed and do nothing, as AXPress does on many web controls (#31758). - action["verification"] = unverifiedAction(reason: "suspected_noop") - return renderActionResult(action: action, snapshot: after) } catch let error as ProviderError where (error.code == "window_not_found" || error.code == "window_stale") && hasRequestedWindowSelector(params) { var fallbackParams = params fallbackParams.removeValue(forKey: "windowId") @@ -293,35 +349,130 @@ final class Provider { if action["verification"] == nil { action["verification"] = ["state": "unverified", "reason": "window_changed"] } - return try renderActionResult(action: action, snapshot: observe(params: fallbackParams)) + snapshot = try observe(params: fallbackParams) } + // Why: measured after the settle reads, since apps that activate themselves on AXPress (Electron, sheet openers) do it a turn later. + let frontAfter = frontmostObservation(of: app) + let tookForeground = ForegroundDelivery.tookForeground( + before: frontBefore, + after: frontAfter, + targetWindowID: snapshot.windowId, + restoreRequested: isForegroundRequest(params) + ) + action["delivery"] = tookForeground ? "foreground" : "background" + return renderActionResult(action: action, snapshot: snapshot) } /// Re-reads the target for about a second, since apps often apply an action on a later run-loop turn. /// Returns the observation to report once something changed, or nil if nothing did. private func observeLateChange(before: Snapshot, params: [String: JSONValue]) throws -> Snapshot? { - let settleParams = params.merging(["restoreWindow": .bool(false)]) { _, replacement in replacement } - let quietParams = settleParams.merging(["noScreenshot": .bool(true)]) { _, replacement in replacement } + let quietParams = params.merging(["noScreenshot": .bool(true)]) { _, replacement in replacement } let deadline = Date().addingTimeInterval(1) while Date() < deadline { Thread.sleep(forTimeInterval: 0.2) let polled = try observe(params: quietParams) if !polled.showsSameState(as: before) { if params["noScreenshot"]?.bool == true { return polled } - return try observe(params: settleParams) + return try observe(params: params) } } return nil } + /// Brings the requested window forward for --restore-window, once per request, and returns the params pinned + /// to that window: raising reorders the window list, so a --window-index could name another window afterwards. + private func recoverRequestedWindow(params: [String: JSONValue]) throws -> [String: JSONValue] { + let app = try resolveApp(try requiredString(params, "app")) + // Why: the window-index cache keys are not per app, so another app's snapshot can sit under the same index. + let cached = try cachedSnapshot(params: params).flatMap { $0.app.pid == app.pid ? $0 : nil } + // Why: a stale index fails after the raise, which would take over the user's screen for nothing. + try requireCachedElements(cached: cached, requestedIndexes: try requestedElementIndexes(params)) + let windows = windowInventory(app: app, appElement: AXUIElementCreateApplication(app.pid)).windows + let byIndex = try requestedWindowIndex(params).flatMap { index in + windows.filter { $0.candidate.layer == 0 }.dropFirst(index).first?.candidate.windowId + } + // Why: the cached snapshot is the window the agent's element indexes were read from, even if the index now names another. + let windowId = try requestedWindowId(params) ?? cached?.windowId ?? byIndex + let target = windowId.flatMap { id in windows.first { $0.candidate.windowId == id } } + if let requested = try requestedWindowId(params), target == nil { + throw ProviderError.coded("window_not_found", "window \(Int(requested)) of \(app.name) is closed or cannot be read, so nothing was raised; run list-windows and pick a listed window") + } + recoverWindow(app, windowId: windowId, windowBounds: target?.candidate.bounds ?? cached?.windowBounds, fallback: target?.element) + guard let windowId else { return params } + // Why: the raise reorders windows and focus, so later reads must not pick the target again; the id wins over + // the index when both are set, and the index still keys the snapshot cache. + var pinned = params + pinned["windowId"] = .number(Double(windowId)) + return pinned + } + + /// On-screen windows, plus minimized, hidden and other-Space windows that accessibility can still read. + private func windowInventory(app: AppDescriptor, appElement: AXUIElement) -> WindowInventory { + let all = WindowCapture.candidates(pid: app.pid, includeOffScreen: true) + let offScreen = all.filter { !$0.isOnScreen && $0.layer == 0 } + var elements: [CGWindowID: AXUIElement] = [:] + var unreadable = Set() + if !offScreen.isEmpty { + var listed: CFTypeRef? + let status = AXUIElementCopyAttributeValue(appElement, kAXWindowsAttribute as CFString, &listed) + for window in (listed as? [AXUIElement]) ?? [] { + if let id = windowNumber(window) { elements[id] = window } + } + let unlisted = offScreen.map(\.windowId).filter { elements[$0] == nil } + // Why: closed windows the app keeps belong to no Space; without SkyLight, try them all. + let inSpaces = WindowSpaces.windowsInAnySpace(unlisted) ?? Set(unlisted) + // Why: kAXWindowsAttribute leaves out windows on other Spaces; skip the lookup when the app stopped answering. + if status != .cannotComplete { + let live = Set(all.map(\.windowId)) + elements.merge(otherSpaceWindows(pid: app.pid, windowIds: inSpaces, live: live)) { listed, _ in listed } + } + unreadable = inSpaces.subtracting(elements.keys) + } + var appHidden: Bool? + let windows = all.compactMap { candidate -> ObservableWindow? in + if candidate.isOnScreen { + return ObservableWindow(candidate: candidate, element: nil, visibility: .onScreen) + } + guard candidate.layer == 0, let element = elements[candidate.windowId] else { return nil } + let visibility: WindowVisibility + if boolAttribute(element, kAXMinimizedAttribute as String) == true { + visibility = .minimized + } else { + if appHidden == nil { appHidden = boolAttribute(appElement, kAXHiddenAttribute as String) == true } + visibility = appHidden == true ? .appHidden : .otherSpace + } + return ObservableWindow(candidate: candidate, element: element, visibility: visibility) + } + return WindowInventory(windows: windows, unreadableOtherSpace: unreadable, windowIds: Set(all.map(\.windowId))) + } + + private func otherSpaceWindows(pid: pid_t, windowIds: Set, live: Set) -> [CGWindowID: AXUIElement] { + var lookups = otherSpaceLookups[pid] ?? OtherSpaceLookups() + lookups.elements = lookups.elements.filter { live.contains($0.key) && windowNumber($0.value) == $0.key } + lookups.misses = lookups.misses.filter { live.contains($0.key) } + let wanted = windowIds.filter { id in + lookups.elements[id] == nil && lookups.misses[id].map { Date().timeIntervalSince($0) >= 10 } ?? true + } + if !wanted.isEmpty { + let found = RemoteAccessibilityWindows.windows(pid: pid, wanted: wanted) + for id in wanted { + if let element = found[id] { + lookups.elements[id] = element + } else { + lookups.misses[id] = Date() + } + } + } + otherSpaceLookups = otherSpaceLookups.filter { pidIsLive($0.key) } + otherSpaceLookups[pid] = lookups + return lookups.elements.filter { windowIds.contains($0.key) } + } + private func observe(params: [String: JSONValue]) throws -> Snapshot { let query = try requiredString(params, "app") let windowId = try requestedWindowId(params) let windowIndex = try requestedWindowIndex(params) let app = try resolveApp(query) - if params["restoreWindow"]?.bool == true { - recoverWindow(app) - } let namespace = snapshotNamespace(params) pruneSnapshotCache() let subtree = try requestedSubtree(params: params, app: app) @@ -330,7 +481,6 @@ final class Provider { includeScreenshot: params["noScreenshot"]?.bool != true, windowId: subtree.map(\.base.windowId) ?? windowId, windowIndex: subtree == nil ? windowIndex : nil, - restoreWindow: params["restoreWindow"]?.bool == true, subtree: subtree, previousSnapshot: { [snapshots] resolvedWindowId in // Why: indexes carry over from this caller's own last view of exactly this window. @@ -511,11 +661,19 @@ final class Provider { } } - private func validateRequestedElements(cached: Snapshot?, current: Snapshot, requestedIndexes: [Int]) throws { + private func requireCachedElements(cached: Snapshot?, requestedIndexes: [Int]) throws { guard !requestedIndexes.isEmpty else { return } guard let cached else { throw ProviderError.coded("element_not_found", "element indexes require a fresh get-app-state snapshot for this app/window") } + for index in requestedIndexes where cached.elements[index] == nil { + throw ProviderError.coded("element_not_found", "element \(index) is stale; run get-app-state again and use a fresh element index") + } + } + + private func validateRequestedElements(cached: Snapshot?, current: Snapshot, requestedIndexes: [Int]) throws { + try requireCachedElements(cached: cached, requestedIndexes: requestedIndexes) + guard let cached else { return } for index in requestedIndexes { // Why: a subtree read also carries the window's earlier records, which prove nothing about the element now. guard let expected = cached.elements[index], @@ -578,6 +736,7 @@ final class Provider { "list": true, "bundleIds": true, "pids": true, + "launch": true, ], "windows": [ "list": true, @@ -646,13 +805,72 @@ final class Provider { } } + private func launchApp(params: [String: JSONValue]) throws -> [String: Any] { + let query = try requiredString(params, "app").trimmingCharacters(in: .whitespacesAndNewlines) + let activate = params["activate"]?.bool == true + // Why: Launch Services sends a running app a reopen event, and many apps answer it by opening a window. + if let running = listApps().first(where: { matches($0, query: query) }) { + try rejectBlockedApp(running) + return renderLaunch(running, launched: false) + } + guard let url = installedApplicationURL(query) else { + throw ProviderError.coded("app_not_found", "app '\(query)' is not running and no installed app has that name, bundle ID or path") + } + if let bundleId = Bundle(url: url)?.bundleIdentifier { + if blockedBundleIds.contains(bundleId) { + throw ProviderError.coded("app_blocked", "app '\(bundleId)' is blocked for safety") + } + // Why: a path or display-name query misses the running check above, and menu bar apps aren't listed. + if let running = NSRunningApplication.runningApplications(withBundleIdentifier: bundleId) + .first(where: { !$0.isTerminated }), + let app = appByPid(running.processIdentifier) { + return renderLaunch(app, launched: false) + } + } + let pid: pid_t + do { + pid = try BlockingAsync.run(timeout: 30, timeoutMessage: "launching '\(query)' timed out") { + let configuration = NSWorkspace.OpenConfiguration() + configuration.activates = activate + return try await NSWorkspace.shared.openApplication(at: url, configuration: configuration).processIdentifier + } + } catch let error as ProviderError { + throw error + } catch { + throw ProviderError.coded("app_launch_failed", "could not launch '\(query)': \(error.localizedDescription)") + } + guard let app = appByPid(pid) else { + throw ProviderError.coded("app_launch_failed", "'\(query)' exited right after it launched") + } + // Why: Launch Services reports the app once it checks in; activation, asked for or not, lands after it finishes launching. + let deadline = Date().addingTimeInterval(5) + while !app.app.isFinishedLaunching, !app.app.isTerminated, Date() < deadline { + Thread.sleep(forTimeInterval: 0.05) + } + if app.app.isTerminated { + throw ProviderError.coded("app_launch_failed", "'\(query)' quit while it was starting") + } + return renderLaunch(app, launched: true) + } + + private func renderLaunch(_ app: AppDescriptor, launched: Bool) -> [String: Any] { + // Why: measured, not taken from the request; an app may activate itself however it was started. + let frontmost = NSWorkspace.shared.frontmostApplication?.processIdentifier == app.pid + return [ + "app": ["name": app.name, "bundleId": jsonNullable(app.bundleId), "pid": Int(app.pid)], + "launched": launched, + "delivery": frontmost ? "foreground" : "background", + ] + } + private func listWindows(params: [String: JSONValue]) throws -> [String: Any] { let app = try resolveApp(try requiredString(params, "app")) - let windows = WindowCapture.candidates(pid: app.pid) - .filter { $0.layer == 0 } + let windows = windowInventory(app: app, appElement: AXUIElementCreateApplication(app.pid)).windows + .filter { $0.candidate.layer == 0 } .enumerated() - .map { index, candidate -> [String: Any] in - [ + .map { index, window -> [String: Any] in + let candidate = window.candidate + return [ "index": index, "app": [ "name": app.name, @@ -665,8 +883,8 @@ final class Provider { "y": Int(candidate.bounds.origin.y.rounded()), "width": Int(candidate.bounds.width.rounded()), "height": Int(candidate.bounds.height.rounded()), - "isMinimized": false, - "isOffscreen": !candidate.isOnScreen, + "isMinimized": window.visibility == .minimized, + "isOffscreen": window.visibility != .onScreen, "screenIndex": jsonNullable(screenIndex(for: candidate.bounds)), "isMain": NSNull(), "platform": [ @@ -696,7 +914,6 @@ final class Provider { includeScreenshot: Bool, windowId: CGWindowID?, windowIndex: Int?, - restoreWindow: Bool, subtree: (root: ElementRecord, base: Snapshot)?, previousSnapshot: (CGWindowID) -> Snapshot? ) throws -> Snapshot { @@ -716,27 +933,36 @@ final class Provider { throw AccessibilityMessaging.appNotResponding(app.name) } enableManualAccessibilityIfNeeded(appElement, app: app) - let windowCandidates = WindowCapture.candidates(pid: app.pid) - let focused = try focusedWindow( - appElement: appElement, - app: app, - visibleWindowCount: windowCandidates.count, - allowRecovery: restoreWindow - ) + let inventory = windowInventory(app: app, appElement: appElement) + let observable = inventory.windows + let focused = try focusedWindow(appElement: appElement, app: app, inventory: inventory) let focusedTitle = stringAttribute(focused, kAXTitleAttribute as String) ?? app.name let canCaptureScreenshot = includeScreenshot && screenCaptureTrustedSettled() guard let capture = WindowCapture.resolve( - candidates: windowCandidates, + candidates: observable.map(\.candidate), titleHint: focusedTitle, windowId: windowId, windowIndex: windowIndex, captureImage: canCaptureScreenshot ) else { - throw ProviderError.coded("window_not_found", "app '\(app.name)' has no on-screen window") + if let windowId, inventory.unreadableOtherSpace.contains(windowId) { + throw unreadableOtherSpaceError(app) + } + let reason = windowId != nil || windowIndex != nil ? "no window matching the requested selector; run list-windows and pick a listed one" : "no window to observe" + throw ProviderError.coded("window_not_found", "app '\(app.name)' has \(reason)") } - guard let window = matchingWindow(appElement: appElement, capture: capture, focused: focused, explicitTarget: windowId != nil || windowIndex != nil) else { + let target = observable.first { $0.candidate.windowId == capture.windowId } + let visibility = target?.visibility ?? .onScreen + guard let window = target?.element ?? matchingWindow(appElement: appElement, capture: capture, focused: focused, explicitTarget: windowId != nil || windowIndex != nil) else { throw ProviderError.coded("window_not_found", "could not match accessibility window to requested window; run get-app-state again or retry without a window selector") } + // Why: a window first read after a large tree walk gets a remote-token id past the lookup's range, so keep the element + // for when the window moves to another Space. + otherSpaceLookups = otherSpaceLookups.filter { pidIsLive($0.key) } + var lookups = otherSpaceLookups[app.pid] ?? OtherSpaceLookups() + lookups.elements = lookups.elements.filter { inventory.windowIds.contains($0.key) } + lookups.elements[capture.windowId] = window + otherSpaceLookups[app.pid] = lookups let title = stringAttribute(window, kAXTitleAttribute as String) ?? capture.title ?? app.name let renderer = TreeRenderer( windowBounds: capture.bounds, @@ -771,6 +997,8 @@ final class Provider { .captured } else if includeScreenshot && !canCaptureScreenshot { .failed("Screen Recording permission is required for Orca Computer Use; grant permission or pass --no-screenshot to inspect accessibility state only.") + } else if includeScreenshot && visibility != .onScreen { + .failed("the window \(visibility.reason), so it has no pixels to capture; the accessibility tree is still current. Pass --no-screenshot to skip the capture.") } else if includeScreenshot { .failed("window screenshot capture returned no image; retry with --no-screenshot if accessibility state is sufficient.") } else { @@ -783,6 +1011,7 @@ final class Provider { windowBounds: capture.bounds, windowId: capture.windowId, windowLayer: capture.layer, + windowVisibility: visibility, treeText: renderTreeText(app: app, title: title, rootIndex: subtree?.root.index, lines: renderer.lines, focused: renderer.focusedSummary), focusedElementId: renderer.focusedElementId, screenshot: screenshot, @@ -795,7 +1024,7 @@ final class Provider { truncated: renderer.truncated, maxDepthReached: renderer.maxDepthReached, scope: subtree.map { SnapshotScope(rootIndex: $0.root.index, walkedIndexes: Set(indexes.order)) }, - appWindowIds: windowCandidates.map(\.windowId).sorted() + appWindowIds: observable.filter { $0.visibility == .onScreen }.map(\.candidate.windowId).sorted() ) } @@ -824,8 +1053,8 @@ final class Provider { "y": Int(snapshot.windowBounds.origin.y.rounded()), "width": Int(snapshot.windowBounds.width.rounded()), "height": Int(snapshot.windowBounds.height.rounded()), - "isMinimized": false, - "isOffscreen": false, + "isMinimized": snapshot.windowVisibility == .minimized, + "isOffscreen": snapshot.windowVisibility != .onScreen, "screenIndex": jsonNullable(screenIndex(for: snapshot.windowBounds)), "platform": [ "layer": snapshot.windowLayer, @@ -872,9 +1101,6 @@ final class Provider { } let modifiers = try KeyMap.parseModifiers(params["modifiers"]?.string) let foreground = isForegroundRequest(params) - if foreground { - recoverWindow(snapshot.app, windowId: snapshot.windowId, windowBounds: snapshot.windowBounds) - } if let elementIndex = try optionalInteger(params, "elementIndex") { let record = try element(snapshot, elementIndex) try requireEnabled(record, snapshot: snapshot) @@ -897,6 +1123,9 @@ final class Provider { actionName: actionName ) } + if !foreground { + try requireBackgroundInputReachable(snapshot, input: "pointer click") + } let point = try pointerTarget(record, snapshot: snapshot) try clickPointer(at: point, button: button, count: count, modifiers: modifiers, snapshot: snapshot, foreground: foreground, element: record.element) guard unselectedRow else { @@ -912,6 +1141,9 @@ final class Provider { actionName: nil ) } + if !foreground { + try requireBackgroundInputReachable(snapshot, input: "pointer click") + } let point = try coordinatePoint(params: params, xKey: "x", yKey: "y", snapshot: snapshot) try clickPointer(at: point, button: button, count: count, modifiers: modifiers, snapshot: snapshot, foreground: foreground) return actionMetadata( @@ -1125,6 +1357,10 @@ final class Provider { return actionMetadata(path: "accessibility", actionName: "AXReplaceSelection", verification: verification) } let pasteKeys = try Input.keyEvents("cmd+v") + if !isForegroundRequest(params) { + // Why: refuse before the clipboard is replaced, so clipboard history never records text that was not sent. + try requireBackgroundInputReachable(snapshot, input: "paste") + } let insertion = TextInput.Insertion(into: focused, text: text) try Input.withPasteboardText(text) { try deliverKeys(pasteKeys, snapshot: snapshot, params: params) @@ -1186,6 +1422,7 @@ final class Provider { try requireTargetWindowFocused(snapshot, restoreWindowRequested: true) try Input.drag(from: start, to: end, targetWindow: snapshot) } else { + try requireBackgroundInputReachable(snapshot, input: "drag") // Why: the mouse-down lands on whatever is under the start point, which can open a menu over the user. guard let startElement = elementAtPoint(start, pid: snapshot.app.pid) ?? fromRecord?.element else { throw unidentifiedBackgroundTarget @@ -1293,6 +1530,28 @@ private func parsePid(_ query: String) -> pid_t? { return pid } +/// An app that isn't running, by absolute path, bundle ID or bundle name in the usual install folders. +private func installedApplicationURL(_ query: String) -> URL? { + let path = (query as NSString).expandingTildeInPath + if path.hasPrefix("/") { + return FileManager.default.fileExists(atPath: path) ? URL(fileURLWithPath: path) : nil + } + if let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: query) { + return url + } + let bundleName = query.lowercased().hasSuffix(".app") ? query : "\(query).app" + let folders = [ + "/Applications", + "/Applications/Utilities", + "/System/Applications", + "/System/Applications/Utilities", + NSHomeDirectory() + "/Applications", + ] + return folders + .map { URL(fileURLWithPath: $0).appendingPathComponent(bundleName) } + .first { FileManager.default.fileExists(atPath: $0.path) } +} + private func matches(_ app: AppDescriptor, query: String) -> Bool { app.name.caseInsensitiveCompare(query) == .orderedSame || app.bundleId?.caseInsensitiveCompare(query) == .orderedSame @@ -1357,18 +1616,13 @@ private func enableManualAccessibilityIfNeeded(_ appElement: AXUIElement, app: A } } -private func focusedWindow(appElement: AXUIElement, app: AppDescriptor, visibleWindowCount: Int, allowRecovery: Bool) throws -> AXUIElement { +private func focusedWindow(appElement: AXUIElement, app: AppDescriptor, inventory: WindowInventory) throws -> AXUIElement { + let observable = inventory.windows let systemWide = AXUIElementCreateSystemWide() if let window = lookupUsableWindow(systemWide: systemWide, appElement: appElement, app: app) { return window } - if allowRecovery { - recoverWindow(app) - if let window = lookupUsableWindow(systemWide: systemWide, appElement: appElement, app: app) { - return window - } - } - if visibleWindowCount > 0 { + if observable.contains(where: { $0.visibility == .onScreen }) { var settledWindow: AXUIElement? let outcome = PermissionTrustSettling.settle { settledWindow = lookupUsableWindow( @@ -1381,12 +1635,26 @@ private func focusedWindow(appElement: AXUIElement, app: AppDescriptor, visibleW if let window = settledWindow, outcome.settled { return window } - throw ProviderError.coded("permission_denied", "app '\(app.name)' has visible windows but no accessibility window (AX reads stayed blocked for \(outcome.waitedMs)ms after retries). macOS Accessibility may need Orca Computer Use toggled off and on again in System Settings.") + var windows: CFTypeRef? + if AXUIElementCopyAttributeValue(appElement, kAXWindowsAttribute as CFString, &windows) == .apiDisabled { + throw ProviderError.coded("permission_denied", "app '\(app.name)' has visible windows but macOS refuses accessibility reads (AX reads stayed blocked for \(outcome.waitedMs)ms after retries). macOS Accessibility may need Orca Computer Use toggled off and on again in System Settings.") + } } - if allowRecovery { - throw ProviderError.coded("window_not_found", "app '\(app.name)' has no accessibility window even after --restore-window; ask the user to open or show one of its windows.") + let offScreen = observable.filter { $0.element != nil } + if let window = (offScreen.first { $0.visibility != .minimized } ?? offScreen.first)?.element { + return window } - throw ProviderError.coded("window_not_found", "app '\(app.name)' has no accessibility window; it may have none open, or they may be minimized, hidden or on another Space. Ask the user to bring one on screen, or ask the user before retrying with --restore-window.") + if observable.contains(where: { $0.visibility == .onScreen }) { + throw ProviderError.coded("window_not_found", "app '\(app.name)' has visible windows but exposes no accessibility window; it may still be starting up. Run get-app-state again shortly.") + } + if !inventory.unreadableOtherSpace.isEmpty { + throw unreadableOtherSpaceError(app) + } + throw ProviderError.coded("window_not_found", "app '\(app.name)' has no open window, counting minimized, hidden and other-Space windows; open one in the app first.") +} + +private func unreadableOtherSpaceError(_ app: AppDescriptor) -> ProviderError { + .coded("window_not_found", "app '\(app.name)' has a window on another Space that accessibility cannot read yet: macOS leaves other-Space windows (full-screen ones included) out of accessibility until something has read them once. Ask the user to show that window (switch to its Space) and run get-app-state while it is showing, or ask the user before retrying with --restore-window.") } private func lookupUsableWindow( @@ -1628,6 +1896,21 @@ private func isForegroundRequest(_ params: [String: JSONValue]) -> Bool { params["restoreWindow"]?.bool == true } +/// Refuses, before anything is sent, background pointer or key input the window cannot receive while off screen. +private func requireBackgroundInputReachable(_ snapshot: Snapshot, input: String) throws { + guard snapshot.windowVisibility != .onScreen else { return } + throw ProviderError.coded("background_unavailable", "window \(Int(snapshot.windowId)) of \(snapshot.app.name) \(snapshot.windowVisibility.reason), so background \(input) cannot reach it without bringing it forward; nothing was sent. Use its elements instead (click --element-index, set-value, type-text --element-index). Ask the user before retrying with --restore-window.") +} + +private func frontmostObservation(of app: AppDescriptor) -> ForegroundDelivery.Observation { + let frontmost = boolAttribute(AXUIElementCreateApplication(app.pid), kAXFrontmostAttribute as String) == true + return ForegroundDelivery.Observation(frontmost: frontmost, focusedWindowID: frontmost ? focusedWindowId(of: app) : nil) +} + +private func focusedWindowId(of app: AppDescriptor) -> CGWindowID? { + copyElement(AXUIElementCreateApplication(app.pid), kAXFocusedWindowAttribute as String).flatMap(windowNumber) +} + private func backgroundTarget(_ snapshot: Snapshot) -> BackgroundInputTransport.Target { BackgroundInputTransport.Target(pid: snapshot.app.pid, windowID: snapshot.windowId, windowFrame: snapshot.windowBounds) } @@ -1737,6 +2020,7 @@ private func scrollPointer(at point: CGPoint, direction: String, pages: Double, try Input.scroll(at: point, wheel1: wheel1, wheel2: wheel2, targetWindow: snapshot) return } + try requireBackgroundInputReachable(snapshot, input: "scroll") try withTransportErrors { try BackgroundInputTransport.scroll(backgroundTarget(snapshot), at: point, wheel1: wheel1, wheel2: wheel2) } @@ -1750,6 +2034,7 @@ private func deliverKeys(_ events: [CGEvent], snapshot: Snapshot, params: [Strin } return } + try requireBackgroundInputReachable(snapshot, input: "key input") try withTransportErrors { try BackgroundInputTransport.sendKeys(events, to: backgroundTarget(snapshot)) } @@ -1786,16 +2071,14 @@ private func matchingWindow(appElement: AXUIElement, capture: WindowCapture, foc } ?? focused } +/// Brings only the target window forward: no other window of the app, and no LaunchServices reopen that can open a new one. private func recoverWindow( _ app: AppDescriptor, - windowId: CGWindowID? = nil, - windowBounds: CGRect? = nil + windowId: CGWindowID?, + windowBounds: CGRect?, + fallback: AXUIElement? ) { _ = app.app.unhide() - _ = app.app.activate(options: [.activateAllWindows]) - if let bundleId = app.bundleId { - openBundle(bundleId) - } let appElement = AXUIElementCreateApplication(app.pid) let focusedWindow = copyElement(appElement, kAXFocusedWindowAttribute as String) var cachedWindows: [AXUIElement]? @@ -1805,26 +2088,23 @@ private func recoverWindow( cachedWindows = value return value } - let targetWindow: AXUIElement? - if let focusedWindow, - (windowId == nil && windowBounds == nil || windowMatchesCapture( - focusedWindow, - windowId: windowId, - windowBounds: windowBounds - )) { - targetWindow = focusedWindow - } else { - let exactWindow = windowId.flatMap { targetId in - windows().first { windowNumber($0) == targetId } - } - targetWindow = exactWindow ?? windowBounds.flatMap { targetBounds in - windows().first { window in - absoluteFrame(window).map { windowFramesMatch($0, targetBounds) } == true - } + // Why: an exact id wins over a frame match, since full-screen and minimized windows can share the focused window's frame. + let exactWindow = windowId.flatMap { targetId in + windows().first { windowNumber($0) == targetId } + } ?? fallback + let sameFrameWindow = windowBounds.flatMap { targetBounds in + windows().first { window in + absoluteFrame(window).map { windowFramesMatch($0, targetBounds) } == true } } - if let window = targetWindow ?? focusedWindow ?? windows().first { + let window = exactWindow ?? sameFrameWindow ?? focusedWindow ?? windows().first + if let window { + // Why: un-minimize first, so activating the app has the target to bring forward. _ = AXUIElementSetAttributeValue(window, kAXMinimizedAttribute as CFString, kCFBooleanFalse) + } + _ = app.app.activate(options: []) + _ = AXUIElementSetAttributeValue(appElement, kAXFrontmostAttribute as CFString, kCFBooleanTrue) + if let window { _ = AXUIElementPerformAction(window, kAXRaiseAction as CFString) _ = AXUIElementSetAttributeValue(window, kAXMainAttribute as CFString, kCFBooleanTrue) _ = AXUIElementSetAttributeValue(window, kAXFocusedAttribute as CFString, kCFBooleanTrue) @@ -1832,16 +2112,6 @@ private func recoverWindow( Thread.sleep(forTimeInterval: 0.4) } -private func windowMatchesCapture( - _ window: AXUIElement, - windowId: CGWindowID?, - windowBounds: CGRect? -) -> Bool { - if let windowId, windowNumber(window) == windowId { return true } - guard let windowBounds, let frame = absoluteFrame(window) else { return false } - return windowFramesMatch(frame, windowBounds) -} - private func windowFramesMatch(_ lhs: CGRect, _ rhs: CGRect) -> Bool { let tolerance: CGFloat = 2 return abs(lhs.minX - rhs.minX) <= tolerance && @@ -1850,16 +2120,6 @@ private func windowFramesMatch(_ lhs: CGRect, _ rhs: CGRect) -> Bool { abs(lhs.height - rhs.height) <= tolerance } -private func openBundle(_ bundleId: String) { - let process = Process() - process.executableURL = URL(fileURLWithPath: "/usr/bin/open") - process.arguments = ["-b", bundleId] - process.standardOutput = Pipe() - process.standardError = Pipe() - try? process.run() - process.waitUntilExit() -} - private func hasRequestedWindowSelector(_ params: [String: JSONValue]) -> Bool { params["windowId"] != nil || params["windowIndex"] != nil } @@ -2969,7 +3229,6 @@ private struct WindowCandidate { var value = Int(bounds.width * bounds.height) if layer == 0 { value += 1_000_000_000 } if title != nil && title?.isEmpty == false { value += 10_000_000 } - if isOnScreen { value += 1_000_000 } if alpha >= 0.99 { value += 100_000 } return value } @@ -3015,6 +3274,8 @@ private struct WindowCapture { return WindowCapture(candidate: visibleWindows[windowIndex], captureImage: captureImage) } guard let best = candidates.sorted(by: { lhs, rhs in + // Why: the focused window's title can name a window on another Space; a visible window still comes first. + if lhs.isOnScreen != rhs.isOnScreen { return lhs.isOnScreen } if let titleHint, lhs.title == titleHint, rhs.title != titleHint { return true } if let titleHint, rhs.title == titleHint, lhs.title != titleHint { return false } return lhs.score > rhs.score @@ -3038,8 +3299,10 @@ private struct WindowCapture { } } - static func candidates(pid: pid_t) -> [WindowCandidate] { - guard let infos = CGWindowListCopyWindowInfo([.optionOnScreenOnly], kCGNullWindowID) as? [[String: Any]] else { + /// Off-screen windows include minimized, hidden and other-Space ones, but also closed windows apps keep; see `windowInventory`. + static func candidates(pid: pid_t, includeOffScreen: Bool = false) -> [WindowCandidate] { + let options: CGWindowListOption = includeOffScreen ? [.optionAll, .excludeDesktopElements] : [.optionOnScreenOnly] + guard let infos = CGWindowListCopyWindowInfo(options, kCGNullWindowID) as? [[String: Any]] else { return [] } return infos.compactMap { info in @@ -3057,7 +3320,8 @@ private struct WindowCapture { guard alpha > 0.01 else { return nil } let sharing = (info[kCGWindowSharingState as String] as? NSNumber).map { $0.intValue } if sharing == 0 { return nil } - let isOnScreen = (info[kCGWindowIsOnscreen as String] as? Bool) ?? true + // Why: CoreGraphics omits the key for off-screen windows, and every window of an on-screen-only list is on screen. + let isOnScreen = (info[kCGWindowIsOnscreen as String] as? Bool) ?? !includeOffScreen return WindowCandidate( windowId: CGWindowID(number.uint32Value), layer: layer, @@ -3120,7 +3384,11 @@ private final class AsyncBox: @unchecked Sendable { } private enum BlockingAsync { - static func run(timeout: TimeInterval, operation: @escaping @Sendable () async throws -> T) throws -> T { + static func run( + timeout: TimeInterval, + timeoutMessage: String = "screenshot capture timed out", + operation: @escaping @Sendable () async throws -> T + ) throws -> T { let semaphore = DispatchSemaphore(value: 0) let box = AsyncBox() let task = Task.detached { @@ -3133,7 +3401,7 @@ private enum BlockingAsync { } guard semaphore.wait(timeout: .now() + timeout) == .success else { task.cancel() - throw ProviderError.coded("action_timeout", "screenshot capture timed out") + throw ProviderError.coded("action_timeout", timeoutMessage) } return try box.result!.get() } diff --git a/native/computer-use-macos/Sources/OrcaComputerUseMacOSCore/ForegroundDelivery.swift b/native/computer-use-macos/Sources/OrcaComputerUseMacOSCore/ForegroundDelivery.swift new file mode 100644 index 00000000000..8609214b27a --- /dev/null +++ b/native/computer-use-macos/Sources/OrcaComputerUseMacOSCore/ForegroundDelivery.swift @@ -0,0 +1,27 @@ +import CoreGraphics + +/// Decides from what happened, not from the request, whether an action put the target in front of the user. +public enum ForegroundDelivery { + public struct Observation: Equatable { + public let frontmost: Bool + /// Read only while the app is frontmost, since only then is its key window the user's. + public let focusedWindowID: CGWindowID? + + public init(frontmost: Bool, focusedWindowID: CGWindowID?) { + self.frontmost = frontmost + self.focusedWindowID = focusedWindowID + } + } + + public static func tookForeground( + before: Observation, + after: Observation, + targetWindowID: CGWindowID, + restoreRequested: Bool + ) -> Bool { + guard after.frontmost else { return false } + // Why: --restore-window input goes through the HID tap, which lands in whatever is in front. + if restoreRequested || !before.frontmost { return true } + return before.focusedWindowID != targetWindowID && after.focusedWindowID == targetWindowID + } +} diff --git a/native/computer-use-macos/Sources/OrcaComputerUseMacOSCore/RemoteAccessibilityWindows.swift b/native/computer-use-macos/Sources/OrcaComputerUseMacOSCore/RemoteAccessibilityWindows.swift new file mode 100644 index 00000000000..131030b1b87 --- /dev/null +++ b/native/computer-use-macos/Sources/OrcaComputerUseMacOSCore/RemoteAccessibilityWindows.swift @@ -0,0 +1,56 @@ +import ApplicationServices +import CoreGraphics +import Darwin +import Foundation + +/// Finds an app's accessibility windows on other Spaces, which `kAXWindowsAttribute` leaves out, +/// by rebuilding element references from remote tokens (the technique AltTab uses). +/// Only finds windows some accessibility client has read before: an app numbers its elements in the order it first hands them out. +public enum RemoteAccessibilityWindows { + /// Windows are usually handed out first (ids 42-73 measured in apps open for days); a window first read after + /// a large tree walk gets a later id. + static let elementIdLimit: UInt64 = 20_000 + /// About 12 µs per id; the budget bounds a slow app. + static let scanBudgetSeconds: CFAbsoluteTime = 0.5 + + /// The windows of `pid` whose window ids are in `wanted`. + public static func windows(pid: pid_t, wanted: Set) -> [CGWindowID: AXUIElement] { + guard let createWithRemoteToken, !wanted.isEmpty else { return [:] } + let deadline = CFAbsoluteTimeGetCurrent() + scanBudgetSeconds + var found: [CGWindowID: AXUIElement] = [:] + for elementId in 0.. Data { + var token = Data() + withUnsafeBytes(of: pid.littleEndian) { token.append(contentsOf: $0) } + withUnsafeBytes(of: Int32(0)) { token.append(contentsOf: $0) } + withUnsafeBytes(of: Int32(0x636F_636F).littleEndian) { token.append(contentsOf: $0) } + withUnsafeBytes(of: elementId.littleEndian) { token.append(contentsOf: $0) } + return token + } + + private typealias CreateWithRemoteToken = @convention(c) (CFData) -> Unmanaged? + + private static let createWithRemoteToken: CreateWithRemoteToken? = { + guard let symbol = dlsym(UnsafeMutableRawPointer(bitPattern: -2), "_AXUIElementCreateWithRemoteToken") else { return nil } + // SAFETY: HIServices exports AXUIElementRef _AXUIElementCreateWithRemoteToken(CFDataRef), returning a +1 reference. + return unsafeBitCast(symbol, to: CreateWithRemoteToken.self) + }() +} diff --git a/native/computer-use-macos/Sources/OrcaComputerUseMacOSCore/WindowSpaces.swift b/native/computer-use-macos/Sources/OrcaComputerUseMacOSCore/WindowSpaces.swift new file mode 100644 index 00000000000..91a9889454a --- /dev/null +++ b/native/computer-use-macos/Sources/OrcaComputerUseMacOSCore/WindowSpaces.swift @@ -0,0 +1,40 @@ +import CoreGraphics +import Darwin +import Foundation + +/// Which windows belong to a Space. Minimized, hidden and other-Space windows do; a closed window an app keeps around does not. +public enum WindowSpaces { + /// Nil when SkyLight's lookup is missing, so closed windows can't be told apart from other-Space ones. + public static func windowsInAnySpace(_ windowIDs: [CGWindowID]) -> Set? { + guard let symbols else { return nil } + let connection = symbols.mainConnection() + return Set(windowIDs.filter { windowID in + let spaces = symbols.copySpacesForWindows(connection, allSpacesMask, [NSNumber(value: windowID)] as CFArray)? + .takeRetainedValue() as? [NSNumber] + return spaces?.isEmpty == false + }) + } + + private static let allSpacesMask: Int32 = 0x7 + + private typealias MainConnectionID = @convention(c) () -> Int32 + private typealias CopySpacesForWindows = @convention(c) (Int32, Int32, CFArray) -> Unmanaged? + + private struct Symbols { + let mainConnection: MainConnectionID + let copySpacesForWindows: CopySpacesForWindows + } + + private static let symbols: Symbols? = { + let skyLight = dlopen("/System/Library/PrivateFrameworks/SkyLight.framework/SkyLight", RTLD_LAZY) + guard let main = dlsym(skyLight, "SLSMainConnectionID"), + let copy = dlsym(skyLight, "SLSCopySpacesForWindows") + else { return nil } + // SAFETY: SkyLight exports CGSConnectionID SLSMainConnectionID(void) and + // CFArrayRef SLSCopySpacesForWindows(CGSConnectionID, CGSSpaceMask, CFArrayRef), returning a +1 array (as yabai uses them). + return Symbols( + mainConnection: unsafeBitCast(main, to: MainConnectionID.self), + copySpacesForWindows: unsafeBitCast(copy, to: CopySpacesForWindows.self) + ) + }() +} diff --git a/native/computer-use-macos/Tests/OrcaComputerUseMacOSTests/ForegroundDeliveryTests.swift b/native/computer-use-macos/Tests/OrcaComputerUseMacOSTests/ForegroundDeliveryTests.swift new file mode 100644 index 00000000000..193b6f5f727 --- /dev/null +++ b/native/computer-use-macos/Tests/OrcaComputerUseMacOSTests/ForegroundDeliveryTests.swift @@ -0,0 +1,25 @@ +import CoreGraphics +import XCTest +@testable import OrcaComputerUseMacOSCore + +final class ForegroundDeliveryTests: XCTestCase { + private func observation(_ frontmost: Bool, _ focusedWindowID: CGWindowID? = nil) -> ForegroundDelivery.Observation { + ForegroundDelivery.Observation(frontmost: frontmost, focusedWindowID: focusedWindowID) + } + + func testBackgroundActionThatActivatesTheAppReportsForeground() { + XCTAssertTrue(ForegroundDelivery.tookForeground(before: observation(false), after: observation(true, 7), targetWindowID: 7, restoreRequested: false)) + XCTAssertFalse(ForegroundDelivery.tookForeground(before: observation(false), after: observation(false), targetWindowID: 7, restoreRequested: false)) + } + + func testActiveAppCountsOnlyWhenTheTargetWindowBecameKey() { + XCTAssertTrue(ForegroundDelivery.tookForeground(before: observation(true, 3), after: observation(true, 7), targetWindowID: 7, restoreRequested: false)) + XCTAssertFalse(ForegroundDelivery.tookForeground(before: observation(true, 7), after: observation(true, 7), targetWindowID: 7, restoreRequested: false)) + XCTAssertFalse(ForegroundDelivery.tookForeground(before: observation(true, 3), after: observation(true, 3), targetWindowID: 7, restoreRequested: false)) + } + + func testRestoreReportsWhatHappenedNotWhatWasAsked() { + XCTAssertTrue(ForegroundDelivery.tookForeground(before: observation(true, 7), after: observation(true, 7), targetWindowID: 7, restoreRequested: true)) + XCTAssertFalse(ForegroundDelivery.tookForeground(before: observation(false), after: observation(false), targetWindowID: 7, restoreRequested: true)) + } +} diff --git a/native/computer-use-windows/runtime-native-controls.test.ps1 b/native/computer-use-windows/runtime-native-controls.test.ps1 new file mode 100644 index 00000000000..23318a18341 --- /dev/null +++ b/native/computer-use-windows/runtime-native-controls.test.ps1 @@ -0,0 +1,117 @@ +param([string]$RuntimePath = (Join-Path $PSScriptRoot "runtime.ps1")) + +$ErrorActionPreference = "Stop" + +function Assert-TestEqual($Actual, $Expected, [string]$Label) { + if ($Actual -cne $Expected) { throw "$Label expected '$Expected', received '$Actual'" } +} + +Add-Type -TypeDefinition @" +using System; +using System.Collections.Generic; + +// A combo box and a list box in a fake process 4242, answering the messages the runtime sends. +public static class OrcaDesktopWin32 { + public static readonly List Calls = new List(); + public static string[] Items; + public static int Selected; + public static int Top; + public static int ListCount; + public static int ClientHeight; + + public static void Reset(string[] items) { + Calls.Clear(); + Items = items; + Selected = 0; + Top = 0; + ListCount = 100; + ClientHeight = 60; + } + public static bool IsWindow(IntPtr hwnd) { return true; } + public static uint GetWindowThreadProcessId(IntPtr hwnd, out uint processId) { processId = 4242; return 1; } + public static int GetWindowLong(IntPtr hwnd, int index) { return 0; } + public static IntPtr GetParent(IntPtr hwnd) { return new IntPtr(101); } + public static int GetDlgCtrlID(IntPtr hwnd) { return 7; } + public static int GetClientHeight(IntPtr hwnd) { return ClientHeight; } + // CB_FINDSTRINGEXACT: case-insensitive, searching after start and wrapping. + public static long? FindControlString(IntPtr hwnd, uint message, int start, string name) { + for (var step = 1; step <= Items.Length; step++) { + var index = (start + step + Items.Length) % Items.Length; + if (string.Equals(Items[index], name, StringComparison.OrdinalIgnoreCase)) { return index; } + } + return -1; + } + public static string GetControlItemText(IntPtr hwnd, uint lengthMessage, uint textMessage, int index) { return Items[index]; } + public static long? SendControlMessage(IntPtr hwnd, uint message, IntPtr wParam, IntPtr lParam) { + switch (message) { + case 0x0146: return Items.Length; + case 0x0147: return Selected; + case 0x014E: Selected = wParam.ToInt32(); Calls.Add("select:" + Selected); return Selected; + case 0x0111: Calls.Add("command:" + hwnd + ":" + wParam.ToInt64().ToString("X")); return 0; + case 0x018E: return Top; + case 0x018B: return ListCount; + case 0x01A1: return 15; + case 0x0197: Top = wParam.ToInt32(); Calls.Add("top:" + Top); return 0; + default: return 0; + } + } +} +"@ + +$tokens = $null +$errors = $null +$ast = [System.Management.Automation.Language.Parser]::ParseFile($RuntimePath, [ref]$tokens, [ref]$errors) +if ($errors.Count) { throw ($errors | Out-String) } +$functions = @( + "Test-OrcaNativeClass", "Test-OrcaEditClass", "Get-OrcaNativeControl", "Assert-OrcaWindowOwner", "Get-OrcaProperty", + "Set-OrcaNativeComboValue", "Set-OrcaElementValue", "Invoke-OrcaNativeListScroll", "Test-OrcaSameRuntimeId" +) +foreach ($definition in $ast.FindAll({ param($node) $node -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $false)) { + if ($definition.Name -in $functions) { . ([scriptblock]::Create($definition.Extent.Text)) } +} + +$process = [pscustomobject]@{ Id = 4242 } +function Get-OrcaRuntimeId($Element) { @($Element.id) } +function Test-OrcaElementDisabled($Element) { -not $Element.Current.IsEnabled } +function Set-OrcaNativeEditValue { $null } +function New-TestControl([string]$ClassName, [bool]$Enabled = $true) { + [pscustomobject]@{ id = 5; Current = [pscustomobject]@{ ClassName = $ClassName; NativeWindowHandle = 303; IsEnabled = $Enabled } } +} +$combo = New-TestControl "WindowsForms10.COMBOBOX.app.0.1" + +# The exact-case option is picked among case-insensitive hits, and the app's change handler is told. +[OrcaDesktopWin32]::Reset(@("Alpha", "gamma", "Gamma")) +$written = Set-OrcaElementValue $combo $process 5 "Gamma" +Assert-TestEqual ([OrcaDesktopWin32]::Calls -join "|") "select:2|command:101:10007" "combo selection messages" +Assert-TestEqual $written.verification.state "verified" "combo verification" + +# Two options with the exact name can't be told apart, so nothing is selected. +[OrcaDesktopWin32]::Reset(@("Gamma", "Beta", "Gamma")) +$message = try { Set-OrcaElementValue $combo $process 5 "Gamma" | Out-Null; $null } catch { $_.Exception.Message } +if ($message -notlike "*more than one option is named 'Gamma'*") { throw "ambiguous combo, received '$message'" } +Assert-TestEqual ([OrcaDesktopWin32]::Calls.Count) 0 "ambiguous combo sends nothing" + +# A disabled combo box is locked against the user, so it is refused before any message. +[OrcaDesktopWin32]::Reset(@("Alpha", "Gamma")) +$message = try { Set-OrcaElementValue (New-TestControl "ComboBox" $false) $process 5 "Gamma" | Out-Null; $null } catch { $_.Exception.Message } +Assert-TestEqual $message "element_not_enabled: element 5 is disabled, so no input was sent; do what enables it first, then run get-app-state and retry once its line no longer lists disabled" "disabled combo" +Assert-TestEqual ([OrcaDesktopWin32]::Calls.Count) 0 "disabled combo sends nothing" + +# A page is the rows the list shows: 60 px at 15 px a row is 4 rows. +$list = New-TestControl "ListBox" +[OrcaDesktopWin32]::Reset(@()) +Assert-TestEqual (Invoke-OrcaNativeListScroll $list $list $process "down" 2) $true "list scroll" +Assert-TestEqual ([OrcaDesktopWin32]::Calls -join "|") "top:8" "list scroll top row" + +# At the end already, there is nothing to move and nothing falls back to ScrollPattern. +[OrcaDesktopWin32]::Reset(@()) +[OrcaDesktopWin32]::Top = 96 +Assert-TestEqual (Invoke-OrcaNativeListScroll $list $list $process "down" 1) $true "list at its end" +Assert-TestEqual ([OrcaDesktopWin32]::Calls.Count) 0 "list at its end sends nothing" + +# A list that shows all its rows leaves the scroll to an enclosing ScrollPattern. +[OrcaDesktopWin32]::Reset(@()) +[OrcaDesktopWin32]::ListCount = 3 +Assert-TestEqual (Invoke-OrcaNativeListScroll $list $list $process "down" 1) $false "short list" +Assert-TestEqual ([OrcaDesktopWin32]::Calls.Count) 0 "short list sends nothing" +Write-Output "windows-native-controls-tests-ok" diff --git a/native/computer-use-windows/runtime-pointer.test.ps1 b/native/computer-use-windows/runtime-pointer.test.ps1 index 09e97aee287..655f7b979c3 100644 --- a/native/computer-use-windows/runtime-pointer.test.ps1 +++ b/native/computer-use-windows/runtime-pointer.test.ps1 @@ -107,7 +107,7 @@ $functions = @( "Get-OrcaClickModifierVirtualKeys", "Send-OrcaMouseClick", "Send-OrcaDrag", "Invoke-OrcaOperation", "Assert-OrcaInputAllowed", "Test-OrcaTargetTookForeground", "Invoke-OrcaPrimaryAction", "Get-OrcaScrollPattern", "Get-OrcaScrollPercent", "Get-OrcaScrollViewSize", "Get-OrcaRuntimeId", "Test-OrcaSameRuntimeId", "Wait-OrcaScrollPercentChange", "Invoke-OrcaScrollPercent", - "Invoke-OrcaScrollPattern" + "Invoke-OrcaScrollPattern", "Invoke-OrcaNativeListScroll", "Invoke-OrcaNativeButtonClick" ) foreach ($definition in $ast.FindAll({ param($node) $node -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $false)) { if ($definition.Name -in $functions) { . ([scriptblock]::Create($definition.Extent.Text)) } @@ -301,9 +301,9 @@ if ($uiaTypes) { $element } $ignored = New-TestScrollElement 30.0 - Assert-TestEqual (Invoke-OrcaScrollPatternReal $ignored $ignored "down" 1) $false "whole page the provider ignores" + Assert-TestEqual (Invoke-OrcaScrollPatternReal $ignored $ignored $null "down" 1) $false "whole page the provider ignores" $atEnd = New-TestScrollElement 100.0 - Assert-TestEqual (Invoke-OrcaScrollPatternReal $atEnd $atEnd "down" 1) $true "whole page at the end of the content" + Assert-TestEqual (Invoke-OrcaScrollPatternReal $atEnd $atEnd $null "down" 1) $true "whole page at the end of the content" } else { Write-Output "skipping UI Automation pattern checks: UIAutomationClient is unavailable" } diff --git a/native/computer-use-windows/runtime-text-entry.test.ps1 b/native/computer-use-windows/runtime-text-entry.test.ps1 new file mode 100644 index 00000000000..f53961aad7b --- /dev/null +++ b/native/computer-use-windows/runtime-text-entry.test.ps1 @@ -0,0 +1,189 @@ +param([string]$RuntimePath = (Join-Path $PSScriptRoot "runtime.ps1")) + +$ErrorActionPreference = "Stop" + +function Assert-TestEqual($Actual, $Expected, [string]$Label) { + if ($Actual -cne $Expected) { throw "$Label expected '$Expected', received '$Actual'" } +} + +Add-Type -TypeDefinition @" +using System; +using System.Collections.Generic; + +public static class OrcaDesktopWin32 { + public static readonly List Calls = new List(); + public static IntPtr Foreground; + public static string EditText; + public static int[] Selection; + public static int EditOwner; + public static bool IgnoreInsertion; + public static long Limit; + + public static void Reset(string text, int start, int end) { + Calls.Clear(); + Foreground = new IntPtr(202); + EditText = text; + Selection = new[] { start, end }; + EditOwner = 4242; + IgnoreInsertion = false; + Limit = 30000; + } + public static int GetProcessIntegrityLevel(int processId) { return 0x2000; } + public static IntPtr GetForegroundWindow() { return Foreground; } + public static bool SetForegroundWindow(IntPtr hwnd) { Calls.Add("activate"); return false; } + public static bool IsIconic(IntPtr hwnd) { return false; } + public static bool ShowWindow(IntPtr hwnd, int command) { Calls.Add("restore"); return true; } + public static bool IsWindow(IntPtr hwnd) { return true; } + public static int GetWindowLong(IntPtr hwnd, int index) { return 0x0004; } + public static uint GetWindowThreadProcessId(IntPtr hwnd, out uint processId) { + processId = hwnd == new IntPtr(303) ? (uint)EditOwner : (hwnd == new IntPtr(101) ? 4242u : 1u); + return 1; + } + public static string GetEditText(IntPtr hwnd, int maxLength) { return EditText; } + public static int[] GetEditSelection(IntPtr hwnd) { return Selection; } + public static bool ReplaceEditSelection(IntPtr hwnd, string text) { + Calls.Add("replace:" + text); + if (!IgnoreInsertion) { + EditText = EditText.Substring(0, Selection[0]) + text + EditText.Substring(Selection[1]); + } + return true; + } + public static bool SendUnicodeText(string text) { Calls.Add("keys:" + text); return true; } + public static long? SendControlMessage(IntPtr hwnd, uint message, IntPtr wParam, IntPtr lParam) { return message == 0x00D5 ? Limit : 0; } +} +"@ + +$tokens = $null +$errors = $null +$ast = [System.Management.Automation.Language.Parser]::ParseFile($RuntimePath, [ref]$tokens, [ref]$errors) +if ($errors.Count) { throw ($errors | Out-String) } +$functions = @( + "Test-OrcaWindowFocused", "Wait-OrcaWindowFocused", "Assert-OrcaKeyboardFocus", "New-OrcaForegroundRefusal", + "Assert-OrcaWindowTarget", "Get-OrcaRequiredString", "Invoke-OrcaOperation", + "Assert-OrcaInputAllowed", "Test-OrcaTargetTookForeground", "Get-OrcaProperty", "Test-OrcaSensitiveElement", + "Test-OrcaEditClass", "Get-OrcaNativeEdit", "Assert-OrcaWindowOwner", "Test-OrcaNativeClass", "ConvertTo-OrcaEditNewlines", + "Invoke-OrcaEditTextInsertion", "Invoke-OrcaElementTextInsertion", "Send-OrcaText", + "Test-OrcaElementDisabled", "Assert-OrcaStillFocused" +) +foreach ($definition in $ast.FindAll({ param($node) $node -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $false)) { + if ($definition.Name -in $functions) { . ([scriptblock]::Create($definition.Extent.Text)) } +} + +$EditReadbackLimit = 1048576 +function Find-OrcaProcess { [pscustomobject]@{ MainWindowHandle = 101; Id = 4242; ProcessName = "Fixture" } } +function Get-OrcaAppName($Process) { $Process.ProcessName } +function Get-OrcaRootElement { $null } +function Get-OrcaWindowFrame { @{ x = 100; y = 200 } } +function Find-OrcaElement($RootElement, $Record) { if ($null -eq $Record) { $null } else { $Record.live } } +function Invoke-OrcaValueTextInsertion { $script:valueWrites++; $null } +function Send-OrcaKey { [OrcaDesktopWin32]::Calls.Add("sendkeys") } +function Send-OrcaHotkey { [OrcaDesktopWin32]::Calls.Add("sendkeys") } +function Send-OrcaPasteText { [OrcaDesktopWin32]::Calls.Add("sendkeys") } +function New-OrcaSnapshot { [pscustomobject]@{} } +function Test-OrcaTextPatternReadOnly { $false } +function Restore-OrcaWindow { [OrcaDesktopWin32]::Calls.Add("activate"); $null } + +function New-TestElement([string]$ClassName, [int64]$Handle, [bool]$Enabled = $true, [string]$ControlType = "ControlType.Document") { + $live = [pscustomobject]@{ + Current = [pscustomobject]@{ + ClassName = $ClassName; NativeWindowHandle = $Handle; IsPassword = $false; IsEnabled = $Enabled; Name = "Body" + AutomationId = ""; LocalizedControlType = "document"; ControlType = [pscustomobject]@{ ProgrammaticName = $ControlType } + } + } + [pscustomobject]@{ index = 7; live = $live } +} + +function Invoke-TestOperation($Operation) { + $message = $null + $result = $null + try { $result = Invoke-OrcaOperation $Operation } catch { $message = $_.Exception.Message } + [pscustomobject]@{ result = $result; message = $message } +} + +# Background keyboard verbs refuse before any key leaves, and say how to enter text without the foreground. +foreach ($operation in @( + [pscustomobject]@{ tool = "type_text"; text = "hello" }, + [pscustomobject]@{ tool = "paste_text"; text = "hello" }, + [pscustomobject]@{ tool = "press_key"; key = "Return" }, + [pscustomobject]@{ tool = "hotkey"; key = "Ctrl+A" } +)) { + [OrcaDesktopWin32]::Reset("", 0, 0) + $operation | Add-Member -NotePropertyName app -NotePropertyValue "Fixture" + $outcome = Invoke-TestOperation $operation + if ($outcome.message -notlike "background_unavailable: *takes the user's keyboard; nothing was sent.*type-text --element-index*Ask the user before retrying with --restore-window.") { + throw "$($operation.tool) background refusal, received '$($outcome.message)'" + } + Assert-TestEqual ([OrcaDesktopWin32]::Calls.Count) 0 "$($operation.tool) background refusal sends nothing" +} + +# Typing into a native edit replaces its selection in the background and reads the result back. +[OrcaDesktopWin32]::Reset("one`r`nthree", 3, 3) +$outcome = Invoke-TestOperation ([pscustomobject]@{ tool = "type_text"; app = "Fixture"; text = "`ntwo"; element = (New-TestElement "Edit" 303) }) +if ($null -ne $outcome.message) { throw "edit insertion failed: $($outcome.message)" } +Assert-TestEqual ([OrcaDesktopWin32]::Calls -join "|") "replace:`r`ntwo" "edit insertion sends one EM_REPLACESEL with CRLF line breaks" +Assert-TestEqual ([OrcaDesktopWin32]::EditText) "one`r`ntwo`r`nthree" "edit insertion result" +Assert-TestEqual $outcome.result.action.actionName "EM_REPLACESEL" "edit insertion action" +Assert-TestEqual $outcome.result.action.delivery "background" "edit insertion delivery" +Assert-TestEqual $outcome.result.action.verification.state "verified" "edit insertion verification" + +# A field that ignores the insertion is an error, not a typed result. +[OrcaDesktopWin32]::Reset("same", 4, 4) +[OrcaDesktopWin32]::IgnoreInsertion = $true +$outcome = Invoke-TestOperation ([pscustomobject]@{ tool = "type_text"; app = "Fixture"; text = "x"; element = (New-TestElement "Edit" 303) }) +if ($outcome.message -notlike "text_insertion_unconfirmed:*") { throw "ignored insertion, received '$($outcome.message)'" } + +# Replacing a selection with the same text changes nothing yet is the requested result. +[OrcaDesktopWin32]::Reset("abc", 0, 3) +$outcome = Invoke-TestOperation ([pscustomobject]@{ tool = "type_text"; app = "Fixture"; text = "abc"; element = (New-TestElement "Edit" 303) }) +if ($null -ne $outcome.message) { throw "identical replacement failed: $($outcome.message)" } +Assert-TestEqual $outcome.result.action.verification.state "verified" "identical replacement verification" + +# An edit cuts text past its limit, so an insertion that would overflow it is refused before anything is sent. +[OrcaDesktopWin32]::Reset("old", 3, 3) +[OrcaDesktopWin32]::Limit = 5 +$outcome = Invoke-TestOperation ([pscustomobject]@{ tool = "type_text"; app = "Fixture"; text = "abc"; element = (New-TestElement "Edit" 303) }) +if ($outcome.message -notlike "element 7 text is not settable: the result would be 6 characters, past the field's 5-character limit") { throw "over-limit insertion, received '$($outcome.message)'" } +Assert-TestEqual ([OrcaDesktopWin32]::EditText) "old" "over-limit insertion leaves the field untouched" + +# A password field can't be read back, yet its limit still holds. +[OrcaDesktopWin32]::Reset("", 0, 0) +[OrcaDesktopWin32]::Limit = 2 +$secret = New-TestElement "Edit" 303 +$secret.live.Current.IsPassword = $true +$outcome = Invoke-TestOperation ([pscustomobject]@{ tool = "type_text"; app = "Fixture"; text = "abc"; element = $secret }) +if ($outcome.message -notlike "*past the field's 2-character limit") { throw "over-limit password insertion, received '$($outcome.message)'" } +Assert-TestEqual ([OrcaDesktopWin32]::Calls.Count) 0 "over-limit password insertion sends nothing" + +# Edit messages would bypass a disabled field's lock, so a disabled field is refused untouched. +[OrcaDesktopWin32]::Reset("", 0, 0) +$outcome = Invoke-TestOperation ([pscustomobject]@{ tool = "type_text"; app = "Fixture"; text = "x"; element = (New-TestElement "Edit" 303 $false) }) +if ($outcome.message -notlike "element 7 text is not settable: the field is disabled") { throw "disabled edit, received '$($outcome.message)'" } +Assert-TestEqual ([OrcaDesktopWin32]::Calls.Count) 0 "disabled edit sends nothing" + +# A recycled window handle owned by another process is never messaged. +[OrcaDesktopWin32]::Reset("", 0, 0) +[OrcaDesktopWin32]::EditOwner = 999 +$outcome = Invoke-TestOperation ([pscustomobject]@{ tool = "type_text"; app = "Fixture"; text = "x"; element = (New-TestElement "Edit" 303) }) +if ($outcome.message -notlike "stale element:*") { throw "recycled edit handle, received '$($outcome.message)'" } +Assert-TestEqual ([OrcaDesktopWin32]::Calls.Count) 0 "recycled edit handle sends nothing" + +# An element with no background text route refuses instead of typing at whatever has focus. +[OrcaDesktopWin32]::Reset("", 0, 0) +$outcome = Invoke-TestOperation ([pscustomobject]@{ tool = "type_text"; app = "Fixture"; text = "x"; element = (New-TestElement "Chrome_RenderWidgetHostHWND" 404) }) +if ($outcome.message -notlike "background_unavailable: element 7 accepts text only as keys*") { throw "no text route, received '$($outcome.message)'" } +Assert-TestEqual ([OrcaDesktopWin32]::Calls.Count) 0 "no text route sends nothing" + +# List and tree items take ValuePattern too, where a write would rename them, so they are refused untouched. +[OrcaDesktopWin32]::Reset("", 0, 0) +$script:valueWrites = 0 +$outcome = Invoke-TestOperation ([pscustomobject]@{ tool = "type_text"; app = "Fixture"; text = "x"; element = (New-TestElement "" 0 $true "ControlType.ListItem") }) +if ($outcome.message -notlike "element 7 text is not settable: it is not a text field") { throw "list item, received '$($outcome.message)'" } +Assert-TestEqual ([OrcaDesktopWin32]::Calls.Count + $script:valueWrites) 0 "list item sends nothing" + +# Foreground typing re-checks focus right before SendInput, so a user's switch never receives the text. +[OrcaDesktopWin32]::Reset("", 0, 0) +[OrcaDesktopWin32]::Foreground = [IntPtr]101 +$outcome = Invoke-TestOperation ([pscustomobject]@{ tool = "type_text"; app = "Fixture"; text = "caf$([char]0x00E9)"; restoreWindow = $true }) +if ($null -ne $outcome.message) { throw "foreground typing failed: $($outcome.message)" } +Assert-TestEqual (@([OrcaDesktopWin32]::Calls | Where-Object { $_ -like "keys:*" }) -join "|") "keys:caf$([char]0x00E9)" "foreground typing sends Unicode input" +Write-Output "windows-text-entry-tests-ok" diff --git a/native/computer-use-windows/runtime.ps1 b/native/computer-use-windows/runtime.ps1 index 9b7cd18717a..d791dc0814f 100644 --- a/native/computer-use-windows/runtime.ps1 +++ b/native/computer-use-windows/runtime.ps1 @@ -89,9 +89,6 @@ public static class OrcaDesktopWin32 { [DllImport("user32.dll")] public static extern bool ScreenToClient(IntPtr hwnd, ref POINT point); - [DllImport("user32.dll", EntryPoint = "PostMessageW")] - public static extern bool PostMessage(IntPtr hwnd, uint message, IntPtr wParam, IntPtr lParam); - [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow); @@ -143,6 +140,27 @@ public static class OrcaDesktopWin32 { [DllImport("user32.dll", EntryPoint = "SendMessageTimeoutW", SetLastError = true)] private static extern IntPtr SendMessageTimeout(IntPtr hwnd, uint message, IntPtr wParam, IntPtr lParam, uint flags, uint timeout, out IntPtr result); + [DllImport("user32.dll", EntryPoint = "SendMessageTimeoutW", CharSet = CharSet.Unicode)] + private static extern IntPtr SendMessageTimeoutString(IntPtr hwnd, uint message, IntPtr wParam, string lParam, uint flags, uint timeout, out IntPtr result); + + [DllImport("user32.dll", EntryPoint = "SendMessageTimeoutW", CharSet = CharSet.Unicode)] + private static extern IntPtr SendMessageTimeoutBuffer(IntPtr hwnd, uint message, IntPtr wParam, System.Text.StringBuilder lParam, uint flags, uint timeout, out IntPtr result); + + [DllImport("user32.dll")] + public static extern bool IsWindow(IntPtr hwnd); + + [DllImport("user32.dll", EntryPoint = "PostMessageW")] + public static extern bool PostMessage(IntPtr hwnd, uint message, IntPtr wParam, IntPtr lParam); + + [DllImport("user32.dll")] + public static extern IntPtr GetParent(IntPtr hwnd); + + [DllImport("user32.dll")] + public static extern int GetDlgCtrlID(IntPtr hwnd); + + [DllImport("user32.dll", EntryPoint = "GetWindowLongW")] + public static extern int GetWindowLong(IntPtr hwnd, int index); + [DllImport("user32.dll")] private static extern bool IsHungAppWindow(IntPtr hwnd); @@ -175,6 +193,90 @@ public static class OrcaDesktopWin32 { return Marshal.GetLastWin32Error() == errorTimeout || IsHungAppWindow(hwnd); } + const uint AbortIfHung = 0x0002; + const uint EditMessageTimeoutMilliseconds = 2000; + + // EM_REPLACESEL: user32 marshals its string across processes and the edit needs no focus; false when it did not answer. + public static bool ReplaceEditSelection(IntPtr hwnd, string text) { + IntPtr result; + return SendMessageTimeoutString(hwnd, 0x00C2, new IntPtr(1), text, AbortIfHung, EditMessageTimeoutMilliseconds, out result) != IntPtr.Zero; + } + + // WM_GETTEXT, at most maxLength characters; null when the window did not answer. + public static string GetEditText(IntPtr hwnd, int maxLength) { + IntPtr length; + if (SendMessageTimeout(hwnd, 0x000E, IntPtr.Zero, IntPtr.Zero, AbortIfHung, EditMessageTimeoutMilliseconds, out length) == IntPtr.Zero) { return null; } + if (length.ToInt64() <= 0) { return ""; } + var capacity = (int)Math.Min(Math.Max(0L, length.ToInt64()), (long)maxLength) + 1; + var buffer = new System.Text.StringBuilder(capacity); + IntPtr copied; + if (SendMessageTimeoutBuffer(hwnd, 0x000D, new IntPtr(capacity), buffer, AbortIfHung, EditMessageTimeoutMilliseconds, out copied) == IntPtr.Zero) { return null; } + return buffer.ToString(); + } + + // A control message with no pointer arguments; null when the window did not answer. + public static long? SendControlMessage(IntPtr hwnd, uint message, IntPtr wParam, IntPtr lParam) { + IntPtr result; + if (SendMessageTimeout(hwnd, message, wParam, lParam, AbortIfHung, EditMessageTimeoutMilliseconds, out result) == IntPtr.Zero) { return null; } + return result.ToInt64(); + } + + // An exact-name search such as CB_FINDSTRINGEXACT, whose string user32 marshals across processes; null when unanswered. + public static long? FindControlString(IntPtr hwnd, uint message, int start, string name) { + IntPtr result; + if (SendMessageTimeoutString(hwnd, message, new IntPtr(start), name, AbortIfHung, EditMessageTimeoutMilliseconds, out result) == IntPtr.Zero) { return null; } + return result.ToInt64(); + } + + // An item's text through a length/text message pair such as CB_GETLBTEXTLEN/CB_GETLBTEXT; null when unanswered. + public static string GetControlItemText(IntPtr hwnd, uint lengthMessage, uint textMessage, int index) { + IntPtr length; + if (SendMessageTimeout(hwnd, lengthMessage, new IntPtr(index), IntPtr.Zero, AbortIfHung, EditMessageTimeoutMilliseconds, out length) == IntPtr.Zero || length.ToInt64() < 0) { return null; } + var buffer = new System.Text.StringBuilder((int)length.ToInt64() + 1); + IntPtr copied; + if (SendMessageTimeoutBuffer(hwnd, textMessage, new IntPtr(index), buffer, AbortIfHung, EditMessageTimeoutMilliseconds, out copied) == IntPtr.Zero) { return null; } + return buffer.ToString(); + } + + public static int GetClientHeight(IntPtr hwnd) { + RECT rect; + return GetClientRect(hwnd, out rect) ? rect.Bottom - rect.Top : 0; + } + + // EM_GETSEL as [start, end]; null when the edit did not answer or an offset is past the 16-bit return value. + public static int[] GetEditSelection(IntPtr hwnd) { + IntPtr result; + if (SendMessageTimeout(hwnd, 0x00B0, IntPtr.Zero, IntPtr.Zero, AbortIfHung, EditMessageTimeoutMilliseconds, out result) == IntPtr.Zero) { return null; } + var packed = result.ToInt64() & 0xFFFFFFFFL; + if (packed == 0xFFFFFFFFL) { return null; } + return new[] { (int)(packed & 0xFFFF), (int)(packed >> 16) }; + } + + // One SendInput batch of KEYEVENTF_UNICODE characters, so any layout types them and no other input interleaves. + public static bool SendUnicodeText(string text) { + const uint keyboardInput = 1; + const uint keyUp = 0x0002; + const uint unicode = 0x0004; + const byte returnKey = 0x0D; + const byte tabKey = 0x09; + var inputs = new List(); + foreach (var unit in text) { + if (unit == '\r') { continue; } + // Why: Return and Tab go as keys, as SendKeys sent them, so dialogs and forms act on them. + if (unit == '\n' || unit == '\t') { + var key = unit == '\n' ? returnKey : tabKey; + inputs.Add(KeyboardInput(keyboardInput, key, 0)); + inputs.Add(KeyboardInput(keyboardInput, key, keyUp)); + continue; + } + inputs.Add(UnicodeInput(unit, unicode)); + inputs.Add(UnicodeInput(unit, unicode | keyUp)); + } + var values = inputs.ToArray(); + if (values.Length == 0) { return true; } + return SendInput((uint)values.Length, values, Marshal.SizeOf(typeof(INPUT))) == (uint)values.Length; + } + // Mandatory integrity RID (0x2000 medium, 0x3000 high), or -1 when the process cannot be queried. public static int GetProcessIntegrityLevel(int processId) { const uint processQueryLimitedInformation = 0x1000; @@ -359,6 +461,15 @@ public static class OrcaDesktopWin32 { }; } + private static INPUT UnicodeInput(char unit, uint flags) { + return new INPUT { + type = 1, + data = new INPUTUNION { + keyboard = new KEYBDINPUT { scanCode = unit, flags = flags } + } + }; + } + private static INPUT MouseInput(uint type, uint flags) { return new INPUT { type = type, @@ -367,6 +478,123 @@ public static class OrcaDesktopWin32 { } }; } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + private struct STARTUPINFO { + public int cb; + public string lpReserved; + public string lpDesktop; + public string lpTitle; + public int dwX; + public int dwY; + public int dwXSize; + public int dwYSize; + public int dwXCountChars; + public int dwYCountChars; + public int dwFillAttribute; + public int dwFlags; + public short wShowWindow; + public short cbReserved2; + public IntPtr lpReserved2; + public IntPtr hStdInput; + public IntPtr hStdOutput; + public IntPtr hStdError; + } + + [StructLayout(LayoutKind.Sequential)] + private struct STARTUPINFOEX { + public STARTUPINFO StartupInfo; + public IntPtr lpAttributeList; + } + + [StructLayout(LayoutKind.Sequential)] + private struct PROCESS_INFORMATION { + public IntPtr hProcess; + public IntPtr hThread; + public int dwProcessId; + public int dwThreadId; + } + + [DllImport("kernel32.dll", EntryPoint = "CreateProcessW", SetLastError = true, CharSet = CharSet.Unicode)] + private static extern bool CreateProcess(string applicationName, System.Text.StringBuilder commandLine, IntPtr processAttributes, IntPtr threadAttributes, bool inheritHandles, uint creationFlags, IntPtr environment, string currentDirectory, ref STARTUPINFOEX startupInfo, out PROCESS_INFORMATION processInformation); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool InitializeProcThreadAttributeList(IntPtr attributeList, int attributeCount, int flags, ref IntPtr size); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool UpdateProcThreadAttribute(IntPtr attributeList, uint flags, IntPtr attribute, IntPtr value, IntPtr size, IntPtr previousValue, IntPtr returnSize); + + [DllImport("kernel32.dll")] + private static extern void DeleteProcThreadAttributeList(IntPtr attributeList); + + [DllImport("user32.dll")] + private static extern IntPtr GetShellWindow(); + + [DllImport("user32.dll", SetLastError = true)] + private static extern bool SetWindowPos(IntPtr hwnd, IntPtr insertAfter, int x, int y, int width, int height, uint flags); + + // Starts an app without handing it the caller's right to take the foreground (design from agent-desktop #615): + // the desktop shell is its parent, and its first ShowWindow is SW_SHOWNOACTIVATE. + public static int LaunchWithoutForeground(string executable, string workingDirectory) { + const uint processCreateProcess = 0x0080; + const uint extendedStartupInfoPresent = 0x00080000; + const int procThreadAttributeParentProcess = 0x00020000; + const int startfUseShowWindow = 0x0001; + const short swShowNoActivate = 4; + uint shellProcessId; + GetWindowThreadProcessId(GetShellWindow(), out shellProcessId); + if (shellProcessId == 0) { + throw new InvalidOperationException("no desktop shell is running to start the app under"); + } + var shell = OpenProcess(processCreateProcess, false, (int)shellProcessId); + if (shell == IntPtr.Zero) { + throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "could not open the desktop shell process"); + } + var attributes = IntPtr.Zero; + var parentHandle = IntPtr.Zero; + var attributesInitialized = false; + try { + var size = IntPtr.Zero; + InitializeProcThreadAttributeList(IntPtr.Zero, 1, 0, ref size); + attributes = Marshal.AllocHGlobal(size); + if (!InitializeProcThreadAttributeList(attributes, 1, 0, ref size)) { + throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); + } + attributesInitialized = true; + // CreateProcess reads the handle through this pointer, so it must outlive the call. + parentHandle = Marshal.AllocHGlobal(IntPtr.Size); + Marshal.WriteIntPtr(parentHandle, shell); + if (!UpdateProcThreadAttribute(attributes, 0, (IntPtr)procThreadAttributeParentProcess, parentHandle, (IntPtr)IntPtr.Size, IntPtr.Zero, IntPtr.Zero)) { + throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); + } + var startup = new STARTUPINFOEX(); + startup.StartupInfo.cb = Marshal.SizeOf(typeof(STARTUPINFOEX)); + startup.StartupInfo.dwFlags = startfUseShowWindow; + startup.StartupInfo.wShowWindow = swShowNoActivate; + startup.lpAttributeList = attributes; + PROCESS_INFORMATION process; + var commandLine = new System.Text.StringBuilder("\"" + executable + "\""); + if (!CreateProcess(executable, commandLine, IntPtr.Zero, IntPtr.Zero, false, extendedStartupInfoPresent, IntPtr.Zero, workingDirectory, ref startup, out process)) { + throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); + } + CloseHandle(process.hThread); + CloseHandle(process.hProcess); + return process.dwProcessId; + } finally { + if (attributesInitialized) { DeleteProcThreadAttributeList(attributes); } + if (attributes != IntPtr.Zero) { Marshal.FreeHGlobal(attributes); } + if (parentHandle != IntPtr.Zero) { Marshal.FreeHGlobal(parentHandle); } + CloseHandle(shell); + } + } + + // Bottom of the z-order without activating: the window stays restored and drivable but covers nothing. + public static bool SendWindowToBack(IntPtr hwnd) { + const uint swpNoSize = 0x0001; + const uint swpNoMove = 0x0002; + const uint swpNoActivate = 0x0010; + return SetWindowPos(hwnd, new IntPtr(1), 0, 0, 0, 0, swpNoSize | swpNoMove | swpNoActivate); + } } "@ @@ -375,6 +603,7 @@ public static class OrcaDesktopWin32 { $MaxNodes = 1200 $MaxDepth = 64 $TextLimit = 500 +$EditReadbackLimit = 1048576 $MaxScreenshotPngBytes = 900000 $MaxScreenshotEdge = 1280 $MinScreenshotScale = 0.25 @@ -390,20 +619,6 @@ $BlockedAppFragments = @( "proton pass" ) -$WindowsMessages = @{ - Char = 0x0102 - KeyDown = 0x0100 - KeyUp = 0x0101 - MouseMove = 0x0200 - LeftDown = 0x0201 - LeftUp = 0x0202 - RightDown = 0x0204 - RightUp = 0x0205 - MiddleDown = 0x0207 - MiddleUp = 0x0208 - Wheel = 0x020A -} - $MouseEvents = @{ LeftDown = 0x0002 LeftUp = 0x0004 @@ -620,13 +835,19 @@ function Wait-OrcaWindowFocused([IntPtr]$WindowHandle, [int]$TimeoutMilliseconds Test-OrcaWindowFocused $WindowHandle } -function Assert-OrcaKeyboardFocus([IntPtr]$WindowHandle, $Operation, $RestoreFailure = $null) { +function Assert-OrcaKeyboardFocus([IntPtr]$WindowHandle, $Operation, $RestoreFailure = $null, $Process = $null) { if (Test-OrcaWindowFocused $WindowHandle) { return } if ([bool]$Operation.restoreWindow) { $reason = if ($null -ne $RestoreFailure) { " ($RestoreFailure)" } else { "" } throw "window_not_focused: keyboard input requires the target window to be focused; restoreWindow was requested but the target window is still not focused$reason, so nothing was sent; ask the user to bring it forward or check desktop permissions" } - throw "window_not_focused: keyboard input requires the target window to be focused, and focusing it would take over the user's keyboard; nothing was sent. Use set-value for editable elements, or ask the user before retrying with --restore-window." + $verb = ([string]$Operation.tool) -replace "_", "-" + $alternative = if ($Operation.tool -in @("type_text", "paste_text")) { + "To enter text in the background, use type-text --element-index or set-value." + } else { + "To press a button in the background, click it with click --element-index; to enter text, use type-text --element-index ." + } + throw (New-OrcaForegroundRefusal $Process "$verb sends keys, which go only to the window in front" $alternative "takes the user's keyboard") } # Raises the target for real mouse input, then returns its frame read after the raise (restoring moves it). @@ -638,9 +859,9 @@ function Restore-OrcaPointerWindow($Process, $RootElement) { Get-OrcaWindowFrame $Process $RootElement } -function New-OrcaForegroundRefusal($Process, [string]$Need, [string]$Alternative = "") { +function New-OrcaForegroundRefusal($Process, [string]$Need, [string]$Alternative = "", [string]$Takes = "moves the user's pointer") { $hint = if ([string]::IsNullOrEmpty($Alternative)) { "" } else { " $Alternative" } - "background_unavailable: $Need, which on Windows brings $(Get-OrcaAppName $Process) to the front and moves the user's pointer; nothing was sent.$hint Ask the user before retrying with --restore-window." + "background_unavailable: $Need, which on Windows brings $(Get-OrcaAppName $Process) to the front and $Takes; nothing was sent.$hint Ask the user before retrying with --restore-window." } function Get-OrcaCursorPosition { @@ -710,6 +931,213 @@ function Get-OrcaValueText($Element) { } } +function Test-OrcaNativeClass([string]$ClassName, [string]$BaseClass) { + # Why: WinForms controls are superclasses named WindowsForms10..app..., and answer the base class's messages. + $ClassName -match "^(?:WindowsForms\d+\.)?(?:$BaseClass)(?:\.|$)" +} + +function Test-OrcaEditClass([string]$ClassName) { + # Why: user32 edits and RichEdit answer EM_ and WM_GETTEXT messages from other processes. + Test-OrcaNativeClass $ClassName "Edit|RichEdit\w*" +} + +# Only for elements Get-OrcaValueText left empty, so never a sensitive one (it answers "[redacted]" for those). +function Get-OrcaEditValueText([IntPtr]$Handle) { + # Why: the client-side edit proxy gives a multiline edit (Notepad's document) no ValuePattern, so its text is read directly. + $text = [OrcaDesktopWin32]::GetEditText($Handle, $TextLimit + 1) + if ($null -eq $text) { return "" } + if ($text.Length -gt $TextLimit) { return $text.Substring(0, $TextLimit) + "..." } + $text +} + +function Get-OrcaNativeEdit($Element, $Process) { + $className = Get-OrcaProperty $Element "ClassName" + if (-not (Test-OrcaEditClass $className)) { return $null } + $handle = try { [IntPtr][int64]$Element.Current.NativeWindowHandle } catch { [IntPtr]::Zero } + if ($handle -eq [IntPtr]::Zero) { return $null } + [pscustomobject]@{ + handle = $handle + processId = [int]$Process.Id + rich = $className -match "RichEdit" + sensitive = Test-OrcaSensitiveElement $Element + } +} + +function Assert-OrcaWindowOwner([IntPtr]$Handle, [int]$ProcessId) { + # Why: a window handle can be recycled for another process between the tree read and the message. + $owner = [uint32]0 + if ([OrcaDesktopWin32]::IsWindow($Handle)) { + [void][OrcaDesktopWin32]::GetWindowThreadProcessId($Handle, [ref]$owner) + } + if ($owner -ne $ProcessId) { + throw "stale element: the control's window no longer belongs to the target app; run get-app-state again and use a fresh element index" + } +} + +# The element's own window when it is a native control of BaseClass owned by the target process. +function Get-OrcaNativeControl($Element, $Process, [string]$BaseClass) { + if (-not (Test-OrcaNativeClass (Get-OrcaProperty $Element "ClassName") $BaseClass)) { return $null } + $handle = try { [IntPtr][int64]$Element.Current.NativeWindowHandle } catch { [IntPtr]::Zero } + if ($handle -eq [IntPtr]::Zero) { return $null } + Assert-OrcaWindowOwner $handle ([int]$Process.Id) + $handle +} + +function ConvertTo-OrcaEditNewlines([string]$Text, [bool]$Collapse) { + $normalized = $Text -replace "`r`n|`r", "`n" + if ($Collapse) { return $normalized } + $normalized -replace "`n", "`r`n" +} + +function Invoke-OrcaEditTextInsertion($Edit, [int]$Index, [string]$Text, [switch]$ReplaceAll) { + Assert-OrcaWindowOwner $Edit.handle $Edit.processId + $style = [OrcaDesktopWin32]::GetWindowLong($Edit.handle, -16) + if (($style -band 0x0800) -ne 0) { throw "element $Index text is not settable: the field is read-only" } + $insert = ConvertTo-OrcaEditNewlines $Text $false + $before = if ($Edit.sensitive) { $null } else { [OrcaDesktopWin32]::GetEditText($Edit.handle, $EditReadbackLimit) } + $selection = [OrcaDesktopWin32]::GetEditSelection($Edit.handle) + # Why: an edit cuts text past its EM_SETLIMITTEXT limit, which would leave a partial write; refuse it untouched instead. + $limit = [OrcaDesktopWin32]::SendControlMessage($Edit.handle, 0x00D5, [IntPtr]::Zero, [IntPtr]::Zero) + if ($null -ne $limit -and $limit -gt 0) { + # Why: RichEdit counts a line break as one character, so its lengths are compared without CRLF. + # Why: a readback cut at the read limit holds only a prefix, so the field reports its own length then. + $current = if ($null -ne $before -and $before.Length -lt $EditReadbackLimit) { (ConvertTo-OrcaEditNewlines $before $Edit.rich).Length } else { [OrcaDesktopWin32]::SendControlMessage($Edit.handle, 0x000E, [IntPtr]::Zero, [IntPtr]::Zero) } + $replaced = if ($ReplaceAll) { $current } elseif ($null -ne $selection) { [Math]::Abs($selection[1] - $selection[0]) } else { 0 } + $length = $current - $replaced + (ConvertTo-OrcaEditNewlines $Text $Edit.rich).Length + if ($null -ne $current -and $length -gt $limit) { throw "element $Index text is not settable: the result would be $length characters, past the field's $limit-character limit" } + } + Assert-OrcaWindowOwner $Edit.handle $Edit.processId + if (-not [OrcaDesktopWin32]::ReplaceEditSelection($Edit.handle, $insert)) { + throw "window_not_responding: the text field did not answer the insertion in time; the text may still land, so run get-app-state before retrying" + } + $action = [pscustomobject]@{ path = "accessibility"; actionName = "EM_REPLACESEL"; fallbackReason = $null; verification = [pscustomobject]@{ state = "unverified"; reason = "readback_unsupported" } } + $after = if ($null -eq $before) { $null } else { [OrcaDesktopWin32]::GetEditText($Edit.handle, $EditReadbackLimit) } + # Why: a text past the read limit can't show the whole result, so it neither proves nor disproves the insertion. + if ($null -eq $after -or $before.Length -ge $EditReadbackLimit -or $after.Length -ge $EditReadbackLimit) { return $action } + # Why: RichEdit counts a line break as one character in its selection but may read it back as CRLF. + $current = if ($Edit.rich) { ConvertTo-OrcaEditNewlines $before $true } else { $before } + $actual = if ($Edit.rich) { ConvertTo-OrcaEditNewlines $after $true } else { $after } + $expected = $null + if ($null -ne $selection -and [Math]::Max($selection[0], $selection[1]) -le $current.Length) { + $start = [Math]::Min($selection[0], $selection[1]) + $end = [Math]::Max($selection[0], $selection[1]) + $expected = $current.Substring(0, $start) + (ConvertTo-OrcaEditNewlines $Text $Edit.rich) + $current.Substring($end) + } + if ($null -ne $expected -and $actual -ceq $expected) { + $action.verification = [pscustomobject]@{ state = "verified"; property = "elementText" } + } elseif ($after -ceq $before) { + throw "text_insertion_unconfirmed: element $Index did not change after the insertion; run get-app-state to check the field before retrying" + } elseif ($null -ne $expected) { + $action.verification = [pscustomobject]@{ state = "unverified"; reason = "text_mismatch" } + } + $action +} + +function Set-OrcaTextCaret($Element, [string]$Leading) { + # Why: a ValuePattern write resets WPF's caret to the start, so the next insertion would land there. + try { + # Why: UIA moves by caret positions, which keep a surrogate pair, a combining sequence and a CRLF together. + $positions = (New-Object Globalization.StringInfo $Leading).LengthInTextElements - ([regex]::Matches($Leading, "`r`n")).Count + $start = [Windows.Automation.Text.TextPatternRangeEndpoint]::Start + $end = [Windows.Automation.Text.TextPatternRangeEndpoint]::End + $leadingRange = $Element.GetCurrentPattern([Windows.Automation.TextPattern]::Pattern).DocumentRange.Clone() + $leadingRange.MoveEndpointByRange($end, $leadingRange, $start) + [void]$leadingRange.MoveEndpointByUnit($end, [Windows.Automation.Text.TextUnit]::Character, $positions) + # Why: a provider that counts positions differently would put the caret elsewhere, so only a confirmed prefix moves it. + if ($leadingRange.GetText(-1) -cne $Leading) { return } + $leadingRange.MoveEndpointByRange($start, $leadingRange, $end) + $leadingRange.Select() + } catch {} +} + +function Get-OrcaTextSelectionOffsets($Element) { + try { + $text = $Element.GetCurrentPattern([Windows.Automation.TextPattern]::Pattern) + $ranges = @($text.GetSelection()) + if ($ranges.Count -ne 1) { return $null } + $leading = $text.DocumentRange.Clone() + $leading.MoveEndpointByRange([Windows.Automation.Text.TextPatternRangeEndpoint]::End, $ranges[0], [Windows.Automation.Text.TextPatternRangeEndpoint]::Start) + $start = $leading.GetText(-1).Length + @($start, ($start + $ranges[0].GetText(-1).Length)) + } catch { + $null + } +} + +function Invoke-OrcaValueTextInsertion($Element, [int]$Index, [string]$Text) { + $pattern = try { $Element.GetCurrentPattern([Windows.Automation.ValuePattern]::Pattern) } catch { $null } + if ($null -eq $pattern) { return $null } + if ($pattern.Current.IsReadOnly) { throw "element $Index text is not settable: the field is read-only" } + # Why: Chromium applies a value write as replaced inner text, which editors with their own model miss or revert. + if ((Get-OrcaProperty $Element "FrameworkId") -eq "Chrome") { return $null } + if (Test-OrcaSensitiveElement $Element) { + throw "element $Index text is not settable at its caret: it is a password field, whose text can't be read; use set-value to replace it" + } + $current = [string]$pattern.Current.Value + $selection = Get-OrcaTextSelectionOffsets $Element + # Why: without a readable selection the caret is unknown; appending matches macOS's default insertion point. + if ($null -eq $selection -or $selection[1] -gt $current.Length) { $selection = @($current.Length, $current.Length) } + $next = $current.Substring(0, $selection[0]) + $Text + $current.Substring($selection[1]) + $pattern.SetValue($next) + # Why: Chromium applies the write asynchronously, so poll briefly like the range readback. + $stopwatch = [Diagnostics.Stopwatch]::StartNew() + while ($true) { + $actual = try { [string]$pattern.Current.Value } catch { $null } + if ($actual -ceq $next -or $stopwatch.ElapsedMilliseconds -ge 500) { break } + Start-Sleep -Milliseconds 25 + } + if ($actual -ceq $next) { Set-OrcaTextCaret $Element ($current.Substring(0, $selection[0]) + $Text) } + $verification = if ($actual -ceq $next) { + [pscustomobject]@{ state = "verified"; property = "elementText" } + } elseif ($null -eq $actual) { + [pscustomobject]@{ state = "unverified"; reason = "readback_unsupported" } + } elseif ($actual -ceq $current) { + [pscustomobject]@{ state = "unverified"; reason = "accessibility_action_unasserted" } + } else { + [pscustomobject]@{ state = "unverified"; reason = "text_mismatch" } + } + [pscustomobject]@{ path = "accessibility"; actionName = "setValue"; fallbackReason = $null; verification = $verification } +} + +function Test-OrcaTextPatternReadOnly($Element) { + try { + $range = $Element.GetCurrentPattern([Windows.Automation.TextPattern]::Pattern).DocumentRange + $range.GetAttributeValue([Windows.Automation.TextPattern]::IsReadOnlyAttribute) -eq $true + } catch { + $false + } +} + +function Invoke-OrcaElementTextInsertion($Element, $Record, $Process, $RootElement, $Operation, [string]$Text) { + if ($null -eq $Element) { + throw "stale element: element $($Record.index) is gone; run get-app-state again and use a fresh element index" + } + $index = [int]$Record.index + # Why: edit messages bypass a disabled field's lock, which set-value's UI Automation write honours. + if (Test-OrcaElementDisabled $Element) { throw "element $index text is not settable: the field is disabled" } + $edit = Get-OrcaNativeEdit $Element $Process + if ($null -ne $edit) { return Invoke-OrcaEditTextInsertion $edit $index $Text } + # Why: list and tree items also take ValuePattern, where a write renames them rather than typing. + $controlType = try { [string]$Element.Current.ControlType.ProgrammaticName } catch { "" } + if ($controlType -notin @("ControlType.Edit", "ControlType.Document")) { + throw "element $index text is not settable: it is not a text field" + } + $action = Invoke-OrcaValueTextInsertion $Element $index $Text + if ($null -ne $action) { return $action } + # Why: a read-only document with no ValuePattern (WPF RichTextBox) says so only through TextPattern; keys would do nothing. + if (Test-OrcaTextPatternReadOnly $Element) { throw "element $index text is not settable: the field is read-only" } + if (-not [bool]$Operation.restoreWindow) { + throw (New-OrcaForegroundRefusal $Process "element $index accepts text only as keys, which go only to the window in front" "" "takes the user's keyboard") + } + $failure = Restore-OrcaWindow $Process $RootElement + Assert-OrcaKeyboardFocus ([IntPtr]$Process.MainWindowHandle) $Operation $failure $Process + try { $Element.SetFocus() } catch { + throw "window_not_focused: element $index did not take keyboard focus; no text was sent" + } + Send-OrcaText ([IntPtr]$Process.MainWindowHandle) $Text + [pscustomobject]@{ path = "synthetic"; actionName = "typeText"; fallbackReason = "actionUnsupported"; verification = [pscustomobject]@{ state = "unverified"; reason = "synthetic_input" } } +} + function Get-OrcaRangeValueText($Element) { try { $pattern = $Element.GetCurrentPattern([Windows.Automation.RangeValuePattern]::Pattern) @@ -849,11 +1277,15 @@ function Test-OrcaPlainTextSubtree($Element, [int]$MaxDepth = 4) { function New-OrcaElementRecord($Element, [int]$Index, $WindowFrame) { $controlType = try { [string]$Element.Current.ControlType.ProgrammaticName } catch { "" } $nativeWindowHandle = try { [int64]$Element.Current.NativeWindowHandle } catch { 0 } + $className = Get-OrcaProperty $Element "ClassName" $patterns = @($Element.GetSupportedPatterns()) $value = Get-OrcaValueText $Element if ([string]::IsNullOrEmpty($value) -and $patterns -contains [Windows.Automation.RangeValuePattern]::Pattern) { $value = Get-OrcaRangeValueText $Element } + if ([string]::IsNullOrEmpty($value) -and $nativeWindowHandle -ne 0 -and (Test-OrcaEditClass $className)) { + $value = Get-OrcaEditValueText ([IntPtr]$nativeWindowHandle) + } [pscustomobject]@{ index = $Index runtimeId = @(Get-OrcaRuntimeId $Element) @@ -861,7 +1293,7 @@ function New-OrcaElementRecord($Element, [int]$Index, $WindowFrame) { name = Get-OrcaProperty $Element "Name" controlType = $controlType localizedControlType = Get-OrcaProperty $Element "LocalizedControlType" - className = Get-OrcaProperty $Element "ClassName" + className = $className value = $value isSelected = Test-OrcaElementSelected $Element isDisabled = Test-OrcaElementDisabled $Element @@ -1211,6 +1643,104 @@ function Get-OrcaWindowList([string]$Query) { } } +function Resolve-OrcaLaunchExecutable([string]$Query) { + $needle = $Query.Trim() + if ([System.IO.Path]::IsPathRooted($needle)) { + if (Test-Path -LiteralPath $needle -PathType Leaf) { return $needle } + throw "appNotFound(`"$Query`")" + } + $exe = if ($needle.EndsWith(".exe", [System.StringComparison]::OrdinalIgnoreCase)) { $needle } else { "$needle.exe" } + $command = Get-Command -Name ([WildcardPattern]::Escape($exe)) -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1 + if ($null -ne $command) { return [string]$command.Source } + # Why: Start-Process finds apps such as chrome.exe through App Paths, which PATH lookup misses. + foreach ($hive in @("HKCU:", "HKLM:")) { + $entry = Get-ItemProperty -LiteralPath "$hive\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\$exe" -ErrorAction SilentlyContinue + if ($null -eq $entry -or [string]::IsNullOrWhiteSpace([string]$entry.'(default)')) { continue } + $path = [Environment]::ExpandEnvironmentVariables(([string]$entry.'(default)').Trim('"')) + if (Test-Path -LiteralPath $path -PathType Leaf) { return $path } + } + throw "appNotFound(`"$Query`")" +} + +function Wait-OrcaLaunchedWindow([int]$ProcessId, [string]$ProcessName, [DateTime]$StartedAt, [int]$TimeoutMilliseconds) { + $deadline = [DateTime]::UtcNow.AddMilliseconds($TimeoutMilliseconds) + while ([DateTime]::UtcNow -lt $deadline) { + $process = Get-Process -Id $ProcessId -ErrorAction SilentlyContinue + if ($null -ne $process -and $process.MainWindowHandle -ne 0) { return $process } + if ($null -eq $process) { + # Why: stubs such as Windows 11's notepad.exe start the real app under the same name and exit. + $handoff = Get-OrcaWindowProcesses | Where-Object { + $_.ProcessName -ieq $ProcessName -and $_.StartTime -ge $StartedAt + } | Select-Object -First 1 + if ($null -ne $handoff) { return $handoff } + } + Start-Sleep -Milliseconds 100 + } + Get-Process -Id $ProcessId -ErrorAction SilentlyContinue +} + +function New-OrcaLaunchResult($Process, [bool]$Launched) { + $owner = [uint32]0 + [void][OrcaDesktopWin32]::GetWindowThreadProcessId([OrcaDesktopWin32]::GetForegroundWindow(), [ref]$owner) + # Why: measured, not taken from the request; an app may raise itself however it was started. + $delivery = if ($owner -eq $Process.Id) { "foreground" } else { "background" } + [pscustomobject]@{ ok = $true; app = New-OrcaAppRecord $Process; launched = $Launched; delivery = $delivery } +} + +function Find-OrcaRunningLaunchTarget([string]$ProcessName, [string]$ExecutablePath = "") { + # Why: starting a running app again usually opens a second window over the user's work; tray-only apps count too. + $session = [System.Diagnostics.Process]::GetCurrentProcess().SessionId + $running = @(Get-Process -Name ([WildcardPattern]::Escape($ProcessName)) -ErrorAction SilentlyContinue | Where-Object { + # A resolved executable must be that file, not another copy with the same name. + $_.SessionId -eq $session -and ([string]::IsNullOrEmpty($ExecutablePath) -or [string]$_.Path -ieq $ExecutablePath) + }) | Sort-Object { $_.MainWindowHandle -eq 0 } | Select-Object -First 1 + if ($null -ne $running) { Assert-OrcaProcessAllowed $running } + $running +} + +function Invoke-OrcaLaunchApp($Operation) { + $query = ([string]$Operation.app).Trim() + if ([string]::IsNullOrWhiteSpace($query)) { throw 'appNotFound("")' } + # A bare name means any running copy; a path means that file, checked once it resolves. + if (-not [System.IO.Path]::IsPathRooted($query)) { + $queryName = if ($query.EndsWith(".exe", [System.StringComparison]::OrdinalIgnoreCase)) { $query.Substring(0, $query.Length - 4) } else { $query } + $running = Find-OrcaRunningLaunchTarget $queryName + if ($null -ne $running) { return New-OrcaLaunchResult $running $false } + } + + $executable = [System.IO.Path]::GetFullPath((Resolve-OrcaLaunchExecutable $query)) + # Why: an App Paths alias can name a differently named executable. + $running = Find-OrcaRunningLaunchTarget ([System.IO.Path]::GetFileNameWithoutExtension($executable)) $executable + if ($null -ne $running) { return New-OrcaLaunchResult $running $false } + $name = [System.IO.Path]::GetFileNameWithoutExtension($executable).ToLowerInvariant().Replace(" ", "") + foreach ($fragment in $BlockedAppFragments) { + if ($name.Contains($fragment.Replace(" ", ""))) { throw "appBlocked(`"$name`")" } + } + $activate = [bool]$Operation.activate + # Why: Orca runs this script with ELECTRON_RUN_AS_NODE=1, which would start an Electron app as bare Node. + Remove-Item Env:ELECTRON_RUN_AS_NODE -ErrorAction SilentlyContinue + $startedAt = (Get-Date).AddSeconds(-1) + try { + $processId = if ($activate) { + (Start-Process -FilePath $executable -PassThru).Id + } else { + [OrcaDesktopWin32]::LaunchWithoutForeground($executable, [System.IO.Path]::GetDirectoryName($executable)) + } + } catch { + throw "app_launch_failed: could not launch '$query': $($_.Exception.Message)" + } + $process = Wait-OrcaLaunchedWindow $processId ([System.IO.Path]::GetFileNameWithoutExtension($executable)) $startedAt 10000 + if ($null -eq $process) { + throw "app_launch_failed: the process started for '$query' exited; if it handed off to another process, find that one with list-apps" + } + $result = New-OrcaLaunchResult $process $true + # Why: an app that still took the foreground keeps the keyboard; burying it would hide where typing goes. + if (-not $activate -and $result.delivery -eq "background" -and $process.MainWindowHandle -ne 0) { + [void][OrcaDesktopWin32]::SendWindowToBack([IntPtr]$process.MainWindowHandle) + } + $result +} + function Get-OrcaHandshake { [pscustomobject]@{ platform = "win32" @@ -1218,13 +1748,13 @@ function Get-OrcaHandshake { providerVersion = "1.0.0" protocolVersion = 1 supports = [pscustomobject]@{ - apps = [pscustomobject]@{ list = $true; bundleIds = $false; pids = $true } + apps = [pscustomobject]@{ list = $true; bundleIds = $false; pids = $true; launch = $true } windows = [pscustomobject]@{ list = $true; targetById = $true; targetByIndex = $true; focus = $false; moveResize = $false } observation = [pscustomobject]@{ screenshot = $true; annotatedScreenshot = $false; elementFrames = $true; ocr = $false; disabledState = $true } actions = [pscustomobject]@{ click = $true typeText = $true - typeTextElement = $false + typeTextElement = $true pressKey = $true hotkey = $true pasteText = $true @@ -1264,8 +1794,27 @@ function Find-OrcaElement($RootElement, $Record) { $null } +# Why: UI Automation's Invoke and Toggle focus a WinForms button first, which activates its background window; +# BM_CLICK runs the button's own click (and check box or radio state) with no focus change. +function Invoke-OrcaNativeButtonClick($Element, $Process) { + if ($null -eq $Process) { return $false } + # Why: measured on WinForms, whose buttons click on BM_CLICK directly; a user32 button simulates the mouse and + # sets focus, which can fail in an inactive dialog, so those keep the UI Automation pattern. + if ((Get-OrcaProperty $Element "ClassName") -notmatch "^WindowsForms\d+\.BUTTON\.") { return $false } + $button = Get-OrcaNativeControl $Element $Process "Button" + if ($null -eq $button) { return $false } + # Why: a group box shares the Button class but presses nothing; real buttons offer one of these patterns. + $patterns = @(try { $Element.GetSupportedPatterns() } catch { @() }) + $pressable = @([Windows.Automation.InvokePattern]::Pattern, [Windows.Automation.TogglePattern]::Pattern, [Windows.Automation.SelectionItemPattern]::Pattern) + if (@($patterns | Where-Object { $pressable -contains $_ }).Count -eq 0) { return $false } + # A click that opens a modal dialog doesn't return until the dialog closes, so an unanswered BM_CLICK still counts as sent. + [void][OrcaDesktopWin32]::SendControlMessage($button, 0x00F5, [IntPtr]::Zero, [IntPtr]::Zero) + $true +} + # False only when the element offers none of these patterns; a pattern that fails is an error, never a cue for a real click. -function Invoke-OrcaPrimaryAction($Element, $Index) { +function Invoke-OrcaPrimaryAction($Element, $Index, $Process) { + if (Invoke-OrcaNativeButtonClick $Element $Process) { return $true } foreach ($pattern in @( [Windows.Automation.InvokePattern]::Pattern, [Windows.Automation.SelectionItemPattern]::Pattern, @@ -1339,7 +1888,87 @@ function Test-OrcaRangeValueMatches([string]$Actual, [double]$Expected) { [Math]::Abs($actualNumber - $Expected) -le [Math]::Min(1e-6 * [Math]::Max(1.0, [Math]::Abs($Expected)), 1e-3) } -function Set-OrcaElementValue($Element, [string]$Value) { +function Set-OrcaNativeComboValue($Element, $Process, [string]$Value) { + $combo = Get-OrcaNativeControl $Element $Process "ComboBox" + if ($null -eq $combo) { return $null } + # Why: an owner-drawn combo without CBS_HASSTRINGS keeps item data, not names, so its items can't be found by name. + $style = [OrcaDesktopWin32]::GetWindowLong($combo, -16) + if (($style -band 0x0030) -ne 0 -and ($style -band 0x0200) -eq 0) { return $null } + $count = [OrcaDesktopWin32]::SendControlMessage($combo, 0x0146, [IntPtr]::Zero, [IntPtr]::Zero) + if ($null -eq $count -or $count -le 0) { return $null } + # Why: CB_FINDSTRINGEXACT ignores case and wraps, so walk its hits once and keep those whose text matches exactly. + $exact = @() + $first = [OrcaDesktopWin32]::FindControlString($combo, 0x0158, -1, $Value) + $hit = $first + for ($step = 0; $null -ne $hit -and $hit -ge 0 -and $step -lt $count; $step++) { + $text = [OrcaDesktopWin32]::GetControlItemText($combo, 0x0149, 0x0148, [int]$hit) + if ($text -ceq $Value) { $exact += [int]$hit } + $hit = [OrcaDesktopWin32]::FindControlString($combo, 0x0158, [int]$hit, $Value) + # Why: a search or read the combo didn't answer could hide a second option with the same name. + if ($null -eq $text -or $null -eq $hit) { + throw "window_not_responding: the combo box did not answer its option search in time; nothing was selected" + } + if ($hit -eq $first) { break } + } + if ($exact.Count -eq 0) { return $null } + if ($exact.Count -gt 1) { throw "element value is not settable by name: more than one option is named '$Value'" } + # CB_SETCURSEL changes the selection without opening the list; CBN_SELCHANGE runs the app's own change handler. + Assert-OrcaWindowOwner $combo ([int]$Process.Id) + if ($null -eq [OrcaDesktopWin32]::SendControlMessage($combo, 0x014E, [IntPtr]$exact[0], [IntPtr]::Zero)) { + throw "window_not_responding: the combo box did not answer in time; the selection may still change, so run get-app-state before retrying" + } + $parent = [OrcaDesktopWin32]::GetParent($combo) + if ($parent -ne [IntPtr]::Zero) { + Assert-OrcaWindowOwner $parent ([int]$Process.Id) + $command = [IntPtr]((1 -shl 16) -bor ([OrcaDesktopWin32]::GetDlgCtrlID($combo) -band 0xFFFF)) + [void][OrcaDesktopWin32]::SendControlMessage($parent, 0x0111, $command, $combo) + } + # Why: the change handler ran inside the send above, so the selection now is the one the app kept. + $selected = [OrcaDesktopWin32]::SendControlMessage($combo, 0x0147, [IntPtr]::Zero, [IntPtr]::Zero) + # Why: a handler that clears the selection leaves CB_ERR (-1), which contradicts the write rather than hiding it. + $actual = if ($null -eq $selected) { $null } elseif ($selected -lt 0) { "" } else { [OrcaDesktopWin32]::GetControlItemText($combo, 0x0149, 0x0148, [int]$selected) } + $verification = if ($actual -ceq $Value) { + [pscustomobject]@{ state = "verified"; property = "value" } + } elseif ($null -eq $actual) { + [pscustomobject]@{ state = "unverified"; reason = "readback_unsupported" } + } else { + [pscustomobject]@{ state = "unverified"; reason = "value_mismatch"; actualPreview = $actual } + } + [pscustomobject]@{ verification = $verification } +} + +function Set-OrcaNativeEditValue($Element, $Process, [int]$Index, [string]$Value) { + $edit = Get-OrcaNativeEdit $Element $Process + if ($null -eq $edit) { return $null } + # Why: UI Automation's ValuePattern focuses the field first, which activates its background window; selecting + # everything and replacing it runs the edit's own change notification, which WM_SETTEXT skips for multiline edits. + Assert-OrcaWindowOwner $edit.handle $edit.processId + if ($null -eq [OrcaDesktopWin32]::SendControlMessage($edit.handle, 0x00B1, [IntPtr]::Zero, [IntPtr](-1))) { + throw "window_not_responding: the text field did not answer in time; nothing was written" + } + $action = Invoke-OrcaEditTextInsertion $edit $Index $Value -ReplaceAll + $verification = if ($action.verification.state -eq "verified") { + [pscustomobject]@{ state = "verified"; property = "value" } + } elseif ($action.verification.reason -eq "text_mismatch") { + [pscustomobject]@{ state = "unverified"; reason = "value_mismatch" } + } else { + $action.verification + } + [pscustomobject]@{ verification = $verification } +} + +function Set-OrcaElementValue($Element, $Process, [int]$Index, [string]$Value) { + if ($null -ne $Process) { + # Why: these write native controls without focusing them; a disabled one is locked against the user, so it is refused. + $native = (Test-OrcaEditClass (Get-OrcaProperty $Element "ClassName")) -or (Test-OrcaNativeClass (Get-OrcaProperty $Element "ClassName") "ComboBox") + if ($native -and (Test-OrcaElementDisabled $Element)) { + throw "element_not_enabled: element $Index is disabled, so no input was sent; do what enables it first, then run get-app-state and retry once its line no longer lists disabled" + } + $combo = Set-OrcaNativeComboValue $Element $Process $Value + if ($null -ne $combo) { return $combo } + $edit = Set-OrcaNativeEditValue $Element $Process $Index $Value + if ($null -ne $edit) { return $edit } + } try { $pattern = $Element.GetCurrentPattern([Windows.Automation.ValuePattern]::Pattern) if (-not $pattern.Current.IsReadOnly) { @@ -1482,8 +2111,44 @@ function Invoke-OrcaScrollPercent($Pattern, [bool]$Vertical, [bool]$Forward, [do $null -ne $after -and $after -ne $Before } -function Invoke-OrcaScrollPattern($Element, $RootElement, [string]$Direction, [double]$Pages) { +# Why: ScrollPattern on a list box focuses it first, which activates its background window; LB_SETTOPINDEX pages the +# list itself. A page is the rows the list shows. +function Invoke-OrcaNativeListScroll($Element, $RootElement, $Process, [string]$Direction, [double]$Pages) { + if ($null -eq $Process -or $Direction -notin @("up", "down")) { return $false } + $rootId = Get-OrcaRuntimeId $RootElement + $current = $Element + $list = $null + # Why: an item scrolls with its list box, the window a wheel at its center would scroll. + while ($null -ne $current -and $null -eq $list) { + $list = Get-OrcaNativeControl $current $Process "ListBox" + if (Test-OrcaSameRuntimeId (Get-OrcaRuntimeId $current) $rootId) { break } + $current = try { [Windows.Automation.TreeWalker]::RawViewWalker.GetParent($current) } catch { $null } + } + if ($null -eq $list) { return $false } + # Why: rows of an LBS_OWNERDRAWVARIABLE list differ in height, so a page can't be counted in rows, and an + # LBS_MULTICOLUMN list pages sideways. + if (([OrcaDesktopWin32]::GetWindowLong($list, -16) -band 0x0220) -ne 0) { return $false } + $top = [OrcaDesktopWin32]::SendControlMessage($list, 0x018E, [IntPtr]::Zero, [IntPtr]::Zero) + $count = [OrcaDesktopWin32]::SendControlMessage($list, 0x018B, [IntPtr]::Zero, [IntPtr]::Zero) + $itemHeight = [OrcaDesktopWin32]::SendControlMessage($list, 0x01A1, [IntPtr]::Zero, [IntPtr]::Zero) + if ($null -eq $top -or $null -eq $count -or $null -eq $itemHeight -or $itemHeight -le 0 -or $count -le 0) { return $false } + $rows = [Math]::Max(1, [Math]::Floor([OrcaDesktopWin32]::GetClientHeight($list) / $itemHeight)) + $step = [int][Math]::Max(1, [Math]::Round($rows * $Pages)) + $target = if ($Direction -eq "down") { $top + $step } else { $top - $step } + # Why: a list that shows every row leaves the scroll to an enclosing ScrollPattern. + if ($count -le $rows) { return $false } + $target = [Math]::Max(0, [Math]::Min($count - $rows, $target)) + # Why: at that end already there is nothing left to move, as ScrollPattern reports at the end of its content. + if ($target -eq $top) { return $true } + if ($null -eq [OrcaDesktopWin32]::SendControlMessage($list, 0x0197, [IntPtr][int]$target, [IntPtr]::Zero)) { + throw "window_not_responding: the list did not answer in time; it may still scroll, so run get-app-state before retrying" + } + $true +} + +function Invoke-OrcaScrollPattern($Element, $RootElement, $Process, [string]$Direction, [double]$Pages) { if ($null -eq $Element) { return $false } + if (Invoke-OrcaNativeListScroll $Element $RootElement $Process $Direction $Pages) { return $true } $vertical = $Direction -in @("up", "down") $forward = $Direction -in @("down", "right") $pattern = Get-OrcaScrollPattern $Element $RootElement $vertical @@ -1621,19 +2286,10 @@ function Send-OrcaDrag($From, $To) { } function Send-OrcaText([IntPtr]$WindowHandle, [string]$Text) { - [void][OrcaDesktopWin32]::SetForegroundWindow($WindowHandle) - $hasNonAscii = $false - foreach ($character in $Text.ToCharArray()) { - if ([int][char]$character -gt 0x7F) { $hasNonAscii = $true; break } + Assert-OrcaStillFocused $WindowHandle + if (-not [OrcaDesktopWin32]::SendUnicodeText($Text)) { + throw "SendInput did not deliver the text; inspect the field before retrying" } - if ($hasNonAscii) { - foreach ($character in $Text.ToCharArray()) { - [void][OrcaDesktopWin32]::PostMessage($WindowHandle, $WindowsMessages.Char, [IntPtr][int][char]$character, [IntPtr]::Zero) - Start-Sleep -Milliseconds 8 - } - return - } - [System.Windows.Forms.SendKeys]::SendWait((ConvertTo-OrcaSendKeysText $Text)) } function Get-OrcaVirtualKey([string]$Key) { @@ -1648,8 +2304,15 @@ function Get-OrcaVirtualKey([string]$Key) { throw "Unsupported key: $Key" } +function Assert-OrcaStillFocused([IntPtr]$WindowHandle) { + # Why: keys go to whatever is in front, so a user switching windows since the focus check must not receive them. + if (-not (Test-OrcaWindowFocused $WindowHandle)) { + throw "window_not_focused: the target window lost focus before the keys were sent; nothing was sent" + } +} + function Send-OrcaKey([IntPtr]$WindowHandle, [string]$Key) { - [void][OrcaDesktopWin32]::SetForegroundWindow($WindowHandle) + Assert-OrcaStillFocused $WindowHandle [System.Windows.Forms.SendKeys]::SendWait((ConvertTo-OrcaSendKeysKey $Key)) } @@ -1690,7 +2353,7 @@ function ConvertTo-OrcaSendKeysHotkey([string]$KeySpec) { function Send-OrcaHotkey([IntPtr]$WindowHandle, [string]$KeySpec) { $keys = ConvertTo-OrcaSendKeysHotkey $KeySpec - [void][OrcaDesktopWin32]::SetForegroundWindow($WindowHandle) + Assert-OrcaStillFocused $WindowHandle [System.Windows.Forms.SendKeys]::SendWait($keys) } @@ -1771,6 +2434,9 @@ function Invoke-OrcaOperation($Operation) { $list = Get-OrcaWindowList $Operation.app return [pscustomobject]@{ ok = $true; app = $list.app; windows = @($list.windows) } } + if ($Operation.tool -eq "launch_app") { + return Invoke-OrcaLaunchApp $Operation + } if ($Operation.tool -eq "get_app_state") { return [pscustomobject]@{ ok = $true; snapshot = New-OrcaSnapshot $Operation.app $includeScreenshot $Operation.windowId $Operation.windowIndex ([bool]$Operation.restoreWindow) $Operation.root } } @@ -1785,9 +2451,10 @@ function Invoke-OrcaOperation($Operation) { $fromElement = Find-OrcaElement $root $Operation.fromElement $toElement = Find-OrcaElement $root $Operation.toElement $handle = [IntPtr]$process.MainWindowHandle - if ($Operation.tool -in @("type_text", "press_key", "hotkey", "paste_text")) { + $typesIntoElement = $Operation.tool -eq "type_text" -and $null -ne $Operation.element + if ($Operation.tool -in @("type_text", "press_key", "hotkey", "paste_text") -and -not $typesIntoElement) { $restoreFailure = if ($restoreWindow) { Restore-OrcaWindow $process $root } else { $null } - Assert-OrcaKeyboardFocus $handle $Operation $restoreFailure + Assert-OrcaKeyboardFocus $handle $Operation $restoreFailure $process } $foregroundBefore = [OrcaDesktopWin32]::GetForegroundWindow() # Why: a stale or frameless target must fail before any refusal or raise; a minimized window has no frames until restored. @@ -1812,7 +2479,7 @@ function Invoke-OrcaOperation($Operation) { else { $null } $alternative = if ($null -eq $Operation.element) { "Click an element by --element-index instead when it lists Invoke, Select or Toggle." } else { "" } if ($null -eq $pointerNeed) { - if (Invoke-OrcaPrimaryAction $element $Operation.element.index) { + if (Invoke-OrcaPrimaryAction $element $Operation.element.index $process) { $action = [pscustomobject]@{ path = "accessibility"; actionName = "primaryAction"; fallbackReason = $null } break } @@ -1860,7 +2527,7 @@ function Invoke-OrcaOperation($Operation) { $at = Get-OrcaPointerPoint $null $null $Operation.x $Operation.y "x" "y" $windowFrame $scrollTarget = Find-OrcaScrollContainerAt $root $at.x $at.y ($Operation.direction -in @("up", "down")) } - if (Invoke-OrcaScrollPattern $scrollTarget $root $Operation.direction $pages) { + if (Invoke-OrcaScrollPattern $scrollTarget $root $process $Operation.direction $pages) { $action = [pscustomobject]@{ path = "accessibility"; actionName = "scroll"; fallbackReason = $null } break } @@ -1904,8 +2571,13 @@ function Invoke-OrcaOperation($Operation) { $action = [pscustomobject]@{ path = "synthetic"; actionName = "drag"; fallbackReason = $null } } "type_text" { - Send-OrcaText $handle (Get-OrcaRequiredString $Operation.text "text") - $action = [pscustomobject]@{ path = "synthetic"; actionName = "typeText"; fallbackReason = $null; verification = [pscustomobject]@{ state = "unverified"; reason = "synthetic_input" } } + $text = Get-OrcaRequiredString $Operation.text "text" + if ($typesIntoElement) { + $action = Invoke-OrcaElementTextInsertion $element $Operation.element $process $root $Operation $text + } else { + Send-OrcaText $handle $text + $action = [pscustomobject]@{ path = "synthetic"; actionName = "typeText"; fallbackReason = $null; verification = [pscustomobject]@{ state = "unverified"; reason = "synthetic_input" } } + } } "press_key" { Send-OrcaKey $handle (Get-OrcaRequiredString $Operation.key "key") @@ -1920,7 +2592,7 @@ function Invoke-OrcaOperation($Operation) { $action = [pscustomobject]@{ path = "clipboard"; actionName = "paste"; fallbackReason = $null; verification = [pscustomobject]@{ state = "unverified"; reason = "clipboard_paste" } } } "set_value" { - $written = if ($null -ne $element) { Set-OrcaElementValue $element ([string]$Operation.value) } else { $null } + $written = if ($null -ne $element) { Set-OrcaElementValue $element $process ([int]$Operation.element.index) ([string]$Operation.value) } else { $null } if ($null -eq $written) { throw "element value is not settable" } diff --git a/opencode.json b/opencode.json index 95f108f0888..1490f953255 100644 --- a/opencode.json +++ b/opencode.json @@ -22,5 +22,16 @@ "command": ["node", "config/scripts/typecheck/language-server/agent-typescript-lsp.mjs"], "extensions": [".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"] } + }, + "mcp": { + "orca-component-fixtures": { + "type": "local", + "command": [ + "node", + "--input-type=module", + "-e", + "import { existsSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { pathToFileURL } from 'node:url'; let dir = process.cwd(); for (;;) { const file = join(dir, 'config/scripts/component-fixtures/fixtures-mcp-server.mjs'); if (existsSync(file)) { await import(pathToFileURL(file).href); break; } const parent = dirname(dir); if (parent === dir) throw new Error('Cannot find the component fixtures server'); dir = parent; }" + ] + } } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index be8837a88c4..490176fe903 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -209,7 +209,7 @@ patchedDependencies: '@xterm/xterm@6.1.0-beta.336': 4b7fbfe7a7f626843ab88535cc6e2181872ea7c107876b15406a295182bc3c2c glob@13.0.6: 163edae2cab27bf7a8cf791c9a0ee368b596bfef898e490b3b77e7e44a8f0223 i18next-cli@1.74.2: 2ee32eef742615cd133e1a6940f027deb3723a8430bb7953bba3a6769b0aac47 - lint-staged@16.4.0: 7333b3837f80a7fbd045964db6d76ba4fc118e49134bdbabb00585b6b7b60673 + lint-staged@16.4.0: b3b398fa76585526ea579cbb07949ce69c2456afb5ab0b49e10de2de4ea76f6d minimatch@10.2.5: 4324868e550a69eee9addc4801001bee415e98339c9b2b88d6ab4fc6028741c6 minimatch@10.2.6: 4324868e550a69eee9addc4801001bee415e98339c9b2b88d6ab4fc6028741c6 minimatch@9.0.9: c356feccb9147b4d6a1cc72b10270defcb95d14cb42ee74ddfe7219341417161 @@ -544,7 +544,7 @@ importers: version: 0.18.10 lint-staged: specifier: ^16.4.0 - version: 16.4.0(patch_hash=7333b3837f80a7fbd045964db6d76ba4fc118e49134bdbabb00585b6b7b60673) + version: 16.4.0(patch_hash=b3b398fa76585526ea579cbb07949ce69c2456afb5ab0b49e10de2de4ea76f6d) lowlight: specifier: ^3.3.0 version: 3.3.0 @@ -13410,7 +13410,7 @@ snapshots: linkifyjs@4.3.3: {} - lint-staged@16.4.0(patch_hash=7333b3837f80a7fbd045964db6d76ba4fc118e49134bdbabb00585b6b7b60673): + lint-staged@16.4.0(patch_hash=b3b398fa76585526ea579cbb07949ce69c2456afb5ab0b49e10de2de4ea76f6d): dependencies: commander: 14.0.3 listr2: 9.0.5 diff --git a/skill-guides/orca-cli/references/browser.md b/skill-guides/orca-cli/references/browser.md index e62030329bb..89eddbb91f3 100644 --- a/skill-guides/orca-cli/references/browser.md +++ b/skill-guides/orca-cli/references/browser.md @@ -64,7 +64,7 @@ ORCA exec --page --command "get text @e3" --json Browser rules: -- Actions (`click`, `fill`, `type`, `keypress`, `select`, `check`, `hover`, `scroll`, `goto`, `back`, `reload`, `mouse` and the rest) wait briefly for the page to settle and then report what they caused in `result.effects` (text mode prints it under the action): `url`/`title` when they changed, `newTabs` with their `browserPageId`, blocked or opened `popups`, `downloads` started, `consoleErrors`, a `passkeyRequest` still waiting, and `page`: `"unchanged"`, or the snapshot lines the action added or replaced, with their refs (`full: true` means the whole new page). `loading: true` means a navigation was still running: `wait --load load`, then `snapshot`. No `page` means the page could not be read: run `snapshot`. `settled: false` means the page was still changing when it was read. An action that opens a JavaScript dialog fails with `browser_dialog_opened` instead. One that opens a file picker fails with `browser_file_chooser_opened`: Orca closes the picker so the system dialog never covers the user's screen; use `ORCA upload` on the file input or its button instead. +- Actions (`click`, `fill`, `type`, `keypress`, `select`, `check`, `hover`, `scroll`, `goto`, `back`, `reload`, `mouse` and the rest) wait briefly for the page to settle and then report what they caused in `result.effects` (text mode prints it under the action): `url`/`title` when they changed, `newTabs` with their `browserPageId`, blocked or opened `popups`, `downloads` started, `consoleErrors`, a `passkeyRequest` still waiting, `blockedPrint` when Orca refused the page's system print dialog (run `ORCA pdf` to save the page instead), and `page`: `"unchanged"`, or the snapshot lines the action added or replaced, with their refs (`full: true` means the whole new page). `loading: true` means a navigation was still running: `wait --load load`, then `snapshot`. No `page` means the page could not be read: run `snapshot`. `settled: false` means the page was still changing when it was read. An action that opens a JavaScript dialog fails with `browser_dialog_opened` instead. One that opens a file picker fails with `browser_file_chooser_opened`: Orca closes the picker so the system dialog never covers the user's screen; use `ORCA upload` on the file input or its button instead. - Snapshot again only when `page.truncated` is true, after `wait`, after a tab switch, on a `browser_stale_ref`, or when a result has no `effects` (older Orca, or a server-hosted page). - Pass `--expect-url ` with `--page` when acting on the wrong resource would matter, such as writing or sending in one conversation of a chat app. Use the exact absolute URL from `result.tab.url` in `ORCA tab current --json`, or `url` from a `snapshot --json` or `goto`. Path, query and fragment must all match, so a page that rewrites its URL while you work refuses until you read the URL again. If the page's URL differs, or a navigation to another URL is under way, the command fails with `browser_expected_url_mismatch`, and `error.data` holds `expectedUrl`, `actualUrl`, `browserPageId` and `mayHaveStarted`. `mayHaveStarted: false` means nothing ran; `true` means a step of a longer command (a typed run, a `select-all`) may have happened, so look at the page before retrying. Success adds `browserPageId` and `checkedUrl`, the URL last checked; the action itself may have navigated since, and `effects.url` says where. The check runs right before the action and again before each later step, but it is not a lock: a route change in the instant after a check can still receive the action. It compares the top-level page only, so it says nothing about an iframe or an app that shows different content at one URL; read the page for those. `exec` refuses the flag, because one raw batch can navigate and retarget. A runtime, client or desktop that cannot check the URL refuses with `incompatible_runtime` or `browser_expect_url_unsupported` rather than acting unguarded. - Refs like `@e1` are assigned by `snapshot`, scoped to one tab, and invalidated by navigation or tab switch. @@ -74,7 +74,7 @@ Browser rules: - When the user says they left notes or comments on a page, read them with `ORCA tab annotations --page ` (the current tab without `--page`). It prints the notes they haven't sent, the same text the tab's Copy button gives, and `--json` returns them in `result.annotations`. Notes the user already sent arrive in your chat instead. - Passkey, fingerprint, Face ID, Windows Hello and security-key prompts can't be completed by you. When `snapshot` or an action's `effects` has `passkeyRequest`, the page is waiting on one from that origin and will wait for minutes. Ask the user to finish the sign-in, or run `ORCA dialog dismiss --page ` to cancel it so the page offers another way to sign in. - A popup window a page opens (sign-in, payment) is listed in `ORCA tab list --json` as a `popup-` page with `openerBrowserPageId`. If a click seems to do nothing, check `tab list`, then drive the popup with `--page popup-`. When it closes, continue on the opener page. -- Browser commands default to the current worktree. `--page` reaches the page in any worktree unless you also pass `--worktree `. Use `--worktree all` only intentionally. +- Browser commands default to the worktree you are in: your cwd's worktree, else your Orca terminal's. Outside both (a shell or IDE Orca did not open, with a cwd outside any worktree), and on every `--environment` command, always pass `--page ` or `--worktree ` (not `all`); find pages with `ORCA tab list --json`. Without either, a local command acts on the tab the user is looking at, and an `--environment` command is refused. `--page` reaches the page in any worktree unless you also pass `--worktree `. Use `--worktree all` only intentionally. - `tab list` shows every tab in the tab bar. After a restart, tabs the user has not opened report `loaded: false`; `tab switch` or any `--page` command loads one in the background without changing the user's visible tab. - `tab create --split ` opens the page in a new pane beside the calling terminal instead of as another tab; focus stays in the terminal. When a browser pane is already open beside it, the page opens as that pane's visible tab instead of adding another pane. - Use typed tab commands (`ORCA tab list/create/close/switch`), not `ORCA exec --command "tab ..."`, so Orca keeps UI state synchronized. diff --git a/skill-guides/orca-computer-use.md b/skill-guides/orca-computer-use.md index 97bc63f0d1d..d9725c08874 100644 --- a/skill-guides/orca-computer-use.md +++ b/skill-guides/orca-computer-use.md @@ -87,6 +87,7 @@ For apps with multiple windows or ambiguous titles, run `list-windows` first. Pr ORCA computer permissions --json ORCA computer capabilities --json ORCA computer list-apps --json +ORCA computer launch-app --json ORCA computer list-windows --app --json ORCA computer get-app-state --app --json ORCA computer wait --app (--text | --enabled | --window-title ) [--timeout ] --json @@ -129,7 +130,7 @@ printf '%s' "$TEXT" | ORCA computer set-value --app --element-index ` inserts at that text field's caret, replacing its selection, without focusing it or bringing its window forward, and reads the text back. It works on macOS and Linux; where `capabilities` lacks `actions.typeTextElement` it fails with `unsupported_capability`; then click the field, confirm it is the focused text receiver, and type without the index. A `value_not_settable` error means that element does not accept text insertion. +- `type-text --element-index ` inserts at that text field's caret, replacing its selection, without focusing it or bringing its window forward, and reads the text back. It works on macOS, Linux and Windows; where `capabilities` lacks `actions.typeTextElement` it fails with `unsupported_capability`; then click the field, confirm it is the focused text receiver, and type without the index. A `value_not_settable` error means that element does not accept text insertion. - Use `press-key` for single/navigation keys such as Return, Escape, Tab, and arrows. Use `hotkey` only for one modifier chord plus one key, such as `CmdOrCtrl+A` or `CmdOrCtrl+Shift+P`; prefer `CmdOrCtrl+...` for cross-platform combos. - Shortcuts that quit or close the app, log out, lock the screen, force-quit or end the session (such as `Cmd+Q` on macOS, `Alt+F4` on Windows and Linux, `Ctrl+Q` on Linux) are refused with `system_shortcut_blocked`. The error says what the shortcut would do; reach that goal another way, such as the window's own close button or menu item. Add `--allow-system-shortcut` only when the user asked for that exact shortcut. - Use `click --modifiers ` for modifier-clicks. Never synthesize separate modifier-down and modifier-up commands around a click; interruption can leave a modifier logically held. @@ -146,22 +147,22 @@ These need the foreground, so ask the user first: - An action fails with `background_unavailable` and no semantic action (`set-value`, `perform-secondary-action`, `type-text --element-index`) can do the job. - A background action reports success but the UI does not change, and no semantic action can do the job. - On macOS, drags in Chromium or Electron apps, and apps that read the hardware pointer (games, OpenGL/Metal views, Java/Tk), which ignore background pointer events. -- On Windows, keyboard input (`press-key`, `hotkey`, `paste-text` and `type-text`) needs the target window focused; otherwise it is refused with `window_not_focused` and nothing is sent. For editable fields use `set-value`. +- On Windows, keyboard input (`press-key`, `hotkey`, `paste-text` and `type-text` without `--element-index`) needs the target window in front; otherwise it is refused with `background_unavailable` and nothing is sent. Enter text with `type-text --element-index` or `set-value`. - On Linux and Windows, actions that need the real pointer, and on Linux keys sent while the target window is not active (Return in a focused GTK text field works). They are refused with `background_unavailable`; see below. -On macOS, a window that is minimized, hidden (Cmd+H) or on another Space cannot be read, so `get-app-state` fails with `window_not_found`. Ask the user to bring it on screen. +On macOS, `list-windows` and `get-app-state` also read windows that are minimized, hidden (Cmd+H) or on another Space, full-screen ones included; `isMinimized` and `isOffscreen` say which. Element actions work there (`click --element-index`, `set-value`, `type-text --element-index`, secondary actions), while coordinate clicks, scrolls, drags, keys and pastes are refused with `background_unavailable` and nothing is sent. Minimized and hidden windows have no screenshot. A window on another Space that nothing has read yet fails with `window_not_found`; ask the user to show it once. On macOS, background actions that would open a menu or dialog over the user are refused before anything is sent: right-clicks and Control-clicks, clicks or drags that start on popup menus, menu buttons, native combo boxes and web ``, set the option with `set-value` on that element. On macOS, every action without `--restore-window` runs in the background: the target app is not brought forward, the user's frontmost app and key window stay put, and the real pointer does not move. `capabilities` reports this as `supports.input.background`. -On Linux, element clicks through the element's accessibility action, `set-value`, `type-text --element-index` and secondary actions run without raising the window or moving the pointer, and Return in a focused GTK text field runs its activate action. Anything that needs the real pointer (clicks with no accessibility action, right, middle, multiple or modified clicks, scrolling, drags) or the focused window (other keys, typing with no focused editable field) is refused with `background_unavailable` and nothing is sent; so is a click that would open a menu, a keyboard-grabbing popup or a dialog (a GTK 4 or Qt label ending in `…`). Ask the user before retrying with `--restore-window`, which raises only the target window, refuses a window on another workspace and puts the cursor back. On Wayland, synthetic pointer and key input never reaches native windows, so it is refused even with the flag. `action.delivery` is measured: `foreground` when the input went to the window in front or the target came forward. +On Linux, element clicks through the element's accessibility action (or, for a list row with none, its list's selection), `set-value`, `type-text --element-index` and secondary actions run without raising the window or moving the pointer, and Return in a focused GTK text field runs its activate action. Scrolling moves the nearest scroll bar under the element or point by whole viewports, or pages a web document, also in the background. Anything that needs the real pointer (other clicks with no accessibility action, right, middle, multiple or modified clicks, scrolls in a view with no scroll bar or document, drags) or the focused window (other keys, typing with no focused editable field) is refused with `background_unavailable` and nothing is sent; so is a click that would open a menu, a keyboard-grabbing popup or a dialog (a GTK 4 or Qt label ending in `…`). Ask the user before retrying with `--restore-window`, which raises only the target window, refuses a window on another workspace and puts the cursor back. On Wayland, synthetic pointer and key input never reaches native windows, so it is refused even with the flag. `action.delivery` is measured: `foreground` when the input went to the window in front or the target came forward. -On Windows, these actions go through UI Automation when the control supports them, without moving the pointer or sending keys: element clicks, `set-value` (including sliders), scrolls of an element or of the scrollable area under `--x`/`--y` (fractional `--pages` too), and secondary actions such as `Invoke`, `Toggle`, `Select`, `Expand`, `Collapse` and `ScrollToVisible` (the last brings a list or tree item into view). Some apps bring their own window forward when driven this way (WPF apps do for most of these), so read `action.delivery`: `background` means the user's foreground window stayed put, `foreground` means the target came forward. Clicks by coordinates, right, middle, modified and multi-clicks, clicks on elements with no `Invoke`, `Select` or `Toggle` action, scrolls over nothing scrollable, and drags need the real mouse, so they are refused with `background_unavailable` and nothing is sent. With `--restore-window` (after asking the user), only the target window comes forward, never one on another virtual desktop, and the pointer goes back where it was. +On Windows, these actions go through UI Automation when the control supports them, without moving the pointer or sending keys: element clicks, `set-value` (including sliders), `type-text --element-index`, scrolls of an element or of the scrollable area under `--x`/`--y` (fractional `--pages` too), and secondary actions such as `Invoke`, `Toggle`, `Select`, `Expand`, `Collapse` and `ScrollToVisible` (the last brings a list or tree item into view). WinForms buttons, check boxes and radio buttons, and native text boxes, combo boxes and list boxes (user32 and WinForms), are driven through their own window messages, which leave the window where it is. User32 buttons, check boxes and radio buttons use UI Automation, so they can bring their window forward. Other apps can bring their own window forward when driven this way (WPF apps do for most of these), so read `action.delivery`: `background` means the user's foreground window stayed put, `foreground` means the target came forward. Clicks by coordinates, right, middle, modified and multi-clicks, clicks on elements with no `Invoke`, `Select` or `Toggle` action, scrolls over nothing scrollable, and drags need the real mouse, so they are refused with `background_unavailable` and nothing is sent. `type-text` without `--element-index`, `press-key`, `hotkey` and `paste-text` send keys, which need the window in front, so they are refused the same way; enter text with `type-text --element-index` or `set-value` instead. With `--restore-window` (after asking the user), only the target window comes forward, never one on another virtual desktop, and the pointer goes back where it was. ## Starting Apps -`orca computer` does not launch apps. On macOS, start one without bringing it forward with `open -g -a ` (or `open -g -b `); plain `open -a` brings it to the front. Some apps, such as many Electron apps, still bring their own window forward when it opens. On Windows and Linux a newly started app usually comes to the front, so ask the user before starting one. +Start a closed app with `ORCA computer launch-app `, not `open -a` or `Start-Process`: those bring the app to the front and take the user's keyboard. `launch-app` asks macOS and Windows to start it in the background and leaves an app that is already running alone. Some apps, such as many Electron apps, still bring their own window forward when they open, so read `delivery`: if it says `foreground`, tell the user the app came to the front. Pass `--activate` only when the user asked for the app to come forward. On Linux it fails with `unsupported_capability`, because a newly started app usually comes to the front there; ask the user before starting one another way. ## Screenshots diff --git a/src/cli/arguments/args-repeatable-flags.test.ts b/src/cli/arguments/args-repeatable-flags.test.ts index 03f54f9bb64..8427da58b41 100644 --- a/src/cli/arguments/args-repeatable-flags.test.ts +++ b/src/cli/arguments/args-repeatable-flags.test.ts @@ -30,17 +30,15 @@ describe('command-scoped repeatable flags', () => { expect(parsed.get('path')).toBe('/c') }) - it.each([ - ['--agent', '--agent=b'], - ['--agent=a', '--agent', '--agent=b'], - ['--agent=a', '--agent'], - ['--agent', '--agent'] - ])('rejects a missing occurrence even among valid values: %j', (...options) => { - const parsed = parseArgs(['example', 'search', ...options], paths, specs) - expect(() => validateCommandAndFlags(specs, parsed)).toThrow('Flag --agent requires a value.') - }) + it.each([['--agent=a', '--agent', '--agent=b']])( + 'rejects a missing occurrence even among valid values: %j', + (...options) => { + const parsed = parseArgs(['example', 'search', ...options], paths, specs) + expect(() => validateCommandAndFlags(specs, parsed)).toThrow('Flag --agent requires a value.') + } + ) - it.each(['label', 'skill'])('also rejects erased missing legacy --%s values', (flag) => { + it.each(['label'])('also rejects erased missing legacy --%s values', (flag) => { const spec = { ...leaf, allowedFlags: [flag], repeatableFlags: [] } const parsed = parseArgs(['ex', 'find', `--${flag}=a`, `--${flag}`, `--${flag}=b`], paths, [ spec diff --git a/src/cli/arguments/args.test.ts b/src/cli/arguments/args.test.ts index e77d0451dd3..6adc1a934ff 100644 --- a/src/cli/arguments/args.test.ts +++ b/src/cli/arguments/args.test.ts @@ -1,12 +1,7 @@ import { describe, expect, it } from 'vitest' import type { CommandSpec } from '../discovery/command-spec' -import { - findCommandSpec, - normalizeCommandPositionals, - parseArgs, - validateCommandAndFlags -} from './args' +import { normalizeCommandPositionals, parseArgs, validateCommandAndFlags } from './args' describe('parseArgs', () => { it('keeps an empty string as a flag value', () => { @@ -17,39 +12,12 @@ describe('parseArgs', () => { expect(parsed.flags.get('json')).toBe(true) }) - it('accepts a flag value that starts with -- via the = form', () => { - const parsed = parseArgs(['terminal', 'send', '--text=--help']) - - expect(parsed.commandPath).toEqual(['terminal', 'send']) - expect(parsed.flags.get('text')).toBe('--help') - }) - it('splits --flag=value on the first = so values may contain =', () => { const parsed = parseArgs(['set', 'cookie', '--value=a=b=c']) expect(parsed.flags.get('value')).toBe('a=b=c') }) - it('treats --flag= as an empty string value', () => { - const parsed = parseArgs(['--value=']) - - expect(parsed.flags.get('value')).toBe('') - }) - - it('does not consume a command token after an unknown flag', () => { - const parsed = parseArgs(['--jso', 'worktree', 'list'], [['worktree', 'list']]) - - expect(parsed.commandPath).toEqual(['worktree', 'list']) - expect(parsed.flags.get('jso')).toBe(true) - }) - - it('does not consume a command token after a flag valid on another command', () => { - const parsed = parseArgs(['--workspace', 'worktree', 'list'], [['worktree', 'list']]) - - expect(parsed.commandPath).toEqual(['worktree', 'list']) - expect(parsed.flags.get('workspace')).toBe(true) - }) - it('recognizes a command path with a boolean flag between its segments', () => { const parsed = parseArgs(['--jso', 'worktree', '--json', 'list'], [['worktree', 'list']]) @@ -69,30 +37,6 @@ describe('parseArgs', () => { expect(parsed.flags.get('repo')).toBe('id:abc') }) - it('still consumes a pre-command flag value before a later command path', () => { - const parsed = parseArgs(['--workspace', 'team-1', 'linear', 'list'], [['linear', 'list']]) - - expect(parsed.commandPath).toEqual(['linear', 'list']) - expect(parsed.flags.get('workspace')).toBe('team-1') - }) - - it('preserves existing pre-command selector values', () => { - const parsed = parseArgs(['--repo', 'id:abc', 'worktree', 'list'], [['worktree', 'list']]) - - expect(parsed.commandPath).toEqual(['worktree', 'list']) - expect(parsed.flags.get('repo')).toBe('id:abc') - }) - - it('preserves a project selector before the project command', () => { - const parsed = parseArgs( - ['--project', 'github:stablyai/orca', 'project', 'setups'], - [['project', 'setups']] - ) - - expect(parsed.commandPath).toEqual(['project', 'setups']) - expect(parsed.flags.get('project')).toBe('github:stablyai/orca') - }) - it('preserves a selector value that is also a registered command', () => { const parsed = parseArgs( ['--environment', 'status', 'worktree', 'list'], @@ -113,16 +57,6 @@ describe('parseArgs', () => { expect(parsed.flags.get('environment')).toBe('worktree') }) - it.each([ - ['--project', 'project', 'project', 'setups'], - ['--project=project', 'project', 'setups'] - ])('preserves a command-named project selector in %j', (...args) => { - const parsed = parseArgs(args, [['project', 'setups']]) - - expect(parsed.commandPath).toEqual(['project', 'setups']) - expect(parsed.flags.get('project')).toBe('project') - }) - it('normalizes partial positionals without conflicting later flag-supplied args', () => { const parsed = normalizeCommandPositionals( [ @@ -151,12 +85,6 @@ describe('parseArgs', () => { expect(parsed.flags.get('permission')).toBe('android.permission.CAMERA') expect(parsed.positionalFlagConflicts).toEqual([]) }) - - it('does not apply repeated flag encoding to ordinary string flags', () => { - const parsed = parseArgs(['linear', 'list', '--workspace', 'old', '--workspace', 'new']) - - expect(parsed.flags.get('workspace')).toBe('new') - }) }) describe('command aliases', () => { @@ -170,20 +98,9 @@ describe('command aliases', () => { summary: 'Remove a worktree', usage: 'orca worktree rm --worktree ', allowedFlags: ['worktree', 'force'] - }, - { - path: ['repo', 'show'], - summary: 'Show a repo', - usage: 'orca repo show --repo ', - allowedFlags: ['repo'], - positionalArgs: ['repo'] } ] - it('resolves an aliased path to the canonical spec', () => { - expect(findCommandSpec(specs, ['worktree', 'remove'])?.path).toEqual(['worktree', 'rm']) - }) - it('canonicalizes an aliased command path during normalization', () => { const normalized = normalizeCommandPositionals( specs, @@ -193,13 +110,6 @@ describe('command aliases', () => { expect(normalized.commandPath).toEqual(['worktree', 'rm']) expect(normalized.flags.get('force')).toBe(true) }) - - it('binds a trailing positional after canonicalizing the base path', () => { - const normalized = normalizeCommandPositionals(specs, parseArgs(['repo', 'show', 'id:abc'])) - - expect(normalized.commandPath).toEqual(['repo', 'show']) - expect(normalized.flags.get('repo')).toBe('id:abc') - }) }) describe('validateCommandAndFlags', () => { @@ -212,13 +122,13 @@ describe('validateCommandAndFlags', () => { } ] - it.each(['environment', 'pairing-code'])('rejects --%s without a value', (flag) => { + it.each(['environment'])('rejects --%s without a value', (flag) => { const parsed = parseArgs([`--${flag}`, 'demo'], [['demo']]) expect(() => validateCommandAndFlags(specs, parsed)).toThrow(`Flag --${flag} requires a value.`) }) - it.each(['environment', 'pairing-code'])('rejects an empty --%s= value', (flag) => { + it.each(['environment'])('rejects an empty --%s= value', (flag) => { const parsed = parseArgs(['demo', `--${flag}=`]) expect(() => validateCommandAndFlags(specs, parsed)).toThrow(`Flag --${flag} requires a value.`) diff --git a/src/cli/browser/browser-command-target.ts b/src/cli/browser/browser-command-target.ts index cad6d2825d8..7364a0c9bcc 100644 --- a/src/cli/browser/browser-command-target.ts +++ b/src/cli/browser/browser-command-target.ts @@ -4,6 +4,7 @@ import { getOptionalStringFlag, getPresentStringFlag } from '../arguments/flags' import type { RuntimeClient } from '../runtime/client' import { RuntimeClientError } from '../runtime/types' import { assertLocalCwdWorktreeSelector } from '../hosts/local-environment-selector' +import { quoteCliCommandArgument } from '../arguments/shell-command-quote' import { getBrowserWorktreeSelector, normalizeWorktreeSelectorForCaller, @@ -46,6 +47,31 @@ async function getEnforceableExpectUrl( return expectUrl } +/** A paired server's fallback is the tab a person is looking at, so a remote command must name its target. */ +export async function getScopedBrowserWorktreeSelector( + flags: Map, + cwd: string, + client: RuntimeClient +): Promise { + const worktree = await getBrowserWorktreeSelector(flags, cwd, client) + if (worktree === undefined && client.isRemote) { + const environment = client.environmentSelector + const remote = environment + ? ` --environment ${quoteCliCommandArgument(environment)}` + : ' --pairing-code ' + throw new RuntimeClientError( + 'invalid_argument', + 'Remote browser commands need --page or a server-side --worktree (not `all`); without one they would act on the tab the user is looking at. Nothing was sent.', + { + nextSteps: [ + `Find the page with \`orca tab list${remote} --json\` and pass its browserPageId as --page.` + ] + } + ) + } + return worktree +} + async function getBrowserPageTarget( flags: Map, cwd: string, @@ -53,7 +79,7 @@ async function getBrowserPageTarget( ): Promise { const page = getOptionalStringFlag(flags, 'page') if (!page) { - return { worktree: await getBrowserWorktreeSelector(flags, cwd, client) } + return { worktree: await getScopedBrowserWorktreeSelector(flags, cwd, client) } } const explicitWorktree = getOptionalStringFlag(flags, 'worktree') diff --git a/src/cli/browser/browser-environment-scope.test.ts b/src/cli/browser/browser-environment-scope.test.ts new file mode 100644 index 00000000000..4225398fbd8 --- /dev/null +++ b/src/cli/browser/browser-environment-scope.test.ts @@ -0,0 +1,86 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +const { callMock, runtimeClientConstructorMock, serveOrcaAppMock, getDefaultUserDataPathMock } = + vi.hoisted(() => ({ + callMock: vi.fn(), + runtimeClientConstructorMock: vi.fn(), + serveOrcaAppMock: vi.fn(), + getDefaultUserDataPathMock: vi.fn(() => '/tmp/orca-user-data') + })) + +vi.mock('../runtime/client', async () => { + const { createRuntimeClientModuleMock } = await import('../index-test-harness.js') + return createRuntimeClientModuleMock({ + callMock, + runtimeClientConstructorMock, + serveOrcaAppMock, + getDefaultUserDataPathMock + }) +}) + +vi.mock('../runtime/launch', async (importOriginal) => ({ + ...(await importOriginal>()), + serveOrcaApp: serveOrcaAppMock +})) + +vi.mock('../runtime/metadata', async (importOriginal) => ({ + ...(await importOriginal>()), + getDefaultUserDataPath: getDefaultUserDataPathMock +})) + +vi.mock( + '../../shared/remote-runtime/host-store/runtime-environment-store', + async (importOriginal) => ({ + ...(await importOriginal>()), + listEnvironments: () => [{ id: 'env-1', name: 'server' }] + }) +) + +import { main } from '../index' +import { okFixture } from '../runtime/fixtures/runtime-rpc-fixtures' + +const browserCalls = (): string[] => + callMock.mock.calls.map(([method]) => String(method)).filter((m) => m.startsWith('browser.')) + +// Unscoped, a paired server resolves the tab its person is looking at. +describe('browser commands over --environment', () => { + beforeEach(() => { + callMock.mockReset() + callMock.mockImplementation(async (method: string) => + method === 'ssh.listTargetSummaries' + ? okFixture('ssh', { targets: [] }) + : okFixture('result', { browserPageId: 'page-1' }) + ) + vi.spyOn(console, 'log').mockImplementation(() => {}) + vi.spyOn(console, 'error').mockImplementation(() => {}) + process.exitCode = undefined + }) + + afterEach(() => { + vi.restoreAllMocks() + process.exitCode = undefined + }) + + it.each([ + [['click', '--element', '@e1']], + [['goto', '--url', 'https://example.com']], + [['tab', 'current']], + [['tab', 'create', '--url', 'https://example.com']], + [['click', '--element', '@e1', '--worktree', 'all']] + ])('refuses %j without --page or --worktree, sending nothing', async (args) => { + await main([...args, '--environment', 'server', '--json'], '/tmp/not-an-orca-worktree') + + expect(browserCalls()).toEqual([]) + expect(process.exitCode).toBe(1) + }) + + it('sends a command that names its page', async () => { + await main( + ['click', '--element', '@e1', '--page', 'page-1', '--environment', 'server', '--json'], + '/tmp/not-an-orca-worktree' + ) + + expect(browserCalls()).toEqual(['browser.click']) + expect(process.exitCode).toBeUndefined() + }) +}) diff --git a/src/cli/browser/browser-format.ts b/src/cli/browser/browser-format.ts index b6e80a3a3d5..23dc073761a 100644 --- a/src/cli/browser/browser-format.ts +++ b/src/cli/browser/browser-format.ts @@ -204,6 +204,11 @@ export function formatBrowserActionEffects(result: unknown): string { if (effects.passkeyRequest) { lines.push(formatPasskeyRequest(effects.passkeyRequest)) } + if (effects.blockedPrint) { + lines.push( + `${effects.blockedPrint.origin} tried to open the system print dialog, which Orca blocked so it wouldn't take over the user's screen. To save the page as a PDF, run \`pdf\`.` + ) + } if (effects.loading) { lines.push('Still loading; run `wait --load load`, then `snapshot`.') } else if (effects.page === undefined) { diff --git a/src/cli/browser/browser-tab.ts b/src/cli/browser/browser-tab.ts index 3cf37c338c4..1e891b732a9 100644 --- a/src/cli/browser/browser-tab.ts +++ b/src/cli/browser/browser-tab.ts @@ -22,12 +22,12 @@ import { import { RuntimeClientError } from '../runtime/types' import { isBrowserTabSplitDirection } from '../../shared/browser-tab-split-direction' import { getBrowserWorktreeSelector } from '../workspace-selection/workspace-selector' -import { getBrowserCommandTarget } from './browser-command-target' +import { getBrowserCommandTarget, getScopedBrowserWorktreeSelector } from './browser-command-target' export const BROWSER_TAB_HANDLERS: Record = { 'open-url': async ({ flags, client, cwd, json }) => { const url = getRequiredStringFlag(flags, 'url') - const worktree = await getBrowserWorktreeSelector(flags, cwd, client) + const worktree = await getScopedBrowserWorktreeSelector(flags, cwd, client) const result = await client.call<{ browserPageId: string }>( 'browser.openUrl', { url, worktree }, @@ -54,7 +54,7 @@ export const BROWSER_TAB_HANDLERS: Record = { printResult(result, json, formatTabAnnotations) }, 'tab current': async ({ flags, client, cwd, json }) => { - const worktree = await getBrowserWorktreeSelector(flags, cwd, client) + const worktree = await getScopedBrowserWorktreeSelector(flags, cwd, client) const result = await client.call('browser.tabCurrent', { worktree }) printResult(result, json, formatTabShow) }, @@ -82,7 +82,7 @@ export const BROWSER_TAB_HANDLERS: Record = { if (split !== undefined && !isBrowserTabSplitDirection(split)) { throw new RuntimeClientError('invalid_argument', '--split must be right, left, down, or up') } - const worktree = await getBrowserWorktreeSelector(flags, cwd, client) + const worktree = await getScopedBrowserWorktreeSelector(flags, cwd, client) // Why the caller's tab: the split should sit beside the terminal that ran the command, not // wherever the user last clicked. Spread conditionally so a plain create keeps its old payload. const callerTabId = process.env.ORCA_TAB_ID diff --git a/src/cli/computer/computer-format.ts b/src/cli/computer/computer-format.ts index 926aa478d6f..cbe0a17d047 100644 --- a/src/cli/computer/computer-format.ts +++ b/src/cli/computer/computer-format.ts @@ -4,6 +4,7 @@ import { quoteCliCommandArgument } from '../arguments/shell-command-quote' import type { ComputerActionMetadata, ComputerActionResult, + ComputerLaunchAppResult, ComputerListAppsResult, ComputerListWindowsResult, ComputerSnapshotResult @@ -108,6 +109,12 @@ export function formatListApps(result: ComputerListAppsResult): string { .join('\n') } +export function formatLaunchApp(result: ComputerLaunchAppResult): string { + const verb = result.launched ? 'Launched' : 'Already running:' + const place = result.delivery === 'foreground' ? 'in the foreground' : 'in the background' + return `${verb} ${result.app.name} pid:${result.app.pid} ${place}` +} + export function formatListWindows(result: ComputerListWindowsResult): string { if (result.windows.length === 0) { return `No windows found for ${result.app.name}.` diff --git a/src/cli/computer/computer.ts b/src/cli/computer/computer.ts index 0b2249bf794..0c9d77ff986 100644 --- a/src/cli/computer/computer.ts +++ b/src/cli/computer/computer.ts @@ -1,5 +1,6 @@ import type { ComputerActionResult, + ComputerLaunchAppResult, ComputerListAppsResult, ComputerListWindowsResult, ComputerProviderCapabilities, @@ -18,6 +19,7 @@ import { RuntimeClientError } from '../runtime/types' import { formatComputerAction, formatGetAppState, + formatLaunchApp, formatListApps, formatListWindows } from './computer-format' @@ -49,6 +51,7 @@ export const COMPUTER_HANDLERS: Record = { }, 'computer permissions': async ({ flags, client, json }) => { const id = getComputerPermissionSetupId(flags) + // Agents run this on errors; System Settings over the user's work is only worth it when something is missing. const result = await client.call<{ platform: NodeJS.Platform helperAppPath: string | null @@ -57,7 +60,7 @@ export const COMPUTER_HANDLERS: Record = { launchQueued?: boolean permissions?: { id: string; status: string }[] nextStep?: string | null - }>('computer.permissions', id ? { id } : {}) + }>('computer.permissions', id ? { id, skipGranted: true } : {}) printResult(result, json, (value) => { if (value.platform !== 'darwin') { return 'Computer-use permission setup is only required on macOS.' @@ -78,6 +81,13 @@ export const COMPUTER_HANDLERS: Record = { .join('\n') }) }, + 'computer launch-app': async ({ flags, client, json }) => { + const result = await client.call('computer.launchApp', { + app: getRequiredStringFlag(flags, 'app'), + ...(flags.get('activate') === true ? { activate: true } : {}) + }) + printResult(result, json, formatLaunchApp) + }, 'computer list-windows': async ({ flags, client, json }) => { const result = await client.call('computer.listWindows', { app: getRequiredStringFlag(flags, 'app') @@ -268,7 +278,7 @@ function getComputerPermissionSetupId( function formatComputerCapabilities(value: ComputerProviderCapabilities): string { return [ `${value.provider} (${value.platform}, protocol ${value.protocolVersion})`, - ` Apps: list=${value.supports.apps.list} bundleIds=${value.supports.apps.bundleIds} pids=${value.supports.apps.pids}`, + ` Apps: list=${value.supports.apps.list} bundleIds=${value.supports.apps.bundleIds} pids=${value.supports.apps.pids} launch=${value.supports.apps.launch === true}`, ` Windows: list=${value.supports.windows.list} targetById=${value.supports.windows.targetById} targetByIndex=${value.supports.windows.targetByIndex}`, ` Observation: screenshot=${value.supports.observation.screenshot} elementFrames=${value.supports.observation.elementFrames} annotatedScreenshot=${value.supports.observation.annotatedScreenshot}`, ` Actions: ${Object.entries(value.supports.actions) diff --git a/src/cli/discovery/command-scoped-flag-help.ts b/src/cli/discovery/command-scoped-flag-help.ts index ba18dc9b3c3..7588b1043cf 100644 --- a/src/cli/discovery/command-scoped-flag-help.ts +++ b/src/cli/discovery/command-scoped-flag-help.ts @@ -24,6 +24,9 @@ const COMMAND_SCOPED_FLAG_HELP: Record> = { query: '--query Show only elements whose line contains this text, with their ancestors' }, + 'computer launch-app': { + activate: '--activate Start the app in front; only when the user asked for it' + }, 'computer wait': { text: '--text Wait until an element line contains this text', enabled: diff --git a/src/cli/discovery/root-help-text-primary.ts b/src/cli/discovery/root-help-text-primary.ts index fc062da5526..6542a3ba371 100644 --- a/src/cli/discovery/root-help-text-primary.ts +++ b/src/cli/discovery/root-help-text-primary.ts @@ -189,6 +189,7 @@ export const ROOT_HELP_TEXT_PRIMARY = [ ' computer capabilities Show computer-use provider capabilities', ' computer permissions Show or open computer-use permission setup', ' computer list-apps List running apps available to computer-use', + ' computer launch-app Start a closed app without taking the foreground', ' 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', diff --git a/src/cli/dispatch/handler-group-manifest.ts b/src/cli/dispatch/handler-group-manifest.ts index 44bbf6624ff..204b9947ac6 100644 --- a/src/cli/dispatch/handler-group-manifest.ts +++ b/src/cli/dispatch/handler-group-manifest.ts @@ -185,6 +185,7 @@ export const HANDLER_GROUPS: readonly HandlerGroup[] = [ 'computer capabilities', 'computer list-apps', 'computer permissions', + 'computer launch-app', 'computer list-windows', 'computer get-app-state', 'computer wait', diff --git a/src/cli/runtime/client.ts b/src/cli/runtime/client.ts index ff260807296..fdd82cd39e9 100644 --- a/src/cli/runtime/client.ts +++ b/src/cli/runtime/client.ts @@ -308,8 +308,13 @@ export class RuntimeClient { } async openOrca(timeoutMs = 15_000): Promise> { - return this.remoteSelected - ? this.getCliStatus() - : openLocalOrca(() => this.getCliStatus(), timeoutMs) + if (this.remoteSelected) { + return this.getCliStatus() + } + return openLocalOrca( + () => this.getCliStatus(), + (options) => this.call<{ requested: boolean }>('desktop.openWindowInBackground', {}, options), + timeoutMs + ) } } diff --git a/src/cli/runtime/envelope-schema.test.ts b/src/cli/runtime/envelope-schema.test.ts deleted file mode 100644 index 2fd6b7d83c4..00000000000 --- a/src/cli/runtime/envelope-schema.test.ts +++ /dev/null @@ -1,50 +0,0 @@ -import '../../shared/remote-runtime/host-store/environment-store-worker-test-fixture' -import { describe, expect, it } from 'vitest' -import { RuntimeRpcEnvelopeSchema } from './envelope-schema' - -describe('RuntimeRpcEnvelopeSchema', () => { - it('accepts and strips additive future success fields', () => { - const parsed = RuntimeRpcEnvelopeSchema.safeParse({ - id: 'req-1', - ok: true, - result: { value: 1, futureResultField: true }, - futureEnvelopeField: 'new', - _meta: { runtimeId: 'runtime-1', futureMetaField: 2 } - }) - - expect(parsed).toEqual({ - success: true, - data: { - id: 'req-1', - ok: true, - result: { value: 1, futureResultField: true }, - _meta: { runtimeId: 'runtime-1' } - } - }) - }) - - it('accepts and strips additive future failure fields', () => { - const parsed = RuntimeRpcEnvelopeSchema.safeParse({ - id: 'req-1', - ok: false, - futureEnvelopeField: 'new', - error: { - code: 'failed', - message: 'Nope', - data: { retryable: true }, - futureErrorField: 2 - }, - _meta: { runtimeId: 'runtime-1', futureMetaField: 3 } - }) - - expect(parsed).toEqual({ - success: true, - data: { - id: 'req-1', - ok: false, - error: { code: 'failed', message: 'Nope', data: { retryable: true } }, - _meta: { runtimeId: 'runtime-1' } - } - }) - }) -}) diff --git a/src/cli/runtime/launch.ts b/src/cli/runtime/launch.ts index 526c689e764..103fe160f93 100644 --- a/src/cli/runtime/launch.ts +++ b/src/cli/runtime/launch.ts @@ -21,6 +21,7 @@ import { superviseForegroundServe } from './serve-update-supervisor' import { RuntimeClientError } from './types' +import { BACKGROUND_OPEN_ARG } from '../../shared/background-open-argv' import { getExecutableAppArgs, resolveAppRoot, @@ -69,7 +70,7 @@ export function launchOrcaApp(): void { const overrideExecutable = process.env.ORCA_APP_EXECUTABLE if (typeof overrideExecutable === 'string' && overrideExecutable.trim().length > 0) { const pinned = pinLaunchUserDataPath( - getExecutableAppArgs(overrideExecutable), + [...getExecutableAppArgs(overrideExecutable), BACKGROUND_OPEN_ARG], stripElectronRunAsNode(process.env), userDataPath ) @@ -93,7 +94,7 @@ export function launchOrcaApp(): void { } const pinned = pinLaunchUserDataPath( - getExecutableAppArgs(process.execPath), + [...getExecutableAppArgs(process.execPath), BACKGROUND_OPEN_ARG], stripElectronRunAsNode(process.env), userDataPath ) @@ -107,12 +108,19 @@ export function launchOrcaApp(): void { ) } -/** Nondefault profiles need a new instance: open drops env and only new instances receive --args. */ +/** Nondefault profiles need a new instance: open drops env and only new instances receive --args. -g keeps the user's app in front. */ export function getMacOpenArgs(appBundlePath: string, userDataPath: string): string[] { if (isSameUserDataPath(userDataPath, getPlatformUserDataPath())) { - return [appBundlePath] + return ['-g', appBundlePath, '--args', BACKGROUND_OPEN_ARG] } - return ['-n', appBundlePath, '--args', `--user-data-dir=${userDataPath}`] + return [ + '-g', + '-n', + appBundlePath, + '--args', + `--user-data-dir=${userDataPath}`, + BACKGROUND_OPEN_ARG + ] } function spawnDetached(command: string, args: string[], options: SpawnOptions): void { diff --git a/src/cli/runtime/open-local-orca.test.ts b/src/cli/runtime/open-local-orca.test.ts new file mode 100644 index 00000000000..560268cee14 --- /dev/null +++ b/src/cli/runtime/open-local-orca.test.ts @@ -0,0 +1,71 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' +import type { CliStatusResult } from '../../shared/runtime-types' +import { launchOrcaApp } from './launch' +import { openLocalOrca } from './open-local-orca' +import type { RuntimeRpcSuccess } from './types' + +vi.mock('./launch', () => ({ launchOrcaApp: vi.fn() })) + +function status(desktopWindowStatus: string): RuntimeRpcSuccess { + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: openLocalOrca reads only runtime.reachable and app.desktopWindowStatus. + return { + ok: true, + result: { runtime: { reachable: true }, app: { desktopWindowStatus } } + } as unknown as RuntimeRpcSuccess +} + +describe('openLocalOrca', () => { + beforeEach(() => { + vi.mocked(launchOrcaApp).mockClear() + }) + + it('asks a running Orca to show its window instead of relaunching it', async () => { + const readStatus = vi + .fn() + .mockResolvedValueOnce(status('openable')) + .mockResolvedValueOnce(status('available')) + const openWindowInBackground = vi.fn(async () => ({ + id: '1', + ok: true as const, + result: { requested: true }, + _meta: { runtimeId: 'runtime' } + })) + + await openLocalOrca(readStatus, openWindowInBackground, 1_000) + + expect(openWindowInBackground).toHaveBeenCalledOnce() + expect(launchOrcaApp).not.toHaveBeenCalled() + }) + + it('does not relaunch an older Orca whose window is already available', async () => { + const available = status('available') + + await expect( + openLocalOrca( + async () => available, + async () => { + throw new Error('method_not_found') + }, + 1_000 + ) + ).resolves.toBe(available) + expect(launchOrcaApp).not.toHaveBeenCalled() + }) + + it('launches Orca when an older headless runtime cannot open its own window', async () => { + const readStatus = vi + .fn() + .mockResolvedValueOnce(status('openable')) + .mockResolvedValueOnce(status('available')) + + await openLocalOrca( + readStatus, + async () => { + throw new Error('method_not_found') + }, + 1_000 + ) + + expect(launchOrcaApp).toHaveBeenCalledOnce() + }) +}) diff --git a/src/cli/runtime/open-local-orca.ts b/src/cli/runtime/open-local-orca.ts index 18c87338954..aafbeab3f8f 100644 --- a/src/cli/runtime/open-local-orca.ts +++ b/src/cli/runtime/open-local-orca.ts @@ -3,21 +3,34 @@ import { launchOrcaApp } from './launch' import { RuntimeClientError, RuntimeRpcFailureError, type RuntimeRpcSuccess } from './types' type ReadStatus = () => Promise> +type OpenWindowInBackground = (options: { + timeoutMs: number +}) => Promise> + +// The runtime only queues a window request; a stuck one must not hold `orca open` for a full RPC timeout. +const OPEN_WINDOW_RPC_TIMEOUT_MS = 5_000 export async function openLocalOrca( readStatus: ReadStatus, + openWindowInBackground: OpenWindowInBackground, timeoutMs: number ): Promise> { const initial = await getStatusForOpen(readStatus) + const windowStatus = initial?.result.app.desktopWindowStatus // A blocked runtime cannot open a window; a second app would only hit its instance lock. - if (initial?.result.app.desktopWindowStatus === 'blocked') { + if (windowStatus === 'blocked') { throwDesktopActivationBlocked() } - launchOrcaApp() - if (initial?.result.app.desktopWindowStatus === 'available') { + // Relaunching a running Orca would hand it the foreground, so ask it to show its own window. + const asked = + initial?.result.runtime.reachable === true && (await askToOpenWindow(openWindowInBackground)) + if (initial && windowStatus === 'available') { return initial } + if (!asked) { + launchOrcaApp() + } const startedAt = Date.now() while (Date.now() - startedAt < timeoutMs) { @@ -55,6 +68,16 @@ async function getStatusForOpen( } } +async function askToOpenWindow(openWindowInBackground: OpenWindowInBackground): Promise { + try { + const response = await openWindowInBackground({ timeoutMs: OPEN_WINDOW_RPC_TIMEOUT_MS }) + return response.result.requested === true + } catch { + // Older runtimes lack the method; any failure falls back to relaunching, as open always did. + return false + } +} + function throwDesktopActivationBlocked(): never { throw new RuntimeClientError( 'desktop_activation_blocked', diff --git a/src/cli/runtime/serve-signal-exit-diagnostic.test.ts b/src/cli/runtime/serve-signal-exit-diagnostic.test.ts index f9afaa9d528..06bdc4da685 100644 --- a/src/cli/runtime/serve-signal-exit-diagnostic.test.ts +++ b/src/cli/runtime/serve-signal-exit-diagnostic.test.ts @@ -1,18 +1,10 @@ import '../../shared/remote-runtime/host-store/environment-store-worker-test-fixture' -import { MacServeStartupAbortError, serveSignalExitError } from './serve-signal-exit-diagnostic' import { EventEmitter } from 'node:events' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, describe, expect, it, vi } from 'vitest' -import { - SERVE_CHILD_FORCE_KILL_SCHEDULING_MARGIN_MS, - superviseForegroundServe -} from './serve-update-supervisor' -import { - QUIT_RENDERER_ACK_TIMEOUT_MS, - WILL_QUIT_TEARDOWN_DEADLINE_MS -} from '../../shared/quit-teardown-deadline' +import { superviseForegroundServe } from './serve-update-supervisor' class FakeChildProcess extends EventEmitter { kill = vi.fn() @@ -41,67 +33,7 @@ afterEach(() => { vi.useRealTimers() }) -describe('serveSignalExitError', () => { - it('classifies darwin SIGABRT as a startup abort', () => { - const error = serveSignalExitError('SIGABRT', 'darwin') - expect(error).toBeInstanceOf(MacServeStartupAbortError) - }) -}) - describe('superviseForegroundServe signal exits', () => { - it('lets pre-commit and committed Electron quit deadlines finish before force-killing serve', async () => { - setPlatform('linux') - vi.useFakeTimers() - const child = new FakeChildProcess() - const supervised = superviseChild(child) - - process.emit('SIGTERM', 'SIGTERM') - expect(child.kill).toHaveBeenCalledOnce() - expect(child.kill).toHaveBeenLastCalledWith('SIGTERM') - - await vi.advanceTimersByTimeAsync(QUIT_RENDERER_ACK_TIMEOUT_MS + WILL_QUIT_TEARDOWN_DEADLINE_MS) - expect(child.kill).toHaveBeenCalledOnce() - - await vi.advanceTimersByTimeAsync(SERVE_CHILD_FORCE_KILL_SCHEDULING_MARGIN_MS) - expect(child.kill).toHaveBeenLastCalledWith('SIGKILL') - expect(child.kill).toHaveBeenCalledTimes(2) - - child.emit('exit', null, 'SIGKILL') - await expect(supervised).rejects.toThrow('Orca serve exited via SIGKILL.') - }) - - it('lets a shared-console Windows child handle Ctrl-C gracefully', async () => { - setPlatform('win32') - vi.useFakeTimers() - const child = new FakeChildProcess() - const supervised = superviseChild(child) - - process.emit('SIGINT', 'SIGINT') - expect(child.kill).not.toHaveBeenCalled() - - child.emit('exit', 0, null) - await expect(supervised).resolves.toBe(0) - }) - - it('forwards Linux terminal hangup and removes the listener after exit', async () => { - setPlatform('linux') - const listenersBefore = process.listeners('SIGHUP') - const child = new FakeChildProcess() - const supervised = superviseChild(child) - - expect(process.listeners('SIGHUP')).toHaveLength(listenersBefore.length + 1) - process.emit('SIGHUP', 'SIGHUP') - expect(child.kill).toHaveBeenCalledWith('SIGHUP') - - child.emit('exit', null, 'SIGHUP') - await expect(supervised).resolves.toBe(0) - expect(process.listeners('SIGHUP')).toEqual(listenersBefore) - - const killCallsAfterExit = child.kill.mock.calls.length - process.emit('SIGHUP', 'SIGHUP') - expect(child.kill).toHaveBeenCalledTimes(killCallsAfterExit) - }) - it('treats a child exit through the caller-forwarded SIGINT as graceful', async () => { setPlatform('linux') const child = new FakeChildProcess() diff --git a/src/cli/runtime/websocket-transport.ts b/src/cli/runtime/websocket-transport.ts index e79ac676dff..2b0350ef31b 100644 --- a/src/cli/runtime/websocket-transport.ts +++ b/src/cli/runtime/websocket-transport.ts @@ -7,6 +7,7 @@ import { RemoteRuntimeClientError } from '../../shared/remote-runtime/remote-run import { AGENT_LAUNCH_RUNTIME_CAPABILITY } from '../../shared/agent-launch/contracts/agent-launch-runtime-capability' import { AGENT_SESSION_RENAME_ONLY_CLIENT_CAPABILITY } from '../../shared/agent-session/structured-agent-session-surface-capabilities' import { REPO_SEARCH_QUALIFIED_REFS_RUNTIME_CAPABILITY } from '../../shared/worktree-runtime-capabilities' +import { CLI_AGENT_CALLER_CLIENT_CAPABILITY } from '../../shared/remote-runtime/cli-agent-caller-capability' import { sendRemoteRuntimeRequestOnSocket } from '../../shared/remote-runtime/requests/remote-runtime-request-socket' import type { RuntimeStatus } from '../../shared/runtime-types' import { RuntimeClientError, type RuntimeRpcResponse } from './types' @@ -15,7 +16,8 @@ import { RuntimeClientError, type RuntimeRpcResponse } from './types' const CLI_CLIENT_CAPABILITIES = [ AGENT_LAUNCH_RUNTIME_CAPABILITY, REPO_SEARCH_QUALIFIED_REFS_RUNTIME_CAPABILITY, - AGENT_SESSION_RENAME_ONLY_CLIENT_CAPABILITY + AGENT_SESSION_RENAME_ONLY_CLIENT_CAPABILITY, + CLI_AGENT_CALLER_CLIENT_CAPABILITY ] as const export async function sendWebSocketRequest( diff --git a/src/cli/skills/bundled-skill-guides.ts b/src/cli/skills/bundled-skill-guides.ts index 4dc65ebf80d..3ab42d774f1 100644 --- a/src/cli/skills/bundled-skill-guides.ts +++ b/src/cli/skills/bundled-skill-guides.ts @@ -737,7 +737,7 @@ const ORCA_CLI_BROWSER_REFERENCE_MARKDOWN = [ "", "Browser rules:", "", - "- Actions (`click`, `fill`, `type`, `keypress`, `select`, `check`, `hover`, `scroll`, `goto`, `back`, `reload`, `mouse` and the rest) wait briefly for the page to settle and then report what they caused in `result.effects` (text mode prints it under the action): `url`/`title` when they changed, `newTabs` with their `browserPageId`, blocked or opened `popups`, `downloads` started, `consoleErrors`, a `passkeyRequest` still waiting, and `page`: `\"unchanged\"`, or the snapshot lines the action added or replaced, with their refs (`full: true` means the whole new page). `loading: true` means a navigation was still running: `wait --load load`, then `snapshot`. No `page` means the page could not be read: run `snapshot`. `settled: false` means the page was still changing when it was read. An action that opens a JavaScript dialog fails with `browser_dialog_opened` instead. One that opens a file picker fails with `browser_file_chooser_opened`: Orca closes the picker so the system dialog never covers the user's screen; use `ORCA upload` on the file input or its button instead.", + "- Actions (`click`, `fill`, `type`, `keypress`, `select`, `check`, `hover`, `scroll`, `goto`, `back`, `reload`, `mouse` and the rest) wait briefly for the page to settle and then report what they caused in `result.effects` (text mode prints it under the action): `url`/`title` when they changed, `newTabs` with their `browserPageId`, blocked or opened `popups`, `downloads` started, `consoleErrors`, a `passkeyRequest` still waiting, `blockedPrint` when Orca refused the page's system print dialog (run `ORCA pdf` to save the page instead), and `page`: `\"unchanged\"`, or the snapshot lines the action added or replaced, with their refs (`full: true` means the whole new page). `loading: true` means a navigation was still running: `wait --load load`, then `snapshot`. No `page` means the page could not be read: run `snapshot`. `settled: false` means the page was still changing when it was read. An action that opens a JavaScript dialog fails with `browser_dialog_opened` instead. One that opens a file picker fails with `browser_file_chooser_opened`: Orca closes the picker so the system dialog never covers the user's screen; use `ORCA upload` on the file input or its button instead.", "- Snapshot again only when `page.truncated` is true, after `wait`, after a tab switch, on a `browser_stale_ref`, or when a result has no `effects` (older Orca, or a server-hosted page).", "- Pass `--expect-url ` with `--page` when acting on the wrong resource would matter, such as writing or sending in one conversation of a chat app. Use the exact absolute URL from `result.tab.url` in `ORCA tab current --json`, or `url` from a `snapshot --json` or `goto`. Path, query and fragment must all match, so a page that rewrites its URL while you work refuses until you read the URL again. If the page's URL differs, or a navigation to another URL is under way, the command fails with `browser_expected_url_mismatch`, and `error.data` holds `expectedUrl`, `actualUrl`, `browserPageId` and `mayHaveStarted`. `mayHaveStarted: false` means nothing ran; `true` means a step of a longer command (a typed run, a `select-all`) may have happened, so look at the page before retrying. Success adds `browserPageId` and `checkedUrl`, the URL last checked; the action itself may have navigated since, and `effects.url` says where. The check runs right before the action and again before each later step, but it is not a lock: a route change in the instant after a check can still receive the action. It compares the top-level page only, so it says nothing about an iframe or an app that shows different content at one URL; read the page for those. `exec` refuses the flag, because one raw batch can navigate and retarget. A runtime, client or desktop that cannot check the URL refuses with `incompatible_runtime` or `browser_expect_url_unsupported` rather than acting unguarded.", "- Refs like `@e1` are assigned by `snapshot`, scoped to one tab, and invalidated by navigation or tab switch.", @@ -747,7 +747,7 @@ const ORCA_CLI_BROWSER_REFERENCE_MARKDOWN = [ "- When the user says they left notes or comments on a page, read them with `ORCA tab annotations --page ` (the current tab without `--page`). It prints the notes they haven't sent, the same text the tab's Copy button gives, and `--json` returns them in `result.annotations`. Notes the user already sent arrive in your chat instead.", "- Passkey, fingerprint, Face ID, Windows Hello and security-key prompts can't be completed by you. When `snapshot` or an action's `effects` has `passkeyRequest`, the page is waiting on one from that origin and will wait for minutes. Ask the user to finish the sign-in, or run `ORCA dialog dismiss --page ` to cancel it so the page offers another way to sign in.", "- A popup window a page opens (sign-in, payment) is listed in `ORCA tab list --json` as a `popup-` page with `openerBrowserPageId`. If a click seems to do nothing, check `tab list`, then drive the popup with `--page popup-`. When it closes, continue on the opener page.", - "- Browser commands default to the current worktree. `--page` reaches the page in any worktree unless you also pass `--worktree `. Use `--worktree all` only intentionally.", + "- Browser commands default to the worktree you are in: your cwd's worktree, else your Orca terminal's. Outside both (a shell or IDE Orca did not open, with a cwd outside any worktree), and on every `--environment` command, always pass `--page ` or `--worktree ` (not `all`); find pages with `ORCA tab list --json`. Without either, a local command acts on the tab the user is looking at, and an `--environment` command is refused. `--page` reaches the page in any worktree unless you also pass `--worktree `. Use `--worktree all` only intentionally.", "- `tab list` shows every tab in the tab bar. After a restart, tabs the user has not opened report `loaded: false`; `tab switch` or any `--page` command loads one in the background without changing the user's visible tab.", "- `tab create --split ` opens the page in a new pane beside the calling terminal instead of as another tab; focus stays in the terminal. When a browser pane is already open beside it, the page opens as that pane's visible tab instead of adding another pane.", "- Use typed tab commands (`ORCA tab list/create/close/switch`), not `ORCA exec --command \"tab ...\"`, so Orca keeps UI state synchronized.", @@ -1108,6 +1108,7 @@ const ORCA_COMPUTER_USE_MARKDOWN = [ "ORCA computer permissions --json", "ORCA computer capabilities --json", "ORCA computer list-apps --json", + "ORCA computer launch-app --json", "ORCA computer list-windows --app --json", "ORCA computer get-app-state --app --json", "ORCA computer wait --app (--text | --enabled | --window-title ) [--timeout ] --json", @@ -1150,7 +1151,7 @@ const ORCA_COMPUTER_USE_MARKDOWN = [ "- Prefer semantic actions: `set-value` for editable fields, `click` for controls, and `perform-secondary-action` only for listed action names.", "- After any UI-changing action, use the returned state or rerun `get-app-state` before choosing the next element index.", "- Use `type-text` only after focusing a field and confirming the app has a focused text receiver; unless the result says `verified focusedText`, inspect the returned state before assuming text landed.", - "- `type-text --element-index ` inserts at that text field's caret, replacing its selection, without focusing it or bringing its window forward, and reads the text back. It works on macOS and Linux; where `capabilities` lacks `actions.typeTextElement` it fails with `unsupported_capability`; then click the field, confirm it is the focused text receiver, and type without the index. A `value_not_settable` error means that element does not accept text insertion.", + "- `type-text --element-index ` inserts at that text field's caret, replacing its selection, without focusing it or bringing its window forward, and reads the text back. It works on macOS, Linux and Windows; where `capabilities` lacks `actions.typeTextElement` it fails with `unsupported_capability`; then click the field, confirm it is the focused text receiver, and type without the index. A `value_not_settable` error means that element does not accept text insertion.", "- Use `press-key` for single/navigation keys such as Return, Escape, Tab, and arrows. Use `hotkey` only for one modifier chord plus one key, such as `CmdOrCtrl+A` or `CmdOrCtrl+Shift+P`; prefer `CmdOrCtrl+...` for cross-platform combos.", "- Shortcuts that quit or close the app, log out, lock the screen, force-quit or end the session (such as `Cmd+Q` on macOS, `Alt+F4` on Windows and Linux, `Ctrl+Q` on Linux) are refused with `system_shortcut_blocked`. The error says what the shortcut would do; reach that goal another way, such as the window's own close button or menu item. Add `--allow-system-shortcut` only when the user asked for that exact shortcut.", "- Use `click --modifiers ` for modifier-clicks. Never synthesize separate modifier-down and modifier-up commands around a click; interruption can leave a modifier logically held.", @@ -1167,22 +1168,22 @@ const ORCA_COMPUTER_USE_MARKDOWN = [ "- An action fails with `background_unavailable` and no semantic action (`set-value`, `perform-secondary-action`, `type-text --element-index`) can do the job.", "- A background action reports success but the UI does not change, and no semantic action can do the job.", "- On macOS, drags in Chromium or Electron apps, and apps that read the hardware pointer (games, OpenGL/Metal views, Java/Tk), which ignore background pointer events.", - "- On Windows, keyboard input (`press-key`, `hotkey`, `paste-text` and `type-text`) needs the target window focused; otherwise it is refused with `window_not_focused` and nothing is sent. For editable fields use `set-value`.", + "- On Windows, keyboard input (`press-key`, `hotkey`, `paste-text` and `type-text` without `--element-index`) needs the target window in front; otherwise it is refused with `background_unavailable` and nothing is sent. Enter text with `type-text --element-index` or `set-value`.", "- On Linux and Windows, actions that need the real pointer, and on Linux keys sent while the target window is not active (Return in a focused GTK text field works). They are refused with `background_unavailable`; see below.", "", - "On macOS, a window that is minimized, hidden (Cmd+H) or on another Space cannot be read, so `get-app-state` fails with `window_not_found`. Ask the user to bring it on screen.", + "On macOS, `list-windows` and `get-app-state` also read windows that are minimized, hidden (Cmd+H) or on another Space, full-screen ones included; `isMinimized` and `isOffscreen` say which. Element actions work there (`click --element-index`, `set-value`, `type-text --element-index`, secondary actions), while coordinate clicks, scrolls, drags, keys and pastes are refused with `background_unavailable` and nothing is sent. Minimized and hidden windows have no screenshot. A window on another Space that nothing has read yet fails with `window_not_found`; ask the user to show it once.", "", "On macOS, background actions that would open a menu or dialog over the user are refused before anything is sent: right-clicks and Control-clicks, clicks or drags that start on popup menus, menu buttons, native combo boxes and web ``, set the option with `set-value` on that element.", "", "On macOS, every action without `--restore-window` runs in the background: the target app is not brought forward, the user's frontmost app and key window stay put, and the real pointer does not move. `capabilities` reports this as `supports.input.background`.", "", - "On Linux, element clicks through the element's accessibility action, `set-value`, `type-text --element-index` and secondary actions run without raising the window or moving the pointer, and Return in a focused GTK text field runs its activate action. Anything that needs the real pointer (clicks with no accessibility action, right, middle, multiple or modified clicks, scrolling, drags) or the focused window (other keys, typing with no focused editable field) is refused with `background_unavailable` and nothing is sent; so is a click that would open a menu, a keyboard-grabbing popup or a dialog (a GTK 4 or Qt label ending in `…`). Ask the user before retrying with `--restore-window`, which raises only the target window, refuses a window on another workspace and puts the cursor back. On Wayland, synthetic pointer and key input never reaches native windows, so it is refused even with the flag. `action.delivery` is measured: `foreground` when the input went to the window in front or the target came forward.", + "On Linux, element clicks through the element's accessibility action (or, for a list row with none, its list's selection), `set-value`, `type-text --element-index` and secondary actions run without raising the window or moving the pointer, and Return in a focused GTK text field runs its activate action. Scrolling moves the nearest scroll bar under the element or point by whole viewports, or pages a web document, also in the background. Anything that needs the real pointer (other clicks with no accessibility action, right, middle, multiple or modified clicks, scrolls in a view with no scroll bar or document, drags) or the focused window (other keys, typing with no focused editable field) is refused with `background_unavailable` and nothing is sent; so is a click that would open a menu, a keyboard-grabbing popup or a dialog (a GTK 4 or Qt label ending in `…`). Ask the user before retrying with `--restore-window`, which raises only the target window, refuses a window on another workspace and puts the cursor back. On Wayland, synthetic pointer and key input never reaches native windows, so it is refused even with the flag. `action.delivery` is measured: `foreground` when the input went to the window in front or the target came forward.", "", - "On Windows, these actions go through UI Automation when the control supports them, without moving the pointer or sending keys: element clicks, `set-value` (including sliders), scrolls of an element or of the scrollable area under `--x`/`--y` (fractional `--pages` too), and secondary actions such as `Invoke`, `Toggle`, `Select`, `Expand`, `Collapse` and `ScrollToVisible` (the last brings a list or tree item into view). Some apps bring their own window forward when driven this way (WPF apps do for most of these), so read `action.delivery`: `background` means the user's foreground window stayed put, `foreground` means the target came forward. Clicks by coordinates, right, middle, modified and multi-clicks, clicks on elements with no `Invoke`, `Select` or `Toggle` action, scrolls over nothing scrollable, and drags need the real mouse, so they are refused with `background_unavailable` and nothing is sent. With `--restore-window` (after asking the user), only the target window comes forward, never one on another virtual desktop, and the pointer goes back where it was.", + "On Windows, these actions go through UI Automation when the control supports them, without moving the pointer or sending keys: element clicks, `set-value` (including sliders), `type-text --element-index`, scrolls of an element or of the scrollable area under `--x`/`--y` (fractional `--pages` too), and secondary actions such as `Invoke`, `Toggle`, `Select`, `Expand`, `Collapse` and `ScrollToVisible` (the last brings a list or tree item into view). WinForms buttons, check boxes and radio buttons, and native text boxes, combo boxes and list boxes (user32 and WinForms), are driven through their own window messages, which leave the window where it is. User32 buttons, check boxes and radio buttons use UI Automation, so they can bring their window forward. Other apps can bring their own window forward when driven this way (WPF apps do for most of these), so read `action.delivery`: `background` means the user's foreground window stayed put, `foreground` means the target came forward. Clicks by coordinates, right, middle, modified and multi-clicks, clicks on elements with no `Invoke`, `Select` or `Toggle` action, scrolls over nothing scrollable, and drags need the real mouse, so they are refused with `background_unavailable` and nothing is sent. `type-text` without `--element-index`, `press-key`, `hotkey` and `paste-text` send keys, which need the window in front, so they are refused the same way; enter text with `type-text --element-index` or `set-value` instead. With `--restore-window` (after asking the user), only the target window comes forward, never one on another virtual desktop, and the pointer goes back where it was.", "", "## Starting Apps", "", - "`orca computer` does not launch apps. On macOS, start one without bringing it forward with `open -g -a ` (or `open -g -b `); plain `open -a` brings it to the front. Some apps, such as many Electron apps, still bring their own window forward when it opens. On Windows and Linux a newly started app usually comes to the front, so ask the user before starting one.", + "Start a closed app with `ORCA computer launch-app `, not `open -a` or `Start-Process`: those bring the app to the front and take the user's keyboard. `launch-app` asks macOS and Windows to start it in the background and leaves an app that is already running alone. Some apps, such as many Electron apps, still bring their own window forward when they open, so read `delivery`: if it says `foreground`, tell the user the app came to the front. Pass `--activate` only when the user asked for the app to come forward. On Linux it fails with `unsupported_capability`, because a newly started app usually comes to the front there; ask the user before starting one another way.", "", "## Screenshots", "", diff --git a/src/cli/specs/computer.ts b/src/cli/specs/computer.ts index 661321d6f70..4960c022ff5 100644 --- a/src/cli/specs/computer.ts +++ b/src/cli/specs/computer.ts @@ -34,6 +34,18 @@ export const COMPUTER_COMMAND_SPECS: CommandSpec[] = [ usage: 'orca computer permissions [--id ] [--json]', allowedFlags: [...GLOBAL_FLAGS, 'id'] }, + { + path: ['computer', 'launch-app'], + summary: 'Start a closed app without taking the foreground', + usage: 'orca computer launch-app [--activate] [--json]', + allowedFlags: [...GLOBAL_FLAGS, 'app', 'activate'], + positionalArgs: ['app'], + notes: [ + 'Leaves an app that is already running as it is. Use this instead of `open -a` or Start-Process, which bring the app to the front.', + 'Use --activate only when the user asked for the app to come forward: it takes the keyboard from whatever they are doing. It applies only when launch-app starts the app.', + 'macOS and Windows only.' + ] + }, { path: ['computer', 'list-windows'], summary: 'List windows for an app available to computer-use', diff --git a/src/cli/specs/core.ts b/src/cli/specs/core.ts index 7c6b71a20ae..c91a201418e 100644 --- a/src/cli/specs/core.ts +++ b/src/cli/specs/core.ts @@ -16,7 +16,7 @@ export const CORE_COMMAND_SPECS: CommandSpec[] = [ usage: 'orca open [--json]', allowedFlags: [...GLOBAL_FLAGS], notes: [ - "It can bring Orca's window to the front. Agents should run it only when the user asks or `orca status --json` reports Orca is not running or still cannot connect after about 30 seconds of retries, never to retry a failed command." + 'Starts Orca without taking the foreground and does nothing when an Orca window is already open. Agents should run it only when the user asks or `orca status --json` reports Orca is not running or still cannot connect after about 30 seconds of retries, never to retry a failed command.' ], examples: ['orca open', 'orca open --json'] }, diff --git a/src/main/acp/discovery/acp-listing-session-models.test.ts b/src/main/acp/discovery/acp-listing-session-models.test.ts new file mode 100644 index 00000000000..987d1237c05 --- /dev/null +++ b/src/main/acp/discovery/acp-listing-session-models.test.ts @@ -0,0 +1,313 @@ +import { createStructuredAgentSessionCatalogReads } from '../../native-chat/agent-session-wire/options/structured-agent-session-catalog-reads' +import type { StructuredAgentSessionHostDeps } from '../../native-chat/agent-session-wire/host/structured-agent-session-host-types' +import type { createAgentModelCatalogSuccessWriter } from '../../native-chat/agent-model-catalog/agent-model-catalog-entry-lifetime' +import { createAcpModelCatalogProbe } from './acp-model-catalog-probe' +import { AgentModelCatalogStore } from '../../native-chat/agent-model-catalog/agent-model-catalog-store' +import { createAgentModelCatalogService } from '../../native-chat/agent-model-catalog/agent-model-catalog-service' +import { NewSessionResponseSchema } from '../generated/acp-protocol.generated' +import recorded from '../fixtures/opencode-v2-start-options.json' +import { expect, it, vi, onTestFinished } from 'vitest' +import { acpLaunchSpecFor } from '../launch/acp-launch-registry' +import { + readSessionModels, + type AcpModelCatalogSessionConnection +} from './acp-listing-session-models' +import type { runAgentModelCatalogListing } from '../../native-chat/agent-model-catalog/agent-model-catalog-probe-runner' +import { runAgentModelCatalogSession } from '../../native-chat/agent-model-catalog/agent-model-catalog-probe-runner' + +const LEARNED_EFFORTS = [ + { value: 'low', label: 'Low' }, + { value: 'high', label: 'High' }, + { value: 'max', label: 'Max' }, + { value: 'default', label: 'Default' } +] + +it('publishes inventory before a slow optional effort reply and retains its valid late result', async () => { + vi.useFakeTimers() + onTestFinished(() => { + vi.useRealTimers() + }) + const late = + Promise.withResolvers< + Awaited> + >() + const observations: Awaited>[] = [] + const connection: AcpModelCatalogSessionConnection = { + initialize: async () => ({ + protocolVersion: 1, + agentCapabilities: { sessionCapabilities: { delete: {} } } + }), + start: async () => ({ + kind: 'new', + sessionId: 'listing', + response: NewSessionResponseSchema.parse(recorded) + }), + setConfigOption: () => late.promise, + deleteSession: async () => ({}), + close: async () => {} + } + const run = runAgentModelCatalogSession< + AcpModelCatalogSessionConnection, + Awaited> + >( + () => connection, + (connection, ready) => + readSessionModels( + acpLaunchSpecFor('opencode2')!, + { kind: 'session', listingNamesConfiguredModel: false }, + connection, + '/workspace', + undefined, + ['opencode/big-pickle'], + undefined, + ready, + (result) => observations.push(result) + ), + { label: 'opencode2', timeoutMs: null } + ) + await vi.advanceTimersByTimeAsync(45_000) + expect(observations[0]?.models.find((row) => row.id === 'opencode/big-pickle')).toMatchObject({ + efforts: [], + effortsKnown: false + }) + const options = structuredClone(recorded.configOptions) + options.find((option) => option.id === 'model')!.currentValue = 'opencode/big-pickle' + late.resolve({ + configOptions: NewSessionResponseSchema.parse({ ...recorded, configOptions: options }) + .configOptions! + }) + const result = await run + expect(result.models.find((row) => row.id === 'opencode/big-pickle')?.efforts).toEqual([ + { value: 'low', label: 'Low' }, + { value: 'high', label: 'High' }, + { value: 'max', label: 'Max' }, + { value: 'default', label: 'Default' } + ]) + expect(result.attemptedModels).toEqual(['opencode/big-pickle']) + expect( + observations.at(-1)?.models.find((row) => row.id === 'opencode/big-pickle')?.efforts + ).toEqual(result.models.find((row) => row.id === 'opencode/big-pickle')?.efforts) + expect( + observations[0]?.models.find((row) => row.id === 'opencode/big-pickle')?.effortsKnown + ).toBe(false) +}) + +function accumulatedCatalogHarness(count: number) { + const baseSpec = acpLaunchSpecFor('opencode2')! + const spec = { + ...baseSpec, + managedRuntime: undefined, + account: { + ...baseSpec.account, + environment: (_home: unknown, env: Record) => env + } + } + const config = (selected = 'm0') => + NewSessionResponseSchema.parse({ + ...recorded, + configOptions: recorded.configOptions.map((option) => + option.id === 'model' + ? { + ...option, + currentValue: selected, + options: Array.from({ length: count }, (_, index) => ({ + value: `m${index}`, + name: `Model ${index}` + })) + } + : option + ) + }) + const requests: { + model: string + reply: PromiseWithResolvers< + Awaited> + > + }[] = [] + const connect = vi.fn((): AcpModelCatalogSessionConnection => ({ + initialize: async () => ({ + protocolVersion: 1, + agentCapabilities: { sessionCapabilities: { delete: {} } } + }), + start: async () => ({ kind: 'new', sessionId: 'listing', response: config() }), + setConfigOption: (_id, model) => { + const reply = + Promise.withResolvers< + Awaited> + >() + requests.push({ model, reply }) + return reply.promise + }, + deleteSession: async () => ({}), + close: async () => {} + })) + const store = new AgentModelCatalogStore() + const writes = vi.spyOn( + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: measure the production publication boundary required by the scaling regression. + store as unknown as { writeSuccess: ReturnType }, + 'writeSuccess' + ) + const probe = createAcpModelCatalogProbe(spec, { + resolveEnvironment: async () => ({}), + resolveCommand: () => process.execPath, + probeVersion: async () => ({ version: '2.1.0' }), + homePath: '/account', + dataDirectory: '/unused', + connectSession: connect + }) + const service = createAgentModelCatalogService({ + store, + getRecord: () => undefined, + getConversation: () => undefined, + drivesRecord: () => true, + resolveAccountHome: async () => ({ variable: 'HOME', path: '/account' }), + probes: { opencode2: probe } + }) + const externalRead = createStructuredAgentSessionCatalogReads(() => ({ + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: sessionless catalog reads only access modelCatalog; no chat or journal is mocked. + deps: { modelCatalog: service } as StructuredAgentSessionHostDeps, + serialize: async (_id, task) => task(), + scopeOwned: async () => null + })) + const read = async (waitForListing = true, selectedModel?: string) => { + const result = await externalRead({ + agent: 'opencode2', + workspacePath: '/workspace', + waitForListing, + selectedModel + }) + if (result.origin === 'unknown') { + throw new Error('Expected the listing inventory') + } + return result + } + onTestFinished(() => service.stop()) + const answer = (index: number) => { + const request = requests[index]! + request.reply.resolve({ configOptions: config(request.model).configOptions! }) + } + return { store, writes, service, requests, connect, read, answer } +} + +it.each([10, 100])( + 'accumulates %i untargeted menus without publishing the full catalog per reply', + async (count) => { + vi.useFakeTimers() + onTestFinished(() => { + vi.useRealTimers() + }) + const h = accumulatedCatalogHarness(count) + const first = h.read() + await vi.advanceTimersByTimeAsync(0) + expect((await first).models).toHaveLength(count) + let delivered = false + const observer = h.read().then((answer) => { + delivered = true + return answer + }) + await vi.advanceTimersByTimeAsync(0) + for (let index = 0; index < count - 1; index++) { + h.answer(index) + await vi.advanceTimersByTimeAsync(0) + if (index < count - 2) { + expect(delivered).toBe(false) + } + } + const final = await observer + expect(final.models?.every((model) => model.effortsKnown !== false)).toBe(true) + expect(h.connect).toHaveBeenCalledOnce() + // One initial inventory and one terminal observation, independent of the target count. + expect(h.writes.mock.calls.length).toBeLessThanOrEqual(2) + expect( + h.writes.mock.calls.reduce((rows, call) => rows + call[2].models.length, 0) + ).toBeLessThanOrEqual(count * 2) + } +) + +it.each(['snapshot', 'deadline', 'selected'])( + 'exposes accumulated menus at a %s boundary while the next switch is unanswered', + async (boundary) => { + vi.useFakeTimers() + onTestFinished(() => { + vi.useRealTimers() + }) + const h = accumulatedCatalogHarness(5) + const first = h.read() + await first + const observer = h.read() + h.answer(0) + await vi.advanceTimersByTimeAsync(6_000) + // m1 has answered; m2 is still pending, beyond the old optional sweep cutoff. + if (boundary === 'snapshot') { + expect((await h.read(false)).models?.find((row) => row.id === 'm1')?.efforts).toEqual( + LEARNED_EFFORTS + ) + } else if (boundary === 'selected') { + expect((await h.read(true, 'm1')).models?.find((row) => row.id === 'm1')?.efforts).toEqual( + LEARNED_EFFORTS + ) + } else { + await vi.advanceTimersByTimeAsync(24_000) + } + expect((await observer).models?.find((row) => row.id === 'm1')?.efforts).toEqual( + LEARNED_EFFORTS + ) + let followed = false + const follow = h.read().then((answer) => { + followed = true + return answer + }) + await vi.advanceTimersByTimeAsync(1_000) + expect(followed).toBe(false) + // New selected demand joins the same owner and is promoted after its outstanding switch. + const selected = h.read(true, 'm4') + h.answer(1) + await vi.advanceTimersByTimeAsync(6_000) + expect(h.requests[2]?.model).toBe('m4') + h.answer(2) + await vi.advanceTimersByTimeAsync(0) + expect((await selected).models?.find((row) => row.id === 'm4')?.efforts).toEqual( + LEARNED_EFFORTS + ) + await follow + h.answer(3) + await vi.advanceTimersByTimeAsync(0) + expect(h.connect).toHaveBeenCalledOnce() + } +) + +it('keeps a slow OpenCode 1.x version response and selects its command listing', async () => { + vi.useFakeTimers() + onTestFinished(() => { + vi.useRealTimers() + }) + const version = Promise.withResolvers<{ version: string }>() + const requested = Promise.withResolvers() + const base = acpLaunchSpecFor('opencode')! + const session = vi.fn() + const command = vi.fn( + async () => 'opencode/slow-valid-model\n' + ) + const probe = createAcpModelCatalogProbe( + { ...base, account: { ...base.account, environment: (_home, env) => env } }, + { + resolveEnvironment: async () => ({}), + resolveCommand: () => process.execPath, + probeVersion: async () => { + requested.resolve() + return version.promise + }, + dataDirectory: '/unused', + homePath: '/account', + runListing: command, + connectSession: session + } + ) + const result = probe({ variable: 'HOME', path: '/account' }, { workspacePath: '/workspace' }) + await requested.promise + await vi.advanceTimersByTimeAsync(45_000) + version.resolve({ version: '1.18.31' }) + expect((await result).models.map((row) => row.id)).toEqual(['opencode/slow-valid-model']) + expect(session).not.toHaveBeenCalled() + expect(command.mock.calls[0]?.[1]).toMatchObject({ timeoutMs: null }) +}) diff --git a/src/main/acp/discovery/acp-listing-session-models.ts b/src/main/acp/discovery/acp-listing-session-models.ts index 3fd072a2000..d3e2b519830 100644 --- a/src/main/acp/discovery/acp-listing-session-models.ts +++ b/src/main/acp/discovery/acp-listing-session-models.ts @@ -13,7 +13,8 @@ import type { InitializeResponse, SetSessionConfigOptionResponse } from '../generated/acp-protocol.generated' -import { withTimeout } from '../../../shared/promise-timeout-fallback' +import type { AgentModelCatalogReady } from '../../native-chat/agent-model-catalog/agent-model-catalog-probe-runner' +import type { AgentModelCatalogProgress } from '../../native-chat/agent-model-catalog/agent-model-catalog-discovery' import { AcpAgentError } from '../transport/acp-errors' /** Session-free capability boundary: no session methods are exposed. */ @@ -61,7 +62,15 @@ export async function readSessionModels( operation?: StructuredAgentResolutionOperation, selectedModels?: readonly string[], launchedModel?: string, - ready?: (cleanup: () => Promise) => void + ready?: AgentModelCatalogReady<{ models: AgentSessionModelOption[]; attemptedModels: string[] }>, + publish?: (result: { models: AgentSessionModelOption[]; attemptedModels: string[] }) => void, + deferred?: { + defer: ( + read: () => { models: AgentSessionModelOption[]; attemptedModels: string[] }, + known: (model: string) => boolean + ) => void + demanded: NonNullable + } ): Promise<{ models: AgentSessionModelOption[]; attemptedModels: string[] }> { const initialized = await connection.initialize() assertStructuredAgentResolutionCurrent(operation) @@ -80,12 +89,16 @@ export async function readSessionModels( }) } } - // Startup and menu discovery have separate budgets; cleanup also runs on cancellation. - ready?.(cleanup) const options = new AcpStructuredOptions(spec.dialect) options.adoptSession(started.response, 'new') - let models = options.read().models + let models = [...options.read().models] const attemptedModels: string[] = [] + const partial = () => { + stopped = true + return { models: [...models], attemptedModels: [...attemptedModels] } + } + ready?.(cleanup, models.length > 0 ? partial : undefined) + const progress = () => publish?.({ models: [...models], attemptedModels: [...attemptedModels] }) try { if (launchedModel !== undefined) { attemptedModels.push(launchedModel) @@ -96,49 +109,77 @@ export async function readSessionModels( row.id === running ? { ...row, efforts: [], effortsKnown: false as const } : row ) } + progress() } else if (spec.dialect.effortChangesModel) { - const byId = new Map(models.map((model) => [model.id, model])) - const targets = selectedModels ? selectedModels.flatMap((id) => byId.get(id) ?? []) : models - const sweep = async () => { - for (const model of targets) { - if (stopped) { - return - } - assertStructuredAgentResolutionCurrent(operation) - if (model.effortsKnown !== false) { - continue - } - attemptedModels.push(model.id) - try { - for (let attempt = 0; attempt < 2 && !stopped; attempt++) { - const write = options.write('model', model.id) - if (write?.method !== 'config') { - break - } - const reply = await connection.setConfigOption(write.configId, write.value) - if (stopped) { - return - } - assertStructuredAgentResolutionCurrent(operation) - options.adoptConfigOptions(reply.configOptions) - const observed = options.read() - if (observed.current.model !== model.id) { - continue - } - const known = observed.models.find((row) => row.id === model.id) - if (known) { - models = models.map((row) => (row.id === model.id ? known : row)) - } - break - } - } catch { - assertStructuredAgentResolutionCurrent(operation) + progress() + const indices = new Map(models.map((model, index) => [model.id, index])) + const remaining = new Set(selectedModels ?? models.map((model) => model.id)) + const visited = new Set() + const known = (id: string) => { + const index = indices.get(id) + return index !== undefined && models[index].effortsKnown !== false + } + const snapshot = () => ({ models: [...models], attemptedModels: [...attemptedModels] }) + while (!stopped) { + const demanded = deferred?.demanded() ?? new Set(selectedModels) + for (const id of demanded) { + if (!visited.has(id) && indices.has(id)) { + remaining.add(id) } } + const id = [...demanded].find((id) => remaining.has(id)) ?? remaining.values().next().value + if (id === undefined) { + break + } + remaining.delete(id) + visited.add(id) + const index = indices.get(id) + if (index === undefined || known(id)) { + continue + } + assertStructuredAgentResolutionCurrent(operation) + let conclusive = false + try { + for (let attempt = 0; attempt < 2 && !stopped; attempt++) { + const write = options.write('model', id) + if (write?.method !== 'config') { + conclusive = true + break + } + const reply = await connection.setConfigOption(write.configId, write.value) + if (stopped) { + break + } + assertStructuredAgentResolutionCurrent(operation) + options.adoptConfigOptions(reply.configOptions) + const observed = options.read() + if (observed.current.model !== id) { + continue + } + const learned = observed.models.find((row) => row.id === id) + if (learned) { + models[index] = learned + } + conclusive = true + break + } + } catch { + assertStructuredAgentResolutionCurrent(operation) + conclusive = true + } + if (conclusive) { + attemptedModels.push(id) + } + if (deferred?.demanded().has(id)) { + progress() + } else if (deferred) { + deferred.defer(snapshot, known) + } else { + progress() + } } - await withTimeout(sweep(), 5_000, undefined) + stopped = true } - assertStructuredAgentResolutionCurrent(operation) return { models, attemptedModels } } finally { if (!ready) { diff --git a/src/main/acp/discovery/acp-model-catalog-probe.ts b/src/main/acp/discovery/acp-model-catalog-probe.ts index 7f8da46c04a..62cf3f8b230 100644 --- a/src/main/acp/discovery/acp-model-catalog-probe.ts +++ b/src/main/acp/discovery/acp-model-catalog-probe.ts @@ -1,9 +1,18 @@ +import { readAgentCliVersion } from '../../agent-launch/preflight/agent-cli-version-probe' import { observeAcpForkSupport } from '../capabilities/acp-fork-support' -import { assertStructuredAgentResolutionCurrent } from '../../native-chat/agent-launch/structured-agent-command-resolution' +import { + assertStructuredAgentResolutionCurrent, + type StructuredAgentResolutionOperation +} from '../../native-chat/agent-launch/structured-agent-command-resolution' import { homedir } from 'node:os' import { join } from 'node:path' import type { AgentCliVersionReader } from '../../agent-launch/preflight/agent-cli-version-probe' -import type { AgentModelCatalogProbe } from '../../native-chat/agent-model-catalog/agent-model-catalog-store' +import type { + AgentModelCatalogProgress, + AgentModelCatalogProbe, + AgentModelCatalogLookup +} from '../../native-chat/agent-model-catalog/agent-model-catalog-discovery' +import { agentModelCatalogInputStamp } from '../../native-chat/agent-model-catalog/agent-model-catalog-input-stamp' import { runAgentModelCatalogListing, runAgentModelCatalogSession @@ -34,6 +43,7 @@ import { AgentModelCatalogUnavailableError } from '../../native-chat/agent-model import { usesModelSpecificAcpEfforts } from '../../../shared/agent-session/options/agent-model-catalog-ownership' export type AcpModelCatalogProbeDeps = AcpLaunchInvocationDeps & { + resolveSavedEnvironment?: () => Promise> probeVersion?: AgentCliVersionReader connect?: (launch: ProviderProcessLaunch) => AcpModelCatalogConnection connectSession?: (launch: ProviderProcessLaunch) => AcpModelCatalogSessionConnection @@ -45,14 +55,17 @@ export function createAcpModelCatalogProbe( spec: AcpLaunchSpec, deps: AcpModelCatalogProbeDeps ): AgentModelCatalogProbe { - const probe: AgentModelCatalogProbe = async (accountHome, options) => { - const operation = options?.operation ?? { signal: options?.signal } + const resolve = async ( + accountHome: Parameters[0], + workspace: string, + lookup: AgentModelCatalogLookup = 'recover', + operation?: StructuredAgentResolutionOperation + ) => { assertStructuredAgentResolutionCurrent(operation) const discovery = spec.modelDiscovery if (discovery.kind === 'unavailable') { throw new Error(discovery.reason) } - const workspace = options?.workspacePath // Reject folderless session discovery before spawning even a version probe. if (!workspace && acpListingNeedsChatFolder(discovery)) { throw folderlessListing(spec) @@ -64,121 +77,175 @@ export function createAcpModelCatalogProbe( accountHome, { ...deps, + ...(lookup !== 'recover' + ? { + resolveEnvironment: deps.resolveSavedEnvironment ?? deps.resolveEnvironment, + refreshEnvironmentAfterMiss: undefined + } + : {}), operation, resolveWorkingDirectory: () => cwd } ) assertStructuredAgentResolutionCurrent(operation) - const release = await checkAcpLaunchVersion( - spec, - { command, prefixArgs, cwd, env }, - acpListingDependsOnRelease(discovery), - deps.probeVersion - ) - assertStructuredAgentResolutionCurrent(operation) - if (release.status === 'tooOld') { - throw new Error(`${spec.agent} at ${command} does not run structured chats`) - } - const listing = acpModelListingForRelease(discovery, release.version) - if (listing === null) { - throw new Error(`${spec.agent} at ${command} lists its models only through its chats`) - } - const launch = { - command, - args: [...prefixArgs, ...spec.args({ fullAccess: false, pluginDir: null })], - cwd, - env, - envToDelete - } - const run = { label: spec.agent, signal: options?.signal } - // OMP records every model switch as account usage; starting on the picked model records none. - const launchedModel = - spec.launchesWithModel && usesModelSpecificAcpEfforts(spec.agent) - ? options?.selectedModels?.[0] - : undefined - let models: AgentSessionModelOption[] - let attemptedModels: string[] | undefined - if (listing.kind === 'command') { - const stdout = await (deps.runListing ?? runAgentModelCatalogListing)( - { + return { + identity: { command, prefixArgs, cwd, workspace }, + inputStamp: JSON.stringify( + await Promise.all( + [ + ...new Set([cwd, ...(discovery.kind === 'command' && workspace ? [workspace] : [])]) + ].map((directory) => + agentModelCatalogInputStamp({ command, prefixArgs, cwd: directory, env }) + ) + ) + ), + run: async ( + signal?: AbortSignal, + runOperation = operation, + selectedModels?: readonly string[], + publish?: AgentModelCatalogProgress + ) => { + const options = { signal, operation: runOperation, selectedModels } + const operation = runOperation + assertStructuredAgentResolutionCurrent(operation) + const release = await checkAcpLaunchVersion( + spec, + { command, prefixArgs, cwd, env, operation }, + acpListingDependsOnRelease(discovery), + deps.probeVersion ?? ((input) => readAgentCliVersion({ ...input, timeoutMs: null })) + ) + assertStructuredAgentResolutionCurrent(operation) + if (release.status === 'tooOld') { + throw new Error(`${spec.agent} at ${command} does not run structured chats`) + } + const listing = acpModelListingForRelease(discovery, release.version) + if (listing === null) { + throw new Error(`${spec.agent} at ${command} lists its models only through its chats`) + } + const launch = { command, - args: [...prefixArgs, ...listing.args], - cwd: workspace ?? cwd, + args: [...prefixArgs, ...spec.args({ fullAccess: false, pluginDir: null })], + cwd, env, envToDelete - }, - { site: `${spec.agent}-model-catalog-probe`, signal: options?.signal } - ) - models = listing.parse(stdout) - } else if (listing.kind === 'session') { - if (!workspace) { - throw folderlessListing(spec) - } - // Remove disposable session storage after the process closes. - const folderArgs = listing.sessionFolderArgs - const listInSession = async (model: string | undefined) => { - const folder = folderArgs - ? await acpRunFolder(join(deps.dataDirectory, 'acp-listing-sessions', spec.agent)) - : null - try { - assertStructuredAgentResolutionCurrent(operation) - const args = [ - ...prefixArgs, - ...spec.args({ fullAccess: false, pluginDir: null, model }), - ...(folderArgs && folder ? folderArgs(folder.path) : []) - ] - return await runAgentModelCatalogSession( - () => (deps.connectSession ?? createAcpAgentConnection)({ ...launch, args }), - (connection, ready) => - readSessionModels( - spec, - listing, - connection, - workspace, - operation, - options?.selectedModels, - model, - spec.dialect.effortChangesModel ? ready : undefined - ), + } + const run = { label: spec.agent, signal: options?.signal, timeoutMs: null } + // OMP records every model switch as account usage; starting on the picked model records none. + const launchedModel = + spec.launchesWithModel && usesModelSpecificAcpEfforts(spec.agent) + ? options?.selectedModels?.[0] + : undefined + let models: AgentSessionModelOption[] + let attemptedModels: string[] | undefined + if (listing.kind === 'command') { + const stdout = await (deps.runListing ?? runAgentModelCatalogListing)( + { + command, + args: [...prefixArgs, ...listing.args], + cwd: workspace ?? cwd, + env, + envToDelete + }, + { site: `${spec.agent}-model-catalog-probe`, signal: options?.signal, timeoutMs: null } + ) + models = listing.parse(stdout) + } else if (listing.kind === 'session') { + if (!workspace) { + throw folderlessListing(spec) + } + // Remove disposable session storage after the process closes. + const folderArgs = listing.sessionFolderArgs + const listInSession = async (model: string | undefined) => { + const folder = folderArgs + ? await acpRunFolder(join(deps.dataDirectory, 'acp-listing-sessions', spec.agent)) + : null + try { + assertStructuredAgentResolutionCurrent(operation) + const args = [ + ...prefixArgs, + ...spec.args({ fullAccess: false, pluginDir: null, model }), + ...(folderArgs && folder ? folderArgs(folder.path) : []) + ] + return await runAgentModelCatalogSession< + AcpModelCatalogSessionConnection, + Awaited> + >( + () => (deps.connectSession ?? createAcpAgentConnection)({ ...launch, args }), + (connection, ready) => + readSessionModels( + spec, + listing, + connection, + workspace, + operation, + options?.selectedModels, + model, + spec.dialect.effortChangesModel ? ready : undefined, + (result) => + publish?.( + { + ...result, + models: [...result.models], + attemptedModels: [...result.attemptedModels], + origin: 'probe', + complete: false + }, + true + ), + publish?.defer && publish.demanded + ? { + defer: (read, known) => + publish.defer?.( + () => ({ ...read(), origin: 'probe', complete: false }), + known + ), + demanded: publish.demanded + } + : undefined + ), + run + ) + } finally { + await folder?.remove() + } + } + let result: Awaited> + try { + result = await listInSession(launchedModel) + } catch (error) { + // OMP exits before ACP on a model it cannot start (a stale `:level` id); list without it. + if (launchedModel === undefined || !(error instanceof AcpAgentExitedError)) { + throw error + } + assertStructuredAgentResolutionCurrent(operation) + result = { ...(await listInSession(undefined)), attemptedModels: [launchedModel] } + } + models = result.models + attemptedModels = result.attemptedModels + } else { + models = await runAgentModelCatalogSession( + () => (deps.connect ?? createAcpAgentConnection)(launch), + async (connection) => { + const initialized = await connection.initialize() + assertStructuredAgentResolutionCurrent(operation) + observeAcpForkSupport(spec.agent, initialized) + return listing.read(initialized, connection) + }, run ) - } finally { - await folder?.remove() } - } - let result: Awaited> - try { - result = await listInSession(launchedModel) - } catch (error) { - // OMP exits before ACP on a model it cannot start (a stale `:level` id); list without it. - if (launchedModel === undefined || !(error instanceof AcpAgentExitedError)) { - throw error + if (models.length === 0) { + throw new Error(`${spec.agent} listed no models`) } - assertStructuredAgentResolutionCurrent(operation) - result = { ...(await listInSession(undefined)), attemptedModels: [launchedModel] } + return { models, origin: 'probe' as const, ...(attemptedModels ? { attemptedModels } : {}) } } - models = result.models - attemptedModels = result.attemptedModels - } else { - models = await runAgentModelCatalogSession( - () => (deps.connect ?? createAcpAgentConnection)(launch), - async (connection) => { - const initialized = await connection.initialize() - assertStructuredAgentResolutionCurrent(operation) - observeAcpForkSupport(spec.agent, initialized) - return listing.read(initialized, connection) - }, - run - ) } - if (models.length === 0) { - throw new Error(`${spec.agent} listed no models`) - } - return { models, origin: 'probe', ...(attemptedModels ? { attemptedModels } : {}) } } - return async (home, options) => { + const probe: AgentModelCatalogProbe = async (home, options) => { try { - return await probe(home, options) + return await ( + await resolve(home, options?.workspacePath ?? '', 'recover', options?.operation) + ).run(options?.signal, options?.operation, options?.selectedModels, options?.publish) } catch (error) { if (error instanceof AcpAgentError && error.code === -32000) { throw new AgentModelCatalogUnavailableError({ reason: 'notSignedIn' }) @@ -186,6 +253,24 @@ export function createAcpModelCatalogProbe( throw error } } + return Object.assign(probe, { + resolve: async (...args: Parameters) => { + const resolved = await resolve(...args) + return { + ...resolved, + run: async (...runArgs: Parameters) => { + try { + return await resolved.run(...runArgs) + } catch (error) { + if (error instanceof AcpAgentError && error.code === -32000) { + throw new AgentModelCatalogUnavailableError({ reason: 'notSignedIn' }) + } + throw error + } + } + } + } + }) } const folderlessListing = (spec: AcpLaunchSpec) => diff --git a/src/main/acp/discovery/acp-model-catalog-selected-demand.test.ts b/src/main/acp/discovery/acp-model-catalog-selected-demand.test.ts new file mode 100644 index 00000000000..a768e570567 --- /dev/null +++ b/src/main/acp/discovery/acp-model-catalog-selected-demand.test.ts @@ -0,0 +1,94 @@ +import type { ProviderProcessLaunch } from '../../provider-process/provider-process-launch' +import { expect, it, vi, onTestFinished } from 'vitest' +import { acpLaunchSpecFor } from '../launch/acp-launch-registry' +import { NewSessionResponseSchema } from '../generated/acp-protocol.generated' +import { createAcpModelCatalogProbe } from './acp-model-catalog-probe' +import type { AcpModelCatalogSessionConnection } from './acp-listing-session-models' +import { AgentModelCatalogStore } from '../../native-chat/agent-model-catalog/agent-model-catalog-store' +import { createAgentModelCatalogService } from '../../native-chat/agent-model-catalog/agent-model-catalog-service' + +it('starts the first OMP listing on demand collected before admission, without a second session', async () => { + const base = acpLaunchSpecFor('omp')! + const connect = vi.fn((launch: ProviderProcessLaunch): AcpModelCatalogSessionConnection => { + const modelAt = launch.args.indexOf('--model') + const current = modelAt !== -1 ? launch.args[modelAt + 1] : 'provider/default' + return { + initialize: async () => ({ + protocolVersion: 1, + agentCapabilities: { sessionCapabilities: { delete: {} } } + }), + start: async () => ({ + kind: 'new', + sessionId: 'listing', + response: NewSessionResponseSchema.parse({ + sessionId: 'listing', + configOptions: [ + { + id: 'model', + name: 'Model', + category: 'model', + type: 'select', + currentValue: current, + options: [ + { value: 'provider/default', name: 'Default' }, + { value: 'provider/selected', name: 'Selected' } + ] + }, + { + id: 'thinking', + name: 'Thinking', + category: 'thought_level', + type: 'select', + currentValue: 'high', + options: [{ value: 'high', name: 'High' }] + } + ] + }) + }), + setConfigOption: async () => { + throw new Error('OMP selects on launch') + }, + deleteSession: async () => ({}), + close: async () => {} + } + }) + const probe = createAcpModelCatalogProbe( + { + ...base, + account: { ...base.account, environment: (_home, env) => env }, + modelDiscovery: { kind: 'session', listingNamesConfiguredModel: false } + }, + { + resolveEnvironment: async () => ({}), + resolveCommand: () => process.execPath, + probeVersion: async () => ({ version: '18.0.0' }), + homePath: '/account', + dataDirectory: '/unused', + connectSession: connect + } + ) + const store = new AgentModelCatalogStore() + const service = createAgentModelCatalogService({ + store, + getRecord: () => undefined, + getConversation: () => undefined, + drivesRecord: () => true, + resolveAccountHome: async () => ({ variable: 'PI_CODING_AGENT_DIR', path: '/account' }), + probes: { omp: probe } + }) + onTestFinished(() => service.stop()) + const result = await service.read({ + agent: 'omp', + workspacePath: '/workspace', + selectedModel: 'provider/selected', + waitForListing: true + }) + if (result.origin === 'unknown') { + throw new Error('Expected the selected model inventory') + } + expect(result.models?.find((row) => row.id === 'provider/selected')).toMatchObject({ + efforts: [{ value: 'high', label: 'High' }] + }) + expect(connect).toHaveBeenCalledOnce() + expect(connect.mock.calls[0]?.[0].args).toEqual(['acp', '--model', 'provider/selected']) +}) diff --git a/src/main/acp/launch/acp-launch-version.ts b/src/main/acp/launch/acp-launch-version.ts index 92fb537d854..41d8b356068 100644 --- a/src/main/acp/launch/acp-launch-version.ts +++ b/src/main/acp/launch/acp-launch-version.ts @@ -1,3 +1,4 @@ +import type { StructuredAgentResolutionOperation } from '../../native-chat/agent-launch/structured-agent-command-resolution' import { checkAgentCliVersion, type AgentCliVersionCheck, @@ -9,7 +10,9 @@ import { noteAcpForkVersion } from '../capabilities/acp-fork-support' export async function checkAcpLaunchVersion( spec: AcpLaunchSpec, - launch: Pick, + launch: Pick & { + operation?: StructuredAgentResolutionOperation + }, wantVersion: boolean, read?: AgentCliVersionReader ): Promise { @@ -18,7 +21,13 @@ export async function checkAcpLaunchVersion( return { status: 'unknown', version: null } } const result = await checkAgentCliVersion( - { program: launch.command, prefixArgs: launch.prefixArgs, cwd: launch.cwd, env: launch.env }, + { + program: launch.command, + prefixArgs: launch.prefixArgs, + cwd: launch.cwd, + env: launch.env, + operation: launch.operation + }, spec.minimumVersions ?? [], read ) diff --git a/src/main/agent-launch/preflight/agent-cli-version-probe.test.ts b/src/main/agent-launch/preflight/agent-cli-version-probe.test.ts index f3508c15ef0..c53b3149acc 100644 --- a/src/main/agent-launch/preflight/agent-cli-version-probe.test.ts +++ b/src/main/agent-launch/preflight/agent-cli-version-probe.test.ts @@ -1,5 +1,5 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import { checkAgentCliVersion } from './agent-cli-version-probe' +import { checkAgentCliVersion, readAgentCliVersion } from './agent-cli-version-probe' const { runProcess } = vi.hoisted(() => ({ runProcess: vi.fn() })) vi.mock('@orca/process-host', () => ({ runProcess })) @@ -38,6 +38,17 @@ describe('checkAgentCliVersion', () => { ) }) + it('propagates owner cancellation instead of certifying a missing CLI', async () => { + const owner = new AbortController() + const closed = Promise.withResolvers() + runProcess.mockReturnValue(closed.promise) + const read = checkAgentCliVersion({ ...INPUT, operation: { signal: owner.signal } }, []) + const observed = expect(read).rejects.toThrow('superseded') + owner.abort(new Error('superseded')) + closed.resolve({ code: null, signal: 'SIGTERM', stdout: '', stderr: '', timedOut: false }) + await observed + }) + it('reads a version printed only on stderr, as Pi 0.73 does', async () => { prints('', { stderr: '0.73.1\n' }) expect(await checkAgentCliVersion(INPUT, ['0.84.0'])).toEqual({ @@ -47,3 +58,15 @@ describe('checkAgentCliVersion', () => { }) }) }) + +it('allows a catalog version prerequisite to complete late without changing normal preflight limits', async () => { + const answer = Promise.withResolvers() + runProcess.mockReturnValueOnce(answer.promise) + const catalog = readAgentCliVersion({ ...INPUT, timeoutMs: null }) + expect(runProcess.mock.lastCall?.[0].timeoutMs).toBeNull() + answer.resolve({ code: 0, stdout: '1.18.31', stderr: '', timedOut: false }) + expect(await catalog).toEqual({ version: '1.18.31' }) + runProcess.mockResolvedValueOnce({ code: null, stdout: '', stderr: '', timedOut: true }) + expect(await readAgentCliVersion(INPUT)).toEqual({ version: null }) + expect(runProcess.mock.lastCall?.[0].timeoutMs).toBe(5_000) +}) diff --git a/src/main/agent-launch/preflight/agent-cli-version-probe.ts b/src/main/agent-launch/preflight/agent-cli-version-probe.ts index d01a9a981ff..84dc4ba53d8 100644 --- a/src/main/agent-launch/preflight/agent-cli-version-probe.ts +++ b/src/main/agent-launch/preflight/agent-cli-version-probe.ts @@ -1,3 +1,7 @@ +import { + assertStructuredAgentResolutionCurrent, + type StructuredAgentResolutionOperation +} from '../../native-chat/agent-launch/structured-agent-command-resolution' import { compareAppVersions, parseCliVersion } from '../../../shared/app-version' import { runProcess } from '@orca/process-host' import type { ProcessSpec } from '@orca/process-host/process-spec' @@ -14,6 +18,8 @@ export type AgentCliVersionCheck = | { status: 'unknown' | 'missing'; version: null } export type AgentCliVersionInput = Pick & { + operation?: StructuredAgentResolutionOperation + timeoutMs?: number | null prefixArgs?: readonly string[] } @@ -60,21 +66,34 @@ export async function readAgentCliVersion( input: AgentCliVersionInput, parseVersion: typeof parseCliVersion = parseCliVersion ): Promise<{ version: string | null; started?: false }> { - const { prefixArgs = [], ...launch } = input + const { prefixArgs = [], operation, timeoutMs = 5_000, ...launch } = input + assertStructuredAgentResolutionCurrent(operation) let result try { result = await runProcess({ ...launch, args: [...prefixArgs, '--version'], terminationBarrier: true, - timeoutMs: 5_000, + signal: operation?.signal, + timeoutMs: + timeoutMs === null + ? null + : Math.max( + 1, + Math.min( + timeoutMs, + operation?.deadline === undefined ? Infinity : operation.deadline - Date.now() + ) + ), maxOutputBytes: 4_096, killOnOutputLimit: true }) } catch (error) { + assertStructuredAgentResolutionCurrent(operation) warnRefused(input.program, `did not start (${String(error)})`) return { version: null, started: false } } + assertStructuredAgentResolutionCurrent(operation) const output = `${result.stdout}\n${result.stderr}` if (result.timedOut || result.outputTruncated || result.code !== 0) { const why = result.timedOut diff --git a/src/main/automations/automation-launch-delivery.test.ts b/src/main/automations/automation-launch-delivery.test.ts index e36787c87d0..ff7c0d97676 100644 --- a/src/main/automations/automation-launch-delivery.test.ts +++ b/src/main/automations/automation-launch-delivery.test.ts @@ -4,6 +4,8 @@ import { mkdtempSync, rmSync } from 'node:fs' import { join } from 'node:path' import { tmpdir } from 'node:os' import { AutomationService } from './automation-service' +import { AutomationRunLaunchError } from './launch/automation-launch' +import type { AutomationRun } from '../../shared/automations/history/automation-run' vi.mock('electron', () => ({ app: { getPath: () => testState.dir } })) @@ -80,4 +82,82 @@ describe('headless dispatch prompt delivery', () => { service.stop() } ) + + it('records launch evidence once the agent terminal exists, and keeps it after retention clears the pane', async () => { + const { store, service, run, row, settle } = await heldLaunch('delivery') + await vi.waitFor(() => expect(row()?.terminalPaneKey).toBe(OPENED.terminalPaneKey)) + await settle("Aider started, but Orca could not send this run's prompt.") + const launchedAt = row()?.launchedAt + expect(launchedAt).toEqual(expect.any(Number)) + expect(row()?.dispatchedAt).toBeNull() + + // Dead-PTY retention forgets the terminal but must not forget that the agent launched. + store.updateAutomationRun({ + runId: run.id, + status: 'dispatch_failed', + error: row()?.error ?? null, + terminalSessionId: null, + terminalPaneKey: null, + terminalPtyId: null + }) + expect(row()).toMatchObject({ terminalPaneKey: null, launchedAt }) + service.stop() + }) +}) + +/** Launches one automation per error, each with a launcher that throws that error. */ +async function failingLaunches(errors: Error[]) { + const store = await createStore() + store.addRepo({ id: 'r1', path: '/repo', displayName: 'repo', badgeColor: '#fff', addedAt: 1 }) + const pending = [...errors] + const service = new AutomationService(store, { + tickMs: 60_000, + allowRemoteHostScheduling: true, + terminalLauncher: { + launch: async () => { + throw pending.shift() ?? new Error('Unexpected launch.') + }, + available: () => true + } + }) + const rows: (AutomationRun | undefined)[] = [] + for (const [index] of errors.entries()) { + const automation = store.createAutomation({ + name: `Nightly ${index}`, + prompt: 'Fix the flaky test', + agentId: 'aider', + projectId: 'r1', + workspaceMode: 'new_per_run', + timezone: 'UTC', + rrule: 'FREQ=DAILY;BYHOUR=9;BYMINUTE=0', + dtstart: Date.UTC(2026, 4, 12) + }) + const run = await service.runNow(automation.id) + await service.runInAutomationLane(automation.id, async () => undefined) + const row = () => store.listAutomationRuns(automation.id).find((entry) => entry.id === run.id) + await vi.waitFor(() => expect(row()?.status).toBe('dispatch_failed')) + rows.push(row()) + } + service.stop() + return rows +} + +describe('launch evidence when the launcher throws', () => { + beforeEach(() => { + testState.dir = mkdtempSync(join(tmpdir(), 'orca-automation-launch-evidence-')) + }) + afterEach(async () => { + await closeTestStores() + rmSync(testState.dir, { recursive: true, force: true }) + }) + + it('records a partial start that opened the agent terminal, but not a launch that opened nothing', async () => { + const [partial, preOpen] = await failingLaunches([ + new AutomationRunLaunchError('Prompt paste failed.', OPENED), + new Error('No window to open the terminal in.') + ]) + expect(partial?.launchedAt).toEqual(expect.any(Number)) + // Safety: a failure before any terminal or session opened leaves no launch evidence. + expect(preOpen?.launchedAt).toBeUndefined() + }) }) diff --git a/src/main/automations/dispatch/automation-launch-runner.ts b/src/main/automations/dispatch/automation-launch-runner.ts index a22519c6e72..a0f204eb3ca 100644 --- a/src/main/automations/dispatch/automation-launch-runner.ts +++ b/src/main/automations/dispatch/automation-launch-runner.ts @@ -75,6 +75,11 @@ export async function runAutomationLaunch(ctx: AutomationLaunchContext): Promise status: refusal ? 'skipped_unavailable' : 'dispatch_failed', workspaceId: automation.workspaceId, ...(error instanceof AutomationRunLaunchError ? error.target : {}), + // A partial start produced the agent's terminal or session before failing, so it + // counts as launched at the dispatch-start time. + ...(error instanceof AutomationRunLaunchError && openedAgent(error.target) + ? { launchedAt: dispatchedAt } + : {}), ...(automation.surface === 'chat' ? { dispatchedAt, precheckResult } : {}), ...(refusal ?? { error: describeDispatchError(error) }) }).run @@ -82,7 +87,11 @@ export async function runAutomationLaunch(ctx: AutomationLaunchContext): Promise const launchRunTarget = launchRunTargetOf(launch) if (launch.delivery) { // A startup dialog can hold delivery, so link the terminal while the run is still dispatching. - const linked = ctx.transition({ status: 'dispatching', ...launchRunTarget }) + const linked = ctx.transition({ + status: 'dispatching', + ...launchRunTarget, + launchedAt: dispatchedAt + }) if (!linked.applied) { return linked.run } @@ -91,12 +100,18 @@ export async function runAutomationLaunch(ctx: AutomationLaunchContext): Promise }) const { error } = await launch.delivery if (error !== null) { - return ctx.transition({ status: 'dispatch_failed', ...launchRunTarget, error }).run + return ctx.transition({ + status: 'dispatch_failed', + ...launchRunTarget, + launchedAt: dispatchedAt, + error + }).run } } const updated = ctx.transition({ status: 'dispatched', dispatchedAt, + launchedAt: dispatchedAt, ...launchRunTarget, // Watched completions omit the precheck result. ...(precheckResult ? { precheckResult } : {}), @@ -143,6 +158,13 @@ export async function runAutomationLaunch(ctx: AutomationLaunchContext): Promise return updated.run } +/** The launch got as far as producing the agent's terminal or chat session. */ +function openedAgent(target: AutomationLaunch | undefined): boolean { + return Boolean( + target && (target.terminalPaneKey || target.terminalSessionId || target.chatSessionId) + ) +} + function launchRunTargetOf(launch: AutomationLaunch) { return { sessionKind: launch.sessionKind ?? 'terminal', diff --git a/src/main/browser/agent-browser/agent-browser-bridge.ts b/src/main/browser/agent-browser/agent-browser-bridge.ts index b746a98d472..521d9132ecd 100644 --- a/src/main/browser/agent-browser/agent-browser-bridge.ts +++ b/src/main/browser/agent-browser/agent-browser-bridge.ts @@ -14,8 +14,8 @@ import type { } from '../../../shared/browser/runtime/runtime-browser-contracts' import { BrowserError } from '../browser-error' import { - parseShellArgs, - stripAgentBrowserTargetArgs + assertAgentBrowserPassthroughStaysOnPage, + parseShellArgs } from './protocol/agent-browser-passthrough-arguments' import { AgentBrowserBridgeInteractionCommands } from './commands/agent-browser-bridge-interaction-commands' import { sendGuestCdpCommand } from '../cdp/debugger/guest-cdp-command' @@ -272,8 +272,8 @@ export class AgentBrowserBridge extends AgentBrowserBridgeInteractionCommands { } async exec(command: string, worktreeId?: string, browserPageId?: string): Promise { - // Passthrough arguments must not override Orca's selected page or CDP proxy. - const args = stripAgentBrowserTargetArgs(parseShellArgs(command.trim())) + const args = parseShellArgs(command.trim()) + assertAgentBrowserPassthroughStaysOnPage(args) return this.enqueueTargetedCommand( worktreeId, browserPageId, diff --git a/src/main/browser/agent-browser/helper-process/agent-browser-process-environment.test.ts b/src/main/browser/agent-browser/helper-process/agent-browser-process-environment.test.ts new file mode 100644 index 00000000000..04f3289c9a9 --- /dev/null +++ b/src/main/browser/agent-browser/helper-process/agent-browser-process-environment.test.ts @@ -0,0 +1,48 @@ +import { mkdtempSync, readFileSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { + createAgentBrowserProcessEnvironment, + pointAtEmptyAgentBrowserConfig +} from './agent-browser-process-environment' + +describe('desktop agent-browser helper environment', () => { + let userDataPath: string | undefined + + afterEach(() => { + if (userDataPath) { + rmSync(userDataPath, { recursive: true, force: true }) + } + }) + + it('drops the user agent-browser settings and points config at an empty Orca file', async () => { + userDataPath = mkdtempSync(join(tmpdir(), 'orca-ab-env-')) + const { env } = createAgentBrowserProcessEnvironment({ + inheritedEnv: { + PATH: '/usr/bin', + AGENT_BROWSER_PIN_TAB: '1', + agent_browser_auto_connect: '1', + AGENT_BROWSER_CDP: 'ws://127.0.0.1:9222/devtools/browser/user', + AGENT_BROWSER_CONFIG: '/home/user/agent-browser.json', + AGENT_BROWSER_SOCKET_DIR: '/tmp/ab-sockets', + AGENT_BROWSER_NAMESPACE: 'work', + AGENT_BROWSER_IDLE_TIMEOUT_MS: '5000' + }, + platform: 'darwin', + userDataPath + }) + await pointAtEmptyAgentBrowserConfig(env, userDataPath) + + expect(env).toEqual({ + PATH: '/usr/bin', + AGENT_BROWSER_SOCKET_DIR: '/tmp/ab-sockets', + AGENT_BROWSER_IDLE_TIMEOUT_MS: '5000', + AGENT_BROWSER_NO_AUTO_DIALOG: '1', + AGENT_BROWSER_CONFIG: join(userDataPath, 'agent-browser-config.json') + }) + expect( + JSON.parse(readFileSync(join(userDataPath, 'agent-browser-config.json'), 'utf8')) + ).toEqual({}) + }) +}) diff --git a/src/main/browser/agent-browser/helper-process/agent-browser-process-environment.ts b/src/main/browser/agent-browser/helper-process/agent-browser-process-environment.ts index f24c40e0654..e77a0d25d2c 100644 --- a/src/main/browser/agent-browser/helper-process/agent-browser-process-environment.ts +++ b/src/main/browser/agent-browser/helper-process/agent-browser-process-environment.ts @@ -1,6 +1,8 @@ import { createHash } from 'node:crypto' import { chmodSync, mkdirSync } from 'node:fs' +import { writeFile } from 'node:fs/promises' import { join } from 'node:path' +import { isolateFromUserAgentBrowserSettings } from './agent-browser-user-settings-isolation' const AGENT_BROWSER_SOCKET_DIRECTORY_PREFIX = 'orca-ab-' @@ -18,12 +20,15 @@ export function createAgentBrowserProcessEnvironment(options: { platform: NodeJS.Platform userDataPath: string }): AgentBrowserProcessEnvironment { - const env = { ...options.inheritedEnv } - // Commands get Orca's tokened proxy URL per spawn; an inherited target must not reach `close`. - delete env.AGENT_BROWSER_CDP - if (!env.AGENT_BROWSER_IDLE_TIMEOUT_MS?.trim()) { - env.AGENT_BROWSER_IDLE_TIMEOUT_MS = String(AGENT_BROWSER_IDLE_TIMEOUT_MS) - } + // Commands get Orca's tokened proxy URL per spawn; a user's autoConnect, pinTab or extensions + // would refuse it or open tabs behind Orca's tab model. + // A namespace moves sockets under namespaces//, where session reset and exit waits don't look. + const env = isolateFromUserAgentBrowserSettings(options.inheritedEnv, [ + 'AGENT_BROWSER_SOCKET_DIR' + ]) + env.AGENT_BROWSER_IDLE_TIMEOUT_MS = + options.inheritedEnv.AGENT_BROWSER_IDLE_TIMEOUT_MS?.trim() || + String(AGENT_BROWSER_IDLE_TIMEOUT_MS) // Its auto-accept dropped alert text; Orca keeps every dialog open until someone answers it. env.AGENT_BROWSER_NO_AUTO_DIALOG = '1' if (options.platform === 'win32' || env.AGENT_BROWSER_SOCKET_DIR?.trim()) { @@ -40,3 +45,14 @@ export function createAgentBrowserProcessEnvironment(options: { env.AGENT_BROWSER_SOCKET_DIR = socketDirectory return { env, ownsSocketDirectory: true } } + +/** Until this lands, agent-browser reads the user's ~/.agent-browser/config.json and ./agent-browser.json. */ +export async function pointAtEmptyAgentBrowserConfig( + env: NodeJS.ProcessEnv, + userDataPath: string +): Promise { + const path = join(userDataPath, 'agent-browser-config.json') + await writeFile(path, '{}\n', { mode: 0o600 }) + // An explicit config skips ~/.agent-browser/config.json and ./agent-browser.json. + env.AGENT_BROWSER_CONFIG = path +} diff --git a/src/main/browser/agent-browser/helper-process/agent-browser-user-settings-isolation.ts b/src/main/browser/agent-browser/helper-process/agent-browser-user-settings-isolation.ts new file mode 100644 index 00000000000..0ea28532e8e --- /dev/null +++ b/src/main/browser/agent-browser/helper-process/agent-browser-user-settings-isolation.ts @@ -0,0 +1,16 @@ +/** + * Orca owns its helpers' launch and target, so a user's AGENT_BROWSER_* settings (headed, + * autoConnect, extensions, pinTab...) must not reach them. Windows env keys ignore case. + * Callers also point AGENT_BROWSER_CONFIG at an Orca file so the user's config files are skipped. + */ +export function isolateFromUserAgentBrowserSettings( + inheritedEnv: NodeJS.ProcessEnv, + keepKeys: readonly string[] +): NodeJS.ProcessEnv { + return Object.fromEntries( + Object.entries(inheritedEnv).filter(([key]) => { + const upper = key.toUpperCase() + return !upper.startsWith('AGENT_BROWSER_') || keepKeys.includes(upper) + }) + ) +} diff --git a/src/main/browser/agent-browser/observation/agent-browser-action-effects.ts b/src/main/browser/agent-browser/observation/agent-browser-action-effects.ts index 0723b6d1445..70feeccb045 100644 --- a/src/main/browser/agent-browser/observation/agent-browser-action-effects.ts +++ b/src/main/browser/agent-browser/observation/agent-browser-action-effects.ts @@ -190,6 +190,10 @@ async function settle( : [] ) ) + const print = events.find((event) => event.kind === 'print') + if (print) { + effects.blockedPrint = { origin: clip(print.origin) } + } effects.consoleErrors = firstItems(watch.consoleErrors.map((error) => clip(error))) return effects } diff --git a/src/main/browser/agent-browser/protocol/agent-browser-passthrough-arguments.test.ts b/src/main/browser/agent-browser/protocol/agent-browser-passthrough-arguments.test.ts new file mode 100644 index 00000000000..dc0a7360d48 --- /dev/null +++ b/src/main/browser/agent-browser/protocol/agent-browser-passthrough-arguments.test.ts @@ -0,0 +1,43 @@ +import { describe, expect, it } from 'vitest' +import { assertAgentBrowserPassthroughStaysOnPage } from './agent-browser-passthrough-arguments' + +describe('assertAgentBrowserPassthroughStaysOnPage', () => { + it.each([ + [['connect', '9222']], + [['tab', 'new']], + [['window', 'new']], + [['close']], + [['set', 'viewport', '800', '600']], + [['set', 'device', 'iPhone 15']], + [['click', '@e1', '--new-tab']], + [['get', 'text', '@e3', '--cdp', 'ws://127.0.0.1:9222']], + [['get', 'url', '--session=other']], + [['snapshot', '--auto-connect']], + [['open', 'https://example.com', '-p', 'browserbase']], + [['fill', '@e1', '--headed']], + [['batch', 'get title', 'connect 9222']], + [['batch', '--bail', 'tab new']], + [['batch', 'c\\onnect 9222']], + [['batch', 'close\\']], + [['inspect']], + [['stream', 'enable', '--port', '9999']], + [['auth', 'login', 'github', '--url', 'https://example.com/login']], + [['batch', 'batch "tab new"']], + [['batch', 'get url --cdp 9222']] + ])('refuses %j', (args) => { + expect(() => assertAgentBrowserPassthroughStaysOnPage(args)).toThrow( + expect.objectContaining({ code: 'invalid_argument' }) + ) + }) + + it.each([ + [['get', 'text', '@e3']], + [['read']], + [['is', 'visible', '@e3']], + [['set', 'media', 'dark']], + [['wait', '--text', 'Done']], + [['batch', '--bail', 'get title', 'snapshot -i']] + ])('runs %j', (args) => { + expect(() => assertAgentBrowserPassthroughStaysOnPage(args)).not.toThrow() + }) +}) diff --git a/src/main/browser/agent-browser/protocol/agent-browser-passthrough-arguments.ts b/src/main/browser/agent-browser/protocol/agent-browser-passthrough-arguments.ts index 1ba5a18c9cc..54cf322e2df 100644 --- a/src/main/browser/agent-browser/protocol/agent-browser-passthrough-arguments.ts +++ b/src/main/browser/agent-browser/protocol/agent-browser-passthrough-arguments.ts @@ -1,3 +1,5 @@ +import { BrowserError } from '../../browser-error' + export function parseShellArgs(input: string): string[] { const args: string[] = [] let current = '' @@ -25,18 +27,179 @@ export function parseShellArgs(input: string): string[] { return args } -export function stripAgentBrowserTargetArgs(args: string[]): string[] { - const stripped: string[] = [] - for (let index = 0; index < args.length; index++) { - const arg = args[index] - if (arg === '--cdp' || arg === '--session') { - index++ - continue +/** Splits a command line the way agent-browser splits each `batch` command (backslash escapes, quotes). */ +export function splitAgentBrowserCommandLine(input: string): string[] { + const args: string[] = [] + let value = '' + let quote: '"' | "'" | null = null + let escaped = false + for (const char of input.trim()) { + if (escaped) { + value += char + escaped = false + } else if (char === '\\' && quote !== "'") { + escaped = true + } else if (quote) { + if (char === quote) { + quote = null + } else { + value += char + } + } else if (char === '"' || char === "'") { + quote = char + } else if (/\s/.test(char)) { + if (value) { + args.push(value) + value = '' + } + } else { + value += char + } + } + // Like agent-browser, drop a trailing lone backslash: `close\` runs `close`. + if (quote) { + throw new BrowserError('invalid_argument', 'Browser command has an unclosed quote.') + } + if (value) { + args.push(value) + } + return args +} + +// agent-browser's global options (0.39 through 0.43). It reads them anywhere in argv, even after +// a command, and they choose the browser, session and launch, which Orca owns. +const GLOBAL_OPTIONS = new Set([ + '--action-policy', + '--allow-file-access', + '--allowed-domains', + '--annotate', + '--args', + '--auto-connect', + '--ca-cert', + '--capture', + '--cdp', + '--cdp-headers', + '--color-scheme', + '--config', + '--confirm-actions', + '--confirm-interactive', + '--content-boundaries', + '--debug', + '--device', + '--download-path', + '--enable', + '--engine', + '--executable-path', + '--extension', + '--fix', + '--headed', + '--headers', + '--hide-scrollbars', + '--idle-timeout', + '--ignore-https-errors', + '--init-script', + '--input-mode', + '--json', + '--max-output', + '--model', + '--namespace', + '--no-auto-connect', + '--no-auto-dialog', + '--no-ca-cert', + '--no-pin-tab', + '--no-webmcp', + '--offline', + '--pin-tab', + '--profile', + '--provider', + '--proxy', + '--proxy-bypass', + '--quick', + '--quiet', + '--relaunch', + '--restore', + '--restore-check-fn', + '--restore-check-text', + '--restore-check-url', + '--restore-save', + '--screenshot-dir', + '--screenshot-format', + '--screenshot-quality', + '--session', + '--session-name', + '--state', + '--use-system-ca', + '--user-agent', + '--verbose', + '--webgpu', + '-p', + '-q', + '-v' +]) + +const TAB_HINT = 'Use the typed tab commands (tab list, tab create, tab switch, tab close).' +const VIEWPORT_HINT = 'Use the typed viewport or set device command.' +const NOT_THE_PAGE = 'It acts outside the selected Orca page.' + +// Commands that leave Orca's page: connect/tab/window/close retarget or create targets behind +// Orca's tab model; inspect opens the system browser; stream serves an unguarded command relay; +// auth types agent-browser's saved logins into any URL; the rest act on the machine. +const REFUSED_COMMANDS = new Map([ + ['connect', NOT_THE_PAGE], + ['inspect', NOT_THE_PAGE], + ['stream', NOT_THE_PAGE], + ['auth', NOT_THE_PAGE], + ['tab', TAB_HINT], + ['window', TAB_HINT], + ['close', TAB_HINT], + ['quit', TAB_HINT], + ['exit', TAB_HINT], + ['state', NOT_THE_PAGE], + ['session', NOT_THE_PAGE], + ['install', NOT_THE_PAGE], + ['upgrade', NOT_THE_PAGE], + ['doctor', NOT_THE_PAGE], + ['dashboard', NOT_THE_PAGE], + ['profiles', NOT_THE_PAGE], + ['plugin', NOT_THE_PAGE], + ['plugins', NOT_THE_PAGE], + ['mcp', NOT_THE_PAGE], + ['chat', NOT_THE_PAGE] +]) + +function refuse(what: string, hint: string): never { + throw new BrowserError('invalid_argument', `exec does not run ${what}. ${hint}`) +} + +/** Refuses raw agent-browser lines that would move the helper off Orca's selected page. */ +export function assertAgentBrowserPassthroughStaysOnPage(args: readonly string[]): void { + for (const arg of args) { + const option = arg.split('=')[0] + if (GLOBAL_OPTIONS.has(option)) { + refuse(`agent-browser's global option ${option}`, 'Orca sets global options itself.') + } + } + const [command, subcommand] = args + if (!command) { + return + } + const refusal = REFUSED_COMMANDS.get(command) + if (refusal) { + refuse(`\`${command}\``, refusal) + } + // Both resize the real window around the page (Browser.setContentsSize). + if (command === 'set' && (subcommand === 'viewport' || subcommand === 'device')) { + refuse(`\`set ${subcommand}\``, VIEWPORT_HINT) + } + if (command === 'click' && args.includes('--new-tab')) { + refuse('`click --new-tab`', TAB_HINT) + } + if (command === 'batch') { + // agent-browser runs every operand except `--bail` as its own command line. + for (const step of args.slice(1)) { + if (step !== '--bail') { + assertAgentBrowserPassthroughStaysOnPage(splitAgentBrowserCommandLine(step)) + } } - if (arg.startsWith('--cdp=') || arg.startsWith('--session=')) { - continue - } - stripped.push(arg) } - return stripped } diff --git a/src/main/browser/agent-browser/session-lifecycle/agent-browser-bridge-shutdown.ts b/src/main/browser/agent-browser/session-lifecycle/agent-browser-bridge-shutdown.ts index 71cea05b84b..98e91769511 100644 --- a/src/main/browser/agent-browser/session-lifecycle/agent-browser-bridge-shutdown.ts +++ b/src/main/browser/agent-browser/session-lifecycle/agent-browser-bridge-shutdown.ts @@ -5,6 +5,7 @@ import type { AgentBrowserCleanupOptions } from './agent-browser-cleanup-options export abstract class AgentBrowserBridgeShutdown extends AgentBrowserBridgeLifecycle { async sweepOrphanedSessions(): Promise { + await this.agentBrowserConfigReady return sweepOrphanedAgentBrowserSessions({ binaryPath: this.agentBrowserBin, env: this.agentBrowserEnv, diff --git a/src/main/browser/agent-browser/session-routing/agent-browser-bridge-state.ts b/src/main/browser/agent-browser/session-routing/agent-browser-bridge-state.ts index 2f943be6a62..664ce842af8 100644 --- a/src/main/browser/agent-browser/session-routing/agent-browser-bridge-state.ts +++ b/src/main/browser/agent-browser/session-routing/agent-browser-bridge-state.ts @@ -3,7 +3,10 @@ import type { ChildProcess } from 'node:child_process' import { BrowserError } from '../../browser-error' import type { BrowserManager } from '../../guest-lifecycle/manager/browser-manager' import type { BrowserPopupPage } from '../../guest-lifecycle/popups/browser-popup-ownership' -import { createAgentBrowserProcessEnvironment } from '../helper-process/agent-browser-process-environment' +import { + createAgentBrowserProcessEnvironment, + pointAtEmptyAgentBrowserConfig +} from '../helper-process/agent-browser-process-environment' import { resolveAgentBrowserBinary } from '../helper-process/agent-browser-binary' import type { QueuedCommand } from '../command-queue/agent-browser-queued-command' import type { SessionState } from '../session-lifecycle/agent-browser-session-state' @@ -22,6 +25,8 @@ export abstract class AgentBrowserBridgeState { protected readonly processingQueues = new Set() protected readonly agentBrowserBin: string protected readonly agentBrowserEnv: NodeJS.ProcessEnv + // Startup spawns await it; agent commands come long after it lands. + protected readonly agentBrowserConfigReady: Promise protected readonly ownsAgentBrowserSocketDirectory: boolean // Null disables idle-replacement detection when daemon lifetime is unbounded. protected readonly agentBrowserIdleTimeoutMs: number | null @@ -38,12 +43,18 @@ export abstract class AgentBrowserBridgeState { protected readonly options: AgentBrowserBridgeOptions = {} ) { this.agentBrowserBin = resolveAgentBrowserBinary() + const userDataPath = app.getPath('userData') const processEnvironment = createAgentBrowserProcessEnvironment({ inheritedEnv: process.env, platform: process.platform, - userDataPath: app.getPath('userData') + userDataPath }) this.agentBrowserEnv = processEnvironment.env + // A failed write keeps agent-browser's own lookup; a missing file would fail every command. + this.agentBrowserConfigReady = pointAtEmptyAgentBrowserConfig( + this.agentBrowserEnv, + userDataPath + ).catch(() => {}) this.ownsAgentBrowserSocketDirectory = processEnvironment.ownsSocketDirectory const idleTimeoutMs = Number(this.agentBrowserEnv.AGENT_BROWSER_IDLE_TIMEOUT_MS) this.agentBrowserIdleTimeoutMs = idleTimeoutMs > 0 ? idleTimeoutMs : null diff --git a/src/main/browser/extensions/actions/browser-extension-popup-host.ts b/src/main/browser/extensions/actions/browser-extension-popup-host.ts index d2a32d6ebf4..ad23e2282e5 100644 --- a/src/main/browser/extensions/actions/browser-extension-popup-host.ts +++ b/src/main/browser/extensions/actions/browser-extension-popup-host.ts @@ -5,6 +5,7 @@ import { isSameExtensionNavigation } from '../../../../shared/browser/extensions/browser-extension-url' import { allowPageUnload } from '../../guest-lifecycle/navigation/browser-guest-unload-policy' +import { recordBrowserSessionInput } from '../../guest-lifecycle/input/browser-guest-input-gesture' import { BrowserExtensionApiError } from '../api-runtime/browser-extension-namespace-registry' import { BROWSER_EXTENSION_POPUP_MAX, @@ -136,6 +137,7 @@ export function openBrowserExtensionPopup(options: BrowserExtensionPopupOptions) } current = popup const contents = window.webContents + recordBrowserSessionInput(contents) contents.on('will-prevent-unload', allowPageUnload) // Why measure in the page: preferred-size-changed only reports while the window paints, and its @@ -188,7 +190,7 @@ export function openBrowserExtensionPopup(options: BrowserExtensionPopupOptions) void watchSize().then(() => { if (!window.isDestroyed()) { presented = true - presentBrowserExtensionWindow(window, true) + presentBrowserExtensionWindow(window, 'focused') } }) }) diff --git a/src/main/browser/extensions/actions/browser-extension-toolbar.ts b/src/main/browser/extensions/actions/browser-extension-toolbar.ts index eb0bb73c23a..c1d04712afa 100644 --- a/src/main/browser/extensions/actions/browser-extension-toolbar.ts +++ b/src/main/browser/extensions/actions/browser-extension-toolbar.ts @@ -134,7 +134,7 @@ function openPopupForPage( openTab: (rawUrl) => { const resolved = resolveBrowserExtensionRequestedUrl(rawUrl, extension.id) if (resolved && (resolved.kind !== 'extension' || resolved.extensionId === extension.id)) { - void openBrowserExtensionTab(page.profileId, resolved.url, true).catch(() => {}) + void openBrowserExtensionTab(page.profileId, resolved.url, { active: true }).catch(() => {}) } } }) diff --git a/src/main/browser/extensions/api-runtime/browser-extension-api-host.test.ts b/src/main/browser/extensions/api-runtime/browser-extension-api-host.test.ts index 20cdd567c36..8c1612a5a8e 100644 --- a/src/main/browser/extensions/api-runtime/browser-extension-api-host.test.ts +++ b/src/main/browser/extensions/api-runtime/browser-extension-api-host.test.ts @@ -95,7 +95,7 @@ const extensionSession = { on: vi.fn() }, serviceWorkers: { on: vi.fn(), getAllRunning: () => ({}), getWorkerFromVersionID: vi.fn() }, - protocol: { handle: vi.fn(), unhandle: vi.fn() } + protocol: { handle: vi.fn(), unhandle: vi.fn(), isProtocolHandled: vi.fn(() => true) } } const handled: unknown[][] = [] @@ -276,3 +276,29 @@ describe('extension subframe listener retirement', () => { } }) }) + +it('forgets the extension protocol twice without Electron refusing the second unhandle', () => { + const handled = new Set() + const unhandle = vi.fn((scheme: string) => { + if (!handled.delete(scheme)) { + throw new Error(`Failed to unhandle protocol: ${scheme}`) + } + }) + const sess = { + ...extensionSession, + protocol: { + handle: (scheme: string) => { + handled.add(scheme) + }, + isProtocolHandled: (scheme: string) => handled.has(scheme), + unhandle + } + } + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: this session copies the host's stub and replaces only protocol, which the retirement path calls. + const session = sess as unknown as Electron.Session + host.attachBrowserExtensionApiHostToSession(session, 'deleted-profile') + host.forgetBrowserExtensionContexts(session) + expect(() => host.forgetBrowserExtensionContexts(session)).not.toThrow() + expect(unhandle).toHaveBeenCalledTimes(1) + expect(unhandle).toHaveBeenCalledWith('orca-extension-api') +}) diff --git a/src/main/browser/extensions/api-runtime/browser-extension-api-host.ts b/src/main/browser/extensions/api-runtime/browser-extension-api-host.ts index f9be8da76d5..e743d4e082b 100644 --- a/src/main/browser/extensions/api-runtime/browser-extension-api-host.ts +++ b/src/main/browser/extensions/api-runtime/browser-extension-api-host.ts @@ -186,7 +186,10 @@ export function attachBrowserExtensionApiHostToSession( export function forgetBrowserExtensionContexts(sess: Electron.Session): void { forgetHostSession(sess) - sess.protocol.unhandle(BROWSER_EXTENSION_API_SCHEME) + // Why: profile delete forgets this host twice, and Electron throws if the scheme is already gone. + if (sess.protocol.isProtocolHandled(BROWSER_EXTENSION_API_SCHEME)) { + sess.protocol.unhandle(BROWSER_EXTENSION_API_SCHEME) + } } export function getBrowserExtensionProfileIdForSession(sess: Electron.Session): string | null { diff --git a/src/main/browser/extensions/api-runtime/browser-extension-worker-api-host.test.ts b/src/main/browser/extensions/api-runtime/browser-extension-worker-api-host.test.ts index 4e55897c27a..0103a34daa1 100644 --- a/src/main/browser/extensions/api-runtime/browser-extension-worker-api-host.test.ts +++ b/src/main/browser/extensions/api-runtime/browser-extension-worker-api-host.test.ts @@ -81,7 +81,8 @@ function makeSession(ids: string[]) { handle: (_scheme: string, handler: ProtocolHandler) => { handlers.push(handler) }, - unhandle: vi.fn() + unhandle: vi.fn(), + isProtocolHandled: () => true } } } diff --git a/src/main/browser/extensions/api/action.ts b/src/main/browser/extensions/api/action.ts index df3e5dfd70e..0375fb8e5a1 100644 --- a/src/main/browser/extensions/api/action.ts +++ b/src/main/browser/extensions/api/action.ts @@ -21,6 +21,7 @@ import { import { requireCallerTab } from '../tabs-and-windows/browser-extension-tab-model' import { getPinnedBrowserExtensionIds } from '../actions/browser-extension-toolbar-pins' import { requestBrowserExtensionPopup } from '../actions/browser-extension-toolbar' +import { findBrowserExtensionCallerAgentPage } from '../tabs-and-windows/browser-extension-agent-caller' const tabIdSchema = z.number().int().nonnegative() const tabDetailsSchema = z.object({ tabId: tabIdSchema.optional() }).loose() @@ -200,7 +201,15 @@ function createActionNamespace(): BrowserExtensionNamespace { })), openPopup: defineBrowserExtensionMethod( z.tuple([z.object({ windowId: z.number().int() }).loose().optional()]), - (caller) => requestBrowserExtensionPopup(caller.session, caller.extension.id) + (caller) => { + // Why: the popup opens focused on the user's own tab, and an agent's call never takes it over. + if (findBrowserExtensionCallerAgentPage(caller)) { + throw new BrowserExtensionApiError( + 'An agent is using this browser profile, so the popup was not opened' + ) + } + return requestBrowserExtensionPopup(caller.session, caller.extension.id) + } ) }, events: ['onClicked', 'onUserSettingsChanged'] diff --git a/src/main/browser/extensions/api/browser-extension-api-namespaces.test.ts b/src/main/browser/extensions/api/browser-extension-api-namespaces.test.ts index 4cad31a7369..fbdfecf6ca5 100644 --- a/src/main/browser/extensions/api/browser-extension-api-namespaces.test.ts +++ b/src/main/browser/extensions/api/browser-extension-api-namespaces.test.ts @@ -141,7 +141,7 @@ const extensionSession = { (_filter: unknown, callback: (details: Record) => void) => callback ) }, - protocol: { handle: vi.fn(), unhandle: vi.fn() }, + protocol: { handle: vi.fn(), unhandle: vi.fn(), isProtocolHandled: vi.fn(() => true) }, extensions: Object.assign(new EventEmitter(), { getExtension: (id: string) => extensions.get(id) ?? null, getAllExtensions: () => [...extensions.values()] diff --git a/src/main/browser/extensions/api/runtime.ts b/src/main/browser/extensions/api/runtime.ts index 4b0f8d16974..238b2e46b0b 100644 --- a/src/main/browser/extensions/api/runtime.ts +++ b/src/main/browser/extensions/api/runtime.ts @@ -1,4 +1,5 @@ import { openBrowserExtensionOptionsPage } from '../tabs-and-windows/browser-extension-options-page' +import { placeBrowserExtensionCallerTab } from '../tabs-and-windows/browser-extension-agent-caller' import { z } from 'zod' import { defineBrowserExtensionMethod, @@ -10,7 +11,11 @@ export function registerRuntimeNamespace(): void { registerBrowserExtensionNamespace('runtime', { methods: { openOptionsPage: defineBrowserExtensionMethod(z.tuple([]), (caller) => - openBrowserExtensionOptionsPage(caller.browserProfileId, caller.extension) + openBrowserExtensionOptionsPage( + caller.browserProfileId, + caller.extension, + placeBrowserExtensionCallerTab(caller, true) + ) ) }, events: ['onInstalled', 'onStartup'] diff --git a/src/main/browser/extensions/api/tabs.ts b/src/main/browser/extensions/api/tabs.ts index b69233ca4d3..ee0cd9d4f5b 100644 --- a/src/main/browser/extensions/api/tabs.ts +++ b/src/main/browser/extensions/api/tabs.ts @@ -34,6 +34,10 @@ import { listBrowserExtensionDiscardedTabs, type BrowserExtensionDiscardedTabEntry } from '../tabs-and-windows/browser-extension-discarded-tab-model' +import { + findBrowserExtensionCallerAgentPage, + placeBrowserExtensionCallerTab +} from '../tabs-and-windows/browser-extension-agent-caller' import { captureVisibleTabMethod } from './tabs-capture' import { tabsPageMethods } from './tabs-page-methods' import { @@ -82,7 +86,11 @@ async function createTab( url: string, active: boolean ): Promise { - const tabId = await openBrowserExtensionTab(caller.browserProfileId, url, active) + const tabId = await openBrowserExtensionTab( + caller.browserProfileId, + url, + placeBrowserExtensionCallerTab(caller, active) + ) return describeBrowserExtensionTab(caller.extension, requireCallerTab(caller, tabId)) } @@ -129,6 +137,17 @@ function queryTabs( .sort((a, b) => a.windowId - b.windowId || a.index - b.index) } +/** Highlighting is activating in a one-tab selection (Chrome); an agent's call never switches tabs. */ +function mayActivateTab( + caller: BrowserExtensionCaller, + properties: z.infer +): boolean { + return ( + Boolean(properties.active || properties.highlighted) && + !findBrowserExtensionCallerAgentPage(caller) + ) +} + /** Activation shows the page, which loads it (onReplaced follows); a URL needs a loaded page. */ async function updateDiscardedTab( caller: BrowserExtensionCaller, @@ -138,7 +157,7 @@ async function updateDiscardedTab( if (properties.url !== undefined) { throw new BrowserExtensionApiError(`Cannot navigate tab ${entry.tab.tabId} until it loads`) } - if (properties.active || properties.highlighted) { + if (mayActivateTab(caller, properties)) { await activateBrowserExtensionDiscardedTab(entry) } const current = findBrowserExtensionDiscardedTab(caller.session, entry.tab.tabId) @@ -229,8 +248,7 @@ export function registerTabsNamespace(): void { if (properties.muted !== undefined) { entry.contents.setAudioMuted(properties.muted) } - // Chrome: highlighting a tab in a one-tab selection model is activating it. - if (properties.active || properties.highlighted) { + if (mayActivateTab(caller, properties)) { await activateBrowserExtensionTab(entry) } return describeBrowserExtensionTab(caller.extension, requireCallerTab(caller, id)) diff --git a/src/main/browser/extensions/api/windows.ts b/src/main/browser/extensions/api/windows.ts index dcf3834777f..659717ac35d 100644 --- a/src/main/browser/extensions/api/windows.ts +++ b/src/main/browser/extensions/api/windows.ts @@ -22,6 +22,7 @@ import { type BrowserExtensionWindowEntry } from '../tabs-and-windows/browser-extension-window-model' import { requireOpenableUrl } from '../tabs-and-windows/browser-extension-requested-navigation' +import { findBrowserExtensionCallerAgentPage } from '../tabs-and-windows/browser-extension-agent-caller' const windowIdSchema = z.number().int() const queryOptionsSchema = z @@ -85,23 +86,34 @@ function requireWindow( return entry } +function applyWindowState( + window: Electron.BrowserWindow, + state: z.infer['state'] +): void { + if (state === 'minimized') { + window.minimize() + } else if (state === 'maximized') { + window.maximize() + } else if (state === 'fullscreen') { + window.setFullScreen(true) + } else if (state === 'normal') { + window.setFullScreen(false) + window.unmaximize() + window.restore() + } +} + function applyWindowUpdate( entry: BrowserExtensionWindowEntry, - info: z.infer + info: z.infer, + byAgent: boolean ): void { const { window } = entry // Why: extensions may focus Orca's main window but never move, resize or minimize it. if (!entry.isMain) { - if (info.state === 'minimized') { - window.minimize() - } else if (info.state === 'maximized') { - window.maximize() - } else if (info.state === 'fullscreen') { - window.setFullScreen(true) - } else if (info.state === 'normal') { - window.setFullScreen(false) - window.unmaximize() - window.restore() + // Why not for an agent: restoring, maximizing or fullscreen can show or raise the window. + if (!byAgent || info.state === 'minimized') { + applyWindowState(window, info.state) } const bounds = window.getBounds() window.setBounds({ @@ -114,8 +126,8 @@ function applyWindowUpdate( if (info.drawAttention !== undefined) { window.flashFrame(info.drawAttention) } - if (info.focused) { - presentBrowserExtensionWindow(window, true) + if (info.focused && !byAgent) { + presentBrowserExtensionWindow(window, 'focused') } } @@ -161,7 +173,11 @@ export function registerWindowsNamespace(): void { top: data?.top, width: data?.width, height: data?.height, - focused: data?.focused !== false, + presentation: findBrowserExtensionCallerAgentPage(caller) + ? 'whenAppActive' + : data?.focused === false + ? 'unfocused' + : 'focused', state: data?.state }) return describe(caller, requireWindow(caller, window.id), { populate: true }) @@ -171,7 +187,7 @@ export function registerWindowsNamespace(): void { z.tuple([windowIdSchema, updateInfoSchema]), (caller, windowId, info) => { const entry = requireWindow(caller, windowId) - applyWindowUpdate(entry, info) + applyWindowUpdate(entry, info, findBrowserExtensionCallerAgentPage(caller) !== null) return describe(caller, entry, undefined) } ), diff --git a/src/main/browser/extensions/browser-extension-management.ts b/src/main/browser/extensions/browser-extension-management.ts index 0ff29fd1a80..abf99f9a949 100644 --- a/src/main/browser/extensions/browser-extension-management.ts +++ b/src/main/browser/extensions/browser-extension-management.ts @@ -258,6 +258,6 @@ export function openBrowserExtensionOptions( if (!extension) { throw new Error('Turn the extension on to open its options') } - await openBrowserExtensionOptionsPage(browserProfileId, extension) + await openBrowserExtensionOptionsPage(browserProfileId, extension, { active: true }) }) } diff --git a/src/main/browser/extensions/permissions/browser-extension-user-gesture.ts b/src/main/browser/extensions/permissions/browser-extension-user-gesture.ts index e0abfe450e3..ddf81edeee9 100644 --- a/src/main/browser/extensions/permissions/browser-extension-user-gesture.ts +++ b/src/main/browser/extensions/permissions/browser-extension-user-gesture.ts @@ -2,7 +2,7 @@ import type { BrowserExtensionCaller } from '../api-runtime/browser-extension-na import { getHostSessionState } from '../api-runtime/browser-extension-contexts' import { getBrowserExtensionContextSignal } from '../api-runtime/browser-extension-context-lifetime' -const USER_GESTURE_WINDOW_MS = 5_000 +export const BROWSER_EXTENSION_USER_GESTURE_WINDOW_MS = 5_000 const recentGestures = new WeakMap() @@ -22,15 +22,21 @@ export function noteBrowserExtensionUserGesture( recentGestures.set(signal, Date.now()) } -export function hasBrowserExtensionUserGesture(caller: BrowserExtensionCaller): boolean { - if (caller.userGesture === true) { - return true - } +/** When Orca last handed the caller's extension a gesture, if still within the gesture window. */ +export function getBrowserExtensionUserGestureAt(caller: BrowserExtensionCaller): number | null { const state = getHostSessionState(caller.session) if (!state) { - return false + return null } const signal = getBrowserExtensionContextSignal(state, caller.extension.id) const at = recentGestures.get(signal) - return at !== undefined && !signal.aborted && Date.now() - at <= USER_GESTURE_WINDOW_MS + return at !== undefined && + !signal.aborted && + Date.now() - at <= BROWSER_EXTENSION_USER_GESTURE_WINDOW_MS + ? at + : null +} + +export function hasBrowserExtensionUserGesture(caller: BrowserExtensionCaller): boolean { + return caller.userGesture === true || getBrowserExtensionUserGestureAt(caller) !== null } diff --git a/src/main/browser/extensions/profile-sessions/browser-extension-session-loader.test.ts b/src/main/browser/extensions/profile-sessions/browser-extension-session-loader.test.ts index dca2ea6a440..22dbccd3d01 100644 --- a/src/main/browser/extensions/profile-sessions/browser-extension-session-loader.test.ts +++ b/src/main/browser/extensions/profile-sessions/browser-extension-session-loader.test.ts @@ -225,6 +225,10 @@ describe('deleted profile action state', () => { if (failedUnload) { mocks.loaded.set(ID, { id: ID }) + getBrowserExtensionProfileStateStore().update(profileId, ID, (state) => ({ + ...state, + workerSignature: 'left-behind' + })) const unload = vi .spyOn(mocks.session.extensions, 'removeExtension') .mockImplementationOnce(() => { @@ -234,6 +238,7 @@ describe('deleted profile action state', () => { await expect(loader.detachBrowserExtensionsFromProfileSession(profile)).rejects.toThrow( 'native unload failed' ) + expect(getBrowserExtensionProfileStateStore().readProfile(profileId)).toEqual({}) } finally { unload.mockRestore() mocks.loaded.delete(ID) diff --git a/src/main/browser/extensions/profile-sessions/browser-extension-session-loader.ts b/src/main/browser/extensions/profile-sessions/browser-extension-session-loader.ts index fffb37c4e39..15ef8a8a457 100644 --- a/src/main/browser/extensions/profile-sessions/browser-extension-session-loader.ts +++ b/src/main/browser/extensions/profile-sessions/browser-extension-session-loader.ts @@ -158,10 +158,10 @@ export async function detachBrowserExtensionsFromProfileSession( } } finally { forgetBrowserExtensionActionProfile(profile.id) + getBrowserExtensionProfileStateStore().forgetProfile(profile.id) + getBrowserExtensionSyncStorageStore().forgetProfile(profile.id) + getBrowserExtensionDnrStore().forgetProfile(profile.id) } - getBrowserExtensionProfileStateStore().forgetProfile(profile.id) - getBrowserExtensionSyncStorageStore().forgetProfile(profile.id) - getBrowserExtensionDnrStore().forgetProfile(profile.id) } /** The extensions setting turned off: every profile drops its extensions until it turns on. */ diff --git a/src/main/browser/extensions/tabs-and-windows/browser-extension-agent-caller.test.ts b/src/main/browser/extensions/tabs-and-windows/browser-extension-agent-caller.test.ts new file mode 100644 index 00000000000..05e31b9ba44 --- /dev/null +++ b/src/main/browser/extensions/tabs-and-windows/browser-extension-agent-caller.test.ts @@ -0,0 +1,112 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +const browserMocks = vi.hoisted(() => ({ + appGetPathMock: vi.fn(() => '/downloads'), + shellOpenExternalMock: vi.fn(), + browserWindowFromWebContentsMock: vi.fn(), + menuBuildFromTemplateMock: vi.fn(), + guestOffMock: vi.fn(), + guestOnMock: vi.fn(), + guestSetBackgroundThrottlingMock: vi.fn(), + guestSetWindowOpenHandlerMock: vi.fn(), + guestOpenDevToolsMock: vi.fn(), + webContentsFromIdMock: vi.fn(), + screenGetCursorScreenPointMock: vi.fn(() => ({ x: 0, y: 0 })), + openPopupWithOriginBarMock: vi.fn() +})) + +vi.mock('electron', () => ({ + app: { prependListener: vi.fn(), off: vi.fn(), getPath: browserMocks.appGetPathMock }, + BrowserWindow: { fromWebContents: browserMocks.browserWindowFromWebContentsMock }, + clipboard: { writeText: vi.fn() }, + shell: { openExternal: browserMocks.shellOpenExternalMock }, + Menu: { buildFromTemplate: browserMocks.menuBuildFromTemplateMock }, + screen: { getCursorScreenPoint: browserMocks.screenGetCursorScreenPointMock }, + webContents: { fromId: browserMocks.webContentsFromIdMock } +})) + +import { browserManager } from '../../guest-lifecycle/manager/browser-manager' +import { runWithActiveRpcCaller } from '../../../runtime/rpc/callers/active-call' +import { withAgentInputInjection } from '../../cdp/input/agent-input-injection' +import { + guestNavigationMethods, + rendererWebContentsId, + resetBrowserManagerMocks, + resetBrowserManagerState +} from '../../guest-lifecycle/manager/browser-manager-test-harness' +import type { BrowserExtensionCaller } from '../api-runtime/browser-extension-namespace-registry' +import { placeBrowserExtensionCallerTab } from './browser-extension-agent-caller' + +const workSession = { name: 'work' } +const otherSession = { name: 'other' } + +/** Registers a guest page; `click` delivers an activating input to it. */ +function registerPage(browserPageId: string, id: number, worktreeId: string) { + const on = vi.fn() + const guest = { + ...guestNavigationMethods(), + id, + session: workSession, + isDestroyed: vi.fn(() => false), + isCurrentlyAudible: vi.fn(() => false), + getType: vi.fn(() => 'webview'), + setBackgroundThrottling: vi.fn(), + setWindowOpenHandler: vi.fn(), + setIgnoreMenuShortcuts: vi.fn(), + on, + prependListener: vi.fn(), + off: vi.fn(), + openDevTools: vi.fn() + } + const previous = browserMocks.webContentsFromIdMock.getMockImplementation() + browserMocks.webContentsFromIdMock.mockImplementation((lookup: number) => + lookup === id ? guest : previous?.(lookup) + ) + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the manager and input tracker call only the methods stubbed above. + const contents = guest as unknown as Electron.WebContents + browserManager.attachGuestPolicies(contents) + browserManager.registerGuest({ + browserPageId, + worktreeId, + webContentsId: id, + rendererWebContentsId + }) + const onInput = on.mock.calls.find(([event]) => event === 'input-event')?.[1] + return { contents, click: () => onInput({}, { type: 'mouseUp' }) } +} + +function caller(session: typeof workSession, extra: Partial = {}) { + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: placement reads session, webContents and userGesture only. + return { session, browserProfileId: 'work', fromWorker: true, ...extra } as BrowserExtensionCaller +} + +beforeEach(() => { + resetBrowserManagerMocks(browserMocks) + resetBrowserManagerState() + browserMocks.webContentsFromIdMock.mockImplementation(() => null) +}) + +describe('an extension tab set off while an agent drives a page in its profile', () => { + it("opens in the background in the agent's workspace, unless the user set it off", async () => { + const userPage = registerPage('user-page', 11, 'wt-user') + const agentPage = registerPage('agent-page', 12, 'wt-agent') + const command = runWithActiveRpcCaller({ kind: 'local-cli' }, () => + browserManager.beginAgentCommand('agent-page') + ) + const placeFromWorker = () => placeBrowserExtensionCallerTab(caller(workSession), true) + const agentPlacement = { active: false, worktreeId: 'wt-agent' } + + expect(placeFromWorker()).toEqual(agentPlacement) + expect(placeBrowserExtensionCallerTab(caller(otherSession), true)).toEqual({ active: true }) + expect( + placeBrowserExtensionCallerTab(caller(workSession, { userGesture: true }), true) + ).toEqual({ active: true }) + + userPage.click() + expect(placeFromWorker()).toEqual({ active: true }) + await withAgentInputInjection(agentPage.contents, async () => agentPage.click()) + expect(placeFromWorker()).toEqual(agentPlacement) + + command.end() + }) +}) diff --git a/src/main/browser/extensions/tabs-and-windows/browser-extension-agent-caller.ts b/src/main/browser/extensions/tabs-and-windows/browser-extension-agent-caller.ts new file mode 100644 index 00000000000..799be836ea7 --- /dev/null +++ b/src/main/browser/extensions/tabs-and-windows/browser-extension-agent-caller.ts @@ -0,0 +1,56 @@ +import { browserManager } from '../../guest-lifecycle/manager/browser-manager' +import { getLastBrowserGuestInputInSession } from '../../guest-lifecycle/input/browser-guest-input-gesture' +import type { BrowserExtensionCaller } from '../api-runtime/browser-extension-namespace-registry' +import { + BROWSER_EXTENSION_USER_GESTURE_WINDOW_MS, + getBrowserExtensionUserGestureAt +} from '../permissions/browser-extension-user-gesture' +import type { openBrowserExtensionTab } from './browser-extension-tab-model' + +export type BrowserExtensionTabPlacement = Parameters[2] + +type AgentPage = { worktreeId: string | null } + +function pageOfGuest(guestWebContentsId: number): AgentPage { + const tabId = browserManager.getOwningTabIdForGuestWebContentsId(guestWebContentsId) + return { worktreeId: (tabId && browserManager.getWorktreeIdForTab(tabId)) ?? null } +} + +/** + * The agent's page this call acts for, or null when the user set it off. An extension reacts to + * its profile's pages (a wallet popup, an OAuth tab) without saying which input started it, so + * the newest recent click, key or Orca-delivered gesture decides, then whether an agent leads. + */ +export function findBrowserExtensionCallerAgentPage( + caller: BrowserExtensionCaller +): AgentPage | null { + const now = Date.now() + const lastInput = getLastBrowserGuestInputInSession(caller.session) + const input = + lastInput && now - lastInput.at <= BROWSER_EXTENSION_USER_GESTURE_WINDOW_MS ? lastInput : null + const gestureAt = getBrowserExtensionUserGestureAt(caller) + if (gestureAt !== null && (!input || gestureAt >= input.at)) { + return null + } + if (input) { + // Agents and remote viewers send injected input; only the desktop user's own may surface. + return input.byAgent ? pageOfGuest(input.guestWebContentsId) : null + } + // Why before userGesture: an agent's click on an extension page also activates that page. + if (caller.webContents && browserManager.isAgentLeadingGuest(caller.webContents.id)) { + return pageOfGuest(caller.webContents.id) + } + return caller.userGesture ? null : browserManager.findAgentPageInSession(caller.session) +} + +/** An agent's extension tab opens in the background beside its page, never in the user's view. */ +export function placeBrowserExtensionCallerTab( + caller: BrowserExtensionCaller, + active: boolean +): BrowserExtensionTabPlacement { + const agentPage = findBrowserExtensionCallerAgentPage(caller) + if (!agentPage) { + return { active } + } + return { active: false, ...(agentPage.worktreeId ? { worktreeId: agentPage.worktreeId } : {}) } +} diff --git a/src/main/browser/extensions/tabs-and-windows/browser-extension-discarded-tabs.test.ts b/src/main/browser/extensions/tabs-and-windows/browser-extension-discarded-tabs.test.ts index fe53dc4afc5..93415b319b2 100644 --- a/src/main/browser/extensions/tabs-and-windows/browser-extension-discarded-tabs.test.ts +++ b/src/main/browser/extensions/tabs-and-windows/browser-extension-discarded-tabs.test.ts @@ -46,7 +46,9 @@ vi.mock('../../guest-lifecycle/manager/browser-manager', () => ({ getWebContentsIdByTabId: () => new Map([...mocks.pageByGuest].map(([id, pageId]) => [pageId, id])), getTabIdForWebContentsId: (id: number) => mocks.pageByGuest.get(id) ?? null, - getWorktreeIdForTab: () => undefined + getWorktreeIdForTab: () => undefined, + isAgentLeadingGuest: () => false, + findAgentPageInSession: () => null } })) vi.mock('../../../runtime/browser/tabs/browser-tab-renderer-create', () => ({})) diff --git a/src/main/browser/extensions/tabs-and-windows/browser-extension-options-page.ts b/src/main/browser/extensions/tabs-and-windows/browser-extension-options-page.ts index 541ab745a2f..0c2d5d8647c 100644 --- a/src/main/browser/extensions/tabs-and-windows/browser-extension-options-page.ts +++ b/src/main/browser/extensions/tabs-and-windows/browser-extension-options-page.ts @@ -1,6 +1,7 @@ import { getChromeExtensionOrigin } from '../../../../shared/browser/extensions/browser-extension-url' import { BrowserExtensionApiError } from '../api-runtime/browser-extension-namespace-registry' import { openBrowserExtensionTab } from './browser-extension-tab-model' +import type { BrowserExtensionTabPlacement } from './browser-extension-agent-caller' function optionsPagePath(extension: Electron.Extension): string | null { const manifest: { options_ui?: { page?: unknown }; options_page?: unknown } = @@ -12,12 +13,13 @@ function optionsPagePath(extension: Electron.Extension): string | null { /** Opens the extension's options page as an Orca tab in the given browser profile. */ export async function openBrowserExtensionOptionsPage( browserProfileId: string, - extension: Electron.Extension + extension: Electron.Extension, + placement: BrowserExtensionTabPlacement ): Promise { const page = optionsPagePath(extension) if (!page) { throw new BrowserExtensionApiError('This extension has no options page') } const url = new URL(page.replace(/^\/+/, ''), `${getChromeExtensionOrigin(extension.id)}/`) - await openBrowserExtensionTab(browserProfileId, url.toString(), true) + await openBrowserExtensionTab(browserProfileId, url.toString(), placement) } diff --git a/src/main/browser/extensions/tabs-and-windows/browser-extension-tab-model.test.ts b/src/main/browser/extensions/tabs-and-windows/browser-extension-tab-model.test.ts index dd92338bfe7..48d2a245c5f 100644 --- a/src/main/browser/extensions/tabs-and-windows/browser-extension-tab-model.test.ts +++ b/src/main/browser/extensions/tabs-and-windows/browser-extension-tab-model.test.ts @@ -79,7 +79,7 @@ describe('an extension tab opened while Orca starts', () => { mocks.pages.set('welcome', { id: 21, session: mocks.workSession, worktreeId: 'wt-a' }) return { browserPageId: 'welcome' } }) - const opening = openBrowserExtensionTab('work', 'https://welcome.test', true) + const opening = openBrowserExtensionTab('work', 'https://welcome.test', { active: true }) await Promise.resolve() expect(mocks.createTab).not.toHaveBeenCalled() window.rendererReady() @@ -87,7 +87,7 @@ describe('an extension tab opened while Orca starts', () => { // A reload waits for the new document to restore again. window.reload() - const reopening = openBrowserExtensionTab('work', 'https://welcome.test', true) + const reopening = openBrowserExtensionTab('work', 'https://welcome.test', { active: true }) await Promise.resolve() expect(mocks.createTab).toHaveBeenCalledTimes(1) window.rendererReady() diff --git a/src/main/browser/extensions/tabs-and-windows/browser-extension-tab-model.ts b/src/main/browser/extensions/tabs-and-windows/browser-extension-tab-model.ts index 7d79f210caa..021fdffa128 100644 --- a/src/main/browser/extensions/tabs-and-windows/browser-extension-tab-model.ts +++ b/src/main/browser/extensions/tabs-and-windows/browser-extension-tab-model.ts @@ -295,7 +295,7 @@ export function recentWorktreeIdForBrowserProfile(profileId: string): string | u export async function openBrowserExtensionTab( browserProfileId: string, url: string, - active: boolean + placement: { active: boolean; worktreeId?: string } ): Promise { const profile = getBrowserExtensionProfile(browserProfileId) // Why wait: extensions open tabs as they load (onInstalled), before Orca's window can take them. @@ -306,10 +306,10 @@ export async function openBrowserExtensionTab( const { browserPageId } = await createBrowserTabInRenderer( { getAuthoritativeWindow: () => mainWindow }, url, - undefined, + placement.worktreeId, profile.id, profile.partition, - active, + placement.active, undefined, recentWorktreeIdForBrowserProfile(profile.id) ) @@ -340,7 +340,7 @@ export async function closeBrowserExtensionTab(entry: BrowserExtensionTabEntry): export async function activateBrowserExtensionTab(entry: BrowserExtensionTabEntry): Promise { const popout = getPopoutWindowForWebContents(entry.contents) if (popout) { - presentBrowserExtensionWindow(popout, true) + presentBrowserExtensionWindow(popout, 'focused') return } const browserPageId = browserManager.getTabIdForWebContentsId(entry.contents.id) diff --git a/src/main/browser/extensions/tabs-and-windows/browser-extension-window-model.test.ts b/src/main/browser/extensions/tabs-and-windows/browser-extension-window-model.test.ts index 283644fdd9d..ac608c74575 100644 --- a/src/main/browser/extensions/tabs-and-windows/browser-extension-window-model.test.ts +++ b/src/main/browser/extensions/tabs-and-windows/browser-extension-window-model.test.ts @@ -56,7 +56,7 @@ describe("a profile's extension windows", () => { const popout = model.createBrowserExtensionPopout(profileB, { url: 'https://b.test/', type: 'popup', - focused: false + presentation: 'unfocused' }) const ids = (sess: Electron.Session) => model.listBrowserExtensionWindows(sess).map((entry) => entry.window.id) @@ -80,7 +80,11 @@ describe("a profile's extension windows", () => { it("keep each profile's last-focused popout when another profile's popout takes focus", () => { const main = new BrowserWindow() model.trackBrowserExtensionMainWindow(main) - const options = { url: 'https://x.test/', type: 'popup' as const, focused: false } + const options = { + url: 'https://x.test/', + type: 'popup' as const, + presentation: 'unfocused' as const + } const popoutA = model.createBrowserExtensionPopout(profileA, options) const popoutB = model.createBrowserExtensionPopout(profileB, options) focus(popoutA) diff --git a/src/main/browser/extensions/tabs-and-windows/browser-extension-window-model.ts b/src/main/browser/extensions/tabs-and-windows/browser-extension-window-model.ts index e023258148b..dff06453444 100644 --- a/src/main/browser/extensions/tabs-and-windows/browser-extension-window-model.ts +++ b/src/main/browser/extensions/tabs-and-windows/browser-extension-window-model.ts @@ -4,10 +4,12 @@ import { normalizeBrowserNavigationUrl } from '../../../../shared/browser-url' import { isSameExtensionNavigation } from '../../../../shared/browser/extensions/browser-extension-url' import { isBackgroundLaunch, + showWhenAppActive, showWindowWithoutStealingFocus } from '../../../window/activation/foreground-activation-policy' import { emitBrowserExtensionEvent } from '../api-runtime/browser-extension-api-host' import { watchBrowserMainWindowReadiness } from '../../guest-lifecycle/browser-main-window-readiness' +import { recordBrowserSessionInput } from '../../guest-lifecycle/input/browser-guest-input-gesture' /** chrome.windows constants. */ export const WINDOW_ID_NONE = -1 @@ -187,9 +189,19 @@ export function describeBrowserExtensionWindow( } } +/** `whenAppActive`: an agent's window, shown without focus once the user is in Orca. */ +export type BrowserExtensionWindowPresentation = 'focused' | 'unfocused' | 'whenAppActive' + /** Focus or reveal per Orca's launch policy; automated (background) runs never take the foreground. */ -export function presentBrowserExtensionWindow(window: BrowserWindow, focused: boolean): void { - if (focused && !isBackgroundLaunch()) { +export function presentBrowserExtensionWindow( + window: BrowserWindow, + presentation: BrowserExtensionWindowPresentation +): void { + if (presentation === 'whenAppActive') { + showWhenAppActive(window) + return + } + if (presentation === 'focused' && !isBackgroundLaunch()) { window.show() window.focus() return @@ -204,7 +216,7 @@ export type BrowserExtensionPopoutOptions = { top?: number width?: number height?: number - focused: boolean + presentation: BrowserExtensionWindowPresentation state?: 'normal' | 'minimized' | 'maximized' | 'fullscreen' } @@ -236,6 +248,7 @@ export function createBrowserExtensionPopout( popoutsByWindowId.delete(windowId) emitBrowserExtensionEvent({ session: sess }, 'windows', 'onRemoved', [windowId]) }) + recordBrowserSessionInput(window.webContents) window.webContents.setWindowOpenHandler(() => ({ action: 'deny' })) // Why: the popout gets no Orca guest policies, so it may only move within its extension or to web URLs. window.webContents.on('will-navigate', (event, url) => { @@ -245,16 +258,19 @@ export function createBrowserExtensionPopout( } }) window.once('ready-to-show', () => { - if (options.state === 'maximized') { - window.maximize() - } else if (options.state === 'fullscreen') { - window.setFullScreen(true) - } if (options.state === 'minimized') { window.minimize() return } - presentBrowserExtensionWindow(window, options.focused) + // Why not for an agent: maximize can show the window and fullscreen moves the user to a new Space. + if (options.presentation !== 'whenAppActive') { + if (options.state === 'maximized') { + window.maximize() + } else if (options.state === 'fullscreen') { + window.setFullScreen(true) + } + } + presentBrowserExtensionWindow(window, options.presentation) }) window.loadURL(options.url).catch((error: unknown) => { console.warn('[browser-extensions] Popout failed to load:', error) diff --git a/src/main/browser/guest-lifecycle/input/browser-guest-input-gesture.ts b/src/main/browser/guest-lifecycle/input/browser-guest-input-gesture.ts index c94358b85ab..61545e4ec86 100644 --- a/src/main/browser/guest-lifecycle/input/browser-guest-input-gesture.ts +++ b/src/main/browser/guest-lifecycle/input/browser-guest-input-gesture.ts @@ -27,6 +27,33 @@ export function isActivatingBrowserGuestInput(input: Electron.InputEvent | undef return typeof input?.type === 'string' && ACTIVATING_INPUT_TYPES.has(input.type) } +export type LastBrowserGuestInput = { at: number; byAgent: boolean; guestWebContentsId: number } + +// Why per session: an extension reacts to its profile's pages without knowing which input set it off. +const lastInputBySession = new WeakMap() + +function noteSessionInput(contents: WebContents, at: number, byAgent: boolean): void { + if (contents.session) { + lastInputBySession.set(contents.session, { at, byAgent, guestWebContentsId: contents.id }) + } +} + +/** For windows with no guest policies (extension popups and popouts): their clicks count for the session too. */ +export function recordBrowserSessionInput(contents: WebContents): void { + contents.on('input-event', (_event, input) => { + if (isActivatingBrowserGuestInput(input)) { + noteSessionInput(contents, Date.now(), isAgentInjectingInput(contents)) + } + }) +} + +/** The latest activating input any guest in `session` received, and who sent it. */ +export function getLastBrowserGuestInputInSession( + session: Electron.Session +): LastBrowserGuestInput | null { + return lastInputBySession.get(session) ?? null +} + /** A recent activating input; `byAgent` when an agent or remote viewer sent it, not this desktop's user. */ export type ObservedBrowserGuestGesture = { byAgent: boolean } @@ -38,7 +65,10 @@ export type BrowserGuestInputGesture = { export function trackBrowserGuestInputGesture( guest: WebContents, - { now = Date.now, onActivatingInput }: { now?: () => number; onActivatingInput?: () => void } = {} + { + now = Date.now, + onActivatingInput + }: { now?: () => number; onActivatingInput?: (byAgent: boolean) => void } = {} ): BrowserGuestInputGesture { let lastGestureAt: number | null = null let lastGestureByAgent = false @@ -46,7 +76,8 @@ export function trackBrowserGuestInputGesture( if (isActivatingBrowserGuestInput(input)) { lastGestureAt = now() lastGestureByAgent = isAgentInjectingInput(guest) - onActivatingInput?.() + noteSessionInput(guest, lastGestureAt, lastGestureByAgent) + onActivatingInput?.(lastGestureByAgent) } } let attached = false diff --git a/src/main/browser/guest-lifecycle/manager/browser-manager-queries.ts b/src/main/browser/guest-lifecycle/manager/browser-manager-queries.ts index 08b454f12b2..fa1da0cdd70 100644 --- a/src/main/browser/guest-lifecycle/manager/browser-manager-queries.ts +++ b/src/main/browser/guest-lifecycle/manager/browser-manager-queries.ts @@ -58,6 +58,20 @@ export abstract class BrowserManagerQueries extends BrowserManagerRegistration { return this.worktreeIdByTabId.get(this.getOwningTabIdForPage(browserTabId)) } + /** The workspace of a page an agent is driving whose guest lives in `sess` (null when unknown). */ + findAgentPageInSession(sess: Electron.Session): { worktreeId: string | null } | null { + for (const browserPageId of this.agentActivity.activePageIds()) { + const guestId = + this.resolvePopupPageWebContentsId(browserPageId) ?? + this.webContentsIdByTabId.get(browserPageId) + const guest = guestId === undefined ? undefined : webContents.fromId(guestId) + if (guest && !guest.isDestroyed() && guest.session === sess) { + return { worktreeId: this.getWorktreeIdForTab(browserPageId) ?? null } + } + } + return null + } + getSessionProfileIdForTab(browserTabId: string): string | null { return this.sessionProfileIdByPageId.get(this.getOwningTabIdForPage(browserTabId)) ?? null } diff --git a/src/main/browser/guest-lifecycle/manager/browser-manager-state.ts b/src/main/browser/guest-lifecycle/manager/browser-manager-state.ts index e0c93da3595..2ae5b243233 100644 --- a/src/main/browser/guest-lifecycle/manager/browser-manager-state.ts +++ b/src/main/browser/guest-lifecycle/manager/browser-manager-state.ts @@ -160,6 +160,8 @@ export abstract class BrowserManagerState extends BrowserManagerViewportScrollSt protected readonly annotationViewportBridgeOpsByTabId = new Map>() protected readonly worktreeIdByTabId = new Map() protected readonly policyAttachedGuestIds = new Set() + // Guests an agent command targeted since the desktop user last gave them input. + protected readonly agentLedGuestIds = new Set() protected readonly offscreenGuestIds = new Set() protected readonly devToolsDock = new BrowserGuestDevToolsDock() protected readonly policyCleanupByGuestId = new Map void>() diff --git a/src/main/browser/guest-lifecycle/manager/browser-manager-test-harness.ts b/src/main/browser/guest-lifecycle/manager/browser-manager-test-harness.ts index a3dca00ccb5..370323652b8 100644 --- a/src/main/browser/guest-lifecycle/manager/browser-manager-test-harness.ts +++ b/src/main/browser/guest-lifecycle/manager/browser-manager-test-harness.ts @@ -60,6 +60,7 @@ export function resetBrowserManagerMocks(mocks: BrowserManagerMocks): void { export function resetBrowserManagerState(): void { browserManager.unregisterAll() browserManager.setBrowserGuestStateChangedListener(null) + browserManager.setAgentLeadListener(null) browserManager.setOffscreenTabOpener(null) browserManager.setDictationShortcutForwardingPredicate(null) browserManager.setSettingsResolver(() => ({})) diff --git a/src/main/browser/guest-lifecycle/manager/browser-manager-visibility.ts b/src/main/browser/guest-lifecycle/manager/browser-manager-visibility.ts index 89bb3d77c93..cea4fdad72e 100644 --- a/src/main/browser/guest-lifecycle/manager/browser-manager-visibility.ts +++ b/src/main/browser/guest-lifecycle/manager/browser-manager-visibility.ts @@ -12,7 +12,8 @@ import { } from '../page-activity/browser-agent-activity' import { BrowserPageEvents, type BrowserPageEvent } from '../page-activity/browser-page-events' import { BrowserPageInputCredit } from '../../routing/input-ownership/browser-page-input-credit' -import { browserPopupPageId } from '../popups/browser-popup-page-id' +import { browserPopupPageId, browserPopupPageWebContentsId } from '../popups/browser-popup-page-id' +import { resolveBrowserRouteGuestPopupOpener } from '../../routing/guest-authority/browser-route-guest-popup-ownership' import type { RpcCallerIdentity } from '../../../runtime/rpc/callers/rpc-caller-identity' export abstract class BrowserManagerVisibility extends BrowserManagerState { @@ -24,6 +25,8 @@ export abstract class BrowserManagerVisibility extends BrowserManagerState { worktreeForPage: (browserPageId) => this.worktreeIdByTabId.get(browserPageId) ?? null }) + private agentLeadListener: ((guestWebContentsId: number, led: boolean) => void) | null = null + protected readonly pageEvents = new BrowserPageEvents() protected readonly pageInputCredit = new BrowserPageInputCredit() @@ -37,7 +40,50 @@ export abstract class BrowserManagerVisibility extends BrowserManagerState { /** Holds the page's agent marking for one queued command, from queueing until it settles. */ beginAgentCommand(browserPageId: string): AgentCommandLease { - return this.agentActivity.beginCommand(browserPageId) + const lease = this.agentActivity.beginCommand(browserPageId) + const pageGuestId = + this.webContentsIdByTabId.get(browserPageId) ?? browserPopupPageWebContentsId(browserPageId) + // An SSH route popup has no guest policies, so its opener carries the lead. + const guestId = + pageGuestId === null + ? null + : (resolveBrowserRouteGuestPopupOpener(pageGuestId) ?? pageGuestId) + // Only attached guests, whose teardown releases the lead. + if (lease.agent && guestId !== null && this.policyAttachedGuestIds.has(guestId)) { + this.leadAgent(guestId) + } + return lease + } + + /** Tells the page's frames when the agent takes or gives up its lead, so they can refuse system dialogs. */ + setAgentLeadListener( + listener: ((guestWebContentsId: number, led: boolean) => void) | null + ): void { + this.agentLeadListener = listener + } + + private leadAgent(guestWebContentsId: number): void { + if (!this.agentLedGuestIds.has(guestWebContentsId)) { + this.agentLedGuestIds.add(guestWebContentsId) + this.agentLeadListener?.(guestWebContentsId, true) + } + } + + /** A popup opened from a page the agent leads starts led too. */ + protected inheritAgentLead(openerWebContentsId: number, popupWebContentsId: number): void { + if ( + this.agentLedGuestIds.has(openerWebContentsId) && + this.policyAttachedGuestIds.has(popupWebContentsId) + ) { + this.leadAgent(popupWebContentsId) + } + } + + /** The desktop user's own input takes the page back from the agent. */ + releaseAgentLead(guestWebContentsId: number): void { + if (this.agentLedGuestIds.delete(guestWebContentsId)) { + this.agentLeadListener?.(guestWebContentsId, false) + } } /** Clears the agent marking from every page this SSH host was driving. */ @@ -65,9 +111,37 @@ export abstract class BrowserManagerVisibility extends BrowserManagerState { } } - /** An agent is driving the page this guest shows (a tab or a popup). */ - isAgentUsingGuest(guestWebContentsId: number): boolean { - return this.isAgentUsingPage(this.pageIdForGuest(guestWebContentsId)) + /** An agent drove the page this guest shows since the desktop user last gave it input. */ + isAgentLedGuest(guestWebContentsId: number): boolean { + // SSH route popups skip guest policies, so they follow their opener's lead. + return this.agentLedGuestIds.has( + resolveBrowserRouteGuestPopupOpener(guestWebContentsId) ?? guestWebContentsId + ) + } + + /** An agent drives the page this guest shows (a tab or a popup), or drove it since the desktop user last gave it input. */ + isAgentLeadingGuest(guestWebContentsId: number): boolean { + return ( + this.isAgentLedGuest(guestWebContentsId) || + this.isAgentUsingPage(this.pageIdForGuest(guestWebContentsId)) + ) + } + + /** A page the agent leads refused its own print dialog, which would take the foreground; tell the agent. */ + reportRefusedPrint(guestWebContentsId: number, origin: string): void { + if (!this.isAgentLedGuest(guestWebContentsId)) { + return + } + // Reach a command on the popup page itself as well as one on the tab that owns it. + const pageIds = new Set([ + this.pageIdForGuest(guestWebContentsId), + this.resolveBrowserTabIdForGuestWebContentsId(guestWebContentsId) + ]) + for (const pageId of pageIds) { + if (pageId) { + this.pageEvents.emit(pageId, { kind: 'print', origin }) + } + } } private pageIdForGuest(guestWebContentsId: number): string { diff --git a/src/main/browser/guest-lifecycle/manager/popups/browser-manager-guest-popup-policy.ts b/src/main/browser/guest-lifecycle/manager/popups/browser-manager-guest-popup-policy.ts index bb23cd8463a..143ea7be548 100644 --- a/src/main/browser/guest-lifecycle/manager/popups/browser-manager-guest-popup-policy.ts +++ b/src/main/browser/guest-lifecycle/manager/popups/browser-manager-guest-popup-policy.ts @@ -29,12 +29,18 @@ import { requestBrowserExternalAppLaunch } from '../../prompts/browser-external- export abstract class BrowserManagerGuestPopupPolicy extends BrowserManagerNavigation { protected installGuestPopupPolicy(guest: Electron.WebContents): () => void { const gesture = trackBrowserGuestInputGesture(guest, { - onActivatingInput: () => this.creditPageInput(guest.id) + onActivatingInput: (byAgent) => { + this.creditPageInput(guest.id) + if (!byAgent) { + this.releaseAgentLead(guest.id) + } + } }) const handleDidCreateWindow = (window: Electron.BrowserWindow): void => { // Popups inherit ownership without replacing the primary guest registration. if (!isBrowserRouteGuestPopup(window.webContents.id)) { this.attachGuestPolicies(window.webContents, this.resolvePopupOwnerContext(guest.id)) + this.inheritAgentLead(guest.id, window.webContents.id) } } guest.on('did-create-window', handleDidCreateWindow) @@ -48,7 +54,7 @@ export abstract class BrowserManagerGuestPopupPolicy extends BrowserManagerNavig // Only the desktop user's own click may bring a popup forward; agents and late opens wait. const showWhenAppActive = observed?.byAgent !== false const activateTab = disposition !== 'background-tab' - const agentOpened = observed?.byAgent !== false && this.isAgentUsingGuest(guest.id) + const agentOpened = observed?.byAgent !== false && this.isAgentLeadingGuest(guest.id) const browserTabId = this.resolvePopupOwnerContext(guest.id)?.browserTabId ?? null const externalUrl = normalizeExternalBrowserUrl(url) @@ -96,8 +102,8 @@ export abstract class BrowserManagerGuestPopupPolicy extends BrowserManagerNavig webPreferences: { ...options.webPreferences, session: guest.session } }) } - } else if (externalUrl && !postBody) { - // Redact the Kagi bearer token before opening the OS browser. + } else if (externalUrl && !postBody && observed?.byAgent === false) { + // The OS browser takes the foreground, so only the desktop user's own click opens it. Redact Kagi's bearer token first. void shell.openExternal(redactKagiSessionToken(externalUrl)) this.forwardOrQueuePopupEvent(guest.id, { origin: safeOrigin(externalUrl), @@ -146,7 +152,7 @@ export abstract class BrowserManagerGuestPopupPolicy extends BrowserManagerNavig guest.downloadURL(newTabUrl) return true } - const agentOpened = observed?.byAgent !== false && this.isAgentUsingGuest(guest.id) + const agentOpened = observed?.byAgent !== false && this.isAgentLeadingGuest(guest.id) if ( this.openLinkInOrcaTab( ownerContext.browserTabId, @@ -223,6 +229,7 @@ export abstract class BrowserManagerGuestPopupPolicy extends BrowserManagerNavig return false } this.attachGuestPolicies(contents, owner) + this.inheritAgentLead(openerGuest.id, contents.id) return true }) const opened = Boolean(options.webContents || options.nativeWindow) diff --git a/src/main/browser/guest-lifecycle/manager/popups/browser-manager-popup-routing.test.ts b/src/main/browser/guest-lifecycle/manager/popups/browser-manager-popup-routing.test.ts index 49d9a894e79..df4a2f4a916 100644 --- a/src/main/browser/guest-lifecycle/manager/popups/browser-manager-popup-routing.test.ts +++ b/src/main/browser/guest-lifecycle/manager/popups/browser-manager-popup-routing.test.ts @@ -44,6 +44,10 @@ vi.mock('../../popups/presentation/popup-origin-bar-window', () => ({ import { browserManager } from '../browser-manager' import { withAgentInputInjection } from '../../../cdp/input/agent-input-injection' import { runWithActiveRpcCaller } from '../../../../runtime/rpc/callers/active-call' +import { + registerBrowserRouteGuestPopup, + releaseBrowserRouteGuestPopup +} from '../../../routing/guest-authority/browser-route-guest-popup-ownership' import { guestNavigationMethods, rendererWebContentsId, @@ -73,7 +77,12 @@ describe('browserManager', () => { // #36804: headless serve pages have no renderer to own Orca's tab model. // orcad's Electron browser registers pages without a workspace, so there is no tab list to open into. - it("opens an agent's link behind the user's tab, and the user's own click in front", async () => { + function attachGuest(): { + guest: { id: number } + rendererSendMock: ReturnType + openLink: (url: string) => void + pressMouse: () => void + } { const rendererSendMock = vi.fn() const guest = { ...guestNavigationMethods(), @@ -99,11 +108,6 @@ describe('browserManager', () => { return null }) browserManager.attachGuestPolicies(guest as never) - browserManager.registerGuest({ - browserPageId: 'browser-1', - webContentsId: guest.id, - rendererWebContentsId - }) const handler: (details: { url: string frameName: string @@ -111,24 +115,46 @@ describe('browserManager', () => { disposition: string }) => unknown = guestSetWindowOpenHandlerMock.mock.calls[0][0] const inputEventHandler = guestOnMock.mock.calls.find(([event]) => event === 'input-event')?.[1] - const openLink = (url: string): void => { - handler({ url, frameName: '', features: '', disposition: 'foreground-tab' }) + return { + guest, + rendererSendMock, + openLink: (url) => { + handler({ url, frameName: '', features: '', disposition: 'foreground-tab' }) + }, + pressMouse: () => inputEventHandler({}, { type: 'mouseUp' }) } + } + + function registerAsTab(guest: { id: number }): void { + browserManager.registerGuest({ + browserPageId: 'browser-1', + webContentsId: guest.id, + rendererWebContentsId + }) + } + + function openedTabs(rendererSendMock: ReturnType): unknown[] { + return rendererSendMock.mock.calls.filter( + ([channel]) => channel === 'browser:open-link-in-orca-tab' + ) + } + + it("opens an agent's link behind the user's tab, and the user's own click in front", async () => { + const { guest, rendererSendMock, openLink, pressMouse } = attachGuest() + registerAsTab(guest) const command = runWithActiveRpcCaller({ kind: 'local-cli' }, () => browserManager.beginAgentCommand('browser-1') ) await withAgentInputInjection(guest as never, async () => { - inputEventHandler({}, { type: 'mouseUp' }) + pressMouse() }) openLink('https://docs.example.com/agent') - inputEventHandler({}, { type: 'mouseUp' }) + pressMouse() openLink('https://docs.example.com/user') command.end() - expect( - rendererSendMock.mock.calls.filter(([channel]) => channel === 'browser:open-link-in-orca-tab') - ).toEqual([ + expect(openedTabs(rendererSendMock)).toEqual([ [ 'browser:open-link-in-orca-tab', { browserPageId: 'browser-1', url: 'https://docs.example.com/agent', activate: false } @@ -139,4 +165,159 @@ describe('browserManager', () => { ] ]) }) + + it('keeps a tab the page opens long after the agent left behind the user, until the user uses the page', async () => { + vi.useFakeTimers() + const { guest, rendererSendMock, openLink, pressMouse } = attachGuest() + registerAsTab(guest) + runWithActiveRpcCaller({ kind: 'local-cli' }, () => + browserManager.beginAgentCommand('browser-1') + ).end() + + vi.advanceTimersByTime(60_000) + openLink('https://login.example.com/late') + pressMouse() + vi.advanceTimersByTime(5_000) + openLink('https://login.example.com/after-user') + + expect(openedTabs(rendererSendMock)).toEqual([ + [ + 'browser:open-link-in-orca-tab', + { browserPageId: 'browser-1', url: 'https://login.example.com/late', activate: false } + ], + [ + 'browser:open-link-in-orca-tab', + { browserPageId: 'browser-1', url: 'https://login.example.com/after-user' } + ] + ]) + }) + + it("keeps a late tab from a popup the agent's page opened behind the user", () => { + vi.useFakeTimers() + const { guest, rendererSendMock } = attachGuest() + registerAsTab(guest) + runWithActiveRpcCaller({ kind: 'local-cli' }, () => + browserManager.beginAgentCommand('browser-1') + ).end() + const popup = { ...guest, id: 146 } + const onDidCreateWindow = guestOnMock.mock.calls.find( + ([event]) => event === 'did-create-window' + )?.[1] + onDidCreateWindow({ webContents: popup }) + const popupHandler = guestSetWindowOpenHandlerMock.mock.calls[1][0] + + vi.advanceTimersByTime(60_000) + popupHandler({ + url: 'https://login.example.com/from-popup', + frameName: '', + features: '', + disposition: 'foreground-tab' + }) + + expect(openedTabs(rendererSendMock)).toEqual([ + [ + 'browser:open-link-in-orca-tab', + { browserPageId: 'browser-1', url: 'https://login.example.com/from-popup', activate: false } + ] + ]) + }) + + it("keeps a late tab from an SSH route popup of the agent's page behind the user", () => { + vi.useFakeTimers() + const { guest, rendererSendMock } = attachGuest() + registerAsTab(guest) + runWithActiveRpcCaller({ kind: 'local-cli' }, () => + browserManager.beginAgentCommand('browser-1') + ).end() + const routePopup = { ...guest, id: 147 } + registerBrowserRouteGuestPopup({ popupWebContentsId: 147, openerWebContentsId: guest.id }) + + vi.advanceTimersByTime(60_000) + browserManager.openNewTabPopup( + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: openNewTabPopup reads only the guest fields this fake copies from the tab guest. + routePopup as never, + { url: 'https://login.example.com/from-route', frameName: '', disposition: 'foreground-tab' }, + null + ) + releaseBrowserRouteGuestPopup(147) + + expect(openedTabs(rendererSendMock)).toEqual([ + [ + 'browser:open-link-in-orca-tab', + { browserPageId: 'browser-1', url: 'https://login.example.com/from-route', activate: false } + ] + ]) + }) + + it("tells the agent's page it leads it, so its print is refused and reported, until the user uses the page", () => { + const { guest, pressMouse } = attachGuest() + registerAsTab(guest) + const leads: [number, boolean][] = [] + browserManager.setAgentLeadListener((guestId, led) => leads.push([guestId, led])) + const events: unknown[] = [] + browserManager.observePageEvents('browser-1', (event) => events.push(event)) + runWithActiveRpcCaller({ kind: 'local-cli' }, () => + browserManager.beginAgentCommand('browser-1') + ).end() + runWithActiveRpcCaller({ kind: 'local-cli' }, () => + browserManager.beginAgentCommand('browser-1') + ).end() + + browserManager.reportRefusedPrint(guest.id, 'https://shop.example.com') + pressMouse() + browserManager.reportRefusedPrint(guest.id, 'https://shop.example.com') + + expect(leads).toEqual([ + [guest.id, true], + [guest.id, false] + ]) + expect(events).toEqual([{ kind: 'print', origin: 'https://shop.example.com' }]) + }) + + it('reports a refused print to an agent command on the popup page that asked', () => { + const { guest } = attachGuest() + registerAsTab(guest) + const onDidCreateWindow = guestOnMock.mock.calls.find( + ([event]) => event === 'did-create-window' + )?.[1] + onDidCreateWindow({ webContents: { ...guest, id: 146 } }) + const events: unknown[] = [] + browserManager.observePageEvents('popup-146', (event) => events.push(event)) + runWithActiveRpcCaller({ kind: 'local-cli' }, () => + browserManager.beginAgentCommand('popup-146') + ).end() + + browserManager.reportRefusedPrint(146, 'https://pay.example.com') + expect(events).toEqual([{ kind: 'print', origin: 'https://pay.example.com' }]) + }) + + it('leads an SSH route popup the agent drives directly through its opener', () => { + const { guest } = attachGuest() + registerAsTab(guest) + registerBrowserRouteGuestPopup({ popupWebContentsId: 147, openerWebContentsId: guest.id }) + runWithActiveRpcCaller({ kind: 'local-cli' }, () => + browserManager.beginAgentCommand('popup-147') + ).end() + + const led = browserManager.isAgentLedGuest(147) + releaseBrowserRouteGuestPopup(147) + expect(led).toBe(true) + }) + + it("opens the system browser for a page with no tab only on the desktop user's own click", async () => { + const { guest, openLink, pressMouse } = attachGuest() + + await withAgentInputInjection(guest as never, async () => { + pressMouse() + }) + openLink('https://docs.example.com/agent') + openLink('https://docs.example.com/scripted') + expect(browserMocks.shellOpenExternalMock).not.toHaveBeenCalled() + + pressMouse() + openLink('https://docs.example.com/user') + expect(browserMocks.shellOpenExternalMock.mock.calls).toEqual([ + ['https://docs.example.com/user'] + ]) + }) }) diff --git a/src/main/browser/guest-lifecycle/manager/registration/browser-manager-guest-cleanup.ts b/src/main/browser/guest-lifecycle/manager/registration/browser-manager-guest-cleanup.ts index bbefafd2190..0e5983c279f 100644 --- a/src/main/browser/guest-lifecycle/manager/registration/browser-manager-guest-cleanup.ts +++ b/src/main/browser/guest-lifecycle/manager/registration/browser-manager-guest-cleanup.ts @@ -16,6 +16,7 @@ export abstract class BrowserManagerGuestCleanup extends BrowserManagerGuestNavi this.policyCleanupByGuestId.delete(guestWebContentsId) } this.policyAttachedGuestIds.delete(guestWebContentsId) + this.releaseAgentLead(guestWebContentsId) this.offscreenGuestIds.delete(guestWebContentsId) this.popupOwnerContextByGuestId.delete(guestWebContentsId) this.pageInitiatedTabBudgetByRootGuestId.delete(guestWebContentsId) diff --git a/src/main/browser/guest-lifecycle/manager/registration/browser-manager-registration.ts b/src/main/browser/guest-lifecycle/manager/registration/browser-manager-registration.ts index 55906091f9f..9b911ca9d60 100644 --- a/src/main/browser/guest-lifecycle/manager/registration/browser-manager-registration.ts +++ b/src/main/browser/guest-lifecycle/manager/registration/browser-manager-registration.ts @@ -222,6 +222,7 @@ export abstract class BrowserManagerRegistration extends BrowserManagerGuestPoli this.unregisterGuest(browserTabId) } this.policyAttachedGuestIds.clear() + this.agentLedGuestIds.clear() this.offscreenGuestIds.clear() // Also clean up guests attached before registration. for (const cleanup of this.policyCleanupByGuestId.values()) { diff --git a/src/main/browser/guest-lifecycle/page-activity/browser-agent-activity.ts b/src/main/browser/guest-lifecycle/page-activity/browser-agent-activity.ts index ca3bbe1a79a..127fe332ac8 100644 --- a/src/main/browser/guest-lifecycle/page-activity/browser-agent-activity.ts +++ b/src/main/browser/guest-lifecycle/page-activity/browser-agent-activity.ts @@ -173,6 +173,10 @@ export class BrowserAgentActivity { return this.pages.has(browserPageId) } + activePageIds(): IterableIterator { + return this.pages.keys() + } + end(browserPageId: string): void { const page = this.pages.get(browserPageId) if (!page) { diff --git a/src/main/browser/guest-lifecycle/page-activity/browser-page-events.ts b/src/main/browser/guest-lifecycle/page-activity/browser-page-events.ts index 42816202e35..5440d83d4bb 100644 --- a/src/main/browser/guest-lifecycle/page-activity/browser-page-events.ts +++ b/src/main/browser/guest-lifecycle/page-activity/browser-page-events.ts @@ -1,7 +1,7 @@ import { EventEmitter } from 'node:events' import type { BrowserPopupEvent } from '../../../../shared/browser/guest-lifecycle/browser-guest-events' -/** Facts the browser manager resolves to their owning page: a tab it asked for, a popup outcome, a download. */ +/** Facts the browser manager resolves to their owning page: a tab it asked for, a popup outcome, a download, a refused print. */ export type BrowserPageEvent = | { kind: 'tab'; url: string } | { @@ -10,6 +10,7 @@ export type BrowserPageEvent = origin: string } | { kind: 'download'; filename: string; origin: string } + | { kind: 'print'; origin: string } /** Keyed by browser page id. */ export class BrowserPageEvents { diff --git a/src/main/browser/guest-lifecycle/prompts/browser-system-dialogs.ts b/src/main/browser/guest-lifecycle/prompts/browser-system-dialogs.ts new file mode 100644 index 00000000000..20ea95aa742 --- /dev/null +++ b/src/main/browser/guest-lifecycle/prompts/browser-system-dialogs.ts @@ -0,0 +1,74 @@ +import { ipcMain, webContents, type Session } from 'electron' +import { + BROWSER_AGENT_LEAD_CHANNEL, + BROWSER_AGENT_LEAD_QUERY_CHANNEL, + BROWSER_PRINT_REFUSED_CHANNEL, + BROWSER_SYSTEM_DIALOGS_PRELOAD_FILE +} from '../../../../shared/browser-system-dialogs' +import { + registerBrowserSessionFramePreload, + unregisterBrowserSessionFramePreload +} from '../../sessions/browser-session-frame-preloads' +import { + listBrowserRouteGuestPopups, + resolveBrowserRouteGuestPopupOpener +} from '../../routing/guest-authority/browser-route-guest-popup-ownership' +import { browserManager } from '../manager/browser-manager' +import { cancelBrowserPasskeyRequests } from '../../webauthn/browser-passkey-requests' + +const PRELOAD_ID = 'orca-browser-system-dialogs' + +/** Watches the session's pages for passkey requests and print dialogs, which Electron doesn't report. */ +export function watchBrowserSystemDialogs(browserSession: Session): void { + if ( + !registerBrowserSessionFramePreload( + browserSession, + PRELOAD_ID, + BROWSER_SYSTEM_DIALOGS_PRELOAD_FILE + ) + ) { + console.warn('[browser] System dialog preload is missing') + } +} + +export function unwatchBrowserSystemDialogs(browserSession: Session): void { + unregisterBrowserSessionFramePreload(browserSession, PRELOAD_ID) +} + +/** Keeps every watched page's frames told whether the agent leads it; once per app. */ +export function listenForBrowserSystemDialogs(): void { + ipcMain.on(BROWSER_AGENT_LEAD_QUERY_CHANNEL, (event) => { + event.reply(BROWSER_AGENT_LEAD_CHANNEL, browserManager.isAgentLedGuest(event.sender.id)) + }) + ipcMain.on(BROWSER_PRINT_REFUSED_CHANNEL, (event) => { + browserManager.reportRefusedPrint(event.sender.id, event.senderFrame?.origin ?? '') + }) + browserManager.setAgentLeadListener((guestWebContentsId, led) => { + // SSH route popups follow their opener's lead. + const routePopups = listBrowserRouteGuestPopups().filter( + (popupId) => resolveBrowserRouteGuestPopupOpener(popupId) === guestWebContentsId + ) + for (const id of [guestWebContentsId, ...routePopups]) { + publishAgentLead(id, led) + // A passkey request held from Windows Hello for the agent can't resume; cancelling it lets + // the page offer its own retry to the user who just came back. + if (!led && process.platform === 'win32') { + cancelBrowserPasskeyRequests(id) + } + } + }) +} + +function publishAgentLead(guestWebContentsId: number, led: boolean): void { + try { + const guest = webContents.fromId(guestWebContentsId) + if (!guest || guest.isDestroyed()) { + return + } + for (const frame of guest.mainFrame.framesInSubtree) { + frame.send(BROWSER_AGENT_LEAD_CHANNEL, led) + } + } catch { + // Best-effort: a guest or frame torn down mid-walk has no dialog left to guard. + } +} diff --git a/src/main/browser/routing/guest-authority/browser-route-guest-popups.ts b/src/main/browser/routing/guest-authority/browser-route-guest-popups.ts index ebe0199e4bd..aed157e867c 100644 --- a/src/main/browser/routing/guest-authority/browser-route-guest-popups.ts +++ b/src/main/browser/routing/guest-authority/browser-route-guest-popups.ts @@ -38,6 +38,8 @@ export type BrowserRouteGuestPopupDependencies = { request: NewBrowserTabPopupRequest, observed: ObservedBrowserGuestGesture | null ) => boolean + /** The desktop user's own input in a popup takes its opener's page back from the agent. */ + releaseAgentLead?: (openerWebContentsId: number) => void openPopupWindow?: ( options: PopupChildWindowOptions, targetUrl: string, @@ -105,7 +107,18 @@ export function createBrowserRouteGuestPopupController(input: { if (!enforceBrowserRouteWebRtcPolicy(contents, () => {})) { return false } - const gesture = trackBrowserGuestInputGesture(contents) + const gesture = trackBrowserGuestInputGesture(contents, { + onActivatingInput: (byAgent) => { + if (byAgent) { + return + } + try { + input.dependencies.releaseAgentLead?.(input.opener.id) + } catch { + // A destroyed opener has no lead left to release. + } + } + }) gestures.add(gesture) try { contents.setWindowOpenHandler(buildWindowOpenHandler(contents, gesture)) diff --git a/src/main/browser/routing/guest-authority/browser-route-webcontents-registry-dependencies.ts b/src/main/browser/routing/guest-authority/browser-route-webcontents-registry-dependencies.ts index c0c3be335b0..80481d6ea6b 100644 --- a/src/main/browser/routing/guest-authority/browser-route-webcontents-registry-dependencies.ts +++ b/src/main/browser/routing/guest-authority/browser-route-webcontents-registry-dependencies.ts @@ -18,5 +18,6 @@ export type BrowserRouteWebContentsRegistryDependencies = { /** Route-guest popup denials need an explicit notice on the owning page. */ reportBlockedPopup?(input: { openerWebContentsId: number; url: string }): void openNewTabPopup?: BrowserRouteGuestPopupDependencies['openNewTabPopup'] + releaseAgentLead?: BrowserRouteGuestPopupDependencies['releaseAgentLead'] maxGuests?: number } diff --git a/src/main/browser/routing/guest-authority/browser-route-webcontents-registry.ts b/src/main/browser/routing/guest-authority/browser-route-webcontents-registry.ts index d83e2dbcb6b..8ddbed35c6b 100644 --- a/src/main/browser/routing/guest-authority/browser-route-webcontents-registry.ts +++ b/src/main/browser/routing/guest-authority/browser-route-webcontents-registry.ts @@ -76,6 +76,7 @@ export class BrowserRouteWebContentsRegistry { this.dependencies.getPartitionForSession(routeSession), reportBlockedPopup: (blocked) => this.dependencies.reportBlockedPopup?.(blocked), openNewTabPopup: this.dependencies.openNewTabPopup, + releaseAgentLead: this.dependencies.releaseAgentLead, isPopupNavigationAllowed: (url) => this.navigationAllowed(state, url, false) }) } catch { diff --git a/src/main/browser/routing/input-ownership/browser-page-input-credit.test.ts b/src/main/browser/routing/input-ownership/browser-page-input-credit.test.ts index 58db3ac263a..0eea461c0c4 100644 --- a/src/main/browser/routing/input-ownership/browser-page-input-credit.test.ts +++ b/src/main/browser/routing/input-ownership/browser-page-input-credit.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest' import { BrowserPageInputCredit } from './browser-page-input-credit' -import type { LOCAL_CLI_RPC_CALLER } from '../../../runtime/rpc/callers/rpc-caller-identity' +import type { RpcCallerIdentity } from '../../../runtime/rpc/callers/rpc-caller-identity' const desktop = { kind: 'paired-device', deviceId: 'desk-1' } as const @@ -8,7 +8,7 @@ function setup() { let now = 1_000 const credit = new BrowserPageInputCredit(() => now) // A command that delivers activating input to the page while it runs, as a click does. - const click = (caller: typeof desktop | typeof LOCAL_CLI_RPC_CALLER, page = 'page-1') => + const click = (caller: RpcCallerIdentity, page = 'page-1') => credit.during(page, caller, async () => credit.creditInput('tab-1', page)) return { credit, click, advance: (ms: number) => (now += ms) } } @@ -22,4 +22,11 @@ describe('BrowserPageInputCredit', () => { expect(credit.consume('tab-1')).toBe('desk-1') expect(credit.consume('tab-1')).toBeNull() }) + + it("credits no one for an agent CLI's click over the desktop's pairing", async () => { + const { credit, click } = setup() + await click({ ...desktop, agent: true }) + + expect(credit.consume('tab-1')).toBeNull() + }) }) diff --git a/src/main/browser/routing/input-ownership/browser-page-input-credit.ts b/src/main/browser/routing/input-ownership/browser-page-input-credit.ts index 39ff41c4958..a3361ade910 100644 --- a/src/main/browser/routing/input-ownership/browser-page-input-credit.ts +++ b/src/main/browser/routing/input-ownership/browser-page-input-credit.ts @@ -1,5 +1,8 @@ import { isRecentBrowserGuestInput } from '../../guest-lifecycle/input/browser-guest-input-gesture' -import type { RpcCallerIdentity } from '../../../runtime/rpc/callers/rpc-caller-identity' +import { + isAgentRpcCaller, + type RpcCallerIdentity +} from '../../../runtime/rpc/callers/rpc-caller-identity' type Runner = { pairedDeviceId: string | null } @@ -23,7 +26,9 @@ export class BrowserPageInputCredit { execute: () => Promise ): Promise { const runner: Runner = { - pairedDeviceId: caller?.kind === 'paired-device' ? caller.deviceId : null + // An agent's CLI moves no one's view, even when it calls with a device's pairing. + pairedDeviceId: + caller?.kind === 'paired-device' && !isAgentRpcCaller(caller) ? caller.deviceId : null } this.runnerByPageId.set(browserPageId, runner) try { diff --git a/src/main/browser/routing/sessions/browser-route-session-runtime.ts b/src/main/browser/routing/sessions/browser-route-session-runtime.ts index 34b376641a3..0204847b2ce 100644 --- a/src/main/browser/routing/sessions/browser-route-session-runtime.ts +++ b/src/main/browser/routing/sessions/browser-route-session-runtime.ts @@ -90,7 +90,8 @@ export const browserRouteWebContentsRegistry = new BrowserRouteWebContentsRegist browserRouteSessionRegistry.retirePreparedPagesOwnedByRenderer(rendererWebContentsId), reportBlockedPopup: (blocked) => browserManager.reportRouteGuestPopupBlocked(blocked), openNewTabPopup: (source, request, observed) => - browserManager.openNewTabPopup(source, request, observed) + browserManager.openNewTabPopup(source, request, observed), + releaseAgentLead: (openerWebContentsId) => browserManager.releaseAgentLead(openerWebContentsId) }) routeWebContentsRegistryRef.current = browserRouteWebContentsRegistry diff --git a/src/main/browser/sessions/browser-session-startup.ts b/src/main/browser/sessions/browser-session-startup.ts index 340c12c3f26..b766bcba1a5 100644 --- a/src/main/browser/sessions/browser-session-startup.ts +++ b/src/main/browser/sessions/browser-session-startup.ts @@ -10,6 +10,7 @@ import { configureBrowserExtensionProfileDataDirectory } from '../extensions/pro import { configurePairedRuntimeBrowserClientHostsForOrcaProfile } from '../client-hosts/lifecycle/paired-runtime-browser-client-host-runtime' import { configureBrowserPasswordVaults } from '../../browser-passwords/browser-password-vaults' import { listenForBrowserPasskeyRequests } from '../webauthn/browser-passkey-requests' +import { listenForBrowserSystemDialogs } from '../guest-lifecycle/prompts/browser-system-dialogs' let initialized = false @@ -22,6 +23,7 @@ export function initializeBrowserSessionsForApp( return } listenForBrowserPasskeyRequests() + listenForBrowserSystemDialogs() if (activeProfile) { browserSessionRegistry.configureForOrcaProfile(activeProfile) diff --git a/src/main/browser/sessions/profiles/browser-session-profile-retirement.ts b/src/main/browser/sessions/profiles/browser-session-profile-retirement.ts index 2af4dabd7ab..52cdc147b98 100644 --- a/src/main/browser/sessions/profiles/browser-session-profile-retirement.ts +++ b/src/main/browser/sessions/profiles/browser-session-profile-retirement.ts @@ -42,7 +42,12 @@ export async function retireDeletedBrowserSessionProfile( } async function clearDeletedBrowserSessionProfile(profile: BrowserSessionProfile): Promise { - await detachBrowserExtensionsFromProfileSession(profile) + try { + await detachBrowserExtensionsFromProfileSession(profile) + } catch (error) { + // Why: the profile is already deleted, so a teardown throw must not skip the storage wipe. + console.warn('[browser-session] Failed to unload extensions for deleted profile', error) + } // Why: clear the partition's storage so deleting a profile doesn't leave orphaned cookies/cache behind. try { const sess = session.fromPartition(profile.partition) diff --git a/src/main/browser/sessions/profiles/browser-session-registry.test.ts b/src/main/browser/sessions/profiles/browser-session-registry.test.ts index eabc72c18b7..c1eba2c7d40 100644 --- a/src/main/browser/sessions/profiles/browser-session-registry.test.ts +++ b/src/main/browser/sessions/profiles/browser-session-registry.test.ts @@ -15,6 +15,7 @@ const { removeCertificateRequestGuardMock: vi.fn() })) const forgetBrowserPasswordVaultMock = vi.hoisted(() => vi.fn(() => true)) +const detachBrowserExtensionsFromProfileSessionMock = vi.hoisted(() => vi.fn(async () => {})) const processUserAgentMode = vi.hoisted(() => { const state: { value: 'clean' | 'native' } = { value: 'clean' } return state @@ -33,7 +34,7 @@ vi.mock('electron', () => ({ // Extension loading and cleanup have their own tests (browser-extension-session-loader.test.ts). vi.mock('../../extensions/profile-sessions/browser-extension-session-loader', () => ({ attachBrowserExtensionsToProfileSession: () => {}, - detachBrowserExtensionsFromProfileSession: async () => {} + detachBrowserExtensionsFromProfileSession: detachBrowserExtensionsFromProfileSessionMock })) vi.mock('../../identity/browser-process-user-agent', () => ({ @@ -83,6 +84,8 @@ describe('BrowserSessionRegistry', () => { profileDirectory }) sessionFromPartitionMock.mockReset() + detachBrowserExtensionsFromProfileSessionMock.mockReset() + detachBrowserExtensionsFromProfileSessionMock.mockResolvedValue(undefined) askForMediaAccessMock.mockReset() getMediaAccessStatusMock.mockReset() removeCertificateRequestGuardMock.mockClear() @@ -227,4 +230,19 @@ describe('BrowserSessionRegistry', () => { const recreated = await ensureManagedBrowserProfile(owner, 'Workspace: feature') expect(recreated?.partition).not.toBe(first.partition) }) + + it('wipes the deleted profile when extension teardown throws', async () => { + detachBrowserExtensionsFromProfileSessionMock.mockRejectedValueOnce( + new Error('Failed to unhandle protocol: orca-extension-api') + ) + const owner = { kind: 'worktree' as const, id: 'repo-1::/work/feature' } + const profile = await ensureManagedBrowserProfile(owner, 'Workspace: feature') + if (!profile) { + throw new Error('Expected managed profile') + } + const sess = sessionFromPartitionMock() + await expect(deleteManagedBrowserProfile(owner)).resolves.toBe(true) + expect(sess.clearStorageData).toHaveBeenCalledTimes(1) + expect(browserSessionRegistry.getProfile(profile.id)).toBeNull() + }) }) diff --git a/src/main/browser/webauthn/browser-passkey-requests.ts b/src/main/browser/webauthn/browser-passkey-requests.ts index 27ba943e878..d4bad4d3e1d 100644 --- a/src/main/browser/webauthn/browser-passkey-requests.ts +++ b/src/main/browser/webauthn/browser-passkey-requests.ts @@ -1,23 +1,15 @@ import { ipcMain, type IpcMainEvent, - type Session, type WebContents, type WebFrameMain, webFrameMain } from 'electron' import { BROWSER_PASSKEY_REQUEST_CANCEL_CHANNEL, - BROWSER_PASSKEY_REQUEST_PENDING_CHANNEL, - BROWSER_PASSKEY_REQUEST_PRELOAD_FILE + BROWSER_PASSKEY_REQUEST_PENDING_CHANNEL } from '../../../shared/browser-passkey-request' import type { BrowserPasskeyRequest } from '../../../shared/browser/runtime/runtime-browser-contracts' -import { - registerBrowserSessionFramePreload, - unregisterBrowserSessionFramePreload -} from '../sessions/browser-session-frame-preloads' - -const PRELOAD_ID = 'orca-browser-passkey-request' // Frames waiting on a passkey request, by guest WebContents id and frame tree node. const pendingFramesByGuest = new Map>() @@ -80,23 +72,6 @@ export function listenForBrowserPasskeyRequests(): void { ipcMain.on(BROWSER_PASSKEY_REQUEST_PENDING_CHANNEL, onPendingChanged) } -/** Watches the session's pages for passkey requests, which Electron neither shows nor reports. */ -export function watchBrowserPasskeyRequests(browserSession: Session): void { - if ( - !registerBrowserSessionFramePreload( - browserSession, - PRELOAD_ID, - BROWSER_PASSKEY_REQUEST_PRELOAD_FILE - ) - ) { - console.warn('[browser-webauthn] Passkey request preload is missing') - } -} - -export function unwatchBrowserPasskeyRequests(browserSession: Session): void { - unregisterBrowserSessionFramePreload(browserSession, PRELOAD_ID) -} - function livePendingFrames(webContentsId: number): WebFrameMain[] { const frames = pendingFramesByGuest.get(webContentsId) if (!frames) { diff --git a/src/main/browser/webauthn/browser-webauthn-access.ts b/src/main/browser/webauthn/browser-webauthn-access.ts index 00f02ca7faa..8befcbcdb42 100644 --- a/src/main/browser/webauthn/browser-webauthn-access.ts +++ b/src/main/browser/webauthn/browser-webauthn-access.ts @@ -4,9 +4,9 @@ import { requestBrowserWebAuthnAccount } from './browser-webauthn-account-picker' import { - unwatchBrowserPasskeyRequests, - watchBrowserPasskeyRequests -} from './browser-passkey-requests' + unwatchBrowserSystemDialogs, + watchBrowserSystemDialogs +} from '../guest-lifecycle/prompts/browser-system-dialogs' const FIDO_HID_USAGE_PAGE = 0xf1d0 const LOCALHOST_HOSTNAMES = new Set(['localhost', '127.0.0.1', '::1', '[::1]']) @@ -104,7 +104,7 @@ export function installBrowserWebAuthnAccessHandlers(browserSession: Session): v handleBrowserSelectWebAuthnAccount(browserSession, event, details, callback) selectWebAuthnAccountHandlers.set(browserSession, selectWebAuthnAccountHandler) browserSession.on('select-webauthn-account', selectWebAuthnAccountHandler) - watchBrowserPasskeyRequests(browserSession) + watchBrowserSystemDialogs(browserSession) } export function clearBrowserWebAuthnAccessHandlers(browserSession: Session): void { @@ -116,5 +116,5 @@ export function clearBrowserWebAuthnAccessHandlers(browserSession: Session): voi } browserSession.setDevicePermissionHandler(null) cancelBrowserWebAuthnAccountRequestsForSession(browserSession) - unwatchBrowserPasskeyRequests(browserSession) + unwatchBrowserSystemDialogs(browserSession) } diff --git a/src/main/claude/model-catalog/claude-model-catalog-observation.ts b/src/main/claude/model-catalog/claude-model-catalog-observation.ts new file mode 100644 index 00000000000..68032bdf3d2 --- /dev/null +++ b/src/main/claude/model-catalog/claude-model-catalog-observation.ts @@ -0,0 +1,43 @@ +import type { AgentSessionAccountKind } from '../../../shared/agent-session/agent-session-availability' +import type { AgentModelCatalogSuccess } from '../../native-chat/agent-model-catalog/agent-model-catalog-entry' +import { + isCompleteClaudeModelListing, + listedModels, + record +} from '../structured/launch/claude-structured-model-catalog' +import { claudeCatalogSignedOut } from './claude-model-catalog-signed-out' +import { + claudeFastModeSupport, + wireClaudeModels +} from '../structured/launch/claude-structured-catalog-listing' +import { readClaudeSettingsApplied } from '../structured/launch/claude-structured-session-options' + +export function readClaudeCatalogObservation( + replies: ReadonlyMap, + env: Readonly>, + account: AgentSessionAccountKind +): AgentModelCatalogSuccess { + const listing = replies.get('orca-model-discovery') + const models = listedModels(listing) + const settings = replies.get('orca-model-settings') + const applied = record(record(settings)?.applied) + const configured = + applied && + ['model', 'effort'].every((key) => applied[key] == null || typeof applied[key] === 'string') + ? (readClaudeSettingsApplied(settings) ?? undefined) + : undefined + if (models.length === 0) { + throw new Error('claude listed no models') + } + const fastModeSupport = claudeFastModeSupport(models) + return { + models: wireClaudeModels(models), + origin: 'probe', + complete: isCompleteClaudeModelListing(listing) && configured !== undefined, + ...(fastModeSupport ? { fastModeSupport } : {}), + ...(configured ? { configured } : {}), + ...(claudeCatalogSignedOut(replies.get('orca-signin-discovery'), settings, env) + ? { unavailable: { reason: 'notSignedIn', account } } + : {}) + } +} diff --git a/src/main/claude/model-catalog/claude-model-catalog-probe.test.ts b/src/main/claude/model-catalog/claude-model-catalog-probe.test.ts index 667762251d4..4943768337e 100644 --- a/src/main/claude/model-catalog/claude-model-catalog-probe.test.ts +++ b/src/main/claude/model-catalog/claude-model-catalog-probe.test.ts @@ -1,3 +1,4 @@ +import type { AgentModelCatalogSuccess } from '../../native-chat/agent-model-catalog/agent-model-catalog-entry' import { mkdtempSync, rmSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -47,3 +48,71 @@ describe('Claude catalog availability', () => { } }) }) + +it('publishes models before settings and preserves a late sign-in response', async () => { + const settings = Promise.withResolvers() + const signin = Promise.withResolvers() + const inventory = Promise.withResolvers() + const observations: { + success: AgentModelCatalogSuccess + accountKnown?: boolean + }[] = [] + const response = (id: string, value: unknown) => + `${JSON.stringify({ + type: 'control_response', + response: { subtype: 'success', request_id: id, response: value } + })}\n` + const probe = createClaudeModelCatalogProbe({ + ...probeDeps(), + resolveEnv: () => ({}), + runListing: async (launch, options) => { + expect( + launch.stdin + ?.split(String.fromCharCode(10)) + .filter(Boolean) + .map((line) => JSON.parse(line).request_id) + ).toEqual(['orca-model-discovery', 'orca-model-settings', 'orca-signin-discovery']) + let stdout = response('orca-model-discovery', { + models: [{ value: 'opus', displayName: 'Opus', supportsEffort: false }] + }) + options.onOutput?.(stdout.slice(0, 17)) + options.onOutput?.(stdout.slice(17)) + inventory.resolve() + await settings.promise + const settingsReply = response('orca-model-settings', { + applied: { model: 'opus' }, + effective: {} + }) + stdout += settingsReply + options.onOutput?.(settingsReply) + await signin.promise + const signinReply = response('orca-signin-discovery', { + account: { tokenSource: 'none', apiProvider: 'firstParty' } + }) + stdout += signinReply + options.onOutput?.(signinReply) + return stdout + } + }) + const done = probe(HOME('/homes/a'), { + publish: (success, accountKnown) => observations.push({ success, accountKnown }) + }) + await inventory.promise + expect(observations[0]).toMatchObject({ + success: { models: [{ id: 'opus' }], complete: false }, + accountKnown: false + }) + settings.resolve() + await Promise.resolve() + signin.resolve() + expect(await done).toMatchObject({ + models: [{ id: 'opus' }], + configured: { model: 'opus' }, + unavailable: { reason: 'notSignedIn' } + }) + expect(observations.at(-1)).toMatchObject({ + accountKnown: true, + success: { unavailable: { reason: 'notSignedIn' } } + }) + expect(observations[0].success.configured).toBeUndefined() +}) diff --git a/src/main/claude/model-catalog/claude-model-catalog-probe.ts b/src/main/claude/model-catalog/claude-model-catalog-probe.ts index fee0027363d..c885cf01b5f 100644 --- a/src/main/claude/model-catalog/claude-model-catalog-probe.ts +++ b/src/main/claude/model-catalog/claude-model-catalog-probe.ts @@ -1,3 +1,5 @@ +import { join } from 'node:path' +import { agentModelCatalogInputStamp } from '../../native-chat/agent-model-catalog/agent-model-catalog-input-stamp' import { withOrcaClaudeChatEntrypoint } from '../../../shared/agent-launch/startup/claude-chat-entrypoint' import { waitForPromiseWithSignal } from '../../../shared/abort-signal-reason' import { @@ -10,17 +12,8 @@ import { } from '../../../shared/claude-model-list-probe' import { requireLegacyAgentSessionAccountHome } from '../../../shared/agent-session/lifecycle/agent-session-account-home' import { runClaudeBackgroundAttempts } from '../../../shared/claude-background-run-isolation' -import { - isCompleteClaudeModelListing, - listedModels, - record -} from '../structured/launch/claude-structured-model-catalog' -import { claudeCatalogSignedOut } from './claude-model-catalog-signed-out' -import { - claudeFastModeSupport, - wireClaudeModels -} from '../structured/launch/claude-structured-catalog-listing' -import { readClaudeSettingsApplied } from '../structured/launch/claude-structured-session-options' +import { listedModels, record } from '../structured/launch/claude-structured-model-catalog' +import { readClaudeCatalogObservation } from './claude-model-catalog-observation' import { readClaudeControlResponses } from '../sdk-transport/claude-control-responses' import { claudeStructuredLaunchArgs } from '../structured/launch/claude-structured-launch-args' import { AgentModelCatalogUnavailableError } from '../../native-chat/agent-model-catalog/agent-model-catalog-unavailable' @@ -39,6 +32,7 @@ import { type ClaudeEnvDeps } from '../structured/launch/claude-structured-child-env' import type { + AgentModelCatalogProgress, AgentModelCatalogProbe, AgentModelCatalogLookup } from '../../native-chat/agent-model-catalog/agent-model-catalog-discovery' @@ -147,7 +141,9 @@ export function createClaudeModelCatalogProbe( const launchDeps = { ...deps, operation, - ...(lookup === 'saved' ? { resolveInheritedEnv: deps.resolveSavedEnvironment } : {}), + ...(lookup !== 'recover' + ? { resolveInheritedEnv: deps.resolveSavedEnvironment ?? deps.resolveInheritedEnv } + : {}), ...(lookup === 'recover' ? {} : { refreshEnvironmentAfterMiss: undefined }), ...(launchDirectory ? { resolveWorkingDirectory: () => launchDirectory } : {}) } @@ -175,6 +171,13 @@ export function createClaudeModelCatalogProbe( const settingSources = listIn === cwd ? CLAUDE_DEFAULT_SETTING_SOURCES : ['user'] const args = argsFor(true, settingSources) return { + inputStamp: await agentModelCatalogInputStamp({ + command, + prefixArgs, + cwd: listIn ?? process.cwd(), + env: launchDirectory ? envWithPathRelativeTo(env, launchDirectory) : env, + configFiles: [join(requireLegacyAgentSessionAccountHome(accountHome).path, 'settings.json')] + }), identity: { command, prefixArgs, @@ -184,7 +187,9 @@ export function createClaudeModelCatalogProbe( }, run: async ( signal?: AbortSignal, - runOperation = operation + runOperation = operation, + _selectedModels?: readonly string[], + publish?: AgentModelCatalogProgress ): Promise => { assertStructuredAgentResolutionCurrent(runOperation) signal?.throwIfAborted() @@ -193,6 +198,14 @@ export function createClaudeModelCatalogProbe( run: (strictMcp) => { assertStructuredAgentResolutionCurrent(runOperation) signal?.throwIfAborted() + const requests = [ + 'orca-model-discovery', + 'orca-model-settings', + 'orca-signin-discovery' + ] + const settled = new Set() + const replies = new Map() + let incomplete: string[] = [] return (deps.runListing ?? runAgentModelCatalogListing)( { command, @@ -208,16 +221,37 @@ export function createClaudeModelCatalogProbe( process.platform ), signal, - partialOutput: { - timeoutMs: 2_000, - ready: (stdout) => { - const replies = readClaudeControlResponses(stdout) - return ( - listedModels(replies.get('orca-model-discovery')).length > 0 && - replies.has('orca-model-settings') + timeoutMs: null, + onOutput: (chunk) => { + const lines = chunk.split('\n') + incomplete.push(lines[0]) + if (lines.length === 1) { + return + } + const stdout = [incomplete.join(''), ...lines.slice(1, -1)].join('\n') + incomplete = [lines.at(-1)!] + const previous = settled.size + for (const [id, value] of readClaudeControlResponses(stdout, (id) => { + if (requests.includes(id)) { + settled.add(id) + } + })) { + if (requests.includes(id)) { + replies.set(id, value) + } + } + if ( + settled.size > previous && + listedModels(replies.get('orca-model-discovery')).length > 0 + ) { + publish?.( + readClaudeCatalogObservation(replies, env, account), + record(record(replies.get('orca-signin-discovery'))?.account) !== null && + record(record(replies.get('orca-model-settings'))?.effective) !== null ) } - } + }, + completeOutput: () => requests.every((id) => settled.has(id)) } ).catch((error: unknown) => { if (error instanceof AgentModelCatalogProbeError) { @@ -237,33 +271,7 @@ export function createClaudeModelCatalogProbe( } throw outcome } - const stdout = outcome - const replies = readClaudeControlResponses(stdout) - const listing = replies.get('orca-model-discovery') - const models = listedModels(listing) - const settings = replies.get('orca-model-settings') - const applied = record(record(settings)?.applied) - const configured = - applied && - ['model', 'effort'].every( - (key) => applied[key] == null || typeof applied[key] === 'string' - ) - ? (readClaudeSettingsApplied(settings) ?? undefined) - : undefined - if (models.length === 0) { - throw new Error('claude listed no models') - } - const fastModeSupport = claudeFastModeSupport(models) - return { - models: wireClaudeModels(models), - origin: 'probe', - complete: isCompleteClaudeModelListing(listing) && configured !== undefined, - ...(fastModeSupport ? { fastModeSupport } : {}), - ...(configured ? { configured } : {}), - ...(claudeCatalogSignedOut(replies.get('orca-signin-discovery'), settings, env) - ? { unavailable: { reason: 'notSignedIn', account } } - : {}) - } + return readClaudeCatalogObservation(readClaudeControlResponses(outcome), env, account) } } } @@ -279,7 +287,10 @@ export function createClaudeModelCatalogProbe( 'recover', options?.operation ) - ).run(options?.signal), - { resolve, resolveLegacy: (home, operation) => resolve(home, undefined, 'recover', operation) } + ).run(options?.signal, options?.operation, options?.selectedModels, options?.publish), + { + resolve, + resolveLegacy: (home, operation, lookup) => resolve(home, undefined, lookup, operation) + } ) } diff --git a/src/main/claude/sdk-transport/claude-control-responses.ts b/src/main/claude/sdk-transport/claude-control-responses.ts index df1f7ab318c..4c5818b6f1d 100644 --- a/src/main/claude/sdk-transport/claude-control-responses.ts +++ b/src/main/claude/sdk-transport/claude-control-responses.ts @@ -6,7 +6,10 @@ function isRecord(value: unknown): value is Record { return typeof value === 'object' && value !== null && !Array.isArray(value) } -export function readClaudeControlResponses(stdout: string): Map { +export function readClaudeControlResponses( + stdout: string, + settled?: (requestId: string) => void +): Map { const responses = new Map() for (const line of stdout.split(/\r?\n/)) { if (!line.trimStart().startsWith('{') || !line.includes('request_id')) { @@ -19,6 +22,12 @@ export function readClaudeControlResponses(stdout: string): Map continue } const response = frame.response + if ( + (response.subtype === 'success' || response.subtype === 'error') && + typeof response.request_id === 'string' + ) { + settled?.(response.request_id) + } if (response.subtype === 'success' && typeof response.request_id === 'string') { responses.set(response.request_id, response.response) } diff --git a/src/main/codex/app-server/codex-app-server-session.ts b/src/main/codex/app-server/codex-app-server-session.ts index 29668ce02cb..03579f0a2b1 100644 --- a/src/main/codex/app-server/codex-app-server-session.ts +++ b/src/main/codex/app-server/codex-app-server-session.ts @@ -25,7 +25,7 @@ export type CodexAppServerInvocation = { env?: Record /** Keys stripped after the overlay, e.g. CODEX_HOME when targeting the real ~/.codex. */ envToDelete?: readonly string[] - timeoutMs: number + timeoutMs: number | null signal?: AbortSignal experimentalApi?: boolean /** Kill unsupervised plugin trees before stdin EOF; root exit leaves children behind and blocks tree killing. */ @@ -186,15 +186,18 @@ export async function runCodexAppServerSession( failPending(error) rejectDeadline(error) } - const deadline = setTimeout( - () => - endSession( - new CodexAppServerTimeoutError( - `codex app-server session exceeded ${invocation.timeoutMs}ms (${invocation.command})` + const deadline = + invocation.timeoutMs === null + ? undefined + : setTimeout( + () => + endSession( + new CodexAppServerTimeoutError( + `codex app-server session exceeded ${invocation.timeoutMs}ms (${invocation.command})` + ) + ), + invocation.timeoutMs ) - ), - invocation.timeoutMs - ) const onAbort = (): void => endSession(new Error('codex app-server session stopped')) if (invocation.signal?.aborted) { onAbort() diff --git a/src/main/codex/configuration/settings/codex-config-mirror-merge.ts b/src/main/codex/configuration/settings/codex-config-mirror-merge.ts index 1cd49e1803e..dbdac8c309b 100644 --- a/src/main/codex/configuration/settings/codex-config-mirror-merge.ts +++ b/src/main/codex/configuration/settings/codex-config-mirror-merge.ts @@ -24,22 +24,22 @@ export function mergeSystemCodexConfigIntoRuntime( const runtimeSections = deduplicateProjectTomlSections(getTomlSections(runtimeConfig)) const runtimeProjectHeaders = new Set( runtimeSections - .filter((section) => isRuntimeProjectTomlSection(section.header)) - .map((section) => getTomlSectionHeaderKey(section.header)) + .filter((section) => isRuntimeProjectTomlSection(section)) + .map((section) => getTomlSectionHeaderKey(section)) ) const systemProjectSections = deduplicateProjectTomlSections( getTomlSections(systemConfig) - ).filter((section) => isRuntimeProjectTomlSection(section.header)) + ).filter((section) => isRuntimeProjectTomlSection(section)) const systemUntrustedProjectHeaders = new Set( systemProjectSections .filter((section) => getProjectTrustLevel(section.block) === 'untrusted') - .map((section) => getRevocationTomlSectionHeaderKey(section.header)) + .map((section) => getRevocationTomlSectionHeaderKey(section)) ) // Exact-cased re-grants must override legacy case-drifted revocations or each mirror reverts trust. const systemTrustedProjectHeaders = new Set( systemProjectSections .filter((section) => getProjectTrustLevel(section.block) === 'trusted') - .map((section) => getTomlSectionHeaderKey(section.header)) + .map((section) => getTomlSectionHeaderKey(section)) ) const systemMcpServers = readMcpServerTomlOwnership(systemConfig) // Mirror ordinary settings while retaining runtime trust, subject to explicit system project revocations. @@ -50,10 +50,10 @@ export function mergeSystemCodexConfigIntoRuntime( }), ...runtimeSections .filter((section) => { - if (isRuntimePreservedTomlSection(section.header)) { + if (isRuntimePreservedTomlSection(section)) { return true } - const mcpServerName = getMcpServerTomlSectionName(section.header) + const mcpServerName = getMcpServerTomlSectionName(section) return ( mcpServerName !== null && !systemMcpServers.ownsRoot && @@ -64,9 +64,9 @@ export function mergeSystemCodexConfigIntoRuntime( }) .filter( (section) => - !isRuntimeProjectTomlSection(section.header) || - !systemUntrustedProjectHeaders.has(getRevocationTomlSectionHeaderKey(section.header)) || - systemTrustedProjectHeaders.has(getTomlSectionHeaderKey(section.header)) + !isRuntimeProjectTomlSection(section) || + !systemUntrustedProjectHeaders.has(getRevocationTomlSectionHeaderKey(section)) || + systemTrustedProjectHeaders.has(getTomlSectionHeaderKey(section)) ) .map((section) => section.block) ]) diff --git a/src/main/codex/configuration/toml/config-toml-runtime-owned-sections.ts b/src/main/codex/configuration/toml/config-toml-runtime-owned-sections.ts index bb1e4777070..9549b4187d4 100644 --- a/src/main/codex/configuration/toml/config-toml-runtime-owned-sections.ts +++ b/src/main/codex/configuration/toml/config-toml-runtime-owned-sections.ts @@ -5,13 +5,9 @@ import { isTomlStructuralLine, updateTomlLineScanState } from './config-toml-line-scan' -import { parseTomlTableHeaderPath } from './config-toml-key-path' +import { parseTomlTableHeaderPath, type ParsedTomlTableHeaderPath } from './config-toml-key-path' import { findProjectTrustLevelEntries } from '../../trust/toml/config-toml-project-trust-level' -import { - CODEX_HOOK_TRUST_KEY, - parseHookStateTomlHeaderKey, - parseProjectTomlHeaderPath as parseCodexProjectHeaderPath -} from './config-toml-syntax' +import { CODEX_HOOK_TRUST_KEY } from './config-toml-syntax' import '../../trust/toml/config-toml-trust' import { codexTrustSourcePathsEqual as codexHookSourcePathsEqual, @@ -24,6 +20,7 @@ export type TomlSection = { header: string block: string start: number + table: ParsedTomlTableHeaderPath | null } export type SharedHookTrustCarry = { @@ -48,14 +45,14 @@ export function stripRuntimeOwnedTomlSections( ...sections .filter( (section) => - !isRuntimeHookTrustTomlSection(section.header) || + !isRuntimeHookTrustTomlSection(section) || (sharedHookTrust !== undefined && - claimSharedHookTrustSection(section.header, sharedHookTrust, carriedHookTrustKeys)) + claimSharedHookTrustSection(section, sharedHookTrust, carriedHookTrustKeys)) ) .filter( (section) => - !isRuntimeProjectTomlSection(section.header) || - !runtimeProjectHeaders.has(getTomlSectionHeaderKey(section.header)) || + !isRuntimeProjectTomlSection(section) || + !runtimeProjectHeaders.has(getTomlSectionHeaderKey(section)) || getProjectTrustLevel(section.block) === 'untrusted' ) .map((section) => section.block) @@ -80,7 +77,8 @@ export function getTomlSections(config: string): TomlSection[] { sections.push({ header: sectionHeader ?? '', block: lines.slice(sectionStart, index).join('\n'), - start: sectionStart + start: sectionStart, + table: parseTomlTableHeaderPath(sectionHeader ?? '') }) } sectionStart = index @@ -92,18 +90,19 @@ export function getTomlSections(config: string): TomlSection[] { sections.push({ header: sectionHeader ?? '', block: lines.slice(sectionStart).join('\n'), - start: sectionStart + start: sectionStart, + table: parseTomlTableHeaderPath(sectionHeader ?? '') }) } return sections } -export function isRuntimePreservedTomlSection(header: string): boolean { - return isRuntimeHookTrustTomlSection(header) || isRuntimeProjectTomlSection(header) +export function isRuntimePreservedTomlSection(section: TomlSection): boolean { + return isRuntimeHookTrustTomlSection(section) || isRuntimeProjectTomlSection(section) } -export function isRuntimeHookTrustTomlSection(header: string): boolean { - const table = parseTomlTableHeaderPath(header) +export function isRuntimeHookTrustTomlSection(section: TomlSection): boolean { + const table = section.table // Preserve the hooks.state parent Codex materializes on Windows, including quoted spellings. return !!table && !table.isArray && table.segments[0] === 'hooks' && table.segments[1] === 'state' } @@ -128,7 +127,7 @@ export function classifyHookTrustKey(key: string, homeDir: string): 'home-scoped export function getHookTrustTomlSectionKeys(sections: readonly TomlSection[]): Set { const keys = new Set() for (const section of sections) { - const key = parseHookStateTomlHeaderKey(section.header) + const key = sectionLeafKey(section, ['hooks', 'state']) if (key !== null) { keys.add(key) } @@ -138,11 +137,11 @@ export function getHookTrustTomlSectionKeys(sections: readonly TomlSection[]): S // Runtime trust wins; compare exact keys to retain required Windows separator variants. function claimSharedHookTrustSection( - header: string, + section: TomlSection, { systemHomeDir }: SharedHookTrustCarry, claimedKeys: Set ): boolean { - const key = parseHookStateTomlHeaderKey(header) + const key = sectionLeafKey(section, ['hooks', 'state']) if (key === null || classifyHookTrustKey(key, systemHomeDir) === 'home-scoped') { return false } @@ -153,32 +152,45 @@ function claimSharedHookTrustSection( return true } -export function isRuntimeProjectTomlSection(header: string): boolean { - return parseCodexProjectHeaderPath(header) !== null +function sectionLeafKey(section: TomlSection, parent: readonly string[]): string | null { + const table = section.table + if ( + !table || + table.isArray || + table.segments.length !== parent.length + 1 || + parent.some((segment, index) => table.segments[index] !== segment) + ) { + return null + } + return table.segments.at(-1) ?? null +} + +export function isRuntimeProjectTomlSection(section: TomlSection): boolean { + return sectionLeafKey(section, ['projects']) !== null } const CODEX_MCP_SERVER_TABLE_ROOT = 'mcp_servers' -export function getMcpServerTomlSectionName(header: string): string | null { - const table = parseTomlTableHeaderPath(header) +export function getMcpServerTomlSectionName(section: TomlSection): string | null { + const table = section.table if (!table || table.isArray || table.segments[0] !== CODEX_MCP_SERVER_TABLE_ROOT) { return null } return table.segments[1] ?? null } -export function getTomlSectionHeaderKey(header: string): string { - const projectPath = parseCodexProjectHeaderPath(header) +export function getTomlSectionHeaderKey(section: TomlSection): string { + const projectPath = sectionLeafKey(section, ['projects']) return projectPath === null - ? header.trim() + ? section.header.trim() : `project:${normalizeCodexProjectPathForLookup(projectPath)}` } // Match legacy WSL revocations loosely so case drift cannot restore trust. -export function getRevocationTomlSectionHeaderKey(header: string): string { - const projectPath = parseCodexProjectHeaderPath(header) +export function getRevocationTomlSectionHeaderKey(section: TomlSection): string { + const projectPath = sectionLeafKey(section, ['projects']) return projectPath === null - ? header.trim() + ? section.header.trim() : `project:${normalizeCodexProjectPathForRevocationLookup(projectPath)}` } @@ -187,11 +199,11 @@ export function deduplicateProjectTomlSections(sections: TomlSection[]): TomlSec const deduplicated: TomlSection[] = [] const projectIndexes = new Map() for (const section of sections) { - if (!isRuntimeProjectTomlSection(section.header)) { + if (!isRuntimeProjectTomlSection(section)) { deduplicated.push(section) continue } - const key = getTomlSectionHeaderKey(section.header) + const key = getTomlSectionHeaderKey(section) const existingIndex = projectIndexes.get(key) if (existingIndex === undefined) { projectIndexes.set(key, deduplicated.length) @@ -225,8 +237,8 @@ export function extractOrdinaryCodexSettings(config: string): string { const sections = deduplicateProjectTomlSections(getTomlSections(config)) const projectHeaders = new Set( sections - .filter((section) => isRuntimeProjectTomlSection(section.header)) - .map((section) => getTomlSectionHeaderKey(section.header)) + .filter((section) => isRuntimeProjectTomlSection(section)) + .map((section) => getTomlSectionHeaderKey(section)) ) // The daemon override belongs to the managed home and must not be promoted. return stripCodexDaemonOverride(stripRuntimeOwnedTomlSections(config, projectHeaders)).trimEnd() diff --git a/src/main/codex/structured/catalog/codex-model-catalog-probe.test.ts b/src/main/codex/structured/catalog/codex-model-catalog-probe.test.ts index 9f5f9ac9a62..5a798b10f0e 100644 --- a/src/main/codex/structured/catalog/codex-model-catalog-probe.test.ts +++ b/src/main/codex/structured/catalog/codex-model-catalog-probe.test.ts @@ -1,5 +1,18 @@ -import { afterEach, describe, expect, it, vi } from 'vitest' +import { runAgentModelCatalogSession } from '../../../native-chat/agent-model-catalog/agent-model-catalog-probe-runner' +import { PrioritySemaphore } from '../../../../shared/priority-semaphore' +import type { AgentModelCatalogSuccess } from '../../../native-chat/agent-model-catalog/agent-model-catalog-entry' +import { mkdtemp, mkdir, writeFile, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { + getSystemCodexHomePath, + resolveOrcaManagedCodexHomePath +} from '../../configuration/homes/codex-home-paths' +import { createAgentModelCatalogService } from '../../../native-chat/agent-model-catalog/agent-model-catalog-service' +import { AgentModelCatalogStore } from '../../../native-chat/agent-model-catalog/agent-model-catalog-store' +import { afterEach, describe, expect, it, vi, onTestFinished } from 'vitest' import { createCodexModelCatalogProbe } from './codex-model-catalog-probe' +import { fetchCodexModelCatalogListing } from './codex-structured-model-catalog' import { runCodexAppServerSession, type CodexAppServerInvocation @@ -13,7 +26,10 @@ const MODEL_ROW = { isDefault: true } -afterEach(() => vi.unstubAllGlobals()) +afterEach(() => { + vi.unstubAllGlobals() + vi.unstubAllEnvs() +}) describe('codex model catalog probe', () => { it.each([ @@ -120,7 +136,7 @@ describe('codex model catalog probe', () => { }) const success = await probe({ variable: 'CODEX_HOME', path: '/homes/a' }) expect(success.complete).toBe(false) - expect(configReadTimeouts).toEqual([3_000]) + expect(configReadTimeouts).toEqual([null]) expect(configReadFailures).toEqual([ expect.objectContaining({ name: 'CodexAppServerTimeoutError', @@ -132,3 +148,271 @@ describe('codex model catalog probe', () => { ]) }, 10_000) }) + +it('observes the shared source config while ignoring mirror writes, and observes a self-contained home itself', async () => { + const folder = await mkdtemp(join(tmpdir(), 'orca-codex-input-source-')) + vi.stubEnv('HOME', folder) + vi.stubEnv('USERPROFILE', folder) + vi.stubEnv('ORCA_USER_DATA_PATH', join(folder, 'user-data')) + const source = getSystemCodexHomePath() + const mirror = resolveOrcaManagedCodexHomePath() + const own = join(folder, 'account') + expect(source.startsWith(folder)).toBe(true) + await Promise.all([source, mirror, own].map((path) => mkdir(path, { recursive: true }))) + for (const path of [source, mirror, own]) { + await writeFile(join(path, 'config.toml'), 'model="original"') + } + let model = 'original' + let account = mirror + const probe = createCodexModelCatalogProbe({ + resolveEnvironment: async () => ({}), + resolveCommand: () => ({ command: process.execPath, prefixArgs: [] }), + runSession: async (_invocation, body) => + body({ + request: async (method) => + method === 'model/list' + ? { data: [{ ...MODEL_ROW, model }], nextCursor: null } + : method === 'config/read' + ? { config: {} } + : {}, + notify: () => {} + }) + }) + const service = createAgentModelCatalogService({ + store: new AgentModelCatalogStore(), + getConversation: () => undefined, + getRecord: () => undefined, + drivesRecord: () => true, + resolveAccountHome: async () => ({ variable: 'CODEX_HOME', path: account }), + probes: { codex: probe } + }) + try { + const read = () => service.read({ agent: 'codex', workspacePath: folder, waitForListing: true }) + expect(await read()).toMatchObject({ models: [{ id: 'original' }] }) + model = 'changed' + await writeFile(join(mirror, 'config.toml'), 'model="mirrored-probe-write"') + expect(await read()).toMatchObject({ models: [{ id: 'original' }] }) + await writeFile(join(source, 'config.toml'), 'model="source-changed"') + expect(await read()).toMatchObject({ models: [{ id: 'changed' }] }) + account = own + expect(await read()).toMatchObject({ models: [{ id: 'changed' }] }) + model = 'own-changed' + await writeFile(join(own, 'config.toml'), 'model="own-changed"') + expect(await read()).toMatchObject({ models: [{ id: 'own-changed' }] }) + } finally { + await service.stop() + await rm(folder, { recursive: true, force: true }) + } +}) + +it('delivers inventory while config and account are pending, then keeps both late replies', async () => { + const config = Promise.withResolvers() + const account = Promise.withResolvers() + const inventoried = Promise.withResolvers() + const observations: AgentModelCatalogSuccess[] = [] + const probe = createCodexModelCatalogProbe({ + resolveEnvironment: async () => ({}), + resolveCommand: () => ({ command: process.execPath, prefixArgs: [] }), + runSession: async (_invocation, body) => + body({ + request: async (method, _params, options) => { + if (options?.timeoutMs !== null) { + throw new Error('catalog request retained its response deadline') + } + if (method === 'config/read') { + return config.promise + } + if (method === 'account/read') { + return account.promise + } + return { data: [MODEL_ROW], nextCursor: null } + }, + notify: () => {} + }) + }) + const done = probe( + { variable: 'CODEX_HOME', path: '/account' }, + { + publish: (success) => { + observations.push(success) + inventoried.resolve() + } + } + ) + await inventoried.promise + expect(observations[0]).toMatchObject({ models: [{ id: 'gpt-live' }], complete: false }) + expect(observations[0].configured).toBeUndefined() + config.resolve({ config: { model: 'gpt-live', model_reasoning_effort: 'high' } }) + account.resolve({ requiresOpenaiAuth: true, account: null }) + expect(await done).toMatchObject({ + models: [{ id: 'gpt-live', defaultEffort: 'high' }], + complete: true, + unavailable: { reason: 'notSignedIn' } + }) + expect(observations[0].complete).toBe(false) +}) + +it.each([ + ['empty', { requiresOpenaiAuth: true, account: null }, 'notSignedIn'], + ['rejected', { requiresOpenaiAuth: true, account: null }, 'notSignedIn'], + ['empty', { requiresOpenaiAuth: false, account: { type: 'apiKey' } }, undefined], + ['empty', {}, undefined] +] as const)( + 'classifies %s inventory after delayed account evidence %j without optional config, retaining admission through close', + async (kind, accountResult, reason) => { + const configRead = vi.fn(() => new Promise(() => {})) + const account = Promise.withResolvers() + const inventory = Promise.withResolvers() + const closing = Promise.withResolvers() + const closed = Promise.withResolvers() + onTestFinished(() => { + account.resolve({ requiresOpenaiAuth: true, account: null }) + closed.resolve() + }) + const probe = createCodexModelCatalogProbe({ + resolveEnvironment: async () => ({}), + resolveCommand: () => ({ command: process.execPath, prefixArgs: [] }), + runSession: async (invocation, body) => + runAgentModelCatalogSession( + () => ({ + request: async (method: string) => { + if (method === 'account/read') { + return account.promise + } + if (method === 'config/read') { + return configRead() + } + inventory.resolve() + if (kind === 'rejected') { + throw new Error('model listing refused') + } + return { data: [], nextCursor: null } + }, + notify: () => {}, + close: () => { + closing.resolve() + return closed.promise + } + }), + body, + { label: 'codex', signal: invocation.signal, timeoutMs: null } + ) + }) + const store = new AgentModelCatalogStore() + const admission = new PrioritySemaphore(1) + const first = store.refresh( + 'signed-out', + 'codex', + (_selected, operation) => + probe( + { variable: 'CODEX_HOME', path: '/account' }, + { operation, signal: operation.signal } + ), + { admission } + ) + await inventory.promise + expect(store.failure('signed-out')).toBeNull() + expect(store.isListing('signed-out')).toBe(true) + account.resolve(accountResult) + await closing.promise + let nextStarted = false + const next = store.refresh( + 'next', + 'codex', + async () => { + nextStarted = true + return { + origin: 'probe', + complete: true, + models: [{ id: 'next', label: 'Next', isDefault: true, efforts: [] }] + } + }, + { admission } + ) + await Promise.resolve() + expect(nextStarted).toBe(false) + closed.resolve() + expect(await first).toBeNull() + expect(configRead).not.toHaveBeenCalled() + expect(store.failure('signed-out')).not.toBeNull() + expect(store.failure('signed-out')?.unavailable).toEqual(reason ? { reason } : undefined) + expect((await next)?.models[0]?.id).toBe('next') + } +) + +// A real child must complete startup and supervised shutdown within this wall-clock budget. +it('settles a failed inventory and pending sign-in classification when the real transport closes', async () => { + const server = String.raw` + const readline = require('node:readline') + readline.createInterface({ input: process.stdin }).on('line', (line) => { + const message = JSON.parse(line) + if (message.method === 'account/read') return + if (message.method === 'model/list') { + process.stdout.write(JSON.stringify({ id: message.id, error: { code: -32000, message: 'inventory unavailable' } }) + '\n', () => process.exit(1)) + return + } + if (typeof message.id === 'number') process.stdout.write(JSON.stringify({ id: message.id, result: {} }) + '\n') + }) + ` + const store = new AgentModelCatalogStore() + const probe = createCodexModelCatalogProbe({ + resolveEnvironment: async () => ({}), + resolveCommand: () => ({ command: process.execPath, prefixArgs: [] }), + runSession: (invocation, body) => + runCodexAppServerSession( + { ...invocation, command: process.execPath, cliPath: null, args: ['-e', server] }, + body + ) + }) + const home = { variable: 'CODEX_HOME', path: '/account' } + await expect( + store.refresh('closed', 'codex', async (_models, operation) => + probe(home, { operation, signal: operation.signal }) + ) + ).resolves.toBeNull() + expect(store.isListing('closed')).toBe(false) + expect(store.failure('closed')).not.toBeNull() +}, 10_000) + +it('keeps a later nonempty page and late configured model after an empty first page', async () => { + const config = Promise.withResolvers() + const observations: string[][] = [] + const request = vi.fn(async (method: string, params?: Record) => { + if (method === 'config/read') { + return config.promise + } + return params?.cursor + ? { data: [MODEL_ROW], nextCursor: null } + : { data: [], nextCursor: 'next' } + }) + const result = fetchCodexModelCatalogListing({ + connection: { request }, + timeoutMs: null, + publish: (listing) => observations.push(listing.models.map((model) => model.id)) + }) + await vi.waitFor(() => expect(observations).toEqual([['gpt-live']])) + config.resolve({ config: { model: 'gpt-live', model_reasoning_effort: 'high' } }) + expect(await result).toMatchObject({ + complete: true, + models: [{ id: 'gpt-live' }], + configured: { model: 'gpt-live', effort: 'high' } + }) +}) + +it.each(['empty', 'malformed', 'capped'])( + 'returns incomplete %s inventory without optional configuration', + async (kind) => { + const request = vi.fn(async (method: string) => { + if (method === 'config/read') { + throw new Error('configuration cannot provide inventory') + } + return kind === 'malformed' + ? { data: [{}], nextCursor: null } + : { data: [], nextCursor: kind === 'capped' ? 'more' : null } + }) + expect( + await fetchCodexModelCatalogListing({ connection: { request }, timeoutMs: null }) + ).toEqual({ models: [], complete: false }) + expect(request.mock.calls.every(([method]) => method === 'model/list')).toBe(true) + } +) diff --git a/src/main/codex/structured/catalog/codex-model-catalog-probe.ts b/src/main/codex/structured/catalog/codex-model-catalog-probe.ts index 2bc912e2a13..b56dbde9ad7 100644 --- a/src/main/codex/structured/catalog/codex-model-catalog-probe.ts +++ b/src/main/codex/structured/catalog/codex-model-catalog-probe.ts @@ -1,3 +1,9 @@ +import { join, resolve as resolvePath } from 'node:path' +import { + getSystemCodexHomePath, + resolveOrcaManagedCodexHomePath +} from '../../configuration/homes/codex-home-paths' +import { agentModelCatalogInputStamp } from '../../../native-chat/agent-model-catalog/agent-model-catalog-input-stamp' import { assertStructuredAgentResolutionCurrent, type StructuredAgentResolutionOperation @@ -21,8 +27,8 @@ import { type CodexStructuredInvocationDeps } from '../launch/codex-structured-launch-resolution' import { requireLegacyAgentSessionAccountHome } from '../../../../shared/agent-session/lifecycle/agent-session-account-home' -import { AGENT_MODEL_CATALOG_PROBE_TIMEOUT_MS } from '../../../native-chat/agent-model-catalog/agent-model-catalog-probe-runner' import type { + AgentModelCatalogProgress, AgentModelCatalogProbe, AgentModelCatalogLookup } from '../../../native-chat/agent-model-catalog/agent-model-catalog-discovery' @@ -64,7 +70,9 @@ export function createCodexModelCatalogProbe( const accountHomePath = requireLegacyAgentSessionAccountHome(accountHome).path const overlay = overlayEnvironmentVariables({}, deps.resolveEnv?.(), process.platform) const readEnvironment = - lookup === 'saved' ? deps.resolveSavedEnvironment : deps.resolveEnvironment + lookup !== 'recover' + ? (deps.resolveSavedEnvironment ?? deps.resolveEnvironment) + : deps.resolveEnvironment const { command, prefixArgs, environment } = await resolveCodexStructuredInvocation({ ...deps, operation, @@ -98,15 +106,48 @@ export function createCodexModelCatalogProbe( CODEX_HOME: accountHomePath } return { + inputStamp: await agentModelCatalogInputStamp({ + command, + prefixArgs, + cwd: listIn ?? process.cwd(), + env: launchDirectory ? envWithPathRelativeTo(env, launchDirectory) : env, + configFiles: [ + join( + resolvePath(accountHomePath) === resolvePath(resolveOrcaManagedCodexHomePath()) + ? getSystemCodexHomePath() + : accountHomePath, + 'config.toml' + ) + ] + }), identity: { command, args, cwd: listIn, overlay: Object.entries(overlay).sort() }, run: async ( signal?: AbortSignal, - runOperation = operation + runOperation = operation, + _selectedModels?: readonly string[], + publish?: AgentModelCatalogProgress ): Promise => { assertStructuredAgentResolutionCurrent(runOperation) signal?.throwIfAborted() deps.prepareHome?.(accountHomePath) const run = deps.runSession ?? runCodexAppServerSession + const successFromListing = ( + listing: Awaited>, + unavailable: AgentModelCatalogSuccess['unavailable'] + ): AgentModelCatalogSuccess => { + return { + models: cwd + ? listing.models + : applyCodexConfiguredLaunchDefaults(listing.models, { + model: listing.configured?.model ?? null, + effort: listing.configured?.effort ?? null + }), + complete: listing.complete !== false, + ...(cwd && listing.configured ? { configured: listing.configured } : {}), + origin: 'probe', + ...(unavailable ? { unavailable } : {}) + } + } const { listing, unavailable } = await run( { command, @@ -114,44 +155,60 @@ export function createCodexModelCatalogProbe( ...(listIn ? { cwd: listIn } : {}), cliPath: command, env, - timeoutMs: AGENT_MODEL_CATALOG_PROBE_TIMEOUT_MS, + timeoutMs: null, ...(signal ? { signal } : {}) }, async (rpc) => { - assertStructuredAgentResolutionCurrent(runOperation) - // Overlap model and account reads to avoid serial probe latency. - const listed = fetchCodexModelCatalogListing({ + let latest: Awaited> | undefined + let accountKnown = false + let unavailable: AgentModelCatalogSuccess['unavailable'] + const observe = (next: NonNullable) => { + assertStructuredAgentResolutionCurrent(runOperation) + latest = next + publish?.(successFromListing(next, unavailable), accountKnown) + } + const account = rpc + .request('account/read', { refreshToken: false }, { timeoutMs: null }) + .catch(() => undefined) + .then((response: unknown) => { + assertStructuredAgentResolutionCurrent(runOperation) + if ( + typeof response === 'object' && + response !== null && + !Array.isArray(response) && + 'account' in response + ) { + accountKnown = true + if ( + 'requiresOpenaiAuth' in response && + response.requiresOpenaiAuth === true && + response.account === null + ) { + const kind = deps.resolveAccountKind?.(accountHomePath) + unavailable = { reason: 'notSignedIn', ...(kind ? { account: kind } : {}) } + } + } + if (latest) { + observe(latest) + } + }) + void account.catch(() => {}) + const listing = await fetchCodexModelCatalogListing({ connection: rpc, cwd, - operation: runOperation + operation: runOperation, + timeoutMs: null, + publish: observe + }).catch(async (error: unknown) => { + await account + if (unavailable) { + throw new AgentModelCatalogUnavailableError(unavailable) + } + throw error }) - void listed.catch(() => {}) - assertStructuredAgentResolutionCurrent(runOperation) - const response: unknown = await rpc - .request('account/read', { refreshToken: false }, { timeoutMs: 2_000 }) - // Failed account reads do not establish sign-out. - .catch(() => undefined) - assertStructuredAgentResolutionCurrent(runOperation) - if ( - typeof response !== 'object' || - response === null || - Array.isArray(response) || - !('requiresOpenaiAuth' in response) || - response.requiresOpenaiAuth !== true || - !('account' in response) || - response.account !== null - ) { - return { listing: await listed, unavailable: undefined } - } - const account = deps.resolveAccountKind?.(accountHomePath) - const signedOut = { reason: 'notSignedIn' as const, ...(account ? { account } : {}) } - // Gateways may misreport sign-out; keep models that were successfully listed. - const inHand = await listed.catch(() => null) - assertStructuredAgentResolutionCurrent(runOperation) - if (!inHand?.models.length) { - throw new AgentModelCatalogUnavailableError(signedOut) - } - return { listing: inHand, unavailable: signedOut } + observe(listing) + await account + return { listing, unavailable } } ).catch((error: unknown) => { if (isMissingProviderExecutable(error, command)) { @@ -161,20 +218,12 @@ export function createCodexModelCatalogProbe( }) assertStructuredAgentResolutionCurrent(runOperation) if (listing.models.length === 0) { + if (unavailable) { + throw new AgentModelCatalogUnavailableError(unavailable) + } throw new Error('codex app-server listed no models') } - return { - models: cwd - ? listing.models - : applyCodexConfiguredLaunchDefaults(listing.models, { - model: listing.configured?.model ?? null, - effort: listing.configured?.effort ?? null - }), - complete: listing.complete !== false, - ...(cwd && listing.configured ? { configured: listing.configured } : {}), - origin: 'probe', - ...(unavailable ? { unavailable } : {}) - } + return successFromListing(listing, unavailable) } } } @@ -184,8 +233,14 @@ export function createCodexModelCatalogProbe( >( async (home, options) => (await resolve(home, options?.cwd ?? process.cwd(), 'recover', options?.operation)).run( - options?.signal + options?.signal, + options?.operation, + options?.selectedModels, + options?.publish ), - { resolve, resolveLegacy: (home, operation) => resolve(home, undefined, 'recover', operation) } + { + resolve, + resolveLegacy: (home, operation, lookup) => resolve(home, undefined, lookup, operation) + } ) } diff --git a/src/main/codex/structured/catalog/codex-structured-model-catalog.ts b/src/main/codex/structured/catalog/codex-structured-model-catalog.ts index 55f15f01174..e56a3ec12bc 100644 --- a/src/main/codex/structured/catalog/codex-structured-model-catalog.ts +++ b/src/main/codex/structured/catalog/codex-structured-model-catalog.ts @@ -154,16 +154,17 @@ export async function listCodexModelPages( export async function fetchCodexModelCatalogListing(input: { connection: Pick - timeoutMs?: number + timeoutMs?: number | null deadlineMs?: number cwd?: string operation?: StructuredAgentResolutionOperation + publish?: (listing: CodexModelCatalogListing) => void }): Promise { const deadline = Math.min( input.operation?.deadline ?? Infinity, input.deadlineMs === undefined ? Infinity : Date.now() + input.deadlineMs ) - const remainingTimeout = (): number | undefined => { + const remainingTimeout = (): number | null | undefined => { assertStructuredAgentResolutionCurrent(input.operation) if (!Number.isFinite(deadline)) { return input.timeoutMs @@ -182,6 +183,11 @@ export async function fetchCodexModelCatalogListing(input: { return response }) ) + assertStructuredAgentResolutionCurrent(input.operation) + if (parsedModels.length === 0) { + return { models: [], complete: false } + } + input.publish?.({ models: parsedModels, complete: false }) const configured = await readCodexConfiguredLaunchDefaults( input.connection, remainingTimeout(), diff --git a/src/main/codex/structured/launch/codex-configured-launch-defaults.ts b/src/main/codex/structured/launch/codex-configured-launch-defaults.ts index 42a43a73027..458c14b1988 100644 --- a/src/main/codex/structured/launch/codex-configured-launch-defaults.ts +++ b/src/main/codex/structured/launch/codex-configured-launch-defaults.ts @@ -17,12 +17,12 @@ function configuredText(value: unknown): string | null { export async function readCodexConfiguredLaunchDefaults( connection: Pick, - timeoutMs = CONFIG_READ_TIMEOUT_MS, + timeoutMs: number | null = CONFIG_READ_TIMEOUT_MS, cwd?: string ): Promise { try { const response = await connection.request('config/read', cwd ? { cwd } : {}, { - timeoutMs: Math.min(timeoutMs, CONFIG_READ_TIMEOUT_MS) + timeoutMs: timeoutMs === null ? null : Math.min(timeoutMs, CONFIG_READ_TIMEOUT_MS) }) const config = typeof response === 'object' && response !== null && 'config' in response diff --git a/src/main/computer/computer-sidecar.ts b/src/main/computer/computer-sidecar.ts index 983f164c744..c4ca1e29e67 100644 --- a/src/main/computer/computer-sidecar.ts +++ b/src/main/computer/computer-sidecar.ts @@ -59,6 +59,9 @@ async function dispatch(method: string, params: Record): Promis case 'listApps': { return await provider.listApps() } + case 'launchApp': { + return await provider.launchApp(params) + } case 'listWindows': { return await provider.listWindows(params) } diff --git a/src/main/computer/desktop-script/desktop-script-apps.test.ts b/src/main/computer/desktop-script/desktop-script-apps.test.ts new file mode 100644 index 00000000000..afa09e6131f --- /dev/null +++ b/src/main/computer/desktop-script/desktop-script-apps.test.ts @@ -0,0 +1,74 @@ +import { describe, expect, it, vi } from 'vitest' +import type { ComputerProviderCapabilities } from '../../../shared/computer-use-runtime-types' +import { launchBridgeApp } from './desktop-script-apps' + +function capabilities(launch: boolean | undefined): ComputerProviderCapabilities { + return { + platform: 'linux', + provider: 'orca-computer-use-linux', + providerVersion: '1.0.0', + protocolVersion: 1, + supports: { + apps: { + list: true, + bundleIds: false, + pids: true, + ...(launch === undefined ? {} : { launch }) + }, + windows: { + list: true, + targetById: true, + targetByIndex: true, + focus: false, + moveResize: false + }, + observation: { + screenshot: true, + annotatedScreenshot: false, + elementFrames: true, + ocr: false + }, + actions: { + click: true, + typeText: true, + pressKey: true, + hotkey: true, + pasteText: true, + scroll: true, + drag: true, + setValue: true, + performAction: true + }, + surfaces: { menus: false, dialogs: false, dock: false, menubar: false } + } + } +} + +describe('launchBridgeApp', () => { + it('refuses before starting anything when the provider cannot launch in the background', async () => { + const callBridge = vi.fn() + + await expect( + launchBridgeApp(capabilities(undefined), callBridge, { app: 'gedit' }) + ).rejects.toMatchObject({ code: 'unsupported_capability' }) + expect(callBridge).not.toHaveBeenCalled() + }) + + it('reports what the provider measured, not what was asked', async () => { + const callBridge = vi.fn(async () => ({ + ok: true, + app: { name: 'charmap', bundleId: 'charmap', pid: 42 }, + launched: true, + delivery: 'foreground' as const + })) + + await expect( + launchBridgeApp(capabilities(true), callBridge, { app: 'charmap' }) + ).resolves.toEqual({ + app: { name: 'charmap', bundleId: 'charmap', pid: 42 }, + launched: true, + delivery: 'foreground' + }) + expect(callBridge).toHaveBeenCalledWith({ tool: 'launch_app', app: 'charmap', activate: false }) + }) +}) diff --git a/src/main/computer/desktop-script/desktop-script-apps.ts b/src/main/computer/desktop-script/desktop-script-apps.ts new file mode 100644 index 00000000000..6a61dfc2bbc --- /dev/null +++ b/src/main/computer/desktop-script/desktop-script-apps.ts @@ -0,0 +1,48 @@ +import type { + ComputerLaunchAppResult, + ComputerListAppsResult, + ComputerProviderCapabilities +} from '../../../shared/computer-use-runtime-types' +import { RuntimeClientError } from '../runtime-client-error' +import { stringParam } from './desktop-script-provider-params' +import type { BridgeRequest, BridgeResponse } from './desktop-script-provider-types' +import { normalizeBridgeApp } from './snapshots/desktop-script-snapshot-rendering' + +type CallBridge = (request: BridgeRequest) => Promise + +export async function listBridgeApps(callBridge: CallBridge): Promise { + const response = await callBridge({ tool: 'list_apps' }) + return { + apps: (response.apps ?? []).map((app) => ({ + name: app.name, + bundleId: app.bundleId ?? app.bundleIdentifier ?? null, + pid: app.pid, + isRunning: true, + lastUsedAt: null, + useCount: null + })) + } +} + +export async function launchBridgeApp( + capabilities: ComputerProviderCapabilities, + callBridge: CallBridge, + params: Record +): Promise { + if (capabilities.supports.apps.launch !== true) { + throw new RuntimeClientError( + 'unsupported_capability', + `${capabilities.provider} does not support apps.launch` + ) + } + const response = await callBridge({ + tool: 'launch_app', + app: stringParam(params, 'app'), + activate: params.activate === true + }) + return { + app: normalizeBridgeApp(response.app), + launched: response.launched === true, + delivery: response.delivery === 'foreground' ? 'foreground' : 'background' + } +} 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 ad42dfead7c..cc1f070813b 100644 --- a/src/main/computer/desktop-script/desktop-script-provider-bridge.ts +++ b/src/main/computer/desktop-script/desktop-script-provider-bridge.ts @@ -175,6 +175,9 @@ export function mapBridgeError(message: string): RuntimeClientError { if (/appBlocked|app blocked/i.test(text)) { return new RuntimeClientError('app_blocked', text) } + if (/app_launch_failed/.test(text)) { + return new RuntimeClientError('app_launch_failed', text) + } if ( /unsupported capability|hotkey.*require|paste_text requires|modified clicks require xdotool|GDK is required for non-character key synthesis/i.test( text diff --git a/src/main/computer/desktop-script/desktop-script-provider-client.ts b/src/main/computer/desktop-script/desktop-script-provider-client.ts index cb64132de77..5cd004b6399 100644 --- a/src/main/computer/desktop-script/desktop-script-provider-client.ts +++ b/src/main/computer/desktop-script/desktop-script-provider-client.ts @@ -3,6 +3,7 @@ import { tmpdir } from 'node:os' import { join } from 'node:path' import type { ComputerActionResult, + ComputerLaunchAppResult, ComputerListAppsResult, ComputerListWindowsResult, ComputerProviderCapabilities, @@ -20,6 +21,7 @@ import { } from './actions/desktop-script-action' import { validateComputerProviderActionParams } from '../action-contracts/computer-provider-action-validation' import { execBridge, mapBridgeError } from './desktop-script-provider-bridge' +import { launchBridgeApp, listBridgeApps } from './desktop-script-apps' import { optionalNumberParam, optionalStringParam, @@ -70,23 +72,18 @@ export class DesktopScriptProviderClient { } async listApps(): Promise { - const response = await this.callBridge({ tool: 'list_apps' }) - return { - apps: (response.apps ?? []).map((app) => ({ - name: app.name, - bundleId: app.bundleId ?? app.bundleIdentifier ?? null, - pid: app.pid, - isRunning: true, - lastUsedAt: null, - useCount: null - })) - } + return await listBridgeApps((request) => this.callBridge(request)) } async capabilities(): Promise { return await this.readCapabilities() } + async launchApp(params: Record): Promise { + const capabilities = await this.readCapabilities() + return await launchBridgeApp(capabilities, (request) => this.callBridge(request), params) + } + async listWindows(params: Record): Promise { const capabilities = await this.readCapabilities() if (!capabilities.supports.windows.list) { diff --git a/src/main/computer/desktop-script/desktop-script-provider-types.ts b/src/main/computer/desktop-script/desktop-script-provider-types.ts index 8b57089c263..85445c834a2 100644 --- a/src/main/computer/desktop-script/desktop-script-provider-types.ts +++ b/src/main/computer/desktop-script/desktop-script-provider-types.ts @@ -6,6 +6,7 @@ import type { export type NativeMethod = | 'handshake' | 'listApps' + | 'launchApp' | 'listWindows' | 'getAppState' | 'click' @@ -20,7 +21,7 @@ export type NativeMethod = export type NativeActionMethod = Exclude< NativeMethod, - 'handshake' | 'listApps' | 'listWindows' | 'getAppState' + 'handshake' | 'listApps' | 'launchApp' | 'listWindows' | 'getAppState' > export type BridgeFrame = { @@ -124,6 +125,9 @@ export type BridgeResponse = { pid: number } windows?: BridgeWindow[] + /** launch_app: false when the app was already running. */ + launched?: boolean + delivery?: 'background' | 'foreground' snapshot?: BridgeSnapshot action?: ComputerActionMetadata } @@ -158,4 +162,6 @@ export type BridgeRequest = { windowIndex?: number noScreenshot?: boolean restoreWindow?: boolean + /** launch_app: let the started app take the foreground. */ + activate?: boolean } diff --git a/src/main/computer/macos-native/macos-native-provider-client.ts b/src/main/computer/macos-native/macos-native-provider-client.ts index ab42be4c378..cd87896db48 100644 --- a/src/main/computer/macos-native/macos-native-provider-client.ts +++ b/src/main/computer/macos-native/macos-native-provider-client.ts @@ -2,6 +2,7 @@ import { rmSync } from 'node:fs' import type net from 'node:net' import type { ComputerActionResult, + ComputerLaunchAppResult, ComputerListAppsResult, ComputerListWindowsResult, ComputerProviderCapabilities, @@ -50,6 +51,11 @@ export class MacOSNativeProviderClient { await this.ensureCompatible() return this.providerCapabilities! } + async launchApp(params: unknown): Promise { + await this.ensureCapability('apps', 'launch') + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the helper's launchApp reply has this shape; sibling methods cast the same way. + return (await this.call('launchApp', params)) as ComputerLaunchAppResult + } async listWindows(params: unknown): Promise { await this.ensureCapability('windows', 'list') return (await this.call('listWindows', params)) as ComputerListWindowsResult diff --git a/src/main/computer/macos-native/macos-native-provider-contract.ts b/src/main/computer/macos-native/macos-native-provider-contract.ts index 251fa8fbd41..27100617773 100644 --- a/src/main/computer/macos-native/macos-native-provider-contract.ts +++ b/src/main/computer/macos-native/macos-native-provider-contract.ts @@ -4,6 +4,7 @@ import type { ComputerProviderCapabilities } from '../../../shared/computer-use- export type NativeMethod = | 'handshake' | 'listApps' + | 'launchApp' | 'listWindows' | 'getAppState' | 'click' @@ -19,7 +20,7 @@ export type NativeMethod = export type NativeActionMethod = Exclude< NativeMethod, - 'handshake' | 'listApps' | 'listWindows' | 'getAppState' | 'terminate' + 'handshake' | 'listApps' | 'launchApp' | 'listWindows' | 'getAppState' | 'terminate' > export type NativeResponse = diff --git a/src/main/computer/macos-permissions/macos-computer-use-permissions-shutdown.test.ts b/src/main/computer/macos-permissions/macos-computer-use-permissions-shutdown.test.ts index 5123bca82ac..28b95b2b687 100644 --- a/src/main/computer/macos-permissions/macos-computer-use-permissions-shutdown.test.ts +++ b/src/main/computer/macos-permissions/macos-computer-use-permissions-shutdown.test.ts @@ -251,4 +251,21 @@ describe('computer-use setup cancellation', () => { }) expect(setupLaunches()).toEqual([['--permission', 'accessibility']]) }) + + it('leaves System Settings closed when an agent asks for a granted permission', async () => { + mocks.peek.mockResolvedValue({ + ...denied, + permissions: [ + { id: 'accessibility', status: 'granted' }, + { id: 'screenshots', status: 'not-granted' } + ] + }) + const { permissions } = await loadModules() + await expect( + permissions.openComputerUsePermissions('accessibility', { skipGranted: true }) + ).resolves.toMatchObject({ launchedHelper: false, openedSettings: false }) + expect(setupLaunches()).toEqual([]) + await permissions.openComputerUsePermissions('screenshots', { skipGranted: true }) + expect(setupLaunches()).toEqual([['--permission', 'screenshots']]) + }) }) diff --git a/src/main/computer/macos-permissions/macos-computer-use-permissions.ts b/src/main/computer/macos-permissions/macos-computer-use-permissions.ts index 4dbba0fdf65..41f34ffedaf 100644 --- a/src/main/computer/macos-permissions/macos-computer-use-permissions.ts +++ b/src/main/computer/macos-permissions/macos-computer-use-permissions.ts @@ -33,9 +33,9 @@ let resetInProgress: Promise | null = null /** `locale` comes from the desktop caller; orcad and CLI opens leave the helper on its default copy. */ export function openComputerUsePermissions( permissionId?: ComputerUsePermissionId, - locale?: string + options: { locale?: string; skipGranted?: boolean } = {} ): Promise { - return openComputerUsePermissionsAsync(permissionId, locale, ++setupRequestSeq, setupGeneration) + return openComputerUsePermissionsAsync(permissionId, options, ++setupRequestSeq, setupGeneration) } function cancelPendingSetupLaunches(): void { @@ -151,7 +151,7 @@ async function launchSetupHelper( async function openComputerUsePermissionsAsync( permissionId: ComputerUsePermissionId | undefined, - locale: string | undefined, + { locale, skipGranted }: { locale?: string; skipGranted?: boolean }, seq: number, generation: number ): Promise { @@ -191,7 +191,13 @@ async function openComputerUsePermissionsAsync( ) } - if (!permissionId && !nextStep) { + const alreadyGranted = permissionId + ? skipGranted === true && + status.permissions.some( + (permission) => permission.id === permissionId && permission.status === 'granted' + ) + : !nextStep + if (alreadyGranted) { return { platform: process.platform, helperAppPath, diff --git a/src/main/computer/sidecar-transport/sidecar-client.ts b/src/main/computer/sidecar-transport/sidecar-client.ts index 60d120e0dfb..31d95815271 100644 --- a/src/main/computer/sidecar-transport/sidecar-client.ts +++ b/src/main/computer/sidecar-transport/sidecar-client.ts @@ -2,6 +2,7 @@ import { fork, type ChildProcess } from 'node:child_process' import { existsSync } from 'node:fs' import type { ComputerActionResult, + ComputerLaunchAppResult, ComputerListAppsResult, ComputerListWindowsResult, ComputerProviderCapabilities, @@ -22,6 +23,7 @@ export const COMPUTER_SIDECAR_ENTRY_FILENAME = 'computer-sidecar.js' type ComputerSidecarMethod = | 'capabilities' | 'listApps' + | 'launchApp' | 'listWindows' | 'getAppState' | 'click' @@ -64,6 +66,13 @@ export async function callComputerSidecarCapabilities(): Promise { + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the sidecar returns the provider's launchApp result unchanged, like its sibling calls. + return (await getComputerSidecar().call('launchApp', params)) as ComputerLaunchAppResult +} + export async function callComputerSidecarListWindows( params: unknown ): Promise { @@ -79,7 +88,7 @@ export async function callComputerSidecarSnapshot( export async function callComputerSidecarAction( method: Exclude< ComputerSidecarMethod, - 'capabilities' | 'listApps' | 'listWindows' | 'getAppState' + 'capabilities' | 'listApps' | 'launchApp' | 'listWindows' | 'getAppState' >, params: unknown ): Promise { diff --git a/src/main/daemon/executable-host/macos/macos-daemon-fork-reason.ts b/src/main/daemon/executable-host/macos/macos-daemon-fork-reason.ts new file mode 100644 index 00000000000..7d04928073f --- /dev/null +++ b/src/main/daemon/executable-host/macos/macos-daemon-fork-reason.ts @@ -0,0 +1,14 @@ +import type { ForkFallbackValue } from '../../../../shared/daemon-tcc-denial-capture' + +export type MacDaemonForkReason = Exclude + +// Why this app's latest launch forked instead of using launchd; null after a launchd launch. +let lastMacForkReason: MacDaemonForkReason | null = null + +export function setLastMacDaemonForkReason(reason: MacDaemonForkReason | null): void { + lastMacForkReason = reason +} + +export function getLastMacDaemonForkReason(): MacDaemonForkReason | null { + return lastMacForkReason +} diff --git a/src/main/daemon/executable-host/macos/macos-daemon-launchd.ts b/src/main/daemon/executable-host/macos/macos-daemon-launchd.ts index 3fc87599c4b..781c20dab78 100644 --- a/src/main/daemon/executable-host/macos/macos-daemon-launchd.ts +++ b/src/main/daemon/executable-host/macos/macos-daemon-launchd.ts @@ -18,6 +18,7 @@ import { type DaemonChildSpawnOptions } from '../../launch/daemon-launched-child-spawn' import { macDaemonJobLabel } from './macos-daemon-job-label' +import { setLastMacDaemonForkReason } from './macos-daemon-fork-reason' import type { DaemonProcessHandle } from '../../launch/daemon-spawner' import { DAEMON_SHUTDOWN_TIMEOUT_MS } from '../../launch/daemon-shutdown-deadline' import { @@ -98,6 +99,7 @@ function fallBackToFork( if (Date.now() > deadlineMs) { throw error } + setLastMacDaemonForkReason(reason) console.warn( `[daemon] macOS terminal host fallback reason=${reason}; forking from the app`, error @@ -124,17 +126,20 @@ export async function launchMacDaemonFromStableBundle( options: MacDaemonLaunchOptions, deadlineMs: number ): Promise { + setLastMacDaemonForkReason(null) // GUI login-session watching excludes serve mode. if ( process.platform !== 'darwin' || !options.macosLoginSessionWatch || !getAppEnvironment().isPackaged() ) { + setLastMacDaemonForkReason('not-eligible') return null } // Older Macs need the fork path because the helper’s Node cannot load there. const systemVersion = process.getSystemVersion?.() if (!systemVersion || compareDottedVersions(systemVersion, NODE_RUNTIME_DARWIN_MINIMUM_OS) < 0) { + setLastMacDaemonForkReason('os-too-old') return null } const label = macDaemonJobLabel(options.userDataPath) diff --git a/src/main/daemon/health/testing/daemon-async-io-probe.test.ts b/src/main/daemon/health/testing/daemon-async-io-probe.test.ts index 6878f76aa3f..7c698fe83aa 100644 --- a/src/main/daemon/health/testing/daemon-async-io-probe.test.ts +++ b/src/main/daemon/health/testing/daemon-async-io-probe.test.ts @@ -21,9 +21,7 @@ describe('daemon asynchronous I/O progress', () => { expect(probe.check()).toBe(first) await vi.advanceTimersByTimeAsync(DAEMON_ASYNC_IO_TIMEOUT_MS) expect(await first).toEqual({ state: 'stalled', pendingMs: DAEMON_ASYNC_IO_TIMEOUT_MS }) - for (let i = 0; i < 100; i++) { - expect(await probe.check()).toEqual(await first) - } + expect(await probe.check()).toEqual(await first) expect(start).toHaveBeenCalledTimes(1) expect(vi.getTimerCount()).toBe(0) }) diff --git a/src/main/daemon/observability/daemon-adoption-telemetry-event.ts b/src/main/daemon/observability/daemon-adoption-telemetry-event.ts index ae14ddc5cee..04995addb96 100644 --- a/src/main/daemon/observability/daemon-adoption-telemetry-event.ts +++ b/src/main/daemon/observability/daemon-adoption-telemetry-event.ts @@ -26,6 +26,12 @@ import { clearDaemonFolderAccessMismatch, recordDaemonFolderAccessMismatch } from '../permissions/daemon-folder-access-mismatch' +import { + noteHelperFolderReadable, + prepareTccDenialCapture, + releaseTccDenialCapture, + runTccDenialCapture +} from '../permissions/tcc-denial-capture' export type DaemonAdoptionOrigin = Pick< EventProps<'daemon_pty_cwd_denied'>, @@ -122,22 +128,35 @@ export async function reportDaemonPtyCwdVerdict(args: { if (!cwd) { return } + const deniedAtMs = Date.now() + const cwdClass = classifyDaemonPtyCwd(cwd, homedir()) + const tccFolderClass = + process.platform === 'darwin' && isMacTccFolderClass(cwdClass) ? cwdClass : null if (args.cwdReadableByDaemon === true) { clearDaemonFolderAccessMismatch(args.daemonIdentity, cwd) // TCC-gated folders only: elsewhere a readable cwd says nothing about the theory. - if ( - process.platform === 'darwin' && - isMacTccFolderClass(classifyDaemonPtyCwd(cwd, homedir())) - ) { + if (tccFolderClass) { + noteHelperFolderReadable(args.daemonIdentity, tccFolderClass) await trackDaemonPtyCwdVerdict('daemon_pty_cwd_readable', cwd, args.pidPath) } return } + // The app-side opendir below can materialize a cloud folder, so read its state first. + const capture = + tccFolderClass && args.cwdReadableByDaemon === false + ? await prepareTccDenialCapture(args.daemonIdentity, tccFolderClass, deniedAtMs) + : null if (!(await hasDaemonPtyCwdDenialDiverged(cwd, args.cwdReadableByDaemon))) { + if (capture) { + releaseTccDenialCapture(capture) + } return } // Show the notice before waiting for the codesign probe. recordDaemonFolderAccessMismatch(args.daemonIdentity, cwd) + if (capture) { + void runTccDenialCapture(capture) + } await trackDaemonPtyCwdVerdict('daemon_pty_cwd_denied', cwd, args.pidPath) } catch { // Best-effort evidence; a spawn must not fail because the notice could not be recorded. diff --git a/src/main/daemon/permissions/tcc-denial-capture.ts b/src/main/daemon/permissions/tcc-denial-capture.ts new file mode 100644 index 00000000000..f389b9340b7 --- /dev/null +++ b/src/main/daemon/permissions/tcc-denial-capture.ts @@ -0,0 +1,248 @@ +// Capture macOS's verdict once per helper incarnation and folder class when a denial is confirmed. + +import { homedir } from 'node:os' +import { runProcess } from '@orca/process-host' +import { join } from 'node:path' +import type { MacTccFolderClass } from '../../../shared/daemon-adoption-telemetry' +import { + bucketElapsed, + bucketGoodReadCount, + bucketHelperAge, + type FullDiskAccessValue, + type HelperOrigin +} from '../../../shared/daemon-tcc-denial-capture' +import { LOCAL_BUILD_COMPATIBILITY_CONTRACT } from '../../../shared/local-build-compatibility-contract' +import type { DeveloperPermissionStatus } from '../../../shared/developer-permissions-types' +import type { EventProps } from '../../../shared/telemetry-events' +import { msSinceWakeSignal } from '../../power/wake-signal-clock' +import { isTelemetryEnabled, track } from '../../telemetry/client' +import { inspectMacProcessCodeIdentity } from '../executable-host/macos/daemon-mac-code-identity' +import { getLastMacDaemonForkReason } from '../executable-host/macos/macos-daemon-fork-reason' +import type { DaemonEndpointIdentity } from '../protocol/daemon-hello-protocol' +import { readTccFolderState, type TccFolderState } from './tcc-denial-folder-state' +import { readTccLogVerdict } from './tcc-denial-log-verdict' +import { checkHelperSignatures } from './tcc-denial-signature-checks' + +const MAX_TRACKED_KEYS = 256 +const CLASS_ROOT_NAME: Record = { + documents: 'Documents', + desktop: 'Desktop', + downloads: 'Downloads' +} +const UNKNOWN_FOLDER_STATE: TccFolderState = { + dataless: 'unknown', + icloud: 'unknown', + volumeType: 'unknown' +} +const APP_STARTED_AT_MS = Date.now() - process.uptime() * 1_000 + +export type TccDenialCaptureStart = { + key: string + identity: DaemonEndpointIdentity + cwdClass: MacTccFolderClass + denialAtMs: number + folderState: TccFolderState + /** Taken at the denial, before any await, so later reads and wakes cannot leak in. */ + context: Pick< + EventProps<'daemon_pty_cwd_denial_capture'>, + | 'good_reads_bucket' + | 'since_good_read_bucket' + | 'since_resume_bucket' + | 'since_unlock_bucket' + | 'since_user_active_bucket' + > +} + +type GoodReads = { count: number; lastAtMs: number } + +const goodReads = new Map() +const claimedKeys = new Map() + +function captureKey(identity: DaemonEndpointIdentity, cwdClass: MacTccFolderClass): string { + return `${identity.pid}:${identity.startedAtMs}:${identity.launchNonce}:${cwdClass}` +} + +function remember(map: Map, key: string, value: V): void { + map.delete(key) + map.set(key, value) + // Bounded memory: past 256 helper-class keys the oldest may be captured once more. + for (const oldest of map.keys()) { + if (map.size <= MAX_TRACKED_KEYS) { + break + } + map.delete(oldest) + } +} + +class TelemetryOptedOutError extends Error {} + +/** Opted-out users never have their log read or checks run; asked again before every stage. */ +function requireTelemetryConsent(): void { + if (!isTelemetryEnabled()) { + throw new TelemetryOptedOutError('telemetry is off') + } +} + +/** Every capture subprocess runs through this, so a stage added later cannot skip consent. */ +export const runProcessWithTelemetryConsent: typeof runProcess = (spec, outputCapture) => { + try { + requireTelemetryConsent() + } catch (error) { + return Promise.reject(error) + } + return runProcess(spec, outputCapture) +} + +/** Earlier good reads are the "was readable, then flipped" evidence for this helper. */ +export function noteHelperFolderReadable( + identity: DaemonEndpointIdentity | null, + cwdClass: MacTccFolderClass, + nowMs: number = Date.now() +): void { + if (!identity) { + return + } + const key = captureKey(identity, cwdClass) + remember(goodReads, key, { count: (goodReads.get(key)?.count ?? 0) + 1, lastAtMs: nowMs }) +} + +/** Read the folder state before the app-side opendir; null once this helper and class were captured. */ +export async function prepareTccDenialCapture( + identity: DaemonEndpointIdentity | null, + cwdClass: MacTccFolderClass, + denialAtMs: number, + readFolderState: typeof readTccFolderState = readTccFolderState +): Promise { + // Opted-out users never have their log read or these checks run. + if (!identity || !isTelemetryEnabled()) { + return null + } + const key = captureKey(identity, cwdClass) + if (claimedKeys.has(key)) { + return null + } + // Claim before the first await: a burst of restored terminals in one folder probes once. + remember(claimedKeys, key, 'preparing') + const reads = goodReads.get(key) + const context: TccDenialCaptureStart['context'] = { + good_reads_bucket: bucketGoodReadCount(reads?.count ?? 0), + since_good_read_bucket: bucketElapsed(reads ? denialAtMs - reads.lastAtMs : null), + since_resume_bucket: bucketElapsed(msSinceWakeSignal('resume', denialAtMs)), + since_unlock_bucket: bucketElapsed(msSinceWakeSignal('unlock-screen', denialAtMs)), + since_user_active_bucket: bucketElapsed(msSinceWakeSignal('user-did-become-active', denialAtMs)) + } + const folderState = await readFolderState( + join(homedir(), CLASS_ROOT_NAME[cwdClass]), + cwdClass, + runProcessWithTelemetryConsent + ).catch(() => UNKNOWN_FOLDER_STATE) + if (!isTelemetryEnabled()) { + claimedKeys.delete(key) + return null + } + return { key, identity, cwdClass, denialAtMs, folderState, context } +} + +/** The app was refused too, so nothing diverged: let a later denial on this helper capture. */ +export function releaseTccDenialCapture(start: TccDenialCaptureStart): void { + if (claimedKeys.get(start.key) === 'preparing') { + claimedKeys.delete(start.key) + } +} + +async function readFullDiskAccess(): Promise { + let status: DeveloperPermissionStatus + try { + // Lazy: the status reader reaches the provider modules that import this capture. + const { getTerminalHostsFullDiskAccessStatus } = + await import('./daemon-full-disk-access-status') + // The import is an await: consent may have gone, and the reader probes the host and disk. + requireTelemetryConsent() + status = await getTerminalHostsFullDiskAccessStatus() + } catch (error) { + if (error instanceof TelemetryOptedOutError) { + throw error + } + return 'unknown' + } + return status === 'granted' || status === 'denied' || status === 'unsupported' + ? status + : 'unknown' +} + +function helperOriginOf(identity: DaemonEndpointIdentity): HelperOrigin { + if (!Number.isFinite(identity.startedAtMs) || identity.startedAtMs <= 0) { + return 'unknown' + } + return identity.startedAtMs < APP_STARTED_AT_MS ? 'adopted' : 'spawned' +} + +/** Never throws, never blocks a spawn; at most one capture per helper incarnation and folder class. */ +export async function runTccDenialCapture(start: TccDenialCaptureStart): Promise { + if (claimedKeys.get(start.key) === 'captured') { + return + } + remember(claimedKeys, start.key, 'captured') + try { + const { identity, cwdClass, denialAtMs, folderState } = start + // Consent can be withdrawn at any await (the app-side read can wait on a prompt), so every + // stage asks again; the shared identity probe is gated here, the rest through the runner. + requireTelemetryConsent() + const helperPath = + (await inspectMacProcessCodeIdentity(identity.pid)).executablePath ?? + identity.spawnerExecPath ?? + null + requireTelemetryConsent() + const [log, signatures, fullDiskAccess] = await Promise.all([ + readTccLogVerdict( + denialAtMs, + { + helperPid: identity.pid, + helperPath, + appId: LOCAL_BUILD_COMPATIBILITY_CONTRACT.appId, + cwdClass + }, + runProcessWithTelemetryConsent + ), + checkHelperSignatures(identity.pid, helperPath, runProcessWithTelemetryConsent), + readFullDiskAccess() + ]) + requireTelemetryConsent() + const origin = helperOriginOf(identity) + const props: EventProps<'daemon_pty_cwd_denial_capture'> = { + cwd_class: cwdClass, + log_access: log.logAccess, + tccd_asked: log.tccdAsked, + auth_value: log.authValue, + auth_reason: log.authReason, + service: log.service, + requirement_failure: log.requirementFailure, + kernel_deny: log.kernelDeny, + live_signature: signatures.live, + disk_copy_signature: signatures.diskCopy, + helper_age_bucket: bucketHelperAge( + identity.startedAtMs > 0 ? denialAtMs - identity.startedAtMs : null + ), + helper_origin: origin, + ...start.context, + full_disk_access: fullDiskAccess, + // An adopted helper was launched by an earlier app process whose reason is gone. + fork_fallback: origin === 'spawned' ? (getLastMacDaemonForkReason() ?? 'none') : 'unknown', + folder_dataless: folderState.dataless, + folder_icloud: folderState.icloud, + volume_type: folderState.volumeType + } + track('daemon_pty_cwd_denial_capture', props) + } catch (error) { + // An opted-out capture was not taken, so a later denial may still capture after opt-in. + if (error instanceof TelemetryOptedOutError) { + claimedKeys.delete(start.key) + } + // Diagnostics are best-effort; a failed capture must not reach terminal creation. + } +} + +export function resetTccDenialCaptureForTests(): void { + goodReads.clear() + claimedKeys.clear() +} diff --git a/src/main/daemon/permissions/tcc-denial-folder-state.ts b/src/main/daemon/permissions/tcc-denial-folder-state.ts new file mode 100644 index 00000000000..f7cb28213e8 --- /dev/null +++ b/src/main/daemon/permissions/tcc-denial-folder-state.ts @@ -0,0 +1,137 @@ +// Folder-class root state read before the app's own opendir can materialize or change it. + +import { realpath } from 'node:fs/promises' +import type { runProcess } from '@orca/process-host' +import type { MacTccFolderClass } from '../../../shared/daemon-adoption-telemetry' +import type { + FolderFlag, + IcloudManaged, + VolumeType +} from '../../../shared/daemon-tcc-denial-capture' + +const PROBE_TIMEOUT_MS = 1_500 +// sys/stat.h SF_DATALESS: the entry's contents live in a file provider, not on disk. +const SF_DATALESS = 0x40000000 +const KNOWN_VOLUME_TYPES: readonly VolumeType[] = [ + 'apfs', + 'hfs', + 'smbfs', + 'nfs', + 'afpfs', + 'webdav', + 'msdos', + 'exfat' +] +// Finder's switches for iCloud "Desktop & Documents Folders". +const ICLOUD_FINDER_KEY: Partial> = { + desktop: 'FXICloudDriveDesktop', + documents: 'FXICloudDriveDocuments' +} + +export type TccFolderState = { + dataless: FolderFlag + icloud: IcloudManaged + volumeType: VolumeType +} + +type ProbeOutput = { code: number | null; timedOut: boolean; stdout: string; stderr: string } + +export function classifyStatFlags(output: ProbeOutput | null): FolderFlag { + if (!output || output.timedOut || output.code !== 0) { + return 'unknown' + } + const flags = Number.parseInt(output.stdout.trim(), 16) + if (!Number.isFinite(flags)) { + return 'unknown' + } + return (flags & SF_DATALESS) !== 0 ? 'yes' : 'no' +} + +export function classifyIcloudSetting(output: ProbeOutput | null): IcloudManaged { + if (!output || output.timedOut) { + return 'unknown' + } + if (output.code === 0) { + const value = output.stdout.trim() + return value === '1' ? 'yes' : value === '0' ? 'no' : 'unknown' + } + // A key that was never written means the user never turned the sync on. + return /does not exist/.test(output.stderr) ? 'no' : 'unknown' +} + +/** Volume type of the longest mount point containing `path`, from `/sbin/mount` output. */ +export function classifyVolumeType(output: ProbeOutput | null, path: string): VolumeType { + if (!output || output.timedOut || output.code !== 0) { + return 'unknown' + } + let best: { mountPoint: string; type: string } | null = null + for (const line of output.stdout.split('\n')) { + const match = / on (.+) \(([^,)]+)/.exec(line) + if (!match) { + continue + } + const [, mountPoint, type] = match + const contains = mountPoint === '/' || path === mountPoint || path.startsWith(`${mountPoint}/`) + if (contains && (!best || mountPoint.length > best.mountPoint.length)) { + best = { mountPoint, type } + } + } + if (!best) { + return 'unknown' + } + return KNOWN_VOLUME_TYPES.find((type) => type === best.type) ?? 'other' +} + +async function probe( + program: string, + args: string[], + run: typeof runProcess +): Promise { + try { + return await run({ + program, + args, + timeoutMs: PROBE_TIMEOUT_MS, + maxOutputBytes: 256 * 1024, + killOnOutputLimit: true, + stdio: ['ignore', 'pipe', 'pipe'] + }) + } catch { + return null + } +} + +/** A root relocated to a hung volume must not hold the denial notice behind it. */ +async function resolveWithin(path: string, timeoutMs: number): Promise { + let timer: NodeJS.Timeout | undefined + const deadline = new Promise((resolve) => { + timer = setTimeout(() => resolve(path), timeoutMs) + timer.unref?.() + }) + try { + return await Promise.race([realpath(path).catch(() => path), deadline]) + } finally { + clearTimeout(timer) + } +} + +/** Metadata of the class root only: none of these read the protected folder's contents. */ +export async function readTccFolderState( + classRoot: string, + cwdClass: MacTccFolderClass, + run: typeof runProcess +): Promise { + // A relocated class root (symlink to another volume) is what decides the volume. + const resolved = await resolveWithin(classRoot, PROBE_TIMEOUT_MS) + const icloudKey = ICLOUD_FINDER_KEY[cwdClass] + const [flags, icloud, mounts] = await Promise.all([ + probe('/usr/bin/stat', ['-f', '%Xf', resolved], run), + icloudKey ? probe('/usr/bin/defaults', ['read', 'com.apple.finder', icloudKey], run) : null, + probe('/sbin/mount', [], run) + ]) + return { + dataless: classifyStatFlags(flags), + icloud: icloudKey ? classifyIcloudSetting(icloud) : 'not_applicable', + volumeType: classifyVolumeType(mounts, resolved) + } +} diff --git a/src/main/daemon/permissions/tcc-denial-log-verdict.ts b/src/main/daemon/permissions/tcc-denial-log-verdict.ts new file mode 100644 index 00000000000..8ea9f65354f --- /dev/null +++ b/src/main/daemon/permissions/tcc-denial-log-verdict.ts @@ -0,0 +1,253 @@ +// macOS's own record of a terminal-host folder denial: tccd decisions and kernel sandbox denies. + +import type { runProcess } from '@orca/process-host' +import { z } from 'zod' +import type { MacTccFolderClass } from '../../../shared/daemon-adoption-telemetry' +import type { + TccAsked, + TccAuthReason, + TccAuthValue, + TccLogAccess, + RequirementFailure, + TccService, + YesNo +} from '../../../shared/daemon-tcc-denial-capture' +import { TCC_AUTH_REASONS } from '../../../shared/daemon-tcc-denial-capture' + +// A prompted request logs its attribution long before its result; 30 s keeps both in one window. +const LOG_LOOKBACK_MS = 30_000 +// End near the denial so later requests of the same class cannot replace its verdict. +const LOG_LOOKAHEAD_MS = 2_000 +const LOG_TIMEOUT_MS = 5_000 +const LOG_MAX_OUTPUT_BYTES = 8 * 1024 * 1024 +const LOG_PREDICATE = '(subsystem == "com.apple.TCC") OR (sender == "Sandbox")' + +export type TccLogVerdict = { + logAccess: TccLogAccess + tccdAsked: TccAsked + authValue: TccAuthValue + authReason: TccAuthReason + service: TccService + requirementFailure: RequirementFailure + kernelDeny: YesNo +} + +export type TccLogSubject = { + helperPid: number + /** Helper executable path; tccd names it in attribution lines. */ + helperPath: string | null + /** Code identifier tccd reports for both the app and its helper. */ + appId: string + cwdClass: MacTccFolderClass +} + +type LogRecord = { message: string; msgId: string | null } + +type MsgGroup = { + relevant: boolean + service: string | null + result: { authValue: string; authReason: string } | null +} + +const LOG_ENTRY_SCHEMA = z.object({ + eventMessage: z.string() +}) +const MSG_ID_PATTERN = /\bmsgID=(\d+\.\d+)/ +const ANY_PID_PATTERN = /\bpid=\d+/ +const SERVICE_PATTERN = /\bservice=kTCCService(\w+)/ +const RESULT_PATTERN = /AUTHREQ_RESULT: .*\bauthValue=(\d+), authReason=(\d+)/ +const REQUIREMENT_FAILURE_PATTERN = /failed to match existing code requirement/i +const STATIC_REQUIREMENT_CHECK_PATTERN = + /matchesCodeRequirement:\]: .* from (.+?) : (.+); status: (-?\d+)/ + +const SERVICE_BY_NAME: Record = { + SystemPolicyDocumentsFolder: 'documents', + SystemPolicyDesktopFolder: 'desktop', + SystemPolicyDownloadsFolder: 'downloads', + SystemPolicyAllFiles: 'all_files' +} + +const AUTH_VALUE_BY_NUMBER: Record = { + '0': 'denied', + '1': 'unknown', + '2': 'allowed', + '3': 'limited' +} + +function toRecord(rawLine: string): LogRecord | null { + let parsed: unknown + try { + parsed = JSON.parse(rawLine) + } catch { + return null + } + const entry = LOG_ENTRY_SCHEMA.safeParse(parsed) + if (!entry.success) { + return null + } + const message = entry.data.eventMessage + return { message, msgId: MSG_ID_PATTERN.exec(message)?.[1] ?? null } +} + +/** A failed requirement check for this code identity; the app and its hosts share it. */ +function isIdentityRequirementFailure(message: string, subject: TccLogSubject): boolean { + if (REQUIREMENT_FAILURE_PATTERN.test(message)) { + return ( + new RegExp(`\\bsubject ${subject.appId.replace(/\./g, '\\.')}\\b`).test(message) || + (!!subject.helperPath && message.includes(subject.helperPath)) + ) + } + // tccd also checks "anchor apple" on every client; only a failed stored requirement counts. + const check = STATIC_REQUIREMENT_CHECK_PATTERN.exec(message) + return ( + !!check && + (check[1] === subject.appId || check[1] === subject.helperPath) && + check[2].trim() !== 'anchor apple' && + check[3] !== '0' + ) +} + +function authReasonOf(raw: string | undefined): TccAuthReason { + if (raw === undefined) { + return 'none' + } + return TCC_AUTH_REASONS.find((reason) => reason === raw) ?? 'other' +} + +/** Derive enums from lines about the helper; raw `log show --style ndjson` text is never kept. */ +export function parseTccLogVerdict( + stdout: string, + subject: TccLogSubject, + logAccess: TccLogAccess +): TccLogVerdict { + const pidPattern = new RegExp(`\\bpid=${subject.helperPid}\\b`) + const pathNeedles = subject.helperPath ? [subject.helperPath, encodeURI(subject.helperPath)] : [] + // An earlier incarnation of the same copy shares the path, so an explicit pid outranks it. + const namesHelper = (message: string): boolean => + pidPattern.test(message) || + (!ANY_PID_PATTERN.test(message) && pathNeedles.some((needle) => message.includes(needle))) + // Sandbox coalesces repeats as "N duplicate reports for Sandbox: …"; folder denies add "System Policy:". + const sandboxDeny = new RegExp( + `Sandbox: [^\\n]*?\\(${subject.helperPid}\\) (?:System Policy: )?deny\\(` + ) + + const records = stdout + .split('\n') + .map(toRecord) + .filter((record): record is LogRecord => record !== null) + const groups = new Map() + for (const record of records) { + if (!record.msgId) { + continue + } + const group = groups.get(record.msgId) ?? { relevant: false, service: null, result: null } + groups.set(record.msgId, group) + group.relevant ||= namesHelper(record.message) + group.service ??= SERVICE_PATTERN.exec(record.message)?.[1] ?? null + const result = RESULT_PATTERN.exec(record.message) + if (result) { + group.result = { authValue: result[1], authReason: result[2] } + } + } + + let kernelDeny = false + let requirementFailure: RequirementFailure = 'no' + for (const record of records) { + const inRelevantGroup = record.msgId !== null && groups.get(record.msgId)?.relevant === true + const deny = sandboxDeny.test(record.message) + const helperRecord = inRelevantGroup || namesHelper(record.message) + const requirement = isIdentityRequirementFailure(record.message, subject) + if (!helperRecord && !deny && !requirement) { + continue + } + kernelDeny ||= deny + if (requirement) { + // Identifier-only evidence may be the app's or another host's check, so it is not blamed here. + requirementFailure = helperRecord + ? 'yes' + : requirementFailure === 'yes' + ? 'yes' + : 'uncorrelated' + } + } + + const relevantGroups = [...groups.values()].filter((group) => group.relevant) + const decided = relevantGroups.filter((group) => group.result !== null) + // Overlapping requests can resolve out of order; a later "allowed" must not mask a denial. + const denied = decided.filter((group) => group.result?.authValue === '0') + const chosen = + denied.findLast( + (group) => group.service !== null && SERVICE_BY_NAME[group.service] === subject.cwdClass + ) ?? + decided.findLast( + (group) => group.service !== null && SERVICE_BY_NAME[group.service] === subject.cwdClass + ) ?? + decided.at(-1) ?? + relevantGroups.at(-1) + const serviceName = chosen?.service ?? null + + return { + logAccess, + // "no" means no request tied to this helper in the window, and only from a complete read. + tccdAsked: relevantGroups.length > 0 ? 'yes' : logAccess === 'ok' ? 'no' : 'unknown', + authValue: chosen?.result ? (AUTH_VALUE_BY_NUMBER[chosen.result.authValue] ?? 'other') : 'none', + authReason: authReasonOf(chosen?.result?.authReason), + service: serviceName === null ? 'none' : (SERVICE_BY_NAME[serviceName] ?? 'other'), + requirementFailure, + kernelDeny: kernelDeny ? 'yes' : 'no' + } +} + +function formatLogTime(atMs: number): string { + // `log show --start/--end` accept "YYYY-MM-DD HH:MM:SS+ZZZZ". + const iso = new Date(atMs).toISOString() + return `${iso.slice(0, 10)} ${iso.slice(11, 19)}+0000` +} + +function classifyLogFailure(output: string): TccLogAccess { + return /must be admin|not permitted|permission denied/i.test(output) ? 'denied' : 'error' +} + +/** Bounded `log show` around the denial; never throws. */ +export async function readTccLogVerdict( + denialAtMs: number, + subject: TccLogSubject, + run: typeof runProcess +): Promise { + let logAccess: TccLogAccess + let stdout = '' + try { + const result = await run({ + program: '/usr/bin/log', + args: [ + 'show', + // Requirement checks (matchesCodeRequirement) are Info records. + '--info', + '--style', + 'ndjson', + '--start', + formatLogTime(denialAtMs - LOG_LOOKBACK_MS), + '--end', + formatLogTime(denialAtMs + LOG_LOOKAHEAD_MS), + '--predicate', + LOG_PREDICATE + ], + timeoutMs: LOG_TIMEOUT_MS, + maxOutputBytes: LOG_MAX_OUTPUT_BYTES, + killOnOutputLimit: true, + stdio: ['ignore', 'pipe', 'pipe'] + }) + stdout = result.stdout + logAccess = result.timedOut + ? 'timeout' + : result.outputTruncated + ? 'truncated' + : result.code === 0 + ? 'ok' + : classifyLogFailure(`${result.stderr}\n${result.stdout}`) + } catch { + logAccess = 'error' + } + // A killed `log show` still printed complete lines before its deadline. + return parseTccLogVerdict(stdout, subject, logAccess) +} diff --git a/src/main/daemon/permissions/tcc-denial-signature-checks.ts b/src/main/daemon/permissions/tcc-denial-signature-checks.ts new file mode 100644 index 00000000000..68c93cc42f2 --- /dev/null +++ b/src/main/daemon/permissions/tcc-denial-signature-checks.ts @@ -0,0 +1,123 @@ +// Whether the live helper and its on-disk copy still carry the app's signature at a denial. + +import type { runProcess } from '@orca/process-host' +import type { SignatureCheck } from '../../../shared/daemon-tcc-denial-capture' + +const CODESIGN = '/usr/bin/codesign' +const CODESIGN_TIMEOUT_MS = 5_000 +const CODESIGN_MAX_OUTPUT_BYTES = 64 * 1024 + +export type HelperSignatureChecks = { + live: SignatureCheck + diskCopy: SignatureCheck +} + +type CodesignRun = { code: number | null; timedOut: boolean; output: string } | null + +/** `…/X.app/Contents/MacOS/x` → `…/X.app`; null when the path is not inside a bundle. */ +export function bundlePathOf(executablePath: string | null): string | null { + if (!executablePath) { + return null + } + const index = executablePath.lastIndexOf('.app/Contents/') + return index === -1 ? null : executablePath.slice(0, index + '.app'.length) +} + +/** codesign exits 1 on a failed verify and 3 on an unmet requirement; a vanished pid proves nothing. */ +export function classifyCodesignVerify(run: CodesignRun): SignatureCheck { + if ( + !run || + run.timedOut || + /no guest with the requested attributes|No such process/i.test(run.output) + ) { + return 'error' + } + if (run.code === 0) { + return 'pass' + } + return run.code === 1 || run.code === 3 ? 'fail' : 'error' +} + +export function designatedRequirementOf(run: CodesignRun): string | null { + if (!run || run.timedOut || run.code !== 0) { + return null + } + return /designated => (.+)/.exec(run.output)?.[1]?.trim() ?? null +} + +async function codesign(args: string[], run: typeof runProcess): Promise { + try { + const result = await run({ + program: CODESIGN, + args, + timeoutMs: CODESIGN_TIMEOUT_MS, + maxOutputBytes: CODESIGN_MAX_OUTPUT_BYTES, + killOnOutputLimit: true, + stdio: ['ignore', 'pipe', 'pipe'] + }) + return { + code: result.code, + timedOut: result.timedOut, + output: `${result.stderr}\n${result.stdout}` + } + } catch { + return null + } +} + +let appRequirement: Promise | null = null + +/** The running app's designated requirement; read once per app process. */ +function appDesignatedRequirement(run: typeof runProcess): Promise { + const appBundle = bundlePathOf(process.execPath) + if (!appBundle) { + return Promise.resolve(null) + } + appRequirement ??= codesign(['--display', '-r-', appBundle], run).then((result) => { + const requirement = designatedRequirementOf(result) + // Retry a failed read at the next denial instead of pinning the failure. + if (requirement === null) { + appRequirement = null + } + return requirement + }) + return appRequirement +} + +// `codesign --verify -R … +pid` fails with EINVAL whenever the requirement matches (macOS 27), so +// verify the live code dynamically and compare its designated requirement with the app's instead. +async function checkLiveHelper(helperPid: number, run: typeof runProcess): Promise { + const [verify, liveDisplay, expected] = await Promise.all([ + codesign(['--verify', '--strict', `+${helperPid}`], run), + codesign(['--display', '-r-', `+${helperPid}`], run), + appDesignatedRequirement(run) + ]) + const verdict = classifyCodesignVerify(verify) + if (verdict !== 'pass') { + return verdict + } + const live = designatedRequirementOf(liveDisplay) + if (!live || !expected) { + return 'error' + } + return live === expected ? 'pass' : 'fail' +} + +export async function checkHelperSignatures( + helperPid: number, + helperExecutablePath: string | null, + run: typeof runProcess +): Promise { + const helperBundle = bundlePathOf(helperExecutablePath) + const [live, diskCopy] = await Promise.all([ + checkLiveHelper(helperPid, run), + helperBundle + ? codesign(['--verify', '--strict', helperBundle], run).then(classifyCodesignVerify) + : Promise.resolve('error') + ]) + return { live, diskCopy } +} + +export function resetHelperSignatureChecksForTests(): void { + appRequirement = null +} diff --git a/src/main/daemon/permissions/testing/tcc-denial-capture.test.ts b/src/main/daemon/permissions/testing/tcc-denial-capture.test.ts new file mode 100644 index 00000000000..4cca5c77c96 --- /dev/null +++ b/src/main/daemon/permissions/testing/tcc-denial-capture.test.ts @@ -0,0 +1,223 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +const mocks = vi.hoisted(() => ({ + track: vi.fn(), + telemetryEnabled: vi.fn(), + logVerdict: vi.fn(), + signatures: vi.fn(), + fullDiskAccess: vi.fn(), + codeIdentity: vi.fn(), + runProcess: vi.fn() +})) +const trackMock = mocks.track +const telemetryEnabledMock = mocks.telemetryEnabled +const logVerdictMock = mocks.logVerdict +vi.mock('@orca/process-host', () => ({ runProcess: mocks.runProcess })) +vi.mock('../../../telemetry/client', () => ({ + track: mocks.track, + isTelemetryEnabled: mocks.telemetryEnabled +})) +vi.mock('../tcc-denial-log-verdict', () => ({ readTccLogVerdict: mocks.logVerdict })) +vi.mock('../tcc-denial-signature-checks', () => ({ checkHelperSignatures: mocks.signatures })) +vi.mock('../daemon-full-disk-access-status', () => ({ + getTerminalHostsFullDiskAccessStatus: mocks.fullDiskAccess +})) +vi.mock('../../executable-host/macos/daemon-mac-code-identity', () => ({ + inspectMacProcessCodeIdentity: mocks.codeIdentity +})) + +import type { DaemonEndpointIdentity } from '../../protocol/daemon-hello-protocol' +import type { readTccFolderState } from '../tcc-denial-folder-state' +import { + noteHelperFolderReadable, + prepareTccDenialCapture, + releaseTccDenialCapture, + resetTccDenialCaptureForTests, + runTccDenialCapture +} from '../tcc-denial-capture' + +const HELPER: DaemonEndpointIdentity = { pid: 1206, startedAtMs: 1_000, launchNonce: 'n1' } +const FOLDER = async () => ({ dataless: 'yes', icloud: 'yes', volumeType: 'apfs' }) as const + +beforeEach(() => { + resetTccDenialCaptureForTests() + trackMock.mockReset() + telemetryEnabledMock.mockReset().mockReturnValue(true) + mocks.signatures.mockReset().mockResolvedValue({ live: 'pass', diskCopy: 'fail' }) + mocks.fullDiskAccess.mockReset().mockResolvedValue('denied') + mocks.codeIdentity.mockReset().mockResolvedValue({ identity: 'resolved', executablePath: null }) + mocks.runProcess.mockReset().mockResolvedValue({ + code: 0, + signal: null, + stdout: '', + stderr: '', + timedOut: false + }) + logVerdictMock.mockReset().mockResolvedValue({ + logAccess: 'ok', + tccdAsked: 'no', + authValue: 'none', + authReason: 'none', + service: 'none', + requirementFailure: 'no', + kernelDeny: 'yes' + }) +}) + +describe('tcc denial capture', () => { + it('runs no probe or log read for a user who opted out of telemetry', async () => { + telemetryEnabledMock.mockReturnValue(false) + const readFolder = vi.fn(FOLDER) + expect(await prepareTccDenialCapture(HELPER, 'documents', 5_000, readFolder)).toBeNull() + expect(readFolder).not.toHaveBeenCalled() + + // Opting out while the app-side read waits stops the capture before the log is read. + telemetryEnabledMock.mockReturnValue(true) + const start = (await prepareTccDenialCapture(HELPER, 'documents', 5_000, FOLDER))! + telemetryEnabledMock.mockReturnValue(false) + await runTccDenialCapture(start) + expect(logVerdictMock).not.toHaveBeenCalled() + expect(trackMock).not.toHaveBeenCalled() + }) + + // Opt out while each stage is in flight: nothing after it may start, and the claim is freed. + it.each([ + { + stage: 'identity probe', + probe: mocks.codeIdentity, + result: { identity: 'resolved', executablePath: null }, + skipped: [mocks.logVerdict, mocks.signatures, mocks.fullDiskAccess] + }, + { + // The access reader is lazily imported, so it starts after an await of its own. + stage: 'log reader, before the lazily imported access reader', + probe: mocks.logVerdict, + result: { + logAccess: 'ok', + tccdAsked: 'no', + authValue: 'none', + authReason: 'none', + service: 'none', + requirementFailure: 'no', + kernelDeny: 'no' + }, + skipped: [mocks.fullDiskAccess] + }, + { + stage: 'log, signature and access probes', + probe: mocks.signatures, + result: { live: 'pass', diskCopy: 'pass' }, + skipped: [] + } + ])('stops after the $stage when telemetry is turned off', async ({ probe, result, skipped }) => { + const start = (await prepareTccDenialCapture(HELPER, 'documents', 5_000, FOLDER))! + probe.mockImplementationOnce(async () => { + telemetryEnabledMock.mockReturnValue(false) + return result + }) + await runTccDenialCapture(start) + + for (const probe of skipped) { + expect(probe).not.toHaveBeenCalled() + } + expect(trackMock).not.toHaveBeenCalled() + telemetryEnabledMock.mockReturnValue(true) + expect(await prepareTccDenialCapture(HELPER, 'documents', 6_000, FOLDER)).not.toBeNull() + }) + + it('stops after the folder probes when telemetry is turned off', async () => { + const readFolder = vi.fn(async () => { + telemetryEnabledMock.mockReturnValue(false) + return FOLDER() + }) + expect(await prepareTccDenialCapture(HELPER, 'documents', 5_000, readFolder)).toBeNull() + telemetryEnabledMock.mockReturnValue(true) + expect(await prepareTccDenialCapture(HELPER, 'documents', 6_000, FOLDER)).not.toBeNull() + }) + + it('hands every probe a runner that refuses to spawn once telemetry is off', async () => { + const readFolder = vi.fn(FOLDER) + await prepareTccDenialCapture(HELPER, 'documents', 5_000, readFolder) + const runner = readFolder.mock.calls[0][2] + + await runner({ program: '/usr/bin/true' }) + telemetryEnabledMock.mockReturnValue(false) + await expect(runner({ program: '/usr/bin/true' })).rejects.toThrow('telemetry is off') + expect(mocks.runProcess).toHaveBeenCalledTimes(1) + }) + + it('captures once per helper incarnation and folder class', async () => { + const start = await prepareTccDenialCapture(HELPER, 'documents', 5_000, FOLDER) + expect(start).not.toBeNull() + await runTccDenialCapture(start!) + await runTccDenialCapture(start!) + + expect(trackMock).toHaveBeenCalledTimes(1) + expect(await prepareTccDenialCapture(HELPER, 'documents', 6_000, FOLDER)).toBeNull() + expect(await prepareTccDenialCapture(HELPER, 'desktop', 6_000, FOLDER)).not.toBeNull() + expect( + await prepareTccDenialCapture({ ...HELPER, launchNonce: 'n2' }, 'documents', 6_000, FOLDER) + ).not.toBeNull() + }) + + it('probes the folder once for concurrent denials, and again only after a release', async () => { + const readFolder = vi.fn(FOLDER) + const starts = await Promise.all( + Array.from({ length: 5 }, () => + prepareTccDenialCapture(HELPER, 'documents', 5_000, readFolder) + ) + ) + expect(readFolder).toHaveBeenCalledTimes(1) + expect(starts.filter((start) => start !== null)).toHaveLength(1) + + releaseTccDenialCapture(starts.find((start) => start !== null)!) + expect(await prepareTccDenialCapture(HELPER, 'documents', 6_000, readFolder)).not.toBeNull() + }) + + it('sends enums only, with earlier good reads of the same class on this helper', async () => { + noteHelperFolderReadable(HELPER, 'documents', 2_000) + noteHelperFolderReadable(HELPER, 'documents', 3_000) + noteHelperFolderReadable(HELPER, 'desktop', 4_500) + + await runTccDenialCapture((await prepareTccDenialCapture(HELPER, 'documents', 65_000, FOLDER))!) + + expect(trackMock).toHaveBeenCalledWith( + 'daemon_pty_cwd_denial_capture', + expect.objectContaining({ + cwd_class: 'documents', + kernel_deny: 'yes', + live_signature: 'pass', + disk_copy_signature: 'fail', + helper_age_bucket: '1-10m', + helper_origin: 'adopted', + good_reads_bucket: '2-5', + since_good_read_bucket: '1-5m', + full_disk_access: 'denied', + fork_fallback: 'unknown', + folder_dataless: 'yes', + folder_icloud: 'yes', + volume_type: 'apfs' + }) + ) + }) + + it('swallows a failing probe and does not retry the same helper and class', async () => { + logVerdictMock.mockRejectedValue(new Error('log exploded')) + const start = (await prepareTccDenialCapture(HELPER, 'downloads', 5_000, FOLDER))! + + await expect(runTccDenialCapture(start)).resolves.toBeUndefined() + expect(trackMock).not.toHaveBeenCalled() + expect(await prepareTccDenialCapture(HELPER, 'downloads', 6_000, FOLDER)).toBeNull() + }) + + it('still captures when the folder state cannot be read', async () => { + const start = await prepareTccDenialCapture(HELPER, 'desktop', 5_000, async () => { + throw new Error('stat failed') + }) + expect(start?.folderState).toEqual({ + dataless: 'unknown', + icloud: 'unknown', + volumeType: 'unknown' + }) + }) +}) diff --git a/src/main/daemon/permissions/testing/tcc-denial-folder-state.test.ts b/src/main/daemon/permissions/testing/tcc-denial-folder-state.test.ts new file mode 100644 index 00000000000..ff8c7a9aed5 --- /dev/null +++ b/src/main/daemon/permissions/testing/tcc-denial-folder-state.test.ts @@ -0,0 +1,51 @@ +import { describe, expect, it } from 'vitest' +import { + classifyIcloudSetting, + classifyStatFlags, + classifyVolumeType +} from '../tcc-denial-folder-state' + +function ok(stdout: string, code = 0, stderr = '') { + return { code, timedOut: false, stdout, stderr } +} + +describe('folder state enums', () => { + it('reads SF_DATALESS from `stat -f %Xf`', () => { + expect(classifyStatFlags(ok('40000000\n'))).toBe('yes') + expect(classifyStatFlags(ok('40000040\n'))).toBe('yes') + expect(classifyStatFlags(ok('0\n'))).toBe('no') + expect(classifyStatFlags(ok('', 1))).toBe('unknown') + }) + + it('treats a never-written Finder key as iCloud off', () => { + expect(classifyIcloudSetting(ok('1\n'))).toBe('yes') + expect(classifyIcloudSetting(ok('0\n'))).toBe('no') + expect( + classifyIcloudSetting( + ok( + '', + 1, + 'The domain/default pair of (com.apple.finder, FXICloudDriveDesktop) does not exist' + ) + ) + ).toBe('no') + expect(classifyIcloudSetting({ ...ok(''), timedOut: true })).toBe('unknown') + }) + + it('picks the longest mount point that contains the folder', () => { + const mounts = ok( + [ + '/dev/disk3s1s1 on / (apfs, sealed, local, read-only, journaled)', + '/dev/disk3s5 on /System/Volumes/Data (apfs, local, journaled, nobrowse, protect, root data)', + '//user@nas/home on /Volumes/home (smbfs, nodev, nosuid, mounted by user)', + '/dev/disk5s1 on /Volumes/home backup (exfat, local, nodev, nosuid, noowners)' + ].join('\n') + ) + expect(classifyVolumeType(mounts, '/Volumes/home/Documents')).toBe('smbfs') + expect(classifyVolumeType(mounts, '/Volumes/home backup/Documents')).toBe('exfat') + expect(classifyVolumeType(mounts, '/Users/user/Documents')).toBe('apfs') + expect( + classifyVolumeType(ok('map auto_home on /System/Volumes/Data/home (autofs)'), '/x') + ).toBe('unknown') + }) +}) diff --git a/src/main/daemon/permissions/testing/tcc-denial-log-verdict.test.ts b/src/main/daemon/permissions/testing/tcc-denial-log-verdict.test.ts new file mode 100644 index 00000000000..d9c39865ba0 --- /dev/null +++ b/src/main/daemon/permissions/testing/tcc-denial-log-verdict.test.ts @@ -0,0 +1,280 @@ +import { describe, expect, it } from 'vitest' +import { runProcess } from '@orca/process-host' +import { parseTccLogVerdict, readTccLogVerdict } from '../tcc-denial-log-verdict' + +// Real tccd lines from a macOS 27.2 VM (usernames redacted), as `log show --style ndjson` emits them. +const HELPER = + '/Users/user/Library/Application Support/orca/daemon-host/macos/terminal-host-57202dec6187e819396a5a0ab07c2932bf5a00fe/app.noindex/Orca Terminal Host.app/Contents/MacOS/orca-terminal-host' +const TCCD = '/System/Library/PrivateFrameworks/TCC.framework/Support/tccd' +const SANDBOXD = '/usr/libexec/sandboxd' +const DENIED_AT_MS = Date.UTC(2026, 10, 13, 20, 24, 30) + +function line(timestamp: string, image: string, pid: number, eventMessage: string): string { + return JSON.stringify({ + timestamp: `2026-11-13 ${timestamp}+0000`, + processImagePath: image, + processID: pid, + subsystem: 'com.apple.TCC', + eventMessage + }) +} + +const HELPER_DOWNLOADS_REFUSED = [ + line( + '20:24:20.457760', + SANDBOXD, + 153, + 'SEND: 0/7 synchronous to com.apple.tccd: request: msgID=153.1126, function=TCCAccessRequest, service=kTCCServiceSystemPolicyDownloadsFolder,' + ), + line( + '20:24:20.458006', + TCCD, + 350, + 'AUTHREQ_CTX: msgID=153.1126, function=TCCAccessRequest, service=kTCCServiceSystemPolicyDownloadsFolder, preflight=no, query=1, client_dict=(null), daemon_dict=' + ), + line( + '20:24:20.458018', + TCCD, + 350, + `AUTHREQ_ATTRIBUTION: msgID=153.1126, attribution={accessing={TCCDProcess: identifier=com.stablyai.orca, pid=1206, auid=501, euid=501, binary_path=${HELPER}}, requesting={TCCDProcess: identifier=com.apple.sandboxd, pid=153, auid=0, euid=0, binary_path=/usr/libexec/sandboxd}, },` + ), + line( + '20:24:49.917729', + TCCD, + 350, + 'AUTHREQ_RESULT: msgID=153.1126, authValue=0, authReason=2, authVersion=1, desired_auth=0, error=(null),' + ) +] + +// The main app's own refusal: same identifier, different pid; it must not count for the helper. +const APP_DOCUMENTS_REFUSED = [ + line( + '20:14:22.730560', + TCCD, + 350, + 'AUTHREQ_ATTRIBUTION: msgID=153.1044, attribution={responsible={TCCDProcess: identifier=com.stablyai.orca, pid=902, auid=501, euid=501, responsible_path=/Applications/Orca.app/Contents/MacOS/Orca, binary_path=/Applications/Orca.app/Contents/MacOS/Orca}, accessing={TCCDProcess: identifier=com.stablyai.orca.helper, pid=1959, auid=501, euid=501, binary_path=/Applications/Orca.app/Contents/Frameworks/Orca Helper.app/Contents/MacOS/Orca Helper}, requesting={TCCDProcess: identifier=com.apple.sandboxd, pid=153, auid=0, euid=0, binary_path=/usr/libexec/sandboxd}, },' + ), + line( + '20:14:22.738046', + TCCD, + 350, + 'AUTHREQ_RESULT: msgID=153.1044, authValue=0, authReason=2, authVersion=1, desired_auth=0, error=(null),' + ) +] + +const HELPER_DOCUMENTS_ALLOWED = [ + line( + '20:16:05.882048', + SANDBOXD, + 153, + 'SEND: 0/7 synchronous to com.apple.tccd: request: msgID=153.1060, function=TCCAccessRequest, service=kTCCServiceSystemPolicyDocumentsFolder,' + ), + line( + '20:16:05.882368', + TCCD, + 350, + `AUTHREQ_ATTRIBUTION: msgID=153.1060, attribution={accessing={TCCDProcess: identifier=com.stablyai.orca, pid=1206, auid=501, euid=501, binary_path=${HELPER}}, requesting={TCCDProcess: identifier=com.apple.sandboxd, pid=153, auid=0, euid=0, binary_path=/usr/libexec/sandboxd}, },` + ), + line( + '20:16:05.891091', + TCCD, + 350, + 'AUTHREQ_RESULT: msgID=153.1060, authValue=2, authReason=2, authVersion=1, desired_auth=0, error=(null),' + ) +] + +// Kernel sandbox report in the shape macOS 27 prints (seen for other processes on a real Mac). +const KERNEL_DENY = JSON.stringify({ + timestamp: '2026-11-13 20:24:20.457000+0000', + processImagePath: '/kernel', + processID: 0, + subsystem: '', + eventMessage: + '1 duplicate report for Sandbox: orca-terminal-host(1206) deny(1) file-read-data /Users/user/Downloads' +}) + +const SUBJECT = { + helperPid: 1206, + helperPath: HELPER, + appId: 'com.stablyai.orca', + cwdClass: 'downloads' as const +} + +function ndjson(lines: string[]): string { + return ['Filtering the log data using "subsystem == \\"com.apple.TCC\\""', ...lines].join('\n') +} + +describe('parseTccLogVerdict', () => { + it("reads the helper's own refused request and ignores the app's", () => { + const verdict = parseTccLogVerdict( + ndjson([...APP_DOCUMENTS_REFUSED, ...HELPER_DOWNLOADS_REFUSED, KERNEL_DENY]), + SUBJECT, + 'ok' + ) + + expect(verdict).toMatchObject({ + logAccess: 'ok', + tccdAsked: 'yes', + authValue: 'denied', + authReason: '2', + service: 'downloads', + requirementFailure: 'no', + kernelDeny: 'yes' + }) + }) + + it('reports no tccd request when only other processes asked', () => { + expect(parseTccLogVerdict(ndjson(APP_DOCUMENTS_REFUSED), SUBJECT, 'ok')).toMatchObject({ + tccdAsked: 'no', + authValue: 'none', + authReason: 'none', + service: 'none', + kernelDeny: 'no' + }) + }) + + it('prefers the decision for the denied folder class over a later one', () => { + const verdict = parseTccLogVerdict( + ndjson([...HELPER_DOWNLOADS_REFUSED, ...HELPER_DOCUMENTS_ALLOWED]), + SUBJECT, + 'ok' + ) + expect(verdict).toMatchObject({ service: 'downloads', authValue: 'denied' }) + + const documents = parseTccLogVerdict( + ndjson([...HELPER_DOWNLOADS_REFUSED, ...HELPER_DOCUMENTS_ALLOWED]), + { ...SUBJECT, cwdClass: 'documents' }, + 'ok' + ) + expect(documents).toMatchObject({ service: 'documents', authValue: 'allowed' }) + }) + + it('keeps a denial that resolves after a later request was allowed', () => { + const [send, ctx, attribution, deniedResult] = HELPER_DOWNLOADS_REFUSED + const later = [send, ctx, attribution].map((entry) => + entry.replaceAll('msgID=153.1126', 'msgID=153.1127') + ) + const allowedResult = deniedResult + .replace('msgID=153.1126', 'msgID=153.1127') + .replace('authValue=0', 'authValue=2') + // Request 1126 starts first, 1127 is allowed, then 1126 is refused. + const verdict = parseTccLogVerdict( + ndjson([send, ctx, attribution, ...later, allowedResult, deniedResult]), + SUBJECT, + 'ok' + ) + expect(verdict).toMatchObject({ authValue: 'denied', service: 'downloads' }) + }) + + it('flags a failed stored requirement but not the routine "anchor apple" check', () => { + const routine = line( + '20:06:45.144194', + TCCD, + 157, + '-[TCCDAccessIdentity matchesCodeRequirement:]: SecStaticCodeCheckValidity() static code (0x7cf9194300) from com.stablyai.orca : anchor apple; status: -67050' + ) + expect(parseTccLogVerdict(ndjson([routine]), SUBJECT, 'ok').requirementFailure).toBe('no') + + const failed = line( + '20:06:46.000000', + TCCD, + 157, + 'Failed to match existing code requirement for subject com.stablyai.orca and service kTCCServiceSystemPolicyDownloadsFolder' + ) + // The app and its hosts share the identifier, so this cannot be blamed on this helper. + expect(parseTccLogVerdict(ndjson([failed]), SUBJECT, 'ok').requirementFailure).toBe( + 'uncorrelated' + ) + + const helperCheck = line( + '20:06:47.000000', + TCCD, + 157, + `-[TCCDAccessIdentity matchesCodeRequirement:]: SecStaticCodeCheckValidity() static code (0x7cf9194300) from ${HELPER} : identifier "com.stablyai.orca" and anchor apple generic; status: -67050` + ) + expect( + parseTccLogVerdict(ndjson([failed, helperCheck]), SUBJECT, 'ok').requirementFailure + ).toBe('yes') + }) + + it('ignores requirement failures for other apps', () => { + const other = line( + '20:06:46.000000', + TCCD, + 157, + 'Failed to match existing code requirement for subject org.example.other and service kTCCServiceSystemPolicyDownloadsFolder' + ) + expect(parseTccLogVerdict(ndjson([other]), SUBJECT, 'ok')).toMatchObject({ + requirementFailure: 'no' + }) + }) + + it('does not credit an earlier helper on the same copy path to this one', () => { + const earlierHelper = HELPER_DOWNLOADS_REFUSED.map((entry) => + entry.replace('pid=1206', 'pid=1100') + ) + expect(parseTccLogVerdict(ndjson(earlierHelper), SUBJECT, 'ok')).toMatchObject({ + tccdAsked: 'no', + authValue: 'none' + }) + }) + + it('leaves tccd_asked unknown when the log was unreadable or cut short', () => { + expect(parseTccLogVerdict('', SUBJECT, 'denied').tccdAsked).toBe('unknown') + expect(parseTccLogVerdict(ndjson(APP_DOCUMENTS_REFUSED), SUBJECT, 'truncated').tccdAsked).toBe( + 'unknown' + ) + }) +}) + +describe('readTccLogVerdict', () => { + it('asks for Info records in a window that ends at the denial', async () => { + let args: readonly string[] = [] + await readTccLogVerdict(DENIED_AT_MS, SUBJECT, async (spec) => { + args = spec.args ?? [] + return { code: 0, signal: null, stdout: '', stderr: '', timedOut: false } + }) + expect(args).toEqual([ + 'show', + '--info', + '--style', + 'ndjson', + '--start', + '2026-11-13 20:24:00+0000', + '--end', + '2026-11-13 20:24:32+0000', + '--predicate', + '(subsystem == "com.apple.TCC") OR (sender == "Sandbox")' + ]) + }) + + it('keeps what a timed-out log printed and reports the timeout', async () => { + const verdict = await readTccLogVerdict(DENIED_AT_MS, SUBJECT, async () => ({ + code: null, + signal: 'SIGTERM', + stdout: ndjson(HELPER_DOWNLOADS_REFUSED), + stderr: '', + timedOut: true + })) + expect(verdict).toMatchObject({ logAccess: 'timeout', authValue: 'denied' }) + }) + + it.skipIf(process.platform === 'win32')( + 'kills a hung log reader at its deadline', + async () => { + // Same spec, but a child that never exits on its own. + const verdict = await readTccLogVerdict(DENIED_AT_MS, SUBJECT, (spec) => + runProcess({ ...spec, program: '/bin/sleep', args: ['60'] }) + ) + expect(verdict.logAccess).toBe('timeout') + }, + 15_000 + ) + + it('swallows a spawn failure', async () => { + const verdict = await readTccLogVerdict(DENIED_AT_MS, SUBJECT, async () => { + throw new Error('spawn ENOENT') + }) + expect(verdict).toMatchObject({ logAccess: 'error', tccdAsked: 'unknown' }) + }) +}) diff --git a/src/main/daemon/permissions/testing/tcc-denial-signature-checks.test.ts b/src/main/daemon/permissions/testing/tcc-denial-signature-checks.test.ts new file mode 100644 index 00000000000..4117127930d --- /dev/null +++ b/src/main/daemon/permissions/testing/tcc-denial-signature-checks.test.ts @@ -0,0 +1,88 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' +import type { runProcess } from '@orca/process-host' + +type ProcessSpec = Parameters[0] +type ProcessResult = Awaited> +import { + checkHelperSignatures, + resetHelperSignatureChecksForTests +} from '../tcc-denial-signature-checks' + +const APP_REQUIREMENT = + 'identifier "com.stablyai.orca" and anchor apple generic and certificate leaf[subject.OU] = "6CX3WHS9HZ"' +const HELPER = + '/Users/user/Library/Application Support/orca/daemon-host/macos/terminal-host-1/app.noindex/Orca Terminal Host.app/Contents/MacOS/orca-terminal-host' + +function result(code: number | null, stdout = '', stderr = '', timedOut = false): ProcessResult { + return { code, signal: null, stdout, stderr, timedOut } +} + +type Replies = { liveVerify: ProcessResult; liveRequirement: string; diskVerify: ProcessResult } + +function codesignFake(replies: Replies) { + return vi.fn(async (spec: ProcessSpec): Promise => { + const args = spec.args ?? [] + const target = args.at(-1) ?? '' + if (args[0] === '--display') { + return target.startsWith('+') + ? result(0, `designated => ${replies.liveRequirement}\n`) + : result(0, `designated => ${APP_REQUIREMENT}\n`) + } + return target.startsWith('+') ? replies.liveVerify : replies.diskVerify + }) +} + +beforeEach(() => { + resetHelperSignatureChecksForTests() + vi.spyOn(process, 'execPath', 'get').mockReturnValue('/Applications/Orca.app/Contents/MacOS/Orca') +}) + +describe('checkHelperSignatures', () => { + it('passes a live helper carrying the app requirement and verifies the copy bundle', async () => { + const run = codesignFake({ + liveVerify: result(0), + liveRequirement: APP_REQUIREMENT, + diskVerify: result(1, '', 'a sealed resource is missing or invalid') + }) + expect(await checkHelperSignatures(1206, HELPER, run)).toEqual({ + live: 'pass', + diskCopy: 'fail' + }) + expect(run).toHaveBeenCalledWith( + expect.objectContaining({ + args: [ + '--verify', + '--strict', + '/Users/user/Library/Application Support/orca/daemon-host/macos/terminal-host-1/app.noindex/Orca Terminal Host.app' + ] + }) + ) + }) + + it('fails a live helper whose requirement differs from the app', async () => { + const run = codesignFake({ + liveVerify: result(0), + liveRequirement: 'identifier "com.stablyai.orca" and anchor apple generic', + diskVerify: result(0) + }) + expect((await checkHelperSignatures(1206, HELPER, run)).live).toBe('fail') + }) + + it('reports error, not fail, for a gone pid, a timeout or a missing path', async () => { + const gone = codesignFake({ + liveVerify: result(1, '', '+1206: host has no guest with the requested attributes'), + liveRequirement: APP_REQUIREMENT, + diskVerify: result(null, '', '', true) + }) + expect(await checkHelperSignatures(1206, HELPER, gone)).toEqual({ + live: 'error', + diskCopy: 'error' + }) + const noPath = codesignFake({ + liveVerify: result(0), + liveRequirement: APP_REQUIREMENT, + diskVerify: result(0) + }) + expect((await checkHelperSignatures(1206, null, noPath)).diskCopy).toBe('error') + }) +}) diff --git a/src/main/daemon/transport/framing/testing/daemon-stream-binary-negotiation.test.ts b/src/main/daemon/transport/framing/testing/daemon-stream-binary-negotiation.test.ts index 2615de05d35..2e0b5a12768 100644 --- a/src/main/daemon/transport/framing/testing/daemon-stream-binary-negotiation.test.ts +++ b/src/main/daemon/transport/framing/testing/daemon-stream-binary-negotiation.test.ts @@ -169,7 +169,7 @@ describe('daemon stream framing negotiation', () => { return new Promise((resolve) => fakeDaemon!.listen(socketPath, resolve)) } - it.each([39, 41, 44])( + it.each([39])( 'falls back to NDJSON against a v%i daemon and keeps an event that shares the hello read', async (protocolVersion) => { let requested: string | undefined diff --git a/src/main/daemon/transport/framing/testing/ndjson.test.ts b/src/main/daemon/transport/framing/testing/ndjson.test.ts index f4e4c92a01f..17f224eaef0 100644 --- a/src/main/daemon/transport/framing/testing/ndjson.test.ts +++ b/src/main/daemon/transport/framing/testing/ndjson.test.ts @@ -7,12 +7,6 @@ import { } from '../../../../../shared/main-process-ndjson-framer' describe('encodeNdjson', () => { - it('accepts the exact line-byte limit and rejects one byte more', () => { - const emptyBytes = Buffer.byteLength(JSON.stringify({ data: '' }), 'utf8') - expect(encodeNdjson({ data: 'abc' }, emptyBytes + 3)).toBe('{"data":"abc"}\n') - expect(() => encodeNdjson({ data: 'abcd' }, emptyBytes + 3)).toThrow(NdjsonLineTooLongError) - }) - it('measures multibyte payloads in UTF-8 bytes, not characters', () => { const emptyBytes = Buffer.byteLength(JSON.stringify({ data: '' }), 'utf8') expect(encodeNdjson({ data: '🐙' }, emptyBytes + 4)).toBe('{"data":"🐙"}\n') diff --git a/src/main/daemon/transport/stream/testing/daemon-stream-data-batcher.test.ts b/src/main/daemon/transport/stream/testing/daemon-stream-data-batcher.test.ts index a451e174534..51f41675e7e 100644 --- a/src/main/daemon/transport/stream/testing/daemon-stream-data-batcher.test.ts +++ b/src/main/daemon/transport/stream/testing/daemon-stream-data-batcher.test.ts @@ -1,7 +1,6 @@ import { describe, expect, it, vi } from 'vitest' import type { Socket } from 'node:net' import { DaemonStreamDataBatcher } from '../daemon-stream-data-batcher' -import { createNdjsonParser } from '../../../../../shared/main-process-ndjson-framer' function createBatcher(options?: ConstructorParameters[1]) { const streamSocket = { @@ -60,27 +59,6 @@ describe('DaemonStreamDataBatcher', () => { } }) - it('holds bulk output while the socket buffer is deep and resumes on the next flush', () => { - vi.useFakeTimers() - try { - const { batcher, streamSocket } = createBatcher() - const bulk = 'x'.repeat(64 * 1024) - - streamSocket.writableLength = 128 * 1024 - batcher.enqueue('client-1', 'session-bulk', bulk) - vi.advanceTimersByTime(2) - expect(nonSentinelWrites(streamSocket)).toHaveLength(0) - expect(batcher.queuedCharsForClient('client-1')).toBe(bulk.length) - - streamSocket.writableLength = 0 - batcher.flush('client-1') - expect(writtenData(streamSocket)).toBe(bulk) - expect(batcher.queuedCharsForClient('client-1')).toBe(0) - } finally { - vi.useRealTimers() - } - }) - it('lets a small session write through the gate while a flooding session holds (per-session fairness)', () => { vi.useFakeTimers() try { @@ -356,32 +334,4 @@ describe('DaemonStreamDataBatcher', () => { vi.useRealTimers() } }) - - it('writes large stream data as parser-sized NDJSON events', () => { - vi.useFakeTimers() - try { - const maxLineBytes = 256 - const { batcher, streamSocket } = createBatcher({ maxLineBytes }) - const data = 'x'.repeat(maxLineBytes * 3) - const onMessage = vi.fn() - const onError = vi.fn() - const parser = createNdjsonParser(onMessage, onError, { maxLineBytes }) - - batcher.enqueue('client-1', 'session-1', data) - vi.advanceTimersByTime(2) - for (const [line] of streamSocket.write.mock.calls) { - parser.feed(String(line)) - } - - expect(onError).not.toHaveBeenCalled() - expect(onMessage).toHaveBeenCalled() - expect( - onMessage.mock.calls - .map(([message]) => (message as { payload?: { data?: string } }).payload?.data ?? '') - .join('') - ).toBe(data) - } finally { - vi.useRealTimers() - } - }) }) diff --git a/src/main/git/worktrees/removal/remove-worktree-clean-preflight.test.ts b/src/main/git/worktrees/removal/remove-worktree-clean-preflight.test.ts index b891958bae4..1b454e6564d 100644 --- a/src/main/git/worktrees/removal/remove-worktree-clean-preflight.test.ts +++ b/src/main/git/worktrees/removal/remove-worktree-clean-preflight.test.ts @@ -7,14 +7,16 @@ const { translateWslOutputPathsMock, statMock, readFileMock, - resolveGitDirMock + resolveGitDirMock, + gitStreamStdoutMock } = vi.hoisted(() => ({ gitExecFileAsyncMock: vi.fn(), gitExecFileSyncMock: vi.fn(), translateWslOutputPathsMock: vi.fn((output: string) => output), statMock: vi.fn(), readFileMock: vi.fn(), - resolveGitDirMock: vi.fn() + resolveGitDirMock: vi.fn(), + gitStreamStdoutMock: vi.fn() })) vi.mock('../../command-runner/git-exec-file', async (importOriginal) => ({ @@ -22,6 +24,10 @@ vi.mock('../../command-runner/git-exec-file', async (importOriginal) => ({ gitExecFileAsync: gitExecFileAsyncMock })) +vi.mock('../../command-runner/git-stream-stdout', () => ({ + gitStreamStdout: gitStreamStdoutMock +})) + vi.mock('../../command-runner/git-exec-file-sync', async (importOriginal) => ({ ...(await importOriginal>()), gitExecFileSync: gitExecFileSyncMock @@ -56,10 +62,9 @@ beforeEach(() => { describe('assertWorktreeCleanForRemoval', () => { beforeEach(() => { - // Calls after the status read are the submodule probe's `ls-files`; failing leaves the verdict to Git. - gitExecFileAsyncMock - .mockReset() - .mockRejectedValue(new Error('git ls-files exited with code 128')) + gitExecFileAsyncMock.mockReset() + // A failed submodule probe leaves the verdict to Git. + gitStreamStdoutMock.mockReset().mockRejectedValue(new Error('git exited with 128: ')) }) it('ignores configured linked paths without mutating them before preflight', async () => { diff --git a/src/main/git/worktrees/removal/worktree-removal-preflight.ts b/src/main/git/worktrees/removal/worktree-removal-preflight.ts index dc6af37e7bb..65fff520b74 100644 --- a/src/main/git/worktrees/removal/worktree-removal-preflight.ts +++ b/src/main/git/worktrees/removal/worktree-removal-preflight.ts @@ -6,6 +6,7 @@ import { SUBMODULE_REMOVAL_REFUSAL } from '../../../../shared/worktree/removal/removal' import { gitExecFileAsync } from '../../command-runner/git-exec-file' +import { gitStreamStdout } from '../../command-runner/git-stream-stdout' import { resolveGitDir } from '../../source-control/resolve-git-dir' import type { WorktreeRemovalPreflightOptions } from '../worktree-operation-options' import { @@ -92,20 +93,46 @@ async function containsSubmoduleCheckout( if (adminDirectory && !(await isWorktreePathMissing(join(adminDirectory, 'modules')))) { return true } - const { stdout: index } = await gitExecFileAsync(['ls-files', '--stage', '-z'], execution) - for (const entry of index.split('\0')) { - if ( - entry.startsWith('160000 ') && - !(await isWorktreePathMissing( - join(hostWorktreePath, entry.slice(entry.indexOf('\t') + 1), '.git') - )) - ) { + for (const gitlink of await readGitlinkPaths(execution)) { + if (!(await isWorktreePathMissing(join(hostWorktreePath, gitlink, '.git')))) { return true } } return false } +/** Stream the index keeping only gitlinks; a large repository's full listing exceeds the buffered cap. */ +async function readGitlinkPaths( + execution: Parameters[1] +): Promise { + const { cwd, env, wslDistro, signal, admissionTier, timeout } = execution + const gitlinks: string[] = [] + const keepGitlink = (entry: string): void => { + if (entry.startsWith('160000 ')) { + gitlinks.push(entry.slice(entry.indexOf('\t') + 1)) + } + } + let carry = '' + await gitStreamStdout(['ls-files', '--stage', '-z'], { + cwd, + env, + wslDistro, + signal, + admissionTier, + timeoutMs: timeout, + // Only gitlink paths are retained, so index size is not a memory risk. + maxBuffer: Number.MAX_SAFE_INTEGER, + maxStderrBuffer: 64 * 1024, + onStdout: (chunk) => { + const entries = (carry + chunk).split('\0') + carry = entries.pop() ?? '' + entries.forEach(keepGitlink) + } + }) + keepGitlink(carry) + return gitlinks +} + /** Exclude only the untracked shared links tolerated by the caller. */ function getBlockingUntrackedStatusEntries( status: string, diff --git a/src/main/git/worktrees/removal/worktree-removal-real-git.test.ts b/src/main/git/worktrees/removal/worktree-removal-real-git.test.ts index 763e20f94de..10bff011570 100644 --- a/src/main/git/worktrees/removal/worktree-removal-real-git.test.ts +++ b/src/main/git/worktrees/removal/worktree-removal-real-git.test.ts @@ -13,6 +13,7 @@ import { removeTree } from '../../../../shared/windows-transient-lock-removal' import { listWorktreesStrict } from '../catalog/worktree-listing' import { removeWorktree } from './worktree-removal' +import { assertWorktreeCleanForRemoval } from './worktree-removal-preflight' import { areWorktreePathsEqual } from '../catalog/worktree-path-comparison' import { isPrunableGitFileWorktree } from '../../../workspaces/removal/worktree-prunable-git-file' import { removeStaleLocalWorktreeRegistration } from '../../../workspaces/removal/local-worktree-removal-recovery' @@ -146,6 +147,31 @@ describe('worktree removal against the real Git binary', () => { expect(await isRegistered(subWorktreePath)).toBe(false) }) + // Builds a ~48k-entry real Git index (>10 MiB of ls-files output), which is slow on Windows runners. + it('finds a checked-out submodule in an index listing larger than the Git output buffer', async () => { + const blob = (await git(['hash-object', '-w', 'seed.txt'], worktreePath)).trim() + const head = (await git(['rev-parse', 'HEAD'], worktreePath)).trim() + const entries = Array.from( + { length: 48_000 }, + (_, i) => `100644 ${blob}\t${'nested-directory/'.repeat(12)}file-${i}.txt\n` + ) + // Sorts last, so the gitlink is only seen after more than 10 MiB of listing. + entries.push(`160000 ${head}\tzz-vendor\n`) + const indexInfo = execFileAsync('git', ['update-index', '--index-info'], { cwd: worktreePath }) + indexInfo.child.stdin?.end(entries.join('')) + await indexInfo + await mkdir(join(worktreePath, 'zz-vendor', '.git'), { recursive: true }) + + await expect( + assertWorktreeCleanForRemoval( + worktreePath, + false, + {}, + join(repoPath, '.git', 'worktrees', 'feature') + ) + ).rejects.toThrow('Worktree has uncommitted or untracked changes, and it contains submodules.') + }, 60_000) + it('prunes a proven malformed registration while retaining checkout files and its branch', async () => { const markerPath = join(worktreePath, '.git') const marker = await readFile(markerPath, 'utf8') diff --git a/src/main/github/accounts/account-binding-required-host.test.ts b/src/main/github/accounts/account-binding-required-host.test.ts deleted file mode 100644 index bf4fb4140a7..00000000000 --- a/src/main/github/accounts/account-binding-required-host.test.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { describe, expect, it, vi } from 'vitest' -import { resolveGhAccountRequiredHost } from './account-binding-required-host' - -function repoFor(remoteUrl: string, canonicalHost?: string) { - const host = - canonicalHost ?? - remoteUrl - .replace(/^git@/, '') - .replace(/^ssh:\/\/(?:[^@]+@)?/, '') - .split(/[:/]/)[0] - ?.toLowerCase() ?? - 'github.com' - return { - path: '/repos/sample-app', - connectionId: null, - gitRemoteIdentity: { - canonicalKey: `${host}/example/sample-app`, - remoteUrl - } - } -} - -describe('resolveGhAccountRequiredHost', () => { - it('keeps a dotted enterprise host literal unless ssh config points it at github.com', async () => { - for (const resolveAlias of [ - async () => ({ hostname: '10.0.0.5' }), - async () => ({ hostname: null }), - async () => { - throw new Error('ssh -G failed') - } - ]) { - await expect( - resolveGhAccountRequiredHost( - repoFor('git@ghe.example.com:example/sample-app.git'), - {}, - resolveAlias - ) - ).resolves.toBe('ghe.example.com') - } - }) - - it('filters a dotted alias that resolves to github.com to github.com', async () => { - const resolveAlias = vi.fn(async () => ({ hostname: 'github.com' })) - await expect( - resolveGhAccountRequiredHost( - repoFor('git@work.github.com:example/sample-app.git'), - {}, - resolveAlias - ) - ).resolves.toBe('github.com') - expect(resolveAlias).toHaveBeenCalledWith('work.github.com', { - repoPath: '/repos/sample-app', - connectionId: null - }) - }) - - it('expands an SCP alias whose path has a leading slash', async () => { - await expect( - resolveGhAccountRequiredHost( - repoFor('git@github-work:/example/sample-app.git', 'github-work'), - {}, - async () => ({ hostname: 'github.com' }) - ) - ).resolves.toBe('github.com') - }) -}) diff --git a/src/main/github/accounts/auth-diagnose.test.ts b/src/main/github/accounts/auth-diagnose.test.ts index b8664916037..2718a6326c8 100644 --- a/src/main/github/accounts/auth-diagnose.test.ts +++ b/src/main/github/accounts/auth-diagnose.test.ts @@ -1,12 +1,5 @@ -import { describe, expect, it, vi } from 'vitest' - -const { ghExecFileAsyncMock } = vi.hoisted(() => ({ ghExecFileAsyncMock: vi.fn() })) - -vi.mock('../../git/command-runner/hosted-cli/gh-exec-file', async (importOriginal) => ({ - ...(await importOriginal>()), - ghExecFileAsync: ghExecFileAsyncMock -})) -import { diagnoseGhAuth, parseAuthStatus } from './auth-diagnose' +import { describe, expect, it } from 'vitest' +import { parseAuthStatus } from './auth-diagnose' describe('parseAuthStatus', () => { it('parses an env-shadowed login alongside a keyring login (real gh output)', () => { @@ -39,49 +32,4 @@ describe('parseAuthStatus', () => { source: 'keyring' }) }) - - it('parses a ported GHES auth-status section', () => { - const text = `ghe.acme.io:8443 - ✓ Logged in to ghe.acme.io:8443 account bob (keyring) - - Active account: true - - Token scopes: 'project', 'repo' -` - - expect(parseAuthStatus(text)).toEqual([ - expect.objectContaining({ host: 'ghe.acme.io:8443', user: 'bob', active: true }) - ]) - }) -}) - -describe('diagnoseGhAuth host scoping', () => { - const TWO_HOST_STATUS = `github.com - ✓ Logged in to github.com account alice (keyring) - - Active account: true - - Token scopes: 'project', 'read:org', 'repo' -ghe.acme.io - ✓ Logged in to ghe.acme.io account bob (keyring) - - Active account: true - - Token scopes: 'repo' -` - - it('reports an unauthenticated required host and keeps scope advice host-scoped', async () => { - ghExecFileAsyncMock.mockResolvedValue({ stdout: TWO_HOST_STATUS, stderr: '' }) - - const diag = await diagnoseGhAuth('github.other-corp.com') - - expect(diag.requiredHost).toBe('github.other-corp.com') - expect(diag.requiredHostAuthenticated).toBe(false) - expect(diag.activeAccount).toBeNull() - }) - - it("scopes the diagnosis to the required host's account", async () => { - ghExecFileAsyncMock.mockResolvedValue({ stdout: TWO_HOST_STATUS, stderr: '' }) - - const diag = await diagnoseGhAuth('GHE.Acme.IO') - - expect(diag.requiredHost).toBe('ghe.acme.io') - expect(diag.requiredHostAuthenticated).toBe(true) - expect(diag.activeAccount?.user).toBe('bob') - expect(diag.missingScopes).toEqual(['project', 'read:org']) - }) }) diff --git a/src/main/github/accounts/github-project-account.test.ts b/src/main/github/accounts/github-project-account.test.ts index ebc0ff09c2b..c7dbd39f91e 100644 --- a/src/main/github/accounts/github-project-account.test.ts +++ b/src/main/github/accounts/github-project-account.test.ts @@ -20,7 +20,6 @@ import { registerGitHubPRReviewHandlers } from '../../ipc/source-control/github- import { ghPullRequestsAndWorkItemsApi } from '../../../preload/api/github/gh-bridge-pull-requests-and-work-items' import { ghMutationsAndProjectsApi } from '../../../preload/api/github/gh-bridge-mutations-and-projects' import type { GhAccountBinding } from '../../../shared/github/accounts/account-binding' -import { RPC_PARAMS_BY_METHOD } from '../../../shared/rpc-contract/rpc-params-catalog.generated' const { exec, handle, invoke } = vi.hoisted(() => ({ exec: vi.fn(), @@ -60,8 +59,6 @@ import type { Project } from '../../../shared/project-types' import type { Repo } from '../../../shared/repo-types' import { ghRepoExecOptions, githubRepoContext } from '../repositories/github-repository-identity' import { installGitHubProjectAccounts } from './github-project-account' -import { codeHostReadExecutionScope as githubReadExecutionScope } from '../../source-control/code-host-read-execution-scope' -import { resolveGitHubRepoExecution } from '../repositories/github-api-repository' let release: (() => void) | undefined afterEach(() => release?.()) @@ -82,20 +79,6 @@ function options(path: string, connectionId?: string | null, extra = {}) { return ghRepoExecOptions(githubRepoContext(path, connectionId, extra)) } -it('uses the saved bound or unbound selection over stale caller options, and isolates cache keys', () => { - let current = repo() - release = installGitHubProjectAccounts({ getRepos: () => [current] }) - const first = options('/repo', null, { ghAccount: bob }) - expect(first.ghAccount).toEqual(alice) - current = repo({ ghAccount: bob }) - const second = options('/repo', null, { ghAccount: alice }) - expect(second.ghAccount).toEqual(bob) - expect(githubReadExecutionScope(first, {})).not.toBe(githubReadExecutionScope(second, {})) - current = repo({ ghAccount: undefined }) - expect(options('/repo', null, { ghAccount: alice }).ghAccount).toBeUndefined() - expect(first.ghAccount).toEqual(alice) -}) - it.each(['cold', 'warm'] as const)( 'resolves a %s child worktree by its host-qualified owning project', (mode) => { @@ -117,23 +100,6 @@ it.each(['cold', 'warm'] as const)( } ) -it('selects before an asynchronous repository lookup and retains that account for execution', async () => { - let current = repo() - release = installGitHubProjectAccounts({ getRepos: () => [current] }) - let finish: () => void = () => {} - const waiting = new Promise((resolve) => { - finish = resolve - }) - const pending = resolveGitHubRepoExecution('/repo', async () => { - await waiting - return { owner: 'acme', repo: 'widgets', host: 'github.com' } - }) - current = repo({ ghAccount: bob }) - finish() - expect((await pending).ghOptions.ghAccount).toEqual(alice) - expect(options('/repo').ghAccount).toEqual(bob) -}) - it('derives runtime ownership again after project runtime changes', () => { const platform = process.platform Object.defineProperty(process, 'platform', { configurable: true, value: 'win32' }) @@ -300,88 +266,6 @@ describe('host account expectation through registered RPC and local preload', () rmSync(testState.dir, { recursive: true, force: true }) }) - it('validates all four schemas and rejects mismatches before provider work on RPC and IPC without changing omission', async () => { - applied(bob) - for (const operation of operations) { - const payload = { - repo: 'id:repo', - ...operation.params, - expectedGhAccount: { host: ' GitHub.COM ', user: ' Bob ' } - } - expect(RPC_PARAMS_BY_METHOD[operation.method].parse(payload)).toEqual({ - ...payload, - expectedGhAccount: bob - }) - for (const malformed of [ - { host: 'github.com' }, - { host: 'bad/host', user: 'Alice' }, - false - ]) { - expect( - RPC_PARAMS_BY_METHOD[operation.method].safeParse({ - ...payload, - expectedGhAccount: malformed - }).success - ).toBe(false) - } - exec.mockClear() - expect( - await dispatcher.dispatch({ - id: 'malformed', - authToken: 'fixture', - method: operation.method, - params: { ...payload, expectedGhAccount: false } - }) - ).toMatchObject({ ok: false, error: { code: 'invalid_argument' } }) - expect(exec).not.toHaveBeenCalled() - expect(await dispatch(operation, alice)).toMatchObject({ - ok: false, - error: { message: expect.stringContaining('GitHub account changed') } - }) - expect(exec).not.toHaveBeenCalled() - await expect(operation.local(alice)).rejects.toThrow('GitHub account changed') - expect(exec).not.toHaveBeenCalled() - expect(await dispatch(operation, bob)).toMatchObject({ ok: true }) - expect(exec).toHaveBeenCalled() - expect(exec.mock.calls.every(([, options]) => options?.ghAccount?.user === 'Bob')).toBe(true) - exec.mockClear() - await operation.local(bob) - expect(exec).toHaveBeenCalled() - expect(exec.mock.calls.every(([, options]) => options?.ghAccount?.user === 'Bob')).toBe(true) - exec.mockClear() - expect(await dispatch(operation)).toMatchObject({ ok: true }) - expect(exec).toHaveBeenCalled() - expect(exec.mock.calls.every(([, options]) => options?.ghAccount?.user === 'Bob')).toBe(true) - } - exec.mockClear() - expect(await dispatch(operations[1], null)).toMatchObject({ ok: false }) - expect(exec).not.toHaveBeenCalled() - applied(null) - expect(await dispatch(operations[1], null)).toMatchObject({ ok: true, result: ['bug'] }) - expect(exec.mock.calls.at(-1)?.[1]?.ghAccount).toBeUndefined() - await expect(operations[1].local(null)).resolves.toEqual(['bug']) - expect(exec.mock.calls.at(-1)?.[1]?.ghAccount).toBeUndefined() - exec.mockClear() - expect(await dispatch(operations[3], alice)).toMatchObject({ ok: false }) - expect(exec).not.toHaveBeenCalled() - await expect( - invoke('gh:listLabels', { - ...local, - ownerRepo: repository, - expectedGhAccount: { user: 'Alice' } - }) - ).rejects.toThrow('Invalid GitHub account expectation') - expect(exec).not.toHaveBeenCalled() - applied(alice) - expect(await dispatch(operations[0], alice)).toMatchObject({ - ok: true, - result: { item: { number: 7, type: 'issue' }, body: 'Alice' } - }) - expect( - await dispatch({ ...operations[0], params: { ...item, canonical: false } }, alice) - ).toMatchObject({ ok: true, result: { body: 'Alice' } }) - }) - it('keeps the actual admitted A capture through a later B update for full details and Viewed, then fails a fresh stale-A request', async () => { for (const operation of [operations[0], operations[3]]) { applied(alice) diff --git a/src/main/github/accounts/github-project-account.ts b/src/main/github/accounts/github-project-account.ts index c091d564bac..aa96fd8636c 100644 --- a/src/main/github/accounts/github-project-account.ts +++ b/src/main/github/accounts/github-project-account.ts @@ -87,3 +87,16 @@ export function resolveGitHubProjectAccount(context: GitHubRepoContext) { ? { ghAccount: normalizeGhAccountBinding(matches[0].ghAccount) ?? undefined } : null } + +/** Policy observes project ownership changes without resolving every workspace's account. */ +export function githubPRPolicyProjects(): { source: object | null; selection: string } { + return { + source, + selection: JSON.stringify( + source?.store + .getRepos() + .map((repo) => [repo.id, repo.path, getStoredRepoExecutionHostId(repo), repo.ghAccount]) ?? + [] + ) + } +} diff --git a/src/main/github/client/lookup/pr-branch-read-resources.ts b/src/main/github/client/lookup/pr-branch-read-resources.ts new file mode 100644 index 00000000000..46d94d85763 --- /dev/null +++ b/src/main/github/client/lookup/pr-branch-read-resources.ts @@ -0,0 +1,94 @@ +import type { CoalescedProbes } from '../../../git/remotes/coalesced-probe' +import type { PRRefreshOutcome } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import type { readTimedPROutcome } from './pr-last-outcome' + +export type PRBranchReadResult = { + outcome: PRRefreshOutcome + sequence: number + current: () => boolean +} + +type PRBranchReadResource = { + probes: CoalescedProbes + running: number + latestStart: number +} + +const resources = new Map() + +export function prBranchReadResource(key: string): PRBranchReadResource { + let resource = resources.get(key) + if (!resource) { + resource = { probes: new Map(), running: 0, latestStart: 0 } + resources.set(key, resource) + } + return resource +} + +export function pendingPRBranchRead( + key: string, + intent: 'fresh' | 'timed' +): Promise | null { + const pending = resources.get(key)?.probes.get(intent) + return pending && Date.now() - pending.startedAt < 2 * 60_000 ? pending.promise : null +} + +export function retirePRBranchReadResource(key: string, resource: PRBranchReadResource): void { + if (resource.probes.size === 0 && resource.running === 0 && resources.get(key) === resource) { + resources.delete(key) + } +} + +export function startPRBranchRead( + key: string, + resource: PRBranchReadResource, + sequence: number +): { current: () => boolean; finish: () => void } { + resource.running += 1 + resource.latestStart = sequence + return { + current: () => + Math.max(resource.latestStart, resources.get(key)?.latestStart ?? 0) === sequence, + finish: () => { + resource.running -= 1 + retirePRBranchReadResource(key, resource) + } + } +} + +export function deliverablePRBranchOutcome( + prepared: Pick, + result: PRBranchReadResult +): PRRefreshOutcome | null { + if (result.current()) { + return result.outcome + } + const cached = prepared.isCurrent() ? prepared.cached() : null + return cached && cached.sequence > result.sequence ? cached.outcome : null +} + +export type PRReadSettlement = ( + outcome: PRRefreshOutcome, + producer: { retained: boolean } +) => PRRefreshOutcome + +export type PreparedPRBranchRead = { + key: string + reprepare: () => Promise + cached: () => ReturnType + isCurrent: () => boolean + accepts: (outcome: PRRefreshOutcome) => boolean + didRead: () => boolean + release: () => void + hold: () => () => void + readRetained: (settle?: PRReadSettlement) => Promise + canRetain: () => boolean + scheduleError: (expected: PRRefreshOutcome, outcome: PRRefreshOutcome) => void + readResult: ( + settle?: PRReadSettlement, + requireRetention?: boolean + ) => Promise + pending: () => Promise | null + withExecution: (operation: () => Promise) => Promise + read: (settle?: PRReadSettlement) => Promise +} diff --git a/src/main/github/client/lookup/pr-for-branch-outcome.ts b/src/main/github/client/lookup/pr-for-branch-outcome.ts index a1d4bc779a5..e4952ecfe99 100644 --- a/src/main/github/client/lookup/pr-for-branch-outcome.ts +++ b/src/main/github/client/lookup/pr-for-branch-outcome.ts @@ -1,56 +1,44 @@ import { withGitHubRepoExecution, - ghRepoExecOptions, - githubRepoContext + captureGitHubRepoExecution, + outsideGitHubRepoExecution } from '../../repositories/github-repository-identity' -import { getRepoExecutionHostId } from '../../../../shared/execution-host' +import { runCoalescedProbe } from '../../../git/remotes/coalesced-probe' +import type { PRRefreshOutcome } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { acquire, release } from '../../execution/github-operation-slots' + +import { hostedReviewLocalGitOptionArgs } from './../github-exec-scope' +import type { GitHubPRBranchLookupOptions } from './pull-request-lookup-data' +import { prRefreshUpstreamError } from './../gh-error-predicates' +import { resolvePRForBranchOutcome } from './branch-lookup-resolution' +import { + readRecentOpenPROutcome, + readTimedPROutcome, + canRetainPROutcome, + beginExactPRLookup, + releaseExactPRLookup, + holdExactPROutcome, + scheduleExactPRError, + recordPRLastOutcome, + startPRLookup +} from './pr-last-outcome' +import { prLookupScope, lastOutcomeKey, completeReadKey } from './pr-read-identity' +import { refreshPRPolicyContext, recordPRPolicyOutcome } from './pr-quiet-policy-evidence' import { hostedReviewRepoScope, scopeGeneration } from '../../../source-control/hosted-review-scope-generations' -import { runCoalescedProbe, type CoalescedProbes } from '../../../git/remotes/coalesced-probe' -import { codeHostReadExecutionScope as githubReadExecutionScope } from '../../../source-control/code-host-read-execution-scope' -import type { PRRefreshOutcome } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' -import { acquire, release } from '../../execution/github-operation-slots' - -import { hostedReviewLocalGitOptionArgs, githubPRStackExecutionScope } from './../github-exec-scope' -import type { GitHubPRBranchLookupOptions } from './pull-request-lookup-data' -import { prRefreshUpstreamError } from './../gh-error-predicates' -import { resolvePRForBranchOutcome } from './branch-lookup-resolution' -import { readRecentOpenPROutcome, recordPRLastOutcome, startPRLookup } from './pr-last-outcome' -const reads: CoalescedProbes = new Map() - -function prLookupScope( - repoPath: string, - connectionId: string | null | undefined, - options: GitHubPRBranchLookupOptions -) { - const localGitOptions = hostedReviewLocalGitOptionArgs(options)[0] ?? {} - const ghOptions = ghRepoExecOptions(githubRepoContext(repoPath, connectionId, localGitOptions)) - return { - localGitOptions, - ghOptions, - executionScope: githubPRStackExecutionScope(connectionId, localGitOptions), - generation: scopeGeneration( - hostedReviewRepoScope(repoPath, getRepoExecutionHostId({ connectionId })) - ), - readScope: githubReadExecutionScope(ghOptions) - } -} - -function lastOutcomeKey( - repoPath: string, - branchName: string, - scope: ReturnType -): string { - return JSON.stringify([ - scope.executionScope, - repoPath, - scope.generation, - branchName, - scope.readScope - ]) -} +import { getRepoExecutionHostId } from '../../../../shared/execution-host' +import { + prBranchReadResource, + pendingPRBranchRead, + retirePRBranchReadResource, + startPRBranchRead, + type PRBranchReadResult, + type PRReadSettlement, + type PreparedPRBranchRead +} from './pr-branch-read-resources' +class PRLookupRetentionRefused extends Error {} /** A phone read may take the last lookup's open PR; it was already observed when it was looked up. */ export async function readRecentPhonePROutcome( @@ -84,19 +72,39 @@ export async function readRecentPhonePROutcome( ) } -export async function getPRForBranchOutcome( +export async function preparePRBranchRead( repoPath: string, branch: string, linkedPRNumber?: number | null, connectionId?: string | null, fallbackPRNumber?: number | null, options: GitHubPRBranchLookupOptions = {} -): Promise { +): Promise { + options = { + ...options, + ...(options.localGitExecOptions + ? { + localGitExecOptions: { + ...options.localGitExecOptions, + ...(options.localGitExecOptions.ghAccount + ? { ghAccount: { ...options.localGitExecOptions.ghAccount } } + : {}) + } + } + : {}), + ...(options.sharedLinkPaths ? { sharedLinkPaths: [...options.sharedLinkPaths] } : {}), + ...(options.exactRepository ? { exactRepository: { ...options.exactRepository } } : {}) + } + const reprepare = () => + outsideGitHubRepoExecution(() => + preparePRBranchRead(repoPath, branch, linkedPRNumber, connectionId, fallbackPRNumber, options) + ) return withGitHubRepoExecution( repoPath, connectionId, hostedReviewLocalGitOptionArgs(options)[0] ?? {}, - async (): Promise => { + async (): Promise => { + const withExecution = captureGitHubRepoExecution() const branchName = branch.replace(/^refs\/heads\//, '') // Detached HEAD cannot discover by branch, but explicit PR numbers remain safe. if ( @@ -104,63 +112,193 @@ export async function getPRForBranchOutcome( typeof linkedPRNumber !== 'number' && typeof fallbackPRNumber !== 'number' ) { - return { kind: 'no-pr', fetchedAt: Date.now() } + return { + key: '', + reprepare, + cached: () => null, + isCurrent: () => true, + accepts: () => true, + didRead: () => false, + release: () => {}, + hold: () => () => {}, + readRetained: async () => ({ kind: 'no-pr', fetchedAt: Date.now() }), + readResult: async () => ({ + outcome: { kind: 'no-pr', fetchedAt: Date.now() }, + sequence: 0, + current: () => true + }), + canRetain: () => true, + scheduleError: () => {}, + pending: () => null, + withExecution, + read: async () => ({ kind: 'no-pr', fetchedAt: Date.now() }) + } } + const policyRevision = refreshPRPolicyContext() const scope = prLookupScope(repoPath, connectionId, options) const { localGitOptions, ghOptions, executionScope } = scope - const key = JSON.stringify([ - executionScope, + const key = completeReadKey( repoPath, - scope.generation, branchName, - linkedPRNumber ?? null, - fallbackPRNumber ?? null, - options.acceptMergedFallbackPR ?? false, - options.currentHeadOid ?? null, - options.checkPageLimit ?? null, - options.exactRepository ?? null, - options.includeDerivedData !== false, - scope.readScope - ]) - return runCoalescedProbe( - reads, - key, - async () => { - await acquire() - const start = startPRLookup() - let outcome: PRRefreshOutcome - try { - outcome = await resolvePRForBranchOutcome({ - repoPath, - branchName, - linkedPRNumber, - connectionId, - fallbackPRNumber, - options, - localGitOptions, - ghOptions, - executionScope - }) - } catch (err) { - outcome = prRefreshUpstreamError(err) - } finally { - release() - } - const narrowed = - options.checkPageLimit !== undefined || - options.exactRepository || - options.includeDerivedData === false - recordPRLastOutcome(lastOutcomeKey(repoPath, branchName, scope), { - start, - linkedPRNumber: linkedPRNumber ?? null, - fallbackPRNumber: fallbackPRNumber ?? null, - outcome: narrowed ? null : outcome - }) - return outcome - }, - 2 * 60_000, - options + linkedPRNumber, + fallbackPRNumber, + options, + scope ) + const ownerKey = lastOutcomeKey(repoPath, branchName, scope) + const coalescingKey = options.resetQuietBackoff || options.userRefresh ? 'fresh' : 'timed' + const pendingAt = (intent: 'fresh' | 'timed') => pendingPRBranchRead(key, intent) + const narrowed = + options.checkPageLimit !== undefined || + options.exactRepository || + options.includeDerivedData === false + let didRead = false + const readCore = (requireRetention = false, settle?: PRReadSettlement) => + withExecution(() => { + const resource = prBranchReadResource(key) + return ( + (!options.userRefresh && coalescingKey === 'timed' ? pendingAt('fresh') : null) ?? + runCoalescedProbe( + resource.probes, + coalescingKey, + async (ownsKey) => { + const start = startPRLookup() + const retained = + !narrowed && + beginExactPRLookup( + ownerKey, + key, + start, + options.resetQuietBackoff || options.userRefresh + ) + if (requireRetention && !retained) { + throw new PRLookupRetentionRefused() + } + const producer = startPRBranchRead(key, resource, start.sequence) + didRead = true + try { + await acquire() + let outcome: PRRefreshOutcome + try { + outcome = await resolvePRForBranchOutcome({ + repoPath, + branchName, + linkedPRNumber, + connectionId, + fallbackPRNumber, + options, + localGitOptions, + ghOptions, + executionScope + }) + } catch (err) { + outcome = prRefreshUpstreamError(err) + } finally { + release() + } + recordPRLastOutcome(ownerKey, { + start, + requestKey: key, + currentHeadOid: options.currentHeadOid ?? null, + resetQuiet: options.userRefresh === true || options.resetQuietBackoff === true, + linkedPRNumber: linkedPRNumber ?? null, + fallbackPRNumber: fallbackPRNumber ?? null, + outcome: narrowed ? null : outcome + }) + if (!narrowed) { + recordPRPolicyOutcome(outcome, { + revision: policyRevision, + current: () => + scopeGeneration( + hostedReviewRepoScope(repoPath, getRepoExecutionHostId({ connectionId })) + ) === scope.generation, + cached: () => readTimedPROutcome(ownerKey, key) + }) + } + if ( + settle && + prepared.isCurrent() && + producer.current() && + (retained ? readTimedPROutcome(ownerKey, key)?.outcome === outcome : ownsKey()) + ) { + outcome = settle(outcome, { retained }) + } + return { + outcome, + sequence: start.sequence, + current: () => + producer.current() && + prepared.isCurrent() && + (!retained || prepared.accepts(outcome)) + } + } finally { + if (retained) { + releaseExactPRLookup(key, start.sequence) + } + producer.finish() + } + }, + 2 * 60_000, + { userRefresh: options.userRefresh === true } + ).finally(() => retirePRBranchReadResource(key, resource)) + ) + }) + const readResult = async ( + settle?: PRReadSettlement, + requireRetention = false + ): Promise => { + try { + return await readCore(requireRetention, settle) + } catch (error) { + if (!(error instanceof PRLookupRetentionRefused)) { + throw error + } + return requireRetention ? null : readCore(false, settle) + } + } + const read = async (settle?: PRReadSettlement): Promise => + (await readResult(settle))!.outcome + const readRetained = async (settle?: PRReadSettlement): Promise => + (await readResult(settle, true))?.outcome ?? null + const prepared: PreparedPRBranchRead = { + key, + reprepare, + cached: () => readTimedPROutcome(ownerKey, key), + isCurrent: () => + outsideGitHubRepoExecution( + () => + lastOutcomeKey( + repoPath, + branchName, + prLookupScope(repoPath, connectionId, options) + ) === ownerKey + ), + accepts: (outcome) => readTimedPROutcome(ownerKey, key)?.outcome === outcome, + didRead: () => didRead, + release: () => {}, + hold: () => holdExactPROutcome(key), + readRetained, + readResult, + canRetain: () => canRetainPROutcome(key), + scheduleError: (expected, outcome) => scheduleExactPRError(key, expected, outcome), + pending: () => pendingAt('fresh') ?? pendingAt('timed'), + withExecution, + read + } + return prepared } ) } + +export type { PRReadSettlement, PreparedPRBranchRead } from './pr-branch-read-resources' + +export async function getPRForBranchOutcome( + ...args: Parameters +): Promise { + const prepared = await preparePRBranchRead(...args) + try { + return await prepared.read() + } finally { + prepared.release() + } +} diff --git a/src/main/github/client/lookup/pr-last-outcome.ts b/src/main/github/client/lookup/pr-last-outcome.ts index 7d6dec32aac..cc2a6032eb4 100644 --- a/src/main/github/client/lookup/pr-last-outcome.ts +++ b/src/main/github/client/lookup/pr-last-outcome.ts @@ -1,4 +1,8 @@ import type { PRRefreshOutcome } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { + isQuietPR, + QUIET_PR_REFRESH_INTERVAL_MS +} from '../../../../shared/github/pull-requests/refresh/pr-quiet-refresh' // A phone's re-read right after the desktop's own lookup reuses it instead of asking GitHub again. const PHONE_REUSE_MAX_AGE_MS = 30_000 @@ -10,33 +14,228 @@ type PRLastOutcome = { start: PRLookupStart linkedPRNumber: number | null fallbackPRNumber: number | null + requestKey?: string + currentHeadOid?: string | null + resetQuiet?: boolean + quiet?: boolean // null: the last lookup was narrowed (CI-line watch), so nothing is reusable until a full one. outcome: PRRefreshOutcome | null } +type ExactOutcome = PRLastOutcome & { + latestSequence: number + activeSequence: number | null + holds: number + invalidatedBeforeSequence: number + ownerKey: string +} const lastOutcomes = new Map() +const exactOutcomes = new Map() let lookupSequence = 0 -// Two lookups can start in the same millisecond; the sequence still orders them. export function startPRLookup(): PRLookupStart { lookupSequence += 1 return { startedAt: Date.now(), sequence: lookupSequence } } -export function recordPRLastOutcome(key: string, entry: PRLastOutcome): void { - const existing = lastOutcomes.get(key) - // A lookup that started earlier but settled later must not replace a newer answer. - if (existing && existing.start.sequence > entry.start.sequence) { +function trim(entries: Map): void { + while (entries.size > MAX_ENTRIES) { + entries.delete(entries.keys().next().value!) + } +} + +function pinned(entry: ExactOutcome): boolean { + return ( + entry.activeSequence !== null || + entry.holds > 0 || + (entry.outcome?.kind === 'upstream-error' && + Math.max(entry.outcome.nextAutoRetryAt ?? 0, entry.outcome.retryDisabledUntil ?? 0) > + Date.now()) + ) +} + +export function canRetainPROutcome(requestKey: string): boolean { + return ( + exactOutcomes.has(requestKey) || + exactOutcomes.size < MAX_ENTRIES || + [...exactOutcomes.values()].some((entry) => !pinned(entry)) + ) +} + +function evictExactOutcome(requestKey: string): void { + const entry = exactOutcomes.get(requestKey) + if (!entry) { return } - lastOutcomes.delete(key) - lastOutcomes.set(key, entry) - const now = Date.now() - for (const [oldestKey, oldest] of lastOutcomes) { - if (lastOutcomes.size <= MAX_ENTRIES && now - oldest.start.startedAt < PHONE_REUSE_MAX_AGE_MS) { - break + const projection = lastOutcomes.get(entry.ownerKey) + if (projection?.requestKey === requestKey) { + projection.outcome = null + } + exactOutcomes.delete(requestKey) +} + +function makeRoom(requestKey: string): boolean { + if (exactOutcomes.has(requestKey) || exactOutcomes.size < MAX_ENTRIES) { + return true + } + const victim = [...exactOutcomes].find(([, value]) => !pinned(value)) + if (!victim) { + return false + } + evictExactOutcome(victim[0]) + return true +} + +export function beginExactPRLookup( + ownerKey: string, + requestKey: string, + start: PRLookupStart, + resetQuiet = false +): boolean { + const previous = exactOutcomes.get(requestKey) + if (previous) { + previous.latestSequence = start.sequence + previous.activeSequence = start.sequence + if (resetQuiet) { + previous.quiet = false } - lastOutcomes.delete(oldestKey) + } else if (makeRoom(requestKey)) { + exactOutcomes.set(requestKey, { + ownerKey, + start, + latestSequence: start.sequence, + activeSequence: start.sequence, + holds: 0, + invalidatedBeforeSequence: 0, + linkedPRNumber: null, + fallbackPRNumber: null, + outcome: null + }) + } else { + return false + } + return true +} + +export function holdExactPROutcome(requestKey: string): () => void { + const entry = exactOutcomes.get(requestKey) + if (!entry) { + return () => {} + } + entry.holds += 1 + let held = true + return () => { + if (held) { + held = false + const current = exactOutcomes.get(requestKey) + if (current) { + current.holds -= 1 + } + } + } +} + +export function releaseExactPRLookup(requestKey: string, sequence: number): void { + const entry = exactOutcomes.get(requestKey) + if (entry?.activeSequence === sequence) { + entry.activeSequence = null + } +} + +function invalidateOwnerSuccesses(ownerKey: string, sequence: number): void { + for (const entry of exactOutcomes.values()) { + if (entry.ownerKey !== ownerKey) { + continue + } + entry.invalidatedBeforeSequence = Math.max(entry.invalidatedBeforeSequence, sequence) + if (entry.outcome?.kind !== 'upstream-error' && entry.start.sequence < sequence) { + entry.outcome = null + entry.quiet = false + } + } +} + +export function recordPRLastOutcome(key: string, entry: PRLastOutcome): void { + const previous = entry.requestKey ? exactOutcomes.get(entry.requestKey) : undefined + if (entry.outcome) { + if ( + !previous || + previous.ownerKey !== key || + previous.latestSequence !== entry.start.sequence || + (entry.outcome.kind !== 'upstream-error' && + entry.start.sequence < previous.invalidatedBeforeSequence) + ) { + return + } + } + if (!entry.outcome || entry.outcome.kind === 'upstream-error') { + invalidateOwnerSuccesses(key, entry.start.sequence) + } + let retained = false + if (entry.requestKey && entry.outcome && previous) { + const quiet = + !entry.resetQuiet && + previous.outcome?.kind === 'found' && + entry.outcome.kind === 'found' && + isQuietPR(entry.outcome.pr, entry.currentHeadOid ?? null) && + JSON.stringify(previous.outcome.pr) === JSON.stringify(entry.outcome.pr) + exactOutcomes.delete(entry.requestKey) + exactOutcomes.set(entry.requestKey, { + ...entry, + ownerKey: key, + quiet, + activeSequence: previous.activeSequence, + holds: previous.holds, + invalidatedBeforeSequence: previous.invalidatedBeforeSequence, + latestSequence: entry.start.sequence + }) + retained = true + } + const existing = lastOutcomes.get(key) + if (!existing || existing.start.sequence <= entry.start.sequence) { + lastOutcomes.delete(key) + lastOutcomes.set(key, retained ? entry : { ...entry, outcome: null }) + trim(lastOutcomes) + } +} + +export function readTimedPROutcome( + key: string, + requestKey: string +): { + outcome: PRRefreshOutcome + quietIntervalMs: number | null + sequence: number +} | null { + const entry = exactOutcomes.get(requestKey) + if (!entry?.outcome || entry.ownerKey !== key) { + return null + } + if ( + entry.outcome.kind !== 'upstream-error' && + entry.start.sequence < entry.invalidatedBeforeSequence + ) { + return null + } + return { + outcome: entry.outcome, + quietIntervalMs: entry.quiet ? QUIET_PR_REFRESH_INTERVAL_MS : null, + sequence: entry.start.sequence + } +} + +export function scheduleExactPRError( + requestKey: string, + expected: PRRefreshOutcome, + scheduled: PRRefreshOutcome +): void { + const entry = exactOutcomes.get(requestKey) + if ( + entry?.outcome === expected && + entry.start.sequence === entry.latestSequence && + scheduled.kind === 'upstream-error' + ) { + entry.outcome = scheduled } } diff --git a/src/main/github/client/lookup/pr-quiet-policy-evidence.ts b/src/main/github/client/lookup/pr-quiet-policy-evidence.ts new file mode 100644 index 00000000000..c4a7d9546ac --- /dev/null +++ b/src/main/github/client/lookup/pr-quiet-policy-evidence.ts @@ -0,0 +1,89 @@ +import type { + GitHubPRRefreshCandidate, + PRRefreshOutcome +} from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { githubPRPolicyProjects } from '../../accounts/github-project-account' +import type { readTimedPROutcome } from './pr-last-outcome' + +let revision = 0 +let projects: ReturnType | undefined +let environment: Record = {} +let cwd = '' + +// Called once at a report boundary, never from a per-row policy decision. +export function refreshPRPolicyContext(): number { + const next = githubPRPolicyProjects() + const keys = Object.keys(process.env) + if ( + next.source !== projects?.source || + next.selection !== projects?.selection || + cwd !== process.cwd() || + keys.length !== Object.keys(environment).length || + keys.some((key) => process.env[key] !== environment[key]) + ) { + revision += 1 + projects = next + environment = { ...process.env } + cwd = process.cwd() + } + return revision +} + +type Evidence = { + revision: number + current: () => boolean + cached: () => ReturnType +} +const completed = new WeakMap() +const projections = new Map() + +function key(candidate: GitHubPRRefreshCandidate): string { + return JSON.stringify([ + candidate.repoId, + candidate.repoPath, + candidate.connectionId ?? null, + candidate.localGitOptions?.wslDistro ?? null, + candidate.branch.replace(/^refs\/heads\//, ''), + candidate.currentHeadOid ?? null, + candidate.linkedPRNumber ?? null, + candidate.linkedPRNumber == null ? (candidate.fallbackPRNumber ?? null) : null, + candidate.linkedPRNumber == null ? (candidate.fallbackPRSource ?? null) : null + ]) +} + +export function recordPRPolicyOutcome(outcome: PRRefreshOutcome, evidence: Evidence): void { + completed.set(outcome, evidence) +} + +export function projectPRPolicyOutcome( + candidate: GitHubPRRefreshCandidate, + outcome: PRRefreshOutcome +): void { + const evidence = completed.get(outcome) + const identity = key(candidate) + projections.delete(identity) + if (evidence) { + projections.set(identity, evidence) + } + while (projections.size > 256) { + projections.delete(projections.keys().next().value!) + } +} + +export function quietPRIntervalForCandidate(candidate: GitHubPRRefreshCandidate): number | null { + const evidence = projections.get(key(candidate)) + if (!evidence || evidence.revision !== revision || !evidence.current()) { + return null + } + const cached = evidence.cached() + if ( + cached?.outcome.kind !== 'found' || + cached.outcome.fetchedAt !== candidate.cachedFetchedAt || + candidate.cachedHasPR !== true || + candidate.cachedPRState !== cached.outcome.pr.state || + candidate.cachedChecksStatus !== cached.outcome.pr.checksStatus + ) { + return null + } + return cached.quietIntervalMs +} diff --git a/src/main/github/client/lookup/pr-read-identity.ts b/src/main/github/client/lookup/pr-read-identity.ts new file mode 100644 index 00000000000..ba55c8871b9 --- /dev/null +++ b/src/main/github/client/lookup/pr-read-identity.ts @@ -0,0 +1,83 @@ +import { ghRepoExecOptions, githubRepoContext } from '../../repositories/github-repository-identity' +import { getRepoExecutionHostId } from '../../../../shared/execution-host' +import { + hostedReviewRepoScope, + scopeGeneration +} from '../../../source-control/hosted-review-scope-generations' +import { codeHostReadExecutionScope as githubReadExecutionScope } from '../../../source-control/code-host-read-execution-scope' +import { hostedReviewLocalGitOptionArgs, githubPRStackExecutionScope } from '../github-exec-scope' +import type { GitHubPRBranchLookupOptions } from './pull-request-lookup-data' +import { readTimedPROutcome } from './pr-last-outcome' + +export function prLookupScope( + repoPath: string, + connectionId: string | null | undefined, + options: GitHubPRBranchLookupOptions +) { + const localGitOptions = hostedReviewLocalGitOptionArgs(options)[0] ?? {} + const ghOptions = ghRepoExecOptions(githubRepoContext(repoPath, connectionId, localGitOptions)) + return { + localGitOptions, + ghOptions, + executionScope: githubPRStackExecutionScope(connectionId, localGitOptions), + generation: scopeGeneration( + hostedReviewRepoScope(repoPath, getRepoExecutionHostId({ connectionId })) + ), + readScope: githubReadExecutionScope(ghOptions) + } +} + +export function lastOutcomeKey( + repoPath: string, + branchName: string, + scope: ReturnType +): string { + return JSON.stringify([ + scope.executionScope, + repoPath, + scope.generation, + branchName, + scope.readScope + ]) +} + +export function completeReadKey( + repoPath: string, + branchName: string, + linkedPRNumber: number | null | undefined, + fallbackPRNumber: number | null | undefined, + options: GitHubPRBranchLookupOptions, + scope: ReturnType +): string { + return JSON.stringify([ + scope.executionScope, + repoPath, + scope.generation, + branchName, + linkedPRNumber ?? null, + fallbackPRNumber ?? null, + options.acceptMergedFallbackPR ?? false, + options.currentHeadOid ?? null, + options.checkPageLimit ?? null, + options.exactRepository ?? null, + options.includeDerivedData !== false, + options.sharedLinkPaths ?? null, + scope.readScope + ]) +} + +export function cachedPRBranchOutcome( + repoPath: string, + branch: string, + linkedPRNumber: number | null, + connectionId: string | null, + fallbackPRNumber: number | null, + options: GitHubPRBranchLookupOptions +): ReturnType { + const branchName = branch.replace(/^refs\/heads\//, '') + const scope = prLookupScope(repoPath, connectionId, options) + return readTimedPROutcome( + lastOutcomeKey(repoPath, branchName, scope), + completeReadKey(repoPath, branchName, linkedPRNumber, fallbackPRNumber, options, scope) + ) +} diff --git a/src/main/github/client/lookup/pull-request-lookup-data.ts b/src/main/github/client/lookup/pull-request-lookup-data.ts index 62f23ec8f95..b6812559e5b 100644 --- a/src/main/github/client/lookup/pull-request-lookup-data.ts +++ b/src/main/github/client/lookup/pull-request-lookup-data.ts @@ -92,6 +92,7 @@ export const PR_BRANCH_LIST_JSON_FIELDS = 'number,title,state,url,updatedAt,isDraft,mergeable,baseRefName,headRefName,baseRefOid,headRefOid,headRepositoryOwner,headRepository,isCrossRepository' export type GitHubPRBranchLookupOptions = HostedReviewExecutionOptions & { + resetQuietBackoff?: boolean userRefresh?: boolean /** CI watches need provider status, without local conflict or stack enrichment. */ includeDerivedData?: boolean diff --git a/src/main/github/pull-requests/refresh/hosted-pr-read-jobs.ts b/src/main/github/pull-requests/refresh/hosted-pr-read-jobs.ts new file mode 100644 index 00000000000..ecd7a1d962f --- /dev/null +++ b/src/main/github/pull-requests/refresh/hosted-pr-read-jobs.ts @@ -0,0 +1,308 @@ +import type { + GitHubPRRefreshCandidate, + PRRefreshOutcome, + PRSnapshotReply +} from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { reviewRefreshIntervalMs } from '../../../../shared/review-refresh-policy' +import { deliverablePRBranchOutcome } from '../../client/lookup/pr-branch-read-resources' +import type { PreparedPRBranchRead } from '../../client/lookup/pr-for-branch-outcome' +import type { PRRefreshQueue, PRRefreshQueueEntry } from './pr-refresh-queue' +import type { PRRefreshPacing } from './pr-refresh-pacing' +import type { PRRefreshRetryState } from './pr-refresh-retry-state' + +export const HOSTED_PR_COMPLETION_WAIT_MS = 25_000 +const RETRY_MS = 10_000 +const MAX_JOBS = 256 +const MAX_WAITERS = 256 +const JOB_LEASE_MS = 5 * 60_000 + +export type HostedPRReadJob = { + prepared: PreparedPRBranchRead + expiresAt: number + running?: boolean + promoteSelected: () => void + listeners: Set<(outcome: PRRefreshOutcome | null) => void> + finish: (outcome: PRRefreshOutcome | null) => void +} + +export class HostedPRReadJobs { + private readonly jobs = new Map() + private waiters = 0 + + constructor( + private readonly queue: PRRefreshQueue, + private readonly pacing: PRRefreshPacing, + private readonly retry: PRRefreshRetryState, + private readonly wake: () => void, + private readonly observe: ( + candidate: GitHubPRRefreshCandidate, + outcome: PRRefreshOutcome + ) => void + ) {} + + has(prepared: PreparedPRBranchRead): boolean { + return this.jobs.has(`hosted::${prepared.key}`) + } + + fresh(prepared: PreparedPRBranchRead): PRRefreshOutcome | null { + return this.freshOutcome(prepared.cached()) + } + + freshOutcome( + cached: ReturnType, + selected = true + ): PRRefreshOutcome | null { + if (!cached || cached.outcome.kind === 'upstream-error') { + return null + } + const outcome = cached.outcome + const interval = + cached.quietIntervalMs ?? + reviewRefreshIntervalMs({ + state: outcome.kind === 'found' ? outcome.pr.state : null, + checksStatus: outcome.kind === 'found' ? outcome.pr.checksStatus : null, + hasReview: outcome.kind === 'found', + selected + }) + return interval === null || Date.now() - outcome.fetchedAt < interval ? outcome : null + } + + private async legacyRead( + prepared: PreparedPRBranchRead, + candidate: GitHubPRRefreshCandidate + ): Promise { + const current = await prepared.reprepare() + try { + const result = await current.readResult((outcome) => { + this.observe(candidate, outcome) + return outcome + }) + return result!.outcome + } finally { + current.release() + } + } + + private gateUntil(prepared: PreparedPRBranchRead): number { + const outcome = prepared.cached()?.outcome + return Math.max( + this.queue.backgroundGateUntil(`hosted::${prepared.key}`), + outcome?.kind === 'upstream-error' + ? Math.max(outcome.nextAutoRetryAt ?? 0, outcome.retryDisabledUntil ?? 0) + : 0 + ) + } + + retryAfterMs(prepared: PreparedPRBranchRead): number { + return Math.max( + RETRY_MS, + this.pacing.nextBudgetDelay(), + prepared.isCurrent() ? this.gateUntil(prepared) - Date.now() : 0 + ) + } + + async complete( + prepared: PreparedPRBranchRead, + candidate: GitHubPRRefreshCandidate, + outcome: PRRefreshOutcome | null, + acceptRevalidationOnly: boolean + ): Promise { + if (outcome && !acceptRevalidationOnly) { + return outcome + } + const current = prepared.isCurrent() + if (current) { + if (outcome && prepared.accepts(outcome)) { + return outcome + } + const fresh = this.fresh(prepared) + if (fresh) { + return fresh + } + } + const delay = this.retryAfterMs(prepared) + const cached = current ? prepared.cached()?.outcome : null + if (cached) { + return cached.kind === 'upstream-error' + ? cached + : { ...cached, revalidation: { retryAfterMs: delay } } + } + return acceptRevalidationOnly + ? { revalidation: { invalidated: true, retryAfterMs: delay } } + : this.legacyRead(prepared, candidate) + } + + async readCold( + candidate: GitHubPRRefreshCandidate, + prepared: PreparedPRBranchRead, + acceptRevalidationOnly: boolean + ): Promise { + try { + const result = await prepared.readResult((outcome) => { + const settled = + acceptRevalidationOnly && outcome.kind === 'upstream-error' + ? this.retry.settleProducerError( + outcome, + `hosted::${prepared.key}`, + { ...candidate, isSelected: false }, + prepared, + this.queue, + true + ) + : outcome + this.observe(candidate, settled) + return settled + }, acceptRevalidationOnly) + const outcome = result + ? acceptRevalidationOnly + ? deliverablePRBranchOutcome(prepared, result) + : result.outcome + : null + return await this.complete(prepared, candidate, outcome, acceptRevalidationOnly) + } finally { + prepared.release() + } + } + + async read( + prepared: PreparedPRBranchRead, + candidate: GitHubPRRefreshCandidate, + deadline: number, + signal?: AbortSignal, + acceptRevalidationOnly = false + ): Promise { + if (!prepared.isCurrent()) { + return this.complete(prepared, candidate, null, acceptRevalidationOnly) + } + const fresh = this.fresh(prepared) + if (fresh) { + return this.complete(prepared, candidate, fresh, acceptRevalidationOnly) + } + const key = `hosted::${prepared.key}` + for (const [oldKey, old] of this.jobs) { + if (old.expiresAt > Date.now()) { + break + } + if (old.running) { + continue + } + this.queue.delete(oldKey) + old.finish(null) + } + const fallback = () => this.complete(prepared, candidate, null, acceptRevalidationOnly) + const gate = this.gateUntil(prepared) + if (prepared.cached()?.outcome.kind === 'upstream-error' && gate > Date.now()) { + return fallback() + } + let job = this.jobs.get(key) + if (!job) { + if (this.jobs.size >= MAX_JOBS) { + return fallback() + } + const listeners: HostedPRReadJob['listeners'] = new Set() + const releaseHold = prepared.hold() + const expiresAt = Date.now() + JOB_LEASE_MS + const entry: PRRefreshQueueEntry = { + key, + candidate, + aliases: new Map(), + reason: 'swr', + priority: candidate.isSelected ? 80 : prepared.cached()?.quietIntervalMs ? 20 : 40, + dueAt: Math.min(expiresAt, Math.max(Date.now(), gate)), + queuedAt: this.queue.nextOrder() + } + job = { + prepared, + promoteSelected: () => { + if (!entry.candidate.isSelected) { + entry.candidate = { ...entry.candidate, isSelected: true } + entry.priority = 80 + this.wake() + } + }, + expiresAt, + listeners, + finish: (outcome) => { + releaseHold() + if (this.jobs.get(key) === job) { + this.jobs.delete(key) + } + const listenersAtCompletion = Array.from(listeners) + for (const listener of listenersAtCompletion) { + listener(outcome) + } + listeners.clear() + } + } + this.jobs.set(key, job) + entry.hosted = job + this.queue.set(key, entry) + this.wake() + } + if (candidate.isSelected && !job.running) { + job.promoteSelected() + } + if ( + !acceptRevalidationOnly || + this.waiters >= MAX_WAITERS || + Date.now() >= deadline || + Math.max(gate - Date.now(), this.pacing.nextBudgetDelay()) > deadline - Date.now() + ) { + return fallback() + } + const waitingJob = job + return new Promise((resolve, reject) => { + let timer: ReturnType + const settle = (outcome: PRRefreshOutcome | null): void => { + cleanup() + void this.complete(prepared, candidate, outcome, acceptRevalidationOnly).then( + resolve, + reject + ) + } + const abort = (): void => { + cleanup() + reject(new Error('Pull request wait was cancelled')) + } + const cleanup = (): void => { + if (!waitingJob.listeners.delete(settle)) { + return + } + this.waiters -= 1 + clearTimeout(timer) + signal?.removeEventListener('abort', abort) + } + waitingJob.listeners.add(settle) + this.waiters += 1 + signal?.addEventListener('abort', abort, { once: true }) + timer = setTimeout( + () => { + cleanup() + try { + resolve(fallback()) + } catch (error) { + reject(error) + } + }, + Math.max(0, deadline - Date.now()) + ) + if (signal?.aborted) { + abort() + } + }) + } + completeFromCache(entry: PRRefreshQueueEntry): boolean { + const job = entry.hosted + if (!job) { + return false + } + const current = job.prepared.isCurrent() + const fresh = current ? this.fresh(job.prepared) : null + if (job.expiresAt > Date.now() && current && !fresh && job.prepared.canRetain()) { + return false + } + this.queue.delete(entry.key) + job.finish(current && job.expiresAt > Date.now() ? fresh : null) + return true + } +} diff --git a/src/main/github/pull-requests/refresh/pr-refresh-candidate-policy.ts b/src/main/github/pull-requests/refresh/pr-refresh-candidate-policy.ts index de532e3d8ad..e3a3e12355f 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-candidate-policy.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-candidate-policy.ts @@ -1,5 +1,8 @@ import { shouldAcceptMergedFallbackPR } from '../../../../shared/github/pull-requests/refresh/pr-branch-fallback-rule' -import { reviewRefreshIntervalMs } from '../../../../shared/review-refresh-policy' +import { + REVIEW_REFRESH_COOLDOWN_MS, + reviewRefreshIntervalMs +} from '../../../../shared/review-refresh-policy' import type { GitHubPRRefreshAlias, GitHubPRRefreshCandidate, @@ -10,6 +13,8 @@ import type { import type { GitHubPRBranchLookupOptions } from '../../client/lookup/pull-request-lookup-data' import { PR_CHECK_PAGE_LIMIT } from '../../client/check/pr-check-rollup' import { parsePRKey } from '../../pr-watch/pr-watch-types' +import { quietPRIntervalForCandidate } from '../../client/lookup/pr-quiet-policy-evidence' +import { cachedPRBranchOutcome } from '../../client/lookup/pr-read-identity' export const MANUAL_MERGEABILITY_PENDING_REFRESH_MS = 2_500 export const POST_PUSH_DELAY_MS = 2_500 @@ -26,6 +31,9 @@ export function hostedReviewOptionArgs( ): [] | [GitHubPRBranchLookupOptions] { const options: GitHubPRBranchLookupOptions = requestOptions.userRefresh === true ? { userRefresh: true } : {} + if (bypassesFreshnessDelay(reason)) { + options.resetQuietBackoff = true + } options.localGitExecOptions = { ...(candidate.localGitOptions?.wslDistro ? { wslDistro: candidate.localGitOptions.wslDistro } @@ -126,10 +134,52 @@ export function shouldSkipFresh( return Date.now() - candidate.cachedFetchedAt < refreshIntervalForCandidate(candidate) } -export function freshRetryAt(candidate: GitHubPRRefreshCandidate): number | null { +export function foregroundRefreshAgeMs(candidate: GitHubPRRefreshCandidate): number { + const interval = refreshIntervalForCandidate(candidate) + return Number.isFinite(interval) ? interval : REVIEW_REFRESH_COOLDOWN_MS +} + +export function foregroundExposureDueAt( + candidate: GitHubPRRefreshCandidate, + reexposed: boolean +): number | null { + return reexposed && + !Number.isFinite(refreshIntervalForCandidate(candidate)) && + candidate.cachedFetchedAt != null && + Date.now() - candidate.cachedFetchedAt >= foregroundRefreshAgeMs(candidate) + ? Date.now() + : null +} + +export function freshRetryAt( + candidate: GitHubPRRefreshCandidate, + foreground = false +): number | null { return candidate.cachedFetchedAt == null || hasStaleHead(candidate) ? null - : candidate.cachedFetchedAt + refreshIntervalForCandidate(candidate) + : candidate.cachedFetchedAt + + (foreground ? foregroundRefreshAgeMs(candidate) : refreshIntervalForCandidate(candidate)) +} + +export function freshQueuedPROutcome( + candidate: GitHubPRRefreshCandidate, + reason: GitHubPRRefreshReason, + foreground = false +): PRRefreshOutcome | null { + const cached = cachedPRBranchOutcome( + candidate.repoPath, + candidate.branch, + candidate.linkedPRNumber ?? null, + candidate.connectionId ?? null, + candidate.linkedPRNumber == null ? (candidate.fallbackPRNumber ?? null) : null, + hostedReviewOptionArgs(candidate, reason)[0] ?? {} + ) + const outcome = cached?.outcome + if (!outcome || outcome.kind === 'upstream-error') { + return null + } + const dueAt = freshRetryAt(visibleCandidateAfterOutcome(candidate, outcome), foreground) + return dueAt !== null && dueAt > Date.now() ? outcome : null } export function aliasFromCandidate(candidate: GitHubPRRefreshCandidate): GitHubPRRefreshAlias { @@ -189,6 +239,12 @@ export function hasStaleHead(candidate: GitHubPRRefreshCandidate): boolean { } export function refreshIntervalForCandidate(candidate: GitHubPRRefreshCandidate): number { + if (candidate.cachedFetchedAt != null && !hasStaleHead(candidate)) { + const quiet = quietPRIntervalForCandidate(candidate) + if (quiet !== null) { + return quiet + } + } return ( reviewRefreshIntervalMs({ state: candidate.cachedPRState, diff --git a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-active-visible-priority.test.ts b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-active-visible-priority.test.ts index 14c258f2d5c..61692bcb6c6 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-active-visible-priority.test.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-active-visible-priority.test.ts @@ -8,9 +8,7 @@ const { coordinatorMocks, moduleMocks } = await vi.hoisted(async () => { vi.mock('./pr-refresh-renderer-port', () => moduleMocks.rendererPortModuleMock(coordinatorMocks)) vi.mock('../../client/lookup/pr-for-branch-outcome', () => { - return { - getPRForBranchOutcome: coordinatorMocks.getPRForBranchOutcomeMock - } + return moduleMocks.clientModuleMock(coordinatorMocks) }) vi.mock('../../repositories/github-api-repository-remote-probe', () => { return { diff --git a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-alias-coalescing.test.ts b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-alias-coalescing.test.ts index db6f0138965..721960799a4 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-alias-coalescing.test.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-alias-coalescing.test.ts @@ -7,9 +7,7 @@ const { coordinatorMocks, moduleMocks } = await vi.hoisted(async () => { vi.mock('./pr-refresh-renderer-port', () => moduleMocks.rendererPortModuleMock(coordinatorMocks)) vi.mock('../../client/lookup/pr-for-branch-outcome', () => { - return { - getPRForBranchOutcome: coordinatorMocks.getPRForBranchOutcomeMock - } + return moduleMocks.clientModuleMock(coordinatorMocks) }) vi.mock('../../repositories/github-api-repository-remote-probe', () => { return { diff --git a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-cache-follow-up.test.ts b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-cache-follow-up.test.ts new file mode 100644 index 00000000000..830cb2b2295 --- /dev/null +++ b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-cache-follow-up.test.ts @@ -0,0 +1,162 @@ +import { afterEach, beforeEach, expect, it, vi } from 'vitest' +import type { PRRefreshOutcome } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { makeCandidate, makePR } from './pr-refresh-coordinator-test-harness' + +const mocks = vi.hoisted(() => ({ resolve: vi.fn(), send: vi.fn(), selection: 0 })) +vi.mock('../../client/lookup/branch-lookup-resolution', () => ({ + resolvePRForBranchOutcome: mocks.resolve +})) +vi.mock('../../accounts/github-project-account', () => ({ + githubPRPolicyProjects: () => ({ source: null, selection: mocks.selection }), + resolveGitHubProjectAccount: () => null +})) +vi.mock('../../execution/github-operation-slots', () => ({ + acquire: async () => {}, + release: () => {} +})) +vi.mock('./pr-refresh-rate-limit-gate', () => ({ prRefreshRateLimitPausedUntil: async () => null })) +vi.mock('./pr-refresh-renderer-port', () => ({ + prRefreshRenderers: () => ({ isRendererOpen: () => true, sendToTrustedUI: mocks.send }) +})) + +beforeEach(() => { + vi.resetModules() + vi.useFakeTimers() + vi.setSystemTime(1_000) + mocks.selection = 0 + mocks.resolve.mockReset() + mocks.send.mockReset() +}) +afterEach(() => { + vi.clearAllTimers() + vi.useRealTimers() +}) + +function found(overrides: Parameters[0] = {}): PRRefreshOutcome { + return { + kind: 'found', + fetchedAt: Date.now(), + pr: makePR({ + checksStatus: 'success', + checksReadComplete: true, + checksFinal: true, + autoMergeEnabled: false, + mergeQueueRequired: false, + ...overrides + }) + } +} + +it('delivers a real refresh after quiet evidence retires without repeating the cached follow-up', async () => { + mocks.resolve.mockImplementation(async () => found()) + const coordinator = await import('./pr-refresh-coordinator') + const { getPRForBranchOutcome } = await import('../../client/lookup/pr-for-branch-outcome') + const candidate = makeCandidate({ currentHeadOid: 'head-sha' }) + const seed = () => + coordinator.runHostedPRRead(candidate, () => + getPRForBranchOutcome('/repo', candidate.branch, null, null, null, { + currentHeadOid: 'head-sha' + }) + ) + await seed() + vi.setSystemTime(2_000) + await seed() + const visible = { + ...candidate, + cachedFetchedAt: 2_000, + cachedHeadOid: 'head-sha', + cachedHasPR: true, + cachedPRState: 'open' as const, + cachedChecksStatus: 'success' as const + } + coordinator.reportVisiblePRRefreshCandidates([visible], 1, 1) + await vi.advanceTimersByTimeAsync(0) + expect(mocks.resolve).toHaveBeenCalledTimes(2) + vi.setSystemTime(132_001) + mocks.selection++ + mocks.resolve.mockImplementation(async () => found({ title: 'updated' })) + mocks.send.mockClear() + mocks.send.mockImplementation(() => { + // Bound the unfixed synchronous loop even though its fake clock would otherwise never advance. + if (mocks.send.mock.calls.length === 8) { + vi.setSystemTime(1_000_000) + } + }) + coordinator.reportVisiblePRRefreshCandidates([visible], 2, 1) + await vi.advanceTimersByTimeAsync(0) + expect(mocks.resolve).toHaveBeenCalledTimes(3) + expect(mocks.send.mock.calls.length).toBeLessThanOrEqual(2) + expect(mocks.send.mock.calls.at(-1)?.[1]).toMatchObject({ + outcome: { fetchedAt: 132_001, pr: { title: 'updated' } } + }) + await vi.advanceTimersByTimeAsync(120_001) + expect(mocks.send.mock.calls.at(-1)?.[1]).toMatchObject({ + outcome: { fetchedAt: 252_001, pr: { title: 'updated' } } + }) +}) + +it('rereads unresolved manual mergeability at its forced follow-up despite the timed cache', async () => { + mocks.resolve + .mockImplementationOnce(async () => + found({ mergeable: 'UNKNOWN', mergeStateStatus: 'UNKNOWN' }) + ) + .mockImplementation(async () => found({ mergeable: 'MERGEABLE', mergeStateStatus: 'CLEAN' })) + const coordinator = await import('./pr-refresh-coordinator') + const candidate = makeCandidate({ currentHeadOid: 'head-sha' }) + coordinator.reportVisiblePRRefreshCandidates([candidate], 1, 1) + expect(await coordinator.refreshPRNow(candidate)).toMatchObject({ + pr: { mergeable: 'UNKNOWN' } + }) + await vi.advanceTimersByTimeAsync(2_501) + expect(mocks.resolve).toHaveBeenCalledTimes(2) + expect(mocks.send.mock.calls.at(-1)?.[1]).toMatchObject({ + outcome: { pr: { mergeable: 'MERGEABLE', mergeStateStatus: 'CLEAN' } } + }) +}) + +it.each([false, true])( + 'refreshes a settled row on re-exposure and shares a newer exact answer (refreshed while queued: %s)', + async (refreshedWhileQueued) => { + mocks.resolve.mockImplementation(async () => found({ state: 'merged' })) + const coordinator = await import('./pr-refresh-coordinator') + const { getPRForBranchOutcome } = await import('../../client/lookup/pr-for-branch-outcome') + const candidate = makeCandidate({ currentHeadOid: 'head-sha', linkedPRNumber: 12 }) + const seed = () => + coordinator.runHostedPRRead(candidate, () => + getPRForBranchOutcome('/repo', candidate.branch, 12, null, null, { + currentHeadOid: 'head-sha', + userRefresh: true + }) + ) + await seed() + const visible = { + ...candidate, + cachedFetchedAt: 1_000, + cachedHeadOid: 'head-sha', + cachedHasPR: true, + cachedPRState: 'merged' as const, + cachedChecksStatus: 'success' as const + } + coordinator.reportVisiblePRRefreshCandidates([visible], 1, 1) + coordinator.reportVisiblePRRefreshCandidates([], 2, 1) + vi.setSystemTime(2_000) + coordinator.reportVisiblePRRefreshCandidates([visible], 3, 1) + await vi.advanceTimersByTimeAsync(0) + expect(mocks.resolve).toHaveBeenCalledTimes(1) + coordinator.reportVisiblePRRefreshCandidates([], 4, 1) + vi.setSystemTime(21_000) + mocks.resolve.mockImplementation(async () => found({ state: 'merged', title: 'updated' })) + mocks.send.mockClear() + coordinator.reportVisiblePRRefreshCandidates([visible], 5, 1) + if (refreshedWhileQueued) { + await seed() + } + await vi.advanceTimersByTimeAsync(0) + expect(mocks.send.mock.calls.at(-1)?.[1]).toMatchObject({ + outcome: { fetchedAt: 21_000, pr: { state: 'merged', title: 'updated' } } + }) + expect(mocks.resolve).toHaveBeenCalledTimes(2) + await vi.advanceTimersByTimeAsync(600_000) + expect(mocks.resolve).toHaveBeenCalledTimes(2) + } +) diff --git a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-completion-order.test.ts b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-completion-order.test.ts index f5470dd7ad2..fd787cc66fe 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-completion-order.test.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-completion-order.test.ts @@ -9,9 +9,7 @@ const { coordinatorMocks, moduleMocks } = await vi.hoisted(async () => { }) vi.mock('./pr-refresh-renderer-port', () => moduleMocks.rendererPortModuleMock(coordinatorMocks)) vi.mock('../../client/lookup/pr-for-branch-outcome', () => { - return { - getPRForBranchOutcome: coordinatorMocks.getPRForBranchOutcomeMock - } + return moduleMocks.clientModuleMock(coordinatorMocks) }) vi.mock('../../repositories/github-api-repository-remote-probe', () => { return { diff --git a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-test-mocks.ts b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-test-mocks.ts index cc79d26e1ea..063957a1c12 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-test-mocks.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-test-mocks.ts @@ -1,4 +1,8 @@ import type { PRRefreshRendererPort } from './pr-refresh-renderer-port' +import type { + PreparedPRBranchRead, + preparePRBranchRead +} from '../../client/lookup/pr-for-branch-outcome' import { vi, type Mock } from 'vitest' import { isWslUncPath } from '../../../../shared/wsl-paths' @@ -51,11 +55,44 @@ export function rendererPortModuleMock(mocks: PRRefreshCoordinatorMocks): Render return { prRefreshRenderers: () => port } } -export type ClientModuleMock = { getPRForBranchOutcome: Mock } +export type ClientModuleMock = { + getPRForBranchOutcome: Mock + preparePRBranchRead: typeof preparePRBranchRead +} export function clientModuleMock(mocks: PRRefreshCoordinatorMocks): ClientModuleMock { return { - getPRForBranchOutcome: mocks.getPRForBranchOutcomeMock + getPRForBranchOutcome: mocks.getPRForBranchOutcomeMock, + preparePRBranchRead: async (...args) => { + const prepared: PreparedPRBranchRead = { + key: '', + reprepare: async () => prepared, + cached: () => null, + isCurrent: () => true, + accepts: () => true, + didRead: () => true, + release: () => {}, + hold: () => () => {}, + canRetain: () => true, + scheduleError: () => {}, + pending: () => null, + withExecution: (operation) => operation(), + read: async (settle) => { + const outcome = await mocks.getPRForBranchOutcomeMock(...args) + return settle ? settle(outcome, { retained: true }) : outcome + }, + readResult: async (settle) => ({ + outcome: await prepared.read(settle), + sequence: 0, + current: () => true + }), + readRetained: async (settle) => { + const outcome = await mocks.getPRForBranchOutcomeMock(...args) + return settle ? settle(outcome, { retained: true }) : outcome + } + } + return prepared + } } } diff --git a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-wsl-stopped-distro.test.ts b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-wsl-stopped-distro.test.ts index ae9ffdc3d13..f14db379010 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-wsl-stopped-distro.test.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-wsl-stopped-distro.test.ts @@ -13,9 +13,7 @@ const gate = vi.hoisted(() => ({ vi.mock('./pr-refresh-renderer-port', () => moduleMocks.rendererPortModuleMock(coordinatorMocks)) vi.mock('../../client/lookup/pr-for-branch-outcome', () => { - return { - getPRForBranchOutcome: coordinatorMocks.getPRForBranchOutcomeMock - } + return moduleMocks.clientModuleMock(coordinatorMocks) }) vi.mock('../../repositories/github-api-repository-remote-probe', () => { return { diff --git a/src/main/github/pull-requests/refresh/pr-refresh-coordinator.ts b/src/main/github/pull-requests/refresh/pr-refresh-coordinator.ts index 231f2911d74..522c1252ff1 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-coordinator.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-coordinator.ts @@ -1,14 +1,18 @@ +import { sharedPromiseSettlementWaiters } from '../../../../shared/promise-settlement-waiters' +import { runImmediatePRRefresh } from './pr-refresh-immediate-read' +import { + deliverablePRBranchOutcome, + type PRBranchReadResult +} from '../../client/lookup/pr-branch-read-resources' import type { GitHubPRRefreshCandidate, GitHubPRRefreshReason, - PRRefreshOutcome + PRRefreshOutcome, + PRSnapshotReply } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' -import { getPRForBranchOutcome } from '../../client/lookup/pr-for-branch-outcome' +import type { PreparedPRBranchRead } from '../../client/lookup/pr-for-branch-outcome' import { aliasFromCandidate, - hostedReviewOptionArgs, - isBackground, - MANUAL_MERGEABILITY_PENDING_REFRESH_MS, refreshKey, shouldBroadcastQueued, validateCandidate @@ -26,13 +30,9 @@ import { type PRWatchJob, type PRWatchJobHooks } from './pr-refresh-watch-jobs' -import { prRefreshRateLimitPausedUntil } from './pr-refresh-rate-limit-gate' import { PRRefreshRetryState } from './pr-refresh-retry-state' import { PRRefreshVisibility } from './pr-refresh-visibility' -import { - blockedWslDistroForPath, - wslDistroForPath -} from '../../../wsl/filesystem/wsl-unc-access-gate' +import { refreshPRPolicyContext } from '../../client/lookup/pr-quiet-policy-evidence' const retry = new PRRefreshRetryState() const queue = new PRRefreshQueue((key) => retry.reset(key)) @@ -46,7 +46,7 @@ export function addPRRefreshOutcomeObserver(observer: PRRefreshOutcomeObserver): } export function setPRRefreshRegistrationCheck(check: PRRefreshRegistrationCheck | null): void { - drainer.setRegistrationCheck(check) + drainer.registrationCheck = check } export function setPRWatchJobHooks(hooks: PRWatchJobHooks | null): void { @@ -72,11 +72,71 @@ export function cancelPRWatchRead(job: PRWatchJob): void { * so a desktop read in flight for the same checkout keeps its follow-up. */ export async function runHostedPRRead( candidate: GitHubPRRefreshCandidate, - read: () => Promise + read: (() => Promise) | PreparedPRBranchRead, + didRead: () => boolean = () => true, + release: () => void = () => {} ): Promise { - const outcome = await read() - events.observe(candidate, outcome) - return outcome + try { + if (typeof read !== 'function') { + const result = await read.readResult((outcome) => { + events.observe(candidate, outcome) + return outcome + }) + return result!.outcome + } + const outcome = await read() + if (didRead()) { + events.observe(candidate, outcome) + } + return outcome + } finally { + if (typeof read !== 'function') { + read.release() + } else { + release() + } + } +} + +export async function runHostedPRSnapshot( + candidate: GitHubPRRefreshCandidate, + prepared: PreparedPRBranchRead, + deadline: number, + signal?: AbortSignal, + acceptRevalidationOnly = false +): Promise { + if (prepared.cached() || drainer.hostedJobs.has(prepared)) { + return drainer.hostedJobs.read(prepared, candidate, deadline, signal, acceptRevalidationOnly) + } + if (acceptRevalidationOnly && !prepared.canRetain()) { + return drainer.hostedJobs.complete(prepared, candidate, null, acceptRevalidationOnly) + } + const pending = prepared.pending() + if (pending) { + const timeout = new Error('Pull request snapshot wait timed out') + let result: PRBranchReadResult + try { + result = await sharedPromiseSettlementWaiters(pending).wait({ + signal, + ...(acceptRevalidationOnly + ? { timeoutMs: Math.max(0, deadline - Date.now()), createTimeoutError: () => timeout } + : {}), + createAbortError: () => new Error('Pull request wait was cancelled') + }) + } catch (error) { + if (error === timeout) { + return drainer.hostedJobs.complete(prepared, candidate, null, acceptRevalidationOnly) + } + throw error + } + return drainer.hostedJobs.complete( + prepared, + candidate, + acceptRevalidationOnly ? deliverablePRBranchOutcome(prepared, result) : result.outcome, + acceptRevalidationOnly + ) + } + return drainer.hostedJobs.readCold(candidate, prepared, acceptRevalidationOnly) } function removeInvisibleVisibleRefreshes(): void { @@ -139,6 +199,7 @@ export function reportVisiblePRRefreshCandidates( generation: number, windowId: number ): void { + refreshPRPolicyContext() const newlyExposed = new Set(candidates.map(refreshKey).filter((key) => !visibility.has(key))) if (!visibility.report(candidates, generation, windowId)) { return @@ -178,110 +239,15 @@ export function _getPRRefreshAliasCountForTests(key: string): number { return queue.aliasCount(key) } -export async function refreshPRNow( +export function refreshPRNow( candidate: GitHubPRRefreshCandidate, reason: GitHubPRRefreshReason = 'manual', options: { userRefresh?: boolean } = {} -): Promise { - const alias = aliasFromCandidate(candidate) - const key = refreshKey(candidate) - const existing = queue.get(key) - const aliasMap = new Map(existing ? existing.aliases : []) - aliasMap.set(alias.cacheKey, alias) - const aliases = Array.from(aliasMap.values()) - const skippedReason = validateCandidate(candidate) - if (skippedReason) { - queue.removeInvalidAlias(key, alias) - const outcome: PRRefreshOutcome = { - kind: 'upstream-error', - errorType: 'unknown', - message: `Cannot refresh PR for this worktree: ${skippedReason}`, - fetchedAt: Date.now() - } - events.broadcast({ aliases: [alias], reason, status: 'skipped', skippedReason }) - return outcome - } - // E9: renderer fallbacks reach here with automatic reasons; only a manual refresh may start a stopped distro. - if ( - isBackground(reason) && - wslDistroForPath(candidate.repoPath, candidate.localGitOptions?.wslDistro) && - (await blockedWslDistroForPath(candidate.repoPath, candidate.localGitOptions?.wslDistro)) - ) { - events.broadcast({ aliases: [alias], reason, status: 'skipped', skippedReason: 'fresh' }) - // Local callers keep their cached PR on an upstream error; nothing is recorded or retried here. - return { - kind: 'upstream-error', - errorType: 'unknown', - message: 'WSL distro is not running', - fetchedAt: Date.now() - } - } - - const primaryGateUntil = await prRefreshRateLimitPausedUntil(candidate, false) - const gateUntil = Math.max(primaryGateUntil ?? 0, retry.manualGateUntil(key)) - if (gateUntil > Date.now()) { - queue.protectBackgroundUntil(key, gateUntil) - queue.set(key, { - key, - candidate, - aliases: aliasMap, - reason, - priority: 40, - dueAt: gateUntil, - queuedAt: queue.nextOrder() - }) - events.broadcast({ - aliases, - reason, - status: 'paused', - pausedUntil: gateUntil, - skippedReason: 'rate-limit' - }) - drainer.schedule(Math.max(1_000, gateUntil - Date.now())) - return { - kind: 'upstream-error', - errorType: 'rate_limited', - message: 'GitHub is temporarily limiting requests. Try again after the limit resets.', - fetchedAt: Date.now(), - nextAutoRetryAt: gateUntil, - retryDisabledUntil: gateUntil - } - } - - queue.delete(key) - const requestSequence = events.nextSequence() - const requestStartedAt = Date.now() - queue.noteRequestStarted(key, requestSequence) - events.broadcast({ aliases, reason, status: 'in-flight', requestStartedAt }, requestSequence) - const outcome = await getPRForBranchOutcome( - candidate.repoPath, - candidate.branch, - candidate.linkedPRNumber ?? null, - candidate.connectionId ?? null, - candidate.linkedPRNumber == null ? (candidate.fallbackPRNumber ?? null) : null, - ...hostedReviewOptionArgs(candidate, reason, options) +): Promise { + return runImmediatePRRefresh( + { queue, retry, visibility, events, drainer }, + candidate, + reason, + options ) - if (!queue.ownsRequest(key, requestSequence)) { - events.broadcast({ aliases, reason, outcome, requestStartedAt }, requestSequence) - return outcome - } - let plannedRetryAt: number | undefined - let broadcastOutcome = outcome - if (outcome.kind === 'upstream-error' && visibility.has(key)) { - plannedRetryAt = retry.nextVisibleErrorRetryAt(key, visibility.candidate(key, candidate)) - broadcastOutcome = retry.withErrorSchedule(outcome, plannedRetryAt) - } - events.observe(candidate, outcome, aliases) - retry.noteManualGate(key, broadcastOutcome) - events.broadcast( - { aliases, reason, outcome: broadcastOutcome, requestStartedAt }, - requestSequence - ) - drainer.scheduleVisibleFollowUp(key, candidate, outcome, 40, aliases, undefined, { - plannedRetryAt, - ...(reason === 'manual' - ? { pendingMergeabilityDelayMs: MANUAL_MERGEABILITY_PENDING_REFRESH_MS } - : {}) - }) - return broadcastOutcome } diff --git a/src/main/github/pull-requests/refresh/pr-refresh-event-publisher.ts b/src/main/github/pull-requests/refresh/pr-refresh-event-publisher.ts index 28af9df1b82..1f16af98ef5 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-event-publisher.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-event-publisher.ts @@ -16,6 +16,8 @@ export type PRRefreshOutcomeObserver = ( aliases: readonly GitHubPRRefreshAlias[] ) => void +import { projectPRPolicyOutcome } from '../../client/lookup/pr-quiet-policy-evidence' + const DIAGNOSTIC_BREADCRUMB_MIN_INTERVAL_MS = 30_000 export class PRRefreshEventPublisher { @@ -35,6 +37,7 @@ export class PRRefreshEventPublisher { outcome: PRRefreshOutcome, aliases: readonly GitHubPRRefreshAlias[] = [] ): void { + projectPRPolicyOutcome(candidate, outcome) for (const observer of this.observers) { try { observer(candidate, outcome, aliases) diff --git a/src/main/github/pull-requests/refresh/pr-refresh-immediate-read.ts b/src/main/github/pull-requests/refresh/pr-refresh-immediate-read.ts new file mode 100644 index 00000000000..8438d1a3604 --- /dev/null +++ b/src/main/github/pull-requests/refresh/pr-refresh-immediate-read.ts @@ -0,0 +1,173 @@ +import type { + GitHubPRRefreshCandidate, + GitHubPRRefreshReason, + PRRefreshOutcome, + PRSnapshotReply +} from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { preparePRBranchRead } from '../../client/lookup/pr-for-branch-outcome' +import { deliverablePRBranchOutcome } from '../../client/lookup/pr-branch-read-resources' +import { + aliasFromCandidate, + refreshKey, + validateCandidate, + isBackground, + hostedReviewOptionArgs, + MANUAL_MERGEABILITY_PENDING_REFRESH_MS +} from './pr-refresh-candidate-policy' +import { + blockedWslDistroForPath, + wslDistroForPath +} from '../../../wsl/filesystem/wsl-unc-access-gate' +import { prRefreshRateLimitPausedUntil } from './pr-refresh-rate-limit-gate' +import type { PRRefreshQueue } from './pr-refresh-queue' +import type { PRRefreshRetryState } from './pr-refresh-retry-state' +import type { PRRefreshVisibility } from './pr-refresh-visibility' +import type { PRRefreshEventPublisher } from './pr-refresh-event-publisher' +import type { PRRefreshQueueDrainer } from './pr-refresh-queue-drainer' + +type ImmediateRefreshOwners = { + queue: PRRefreshQueue + retry: PRRefreshRetryState + visibility: PRRefreshVisibility + events: PRRefreshEventPublisher + drainer: PRRefreshQueueDrainer +} +export async function runImmediatePRRefresh( + { queue, retry, visibility, events, drainer }: ImmediateRefreshOwners, + candidate: GitHubPRRefreshCandidate, + reason: GitHubPRRefreshReason = 'manual', + options: { userRefresh?: boolean } = {} +): Promise { + const alias = aliasFromCandidate(candidate) + const key = refreshKey(candidate) + const existing = queue.get(key) + const aliasMap = new Map(existing ? existing.aliases : []) + aliasMap.set(alias.cacheKey, alias) + const aliases = Array.from(aliasMap.values()) + const skippedReason = validateCandidate(candidate) + if (skippedReason) { + queue.removeInvalidAlias(key, alias) + const outcome: PRRefreshOutcome = { + kind: 'upstream-error', + errorType: 'unknown', + message: `Cannot refresh PR for this worktree: ${skippedReason}`, + fetchedAt: Date.now() + } + events.broadcast({ aliases: [alias], reason, status: 'skipped', skippedReason }) + return outcome + } + // E9: renderer fallbacks reach here with automatic reasons; only a manual refresh may start a stopped distro. + if ( + isBackground(reason) && + wslDistroForPath(candidate.repoPath, candidate.localGitOptions?.wslDistro) && + (await blockedWslDistroForPath(candidate.repoPath, candidate.localGitOptions?.wslDistro)) + ) { + events.broadcast({ aliases: [alias], reason, status: 'skipped', skippedReason: 'fresh' }) + // Local callers keep their cached PR on an upstream error; nothing is recorded or retried here. + return { + kind: 'upstream-error', + errorType: 'unknown', + message: 'WSL distro is not running', + fetchedAt: Date.now() + } + } + + const primaryGateUntil = await prRefreshRateLimitPausedUntil(candidate, false) + const gateUntil = Math.max(primaryGateUntil ?? 0, retry.manualGateUntil(key)) + if (gateUntil > Date.now()) { + queue.protectBackgroundUntil(key, gateUntil) + queue.set(key, { + key, + candidate, + aliases: aliasMap, + reason, + priority: 40, + dueAt: gateUntil, + queuedAt: queue.nextOrder() + }) + events.broadcast({ + aliases, + reason, + status: 'paused', + pausedUntil: gateUntil, + skippedReason: 'rate-limit' + }) + drainer.schedule(Math.max(1_000, gateUntil - Date.now())) + return { + kind: 'upstream-error', + errorType: 'rate_limited', + message: 'GitHub is temporarily limiting requests. Try again after the limit resets.', + fetchedAt: Date.now(), + nextAutoRetryAt: gateUntil, + retryDisabledUntil: gateUntil + } + } + + queue.delete(key) + const requestSequence = events.nextSequence() + const requestStartedAt = Date.now() + queue.noteRequestStarted(key, requestSequence) + events.broadcast({ aliases, reason, status: 'in-flight', requestStartedAt }, requestSequence) + const prepared = await preparePRBranchRead( + candidate.repoPath, + candidate.branch, + candidate.linkedPRNumber ?? null, + candidate.connectionId ?? null, + candidate.linkedPRNumber == null ? (candidate.fallbackPRNumber ?? null) : null, + ...hostedReviewOptionArgs(candidate, reason, options) + ) + try { + const result = await prepared.readResult( + drainer.scheduler.settleRead(key, candidate, aliases, prepared, requestSequence) + ) + if (!result) { + throw new Error('An unrestricted PR read must return its own result') + } + const outcome = deliverablePRBranchOutcome(prepared, result) + if (!outcome) { + if (queue.ownsRequest(key, requestSequence)) { + events.broadcast( + { aliases, reason, status: 'skipped', skippedReason: 'fresh' }, + requestSequence + ) + drainer.scheduler.completeWithoutOutcome( + key, + candidate, + aliases, + 40, + undefined, + !isBackground(reason) + ) + } + return { + revalidation: { invalidated: true, retryAfterMs: drainer.hostedJobs.retryAfterMs(prepared) } + } + } + if (!queue.ownsRequest(key, requestSequence)) { + events.broadcast({ aliases, reason, outcome, requestStartedAt }, requestSequence) + return outcome + } + let plannedRetryAt: number | undefined + let broadcastOutcome = outcome + if (outcome.kind === 'upstream-error' && visibility.has(key)) { + plannedRetryAt = + outcome.nextAutoRetryAt ?? + retry.nextVisibleErrorRetryAt(key, visibility.candidate(key, candidate)) + broadcastOutcome = retry.withErrorSchedule(outcome, plannedRetryAt) + } + retry.noteManualGate(key, broadcastOutcome) + events.broadcast( + { aliases, reason, outcome: broadcastOutcome, requestStartedAt }, + requestSequence + ) + drainer.scheduleVisibleFollowUp(key, candidate, outcome, 40, aliases, undefined, { + plannedRetryAt, + ...(reason === 'manual' + ? { pendingMergeabilityDelayMs: MANUAL_MERGEABILITY_PENDING_REFRESH_MS } + : {}) + }) + return broadcastOutcome + } finally { + prepared.release() + } +} diff --git a/src/main/github/pull-requests/refresh/pr-refresh-pacing.ts b/src/main/github/pull-requests/refresh/pr-refresh-pacing.ts index 2013a5e05ae..0353b30c4b4 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-pacing.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-pacing.ts @@ -7,6 +7,13 @@ const BACKGROUND_BUDGET_MAX = 20 const ACTIVE_BURST_WINDOW_MS = 30_000 const ACTIVE_BURST_MAX = 3 +export function isSelectedBudgetedPRRefresh(entry: PRRefreshQueueEntry): boolean { + return ( + entry.candidate.isSelected === true && + (entry.reason === 'visible' || (entry.reason === 'swr' && entry.hosted !== undefined)) + ) +} + export function usesActiveRefreshPacing(entry: PRRefreshQueueEntry): boolean { // Selected admission survives deselection while queued; periodic follow-ups clear the flag. return ( @@ -31,8 +38,7 @@ export class PRRefreshPacing { } noteBackgroundStart(entry: PRRefreshQueueEntry): void { - this.lastBudgetedStartWasSelected = - entry.reason === 'visible' && entry.candidate.isSelected === true + this.lastBudgetedStartWasSelected = isSelectedBudgetedPRRefresh(entry) const now = Date.now() this.lastBackgroundStartAt = now this.backgroundStarts.push(now) @@ -70,8 +76,7 @@ export class PRRefreshPacing { private priority(entry: PRRefreshQueueEntry): number { // Reserve the next budgeted slot for other ready reviews after a selected list read. return this.lastBudgetedStartWasSelected && - entry.reason === 'visible' && - entry.candidate.isSelected === true && + isSelectedBudgetedPRRefresh(entry) && entry.bypassBackgroundBudget !== true ? 0 : entry.priority diff --git a/src/main/github/pull-requests/refresh/pr-refresh-queue-drainer.ts b/src/main/github/pull-requests/refresh/pr-refresh-queue-drainer.ts index 17d3712655f..54c39d72422 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-queue-drainer.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-queue-drainer.ts @@ -1,41 +1,38 @@ -import type { - GitHubPRRefreshAlias, - GitHubPRRefreshCandidate, - PRRefreshOutcome -} from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' -import { getPRForBranchOutcome } from '../../client/lookup/pr-for-branch-outcome' +import { admitPreparedPRRead } from './pr-refresh-read-admission' +import { deliverablePRBranchOutcome } from '../../client/lookup/pr-branch-read-resources' +import type { GitHubPRRefreshCandidate } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { preparePRBranchRead } from '../../client/lookup/pr-for-branch-outcome' import { - aliasFromCandidate, - freshRetryAt, hostedReviewOptionArgs, + freshQueuedPROutcome, + bypassesFreshnessDelay, isBackground, isBudgetedBackground, - isMergeabilityPendingOutcome, - sameAliasRequestIdentity, - validateCandidate, - visibleCandidateAfterOutcome, - visibleFollowUpPriority + validateCandidate } from './pr-refresh-candidate-policy' import type { PRRefreshEventPublisher } from './pr-refresh-event-publisher' import { type PRRefreshPacing, usesActiveRefreshPacing } from './pr-refresh-pacing' -import type { PRRefreshQueue, PRRefreshQueueEntry } from './pr-refresh-queue' +import type { PRRefreshQueue } from './pr-refresh-queue' import { PRWatchJobRunner } from './pr-refresh-watch-jobs' import { prRefreshRateLimitPausedUntil } from './pr-refresh-rate-limit-gate' import { blockedWslDistroForPath, wslDistroForPath } from '../../../wsl/filesystem/wsl-unc-access-gate' -import { outsideWslUserAct } from '../../../wsl/discovery/wsl-user-act-scope' import type { PRRefreshRetryState } from './pr-refresh-retry-state' import type { PRRefreshVisibility } from './pr-refresh-visibility' +import { HostedPRReadJobs } from './hosted-pr-read-jobs' +import { PRRefreshQueueScheduler } from './pr-refresh-queue-scheduler' export type PRRefreshRegistrationCheck = (candidate: GitHubPRRefreshCandidate) => boolean export class PRRefreshQueueDrainer { private draining = false - private timer: ReturnType | null = null - private registrationCheck: PRRefreshRegistrationCheck | null = null + registrationCheck: PRRefreshRegistrationCheck | null = null readonly watchJobs: PRWatchJobRunner + readonly hostedJobs: HostedPRReadJobs + readonly scheduler: PRRefreshQueueScheduler + readonly scheduleVisibleFollowUp: PRRefreshQueueScheduler['scheduleVisibleFollowUp'] constructor( private readonly queue: PRRefreshQueue, @@ -45,110 +42,27 @@ export class PRRefreshQueueDrainer { private readonly events: PRRefreshEventPublisher ) { this.watchJobs = new PRWatchJobRunner(queue, retry, this) - } - - setRegistrationCheck(check: PRRefreshRegistrationCheck | null): void { - this.registrationCheck = check + this.scheduler = new PRRefreshQueueScheduler( + queue, + pacing, + visibility, + retry, + events, + this.watchJobs, + () => this.drain() + ) + this.scheduleVisibleFollowUp = this.scheduler.scheduleVisibleFollowUp.bind(this.scheduler) + this.hostedJobs = new HostedPRReadJobs( + queue, + pacing, + retry, + () => this.schedule(), + (candidate, outcome) => events.observe(candidate, outcome) + ) } schedule(delay = 0): void { - clearTimeout(this.timer ?? undefined) - this.timer = setTimeout(() => { - this.timer = null - // The drain serves every queued repo; a scheduling call inside a WSL user act must not lend it that act. - void outsideWslUserAct(() => this.drain()) - }, delay) - } - - scheduleVisibleFollowUp( - key: string, - candidate: GitHubPRRefreshCandidate, - outcome: PRRefreshOutcome, - priority: number, - aliases: GitHubPRRefreshAlias[], - windowId?: number, - options?: { pendingMergeabilityDelayMs?: number; plannedRetryAt?: number } - ): void { - if (outcome.kind === 'upstream-error') { - const retryAt = Math.max( - options?.plannedRetryAt ?? - this.retry.nextVisibleErrorRetryAt(key, this.visibility.candidate(key, candidate)), - this.retry.manualGateUntil(key) - ) - this.queue.protectBackgroundUntil(key, retryAt) - if (!this.visibility.has(key)) { - return - } - this.queue.setVisibleFollowUp({ - key, - candidate, - aliases: new Map(aliases.map((alias) => [alias.cacheKey, alias])), - reason: 'visible', - priority, - dueAt: retryAt, - queuedAt: this.queue.nextOrder(), - windowId - }) - this.schedule(retryAt - Date.now()) - return - } - this.retry.reset(key) - const refreshed = visibleCandidateAfterOutcome(candidate, outcome) - this.visibility.update(refreshed) - if (!this.visibility.has(key)) { - return - } - const followUpCandidate = this.visibility.candidate(key, refreshed) - const regularDueAt = freshRetryAt(followUpCandidate) ?? Date.now() - const pendingDueAt = - options?.pendingMergeabilityDelayMs !== undefined && isMergeabilityPendingOutcome(outcome) - ? outcome.fetchedAt + options.pendingMergeabilityDelayMs - : null - const dueAt = pendingDueAt === null ? regularDueAt : Math.min(regularDueAt, pendingDueAt) - if (!Number.isFinite(dueAt)) { - const pending = this.queue.get(key) - if ( - pending?.reason === 'visible' && - sameAliasRequestIdentity( - aliasFromCandidate(pending.candidate), - aliasFromCandidate(followUpCandidate) - ) - ) { - this.queue.delete(key) - } - return - } - this.queue.setVisibleFollowUp({ - key, - candidate: followUpCandidate, - aliases: new Map(aliases.map((alias) => [alias.cacheKey, alias])), - reason: 'visible', - priority: visibleFollowUpPriority(followUpCandidate, priority), - dueAt, - queuedAt: this.queue.nextOrder(), - bypassBackgroundBudget: pendingDueAt !== null, - followUp: true, - windowId - }) - this.schedule(Math.max(0, dueAt - Date.now())) - } - - private ordered(): PRRefreshQueueEntry[] { - this.watchJobs.age(this.queue.values(), Date.now()) - return this.queue.ordered((a, b) => this.pacing.order(a, b)) - } - - private nextQueuedWakeDelay(excludedKey: string): number | null { - const now = Date.now() - let nextDelay = Number.POSITIVE_INFINITY - for (const entry of this.queue.values()) { - if (entry.key === excludedKey) { - continue - } - const delay = entry.dueAt > now ? entry.dueAt - now : this.pacing.entryDelay(entry) - nextDelay = Math.min(nextDelay, delay) - } - return Number.isFinite(nextDelay) ? Math.max(0, nextDelay) : null + this.scheduler.schedule(delay) } private async drain(): Promise { @@ -157,8 +71,12 @@ export class PRRefreshQueueDrainer { } this.draining = true try { + const { queue, pacing, events, scheduler, watchJobs } = this while (this.queue.size > 0) { - let next = this.ordered()[0] + let next = this.scheduler.ordered()[0] + if (this.hostedJobs.completeFromCache(next)) { + continue + } const waitMs = next.dueAt - Date.now() if (waitMs > 0) { this.schedule(waitMs) @@ -167,23 +85,27 @@ export class PRRefreshQueueDrainer { let delay = this.pacing.entryDelay(next) if (delay > 0) { - const runnable = this.ordered().find( - (entry) => entry.dueAt <= Date.now() && this.pacing.entryDelay(entry) === 0 - ) + const runnable = this.scheduler + .ordered() + .find((entry) => entry.dueAt <= Date.now() && this.pacing.entryDelay(entry) === 0) if (runnable && runnable.key !== next.key) { next = runnable delay = 0 } else { - this.notePacingDelay(next) - this.schedule(Math.min(delay, this.nextQueuedWakeDelay(next.key) ?? delay)) + this.scheduler.notePacingDelay(next) + this.schedule(Math.min(delay, this.scheduler.nextQueuedWakeDelay(next.key) ?? delay)) return } } this.queue.delete(next.key) + if (this.hostedJobs.completeFromCache(next)) { + continue + } const aliases = Array.from(next.aliases.values()) const skippedReason = validateCandidate(next.candidate) if (skippedReason) { + next.hosted?.finish(null) this.events.record('skipped', next.reason, skippedReason) this.events.broadcast({ aliases, reason: next.reason, status: 'skipped', skippedReason }) continue @@ -199,6 +121,7 @@ export class PRRefreshQueueDrainer { next.candidate.localGitOptions?.wslDistro ))) ) { + next.hosted?.finish(null) this.events.broadcast({ aliases, reason: next.reason, @@ -208,70 +131,158 @@ export class PRRefreshQueueDrainer { this.watchJobs.skipped(next) continue } + if ( + !next.hosted && + !next.watch && + !next.bypassBackgroundBudget && + (next.reason === 'visible' || next.reason === 'swr') + ) { + const candidate = next.candidate + const fresh = freshQueuedPROutcome(candidate, next.reason, next.foreground) + if (fresh) { + this.events.broadcast({ aliases, reason: next.reason, outcome: fresh }) + this.scheduleVisibleFollowUp( + next.key, + candidate, + fresh, + next.priority, + aliases, + next.windowId + ) + continue + } + } const requestSequence = this.events.nextSequence() const requestStartedAt = Date.now() this.queue.noteRequestStarted(next.key, requestSequence) - this.events.broadcast( - { aliases, reason: next.reason, status: 'in-flight', requestStartedAt }, - requestSequence - ) - - if (isBackground(next.reason)) { - const pausedUntil = await prRefreshRateLimitPausedUntil(next.candidate, true) + if (!next.hosted) { + this.events.broadcast( + { aliases, reason: next.reason, status: 'in-flight', requestStartedAt }, + requestSequence + ) + } + let joinedRead = next.hosted?.prepared.pending() + if (isBackground(next.reason) && !joinedRead) { + const checkRateLimit = () => prRefreshRateLimitPausedUntil(next.candidate, true) + const pausedUntil = await (next.hosted + ? next.hosted.prepared.withExecution(checkRateLimit) + : checkRateLimit()) if (!this.queue.ownsRequest(next.key, requestSequence)) { + next.hosted?.finish(null) continue } if (pausedUntil !== null) { this.queue.protectBackgroundUntil(next.key, pausedUntil) - this.queue.set(next.key, { ...next, dueAt: pausedUntil }) - this.events.broadcast({ - aliases, - reason: next.reason, - status: 'paused', - pausedUntil, - skippedReason: 'rate-limit' - }) - this.schedule(Math.max(1_000, pausedUntil - Date.now())) + const dueAt = Math.min(pausedUntil, next.hosted?.expiresAt ?? Infinity) + next.dueAt = dueAt + this.queue.set(next.key, next) + if (!next.hosted) { + this.events.broadcast({ + aliases, + reason: next.reason, + status: 'paused', + pausedUntil, + skippedReason: 'rate-limit' + }) + } + this.schedule(Math.max(1_000, dueAt - Date.now())) continue } - if (next.bypassBackgroundBudget !== true && isBudgetedBackground(next.reason)) { + if (this.hostedJobs.completeFromCache(next)) { + continue + } + if ( + next.watch && + next.bypassBackgroundBudget !== true && + isBudgetedBackground(next.reason) + ) { this.pacing.noteBackgroundStart(next) this.watchJobs.noteBudgetedStart(next) } - if (usesActiveRefreshPacing(next)) { + if (next.watch && usesActiveRefreshPacing(next)) { this.pacing.noteActiveStart(next) } } - if (await this.watchJobs.readHead(next)) { continue } - const outcome = await getPRForBranchOutcome( - next.candidate.repoPath, - next.candidate.branch, - next.candidate.linkedPRNumber ?? null, - next.candidate.connectionId ?? null, - next.candidate.linkedPRNumber == null ? (next.candidate.fallbackPRNumber ?? null) : null, - ...hostedReviewOptionArgs(next.candidate, next.reason, { watchPRKey: next.watch?.prKey }) + if (next.hosted) { + next.hosted.running = true + } + const prepared = + next.hosted?.prepared ?? + (await preparePRBranchRead( + next.candidate.repoPath, + next.candidate.branch, + next.candidate.linkedPRNumber ?? null, + next.candidate.connectionId ?? null, + next.candidate.linkedPRNumber == null + ? (next.candidate.fallbackPRNumber ?? null) + : null, + ...hostedReviewOptionArgs(next.candidate, next.reason, { + watchPRKey: next.watch?.prKey + }) + )) + joinedRead ??= bypassesFreshnessDelay(next.reason) ? undefined : prepared.pending() + const admitted = admitPreparedPRRead( + { queue, pacing, events, scheduler, watchJobs }, + next, + prepared, + aliases, + requestSequence, + requestStartedAt, + Boolean(joinedRead) ) + if (!admitted) { + continue + } + const settle = this.scheduler.settleRead( + next.key, + next.candidate, + aliases, + prepared, + requestSequence, + Boolean(next.hosted) + ) + const result = await (joinedRead ?? prepared.readResult(settle, Boolean(next.hosted))) + const outcome = result ? deliverablePRBranchOutcome(prepared, result) : null + if (!outcome) { + this.scheduler.finishWithoutOutcome(next, aliases, requestSequence) + continue + } + if ( + next.hosted && + (!next.hosted.prepared.isCurrent() || !next.hosted.prepared.accepts(outcome)) + ) { + next.hosted.finish(null) + continue + } if (!this.queue.ownsRequest(next.key, requestSequence)) { + next.hosted?.finish(null) this.events.broadcast( { aliases, reason: next.reason, outcome, requestStartedAt }, requestSequence ) continue } - let plannedRetryAt: number | undefined - let broadcastOutcome = outcome - if (outcome.kind === 'upstream-error' && this.visibility.has(next.key)) { - plannedRetryAt = this.retry.nextVisibleErrorRetryAt( - next.key, - this.visibility.candidate(next.key, next.candidate) - ) - broadcastOutcome = this.retry.withErrorSchedule(outcome, plannedRetryAt) - } - this.events.observe(next.candidate, outcome, aliases) + const plannedRetryAt = this.retry.errorFollowUpAt( + next.key, + this.visibility.candidate(next.key, next.candidate), + outcome, + !next.hosted && this.visibility.has(next.key) + ) + const broadcastOutcome = + plannedRetryAt === undefined + ? outcome + : this.retry.withErrorSchedule(outcome, plannedRetryAt) this.retry.noteManualGate(next.key, broadcastOutcome) + if (next.hosted) { + if (outcome.kind !== 'upstream-error') { + this.retry.reset(next.key) + } + next.hosted.finish(broadcastOutcome) + continue + } this.events.broadcast( { aliases, reason: next.reason, outcome: broadcastOutcome, requestStartedAt }, requestSequence @@ -294,22 +305,4 @@ export class PRRefreshQueueDrainer { this.draining = false } } - - private notePacingDelay(entry: PRRefreshQueueEntry): void { - if (this.pacing.isActiveBurstDelayed(entry) && !entry.activeDelayNotified) { - entry.activeDelayNotified = true - this.events.broadcast({ - aliases: Array.from(entry.aliases.values()), - reason: entry.reason, - status: 'queued' - }) - } - if ( - entry.bypassBackgroundBudget !== true && - isBudgetedBackground(entry.reason) && - this.pacing.nextBudgetDelay() > 0 - ) { - this.events.record('background-pause', entry.reason) - } - } } diff --git a/src/main/github/pull-requests/refresh/pr-refresh-queue-growth-bound.test.ts b/src/main/github/pull-requests/refresh/pr-refresh-queue-growth-bound.test.ts index 980557b97bc..2a2323a7209 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-queue-growth-bound.test.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-queue-growth-bound.test.ts @@ -8,9 +8,7 @@ const { coordinatorMocks, moduleMocks } = await vi.hoisted(async () => { vi.mock('./pr-refresh-renderer-port', () => moduleMocks.rendererPortModuleMock(coordinatorMocks)) vi.mock('../../client/lookup/pr-for-branch-outcome', () => { - return { - getPRForBranchOutcome: coordinatorMocks.getPRForBranchOutcomeMock - } + return moduleMocks.clientModuleMock(coordinatorMocks) }) vi.mock('../../repositories/github-api-repository-remote-probe', () => { return { diff --git a/src/main/github/pull-requests/refresh/pr-refresh-queue-scheduler.ts b/src/main/github/pull-requests/refresh/pr-refresh-queue-scheduler.ts new file mode 100644 index 00000000000..2a2459cd804 --- /dev/null +++ b/src/main/github/pull-requests/refresh/pr-refresh-queue-scheduler.ts @@ -0,0 +1,298 @@ +import type { + GitHubPRRefreshAlias, + GitHubPRRefreshCandidate, + PRRefreshOutcome +} from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { + aliasFromCandidate, + freshRetryAt, + isBudgetedBackground, + isMergeabilityPendingOutcome, + sameAliasRequestIdentity, + visibleCandidateAfterOutcome, + visibleFollowUpPriority +} from './pr-refresh-candidate-policy' +import type { + PreparedPRBranchRead, + PRReadSettlement +} from '../../client/lookup/pr-for-branch-outcome' +import type { PRRefreshEventPublisher } from './pr-refresh-event-publisher' +import type { PRRefreshPacing } from './pr-refresh-pacing' +import type { PRRefreshQueue, PRRefreshQueueEntry } from './pr-refresh-queue' +import type { PRWatchJobRunner } from './pr-refresh-watch-jobs' +import type { PRRefreshRetryState } from './pr-refresh-retry-state' +import type { PRRefreshVisibility } from './pr-refresh-visibility' +import { outsideWslUserAct } from '../../../wsl/discovery/wsl-user-act-scope' + +export class PRRefreshQueueScheduler { + private timer: ReturnType | null = null + private timerDueAt = Infinity + + constructor( + private readonly queue: PRRefreshQueue, + private readonly pacing: PRRefreshPacing, + private readonly visibility: PRRefreshVisibility, + private readonly retry: PRRefreshRetryState, + private readonly events: PRRefreshEventPublisher, + private readonly watchJobs: PRWatchJobRunner, + private readonly runDrain: () => Promise + ) {} + + schedule(delay = 0): void { + const dueAt = performance.now() + Math.max(0, delay) + if (this.timer !== null && this.timerDueAt <= dueAt) { + return + } + clearTimeout(this.timer ?? undefined) + this.timerDueAt = dueAt + this.timer = setTimeout(() => { + this.timer = null + this.timerDueAt = Infinity + // The drain serves every queued repo; a scheduling call inside a WSL user act must not lend it that act. + void outsideWslUserAct(() => this.runDrain()) + }, delay) + } + + settleRead( + key: string, + candidate: GitHubPRRefreshCandidate, + aliases: GitHubPRRefreshAlias[], + prepared: PreparedPRBranchRead, + requestSequence: number, + hosted = false + ): PRReadSettlement { + return (outcome, producer) => { + if (!prepared.isCurrent()) { + return outcome + } + const settled = + producer.retained && + outcome.kind === 'upstream-error' && + (hosted || this.visibility.has(key)) + ? this.retry.settleProducerError( + outcome, + key, + hosted + ? { ...candidate, isSelected: false } + : this.visibility.candidate(key, candidate), + prepared, + this.queue, + this.queue.ownsRequest(key, requestSequence) + ) + : outcome + this.events.observe(candidate, settled, aliases) + return settled + } + } + + completeWithoutOutcome( + key: string, + candidate: GitHubPRRefreshCandidate, + aliases: GitHubPRRefreshAlias[], + priority: number, + windowId?: number, + foreground = false + ): void { + if (!this.visibility.has(key) || this.queue.get(key)) { + return + } + const current = this.visibility.candidate(key, candidate) + const dueAt = Math.max( + freshRetryAt(current, foreground) ?? Date.now(), + this.queue.backgroundGateUntil(key), + this.retry.manualGateUntil(key) + ) + if (!Number.isFinite(dueAt)) { + return + } + this.queue.setVisibleFollowUp({ + key, + candidate: current, + aliases: new Map(aliases.map((alias) => [alias.cacheKey, alias])), + reason: 'visible', + priority: visibleFollowUpPriority(current, priority), + dueAt, + queuedAt: this.queue.nextOrder(), + followUp: !foreground, + foreground, + windowId + }) + this.schedule(Math.max(0, dueAt - Date.now())) + } + + finishWithoutOutcome( + next: PRRefreshQueueEntry, + aliases: GitHubPRRefreshAlias[], + requestSequence: number + ): void { + next.hosted?.finish(null) + if (!next.hosted && this.queue.ownsRequest(next.key, requestSequence)) { + this.events.broadcast( + { aliases, reason: next.reason, status: 'skipped', skippedReason: 'fresh' }, + requestSequence + ) + if (next.watch) { + this.watchJobs.skipped(next) + } else { + this.completeWithoutOutcome( + next.key, + next.candidate, + aliases, + next.priority, + next.windowId, + next.foreground + ) + } + } + } + + consumeGatedError( + entry: PRRefreshQueueEntry, + prepared: PreparedPRBranchRead, + requestSequence: number, + requestStartedAt: number + ): boolean { + if ( + entry.hosted || + entry.watch || + entry.bypassBackgroundBudget || + (entry.reason !== 'visible' && entry.reason !== 'swr') || + !prepared.isCurrent() + ) { + return false + } + const outcome = prepared.cached()?.outcome + if (outcome?.kind !== 'upstream-error') { + return false + } + const retryAt = Math.max(outcome.nextAutoRetryAt ?? 0, outcome.retryDisabledUntil ?? 0) + if (retryAt <= Date.now()) { + return false + } + const aliases = Array.from(entry.aliases.values()) + this.retry.noteManualGate(entry.key, outcome) + this.events.broadcast( + { aliases, reason: entry.reason, outcome, requestStartedAt }, + requestSequence + ) + this.scheduleVisibleFollowUp( + entry.key, + entry.candidate, + outcome, + entry.priority, + aliases, + entry.windowId, + { plannedRetryAt: retryAt } + ) + return true + } + + scheduleVisibleFollowUp( + key: string, + candidate: GitHubPRRefreshCandidate, + outcome: PRRefreshOutcome, + priority: number, + aliases: GitHubPRRefreshAlias[], + windowId?: number, + options?: { pendingMergeabilityDelayMs?: number; plannedRetryAt?: number } + ): void { + if (outcome.kind === 'upstream-error') { + const retryAt = Math.max( + options?.plannedRetryAt ?? + this.retry.nextVisibleErrorRetryAt(key, this.visibility.candidate(key, candidate)), + this.retry.manualGateUntil(key) + ) + this.queue.protectBackgroundUntil(key, retryAt) + if (!this.visibility.has(key)) { + return + } + this.queue.setVisibleFollowUp({ + key, + candidate, + aliases: new Map(aliases.map((alias) => [alias.cacheKey, alias])), + reason: 'visible', + priority, + dueAt: retryAt, + queuedAt: this.queue.nextOrder(), + windowId + }) + this.schedule(retryAt - Date.now()) + return + } + this.retry.reset(key) + const refreshed = visibleCandidateAfterOutcome(candidate, outcome) + this.visibility.update(refreshed) + if (!this.visibility.has(key)) { + return + } + const followUpCandidate = this.visibility.candidate(key, refreshed) + const regularDueAt = freshRetryAt(followUpCandidate) ?? Date.now() + const pendingDueAt = + options?.pendingMergeabilityDelayMs !== undefined && isMergeabilityPendingOutcome(outcome) + ? outcome.fetchedAt + options.pendingMergeabilityDelayMs + : null + const dueAt = pendingDueAt === null ? regularDueAt : Math.min(regularDueAt, pendingDueAt) + if (!Number.isFinite(dueAt)) { + const pending = this.queue.get(key) + if ( + pending?.reason === 'visible' && + sameAliasRequestIdentity( + aliasFromCandidate(pending.candidate), + aliasFromCandidate(followUpCandidate) + ) + ) { + this.queue.delete(key) + } + return + } + this.queue.setVisibleFollowUp({ + key, + candidate: followUpCandidate, + aliases: new Map(aliases.map((alias) => [alias.cacheKey, alias])), + reason: 'visible', + priority: visibleFollowUpPriority(followUpCandidate, priority), + dueAt, + queuedAt: this.queue.nextOrder(), + bypassBackgroundBudget: pendingDueAt !== null, + followUp: true, + windowId + }) + this.schedule(Math.max(0, dueAt - Date.now())) + } + + ordered(): PRRefreshQueueEntry[] { + this.watchJobs.age(this.queue.values(), Date.now()) + return this.queue.ordered((a, b) => this.pacing.order(a, b)) + } + + nextQueuedWakeDelay(excludedKey: string): number | null { + const now = Date.now() + let nextDelay = Number.POSITIVE_INFINITY + for (const entry of this.queue.values()) { + if (entry.key === excludedKey) { + continue + } + const delay = entry.dueAt > now ? entry.dueAt - now : this.pacing.entryDelay(entry) + nextDelay = Math.min(nextDelay, delay) + } + return Number.isFinite(nextDelay) ? Math.max(0, nextDelay) : null + } + + notePacingDelay(entry: PRRefreshQueueEntry): void { + if (this.pacing.isActiveBurstDelayed(entry) && !entry.activeDelayNotified) { + entry.activeDelayNotified = true + this.events.broadcast({ + aliases: Array.from(entry.aliases.values()), + reason: entry.reason, + status: 'queued' + }) + } + if ( + entry.bypassBackgroundBudget !== true && + isBudgetedBackground(entry.reason) && + this.pacing.nextBudgetDelay() > 0 + ) { + this.events.record('background-pause', entry.reason) + } + } +} diff --git a/src/main/github/pull-requests/refresh/pr-refresh-queue.ts b/src/main/github/pull-requests/refresh/pr-refresh-queue.ts index 00caf6e68ae..77db82b084b 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-queue.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-queue.ts @@ -1,4 +1,5 @@ import { REVIEW_REFRESH_COOLDOWN_MS } from '../../../../shared/review-refresh-policy' +import type { HostedPRReadJob } from './hosted-pr-read-jobs' import type { GitHubPRRefreshAlias, GitHubPRRefreshCandidate, @@ -7,11 +8,11 @@ import type { import { aliasFromCandidate, bypassesFreshnessDelay, + foregroundExposureDueAt, freshRetryAt, POST_PUSH_DELAY_MS, refreshKey, sameAliasRequestIdentity, - refreshIntervalForCandidate, shouldSkipFresh, visibleFollowUpPriority } from './pr-refresh-candidate-policy' @@ -27,9 +28,11 @@ export type PRRefreshQueueEntry = { bypassBackgroundBudget?: boolean activeDelayNotified?: boolean followUp?: boolean + foreground?: boolean windowId?: number /** A watch job: the PR's current head, or with `headSha` one earlier head's checks. */ watch?: { prKey: string; headSha?: string } + hosted?: HostedPRReadJob } export type PRRefreshEnqueue = { @@ -108,6 +111,10 @@ export class PRRefreshQueue { return this.backgroundNotBefore.get(key)?.requestSequence === requestSequence } + backgroundGateUntil(key: string): number { + return this.backgroundNotBefore.get(key)?.until ?? 0 + } + retimeVisible( candidateFor: (key: string, candidate: GitHubPRRefreshCandidate) => GitHubPRRefreshCandidate ): void { @@ -138,14 +145,7 @@ export class PRRefreshQueue { const key = refreshKey(candidate) const existing = this.entries.get(key) const freshDueAt = shouldSkipFresh(candidate, reason) ? freshRetryAt(candidate) : null - const stopped = !Number.isFinite(refreshIntervalForCandidate(candidate)) - const exposureDueAt = - reexposed && - stopped && - candidate.cachedFetchedAt != null && - Date.now() - candidate.cachedFetchedAt >= REVIEW_REFRESH_COOLDOWN_MS - ? Date.now() - : null + const exposureDueAt = foregroundExposureDueAt(candidate, reexposed) const dueAt = Math.max( exposureDueAt ?? freshDueAt ?? Date.now() + (reason === 'post-push' ? POST_PUSH_DELAY_MS : 0), bypassesFreshnessDelay(reason) ? 0 : (this.backgroundNotBefore.get(key)?.until ?? 0) @@ -163,6 +163,7 @@ export class PRRefreshQueue { dueAt, queuedAt: this.nextOrder(), followUp: reason === 'visible' && freshDueAt !== null && exposureDueAt === null, + foreground: exposureDueAt !== null, windowId }) return { alias, key, dueAt, coalesced: false } @@ -171,11 +172,12 @@ export class PRRefreshQueue { setLiveAlias(existing.aliases, alias) if ( reason === 'visible' && - !shouldSkipFresh(candidate, reason) && + (exposureDueAt !== null || !shouldSkipFresh(candidate, reason)) && existing.reason === 'visible' ) { existing.dueAt = Math.min(existing.dueAt, dueAt) existing.followUp = false + existing.foreground ||= exposureDueAt !== null } const shouldPromote = priority > existing.priority || diff --git a/src/main/github/pull-requests/refresh/pr-refresh-quiet-policy.perf.test.ts b/src/main/github/pull-requests/refresh/pr-refresh-quiet-policy.perf.test.ts new file mode 100644 index 00000000000..4aa8e2d8c4e --- /dev/null +++ b/src/main/github/pull-requests/refresh/pr-refresh-quiet-policy.perf.test.ts @@ -0,0 +1,111 @@ +import { afterEach, expect, it, vi } from 'vitest' +import { makeCandidate, makePR } from './pr-refresh-coordinator-test-harness' + +vi.mock('../../client/lookup/branch-lookup-resolution', () => ({ + resolvePRForBranchOutcome: async () => ({ + kind: 'found', + fetchedAt: Date.now(), + pr: makePR({ + headSha: 'head', + checksStatus: 'success', + checksReadComplete: true, + checksFinal: true, + autoMergeEnabled: false, + mergeQueueRequired: false + }) + }) +})) +vi.mock('../../execution/github-operation-slots', () => ({ + acquire: async () => {}, + release: () => {} +})) + +let release: (() => void) | undefined +afterEach(() => { + release?.() + vi.useRealTimers() + vi.restoreAllMocks() +}) + +it.each([750, 7500])( + 'selects owned quiet policy without inventory discovery at %i workspaces', + async (population) => { + vi.resetModules() + vi.useFakeTimers() + vi.setSystemTime(1_000) + const accounts = await import('../../accounts/github-project-account') + const execution = await import('../../../source-control/code-host-read-execution-scope') + const repository = { + id: 'repo', + path: '/repo', + displayName: 'Repo', + badgeColor: 'blue', + addedAt: 0, + ghAccount: { host: 'github.com', user: 'first' } + } + const inventory = vi.fn(() => + Array.from({ length: population }, (_, row) => ({ repoId: 'repo', path: `/repo/wt-${row}` })) + ) + const metadata = vi.fn(() => ({})) + release = accounts.installGitHubProjectAccounts( + { getRepos: () => [repository], getAllWorktreeMetaForHost: metadata }, + inventory + ) + const discovery = vi.spyOn(accounts, 'resolveGitHubProjectAccount') + const hashing = vi.spyOn(execution, 'codeHostReadExecutionScope') + const { getPRForBranchOutcome } = await import('../../client/lookup/pr-for-branch-outcome') + const { runHostedPRRead, reportVisiblePRRefreshCandidates } = + await import('./pr-refresh-coordinator') + const { refreshIntervalForCandidate } = await import('./pr-refresh-candidate-policy') + const { refreshPRPolicyContext } = await import('../../client/lookup/pr-quiet-policy-evidence') + const candidate = makeCandidate({ + repoId: 'repo', + repoPath: '/repo/wt-0', + branch: 'branch', + currentHeadOid: 'head', + cachedHeadOid: 'head', + cachedFetchedAt: 2_000, + cachedPRState: 'open', + cachedChecksStatus: 'success', + cachedHasPR: true + }) + const read = () => + runHostedPRRead(candidate, () => + getPRForBranchOutcome('/repo/wt-0', 'branch', null, null, null, { currentHeadOid: 'head' }) + ) + await read() + vi.setSystemTime(2_000) + await read() + expect(refreshIntervalForCandidate(candidate)).toBe(300_000) + discovery.mockClear() + hashing.mockClear() + inventory.mockClear() + metadata.mockClear() + reportVisiblePRRefreshCandidates([candidate], 1, 1) + for (let row = 0; row < population; row++) { + refreshIntervalForCandidate(candidate) + } + expect(discovery).not.toHaveBeenCalled() + expect(hashing).not.toHaveBeenCalled() + expect(inventory).not.toHaveBeenCalled() + expect(metadata).not.toHaveBeenCalled() + expect(refreshIntervalForCandidate({ ...candidate, fallbackPRNumber: 9 })).toBe(120_000) + const { bumpScopeGeneration, hostedReviewRepoScope } = + await import('../../../source-control/hosted-review-scope-generations') + bumpScopeGeneration(hostedReviewRepoScope(candidate.repoPath, 'local')) + expect(refreshIntervalForCandidate(candidate)).toBe(120_000) + vi.setSystemTime(3_000) + await read() + vi.setSystemTime(4_000) + await read() + expect(refreshIntervalForCandidate({ ...candidate, cachedFetchedAt: 4_000 })).toBe(300_000) + repository.ghAccount = { host: 'github.com', user: 'second' } + refreshPRPolicyContext() + expect(refreshIntervalForCandidate({ ...candidate, cachedFetchedAt: 4_000 })).toBe(120_000) + vi.setSystemTime(5_000) + await read() + vi.setSystemTime(6_000) + await read() + expect(refreshIntervalForCandidate({ ...candidate, cachedFetchedAt: 6_000 })).toBe(300_000) + } +) diff --git a/src/main/github/pull-requests/refresh/pr-refresh-read-admission.ts b/src/main/github/pull-requests/refresh/pr-refresh-read-admission.ts new file mode 100644 index 00000000000..d6a0f23af2a --- /dev/null +++ b/src/main/github/pull-requests/refresh/pr-refresh-read-admission.ts @@ -0,0 +1,64 @@ +import type { GitHubPRRefreshAlias } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import type { PreparedPRBranchRead } from '../../client/lookup/pr-for-branch-outcome' +import { isBackground, isBudgetedBackground } from './pr-refresh-candidate-policy' +import type { PRRefreshEventPublisher } from './pr-refresh-event-publisher' +import { type PRRefreshPacing, usesActiveRefreshPacing } from './pr-refresh-pacing' +import type { PRRefreshQueue, PRRefreshQueueEntry } from './pr-refresh-queue' +import type { PRRefreshQueueScheduler } from './pr-refresh-queue-scheduler' +import type { PRWatchJobRunner } from './pr-refresh-watch-jobs' + +type PRReadAdmissionOwners = { + queue: PRRefreshQueue + pacing: PRRefreshPacing + events: PRRefreshEventPublisher + scheduler: PRRefreshQueueScheduler + watchJobs: PRWatchJobRunner +} + +export function admitPreparedPRRead( + { queue, pacing, events, scheduler, watchJobs }: PRReadAdmissionOwners, + next: PRRefreshQueueEntry, + prepared: PreparedPRBranchRead, + aliases: GitHubPRRefreshAlias[], + requestSequence: number, + requestStartedAt: number, + joinedRead: boolean +): boolean { + if (!queue.ownsRequest(next.key, requestSequence) || !prepared.isCurrent()) { + next.hosted?.finish(null) + prepared.release() + if (!next.hosted && queue.ownsRequest(next.key, requestSequence)) { + events.broadcast( + { aliases, reason: next.reason, status: 'skipped', skippedReason: 'fresh' }, + requestSequence + ) + if (next.watch) { + watchJobs.skipped(next) + } else { + scheduler.completeWithoutOutcome( + next.key, + next.candidate, + aliases, + next.priority, + next.windowId, + next.foreground + ) + } + } + return false + } + if (scheduler.consumeGatedError(next, prepared, requestSequence, requestStartedAt)) { + prepared.release() + return false + } + if (!next.watch && isBackground(next.reason) && !joinedRead) { + if (next.bypassBackgroundBudget !== true && isBudgetedBackground(next.reason)) { + pacing.noteBackgroundStart(next) + watchJobs.noteBudgetedStart(next) + } + if (usesActiveRefreshPacing(next)) { + pacing.noteActiveStart(next) + } + } + return true +} diff --git a/src/main/github/pull-requests/refresh/pr-refresh-retry-state.ts b/src/main/github/pull-requests/refresh/pr-refresh-retry-state.ts index 13a29365156..3423968a04c 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-retry-state.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-retry-state.ts @@ -4,6 +4,8 @@ import type { } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' import { lookupBackoffDelayMs } from '../../../source-control/hosted-review-refresh-pacing' import { refreshIntervalForCandidate } from './pr-refresh-candidate-policy' +import type { PreparedPRBranchRead } from '../../client/lookup/pr-for-branch-outcome' +import type { PRRefreshQueue } from './pr-refresh-queue' export class PRRefreshRetryState { private readonly errorBackoff = new Map() @@ -50,4 +52,36 @@ export class PRRefreshRetryState { nextAutoRetryAt: cooldownUntil !== undefined ? Math.max(retryAt, cooldownUntil) : retryAt } } + + errorFollowUpAt( + key: string, + candidate: GitHubPRRefreshCandidate, + outcome: PRRefreshOutcome, + visible: boolean + ): number | undefined { + return outcome.kind === 'upstream-error' + ? (outcome.nextAutoRetryAt ?? + (visible ? this.nextVisibleErrorRetryAt(key, candidate) : undefined)) + : undefined + } + + settleProducerError( + outcome: PRRefreshOutcome, + key: string, + candidate: GitHubPRRefreshCandidate, + prepared: PreparedPRBranchRead, + queue: PRRefreshQueue, + ownsRequest: boolean + ): PRRefreshOutcome { + const retryAt = this.nextVisibleErrorRetryAt(key, candidate) + const result = this.withErrorSchedule(outcome, retryAt) + prepared.scheduleError(outcome, result) + if (ownsRequest) { + queue.protectBackgroundUntil( + key, + Math.max(retryAt, outcome.kind === 'upstream-error' ? (outcome.retryDisabledUntil ?? 0) : 0) + ) + } + return result + } } diff --git a/src/main/github/pull-requests/refresh/pr-refresh-watch-jobs.ts b/src/main/github/pull-requests/refresh/pr-refresh-watch-jobs.ts index 9551d0e28a0..66d6737f246 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-watch-jobs.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-watch-jobs.ts @@ -2,9 +2,11 @@ import type { GitHubPRRefreshCandidate, PRRefreshOutcome } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' -import type { PRInfo } from '../../../../shared/github/pull-requests/pull-request-types' +import { prChecksDone } from '../../../../shared/github/pull-requests/refresh/pr-quiet-refresh' +export { prChecksDone } from '../../../../shared/github/pull-requests/refresh/pr-quiet-refresh' import { reviewRefreshIntervalMs } from '../../../../shared/review-refresh-policy' import type { PRRefreshQueue, PRRefreshQueueEntry } from './pr-refresh-queue' +import { isSelectedBudgetedPRRefresh } from './pr-refresh-pacing' import type { PRRefreshRetryState } from './pr-refresh-retry-state' export type PRWatchJob = NonNullable @@ -33,14 +35,6 @@ export function prWatchQueueKey(job: PRWatchJob): string { return job.headSha ? `sha-checks::${job.prKey}::${job.headSha}` : `watch::${job.prKey}` } -/** A complete read with nothing left running, or a complete read with no checks at all. A failed - * check beside a running one is not done, whatever the sidebar's failure-first summary says. */ -export function prChecksDone(pr: PRInfo): boolean { - return ( - pr.checksFinal === true || (pr.checksReadComplete === true && pr.checksSummary?.total === 0) - ) -} - /** A watch's next read: the background cadence, or none once the PR has settled. */ export function prWatchNextDueAt(outcome: PRRefreshOutcome): number | null { if (outcome.kind === 'upstream-error') { @@ -104,7 +98,7 @@ export class PRWatchJobRunner { noteBudgetedStart(entry: PRRefreshQueueEntry): void { // Selected list reads have their own reserved turn; preserve the watch/list alternation. - if (entry.reason !== 'visible' || entry.candidate.isSelected !== true) { + if (!isSelectedBudgetedPRRefresh(entry)) { this.lastBudgetedStartWasWatch = entry.watch !== undefined } } diff --git a/src/main/github/repositories/github-repository-identity.ts b/src/main/github/repositories/github-repository-identity.ts index 360bdd7d285..0e2f1c7640d 100644 --- a/src/main/github/repositories/github-repository-identity.ts +++ b/src/main/github/repositories/github-repository-identity.ts @@ -61,6 +61,15 @@ export async function withGitHubRepoExecution( return operations.run(context, operation) } +export function captureGitHubRepoExecution(): (operation: () => Promise) => Promise { + const context = operations.getStore() + return (operation) => (context ? operations.run(context, operation) : operation()) +} + +export function outsideGitHubRepoExecution(operation: () => T): T { + return operations.exit(operation) +} + export function githubRepoContext( repoPath: string, connectionId?: string | null, diff --git a/src/main/index.ts b/src/main/index.ts index fe9cc193c41..4a01437034b 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -51,20 +51,23 @@ import { registerOrcaProtocolClient } from './startup/open-routing/orca-protocol import { clearWindowsStdHandleInheritance } from './startup/windows-startup/windows-std-handle-inheritance' import { installWindowsIsolatedSpawner } from './startup/windows-startup/windows-isolated-spawner' import { dropE2eSpawnLogOutsideE2e } from './startup/testing/e2e-spawn-log-env' +import type { MainWindowOpenOptions } from './window/main-window/main-window-contracts' +import type { DesktopActivationRequest } from './startup/serve-launch/serve-desktop-activation' +import { argvRequestsBackgroundOpen, BACKGROUND_OPEN_ARG } from '../shared/background-open-argv' +import { setBackgroundWindowOpener } from './window/activation/background-window-open' // Before anything spawns, so no daemon, PTY or agent inherits it outside e2e. dropE2eSpawnLogOutsideE2e() -async function openMainWindow( - options: { revealOnDidFinishLoad?: boolean } = {} -): Promise { +async function openMainWindow(options: MainWindowOpenOptions = {}): Promise { return await openMainWindowController(options) } setMainWindowOpener(openMainWindow) +setBackgroundWindowOpener(() => requestDesktopActivation([BACKGROUND_OPEN_ARG])) -function focusExistingWindow(): void { - focusExistingWindowAction() +function focusExistingWindow(request: DesktopActivationRequest = {}): void { + focusExistingWindowAction(request) } const requestDesktopHandoff = createDesktopHandoff({ @@ -88,7 +91,9 @@ function requestDesktopActivation(argv: readonly string[] = []): void { requestDesktopHandoff() return } - state.desktopActivationGate?.requestActivation() + state.desktopActivationGate?.requestActivation({ + background: argvRequestsBackgroundOpen(argv) + }) } /** Buffer documents until listener readiness; webContents.send silently drops unhandled messages. */ diff --git a/src/main/ipc/computer-use-permissions.ts b/src/main/ipc/computer-use-permissions.ts index 1fb4a4ec047..73edce4f5cd 100644 --- a/src/main/ipc/computer-use-permissions.ts +++ b/src/main/ipc/computer-use-permissions.ts @@ -17,7 +17,9 @@ export function registerComputerUsePermissionHandlers(): void { ): Promise => { const { openComputerUsePermissions } = await import('../computer/macos-permissions/macos-computer-use-permissions') - return openComputerUsePermissions(args?.id, mainI18n.resolvedLanguage ?? mainI18n.language) + return openComputerUsePermissions(args?.id, { + locale: mainI18n.resolvedLanguage ?? mainI18n.language + }) } ) ipcMain.handle( diff --git a/src/main/ipc/filesystem/access/chat-image-target.ts b/src/main/ipc/filesystem/access/chat-image-target.ts new file mode 100644 index 00000000000..de4d8611e2a --- /dev/null +++ b/src/main/ipc/filesystem/access/chat-image-target.ts @@ -0,0 +1,27 @@ +import { stat } from 'node:fs/promises' +import { + filePreviewExtension, + filePreviewForPath +} from '../../../../shared/file-preview/file-preview-kind' +import { openLocalRegularFile, readImageFileSignature } from '../local-regular-file-read' +import { windowsFsPath } from '../../../wsl/filesystem/wsl-unc-drive-fs-path' + +/** Chat draws images only (PDFs need another surface); Telegram's `.jpg` links lead to extensionless files, so those count by their bytes. */ +export async function isChatImageTarget(filePath: string): Promise { + if (filePreviewForPath(filePath)?.kind === 'image') { + return true + } + if (filePreviewExtension(filePath) !== '') { + return false + } + // Devices are extensionless, and opening one (a serial port) can act on it. + if (!(await stat(windowsFsPath(filePath))).isFile()) { + return false + } + const { handle } = await openLocalRegularFile(filePath) + try { + return (await readImageFileSignature(handle)) !== null + } finally { + await handle.close() + } +} diff --git a/src/main/ipc/filesystem/access/local-file-access-resolution.test.ts b/src/main/ipc/filesystem/access/local-file-access-resolution.test.ts index e67ddd17d71..e992d62e66d 100644 --- a/src/main/ipc/filesystem/access/local-file-access-resolution.test.ts +++ b/src/main/ipc/filesystem/access/local-file-access-resolution.test.ts @@ -222,6 +222,33 @@ describe('chat-image requests', () => { ).toBe('denied') } ) + + it.skipIf(process.platform === 'win32')( + 'show an image-named link to an extensionless file only when its bytes are an image', + async () => { + const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 0]) + await writeFile(join(outside, 'photo'), png) + await writeFile(join(outside, 'notes'), 'outside notes\n') + await symlink(join(outside, 'photo'), join(outside, 'photo.jpg')) + await symlink(join(outside, 'notes'), join(outside, 'notes.jpg')) + const store = makeStore({}) + + const imagePath = await resolveLocalFileRequestPath( + join(outside, 'photo.jpg'), + CHAT_IMAGE, + store + ) + await expect(readLocalFileContent(imagePath, { imageBySignature: true })).resolves.toEqual({ + content: png.toString('base64'), + isBinary: true, + isImage: true, + mimeType: 'image/png' + }) + expect( + await settles(resolveLocalFileRequestPath(join(outside, 'notes.jpg'), CHAT_IMAGE, store)) + ).toBe('denied') + } + ) }) describe('project-folder-link listings', () => { diff --git a/src/main/ipc/filesystem/access/local-file-access-resolution.ts b/src/main/ipc/filesystem/access/local-file-access-resolution.ts index 2075c603f64..0ec5ab15227 100644 --- a/src/main/ipc/filesystem/access/local-file-access-resolution.ts +++ b/src/main/ipc/filesystem/access/local-file-access-resolution.ts @@ -8,7 +8,6 @@ import { type ResolveAuthorizedPathOptions } from './filesystem-auth' import { isDescendantOrEqual } from './filesystem-path-containment' -import { filePreviewForPath } from '../../../../shared/file-preview/file-preview-kind' import { getFloatingWorkspaceFileRoots } from '../../floating-workspace-directory' import { isDeviceNamespacePath, @@ -16,6 +15,7 @@ import { isWindowsReservedDeviceName } from '../../automatic-load-path-text' import { NOT_A_REGULAR_FILE_MESSAGE } from '../local-regular-file-read' +import { isChatImageTarget } from './chat-image-target' import { realpathAcrossWslDriveMount, windowsFsPath @@ -60,11 +60,6 @@ export async function resolveUserNamedRegularFile( return filePath } -// Chat uses img rendering; PDF previews need a different surface. -function isChatImage(filePath: string): boolean { - return filePreviewForPath(filePath)?.kind === 'image' -} - // Resolve path text first because devices can hide behind dot and parent segments. function isRefusedAutomaticLoadPath(filePath: string): boolean { return isDeviceNamespacePath(filePath) || isWindowsReservedDeviceName(filePath) @@ -124,7 +119,7 @@ async function resolveChatImagePath(targetPath: string, store: RuntimeStore): Pr await resolveDesktopAuthorizedPath(realTarget, store) } // A .png symlink to a secret is not an image. - if (!isChatImage(realTarget)) { + if (!(await isChatImageTarget(realTarget))) { throw new Error(CHAT_IMAGE_TYPE_MESSAGE) } return realTarget diff --git a/src/main/ipc/filesystem/filesystem-file-content-inspection.ts b/src/main/ipc/filesystem/filesystem-file-content-inspection.ts index 6a4634255f4..5edc756e1bf 100644 --- a/src/main/ipc/filesystem/filesystem-file-content-inspection.ts +++ b/src/main/ipc/filesystem/filesystem-file-content-inspection.ts @@ -7,6 +7,7 @@ import { BINARY_PROBE_BYTES, isBinaryBuffer } from '../../../shared/file-preview import { fileTooLargeError, openLocalRegularFile, + readImageFileSignature, readLocalFileBounded, readLocalFilePrefix } from './local-regular-file-read' @@ -27,10 +28,18 @@ export type LocalFileContent = { } /** One open, one handle: the size check, binary probe and read all see the same regular file. */ -export async function readLocalFileContent(filePath: string): Promise { +export async function readLocalFileContent( + filePath: string, + options: { imageBySignature?: boolean } = {} +): Promise { const { handle, stats } = await openLocalRegularFile(filePath) try { - const mimeType = IMAGE_AND_PDF_PREVIEW_MIME_TYPES[filePreviewExtension(filePath)] + const extension = filePreviewExtension(filePath) + const mimeType = + IMAGE_AND_PDF_PREVIEW_MIME_TYPES[extension] ?? + (options.imageBySignature && extension === '' + ? (await readImageFileSignature(handle))?.mimeType + : undefined) const sizeLimit = mimeType ? MAX_PREVIEWABLE_BINARY_SIZE : MAX_TEXT_FILE_SIZE if (stats.size > sizeLimit) { throw fileTooLargeError(stats.size, sizeLimit) diff --git a/src/main/ipc/filesystem/filesystem-read-handlers.ts b/src/main/ipc/filesystem/filesystem-read-handlers.ts index b6d7dcbbb46..82117184903 100644 --- a/src/main/ipc/filesystem/filesystem-read-handlers.ts +++ b/src/main/ipc/filesystem/filesystem-read-handlers.ts @@ -125,7 +125,9 @@ export function registerFilesystemReadHandlers(context: FilesystemHandlerContext } return args.includeLocalLogMetadata === true ? readLocalLogSnapshot(filePath) - : readLocalFileContent(filePath) + : readLocalFileContent(filePath, { + imageBySignature: args.access?.kind === 'chat-image' + }) }, { wake: args.wakeWslDistro === true } ) diff --git a/src/main/ipc/filesystem/filesystem-runtime-upload-staging.ts b/src/main/ipc/filesystem/filesystem-runtime-upload-staging.ts index f701b84607e..81642bfcbc0 100644 --- a/src/main/ipc/filesystem/filesystem-runtime-upload-staging.ts +++ b/src/main/ipc/filesystem/filesystem-runtime-upload-staging.ts @@ -4,13 +4,10 @@ import { REMOTE_IMPORT_MAX_TOTAL_BYTES } from '../runtime-import-limits' import { constants } from 'node:fs' -import { lstat, open, readdir, realpath, type FileHandle } from 'node:fs/promises' +import { lstat, open, readdir, realpath } from 'node:fs/promises' import { basename, extname, isAbsolute, join, relative, resolve, sep } from 'node:path' import { classifyImportSource } from './filesystem-import-source-stat' -import { - detectImageFileSignature, - IMAGE_SIGNATURE_BYTES -} from '../../../shared/image-file-signature' +import { readImageFileSignature } from './local-regular-file-read' import type { StagedRuntimeUploadEntry as StagedExternalImportEntry, StagedRuntimeUploadSource as StagedExternalImportSource @@ -180,7 +177,7 @@ async function stageFileEntry( }, byteLength: openedStat.size, imageExtension: options.detectImageExtension - ? await readImageExtension(fileHandle) + ? (await readImageFileSignature(fileHandle))?.extension : undefined } } finally { @@ -188,12 +185,6 @@ async function stageFileEntry( } } -async function readImageExtension(fileHandle: FileHandle): Promise { - const header = Buffer.alloc(IMAGE_SIGNATURE_BYTES) - const { bytesRead } = await fileHandle.read(header, 0, IMAGE_SIGNATURE_BYTES, 0) - return detectImageFileSignature(header.subarray(0, bytesRead))?.extension -} - async function assertRealPathInsideRoot( rootRealPath: string, candidatePath: string, diff --git a/src/main/ipc/filesystem/local-regular-file-read.ts b/src/main/ipc/filesystem/local-regular-file-read.ts index 7ff19f48e85..d3ef34bf04f 100644 --- a/src/main/ipc/filesystem/local-regular-file-read.ts +++ b/src/main/ipc/filesystem/local-regular-file-read.ts @@ -2,6 +2,11 @@ import { constants, type Stats } from 'node:fs' import { open, type FileHandle } from 'node:fs/promises' import { isENOENT } from './access/filesystem-path-containment' import { windowsFsPath } from '../../wsl/filesystem/wsl-unc-drive-fs-path' +import { + IMAGE_SIGNATURE_BYTES, + detectImageFileSignature, + type ImageFileSignature +} from '../../../shared/image-file-signature' export const NOT_A_REGULAR_FILE_MESSAGE = 'Not a regular file' @@ -66,6 +71,12 @@ export async function readLocalFilePrefix(handle: FileHandle, bytes: number): Pr return probe.subarray(0, bytesRead) } +export async function readImageFileSignature( + handle: FileHandle +): Promise { + return detectImageFileSignature(await readLocalFilePrefix(handle, IMAGE_SIGNATURE_BYTES)) +} + export async function assertLocalWriteTargetIsRegularFile(filePath: string): Promise { let handle: FileHandle try { diff --git a/src/main/ipc/preflight.ts b/src/main/ipc/preflight.ts index 5df5385a9c9..79d92918e35 100644 --- a/src/main/ipc/preflight.ts +++ b/src/main/ipc/preflight.ts @@ -78,11 +78,11 @@ export function registerPreflightHandlers(store?: Pick): v ipcMain.handle( 'preflight:refreshAgents', - async (_event, args?: AgentDetectionRequest & PreflightWake) => + async (_event, args?: AgentDetectionRequest & PreflightWake & { userInitiated?: boolean }) => runPreflightAct(args, async () => { const host: AgentDetectionHost = { kind: 'local', context: args } await assertAgentDetectionHostRunning(host) - return refreshAgentsOnHost(host, detectionOptions(args)) + return refreshAgentsOnHost(host, detectionOptions(args), args?.userInitiated) }) ) diff --git a/src/main/ipc/register-core-handlers/core-handler-lifecycle.ts b/src/main/ipc/register-core-handlers/core-handler-lifecycle.ts new file mode 100644 index 00000000000..fdb0d8ad94f --- /dev/null +++ b/src/main/ipc/register-core-handlers/core-handler-lifecycle.ts @@ -0,0 +1,33 @@ +import type { + AiVaultPrepareSessionResumeArgs, + AiVaultPrepareSessionResumeResult +} from '../../../shared/ai-vault/resume/ai-vault-resume-preparation' +import type { OrcaRuntimeService } from '../../runtime/orca-runtime' +import { settleWithinMs } from '../../startup/quit/quit-teardown-deadline' + +const RELAUNCH_DISMISSAL_CLOSE_TIMEOUT_MS = 2_000 + +export async function closeNotificationDismissalsBeforeRelaunch( + runtime: OrcaRuntimeService +): Promise { + const result = await settleWithinMs( + runtime.closeNotificationDismissals(), + RELAUNCH_DISMISSAL_CLOSE_TIMEOUT_MS + ) + if (result.outcome === 'timed-out') { + console.warn('[notifications] Relaunching before dismissal recovery state finished saving') + } +} + +export type CoreHandlerLifecycleOptions = { + deferHostInitialization?: boolean + hostInitialization?: Promise + onBeforeRelaunch?: () => void | Promise + onOrcaProfileAuthMutation?: () => void + onBeforeOrcaProfileSignOut?: () => void + onAfterOrcaProfileSignOut?: () => void + getAdditionalAiVaultCodexHomePaths?: () => readonly string[] + prepareAiVaultSessionResume?: ( + args: AiVaultPrepareSessionResumeArgs + ) => Promise +} diff --git a/src/main/ipc/register-core-handlers/register-core-handlers.ts b/src/main/ipc/register-core-handlers/register-core-handlers.ts index 05e18dec4b7..d272174030c 100644 --- a/src/main/ipc/register-core-handlers/register-core-handlers.ts +++ b/src/main/ipc/register-core-handlers/register-core-handlers.ts @@ -1,4 +1,3 @@ -import { closeNotificationDismissalsBeforeRelaunch } from './relaunch-notification-dismissals' import { app } from 'electron' import { ORCA_LAYOUT_OWNER_IS_MAIN } from '../../../shared/layout-owner-switch' import { registerAppHandlers } from '../app' @@ -86,10 +85,6 @@ import type { AutomationService } from '../../automations/automation-service' import type { AgentAwakeService } from '../../power/agent-awake-service' import type { CrashReportStore } from '../../crash-reporting/reports/crash-report-store' import type { KeybindingService } from '../../keybindings/keybinding-service' -import type { - AiVaultPrepareSessionResumeArgs, - AiVaultPrepareSessionResumeResult -} from '../../../shared/ai-vault/resume/ai-vault-resume-preparation' import { getSavedRuntimeAiVaultHostInfos, deleteRuntimeAiVaultSession, @@ -103,24 +98,14 @@ import { ensureStructuredAgentSessionHostUnlessRefused } from '../../runtime/age import { getAgentSessionAttachmentStore } from '../../native-chat/agent-session-attachments/agent-session-attachment-store-registry' import type { PluginService } from '../../plugins/plugin-service' import type { PluginMarketplaceHandlerServices } from '../plugin-marketplaces' - import { getRuntimeEnvironmentRegistryRoot } from '../runtime-environments/catalog/runtime-environment-registry-root' +import { + closeNotificationDismissalsBeforeRelaunch, + type CoreHandlerLifecycleOptions +} from './core-handler-lifecycle' let registration: Promise | null = null -type CoreHandlerLifecycleOptions = { - deferHostInitialization?: boolean - hostInitialization?: Promise - onBeforeRelaunch?: () => void | Promise - onOrcaProfileAuthMutation?: () => void - onBeforeOrcaProfileSignOut?: () => void - onAfterOrcaProfileSignOut?: () => void - getAdditionalAiVaultCodexHomePaths?: () => readonly string[] - prepareAiVaultSessionResume?: ( - args: AiVaultPrepareSessionResumeArgs - ) => Promise -} - export async function registerCoreHandlers( store: Store, runtime: OrcaRuntimeService, diff --git a/src/main/ipc/register-core-handlers/relaunch-notification-dismissals.ts b/src/main/ipc/register-core-handlers/relaunch-notification-dismissals.ts deleted file mode 100644 index 735465b4f84..00000000000 --- a/src/main/ipc/register-core-handlers/relaunch-notification-dismissals.ts +++ /dev/null @@ -1,17 +0,0 @@ -import type { OrcaRuntimeService } from '../../runtime/orca-runtime' -import { settleWithinMs } from '../../startup/quit/quit-teardown-deadline' - -// Same cap as the restart path's auth preservation: bookkeeping must not hold Restart on a stalled disk. -export const RELAUNCH_DISMISSAL_CLOSE_TIMEOUT_MS = 2_000 - -export async function closeNotificationDismissalsBeforeRelaunch( - runtime: OrcaRuntimeService -): Promise { - const result = await settleWithinMs( - runtime.closeNotificationDismissals(), - RELAUNCH_DISMISSAL_CLOSE_TIMEOUT_MS - ) - if (result.outcome === 'timed-out') { - console.warn('[notifications] Relaunching before dismissal recovery state finished saving') - } -} diff --git a/src/main/ipc/runtime-environments/requests/runtime-environment-call-queue.ts b/src/main/ipc/runtime-environments/requests/runtime-environment-call-queue.ts index e8cb219318e..c75ac4540f9 100644 --- a/src/main/ipc/runtime-environments/requests/runtime-environment-call-queue.ts +++ b/src/main/ipc/runtime-environments/requests/runtime-environment-call-queue.ts @@ -1,12 +1,13 @@ import { RuntimeRpcCallQueuePool } from '../../../../shared/runtime-rpc-call-queue' -const runtimeCallQueuePool = new RuntimeRpcCallQueuePool() +export const runtimeCallQueuePool = new RuntimeRpcCallQueuePool() export function enqueueRuntimeCall( selector: string, method: string, run: () => Promise, - signal?: AbortSignal + signal?: AbortSignal, + params?: unknown ): Promise { - return runtimeCallQueuePool.enqueue(selector, method, run, 0, signal) + return runtimeCallQueuePool.enqueue(selector, method, run, 0, signal, params) } diff --git a/src/main/ipc/runtime-environments/requests/runtime-environment-subscription-routing.ts b/src/main/ipc/runtime-environments/requests/runtime-environment-subscription-routing.ts new file mode 100644 index 00000000000..b043baca020 --- /dev/null +++ b/src/main/ipc/runtime-environments/requests/runtime-environment-subscription-routing.ts @@ -0,0 +1,184 @@ +import { RemoteRuntimeClientError } from '../../../../shared/remote-runtime/remote-runtime-client-error' +import { throwIfSignalAborted } from '../../../../shared/abort-signal-reason' +import { + type RuntimeEnvironmentSetupFailure, + runtimeEnvironmentSetupFailure +} from '../../../../shared/remote-runtime/host-catalog/runtime-environment-subscription-setup' +import { assertCrewSourceCurrent } from '../subscriptions/runtime-environment-subscription-setup' +import { getPreferredPairedRuntimeConnection } from '../../../../shared/remote-runtime/host-catalog/runtime-environments' +import { ELECTRON_REMOTE_RUNTIME_CLIENT_CAPABILITIES } from '../../../../shared/electron-remote-runtime-client-capabilities' +import { resolveEnvironment } from '../../../../shared/remote-runtime/host-store/runtime-environment-store' +import { + resolveManagedRuntimeEnvironment, + type ManagedRuntimeCallPreparation +} from '../managed-orcad/runtime-environment-managed-tunnel' +import { recordRuntimeEnvironmentUsage } from '../../../../shared/remote-runtime/host-store/runtime-environment-usage-record' +import type * as Rpc from '../../../../shared/runtime-rpc-envelope' +import { subscribeRemoteRuntimeTransport } from '../../../../shared/remote-runtime/requests/remote-runtime-subscription-transport' +import type { RemoteRuntimeTransportSubscription } from '../../../../shared/remote-runtime/requests/remote-runtime-subscription-contract' +import { withRuntimeUnreachableHint } from './runtime-environment-unreachable-hint' +import { decodeRuntimeEnvironmentSubscriptionEvents } from '../subscriptions/runtime-environment-subscription-events' +import { runtimeCallQueuePool } from './runtime-environment-call-queue' +import { isTimedPRRead } from '../../../../shared/runtime-rpc-call-queue' +import { enqueueRuntimeSubscription } from '../../../../shared/runtime-rpc-subscription-queue' +import { + shouldRouteSubscriptionBySupport, + subscribeSupportRoutedRuntimeEnvironment +} from './runtime-environment-support-routing' + +const DEFAULT_REMOTE_RUNTIME_TIMEOUT_MS = 15_000 + +export async function subscribeRuntimeEnvironment( + userDataPath: string, + selector: string, + method: string, + params: unknown, + timeoutMs: number | undefined, + rawCallbacks: { + onEvent: ( + payload: + | { type: 'response'; response: Rpc.RuntimeRpcResponse } + | { type: 'binary'; bytes: Uint8Array } + | { type: 'error'; code: string; message: string } + | { type: 'close' } + ) => void + onClose: () => void + }, + isCurrent: () => boolean = () => true, + signal?: AbortSignal, + preparation?: ManagedRuntimeCallPreparation, + crewAttempt?: { + assertCurrent: () => Promise + observeFailure: (failure: RuntimeEnvironmentSetupFailure) => void + } +): Promise { + let completeTimed = (): void => {} + const callbacks = { + onEvent: decodeRuntimeEnvironmentSubscriptionEvents( + method, + (event) => { + if (event.type !== 'binary') { + completeTimed() + } + rawCallbacks.onEvent(event) + }, + isCurrent + ), + onClose: () => { + completeTimed() + rawCallbacks.onClose() + } + } + await crewAttempt?.assertCurrent() + const environment = crewAttempt + ? await resolveEnvironment(userDataPath, selector) + : await resolveManagedRuntimeEnvironment(userDataPath, selector, preparation, method, signal) + await crewAttempt?.assertCurrent() + throwIfSignalAborted(signal) + if (crewAttempt) { + assertCrewSourceCurrent(environment) + } + const pairedConnection = getPreferredPairedRuntimeConnection(environment) + const effectiveTimeoutMs = timeoutMs ?? DEFAULT_REMOTE_RUNTIME_TIMEOUT_MS + let markedUsed = false + const markUsedOnce = (runtimeId: string): void => { + if (markedUsed || !isCurrent()) { + return + } + markedUsed = true + recordRuntimeEnvironmentUsage(userDataPath, environment.id, { runtimeId }) + } + const callbacksWithMarkUsed = { + onResponse: (response: Rpc.RuntimeRpcResponse) => { + if ( + crewAttempt && + response._meta?.runtimeId != null && + response._meta.runtimeId !== environment.runtimeId + ) { + throw new RemoteRuntimeClientError( + 'runtime_environment_changed', + 'Runtime environment identity changed; refresh and try again' + ) + } + if (response.ok === true) { + markUsedOnce(response._meta.runtimeId) + } + callbacks.onEvent({ type: 'response' as const, response }) + }, + onBinary: (bytes: Uint8Array) => + callbacks.onEvent({ type: 'binary' as const, bytes }), + onError: (error: { code: string; message: string }) => + callbacks.onEvent({ + type: 'error' as const, + code: error.code, + message: withRuntimeUnreachableHint(environment, error.message, pairedConnection.endpoint) + }), + onClose: () => { + callbacks.onEvent({ type: 'close' as const }) + callbacks.onClose() + } + } + // Initial connection failures reject; midstream errors use onError above. + const connect = async (): Promise => { + try { + if (shouldRouteSubscriptionBySupport(method)) { + return await subscribeSupportRoutedRuntimeEnvironment({ + userDataPath, + environment, + method, + params, + timeoutMs: effectiveTimeoutMs, + callbacks, + isCurrent, + signal + }) + } + const subscription = await subscribeRemoteRuntimeTransport( + pairedConnection, + method, + params, + effectiveTimeoutMs, + callbacksWithMarkUsed, + { clientCapabilities: ELECTRON_REMOTE_RUNTIME_CLIENT_CAPABILITIES, signal } + ) + if (crewAttempt) { + try { + throwIfSignalAborted(signal) + await crewAttempt.assertCurrent() + throwIfSignalAborted(signal) + assertCrewSourceCurrent(environment) + } catch (error) { + subscription.close() + throw error + } + } + return subscription + } catch (error) { + if (error instanceof Error && error.name !== 'AbortError') { + error.message = withRuntimeUnreachableHint( + environment, + error.message, + pairedConnection.endpoint + ) + } + crewAttempt?.observeFailure(runtimeEnvironmentSetupFailure(error, 'subscription')) + throw error + } + } + if (!isTimedPRRead(method, params)) { + return connect() + } + const subscription = await enqueueRuntimeSubscription( + runtimeCallQueuePool, + environment.id, + method, + params, + (complete) => { + completeTimed = complete + return connect() + }, + (handle) => handle.close(), + signal + ) + return { ...subscription, close: () => completeTimed() } +} diff --git a/src/main/ipc/runtime-environments/requests/runtime-environment-transport-routing.ts b/src/main/ipc/runtime-environments/requests/runtime-environment-transport-routing.ts index 4c458ab99eb..9209541d57e 100644 --- a/src/main/ipc/runtime-environments/requests/runtime-environment-transport-routing.ts +++ b/src/main/ipc/runtime-environments/requests/runtime-environment-transport-routing.ts @@ -2,13 +2,6 @@ import { markEnvironmentUsedFromResponse, shouldUseCachedRequestConnection } from './runtime-environment-usage-response' -import { RemoteRuntimeClientError } from '../../../../shared/remote-runtime/remote-runtime-client-error' -import { throwIfSignalAborted } from '../../../../shared/abort-signal-reason' -import { - type RuntimeEnvironmentSetupFailure, - runtimeEnvironmentSetupFailure -} from '../../../../shared/remote-runtime/host-catalog/runtime-environment-subscription-setup' -import { assertCrewSourceCurrent } from '../subscriptions/runtime-environment-subscription-setup' import { getRuntimeEnvironmentStatus } from '../status/runtime-environment-status-probe' import { getPreferredPairedRuntimeConnection } from '../../../../shared/remote-runtime/host-catalog/runtime-environments' import { ELECTRON_REMOTE_RUNTIME_CLIENT_CAPABILITIES } from '../../../../shared/electron-remote-runtime-client-capabilities' @@ -16,16 +9,11 @@ import { resolveEnvironment } from '../../../../shared/remote-runtime/host-store import { prepareFencedRuntimeRequest } from './runtime-environment-request-preparation' import { requireWslHostWithoutStart, - resolveManagedRuntimeEnvironment, type ManagedRuntimeCallPreparation } from '../managed-orcad/runtime-environment-managed-tunnel' -import { recordRuntimeEnvironmentUsage } from '../../../../shared/remote-runtime/host-store/runtime-environment-usage-record' import type * as Rpc from '../../../../shared/runtime-rpc-envelope' import '../../../../shared/remote-runtime/requests/remote-runtime-request-socket' -import { subscribeRemoteRuntimeTransport } from '../../../../shared/remote-runtime/requests/remote-runtime-subscription-transport' -import type { RemoteRuntimeTransportSubscription } from '../../../../shared/remote-runtime/requests/remote-runtime-subscription-contract' import { withRuntimeUnreachableHint } from './runtime-environment-unreachable-hint' -import { decodeRuntimeEnvironmentSubscriptionEvents } from '../subscriptions/runtime-environment-subscription-events' import { enqueueRuntimeCall } from './runtime-environment-call-queue' import { sendRemoteRuntimeConnectionRequestAbortable, @@ -34,9 +22,7 @@ import { import { runtimeEnvironmentRevisionFailure } from './runtime-environment-revision-guard' import { executeSupportRoutedCall, - shouldRouteCallBySupport, - shouldRouteSubscriptionBySupport, - subscribeSupportRoutedRuntimeEnvironment + shouldRouteCallBySupport } from './runtime-environment-support-routing' import { shouldUseSharedControlEnvelope } from '../connections/runtime-environment-shared-control-envelope' import { sendEnvelopedOneShotCall } from './runtime-environment-runtime-pinned-send' @@ -169,7 +155,8 @@ export async function callRuntimeEnvironment( markEnvironmentUsedFromResponse(userDataPath, currentEnvironment.id, response) return response }, - options?.signal + options?.signal, + params ) } catch (error) { if (error instanceof Error && error.name !== 'AbortError') { @@ -179,126 +166,4 @@ export async function callRuntimeEnvironment( } } -export async function subscribeRuntimeEnvironment( - userDataPath: string, - selector: string, - method: string, - params: unknown, - timeoutMs: number | undefined, - rawCallbacks: { - onEvent: ( - payload: - | { type: 'response'; response: Rpc.RuntimeRpcResponse } - | { type: 'binary'; bytes: Uint8Array } - | { type: 'error'; code: string; message: string } - | { type: 'close' } - ) => void - onClose: () => void - }, - isCurrent: () => boolean = () => true, - signal?: AbortSignal, - preparation?: ManagedRuntimeCallPreparation, - crewAttempt?: { - assertCurrent: () => Promise - observeFailure: (failure: RuntimeEnvironmentSetupFailure) => void - } -): Promise { - const callbacks = { - ...rawCallbacks, - onEvent: decodeRuntimeEnvironmentSubscriptionEvents(method, rawCallbacks.onEvent, isCurrent) - } - await crewAttempt?.assertCurrent() - const environment = crewAttempt - ? await resolveEnvironment(userDataPath, selector) - : await resolveManagedRuntimeEnvironment(userDataPath, selector, preparation, method, signal) - await crewAttempt?.assertCurrent() - throwIfSignalAborted(signal) - if (crewAttempt) { - assertCrewSourceCurrent(environment) - } - const pairedConnection = getPreferredPairedRuntimeConnection(environment) - const effectiveTimeoutMs = timeoutMs ?? DEFAULT_REMOTE_RUNTIME_TIMEOUT_MS - let markedUsed = false - const markUsedOnce = (runtimeId: string): void => { - if (markedUsed || !isCurrent()) { - return - } - markedUsed = true - recordRuntimeEnvironmentUsage(userDataPath, environment.id, { runtimeId }) - } - const callbacksWithMarkUsed = { - onResponse: (response: Rpc.RuntimeRpcResponse) => { - if ( - crewAttempt && - response._meta?.runtimeId != null && - response._meta.runtimeId !== environment.runtimeId - ) { - throw new RemoteRuntimeClientError( - 'runtime_environment_changed', - 'Runtime environment identity changed; refresh and try again' - ) - } - if (response.ok === true) { - markUsedOnce(response._meta.runtimeId) - } - callbacks.onEvent({ type: 'response' as const, response }) - }, - onBinary: (bytes: Uint8Array) => - callbacks.onEvent({ type: 'binary' as const, bytes }), - onError: (error: { code: string; message: string }) => - callbacks.onEvent({ - type: 'error' as const, - code: error.code, - message: withRuntimeUnreachableHint(environment, error.message, pairedConnection.endpoint) - }), - onClose: () => { - callbacks.onEvent({ type: 'close' as const }) - callbacks.onClose() - } - } - // Initial connection failures reject; midstream errors use onError above. - try { - if (shouldRouteSubscriptionBySupport(method)) { - return await subscribeSupportRoutedRuntimeEnvironment({ - userDataPath, - environment, - method, - params, - timeoutMs: effectiveTimeoutMs, - callbacks, - isCurrent, - signal - }) - } - const subscription = await subscribeRemoteRuntimeTransport( - pairedConnection, - method, - params, - effectiveTimeoutMs, - callbacksWithMarkUsed, - { clientCapabilities: ELECTRON_REMOTE_RUNTIME_CLIENT_CAPABILITIES, signal } - ) - if (crewAttempt) { - try { - throwIfSignalAborted(signal) - await crewAttempt.assertCurrent() - throwIfSignalAborted(signal) - assertCrewSourceCurrent(environment) - } catch (error) { - subscription.close() - throw error - } - } - return subscription - } catch (error) { - if (error instanceof Error && error.name !== 'AbortError') { - error.message = withRuntimeUnreachableHint( - environment, - error.message, - pairedConnection.endpoint - ) - } - crewAttempt?.observeFailure(runtimeEnvironmentSetupFailure(error, 'subscription')) - throw error - } -} +export { subscribeRuntimeEnvironment } from './runtime-environment-subscription-routing' diff --git a/src/main/ipc/runtime-environments/runtime-environment-call-queue.test.ts b/src/main/ipc/runtime-environments/runtime-environment-call-queue.test.ts new file mode 100644 index 00000000000..8e1edc94148 --- /dev/null +++ b/src/main/ipc/runtime-environments/runtime-environment-call-queue.test.ts @@ -0,0 +1,19 @@ +import { expect, it } from 'vitest' +import { enqueueRuntimeCall } from './requests/runtime-environment-call-queue' + +it('passes timed intent to the desktop queue without holding status or manual PR reads', async () => { + const release = Promise.withResolvers() + const waiting = [0, 1].map(() => + enqueueRuntimeCall('host', 'github.prForBranch', () => release.promise, undefined, { + reason: 'swr' + }) + ) + await expect( + enqueueRuntimeCall('host', 'github.prForBranch', async () => 'current', undefined, { + reason: 'manual' + }) + ).resolves.toBe('current') + await expect(enqueueRuntimeCall('host', 'git.status', async () => 'clean')).resolves.toBe('clean') + release.resolve() + await Promise.all(waiting) +}) diff --git a/src/main/ipc/runtime-environments/subscriptions/runtime-environments-subscription-lifecycle.test.ts b/src/main/ipc/runtime-environments/subscriptions/runtime-environments-subscription-lifecycle.test.ts index f01ea19585d..919516fd8fa 100644 --- a/src/main/ipc/runtime-environments/subscriptions/runtime-environments-subscription-lifecycle.test.ts +++ b/src/main/ipc/runtime-environments/subscriptions/runtime-environments-subscription-lifecycle.test.ts @@ -201,6 +201,69 @@ describe('registerRuntimeEnvironmentHandlers', () => { } } ) + it('holds timed signal-route admission after setup while foreground and status subscriptions remain runnable', async () => { + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: these handlers use only the fixture settings methods. + await registerRuntimeEnvironmentHandlers(store as never) + const finish: (() => void)[] = [] + subscribeRemoteRuntimeRequestMock.mockImplementation( + async (_connection, _method, _params, _timeout, callbacks) => { + finish.push(() => + callbacks.onResponse({ id: 'rpc', ok: true, result: {}, _meta: { runtimeId: 'runtime' } }) + ) + return { requestId: `request-${finish.length}`, close: vi.fn(), sendBinary: vi.fn() } + } + ) + const add = handler<{ name: string; pairingCode: string }, unknown>( + 'runtimeEnvironments:addFromPairingCode' + ) + await add(null, { name: 'desk', pairingCode: pairingCode() }) + const sender = { + id: 1, + isDestroyed: () => false, + send: vi.fn(), + on: vi.fn(), + removeListener: vi.fn() + } + const subscribe = handler< + { + selector: string + method: string + params: { reason?: string; selected?: boolean } + subscriptionId: string + }, + { subscriptionId: string; requestId: string } + >('runtimeEnvironments:subscribe') + const start = (subscriptionId: string, reason: string, method = 'github.prForBranch') => + subscribe( + { sender }, + { selector: 'desk', subscriptionId, method, params: { reason, selected: true } } + ) + await start('timer-1', 'swr') + expect(subscribeRemoteRuntimeRequestMock.mock.calls[0][2]).toMatchObject({ + reason: 'swr', + selected: true + }) + await start('timer-2', 'swr') + const third = start('timer-3', 'swr') + const cancelled = expect(third).rejects.toThrow() + await start('manual', 'manual') + await start('status', '', 'git.status') + await start('review', '', 'hostedReview.forBranch') + expect(finish).toHaveLength(5) + const cancel = handler<{ subscriptionId: string }, unknown>('runtimeEnvironments:unsubscribe') + await cancel({ sender }, { subscriptionId: 'timer-3' }) + await cancelled + const fourth = start('timer-4', 'swr') + finish[0]() + await fourth + expect(finish).toHaveLength(6) + const unsubscribe = handler<{ subscriptionId: string }, unknown>( + 'runtimeEnvironments:unsubscribe' + ) + for (const subscriptionId of ['timer-1', 'timer-2', 'timer-4', 'manual', 'status', 'review']) { + await unsubscribe({ sender }, { subscriptionId }) + } + }) it('rejects cross-window streaming subscription control', async () => { await registerRuntimeEnvironmentHandlers(store as never) diff --git a/src/main/ipc/source-control/github-pr-refresh-handlers.ts b/src/main/ipc/source-control/github-pr-refresh-handlers.ts index 6fd54453d72..b174d20d914 100644 --- a/src/main/ipc/source-control/github-pr-refresh-handlers.ts +++ b/src/main/ipc/source-control/github-pr-refresh-handlers.ts @@ -4,7 +4,7 @@ import type { GitHubPRRefreshCandidate, GitHubPRRefreshEnqueueResult, GitHubPRRefreshReason, - PRRefreshOutcome + PRSnapshotReply } from '../../../shared/github/pull-requests/refresh/pull-request-refresh-types' import type { Repo } from '../../../shared/repo-types' import type { OrcaRuntimeService } from '../../runtime/orca-runtime' @@ -44,8 +44,8 @@ export function registerGitHubPRRefreshHandlers( ): void { // The windows these handlers serve are the renderers the coordinator reports to. setPRRefreshRendererPort(electronPRRefreshRenderer) - function recordPRIfNeeded(repo: Repo, outcome: PRRefreshOutcome): void { - if (outcome.kind === 'found' && !stats.hasCountedPR(outcome.pr.url)) { + function recordPRIfNeeded(repo: Repo, outcome: PRSnapshotReply): void { + if ('kind' in outcome && outcome.kind === 'found' && !stats.hasCountedPR(outcome.pr.url)) { stats.record({ type: 'pr_created', at: Date.now(), diff --git a/src/main/ipc/telemetry.ts b/src/main/ipc/telemetry.ts index ceb99489cd9..fdde258a5a1 100644 --- a/src/main/ipc/telemetry.ts +++ b/src/main/ipc/telemetry.ts @@ -28,6 +28,7 @@ const MAIN_OWNED_TELEMETRY_EVENTS = new Set([ 'daemon_audit_eligibility', 'daemon_pty_cwd_denied', 'daemon_pty_cwd_readable', + 'daemon_pty_cwd_denial_capture', 'star_nag_outcome', 'feature_interaction_usage_bucket_reached', 'first_agent_interaction', diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-account-expiry.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-account-expiry.ts index 3674f20d6e9..1d65f224792 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-account-expiry.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-account-expiry.ts @@ -12,7 +12,7 @@ const ACCOUNT_SETTINGS = { /** Account changes invalidate probe answers even when the account path stays the same. */ export function expireAgentModelCatalogFailuresForSettings( - store: Pick, + store: Pick, updates: Partial ): void { const agents = new Set( @@ -21,7 +21,7 @@ export function expireAgentModelCatalogFailuresForSettings( .map(([, agent]) => agent) ) for (const agent of agents) { - store.expireFailures(agent) + store.expireScopes(agent) } } @@ -72,9 +72,9 @@ export function agentsWithChangedLaunchSettings( ) } -/** Account changes expire failures; command/environment changes also expire saved listings. */ +/** Account and launch-setting changes retain display while requiring rediscovery. */ export function createAgentModelCatalogSettingsExpiry( - store: Pick, + store: Pick, initial: AgentLaunchSettings ): (updates: Partial, settings: AgentLaunchSettings) => void { let launch = initial diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-account-resolution.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-account-resolution.ts new file mode 100644 index 00000000000..4c4f8d3a8fe --- /dev/null +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-account-resolution.ts @@ -0,0 +1,46 @@ +import { independentAgentModelCatalog } from '../../../shared/agent-session/options/agent-model-catalog-ownership' +import type { AgentModelCatalogServiceDeps } from './agent-model-catalog-service-contract' +import type { AgentModelCatalogLaunch } from './agent-model-catalog-refresh' +import { recordCatalogKey } from './agent-model-catalog-fingerprint' +import { newChatCatalogKey } from './agent-model-catalog-prewarm' +import { catalogFolder, sessionWorkspacePath } from './agent-model-catalog-workspace' + +export async function resolveAgentModelCatalogAccount( + deps: AgentModelCatalogServiceDeps, + launch: AgentModelCatalogLaunch, + current: () => boolean +) { + const { agent, records } = launch + const { record, scoped } = records + const workspacePath = + launch.inSessionWorkspace || + (record && + launch.workspacePath === undefined && + !launch.legacy && + (independentAgentModelCatalog(agent) || deps.agentReadsProjectModelConfig?.(agent) === true)) + ? record + ? await sessionWorkspacePath(deps, record) + : null + : launch.workspacePath + if (!current()) { + return null + } + const folder = await catalogFolder(deps, agent, workspacePath, scoped) + if (!current()) { + return null + } + const account = scoped + ? recordCatalogKey(scoped, folder) + : await newChatCatalogKey(deps, agent, folder) + if (!current() || !account) { + return null + } + return { + record, + scoped, + workspacePath, + folder, + base: account.fingerprint, + probeHome: account.accountHome + } +} diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-discovery-freshness.test.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-discovery-freshness.test.ts index e49f7e3ad20..224a1c183d8 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-discovery-freshness.test.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-discovery-freshness.test.ts @@ -102,7 +102,7 @@ describe('a live listing beside the account-level discovery', () => { expect(read?.models[0]?.defaultEffort).toBe('high') }) - it('still refreshes the account listing after ten minutes of live saves', async () => { + it('keeps discovery across elapsed time and live saves until the user refreshes', async () => { let configured = 'grok-4' const run = harness(async () => discovery(configured)) expect(await run.defaultOf()).toBe('grok-4') @@ -113,7 +113,10 @@ describe('a live listing beside the account-level discovery', () => { run.advance(60_000) run.saveLive() } - expect(run.store.shouldRefresh(agentModelCatalogFingerprintForRecord(RECORD))).toBe(true) + expect(run.store.shouldRefresh(agentModelCatalogFingerprintForRecord(RECORD))).toBe(false) + await run.catalog.demand({ agent: 'grok' }) + expect(await run.defaultOf()).toBe('grok-4') + run.store.refreshInputs(true) await run.catalog.demand({ agent: 'grok' }) await vi.waitFor(() => expect(run.probes).toHaveBeenCalledTimes(2)) await vi.waitFor(async () => expect(await run.defaultOf()).toBe('grok-3-mini')) diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-discovery.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-discovery.ts index 358cd99b082..6d7c579c24e 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-discovery.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-discovery.ts @@ -2,12 +2,24 @@ import type { StructuredAgentResolutionOperation } from '../agent-launch/structu import type { AgentSessionAccountHome } from '../../../shared/agent-session/lifecycle/agent-session-account-home' import type { AgentModelCatalogSuccess } from './agent-model-catalog-entry' +export type AgentModelCatalogProgress = (( + success: AgentModelCatalogSuccess, + accountKnown?: boolean +) => void) & { + /** Retain the latest accumulator; only real reads or settlement materialize it. */ + defer?: (read: () => AgentModelCatalogSuccess, known: (model: string) => boolean) => void + demanded?: () => ReadonlySet +} + export type AgentModelCatalogResolvedProbe = { identity: unknown + inputStamp?: string /** `operation` owns the run; it defaults to the one the invocation was resolved under. */ run: ( signal?: AbortSignal, - operation?: StructuredAgentResolutionOperation + operation?: StructuredAgentResolutionOperation, + selectedModels?: readonly string[], + publish?: AgentModelCatalogProgress ) => Promise } @@ -22,11 +34,13 @@ export type AgentModelCatalogProbe = (( cwd?: string selectedModels?: readonly string[] operation?: StructuredAgentResolutionOperation + publish?: AgentModelCatalogProgress } ) => Promise) & { resolveLegacy?: ( home: AgentSessionAccountHome, - operation?: StructuredAgentResolutionOperation + operation?: StructuredAgentResolutionOperation, + lookup?: AgentModelCatalogLookup ) => Promise resolve?: ( home: AgentSessionAccountHome, diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-effort-discovery.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-effort-discovery.ts index 3bd96e94007..cc728b573a7 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-effort-discovery.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-effort-discovery.ts @@ -29,7 +29,7 @@ export function createAgentModelEffortDiscovery(input: { }, due: (fingerprint: string, model: string | undefined): boolean => !input.failed(fingerprint) && - agentModelEffortProbeDue(input.entries.get(fingerprint), input.attempts, model, input.now()), + agentModelEffortProbeDue(input.entries.get(fingerprint), input.attempts, model), demand: (fingerprint: string, model: string | undefined): void => demandAgentModelEfforts(input.refreshes.get(fingerprint), model, input.cap), attempted: (fingerprint: string, agent: string, model: string): void => diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-entry-lifetime.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-entry-lifetime.ts index 68c3194af40..ea55d5a5ce0 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-entry-lifetime.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-entry-lifetime.ts @@ -1,39 +1,23 @@ -import { - agentModelCatalogEntryWithSuccess, - type AgentModelCatalogEntry, - type AgentModelCatalogSuccess, - type AgentModelCatalogSource -} from './agent-model-catalog-entry' import { independentAgentModelCatalog, usesModelSpecificAcpEfforts } from '../../../shared/agent-session/options/agent-model-catalog-ownership' - -export const AGENT_MODEL_CATALOG_FRESH_MS = 10 * 60_000 +import type { AgentModelCatalogFailures } from './agent-model-catalog-failures' +import type { AgentModelCatalogPersistence } from './agent-model-catalog-persistence' +import type { createAgentModelEffortDiscovery } from './agent-model-catalog-effort-discovery' +import { + agentModelCatalogEntryWithSuccess, + agentModelCatalogListingKey, + type AgentModelCatalogEntry, + type AgentModelCatalogSuccess, + type AgentModelCatalogSource +} from './agent-model-catalog-entry' export function agentModelCatalogEntryIsStale( entry: AgentModelCatalogEntry, - due: boolean, - now: number + due: boolean ): boolean { - return ( - due || - !entry.discovered || - (independentAgentModelCatalog(entry.agent) && entry.discovered.complete !== true) || - now - entry.discovered.at >= - (independentAgentModelCatalog(entry.agent) ? 2 * 60_000 : AGENT_MODEL_CATALOG_FRESH_MS) - ) -} - -export function pruneAgentModelCatalogPartialAttempts( - attempts: Map, - now: number -): void { - for (const [key, attempt] of attempts) { - if (now - attempt.at >= 2 * 60_000) { - attempts.delete(key) - } - } + return due || !entry.discovered } export function recordAgentModelCatalogPartialAttempt( @@ -55,13 +39,11 @@ export function recordAgentModelCatalogPartialAttempt( export function agentModelEffortProbeDue( entry: AgentModelCatalogEntry | undefined, attempts: Map, - model: string | undefined, - now: number + model: string | undefined ): boolean { if (!model || !entry || !usesModelSpecificAcpEfforts(entry.agent)) { return false } - pruneAgentModelCatalogPartialAttempts(attempts, now) return ( entry.models.some((row) => row.id === model && row.effortsKnown === false) && !attempts.has(JSON.stringify([entry.fingerprint, model])) @@ -132,3 +114,82 @@ export function createAgentModelCatalogEntryFactory( now() ) } + +function writeAgentModelCatalogSuccess( + state: { + entries: Map + retiredObservations: Set + dueEntries: Set + failures: AgentModelCatalogFailures + efforts: ReturnType + entryFromSuccess: ReturnType + persistence: AgentModelCatalogPersistence | null + evict: () => void + }, + fingerprint: string, + agent: string, + success: AgentModelCatalogSuccess, + source: AgentModelCatalogSource, + settled = true, + accountKnown = settled, + unchanged = false +): AgentModelCatalogEntry | null { + const entry = + (unchanged ? state.entries.get(fingerprint) : undefined) ?? + state.entryFromSuccess(fingerprint, agent, success, source) + if (!entry) { + if (settled && source === 'discovery' && independentAgentModelCatalog(agent)) { + state.failures.probeFailed(fingerprint, agent, new Error('agent listed no models')) + } + return null + } + state.retiredObservations.delete(fingerprint) + const previous = state.entries.get(fingerprint) + state.entries.delete(fingerprint) + state.entries.set(fingerprint, entry) + // Live listings cannot clear discovery failures or account-unavailability verdicts. + if (source === 'discovery') { + if (settled) { + state.dueEntries.delete(fingerprint) + } + if (accountKnown || success.unavailable) { + state.failures.listed(fingerprint, agent, success.origin, success.unavailable) + } + } + if (settled && source === 'discovery' && independentAgentModelCatalog(agent)) { + state.efforts.catalogAttempted(fingerprint, agent, success.complete === true) + } + state.evict() + // Unchanged per-turn listings need no disk write. + if ( + previous !== entry && + (!previous || agentModelCatalogListingKey(previous) !== agentModelCatalogListingKey(entry)) + ) { + state.persistence?.save(() => [...state.entries.values()]) + } + return entry +} + +export function createAgentModelCatalogSuccessWriter( + readState: () => Parameters[0] +) { + return ( + fingerprint: string, + agent: string, + success: AgentModelCatalogSuccess, + source: AgentModelCatalogSource, + settled = true, + accountKnown = settled, + unchanged = false + ) => + writeAgentModelCatalogSuccess( + readState(), + fingerprint, + agent, + success, + source, + settled, + accountKnown, + unchanged + ) +} diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-input-stamp.test.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-input-stamp.test.ts new file mode 100644 index 00000000000..ae5d21db97f --- /dev/null +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-input-stamp.test.ts @@ -0,0 +1,309 @@ +import { createAcpModelCatalogProbe } from '../../acp/discovery/acp-model-catalog-probe' +import { acpLaunchSpecFor } from '../../acp/launch/acp-launch-registry' +import { openCodeAcpAccountBinding } from '../../opencode/accounts/opencode-structured-account-home' +import { ManagedDataAccountService } from '../../managed-data-accounts/service' +import { mkdtemp, writeFile, readFile, rm, mkdir } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join, dirname } from 'node:path' +import { expect, it, onTestFinished } from 'vitest' +import { agentModelCatalogInputStamp } from './agent-model-catalog-input-stamp' +import { agentModelCatalogScriptEntry } from './agent-model-catalog-script-entry' +import { createAgentModelCatalogService } from './agent-model-catalog-service' +import { AgentModelCatalogStore } from './agent-model-catalog-store' +import type { AgentModelCatalogProbe } from './agent-model-catalog-discovery' + +it.each(['grok', 'codex'])( + 'observes %s installs before deciding freshness, ignoring probe-owned writes', + async (agent) => { + const folder = await mkdtemp(join(tmpdir(), 'orca-catalog-inputs-')) + onTestFinished(() => rm(folder, { recursive: true, force: true })) + const script = join(folder, 'cli.js') + const config = join(folder, 'settings.json') + await writeFile(script, 'version-one') + await writeFile(config, '{}') + let model = 'first' + const resolve = async () => ({ + identity: 'same-invocation', + inputStamp: await agentModelCatalogInputStamp({ + command: process.execPath, + prefixArgs: [script], + cwd: folder, + env: {}, + configFiles: [config] + }), + run: async () => ({ + models: [{ id: model, label: model, efforts: [], isDefault: true }], + origin: 'probe' as const + }) + }) + const probe: AgentModelCatalogProbe = Object.assign( + async () => { + throw new Error('unresolved') + }, + { resolve, resolveLegacy: resolve } + ) + const service = createAgentModelCatalogService({ + store: new AgentModelCatalogStore(), + getConversation: () => undefined, + getRecord: () => undefined, + drivesRecord: () => true, + resolveAccountHome: async () => ({ + variable: agent === 'codex' ? 'CODEX_HOME' : 'GROK_HOME', + path: folder + }), + probes: { [agent]: probe } + }) + onTestFinished(() => service.stop()) + const read = () => + service.read({ + agent, + legacy: agent === 'codex', + workspacePath: folder, + waitForListing: true + }) + expect(await read()).toMatchObject({ models: [{ id: 'first' }] }) + model = 'second' + await writeFile(join(folder, '.claude.json'), 'probe usage') + await read() + expect(await read()).toMatchObject({ models: [{ id: 'first' }] }) + await writeFile(script, 'version-two-with-new-model') + expect(await read()).toMatchObject({ models: [{ id: 'second' }] }) + model = 'configured' + await writeFile(config, '{"model":"configured"}') + expect(await read()).toMatchObject({ models: [{ id: 'configured' }] }) + } +) + +it('resolves relative PATH installs and direct interpreter scripts from their actual cwd', async () => { + const folder = await mkdtemp(join(tmpdir(), 'orca-catalog-relative-')) + onTestFinished(() => rm(folder, { recursive: true, force: true })) + const command = join(folder, 'bin', process.platform === 'win32' ? 'node.exe' : 'node') + await mkdir(dirname(command), { recursive: true }) + await writeFile(command, 'install-one', { mode: 0o755 }) + await writeFile(join(folder, 'cli.js'), 'script-one') + const input = { command: 'node', prefixArgs: ['cli.js'], cwd: folder, env: { PATH: 'bin' } } + const before = await agentModelCatalogInputStamp(input) + await writeFile(command, 'install-two-new') + const installed = await agentModelCatalogInputStamp(input) + expect(installed).not.toBe(before) + await writeFile(join(folder, 'cli.js'), 'script-two-new') + expect(await agentModelCatalogInputStamp(input)).not.toBe(installed) +}) + +it.each(['bun', 'deno'])( + 'observes the direct %s run operand rather than the run subcommand', + async (command) => { + const folder = await mkdtemp(join(tmpdir(), 'orca-catalog-script-')) + onTestFinished(() => rm(folder, { recursive: true, force: true })) + await writeFile(join(folder, 'agent.ts'), 'one') + const input = { command, prefixArgs: ['run', 'agent.ts'], cwd: folder, env: {} } + const original = await agentModelCatalogInputStamp(input) + await writeFile(join(folder, 'run'), 'unrelated') + expect(await agentModelCatalogInputStamp(input)).toBe(original) + await writeFile(join(folder, 'agent.ts'), 'replacement-script') + expect(await agentModelCatalogInputStamp(input)).not.toBe(original) + } +) + +it('observes the real ACP legacy command script in its listing folder before using saved rows', async () => { + const folder = await mkdtemp(join(tmpdir(), 'orca-catalog-legacy-acp-')) + onTestFinished(() => rm(folder, { recursive: true, force: true })) + const home = join(folder, 'home') + const workspace = join(folder, 'workspace') + await Promise.all([mkdir(home), mkdir(workspace)]) + await writeFile(join(home, 'agent.js'), 'home-model') + await writeFile(join(workspace, 'agent.js'), 'workspace-model') + const probe = createAcpModelCatalogProbe( + { + ...acpLaunchSpecFor('grok')!, + minimumVersions: undefined, + modelDiscovery: { + kind: 'command', + args: [], + listingNamesConfiguredModel: true, + parse: (id) => [{ id, label: id, efforts: [], isDefault: true }] + } + }, + { + homePath: home, + dataDirectory: folder, + resolveEnvironment: async () => ({}), + resolveCommandSettings: () => ({ + agentCmdOverrides: { grok: `"${process.execPath}" agent.js` } + }), + runListing: async (launch) => readFile(join(launch.cwd!, launch.args[0]!), 'utf8') + } + ) + const store = new AgentModelCatalogStore() + const service = createAgentModelCatalogService({ + store, + getConversation: () => undefined, + getRecord: () => undefined, + drivesRecord: () => true, + resolveAccountHome: async () => ({ variable: 'GROK_HOME', path: home }), + probes: { grok: probe } + }) + onTestFinished(() => service.stop()) + const read = async () => { + const params = { agent: 'grok', legacy: true, workspacePath: workspace, waitForListing: true } + await service.demand(params) + return service.read(params) + } + expect(await read()).toMatchObject({ models: [{ id: 'workspace-model' }] }) + await writeFile(join(workspace, 'agent.js'), 'upgraded-workspace-model') + expect(await read()).toMatchObject({ models: [{ id: 'upgraded-workspace-model' }] }) +}) + +it.each([ + ['node', '--no-warnings'], + ['python3', '-u'] +])('refreshes a cached catalog when the %s script after an option changes', async (name, flag) => { + const folder = await mkdtemp(join(tmpdir(), 'orca-catalog-flagged-')) + onTestFinished(() => rm(folder, { recursive: true, force: true })) + const home = join(folder, 'home') + const workspace = join(folder, 'workspace') + await Promise.all([mkdir(home), mkdir(workspace)]) + const executable = join(folder, process.platform === 'win32' ? `${name}.exe` : name) + await writeFile(executable, 'fixture interpreter', { mode: 0o755 }) + await writeFile(join(home, 'agent.js'), 'qa/home') + await writeFile(join(workspace, 'agent.js'), 'qa/first') + const accounts = new ManagedDataAccountService(join(folder, 'profiles')) + const probe = createAcpModelCatalogProbe( + { + ...acpLaunchSpecFor('opencode')!, + account: openCodeAcpAccountBinding(() => accounts) + }, + { + homePath: home, + dataDirectory: folder, + resolveEnvironment: async () => ({}), + resolveCommandSettings: () => ({ + agentCmdOverrides: { opencode: `"${executable}" ${flag} agent.js` } + }), + probeVersion: async (input) => { + expect(input).toMatchObject({ + program: executable, + prefixArgs: [flag, 'agent.js'], + cwd: home + }) + return { version: '1.19.5' } + }, + runListing: async (launch) => { + expect(launch).toMatchObject({ + command: executable, + args: [flag, 'agent.js', 'models', '--verbose'], + cwd: workspace + }) + return readFile(join(workspace, 'agent.js'), 'utf8') + } + } + ) + const service = createAgentModelCatalogService({ + store: new AgentModelCatalogStore(), + getConversation: () => undefined, + getRecord: () => undefined, + drivesRecord: () => true, + resolveAccountHome: async () => ({ kind: 'opencode', locator: { kind: 'unmanaged' } }), + listsInChatFolder: new Set(['opencode']), + probes: { opencode: probe } + }) + onTestFinished(() => service.stop()) + const read = () => + service.read({ agent: 'opencode', workspacePath: workspace, waitForListing: true }) + expect(await read()).toMatchObject({ models: [{ id: 'qa/first' }] }) + await writeFile(join(workspace, 'agent.js'), 'qa/upgraded-script') + expect(await read()).toMatchObject({ models: [{ id: 'qa/upgraded-script' }] }) +}) + +it.each([ + { command: 'node', args: ['--trace-warnings', '--no-warnings', 'agent.js', 'argument.txt'] }, + { command: 'node', args: ['-r', 'preload.js', '--import=loader.js', 'agent.js', 'argument.txt'] }, + { command: 'node', args: ['--', '-agent.js'], script: '-agent.js' }, + { command: 'python3', args: ['-u', '-W', 'ignore', '-Xdev', 'agent.js', 'argument.txt'] }, + { command: 'deno', args: ['run', '-A', '--allow-read', 'agent.js', 'argument.txt'] }, + { + command: 'deno', + args: [ + 'run', + '--allow-read=permission.txt', + '--config', + 'config.json', + '--import-map=map.json', + 'agent.js' + ] + } +])( + 'stamps only the direct file in $command $args', + async ({ command, args, script = 'agent.js' }) => { + const folder = await mkdtemp(join(tmpdir(), 'orca-catalog-operand-')) + onTestFinished(() => rm(folder, { recursive: true, force: true })) + await writeFile(join(folder, script), 'original script') + const input = { command, prefixArgs: args, cwd: folder, env: {} } + const original = await agentModelCatalogInputStamp(input) + for (const name of [ + 'preload.js', + 'loader.js', + 'ignore', + 'permission.txt', + 'config.json', + 'map.json', + 'argument.txt' + ]) { + await writeFile(join(folder, name), 'unselected file changed') + } + expect(await agentModelCatalogInputStamp(input)).toBe(original) + await writeFile(join(folder, script), 'replacement entry script') + expect(await agentModelCatalogInputStamp(input)).not.toBe(original) + } +) + +it.skipIf(process.platform === 'win32')( + 'observes a Node literal filename containing a colon', + async () => { + const folder = await mkdtemp(join(tmpdir(), 'orca-catalog-colon-')) + onTestFinished(() => rm(folder, { recursive: true, force: true })) + const script = join(folder, 'agent:stable.cjs') + await writeFile(script, 'original script') + const input = { command: 'node', prefixArgs: ['agent:stable.cjs'], cwd: folder, env: {} } + const original = await agentModelCatalogInputStamp(input) + await writeFile(script, 'replacement entry script') + expect(await agentModelCatalogInputStamp(input)).not.toBe(original) + } +) + +it.each<[string, string[]]>([ + ['node', ['--unknown', 'candidate.js', 'agent.js']], + ['node', ['--no-warnings=true', 'candidate.js']], + ['node', ['--require', '--', 'candidate.js']], + ['node', ['--require=', 'candidate.js']], + ['node', ['-rpreload.js', 'candidate.js']], + ['node', ['--eval=candidate.js', 'agent.js']], + ['node', ['-p', 'candidate.js', 'agent.js']], + ['node', ['inspect', 'candidate.js']], + ['node', ['-', 'candidate.js']], + ['node', ['--']], + ['python3', ['-c', 'candidate.js', 'agent.js']], + ['python3', ['-mcandidate.js', 'agent.js']], + ['python3', ['-uB', 'candidate.js']], + ['deno', ['run', '--unknown=value', 'candidate.js']], + ['deno', ['run', '--allow-fictional', 'candidate.js']], + ['deno', ['run', '--config', '--', 'candidate.js']], + ['deno', ['run', '--allow-read=', 'candidate.js']], + ['deno', ['run', 'npm:candidate.js']], + ['deno', ['run', 'https://example.com/candidate.js']], + ['deno', ['run', '-', 'candidate.js']] +])('does not guess a file for opaque %s arguments %j', async (command, args) => { + const folder = await mkdtemp(join(tmpdir(), 'orca-catalog-opaque-')) + onTestFinished(() => rm(folder, { recursive: true, force: true })) + const input = { command, prefixArgs: args, cwd: folder, env: {} } + const original = await agentModelCatalogInputStamp(input) + await writeFile(join(folder, 'candidate.js'), 'not an identified file') + await writeFile(join(folder, 'agent.js'), 'not an identified file') + expect(await agentModelCatalogInputStamp(input)).toBe(original) +}) + +it('recognizes a Deno Windows file without treating its drive as a URI scheme', () => { + expect(agentModelCatalogScriptEntry('deno', ['run', '-A', 'C:\\tools\\agent.ts'])).toBe( + 'C:\\tools\\agent.ts' + ) +}) diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-input-stamp.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-input-stamp.ts new file mode 100644 index 00000000000..5a0b91b8683 --- /dev/null +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-input-stamp.ts @@ -0,0 +1,49 @@ +import { access, realpath, stat } from 'node:fs/promises' +import { constants } from 'node:fs' +import { basename, delimiter, isAbsolute, resolve } from 'node:path' +import { getExecutableNames } from '../../../shared/node-cli-command-resolution' +import { agentModelCatalogScriptEntry } from './agent-model-catalog-script-entry' + +async function fileStamp(path: string): Promise { + try { + const target = await realpath(path) + const info = await stat(target) + return [target, info.ino, info.size, info.ctimeMs, info.mtimeMs] + } catch { + return [path, 'unknown'] + } +} + +/** Observe install/config identity without running the CLI or reading credentials. */ +export async function agentModelCatalogInputStamp(input: { + command: string + prefixArgs?: readonly string[] + cwd: string + env: NodeJS.ProcessEnv + configFiles?: readonly string[] +}): Promise { + const pathEnv = input.env.PATH ?? input.env.Path ?? '' + let executable = resolve(input.cwd, input.command) + if (!isAbsolute(input.command) && !/[\\/]/.test(input.command)) { + search: for (const directory of pathEnv.split(delimiter)) { + for (const name of getExecutableNames(process.platform, input.command)) { + const candidate = resolve(input.cwd, directory, name) + try { + await access(candidate, process.platform === 'win32' ? constants.F_OK : constants.X_OK) + executable = candidate + break search + } catch {} + } + } + } + const interpreter = basename(input.command) + .replace(/\.exe$/i, '') + .toLowerCase() + const operand = agentModelCatalogScriptEntry(interpreter, input.prefixArgs ?? []) + const script = operand ? resolve(input.cwd, operand) : undefined + return JSON.stringify([ + await fileStamp(executable), + script ? await fileStamp(script) : null, + await Promise.all((input.configFiles ?? []).map(fileStamp)) + ]) +} diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-inputs.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-inputs.ts new file mode 100644 index 00000000000..d705ecfff78 --- /dev/null +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-inputs.ts @@ -0,0 +1,83 @@ +import type { AgentModelCatalogEntry } from './agent-model-catalog-entry' +import type { AgentModelCatalogFailures } from './agent-model-catalog-failures' +import type { AgentModelCatalogListingWaiters } from './agent-model-catalog-listing-waiters' +import { retireAgentModelCatalogListing, type InFlightListing } from './agent-model-catalog-lister' + +export class AgentModelCatalogInputs { + private readonly inputStamps = new Map() + + constructor( + private readonly state: { + entries: Map + refreshes: Map + dueEntries: Set + failures: AgentModelCatalogFailures + partialAttempts: Map + listingWaiters: AgentModelCatalogListingWaiters + agents: () => Iterable + retireAgent: (agent: string) => void + cap: number + } + ) {} + + observeInputs(fingerprint: string, stamp: string): void { + const previous = this.inputStamps.get(fingerprint) + this.inputStamps.delete(fingerprint) + this.inputStamps.set(fingerprint, stamp) + if (previous !== stamp && (previous !== undefined || this.state.entries.has(fingerprint))) { + this.expireScope(fingerprint) + } + if (this.inputStamps.size > this.state.cap) { + this.inputStamps.delete(this.inputStamps.keys().next().value!) + } + } + + expireScope(fingerprint: string): void { + const flight = this.state.refreshes.get(fingerprint) + if (flight) { + retireAgentModelCatalogListing( + this.state.refreshes, + this.state.listingWaiters, + fingerprint, + flight + ) + } + if (this.state.entries.has(fingerprint)) { + this.state.dueEntries.add(fingerprint) + } + this.state.failures.expire(fingerprint) + this.state.partialAttempts.delete(fingerprint) + const prefix = `${JSON.stringify([fingerprint]).slice(0, -1)},` + for (const key of this.state.partialAttempts.keys()) { + if (key.startsWith(prefix)) { + this.state.partialAttempts.delete(key) + } + } + } + + refreshInputs(userInitiated: boolean): void { + for (const agent of this.state.agents()) { + this.state.failures.expireAgent(agent) + } + if (userInitiated) { + this.expireScopes() + } + } + + expireScopes(agent?: string): void { + if (agent) { + this.state.retireAgent(agent) + } + const scopes = new Set([ + ...[...this.state.entries.values()] + .filter((entry) => !agent || entry.agent === agent) + .map((entry) => entry.fingerprint), + ...[...this.state.refreshes] + .filter(([, flight]) => !agent || flight.agent === agent) + .map(([fingerprint]) => fingerprint) + ]) + for (const fingerprint of scopes) { + this.expireScope(fingerprint) + } + } +} diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-lister.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-lister.ts index c070ecf3eb0..237702e6dd7 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-lister.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-lister.ts @@ -1,5 +1,4 @@ import type { StructuredAgentResolutionOperation } from '../agent-launch/structured-agent-command-resolution' -import type { AgentSessionAccountHome } from '../../../shared/agent-session/lifecycle/agent-session-account-home' import type { PrioritySemaphore } from '../../../shared/priority-semaphore' import { startSpan } from '../../observability/tracer' import { @@ -12,10 +11,7 @@ import type { AgentModelCatalogSource, AgentModelCatalogSuccess } from './agent-model-catalog-entry' -import type { - AgentModelCatalogProbe, - AgentModelCatalogResolvedProbe -} from './agent-model-catalog-discovery' +import type { AgentModelCatalogProgress } from './agent-model-catalog-discovery' import type { AgentModelCatalogStore } from './agent-model-catalog-store' /** Pin listings to the spawned child's account so later selections cannot receive its results. */ @@ -25,26 +21,25 @@ export type AgentModelCatalogSessionAccess = { accountHomePath: string } -/** `owner` is the listing's own operation: its fixed budget, never a reader's. */ +/** The owner outlives readers and fences all observations from this listing. */ export type AgentModelCatalogListModels = ( selectedModels: readonly string[] | undefined, - owner: StructuredAgentResolutionOperation + owner: StructuredAgentResolutionOperation, + publish?: AgentModelCatalogProgress ) => Promise /** Covers the gap between one waiting read and the next, so a following reader keeps its place. */ const AGENT_MODEL_CATALOG_RELEASE_GRACE_MS = 5_000 -/** Queued listings need a holder; admitted probes run to their own timeout. */ +/** Queued listings need a holder; admitted probes run until completion or retirement. */ export type AgentModelCatalogListingBudget = { - /** Expiry withdraws it from admission, stops its process and drops its late answer. */ + /** Queue expiry withdraws unadmitted work only. */ deadline?: number /** This caller's interest; a queued listing is withdrawn a grace after no holder is left. */ signal?: AbortSignal /** Service shutdown retires the listing immediately, without the holder release grace. */ lifetime?: AbortSignal admission?: PrioritySemaphore - /** Execution time after admission. */ - timeoutMs?: number } export type InFlightListing = { @@ -53,6 +48,7 @@ export type InFlightListing = { current: () => boolean /** Adds a queued holder until its signal aborts; admitted probes need none. */ hold: (signal: AbortSignal | undefined) => void + flush: (model?: string) => void promise: Promise finish: (entry: AgentModelCatalogEntry | null) => void cleanup: () => void @@ -64,7 +60,11 @@ export function demandAgentModelEfforts( cap: number ): void { if (model && flight && flight.targets.size < cap) { + const fresh = !flight.targets.has(model) flight.targets.add(model) + if (fresh) { + flight.flush(model) + } } } @@ -96,15 +96,19 @@ export function refreshAgentModelCatalogListing(input: { refreshes: Map failures: AgentModelCatalogFailures listingWaiters: AgentModelCatalogListingWaiters + failed: () => void writeSuccess: ( fingerprint: string, agent: string, success: AgentModelCatalogSuccess, - source: AgentModelCatalogSource + source: AgentModelCatalogSource, + settled?: boolean, + accountKnown?: boolean, + unchanged?: boolean ) => AgentModelCatalogEntry | null }): Promise { const { fingerprint, agent, listModels } = input - const { signal, lifetime, admission, timeoutMs, deadline: budgetDeadline } = input.budget ?? {} + const { signal, lifetime, admission, deadline: budgetDeadline } = input.budget ?? {} let deadline = budgetDeadline const existing = input.refreshes.get(fingerprint) if (existing) { @@ -117,6 +121,8 @@ export function refreshAgentModelCatalogListing(input: { let expiry: ReturnType | undefined const holders = new Map void>() // A listing started without a signal is held until it settles. + let admitted = false + const queuedAt = Date.now() let held = signal === undefined const setDeadline = (next: number | undefined): void => { deadline = next @@ -125,10 +131,12 @@ export function refreshAgentModelCatalogListing(input: { expiry = setTimeout(retire, Math.max(0, deadline - Date.now())) } } + let flush: (model?: string) => void = () => {} const flight: InFlightListing = { agent, targets: new Set(input.selectedModel ? [input.selectedModel] : []), - current: () => !lifetime?.aborted && (deadline === undefined || Date.now() < deadline), + current: () => + !lifetime?.aborted && (admitted || deadline === undefined || Date.now() < deadline), hold: (holder) => { if (!held && holder && !holder.aborted && !holders.has(holder)) { setDeadline(budgetDeadline) @@ -147,6 +155,7 @@ export function refreshAgentModelCatalogListing(input: { holder.addEventListener('abort', abandon, { once: true }) } }, + flush: (model) => flush(model), promise: result.promise, finish: result.resolve, cleanup: () => { @@ -171,51 +180,114 @@ export function refreshAgentModelCatalogListing(input: { const span = startSpan('agentModelCatalog.discovery', { attributes: { agent, lister: 'probe' } }) void Promise.resolve().then(async () => { let release: (() => void) | undefined + let entry: AgentModelCatalogEntry | null = null + let lastSuccess: AgentModelCatalogSuccess | undefined + let lastAccountKnown = false + let published: string | undefined + let deferred: + | { read: () => AgentModelCatalogSuccess; known: (model: string) => boolean } + | undefined + const publish = (success: AgentModelCatalogSuccess, accountKnown = false, settled = false) => { + if (!current() || stopped.signal.aborted || success.models.length === 0) { + return + } + const key = JSON.stringify([success, accountKnown]) + if (key === published) { + if (settled) { + entry = input.writeSuccess( + fingerprint, + agent, + success, + 'discovery', + true, + accountKnown, + true + ) + } + return + } + deferred = undefined + published = key + lastSuccess = success + lastAccountKnown = accountKnown + entry = input.writeSuccess(fingerprint, agent, success, 'discovery', settled, accountKnown) + input.listingWaiters.notify(fingerprint) + } + const progress: AgentModelCatalogProgress = Object.assign(publish, { + defer: (read: () => AgentModelCatalogSuccess, known: (model: string) => boolean) => { + if (current() && !stopped.signal.aborted) { + deferred = { read, known } + } + }, + demanded: () => flight.targets + }) + flush = (model) => { + if (!current() || stopped.signal.aborted || !deferred || (model && !deferred.known(model))) { + return + } + const observation = deferred + deferred = undefined + publish(observation.read(), true) + } try { release = await admission?.acquire(0, stopped.signal) if (!current()) { span.end() return } + admitted = true + span.setAttribute('queuedMs', Date.now() - queuedAt) held = true - // Admission replaces the abandonment deadline with the probe's own execution budget. - setDeadline( - timeoutMs === undefined - ? budgetDeadline - : Math.min(budgetDeadline ?? Infinity, Date.now() + timeoutMs) - ) + setDeadline(undefined) const owner: StructuredAgentResolutionOperation = { - current, - signal: stopped.signal, - deadline + current: () => current() && !stopped.signal.aborted, + signal: stopped.signal } - let targets = input.selectedModel ? [input.selectedModel] : undefined - let entry: AgentModelCatalogEntry | null = null + let targets = flight.targets.size > 0 ? [...flight.targets] : undefined + const tried = new Set() do { - const success = await listModels(targets, owner) + for (const target of targets ?? []) { + tried.add(target) + } + const success = await listModels(targets, owner, progress) if (!current()) { return } + deferred = undefined span.setAttribute('models', success.models.length) - entry = input.writeSuccess(fingerprint, agent, success, 'discovery') - const attempted = new Set(targets ? (success.attemptedModels ?? targets) : []) - for (const target of targets ?? []) { - if (attempted.has(target)) { - input.targetAttempted(target) - } + publish(success, lastSuccess ? lastAccountKnown : true, true) + const attempted = new Set(success.attemptedModels ?? targets ?? []) + for (const target of attempted) { + tried.add(target) + input.targetAttempted(target) + } + for (const target of flight.targets) { if (attempted.has(target) || !input.targetDue(target)) { flight.targets.delete(target) } } - targets = [...flight.targets].filter(input.targetDue) - } while (targets.length > 0 && current()) + targets = [...flight.targets].filter( + (target) => !tried.has(target) && input.targetDue(target) + ) + } while (targets.length > 0 && current() && !stopped.signal.aborted) if (current()) { flight.finish(entry) } span.end() } catch (error) { span.fail(error instanceof Error ? error : String(error)) - if (current() && !(error instanceof AgentModelCatalogListingStoppedError)) { + if ( + current() && + !stopped.signal.aborted && + !(error instanceof AgentModelCatalogListingStoppedError) + ) { + flush() + if (entry && lastSuccess) { + publish({ ...lastSuccess, complete: false }, lastAccountKnown, true) + flight.finish(entry) + return + } + input.failed() input.failures.probeFailed(fingerprint, agent, error) } } finally { @@ -225,45 +297,3 @@ export function refreshAgentModelCatalogListing(input: { }) return flight.promise } - -export function scopedAgentModelCatalogLister(input: { - legacy?: boolean - signal: AbortSignal - resolved: AgentModelCatalogResolvedProbe | undefined - probe: AgentModelCatalogProbe | undefined - probeHome: AgentSessionAccountHome | null - workspacePath: string | undefined -}): AgentModelCatalogListModels { - const { legacy, resolved, probe, probeHome, workspacePath } = input - return async (selectedModels, operation) => { - const signal = operation.signal - ? AbortSignal.any([input.signal, operation.signal]) - : input.signal - const deadline = new AbortController() - // Share the retry budget and await bounded provider teardown before settling. - const timer = legacy ? setTimeout(() => deadline.abort(), 30_000) : undefined - const attemptSignal = AbortSignal.any([signal, deadline.signal]) - try { - // A slot that frees as the budget ends never starts the queued listing. - if (operation.current?.() === false) { - throw new AgentModelCatalogListingStoppedError('listing budget ended') - } - if (resolved) { - return await resolved.run(attemptSignal, operation) - } - if (!probe || !probeHome) { - throw new AgentModelCatalogListingStoppedError('catalog scope unavailable') - } - return await probe(probeHome, { - signal: attemptSignal, - workspacePath, - ...(selectedModels ? { selectedModels } : {}), - operation - }) - } catch (error) { - throw signal.aborted ? new AgentModelCatalogListingStoppedError(String(error)) : error - } finally { - clearTimeout(timer) - } - } -} diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-listing-waiters.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-listing-waiters.ts index c88765eacf8..a0f7ef5c45b 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-listing-waiters.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-listing-waiters.ts @@ -1,10 +1,6 @@ import type { AgentModelCatalogStore } from './agent-model-catalog-store' import type { AgentModelCatalogReadParams } from './agent-model-catalog-service-contract' import type { AgentModelCatalogBinding } from './agent-model-catalog-snapshot' -import { - independentAgentModelCatalog, - usesModelSpecificAcpEfforts -} from '../../../shared/agent-session/options/agent-model-catalog-ownership' import { withTimeout } from '../../../shared/promise-timeout-fallback' import type { AgentModelCatalogEntry } from './agent-model-catalog-entry' import type { AgentModelCatalogRefresh } from './agent-model-catalog-refresh' @@ -25,12 +21,13 @@ export class AgentModelCatalogListingWaiters { fingerprint: string, waitForChange = false, terminal = false, - signal?: AbortSignal + signal?: AbortSignal, + baseline?: AgentModelCatalogEntry | null ): Promise | null { if (!this.listing(fingerprint) || signal?.aborted) { return null } - const previous = this.read(fingerprint) + const previous = baseline === undefined ? this.read(fingerprint) : baseline return new Promise((resolve) => { const waiters = this.waiters.get(fingerprint) ?? new Set<() => void>() let settled = false @@ -99,29 +96,35 @@ export async function waitForAgentModelCatalogRead(input: { params: AgentModelCatalogReadParams reader: { deadline: number; signal: AbortSignal } store: AgentModelCatalogStore - refresh: AgentModelCatalogRefresh | undefined + refresh: () => AgentModelCatalogRefresh | undefined binding: () => AgentModelCatalogBinding | undefined }): Promise { - const { params, store, refresh } = input + const { params, store } = input // Once the reader stops waiting, nothing it left behind subscribes to or holds a listing wait. const left = new AbortController() + const original = input.binding() + const baseline = original ? store.get(original.fingerprint) : null + const refresh = input.refresh() const wait = async (): Promise => { await refresh?.listing - await (refresh?.defaults ?? input.binding()?.projecting) const bound = left.signal.aborted ? undefined : input.binding() - if ( - bound && - (!store.get(bound.fingerprint) || - independentAgentModelCatalog(params.agent) || - (usesModelSpecificAcpEfforts(params.agent) && store.isListing(bound.fingerprint)) || - store.heldReasonDue(bound.fingerprint)) - ) { - await store.pendingListing( + if (bound) { + const entry = store.get(bound.fingerprint) + if (params.progress !== false && entry && entry !== baseline) { + return + } + const pending = store.pendingListing( bound.fingerprint, true, - params.legacy || usesModelSpecificAcpEfforts(params.agent), - left.signal + params.progress === false, + left.signal, + baseline ) + const projection = + params.progress === false && store.isListing(bound.fingerprint) + ? undefined + : bound.projecting + await (pending && projection ? Promise.race([pending, projection]) : (pending ?? projection)) } if (bound && !store.isListing(bound.fingerprint)) { await refresh?.done diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-lookup.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-lookup.ts index c887e78f97a..9bbd4c45abb 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-lookup.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-lookup.ts @@ -54,6 +54,7 @@ export function createAgentModelCatalogLookup( return undefined } const refresh = refreshes.join(found.key, found.launch) + refresh.hold(operation) refresh.want( usesModelSpecificAcpEfforts(params.agent) ? (params.selectedModel ?? diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-probe-runner.test.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-probe-runner.test.ts index 11c34b40566..9dc30f878c5 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-probe-runner.test.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-probe-runner.test.ts @@ -168,6 +168,46 @@ describe('the shared catalog probe runner', () => { expect(failure).toMatchObject({ reason: 'spawn', executableMissing: true }) }) + it('returns a ready partial list on work expiry, awaits close, and drops it on cancellation', async () => { + for (const expiry of ['body', 'owner', 'cancel'] as const) { + const stop = new AbortController() + const closing = Promise.withResolvers() + const beganClose = Promise.withResolvers() + const run = runAgentModelCatalogSession( + () => ({ + close: () => { + beganClose.resolve() + return closing.promise + } + }), + async (_connection, ready) => { + ready( + async () => {}, + () => ['usable-model'] + ) + return new Promise(() => {}) + }, + { label: 'partial', signal: stop.signal, timeoutMs: expiry === 'body' ? 20 : 60_000 } + ) + const observed = run.then( + (value) => value, + (error: unknown) => error + ) + if (expiry !== 'body') { + stop.abort( + expiry === 'owner' ? new AgentModelCatalogProbeError('deadline', 'timeout') : undefined + ) + } + await beganClose.promise + closing.resolve() + if (expiry === 'cancel') { + expect(await observed).toMatchObject({ reason: 'stopped' }) + } else { + expect(await observed).toEqual(['usable-model']) + } + } + }) + it('closes a probe connection on success, failure and timeout alike', async () => { const closes: string[] = [] const open = (name: string) => () => ({ diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-probe-runner.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-probe-runner.ts index d65011427c1..f8576dfc81d 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-probe-runner.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-probe-runner.ts @@ -1,5 +1,6 @@ import type { ClaudeBackgroundFailedExit } from '../../../shared/claude-background-run-isolation' import type { PipedProcessSpawner } from '@orca/process-host/process-spec' +import { StringDecoder } from 'node:string_decoder' import { withTimeout } from '../../../shared/promise-timeout-fallback' import { createOutputSink } from '@orca/process-host/bounded-output-sink' @@ -53,7 +54,7 @@ export type AgentModelCatalogListingLaunch = ProviderProcessLaunch & { export type AgentModelCatalogRunnerOptions = { site: string - timeoutMs?: number + timeoutMs?: number | null maxOutputBytes?: number signal?: AbortSignal spawnImpl?: PipedProcessSpawner @@ -61,6 +62,8 @@ export type AgentModelCatalogRunnerOptions = { inheritedEnv?: NodeJS.ProcessEnv /** Finish required replies even when an optional control request never completes. */ partialOutput?: { ready: (stdout: string) => boolean; timeoutMs: number } + onOutput?: (chunk: string) => void + completeOutput?: () => boolean } /** Bound the listing and await child teardown before success, failure or cancellation settles. */ @@ -68,7 +71,8 @@ export async function runAgentModelCatalogListing( launch: AgentModelCatalogListingLaunch, options: AgentModelCatalogRunnerOptions ): Promise { - const timeoutMs = options.timeoutMs ?? AGENT_MODEL_CATALOG_PROBE_TIMEOUT_MS + const timeoutMs = + options.timeoutMs === undefined ? AGENT_MODEL_CATALOG_PROBE_TIMEOUT_MS : options.timeoutMs throwIfStopped(options.signal, launch.command) const managed = spawnManagedProviderProcess(launch, { site: options.site, @@ -79,6 +83,7 @@ export async function runAgentModelCatalogListing( ...(options.platform ? { platform: options.platform } : {}), ...(options.inheritedEnv ? { inheritedEnv: options.inheritedEnv } : {}) }) + const progressDecoder = options.onOutput ? new StringDecoder('utf8') : undefined const { child } = managed const output = createOutputSink( options.maxOutputBytes ?? AGENT_MODEL_CATALOG_PROBE_MAX_OUTPUT_BYTES @@ -106,13 +111,22 @@ export async function runAgentModelCatalogListing( // Required replies already in hand outlive an optional reply that runs out of time or room. const failUnlessReady = (message: string, reason: AgentModelCatalogProbeFailure): void => partialTimer === undefined ? fail(message, reason) : resolve(output.text()) - onAbort = () => fail(`${launch.command} listing stopped`, 'stopped') + onAbort = () => + isAgentModelCatalogWorkTimeout(options.signal) + ? failUnlessReady(`${launch.command} listing timed out`, 'timeout') + : fail(`${launch.command} listing stopped`, 'stopped') options.signal?.addEventListener('abort', onAbort, { once: true }) - timer = setTimeout( - () => - failUnlessReady(`${launch.command} did not list models within ${timeoutMs}ms`, 'timeout'), - timeoutMs - ) + timer = + timeoutMs === null + ? undefined + : setTimeout( + () => + failUnlessReady( + `${launch.command} did not list models within ${timeoutMs}ms`, + 'timeout' + ), + timeoutMs + ) child.stdout.on('data', (chunk: Buffer | string) => { output.write(chunk) if (output.truncated()) { @@ -120,8 +134,24 @@ export async function runAgentModelCatalogListing( `${launch.command} listed more than the model catalog reads`, 'output-overflow' ) - } else if (partialTimer === undefined && options.partialOutput?.ready(output.text())) { - partialTimer = setTimeout(() => resolve(output.text()), options.partialOutput.timeoutMs) + } else { + try { + if (progressDecoder) { + options.onOutput?.( + progressDecoder.write(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)) + ) + } + if (options.completeOutput?.()) { + resolve(output.text()) + } else if (partialTimer === undefined && options.partialOutput?.ready(output.text())) { + partialTimer = setTimeout( + () => resolve(output.text()), + options.partialOutput.timeoutMs + ) + } + } catch (error) { + reject(error) + } } }) child.stdout.on('error', () => {}) @@ -167,41 +197,61 @@ function throwIfStopped(signal: AbortSignal | undefined, label: string): void { } } +export function isAgentModelCatalogWorkTimeout(signal?: AbortSignal): boolean { + return ( + signal?.aborted === true && + signal.reason instanceof AgentModelCatalogProbeError && + signal.reason.reason === 'timeout' + ) +} + +export type AgentModelCatalogReady = (cleanup: () => Promise, partial?: () => T) => void + export type AgentModelCatalogProbeConnection = { close(): Promise } export async function runAgentModelCatalogSession( open: () => C, - body: (connection: C, ready: (cleanup: () => Promise) => void) => Promise, - options: { label: string; timeoutMs?: number; signal?: AbortSignal } + body: (connection: C, ready: AgentModelCatalogReady) => Promise, + options: { label: string; timeoutMs?: number | null; signal?: AbortSignal } ): Promise { - const timeoutMs = options.timeoutMs ?? AGENT_MODEL_CATALOG_PROBE_TIMEOUT_MS + const timeoutMs = + options.timeoutMs === undefined ? AGENT_MODEL_CATALOG_PROBE_TIMEOUT_MS : options.timeoutMs throwIfStopped(options.signal, options.label) const connection = open() let timer: ReturnType | undefined let cleanup: (() => Promise) | undefined - const ready = (closeSession: () => Promise): void => { - cleanup = closeSession - clearTimeout(timer) + let partial: (() => T) | undefined + let settled = false + const ready: AgentModelCatalogReady = (closeSession, readPartial) => { + if (!settled) { + cleanup = closeSession + partial = readPartial + } } let onAbort = (): void => {} - const deadline = new Promise((_resolve, reject) => { - onAbort = () => - reject(new AgentModelCatalogProbeError(`${options.label} listing stopped`, 'stopped')) + const deadline = new Promise((resolve, reject) => { + const expire = (): void => { + settled = true + if (partial) { + resolve(partial()) + } else { + reject(new AgentModelCatalogProbeError(`${options.label} listing timed out`, 'timeout')) + } + } + onAbort = () => { + if (isAgentModelCatalogWorkTimeout(options.signal)) { + expire() + } else { + reject(new AgentModelCatalogProbeError(`${options.label} listing stopped`, 'stopped')) + } + } options.signal?.addEventListener('abort', onAbort, { once: true }) - timer = setTimeout( - () => - reject( - new AgentModelCatalogProbeError( - `${options.label} did not list models within ${timeoutMs}ms`, - 'timeout' - ) - ), - timeoutMs - ) + timer = timeoutMs === null ? undefined : setTimeout(expire, timeoutMs) }) try { return await Promise.race([body(connection, ready), deadline]) } finally { + settled = true clearTimeout(timer) options.signal?.removeEventListener('abort', onAbort) if (cleanup) { diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-refresh.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-refresh.ts index 202174b0d1b..d8593cba732 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-refresh.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-refresh.ts @@ -1,3 +1,4 @@ +import { resolveAgentModelCatalogAccount } from './agent-model-catalog-account-resolution' import type { PrioritySemaphore } from '../../../shared/priority-semaphore' import { independentAgentModelCatalog } from '../../../shared/agent-session/options/agent-model-catalog-ownership' import type { @@ -5,16 +6,12 @@ import type { AgentModelCatalogServiceDeps } from './agent-model-catalog-service-contract' import type { AgentModelCatalogReadRecords } from './agent-model-catalog-running-account' -import { recordCatalogKey } from './agent-model-catalog-fingerprint' -import { newChatCatalogKey } from './agent-model-catalog-prewarm' import { agentModelCatalogDefaultFollowsAccount, - agentModelCatalogListedDefault, - catalogFolder, - sessionWorkspacePath + agentModelCatalogListedDefault } from './agent-model-catalog-workspace' import { resolveAgentModelCatalogScope } from './agent-model-catalog-scope' -import { scopedAgentModelCatalogLister } from './agent-model-catalog-lister' +import { scopedAgentModelCatalogLister } from './agent-model-catalog-scoped-lister' import { seedAccountList } from './agent-model-catalog-account-list' import type { AgentModelCatalogEntry } from './agent-model-catalog-entry' import { @@ -22,10 +19,7 @@ import { bindAgentModelCatalogScope, type AgentModelCatalogBinding } from './agent-model-catalog-snapshot' -import { AGENT_MODEL_CATALOG_PICKER_WAIT_MS } from './agent-model-catalog-listing-waiters' - -/** Bounds scope resolution and the folder's settings; listings own their execution budget. */ -const AGENT_MODEL_CATALOG_REFRESH_MS = AGENT_MODEL_CATALOG_PICKER_WAIT_MS +import type { AgentModelCatalogReadOperation } from './agent-model-catalog-operation' /** What a refresh resolves, captured when it starts; no reader's session, deadline or callback. */ export type AgentModelCatalogLaunch = { @@ -48,6 +42,7 @@ export type AgentModelCatalogRefresh = { defaults: Promise /** Its listing ingested and the account's list seeded, or it ended. */ done: Promise + hold: (reader: AgentModelCatalogReadOperation) => void /** Also lists efforts for a reader's selected model. */ want: (model: string | undefined) => void } @@ -55,7 +50,7 @@ export type AgentModelCatalogRefresh = { type Held = AgentModelCatalogRefresh & { workspaceId?: string; end: () => void } /** Equivalent requests share one refresh that belongs to no reader. It ends when it finishes, at - * its setup budget, on its agent's or workspace's retirement, or with the service; readers only + * on its agent's or workspace's retirement, or with the service; readers only * wait on it, each until its own deadline or cancellation, and never extend or end it. */ export function createAgentModelCatalogRefreshes(context: { deps: AgentModelCatalogServiceDeps @@ -70,16 +65,11 @@ export function createAgentModelCatalogRefreshes(context: { const start = (key: string, launch: AgentModelCatalogLaunch): Held => { const { agent } = launch const retirement = deps.store.retirementRevision(agent) - const deadline = Date.now() + AGENT_MODEL_CATALOG_REFRESH_MS const ended = new AbortController() - // Retiring its agent ends it at once, like its budget, its workspace or shutdown. + // Scope work belongs to the host, independent of reader deadlines. const signal = AbortSignal.any([lifetime, ended.signal, deps.store.retirements.signal(agent)]) const current = (): boolean => - !signal.aborted && - Date.now() < deadline && - deps.store.retirementRevision(agent) === retirement - const timer = setTimeout(() => ended.abort(), AGENT_MODEL_CATALOG_REFRESH_MS) - timer.unref?.() + !signal.aborted && deps.store.retirementRevision(agent) === retirement const gone = new Promise((resolve) => { signal.addEventListener('abort', () => resolve(null), { once: true }) }) @@ -95,10 +85,12 @@ export function createAgentModelCatalogRefreshes(context: { const listing = Promise.withResolvers() const defaults = Promise.withResolvers() const targets = new Set() - let list: ((model: string | undefined) => void) | undefined + const holders = new Set() + let register: ((holder?: AbortSignal) => void) | undefined + let demand: ((model: string) => void) | undefined let projecting: Promise | undefined const run = async (): Promise => { - const facts = await bounded(resolveAccount(deps, launch, current)) + const facts = await bounded(resolveAgentModelCatalogAccount(deps, launch, current)) if (!facts) { return } @@ -135,6 +127,7 @@ export function createAgentModelCatalogRefreshes(context: { if (keepDefault !== null) { bound.keepDefault = keepDefault } + bound.projecting = undefined }) projecting = bound.projecting void projecting.finally(defaults.resolve) @@ -146,7 +139,7 @@ export function createAgentModelCatalogRefreshes(context: { base, probeHome, current, - { deadline, current, signal } + { current, signal } ) ) if (!scope) { @@ -171,43 +164,60 @@ export function createAgentModelCatalogRefreshes(context: { probeHome, workspacePath: cwd ?? folder }) - list = (model) => { - if ( - current() && - (deps.store.probeDue(fingerprint) || - deps.store.efforts.due(fingerprint, model) || - deps.store.isListing(fingerprint)) + const attach = (holder: AbortSignal) => { + if (holder.aborted || !current()) { + return + } + completion = deps.store.refresh( + fingerprint, + agent, + (selected, owner, publish) => { + const task = lister(selected, owner, publish) + tasks.add(task) + void task.finally(() => tasks.delete(task)).catch(() => {}) + return task + }, + { signal: holder, lifetime, admission } + ) + } + register = (holder) => { + if (!current()) { + return + } + if (deps.store.isListing(fingerprint)) { + if (holder) { + attach(holder) + } else { + for (const live of holders) { + attach(live) + } + } + } else if ( + deps.store.probeDue(fingerprint) || + [...targets].some((model) => deps.store.efforts.due(fingerprint, model)) ) { - // This refresh holds the listing while it runs; once admitted, the listing runs on. - completion = deps.store.refresh( - fingerprint, - agent, - (selected, owner) => { - const task = lister(selected, owner) - tasks.add(task) - void task.finally(() => tasks.delete(task)).catch(() => {}) - return task - }, - { - signal, - lifetime, - ...(launch.legacy - ? { deadline } - : { admission, timeoutMs: AGENT_MODEL_CATALOG_REFRESH_MS }) - }, - model - ) + for (const live of holders) { + attach(live) + } + for (const model of targets) { + deps.store.efforts.demand(fingerprint, model) + } } } - for (const model of targets.size > 0 ? [...targets] : [undefined]) { - list(model) + demand = (model) => { + if (!deps.store.isListing(fingerprint)) { + register?.() + } else { + deps.store.efforts.demand(fingerprint, model) + } } + register() } listing.resolve() if (launch.passive) { await bound.projecting } else { - // Discovery may finish after this refresh's setup budget ends. + // Readers can leave while current discovery continues. const seeded = Promise.resolve(completion).then((entry) => { if (entry !== null && bound.current()) { seedAccountList(deps, scope, agent) @@ -219,13 +229,31 @@ export function createAgentModelCatalogRefreshes(context: { const end = (): void => ended.abort() const held: Held = { current: () => current() && refreshes.get(key) === held, + hold: (reader) => { + if (reader.signal.aborted) { + return + } + const released = new AbortController() + const timer = + reader.deadline === undefined + ? undefined + : setTimeout(() => released.abort(), Math.max(0, reader.deadline - Date.now())) + const holder = AbortSignal.any([signal, reader.signal, released.signal]) + const leave = () => { + clearTimeout(timer) + holders.delete(holder) + } + holders.add(holder) + holder.addEventListener('abort', leave, { once: true }) + register?.(holder) + }, listing: listing.promise, defaults: defaults.promise, done: Promise.resolve(), want: (model) => { if (model && !targets.has(model)) { targets.add(model) - list?.(model) + demand?.(model) } }, workspaceId: launch.records.record?.location.workspaceId, @@ -235,11 +263,10 @@ export function createAgentModelCatalogRefreshes(context: { held.done = run() .catch(() => {}) .finally(() => listing.resolve()) - // The folder's settings may still arrive, inside the same budget, after the listing ends. + // The folder's settings may still arrive after the listing ends. const settled = held.done .then(() => projecting) .finally(() => { - clearTimeout(timer) ended.abort() defaults.resolve() if (refreshes.get(key) === held) { @@ -267,43 +294,3 @@ export function createAgentModelCatalogRefreshes(context: { } } } - -async function resolveAccount( - deps: AgentModelCatalogServiceDeps, - launch: AgentModelCatalogLaunch, - current: () => boolean -) { - const { agent, records } = launch - const { record, scoped } = records - const workspacePath = - launch.inSessionWorkspace || - (record && - launch.workspacePath === undefined && - !launch.legacy && - (independentAgentModelCatalog(agent) || deps.agentReadsProjectModelConfig?.(agent) === true)) - ? record - ? await sessionWorkspacePath(deps, record) - : null - : launch.workspacePath - if (!current()) { - return null - } - const folder = await catalogFolder(deps, agent, workspacePath, scoped) - if (!current()) { - return null - } - const account = scoped - ? recordCatalogKey(scoped, folder) - : await newChatCatalogKey(deps, agent, folder) - if (!current() || !account) { - return null - } - return { - record, - scoped, - workspacePath, - folder, - base: account.fingerprint, - probeHome: account.accountHome - } -} diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-scope.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-scope.ts index 90a4cedaef7..94a3615bfee 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-scope.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-scope.ts @@ -34,7 +34,7 @@ export async function resolveAgentModelCatalogScope( if (!current()) { return null } - if (!independentAgentModelCatalog(params.agent) || !home) { + if (!home) { return { base, fingerprint: base, exact: false } } if (record && !isNativeLocalLocation(record.location)) { @@ -43,15 +43,26 @@ export async function resolveAgentModelCatalogScope( const probe = deps.probes?.[params.agent] // Fixed when its readers asked: no chat closing later turns a shared refresh passive. const savedOnly = () => !!params.savedOnly - if (params.legacy) { + if (params.legacy && independentAgentModelCatalog(params.agent)) { const fingerprint = agentModelCatalogScopeFingerprint(base, 'legacy-account') - const resolved = - savedOnly() || !deps.store.probeDue(fingerprint) - ? null - : await probe?.resolveLegacy?.(home, operation).catch(() => null) + let resolved = await probe + ?.resolveLegacy?.(home, operation, savedOnly() ? 'saved' : 'snapshot') + .catch(() => null) if (!current()) { return null } + if (resolved?.inputStamp !== undefined) { + deps.store.observeInputs(fingerprint, resolved.inputStamp) + } + if (!savedOnly() && deps.store.probeDue(fingerprint)) { + resolved = await probe?.resolveLegacy?.(home, operation, 'recover').catch(() => null) + } + if (!current()) { + return null + } + if (resolved?.inputStamp !== undefined) { + deps.store.observeInputs(fingerprint, resolved.inputStamp) + } return { base, fingerprint, @@ -59,6 +70,29 @@ export async function resolveAgentModelCatalogScope( ...(resolved ? { resolved } : {}) } } + if (!independentAgentModelCatalog(params.agent)) { + const cwd = + params.workspacePath ?? (record ? await sessionWorkspacePath(deps, record) : undefined) + let resolved = await probe + ?.resolve?.(home, cwd ?? '', savedOnly() ? 'saved' : 'snapshot', operation) + .catch(() => null) + if (!current()) { + return null + } + if (resolved?.inputStamp !== undefined) { + deps.store.observeInputs(base, resolved.inputStamp) + } + if (!savedOnly() && deps.store.probeDue(base)) { + resolved = await probe?.resolve?.(home, cwd ?? '', 'recover', operation).catch(() => null) + } + if (!current()) { + return null + } + if (resolved?.inputStamp !== undefined) { + deps.store.observeInputs(base, resolved.inputStamp) + } + return { base, fingerprint: base, exact: false, ...(resolved ? { resolved } : {}) } + } // The list belongs to the account and the CLI a chat here would launch, not to this folder: // resolve the executable where the chat runs, but list from an empty Orca-owned folder so the // listing reads only account settings (a home folder can hold another account's settings as @@ -90,6 +124,9 @@ export async function resolveAgentModelCatalogScope( if (!current()) { return null } + if (resolved?.inputStamp !== undefined) { + deps.store.observeInputs(scopeKey(resolved), resolved.inputStamp) + } // Rerun shells only for reads due to list; saved-only reads must remain passive. if (!savedOnly() && (!resolved || deps.store.probeDue(scopeKey(resolved)))) { resolved = await resolve('recover') @@ -97,6 +134,9 @@ export async function resolveAgentModelCatalogScope( if (!current() || (probe?.resolve && !resolved)) { return null } + if (resolved?.inputStamp !== undefined) { + deps.store.observeInputs(scopeKey(resolved), resolved.inputStamp) + } return { base, fingerprint: scopeKey(resolved), diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-scoped-lister.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-scoped-lister.ts new file mode 100644 index 00000000000..424b3a0a33b --- /dev/null +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-scoped-lister.ts @@ -0,0 +1,48 @@ +import type { AgentSessionAccountHome } from '../../../shared/agent-session/lifecycle/agent-session-account-home' +import type { + AgentModelCatalogProbe, + AgentModelCatalogResolvedProbe +} from './agent-model-catalog-discovery' +import type { AgentModelCatalogListModels } from './agent-model-catalog-lister' +import { AgentModelCatalogListingStoppedError } from './agent-model-catalog-failures' + +export function scopedAgentModelCatalogLister(input: { + legacy?: boolean + signal: AbortSignal + resolved: AgentModelCatalogResolvedProbe | undefined + probe: AgentModelCatalogProbe | undefined + probeHome: AgentSessionAccountHome | null + workspacePath: string | undefined +}): AgentModelCatalogListModels { + const { resolved, probe, probeHome, workspacePath } = input + return async (selectedModels, operation, publish) => { + const signal = operation.signal + ? AbortSignal.any([input.signal, operation.signal]) + : input.signal + try { + // A slot that frees as the budget ends never starts the queued listing. + if (operation.current?.() === false) { + throw new AgentModelCatalogListingStoppedError('listing budget ended') + } + if (resolved) { + return await resolved.run(signal, operation, selectedModels, publish) + } + if (!probe || !probeHome) { + throw new AgentModelCatalogListingStoppedError('catalog scope unavailable') + } + return await probe(probeHome, { + signal, + workspacePath, + ...(selectedModels ? { selectedModels } : {}), + operation, + publish + }) + } catch (error) { + throw input.signal.aborted + ? new AgentModelCatalogListingStoppedError(String(error)) + : operation.signal?.aborted + ? operation.signal.reason + : error + } + } +} diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-script-entry.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-script-entry.ts new file mode 100644 index 00000000000..cfb6a4be997 --- /dev/null +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-script-entry.ts @@ -0,0 +1,104 @@ +import { NODE_INTERPRETER_OPTIONS_WITH_VALUE } from '../../../shared/node-interpreter-options' + +const NODE_FLAGS = new Set([ + '--no-warnings', + '--trace-warnings', + '--enable-source-maps', + '--preserve-symlinks', + '--preserve-symlinks-main' +]) +const PYTHON_FLAGS = new Set(['-u', '-B', '-E', '-s', '-S', '-I', '-O', '-OO', '-q']) +const PYTHON_VALUES = new Set(['-W', '-X']) +const DENO_FLAGS = new Set(['-A', '--allow-all', '--no-prompt']) +const DENO_VALUES = new Set(['-c', '--config', '--import-map']) +const DENO_PERMISSIONS = new Set([ + '--allow-read', + '--allow-write', + '--allow-net', + '--allow-env', + '--allow-run', + '--allow-ffi', + '--allow-sys', + '--allow-import', + '--deny-read', + '--deny-write', + '--deny-net', + '--deny-env', + '--deny-run', + '--deny-ffi', + '--deny-sys' +]) + +function scriptFile(token: string | undefined, deno = false): string | undefined { + if (!token || token === '-') { + return undefined + } + return deno && /^[a-z][a-z\d+.-]*:/i.test(token) && !/^[a-z]:[\\/]/i.test(token) + ? undefined + : token +} + +/** Unknown option arity cannot safely identify a file; those commands use explicit Refresh. */ +export function agentModelCatalogScriptEntry( + interpreter: string, + args: readonly string[] +): string | undefined { + const node = /^(node|nodejs)$/.test(interpreter) + const python = /^python\d*(?:\.\d+)?$/.test(interpreter) + const deno = interpreter === 'deno' + if (!node && !python && !deno) { + if (!/^(bun|ruby|sh|bash|zsh)$/.test(interpreter)) { + return undefined + } + const operand = interpreter === 'bun' && args[0] === 'run' ? args[1] : args[0] + return operand?.startsWith('-') || (interpreter === 'bun' && operand === 'eval') + ? undefined + : scriptFile(operand) + } + if (deno && args[0] !== 'run') { + return undefined + } + const flags = node ? NODE_FLAGS : python ? PYTHON_FLAGS : DENO_FLAGS + const values = node ? NODE_INTERPRETER_OPTIONS_WITH_VALUE : python ? PYTHON_VALUES : DENO_VALUES + for (let index = deno ? 1 : 0; index < args.length; index += 1) { + const token = args[index]! + if (!token) { + return undefined + } + if (token === '--') { + return scriptFile(args[index + 1], deno) + } + if (!token.startsWith('-')) { + return node && token === 'inspect' ? undefined : scriptFile(token, deno) + } + if (flags.has(token)) { + continue + } + if (values.has(token)) { + const value = args[++index] + if (!value || value.startsWith('-')) { + return undefined + } + continue + } + const equals = token.indexOf('=') + const name = equals === -1 ? token : token.slice(0, equals) + if (token.startsWith('--') && values.has(name) && equals !== -1) { + if (equals === token.length - 1) { + return undefined + } + continue + } + if (deno && DENO_PERMISSIONS.has(name)) { + if (equals === token.length - 1) { + return undefined + } + continue + } + if (python && /^-[WX].+/.test(token)) { + continue + } + return undefined + } + return undefined +} diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-service-contract.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-service-contract.ts index a790ee48ed5..88ec30489ed 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-service-contract.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-service-contract.ts @@ -62,6 +62,8 @@ export type AgentModelCatalogService = { export type AgentModelCatalogReadParams = { agent: string + /** Older readers follow only actual wait expiry, so retain their terminal wait. */ + progress?: boolean records?: AgentModelCatalogReadRecords | null operation?: AgentModelCatalogReadOperation /** Original explicit operation budget, shared with compatibility saved-scope continuation. */ diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-service.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-service.ts index 71d60ff9a0d..cc41d75786f 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-service.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-service.ts @@ -1,3 +1,4 @@ +import { onAgentsRefreshedOnHost } from '../../preflight/agents-refreshed-on-host' import { onLiveAccount } from './agent-model-catalog-running-account' import { agentSessionConversationView } from '../../../shared/agent-session/lifecycle/agent-session-conversation-view' import type { AgentSessionRecord } from '../../../shared/agent-session/lifecycle/agent-session-record' @@ -34,6 +35,9 @@ import { export function createAgentModelCatalogService( deps: AgentModelCatalogServiceDeps ): AgentModelCatalogService { + const unbindRefresh = onAgentsRefreshedOnHost((userInitiated) => + deps.store.refreshInputs(userInitiated) + ) const tasks = new Set>() const lifetime = new AbortController() const admission = new PrioritySemaphore(2) @@ -78,9 +82,18 @@ export function createAgentModelCatalogService( const operation = params.operation ?? captureRead(params) await waitWithinAgentModelCatalogRead(operation, lookup.request(params, operation)?.done) } + const flush = (params: AgentModelCatalogReadParams) => { + const bound = memoryBinding(params) + if (!params.savedOnly && bound?.current()) { + deps.store.flushProgress(bound.fingerprint) + } + } return { captureRead, - snapshot, + snapshot: (params) => { + flush(params) + return snapshot(params) + }, demand: async (params) => { if (!params.savedOnly) { await demand(params) @@ -108,7 +121,7 @@ export function createAgentModelCatalogService( params, reader: { deadline, signal: operation.signal }, store: deps.store, - refresh: lookup.request(params, operation), + refresh: () => lookup.request(params, operation), binding: () => bindings.get(requestKey(params)) }) } @@ -118,6 +131,7 @@ export function createAgentModelCatalogService( expired && params.operation?.current({ ignoreDeadline: true }) ? { ...params, operation: undefined } : params + flush(answerParams) const result = snapshot(answerParams) const answer = lifetime.signal.aborted ? result @@ -140,6 +154,7 @@ export function createAgentModelCatalogService( async stop() { lifetime.abort() unbindRetirement() + unbindRefresh() retireContext() deps.store.clearPartialAttempts() await Promise.allSettled(tasks) diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-shared-listing.test.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-shared-listing.test.ts index 836bb19aec6..9720d720134 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-shared-listing.test.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-shared-listing.test.ts @@ -1,3 +1,5 @@ +import { PrioritySemaphore } from '../../../shared/priority-semaphore' +import { runAgentModelCatalogSession } from './agent-model-catalog-probe-runner' import { expect, it, onTestFinished, vi } from 'vitest' import { createAgentModelCatalogService } from './agent-model-catalog-service' import { @@ -5,6 +7,7 @@ import { type StructuredAgentResolutionOperation } from '../agent-launch/structured-agent-command-resolution' import { AgentModelCatalogStore } from './agent-model-catalog-store' +import type { AgentModelCatalogProgress } from './agent-model-catalog-discovery' import type { AgentModelCatalogSuccess } from './agent-model-catalog-entry' function listing(id: string): AgentModelCatalogSuccess { @@ -29,7 +32,12 @@ function sharedListingHarness() { deadline?: number }[] = [] const run = vi.fn( - (identity: string, signal?: AbortSignal, operation?: StructuredAgentResolutionOperation) => { + ( + identity: string, + signal?: AbortSignal, + operation?: StructuredAgentResolutionOperation, + _publish?: AgentModelCatalogProgress + ) => { runs.push({ identity, signal, current: operation?.current, deadline: operation?.deadline }) return new Promise((resolve, reject) => { void listingFor(identity).promise.then(resolve) @@ -60,7 +68,12 @@ function sharedListingHarness() { const identity = folder?.startsWith('/own') ? folder : 'codex' return { identity, - run: (signal?: AbortSignal, owner = operation) => run(identity, signal, owner) + run: ( + signal?: AbortSignal, + owner = operation, + _selected?: readonly string[], + publish?: AgentModelCatalogProgress + ) => run(identity, signal, owner, publish) } } }) @@ -91,3 +104,281 @@ it("never answers a newly picked folder with the previous folder's late listing" models: [{ id: 'old' }] }) }) + +it('keeps physical admission through late success and actual teardown', async () => { + vi.useFakeTimers() + onTestFinished(() => { + vi.useRealTimers() + }) + const store = new AgentModelCatalogStore() + const admission = new PrioritySemaphore(1) + const close = Promise.withResolvers() + const closing = Promise.withResolvers() + const answer = Promise.withResolvers() + const first = store.refresh( + 'first', + 'grok', + async (_selected, owner, publish) => + runAgentModelCatalogSession( + () => ({ + close: () => { + closing.resolve() + return close.promise + } + }), + async () => { + publish?.(listing('partial')) + const result = await answer.promise + assertStructuredAgentResolutionCurrent(owner) + return result + }, + { label: 'grok', signal: owner.signal, timeoutMs: null } + ), + { admission } + ) + let secondStarted = false + const second = store.refresh( + 'second', + 'grok', + async () => { + secondStarted = true + return listing('second') + }, + { admission } + ) + await vi.advanceTimersByTimeAsync(45_000) + expect(store.get('first')?.models[0]?.id).toBe('partial') + answer.resolve(listing('late')) + await closing.promise + expect(secondStarted).toBe(false) + expect(store.isListing('first')).toBe(true) + expect(store.get('first')?.models[0]?.id).toBe('partial') + close.resolve() + expect((await first)?.models[0]?.id).toBe('late') + expect((await second)?.models[0]?.id).toBe('second') +}) + +it.each([false, true])( + 'keeps a slow successful listing after its reader leaves (saved=%s)', + async (saved) => { + vi.useFakeTimers() + onTestFinished(() => { + vi.useRealTimers() + }) + const h = sharedListingHarness() + if (saved) { + const initial = h.service.read({ agent: 'codex', waitForListing: true }) + await vi.advanceTimersByTimeAsync(0) + h.discovery.resolve({ ...listing('saved'), complete: true }) + await initial + h.store.expireScopes('codex') + } + const answer = Promise.withResolvers() + h.run.mockImplementation((_identity, signal, operation) => + runAgentModelCatalogSession( + () => ({ close: async () => {} }), + async () => { + const result = await answer.promise + assertStructuredAgentResolutionCurrent(operation) + return result + }, + { label: 'cold', signal, timeoutMs: null } + ) + ) + const reader = h.service.read({ agent: 'codex', waitForListing: true }) + await vi.advanceTimersByTimeAsync(30_000) + expect(await reader).toMatchObject({ listingInProgress: true, waitExpired: true }) + const snapshot = h.service.snapshot({ agent: 'codex' }) + if (saved) { + expect(snapshot).toMatchObject({ models: [{ id: 'saved' }] }) + } else { + expect(snapshot.origin).toBe('unknown') + } + await vi.advanceTimersByTimeAsync(15_000) + answer.resolve({ ...listing('slow-success'), complete: true }) + await vi.advanceTimersByTimeAsync(0) + expect(await h.service.read({ agent: 'codex', waitForListing: true })).toMatchObject({ + models: [{ id: 'slow-success' }] + }) + expect(h.service.snapshot({ agent: 'codex' }).listingInProgress).toBeUndefined() + } +) + +it.each([true, false])( + 'delivers progress and waits for a changed observation (progress=%s)', + async (progress) => { + vi.useFakeTimers() + onTestFinished(() => { + vi.useRealTimers() + }) + const h = sharedListingHarness() + const answer = Promise.withResolvers() + let publish: AgentModelCatalogProgress | undefined + h.run.mockImplementation((_identity, _signal, _operation, notify) => { + publish = notify + return answer.promise + }) + const first = h.service.read({ agent: 'codex', waitForListing: true, progress }) + await vi.advanceTimersByTimeAsync(0) + publish?.({ ...listing('listed'), complete: false }) + await vi.advanceTimersByTimeAsync(progress ? 0 : 30_000) + expect(await first).toMatchObject({ + models: [{ id: 'listed', isDefault: false }], + listingInProgress: true + }) + expect((await first).waitExpired).toBe(progress ? undefined : true) + let next: unknown + const follow = h.service + .read({ agent: 'codex', waitForListing: true, progress: true }) + .then((result) => { + next = result + }) + await vi.advanceTimersByTimeAsync(1_000) + expect(next).toBeUndefined() + publish?.({ ...listing('listed'), complete: true, configured: { model: 'listed' } }, true) + await follow + expect(next).toMatchObject({ + models: [{ id: 'listed', isDefault: true }], + listingInProgress: true + }) + answer.resolve({ ...listing('listed'), complete: true, configured: { model: 'listed' } }) + await vi.advanceTimersByTimeAsync(0) + expect(h.service.snapshot({ agent: 'codex' }).listingInProgress).toBeUndefined() + } +) + +it('keeps an account verdict until discovery actually answers it, including optional failure', async () => { + const store = new AgentModelCatalogStore() + store.recordSuccess( + 'held', + 'codex', + { ...listing('saved'), complete: true, unavailable: { reason: 'notSignedIn' } }, + 'discovery' + ) + store.expireFailure('held') + await store.refresh('held', 'codex', async (_selected, _owner, publish) => { + publish?.({ ...listing('listed'), complete: true }, false) + return { ...listing('listed'), complete: true } + }) + expect(store.failure('held')?.unavailable).toEqual({ reason: 'notSignedIn' }) + await store.refresh('held', 'codex', async (_selected, _owner, publish) => { + publish?.({ ...listing('listed'), complete: true }, true) + return { ...listing('listed'), complete: true } + }) + expect(store.failure('held')).toBeNull() +}) + +it('fences obsolete progress and final results while retaining admission until cancelled teardown', async () => { + const store = new AgentModelCatalogStore() + const admission = new PrioritySemaphore(1) + const started = Promise.withResolvers() + const closing = Promise.withResolvers() + const closed = Promise.withResolvers() + const answer = Promise.withResolvers() + let notify: AgentModelCatalogProgress | undefined + const first = store.refresh( + 'old', + 'grok', + async (_selected, owner, publish) => + runAgentModelCatalogSession( + () => ({ + close: () => { + closing.resolve() + return closed.promise + } + }), + async () => { + notify = publish + publish?.(listing('usable')) + started.resolve() + return answer.promise + }, + { label: 'grok', timeoutMs: null, signal: owner.signal } + ), + { admission } + ) + await started.promise + notify?.defer?.( + () => listing('obsolete-deferred'), + () => true + ) + store.expireScopes('grok') + store.flushProgress('old') + await closing.promise + notify?.(listing('obsolete-progress')) + notify?.defer?.( + () => listing('obsolete-new-deferred'), + () => true + ) + store.flushProgress('old') + answer.resolve(listing('obsolete-final')) + let entered = false + const next = store.refresh( + 'new', + 'grok', + async () => { + entered = true + return listing('new') + }, + { admission } + ) + await Promise.resolve() + expect(entered).toBe(false) + expect(store.get('old')?.models[0]?.id).toBe('usable') + closed.resolve() + expect(await first).toBeNull() + expect((await next)?.models[0]?.id).toBe('new') + expect(store.get('old')?.models[0]?.id).toBe('usable') +}) + +it('withdraws an abandoned queued scope while the two admitted listings continue', async () => { + vi.useFakeTimers() + onTestFinished(() => { + vi.useRealTimers() + }) + const h = sharedListingHarness() + void h.service.demand({ agent: 'codex', workspacePath: '/own-a' }) + void h.service.demand({ agent: 'codex', workspacePath: '/own-b' }) + const queued = h.service.read({ agent: 'codex', workspacePath: '/own-c', waitForListing: true }) + await vi.advanceTimersByTimeAsync(30_000) + expect(await queued).toMatchObject({ + origin: 'unknown', + listingInProgress: true, + waitExpired: true + }) + await vi.advanceTimersByTimeAsync(5_001) + expect( + h.service.snapshot({ agent: 'codex', workspacePath: '/own-c' }).listingInProgress + ).toBeUndefined() + h.listingFor('/own-a').resolve({ ...listing('a'), complete: true }) + await vi.advanceTimersByTimeAsync(0) + expect(h.service.snapshot({ agent: 'codex', workspacePath: '/own-c' }).origin).toBe('unknown') +}) + +it.each([false, true])( + 'settles an identical final observation, including eviction (evicted=%s)', + async (evicted) => { + const store = new AgentModelCatalogStore() + let first: unknown + const success = { ...listing('same'), complete: true } + const result = await store.refresh('same', 'codex', async (_selected, _owner, publish) => { + publish?.(success, true) + first = store.get('same') + if (evicted) { + for (let index = 0; index < 256; index++) { + store.recordSuccess(`other-${index}`, 'codex', success, 'discovery') + } + expect(store.get('same')).toBeNull() + } + return success + }) + expect(result?.models[0]?.id).toBe('same') + if (!evicted) { + expect(store.get('same')).toBe(first) + } + expect(store.get('same')?.models[0]?.id).toBe('same') + expect(store.failure('same')).toBeNull() + expect(store.isListing('same')).toBe(false) + expect(store.probeDue('same')).toBe(false) + } +) diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-shared-setup.test.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-shared-setup.test.ts index 19c2f6f17f2..613981d2654 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-shared-setup.test.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-shared-setup.test.ts @@ -137,7 +137,7 @@ it('keeps one listing for a workspace on the same folder when another workspace expect(await a).toMatchObject({ models: [{ id: 'other' }] }) }) -it('ends a shorter reader at its own deadline, and a stalled refresh lists nothing after its budget', async () => { +it('ends a shorter reader while scope resolution remains current', async () => { vi.useFakeTimers() onTestFinished(() => { vi.useRealTimers() @@ -163,9 +163,7 @@ it('ends a shorter reader at its own deadline, and a stalled refresh lists nothi // Its readers are gone, but the refresh runs on its own budget. expect(h.service.snapshot({ agent: 'codex', sessionId: 'b' })).toHaveProperty('listingInProgress') await vi.advanceTimersByTimeAsync(29_000) - expect(h.service.snapshot({ agent: 'codex', sessionId: 'b' })).not.toHaveProperty( - 'listingInProgress' - ) + expect(h.service.snapshot({ agent: 'codex', sessionId: 'b' })).toHaveProperty('listingInProgress') gate.resolve() await vi.advanceTimersByTimeAsync(0) expect(h.run).not.toHaveBeenCalled() @@ -177,7 +175,8 @@ it('holds readers of a stalled refresh only to their own deadlines, and starts n vi.useRealTimers() }) const h = harness() - h.folder.gate = Promise.withResolvers() + const gate = Promise.withResolvers() + h.folder.gate = gate void h.service.demand({ agent: 'codex', sessionId: 'a', deadline: Date.now() + 1_000 }) const b = h.service.read({ agent: 'codex', @@ -187,7 +186,7 @@ it('holds readers of a stalled refresh only to their own deadlines, and starts n }) await vi.advanceTimersByTimeAsync(30_000) // b's deadline ended its wait; a reader leaving never restarts the work. - expect(await b).toEqual({ origin: 'unknown' }) + expect(await b).toMatchObject({ origin: 'unknown', listingInProgress: true, waitExpired: true }) expect(h.recordWorkspacePath).toHaveBeenCalledOnce() const c = h.service.read({ agent: 'codex', @@ -195,8 +194,10 @@ it('holds readers of a stalled refresh only to their own deadlines, and starts n waitForListing: true, deadline: Date.now() + 30_000 }) - await vi.waitFor(() => expect(h.run).toHaveBeenCalledOnce()) - expect(h.recordWorkspacePath).toHaveBeenCalledTimes(2) + await vi.advanceTimersByTimeAsync(15_000) + gate.resolve() + await vi.advanceTimersByTimeAsync(0) + expect(h.recordWorkspacePath).toHaveBeenCalledOnce() h.discovery.resolve(listing('fresh')) await vi.advanceTimersByTimeAsync(0) expect(await c).toMatchObject({ models: [{ id: 'fresh' }] }) @@ -226,26 +227,32 @@ it("keeps the folder's defaults when they arrive after a shorter last reader lef expect(await a).toMatchObject({ models: [{ id: 'listed', isDefault: true }] }) }) -it("keeps the listing when the folder's settings stall, naming no default, within the reader's budget", async () => { +it('delivers inventory before slow folder defaults, then delivers the proven default', async () => { vi.useFakeTimers() onTestFinished(() => { vi.useRealTimers() }) const h = harness(agent) - h.folder.settings = Promise.withResolvers() - void h.service.demand({ agent, sessionId: 'a', deadline: Date.now() + 1_000 }) - let answer: unknown - void h.service - .read({ agent, sessionId: 'b', waitForListing: true, deadline: Date.now() + 30_000 }) - .then((result) => { - answer = result - }) - await vi.waitFor(() => expect(h.run).toHaveBeenCalledOnce()) + const settings = Promise.withResolvers() + h.folder.settings = settings + const first = h.service.read({ agent, sessionId: 'b', waitForListing: true }) + await vi.advanceTimersByTimeAsync(0) h.discovery.resolve(listing('listed')) - await vi.advanceTimersByTimeAsync(30_000) - // Whether the project picks its own model stays unknown, so the account's default isn't named. - expect(answer).toMatchObject({ models: [{ id: 'listed', isDefault: false }] }) - expect(answer).not.toHaveProperty('listingInProgress') + await vi.advanceTimersByTimeAsync(0) + expect(await first).toMatchObject({ + models: [{ id: 'listed', isDefault: false }], + listingInProgress: true + }) + let next: unknown + const follow = h.service.read({ agent, sessionId: 'b', waitForListing: true }).then((result) => { + next = result + }) + await vi.advanceTimersByTimeAsync(1_000) + expect(next).toBeUndefined() + settings.resolve() + await follow + expect(next).toMatchObject({ models: [{ id: 'listed', isDefault: true }] }) + expect(next).not.toHaveProperty('listingInProgress') }) it("never lets a moved folder's late settings describe the folder it moved to", async () => { @@ -326,3 +333,43 @@ it('keeps one folder-agent listing for a workspace on the same folder when anoth expect(await a).toMatchObject({ models: [{ id: 'other' }] }) expect(probe.mock.calls.map((call) => call[1]?.workspacePath)).toEqual(['/repo', '/other']) }) + +it.each([10, 100])( + 'registers %i readers and unique targets once per shared flight', + async (count) => { + vi.useFakeTimers() + onTestFinished(() => { + vi.useRealTimers() + }) + for (const delayedScope of [false, true]) { + const h = harness('opencode2') + const scope = Promise.withResolvers() + if (delayedScope) { + h.folder.gate = scope + } + const registrations = vi.spyOn(h.store, 'refresh') + const submissions = vi.spyOn(h.store.efforts, 'demand') + const readers: Promise[] = [] + for (let index = 0; index < count; index++) { + readers.push( + h.service.read({ + agent: 'opencode2', + sessionId: 'a', + selectedModel: `m${index}`, + waitForListing: true + }) + ) + if (!delayedScope) { + await vi.advanceTimersByTimeAsync(0) + } + } + scope.resolve() + await vi.advanceTimersByTimeAsync(0) + expect(h.run).toHaveBeenCalledOnce() + expect(registrations.mock.calls.length).toBe(count) + expect(submissions.mock.calls.length).toBe(count) + h.discovery.resolve(listing('shared')) + await Promise.all(readers) + } + } +) diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-snapshot.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-snapshot.ts index 55cb348482b..d654c868e70 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-snapshot.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-snapshot.ts @@ -8,10 +8,7 @@ import type { AgentSessionConversationView } from '../../../shared/agent-session import type { AgentSessionModelCatalogResult } from '../../../shared/agent-session/agent-session-wire' import { agentModelCatalogResultFromEntry } from './agent-model-catalog-entry' import { agentModelCatalogDefaultFollowsAccount } from './agent-model-catalog-workspace' -import { - independentAgentModelCatalog, - usesModelSpecificAcpEfforts -} from '../../../shared/agent-session/options/agent-model-catalog-ownership' +import { independentAgentModelCatalog } from '../../../shared/agent-session/options/agent-model-catalog-ownership' import type { AgentModelCatalogReadParams, AgentModelCatalogServiceDeps @@ -239,11 +236,11 @@ export function projectAgentModelCatalogSnapshot( ...(!params.savedOnly && !params.verifiedOnly && (deps.store.isListing(bound.fingerprint) || + (resolving && bound.projecting !== undefined) || (resolving && (deps.store.probeDue(bound.fingerprint) || // The picked model's menu is listed by the refresh this read just asked for. - deps.store.efforts.due(bound.fingerprint, params.selectedModel)))) && - (independent || !listed || usesModelSpecificAcpEfforts(params.agent)) + deps.store.efforts.due(bound.fingerprint, params.selectedModel)))) ? { listingInProgress: true as const } : {}), ...(deps.store.failure(bound.fingerprint)?.unavailable diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-store.test.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-store.test.ts index 27013afc0b9..2cb06345424 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-store.test.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-store.test.ts @@ -4,6 +4,7 @@ import { join } from 'node:path' import { describe, expect, it } from 'vitest' import type { AgentSessionModelOption } from '../../../shared/agent-session/agent-session-wire' import { createAgentModelCatalogFilePersistence } from './agent-model-catalog-persistence' +import { expireAgentModelCatalogFailuresForSettings } from './agent-model-catalog-account-expiry' import { AgentModelCatalogStore, type AgentModelCatalogSuccess } from './agent-model-catalog-store' function models(...ids: string[]): AgentSessionModelOption[] { @@ -23,6 +24,52 @@ function success(...ids: string[]): AgentModelCatalogSuccess { } describe('agent model catalog store', () => { + it('invalidates changed inputs only, retaining rows and incomplete evidence until rediscovery', async () => { + let now = 0 + const store = new AgentModelCatalogStore({ now: () => now }) + store.observeInputs('a', 'install-1') + store.recordSuccess( + 'a', + 'claude', + { ...success('opus'), origin: 'probe', complete: false }, + 'discovery' + ) + store.recordSuccess( + 'b', + 'codex', + { ...success('gpt'), origin: 'probe', complete: true }, + 'discovery' + ) + now = 13 * 60 * 60_000 + store.refreshInputs(false) + expect(store.probeDue('a')).toBe(false) + expect(store.verified(store.get('a')!)).toBe(false) + expect(store.probeDue('b')).toBe(false) + store.observeInputs('a', 'install-2') + expect(store.probeDue('a')).toBe(true) + expect(store.get('a')?.models[0]?.id).toBe('opus') + expect(store.probeDue('b')).toBe(false) + expect(store.retirementRevision('claude')).toBe(0) + await store.refresh('a', 'claude', async () => { + throw new Error('network unavailable') + }) + expect(store.probeDue('a')).toBe(false) + now += 30_001 + expect(store.probeDue('a')).toBe(true) + expect(store.get('a')?.models[0]?.id).toBe('opus') + await store.refresh('a', 'claude', async () => ({ + ...success('sonnet'), + origin: 'probe', + complete: true + })) + now += 13 * 60 * 60_000 + expect(store.probeDue('a')).toBe(false) + expect(store.get('a')?.models[0]?.id).toBe('sonnet') + store.refreshInputs(true) + expect(store.probeDue('a')).toBe(true) + expect(store.probeDue('b')).toBe(true) + }) + it("keeps a live child's default effort through a listing that names none, across a restart", async () => { const directory = mkdtempSync(join(tmpdir(), 'agent-model-catalog-')) const store = new AgentModelCatalogStore() @@ -102,3 +149,43 @@ describe('agent model catalog store', () => { ) }) }) + +it('requires validation when a retained catalog has lost its bounded input baseline', () => { + const store = new AgentModelCatalogStore() + for (let index = 0; index < 256; index++) { + store.observeInputs(String(index), 'original') + store.recordSuccess(String(index), 'grok', success('old'), 'discovery') + } + store.get('0') + store.observeInputs('256', 'original') + store.recordSuccess('256', 'grok', success('new'), 'discovery') + store.observeInputs('0', 'replacement') + expect(store.probeDue('0')).toBe(true) + expect(store.get('0')?.models[0]?.id).toBe('old') + store.observeInputs('2', 'original') + expect(store.probeDue('2')).toBe(false) +}) + +it('requires new account evidence after same-home reauthentication while preserving other agents', async () => { + const store = new AgentModelCatalogStore() + for (const agent of ['claude', 'codex', 'grok']) { + store.recordSuccess(agent, agent, { ...success('old'), complete: true }, 'discovery') + } + const stale = Promise.withResolvers() + const flight = store.refresh('claude', 'claude', () => stale.promise) + await Promise.resolve() + expireAgentModelCatalogFailuresForSettings(store, { claudeManagedAccounts: [] }) + expect(store.probeDue('claude')).toBe(true) + expect(store.get('claude')?.models[0]?.id).toBe('old') + expect(store.probeDue('codex')).toBe(false) + expect(store.probeDue('grok')).toBe(false) + stale.resolve(success('wrong-account')) + await flight + await Promise.resolve() + expect(store.get('claude')?.models[0]?.id).toBe('old') + await store.refresh('claude', 'claude', async () => ({ + ...success('new-account'), + complete: true + })) + expect(store.get('claude')?.models[0]?.id).toBe('new-account') +}) diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-store.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-store.ts index 781ced64ed5..6786abc1b6c 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-store.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-store.ts @@ -1,12 +1,13 @@ +import { AgentModelCatalogInputs } from './agent-model-catalog-inputs' import { createAgentModelEffortDiscovery } from './agent-model-catalog-effort-discovery' import { createAgentModelCatalogFences } from './agent-model-catalog-fences' import { evictAgentModelCatalogEntries, createAgentModelCatalogEntryFactory, + createAgentModelCatalogSuccessWriter, retireAgentModelCatalogObservations, agentModelCatalogLaunchAgents, - agentModelCatalogEntryIsStale, - pruneAgentModelCatalogPartialAttempts + agentModelCatalogEntryIsStale } from './agent-model-catalog-entry-lifetime' import { independentAgentModelCatalog } from '../../../shared/agent-session/options/agent-model-catalog-ownership' export type { AgentModelCatalogProbe } from './agent-model-catalog-discovery' @@ -41,10 +42,6 @@ import { export { AGENT_MODEL_CATALOG_FAILURE_TTL_MS, AGENT_MODEL_CATALOG_PICKER_WAIT_MS } export type { AgentModelCatalogFailure } -// Persist successful catalogs only; failures/unavailability are held separately. -// Saved display data may be stale, while launch proof requires fresh discovery in this process. - -export { AGENT_MODEL_CATALOG_FRESH_MS } from './agent-model-catalog-entry-lifetime' export const AGENT_MODEL_CATALOG_MAX_ENTRIES = 256 import { @@ -65,6 +62,7 @@ export class AgentModelCatalogStore { (fingerprint) => this.refreshes.has(fingerprint) ) // Changed launch commands/env invalidate catalogs until rediscovery. + private readonly inputs: AgentModelCatalogInputs private readonly dueEntries = new Set() private readonly partialAttempts = new Map() private readonly retirementRevisions = new Map() @@ -78,6 +76,17 @@ export class AgentModelCatalogStore { constructor(options?: { now?: () => number }) { this.now = options?.now ?? Date.now this.failures = new AgentModelCatalogFailures(this.now) + this.inputs = new AgentModelCatalogInputs({ + entries: this.entries, + refreshes: this.refreshes, + dueEntries: this.dueEntries, + failures: this.failures, + partialAttempts: this.partialAttempts, + listingWaiters: this.listingWaiters, + agents: () => agentModelCatalogLaunchAgents(this.retirementRevisions, this.entries), + retireAgent: (agent) => this.expireFailures(agent), + cap: AGENT_MODEL_CATALOG_MAX_ENTRIES + }) this.efforts = createAgentModelEffortDiscovery({ entries: this.entries, refreshes: this.refreshes, @@ -94,9 +103,7 @@ export class AgentModelCatalogStore { for (const entry of await persistence.load()) { if (!this.entries.has(entry.fingerprint)) { this.entries.set(entry.fingerprint, entry) - if (independentAgentModelCatalog(entry.agent)) { - this.dueEntries.add(entry.fingerprint) - } + this.dueEntries.add(entry.fingerprint) } } this.evictOverCap() @@ -128,7 +135,7 @@ export class AgentModelCatalogStore { /** Live saves must not delay account-level discovery. */ isStale(entry: AgentModelCatalogEntry): boolean { - return agentModelCatalogEntryIsStale(entry, this.dueEntries.has(entry.fingerprint), this.now()) + return agentModelCatalogEntryIsStale(entry, this.dueEntries.has(entry.fingerprint)) } failureDetail(fingerprint: string): string | null { @@ -172,6 +179,18 @@ export class AgentModelCatalogStore { this.failures.expireAgent(agent) } + observeInputs(fingerprint: string, stamp: string): void { + this.inputs.observeInputs(fingerprint, stamp) + } + + refreshInputs(userInitiated: boolean): void { + this.inputs.refreshInputs(userInitiated) + } + + expireScopes(agent?: string): void { + this.inputs.expireScopes(agent) + } + expireFailure(fingerprint: string): void { this.failures.expire(fingerprint) } @@ -223,46 +242,17 @@ export class AgentModelCatalogStore { this.listingWaiters.notify(fingerprint) } - private writeSuccess( - fingerprint: string, - agent: string, - success: AgentModelCatalogSuccess, - source: AgentModelCatalogSource - ): AgentModelCatalogEntry | null { - const entry = this.entryFromSuccess(fingerprint, agent, success, source) - if (!entry) { - if (source === 'discovery' && independentAgentModelCatalog(agent)) { - this.failures.probeFailed(fingerprint, agent, new Error('agent listed no models')) - } - return null - } - this.retiredObservations.delete(fingerprint) - const previous = this.entries.get(fingerprint) - this.entries.delete(fingerprint) - this.entries.set(fingerprint, entry) - // Live listings cannot clear discovery failures or account-unavailability verdicts. - if ( - source === 'discovery' && - (!independentAgentModelCatalog(agent) || success.complete === true) - ) { - this.dueEntries.delete(fingerprint) - this.failures.listed(fingerprint, agent, success.origin, success.unavailable) - } - if (source === 'discovery' && independentAgentModelCatalog(agent)) { - this.efforts.catalogAttempted(fingerprint, agent, success.complete === true) - if (success.complete !== true) { - this.failures.listed(fingerprint, agent, success.origin, success.unavailable) - } - } - this.evictOverCap() - // Unchanged per-turn listings need no disk write. - if (!previous || agentModelCatalogListingKey(previous) !== agentModelCatalogListingKey(entry)) { - this.persistence?.save(() => [...this.entries.values()]) - } - return entry - } + private readonly writeSuccess = createAgentModelCatalogSuccessWriter(() => ({ + entries: this.entries, + retiredObservations: this.retiredObservations, + dueEntries: this.dueEntries, + failures: this.failures, + efforts: this.efforts, + entryFromSuccess: this.entryFromSuccess, + persistence: this.persistence, + evict: () => this.evictOverCap() + })) - /** One probe per canonical scope; failures resolve null. Its starter's budget bounds it. */ refresh( fingerprint: string, agent: string, @@ -283,10 +273,18 @@ export class AgentModelCatalogStore { refreshes: this.refreshes, failures: this.failures, listingWaiters: this.listingWaiters, + failed: () => { + if (this.entries.has(fingerprint)) { + this.dueEntries.add(fingerprint) + } + this.partialAttempts.delete(fingerprint) + }, writeSuccess: (...args) => this.writeSuccess(...args) }) } + flushProgress = (key: string): void => this.refreshes.get(key)?.flush() + isListing(fingerprint: string): boolean { const flight = this.refreshes.get(fingerprint) if (flight && !flight.current()) { @@ -302,6 +300,7 @@ export class AgentModelCatalogStore { verified(entry: AgentModelCatalogEntry): boolean { return ( !this.isStale(entry) && + (!independentAgentModelCatalog(entry.agent) || entry.discovered?.complete === true) && !this.hasActiveFailure(entry.fingerprint) && !this.failure(entry.fingerprint)?.unavailable ) @@ -319,7 +318,6 @@ export class AgentModelCatalogStore { if (this.isListing(fingerprint) || this.hasActiveFailure(fingerprint)) { return false } - pruneAgentModelCatalogPartialAttempts(this.partialAttempts, this.now()) const entry = this.entries.get(fingerprint) return !this.partialAttempts.has(fingerprint) && (!entry || this.isStale(entry)) } diff --git a/src/main/native-chat/agent-session-wire/options/structured-agent-session-catalog-reads.ts b/src/main/native-chat/agent-session-wire/options/structured-agent-session-catalog-reads.ts index cbf7642a223..da4f1024c4c 100644 --- a/src/main/native-chat/agent-session-wire/options/structured-agent-session-catalog-reads.ts +++ b/src/main/native-chat/agent-session-wire/options/structured-agent-session-catalog-reads.ts @@ -59,7 +59,7 @@ export function createStructuredAgentSessionCatalogReads( return { origin: 'unknown' } } const request = { ...params, records, operation, deadline: operation.deadline } - if (!params.savedOnly) { + if (!params.savedOnly && !params.waitForListing) { void catalog.demand(request) } return catalog.read(request) diff --git a/src/main/orcad/browser-providers/chromium/external-chromium-agent-browser-environment.ts b/src/main/orcad/browser-providers/chromium/external-chromium-agent-browser-environment.ts new file mode 100644 index 00000000000..f9886cac52c --- /dev/null +++ b/src/main/orcad/browser-providers/chromium/external-chromium-agent-browser-environment.ts @@ -0,0 +1,27 @@ +import { isolateFromUserAgentBrowserSettings } from '../../../browser/agent-browser/helper-process/agent-browser-user-settings-isolation' + +// Both pick the socket directory, so dropping either loses a running helper and its live tabs. +const SOCKET_LOCATION_KEYS = ['AGENT_BROWSER_SOCKET_DIR', 'AGENT_BROWSER_NAMESPACE'] + +// No idle timeout: this daemon owns the user’s remote Chromium and all its live tabs. +// agent-browser 0.33+ reads an unset timeout as one hour, so only "0" keeps it unbounded. +export function externalChromiumAgentBrowserEnvironment(options: { + inheritedEnv: NodeJS.ProcessEnv + executablePath: string + profilePath: string + sessionName: string + configPath: string + browserArgs?: readonly string[] +}): NodeJS.ProcessEnv { + return { + ...isolateFromUserAgentBrowserSettings(options.inheritedEnv, SOCKET_LOCATION_KEYS), + AGENT_BROWSER_IDLE_TIMEOUT_MS: + options.inheritedEnv.AGENT_BROWSER_IDLE_TIMEOUT_MS?.trim() || '0', + AGENT_BROWSER_EXECUTABLE_PATH: options.executablePath, + AGENT_BROWSER_PROFILE: options.profilePath, + AGENT_BROWSER_SESSION: options.sessionName, + // An explicit config skips ~/.agent-browser/config.json and ./agent-browser.json. + AGENT_BROWSER_CONFIG: options.configPath, + AGENT_BROWSER_ARGS: options.browserArgs?.join('\n') ?? '' + } +} diff --git a/src/main/orcad/browser-providers/chromium/external-chromium-browser-session.test.ts b/src/main/orcad/browser-providers/chromium/external-chromium-browser-session.test.ts index 144fd9edec3..a25b07c0164 100644 --- a/src/main/orcad/browser-providers/chromium/external-chromium-browser-session.test.ts +++ b/src/main/orcad/browser-providers/chromium/external-chromium-browser-session.test.ts @@ -12,7 +12,8 @@ vi.mock('node:fs/promises', () => ({ readFile: vi.fn(async () => Buffer.from('')), realpath: vi.fn(async (path: string) => path), access: vi.fn(async () => undefined), - rm: vi.fn(async () => undefined) + rm: vi.fn(async () => undefined), + writeFile: vi.fn(async () => undefined) })) import { ExternalChromiumBrowserSession } from './external-chromium-browser-session' @@ -41,6 +42,37 @@ describe('orcad external-chromium agent-browser environment', () => { vi.mocked(mkdir).mockClear() }) + it('launches headless with only Orca agent-browser settings', async () => { + runProcessMock.mockResolvedValue({ + code: 0, + signal: null, + stdout: JSON.stringify([{ success: true, result: { url: 'about:blank' } }]), + stderr: '', + timedOut: false + }) + vi.stubEnv('AGENT_BROWSER_HEADED', '1') + vi.stubEnv('agent_browser_extensions', '/ext') + vi.stubEnv('AGENT_BROWSER_SOCKET_DIR', '/tmp/ab-sockets') + try { + await new ExternalChromiumBrowserSession( + '/opt/orca/agent-browser', + { executablePath: BASE.executablePath, provider: 'chromium' }, + '/state' + ).run(['get', 'url']) + } finally { + vi.unstubAllEnvs() + } + const [spec] = runProcessMock.mock.calls[0] + const headed = spec.args?.indexOf('--headed') ?? -1 + expect(spec.args?.slice(headed, headed + 2)).toEqual(['--headed', 'false']) + expect(spec.env).toMatchObject({ + AGENT_BROWSER_SOCKET_DIR: '/tmp/ab-sockets', + AGENT_BROWSER_CONFIG: join('/state', 'agent-browser-config.json') + }) + expect(spec.env).not.toHaveProperty('AGENT_BROWSER_HEADED') + expect(spec.env).not.toHaveProperty('agent_browser_extensions') + }) + it('reports a ref whose element is gone as stale', async () => { runProcessMock.mockResolvedValue({ code: 1, diff --git a/src/main/orcad/browser-providers/chromium/external-chromium-browser-session.ts b/src/main/orcad/browser-providers/chromium/external-chromium-browser-session.ts index c014d87e5a7..fdcfc730f51 100644 --- a/src/main/orcad/browser-providers/chromium/external-chromium-browser-session.ts +++ b/src/main/orcad/browser-providers/chromium/external-chromium-browser-session.ts @@ -1,5 +1,5 @@ import { createHash, randomUUID } from 'node:crypto' -import { chmod, mkdir, readFile, rm } from 'node:fs/promises' +import { chmod, mkdir, readFile, rm, writeFile } from 'node:fs/promises' import { join } from 'node:path' import { z } from 'zod' import { BrowserError } from '../../../browser/browser-error' @@ -17,6 +17,7 @@ import { } from '../../../browser/agent-browser/observation/agent-browser-screenshot-arguments' import { runProcess } from '@orca/process-host' import { resolveExternalChromiumProfilePath } from './external-chromium-profile-path' +import { externalChromiumAgentBrowserEnvironment } from './external-chromium-agent-browser-environment' const COMMAND_TIMEOUT_MS = 90_000 const MAX_OUTPUT_BYTES = 50 * 1024 * 1024 @@ -73,26 +74,6 @@ function classifyAgentBrowserError(message: string): string { return 'browser_error' } -// No idle timeout: this daemon owns the user’s remote Chromium and all its live tabs. -// agent-browser 0.33+ reads an unset timeout as one hour, so only "0" keeps it unbounded. -export function externalChromiumAgentBrowserEnvironment(options: { - inheritedEnv: NodeJS.ProcessEnv - executablePath: string - profilePath: string - sessionName: string - browserArgs?: readonly string[] -}): NodeJS.ProcessEnv { - return { - ...options.inheritedEnv, - AGENT_BROWSER_IDLE_TIMEOUT_MS: - options.inheritedEnv.AGENT_BROWSER_IDLE_TIMEOUT_MS?.trim() || '0', - AGENT_BROWSER_EXECUTABLE_PATH: options.executablePath, - AGENT_BROWSER_PROFILE: options.profilePath, - AGENT_BROWSER_SESSION: options.sessionName, - AGENT_BROWSER_ARGS: options.browserArgs?.join('\n') ?? '' - } -} - // A profile held by another Chromium also fails ProcessSingleton; only an access failure means unwritable. function unwritableProfileLockLine(message: string): string | undefined { return message @@ -105,6 +86,7 @@ export class ExternalChromiumBrowserSession { private readonly identity: string private readonly sessionName: string private profilePathResolution: Promise | null = null + private configPathWrite: Promise | null = null constructor( private readonly agentBrowserPath: string, @@ -127,6 +109,26 @@ export class ExternalChromiumBrowserSession { return this.profilePathResolution } + // Empty, so agent-browser's defaults (headless, no extensions) apply instead of a user's config. + private configPath(): Promise { + if (!this.configPathWrite) { + const write = (async () => { + const path = join(this.statePath, 'agent-browser-config.json') + await mkdir(this.statePath, { recursive: true, mode: 0o700 }) + await writeFile(path, '{}\n', { mode: 0o600 }) + return path + })() + // A failed write retries on the next command instead of failing every later one. + write.catch(() => { + if (this.configPathWrite === write) { + this.configPathWrite = null + } + }) + this.configPathWrite = write + } + return this.configPathWrite + } + async start(signal?: AbortSignal): Promise { signal?.throwIfAborted() const profilePath = await this.profilePath() @@ -267,7 +269,10 @@ export class ExternalChromiumBrowserSession { signal?.throwIfAborted() const profilePath = await this.profilePath() signal?.throwIfAborted() - const args = ['--session', this.sessionName, '--profile', profilePath] + const configPath = await this.configPath() + signal?.throwIfAborted() + // Pinned per command so a daemon started headed by an older environment relaunches headless. + const args = ['--session', this.sessionName, '--profile', profilePath, '--headed', 'false'] if (this.launch.browserArgs?.length) { args.push('--args', this.launch.browserArgs.join('\n')) } @@ -277,6 +282,7 @@ export class ExternalChromiumBrowserSession { executablePath: this.launch.executablePath, profilePath, sessionName: this.sessionName, + configPath, browserArgs: this.launch.browserArgs }) const result = await runProcess({ diff --git a/src/main/orcad/browser-providers/chromium/external-chromium-command-arguments.ts b/src/main/orcad/browser-providers/chromium/external-chromium-command-arguments.ts index 8f7fa05b8d7..1fcdad2e183 100644 --- a/src/main/orcad/browser-providers/chromium/external-chromium-command-arguments.ts +++ b/src/main/orcad/browser-providers/chromium/external-chromium-command-arguments.ts @@ -1,69 +1,9 @@ -import { BrowserError } from '../../../browser/browser-error' import { agentBrowserSetMediaArguments } from '../../../browser/agent-browser/protocol/agent-browser-set-media-arguments' import { agentBrowserWaitArguments } from '../../../browser/agent-browser/protocol/agent-browser-wait-arguments' -const TARGET_OVERRIDE_FLAGS: Record = { - '--args': true, - '--cdp': true, - '--executable-path': true, - '--profile': true, - '--session': true -} -const TARGET_OVERRIDE_FLAG_PREFIXES = Object.keys(TARGET_OVERRIDE_FLAGS).map((flag) => `${flag}=`) - -function parseExecArguments(input: string): string[] { - const args: string[] = [] - let value = '' - let quote: '"' | "'" | null = null - let escaped = false - for (const char of input.trim()) { - if (escaped) { - value += char - escaped = false - } else if (char === '\\' && quote !== "'") { - escaped = true - } else if (quote) { - if (char === quote) { - quote = null - } else { - value += char - } - } else if (char === '"' || char === "'") { - quote = char - } else if (/\s/.test(char)) { - if (value) { - args.push(value) - value = '' - } - } else { - value += char - } - } - if (escaped) { - value += '\\' - } - if (quote) { - throw new BrowserError('invalid_argument', 'Browser command has an unclosed quote.') - } - if (value) { - args.push(value) - } - return args -} -function stripTargetOverrideArguments(args: string[]): string[] { - const stripped: string[] = [] - for (let index = 0; index < args.length; index++) { - const arg = args[index] - if (TARGET_OVERRIDE_FLAGS[arg]) { - index++ - continue - } - if (TARGET_OVERRIDE_FLAG_PREFIXES.some((prefix) => arg.startsWith(prefix))) { - continue - } - stripped.push(arg) - } - return stripped -} +import { + assertAgentBrowserPassthroughStaysOnPage, + splitAgentBrowserCommandLine +} from '../../../browser/agent-browser/protocol/agent-browser-passthrough-arguments' export function externalChromiumWaitCommand( params: Record @@ -214,8 +154,11 @@ export function externalChromiumCommandArguments( return ['network', 'har', 'start'] case 'browserCaptureStop': return ['network', 'har', 'stop'] - case 'browserExec': - return stripTargetOverrideArguments(parseExecArguments(text('command'))) + case 'browserExec': { + const args = splitAgentBrowserCommandLine(text('command')) + assertAgentBrowserPassthroughStaysOnPage(args) + return args + } case 'browserSetViewport': if ( params.mobile === true || diff --git a/src/main/orcad/browser-providers/chromium/external-chromium-profile-path.test.ts b/src/main/orcad/browser-providers/chromium/external-chromium-profile-path.test.ts index e614c67986c..49531682b86 100644 --- a/src/main/orcad/browser-providers/chromium/external-chromium-profile-path.test.ts +++ b/src/main/orcad/browser-providers/chromium/external-chromium-profile-path.test.ts @@ -94,11 +94,4 @@ describe.skipIf(process.platform === 'win32')('external Chromium profile path', await expect(resolve(launcher)).resolves.toBe(stateProfile) }) - - it('leaves non-Linux hosts on the state profile', async () => { - const launcher = await installSnap('chromium', 'name: chromium\n') - environment = { ...environment, platform: 'darwin' } - - await expect(resolve(launcher)).resolves.toBe(join(root, 'state', 'browser-chromium')) - }) }) diff --git a/src/main/orcad/credentials/orcad-plaintext-credential-migration.test.ts b/src/main/orcad/credentials/orcad-plaintext-credential-migration.test.ts index d47691a4db1..156e48ab1a6 100644 --- a/src/main/orcad/credentials/orcad-plaintext-credential-migration.test.ts +++ b/src/main/orcad/credentials/orcad-plaintext-credential-migration.test.ts @@ -55,16 +55,6 @@ describe('migratePlaintextCredentials', () => { expect(logs.join('\n')).not.toContain('lin_api_plaintext') }) - it("stages in its own temp file, never a concurrent writer's shared one", () => { - const path = join(dir, 'linear-token.enc') - writeFileSync(path, 'lin_api_plaintext', 'utf8') - writeFileSync(`${path}.tmp`, 'other-writer-in-progress', 'utf8') - - migratePlaintextCredentials(store, [{ path, raw: true }], log) - expect(readFileSync(`${path}.tmp`, 'utf8')).toBe('other-writer-in-progress') - expect(readStoredCredentialToken('Linear', readFileSync(path))).toBe('lin_api_plaintext') - }) - it('rewrites a plaintext envelope as an encrypted one', () => { const path = join(dir, 'minimax-api-key.enc') const prefix = 'orca-minimax-api-key:v1:' diff --git a/src/main/persistence/scheduling-automations/automation-run-operations.ts b/src/main/persistence/scheduling-automations/automation-run-operations.ts index 50f53935dcb..3e752a7b21e 100644 --- a/src/main/persistence/scheduling-automations/automation-run-operations.ts +++ b/src/main/persistence/scheduling-automations/automation-run-operations.ts @@ -229,6 +229,10 @@ export function updateAutomationRun( startedAt: current.startedAt ?? now, dispatchedAt: result.status === 'dispatched' ? (result.dispatchedAt ?? now) : current.dispatchedAt, + // Launch evidence is durable: the first recorded dispatch-start time survives later rewrites. + ...((current.launchedAt ?? result.launchedAt) !== undefined + ? { launchedAt: current.launchedAt ?? result.launchedAt } + : {}), // Later updates (completion) keep the dispatch's note. ...(result.setupSkippedReason === 'setup-changed' ? { diff --git a/src/main/pi/rpc-runtime-metadata.ts b/src/main/pi/rpc-runtime-metadata.ts index 3193edd9d28..bd184a51b20 100644 --- a/src/main/pi/rpc-runtime-metadata.ts +++ b/src/main/pi/rpc-runtime-metadata.ts @@ -17,6 +17,7 @@ export const PI_RPC_RUNTIME_METADATA: StructuredAgentRuntimeMetadata = { listingNamesConfiguredModel: false, probe: createPiModelCatalogProbe({ resolveEnvironment: () => context.environment.resolveAgentEnvironment('pi'), + resolveSavedEnvironment: () => context.environment.resolveAgentEnvironment('pi', true), refreshEnvironmentAfterMiss: context.environment.refreshAfterMissingProgram, ...(context.deps.resolveAgentCommandSettings ? { resolveCommandSettings: context.deps.resolveAgentCommandSettings } diff --git a/src/main/pi/rpc/launch/rpc-launch-resolution.ts b/src/main/pi/rpc/launch/rpc-launch-resolution.ts index a6b2b1ce698..39784f4ecf4 100644 --- a/src/main/pi/rpc/launch/rpc-launch-resolution.ts +++ b/src/main/pi/rpc/launch/rpc-launch-resolution.ts @@ -76,6 +76,7 @@ export function resolvePiRpcCommand( export function checkPiRpcVersion( launch: { + operation?: StructuredAgentResolutionOperation command: string prefixArgs?: readonly string[] cwd: string @@ -84,7 +85,13 @@ export function checkPiRpcVersion( read?: AgentCliVersionReader ): Promise { return checkAgentCliVersion( - { program: launch.command, prefixArgs: launch.prefixArgs, cwd: launch.cwd, env: launch.env }, + { + program: launch.command, + prefixArgs: launch.prefixArgs, + cwd: launch.cwd, + env: launch.env, + operation: launch.operation + }, PI_RPC_MINIMUM_VERSIONS, read ) diff --git a/src/main/pi/rpc/options/rpc-model-catalog-probe.test.ts b/src/main/pi/rpc/options/rpc-model-catalog-probe.test.ts new file mode 100644 index 00000000000..d58a9d477aa --- /dev/null +++ b/src/main/pi/rpc/options/rpc-model-catalog-probe.test.ts @@ -0,0 +1,88 @@ +import { expect, it, vi, onTestFinished } from 'vitest' +import { createPiModelCatalogProbe } from './rpc-model-catalog-probe' +import type { AgentModelCatalogSuccess } from '../../../native-chat/agent-model-catalog/agent-model-catalog-entry' + +it('publishes a slow inventory independently of state, then keeps the late default', async () => { + vi.useFakeTimers() + onTestFinished(() => { + vi.useRealTimers() + }) + const inventory = Promise.withResolvers() + const state = Promise.withResolvers() + const requested = Promise.withResolvers() + const observations: AgentModelCatalogSuccess[] = [] + const probe = createPiModelCatalogProbe({ + resolveEnvironment: async () => ({}), + resolveCommand: () => process.execPath, + probeVersion: async () => ({ version: '0.84.0' }), + homePath: '/account', + connect: () => ({ + request: (command, _params, options) => { + // The real peer's explicit-null contract retains responses past its default 30s wait. + if (options?.timeoutMs !== null) { + return Promise.reject(new Error('catalog request retained its response deadline')) + } + if (command === 'get_available_models') { + requested.resolve() + return inventory.promise + } + return state.promise + }, + close: async () => {} + }) + }) + const result = probe( + { variable: 'PI_CODING_AGENT_DIR', path: '/account' }, + { publish: (success) => observations.push(success) } + ) + await requested.promise + await vi.advanceTimersByTimeAsync(45_000) + inventory.resolve({ + models: [{ id: 'gpt-live', provider: 'openai', name: 'GPT Live', reasoning: false }] + }) + await vi.advanceTimersByTimeAsync(0) + expect(observations[0]).toMatchObject({ models: [{ id: 'openai/gpt-live' }] }) + expect(observations[0].configured).toBeUndefined() + state.resolve({ model: { id: 'gpt-live', provider: 'openai' }, thinkingLevel: 'high' }) + expect(await result).toMatchObject({ + models: [{ id: 'openai/gpt-live' }], + configured: { model: 'openai/gpt-live', effort: 'high' } + }) + expect(observations[0].configured).toBeUndefined() +}) + +it.each(['empty', 'rejected'])( + 'retains delayed sign-out evidence after %s inventory', + async (kind) => { + const state = Promise.withResolvers() + const requested = Promise.withResolvers() + const probe = createPiModelCatalogProbe({ + resolveEnvironment: async () => ({}), + resolveCommand: () => process.execPath, + probeVersion: async () => ({ version: '0.84.0' }), + homePath: '/account', + connect: () => ({ + request: (command) => { + if (command === 'get_state') { + return state.promise + } + requested.resolve() + return kind === 'empty' + ? Promise.resolve({ models: [] }) + : Promise.reject(new Error('inventory unavailable')) + }, + close: async () => {} + }) + }) + let settled = false + const result = probe({ variable: 'PI_CODING_AGENT_DIR', path: '/account' }).finally(() => { + settled = true + }) + const verdict = expect(result).rejects.toMatchObject({ unavailable: { reason: 'notSignedIn' } }) + await requested.promise + await Promise.resolve() + expect(settled).toBe(false) + state.resolve({ model: { id: 'unknown', provider: 'unknown' } }) + await verdict + } +) diff --git a/src/main/pi/rpc/options/rpc-model-catalog-probe.ts b/src/main/pi/rpc/options/rpc-model-catalog-probe.ts index 4c693c62485..0d76b60f453 100644 --- a/src/main/pi/rpc/options/rpc-model-catalog-probe.ts +++ b/src/main/pi/rpc/options/rpc-model-catalog-probe.ts @@ -1,14 +1,23 @@ +import { agentModelCatalogInputStamp } from '../../../native-chat/agent-model-catalog/agent-model-catalog-input-stamp' import { assertStructuredAgentResolutionCurrent, + type StructuredAgentResolutionOperation, type StructuredAgentCommandSettings } from '../../../native-chat/agent-launch/structured-agent-command-resolution' import { homedir } from 'node:os' import { z } from 'zod' import { requireLegacyAgentSessionAccountHome } from '../../../../shared/agent-session/lifecycle/agent-session-account-home' import type { resolveCliCommand } from '../../../../shared/node-cli-command-resolution' -import type { AgentCliVersionReader } from '../../../agent-launch/preflight/agent-cli-version-probe' +import { + readAgentCliVersion, + type AgentCliVersionReader +} from '../../../agent-launch/preflight/agent-cli-version-probe' import { JsonlRpcAgentConnection } from '../../../jsonl-rpc/agent-connection' -import type { AgentModelCatalogProbe } from '../../../native-chat/agent-model-catalog/agent-model-catalog-discovery' +import type { + AgentModelCatalogProgress, + AgentModelCatalogProbe, + AgentModelCatalogLookup +} from '../../../native-chat/agent-model-catalog/agent-model-catalog-discovery' import type { AgentModelCatalogSuccess } from '../../../native-chat/agent-model-catalog/agent-model-catalog-entry' import { runAgentModelCatalogSession } from '../../../native-chat/agent-model-catalog/agent-model-catalog-probe-runner' import { AgentModelCatalogUnavailableError } from '../../../native-chat/agent-model-catalog/agent-model-catalog-unavailable' @@ -26,6 +35,7 @@ const discoveryStateSchema = z.object({ }) export type PiModelCatalogProbeDeps = { + resolveSavedEnvironment?: () => Promise resolveEnvironment: () => Promise /** Refresh once on a missing Pi binary; absent disables retry. */ refreshEnvironmentAfterMiss?: (askedAt: number) => Promise @@ -54,74 +64,129 @@ function connectPiModelCatalog(launch: ProviderProcessLaunch): PiModelCatalogCon } export function createPiModelCatalogProbe(deps: PiModelCatalogProbeDeps): AgentModelCatalogProbe { - return async (accountHome, options): Promise => { - const operation = options?.operation ?? { signal: options?.signal } + const resolve = async ( + accountHome: Parameters[0], + _workspace: string, + lookup: AgentModelCatalogLookup = 'recover', + operation?: StructuredAgentResolutionOperation + ) => { assertStructuredAgentResolutionCurrent(operation) const cwd = deps.homePath ?? homedir() const { env, command, prefixArgs } = await resolvePiRpcInvocation({ operation, accountDir: requireLegacyAgentSessionAccountHome(accountHome).path, - resolveEnvironment: deps.resolveEnvironment, + resolveEnvironment: + lookup === 'recover' + ? deps.resolveEnvironment + : (deps.resolveSavedEnvironment ?? deps.resolveEnvironment), commandSettings: deps.resolveCommandSettings?.(), resolveCommand: deps.resolveCommand, - refreshEnvironmentAfterMiss: deps.refreshEnvironmentAfterMiss, + refreshEnvironmentAfterMiss: + lookup === 'recover' ? deps.refreshEnvironmentAfterMiss : undefined, resolveWorkingDirectory: () => cwd }) assertStructuredAgentResolutionCurrent(operation) - const release = await checkPiRpcVersion({ command, prefixArgs, cwd, env }, deps.probeVersion) - assertStructuredAgentResolutionCurrent(operation) - if (release.status === 'tooOld') { - throw new Error(`pi at ${command} does not run structured chats`) - } - const launch = buildPiRpcLaunch({ - command, - prefixArgs, - cwd, - ...piRpcSessionlessEnvironment(env), - fullAccess: true - }) - const success = await runAgentModelCatalogSession( - () => - (deps.connect ?? connectPiModelCatalog)({ - ...launch, - args: [...launch.args, '--no-session'] - }), - async (connection): Promise => { - let configured: AgentModelCatalogSuccess['configured'] - let unknownProvider = false - try { - const state = discoveryStateSchema.safeParse(await connection.request('get_state')) - unknownProvider = state.success && state.data.model?.provider === 'unknown' - if (state.success && state.data.model && !unknownProvider) { - configured = { - model: piModelOptionId(state.data.model.provider, state.data.model.id), - ...(typeof state.data.thinkingLevel === 'string' && state.data.thinkingLevel - ? { effort: state.data.thinkingLevel } - : {}) - } - } - } catch { - // Optional default facts cannot prevent a usable inventory from being discovered. - } + return { + identity: { command, prefixArgs, cwd }, + inputStamp: await agentModelCatalogInputStamp({ command, prefixArgs, cwd, env }), + run: async ( + signal?: AbortSignal, + runOperation = operation, + _selectedModels?: readonly string[], + publish?: AgentModelCatalogProgress + ): Promise => { + const operation = runOperation + const options = { signal } assertStructuredAgentResolutionCurrent(operation) - options?.signal?.throwIfAborted() - // Pi's own "No models available" condition: no selected provider and nothing listable. - const models = await readPiRpcModelOptions(connection).catch((error: unknown) => { - if (unknownProvider) { - throw new AgentModelCatalogUnavailableError({ reason: 'notSignedIn' }) - } - throw error - }) - if (unknownProvider && models.length === 0) { - throw new AgentModelCatalogUnavailableError({ reason: 'notSignedIn' }) + const release = await checkPiRpcVersion( + { command, prefixArgs, cwd, env, operation }, + deps.probeVersion ?? ((input) => readAgentCliVersion({ ...input, timeoutMs: null })) + ) + assertStructuredAgentResolutionCurrent(operation) + if (release.status === 'tooOld') { + throw new Error(`pi at ${command} does not run structured chats`) } - return { models, origin: 'probe', ...(configured ? { configured } : {}) } - }, - { label: 'pi', signal: options?.signal } - ) - if (success.models.length === 0) { - throw new Error('pi listed no models') + const launch = buildPiRpcLaunch({ + command, + prefixArgs, + cwd, + ...piRpcSessionlessEnvironment(env), + fullAccess: true + }) + const success = await runAgentModelCatalogSession( + () => + (deps.connect ?? connectPiModelCatalog)({ + ...launch, + args: [...launch.args, '--no-session'] + }), + async (connection): Promise => { + let configured: AgentModelCatalogSuccess['configured'] + let unknownProvider = false + let stateKnown = false + let models: AgentModelCatalogSuccess['models'] | undefined + const progress = () => { + assertStructuredAgentResolutionCurrent(operation) + if (models?.length) { + publish?.( + { models, origin: 'probe', ...(configured ? { configured } : {}) }, + stateKnown + ) + } + } + const state = connection + .request('get_state', undefined, { timeoutMs: null }) + .catch(() => undefined) + .then((response) => { + assertStructuredAgentResolutionCurrent(operation) + const parsed = discoveryStateSchema.safeParse(response) + stateKnown = parsed.success + unknownProvider = parsed.success && parsed.data.model?.provider === 'unknown' + if (parsed.success && parsed.data.model && !unknownProvider) { + configured = { + model: piModelOptionId(parsed.data.model.provider, parsed.data.model.id), + ...(typeof parsed.data.thinkingLevel === 'string' && parsed.data.thinkingLevel + ? { effort: parsed.data.thinkingLevel } + : {}) + } + } + progress() + }) + void state.catch(() => {}) + try { + models = await readPiRpcModelOptions(connection, { timeoutMs: null }) + } catch (error) { + await state + if (unknownProvider) { + throw new AgentModelCatalogUnavailableError({ reason: 'notSignedIn' }) + } + throw error + } + progress() + await state + assertStructuredAgentResolutionCurrent(operation) + options.signal?.throwIfAborted() + if (unknownProvider && models.length === 0) { + throw new AgentModelCatalogUnavailableError({ reason: 'notSignedIn' }) + } + return { models, origin: 'probe', ...(configured ? { configured } : {}) } + }, + { label: 'pi', signal: options?.signal, timeoutMs: null } + ) + if (success.models.length === 0) { + throw new Error('pi listed no models') + } + return success + } } - return success } + return Object.assign>( + async (home, options) => + (await resolve(home, '', 'recover', options?.operation)).run( + options?.signal, + options?.operation, + options?.selectedModels, + options?.publish + ), + { resolve } + ) } diff --git a/src/main/pi/rpc/options/rpc-model-options.ts b/src/main/pi/rpc/options/rpc-model-options.ts index 1c1b0b45c40..4c2936a6201 100644 --- a/src/main/pi/rpc/options/rpc-model-options.ts +++ b/src/main/pi/rpc/options/rpc-model-options.ts @@ -35,9 +35,12 @@ export function parsePiModelOptionId(value: string): { provider: string; modelId /** Native inventory projection shared by running chats and the pre-session catalog. */ export async function readPiRpcModelOptions( - rpc: PiRpcRequester + rpc: PiRpcRequester, + options?: { timeoutMs?: number | null } ): Promise { - const models = modelsSchema.parse(await rpc.request('get_available_models')).models + const models = modelsSchema.parse( + await rpc.request('get_available_models', undefined, options) + ).models return models.map((model) => { const id = piModelOptionId(model.provider, model.id) const efforts = diff --git a/src/main/power/wake-signal-clock.ts b/src/main/power/wake-signal-clock.ts new file mode 100644 index 00000000000..9f3742eac33 --- /dev/null +++ b/src/main/power/wake-signal-clock.ts @@ -0,0 +1,15 @@ +// Wake-type signals that could precede a terminal-host folder denial; times only, kept in memory. +// Electron-free: the desktop shell feeds it, and runtime-reachable daemon code reads it. +export type WakeSignal = 'resume' | 'unlock-screen' | 'user-did-become-active' + +const lastSeenAtMs = new Map() + +export function recordWakeSignal(signal: WakeSignal, nowMs: number = Date.now()): void { + lastSeenAtMs.set(signal, nowMs) +} + +/** Null when the signal has not fired since this app process started. */ +export function msSinceWakeSignal(signal: WakeSignal, nowMs: number): number | null { + const at = lastSeenAtMs.get(signal) + return at === undefined ? null : Math.max(0, nowMs - at) +} diff --git a/src/main/preflight/agents-refreshed-on-host.ts b/src/main/preflight/agents-refreshed-on-host.ts index 8028cb9f90f..78f70ec2d87 100644 --- a/src/main/preflight/agents-refreshed-on-host.ts +++ b/src/main/preflight/agents-refreshed-on-host.ts @@ -1,11 +1,11 @@ -const agentRefreshListeners = new Set<() => void>() +const agentRefreshListeners = new Set<(userInitiated: boolean) => void>() /** An agent refresh is how a user says a CLI or its login changed; host caches drop on it. */ -export function onAgentsRefreshedOnHost(listener: () => void): () => void { +export function onAgentsRefreshedOnHost(listener: (userInitiated: boolean) => void): () => void { agentRefreshListeners.add(listener) return () => agentRefreshListeners.delete(listener) } -export function notifyAgentsRefreshedOnHost(): void { - agentRefreshListeners.forEach((listener) => listener()) +export function notifyAgentsRefreshedOnHost(userInitiated = false): void { + agentRefreshListeners.forEach((listener) => listener(userInitiated)) } diff --git a/src/main/preflight/workspace-agent-detection.ts b/src/main/preflight/workspace-agent-detection.ts index 6b73d7486d9..820a83b18e3 100644 --- a/src/main/preflight/workspace-agent-detection.ts +++ b/src/main/preflight/workspace-agent-detection.ts @@ -157,7 +157,8 @@ export async function assertAgentDetectionHostRunning(host: AgentDetectionHost): export async function refreshAgentsOnHost( host: AgentDetectionHost, - options?: InstalledAgentDetectionOptions + options?: InstalledAgentDetectionOptions, + userInitiated = false ): Promise { if (host.kind === 'ssh') { throw new SshHostNotServedError(host.connectionId) @@ -169,7 +170,7 @@ export async function refreshAgentsOnHost( ? refreshShellPathAndDetectAgents(host.context, options) : refreshShellPathAndDetectAgents(host.context) const result = wsl ? await detect() : await captureHostShellEnvironment(detect) - notifyAgentsRefreshedOnHost() + notifyAgentsRefreshedOnHost(userInitiated && !wsl) return result } return wsl ? refresh() : refreshAtRestCommandContext(refresh) diff --git a/src/main/rate-limits/providers/codex/codex-backend-usage-client.test.ts b/src/main/rate-limits/providers/codex/codex-backend-usage-client.test.ts new file mode 100644 index 00000000000..306b1c1b1e4 --- /dev/null +++ b/src/main/rate-limits/providers/codex/codex-backend-usage-client.test.ts @@ -0,0 +1,60 @@ +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import { + fetchCodexRateLimitsViaBackend, + supplementCodexUsageWindows +} from './codex-backend-usage-client' + +const spendCapPayload = { + plan_type: 'business', + rate_limit: null, + spend_control: { + reached: false, + individual_limit: { remaining_percent: 75, reset_at: 1_785_542_400 } + } +} + +describe('Codex backend spend_control', () => { + let codexHomePath: string + + beforeEach(() => { + codexHomePath = mkdtempSync(join(tmpdir(), 'codex-backend-usage-')) + writeFileSync( + join(codexHomePath, 'auth.json'), + JSON.stringify({ tokens: { access_token: 't' } }) + ) + }) + + afterEach(() => { + rmSync(codexHomePath, { recursive: true, force: true }) + }) + + it('shows the individual spend cap as the monthly window when no 5h/7d window is reported', async () => { + const limits = await fetchCodexRateLimitsViaBackend( + async () => new Response(JSON.stringify(spendCapPayload)), + { codexHomePath } + ) + + expect(limits?.session).toBeNull() + expect(limits?.weekly).toBeNull() + expect(limits?.monthly).toMatchObject({ + usedPercent: 25, + windowMinutes: 43_200, + resetsAt: 1_785_542_400_000 + }) + }) + + it('reads the spend cap from the backend when the app-server reports no usage window', async () => { + const limits = await supplementCodexUsageWindows( + { provider: 'codex', session: null, weekly: null, updatedAt: 1, error: null, status: 'ok' }, + async () => new Response(JSON.stringify(spendCapPayload)), + { codexHomePath } + ) + + expect(limits.session).toBeNull() + expect(limits.weekly).toBeNull() + expect(limits.monthly).toMatchObject({ usedPercent: 25, windowMinutes: 43_200 }) + }) +}) diff --git a/src/main/rate-limits/providers/codex/codex-backend-usage-client.ts b/src/main/rate-limits/providers/codex/codex-backend-usage-client.ts index a63e75a57b8..48fe5eaa2b3 100644 --- a/src/main/rate-limits/providers/codex/codex-backend-usage-client.ts +++ b/src/main/rate-limits/providers/codex/codex-backend-usage-client.ts @@ -13,7 +13,10 @@ import { type CodexBackendRequest } from './codex-backend-auth' import type { CodexRateLimitFetchOptions } from './codex-rate-limit-fetch-options' -import { mapCodexRateLimitWindow } from './codex-rate-limit-window-mapper' +import { + mapCodexIndividualLimitWindow, + mapCodexRateLimitWindow +} from './codex-rate-limit-window-mapper' import { mapBackendRateLimitResetCredits } from './codex-reset-credit-client' type BackendRateLimitWindow = { @@ -28,6 +31,9 @@ type BackendUsageResponse = { primary_window?: BackendRateLimitWindow | null secondary_window?: BackendRateLimitWindow | null } | null + spend_control?: { + individual_limit?: { remaining_percent?: number; reset_at?: number } | null + } | null rate_limit_reset_credits?: Parameters[0] } @@ -79,16 +85,28 @@ export async function fetchCodexRateLimitsViaBackend( primary: backendWindowToSnapshot(payload.rate_limit?.primary_window), secondary: backendWindowToSnapshot(payload.rate_limit?.secondary_window) }) + const session = mapCodexRateLimitWindow( + classified.session, + snapshotWindowMinutes(classified.session, CODEX_SESSION_WINDOW_MINUTES) + ) + const weekly = mapCodexRateLimitWindow( + classified.weekly, + snapshotWindowMinutes(classified.weekly, CODEX_WEEKLY_WINDOW_MINUTES) + ) + const individualLimit = payload.spend_control?.individual_limit + // Why: match the app-server mapping so an RPC fallback does not drop the monthly meter. + const monthly = + session || weekly + ? null + : mapCodexIndividualLimitWindow({ + remainingPercent: individualLimit?.remaining_percent, + resetsAt: individualLimit?.reset_at + }) return { provider: 'codex', - session: mapCodexRateLimitWindow( - classified.session, - snapshotWindowMinutes(classified.session, CODEX_SESSION_WINDOW_MINUTES) - ), - weekly: mapCodexRateLimitWindow( - classified.weekly, - snapshotWindowMinutes(classified.weekly, CODEX_WEEKLY_WINDOW_MINUTES) - ), + session, + weekly, + monthly, planType: payload.plan_type, ...(payload.rate_limit_reset_credits !== undefined ? { @@ -102,12 +120,13 @@ export async function fetchCodexRateLimitsViaBackend( } } -export async function supplementCodexSessionWindow( +export async function supplementCodexUsageWindows( limits: ProviderRateLimits, request: CodexBackendRequest, options?: CodexRateLimitFetchOptions ): Promise { - if (options?.signal?.aborted || limits.session || !limits.weekly) { + // Why: app-servers that omit individualLimit report no window for spend-capped seats (#8664). + if (options?.signal?.aborted || limits.status !== 'ok' || limits.session || limits.monthly) { return limits } try { @@ -117,9 +136,10 @@ export async function supplementCodexSessionWindow( } const rateLimitResetCredits = backend.rateLimitResetCredits ?? limits.rateLimitResetCredits if (!backend.session) { - return rateLimitResetCredits === limits.rateLimitResetCredits + const monthly = limits.weekly ? null : backend.monthly + return !monthly && rateLimitResetCredits === limits.rateLimitResetCredits ? limits - : { ...limits, rateLimitResetCredits } + : { ...limits, monthly, rateLimitResetCredits } } return { ...limits, diff --git a/src/main/rate-limits/providers/codex/codex-fetcher.ts b/src/main/rate-limits/providers/codex/codex-fetcher.ts index deab887eba4..58a38077c64 100644 --- a/src/main/rate-limits/providers/codex/codex-fetcher.ts +++ b/src/main/rate-limits/providers/codex/codex-fetcher.ts @@ -27,7 +27,7 @@ import { getMainHttpClient } from '../../../network/http-client' import { probeCodexAuthPresence } from './codex-auth-presence' import { fetchCodexRateLimitsViaBackend, - supplementCodexSessionWindow + supplementCodexUsageWindows } from './codex-backend-usage-client' import type { CodexRateLimitFetchOptions } from './codex-rate-limit-fetch-options' import { abortedCodexRateLimitResult } from './codex-rate-limit-fetch-result' @@ -132,8 +132,8 @@ async function supplementBackendMetadata( limits: ProviderRateLimits, options?: CodexRateLimitFetchOptions ): Promise { - const withSession = await supplementCodexSessionWindow(limits, requestCodexBackend, options) - return supplementCodexRateLimitResetCredits(withSession, requestCodexBackend, options) + const withWindows = await supplementCodexUsageWindows(limits, requestCodexBackend, options) + return supplementCodexRateLimitResetCredits(withWindows, requestCodexBackend, options) } function codexUnavailable(error: string, status: 'error' | 'unavailable'): ProviderRateLimits { diff --git a/src/main/rate-limits/providers/codex/codex-rate-limit-window-classification.ts b/src/main/rate-limits/providers/codex/codex-rate-limit-window-classification.ts index 426a76a83ea..c425d8b9d2e 100644 --- a/src/main/rate-limits/providers/codex/codex-rate-limit-window-classification.ts +++ b/src/main/rate-limits/providers/codex/codex-rate-limit-window-classification.ts @@ -1,5 +1,6 @@ export const CODEX_SESSION_WINDOW_MINUTES = 300 export const CODEX_WEEKLY_WINDOW_MINUTES = 10080 +export const CODEX_MONTHLY_WINDOW_MINUTES = 43_200 // Older Codex bucket lengths drift by one minute; tolerate that without absorbing other durations. const CODEX_WINDOW_DURATION_TOLERANCE_MINUTES = 1 @@ -13,6 +14,11 @@ export type CodexRateWindowSnapshot = { export type CodexRateLimitWindowsSnapshot = { primary?: CodexRateWindowSnapshot | null secondary?: CodexRateWindowSnapshot | null + /** Per-seat monthly spend cap on workspace plans. */ + individualLimit?: { + remainingPercent?: unknown + resetsAt?: unknown + } | null /** Pay-as-you-go credit balance, when the app-server reports one. */ credits?: { hasCredits?: boolean diff --git a/src/main/rate-limits/providers/codex/codex-rate-limit-window-mapper.ts b/src/main/rate-limits/providers/codex/codex-rate-limit-window-mapper.ts index e5d8c1aa898..2958ef07152 100644 --- a/src/main/rate-limits/providers/codex/codex-rate-limit-window-mapper.ts +++ b/src/main/rate-limits/providers/codex/codex-rate-limit-window-mapper.ts @@ -1,5 +1,9 @@ import type { RateLimitWindow } from '../../../../shared/rate-limit-types' -import type { CodexRateWindowSnapshot } from './codex-rate-limit-window-classification' +import { + CODEX_MONTHLY_WINDOW_MINUTES, + type CodexRateLimitWindowsSnapshot, + type CodexRateWindowSnapshot +} from './codex-rate-limit-window-classification' export function mapCodexRateLimitWindow( raw: CodexRateWindowSnapshot | null | undefined, @@ -36,3 +40,15 @@ export function mapCodexRateLimitWindow( resetDescription } } + +export function mapCodexIndividualLimitWindow( + raw: CodexRateLimitWindowsSnapshot['individualLimit'] +): RateLimitWindow | null { + if (typeof raw?.remainingPercent !== 'number' || !Number.isFinite(raw.remainingPercent)) { + return null + } + return mapCodexRateLimitWindow( + { usedPercent: 100 - raw.remainingPercent, resetsAt: raw.resetsAt }, + CODEX_MONTHLY_WINDOW_MINUTES + ) +} diff --git a/src/main/rate-limits/providers/codex/codex-rpc-rate-limit-mapper.test.ts b/src/main/rate-limits/providers/codex/codex-rpc-rate-limit-mapper.test.ts new file mode 100644 index 00000000000..d8979cc71aa --- /dev/null +++ b/src/main/rate-limits/providers/codex/codex-rpc-rate-limit-mapper.test.ts @@ -0,0 +1,36 @@ +import { describe, expect, it } from 'vitest' +import { mapCodexRpcRateLimits } from './codex-rpc-rate-limit-mapper' + +describe('mapCodexRpcRateLimits individualLimit', () => { + it('shows the spend cap as the monthly window when no 5h/7d window is reported', () => { + const limits = mapCodexRpcRateLimits({ + rateLimits: { + primary: null, + secondary: null, + individualLimit: { remainingPercent: 80, resetsAt: 1_800_000_000 } + } + }) + + expect(limits.session).toBeNull() + expect(limits.weekly).toBeNull() + expect(limits.monthly).toMatchObject({ + usedPercent: 20, + windowMinutes: 43_200, + resetsAt: 1_800_000_000_000 + }) + }) + + it('keeps the 5h/7d windows and ignores the spend cap when they are reported', () => { + const limits = mapCodexRpcRateLimits({ + rateLimits: { + primary: { usedPercent: 40, windowDurationMins: 300 }, + secondary: { usedPercent: 10, windowDurationMins: 10080 }, + individualLimit: { remainingPercent: 80, resetsAt: 1_800_000_000 } + } + }) + + expect(limits.session?.usedPercent).toBe(40) + expect(limits.weekly?.usedPercent).toBe(10) + expect(limits.monthly).toBeNull() + }) +}) diff --git a/src/main/rate-limits/providers/codex/codex-rpc-rate-limit-mapper.ts b/src/main/rate-limits/providers/codex/codex-rpc-rate-limit-mapper.ts index 7cb9fa6fc4c..1424d84f96a 100644 --- a/src/main/rate-limits/providers/codex/codex-rpc-rate-limit-mapper.ts +++ b/src/main/rate-limits/providers/codex/codex-rpc-rate-limit-mapper.ts @@ -6,7 +6,10 @@ import { type CodexRateLimitWindowsSnapshot } from './codex-rate-limit-window-classification' import { mapCodexCredits } from './codex-credit-balance' -import { mapCodexRateLimitWindow } from './codex-rate-limit-window-mapper' +import { + mapCodexIndividualLimitWindow, + mapCodexRateLimitWindow +} from './codex-rate-limit-window-mapper' import { mapRpcRateLimitResetCredits, type RpcRateLimitResetCredits @@ -21,10 +24,16 @@ export function mapCodexRpcRateLimits(wrapper: RpcRateLimitsResponse): ProviderR const classified = classifyCodexRateLimitWindows(wrapper?.rateLimits) const credits = mapRpcRateLimitResetCredits(wrapper?.rateLimitResetCredits) const extraUsage = mapCodexCredits(wrapper?.rateLimits?.credits) + const session = mapCodexRateLimitWindow(classified.session, CODEX_SESSION_WINDOW_MINUTES) + const weekly = mapCodexRateLimitWindow(classified.weekly, CODEX_WEEKLY_WINDOW_MINUTES) + // Why: workspace seats can report only a spend cap; like Claude's spend limit, show it only when no 5h/7d window exists. + const monthly = + session || weekly ? null : mapCodexIndividualLimitWindow(wrapper?.rateLimits?.individualLimit) return { provider: 'codex', - session: mapCodexRateLimitWindow(classified.session, CODEX_SESSION_WINDOW_MINUTES), - weekly: mapCodexRateLimitWindow(classified.weekly, CODEX_WEEKLY_WINDOW_MINUTES), + session, + weekly, + monthly, ...(credits !== undefined ? { rateLimitResetCredits: credits } : {}), ...(extraUsage ? { extraUsage } : {}), updatedAt: Date.now(), diff --git a/src/main/rate-limits/service/service-account-refresh.ts b/src/main/rate-limits/service/service-account-refresh.ts index 0cb65b2070f..7ce699d903d 100644 --- a/src/main/rate-limits/service/service-account-refresh.ts +++ b/src/main/rate-limits/service/service-account-refresh.ts @@ -137,10 +137,11 @@ export abstract class RateLimitServiceAccountRefresh extends RateLimitServiceIna target?: CodexAccountSelectionTarget ): Promise { const nextTarget = normalizeCodexAccountSelectionTarget(target) - // Weekly-only plans still need outgoing switcher snapshots. + // Weekly-only and monthly-only plans still need outgoing switcher snapshots. if ( outgoingAccountId && - (this.state.codex?.session || this.state.codex?.weekly) && + this.state.codex && + hasUsageWindows(this.state.codex) && this.isSameCodexTarget(this.codexFetchTarget, nextTarget) ) { this.inactiveCodexCache.set(outgoingAccountId, this.state.codex) diff --git a/src/main/rate-limits/service/service-inactive-account-previews.test.ts b/src/main/rate-limits/service/service-inactive-account-previews.test.ts index 312507c095d..37a4e826d98 100644 --- a/src/main/rate-limits/service/service-inactive-account-previews.test.ts +++ b/src/main/rate-limits/service/service-inactive-account-previews.test.ts @@ -115,4 +115,21 @@ describe('RateLimitService', () => { } ]) }) + + it('keeps a monthly-only outgoing Codex snapshot for the switcher', async () => { + const service = new RateLimitService() + service.setInactiveCodexAccountsResolver(() => [ + { id: 'account-1', resolveHome: () => ({ kind: 'skip' }) } + ]) + vi.mocked(fetchCodexRateLimits).mockResolvedValue({ + ...okProvider('codex', 0), + session: null, + monthly: { usedPercent: 20, windowMinutes: 43_200, resetsAt: null, resetDescription: null } + }) + await service.refresh() + + await service.refreshForCodexAccountChange('account-1') + + expect(service.getState().inactiveCodexAccounts[0]?.rateLimits?.monthly?.usedPercent).toBe(20) + }) }) diff --git a/src/main/runtime/agent-session/environment/structured-agent-shell-environment-catalog.test.ts b/src/main/runtime/agent-session/environment/structured-agent-shell-environment-catalog.test.ts index 0935e71ddb3..89cf9be8276 100644 --- a/src/main/runtime/agent-session/environment/structured-agent-shell-environment-catalog.test.ts +++ b/src/main/runtime/agent-session/environment/structured-agent-shell-environment-catalog.test.ts @@ -1,3 +1,9 @@ +import { mkdtemp, mkdir, writeFile, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join, delimiter } from 'node:path' +import { resolveCliCommand } from '../../../../shared/node-cli-command-resolution' +import { createCodexModelCatalogProbe } from '../../../codex/structured/catalog/codex-model-catalog-probe' +import { refreshAtRestCommandContext } from '../../../native-chat/agent-session-wire/options/at-rest-command-context' import { agentSessionConversationView } from '../../../../shared/agent-session/lifecycle/agent-session-conversation-view' import { afterEach, expect, it, vi } from 'vitest' import { agentSessionRecordFixture } from '../../../../shared/agent-session/lifecycle/agent-session-record.test-fixture' @@ -11,8 +17,8 @@ import { const shell = vi.hoisted( (): { - current: { PATH: string; IGNORED: string } - next: { PATH: string; IGNORED: string } + current: { PATH: string; Path?: string; path?: string; IGNORED: string } + next: { PATH: string; Path?: string; path?: string; IGNORED: string } saved: string[] nextSaved: string[] } => ({ @@ -68,11 +74,12 @@ function rig(overlay?: Record) { workspaceMayOverrideDefaultModel: defaults }) const environment = createStructuredAgentEnvironmentResolvers({ - resolveShellEnvironmentPolicy: () => ({ inheritAll: false, names: [] }), + resolveShellEnvironmentPolicy: () => ({ inheritAll: false, names: ['IGNORED'] }), resolveAgentLaunchEnv: () => overlay ?? {} }) cleanups.push(service.stop, environment.bindCatalogSources(['grok'])) const listing = (id: string) => ({ + origin: 'probe' as const, models: [{ id, label: id, isDefault: true, efforts: [] }], configuredDefault: { modelId: id, effort: 'high' } }) @@ -86,21 +93,38 @@ function rig(overlay?: Record) { } } -it('keeps a Windows catalog source while only a shadowed inherited path changes', async () => { - vi.stubGlobal('process', { ...process, platform: 'win32' }) - const overlay = { path: '/configured/bin' } - const h = rig(overlay) - const source = h.service.captureSource(h.record) - cleanups.push(() => source.release?.()) - await captureHostShellEnvironment(async () => { - shell.next = { PATH: '/changed', IGNORED: 'a' } - }) - expect(source()).toBe(true) - await captureHostShellEnvironment(async () => { - overlay.path = '/new-configured/bin' - }) - expect(source()).toBe(false) -}) +it.each(['darwin', 'win32'] as const)( + 'keeps a %s catalog source through PATH-only and overlay-shadowed changes', + async (platform) => { + vi.stubGlobal('process', { ...process, platform }) + shell.current = { PATH: '/a', Path: '/shadow-a', path: '/shadow-b', IGNORED: 'a' } + const overlay = { IGNORED: 'configured' } + const h = rig(overlay) + const source = h.service.captureSource(h.record) + cleanups.push(() => source.release?.()) + agentModelCatalogStore.recordSuccess(h.fingerprint, 'grok', h.listing('saved'), 'discovery') + const held = Promise.withResolvers>() + const flight = agentModelCatalogStore.refresh(h.fingerprint, 'grok', () => held.promise) + await Promise.resolve() + await captureHostShellEnvironment(async () => { + shell.next = { + PATH: '/changed', + Path: '/other-shadow-a', + path: '/other-shadow-b', + IGNORED: 'shadowed' + } + }) + expect(source()).toBe(true) + expect(agentModelCatalogStore.get(h.fingerprint)?.models[0]?.id).toBe('saved') + held.resolve(h.listing('current')) + await flight + expect(agentModelCatalogStore.get(h.fingerprint)?.models[0]?.id).toBe('current') + await captureHostShellEnvironment(async () => { + overlay.IGNORED = 'new-configured' + }) + expect(source()).toBe(false) + } +) it('retires a producer on effective Refresh before new publication and fences late defaults and new old reports', async () => { const h = rig() @@ -116,7 +140,7 @@ it('retires a producer on effective Refresh before new publication and fences la h.service.recordLiveListing(h.record.sessionId, h.listing('A'), sourceA) await vi.waitFor(() => expect(h.defaults).toHaveBeenCalledOnce()) await captureHostShellEnvironment(async () => { - shell.next = { PATH: '/b', IGNORED: 'a' } + shell.next = { PATH: '/b', IGNORED: 'b' } }) expect(sourceA()).toBe(false) const sourceB = h.service.captureSource(h.record) @@ -169,3 +193,113 @@ it.each([true, false])( expect(env.CODEX_CI).toBe('set-by-user') } ) + +it('reuses the selected Codex across folders and capture, then follows a different PATH executable', async () => { + const folder = await mkdtemp(join(tmpdir(), 'orca-catalog-capture-')) + const [a, b, bin, replacement, unrelated] = ['a', 'b', 'bin', 'replacement', 'unrelated'].map( + (name) => join(folder, name) + ) + await Promise.all([a, b, bin, replacement, unrelated].map((path) => mkdir(path))) + const executable = process.platform === 'win32' ? 'catalog-codex.cmd' : 'catalog-codex' + await Promise.all( + [bin, replacement].map((path) => + writeFile(join(path, executable), 'test binary identity', { mode: 0o755 }) + ) + ) + shell.current = { + PATH: `../bin${delimiter}./node_modules/.bin${delimiter}${folder}/unused-one${delimiter}`, + IGNORED: 'a' + } + shell.next = { ...shell.current } + const environment = createStructuredAgentEnvironmentResolvers({ + prewarm: false, + resolveShellEnvironmentPolicy: () => ({ inheritAll: false, names: [] }) + }) + const firstStarted = Promise.withResolvers() + const otherStarted = Promise.withResolvers() + const first = Promise.withResolvers() + const other = Promise.withResolvers() + let duplicate = false + const probe = createCodexModelCatalogProbe({ + resolveEnvironment: () => environment.resolveAgentEnvironment('codex'), + resolveSavedEnvironment: () => environment.resolveAgentEnvironment('codex', true), + resolveCommand: (options) => ({ + command: resolveCliCommand('catalog-codex', options), + prefixArgs: [] + }), + runSession: async (invocation, body) => { + let id = 'replacement' + if (invocation.command === join(bin, executable)) { + id = duplicate ? 'duplicate' : 'original' + duplicate = true + firstStarted.resolve() + await first.promise + } + return body({ + request: async (method) => + method === 'model/list' + ? { + data: [ + { + model: id, + displayName: id, + hidden: false, + supportedReasoningEfforts: [], + isDefault: true + } + ], + nextCursor: null + } + : method === 'config/read' + ? { config: {} } + : {}, + notify: () => {} + }) + } + }) + const service = createAgentModelCatalogService({ + store: agentModelCatalogStore, + getConversation: () => undefined, + getRecord: () => undefined, + drivesRecord: () => true, + resolveAccountHome: async () => ({ variable: 'CODEX_HOME', path: folder }), + probes: { codex: probe } + }) + cleanups.push(environment.bindCatalogSources(['codex']), service.stop, () => { + void rm(folder, { recursive: true, force: true }) + }) + const read = (workspacePath: string) => + service.read({ agent: 'codex', workspacePath, waitForListing: true }) + const firstRead = read(a) + await firstStarted.promise + await refreshAtRestCommandContext(() => + captureHostShellEnvironment(async () => { + shell.next = { ...shell.current, PATH: `../bin${delimiter}${folder}/unused-two${delimiter}` } + }) + ) + const shared = read(b) + first.resolve() + await firstRead + expect(await shared).toMatchObject({ models: [{ id: 'original' }] }) + const unrelatedRead = agentModelCatalogStore.refresh( + 'unrelated-input-fixture', + 'codex', + async () => { + otherStarted.resolve() + await other.promise + return { + origin: 'probe', + models: [{ id: 'unrelated', label: 'Unrelated', efforts: [], isDefault: true }] + } + } + ) + await otherStarted.promise + await refreshAtRestCommandContext(() => + captureHostShellEnvironment(async () => { + shell.next = { ...shell.current, PATH: `${replacement}${delimiter}../bin${delimiter}` } + }) + ) + expect(await read(a)).toMatchObject({ models: [{ id: 'replacement' }] }) + other.resolve() + expect(await unrelatedRead).toMatchObject({ models: [{ id: 'unrelated' }] }) +}) diff --git a/src/main/runtime/agent-session/environment/structured-agent-shell-environment.ts b/src/main/runtime/agent-session/environment/structured-agent-shell-environment.ts index 256ce90d5ce..abbe75437ef 100644 --- a/src/main/runtime/agent-session/environment/structured-agent-shell-environment.ts +++ b/src/main/runtime/agent-session/environment/structured-agent-shell-environment.ts @@ -204,7 +204,7 @@ export function createStructuredAgentEnvironmentResolvers( /** The shared base every agent's child env starts from, before its own overlay. */ resolveBaseEnvironment: (cachedOnly?: boolean) => Promise> /** The base with the user's overlay for `agent` laid over it. */ - resolveAgentEnvironment: (agent: string) => Promise> + resolveAgentEnvironment: (agent: string, cachedOnly?: boolean) => Promise> refreshAfterMissingProgram: (askedAt: number) => Promise bindCatalogSources: (agents: readonly string[]) => () => void } { @@ -245,7 +245,16 @@ export function createStructuredAgentEnvironmentResolvers( overlays.get(agent), process.platform ) - return [agent, structuredAgentEnvironmentIdentity(effective)] + return [ + agent, + structuredAgentEnvironmentIdentity( + Object.fromEntries( + Object.entries(effective).filter(([key]) => + process.platform === 'win32' ? key.toUpperCase() !== 'PATH' : key !== 'PATH' + ) + ) + ) + ] }) ) } @@ -256,9 +265,9 @@ export function createStructuredAgentEnvironmentResolvers( } }, resolveBaseEnvironment: resolveBase, - resolveAgentEnvironment: async (agent) => + resolveAgentEnvironment: async (agent, cachedOnly) => overlayEnvironmentVariables( - await resolveBase(), + await resolveBase(cachedOnly), sources.resolveAgentLaunchEnv?.(agent), process.platform ) diff --git a/src/main/runtime/agent-session/model-catalog/structured-agent-model-catalog-discovery.ts b/src/main/runtime/agent-session/model-catalog/structured-agent-model-catalog-discovery.ts index eb1f124e40f..21a52b02ef3 100644 --- a/src/main/runtime/agent-session/model-catalog/structured-agent-model-catalog-discovery.ts +++ b/src/main/runtime/agent-session/model-catalog/structured-agent-model-catalog-discovery.ts @@ -87,6 +87,7 @@ export function acpModelCatalogDiscovery( ...(acpListingNeedsChatFolder(discovery) ? { listsInChatFolder: true as const } : {}), probe: createAcpModelCatalogProbe(spec, { resolveEnvironment: environment.resolveBaseEnvironment, + resolveSavedEnvironment: () => environment.resolveBaseEnvironment(true), refreshEnvironmentAfterMiss: environment.refreshAfterMissingProgram, dataDirectory: deps.stateDirectory, ...(deps.resolveAgentLaunchEnv ? { resolveLaunchEnv: deps.resolveAgentLaunchEnv } : {}), diff --git a/src/main/runtime/client/events/runtime-notifier-contract.ts b/src/main/runtime/client/events/runtime-notifier-contract.ts index 299078fa642..b92d0e77789 100644 --- a/src/main/runtime/client/events/runtime-notifier-contract.ts +++ b/src/main/runtime/client/events/runtime-notifier-contract.ts @@ -131,6 +131,8 @@ export type RuntimeNotifier = { sourceLeafId?: string telemetrySource?: TerminalPaneSplitSource newLeafId?: string + /** An agent's split: the new pane leaves the user's active pane and keyboard focus alone. */ + activate?: false } ): void /** `recordInteraction: false` marks a title the host applied, not one the user typed. */ diff --git a/src/main/runtime/client/settings/runtime-client-settings-shape.ts b/src/main/runtime/client/settings/runtime-client-settings-shape.ts index c59ee8d22e6..cb86e2fdfd2 100644 --- a/src/main/runtime/client/settings/runtime-client-settings-shape.ts +++ b/src/main/runtime/client/settings/runtime-client-settings-shape.ts @@ -25,6 +25,7 @@ export type RuntimeClientSettings = Pick< | 'defaultLinearTeamSelection' | 'githubProjects' | 'experimentalNewWorktreeCardStyle' + | 'worktreeCardDetailsRow' | 'compactWorktreeCards' | 'minimaxGroupId' | 'minimaxUsageModels' @@ -75,6 +76,7 @@ export type RuntimeClientSettingsUpdate = Pick< | 'defaultLinearTeamSelection' | 'githubProjects' | 'experimentalNewWorktreeCardStyle' + | 'worktreeCardDetailsRow' | 'compactWorktreeCards' | 'minimaxGroupId' | 'minimaxUsageModels' diff --git a/src/main/runtime/client/settings/runtime-client-settings.ts b/src/main/runtime/client/settings/runtime-client-settings.ts index 4008aa9c0ad..bb5f03f96c1 100644 --- a/src/main/runtime/client/settings/runtime-client-settings.ts +++ b/src/main/runtime/client/settings/runtime-client-settings.ts @@ -91,6 +91,7 @@ export class RuntimeClientSettingsController { : null, githubProjects: settings.githubProjects, experimentalNewWorktreeCardStyle: settings.experimentalNewWorktreeCardStyle === true, + worktreeCardDetailsRow: settings.worktreeCardDetailsRow !== false, compactWorktreeCards: settings.compactWorktreeCards === true, minimaxGroupId: settings.minimaxGroupId ?? '', minimaxUsageModels: settings.minimaxUsageModels ?? 'general', diff --git a/src/main/runtime/client/settings/runtime-workspace-view-change-events.ts b/src/main/runtime/client/settings/runtime-workspace-view-change-events.ts index 275a24b3159..b1fd9c112f6 100644 --- a/src/main/runtime/client/settings/runtime-workspace-view-change-events.ts +++ b/src/main/runtime/client/settings/runtime-workspace-view-change-events.ts @@ -9,6 +9,7 @@ import type { RuntimeStore } from '../../construction/runtime-store-contract' const WORKSPACE_VIEW_SETTINGS: readonly (keyof GlobalSettings)[] = [ 'compactWorktreeCards', 'experimentalNewWorktreeCardStyle', + 'worktreeCardDetailsRow', 'showPinnedWorktreesInGroups' ] diff --git a/src/main/runtime/construction/runtime-store-contract.ts b/src/main/runtime/construction/runtime-store-contract.ts index 3d976eede42..26469e02989 100644 --- a/src/main/runtime/construction/runtime-store-contract.ts +++ b/src/main/runtime/construction/runtime-store-contract.ts @@ -168,6 +168,7 @@ export type RuntimeStore = { defaultLinearTeamSelection?: GlobalSettings['defaultLinearTeamSelection'] githubProjects?: GlobalSettings['githubProjects'] experimentalNewWorktreeCardStyle?: GlobalSettings['experimentalNewWorktreeCardStyle'] + worktreeCardDetailsRow?: GlobalSettings['worktreeCardDetailsRow'] compactWorktreeCards?: GlobalSettings['compactWorktreeCards'] minimaxGroupId?: GlobalSettings['minimaxGroupId'] minimaxUsageModels?: GlobalSettings['minimaxUsageModels'] diff --git a/src/main/runtime/files/file-reads.ts b/src/main/runtime/files/file-reads.ts index 587faf2088c..4e71c37332c 100644 --- a/src/main/runtime/files/file-reads.ts +++ b/src/main/runtime/files/file-reads.ts @@ -28,7 +28,11 @@ import { windowsFsPath } from '../../wsl/filesystem/wsl-unc-drive-fs-path' import { guardWslUncPathAccess } from '../../wsl/filesystem/wsl-unc-access-gate' import { resolveLocalFileRequestPath } from '../../ipc/filesystem/access/local-file-access-resolution' import type { LocalFileAccess } from '../../../shared/file-access/local-file-access' -import { filePreviewForPath } from '../../../shared/file-preview/file-preview-kind' +import { + filePreviewExtension, + filePreviewForPath +} from '../../../shared/file-preview/file-preview-kind' +import { detectImageFileSignature } from '../../../shared/image-file-signature' import { resolveRuntimeAuthorizedPath } from './chat-scratch-file-root' import { NodeFileReadTooLargeError, @@ -211,15 +215,20 @@ export class RuntimeFileReads { const imagePath = await resolveLocalFileRequestPath(filePath, CHAT_IMAGE_ACCESS, store) // Paths inside project roots skip the rule's type check. const preview = filePreviewForPath(imagePath) - if (preview?.kind !== 'image') { + const extensionless = filePreviewExtension(imagePath) === '' + if (preview?.kind !== 'image' && !extensionless) { throw new Error('not_an_image') } - const { mimeType } = preview const buffer = await readPreviewBytes( windowsFsPath(imagePath), binaryPreviewMaxBytes(maxContentBytes), { regularFileOnly: true } ) + const mimeType = + preview?.kind === 'image' ? preview.mimeType : detectImageFileSignature(buffer)?.mimeType + if (!mimeType) { + throw new Error('not_an_image') + } return assertPreviewWithinTransportBudget( { content: buffer.toString('base64'), isBinary: true, isImage: true, mimeType }, maxContentBytes diff --git a/src/main/runtime/files/orca-runtime-files-preview-budget.test.ts b/src/main/runtime/files/orca-runtime-files-preview-budget.test.ts index aaae2faf6a0..60f4e77ef03 100644 --- a/src/main/runtime/files/orca-runtime-files-preview-budget.test.ts +++ b/src/main/runtime/files/orca-runtime-files-preview-budget.test.ts @@ -1,6 +1,6 @@ import { RuntimeFileReads } from './file-reads' import { afterEach, describe, expect, it, vi } from 'vitest' -import { mkdtemp, rm, writeFile } from 'node:fs/promises' +import { mkdtemp, rm, symlink, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { resolveAuthorizedPathMock, statMock } from './runtime-file-command-mock-registry' @@ -77,6 +77,37 @@ describe('RuntimeFileReads', () => { commands.readChatImagePreview('id:wt-1', join(dir, 'notes.txt')) ).rejects.toThrow('not_an_image') }) + + it.skipIf(process.platform === 'win32')( + 'reads an extensionless chat image by its bytes, inside or outside project roots', + async () => { + const dir = await previewFixture() + const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 0]) + await writeFile(join(dir, 'photo'), png) + await writeFile(join(dir, 'notes'), 'secret') + await symlink(join(dir, 'photo'), join(dir, 'photo.jpg')) + await symlink(join(dir, 'notes'), join(dir, 'notes.jpg')) + const { commands } = createFileReads() + statMock.mockResolvedValue({ isFile: () => true }) + resolveAuthorizedPathMock.mockRejectedValue(new Error('Access denied')) + + await expect( + commands.readChatImagePreview('id:wt-1', join(dir, 'photo.jpg')) + ).resolves.toEqual({ + content: png.toString('base64'), + isBinary: true, + isImage: true, + mimeType: 'image/png' + }) + await expect( + commands.readChatImagePreview('id:wt-1', join(dir, 'notes.jpg')) + ).rejects.toThrow('only show local image files') + resolveAuthorizedPathMock.mockImplementation(async (p: string) => p) + await expect(commands.readChatImagePreview('id:wt-1', join(dir, 'notes'))).rejects.toThrow( + 'not_an_image' + ) + } + ) }) }) diff --git a/src/main/runtime/integrations/reviews/hosted-pr-producer-settlement-test-cases.ts b/src/main/runtime/integrations/reviews/hosted-pr-producer-settlement-test-cases.ts new file mode 100644 index 00000000000..420d5caeb56 --- /dev/null +++ b/src/main/runtime/integrations/reviews/hosted-pr-producer-settlement-test-cases.ts @@ -0,0 +1,235 @@ +import { it, expect, vi, type Mock } from 'vitest' +import { makeCandidate } from '../../../github/pull-requests/refresh/pr-refresh-coordinator-test-harness' +import type { + PRRefreshOutcome, + PRSnapshotReply +} from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' + +export function registerPRProducerSettlementTests({ + read, + mocks, + found +}: { + read: ( + branch: string, + reason?: 'active' | 'visible' | 'swr' | 'manual', + head?: string + ) => Promise + mocks: { resolve: Mock; gate: Mock } + found: () => PRRefreshOutcome +}): void { + it('retires a superseded producer error without waiting for or scheduling over fresh work', async () => { + const timedProvider = Promise.withResolvers() + const freshProvider = Promise.withResolvers() + mocks.resolve + .mockReturnValueOnce(timedProvider.promise) + .mockReturnValueOnce(freshProvider.promise) + const timed = read('superseded-error') + await vi.advanceTimersByTimeAsync(0) + const joined = read('superseded-error') + await vi.advanceTimersByTimeAsync(0) + const fresh = read('superseded-error', 'active') + await vi.advanceTimersByTimeAsync(0) + timedProvider.reject(new Error('obsolete network error')) + expect(await Promise.all([timed, joined])).toEqual([ + { revalidation: { invalidated: true, retryAfterMs: 10_000 } }, + { revalidation: { invalidated: true, retryAfterMs: 10_000 } } + ]) + freshProvider.resolve(found()) + const current = await fresh + expect(await read('superseded-error')).toEqual(current) + expect(mocks.resolve).toHaveBeenCalledTimes(2) + }) + + it('delivers the producer error schedule to cold pending consumers before ordinary retry', async () => { + const { addPRRefreshOutcomeObserver } = + await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const observe = vi.fn() + const remove = addPRRefreshOutcomeObserver(observe) + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(provider.promise) + const cold = read('cold-error') + await vi.advanceTimersByTimeAsync(0) + const joined = read('cold-error') + await vi.advanceTimersByTimeAsync(0) + provider.reject(new Error('network offline')) + const results = await Promise.all([cold, joined]) + expect(results[0]).toMatchObject({ + kind: 'upstream-error', + nextAutoRetryAt: expect.any(Number) + }) + expect(results[1]).toEqual(results[0]) + expect(await read('cold-error')).toEqual(results[0]) + expect(mocks.resolve).toHaveBeenCalledTimes(1) + expect(observe).toHaveBeenCalledTimes(1) + remove() + }) + + it.each(['queued', 'immediate'] as const)( + 'keeps the %s producer policy when another active consumer joins the same task', + async (origin) => { + const { + addPRRefreshOutcomeObserver, + reportVisiblePRRefreshCandidates, + enqueuePRRefresh, + refreshPRNow + } = await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const { setPRRefreshRendererPort } = + await import('../../../github/pull-requests/refresh/pr-refresh-renderer-port') + setPRRefreshRendererPort({ isRendererOpen: () => true, sendToTrustedUI: () => {} }) + const candidate = makeCandidate({ + cacheKey: '/repo::joined-active', + repoId: 'repo', + branch: 'joined-active', + currentHeadOid: 'head-a', + isSelected: true + }) + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(provider.promise) + const observe = vi.fn() + const remove = addPRRefreshOutcomeObserver(observe) + try { + mocks.gate.mockResolvedValue(Date.now() + 60_000) + reportVisiblePRRefreshCandidates([candidate], 1, 1) + await vi.advanceTimersByTimeAsync(0) + expect(mocks.resolve).not.toHaveBeenCalled() + mocks.gate.mockResolvedValue(null) + let producer: Promise | undefined + if (origin === 'queued') { + enqueuePRRefresh(candidate, 'active', 80) + } else { + producer = refreshPRNow(candidate, 'active') + } + await vi.advanceTimersByTimeAsync(0) + expect(mocks.resolve).toHaveBeenCalledTimes(1) + const sibling = refreshPRNow(candidate, 'active') + await vi.advanceTimersByTimeAsync(0) + const hosted = read('joined-active') + await vi.advanceTimersByTimeAsync(0) + expect(mocks.resolve).toHaveBeenCalledTimes(1) + provider.reject(new Error('network offline')) + const result = await hosted + expect(result).toMatchObject({ + kind: 'upstream-error', + nextAutoRetryAt: expect.any(Number) + }) + expect(await sibling).toEqual(result) + if (producer) { + expect(await producer).toEqual(result) + } + expect(await read('joined-active')).toEqual(result) + expect(mocks.resolve).toHaveBeenCalledTimes(1) + expect(observe).toHaveBeenCalledTimes(1) + } finally { + remove() + setPRRefreshRendererPort(null) + } + } + ) + + it.each([false, true])( + 'observes one joined success while fencing a genuine successor (userRefresh=%s)', + async (userRefresh) => { + const { addPRRefreshOutcomeObserver, refreshPRNow } = + await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const candidate = makeCandidate({ + cacheKey: '/repo::joined-success', + repoId: 'repo', + branch: 'joined-success', + currentHeadOid: 'head-a' + }) + const first = Promise.withResolvers() + const successor = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(first.promise).mockReturnValueOnce(successor.promise) + const observe = vi.fn() + const remove = addPRRefreshOutcomeObserver(observe) + try { + const producer = refreshPRNow(candidate, 'active') + await vi.advanceTimersByTimeAsync(0) + const sibling = refreshPRNow(candidate, 'active', { userRefresh }) + await vi.advanceTimersByTimeAsync(0) + expect(mocks.resolve).toHaveBeenCalledTimes(userRefresh ? 2 : 1) + const joined = read('joined-success') + await vi.advanceTimersByTimeAsync(0) + first.resolve(found()) + await producer + if (userRefresh) { + expect(observe).not.toHaveBeenCalled() + successor.resolve(found()) + } + const result = await sibling + expect(await joined).toEqual(result) + expect(result).toMatchObject({ kind: 'found' }) + expect(observe).toHaveBeenCalledTimes(1) + } finally { + remove() + } + } + ) + + it.each(['queued', 'immediate', 'headless-immediate', 'foreground'] as const)( + 'settles the %s producer before its hosted pending error consumer', + async (origin) => { + const { addPRRefreshOutcomeObserver, reportVisiblePRRefreshCandidates, refreshPRNow } = + await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const { setPRRefreshRendererPort } = + await import('../../../github/pull-requests/refresh/pr-refresh-renderer-port') + if (origin === 'queued' || origin === 'immediate') { + setPRRefreshRendererPort({ isRendererOpen: () => true, sendToTrustedUI: () => {} }) + } + const immediate = origin === 'immediate' || origin === 'headless-immediate' + const candidate = makeCandidate({ + cacheKey: '/repo::origin-error', + repoId: 'repo', + branch: 'origin-error', + currentHeadOid: 'head-a', + isSelected: true + }) + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(provider.promise) + const observe = vi.fn() + const remove = addPRRefreshOutcomeObserver(observe) + try { + let producer: Promise | undefined + if (origin === 'foreground') { + producer = read('origin-error', 'active') + } else { + if (immediate) { + mocks.gate.mockResolvedValue(Date.now() + 60_000) + } + reportVisiblePRRefreshCandidates([candidate], 1, 1) + await vi.advanceTimersByTimeAsync(0) + if (immediate) { + expect(mocks.resolve).not.toHaveBeenCalled() + mocks.gate.mockResolvedValue(null) + producer = refreshPRNow(candidate, 'manual') + } + } + await vi.advanceTimersByTimeAsync(0) + expect(mocks.resolve).toHaveBeenCalledTimes(1) + const joined = read('origin-error') + await vi.advanceTimersByTimeAsync(0) + provider.reject(new Error('network offline')) + const result = await joined + expect(result).toMatchObject({ kind: 'upstream-error' }) + if (!('kind' in result) || result.kind !== 'upstream-error') { + throw new Error('expected the actual provider failure') + } + if (origin === 'foreground' || origin === 'headless-immediate') { + expect(result.nextAutoRetryAt).toBeUndefined() + } else { + expect(result.nextAutoRetryAt).toEqual(expect.any(Number)) + } + if (producer) { + expect(await producer).toEqual(result) + } + await vi.advanceTimersByTimeAsync(0) + expect(mocks.resolve).toHaveBeenCalledTimes(1) + expect(observe).toHaveBeenCalledTimes(1) + } finally { + remove() + setPRRefreshRendererPort(null) + } + } + ) +} diff --git a/src/main/runtime/integrations/reviews/hosted-pr-selected-demand.test.ts b/src/main/runtime/integrations/reviews/hosted-pr-selected-demand.test.ts new file mode 100644 index 00000000000..d241badea7a --- /dev/null +++ b/src/main/runtime/integrations/reviews/hosted-pr-selected-demand.test.ts @@ -0,0 +1,157 @@ +import { expect, it, vi } from 'vitest' +import { commands, found, mocks, read, rpcRead } from './runtime-hosted-pr-snapshot-test-harness' +import { PrForBranch } from '../../../../shared/rpc-contract/github/github-pull-request-params' + +it('promotes a shared selected demand without losing other rows or renewing its attempt', async () => { + const branches = ['ordinary-a', 'selected-a', 'selected-b'] + await Promise.all(branches.map((branch) => read(branch, 'active'))) + await vi.advanceTimersByTimeAsync(60_000) + // ast-grep-ignore: test-no-wall-clock -- The imported runtime fixture installs the fake clock. + const gate = Date.now() + 60_000 + mocks.gate.mockResolvedValue(gate) + const starts: string[] = [] + mocks.resolve.mockImplementation(async ({ branchName }) => { + starts.push(branchName) + return found() + }) + const demand = (branch: string, selected = false) => + rpcRead({ + repo: 'repo', + branch, + currentHeadOid: 'head-a', + reason: 'swr', + acceptRevalidationOnly: true, + selected + }) + const waiting = branches.map((branch) => demand(branch)) + await vi.advanceTimersByTimeAsync(25_000) + await Promise.all(waiting) + expect(starts).toEqual([]) + await demand('selected-a', true) + await demand('selected-b', true) + await demand('selected-a', false) + const { schedulePRWatchRead } = + await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const { makeCandidate } = + await import('../../../github/pull-requests/refresh/pr-refresh-coordinator-test-harness') + schedulePRWatchRead( + { prKey: 'github.com/acme/repo#12' }, + makeCandidate({ + repoId: 'repo', + branch: 'watched', + cacheKey: '/repo::watched', + currentHeadOid: 'head-a' + }), + gate + ) + mocks.gate.mockResolvedValue(null) + await vi.advanceTimersByTimeAsync(65_000) + expect(starts).toEqual(['selected-a', 'ordinary-a', 'selected-b', 'watched']) + + // A selected request stays finite even when its only waiter detaches. + await read('expires', 'active') + await vi.advanceTimersByTimeAsync(60_000) + // ast-grep-ignore: test-no-wall-clock -- The imported runtime fixture installs the fake clock. + mocks.gate.mockResolvedValue(Date.now() + 7 * 60_000) + const abort = new AbortController() + const expires = commands.getRepoPRForBranch( + 'repo', + 'expires', + null, + null, + false, + 'head-a', + 'swr', + false, + undefined, + abort.signal, + true, + true + ) + const cancelled = expect(expires).rejects.toThrow('cancelled') + await vi.advanceTimersByTimeAsync(0) + abort.abort() + await cancelled + await vi.advanceTimersByTimeAsync(4 * 60_000) + await demand('expires', true) + const beforeExpiry = starts.length + await vi.advanceTimersByTimeAsync(4 * 60_000) + expect(starts.length).toBe(beforeExpiry) +}) + +it('keeps selected preference optional for old snapshot request parsers', () => { + const params = { repo: 'repo', branch: 'main', reason: 'swr', selected: true } + expect(PrForBranch.parse(params)).toMatchObject({ selected: true }) + expect(PrForBranch.omit({ selected: true }).parse(params)).toEqual({ + repo: 'repo', + branch: 'main', + reason: 'swr' + }) + expect(PrForBranch.parse({ repo: 'repo', branch: 'main' })).toEqual({ + repo: 'repo', + branch: 'main' + }) +}) + +it('keeps a selected join during an awaited quota probe and its later paused admission', async () => { + await read('quiet-selected', 'active') + await vi.advanceTimersByTimeAsync(60_000) + const confirmation = read('quiet-selected') + await vi.advanceTimersByTimeAsync(0) + await confirmation + const branches = ['ordinary-a', 'ordinary-b', 'ordinary-c'] + await Promise.all(branches.map((branch) => read(branch, 'active'))) + await vi.advanceTimersByTimeAsync(300_000) + const quotaProbe = Promise.withResolvers() + // ast-grep-ignore: test-no-wall-clock -- The imported runtime fixture installs the fake clock. + const resetAt = Date.now() + 60_000 + mocks.gate.mockResolvedValue(resetAt).mockReturnValueOnce(quotaProbe.promise) + const starts: string[] = [] + mocks.resolve.mockImplementation(async ({ branchName }) => { + starts.push(branchName) + return found() + }) + const demand = (branch: string, selected = false) => + rpcRead({ + repo: 'repo', + branch, + currentHeadOid: 'head-a', + reason: 'swr', + acceptRevalidationOnly: true, + selected + }) + const first = demand('quiet-selected') + await vi.advanceTimersByTimeAsync(0) + expect(mocks.gate).toHaveBeenCalled() + expect(starts).toEqual([]) + const joined = demand('quiet-selected', true) + const ordinary = branches.map((branch) => demand(branch)) + await vi.advanceTimersByTimeAsync(0) + quotaProbe.resolve(resetAt) + await vi.advanceTimersByTimeAsync(25_000) + await Promise.all([first, joined, ...ordinary]) + mocks.gate.mockResolvedValue(null) + await vi.advanceTimersByTimeAsync(65_000) + expect(starts).toEqual(['quiet-selected', 'ordinary-a', 'ordinary-b', 'ordinary-c']) +}) + +it('uses selected demand only for admission priority, preserving the shared retry interval', async () => { + await read('retry-policy', 'active') + await vi.advanceTimersByTimeAsync(60_000) + mocks.resolve.mockRejectedValueOnce(new Error('offline')) + // ast-grep-ignore: test-no-wall-clock -- The imported runtime fixture installs the fake clock. + const requestedAt = Date.now() + const response = rpcRead({ + repo: 'repo', + branch: 'retry-policy', + currentHeadOid: 'head-a', + reason: 'swr', + acceptRevalidationOnly: true, + selected: true + }) + await vi.advanceTimersByTimeAsync(0) + expect(await response).toMatchObject({ + kind: 'upstream-error', + nextAutoRetryAt: requestedAt + 120_000 + }) +}) diff --git a/src/main/runtime/integrations/reviews/runtime-hosted-pr-deadlines.test.ts b/src/main/runtime/integrations/reviews/runtime-hosted-pr-deadlines.test.ts new file mode 100644 index 00000000000..a12fd68ed62 --- /dev/null +++ b/src/main/runtime/integrations/reviews/runtime-hosted-pr-deadlines.test.ts @@ -0,0 +1,142 @@ +import { expect, it, vi } from 'vitest' +import type { PRRefreshOutcome } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { makeCandidate } from '../../../github/pull-requests/refresh/pr-refresh-coordinator-test-harness' +import { fixture, found, mocks, read, rpcRead } from './runtime-hosted-pr-snapshot-test-harness' + +it('delivers post-push data from its fresh producer while an older timed producer is pending', async () => { + const coordinator = await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const old = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(old.promise) + const producer = read('pushed', 'swr') + await vi.advanceTimersByTimeAsync(0) + const delivered: PRRefreshOutcome[] = [] + const remove = coordinator.addPRRefreshOutcomeObserver((_candidate, outcome) => + delivered.push(outcome) + ) + try { + fixture.title = 'after push' + coordinator.enqueuePRRefresh( + makeCandidate({ branch: 'pushed', cacheKey: '/repo::pushed', currentHeadOid: 'head-a' }), + 'post-push' + ) + await vi.advanceTimersByTimeAsync(2_500) + expect(delivered).toContainEqual({ + kind: 'found', + fetchedAt: 3_500, + pr: expect.objectContaining({ title: 'after push' }) + }) + old.resolve(found()) + await producer + } finally { + remove() + } +}) + +it('delivers an unrelated row without a spacing delay after joining an existing producer', async () => { + const coordinator = await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const result = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(result.promise) + const producer = read('joining', 'active') + await vi.advanceTimersByTimeAsync(0) + const delivered: { branch: string; outcome: PRRefreshOutcome }[] = [] + const remove = coordinator.addPRRefreshOutcomeObserver((candidate, outcome) => { + delivered.push({ branch: candidate.branch, outcome }) + }) + try { + for (const branch of ['joining', 'unrelated']) { + coordinator.enqueuePRRefresh( + makeCandidate({ branch, cacheKey: `/repo::${branch}`, currentHeadOid: 'head-a' }), + 'swr' + ) + } + await vi.advanceTimersByTimeAsync(0) + fixture.title = 'shared answer' + result.resolve(found()) + await producer + await vi.advanceTimersByTimeAsync(0) + expect(delivered).toContainEqual({ + branch: 'unrelated', + outcome: { + kind: 'found', + fetchedAt: 1_000, + pr: expect.objectContaining({ title: 'shared answer' }) + } + }) + } finally { + remove() + } +}) + +it.each([false, true])( + 'delivers post-push data while another row waits for a later rate-limit reset (clock jump: %s)', + async (clockJump) => { + const coordinator = await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const delivered: PRRefreshOutcome[] = [] + const remove = coordinator.addPRRefreshOutcomeObserver((_candidate, outcome) => + delivered.push(outcome) + ) + const candidate = (branch: string) => + makeCandidate({ + branch, + cacheKey: `/repo::${branch}`, + currentHeadOid: 'head-a' + }) + try { + fixture.title = 'after push' + mocks.gate.mockImplementation(async (row) => (row.branch === 'later' ? 61_000 : null)) + if (clockJump) { + await coordinator.refreshPRNow(candidate('later')) + vi.setSystemTime(301_000) + } + coordinator.enqueuePRRefresh(candidate('pushed'), 'post-push') + await vi.advanceTimersByTimeAsync(0) + if (!clockJump) { + await coordinator.refreshPRNow(candidate('later')) + } + await vi.advanceTimersByTimeAsync(2_500) + expect(delivered).toContainEqual({ + kind: 'found', + fetchedAt: clockJump ? 303_500 : 3_500, + pr: expect.objectContaining({ title: 'after push' }) + }) + } finally { + remove() + } + } +) + +it.each(['deadline', 'abort'] as const)( + 'detaches an automatic cold-read waiter on %s while its producer completes for another client', + async (end) => { + const result = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(result.promise) + const producer = read('pending', 'active') + await vi.advanceTimersByTimeAsync(0) + const controller = new AbortController() + const joined = rpcRead( + { + repo: 'repo', + branch: 'pending', + currentHeadOid: 'head-a', + reason: 'swr', + acceptRevalidationOnly: true + }, + controller.signal + ) + const abandoned = + end === 'abort' + ? expect(joined).rejects.toThrow('cancelled') + : expect(joined).resolves.toMatchObject({ revalidation: { retryAfterMs: 10_000 } }) + await vi.advanceTimersByTimeAsync(0) + if (end === 'abort') { + controller.abort() + } else { + await vi.advanceTimersByTimeAsync(25_000) + } + await abandoned + fixture.title = 'producer completed' + result.resolve(found()) + await expect(producer).resolves.toMatchObject({ pr: { title: 'producer completed' } }) + await expect(read('pending')).resolves.toMatchObject({ pr: { title: 'producer completed' } }) + } +) diff --git a/src/main/runtime/integrations/reviews/runtime-hosted-pr-owner-delivery.test.ts b/src/main/runtime/integrations/reviews/runtime-hosted-pr-owner-delivery.test.ts new file mode 100644 index 00000000000..244cfff8ffd --- /dev/null +++ b/src/main/runtime/integrations/reviews/runtime-hosted-pr-owner-delivery.test.ts @@ -0,0 +1,105 @@ +import { expect, it, vi } from 'vitest' +import type { PRRefreshOutcome } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { commands, fixture, found, mocks, read } from './runtime-hosted-pr-snapshot-test-harness' + +it('returns completed legacy cold and pending replies without fallback IO or a successor wait', async () => { + const { preparePRBranchRead } = + await import('../../../github/client/lookup/pr-for-branch-outcome') + const { addPRRefreshOutcomeObserver } = + await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const releases: (() => void)[] = [] + const observe = vi.fn() + const remove = addPRRefreshOutcomeObserver(observe) + try { + for (let index = 0; index < 256; index++) { + const branch = `protected-${index}` + await read(branch, 'active') + const prepared = await preparePRBranchRead('/repo', branch, null, null, null, { + currentHeadOid: 'head-a' + }) + releases.push(prepared.hold()) + } + mocks.resolve.mockClear() + observe.mockClear() + for (const freshFirst of [true, false]) { + const branch = freshFirst ? 'legacy-finished-successor' : 'legacy-running-successor' + const old = Promise.withResolvers() + const fresh = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(old.promise).mockReturnValueOnce(fresh.promise) + const legacy = () => + commands.getRepoPRForBranch('repo', branch, null, null, false, 'head-a', 'swr') + const first = legacy() + await vi.advanceTimersByTimeAsync(0) + const sibling = legacy() + await vi.advanceTimersByTimeAsync(0) + const foreground = read(branch, 'active') + await vi.advanceTimersByTimeAsync(0) + const delivered = vi.fn() + const completed = Promise.all([first, sibling]).then(delivered) + try { + expect(await read(branch)).toEqual({ + revalidation: { invalidated: true, retryAfterMs: 10_000 } + }) + if (freshFirst) { + fresh.resolve(found()) + await foreground + } + const own: PRRefreshOutcome = { kind: 'no-pr', fetchedAt: 1_000 } + old.resolve(own) + await vi.advanceTimersByTimeAsync(0) + expect(delivered).toHaveBeenCalledWith([own, own]) + } finally { + old.resolve({ kind: 'no-pr', fetchedAt: 1_000 }) + fresh.resolve(found()) + await Promise.all([completed, foreground]) + } + } + expect(mocks.resolve).toHaveBeenCalledTimes(4) + expect(observe).toHaveBeenCalledTimes(2) + } finally { + for (const release of releases) { + release() + } + remove() + } + fixture.title = 'legitimate automatic read' + expect(await read('after-capacity')).toMatchObject({ pr: { title: 'legitimate automatic read' } }) +}) + +it.each(['generation-cold', 'account-cold', 'account-direct'] as const)( + 'fences %s settlement before obsolete error policy and observation', + async (change) => { + const { addPRRefreshOutcomeObserver, _getPRRefreshErrorBackoffCountForTests } = + await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const { invalidateHostedReviewBranchCache } = + await import('../../../source-control/hosted-review-branch-cache') + const observe = vi.fn() + const remove = addPRRefreshOutcomeObserver(observe) + const provider = Promise.withResolvers() + mocks.account.mockReturnValue({ ghAccount: { host: 'github.com', user: 'before' } }) + mocks.resolve.mockReturnValueOnce(provider.promise) + const pending = read('scope', change === 'account-direct' ? 'active' : 'swr') + await vi.advanceTimersByTimeAsync(0) + if (change === 'generation-cold') { + invalidateHostedReviewBranchCache('/repo', 'local') + } else { + mocks.account.mockReturnValue({ ghAccount: { host: 'github.com', user: 'after' } }) + } + try { + provider.reject(new Error('network offline')) + const result = await pending + if (change === 'account-direct') { + expect(result).toMatchObject({ kind: 'upstream-error' }) + } else { + expect(result).toEqual({ revalidation: { invalidated: true, retryAfterMs: 10_000 } }) + } + expect(observe).not.toHaveBeenCalled() + expect(_getPRRefreshErrorBackoffCountForTests()).toBe(0) + fixture.title = 'current scope' + expect(await read('scope', 'active')).toMatchObject({ pr: { title: 'current scope' } }) + expect(observe).toHaveBeenCalledTimes(1) + } finally { + remove() + } + } +) diff --git a/src/main/runtime/integrations/reviews/runtime-hosted-pr-snapshot-test-harness.ts b/src/main/runtime/integrations/reviews/runtime-hosted-pr-snapshot-test-harness.ts new file mode 100644 index 00000000000..0f617d38b8a --- /dev/null +++ b/src/main/runtime/integrations/reviews/runtime-hosted-pr-snapshot-test-harness.ts @@ -0,0 +1,110 @@ +// oxlint-disable-next-line typescript/triple-slash-reference -- Renderer integration tests also compile this main fixture with its ambient build constants. +/// +import { afterEach, beforeEach, vi, type Mock } from 'vitest' +import { makePR } from '../../../github/pull-requests/refresh/pr-refresh-coordinator-test-harness' +import type { PRRefreshOutcome } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import type { RuntimeHostedReviewCommands } from './runtime-hosted-review-commands' +import type { RpcContext } from '../../rpc/core' + +const mocks: { resolve: Mock; account: Mock; gate: Mock } = vi.hoisted(() => ({ + resolve: vi.fn(), + account: vi.fn(), + gate: vi.fn() +})) +export { mocks } +vi.mock('../../../github/accounts/github-project-account', () => ({ + githubPRPolicyProjects: () => ({ source: null, selection: String(mocks.account()) }), + resolveGitHubProjectAccount: mocks.account +})) +vi.mock('../../../github/execution/github-operation-slots', () => ({ + acquire: async () => {}, + release: () => {} +})) +vi.mock('../../../github/client/lookup/branch-lookup-resolution', () => ({ + resolvePRForBranchOutcome: mocks.resolve +})) +vi.mock('../../../github/pull-requests/refresh/pr-refresh-rate-limit-gate', () => ({ + prRefreshRateLimitPausedUntil: mocks.gate +})) + +export let commands: RuntimeHostedReviewCommands +export const fixture = { title: 'before' } +export const found = (): PRRefreshOutcome => ({ + kind: 'found', + fetchedAt: Date.now(), + pr: makePR({ + title: fixture.title, + headSha: 'head-a', + checksStatus: 'success', + checksReadComplete: true, + checksFinal: true, + autoMergeEnabled: false, + mergeQueueRequired: false + }) +}) +export const read = ( + branch: string, + reason: 'active' | 'visible' | 'swr' | 'manual' = 'swr', + head = 'head-a' +) => + commands.getRepoPRForBranch( + 'repo', + branch, + null, + null, + false, + head, + reason, + undefined, + undefined, + undefined, + true + ) + +beforeEach(async () => { + vi.resetModules() + vi.useFakeTimers() + vi.setSystemTime(1_000) + fixture.title = 'before' + mocks.resolve.mockReset().mockImplementation(async () => found()) + mocks.account.mockReset().mockReturnValue(null) + mocks.gate.mockReset().mockResolvedValue(null) + // Resolve the real handler before fake-clock assertions begin. + await import('../../rpc/methods/source-control/github/github-pull-request-methods') + const { RuntimeHostedReviewCommands: Commands } = await import('./runtime-hosted-review-commands') + const repo = { + id: 'repo', + path: '/repo', + kind: 'git' as const, + displayName: 'repo', + badgeColor: 'blue', + addedAt: 0 + } + commands = new Commands({ + resolveRepo: async () => repo, + resolveTarget: async () => ({ repo, repoPath: repo.path }), + getExecutionOptions: () => undefined, + recordCreated: () => {} + }) +}) +afterEach(() => { + vi.clearAllTimers() + vi.useRealTimers() +}) + +export async function rpcRead(params: unknown, signal?: AbortSignal) { + const { GITHUB_PULL_REQUEST_METHODS } = + await import('../../rpc/methods/source-control/github/github-pull-request-methods') + const { PrForBranch } = + await import('../../../../shared/rpc-contract/github/github-pull-request-params') + const { repo, branch, ...options } = PrForBranch.parse(params) + const parsed = { repo, branch, ...options } + const runtime = { getRepoPRForBranch: commands.getRepoPRForBranch.bind(commands) } + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: this handler only needs the bound real PR command and signal. + const context = { runtime, signal } as unknown as RpcContext + const method = GITHUB_PULL_REQUEST_METHODS.find((entry) => entry.name === 'github.prForBranch') + if (!method) { + throw new Error('PR RPC method is missing') + } + return method.handler(parsed, context) +} diff --git a/src/main/runtime/integrations/reviews/runtime-hosted-pr-snapshots.test.ts b/src/main/runtime/integrations/reviews/runtime-hosted-pr-snapshots.test.ts new file mode 100644 index 00000000000..5b078a94251 --- /dev/null +++ b/src/main/runtime/integrations/reviews/runtime-hosted-pr-snapshots.test.ts @@ -0,0 +1,576 @@ +import { describe, expect, it, vi } from 'vitest' +import { makeCandidate } from '../../../github/pull-requests/refresh/pr-refresh-coordinator-test-harness' +import type { PRRefreshOutcome } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' +import { registerPRProducerSettlementTests } from './hosted-pr-producer-settlement-test-cases' +import { commands, fixture, found, mocks, read } from './runtime-hosted-pr-snapshot-test-harness' + +describe('hosted PR snapshots', () => { + it('shares true-aged timer data while legacy discovery and actions read current data', async () => { + await read('shared', 'active') + fixture.title = 'after' + expect(await read('shared')).toMatchObject({ + kind: 'found', + pr: { title: 'before' }, + fetchedAt: 1_000 + }) + vi.setSystemTime(2_000) + expect(await read('shared', 'visible')).toMatchObject({ + kind: 'found', + pr: { title: 'after' }, + fetchedAt: 2_000 + }) + fixture.title = 'manual' + expect(await read('shared', 'manual')).toMatchObject({ kind: 'found', pr: { title: 'manual' } }) + }) + + it('keeps a confirmed unchanged quiet result, but a different request head reads again', async () => { + await read('quiet', 'active') + await vi.advanceTimersByTimeAsync(60_000) + const confirmation = read('quiet') + await vi.advanceTimersByTimeAsync(0) + expect(await confirmation).toMatchObject({ fetchedAt: 61_000 }) + await vi.advanceTimersByTimeAsync(120_000) + fixture.title = 'new provider state' + expect(await read('quiet')).toMatchObject({ pr: { title: 'before' }, fetchedAt: 61_000 }) + expect(await read('quiet', 'swr', 'head-b')).toMatchObject({ + pr: { title: 'new provider state' }, + fetchedAt: 181_000 + }) + }) + + it('returns completed checks in each due response for three queued rows', async () => { + await Promise.all(['one', 'two', 'three'].map((branch) => read(branch, 'active'))) + fixture.title = 'finished checks' + mocks.resolve.mockImplementation( + () => + new Promise((resolve) => { + // ast-grep-ignore: test-no-real-time-wait -- Fake-clock provider delay exercises host admission and completion. + setTimeout(() => resolve(found()), 1_000) + }) + ) + await vi.advanceTimersByTimeAsync(60_000) + const responses = Promise.all(['one', 'two', 'three'].map((branch) => read(branch))) + await vi.advanceTimersByTimeAsync(21_000) + expect(await responses).toEqual([ + expect.objectContaining({ pr: expect.objectContaining({ title: 'finished checks' }) }), + expect.objectContaining({ pr: expect.objectContaining({ title: 'finished checks' }) }), + expect.objectContaining({ pr: expect.objectContaining({ title: 'finished checks' }) }) + ]) + }) + + it('delivers a deferred provider failure to the next timer instead of reviving old success', async () => { + await read('slow', 'active') + const provider = Promise.withResolvers() + mocks.resolve.mockImplementation(() => provider.promise) + await vi.advanceTimersByTimeAsync(60_000) + const waiting = read('slow') + await vi.advanceTimersByTimeAsync(25_000) + expect(await waiting).toMatchObject({ + kind: 'found', + fetchedAt: 1_000, + revalidation: { retryAfterMs: 10_000 } + }) + provider.reject(new Error('network down')) + await vi.advanceTimersByTimeAsync(0) + expect(await read('slow')).toMatchObject({ kind: 'upstream-error' }) + mocks.resolve.mockImplementation(async () => found()) + fixture.title = 'manual recovery' + expect(await read('slow', 'manual')).toMatchObject({ pr: { title: 'manual recovery' } }) + }) + + it('retrieves a queued completion on its short retry after local work delays admission', async () => { + await Promise.all(['one', 'two', 'three'].map((branch) => read(branch, 'active'))) + mocks.resolve.mockImplementation( + ({ branchName }) => + new Promise((resolve) => { + // ast-grep-ignore: test-no-real-time-wait -- Fake-clock provider delay exercises host admission and completion. + setTimeout(() => resolve(found()), branchName === 'blocking' ? 6_000 : 1_000) + }) + ) + await vi.advanceTimersByTimeAsync(60_000) + const { enqueuePRRefresh } = + await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + enqueuePRRefresh(makeCandidate({ branch: 'blocking' }), 'active') + await vi.advanceTimersByTimeAsync(0) + fixture.title = 'delivered' + const responses = Promise.all(['one', 'two', 'three'].map((branch) => read(branch))) + await vi.advanceTimersByTimeAsync(25_000) + expect(await responses).toMatchObject([ + { pr: { title: 'delivered' } }, + { pr: { title: 'delivered' } }, + { pr: { title: 'before' }, revalidation: { retryAfterMs: 10_000 } } + ]) + await vi.advanceTimersByTimeAsync(10_000) + expect(await read('three')).toMatchObject({ pr: { title: 'delivered' }, fetchedAt: 88_000 }) + }) + + it('returns a no-data retry after the project account changes', async () => { + mocks.account.mockReturnValue({ ghAccount: { host: 'github.com', user: 'before' } }) + await read('account', 'active') + await vi.advanceTimersByTimeAsync(60_000) + const gate = Promise.withResolvers() + mocks.gate.mockReturnValue(gate.promise) + const waiting = read('account') + await vi.advanceTimersByTimeAsync(0) + mocks.account.mockReturnValue({ ghAccount: { host: 'github.com', user: 'after' } }) + gate.resolve(null) + await vi.advanceTimersByTimeAsync(0) + expect(await waiting).toEqual({ revalidation: { invalidated: true, retryAfterMs: 10_000 } }) + fixture.title = 'new account' + expect(await read('account')).toMatchObject({ pr: { title: 'new account' } }) + }) + + it('cancels one client wait while another receives the shared provider result', async () => { + await read('cancel', 'active') + await vi.advanceTimersByTimeAsync(60_000) + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValue(provider.promise) + const controller = new AbortController() + const cancelled = commands.getRepoPRForBranch( + 'repo', + 'cancel', + null, + null, + false, + 'head-a', + 'swr', + false, + undefined, + controller.signal, + true + ) + const rejected = expect(cancelled).rejects.toThrow('cancelled') + const remaining = read('cancel') + await vi.advanceTimersByTimeAsync(0) + controller.abort() + await rejected + fixture.title = 'shared completion' + provider.resolve(found()) + await vi.advanceTimersByTimeAsync(0) + expect(await remaining).toMatchObject({ pr: { title: 'shared completion' } }) + }) + + it('returns a receipt to excess waiters and keeps the shared result after cancellation', async () => { + await read('many', 'active') + await vi.advanceTimersByTimeAsync(60_000) + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValue(provider.promise) + const controllers = Array.from({ length: 256 }, () => new AbortController()) + const waiting = controllers.map((controller) => + commands.getRepoPRForBranch( + 'repo', + 'many', + null, + null, + false, + 'head-a', + 'swr', + false, + undefined, + controller.signal, + true + ) + ) + const cancellations = Promise.allSettled(waiting) + await vi.advanceTimersByTimeAsync(0) + expect(await read('many')).toMatchObject({ + fetchedAt: 1_000, + revalidation: { retryAfterMs: 10_000 } + }) + for (const controller of controllers) { + controller.abort() + } + await cancellations + fixture.title = 'completed without waiters' + provider.resolve(found()) + await vi.advanceTimersByTimeAsync(0) + expect(await read('many')).toMatchObject({ pr: { title: 'completed without waiters' } }) + }) + + it('lets a rate-limited job expire without starting it after demand stops', async () => { + await read('lease', 'active') + await vi.advanceTimersByTimeAsync(60_000) + mocks.gate.mockResolvedValue(Date.now() + 900_000) + const waiting = read('lease') + await vi.advanceTimersByTimeAsync(25_000) + expect(await waiting).toMatchObject({ + fetchedAt: 1_000, + revalidation: { retryAfterMs: 875_000 } + }) + fixture.title = 'not requested' + await vi.advanceTimersByTimeAsync(900_000) + const { readRecentPhonePROutcome } = + await import('../../../github/client/lookup/pr-for-branch-outcome') + expect( + await readRecentPhonePROutcome('/repo', 'lease', null, null, null, { + currentHeadOid: 'head-a' + }) + ).toBeNull() + mocks.gate.mockResolvedValue(null) + expect(await read('lease', 'manual')).toMatchObject({ pr: { title: 'not requested' } }) + }) + it('retains the exact late error and budget gate after narrowed and other-head writes, then recovers', async () => { + await read('retained', 'active') + await vi.advanceTimersByTimeAsync(60_000) + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(provider.promise) + const waiting = read('retained') + await vi.advanceTimersByTimeAsync(25_000) + expect(await waiting).toMatchObject({ revalidation: { retryAfterMs: 10_000 } }) + provider.resolve({ + kind: 'upstream-error', + errorType: 'network', + message: 'offline', + fetchedAt: Date.now() + }) + await vi.advanceTimersByTimeAsync(0) + const { getPRForBranchOutcome } = + await import('../../../github/client/lookup/pr-for-branch-outcome') + await getPRForBranchOutcome('/repo', 'retained', null, null, null, { + currentHeadOid: 'head-a', + includeDerivedData: false + }) + await read('retained', 'active', 'head-b') + const before = mocks.resolve.mock.calls.length + const error = await read('retained') + expect(error).toMatchObject({ + kind: 'upstream-error', + message: 'offline', + nextAutoRetryAt: expect.any(Number) + }) + expect(mocks.resolve).toHaveBeenCalledTimes(before) + if (!('kind' in error) || error.kind !== 'upstream-error') { + throw new Error('expected the actual provider failure') + } + await vi.advanceTimersByTimeAsync(error.nextAutoRetryAt! - Date.now()) + fixture.title = 'recovered' + const recovered = read('retained') + await vi.advanceTimersByTimeAsync(0) + expect(await recovered).toMatchObject({ kind: 'found', pr: { title: 'recovered' } }) + }) + + it('delivers its exact completion when another full identity advances the phone projection', async () => { + await read('identities', 'active') + await vi.advanceTimersByTimeAsync(60_000) + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(provider.promise) + const waiting = read('identities') + await vi.advanceTimersByTimeAsync(0) + fixture.title = 'other head' + await read('identities', 'active', 'head-b') + fixture.title = 'original exact completion' + provider.resolve(found()) + await vi.advanceTimersByTimeAsync(0) + expect(await waiting).toMatchObject({ + kind: 'found', + pr: { title: 'original exact completion' } + }) + }) + + it('shares overlapping fresh discovery across clients without joining older timed work', async () => { + mocks.resolve.mockImplementation(async () => { + // ast-grep-ignore: test-no-real-time-wait -- Fake-clock provider delay exercises host admission and completion. + await new Promise((resolve) => setTimeout(resolve, 1_000)) + return found() + }) + const discoveries = Promise.all( + Array.from({ length: 2 }, () => + Promise.all(Array.from({ length: 14 }, (_, row) => read(`discovery-${row}`, 'active'))) + ) + ) + await vi.advanceTimersByTimeAsync(1_000) + await discoveries + expect(mocks.resolve).toHaveBeenCalledTimes(14) + await vi.advanceTimersByTimeAsync(60_000) + const slow = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(slow.promise) + const timed = read('discovery-0') + await vi.advanceTimersByTimeAsync(0) + fixture.title = 'fresh action' + const actions = Promise.all([read('discovery-0', 'active'), read('discovery-0', 'active')]) + await vi.advanceTimersByTimeAsync(1_000) + expect(await actions).toMatchObject([ + { pr: { title: 'fresh action' } }, + { pr: { title: 'fresh action' } } + ]) + expect(mocks.resolve).toHaveBeenCalledTimes(16) + fixture.title = 'obsolete timer' + slow.resolve(found()) + await vi.advanceTimersByTimeAsync(0) + expect(await timed).toMatchObject({ pr: { title: 'fresh action' } }) + expect(await read('discovery-0')).toMatchObject({ pr: { title: 'fresh action' } }) + }) + + it.each([true, false])( + 'does not restore full content after a narrowed observation (warm=%s)', + async (warm) => { + if (warm) { + await read('fenced', 'active') + await vi.advanceTimersByTimeAsync(60_000) + } + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(provider.promise) + const timed = read('fenced') + await vi.advanceTimersByTimeAsync(0) + const joined = read('fenced') + await vi.advanceTimersByTimeAsync(0) + const { getPRForBranchOutcome, readRecentPhonePROutcome } = + await import('../../../github/client/lookup/pr-for-branch-outcome') + await getPRForBranchOutcome('/repo', 'fenced', null, null, null, { + currentHeadOid: 'head-a', + includeDerivedData: false + }) + fixture.title = 'obsolete full content' + provider.resolve(found()) + await vi.advanceTimersByTimeAsync(0) + expect(await Promise.all([timed, joined])).toEqual([ + { revalidation: { invalidated: true, retryAfterMs: 10_000 } }, + { revalidation: { invalidated: true, retryAfterMs: 10_000 } } + ]) + expect(mocks.resolve).toHaveBeenCalledTimes(warm ? 3 : 2) + expect( + await readRecentPhonePROutcome('/repo', 'fenced', null, null, null, { + currentHeadOid: 'head-a' + }) + ).toBeNull() + fixture.title = 'new foreground observation' + expect(await read('fenced', 'active')).toMatchObject({ + pr: { title: 'new foreground observation' } + }) + } + ) + + it('delivers a retained cold no-PR completion to both timed consumers', async () => { + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(provider.promise) + const cold = read('cold-absence') + await vi.advanceTimersByTimeAsync(0) + const joined = read('cold-absence') + await vi.advanceTimersByTimeAsync(0) + provider.resolve({ kind: 'no-pr', fetchedAt: 1_000 }) + expect(await Promise.all([cold, joined])).toEqual([ + { kind: 'no-pr', fetchedAt: 1_000 }, + { kind: 'no-pr', fetchedAt: 1_000 } + ]) + }) + + registerPRProducerSettlementTests({ read, mocks, found }) + + it('delivers the current result to cold timers and their pending joiners after a fresh successor', async () => { + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(provider.promise) + const cold = read('cold-successor') + await vi.advanceTimersByTimeAsync(0) + const joined = read('cold-successor') + await vi.advanceTimersByTimeAsync(0) + fixture.title = 'authoritative foreground' + await read('cold-successor', 'active') + fixture.title = 'superseded cold content' + provider.resolve(found()) + await vi.advanceTimersByTimeAsync(0) + expect(await Promise.all([cold, joined])).toMatchObject([ + { kind: 'found', pr: { title: 'authoritative foreground' } }, + { kind: 'found', pr: { title: 'authoritative foreground' } } + ]) + expect(mocks.resolve).toHaveBeenCalledTimes(2) + }) + + it('gives a superseded timer a short retry until another client fresh read completes', async () => { + await read('successor', 'active') + await vi.advanceTimersByTimeAsync(60_000) + const timedProvider = Promise.withResolvers() + const freshProvider = Promise.withResolvers() + mocks.resolve + .mockReturnValueOnce(timedProvider.promise) + .mockReturnValueOnce(freshProvider.promise) + const timed = read('successor') + await vi.advanceTimersByTimeAsync(0) + const fresh = read('successor', 'active') + await vi.advanceTimersByTimeAsync(0) + fixture.title = 'superseded timer' + timedProvider.resolve(found()) + await vi.advanceTimersByTimeAsync(0) + expect(await timed).toMatchObject({ + kind: 'found', + pr: { title: 'before' }, + fetchedAt: 1_000, + revalidation: { retryAfterMs: 10_000 } + }) + fixture.title = 'another client fresh result' + freshProvider.resolve(found()) + await fresh + const starts = mocks.resolve.mock.calls.length + expect(await read('successor')).toMatchObject({ pr: { title: 'another client fresh result' } }) + expect(mocks.resolve).toHaveBeenCalledTimes(starts) + }) + + it('retains an in-flight timer failure when a new branch fills the exact-result cache', async () => { + for (let branch = 0; branch < 256; branch += 1) { + await read(`capacity-${branch}`, 'active') + } + await vi.advanceTimersByTimeAsync(60_000) + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(provider.promise) + const waiting = read('capacity-0') + await vi.advanceTimersByTimeAsync(25_000) + expect(await waiting).toMatchObject({ revalidation: { retryAfterMs: 10_000 } }) + await read('capacity-new', 'active') + provider.resolve({ + kind: 'upstream-error', + errorType: 'network', + message: 'retained late failure', + fetchedAt: Date.now() + }) + await vi.advanceTimersByTimeAsync(0) + const starts = mocks.resolve.mock.calls.length + expect(await read('capacity-0')).toMatchObject({ + kind: 'upstream-error', + message: 'retained late failure', + nextAutoRetryAt: expect.any(Number) + }) + expect(mocks.resolve).toHaveBeenCalledTimes(starts) + const error = await read('capacity-0') + if (!('kind' in error) || error.kind !== 'upstream-error') { + throw new Error('expected the retained capacity failure') + } + await vi.advanceTimersByTimeAsync(error.nextAutoRetryAt! - Date.now()) + const pausedUntil = Date.now() + 60_000 + mocks.gate.mockResolvedValue(pausedUntil) + const gated = read('capacity-0') + await vi.advanceTimersByTimeAsync(25_000) + expect(await gated).toMatchObject({ kind: 'upstream-error', message: 'retained late failure' }) + expect(mocks.resolve).toHaveBeenCalledTimes(starts) + fixture.title = 'capacity recovery' + mocks.gate.mockResolvedValue(null) + await vi.advanceTimersByTimeAsync(35_000) + expect(await read('capacity-0')).toMatchObject({ + kind: 'found', + pr: { title: 'capacity recovery' } + }) + }) + + it('keeps unrelated timer data after many branch watches invalidate their own content', async () => { + const { getPRForBranchOutcome } = + await import('../../../github/client/lookup/pr-for-branch-outcome') + const watch = (branch: string) => + getPRForBranchOutcome('/repo', branch, null, null, null, { + currentHeadOid: 'head-a', + includeDerivedData: false + }) + for (let branch = 0; branch < 256; branch += 1) { + await watch(`watch-${branch}`) + } + await read('unrelated', 'active') + await watch('watch-0') + await watch('watch-256') + fixture.title = 'should wait for its own refresh' + const starts = mocks.resolve.mock.calls.length + expect(await read('unrelated')).toMatchObject({ + kind: 'found', + pr: { title: 'before' }, + fetchedAt: 1_000 + }) + expect(mocks.resolve).toHaveBeenCalledTimes(starts) + }) + + it('refuses extra automatic retention while protected reads fill capacity but leaves foreground usable', async () => { + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValue(provider.promise) + const foreground = Array.from({ length: 256 }, (_, branch) => + read(`protected-${branch}`, 'active') + ) + await vi.advanceTimersByTimeAsync(0) + expect(await read('excess')).toEqual({ + revalidation: { invalidated: true, retryAfterMs: 10_000 } + }) + expect(mocks.resolve).toHaveBeenCalledTimes(256) + mocks.resolve.mockImplementation(async () => found()) + fixture.title = 'foreground still available' + expect(await read('excess', 'active')).toMatchObject({ + pr: { title: 'foreground still available' } + }) + provider.resolve(found()) + await Promise.all(foreground) + expect(await read('excess')).toMatchObject({ kind: 'found' }) + }) + + it.each(['runtime', 'local'] as const)( + 'observes one provider read when %s starts the local/runtime overlap', + async (first) => { + const { addPRRefreshOutcomeObserver, enqueuePRRefresh } = + await import('../../../github/pull-requests/refresh/pr-refresh-coordinator') + const { setPRRefreshRendererPort } = + await import('../../../github/pull-requests/refresh/pr-refresh-renderer-port') + const observe = vi.fn() + const broadcast = vi.fn() + const remove = addPRRefreshOutcomeObserver(observe) + setPRRefreshRendererPort({ isRendererOpen: () => true, sendToTrustedUI: broadcast }) + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValue(provider.promise) + const local = () => + enqueuePRRefresh( + makeCandidate({ + cacheKey: '/repo::observed', + repoId: 'repo', + branch: 'observed', + currentHeadOid: 'head-a' + }), + 'active' + ) + try { + let runtime: ReturnType + if (first === 'runtime') { + runtime = read('observed', 'active') + await vi.advanceTimersByTimeAsync(0) + local() + await vi.advanceTimersByTimeAsync(0) + } else { + local() + await vi.advanceTimersByTimeAsync(0) + runtime = read('observed', 'active') + await vi.advanceTimersByTimeAsync(0) + } + fixture.title = 'shared observable result' + provider.resolve(found()) + expect(await runtime).toMatchObject({ pr: { title: 'shared observable result' } }) + await vi.advanceTimersByTimeAsync(0) + expect(mocks.resolve).toHaveBeenCalledTimes(1) + expect(observe).toHaveBeenCalledTimes(1) + expect(broadcast).toHaveBeenCalledWith( + 'gh:prRefreshEvent', + expect.objectContaining({ + aliases: expect.arrayContaining([ + expect.objectContaining({ cacheKey: '/repo::observed' }) + ]), + outcome: expect.objectContaining({ + kind: 'found', + pr: expect.objectContaining({ title: 'shared observable result' }) + }) + }) + ) + } finally { + remove() + setPRRefreshRendererPort(null) + } + } + ) + + it('returns legacy timed cache immediately and never sends a receipt-only reply', async () => { + await read('legacy', 'active') + await vi.advanceTimersByTimeAsync(60_000) + const provider = Promise.withResolvers() + mocks.resolve.mockReturnValueOnce(provider.promise) + const legacy = await commands.getRepoPRForBranch( + 'repo', + 'legacy', + null, + null, + false, + 'head-a', + 'swr' + ) + expect(legacy).toMatchObject({ kind: 'found', fetchedAt: 1_000 }) + provider.resolve(found()) + await vi.advanceTimersByTimeAsync(0) + }) +}) diff --git a/src/main/runtime/integrations/reviews/runtime-hosted-review-commands.ts b/src/main/runtime/integrations/reviews/runtime-hosted-review-commands.ts index 6a14b007a13..48c026c69f0 100644 --- a/src/main/runtime/integrations/reviews/runtime-hosted-review-commands.ts +++ b/src/main/runtime/integrations/reviews/runtime-hosted-review-commands.ts @@ -1,18 +1,25 @@ import { - getPRForBranchOutcome, + preparePRBranchRead, readRecentPhonePROutcome } from '../../../github/client/lookup/pr-for-branch-outcome' import type { GitHubRepositoryIdentity as GitHubOwnerRepo } from '../../../../shared/github/repositories/repository-identity' import type { GitHubPRRefreshReason, - PRRefreshOutcome + PRSnapshotReply } from '../../../../shared/github/pull-requests/refresh/pull-request-refresh-types' import type { Repo } from '../../../../shared/repo-types' import type { DeviceScope } from '../../../../shared/runtime-session-contracts' import { getRepoSlug, getRepoUpstream } from '../../../github/client/fetch/repo-slug-upstream' -import { admissionTierForRefreshReason } from '../../../github/pull-requests/refresh/pr-refresh-candidate-policy' +import { + admissionTierForRefreshReason, + bypassesFreshnessDelay +} from '../../../github/pull-requests/refresh/pr-refresh-candidate-policy' import type { GitHubPRBranchLookupOptions } from '../../../github/client/lookup/pull-request-lookup-data' -import { runHostedPRRead } from '../../../github/pull-requests/refresh/pr-refresh-coordinator' +import { + runHostedPRRead, + runHostedPRSnapshot +} from '../../../github/pull-requests/refresh/pr-refresh-coordinator' +import { HOSTED_PR_COMPLETION_WAIT_MS } from '../../../github/pull-requests/refresh/hosted-pr-read-jobs' import { type CreateHostedReviewInput, type CreateHostedReviewResult, @@ -78,8 +85,12 @@ export class RuntimeHostedReviewCommands { currentHeadOid?: string | null, reason?: GitHubPRRefreshReason, userRefresh?: boolean, - clientKind?: DeviceScope - ): Promise { + clientKind?: DeviceScope, + signal?: AbortSignal, + acceptRevalidationOnly = false, + selected = false + ): Promise { + const deadline = Date.now() + HOSTED_PR_COMPLETION_WAIT_MS const repo = await this.deps.resolveRepo(repoSelector) const lookupOptions: GitHubPRBranchLookupOptions = { ...this.deps.getExecutionOptions( @@ -90,16 +101,17 @@ export class RuntimeHostedReviewCommands { if (userRefresh === true) { lookupOptions.userRefresh = true } + if (reason && bypassesFreshnessDelay(reason)) { + lookupOptions.resetQuietBackoff = true + } if (acceptMergedFallbackPR === true) { lookupOptions.acceptMergedFallbackPR = true } if (typeof currentHeadOid === 'string' && currentHeadOid.trim().length > 0) { lookupOptions.currentHeadOid = currentHeadOid.trim() } - const lookupOptionArgs: [] | [GitHubPRBranchLookupOptions] = - Object.keys(lookupOptions).length > 0 ? [lookupOptions] : [] const connectionId = hostedReviewSshConnectionId(getStoredRepoExecutionHostId(repo)) - // Desktop callers and reason-carrying refreshes keep their own GitHub read. + // Legacy phone reads retain their narrower thirty-second reuse contract. if (clientKind === 'mobile' && reason === undefined) { const recent = await readRecentPhonePROutcome( repo.path, @@ -113,27 +125,31 @@ export class RuntimeHostedReviewCommands { return recent } } - // Through the coordinator, so a client's read also serves this server's CI-line watches. - return runHostedPRRead( - { - cacheKey: `${repo.path}::${branch}`, - repoKind: repo.kind ?? 'git', - repoId: repo.id, - repoPath: repo.path, - branch, - connectionId, - linkedPRNumber: linkedPRNumber ?? null - }, - () => - getPRForBranchOutcome( - repo.path, - branch, - linkedPRNumber ?? null, - connectionId, - linkedPRNumber == null ? (fallbackPRNumber ?? null) : null, - ...lookupOptionArgs - ) + const candidate = { + cacheKey: `${repo.path}::${branch}`, + repoKind: repo.kind ?? ('git' as const), + repoId: repo.id, + repoPath: repo.path, + branch, + connectionId, + currentHeadOid: lookupOptions.currentHeadOid ?? null, + linkedPRNumber: linkedPRNumber ?? null, + fallbackPRNumber: linkedPRNumber == null ? (fallbackPRNumber ?? null) : null, + localGitOptions: lookupOptions.localGitExecOptions, + isSelected: reason === 'swr' && userRefresh !== true && selected + } + const prepared = await preparePRBranchRead( + repo.path, + branch, + linkedPRNumber ?? null, + connectionId, + linkedPRNumber == null ? (fallbackPRNumber ?? null) : null, + lookupOptions ) + if (reason === 'swr' && userRefresh !== true) { + return runHostedPRSnapshot(candidate, prepared, deadline, signal, acceptRevalidationOnly) + } + return runHostedPRRead(candidate, prepared) } async getHostedReviewForBranch(args: { diff --git a/src/main/runtime/integrations/reviews/runtime-hosted-review-pr-reuse.test.ts b/src/main/runtime/integrations/reviews/runtime-hosted-review-pr-reuse.test.ts index caa4161c0a1..5da571c10ae 100644 --- a/src/main/runtime/integrations/reviews/runtime-hosted-review-pr-reuse.test.ts +++ b/src/main/runtime/integrations/reviews/runtime-hosted-review-pr-reuse.test.ts @@ -1,3 +1,4 @@ +import type { PreparedPRBranchRead } from '../../../github/client/lookup/pr-for-branch-outcome' import type * as GithubRepositoryIdentityModule from '../../../github/repositories/github-repository-identity' import { beforeEach, describe, expect, it, vi } from 'vitest' import type { @@ -31,7 +32,10 @@ vi.mock('../../../github/client/lookup/branch-lookup-resolution', () => ({ resolvePRForBranchOutcome: mocks.resolve })) vi.mock('../../../github/pull-requests/refresh/pr-refresh-coordinator', () => ({ - runHostedPRRead: (_candidate: unknown, read: () => Promise) => read() + runHostedPRRead: ( + _candidate: unknown, + read: (() => Promise) | PreparedPRBranchRead + ) => (typeof read === 'function' ? read() : read.read()) })) import { RuntimeHostedReviewCommands } from './runtime-hosted-review-commands' @@ -153,6 +157,6 @@ describe('phone PR lookups reuse the desktop’s last result', () => { userRefresh, clientKind ) - expect(outcome.kind === 'found' ? outcome.pr.title : null).toBe(expected) + expect('kind' in outcome && outcome.kind === 'found' ? outcome.pr.title : null).toBe(expected) }) }) diff --git a/src/main/runtime/orchestration/db/questions/orchestration-mutation-question-db.test.ts b/src/main/runtime/orchestration/db/questions/orchestration-mutation-question-db.test.ts index 6ec52f4eaa0..e77374310fa 100644 --- a/src/main/runtime/orchestration/db/questions/orchestration-mutation-question-db.test.ts +++ b/src/main/runtime/orchestration/db/questions/orchestration-mutation-question-db.test.ts @@ -70,6 +70,39 @@ describe('OrchestrationDb mutation and question state', () => { ).toThrow(/different answer/) }) + it('marks the question it answers read and leaves other unread mail alone', () => { + const d = createDb() + const run = d.createRun({ + objective: 'Read on answer', + coordinatorHandle: 'term_coord', + coordinatorPaneKey: 'tab_coord:11111111-1111-4111-8111-111111111111' + }) + const task = d.createTask({ spec: 'ask', runId: run.id }) + const dispatch = createRootDispatch(d, task.id, 'term_worker') + const created = d.createQuestion({ + runId: run.id, + dispatchId: dispatch.id, + askerHandle: 'term_worker', + question: 'Which format?' + }) + const status = d.insertMessage({ + runId: run.id, + from: `dispatch:${dispatch.id}`, + to: `run:${run.id}`, + subject: 'Progress' + }) + + d.answerQuestion({ + messageId: created.message.id, + runId: run.id, + consumerGeneration: run.consumer_generation, + body: 'old' + }) + + expect(d.getMessageById(created.message.id)?.read).toBe(1) + expect(d.getMessageById(status.id)?.read).toBe(0) + }) + it('closes pending questions with their Dispatch', () => { const d = createDb() const run = d.createRun({ diff --git a/src/main/runtime/orchestration/db/questions/question-threads.ts b/src/main/runtime/orchestration/db/questions/question-threads.ts index 5741dc5750a..ee0a450ed12 100644 --- a/src/main/runtime/orchestration/db/questions/question-threads.ts +++ b/src/main/runtime/orchestration/db/questions/question-threads.ts @@ -123,6 +123,7 @@ export function answerQuestion( `Question ${params.messageId} is closed because its Dispatch is inactive.` ) } + this.markAsRead([question.message_id]) if (question.status === 'answered') { if (question.answer_body !== params.body || !question.answer_message_id) { throw new OrchestrationError( diff --git a/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-worker-cleared-session.test.ts b/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-worker-cleared-session.test.ts index 26278fc62d4..13f816fb917 100644 --- a/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-worker-cleared-session.test.ts +++ b/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-worker-cleared-session.test.ts @@ -1,9 +1,5 @@ -import { closeTestJournalHostDatabases } from '../../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import { agentSessionConversationView } from '../../../../../shared/agent-session/lifecycle/agent-session-conversation-view' /** Worker-level reads/operations follow clear successors and the full lineage; unresolved successors remain unverifiable. */ -import { mkdtemp, rm } from 'node:fs/promises' -import { tmpdir } from 'node:os' -import { join } from 'node:path' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import type { AgentJournalRenderItem } from '../../../../../shared/agent-session/journal/agent-session-journal-types' import type { AgentSessionRecord } from '../../../../../shared/agent-session/lifecycle/agent-session-record' @@ -22,7 +18,6 @@ vi.mock( const { readStructuredWorkerTerminal } = await import('./structured-worker-terminal-read') const { - observeStructuredSession, observeStructuredWorker, resolveStructuredWorkerAuthority, resolveStructuredWorkerIdentityForSession @@ -44,10 +39,6 @@ const { readStructuredLineageJournalPage, STRUCTURED_JOURNAL_PAGE_LIMIT } = await import('../../../agent-session/environment/structured-session-child-env.test-support') const { foundAgentSessionRecord } = await import('../../../agent-session/records/agent-session-record-founding') -const { applyAgentSessionRestartAdjudication } = - await import('../../../agent-session/ownership/agent-session-restart-lease-transitions') -const { openTestAgentSessionRecordStore } = - await import('../../../agent-session/records/agent-session-record-store-test-harness') const { OrcaRuntimeService } = await import('../../../orca-runtime') const { OrchestrationDb } = await import('../../db/orchestration-db') const { rpcRuntime } = await import('./structured-worker-rpc.test-support') @@ -361,15 +352,6 @@ describe.each(['live', 'at-rest'] as const)( (successor) => { beforeEach(() => installClearedWorker(successor)) - it("serves terminal read from the whole conversation, the successor's last", async () => { - const identity = registerWorker() - const read = await readStructuredWorkerTerminal({ handle: identity.handle, db: null }) - expect(historyAsked).toEqual([SUCCESSOR, MINTED]) - const lines = JSON.stringify(read) - expect(lines.indexOf('PRE-CLEAR')).toBeGreaterThan(-1) - expect(lines.indexOf('POST-CLEAR')).toBeGreaterThan(lines.indexOf('PRE-CLEAR')) - }) - it('reads the whole conversation from its chat once released, keeping no copy', async () => { const dispatchId = startWorkerDispatch(db, registerWorker()) await expect(releaseSucceeded(dispatchId)).resolves.toMatchObject({ state: 'released' }) @@ -386,21 +368,6 @@ describe.each(['live', 'at-rest'] as const)( ).rejects.toMatchObject({ code: 'cursor_dispatch_mismatch' }) }) - it('stops the worker by closing the successor, and can then release it', async () => { - const identity = registerWorker() - const stop = await stopStructuredWorker(identity) - expect(closed).toEqual([SUCCESSOR]) - expect(stop.stopped).toBe(true) - // Never-started successors have no death evidence; retiring their tab must still let release prove exit. - structuredWorkerIdentities.clear() - await expect( - new OrcaRuntimeService().inspectTerminalProcessIncarnationLiveness( - identity.processIncarnation, - JSON.stringify(LOCAL_SCOPE) - ) - ).resolves.toBe('exited') - }) - it('finds the worker from its durable row after a restart', () => { const identity = registerWorker() startWorkerDispatch(db, identity) @@ -419,7 +386,7 @@ describe.each(['live', 'at-rest'] as const)( describe('a successor no agent ever ran', () => { beforeEach(() => installClearedWorker('at-rest')) - it.each([false, true])( + it.each([true])( 'lets a stopped worker be released (output read in between, reopening the chat: %s)', async (readBetween) => { const identity = registerWorker() @@ -440,71 +407,6 @@ describe('a successor no agent ever ran', () => { ) }) -describe('a released session whose start was attempted stays unverifiable without proof', () => { - let directory: string - - beforeEach(async () => { - directory = await mkdtemp(join(tmpdir(), 'orca-cleared-successor-')) - installClearedWorker('at-rest') - visibleTabs = [] - }) - - afterEach(async () => { - await closeTestJournalHostDatabases() - await rm(directory, { recursive: true, force: true }) - }) - - async function reserveSuccessor(operationId: string) { - const store = await openTestAgentSessionRecordStore(directory) - const reserved = await store.reserveOwner({ - sessionId: SUCCESSOR, - location: liveRecord(SUCCESSOR).location, - provider: 'claude', - accountHome: { variable: 'CLAUDE_CONFIG_DIR', path: '/home/dev/.claude' }, - expectedFence: null, - spawnToken: 'spawn-a', - claimKeyId: 'key-1', - handoffOperationId: operationId, - probe: { outcome: 'indeterminate', reason: 'no answer' }, - operation: { callerKey: 'client-1', operationId, fingerprint: 'fp-1' }, - now: NOW - }) - return { store, reserved: reserved.record } - } - - const NOW = 1_800_000_000_000 - const OPERATION_ID = `${NOW}-${'1'.padStart(32, '0')}` - - it('after a restart released a reservation whose child was never ruled out', async () => { - const { reserved } = await reserveSuccessor(OPERATION_ID) - const recovered = applyAgentSessionRestartAdjudication({ - record: reserved, - probe: { outcome: 'indeterminate', reason: 'no answer' }, - now: NOW + 1 - }) - expect(recovered.lease).toMatchObject({ claimStatus: 'released', deathEvidence: null }) - records.set(SUCCESSOR, recovered) - expect(observeStructuredSession(SUCCESSOR).status).toBe('unverifiable') - }) - - it('after a failed first start whose exit was not proven', async () => { - const { store, reserved } = await reserveSuccessor(OPERATION_ID) - const settled = await store.settleFailedAcquisition({ - sessionId: SUCCESSOR, - fence: reserved.lease.runtimeFence, - spawnToken: 'spawn-a', - callerKey: 'client-1', - operationId: OPERATION_ID, - outcome: { status: 'failed', code: 'agent_session_operation_invalid', message: 'failed' }, - exitProof: 'unproven', - now: NOW + 1 - }) - expect(settled.lease).toMatchObject({ claimStatus: 'released', deathEvidence: null }) - records.set(SUCCESSOR, settled) - expect(observeStructuredSession(SUCCESSOR).status).toBe('unverifiable') - }) -}) - describe('stopping a worker while a /clear commits', () => { beforeEach(() => { installUnclearedWorker() @@ -555,7 +457,7 @@ describe('worker-stop on a structured worker that reads exited without any /clea }) }) -describe.each([{ clears: 1 }, { clears: 2 }])( +describe.each([{ clears: 2 }])( 'a retired lineage of $clears clear(s) whose later sessions cannot be read', (lineage) => { beforeEach(() => { @@ -607,13 +509,6 @@ describe('the lineage journal page', () => { expect(page?.sessionIds.filter((id) => id === MINTED)).toHaveLength(50) expect(page?.hasOlder).toBe(true) }) - - it('ends at an earlier session it cannot read, and says older history was left out', async () => { - unreadable.add(MINTED) - const read = await readJournal(registerWorker()) - expect(texts(read.transcript?.messages)).toContain('POST-CLEAR') - expect(read.warnings).toContain('Older journal items were omitted from this page.') - }) }) describe('a worker cleared twice', () => { diff --git a/src/main/runtime/rpc/callers/active-call.ts b/src/main/runtime/rpc/callers/active-call.ts index 5f2f49adf70..ac9958555d3 100644 --- a/src/main/runtime/rpc/callers/active-call.ts +++ b/src/main/runtime/rpc/callers/active-call.ts @@ -1,6 +1,6 @@ import { AsyncLocalStorage } from 'node:async_hooks' import { isOrcaSessionId } from '../../../../shared/orca-session-address' -import { isAgentRpcCaller, type RpcCallerIdentity } from './rpc-caller-identity' +import type { RpcCallerIdentity } from './rpc-caller-identity' type ActiveRpcCall = { caller: RpcCallerIdentity | undefined @@ -17,7 +17,7 @@ export function runWithActiveRpcCaller( ): T { // Only this host's CLI can name a chat here; as in the handoff card, other callers act for a person. const agentSessionId = - isAgentRpcCaller(caller) && claimedAgentSessionId && isOrcaSessionId(claimedAgentSessionId) + caller?.kind === 'local-cli' && claimedAgentSessionId && isOrcaSessionId(claimedAgentSessionId) ? claimedAgentSessionId : undefined return activeCall.run({ caller, agentSessionId }, run) diff --git a/src/main/runtime/rpc/callers/rpc-caller-identity.ts b/src/main/runtime/rpc/callers/rpc-caller-identity.ts index 0f726a4238e..4f138dc164b 100644 --- a/src/main/runtime/rpc/callers/rpc-caller-identity.ts +++ b/src/main/runtime/rpc/callers/rpc-caller-identity.ts @@ -1,29 +1,34 @@ /** Transport-proven identity, never request params, controls durable ledger namespaces and whose view a launch moves. */ import type { RpcDispatchStreamingOptions } from '../dispatch/dispatcher-stream-options' +import { CLI_AGENT_CALLER_CLIENT_CAPABILITY } from '../../../../shared/remote-runtime/cli-agent-caller-capability' export type RpcCallerIdentity = /** The `orca` CLI over the runtime socket, and the in-process bridges that relay it. */ | { kind: 'local-cli' } /** All local IPC windows share identity across reload and app restart. */ | { kind: 'desktop' } - /** A paired phone or remote desktop, by its device subject, which survives credential rotation. */ - | { kind: 'paired-device'; deviceId: string } + /** A paired phone or remote desktop, by its device subject, which survives credential rotation. + * `agent`: the orca CLI calling with that device's pairing (`--environment`), not the person using it. */ + | { kind: 'paired-device'; deviceId: string; agent?: true } export const DESKTOP_RPC_CALLER: RpcCallerIdentity = { kind: 'desktop' } /** Stamped by the owner-token runtime socket, which only the local `orca` CLI holds. */ export const LOCAL_CLI_RPC_CALLER: RpcCallerIdentity = { kind: 'local-cli' } -/** The orca CLI agents run, locally or forwarded from a remote host; relayed devices are people. */ +/** The orca CLI agents run, on this host or over a device's pairing; people's windows and phones are not. */ export function isAgentRpcCaller(caller: RpcCallerIdentity | undefined): boolean { - return caller?.kind === 'local-cli' + return caller?.kind === 'local-cli' || (caller?.kind === 'paired-device' && caller.agent === true) } /** Trust explicit transport identity; unnamed declared clients have none. Temporarily, no declared client means local CLI. * The runtime socket stamps local-cli explicitly; plugin approval never trusts identity alone (it needs the approve fingerprint). */ export function resolveRpcCallerIdentity( transport: - | Pick + | Pick< + RpcDispatchStreamingOptions, + 'caller' | 'clientKind' | 'pairedDeviceId' | 'clientCapabilities' + > | undefined ): RpcCallerIdentity | undefined { if (transport?.caller) { @@ -31,7 +36,10 @@ export function resolveRpcCallerIdentity( } const deviceId = transport?.pairedDeviceId?.trim() if (deviceId) { - return { kind: 'paired-device', deviceId } + // A false claim only gives up the person's view-following, never any authority. + return transport?.clientCapabilities?.includes(CLI_AGENT_CALLER_CLIENT_CAPABILITY) + ? { kind: 'paired-device', deviceId, agent: true } + : { kind: 'paired-device', deviceId } } return transport?.clientKind === undefined ? { kind: 'local-cli' } : undefined } diff --git a/src/main/runtime/rpc/methods/agent-launch/agent-launch-handoff-card.ts b/src/main/runtime/rpc/methods/agent-launch/agent-launch-handoff-card.ts index 87056f65c61..cd4d35a2ce8 100644 --- a/src/main/runtime/rpc/methods/agent-launch/agent-launch-handoff-card.ts +++ b/src/main/runtime/rpc/methods/agent-launch/agent-launch-handoff-card.ts @@ -8,7 +8,6 @@ import { startedAgentSender } from '../../../orchestration/workers/launch/worker-start-card' import type { RpcContext } from '../../core' -import { isAgentRpcCaller } from '../../callers/rpc-caller-identity' /** An agent that hands work to another from its chat leaves a card there linking to it. * Display bookkeeping: nothing here refuses, holds or fails the launch, whose result passes through. */ @@ -27,10 +26,10 @@ function prepareHandoffRecord( params: AgentLaunchParams & { operationId: string }, context: RpcContext ): (result: AgentLaunchResult) => void { - // Only the orca CLI on this host names its chat; windows, phones and remote CLIs launch for a person. + // Only the orca CLI on this host names a chat here; windows and phones launch for a person, and a remote CLI's chat is on its own host. const callerSessionId = context.orchestrationCompatibilityEvidence?.agentSessionId if ( - !isAgentRpcCaller(context.caller) || + context.caller?.kind !== 'local-cli' || callerSessionId === undefined || !isOrcaSessionId(callerSessionId) ) { diff --git a/src/main/runtime/rpc/methods/computer/computer.ts b/src/main/runtime/rpc/methods/computer/computer.ts index 0279258b115..182e27f5e6a 100644 --- a/src/main/runtime/rpc/methods/computer/computer.ts +++ b/src/main/runtime/rpc/methods/computer/computer.ts @@ -1,6 +1,7 @@ import { callComputerSidecarAction, callComputerSidecarCapabilities, + callComputerSidecarLaunchApp, callComputerSidecarListApps, callComputerSidecarListWindows, callComputerSidecarSnapshot, @@ -13,6 +14,7 @@ import { ComputerPermissions, Drag, Hotkey, + LaunchApp, ListApps, ListWindows, PasteText, @@ -55,7 +57,7 @@ export const COMPUTER_METHODS = [ handler: async (params) => { const { openComputerUsePermissions } = await import('../../../../computer/macos-permissions/macos-computer-use-permissions') - return openComputerUsePermissions(params.id) + return openComputerUsePermissions(params.id, { skipGranted: params.skipGranted === true }) } }), defineMethod({ @@ -68,6 +70,14 @@ export const COMPUTER_METHODS = [ return computerUsePermissionStatusGate.read() } }), + defineMethod({ + name: 'computer.launchApp', + permission: 'desktop-control', + params: LaunchApp, + handler: async (params) => { + return await callComputerSidecarLaunchApp(params) + } + }), defineMethod({ name: 'computer.listWindows', permission: 'desktop-control', diff --git a/src/main/runtime/rpc/methods/files/files.test.ts b/src/main/runtime/rpc/methods/files/files.test.ts index fda339aff54..9588b750d22 100644 --- a/src/main/runtime/rpc/methods/files/files.test.ts +++ b/src/main/runtime/rpc/methods/files/files.test.ts @@ -137,4 +137,27 @@ describe('file RPC methods', () => { expect(unwatch).toHaveBeenCalledTimes(1) expect(cleanups.size).toBe(0) }) + + it('opens phone and older-CLI files without moving the desktop unless a target asks to', async () => { + const openMobileFile = vi.fn(async () => ({})) + const openMobileDiff = vi.fn(async () => ({})) + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: these handlers call only the two open methods and getRuntimeId. + const runtime = { + getRuntimeId: () => 'test-runtime', + openMobileFile, + openMobileDiff + } as unknown as OrcaRuntimeService + const dispatcher = new RpcDispatcher({ runtime, methods: FILE_METHODS }) + const target = { worktree: 'id:wt-1', relativePath: 'README.md' } + + await dispatcher.dispatch(makeRequest('files.open', target)) + await dispatcher.dispatch(makeRequest('files.openDiff', target)) + await dispatcher.dispatch(makeRequest('files.open', { ...target, navigation: 'all' })) + + expect(openMobileFile.mock.calls).toEqual([ + ['id:wt-1', 'README.md', 'caller'], + ['id:wt-1', 'README.md', 'all'] + ]) + expect(openMobileDiff.mock.calls).toEqual([['id:wt-1', 'README.md', false, 'caller']]) + }) }) diff --git a/src/main/runtime/rpc/methods/files/files.ts b/src/main/runtime/rpc/methods/files/files.ts index c5992a77d08..d334ffeb826 100644 --- a/src/main/runtime/rpc/methods/files/files.ts +++ b/src/main/runtime/rpc/methods/files/files.ts @@ -79,7 +79,7 @@ export const FILE_METHODS = [ runtime.openMobileFile( params.worktree, params.relativePath, - params.navigation, + params.navigation ?? 'caller', ...(params.line === undefined ? [] : [{ line: params.line, column: params.column }]) ) }), @@ -92,7 +92,7 @@ export const FILE_METHODS = [ params.worktree, params.relativePath, params.staged === true, - params.navigation, + params.navigation ?? 'caller', ...(params.oldPath ? ([params.oldPath] as const) : []) ) }), diff --git a/src/main/runtime/rpc/methods/host-management/desktop-window.ts b/src/main/runtime/rpc/methods/host-management/desktop-window.ts new file mode 100644 index 00000000000..f1e7a27af5c --- /dev/null +++ b/src/main/runtime/rpc/methods/host-management/desktop-window.ts @@ -0,0 +1,12 @@ +import { defineMethod } from '../../core' +import { requestBackgroundWindowOpen } from '../../../../window/activation/background-window-open' + +export const DESKTOP_WINDOW_METHODS = [ + // `orca open` asks the running app instead of relaunching it, which would hand it the foreground. + defineMethod({ + name: 'desktop.openWindowInBackground', + permission: 'desktop-control', + params: null, + handler: async () => ({ requested: requestBackgroundWindowOpen() }) + }) +] diff --git a/src/main/runtime/rpc/methods/preflight/preflight.ts b/src/main/runtime/rpc/methods/preflight/preflight.ts index e8ea0998ae0..d912e6fc88f 100644 --- a/src/main/runtime/rpc/methods/preflight/preflight.ts +++ b/src/main/runtime/rpc/methods/preflight/preflight.ts @@ -14,7 +14,7 @@ import { refreshAgentsOnHost } from '../../../../preflight/workspace-agent-detection' import { - PreflightAgentDetection, + PreflightAgentRefresh, PreflightProjectAgentDetection, PreflightCheck, PreflightDetectRemoteAgents, @@ -74,11 +74,11 @@ export const PREFLIGHT_METHODS = [ defineMethod({ name: 'preflight.refreshAgents', permission: 'workspace', - params: PreflightAgentDetection, + params: PreflightAgentRefresh, handler: async (params, { runtime }) => { const host = await resolveQualifiedAgentDetectionHost(runtime, params) await assertAgentDetectionHostRunning(host) - return refreshAgentsOnHost(host, hostDetectionOptions(runtime)) + return refreshAgentsOnHost(host, hostDetectionOptions(runtime), params.userInitiated) } }) ] diff --git a/src/main/runtime/rpc/methods/rpc-method-registry.ts b/src/main/runtime/rpc/methods/rpc-method-registry.ts index c0168ed85ee..3d603d1141f 100644 --- a/src/main/runtime/rpc/methods/rpc-method-registry.ts +++ b/src/main/runtime/rpc/methods/rpc-method-registry.ts @@ -1,4 +1,5 @@ import { STATUS_METHODS } from './host-management/status' +import { DESKTOP_WINDOW_METHODS } from './host-management/desktop-window' import { SIDEBAR_INPUT_METHODS } from './sidebar-inputs/sidebar-inputs' import { ORCAD_TERMINAL_CENSUS_METHODS } from './orcad/orcad-terminal-census' import { ORCAD_MIGRATION_METHODS } from './orcad/orcad-migration' @@ -87,6 +88,7 @@ import { ORCA_SIDEBAR_INPUTS_IS_MAIN } from '../../../../shared/sidebar-inputs-s // Keep registration order and the full RPC/security surface visible in one manifest. export const ALL_RPC_METHODS = [ ...STATUS_METHODS, + ...DESKTOP_WINDOW_METHODS, ...ORCAD_TERMINAL_CENSUS_METHODS, ...ORCAD_MIGRATION_METHODS, ...SESSION_TERMINAL_SCROLLBACK_METHODS, diff --git a/src/main/runtime/rpc/methods/source-control/github/github-pull-request-methods.ts b/src/main/runtime/rpc/methods/source-control/github/github-pull-request-methods.ts index 0c2c03dbfa0..924b2cf2bbe 100644 --- a/src/main/runtime/rpc/methods/source-control/github/github-pull-request-methods.ts +++ b/src/main/runtime/rpc/methods/source-control/github/github-pull-request-methods.ts @@ -16,7 +16,7 @@ export const GITHUB_PULL_REQUEST_METHODS = [ name: 'github.prForBranch', permission: 'workspace', params: PrForBranch, - handler: async (params, { runtime, clientKind }) => + handler: async (params, { runtime, clientKind, signal }) => runtime.getRepoPRForBranch( params.repo, params.branch, @@ -26,7 +26,10 @@ export const GITHUB_PULL_REQUEST_METHODS = [ params.currentHeadOid, params.reason, params.userRefresh, - clientKind + clientKind, + signal, + params.acceptRevalidationOnly, + params.selected ) }), defineMethod({ diff --git a/src/main/runtime/rpc/methods/structured-sessions/catalog/structured-agent-session-options-read.test.ts b/src/main/runtime/rpc/methods/structured-sessions/catalog/structured-agent-session-options-read.test.ts index d92cf102a78..33512588f64 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/catalog/structured-agent-session-options-read.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/catalog/structured-agent-session-options-read.test.ts @@ -78,6 +78,25 @@ describe('agentSession.modelCatalog', () => { ) }) + it.each([false, true])( + 'negotiates early progress through the existing client capability (enabled=%s)', + async (enabled) => { + record = { ...agentSessionRecordFixture(), provider: 'codex' } + const result = await call( + 'agentSession.modelCatalog', + { agent: 'codex', sessionId: record.sessionId, waitForListing: true }, + { + ...STRUCTURED_CLIENT, + clientCapabilities: DESKTOP_RENDERER_RUNTIME_CLIENT_CAPABILITIES.filter( + (capability) => enabled || capability !== 'agent-session.model-catalog.progress.v1' + ) + } + ) + expect(result.ok, JSON.stringify(result)).toBe(true) + expect(read).toHaveBeenLastCalledWith(expect.objectContaining({ progress: enabled })) + } + ) + it("keeps a moved floating workspace's list apart from the old folder's late answer", async () => { const late = Promise.withResolvers() const listed = (id: string): AgentModelCatalogSuccess => ({ diff --git a/src/main/runtime/rpc/methods/structured-sessions/catalog/structured-agent-session-options-read.ts b/src/main/runtime/rpc/methods/structured-sessions/catalog/structured-agent-session-options-read.ts index df3412bce8d..64de6072066 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/catalog/structured-agent-session-options-read.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/catalog/structured-agent-session-options-read.ts @@ -1,4 +1,7 @@ -import { AGENT_SESSION_COHERENT_MODEL_CATALOG_CAPABILITY } from '../../../../../../shared/agent-session/structured-agent-session-surface-capabilities' +import { + AGENT_SESSION_COHERENT_MODEL_CATALOG_CAPABILITY, + AGENT_SESSION_MODEL_CATALOG_PROGRESS_CAPABILITY +} from '../../../../../../shared/agent-session/structured-agent-session-surface-capabilities' // options reads at-rest records/catalogs or the live provider; modelCatalog reads stored/start-published facts without waiting on attach. // Older hosts return method_not_found (mobile may see forbidden); clients retain static seeds. @@ -113,6 +116,10 @@ export const STRUCTURED_AGENT_SESSION_OPTIONS_READ_METHODS = [ const request = { ...params, ...scope, + progress: + ctx.clientKind === undefined || + ctx.clientCapabilities?.includes(AGENT_SESSION_MODEL_CATALOG_PROGRESS_CAPABILITY) === + true, ...(waits && resolving ? { deadline } : {}), ...(launchDirectory ? { workspacePath: launchDirectory } @@ -140,7 +147,7 @@ export const STRUCTURED_AGENT_SESSION_OPTIONS_READ_METHODS = [ ) } // A floating read lists the folder it resolves to (above), never the unresolved one. - if (!host && !params.savedOnly && !follows) { + if (!waits && !host && !params.savedOnly && !follows) { void catalog.demand(request) } const result = host ? await host.readModelCatalog(request) : await catalog.read(request) diff --git a/src/main/runtime/terminal/launch/terminal-split.ts b/src/main/runtime/terminal/launch/terminal-split.ts index 328ddfe7b8e..1a82cd4ae3e 100644 --- a/src/main/runtime/terminal/launch/terminal-split.ts +++ b/src/main/runtime/terminal/launch/terminal-split.ts @@ -101,7 +101,8 @@ export abstract class OrcaRuntimeWithSplitTerminal extends OrcaRuntimeWithStopEx worktreeId: leaf.worktreeId, sourceLeafId: leaf.leafId, telemetrySource: opts.telemetrySource, - newLeafId + newLeafId, + ...(opts.activate === false ? { activate: false } : {}) }) return await this.awaitRendererSplit(split, newLeafId) } diff --git a/src/main/ssh/remote-host/node-toolchain/ssh-remote-node-resolution.test.ts b/src/main/ssh/remote-host/node-toolchain/ssh-remote-node-resolution.test.ts index 37ccfbeb69c..17573f334f9 100644 --- a/src/main/ssh/remote-host/node-toolchain/ssh-remote-node-resolution.test.ts +++ b/src/main/ssh/remote-host/node-toolchain/ssh-remote-node-resolution.test.ts @@ -16,8 +16,7 @@ vi.mock('../../remote-exec/remote-exec-command', async (importOriginal) => ({ // Why: await import() is required so vi.mock() above registers before the // module under test is evaluated. Static import would bypass the mock. -const { RemoteNodeNotFoundError, resolveRemoteNodePath } = - await import('./ssh-remote-node-resolution') +const { resolveRemoteNodePath } = await import('./ssh-remote-node-resolution') const conn = {} as SshConnection @@ -32,12 +31,6 @@ function loginShellAnswer(output: string) { } } -function loginShellProbe(shell: string, mode: string) { - return expect.stringMatching( - new RegExp(`^'${shell}' ${mode} 'printf %s \\w+ \\w+; command -v node; printf %s \\w+ \\w+'$`) - ) -} - describe('resolveRemoteNodePath', () => { beforeEach(() => { execCommandMock.mockReset() @@ -115,87 +108,6 @@ describe('resolveRemoteNodePath', () => { } }) - it('finds node under a MISE_DATA_DIR present only in the probe environment', async () => { - execCommandMock - .mockResolvedValueOnce('/home/u/.local/share/mise/shims/node\n') - .mockResolvedValueOnce('v20.11.0\n') - - await resolveRemoteNodePath(conn) - - const callScript = execCommandMock.mock.calls[0]![1] as string - const home = mkdtempSync(path.join(os.tmpdir(), 'orca-mise-env-probe-')) - try { - const miseDataDir = path.join(home, 'env-mise') - const installPath = path.join(miseDataDir, 'installs/node/v20.11.0/bin/node') - mkdirSync(path.dirname(installPath), { recursive: true }) - writeFileSync(installPath, '#!/bin/sh\nprintf "v20.11.0\\n"\n') - chmodSync(installPath, 0o755) - - const output = execFileSync('/bin/sh', ['-c', callScript], { - encoding: 'utf8', - env: { HOME: home, MISE_DATA_DIR: miseDataDir, PATH: '/usr/bin:/bin' } - }) - - expect(output.split('\n')).toContain(installPath) - } finally { - rmSync(home, { recursive: true, force: true }) - } - }) - - it('falls back to XDG_DATA_HOME for mise installs when MISE_DATA_DIR is unset', async () => { - execCommandMock - .mockResolvedValueOnce('/home/u/.local/share/mise/shims/node\n') - .mockResolvedValueOnce('v20.11.0\n') - - await resolveRemoteNodePath(conn) - - const callScript = execCommandMock.mock.calls[0]![1] as string - const home = mkdtempSync(path.join(os.tmpdir(), 'orca-mise-xdg-probe-')) - try { - const xdgDataHome = path.join(home, 'xdg') - const installPath = path.join(xdgDataHome, 'mise/installs/node/v20.11.0/bin/node') - mkdirSync(path.dirname(installPath), { recursive: true }) - writeFileSync(installPath, '#!/bin/sh\nprintf "v20.11.0\\n"\n') - chmodSync(installPath, 0o755) - - const output = execFileSync('/bin/sh', ['-c', callScript], { - encoding: 'utf8', - env: { HOME: home, XDG_DATA_HOME: xdgDataHome, PATH: '/usr/bin:/bin' } - }) - - expect(output.split('\n')).toContain(installPath) - } finally { - rmSync(home, { recursive: true, force: true }) - } - }) - - it('expands tilde NVM_DIR assignments from shell dotfiles', async () => { - execCommandMock - .mockResolvedValueOnce('/home/u/.nvm/versions/node/v20.11.0/bin/node\n') - .mockResolvedValueOnce('v20.11.0\n') - - await resolveRemoteNodePath(conn) - - const callScript = execCommandMock.mock.calls[0]![1] as string - const home = mkdtempSync(path.join(os.tmpdir(), 'orca-nvm-probe-')) - try { - const nodePath = path.join(home, 'tilde-nvm/versions/node/v20.11.0/bin/node') - mkdirSync(path.dirname(nodePath), { recursive: true }) - writeFileSync(nodePath, '#!/bin/sh\nprintf "v20.11.0\\n"\n') - chmodSync(nodePath, 0o755) - writeFileSync(path.join(home, '.zshrc'), 'export NVM_DIR=~/tilde-nvm\n') - - const output = execFileSync('/bin/sh', ['-c', callScript], { - encoding: 'utf8', - env: { HOME: home, PATH: '/usr/bin:/bin' } - }) - - expect(output.split('\n')).toContain(nodePath) - } finally { - rmSync(home, { recursive: true, force: true }) - } - }) - it('expands an $XDG_DATA_HOME-relative MISE_DATA_DIR assignment from shell dotfiles', async () => { execCommandMock .mockResolvedValueOnce('/home/u/.local/share/mise/shims/node\n') @@ -257,38 +169,6 @@ describe('resolveRemoteNodePath', () => { } }) - it('rejects a path-probe candidate whose version is below the minimum', async () => { - // Probe returns two candidates; the first (v10) is too old, the second - // (v20) must be selected instead. - execCommandMock - .mockResolvedValueOnce( - '/home/u/.nvm/versions/node/v10.24.1/bin/node\n/home/u/.nvm/versions/node/v20.11.0/bin/node\n' - ) - .mockResolvedValueOnce('v10.24.1\n') - .mockResolvedValueOnce('v20.11.0\n') - - await expect(resolveRemoteNodePath(conn)).resolves.toBe( - '/home/u/.nvm/versions/node/v20.11.0/bin/node' - ) - }) - - it('falls back to the login shell when path probes find nothing', async () => { - execCommandMock - .mockResolvedValueOnce('\n') - .mockResolvedValueOnce('/bin/zsh') - .mockImplementationOnce(loginShellAnswer('/home/u/.nvm/versions/node/v20.11.0/bin/node\n')) - .mockResolvedValueOnce('v20.11.0\n') - - await expect(resolveRemoteNodePath(conn)).resolves.toBe( - '/home/u/.nvm/versions/node/v20.11.0/bin/node' - ) - - expect(execCommandMock).toHaveBeenNthCalledWith(3, conn, loginShellProbe('/bin/zsh', '-lc'), { - wrapCommand: false, - timeoutMs: 8_000 - }) - }) - it('falls back to the login shell when every path-probe candidate is too old', async () => { execCommandMock .mockResolvedValueOnce('/old/node\n') @@ -302,44 +182,6 @@ describe('resolveRemoteNodePath', () => { ) }) - it('uses -c (not -lc) for a csh login shell, which rejects combined -lc', async () => { - // csh/tcsh require plain -c; -lc/-l can fail with Bad modifier (#8701). - execCommandMock - .mockResolvedValueOnce('\n') - .mockResolvedValueOnce('/bin/csh\n') - .mockImplementationOnce(loginShellAnswer('/usr/local/bin/node\n')) - .mockResolvedValueOnce('v20.0.0\n') - - await expect(resolveRemoteNodePath(conn)).resolves.toBe('/usr/local/bin/node') - expect(execCommandMock).toHaveBeenNthCalledWith(3, conn, loginShellProbe('/bin/csh', '-c'), { - wrapCommand: false, - timeoutMs: 8_000 - }) - }) - - it('keeps the default path-probe fallback for SSH session-limit-shaped errors', async () => { - const sessionLimitError = Object.assign(new Error('(SSH) Channel open failure: open failed'), { - reason: 4 - }) - execCommandMock - .mockRejectedValueOnce(sessionLimitError) - .mockResolvedValueOnce('/bin/bash') - .mockResolvedValueOnce('\n') - - await expect(resolveRemoteNodePath(conn)).rejects.toThrow(/Node\.js not found/) - }) - - it('rethrows SSH session-limit errors from the path probe in strict mode', async () => { - const sessionLimitError = Object.assign(new Error('(SSH) Channel open failure: open failed'), { - reason: 4 - }) - execCommandMock.mockRejectedValueOnce(sessionLimitError) - - await expect( - resolveRemoteNodePath(conn, undefined, { rethrowSessionLimitErrors: true }) - ).rejects.toBe(sessionLimitError) - }) - it('surfaces an AbortError instead of not-found when the shared signal is aborted', async () => { // Shared bootstrap abort must remain cancellation; swallowing it as Node-not-found defeats sequential fallback. const controller = new AbortController() @@ -366,58 +208,4 @@ describe('resolveRemoteNodePath', () => { 'C:/Program Files/nodejs/node.exe' ) }) - - describe('strict resolution, which settles rung D only on an answered "no Node"', () => { - const lostChannel = (): Error => - Object.assign(new Error('channel closed'), { sshChannelCloseConfirmed: false }) - - it('proves a Windows host has no Node when the discovery script answers none', async () => { - execCommandMock.mockRejectedValueOnce( - Object.assign(new Error('Node.js not found (exit 1)'), { exitCode: 1, stdout: '' }) - ) - - await expect( - resolveRemoteNodePath(conn, getRemoteHostPlatform('win32-x64'), { strict: true }) - ).rejects.toBeInstanceOf(RemoteNodeNotFoundError) - }) - - it('rethrows a refused channel (MaxSessions) instead of calling it "no Node"', async () => { - const refused = Object.assign(new Error('(SSH) Channel open failure: open failed'), { - reason: 2 - }) - execCommandMock.mockRejectedValueOnce(refused) - - await expect( - resolveRemoteNodePath(conn, getRemoteHostPlatform('win32-x64'), { strict: true }) - ).rejects.toBe(refused) - }) - - it('rethrows a Windows probe the host never answered', async () => { - const lost = lostChannel() - execCommandMock.mockRejectedValueOnce(lost) - - await expect( - resolveRemoteNodePath(conn, getRemoteHostPlatform('win32-x64'), { strict: true }) - ).rejects.toBe(lost) - }) - - it('rethrows a lost POSIX version check rather than calling the candidate unusable', async () => { - const lost = lostChannel() - execCommandMock.mockResolvedValueOnce('/usr/local/bin/node\n').mockRejectedValueOnce(lost) - - await expect(resolveRemoteNodePath(conn, undefined, { strict: true })).rejects.toBe(lost) - }) - - it('proves a POSIX host has no Node when every probe answered', async () => { - execCommandMock - .mockResolvedValueOnce('') - .mockResolvedValueOnce('/bin/bash') - .mockResolvedValueOnce('') - .mockResolvedValueOnce('') - - await expect(resolveRemoteNodePath(conn, undefined, { strict: true })).rejects.toBeInstanceOf( - RemoteNodeNotFoundError - ) - }) - }) }) diff --git a/src/main/startup/main-process-observers.ts b/src/main/startup/main-process-observers.ts index 5b9e69bce58..2c4c7c7d5f6 100644 --- a/src/main/startup/main-process-observers.ts +++ b/src/main/startup/main-process-observers.ts @@ -4,6 +4,7 @@ import { join } from 'node:path' import { AgentAwakeService } from '../power/agent-awake-service' import { normalizeComputerAwakeMode } from '../../shared/computer-awake-mode' import { registerSystemResumeBroadcast } from '../power/system-resume-broadcast' +import { recordWakeSignal } from '../power/wake-signal-clock' import { readDesktopAwayState } from '../notifications/desktop-away-state' import { listCapableSharedControlSockets, @@ -30,12 +31,29 @@ import { runAfterFirstWindowShown } from './main-window/first-window-deferral' const TELEMETRY_CLIENT_START_FALLBACK_MS = 15_000 +function feedWakeSignalClock(): () => void { + const onResume = (): void => recordWakeSignal('resume') + const onUnlock = (): void => recordWakeSignal('unlock-screen') + const onUserActive = (): void => recordWakeSignal('user-did-become-active') + powerMonitor.on('resume', onResume) + powerMonitor.on('unlock-screen', onUnlock) + powerMonitor.on('user-did-become-active', onUserActive) + return () => { + powerMonitor.off('resume', onResume) + powerMonitor.off('unlock-screen', onUnlock) + powerMonitor.off('user-did-become-active', onUserActive) + } +} + export function initializeMainProcessObservers(): void { const store = state.store if (!store) { throw new Error('Store must be initialized before observers') } state.unsubscribeSystemResumeBroadcast = registerSystemResumeBroadcast() + if (process.platform === 'darwin') { + state.unsubscribeWakeSignalClock = feedWakeSignalClock() + } state.stopDesktopPresenceReporter = startDesktopPresenceReporter({ readAway: () => readDesktopAwayState(powerMonitor), listTargets: listCapableSharedControlSockets, diff --git a/src/main/startup/main-process-preflight.ts b/src/main/startup/main-process-preflight.ts index 090ef91e592..66a0e592e2e 100644 --- a/src/main/startup/main-process-preflight.ts +++ b/src/main/startup/main-process-preflight.ts @@ -116,7 +116,7 @@ import { import { ensureWindowsAppDataPath } from './windows-startup/windows-app-data-path' export type MainProcessPreflightOptions = { - focusExistingWindow: () => void + focusExistingWindow: (request: { background?: boolean }) => void requestDesktopActivation: (argv?: readonly string[]) => void } @@ -166,10 +166,10 @@ function initializeMainProcessPreflight(options: MainProcessPreflightOptions): b state.desktopActivationGate = createServeDesktopActivationGate({ // Hold activation until the startup window exists; early second-instance activation otherwise opens a duplicate and aborts launch. initialState: 'initializing', - activateWindow: () => { + activateWindow: (request) => { // Updater replacement must not resurrect the old bundle. if (!isQuittingForUpdate()) { - options.focusExistingWindow() + options.focusExistingWindow(request) } }, onBlocked: (reason) => console.error(`[serve] Desktop activation blocked: ${reason}`) diff --git a/src/main/startup/main-process-state.ts b/src/main/startup/main-process-state.ts index 79efb13c884..864688b8ace 100644 --- a/src/main/startup/main-process-state.ts +++ b/src/main/startup/main-process-state.ts @@ -116,6 +116,7 @@ export const mainProcessState = { crashReports: null as CrashReportStore | null, unsubscribeAgentAwakeStatusChanges: null as (() => void) | null, unsubscribeSystemResumeBroadcast: null as (() => void) | null, + unsubscribeWakeSignalClock: null as (() => void) | null, // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: widens the null slot to the stop function assigned by main-process-observers. stopDesktopPresenceReporter: null as (() => void) | null, watcherShutdownPromise: null as Promise | null, diff --git a/src/main/startup/main-window/main-window-actions.ts b/src/main/startup/main-window/main-window-actions.ts index 0147a1aa3a7..0e98ebcba58 100644 --- a/src/main/startup/main-window/main-window-actions.ts +++ b/src/main/startup/main-window/main-window-actions.ts @@ -47,20 +47,21 @@ import { import { isHoldingForNewerProtocolDaemon } from '../../daemon/providers/routing/daemon-newer-protocol-hold' import { getDaemonProvider } from '../../daemon/providers/lifecycle/daemon-provider-state' import { confirmNoDaemonSessionsRemain } from '../../ipc/pty/pty-management' +import type { MainWindowOpenOptions } from '../../window/main-window/main-window-contracts' +import type { DesktopActivationRequest } from '../serve-launch/serve-desktop-activation' // Inject the opener to avoid an actions/lifecycle import cycle. -let openWindow: (options?: { - revealOnDidFinishLoad?: boolean -}) => BrowserWindow | Promise +let openWindow: (options?: MainWindowOpenOptions) => BrowserWindow | Promise export function setMainWindowOpener( - opener: (options?: { revealOnDidFinishLoad?: boolean }) => BrowserWindow | Promise + opener: (options?: MainWindowOpenOptions) => BrowserWindow | Promise ): void { openWindow = opener } -export function focusExistingWindow(): void { +export function focusExistingWindow(request: DesktopActivationRequest = {}): void { focusExistingMainWindow({ app, + background: request.background === true, getWindow: () => state.mainWindow, openWindow, // Repair rewrites renderer DACLs; second-click activation during a blank launch must wait for the gated opener. diff --git a/src/main/startup/main-window/main-window-controller.ts b/src/main/startup/main-window/main-window-controller.ts index d50a64a321b..560856022c7 100644 --- a/src/main/startup/main-window/main-window-controller.ts +++ b/src/main/startup/main-window/main-window-controller.ts @@ -45,14 +45,18 @@ import { applyBackgroundActivationPolicy, applyServeDesktopWindowActivationPolicy } from '../../window/activation/foreground-activation-policy' +import type { MainWindowOpenOptions } from '../../window/main-window/main-window-contracts' +import { argvRequestsBackgroundOpen } from '../../../shared/background-open-argv' const TRAY_CREATE_FALLBACK_MS = 12_000 const AGENT_STATE_CRASH_BREADCRUMB_MIN_INTERVAL_MS = 30_000 -export async function openMainWindow( - options: { revealOnDidFinishLoad?: boolean } = {} -): Promise { +export async function openMainWindow(options: MainWindowOpenOptions = {}): Promise { logStartupMilestone('open-main-window-start') + // The launch's first window honours the background request `orca open` put on its argv. + const revealInBackground = + options.revealInBackground ?? + (!state.hasOpenedMainWindow && argvRequestsBackgroundOpen(process.argv)) const { store, keybindings } = requireMainWindowServices({ store: state.store, runtime: state.runtime, @@ -91,7 +95,7 @@ export async function openMainWindow( } } if (state.isServeMode) { - applyServeDesktopWindowActivationPolicy({ warn: console.warn }) + applyServeDesktopWindowActivationPolicy({ warn: console.warn, background: revealInBackground }) } const window = createMainWindow(store, { getIsQuitting: () => state.isQuitting, @@ -144,6 +148,7 @@ export async function openMainWindow( }, deferLoad: true, ...(options.revealOnDidFinishLoad === true ? { revealOnDidFinishLoad: true } : {}), + revealInBackground, title: state.devInstanceIdentity?.name ?? app.name, getKeybindings: () => keybindings.getOverrides(), onBeforeReload: ({ ignoreCache, webContentsId }) => { diff --git a/src/main/startup/quit/app-relaunch.ts b/src/main/startup/quit/app-relaunch.ts index 3059783d668..1b535b3d7b2 100644 --- a/src/main/startup/quit/app-relaunch.ts +++ b/src/main/startup/quit/app-relaunch.ts @@ -3,6 +3,7 @@ import type { CrashReportBreadcrumbData } from '../../../shared/crash-reporting' import { recordDurableCrashBreadcrumb } from '../../crash-reporting/breadcrumbs/durable-crash-breadcrumb' import { runWithLaunchPath } from '../shell-environment/hydrate-shell-path' import { getWindowsAdministratorRelaunchOptions } from '../../windows/windows-administrator-restart' +import { BACKGROUND_OPEN_ARG } from '../../../shared/background-open-argv' export type AppRelaunchReason = | 'admin-restart' @@ -20,7 +21,17 @@ export function relaunchApp( ): void { // Persist the cause before relaunch can exit this process. recordDurableCrashBreadcrumb('app_relaunch_requested', { ...data, reason }) - runWithLaunchPath(() => app.relaunch(options ?? getWindowsAdministratorRelaunchOptions())) + runWithLaunchPath(() => app.relaunch(options ?? defaultRelaunchOptions())) +} + +// The user restarted Orca, so the new window comes forward even when `orca open` started this one. +function defaultRelaunchOptions(): Electron.RelaunchOptions | undefined { + const administrator = getWindowsAdministratorRelaunchOptions() + if (!administrator && !process.argv.includes(BACKGROUND_OPEN_ARG)) { + return undefined + } + const args = administrator?.args ?? process.argv.slice(1) + return { ...administrator, args: args.filter((arg) => arg !== BACKGROUND_OPEN_ARG) } } export async function runBeforeRelaunchCleanup( diff --git a/src/main/startup/quit/main-process-quit.ts b/src/main/startup/quit/main-process-quit.ts index 45256cf5bc5..dd88702fc57 100644 --- a/src/main/startup/quit/main-process-quit.ts +++ b/src/main/startup/quit/main-process-quit.ts @@ -121,6 +121,8 @@ function installWillQuitHandler(): void { state.desktopPushService?.stop() state.unsubscribeSystemResumeBroadcast?.() state.unsubscribeSystemResumeBroadcast = null + state.unsubscribeWakeSignalClock?.() + state.unsubscribeWakeSignalClock = null state.stopDesktopPresenceReporter?.() state.stopDesktopPresenceReporter = null // Keep log-stream diagnostics alive through renderer quit vetoes. diff --git a/src/main/startup/serve-launch/serve-desktop-activation.test.ts b/src/main/startup/serve-launch/serve-desktop-activation.test.ts index 18e3e147771..0b508d474dd 100644 --- a/src/main/startup/serve-launch/serve-desktop-activation.test.ts +++ b/src/main/startup/serve-launch/serve-desktop-activation.test.ts @@ -2,40 +2,15 @@ import { describe, expect, it, vi } from 'vitest' import { createServeDesktopActivationGate } from './serve-desktop-activation' describe('createServeDesktopActivationGate', () => { - it('coalesces activation requests while serve is initializing and drains once when ready', () => { + it('lets a queued user activation keep the foreground over a background open', () => { const activateWindow = vi.fn() - const gate = createServeDesktopActivationGate({ - initialState: 'initializing', - activateWindow - }) + const gate = createServeDesktopActivationGate({ initialState: 'initializing', activateWindow }) + gate.requestActivation({ background: true }) gate.requestActivation() - gate.requestActivation() - - expect(activateWindow).not.toHaveBeenCalled() - expect(gate.getState()).toBe('initializing') - + gate.requestActivation({ background: true }) gate.markReady() - expect(activateWindow).toHaveBeenCalledOnce() - expect(gate.getState()).toBe('ready') - }) - - it('drops pending activation and fails closed when promotion is blocked', () => { - const activateWindow = vi.fn() - const onBlocked = vi.fn() - const gate = createServeDesktopActivationGate({ - initialState: 'initializing', - activateWindow, - onBlocked - }) - - gate.requestActivation() - gate.markBlocked('persistent PTY provider unavailable') - gate.requestActivation() - - expect(activateWindow).not.toHaveBeenCalled() - expect(onBlocked).toHaveBeenCalledTimes(2) - expect(gate.getState()).toBe('blocked') + expect(activateWindow.mock.calls).toEqual([[{ background: false }]]) }) }) diff --git a/src/main/startup/serve-launch/serve-desktop-activation.ts b/src/main/startup/serve-launch/serve-desktop-activation.ts index 76611e00aa4..bcf3f06167b 100644 --- a/src/main/startup/serve-launch/serve-desktop-activation.ts +++ b/src/main/startup/serve-launch/serve-desktop-activation.ts @@ -2,9 +2,11 @@ import type { RuntimeDesktopWindowStatus } from '../../../shared/runtime-session type ActivationGateState = Exclude | 'ready' +export type DesktopActivationRequest = { background?: boolean } + export type ServeDesktopActivationGate = { getState: () => ActivationGateState - requestActivation: () => void + requestActivation: (request?: DesktopActivationRequest) => void markReady: () => void markBlocked: (reason: string) => void } @@ -44,22 +46,24 @@ export function releaseDesktopActivationAfter( export function createServeDesktopActivationGate(options: { initialState: 'initializing' | 'ready' - activateWindow: () => void + activateWindow: (request: DesktopActivationRequest) => void onBlocked?: (reason: string) => void }): ServeDesktopActivationGate { let state: ActivationGateState = options.initialState - let pendingActivation = false + let pendingActivation: DesktopActivationRequest | null = null let blockedReason = 'desktop activation is unavailable' return { getState: () => state, - requestActivation: () => { + requestActivation: (request = {}) => { if (state === 'ready') { - options.activateWindow() + options.activateWindow(request) return } if (state === 'initializing') { - pendingActivation = true + // Any queued user activation keeps its claim to the foreground. + const background = request.background === true && pendingActivation?.background !== false + pendingActivation = { background } return } options.onBlocked?.(blockedReason) @@ -70,8 +74,9 @@ export function createServeDesktopActivationGate(options: { } state = 'ready' if (pendingActivation) { - pendingActivation = false - options.activateWindow() + const request = pendingActivation + pendingActivation = null + options.activateWindow(request) } }, markBlocked: (reason) => { @@ -81,7 +86,7 @@ export function createServeDesktopActivationGate(options: { state = 'blocked' blockedReason = reason if (pendingActivation) { - pendingActivation = false + pendingActivation = null options.onBlocked?.(blockedReason) } } diff --git a/src/main/startup/serve-launch/serve-mode-argv.test.ts b/src/main/startup/serve-launch/serve-mode-argv.test.ts index 398880d84a8..81215fb19fc 100644 --- a/src/main/startup/serve-launch/serve-mode-argv.test.ts +++ b/src/main/startup/serve-launch/serve-mode-argv.test.ts @@ -47,24 +47,6 @@ describe('serve-mode-argv', () => { ]) }) - it('leaves real GUI launch argv alone', () => { - // False serve detection replaces a desktop window with a listening runtime; exercise actual desktop argv shapes. - for (const argv of [ - ['/Applications/Orca.app/Contents/MacOS/Orca', '-psn_0_123456'], - ['C:\\Program Files\\Orca\\Orca.exe', '--squirrel-firstrun'], - ['C:\\Program Files\\Orca\\Orca.exe', 'orca://worktree/serve'], - ['/opt/orca/orca-ide', '/home/u/serve'], - // Value tokens named serve are never subcommands. - ['/opt/orca/orca-ide', '--pairing-code', 'serve'], - ['/opt/orca/orca-ide', '--environment=serve'], - ['/opt/orca/orca-ide', '--', 'serve'], - ['/opt/orca/orca-ide', 'Serve'] - ]) { - expect(argvRequestsServeMode(argv), argv.join(' ')).toBe(false) - expect(normalizeServeModeArgv(argv)).toEqual(argv) - } - }) - it('does not treat a later positional serve as the subcommand after another command', () => { expect(findServeSubcommandIndex(['app', 'status', 'serve'])).toBe(-1) expect(argvRequestsServeMode(['app', 'status', 'serve'])).toBe(false) @@ -78,14 +60,6 @@ describe('serve-mode-argv', () => { ]) }) - it('does not splice prototype members onto argv for stray positionals', () => { - expect(normalizeServeModeArgv(['/AppRun', 'serve', 'toString'])).toEqual([ - '/AppRun', - '--serve', - 'toString' - ]) - }) - it('passes args after `--` through verbatim', () => { expect(normalizeServeModeArgv(['/AppRun', 'serve', '--json', '--', '--port', '1'])).toEqual([ '/AppRun', diff --git a/src/main/startup/serve-launch/serve-pinned-bind-host.test.ts b/src/main/startup/serve-launch/serve-pinned-bind-host.test.ts index 3d30df04722..2068b0e8ead 100644 --- a/src/main/startup/serve-launch/serve-pinned-bind-host.test.ts +++ b/src/main/startup/serve-launch/serve-pinned-bind-host.test.ts @@ -5,13 +5,7 @@ import { getServeOptions } from './serve-options' import './serve-pinned-bind-host' describe('Electron serve listener pin', () => { - it('rejects hostnames, empty values and --mobile-pairing with a narrower bind', () => { - expect(() => getServeOptions(['/AppRun', '--serve', '--serve-bind'])).toThrow( - 'Missing value for --serve-bind.' - ) - expect(() => getServeOptions(['/AppRun', '--serve', '--serve-bind', 'host.example'])).toThrow( - /literal IP address/ - ) + it('rejects --mobile-pairing with a narrower bind', () => { expect(() => getServeOptions(['/AppRun', '--serve', '--serve-bind', '127.0.0.1', '--serve-mobile-pairing']) ).toThrow(/--mobile-pairing needs --bind 0\.0\.0\.0/) diff --git a/src/main/startup/serve-launch/serve-user-data-path-guard.test.ts b/src/main/startup/serve-launch/serve-user-data-path-guard.test.ts index e5be57f3dcd..23c90ed011a 100644 --- a/src/main/startup/serve-launch/serve-user-data-path-guard.test.ts +++ b/src/main/startup/serve-launch/serve-user-data-path-guard.test.ts @@ -2,14 +2,6 @@ import { describe, expect, it, vi } from 'vitest' import { checkServeUserDataPath } from './serve-user-data-path-guard' describe('checkServeUserDataPath', () => { - it('refuses a serve that landed on a profile other than the one the CLI chose', () => { - const env: NodeJS.ProcessEnv = { ORCA_SERVE_USER_DATA_PATH: '/isolated/profile' } - expect( - checkServeUserDataPath({ isServeMode: true, userDataPath: '/default/orca', env }) - ).toContain('/isolated/profile') - expect(env.ORCA_SERVE_USER_DATA_PATH).toBeUndefined() - }) - it('allows the chosen profile, including another spelling of it', () => { expect( checkServeUserDataPath({ @@ -27,13 +19,6 @@ describe('checkServeUserDataPath', () => { }) ).toBeNull() }) - - it('leaves launches without a CLI choice, or outside serve, alone', () => { - expect(checkServeUserDataPath({ isServeMode: true, userDataPath: '/a', env: {} })).toBeNull() - const env: NodeJS.ProcessEnv = { ORCA_SERVE_USER_DATA_PATH: '/b' } - expect(checkServeUserDataPath({ isServeMode: false, userDataPath: '/a', env })).toBeNull() - expect(env.ORCA_SERVE_USER_DATA_PATH).toBeUndefined() - }) }) describe('configureOrcaUserDataPathEnv on a serve launch', () => { diff --git a/src/main/tray/system-tray.test.ts b/src/main/tray/system-tray.test.ts deleted file mode 100644 index 6f146f1f144..00000000000 --- a/src/main/tray/system-tray.test.ts +++ /dev/null @@ -1,649 +0,0 @@ -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import type * as SystemTrayModule from './system-tray' - -const { - attentionImage, - baseMacImage, - composeAttentionMock, - createAppIconImageMock, - createFromPathMock, - devBadgeImage, - devBadgeRetinaImage, - menuFromTemplateMock, - nativeThemeMock, - resizedImage, - retinaMacImage, - stampDevBadgeMock, - themeState, - tintedMacImage, - tintTemplateMock, - trayInstances -} = vi.hoisted(() => { - const makeImage = (name: string) => ({ - name, - addRepresentation: vi.fn(), - getScaleFactors: vi.fn(() => [1, 2]), - getSize: vi.fn(() => ({ width: 16, height: 16 })), - setTemplateImage: vi.fn(), - toBitmap: vi.fn(() => Buffer.alloc(16 * 16 * 4)), - toDataURL: vi.fn(() => `data:image/png;base64,${name}`) - }) - const resizedImage = makeImage('windows') - const baseMacImage = makeImage('mac-base') - const retinaMacImage = makeImage('mac-retina') - const tintedMacImage = makeImage('mac-tinted') - const attentionImage = makeImage('attention') - const devBadgeImage = makeImage('dev-badge') - const devBadgeRetinaImage = makeImage('dev-badge-retina') - const themeState = { updatedListener: null as (() => void) | null } - const nativeThemeMock = { - shouldUseDarkColors: false, - on: vi.fn((_event: string, listener: () => void) => { - themeState.updatedListener = listener - }), - removeListener: vi.fn((_event: string, listener: () => void) => { - if (themeState.updatedListener === listener) { - themeState.updatedListener = null - } - }) - } - return { - attentionImage, - baseMacImage, - composeAttentionMock: vi.fn(() => attentionImage), - createAppIconImageMock: vi.fn(), - createFromPathMock: vi.fn(), - devBadgeImage, - devBadgeRetinaImage, - menuFromTemplateMock: vi.fn((template: unknown) => ({ template })), - nativeThemeMock, - resizedImage, - retinaMacImage, - stampDevBadgeMock: vi.fn((_base: unknown, scaleFactor?: number) => - scaleFactor === 2 ? devBadgeRetinaImage : devBadgeImage - ), - themeState, - tintedMacImage, - tintTemplateMock: vi.fn(() => tintedMacImage), - trayInstances: [] as FakeTray[] - } -}) - -class FakeTray { - setToolTip = vi.fn() - setTitle = vi.fn() - setContextMenu = vi.fn() - setImage = vi.fn() - on = vi.fn() - destroy = vi.fn() - isDestroyed = vi.fn(() => false) - constructor(public readonly image: unknown) { - trayInstances.push(this) - } -} - -vi.mock('electron', () => ({ - Tray: FakeTray, - Menu: { buildFromTemplate: menuFromTemplateMock }, - nativeImage: { createFromPath: createFromPathMock }, - nativeTheme: nativeThemeMock -})) - -vi.mock('../../../resources/tray/orca-menu-barTemplate.png?asset&asarUnpack', () => ({ - default: '/assets/orca-menu-barTemplate.png' -})) - -vi.mock('../../../resources/tray/orca-menu-barTemplate@2x.png?asset&asarUnpack', () => ({ - default: '/assets/orca-menu-barTemplate@2x.png' -})) - -vi.mock('../window/desktop-integration/app-icon', () => ({ - createAppIconImage: createAppIconImageMock -})) - -vi.mock('./tray-attention-icon', () => ({ - composeTrayAttentionIcon: composeAttentionMock, - tintTrayTemplateForAttention: tintTemplateMock -})) - -vi.mock('./tray-dev-badge', () => ({ - stampTrayDevBadge: stampDevBadgeMock -})) - -type TrayModule = typeof SystemTrayModule -type MenuItem = { label?: string; type?: string; click?: () => void } - -const originalPlatform = process.platform - -function setPlatform(platform: NodeJS.Platform): void { - Object.defineProperty(process, 'platform', { value: platform, configurable: true }) -} - -async function loadModule(): Promise { - vi.resetModules() - return import('./system-tray') -} - -function createOptions( - overrides: { isDevInstance?: boolean; devInstanceLabel?: string | null } = {} -) { - return { - appIcon: 'classic', - isDevInstance: overrides.isDevInstance ?? false, - devInstanceLabel: overrides.devInstanceLabel ?? null, - onOpen: vi.fn(), - onOpenSettings: vi.fn(), - onCheckForUpdates: vi.fn(), - onQuit: vi.fn() - } -} - -function builtMenuItems(): MenuItem[] { - return menuFromTemplateMock.mock.calls.at(-1)?.[0] as MenuItem[] -} - -// Flush the deferred native scene mutation after the AppKit caller stack returns. -function flushTraySceneMutation(): void { - vi.advanceTimersByTime(0) -} - -beforeEach(() => { - vi.useFakeTimers() - trayInstances.length = 0 - menuFromTemplateMock.mockClear() - composeAttentionMock.mockClear() - tintTemplateMock.mockClear() - createAppIconImageMock.mockReset() - createAppIconImageMock.mockReturnValue({ resize: vi.fn(() => resizedImage) }) - createFromPathMock.mockReset() - createFromPathMock.mockImplementation((path: string) => - path.includes('@2x') ? retinaMacImage : baseMacImage - ) - stampDevBadgeMock.mockClear() - stampDevBadgeMock.mockImplementation((_base: unknown, scaleFactor?: number) => - scaleFactor === 2 ? devBadgeRetinaImage : devBadgeImage - ) - for (const image of [ - baseMacImage, - retinaMacImage, - tintedMacImage, - attentionImage, - devBadgeImage, - devBadgeRetinaImage - ]) { - image.addRepresentation.mockClear() - image.getScaleFactors.mockReset().mockReturnValue([1, 2]) - image.getSize.mockReset().mockReturnValue({ width: 16, height: 16 }) - image.setTemplateImage.mockClear() - image.toBitmap.mockClear() - image.toDataURL.mockClear().mockReturnValue(`data:image/png;base64,${image.name}`) - } - nativeThemeMock.shouldUseDarkColors = false - nativeThemeMock.on.mockClear() - nativeThemeMock.removeListener.mockClear() - themeState.updatedListener = null -}) - -afterEach(() => { - setPlatform(originalPlatform) - vi.useRealTimers() - vi.restoreAllMocks() -}) - -describe('createSystemTray', () => { - it('keeps the Windows icon, Open/Quit menu, and left-click behavior', async () => { - setPlatform('win32') - const { createSystemTray } = await loadModule() - const options = createOptions() - - createSystemTray(options) - - expect(trayInstances).toHaveLength(1) - expect(trayInstances[0].image).toBe(resizedImage) - expect(trayInstances[0].setToolTip).toHaveBeenCalledWith('Orca') - expect(builtMenuItems().map((item) => item.label)).toEqual(['Open Orca', undefined, 'Quit']) - const clickHandler = trayInstances[0].on.mock.calls.find((call) => call[0] === 'click')?.[1] - expect(clickHandler).toBeTypeOf('function') - - builtMenuItems()[0].click?.() - ;(clickHandler as () => void)() - builtMenuItems()[2].click?.() - expect(options.onOpen).toHaveBeenCalledTimes(2) - expect(options.onQuit).toHaveBeenCalledOnce() - }) - - it('creates a macOS template status item with the full native menu', async () => { - setPlatform('darwin') - const { createSystemTray } = await loadModule() - const options = createOptions() - - createSystemTray(options) - - expect(trayInstances).toHaveLength(1) - expect(trayInstances[0].image).toBe(baseMacImage) - expect(baseMacImage.setTemplateImage).toHaveBeenCalledWith(true) - expect(baseMacImage.addRepresentation).toHaveBeenCalledWith({ - scaleFactor: 2, - dataURL: 'data:image/png;base64,mac-retina' - }) - expect(builtMenuItems().map((item) => item.label)).toEqual([ - 'Open Orca', - undefined, - 'Settings', - 'Check for Updates...', - undefined, - 'Quit' - ]) - expect(trayInstances[0].on).not.toHaveBeenCalled() - expect(nativeThemeMock.on).toHaveBeenCalledWith('updated', expect.any(Function)) - - for (const [label, callback] of [ - ['Open Orca', options.onOpen], - ['Settings', options.onOpenSettings], - ['Check for Updates...', options.onCheckForUpdates], - ['Quit', options.onQuit] - ] as const) { - builtMenuItems() - .find((item) => item.label === label) - ?.click?.() - expect(callback).toHaveBeenCalledOnce() - } - }) - - it.each(['win32', 'darwin'] as const)( - 'offers Quit Completely directly above Quit on %s when a handler is supplied', - async (platform) => { - setPlatform(platform) - const { createSystemTray } = await loadModule() - const options = { ...createOptions(), onQuitAndStopAllSessions: vi.fn() } - - createSystemTray(options) - - expect(builtMenuItems().map((item) => item.label)).toEqual( - platform === 'darwin' - ? [ - 'Open Orca', - undefined, - 'Settings', - 'Check for Updates...', - undefined, - 'Quit Completely...', - 'Quit' - ] - : ['Open Orca', undefined, 'Quit Completely...', 'Quit'] - ) - builtMenuItems().at(-2)?.click?.() - expect(options.onQuitAndStopAllSessions).toHaveBeenCalledOnce() - expect(options.onQuit).not.toHaveBeenCalled() - } - ) - - it('does not create a blank macOS item when the template asset fails to load', async () => { - setPlatform('darwin') - baseMacImage.getSize.mockReturnValue({ width: 0, height: 0 }) - const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) - const { createSystemTray } = await loadModule() - - expect(createSystemTray(createOptions())).toBeNull() - expect(trayInstances).toHaveLength(0) - expect(warn).toHaveBeenCalledWith('[system-tray] macOS menu bar icon could not be loaded') - }) - - it('is idempotent and remains disabled on Linux', async () => { - setPlatform('darwin') - const { createSystemTray } = await loadModule() - const options = createOptions() - expect(createSystemTray(options)).toBe(createSystemTray(options)) - expect(trayInstances).toHaveLength(1) - - setPlatform('linux') - const linuxModule = await loadModule() - expect(linuxModule.createSystemTray(options)).toBeNull() - }) -}) - -describe('dev instance indicator', () => { - it('falls back to the plain icon when badge stamping fails', async () => { - setPlatform('darwin') - const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) - stampDevBadgeMock.mockImplementation(() => { - throw new Error('bitmap failure') - }) - const { createSystemTray } = await loadModule() - - expect(createSystemTray(createOptions({ isDevInstance: true }))).not.toBeNull() - expect(trayInstances[0].image).toBe(baseMacImage) - expect(warn).toHaveBeenCalledWith( - '[system-tray] dev badge could not be stamped; showing plain icon', - expect.any(Error) - ) - }) -}) - -describe('macOS visibility', () => { - it('creates and destroys the item as the setting toggles', async () => { - setPlatform('darwin') - const { setMacMenuBarIconVisible } = await loadModule() - const options = createOptions() - - expect(setMacMenuBarIconVisible(false, options)).toBeNull() - expect(trayInstances).toHaveLength(0) - expect(setMacMenuBarIconVisible(true, options)).not.toBeNull() - expect(trayInstances).toHaveLength(1) - - setMacMenuBarIconVisible(false, options) - setMacMenuBarIconVisible(false, options) - expect(trayInstances[0].destroy).toHaveBeenCalledOnce() - }) -}) - -describe('setTrayAttention', () => { - it('preserves the Windows attention swap behavior', async () => { - setPlatform('win32') - const { createSystemTray, setTrayAttention } = await loadModule() - createSystemTray(createOptions()) - const created = trayInstances[0] - created.setImage.mockClear() - - setTrayAttention(true) - flushTraySceneMutation() - expect(composeAttentionMock).toHaveBeenCalledWith(resizedImage) - expect(created.setImage).toHaveBeenCalledWith(attentionImage) - setTrayAttention(false) - flushTraySceneMutation() - expect(created.setImage).toHaveBeenLastCalledWith(resizedImage) - }) - - it('uses a literal theme-aware glyph for macOS attention, then restores template mode', async () => { - setPlatform('darwin') - const { createSystemTray, setTrayAttention } = await loadModule() - createSystemTray(createOptions()) - const created = trayInstances[0] - created.setImage.mockClear() - created.setToolTip.mockClear() - - setTrayAttention(true) - flushTraySceneMutation() - expect(tintTemplateMock).toHaveBeenCalledWith(baseMacImage, false) - expect(composeAttentionMock).toHaveBeenCalledWith(tintedMacImage) - // Bitmap rebuild starts at 1x; re-add 2x or attention glyphs blur on Retina. - expect(tintTemplateMock).toHaveBeenCalledWith(baseMacImage, false, 2) - expect(attentionImage.addRepresentation).toHaveBeenCalledWith({ - scaleFactor: 2, - dataURL: 'data:image/png;base64,attention' - }) - expect(attentionImage.setTemplateImage).toHaveBeenCalledWith(false) - expect(created.setImage).toHaveBeenCalledWith(attentionImage) - expect(created.setToolTip).toHaveBeenCalledWith('Orca - activity waiting') - - setTrayAttention(false) - flushTraySceneMutation() - expect(baseMacImage.setTemplateImage).toHaveBeenLastCalledWith(true) - expect(created.setImage).toHaveBeenLastCalledWith(baseMacImage) - expect(created.setToolTip).toHaveBeenLastCalledWith('Orca') - }) - - it('recomposes active macOS attention when the system appearance changes', async () => { - setPlatform('darwin') - const { createSystemTray, setTrayAttention } = await loadModule() - createSystemTray(createOptions()) - setTrayAttention(true) - flushTraySceneMutation() - tintTemplateMock.mockClear() - nativeThemeMock.shouldUseDarkColors = true - - themeState.updatedListener?.() - // Appearance events also arrive from AppKit; defer their native writes. - expect(tintTemplateMock).not.toHaveBeenCalled() - - flushTraySceneMutation() - expect(tintTemplateMock).toHaveBeenCalledWith(baseMacImage, true) - }) - - it('reflects attention requested before deferred macOS creation', async () => { - setPlatform('darwin') - const { createSystemTray, setTrayAttention } = await loadModule() - setTrayAttention(true) - createSystemTray(createOptions()) - - expect(trayInstances[0].setImage).toHaveBeenCalledWith(attentionImage) - }) - - it('ignores repeated same-state attention calls', async () => { - setPlatform('win32') - const { createSystemTray, setTrayAttention } = await loadModule() - createSystemTray(createOptions()) - const created = trayInstances[0] - created.setImage.mockClear() - - setTrayAttention(true) - setTrayAttention(true) - flushTraySceneMutation() - - expect(created.setImage).toHaveBeenCalledTimes(1) - }) - - it('never touches the NSStatusItem scene inside the caller stack', async () => { - setPlatform('darwin') - const { createSystemTray, setTrayAttention } = await loadModule() - createSystemTray(createOptions()) - const created = trayInstances[0] - created.setImage.mockClear() - created.setToolTip.mockClear() - - // Native scene writes inside AppKit window-state dispatch can deadlock main on macOS 26. - setTrayAttention(true) - expect(created.setImage).not.toHaveBeenCalled() - expect(created.setToolTip).not.toHaveBeenCalled() - - flushTraySceneMutation() - expect(created.setImage).toHaveBeenCalledWith(attentionImage) - }) - - it('collapses a burst of toggles into one deferred repaint', async () => { - setPlatform('darwin') - const { createSystemTray, setTrayAttention } = await loadModule() - createSystemTray(createOptions()) - const created = trayInstances[0] - created.setImage.mockClear() - - setTrayAttention(true) - setTrayAttention(false) - setTrayAttention(true) - flushTraySceneMutation() - - expect(created.setImage).toHaveBeenCalledTimes(1) - expect(created.setImage).toHaveBeenCalledWith(attentionImage) - }) - - it('still dedupes after the deferred repaint has run', async () => { - setPlatform('darwin') - const { createSystemTray, setTrayAttention } = await loadModule() - createSystemTray(createOptions()) - const created = trayInstances[0] - created.setImage.mockClear() - - setTrayAttention(true) - flushTraySceneMutation() - setTrayAttention(true) - flushTraySceneMutation() - - expect(created.setImage).toHaveBeenCalledTimes(1) - }) - - it('does not throw when the tray is destroyed before the deferred repaint runs', async () => { - setPlatform('darwin') - const { createSystemTray, destroySystemTray, setTrayAttention } = await loadModule() - createSystemTray(createOptions()) - const created = trayInstances[0] - created.setImage.mockClear() - - setTrayAttention(true) - destroySystemTray() - - expect(() => flushTraySceneMutation()).not.toThrow() - expect(created.setImage).not.toHaveBeenCalled() - }) - - it('keeps a pending attention dot across a macOS hide/show toggle', async () => { - setPlatform('darwin') - const { setMacMenuBarIconVisible, setTrayAttention } = await loadModule() - const options = createOptions() - setMacMenuBarIconVisible(true, options) - setTrayAttention(true) - - setMacMenuBarIconVisible(false, options) - setMacMenuBarIconVisible(true, options) - - expect(trayInstances).toHaveLength(2) - expect(trayInstances[1].setImage).toHaveBeenCalledWith(attentionImage) - }) -}) - -describe('destroySystemTray', () => { - it('destroys idempotently and removes the macOS appearance listener', async () => { - setPlatform('darwin') - const { createSystemTray, destroySystemTray } = await loadModule() - createSystemTray(createOptions()) - const created = trayInstances[0] - const listener = themeState.updatedListener - - destroySystemTray() - destroySystemTray() - - expect(created.destroy).toHaveBeenCalledOnce() - expect(nativeThemeMock.removeListener).toHaveBeenCalledWith('updated', listener) - }) -}) - -describe('macOS hardening', () => { - it('warns but still creates the item when only the @2x asset fails to load', async () => { - setPlatform('darwin') - retinaMacImage.getSize.mockReturnValue({ width: 0, height: 0 }) - const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) - const { createSystemTray } = await loadModule() - - expect(createSystemTray(createOptions())).not.toBeNull() - expect(trayInstances).toHaveLength(1) - expect(baseMacImage.addRepresentation).not.toHaveBeenCalled() - expect(warn).toHaveBeenCalledWith( - '[system-tray] macOS retina menu bar icon could not be loaded' - ) - }) - - it('degrades to the plain icon instead of throwing when attention composition fails', async () => { - setPlatform('darwin') - const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) - const { createSystemTray, setTrayAttention } = await loadModule() - createSystemTray(createOptions()) - const created = trayInstances[0] - created.setImage.mockClear() - composeAttentionMock.mockImplementationOnce(() => { - throw new Error('native image failure') - }) - - setTrayAttention(true) - expect(() => flushTraySceneMutation()).not.toThrow() - expect(created.setImage).toHaveBeenLastCalledWith(baseMacImage) - expect(created.setToolTip).toHaveBeenLastCalledWith('Orca') - expect(warn).toHaveBeenCalledWith( - '[system-tray] macOS attention icon failed; showing plain icon', - expect.any(Error) - ) - }) - - it('contains a throwing menu-item callback instead of crashing', async () => { - setPlatform('darwin') - const error = vi.spyOn(console, 'error').mockImplementation(() => {}) - const { createSystemTray } = await loadModule() - const options = createOptions() - options.onOpenSettings.mockImplementation(() => { - throw new Error('boom') - }) - createSystemTray(options) - - const settingsItem = builtMenuItems().find((item) => item.label === 'Settings') - expect(() => settingsItem?.click?.()).not.toThrow() - expect(error).toHaveBeenCalledWith('[system-tray] menu action failed', expect.any(Error)) - }) -}) - -describe('language changes', () => { - it.each(['darwin', 'win32'] as const)( - 'refreshes the existing %s menu and keeps its actions', - async (platform) => { - setPlatform(platform) - const { createSystemTray, rebuildSystemTrayMenu } = await loadModule() - const { setMainUiLanguage } = await import('../i18n/main-i18n') - await setMainUiLanguage('en') - const options = { ...createOptions(), onQuitAndStopAllSessions: vi.fn() } - createSystemTray(options) - expect(builtMenuItems()[0].label).toBe('Open Orca') - - await setMainUiLanguage('ja') - rebuildSystemTrayMenu() - expect(builtMenuItems()[0].label).toBe('Open Orca') - flushTraySceneMutation() - - expect(trayInstances).toHaveLength(1) - expect(trayInstances[0].setContextMenu).toHaveBeenCalledTimes(2) - expect(builtMenuItems()[0].label).toBe('Orca を開く') - expect(builtMenuItems().at(-2)?.label).toBe('完全に終了…') - expect(builtMenuItems().at(-1)?.label).toBe('終了') - builtMenuItems()[0].click?.() - builtMenuItems().at(-2)?.click?.() - builtMenuItems().at(-1)?.click?.() - expect(options.onOpen).toHaveBeenCalledOnce() - expect(options.onQuitAndStopAllSessions).toHaveBeenCalledOnce() - expect(options.onQuit).toHaveBeenCalledOnce() - if (platform === 'darwin') { - expect(builtMenuItems()[2].label).toBe('設定') - expect(builtMenuItems()[3].label).toBe('アップデートを確認…') - builtMenuItems()[2].click?.() - builtMenuItems()[3].click?.() - expect(options.onOpenSettings).toHaveBeenCalledOnce() - expect(options.onCheckForUpdates).toHaveBeenCalledOnce() - expect(nativeThemeMock.on).toHaveBeenCalledTimes(1) - } else { - expect(trayInstances[0].on).toHaveBeenCalledTimes(1) - } - } - ) - - it('refreshes the attention tooltip and replacements in the active language catalog', async () => { - setPlatform('darwin') - const { createSystemTray, rebuildSystemTrayMenu, setTrayAttention } = await loadModule() - const { mainI18n, setMainUiLanguage } = await import('../i18n/main-i18n') - await setMainUiLanguage('en') - createSystemTray(createOptions({ isDevInstance: true, devInstanceLabel: 'language-test' })) - setTrayAttention(true) - flushTraySceneMutation() - await setMainUiLanguage('ja') - rebuildSystemTrayMenu() - flushTraySceneMutation() - expect(trayInstances[0].setToolTip).toHaveBeenLastCalledWith( - 'Orca DEV (language-test) - 件のアクティビティが待機中' - ) - expect(builtMenuItems()[0].label).toBe('Orca DEV (language-test)') - expect(composeAttentionMock).toHaveBeenCalled() - - mainI18n.addResource('ja', 'translation', 'tray.openOrca', 'Orca を表示') - rebuildSystemTrayMenu() - flushTraySceneMutation() - expect(builtMenuItems()[2].label).toBe('Orca を表示') - }) - - it('does nothing without a tray or when destroyed before the deferred refresh', async () => { - setPlatform('darwin') - const { createSystemTray, destroySystemTray, rebuildSystemTrayMenu } = await loadModule() - rebuildSystemTrayMenu() - flushTraySceneMutation() - expect(menuFromTemplateMock).not.toHaveBeenCalled() - createSystemTray(createOptions()) - rebuildSystemTrayMenu() - destroySystemTray() - menuFromTemplateMock.mockClear() - flushTraySceneMutation() - expect(menuFromTemplateMock).not.toHaveBeenCalled() - }) -}) diff --git a/src/main/tray/tray-dev-badge.test.ts b/src/main/tray/tray-dev-badge.test.ts deleted file mode 100644 index 7e87d0c975f..00000000000 --- a/src/main/tray/tray-dev-badge.test.ts +++ /dev/null @@ -1,81 +0,0 @@ -import { describe, expect, it, vi } from 'vitest' - -const createFromBitmapMock = vi.hoisted(() => - vi.fn((buffer: Buffer, options: { width: number; height: number }) => ({ - __image: true, - buffer, - ...options - })) -) - -vi.mock('electron', () => ({ - nativeImage: { createFromBitmap: createFromBitmapMock } -})) - -import { stampTrayDevBadge } from './tray-dev-badge' - -function fakeTemplate(width: number, height: number) { - // All-transparent base so any opaque pixel in the result is the badge. - return { - getSize: () => ({ width, height }), - toBitmap: vi.fn((options?: { scaleFactor?: number }) => { - const scale = options?.scaleFactor ?? 1 - return Buffer.alloc(width * scale * height * scale * 4, 0) - }) - } -} - -describe('stampTrayDevBadge', () => { - it('keeps the template canvas size and stamps opaque template-black pixels', () => { - createFromBitmapMock.mockClear() - const width = 22 - const height = 14 - stampTrayDevBadge(fakeTemplate(width, height) as never) - - const [bitmap, options] = createFromBitmapMock.mock.calls[0] - expect(options).toEqual({ width, height }) - - let stamped = 0 - let maxX = 0 - let maxY = 0 - for (let y = 0; y < height; y++) { - for (let x = 0; x < width; x++) { - const o = (y * width + x) * 4 - if (bitmap[o + 3] === 0xff) { - expect([bitmap[o], bitmap[o + 1], bitmap[o + 2]]).toEqual([0x00, 0x00, 0x00]) - stamped++ - maxX = Math.max(maxX, x) - maxY = Math.max(maxY, y) - } - } - } - expect(stamped).toBeGreaterThan(0) - expect(maxX).toBeLessThan(width) - expect(maxY).toBeLessThan(height) - }) - - it('reads and scales the requested Retina representation', () => { - const opaque = (buffer: Buffer): number => { - let count = 0 - for (let o = 3; o < buffer.length; o += 4) { - if (buffer[o] === 0xff) { - count++ - } - } - return count - } - - createFromBitmapMock.mockClear() - const base = fakeTemplate(22, 14) - stampTrayDevBadge(base as never, 2) - expect(base.toBitmap).toHaveBeenCalledWith({ scaleFactor: 2 }) - const [bitmap2x, options2x] = createFromBitmapMock.mock.calls[0] - expect(options2x).toEqual({ width: 44, height: 28 }) - - createFromBitmapMock.mockClear() - stampTrayDevBadge(fakeTemplate(22, 14) as never, 1) - const [bitmap1x] = createFromBitmapMock.mock.calls[0] - - expect(opaque(bitmap2x)).toBe(opaque(bitmap1x) * 4) - }) -}) diff --git a/src/main/updater/linux-package/linux-package-update-recovery.test.ts b/src/main/updater/linux-package/linux-package-update-recovery.test.ts index b6a745edf35..b0c94fea8a5 100644 --- a/src/main/updater/linux-package/linux-package-update-recovery.test.ts +++ b/src/main/updater/linux-package/linux-package-update-recovery.test.ts @@ -156,18 +156,6 @@ describe('captureLinuxPackageArtifact', () => { expect(capture({ files: [{ url: 'orca.deb', sha512: 'not-a-digest' }] })).toBeNull() expect(recovery.getTrackedLinuxPackageArtifact()?.sha512).toBe(SHA512) }) - - it('does not arm recovery from a download with no usable digest', () => { - capture({ files: [{ url: 'orca.deb' }] }) - capture({ files: [{ url: 'orca%ZZ.deb', sha512: SHA512 }] }) - capture({ - files: [ - { url: 'orca.deb', sha512: SHA512 }, - { url: 'https://mirror.example.com/orca.deb', sha512: 'conflicting-digest' } - ] - }) - expect(recovery.getTrackedLinuxPackageArtifact()).toBeNull() - }) }) describePosix('resolveLinuxPackageInstallInstructions', () => { @@ -222,16 +210,6 @@ describePosix('digest validation', () => { }) describePosix('cache containment', () => { - it('rejects a path that traverses out of the cache', async () => { - const filePath = path.join(outsideDir, 'orca.deb') - await fsp.writeFile(filePath, PAYLOAD) - capture({ downloadedFile: path.join(downloadDir, '..', '..', '..', 'outside', 'orca.deb') }) - await expect(recovery.resolveLinuxPackageInstallInstructions(recoveryFor())).resolves.toEqual({ - ok: false, - reason: 'not-regular' - }) - }) - it('rejects a directory in place of the package', async () => { await fsp.mkdir(path.join(downloadDir, 'orca.deb')) capture() @@ -251,60 +229,10 @@ describePosix('cache containment', () => { reason: 'not-regular' }) }) - - it('rejects a package whose parent symlink escapes the cache', async () => { - await fsp.writeFile(path.join(outsideDir, 'orca.deb'), PAYLOAD) - await fsp.symlink(outsideDir, path.join(cacheRoot, 'escape')) - capture({ downloadedFile: path.join(cacheRoot, 'escape', 'orca.deb') }) - await expect(recovery.resolveLinuxPackageInstallInstructions(recoveryFor())).resolves.toEqual({ - ok: false, - reason: 'not-regular' - }) - }) - - it('rejects a subdirectory of the pending directory', async () => { - const nested = path.join(downloadDir, 'nested') - await fsp.mkdir(nested) - const filePath = path.join(nested, 'orca.deb') - await fsp.writeFile(filePath, PAYLOAD) - capture({ downloadedFile: filePath }) - await expect(recovery.resolveLinuxPackageInstallInstructions(recoveryFor())).resolves.toEqual({ - ok: false, - reason: 'not-regular' - }) - }) - - it('uses the home cache when XDG_CACHE_HOME is unset', async () => { - const homeCache = path.join(tempRoot, 'home') - const homeDownloadDir = path.join(homeCache, '.cache', 'orca-updater', 'pending') - await fsp.mkdir(homeDownloadDir, { recursive: true }) - const filePath = path.join(homeDownloadDir, 'orca.deb') - await fsp.writeFile(filePath, PAYLOAD) - vi.stubEnv('XDG_CACHE_HOME', '') - vi.spyOn(os, 'homedir').mockReturnValue(homeCache) - capture({ downloadedFile: filePath }) - await expect(recovery.resolveLinuxPackageInstallInstructions(recoveryFor())).resolves.toEqual({ - ok: true, - command: 'installed command', - packageFileName: 'orca.deb' - }) - }) }) // Pending-directory shape is defense in depth; cache-root containment excludes other users’ swap locations. describePosix('pending-directory anchoring', () => { - it('rejects a package in an attacker-owned directory under the cache root', async () => { - const evilDir = path.join(cacheRoot, 'evil') - await fsp.mkdir(evilDir) - const filePath = path.join(evilDir, 'orca.deb') - await fsp.writeFile(filePath, PAYLOAD) - capture({ downloadedFile: filePath }) - await expect(recovery.resolveLinuxPackageInstallInstructions(recoveryFor())).resolves.toEqual({ - ok: false, - reason: 'not-regular' - }) - }) - it('rejects a pending directory buried deeper than one level under the cache root', async () => { const deepPending = path.join(cacheRoot, 'evil', 'nested', 'pending') await fsp.mkdir(deepPending, { recursive: true }) @@ -317,19 +245,6 @@ describePosix('pending-directory anchoring', () => { }) }) - it('accepts any updater directory name that holds the pending directory', async () => { - const otherPending = path.join(cacheRoot, 'orca-updater-next', 'pending') - await fsp.mkdir(otherPending, { recursive: true }) - const filePath = path.join(otherPending, 'orca.deb') - await fsp.writeFile(filePath, PAYLOAD) - capture({ downloadedFile: filePath }) - await expect(recovery.resolveLinuxPackageInstallInstructions(recoveryFor())).resolves.toEqual({ - ok: true, - command: 'installed command', - packageFileName: 'orca.deb' - }) - }) - it('accepts a pending directory reached through a symlink that stays in the cache', async () => { await writePackage('orca.deb') await fsp.symlink(updaterDir, path.join(cacheRoot, 'link-to-updater')) @@ -341,15 +256,3 @@ describePosix('pending-directory anchoring', () => { }) }) }) - -describePosix('resolveLinuxPackageRevealTarget', () => { - it('rejects a package that fails validation', async () => { - const filePath = await writePackage('orca.deb') - capture() - await fsp.writeFile(filePath, 'tampered payload') - await expect(recovery.resolveLinuxPackageRevealTarget(recoveryFor())).resolves.toEqual({ - ok: false, - reason: 'hash-mismatch' - }) - }) -}) diff --git a/src/main/window/activation/background-window-open.ts b/src/main/window/activation/background-window-open.ts new file mode 100644 index 00000000000..6de1ae1ffff --- /dev/null +++ b/src/main/window/activation/background-window-open.ts @@ -0,0 +1,15 @@ +// Registered by the desktop main process; orcad has no window to open and leaves it unset. +let openWindowInBackground: (() => void) | null = null + +export function setBackgroundWindowOpener(opener: () => void): void { + openWindowInBackground = opener +} + +/** Shows Orca's window without taking the foreground; false when this host has no desktop. */ +export function requestBackgroundWindowOpen(): boolean { + if (!openWindowInBackground) { + return false + } + openWindowInBackground() + return true +} diff --git a/src/main/window/activation/focus-existing-window.test.ts b/src/main/window/activation/focus-existing-window.test.ts new file mode 100644 index 00000000000..55cd6d46f03 --- /dev/null +++ b/src/main/window/activation/focus-existing-window.test.ts @@ -0,0 +1,75 @@ +import type { BrowserWindow } from 'electron' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +vi.mock('electron', () => ({ app: {}, BaseWindow: { getFocusedWindow: () => null } })) + +const { focusExistingMainWindow } = await import('./focus-existing-window') + +function makeWindow(visible: boolean) { + return { + isDestroyed: () => false, + isMinimized: () => false, + isVisible: () => visible, + restore: vi.fn(), + show: vi.fn(), + showInactive: vi.fn(), + focus: vi.fn(), + moveTop: vi.fn(), + isAlwaysOnTop: () => false, + setAlwaysOnTop: vi.fn() + } +} + +describe('focusExistingMainWindow for a background open', () => { + // The test runner itself launches in the background, which would hide what the request decides. + beforeEach(() => { + vi.stubEnv('ORCA_BACKGROUND_LAUNCH', '') + }) + afterEach(() => { + vi.unstubAllEnvs() + }) + + it('shows a hidden window without taking the foreground', () => { + const window = makeWindow(false) + const app = { focus: vi.fn(), isReady: () => true } + + focusExistingMainWindow({ + app, + background: true, + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the background path reads only these window methods. + getWindow: () => window as unknown as BrowserWindow, + openWindow: vi.fn(), + platform: 'win32', + setTimeout: vi.fn() + }) + + expect(window.showInactive).toHaveBeenCalledOnce() + expect(app.focus).not.toHaveBeenCalled() + expect(window.show).not.toHaveBeenCalled() + expect(window.focus).not.toHaveBeenCalled() + expect(window.moveTop).not.toHaveBeenCalled() + expect(window.setAlwaysOnTop).not.toHaveBeenCalled() + }) + + it('opens a missing window to reveal itself in the background', () => { + const window = makeWindow(false) + const app = { focus: vi.fn(), isReady: () => true } + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the background path reads only these window methods. + const openWindow = vi.fn((_options?: unknown) => window as unknown as BrowserWindow) + + expect( + focusExistingMainWindow({ + app, + background: true, + getWindow: () => null, + openWindow, + platform: 'win32', + setTimeout: vi.fn() + }) + ).toBe('opened') + + expect(openWindow).toHaveBeenCalledWith({ revealInBackground: true }) + expect(app.focus).not.toHaveBeenCalled() + expect(window.showInactive).not.toHaveBeenCalled() + }) +}) diff --git a/src/main/window/activation/focus-existing-window.ts b/src/main/window/activation/focus-existing-window.ts index 8c3e0c9fe1d..678dd60ce9e 100644 --- a/src/main/window/activation/focus-existing-window.ts +++ b/src/main/window/activation/focus-existing-window.ts @@ -12,7 +12,10 @@ export type FocusExistingMainWindowResult = 'focused' | 'opened' | 'pending' export type FocusExistingMainWindowOptions = { app: Pick getWindow: () => BrowserWindow | null - openWindow: () => BrowserWindow | Promise + // Structural: the web project typechecks this file and can't reach main-window-contracts. + openWindow: (options?: { revealInBackground?: boolean }) => BrowserWindow | Promise + /** `orca open` asked: show the window if needed, never take the foreground. */ + background?: boolean /** False while some other path must own the first window; the reopen is dropped, not queued. */ canOpenWindow?: () => boolean platform?: NodeJS.Platform @@ -82,8 +85,21 @@ function activateWindow( window: BrowserWindow, app: Pick, platform: NodeJS.Platform, - setTimer: FocusTimer + setTimer: FocusTimer, + background: boolean ): void { + if (background) { + // A minimized window stays minimized: restoring it would pull the user's attention. + if ( + !window.isDestroyed() && + !window.isVisible() && + !window.isMinimized() && + !isWindowlessLaunch() + ) { + window.showInactive() + } + return + } safelyFocusApp(app) safelyRevealWindow(window) // Windows reinforcement raises windows over other work; suppress it during background launches. @@ -109,7 +125,8 @@ function openWindowWithRetry( >, platform: NodeJS.Platform, setTimer: FocusTimer, - attempt: number + attempt: number, + background: boolean ): BrowserWindow | null | Promise { const failed = (error: unknown): null => { opts.warn?.('[window] Failed to reopen main window for second-instance launch', error) @@ -119,27 +136,27 @@ function openWindowWithRetry( setTimer(() => { const existing = opts.getWindow() if (existing && !existing.isDestroyed()) { - activateWindow(existing, opts.app, platform, setTimer) + activateWindow(existing, opts.app, platform, setTimer, background) return } if (opts.canOpenWindow?.() === false) { return } - const window = openWindowWithRetry(opts, platform, setTimer, attempt + 1) + const window = openWindowWithRetry(opts, platform, setTimer, attempt + 1, background) if (window instanceof Promise) { void window.then((ready) => { - if (ready) { - activateWindow(ready, opts.app, platform, setTimer) + if (ready && !background) { + activateWindow(ready, opts.app, platform, setTimer, false) } }) - } else if (window) { - activateWindow(window, opts.app, platform, setTimer) + } else if (window && !background) { + activateWindow(window, opts.app, platform, setTimer, false) } }, REOPEN_RETRY_DELAY_MS) return null } try { - const window = opts.openWindow() + const window = opts.openWindow(background ? { revealInBackground: true } : undefined) return window instanceof Promise ? window.catch(failed) : window } catch (error) { return failed(error) @@ -151,6 +168,7 @@ export function focusExistingMainWindow( ): FocusExistingMainWindowResult { const platform = opts.platform ?? process.platform const setTimer = opts.setTimeout ?? setTimeout + const background = opts.background === true let window = opts.getWindow() let openedWindow = false @@ -158,11 +176,11 @@ export function focusExistingMainWindow( if (!opts.app.isReady() || opts.canOpenWindow?.() === false) { return 'pending' } - const opening = openWindowWithRetry(opts, platform, setTimer, 1) + const opening = openWindowWithRetry(opts, platform, setTimer, 1, background) if (opening instanceof Promise) { void opening.then((ready) => { - if (ready) { - activateWindow(ready, opts.app, platform, setTimer) + if (ready && !background) { + activateWindow(ready, opts.app, platform, setTimer, false) } }) return 'pending' @@ -174,6 +192,9 @@ export function focusExistingMainWindow( openedWindow = true } - activateWindow(window, opts.app, platform, setTimer) + // A window opened in the background reveals itself inactive on first paint. + if (!(openedWindow && background)) { + activateWindow(window, opts.app, platform, setTimer, background) + } return openedWindow ? 'opened' : 'focused' } diff --git a/src/main/window/activation/foreground-activation-policy.ts b/src/main/window/activation/foreground-activation-policy.ts index 5f0926df050..e91c00fd1cd 100644 --- a/src/main/window/activation/foreground-activation-policy.ts +++ b/src/main/window/activation/foreground-activation-policy.ts @@ -68,7 +68,10 @@ export function applyBackgroundActivationPolicy(options: ActivationPolicyOptions /** Gives a serve process its Dock tile and menu bar back while it hosts a desktop window. */ export function applyServeDesktopWindowActivationPolicy( - options: Omit = {} + options: Omit & { + /** The window opens behind the user's app: take the Dock tile without dock.show()'s activation. */ + background?: boolean + } = {} ): boolean { const platform = options.platform ?? process.platform // dock.show() activates the app, so automated launches keep the accessory policy. @@ -78,9 +81,11 @@ export function applyServeDesktopWindowActivationPolicy( try { const app = options.app ?? electronApp app.setActivationPolicy('regular') - void app.dock?.show?.().catch((error: unknown) => { - options.warn?.('[window] Failed to show the Dock icon', error) - }) + if (options.background !== true) { + void app.dock?.show?.().catch((error: unknown) => { + options.warn?.('[window] Failed to show the Dock icon', error) + }) + } return true } catch (error) { options.warn?.('[window] Failed to apply foreground activation policy', error) diff --git a/src/main/window/createMainWindow.ts b/src/main/window/createMainWindow.ts index d7a1ef426ac..4855db93ffa 100644 --- a/src/main/window/createMainWindow.ts +++ b/src/main/window/createMainWindow.ts @@ -209,6 +209,7 @@ export function createMainWindow( const state = installMainWindowStateLifecycle({ mainWindow, revealOnDidFinishLoad: opts?.revealOnDidFinishLoad === true, + revealInBackground: opts?.revealInBackground, savedMaximized, savedFullScreen, store diff --git a/src/main/window/main-window/main-window-contracts.ts b/src/main/window/main-window/main-window-contracts.ts index 0d84dd910c7..4a1654db8fb 100644 --- a/src/main/window/main-window/main-window-contracts.ts +++ b/src/main/window/main-window/main-window-contracts.ts @@ -13,6 +13,11 @@ export type MainWindowLoadObserver = { onError?: (error: Error) => void } +export type MainWindowOpenOptions = Pick< + CreateMainWindowOptions, + 'revealOnDidFinishLoad' | 'revealInBackground' +> + export type CreateMainWindowOptions = { /** Returns true when a manual app.quit() (Cmd+Q) is in progress, so the renderer skips the running-process confirm dialog. */ getIsQuitting?: () => boolean @@ -43,6 +48,8 @@ export type CreateMainWindowOptions = { deferLoad?: boolean /** Reveal after load instead of first paint when startup must show the shell before slower renderer work. */ revealOnDidFinishLoad?: boolean + /** Show without taking the foreground: `orca open` asked, not the user. */ + revealInBackground?: boolean title?: string getKeybindings?: () => KeybindingOverrides | undefined onBeforeReload?: (options: { ignoreCache: boolean; webContentsId: number }) => void diff --git a/src/main/window/main-window/main-window-state-lifecycle.ts b/src/main/window/main-window/main-window-state-lifecycle.ts index 7e6db78021d..bb86be1c48f 100644 --- a/src/main/window/main-window/main-window-state-lifecycle.ts +++ b/src/main/window/main-window/main-window-state-lifecycle.ts @@ -19,11 +19,19 @@ export type MainWindowStateLifecycle = { export function installMainWindowStateLifecycle(args: { mainWindow: BrowserWindow revealOnDidFinishLoad: boolean + revealInBackground?: boolean savedMaximized: boolean savedFullScreen: boolean store: Store | null }): MainWindowStateLifecycle { - const { mainWindow, revealOnDidFinishLoad, savedMaximized, savedFullScreen, store } = args + const { + mainWindow, + revealOnDidFinishLoad, + revealInBackground = false, + savedMaximized, + savedFullScreen, + store + } = args mainWindow.webContents.on('dom-ready', () => { const level = store?.getUI().uiZoomLevel ?? 0 mainWindow.webContents.setZoomLevel(level) @@ -67,12 +75,20 @@ export function installMainWindowStateLifecycle(args: { if (isWindowlessLaunch()) { return } - if (savedMaximized) { - mainWindow.maximize() + if (revealInBackground) { + // Maximizing a hidden window shows it active; Windows activates on any maximize, so it stays restored there. + mainWindow.showInactive() + if (savedMaximized && process.platform !== 'win32') { + mainWindow.maximize() + } + } else { + if (savedMaximized) { + mainWindow.maximize() + } + showWindowWithoutStealingFocus(mainWindow) } - showWindowWithoutStealingFocus(mainWindow) // Fullscreen needs a shown window; after maximize so leaving it returns there. Background launches must not take a Space. - if (savedFullScreen && !isBackgroundLaunch()) { + if (savedFullScreen && !isBackgroundLaunch() && !revealInBackground) { mainWindow.setFullScreen(true) } } diff --git a/src/main/window/runtime-window-lifecycle.ts b/src/main/window/runtime-window-lifecycle.ts index f1bc293bfc6..478145e006f 100644 --- a/src/main/window/runtime-window-lifecycle.ts +++ b/src/main/window/runtime-window-lifecycle.ts @@ -176,7 +176,8 @@ export function registerRuntimeWindowLifecycle( worktreeId: opts.worktreeId, sourceLeafId: opts.sourceLeafId, telemetrySource: opts.telemetrySource, - newLeafId: opts.newLeafId + newLeafId: opts.newLeafId, + ...(opts.activate === false ? { activate: false } : {}) }) }, renameTerminal: (tabId, title, options) => diff --git a/src/main/windows/windows-administrator-restart.ts b/src/main/windows/windows-administrator-restart.ts index 4c1d9d75763..9985d2fc31b 100644 --- a/src/main/windows/windows-administrator-restart.ts +++ b/src/main/windows/windows-administrator-restart.ts @@ -4,6 +4,7 @@ import { app } from 'electron' import { quoteWindowsArgument } from '@orca/process-host/windows-command-line' import { getBundledLauncherPath } from '../cli/packaged-assets/bundled-cli-launcher-path' import { getWindowsAdministratorStatus } from './windows-administrator-status' +import { BACKGROUND_OPEN_ARG } from '../../shared/background-open-argv' const RESTART_LOCAL_TERMINALS_ARG = '--orca-restart-local-terminals-as-administrator' @@ -28,7 +29,10 @@ export function prepareWindowsAdministratorRestart(): Electron.RelaunchOptions { if (!launcher || !existsSync(launcher)) { throw new Error('The Windows administrator launcher is unavailable.') } - const args = process.argv.slice(1).filter((arg) => arg !== RESTART_LOCAL_TERMINALS_ARG) + // The user asked for this restart, so the elevated window comes forward even when `orca open` started this one. + const args = process.argv + .slice(1) + .filter((arg) => arg !== RESTART_LOCAL_TERMINALS_ARG && arg !== BACKGROUND_OPEN_ARG) return { execPath: launcher, args: [ diff --git a/src/main/wsl/discovery/wsl-distro-holder.test.ts b/src/main/wsl/discovery/wsl-distro-holder.test.ts index 16846777da7..ea8147d0967 100644 --- a/src/main/wsl/discovery/wsl-distro-holder.test.ts +++ b/src/main/wsl/discovery/wsl-distro-holder.test.ts @@ -4,7 +4,7 @@ import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import type { WindowsProcessEvidence } from '../../daemon/endpoint/identity/daemon-incarnation-evidence-types' -import { isWslDistroHolderCommandLine, WSL_DISTRO_HOLDER_MARKER } from './wsl-distro-holder-command' +import { WSL_DISTRO_HOLDER_MARKER } from './wsl-distro-holder-command' import { WSL_HOLDER_UNVERIFIABLE_REPLACE_AFTER_MS, createWslDistroHolder @@ -99,23 +99,11 @@ afterEach(async () => { await rm(userDataPath, { recursive: true, force: true }) }) -describe('holder command line', () => { - it('matches only our holder for the same distro', () => { - expect(isWslDistroHolderCommandLine(HOLDER_COMMAND('Alpine'), 'alpine')).toBe(true) - expect(isWslDistroHolderCommandLine(HOLDER_COMMAND('Alpine-2'), 'Alpine')).toBe(false) - expect(isWslDistroHolderCommandLine('wsl.exe -d Alpine --exec sleep 9', 'Alpine')).toBe(false) - expect( - isWslDistroHolderCommandLine(`node x.js -d Alpine ${WSL_DISTRO_HOLDER_MARKER}`, 'Alpine') - ).toBe(false) - }) -}) - describe('createWslDistroHolder', () => { it('a background ensure re-reads the running list right before spawning, and never boots (E9)', async () => { const holder = makeHolder() const requireRunning = vi.fn(async () => false) await expect(holder.ensureHeld('Alpine', { requireRunning })).rejects.toThrow('isn’t running') - expect(requireRunning).toHaveBeenCalledTimes(1) expect(host.spawned).toEqual([]) requireRunning.mockResolvedValue(true) @@ -143,24 +131,6 @@ describe('createWslDistroHolder', () => { expect(host.spawned).toHaveLength(1) }) - it('adopts a valid holder left by a previous run without spawning another', async () => { - host.processes.set(77, { commandLine: HOLDER_COMMAND('Alpine'), startedAtMs: 5_000 }) - await writeRecord(record()) - const holder = makeHolder() - expect(await holder.adoptHolders()).toEqual([{ distro: 'Alpine', state: 'live' }]) - expect((await holder.ensureHeld('Alpine')).outcome).toBe('adopted') - expect(host.spawned).toEqual([]) - }) - - it('rejects a reused pid (creation time differs) and drops the record', async () => { - host.processes.set(77, { commandLine: HOLDER_COMMAND('Alpine'), startedAtMs: 9_999 }) - await writeRecord(record()) - const holder = makeHolder() - expect(await holder.adoptHolders()).toEqual([{ distro: 'Alpine', state: 'exited' }]) - expect(await recordFiles()).toEqual([]) - expect(host.killed).toEqual([]) - }) - it('releases every verified holder it recorded and nothing it cannot verify', async () => { host.processes.set(77, { commandLine: HOLDER_COMMAND('Alpine'), startedAtMs: 5_000 }) await writeRecord(record()) @@ -253,7 +223,6 @@ describe('createWslDistroHolder', () => { sleep: async () => {} }) expect(await released.release('Alpine')).toBe('exited') - expect(midExitReads).toBeLessThan(0) expect(await recordFiles()).toEqual([]) expect(await holder.holderState('Alpine')).toBe('exited') }) @@ -267,14 +236,7 @@ describe('createWslDistroHolder', () => { const firstSleep = new Promise((resolve) => { notifySleep = resolve }) - const queryTimes: number[] = [] const released = makeHolder({ - queryProcess: async (pid) => { - if (host.killed.includes(pid)) { - queryTimes.push(clock) - } - return host.query(pid) - }, killProcess: (pid) => { host.killed.push(pid) host.processes.set(pid, { commandLine: null, startedAtMs: 5_000 }) @@ -296,20 +258,16 @@ describe('createWslDistroHolder', () => { return state }) await firstSleep - expect(queryTimes).toEqual([0]) expect(settled).toBe(false) const finalSleep = new Promise((resolve) => { notifySleep = resolve }) resumeSleep(4_999) await finalSleep - expect(queryTimes).toEqual([0, 4_999]) expect(settled).toBe(false) expect(await recordFiles()).toEqual(['alpine.json']) resumeSleep(5_000) expect(await release).toBe('unverifiable') - expect(clock).toBe(5_000) - expect(queryTimes).toEqual([0, 4_999, 5_000]) expect(host.killed).toEqual([4000]) expect(await recordFiles()).toEqual(['alpine.json']) }) @@ -380,9 +338,6 @@ describe('releaseAll for uninstall', () => { ]) expect(host.killed.sort()).toEqual([77, 88]) expect(await recordFiles()).toEqual([]) - expect(console.warn).toHaveBeenCalledWith( - '[wsl] uninstall: Alpine holder pid 77 was live, now exited' - ) }) it('never kills a pid whose command line is not the holder for the recorded distro', async () => { @@ -427,8 +382,5 @@ describe('releaseAll for uninstall', () => { ]) expect(host.killed).toEqual([77]) expect(await recordFiles()).toEqual(['alpine.json']) - expect(console.warn).toHaveBeenCalledWith( - '[wsl] uninstall: Alpine holder pid 78 was unverifiable, now unverifiable' - ) }) }) diff --git a/src/preload/api/github/github-pull-request-api.ts b/src/preload/api/github/github-pull-request-api.ts index c763ebb7798..0a56df1cf50 100644 --- a/src/preload/api/github/github-pull-request-api.ts +++ b/src/preload/api/github/github-pull-request-api.ts @@ -14,7 +14,7 @@ import type { GitHubPRRefreshEnqueueResult, GitHubPRRefreshEvent, GitHubPRRefreshReason, - PRRefreshOutcome + PRSnapshotReply } from '../../../shared/github/pull-requests/refresh/pull-request-refresh-types' import type { GitHubRepositoryIdentity as GitHubOwnerRepo } from '../../../shared/github/repositories/repository-identity' import type { @@ -52,7 +52,7 @@ export type GithubPullRequestApi = { candidate: GitHubPRRefreshCandidate reason?: GitHubPRRefreshReason userRefresh?: boolean - }) => Promise + }) => Promise enqueuePRRefresh: (args: { candidate: GitHubPRRefreshCandidate reason: GitHubPRRefreshReason diff --git a/src/preload/api/preflight/preflight-api.ts b/src/preload/api/preflight/preflight-api.ts index 1a302473560..8051b767317 100644 --- a/src/preload/api/preflight/preflight-api.ts +++ b/src/preload/api/preflight/preflight-api.ts @@ -48,7 +48,11 @@ export type PreflightApi = { ) => Promise /** Complete by default; background callers opt into host-owned enabled preferences. */ detectAgents: ( - args?: PreflightRuntimeContext & { enabledOnly?: boolean; wakeWslDistro?: boolean } + args?: PreflightRuntimeContext & { + enabledOnly?: boolean + wakeWslDistro?: boolean + userInitiated?: boolean + } ) => Promise /** A WSL distro a background check was refused for is running now; re-ask. Desktop only. */ onWslDistroRunning?: (callback: (distro: string) => void) => () => void @@ -56,7 +60,11 @@ export type PreflightApi = { zcodeInteractiveCapability: () => Promise /** `wakeWslDistro`: a user act that may start a stopped WSL distro; background refreshes omit it. */ refreshAgents: ( - args?: PreflightRuntimeContext & { enabledOnly?: boolean; wakeWslDistro?: boolean } + args?: PreflightRuntimeContext & { + enabledOnly?: boolean + wakeWslDistro?: boolean + userInitiated?: boolean + } ) => Promise detectRemoteAgents: (args: { connectionId: string }) => Promise detectRemoteWindowsTerminalCapabilities: (args: { connectionId: string }) => Promise<{ diff --git a/src/preload/api/preflight/preflight-bridge.ts b/src/preload/api/preflight/preflight-bridge.ts index da0c45ad7a6..e337af98440 100644 --- a/src/preload/api/preflight/preflight-bridge.ts +++ b/src/preload/api/preflight/preflight-bridge.ts @@ -29,7 +29,11 @@ export const preflightApi = { linear: { connected: boolean } }> => ipcRenderer.invoke('preflight:check', args), detectAgents: ( - args?: PreflightRuntimeContext & { enabledOnly?: boolean; wakeWslDistro?: boolean } + args?: PreflightRuntimeContext & { + enabledOnly?: boolean + wakeWslDistro?: boolean + userInitiated?: boolean + } ): Promise => ipcRenderer.invoke('preflight:detectAgents', args), onWslDistroRunning: (callback: (distro: string) => void): (() => void) => { const listener = (_event: Electron.IpcRendererEvent, distro: string): void => callback(distro) @@ -39,7 +43,11 @@ export const preflightApi = { zcodeInteractiveCapability: (): Promise => ipcRenderer.invoke('preflight:zcodeInteractiveCapability'), refreshAgents: ( - args?: PreflightRuntimeContext & { enabledOnly?: boolean; wakeWslDistro?: boolean } + args?: PreflightRuntimeContext & { + enabledOnly?: boolean + wakeWslDistro?: boolean + userInitiated?: boolean + } ): Promise => ipcRenderer.invoke('preflight:refreshAgents', args), detectRemoteAgents: (args: { connectionId: string }): Promise => ipcRenderer.invoke('preflight:detectRemoteAgents', args), diff --git a/src/preload/api/window-ui/ui-bridge-terminal-and-session-tabs.ts b/src/preload/api/window-ui/ui-bridge-terminal-and-session-tabs.ts index 11f75d66557..fa57dcf5c79 100644 --- a/src/preload/api/window-ui/ui-bridge-terminal-and-session-tabs.ts +++ b/src/preload/api/window-ui/ui-bridge-terminal-and-session-tabs.ts @@ -158,6 +158,7 @@ export const uiTerminalAndSessionTabsApi = { sourceLeafId?: string telemetrySource?: TerminalPaneSplitSource newLeafId?: string + activate?: false }) => void ): (() => void) => { const listener = ( @@ -171,6 +172,7 @@ export const uiTerminalAndSessionTabsApi = { sourceLeafId?: string telemetrySource?: TerminalPaneSplitSource newLeafId?: string + activate?: false } ) => callback(data) ipcRenderer.on('ui:splitTerminal', listener) diff --git a/src/preload/api/window-ui/ui-command-event-api.ts b/src/preload/api/window-ui/ui-command-event-api.ts index a39ca774733..d509efd2ef1 100644 --- a/src/preload/api/window-ui/ui-command-event-api.ts +++ b/src/preload/api/window-ui/ui-command-event-api.ts @@ -240,6 +240,7 @@ export type UiCommandEventApi = { sourceLeafId?: string telemetrySource?: TerminalPaneSplitSource newLeafId?: string + activate?: false }) => void ) => () => void onRenameTerminal: ( diff --git a/src/preload/browser-passkey-request.ts b/src/preload/browser-passkey-request.ts deleted file mode 100644 index 4066364b673..00000000000 --- a/src/preload/browser-passkey-request.ts +++ /dev/null @@ -1,23 +0,0 @@ -import type { ContextBridge, IpcRenderer } from 'electron' -import { - BROWSER_PASSKEY_REQUEST_CANCEL_CHANNEL, - BROWSER_PASSKEY_REQUEST_PENDING_CHANNEL -} from '../shared/browser-passkey-request' -import { installBrowserPasskeyRequestWatch } from './browser/passkeys/browser-passkey-request-watch' - -// A session preload: it runs in every frame of every browser page, popup and serve window. -const electron: { - contextBridge: ContextBridge - ipcRenderer: Pick -} = require('electron') -const { contextBridge, ipcRenderer } = electron - -const cancel: unknown = contextBridge.executeInMainWorld({ - func: installBrowserPasskeyRequestWatch, - args: [(pending: boolean) => ipcRenderer.send(BROWSER_PASSKEY_REQUEST_PENDING_CHANNEL, pending)] -}) -ipcRenderer.on(BROWSER_PASSKEY_REQUEST_CANCEL_CHANNEL, () => { - if (typeof cancel === 'function') { - cancel() - } -}) diff --git a/src/preload/browser-system-dialogs.ts b/src/preload/browser-system-dialogs.ts new file mode 100644 index 00000000000..dec1686c676 --- /dev/null +++ b/src/preload/browser-system-dialogs.ts @@ -0,0 +1,54 @@ +import type { ContextBridge, IpcRenderer } from 'electron' +import { + BROWSER_PASSKEY_REQUEST_CANCEL_CHANNEL, + BROWSER_PASSKEY_REQUEST_PENDING_CHANNEL +} from '../shared/browser-passkey-request' +import { + BROWSER_AGENT_LEAD_CHANNEL, + BROWSER_AGENT_LEAD_QUERY_CHANNEL, + BROWSER_PRINT_REFUSED_CHANNEL +} from '../shared/browser-system-dialogs' +import { installBrowserPasskeyRequestWatch } from './browser/passkeys/browser-passkey-request-watch' +import { installBrowserPrintGuard } from './browser/printing/browser-print-guard' + +// A session preload: it runs in every frame of every browser page, popup and serve window. +const electron: { + contextBridge: ContextBridge + ipcRenderer: Pick +} = require('electron') +const { contextBridge, ipcRenderer } = electron + +// Pushed by main, since print() must decide synchronously. Unknown until main answers, which +// refuses: an agent-led page's script can print before the answer lands. +let agentLed: boolean | null = null +ipcRenderer.on(BROWSER_AGENT_LEAD_CHANNEL, (_event, led: unknown) => { + agentLed = led === true +}) +ipcRenderer.send(BROWSER_AGENT_LEAD_QUERY_CHANNEL) +// Only Windows hands a passkey request to a system dialog (Windows Hello). +const passkeysOpenSystemDialog = process.platform === 'win32' + +const cancel: unknown = contextBridge.executeInMainWorld({ + func: installBrowserPasskeyRequestWatch, + args: [ + (pending: boolean) => ipcRenderer.send(BROWSER_PASSKEY_REQUEST_PENDING_CHANNEL, pending), + () => !(passkeysOpenSystemDialog && agentLed !== false) + ] +}) +ipcRenderer.on(BROWSER_PASSKEY_REQUEST_CANCEL_CHANNEL, () => { + if (typeof cancel === 'function') { + cancel() + } +}) +contextBridge.executeInMainWorld({ + func: installBrowserPrintGuard, + args: [ + () => { + if (agentLed === false) { + return true + } + ipcRenderer.send(BROWSER_PRINT_REFUSED_CHANNEL) + return false + } + ] +}) diff --git a/src/preload/browser/passkeys/browser-passkey-request-watch.test.ts b/src/preload/browser/passkeys/browser-passkey-request-watch.test.ts index 67e75358272..ff1de32c283 100644 --- a/src/preload/browser/passkeys/browser-passkey-request-watch.test.ts +++ b/src/preload/browser/passkeys/browser-passkey-request-watch.test.ts @@ -9,7 +9,9 @@ function waitForAbort(signal?: AbortSignal | null): Promise { } class PendingCredentialsContainer { + static asked = 0 get(options?: CredentialRequestOptions): Promise { + PendingCredentialsContainer.asked += 1 return waitForAbort(options?.signal) } create(options?: CredentialCreationOptions): Promise { @@ -19,7 +21,7 @@ class PendingCredentialsContainer { const original = Object.getOwnPropertyDescriptor(globalThis, 'CredentialsContainer') -function install(): { +function install(mayAskSystem: () => boolean = () => true): { credentials: PendingCredentialsContainer reports: boolean[] cancel: () => void @@ -29,7 +31,8 @@ function install(): { value: PendingCredentialsContainer }) const reports: boolean[] = [] - const cancel = installBrowserPasskeyRequestWatch((pending) => reports.push(pending)) + PendingCredentialsContainer.asked = 0 + const cancel = installBrowserPasskeyRequestWatch((pending) => reports.push(pending), mayAskSystem) return { credentials: new PendingCredentialsContainer(), reports, cancel } } @@ -62,4 +65,21 @@ describe('installBrowserPasskeyRequestWatch', () => { await expect(request).rejects.toMatchObject({ name: 'AbortError' }) expect(reports).toEqual([true, false]) }) + + it("holds a request main keeps from the system dialog until it is cancelled or the user's own request replaces it", async () => { + let mayAskSystem = false + const { credentials, reports, cancel } = install(() => mayAskSystem) + const cancelled = credentials.get({ publicKey }) + expect(PendingCredentialsContainer.asked).toBe(0) + expect(reports).toEqual([true]) + cancel() + await expect(cancelled).rejects.toMatchObject({ name: 'NotAllowedError' }) + + const replaced = credentials.get({ publicKey }) + mayAskSystem = true + void credentials.get({ publicKey }).catch(() => {}) + await expect(replaced).rejects.toMatchObject({ name: 'NotAllowedError' }) + expect(PendingCredentialsContainer.asked).toBe(1) + cancel() + }) }) diff --git a/src/preload/browser/passkeys/browser-passkey-request-watch.ts b/src/preload/browser/passkeys/browser-passkey-request-watch.ts index f4eb0b2b55e..70ff634a167 100644 --- a/src/preload/browser/passkeys/browser-passkey-request-watch.ts +++ b/src/preload/browser/passkeys/browser-passkey-request-watch.ts @@ -1,13 +1,22 @@ /** - * Runs serialized in the page's main world. Electron has no passkey UI and tells main nothing, so - * the page's calls report when the document starts and stops waiting on one. Returns the canceller. + * Runs serialized in the page's main world. Electron tells main nothing about passkey requests, so + * the page's calls report when the document starts and stops waiting on one. A request main won't + * hand to the OS (Windows Hello would take the foreground) waits unsent until cancelled. Returns the canceller. */ -export function installBrowserPasskeyRequestWatch(report: (pending: boolean) => void): () => void { +export function installBrowserPasskeyRequestWatch( + report: (pending: boolean) => void, + mayAskSystem: () => boolean +): () => void { // Insecure documents have no credentials API. if (typeof CredentialsContainer === 'undefined') { return () => {} } const pending = new Set() + // Requests main kept from the OS; the user's own later request replaces them. + const held = new Set() + // The page's own "user cancelled" path, so it offers another way to sign in. + const cancelled = (): DOMException => + new DOMException('The passkey request was cancelled.', 'NotAllowedError') type Options = { publicKey?: unknown; mediation?: string; signal?: AbortSignal } type Request = (options?: O) => Promise const watch = (original: Request): Request => @@ -28,7 +37,21 @@ export function installBrowserPasskeyRequestWatch(report: (pending: boolean) => // oxlint-disable-next-line anti-slop/no-reflect-get -- Proxy `get` trap: raw string|symbol pass-through; the receiver stays the page's object on purpose. key === 'signal' ? signal : Reflect.get(target, key) }) - request = original.call(this, withSignal) + if (mayAskSystem()) { + for (const stale of held) { + stale.abort(cancelled()) + } + request = original.call(this, withSignal) + } else { + held.add(controller) + request = new Promise((_resolve, reject) => { + if (signal.aborted) { + reject(signal.reason) + return + } + signal.addEventListener('abort', () => reject(signal.reason), { once: true }) + }) + } } catch (error) { return Promise.reject(error) } @@ -37,6 +60,7 @@ export function installBrowserPasskeyRequestWatch(report: (pending: boolean) => report(true) } const settle = (): void => { + held.delete(controller) if (pending.delete(controller) && pending.size === 0) { report(false) } @@ -49,8 +73,7 @@ export function installBrowserPasskeyRequestWatch(report: (pending: boolean) => credentials.create = watch(credentials.create) return () => { for (const controller of pending) { - // The page's own "user cancelled" path, so it offers another way to sign in. - controller.abort(new DOMException('The passkey request was cancelled.', 'NotAllowedError')) + controller.abort(cancelled()) } } } diff --git a/src/preload/browser/printing/browser-print-guard.ts b/src/preload/browser/printing/browser-print-guard.ts new file mode 100644 index 00000000000..6005aaad847 --- /dev/null +++ b/src/preload/browser/printing/browser-print-guard.ts @@ -0,0 +1,14 @@ +/** + * Runs serialized in the page's main world. Electron's `print()` opens the system print dialog, + * which takes the foreground, so main may refuse it; a refused call returns like a throttled one. + */ +export function installBrowserPrintGuard(mayOpen: () => boolean): void { + const print = window.print + try { + window.print = () => { + if (mayOpen()) { + print.call(window) + } + } + } catch {} +} diff --git a/src/renderer/src/app-shell/appearance/notice-surface-tokens.test.ts b/src/renderer/src/app-shell/appearance/notice-surface-tokens.test.ts index e9c4e7936e4..4f6fc8267f8 100644 --- a/src/renderer/src/app-shell/appearance/notice-surface-tokens.test.ts +++ b/src/renderer/src/app-shell/appearance/notice-surface-tokens.test.ts @@ -7,7 +7,9 @@ const DARK_BUILT_INS = { '--popover': '#171717', '--popover-foreground': '#f0f0f0', '--muted': '#262626', - '--muted-foreground': '#acacac' + '--muted-foreground': '#acacac', + '--accent': '#404040', + '--accent-foreground': '#f0f0f0' } const probe = (value: string): Rgba | null => (value.startsWith('#') ? hexToRgb(value) : null) @@ -66,18 +68,37 @@ function expectReadable(themeTokens: Record): void { expect(wcagContrast(panel, muted)).toBeGreaterThanOrEqual(4.5) expect(wcagContrast(panel, text)).toBeGreaterThanOrEqual(4.5) } + // Hover fills (bg-accent, ghost dark:hover:bg-accent/50) paint accent-foreground or, for ghost + // buttons with hover:text-foreground, popover text. Without notice tokens, main.css paints the + // palette's --accent and --accent-foreground. + const accent = notice['--notice-accent'] ?? palette['--accent'] + const accentText = notice['--notice-accent-foreground'] ?? palette['--accent-foreground'] + for (const alpha of [0.5, 1]) { + const hover = composite(accent, surface, alpha) + expect(wcagContrast(hover, accentText)).toBeGreaterThanOrEqual(4.5) + expect(wcagContrast(hover, text)).toBeGreaterThanOrEqual(4.5) + } + // Dark outline buttons fill with dark:bg-input/30, /50 on hover, under popover text. + const input = notice['--notice-input'] ?? palette['--input'] + if (input?.startsWith('#')) { + for (const alpha of [0.3, 0.5]) { + expect(wcagContrast(composite(input, surface, alpha), text)).toBeGreaterThanOrEqual(4.5) + } + } } describe('noticeSurfaceTokens', () => { // Popovers the theme validator accepts that once painted notice text under 4.5:1: #5a5a5a // through Chromium's color-mix rounding and an over-lifted 80% description, #383838 through hex - // rounding, white-on-dark-base through dark muted panels, the rest unlifted. + // rounding, white-on-dark-base through dark muted panels, hover and outline fills, the rest + // unlifted. it.each>([ { '--popover': '#5a5a5a' }, { '--popover': '#383838' }, { '--popover': '#444444' }, { '--popover': '#303030', '--popover-foreground': '#a0a0a0' }, - { '--popover': '#ffffff', '--popover-foreground': '#111111' } + { '--popover': '#ffffff', '--popover-foreground': '#111111' }, + { '--popover': '#ffffff', '--popover-foreground': '#747474', '--input': '#262626' } ])('keeps notice text 4.5:1 for an accepted %o', (themeTokens) => { expectReadable(themeTokens) }) diff --git a/src/renderer/src/app-shell/appearance/notice-surface-tokens.ts b/src/renderer/src/app-shell/appearance/notice-surface-tokens.ts index 7afca6bb419..5b87a6966fe 100644 --- a/src/renderer/src/app-shell/appearance/notice-surface-tokens.ts +++ b/src/renderer/src/app-shell/appearance/notice-surface-tokens.ts @@ -13,6 +13,11 @@ export const NOTICE_SECONDARY_TOKEN = '--notice-secondary' export const NOTICE_MUTED_FOREGROUND_TOKEN = '--notice-muted-foreground' export const NOTICE_SECONDARY_EDGE_TOKEN = '--notice-secondary-edge' export const NOTICE_MUTED_TOKEN = '--notice-muted' +export const NOTICE_ACCENT_TOKEN = '--notice-accent' +export const NOTICE_ACCENT_FOREGROUND_TOKEN = '--notice-accent-foreground' +export const NOTICE_INPUT_TOKEN = '--notice-input' +export const NOTICE_EDGE_TOKEN = '--notice-edge' +export const NOTICE_OUTLINE_EDGE_TOKEN = '--notice-outline-edge' // Keep in sync with the built-in notice mixes in main.css. const NOTICE_LIFT = { light: 0, dark: 7 } as const const NOTICE_SECONDARY_MIX = { light: 6, dark: 10 } as const @@ -102,7 +107,17 @@ export function noticeSurfaceTokensFor( // Why: built-in palettes set --muted equal to --secondary; keeping that here keeps muted panels // on the notice surface's polarity when a theme's popover and app polarity differ. [NOTICE_MUTED_TOKEN]: secondaryHex, - [NOTICE_MUTED_FOREGROUND_TOKEN]: rgbaToHex(muted) + [NOTICE_MUTED_FOREGROUND_TOKEN]: rgbaToHex(muted), + // Why: hover (bg-accent, bg-accent/50) and outline fills (dark:bg-input/30, /50) paint under + // popover text in the notice scope; the secondary fill is the deepest one it stays readable on. + [NOTICE_ACCENT_TOKEN]: secondaryHex, + [NOTICE_ACCENT_FOREGROUND_TOKEN]: rgbaToHex(text), + [NOTICE_INPUT_TOKEN]: secondaryHex, + // Why: edges never sit under text, so they follow the popover text like the card border does; + // the theme's own --border/--input can be on the app's polarity, not the popover's. + [NOTICE_EDGE_TOKEN]: 'var(--notice-border)', + // Why: --input now only fills, so dark outline buttons (border-input) need the edge drawn. + [NOTICE_OUTLINE_EDGE_TOKEN]: 'var(--notice-border)' } } diff --git a/src/renderer/src/app-shell/use-visible-hosted-review-refresh.ts b/src/renderer/src/app-shell/use-visible-hosted-review-refresh.ts index 01a33014bd2..5605fc1e900 100644 --- a/src/renderer/src/app-shell/use-visible-hosted-review-refresh.ts +++ b/src/renderer/src/app-shell/use-visible-hosted-review-refresh.ts @@ -1,9 +1,10 @@ import { useEffect } from 'react' +import { onRuntimeEnvironmentRevisionsChanged } from '@/runtime/rpc/environment-revisions/runtime-environment-revision' import { useAppStore } from '@/store/index' import { isWindowVisible } from '@/lib/window-activity/window-visibility-interval' import { isWebClientLocation } from '@/app-shell/platform/web-client-location' import { createVisibleHostedReviewRefreshScheduler } from '@/store/slices/hosted-review/refresh/visible-hosted-review-refresh-scheduler' -import { getWorkspaceReferenceRefreshTargets } from '@/components/sidebar/workspace-card/review/workspace-reference-refresh-targets' +import { createWorkspaceReferenceRefreshTargetsSelector } from '@/components/sidebar/workspace-card/review/workspace-reference-refresh-targets' import { getVisibleHostedReviewRefreshTargets, visibleHostedReviewRefreshInputsChanged @@ -15,29 +16,45 @@ export function useVisibleHostedReviewRefresh({ enabled }: { enabled: boolean }) return } const scheduler = createVisibleHostedReviewRefreshScheduler() - const update = (): void => { + const selectedOnly = isWebClientLocation() + const selectReferences = createWorkspaceReferenceRefreshTargetsSelector({ selectedOnly }) + let hostedTargets: ReturnType = [] + let referenceTargets: ReturnType = [] + const update = (reviewInputsChanged: boolean): void => { + if (!isWindowVisible()) { + return + } const state = useAppStore.getState() - const selectedOnly = isWebClientLocation() + const references = selectReferences(state, reviewInputsChanged) + if (!reviewInputsChanged && references === referenceTargets) { + return + } + referenceTargets = references + if (reviewInputsChanged) { + hostedTargets = getVisibleHostedReviewRefreshTargets(state, useAppStore.getState, { + selectedOnly + }) + } scheduler.update([ - ...getVisibleHostedReviewRefreshTargets(state, useAppStore.getState, { selectedOnly }), - ...getWorkspaceReferenceRefreshTargets(state, { selectedOnly }) + // Collapsed review rollups also need background reads on paired clients; this shared queue bounds them. + ...hostedTargets, + ...referenceTargets ]) } const visibilityChanged = (): void => { scheduler.setVisible(false) - update() + update(true) scheduler.setVisible(isWindowVisible()) } const unsubscribe = useAppStore.subscribe((state, previous) => { - if (visibleHostedReviewRefreshInputsChanged(state, previous)) { - update() - } + update(visibleHostedReviewRefreshInputsChanged(state, previous)) }) - update() + const stopRevisionChanges = onRuntimeEnvironmentRevisionsChanged(() => update(true)) visibilityChanged() document.addEventListener('visibilitychange', visibilityChanged) return () => { unsubscribe() + stopRevisionChanges() document.removeEventListener('visibilitychange', visibilityChanged) scheduler.dispose() } diff --git a/src/renderer/src/assets/main.css b/src/renderer/src/assets/main.css index 88a4063759d..360fae812f2 100644 --- a/src/renderer/src/assets/main.css +++ b/src/renderer/src/assets/main.css @@ -104,6 +104,7 @@ --color-question-card-border: var(--question-card-border); --color-question-option-border: var(--question-option-border); --color-chat-focus-ring: var(--chat-focus-ring); + --color-unread: var(--unread); --color-chart-1: var(--chart-1); --color-chart-2: var(--chart-2); --color-chart-3: var(--chart-3); @@ -269,6 +270,8 @@ /* Answer card edge and the outline shared by its option tiles, badges, answer field and Skip. */ --question-card-border-mix: 14%; --question-option-border-mix: 16%; + /* A finished agent result the user hasn't seen; blue so it never reads as a state color. */ + --unread: var(--color-blue-500); /* Keep PR-state hues stable across theme changes. */ --workspace-status-done: #c7a594; --workspace-status-review: #16a34a; @@ -388,6 +391,7 @@ --agent-question-text: var(--color-orange-300); --question-card-border-mix: 16%; --question-option-border-mix: 12%; + --unread: var(--color-blue-400); --workspace-status-done: #c7a594; --workspace-status-review: #16a34a; --workspace-status-progress: #d4a300; @@ -552,20 +556,25 @@ } /* One lifted surface for toasts and corner notification cards; the default card/popover blends into the app. - App themes replace the surface, secondary and muted colors with readable hex (notice-surface-tokens.ts). */ + App themes replace the surface, fill and edge colors with readable ones (notice-surface-tokens.ts). */ :root { + --notice-accent: var(--accent); + --notice-accent-foreground: var(--accent-foreground); + --notice-edge: var(--border); + --notice-input: var(--input); --notice-muted: var(--muted); --notice-muted-foreground: var(--muted-foreground); + --notice-outline-edge: transparent; --notice-secondary-edge: transparent; --notice-surface: var(--popover); - --notice-border: color-mix(in srgb, var(--foreground) 14%, transparent); + --notice-border: color-mix(in srgb, var(--popover-foreground) 14%, transparent); --notice-secondary: color-mix(in srgb, var(--foreground) 6%, var(--notice-surface)); --notice-shadow: 0 11px 32px -6px rgb(0 0 0 / 0.2), 0 2px 6px rgb(0 0 0 / 0.08); } .dark { --notice-surface: color-mix(in srgb, var(--foreground) 7%, var(--popover)); - --notice-border: color-mix(in srgb, var(--foreground) 15%, transparent); + --notice-border: color-mix(in srgb, var(--popover-foreground) 15%, transparent); --notice-secondary: color-mix(in srgb, var(--foreground) 10%, var(--notice-surface)); --notice-shadow: 0 11px 32px -6px rgb(0 0 0 / 0.55), 0 2px 6px rgb(0 0 0 / 0.22), @@ -585,6 +594,10 @@ --muted-foreground: var(--notice-muted-foreground); --secondary: var(--notice-secondary); --secondary-foreground: var(--popover-foreground); + --accent: var(--notice-accent); + --accent-foreground: var(--notice-accent-foreground); + --input: var(--notice-input); + --border: var(--notice-edge); } /* Why inset outline: an edge for themes that leave secondary buttons unfilled, without moving layout. */ @@ -597,6 +610,13 @@ outline-offset: -1px; } +.dark + :is([data-sonner-toaster], .notification-card-stack) + [data-slot='button'][data-variant='outline'] { + outline: 1px solid var(--notice-outline-edge); + outline-offset: -1px; +} + [data-sonner-toaster] { font-family: var(--font-sans); /* Why: above the floating workspace (z-45 panel, z-46 launcher), which parks in the toasts' corner. */ @@ -1764,7 +1784,7 @@ html.native-shell .app-layout { } .worktree-agent-row-hover:hover { - background: color-mix(in srgb, var(--sidebar-foreground) 1.25%, transparent); + background: var(--worktree-sidebar-accent); } .worktree-agent-row-hover[data-focused-agent-pane='true'] { @@ -1772,10 +1792,6 @@ html.native-shell .app-layout { background: color-mix(in srgb, var(--sidebar-foreground) 12%, var(--sidebar-accent)); } -.dark .worktree-agent-row-hover:hover { - background: color-mix(in srgb, var(--accent) 18%, transparent); -} - .dark .worktree-agent-row-hover[data-focused-agent-pane='true'] { background: color-mix(in srgb, var(--accent) 70%, transparent); } @@ -2129,16 +2145,7 @@ html.native-shell .app-layout { .compact-agent-row.worktree-agent-lineage-parent-row.worktree-agent-row-hover:hover, [data-compact-agent-list='true'] .compact-agent-row.worktree-agent-lineage-child-row.worktree-agent-row-hover:hover { - background: color-mix(in srgb, var(--sidebar-foreground) 1.25%, transparent); -} - -.dark - [data-compact-agent-list='true'] - .compact-agent-row.worktree-agent-lineage-parent-row.worktree-agent-row-hover:hover, -.dark - [data-compact-agent-list='true'] - .compact-agent-row.worktree-agent-lineage-child-row.worktree-agent-row-hover:hover { - background: color-mix(in srgb, var(--accent) 18%, transparent); + background: var(--worktree-sidebar-accent); } [data-compact-agent-list='true'] diff --git a/src/renderer/src/components/AgentStateDot.fixture.tsx b/src/renderer/src/components/AgentStateDot.fixture.tsx index 36180c57d56..2064f0e5eba 100644 --- a/src/renderer/src/components/AgentStateDot.fixture.tsx +++ b/src/renderer/src/components/AgentStateDot.fixture.tsx @@ -42,3 +42,18 @@ export const withProgress = defineFixture({ width: 420, render: () => }) + +// Unread swaps only the done check for the unread dot; live and needs-you glyphs keep precedence. +export const unread = defineFixture({ + width: 420, + render: () => ( +
    + {(['done', 'working', 'waiting', 'failed'] as const).map((state) => ( +
  • + + {agentStateLabel(state)} · unread +
  • + ))} +
+ ) +}) diff --git a/src/renderer/src/components/AgentStateDot.tsx b/src/renderer/src/components/AgentStateDot.tsx index 3772ef44f3a..187b7492b03 100644 --- a/src/renderer/src/components/AgentStateDot.tsx +++ b/src/renderer/src/components/AgentStateDot.tsx @@ -2,7 +2,7 @@ import React from 'react' import { Activity, CircleCheck, CircleDashed } from 'lucide-react' import { cn } from '@/lib/class-names' import { translate } from '@/i18n/i18n' -import { useUiLanguageRevision } from '@/i18n/use-ui-language-revision' +import { useTranslate } from '@/i18n/use-translate' import { AgentQuestionIcon } from '@/components/AgentQuestionIcon' import { AgentWorkingSpinner } from '@/components/AgentWorkingSpinner' import { @@ -57,6 +57,8 @@ type Props = { tooltipSide?: StateIndicatorTooltipSide /** Program-reported 0–100; live states then draw a filling ring instead of the spinner/glyph. */ progress?: number + /** Unread completion swaps the check for the workspace unread dot; live and needs-you glyphs keep precedence. */ + unread?: boolean } type ProgressRing = { tone: AgentProgressRingTone; percent: number } @@ -94,14 +96,20 @@ export const AgentStateDot = React.memo(function AgentStateDot({ className, title, tooltipSide, - progress + progress, + unread = false }: Props): React.JSX.Element { - useUiLanguageRevision() + const t = useTranslate() const box = size === 'md' ? 'h-3 w-3' : 'h-2.5 w-2.5' const inner = size === 'md' ? 'size-2' : 'size-1.5' const icon = size === 'md' ? 'size-3' : 'size-2.5' const ring = progressRing(state, progress) - const label = withRingPercent(agentStateLabel(state), ring) + const showUnreadDot = unread && state === 'done' + const label = showUnreadDot + ? t('workspace.activity.unreadLabel', '{{status}} · Unread', { + status: agentStateLabel(state) + }) + : withRingPercent(agentStateLabel(state), ring) const tooltipLabel = title === null ? null : (title ?? label) let indicator: React.JSX.Element @@ -131,6 +139,16 @@ export const AgentStateDot = React.memo(function AgentStateDot({