Skip to content

Commit a597d20

Browse files
refactor: read named env settings through Config
Named settings that code interprets (XDG_*, WINDIR/SystemRoot, TMPDIR/TEMP/TMP, APPIMAGE, launcher path, tunnel transport, Forgejo dirs, recorder and eval settings) now come from Effect Config, and tests provide them with ConfigProvider. HostProcess.Environment stays only where the env is passed through to a child process. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 7968113 commit a597d20

20 files changed

Lines changed: 179 additions & 60 deletions

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

Lines changed: 9 additions & 3 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,13 +313,13 @@ const startup = Effect.gen(function* () {
307313
yield* desktopSettings.load;
308314

309315
if (linuxElectronOptions !== null) {
310-
const env = yield* HostProcess.Environment;
316+
const xdgDesktop = yield* LinuxDesktopSessionConfig;
311317
yield* logStartupInfo("linux password store configured", {
312318
passwordStore: hasCommandLinePasswordStore
313319
? "command-line"
314320
: (linuxElectronOptions.passwordStore ?? "electron-default"),
315-
xdgCurrentDesktop: env.XDG_CURRENT_DESKTOP ?? null,
316-
xdgSessionDesktop: env.XDG_SESSION_DESKTOP ?? null,
321+
xdgCurrentDesktop: Option.getOrNull(xdgDesktop.current),
322+
xdgSessionDesktop: Option.getOrNull(xdgDesktop.session),
317323
});
318324
}
319325

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as NodeFSP from "node:fs/promises";
33

44
import type { DesktopCliCommandState } from "@t3tools/contracts";
55
import * as HostProcess from "@t3tools/shared/HostProcess";
6+
import * as Config from "effect/Config";
67
import * as Context from "effect/Context";
78
import * as Effect from "effect/Effect";
89
import * as FileSystem from "effect/FileSystem";
@@ -63,10 +64,16 @@ export class DesktopCliCommand extends Context.Service<
6364
>()("@t3tools/desktop/app/DesktopCliCommand") {}
6465

6566
/** @public */
67+
const WindowsRootConfig = Config.String("SystemRoot").pipe(
68+
Config.orElse(() => Config.String("WINDIR")),
69+
Config.option,
70+
);
71+
6672
export const make = Effect.gen(function* () {
6773
const environment = yield* DesktopEnvironment.DesktopEnvironment;
6874
const fs = yield* FileSystem.FileSystem;
6975
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
76+
// Shell discovery patches PATH into the live env after startup; a Config read would be stale.
7077
const env = yield* HostProcess.Environment;
7178
const path = environment.path;
7279
const windows = environment.platform === "win32";
@@ -110,7 +117,7 @@ export const make = Effect.gen(function* () {
110117
const powershell = (env: Record<string, string>) =>
111118
Effect.scoped(
112119
Effect.gen(function* () {
113-
const root = env.SystemRoot ?? env.WINDIR;
120+
const root = Option.getOrUndefined(yield* WindowsRootConfig);
114121
const handle = yield* spawner.spawn(
115122
ChildProcess.make(
116123
root ? `${root}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe` : "powershell.exe",

‎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: 21 additions & 5 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,11 +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;
56-
const env = yield* HostProcess.Environment;
65+
const linuxDesktopEntryConfig = yield* LinuxDesktopEntryConfig;
5766
return yield* Effect.sync((): DesktopPreReadyElectronOptions["Service"] => {
5867
const linuxPasswordStoreCommandLine =
5968
platform === "linux"
@@ -64,7 +73,7 @@ export const make = Effect.gen(function* () {
6473

6574
if (linux !== null) {
6675
DesktopEarlyElectronStartup.restoreEarlyLinuxDeviceScaleFactor({
67-
env,
76+
env: process.env,
6877
homeDirectory,
6978
joinPath: NodePath.posix.join,
7079
commandLine: Electron.app.commandLine,
@@ -78,7 +87,9 @@ export const make = Effect.gen(function* () {
7887
// have removed the executable referenced by the previous launch's entry.
7988
try {
8089
const applicationsDir = NodePath.posix.join(
81-
env.XDG_DATA_HOME?.trim() || NodePath.posix.join(homeDirectory, ".local", "share"),
90+
Option.getOrElse(linuxDesktopEntryConfig.xdgDataHome, () =>
91+
NodePath.posix.join(homeDirectory, ".local", "share"),
92+
),
8293
"applications",
8394
);
8495
NodeFS.mkdirSync(applicationsDir, { recursive: true });
@@ -110,7 +121,12 @@ export const make = Effect.gen(function* () {
110121
appVersion: Electron.app.getVersion(),
111122
}).displayName,
112123
execTarget:
113-
env.T3CODE_DESKTOP_LAUNCHER_PATH?.trim() || env.APPIMAGE?.trim() || process.execPath,
124+
Option.getOrUndefined(
125+
Option.orElse(
126+
linuxDesktopEntryConfig.launcherPath,
127+
() => linuxDesktopEntryConfig.appImagePath,
128+
),
129+
) ?? process.execPath,
114130
scheme: ElectronProtocol.getDesktopScheme(linux.isDevelopment),
115131
...(iconPath === undefined ? {} : { iconPath }),
116132
}),
@@ -124,7 +140,7 @@ export const make = Effect.gen(function* () {
124140
}
125141
// Chromium keeps only the last --disable-features value, so pass the merged list.
126142
const disableFeatures = DesktopEarlyElectronStartup.resolveLinuxDisableFeaturesSwitch({
127-
env,
143+
env: process.env,
128144
ozonePlatform: readCommandLineSwitchValue(Electron.app.commandLine, "ozone-platform"),
129145
enableFeatures: readCommandLineSwitchValue(Electron.app.commandLine, "enable-features"),
130146
disableFeatures: readCommandLineSwitchValue(Electron.app.commandLine, "disable-features"),

‎apps/desktop/src/snapShot/DesktopSnapShot.test.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
type DesktopSnapShotEvent,
88
} from "@t3tools/contracts";
99
import * as SecureRandom from "effect/crypto/SecureRandom";
10+
import * as ConfigProvider from "effect/ConfigProvider";
1011
import * as Effect from "effect/Effect";
1112
import * as Fiber from "effect/Fiber";
1213
import * as FileSystem from "effect/FileSystem";
@@ -1921,7 +1922,13 @@ it.effect(
19211922
linuxBackendMock.mockResolvedValue(backend);
19221923
yield* Effect.scoped(
19231924
Effect.gen(function* () {
1924-
const service = yield* DesktopSnapShot.make;
1925+
const service = yield* DesktopSnapShot.make.pipe(
1926+
Effect.provide(
1927+
ConfigProvider.layer(
1928+
ConfigProvider.fromEnv({ env: { XDG_CURRENT_DESKTOP: desktop } }),
1929+
),
1930+
),
1931+
);
19251932
yield* service.initialize;
19261933
const state = yield* service.state;
19271934
assert.equal(state.linuxDesktop, desktop.toLowerCase());

‎apps/desktop/src/snapShot/DesktopSnapShot.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import * as Base64 from "effect/encoding/Base64";
3030
import * as FileSystem from "effect/FileSystem";
3131
import * as Layer from "effect/Layer";
3232
import * as Option from "effect/Option";
33+
import * as Config from "effect/Config";
3334
import * as Path from "effect/Path";
3435
import * as Predicate from "effect/Predicate";
3536
import * as Ref from "effect/Ref";
@@ -768,6 +769,9 @@ export const make = Effect.gen(function* () {
768769
const captureDirectory = path.join(environment.stateDir, "snap-shots");
769770
const linuxAppId = environment.linuxDesktopEntryName.replace(/\.desktop$/, "");
770771
const home = yield* HostProcess.HomeDirectory;
772+
const xdgCurrentDesktop = yield* Config.String("XDG_CURRENT_DESKTOP").pipe(
773+
Config.withDefault(""),
774+
);
771775
let shortcutVerified = false;
772776
const gnomeSetupPaths = {
773777
bundle: environment.isPackaged
@@ -1619,10 +1623,10 @@ export const make = Effect.gen(function* () {
16191623
),
16201624
Effect.flatMap((state) =>
16211625
Effect.gen(function* () {
1622-
const env = yield* HostProcess.Environment;
16231626
const linuxDesktop =
16241627
state.mode === "portal"
1625-
? env.XDG_CURRENT_DESKTOP?.toLowerCase()
1628+
? xdgCurrentDesktop
1629+
.toLowerCase()
16261630
.split(":")
16271631
.find(
16281632
(name) =>

‎apps/desktop/src/wsl/DesktopWslEnvironment.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as HostProcess from "@t3tools/shared/HostProcess";
1+
import * as Config from "effect/Config";
22
import * as Context from "effect/Context";
33
import * as Duration from "effect/Duration";
44
import * as Effect from "effect/Effect";
@@ -1142,7 +1142,7 @@ export const layer = Layer.effect(
11421142
const environment = yield* DesktopEnvironment.DesktopEnvironment;
11431143
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
11441144
const fileSystem = yield* FileSystem.FileSystem;
1145-
const windir = (yield* HostProcess.Environment).WINDIR ?? "C:\\Windows";
1145+
const windir = yield* Config.String("WINDIR").pipe(Config.withDefault("C:\\Windows"));
11461146

11471147
const provideSpawner = <A, E>(
11481148
effect: Effect.Effect<A, E, ChildProcessSpawner.ChildProcessSpawner>,

‎apps/server/integration/transferBudgetV2.integration.test.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import {
2626
type OrchestrationV2ThreadStreamItem,
2727
type OrchestrationV2ShellStreamItem,
2828
} from "@t3tools/contracts";
29-
import * as HostProcess from "@t3tools/shared/HostProcess";
3029
import * as Context from "effect/Context";
30+
import * as Config from "effect/Config";
3131
import * as Effect from "effect/Effect";
3232
import * as FileSystem from "effect/FileSystem";
3333
import * as Layer from "effect/Layer";
@@ -454,10 +454,13 @@ it.live("reports V2 thread HTTP and WebSocket transfer budgets", () =>
454454
}
455455
const fs = yield* FileSystem.FileSystem;
456456
const report = formatTransferBudgetReport(runs);
457-
const env = yield* HostProcess.Environment;
457+
const outputPaths = yield* Config.all({
458+
report: Config.String("T3CODE_TRANSFER_BUDGET_REPORT_PATH").pipe(Config.withDefault("")),
459+
result: Config.String("T3CODE_TRANSFER_BUDGET_RESULT_PATH").pipe(Config.withDefault("")),
460+
});
458461
for (const [path, contents] of [
459-
[env.T3CODE_TRANSFER_BUDGET_REPORT_PATH, report],
460-
[env.T3CODE_TRANSFER_BUDGET_RESULT_PATH, formatTransferBudgetResult(runs)],
462+
[outputPaths.report, report],
463+
[outputPaths.result, formatTransferBudgetResult(runs)],
461464
])
462465
if (path && contents) yield* fs.writeFileString(path, contents);
463466
yield* Effect.log(report);

‎apps/server/scripts/acp-mock-agent.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// @effect-diagnostics nodeBuiltinImport:off
33
import * as NodeFS from "node:fs";
44

5-
import * as HostProcess from "@t3tools/shared/HostProcess";
65
import * as Effect from "effect/Effect";
76
import * as Schema from "effect/Schema";
87
import * as Deferred from "effect/Deferred";
@@ -44,6 +43,9 @@ const omitInitializeModelState = process.env.T3_ACP_OMIT_INITIALIZE_MODEL_STATE
4443
const completeFirstPromptOnCancel = process.env.T3_ACP_COMPLETE_FIRST_PROMPT_ON_CANCEL === "1";
4544
const floodStderr = process.env.T3_ACP_FLOOD_STDERR === "1";
4645
const hangPromptForever = process.env.T3_ACP_HANG_PROMPT_FOREVER === "1";
46+
const agentVersion = process.env.T3_ACP_AGENT_VERSION ?? "0.0.0";
47+
const geminiApiKey = process.env.GEMINI_API_KEY;
48+
const idleError = process.env.T3_ACP_IDLE_ERROR;
4749
const emitTrackedToolCallKindThenHang = process.env.T3_ACP_EMIT_TRACKED_TOOL_CALL_KIND_THEN_HANG;
4850
const clientFsProbePath = process.env.T3_ACP_CLIENT_FS_PROBE_PATH;
4951
const clientFsProbeLogPath = process.env.T3_ACP_CLIENT_FS_PROBE_LOG_PATH;
@@ -468,7 +470,6 @@ function emitGrokKillTaskScenario(
468470
}
469471

470472
const program = Effect.gen(function* () {
471-
const hostEnv = yield* HostProcess.Environment;
472473
const agent = yield* EffectAcpAgent.AcpAgent;
473474
const nativeCancelRequested = yield* Deferred.make<void>();
474475
const nativeCancelRelease = yield* Deferred.make<void>();
@@ -514,7 +515,7 @@ const program = Effect.gen(function* () {
514515
parameterizedModelPicker = request.capabilities?._meta?.parameterizedModelPicker === true;
515516
return {
516517
protocolVersion: 2,
517-
info: { name: "t3-acp-mock-agent", version: hostEnv.T3_ACP_AGENT_VERSION ?? "0.0.0" },
518+
info: { name: "t3-acp-mock-agent", version: agentVersion },
518519
...(omitInitializeModelState ? {} : { _meta: { modelState: modelState() } }),
519520
capabilities: {
520521
session: {
@@ -552,7 +553,7 @@ const program = Effect.gen(function* () {
552553
if (antigravityProfile) {
553554
if (
554555
request.methodId !== "oauth-personal" &&
555-
!(request.methodId === "gemini-api-key" && hostEnv.GEMINI_API_KEY)
556+
!(request.methodId === "gemini-api-key" && geminiApiKey)
556557
) {
557558
return yield* AcpError.AcpRequestError.invalidParams(
558559
`Mock Antigravity rejected auth method ${request.methodId}.`,
@@ -813,7 +814,6 @@ const program = Effect.gen(function* () {
813814
const requestedSessionId = String(request.sessionId ?? sessionId);
814815
beginAcpMockPrompt(cancelledSessions, requestedSessionId);
815816
promptCount += 1;
816-
const idleError = hostEnv.T3_ACP_IDLE_ERROR;
817817
if (idleError === "grok-background" && promptCount === 1) {
818818
// The structured completion lands before the prompt settles, so it is buffered.
819819
const taskId = "call-idle-error-background";

0 commit comments

Comments
 (0)