Skip to content

feat(ssh): persist and auto-restore SSH sessions across app restarts - #1026

Merged
Jinwoo-H merged 11 commits into
mainfrom
Jinwoo-H/persist-ssh-985
Apr 26, 2026
Merged

Jinwoo-H merged 11 commits into
mainfrom
Jinwoo-H/persist-ssh-985

Conversation

@Jinwoo-H

Copy link
Copy Markdown
Contributor

Summary

Closes #985. When Orca restarts, SSH connections and remote terminal sessions are now automatically restored instead of requiring manual reconnection.

  • Persist SSH state at shutdown: Captures active SSH connection IDs and remote PTY session IDs from renderer-side Zustand state during beforeunload — no sync IPC needed
  • Auto-reconnect on startup: Re-establishes SSH connections before terminal reconnect; passphrase-protected targets are deferred to tab focus to avoid stacking credential dialogs
  • Reattach remote PTYs: Routes sessionId through SshPtyProvider.spawn() → relay pty.attach with try/catch fallback to fresh spawn when grace window expires
  • Dedicated replay channel: Adds pty:replay IPC channel so relay replay data routes through xterm's replay guard, preventing auto-reply leakage
  • Configurable grace period: Per-target relay grace time (60s–3600s, default 300s) exposed in SSH target settings UI

Files changed (18)

  • Shared types: ssh-types.ts, types.ts, workspace-session-schema.ts — new fields
  • Main process: ssh-pty-provider.ts, ssh-relay-helpers.ts, ssh.ts, ssh-relay-deploy.ts, types.ts — sessionId routing, replay wiring, passphrase tracking, configurable grace time
  • Preload: index.ts, api-types.d.ts — onReplay binding
  • Renderer: App.tsx, workspace-session.ts, terminals.ts, pty-connection.ts, pty-dispatcher.ts, pty-transport.ts, SshPane.tsx, SshTargetForm.tsx — capture/restore/reconnect/UI

Test plan

  • Connect to SSH target with 2+ terminal tabs running long processes, quit Orca, relaunch within 5min → SSH reconnects, terminals restore with scrollback
  • Quit Orca, wait past grace window (5min default), relaunch → SSH reconnects, terminals show "Session expired" and spawn fresh shells
  • With passphrase-protected key: quit and relaunch → tabs show error until focused, focusing triggers passphrase dialog, then terminal restores
  • Verify orca-data.json contains activeConnectionIdsAtShutdown and remoteSessionIdsByTabId after quit with SSH tabs active
  • Change relay grace period in SSH target settings, verify relay uses the configured value
  • Mixed local + remote terminals → both restore correctly
  • Force-quit Orca (no beforeunload) → no crash on next launch, clean fallback

@Jinwoo-H
Jinwoo-H force-pushed the Jinwoo-H/persist-ssh-985 branch 2 times, most recently from 4bdc694 to edbd394 Compare April 24, 2026 22:54
@Jinwoo-H Jinwoo-H closed this Apr 24, 2026
@Jinwoo-H Jinwoo-H reopened this Apr 24, 2026
@Jinwoo-H
Jinwoo-H force-pushed the Jinwoo-H/persist-ssh-985 branch 2 times, most recently from edbd394 to aa136fc Compare April 24, 2026 22:58
@Jinwoo-H Jinwoo-H closed this Apr 25, 2026
@Jinwoo-H Jinwoo-H reopened this Apr 25, 2026
@Jinwoo-H
Jinwoo-H force-pushed the Jinwoo-H/persist-ssh-985 branch from 14889bd to 13096a9 Compare April 25, 2026 02:55
@Jinwoo-H Jinwoo-H closed this Apr 25, 2026
@Jinwoo-H Jinwoo-H reopened this Apr 25, 2026
@nwparker nwparker added the size/l Large PR (≤2500 added lines, ≤50 files) label Apr 26, 2026
@Jinwoo-H
Jinwoo-H force-pushed the Jinwoo-H/persist-ssh-985 branch from 0724f0f to c572b6d Compare April 26, 2026 03:06
Jinwoo-H added 11 commits April 25, 2026 23:07
…985)

Capture active SSH connections and remote PTY session IDs at shutdown,
auto-reconnect on startup, and reattach to relay PTYs within the grace
window. Passphrase-protected targets are deferred to tab focus to avoid
stacking credential dialogs at startup.
workspace-session.test.ts was missing sshConnectionStates (Map),
repos, and worktreesByRepo in the snapshot fixture. pty-connection.test.ts
was missing deferredSshReconnectTargets in the mock store state.
The relay now creates a Unix domain socket (relay.sock) alongside its
stdin/stdout transport. When the app restarts and SSH reconnects,
launchRelay probes for the socket and launches in --connect mode to
bridge the new SSH channel to the existing relay process — preserving
live PTY sessions and scrollback buffers across restarts.

If the socket is stale (crashed relay), the probe fails gracefully and
a fresh relay is launched as before.
…h, credential leak

P0 fixes:
- Deferred SSH reconnect now attempts pty.attach with the persisted
  session ID instead of always spawning a fresh shell
- Socket created with chmod 0600 to prevent other users on shared
  hosts from hijacking PTY sessions
- credentialRequestedForTarget cleared on connect failure so targets
  aren't permanently misclassified as needing passphrase

P1 fixes:
- Grace timer only starts when the closing socket is still the active
  one, preventing spurious shutdown when a socket is replaced
- Dispatcher writes to a no-op after stdin dies instead of a dead pipe
- Connect-mode bridge has a 5s timeout to avoid hanging on stale sockets
- graceTime clamped to integer before shell interpolation
…ects

Round 2 review fixes:

- Add deferredSshSessionIdsByTabId store field to preserve remote PTY
  session IDs for passphrase-protected targets across the startup clear
  of pendingReconnectPtyIdByTabId. Without this, deferred reconnect
  always fell back to fresh spawn because the IDs were already gone.

- Remove target from deferredSshReconnectTargets BEFORE awaiting
  ssh.connect to prevent split panes from each triggering parallel
  connect calls that race and fail with "already in progress".

- Check sshConnectionStates before calling ssh.connect so panes that
  mount after another pane already completed the deferred connect skip
  the redundant call and proceed directly to reattach.
…ssion expired toast

- Pass target's relayGracePeriodSeconds to reestablishRelayStack so
  network-blip reconnects inherit the configured grace period instead
  of always using the 300s default.

- Reset relay dispatcher sequence counters and decoder on socket
  reconnect so the new client's seq=1 frames are properly acked.
  Without this, the relay's highestReceivedSeq stays at the old
  client's value, preventing ack updates for early frames.

- Surface sessionExpired flag from SSH pty.spawn to the renderer so
  users see "Previous session expired — started a new shell" when
  reattach fails due to elapsed grace window.
… machine

Replace scattered relay state (activeMultiplexers Map, initializedConnections
Set, explicitRelaySetupTargets Set, reestablishAbortControllers Map) with a
single SshRelaySession class per SSH target.

The session class encapsulates the full relay lifecycle:
- State machine: idle → deploying → ready → reconnecting → disposed
- Owns: multiplexer, providers (pty/fs/git), abort controller
- Single `registerProviders()` path used by both establish() and reconnect(),
  eliminating the duplication that caused past bugs where one path forgot
  a registration step the other handled

Delete ssh-relay-helpers.ts — all logic now lives in the session class.
Add 13 unit tests covering state transitions, error recovery, overlapping
reconnects, PTY reattach, and port forward cleanup.
…rtial cleanup

- Dispose existing session before creating new one in ssh:connect,
  preventing mux/provider/timer leaks on double-connect.

- Allow onStateChange to retry reconnect when session is in
  'reconnecting' state (not just 'ready'). Without this, a failed
  relay deploy on a working SSH connection permanently bricked the
  session with no recovery path.

- Clean up mux and partially-registered providers in establish()
  error path via teardownProviders(), preventing leaks when
  registerRelayRoots or provider construction throws.

- Add 2 tests: partial-registration cleanup and reconnect retry
  after failure.
Restrict reconnect() to only accept 'ready' and 'reconnecting' states.
Calling from 'deploying' would tear down a mux that establish() is
concurrently using; 'idle' means no session was established yet.
Previously only guarded against 'disposed'.
@Jinwoo-H
Jinwoo-H force-pushed the Jinwoo-H/persist-ssh-985 branch from c572b6d to 198c557 Compare April 26, 2026 03:10
@github-actions github-actions Bot added size/xl Extra large PR (>2500 added lines or >50 files) and removed size/l Large PR (≤2500 added lines, ≤50 files) labels Apr 26, 2026
@Jinwoo-H
Jinwoo-H merged commit 6729825 into main Apr 26, 2026
3 checks passed
@Jinwoo-H
Jinwoo-H deleted the Jinwoo-H/persist-ssh-985 branch April 26, 2026 03:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/xl Extra large PR (>2500 added lines or >50 files)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Persist and auto-restore SSH connections + remote terminal sessions across app restarts

2 participants