TypeScript
perf(server): worktree creation spawns fewer git processes (#28582)
Hieu Le74003bd18d1b
diff --git a/apps/server/src/git/GitWorkflowService.ts b/apps/server/src/git/GitWorkflowService.tsindex fdc00849af2..bf89c7cc1ba 100644--- a/apps/server/src/git/GitWorkflowService.ts+++ b/apps/server/src/git/GitWorkflowService.ts@@ -105,6 +105,7 @@ export class GitWorkflowService extends Context.Service<readonly fetchWorktreeBaseCommit: (input: {readonly cwd: string;readonly refName: string;+ readonly remote?: GitVcsDriver.GitWorktreeBaseRemote;}) => Effect.Effect<{ readonly commitSha: string; readonly remoteRefName: string } | null,GitCommandErrordiff --git a/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts b/apps/server/src/orchestration-v2/ThreadLaunchService.test.tsindex 06438082acd..ce9e872b977 100644--- a/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts+++ b/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts@@ -1119,6 +1119,29 @@ it.effect.each(["stable", "upstream/release"])(),);+it.effect("fetches the origin base from the remote it already resolved", () => {+ let fetchInput: Deferred.Deferred<unknown> | null = null;+ const harness = makeHarness({+ fetchWorktreeBaseCommit: (input) => Deferred.succeed(fetchInput!, input).pipe(Effect.as(null)),+ });+ return Effect.gen(function* () {+ fetchInput = yield* Deferred.make<unknown>();+ const launches = yield* ThreadLaunch.ThreadLaunchService;+ yield* launches.launch(+ launchInput({+ command: "command:launch:resolved-remote",+ thread: "thread:launch:resolved-remote",+ workspace: { type: "worktree", baseRef: "main", startFromOrigin: true },+ }),+ );+ assert.deepEqual(yield* Deferred.await(fetchInput), {+ cwd: project.workspaceRoot,+ refName: "main",+ remote: { remoteName: "origin", branchName: "main" },+ });+ }).pipe(Effect.provide(harness.layer));+});+it.effect("shows the fetch diagnosis when preparing a worktree from origin fails", () => {const detail ="Git could not authenticate with the remote. Check Git credentials or SSH access on the server, then retry.";diff --git a/apps/server/src/orchestration-v2/ThreadLaunchService.ts b/apps/server/src/orchestration-v2/ThreadLaunchService.tsindex b68eef4b2a4..4ab38c4b305 100644--- a/apps/server/src/orchestration-v2/ThreadLaunchService.ts+++ b/apps/server/src/orchestration-v2/ThreadLaunchService.ts@@ -396,20 +396,23 @@ const make = Effect.gen(function* () {}let startRef = input.workspaceStrategy.baseRef;let baseRefName = input.workspaceStrategy.baseRef;- const shouldFetch =- input.workspaceStrategy.startFromOrigin === true &&- (yield* git- .resolveWorktreeBaseRemote({- cwd: project.workspaceRoot,- refName: input.workspaceStrategy.baseRef,- })- .pipe(Effect.mapError(mapError(input, "provision-worktree", threadId)))) !== null;+ const remote =+ input.workspaceStrategy.startFromOrigin === true+ ? yield* git+ .resolveWorktreeBaseRemote({+ cwd: project.workspaceRoot,+ refName: input.workspaceStrategy.baseRef,+ })+ .pipe(Effect.mapError(mapError(input, "provision-worktree", threadId)))+ : null;+ const shouldFetch = remote !== null;yield* setupTracker.stageStatus(threadId, "fetch", shouldFetch ? "running" : "skipped");- if (shouldFetch) {+ if (remote !== null) {const remoteBase = yield* git.fetchWorktreeBaseCommit({cwd: project.workspaceRoot,refName: input.workspaceStrategy.baseRef,+ remote,}).pipe(Effect.mapError(mapError(input, "provision-worktree", threadId)));if (remoteBase !== null) {diff --git a/apps/server/src/vcs/GitVcsDriver.ts b/apps/server/src/vcs/GitVcsDriver.tsindex fe49a6b1391..3cd9e36cfe4 100644--- a/apps/server/src/vcs/GitVcsDriver.ts+++ b/apps/server/src/vcs/GitVcsDriver.ts@@ -285,6 +285,11 @@ export interface GitWriteConfigValueInput {readonly value: string;}+export interface GitWorktreeBaseRemote {+ remoteName: string;+ branchName: string;+}+export interface GitSetBranchUpstreamInput {cwd: string;branch: string;@@ -392,10 +397,13 @@ export class GitVcsDriver extends Context.Service<) => Effect.Effect<GitResolveRemoteTrackingCommitResult, GitCommandError>;readonly resolveWorktreeBaseRemote: (input: GitFetchWorktreeBaseCommitInput,- ) => Effect.Effect<{ remoteName: string; branchName: string } | null, GitCommandError>;- /** Fetches the configured upstream, explicit remote ref, or origin; null uses the local base. */+ ) => Effect.Effect<GitWorktreeBaseRemote | null, GitCommandError>;+ /**+ * Fetches the configured upstream, explicit remote ref, or origin; null uses the local base.+ * A caller that already ran `resolveWorktreeBaseRemote` passes its result as `remote`.+ */readonly fetchWorktreeBaseCommit: (- input: GitFetchWorktreeBaseCommitInput,+ input: GitFetchWorktreeBaseCommitInput & { readonly remote?: GitWorktreeBaseRemote },) => Effect.Effect<GitResolveRemoteTrackingCommitResult | null, GitCommandError>;readonly fetchRemoteBranch: (input: GitFetchRemoteBranchInput,diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.tsindex f47407385a7..29d67323426 100644--- a/apps/server/src/vcs/GitVcsDriverCore.test.ts+++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts@@ -5579,6 +5579,55 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {}),);+ it.effect("fetches a worktree base without downloading tags", () =>+ Effect.gen(function* () {+ const cwd = yield* makeTmpDir();+ const remote = yield* makeTmpDir("git-remote-");+ const { initialBranch } = yield* initRepoWithCommit(cwd);+ yield* git(remote, ["init", "--bare"]);+ yield* git(cwd, ["remote", "add", "origin", remote]);+ yield* git(cwd, ["push", "-u", "origin", initialBranch]);+ const localHead = yield* git(cwd, ["rev-parse", "HEAD"]);+ yield* git(cwd, ["commit", "--allow-empty", "-m", "tagged release"]);+ const remoteHead = yield* git(cwd, ["rev-parse", "HEAD"]);+ yield* git(cwd, ["tag", "v1"]);+ yield* git(cwd, ["push", "origin", initialBranch, "v1"]);+ yield* git(cwd, ["tag", "-d", "v1"]);+ yield* git(cwd, ["reset", "--hard", localHead]);+ yield* git(cwd, ["update-ref", "-d", `refs/remotes/origin/${initialBranch}`]);+ const driver = yield* GitVcsDriver.GitVcsDriver;++ assert.deepEqual(yield* driver.fetchWorktreeBaseCommit({ cwd, refName: initialBranch }), {+ commitSha: remoteHead,+ remoteRefName: `origin/${initialBranch}`,+ });+ assert.equal(yield* git(cwd, ["tag", "--list", "v1"]), "");+ }),+ );++ it.effect("fetches the remote a caller already resolved for the worktree base", () =>+ Effect.gen(function* () {+ const cwd = yield* makeTmpDir();+ const remote = yield* makeTmpDir("git-remote-");+ const { initialBranch } = yield* initRepoWithCommit(cwd);+ yield* git(remote, ["init", "--bare"]);+ yield* git(cwd, ["remote", "add", "origin", remote]);+ yield* git(cwd, ["push", "origin", initialBranch]);+ const head = yield* git(cwd, ["rev-parse", "HEAD"]);+ const driver = yield* GitVcsDriver.GitVcsDriver;++ assert.isNull(yield* driver.fetchWorktreeBaseCommit({ cwd, refName: "unpublished" }));+ assert.deepEqual(+ yield* driver.fetchWorktreeBaseCommit({+ cwd,+ refName: "unpublished",+ remote: { remoteName: "origin", branchName: initialBranch },+ }),+ { commitSha: head, remoteRefName: `origin/${initialBranch}` },+ );+ }),+ );+it.effect("fails instead of falling back when the worktree base remote is unreachable", () =>Effect.gen(function* () {const cwd = yield* makeTmpDir();diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.tsindex 04895520f81..9c482a8f7f2 100644--- a/apps/server/src/vcs/GitVcsDriverCore.ts+++ b/apps/server/src/vcs/GitVcsDriverCore.ts@@ -4162,7 +4162,17 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*const progress = options?.progress;const onCheckoutProgress = progress?.onCheckoutProgress;- const checkoutWorkers = (yield* readConfigValue(input.cwd, "checkout.workers")) ?? "0";+ // Both reads leave the repository alone, so they share one round trip before the checkout.+ const [configuredCheckoutWorkers, base] = yield* Effect.all(+ [+ readConfigValue(input.cwd, "checkout.workers"),+ input.newRefName && input.baseRefName+ ? resolveBranchBase(input.cwd, input.baseRefName)+ : Effect.succeed(null),+ ],+ { concurrency: "unbounded" },+ );+ const checkoutWorkers = configuredCheckoutWorkers ?? "0";yield* executeGitWithStableDiagnostics("GitVcsDriver.createWorktree",input.cwd,@@ -4272,32 +4282,27 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*}const newRefName = input.newRefName;- if (newRefName && input.baseRefName) {- const base = yield* resolveBranchBase(input.cwd, input.baseRefName);- if (base) {- yield* Effect.gen(function* () {+ if (newRefName && base) {+ yield* Effect.gen(function* () {+ yield* writeConfigValue({+ cwd: input.cwd,+ key: branchBaseConfigKey(newRefName),+ value: base.branchName,+ });+ if (base.baseRef.startsWith("refs/remotes/")) {+ // The base key names a branch on the host; this one keeps the remote it came from.yield* writeConfigValue({cwd: input.cwd,- key: branchBaseConfigKey(newRefName),- value: base.branchName,+ key: `branch.${newRefName}.t3-merge-base-ref`,+ value: base.baseRef,});- if (base.baseRef.startsWith("refs/remotes/")) {- // The base key names a branch on the host; this one keeps the remote it came from.- yield* writeConfigValue({- cwd: input.cwd,- key: `branch.${input.newRefName}.t3-merge-base-ref`,- value: base.baseRef,- });- }- }).pipe(- Effect.catchTags({- GitCommandError: () =>- Effect.logWarning(- "worktree base-ref configuration failed; using base-branch fallback",- ),- }),- );- }+ }+ }).pipe(+ Effect.catchTags({+ GitCommandError: () =>+ Effect.logWarning("worktree base-ref configuration failed; using base-branch fallback"),+ }),+ );}return {@@ -4463,7 +4468,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*const branch =parseRemoteRefWithRemoteNames(input.refName, [input.remoteName])?.branchName ?? input.refName;const scopedArgs = [- ...args,+ "fetch",+ "--quiet",+ "--no-tags",+ "--",+ input.remoteName,`+refs/heads/${branch}:refs/remotes/${input.remoteName}/${branch}`,];const result = yield* runFetch(scopedArgs);@@ -4543,7 +4552,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*const fetchWorktreeBaseCommit: GitVcsDriver.GitVcsDriver["Service"]["fetchWorktreeBaseCommit"] =Effect.fn("fetchWorktreeBaseCommit")(function* (input) {- const target = yield* resolveWorktreeBaseRemote(input);+ const target = input.remote ?? (yield* resolveWorktreeBaseRemote(input));if (target === null) return null;const { remoteName, branchName } = target;const remoteRefName = `${remoteName}/${branchName}`;