@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, 04:45 UTC

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