fix(server): close sessions when provider turns abort #8859
main fromrefs/pull/8859/head·opened View on GitHubProblem#
An interrupted provider turn can emit canonical turn.aborted without a following turn.completed. Runtime ingestion already recorded the abort and cleared plan progress, but it did not close the durable thread-session lifecycle. The session could remain running with a dangling activeTurnId, leaving the UI stuck on “Working” and refusing settlement even after Stop succeeded.
Fixes #8802.
Fix#
Treat an authoritative turn.aborted as a lifecycle-closing event:
- accept it only when its turn ID exactly matches the currently active turn, so delayed, duplicate, untargeted, and conflicting aborts cannot overwrite newer lifecycle state;
- move the still-live provider session to
ready, clearactiveTurnId, and clear the previous error through the existing ready-state path; - finalize buffered assistant output and proposed plans through the existing turn-completion path;
- clear plan progress only when the abort passes the same lifecycle guard.
This is provider-agnostic. ready is intentional because the provider session remains alive and reusable after the turn is interrupted.
Verification#
- Repeated the original OpenCode/Z.AI insufficient-balance reproduction against this source build using the existing live environment data. After the same retry/failure sequence, Stop succeeded, “Working” cleared, and the persisted session no longer remained active.
- Confirmed the canonical
turn.abortedin the provider event log and the durable projections:active_turn_id = null, the session was no longerrunning, and the turn projected asinterrupted. vp test runacross runtime ingestion, provider command reactor, ProviderService, and OpenCodeAdapter: 6 files, 224 tests passed.- Server typecheck exited successfully; targeted lint, formatting, and
git diff --checkpassed.
Related work#
- #6018 covers the same missing ingestion transition with broader ordering/projection coverage and different
interruptedsession semantics. This PR keeps the live provider session reusable asreadyand is limited to the incident path plus stale-abort guards. - #8813 concerns the OpenCode adapter interrupt path. On current
main, the adapter already emitsturn.aborted; this change consumes that event in orchestration. - #7368 and #4524 involve abort failures rather than a successful abort whose lifecycle event was not applied.
Prepared with GPT-5.6 Sol using the Codex harness in T3 Code.
Note
Medium Risk Changes orchestration session lifecycle and turn-settlement paths; guards limit blast radius, but incorrect abort matching could still affect thread readiness or UI “working” state.
Overview
Fixes threads that stayed running with a dangling activeTurnId when a provider emitted turn.aborted instead of turn.completed after interrupt (e.g. OpenCode), which blocked Stop/settle and left the UI on “Working”.
Provider runtime ingestion now treats an authoritative turn.aborted like turn settlement: session → ready, activeTurnId cleared, partial assistant text and proposed plans finalized, and plan progress cleared. The provider session is left reusable (ready, not stopped).
Guards apply lifecycle updates only when the abort’s turn id matches the active turn, so wrong-turn, duplicate, or post-session.exited aborts cannot overwrite newer state.
Regression tests cover the happy path and stale/late abort behavior.
Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 891ab7d9fa00ff10603e3f25996cda487d0cfda7. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).Note
Fix turn.aborted to close active session and finalize messages in ProviderRuntimeIngestion#
turn.abortednow transitions the session toready, clearsactiveTurnId, and finalizes the assistant message and proposed plan — matching the existingturn.completedhandling.- A lifecycle guard via
shouldApplyThreadLifecycleensures only aborts whoseturnIdmatches the currentactiveTurnIdare processed; stale or duplicate aborts no longer modify the session or clear plan progress. - Adds tests in ProviderRuntimeIngestion.test.ts covering both the active-turn abort path and the stale-abort rejection path.
- Risk: late
turn.abortedevents arriving aftersession.exitedare now ignored; any out-of-tree logic relying on stale aborts clearing plan progress will no longer see that effect.
- 1
github-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
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the [Cursor dashboard](https://www.cursor.com/dashboard/bugbot).
Want fixes drafted automatically? Bugbot Autofix can create code changes for findings. A team admin can enable Autofix in the Cursor dashboard.
Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 39a142e5cc17fdbdbcf2fccea3c1b2d1bcbc12e3. Configure [here](https://www.cursor.com/dashboard/bugbot).apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1617Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1602,4 +1612,6 @@16021612 const nextActiveTurnId =16031613 event.type === "turn.started"16041614 ? (eventTurnId ?? null)1605- : event.type === "turn.completed" || event.type === "session.exited"1615+ : event.type === "turn.completed" ||1616+ event.type === "turn.aborted" ||1617+ event.type === "session.exited"Abort skips message and plan finalize#
Medium Severity
Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 39a142e5cc17fdbdbcf2fccea3c1b2d1bcbc12e3. Configure [here](https://www.cursor.com/dashboard/bugbot).turn.abortednow closes the session asreadyand clearsactiveTurnId, but it still skips theturn.completedfinalize path that flushes buffered assistant text and proposed plans. After Stop, the thread looks idle while in-flight output can staystreamingor never persist.Fixed in 891ab7d9. Authoritative
turn.abortedevents now reuse the existing turn-completion finalization path for buffered assistant messages and proposed plans. The lifecycle guard gates that path, so stale or conflicting aborts neither finalize buffered data nor alter the active session. Regression coverage now exercises both cases. Verification: the targeted 6-suite matrix passes (224 tests), the post-format ingestion suite passes (51 tests), and server typecheck plus targeted lint/format/diff checks are clean.Approvability#
Verdict: Approved at
891ab7dMacroscope's review found this PR approvable — This is a focused server bug fix that closes only the currently active session turn on a matching provider abort, while ignoring stale or late aborts. The behavior is covered by targeted lifecycle regression tests and introduces no schema, configuration, deployment, or sensitive-area changes.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
Independent reproduction on 2026-09-02 with OpenCode on t3
0.0.38(Linux x64, Node 24, systemd service, web client).Stop succeeded end to end:
interruptTurnandopencode.session.abortspans exited successfully- OpenCode emitted
session.errorMessageAbortedError, thensession.statusidle andsession.idle - the adapter emitted canonical
turn.abortedwith the turn id at18:38:14.714Z
The durable session state did not follow:
- the turn projected as
interrupted projection_thread_sessionsstayedrunningwith thatactive_turn_idfor 41 minutes- eight further interrupt requests were accepted with no effect
thread.settlewas rejected withOrchestrationThreadSettleBlockedError- no
thread.session-setwas persisted between therunningtransition and thesession.exitedthat a manual SIGTERM produced
Affected builds: the same lifecycle list without
turn.abortedis present in the0.0.34-nightly.20260821.1153and0.0.39-nightly.20260902.1256bundles.Verification: merged this branch onto main
aab404964locally. Ingestion suite 52/52,ProviderCommandReactor51/51, server typecheck clean. The new lifecycle test fails on unpatched main.Also relates to #4713.
Written by: Fable 5.1
Note
🤖 GPT-6 Astra (preview) responding on behalf of Theo
This note is part of an automated cleanup pass.
Carryover from #6018 at 7c52a7c4a7: retain the event-ordering cases for unscoped, stale, duplicate, and post-terminal aborts, plus later session state, completion, and exit events after an interrupt. Its projection rebuild case compares persisted interrupted lifecycle and pending approval/user-input requests. Keep those invariants alongside this PR's buffered text and proposed-plan finalization. The large fixture expansion is a source of cases, not code claimed to have been copied or run.
Closing as superseded by merged PR #9653.
Retested the original OpenCode/Z.AI insufficient-balance reproduction on the published
0.0.39-nightly.20260906.1293, without this PR's patch. Stop now clears the running UI state, and the persisted session transitions tointerruptedwithactiveTurnId = null42 ms after the interrupt request. A separate subscription with credits completed normally and returned “Pong”.Full verification, UTC timestamps, and test limits: https://github.com/pingdotgg/t3code/issues/8802#issuecomment-5559362626
#8802 is now closed as completed for that original reproduction. This does not claim the broader cases in #4713 are resolved. No rebase or merge of this PR is needed for the verified case.
hahasagined(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.

Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
72db25ac-bb51-43d9-be55-cbe871efbf31You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Warning
Your free Security trial is over. An organization admin can activate Security or dismiss this notice.
Comment `@coderabbitai help` to get the list of available commands.