From d53e89be6c10c689475ade901db883d899b352f0 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 1 Nov 2026 21:18:53 +0000 Subject: [PATCH 1/5] test(desktop): reproduce #28890, cache miss reports shell env prepared installIntoProcess must report false when no matching shell environment cache was applied, so the server hydrates PATH itself on first launch. It returns true unconditionally on main. The cache-hit test now also asserts true, so a fix cannot regress to always-false. Co-Authored-By: Claude Opus 5.5 --- apps/desktop/src/shell/DesktopShellEnvironment.test.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts index 79344567fcd6c..2fb3411b3ebe5 100644 --- a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts +++ b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts @@ -4,6 +4,7 @@ import { assert, describe, it } from "@effect/vitest"; import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Effect from "effect/Effect"; import * as Deferred from "effect/Deferred"; +import * as Fiber from "effect/Fiber"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Logger from "effect/Logger"; @@ -145,7 +146,7 @@ function runShellEnvironment(input: { } describe("DesktopShellEnvironment", () => { - it.effect("does not wait for discovery when the cache is missing", () => + it.effect("does not wait for discovery and reports unprepared when the cache is missing", () => Effect.gen(function* () { const started = yield* Deferred.make(); const release = yield* Deferred.make(); @@ -164,6 +165,7 @@ describe("DesktopShellEnvironment", () => { const startup = yield* service.installIntoProcess.pipe(Effect.forkChild); yield* Deferred.await(started); assert.isDefined(startup.pollUnsafe()); + assert.isFalse(yield* Fiber.join(startup)); yield* Deferred.succeed(release, undefined); return yield* service.awaitDiscovery; }), @@ -338,7 +340,7 @@ describe("DesktopShellEnvironment", () => { handler: () => "", use: (service) => Effect.gen(function* () { - yield* service.installIntoProcess; + assert.isTrue(yield* service.installIntoProcess); const replay = yield* Stream.runHead(service.updates); assert.isTrue(Option.isSome(replay)); if (Option.isSome(replay)) { From c373ea919e1ed9070f5dd87aadf0ba98bb4aded9 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 1 Nov 2026 21:28:23 +0000 Subject: [PATCH 2/5] fix(desktop): report shell environment unprepared on a cache miss Since #28556, installIntoProcess returned true even when no cached shell environment was applied, so the backend skipped its own PATH hydration and started with the bare launchd PATH on first launch. Providers probed before background discovery finished stayed "not found" for the session. installIntoProcess now returns true only when it applied a matching cached environment. On a miss, an unreadable cache, or a key mismatch it returns false and the server hydrates PATH itself. Background refresh, publishing, and cache writes are unchanged. Fixes pingdotgg/t3code#28890 Co-Authored-By: Claude Opus 5.5 --- apps/desktop/src/shell/DesktopShellEnvironment.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.ts b/apps/desktop/src/shell/DesktopShellEnvironment.ts index 556f7090b422e..bd45a97f724b0 100644 --- a/apps/desktop/src/shell/DesktopShellEnvironment.ts +++ b/apps/desktop/src/shell/DesktopShellEnvironment.ts @@ -621,8 +621,12 @@ export const make = Effect.gen(function* () { Effect.gen(function* () { const cache = yield* fileSystem .readFileString(cachePath) - .pipe(Effect.flatMap(decodeCache), Effect.option); - if (Option.isSome(cache) && cache.value.key === key) { + .pipe( + Effect.flatMap(decodeCache), + Effect.option, + Effect.map(Option.filter((entry) => entry.key === key)), + ); + if (Option.isSome(cache)) { const cached = Object.fromEntries( CACHED_ENV_NAMES.flatMap((name) => { const value = cache.value.environment[name]; @@ -648,7 +652,7 @@ export const make = Effect.gen(function* () { ); } yield* Effect.forkIn(refresh, scope); - return true; + return Option.isSome(cache); }).pipe(Effect.withSpan("desktop.shellEnvironment.installIntoProcess")), ); const updates = Stream.unwrap( From c8db3461e488aa2f0766a791248770a81cea4ce0 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 1 Nov 2026 21:35:01 +0000 Subject: [PATCH 3/5] fix(desktop): describe installIntoProcess's prepared contract and test the key mismatch Co-Authored-By: Claude Opus 5.5 --- .../src/shell/DesktopShellEnvironment.test.ts | 63 ++++++++++--------- .../src/shell/DesktopShellEnvironment.ts | 2 +- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts index 2fb3411b3ebe5..861beee7b6f2b 100644 --- a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts +++ b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts @@ -269,37 +269,40 @@ describe("DesktopShellEnvironment", () => { "not json", '{"version":2,"key":"old","environment":{"PATH":"/bad/bin"}}', '{"version":1,"key":"other-shell","environment":{"PATH":"/bad/bin"}}', - ])("ignores an unusable cache and still starts before discovery: %s", (contents) => - Effect.gen(function* () { - const fs = yield* FileSystem.FileSystem; - const stateDir = yield* fs.makeTempDirectoryScoped(); - yield* fs.writeFileString(`${stateDir}/shell-environment-cache.json`, contents); - const started = yield* Deferred.make(); - const release = yield* Deferred.make(); - const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin" }; - yield* runShellEnvironment({ - env, - stateDir, - platform: "darwin", - handler: () => "", - processSpawner: ChildProcessSpawner.make(() => - Deferred.succeed(started, undefined).pipe( - Effect.andThen(Deferred.await(release)), - Effect.as(makeProcess(envOutput({ PATH: "/fresh/bin" }))), + ])( + "ignores an unusable cache, reports unprepared and still starts before discovery: %s", + (contents) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const stateDir = yield* fs.makeTempDirectoryScoped(); + yield* fs.writeFileString(`${stateDir}/shell-environment-cache.json`, contents); + const started = yield* Deferred.make(); + const release = yield* Deferred.make(); + const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin" }; + yield* runShellEnvironment({ + env, + stateDir, + platform: "darwin", + handler: () => "", + processSpawner: ChildProcessSpawner.make(() => + Deferred.succeed(started, undefined).pipe( + Effect.andThen(Deferred.await(release)), + Effect.as(makeProcess(envOutput({ PATH: "/fresh/bin" }))), + ), ), - ), - use: (service) => - Effect.gen(function* () { - const startup = yield* service.installIntoProcess.pipe(Effect.forkChild); - yield* Deferred.await(started); - assert.isDefined(startup.pollUnsafe()); - assert.equal(env.PATH, "/usr/bin"); - yield* Deferred.succeed(release, undefined); - return yield* service.awaitDiscovery; - }), - }); - assert.equal(env.PATH, "/fresh/bin:/usr/bin"); - }).pipe(Effect.provide(NodeServices.layer)), + use: (service) => + Effect.gen(function* () { + const startup = yield* service.installIntoProcess.pipe(Effect.forkChild); + yield* Deferred.await(started); + assert.isDefined(startup.pollUnsafe()); + assert.isFalse(yield* Fiber.join(startup)); + assert.equal(env.PATH, "/usr/bin"); + yield* Deferred.succeed(release, undefined); + return yield* service.awaitDiscovery; + }), + }); + assert.equal(env.PATH, "/fresh/bin:/usr/bin"); + }).pipe(Effect.provide(NodeServices.layer)), ); it.effect( diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.ts b/apps/desktop/src/shell/DesktopShellEnvironment.ts index bd45a97f724b0..ad237838d0178 100644 --- a/apps/desktop/src/shell/DesktopShellEnvironment.ts +++ b/apps/desktop/src/shell/DesktopShellEnvironment.ts @@ -75,7 +75,7 @@ export class DesktopShellEnvironmentCommandTimeoutError extends Schema.TaggedErr export class DesktopShellEnvironment extends Context.Service< DesktopShellEnvironment, { - // Desktop owns discovery, including on cache misses; the backend receives updates over fd4. + // True only when a matching cached environment was applied; false leaves server hydration on. readonly installIntoProcess: Effect.Effect; readonly awaitDiscovery: Effect.Effect; readonly updates: Stream.Stream; From 78c50b01d142113db622b5c9ca91aaf01ce62a59 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 10 Nov 2026 23:48:21 +0000 Subject: [PATCH 4/5] test(desktop): drop duplicate Fiber import from merge Co-Authored-By: Claude Opus 5.5 --- apps/desktop/src/shell/DesktopShellEnvironment.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts index 8f20eb5cbb395..39dbf5da1f192 100644 --- a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts +++ b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts @@ -3,7 +3,6 @@ import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, describe, it } from "@effect/vitest"; import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Effect from "effect/Effect"; -import * as Fiber from "effect/Fiber"; import * as Deferred from "effect/Deferred"; import * as Fiber from "effect/Fiber"; import * as FileSystem from "effect/FileSystem"; From 2cefde7af1fea78d7b1190fbbbea1139cef8ce42 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 11 Nov 2026 03:07:56 +0000 Subject: [PATCH 5/5] fix(desktop): report shell environment prepared once discovery has finished Backend restarts after a cache-miss launch no longer repeat the server's login-shell PATH probe when desktop discovery already prepared PATH. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 --- .../src/shell/DesktopShellEnvironment.test.ts | 52 ++++++++++--------- .../src/shell/DesktopShellEnvironment.ts | 19 ++++++- 2 files changed, 45 insertions(+), 26 deletions(-) diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts index 39dbf5da1f192..305e7e16b55b5 100644 --- a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts +++ b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts @@ -163,31 +163,35 @@ function runShellEnvironment(input: { } describe("DesktopShellEnvironment", () => { - it.effect("does not wait for discovery and reports unprepared when the cache is missing", () => - Effect.gen(function* () { - const started = yield* Deferred.make(); - const release = yield* Deferred.make(); - yield* runShellEnvironment({ - env: { SHELL: "/test-shell", PATH: "/usr/bin" }, - platform: "darwin", - handler: () => "", - processSpawner: ChildProcessSpawner.make(() => - Deferred.succeed(started, undefined).pipe( - Effect.andThen(Deferred.await(release)), - Effect.as(makeProcess(envOutput({ PATH: "/fresh/bin" }))), + it.effect( + "reports unprepared on a cache miss without waiting, then prepared after discovery", + () => + Effect.gen(function* () { + const started = yield* Deferred.make(); + const release = yield* Deferred.make(); + const preparedAfterDiscovery = yield* runShellEnvironment({ + env: { SHELL: "/test-shell", PATH: "/usr/bin" }, + platform: "darwin", + handler: () => "", + processSpawner: ChildProcessSpawner.make(() => + Deferred.succeed(started, undefined).pipe( + Effect.andThen(Deferred.await(release)), + Effect.as(makeProcess(envOutput({ PATH: "/fresh/bin" }))), + ), ), - ), - use: (service) => - Effect.gen(function* () { - const startup = yield* service.installIntoProcess.pipe(Effect.forkChild); - yield* Deferred.await(started); - assert.isDefined(startup.pollUnsafe()); - assert.isFalse(yield* Fiber.join(startup)); - yield* Deferred.succeed(release, undefined); - return yield* service.awaitDiscovery; - }), - }); - }), + use: (service) => + Effect.gen(function* () { + const startup = yield* service.installIntoProcess.pipe(Effect.forkChild); + yield* Deferred.await(started); + assert.isDefined(startup.pollUnsafe()); + assert.isFalse(yield* Fiber.join(startup)); + yield* Deferred.succeed(release, undefined); + assert.isTrue(yield* service.awaitDiscovery); + return yield* service.installIntoProcess; + }), + }); + assert.isTrue(preparedAfterDiscovery); + }), ); it.effect( diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.ts b/apps/desktop/src/shell/DesktopShellEnvironment.ts index 815f88d77db68..0cd7359a5aad7 100644 --- a/apps/desktop/src/shell/DesktopShellEnvironment.ts +++ b/apps/desktop/src/shell/DesktopShellEnvironment.ts @@ -9,6 +9,7 @@ import { import * as Context from "effect/Context"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; +import * as Exit from "effect/Exit"; import * as Fiber from "effect/Fiber"; import * as Deferred from "effect/Deferred"; import * as Path from "effect/Path"; @@ -77,7 +78,7 @@ export class DesktopShellEnvironmentCommandTimeoutError extends Schema.TaggedErr export class DesktopShellEnvironment extends Context.Service< DesktopShellEnvironment, { - // True only when a matching cached environment was applied; false leaves server hydration on. + // True once a matching cache or finished discovery prepared PATH; false keeps server hydration. readonly installIntoProcess: Effect.Effect; readonly awaitDiscovery: Effect.Effect; /** Whether the user's own login shell, not a fallback, gave the PATH a new terminal gets. */ @@ -644,7 +645,7 @@ export const make = Effect.gen(function* () { Effect.withSpan("desktop.shellEnvironment.refresh"), Effect.onExit((exit) => Deferred.done(discovered, exit)), ); - const installIntoProcess = yield* Effect.cached( + const applyCache = yield* Effect.cached( Effect.gen(function* () { const cache = yield* fileSystem .readFileString(cachePath) @@ -692,6 +693,20 @@ export const make = Effect.gen(function* () { ); }), ); + const installIntoProcess = applyCache.pipe( + Effect.flatMap((cacheApplied) => + cacheApplied + ? Effect.succeed(true) + : Deferred.poll(discovered).pipe( + Effect.flatMap(Effect.transposeOption), + Effect.exit, + Effect.map( + (exit) => + Exit.isSuccess(exit) && Option.exists(exit.value, ({ prepared }) => prepared), + ), + ), + ), + ); return DesktopShellEnvironment.of({ installIntoProcess, awaitDiscovery: installIntoProcess.pipe(