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

WorkflowInspectionService.test.ts

349 lines14.2 KB

1// @effect-diagnostics nodeBuiltinImport:off - test builds fixtures via Node fs/path directly.
2// @effect-diagnostics preferSchemaOverJson:off - fixtures serialize plain JSON journal records.
3import * as NodeFSP from "node:fs/promises";
4import * as NodePath from "node:path";
5​
6import { assert, describe, it } from "@effect/vitest";
7import * as NodeServices from "@effect/platform-node/NodeServices";
8import * as Effect from "effect/Effect";
9import * as FileSystem from "effect/FileSystem";
10​
11import * as WorkflowInspectionService from "./WorkflowInspectionService.ts";
12​
13interface Layout {
14 readonly root: string;
15 readonly transcriptDir: string;
16 readonly scriptsDir: string;
17 readonly scriptPath: string;
19​
20const makeLayout = (fs: FileSystem.FileSystem, root: string) =>
21 Effect.gen(function* () {
22 const sessionDir = NodePath.join(root, "proj", "sess");
23 const transcriptDir = NodePath.join(sessionDir, "subagents", "workflows", "wf_abc");
24 const scriptsDir = NodePath.join(sessionDir, "workflows", "scripts");
25 yield* fs.makeDirectory(transcriptDir, { recursive: true });
26 yield* fs.makeDirectory(scriptsDir, { recursive: true });
27 return {
28 root,
29 transcriptDir,
30 scriptsDir,
31 scriptPath: NodePath.join(scriptsDir, "spec.js"),
32 } satisfies Layout;
33 });
34​
35/** Build a service instance whose projects root is an isolated temp dir. */
36const setup = Effect.gen(function* () {
37 const fs = yield* FileSystem.FileSystem;
38 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-workflow-inspection-" });
39 const layout = yield* makeLayout(fs, root);
40 const service = yield* WorkflowInspectionService.make({ projectsRoot: root });
41 return { fs, service, layout };
42});
43​
44describe("WorkflowInspectionService", () => {
45 describe("readScript", () => {
46 it.effect("reads a contained script and reports it untruncated", () =>
47 Effect.gen(function* () {
48 const { fs, service, layout } = yield* setup;
49 yield* fs.writeFileString(layout.scriptPath, "export const run = () => 1;\n");
50​
51 const result = yield* service.readScript({ scriptPath: layout.scriptPath });
52 assert.equal(result.source, "export const run = () => 1;\n");
53 assert.isFalse(result.truncated);
54 }).pipe(Effect.provide(NodeServices.layer)),
55 );
56​
57 it.effect("clips scripts larger than the cap and marks them truncated", () =>
58 Effect.gen(function* () {
59 const { fs, service, layout } = yield* setup;
60 const big = "a".repeat(512 * 1024 + 128);
61 yield* fs.writeFileString(layout.scriptPath, big);
62​
63 const result = yield* service.readScript({ scriptPath: layout.scriptPath });
64 assert.isTrue(result.truncated);
65 assert.equal(result.source.length, 512 * 1024);
66 }).pipe(Effect.provide(NodeServices.layer)),
67 );
68​
69 it.effect("rejects a relative path as invalid-path", () =>
70 Effect.gen(function* () {
71 const { service } = yield* setup;
72 const error = yield* service
73 .readScript({ scriptPath: "relative/spec.js" })
74 .pipe(Effect.flip);
75 assert.equal(error._tag, "WorkflowInspectionError");
76 assert.equal(error.reason, "invalid-path");
77 }).pipe(Effect.provide(NodeServices.layer)),
78 );
79​
80 it.effect("rejects a path outside the projects root as invalid-path", () =>
81 Effect.gen(function* () {
82 const { fs, service } = yield* setup;
83 const outside = yield* fs.makeTempDirectoryScoped({ prefix: "t3-workflow-outside-" });
84 const outsideScript = NodePath.join(outside, "escape.js");
85 yield* fs.writeFileString(outsideScript, "export const x = 1;");
86​
87 const error = yield* service.readScript({ scriptPath: outsideScript }).pipe(Effect.flip);
88 assert.equal(error._tag, "WorkflowInspectionError");
89 assert.equal(error.reason, "invalid-path");
90 }).pipe(Effect.provide(NodeServices.layer)),
91 );
92​
93 it.effect("rejects a symlink inside the root that escapes it as invalid-path", () =>
94 Effect.gen(function* () {
95 const { fs, service, layout } = yield* setup;
96 const outside = yield* fs.makeTempDirectoryScoped({ prefix: "t3-workflow-outside-" });
97 const outsideScript = NodePath.join(outside, "real.js");
98 yield* fs.writeFileString(outsideScript, "export const x = 1;");
99​
100 const linkPath = NodePath.join(layout.scriptsDir, "link.js");
101 yield* Effect.promise(() => NodeFSP.symlink(outsideScript, linkPath));
102​
103 const error = yield* service.readScript({ scriptPath: linkPath }).pipe(Effect.flip);
104 assert.equal(error._tag, "WorkflowInspectionError");
105 assert.equal(error.reason, "invalid-path");
106 }).pipe(Effect.provide(NodeServices.layer)),
107 );
108​
109 it.effect("rejects a non-script extension as invalid-path", () =>
110 Effect.gen(function* () {
111 const { fs, service, layout } = yield* setup;
112 const badPath = NodePath.join(layout.scriptsDir, "spec.txt");
113 yield* fs.writeFileString(badPath, "not a script");
114​
115 const error = yield* service.readScript({ scriptPath: badPath }).pipe(Effect.flip);
116 assert.equal(error._tag, "WorkflowInspectionError");
117 assert.equal(error.reason, "invalid-path");
118 }).pipe(Effect.provide(NodeServices.layer)),
119 );
120​
121 it.effect("reports a missing script as not-found", () =>
122 Effect.gen(function* () {
123 const { service, layout } = yield* setup;
124 const missing = NodePath.join(layout.scriptsDir, "missing.js");
125 const error = yield* service.readScript({ scriptPath: missing }).pipe(Effect.flip);
126 assert.equal(error._tag, "WorkflowInspectionError");
127 assert.equal(error.reason, "not-found");
128 }).pipe(Effect.provide(NodeServices.layer)),
129 );
130 });
131​
132 describe("readJournal", () => {
133 it.effect("summarizes started and result records with clipping", () =>
134 Effect.gen(function* () {
135 const { fs, service, layout } = yield* setup;
136 const bigResult = "z".repeat(64 * 1024);
137 const lines = [
138 JSON.stringify({ type: "started", key: "k1", agentId: "a1" }),
139 JSON.stringify({
140 type: "result",
141 key: "k1",
142 agentId: "a1",
143 result: { ok: true, value: 42 },
144 }),
145 JSON.stringify({ type: "started", key: "k2", agentId: "a2" }),
146 "this-is-not-json{",
147 JSON.stringify({ type: "result", key: "k3", agentId: "a3", result: bigResult }),
148 ];
149 yield* fs.writeFileString(
150 NodePath.join(layout.transcriptDir, "journal.jsonl"),
151 `${lines.join("\n")}\n`,
152 );
153​
154 const result = yield* service.readJournal({ transcriptDir: layout.transcriptDir });
155 assert.isFalse(result.truncated);
156 assert.deepEqual(
157 result.entries.map((entry) => entry.agentId),
158 ["a1", "a2", "a3"],
159 );
160​
161 const a1 = result.entries[0];
162 assert.isTrue(a1?.hasResult);
163 assert.equal(a1?.resultJson, JSON.stringify({ ok: true, value: 42 }));
164 assert.isUndefined(a1?.resultTruncated);
165​
166 const a2 = result.entries[1];
167 assert.isFalse(a2?.hasResult);
168 assert.isUndefined(a2?.resultJson);
169​
170 const a3 = result.entries[2];
171 assert.isTrue(a3?.hasResult);
172 assert.isTrue(a3?.resultTruncated);
173 assert.equal(a3?.resultJson?.length, 32 * 1024);
174 }).pipe(Effect.provide(NodeServices.layer)),
175 );
176​
177 it.effect("reports a missing journal as not-found", () =>
178 Effect.gen(function* () {
179 const { service, layout } = yield* setup;
180 const error = yield* service
181 .readJournal({ transcriptDir: layout.transcriptDir })
182 .pipe(Effect.flip);
183 assert.equal(error._tag, "WorkflowInspectionError");
184 assert.equal(error.reason, "not-found");
185 }).pipe(Effect.provide(NodeServices.layer)),
186 );
187​
188 it.effect("rejects a journal.jsonl symlink that escapes the root as invalid-path", () =>
189 Effect.gen(function* () {
190 const { fs, service, layout } = yield* setup;
191 const outside = yield* fs.makeTempDirectoryScoped({ prefix: "t3-workflow-outside-" });
192 const secret = NodePath.join(outside, "secret.jsonl");
193 yield* fs.writeFileString(secret, JSON.stringify({ type: "started", agentId: "leak" }));
194​
195 const escapeDir = NodePath.join(layout.transcriptDir, "..", "wf_journal_escape");
196 yield* fs.makeDirectory(escapeDir, { recursive: true });
197 yield* Effect.promise(() =>
198 NodeFSP.symlink(secret, NodePath.join(escapeDir, "journal.jsonl")),
199 );
200​
201 const error = yield* service.readJournal({ transcriptDir: escapeDir }).pipe(Effect.flip);
202 assert.equal(error._tag, "WorkflowInspectionError");
203 assert.equal(error.reason, "invalid-path");
204 }).pipe(Effect.provide(NodeServices.layer)),
205 );
206​
207 it.effect("rejects a transcript dir outside the root as invalid-path", () =>
208 Effect.gen(function* () {
209 const { fs, service } = yield* setup;
210 const outside = yield* fs.makeTempDirectoryScoped({ prefix: "t3-workflow-outside-" });
211 yield* fs.writeFileString(NodePath.join(outside, "journal.jsonl"), "");
212​
213 const error = yield* service.readJournal({ transcriptDir: outside }).pipe(Effect.flip);
214 assert.equal(error._tag, "WorkflowInspectionError");
215 assert.equal(error.reason, "invalid-path");
216 }).pipe(Effect.provide(NodeServices.layer)),
217 );
218 });
219​
220 describe("readAgentTranscript", () => {
221 it.effect("reads the full transcript and reports completion", () =>
222 Effect.gen(function* () {
223 const { fs, service, layout } = yield* setup;
224 yield* fs.writeFileString(
225 NodePath.join(layout.transcriptDir, "agent-a1.jsonl"),
226 "l0\nl1\nl2\n",
227 );
228​
229 const result = yield* service.readAgentTranscript({
230 transcriptDir: layout.transcriptDir,
231 agentId: "a1",
232 });
233 assert.deepEqual(result.lines, ["l0", "l1", "l2"]);
234 assert.equal(result.nextLine, 3);
235 assert.isTrue(result.complete);
236 }).pipe(Effect.provide(NodeServices.layer)),
237 );
238​
239 it.effect("returns the remainder from a mid-file cursor", () =>
240 Effect.gen(function* () {
241 const { fs, service, layout } = yield* setup;
242 yield* fs.writeFileString(
243 NodePath.join(layout.transcriptDir, "agent-a1.jsonl"),
244 "l0\nl1\nl2\n",
245 );
246​
247 const result = yield* service.readAgentTranscript({
248 transcriptDir: layout.transcriptDir,
249 agentId: "a1",
250 afterLine: 1,
251 });
252 assert.deepEqual(result.lines, ["l1", "l2"]);
253 assert.equal(result.nextLine, 3);
254 assert.isTrue(result.complete);
255 }).pipe(Effect.provide(NodeServices.layer)),
256 );
257​
258 it.effect("returns empty and complete when the cursor is past end-of-file", () =>
259 Effect.gen(function* () {
260 const { fs, service, layout } = yield* setup;
261 yield* fs.writeFileString(
262 NodePath.join(layout.transcriptDir, "agent-a1.jsonl"),
263 "l0\nl1\nl2\n",
264 );
265​
266 const result = yield* service.readAgentTranscript({
267 transcriptDir: layout.transcriptDir,
268 agentId: "a1",
269 afterLine: 10,
270 });
271 assert.deepEqual(result.lines, []);
272 assert.equal(result.nextLine, 10);
273 assert.isTrue(result.complete);
274 }).pipe(Effect.provide(NodeServices.layer)),
275 );
276​
277 it.effect("rejects a traversal agent id as invalid-path", () =>
278 Effect.gen(function* () {
279 const { service, layout } = yield* setup;
280 const error = yield* service
281 .readAgentTranscript({ transcriptDir: layout.transcriptDir, agentId: "../journal" })
282 .pipe(Effect.flip);
283 assert.equal(error._tag, "WorkflowInspectionError");
284 assert.equal(error.reason, "invalid-path");
285 }).pipe(Effect.provide(NodeServices.layer)),
286 );
287​
288 it.effect("pages a long transcript at the line cap", () =>
289 Effect.gen(function* () {
290 const { fs, service, layout } = yield* setup;
291 const lines = Array.from({ length: 500 }, (_unused, index) => `line-${index}`);
292 yield* fs.writeFileString(
293 NodePath.join(layout.transcriptDir, "agent-a1.jsonl"),
294 lines.join("\n"),
295 );
296​
297 const first = yield* service.readAgentTranscript({
298 transcriptDir: layout.transcriptDir,
299 agentId: "a1",
300 });
301 assert.equal(first.lines.length, 400);
302 assert.equal(first.nextLine, 400);
303 assert.isFalse(first.complete);
304​
305 const second = yield* service.readAgentTranscript({
306 transcriptDir: layout.transcriptDir,
307 agentId: "a1",
308 afterLine: first.nextLine,
309 });
310 assert.equal(second.lines.length, 100);
311 assert.equal(second.nextLine, 500);
312 assert.isTrue(second.complete);
313 assert.equal(second.lines[99], "line-499");
314 }).pipe(Effect.provide(NodeServices.layer)),
315 );
316​
317 it.effect("rejects an agent transcript symlink that escapes the root as invalid-path", () =>
318 Effect.gen(function* () {
319 const { fs, service, layout } = yield* setup;
320 const outside = yield* fs.makeTempDirectoryScoped({ prefix: "t3-workflow-outside-" });
321 const secret = NodePath.join(outside, "secret.txt");
322 yield* fs.writeFileString(secret, "root:x:0:0::/root:/bin/bash");
323​
324 const escapeDir = NodePath.join(layout.transcriptDir, "..", "wf_transcript_escape");
325 yield* fs.makeDirectory(escapeDir, { recursive: true });
326 yield* Effect.promise(() =>
327 NodeFSP.symlink(secret, NodePath.join(escapeDir, "agent-leak.jsonl")),
328 );
329​
330 const error = yield* service
331 .readAgentTranscript({ transcriptDir: escapeDir, agentId: "leak" })
332 .pipe(Effect.flip);
333 assert.equal(error._tag, "WorkflowInspectionError");
334 assert.equal(error.reason, "invalid-path");
335 }).pipe(Effect.provide(NodeServices.layer)),
336 );
337​
338 it.effect("reports a missing transcript as not-found", () =>
339 Effect.gen(function* () {
340 const { service, layout } = yield* setup;
341 const error = yield* service
342 .readAgentTranscript({ transcriptDir: layout.transcriptDir, agentId: "missing" })
343 .pipe(Effect.flip);
344 assert.equal(error._tag, "WorkflowInspectionError");
345 assert.equal(error.reason, "not-found");
346 }).pipe(Effect.provide(NodeServices.layer)),
347 );
348 });
349});