@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 409 lines 12.4 KB
Raw
1 import * as Context from "effect/Context";
2 import * as Duration from "effect/Duration";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import * as Option from "effect/Option";
6 import * as PlatformError from "effect/PlatformError";
7 import * as Schema from "effect/Schema";
8 import * as Scope from "effect/Scope";
9 import * as Stream from "effect/Stream";
10 import * as ChildProcess from "effect/process/ChildProcess";
11 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
12 import { resolveSpawnCommand } from "@t3tools/shared/shell";
13 import {
14 collectUint8StreamText,
15 decodeUtf8,
16 type CollectedUint8StreamText,
17 } from "@t3tools/provider-core/server/collectStreamText";
18
19 export interface ProcessRunInput {
20 readonly command: string;
21 readonly args: ReadonlyArray<string>;
22 readonly cwd?: string | undefined;
23 readonly spawnCwd?: string | undefined;
24 readonly timeout?: Duration.Input | undefined;
25 readonly env?: NodeJS.ProcessEnv | undefined;
26 readonly stdin?: string | undefined;
27 /** Receives every stdout chunk, including bytes beyond the buffered output limit. */
28 readonly onStdoutChunk?: ((chunk: Uint8Array) => void) | undefined;
29 /** Runs after successful spawn, before cancellation can interrupt acquisition. */
30 readonly onSpawn?: (() => void) | undefined;
31 readonly maxOutputBytes?: number | undefined;
32 readonly outputMode?: "error" | "truncate" | undefined;
33 readonly truncatedMarker?: string | undefined;
34 /** Timed-out results discard partial stdout/stderr. */
35 readonly timeoutBehavior?: "error" | "timedOutResult" | undefined;
36 }
37
38 export interface ProcessRunOutput {
39 readonly stdout: string;
40 readonly stderr: string;
41 readonly code: ChildProcessSpawner.ExitCode | null;
42 readonly timedOut: boolean;
43 readonly stdoutTruncated: boolean;
44 readonly stderrTruncated: boolean;
45 readonly stdoutInvalidUtf8: boolean;
46 readonly stderrInvalidUtf8: boolean;
47 }
48
49 const ProcessInvocationFields = {
50 command: Schema.String,
51 argumentCount: Schema.Number,
52 cwd: Schema.optional(Schema.String),
53 spawnCwd: Schema.optional(Schema.String),
54 };
55
56 const formatProcessInvocation = (input: {
57 readonly command: string;
58 readonly cwd?: string | undefined;
59 readonly spawnCwd?: string | undefined;
60 }): string => {
61 const executionCwd = input.spawnCwd ?? input.cwd;
62 return executionCwd === undefined
63 ? `'${input.command}'`
64 : `'${input.command}' in '${executionCwd}'`;
65 };
66
67 export class ProcessSpawnError extends Schema.TaggedError<ProcessSpawnError>()(
68 "ProcessSpawnError",
69 {
70 ...ProcessInvocationFields,
71 resolvedCommand: Schema.optional(Schema.String),
72 resolvedArgumentCount: Schema.optional(Schema.Number),
73 shell: Schema.optional(Schema.Boolean),
74 cause: Schema.Defect(),
75 },
76 ) {
77 override get message(): string {
78 return `Failed to spawn process ${formatProcessInvocation(this)}`;
79 }
80 }
81
82 export class ProcessStdinError extends Schema.TaggedError<ProcessStdinError>()(
83 "ProcessStdinError",
84 {
85 ...ProcessInvocationFields,
86 stdinBytes: Schema.Number,
87 cause: Schema.Defect(),
88 },
89 ) {
90 override get message(): string {
91 return `Failed to write stdin for process ${formatProcessInvocation(this)}`;
92 }
93 }
94
95 export class ProcessOutputLimitError extends Schema.TaggedError<ProcessOutputLimitError>()(
96 "ProcessOutputLimitError",
97 {
98 ...ProcessInvocationFields,
99 stream: Schema.Literals(["stdout", "stderr"]),
100 maxBytes: Schema.Number,
101 observedBytes: Schema.Number,
102 },
103 ) {
104 override get message(): string {
105 return `Process ${formatProcessInvocation(this)} ${this.stream} produced ${this.observedBytes} bytes, exceeding the ${this.maxBytes} byte limit`;
106 }
107 }
108
109 export class ProcessReadError extends Schema.TaggedError<ProcessReadError>()("ProcessReadError", {
110 ...ProcessInvocationFields,
111 stream: Schema.Literals(["stdout", "stderr", "exitCode"]),
112 cause: Schema.Defect(),
113 }) {
114 override get message(): string {
115 return `Failed to read ${this.stream} for process ${formatProcessInvocation(this)}`;
116 }
117 }
118
119 export class ProcessTimeoutError extends Schema.TaggedError<ProcessTimeoutError>()(
120 "ProcessTimeoutError",
121 {
122 ...ProcessInvocationFields,
123 timeoutMs: Schema.Number,
124 },
125 ) {
126 override get message(): string {
127 return `Process ${formatProcessInvocation(this)} timed out after ${this.timeoutMs}ms`;
128 }
129 }
130
131 export const ProcessRunError = Schema.Union([
132 ProcessSpawnError,
133 ProcessStdinError,
134 ProcessOutputLimitError,
135 ProcessReadError,
136 ProcessTimeoutError,
137 ]);
138 export type ProcessRunError = typeof ProcessRunError.Type;
139
140 export class ProcessRunner extends Context.Service<
141 ProcessRunner,
142 {
143 readonly run: (input: ProcessRunInput) => Effect.Effect<ProcessRunOutput, ProcessRunError>;
144 }
145 >()("t3/processRunner") {}
146
147 const DEFAULT_TIMEOUT = "60 seconds";
148 // Keep SIGTERM-ignoring helpers from blocking timeouts or graceful shutdown.
149 const FORCE_KILL_AFTER = "1 second";
150 const DEFAULT_MAX_OUTPUT_BYTES = 8 * 1024 * 1024;
151
152 const collectText = Effect.fnUntraced(function* (input: {
153 readonly command: string;
154 readonly args: ReadonlyArray<string>;
155 readonly cwd?: string | undefined;
156 readonly spawnCwd?: string | undefined;
157 readonly streamName: "stdout" | "stderr";
158 readonly stream: Stream.Stream<Uint8Array, PlatformError.PlatformError>;
159 readonly maxOutputBytes: number;
160 readonly outputMode: "error" | "truncate";
161 readonly truncatedMarker: string;
162 }) {
163 const stream = input.stream.pipe(
164 Stream.mapError(
165 (cause) =>
166 new ProcessReadError({
167 command: input.command,
168 argumentCount: input.args.length,
169 cwd: input.cwd,
170 spawnCwd: input.spawnCwd,
171 stream: input.streamName,
172 cause,
173 }),
174 ),
175 );
176
177 if (input.outputMode === "truncate") {
178 return yield* collectUint8StreamText({
179 stream,
180 maxBytes: input.maxOutputBytes,
181 truncatedMarker: input.truncatedMarker,
182 });
183 }
184
185 return yield* stream.pipe(
186 Stream.runFoldEffect<
187 {
188 readonly chunks: Uint8Array<ArrayBufferLike>[];
189 readonly bytes: number;
190 },
191 Uint8Array<ArrayBufferLike>,
192 ProcessOutputLimitError | ProcessReadError,
193 never
194 >(
195 () => ({ chunks: [], bytes: 0 }),
196 (state, chunk) => {
197 const remainingBytes = input.maxOutputBytes - state.bytes;
198 if (chunk.byteLength > remainingBytes) {
199 return Effect.fail(
200 new ProcessOutputLimitError({
201 command: input.command,
202 argumentCount: input.args.length,
203 cwd: input.cwd,
204 spawnCwd: input.spawnCwd,
205 stream: input.streamName,
206 maxBytes: input.maxOutputBytes,
207 observedBytes: state.bytes + chunk.byteLength,
208 }),
209 );
210 }
211
212 state.chunks.push(chunk);
213 return Effect.succeed({
214 chunks: state.chunks,
215 bytes: state.bytes + chunk.byteLength,
216 });
217 },
218 ),
219 Effect.map((state): CollectedUint8StreamText => ({
220 ...decodeUtf8(Buffer.concat(state.chunks, state.bytes)),
221 bytes: state.bytes,
222 truncated: false,
223 })),
224 );
225 });
226
227 function finalizeRunProcess<R>(
228 effect: Effect.Effect<ProcessRunOutput, ProcessRunError, R | Scope.Scope>,
229 input: ProcessRunInput,
230 ): Effect.Effect<ProcessRunOutput, ProcessRunError, Exclude<R, Scope.Scope>> {
231 const timeout = Duration.fromInputUnsafe(input.timeout ?? DEFAULT_TIMEOUT);
232 const timeoutBehavior = input.timeoutBehavior ?? "error";
233
234 return effect.pipe(
235 Effect.scoped,
236 Effect.timeoutOption(timeout),
237 Effect.flatMap((result) => {
238 if (Option.isSome(result)) {
239 return Effect.succeed(result.value);
240 }
241 if (timeoutBehavior === "timedOutResult") {
242 return Effect.succeed({
243 stdout: "",
244 stderr: "",
245 code: null,
246 timedOut: true,
247 stdoutTruncated: false,
248 stderrTruncated: false,
249 stdoutInvalidUtf8: false,
250 stderrInvalidUtf8: false,
251 } satisfies ProcessRunOutput);
252 }
253 return Effect.fail(
254 new ProcessTimeoutError({
255 command: input.command,
256 argumentCount: input.args.length,
257 cwd: input.cwd,
258 spawnCwd: input.spawnCwd,
259 timeoutMs: Duration.toMillis(timeout),
260 }),
261 );
262 }),
263 );
264 }
265
266 const commandName = (command: string) => command.replace(/^.*[\\/]/, "");
267
268 const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* (
269 spawner: ChildProcessSpawner.ChildProcessSpawner["Service"],
270 input: ProcessRunInput,
271 ): Effect.fn.Return<ProcessRunOutput, ProcessRunError, Scope.Scope> {
272 yield* Effect.annotateCurrentSpan("process.command", commandName(input.command));
273 const maxOutputBytes = input.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES;
274 const outputMode = input.outputMode ?? "error";
275 const truncatedMarker = input.truncatedMarker ?? "";
276 const extendEnv = input.env !== undefined;
277 const spawnCommand = yield* resolveSpawnCommand(
278 input.command,
279 input.args,
280 input.env === undefined ? {} : { env: input.env, extendEnv },
281 );
282
283 const toSpawnError = (cause: unknown) =>
284 new ProcessSpawnError({
285 command: input.command,
286 argumentCount: input.args.length,
287 cwd: input.cwd,
288 spawnCwd: input.spawnCwd,
289 resolvedCommand: spawnCommand.command,
290 resolvedArgumentCount: spawnCommand.args.length,
291 shell: spawnCommand.shell,
292 cause,
293 });
294
295 const spawn = spawner
296 .spawn(
297 ChildProcess.make(spawnCommand.command, spawnCommand.args, {
298 ...((input.spawnCwd ?? input.cwd) ? { cwd: input.spawnCwd ?? input.cwd } : {}),
299 ...(input.env !== undefined
300 ? {
301 env: input.env,
302 extendEnv,
303 }
304 : {}),
305 shell: spawnCommand.shell,
306 forceKillAfter: FORCE_KILL_AFTER,
307 }),
308 )
309 .pipe(
310 Effect.mapError(toSpawnError),
311 // Synchronous spawn throws arrive as defects; recover them too so callers see typed spawn failures.
312 Effect.catchDefect((cause) => Effect.fail(toSpawnError(cause))),
313 );
314 const child = yield* input.onSpawn === undefined
315 ? spawn
316 : spawn.pipe(
317 Effect.tap(() => Effect.sync(() => input.onSpawn?.())),
318 Effect.uninterruptible,
319 );
320
321 const stdin = input.stdin;
322 const onStdoutChunk = input.onStdoutChunk;
323 const writeStdin =
324 stdin === undefined
325 ? Effect.void
326 : Stream.run(Stream.encodeText(Stream.make(stdin)), child.stdin).pipe(
327 Effect.mapError(
328 (cause) =>
329 new ProcessStdinError({
330 command: input.command,
331 argumentCount: input.args.length,
332 cwd: input.cwd,
333 spawnCwd: input.spawnCwd,
334 stdinBytes: Buffer.byteLength(stdin),
335 cause,
336 }),
337 ),
338 );
339
340 const [stdout, stderr] = yield* Effect.all(
341 [
342 collectText({
343 command: input.command,
344 args: input.args,
345 cwd: input.cwd,
346 spawnCwd: input.spawnCwd,
347 streamName: "stdout",
348 stream: onStdoutChunk
349 ? child.stdout.pipe(Stream.tap((chunk) => Effect.sync(() => onStdoutChunk(chunk))))
350 : child.stdout,
351 maxOutputBytes,
352 outputMode,
353 truncatedMarker,
354 }),
355 collectText({
356 command: input.command,
357 args: input.args,
358 cwd: input.cwd,
359 spawnCwd: input.spawnCwd,
360 streamName: "stderr",
361 stream: child.stderr,
362 maxOutputBytes,
363 outputMode,
364 truncatedMarker,
365 }),
366 writeStdin,
367 ],
368 { concurrency: "unbounded" },
369 );
370
371 const exitCode = yield* child.exitCode.pipe(
372 Effect.mapError(
373 (cause) =>
374 new ProcessReadError({
375 command: input.command,
376 argumentCount: input.args.length,
377 cwd: input.cwd,
378 spawnCwd: input.spawnCwd,
379 stream: "exitCode",
380 cause,
381 }),
382 ),
383 );
384
385 return {
386 stdout: stdout.text,
387 stderr: stderr.text,
388 code: exitCode,
389 timedOut: false,
390 stdoutTruncated: stdout.truncated,
391 stderrTruncated: stderr.truncated,
392 stdoutInvalidUtf8: stdout.invalidUtf8,
393 stderrInvalidUtf8: stderr.invalidUtf8,
394 } satisfies ProcessRunOutput;
395 });
396
397 /** @public */
398 export const make = Effect.fn("ProcessRunner.make")(function* () {
399 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
400
401 const run: ProcessRunner["Service"]["run"] = (input) =>
402 finalizeRunProcess(runProcessCore(spawner, input), input);
403
404 return ProcessRunner.of({
405 run,
406 });
407 });
408
409 export const layer = Layer.effect(ProcessRunner, make());