From c27a406d488df754b2505478ef28c156fe7952d0 Mon Sep 17 00:00:00 2001 From: Jinwoo Hong <73622457+Jinwoo-H@users.noreply.github.com> Date: Fri, 29 May 2026 01:17:49 -0400 Subject: [PATCH] Detect Antigravity dispatch injection readiness (#3031) Co-authored-by: Orca --- src/main/runtime/orca-runtime.test.ts | 368 ++++++++++++++++++++++++++ src/main/runtime/orca-runtime.ts | 72 +++-- 2 files changed, 422 insertions(+), 18 deletions(-) diff --git a/src/main/runtime/orca-runtime.test.ts b/src/main/runtime/orca-runtime.test.ts index 59766abf5..16e4031af 100644 --- a/src/main/runtime/orca-runtime.test.ts +++ b/src/main/runtime/orca-runtime.test.ts @@ -445,6 +445,36 @@ const TEST_WORKTREE_PATH = '/tmp/worktree-a' const TEST_WORKTREE_ID = `${TEST_REPO_ID}::${TEST_WORKTREE_PATH}` const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/ +function antigravityReadyScreen(model = 'Gemini 3.5 Flash (High)'): string { + return [ + 'Antigravity CLI 1.0.3', + 'user@example.com (Antigravity Business)', + model, + '~/orca/workspaces/orca/agy-dispatch-issue', + '>' + ].join('\n') +} + +function antigravityPromptBeforeModelReadyScreen(model = 'Gemini 3.5 Flash (High)'): string { + return [ + 'Antigravity CLI 1.0.3', + 'user@example.com', + '~/orca/workspaces/orca/agy-dispatch-issue', + '', + '', + '', + '', + '>', + '', + '? for shortcuts', + `\t\t ${model}`, + '~/orca/workspaces/orca/agy-dispatch-issue', + '', + model, + ' (Antigravity Business)' + ].join('\n') +} + // Why: these runtime feature tests only need message-queue semantics; using // SQLite here makes them fail on unrelated better-sqlite3 native ABI drift. class InMemoryOrchestrationMessages { @@ -3005,6 +3035,91 @@ describe('OrcaRuntimeService', () => { }) }) + it('resolves tui-idle from an Antigravity ready prompt preview', async () => { + const runtime = new OrcaRuntimeService(store) + runtime.setPtyController({ + spawn: vi.fn().mockResolvedValue({ id: 'pty-bg' }), + write: () => true, + kill: () => true, + getForegroundProcess: async () => null + }) + const { handle } = await runtime.createTerminal(`path:${TEST_WORKTREE_PATH}`) + runtime.onPtyData('pty-bg', antigravityReadyScreen('Gemini 4 Experimental (High)'), Date.now()) + + await expect( + runtime.waitForTerminal(handle, { condition: 'tui-idle', timeoutMs: 1_000 }) + ).resolves.toMatchObject({ + handle, + condition: 'tui-idle', + status: 'running' + }) + }) + + it('resolves tui-idle from an Antigravity prompt before the model line', async () => { + const runtime = new OrcaRuntimeService(store) + runtime.setPtyController({ + spawn: vi.fn().mockResolvedValue({ id: 'pty-bg' }), + write: () => true, + kill: () => true, + getForegroundProcess: async () => null + }) + const { handle } = await runtime.createTerminal(`path:${TEST_WORKTREE_PATH}`) + runtime.onPtyData( + 'pty-bg', + [ + 'Do you trust this workspace directory?\n', + 'Press t to trust\n', + antigravityPromptBeforeModelReadyScreen('Gemini 3.5 Flash (High)') + ].join(''), + Date.now() + ) + + await expect( + runtime.waitForTerminal(handle, { condition: 'tui-idle', timeoutMs: 1_000 }) + ).resolves.toMatchObject({ + handle, + condition: 'tui-idle', + satisfied: true, + status: 'running' + }) + }) + + it('resolves live-leaf tui-idle from an Antigravity ready prompt preview', async () => { + const runtime = new OrcaRuntimeService(store) + runtime.attachWindow(1) + runtime.syncWindowGraph(1, { + tabs: [ + { + tabId: 'tab-1', + worktreeId: TEST_WORKTREE_ID, + title: 'Terminal', + activeLeafId: 'pane:1', + layout: null + } + ], + leaves: [ + { + tabId: 'tab-1', + worktreeId: TEST_WORKTREE_ID, + leafId: 'pane:1', + paneRuntimeId: 1, + ptyId: 'pty-1', + paneTitle: null + } + ] + }) + runtime.onPtyData('pty-1', antigravityReadyScreen(), Date.now()) + const [terminal] = (await runtime.listTerminals()).terminals + + await expect( + runtime.waitForTerminal(terminal.handle, { condition: 'tui-idle', timeoutMs: 1_000 }) + ).resolves.toMatchObject({ + handle: terminal.handle, + condition: 'tui-idle', + status: 'running' + }) + }) + it('resolves tui-idle from a Codex ready prompt even when stale startup lines remain', async () => { const runtime = new OrcaRuntimeService(store) runtime.setPtyController({ @@ -3042,6 +3157,36 @@ describe('OrcaRuntimeService', () => { }) }) + it('resolves tui-idle when a stale Codex prompt is followed by Antigravity readiness', async () => { + const runtime = new OrcaRuntimeService(store) + runtime.setPtyController({ + spawn: vi.fn().mockResolvedValue({ id: 'pty-bg' }), + write: () => true, + kill: () => true, + getForegroundProcess: async () => null + }) + const { handle } = await runtime.createTerminal(`path:${TEST_WORKTREE_PATH}`) + runtime.onPtyData( + 'pty-bg', + [ + 'Do you trust this workspace directory?\n', + 'Press t to trust\n', + antigravityReadyScreen(), + '\n' + ].join(''), + Date.now() + ) + + await expect( + runtime.waitForTerminal(handle, { condition: 'tui-idle', timeoutMs: 1_000 }) + ).resolves.toMatchObject({ + handle, + condition: 'tui-idle', + satisfied: true, + status: 'running' + }) + }) + it('resolves tui-idle when a stale Codex prompt is followed by the ready header', async () => { const runtime = new OrcaRuntimeService(store) runtime.setPtyController({ @@ -4013,6 +4158,229 @@ describe('OrcaRuntimeService', () => { await expect(runtime.isTerminalRunningAgent(handle)).resolves.toBe(true) }) + it('recognizes runtime-created Antigravity PTY handles from the ready prompt', async () => { + const runtime = new OrcaRuntimeService(store) + runtime.setPtyController({ + spawn: vi.fn().mockResolvedValue({ id: 'pty-bg' }), + write: () => true, + kill: () => true, + getForegroundProcess: async () => null + }) + runtime.attachWindow(1) + runtime.syncWindowGraph(1, { tabs: [], leaves: [] }) + const { handle } = await runtime.createTerminal(`path:${TEST_WORKTREE_PATH}`, { + command: 'agy', + title: 'worker' + }) + + runtime.onPtyData('pty-bg', antigravityReadyScreen(), 100) + + await expect(runtime.isTerminalRunningAgent(handle)).resolves.toBe(true) + }) + + it('recognizes Antigravity ready tails with the prompt before the model line', async () => { + const runtime = new OrcaRuntimeService(store) + runtime.setPtyController({ + spawn: vi.fn().mockResolvedValue({ id: 'pty-bg' }), + write: () => true, + kill: () => true, + getForegroundProcess: async () => null + }) + runtime.attachWindow(1) + runtime.syncWindowGraph(1, { tabs: [], leaves: [] }) + const { handle } = await runtime.createTerminal(`path:${TEST_WORKTREE_PATH}`, { + command: 'agy', + title: 'worker' + }) + + runtime.onPtyData('pty-bg', antigravityPromptBeforeModelReadyScreen(), 100) + + await expect(runtime.isTerminalRunningAgent(handle)).resolves.toBe(true) + }) + + it('recognizes live leaf Antigravity terminals from the ready prompt', async () => { + const runtime = new OrcaRuntimeService(store) + runtime.setPtyController({ + write: () => true, + kill: () => true, + getForegroundProcess: async () => null + }) + runtime.attachWindow(1) + runtime.syncWindowGraph(1, { + tabs: [ + { + tabId: 'tab-1', + worktreeId: TEST_WORKTREE_ID, + title: 'Terminal', + activeLeafId: 'pane:1', + layout: null + } + ], + leaves: [ + { + tabId: 'tab-1', + worktreeId: TEST_WORKTREE_ID, + leafId: 'pane:1', + paneRuntimeId: 1, + ptyId: 'pty-1', + paneTitle: null + } + ] + }) + runtime.onPtyData('pty-1', antigravityReadyScreen(), 100) + const [terminal] = (await runtime.listTerminals()).terminals + + await expect(runtime.isTerminalRunningAgent(terminal.handle)).resolves.toBe(true) + }) + + it('does not recognize partial Antigravity startup output as an agent', async () => { + const runtime = new OrcaRuntimeService(store) + runtime.setPtyController({ + spawn: vi.fn().mockResolvedValue({ id: 'pty-bg' }), + write: () => true, + kill: () => true, + getForegroundProcess: async () => null + }) + runtime.attachWindow(1) + runtime.syncWindowGraph(1, { tabs: [], leaves: [] }) + const { handle } = await runtime.createTerminal(`path:${TEST_WORKTREE_PATH}`, { + command: 'agy', + title: 'worker' + }) + + runtime.onPtyData( + 'pty-bg', + [ + 'Antigravity CLI 1.0.3', + 'user@example.com (Antigravity Business)', + 'Gemini 3.5 Flash (High)' + ].join('\n'), + 100 + ) + + await expect(runtime.isTerminalRunningAgent(handle)).resolves.toBe(false) + }) + + it('rejects a later Antigravity header with a prompt but no model line', async () => { + const runtime = new OrcaRuntimeService(store) + runtime.setPtyController({ + spawn: vi.fn().mockResolvedValue({ id: 'pty-bg' }), + write: () => true, + kill: () => true, + getForegroundProcess: async () => null + }) + runtime.attachWindow(1) + runtime.syncWindowGraph(1, { tabs: [], leaves: [] }) + const { handle } = await runtime.createTerminal(`path:${TEST_WORKTREE_PATH}`, { + command: 'agy', + title: 'worker' + }) + + runtime.onPtyData( + 'pty-bg', + [ + antigravityReadyScreen(), + '\nAntigravity CLI 1.0.4\n', + 'user@example.com (Antigravity Business)\n', + '~/orca/workspaces/orca/agy-dispatch-issue\n', + '>\n' + ].join(''), + 100 + ) + + await expect(runtime.isTerminalRunningAgent(handle)).resolves.toBe(false) + }) + + it('uses the latest Antigravity header when checking readiness', async () => { + const runtime = new OrcaRuntimeService(store) + runtime.setPtyController({ + spawn: vi.fn().mockResolvedValue({ id: 'pty-bg' }), + write: () => true, + kill: () => true, + getForegroundProcess: async () => null + }) + runtime.attachWindow(1) + runtime.syncWindowGraph(1, { tabs: [], leaves: [] }) + const { handle } = await runtime.createTerminal(`path:${TEST_WORKTREE_PATH}`, { + command: 'agy', + title: 'worker' + }) + + runtime.onPtyData( + 'pty-bg', + [ + antigravityReadyScreen(), + '\nAntigravity CLI 1.0.4\n', + 'user@example.com (Antigravity Business)\n', + 'Gemini 4 Experimental (High)\n', + 'Do you trust this workspace directory?\n' + ].join(''), + 100 + ) + + await expect(runtime.isTerminalRunningAgent(handle)).resolves.toBe(false) + }) + + it('recognizes Antigravity prompts written as the current partial line', async () => { + const runtime = new OrcaRuntimeService(store) + runtime.setPtyController({ + spawn: vi.fn().mockResolvedValue({ id: 'pty-bg' }), + write: () => true, + kill: () => true, + getForegroundProcess: async () => null + }) + runtime.attachWindow(1) + runtime.syncWindowGraph(1, { tabs: [], leaves: [] }) + const { handle } = await runtime.createTerminal(`path:${TEST_WORKTREE_PATH}`, { + command: 'agy', + title: 'worker' + }) + + runtime.onPtyData( + 'pty-bg', + [ + 'Antigravity CLI 1.0.3\n', + 'user@example.com (Antigravity Business)\n', + 'Gemini 3.5 Flash (High)\n', + '~/orca/workspaces/orca/agy-dispatch-issue\n' + ].join(''), + 100 + ) + runtime.onPtyData('pty-bg', ' > ', 101) + + await expect(runtime.isTerminalRunningAgent(handle)).resolves.toBe(true) + }) + + it('does not classify agy workspace paths or titles without the ready prompt', async () => { + const runtime = new OrcaRuntimeService(store) + runtime.attachWindow(1) + runtime.syncWindowGraph(1, { + tabs: [ + { + tabId: 'tab-1', + worktreeId: TEST_WORKTREE_ID, + title: '/tmp/agy-workspace', + activeLeafId: 'pane:1', + layout: null + } + ], + leaves: [ + { + tabId: 'tab-1', + worktreeId: TEST_WORKTREE_ID, + leafId: 'pane:1', + paneRuntimeId: 1, + ptyId: 'pty-1', + paneTitle: '/tmp/agy-workspace' + } + ] + }) + runtime.onPtyData('pty-1', 'cd /tmp/agy-workspace\n', 100) + const [terminal] = (await runtime.listTerminals()).terminals + + await expect(runtime.isTerminalRunningAgent(terminal.handle)).resolves.toBe(false) + }) + it('keeps mobile terminal surfaces visible while their leaf handle is pending', async () => { const runtime = new OrcaRuntimeService(store) runtime.attachWindow(1) diff --git a/src/main/runtime/orca-runtime.ts b/src/main/runtime/orca-runtime.ts index 3083a3f3a..f4806c941 100644 --- a/src/main/runtime/orca-runtime.ts +++ b/src/main/runtime/orca-runtime.ts @@ -4791,7 +4791,7 @@ export class OrcaRuntimeService { if ( condition === 'tui-idle' && (this.getAdoptedPtyExplicitIdleStatus(pty.pty) === 'idle' || - isCodexReadyPromptPreview(ptyWaitText)) + isKnownReadyPromptPreview(ptyWaitText)) ) { return buildPtyTerminalWaitResult(handle, condition, pty.pty) } @@ -4849,7 +4849,7 @@ export class OrcaRuntimeService { this.resolveWaiter(waiter, buildPtyTerminalWaitResult(handle, condition, live.pty)) } else if ( this.getAdoptedPtyExplicitIdleStatus(live.pty) === 'idle' || - isCodexReadyPromptPreview(livePtyWaitText) + isKnownReadyPromptPreview(livePtyWaitText) ) { this.resolveWaiter(waiter, buildPtyTerminalWaitResult(handle, condition, live.pty)) } else { @@ -4882,7 +4882,7 @@ export class OrcaRuntimeService { const fastPathTitle = leaf.paneTitle ?? this.tabs.get(leaf.tabId)?.title if ( (fastPathTitle && detectExplicitIdleStatusFromTitle(fastPathTitle) === 'idle') || - isCodexReadyPromptPreview(leafWaitText) + isKnownReadyPromptPreview(leafWaitText) ) { return buildTerminalWaitResult(handle, condition, leaf) } @@ -4954,13 +4954,13 @@ export class OrcaRuntimeService { // the first waiter consumes the status and all later ones see null. this.resolveWaiter(waiter, buildTerminalWaitResult(handle, condition, live.leaf)) } else { - // Why: renderer-synced previews can show Codex's ready prompt even + // Why: renderer-synced previews can show a known ready prompt even // while the last OSC title is still "working"; keep polling the // preview/title until the waiter resolves or hits its timeout. const fastPathTitle = live.leaf.paneTitle ?? this.tabs.get(live.leaf.tabId)?.title if ( (fastPathTitle && detectExplicitIdleStatusFromTitle(fastPathTitle) === 'idle') || - isCodexReadyPromptPreview(liveLeafWaitText) + isKnownReadyPromptPreview(liveLeafWaitText) ) { this.resolveWaiter(waiter, buildTerminalWaitResult(handle, condition, live.leaf)) } else { @@ -11067,9 +11067,9 @@ export class OrcaRuntimeService { // presence, but the runtime may not see PTY data for daemon-hosted terminals // (the daemon adapter stubs getForegroundProcess). This checks three signals // in order: (1) lastAgentStatus from PTY data OSC titles, (2) the renderer- - // synced tab title (which reflects OSC titles from the xterm instance), and - // (3) the PTY foreground process. Returns true if any signal indicates a - // non-shell agent is running. + // synced tab title (which reflects OSC titles from the xterm instance), (3) + // retained ready-tail text, and (4) the PTY foreground process. Returns true + // if any signal indicates a non-shell agent is running. async isTerminalRunningAgent(handle: string): Promise { try { const pty = this.getLivePtyForHandle(handle) @@ -11089,7 +11089,7 @@ export class OrcaRuntimeService { return true } const waitText = buildTerminalWaitText(leaf.tailBuffer, leaf.tailPartialLine, leaf.preview) - if (isCodexReadyPromptPreview(waitText)) { + if (isKnownReadyPromptPreview(waitText)) { return true } if (!leaf.ptyId || !this.ptyController) { @@ -11114,7 +11114,7 @@ export class OrcaRuntimeService { return true } const waitText = buildTerminalWaitText(pty.tailBuffer, pty.tailPartialLine, pty.preview) - if (isCodexReadyPromptPreview(waitText)) { + if (isKnownReadyPromptPreview(waitText)) { return true } if (!this.ptyController) { @@ -11558,7 +11558,7 @@ export class OrcaRuntimeService { ) return } - if (isCodexReadyPromptPreview(leafWaitText)) { + if (isKnownReadyPromptPreview(leafWaitText)) { if (waiter.pollInterval) { clearInterval(waiter.pollInterval) waiter.pollInterval = null @@ -11615,7 +11615,7 @@ export class OrcaRuntimeService { // Use that live xterm title as the same readiness signal as leaf handles. if ( this.getAdoptedPtyExplicitIdleStatus(pty) === 'idle' || - isCodexReadyPromptPreview(ptyWaitText) + isKnownReadyPromptPreview(ptyWaitText) ) { if (waiter.pollInterval) { clearInterval(waiter.pollInterval) @@ -12416,7 +12416,7 @@ function buildTerminalWaitText(lines: string[], partialLine: string, preview: st .filter(Boolean) .join('\n') // Why: the user-facing preview is intentionally short, but wait readiness - // needs the retained terminal tail so Codex headers are not truncated away. + // needs the retained terminal tail so known ready headers are not truncated away. return waitText.length > 0 ? waitText : preview } @@ -12694,9 +12694,9 @@ function detectExplicitIdleStatusFromTitle(title: string): AgentStatus | null { return null } -function isCodexReadyPromptPreview(preview: string): boolean { +function isKnownReadyPromptPreview(preview: string): boolean { const normalized = preview.toLowerCase() - const readyIndex = findCodexReadyPromptIndex(normalized) + const readyIndex = findKnownReadyPromptIndex(normalized) if (readyIndex === null) { return false } @@ -12713,15 +12713,23 @@ function detectTerminalWaitBlockedReason(preview: string): RuntimeTerminalWaitBl if (blockedSignal === null) { return null } - const readyIndex = findCodexReadyPromptIndex(normalized) - // Why: retained terminal tails can include stale startup modals. If Codex's - // ready header appears after that modal, the latest signal is ready. + const readyIndex = findKnownReadyPromptIndex(normalized) + // Why: retained terminal tails can include stale startup modals. If a known + // ready prompt appears after that modal, the latest signal is ready. if (readyIndex !== null && readyIndex > blockedSignal.index) { return null } return blockedSignal.reason } +function findKnownReadyPromptIndex(normalized: string): number | null { + const indexes = [ + findCodexReadyPromptIndex(normalized), + findAntigravityReadyPromptIndex(normalized) + ].filter((index): index is number => index !== null) + return indexes.length > 0 ? Math.max(...indexes) : null +} + function findCodexReadyPromptIndex(normalized: string): number | null { const headerIndex = normalized.lastIndexOf('openai codex') if (headerIndex === -1) { @@ -12733,6 +12741,34 @@ function findCodexReadyPromptIndex(normalized: string): number | null { return readySegment.includes('model:') && readySegment.includes('directory:') ? headerIndex : null } +function findAntigravityReadyPromptIndex(normalized: string): number | null { + const headerIndex = normalized.lastIndexOf('antigravity cli') + if (headerIndex === -1) { + return null + } + const readySegment = normalized.slice(headerIndex) + const lines = readySegment.split('\n') + let offset = 0 + let modelIndex: number | null = null + let promptIndex: number | null = null + + for (const line of lines) { + const trimmed = line.trim() + const lineIndex = headerIndex + offset + if (lineIndex > headerIndex && trimmed.length > 0) { + if (modelIndex === null && trimmed.startsWith('gemini')) { + modelIndex = lineIndex + line.indexOf(trimmed) + } + if (promptIndex === null && trimmed === '>') { + promptIndex = lineIndex + line.indexOf('>') + } + } + offset += line.length + 1 + } + + return modelIndex !== null && promptIndex !== null ? Math.max(modelIndex, promptIndex) : null +} + function findTerminalWaitBlockedSignal( normalized: string ): { reason: RuntimeTerminalWaitBlockedReason; index: number } | null {