TypeScript
fix(server): subscribe before provider settings watcher (#9271)
t3-code[bot]f90e2f2bd26e
diff --git a/apps/server/src/provider/Layers/ProviderInstanceRegistryHydration.ts b/apps/server/src/provider/Layers/ProviderInstanceRegistryHydration.tsindex 0fd88b4262a..31c9fcbe871 100644--- a/apps/server/src/provider/Layers/ProviderInstanceRegistryHydration.ts+++ b/apps/server/src/provider/Layers/ProviderInstanceRegistryHydration.ts@@ -31,7 +31,7 @@* 1. Read the current `ServerSettings` once and use it to seed the* registry's initial state via `ProviderInstanceRegistryMutableLayer`.* 2. Fork a daemon fiber (lifetime tied to the layer's scope) that- * subscribes to `ServerSettingsService.streamChanges` and calls+ * acquires `ServerSettingsService.subscribeChanges` and calls* `ProviderInstanceRegistryMutator.reconcile` on every emission.** Failures inside the watcher are logged and swallowed so a single bad@@ -118,7 +118,8 @@ const SettingsWatcherLive = Layer.effectDiscard(Effect.gen(function* () {const mutator = yield* ProviderInstanceRegistryMutator;const serverSettings = yield* ServerSettingsService;- yield* serverSettings.streamChanges.pipe(+ const settingsChanges = yield* serverSettings.subscribeChanges;+ yield* settingsChanges.pipe(Stream.runForEach((next) =>mutator.reconcile(deriveProviderInstanceConfigMap(next))@@ -141,8 +142,8 @@ const SettingsWatcherLive = Layer.effectDiscard(* - `ProviderInstanceRegistryMutableLayer` produces the registry +* mutator from the initial config map. Its scope owns every* per-instance child scope created during reconcile.- * - `SettingsWatcherLive` consumes the mutator and runs a daemon fiber- * in the same scope.+ * - `SettingsWatcherLive` consumes the mutator, acquires its settings+ * subscription before forking, and runs a daemon fiber in the same scope.** Composing via `Layer.provideMerge` makes the watcher's deps available* from the mutable layer while still surfacing the registry as an output.diff --git a/apps/server/src/provider/Layers/ProviderRegistry.test.ts b/apps/server/src/provider/Layers/ProviderRegistry.test.tsindex cc9fc58ccc4..08650758c30 100644--- a/apps/server/src/provider/Layers/ProviderRegistry.test.ts+++ b/apps/server/src/provider/Layers/ProviderRegistry.test.ts@@ -1944,7 +1944,8 @@ it.layer(Layer.mergeAll(NodeServices.layer, ServerSettingsModule.layerTest(), Teconst firstMissing = `t3code_codex_first_`;const secondMissing = `t3code_codex_second_`;const spawnedCommands: Array<string> = [];- const serverSettings = yield* makeMutableServerSettingsService(+ const allowLazySettingsStream = yield* Deferred.make<void>();+ const mutableServerSettings = yield* makeMutableServerSettingsService(decodeServerSettings(deepMerge(encodedDefaultServerSettings, {providers: {@@ -1957,6 +1958,14 @@ it.layer(Layer.mergeAll(NodeServices.layer, ServerSettingsModule.layerTest(), Te}),),);+ const serverSettings = {+ ...mutableServerSettings,+ streamChanges: Stream.unwrap(+ Deferred.await(allowLazySettingsStream).pipe(+ Effect.as(mutableServerSettings.streamChanges),+ ),+ ),+ } satisfies ServerSettingsModule.ServerSettingsService["Service"];const scope = yield* Scope.make();yield* Effect.addFinalizer(() => Scope.close(scope, Exit.void));const providerRegistryLayer = ProviderRegistryLive.pipe(@@ -2016,7 +2025,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, ServerSettingsModule.layerTest(), Teassert.deepStrictEqual(spawnedCommands, [firstMissing]);// Drive a settings change. The Hydration layer's- // `SettingsWatcherLive` consumes this via `streamChanges`,+ // `SettingsWatcherLive` consumes this via `subscribeChanges`,// calls `reconcile`, which rebuilds the codex instance (the// envelope changed because `binaryPath` differs → `entryEqual`// is false). The registry's `Stream.runForEach(@@ -2028,6 +2037,9 @@ it.layer(Layer.mergeAll(NodeServices.layer, ServerSettingsModule.layerTest(), Tecodex: { enabled: true, binaryPath: secondMissing },},});+ // Start the lazy stream only after publishing. A watcher that did+ // not subscribe before forking has already lost this update.+ yield* Deferred.succeed(allowLazySettingsStream, undefined);// Poll until the injected process boundary observes the new// executable. This verifies the public settings-to-probe behavior