Skip to content

Commit 84e58a0

Browse files
rianradeckclaude
andcommitted
fix(server): threads launched into an existing worktree record its branch
A launch with workspaceStrategy existing_worktree and no branch stored branch null, because the server never read the checkout. Threads with no branch get no PR link or status, never settle on merge, show no branch in the sidebar, and keep their worktree in storage cleanup. Read the branch the worktree has checked out when the launch binds it. A detached HEAD records no branch, and a failed read logs a warning and records none. A branch read from the checkout is never renamed by the launch, even a temporary t3/<hex> one: this launch did not create it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2d0efd3 commit 84e58a0

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ function makeHarness(options: HarnessOptions = {}) {
211211
resolveWorktreeBaseRemote: () => Effect.succeed({ remoteName: "origin", branchName: "main" }),
212212
remoteExists: options.remoteExists ?? (() => Effect.succeed(true)),
213213
remoteBranchExists: () => Effect.succeed(true),
214+
currentBranch: () => Effect.succeed(null),
214215
removeWorktree,
215216
deleteLocalBranch,
216217
...options.git,
@@ -1056,6 +1057,48 @@ it.effect("runs a Scratch scheduled task saved with a worktree in its own folder
10561057
}).pipe(Effect.provide(harness.layer));
10571058
});
10581059

1060+
it.effect.each([
1061+
{ case: "its checked-out branch", head: "feat/agent-made", expected: "feat/agent-made" },
1062+
{ case: "its temporary branch, unrenamed", head: "t3/1234abcd", expected: "t3/1234abcd" },
1063+
{ case: "no branch when HEAD is unreadable", head: null, expected: null },
1064+
{ case: "the branch the caller passed", branch: "feat/explicit", expected: "feat/explicit" },
1065+
])("binds an existing worktree with $case", ({ head, branch, expected }) => {
1066+
const worktreePath = "/repo-worktrees/agent-made";
1067+
const harness = makeHarness({
1068+
git: {
1069+
currentBranch: (cwd) =>
1070+
cwd !== worktreePath || head === undefined
1071+
? Effect.die(`read the branch of ${cwd}`)
1072+
: head !== null
1073+
? Effect.succeed(head)
1074+
: Effect.fail(
1075+
new GitCommandError({
1076+
operation: "GitWorkflowService.currentBranch",
1077+
command: "git symbolic-ref",
1078+
cwd,
1079+
detail: "Failed to read the checked-out branch.",
1080+
}),
1081+
),
1082+
},
1083+
});
1084+
return Effect.gen(function* () {
1085+
const launches = yield* ThreadLaunch.ThreadLaunchService;
1086+
const launched = yield* launches.launch(
1087+
launchInput({
1088+
command: "command:launch:existing-worktree",
1089+
thread: "thread:launch:existing-worktree",
1090+
message: "Keep going",
1091+
workspace: { type: "existing_worktree", worktreePath, ...(branch ? { branch } : {}) },
1092+
}),
1093+
);
1094+
const projection = yield* preparationSettled(launched.threadId);
1095+
assert.equal(projection.runs[0]?.status, "starting");
1096+
assert.equal(projection.thread.worktreePath, worktreePath);
1097+
assert.equal(projection.thread.branch, expected);
1098+
assert.equal(harness.generateBranchName.mock.calls.length, 0);
1099+
}).pipe(Effect.provide(harness.layer));
1100+
});
1101+
10591102
it.effect.each(["stable", "upstream/release"])(
10601103
"launches from %s with the fetched commit and remote PR base",
10611104
(baseRef) =>

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,26 @@ const make = Effect.gen(function* () {
373373

374374
const requestedBranch = input.workspaceStrategy.branch;
375375
let branch: string | null;
376+
// A branch read from an attached checkout is not this launch's to rename, even a temporary one.
377+
let branchFromCheckout = false;
376378
if (input.workspaceStrategy.type === "worktree" && requestedBranch === undefined) {
377379
const uuid = yield* randomUuidV4;
378380
branch = buildTemporaryWorktreeBranchName(() => uuid.replaceAll("-", ""));
381+
} else if (
382+
input.workspaceStrategy.type === "existing_worktree" &&
383+
requestedBranch === undefined
384+
) {
385+
const checkout = input.workspaceStrategy.worktreePath;
386+
branch = yield* git.currentBranch(checkout).pipe(
387+
Effect.catch((cause) =>
388+
Effect.logWarning("Failed to read the attached worktree's branch", {
389+
threadId,
390+
worktreePath: checkout,
391+
cause,
392+
}).pipe(Effect.as(null)),
393+
),
394+
);
395+
branchFromCheckout = true;
379396
} else {
380397
branch = requestedBranch ?? null;
381398
}
@@ -481,6 +498,7 @@ const make = Effect.gen(function* () {
481498

482499
if (
483500
reused === undefined &&
501+
!branchFromCheckout &&
484502
worktreePath !== null &&
485503
branch !== null &&
486504
initialMessage !== undefined &&

0 commit comments

Comments
 (0)