Skip to content

fix(terminal): one send owns the terminal's input until its last byte - #26676

Draft
brennanb2025 wants to merge 2 commits into
mainfrom
brennanb2025/control-channel-admission
Draft

brennanb2025 wants to merge 2 commits into
mainfrom
brennanb2025/control-channel-admission

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 17 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​1349 $\color{#cf222e}{\Huge{\mathbf{−}}}$​27 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​1322
Prod 30 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​1382 $\color{#cf222e}{\Huge{\mathbf{−}}}$​405 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​977

ELI5

When two things type into the same terminal at the same moment, Orca could shuffle their keystrokes together. Two orca terminal send --text "<command>" --enter calls to one shell could come out as one merged line, commandAcommandB, followed by two Enters. The shell then ran a command nobody asked for. Now each send has the terminal to itself from its first character through its Enter, and the next writer waits its turn. Your own typing still always gets through: a send can only hold the terminal for a bounded time, and Ctrl-C always cuts in.

What Changed

Before: a single terminal send is several separate writes: the text (split into chunks for large pastes), a pause so the terminal can take in the text, then Enter. Nothing kept the terminal reserved across those steps. A second send, a keystroke from the desktop, input from a paired phone or desktop, or an agent prompt could land between one send's text and its Enter. The visible result was two commands merged into one line, or a keystroke inserted into the middle of a command.

After:

  • Two sends to the same terminal arrive as two separate commands, in order.
  • A keystroke you type while a send is in progress lands after that send's Enter, not inside its text.
  • Ctrl-C (from the desktop or a paired client) jumps the queue. The send in progress stops before its remaining bytes (for example its Enter) and reports that it was only partly written. Sends that were already waiting keep their order behind the Ctrl-C.
  • If a send's write gets stuck, for example because the terminal host never acknowledges it, the send is abandoned once its time limit passes. The limit covers its writes, its computed paste-ingest delay and a fixed one-second margin. It can never write another byte. The queue moves on, so your typing goes through, and the stuck send reports its own failure: "partially written, outcome unknown" if any bytes went out, or "no effect" if none did.
  • A send that was still waiting when its caller gave up is removed with no effect. Once a plain send's first byte has been written, it finishes its own Enter even if the CLI that asked for it disconnects. Text left in a shell without its Enter would otherwise get glued onto the front of the next command, which is the same bug.
  • Different terminals never wait on each other.
  • No UI change. Error results use the existing write outcomes (partial_write / unverifiable, refused).

Mechanism: a new per-terminal input queue in the main process (src/main/runtime/pty-input-transactions.ts, built on keyed-operation-queue.ts). It is keyed by the PTY id plus the PTY's incarnation, which src/main/ipc/pty/provider/input-binding.ts binds from the provider that owns the PTY. A respawned PTY that reuses an id therefore never waits behind, or receives, input meant for the old process. Every writer of terminal input on this host goes through this one queue:

  • runtime terminal send (CLI, mobile, paired multiplex input), via RuntimeTerminalWriter
  • desktop keystrokes via pty:write / pty:writeAccepted
  • dashboard terminal preview input
  • agent prompt paste, submit and resubmit
  • startup commands (local provider shell-ready barrier, worktree startup drafts, recovered bare-renderer startup)
  • orchestration mailbox pointers

The queue keeps the existing "previous entry settles, failure still releases, remove the tail only by identity" pattern. The agent-prompt per-generation queue now reuses the same queue implementation instead of its own copy. When the terminal is idle, a keystroke is written synchronously exactly as before, with no added latency. Every write boundary rechecks the binding: the same PTY incarnation is still current, the transaction has not been preempted, and it is still within its time limit. Terminal query replies (the bytes a terminal answers its own status queries with) keep a narrow bypass so terminal negotiation can't deadlock behind a waiting prompt.

Agent prompts keep their existing per-generation turn reservation. Only the bytes (paste, ingest wait, submit, resubmit) hold the input queue. Checking that the agent actually started a turn now runs after the queue is released, so waiting on an agent never blocks your typing.

Why

The bug is that a send's ownership of the terminal did not span its own writes. Making individual writes ordered can't fix that, because text and Enter are separate writes by design: ConPTY chunking, paste ingest delays, and the permission and generation checks between writes. The fix is one operation queue per terminal on the host that writes the PTY: each input message is written as one unit after the previous operation settles, a failed predecessor still releases the queue, and only the matching tail is removed. Here the unit is a transaction covering all of one send's writes.

Alternatives considered:

  • A lock only around terminal send: rejected. Desktop typing, paired input and agent prompts would still land inside a send.
  • Merging text and Enter into one write: rejected. It breaks large-paste ingestion on Windows ConPTY and the agent composers that need the pause before Enter.
  • Holding the input queue through agent turn verification: rejected. Waiting on an agent must never block user input.

Design choices:

  • INTENDED: the unit of ownership is a multi-write transaction, not a single write. Orca must keep chunking, ingest delays and the per-write permission and generation checks. The transaction holds the terminal only through its last byte.
  • INTENDED: a bounded hold plus Ctrl-C preemption. Orca's queue carries the user's own keystrokes, so a stalled send must never hold them indefinitely. At expiry, a write already handed to the provider is left to the provider and no later byte from that send is written.
  • INTENDED: two queues for agent prompts (semantic turn order per lifecycle generation, plus byte ownership), built on one queue implementation, so turn observation never holds input.
  • INTENDED: terminal query replies bypass the queue (only recognized query-reply bytes, not anything merely labelled as one), so terminal negotiation cannot deadlock.
  • TEMPORARY: for SSH terminals, every writer inside one desktop is ordered, because they all share this queue before the existing relay frames. Two different desktops typing into the same remote terminal are not ordered against each other. That needs a capability-negotiated transaction on the relay, which is the next PR in this series.
  • TEMPORARY: agent-prompt cancellation semantics are unchanged: a prompt aborted between paste and submit still stops, as it does today. A follow-up will audit host-owned completion for prompts.
  • TEMPORARY: typed mailbox pointers queue each input frame separately. Their deferred-until-idle Enter is not grouped with the paste, because holding input until the agent goes idle would block the user. A mailbox-delivery follow-up will group them under a bounded hold.

Linked Issue

N/A (maintainer root-cause series: control-channel admission, part 1). Related stopgap: #26549.

Visual Proof

N/A. This is a main-process input-ordering change with no UI. Proof is the deterministic ordering tests below. A live run that sends to a shell from two CLIs at once will be added from QA.

Testing

  • New: pty-input-transactions.test.ts, pty-input-bounded-hold.test.ts, runtime-terminal-input-transactions.test.ts, runtime-terminal-input-ownership.test.ts, input-binding.test.ts. They cover:

    • A paused after its text while B and a desktop keystroke queue: the final bytes are A-text, A-Enter, then the keystroke and B, through the real sendTerminal and pty:write paths.
    • Different PTYs run concurrently.
    • A failed or refused predecessor releases the queue, and the map is empty afterwards.
    • Abort while queued writes nothing; abort after the first byte still writes the Enter.
    • Ctrl-C mid-send drops that send's Enter and reports a partial write; queued sends still run.
    • A replacement PTY incarnation doesn't wait behind the old one.
    • A stalled provider write: the user's keystroke lands at the send's time limit, the stalled send reports its own failure, and a late acknowledgment writes nothing more.
    • A 320 KB Windows send is not abandoned before its computed ingest delay.
    • An agent prompt releases input before turn verification.
  • Two existing tests updated to the new contract: an abort after the text now still writes Enter, and a refused Enter after accepted text is reported as partial_write.

  • Targeted vitest (18 files, 194 tests) passed on this head. tsc for the node project and the repo's static CI checks passed locally.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

Review

Review summary will be posted as a comment.

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • Cross-platform: the time limit uses the same per-host ingest delay as before, so a Windows ConPTY host gets its larger delay. No platform-specific code was added.
  • SSH: ordering is per desktop; cross-desktop ordering is the TEMPORARY item above. The execution host still owns the PTY, and nothing falls back to local execution.
  • Mobile/paired clients: their input goes through the same per-terminal queue, and Ctrl-C from them preempts as well.
  • Compatibility: no RPC, stream or relay wire change.
  • Performance: idle keystrokes stay synchronous. Queued writes drain in a loop, not recursively.

Author: @BrennanKB5

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

Two sends to one shell could interleave as A-text, B-text, A-Enter, B-Enter and
run as one merged command. Every writer of a PTY's input on this host (terminal
send, desktop keystrokes, paired/mobile input, agent prompt paste/submit,
startup commands, mailbox pointers) now goes through one per-PTY, per-incarnation
input queue; a send holds it from its first chunk through its Enter, under a
bounded hold so a stalled send can never keep the user's typing waiting.
Ctrl-C preempts the remaining bytes of a send in progress.
Review round 1: a wall-clock limit on a whole send could fire on healthy work
and drop the Enter after the text, recreating merged commands. Replace it with a
per-await leak backstop above the providers' own acknowledgment limits, make
Ctrl-C stop a send's waits immediately without reordering the user's earlier
keys, keep a prompt accepted after its first Enter, let the transaction own its
writes, route writers that don't opt in through the same queue, and key the
queue by terminal id.

This branch has not been deployed

No deployments
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.

1 participant