Skip to content

Commit 8212aaa

Browse files
fix(server): a new worktree thread's agent starts on its renamed branch (pingdotgg#34781)
1 parent 4bf5d53 commit 8212aaa

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2353,6 +2353,57 @@ it.effect("stops an in-flight branch rename when tracked setup is cancelled", ()
23532353
}),
23542354
);
23552355

2356+
it.effect("starts the agent of a new worktree thread only after its branch is renamed", () =>
2357+
Effect.gen(function* () {
2358+
const branchNameStarted = yield* Deferred.make<void>();
2359+
const allowBranchName = yield* Deferred.make<void>();
2360+
const releasedAtRename = yield* Deferred.make<boolean>();
2361+
let releaseEffects: Effect.Effect<ReadonlyArray<unknown>> = Effect.succeed([]);
2362+
const harness = makeHarness({
2363+
generateBranchName: () =>
2364+
Deferred.succeed(branchNameStarted, undefined).pipe(
2365+
Effect.andThen(Deferred.await(allowBranchName)),
2366+
Effect.as({ branch: "generated-branch" }),
2367+
),
2368+
renameBranch: (input) =>
2369+
Effect.suspend(() => releaseEffects).pipe(
2370+
Effect.flatMap((effects) => Deferred.succeed(releasedAtRename, effects.length > 0)),
2371+
Effect.as({ branch: input.newBranch }),
2372+
),
2373+
});
2374+
yield* Effect.gen(function* () {
2375+
const launches = yield* ThreadLaunch.ThreadLaunchService;
2376+
const tracker = yield* WorktreeSetupTracker.WorktreeSetupTracker;
2377+
const outbox = yield* EffectOutbox.EffectOutboxV2;
2378+
const input = launchInput({
2379+
command: "launch:release-after-rename",
2380+
thread: "thread:release-after-rename",
2381+
message: "Start",
2382+
workspace: { type: "worktree", baseRef: "main" },
2383+
});
2384+
releaseEffects = outbox
2385+
.listByCommandId(CommandId.make(`${input.commandId}:release`))
2386+
.pipe(Effect.orDie);
2387+
const launched = yield* launches.launch(input);
2388+
yield* Deferred.await(branchNameStarted);
2389+
yield* tracker.stream(launched.threadId).pipe(
2390+
Stream.filter(
2391+
(snapshot) =>
2392+
snapshot?.stages.some(
2393+
(stage) => stage.id === "setup-script" && stage.status === "skipped",
2394+
) === true,
2395+
),
2396+
Stream.runHead,
2397+
);
2398+
yield* Deferred.succeed(allowBranchName, undefined);
2399+
assert.isFalse(
2400+
yield* Deferred.await(releasedAtRename),
2401+
"the agent's run was released while its branch still had the temporary name",
2402+
);
2403+
}).pipe(Effect.provide(harness.layer));
2404+
}),
2405+
);
2406+
23562407
it.effect.each([0, 1])("releases an async setup before its completion with exit %s", (exitCode) =>
23572408
Effect.gen(function* () {
23582409
const completion = yield* Deferred.make<{ exitCode: number | null; durationMs: number }>();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,8 @@ const make = Effect.gen(function* () {
719719
} else {
720720
yield* setupTracker.stageStatus(threadId, "setup-script", "skipped");
721721
}
722+
// The agent must start on the branch name the thread will show.
723+
if (branchRename !== null) yield* Fiber.join(branchRename);
722724
yield* setupTracker.markUncancellable(threadId);
723725
yield* setupTracker.stageStatus(threadId, "agent", "running");
724726
if (runId !== null) {

0 commit comments

Comments
 (0)