Skip to content

Commit 52d621a

Browse files
KinanLakmaria-rcksjuliusmarminge
authored
fix(desktop,web): Open in runs locally for a background service sharing the desktop's T3 home (#21661)
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent afb4f91 commit 52d621a

10 files changed

Lines changed: 114 additions & 22 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ import {
152152
import { isMarkdownFileLinkLabel } from "@t3tools/shared/markdownLinks";
153153
import { isElectron } from "../env";
154154
import { readLocalApi } from "../localApi";
155+
import { appAtomRegistry } from "../rpc/atomRegistry";
156+
import { hostEnvironmentIdAtom } from "../state/primaryEnvironment";
155157
import { cn } from "../lib/utils";
156158
import { resolveRemoteOpenState, useRemoteOpenResolution } from "../remoteOpen";
157159
import { useRightPanelStore } from "../rightPanelStore";
@@ -619,6 +621,7 @@ function useChatMarkdownState({
619621
sshTarget: null,
620622
remoteOpenTargets: undefined,
621623
isDesktopRenderer: window.desktopBridge !== undefined,
624+
hostEnvironmentId: appAtomRegistry.get(hostEnvironmentIdAtom),
622625
}).mode !== "local-exec"
623626
) {
624627
description = `The file was not found on ${target.label} at ${error.checkedPath ?? error.resource.path}.`;

‎apps/web/src/components/ChatView.logic.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { shouldShowOpenInPicker } from "./chat/OpenInPicker.logic";
12
import {
23
boundAttachmentIds,
34
buildExpiredTerminalContextToastCopy,
@@ -1216,3 +1217,25 @@ describe("deriveAvailableRevertTurnCounts", () => {
12161217
expect(deriveAvailableRevertTurnCounts(null).size).toBe(0);
12171218
});
12181219
});
1220+
1221+
describe("Open in for the host environment", () => {
1222+
it("shows both local environments when the primary has a separate WSL home", () => {
1223+
const primaryEnvironmentId = EnvironmentId.make("wsl");
1224+
const hostEnvironmentId = EnvironmentId.make("host");
1225+
for (const [activeThreadEnvironmentId, visible] of [
1226+
[primaryEnvironmentId, true],
1227+
[hostEnvironmentId, true],
1228+
[EnvironmentId.make("other"), false],
1229+
] as const) {
1230+
expect(
1231+
shouldShowOpenInPicker({
1232+
activeProjectName: "Project",
1233+
activeThreadEnvironmentId,
1234+
primaryEnvironmentId,
1235+
hostEnvironmentId,
1236+
remoteOpenMode: "local-exec",
1237+
}),
1238+
).toBe(visible);
1239+
}
1240+
});
1241+
});

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,8 @@ import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../termina
231231
import { useKnownTerminalSessions, useThreadRunningTerminalIds } from "../state/terminalSessions";
232232
import { useEnvironmentQueryRefresh } from "../state/query";
233233
import { useEnvironmentScope } from "~/state/session";
234-
import {
235-
primaryServerAvailableEditorsAtom,
236-
primaryServerKeybindingsAtom,
237-
serverEnvironment,
238-
} from "../state/server";
234+
import { primaryServerKeybindingsAtom, serverEnvironment } from "../state/server";
235+
import { hostEnvironmentIdAtom } from "../state/primaryEnvironment";
239236
import { threadEnvironment } from "../state/threads";
240237
import { resolveProviderSkillsForCwd } from "@t3tools/client-runtime/providerSkills";
241238
import { chatGitStatusAtom, NO_CHAT_GIT_STATUS_ATOM, vcsEnvironment } from "../state/vcs";
@@ -1157,7 +1154,6 @@ export default function ChatView(props: ChatViewProps) {
11571154
useRightPanelStore.getState().reconcileFileSurfaces(activeThreadRef, activeProject !== null);
11581155
}, [activeEnvironmentBootstrapComplete, activeProject, activeThreadRef]);
11591156

1160-
const primaryEnvironmentId = primaryEnvironment?.environmentId ?? null;
11611157
const activeEnvironment =
11621158
activeThread == null ? null : (environmentById.get(activeThread.environmentId) ?? null);
11631159
const activeEnvironmentUnavailable =
@@ -1612,17 +1608,21 @@ export default function ChatView(props: ChatViewProps) {
16121608
const scriptKeybindings =
16131609
useAtomValue(serverEnvironment.configValueAtom(environmentId))?.keybindings ??
16141610
DEFAULT_RESOLVED_KEYBINDINGS;
1615-
const availableEditors = useAtomValue(primaryServerAvailableEditorsAtom);
1616-
const remoteOpenState = useRemoteOpenState(activeThread?.environmentId ?? environmentId);
1611+
const openInEnvironmentId = activeThread?.environmentId ?? environmentId;
1612+
const availableEditors =
1613+
useAtomValue(serverEnvironment.configValueAtom(openInEnvironmentId))?.availableEditors ?? [];
1614+
const hostEnvironmentId = useAtomValue(hostEnvironmentIdAtom);
1615+
const remoteOpenState = useRemoteOpenState(openInEnvironmentId);
16171616
const showOpenInPicker = shouldShowOpenInPicker({
16181617
activeProjectName: activeProject?.title,
1619-
activeThreadEnvironmentId: activeThread?.environmentId ?? environmentId,
1620-
primaryEnvironmentId,
1618+
activeThreadEnvironmentId: openInEnvironmentId,
1619+
primaryEnvironmentId: primaryEnvironment?.environmentId ?? null,
1620+
hostEnvironmentId,
16211621
remoteOpenMode: remoteOpenState.mode,
16221622
});
16231623
useOpenFavoriteEditorShortcut({
16241624
enabled: showOpenInPicker && !backgroundThread,
1625-
environmentId: activeThread?.environmentId ?? environmentId,
1625+
environmentId: openInEnvironmentId,
16261626
keybindings,
16271627
availableEditors,
16281628
openInCwd: gitCwd,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { EnvironmentId } from "@t3tools/contracts";
2+
import { describe, expect, it } from "vite-plus/test";
3+
4+
import { shouldShowOpenInPicker } from "./OpenInPicker.logic";
5+
6+
describe("shouldShowOpenInPicker", () => {
7+
it("shows both local environments when the primary has a separate WSL home", () => {
8+
const primaryEnvironmentId = EnvironmentId.make("wsl");
9+
const hostEnvironmentId = EnvironmentId.make("host");
10+
for (const [activeThreadEnvironmentId, visible] of [
11+
[primaryEnvironmentId, true],
12+
[hostEnvironmentId, true],
13+
[EnvironmentId.make("other"), false],
14+
] as const) {
15+
expect(
16+
shouldShowOpenInPicker({
17+
activeProjectName: "Project",
18+
activeThreadEnvironmentId,
19+
primaryEnvironmentId,
20+
hostEnvironmentId,
21+
remoteOpenMode: "local-exec",
22+
}),
23+
).toBe(visible);
24+
}
25+
});
26+
});

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ export function shouldShowOpenInPicker(input: {
66
readonly activeProjectName: string | undefined;
77
readonly activeThreadEnvironmentId: EnvironmentId;
88
readonly primaryEnvironmentId: EnvironmentId | null;
9+
readonly hostEnvironmentId: string | null;
910
readonly remoteOpenMode: RemoteOpenMode;
1011
}): boolean {
1112
if (!input.activeProjectName) return false;
1213
if (
13-
input.primaryEnvironmentId !== null &&
14-
input.activeThreadEnvironmentId === input.primaryEnvironmentId
14+
input.activeThreadEnvironmentId === input.primaryEnvironmentId ||
15+
input.activeThreadEnvironmentId === input.hostEnvironmentId
1516
) {
1617
return true;
1718
}

‎apps/web/src/components/files/FilePreviewPanel.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import { buildFileReviewComment } from "~/reviewCommentContext";
7171
import { assetEnvironment } from "~/state/assets";
7272
import { usePreviewAvailable } from "~/browser/previewRuntime";
7373
import { useEnvironmentHttpBaseUrl, usePrimaryEnvironmentId } from "~/state/environments";
74+
import { hostEnvironmentIdAtom } from "~/state/primaryEnvironment";
7475
import { previewEnvironment } from "~/state/preview";
7576
import { serverEnvironment } from "~/state/server";
7677
import { useEnvironmentScope } from "~/state/session";
@@ -1025,6 +1026,7 @@ export default function FilePreviewPanel({
10251026
const wordWrap = wordWrapOverride ?? defaultWordWrap;
10261027
const canOperatePreview = useEnvironmentScope(environmentId, AuthPreviewOperateScope);
10271028
const primaryEnvironmentId = usePrimaryEnvironmentId();
1029+
const hostEnvironmentId = useAtomValue(hostEnvironmentIdAtom);
10281030
const remoteOpenState = useRemoteOpenState(environmentId);
10291031
const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId));
10301032
const environmentHttpBaseUrl = useEnvironmentHttpBaseUrl(environmentId);
@@ -1281,7 +1283,9 @@ export default function FilePreviewPanel({
12811283
</div>
12821284
</ScrollArea>
12831285
{absolutePath &&
1284-
(environmentId === primaryEnvironmentId || remoteOpenState.mode !== "local-exec") ? (
1286+
(environmentId === primaryEnvironmentId ||
1287+
environmentId === hostEnvironmentId ||
1288+
remoteOpenState.mode !== "local-exec") ? (
12851289
<OpenInPicker
12861290
environmentId={environmentId}
12871291
keybindings={keybindings}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe("resolveRemoteOpenState", () => {
3131
sshTarget: null,
3232
isDesktopRenderer: false,
3333
remoteOpenTargets: TAILSCALE_TARGETS,
34+
hostEnvironmentId: null,
3435
}),
3536
).toEqual({ mode: "local-exec" });
3637
});
@@ -42,6 +43,7 @@ describe("resolveRemoteOpenState", () => {
4243
sshTarget: null,
4344
isDesktopRenderer: false,
4445
remoteOpenTargets: TAILSCALE_TARGETS,
46+
hostEnvironmentId: null,
4547
}),
4648
).toEqual({
4749
mode: "remote-links",
@@ -57,10 +59,40 @@ describe("resolveRemoteOpenState", () => {
5759
sshTarget: null,
5860
isDesktopRenderer: true,
5961
remoteOpenTargets: TAILSCALE_TARGETS,
62+
hostEnvironmentId: null,
6063
}),
6164
).toEqual({ mode: "local-exec" });
6265
});
6366

67+
it("keeps exec behavior for the environment that owns the desktop's T3 home", () => {
68+
// With Local environment off, a background service on this machine can own the
69+
// desktop's home and still be reached through T3 Connect.
70+
expect(
71+
resolveRemoteOpenState({
72+
target: new RelayConnectionTarget({ environmentId, label: "sol" }),
73+
sshTarget: null,
74+
isDesktopRenderer: true,
75+
remoteOpenTargets: TAILSCALE_TARGETS,
76+
hostEnvironmentId: environmentId,
77+
}),
78+
).toEqual({ mode: "local-exec" });
79+
});
80+
81+
it("uses deep links for other environments when the desktop home has its own", () => {
82+
expect(
83+
resolveRemoteOpenState({
84+
target: new RelayConnectionTarget({ environmentId, label: "sol" }),
85+
sshTarget: null,
86+
isDesktopRenderer: true,
87+
remoteOpenTargets: TAILSCALE_TARGETS,
88+
hostEnvironmentId: EnvironmentId.make("environment-host"),
89+
}),
90+
).toEqual({
91+
mode: "remote-links",
92+
host: { kind: "tailscale", host: "sol.tail1234.ts.net" },
93+
});
94+
});
95+
6496
it("leaves a bare desktop SSH alias for the editor to resolve through SSH config", () => {
6597
const state = resolveRemoteOpenState({
6698
target: new SshConnectionTarget({ environmentId, label: "sol", connectionId: "ssh-1" }),
@@ -69,6 +101,7 @@ describe("resolveRemoteOpenState", () => {
69101
remoteOpenTargets: [
70102
{ kind: "tailscale", host: "sol.tail1234.ts.net", username: "server-user" },
71103
],
104+
hostEnvironmentId: null,
72105
});
73106
expect(state).toEqual({ mode: "remote-links", host: { kind: "ssh-alias", host: "sol" } });
74107
if (state.mode !== "remote-links") throw new Error("Expected a remote link");
@@ -97,6 +130,7 @@ describe("resolveRemoteOpenState", () => {
97130
sshTarget,
98131
isDesktopRenderer: true,
99132
remoteOpenTargets: TAILSCALE_TARGETS,
133+
hostEnvironmentId: null,
100134
});
101135
expect(state.mode).toBe("remote-links");
102136
if (state.mode !== "remote-links") throw new Error("Expected a remote link");
@@ -121,6 +155,7 @@ describe("resolveRemoteOpenState", () => {
121155
remoteOpenTargets: [
122156
{ kind: "tailscale", host: "server.tail1234.ts.net", username: "admin-user" },
123157
],
158+
hostEnvironmentId: null,
124159
});
125160
expect(state.mode).toBe("remote-links");
126161
if (state.mode !== "remote-links") throw new Error("Expected a remote link");
@@ -137,6 +172,7 @@ describe("resolveRemoteOpenState", () => {
137172
sshTarget: null,
138173
isDesktopRenderer: false,
139174
remoteOpenTargets,
175+
hostEnvironmentId: null,
140176
}),
141177
).toEqual({ mode: "remote-unavailable" });
142178
}

‎apps/web/src/remoteOpen.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { isDesktopLocalConnectionTarget } from "~/connection/desktopLocal";
1414
import { isLoopbackHostname } from "~/environments/primary/target";
1515
import { useLocalStorage } from "~/hooks/useLocalStorage";
1616
import { environmentPresentations } from "~/state/presentation";
17+
import { hostEnvironmentIdAtom } from "~/state/primaryEnvironment";
1718

1819
export interface RemoteOpenHost {
1920
readonly kind: "ssh-alias" | RemoteOpenTarget["kind"];
@@ -53,9 +54,10 @@ export function resolveRemoteOpenState(input: {
5354
readonly sshTarget: { readonly alias: string; readonly username: string | null } | null;
5455
readonly remoteOpenTargets: ReadonlyArray<RemoteOpenTarget> | undefined;
5556
readonly isDesktopRenderer: boolean;
57+
readonly hostEnvironmentId: string | null;
5658
}): RemoteOpenState {
5759
const { target } = input;
58-
if (target === null) {
60+
if (target === null || target.environmentId === input.hostEnvironmentId) {
5961
return LOCAL_EXEC;
6062
}
6163
if (target._tag === "PrimaryConnectionTarget") {
@@ -120,6 +122,7 @@ const remoteOpenResolutionAtom = Atom.family((environmentId: EnvironmentId) =>
120122
profile !== null && profile._tag === "SshConnectionProfile" ? profile.target : null,
121123
remoteOpenTargets: presentation?.serverConfig?.remoteOpenTargets,
122124
isDesktopRenderer: window.desktopBridge !== undefined,
125+
hostEnvironmentId: get(hostEnvironmentIdAtom),
123126
}),
124127
isResolved: true,
125128
};

‎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,
@@ -53,7 +52,6 @@ interface PrimaryServerState {
5352
readonly welcome: ServerLifecycleWelcomePayload | null;
5453
}
5554

56-
const EMPTY_AVAILABLE_EDITORS: ReadonlyArray<EditorId> = [];
5755
export const EMPTY_SERVER_PROVIDERS: ReadonlyArray<ServerProvider> = [];
5856
const EMPTY_PRIMARY_SERVER_STATE: PrimaryServerState = {
5957
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
const themeServerConfigAtom = Atom.make((get): ServerConfig | null => {

‎docs/user/remote-access.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,9 @@ desktop app, open **Settings → Environments** and switch off **Local
465465
environment**. T3 Code restarts without a local server: no local agents or terminals run, WSL
466466
backends stay off, and other devices can no longer connect to this computer. Your projects,
467467
history, and saved connections are kept, and you keep working through pairing, T3 Connect, or SSH.
468+
If a [background service](./background-service.md) on this computer uses the same T3 home, the
469+
app still treats it as this computer: **Open in** and file reveal for its projects run here,
470+
whichever connection you reach it through.
468471

469472
Switch **Local environment** back on in the same place to restart with your previous local
470473
settings.

0 commit comments

Comments
 (0)