Skip to content

Commit 30d4195

Browse files
PR Batch Testerclaude
andcommitted
fix(server): skip the background branch rename once the real ref has moved (Macroscope #5)
git.renameBranch ran unconditionally after branch-name generation, by name, regardless of whether the worktree's actual checked-out branch was still oldBranch by then. The earlier compare-and-swap on the dispatch stops the thread's stamp from being overwritten, but the git rename itself would still fire and rewrite a ref the agent had already checked off of, since followBranchDrift only reconciles the stamp at turn end and this rename does not wait for it. Re-check the worktree's actual branch right before renaming and skip the rename (and the dispatch) if it no longer matches oldBranch. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 94bbbbd commit 30d4195

2 files changed

Lines changed: 130 additions & 28 deletions

File tree

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

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ interface HarnessOptions {
100100
readonly createWorktree?: GitWorkflow.GitWorkflowService["Service"]["createWorktree"];
101101
readonly fetchRemote?: GitWorkflow.GitWorkflowService["Service"]["fetchRemote"];
102102
readonly renameBranch?: GitWorkflow.GitWorkflowService["Service"]["renameBranch"];
103+
readonly localStatus?: GitWorkflow.GitWorkflowService["Service"]["localStatus"];
103104
readonly runSetup?: ProjectSetupScriptRunner.ProjectSetupScriptRunner["Service"]["runForThread"];
104105
readonly generateTitle?: TextGeneration.TextGeneration["Service"]["generateThreadTitle"];
105106
readonly generateBranchName?: TextGeneration.TextGeneration["Service"]["generateBranchName"];
@@ -118,16 +119,46 @@ function makeHarness(options: HarnessOptions = {}) {
118119
const threadManagement = ThreadManagement.layer.pipe(Layer.provide(orchestrator));
119120
const receipts = CommandReceiptStore.layer.pipe(Layer.provide(database));
120121
const outbox = EffectOutbox.layer.pipe(Layer.provide(database));
122+
// The branch-rename flow re-checks this right before renaming (#11078
123+
// review): this harness never actually runs git, so it is tracked here
124+
// instead, starting at the literal most tests give an existing_worktree
125+
// (no createWorktree call to learn it from otherwise) and updated to
126+
// match whatever a new worktree actually gets checked out on, including a
127+
// server-generated name the client never specified.
128+
let liveBranch = "t3code/abcd1234";
121129
const createWorktree = vi.fn(
122-
options.createWorktree ??
123-
((input) =>
124-
Effect.succeed({
125-
worktree: { path: "/repo-worktrees/feature", refName: input.newRefName, headSha: "abc" },
126-
} as never)),
130+
(
131+
...args: Parameters<GitWorkflow.GitWorkflowService["Service"]["createWorktree"]>
132+
): ReturnType<GitWorkflow.GitWorkflowService["Service"]["createWorktree"]> => {
133+
liveBranch = args[0].newRefName ?? liveBranch;
134+
return (
135+
options.createWorktree ??
136+
((input) =>
137+
Effect.succeed({
138+
worktree: {
139+
path: "/repo-worktrees/feature",
140+
refName: input.newRefName,
141+
headSha: "abc",
142+
},
143+
} as never))
144+
)(...args);
145+
},
127146
);
128147
const renameBranch = vi.fn(
129148
options.renameBranch ?? ((input) => Effect.succeed({ branch: input.newBranch })),
130149
);
150+
const localStatus = vi.fn(
151+
options.localStatus ??
152+
(() =>
153+
Effect.succeed({
154+
isRepo: true,
155+
hasPrimaryRemote: true,
156+
isDefaultRef: false,
157+
refName: liveBranch,
158+
hasWorkingTreeChanges: false,
159+
workingTree: { files: [], insertions: 0, deletions: 0 },
160+
} as never)),
161+
);
131162
const removeWorktree = vi.fn(
132163
(_input: Parameters<GitWorkflow.GitWorkflowService["Service"]["removeWorktree"]>[0]) =>
133164
Effect.void,
@@ -166,6 +197,7 @@ function makeHarness(options: HarnessOptions = {}) {
166197
Layer.mock(GitWorkflow.GitWorkflowService)({
167198
createWorktree,
168199
renameBranch,
200+
localStatus,
169201
fetchRemote: options.fetchRemote ?? (() => Effect.void),
170202
remoteExists: () => Effect.succeed(true),
171203
remoteBranchExists: () => Effect.succeed(true),
@@ -227,6 +259,7 @@ function makeHarness(options: HarnessOptions = {}) {
227259
createWorktree,
228260
removeWorktree,
229261
renameBranch,
262+
localStatus,
230263
generateBranchName,
231264
generateThreadTitle,
232265
runSetup,
@@ -1231,6 +1264,61 @@ it.effect(
12311264
}),
12321265
);
12331266

1267+
it.effect(
1268+
"skips the background branch rename itself once the worktree's real branch has already moved (#11078 review)",
1269+
() =>
1270+
Effect.gen(function* () {
1271+
const branchNameStarted = yield* Deferred.make<void>();
1272+
const allowBranchName = yield* Deferred.make<void>();
1273+
const harness = makeHarness({
1274+
createWorktree: (input) =>
1275+
Effect.succeed({
1276+
worktree: { path: "/repo-worktrees/temp", refName: input.newRefName, headSha: "abc" },
1277+
} as never),
1278+
generateBranchName: () =>
1279+
Deferred.succeed(branchNameStarted, undefined).pipe(
1280+
Effect.andThen(Deferred.await(allowBranchName)),
1281+
Effect.as({ branch: "generated-branch" }),
1282+
),
1283+
// The agent checked out a real branch itself during the first turn,
1284+
// before followBranchDrift (which only runs at turn end, unlike this
1285+
// rename) has had any chance to correct the thread's stamp — so a
1286+
// stamp-only check here would still see the temp branch and miss it.
1287+
localStatus: () =>
1288+
Effect.succeed({
1289+
isRepo: true,
1290+
hasPrimaryRemote: true,
1291+
isDefaultRef: false,
1292+
refName: "feature/agent-chosen",
1293+
hasWorkingTreeChanges: false,
1294+
workingTree: { files: [], insertions: 0, deletions: 0 },
1295+
} as never),
1296+
});
1297+
yield* Effect.gen(function* () {
1298+
const launches = yield* ThreadLaunch.ThreadLaunchService;
1299+
const threads = yield* ThreadManagement.ThreadManagementService;
1300+
const launched = yield* launches.launch(
1301+
launchInput({
1302+
command: "command:launch:real-branch-moved",
1303+
thread: "thread:launch:real-branch-moved",
1304+
message: "Build the feature",
1305+
workspace: { type: "worktree", baseRef: "main", branch: "t3code/abcd1234" },
1306+
}),
1307+
);
1308+
yield* Deferred.await(branchNameStarted);
1309+
yield* Deferred.succeed(allowBranchName, undefined);
1310+
yield* waitUntil(() => Effect.sync(() => harness.localStatus.mock.calls.length > 0));
1311+
// The actual `git branch -m` never runs against a ref the agent has
1312+
// already checked off of.
1313+
assert.equal(harness.renameBranch.mock.calls.length, 0);
1314+
assert.equal(
1315+
(yield* threads.getThreadProjection(launched.threadId)).thread.branch,
1316+
"t3code/abcd1234",
1317+
);
1318+
}).pipe(Effect.provide(harness.layer));
1319+
}),
1320+
);
1321+
12341322
it.effect("keeps an explicit branch name instead of generating one", () =>
12351323
Effect.gen(function* () {
12361324
const harness = makeHarness();

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

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -422,29 +422,43 @@ const make = Effect.gen(function* () {
422422
const worktreeCwd = worktreePath;
423423
yield* generateBranchNameFor(worktreeCwd, initialMessage).pipe(
424424
Effect.flatMap(({ branch: newBranch, exactName }) =>
425-
git.renameBranch({
426-
cwd: worktreeCwd,
427-
oldBranch,
428-
newBranch,
429-
...(exactName ? { exactName: true } : {}),
430-
}),
431-
),
432-
Effect.flatMap((renamed) =>
433-
threads.dispatch({
434-
type: "thread.metadata.update",
435-
commandId: CommandId.make(`${input.commandId}:branch-rename`),
436-
threadId,
437-
branch: renamed.branch,
438-
worktreePath: worktreeCwd,
439-
// Generation and the actual `git branch -m` both take a beat,
440-
// during which the agent can check out a real branch itself
441-
// (followBranchDrift then corrects the stamp before this
442-
// lands). A compare-and-swap drops the rename instead of
443-
// overwriting that correction with a branch nothing has
444-
// checked out.
445-
expectedBranch: oldBranch,
446-
expectedWorktreePath: worktreeCwd,
447-
}),
425+
// Generation takes a beat, during which the agent can check out
426+
// a real branch itself. Re-checking right before the git rename
427+
// (rather than trusting the stamp this task started with) keeps
428+
// the rename from rewriting a ref nothing has checked out
429+
// anymore (#11078 review); the compare-and-swap below is the
430+
// same guard for the thread's metadata stamp.
431+
git.localStatus({ cwd: worktreeCwd }).pipe(
432+
Effect.flatMap((local) =>
433+
local.refName !== oldBranch
434+
? Effect.void
435+
: git
436+
.renameBranch({
437+
cwd: worktreeCwd,
438+
oldBranch,
439+
newBranch,
440+
...(exactName ? { exactName: true } : {}),
441+
})
442+
.pipe(
443+
Effect.flatMap((renamed) =>
444+
threads.dispatch({
445+
type: "thread.metadata.update",
446+
commandId: CommandId.make(`${input.commandId}:branch-rename`),
447+
threadId,
448+
branch: renamed.branch,
449+
worktreePath: worktreeCwd,
450+
// followBranchDrift can still land between this
451+
// check and the rename+dispatch below; this
452+
// compare-and-swap drops the rename instead of
453+
// overwriting that correction.
454+
expectedBranch: oldBranch,
455+
expectedWorktreePath: worktreeCwd,
456+
}),
457+
),
458+
Effect.asVoid,
459+
),
460+
),
461+
),
448462
),
449463
Effect.catchCause((cause) =>
450464
Effect.logWarning("Thread worktree branch rename failed", {

0 commit comments

Comments
 (0)