Skip to content
Open
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
1 change: 1 addition & 0 deletions src/cli/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export {
formatProjectList
} from './project-format'
export {
formatTerminalAgentStatus,
formatTerminalClose,
formatTerminalCreate,
formatTerminalFocus,
Expand Down
1 change: 1 addition & 0 deletions src/cli/handler-group-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const HANDLER_GROUPS: readonly HandlerGroup[] = [
keys: [
'terminal list',
'terminal show',
'terminal agent-status',
'terminal read',
'terminal send',
'terminal wait',
Expand Down
99 changes: 99 additions & 0 deletions src/cli/handlers/terminal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,102 @@ describe('terminal send CLI', () => {
expect(log).not.toHaveBeenCalled()
})
})

describe('terminal agent-status CLI', () => {
afterEach(() => {
vi.restoreAllMocks()
process.exitCode = ORIGINAL_EXIT_CODE
})

it.each(['working', 'permission', 'idle'] as const)(
'prints terminal agent status JSON for %s',
async (status) => {
process.exitCode = undefined
const call = vi.fn().mockResolvedValue({
id: 'req-agent-status',
ok: true,
result: {
agentStatus: {
handle: 'term_worker',
isRunningAgent: true,
status
}
},
_meta: { runtimeId: 'runtime-1' }
})
const log = vi.spyOn(console, 'log').mockImplementation(() => {})

await TERMINAL_HANDLERS['terminal agent-status']({
flags: new Map([['terminal', 'term_worker']]),
client: terminalClient(call),
cwd: '/tmp/worktree',
json: true
})

expect(call).toHaveBeenCalledWith('terminal.agentStatus', { terminal: 'term_worker' })
expect(JSON.parse(String(log.mock.calls[0]?.[0])).result.agentStatus).toEqual({
handle: 'term_worker',
isRunningAgent: true,
status
})
}
)

it('prints terminal agent status in human-readable mode', async () => {
process.exitCode = undefined
const call = vi.fn().mockResolvedValue({
id: 'req-agent-status',
ok: true,
result: {
agentStatus: {
handle: 'term_worker',
isRunningAgent: true,
status: 'permission'
}
},
_meta: { runtimeId: 'runtime-1' }
})
const log = vi.spyOn(console, 'log').mockImplementation(() => {})

await TERMINAL_HANDLERS['terminal agent-status']({
flags: new Map([['terminal', 'term_worker']]),
client: terminalClient(call),
cwd: '/tmp/worktree',
json: false
})

expect(call).toHaveBeenCalledWith('terminal.agentStatus', { terminal: 'term_worker' })
expect(String(log.mock.calls[0]?.[0])).toBe(
['handle: term_worker', 'isRunningAgent: true', 'status: permission'].join('\n')
)
})

it('prints null status for a terminal that is not running an agent', async () => {
process.exitCode = undefined
const call = vi.fn().mockResolvedValue({
id: 'req-agent-status',
ok: true,
result: {
agentStatus: {
handle: 'term_shell',
isRunningAgent: false,
status: null
}
},
_meta: { runtimeId: 'runtime-1' }
})
const log = vi.spyOn(console, 'log').mockImplementation(() => {})

await TERMINAL_HANDLERS['terminal agent-status']({
flags: new Map([['terminal', 'term_shell']]),
client: terminalClient(call),
cwd: '/tmp/worktree',
json: false
})

expect(call).toHaveBeenCalledWith('terminal.agentStatus', { terminal: 'term_shell' })
const printed = String(log.mock.calls[0]?.[0])
expect(printed).toContain('isRunningAgent: false')
expect(printed).toContain('status: null')
})
})
11 changes: 11 additions & 0 deletions src/cli/handlers/terminal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
RuntimeTerminalAgentStatus,
RuntimeTerminalCreate,
RuntimeTerminalFocus,
RuntimeTerminalListResult,
Expand All @@ -12,6 +13,7 @@ import type {
import type { CommandHandler } from '../dispatch'
import { shouldUseRendererBackedInteractiveTerminal } from '../codex-command-classification'
import {
formatTerminalAgentStatus,
formatTerminalCreate,
formatTerminalFocus,
formatTerminalList,
Expand Down Expand Up @@ -88,6 +90,15 @@ export const TERMINAL_HANDLERS: Record<string, CommandHandler> = {
})
printResult(result, json, formatTerminalShow)
},
'terminal agent-status': async ({ flags, client, cwd, json }) => {
const result = await client.call<{ agentStatus: RuntimeTerminalAgentStatus }>(
'terminal.agentStatus',
{
terminal: await getTerminalHandle(flags, cwd, client)
}
)
printResult(result, json, formatTerminalAgentStatus)
},
'terminal read': async ({ flags, client, cwd, json }) => {
const cursorFlag = getOptionalStringFlag(flags, 'cursor')
const cursor =
Expand Down
11 changes: 11 additions & 0 deletions src/cli/specs/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ export const CORE_COMMAND_SPECS: CommandSpec[] = [
usage: 'orca terminal show [--terminal <handle>] [--json]',
allowedFlags: [...GLOBAL_FLAGS, 'terminal']
},
{
path: ['terminal', 'agent-status'],
summary: 'Show the detected agent state for a terminal',
usage: 'orca terminal agent-status [--terminal <handle>] [--json]',
allowedFlags: [...GLOBAL_FLAGS, 'terminal'],
notes: ['Reports terminal.agentStatus state: working, permission, idle, or null.'],
examples: [
'orca terminal agent-status --terminal term_abc123',
'orca terminal agent-status --terminal term_abc123 --json'
]
},
{
path: ['terminal', 'read'],
summary: 'Read bounded terminal output',
Expand Down
19 changes: 19 additions & 0 deletions src/cli/terminal-format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
RuntimeTerminalWaitBlockedReason
} from '../shared/runtime-terminal-contracts'
import {
formatTerminalAgentStatus,
formatTerminalClose,
formatTerminalFocus,
formatTerminalRead,
Expand Down Expand Up @@ -257,3 +258,21 @@ describe('formatTerminalRead draft separation', () => {
expect(output).toContain('\n\nBuild passed\n❯')
})
})

describe('formatTerminalAgentStatus', () => {
it('prints handle, running flag, and status', () => {
expect(
formatTerminalAgentStatus({
agentStatus: { handle: 'term_worker', isRunningAgent: true, status: 'permission' }
})
).toBe(['handle: term_worker', 'isRunningAgent: true', 'status: permission'].join('\n'))
})

it('prints null when the terminal is not running an agent', () => {
expect(
formatTerminalAgentStatus({
agentStatus: { handle: 'term_shell', isRunningAgent: false, status: null }
})
).toBe(['handle: term_shell', 'isRunningAgent: false', 'status: null'].join('\n'))
})
})
12 changes: 12 additions & 0 deletions src/cli/terminal-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { describeTerminalWaitBlockedReason } from '../shared/terminal/runtime-po
import { formatListingHostScope, type WithAnnotatedHostScope } from './omitted-host-scope-selectors'
import type {
RuntimeTerminalClose,
RuntimeTerminalAgentStatus,
RuntimeTerminalCreate,
RuntimeTerminalFocus,
RuntimeTerminalListResult,
Expand Down Expand Up @@ -128,6 +129,17 @@ function formatAgentWait(agentWait: RuntimeTerminalShow['agentWait']): string {
return `${describeTerminalWaitBlockedReason(agentWait.reason)} (via ${agentWait.source})`
}

export function formatTerminalAgentStatus(result: {
agentStatus: RuntimeTerminalAgentStatus
}): string {
const agentStatus = result.agentStatus
return [
`handle: ${agentStatus.handle}`,
`isRunningAgent: ${agentStatus.isRunningAgent}`,
`status: ${agentStatus.status ?? 'null'}`
].join('\n')
}

export function formatTerminalRead(result: { terminal: RuntimeTerminalRead }): string {
const terminal = result.terminal
const oldestCursor =
Expand Down