Skip to content

Commit 74003bd

Browse files
hieu-leeclaude
andauthored
perf(server): worktree creation spawns fewer git processes (#28582)
Co-authored-by: Claude Sonnet 5.5 <[email protected]>
1 parent bcc6c08 commit 74003bd

6 files changed

Lines changed: 131 additions & 38 deletions

File tree

‎apps/server/src/git/GitWorkflowService.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export class GitWorkflowService extends Context.Service<
105105
readonly fetchWorktreeBaseCommit: (input: {
106106
readonly cwd: string;
107107
readonly refName: string;
108+
readonly remote?: GitVcsDriver.GitWorktreeBaseRemote;
108109
}) => Effect.Effect<
109110
{ readonly commitSha: string; readonly remoteRefName: string } | null,
110111
GitCommandError

‎apps/server/src/orchestration-v2/ThreadLaunchService.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,29 @@ it.effect.each(["stable", "upstream/release"])(
11191119
),
11201120
);
11211121

1122+
it.effect("fetches the origin base from the remote it already resolved", () => {
1123+
let fetchInput: Deferred.Deferred<unknown> | null = null;
1124+
const harness = makeHarness({
1125+
fetchWorktreeBaseCommit: (input) => Deferred.succeed(fetchInput!, input).pipe(Effect.as(null)),
1126+
});
1127+
return Effect.gen(function* () {
1128+
fetchInput = yield* Deferred.make<unknown>();
1129+
const launches = yield* ThreadLaunch.ThreadLaunchService;
1130+
yield* launches.launch(
1131+
launchInput({
1132+
command: "command:launch:resolved-remote",
1133+
thread: "thread:launch:resolved-remote",
1134+
workspace: { type: "worktree", baseRef: "main", startFromOrigin: true },
1135+
}),
1136+
);
1137+
assert.deepEqual(yield* Deferred.await(fetchInput), {
1138+
cwd: project.workspaceRoot,
1139+
refName: "main",
1140+
remote: { remoteName: "origin", branchName: "main" },
1141+
});
1142+
}).pipe(Effect.provide(harness.layer));
1143+
});
1144+
11221145
it.effect("shows the fetch diagnosis when preparing a worktree from origin fails", () => {
11231146
const detail =
11241147
"Git could not authenticate with the remote. Check Git credentials or SSH access on the server, then retry.";

‎apps/server/src/orchestration-v2/ThreadLaunchService.ts‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -396,20 +396,23 @@ const make = Effect.gen(function* () {
396396
}
397397
let startRef = input.workspaceStrategy.baseRef;
398398
let baseRefName = input.workspaceStrategy.baseRef;
399-
const shouldFetch =
400-
input.workspaceStrategy.startFromOrigin === true &&
401-
(yield* git
402-
.resolveWorktreeBaseRemote({
403-
cwd: project.workspaceRoot,
404-
refName: input.workspaceStrategy.baseRef,
405-
})
406-
.pipe(Effect.mapError(mapError(input, "provision-worktree", threadId)))) !== null;
399+
const remote =
400+
input.workspaceStrategy.startFromOrigin === true
401+
? yield* git
402+
.resolveWorktreeBaseRemote({
403+
cwd: project.workspaceRoot,
404+
refName: input.workspaceStrategy.baseRef,
405+
})
406+
.pipe(Effect.mapError(mapError(input, "provision-worktree", threadId)))
407+
: null;
408+
const shouldFetch = remote !== null;
407409
yield* setupTracker.stageStatus(threadId, "fetch", shouldFetch ? "running" : "skipped");
408-
if (shouldFetch) {
410+
if (remote !== null) {
409411
const remoteBase = yield* git
410412
.fetchWorktreeBaseCommit({
411413
cwd: project.workspaceRoot,
412414
refName: input.workspaceStrategy.baseRef,
415+
remote,
413416
})
414417
.pipe(Effect.mapError(mapError(input, "provision-worktree", threadId)));
415418
if (remoteBase !== null) {

‎apps/server/src/vcs/GitVcsDriver.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,11 @@ export interface GitWriteConfigValueInput {
285285
readonly value: string;
286286
}
287287

288+
export interface GitWorktreeBaseRemote {
289+
remoteName: string;
290+
branchName: string;
291+
}
292+
288293
export interface GitSetBranchUpstreamInput {
289294
cwd: string;
290295
branch: string;
@@ -392,10 +397,13 @@ export class GitVcsDriver extends Context.Service<
392397
) => Effect.Effect<GitResolveRemoteTrackingCommitResult, GitCommandError>;
393398
readonly resolveWorktreeBaseRemote: (
394399
input: GitFetchWorktreeBaseCommitInput,
395-
) => Effect.Effect<{ remoteName: string; branchName: string } | null, GitCommandError>;
396-
/** Fetches the configured upstream, explicit remote ref, or origin; null uses the local base. */
400+
) => Effect.Effect<GitWorktreeBaseRemote | null, GitCommandError>;
401+
/**
402+
* Fetches the configured upstream, explicit remote ref, or origin; null uses the local base.
403+
* A caller that already ran `resolveWorktreeBaseRemote` passes its result as `remote`.
404+
*/
397405
readonly fetchWorktreeBaseCommit: (
398-
input: GitFetchWorktreeBaseCommitInput,
406+
input: GitFetchWorktreeBaseCommitInput & { readonly remote?: GitWorktreeBaseRemote },
399407
) => Effect.Effect<GitResolveRemoteTrackingCommitResult | null, GitCommandError>;
400408
readonly fetchRemoteBranch: (
401409
input: GitFetchRemoteBranchInput,

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5579,6 +5579,55 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
55795579
}),
55805580
);
55815581

5582+
it.effect("fetches a worktree base without downloading tags", () =>
5583+
Effect.gen(function* () {
5584+
const cwd = yield* makeTmpDir();
5585+
const remote = yield* makeTmpDir("git-remote-");
5586+
const { initialBranch } = yield* initRepoWithCommit(cwd);
5587+
yield* git(remote, ["init", "--bare"]);
5588+
yield* git(cwd, ["remote", "add", "origin", remote]);
5589+
yield* git(cwd, ["push", "-u", "origin", initialBranch]);
5590+
const localHead = yield* git(cwd, ["rev-parse", "HEAD"]);
5591+
yield* git(cwd, ["commit", "--allow-empty", "-m", "tagged release"]);
5592+
const remoteHead = yield* git(cwd, ["rev-parse", "HEAD"]);
5593+
yield* git(cwd, ["tag", "v1"]);
5594+
yield* git(cwd, ["push", "origin", initialBranch, "v1"]);
5595+
yield* git(cwd, ["tag", "-d", "v1"]);
5596+
yield* git(cwd, ["reset", "--hard", localHead]);
5597+
yield* git(cwd, ["update-ref", "-d", `refs/remotes/origin/${initialBranch}`]);
5598+
const driver = yield* GitVcsDriver.GitVcsDriver;
5599+
5600+
assert.deepEqual(yield* driver.fetchWorktreeBaseCommit({ cwd, refName: initialBranch }), {
5601+
commitSha: remoteHead,
5602+
remoteRefName: `origin/${initialBranch}`,
5603+
});
5604+
assert.equal(yield* git(cwd, ["tag", "--list", "v1"]), "");
5605+
}),
5606+
);
5607+
5608+
it.effect("fetches the remote a caller already resolved for the worktree base", () =>
5609+
Effect.gen(function* () {
5610+
const cwd = yield* makeTmpDir();
5611+
const remote = yield* makeTmpDir("git-remote-");
5612+
const { initialBranch } = yield* initRepoWithCommit(cwd);
5613+
yield* git(remote, ["init", "--bare"]);
5614+
yield* git(cwd, ["remote", "add", "origin", remote]);
5615+
yield* git(cwd, ["push", "origin", initialBranch]);
5616+
const head = yield* git(cwd, ["rev-parse", "HEAD"]);
5617+
const driver = yield* GitVcsDriver.GitVcsDriver;
5618+
5619+
assert.isNull(yield* driver.fetchWorktreeBaseCommit({ cwd, refName: "unpublished" }));
5620+
assert.deepEqual(
5621+
yield* driver.fetchWorktreeBaseCommit({
5622+
cwd,
5623+
refName: "unpublished",
5624+
remote: { remoteName: "origin", branchName: initialBranch },
5625+
}),
5626+
{ commitSha: head, remoteRefName: `origin/${initialBranch}` },
5627+
);
5628+
}),
5629+
);
5630+
55825631
it.effect("fails instead of falling back when the worktree base remote is unreachable", () =>
55835632
Effect.gen(function* () {
55845633
const cwd = yield* makeTmpDir();

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4162,7 +4162,17 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
41624162
const progress = options?.progress;
41634163
const onCheckoutProgress = progress?.onCheckoutProgress;
41644164

4165-
const checkoutWorkers = (yield* readConfigValue(input.cwd, "checkout.workers")) ?? "0";
4165+
// Both reads leave the repository alone, so they share one round trip before the checkout.
4166+
const [configuredCheckoutWorkers, base] = yield* Effect.all(
4167+
[
4168+
readConfigValue(input.cwd, "checkout.workers"),
4169+
input.newRefName && input.baseRefName
4170+
? resolveBranchBase(input.cwd, input.baseRefName)
4171+
: Effect.succeed(null),
4172+
],
4173+
{ concurrency: "unbounded" },
4174+
);
4175+
const checkoutWorkers = configuredCheckoutWorkers ?? "0";
41664176
yield* executeGitWithStableDiagnostics(
41674177
"GitVcsDriver.createWorktree",
41684178
input.cwd,
@@ -4272,32 +4282,27 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
42724282
}
42734283

42744284
const newRefName = input.newRefName;
4275-
if (newRefName && input.baseRefName) {
4276-
const base = yield* resolveBranchBase(input.cwd, input.baseRefName);
4277-
if (base) {
4278-
yield* Effect.gen(function* () {
4285+
if (newRefName && base) {
4286+
yield* Effect.gen(function* () {
4287+
yield* writeConfigValue({
4288+
cwd: input.cwd,
4289+
key: branchBaseConfigKey(newRefName),
4290+
value: base.branchName,
4291+
});
4292+
if (base.baseRef.startsWith("refs/remotes/")) {
4293+
// The base key names a branch on the host; this one keeps the remote it came from.
42794294
yield* writeConfigValue({
42804295
cwd: input.cwd,
4281-
key: branchBaseConfigKey(newRefName),
4282-
value: base.branchName,
4296+
key: `branch.${newRefName}.t3-merge-base-ref`,
4297+
value: base.baseRef,
42834298
});
4284-
if (base.baseRef.startsWith("refs/remotes/")) {
4285-
// The base key names a branch on the host; this one keeps the remote it came from.
4286-
yield* writeConfigValue({
4287-
cwd: input.cwd,
4288-
key: `branch.${input.newRefName}.t3-merge-base-ref`,
4289-
value: base.baseRef,
4290-
});
4291-
}
4292-
}).pipe(
4293-
Effect.catchTags({
4294-
GitCommandError: () =>
4295-
Effect.logWarning(
4296-
"worktree base-ref configuration failed; using base-branch fallback",
4297-
),
4298-
}),
4299-
);
4300-
}
4299+
}
4300+
}).pipe(
4301+
Effect.catchTags({
4302+
GitCommandError: () =>
4303+
Effect.logWarning("worktree base-ref configuration failed; using base-branch fallback"),
4304+
}),
4305+
);
43014306
}
43024307

43034308
return {
@@ -4463,7 +4468,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
44634468
const branch =
44644469
parseRemoteRefWithRemoteNames(input.refName, [input.remoteName])?.branchName ?? input.refName;
44654470
const scopedArgs = [
4466-
...args,
4471+
"fetch",
4472+
"--quiet",
4473+
"--no-tags",
4474+
"--",
4475+
input.remoteName,
44674476
`+refs/heads/${branch}:refs/remotes/${input.remoteName}/${branch}`,
44684477
];
44694478
const result = yield* runFetch(scopedArgs);
@@ -4543,7 +4552,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
45434552

45444553
const fetchWorktreeBaseCommit: GitVcsDriver.GitVcsDriver["Service"]["fetchWorktreeBaseCommit"] =
45454554
Effect.fn("fetchWorktreeBaseCommit")(function* (input) {
4546-
const target = yield* resolveWorktreeBaseRemote(input);
4555+
const target = input.remote ?? (yield* resolveWorktreeBaseRemote(input));
45474556
if (target === null) return null;
45484557
const { remoteName, branchName } = target;
45494558
const remoteRefName = `${remoteName}/${branchName}`;

0 commit comments

Comments
 (0)