Skip to content

Commit 5de22fa

Browse files
committed
Merge remote-tracking branch 'github/main' into pr-31219
# Conflicts: # apps/server/src/orchestration-v2/ProjectionStore.ts # apps/server/src/orchestration-v2/threadHistoryPaging.ts
2 parents f4e863f + 67eebac commit 5de22fa

366 files changed

Lines changed: 11228 additions & 3392 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/desktop/src/app/DesktopApp.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Cause from "effect/Cause";
2+
import * as Config from "effect/Config";
23
import * as Duration from "effect/Duration";
34
import * as Effect from "effect/Effect";
45
import * as Option from "effect/Option";
@@ -42,6 +43,11 @@ const DEFAULT_DESKTOP_BACKEND_PORT = 3773;
4243
const MAX_TCP_PORT = 65_535;
4344
const DESKTOP_BACKEND_PORT_PROBE_HOSTS = ["127.0.0.1", "0.0.0.0", "::"] as const;
4445

46+
const LinuxDesktopSessionConfig = Config.all({
47+
current: Config.String("XDG_CURRENT_DESKTOP").pipe(Config.option),
48+
session: Config.String("XDG_SESSION_DESKTOP").pipe(Config.option),
49+
});
50+
4551
const makeDesktopRunId = SecureRandom.SecureRandom.pipe(
4652
Effect.flatMap((random) => random.uuidV4()),
4753
Effect.map((value) => value.replaceAll("-", "").slice(0, 12)),
@@ -307,12 +313,13 @@ const startup = Effect.gen(function* () {
307313
yield* desktopSettings.load;
308314

309315
if (linuxElectronOptions !== null) {
316+
const xdgDesktop = yield* LinuxDesktopSessionConfig;
310317
yield* logStartupInfo("linux password store configured", {
311318
passwordStore: hasCommandLinePasswordStore
312319
? "command-line"
313320
: (linuxElectronOptions.passwordStore ?? "electron-default"),
314-
xdgCurrentDesktop: process.env.XDG_CURRENT_DESKTOP ?? null,
315-
xdgSessionDesktop: process.env.XDG_SESSION_DESKTOP ?? null,
321+
xdgCurrentDesktop: Option.getOrNull(xdgDesktop.current),
322+
xdgSessionDesktop: Option.getOrNull(xdgDesktop.session),
316323
});
317324
}
318325

‎apps/desktop/src/app/DesktopCliCommand.test.ts‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
2-
import { afterEach, beforeEach, expect, it } from "@effect/vitest";
2+
import { expect, it } from "@effect/vitest";
33
import * as HostProcess from "@t3tools/shared/HostProcess";
44
import * as Effect from "effect/Effect";
55
import * as FileSystem from "effect/FileSystem";
@@ -29,7 +29,13 @@ const environmentFor = (
2929
} as unknown as DesktopEnvironment.DesktopEnvironment["Service"]);
3030

3131
const commandIn = (
32-
input: { home: string; baseDir?: string; platform?: NodeJS.Platform; isPackaged?: boolean },
32+
input: {
33+
home: string;
34+
baseDir?: string;
35+
platform?: NodeJS.Platform;
36+
isPackaged?: boolean;
37+
env?: NodeJS.ProcessEnv;
38+
},
3339
spawner?: ChildProcessSpawner.ChildProcessSpawner["Service"],
3440
) =>
3541
Effect.gen(function* () {
@@ -42,6 +48,7 @@ const commandIn = (
4248
DesktopEnvironment.DesktopEnvironment,
4349
environmentFor(path, { ...input, baseDir }),
4450
),
51+
Effect.provideService(HostProcess.Environment, input.env ?? { PATH: "" }),
4552
);
4653
return yield* spawner
4754
? make.pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner))
@@ -84,14 +91,6 @@ const fakePowerShell = (initial: string) => {
8491
return { registry, spawner };
8592
};
8693

87-
const machinePath = process.env.PATH;
88-
beforeEach(() => {
89-
process.env.PATH = "";
90-
});
91-
afterEach(() => {
92-
process.env.PATH = machinePath;
93-
});
94-
9594
it.layer(NodeServices.layer)("DesktopCliCommand", (it) => {
9695
it.effect("links the launcher onto PATH and removes only that link", () =>
9796
Effect.gen(function* () {
@@ -124,7 +123,8 @@ it.layer(NodeServices.layer)("DesktopCliCommand", (it) => {
124123
const fs = yield* FileSystem.FileSystem;
125124
const path = yield* Path.Path;
126125
const home = yield* fs.makeTempDirectoryScoped();
127-
const command = yield* commandIn({ home });
126+
const env: NodeJS.ProcessEnv = { PATH: "" };
127+
const command = yield* commandIn({ home, env });
128128
const theirs = path.join(home, ".local", "bin", "t3");
129129
yield* fs.makeDirectory(path.dirname(theirs), { recursive: true });
130130
yield* fs.writeFileString(theirs, "npm's t3\n");
@@ -139,7 +139,7 @@ it.layer(NodeServices.layer)("DesktopCliCommand", (it) => {
139139

140140
const launcher = path.join(home, ".t3", "bin", "t3");
141141
yield* fs.writeFileString(launcher, "npm's t3\n");
142-
process.env.PATH = path.dirname(launcher);
142+
env.PATH = path.dirname(launcher);
143143
expect(yield* command.state).toMatchObject({ onPath: false, shadowedBy: launcher });
144144
expect((yield* Effect.flip(command.install)).message).toContain("Could not set up");
145145
yield* command.uninstall;
@@ -173,9 +173,9 @@ it.layer(NodeServices.layer)("DesktopCliCommand", (it) => {
173173
const shadow = path.join(home, "shadow");
174174
yield* fs.makeDirectory(shadow);
175175
yield* fs.writeFileString(path.join(shadow, "t3"), "#!/bin/sh\n", { mode: 0o755 });
176-
process.env.PATH = [shadow, path.join(home, ".local", "bin")].join(":");
176+
const env = { PATH: [shadow, path.join(home, ".local", "bin")].join(":") };
177177

178-
const command = yield* commandIn({ home });
178+
const command = yield* commandIn({ home, env });
179179
const theirs = path.join(shadow, "t3");
180180
expect((yield* command.state).shadowedBy).toBe(theirs);
181181
const error = yield* Effect.flip(command.install);
@@ -209,9 +209,9 @@ it.layer(NodeServices.layer)("DesktopCliCommand", (it) => {
209209
yield* fs.chmod(path.join(plain, "t3"), process.getuid?.() === 0 ? 0o600 : 0o601);
210210
yield* fs.makeDirectory(shadow);
211211
yield* fs.writeFileString(path.join(shadow, "t3"), "#!/bin/sh\n", { mode: 0o755 });
212-
process.env.PATH = [folder, plain, shadow, path.join(home, ".local", "bin")].join(":");
212+
const env = { PATH: [folder, plain, shadow, path.join(home, ".local", "bin")].join(":") };
213213

214-
const command = yield* commandIn({ home });
214+
const command = yield* commandIn({ home, env });
215215
expect((yield* command.state).shadowedBy).toBe(path.join(shadow, "t3"));
216216

217217
const error = yield* Effect.flip(command.install);
@@ -231,9 +231,9 @@ it.layer(NodeServices.layer)("DesktopCliCommand", (it) => {
231231
const path = yield* Path.Path;
232232
const home = yield* fs.makeTempDirectoryScoped();
233233
const launcher = path.join(home, ".t3", "bin", "t3");
234-
process.env.PATH = [path.dirname(launcher), path.join(home, ".local", "bin")].join(":");
234+
const env = { PATH: [path.dirname(launcher), path.join(home, ".local", "bin")].join(":") };
235235

236-
const command = yield* commandIn({ home });
236+
const command = yield* commandIn({ home, env });
237237
const installed = yield* command.install;
238238

239239
expect(yield* fs.exists(launcher)).toBe(true);

‎apps/desktop/src/app/DesktopCliCommand.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import * as NodeFSP from "node:fs/promises";
33

44
import type { DesktopCliCommandState } from "@t3tools/contracts";
5+
import * as HostProcess from "@t3tools/shared/HostProcess";
56
import * as Context from "effect/Context";
67
import * as Effect from "effect/Effect";
78
import * as FileSystem from "effect/FileSystem";
@@ -61,11 +62,13 @@ export class DesktopCliCommand extends Context.Service<
6162
}
6263
>()("@t3tools/desktop/app/DesktopCliCommand") {}
6364

64-
/** @public */
6565
export const make = Effect.gen(function* () {
6666
const environment = yield* DesktopEnvironment.DesktopEnvironment;
6767
const fs = yield* FileSystem.FileSystem;
6868
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
69+
// Read the live env: shell discovery patches PATH after startup, and Windows variable
70+
// names are case-insensitive only on process.env, not on Config's copy of it.
71+
const env = yield* HostProcess.Environment;
6972
const path = environment.path;
7073
const windows = environment.platform === "win32";
7174
const launcher = DesktopCliShim.launcherPath(environment);
@@ -105,15 +108,15 @@ export const make = Effect.gen(function* () {
105108
Effect.orElseSucceed(() => false),
106109
);
107110

108-
const powershell = (env: Record<string, string>) =>
111+
const powershell = (overrides: Record<string, string>) =>
109112
Effect.scoped(
110113
Effect.gen(function* () {
111-
const root = process.env.SystemRoot ?? process.env.WINDIR;
114+
const root = env.SystemRoot ?? env.WINDIR;
112115
const handle = yield* spawner.spawn(
113116
ChildProcess.make(
114117
root ? `${root}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe` : "powershell.exe",
115118
["-NoLogo", "-NoProfile", "-NonInteractive", "-Command", WINDOWS_USER_PATH_SCRIPT],
116-
{ stdin: "ignore", stderr: "ignore", env, extendEnv: true },
119+
{ stdin: "ignore", stderr: "ignore", env: overrides, extendEnv: true },
117120
),
118121
);
119122
const [stdout, exitCode] = yield* Effect.all(
@@ -137,14 +140,17 @@ export const make = Effect.gen(function* () {
137140
const isExecutableFile = Effect.fn("DesktopCliCommand.isExecutableFile")(
138141
function* (target: string) {
139142
if ((yield* fs.stat(target)).type !== "File") return false;
140-
yield* Effect.tryPromise(() => NodeFSP.access(target, NodeFSP.constants.X_OK));
143+
yield* Effect.tryPromise({
144+
try: () => NodeFSP.access(target, NodeFSP.constants.X_OK),
145+
catch: () => fail(`${target} is not executable.`),
146+
});
141147
return true;
142148
},
143149
Effect.orElseSucceed(() => false),
144150
);
145151

146152
const firstOnPath = Effect.gen(function* () {
147-
for (const directory of pathEntries(process.env.PATH, ":")) {
153+
for (const directory of pathEntries(env.PATH, ":")) {
148154
const candidate = path.join(directory, "t3");
149155
if (yield* isExecutableFile(candidate)) return Option.some(candidate);
150156
}
@@ -239,7 +245,7 @@ export const make = Effect.gen(function* () {
239245
`Another t3 at ${shadowedBy.value} runs first in a new terminal. Remove it, or run the launcher directly at ${launcher}.`,
240246
);
241247
}
242-
const onPath = pathEntries(process.env.PATH, ":");
248+
const onPath = pathEntries(env.PATH, ":");
243249
const candidates = unixCandidates(environment.homeDirectory, environment.platform);
244250
for (const directory of [
245251
...candidates.filter((candidate) => onPath.includes(candidate)),

‎apps/desktop/src/app/DesktopConfig.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const trimNonEmptyOption = (value: string): Option.Option<string> => {
88
return trimmed.length > 0 ? Option.some(trimmed) : Option.none();
99
};
1010

11-
const trimmedString = (name: string) =>
11+
export const trimmedString = (name: string) =>
1212
Config.String(name).pipe(Config.option, Config.map(Option.flatMap(trimNonEmptyOption)));
1313

1414
const optionalBoolean = (name: string) =>

‎apps/desktop/src/app/DesktopPreReadyPlatform.test.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ vi.mock("node:child_process", () => ({
5757
execFileSync: execFileSyncMock,
5858
}));
5959

60+
import * as DesktopConfig from "./DesktopConfig.ts";
6061
import * as DesktopPreReadyPlatform from "./DesktopPreReadyPlatform.ts";
6162

6263
describe("DesktopPreReadyPlatform", () => {
@@ -124,8 +125,6 @@ describe("DesktopPreReadyPlatform", () => {
124125
{ previousEntry: 'Exec="/Applications/deleted-previous.AppImage" %U', label: "stale" },
125126
])("prepares a $label Linux desktop entry before startup yields", ({ previousEntry }) => {
126127
vi.stubEnv("VITE_DEV_SERVER_URL", "");
127-
vi.stubEnv("XDG_DATA_HOME", "/xdg");
128-
vi.stubEnv("APPIMAGE", "/Applications/current.AppImage");
129128
getSwitchValueMock.mockReturnValue("");
130129
let desktopName = "t3code.desktop";
131130
let desktopEntry = previousEntry;
@@ -150,6 +149,12 @@ describe("DesktopPreReadyPlatform", () => {
150149
yield* Layer.build(
151150
DesktopPreReadyPlatform.layer.pipe(
152151
Layer.provide(Layer.succeed(HostProcess.Platform, "linux")),
152+
Layer.provide(
153+
DesktopConfig.layerTest({
154+
XDG_DATA_HOME: "/xdg",
155+
APPIMAGE: "/Applications/current.AppImage",
156+
}),
157+
),
153158
),
154159
);
155160
const identity = yield* Effect.promise(() => portalIdentity);
@@ -168,16 +173,20 @@ describe("DesktopPreReadyPlatform", () => {
168173

169174
it.effect("points the early portal entry at a configured distribution launcher", () => {
170175
vi.stubEnv("VITE_DEV_SERVER_URL", "");
171-
vi.stubEnv("XDG_DATA_HOME", "/xdg");
172-
vi.stubEnv("APPIMAGE", "/Applications/current.AppImage");
173-
vi.stubEnv("T3CODE_DESKTOP_LAUNCHER_PATH", " /opt/T3 Code/t3code ");
174176
getSwitchValueMock.mockReturnValue("");
175177

176178
return Effect.scoped(
177179
Effect.gen(function* () {
178180
yield* Layer.build(
179181
DesktopPreReadyPlatform.layer.pipe(
180182
Layer.provide(Layer.succeed(HostProcess.Platform, "linux")),
183+
Layer.provide(
184+
DesktopConfig.layerTest({
185+
XDG_DATA_HOME: "/xdg",
186+
APPIMAGE: "/Applications/current.AppImage",
187+
T3CODE_DESKTOP_LAUNCHER_PATH: " /opt/T3 Code/t3code ",
188+
}),
189+
),
181190
),
182191
);
183192
const entry = writeFileSyncMock.mock.calls.find(

‎apps/desktop/src/app/DesktopPreReadyPlatform.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
import * as NodeChildProcess from "node:child_process";
33
import * as NodeFS from "node:fs";
44
import * as NodePath from "node:path";
5+
import * as Config from "effect/Config";
56
import * as Context from "effect/Context";
67
import * as Effect from "effect/Effect";
78
import * as Layer from "effect/Layer";
9+
import * as Option from "effect/Option";
810

911
import * as Electron from "electron";
1012
import * as HostProcess from "@t3tools/shared/HostProcess";
1113

1214
import * as DesktopEarlyElectronStartup from "./DesktopEarlyElectronStartup.ts";
15+
import { trimmedString } from "./DesktopConfig.ts";
1316
import { resolveDesktopAppBranding } from "./DesktopEnvironment.ts";
1417
import { renderUrlHandlerDesktopEntry } from "./DesktopLinuxUrlHandler.ts";
1518
import * as ElectronProtocol from "../electron/ElectronProtocol.ts";
@@ -49,10 +52,17 @@ export class DesktopPreReadyElectronOptions extends Context.Service<
4952
}
5053
>()("@t3tools/desktop/app/DesktopPreReadyPlatform/DesktopPreReadyElectronOptions") {}
5154

55+
const LinuxDesktopEntryConfig = Config.all({
56+
xdgDataHome: trimmedString("XDG_DATA_HOME"),
57+
launcherPath: trimmedString("T3CODE_DESKTOP_LAUNCHER_PATH"),
58+
appImagePath: trimmedString("APPIMAGE"),
59+
});
60+
5261
/** @public */
5362
export const make = Effect.gen(function* () {
5463
const platform = yield* HostProcess.Platform;
5564
const homeDirectory = yield* HostProcess.HomeDirectory;
65+
const linuxDesktopEntryConfig = yield* LinuxDesktopEntryConfig;
5666
return yield* Effect.sync((): DesktopPreReadyElectronOptions["Service"] => {
5767
const linuxPasswordStoreCommandLine =
5868
platform === "linux"
@@ -77,8 +87,9 @@ export const make = Effect.gen(function* () {
7787
// have removed the executable referenced by the previous launch's entry.
7888
try {
7989
const applicationsDir = NodePath.posix.join(
80-
process.env.XDG_DATA_HOME?.trim() ||
90+
Option.getOrElse(linuxDesktopEntryConfig.xdgDataHome, () =>
8191
NodePath.posix.join(homeDirectory, ".local", "share"),
92+
),
8293
"applications",
8394
);
8495
NodeFS.mkdirSync(applicationsDir, { recursive: true });
@@ -110,9 +121,12 @@ export const make = Effect.gen(function* () {
110121
appVersion: Electron.app.getVersion(),
111122
}).displayName,
112123
execTarget:
113-
process.env.T3CODE_DESKTOP_LAUNCHER_PATH?.trim() ||
114-
process.env.APPIMAGE?.trim() ||
115-
process.execPath,
124+
Option.getOrUndefined(
125+
Option.orElse(
126+
linuxDesktopEntryConfig.launcherPath,
127+
() => linuxDesktopEntryConfig.appImagePath,
128+
),
129+
) ?? process.execPath,
116130
scheme: ElectronProtocol.getDesktopScheme(linux.isDevelopment),
117131
...(iconPath === undefined ? {} : { iconPath }),
118132
}),

0 commit comments

Comments
 (0)