Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CodexTextGeneration.test.ts

758 lines28.6 KB

1import {
2 DEFAULT_TEXT_GENERATION_MODEL_BY_PROVIDER,
3 ProjectId,
4 ProviderDriverKind,
5 type ModelSelection,
6 type ServerProvider,
7} from "@t3tools/contracts";
8import type { ProviderInstance } from "@t3tools/provider-core/server/driver";
9import * as ProviderInstallationGate from "../provider/ProviderInstallationGate.ts";
10import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts";
11import * as ServerSettings from "../serverSettings.ts";
12import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
13import * as NodeServices from "@effect/platform-node/NodeServices";
14import { it } from "@effect/vitest";
15import * as Cause from "effect/Cause";
16import * as Effect from "effect/Effect";
17import * as Exit from "effect/Exit";
18import * as Fiber from "effect/Fiber";
19import * as FileSystem from "effect/FileSystem";
20import * as Layer from "effect/Layer";
21import * as Path from "effect/Path";
22import * as Schema from "effect/Schema";
23import * as Scope from "effect/Scope";
24import { createModelSelection } from "@t3tools/shared/model";
25import { expect } from "vite-plus/test";
26​
27import { CodexSettings, ProviderInstanceId } from "@t3tools/contracts";
28​
29import * as ServerConfig from "../config.ts";
30import { makeProcessProbe, processProbeSource } from "../testUtils/processProbe.ts";
31import * as TextGeneration from "./TextGeneration.ts";
32import * as TextGenerators from "./TextGenerators.ts";
33import { makeCodexTextGeneration } from "./CodexTextGeneration.ts";
34import { writeFakeCli } from "@t3tools/provider-testing/fakeCli";
35const decodeCodexSettings = Schema.decodeSync(CodexSettings);
36​
37const DEFAULT_TEST_MODEL_SELECTION = createModelSelection(
38 ProviderInstanceId.make("codex"),
39 "gpt-5.4-mini",
40);
41​
42const layerCodexTextGenerationTest = ServerConfig.ServerConfig.layerTest(process.cwd(), {
43 prefix: "t3code-codex-text-generation-test-",
44}).pipe(Layer.provideMerge(NodeServices.layer));
45​
46interface FakeCodexInput {
47 output: string;
48 lifecycle?: { port: number; command: "exec" | "mcp list" };
49 exitCode?: number;
50 stderr?: string;
51 requireImage?: boolean;
52 requireServiceTier?: string;
53 requireReasoningEffort?: string;
54 forbidReasoningEffort?: boolean;
55 requireArg?: string;
56 forbidArg?: string;
57 stdinMustContain?: string;
58 stdinMustNotContain?: string;
59 mcpServers?: ReadonlyArray<{ name: string; enabled: boolean }> | number;
60 requiredCwd?: string;
61 requireInstructions?: string;
63​
64function makeFakeCodexBinary(dir: string, input: FakeCodexInput) {
65 const check = JSON.stringify({
66 requireImage: input.requireImage ?? false,
67 requireServiceTier: input.requireServiceTier ?? null,
68 requireReasoningEffort: input.requireReasoningEffort ?? null,
69 forbidReasoningEffort: input.forbidReasoningEffort ?? false,
70 requireArg: input.requireArg ?? null,
71 forbidArg: input.forbidArg ?? null,
72 stdinMustContain: input.stdinMustContain ?? null,
73 stdinMustNotContain: input.stdinMustNotContain ?? null,
74 mcpServers: input.mcpServers ?? [],
75 requiredCwd: input.requiredCwd ?? null,
76 requireInstructions: input.requireInstructions ?? null,
77 stderr: input.stderr ?? null,
78 output: input.output,
79 exitCode: input.exitCode ?? 0,
80 });
81 return Effect.gen(function* () {
82 const path = yield* Path.Path;
83 return writeFakeCli({
84 directory: path.join(dir, "bin"),
85 name: "codex",
86 source: [
87 'import * as NodeFS from "node:fs";',
88 `const check = ${check};`,
89 "const args = process.argv.slice(2);",
90 'const isMcpList = args.includes("mcp") && args.includes("list");',
91 ...(input.lifecycle === undefined
92 ? []
93 : [
94 `if (isMcpList === ${input.lifecycle.command === "mcp list"}) {`,
95 'process.on("SIGTERM", () => {});',
96 processProbeSource(input.lifecycle.port),
97 "await new Promise(() => {});",
98 "}",
99 ]),
100 "if (isMcpList) {",
101 ' if (typeof check.mcpServers === "number") process.exit(check.mcpServers);',
102 " process.stdout.write(JSON.stringify(check.mcpServers));",
103 " process.exit(0);",
104 "}",
105 'const originalArgs = ` ${args.join(" ")} `;',
106 "let outputPath = null;",
107 "let seenImage = false;",
108 'let seenServiceTier = "";',
109 'let seenReasoningEffort = "";',
110 'let instructions = "";',
111 "for (let index = 0; index < args.length; index += 1) {",
112 ' if (args[index] === "--image") {',
113 " index += 1;",
114 " if (args[index]) seenImage = true;",
115 ' } else if (args[index] === "--config") {',
116 " index += 1;",
117 ' const value = args[index] ?? "";',
118 ' if (value.startsWith("developer_instructions=")) instructions = JSON.parse(value.slice("developer_instructions=".length));',
119 ' if (value.startsWith("service_tier=")) seenServiceTier = value;',
120 ' if (value.startsWith("model_reasoning_effort=")) seenReasoningEffort = value;',
121 ' } else if (args[index] === "--output-last-message") {',
122 " index += 1;",
123 " outputPath = args[index] ?? null;",
124 " }",
125 "}",
126 "const chunks = [];",
127 "for await (const chunk of process.stdin) chunks.push(chunk);",
128 'const stdinContent = Buffer.concat(chunks).toString("utf8");',
129 "function fail(message, code) {",
130 ' process.stderr.write(message + "\\n");',
131 " process.exit(code);",
132 "}",
133 'if (check.requiredCwd !== null && process.cwd() !== check.requiredCwd) fail("cannot inspect checkout", 10);',
134 'if (check.requireInstructions !== null && !instructions.includes(check.requireInstructions)) fail("missing writer task", 11);',
135 "if (check.requireArg !== null && !originalArgs.includes(` ${check.requireArg} `)) {",
136 ' fail("missing arg: " + check.requireArg, 8);',
137 "}",
138 "if (check.forbidArg !== null && originalArgs.includes(` ${check.forbidArg} `)) {",
139 ' fail("forbidden arg: " + check.forbidArg, 9);',
140 "}",
141 'if (check.requireImage && !seenImage) fail("missing --image input", 2);',
142 "if (",
143 " check.requireServiceTier !== null &&",
144 ' seenServiceTier !== `service_tier="${check.requireServiceTier}"`',
145 ") {",
146 ' fail("unexpected service tier config: " + seenServiceTier, 5);',
147 "}",
148 "if (",
149 " check.requireReasoningEffort !== null &&",
150 ' seenReasoningEffort !== `model_reasoning_effort="${check.requireReasoningEffort}"`',
151 ") {",
152 ' fail("unexpected reasoning effort config: " + seenReasoningEffort, 6);',
153 "}",
154 "if (check.forbidReasoningEffort && seenReasoningEffort.length > 0) {",
155 ' fail("reasoning effort config should be omitted: " + seenReasoningEffort, 7);',
156 "}",
157 "if (check.stdinMustContain !== null && !stdinContent.includes(check.stdinMustContain)) {",
158 ' fail("stdin missing expected content", 3);',
159 "}",
160 "if (check.stdinMustNotContain !== null && stdinContent.includes(check.stdinMustNotContain)) {",
161 ' fail("stdin contained forbidden content", 4);',
162 "}",
163 'if (check.stderr !== null) process.stderr.write(check.stderr + "\\n");',
164 'if (outputPath !== null) NodeFS.writeFileSync(outputPath, check.output + "\\n");',
165 "process.exitCode = check.exitCode;",
166 "",
167 ].join("\n"),
168 });
169 });
171​
172function withFakeCodexEnv<A, E, R>(
173 input: FakeCodexInput & {
174 launchArgs?: string;
175 environment?: NodeJS.ProcessEnv;
176 models?: ReadonlyArray<string>;
177 defaultModel?: string;
178 customModels?: ReadonlyArray<string>;
179 managedRuntime?: boolean;
180 },
181 effectFn: (textGeneration: TextGeneration.TextGeneration["Service"]) => Effect.Effect<A, E, R>,
182) {
183 return Effect.gen(function* () {
184 const fs = yield* FileSystem.FileSystem;
185 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-codex-text-" });
186 const codexPath = yield* makeFakeCodexBinary(tempDir, input);
187 const config = decodeCodexSettings({
188 binaryPath: codexPath,
189 launchArgs: input.launchArgs,
190 customModels: input.customModels,
191 });
192 const textGeneration = yield* makeCodexTextGeneration(
193 config,
194 input.environment === undefined ? undefined : { ...process.env, ...input.environment },
195 Effect.succeed(
196 (input.models ?? []).map((slug) => ({
197 slug,
198 name: slug,
199 isCustom: false,
200 ...(slug === input.defaultModel ? { isDefault: true } : {}),
201 capabilities: null,
202 })),
203 ),
204 input.managedRuntime
205 ? Effect.succeed({
206 config,
207 environment: input.environment ?? process.env,
208 revision: "test",
209 })
210 : undefined,
211 );
212 return yield* effectFn(textGeneration);
213 }).pipe(Effect.scoped);
215​
216it.layer(layerCodexTextGenerationTest)("CodexTextGeneration", (it) => {
217 it.effect.each(["exec", "mcp list"] as const)(
218 "kills a stalled %s CLI when its request is interrupted",
219 (command) =>
220 Effect.gen(function* () {
221 const probe = yield* makeProcessProbe;
222 yield* withFakeCodexEnv(
223 { output: "", lifecycle: { port: probe.port, command } },
224 (textGeneration) =>
225 Effect.gen(function* () {
226 const fiber = yield* textGeneration
227 .generateThreadTitle({
228 cwd: process.cwd(),
229 message: "Name this thread",
230 modelSelection: DEFAULT_TEST_MODEL_SELECTION,
231 })
232 .pipe(Effect.forkScoped);
233 yield* probe.started;
234​
235 const interrupted = yield* Fiber.interrupt(fiber).pipe(Effect.forkScoped);
236 yield* probe.exited;
237 yield* Fiber.join(interrupted);
238 const exit = yield* Fiber.await(fiber);
239 expect(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)).toBe(true);
240 }),
241 );
242 }),
243 );
244​
245 it.effect.each([
246 {
247 selectedModel: "gpt-5.6-luna",
248 models: ["openai.gpt-5.6-luna"],
249 expected: "openai.gpt-5.6-luna",
250 },
251 {
252 selectedModel: "openai.gpt-5.6-luna",
253 models: ["openai.gpt-5.6-luna"],
254 expected: "openai.gpt-5.6-luna",
255 },
256 {
257 selectedModel: "openai.gpt-5.6-luna",
258 models: ["gpt-6-astra", "gpt-5.6-luna"],
259 defaultModel: "gpt-6-astra",
260 expected: "gpt-5.6-luna",
261 },
262 {
263 selectedModel: "gpt-6-luna",
264 models: ["gpt-5.6-luna", "gpt-6-astra"],
265 defaultModel: "gpt-6-astra",
266 expected: "gpt-6-astra",
267 },
268 { selectedModel: "gpt-6-luna", models: ["gpt-5.6-luna"], expected: "gpt-5.6-luna" },
269 { selectedModel: "gpt-6-luna", models: [], expected: "gpt-6-luna" },
270 ])(
271 "dispatches $expected for $selectedModel from $models",
272 ({ selectedModel, models, defaultModel, expected }) =>
273 withFakeCodexEnv(
274 {
275 output: JSON.stringify({ title: "Live title" }),
276 models,
277 ...(defaultModel ? { defaultModel } : {}),
278 requireArg: `--model ${expected}`,
279 },
280 (textGeneration) =>
281 Effect.gen(function* () {
282 const result = yield* textGeneration.generateThreadTitle({
283 cwd: process.cwd(),
284 message: "Describe this change",
285 modelSelection: createModelSelection(ProviderInstanceId.make("codex"), selectedModel),
286 });
287 expect(result.title).toBe("Live title");
288 }),
289 ),
290 );
291​
292 it.effect("drops the requested model's options when falling back to another model", () =>
293 withFakeCodexEnv(
294 {
295 output: JSON.stringify({ title: "Live title" }),
296 models: ["gpt-6-astra"],
297 requireArg: '--model gpt-6-astra --config model_reasoning_effort="low"',
298 forbidArg: 'service_tier="priority"',
299 },
300 (textGeneration) =>
301 textGeneration.generateThreadTitle({
302 cwd: process.cwd(),
303 message: "Describe this change",
304 modelSelection: createModelSelection(ProviderInstanceId.make("codex"), "gpt-6-luna", [
305 { id: "reasoningEffort", value: "none" },
306 { id: "serviceTier", value: "priority" },
307 ]),
308 }),
309 ),
310 );
311​
312 it.effect("sends a configured custom model the managed snapshot leaves out", () =>
313 withFakeCodexEnv(
314 {
315 output: JSON.stringify({ title: "Live title" }),
316 models: ["gpt-6-astra", "gpt-5.6-luna"],
317 defaultModel: "gpt-6-astra",
318 customModels: ["gpt-5.6-luna-preview"],
319 managedRuntime: true,
320 requireArg: '--model gpt-5.6-luna-preview --config model_reasoning_effort="high"',
321 },
322 (textGeneration) =>
323 textGeneration.generateThreadTitle({
324 cwd: process.cwd(),
325 message: "Describe this change",
326 modelSelection: createModelSelection(
327 ProviderInstanceId.make("codex"),
328 "gpt-5.6-luna-preview",
329 [{ id: "reasoningEffort", value: "high" }],
330 ),
331 }),
332 ),
333 );
334​
335 it.effect("omits a persisted service tier for managed ChatGPT text generation", () =>
336 withFakeCodexEnv(
337 {
338 output: JSON.stringify({ subject: "Update project", body: "" }),
339 managedRuntime: true,
340 forbidArg: 'service_tier="priority"',
341 },
342 (textGeneration) =>
343 textGeneration.generateCommitMessage({
344 cwd: process.cwd(),
345 branch: "feature/chatgpt",
346 stagedSummary: "M README.md",
347 stagedPatch: "diff --git a/README.md b/README.md",
348 modelSelection: createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
349 { id: "serviceTier", value: "priority" },
350 ]),
351 }),
352 ),
353 );
354​
355 it.effect("preserves custom PR instructions while allowing checkout inspection", () =>
356 withFakeCodexEnv(
357 {
358 output: JSON.stringify({ title: "Remove default shapes", body: "Removes default shapes." }),
359 launchArgs: "--config 'developer_instructions=\"Use TEAM: as the PR title prefix\"'",
360 requiredCwd: process.cwd(),
361 requireInstructions: "Use TEAM: as the PR title prefix",
362 forbidArg: "--config features.shell_tool=false",
363 mcpServers: [
364 { name: "augmenter", enabled: true },
365 { name: "my.server", enabled: true },
366 { name: "already-off", enabled: false },
367 ],
368 requireArg:
369 '--config features.plugins=false --config features.apps=false --config mcp_servers={"augmenter"={enabled=false},"my.server"={enabled=false}}',
370 },
371 (textGeneration) =>
372 Effect.gen(function* () {
373 const result = yield* textGeneration.generatePrContent({
374 cwd: process.cwd(),
375 baseBranch: "main",
376 headBranch: "remove-default-shapes",
377 commitSummary: "Remove default shapes",
378 diffSummary: "shapes.ts | 2 --",
379 diffPatch: "-addDefaultShapes();",
380 threadContext: "user: in a few minutes PR will be up then $babysit-pr",
381 modelSelection: DEFAULT_TEST_MODEL_SELECTION,
382 });
383 expect(result.title).toBe("Remove default shapes");
384 }),
385 ),
386 );
387​
388 it.effect("still generates when Codex cannot list its MCP servers", () =>
389 withFakeCodexEnv(
390 {
391 output: JSON.stringify({ branch: "remove-default-shapes" }),
392 mcpServers: 2,
393 requireArg: "--config features.plugins=false --config features.apps=false --ephemeral",
394 forbidArg: "mcp_servers=",
395 },
396 (textGeneration) =>
397 Effect.gen(function* () {
398 const result = yield* textGeneration.generateBranchName({
399 cwd: process.cwd(),
400 message: "Remove default shapes from characters",
401 modelSelection: DEFAULT_TEST_MODEL_SELECTION,
402 });
403 expect(result.branch).toBe("remove-default-shapes");
404 }),
405 ),
406 );
407​
408 it.effect("keeps plugins and apps off over launch args that turn them on", () =>
409 withFakeCodexEnv(
410 {
411 output: JSON.stringify({ title: "Remove default shapes" }),
412 launchArgs: "--config features.plugins=true --config features.apps=true",
413 requireArg:
414 "--config features.plugins=true --config features.apps=true --config features.plugins=false --config features.apps=false --ephemeral",
415 },
416 (textGeneration) =>
417 Effect.gen(function* () {
418 const result = yield* textGeneration.generateThreadTitle({
419 cwd: process.cwd(),
420 message: "Remove default shapes from characters",
421 modelSelection: DEFAULT_TEST_MODEL_SELECTION,
422 });
423 expect(result.title).toBe("Remove default shapes");
424 }),
425 ),
426 );
427​
428 it.effect("passes exec-safe launch args into codex exec", () =>
429 withFakeCodexEnv(
430 {
431 output: JSON.stringify({
432 subject: "Add important change",
433 body: "",
434 }),
435 launchArgs: "--strict-config --oss --local-provider ollama --listen off",
436 requireArg: "--strict-config --oss --local-provider ollama",
437 forbidArg: "--listen",
438 },
439 (textGeneration) =>
440 textGeneration.generateCommitMessage({
441 cwd: process.cwd(),
442 branch: "feature/codex-effect",
443 stagedSummary: "M README.md",
444 stagedPatch: "diff --git a/README.md b/README.md",
445 modelSelection: DEFAULT_TEST_MODEL_SELECTION,
446 }),
447 ),
448 );
449​
450 it.effect("generates branch names even when the ambient scope is already closed", () =>
451 withFakeCodexEnv(
452 {
453 output: JSON.stringify({
454 branch: "feat/background-generation",
455 }),
456 },
457 (textGeneration) =>
458 Effect.gen(function* () {
459 // Background fibers can outlive request scopes; temp files scoped there would be reaped on creation.
460 const closedScope = yield* Scope.make();
461 yield* Scope.close(closedScope, Exit.void);
462​
463 const generated = yield* textGeneration
464 .generateBranchName({
465 cwd: process.cwd(),
466 message: "Please update session handling.",
467 modelSelection: DEFAULT_TEST_MODEL_SELECTION,
468 })
469 .pipe(Effect.provideService(Scope.Scope, closedScope));
470​
471 expect(generated.branch).toBe("feat/background-generation");
472 }),
473 ),
474 );
475​
476 it.effect("returns the refinement signal for an unresolved subject", () =>
477 withFakeCodexEnv(
478 { output: JSON.stringify({ title: "Investigate issue", needsRefinement: true }) },
479 (textGeneration) =>
480 Effect.gen(function* () {
481 expect(
482 yield* textGeneration.generateThreadTitle({
483 cwd: process.cwd(),
484 message: "Fix this",
485 modelSelection: DEFAULT_TEST_MODEL_SELECTION,
486 }),
487 ).toEqual({ title: "Investigate issue", needsRefinement: true });
488 }),
489 ),
490 );
491​
492 it.effect("resolves persisted attachment ids to files for codex image inputs", () =>
493 withFakeCodexEnv(
494 {
495 output: JSON.stringify({
496 branch: "fix/ui-regression",
497 }),
498 requireImage: true,
499 },
500 (textGeneration) =>
501 Effect.gen(function* () {
502 const fs = yield* FileSystem.FileSystem;
503 const path = yield* Path.Path;
504 const { attachmentsDir } = yield* ServerConfig.ServerConfig;
505 const attachmentId = "thread-1-attachment";
506 const imagePath = path.join(attachmentsDir, `${attachmentId}.png`);
507 yield* fs.makeDirectory(attachmentsDir, { recursive: true });
508 yield* fs.writeFile(imagePath, Buffer.from("hello"));
509​
510 const generated = yield* textGeneration
511 .generateBranchName({
512 modelSelection: DEFAULT_TEST_MODEL_SELECTION,
513 cwd: process.cwd(),
514 message: "Fix layout bug from screenshot.",
515 attachments: [
516 {
517 type: "image",
518 id: attachmentId,
519 name: "bug.png",
520 mimeType: "image/png",
521 sizeBytes: 5,
522 },
523 ],
524 })
525 .pipe(
526 Effect.tap(() =>
527 fs.stat(imagePath).pipe(
528 Effect.map((fileInfo) => {
529 expect(fileInfo.type).toBe("File");
530 }),
531 ),
532 ),
533 Effect.ensuring(fs.remove(imagePath).pipe(Effect.ignore)),
534 );
535​
536 expect(generated.branch).toBe("fix/ui-regression");
537 }),
538 ),
539 );
540});
541​
542const PREFLIGHT_PRIMARY_STATES = [
543 "ready",
544 "pending",
545 "exhausted",
546 "not installed",
547 "not installed, fallback disabled for project",
548 "signed out",
549 "signed out, fallback failed",
550] as const;
551​
552it.effect.each(PREFLIGHT_PRIMARY_STATES)("text generation preflight: %s", (primaryState) =>
553 Effect.gen(function* () {
554 const primary = ProviderInstanceId.make("codex");
555 const fallback = ProviderInstanceId.make("claude_work");
556 const projectId = ProjectId.make("project-a");
557 const selection = {
558 instanceId: primary,
559 model: "chat-model",
560 options: [{ id: "effort", value: "high" }],
561 };
562 const calls: ModelSelection[] = [];
563 const instances: ProviderInstance[] = [primary, fallback].map((instanceId) => {
564 const driver = ProviderDriverKind.make(instanceId === primary ? "codex" : "claudeAgent");
565 const snapshot: ServerProvider = {
566 instanceId,
567 driver,
568 enabled: true,
569 installed: true,
570 status: "ready",
571 auth: { status: "authenticated" },
572 version: null,
573 checkedAt: "1970-01-01T00:00:00Z",
574 models: [],
575 slashCommands: [],
576 skills: [],
577 ...(primaryState === "pending" && instanceId === primary
578 ? { installed: false, status: "warning", auth: { status: "unknown" } }
579 : {}),
580 ...(primaryState.startsWith("not installed") && instanceId === primary
581 ? { installed: false, status: "error", auth: { status: "unknown" } }
582 : {}),
583 ...(primaryState.startsWith("signed out") && instanceId === primary
584 ? { status: "error", auth: { status: "unauthenticated" } }
585 : {}),
586 ...(primaryState === "signed out, fallback failed" && instanceId === fallback
587 ? { status: "error", auth: { status: "unknown" } }
588 : {}),
589 ...(primaryState === "exhausted" && instanceId === primary
590 ? {
591 usageLimits: {
592 checkedAt: "1970-01-01T00:00:00Z",
593 windows: [{ id: "session", kind: "session", label: "Session", usedPercent: 100 }],
594 },
595 }
596 : {}),
597 };
598 return {
599 instanceId,
600 driverKind: driver,
601 continuationIdentity: { driverKind: driver, continuationKey: instanceId },
602 displayName: undefined,
603 enabled: true,
604 snapshot: { getSnapshot: Effect.succeed(snapshot) } as ProviderInstance["snapshot"],
605 orchestrationAdapter: {} as ProviderInstance["orchestrationAdapter"],
606 textGeneration: TextGeneration.TextGeneration.of({
607 generateCommitMessage: () => Effect.die("unused"),
608 generatePrContent: () => Effect.die("unused"),
609 generateBranchName: () => Effect.die("unused"),
610 generateReviewGuide: () => Effect.die("unused"),
611 generateTranscriptionPostProcessing: () => Effect.die("unused"),
612 generateThreadTitle: (input) =>
613 Effect.sync(() => {
614 calls.push(input.modelSelection);
615 return { title: "Generated title" };
616 }),
617 }),
618 };
619 });
620 const service = yield* TextGeneration.make.pipe(
621 Effect.provide(
622 Layer.mergeAll(
623 Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({
624 listInstances: Effect.succeed(instances),
625 getInstance: (id) =>
626 Effect.succeed(instances.find((instance) => instance.instanceId === id)),
627 }),
628 ServerSettings.layerTest({
629 providerInstances: {
630 [fallback]: { driver: ProviderDriverKind.make("claudeAgent"), config: {} },
631 },
632 ...(primaryState === "not installed, fallback disabled for project"
633 ? {
634 projectSettingsOverrides: {
635 [projectId]: { providerInstances: { [fallback]: { enabled: false } } },
636 },
637 }
638 : {}),
639 }),
640 Layer.mock(TextGenerators.TextGenerators)({ runner: () => Effect.undefined }),
641 Layer.mock(SourceControlProviderRegistry.SourceControlProviderRegistry)({
642 resolveLink: () => Effect.die("unused"),
643 }),
644 ProviderInstallationGate.layer,
645 ),
646 ),
647 );
648 expect(
649 yield* service.generateThreadTitle({
650 cwd: "/tmp",
651 message: "hello",
652 linkedContext: "",
653 modelSelection: selection,
654 projectId,
655 }),
656 ).toEqual({ title: "Generated title" });
657 expect(calls).toEqual([
658 primaryState === "ready" ||
659 primaryState === "pending" ||
660 primaryState === "not installed, fallback disabled for project" ||
661 primaryState === "signed out, fallback failed"
662 ? selection
663 : {
664 instanceId: fallback,
665 model:
666 DEFAULT_TEXT_GENERATION_MODEL_BY_PROVIDER[ProviderDriverKind.make("claudeAgent")],
667 },
668 ]);
669 }),
670);
671​
672it.effect(
673 "a review guide aimed at a text generator falls back to an agent that can read the checkout",
674 () =>
675 Effect.gen(function* () {
676 const claude = ProviderInstanceId.make("claudeAgent");
677 const driver = ProviderDriverKind.make("claudeAgent");
678 const calls: ModelSelection[] = [];
679 const instance = {
680 instanceId: claude,
681 driverKind: driver,
682 continuationIdentity: { driverKind: driver, continuationKey: claude },
683 displayName: undefined,
684 enabled: true,
685 snapshot: {
686 getSnapshot: Effect.succeed<ServerProvider>({
687 instanceId: claude,
688 driver,
689 enabled: true,
690 installed: true,
691 status: "ready",
692 auth: { status: "authenticated" },
693 version: null,
694 checkedAt: "1970-01-01T00:00:00Z",
695 models: [],
696 slashCommands: [],
697 skills: [],
698 }),
699 } as ProviderInstance["snapshot"],
700 orchestrationAdapter: {} as ProviderInstance["orchestrationAdapter"],
701 textGeneration: TextGeneration.TextGeneration.of({
702 generateCommitMessage: () => Effect.die("unused"),
703 generatePrContent: () => Effect.die("unused"),
704 generateBranchName: () => Effect.die("unused"),
705 generateThreadTitle: () => Effect.die("unused"),
706 generateTranscriptionPostProcessing: () => Effect.die("unused"),
707 generateReviewGuide: (input) =>
708 Effect.sync(() => {
709 calls.push(input.modelSelection);
710 return {} as TextGeneration.ReviewGuideGenerationResult;
711 }),
712 }),
713 } satisfies ProviderInstance;
714 const serviceWith = (instances: ReadonlyArray<ProviderInstance>) =>
715 TextGeneration.make.pipe(
716 Effect.provide(
717 Layer.mergeAll(
718 Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({
719 listInstances: Effect.succeed(instances),
720 getInstance: (id) =>
721 Effect.succeed(instances.find((candidate) => candidate.instanceId === id)),
722 }),
723 ServerSettings.layerTest({}),
724 Layer.mock(TextGenerators.TextGenerators)({
725 runner: (id) =>
726 Effect.succeed(
727 id === "openrouter"
728 ? () => Effect.die("a text generator cannot investigate")
729 : undefined,
730 ),
731 }),
732 Layer.mock(SourceControlProviderRegistry.SourceControlProviderRegistry)({
733 resolveLink: () => Effect.die("unused"),
734 }),
735 ProviderInstallationGate.layer,
736 ),
737 ),
738 );
739 const input = {
740 cwd: "/tmp",
741 number: 1,
742 title: "Change",
743 description: "",
744 baseBranch: "main",
745 baseCommit: "abc",
746 settledReview: "",
747 modelSelection: { instanceId: ProviderInstanceId.make("openrouter"), model: "some/model" },
748 };
749 yield* (yield* serviceWith([instance])).generateReviewGuide(input);
750 expect(calls.map((selection) => selection.instanceId)).toEqual([claude]);
751​
752 // With no agent ready, it says what is missing instead of looking up a provider by that id.
753 const failure = yield* Effect.flip((yield* serviceWith([])).generateReviewGuide(input));
754 expect(failure.detail).toMatch(
755 /^Review guides need a coding agent that can read the checkout/,
756 );
757 }),
758);