Open History to see the last commit
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 { tailClaudeTaskOutput } from "./ClaudeTaskOutputTail.ts";
11
12const SESSION_ID = "0c91bb28-9e89-4889-bd22-3de9880233d3";
13const TASK_ID = "bk6xreeye";
14
15// Claude Code's layout: <root>/<project>/<session_id>/tasks/<task_id>.output.
16const makeTaskFile = Effect.gen(function* () {
17 const fileSystem = yield* FileSystem.FileSystem;
18 const path = yield* Path.Path;
19 const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "claude-task-output-" });
20 const tasks = path.join(root, "-private-tmp-project", SESSION_ID, "tasks");
21 return { root, tasks, file: path.join(tasks, `${TASK_ID}.output`) };
22});
23
24// Live clock and a real file: this proves the tail follows the writer, which
25// TestClock cannot interleave with filesystem IO.
26describe("tailClaudeTaskOutput", () => {
27 it.live("follows a file that appears after the task starts, across split characters", () =>
28 Effect.scoped(
29 Effect.gen(function* () {
30 const fileSystem = yield* FileSystem.FileSystem;
31 const { root, tasks, file } = yield* makeTaskFile;
32 const chunks = yield* Queue.unbounded<string>();
33 yield* tailClaudeTaskOutput({
34 root,
35 sessionId: SESSION_ID,
36 taskId: TASK_ID,
37 stop: yield* Deferred.make<void>(),
38 onChunk: (chunk) => Queue.offer(chunks, chunk),
39 }).pipe(Effect.forkScoped);
40
41 yield* fileSystem.makeDirectory(tasks, { recursive: true });
42 // "é" is two bytes; the first write ends between them.
43 const encoded = new TextEncoder().encode("café 1\n");
44 yield* fileSystem.writeFile(file, encoded.subarray(0, 4));
45 assert.equal(yield* Queue.take(chunks), "caf");
46
47 yield* fileSystem.writeFile(file, encoded);
48 assert.equal(yield* Queue.take(chunks), "é 1\n");
49 }),
50 ).pipe(Effect.provide(NodeServices.layer)),
51 );
52
53 it.live("skips ahead when the command writes faster than it is read", () =>
54 Effect.scoped(
55 Effect.gen(function* () {
56 const fileSystem = yield* FileSystem.FileSystem;
57 const { root, tasks, file } = yield* makeTaskFile;
58 yield* fileSystem.makeDirectory(tasks, { recursive: true });
59 yield* fileSystem.writeFileString(file, "start\n");
60 const chunks = yield* Queue.unbounded<string>();
61 yield* tailClaudeTaskOutput({
62 root,
63 sessionId: SESSION_ID,
64 taskId: TASK_ID,
65 stop: yield* Deferred.make<void>(),
66 onChunk: (chunk) => Queue.offer(chunks, chunk),
67 }).pipe(Effect.forkScoped);
68 assert.equal(yield* Queue.take(chunks), "start\n");
69
70 yield* fileSystem.writeFileString(file, `start\n${"y\n".repeat(1_000_000)}end\n`);
71 const flooded = yield* Queue.take(chunks);
72 assert.isAtMost(flooded.length, 256 * 1024 + 1);
73 assert.isTrue(flooded.startsWith("\n"));
74 assert.isTrue(flooded.endsWith("y\nend\n"));
75 }),
76 ).pipe(Effect.provide(NodeServices.layer)),
77 );
78
79 it.live("reads what was written last when the tool result stops it", () =>
80 Effect.scoped(
81 Effect.gen(function* () {
82 const fileSystem = yield* FileSystem.FileSystem;
83 const { root, tasks, file } = yield* makeTaskFile;
84 yield* fileSystem.makeDirectory(tasks, { recursive: true });
85 yield* fileSystem.writeFileString(file, "one\n");
86 const chunks: Array<string> = [];
87 const stop = yield* Deferred.make<void>();
88 const first = yield* Deferred.make<void>();
89 const fiber = yield* tailClaudeTaskOutput({
90 root,
91 sessionId: SESSION_ID,
92 taskId: TASK_ID,
93 stop,
94 onChunk: (chunk) =>
95 Effect.sync(() => chunks.push(chunk)).pipe(
96 Effect.andThen(Deferred.succeed(first, undefined)),
97 ),
98 }).pipe(Effect.forkScoped);
99 yield* Deferred.await(first);
100 // Written right before the command exits, then the result arrives.
101 yield* fileSystem.writeFileString(file, "one\ntwo\n");
102 yield* Deferred.succeed(stop, undefined);
103 yield* Fiber.join(fiber);
104 assert.deepEqual(chunks, ["one\n", "two\n"]);
105 }).pipe(Effect.provide(NodeServices.layer)),
106 ),
107 );
108
109 it.live("ends when stopped even if Claude Code never wrote the file", () =>
110 Effect.scoped(
111 Effect.gen(function* () {
112 const { root } = yield* makeTaskFile;
113 const stop = yield* Deferred.make<void>();
114 const fiber = yield* tailClaudeTaskOutput({
115 root,
116 sessionId: SESSION_ID,
117 taskId: TASK_ID,
118 stop,
119 onChunk: () => Effect.die("no file, no output"),
120 }).pipe(Effect.forkScoped);
121 yield* Deferred.succeed(stop, undefined);
122 yield* Fiber.join(fiber);
123 }).pipe(Effect.provide(NodeServices.layer)),
124 ),
125 );
126});