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

ClaudeTextGeneration.test.ts

471 lines16.7 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { it } from "@effect/vitest";
3import { ClaudeSettings, ProviderInstanceId } from "@t3tools/contracts";
4import * as HostProcess from "@t3tools/shared/HostProcess";
5import { createModelSelection } from "@t3tools/shared/model";
6import * as Effect from "effect/Effect";
7import * as FileSystem from "effect/FileSystem";
8import * as Layer from "effect/Layer";
9import * as Path from "effect/Path";
10import * as Schema from "effect/Schema";
11import { expect } from "vite-plus/test";
12​
13import * as ServerConfig from "../config.ts";
14import { processProbeSource } from "../testUtils/processProbe.ts";
15import {
16 SYNTHETIC_CLAUDE_CAPABLE_MODEL,
17 SYNTHETIC_CLAUDE_COLLIDING_ALIAS,
18 SYNTHETIC_CLAUDE_MODEL_CATALOG,
19 SYNTHETIC_CLAUDE_STANDARD_MODEL,
20} from "../provider/ClaudeModelCatalog.testFixtures.ts";
21import * as TextGeneration from "./TextGeneration.ts";
22import { sanitizeThreadTitle } from "@t3tools/provider-core/server/textGenerationUtils";
23import { makeClaudeTextGeneration } from "./ClaudeTextGeneration.ts";
24import { writeFakeCli } from "@t3tools/provider-testing/fakeCli";
25const decodeClaudeSettings = Schema.decodeSync(ClaudeSettings);
26​
27const layerClaudeTextGenerationTest = ServerConfig.ServerConfig.layerTest(process.cwd(), {
28 prefix: "t3code-claude-text-generation-test-",
29}).pipe(Layer.provideMerge(NodeServices.layer));
30​
31// The stub behaviour lives in Node so the same implementation runs on Windows,
32// where a shebang file is not executable and would fall through to the real
33// Claude CLI on PATH; `writeFakeCli` picks the launcher shape per host.
34function makeFakeClaudeBinary(dir: string, lifecyclePort?: number) {
35 return Effect.gen(function* () {
36 const path = yield* Path.Path;
37 const fs = yield* FileSystem.FileSystem;
38 const platform = yield* HostProcess.Platform;
39 const binDir = path.join(dir, "bin");
40 const fixturePath = yield* path.fromFileUrl(
41 new URL("./testing/ClaudeTextGeneration.fixture.mjs", import.meta.url),
42 );
43 const source = yield* fs.readFileString(fixturePath);
44 writeFakeCli({
45 directory: binDir,
46 name: "claude",
47 platform,
48 source:
49 lifecyclePort === undefined
50 ? source
51 : `process.on("SIGTERM", () => {}); ${processProbeSource(lifecyclePort)}`,
52 });
53 return binDir;
54 });
56​
57function withFakeClaudeEnv<A, E, R>(
58 input: {
59 output: string;
60 rejectsNoSessionPersistence?: boolean;
61 lifecyclePort?: number;
62 exitCode?: number;
63 stderr?: string;
64 argsMustContain?: string;
65 argsMustNotContain?: string;
66 stdinMustContain?: string;
67 configDirMustBe?: string;
68 cwdMustNotBe?: string;
69 claudeConfig?: Partial<ClaudeSettings>;
70 },
71 effectFn: (textGeneration: TextGeneration.TextGeneration["Service"]) => Effect.Effect<A, E, R>,
72) {
73 return Effect.gen(function* () {
74 const fs = yield* FileSystem.FileSystem;
75 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-claude-text-" });
76 const binDir = yield* makeFakeClaudeBinary(tempDir, input.lifecyclePort);
77 const pathDelimiter = (yield* HostProcess.isWindows) ? ";" : ":";
78 const hostEnv = yield* HostProcess.Environment;
79 const fakeClaudeEnv: Record<string, string | undefined> = {
80 T3_FAKE_CLAUDE_OUTPUT: input.output,
81 T3_FAKE_CLAUDE_REJECTS_NO_SESSION_PERSISTENCE: input.rejectsNoSessionPersistence
82 ? "1"
83 : undefined,
84 T3_FAKE_CLAUDE_EXIT_CODE: input.exitCode === undefined ? undefined : String(input.exitCode),
85 T3_FAKE_CLAUDE_STDERR: input.stderr,
86 T3_FAKE_CLAUDE_ARGS_MUST_CONTAIN: input.argsMustContain,
87 T3_FAKE_CLAUDE_ARGS_MUST_NOT_CONTAIN: input.argsMustNotContain,
88 T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN: input.stdinMustContain,
89 T3_FAKE_CLAUDE_CWD_MUST_NOT_BE: input.cwdMustNotBe,
90 T3_FAKE_CLAUDE_CONFIG_DIR_MUST_BE: input.configDirMustBe,
91 };
92 const environment: NodeJS.ProcessEnv = {
93 ...Object.fromEntries(
94 Object.entries(hostEnv).filter(([name]) => !name.startsWith("T3_FAKE_CLAUDE_")),
95 ),
96 ...Object.fromEntries(
97 Object.entries(fakeClaudeEnv).filter(([, value]) => value !== undefined),
98 ),
99 PATH: `${binDir}${pathDelimiter}${hostEnv.PATH ?? ""}`,
100 };
101​
102 const config = decodeClaudeSettings(input.claudeConfig ?? {});
103 const textGeneration = yield* makeClaudeTextGeneration(
104 config,
105 environment,
106 Effect.succeed(SYNTHETIC_CLAUDE_MODEL_CATALOG),
107 );
108 return yield* effectFn(textGeneration);
109 }).pipe(Effect.scoped);
111​
112it.layer(layerClaudeTextGenerationTest)("ClaudeTextGeneration", (it) => {
113 it.effect(
114 "keeps canonical built-in capabilities when a custom model collides with its alias",
115 () =>
116 withFakeClaudeEnv(
117 {
118 output: JSON.stringify({
119 structured_output: {
120 title: "Improve orchestration flow",
121 body: "Body",
122 },
123 }),
124 argsMustContain: `--model ${SYNTHETIC_CLAUDE_CAPABLE_MODEL}[expanded] --effort max --settings {"disableAllHooks":true,"fastMode":true}`,
125 claudeConfig: { customModels: [SYNTHETIC_CLAUDE_COLLIDING_ALIAS] },
126 },
127 (textGeneration) =>
128 Effect.gen(function* () {
129 const generated = yield* textGeneration.generatePrContent({
130 cwd: process.cwd(),
131 baseBranch: "main",
132 headBranch: "feature/claude-effect",
133 commitSummary: "Improve orchestration",
134 diffSummary: "1 file changed",
135 diffPatch: "diff --git a/README.md b/README.md",
136 modelSelection: {
137 ...createModelSelection(
138 ProviderInstanceId.make("claudeAgent"),
139 SYNTHETIC_CLAUDE_CAPABLE_MODEL,
140 [
141 { id: "effort", value: "max" },
142 { id: "fastMode", value: true },
143 ],
144 ),
145 },
146 });
147​
148 expect(generated.title).toBe("Improve orchestration flow");
149 }),
150 ),
151 );
152​
153 it.effect(
154 "generates thread titles outside the project with tools, skills, and hooks disabled",
155 () =>
156 withFakeClaudeEnv(
157 {
158 output: JSON.stringify({
159 structured_output: {
160 title:
161 ' "Reconnect failures after restart because the session state does not recover" ',
162 },
163 }),
164 cwdMustNotBe: process.cwd(),
165 stdinMustContain: "/call-script",
166 },
167 (textGeneration) =>
168 Effect.gen(function* () {
169 const generated = yield* textGeneration.generateThreadTitle({
170 cwd: process.cwd(),
171 message: "/call-script",
172 modelSelection: {
173 instanceId: ProviderInstanceId.make("claudeAgent"),
174 model: SYNTHETIC_CLAUDE_STANDARD_MODEL,
175 },
176 });
177​
178 expect(generated.title).toBe(
179 sanitizeThreadTitle(
180 '"Reconnect failures after restart because the session state does not recover"',
181 ),
182 );
183 }),
184 ),
185 );
186​
187 it.effect("sends thread title instructions to the provider prompt", () =>
188 withFakeClaudeEnv(
189 {
190 output: JSON.stringify({ structured_output: { title: "Reconnect failures" } }),
191 stdinMustContain: "Additional instructions:\nUse two to four words.",
192 },
193 (textGeneration) =>
194 textGeneration.generateThreadTitle({
195 cwd: process.cwd(),
196 message: "Investigate reconnect failures",
197 instructions: "Use two to four words.",
198 modelSelection: {
199 instanceId: ProviderInstanceId.make("claudeAgent"),
200 model: SYNTHETIC_CLAUDE_STANDARD_MODEL,
201 },
202 }),
203 ),
204 );
205​
206 it.effect("runs Claude text generation with the configured CLAUDE_CONFIG_DIR", () =>
207 Effect.gen(function* () {
208 const path = yield* Path.Path;
209 const claudeConfigDir = path.join(process.cwd(), ".claude-work-test");
210 return yield* withFakeClaudeEnv(
211 {
212 output: JSON.stringify({
213 structured_output: {
214 title: "Use Claude home",
215 },
216 }),
217 configDirMustBe: claudeConfigDir,
218 claudeConfig: { homePath: claudeConfigDir },
219 },
220 (textGeneration) =>
221 Effect.gen(function* () {
222 const generated = yield* textGeneration.generateThreadTitle({
223 cwd: process.cwd(),
224 message: "thread title",
225 modelSelection: {
226 instanceId: ProviderInstanceId.make("claudeAgent"),
227 model: SYNTHETIC_CLAUDE_STANDARD_MODEL,
228 },
229 });
230​
231 expect(generated.title).toBe(sanitizeThreadTitle("Use Claude home"));
232 }),
233 );
234 }),
235 );
236​
237 it.effect("passes --settings from the instance launch args to Claude text generation", () =>
238 withFakeClaudeEnv(
239 {
240 output: JSON.stringify({
241 structured_output: {
242 title: "Use instance settings",
243 },
244 }),
245 argsMustContain: `"env":{"ANTHROPIC_VERTEX_PROJECT_ID":"vertex-project-a"}`,
246 claudeConfig: {
247 launchArgs: `--settings '{"env":{"ANTHROPIC_VERTEX_PROJECT_ID":"vertex-project-a"}}'`,
248 },
249 },
250 (textGeneration) =>
251 Effect.gen(function* () {
252 const generated = yield* textGeneration.generateThreadTitle({
253 cwd: process.cwd(),
254 message: "thread title",
255 modelSelection: {
256 instanceId: ProviderInstanceId.make("claudeAgent"),
257 model: SYNTHETIC_CLAUDE_STANDARD_MODEL,
258 },
259 });
260​
261 expect(generated.title).toBe(sanitizeThreadTitle("Use instance settings"));
262 }),
263 ),
264 );
265​
266 it.effect("retries without --no-session-persistence when the Claude CLI rejects it", () =>
267 withFakeClaudeEnv(
268 {
269 output: JSON.stringify({ structured_output: { title: "Reconnect failures" } }),
270 rejectsNoSessionPersistence: true,
271 },
272 (textGeneration) =>
273 Effect.gen(function* () {
274 const generated = yield* textGeneration.generateThreadTitle({
275 cwd: process.cwd(),
276 message: "Investigate reconnect failures",
277 modelSelection: {
278 instanceId: ProviderInstanceId.make("claudeAgent"),
279 model: SYNTHETIC_CLAUDE_STANDARD_MODEL,
280 },
281 });
282​
283 expect(generated.title).toBe("Reconnect failures");
284 }),
285 ),
286 );
287​
288 it.effect.each([
289 { verbose: false, label: "normal" },
290 { verbose: true, label: "verbose" },
291 ])("unwraps a JSON title in $label Claude output", ({ verbose }) => {
292 const result = {
293 type: "result",
294 structured_output: { title: '{"title": "Refresh ev-stg APP ASG instances"}' },
295 };
296 return withFakeClaudeEnv(
297 { output: JSON.stringify(verbose ? [result] : result) },
298 (textGeneration) =>
299 Effect.gen(function* () {
300 const generated = yield* textGeneration.generateThreadTitle({
301 cwd: process.cwd(),
302 message: "Refresh ev-stg APP ASG instances",
303 modelSelection: {
304 instanceId: ProviderInstanceId.make("claudeAgent"),
305 model: SYNTHETIC_CLAUDE_STANDARD_MODEL,
306 },
307 });
308​
309 expect(generated.title).toBe("Refresh ev-stg APP ASG instances");
310 }),
311 );
312 });
313​
314 it.effect.each([
315 { previousTitle: undefined, label: "generating" },
316 { previousTitle: "Old thread title", label: "regenerating" },
317 ])("reads the result from verbose Claude output when $label a title", ({ previousTitle }) =>
318 withFakeClaudeEnv(
319 {
320 output: JSON.stringify([
321 { type: "system", subtype: "init" },
322 { type: "assistant", message: { content: [] } },
323 { type: "user", message: { content: [] } },
324 { type: "rate_limit_event" },
325 {
326 type: "result",
327 subtype: "success",
328 result: '{"title":"Refresh ev-stg APP ASG Instances"}',
329 structured_output: { title: "Refresh ev-stg APP ASG Instances" },
330 },
331 ]),
332 },
333 (textGeneration) =>
334 Effect.gen(function* () {
335 const generated = yield* textGeneration.generateThreadTitle({
336 cwd: process.cwd(),
337 message: "Refresh ev-stg APP ASG instances",
338 previousTitle,
339 modelSelection: {
340 instanceId: ProviderInstanceId.make("claudeAgent"),
341 model: SYNTHETIC_CLAUDE_STANDARD_MODEL,
342 },
343 });
344​
345 expect(generated.title).toBe("Refresh ev-stg APP ASG Instances");
346 }),
347 ),
348 );
349​
350 it.effect.each([
351 ["empty message array", []],
352 ["missing result", [{ type: "assistant", structured_output: { title: "Not a result" } }]],
353 ["invalid title", [{ type: "result", structured_output: { title: 42 } }]],
354 [
355 "final result without structured output",
356 [
357 { type: "result", structured_output: { title: "Earlier result" } },
358 { type: "result", subtype: "error_max_structured_output_retries" },
359 ],
360 ],
361 ] as const)("rejects verbose Claude output with %s", ([, output]) =>
362 withFakeClaudeEnv({ output: JSON.stringify(output) }, (textGeneration) =>
363 Effect.gen(function* () {
364 const error = yield* Effect.flip(
365 textGeneration.generateThreadTitle({
366 cwd: process.cwd(),
367 message: "Name this thread",
368 modelSelection: {
369 instanceId: ProviderInstanceId.make("claudeAgent"),
370 model: SYNTHETIC_CLAUDE_STANDARD_MODEL,
371 },
372 }),
373 );
374​
375 expect(error._tag).toBe("TextGenerationError");
376 }),
377 ),
378 );
379​
380 const apiError = "API Error: 400 Model blocked by guardrail: 4 endpoints excluded";
381 const wrapperNotice = "Using the OpenRouter credential from the global credential file.";
382 const structuredOutputError = "Failed to provide valid structured output after 5 attempts";
383 const structuredOutputFailure = {
384 type: "result",
385 subtype: "error_max_structured_output_retries",
386 is_error: true,
387 errors: [structuredOutputError],
388 };
389 it.effect.each([
390 [
391 "the errors list of a result with no result text",
392 JSON.stringify(structuredOutputFailure),
393 structuredOutputError,
394 ],
395 [
396 "the errors list from verbose output",
397 JSON.stringify([{ type: "system", subtype: "init" }, structuredOutputFailure]),
398 structuredOutputError,
399 ],
400 [
401 "the errors list without CLI telemetry",
402 JSON.stringify({
403 ...structuredOutputFailure,
404 subtype: "error_during_execution",
405 errors: ["[ede_diagnostic] result_type=user last_content_type=n/a", apiError],
406 }),
407 apiError,
408 ],
409 [
410 "the stdout error over a stderr notice",
411 JSON.stringify({ is_error: true, api_error_status: 400, result: apiError }),
412 apiError,
413 ],
414 [
415 "the verbose stdout error over a stderr notice",
416 JSON.stringify([
417 { type: "system", subtype: "init" },
418 { type: "result", is_error: true, result: apiError },
419 { type: "result", is_error: true, result: " " },
420 ]),
421 apiError,
422 ],
423 ["stderr when stdout has no error", "not json", wrapperNotice],
424 [
425 "a stdout error naming --no-session-persistence without retrying",
426 JSON.stringify({
427 is_error: true,
428 result: "error: unknown option '--no-session-persistence'",
429 }),
430 "error: unknown option '--no-session-persistence'",
431 ],
432 ] as const)("reports %s when the Claude CLI fails", ([, output, expected]) =>
433 withFakeClaudeEnv({ output, stderr: wrapperNotice, exitCode: 1 }, (textGeneration) =>
434 Effect.gen(function* () {
435 const error = yield* Effect.flip(
436 textGeneration.generateThreadTitle({
437 cwd: process.cwd(),
438 message: "Name this thread",
439 modelSelection: {
440 instanceId: ProviderInstanceId.make("claudeAgent"),
441 model: SYNTHETIC_CLAUDE_STANDARD_MODEL,
442 },
443 }),
444 );
445​
446 expect(error.detail).toBe(`Claude CLI command failed: ${expected}`);
447 }),
448 ),
449 );
450​
451 it.effect("does not report the raw result JSON when a failed run writes nothing to stderr", () =>
452 withFakeClaudeEnv(
453 { output: JSON.stringify(structuredOutputFailure), exitCode: 1 },
454 (textGeneration) =>
455 Effect.gen(function* () {
456 const error = yield* Effect.flip(
457 textGeneration.generateThreadTitle({
458 cwd: process.cwd(),
459 message: "Name this thread",
460 modelSelection: {
461 instanceId: ProviderInstanceId.make("claudeAgent"),
462 model: SYNTHETIC_CLAUDE_STANDARD_MODEL,
463 },
464 }),
465 );
466​
467 expect(error.detail).toBe(`Claude CLI command failed: ${structuredOutputError}`);
468 }),
469 ),
470 );
471});