@devries-cameron20 / t3code-1 / commits / 564b2f96b2
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

perf(server): batch worktree branch allocation and avoid path collisions (#32934)

Co-authored-by: GPT 6.1 Sol <codex@openai.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Grok 4.7 <noreply@x.ai>

Wout Stiens committed Nov 8, 2026 at 22:24 UTC 564b2f96b21076353c334a82eaf7af6e60781f60
2 files changed +176 -59
apps/server/src/vcs/GitVcsDriverCore.test.ts
+140 -24
@@ -4319,13 +4319,13 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
4319 }),
4320 );
4321
4322 - it.effect("flattens a generated name when a branch at its parent path blocks it", () =>
4322 + it.effect.each(["t3", "T3"])("flattens a generated name when parent %s blocks it", (parent) =>
4323 Effect.gen(function* () {
4324 const cwd = yield* makeTmpDir();
4325 yield* initRepoWithCommit(cwd);
4326 const driver = yield* GitVcsDriver.GitVcsDriver;
4327 // A local `t3` branch is why launches start on the flat `t3-<hex>` temporary branch.
4328 - yield* git(cwd, ["branch", "t3"]);
4328 + yield* git(cwd, ["branch", parent]);
4329 yield* git(cwd, ["checkout", "-b", "t3-abcd1234"]);
4330 yield* git(cwd, ["branch", "t3-fix-login"]);
4331
@@ -4340,26 +4340,112 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
4340 }),
4341 );
4342
4343 - it.effect("skips a flattened name that a nested branch blocks", () =>
4343 + it.effect.each(["t3-fix-login/child", "T3-Fix-Login/child", "T3-FIX-LOGIN"])(
4344 + "skips a flattened name blocked by %s",
4345 + (occupied) =>
4346 + Effect.gen(function* () {
4347 + const cwd = yield* makeTmpDir();
4348 + yield* initRepoWithCommit(cwd);
4349 + const driver = yield* GitVcsDriver.GitVcsDriver;
4350 + yield* git(cwd, ["branch", "t3"]);
4351 + yield* git(cwd, ["checkout", "-b", "t3-abcd1234"]);
4352 + yield* git(cwd, ["branch", occupied]);
4353 +
4354 + const renamed = yield* driver.renameBranch({
4355 + cwd,
4356 + oldBranch: "t3-abcd1234",
4357 + newBranch: "t3/fix-login",
4358 + });
4359 +
4360 + assert.equal(renamed.branch, "t3-fix-login-1");
4361 + assert.equal(yield* git(cwd, ["branch", "--show-current"]), "t3-fix-login-1");
4362 + }),
4363 + );
4364 +
4365 + it.effect("refuses exhausted automatic suffixes without changing existing branches", () =>
4366 Effect.gen(function* () {
4367 const cwd = yield* makeTmpDir();
4346 - yield* initRepoWithCommit(cwd);
4368 + const { initialBranch } = yield* initRepoWithCommit(cwd);
4369 const driver = yield* GitVcsDriver.GitVcsDriver;
4348 - yield* git(cwd, ["branch", "t3"]);
4349 - yield* git(cwd, ["checkout", "-b", "t3-abcd1234"]);
4350 - yield* git(cwd, ["branch", "t3-fix-login/child"]);
4351 -
4352 - const renamed = yield* driver.renameBranch({
4353 - cwd,
4354 - oldBranch: "t3-abcd1234",
4355 - newBranch: "t3/fix-login",
4356 - });
4357 -
4358 - assert.equal(renamed.branch, "t3-fix-login-1");
4359 - assert.equal(yield* git(cwd, ["branch", "--show-current"]), "t3-fix-login-1");
4370 + const oid = yield* git(cwd, ["rev-parse", "HEAD"]);
4371 + for (let index = 0; index <= 100; index += 1) {
4372 + yield* git(cwd, [
4373 + "update-ref",
4374 + `refs/heads/${index === 0 ? "topic" : `topic-${index}`}`,
4375 + oid,
4376 + ]);
4377 + }
4378 + const result = yield* driver
4379 + .renameBranch({ cwd, oldBranch: initialBranch, newBranch: "topic" })
4380 + .pipe(Effect.result);
4381 + assert.isTrue(Result.isFailure(result));
4382 + if (Result.isFailure(result))
4383 + assert.include(result.failure.detail, "Could not find an available branch name");
4384 + assert.equal(yield* git(cwd, ["branch", "--show-current"]), initialBranch);
4385 + assert.equal((yield* driver.listLocalBranchNames(cwd)).length, 102);
4386 }),
4387 );
4388
4389 + it.effect.each(["rename", "createWorktree"] as const)(
4390 + "preserves a concurrent creator's branch when %s loses the atomic ref operation",
4391 + (operation) =>
4392 + Effect.gen(function* () {
4393 + const cwd = yield* makeTmpDir();
4394 + const { initialBranch } = yield* initRepoWithCommit(cwd);
4395 + const expected = yield* git(cwd, [
4396 + "commit-tree",
4397 + "HEAD^{tree}",
4398 + "-m",
4399 + "concurrent winner",
4400 + ]);
4401 + const original = yield* GitVcsDriver.GitVcsDriver;
4402 + const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
4403 + let claimed = false;
4404 + const spawner = ChildProcessSpawner.make((command) =>
4405 + Effect.gen(function* () {
4406 + if (
4407 + !claimed &&
4408 + ChildProcess.isStandardCommand(command) &&
4409 + command.args[0] === "branch" &&
4410 + command.args.includes(operation === "rename" ? "-m" : "--no-track")
4411 + ) {
4412 + claimed = true;
4413 + yield* original
4414 + .execute({
4415 + cwd,
4416 + operation: "test.concurrentCreator",
4417 + args: ["update-ref", "refs/heads/topic", expected, ""],
4418 + })
4419 + .pipe(Effect.orDie);
4420 + }
4421 + return yield* delegate.spawn(command);
4422 + }),
4423 + );
4424 + const driver = yield* makeGitVcsDriverCore().pipe(
4425 + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
4426 + Effect.provide(layerServerConfig),
4427 + );
4428 + const result = yield* (
4429 + operation === "rename"
4430 + ? driver
4431 + .renameBranch({ cwd, oldBranch: initialBranch, newBranch: "topic" })
4432 + .pipe(Effect.asVoid)
4433 + : driver
4434 + .createWorktree({
4435 + cwd,
4436 + refName: initialBranch,
4437 + newRefName: "topic",
4438 + path: NodePath.join(cwd, "checkout"),
4439 + })
4440 + .pipe(Effect.asVoid)
4441 + ).pipe(Effect.result);
4442 + assert.isTrue(claimed);
4443 + assert.isTrue(Result.isFailure(result));
4444 + assert.equal(yield* git(cwd, ["rev-parse", "refs/heads/topic"]), expected);
4445 + assert.equal(yield* git(cwd, ["branch", "--show-current"]), initialBranch);
4446 + }),
4447 + );
4448 +
4449 it.effect("rejects a missing branch without restoring a matching dirty file", () =>
4450 Effect.gen(function* () {
4451 const cwd = yield* makeTmpDir();
@@ -4956,13 +5042,15 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
5042 { cwd, path: null, refName: initialBranch, newRefName: "feature/custom-dir" },
5043 { worktreesDirectory },
5044 );
4959 - const expected = pathService.join(
4960 - worktreesDirectory,
4961 - pathService.basename(cwd),
4962 - "feature-custom-dir",
5045 + assert.equal(
5046 + pathService.dirname(created.worktree.path),
5047 + pathService.join(worktreesDirectory, pathService.basename(cwd)),
5048 + );
5049 + assert.match(
5050 + pathService.basename(created.worktree.path),
5051 + /^feature-custom-dir-[0-9a-f]{16}$/,
5052 );
4964 - assert.equal(created.worktree.path, expected);
4965 - assert.equal(yield* fileSystem.exists(expected), true);
5053 + assert.equal(yield* fileSystem.exists(created.worktree.path), true);
5054
5055 const error = yield* driver
5056 .createWorktree(
@@ -4982,6 +5070,33 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
5070 }),
5071 );
5072
5073 + it.effect(
5074 + "keeps generated paths distinct across branch spellings and equal repository basenames",
5075 + () =>
5076 + Effect.gen(function* () {
5077 + const fs = yield* FileSystem.FileSystem;
5078 + const path = yield* Path.Path;
5079 + const root = yield* makeTmpDir();
5080 + const worktreesDirectory = yield* makeTmpDir();
5081 + const driver = yield* GitVcsDriver.GitVcsDriver;
5082 + const paths = new Set<string>();
5083 + for (const owner of ["one", "two"]) {
5084 + const cwd = path.join(root, owner, "repository");
5085 + yield* fs.makeDirectory(cwd, { recursive: true });
5086 + const { initialBranch } = yield* initRepoWithCommit(cwd);
5087 + for (const branch of ["feat/a", "feat-a"]) {
5088 + const created = yield* driver.createWorktree(
5089 + { cwd, path: null, refName: initialBranch, newRefName: branch },
5090 + { worktreesDirectory },
5091 + );
5092 + paths.add(created.worktree.path);
5093 + assert.equal(yield* git(created.worktree.path, ["branch", "--show-current"]), branch);
5094 + }
5095 + }
5096 + assert.equal(paths.size, 4);
5097 + }),
5098 + );
5099 +
5100 it.effect("keeps generated worktree folders short without changing long branch names", () =>
5101 Effect.gen(function* () {
5102 const pathService = yield* Path.Path;
@@ -4996,6 +5111,8 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
5111 `feature/${"a".repeat(40)}/5fac`,
5112 `feature/${"a".repeat(40)}/18828`,
5113 `${"a".repeat(22)}😀/${"b".repeat(30)}`,
5114 + "😀".repeat(60),
5115 + "界".repeat(80),
5116 "z".repeat(40),
5117 "z".repeat(41),
5118 ];
@@ -5005,8 +5122,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
5122 { worktreesDirectory },
5123 );
5124 const folder = pathService.basename(created.worktree.path);
5008 - assert.isAtMost(folder.length, 40);
5009 - if (branch.length <= 40) assert.equal(folder, branch);
5125 + assert.isAtMost(Buffer.byteLength(folder), 40);
5126 assert.equal(yield* git(created.worktree.path, ["branch", "--show-current"]), branch);
5127 assert.include(
5128 yield* git(cwd, ["-c", "core.quotePath=false", "worktree", "list", "--porcelain"]),
apps/server/src/vcs/GitVcsDriverCore.ts
+36 -35
@@ -1262,38 +1262,38 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1262 cwd: string,
1263 requestedBranch: string,
1264 ) {
1265 - // A branch at a parent path (such as `t3`) blocks every ref nested under it, so no suffix
1266 - // would help; flatten to a sibling the way temporary worktree branches already do.
1267 - const segments = requestedBranch.split("/");
1268 - let blockedByParent = false;
1269 - for (let index = 1; index < segments.length && !blockedByParent; index += 1) {
1270 - blockedByParent = yield* branchExists(cwd, segments.slice(0, index).join("/"));
1265 + // Exceeding the limit fails the command, so the set is never built from a partial list.
1266 + const { stdout } = yield* executeGit(
1267 + "GitVcsDriver.resolveAvailableBranchName",
1268 + cwd,
1269 + ["for-each-ref", "--format=%(refname:strip=2)", "refs/heads/"],
1270 + { maxOutputBytes: 16 * 1024 * 1024 },
1271 + );
1272 + // Reserve case variants too: loose refs can live on case-insensitive filesystems.
1273 + const branches = new Set(
1274 + stdout
1275 + .split("\n")
1276 + .filter(Boolean)
1277 + .map((name) => name.toLowerCase()),
1278 + );
1279 + // A branch nested under a name (`t3-fix/child`) blocks it as surely as one with the name.
1280 + const blockedNames = new Set(branches);
1281 + for (const branch of branches) {
1282 + const segments = branch.split("/");
1283 + for (let index = 1; index < segments.length; index += 1) {
1284 + blockedNames.add(segments.slice(0, index).join("/"));
1285 + }
1286 }
1287 + // An ancestor ref blocks every suffix beneath it, so flatten to a sibling.
1288 + const segments = requestedBranch.split("/");
1289 + const blockedByParent = segments.some(
1290 + (_, index) => index > 0 && branches.has(segments.slice(0, index).join("/").toLowerCase()),
1291 + );
1292 const desiredBranch = blockedByParent ? segments.join("-") : requestedBranch;
1273 - // A branch nested under the name (`t3-fix/child`) blocks it as surely as one with the name.
1274 - const isBranchNameTaken = (name: string) =>
1275 - Effect.zipWith(
1276 - branchExists(cwd, name),
1277 - runGitStdout(
1278 - "GitVcsDriver.branchExists",
1279 - cwd,
1280 - ["for-each-ref", "--count=1", "--format=%(refname)", `refs/heads/${name}/`],
1281 - true,
1282 - ),
1283 - (exists, nested) => exists || nested.trim().length > 0,
1284 - );
1285 -
1286 - const isDesiredTaken = yield* isBranchNameTaken(desiredBranch);
1287 - if (!isDesiredTaken) {
1288 - return desiredBranch;
1289 - }
1290 -
1293 + if (!blockedNames.has(desiredBranch.toLowerCase())) return desiredBranch;
1294 for (let suffix = 1; suffix <= 100; suffix += 1) {
1295 const candidate = `${desiredBranch}-${suffix}`;
1293 - const isCandidateTaken = yield* isBranchNameTaken(candidate);
1294 - if (!isCandidateTaken) {
1295 - return candidate;
1296 - }
1296 + if (!blockedNames.has(candidate.toLowerCase())) return candidate;
1297 }
1298
1299 return yield* new GitCommandError({
@@ -4239,14 +4239,15 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
4239 detail: `The worktree location "${options?.worktreesDirectory}" must be an absolute folder on this machine, not a drive root. Change it in Settings → Worktrees.`,
4240 });
4241 }
4242 - let directoryName = targetBranch.replace(/\//g, "-");
4243 - // Leave room for files beneath the worktree on Windows, regardless of branch length.
4244 - if (directoryName.length > 40) {
4245 - const hash = yield* digest.digest(Digest.sha256, new TextEncoder().encode(targetBranch));
4246 - // Avoid splitting a surrogate pair, which Git would list back as a different path.
4247 - const prefix = directoryName.slice(0, 23).replace(/[\uD800-\uDBFF]$/, "");
4248 - directoryName = `${prefix}-${Hex.encode(hash).slice(0, 16)}`;
4242 + const identity = `${yield* readGitCommonDir(input.cwd)}\0${targetBranch}`;
4243 + const hash = yield* digest.digest(Digest.sha256, new TextEncoder().encode(identity));
4244 + // UTF-8 bytes bound UTF-16 length too, leaving room for files on Windows.
4245 + let prefix = "";
4246 + for (const character of targetBranch.replace(/\//g, "-")) {
4247 + if (Buffer.byteLength(prefix + character) > 23) break;
4248 + prefix += character;
4249 }
4250 + const directoryName = `${prefix}-${Hex.encode(hash).slice(0, 16)}`;
4251 worktreePath = path.join(parentDir, repoName, directoryName);
4252 }
4253 const newRefName = input.newRefName;