Open History to see the last commit
1import * as Context from "effect/Context";
2import * as Duration from "effect/Duration";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Match from "effect/Match";
6import * as Schedule from "effect/Schedule";
7import * as Semaphore from "effect/Semaphore";
8import { ChildProcessSpawner } from "effect/process";
9
10import {
11 type VcsError,
12 type VcsProcessErrorContext,
13 VcsProcessExitError,
14 type VcsProcessExitFailureKind,
15 VcsProcessMissingExitCodeError,
16 VcsProcessOutputLimitError,
17 VcsProcessOutputReadError,
18 VcsProcessSpawnError,
19 VcsProcessStdinWriteError,
20 VcsProcessTimeoutError,
21} from "@t3tools/contracts";
22import * as HostProcess from "@t3tools/shared/HostProcess";
23import type { SourceControlProcessInput } from "@t3tools/source-control-core/server/SourceControlHost";
24
25import { gitCommandDuration, gitCommandsTotal, withMetrics } from "../observability/Metrics.ts";
26import * as ProcessRunner from "../processRunner.ts";
27
28export interface VcsProcessInput {
29 readonly operation: string;
30 readonly command: string;
31 readonly args: ReadonlyArray<string>;
32 readonly cwd: string;
33 readonly spawnCwd?: string;
34 readonly stdin?: string;
35 readonly onStdoutChunk?: (chunk: Uint8Array) => void;
36 readonly env?: NodeJS.ProcessEnv;
37 readonly allowNonZeroExit?: boolean;
38 readonly timeoutMs?: number;
39 readonly maxOutputBytes?: number;
40 readonly outputMode?: ProcessRunner.ProcessRunInput["outputMode"];
41 readonly appendTruncationMarker?: boolean;
42 readonly classifyFailure?: SourceControlProcessInput["classifyFailure"];
43 readonly notFoundDetail?: string;
44 readonly concurrency?: SourceControlProcessInput["concurrency"];
45}
46
47export interface VcsProcessOutput {
48 readonly exitCode: ChildProcessSpawner.ExitCode;
49 readonly stdout: string;
50 readonly stderr: string;
51 readonly stdoutTruncated: boolean;
52 readonly stderrTruncated: boolean;
53 readonly stdoutInvalidUtf8?: boolean;
54 readonly stderrInvalidUtf8?: boolean;
55}
56
57export class VcsProcess extends Context.Service<
58 VcsProcess,
59 {
60 readonly run: (input: VcsProcessInput) => Effect.Effect<VcsProcessOutput, VcsError>;
61 }
62>()("t3/vcs/VcsProcess") {}
63
64const DEFAULT_TIMEOUT_MS = 30_000;
65const DEFAULT_MAX_OUTPUT_BYTES = 1_000_000;
66const OUTPUT_TRUNCATED_MARKER = "\n\n[truncated]";
67const VCS_PROCESS_CONCURRENCY = 8;
68// Forge CLIs can take seconds per call; cap them together so git, checkpoints included, keeps slots.
69const NON_GIT_PROCESS_CONCURRENCY = VCS_PROCESS_CONCURRENCY / 2;
70
71/** Shown to users when a Git command fails because another process holds the index lock. */
72export const GIT_INDEX_LOCKED_DETAIL =
73 "Git's index is locked. Wait for other Git operations to finish, then try again.";
74
75export const CHECKPOINT_CAPTURE_OPERATION = "GitVcsDriver.checkpoints.captureCheckpoint";
76
77// Caller operations are diagnostic strings; only known labels may become metric attributes.
78const gitMetricOperations = new Set([
79 CHECKPOINT_CAPTURE_OPERATION,
80 "GitVcsDriver.checkpoints.deleteCheckpointRefs",
81 "GitVcsDriver.checkpoints.diffCheckpoints",
82 "GitVcsDriver.checkpoints.hasHeadCommit",
83 "GitVcsDriver.checkpoints.listAuthoredPaths",
84 "GitVcsDriver.checkpoints.resolveCheckpointCommit",
85 "GitVcsDriver.checkpoints.resolveGitCommonDir",
86 "GitVcsDriver.checkpoints.resolveHeadCommit",
87 "GitVcsDriver.checkpoints.restoreCheckpoint",
88 "GitVcsDriver.detectRepository",
89 "GitVcsDriver.filterIgnoredPaths",
90 "GitVcsDriver.initRepository",
91 "GitVcsDriver.isInsideWorkTree",
92 "GitVcsDriver.listRemotes",
93 "GitVcsDriver.listWorkspaceFiles",
94 "GitVcsDriver.resolveGitCommonDir",
95]);
96
97/**
98 * Append after inherited entries without changing argv or the user's config.
99 * Windows env keys ignore case: read the last spelling of the count so command overrides win,
100 * then update every spelling because Node may keep any one.
101 * Leave invalid counts untouched so Git reports them.
102 */
103export const gitConfigEnv = (
104 env: NodeJS.ProcessEnv,
105 key: string,
106 value: string,
107 platform: NodeJS.Platform,
108): NodeJS.ProcessEnv => {
109 const countKeys = Object.keys(env).filter((name) =>
110 platform === "win32" ? name.toUpperCase() === "GIT_CONFIG_COUNT" : name === "GIT_CONFIG_COUNT",
111 );
112 const countKey = countKeys.at(-1) ?? "GIT_CONFIG_COUNT";
113 const inherited = env[countKey];
114 if (
115 inherited !== undefined &&
116 inherited !== "" &&
117 // Match Git's strtoul count syntax, including whitespace, signs and non-negative -0.
118 /^[ \t\r\n\v\f]*(?:\+?\d+|-0+)/.exec(inherited)?.[0] !== inherited
119 ) {
120 return {};
121 }
122 const count = inherited === undefined || inherited === "" ? 0 : Number.parseInt(inherited, 10);
123 return {
124 ...Object.fromEntries([...countKeys, countKey].map((name) => [name, String(count + 1)])),
125 [`GIT_CONFIG_KEY_${count}`]: key,
126 [`GIT_CONFIG_VALUE_${count}`]: value,
127 };
128};
129
130/**
131 * A project can contain a fake bare repository whose config runs commands such as `core.fsmonitor`.
132 * Require `--git-dir` or `GIT_DIR` to open bare repositories; Git before 2.38 ignores this setting.
133 */
134export const explicitBareRepositoryEnv = (env: NodeJS.ProcessEnv, platform: NodeJS.Platform) =>
135 gitConfigEnv(env, "safe.bareRepository", "explicit", platform);
136
137const classifyNonZeroExit = (
138 stderr: string,
139 classifyFailure: VcsProcessInput["classifyFailure"],
140): VcsProcessExitFailureKind => {
141 const normalized = stderr.toLowerCase();
142 const commandKind = classifyFailure?.(stderr) ?? null;
143
144 if (
145 commandKind === "authentication" ||
146 normalized.includes("authentication failed") ||
147 normalized.includes("not logged in") ||
148 normalized.includes("no oauth token") ||
149 normalized.includes("unauthorized")
150 ) {
151 return "authentication";
152 }
153
154 if (
155 commandKind === "rate-limited" ||
156 normalized.includes("api rate limit") ||
157 normalized.includes("rate limit exceeded") ||
158 normalized.includes("secondary rate limit") ||
159 normalized.includes("too many requests") ||
160 normalized.includes("http 429")
161 ) {
162 return "rate-limited";
163 }
164
165 return commandKind ?? "command-failed";
166};
167
168// Classify before discarding stderr; keep paths and process output out of errors.
169const isTransientGitExit = (stderr: string) =>
170 /unable to create [^\n]*\.lock['"]?: file exists/i.test(stderr) ||
171 /(?:unable to stat|lstat\(|error: open\()[^\n]+: no such file or directory/i.test(stderr);
172
173// On macOS, Git's fsync uses F_FULLFSYNC; writeout-only still uses fsync(2).
174// Network mounts can reject F_FULLFSYNC. Ref writes omit errno, so retry their
175// bare write error too; explicit unrelated errors remain failures.
176// Linux writeout-only only starts writeback, so it must not use this fallback.
177const FULL_FSYNC_ARG = "core.fsyncMethod=fsync";
178const BATCH_FSYNC_ARG = "core.fsyncMethod=batch";
179const WRITEOUT_ONLY_FSYNC_ARG = "core.fsyncMethod=writeout-only";
180const canRetryFullFsync = (stderr: string) =>
181 /fsync error on [^\r\n]+: operation not supported\r?$/im.test(stderr) ||
182 /cannot update ref [^\r\n]+: couldn't write '[^\r\n]+\.lock'\r?$/im.test(stderr);
183
184export const make = Effect.gen(function* () {
185 const processRunner = yield* ProcessRunner.ProcessRunner;
186 const hostPlatform = yield* HostProcess.Platform;
187 const vcsProcesses = yield* Semaphore.make(VCS_PROCESS_CONCURRENCY);
188 const nonGitProcesses = yield* Semaphore.make(NON_GIT_PROCESS_CONCURRENCY);
189 const keyedProcesses = new Map<string, Semaphore.Semaphore>();
190 const keyedPermit = (concurrency: NonNullable<VcsProcessInput["concurrency"]>) => {
191 let semaphore = keyedProcesses.get(concurrency.key);
192 if (semaphore === undefined) {
193 semaphore = Semaphore.makeUnsafe(concurrency.limit);
194 keyedProcesses.set(concurrency.key, semaphore);
195 }
196 return semaphore.withPermits(1);
197 };
198
199 const spawn = Effect.fn("VcsProcess.spawn")(function* (
200 input: VcsProcessInput,
201 baseError: VcsProcessErrorContext,
202 ) {
203 let failedExit = false;
204 const execution = processRunner
205 .run({
206 command: input.command,
207 args: input.args,
208 cwd: input.cwd,
209 ...(input.spawnCwd !== undefined ? { spawnCwd: input.spawnCwd } : {}),
210 ...(input.stdin !== undefined ? { stdin: input.stdin } : {}),
211 ...(input.onStdoutChunk !== undefined ? { onStdoutChunk: input.onStdoutChunk } : {}),
212 env: {
213 ...input.env,
214 ...explicitBareRepositoryEnv({ ...process.env, ...input.env }, hostPlatform),
215 },
216 timeout: input.timeoutMs ?? DEFAULT_TIMEOUT_MS,
217 maxOutputBytes: input.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES,
218 outputMode: input.outputMode ?? "truncate",
219 truncatedMarker: input.appendTruncationMarker ? OUTPUT_TRUNCATED_MARKER : "",
220 timeoutBehavior: "error",
221 })
222 .pipe(
223 Effect.tap((result) => {
224 failedExit = result.code === null || (!input.allowNonZeroExit && result.code !== 0);
225 return Effect.void;
226 }),
227 Effect.mapError(
228 Match.valueTags({
229 ProcessSpawnError: (error) =>
230 VcsProcessSpawnError.fromProcessSpawnError(baseError, error),
231 ProcessOutputLimitError: (error) =>
232 new VcsProcessOutputLimitError({
233 ...baseError,
234 stream: error.stream,
235 maxBytes: error.maxBytes,
236 observedBytes: error.observedBytes,
237 }),
238 ProcessTimeoutError: (error) =>
239 VcsProcessTimeoutError.fromProcessTimeoutError(baseError, error),
240 ProcessStdinError: (error) =>
241 new VcsProcessStdinWriteError({
242 ...baseError,
243 stdinBytes: error.stdinBytes,
244 cause: error.cause,
245 }),
246 ProcessReadError: (error) =>
247 new VcsProcessOutputReadError({
248 ...baseError,
249 stream: error.stream,
250 cause: error.cause,
251 }),
252 }),
253 ),
254 );
255
256 return yield* input.command === "git"
257 ? execution.pipe(
258 withMetrics({
259 counter: gitCommandsTotal,
260 timer: gitCommandDuration,
261 attributes: {
262 operation: gitMetricOperations.has(input.operation) ? input.operation : "other",
263 },
264 outcomeAttributes: (outcome) => ({
265 outcome: outcome === "success" && failedExit ? "failure" : outcome,
266 }),
267 }),
268 )
269 : execution;
270 });
271
272 const runUnbounded = Effect.fn("VcsProcess.runUnbounded")(function* (input: VcsProcessInput) {
273 const baseError = {
274 operation: input.operation,
275 command: input.command,
276 cwd: input.cwd,
277 argumentCount: input.args.length,
278 };
279
280 let result = yield* spawn(input, baseError);
281
282 if (
283 hostPlatform === "darwin" &&
284 input.operation === CHECKPOINT_CAPTURE_OPERATION &&
285 !input.allowNonZeroExit &&
286 result.code !== null &&
287 result.code !== 0 &&
288 input.command === "git" &&
289 (input.args.includes(FULL_FSYNC_ARG) || input.args.includes(BATCH_FSYNC_ARG)) &&
290 canRetryFullFsync(result.stderr)
291 ) {
292 result = yield* spawn(
293 {
294 ...input,
295 args: input.args.map((arg) =>
296 arg === FULL_FSYNC_ARG || arg === BATCH_FSYNC_ARG ? WRITEOUT_ONLY_FSYNC_ARG : arg,
297 ),
298 },
299 baseError,
300 );
301 }
302
303 if (result.code === null) {
304 return yield* new VcsProcessMissingExitCodeError(baseError);
305 }
306
307 if (!input.allowNonZeroExit && result.code !== 0) {
308 const failureKind = classifyNonZeroExit(result.stderr, input.classifyFailure);
309 const error = VcsProcessExitError.fromProcessExit(
310 baseError,
311 {
312 exitCode: result.code,
313 stderr: result.stderr,
314 stderrTruncated: result.stderrTruncated,
315 },
316 failureKind,
317 {
318 retryable:
319 input.command === "git" &&
320 failureKind === "command-failed" &&
321 isTransientGitExit(result.stderr),
322 ...(input.notFoundDetail === undefined ? {} : { notFoundDetail: input.notFoundDetail }),
323 },
324 );
325 if (
326 input.command === "git" &&
327 /^fatal: unable to create ['"][^\r\n]*[/\\]index\.lock['"]: file exists\.?$/im.test(
328 result.stderr,
329 )
330 ) {
331 return yield* new VcsProcessExitError({
332 ...error,
333 detail: GIT_INDEX_LOCKED_DETAIL,
334 });
335 }
336 return yield* error;
337 }
338
339 return {
340 exitCode: result.code,
341 stdout: result.stdout,
342 stderr: result.stderr,
343 stdoutTruncated: result.stdoutTruncated,
344 stderrTruncated: result.stderrTruncated,
345 stdoutInvalidUtf8: result.stdoutInvalidUtf8 ?? false,
346 stderrInvalidUtf8: result.stderrInvalidUtf8 ?? false,
347 } satisfies VcsProcessOutput;
348 });
349
350 const run = Effect.fn("VcsProcess.run")(function* (input: VcsProcessInput) {
351 const bounded = vcsProcesses.withPermits(1)(runUnbounded(input));
352 if (
353 input.command === "git" &&
354 input.operation === CHECKPOINT_CAPTURE_OPERATION &&
355 input.onStdoutChunk === undefined
356 ) {
357 return yield* bounded.pipe(
358 Effect.tapError((error) =>
359 Effect.logDebug("checkpoint Git command failed", {
360 operation: input.operation,
361 errorTag: error._tag,
362 }),
363 ),
364 Effect.retry({
365 times: 2,
366 while: (error) => error._tag === "VcsProcessExitError" && error.retryable === true,
367 schedule: Schedule.spaced(Duration.millis(75)),
368 }),
369 );
370 }
371 const shared = input.command === "git" ? bounded : nonGitProcesses.withPermits(1)(bounded);
372 return yield* input.concurrency === undefined ? shared : keyedPermit(input.concurrency)(shared);
373 });
374
375 return VcsProcess.of({ run });
376});
377
378export const layer = Layer.effect(VcsProcess, make).pipe(Layer.provide(ProcessRunner.layer));