Skip to content

Commit e8675f1

Browse files
committed
fix(server): ignore late aborts after a turn finishes
1 parent 3c1884e commit e8675f1

2 files changed

Lines changed: 77 additions & 9 deletions

File tree

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

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ describe("ProviderRuntimeIngestion", () => {
427427
it.each([
428428
{ source: "the previous turn", turnId: asTurnId("opencode-stopped-turn") },
429429
{ source: "an unspecified turn", turnId: undefined },
430-
])("keeps a new OpenCode turn running after an abort for $source", async (lateAbort) => {
430+
])("ignores late OpenCode aborts for $source across newer turns", async (lateAbort) => {
431431
const harness = await createHarness({
432432
serverSettings: { enableLegacyTokenStreaming: true },
433433
});
@@ -487,6 +487,78 @@ describe("ProviderRuntimeIngestion", () => {
487487
streaming: true,
488488
}),
489489
]);
490+
491+
harness.emit({
492+
...base,
493+
type: "turn.completed",
494+
eventId: asEventId("opencode-next-completed"),
495+
turnId: nextTurnId,
496+
createdAt: "2026-01-01T00:00:02.000Z",
497+
payload: { state: "completed" },
498+
});
499+
await harness.drain();
500+
501+
const pendingAt = "2026-01-01T00:00:03.000Z";
502+
for (const hasPendingStart of [false, true]) {
503+
if (hasPendingStart) {
504+
await harness.dispatch({
505+
type: "thread.turn.start",
506+
commandId: CommandId.make("opencode-pending-start"),
507+
threadId,
508+
message: {
509+
messageId: asMessageId("opencode-pending-message"),
510+
role: "user",
511+
text: "Start another turn.",
512+
attachments: [],
513+
},
514+
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
515+
runtimeMode: "approval-required",
516+
createdAt: pendingAt,
517+
});
518+
harness.emit({
519+
...base,
520+
type: "session.state.changed",
521+
eventId: asEventId("opencode-pending-starting"),
522+
createdAt: pendingAt,
523+
payload: { state: "starting" },
524+
});
525+
}
526+
harness.emit({
527+
...base,
528+
type: "turn.aborted",
529+
eventId: asEventId(`opencode-late-abort-after-completion-${hasPendingStart}`),
530+
...(lateAbort.turnId ? { turnId: lateAbort.turnId } : {}),
531+
createdAt: "2026-01-01T00:00:04.000Z",
532+
payload: { reason: "Interrupted by user." },
533+
});
534+
await harness.drain();
535+
536+
const completedThread = (await harness.readModel()).threads.find(
537+
(entry) => entry.id === threadId,
538+
);
539+
expect(completedThread?.session).toMatchObject({
540+
status: hasPendingStart ? "starting" : "ready",
541+
activeTurnId: null,
542+
});
543+
expect(completedThread?.latestTurn).toMatchObject({ turnId: nextTurnId, state: "completed" });
544+
}
545+
546+
harness.emit({
547+
...base,
548+
type: "turn.started",
549+
eventId: asEventId("opencode-pending-started"),
550+
turnId: asTurnId("opencode-pending-turn"),
551+
createdAt: "2026-01-01T00:00:05.000Z",
552+
});
553+
await harness.drain();
554+
const startedThread = (await harness.readModel()).threads.find(
555+
(entry) => entry.id === threadId,
556+
);
557+
expect(startedThread?.latestTurn).toMatchObject({
558+
turnId: asTurnId("opencode-pending-turn"),
559+
state: "running",
560+
requestedAt: pendingAt,
561+
});
490562
});
491563

492564
it("applies provider session.state.changed transitions directly", async () => {

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,14 +1633,10 @@ const make = Effect.gen(function* () {
16331633
if (activeTurnId !== null && eventTurnId !== undefined) {
16341634
return sameId(activeTurnId, eventTurnId);
16351635
}
1636-
// No active turn tracked: accept only completions that name their
1637-
// turn (covers a real completion whose turn.started was lost). An
1638-
// untargeted completion cannot prove it belongs to any turn this
1639-
// thread ran — the known emitter was the Claude resume handshake
1640-
// (system/init + result(num_turns: 0)), which is not a turn at
1641-
// all — and applying it here stomps the "starting" lifecycle
1642-
// state while a turn start is pending.
1643-
return eventTurnId !== undefined;
1636+
// A named completion can recover a lost turn.started event.
1637+
// An abort needs an active turn so a delayed stop cannot replace
1638+
// a ready session or clear a newer pending start.
1639+
return event.type === "turn.completed" && eventTurnId !== undefined;
16441640
default:
16451641
return true;
16461642
}

0 commit comments

Comments
 (0)