Skip to content

feat(native): SSH environments, first slice - #90

Merged
rynfar merged 4 commits into
nativefrom
nw/88-ssh-environments
Nov 3, 2026
Merged

rynfar merged 4 commits into
nativefrom
nw/88-ssh-environments

Conversation

@rynfar

@rynfar rynfar commented Nov 2, 2026 •

Copy link
Copy Markdown
Owner

Closes the first slice of #88 (ledger row CN-SSH, stays partial).

Problem

The desktop app can add an environment over SSH: it reads your SSH config, starts T3 Code on the host, forwards a local port to it, pairs over SSH, and reconnects it on launch. The native app could only pair with a link or with T3 Code on this Mac.

How it was ported

The desktop's SSH work lives in its main process. The native app is a desktop app, so it does that work itself.

  • crates/core/src/ssh (pure, tested) ports packages/ssh/src/config.ts (host discovery through Include, OpenSSH quoting and escapes, known_hosts with hashed and pattern entries skipped, certain-HostName dedupe), command.ts (ssh -G parsing, connection key, SHA-256 state key, base and command arguments with the host after --), tunnel.ts (the runner, launch, pairing and stop scripts, launch and pairing output after login-shell noise, tunnel arguments), auth.ts (askpass script and environment, auth-failure and agent-signing classifiers), plus the web's parseManualDesktopSshTarget, formatDesktopSshTarget and filterDiscoveredSshHosts. Tests mirror config.test.ts, command.test.ts, auth.test.ts and the script cases of tunnel.test.ts (state-key vectors 711bc738002d72fd and 326264c4f08c8a0c match).
  • crates/client/src/ssh.rs ports SshEnvironmentManager and the web's SSH gateway (provisionDesktopSshEnvironment and prepareDesktopSshEnvironment). It resolves the target, launches or reuses the remote server, forwards a local port, and waits for / to answer. It mints a pairing credential over SSH, checks the descriptor (identity and protocol), and redeems the credential. Every ssh runs in its own process group and is stopped through the PID this module spawned (SIGTERM, then SIGKILL after 2 s), never by name. One-shot commands die with their future, and the tunnel's stderr tail (16 KiB) explains an early exit.
  • Reconnects. The supervisor gained a Prepare step that runs before every connection attempt, the way the web resolver authorizes each one. An SSH environment keeps one local port for the session, so a dead tunnel is rebuilt under the supervisor without changing its address. A cancelled prompt or a host that now reaches another machine is ConnectionStatus::Blocked: phase Error, no retries until Retry.
  • Saved environments carry the SSH target (ssh, skipped when absent, so older files read as direct). Rows and Settings → Environments show SSH user@host:port (environmentTransportLabel). Remove and Disconnect close the tunnel and stop the remote server this app started (registry.ts disconnectSsh). Quit stops the tunnels.
  • UI. The connect screen's Add environment gets the web's two mode cards (Remote link, SSH). The SSH form has the host field with discovered, unsaved hosts listed under it (virtualized, prefix matches first, Loading hosts…, No hosts match "…".), plus Username and Port. A suggested host connects at once. Errors show in an error alert in the web's words (Could not prepare the SSH environment: …), the button reads Adding… while busy, and success toasts Environment connected / <alias> is ready over an SSH-managed tunnel.. A rejected SSH environment offers Pair again over SSH.
  • Password and passphrase prompt (included). Batch mode comes first. On an auth failure a password could fix, the app asks in a port of SshPasswordPromptDialog (title, description with the target in mono, countdown, Expired/Dismiss, the web's hint and errors) and retries through an askpass helper written once per session into a private 0700 directory. It asks twice at most and never for an agent-signing failure. Answers are kept in memory for the session only.

Deliberate differences

  • Remote T3 version. The desktop runs its own app version on the host. The native app runs the release of the t3 CLI installed on this Mac, else the latest GitHub release.
  • Release archives only. The desktop's development mode (a Node source checkout on the remote) has no native counterpart, so its Node discovery scripts are left out.
  • Separate remote state. Remote state lives under ~/.t3/ssh-launch/native-<key>. The desktop's runner script can't be reproduced byte for byte (it embeds bundled JS), and on a shared key each app would replace the other's server on every reconnect. Each app adopts the other's server through the default runtime file instead.
  • Quit stops tunnels, not the remote server. The next launch reuses the server, and quitting doesn't pull it from under another client. The desktop stops a managed server on quit.
  • Inline host list. Discovered hosts are listed under the field instead of in a popup. The Remote link card says "Paste a pairing link from the backend.", because the native form takes a link, not host + code.

Verification

  • cargo test --workspace and cargo clippy --workspace --all-targets -- -D warnings pass. So do node scripts/gen-theme.ts --check, node --test parity/sync.test.ts (42 pass) and conformance/generate.ts --check.
  • Process tests (crates/client/src/ssh/tests.rs) run against a fake ssh shell script. Provisioning launches, forwards and pairs, and a second prepare reuses the tunnel and refuses another machine. Disconnect kills the tunnel by PID and runs the stop script; shutdown kills tunnels and leaves the server. A password host prompts twice (wrong, then right) and gets in through the real askpass helper. An agent-signing failure never prompts, and a cancelled prompt is Blocked. An early tunnel exit reports a 16 KiB stderr tail ending in café across a split UTF-8 chunk. The remote scripts pass sh -n.
  • The supervisor test covers a Blocked prepare, waiting for Retry, preparing again before reconnecting after a dropped session.
  • End to end, offscreen. I used a fake ssh, a scratch HOME with SSH config, and a loopback forwarder standing in for the tunnel, with the smoke server as the "remote" and a read-only pairing credential I minted. Adding devbox: -G, batch launch denied, in-app prompt, askpass retry, tunnel, pairing, redeem, connected. The supervisor's prepare then reused the single tunnel. Relaunching with the saved environment reconnected through the prepare hook. Disconnect from Settings stopped the tunnel by its PID and ran the stop script. I revoked the two smoke sessions this created afterwards.
  • Live SSH was not verified. ssh -o BatchMode=yes localhost true fails here (host key verification), and I didn't change ~/.ssh or system settings. No real ssh, remote shell, or release download ran.

Screenshots (offscreen renderer, fixture hosts)

  • SSH mode with discovered hosts: /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/3998c826-bb1a-4777-8d4c-a9ea46007740/scratchpad/ssh88/shots/01-ssh-hosts.png
  • Typing filters the list: /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/3998c826-bb1a-4777-8d4c-a9ea46007740/scratchpad/ssh88/shots/02-filter.png
  • Adding… (busy, inputs disabled): /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/3998c826-bb1a-4777-8d4c-a9ea46007740/scratchpad/ssh88/shots/03-adding.png
  • Error alert: /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/3998c826-bb1a-4777-8d4c-a9ea46007740/scratchpad/ssh88/shots/04-error.png
  • Password prompt: /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/3998c826-bb1a-4777-8d4c-a9ea46007740/scratchpad/ssh88/shots/05-password.png
  • Connected over the tunnel after the prompt: /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/3998c826-bb1a-4777-8d4c-a9ea46007740/scratchpad/ssh88/shots/06-live-connected.png
  • Relaunch reconnects; Settings → Environments shows SSH [email protected]:22: /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/3998c826-bb1a-4777-8d4c-a9ea46007740/scratchpad/ssh88/shots/07-relaunch-settings.png
  • After Disconnect, the saved SSH row: /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/3998c826-bb1a-4777-8d4c-a9ea46007740/scratchpad/ssh88/shots/08-disconnected.png

After review

  • The remote stop and launch scripts signal a recorded server only when its recorded and current start times (ps -o lstart=) are both known and equal. A reused PID, or a host with no way to tell, is never signalled. The launch-failure kill uses the shell's own $!. The desktop trusts kill -0 here (tunnel.ts:644, :679, :745).
  • Quitting SIGKILLs every ssh process group the app spawned before the quit handler returns. That includes launches still installing and tunnels still waiting to answer, and it fits GPUI's 200 ms quit budget. Nothing new starts after a quit.
  • Only a bind collision ("Address already in use") retries the tunnel on another port. The supervisor's step then reports the new address (ClientEvent::Moved), and the app reconnects there.
  • Every command pins the resolved host name (-o HostName=), with its user and port; only ssh -G itself is unpinned. A running tunnel keeps the resolution it started with, so Disconnect and Remove stop the right tunnel and server after a config change mid-session.

Gaps (second slice)

  • Live SSH against a real host (see above).
  • Keyboard navigation of the host list (arrows, Enter on the highlighted host, ⌘1–9 shortcuts), and filling Username/Port from ssh -G when a suggestion is picked.
  • The web's Add route mode (an SSH route to an existing environment) and multi-route environments.
  • Re-pairing a saved token that lacks newer standard scopes (prepareDesktopSshEnvironment's scope check). Today a rejected token prompts Pair again over SSH.
  • The desktop's 500-on-redeem hint for a reused server on another build.
  • Remote server log tail in tunnel-failure diagnostics (readRemoteServerLogTail).
  • A crash (not a quit) leaves tunnels running. A watchdog would need the remote side's help.

Built with Claude Opus 5.5 in Claude Code.

Adds SSH as a way to add an environment, as the desktop app does: discover hosts
from ~/.ssh/config and known_hosts, start the T3 release on the host over ssh,
forward a local port to it, pair over SSH, save the environment, reconnect it on
launch, and stop the tunnel and remote server on remove or disconnect. A host
that wants a password asks in an in-app prompt answered through an askpass helper.
…, recorded routes

The remote stop and launch scripts signal a recorded server only while it
still has the start time recorded with it, so a reused PID is left alone.
Quitting stops every ssh the app started, including launches still
installing and tunnels still waiting to answer, and waits until each is gone.
A tunnel whose local port another process took moves to a free one, and the
client reconnects there. Disconnect and remove find the tunnel a target
reached and stop the server on the host it started on, even if the SSH config
moved the alias since.
@rynfar
rynfar force-pushed the nw/88-ssh-environments branch from d2cfa1a to 64c29d8 Compare November 3, 2026 00:17
…at once

A recorded remote server is signalled only when both its recorded and its
current start time are known and equal. Quitting SIGKILLs every ssh process
group the app spawned before returning, inside the quit budget. Only a bind
collision moves the tunnel to another port. Every command pins the resolved
host name, so a HostName added to the config mid-session cannot move the stop.
…nch-failure kill

The tunnel monitor deregisters its process group as soon as the leader is
reaped, before the diagnostics drain, so a quit never signals a reused group
ID. The launch script's readiness-failure path checks the recorded start time
before killing, as the stop path does.
@rynfar
rynfar merged commit 26e136c into native Nov 3, 2026
@rynfar rynfar mentioned this pull request Nov 3, 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.

1 participant