Skip to content

Commit 94c2cb0

Browse files
committed
fix(server): preserve completed session after interrupt failure
1 parent c846287 commit 94c2cb0

2 files changed

Lines changed: 106 additions & 1 deletion

File tree

‎apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2565,6 +2565,87 @@ describe("ProviderCommandReactor", () => {
25652565
expect(harness.stopSession).toHaveBeenCalledWith({ threadId: ThreadId.make("thread-1") });
25662566
});
25672567

2568+
it("does not overwrite a session that became ready while an interrupt failed", async () => {
2569+
const harness = await createHarness();
2570+
const now = "2026-01-01T00:00:00.000Z";
2571+
const completedAt = "2026-01-01T00:00:01.000Z";
2572+
2573+
await Effect.runPromise(
2574+
harness.engine.dispatch({
2575+
type: "thread.session.set",
2576+
commandId: CommandId.make("cmd-session-set-interrupt-race"),
2577+
threadId: ThreadId.make("thread-1"),
2578+
session: {
2579+
threadId: ThreadId.make("thread-1"),
2580+
status: "running",
2581+
providerName: "codex",
2582+
runtimeMode: "approval-required",
2583+
activeTurnId: asTurnId("turn-1"),
2584+
lastError: null,
2585+
updatedAt: now,
2586+
},
2587+
createdAt: now,
2588+
}),
2589+
);
2590+
2591+
harness.interruptTurn.mockImplementation(() =>
2592+
harness.engine
2593+
.dispatch({
2594+
type: "thread.session.set",
2595+
commandId: CommandId.make("cmd-session-set-natural-completion"),
2596+
threadId: ThreadId.make("thread-1"),
2597+
session: {
2598+
threadId: ThreadId.make("thread-1"),
2599+
status: "ready",
2600+
providerName: "codex",
2601+
runtimeMode: "approval-required",
2602+
activeTurnId: null,
2603+
lastError: null,
2604+
updatedAt: completedAt,
2605+
},
2606+
createdAt: completedAt,
2607+
})
2608+
.pipe(
2609+
Effect.catchCause((cause) => Effect.die(cause)),
2610+
Effect.andThen(
2611+
Effect.fail(
2612+
new ProviderAdapterRequestError({
2613+
provider: "codex",
2614+
method: "thread.interrupt",
2615+
detail: "provider session disappeared",
2616+
}),
2617+
),
2618+
),
2619+
),
2620+
);
2621+
2622+
await Effect.runPromise(
2623+
harness.engine.dispatch({
2624+
type: "thread.turn.interrupt",
2625+
commandId: CommandId.make("cmd-turn-interrupt-race"),
2626+
threadId: ThreadId.make("thread-1"),
2627+
turnId: asTurnId("turn-1"),
2628+
createdAt: now,
2629+
}),
2630+
);
2631+
2632+
await harness.drain();
2633+
2634+
const thread = (await harness.readModel()).threads.find(
2635+
(entry) => entry.id === ThreadId.make("thread-1"),
2636+
);
2637+
expect(thread?.session).toMatchObject({
2638+
status: "ready",
2639+
activeTurnId: null,
2640+
lastError: null,
2641+
updatedAt: completedAt,
2642+
});
2643+
expect(harness.stopSession).not.toHaveBeenCalled();
2644+
expect(
2645+
thread?.activities.some((activity) => activity.kind === "provider.turn.interrupt.failed"),
2646+
).toBe(false);
2647+
});
2648+
25682649
it("starts a fresh session when only projected session state exists", async () => {
25692650
const harness = await createHarness();
25702651
const now = "2026-01-01T00:00:00.000Z";

‎apps/server/src/orchestration/Layers/ProviderCommandReactor.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,18 @@ const make = Effect.gen(function* () {
11991199

12001200
const detail = formatFailureDetail(cause);
12011201
return Effect.gen(function* () {
1202+
const latestThread = yield* resolveThread(event.payload.threadId);
1203+
const latestSession = latestThread?.session;
1204+
if (
1205+
!latestSession ||
1206+
latestSession.status === "stopped" ||
1207+
latestSession.activeTurnId === null ||
1208+
(event.payload.turnId !== undefined &&
1209+
latestSession.activeTurnId !== event.payload.turnId)
1210+
) {
1211+
return;
1212+
}
1213+
12021214
yield* providerService.stopSession({ threadId: event.payload.threadId }).pipe(
12031215
Effect.catchCause((stopCause) => {
12041216
if (Cause.hasInterruptsOnly(stopCause)) {
@@ -1214,10 +1226,22 @@ const make = Effect.gen(function* () {
12141226
);
12151227
}),
12161228
);
1229+
const stoppedThread = yield* resolveThread(event.payload.threadId);
1230+
const stoppedSession = stoppedThread?.session;
1231+
if (
1232+
!stoppedSession ||
1233+
stoppedSession.status === "stopped" ||
1234+
stoppedSession.activeTurnId === null ||
1235+
(event.payload.turnId !== undefined &&
1236+
stoppedSession.activeTurnId !== event.payload.turnId)
1237+
) {
1238+
return;
1239+
}
1240+
12171241
yield* setThreadSession({
12181242
threadId: event.payload.threadId,
12191243
session: {
1220-
...session,
1244+
...stoppedSession,
12211245
status: "stopped",
12221246
activeTurnId: null,
12231247
lastError: detail,

0 commit comments

Comments
 (0)