Skip to content

Commit 2e7b142

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 458b64b commit 2e7b142

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
@@ -107,6 +107,7 @@ interface HarnessOptions {
107107
readonly fetchRemote?: GitWorkflow.GitWorkflowService["Service"]["fetchRemote"];
108108
readonly hasCommit?: GitWorkflow.GitWorkflowService["Service"]["hasCommit"];
109109
readonly renameBranch?: GitWorkflow.GitWorkflowService["Service"]["renameBranch"];
110+
readonly localStatus?: GitWorkflow.GitWorkflowService["Service"]["localStatus"];
110111
readonly runSetup?: ProjectSetupScriptRunner.ProjectSetupScriptRunner["Service"]["runForThread"];
111112
readonly generateTitle?: TextGeneration.TextGeneration["Service"]["generateThreadTitle"];
112113
readonly generateBranchName?: TextGeneration.TextGeneration["Service"]["generateBranchName"];
@@ -131,16 +132,46 @@ function makeHarness(options: HarnessOptions = {}) {
131132
Layer.provide(layerReceipts),
132133
);
133134
const layerOutbox = EffectOutbox.layer.pipe(Layer.provide(layerDatabase));
135+
// The branch-rename flow re-checks this right before renaming (#11078
136+
// review): this harness never actually runs git, so it is tracked here
137+
// instead, starting at the literal most tests give an existing_worktree
138+
// (no createWorktree call to learn it from otherwise) and updated to
139+
// match whatever a new worktree actually gets checked out on, including a
140+
// server-generated name the client never specified.
141+
let liveBranch = "t3code/abcd1234";
134142
const createWorktree = vi.fn(
135-
options.createWorktree ??
136-
((input) =>
137-
Effect.succeed({
138-
worktree: { path: "/repo-worktrees/feature", refName: input.newRefName, headSha: "abc" },
139-
} as never)),
143+
(
144+
...args: Parameters<GitWorkflow.GitWorkflowService["Service"]["createWorktree"]>
145+
): ReturnType<GitWorkflow.GitWorkflowService["Service"]["createWorktree"]> => {
146+
liveBranch = args[0].newRefName ?? liveBranch;
147+
return (
148+
options.createWorktree ??
149+
((input) =>
150+
Effect.succeed({
151+
worktree: {
152+
path: "/repo-worktrees/feature",
153+
refName: input.newRefName,
154+
headSha: "abc",
155+
},
156+
} as never))
157+
)(...args);
158+
},
140159
);
141160
const renameBranch = vi.fn(
142161
options.renameBranch ?? ((input) => Effect.succeed({ branch: input.newBranch })),
143162
);
163+
const localStatus = vi.fn(
164+
options.localStatus ??
165+
(() =>
166+
Effect.succeed({
167+
isRepo: true,
168+
hasPrimaryRemote: true,
169+
isDefaultRef: false,
170+
refName: liveBranch,
171+
hasWorkingTreeChanges: false,
172+
workingTree: { files: [], insertions: 0, deletions: 0 },
173+
} as never)),
174+
);
144175
const removeWorktree = vi.fn(
145176
(_input: Parameters<GitWorkflow.GitWorkflowService["Service"]["removeWorktree"]>[0]) =>
146177
Effect.void,
@@ -180,6 +211,7 @@ function makeHarness(options: HarnessOptions = {}) {
180211
Layer.mock(GitWorkflow.GitWorkflowService)({
181212
createWorktree,
182213
renameBranch,
214+
localStatus,
183215
fetchRemote: options.fetchRemote ?? (() => Effect.void),
184216
hasCommit: options.hasCommit ?? (() => Effect.succeed(false)),
185217
remoteExists: () => Effect.succeed(true),
@@ -253,6 +285,7 @@ function makeHarness(options: HarnessOptions = {}) {
253285
createWorktree,
254286
removeWorktree,
255287
renameBranch,
288+
localStatus,
256289
generateBranchName,
257290
generateThreadTitle,
258291
runSetup,
@@ -1299,6 +1332,61 @@ it.effect(
12991332
}),
13001333
);
13011334

1335+
it.effect(
1336+
"skips the background branch rename itself once the worktree's real branch has already moved (#11078 review)",
1337+
() =>
1338+
Effect.gen(function* () {
1339+
const branchNameStarted = yield* Deferred.make<void>();
1340+
const allowBranchName = yield* Deferred.make<void>();
1341+
const harness = makeHarness({
1342+
createWorktree: (input) =>
1343+
Effect.succeed({
1344+
worktree: { path: "/repo-worktrees/temp", refName: input.newRefName, headSha: "abc" },
1345+
} as never),
1346+
generateBranchName: () =>
1347+
Deferred.succeed(branchNameStarted, undefined).pipe(
1348+
Effect.andThen(Deferred.await(allowBranchName)),
1349+
Effect.as({ branch: "generated-branch" }),
1350+
),
1351+
// The agent checked out a real branch itself during the first turn,
1352+
// before followBranchDrift (which only runs at turn end, unlike this
1353+
// rename) has had any chance to correct the thread's stamp — so a
1354+
// stamp-only check here would still see the temp branch and miss it.
1355+
localStatus: () =>
1356+
Effect.succeed({
1357+
isRepo: true,
1358+
hasPrimaryRemote: true,
1359+
isDefaultRef: false,
1360+
refName: "feature/agent-chosen",
1361+
hasWorkingTreeChanges: false,
1362+
workingTree: { files: [], insertions: 0, deletions: 0 },
1363+
} as never),
1364+
});
1365+
yield* Effect.gen(function* () {
1366+
const launches = yield* ThreadLaunch.ThreadLaunchService;
1367+
const threads = yield* ThreadManagement.ThreadManagementService;
1368+
const launched = yield* launches.launch(
1369+
launchInput({
1370+
command: "command:launch:real-branch-moved",
1371+
thread: "thread:launch:real-branch-moved",
1372+
message: "Build the feature",
1373+
workspace: { type: "worktree", baseRef: "main", branch: "t3code/abcd1234" },
1374+
}),
1375+
);
1376+
yield* Deferred.await(branchNameStarted);
1377+
yield* Deferred.succeed(allowBranchName, undefined);
1378+
yield* waitUntil(() => Effect.sync(() => harness.localStatus.mock.calls.length > 0));
1379+
// The actual `git branch -m` never runs against a ref the agent has
1380+
// already checked off of.
1381+
assert.equal(harness.renameBranch.mock.calls.length, 0);
1382+
assert.equal(
1383+
(yield* threads.getThreadProjection(launched.threadId)).thread.branch,
1384+
"t3code/abcd1234",
1385+
);
1386+
}).pipe(Effect.provide(harness.layer));
1387+
}),
1388+
);
1389+
13021390
it.effect("keeps an explicit branch name instead of generating one", () =>
13031391
Effect.gen(function* () {
13041392
const harness = makeHarness();

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

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -510,29 +510,43 @@ const make = Effect.gen(function* () {
510510
const worktreeCwd = worktreePath;
511511
yield* generateBranchNameFor(worktreeCwd, initialMessage).pipe(
512512
Effect.flatMap(({ branch: newBranch, exactName }) =>
513-
git.renameBranch({
514-
cwd: worktreeCwd,
515-
oldBranch,
516-
newBranch,
517-
...(exactName ? { exactName: true } : {}),
518-
}),
519-
),
520-
Effect.flatMap((renamed) =>
521-
threads.dispatch({
522-
type: "thread.metadata.update",
523-
commandId: CommandId.make(`${input.commandId}:branch-rename`),
524-
threadId,
525-
branch: renamed.branch,
526-
worktreePath: worktreeCwd,
527-
// Generation and the actual `git branch -m` both take a beat,
528-
// during which the agent can check out a real branch itself
529-
// (followBranchDrift then corrects the stamp before this
530-
// lands). A compare-and-swap drops the rename instead of
531-
// overwriting that correction with a branch nothing has
532-
// checked out.
533-
expectedBranch: oldBranch,
534-
expectedWorktreePath: worktreeCwd,
535-
}),
513+
// Generation takes a beat, during which the agent can check out
514+
// a real branch itself. Re-checking right before the git rename
515+
// (rather than trusting the stamp this task started with) keeps
516+
// the rename from rewriting a ref nothing has checked out
517+
// anymore (#11078 review); the compare-and-swap below is the
518+
// same guard for the thread's metadata stamp.
519+
git.localStatus({ cwd: worktreeCwd }).pipe(
520+
Effect.flatMap((local) =>
521+
local.refName !== oldBranch
522+
? Effect.void
523+
: git
524+
.renameBranch({
525+
cwd: worktreeCwd,
526+
oldBranch,
527+
newBranch,
528+
...(exactName ? { exactName: true } : {}),
529+
})
530+
.pipe(
531+
Effect.flatMap((renamed) =>
532+
threads.dispatch({
533+
type: "thread.metadata.update",
534+
commandId: CommandId.make(`${input.commandId}:branch-rename`),
535+
threadId,
536+
branch: renamed.branch,
537+
worktreePath: worktreeCwd,
538+
// followBranchDrift can still land between this
539+
// check and the rename+dispatch below; this
540+
// compare-and-swap drops the rename instead of
541+
// overwriting that correction.
542+
expectedBranch: oldBranch,
543+
expectedWorktreePath: worktreeCwd,
544+
}),
545+
),
546+
Effect.asVoid,
547+
),
548+
),
549+
),
536550
),
537551
Effect.catchCause((cause) =>
538552
Effect.logWarning("Thread worktree branch rename failed", {

0 commit comments

Comments
 (0)