Files
orca/src/main/browser/agent-browser-bridge-queue.ts
T
Neil db22421ae7 fix(ssh): agents on a managed SSH host can drive the desktop Orca browser again, behind the per-host switch (#29384)
* fix(ssh): an opted-in desktop serves its browser to a managed SSH host's orca CLI again

* fix(ssh): scope bridged pages through the renderer, render them as desktop pages, and route the e2e

* fix(ssh): bridge refuses file URLs and clipboard reads, revokes on disconnect, closes desktop tabs

* fix(ssh): judge bridged paste chords as the page sees them; accept long workspace ids

* fix(ssh): bridged clipboard denial follows popups; revoked commands never leave the browser queue

* fix(ssh): refuse option-like host values bound for agent-browser argv; report undeliverable results

* fix(ssh): bound bridged error results and fall back when the host refuses one

* fix(ssh): bridge only routed host tabs, and bound bridged page scripts by a deadline and revocation

* fix(ssh): cap waiting bridged commands below the desktop socket's result capacity

* fix(ssh): bridge revokes on routing changes, checks each page's route at run time, and cancels host-driven downloads

* fix(ssh): name the bridge registry's owner type

* fix(ssh): bridge refuses pages showing a local document, and never starts a command past orcad's deadline

* fix(ssh): bridged commands may leave Chromium error pages, and stop starting before orcad's deadline

* fix(ssh): ask for a fresh opt-in to host CLI control saved before it drove the desktop browser

* test(ssh): type the bridge registry deadline test
2026-10-15 23:40:57 -07:00

137 lines
5.2 KiB
TypeScript

import type { BrowserTabSwitchResult } from '../../shared/runtime-types'
import { BrowserError } from './cdp-bridge'
import { AgentBrowserBridgeShutdown } from './agent-browser-bridge-shutdown'
import { ORCA_TAB_SESSION_PREFIX } from './agent-browser-orphan-sweep'
import type {
EnqueueTargetedCommandOptions,
ResolvedBrowserCommandTarget
} from './agent-browser-bridge-types'
import { captureRemoteCliBrowserRevocation } from '../runtime/desktop-browser-bridge/remote-cli-browser-scope'
export abstract class AgentBrowserBridgeQueue extends AgentBrowserBridgeShutdown {
// Why: route tab switch through the command queue so it can't race in-flight commands targeting the old tab.
async tabSwitch(
index: number | undefined,
worktreeId?: string,
browserPageId?: string
): Promise<BrowserTabSwitchResult> {
return this.enqueueCommand(worktreeId, async () => {
const tabs = this.getRegisteredTabs(worktreeId)
// Why: queue delay can change the tab list before execution — recompute against live webContents so no vanished index is activated.
const liveEntries = [...tabs.entries()].filter(([, wcId]) => this.getWebContents(wcId))
let switchedIndex = index ?? -1
let resolvedPageId = browserPageId
if (resolvedPageId) {
switchedIndex = liveEntries.findIndex(([tabId]) => tabId === resolvedPageId)
}
if (switchedIndex < 0 || switchedIndex >= liveEntries.length) {
const targetLabel =
resolvedPageId != null ? `Browser page ${resolvedPageId}` : `Tab index ${index}`
throw new BrowserError(
'browser_tab_not_found',
`${targetLabel} out of range (0-${liveEntries.length - 1})`
)
}
const [tabId, wcId] = liveEntries[switchedIndex]
this.activeWebContentsId = wcId
// Why: resolveActiveTab prefers the per-worktree map, so update it or later commands keep routing to the old tab.
const owningWorktreeId = worktreeId ?? this.browserManager.getWorktreeIdForTab(tabId)
// Why: `tab switch --page` may omit --worktree, so still update the owning worktree's active slot for later scoped commands.
if (owningWorktreeId) {
this.activeWebContentsPerWorktree.set(owningWorktreeId, wcId)
}
this.options.onTabsChanged?.(owningWorktreeId ?? undefined)
return { switched: switchedIndex, browserPageId: tabId }
})
}
// ── Internal ──
protected async enqueueCommand<T>(
worktreeId: string | undefined,
execute: (sessionName: string) => Promise<T>
): Promise<T> {
return this.enqueueTargetedCommand(worktreeId, undefined, execute)
}
protected async enqueueTargetedCommand<T>(
worktreeId: string | undefined,
browserPageId: string | undefined,
execute: (sessionName: string, target: ResolvedBrowserCommandTarget) => Promise<T>,
options: EnqueueTargetedCommandOptions = {}
): Promise<T> {
this.assertCommandAdmission()
const assertNotRevoked = captureRemoteCliBrowserRevocation()
assertNotRevoked()
const { browserPageId: pageId } = this.resolveCommandTarget(
worktreeId,
browserPageId,
options.requireScopedTarget
)
const sessionName = `${ORCA_TAB_SESSION_PREFIX}${pageId}`
return new Promise<T>((resolve, reject) => {
let queue = this.commandQueues.get(sessionName)
if (!queue) {
queue = []
this.commandQueues.set(sessionName, queue)
}
queue.push({
execute: () => {
assertNotRevoked()
return this.executeQueuedCommand(
worktreeId,
pageId,
(session, target) => {
assertNotRevoked(target.webContentsId)
return execute(session, target)
},
options
)
},
resolve: resolve as (value: unknown) => void,
reject
})
this.processQueue(sessionName)
})
}
protected async executeQueuedCommand<T>(
worktreeId: string | undefined,
browserPageId: string,
execute: (sessionName: string, target: ResolvedBrowserCommandTarget) => Promise<T>,
options: EnqueueTargetedCommandOptions
): Promise<T> {
this.assertCommandAdmission()
const sessionName = `${ORCA_TAB_SESSION_PREFIX}${browserPageId}`
// Why: the page's guest can change while queued; bind to the one current at execution.
const target = this.resolveCommandTarget(worktreeId, browserPageId)
if (options.ensureSession !== false) {
await this.ensureSession(sessionName, browserPageId, target.webContentsId)
}
return execute(sessionName, target)
}
protected async processQueue(sessionName: string): Promise<void> {
if (this.processingQueues.has(sessionName)) {
return
}
this.processingQueues.add(sessionName)
const queue = this.commandQueues.get(sessionName)
while (queue && queue.length > 0) {
const cmd = queue.shift()!
try {
const result = await cmd.execute()
cmd.resolve(result)
} catch (error) {
cmd.reject(error)
}
}
if (queue && queue.length === 0 && this.commandQueues.get(sessionName) === queue) {
this.commandQueues.delete(sessionName)
}
this.processingQueues.delete(sessionName)
}
}