diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index 5e070c3d42a51..a5ec6a0d05413 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -876,6 +876,89 @@ 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> = []; + let failRemoteLookup = false; + const spawner = ChildProcessSpawner.make((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), + ); + 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); + + 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); + + // 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)), +); + it.effect.each([ { kind: "working-tree", request: { sourceKind: "working-tree" } }, { kind: "commit", request: { sourceKind: "commit", commitSha: "HEAD" } }, diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index fcf3b3cbbd605..6e7725833c022 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,13 @@ 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; + inputsStamp: string; +}> {} + class GitRefsSnapshotCacheKey extends Data.Class<{ gitCommonDir: string; epoch: number; @@ -2256,13 +2264,11 @@ 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; - }, + options?: { readonly allowRemoteOfCurrent?: boolean }, + trace?: { dependsOnRemoteOfCurrent: boolean; degraded: boolean }, ) { const configuredBaseRef = yield* readConfigValue(cwd, `branch.${refName}.t3-merge-base-ref`); if ( @@ -2296,16 +2302,17 @@ 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), 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), ]; @@ -2326,9 +2333,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* continue; } if (normalizedCandidate === refName) { + // 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 +2347,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* refName: normalizedCandidate, })) ) { + if (trace) trace.dependsOnRemoteOfCurrent = true; return { baseRef: `${primaryRemoteName}/${normalizedCandidate}`, comparisonRef: `refs/remotes/${primaryRemoteName}/${normalizedCandidate}`, @@ -2361,19 +2372,129 @@ 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; }); + // 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* (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( + (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( + ["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)); + } + } + tokens.push(configHash); + return tokens.join("\0"); + }, + ); + const baseBranchCache = yield* Cache.makeWith( + Effect.fnUntraced(function* (key: BaseBranchCacheKey) { + const trace = { dependsOnRemoteOfCurrent: false, degraded: false }; + const base = yield* findBaseBranchForNoUpstream( + key.cwd, + key.refName, + { allowRemoteOfCurrent: key.allowRemoteOfCurrent }, + trace, + ); + return { base, ...trace }; + }), + { + 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 }, + ) { + const gitCommonDir = (yield* resolveRepositoryPaths(cwd).pipe( + Effect.orElseSucceed(() => null), + ))?.gitCommonDir; + const inputsStamp = + gitCommonDir === undefined + ? null + : yield* readBaseBranchInputsStamp(cwd, gitCommonDir).pipe( + Effect.orElseSucceed(() => null), + ); + if (inputsStamp === null) { + return yield* findBaseBranchForNoUpstream(cwd, refName, options); + } + const key = { + cwd: normalizeRepositoryPathsCacheKey(cwd), + refName, + inputsStamp, + }; + const allowRemoteOfCurrent = options?.allowRemoteOfCurrent === true; + 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 }), + entry, + ); + } + return entry.base; + }, + ); + 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; } @@ -2474,9 +2595,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 { @@ -2760,13 +2879,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 } : {}), @@ -4004,7 +4120,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* cwd: string, branch: string | null, explicitBaseRef?: string, - knownOriginDefaultBranch: string | null = null, ) { const base = explicitBaseRef !== undefined @@ -4018,7 +4133,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" }; @@ -4051,18 +4165,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))