Skip to content

Commit 2cefde7

Browse files
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 <[email protected]>
1 parent 41e1e7d commit 2cefde7

2 files changed

Lines changed: 45 additions & 26 deletions

File tree

‎apps/desktop/src/shell/DesktopShellEnvironment.test.ts‎

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -163,31 +163,35 @@ function runShellEnvironment(input: {
163163
}
164164

165165
describe("DesktopShellEnvironment", () => {
166-
it.effect("does not wait for discovery and reports unprepared when the cache is missing", () =>
167-
Effect.gen(function* () {
168-
const started = yield* Deferred.make<void>();
169-
const release = yield* Deferred.make<void>();
170-
yield* runShellEnvironment({
171-
env: { SHELL: "/test-shell", PATH: "/usr/bin" },
172-
platform: "darwin",
173-
handler: () => "",
174-
processSpawner: ChildProcessSpawner.make(() =>
175-
Deferred.succeed(started, undefined).pipe(
176-
Effect.andThen(Deferred.await(release)),
177-
Effect.as(makeProcess(envOutput({ PATH: "/fresh/bin" }))),
166+
it.effect(
167+
"reports unprepared on a cache miss without waiting, then prepared after discovery",
168+
() =>
169+
Effect.gen(function* () {
170+
const started = yield* Deferred.make<void>();
171+
const release = yield* Deferred.make<void>();
172+
const preparedAfterDiscovery = yield* runShellEnvironment({
173+
env: { SHELL: "/test-shell", PATH: "/usr/bin" },
174+
platform: "darwin",
175+
handler: () => "",
176+
processSpawner: ChildProcessSpawner.make(() =>
177+
Deferred.succeed(started, undefined).pipe(
178+
Effect.andThen(Deferred.await(release)),
179+
Effect.as(makeProcess(envOutput({ PATH: "/fresh/bin" }))),
180+
),
178181
),
179-
),
180-
use: (service) =>
181-
Effect.gen(function* () {
182-
const startup = yield* service.installIntoProcess.pipe(Effect.forkChild);
183-
yield* Deferred.await(started);
184-
assert.isDefined(startup.pollUnsafe());
185-
assert.isFalse(yield* Fiber.join(startup));
186-
yield* Deferred.succeed(release, undefined);
187-
return yield* service.awaitDiscovery;
188-
}),
189-
});
190-
}),
182+
use: (service) =>
183+
Effect.gen(function* () {
184+
const startup = yield* service.installIntoProcess.pipe(Effect.forkChild);
185+
yield* Deferred.await(started);
186+
assert.isDefined(startup.pollUnsafe());
187+
assert.isFalse(yield* Fiber.join(startup));
188+
yield* Deferred.succeed(release, undefined);
189+
assert.isTrue(yield* service.awaitDiscovery);
190+
return yield* service.installIntoProcess;
191+
}),
192+
});
193+
assert.isTrue(preparedAfterDiscovery);
194+
}),
191195
);
192196

193197
it.effect(

‎apps/desktop/src/shell/DesktopShellEnvironment.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import * as Context from "effect/Context";
1010
import * as Duration from "effect/Duration";
1111
import * as Effect from "effect/Effect";
12+
import * as Exit from "effect/Exit";
1213
import * as Fiber from "effect/Fiber";
1314
import * as Deferred from "effect/Deferred";
1415
import * as Path from "effect/Path";
@@ -77,7 +78,7 @@ export class DesktopShellEnvironmentCommandTimeoutError extends Schema.TaggedErr
7778
export class DesktopShellEnvironment extends Context.Service<
7879
DesktopShellEnvironment,
7980
{
80-
// True only when a matching cached environment was applied; false leaves server hydration on.
81+
// True once a matching cache or finished discovery prepared PATH; false keeps server hydration.
8182
readonly installIntoProcess: Effect.Effect<boolean>;
8283
readonly awaitDiscovery: Effect.Effect<boolean>;
8384
/** 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* () {
644645
Effect.withSpan("desktop.shellEnvironment.refresh"),
645646
Effect.onExit((exit) => Deferred.done(discovered, exit)),
646647
);
647-
const installIntoProcess = yield* Effect.cached(
648+
const applyCache = yield* Effect.cached(
648649
Effect.gen(function* () {
649650
const cache = yield* fileSystem
650651
.readFileString(cachePath)
@@ -692,6 +693,20 @@ export const make = Effect.gen(function* () {
692693
);
693694
}),
694695
);
696+
const installIntoProcess = applyCache.pipe(
697+
Effect.flatMap((cacheApplied) =>
698+
cacheApplied
699+
? Effect.succeed(true)
700+
: Deferred.poll(discovered).pipe(
701+
Effect.flatMap(Effect.transposeOption),
702+
Effect.exit,
703+
Effect.map(
704+
(exit) =>
705+
Exit.isSuccess(exit) && Option.exists(exit.value, ({ prepared }) => prepared),
706+
),
707+
),
708+
),
709+
);
695710
return DesktopShellEnvironment.of({
696711
installIntoProcess,
697712
awaitDiscovery: installIntoProcess.pipe(

0 commit comments

Comments
 (0)