t3code icon indicating copy to clipboard operation
t3code copied to clipboard

Add scoped SSH preview port forwarding

Open juliusmarminge opened this issue 4 months ago โ€ข 3 comments

Summary

  • add an Effect-based environment port router with scoped route ownership
  • acquire ref-counted, loopback-only SSH port forwards through the desktop bridge
  • route preview automation, discovered ports, and manual navigation consistently
  • keep T3 Connect forwarding explicitly unsupported for now

Verification

  • vp check
  • vp run typecheck
  • 48 focused SSH, IPC, client-runtime, and preview tests
  • React Doctor changed-file scan (no actionable diagnostics)
  • the orchestration-timeout test file from the all-repo parallel run passes 39/39 standalone

[!NOTE] Medium Risk Cross-cutting changes to SSH tunnel lifecycle and preview navigation; lease or scope bugs could leak forwards or break remote previews, though auth and relay paths are largely unchanged.

Overview Adds scoped SSH preview port forwarding so localhost/dev-server URLs on SSH environments resolve through ref-counted loopback tunnels instead of only rewriting hosts for direct private-network access.

The stack wires desktop IPC (acquireSshPortForward / releaseSshPortForward) through SshEnvironmentManager (shared forwards per remote port, TCP readiness, cleanup on disconnect), exposes forwardPort on SshEnvironmentGateway (desktop bridge with acquire/release; mobile blocked), and introduces EnvironmentPortRouter in client-runtime for direct vs private-network vs ssh-forward resolution (relay/T3 Connect ports still rejected).

Web preview replaces synchronous browserTargetResolver URL mapping with async route acquisition (commit/release per tab, SSH scopes held until tab close). Preview automation, manual navigation, discovered ports, and session close all use this lifecycle. Contracts add ssh-forward to PreviewUrlResolution; NetService.hasListenerOnHost supports forward readiness probes.

Reviewed by Cursor Bugbot for commit e5f5c40fdc3f82a828a75f5852664ffa85ae9b35. Bugbot is set up for automated code reviews on this repo. Configure here.

[!NOTE]

Add scoped SSH preview port forwarding with reference-counted lease management

  • Adds acquirePortForward and releasePortForward to SshEnvironmentManager, tracking concurrent leases per target/remotePort and tearing down the SSH tunnel only when the last lease is released.
  • Introduces EnvironmentPortRouter in packages/client-runtime/src/preview/router.ts that resolves preview navigation targets to direct, direct-private-network, or ssh-forward resolutions, acquiring scoped SSH port forwards for SSH connections.
  • Replaces synchronous URL resolution in browser navigation with an acquire/commit/release model (acquireBrowserNavigationTarget, releaseBrowserNavigationRoute, withBrowserNavigationRoute) so SSH-forward leases remain alive per tab until the tab is closed.
  • Extends the desktop IPC bridge with acquireSshPortForward and releaseSshPortForward channels, wired through DesktopSshEnvironment and exposed on window.desktopBridge.
  • Adds hasListenerOnHost to NetService and uses it to poll for TCP readiness when establishing port-forward tunnels, with a timeout bounded by SSH_READY_TIMEOUT_MS.
  • Risk: SSH port forwards now hold OS resources (a TCP tunnel process) per active tab; tabs that are never explicitly closed or whose routes are never released will leak the forward until the environment is disconnected.
๐Ÿ“Š Macroscope summarized e5f5c40. 21 files reviewed, 0 issues evaluated, 0 issues filtered, 0 comments posted

๐Ÿ—‚๏ธ Filtered Issues

No issues evaluated.

juliusmarminge avatar Jul 16 '26 09:07 juliusmarminge

[!IMPORTANT]

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

โš™๏ธ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: be16e21a-13d5-4686-8d19-160fab814eeb

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • [ ] ๐Ÿ” Trigger review
โœจ Finishing Touches
๐Ÿงช Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Commit unit tests in branch t3code/add-ssh-preview-port-forwarding

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

coderabbitai[bot] avatar Jul 16 '26 09:07 coderabbitai[bot]

Approvability

Verdict: Needs human review

5 blocking correctness issues found. New feature adding SSH preview port forwarding with complex lifecycle management. Multiple unresolved review comments identify potential resource leaks, race conditions, and concurrency bugs that require human attention before merging.

You can customize Macroscope's approvability policy. Learn more.

macroscopeapp[bot] avatar Jul 16 '26 10:07 macroscopeapp[bot]

Any update on this? I'd find this very helpful

HugoMarchant avatar Aug 03 '26 14:08 HugoMarchant

Ready for review: #7639 is a companion PR targeted directly at this PR feature branch.

Merging #7639 will add the fixes to #4039 and rerun this PR checks. No cherry-pick or local branch setup is needed.

It addresses all seven distinct actionable findings on this PR:

  • wait for the actual OpenSSH forward-ready signal
  • serialize forward reuse and final lease release
  • cancel pending forwards during manager shutdown
  • close forwards when setup is interrupted
  • prevent stale tab navigation from replacing newer routes
  • preserve routes until in-flight navigation finishes
  • release routes after disconnect, authoritative tab removal, and environment removal

The companion review found and fixed six follow-ups: removed tabs retire routes until in-flight work finishes, shutdown interrupts pending SSH setup before waiting on the forwarding lock, ready forwards close if interrupted before registration, environment cleanup closes route scopes in the caller Effect runtime, preview-close consumers use the canonical route module directly, and cancelled route acquisition does not open a useless local browser tab.

Manual two-machine testing between Nova and Showtime also found two entry-point defects. Terminal links now use the environment router before opening the preview, and preview forwards resolve remote localhost so normal IPv4 or IPv6 dev-server binding works without a special host flag. A direct SSH forwarding smoke test returned HTTP 200.

Validation passed: focused web and SSH tests, web and SSH package typechecks, targeted formatting and lint, and git diff --check.

All automated checks on #7639 are terminal with no failures, and all companion review threads are resolved. Macroscope left an informational note that human review is appropriate because this changes SSH forwarding lifecycle behavior.

Two-machine validation details, sanitized environment specs, and screenshot proof: https://github.com/pingdotgg/t3code/pull/7639#issuecomment-5362320919

r4lphio avatar Aug 20 '26 00:08 r4lphio