TypeScript
fix(server): recover pulls from unrelated ref locks (#22107)
Yash Singh3fdcef099449
diff --git a/apps/server/src/vcs/GitVcsDriver.ts b/apps/server/src/vcs/GitVcsDriver.tsindex 5c7b20b34c5..10a7c183270 100644--- a/apps/server/src/vcs/GitVcsDriver.ts+++ b/apps/server/src/vcs/GitVcsDriver.ts@@ -50,6 +50,7 @@ export interface ExecuteGitInput {readonly env?: NodeJS.ProcessEnv;readonly allowNonZeroExit?: boolean;readonly timeoutMs?: number | null;+ readonly forceKillAfterMs?: number;readonly maxOutputBytes?: number;readonly appendTruncationMarker?: boolean;/**diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.tsindex db48a5e9526..304c2a537fd 100644--- a/apps/server/src/vcs/GitVcsDriverCore.test.ts+++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts@@ -2,6 +2,7 @@import * as NodeFS from "node:fs";import * as NodeServices from "@effect/platform-node/NodeServices";import * as HostProcess from "@t3tools/shared/HostProcess";+import { writeFakeCli } from "@t3tools/provider-testing/fakeCli";import { assert, it, describe } from "@effect/vitest";import * as Deferred from "effect/Deferred";import * as Effect from "effect/Effect";@@ -39,6 +40,7 @@ import {} from "./GitVcsDriverCore.ts";import * as GitVcsDriver from "./GitVcsDriver.ts";import { gitConfigEnv } from "./VcsProcess.ts";+import { makeProcessProbe, processProbeSource } from "../testUtils/processProbe.ts";const encodeGitCommandError = Schema.encodeEffect(Schema.fromJsonString(GitCommandError));@@ -492,10 +494,259 @@ it.effect("coalesces concurrent ref pages into one repository snapshot", () =>).pipe(Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer)))),);-describe("pull output beyond the output cap", () => {+describe("pullCurrentBranch", () => {+ const controlledStatusFetch = Effect.gen(function* () {+ const fs = yield* FileSystem.FileSystem;+ const cwd = yield* makeTmpDir();+ yield* fs.makeDirectory(`${cwd}/.git`);+ const started = yield* Queue.unbounded<{+ cwd: string;+ release: Deferred.Deferred<ChildProcessSpawner.ExitCode>;+ }>();+ const finalized = yield* Queue.unbounded<string>();+ const warnings = yield* Queue.unbounded<void>();+ const attempts = yield* Ref.make(0);+ const pulls = yield* Ref.make(0);+ const fetching = yield* Ref.make(false);+ const head = yield* Ref.make("before");+ const pullRemoteRead = yield* Deferred.make<void>();+ const logger = Logger.make<unknown, void>(() => {+ Queue.offerUnsafe(warnings, undefined);+ });+ const spawner = ChildProcessSpawner.make((command) =>+ Effect.gen(function* () {+ if (!ChildProcess.isStandardCommand(command)) return makeNonRepositoryHandle();+ const workingDirectory = command.options.cwd ?? cwd;+ const args = command.args;+ if (args.includes("fetch")) {+ const release = yield* Deferred.make<ChildProcessSpawner.ExitCode>();+ yield* Ref.set(fetching, true);+ yield* Ref.update(attempts, (count) => count + 1);+ yield* Queue.offer(started, { cwd: workingDirectory, release });+ yield* Effect.addFinalizer(() => Queue.offer(finalized, workingDirectory));+ yield* Effect.addFinalizer(() => Ref.set(fetching, false));+ return ChildProcessSpawner.makeHandle({+ ...makeSuccessfulHandle(""),+ stderr: Stream.encodeText(Stream.make("fatal: remote unavailable\n")),+ exitCode: Deferred.await(release),+ });+ }+ if (args[0] === "pull") {+ yield* Ref.update(pulls, (count) => count + 1);+ if (yield* Ref.get(fetching)) {+ return ChildProcessSpawner.makeHandle({+ ...makeSuccessfulHandle(""),+ exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(1)),+ stderr: Stream.encodeText(+ Stream.make("error: cannot lock ref 'refs/remotes/origin/main': File exists\n"),+ ),+ });+ }+ yield* Ref.set(head, "after");+ return makeSuccessfulHandle("");+ }+ if (args.includes("branch.main.remote")) {+ yield* Deferred.succeed(pullRemoteRead, undefined);+ return makeSuccessfulHandle("origin\n");+ }+ if (args[0] === "status")+ return makeSuccessfulHandle(+ "# branch.oid before\0# branch.head main\0# branch.upstream origin/main\0# branch.ab +0 -1\0",+ );+ if (args[0] === "diff-index") return makeSuccessfulHandle("");+ if (args.length === 2 && args[0] === "rev-parse" && args[1] === "HEAD")+ return makeSuccessfulHandle(`${yield* Ref.get(head)}\n`);+ if (args.includes("--git-common-dir"))+ return makeSuccessfulHandle(`${workingDirectory}/.git\n`);+ if (args.includes("--show-toplevel")) return makeSuccessfulHandle(`${workingDirectory}\n`);+ if (args.includes("@{upstream}")) return makeSuccessfulHandle("origin/main\n");+ if (args[0] === "remote") return makeSuccessfulHandle("origin\n");+ if (args[0] === "rev-list") return makeSuccessfulHandle("0\t0\n");+ if (args.includes("refs/remotes/origin/HEAD"))+ return makeSuccessfulHandle("refs/remotes/origin/main\n");+ return makeSuccessfulHandle("main\n");+ }),+ );+ const driver = yield* makeGitVcsDriverCore().pipe(+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),+ );+ const status = Effect.fn("test.controlledStatusFetch.status")((directory = cwd) =>+ driver+ .statusDetailsRemote(directory)+ .pipe(Effect.provideService(Logger.CurrentLoggers, new Set([logger]))),+ );+ return { cwd, driver, status, started, finalized, warnings, attempts, pulls, pullRemoteRead };+ });++ it.effect.each([0, 128])(+ "waits for an existing status fetch before pulling after fetch exit %s",+ (exitCode) =>+ Effect.gen(function* () {+ const fixture = yield* controlledStatusFetch;+ const status = yield* fixture.status().pipe(Effect.forkScoped);+ const fetch = yield* Queue.take(fixture.started);+ yield* TestClock.adjust("5 seconds");+ yield* Fiber.join(status);++ const pull = yield* fixture.driver.pullCurrentBranch(fixture.cwd).pipe(Effect.forkScoped);+ yield* Deferred.await(fixture.pullRemoteRead);+ yield* TestClock.adjust("30 seconds");+ assert.equal(yield* Ref.get(fixture.pulls), 0);+ yield* Deferred.succeed(fetch.release, ChildProcessSpawner.ExitCode(exitCode));++ assert.equal((yield* Fiber.join(pull)).status, "pulled");+ assert.equal(yield* Ref.get(fixture.pulls), 1);+ assert.equal(yield* Ref.get(fixture.attempts), 1);+ }).pipe(+ Effect.scoped,+ Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer))),+ ),+ );++ it.effect("does not start a status fetch while pulling", () =>+ Effect.gen(function* () {+ const fixture = yield* controlledStatusFetch;++ assert.equal((yield* fixture.driver.pullCurrentBranch(fixture.cwd)).status, "pulled");+ assert.equal(yield* Ref.get(fixture.pulls), 1);+ assert.equal(yield* Ref.get(fixture.attempts), 0);+ }).pipe(+ Effect.scoped,+ Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer))),+ ),+ );++ it.effect("backs off failures after the status waiter has returned", () =>+ Effect.gen(function* () {+ const fixture = yield* controlledStatusFetch;+ const firstStatus = yield* fixture.status().pipe(Effect.forkScoped);+ const first = yield* Queue.take(fixture.started);+ yield* TestClock.adjust("5 seconds");+ yield* Fiber.join(firstStatus);+ yield* Deferred.succeed(first.release, ChildProcessSpawner.ExitCode(128));+ yield* Queue.take(fixture.finalized);+ yield* Queue.take(fixture.warnings);++ yield* TestClock.adjust("15 seconds");+ yield* fixture.status();+ assert.equal(yield* Ref.get(fixture.attempts), 1);++ yield* TestClock.adjust("15 seconds");+ const secondStatus = yield* fixture.status().pipe(Effect.forkScoped);+ const second = yield* Queue.take(fixture.started);+ yield* TestClock.adjust("5 seconds");+ yield* Fiber.join(secondStatus);+ yield* Deferred.succeed(second.release, ChildProcessSpawner.ExitCode(128));+ yield* Queue.take(fixture.finalized);+ yield* Queue.take(fixture.warnings);++ for (let i = 0; i < 3; i++) {+ yield* TestClock.adjust("15 seconds");+ yield* fixture.status();+ assert.equal(yield* Ref.get(fixture.attempts), 2);+ }+ yield* TestClock.adjust("15 seconds");+ const recoveredStatus = yield* fixture.status().pipe(Effect.forkScoped);+ const recovered = yield* Queue.take(fixture.started);+ yield* Deferred.succeed(recovered.release, ChildProcessSpawner.ExitCode(0));+ yield* Fiber.join(recoveredStatus);+ assert.equal(yield* Ref.get(fixture.attempts), 3);++ yield* TestClock.adjust("15 seconds");+ const failedAgainStatus = yield* fixture.status().pipe(Effect.forkScoped);+ const failedAgain = yield* Queue.take(fixture.started);+ yield* Deferred.succeed(failedAgain.release, ChildProcessSpawner.ExitCode(128));+ yield* Fiber.join(failedAgainStatus);+ yield* Queue.take(fixture.warnings);+ yield* TestClock.adjust("15 seconds");+ yield* fixture.status();+ assert.equal(yield* Ref.get(fixture.attempts), 4);++ yield* TestClock.adjust("15 seconds");+ const resetStatus = yield* fixture.status().pipe(Effect.forkScoped);+ yield* TestClock.adjust("5 seconds");+ yield* Fiber.join(resetStatus);+ assert.equal(yield* Ref.get(fixture.attempts), 5);+ const reset = yield* Queue.take(fixture.started);+ yield* Deferred.succeed(reset.release, ChildProcessSpawner.ExitCode(0));+ }).pipe(+ Effect.scoped,+ Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer))),+ ),+ );++ it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(+ "bounds cleanup when a Git subprocess ignores SIGTERM",+ () =>+ Effect.gen(function* () {+ const driver = yield* GitVcsDriver.GitVcsDriver;+ const directory = yield* makeTmpDir();+ const probe = yield* makeProcessProbe;+ writeFakeCli({+ directory,+ name: "git",+ source: `process.on("SIGTERM", () => {}); ${processProbeSource(probe.port)}`,+ });+ const run = yield* driver+ .execute({+ operation: "test.stalledStatusFetch",+ cwd: directory,+ args: ["fetch"],+ env: { PATH: `${directory}:${process.env.PATH ?? ""}` },+ timeoutMs: 1_000,+ forceKillAfterMs: 100,+ })+ .pipe(Effect.flip, Effect.forkScoped);+ yield* probe.started;++ yield* TestClock.adjust("1 second");++ yield* probe.exited;+ assert.equal((yield* Fiber.join(run)).detail, "Git command timed out.");+ }).pipe(Effect.scoped, Effect.provide(layerTest)),+ );++ it.effect("releases stalled background worker slots at their lifetime limit", () =>+ Effect.gen(function* () {+ const fs = yield* FileSystem.FileSystem;+ const fixture = yield* controlledStatusFetch;+ yield* fixture.driver.listRefs({ cwd: fixture.cwd });+ const statuses = [];+ for (let i = 0; i < 4; i++) {+ const cwd = `${fixture.cwd}/repo-${i}`;+ yield* fs.makeDirectory(`${cwd}/.git`, { recursive: true });+ statuses.push(yield* fixture.status(cwd).pipe(Effect.forkScoped));+ yield* Queue.take(fixture.started);+ }+ yield* TestClock.adjust("5 seconds");+ yield* Effect.forEach(statuses, Fiber.join);+ const waiting = yield* fixture.status().pipe(Effect.forkScoped);+ yield* TestClock.adjust("5 seconds");+ yield* Fiber.join(waiting);+ assert.equal(yield* Ref.get(fixture.attempts), 4);++ yield* TestClock.adjust(290_000);+ assert.equal(yield* Ref.get(fixture.attempts), 5);+ for (let i = 0; i < 4; i++) yield* Queue.take(fixture.finalized);+ const healthy = yield* Queue.take(fixture.started);+ assert.equal(+ NodeFS.realpathSync.native(healthy.cwd),+ NodeFS.realpathSync.native(fixture.cwd),+ );+ yield* Deferred.succeed(healthy.release, ChildProcessSpawner.ExitCode(0));+ yield* Queue.take(fixture.finalized);+ }).pipe(+ Effect.scoped,+ Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer))),+ ),+ );+// A fast-forward that prints over 1 MB takes thousands of files, so the real pull's output is// padded past the cap instead.- const withPaddedPull = (stream: "stdout" | "stderr") =>+ const withPaddedPull = (+ stream: "stdout" | "stderr",+ onPull?: (args: ReadonlyArray<string>) => void,+ ) =>Effect.gen(function* () {const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;const padding = new Uint8Array(1_000_001).fill(0x20);@@ -506,6 +757,7 @@ describe("pull output beyond the output cap", () => {ChildProcess.isStandardCommand(command) &&command.args.includes("pull") &&command.args.includes("--ff-only");+ if (isPull) onPull?.(command.args);return isPull? ChildProcessSpawner.makeHandle({...handle,@@ -534,9 +786,235 @@ describe("pull output beyond the output cap", () => {yield* git(updater, ["add", "remote.txt"]);yield* git(updater, ["commit", "-m", "remote commit"]);yield* git(updater, ["push", "origin", initialBranch]);- return { cwd, remoteHead: yield* git(updater, ["rev-parse", "HEAD"]) };+ return {+ cwd,+ remote,+ updater,+ initialBranch,+ remoteHead: yield* git(updater, ["rev-parse", "HEAD"]),+ };});+ it.effect("still fetches other branches on the first pull", () =>+ Effect.gen(function* () {+ const driver = yield* GitVcsDriver.GitVcsDriver;+ const { cwd, updater, remoteHead } = yield* cloneBehindRemote;+ yield* git(updater, ["push", "origin", "HEAD:refs/heads/other"]);++ const result = yield* driver.pullCurrentBranch(cwd);++ assert.equal(result.status, "pulled");+ assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), remoteHead);+ assert.equal(yield* git(cwd, ["rev-parse", "refs/remotes/origin/other"]), remoteHead);+ }).pipe(Effect.provide(layerTest)),+ );++ it.effect.each([false, true])(+ "pulls past an unrelated lock with custom fetch mapping=%s",+ (customMapping) =>+ Effect.gen(function* () {+ const driver = yield* GitVcsDriver.GitVcsDriver;+ const fs = yield* FileSystem.FileSystem;+ const { cwd, updater, initialBranch, remoteHead } = yield* cloneBehindRemote;+ yield* git(updater, ["push", "origin", "HEAD:refs/heads/other"]);+ const remoteName = customMapping ? "team/upstream" : "origin";+ const refPrefix = customMapping+ ? `refs/remotes/${remoteName}/mapped`+ : "refs/remotes/origin";+ if (customMapping) {+ yield* git(cwd, ["remote", "rename", "origin", remoteName]);+ yield* git(cwd, ["config", `remote.${remoteName}.fetch`, `+refs/heads/*:${refPrefix}/*`]);+ yield* git(cwd, ["update-ref", `${refPrefix}/${initialBranch}`, "HEAD"]);+ }+ const lock = `.git/${refPrefix}/other.lock`;+ yield* writeTextFile(cwd, lock, "");++ const result = yield* driver.pullCurrentBranch(cwd);++ assert.equal(result.status, "pulled");+ assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), remoteHead);+ assert.equal(yield* git(cwd, ["rev-parse", `${refPrefix}/${initialBranch}`]), remoteHead);+ assert.isTrue(yield* fs.exists(`${cwd}/${lock}`));+ assert.equal(yield* fs.readFileString(`${cwd}/${lock}`), "");+ }).pipe(Effect.provide(layerTest)),+ );++ it.effect("reports an upstream lock after the fallback without removing it", () =>+ Effect.gen(function* () {+ const driver = yield* GitVcsDriver.GitVcsDriver;+ const fs = yield* FileSystem.FileSystem;+ const { cwd, initialBranch } = yield* cloneBehindRemote;+ const before = yield* git(cwd, ["rev-parse", "HEAD"]);+ const lock = `.git/refs/remotes/origin/${initialBranch}.lock`;+ yield* writeTextFile(cwd, lock, "");++ const error = yield* Effect.flip(driver.pullCurrentBranch(cwd));++ assert.include(error.detail, "could not update a local reference");+ assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), before);+ assert.isTrue(yield* fs.exists(`${cwd}/${lock}`));+ }).pipe(Effect.provide(layerTest)),+ );++ it.effect.each(["branch", "HEAD"] as const)(+ "does not retry a failed fetch if another operation moved %s",+ (moved) =>+ Effect.gen(function* () {+ const fixtureDriver = yield* GitVcsDriver.GitVcsDriver;+ const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;+ const { cwd, updater } = yield* cloneBehindRemote;+ yield* git(updater, ["push", "origin", "HEAD:refs/heads/other"]);+ yield* writeTextFile(cwd, ".git/refs/remotes/origin/other.lock", "");+ let pulls = 0;+ const spawner = ChildProcessSpawner.make((command) =>+ Effect.gen(function* () {+ const handle = yield* delegate.spawn(command);+ if (!ChildProcess.isStandardCommand(command) || !command.args.includes("pull")) {+ return handle;+ }+ pulls += 1;+ return ChildProcessSpawner.makeHandle({+ ...handle,+ exitCode: handle.exitCode.pipe(+ Effect.tap(() =>+ git(+ cwd,+ moved === "branch"+ ? ["checkout", "-b", "moved"]+ : ["commit", "--allow-empty", "-m", "concurrent commit"],+ ).pipe(+ Effect.provideService(GitVcsDriver.GitVcsDriver, fixtureDriver),+ Effect.orDie,+ ),+ ),+ ),+ });+ }),+ );+ const driver = yield* makeGitVcsDriverCore().pipe(+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),+ );++ const error = yield* Effect.flip(driver.pullCurrentBranch(cwd));++ assert.include(error.detail, "could not update a local reference");+ assert.equal(pulls, 1);+ }).pipe(Effect.scoped, Effect.provide(layerServerConfig.pipe(Layer.provideMerge(layerTest)))),+ );++ it.effect.each(["deadline", "subscriber cancellation"] as const)(+ "lets a background fetch finish after %s without leaving its lock behind",+ (interruption) =>+ Effect.gen(function* () {+ const fs = yield* FileSystem.FileSystem;+ const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;+ const { cwd } = yield* cloneBehindRemote;+ const lockPath = `${cwd}/.git/refs/remotes/origin/in-flight.lock`;+ const started = yield* Deferred.make<void>();+ const release = yield* Deferred.make<void>();+ const finalized = yield* Deferred.make<void>();+ const spawner = ChildProcessSpawner.make((command) =>+ Effect.gen(function* () {+ if (!ChildProcess.isStandardCommand(command) || !command.args.includes("fetch")) {+ return yield* delegate.spawn(command);+ }+ // Model Git's gap between creating a lock and registering signal cleanup.+ yield* fs.writeFileString(lockPath, "");+ yield* Effect.addFinalizer(() => Deferred.succeed(finalized, undefined));+ yield* Deferred.succeed(started, undefined);+ return ChildProcessSpawner.makeHandle({+ ...makeSuccessfulHandle(""),+ exitCode: Deferred.await(release).pipe(+ Effect.andThen(fs.remove(lockPath)),+ Effect.as(ChildProcessSpawner.ExitCode(0)),+ ),+ });+ }),+ );+ const driver = yield* makeGitVcsDriverCore().pipe(+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),+ );+ const status = yield* driver.statusDetailsRemote(cwd).pipe(Effect.forkScoped);+ yield* Deferred.await(started);++ if (interruption === "deadline") {+ yield* TestClock.adjust("5 seconds");+ yield* Fiber.join(status);+ } else {+ yield* Fiber.interrupt(status);+ }++ assert.isFalse(yield* Deferred.isDone(finalized));+ assert.isTrue(yield* fs.exists(lockPath));++ yield* Deferred.succeed(release, undefined);+ yield* Deferred.await(finalized);+ assert.isFalse(yield* fs.exists(lockPath));+ }).pipe(Effect.scoped, Effect.provide(layerServerConfig.pipe(Layer.provideMerge(layerTest)))),+ );++ it.effect("reuses a background fetch across status wait deadlines", () =>+ Effect.gen(function* () {+ const fs = yield* FileSystem.FileSystem;+ const cwd = yield* makeTmpDir();+ yield* fs.makeDirectory(`${cwd}/.git`);+ const started = yield* Deferred.make<void>();+ const release = yield* Deferred.make<void>();+ const finalized = yield* Deferred.make<void>();+ const attempts = yield* Ref.make(0);+ const spawner = ChildProcessSpawner.make((command) =>+ Effect.gen(function* () {+ if (!ChildProcess.isStandardCommand(command)) return makeNonRepositoryHandle();+ if (command.args.includes("fetch")) {+ yield* Ref.update(attempts, (count) => count + 1);+ yield* Deferred.succeed(started, undefined);+ yield* Effect.addFinalizer(() => Deferred.succeed(finalized, undefined));+ return ChildProcessSpawner.makeHandle({+ ...makeSuccessfulHandle(""),+ exitCode: Deferred.await(release).pipe(Effect.as(ChildProcessSpawner.ExitCode(0))),+ });+ }+ const args = command.args;+ return makeSuccessfulHandle(+ args.includes("--git-common-dir")+ ? `${cwd}/.git\n`+ : args.includes("--show-toplevel")+ ? `${cwd}\n`+ : args.includes("@{upstream}")+ ? "origin/main\n"+ : args[0] === "remote"+ ? "origin\n"+ : args[0] === "rev-list"+ ? "0\t0\n"+ : args.includes("refs/remotes/origin/HEAD")+ ? "refs/remotes/origin/main\n"+ : "main\n",+ );+ }),+ );+ const driver = yield* makeGitVcsDriverCore().pipe(+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),+ );+ const first = yield* driver.statusDetailsRemote(cwd).pipe(Effect.forkScoped);+ yield* Deferred.await(started);+ yield* TestClock.adjust("5 seconds");+ yield* Fiber.join(first);++ yield* TestClock.adjust("15 seconds");+ const second = yield* driver.statusDetailsRemote(cwd).pipe(Effect.forkScoped);+ yield* TestClock.adjust("5 seconds");+ yield* Fiber.join(second);++ assert.equal(yield* Ref.get(attempts), 1);+ assert.isFalse(yield* Deferred.isDone(finalized));+ yield* Deferred.succeed(release, undefined);+ yield* Deferred.await(finalized);+ }).pipe(+ Effect.scoped,+ Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer))),+ ),+ );+it.effect.each(["stdout", "stderr"] as const)("reports a fast-forward whose %s passes the cap as pulled",(stream) =>@@ -558,7 +1036,8 @@ describe("pull output beyond the output cap", () => {it.effect("still fails a pull that Git rejects, whatever its output size", () =>Effect.scoped(Effect.gen(function* () {- const driver = yield* withPaddedPull("stderr");+ const pulls: ReadonlyArray<string>[] = [];+ const driver = yield* withPaddedPull("stderr", (args) => pulls.push(args));yield* Effect.gen(function* () {const { cwd } = yield* cloneBehindRemote;yield* writeTextFile(cwd, "local.txt", "local\n");@@ -570,6 +1049,7 @@ describe("pull output beyond the output cap", () => {assert.equal(error.operation, "GitVcsDriver.pullCurrentBranch.pull");assert.equal(error.exitCode, 128);assert.notInclude(error.detail, "exceeded");+ assert.lengthOf(pulls, 1);}).pipe(Effect.provideService(GitVcsDriver.GitVcsDriver, driver));}),).pipe(Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer)))),diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.tsindex 475ba3d2a95..1d706a86c1a 100644--- a/apps/server/src/vcs/GitVcsDriverCore.ts+++ b/apps/server/src/vcs/GitVcsDriverCore.ts@@ -1,5 +1,6 @@import * as Cache from "effect/Cache";import * as Cause from "effect/Cause";+import * as Clock from "effect/Clock";import * as Data from "effect/Data";import * as Crypto from "effect/Crypto";import * as DateTime from "effect/DateTime";@@ -7,6 +8,7 @@ import * as Duration from "effect/Duration";import * as Effect from "effect/Effect";import * as Hex from "effect/encoding/Hex";import * as Exit from "effect/Exit";+import * as Fiber from "effect/Fiber";import * as FileSystem from "effect/FileSystem";import * as Option from "effect/Option";import * as Path from "effect/Path";@@ -80,6 +82,7 @@ const REVIEW_DIFF_ARGS = [];const STATUS_UPSTREAM_REFRESH_INTERVAL = Duration.seconds(15);const STATUS_UPSTREAM_REFRESH_TIMEOUT = Duration.seconds(5);+const STATUS_UPSTREAM_FETCH_TIMEOUT = Duration.minutes(5);const STATUS_UPSTREAM_REFRESH_FAILURE_BASE_COOLDOWN = Duration.seconds(30);const STATUS_UPSTREAM_REFRESH_FAILURE_MAX_COOLDOWN = Duration.minutes(15);@@ -170,6 +173,7 @@ interface GitRefsSnapshot {interface ExecuteGitOptions {stdin?: string | undefined;timeoutMs?: number | null | undefined;+ forceKillAfterMs?: number | undefined;allowNonZeroExit?: boolean | undefined;fallbackErrorDetail?: string | undefined;env?: NodeJS.ProcessEnv | undefined;@@ -916,6 +920,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*const { worktreesDir } = yield* ServerConfig.ServerConfig;const crypto = yield* Crypto.Crypto;const hostPlatform = yield* HostProcess.Platform;+ const serviceScope = yield* Effect.scope;+ const statusFetches = new Map<string, Fiber.Fiber<void, GitCommandError>>();+ const statusFetchProcesses = yield* Semaphore.make(4);const executeRaw: GitVcsDriver.GitVcsDriver["Service"]["execute"] = Effect.fnUntraced(function* (input) {@@ -959,6 +966,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*ChildProcess.make(executable, commandInput.args, {cwd: commandInput.cwd,env: spawnEnv,+ forceKillAfter: input.forceKillAfterMs,}),).pipe(@@ -1096,6 +1104,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*...(options.env !== undefined ? { env: options.env } : {}),allowNonZeroExit: true,...(options.timeoutMs !== undefined ? { timeoutMs: options.timeoutMs } : {}),+ ...(options.forceKillAfterMs !== undefined+ ? { forceKillAfterMs: options.forceKillAfterMs }+ : {}),...(options.maxOutputBytes !== undefined ? { maxOutputBytes: options.maxOutputBytes } : {}),...(options.appendTruncationMarker !== undefined? { appendTruncationMarker: options.appendTruncationMarker }@@ -1244,36 +1255,71 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*);});- const fetchRemoteForStatus = (+ const fetchRemoteForStatus = Effect.fn("GitVcsDriver.fetchRemoteForStatus.wait")(function* (gitCommonDir: string,remoteName: string,- ): Effect.Effect<void, GitCommandError> => {+ ) {const fetchCwd =path.basename(gitCommonDir) === ".git" ? path.dirname(gitCommonDir) : gitCommonDir;// `--no-auto-gc` (a synonym of `--no-auto-maintenance` that older Git also knows) keeps// this poll from starting `git gc --auto`. When that gc fails, for example on a repository// with missing objects, Git retries it on every fetch and leaves a full-size `tmp_pack_*`// behind each time, so a background poll could fill the disk.- return executeGit(- "GitVcsDriver.fetchRemoteForStatus",- fetchCwd,- [- "--git-dir",- gitCommonDir,- "fetch",- "--quiet",- "--no-tags",- "--no-auto-gc",- "--",- remoteName,- ],- {- env: STATUS_UPSTREAM_REFRESH_ENV,- fallbackErrorDetail: "Background Git fetch exited with a non-zero status.",- timeoutMs: Duration.toMillis(STATUS_UPSTREAM_REFRESH_TIMEOUT),- },- ).pipe(Effect.asVoid);- };+ const args = [+ "--git-dir",+ gitCommonDir,+ "fetch",+ "--quiet",+ "--no-tags",+ "--no-auto-gc",+ "--",+ remoteName,+ ];+ const key = `${gitCommonDir}\0${remoteName}`;+ const failure = statusRemoteRefreshFailures.get(key);+ if (failure && (yield* Clock.currentTimeMillis) < failure.retryAt) return;+ const fiber = yield* Effect.uninterruptible(+ Effect.gen(function* () {+ const pending = statusFetches.get(key);+ if (pending) return pending;+ // Frequent poll deadlines and disconnected clients must not interrupt a+ // healthy writer. A separate safety limit prevents a hung transport from+ // occupying its worker slot indefinitely.+ const started = yield* executeGit("GitVcsDriver.fetchRemoteForStatus", fetchCwd, args, {+ env: STATUS_UPSTREAM_REFRESH_ENV,+ fallbackErrorDetail: "Background Git fetch exited with a non-zero status.",+ timeoutMs: Duration.toMillis(STATUS_UPSTREAM_FETCH_TIMEOUT),+ forceKillAfterMs: 1_000,+ }).pipe(+ Effect.asVoid,+ Effect.tap(() =>+ Effect.sync(() => {+ statusRemoteRefreshFailures.delete(key);+ }),+ ),+ Effect.tapError(() => recordStatusRemoteRefreshFailure(key)),+ Effect.tapCause((cause) => Effect.logWarning("Background Git fetch failed", cause)),+ statusFetchProcesses.withPermits(1),+ Effect.onExit(() =>+ Effect.sync(() => {+ statusFetches.delete(key);+ }),+ ),+ Effect.forkIn(serviceScope, { startImmediately: false, uninterruptible: false }),+ );+ statusFetches.set(key, started);+ return started;+ }),+ );+ const result = yield* Fiber.join(fiber).pipe(+ Effect.timeoutOption(STATUS_UPSTREAM_REFRESH_TIMEOUT),+ );+ if (Option.isNone(result)) {+ yield* Effect.logDebug(+ "Background Git fetch is still running; using the last fetched references.",+ );+ }+ });const resolveRepositoryPathsUncached = Effect.fn("resolveRepositoryPathsUncached")(function* (cwd: string,@@ -1468,31 +1514,30 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*});});- const statusRemoteRefreshFailureCounts = new Map<string, number>();+ const statusRemoteRefreshFailures = new Map<string, { count: number; retryAt: number }>();const statusRemoteRefreshFailureKey = (cacheKey: StatusRemoteRefreshCacheKey) =>`${cacheKey.gitCommonDir}\0${cacheKey.remoteName}`;- const recordStatusRemoteRefreshFailure = (cacheKey: StatusRemoteRefreshCacheKey) => {- const key = statusRemoteRefreshFailureKey(cacheKey);- const nextCount = (statusRemoteRefreshFailureCounts.get(key) ?? 0) + 1;- statusRemoteRefreshFailureCounts.delete(key);- statusRemoteRefreshFailureCounts.set(key, nextCount);- if (statusRemoteRefreshFailureCounts.size > STATUS_UPSTREAM_REFRESH_CACHE_CAPACITY) {- const oldestKey = statusRemoteRefreshFailureCounts.keys().next().value;+ const recordStatusRemoteRefreshFailure = Effect.fn(+ "GitVcsDriver.recordStatusRemoteRefreshFailure",+ )(function* (key: string) {+ const nextCount = (statusRemoteRefreshFailures.get(key)?.count ?? 0) + 1;+ const now = yield* Clock.currentTimeMillis;+ statusRemoteRefreshFailures.delete(key);+ statusRemoteRefreshFailures.set(key, {+ count: nextCount,+ retryAt: now + Duration.toMillis(statusUpstreamRefreshFailureCooldown(nextCount)),+ });+ if (statusRemoteRefreshFailures.size > STATUS_UPSTREAM_REFRESH_CACHE_CAPACITY) {+ const oldestKey = statusRemoteRefreshFailures.keys().next().value;if (oldestKey !== undefined) {- statusRemoteRefreshFailureCounts.delete(oldestKey);+ statusRemoteRefreshFailures.delete(oldestKey);}}- };- const clearStatusRemoteRefreshFailures = (cacheKey: StatusRemoteRefreshCacheKey) => {- statusRemoteRefreshFailureCounts.delete(statusRemoteRefreshFailureKey(cacheKey));- };+ });const refreshStatusRemoteCacheEntry = Effect.fn("refreshStatusRemoteCacheEntry")(function* (cacheKey: StatusRemoteRefreshCacheKey,) {return yield* fetchRemoteForStatus(cacheKey.gitCommonDir, cacheKey.remoteName).pipe(- Effect.tap(() => Effect.sync(() => clearStatusRemoteRefreshFailures(cacheKey))),- Effect.tapError(() => Effect.sync(() => recordStatusRemoteRefreshFailure(cacheKey))),- Effect.tapCause((cause) => Effect.logWarning("Background Git fetch failed", cause)),Effect.as(true as const),);});@@ -1507,7 +1552,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*Exit.isSuccess(exit)? STATUS_UPSTREAM_REFRESH_INTERVAL: statusUpstreamRefreshFailureCooldown(- statusRemoteRefreshFailureCounts.get(statusRemoteRefreshFailureKey(cacheKey)) ?? 1,+ statusRemoteRefreshFailures.get(statusRemoteRefreshFailureKey(cacheKey))?.count ?? 1,),});@@ -2594,7 +2639,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*const pullCurrentBranch: GitVcsDriver.GitVcsDriver["Service"]["pullCurrentBranch"] = Effect.fn("pullCurrentBranch",)(function* (cwd) {- const details = yield* statusDetails(cwd);+ const details = yield* readStatusDetailsLocal(cwd);const refName = details.branch;if (!refName) {return yield* new GitCommandError({@@ -2616,6 +2661,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*detail: "Current branch has no upstream configured. Push with upstream first.",});}+ const remote = yield* readConfigValue(cwd, `branch.${refName}.remote`);+ if (remote) {+ const gitCommonDir = yield* resolveGitCommonDir(cwd);+ const pending = statusFetches.get(`${gitCommonDir}\0${remote}`);+ // The status wait can finish while its fetch still owns reference locks.+ // Let our existing writer finish before starting the pull's fetch.+ if (pending) yield* Fiber.await(pending);+ }const beforeSha = yield* runGitStdout("GitVcsDriver.pullCurrentBranch.beforeSha",cwd,@@ -2625,11 +2678,67 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*// After a successful pull, HEAD before and after decides the result, so its output is not// needed. A large fast-forward's diffstat can exceed the output cap, which must not fail a// pull Git applied.- yield* executeGit("GitVcsDriver.pullCurrentBranch.pull", cwd, ["pull", "--ff-only"], {+ const options = {timeoutMs: 30_000,- fallbackErrorDetail: "git pull failed",appendTruncationMarker: true,- });+ allowNonZeroExit: true,+ };+ let args = ["pull", "--ff-only"];+ let result = yield* executeGitWithStableDiagnostics(+ "GitVcsDriver.pullCurrentBranch.pull",+ cwd,+ args,+ options,+ );+ if (result.exitCode !== 0 && isRefLockFailure(result.stderr)) {+ const [remote, mergeRefs, currentBranch, currentSha] = yield* Effect.all([+ readConfigValue(cwd, `branch.${refName}.remote`),+ runGitStdout(+ "GitVcsDriver.pullCurrentBranch.mergeRefs",+ cwd,+ ["config", "--get-all", `branch.${refName}.merge`],+ true,+ ).pipe(Effect.map((stdout) => stdout.trim().split("\n").filter(Boolean))),+ runGitStdout(+ "GitVcsDriver.pullCurrentBranch.currentBranch",+ cwd,+ ["symbolic-ref", "--quiet", "--short", "HEAD"],+ true,+ ).pipe(Effect.map((stdout) => stdout.trim())),+ runGitStdout("GitVcsDriver.pullCurrentBranch.currentSha", cwd, ["rev-parse", "HEAD"]).pipe(+ Effect.map((stdout) => stdout.trim()),+ ),+ ]);+ // A full fetch can fail on an unrelated branch's lock. Keep that fetch as the+ // default, but retry only the configured upstream before any checkout movement.+ const [mergeRef] = mergeRefs;+ if (+ remote &&+ mergeRef &&+ mergeRefs.length === 1 &&+ currentBranch === refName &&+ currentSha === beforeSha+ ) {+ args = ["pull", "--ff-only", "--", remote, mergeRef];+ result = yield* executeGitWithStableDiagnostics(+ "GitVcsDriver.pullCurrentBranch.pull",+ cwd,+ args,+ options,+ );+ }+ }+ if (result.exitCode !== 0) {+ const reason = classifyGitFailure(result.stderr);+ return yield* new GitCommandError({+ ...gitCommandContext({ operation: "GitVcsDriver.pullCurrentBranch.pull", cwd, args }),+ ...(reason === null ? {} : { reason }),+ detail: fetchFailureDetail(result.stderr) ?? "git pull failed",+ exitCode: result.exitCode,+ stdoutLength: result.stdout.length,+ stderrLength: result.stderr.length,+ });+ }const afterSha = yield* runGitStdout("GitVcsDriver.pullCurrentBranch.afterSha",cwd,@@ -2637,7 +2746,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*true,).pipe(Effect.map((stdout) => stdout.trim()));- const refreshed = yield* statusDetails(cwd);+ const refreshed = yield* readStatusDetailsLocal(cwd);return {status: beforeSha.length > 0 && beforeSha === afterSha ? "skipped_up_to_date" : "pulled",refName,