Skip to content

Commit 1312467

Browse files
committed
fix(web): Open in works on a service paired from the same T3 home
1 parent 01a8db0 commit 1312467

9 files changed

Lines changed: 110 additions & 2 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
getLocalEnvironmentBootstraps,
3838
getLocalEnvironmentBearerToken,
3939
installRelayTelemetryEnabled,
40+
getHomeEnvironmentId,
4041
getSystemLocale,
4142
getWindowFullscreenState,
4243
openExternal,
@@ -150,6 +151,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers"
150151
yield* ipc.handle(pasteAsText);
151152
yield* ipc.handle(revealWindow);
152153
yield* ipc.handle(probeRemoteEditors);
154+
yield* ipc.handle(getHomeEnvironmentId);
153155
yield* ipc.handle(getUpdateState);
154156
yield* ipc.handle(setUpdateChannel);
155157
yield* ipc.handle(downloadUpdate);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const CLI_COMMAND_UNINSTALL_CHANNEL = "desktop:cli-command-uninstall";
3434
export const GET_APP_BRANDING_CHANNEL = "desktop:get-app-branding";
3535
export const GET_RELAY_TELEMETRY_ENABLED_CHANNEL = "desktop:get-relay-telemetry-enabled";
3636
export const GET_SYSTEM_LOCALE_CHANNEL = "desktop:get-system-locale";
37+
export const GET_HOME_ENVIRONMENT_ID_CHANNEL = "desktop:get-home-environment-id";
3738
export const GET_LOCAL_ENVIRONMENT_BOOTSTRAPS_CHANNEL = "desktop:get-local-environment-bootstraps";
3839
export const GET_LOCAL_ENVIRONMENT_ENABLED_CHANNEL = "desktop:get-local-environment-enabled";
3940
export const SET_LOCAL_ENVIRONMENT_ENABLED_CHANNEL = "desktop:set-local-environment-enabled";

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ vi.mock("electron", () => ({
2020
BrowserWindow: { fromWebContents: ownerWindow },
2121
}));
2222

23+
import * as DesktopEnvironment from "../../app/DesktopEnvironment.ts";
2324
import * as DesktopBackendConfiguration from "../../backend/DesktopBackendConfiguration.ts";
2425
import * as DesktopBackendManager from "../../backend/DesktopBackendManager.ts";
2526
import * as DesktopBackendPool from "../../backend/DesktopBackendPool.ts";
@@ -33,6 +34,7 @@ import {
3334
getLocalEnvironmentBootstraps,
3435
installRelayTelemetryEnabled,
3536
pickProjectFavicon,
37+
getHomeEnvironmentId,
3638
probeRemoteEditors,
3739
readMedia,
3840
} from "./window.ts";
@@ -386,3 +388,20 @@ it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
386388
assert.include(editors, "webstorm");
387389
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
388390
);
391+
392+
it.effect("reads the home's environment id and treats an unwritten home as none", () =>
393+
Effect.gen(function* () {
394+
const fs = yield* FileSystem.FileSystem;
395+
const path = yield* Path.Path;
396+
const stateDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-home-environment-" });
397+
const read = getHomeEnvironmentId.handler(undefined).pipe(
398+
Effect.provideService(DesktopEnvironment.DesktopEnvironment, {
399+
path,
400+
stateDir,
401+
} as DesktopEnvironment.DesktopEnvironment["Service"]),
402+
);
403+
assert.isNull(yield* read);
404+
yield* fs.writeFileString(path.join(stateDir, "environment-id"), "environment-1\n");
405+
assert.equal(yield* read, "environment-1");
406+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
407+
);

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,22 @@ export const openSystemSettings = DesktopIpc.makeIpcMethod({
381381
}),
382382
});
383383

384+
export const getHomeEnvironmentId = DesktopIpc.makeIpcMethod({
385+
channel: IpcChannels.GET_HOME_ENVIRONMENT_ID_CHANNEL,
386+
payload: Schema.Undefined,
387+
result: Schema.NullOr(Schema.String),
388+
handler: Effect.fn("desktop.ipc.window.getHomeEnvironmentId")(function* () {
389+
const environment = yield* DesktopEnvironment.DesktopEnvironment;
390+
const fileSystem = yield* FileSystem.FileSystem;
391+
return yield* fileSystem
392+
.readFileString(environment.path.join(environment.stateDir, "environment-id"))
393+
.pipe(
394+
Effect.map((contents) => contents.trim() || null),
395+
Effect.orElseSucceed(() => null),
396+
);
397+
}),
398+
});
399+
384400
export const probeRemoteEditors = DesktopIpc.makeIpcMethod({
385401
channel: IpcChannels.PROBE_REMOTE_EDITORS_CHANNEL,
386402
payload: Schema.Undefined,

‎apps/desktop/src/preload.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ contextBridge.exposeInMainWorld("desktopBridge", {
208208
openSystemSettings: (pane: string) =>
209209
ipcRenderer.invoke(IpcChannels.OPEN_SYSTEM_SETTINGS_CHANNEL, pane),
210210
probeRemoteEditors: () => ipcRenderer.invoke(IpcChannels.PROBE_REMOTE_EDITORS_CHANNEL, undefined),
211+
getHomeEnvironmentId: () =>
212+
ipcRenderer.invoke(IpcChannels.GET_HOME_ENVIRONMENT_ID_CHANNEL, undefined),
211213
pasteAsText: () => ipcRenderer.invoke(IpcChannels.PASTE_AS_TEXT_CHANNEL, undefined),
212214
onMenuAction: (listener) => {
213215
const wrappedListener = (_event: Electron.IpcRendererEvent, action: unknown) => {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ import { isMarkdownFileLinkLabel } from "@t3tools/shared/markdownLinks";
150150
import { isElectron } from "../env";
151151
import { readLocalApi } from "../localApi";
152152
import { cn } from "../lib/utils";
153-
import { resolveRemoteOpenState, useRemoteOpenResolution } from "../remoteOpen";
153+
import {
154+
readDesktopHomeEnvironmentId,
155+
resolveRemoteOpenState,
156+
useRemoteOpenResolution,
157+
} from "../remoteOpen";
154158
import { useRightPanelStore } from "../rightPanelStore";
155159
import { readThreadShell, useProjects } from "../state/entities";
156160
import { serverEnvironment } from "../state/server";
@@ -601,6 +605,7 @@ function useChatMarkdownState({
601605
sshTarget: null,
602606
remoteOpenTargets: undefined,
603607
isDesktopRenderer: window.desktopBridge !== undefined,
608+
desktopHomeEnvironmentId: readDesktopHomeEnvironmentId(),
604609
}).mode !== "local-exec"
605610
) {
606611
description = `The file was not found on ${target.label} at ${error.checkedPath ?? error.resource.path}.`;

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
BearerConnectionTarget,
23
PrimaryConnectionTarget,
34
RelayConnectionTarget,
45
SshConnectionTarget,
@@ -30,6 +31,7 @@ describe("resolveRemoteOpenState", () => {
3031
target: primaryTarget("http://127.0.0.1:8000"),
3132
sshTarget: null,
3233
isDesktopRenderer: false,
34+
desktopHomeEnvironmentId: null,
3335
remoteOpenTargets: TAILSCALE_TARGETS,
3436
}),
3537
).toEqual({ mode: "local-exec" });
@@ -41,6 +43,7 @@ describe("resolveRemoteOpenState", () => {
4143
target: primaryTarget("https://sol.tail1234.ts.net"),
4244
sshTarget: null,
4345
isDesktopRenderer: false,
46+
desktopHomeEnvironmentId: null,
4447
remoteOpenTargets: TAILSCALE_TARGETS,
4548
}),
4649
).toEqual({
@@ -56,6 +59,7 @@ describe("resolveRemoteOpenState", () => {
5659
target: primaryTarget("http://172.29.112.1:14369"),
5760
sshTarget: null,
5861
isDesktopRenderer: true,
62+
desktopHomeEnvironmentId: null,
5963
remoteOpenTargets: TAILSCALE_TARGETS,
6064
}),
6165
).toEqual({ mode: "local-exec" });
@@ -66,6 +70,7 @@ describe("resolveRemoteOpenState", () => {
6670
target: new SshConnectionTarget({ environmentId, label: "sol", connectionId: "ssh-1" }),
6771
sshTarget: { alias: "sol", username: null },
6872
isDesktopRenderer: true,
73+
desktopHomeEnvironmentId: null,
6974
remoteOpenTargets: [
7075
{ kind: "tailscale", host: "sol.tail1234.ts.net", username: "server-user" },
7176
],
@@ -96,6 +101,7 @@ describe("resolveRemoteOpenState", () => {
96101
target: new SshConnectionTarget({ environmentId, label: "server", connectionId: "ssh-1" }),
97102
sshTarget,
98103
isDesktopRenderer: true,
104+
desktopHomeEnvironmentId: null,
99105
remoteOpenTargets: TAILSCALE_TARGETS,
100106
});
101107
expect(state.mode).toBe("remote-links");
@@ -118,6 +124,7 @@ describe("resolveRemoteOpenState", () => {
118124
target: new RelayConnectionTarget({ environmentId, label: "server" }),
119125
sshTarget: null,
120126
isDesktopRenderer: false,
127+
desktopHomeEnvironmentId: null,
121128
remoteOpenTargets: [
122129
{ kind: "tailscale", host: "server.tail1234.ts.net", username: "admin-user" },
123130
],
@@ -129,13 +136,41 @@ describe("resolveRemoteOpenState", () => {
129136
).toBe("cursor://vscode-remote/ssh-remote+admin-user%40server.tail1234.ts.net/work/project");
130137
});
131138

139+
it("keeps exec behavior for a paired server on the desktop's own T3 home", () => {
140+
const pairedTarget = new BearerConnectionTarget({
141+
environmentId,
142+
label: "laptop service",
143+
connectionId: "paired-1",
144+
});
145+
const input = {
146+
target: pairedTarget,
147+
sshTarget: null,
148+
remoteOpenTargets: undefined,
149+
isDesktopRenderer: true,
150+
};
151+
expect(resolveRemoteOpenState({ ...input, desktopHomeEnvironmentId: environmentId })).toEqual({
152+
mode: "local-exec",
153+
});
154+
expect(resolveRemoteOpenState({ ...input, desktopHomeEnvironmentId: "other-home" })).toEqual({
155+
mode: "remote-unavailable",
156+
});
157+
expect(
158+
resolveRemoteOpenState({
159+
...input,
160+
sshTarget: { alias: "laptop", username: null },
161+
desktopHomeEnvironmentId: environmentId,
162+
}),
163+
).toEqual({ mode: "remote-links", host: { kind: "ssh-alias", host: "laptop" } });
164+
});
165+
132166
it("reports unavailable when a remote environment advertises no hosts", () => {
133167
for (const remoteOpenTargets of [[], undefined] as const) {
134168
expect(
135169
resolveRemoteOpenState({
136170
target: new RelayConnectionTarget({ environmentId, label: "sol" }),
137171
sshTarget: null,
138172
isDesktopRenderer: false,
173+
desktopHomeEnvironmentId: null,
139174
remoteOpenTargets,
140175
}),
141176
).toEqual({ mode: "remote-unavailable" });

‎apps/web/src/remoteOpen.ts‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { useAtomValue } from "@effect/atom-react";
1313
import { isDesktopLocalConnectionTarget } from "~/connection/desktopLocal";
1414
import { isLoopbackHostname } from "~/environments/primary/target";
1515
import { useLocalStorage } from "~/hooks/useLocalStorage";
16+
import { appAtomRegistry } from "~/rpc/atomRegistry";
1617
import { environmentPresentations } from "~/state/presentation";
1718

1819
export interface RemoteOpenHost {
@@ -53,6 +54,7 @@ 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 desktopHomeEnvironmentId: string | null;
5658
}): RemoteOpenState {
5759
const { target } = input;
5860
if (target === null) {
@@ -67,7 +69,13 @@ export function resolveRemoteOpenState(input: {
6769
if (hostname !== null && isLoopbackHostname(hostname)) {
6870
return LOCAL_EXEC;
6971
}
70-
} else if (isDesktopLocalConnectionTarget(target)) {
72+
} else if (
73+
// A background service paired from the desktop shares its T3 home and environment id.
74+
(target._tag === "BearerConnectionTarget" &&
75+
input.sshTarget === null &&
76+
target.environmentId === input.desktopHomeEnvironmentId) ||
77+
isDesktopLocalConnectionTarget(target)
78+
) {
7179
return LOCAL_EXEC;
7280
}
7381

@@ -94,6 +102,22 @@ export function resolveRemoteOpenState(input: {
94102
return REMOTE_UNAVAILABLE;
95103
}
96104

105+
// Read once per page: the id is permanent once a server has written it to the home.
106+
const desktopHomeEnvironmentIdAtom = Atom.make((get): string | null => {
107+
const read = window.desktopBridge?.getHomeEnvironmentId;
108+
read?.().then(
109+
(id) => {
110+
if (id !== null) get.setSelf(id);
111+
},
112+
() => undefined,
113+
);
114+
return null;
115+
}).pipe(Atom.keepAlive, Atom.withLabel("desktop-home-environment-id"));
116+
117+
export function readDesktopHomeEnvironmentId(): string | null {
118+
return appAtomRegistry.get(desktopHomeEnvironmentIdAtom);
119+
}
120+
97121
function remoteOpenStatesEqual(previous: RemoteOpenState, next: RemoteOpenState): boolean {
98122
if (previous.mode !== next.mode) return false;
99123
if (previous.mode !== "remote-links" || next.mode !== "remote-links") return true;
@@ -120,6 +144,8 @@ const remoteOpenResolutionAtom = Atom.family((environmentId: EnvironmentId) =>
120144
profile !== null && profile._tag === "SshConnectionProfile" ? profile.target : null,
121145
remoteOpenTargets: presentation?.serverConfig?.remoteOpenTargets,
122146
isDesktopRenderer: window.desktopBridge !== undefined,
147+
desktopHomeEnvironmentId:
148+
entry.target._tag === "BearerConnectionTarget" ? get(desktopHomeEnvironmentIdAtom) : null,
123149
}),
124150
isResolved: true,
125151
};

‎packages/contracts/src/ipc.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,8 @@ export interface DesktopBridge {
11161116
openSystemSettings: (pane: SystemSettingsPane) => Promise<boolean>;
11171117
checkSystemPermission: (pane: SystemSettingsPane) => Promise<boolean>;
11181118
probeRemoteEditors: () => Promise<readonly EditorId[]>;
1119+
/** The environment id stored in this desktop's T3 home; null before any server has written it. */
1120+
getHomeEnvironmentId: () => Promise<string | null>;
11191121
pasteAsText: () => Promise<void>;
11201122
onMenuAction: (listener: (action: string) => void) => () => void;
11211123
onSnapShotEvent: (listener: (event: DesktopSnapShotEvent) => void) => () => void;

0 commit comments

Comments
 (0)