Files
archived-orca/src/cli/selectors.ts
T
Kelvin Amoaba eb55a5a6d3 feat(browser): --expect-url refuses a browser command when the page is at another URL (#37575)
* feat(browser): guard page commands with an expected URL

* fix(browser): refuse an empty --expect-url and check it on dialogs and the forwarding desktop

* fix(browser): recheck the expected URL right before keys go out

* refactor(browser): drop duplicate session type, dialog alias and per-capability sidecar fields

* chore(browser): satisfy the changed-code gates for the expected URL guard
2026-11-03 02:11:23 +00:00

299 lines
9.7 KiB
TypeScript

import { resolve as resolvePath } from 'node:path'
import type { ComputerAppQuery, RuntimeWorktreeListResult } from '../shared/runtime-types'
import {
isWslUncPathForCallerLinuxPath,
isWslUncPathForLinuxMountedPath
} from '../shared/cross-platform-path'
import { parseWslUncPath } from '../shared/wsl-paths'
import type { RuntimeClient } from './runtime-client'
import { RuntimeClientError } from './runtime/types'
import { getOptionalStringFlag, getPresentStringFlag, getRequiredStringFlag } from './flags'
import {
getCallerExecutionHostId,
resolveCallerPaneWorktreeSelector,
selectWorktreeCreateCwdContext,
selectEnclosingWorktreeSelectionOnHost,
selectorPurpose,
type WorkspaceSelectionPurpose,
type HostScopedWorktreeSelection
} from './caller-host-worktree-selection'
export type ComputerCliTarget = {
session?: string
worktree?: string
app: ComputerAppQuery
}
export function buildCurrentWorktreeSelector(cwd: string): string {
return `path:${resolvePath(cwd)}`
}
export function normalizeWorktreeSelector(selector: string, cwd: string): string {
if (selector === 'active' || selector === 'current') {
return buildCurrentWorktreeSelector(cwd)
}
return selector
}
/** Resolve Linux selectors to stored UNC paths here, where the caller's distro is known. */
export async function resolveCallerDistroPathSelector(
selector: string,
cwd: string,
client: RuntimeClient
): Promise<string> {
// A UNC cwd proves Windows/WSL routing; WSL_DISTRO_NAME is also set for Linux-native runtimes.
const callerDistro = parseWslUncPath(cwd)?.distro
const linuxPath = selector.startsWith('path:') ? selector.slice(5) : ''
const isLinuxMountedPath = /^\/mnt\/[A-Za-z](?:\/|$)/.test(linuxPath)
if (
(!callerDistro && !isLinuxMountedPath) ||
client.isRemote ||
!linuxPath.startsWith('/') ||
linuxPath.startsWith('//') ||
// Backslash is a legal Linux filename character but a separator once a path reads as UNC.
linuxPath.includes('\\')
) {
return selector
}
const worktrees = await client.call<RuntimeWorktreeListResult>('worktree.list', { limit: 10_000 })
const match = worktrees.result.worktrees.find((worktree) =>
isLinuxMountedPath
? isWslUncPathForLinuxMountedPath(worktree.path, linuxPath)
: isWslUncPathForCallerLinuxPath(worktree.path, linuxPath, callerDistro!)
)
// Preserve unmatched input verbatim; synthesizing a UNC path could guess the wrong distro.
return match ? `path:${match.path}` : selector
}
export async function normalizeWorktreeSelectorForCaller(
selector: string,
cwd: string,
client: RuntimeClient
): Promise<string> {
const normalized = normalizeWorktreeSelector(selector, cwd)
return resolveCallerDistroPathSelector(normalized, cwd, client)
}
export function assertLocalCwdWorktreeSelector(selector: string, client: RuntimeClient): void {
if (!client.isRemote) {
return
}
// A paired CLI's cwd belongs to the client filesystem, so it cannot identify server worktrees.
throw new RuntimeClientError(
'invalid_argument',
`${selector} is a local cwd shortcut and cannot be resolved against a remote runtime. Pass an explicit server-side worktree selector such as identity:<identity>, id:<repo-id>::<path>, name:<displayName>, branch:<branch>, issue:<number>, or path:<absolute-server-path>.`
)
}
export async function resolveCurrentWorktreeSelector(
cwd: string,
client: RuntimeClient,
purpose: WorkspaceSelectionPurpose = 'worktree'
): Promise<string> {
return (await resolveCurrentWorktreeSelection(cwd, client, purpose)).selector
}
export async function resolveCurrentWorktreeSelection(
cwd: string,
client: RuntimeClient,
purpose: WorkspaceSelectionPurpose = 'worktree'
): Promise<HostScopedWorktreeSelection> {
assertLocalCwdWorktreeSelector('current', client)
const worktrees = await client.call<RuntimeWorktreeListResult>('worktree.list', { limit: 10_000 })
// Preserve the repo id: duplicate registrations can make a bare path selector ambiguous.
const selection = selectEnclosingWorktreeSelectionOnHost(
worktrees.result.worktrees,
cwd,
getCallerExecutionHostId(),
process.env.ORCA_WORKTREE_ID,
purpose
)
if (!selection) {
throw new RuntimeClientError(
'selector_not_found',
`No Orca-managed worktree contains the current directory: ${resolvePath(cwd)}`
)
}
return selection
}
export async function resolveWorktreeCreateCwdContext(
cwd: string,
client: RuntimeClient,
inferRepo: boolean,
inferParent = true
): Promise<ReturnType<typeof selectWorktreeCreateCwdContext>> {
assertLocalCwdWorktreeSelector('current', client)
const worktrees = await client.call<RuntimeWorktreeListResult>('worktree.list', { limit: 10_000 })
return selectWorktreeCreateCwdContext(worktrees.result.worktrees, cwd, inferRepo, inferParent)
}
export async function getOptionalWorktreeSelector(
flags: Map<string, string | boolean>,
name: string,
cwd: string,
client: RuntimeClient
): Promise<string | undefined> {
const value = getOptionalStringFlag(flags, name)
if (!value) {
return undefined
}
if (value === 'active' || value === 'current') {
assertLocalCwdWorktreeSelector(value, client)
return await resolveCurrentWorktreeSelector(cwd, client, selectorPurpose(name))
}
return await normalizeWorktreeSelectorForCaller(value, cwd, client)
}
export async function getRequiredWorktreeSelector(
flags: Map<string, string | boolean>,
name: string,
cwd: string,
client: RuntimeClient
): Promise<string> {
const value = getRequiredStringFlag(flags, name)
if (value === 'active' || value === 'current') {
assertLocalCwdWorktreeSelector(value, client)
return await resolveCurrentWorktreeSelector(cwd, client, selectorPurpose(name))
}
return await normalizeWorktreeSelectorForCaller(value, cwd, client)
}
async function optionalCurrentWorktreeSelector(
cwd: string,
client: RuntimeClient,
purpose: WorkspaceSelectionPurpose = 'worktree'
) {
try {
return await resolveCurrentWorktreeSelector(cwd, client, purpose)
} catch (error) {
if (error instanceof RuntimeClientError && error.code === 'selector_ambiguous') {
throw error
}
return undefined
}
}
// Remote commands leave worktree unset to use server-side focus.
export async function getBrowserWorktreeSelector(
flags: Map<string, string | boolean>,
cwd: string,
client: RuntimeClient,
purpose: WorkspaceSelectionPurpose = 'worktree'
): Promise<string | undefined> {
const value = getOptionalStringFlag(flags, 'worktree')
if (value === 'all') {
return undefined
}
if (value) {
if (value === 'active' || value === 'current') {
assertLocalCwdWorktreeSelector(value, client)
return await resolveCurrentWorktreeSelector(cwd, client, purpose)
}
return await normalizeWorktreeSelectorForCaller(value, cwd, client)
}
if (client.isRemote) {
return undefined
}
return optionalCurrentWorktreeSelector(cwd, client, purpose)
}
export async function getTerminalHandle(
flags: Map<string, string | boolean>,
cwd: string,
client: RuntimeClient,
options: { requireUnambiguous?: boolean; purpose?: WorkspaceSelectionPurpose } = {}
): Promise<string> {
const explicit = getPresentStringFlag(flags, 'terminal')
if (explicit) {
return explicit
}
const worktree = await getBrowserWorktreeSelector(flags, cwd, client, options.purpose)
const response = await client.call<{ handle: string }>('terminal.resolveActive', {
worktree,
...(options.requireUnambiguous ? { requireUnambiguous: true } : {})
})
return response.result.handle
}
export async function getComputerCommandTarget(
flags: Map<string, string | boolean>,
cwd: string,
client: RuntimeClient
): Promise<ComputerCliTarget> {
const app = getRequiredStringFlag(flags, 'app')
const session = getOptionalStringFlag(flags, 'session')
const worktree = getOptionalStringFlag(flags, 'worktree')
if (session && worktree) {
throw new RuntimeClientError(
'invalid_argument',
'Computer-use targeting accepts either --session or --worktree, not both'
)
}
if (session) {
return { session, app }
}
return {
app,
worktree: await getBrowserWorktreeSelector(flags, cwd, client)
}
}
export type EmulatorCliTarget = {
worktree?: string
device?: string
emulator?: string // Orca id from list
}
export async function getEmulatorWorktreeSelector(
flags: Map<string, string | boolean>,
cwd: string,
client: RuntimeClient
): Promise<string | undefined> {
const explicit = getOptionalStringFlag(flags, 'worktree')
if (explicit === 'all') {
return undefined
}
if (explicit) {
if (explicit === 'active' || explicit === 'current') {
assertLocalCwdWorktreeSelector(explicit, client)
return resolveCurrentWorktreeSelector(cwd, client)
}
return explicit
}
if (client.isRemote) {
return undefined
}
const terminalWorktreeId = process.env.ORCA_WORKTREE_ID
if (terminalWorktreeId?.trim()) {
const scoped = await resolveCallerPaneWorktreeSelector(terminalWorktreeId, client)
if (scoped) {
return scoped
}
}
const folderWorkspaceId = process.env.ORCA_WORKSPACE_ID?.trim()
if (folderWorkspaceId?.startsWith('folder:')) {
return folderWorkspaceId
}
return optionalCurrentWorktreeSelector(cwd, client)
}
export async function getEmulatorCommandTarget(
flags: Map<string, string | boolean>,
cwd: string,
client: RuntimeClient
): Promise<EmulatorCliTarget> {
const device = getOptionalStringFlag(flags, 'device')
const emulator = getOptionalStringFlag(flags, 'emulator')
const worktree = await getEmulatorWorktreeSelector(flags, cwd, client)
if (device || emulator) {
return {
device: device || undefined,
emulator: emulator || undefined,
worktree
}
}
return { worktree }
}