Skip to content

fix(orchestration): wait for Codex composer before paste - #14888

Closed
bbingz wants to merge 4 commits into
stablyai:mainfrom
bbingz:fix/9976-codex-composer-before-paste
Closed

bbingz wants to merge 4 commits into
stablyai:mainfrom
bbingz:fix/9976-codex-composer-before-paste

Conversation

@bbingz

@bbingz bbingz commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

Codex can turn on bracketed paste before its composer is actually ready. Orca now waits for that composer signal, then pastes. If the PTY dies while waiting, the send fails instead of writing into a dead session.

What Changed

  • Before writing a Codex prompt, wait for the composer-ready PTY signal (?2004h, alt-screen, bold ›).
  • Abort the wait immediately if the PTY exits; re-check generation and writability after the wait.
  • Honor options.signal inside the composer wait on both prompt paths, and dispose data/exit listeners plus quiet/hard timers on abort (including pre-aborted, buffered-ready, and PTY-exit races). Aborted waits reject request_aborted instead of waiting out the 20s timeout as agent_composer_not_ready.
  • Dispose subscriptions correctly even when subscribing to an already-exited PTY invokes the exit callback synchronously; no abort listener is registered after settlement.
  • Call the existing agentSessionPtyWriteGate.assertAdmitted preflight on both sendTerminalAgentPrompt paths before the composer wait. Native/Structured Chat can own the pane with no composer; waiting first hid the typed ownership refusal as agent_composer_not_ready. Per-write assertAdmitted / assertReadmitted still run at write time.
  • Existing post-paste Enter gating, cancellation, generation, and permission assertions stay.

Why

#14575 waits for Codex to redraw after paste and before Enter. That protects submit timing, but an MCP-heavy startup can redraw before that point and erase an early paste entirely. Waiting for the strong composer signal before paste closes that earlier race without changing the remote wire contract.

Linked Issue

Partially addresses #9976 (the Codex input-missing startup race; this PR does not implement the issue's broader delivery-state or first-turn deadline contract).

Visual Proof

N/A — no visual redesign. Behavior is covered by deterministic PTY/runtime tests.

Testing

Validation is tied to c610ce71d5673bb1d2bfdabba52e1ef5d1b6838b, based on main 8f78c28248fbfa4d55fb837ab6698ac77d4e35c5.

  • Independent final-SHA Vitest: 10 files passed, 1,335 tests passed / 1 skipped, including the complete orca-runtime.test.ts umbrella, all 12 delivery-receipt tests, Structured Chat refusal/lease-transfer cases, and composer readiness/cancellation/cleanup regressions.
  • CLI/Node/Web direct typechecks with --composite false, all 13 changed-file lint/format checks, native dependency preflight, and diff whitespace checks passed.
  • Full non-Docker suite on ancestor 3bf2a31f60: 8,241 files passed / 14 failed / 61 skipped; 76,586 tests passed / 41 failed / 391 skipped, 836.47 seconds. All 27 known clean-main failure names match. Thirteen additional runtime failures led to the final pre-wait ownership check and explicitly scoped fixture inputs described below; all pass in the final-SHA run. One cross-version checkout import timed out and produced a subsequent lock-file exception. The unchanged checkout file passed all 10 tests in separate candidate and clean-main reruns with no unhandled error; the full-load failure was not reproduced.
  • The final delta from the full-tested ancestor is one production file (the existing write-admission preflight before both composer waits) and four test files. The entire affected runtime umbrella and changed tests were rerun on the final SHA. A second full-suite run was not performed; this does not claim a fully green final-SHA full suite.
  • Physical Codex TUI, Windows/WSL/SSH smoke tests and production build were not run. No rendered UI change.

Existing-test Codex readiness seeds (declared): these cases already launched codex and would now fail agent_composer_not_ready before their original assertions. Each was given \x1b[?2004h\x1b[?1049h\x1b[1m›\x1b[0m on pty-prompt / pty-bg / the Structured Chat worker PTY only; assertions were not weakened.

  • agent-prompt-submission-runtime.test.ts: queued receipt with existing-turn output; hook-only oldest queued receipt; one lifecycle reservation.
  • agent-prompt-receipt-correlation.test.ts: FIFO historical lifecycle edges.
  • worker-start-prompt-contract.test.ts: Codex harness (accepted/swallowed worker-start) and the busy-turn queued send.
  • terminal-creation-and-readiness-part-07.spec.ts: Codex branch of composer-frame settle-before-submit.
  • terminal-creation-and-readiness-part-08.spec.ts: late Codex render marker / fresh quiescence, and the foreground-detected Codex settle case (createTerminal without launchAgent; getForegroundProcess returns codex). Original post-paste Enter-gating assertions stay.
  • orchestration-structured-chat-lease.test.ts: only stops a prompt when Structured Chat takes the lease between paste chunks. That case starts as TUI, so preflight admits; Codex launch identity still waits for composer before the first paste chunk. The two native-owner refusal tests (fails worker-start truthfully…, reports a real gate refusal…) are not seeded — they must keep failing immediately with typed agent_session_conflict / zero bytes, not after a 20s composer wait.
  • rpc/terminal-prompt-delivery-receipt.test.ts: file-local createHarness(agent, busy) seeds only when agent === 'codex'. Claude and aider harnesses stay unseeded. Every receipt/dedup/queue/draft assertion is unchanged. This is a fixture adaptation: the harness already launched Codex and replaced the write controller, but never emitted composer-ready bytes, so the new pre-paste wait starved paste/Enter and the original receipt assertions never ran.

createAgentPromptSubmissionRuntime itself is not auto-seeded, so the new wait-before-write, timeout/exit, abort-during-wait, and native-owner preflight tests still observe an empty write list.

Independent review cases in orca-runtime-composer-cancellation-review.test.ts were copied with unused-import/format cleanup only; both it.each abort assertions (failure defined quickly, writes === [], still request_aborted after 20s) are unchanged.

Review

Cursor exact-SHA review PASS on c610ce71d5, followed by independent Codex source review and the final checks above. The two native-owner refusal tests remain unseeded and preserve their typed refusal and zero-byte assertions. The shared prompt fixture remains unseeded, preserving readiness timeout, exit and cancellation coverage. Existing permission, generation, Enter-gating, receipt and replay assertions remain.

Compatibility and Security

Send-path only; waitForWorktreeStartupDraft is unchanged. Non-Codex agents skip the wait. Remote/SSH PTYs use the same composer signal. No new RPC method or stream opcode.

AI Disclosure

Cursor ported and reviewed the rebase onto 8f78c28. Codex independently reviewed the candidate, supplied cancellation/cleanup regression cases, and ran final checks.

@coderabbitai

coderabbitai Bot commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: 8ac847bb-34c9-4c94-aa4f-ea245ffed501

📥 Commits

Reviewing files that changed from the base of the PR and between c610ce71d5673bb1d2bfdabba52e1ef5d1b6838b and 4993842.

📒 Files selected for processing (3)
  • src/main/runtime/orca-runtime-controller-knows-pty-is-live.ts
  • src/main/runtime/orca-runtime-tests/terminal-creation-and-readiness-part-07.spec.ts
  • src/main/runtime/rpc/methods/orchestration/worker/worker-start-prompt-contract.test.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The runtime adds Codex composer-readiness detection for PTY draft input. Prompt submission waits for readiness, handles cancellation and timeout errors, and revalidates PTY lifecycle and identity before writing. New tests cover readiness scanning, cleanup, terminal exit, generation changes, native-runtime ownership, and cancellation. Existing prompt-flow tests now emit Codex terminal readiness sequences.

Priority: ➖ Normal

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 13 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary change: waiting for the Codex composer before pasting.
Description check ✅ Passed The description is comprehensive and covers the user impact, implementation, rationale, linked issue, testing, compatibility, security, and AI disclosure. It includes the required visual-proof explana…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 4047e966-7f72-453c-8d6f-de61c4a3e189

📥 Commits

Reviewing files that changed from the base of the PR and between e4e54a1 and af5fd8b7f262cbc290de5c05208c75ef2bbe1d1d.

📒 Files selected for processing (2)
  • src/main/runtime/orca-runtime.test.ts
  • src/main/runtime/orca-runtime.ts

Included review availability: Your plan includes up to 10 reviews per rolling hour; 8 remain after this review.

Comment thread src/main/runtime/orca-runtime.ts Resolved
@coderabbitai

coderabbitai Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@bbingz

bbingz commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Refreshed onto origin/main@b2612de157d58320f2b90fa319d04c4f8bbab2cd.

Review is bound to 653becd23e40e78214b55ec8624c9d5839cbb3e9:

  • conflict resolution preserves main's serialized prompt submission, lifecycle-generation checks, abort signal, and post-submit verification; the Codex composer-ready gate remains before any prompt bytes;
  • existing-test change rationale: Codex prompt tests now seed composer readiness, and the new readiness test emits main's submit acknowledgement so it reaches its original single-submit assertions; no assertion was deleted and the repaired function is not mocked;
  • focused runtime suite: 1,167 passed, 1 skipped;
  • pnpm typecheck, targeted oxlint, oxfmt, and git diff --check: passed;
  • full Vitest: 54,578 passed, 183 skipped, 10 failed only in two untouched renderer IME/xterm files; isolated rerun reproduced the same 10 failures, outside this PR's two-file diff.

External rereview was not rerun in this bounded patrol; this is a non-security change, so the SHA-bound source review and checks above are the publication gate.

@nwparker nwparker removed the P1 label Aug 19, 2026
@bbingz
bbingz force-pushed the fix/9976-codex-composer-before-paste branch from 653becd to c610ce7 Compare September 8, 2026 19:37
@coderabbitai

coderabbitai Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

bbingz and others added 4 commits September 20, 2026 18:57
Codex can enable bracketed paste before the composer mounts. Wait for
the composer-ready signal, abort if the PTY exits, and seed existing
Codex send cases so the wait does not invent a timeout failure.

Co-authored-by: Cursor <[email protected]>
Already-aborted and mid-wait Codex prompts ignored options.signal and failed as
agent_composer_not_ready after 20s. Reject request_aborted on both send paths
and dispose wait listeners/timers instead of racing the promise.

Co-authored-by: Cursor <[email protected]>
subscribeToPtyExit invokes the listener immediately for an already-exited PTY,
which settled the wait before unsubscribeExit was assigned and then registered
an abort listener after reject. Bind listeners first, then clean up if the
subscribe already settled.

Co-authored-by: Cursor <[email protected]>
Native/Structured Chat can own a pane with no composer; waiting first
turned typed refusals into agent_composer_not_ready. Existing Codex
receipt and settle fixtures seed readiness only.

Co-authored-by: Cursor <[email protected]>
@bbingz
bbingz force-pushed the fix/9976-codex-composer-before-paste branch from c610ce7 to 4993842 Compare September 20, 2026 10:57
@bbingz

bbingz commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

Closing as superseded by #23745, which waits for Codex's composer on worker-start and agent.launch (together with #24257 and #25779). One gap remains in case a follow-up is wanted: generic dispatch --inject and terminal send still paste without that wait.

@bbingz bbingz closed this Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants