Skip to content

Commit e078835

Browse files
t3dotggclaude
andcommitted
fix: address review findings on remote open-in-editor
- probe both loopback families for sshd (IPv6-only bindings) - treat the desktop app's own primary as local even on a WSL NAT URL - only record hint-seen/preferred editor when the shell accepted the URL - add focused RemoteOpenTargets tests; complete NetService mocks from #6021 Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 887febc commit e078835

6 files changed

Lines changed: 181 additions & 10 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import { it } from "@effect/vitest";
2+
import { HostProcessHostname } from "@t3tools/shared/hostProcess";
3+
import * as NetService from "@t3tools/shared/Net";
4+
import * as Effect from "effect/Effect";
5+
import * as Layer from "effect/Layer";
6+
import * as Sink from "effect/Sink";
7+
import * as Stream from "effect/Stream";
8+
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
9+
import { describe, expect } from "vite-plus/test";
10+
11+
import * as RemoteOpenTargets from "./RemoteOpenTargets.ts";
12+
13+
const encoder = new TextEncoder();
14+
15+
const TAILSCALE_STATUS_JSON = JSON.stringify({
16+
Self: { DNSName: "bb-1.tail1234.ts.net.", TailscaleIPs: ["100.64.1.2"] },
17+
});
18+
19+
/** Spawner whose `tailscale status --json` exits with the given output. */
20+
const spawnerLayer = (input: { readonly exitCode: number; readonly stdout: string }) =>
21+
Layer.succeed(
22+
ChildProcessSpawner.ChildProcessSpawner,
23+
ChildProcessSpawner.make(() =>
24+
Effect.succeed(
25+
ChildProcessSpawner.makeHandle({
26+
pid: ChildProcessSpawner.ProcessId(1),
27+
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(input.exitCode)),
28+
isRunning: Effect.succeed(false),
29+
kill: () => Effect.void,
30+
unref: Effect.succeed(Effect.void),
31+
stdin: Sink.drain,
32+
stdout: Stream.make(encoder.encode(input.stdout)),
33+
stderr: Stream.empty,
34+
all: Stream.empty,
35+
getInputFd: () => Sink.drain,
36+
getOutputFd: () => Stream.empty,
37+
}),
38+
),
39+
),
40+
);
41+
42+
const netLayer = (input: { readonly ipv4: boolean; readonly ipv6: boolean }) =>
43+
Layer.succeed(NetService.NetService, {
44+
canListenOnHost: () => Effect.succeed(true),
45+
isPortAvailableOnLoopback: () => Effect.succeed(true),
46+
hasListenerOnHost: (_port, host) => Effect.succeed(host === "::1" ? input.ipv6 : input.ipv4),
47+
reserveLoopbackPort: () => Effect.succeed(40_000),
48+
findAvailablePort: (preferred) => Effect.succeed(preferred),
49+
});
50+
51+
const resolveTargets = (input: {
52+
readonly sshd: { readonly ipv4: boolean; readonly ipv6: boolean };
53+
readonly tailscale: { readonly exitCode: number; readonly stdout: string };
54+
readonly hostname: string;
55+
}) =>
56+
Effect.flatMap(RemoteOpenTargets.RemoteOpenTargets, (service) => service.resolveTargets()).pipe(
57+
Effect.provideService(HostProcessHostname, input.hostname),
58+
Effect.provide(
59+
RemoteOpenTargets.layer.pipe(
60+
Layer.provide(Layer.mergeAll(netLayer(input.sshd), spawnerLayer(input.tailscale))),
61+
),
62+
),
63+
);
64+
65+
const TAILSCALE_UP = { exitCode: 0, stdout: TAILSCALE_STATUS_JSON };
66+
const TAILSCALE_DOWN = { exitCode: 1, stdout: "" };
67+
68+
describe("RemoteOpenTargets", () => {
69+
it.effect("advertises nothing when no sshd accepts on either loopback", () =>
70+
Effect.gen(function* () {
71+
const targets = yield* resolveTargets({
72+
sshd: { ipv4: false, ipv6: false },
73+
tailscale: TAILSCALE_UP,
74+
hostname: "bb-1",
75+
});
76+
expect(targets).toEqual([]);
77+
}),
78+
);
79+
80+
it.effect("orders the tailnet name before the mDNS name", () =>
81+
Effect.gen(function* () {
82+
const targets = yield* resolveTargets({
83+
sshd: { ipv4: true, ipv6: true },
84+
tailscale: TAILSCALE_UP,
85+
hostname: "bb-1",
86+
});
87+
expect(targets).toEqual([
88+
{ kind: "tailscale", host: "bb-1.tail1234.ts.net" },
89+
{ kind: "mdns", host: "bb-1.local" },
90+
]);
91+
}),
92+
);
93+
94+
it.effect("accepts an sshd bound to IPv6 loopback only", () =>
95+
Effect.gen(function* () {
96+
const targets = yield* resolveTargets({
97+
sshd: { ipv4: false, ipv6: true },
98+
tailscale: TAILSCALE_DOWN,
99+
hostname: "bb-1",
100+
});
101+
expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local" }]);
102+
}),
103+
);
104+
105+
it.effect("falls back to mDNS alone when tailscale is unavailable", () =>
106+
Effect.gen(function* () {
107+
const targets = yield* resolveTargets({
108+
sshd: { ipv4: true, ipv6: false },
109+
tailscale: TAILSCALE_DOWN,
110+
hostname: "bb-1",
111+
});
112+
expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local" }]);
113+
}),
114+
);
115+
116+
it.effect("shortens an FQDN hostname to its first label for mDNS", () =>
117+
Effect.gen(function* () {
118+
const targets = yield* resolveTargets({
119+
sshd: { ipv4: true, ipv6: true },
120+
tailscale: TAILSCALE_DOWN,
121+
hostname: "bb-1.example.com",
122+
});
123+
expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local" }]);
124+
}),
125+
);
126+
});

‎apps/server/src/environment/RemoteOpenTargets.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ export const make = Effect.gen(function* () {
3333
const resolveTargets = Effect.gen(function* () {
3434
// No local sshd means no name can work; advertise nothing so clients
3535
// render a clear "no SSH route" state instead of links that hang.
36-
const sshdListening = yield* net.hasListenerOnHost(SSH_PORT, "127.0.0.1");
36+
// Check both loopback families: sshd can be bound IPv6-only.
37+
const sshdListening = yield* Effect.zipWith(
38+
net.hasListenerOnHost(SSH_PORT, "127.0.0.1"),
39+
net.hasListenerOnHost(SSH_PORT, "::1"),
40+
(ipv4, ipv6) => ipv4 || ipv6,
41+
);
3742
if (!sshdListening) {
3843
return [];
3944
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ let integrationListeningPort: number | null = null;
4747
const TestIntegrationNet = Layer.succeed(Net.NetService, {
4848
canListenOnHost: () => Effect.succeed(true),
4949
isPortAvailableOnLoopback: (port) => Effect.sync(() => port !== integrationListeningPort),
50+
hasListenerOnHost: (port) => Effect.sync(() => port === integrationListeningPort),
5051
reserveLoopbackPort: () => Effect.succeed(40_000),
5152
findAvailablePort: (preferred) => Effect.succeed(preferred),
5253
});
@@ -108,6 +109,7 @@ const makeLsofScannerLayer = (input: {
108109
Layer.succeed(Net.NetService, {
109110
canListenOnHost: () => Effect.succeed(true),
110111
isPortAvailableOnLoopback: () => Effect.succeed(true),
112+
hasListenerOnHost: () => Effect.succeed(false),
111113
reserveLoopbackPort: () => Effect.succeed(40_000),
112114
findAvailablePort: (preferred) => Effect.succeed(preferred),
113115
}),

‎apps/web/src/components/chat/OpenInPicker.tsx‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,13 @@ export const OpenInPicker = memo(function OpenInPicker({
238238
absolutePath: openInCwd,
239239
});
240240
if (url === undefined) return;
241-
openRemoteEditorUrl(url);
242-
markRemoteHintSeen();
243-
setPreferredEditor(editor);
241+
// Only record hint-seen/preferred when the shell actually accepted
242+
// the URL (an older desktop build can refuse the editor scheme).
243+
void openRemoteEditorUrl(url).then((opened) => {
244+
if (!opened) return;
245+
markRemoteHintSeen();
246+
setPreferredEditor(editor);
247+
});
244248
return;
245249
}
246250
const result = openInEditorMutation({

‎apps/web/src/remoteOpen.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe("resolveRemoteOpenState", () => {
3030
resolveRemoteOpenState({
3131
target: primaryTarget("http://127.0.0.1:8000"),
3232
sshAlias: null,
33+
isDesktopRenderer: false,
3334
remoteOpenTargets: TAILSCALE_TARGETS,
3435
}),
3536
).toEqual({ mode: "local-exec" });
@@ -40,6 +41,7 @@ describe("resolveRemoteOpenState", () => {
4041
resolveRemoteOpenState({
4142
target: primaryTarget("https://sol.tail1234.ts.net"),
4243
sshAlias: null,
44+
isDesktopRenderer: false,
4345
remoteOpenTargets: TAILSCALE_TARGETS,
4446
}),
4547
).toEqual({
@@ -48,6 +50,19 @@ describe("resolveRemoteOpenState", () => {
4850
});
4951
});
5052

53+
it("keeps exec behavior for the desktop app's own primary even on a NAT URL", () => {
54+
// wsl-only mode binds the primary to the WSL2 NAT address; it is still
55+
// this machine because the desktop app manages its own primary backend.
56+
expect(
57+
resolveRemoteOpenState({
58+
target: primaryTarget("http://172.29.112.1:14369"),
59+
sshAlias: null,
60+
isDesktopRenderer: true,
61+
remoteOpenTargets: TAILSCALE_TARGETS,
62+
}),
63+
).toEqual({ mode: "local-exec" });
64+
});
65+
5166
it("keeps exec behavior for desktop-local secondary backends", () => {
5267
expect(
5368
resolveRemoteOpenState({
@@ -57,6 +72,7 @@ describe("resolveRemoteOpenState", () => {
5772
connectionId: "local:wsl-1",
5873
}),
5974
sshAlias: null,
75+
isDesktopRenderer: false,
6076
remoteOpenTargets: TAILSCALE_TARGETS,
6177
}),
6278
).toEqual({ mode: "local-exec" });
@@ -71,6 +87,7 @@ describe("resolveRemoteOpenState", () => {
7187
connectionId: "ssh-1",
7288
}),
7389
sshAlias: "sol",
90+
isDesktopRenderer: true,
7491
remoteOpenTargets: TAILSCALE_TARGETS,
7592
}),
7693
).toEqual({ mode: "remote-links", host: { kind: "ssh-alias", host: "sol" } });
@@ -82,6 +99,7 @@ describe("resolveRemoteOpenState", () => {
8299
resolveRemoteOpenState({
83100
target: new RelayConnectionTarget({ environmentId, label: "sol" }),
84101
sshAlias: null,
102+
isDesktopRenderer: false,
85103
remoteOpenTargets,
86104
}),
87105
).toEqual({ mode: "remote-unavailable" });
@@ -93,6 +111,7 @@ describe("resolveRemoteOpenState", () => {
93111
resolveRemoteOpenState({
94112
target: null,
95113
sshAlias: null,
114+
isDesktopRenderer: false,
96115
remoteOpenTargets: undefined,
97116
}),
98117
).toEqual({ mode: "local-exec" });

‎apps/web/src/remoteOpen.ts‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,23 @@ export function resolveRemoteOpenState(input: {
5353
readonly sshAlias: string | null;
5454
/** Server-advertised hosts; undefined on servers that predate the feature. */
5555
readonly remoteOpenTargets: ReadonlyArray<RemoteOpenTarget> | undefined;
56+
/** True when running inside the desktop app's renderer. */
57+
readonly isDesktopRenderer: boolean;
5658
}): RemoteOpenState {
5759
const { target } = input;
5860
// No catalog entry: keep today's exec behavior rather than guessing.
5961
if (target === null) {
6062
return LOCAL_EXEC;
6163
}
6264
if (target._tag === "PrimaryConnectionTarget") {
65+
// The desktop app manages its own primary backend, so it is always on
66+
// this machine even when its URL is not loopback (wsl-only mode binds
67+
// the WSL2 NAT address). In a browser, a loopback primary means the
68+
// browser runs on the serving machine; a tailnet/LAN URL means remote.
69+
if (input.isDesktopRenderer) {
70+
return LOCAL_EXEC;
71+
}
6372
const hostname = parseHostname(target.httpBaseUrl);
64-
// A loopback primary means the browser (or desktop renderer) runs on the
65-
// machine that serves it. A tailnet/LAN URL means we are remote even
66-
// though the target is "primary".
6773
if (hostname !== null && isLoopbackHostname(hostname)) {
6874
return LOCAL_EXEC;
6975
}
@@ -95,6 +101,7 @@ export function useRemoteOpenState(environmentId: EnvironmentId | null): RemoteO
95101
target: presentation.entry.target,
96102
sshAlias,
97103
remoteOpenTargets: presentation.serverConfig?.remoteOpenTargets,
104+
isDesktopRenderer: window.desktopBridge !== undefined,
98105
});
99106
}, [presentation]);
100107
}
@@ -151,14 +158,22 @@ export function useRemoteCapableEditors(): ReadonlyArray<EditorId> {
151158
* shell so the OS handler opens without navigating the renderer; in a
152159
* browser, assign the location — unlike window.open this does not leave a
153160
* blank tab behind.
161+
*
162+
* Resolves false when the desktop shell refused the URL (e.g. an older
163+
* build whose protocol allowlist predates editor schemes) so callers do not
164+
* record a successful open that never happened.
154165
*/
155-
export function openRemoteEditorUrl(url: string): void {
166+
export async function openRemoteEditorUrl(url: string): Promise<boolean> {
156167
const bridge = window.desktopBridge;
157168
if (bridge !== undefined) {
158-
void bridge.openExternal(url);
159-
return;
169+
try {
170+
return await bridge.openExternal(url);
171+
} catch {
172+
return false;
173+
}
160174
}
161175
window.location.assign(url);
176+
return true;
162177
}
163178

164179
/**

0 commit comments

Comments
 (0)