The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 07:00 UTC
perf(server): worktree creation spawns fewer git processes (#28582)
Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
Hieu Le committed
Nov 1, 2026 at 22:55 UTC
74003bd18d1b7ed701d1cc733268229b6769d577
6 files changed
+131
-38
apps/server/src/git/GitWorkflowService.ts
+1
@@ -105,6 +105,7 @@ export class GitWorkflowService extends Context.Service<
105
readonly fetchWorktreeBaseCommit: (input: {
106
readonly cwd: string;
107
readonly refName: string;
108
+ readonly remote?: GitVcsDriver.GitWorktreeBaseRemote;
109
}) => Effect.Effect<
110
{ readonly commitSha: string; readonly remoteRefName: string } | null,
111
GitCommandError
apps/server/src/orchestration-v2/ThreadLaunchService.test.ts
+23
@@ -1119,6 +1119,29 @@ it.effect.each(["stable", "upstream/release"])(
1119
),
1120
);
1121
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
+
1145
it.effect("shows the fetch diagnosis when preparing a worktree from origin fails", () => {
1146
const detail =
1147
"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
+12
-9
@@ -396,20 +396,23 @@ const make = Effect.gen(function* () {
396
}
397
let startRef = input.workspaceStrategy.baseRef;
398
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;
409
yield* setupTracker.stageStatus(threadId, "fetch", shouldFetch ? "running" : "skipped");
408
- if (shouldFetch) {
410
+ if (remote !== null) {
411
const remoteBase = yield* git
412
.fetchWorktreeBaseCommit({
413
cwd: project.workspaceRoot,
414
refName: input.workspaceStrategy.baseRef,
415
+ remote,
416
})
417
.pipe(Effect.mapError(mapError(input, "provision-worktree", threadId)));
418
if (remoteBase !== null) {
apps/server/src/vcs/GitVcsDriver.ts
+11
-3
@@ -285,6 +285,11 @@ export interface GitWriteConfigValueInput {
285
readonly value: string;
286
}
287
288
+export interface GitWorktreeBaseRemote {
289
+ remoteName: string;
290
+ branchName: string;
291
+}
292
+
293
export interface GitSetBranchUpstreamInput {
294
cwd: string;
295
branch: string;
@@ -392,10 +397,13 @@ export class GitVcsDriver extends Context.Service<
397
) => Effect.Effect<GitResolveRemoteTrackingCommitResult, GitCommandError>;
398
readonly resolveWorktreeBaseRemote: (
399
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
+ */
405
readonly fetchWorktreeBaseCommit: (
398
- input: GitFetchWorktreeBaseCommitInput,
406
+ input: GitFetchWorktreeBaseCommitInput & { readonly remote?: GitWorktreeBaseRemote },
407
) => Effect.Effect<GitResolveRemoteTrackingCommitResult | null, GitCommandError>;
408
readonly fetchRemoteBranch: (
409
input: GitFetchRemoteBranchInput,
apps/server/src/vcs/GitVcsDriverCore.test.ts
+49
@@ -5579,6 +5579,55 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
5579
}),
5580
);
5581
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
+
5631
it.effect("fails instead of falling back when the worktree base remote is unreachable", () =>
5632
Effect.gen(function* () {
5633
const cwd = yield* makeTmpDir();
apps/server/src/vcs/GitVcsDriverCore.ts
+35
-26
@@ -4162,7 +4162,17 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
4162
const progress = options?.progress;
4163
const onCheckoutProgress = progress?.onCheckoutProgress;
4164
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";
4176
yield* executeGitWithStableDiagnostics(
4177
"GitVcsDriver.createWorktree",
4178
input.cwd,
@@ -4272,32 +4282,27 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
4282
}
4283
4284
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.
4294
yield* writeConfigValue({
4295
cwd: input.cwd,
4281
- key: branchBaseConfigKey(newRefName),
4282
- value: base.branchName,
4296
+ key: `branch.${newRefName}.t3-merge-base-ref`,
4297
+ value: base.baseRef,
4298
});
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
+ );
4306
}
4307
4308
return {
@@ -4463,7 +4468,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
4468
const branch =
4469
parseRemoteRefWithRemoteNames(input.refName, [input.remoteName])?.branchName ?? input.refName;
4470
const scopedArgs = [
4466
- ...args,
4471
+ "fetch",
4472
+ "--quiet",
4473
+ "--no-tags",
4474
+ "--",
4475
+ input.remoteName,
4476
`+refs/heads/${branch}:refs/remotes/${input.remoteName}/${branch}`,
4477
];
4478
const result = yield* runFetch(scopedArgs);
@@ -4543,7 +4552,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
4552
4553
const fetchWorktreeBaseCommit: GitVcsDriver.GitVcsDriver["Service"]["fetchWorktreeBaseCommit"] =
4554
Effect.fn("fetchWorktreeBaseCommit")(function* (input) {
4546
- const target = yield* resolveWorktreeBaseRemote(input);
4555
+ const target = input.remote ?? (yield* resolveWorktreeBaseRemote(input));
4556
if (target === null) return null;
4557
const { remoteName, branchName } = target;
4558
const remoteRefName = `${remoteName}/${branchName}`;