Skip to content

Commit 4f4350e

Browse files
committed
fix(server): keep provider sessions alive during cosmetic edits
1 parent 519d64f commit 4f4350e

4 files changed

Lines changed: 167 additions & 28 deletions

File tree

‎apps/server/src/provider/ProviderInstanceRegistry.test.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,80 @@ describe("ProviderInstanceRegistry — multi-instance codex slice", () => {
320320
}).pipe(Effect.provide(layerTest)),
321321
);
322322

323+
it.live("retains live instances for presentation edits but replaces runtime edits", () =>
324+
Effect.gen(function* () {
325+
let closed = 0;
326+
const trackingDriver: typeof CodexDriver = {
327+
...CodexDriver,
328+
create: (input) =>
329+
CodexDriver.create(input).pipe(
330+
Effect.tap(() =>
331+
Effect.addFinalizer(() =>
332+
Effect.sync(() => {
333+
closed += 1;
334+
}),
335+
),
336+
),
337+
),
338+
};
339+
const instanceId = ProviderInstanceId.make("codex_presentation");
340+
const baseEntry = {
341+
driver: ProviderDriverKind.make("codex"),
342+
displayName: "Personal",
343+
accentColor: "#123456",
344+
enabled: false,
345+
config: makeCodexConfig({}),
346+
};
347+
const { registry, mutator } = yield* makeProviderInstanceRegistry<CodexDriverEnv>({
348+
drivers: [trackingDriver],
349+
configMap: { [instanceId]: baseEntry },
350+
});
351+
const original = yield* registry.getInstance(instanceId);
352+
expect(original).toBeDefined();
353+
354+
yield* mutator.reconcile({
355+
[instanceId]: {
356+
...baseEntry,
357+
displayName: "Work",
358+
accentColor: "#654321",
359+
},
360+
});
361+
362+
const retained = yield* registry.getInstance(instanceId);
363+
expect(retained).toBe(original);
364+
expect(retained!.displayName).toBe("Work");
365+
expect(retained!.accentColor).toBe("#654321");
366+
expect((yield* retained!.snapshot.getSnapshot).displayName).toBe("Work");
367+
expect((yield* retained!.snapshot.getSnapshot).accentColor).toBe("#654321");
368+
expect(closed).toBe(0);
369+
370+
const clearedEntry = {
371+
driver: baseEntry.driver,
372+
enabled: baseEntry.enabled,
373+
config: baseEntry.config,
374+
};
375+
yield* mutator.reconcile({ [instanceId]: clearedEntry });
376+
const cleared = yield* registry.getInstance(instanceId);
377+
expect(cleared).toBe(original);
378+
expect(cleared!.displayName).toBeUndefined();
379+
expect(cleared!.accentColor).toBeUndefined();
380+
const clearedSnapshot = yield* cleared!.snapshot.getSnapshot;
381+
expect(clearedSnapshot.displayName).toBeUndefined();
382+
expect(clearedSnapshot.accentColor).toBeUndefined();
383+
expect(closed).toBe(0);
384+
385+
yield* mutator.reconcile({
386+
[instanceId]: {
387+
...clearedEntry,
388+
config: makeCodexConfig({ customModels: ["runtime-change"] }),
389+
},
390+
});
391+
const replaced = yield* registry.getInstance(instanceId);
392+
expect(replaced).not.toBe(original);
393+
expect(closed).toBe(1);
394+
}).pipe(Effect.provide(layerTest)),
395+
);
396+
323397
it.live("treats an explicit in-config enabled:false as disabling despite the envelope", () =>
324398
Effect.gen(function* () {
325399
// Old settings files can carry both flags with conflicting values.

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

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ export class ProviderInstanceRegistry extends Context.Service<
131131

132132
/**
133133
* Live registry entry: the materialized `ProviderInstance` + the fresh
134-
* child scope its `create` effect ran in + the original `entry` envelope
135-
* so `reconcile` can cheaply detect "no-op" updates.
134+
* child scope its `create` effect ran in + the latest `entry` envelope so
135+
* `reconcile` can compare subsequent runtime and presentation updates.
136136
*/
137137
interface LiveEntry {
138138
readonly instance: ProviderInstance;
139139
readonly scope: Scope.Closeable;
140-
readonly entry: ProviderInstanceConfig;
140+
entry: ProviderInstanceConfig;
141141
}
142142

143143
/**
@@ -151,14 +151,18 @@ interface RegistryState {
151151
}
152152

153153
/**
154-
* Structural equality on `ProviderInstanceConfig` envelopes. Used by
155-
* `reconcile` to skip rebuilds when settings arrive unchanged. Config
156-
* payloads are opaque `unknown` at the envelope layer; `Equal.equals`
157-
* falls back to structural equality for plain records, which matches how
158-
* the schema decode output is constructed.
154+
* Runtime identity equality on `ProviderInstanceConfig` envelopes. Changes
155+
* to display metadata are applied to the retained instance; driver, enable,
156+
* environment, and decoded config changes rebuild it. Config payloads are
157+
* opaque `unknown` at the envelope layer; `Equal.equals` falls back to
158+
* structural equality for plain records, which matches how the schema decode
159+
* output is constructed.
159160
*/
160-
const entryEqual = (a: ProviderInstanceConfig, b: ProviderInstanceConfig): boolean =>
161-
Equal.equals(a, b);
161+
const runtimeEntryEqual = (a: ProviderInstanceConfig, b: ProviderInstanceConfig): boolean =>
162+
a.driver === b.driver &&
163+
a.enabled === b.enabled &&
164+
Equal.equals(a.environment ?? [], b.environment ?? []) &&
165+
Equal.equals(a.config, b.config);
162166

163167
/**
164168
* Resolve an entry's enabled state. An explicit false on either the
@@ -269,13 +273,51 @@ const buildEntry = <R>(input: {
269273
};
270274
}
271275

276+
const baseInstance = createResult.success;
277+
let live!: LiveEntry;
278+
const withPresentation = <T extends ServerProvider>(snapshot: T): T => {
279+
const {
280+
displayName: _displayName,
281+
accentColor: _accentColor,
282+
...snapshotWithoutPresentation
283+
} = snapshot;
284+
return {
285+
...snapshotWithoutPresentation,
286+
...(live.entry.displayName === undefined ? {} : { displayName: live.entry.displayName }),
287+
...(live.entry.accentColor === undefined ? {} : { accentColor: live.entry.accentColor }),
288+
} as T;
289+
};
290+
const snapshot = {
291+
...baseInstance.snapshot,
292+
getSnapshot: baseInstance.snapshot.getSnapshot.pipe(Effect.map(withPresentation)),
293+
refresh: baseInstance.snapshot.refresh.pipe(Effect.map(withPresentation)),
294+
streamChanges: baseInstance.snapshot.streamChanges.pipe(Stream.map(withPresentation)),
295+
};
296+
const baseSnapshotForCwd = baseInstance.snapshotForCwd;
297+
const instance: ProviderInstance = {
298+
...baseInstance,
299+
get displayName() {
300+
return live.entry.displayName;
301+
},
302+
get accentColor() {
303+
return live.entry.accentColor;
304+
},
305+
snapshot,
306+
...(baseSnapshotForCwd
307+
? {
308+
snapshotForCwd: (cwd: string) =>
309+
baseSnapshotForCwd(cwd).pipe(Effect.map(withPresentation)),
310+
}
311+
: {}),
312+
};
313+
live = {
314+
instance,
315+
scope: childScope,
316+
entry,
317+
};
272318
return {
273319
kind: "live" as const,
274-
live: {
275-
instance: createResult.success,
276-
scope: childScope,
277-
entry,
278-
},
320+
live,
279321
};
280322
});
281323

@@ -298,18 +340,19 @@ const makeReconcile = <R>(input: {
298340
nextRaw.map(([raw]) => ProviderInstanceId.make(raw)),
299341
);
300342

301-
// 1. Close scopes for instances that disappeared or whose config
302-
// changed. Do this BEFORE creating replacements so ids map 1-to-1
303-
// to live scopes at all times.
343+
// 1. Close scopes for instances that disappeared or whose runtime
344+
// config changed. Do this BEFORE creating replacements so ids map
345+
// 1-to-1 to live scopes at all times.
304346
const removedIds: Array<ProviderInstanceId> = [];
305347
const replacedIds = new Set<ProviderInstanceId>();
348+
let presentationChanged = false;
306349
for (const [instanceId, live] of previousEntries) {
307350
if (!nextKeys.has(instanceId)) {
308351
removedIds.push(instanceId);
309352
continue;
310353
}
311354
const nextEntry = configMap[instanceId];
312-
if (nextEntry !== undefined && !entryEqual(live.entry, nextEntry)) {
355+
if (nextEntry !== undefined && !runtimeEntryEqual(live.entry, nextEntry)) {
313356
replacedIds.add(instanceId);
314357
}
315358
}
@@ -334,7 +377,10 @@ const makeReconcile = <R>(input: {
334377

335378
const existing = previousEntries.get(instanceId);
336379
if (existing !== undefined && !replacedIds.has(instanceId)) {
337-
// No-op update: keep the existing live entry and scope.
380+
// Presentation-only update: keep the live instance and scope while
381+
// letting its presentation-aware snapshots read the new entry.
382+
presentationChanged ||= !Equal.equals(existing.entry, entry);
383+
existing.entry = entry;
338384
builtEntries.set(instanceId, existing);
339385
continue;
340386
}
@@ -368,6 +414,7 @@ const makeReconcile = <R>(input: {
368414
orderChanged ||
369415
removedIds.length > 0 ||
370416
replacedIds.size > 0 ||
417+
presentationChanged ||
371418
builtEntries.size !== previousEntries.size;
372419
const unavailableChanged =
373420
builtUnavailable.size !== previousUnavailable.size ||

‎apps/server/src/provider/ProviderRegistry.test.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,8 @@ it.layer(
15811581
skills: [],
15821582
} as const satisfies ServerProvider;
15831583
const refreshCalls = yield* Ref.make(0);
1584+
const currentProvider = yield* Ref.make<ServerProvider>(initialProvider);
1585+
const registryChanges = yield* PubSub.unbounded<void>();
15841586
const instance = {
15851587
instanceId: codexInstanceId,
15861588
driverKind: codexDriver,
@@ -1598,7 +1600,7 @@ it.layer(
15981600
packageName: null,
15991601
}),
16001602
),
1601-
getSnapshot: Effect.succeed(initialProvider),
1603+
getSnapshot: Ref.get(currentProvider),
16021604
refresh: Ref.update(refreshCalls, (count) => count + 1).pipe(
16031605
Effect.andThen(Effect.never),
16041606
),
@@ -1615,8 +1617,8 @@ it.layer(
16151617
Effect.succeed(instanceId === codexInstanceId ? instance : undefined),
16161618
listInstances: Effect.succeed([instance]),
16171619
listUnavailable: Effect.succeed([]),
1618-
streamChanges: Stream.empty,
1619-
subscribeChanges: Effect.flatMap(PubSub.unbounded<void>(), PubSub.subscribe),
1620+
streamChanges: Stream.fromPubSub(registryChanges),
1621+
subscribeChanges: PubSub.subscribe(registryChanges),
16201622
},
16211623
);
16221624
const scope = yield* Scope.make();
@@ -1636,6 +1638,22 @@ it.layer(
16361638
const registry = yield* ProviderRegistry.ProviderRegistry;
16371639
assert.deepStrictEqual(yield* registry.getProviders, [initialProvider]);
16381640
assert.strictEqual(yield* Ref.get(refreshCalls), 0);
1641+
1642+
const updates = yield* registry.streamChanges.pipe(Stream.runHead, Effect.forkChild);
1643+
// Let Stream.runHead acquire its PubSub subscription before the
1644+
// registry mutation receipt is published.
1645+
yield* Effect.yieldNow;
1646+
const presentationProvider = {
1647+
...initialProvider,
1648+
displayName: "Work",
1649+
accentColor: "#654321",
1650+
} satisfies ServerProvider;
1651+
yield* Ref.set(currentProvider, presentationProvider);
1652+
yield* PubSub.publish(registryChanges, undefined);
1653+
const update = yield* Fiber.join(updates);
1654+
const providers = update._tag === "Some" ? update.value : [];
1655+
assert.strictEqual(providers[0]?.displayName, presentationProvider.displayName);
1656+
assert.strictEqual(providers[0]?.accentColor, presentationProvider.accentColor);
16391657
}).pipe(Effect.provide(runtimeServices));
16401658
}),
16411659
);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -810,9 +810,9 @@ export const layer = Layer.effect(
810810
* - subscribe to each newly-added or rebuilt instance's
811811
* `streamChanges` (so periodic + enrichment refreshes land in
812812
* `providersRef`);
813-
* - read each newly-added/rebuilt instance's current snapshot after
814-
* subscribing, closing the race with its independently-running
815-
* background startup probe;
813+
* - read each instance's current snapshot after subscribing newly-added
814+
* instances, so presentation-only registry updates are reflected even
815+
* when the live instance identity is carried over;
816816
* - prune `providersRef` of instances that no longer exist.
817817
*
818818
* Provider refreshes are owned by each managed provider and never run
@@ -898,8 +898,8 @@ export const layer = Layer.effect(
898898
// subscription race without putting external work on the registry
899899
// or HTTP server construction path.
900900
yield* Effect.forEach(
901-
newlyAdded,
902-
([, instance]) =>
901+
instances,
902+
(instance) =>
903903
Effect.gen(function* () {
904904
const source = buildSnapshotSource(instance);
905905
const provider = yield* source.getSnapshot;

0 commit comments

Comments
 (0)