Open History to see the last commit
1/**
2 * Multi-instance validation slices for `ProviderInstanceRegistryLive`.
3 *
4 * Two axes of the driver/registry refactor are exercised here:
5 *
6 * 1. **Same driver, many instances** — the "multi-instance codex slice"
7 * describe block below configures two independent `codex` instances and
8 * asserts each gets its own closures and identity. This is the
9 * multi-codex capability the refactor exists to unlock.
10 *
11 * 2. **Many drivers, one registry** — the "all drivers slice" describe
12 * block below configures one instance of every shipped driver
13 * (`codex`, `claudeAgent`, `cursor`, `grok`, `opencode`) in a single
14 * `ProviderInstanceConfigMap` and asserts the registry boots them all
15 * without cross-contamination. This proves the driver SPI is uniform
16 * across every provider — any driver plugs into the registry through
17 * the same `ProviderDriver` value contract.
18 *
19 * Every instance in these tests is configured with `enabled: false` so the
20 * provider-status checks short-circuit to pending/disabled snapshots
21 * without trying to spawn real `codex` / `claude` / `agent` / `grok` / `opencode`
22 * binaries. That keeps the assertions focused on registry routing
23 * behaviour rather than the runtime details of each provider.
24 */
25import { describe, expect, it } from "@effect/vitest";
26import * as NodeServices from "@effect/platform-node/NodeServices";
27import {
28 type ClaudeSettings,
29 type CodexSettings,
30 type CursorSettings,
31 type GrokSettings,
32 type OpenCodeSettings,
33 ProviderDriverKind,
34 type ProviderInstanceConfigMap,
35 ProviderInstanceId,
36} from "@t3tools/contracts";
37import * as Effect from "effect/Effect";
38import * as Layer from "effect/Layer";
39import { HttpClient, HttpClientResponse } from "effect/unstable/http";
40
41import { ServerConfig } from "../../config.ts";
42import { ServerSettingsService } from "../../serverSettings.ts";
43import { ClaudeDriver } from "../Drivers/ClaudeDriver.ts";
44import { CodexDriver } from "../Drivers/CodexDriver.ts";
45import { CursorDriver } from "../Drivers/CursorDriver.ts";
46import { GrokDriver } from "../Drivers/GrokDriver.ts";
47import { OpenCodeDriver } from "../Drivers/OpenCodeDriver.ts";
48import { OpenCodeRuntimeLive } from "../opencodeRuntime.ts";
49import { NoOpProviderEventLoggers, ProviderEventLoggers } from "./ProviderEventLoggers.ts";
50import { makeProviderInstanceRegistry } from "./ProviderInstanceRegistryLive.ts";
51
52const TestHttpClientLive = Layer.succeed(
53 HttpClient.HttpClient,
54 HttpClient.make((request) =>
55 Effect.succeed(HttpClientResponse.fromWeb(request, Response.json({ version: "0.0.0" }))),
56 ),
57);
58
59const makeCodexConfig = (overrides: Partial<CodexSettings>): CodexSettings => ({
60 enabled: false,
61 binaryPath: "codex",
62 homePath: "",
63 shadowHomePath: "",
64 customModels: [],
65 ...overrides,
66});
67
68const makeClaudeConfig = (overrides: Partial<ClaudeSettings>): ClaudeSettings => ({
69 enabled: false,
70 binaryPath: "claude",
71 homePath: "",
72 customModels: [],
73 launchArgs: "",
74 ...overrides,
75});
76
77const makeCursorConfig = (overrides: Partial<CursorSettings>): CursorSettings => ({
78 enabled: false,
79 binaryPath: "agent",
80 apiEndpoint: "",
81 customModels: [],
82 ...overrides,
83});
84
85const makeGrokConfig = (overrides: Partial<GrokSettings>): GrokSettings => ({
86 enabled: false,
87 binaryPath: "grok",
88 customModels: [],
89 ...overrides,
90});
91
92const makeOpenCodeConfig = (overrides: Partial<OpenCodeSettings>): OpenCodeSettings => ({
93 enabled: false,
94 binaryPath: "opencode",
95 serverUrl: "",
96 serverPassword: "",
97 customModels: [],
98 ...overrides,
99});
100
101describe("ProviderInstanceRegistryLive — multi-instance codex slice", () => {
102 // `ServerConfig.layerTest` needs `FileSystem` to materialize its scratch
103 // directory. `Layer.merge` just unions requirements, so we have to push
104 // `NodeServices.layer` through `Layer.provideMerge` to satisfy that
105 // dependency while still surfacing NodeServices to the test body (the
106 // codex driver's `create` yields `ChildProcessSpawner` directly).
107 const testLayer = ServerConfig.layerTest(process.cwd(), {
108 prefix: "provider-instance-registry-test",
109 }).pipe(
110 Layer.provideMerge(NodeServices.layer),
111 Layer.provideMerge(ServerSettingsService.layerTest()),
112 Layer.provideMerge(TestHttpClientLive),
113 Layer.provideMerge(Layer.succeed(ProviderEventLoggers, NoOpProviderEventLoggers)),
114 );
115
116 it.live("boots two independent codex instances from a ProviderInstanceConfigMap", () =>
117 Effect.gen(function* () {
118 const personalId = ProviderInstanceId.make("codex_personal");
119 const workId = ProviderInstanceId.make("codex_work");
120 const codexDriverKind = ProviderDriverKind.make("codex");
121
122 const configMap: ProviderInstanceConfigMap = {
123 [personalId]: {
124 driver: codexDriverKind,
125 displayName: "Codex (personal)",
126 enabled: false,
127 config: makeCodexConfig({
128 binaryPath: "/opt/codex-personal/bin/codex",
129 homePath: "/home/julius/.codex_personal",
130 customModels: ["personal-preview"],
131 }),
132 },
133 [workId]: {
134 driver: codexDriverKind,
135 displayName: "Codex (work)",
136 enabled: false,
137 config: makeCodexConfig({
138 binaryPath: "/opt/codex-work/bin/codex",
139 homePath: "/home/julius/.codex",
140 customModels: ["work-preview"],
141 }),
142 },
143 };
144
145 const { registry } = yield* makeProviderInstanceRegistry({
146 drivers: [CodexDriver],
147 configMap,
148 });
149
150 const instances = yield* registry.listInstances;
151 expect(instances.map((instance) => instance.instanceId).toSorted()).toEqual(
152 [personalId, workId].toSorted(),
153 );
154 expect(instances.every((instance) => instance.driverKind === codexDriverKind)).toBe(true);
155 expect(instances.map((instance) => instance.displayName).toSorted()).toEqual(
156 ["Codex (personal)", "Codex (work)"].toSorted(),
157 );
158
159 // Each instance must be retrievable by id and carry its *own* closures.
160 const personal = yield* registry.getInstance(personalId);
161 const work = yield* registry.getInstance(workId);
162 expect(personal).toBeDefined();
163 expect(work).toBeDefined();
164 expect(personal!.adapter).not.toBe(work!.adapter);
165 expect(personal!.textGeneration).not.toBe(work!.textGeneration);
166 expect(personal!.snapshot).not.toBe(work!.snapshot);
167
168 // Snapshots identify themselves by instanceId + driver — this is
169 // what makes per-instance routing distinguishable downstream.
170 const personalSnapshot = yield* personal!.snapshot.getSnapshot;
171 expect(personalSnapshot.instanceId).toBe(personalId);
172 expect(personalSnapshot.driver).toBe(codexDriverKind);
173 expect(personalSnapshot.enabled).toBe(false);
174 expect(personalSnapshot.continuation?.groupKey).toBe(
175 "codex:home:/home/julius/.codex_personal",
176 );
177
178 const workSnapshot = yield* work!.snapshot.getSnapshot;
179 expect(workSnapshot.instanceId).toBe(workId);
180 expect(workSnapshot.driver).toBe(codexDriverKind);
181 expect(workSnapshot.enabled).toBe(false);
182 expect(workSnapshot.continuation?.groupKey).toBe("codex:home:/home/julius/.codex");
183
184 // Nothing goes to the unavailable bucket — both drivers are registered.
185 const unavailable = yield* registry.listUnavailable;
186 expect(unavailable).toEqual([]);
187 }).pipe(Effect.provide(testLayer)),
188 );
189
190 it.live(
191 "shadows instances whose driver is not registered in this build without failing boot",
192 () =>
193 Effect.gen(function* () {
194 const codexId = ProviderInstanceId.make("codex_main");
195 const ghostId = ProviderInstanceId.make("ghost_main");
196
197 const configMap: ProviderInstanceConfigMap = {
198 [codexId]: {
199 driver: ProviderDriverKind.make("codex"),
200 enabled: false,
201 config: makeCodexConfig({}),
202 },
203 [ghostId]: {
204 driver: ProviderDriverKind.make("ghostDriver"),
205 displayName: "A fork-only driver we don't ship",
206 enabled: false,
207 config: { arbitrary: "payload", preserved: true },
208 },
209 };
210
211 const { registry } = yield* makeProviderInstanceRegistry({
212 drivers: [CodexDriver],
213 configMap,
214 });
215
216 const instances = yield* registry.listInstances;
217 expect(instances).toHaveLength(1);
218 expect(instances[0]!.instanceId).toBe(codexId);
219
220 const unavailable = yield* registry.listUnavailable;
221 expect(unavailable).toHaveLength(1);
222 const ghost = unavailable[0]!;
223 expect(ghost.instanceId).toBe(ghostId);
224 expect(ghost.driver).toBe("ghostDriver");
225 expect(ghost.availability).toBe("unavailable");
226 expect(ghost.unavailableReason).toMatch(/ghostDriver/);
227 }).pipe(Effect.provide(testLayer)),
228 );
229});
230
231describe("ProviderInstanceRegistryLive — all drivers slice", () => {
232 // All drivers need `NodeServices` (ChildProcessSpawner + FileSystem +
233 // Path). `OpenCodeDriver.create` additionally yields `OpenCodeRuntime`
234 // at construction time, so we wire `OpenCodeRuntimeLive` into the stack.
235 // `OpenCodeRuntimeLive` bundles its own `NetService.layer` via
236 // `Layer.provide`, so the only external requirement it still exposes is
237 // `ChildProcessSpawner` — resolved here by piping it through
238 // `provideMerge(NodeServices.layer)`.
239 //
240 // The nested `provideMerge`s read bottom-up: `NodeServices.layer`
241 // provides `OpenCodeRuntimeLive`'s deps while keeping its own outputs
242 // surfaced; that merged layer then provides `ServerConfig.layerTest`'s
243 // `FileSystem` dep while keeping everything else surfaced to the test.
244 const infraLayer = OpenCodeRuntimeLive.pipe(Layer.provideMerge(NodeServices.layer));
245 const testLayer = ServerConfig.layerTest(process.cwd(), {
246 prefix: "provider-instance-registry-all-drivers-test",
247 }).pipe(
248 Layer.provideMerge(infraLayer),
249 Layer.provideMerge(ServerSettingsService.layerTest()),
250 Layer.provideMerge(TestHttpClientLive),
251 Layer.provideMerge(Layer.succeed(ProviderEventLoggers, NoOpProviderEventLoggers)),
252 );
253
254 it.live("boots one instance of every shipped driver from a single config map", () =>
255 Effect.gen(function* () {
256 const codexId = ProviderInstanceId.make("codex_default");
257 const claudeId = ProviderInstanceId.make("claude_default");
258 const cursorId = ProviderInstanceId.make("cursor_default");
259 const grokId = ProviderInstanceId.make("grok_default");
260 const openCodeId = ProviderInstanceId.make("opencode_default");
261
262 const codexDriverKind = ProviderDriverKind.make("codex");
263 const claudeDriverKind = ProviderDriverKind.make("claudeAgent");
264 const cursorDriverKind = ProviderDriverKind.make("cursor");
265 const grokDriverKind = ProviderDriverKind.make("grok");
266 const openCodeDriverKind = ProviderDriverKind.make("opencode");
267
268 const configMap: ProviderInstanceConfigMap = {
269 [codexId]: {
270 driver: codexDriverKind,
271 displayName: "Codex",
272 enabled: false,
273 config: makeCodexConfig({ homePath: "/home/julius/.codex" }),
274 },
275 [claudeId]: {
276 driver: claudeDriverKind,
277 displayName: "Claude",
278 enabled: false,
279 config: makeClaudeConfig({
280 homePath: "/home/julius/.claude-work",
281 launchArgs: "--verbose",
282 }),
283 },
284 [cursorId]: {
285 driver: cursorDriverKind,
286 displayName: "Cursor",
287 enabled: false,
288 config: makeCursorConfig({}),
289 },
290 [grokId]: {
291 driver: grokDriverKind,
292 displayName: "Grok",
293 enabled: false,
294 config: makeGrokConfig({}),
295 },
296 [openCodeId]: {
297 driver: openCodeDriverKind,
298 displayName: "OpenCode",
299 enabled: false,
300 config: makeOpenCodeConfig({}),
301 },
302 };
303
304 const { registry } = yield* makeProviderInstanceRegistry({
305 drivers: [CodexDriver, ClaudeDriver, CursorDriver, GrokDriver, OpenCodeDriver],
306 configMap,
307 });
308
309 // Every configured instance must materialize — none downgraded to a
310 // shadow snapshot, because every driver in the map is registered.
311 const unavailable = yield* registry.listUnavailable;
312 expect(unavailable).toEqual([]);
313
314 const instances = yield* registry.listInstances;
315 expect(instances).toHaveLength(5);
316 expect(instances.map((instance) => instance.instanceId).toSorted()).toEqual(
317 [codexId, claudeId, cursorId, grokId, openCodeId].toSorted(),
318 );
319
320 // Instance lookup by id resolves each instance to its own bundle —
321 // this is how rest-of-server routes turn/session calls in the new
322 // model. Each driver's bundle carries its advertised `driverKind`.
323 const codex = yield* registry.getInstance(codexId);
324 const claude = yield* registry.getInstance(claudeId);
325 const cursor = yield* registry.getInstance(cursorId);
326 const grok = yield* registry.getInstance(grokId);
327 const openCode = yield* registry.getInstance(openCodeId);
328 expect(codex?.driverKind).toBe(codexDriverKind);
329 expect(claude?.driverKind).toBe(claudeDriverKind);
330 expect(cursor?.driverKind).toBe(cursorDriverKind);
331 expect(grok?.driverKind).toBe(grokDriverKind);
332 expect(openCode?.driverKind).toBe(openCodeDriverKind);
333 expect(codex?.displayName).toBe("Codex");
334 expect(claude?.displayName).toBe("Claude");
335 expect(cursor?.displayName).toBe("Cursor");
336 expect(grok?.displayName).toBe("Grok");
337 expect(openCode?.displayName).toBe("OpenCode");
338
339 // Every instance owns its own set of closures — no sharing across
340 // drivers. `adapter` / `textGeneration` / `snapshot` are all
341 // distinct references even when two instances happen to share a
342 // trait (e.g. Cursor + others all use a stub-or-real
343 // `textGeneration`; they must still be different object values).
344 const adapters = [
345 codex!.adapter,
346 claude!.adapter,
347 cursor!.adapter,
348 grok!.adapter,
349 openCode!.adapter,
350 ];
351 expect(new Set(adapters).size).toBe(adapters.length);
352 const textGenerations = [
353 codex!.textGeneration,
354 claude!.textGeneration,
355 cursor!.textGeneration,
356 grok!.textGeneration,
357 openCode!.textGeneration,
358 ];
359 expect(new Set(textGenerations).size).toBe(textGenerations.length);
360 const snapshots = [
361 codex!.snapshot,
362 claude!.snapshot,
363 cursor!.snapshot,
364 grok!.snapshot,
365 openCode!.snapshot,
366 ];
367 expect(new Set(snapshots).size).toBe(snapshots.length);
368
369 // Snapshots identify themselves by `instanceId` + `driver` so
370 // downstream aggregation in `ProviderRegistry` can tell instances
371 // apart even when two share a driver. With `enabled: false`, the
372 // check short-circuits and we get a disabled/pending snapshot back
373 // — that's enough signal to validate the stamping wrapper without
374 // spawning real binaries.
375 const codexSnapshot = yield* codex!.snapshot.getSnapshot;
376 expect(codexSnapshot.instanceId).toBe(codexId);
377 expect(codexSnapshot.driver).toBe(codexDriverKind);
378 expect(codexSnapshot.enabled).toBe(false);
379 expect(codexSnapshot.continuation?.groupKey).toBe("codex:home:/home/julius/.codex");
380
381 const claudeSnapshot = yield* claude!.snapshot.getSnapshot;
382 expect(claudeSnapshot.instanceId).toBe(claudeId);
383 expect(claudeSnapshot.driver).toBe(claudeDriverKind);
384 expect(claudeSnapshot.enabled).toBe(false);
385 expect(claudeSnapshot.continuation?.groupKey).toBe("claude:home:/home/julius/.claude-work");
386
387 const cursorSnapshot = yield* cursor!.snapshot.getSnapshot;
388 expect(cursorSnapshot.instanceId).toBe(cursorId);
389 expect(cursorSnapshot.driver).toBe(cursorDriverKind);
390 expect(cursorSnapshot.enabled).toBe(false);
391 expect(cursorSnapshot.continuation?.groupKey).toBe(
392 `${cursorDriverKind}:instance:${cursorId}`,
393 );
394
395 const grokSnapshot = yield* grok!.snapshot.getSnapshot;
396 expect(grokSnapshot.instanceId).toBe(grokId);
397 expect(grokSnapshot.driver).toBe(grokDriverKind);
398 expect(grokSnapshot.enabled).toBe(false);
399 expect(grokSnapshot.continuation?.groupKey).toBe(`${grokDriverKind}:instance:${grokId}`);
400
401 const openCodeSnapshot = yield* openCode!.snapshot.getSnapshot;
402 expect(openCodeSnapshot.instanceId).toBe(openCodeId);
403 expect(openCodeSnapshot.driver).toBe(openCodeDriverKind);
404 expect(openCodeSnapshot.enabled).toBe(false);
405 expect(openCodeSnapshot.continuation?.groupKey).toBe(
406 `${openCodeDriverKind}:instance:${openCodeId}`,
407 );
408 }).pipe(Effect.provide(testLayer)),
409 );
410});