diff --git a/src/cli/handlers/terminal.test.ts b/src/cli/handlers/terminal.test.ts index bc411e322bf9..727befe4fb74 100644 --- a/src/cli/handlers/terminal.test.ts +++ b/src/cli/handlers/terminal.test.ts @@ -245,3 +245,51 @@ describe('terminal send CLI', () => { }) }) }) + +describe('terminal create CLI', () => { + afterEach(() => { + vi.restoreAllMocks() + }) + + async function createWith(command: string | undefined): Promise> { + const call = vi.fn().mockResolvedValue({ + result: { terminal: { handle: 'term-1', tabId: 'tab-1', worktreeId: 'wt-1' } } + }) + vi.spyOn(console, 'log').mockImplementation(() => {}) + const flags = new Map([['worktree', 'path:/tmp/wt']]) + if (command !== undefined) { + flags.set('command', command) + } + await TERMINAL_HANDLERS['terminal create']({ + flags, + client: { call, isRemote: false } as unknown as RuntimeClient, + cwd: '/tmp/worktree', + json: true + }) + return call.mock.calls[0]![1] as Record + } + + // Why (#17291): a worker-start chained right after create must find the agent already recognized. + it.each([ + ['codex', 'codex'], + ['claude --model sonnet --effort high', 'claude'], + ['codex --yolo', 'codex'] + ])('infers launchAgent from the interactive agent command %s', async (command, agent) => { + expect(await createWith(command)).toMatchObject({ command, launchAgent: agent }) + }) + + it.each([ + 'codex exec "summarize"', + 'claude -p "hello"', + 'claude --version', + 'npm run dev', + // Why: the interactivity of other agents' commands is not classified here, and a launchAgent + // also marks the pane unattended, so they stay unlabeled until the runtime infers them. + 'gemini', + 'opencode run "summarize"', + 'aider --message "fix"', + undefined + ])('leaves launchAgent unset for %s', async (command) => { + expect(await createWith(command)).not.toHaveProperty('launchAgent') + }) +}) diff --git a/src/cli/handlers/terminal.ts b/src/cli/handlers/terminal.ts index a0a8dc773cce..a3a7a7955ce3 100644 --- a/src/cli/handlers/terminal.ts +++ b/src/cli/handlers/terminal.ts @@ -12,6 +12,7 @@ import type { } from '../../shared/runtime-types' import type { CommandHandler } from '../dispatch' import { shouldUseRendererBackedInteractiveTerminal } from '../codex-command-classification' +import { recognizeAgentProcessFromCommandLine } from '../../shared/agent-process-recognition' import { formatTerminalClose, formatTerminalCreate, @@ -182,6 +183,18 @@ export const TERMINAL_HANDLERS: Record = { const useRendererBackedInteractiveTerminal = !client.isRemote && shouldUseRendererBackedInteractiveTerminal(command) const focus = flags.get('focus') === true + // Why (#17291): without this the pane has no launchAgent until the runtime infers one from an + // OSC title, so a worker-start chained right after create takes the open-loop submit delay and + // the generic readiness path. Only claude and codex are labeled: they are the agents with an + // echo gate to unlock, and the only ones whose one-shot forms (`claude -p`, `codex exec`, …) + // are all known here. A launchAgent also marks the pane unattended, so an unknown agent's + // one-shot must not be guessed interactive. + const recognizedAgent = recognizeAgentProcessFromCommandLine(command)?.agent + const inferredLaunchAgent = + (recognizedAgent === 'claude' || recognizedAgent === 'codex') && + shouldUseRendererBackedInteractiveTerminal(command) + ? recognizedAgent + : undefined const result = await client.call<{ terminal: RuntimeTerminalCreate }>('terminal.create', { worktree: await getBrowserWorktreeSelector(flags, cwd, client), command, @@ -191,7 +204,8 @@ export const TERMINAL_HANDLERS: Record = { // unless the caller explicitly asks for focus. focus, ...(focus ? { presentation: 'focused' } : {}), - ...(useRendererBackedInteractiveTerminal ? { rendererBacked: true, activate: focus } : {}) + ...(useRendererBackedInteractiveTerminal ? { rendererBacked: true, activate: focus } : {}), + ...(inferredLaunchAgent ? { launchAgent: inferredLaunchAgent } : {}) }) printResult(result, json, formatTerminalCreate) },