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, 09:45 UTC
fix(server): a timed-out branch totals diff is not rerun until HEAD or its base moves (#37455)
Julius Marminge committed
Nov 15, 2026 at 19:36 UTC
94c0784d7c174156ea64dd24e582b36c9d3ba1df
2 files changed
+139
-2
apps/server/src/vcs/GitVcsDriverCore.test.ts
+82
-1
@@ -9744,7 +9744,7 @@ describe("Windows long path configuration", () => {
9744
});
9745
});
9746
9747
-it.layer(layerTest)("branch change totals after a failed untracked listing", (it) => {
9747
+it.layer(layerTest)("branch change totals after a failed read", (it) => {
9748
it.effect("do not list untracked files again on every status read", () =>
9749
Effect.gen(function* () {
9750
const fs = yield* FileSystem.FileSystem;
@@ -9799,4 +9799,85 @@ it.layer(layerTest)("branch change totals after a failed untracked listing", (it
9799
assert.deepStrictEqual(status.branchChanges, { baseRef: null, insertions: 2, deletions: 0 });
9800
}),
9801
);
9802
+
9803
+ it.effect("back off a timed-out branch diff until it expires or HEAD moves", () =>
9804
+ Effect.gen(function* () {
9805
+ const cwd = yield* makeTmpDir("git-branch-totals-timeout-");
9806
+ yield* initRepoWithCommit(cwd);
9807
+ yield* git(cwd, ["branch", "-M", "main"]);
9808
+ yield* git(cwd, ["checkout", "-b", "feature"]);
9809
+ yield* writeTextFile(cwd, "feature.txt", "one\n");
9810
+ yield* git(cwd, ["add", "feature.txt"]);
9811
+ yield* git(cwd, ["commit", "-m", "feature"]);
9812
+
9813
+ let diffs = 0;
9814
+ let hang = true;
9815
+ const diffStarted = yield* Queue.unbounded<void>();
9816
+ const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
9817
+ const spawner = ChildProcessSpawner.make((command) =>
9818
+ Effect.suspend(() => {
9819
+ // Status diffs the working tree against HEAD; the branch totals diff the merge base.
9820
+ if (
9821
+ !ChildProcess.isStandardCommand(command) ||
9822
+ !command.args.includes("diff-index") ||
9823
+ command.args.includes("HEAD")
9824
+ ) {
9825
+ return delegate.spawn(command);
9826
+ }
9827
+ diffs++;
9828
+ if (!hang) return delegate.spawn(command);
9829
+ return Queue.offer(diffStarted, undefined).pipe(
9830
+ Effect.as(
9831
+ ChildProcessSpawner.makeHandle({
9832
+ ...makeSuccessfulHandle(""),
9833
+ isRunning: Effect.succeed(true),
9834
+ exitCode: Effect.never,
9835
+ }),
9836
+ ),
9837
+ );
9838
+ }),
9839
+ );
9840
+ const driver = yield* makeGitVcsDriverCore().pipe(
9841
+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
9842
+ Effect.provide(layerServerConfig),
9843
+ );
9844
+ const read = driver.statusDetailsLocal(cwd, { includeBranchChanges: true });
9845
+
9846
+ const first = yield* read.pipe(Effect.forkChild);
9847
+ yield* Queue.take(diffStarted);
9848
+ yield* TestClock.adjust("30 seconds");
9849
+ assert.strictEqual((yield* Fiber.join(first)).branchChanges, undefined);
9850
+ assert.strictEqual(diffs, 1);
9851
+ assert.strictEqual((yield* read).branchChanges, undefined);
9852
+ assert.strictEqual(diffs, 1, "the same HEAD and base are not diffed within the backoff");
9853
+
9854
+ hang = false;
9855
+ yield* TestClock.adjust("1 minute");
9856
+ assert.deepStrictEqual((yield* read).branchChanges, {
9857
+ baseRef: "main",
9858
+ insertions: 1,
9859
+ deletions: 0,
9860
+ });
9861
+ assert.strictEqual(diffs, 2, "it is retried once the backoff passes");
9862
+
9863
+ hang = true;
9864
+ const again = yield* read.pipe(Effect.forkChild);
9865
+ yield* Queue.take(diffStarted);
9866
+ yield* TestClock.adjust("30 seconds");
9867
+ yield* Fiber.join(again);
9868
+ yield* read;
9869
+ assert.strictEqual(diffs, 3);
9870
+
9871
+ hang = false;
9872
+ yield* writeTextFile(cwd, "feature.txt", "one\ntwo\n");
9873
+ yield* git(cwd, ["commit", "-am", "more"]);
9874
+ const status = yield* read;
9875
+ assert.strictEqual(diffs, 4, "a new HEAD diffs again within the backoff");
9876
+ assert.deepStrictEqual(status.branchChanges, {
9877
+ baseRef: "main",
9878
+ insertions: 2,
9879
+ deletions: 0,
9880
+ });
9881
+ }),
9882
+ );
9883
});
apps/server/src/vcs/GitVcsDriverCore.ts
+57
-1
@@ -1736,6 +1736,16 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1736
readonly detail: string;
1737
}
1738
>();
1739
+ // Keyed by cwd: the HEAD and merge base whose totals diff failed, retried after a backoff.
1740
+ const branchTotalsDiffFailures = new Map<
1741
+ string,
1742
+ {
1743
+ readonly key: string;
1744
+ readonly count: number;
1745
+ readonly retryAt: number;
1746
+ readonly detail: string;
1747
+ }
1748
+ >();
1749
const statusRemoteRefreshFailureKey = (cacheKey: StatusRemoteRefreshCacheKey) =>
1750
`${cacheKey.gitCommonDir}\0${cacheKey.remoteName}`;
1751
const recordStatusRemoteRefreshFailure = Effect.fn(
@@ -2484,6 +2494,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2494
// and also for an unborn HEAD, which can still track a fetched upstream.
2495
let upstreamRefExists = false;
2496
let isUnbornHead = false;
2497
+ let headOid: string | null = null;
2498
let aheadCount = 0;
2499
let behindCount = 0;
2500
let aheadOfDefaultCount = 0;
@@ -2498,6 +2509,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2509
isUnbornHead = true;
2510
continue;
2511
}
2512
+ if (line.startsWith("# branch.oid ")) {
2513
+ headOid = line.slice("# branch.oid ".length).trim();
2514
+ continue;
2515
+ }
2516
if (line.startsWith("# branch.head ")) {
2517
const value = line.slice("# branch.head ".length).trim();
2518
// `(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*
2568
? readBranchChangeTotals(repositoryPaths?.worktreeRoot ?? cwd, refName, {
2569
knownOriginDefaultBranch: defaultBranch,
2570
untrackedRoots,
2571
+ headOid,
2572
}).pipe(Effect.orElseSucceed(() => undefined))
2573
: Effect.undefined,
2574
],
@@ -3847,6 +3863,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3863
options?: {
3864
readonly knownOriginDefaultBranch: string | null;
3865
readonly untrackedRoots: ReadonlyArray<string>;
3866
+ readonly headOid: string | null;
3867
},
3868
) {
3869
const resolved = yield* resolveReviewMergeBase(
@@ -3860,6 +3877,21 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3877
branch === null || (yield* branchExists(cwd, branch))
3878
? resolved
3879
: { baseRef: null, mergeBase: "HEAD" };
3880
+ // A moved HEAD or merge base skips the backoff and diffs again.
3881
+ const diffKey = options?.headOid ? `${options.headOid}\0${mergeBase}` : null;
3882
+ const diffFailure = branchTotalsDiffFailures.get(cwd);
3883
+ if (
3884
+ diffFailure !== undefined &&
3885
+ diffFailure.key === diffKey &&
3886
+ (yield* Clock.currentTimeMillis) < diffFailure.retryAt
3887
+ ) {
3888
+ return yield* new GitCommandError({
3889
+ operation: "GitVcsDriver.readBranchChangeTotals",
3890
+ command: "git diff --numstat",
3891
+ cwd,
3892
+ detail: diffFailure.detail,
3893
+ });
3894
+ }
3895
const roots = options?.untrackedRoots ?? [];
3896
// Bound pathspec matching work and stay below Windows' command-line limit.
3897
const pathspecs =
@@ -3921,7 +3953,31 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3953
env: untracked.env,
3954
},
3955
);
3924
- let result = yield* readNumstat(mergeBase);
3956
+ let result = yield* readNumstat(mergeBase).pipe(
3957
+ Effect.tapError((error) =>
3958
+ Effect.gen(function* () {
3959
+ if (diffKey === null) return;
3960
+ const count = diffFailure?.key === diffKey ? diffFailure.count + 1 : 1;
3961
+ branchTotalsDiffFailures.delete(cwd);
3962
+ branchTotalsDiffFailures.set(cwd, {
3963
+ key: diffKey,
3964
+ count,
3965
+ retryAt:
3966
+ (yield* Clock.currentTimeMillis) +
3967
+ Math.min(
3968
+ UNTRACKED_TOTALS_RETRY_MIN_MS * 2 ** (count - 1),
3969
+ UNTRACKED_TOTALS_RETRY_MAX_MS,
3970
+ ),
3971
+ detail: error.detail,
3972
+ });
3973
+ if (branchTotalsDiffFailures.size > STATUS_UPSTREAM_REFRESH_CACHE_CAPACITY) {
3974
+ const oldestKey = branchTotalsDiffFailures.keys().next().value;
3975
+ if (oldestKey !== undefined) branchTotalsDiffFailures.delete(oldestKey);
3976
+ }
3977
+ }),
3978
+ ),
3979
+ );
3980
+ branchTotalsDiffFailures.delete(cwd);
3981
if (result.exitCode !== 0 && mergeBase === "HEAD" && isUnbornHeadStderr(result.stderr)) {
3982
result = yield* readNumstat(yield* readEmptyTreeHash(cwd));
3983
}