Skip to content

Commit 6d12bb1

Browse files
t3dotggclaude
andauthored
fix(server): Stop during a restart no longer leaves the thread stuck (#27141)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 36dac68 commit 6d12bb1

2 files changed

Lines changed: 151 additions & 0 deletions

File tree

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11062,6 +11062,45 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1106211062
occurredAt: now,
1106311063
payload: { ...run, status: "interrupted", completedAt: now },
1106411064
});
11065+
// Stop cancels a pending restart, so it must end the turn that restart could not stop.
11066+
const supersededTurn = projection.providerTurns.findLast(
11067+
(candidate) =>
11068+
candidate.status === "running" &&
11069+
projection.attempts.some(
11070+
(attempt) => attempt.id === candidate.runAttemptId && attempt.runId === run.id,
11071+
),
11072+
);
11073+
const supersededOwner =
11074+
supersededTurn === undefined
11075+
? undefined
11076+
: projection.providerThreads.find(
11077+
(candidate) => candidate.id === supersededTurn.providerThreadId,
11078+
);
11079+
if (
11080+
supersededTurn !== undefined &&
11081+
supersededOwner !== undefined &&
11082+
supersededOwner.providerSessionId !== null &&
11083+
Option.isSome(
11084+
yield* providerSessions
11085+
.get(supersededOwner.providerSessionId)
11086+
.pipe(Effect.orElseSucceed(() => Option.none())),
11087+
)
11088+
) {
11089+
const id = `effect:${command.commandId}:provider-turn.interrupt:${supersededTurn.id}`;
11090+
if (!otherProviderInterrupts.some((effect) => effect.id === id)) {
11091+
otherProviderInterrupts.push({
11092+
id,
11093+
commandId: command.commandId,
11094+
threadId: command.threadId,
11095+
request: {
11096+
type: "provider-turn.interrupt",
11097+
providerSessionId: supersededOwner.providerSessionId,
11098+
providerThreadId: supersededOwner.id,
11099+
providerTurnId: supersededTurn.id,
11100+
},
11101+
});
11102+
}
11103+
}
1106511104
yield* stopRemainingWork();
1106611105
yield* Ref.update(effects, (existing) => [...existing, ...otherProviderInterrupts]);
1106711106
return {

‎apps/server/src/orchestration-v2/SelectionRestart.integration.test.ts‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,118 @@ it.live("retries a failed selection restart session open before completing", ()
438438
),
439439
);
440440

441+
it.live("Stop during a restart interrupts the turn the restart could not stop", () =>
442+
Effect.scoped(
443+
Effect.gen(function* () {
444+
const cwd = yield* checkpointWorkspace("selection-restart-stop");
445+
const threadId = ThreadId.make("thread:selection-restart-stop");
446+
const state = yield* Ref.make<RestartAdapterState>({
447+
activeTurn: null,
448+
opened: [],
449+
started: [],
450+
closedSessionCount: 0,
451+
failedReplacementOpen: false,
452+
});
453+
const base = makeRestartAdapter(state);
454+
// A wedged provider ignores the restart's soft cancel; only Stop ends its turn.
455+
const adapter: ProviderAdapter.ProviderAdapterV2["Service"] = {
456+
...base,
457+
openSession: (input) =>
458+
base.openSession(input).pipe(
459+
Effect.map((session) => ({
460+
...session,
461+
interruptTurn: (interrupt: ProviderAdapter.ProviderAdapterV2InterruptInput) =>
462+
interrupt.requestRuntimeRestart === true
463+
? session.interruptTurn(interrupt)
464+
: Effect.never,
465+
})),
466+
),
467+
};
468+
469+
const result = yield* Effect.gen(function* () {
470+
const orchestrator = yield* Orchestrator.OrchestratorV2;
471+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
472+
const running = yield* orchestrator.streamDomainEvents.pipe(
473+
Stream.filter(
474+
(event) => event.type === "provider-turn.updated" && event.payload.status === "running",
475+
),
476+
Stream.take(1),
477+
Stream.runDrain,
478+
Effect.forkScoped({ startImmediately: true }),
479+
);
480+
yield* orchestrator.dispatch({
481+
type: "thread.create",
482+
createdBy: "user",
483+
creationSource: "web",
484+
commandId: CommandId.make("command:selection-restart-stop:create"),
485+
threadId,
486+
projectId: ProjectId.make("project:selection-restart-stop"),
487+
title: "Stop during restart",
488+
modelSelection: initialSelection,
489+
runtimeMode: "full-access",
490+
interactionMode: "default",
491+
branch: null,
492+
worktreePath: cwd,
493+
});
494+
yield* orchestrator.dispatch({
495+
type: "message.dispatch",
496+
createdBy: "user",
497+
creationSource: "web",
498+
commandId: CommandId.make("command:selection-restart-stop:first"),
499+
threadId,
500+
messageId: MessageId.make("message:selection-restart-stop:first"),
501+
text: "first",
502+
attachments: [],
503+
modelSelection: initialSelection,
504+
dispatchMode: { type: "start_immediately" },
505+
});
506+
yield* worker.drain();
507+
yield* Fiber.join(running);
508+
const runId = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!.id;
509+
510+
yield* orchestrator.dispatch({
511+
type: "message.dispatch",
512+
createdBy: "user",
513+
creationSource: "web",
514+
commandId: CommandId.make("command:selection-restart-stop:steer"),
515+
threadId,
516+
messageId: MessageId.make("message:selection-restart-stop:steer"),
517+
text: "Are you still going?",
518+
attachments: [],
519+
modelSelection: initialSelection,
520+
dispatchMode: { type: "restart_active", targetRunId: runId },
521+
});
522+
yield* orchestrator.dispatch({
523+
type: "run.interrupt",
524+
commandId: CommandId.make("command:selection-restart-stop:stop"),
525+
threadId,
526+
runId,
527+
});
528+
yield* worker.drain();
529+
return {
530+
projection: yield* orchestrator.getThreadProjection(threadId),
531+
captured: yield* Ref.get(state),
532+
};
533+
}).pipe(
534+
Effect.provide(
535+
ProviderReplayHarness.layerWithRegistry(
536+
{ name: "selection-restart-stop" },
537+
ProviderAdapterRegistry.layerSingle(adapter),
538+
),
539+
),
540+
);
541+
542+
assert.isNull(result.captured.activeTurn);
543+
assert.lengthOf(result.captured.started, 1);
544+
assert.equal(result.projection.runs[0]?.status, "interrupted");
545+
assert.deepEqual(
546+
result.projection.providerTurns.map((turn) => turn.status),
547+
["interrupted"],
548+
);
549+
}),
550+
),
551+
);
552+
441553
it.live(
442554
"restarts the live session on a model change when a newer stopped session record exists",
443555
() =>

0 commit comments

Comments
 (0)