@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

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