Fix CLI-launched agent terminal viewport (#2561)

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Jinwoo Hong
2026-05-21 15:35:48 -07:00
committed by GitHub
co-authored by Orca
parent 630de67fcb
commit a75a822339
8 changed files with 233 additions and 46 deletions
+56 -26
View File
@@ -1,39 +1,69 @@
import { describe, expect, it } from 'vitest'
import { shouldForceVisibleCodexTerminal } from './codex-command-classification'
import {
shouldUseRendererBackedCodexTerminal,
shouldUseRendererBackedInteractiveTerminal
} from './codex-command-classification'
describe('shouldForceVisibleCodexTerminal', () => {
it('forces visible terminal creation for interactive Codex sessions', () => {
expect(shouldForceVisibleCodexTerminal('codex')).toBe(true)
expect(shouldForceVisibleCodexTerminal('codex -m gpt-5 "fix the flaky test"')).toBe(true)
expect(shouldForceVisibleCodexTerminal('codex resume --last')).toBe(true)
expect(shouldForceVisibleCodexTerminal('codex fork')).toBe(true)
expect(shouldForceVisibleCodexTerminal('codex login')).toBe(true)
expect(shouldForceVisibleCodexTerminal('codex cloud')).toBe(true)
expect(shouldForceVisibleCodexTerminal('codex -c active=cloud cloud')).toBe(true)
expect(shouldForceVisibleCodexTerminal('codex.cmd resume --last')).toBe(true)
expect(shouldForceVisibleCodexTerminal('env OPENAI_API_KEY=stub codex')).toBe(true)
describe('shouldUseRendererBackedCodexTerminal', () => {
it('uses renderer-backed terminal creation for interactive Codex sessions', () => {
expect(shouldUseRendererBackedCodexTerminal('codex')).toBe(true)
expect(shouldUseRendererBackedCodexTerminal('codex -m gpt-5 "fix the flaky test"')).toBe(true)
expect(shouldUseRendererBackedCodexTerminal('codex resume --last')).toBe(true)
expect(shouldUseRendererBackedCodexTerminal('codex fork')).toBe(true)
expect(shouldUseRendererBackedCodexTerminal('codex login')).toBe(true)
expect(shouldUseRendererBackedCodexTerminal('codex cloud')).toBe(true)
expect(shouldUseRendererBackedCodexTerminal('codex -c active=cloud cloud')).toBe(true)
expect(shouldUseRendererBackedCodexTerminal('codex.cmd resume --last')).toBe(true)
expect(shouldUseRendererBackedCodexTerminal('env OPENAI_API_KEY=stub codex')).toBe(true)
})
it('keeps one-shot Codex commands on the background path', () => {
expect(shouldForceVisibleCodexTerminal('codex exec summarize')).toBe(false)
expect(shouldForceVisibleCodexTerminal('codex -m gpt-5 review')).toBe(false)
expect(shouldForceVisibleCodexTerminal('codex login status')).toBe(false)
expect(shouldForceVisibleCodexTerminal('codex login --with-api-key')).toBe(false)
expect(shouldForceVisibleCodexTerminal('codex cloud list --json')).toBe(false)
expect(shouldForceVisibleCodexTerminal('codex -c active=cloud cloud list --json')).toBe(false)
expect(shouldForceVisibleCodexTerminal('codex cloud --enable foo list --json')).toBe(false)
expect(shouldUseRendererBackedCodexTerminal('codex exec summarize')).toBe(false)
expect(shouldUseRendererBackedCodexTerminal('codex -m gpt-5 review')).toBe(false)
expect(shouldUseRendererBackedCodexTerminal('codex login status')).toBe(false)
expect(shouldUseRendererBackedCodexTerminal('codex login --with-api-key')).toBe(false)
expect(shouldUseRendererBackedCodexTerminal('codex cloud list --json')).toBe(false)
expect(shouldUseRendererBackedCodexTerminal('codex -c active=cloud cloud list --json')).toBe(
false
)
expect(shouldUseRendererBackedCodexTerminal('codex cloud --enable foo list --json')).toBe(false)
expect(
shouldForceVisibleCodexTerminal('env -u DEBUG CODEX_HOME=/tmp/codex codex exec summarize')
shouldUseRendererBackedCodexTerminal(
'env -u DEBUG CODEX_HOME=/tmp/codex codex exec summarize'
)
).toBe(false)
expect(shouldForceVisibleCodexTerminal('codex cloud exec "fix it"')).toBe(false)
expect(shouldForceVisibleCodexTerminal('codex cloud --version')).toBe(false)
expect(shouldForceVisibleCodexTerminal('codex --help')).toBe(false)
expect(shouldUseRendererBackedCodexTerminal('codex cloud exec "fix it"')).toBe(false)
expect(shouldUseRendererBackedCodexTerminal('codex cloud --version')).toBe(false)
expect(shouldUseRendererBackedCodexTerminal('codex --help')).toBe(false)
})
it('ignores non-Codex commands', () => {
expect(shouldForceVisibleCodexTerminal(undefined)).toBe(false)
expect(shouldForceVisibleCodexTerminal('claude')).toBe(false)
expect(shouldForceVisibleCodexTerminal('npm exec codex')).toBe(false)
expect(shouldUseRendererBackedCodexTerminal(undefined)).toBe(false)
expect(shouldUseRendererBackedCodexTerminal('claude')).toBe(false)
expect(shouldUseRendererBackedCodexTerminal('npm exec codex')).toBe(false)
})
})
describe('shouldUseRendererBackedInteractiveTerminal', () => {
it('uses renderer-backed terminal creation for interactive Claude sessions', () => {
expect(shouldUseRendererBackedInteractiveTerminal('claude')).toBe(true)
expect(shouldUseRendererBackedInteractiveTerminal('claude --prefill "review this"')).toBe(true)
expect(shouldUseRendererBackedInteractiveTerminal('/opt/anthropic/bin/claude')).toBe(true)
expect(shouldUseRendererBackedInteractiveTerminal('env ANTHROPIC_BASE_URL=test claude')).toBe(
true
)
})
it('keeps one-shot Claude commands on the background path', () => {
expect(shouldUseRendererBackedInteractiveTerminal('claude -p "summarize"')).toBe(false)
expect(shouldUseRendererBackedInteractiveTerminal('claude --print "summarize"')).toBe(false)
expect(shouldUseRendererBackedInteractiveTerminal('claude --help')).toBe(false)
expect(shouldUseRendererBackedInteractiveTerminal('claude --version')).toBe(false)
})
it('preserves Codex command classification', () => {
expect(shouldUseRendererBackedInteractiveTerminal('codex')).toBe(true)
expect(shouldUseRendererBackedInteractiveTerminal('codex exec summarize')).toBe(false)
})
})
+31 -1
View File
@@ -130,6 +130,10 @@ function isCodexExecutable(command: string): boolean {
return command === 'codex' || command === 'codex.exe' || command === 'codex.cmd'
}
function isClaudeExecutable(command: string): boolean {
return command === 'claude' || command === 'claude.exe' || command === 'claude.cmd'
}
function isShellAssignment(token: string): boolean {
return /^[A-Za-z_][A-Za-z0-9_]*=/.test(token)
}
@@ -178,6 +182,11 @@ function isVersionFlag(token: string): boolean {
return token === '--version' || token === '-V'
}
function isClaudePrintFlag(token: string): boolean {
const optionName = codexGlobalOptionName(token)
return optionName === '-p' || optionName === '--print'
}
function findCodexSubcommand(
tokens: string[],
startIndex: number,
@@ -259,7 +268,7 @@ function isNonInteractiveCodexSubcommand(tokens: string[]): boolean {
return CODEX_NON_INTERACTIVE_SUBCOMMANDS.has(normalizedSubcommand)
}
export function shouldForceVisibleCodexTerminal(command: string | undefined): boolean {
export function shouldUseRendererBackedCodexTerminal(command: string | undefined): boolean {
if (!command) {
return false
}
@@ -275,3 +284,24 @@ export function shouldForceVisibleCodexTerminal(command: string | undefined): bo
return !isNonInteractiveCodexSubcommand(tokens)
}
export function shouldUseRendererBackedInteractiveTerminal(command: string | undefined): boolean {
if (!command) {
return false
}
const tokens = stripShellLaunchPrefix(
tokenizeLeadingShellWords(command.trim(), 32).filter((token) => token.length > 0)
)
const executable = tokens[0] ? commandBasename(tokens[0]) : ''
if (isCodexExecutable(executable)) {
return !isNonInteractiveCodexSubcommand(tokens)
}
if (isClaudeExecutable(executable)) {
return !tokens
.slice(1)
.some((token) => isHelpFlag(token) || isVersionFlag(token) || isClaudePrintFlag(token))
}
return false
}
+7 -6
View File
@@ -11,7 +11,7 @@ import type {
RuntimeTerminalWait
} from '../../shared/runtime-types'
import type { CommandHandler } from '../dispatch'
import { shouldForceVisibleCodexTerminal } from '../codex-command-classification'
import { shouldUseRendererBackedInteractiveTerminal } from '../codex-command-classification'
import {
formatTerminalClose,
formatTerminalCreate,
@@ -130,17 +130,18 @@ export const TERMINAL_HANDLERS: Record<string, CommandHandler> = {
)
}
const command = getOptionalStringFlag(flags, 'command')
const useRendererBackedInteractiveTerminal =
!client.isRemote && shouldUseRendererBackedInteractiveTerminal(command)
const focus = flags.get('focus') === true
const rendererBacked = !client.isRemote && shouldForceVisibleCodexTerminal(command)
const result = await client.call<{ terminal: RuntimeTerminalCreate }>('terminal.create', {
worktree: await getBrowserWorktreeSelector(flags, cwd, client),
command,
title: getOptionalStringFlag(flags, 'title'),
// Why: Codex's interactive TUI must be born in a renderer-backed
// terminal. The runtime's default create path spawns first in a
// headless/background PTY and only adopts into the UI afterward.
// Why: interactive local agent TUIs need the renderer-backed terminal
// path for browser-side features, but CLI creates must stay backgrounded
// unless the caller explicitly asks for focus.
focus,
...(rendererBacked ? { rendererBacked: true, activate: focus } : {})
...(useRendererBackedInteractiveTerminal ? { rendererBacked: true, activate: focus } : {})
})
printResult(result, json, formatTerminalCreate)
},
+76 -2
View File
@@ -923,7 +923,7 @@ describe('orca cli worktree awareness', () => {
})
})
it('forces the visible terminal path for interactive Codex startup commands', async () => {
it('keeps interactive Codex startup commands backgrounded unless focus is explicit', async () => {
queueFixtures(
callMock,
okFixture('req_terminal_create', {
@@ -1144,7 +1144,7 @@ describe('orca cli worktree awareness', () => {
})
})
it('forces the visible terminal path for Codex prompts after global options', async () => {
it('keeps Codex prompts after global options backgrounded unless focus is explicit', async () => {
queueFixtures(
callMock,
okFixture('req_terminal_create', {
@@ -1182,6 +1182,80 @@ describe('orca cli worktree awareness', () => {
})
})
it('keeps interactive Claude startup commands backgrounded unless focus is explicit', async () => {
queueFixtures(
callMock,
okFixture('req_terminal_create', {
terminal: {
handle: 'term_1',
worktreeId: 'repo-1::/tmp/repo/feature',
title: 'Claude'
}
})
)
vi.spyOn(console, 'log').mockImplementation(() => {})
await main(
[
'terminal',
'create',
'--worktree',
'path:/tmp/repo/feature',
'--title',
'Claude',
'--command',
'claude',
'--json'
],
'/tmp/repo'
)
expect(callMock).toHaveBeenCalledWith('terminal.create', {
worktree: 'path:/tmp/repo/feature',
command: 'claude',
title: 'Claude',
focus: false,
rendererBacked: true,
activate: false
})
})
it('keeps Claude print commands on the background terminal path', async () => {
queueFixtures(
callMock,
okFixture('req_terminal_create', {
terminal: {
handle: 'term_1',
worktreeId: 'repo-1::/tmp/repo/feature',
title: 'Claude print'
}
})
)
vi.spyOn(console, 'log').mockImplementation(() => {})
await main(
[
'terminal',
'create',
'--worktree',
'path:/tmp/repo/feature',
'--title',
'Claude print',
'--command',
'claude -p "summarize"',
'--json'
],
'/tmp/repo'
)
expect(callMock).toHaveBeenCalledWith('terminal.create', {
worktree: 'path:/tmp/repo/feature',
command: 'claude -p "summarize"',
title: 'Claude print',
focus: false
})
})
it('uses the resolved enclosing worktree for other worktree consumers', async () => {
queueFixtures(
callMock,