Skip to content

Commit 887febc

Browse files
t3dotggclaude
andcommitted
feat(web): open remote environments in your local editor over SSH
When viewing T3 Code from a machine other than the environment's host, the Open button now hands the OS a vscode://vscode-remote/ssh-remote+ deep link instead of launching an editor on the far machine. The server advertises its tailnet MagicDNS name (falling back to hostname.local), gated on a local sshd check; desktop-SSH environments reuse their real ssh config alias. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 7e01d33 commit 887febc

23 files changed

Lines changed: 635 additions & 46 deletions

File tree

‎apps/desktop/src/electron/ElectronShell.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
import { REMOTE_CAPABLE_EDITOR_IDS, remoteSchemeForEditor } from "@t3tools/contracts";
12
import * as Context from "effect/Context";
23
import * as Effect from "effect/Effect";
34
import * as Layer from "effect/Layer";
45
import * as Option from "effect/Option";
56

67
import * as Electron from "electron";
78

8-
const SAFE_EXTERNAL_PROTOCOLS = new Set(["http:", "https:"]);
9+
// Remote open-in-editor deep links (`vscode://vscode-remote/ssh-remote+…`)
10+
// must reach the OS handler; every other non-web scheme stays blocked.
11+
const SAFE_EXTERNAL_PROTOCOLS = new Set([
12+
"http:",
13+
"https:",
14+
...REMOTE_CAPABLE_EDITOR_IDS.flatMap((id) => {
15+
const scheme = remoteSchemeForEditor(id);
16+
return scheme === undefined ? [] : [`${scheme}:`];
17+
}),
18+
]);
919

1020
export function parseSafeExternalUrl(rawUrl: unknown): Option.Option<string> {
1121
if (typeof rawUrl !== "string") {

‎apps/desktop/src/ipc/DesktopIpcHandlers.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
getLocalEnvironmentBearerToken,
3737
getWindowFullscreenState,
3838
openExternal,
39+
probeRemoteEditors,
3940
pickFolder,
4041
pickThemeFiles,
4142
setTheme,
@@ -83,6 +84,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers"
8384
yield* ipc.handle(setTheme);
8485
yield* ipc.handle(showContextMenu);
8586
yield* ipc.handle(openExternal);
87+
yield* ipc.handle(probeRemoteEditors);
8688
yield* ipc.handle(getUpdateState);
8789
yield* ipc.handle(setUpdateChannel);
8890
yield* ipc.handle(downloadUpdate);

‎apps/desktop/src/ipc/channels.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export const PICK_THEME_FILES_CHANNEL = "desktop:pick-theme-files";
33
export const SET_THEME_CHANNEL = "desktop:set-theme";
44
export const CONTEXT_MENU_CHANNEL = "desktop:context-menu";
55
export const OPEN_EXTERNAL_CHANNEL = "desktop:open-external";
6+
export const PROBE_REMOTE_EDITORS_CHANNEL = "desktop:probe-remote-editors";
67
export const MENU_ACTION_CHANNEL = "desktop:menu-action";
78
export const GET_WINDOW_FULLSCREEN_STATE_CHANNEL = "desktop:get-window-fullscreen-state";
89
export const WINDOW_FULLSCREEN_STATE_CHANNEL = "desktop:window-fullscreen-state";

‎apps/desktop/src/ipc/methods/window.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ import {
33
DesktopAppBrandingSchema,
44
DesktopEnvironmentBootstrapSchema,
55
DesktopThemeSchema,
6+
EDITORS,
7+
EditorId,
68
PickedThemeFileSchema,
79
PickFolderOptionsSchema,
810
PRIMARY_LOCAL_ENVIRONMENT_ID,
11+
REMOTE_CAPABLE_EDITOR_IDS,
912
type DesktopEnvironmentBootstrap,
1013
type PickedThemeFile,
1114
} from "@t3tools/contracts";
15+
import { isCommandAvailable } from "@t3tools/shared/shell";
1216
import * as NodeOS from "node:os";
1317
import * as FileSystem from "effect/FileSystem";
1418
import * as Path from "effect/Path";
@@ -261,6 +265,30 @@ export const openExternal = DesktopIpc.makeIpcMethod({
261265
}),
262266
});
263267

268+
export const probeRemoteEditors = DesktopIpc.makeIpcMethod({
269+
channel: IpcChannels.PROBE_REMOTE_EDITORS_CHANNEL,
270+
payload: Schema.Undefined,
271+
result: Schema.Array(EditorId),
272+
// Probes THIS machine (where the renderer runs) for remote-capable editor
273+
// CLIs, unlike the server's probe which walks the environment host's PATH.
274+
// A Finder-launched app can miss PATH entries; an empty result makes the
275+
// renderer fall back to VS Code only, so that fails soft.
276+
handler: Effect.fn("desktop.ipc.window.probeRemoteEditors")(function* () {
277+
const available: Array<EditorId> = [];
278+
for (const editorId of REMOTE_CAPABLE_EDITOR_IDS) {
279+
const commands = EDITORS.find((editor) => editor.id === editorId)?.commands;
280+
if (!commands) continue;
281+
for (const command of commands) {
282+
if (yield* isCommandAvailable(command, { env: process.env })) {
283+
available.push(editorId);
284+
break;
285+
}
286+
}
287+
}
288+
return available;
289+
}),
290+
});
291+
264292
/** Theme files are a few KB; anything larger returns empty text and lets the
265293
* renderer reject it by size without the contents ever crossing the bridge. */
266294
const PICKED_THEME_FILE_MAX_BYTES = 256 * 1024;

‎apps/desktop/src/preload.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ contextBridge.exposeInMainWorld("desktopBridge", {
105105
...(position === undefined ? {} : { position }),
106106
}),
107107
openExternal: (url: string) => ipcRenderer.invoke(IpcChannels.OPEN_EXTERNAL_CHANNEL, url),
108+
probeRemoteEditors: () => ipcRenderer.invoke(IpcChannels.PROBE_REMOTE_EDITORS_CHANNEL, undefined),
108109
onMenuAction: (listener) => {
109110
const wrappedListener = (_event: Electron.IpcRendererEvent, action: unknown) => {
110111
if (typeof action !== "string") return;

‎apps/desktop/src/wsl/DesktopWslBackend.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const backendConfigurationLayer = Layer.succeed(
7777
const netLayer = Layer.succeed(NetService.NetService, {
7878
canListenOnHost: () => Effect.succeed(true),
7979
isPortAvailableOnLoopback: () => Effect.succeed(true),
80+
hasListenerOnHost: () => Effect.succeed(false),
8081
reserveLoopbackPort: () => Effect.succeed(41773),
8182
findAvailablePort: (preferred) => Effect.succeed(preferred),
8283
} satisfies NetService.NetService["Service"]);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* RemoteOpenTargets - resolves the SSH hostnames this environment advertises
3+
* for remote open-in-editor deep links (`vscode://vscode-remote/ssh-remote+…`).
4+
*
5+
* The server can only check itself: sshd listening locally, tailscaled
6+
* reporting a MagicDNS name, and the machine hostname for mDNS. Whether a
7+
* given name resolves from the viewer's machine is inherently client-side.
8+
* Targets are ordered most-reachable first (tailnet name works from anywhere
9+
* on the tailnet; `<hostname>.local` only on the same LAN).
10+
*/
11+
import { type RemoteOpenTarget } from "@t3tools/contracts";
12+
import { HostProcessHostname } from "@t3tools/shared/hostProcess";
13+
import * as NetService from "@t3tools/shared/Net";
14+
import { readTailscaleStatus } from "@t3tools/tailscale";
15+
import * as Context from "effect/Context";
16+
import * as Effect from "effect/Effect";
17+
import * as Layer from "effect/Layer";
18+
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
19+
20+
const SSH_PORT = 22;
21+
22+
export class RemoteOpenTargets extends Context.Service<
23+
RemoteOpenTargets,
24+
{
25+
readonly resolveTargets: () => Effect.Effect<ReadonlyArray<RemoteOpenTarget>>;
26+
}
27+
>()("t3/environment/RemoteOpenTargets") {}
28+
29+
export const make = Effect.gen(function* () {
30+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
31+
const net = yield* NetService.NetService;
32+
33+
const resolveTargets = Effect.gen(function* () {
34+
// No local sshd means no name can work; advertise nothing so clients
35+
// render a clear "no SSH route" state instead of links that hang.
36+
const sshdListening = yield* net.hasListenerOnHost(SSH_PORT, "127.0.0.1");
37+
if (!sshdListening) {
38+
return [];
39+
}
40+
41+
const targets: Array<RemoteOpenTarget> = [];
42+
43+
// Tailscale absent or down is the common case, not an error.
44+
const magicDnsName = yield* readTailscaleStatus.pipe(
45+
Effect.map((status) => status.magicDnsName),
46+
Effect.orElseSucceed(() => null),
47+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
48+
);
49+
if (magicDnsName !== null) {
50+
targets.push({ kind: "tailscale", host: magicDnsName });
51+
}
52+
53+
// os.hostname() may already be an FQDN (macOS often reports
54+
// "Name.local"); mDNS names are always `<first-label>.local`.
55+
const hostname = yield* HostProcessHostname;
56+
const shortHostname = hostname.split(".")[0]?.trim();
57+
if (shortHostname !== undefined && shortHostname.length > 0) {
58+
targets.push({ kind: "mdns", host: `${shortHostname}.local` });
59+
}
60+
61+
return targets;
62+
});
63+
64+
return RemoteOpenTargets.of({ resolveTargets: () => resolveTargets });
65+
});
66+
67+
export const layer = Layer.effect(RemoteOpenTargets, make);

‎apps/server/src/preview/PortScanner.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const makeProbeFailureLayer = (
6262
Layer.succeed(Net.NetService, {
6363
canListenOnHost: () => Effect.succeed(true),
6464
isPortAvailableOnLoopback: () => Effect.succeed(true),
65+
hasListenerOnHost: () => Effect.succeed(false),
6566
reserveLoopbackPort: () => Effect.succeed(40_000),
6667
findAvailablePort: (preferred) => Effect.succeed(preferred),
6768
}),

‎apps/server/src/server.test.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ import * as CheckpointDiffQuery from "./checkpointing/CheckpointDiffQuery.ts";
107107
import * as GitManager from "./git/GitManager.ts";
108108
import * as Keybindings from "./keybindings.ts";
109109
import * as ExternalLauncher from "./process/externalLauncher.ts";
110+
import * as RemoteOpenTargets from "./environment/RemoteOpenTargets.ts";
110111
import * as OrchestrationEngine from "./orchestration/Services/OrchestrationEngine.ts";
111112
import { OrchestrationListenerCallbackError } from "./orchestration/Errors.ts";
112113
import * as ProjectionSnapshotQuery from "./orchestration/Services/ProjectionSnapshotQuery.ts";
@@ -650,10 +651,15 @@ const buildAppUnderTest = (options?: {
650651
}),
651652
),
652653
Layer.provide(
653-
Layer.mock(ExternalLauncher.ExternalLauncher)({
654-
resolveAvailableEditors: () => Effect.succeed([]),
655-
...options?.layers?.externalLauncher,
656-
}),
654+
Layer.mergeAll(
655+
Layer.mock(ExternalLauncher.ExternalLauncher)({
656+
resolveAvailableEditors: () => Effect.succeed([]),
657+
...options?.layers?.externalLauncher,
658+
}),
659+
Layer.mock(RemoteOpenTargets.RemoteOpenTargets)({
660+
resolveTargets: () => Effect.succeed([]),
661+
}),
662+
),
657663
),
658664
Layer.provide(
659665
Layer.mock(ProcessDiagnostics.ProcessDiagnostics)({

‎apps/server/src/server.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import * as SourceControlRepositoryService from "./sourceControl/SourceControlRe
8181
import * as ProjectSetupScriptRunner from "./project/ProjectSetupScriptRunner.ts";
8282
import { ObservabilityLive } from "./observability/Layers/Observability.ts";
8383
import * as ServerEnvironment from "./environment/ServerEnvironment.ts";
84+
import * as RemoteOpenTargets from "./environment/RemoteOpenTargets.ts";
8485
import { authHttpApiLayer, environmentAuthenticatedAuthLayer } from "./auth/http.ts";
8586
import * as ServerSecretStore from "./auth/ServerSecretStore.ts";
8687
import * as EnvironmentAuth from "./auth/EnvironmentAuth.ts";
@@ -420,6 +421,7 @@ const RuntimeDependenciesLive = RuntimeCoreDependenciesLive.pipe(
420421
Layer.provideMerge(TraceDiagnostics.layer),
421422
Layer.provideMerge(AnalyticsService.layer),
422423
Layer.provideMerge(ExternalLauncher.layer),
424+
Layer.provideMerge(RemoteOpenTargets.layer),
423425
Layer.provideMerge(ServerLifecycleEvents.layer),
424426
Layer.provide(NetService.layer),
425427
);

0 commit comments

Comments
 (0)