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