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

workflowFileRead.ts

96 lines3.6 KB

1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeFS from "node:fs";
3import * as NodeFSP from "node:fs/promises";
4import * as NodePath from "node:path";
5​
6import { OrchestrationWorkflowFileError } from "@t3tools/contracts";
7import * as Effect from "effect/Effect";
8import * as HostProcess from "@t3tools/shared/HostProcess";
9​
10function workflowFilesRoot(home: string, configDir?: string): string {
11 return NodePath.join(configDir?.trim() || NodePath.join(home, ".claude"), "projects");
13​
14export const readContainedWorkflowFile = Effect.fn("orchestration.readContainedWorkflowFile")(
15 function* (input: {
16 readonly path: string;
17 readonly extension: string;
18 readonly byteCap: number;
19 readonly tail?: boolean;
20 readonly configDir?: string;
21 }) {
22 if (!NodePath.isAbsolute(input.path) || NodePath.extname(input.path) !== input.extension) {
23 return yield* new OrchestrationWorkflowFileError({
24 reason: "invalid-path",
25 path: input.path,
26 });
27 }
28​
29 const home = yield* HostProcess.HomeDirectory;
30 const root = yield* Effect.tryPromise({
31 try: () => NodeFSP.realpath(workflowFilesRoot(home, input.configDir)),
32 catch: (cause) =>
33 new OrchestrationWorkflowFileError({
34 reason: "root-unavailable",
35 path: input.path,
36 cause,
37 }),
38 });
39​
40 // Realpath the FILE itself (not just its directory): a symlink named
41 // like a transcript inside a contained directory must not escape.
42 const resolved = yield* Effect.tryPromise({
43 try: () => NodeFSP.realpath(input.path),
44 catch: (cause) =>
45 new OrchestrationWorkflowFileError({ reason: "not-found", path: input.path, cause }),
46 });
47​
48 if (resolved !== root && !resolved.startsWith(`${root}${NodePath.sep}`)) {
49 return yield* new OrchestrationWorkflowFileError({ reason: "outside-root", path: resolved });
50 }
51 if (NodePath.extname(resolved) !== input.extension) {
52 return yield* new OrchestrationWorkflowFileError({
53 reason: "wrong-extension",
54 path: resolved,
55 });
56 }
57​
58 // fd inode vs path inode catches a swapped leaf, not a swapped intermediate
59 // dir; root is 0700 ~/.claude, so that already needs the uid we run as.
60 const read = yield* Effect.tryPromise({
61 try: async () => {
62 const handle = await NodeFSP.open(
63 resolved,
64 NodeFS.constants.O_RDONLY | (NodeFS.constants.O_NONBLOCK ?? 0),
65 );
66 try {
67 const stat = await handle.stat();
68 if (!stat.isFile()) {
69 return { failure: "not-regular-file" as const };
70 }
71 const pathStat = await NodeFSP.lstat(resolved);
72 if (stat.ino !== pathStat.ino || stat.dev !== pathStat.dev) {
73 return { failure: "changed-during-read" as const };
74 }
75 const buffer = Buffer.alloc(Math.min(stat.size, input.byteCap + (input.tail ? 1 : 0)));
76 const truncated = stat.size > buffer.length;
77 const offset = input.tail ? stat.size - buffer.length : 0;
78 const { bytesRead } = await handle.read(buffer, 0, buffer.length, offset);
79 return {
80 contents: buffer.subarray(0, bytesRead).toString("utf8"),
81 truncated,
82 };
83 } finally {
84 await handle.close();
85 }
86 },
87 catch: (cause) =>
88 new OrchestrationWorkflowFileError({ reason: "read-failed", path: resolved, cause }),
89 });
90 if ("failure" in read) {
91 return yield* new OrchestrationWorkflowFileError({ reason: read.failure, path: resolved });
92 }
93​
94 return { path: resolved, contents: read.contents, truncated: read.truncated };
95 },
96);