Files
orca/src/main/runtime/runtime-agent-launch-resolution.ts
T
Kelvin Amoaba d503ff9f16 fix(terminal): hold a phone send until a new Claude tab's composer is up (#36494)
* fix(terminal): hold a phone send until a new Claude tab's composer is up

* fix(terminal): replace a reused PTY's launch-composer watch
2026-10-31 00:13:34 +00:00

144 lines
5.3 KiB
TypeScript

import { getRepoRoutingConnectionId } from '../../shared/execution-host'
import { createHash } from 'node:crypto'
import type { Repo } from '../../shared/repo-types'
import type { TuiAgent } from '../../shared/tui-agent'
import type { SleepingAgentLaunchConfig } from '../../shared/agent-session-resume'
import type { ClaudeAgentTeamsMode } from '../../shared/claude-agent-teams-tmux-compat'
import type { ProjectExecutionRuntimeResolution } from '../../shared/project-execution-runtime'
import {
executionRuntimePlatform,
remoteExecutionPlatform
} from '../../shared/agent-launch-runtime'
import {
getTuiAgentLaunchCommand,
isTuiAgent,
TUI_AGENT_CONFIG
} from '../../shared/tui-agent-config'
import { isTuiAgentEnabled } from '../../shared/tui-agent-selection'
import { recognizeAgentLaunchCommand } from '../../shared/agent-command-line-intent'
export function mergeTerminalEnvDeletionKeys(
first: readonly string[] | undefined,
second: readonly string[] | undefined
): string[] | undefined {
const merged = [...new Set([...(first ?? []), ...(second ?? [])])]
return merged.length > 0 ? merged : undefined
}
export function isAgentSessionOperationOutcomeUnknown(error: unknown): boolean {
return (
typeof error === 'object' &&
error !== null &&
'agentSessionOperationOutcome' in error &&
error.agentSessionOperationOutcome === 'unknown'
)
}
export function deterministicAgentSessionUuid(seed: string): string {
const hex = createHash('sha256').update(seed).digest('hex').slice(0, 32).split('')
hex[12] = '4'
hex[16] = ((Number.parseInt(hex[16]!, 16) & 0x3) | 0x8).toString(16)
const value = hex.join('')
return `${value.slice(0, 8)}-${value.slice(8, 12)}-${value.slice(12, 16)}-${value.slice(16, 20)}-${value.slice(20)}`
}
export function copySleepingAgentLaunchConfig(
config: SleepingAgentLaunchConfig
): SleepingAgentLaunchConfig {
return {
...(config.agentCommand ? { agentCommand: config.agentCommand } : {}),
agentArgs: config.agentArgs,
agentEnv: { ...config.agentEnv },
...(config.ompResumeFilePath ? { ompResumeFilePath: config.ompResumeFilePath } : {})
}
}
export function normalizeAgentLaunchCommandForMatch(command: string): string {
return command.trim().replace(/\s+/g, ' ')
}
export function resolveBareAgentLaunchCommand(args: {
command: string | undefined
settings: {
agentCmdOverrides?: Partial<Record<TuiAgent, string>> | null
disabledTuiAgents?: Iterable<unknown> | null
}
platform: NodeJS.Platform
isRemote: boolean
}): TuiAgent | null {
const command = args.command ? normalizeAgentLaunchCommandForMatch(args.command) : ''
if (!command) {
return null
}
const cmdOverrides = args.settings.agentCmdOverrides ?? {}
for (const agent of Object.keys(TUI_AGENT_CONFIG) as TuiAgent[]) {
if (!isTuiAgentEnabled(agent, args.settings.disabledTuiAgents)) {
continue
}
const override = cmdOverrides[agent]?.trim()
const defaultLaunchCommand = getTuiAgentLaunchCommand(TUI_AGENT_CONFIG[agent], args.platform, {
isRemote: args.isRemote
})
const launchCommands = override ? [defaultLaunchCommand, override] : [defaultLaunchCommand]
if (
launchCommands.some((candidate) => command === normalizeAgentLaunchCommandForMatch(candidate))
) {
return agent
}
}
return null
}
/** Why: flags keep `omp --model x` unmanaged, but the pane is still omp's for tui-idle and identity. */
export function resolveLaunchCommandPaneAgent(command: string | null | undefined): TuiAgent | null {
const recognized = recognizeAgentLaunchCommand(command)
return recognized && isTuiAgent(recognized.agent) ? recognized.agent : null
}
/** The agent a fresh launch's command starts; a command override keeps the planned agent. No command starts none. */
export function resolveLaunchedAgent(
command: string | null | undefined,
plannedAgent: unknown
): TuiAgent | null {
if (!command) {
return null
}
const recognized = resolveLaunchCommandPaneAgent(command)
// A known agent run headless (`claude --print`) has no composer; only an unrecognized override keeps the plan.
if (recognized || recognizeAgentLaunchCommand(command, { includeHeadlessOneShot: true })) {
return recognized
}
return isTuiAgent(plannedAgent) ? plannedAgent : null
}
export function inferCapturedClaudeAgentTeamsMode(
launchConfig: SleepingAgentLaunchConfig | undefined,
command: string | undefined,
currentMode: ClaudeAgentTeamsMode | undefined
): ClaudeAgentTeamsMode | undefined {
const capturedCommand = launchConfig?.agentCommand?.trim() || command?.trim() || ''
const capturedArgs = launchConfig?.agentArgs?.trim() ?? ''
const capturedLaunch = `${capturedCommand} ${capturedArgs}`.trim()
if (/(^|\s)--teammate-mode(?:=|\s+)auto(?:\s|$)/.test(capturedLaunch)) {
return 'native-panes-shim'
}
if (/(^|\s)--teammate-mode(?:=|\s+)in-process(?:\s|$)/.test(capturedLaunch)) {
return 'in-process'
}
if (launchConfig && /(^|\s)--resume(?:\s|=|$)/.test(command?.trim() ?? '')) {
return 'off'
}
return currentMode
}
export function getAgentLaunchPlatformForRepo(
repo: Pick<Repo, 'connectionId' | 'executionHostId' | 'path'>,
projectRuntime?: ProjectExecutionRuntimeResolution
): NodeJS.Platform {
return getRepoRoutingConnectionId(repo)
? remoteExecutionPlatform(repo.path)
: executionRuntimePlatform(projectRuntime, undefined, process.platform)
}