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
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; 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.live("skips ahead when the command writes faster than it is read", () =>
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 yield* fileSystem.writeFileString(file, "start\n");
58 const chunks = yield* Queue.unbounded<string>();
59 yield* tailClaudeTaskOutput({
60 root,
61 sessionId: SESSION_ID,
62 taskId: TASK_ID,
63 stop: yield* Deferred.make<void>(),
64 onChunk: (chunk) => Queue.offer(chunks, chunk),
65 }).pipe(Effect.forkScoped);
66 assert.equal(yield* Queue.take(chunks), "start\n");
67
68 yield* fileSystem.writeFileString(file, `start\n${"y\n".repeat(1_000_000)}end\n`);
69 const flooded = yield* Queue.take(chunks);
70 assert.isAtMost(flooded.length, 256 * 1024 + 1);
71 assert.isTrue(flooded.startsWith("\n"));
72 assert.isTrue(flooded.endsWith("y\nend\n"));
73 }),
74 ).pipe(Effect.provide(NodeServices.layer)),
75 );
76
77 it.live("reads what was written last when the tool result stops it", () =>
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, "one\n");
84 const chunks: Array<string> = [];
85 const stop = yield* Deferred.make<void>();
86 const first = yield* Deferred.make<void>();
87 const fiber = yield* tailClaudeTaskOutput({
88 root,
89 sessionId: SESSION_ID,
90 taskId: TASK_ID,
91 stop,
92 onChunk: (chunk) =>
93 Effect.sync(() => chunks.push(chunk)).pipe(
94 Effect.andThen(Deferred.succeed(first, undefined)),
95 ),
96 }).pipe(Effect.forkScoped);
97 yield* Deferred.await(first);
98 yield* fileSystem.writeFileString(file, "one\ntwo\n");
99 yield* Deferred.succeed(stop, undefined);
100 yield* Fiber.join(fiber);
101 assert.deepEqual(chunks, ["one\n", "two\n"]);
102 }).pipe(Effect.provide(NodeServices.layer)),
103 ),
104 );
105});