Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): keep turn interrupts responsive #6531

ClosedNeilTheFisher(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/6531/headopened View on GitHub

Summary

  • settle OpenCode interrupts locally before waiting for the provider abort request
  • process interrupt commands on a dedicated worker so unrelated long-running commands cannot block Stop
  • treat OpenCode MessageAbortedError events as expected cancellation and keep the session ready
  • settle turn.aborted events in runtime ingestion

Validation

  • Live reproduction: Stop terminated a running test:ci command and settled backend state in under 100 ms
  • Focused server tests: 126 passed
  • Repository build: passed
  • Full server suite: 2,481 passed; 13 unrelated baseline failures (11 GitManager worktree tests, one newline-worktree-path test, and the existing ProviderRegistry reprobe test)

Note

Medium Risk Changes orchestration turn lifecycle and OpenCode interrupt ordering; behavior is covered by new reactor, ingestion, and adapter tests but affects user-visible Stop semantics.

Overview Stop no longer waits behind long-running provider work on the general command reactor queue: thread.turn-interrupt-requested events go to a dedicated interrupt worker, so a blocked session start on another thread cannot delay interrupt handling.

For OpenCode, interruptTurn now publishes turn.aborted and marks the session ready locally before calling session.abort. The remote abort runs best-effort in the background (errors ignored), so hung provider retries cannot block the UI transition. MessageAbortedError from session.error is treated as expected user cancellation (ready session, turn.aborted) instead of a provider error.

Runtime ingestion applies turn.aborted like a turn end: session returns to ready, active turn is cleared, and assistant messages are finalized the same path as completion.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit c9d0bcb410d7f5901ebcbd2a6a7bbf3df8b86b09. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).

Note

Fix turn interrupts to process on a dedicated worker independently of blocked provider operations

  • Routes thread.turn-interrupt-requested events to a new dedicated interruptWorker in ProviderCommandReactor, preventing interrupts from queuing behind long-running start/stop/recovery tasks.
  • Reworks interruptTurn in OpenCodeAdapter to immediately settle the local session and emit turn.aborted before calling the remote abort, so the interrupt is responsive regardless of remote failures.
  • Treats session.error with MessageAbortedError as a successful interrupt: sets session status to ready, clears lastError, and emits turn.aborted instead of transitioning to an error state.
  • Handles turn.aborted in ProviderRuntimeIngestion by clearing activeTurnId, setting thread session to ready, and finalizing any assistant messages for the turn.
  • Behavioral Change: remote abort failures are now silently logged and ignored; callers of interruptTurn no longer receive abort errors.
Macroscope summarized c9d0bcb.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Important

      Review skipped

      Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

      ⚙️ Run configuration

      Configuration used: Repository UI

      Review profile: CHILL

      Plan: Pro Plus

      Run ID: 0ba15257-ad95-4c2a-9509-30ee6c13d3ef

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    3. @@ -1409,4 +1414,5 @@
      14091414 event.type === "thread.user-input-response-requested" ||
      14101415 event.type === "thread.session-stop-requested"
      14111416 ) {
      1412- return yield* worker.enqueue(event);
      1417+ return yield* event.type === "thread.turn-interrupt-requested"
      1418+ ? interruptWorker.enqueue(event)
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟠 High Layers/ProviderCommandReactor.ts:1418

      A thread.turn-interrupt-requested can be processed before the same thread’s queued thread.turn-start-requested finishes, so providerService.interruptTurn runs before the provider session or turn exists; its failure is only logged, and the later turn starts without ever being interrupted. Coordinate the two lanes per thread while allowing interrupts for unrelated threads to bypass blocked work.

      🤖 Copy this AI Prompt to have your agent fix this:
      In file @apps/server/src/orchestration/Layers/ProviderCommandReactor.ts around line 1418:
      
      A `thread.turn-interrupt-requested` can be processed before the same thread’s queued `thread.turn-start-requested` finishes, so `providerService.interruptTurn` runs before the provider session or turn exists; its failure is only logged, and the later turn starts without ever being interrupted. Coordinate the two lanes per thread while allowing interrupts for unrelated threads to bypass blocked work.
    4. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Needs human review

      2 blocking correctness issues found. Two High-severity findings identify race conditions where interrupts can process before turns start or where stale aborts can cancel subsequent turns. The changes also introduce new concurrency patterns in provider session management that warrant careful review.

      You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    5. @@ -1547,11 +1576,20 @@
      1576+ if (targetTurnId) {
      15471577 yield* emit({
      15481578 ...(yield* buildEventBase({
      15491579 threadId,
      1550- turnId: turnId ?? context.activeTurnId,
      1580+ turnId: targetTurnId,
      15511581 })),
      15521582 type: "turn.aborted",
      15531583 payload: {
      15541584 reason: "Interrupted by user.",
      15551585 },
      15561586 });
      15571587 }
      1588+ // Publish the local lifecycle transition before waiting on the remote
      1589+ // abort. OpenCode can sit in a provider retry (for example after a
      1590+ // monthly usage limit) long enough for session.abort to hang, but the
      1591+ // user must still be able to stop the turn immediately.
      1592+ context.activeTurnId = undefined;
      1593+ context.activeAgent = undefined;
      1594+ context.activeVariant = undefined;
      1595+ yield* updateProviderSession(context, { status: "ready" }, { clearActiveTurnId: true });
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟠 High Layers/OpenCodeAdapter.ts:1595

      interruptTurn marks the session ready and clears activeTurnId before session.abort settles, so a new sendTurn can start on the same session while the old abort is pending; that abort can cancel the new turn. The later MessageAbortedError then reads the newer context.activeTurnId, clears it, and emits turn.aborted for the wrong turn. Prevent new prompts until the abort settles without delaying the local lifecycle update, or correlate the abort and error with the interrupted turn before mutating session state.

      🤖 Copy this AI Prompt to have your agent fix this:
      In file @apps/server/src/provider/Layers/OpenCodeAdapter.ts around line 1595:
      
      `interruptTurn` marks the session `ready` and clears `activeTurnId` before `session.abort` settles, so a new `sendTurn` can start on the same session while the old abort is pending; that abort can cancel the new turn. The later `MessageAbortedError` then reads the newer `context.activeTurnId`, clears it, and emits `turn.aborted` for the wrong turn. Prevent new prompts until the abort settles without delaying the local lifecycle update, or correlate the abort and error with the interrupted turn before mutating session state.
    6. Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.

      Fix All in Cursor ❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the [Cursor dashboard](https://www.cursor.com/dashboard/bugbot). Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit c9d0bcb410d7f5901ebcbd2a6a7bbf3df8b86b09. Configure [here](https://www.cursor.com/dashboard/bugbot).
      @@ -1557 +1587,21 @@
      15571587 }
      1588+ // Publish the local lifecycle transition before waiting on the remote
      1589+ // abort. OpenCode can sit in a provider retry (for example after a
      1590+ // monthly usage limit) long enough for session.abort to hang, but the
      1591+ // user must still be able to stop the turn immediately.
      1592+ context.activeTurnId = undefined;
      1593+ context.activeAgent = undefined;
      1594+ context.activeVariant = undefined;
      1595+ yield* updateProviderSession(context, { status: "ready" }, { clearActiveTurnId: true });
      1596+
      1597+ // Best effort: the lifecycle event above is the authoritative UI
      1598+ // transition, while this request stops the provider's in-flight work
      1599+ // when the OpenCode server is responsive. Run it in the session scope
      1600+ // so a later session stop also cancels a hung abort request.
      1601+ yield* runOpenCodeSdk("session.abort", () =>
      1602+ context.client.session.abort({ sessionID: context.openCodeSessionId }),
      1603+ ).pipe(
      1604+ Effect.mapError(toRequestError),
      1605+ Effect.ignore({ log: true }),
      1606+ Effect.forkIn(context.sessionScope),
      1607+ );

      Stale abort can cancel later turns

      High Severity

      interruptTurn now returns after a local turn.aborted and forks session.abort without binding it to that turn. A follow-up send can start while the abort is still in flight, so the remote abort or a later MessageAbortedError can cancel or settle the new turn instead of the stopped one.

      Additional Locations (1)
      Fix in Cursor Fix in Web
      Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit c9d0bcb410d7f5901ebcbd2a6a7bbf3df8b86b09. Configure [here](https://www.cursor.com/dashboard/bugbot).
      @@ -1581,4 +1585,6 @@
      15811585 const nextActiveTurnId =
      15821586 event.type === "turn.started"
      15831587 ? (eventTurnId ?? null)
      1584- : event.type === "turn.completed" || event.type === "session.exited"
      1588+ : event.type === "turn.completed" ||
      1589+ event.type === "turn.aborted" ||
      1590+ event.type === "session.exited"

      Failed prompts can look like interrupts

      Medium Severity

      Ingestion now maps every turn.aborted to session ready with lastError cleared. OpenCode already emits turn.aborted when a fresh promptAsync fails, so that failure can overwrite recoverTurnStartFailure and hide the error in orchestration session state.

      Additional Locations (1)
      Fix in Cursor Fix in Web
      Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit c9d0bcb410d7f5901ebcbd2a6a7bbf3df8b86b09. Configure [here](https://www.cursor.com/dashboard/bugbot).
    7. Note

      🤖 GPT-5.6 Sol responding on behalf of Theo

      We're closing this PR as we clean up the T3 Code backlog. Thank you for taking the time to put this together.

      Closing this interrupt implementation because it marks the session ready before the provider abort completes. A new turn can then start in the same OpenCode session and be cancelled by the delayed session-wide abort. The Stop latency problem is valid, but the fix must keep cancellation tied to the intended turn.

      If you believe we closed this in error, please reopen the PR and leave a comment explaining what we missed. If GitHub does not let you reopen it, leave a comment here and we'll take another look.

Sign in to join this conversation.