Skip to content

Commit 19798e5

Browse files
committed
fix(web): editors open locally for a server saved at 127.0.0.1
A saved environment reached through a loopback route, such as a 127.0.0.1 route next to T3 Connect on app.t3.codes, was treated as remote: the Open in menu offered only VS Code and opened it over SSH. The route in use now decides, so a loopback route lets the server launch the editor, and the menu lists that server's own editors instead of the primary server's. The last answer stands while the connection is re-established, so the menu does not flicker.
1 parent d53873b commit 19798e5

6 files changed

Lines changed: 110 additions & 56 deletions

File tree

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ import { isMarkdownFileLinkLabel } from "@t3tools/shared/markdownLinks";
146146
import { isElectron } from "../env";
147147
import { readLocalApi } from "../localApi";
148148
import { cn } from "../lib/utils";
149-
import { resolveRemoteOpenState, useRemoteOpenResolution } from "../remoteOpen";
149+
import { readRemoteOpenState, useRemoteOpenResolution } from "../remoteOpen";
150150
import { useRightPanelStore } from "../rightPanelStore";
151151
import { readThreadShell, useProjects } from "../state/entities";
152152
import { serverEnvironment } from "../state/server";
@@ -554,15 +554,7 @@ function useChatMarkdownState({
554554
) {
555555
// The connection may resolve while the media lookup is pending.
556556
const target = readPreparedConnection(environmentId)?.target ?? null;
557-
if (
558-
target !== null &&
559-
resolveRemoteOpenState({
560-
target,
561-
sshTarget: null,
562-
remoteOpenTargets: undefined,
563-
isDesktopRenderer: window.desktopBridge !== undefined,
564-
}).mode !== "local-exec"
565-
) {
557+
if (target !== null && readRemoteOpenState(environmentId).mode !== "local-exec") {
566558
description = `The file was not found on ${target.label} at ${error.checkedPath ?? error.resource.path}.`;
567559
}
568560
}

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import {
4343
latestExecutedRun,
4444
latestRootProviderFailure,
4545
} from "@t3tools/shared/orchestrationV2ThreadError";
46-
import type { UsageLimitSourceSnapshots } from "@t3tools/contracts";
46+
import type { EditorId, UsageLimitSourceSnapshots } from "@t3tools/contracts";
4747
import { feedbackBannerItem } from "./chat/ComposerFeedback";
4848
import { useUsageLimitsPanel } from "./chat/useUsageLimitsPanel";
4949
import { useWorktreeSetup } from "./chat/useWorktreeSetup";
@@ -217,11 +217,7 @@ import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../termina
217217
import { useKnownTerminalSessions, useThreadRunningTerminalIds } from "../state/terminalSessions";
218218
import { useEnvironmentQuery } from "../state/query";
219219
import { useEnvironmentScope } from "~/state/session";
220-
import {
221-
primaryServerAvailableEditorsAtom,
222-
primaryServerKeybindingsAtom,
223-
serverEnvironment,
224-
} from "../state/server";
220+
import { primaryServerKeybindingsAtom, serverEnvironment } from "../state/server";
225221
import { threadEnvironment } from "../state/threads";
226222
import { resolveProviderSkillsForCwd } from "@t3tools/client-runtime/providerSkills";
227223
import { vcsEnvironment } from "../state/vcs";
@@ -266,8 +262,6 @@ import {
266262
resolveScrollToEndClearance,
267263
} from "./composerFooterLayout";
268264
import { ChatHeader } from "./chat/ChatHeader";
269-
import { useRemoteOpenState } from "~/remoteOpen";
270-
import { shouldShowOpenInPicker } from "./chat/OpenInPicker.logic";
271265
import { useOpenFavoriteEditorShortcut } from "./chat/OpenInPickerShortcut";
272266
import {
273267
PanelLayoutControls,
@@ -372,6 +366,7 @@ const EMPTY_PROVIDER_SKILLS: ServerProvider["skills"] = [];
372366
const EMPTY_AUTO_UPDATE_ENVIRONMENTS: ReadonlyArray<EnvironmentPresentation> = [];
373367

374368
const EMPTY_PENDING_FILE_SURFACE_IDS: ReadonlySet<string> = new Set();
369+
const EMPTY_AVAILABLE_EDITORS: ReadonlyArray<EditorId> = [];
375370

376371
type ChatViewProps =
377372
| {
@@ -1109,7 +1104,6 @@ export default function ChatView(props: ChatViewProps) {
11091104
useRightPanelStore.getState().reconcileFileSurfaces(activeThreadRef, activeProject !== null);
11101105
}, [activeEnvironmentBootstrapComplete, activeProject, activeThreadRef]);
11111106

1112-
const primaryEnvironmentId = primaryEnvironment?.environmentId ?? null;
11131107
useEffect(() => {
11141108
if (!activeThreadRef || !activeProjectRef) return;
11151109
registerFaviconProjectForThread(activeThreadRef, activeProjectRef);
@@ -1596,14 +1590,11 @@ export default function ChatView(props: ChatViewProps) {
15961590
const scriptKeybindings =
15971591
useAtomValue(serverEnvironment.configValueAtom(environmentId))?.keybindings ??
15981592
DEFAULT_RESOLVED_KEYBINDINGS;
1599-
const availableEditors = useAtomValue(primaryServerAvailableEditorsAtom);
1600-
const remoteOpenState = useRemoteOpenState(activeThread?.environmentId ?? environmentId);
1601-
const showOpenInPicker = shouldShowOpenInPicker({
1602-
activeProjectName: activeProject?.title,
1603-
activeThreadEnvironmentId: activeThread?.environmentId ?? environmentId,
1604-
primaryEnvironmentId,
1605-
remoteOpenMode: remoteOpenState.mode,
1606-
});
1593+
// The thread's own server launches the editor, so its editor list fills the menu.
1594+
const availableEditors =
1595+
useAtomValue(serverEnvironment.configValueAtom(activeThread?.environmentId ?? environmentId))
1596+
?.availableEditors ?? EMPTY_AVAILABLE_EDITORS;
1597+
const showOpenInPicker = Boolean(activeProject?.title);
16071598
useOpenFavoriteEditorShortcut({
16081599
enabled: showOpenInPicker,
16091600
environmentId: activeThread?.environmentId ?? environmentId,

‎apps/web/src/components/chat/OpenInPicker.logic.ts‎

Lines changed: 0 additions & 19 deletions
This file was deleted.

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import {
2+
BearerConnectionProfile,
3+
BearerConnectionTarget,
24
PrimaryConnectionTarget,
35
RelayConnectionTarget,
46
SshConnectionTarget,
57
} from "@t3tools/client-runtime/connection";
68
import { buildRemoteOpenUrl, EnvironmentId } from "@t3tools/contracts";
9+
import * as Option from "effect/Option";
710
import { describe, expect, it } from "vite-plus/test";
811

912
import { resolveRemoteOpenState } from "./remoteOpen";
@@ -18,6 +21,19 @@ const primaryTarget = (httpBaseUrl: string) =>
1821
wsBaseUrl: httpBaseUrl.replace("http", "ws"),
1922
});
2023

24+
const bearerRoute = (httpBaseUrl: string) => ({
25+
target: new BearerConnectionTarget({ environmentId, label: "sol", connectionId: "bearer-1" }),
26+
profile: Option.some(
27+
new BearerConnectionProfile({
28+
connectionId: "bearer-1",
29+
environmentId,
30+
label: "sol",
31+
httpBaseUrl,
32+
wsBaseUrl: httpBaseUrl.replace("http", "ws"),
33+
}),
34+
),
35+
});
36+
2137
const TAILSCALE_TARGETS = [
2238
{ kind: "tailscale", host: "sol.tail1234.ts.net" },
2339
{ kind: "mdns", host: "sol.local" },
@@ -27,6 +43,7 @@ describe("resolveRemoteOpenState", () => {
2743
it("keeps exec behavior for a loopback primary target", () => {
2844
expect(
2945
resolveRemoteOpenState({
46+
connectedRoute: null,
3047
target: primaryTarget("http://127.0.0.1:8000"),
3148
sshTarget: null,
3249
isDesktopRenderer: false,
@@ -35,9 +52,54 @@ describe("resolveRemoteOpenState", () => {
3552
).toEqual({ mode: "local-exec" });
3653
});
3754

55+
it.each(["http://127.0.0.1:3773", "http://127.0.1.1:3773", "http://localhost:3773"])(
56+
"keeps exec behavior when the route in use is a saved loopback route (%s)",
57+
(httpBaseUrl) => {
58+
// A browser on app.t3.codes can save T3 Connect first and a loopback address as another route.
59+
expect(
60+
resolveRemoteOpenState({
61+
target: new RelayConnectionTarget({ environmentId, label: "sol" }),
62+
connectedRoute: bearerRoute(httpBaseUrl),
63+
sshTarget: null,
64+
isDesktopRenderer: false,
65+
remoteOpenTargets: TAILSCALE_TARGETS,
66+
}),
67+
).toEqual({ mode: "local-exec" });
68+
},
69+
);
70+
71+
it("uses deep links when the route in use is a saved network route", () => {
72+
expect(
73+
resolveRemoteOpenState({
74+
target: new RelayConnectionTarget({ environmentId, label: "sol" }),
75+
connectedRoute: bearerRoute("http://192.168.1.20:3773"),
76+
sshTarget: null,
77+
isDesktopRenderer: false,
78+
remoteOpenTargets: TAILSCALE_TARGETS,
79+
}),
80+
).toEqual({
81+
mode: "remote-links",
82+
host: { kind: "tailscale", host: "sol.tail1234.ts.net" },
83+
});
84+
});
85+
86+
it("uses deep links for an SSH route even though it rides a loopback forward", () => {
87+
const target = new SshConnectionTarget({ environmentId, label: "sol", connectionId: "ssh-1" });
88+
expect(
89+
resolveRemoteOpenState({
90+
target,
91+
connectedRoute: { target, profile: Option.none() },
92+
sshTarget: { alias: "sol", username: null },
93+
isDesktopRenderer: true,
94+
remoteOpenTargets: TAILSCALE_TARGETS,
95+
}).mode,
96+
).toBe("remote-links");
97+
});
98+
3899
it("uses deep links for a primary target reached over the network", () => {
39100
expect(
40101
resolveRemoteOpenState({
102+
connectedRoute: null,
41103
target: primaryTarget("https://sol.tail1234.ts.net"),
42104
sshTarget: null,
43105
isDesktopRenderer: false,
@@ -54,6 +116,7 @@ describe("resolveRemoteOpenState", () => {
54116
// this machine because the desktop app manages its own primary backend.
55117
expect(
56118
resolveRemoteOpenState({
119+
connectedRoute: null,
57120
target: primaryTarget("http://172.29.112.1:14369"),
58121
sshTarget: null,
59122
isDesktopRenderer: true,
@@ -64,6 +127,7 @@ describe("resolveRemoteOpenState", () => {
64127

65128
it("leaves a bare desktop SSH alias for the editor to resolve through SSH config", () => {
66129
const state = resolveRemoteOpenState({
130+
connectedRoute: null,
67131
target: new SshConnectionTarget({ environmentId, label: "sol", connectionId: "ssh-1" }),
68132
sshTarget: { alias: "sol", username: null },
69133
isDesktopRenderer: true,
@@ -94,6 +158,7 @@ describe("resolveRemoteOpenState", () => {
94158
{ alias: "admin-user@server", username: " " },
95159
])("keeps one login username in editor links for $alias (username: '$username')", (sshTarget) => {
96160
const state = resolveRemoteOpenState({
161+
connectedRoute: null,
97162
target: new SshConnectionTarget({ environmentId, label: "server", connectionId: "ssh-1" }),
98163
sshTarget,
99164
isDesktopRenderer: true,
@@ -116,6 +181,7 @@ describe("resolveRemoteOpenState", () => {
116181

117182
it("uses the advertised login username in editor links over a relay", () => {
118183
const state = resolveRemoteOpenState({
184+
connectedRoute: null,
119185
target: new RelayConnectionTarget({ environmentId, label: "server" }),
120186
sshTarget: null,
121187
isDesktopRenderer: false,
@@ -134,6 +200,7 @@ describe("resolveRemoteOpenState", () => {
134200
for (const remoteOpenTargets of [[], undefined] as const) {
135201
expect(
136202
resolveRemoteOpenState({
203+
connectedRoute: null,
137204
target: new RelayConnectionTarget({ environmentId, label: "sol" }),
138205
sshTarget: null,
139206
isDesktopRenderer: false,

‎apps/web/src/remoteOpen.ts‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import type { ConnectionTarget } from "@t3tools/client-runtime/connection";
1+
import {
2+
connectionRouteId,
3+
connectionRouteKind,
4+
connectionRoutes,
5+
type ConnectionRoute,
6+
type ConnectionTarget,
7+
} from "@t3tools/client-runtime/connection";
28
import {
39
REMOTE_CAPABLE_EDITOR_IDS,
410
type EditorId,
@@ -14,7 +20,9 @@ import { useEffect, useState } from "react";
1420
import { isDesktopLocalConnectionTarget } from "~/connection/desktopLocal";
1521
import { isLoopbackHostname } from "~/environments/primary/target";
1622
import { useLocalStorage } from "~/hooks/useLocalStorage";
23+
import { appAtomRegistry } from "~/rpc/atomRegistry";
1724
import { environmentPresentations } from "~/state/presentation";
25+
import { environmentSession } from "~/state/session";
1826

1927
export interface RemoteOpenHost {
2028
readonly kind: "ssh-alias" | RemoteOpenTarget["kind"];
@@ -51,6 +59,8 @@ function parseHostname(url: string): string | null {
5159

5260
export function resolveRemoteOpenState(input: {
5361
readonly target: ConnectionTarget | null;
62+
/** The saved route the client is connected through, which need not be the first one. */
63+
readonly connectedRoute: ConnectionRoute | null;
5464
readonly sshTarget: { readonly alias: string; readonly username: string | null } | null;
5565
readonly remoteOpenTargets: ReadonlyArray<RemoteOpenTarget> | undefined;
5666
readonly isDesktopRenderer: boolean;
@@ -59,6 +69,10 @@ export function resolveRemoteOpenState(input: {
5969
if (target === null) {
6070
return LOCAL_EXEC;
6171
}
72+
// A saved route to a loopback address reaches a server on this same machine.
73+
if (input.connectedRoute !== null && connectionRouteKind(input.connectedRoute) === "loopback") {
74+
return LOCAL_EXEC;
75+
}
6276
if (target._tag === "PrimaryConnectionTarget") {
6377
// The desktop app manages its own primary backend, so it is always on this machine even
6478
// when its URL is not loopback (wsl-only mode binds the WSL2 NAT address).
@@ -108,26 +122,38 @@ function remoteOpenStatesEqual(previous: RemoteOpenState, next: RemoteOpenState)
108122

109123
// The presentation changes with every server config push and reconnect. This
110124
// keeps the previous resolution while it is equal, so subscribers re-render only
111-
// when where files open actually changes.
125+
// when where files open actually changes. Between connections the route in use is
126+
// unknown, so the last resolution stands until a route connects again.
112127
const remoteOpenResolutionAtom = Atom.family((environmentId: EnvironmentId) =>
113128
Atom.make((get): RemoteOpenResolution => {
114129
const presentation = get(environmentPresentations.presentationAtom(environmentId));
115130
const entry = presentation?.entry ?? null;
116131
if (entry === null) {
117132
return UNRESOLVED_REMOTE_OPEN;
118133
}
134+
const previous = Option.getOrNull(get.self<RemoteOpenResolution>());
135+
const connection = Option.getOrNull(
136+
get(environmentSession.preparedConnectionValueAtom(environmentId)),
137+
);
138+
if (connection === null && previous !== null && previous.isResolved) {
139+
return previous;
140+
}
141+
const connectedRouteId = connection === null ? null : connectionRouteId(connection.target);
119142
const profile = Option.getOrNull(entry.profile);
120143
const next: RemoteOpenResolution = {
121144
state: resolveRemoteOpenState({
122145
target: entry.target,
146+
connectedRoute:
147+
connectionRoutes(entry).find(
148+
(route) => connectionRouteId(route.target) === connectedRouteId,
149+
) ?? null,
123150
sshTarget:
124151
profile !== null && profile._tag === "SshConnectionProfile" ? profile.target : null,
125152
remoteOpenTargets: presentation?.serverConfig?.remoteOpenTargets,
126153
isDesktopRenderer: window.desktopBridge !== undefined,
127154
}),
128155
isResolved: true,
129156
};
130-
const previous = Option.getOrNull(get.self<RemoteOpenResolution>());
131157
return previous !== null &&
132158
previous.isResolved &&
133159
remoteOpenStatesEqual(previous.state, next.state)
@@ -148,6 +174,10 @@ export function useRemoteOpenState(environmentId: EnvironmentId | null): RemoteO
148174
return useRemoteOpenResolution(environmentId).state;
149175
}
150176

177+
export function readRemoteOpenState(environmentId: EnvironmentId): RemoteOpenState {
178+
return appAtomRegistry.get(remoteOpenResolutionAtom(environmentId)).state;
179+
}
180+
151181
const REMOTE_FALLBACK_EDITORS: ReadonlyArray<EditorId> = ["vscode"];
152182

153183
let cachedProbedEditors: ReadonlyArray<EditorId> | null = null;

‎apps/web/src/state/server.ts‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
DEFAULT_SERVER_SETTINGS,
3-
type EditorId,
43
type EnvironmentId,
54
type EnvironmentTheme,
65
type ServerConfig,
@@ -55,7 +54,6 @@ interface PrimaryServerState {
5554
readonly welcome: ServerLifecycleWelcomePayload | null;
5655
}
5756

58-
const EMPTY_AVAILABLE_EDITORS: ReadonlyArray<EditorId> = [];
5957
export const EMPTY_SERVER_PROVIDERS: ReadonlyArray<ServerProvider> = [];
6058
const EMPTY_PRIMARY_SERVER_STATE: PrimaryServerState = {
6159
config: null,
@@ -166,11 +164,6 @@ export const primaryServerKeybindingsAtom = createShortcutKeybindingsAtom({
166164
environmentsAtom: environmentConnectionsAtom,
167165
}).pipe(Atom.withLabel("web-primary-server-keybindings"));
168166

169-
export const primaryServerAvailableEditorsAtom = Atom.make(
170-
(get): ReadonlyArray<EditorId> =>
171-
get(primaryServerConfigAtom)?.availableEditors ?? EMPTY_AVAILABLE_EDITORS,
172-
).pipe(Atom.withLabel("web-primary-server-available-editors"));
173-
174167
const EMPTY_ENVIRONMENT_THEMES: ReadonlyArray<EnvironmentTheme> = [];
175168

176169
export const primaryServerEnvironmentThemesAtom = Atom.make(

0 commit comments

Comments
 (0)