Open History to see the last commit
1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Match from "effect/Match";
5import { ChildProcessSpawner } from "effect/unstable/process";
6
7import {
8 type VcsError,
9 VcsProcessExitError,
10 type VcsProcessExitFailureKind,
11 VcsProcessMissingExitCodeError,
12 VcsProcessOutputLimitError,
13 VcsProcessOutputReadError,
14 VcsProcessSpawnError,
15 VcsProcessStdinWriteError,
16 VcsProcessTimeoutError,
17} from "@t3tools/contracts";
18import * as ProcessRunner from "../processRunner.ts";
19
20export interface VcsProcessInput {
21 readonly operation: string;
22 readonly command: string;
23 readonly args: ReadonlyArray<string>;
24 readonly cwd: string;
25 readonly spawnCwd?: string;
26 readonly stdin?: string;
27 readonly env?: NodeJS.ProcessEnv;
28 readonly allowNonZeroExit?: boolean;
29 readonly timeoutMs?: number;
30 readonly maxOutputBytes?: number;
31 readonly appendTruncationMarker?: boolean;
32}
33
34export interface VcsProcessOutput {
35 readonly exitCode: ChildProcessSpawner.ExitCode;
36 readonly stdout: string;
37 readonly stderr: string;
38 readonly stdoutTruncated: boolean;
39 readonly stderrTruncated: boolean;
40 /** Present on real process output; optional so narrow test doubles remain lightweight. */
41 readonly stdoutInvalidUtf8?: boolean;
42 readonly stderrInvalidUtf8?: boolean;
43}
44
45export class VcsProcess extends Context.Service<
46 VcsProcess,
47 {
48 readonly run: (input: VcsProcessInput) => Effect.Effect<VcsProcessOutput, VcsError>;
49 }
50>()("t3/vcs/VcsProcess") {}
51
52const DEFAULT_TIMEOUT_MS = 30_000;
53const DEFAULT_MAX_OUTPUT_BYTES = 1_000_000;
54const OUTPUT_TRUNCATED_MARKER = "\n\n[truncated]";
55
56const classifyNonZeroExit = (command: string, stderr: string): VcsProcessExitFailureKind => {
57 const normalized = stderr.toLowerCase();
58
59 if (
60 normalized.includes("authentication failed") ||
61 normalized.includes("not logged in") ||
62 normalized.includes("gh auth login") ||
63 normalized.includes("glab auth login") ||
64 normalized.includes("az devops login") ||
65 normalized.includes("please run az login") ||
66 normalized.includes("no oauth token") ||
67 normalized.includes("unauthorized")
68 ) {
69 return "authentication";
70 }
71
72 if (
73 (command === "gh" &&
74 (normalized.includes("could not resolve to a pullrequest") ||
75 normalized.includes("repository.pullrequest") ||
76 normalized.includes("no pull requests found for branch") ||
77 normalized.includes("pull request not found"))) ||
78 (command === "glab" &&
79 (normalized.includes("merge request not found") ||
80 normalized.includes("not found") ||
81 normalized.includes("404"))) ||
82 (command === "az" &&
83 normalized.includes("pull request") &&
84 (normalized.includes("not found") || normalized.includes("does not exist")))
85 ) {
86 return "not-found";
87 }
88
89 return "command-failed";
90};
91
92export const make = Effect.gen(function* () {
93 const processRunner = yield* ProcessRunner.ProcessRunner;
94
95 const run = Effect.fn("VcsProcess.run")(function* (input: VcsProcessInput) {
96 const baseError = {
97 operation: input.operation,
98 command: input.command,
99 cwd: input.cwd,
100 argumentCount: input.args.length,
101 };
102
103 const result = yield* processRunner
104 .run({
105 command: input.command,
106 args: input.args,
107 cwd: input.cwd,
108 ...(input.spawnCwd !== undefined ? { spawnCwd: input.spawnCwd } : {}),
109 ...(input.stdin !== undefined ? { stdin: input.stdin } : {}),
110 ...(input.env !== undefined ? { env: input.env } : {}),
111 timeout: input.timeoutMs ?? DEFAULT_TIMEOUT_MS,
112 maxOutputBytes: input.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES,
113 outputMode: "truncate",
114 truncatedMarker: input.appendTruncationMarker ? OUTPUT_TRUNCATED_MARKER : "",
115 timeoutBehavior: "error",
116 })
117 .pipe(
118 Effect.mapError(
119 Match.valueTags({
120 ProcessSpawnError: (error) =>
121 VcsProcessSpawnError.fromProcessSpawnError(baseError, error),
122 ProcessOutputLimitError: (error) =>
123 new VcsProcessOutputLimitError({
124 ...baseError,
125 stream: error.stream,
126 maxBytes: error.maxBytes,
127 observedBytes: error.observedBytes,
128 }),
129 ProcessTimeoutError: (error) =>
130 VcsProcessTimeoutError.fromProcessTimeoutError(baseError, error),
131 ProcessStdinError: (error) =>
132 new VcsProcessStdinWriteError({
133 ...baseError,
134 stdinBytes: error.stdinBytes,
135 cause: error.cause,
136 }),
137 ProcessReadError: (error) =>
138 new VcsProcessOutputReadError({
139 ...baseError,
140 stream: error.stream,
141 cause: error.cause,
142 }),
143 }),
144 ),
145 );
146
147 if (result.code === null) {
148 return yield* new VcsProcessMissingExitCodeError(baseError);
149 }
150
151 if (!input.allowNonZeroExit && result.code !== 0) {
152 return yield* VcsProcessExitError.fromProcessExit(
153 baseError,
154 {
155 exitCode: result.code,
156 stderr: result.stderr,
157 stderrTruncated: result.stderrTruncated,
158 },
159 classifyNonZeroExit(input.command, result.stderr),
160 );
161 }
162
163 return {
164 exitCode: result.code,
165 stdout: result.stdout,
166 stderr: result.stderr,
167 stdoutTruncated: result.stdoutTruncated,
168 stderrTruncated: result.stderrTruncated,
169 stdoutInvalidUtf8: result.stdoutInvalidUtf8 ?? false,
170 stderrInvalidUtf8: result.stderrInvalidUtf8 ?? false,
171 } satisfies VcsProcessOutput;
172 });
173
174 return VcsProcess.of({ run });
175});
176
177export const layer = Layer.effect(VcsProcess, make).pipe(Layer.provide(ProcessRunner.layer));