Skip to content

Commit d2a60b6

Browse files
fix(server): preserve current providers across startup settings races
1 parent 0e0f549 commit d2a60b6

2 files changed

Lines changed: 130 additions & 220 deletions

File tree

‎apps/server/src/provider/ProviderInstanceRegistryHydration.ts‎

Lines changed: 3 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,3 @@
1-
/**
2-
* ProviderInstanceRegistryHydration — derive a `ProviderInstanceConfigMap`
3-
* from `ServerSettings` and keep `ProviderInstanceRegistry` in sync with it.
4-
*
5-
* The server still reads two shapes:
6-
*
7-
* 1. `settings.providerInstances` — the new driver-agnostic map the
8-
* registry expects. Keyed by `ProviderInstanceId`, values are
9-
* `ProviderInstanceConfig` envelopes.
10-
* 2. `settings.providers.<kind>` — the legacy single-instance-per-driver
11-
* fields (`providers.codex`, `providers.claudeAgent`, …). These are
12-
* the source of truth for every deployment that hasn't been migrated
13-
* yet to an explicit `providerInstances` entry.
14-
*
15-
* This module bridges (2) into (1) and wires the resulting map into a
16-
* mutable registry. For every built-in driver whose id is not already
17-
* present in `providerInstances` (keyed on
18-
* `defaultInstanceIdForDriver(driverKind)` — literally the driver kind as a
19-
* routing slug), we synthesize an envelope from the legacy field. The
20-
* registry decodes both flavours through the same `configSchema` and ends
21-
* up with one uniform `ProviderInstance` per entry.
22-
*
23-
* Explicit `providerInstances` entries always win — users can already
24-
* override the legacy `providers.<kind>` blob by authoring a
25-
* `providerInstances.codex` entry with a matching driver, and we don't
26-
* want the synthesized envelope to silently stomp their config.
27-
*
28-
* Hot-reload
29-
* ----------
30-
* On layer build we:
31-
* 1. Acquire `ServerSettingsService.subscribeChanges`, so a change saved
32-
* while the registry is still building is queued rather than lost.
33-
* 2. Read the current `ServerSettings` once and use it to seed the
34-
* registry's initial state via `ProviderInstanceRegistry.layer`.
35-
* 3. Fork a daemon fiber (lifetime tied to the layer's scope) that, on
36-
* every emission, re-reads the current settings and calls
37-
* `ProviderInstanceRegistryMutator.reconcile` with them. A queued
38-
* change may predate the seed, so its snapshot is not applied as is.
39-
*
40-
* Failures inside the watcher are logged and swallowed so a single bad
41-
* settings emission cannot kill the registry. Unknown drivers and invalid
42-
* configs already round-trip through the registry's own "unavailable"
43-
* shadow bucket.
44-
*
45-
* @module provider/ProviderInstanceRegistryHydration
46-
*/
471
import {
482
defaultInstanceIdForDriver,
493
providerInstanceConfigEnabledFlag,
@@ -115,28 +69,16 @@ export const deriveProviderInstanceConfigMap = (
11569
return merged as ProviderInstanceConfigMap;
11670
};
11771

118-
/**
119-
* Layer that consumes `ProviderInstanceRegistryMutator` and forks a
120-
* settings-watcher fiber. The fiber's lifetime is tied to the enclosing
121-
* layer scope (process lifetime in production), so it is interrupted on
122-
* shutdown without leaking.
123-
*
124-
* Errors inside the watcher are logged and swallowed — the registry's own
125-
* "unavailable" bucket already absorbs unknown drivers and invalid
126-
* configs, so the only way the watcher could fail is a settings stream
127-
* tear-down, which logs and exits cleanly.
128-
*/
12972
const layerSettingsWatcher = (settingsChanges: Stream.Stream<ServerSettings>) =>
13073
Layer.effectDiscard(
13174
Effect.gen(function* () {
13275
const mutator = yield* ProviderInstanceRegistryMutator.ProviderInstanceRegistryMutator;
13376
const serverSettings = yield* Settings.ServerSettingsService;
13477
yield* settingsChanges.pipe(
135-
Stream.runForEach((next) =>
78+
Stream.runForEach(() =>
13679
// The subscription predates the seed read, so a queued change can be
137-
// older than the seed. Reconcile against the current settings.
80+
// older than the seed. A failed current read must leave the registry intact.
13881
serverSettings.getSettings.pipe(
139-
Effect.orElseSucceed(() => next),
14082
Effect.flatMap((current) =>
14183
mutator.reconcile(deriveProviderInstanceConfigMap(current)),
14284
),
@@ -150,32 +92,14 @@ const layerSettingsWatcher = (settingsChanges: Stream.Stream<ServerSettings>) =>
15092
}),
15193
);
15294

153-
/**
154-
* Hydrate `ProviderInstanceRegistry` from `ServerSettings` and keep it in
155-
* sync with subsequent `streamChanges` emissions.
156-
*
157-
* The Layer's two halves:
158-
* - `ProviderInstanceRegistry.layer` produces the registry +
159-
* mutator from the initial config map. Its scope owns every
160-
* per-instance child scope created during reconcile.
161-
* - `layerSettingsWatcher` consumes the mutator and drains the settings
162-
* subscription, acquired before the seed read, in a daemon fiber in the
163-
* same scope.
164-
*
165-
* Composing via `Layer.provideMerge` makes the watcher's deps available
166-
* from the mutable layer while still surfacing the registry as an output.
167-
* The mutator tag is technically also exposed; only this module imports
168-
* it, so the visibility leak is harmless in practice.
169-
*/
17095
export const layer: Layer.Layer<
17196
ProviderInstanceRegistry.ProviderInstanceRegistry,
17297
never,
17398
ProviderInstanceRegistryHydrationEnv
17499
> = Layer.unwrap(
175100
Effect.gen(function* () {
176101
const serverSettings = yield* Settings.ServerSettingsService;
177-
// Subscribe before reading the seed: building the registry takes a while,
178-
// and a change published before the watcher subscribed would never arrive.
102+
// Subscribe before the seed read so saves during registry construction are queued.
179103
const settingsChanges = yield* serverSettings.subscribeChanges;
180104
const initialSettings: ServerSettings | undefined = yield* serverSettings.getSettings.pipe(
181105
Effect.orElseSucceed(() => undefined),

0 commit comments

Comments
 (0)