diff --git a/src/main/runtime/orca-runtime-get-worktree-terminal-provisioning-host.ts b/src/main/runtime/orca-runtime-get-worktree-terminal-provisioning-host.ts index d9d6b2acfb93..10438e4d2efa 100644 --- a/src/main/runtime/orca-runtime-get-worktree-terminal-provisioning-host.ts +++ b/src/main/runtime/orca-runtime-get-worktree-terminal-provisioning-host.ts @@ -5,10 +5,14 @@ import type { WorktreeTerminalProvisioningHost } from './runtime-worktree-terminal-provisioning' import type { TerminalCreateOptions } from './runtime-terminal-contracts' -import type { WorktreeStartupReadinessHost } from './runtime-worktree-startup-readiness' +import { + waitForWorktreeStartupDraft, + type WorktreeStartupReadinessHost +} from './runtime-worktree-startup-readiness' import { prefetchWorktreeCreateBase } from '../worktree-create-base-prefetch' import { prepareWorktreeCreateForRepo } from '../worktree-create-preparation' import { getWorktreeCreatePrefetchGitOptions } from '../project-runtime-git-options' +import type { TuiAgent } from '../../shared/tui-agent' export class OrcaRuntimeWithGetWorktreeTerminalProvisioningHost extends OrcaRuntimeWithActivateManagedWorktree { protected getWorktreeTerminalProvisioningHost(): WorktreeTerminalProvisioningHost { @@ -40,6 +44,36 @@ export class OrcaRuntimeWithGetWorktreeTerminalProvisioningHost extends OrcaRunt } } + /** + * Waits for `agent`'s own composer-mount marker (the same PTY-output scanner that gates the + * editable "start workspace from issue" draft paste) instead of the generic tui-idle signal, + * which fires on OSC-title idle and can resolve while the TUI is still on its splash screen. + * `timeoutMs`, when given, caps the agent's own hard timeout (never extends it) so a caller + * with a short overall budget isn't blocked past it by this wait alone. + * + * Always resolves — including on a `getWorktreeStartupReadinessHost` failure (e.g. the PTY + * tore down between placement and this call) — the same "proceed anyway" contract + * `pasteWorktreeStartupDraftWhenReady` already relies on `.catch` for. Resolves `false` on + * any of those non-ready outcomes; callers should treat that as best effort, not a failure. + */ + async waitForAgentComposerReady( + handle: string, + agent: TuiAgent, + timeoutMs?: number + ): Promise { + try { + const ptyId = await waitForWorktreeStartupDraft( + this.getWorktreeStartupReadinessHost(), + handle, + agent, + timeoutMs + ) + return ptyId !== null + } catch { + return false + } + } + async prefetchManagedWorktreeCreateBase(args: { repoSelector: string baseBranch?: string diff --git a/src/main/runtime/rpc/methods/orchestration/worker/local-worker-start.ts b/src/main/runtime/rpc/methods/orchestration/worker/local-worker-start.ts index f8cd1033c97c..c025aa19767c 100644 --- a/src/main/runtime/rpc/methods/orchestration/worker/local-worker-start.ts +++ b/src/main/runtime/rpc/methods/orchestration/worker/local-worker-start.ts @@ -1,5 +1,7 @@ import type { OrcaRuntimeService } from '../../../../orca-runtime' import { describeTerminalWaitBlockedReason } from '../../../../../../shared/terminal-wait-blocked-reason-legacy-alias' +import { TUI_AGENT_CONFIG } from '../../../../../../shared/tui-agent-config' +import { resolveDraftPasteReadyTimeoutMs } from '../../../../../../shared/draft-paste-ready-timeout' import type { OrchestrationDb } from '../../../../orchestration/db' import type { RunRow, TaskRow } from '../../../../orchestration/types' import { resolveDispatchCreator } from '../runs/dispatch-creator' @@ -194,6 +196,27 @@ export async function startLocalWorker(args: { ) } } + // tui-idle fires on the generic OSC-title idle edge. Agents that enable bracketed paste + // before their composer actually mounts (opencode, mimo-code) can hit that edge while still + // on the boot splash, so the preamble below lands on a screen with no composer to catch it + // and is silently dropped. Wait for the agent's own composer-mount marker on top of + // tui-idle for just those agents — Codex has its own composer-wait fix in flight upstream, + // and structured/other agents are unaffected. + if ( + !structuredSession && + agent && + TUI_AGENT_CONFIG[agent].draftPasteReadySignal === 'render-cursor-after-bracketed-paste' + ) { + // Cap by (never extend past) the caller's own budget, so an explicit short + // --timeout-ms still bounds total dispatch latency the way it does for every other + // wait above. waitForAgentComposerReady never throws — a failure here is "proceed + // anyway", the same best-effort contract the pre-existing draft-paste caller relies on. + await runtime.waitForAgentComposerReady( + terminalHandle, + agent, + Math.min(params.timeoutMs ?? 60_000, resolveDraftPasteReadyTimeoutMs(agent)) + ) + } const terminalAuthority = requireWorkerAuthority(runtime, terminalHandle) const capability = db.prepareStartingWorkerAuthority({ dispatchId: started.dispatch.id, diff --git a/src/main/runtime/rpc/methods/orchestration/worker/worker-start-opencode-composer-wait.test.ts b/src/main/runtime/rpc/methods/orchestration/worker/worker-start-opencode-composer-wait.test.ts new file mode 100644 index 000000000000..d82eef65c024 --- /dev/null +++ b/src/main/runtime/rpc/methods/orchestration/worker/worker-start-opencode-composer-wait.test.ts @@ -0,0 +1,112 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { createOrchestrationWorkerReleaseHarness } from './worker-release.test-support' +import { resolveDraftPasteReadyTimeoutMs } from '../../../../../../shared/draft-paste-ready-timeout' + +const OPENCODE_DEFAULT_COMPOSER_TIMEOUT_MS = resolveDraftPasteReadyTimeoutMs('opencode') + +// tui-idle fires on the generic OSC-title idle edge. opencode (and mimo-code, which shares its +// config) enable bracketed paste before their composer actually mounts, so tui-idle can resolve +// on their boot splash: worker-start then pastes the dispatch preamble into a screen with no +// composer to catch it, and the preamble is silently dropped even though the receipt reports +// `input_accepted` / `ready`. This asserts worker-start waits for opencode's own composer-mount +// marker (`waitForAgentComposerReady`, reusing the existing render-cursor-after-bracketed-paste +// detector) before writing the preamble, and that unrelated agents are unaffected. +describe('worker-start composer readiness for opencode', () => { + const harness = createOrchestrationWorkerReleaseHarness() + beforeEach(() => harness.setup()) + afterEach(() => harness.cleanup()) + + it('waits for the opencode composer marker before delivering the preamble', async () => { + const composerWait = vi + .spyOn(harness.runtime, 'waitForAgentComposerReady') + .mockResolvedValue(true) + const sends: string[] = [] + vi.spyOn(harness.runtime, 'sendTerminalAgentPrompt').mockImplementation(async () => { + sends.push('sendTerminalAgentPrompt') + return { handle: 'term_worker', accepted: true, bytesWritten: 1 } + }) + composerWait.mockImplementation(async () => { + sends.push('waitForAgentComposerReady') + return true + }) + + const task = harness.db.createTask({ + spec: 'opencode composer fixture', + runId: harness.activeRunId + }) + const result = (await harness.call('orchestration.workerStart', { + task: task.id, + from: 'term_coord', + agent: 'opencode' + })) as { state: string } + + expect(result.state).toBe('ready') + expect(composerWait).toHaveBeenCalledWith( + 'term_worker', + 'opencode', + OPENCODE_DEFAULT_COMPOSER_TIMEOUT_MS + ) + expect(sends).toEqual(['waitForAgentComposerReady', 'sendTerminalAgentPrompt']) + }) + + it('caps the composer wait at an explicit --timeout-ms shorter than the default', async () => { + const composerWait = vi + .spyOn(harness.runtime, 'waitForAgentComposerReady') + .mockResolvedValue(true) + + const task = harness.db.createTask({ + spec: 'opencode short timeout fixture', + runId: harness.activeRunId + }) + await harness.call('orchestration.workerStart', { + task: task.id, + from: 'term_coord', + agent: 'opencode', + timeoutMs: 2_000 + }) + + expect(composerWait).toHaveBeenCalledWith('term_worker', 'opencode', 2_000) + }) + + it('proceeds anyway when the opencode composer marker times out', async () => { + vi.spyOn(harness.runtime, 'waitForAgentComposerReady').mockResolvedValue(false) + + const task = harness.db.createTask({ + spec: 'opencode timeout fixture', + runId: harness.activeRunId + }) + const result = (await harness.call('orchestration.workerStart', { + task: task.id, + from: 'term_coord', + agent: 'opencode' + })) as { state: string } + + expect(result.state).toBe('ready') + }) + + it('does not gate an unrelated agent (codex) on the opencode composer marker', async () => { + const composerWait = vi + .spyOn(harness.runtime, 'waitForAgentComposerReady') + .mockResolvedValue(true) + + const task = harness.db.createTask({ spec: 'codex fixture', runId: harness.activeRunId }) + const result = (await harness.call('orchestration.workerStart', { + task: task.id, + from: 'term_coord', + agent: 'codex' + })) as { state: string } + + expect(result.state).toBe('ready') + expect(composerWait).not.toHaveBeenCalled() + }) + + it('does not gate a reused --terminal (no --agent, e.g. a pre-warmed handle)', async () => { + const composerWait = vi + .spyOn(harness.runtime, 'waitForAgentComposerReady') + .mockResolvedValue(true) + + await harness.startWorker({ terminal: 'term_worker' }) + + expect(composerWait).not.toHaveBeenCalled() + }) +}) diff --git a/src/main/runtime/runtime-worktree-startup-readiness.ts b/src/main/runtime/runtime-worktree-startup-readiness.ts index 77b4e1315db7..a4d18adec29a 100644 --- a/src/main/runtime/runtime-worktree-startup-readiness.ts +++ b/src/main/runtime/runtime-worktree-startup-readiness.ts @@ -89,7 +89,8 @@ export async function waitForWorktreeStartupFollowup( export function waitForWorktreeStartupDraft( host: WorktreeStartupReadinessHost, handle: string, - agent: TuiAgent + agent: TuiAgent, + timeoutOverrideMs?: number ): Promise { const ptyId = host.getPtyId(handle) if (!ptyId) { @@ -134,6 +135,9 @@ export function waitForWorktreeStartupDraft( if (replay) { observe(replay) } - hardTimer = setTimeout(() => finish(null), resolveDraftPasteReadyTimeoutMs(agent)) + hardTimer = setTimeout( + () => finish(null), + resolveDraftPasteReadyTimeoutMs(agent, timeoutOverrideMs) + ) }) }