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

ClaudeTaskOutputTail.test.ts

168 lines6.9 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import * as Deferred from "effect/Deferred";
4import * as Effect from "effect/Effect";
5import * as Fiber from "effect/Fiber";
6import * as FileSystem from "effect/FileSystem";
7import * as Path from "effect/Path";
8import * as Queue from "effect/Queue";
9​
10import { claudeTaskOutputRoot, tailClaudeTaskOutput } from "./ClaudeTaskOutputTail.ts";
11​
12const SESSION_ID = "0c91bb28-9e89-4889-bd22-3de9880233d3";
13const TASK_ID = "bk6xreeye";
14​
15const makeTaskFile = Effect.gen(function* () {
16 const fileSystem = yield* FileSystem.FileSystem;
17 const path = yield* Path.Path;
18 const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "claude-task-output-" });
19 const tasks = path.join(root, "-private-tmp-project", SESSION_ID, "tasks");
20 return { root, tasks, file: path.join(tasks, `${TASK_ID}.output`) };
21});
22​
23// TestClock cannot interleave with real filesystem I/O; tests that wait on a poll use the live clock.
24describe("tailClaudeTaskOutput", () => {
25 it.live("follows a file that appears after the task starts, across split characters", () =>
26 Effect.scoped(
27 Effect.gen(function* () {
28 const fileSystem = yield* FileSystem.FileSystem;
29 const { root, tasks, file } = yield* makeTaskFile;
30 const chunks = yield* Queue.unbounded<string>();
31 yield* tailClaudeTaskOutput({
32 root,
33 sessionId: SESSION_ID,
34 taskId: TASK_ID,
35 stop: yield* Deferred.make<void>(),
36 onChunk: (chunk) => Queue.offer(chunks, chunk),
37 }).pipe(Effect.forkScoped);
38​
39 yield* fileSystem.makeDirectory(tasks, { recursive: true });
40 // Split the two-byte é across writes.
41 const encoded = new TextEncoder().encode("café 1\n");
42 yield* fileSystem.writeFile(file, encoded.subarray(0, 4));
43 assert.equal(yield* Queue.take(chunks), "caf");
44​
45 yield* fileSystem.writeFile(file, encoded);
46 assert.equal(yield* Queue.take(chunks), "é 1\n");
47 }),
48 ).pipe(Effect.provide(NodeServices.layer)),
49 );
50​
51 it.effect("drops a split character when the file is truncated and rewritten", () =>
52 Effect.scoped(
53 Effect.gen(function* () {
54 const fileSystem = yield* FileSystem.FileSystem;
55 const { root, tasks, file } = yield* makeTaskFile;
56 yield* fileSystem.makeDirectory(tasks, { recursive: true });
57 // End the first read inside the two-byte é.
58 yield* fileSystem.writeFile(file, new TextEncoder().encode("café").subarray(0, 4));
59 const chunks = yield* Queue.unbounded<string>();
60 const stop = yield* Deferred.make<void>();
61 yield* tailClaudeTaskOutput({
62 root,
63 sessionId: SESSION_ID,
64 taskId: TASK_ID,
65 stop,
66 onChunk: (chunk) => Queue.offer(chunks, chunk),
67 }).pipe(Effect.forkScoped);
68 assert.equal(yield* Queue.take(chunks), "caf");
69​
70 yield* fileSystem.writeFileString(file, "ok\n");
71 yield* Deferred.succeed(stop, undefined);
72 assert.equal(yield* Queue.take(chunks), "ok\n");
73 }),
74 ).pipe(Effect.provide(NodeServices.layer)),
75 );
76​
77 it.live("skips ahead when the command writes faster than it is read", () =>
78 Effect.scoped(
79 Effect.gen(function* () {
80 const fileSystem = yield* FileSystem.FileSystem;
81 const { root, tasks, file } = yield* makeTaskFile;
82 yield* fileSystem.makeDirectory(tasks, { recursive: true });
83 yield* fileSystem.writeFileString(file, "start\n");
84 const chunks = yield* Queue.unbounded<string>();
85 yield* tailClaudeTaskOutput({
86 root,
87 sessionId: SESSION_ID,
88 taskId: TASK_ID,
89 stop: yield* Deferred.make<void>(),
90 onChunk: (chunk) => Queue.offer(chunks, chunk),
91 }).pipe(Effect.forkScoped);
92 assert.equal(yield* Queue.take(chunks), "start\n");
93​
94 // The skip lands on the second byte of a two-byte é.
95 yield* fileSystem.writeFileString(file, `start\n${"é\n".repeat(1_000_000)}end.\n`);
96 const flooded = yield* Queue.take(chunks);
97 assert.isAtMost(flooded.length, 256 * 1024 + 1);
98 assert.equal(flooded.slice(0, 3), "\n\né");
99 assert.isTrue(flooded.endsWith("é\nend.\n"));
100 }),
101 ).pipe(Effect.provide(NodeServices.layer)),
102 );
103​
104 it.live("reads what was written last when the tool result stops it", () =>
105 Effect.scoped(
106 Effect.gen(function* () {
107 const fileSystem = yield* FileSystem.FileSystem;
108 const { root, tasks, file } = yield* makeTaskFile;
109 yield* fileSystem.makeDirectory(tasks, { recursive: true });
110 yield* fileSystem.writeFileString(file, "one\n");
111 const chunks: Array<string> = [];
112 const stop = yield* Deferred.make<void>();
113 const first = yield* Deferred.make<void>();
114 const fiber = yield* tailClaudeTaskOutput({
115 root,
116 sessionId: SESSION_ID,
117 taskId: TASK_ID,
118 stop,
119 onChunk: (chunk) =>
120 Effect.sync(() => chunks.push(chunk)).pipe(
121 Effect.andThen(Deferred.succeed(first, undefined)),
122 ),
123 }).pipe(Effect.forkScoped);
124 yield* Deferred.await(first);
125 yield* fileSystem.writeFileString(file, "one\ntwo\n");
126 yield* Deferred.succeed(stop, undefined);
127 yield* Fiber.join(fiber);
128 assert.deepEqual(chunks, ["one\n", "two\n"]);
129 }).pipe(Effect.provide(NodeServices.layer)),
130 ),
131 );
132});
133​
134describe("claudeTaskOutputRoot", () => {
135 it.effect.skipIf(process.getuid === undefined)(
136 "uses the temp dir Claude Code resolves from its environment on each platform",
137 () =>
138 Effect.gen(function* () {
139 const path = yield* Path.Path;
140 const user = `claude-${process.getuid?.()}`;
141 const root = (
142 environment: Record<string, string>,
143 platform: NodeJS.Platform,
144 cwd: string | null = null,
145 ) => claudeTaskOutputRoot(environment, path, cwd, platform);
146 assert.equal(root({ TMPDIR: "/var/folders/x/T/" }, "linux"), `/var/folders/x/T/${user}`);
147 assert.equal(root({ TMP: "/tmp/user/1000" }, "linux"), `/tmp/user/1000/${user}`);
148 assert.equal(root({}, "linux"), `/tmp/${user}`);
149 assert.equal(root({ TMPDIR: "/var/folders/x/T/" }, "darwin"), `/tmp/${user}`);
150 assert.equal(
151 root({ CLAUDE_CODE_TMPDIR: "/srv/claude", TMPDIR: "/var/folders/x/T/" }, "linux"),
152 `/srv/claude/${user}`,
153 );
154 assert.equal(
155 root({ CLAUDE_CODE_TMPDIR: "/srv/claude", TMPDIR: "/var/folders/x/T/" }, "darwin"),
156 `/srv/claude/${user}`,
157 );
158 assert.equal(
159 root({ TMPDIR: "tmp" }, "linux", "/work/project"),
160 `/work/project/tmp/${user}`,
161 );
162 assert.equal(
163 root({ CLAUDE_CODE_TMPDIR: "../claude" }, "darwin", "/work/project"),
164 `/work/claude/${user}`,
165 );
166 }).pipe(Effect.provide(NodeServices.layer)),
167 );
168});