From 18f5fa51229da4e13a8a738fcf0d86af51c535d9 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 1 Nov 2026 15:41:30 +0000 Subject: [PATCH 1/6] test(desktop): reproduce #28714, shell env cache miss reported as prepared A cache miss in installIntoProcess yields true before anything reaches process.env, so the server skips its own PATH hydration. These tests reproduce #28714 and fail on main. Co-Authored-By: Claude Opus 5.5 --- apps/desktop/src/shell/DesktopShellEnvironment.test.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts index 79344567fcd6..b5bde775927f 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"; @@ -164,6 +165,10 @@ describe("DesktopShellEnvironment", () => { const startup = yield* service.installIntoProcess.pipe(Effect.forkChild); yield* Deferred.await(started); assert.isDefined(startup.pollUnsafe()); + assert.isFalse( + yield* Fiber.join(startup), + "nothing was installed yet, so the server must hydrate PATH itself", + ); yield* Deferred.succeed(release, undefined); return yield* service.awaitDiscovery; }), @@ -231,7 +236,7 @@ describe("DesktopShellEnvironment", () => { }), use: (service) => Effect.gen(function* () { - yield* service.installIntoProcess; + assert.isTrue(yield* service.installIntoProcess); yield* service.installIntoProcess; assert.equal(env.PATH, "/old/bin:/usr/bin:/launcher/bin"); assert.equal(env.SSH_AUTH_SOCK, "/current-session.sock"); From 8dc3e6ff446dc1f1a714288091249ee86c91735b Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 1 Nov 2026 15:49:01 +0000 Subject: [PATCH 2/6] fix(desktop): let the server hydrate PATH when the shell env cache misses installIntoProcess reported the shell environment as prepared even on a cache miss, before anything reached process.env. The server then skipped its own PATH hydration and probed providers with the bare launcher PATH, reporting login-shell-only CLIs as missing for the session. Yield true only when a cached environment was applied. Discovery still runs in the background in both cases, so startup stays non-blocking. Fixes pingdotgg/t3code#28714 Co-Authored-By: Claude Opus 5.5 --- apps/desktop/src/shell/DesktopShellEnvironment.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.ts b/apps/desktop/src/shell/DesktopShellEnvironment.ts index 556f7090b422..dff70213ca74 100644 --- a/apps/desktop/src/shell/DesktopShellEnvironment.ts +++ b/apps/desktop/src/shell/DesktopShellEnvironment.ts @@ -75,7 +75,8 @@ 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. + // Yields true only when a cached environment was applied; on a miss the backend hydrates + // PATH itself until desktop discovery arrives over fd4. readonly installIntoProcess: Effect.Effect; readonly awaitDiscovery: Effect.Effect; readonly updates: Stream.Stream; @@ -622,7 +623,8 @@ export const make = Effect.gen(function* () { const cache = yield* fileSystem .readFileString(cachePath) .pipe(Effect.flatMap(decodeCache), Effect.option); - if (Option.isSome(cache) && cache.value.key === key) { + const hit = Option.isSome(cache) && cache.value.key === key; + if (hit) { const cached = Object.fromEntries( CACHED_ENV_NAMES.flatMap((name) => { const value = cache.value.environment[name]; @@ -648,7 +650,7 @@ export const make = Effect.gen(function* () { ); } yield* Effect.forkIn(refresh, scope); - return true; + return hit; }).pipe(Effect.withSpan("desktop.shellEnvironment.installIntoProcess")), ); const updates = Stream.unwrap( From afede16a7a38b21be21ee696a1c3027f6f6b73e7 Mon Sep 17 00:00:00 2001 From: "Russ T. Fugal" <128427608+SARAsBooks@users.noreply.github.com> Date: Wed, 4 Nov 2026 14:48:40 -0700 Subject: [PATCH 3/6] fix(server): providers find a shell PATH that arrives after startup The desktop now starts the server before it knows the user's shell PATH and sends it later. The server applied it to process.env, but provider drivers copy the environment when built (Claude, Muse, OpenCode with instance env), and a managed policy hands drivers a filtered copy made at startup. So a CLI only on the shell PATH stayed "not found" for the session, and Refresh couldn't help. When the desktop's shell environment changes the server's PATH, let pending provider checks finish, then rebuild the enabled instances whose completed check still can't find their CLI, and check them again. The registry rebuilds an instance only if it is still the one that was selected, so a replacement made meanwhile is left alone, and instances that found their CLI keep running. ACP Registry is excluded, since it reports installed:false for setup states. Under a managed policy, drivers now get a live filtered view of the host environment instead of a startup copy. Co-Authored-By: Claude Opus 5.5 Co-Authored-By: GPT-6.1 Sol (Codex) --- apps/server/src/policy/ManagedPolicy.test.ts | 10 +- apps/server/src/policy/managedProviders.ts | 30 +++- .../src/provider/ProviderHostPathWatcher.ts | 18 ++ .../provider/ProviderInstanceRegistry.test.ts | 17 ++ .../src/provider/ProviderInstanceRegistry.ts | 17 ++ .../ProviderInstanceRegistryMutator.ts | 3 + .../src/provider/ProviderRegistry.test.ts | 164 ++++++++++++++++++ apps/server/src/provider/ProviderRegistry.ts | 31 ++++ .../providerMaintenanceRunner.test.ts | 1 + .../testUtils/providerRegistryMock.ts | 1 + .../DesktopTelemetryReceiver.ts | 27 ++- apps/server/src/server.ts | 2 + 12 files changed, 311 insertions(+), 10 deletions(-) create mode 100644 apps/server/src/provider/ProviderHostPathWatcher.ts diff --git a/apps/server/src/policy/ManagedPolicy.test.ts b/apps/server/src/policy/ManagedPolicy.test.ts index 26e74f05b037..58692dd4ba32 100644 --- a/apps/server/src/policy/ManagedPolicy.test.ts +++ b/apps/server/src/policy/ManagedPolicy.test.ts @@ -187,7 +187,15 @@ it.layer(NodeServices.layer)("ManagedPolicy.load", (it) => { ]); // The host variable that replaces Codex's launch arguments is ignored too. const host = { PATH: "/usr/bin", T3CODE_CODEX_LAUNCH_ARGS: "-c mcp_servers.x.command=sh" }; - assert.deepEqual(providerHostEnvironment(policy.summary, host), { PATH: "/usr/bin" }); + const view = providerHostEnvironment(policy.summary, host); + assert.deepEqual({ ...view }, { PATH: "/usr/bin" }); + host.PATH = "/late/bin"; + Object.assign(host, { NEW_VARIABLE: "late", t3code_codex_launch_args: "hidden" }); + assert.deepEqual({ ...view }, { PATH: "/late/bin", NEW_VARIABLE: "late" }); + assert.isUndefined(view.T3CODE_CODEX_LAUNCH_ARGS); + assert.isFalse("t3code_codex_launch_args" in view); + assert.isFalse(Reflect.set(view, "PATH", "/replaced")); + assert.equal(host.PATH, "/late/bin"); assert.strictEqual(providerHostEnvironment(undefined, host), host); }), ); diff --git a/apps/server/src/policy/managedProviders.ts b/apps/server/src/policy/managedProviders.ts index ac390097e150..3af3d6d352e6 100644 --- a/apps/server/src/policy/managedProviders.ts +++ b/apps/server/src/policy/managedProviders.ts @@ -162,13 +162,29 @@ export const providerHostEnvironment = ( environment: NodeJS.ProcessEnv, ): NodeJS.ProcessEnv => { if (summary === undefined) return environment; - const next = { ...environment }; - for (const name of Object.keys(next)) { - if (LAUNCH_OVERRIDE_VARIABLES.some((override) => override === name.toUpperCase())) { - delete next[name]; - } - } - return next; + const allowed = (name: string | symbol) => + typeof name !== "string" || + !LAUNCH_OVERRIDE_VARIABLES.some((override) => override === name.toUpperCase()); + return new Proxy( + {}, + { + get: (_, name) => (allowed(name) ? Reflect.get(environment, name) : undefined), + has: (_, name) => allowed(name) && Reflect.has(environment, name), + ownKeys: () => Reflect.ownKeys(environment).filter(allowed), + getOwnPropertyDescriptor: (_, name) => + allowed(name) && Reflect.has(environment, name) + ? { + configurable: true, + enumerable: true, + value: Reflect.get(environment, name), + writable: false, + } + : undefined, + set: () => false, + deleteProperty: () => false, + defineProperty: () => false, + }, + ); }; /** Each driver's home variable, and the settings that rewrite it at launch. */ diff --git a/apps/server/src/provider/ProviderHostPathWatcher.ts b/apps/server/src/provider/ProviderHostPathWatcher.ts new file mode 100644 index 000000000000..b77d9ec39e31 --- /dev/null +++ b/apps/server/src/provider/ProviderHostPathWatcher.ts @@ -0,0 +1,18 @@ +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Stream from "effect/Stream"; + +import * as DesktopTelemetryReceiver from "../resourceTelemetry/DesktopTelemetryReceiver.ts"; +import * as ProviderRegistry from "./ProviderRegistry.ts"; + +/** The desktop's shell PATH can arrive after drivers were built and checked against a bare one. */ +export const layer = Layer.effectDiscard( + Effect.gen(function* () { + const receiver = yield* DesktopTelemetryReceiver.DesktopTelemetryReceiver; + const providers = yield* ProviderRegistry.ProviderRegistry; + yield* receiver.pathChanges.pipe( + Stream.runForEach(() => providers.rebuildMissingAndRefresh), + Effect.forkScoped, + ); + }), +); diff --git a/apps/server/src/provider/ProviderInstanceRegistry.test.ts b/apps/server/src/provider/ProviderInstanceRegistry.test.ts index 4bae35385d62..0280bb7246dc 100644 --- a/apps/server/src/provider/ProviderInstanceRegistry.test.ts +++ b/apps/server/src/provider/ProviderInstanceRegistry.test.ts @@ -944,6 +944,23 @@ describe("ProviderInstanceRegistry secret environment", () => { }).pipe(Effect.scoped); }); + it.effect("does not rebuild an instance replaced after selection", () => { + const fixture = setup({ [secret.reference]: "sk-live" }); + return Effect.gen(function* () { + const { registry, mutator } = yield* fixture.make; + const selected = yield* registry.listInstances; + fixture.vault.set(secret.reference, "sk-rotated"); + yield* mutator.reloadSecrets(); + const replacement = yield* registry.getInstance(instanceId); + expect(replacement).not.toBe(selected[0]); + yield* mutator.rebuild(selected); + expect(yield* registry.getInstance(instanceId)).toBe(replacement); + expect(fixture.created).toHaveLength(2); + yield* mutator.rebuild([replacement!]); + expect(fixture.created).toHaveLength(3); + }).pipe(Effect.scoped); + }); + it.effect("reads a config field typed SettingsSecretValue and rebuilds when it rotates", () => { const apiKey: SecretReference = { ...secret, reference: "op://Private/Gateway/key" }; const fixture = setup( diff --git a/apps/server/src/provider/ProviderInstanceRegistry.ts b/apps/server/src/provider/ProviderInstanceRegistry.ts index cc3ca60fcb82..23a26ff6fdcc 100644 --- a/apps/server/src/provider/ProviderInstanceRegistry.ts +++ b/apps/server/src/provider/ProviderInstanceRegistry.ts @@ -504,6 +504,22 @@ export const makeProviderInstanceRegistry = Effect.fn( Effect.provideContext(driverContext), ); + const rebuild: ProviderInstanceRegistryMutator.ProviderInstanceRegistryMutatorShape["rebuild"] = + Effect.fn("ProviderInstanceRegistry.rebuild")( + function* (instances) { + const live = yield* Ref.get(entries); + const ids = new Set( + instances + .filter((instance) => live.get(instance.instanceId)?.instance === instance) + .map((instance) => instance.instanceId), + ); + if (ids.size === 0) return; + yield* reconcileWithR(yield* Ref.get(configMapRef), ids); + }, + (effect) => semaphore.withPermits(1)(effect), + Effect.provideContext(driverContext), + ); + // Reading secrets can wait on a vault unlock, so the server does not wait for it to start. yield* semaphore.withPermits(1)( Ref.set(configMapRef, input.configMap).pipe( @@ -554,6 +570,7 @@ export const makeProviderInstanceRegistry = Effect.fn( const mutator: ProviderInstanceRegistryMutator.ProviderInstanceRegistryMutatorShape = { reconcile, reloadSecrets, + rebuild, }; return { registry, mutator }; diff --git a/apps/server/src/provider/ProviderInstanceRegistryMutator.ts b/apps/server/src/provider/ProviderInstanceRegistryMutator.ts index 35eb52d3f290..2a5659cf6a3d 100644 --- a/apps/server/src/provider/ProviderInstanceRegistryMutator.ts +++ b/apps/server/src/provider/ProviderInstanceRegistryMutator.ts @@ -1,5 +1,6 @@ /** Reconcile closes removed/replaced scopes before creating replacements: at most one live instance per id. */ import type { ProviderInstanceConfigMap, ProviderInstanceId } from "@t3tools/contracts"; +import type { ProviderInstance } from "@t3tools/provider-core/server/driver"; import * as Context from "effect/Context"; import type * as Effect from "effect/Effect"; @@ -7,6 +8,8 @@ export interface ProviderInstanceRegistryMutatorShape { readonly reconcile: (configMap: ProviderInstanceConfigMap) => Effect.Effect; /** Re-reads the secrets of every instance, or one, and rebuilds those whose secrets changed. */ readonly reloadSecrets: (instanceId?: ProviderInstanceId) => Effect.Effect; + /** Rebuilds these instances from their current config, so their drivers read the environment again. */ + readonly rebuild: (instances: ReadonlyArray) => Effect.Effect; } export class ProviderInstanceRegistryMutator extends Context.Service< diff --git a/apps/server/src/provider/ProviderRegistry.test.ts b/apps/server/src/provider/ProviderRegistry.test.ts index e318a3c79a66..d923726d3a2f 100644 --- a/apps/server/src/provider/ProviderRegistry.test.ts +++ b/apps/server/src/provider/ProviderRegistry.test.ts @@ -68,6 +68,7 @@ import { } from "./providerStatusCache.ts"; import { COMPACT_SLASH_COMMAND } from "@t3tools/provider-core/server/snapshotProbe"; import type { + AnyProviderDriver, ProviderInstance, ProviderWorkspaceSnapshot, } from "@t3tools/provider-core/server/driver"; @@ -76,6 +77,10 @@ import * as ProviderInstanceRegistryMutator from "./ProviderInstanceRegistryMuta import * as ProviderRegistry from "./ProviderRegistry.ts"; import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment"; import { makeManualOnlyProviderMaintenanceCapabilities } from "@t3tools/provider-core/server/maintenanceResolver"; +import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver"; +import * as ProviderHostPathWatcher from "./ProviderHostPathWatcher.ts"; +import * as DesktopTelemetryReceiver from "../resourceTelemetry/DesktopTelemetryReceiver.ts"; +import { providerHostEnvironment } from "../policy/managedProviders.ts"; vi.mock("@anthropic-ai/claude-agent-sdk", { spy: true }); const decodeServerProvider = Schema.decodeSync(ServerProvider); @@ -1156,6 +1161,165 @@ it.layer( ); }); + it.effect.each(["replay", "in-flight missing", "in-flight installed"] as const)( + "recovers a copied driver environment after late PATH (%s)", + (mode) => + Effect.gen(function* () { + const host = { PATH: "/early/bin", T3CODE_CODEX_LAUNCH_ARGS: "hidden" }; + const environment = providerHostEnvironment( + { organization: "PATH test", lockedSettings: [] }, + host, + ); + const updates = yield* DesktopTelemetryReceiver.makeDesktopShellEnvironmentUpdates( + host, + "linux", + ); + const entered = yield* Deferred.make(); + const release = yield* Deferred.make(); + const finished = yield* Deferred.make(); + const paths: Array = []; + let first = true; + const driverKind = ProviderDriverKind.make("pi"); + const instanceId = ProviderInstanceId.make("late-path"); + const driver: AnyProviderDriver = { + driverKind, + metadata: { displayName: "PATH fixture" }, + configSchema: Schema.Unknown, + defaultConfig: () => ({}), + create: (input) => + Effect.gen(function* () { + const copied = { ...environment }; + paths.push(copied.PATH); + const installed = + mode === "in-flight installed" || copied.PATH?.includes("/late/bin") === true; + const snapshot: ServerProvider = { + instanceId, + driver: driverKind, + enabled: true, + installed: false, + status: "warning", + auth: { status: "unknown" }, + version: null, + checkedAt: "2026-10-03T00:00:00.000Z", + models: [], + slashCommands: [], + skills: [], + }; + const current = yield* Ref.make(snapshot); + return { + instanceId: input.instanceId, + driverKind, + enabled: true, + displayName: undefined, + continuationIdentity: { driverKind, continuationKey: "pi:late-path" }, + snapshot: { + resolveMaintenance: () => + Effect.succeed( + makeManualOnlyProviderMaintenanceCapabilities({ + provider: driverKind, + packageName: null, + }), + ), + getSnapshot: Ref.get(current), + refresh: Effect.gen(function* () { + if (first && mode !== "replay") { + first = false; + yield* Deferred.succeed(entered, undefined); + yield* Deferred.await(release); + } + const checked = { + ...snapshot, + installed, + status: installed ? ("ready" as const) : ("error" as const), + }; + yield* Ref.set(current, checked); + return checked; + }), + streamChanges: Stream.empty, + applyUsageLimits: () => Effect.void, + }, + orchestrationAdapter: {} as ProviderInstance["orchestrationAdapter"], + textGeneration: {} as ProviderInstance["textGeneration"], + } satisfies ProviderInstance; + }), + }; + const built = yield* ProviderInstanceRegistry.makeProviderInstanceRegistry({ + drivers: [driver], + configMap: { [instanceId]: { driver: driverKind, enabled: true } }, + }).pipe(Effect.provide(Layer.mock(SecretResolver.SecretResolver)({}))); + yield* Effect.gen(function* () { + const registry = yield* ProviderRegistry.ProviderRegistry; + const running = + mode === "replay" ? undefined : yield* registry.refresh().pipe(Effect.forkScoped); + if (running) yield* Deferred.await(entered); + yield* updates.apply({ + version: 1, + type: "shellEnvironment", + platform: "linux", + discovered: true, + environment: { PATH: "/early/bin:/late/bin" }, + }); + expect(paths).toEqual(["/early/bin"]); + if (mode === "replay") { + yield* Layer.build( + ProviderHostPathWatcher.layer.pipe( + Layer.provide( + DesktopTelemetryReceiver.layerTest({ pathChanges: updates.pathChanges }), + ), + Layer.provide( + Layer.succeed(ProviderRegistry.ProviderRegistry, { + ...registry, + rebuildMissingAndRefresh: registry.rebuildMissingAndRefresh.pipe( + Effect.tap(() => Deferred.succeed(finished, undefined)), + ), + }), + ), + ), + ); + } else { + yield* registry.rebuildMissingAndRefresh.pipe( + Effect.tap(() => Deferred.succeed(finished, undefined)), + Effect.forkScoped({ startImmediately: true }), + ); + expect(paths).toEqual(["/early/bin"]); + } + yield* Deferred.succeed(release, undefined); + if (running) yield* Fiber.join(running); + yield* Deferred.await(finished); + expect( + (yield* registry.getProviders).find((row) => row.instanceId === instanceId) + ?.installed, + ).toBe(true); + expect(paths).toEqual( + mode === "in-flight installed" + ? ["/early/bin"] + : ["/early/bin", "/early/bin:/late/bin"], + ); + }).pipe( + Effect.provide( + ProviderRegistry.layer.pipe( + Layer.provide( + Layer.succeed(ProviderInstanceRegistry.ProviderInstanceRegistry, built.registry), + ), + Layer.provide( + Layer.succeed( + ProviderInstanceRegistryMutator.ProviderInstanceRegistryMutator, + built.mutator, + ), + ), + ), + ), + ); + }).pipe( + Effect.scoped, + Effect.provide( + ServerConfig.layerTest(process.cwd(), { prefix: "t3-late-path-" }).pipe( + Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), + ), + ), + ), + ); + describe("Codex model inventories", () => { const cachedProvider = { instanceId: ProviderInstanceId.make("codex-personal"), diff --git a/apps/server/src/provider/ProviderRegistry.ts b/apps/server/src/provider/ProviderRegistry.ts index fb6aeaac26b6..537a46b08a36 100644 --- a/apps/server/src/provider/ProviderRegistry.ts +++ b/apps/server/src/provider/ProviderRegistry.ts @@ -69,6 +69,12 @@ export class ProviderRegistry extends Context.Service< instanceId?: ProviderInstanceId, ) => Effect.Effect>; + /** + * Rebuilds and re-checks enabled instances not yet found installed, after the host environment + * changed. Drivers copy the environment when built, so only a rebuild sees a later PATH. + */ + readonly rebuildMissingAndRefresh: Effect.Effect>; + /** Only refreshed instances can verify an update; the published list still includes every row. */ readonly refreshAfterUpdate: ( provider: ProviderDriverKind, @@ -692,6 +698,28 @@ export const layer = Layer.effect( return yield* instanceId === undefined ? refresh() : refreshInstance(instanceId); }); + // Instances that found their CLI keep running; restarting them could end live sessions. + const rebuildMissingAndRefresh = Effect.fn("rebuildMissingAndRefresh")(function* () { + const instances = yield* instanceRegistry.listInstances; + yield* refresh(); + const missing = yield* Effect.filter(instances, (instance) => + Effect.gen(function* () { + // ACP also reports installed:false for setup states unrelated to executable discovery. + if ( + !instance.enabled || + instance.driverKind === "acpRegistry" || + !(yield* isCurrentInstance(instance)) + ) + return false; + const snapshot = yield* instance.snapshot.getSnapshot; + return !snapshot.installed; + }), + ); + if (missing.length === 0) return yield* Ref.get(providersRef); + yield* instanceRegistryMutator.rebuild(missing); + return yield* refresh(); + }); + const refreshAfterUpdate = Effect.fn("refreshAfterUpdate")(function* ( provider: ProviderDriverKind, selectedInstanceId: ProviderInstanceId, @@ -1077,6 +1105,9 @@ export const layer = Layer.effect( refreshInstance(instanceId).pipe(Effect.catchCause(recoverRefreshFailure)), reloadSecretsAndRefresh: (instanceId?: ProviderInstanceId) => reloadSecretsAndRefresh(instanceId).pipe(Effect.catchCause(recoverRefreshFailure)), + rebuildMissingAndRefresh: rebuildMissingAndRefresh().pipe( + Effect.catchCause(recoverRefreshFailure), + ), refreshWorkspaceSnapshot: (input) => refreshWorkspaceSnapshot(input).pipe(Effect.catchCause(recoverRefreshFailure)), refreshAfterUpdate, diff --git a/apps/server/src/provider/providerMaintenanceRunner.test.ts b/apps/server/src/provider/providerMaintenanceRunner.test.ts index 77a44c7fa29a..9d9cd24ecd1e 100644 --- a/apps/server/src/provider/providerMaintenanceRunner.test.ts +++ b/apps/server/src/provider/providerMaintenanceRunner.test.ts @@ -198,6 +198,7 @@ function makeRegistry( refresh: () => Ref.get(providersRef), refreshInstance: () => Ref.get(providersRef), reloadSecretsAndRefresh: () => Ref.get(providersRef), + rebuildMissingAndRefresh: Ref.get(providersRef), refreshAfterUpdate: () => { const version = options?.installedVersionAfterUpdate; if (version === undefined) { diff --git a/apps/server/src/provider/testUtils/providerRegistryMock.ts b/apps/server/src/provider/testUtils/providerRegistryMock.ts index f75ecd141e71..221f4de50c43 100644 --- a/apps/server/src/provider/testUtils/providerRegistryMock.ts +++ b/apps/server/src/provider/testUtils/providerRegistryMock.ts @@ -12,6 +12,7 @@ const makeProviderRegistryMock = ( refresh: () => Effect.succeed(providers), refreshInstance: () => Effect.succeed(providers), reloadSecretsAndRefresh: () => Effect.succeed(providers), + rebuildMissingAndRefresh: Effect.succeed(providers), refreshAfterUpdate: () => Effect.succeed(providers), refreshWorkspaceSnapshot: () => Effect.succeed(providers), invalidateModelCaches: () => Effect.void, diff --git a/apps/server/src/resourceTelemetry/DesktopTelemetryReceiver.ts b/apps/server/src/resourceTelemetry/DesktopTelemetryReceiver.ts index 7d9de87ecfd9..c769e5e95796 100644 --- a/apps/server/src/resourceTelemetry/DesktopTelemetryReceiver.ts +++ b/apps/server/src/resourceTelemetry/DesktopTelemetryReceiver.ts @@ -175,6 +175,8 @@ export class DesktopTelemetryReceiver extends Context.Service< never, Scope.Scope >; + /** Emits when a desktop shell environment changes this process's PATH; replays the last one. */ + readonly pathChanges: Stream.Stream; readonly health: Effect.Effect; readonly subscribeHealth: Effect.Effect< { @@ -395,12 +397,31 @@ export function requireDesktopTelemetryWriteProgress( : Effect.fail(new DesktopTelemetryControlStalled({ fd, remainingBytes })); } +export const makeDesktopShellEnvironmentUpdates = Effect.fn( + "DesktopTelemetryReceiver.makeDesktopShellEnvironmentUpdates", +)(function* (environment: NodeJS.ProcessEnv, platform: NodeJS.Platform) { + const applyShellEnvironment = makeApplyDesktopShellEnvironmentSnapshot(environment, platform); + // Replayed, because the provider watcher may subscribe after the snapshot arrived. + const pathChanges = yield* Effect.acquireRelease( + PubSub.sliding({ capacity: 1, replay: 1 }), + PubSub.shutdown, + ); + const readPath = () => environment.PATH ?? environment.Path; + // Untraced: applyShellEnvironment already carries this operation's span. + const apply = Effect.fnUntraced(function* (message: DesktopShellEnvironmentSnapshot) { + const before = readPath(); + yield* applyShellEnvironment(message); + if (readPath() !== before) yield* PubSub.publish(pathChanges, undefined); + }); + return { apply, pathChanges: Stream.fromPubSub(pathChanges) }; +}); + /** @public */ export const make = Effect.fn("resourceTelemetry.desktopTelemetryReceiver.make")(function* () { const config = yield* ServerConfig.ServerConfig; const environment = yield* HostProcess.Environment; const platform = yield* HostProcess.Platform; - const applyShellSnapshot = makeApplyDesktopShellEnvironmentSnapshot(environment, platform); + const shellEnvironment = yield* makeDesktopShellEnvironmentUpdates(environment, platform); const serverSettings = yield* ServerSettings.ServerSettingsService; const latest = yield* Ref.make(Option.none()); const receiverStartedAt = yield* DateTime.now; @@ -597,7 +618,7 @@ export const make = Effect.fn("resourceTelemetry.desktopTelemetryReceiver.make") } if (message.type === "shellEnvironment") { - return recordContact.pipe(Effect.andThen(applyShellSnapshot(message))); + return recordContact.pipe(Effect.andThen(shellEnvironment.apply(message))); } if (message.type === "desktopUpdateStatus") { @@ -704,6 +725,7 @@ export const make = Effect.fn("resourceTelemetry.desktopTelemetryReceiver.make") return DesktopTelemetryReceiver.of({ latest: Ref.get(latest), changes: Stream.fromPubSub(changes), + pathChanges: shellEnvironment.pathChanges, subscribe: snapshotMutex.withPermits(1)( Effect.gen(function* () { const initial = yield* Ref.get(latest); @@ -767,6 +789,7 @@ export const layerTest = ( DesktopTelemetryReceiver.of({ latest, changes, + pathChanges: overrides.pathChanges ?? Stream.empty, subscribe: overrides.subscribe ?? latest.pipe( diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index 53f53db72cc5..146aebcdf52c 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -70,6 +70,7 @@ import { BUILT_IN_SOURCE_CONTROL_DRIVER_LIST } from "./sourceControl/sourceContr import * as TextGeneration from "./textGeneration/TextGeneration.ts"; import * as SecretResolver from "./secrets/SecretResolver.ts"; import * as ProviderInstanceRegistryHydration from "./provider/ProviderInstanceRegistryHydration.ts"; +import * as ProviderHostPathWatcher from "./provider/ProviderHostPathWatcher.ts"; import * as ProjectSettingsOverrideSweep from "./provider/ProjectSettingsOverrideSweep.ts"; import * as TerminalManager from "./terminal/Manager.ts"; import * as McpHttpServer from "./mcp/McpHttpServer.ts"; @@ -626,6 +627,7 @@ const layerProviderInstallationRefresh = Layer.effectDiscard( const layerRuntimeCoreDependenciesBase = Layer.mergeAll( AgentAwarenessRelay.layer, + ProviderHostPathWatcher.layer.pipe(Layer.provide(layerDesktopTelemetryReceiver)), HeldHooksWaker.layer, layerThreadSettlementWorker, layerIdleCompactionWorker, From a4b98b1430fbfbae6f9950ff07a890c52390dbc6 Mon Sep 17 00:00:00 2001 From: eyeveil <64274427+eyeveil@users.noreply.github.com> Date: Fri, 6 Nov 2026 09:49:15 +0100 Subject: [PATCH 4/6] Re-check providers whose check failed when a late shell PATH arrives --- .../src/provider/ProviderRegistry.test.ts | 28 +++++++++++-------- apps/server/src/provider/ProviderRegistry.ts | 7 +++-- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/apps/server/src/provider/ProviderRegistry.test.ts b/apps/server/src/provider/ProviderRegistry.test.ts index d923726d3a2f..ca9f6f1a1d8f 100644 --- a/apps/server/src/provider/ProviderRegistry.test.ts +++ b/apps/server/src/provider/ProviderRegistry.test.ts @@ -1161,10 +1161,13 @@ it.layer( ); }); - it.effect.each(["replay", "in-flight missing", "in-flight installed"] as const)( + it.effect.each(["replay", "in-flight missing", "in-flight installed", "launcher"] as const)( "recovers a copied driver environment after late PATH (%s)", (mode) => Effect.gen(function* () { + // "launcher": the CLI is found on the early PATH, but its check fails until the late + // PATH brings the interpreter it needs, like an npm launcher that runs node. + const replay = mode === "replay" || mode === "launcher"; const host = { PATH: "/early/bin", T3CODE_CODEX_LAUNCH_ARGS: "hidden" }; const environment = providerHostEnvironment( { organization: "PATH test", lockedSettings: [] }, @@ -1190,8 +1193,9 @@ it.layer( Effect.gen(function* () { const copied = { ...environment }; paths.push(copied.PATH); - const installed = - mode === "in-flight installed" || copied.PATH?.includes("/late/bin") === true; + const late = copied.PATH?.includes("/late/bin") === true; + const installed = mode === "in-flight installed" || mode === "launcher" || late; + const ready = mode === "launcher" ? late : installed; const snapshot: ServerProvider = { instanceId, driver: driverKind, @@ -1222,7 +1226,7 @@ it.layer( ), getSnapshot: Ref.get(current), refresh: Effect.gen(function* () { - if (first && mode !== "replay") { + if (first && !replay) { first = false; yield* Deferred.succeed(entered, undefined); yield* Deferred.await(release); @@ -1230,7 +1234,7 @@ it.layer( const checked = { ...snapshot, installed, - status: installed ? ("ready" as const) : ("error" as const), + status: ready ? ("ready" as const) : ("error" as const), }; yield* Ref.set(current, checked); return checked; @@ -1249,8 +1253,7 @@ it.layer( }).pipe(Effect.provide(Layer.mock(SecretResolver.SecretResolver)({}))); yield* Effect.gen(function* () { const registry = yield* ProviderRegistry.ProviderRegistry; - const running = - mode === "replay" ? undefined : yield* registry.refresh().pipe(Effect.forkScoped); + const running = replay ? undefined : yield* registry.refresh().pipe(Effect.forkScoped); if (running) yield* Deferred.await(entered); yield* updates.apply({ version: 1, @@ -1260,7 +1263,7 @@ it.layer( environment: { PATH: "/early/bin:/late/bin" }, }); expect(paths).toEqual(["/early/bin"]); - if (mode === "replay") { + if (replay) { yield* Layer.build( ProviderHostPathWatcher.layer.pipe( Layer.provide( @@ -1286,10 +1289,11 @@ it.layer( yield* Deferred.succeed(release, undefined); if (running) yield* Fiber.join(running); yield* Deferred.await(finished); - expect( - (yield* registry.getProviders).find((row) => row.instanceId === instanceId) - ?.installed, - ).toBe(true); + const row = (yield* registry.getProviders).find( + (candidate) => candidate.instanceId === instanceId, + ); + expect(row?.installed).toBe(true); + expect(row?.status).toBe("ready"); expect(paths).toEqual( mode === "in-flight installed" ? ["/early/bin"] diff --git a/apps/server/src/provider/ProviderRegistry.ts b/apps/server/src/provider/ProviderRegistry.ts index 537a46b08a36..bd8832415247 100644 --- a/apps/server/src/provider/ProviderRegistry.ts +++ b/apps/server/src/provider/ProviderRegistry.ts @@ -70,7 +70,7 @@ export class ProviderRegistry extends Context.Service< ) => Effect.Effect>; /** - * Rebuilds and re-checks enabled instances not yet found installed, after the host environment + * Rebuilds and re-checks enabled instances not found or whose check failed, after the host environment * changed. Drivers copy the environment when built, so only a rebuild sees a later PATH. */ readonly rebuildMissingAndRefresh: Effect.Effect>; @@ -698,7 +698,8 @@ export const layer = Layer.effect( return yield* instanceId === undefined ? refresh() : refreshInstance(instanceId); }); - // Instances that found their CLI keep running; restarting them could end live sessions. + // Instances whose check passed keep running; restarting them could end live sessions. A failed + // check is rebuilt too: an npm launcher is found on the bare PATH but cannot find its node. const rebuildMissingAndRefresh = Effect.fn("rebuildMissingAndRefresh")(function* () { const instances = yield* instanceRegistry.listInstances; yield* refresh(); @@ -712,7 +713,7 @@ export const layer = Layer.effect( ) return false; const snapshot = yield* instance.snapshot.getSnapshot; - return !snapshot.installed; + return !snapshot.installed || snapshot.status === "error"; }), ); if (missing.length === 0) return yield* Ref.get(providersRef); From 3e807310189265119af7ee6ac9359f82a1a73c0c Mon Sep 17 00:00:00 2001 From: eyeveil <64274427+eyeveil@users.noreply.github.com> Date: Fri, 6 Nov 2026 09:50:47 +0100 Subject: [PATCH 5/6] Track carried upstream PRs #28719 and #31169 --- .github/fork/carried-prs.json | 10 ++++++++++ apps/desktop/src/shell/DesktopShellEnvironment.test.ts | 1 - 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.github/fork/carried-prs.json b/.github/fork/carried-prs.json index bc7256800173..bfc493b93a54 100644 --- a/.github/fork/carried-prs.json +++ b/.github/fork/carried-prs.json @@ -44,6 +44,16 @@ "number": 19273, "takenSha": "b8d5c37cf59f7989edaf14dea2822f28ae841fdb", "reason": "Local-model composer dictation" + }, + { + "number": 28719, + "takenSha": "8dc3e6ff446dc1f1a714288091249ee86c91735b", + "reason": "Desktop cache miss lets the server hydrate PATH before provider checks" + }, + { + "number": 31169, + "takenSha": "afede16a7a38b21be21ee696a1c3027f6f6b73e7", + "reason": "Providers re-check when a shell PATH arrives after startup; fork also re-checks failed checks" } ] } diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts index 6cd69ce760b0..057e0dbf68a0 100644 --- a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts +++ b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts @@ -5,7 +5,6 @@ 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"; import * as Layer from "effect/Layer"; import * as Logger from "effect/Logger"; From 733bc29a7cd787a184fd6635a668970cb23cad8e Mon Sep 17 00:00:00 2001 From: eyeveil <64274427+eyeveil@users.noreply.github.com> Date: Fri, 6 Nov 2026 10:01:15 +0100 Subject: [PATCH 6/6] Read pending secrets outside the registry lock before a late-PATH rebuild --- .../provider/ProviderInstanceRegistry.test.ts | 51 +++++++++++++++++++ .../src/provider/ProviderInstanceRegistry.ts | 49 +++++++++++++----- 2 files changed, 86 insertions(+), 14 deletions(-) diff --git a/apps/server/src/provider/ProviderInstanceRegistry.test.ts b/apps/server/src/provider/ProviderInstanceRegistry.test.ts index c8748da340f3..d7bf1f1f42ee 100644 --- a/apps/server/src/provider/ProviderInstanceRegistry.test.ts +++ b/apps/server/src/provider/ProviderInstanceRegistry.test.ts @@ -32,6 +32,7 @@ import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Path from "effect/Path"; import * as PubSub from "effect/PubSub"; +import * as Queue from "effect/Queue"; import * as Schema from "effect/Schema"; import * as TestClock from "effect/testing/TestClock"; import { HttpClient, HttpClientResponse } from "effect/http"; @@ -962,6 +963,56 @@ describe("ProviderInstanceRegistry secret environment", () => { }).pipe(Effect.scoped); }); + it.effect("keeps a rebuilt instance live while another instance's secret read waits", () => { + const plainId = ProviderInstanceId.make("fake_plain"); + const created: Array = []; + return Effect.gen(function* () { + const gate = yield* Deferred.make(); + const readStarts = yield* Queue.unbounded(); + const driver: AnyProviderDriver = { + driverKind, + metadata: { displayName: "Fake" }, + configSchema: Schema.Unknown, + defaultConfig: () => ({}), + create: (input) => + Effect.sync(() => { + created.push(input.instanceId); + return { instanceId: input.instanceId, enabled: input.enabled } as ProviderInstance; + }), + }; + const resolver = Layer.succeed(SecretResolver.SecretResolver, { + read: (references) => + Queue.offer(readStarts, undefined).pipe( + Effect.andThen(Deferred.await(gate)), + Effect.as(references.map(() => "sk-live")), + ), + invalidate: () => Effect.void, + }); + const { registry, mutator } = yield* makeProviderInstanceRegistry({ + drivers: [driver], + configMap: { + [instanceId]: { + driver: driverKind, + config: {}, + environment: [{ name: "OPENAI_API_KEY", value: secret, sensitive: true }], + }, + [plainId]: { driver: driverKind, config: {} }, + }, + }).pipe(Effect.provide(resolver)); + const plain = yield* registry.getInstance(plainId); + // Startup's read and the rebuild's read both wait on the vault. + const rebuilding = yield* mutator.rebuild([plain!]).pipe(Effect.forkScoped); + yield* Queue.take(readStarts); + yield* Queue.take(readStarts); + expect(yield* registry.getInstance(plainId)).toBe(plain); + + yield* Deferred.succeed(gate, undefined); + yield* Fiber.join(rebuilding); + expect(yield* registry.getInstance(plainId)).not.toBe(plain); + expect(created.filter((id) => id === plainId)).toHaveLength(2); + }).pipe(Effect.scoped); + }); + it.effect("reads a config field typed SettingsSecretValue and rebuilds when it rotates", () => { const apiKey: SecretReference = { ...secret, reference: "op://Private/Gateway/key" }; const fixture = setup( diff --git a/apps/server/src/provider/ProviderInstanceRegistry.ts b/apps/server/src/provider/ProviderInstanceRegistry.ts index 23a26ff6fdcc..65ab6b3b53a0 100644 --- a/apps/server/src/provider/ProviderInstanceRegistry.ts +++ b/apps/server/src/provider/ProviderInstanceRegistry.ts @@ -505,20 +505,41 @@ export const makeProviderInstanceRegistry = Effect.fn( ); const rebuild: ProviderInstanceRegistryMutator.ProviderInstanceRegistryMutatorShape["rebuild"] = - Effect.fn("ProviderInstanceRegistry.rebuild")( - function* (instances) { - const live = yield* Ref.get(entries); - const ids = new Set( - instances - .filter((instance) => live.get(instance.instanceId)?.instance === instance) - .map((instance) => instance.instanceId), - ); - if (ids.size === 0) return; - yield* reconcileWithR(yield* Ref.get(configMapRef), ids); - }, - (effect) => semaphore.withPermits(1)(effect), - Effect.provideContext(driverContext), - ); + Effect.fn("ProviderInstanceRegistry.rebuild")(function* (instances) { + // Reconcile also builds every pending instance, so read the secrets it needs first and + // outside the lock, as startup does; a vault prompt then holds neither the lock nor the + // selected instances, which stay live until the cached read lets them rebuild at once. + const configMap = yield* Ref.get(configMapRef); + const live = yield* Ref.get(entries); + const selected = new Set(instances.map((instance) => instance.instanceId)); + const references = yield* Effect.filter( + Object.entries(configMap), + ([rawInstanceId, entry]) => { + const id = ProviderInstanceId.make(rawInstanceId); + return (selected.has(id) || !live.has(id)) && + secretReferencesOf(driversById, entry).length > 0 + ? isEntryEnabled(driversById, entry) + : Effect.succeed(false); + }, + ).pipe( + Effect.map((targets) => + targets.flatMap(([, entry]) => secretReferencesOf(driversById, entry)), + ), + ); + if (references.length > 0) yield* (yield* SecretResolver.SecretResolver).read(references); + yield* semaphore.withPermits(1)( + Effect.gen(function* () { + const current = yield* Ref.get(entries); + const ids = new Set( + instances + .filter((instance) => current.get(instance.instanceId)?.instance === instance) + .map((instance) => instance.instanceId), + ); + if (ids.size === 0) return; + yield* reconcileWithR(yield* Ref.get(configMapRef), ids); + }), + ); + }, Effect.provideContext(driverContext)); // Reading secrets can wait on a vault unlock, so the server does not wait for it to start. yield* semaphore.withPermits(1)(