fix(server): settle cancelled worktree setup before rollback (#12176)

This commit is contained in:
Julius Marminge authored and GitHub committed 2026-09-16 18:42:24 -07:00
1 parent 962bf62921
commit 3bee4c286e
2 files changed
+39 -3

No files matched your search

+36 -3
View File
@@ -11614,9 +11614,22 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
);
it.effect.each([
{ caseName: "async setup scripts let the turn start before the script exits", async: true },
{ caseName: "sync setup scripts hold the turn until the script exits", async: false },
])("$caseName", ({ async }) =>
{
caseName: "async setup scripts let the turn start before the script exits",
async: true,
cancel: false,
},
{
caseName: "sync setup scripts hold the turn until the script exits",
async: false,
cancel: false,
},
{
caseName: "cancelling worktree setup publishes its outcome and cleans up the thread",
async: false,
cancel: true,
},
])("$caseName", ({ async, cancel }) =>
Effect.gen(function* () {
const dispatchedCommands: Array<OrchestrationCommand> = [];
const scriptExit = yield* Deferred.make<void>();
@@ -11750,6 +11763,26 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
assert.equal(stageStatus(running, "agent"), "pending");
assert.isFalse(turnStarted());
if (cancel) {
const cancelled = yield* Effect.scoped(
withWsRpcClient(wsUrl, (client) => client[WS_METHODS.worktreeSetupCancel]({ threadId })),
);
assert.isTrue(cancelled.cancelled);
assertTrue(dispatchedCommands.some((command) => command.type === "thread.delete"));
const outcome = dispatchedCommands.findLast(
(command) =>
command.type === "thread.activity.append" && command.activity.kind === "worktree-setup",
);
assertTrue(outcome?.type === "thread.activity.append");
assert.propertyVal(outcome.activity.payload, "phase", "cancelled");
const result = yield* Fiber.join(dispatchFiber).pipe(Effect.result);
assertTrue(result._tag === "Failure");
assert.propertyVal(result.failure, "message", "Worktree setup cancelled.");
assert.propertyVal(result.failure, "bootstrapThreadDisposition", "deleted");
assert.isFalse(turnStarted());
return;
}
// The client that sent the message goes away mid-setup (a reload or a
// dropped socket). The bootstrap belongs to the server, not the
// connection: the thread already exists for every client, so it must
+3
View File
@@ -1620,6 +1620,7 @@ const makeWsRpcLayer = (
);
const settledBootstrapProgram = bootstrapProgram.pipe(
Effect.interruptible,
Effect.catchCause((cause) => {
const dispatchError = toBootstrapDispatchCommandCauseError(cause);
if (Cause.hasInterruptsOnly(cause)) {
@@ -1687,6 +1688,8 @@ const makeWsRpcLayer = (
),
).pipe(Effect.andThen(cleanupAndFail(cause, dispatchError)));
}),
// Cancellation must finish recording and rollback after the bootstrap is interrupted.
Effect.uninterruptible,
);
// The bootstrap outlives the connection that asked for it: a reload