Skip to content

Commit 5df18b6

Browse files
hieu-leeclaude
andcommitted
perf(server): worktree creation spawns fewer git processes
Thread launch resolved the worktree base remote twice (git remote plus git for-each-ref each time, plus a driver lookup). It now resolves once and hands the result to the fetch. createWorktree read checkout.workers and the base-ref comparison one after the other after the base was known. Both are read-only, so they now share one round trip before git worktree add. The single-branch base fetch now passes --no-tags like the other scoped fetches, so a repository with many tags no longer advertises and follows them for one branch. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent b50d8a1 commit 5df18b6

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
@@ -1114,6 +1114,29 @@ it.effect.each(["stable", "upstream/release"])(
11141114
),
11151115
);
11161116

1117+
it.effect("fetches the origin base from the remote it already resolved", () => {
1118+
let fetchInput: Deferred.Deferred<unknown> | null = null;
1119+
const harness = makeHarness({
1120+
fetchWorktreeBaseCommit: (input) => Deferred.succeed(fetchInput!, input).pipe(Effect.as(null)),
1121+
});
1122+
return Effect.gen(function* () {
1123+
fetchInput = yield* Deferred.make<unknown>();
1124+
const launches = yield* ThreadLaunch.ThreadLaunchService;
1125+
yield* launches.launch(
1126+
launchInput({
1127+
command: "command:launch:resolved-remote",
1128+
thread: "thread:launch:resolved-remote",
1129+
workspace: { type: "worktree", baseRef: "main", startFromOrigin: true },
1130+
}),
1131+
);
1132+
assert.deepEqual(yield* Deferred.await(fetchInput), {
1133+
cwd: project.workspaceRoot,
1134+
refName: "main",
1135+
remote: { remoteName: "origin", branchName: "main" },
1136+
});
1137+
}).pipe(Effect.provide(harness.layer));
1138+
});
1139+
11171140
it.effect("shows the fetch diagnosis when preparing a worktree from origin fails", () => {
11181141
const detail =
11191142
"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
@@ -5413,6 +5413,55 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
54135413
}),
54145414
);
54155415

5416+
it.effect("fetches a worktree base without downloading tags", () =>
5417+
Effect.gen(function* () {
5418+
const cwd = yield* makeTmpDir();
5419+
const remote = yield* makeTmpDir("git-remote-");
5420+
const { initialBranch } = yield* initRepoWithCommit(cwd);
5421+
yield* git(remote, ["init", "--bare"]);
5422+
yield* git(cwd, ["remote", "add", "origin", remote]);
5423+
yield* git(cwd, ["push", "-u", "origin", initialBranch]);
5424+
const localHead = yield* git(cwd, ["rev-parse", "HEAD"]);
5425+
yield* git(cwd, ["commit", "--allow-empty", "-m", "tagged release"]);
5426+
const remoteHead = yield* git(cwd, ["rev-parse", "HEAD"]);
5427+
yield* git(cwd, ["tag", "v1"]);
5428+
yield* git(cwd, ["push", "origin", initialBranch, "v1"]);
5429+
yield* git(cwd, ["tag", "-d", "v1"]);
5430+
yield* git(cwd, ["reset", "--hard", localHead]);
5431+
yield* git(cwd, ["update-ref", "-d", `refs/remotes/origin/${initialBranch}`]);
5432+
const driver = yield* GitVcsDriver.GitVcsDriver;
5433+
5434+
assert.deepEqual(yield* driver.fetchWorktreeBaseCommit({ cwd, refName: initialBranch }), {
5435+
commitSha: remoteHead,
5436+
remoteRefName: `origin/${initialBranch}`,
5437+
});
5438+
assert.equal(yield* git(cwd, ["tag", "--list", "v1"]), "");
5439+
}),
5440+
);
5441+
5442+
it.effect("fetches the remote a caller already resolved for the worktree base", () =>
5443+
Effect.gen(function* () {
5444+
const cwd = yield* makeTmpDir();
5445+
const remote = yield* makeTmpDir("git-remote-");
5446+
const { initialBranch } = yield* initRepoWithCommit(cwd);
5447+
yield* git(remote, ["init", "--bare"]);
5448+
yield* git(cwd, ["remote", "add", "origin", remote]);
5449+
yield* git(cwd, ["push", "origin", initialBranch]);
5450+
const head = yield* git(cwd, ["rev-parse", "HEAD"]);
5451+
const driver = yield* GitVcsDriver.GitVcsDriver;
5452+
5453+
assert.isNull(yield* driver.fetchWorktreeBaseCommit({ cwd, refName: "unpublished" }));
5454+
assert.deepEqual(
5455+
yield* driver.fetchWorktreeBaseCommit({
5456+
cwd,
5457+
refName: "unpublished",
5458+
remote: { remoteName: "origin", branchName: initialBranch },
5459+
}),
5460+
{ commitSha: head, remoteRefName: `origin/${initialBranch}` },
5461+
);
5462+
}),
5463+
);
5464+
54165465
it.effect("fails instead of falling back when the worktree base remote is unreachable", () =>
54175466
Effect.gen(function* () {
54185467
const cwd = yield* makeTmpDir();

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

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

4150-
const checkoutWorkers = (yield* readConfigValue(input.cwd, "checkout.workers")) ?? "0";
4150+
// Both reads leave the repository alone, so they share one round trip before the checkout.
4151+
const [configuredCheckoutWorkers, base] = yield* Effect.all(
4152+
[
4153+
readConfigValue(input.cwd, "checkout.workers"),
4154+
input.newRefName && input.baseRefName
4155+
? resolveBranchBase(input.cwd, input.baseRefName)
4156+
: Effect.succeed(null),
4157+
],
4158+
{ concurrency: "unbounded" },
4159+
);
4160+
const checkoutWorkers = configuredCheckoutWorkers ?? "0";
41514161
yield* executeGitWithStableDiagnostics(
41524162
"GitVcsDriver.createWorktree",
41534163
input.cwd,
@@ -4253,32 +4263,27 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
42534263
}
42544264

42554265
const newRefName = input.newRefName;
4256-
if (newRefName && input.baseRefName) {
4257-
const base = yield* resolveBranchBase(input.cwd, input.baseRefName);
4258-
if (base) {
4259-
yield* Effect.gen(function* () {
4266+
if (newRefName && base) {
4267+
yield* Effect.gen(function* () {
4268+
yield* writeConfigValue({
4269+
cwd: input.cwd,
4270+
key: branchBaseConfigKey(newRefName),
4271+
value: base.branchName,
4272+
});
4273+
if (base.baseRef.startsWith("refs/remotes/")) {
4274+
// The base key names a branch on the host; this one keeps the remote it came from.
42604275
yield* writeConfigValue({
42614276
cwd: input.cwd,
4262-
key: branchBaseConfigKey(newRefName),
4263-
value: base.branchName,
4277+
key: `branch.${newRefName}.t3-merge-base-ref`,
4278+
value: base.baseRef,
42644279
});
4265-
if (base.baseRef.startsWith("refs/remotes/")) {
4266-
// The base key names a branch on the host; this one keeps the remote it came from.
4267-
yield* writeConfigValue({
4268-
cwd: input.cwd,
4269-
key: `branch.${input.newRefName}.t3-merge-base-ref`,
4270-
value: base.baseRef,
4271-
});
4272-
}
4273-
}).pipe(
4274-
Effect.catchTags({
4275-
GitCommandError: () =>
4276-
Effect.logWarning(
4277-
"worktree base-ref configuration failed; using base-branch fallback",
4278-
),
4279-
}),
4280-
);
4281-
}
4280+
}
4281+
}).pipe(
4282+
Effect.catchTags({
4283+
GitCommandError: () =>
4284+
Effect.logWarning("worktree base-ref configuration failed; using base-branch fallback"),
4285+
}),
4286+
);
42824287
}
42834288

42844289
return {
@@ -4444,7 +4449,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
44444449
const branch =
44454450
parseRemoteRefWithRemoteNames(input.refName, [input.remoteName])?.branchName ?? input.refName;
44464451
const scopedArgs = [
4447-
...args,
4452+
"fetch",
4453+
"--quiet",
4454+
"--no-tags",
4455+
"--",
4456+
input.remoteName,
44484457
`+refs/heads/${branch}:refs/remotes/${input.remoteName}/${branch}`,
44494458
];
44504459
const result = yield* runFetch(scopedArgs);
@@ -4524,7 +4533,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
45244533

45254534
const fetchWorktreeBaseCommit: GitVcsDriver.GitVcsDriver["Service"]["fetchWorktreeBaseCommit"] =
45264535
Effect.fn("fetchWorktreeBaseCommit")(function* (input) {
4527-
const target = yield* resolveWorktreeBaseRemote(input);
4536+
const target = input.remote ?? (yield* resolveWorktreeBaseRemote(input));
45284537
if (target === null) return null;
45294538
const { remoteName, branchName } = target;
45304539
const remoteRefName = `${remoteName}/${branchName}`;

0 commit comments

Comments
 (0)