Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
84 changes: 84 additions & 0 deletions src/main/runtime/terminal-wait-codex-ready-header.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { AGENT_PROMPT_TEST_WORKTREE_PATH } from './agent-prompt-submission-runtime-test-fixture'
import { OrcaRuntimeService } from './orca-runtime'
import { makeStore } from './runtime-rpc-worktree-store-fixtures'

vi.mock('../git/worktree', () => ({
listWorktrees: vi.fn().mockResolvedValue([
{
path: '/tmp/worktree-a',
head: 'abc',
branch: 'feature/codex-ready-header',
isBare: false,
isMainWorktree: false
}
]),
listWorktreesStrict: vi.fn().mockResolvedValue([
{
path: '/tmp/worktree-a',
head: 'abc',
branch: 'feature/codex-ready-header',
isBare: false,
isMainWorktree: false
}
])
}))

const PTY = 'pty-codex'
const BOOT_HEADER = ' >_ OpenAI Codex (v0.152.0)\n model: loading\n directory: loading\n'
const READY_HEADER =
' >_ OpenAI Codex (v0.152.0)\n model: gpt-5.6-sol medium\n directory: /tmp/worktree-a\n'

async function createCodexTerminal(): Promise<{ runtime: OrcaRuntimeService; handle: string }> {
const runtime = new OrcaRuntimeService(makeStore() as never)
runtime.setPtyController({
spawn: async () => ({ id: PTY }),
write: () => true,
kill: () => true,
getForegroundProcess: async () => null
})
const terminal = await runtime.createTerminal(`path:${AGENT_PROMPT_TEST_WORKTREE_PATH}`, {
launchAgent: 'codex'
})
return { runtime, handle: terminal.handle }
}

describe('tui-idle and the Codex boot header', () => {
afterEach(() => vi.useRealTimers())

// Why: worker-start pastes as soon as tui-idle resolves; on this header Codex is still booting
// and the paste lands scrambled on the splash screen or parks unsubmitted.
it('does not treat a header still loading its model and directory as ready', async () => {
vi.useFakeTimers()
const { runtime, handle } = await createCodexTerminal()
runtime.onPtyData(PTY, BOOT_HEADER, Date.now())

const wait = runtime.waitForTerminal(handle, { condition: 'tui-idle', timeoutMs: 1_000 })
const rejected = expect(wait).rejects.toThrow('timeout')
await vi.advanceTimersByTimeAsync(3_000)

await rejected
})

it('treats the header as ready once the model and directory are settled', async () => {
const { runtime, handle } = await createCodexTerminal()
runtime.onPtyData(PTY, BOOT_HEADER, Date.now())
runtime.onPtyData(PTY, READY_HEADER, Date.now())

await expect(
runtime.waitForTerminal(handle, { condition: 'tui-idle', timeoutMs: 1_000 })
).resolves.toMatchObject({ condition: 'tui-idle', status: 'running' })
})

it('does not treat a header whose values have not arrived yet as ready', async () => {
vi.useFakeTimers()
const { runtime, handle } = await createCodexTerminal()
runtime.onPtyData(PTY, ' >_ OpenAI Codex (v0.152.0)\n model:\n directory:\n', Date.now())

const wait = runtime.waitForTerminal(handle, { condition: 'tui-idle', timeoutMs: 1_000 })
const rejected = expect(wait).rejects.toThrow('timeout')
await vi.advanceTimersByTimeAsync(3_000)

await rejected
})
})
22 changes: 21 additions & 1 deletion src/main/runtime/terminal-wait-detection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,27 @@ function findCodexReadyPromptIndex(normalized: string): number | null {
}
const readySegment = normalized.slice(headerIndex)
// Why: Codex prints permissions only in YOLO mode; the stable ready header is OpenAI Codex + model + directory.
return readySegment.includes('model:') && readySegment.includes('directory:') ? headerIndex : null
// Why last occurrence: a repaint appends the settled row after the booting one in a stacked tail.
const modelIndex = readySegment.lastIndexOf('model:')
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Comment thread
pullfrog[bot] marked this conversation as resolved.
Resolved
const directoryIndex = readySegment.lastIndexOf('directory:')
if (modelIndex === -1 || directoryIndex === -1) {
return null
}
// Why: the same header is painted with `model: loading` / `directory: loading` while Codex boots
// and its composer does not take input yet; only settled values are readiness.
if (
codexHeaderValueIsUnsettled(readySegment, modelIndex + 'model:'.length) ||
codexHeaderValueIsUnsettled(readySegment, directoryIndex + 'directory:'.length)
) {
return null
}
return headerIndex
}

function codexHeaderValueIsUnsettled(segment: string, valueStart: number): boolean {
const lineEnd = segment.indexOf('\n', valueStart)
const value = segment.slice(valueStart, lineEnd === -1 ? undefined : lineEnd).trim()
return value.length === 0 || value.startsWith('loading')
}

function findAntigravityReadyPromptIndex(normalized: string): number | null {
Expand Down
Loading