fix(orchestration): persist OpenCode abort state #8939
Closeddlowzzxx(GitHub profile, opens in a new tab)wants to merge 1 commit into
main fromrefs/pull/8939/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:S
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts lines 386 to 389OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -369,3 +369,21 @@369369 expect(thread.session?.lastError).toBe("turn failed");370370 });371371372+ it("settles the durable session when a turn is aborted", async () => {373+ const harness = await createHarness();374+ const now = "2026-01-01T00:00:00.000Z";375+ const turnId = asTurnId("turn-aborted");376+377+ harness.emit({378+ type: "turn.started",379+ eventId: asEventId("evt-turn-aborted-started"),380+ provider: ProviderDriverKind.make("opencode"),381+ threadId: asThreadId("thread-1"),382+ createdAt: now,383+ turnId,384+ });385+386+ await waitForThread(387+ harness.readModel,388+ (thread) => thread.session?.status === "running" && thread.session?.activeTurnId === turnId,389+ );apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1598Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -1546,3 +1546,4 @@15461546 case "turn.started":15471547 return !conflictsWithActiveTurn || conflictingTurnStartIsPendingTurnStart;15481548 case "turn.completed":1549+ case "turn.aborted":
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1625OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1616,4 +1622,4 @@16161622 : event.type === "turn.completed" &&16171623 normalizeRuntimeTurnState(event.payload.state) === "failed"16181624 ? (event.payload.errorMessage ?? thread.session?.lastError ?? "Turn failed")1619- : status === "ready"1625+ : status === "ready" || status === "interrupted"apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1625OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1616,4 +1622,4 @@16161622 : event.type === "turn.completed" &&16171623 normalizeRuntimeTurnState(event.payload.state) === "failed"16181624 ? (event.payload.errorMessage ?? thread.session?.lastError ?? "Turn failed")1619- : status === "ready"1625+ : status === "ready" || status === "interrupted"
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1563OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1549,14 +1550,14 @@15491550 if (conflictsWithActiveTurn || missingTurnForActiveTurn) {15501551 return false;15511552 }15521553 // Only the active turn may close the lifecycle state.15531554 if (activeTurnId !== null && eventTurnId !== undefined) {15541555 return sameId(activeTurnId, eventTurnId);15551556 }1556- // No active turn tracked: accept only completions that name their1557- // turn (covers a real completion whose turn.started was lost). An1558- // untargeted completion cannot prove it belongs to any turn this1559- // thread ran — the known emitter was the Claude resume handshake1560- // (system/init + result(num_turns: 0)), which is not a turn at1561- // all — and applying it here stomps the "starting" lifecycle1562- // state while a turn start is pending.1557+ // No active turn tracked: accept only terminal events that name1558+ // their turn (covers a real completion/abort whose turn.started1559+ // was lost). An untargeted terminal event cannot prove it belongs1560+ // to any turn this thread ran — the known emitter was the Claude1561+ // resume handshake (system/init + result(num_turns: 0)), which is1562+ // not a turn at all — and applying it here stomps the "starting"1563+ // lifecycle state while a turn start is pending.
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:S
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1628Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1551 +1587,21 @@15511587 }1588+ // A named abort may arrive after the server has requested a new1589+ // turn but before its turn.started event. In that window there1590+ // is no projected active turn to compare against, so only the1591+ // provider's currently active turn can settle the pending start.1592+ if (event.type === "turn.aborted" && hasPendingTurnStart && activeTurnId === null) {1593+ return (1594+ eventTurnId !== undefined &&1595+ expectedProviderTurn !== undefined &&1596+ sameId(1597+ expectedProviderTurn.activeTurnId ?? expectedProviderTurn.lastAbortedTurnId,1598+ eventTurnId,1599+ ) &&1600+ Option.isSome(pendingTurnStart) &&1601+ sameId(1602+ expectedProviderTurn.activeTurnId !== undefined1603+ ? expectedProviderTurn.activeTurnStartMessageId1604+ : expectedProviderTurn.lastAbortedMessageId,1605+ pendingTurnStart.value.messageId,1606+ )1607+ );
- apps/server/src/provider/Layers/OpenCodeAdapter.ts lines 762 to 769Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -672,3 +676,14 @@672676 if (options?.clearActiveTurnId) {673677 delete mutableSession.activeTurnId;674678 }679+ if (options?.clearActiveTurnStartMessageId) {680+ delete mutableSession.activeTurnStartMessageId;681+ }682+ if (patch.activeTurnId !== undefined) {683+ delete mutableSession.lastAbortedTurnId;684+ delete mutableSession.lastAbortedMessageId;685+ delete mutableSession.activeTurnStartMessageId;686+ if (patch.activeTurnStartMessageId !== undefined) {687+ mutableSession.activeTurnStartMessageId = patch.activeTurnStartMessageId;688+ }689+ }
- apps/server/src/provider/Layers/OpenCodeAdapter.ts line 2683OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2643,3 +2680,4 @@26432680 context.promptAdmission = promptAdmission;2644268126452682 context.activeTurnId = turnId;2683+ context.activeTurnStartMessageId = input.turnStartMessageId;
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1598Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1550,3 +1586,4 @@15501586 case "turn.started":15511587 return !conflictsWithActiveTurn || conflictingTurnStartIsPendingTurnStart;15521588 case "turn.completed":1589+ case "turn.aborted":apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1539Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1520,3 +1547,7 @@15201547 });15211548 const hasPendingTurnStart =15221549 Option.isSome(pendingTurnStart) && thread.session?.status === "starting";1550+ const expectedProviderTurn =1551+ event.type === "turn.aborted" && hasPendingTurnStart1552+ ? yield* getExpectedProviderTurnForThread(thread.id)1553+ : undefined;
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1607Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1560,8 +1614,15 @@15601614 case "thread.started":15611615 return true;15621616 case "turn.started":1563- return !conflictsWithActiveTurn || conflictingTurnStartIsPendingTurnStart;1617+ return (1618+ !turnStartedMatchesRetainedAbort &&1619+ (!conflictsWithActiveTurn || conflictingTurnStartIsPendingTurnStart)1620+ );15641621 case "turn.completed":1622+ case "turn.aborted":15651623 if (conflictsWithActiveTurn || missingTurnForActiveTurn) {15661624 return false;15671625 }1626+ if (pendingTurnStartMatchesRetainedAbort) {1627+ return false;1628+ }apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1629Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1567,0 +1630,21 @@1630+ // turn but before its turn.started event. In that window there1631+ // is no projected active turn to compare against, so only the1632+ // provider's currently active turn can settle the pending start.1633+ if (event.type === "turn.aborted" && hasPendingTurnStart && activeTurnId === null) {1634+ return (1635+ eventTurnId !== undefined &&1636+ expectedProviderTurn !== undefined &&1637+ sameId(1638+ expectedProviderTurn.activeTurnId ?? expectedProviderTurn.lastAbortedTurnId,1639+ eventTurnId,1640+ ) &&1641+ Option.isSome(pendingTurnStart) &&1642+ (expectedProviderTurn.provider !== "opencode" ||1643+ sameId(1644+ expectedProviderTurn.activeTurnId !== undefined1645+ ? expectedProviderTurn.activeTurnStartMessageId1646+ : expectedProviderTurn.lastAbortedMessageId,1647+ pendingTurnStart.value.messageId,1648+ ))1649+ );1650+ }
juliusmarminge added labelsmacroscope-review
dlowzzxx(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
We couldn't safely recover the incremental review. No full review was started, and the last reviewed checkpoint was preserved. Retry later, or explicitly request a full review by commenting
@coderabbitai full review.You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
📝 Walkthrough
Walkthrough#
The provider now propagates turn-start message IDs, records aborted-turn correlations, validates
turn.abortedevents, updates session state, finalizes content, and clears correlated plan progress. Prompt failures persist errors. Tests cover accepted, rejected, buffered, and streamed abort paths.Changes#
Provider runtime abort handling
packages/contracts/src/provider.ts,apps/server/src/orchestration/Layers/ProviderCommandReactor.tsapps/server/src/provider/Layers/OpenCodeAdapter.ts,apps/server/src/provider/Layers/OpenCodeAdapter.test.tsapps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts,apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.tsEstimated code review effort: 4 (Complex) | ~60 minutes
Suggested reviewers:
juliusmarminge,t3dotggMerge Risk: 🟡 Moderate · up to
147e4OpenCode steering timeouts can record the wrong turn token, causing the resulting abort to be ignored and leaving a durable session stuck in progress; accepted aborts may also leave partial assistant output unfinished. The PR should not merge until these abort paths are corrected or explicitly accepted by the owner.
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
#8895by persisting OpenCode abort outcomes, clearing active turns, correlating aborts with the correct turn-start message, rejecting stale or mismatched aborts, and preservi…Full details: Description check
Explanation
The description clearly explains the changes, motivation, scope, behavior, and verification results. It omits the template headings and checklist, but the required information is otherwise substantially present.
Full details: Linked Issues check
Explanation
The changes address issue
#8895by persisting OpenCode abort outcomes, clearing active turns, correlating aborts with the correct turn-start message, rejecting stale or mismatched aborts, and preserving distinct interrupted and error states.Full details: Out of Scope Changes check
Explanation
The production changes and regression tests remain focused on OpenCode abort persistence, provider-session correlation, lifecycle finalization, and preservation of existing provider semantics. No unrelated changes are evident.
✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment `@coderabbitai help` to get the list of available commands.