From 1d152601a5b3540bb5acf693a2fa685e289aace6 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 23:53:25 +0000 Subject: [PATCH 1/5] test(server): a branch's base is resolved once while refs are unchanged Repro for #39984: on main, two local status reads and the two diff preview passes resolve the base branch four times on an unchanged repository. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/vcs/GitVcsDriverCore.test.ts | 41 ++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index c51f6fef60260..e56591ba7954a 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -876,6 +876,47 @@ it.effect.each(["on a branch", "detached", "unborn"] as const)( ).pipe(Effect.provide(layerTest)), ); +it.effect("resolves a branch's base once while its refs and branch config are unchanged", () => + Effect.scoped( + Effect.gen(function* () { + const delegate = yield* ChildProcessSpawner.ChildProcessSpawner; + const spawnedArgs: Array> = []; + const spawner = ChildProcessSpawner.make((command) => { + if (ChildProcess.isStandardCommand(command)) spawnedArgs.push(command.args.map(String)); + return delegate.spawn(command); + }); + const driver = yield* makeGitVcsDriverCore().pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + ); + const cwd = yield* makeTmpDir(); + yield* initRepoWithCommit(cwd); + yield* git(cwd, ["checkout", "-b", "feature/x"]); + yield* writeTextFile(cwd, "feature.txt", "feature\n"); + yield* git(cwd, ["add", "."]); + yield* git(cwd, ["commit", "-m", "feature"]); + const lookups = () => { + const count = spawnedArgs.filter((args) => + args.includes("branch.feature/x.t3-merge-base-ref"), + ).length; + spawnedArgs.length = 0; + return count; + }; + spawnedArgs.length = 0; + + // One thread switch: two local status reads and the two diff preview passes. + yield* driver.statusDetailsLocal(cwd); + yield* driver.statusDetailsLocal(cwd); + yield* driver.getReviewDiffPreview({ cwd, trackedOnly: true }); + yield* driver.getReviewDiffPreview({ cwd }); + assert.equal(lookups(), 1); + + yield* git(cwd, ["config", "branch.feature/x.t3-merge-base-ref", "refs/heads/main"]); + yield* driver.statusDetailsLocal(cwd); + assert.equal(lookups(), 1); + }), + ).pipe(Effect.provide(layerTest)), +); + it.effect.each([ { kind: "working-tree", request: { sourceKind: "working-tree" } }, { kind: "commit", request: { sourceKind: "commit", commitSha: "HEAD" } }, From 9f996afa6bd92c95ad44c2f5d7ef3c715113f9ce Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 21 Nov 2026 01:03:31 +0000 Subject: [PATCH 2/5] perf(server): resolve a branch's base once while its refs and config are unchanged A thread switch resolved the same base branch five times, each a chain of up to seven serial git processes. Cache the answer per cwd, branch and options, keyed by a stat stamp of the repository config, packed-refs, reftable list and the loose ref directories, so any ref or config change, from T3 or outside it, misses the cache. A lookup that never met the branch itself as a candidate answers both allowRemoteOfCurrent modes. Fixes pingdotgg/t3code#39984 Co-Authored-By: Claude Opus 5.5 --- apps/server/src/vcs/GitVcsDriverCore.ts | 120 +++++++++++++++++++++++- 1 file changed, 118 insertions(+), 2 deletions(-) diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index a4bde8c983817..cb6a83aa59423 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -137,6 +137,7 @@ const LIST_REFS_REFRESH_COALESCE_TTL = Duration.seconds(5); const LIST_REFS_REFRESH_FAILURE_COOLDOWN = Duration.seconds(30); const STATUS_DEFAULT_BRANCH_CACHE_TTL = Duration.minutes(5); const STATUS_ORIGIN_EXISTS_CACHE_TTL = Duration.minutes(5); +const BASE_BRANCH_CACHE_TTL = Duration.minutes(5); const STATUS_UPSTREAM_REFRESH_ENV = Object.freeze({ GCM_INTERACTIVE: "never", GIT_ASKPASS: "", @@ -188,6 +189,14 @@ function statusUpstreamRefreshFailureCooldown(consecutiveFailures: number): Dura return Duration.min(Duration.millis(cooldownMs), STATUS_UPSTREAM_REFRESH_FAILURE_MAX_COOLDOWN); } +class BaseBranchCacheKey extends Data.Class<{ + cwd: string; + refName: string; + allowRemoteOfCurrent: boolean; + knownOriginDefaultBranch: string | null; + inputsStamp: string; +}> {} + class GitRefsSnapshotCacheKey extends Data.Class<{ gitCommonDir: string; epoch: number; @@ -2245,13 +2254,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* return { baseRef: comparisonRef.slice("refs/remotes/".length), comparisonRef }; }); - const resolveBaseBranchForNoUpstream = Effect.fn("resolveBaseBranchForNoUpstream")(function* ( + const findBaseBranchForNoUpstream = Effect.fn("resolveBaseBranchForNoUpstream")(function* ( cwd: string, refName: string, options?: { readonly allowRemoteOfCurrent?: boolean; readonly knownOriginDefaultBranch?: string | null; }, + trace?: { metCurrentBranch: boolean }, ) { const configuredBaseRef = yield* readConfigValue(cwd, `branch.${refName}.t3-merge-base-ref`); if ( @@ -2315,6 +2325,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* continue; } if (normalizedCandidate === refName) { + if (trace) trace.metCurrentBranch = true; if (options?.allowRemoteOfCurrent) { const trackedRemoteBase = yield* resolveTrackedRemoteBaseRef(cwd, refName); if (trackedRemoteBase !== null) return trackedRemoteBase; @@ -2350,13 +2361,118 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* } if (yield* branchExists(cwd, normalizedCandidate)) { - return { baseRef: normalizedCandidate, comparisonRef: `refs/heads/${normalizedCandidate}` }; + return { + baseRef: normalizedCandidate, + comparisonRef: `refs/heads/${normalizedCandidate}`, + }; } } return null; }); + // Git rewrites config and packed-refs by rename and writes each loose ref by a rename in its + // directory, so these stats change whenever a base-branch answer could. Cost: one stat and one + // listing per directory under refs/heads and refs/remotes. + const readBaseBranchInputsStamp = Effect.fn("GitVcsDriverCore.readBaseBranchInputsStamp")( + function* (gitCommonDir: string) { + const statToken = (target: string) => + fileSystem.stat(target).pipe( + Effect.map( + (info) => + `${Option.getOrElse(info.ino, () => "")}:${info.size}:${Option.match(info.mtime, { + onNone: () => "", + onSome: (mtime) => mtime.getTime(), + })}`, + ), + Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed("-")), + ); + const tokens = yield* Effect.forEach( + ["config", "packed-refs", "reftable/tables.list"], + (name) => statToken(path.join(gitCommonDir, name)), + { concurrency: "unbounded" }, + ); + const directories = [ + path.join(gitCommonDir, "refs", "heads"), + path.join(gitCommonDir, "refs", "remotes"), + ]; + for (let index = 0; index < directories.length; index++) { + const directory = directories[index]!; + tokens.push(`${directory}=${yield* statToken(directory)}`); + const entries = yield* fileSystem + .readDirectoryEntries(directory) + .pipe(Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed([]))); + for (const entry of entries.toSorted((a, b) => a.name.localeCompare(b.name))) { + if (entry.type === "Directory") directories.push(path.join(directory, entry.name)); + } + } + return tokens.join("\0"); + }, + ); + const baseBranchCache = yield* Cache.makeWith( + Effect.fnUntraced(function* (key: BaseBranchCacheKey) { + const trace = { metCurrentBranch: false }; + const base = yield* findBaseBranchForNoUpstream( + key.cwd, + key.refName, + { + allowRemoteOfCurrent: key.allowRemoteOfCurrent, + knownOriginDefaultBranch: key.knownOriginDefaultBranch, + }, + trace, + ); + return { base, dependsOnRemoteOfCurrent: trace.metCurrentBranch }; + }), + { + capacity: 2_048, + timeToLive: Exit.match({ + onSuccess: () => BASE_BRANCH_CACHE_TTL, + onFailure: () => Duration.zero, + }), + }, + ); + const resolveBaseBranchForNoUpstream = Effect.fn("resolveBaseBranchForNoUpstream.cached")( + function* ( + cwd: string, + refName: string, + options?: { + readonly allowRemoteOfCurrent?: boolean; + readonly knownOriginDefaultBranch?: string | null; + }, + ) { + const gitCommonDir = (yield* resolveRepositoryPaths(cwd).pipe( + Effect.orElseSucceed(() => null), + ))?.gitCommonDir; + const inputsStamp = + gitCommonDir === undefined + ? null + : yield* readBaseBranchInputsStamp(gitCommonDir).pipe(Effect.orElseSucceed(() => null)); + if (inputsStamp === null) { + return yield* findBaseBranchForNoUpstream(cwd, refName, options); + } + const key = { + cwd: normalizeRepositoryPathsCacheKey(cwd), + refName, + knownOriginDefaultBranch: options?.knownOriginDefaultBranch ?? null, + inputsStamp, + }; + const allowRemoteOfCurrent = options?.allowRemoteOfCurrent === true; + const entry = yield* Cache.get( + baseBranchCache, + new BaseBranchCacheKey({ ...key, allowRemoteOfCurrent }), + ); + // allowRemoteOfCurrent only matters once a candidate is the branch itself. + if (!entry.dependsOnRemoteOfCurrent) { + yield* Cache.set( + baseBranchCache, + new BaseBranchCacheKey({ ...key, allowRemoteOfCurrent: !allowRemoteOfCurrent }), + entry, + ); + } + return entry.base; + }, + ); + const computeAheadCountAgainstBase = Effect.fn("computeAheadCountAgainstBase")(function* ( cwd: string, refName: string, From b35c1f262b7f25dab42a57cb923d717d65488736 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 21 Nov 2026 01:29:07 +0000 Subject: [PATCH 3/5] perf(server): share one base-branch answer between status and diff preview Status passed its cached origin default branch and the diff preview did not, so the same branch was cached twice. The cache makes that shortcut moot: the lookup reads origin/HEAD itself, which the stamp covers. A lookup that checked the remote of the branch itself and found none now also answers the mode that skips it. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/vcs/GitVcsDriverCore.ts | 55 +++++++------------------ 1 file changed, 15 insertions(+), 40 deletions(-) diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index cb6a83aa59423..33148f62a9d5b 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -193,7 +193,6 @@ class BaseBranchCacheKey extends Data.Class<{ cwd: string; refName: string; allowRemoteOfCurrent: boolean; - knownOriginDefaultBranch: string | null; inputsStamp: string; }> {} @@ -2257,11 +2256,8 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const findBaseBranchForNoUpstream = Effect.fn("resolveBaseBranchForNoUpstream")(function* ( cwd: string, refName: string, - options?: { - readonly allowRemoteOfCurrent?: boolean; - readonly knownOriginDefaultBranch?: string | null; - }, - trace?: { metCurrentBranch: boolean }, + options?: { readonly allowRemoteOfCurrent?: boolean }, + trace?: { dependsOnRemoteOfCurrent: boolean }, ) { const configuredBaseRef = yield* readConfigValue(cwd, `branch.${refName}.t3-merge-base-ref`); if ( @@ -2302,9 +2298,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* Effect.succeed(configuredBaseBranch), primaryRemoteName === null ? Effect.succeed(null) - : primaryRemoteName === "origin" && options?.knownOriginDefaultBranch != null - ? Effect.succeed(options.knownOriginDefaultBranch) - : resolveDefaultBranchName(cwd, primaryRemoteName), + : resolveDefaultBranchName(cwd, primaryRemoteName), ...DEFAULT_BASE_BRANCH_CANDIDATES.map(Effect.succeed), ]; @@ -2325,10 +2319,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* continue; } if (normalizedCandidate === refName) { - if (trace) trace.metCurrentBranch = true; + // Without allowRemoteOfCurrent the answer it would have given here is unknown. + if (trace) trace.dependsOnRemoteOfCurrent = true; if (options?.allowRemoteOfCurrent) { const trackedRemoteBase = yield* resolveTrackedRemoteBaseRef(cwd, refName); if (trackedRemoteBase !== null) return trackedRemoteBase; + if (trace) trace.dependsOnRemoteOfCurrent = false; if ( primaryRemoteName && (yield* remoteBranchExists({ @@ -2337,6 +2333,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* refName: normalizedCandidate, })) ) { + if (trace) trace.dependsOnRemoteOfCurrent = true; return { baseRef: `${primaryRemoteName}/${normalizedCandidate}`, comparisonRef: `refs/remotes/${primaryRemoteName}/${normalizedCandidate}`, @@ -2411,17 +2408,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ); const baseBranchCache = yield* Cache.makeWith( Effect.fnUntraced(function* (key: BaseBranchCacheKey) { - const trace = { metCurrentBranch: false }; + const trace = { dependsOnRemoteOfCurrent: false }; const base = yield* findBaseBranchForNoUpstream( key.cwd, key.refName, - { - allowRemoteOfCurrent: key.allowRemoteOfCurrent, - knownOriginDefaultBranch: key.knownOriginDefaultBranch, - }, + { allowRemoteOfCurrent: key.allowRemoteOfCurrent }, trace, ); - return { base, dependsOnRemoteOfCurrent: trace.metCurrentBranch }; + return { base, dependsOnRemoteOfCurrent: trace.dependsOnRemoteOfCurrent }; }), { capacity: 2_048, @@ -2435,10 +2429,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* function* ( cwd: string, refName: string, - options?: { - readonly allowRemoteOfCurrent?: boolean; - readonly knownOriginDefaultBranch?: string | null; - }, + options?: { readonly allowRemoteOfCurrent?: boolean }, ) { const gitCommonDir = (yield* resolveRepositoryPaths(cwd).pipe( Effect.orElseSucceed(() => null), @@ -2453,7 +2444,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const key = { cwd: normalizeRepositoryPathsCacheKey(cwd), refName, - knownOriginDefaultBranch: options?.knownOriginDefaultBranch ?? null, inputsStamp, }; const allowRemoteOfCurrent = options?.allowRemoteOfCurrent === true; @@ -2461,7 +2451,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* baseBranchCache, new BaseBranchCacheKey({ ...key, allowRemoteOfCurrent }), ); - // allowRemoteOfCurrent only matters once a candidate is the branch itself. if (!entry.dependsOnRemoteOfCurrent) { yield* Cache.set( baseBranchCache, @@ -2476,9 +2465,8 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const computeAheadCountAgainstBase = Effect.fn("computeAheadCountAgainstBase")(function* ( cwd: string, refName: string, - options?: { readonly knownOriginDefaultBranch: string | null }, ) { - const base = yield* resolveBaseBranchForNoUpstream(cwd, refName, options); + const base = yield* resolveBaseBranchForNoUpstream(cwd, refName); if (!base) { return 0; } @@ -2579,9 +2567,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* branch && !isDefaultBranch ? upstreamRef === null ? aheadCount - : yield* computeAheadCountAgainstBase(cwd, branch, { - knownOriginDefaultBranch: defaultBranch, - }).pipe(Effect.orElseSucceed(() => 0)) + : yield* computeAheadCountAgainstBase(cwd, branch).pipe(Effect.orElseSucceed(() => 0)) : 0; return { @@ -2865,13 +2851,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const [baseAheadCount, branchChanges] = yield* Effect.all( [ refName !== null && (needsFallbackAhead || needsAheadOfDefault) - ? computeAheadCountAgainstBase(cwd, refName, { - knownOriginDefaultBranch: defaultBranch, - }).pipe(Effect.orElseSucceed(() => 0)) + ? computeAheadCountAgainstBase(cwd, refName).pipe(Effect.orElseSucceed(() => 0)) : Effect.succeed(0), options?.includeBranchChanges ? readBranchChangeTotals(repositoryPaths?.worktreeRoot ?? cwd, refName, { - knownOriginDefaultBranch: defaultBranch, untrackedRoots, headOid, ...(index ? { indexPath: index } : {}), @@ -4109,7 +4092,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* cwd: string, branch: string | null, explicitBaseRef?: string, - knownOriginDefaultBranch: string | null = null, ) { const base = explicitBaseRef !== undefined @@ -4123,7 +4105,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* : branch ? yield* resolveBaseBranchForNoUpstream(cwd, branch, { allowRemoteOfCurrent: true, - knownOriginDefaultBranch, }).pipe(Effect.orElseSucceed(() => null)) : null; if (base === null) return { baseRef: null, mergeBase: "HEAD" }; @@ -4156,18 +4137,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* cwd: string, branch: string | null, options?: { - readonly knownOriginDefaultBranch: string | null; readonly untrackedRoots: ReadonlyArray; readonly headOid: string | null; readonly indexPath?: string; }, ) { - const resolved = yield* resolveReviewMergeBase( - cwd, - branch, - undefined, - options?.knownOriginDefaultBranch ?? null, - ); + const resolved = yield* resolveReviewMergeBase(cwd, branch); // A rename after `git status` read HEAD moves the base config away, so the base was guessed. const { baseRef, mergeBase } = branch === null || (yield* branchExists(cwd, branch)) From 938fa6a776e50e384b7c58769b3fdbbd06082546 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 21 Nov 2026 02:59:21 +0000 Subject: [PATCH 4/5] fix(server): base-branch cache sees included and global git config The cache stamp watched only the repository config file, so a branch base set through include.path, global, system or worktree config stayed stale. Stamp the effective config listing instead. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/vcs/GitVcsDriverCore.test.ts | 15 ++++++++++++ apps/server/src/vcs/GitVcsDriverCore.ts | 25 +++++++++++++++----- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index e56591ba7954a..0b1c61069eae7 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -913,6 +913,21 @@ it.effect("resolves a branch's base once while its refs and branch config are un yield* git(cwd, ["config", "branch.feature/x.t3-merge-base-ref", "refs/heads/main"]); yield* driver.statusDetailsLocal(cwd); assert.equal(lookups(), 1); + + const includeDir = yield* makeTmpDir(); + const includedConfig = `${includeDir}/included.gitconfig`; + yield* git(cwd, ["config", "include.path", includedConfig]); + yield* driver.statusDetailsLocal(cwd); + lookups(); + yield* git(cwd, [ + "config", + "--file", + includedConfig, + "branch.feature/x.t3-merge-base", + "main", + ]); + yield* driver.statusDetailsLocal(cwd); + assert.equal(lookups(), 1); }), ).pipe(Effect.provide(layerTest)), ); diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index 33148f62a9d5b..ef6895185c945 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -2368,11 +2368,21 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* return null; }); - // Git rewrites config and packed-refs by rename and writes each loose ref by a rename in its - // directory, so these stats change whenever a base-branch answer could. Cost: one stat and one - // listing per directory under refs/heads and refs/remotes. + // The effective config listing covers included, global, system and worktree config. Git writes + // packed-refs by rename and each loose ref by a rename in its directory, so those stats change + // whenever a ref could. Cost: one git process, plus a stat and listing per refs directory. const readBaseBranchInputsStamp = Effect.fn("GitVcsDriverCore.readBaseBranchInputsStamp")( - function* (gitCommonDir: string) { + function* (cwd: string, gitCommonDir: string) { + const effectiveConfig = yield* executeGit( + "GitVcsDriver.readBaseBranchInputsStamp", + cwd, + ["config", "--list", "-z"], + { allowNonZeroExit: true }, + ); + if (effectiveConfig.exitCode !== 0 || effectiveConfig.stdoutTruncated) return null; + const configHash = Hex.encode( + yield* digest.digest(Digest.sha256, new TextEncoder().encode(effectiveConfig.stdout)), + ); const statToken = (target: string) => fileSystem.stat(target).pipe( Effect.map( @@ -2385,7 +2395,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed("-")), ); const tokens = yield* Effect.forEach( - ["config", "packed-refs", "reftable/tables.list"], + ["packed-refs", "reftable/tables.list"], (name) => statToken(path.join(gitCommonDir, name)), { concurrency: "unbounded" }, ); @@ -2403,6 +2413,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* if (entry.type === "Directory") directories.push(path.join(directory, entry.name)); } } + tokens.push(configHash); return tokens.join("\0"); }, ); @@ -2437,7 +2448,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const inputsStamp = gitCommonDir === undefined ? null - : yield* readBaseBranchInputsStamp(gitCommonDir).pipe(Effect.orElseSucceed(() => null)); + : yield* readBaseBranchInputsStamp(cwd, gitCommonDir).pipe( + Effect.orElseSucceed(() => null), + ); if (inputsStamp === null) { return yield* findBaseBranchForNoUpstream(cwd, refName, options); } From 155fa3daf5ba499330b8271c13a60e4457714c83 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 21 Nov 2026 05:26:25 +0000 Subject: [PATCH 5/5] fix(server): base-branch cache drops a fallback from a failed remote lookup When listing remotes fails, the resolver falls back to a local candidate. That answer is no longer kept for the cache TTL, so the next read resolves again once git recovers. A repository with no remote stays cacheable. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/vcs/GitVcsDriverCore.test.ts | 31 ++++++++++++++++++-- apps/server/src/vcs/GitVcsDriverCore.ts | 24 ++++++++------- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index 0b1c61069eae7..cfe0377518fea 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -881,9 +881,19 @@ it.effect("resolves a branch's base once while its refs and branch config are un Effect.gen(function* () { const delegate = yield* ChildProcessSpawner.ChildProcessSpawner; const spawnedArgs: Array> = []; + let failRemoteLookup = false; const spawner = ChildProcessSpawner.make((command) => { - if (ChildProcess.isStandardCommand(command)) spawnedArgs.push(command.args.map(String)); - return delegate.spawn(command); + if (!ChildProcess.isStandardCommand(command)) return delegate.spawn(command); + spawnedArgs.push(command.args.map(String)); + return failRemoteLookup && command.args[0] === "remote" + ? Effect.fail( + PlatformError.systemError({ + _tag: "Unknown", + module: "ChildProcess", + method: "spawn", + }), + ) + : delegate.spawn(command); }); const driver = yield* makeGitVcsDriverCore().pipe( Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), @@ -928,6 +938,23 @@ it.effect("resolves a branch's base once while its refs and branch config are un ]); yield* driver.statusDetailsLocal(cwd); assert.equal(lookups(), 1); + + // A fallback forced by a failed remote lookup is resolved again once git recovers. + yield* git(cwd, [ + "config", + "--file", + includedConfig, + "--unset", + "branch.feature/x.t3-merge-base", + ]); + yield* git(cwd, ["config", "--unset", "branch.feature/x.t3-merge-base-ref"]); + failRemoteLookup = true; + yield* driver.getReviewDiffPreview({ cwd }); + failRemoteLookup = false; + lookups(); + yield* driver.getReviewDiffPreview({ cwd }); + yield* driver.getReviewDiffPreview({ cwd }); + assert.equal(lookups(), 1); }), ).pipe(Effect.provide(layerTest)), ); diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index ef6895185c945..4147ebf7ada9f 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -2257,7 +2257,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* cwd: string, refName: string, options?: { readonly allowRemoteOfCurrent?: boolean }, - trace?: { dependsOnRemoteOfCurrent: boolean }, + trace?: { dependsOnRemoteOfCurrent: boolean; degraded: boolean }, ) { const configuredBaseRef = yield* readConfigValue(cwd, `branch.${refName}.t3-merge-base-ref`); if ( @@ -2291,8 +2291,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* if (trackedBase !== null) return trackedBase; } - const primaryRemoteName = yield* resolvePrimaryRemoteName(cwd).pipe( - Effect.orElseSucceed(() => null), + const primaryRemoteName = yield* findPrimaryRemoteName(cwd).pipe( + Effect.orElseSucceed(() => { + if (trace) trace.degraded = true; + return null; + }), ); const candidates = [ Effect.succeed(configuredBaseBranch), @@ -2419,14 +2422,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ); const baseBranchCache = yield* Cache.makeWith( Effect.fnUntraced(function* (key: BaseBranchCacheKey) { - const trace = { dependsOnRemoteOfCurrent: false }; + const trace = { dependsOnRemoteOfCurrent: false, degraded: false }; const base = yield* findBaseBranchForNoUpstream( key.cwd, key.refName, { allowRemoteOfCurrent: key.allowRemoteOfCurrent }, trace, ); - return { base, dependsOnRemoteOfCurrent: trace.dependsOnRemoteOfCurrent }; + return { base, ...trace }; }), { capacity: 2_048, @@ -2460,11 +2463,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* inputsStamp, }; const allowRemoteOfCurrent = options?.allowRemoteOfCurrent === true; - const entry = yield* Cache.get( - baseBranchCache, - new BaseBranchCacheKey({ ...key, allowRemoteOfCurrent }), - ); - if (!entry.dependsOnRemoteOfCurrent) { + const cacheKey = new BaseBranchCacheKey({ ...key, allowRemoteOfCurrent }); + const entry = yield* Cache.get(baseBranchCache, cacheKey); + // A fallback forced by a failed remote lookup must not outlive the failure. + if (entry.degraded) { + yield* Cache.invalidate(baseBranchCache, cacheKey); + } else if (!entry.dependsOnRemoteOfCurrent) { yield* Cache.set( baseBranchCache, new BaseBranchCacheKey({ ...key, allowRemoteOfCurrent: !allowRemoteOfCurrent }),