Skip to content

refactor(terminal): one remote input hold - #27781

Merged
OrcaWin merged 3 commits into
mainfrom
OrcaWin/p399-2-5
Oct 12, 2026
Merged

OrcaWin merged 3 commits into
mainfrom
OrcaWin/p399-2-5

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​219 $\color{#cf222e}{\Huge{\mathbf{−}}}$​195 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​24
Prod 5 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​362 $\color{#cf222e}{\Huge{\mathbf{−}}}$​376 $\color{#cf222e}{\Huge{\mathbf{−}}}$​14

ELI5

When Orca reopens a remote terminal it first paints the old screen, and you can start typing before the terminal is reconnected. Two separate pieces of code used to hold those keystrokes: one for "before the first connect finishes" and one for "while reconnecting". This PR keeps only the second one and teaches it the first one's job. Typing behaves the same; there is one less layer.

What Changed

  • Before: remote-reattach-input-buffer.ts wrapped every remote terminal connection. It buffered typing from the moment the terminal was created, kept it while the first connect ran, and sent it only if that connect came back attached to the same remote terminal id. Inside the transport, a second buffer (the recovery input hold) did the same job during reconnects.
  • After: the wrapper is gone. The recovery input hold now starts in an "unbound" state when the transport is created, so typing on a restored screen is held from the first keystroke. At connect it is bound to the terminal the connect is restoring. That is only when the requested id is this transport's own remote:<env>@@<handle> id. A fresh create, an id from another server or an old id without an owner drops it, as before. When the connect ends it is either kept for delivery, or dropped if no terminal bound. attach, disconnect, detach and destroy drop it, as before. Delivery uses the hold's existing endpoint check: typing aimed at one terminal is never sent to a replacement terminal (fix(daemon): respawn on PTY write dropped to a dead daemon socket (STA-2373) #10065).
  • The setConnectForRecovery hook on PtyTransport existed only so the wrapper could intercept internal recovery connects. It is removed. Recovery connects now call the same holding connect directly.
  • One small timing difference: restored typing is now sent once the reattached stream has finished attaching (the host's subscribed reply and first screen snapshot). Typing held during a reconnect already waited for that point. The wrapper sent it a moment earlier, when the connect call returned. The keystrokes, their order and which terminal they reach do not change.

Why

There were two buffers with overlapping jobs and different rules: the wrapper compared terminal ids, while the hold compared endpoints. That meant two places to get the "never type into the wrong shell" rule right. Merging them into the hold, the one that already handles recovery, removes the wrapper and the setConnectForRecovery indirection. The reviewed design had a simpler version: seed the hold only at connect time. It was rejected because that drops typing made before connect starts. That is why the hold starts unbound when the transport is created and binds at the first connect.

Net lines: production −13 (wrapper −90, transport +16, hold +63, transport types −2), tests +24 (wrapper suite −150, transport suite +158, hold suite +16). The tests grew because they now run against the real transport, and one new case covers typing after the host binds a terminal but before its first snapshot arrives. Production is smaller, which is the condition the design set for keeping this PR. The transport diff looks large because the connect body moved out of the object literal and was re-indented. Use ?w=1 to review it.

Dead-code proof: rg "withRemoteReattachInputBuffer|remote-reattach-input-buffer|setConnectForRecovery" over src config tests mobile scripts .github finds no match. The @ts-nocheck files do not reference either symbol. pnpm run build:electron-vite passes.

Linked Issue

Phase 3.99 cleanup (item 2.5). Maintainer PR, no issue.

Visual Proof

N/A. There is no UI change, and the same keystrokes reach the same terminal.

Testing

  • I manually tested these changes locally

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

  • The wrapper's cases now run against the real transport (remote-runtime-pty-transport-reattach-type-ahead.test.ts):

    • typing before connect and during a reattach, including an acknowledged Ctrl+C, is delivered in order;
    • a fresh create, another server's id or an owner-less id drops it;
    • a replacement terminal or a rejected create never receives it, and query replies are not held;
    • attach, disconnect, detach and destroy settle pending acknowledged writes as undelivered.
  • New transport case (from review): typing between the host binding a terminal and that terminal's first snapshot reaches it only if it is the restored one. A replacement terminal never receives it.

  • New hold unit test: an older connect that was superseded cannot drop the newer connect's held typing.

  • Mutation check: disabling the unbound start, binding to the wrong endpoint, or skipping the drop each makes these specs fail.

  • pnpm test on all remote-runtime*, pty-preconnect* and pty-connection* specs: 128 files, 1079 tests pass.

  • Also pass: pnpm tc:web, oxlint, pnpm run check:code-quality:changed, pnpm lint, pnpm run build:electron-vite.

  • Platforms: renderer-only logic with no platform branches. SSH-managed hosts use this same remote runtime transport, so the reattach behaviour is the same for them.

AI Disclosure

Review

Reviewed for P0/P1 issues by the author and by an independent codex reviewer (GPT-6.1 Sol, high).

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

  • Security: none.
  • Cross-platform: renderer only.
  • Remote SSH: covered by the same transport.
  • Mobile: not touched.
  • Wire compatibility: no wire change.
  • Performance: none.

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)

@coderabbitai

coderabbitai Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

📝 Walkthrough
📝 Walkthrough

Walkthrough

The remote PTY transport now holds input before a terminal handle is bound and associates held input with restored-session connection attempts. The recovery input hold tracks connection expectations and releases input when a matching handle is bound. The transport no longer uses the separate reattach buffer wrapper. Tests cover input ordering, delivery, rejection, and clearing during connection and lifecycle changes. The previous input-buffer test file and its CI timing entry were removed.



Priority: ➖ Normal

Merge Risk: 🔵 Low · up to 31118

Remote PTY recovery can send queued keystrokes to a replacement generation, and a recoverable subscribe failure can leave an accepted write pending. Both risks are confined to recovery paths; track incarnation binding and failed-connect settlement before relying on those paths broadly.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 70.59% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 17 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
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.
Title check Passed The title is concise and accurately identifies the main change: consolidating remote terminal input handling into one recovery input hold.
Description check Passed The description follows the repository template and explains the behavior, implementation, rationale, testing, compatibility considerations, and review status. The maintainer-specific issue guidance i…

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR



  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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: Advanced
  • Run ID: 27996dd3-6e08-4d63-a964-33c6ac6040a4
📥 Commits

Reviewing files that changed from the base of the PR and between 411d5c2 and ee8ae37.

📒 Files selected for processing (8)
  • config/scripts/ci-shard-timings.json
  • src/renderer/src/components/terminal-pane/pty-transport-types.ts
  • src/renderer/src/components/terminal-pane/remote-reattach-input-buffer.test.ts
  • src/renderer/src/components/terminal-pane/remote-reattach-input-buffer.ts
  • src/renderer/src/components/terminal-pane/remote-runtime-pty-transport-reattach-type-ahead.test.ts
  • src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts
  • src/renderer/src/components/terminal-pane/remote-runtime-recovery-input-hold.test.ts
  • src/renderer/src/components/terminal-pane/remote-runtime-recovery-input-hold.ts
💤 Files with no reviewable changes (4)
  • config/scripts/ci-shard-timings.json
  • src/renderer/src/components/terminal-pane/pty-transport-types.ts
  • src/renderer/src/components/terminal-pane/remote-reattach-input-buffer.test.ts
  • src/renderer/src/components/terminal-pane/remote-reattach-input-buffer.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.

Comment on lines +2647 to +2659
// Why: typing on a restored screen reaches its original terminal only, never a fresh create.
async function connectHoldingRestoredInput(
options: Parameters<PtyTransport['connect']>[0]
): Promise<PtyConnectResult | undefined> {
const expected = recoveryInputHold.expect(restoredInputEndpoint(options.sessionId))
try {
return await connectPane(options)
} finally {
recoveryInputHold.settleExpectation(expected, handle !== null && !destroyed)
if (handle && recoveryInputHold.isHolding()) {
releaseHeldInput()
}
}

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.

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

🔎 Supported by static analysis

🏁 Script executed:

sed -n '1490,1645p' src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts
sed -n '2640,2685p' src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts
sed -n '2800,2880p' src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts
sed -n '115,155p' src/renderer/src/components/terminal-pane/remote-runtime-recovery-input-hold.ts

Repository: stablyai/orca

Length of output: 11264


🏁 Script executed:

set -o pipefail
printf '%s\n' '--- wrapper references and connect entrypoints ---'
rg -n -F --glob '*.ts' -- 'connectHoldingRestoredInput' src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts
rg -n -F --glob '*.ts' -- 'connectPane(' src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts
printf '%s\n' '--- wrapper and connect body ---'
sed -n '2628,2690p' src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts
sed -n '2790,2925p' src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts
printf '%s\n' '--- hold implementation ---'
sed -n '1,175p' src/renderer/src/components/terminal-pane/remote-runtime-recovery-input-hold.ts
printf '%s\n' '--- lifecycle cleanup and recovery/retry callers ---'
rg -n -F --glob '*.ts' -e 'recoveryInputHold.discard' -e 'connect(' -e 'retry' -e 'recover' src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts | sed -n '1,220p'

Repository: stablyai/orca

Length of output: 27985


🏁 Script executed:

set -o pipefail
printf '%s\n' '--- automatic recovery caller ---'
sed -n '950,1008p' src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts
printf '%s\n' '--- lifecycle methods and retry caller ---'
sed -n '2918,3140p' src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts
printf '%s\n' '--- accepted-input public path ---'
sed -n '2960,3035p' src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts

Repository: stablyai/orca

Length of output: 12492


Settle the input hold from the connect result.

connectPane returns undefined when creation fails or its lifecycle epoch becomes stale. The wrapper instead uses the earlier non-null handle as success. This can leave an accepted input promise pending until a later retry or cleanup. A ready but different endpoint causes the hold to discard the input, not send it to another terminal.

Use the current connect result as the bind condition.

Suggested fix
   async function connectHoldingRestoredInput(
     options: Parameters<PtyTransport['connect']>[0]
   ): Promise<PtyConnectResult | undefined> {
     const expected = recoveryInputHold.expect(restoredInputEndpoint(options.sessionId))
+    let result: PtyConnectResult | undefined
     try {
-      return await connectPane(options)
+      result = await connectPane(options)
+      return result
     } finally {
-      recoveryInputHold.settleExpectation(expected, handle !== null && !destroyed)
+      recoveryInputHold.settleExpectation(expected, result !== undefined && !destroyed)
       if (handle && recoveryInputHold.isHolding()) {
         releaseHeldInput()
       }
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Why: typing on a restored screen reaches its original terminal only, never a fresh create.
async function connectHoldingRestoredInput(
options: Parameters<PtyTransport['connect']>[0]
): Promise<PtyConnectResult | undefined> {
const expected = recoveryInputHold.expect(restoredInputEndpoint(options.sessionId))
try {
return await connectPane(options)
} finally {
recoveryInputHold.settleExpectation(expected, handle !== null && !destroyed)
if (handle && recoveryInputHold.isHolding()) {
releaseHeldInput()
}
}
// Why: typing on a restored screen reaches its original terminal only, never a fresh create.
async function connectHoldingRestoredInput(
options: Parameters<PtyTransport['connect']>[0]
): Promise<PtyConnectResult | undefined> {
const expected = recoveryInputHold.expect(restoredInputEndpoint(options.sessionId))
let result: PtyConnectResult | undefined
try {
result = await connectPane(options)
return result
} finally {
recoveryInputHold.settleExpectation(expected, result !== undefined && !destroyed)
if (handle && recoveryInputHold.isHolding()) {
releaseHeldInput()
}
}

@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.

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

🟡 Minor · Bind restored input to the known PTY incarnation. · remote-runtime-pty-transport.ts:1613-1617

src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts:1613-1617
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Bind restored input to the known PTY incarnation.

During same-handle recovery, restoredInputEndpoint replaces the existing incarnation with null. The hold treats null as a wildcard, so a provider result with the same handle and a new incarnation can match the held endpoint. releaseHeldInput then flushes queued input to the replacement PTY generation instead of dropping it.

Suggested fix
   function restoredInputEndpoint(sessionId: string | undefined): RemoteRuntimeInputEndpoint | null {
     const restoredHandle = sessionId ? getRemoteRuntimeTerminalHandle(sessionId) : null
     return restoredHandle &&
       toRemoteRuntimePtyId(restoredHandle, currentRuntimeEnvironmentId) === sessionId
-      ? { handle: restoredHandle, incarnationId: null }
+      ? { handle: restoredHandle, incarnationId: authoritativePtyIncarnationId }
       : null
   }

ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d4042b80-758f-4e1b-8846-d7f97fe4009c
📥 Commits

Reviewing files that changed from the base of the PR and between cb630b9 and 31118d0.

📒 Files selected for processing (1)
  • src/renderer/src/components/terminal-pane/remote-runtime-recovery-input-hold.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.

@OrcaWin
OrcaWin merged commit 57a7c22 into main Oct 12, 2026
45 checks passed
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