Skip to content

Commit 42a9796

Browse files
committed
fix(server): identify owned worktrees before cancellation cleanup
1 parent 49ec42e commit 42a9796

2 files changed

Lines changed: 72 additions & 32 deletions

File tree

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,16 +1557,22 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
15571557
});
15581558

15591559
describe("worktree operations", () => {
1560-
for (const state of ["new", "new-missing", "existing", "existing-missing"] as const) {
1560+
for (const state of [
1561+
"new",
1562+
"new-missing",
1563+
"existing",
1564+
"existing-missing",
1565+
"concurrent",
1566+
] as const) {
15611567
it.effect(`cleans up only newly registered worktrees on interruption: ${state}`, () =>
15621568
Effect.gen(function* () {
15631569
const cwd = yield* makeTmpDir();
15641570
const { initialBranch } = yield* initRepoWithCommit(cwd);
15651571
const fs = yield* FileSystem.FileSystem;
15661572
const path = yield* Path.Path;
15671573
const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "interrupted");
1568-
const existing = state.startsWith("existing");
1569-
if (existing) {
1574+
const existing = state.startsWith("existing") || state === "concurrent";
1575+
if (state.startsWith("existing")) {
15701576
yield* git(cwd, ["worktree", "add", "-b", "feature/existing", worktreePath]);
15711577
if (state === "existing-missing") {
15721578
yield* fs.remove(worktreePath, { recursive: true });
@@ -1576,10 +1582,16 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
15761582
const commandFinished = yield* Deferred.make<void>();
15771583
const spawner = ChildProcessSpawner.make((command) =>
15781584
Effect.gen(function* () {
1579-
const handle = yield* delegate.spawn(command);
1580-
return ChildProcess.isStandardCommand(command) &&
1585+
const isWorktreeAdd =
1586+
ChildProcess.isStandardCommand(command) &&
15811587
command.args.includes("worktree") &&
1582-
command.args.includes("add")
1588+
command.args.includes("add");
1589+
if (isWorktreeAdd && state === "concurrent") {
1590+
yield* git(cwd, ["worktree", "add", "-b", "feature/existing", worktreePath]);
1591+
yield* writeTextFile(worktreePath, "uncommitted.txt", "keep these edits");
1592+
}
1593+
const handle = yield* delegate.spawn(command);
1594+
return isWorktreeAdd
15831595
? ChildProcessSpawner.makeHandle({
15841596
...handle,
15851597
exitCode: handle.exitCode.pipe(
@@ -1608,7 +1620,13 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
16081620
}
16091621
yield* Fiber.interrupt(creating);
16101622

1611-
assert.equal(yield* fs.exists(worktreePath), state === "existing");
1623+
assert.equal(yield* fs.exists(worktreePath), existing && !state.endsWith("missing"));
1624+
if (state === "concurrent") {
1625+
assert.equal(
1626+
yield* fs.readFileString(path.join(worktreePath, "uncommitted.txt")),
1627+
"keep these edits",
1628+
);
1629+
}
16121630
const registered = yield* git(cwd, ["worktree", "list", "--porcelain", "-z"]);
16131631
assert.equal(registered.includes("feature/existing"), existing);
16141632
assert.notInclude(registered, "feature/interrupted");
@@ -1644,6 +1662,7 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
16441662
baseRefName: initialBranch,
16451663
});
16461664

1665+
assert.notInclude(yield* git(cwd, ["worktree", "list", "--porcelain"]), "locked");
16471666
assert.equal(yield* fs.readFileString(path.join(worktreePath, "checkout-workers")), "0\n");
16481667
assert.equal(yield* fs.readFileString(path.join(worktreePath, "asset.txt")), "filtered\n");
16491668
assert.equal(

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

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3036,29 +3036,21 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
30363036
const sanitizedBranch = targetBranch.replace(/\//g, "-");
30373037
const repoName = path.basename(input.cwd);
30383038
const worktreePath = input.path ?? path.join(worktreesDir, repoName, sanitizedBranch);
3039-
const args = input.newRefName
3040-
? ["worktree", "add", "-b", input.newRefName, worktreePath, input.refName]
3041-
: ["worktree", "add", worktreePath, input.refName];
3039+
const lockReason = `t3code-create-${yield* crypto.randomUUIDv4.pipe(Effect.orDie)}`;
3040+
const args = [
3041+
"worktree",
3042+
"add",
3043+
"--lock",
3044+
"--reason",
3045+
lockReason,
3046+
...(input.newRefName ? ["-b", input.newRefName] : []),
3047+
worktreePath,
3048+
input.refName,
3049+
];
30423050
const progress = options?.progress;
30433051
const onCheckoutProgress = progress?.onCheckoutProgress;
30443052

30453053
const checkoutWorkers = (yield* readConfigValue(input.cwd, "checkout.workers")) ?? "0";
3046-
const registeredWorktrees = yield* runGitStdout(
3047-
"GitVcsDriver.createWorktree.registeredPaths",
3048-
input.cwd,
3049-
["worktree", "list", "--porcelain", "-z"],
3050-
);
3051-
const worktreeAlreadyRegistered =
3052-
registeredWorktrees
3053-
.split("\0")
3054-
.some(
3055-
(field) =>
3056-
field.startsWith("worktree ") &&
3057-
path.resolve(field.slice("worktree ".length)) === path.resolve(worktreePath),
3058-
) ||
3059-
(yield* fileSystem
3060-
.exists(path.join(worktreePath, ".git"))
3061-
.pipe(Effect.orElseSucceed(() => true)));
30623054
yield* executeGit(
30633055
"GitVcsDriver.createWorktree",
30643056
input.cwd,
@@ -3082,12 +3074,41 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
30823074
},
30833075
).pipe(
30843076
Effect.tap(() => progress?.onWorktreeClaimed?.(worktreePath) ?? Effect.void),
3085-
Effect.onInterrupt(() =>
3086-
worktreeAlreadyRegistered
3087-
? Effect.void
3088-
: removeWorktree({ cwd: input.cwd, path: worktreePath, force: true }).pipe(
3089-
Effect.ignoreCause({ log: true }),
3090-
),
3077+
Effect.onExit((exit) =>
3078+
Effect.gen(function* () {
3079+
if (!Exit.isSuccess(exit)) {
3080+
const registeredWorktrees = yield* runGitStdout(
3081+
"GitVcsDriver.createWorktree.registeredPaths",
3082+
input.cwd,
3083+
["worktree", "list", "--porcelain", "-z"],
3084+
);
3085+
const matchingPaths = registeredWorktrees
3086+
.split("\0\0")
3087+
.filter((record) =>
3088+
record
3089+
.split("\0")
3090+
.some(
3091+
(field) =>
3092+
field.startsWith("worktree ") &&
3093+
path.resolve(field.slice("worktree ".length)) === path.resolve(worktreePath),
3094+
),
3095+
);
3096+
if (
3097+
matchingPaths.length !== 1 ||
3098+
!matchingPaths[0]?.split("\0").includes(`locked ${lockReason}`)
3099+
) {
3100+
return;
3101+
}
3102+
}
3103+
yield* runGit("GitVcsDriver.createWorktree.unlock", input.cwd, [
3104+
"worktree",
3105+
"unlock",
3106+
worktreePath,
3107+
]);
3108+
if (Exit.hasInterrupts(exit)) {
3109+
yield* removeWorktree({ cwd: input.cwd, path: worktreePath, force: true });
3110+
}
3111+
}).pipe(Effect.ignoreCause({ log: true })),
30913112
),
30923113
);
30933114

0 commit comments

Comments
 (0)