Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/cli/handlers/terminal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,51 @@ describe('terminal send CLI', () => {
})
})
})

describe('terminal create CLI', () => {
afterEach(() => {
vi.restoreAllMocks()
})

async function createWith(command: string | undefined): Promise<Record<string, unknown>> {
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<string, string | boolean>([['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<string, unknown>
}

// 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')
})
})
16 changes: 15 additions & 1 deletion src/cli/handlers/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -182,6 +183,18 @@ export const TERMINAL_HANDLERS: Record<string, CommandHandler> = {
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,
Expand All @@ -191,7 +204,8 @@ export const TERMINAL_HANDLERS: Record<string, CommandHandler> = {
// 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)
},
Expand Down