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
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -40,6 +44,22 @@ 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.
* Resolves `false` on the agent's own hard timeout — callers should treat that as "proceed
* anyway" (best effort), the same way the draft-paste path does, not as a hard failure.
*/
async waitForAgentComposerReady(handle: string, agent: TuiAgent): Promise<boolean> {
const ptyId = await waitForWorktreeStartupDraft(
this.getWorktreeStartupReadinessHost(),
handle,
agent
)
return ptyId !== null
}

async prefetchManagedWorktreeCreateBase(args: {
repoSelector: string
baseBranch?: string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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 type { OrchestrationDb } from '../../../../orchestration/db'
import type { RunRow, TaskRow } from '../../../../orchestration/types'
import { resolveDispatchCreator } from '../runs/dispatch-creator'
Expand Down Expand Up @@ -194,6 +195,19 @@ 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'
) {
await runtime.waitForAgentComposerReady(terminalHandle, agent)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The returned boolean is dropped, so a composer timeout — which is exactly the opencode race this PR fixes — still leaves no trace beyond an ordinary state=ready receipt. Consider logging a warning (the draft-paste path does this in pasteWorktreeStartupDraftWhenReady) or folding the value into the receipt, so a timed-out paste is distinguishable from a landed one.

}
const terminalAuthority = requireWorkerAuthority(runtime, terminalHandle)
const capability = db.prepareStartingWorkerAuthority({
dispatchId: started.dispatch.id,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { createOrchestrationWorkerReleaseHarness } from './worker-release.test-support'

// 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')
expect(sends).toEqual(['waitForAgentComposerReady', 'sendTerminalAgentPrompt'])
})

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')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test only pins that a timeout doesn't block the start; it would still pass if the code skipped the preamble after waitForAgentComposerReady returned false, which is the "proceed anyway" behavior the wrapper JSDoc promises. Assert that sendTerminalAgentPrompt was still called — e.g. the sends ordering pattern from the first test — to actually cover best-effort delivery.

})

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()
})
Comment on lines +19 to +111

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Add mimo-code coverage to the composer gate tests

opencode and mimo-code both use promptInjectionMode: 'flag-prompt' and draftPasteReadySignal: 'render-cursor-after-bracketed-paste', so both reach the production composer gate. The tests cover only opencode. Parameterize the ordering and timeout tests over opencode and mimo-code, including the expected waitForAgentComposerReady argument. Otherwise, a regression in the mimo-code path can pass these tests.

})
Loading