Skip to content

Commit 0430cb1

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 a0c423d commit 0430cb1

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
@@ -102,6 +102,7 @@ interface HarnessOptions {
102102
readonly fetchRemote?: GitWorkflow.GitWorkflowService["Service"]["fetchRemote"];
103103
readonly hasCommit?: GitWorkflow.GitWorkflowService["Service"]["hasCommit"];
104104
readonly renameBranch?: GitWorkflow.GitWorkflowService["Service"]["renameBranch"];
105+
readonly localStatus?: GitWorkflow.GitWorkflowService["Service"]["localStatus"];
105106
readonly runSetup?: ProjectSetupScriptRunner.ProjectSetupScriptRunner["Service"]["runForThread"];
106107
readonly generateTitle?: TextGeneration.TextGeneration["Service"]["generateThreadTitle"];
107108
readonly generateBranchName?: TextGeneration.TextGeneration["Service"]["generateBranchName"];
@@ -120,16 +121,46 @@ function makeHarness(options: HarnessOptions = {}) {
120121
const layerThreadManagement = ThreadManagement.layer.pipe(Layer.provide(layerOrchestrator));
121122
const layerReceipts = CommandReceiptStore.layer.pipe(Layer.provide(layerDatabase));
122123
const layerOutbox = EffectOutbox.layer.pipe(Layer.provide(layerDatabase));
124+
// The branch-rename flow re-checks this right before renaming (#11078
125+
// review): this harness never actually runs git, so it is tracked here
126+
// instead, starting at the literal most tests give an existing_worktree
127+
// (no createWorktree call to learn it from otherwise) and updated to
128+
// match whatever a new worktree actually gets checked out on, including a
129+
// server-generated name the client never specified.
130+
let liveBranch = "t3code/abcd1234";
123131
const createWorktree = vi.fn(
124-
options.createWorktree ??
125-
((input) =>
126-
Effect.succeed({
127-
worktree: { path: "/repo-worktrees/feature", refName: input.newRefName, headSha: "abc" },
128-
} as never)),
132+
(
133+
...args: Parameters<GitWorkflow.GitWorkflowService["Service"]["createWorktree"]>
134+
): ReturnType<GitWorkflow.GitWorkflowService["Service"]["createWorktree"]> => {
135+
liveBranch = args[0].newRefName ?? liveBranch;
136+
return (
137+
options.createWorktree ??
138+
((input) =>
139+
Effect.succeed({
140+
worktree: {
141+
path: "/repo-worktrees/feature",
142+
refName: input.newRefName,
143+
headSha: "abc",
144+
},
145+
} as never))
146+
)(...args);
147+
},
129148
);
130149
const renameBranch = vi.fn(
131150
options.renameBranch ?? ((input) => Effect.succeed({ branch: input.newBranch })),
132151
);
152+
const localStatus = vi.fn(
153+
options.localStatus ??
154+
(() =>
155+
Effect.succeed({
156+
isRepo: true,
157+
hasPrimaryRemote: true,
158+
isDefaultRef: false,
159+
refName: liveBranch,
160+
hasWorkingTreeChanges: false,
161+
workingTree: { files: [], insertions: 0, deletions: 0 },
162+
} as never)),
163+
);
133164
const removeWorktree = vi.fn(
134165
(_input: Parameters<GitWorkflow.GitWorkflowService["Service"]["removeWorktree"]>[0]) =>
135166
Effect.void,
@@ -168,6 +199,7 @@ function makeHarness(options: HarnessOptions = {}) {
168199
Layer.mock(GitWorkflow.GitWorkflowService)({
169200
createWorktree,
170201
renameBranch,
202+
localStatus,
171203
fetchRemote: options.fetchRemote ?? (() => Effect.void),
172204
hasCommit: options.hasCommit ?? (() => Effect.succeed(false)),
173205
remoteExists: () => Effect.succeed(true),
@@ -239,6 +271,7 @@ function makeHarness(options: HarnessOptions = {}) {
239271
createWorktree,
240272
removeWorktree,
241273
renameBranch,
274+
localStatus,
242275
generateBranchName,
243276
generateThreadTitle,
244277
runSetup,
@@ -1284,6 +1317,61 @@ it.effect(
12841317
}),
12851318
);
12861319

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

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

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -439,29 +439,43 @@ const make = Effect.gen(function* () {
439439
const worktreeCwd = worktreePath;
440440
yield* generateBranchNameFor(worktreeCwd, initialMessage).pipe(
441441
Effect.flatMap(({ branch: newBranch, exactName }) =>
442-
git.renameBranch({
443-
cwd: worktreeCwd,
444-
oldBranch,
445-
newBranch,
446-
...(exactName ? { exactName: true } : {}),
447-
}),
448-
),
449-
Effect.flatMap((renamed) =>
450-
threads.dispatch({
451-
type: "thread.metadata.update",
452-
commandId: CommandId.make(`${input.commandId}:branch-rename`),
453-
threadId,
454-
branch: renamed.branch,
455-
worktreePath: worktreeCwd,
456-
// Generation and the actual `git branch -m` both take a beat,
457-
// during which the agent can check out a real branch itself
458-
// (followBranchDrift then corrects the stamp before this
459-
// lands). A compare-and-swap drops the rename instead of
460-
// overwriting that correction with a branch nothing has
461-
// checked out.
462-
expectedBranch: oldBranch,
463-
expectedWorktreePath: worktreeCwd,
464-
}),
442+
// Generation takes a beat, during which the agent can check out
443+
// a real branch itself. Re-checking right before the git rename
444+
// (rather than trusting the stamp this task started with) keeps
445+
// the rename from rewriting a ref nothing has checked out
446+
// anymore (#11078 review); the compare-and-swap below is the
447+
// same guard for the thread's metadata stamp.
448+
git.localStatus({ cwd: worktreeCwd }).pipe(
449+
Effect.flatMap((local) =>
450+
local.refName !== oldBranch
451+
? Effect.void
452+
: git
453+
.renameBranch({
454+
cwd: worktreeCwd,
455+
oldBranch,
456+
newBranch,
457+
...(exactName ? { exactName: true } : {}),
458+
})
459+
.pipe(
460+
Effect.flatMap((renamed) =>
461+
threads.dispatch({
462+
type: "thread.metadata.update",
463+
commandId: CommandId.make(`${input.commandId}:branch-rename`),
464+
threadId,
465+
branch: renamed.branch,
466+
worktreePath: worktreeCwd,
467+
// followBranchDrift can still land between this
468+
// check and the rename+dispatch below; this
469+
// compare-and-swap drops the rename instead of
470+
// overwriting that correction.
471+
expectedBranch: oldBranch,
472+
expectedWorktreePath: worktreeCwd,
473+
}),
474+
),
475+
Effect.asVoid,
476+
),
477+
),
478+
),
465479
),
466480
Effect.catchCause((cause) =>
467481
Effect.logWarning("Thread worktree branch rename failed", {

0 commit comments

Comments
 (0)