diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index 9e5b20205fefb..efaf9826408cd 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -9744,7 +9744,7 @@ describe("Windows long path configuration", () => { }); }); -it.layer(layerTest)("branch change totals after a failed untracked listing", (it) => { +it.layer(layerTest)("branch change totals after a failed read", (it) => { it.effect("do not list untracked files again on every status read", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; @@ -9799,4 +9799,85 @@ it.layer(layerTest)("branch change totals after a failed untracked listing", (it assert.deepStrictEqual(status.branchChanges, { baseRef: null, insertions: 2, deletions: 0 }); }), ); + + it.effect("back off a timed-out branch diff until it expires or HEAD moves", () => + Effect.gen(function* () { + const cwd = yield* makeTmpDir("git-branch-totals-timeout-"); + yield* initRepoWithCommit(cwd); + yield* git(cwd, ["branch", "-M", "main"]); + yield* git(cwd, ["checkout", "-b", "feature"]); + yield* writeTextFile(cwd, "feature.txt", "one\n"); + yield* git(cwd, ["add", "feature.txt"]); + yield* git(cwd, ["commit", "-m", "feature"]); + + let diffs = 0; + let hang = true; + const diffStarted = yield* Queue.unbounded(); + const delegate = yield* ChildProcessSpawner.ChildProcessSpawner; + const spawner = ChildProcessSpawner.make((command) => + Effect.suspend(() => { + // Status diffs the working tree against HEAD; the branch totals diff the merge base. + if ( + !ChildProcess.isStandardCommand(command) || + !command.args.includes("diff-index") || + command.args.includes("HEAD") + ) { + return delegate.spawn(command); + } + diffs++; + if (!hang) return delegate.spawn(command); + return Queue.offer(diffStarted, undefined).pipe( + Effect.as( + ChildProcessSpawner.makeHandle({ + ...makeSuccessfulHandle(""), + isRunning: Effect.succeed(true), + exitCode: Effect.never, + }), + ), + ); + }), + ); + const driver = yield* makeGitVcsDriverCore().pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + Effect.provide(layerServerConfig), + ); + const read = driver.statusDetailsLocal(cwd, { includeBranchChanges: true }); + + const first = yield* read.pipe(Effect.forkChild); + yield* Queue.take(diffStarted); + yield* TestClock.adjust("30 seconds"); + assert.strictEqual((yield* Fiber.join(first)).branchChanges, undefined); + assert.strictEqual(diffs, 1); + assert.strictEqual((yield* read).branchChanges, undefined); + assert.strictEqual(diffs, 1, "the same HEAD and base are not diffed within the backoff"); + + hang = false; + yield* TestClock.adjust("1 minute"); + assert.deepStrictEqual((yield* read).branchChanges, { + baseRef: "main", + insertions: 1, + deletions: 0, + }); + assert.strictEqual(diffs, 2, "it is retried once the backoff passes"); + + hang = true; + const again = yield* read.pipe(Effect.forkChild); + yield* Queue.take(diffStarted); + yield* TestClock.adjust("30 seconds"); + yield* Fiber.join(again); + yield* read; + assert.strictEqual(diffs, 3); + + hang = false; + yield* writeTextFile(cwd, "feature.txt", "one\ntwo\n"); + yield* git(cwd, ["commit", "-am", "more"]); + const status = yield* read; + assert.strictEqual(diffs, 4, "a new HEAD diffs again within the backoff"); + assert.deepStrictEqual(status.branchChanges, { + baseRef: "main", + insertions: 2, + deletions: 0, + }); + }), + ); }); diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index da1794188b0b2..89bb1fa039ddd 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -1736,6 +1736,16 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* readonly detail: string; } >(); + // Keyed by cwd: the HEAD and merge base whose totals diff failed, retried after a backoff. + const branchTotalsDiffFailures = new Map< + string, + { + readonly key: string; + readonly count: number; + readonly retryAt: number; + readonly detail: string; + } + >(); const statusRemoteRefreshFailureKey = (cacheKey: StatusRemoteRefreshCacheKey) => `${cacheKey.gitCommonDir}\0${cacheKey.remoteName}`; const recordStatusRemoteRefreshFailure = Effect.fn( @@ -2484,6 +2494,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* // and also for an unborn HEAD, which can still track a fetched upstream. let upstreamRefExists = false; let isUnbornHead = false; + let headOid: string | null = null; let aheadCount = 0; let behindCount = 0; let aheadOfDefaultCount = 0; @@ -2498,6 +2509,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* isUnbornHead = true; continue; } + if (line.startsWith("# branch.oid ")) { + headOid = line.slice("# branch.oid ".length).trim(); + continue; + } if (line.startsWith("# branch.head ")) { const value = line.slice("# branch.head ".length).trim(); // `(unknown)` is printed while HEAD names no resolvable branch, e.g. mid-`git clone`. @@ -2553,6 +2568,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ? readBranchChangeTotals(repositoryPaths?.worktreeRoot ?? cwd, refName, { knownOriginDefaultBranch: defaultBranch, untrackedRoots, + headOid, }).pipe(Effect.orElseSucceed(() => undefined)) : Effect.undefined, ], @@ -3847,6 +3863,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* options?: { readonly knownOriginDefaultBranch: string | null; readonly untrackedRoots: ReadonlyArray; + readonly headOid: string | null; }, ) { const resolved = yield* resolveReviewMergeBase( @@ -3860,6 +3877,21 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* branch === null || (yield* branchExists(cwd, branch)) ? resolved : { baseRef: null, mergeBase: "HEAD" }; + // A moved HEAD or merge base skips the backoff and diffs again. + const diffKey = options?.headOid ? `${options.headOid}\0${mergeBase}` : null; + const diffFailure = branchTotalsDiffFailures.get(cwd); + if ( + diffFailure !== undefined && + diffFailure.key === diffKey && + (yield* Clock.currentTimeMillis) < diffFailure.retryAt + ) { + return yield* new GitCommandError({ + operation: "GitVcsDriver.readBranchChangeTotals", + command: "git diff --numstat", + cwd, + detail: diffFailure.detail, + }); + } const roots = options?.untrackedRoots ?? []; // Bound pathspec matching work and stay below Windows' command-line limit. const pathspecs = @@ -3921,7 +3953,31 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* env: untracked.env, }, ); - let result = yield* readNumstat(mergeBase); + let result = yield* readNumstat(mergeBase).pipe( + Effect.tapError((error) => + Effect.gen(function* () { + if (diffKey === null) return; + const count = diffFailure?.key === diffKey ? diffFailure.count + 1 : 1; + branchTotalsDiffFailures.delete(cwd); + branchTotalsDiffFailures.set(cwd, { + key: diffKey, + count, + retryAt: + (yield* Clock.currentTimeMillis) + + Math.min( + UNTRACKED_TOTALS_RETRY_MIN_MS * 2 ** (count - 1), + UNTRACKED_TOTALS_RETRY_MAX_MS, + ), + detail: error.detail, + }); + if (branchTotalsDiffFailures.size > STATUS_UPSTREAM_REFRESH_CACHE_CAPACITY) { + const oldestKey = branchTotalsDiffFailures.keys().next().value; + if (oldestKey !== undefined) branchTotalsDiffFailures.delete(oldestKey); + } + }), + ), + ); + branchTotalsDiffFailures.delete(cwd); if (result.exitCode !== 0 && mergeBase === "HEAD" && isUnbornHeadStderr(result.stderr)) { result = yield* readNumstat(yield* readEmptyTreeHash(cwd)); }