Skip to content
TypeScript

fix(server): meter checkpoint git subprocess attempts (#24218)

Julius Marmingee340aaeeb98b
diff --git a/apps/server/src/vcs/GitVcsDriver.test.ts b/apps/server/src/vcs/GitVcsDriver.test.ts
index 3a36389ffb7..e1051895113 100644
--- a/apps/server/src/vcs/GitVcsDriver.test.ts
+++ b/apps/server/src/vcs/GitVcsDriver.test.ts
@@ -12,12 +12,15 @@ import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as PlatformError from "effect/PlatformError";
+import { ChildProcessSpawner } from "effect/process";
+import * as Metric from "effect/Metric";
import { assert, it } from "@effect/vitest";
import { CheckpointRef, GitCommandError, VcsProcessExitError } from "@t3tools/contracts";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
import * as ServerConfig from "../config.ts";
+import { gitCommandDuration, gitCommandsTotal } from "../observability/Metrics.ts";
import * as CheckpointStore from "../checkpointing/CheckpointStore.ts";
import * as ProcessRunner from "../processRunner.ts";
import * as GitVcsDriver from "./GitVcsDriver.ts";
@@ -1017,3 +1020,224 @@ it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
assert.strictEqual(detectionError._tag, "VcsProcessSpawnError");
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
+
+const gitCommandCount = (operation: string, outcome: string) =>
+ Metric.value(
+ Metric.withAttributes(gitCommandsTotal, [
+ ["operation", operation],
+ ["outcome", outcome],
+ ]),
+ ).pipe(Effect.map((state) => state.count));
+
+const gitCommandTiming = (operation: string) =>
+ Metric.value(Metric.withAttributes(gitCommandDuration, [["operation", operation]]));
+
+// Keep VcsProcess's exit classification; replace only the subprocess and its clock.
+const makeMeteredDriver = Effect.fn("test.makeMeteredDriver")(function* (exitCode: number) {
+ const process = yield* VcsProcess.make.pipe(
+ Effect.provideService(ProcessRunner.ProcessRunner, {
+ run: () =>
+ TestClock.adjust("250 millis").pipe(
+ Effect.as({
+ code: ChildProcessSpawner.ExitCode(exitCode),
+ stdout: "git output",
+ stderr: exitCode === 0 ? "" : "fatal: test failure",
+ timedOut: false,
+ stdoutTruncated: false,
+ stderrTruncated: false,
+ stdoutInvalidUtf8: false,
+ stderrInvalidUtf8: false,
+ }),
+ ),
+ }),
+ );
+ return yield* GitVcsDriver.makeVcsDriverShape().pipe(
+ Effect.provideService(VcsProcess.VcsProcess, process),
+ );
+});
+
+it.effect("GitVcsDriver meters each git command once under its operation", () =>
+ Effect.gen(function* () {
+ const operation = "GitVcsDriver.checkpoints.resolveHeadCommit";
+ const driver = yield* makeMeteredDriver(0);
+
+ const result = yield* driver.execute({ operation, cwd: "/repo", args: ["rev-parse", "HEAD"] });
+ yield* driver.execute({ operation, cwd: "/another-repo", args: ["rev-parse", "other-ref"] });
+
+ assert.strictEqual(result.stdout, "git output");
+ assert.strictEqual(yield* gitCommandCount(operation, "success"), 2);
+ assert.strictEqual(yield* gitCommandCount(operation, "failure"), 0);
+ const timing = yield* gitCommandTiming(operation);
+ assert.strictEqual(timing.count, 2);
+ assert.strictEqual(timing.sum, 500);
+ }).pipe(
+ Effect.provide(NodeServices.layer),
+ Effect.provideService(Metric.MetricRegistry, new Map()),
+ ),
+);
+
+it.effect("GitVcsDriver counts an allowed non-zero exit as a success", () =>
+ Effect.gen(function* () {
+ const operation = "GitVcsDriver.checkpoints.hasHeadCommit";
+ const driver = yield* makeMeteredDriver(1);
+
+ const result = yield* driver.execute({
+ operation,
+ cwd: "/repo",
+ args: ["rev-parse", "--verify", "HEAD"],
+ allowNonZeroExit: true,
+ });
+
+ assert.strictEqual(result.exitCode, 1);
+ assert.strictEqual(yield* gitCommandCount(operation, "success"), 1);
+ assert.strictEqual(yield* gitCommandCount(operation, "failure"), 0);
+ const timing = yield* gitCommandTiming(operation);
+ assert.strictEqual(timing.count, 1);
+ assert.strictEqual(timing.sum, 250);
+ }).pipe(
+ Effect.provide(NodeServices.layer),
+ Effect.provideService(Metric.MetricRegistry, new Map()),
+ ),
+);
+
+it.effect("GitVcsDriver counts a failed git command as a failure", () =>
+ Effect.gen(function* () {
+ const operation = "GitVcsDriver.checkpoints.resolveHeadCommit";
+ const driver = yield* makeMeteredDriver(128);
+
+ const error = yield* driver
+ .execute({ operation, cwd: "/repo", args: ["rev-parse", "HEAD"] })
+ .pipe(Effect.flip);
+
+ assert.strictEqual(error._tag, "VcsProcessExitError");
+ assert.strictEqual(yield* gitCommandCount(operation, "failure"), 1);
+ assert.strictEqual(yield* gitCommandCount(operation, "success"), 0);
+ const timing = yield* gitCommandTiming(operation);
+ assert.strictEqual(timing.count, 1);
+ assert.strictEqual(timing.sum, 250);
+ }).pipe(
+ Effect.provide(NodeServices.layer),
+ Effect.provideService(Metric.MetricRegistry, new Map()),
+ ),
+);
+
+it.effect("checkpoint capture meters every git process it starts, including streamed ones", () =>
+ Effect.gen(function* () {
+ const fileSystem = yield* FileSystem.FileSystem;
+ const liveProcess = yield* VcsProcess.VcsProcess;
+ const operation = VcsProcess.CHECKPOINT_CAPTURE_OPERATION;
+ let started = 0;
+ let streamed = 0;
+ const driver = yield* GitVcsDriver.makeVcsDriverShape();
+ const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
+ Effect.provideService(VcsProcess.VcsProcess, {
+ run: (input) => {
+ if (input.operation === operation) {
+ started++;
+ if (input.onStdoutChunk !== undefined) streamed++;
+ }
+ return liveProcess.run(input);
+ },
+ }),
+ );
+ const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-metrics-" });
+ const { checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
+
+ yield* captureDriver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
+
+ assert.isAbove(streamed, 0);
+ assert.strictEqual(yield* gitCommandCount(operation, "success"), started);
+ assert.strictEqual(yield* gitCommandCount(operation, "failure"), 0);
+ assert.strictEqual((yield* gitCommandTiming(operation)).count, started);
+ }).pipe(
+ Effect.scoped,
+ Effect.provide(layerGitContract),
+ Effect.provideService(Metric.MetricRegistry, new Map()),
+ ),
+);
+
+it.effect("git metrics finalize cancellation without exposing arbitrary operation labels", () =>
+ Effect.gen(function* () {
+ const started = yield* Deferred.make<void>();
+ const process = yield* VcsProcess.make.pipe(
+ Effect.provideService(ProcessRunner.ProcessRunner, {
+ run: () => Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never)),
+ }),
+ );
+ const driver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
+ Effect.provideService(VcsProcess.VcsProcess, process),
+ );
+ const operation = "arbitrary operation /private/path";
+ const fiber = yield* driver
+ .execute({ operation, cwd: "/repo", args: ["rev-parse", "HEAD"] })
+ .pipe(Effect.forkChild);
+ yield* Deferred.await(started);
+ yield* TestClock.adjust("250 millis");
+ yield* Fiber.interrupt(fiber);
+
+ assert.strictEqual(yield* gitCommandCount("other", "interrupt"), 1);
+ assert.strictEqual(yield* gitCommandCount(operation, "interrupt"), 0);
+ const timing = yield* gitCommandTiming("other");
+ assert.strictEqual(timing.count, 1);
+ assert.strictEqual(timing.sum, 250);
+ }).pipe(
+ Effect.provide(NodeServices.layer),
+ Effect.provideService(Metric.MetricRegistry, new Map()),
+ ),
+);
+
+it.effect("git metrics count fsync fallback and transient retry subprocesses separately", () =>
+ Effect.gen(function* () {
+ const retryReady = yield* Deferred.make<void>();
+ const operation = VcsProcess.CHECKPOINT_CAPTURE_OPERATION;
+ let attempts = 0;
+ const process = yield* VcsProcess.make.pipe(
+ Effect.provideService(HostProcess.Platform, "darwin"),
+ Effect.provideService(ProcessRunner.ProcessRunner, {
+ run: (input) =>
+ Effect.gen(function* () {
+ attempts++;
+ yield* TestClock.adjust("250 millis");
+ if (attempts === 2) {
+ assert.include(input.args, "core.fsyncMethod=writeout-only");
+ yield* Deferred.succeed(retryReady, undefined);
+ }
+ return {
+ code: ChildProcessSpawner.ExitCode(attempts < 3 ? 128 : 0),
+ stdout: "",
+ stderr:
+ attempts === 1
+ ? "fatal: fsync error on '.git/objects/test': Operation not supported"
+ : attempts === 2
+ ? "fatal: Unable to create '/repo/.git/index.lock': File exists."
+ : "",
+ timedOut: false,
+ stdoutTruncated: false,
+ stderrTruncated: false,
+ stdoutInvalidUtf8: false,
+ stderrInvalidUtf8: false,
+ };
+ }),
+ }),
+ );
+ const driver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
+ Effect.provideService(VcsProcess.VcsProcess, process),
+ );
+ const fiber = yield* driver
+ .execute({ operation, cwd: "/repo", args: ["-c", "core.fsyncMethod=batch", "add"] })
+ .pipe(Effect.forkChild);
+ yield* Deferred.await(retryReady);
+ yield* TestClock.adjust("75 millis");
+ yield* Fiber.join(fiber);
+
+ assert.strictEqual(attempts, 3);
+ assert.strictEqual(yield* gitCommandCount(operation, "failure"), 2);
+ assert.strictEqual(yield* gitCommandCount(operation, "success"), 1);
+ const timing = yield* gitCommandTiming(operation);
+ assert.strictEqual(timing.count, 3);
+ assert.strictEqual(timing.sum, 750);
+ }).pipe(
+ Effect.provide(NodeServices.layer),
+ Effect.provideService(Metric.MetricRegistry, new Map()),
+ ),
+);
diff --git a/apps/server/src/vcs/GitVcsDriver.ts b/apps/server/src/vcs/GitVcsDriver.ts
index 684285d608d..3f839643086 100644
--- a/apps/server/src/vcs/GitVcsDriver.ts
+++ b/apps/server/src/vcs/GitVcsDriver.ts
@@ -503,39 +503,41 @@ function parseGitRemoteVerboseOutput(
return remotes;
}
-const gitCommand = (
- process: VcsProcess.VcsProcess["Service"],
- operation: string,
- cwd: string,
- args: ReadonlyArray<string>,
- options?: {
- readonly stdin?: string;
- readonly env?: NodeJS.ProcessEnv;
- readonly allowNonZeroExit?: boolean;
- readonly timeoutMs?: number;
- readonly maxOutputBytes?: number;
- readonly outputMode?: VcsProcess.VcsProcessInput["outputMode"];
- readonly appendTruncationMarker?: boolean;
- },
-) =>
- process.run({
- operation,
- command: "git",
- args: ["-C", cwd, ...args],
- cwd,
- spawnCwd: globalThis.process.cwd(),
- ...(options?.stdin !== undefined ? { stdin: options.stdin } : {}),
- ...(options?.env !== undefined ? { env: options.env } : {}),
- ...(options?.allowNonZeroExit !== undefined
- ? { allowNonZeroExit: options.allowNonZeroExit }
- : {}),
- ...(options?.timeoutMs !== undefined ? { timeoutMs: options.timeoutMs } : {}),
- ...(options?.maxOutputBytes !== undefined ? { maxOutputBytes: options.maxOutputBytes } : {}),
- ...(options?.outputMode !== undefined ? { outputMode: options.outputMode } : {}),
- ...(options?.appendTruncationMarker !== undefined
- ? { appendTruncationMarker: options.appendTruncationMarker }
- : {}),
- });
+const gitCommand = Effect.fn("GitVcsDriver.gitCommand")(
+ (
+ process: VcsProcess.VcsProcess["Service"],
+ operation: string,
+ cwd: string,
+ args: ReadonlyArray<string>,
+ options?: {
+ readonly stdin?: string;
+ readonly env?: NodeJS.ProcessEnv;
+ readonly allowNonZeroExit?: boolean;
+ readonly timeoutMs?: number;
+ readonly maxOutputBytes?: number;
+ readonly outputMode?: VcsProcess.VcsProcessInput["outputMode"];
+ readonly appendTruncationMarker?: boolean;
+ },
+ ) =>
+ process.run({
+ operation,
+ command: "git",
+ args: ["-C", cwd, ...args],
+ cwd,
+ spawnCwd: globalThis.process.cwd(),
+ ...(options?.stdin !== undefined ? { stdin: options.stdin } : {}),
+ ...(options?.env !== undefined ? { env: options.env } : {}),
+ ...(options?.allowNonZeroExit !== undefined
+ ? { allowNonZeroExit: options.allowNonZeroExit }
+ : {}),
+ ...(options?.timeoutMs !== undefined ? { timeoutMs: options.timeoutMs } : {}),
+ ...(options?.maxOutputBytes !== undefined ? { maxOutputBytes: options.maxOutputBytes } : {}),
+ ...(options?.outputMode !== undefined ? { outputMode: options.outputMode } : {}),
+ ...(options?.appendTruncationMarker !== undefined
+ ? { appendTruncationMarker: options.appendTruncationMarker }
+ : {}),
+ }),
+);
export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* () {
const fileSystem = yield* FileSystem.FileSystem;
diff --git a/apps/server/src/vcs/VcsProcess.ts b/apps/server/src/vcs/VcsProcess.ts
index 40823895077..2a62c6fb1ac 100644
--- a/apps/server/src/vcs/VcsProcess.ts
+++ b/apps/server/src/vcs/VcsProcess.ts
@@ -21,6 +21,7 @@ import {
} from "@t3tools/contracts";
import * as HostProcess from "@t3tools/shared/HostProcess";
+import { gitCommandDuration, gitCommandsTotal, withMetrics } from "../observability/Metrics.ts";
import * as ProcessRunner from "../processRunner.ts";
export interface VcsProcessInput {
@@ -68,6 +69,27 @@ export const GIT_INDEX_LOCKED_DETAIL =
export const CHECKPOINT_CAPTURE_OPERATION = "GitVcsDriver.checkpoints.captureCheckpoint";
+// Caller operations are diagnostic strings; only known labels may become metric attributes.
+const gitMetricOperations = new Set([
+ CHECKPOINT_CAPTURE_OPERATION,
+ "GitVcsDriver.checkpoints.deleteCheckpointRefs",
+ "GitVcsDriver.checkpoints.diffCheckpoints",
+ "GitVcsDriver.checkpoints.hasHeadCommit",
+ "GitVcsDriver.checkpoints.listAuthoredPaths",
+ "GitVcsDriver.checkpoints.resolveCheckpointCommit",
+ "GitVcsDriver.checkpoints.resolveGitCommonDir",
+ "GitVcsDriver.checkpoints.resolveHeadCommit",
+ "GitVcsDriver.checkpoints.restoreCheckpoint",
+ "GitVcsDriver.detectRepository.commonDir",
+ "GitVcsDriver.detectRepository.root",
+ "GitVcsDriver.filterIgnoredPaths",
+ "GitVcsDriver.initRepository",
+ "GitVcsDriver.isInsideWorkTree",
+ "GitVcsDriver.listRemotes",
+ "GitVcsDriver.listWorkspaceFiles",
+ "GitVcsDriver.resolveGitCommonDir",
+]);
+
/**
* Appends `key=value` to the `GIT_CONFIG_*` entries git reads as command-line
* config, so argv stays the same on every platform and nothing is written to
@@ -184,8 +206,12 @@ export const make = Effect.gen(function* () {
const vcsProcesses = yield* Semaphore.make(VCS_PROCESS_CONCURRENCY);
const githubProcesses = yield* Semaphore.make(GITHUB_PROCESS_CONCURRENCY);
- const spawn = (input: VcsProcessInput, baseError: VcsProcessErrorContext) =>
- processRunner
+ const spawn = Effect.fn("VcsProcess.spawn")(function* (
+ input: VcsProcessInput,
+ baseError: VcsProcessErrorContext,
+ ) {
+ let failedExit = false;
+ const execution = processRunner
.run({
command: input.command,
args: input.args,
@@ -204,6 +230,10 @@ export const make = Effect.gen(function* () {
timeoutBehavior: "error",
})
.pipe(
+ Effect.tap((result) => {
+ failedExit = result.code === null || (!input.allowNonZeroExit && result.code !== 0);
+ return Effect.void;
+ }),
Effect.mapError(
Match.valueTags({
ProcessSpawnError: (error) =>
@@ -233,6 +263,22 @@ export const make = Effect.gen(function* () {
),
);
+ return yield* input.command === "git"
+ ? execution.pipe(
+ withMetrics({
+ counter: gitCommandsTotal,
+ timer: gitCommandDuration,
+ attributes: {
+ operation: gitMetricOperations.has(input.operation) ? input.operation : "other",
+ },
+ outcomeAttributes: (outcome) => ({
+ outcome: outcome === "success" && failedExit ? "failure" : outcome,
+ }),
+ }),
+ )
+ : execution;
+ });
+
const runUnbounded = Effect.fn("VcsProcess.runUnbounded")(function* (input: VcsProcessInput) {
const baseError = {
operation: input.operation,