Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): open a new OpenCode turn after the provider goes idle #11090

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

What Changed

sendTurn no longer decides steer-vs-new-turn from the cached activeTurnId alone. Before reusing it, the adapter confirms the native session status — but only where an idle reading is actually authoritative for that turn.

An idle reading is not authoritative when idle evidence is deliberately withheld (prompt admission, post-interruption, or pending reconciliation each mean a known-stale idle is in flight and the prompt is a genuine mid-turn steer), nor before OpenCode has reported the turn busy, since it then describes the state before the turn started rather than after it ended. An unavailable or undecodable status keeps the previous behavior: never guess idle.

Two follow-on details the change needs to be correct:

  • Completing a turn from the preflight leaves the native idle it stood in for still in flight, so idle reconciliation is armed. The next idle is checked against live status instead of trusted, and retires the turn it belongs to rather than the turn the follow-up just opened.
  • The status request is itself a suspension point, so activeTurnId is re-read after it rather than trusting the id captured before it — the event pump can retire the turn while the request is in flight, which is the same race in a narrower window.

Two deterministic regression tests cover both orderings: the idle event arriving before the follow-up, and the idle event landing during the preflight. Both were verified to fail against the unfixed adapter with the reported symptom (the follow-up reusing the finished turn's id).

Why

Fixes #10973.

After an OpenCode task finished and the thread went idle, the first follow-up message was ignored. activeTurnId is cleared asynchronously by the event pump once native idle evidence is processed, so a prompt submitted during that gap reused the finished turn's id. That skipped turn.started and accrued the new work's tokens to the previous turn — from the user's side, the message looked like it did nothing.

The narrow fix is to confirm the provider's real status before trusting a cached turn id, while leaving every case where the cached id is legitimately still live untouched. In particular, the existing coverage for a mid-turn steer racing a stale idle status is unchanged and still green: that path is classified as withheld idle evidence and skips the status read entirely.

Verification: focused adapter suite 112/112 (run repeatedly for stability), all eight OpenCode test files 195/195, apps/server typecheck clean, targeted lint and format clean.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes — N/A, server-only change with no UI surface
  • I included a video for animation/interaction changes — N/A

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of concurrent follow-up turns to prevent completed turns from being incorrectly reused.
    • Ensured stale or delayed idle-status events do not end newer active turns.
    • Improved recovery when status updates race with turn submission, preserving the correct active turn and running state.
    1. Bugbot is paused — on-demand spend limit reached

      Bugbot uses usage-based billing for this team and has hit its on-demand spend limit.

      A team admin can raise the spend limit in the Cursor dashboard, or wait for the next billing cycle to continue.

    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    3. Review Change StackReview Change Stack

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration

      Configuration used: Path: .coderabbit.yaml

      Review profile: CHILL

      Plan: Advanced

      Run ID: 6cad2ede-37b9-4cda-9850-1981cd362c36

      📥 Commits

      Reviewing files that changed from the base of the PR and between d29c56a5c404cb0f58d3b2ac41762fa0d0ac28d4 and 0596dc475dea65e6f71223e2434712b96eb1c61a.

      📒 Files selected for processing (2)
      • apps/server/src/provider/Layers/OpenCodeAdapter.test.ts
      • apps/server/src/provider/Layers/OpenCodeAdapter.ts

      Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


      📝 Walkthrough

      Walkthrough

      The OpenCode adapter now tracks native busy status before reusing an active turn. It performs a status preflight when required, completes stale turns, and preserves newer turns across delayed idle events. Two regression tests cover these concurrency races.

      Changes

      OpenCode turn boundary

      Layer / File(s)Summary
      Native busy-state tracking
      apps/server/src/provider/Layers/OpenCodeAdapter.ts
      The session context records native busy or retry status. The flag resets when turns complete, fail, are interrupted, encounter errors, or start fresh.
      Guarded steering and race coverage
      apps/server/src/provider/Layers/OpenCodeAdapter.ts, apps/server/src/provider/Layers/OpenCodeAdapter.test.ts
      sendTurn checks native status before reusing a busy active turn. Tests cover delayed idle events and suspended status preflight.

      Estimated code review effort: 3 (Moderate) | ~20 minutes

      Severity of issue fixed: Low

      Sequence Diagram(s)

      sequenceDiagram
        participant FollowUpCaller
        participant sendTurn
        participant OpenCode
        participant TurnState
        FollowUpCaller->>sendTurn: submit follow-up
        sendTurn->>OpenCode: request session.status
        OpenCode-->>sendTurn: idle or missing status
        sendTurn->>TurnState: complete stale active turn
        sendTurn->>OpenCode: start distinct follow-up turn

      Suggested reviewers: t3dotgg, maria-rcks, juliusmarminge

      Merge Risk: ⚪ Minimal · up to 0596d

      This change separates completed OpenCode follow-up turns from legitimate mid-turn steering by checking native status before reusing cached turn IDs. Focused tests and validation checks pass, with no concrete current-head correctness or availability issue identified.

      🚥 Pre-merge checks | ✅ 4 | ❌ 1

      ❌ Failed checks (1 warning)

      Check nameStatusExplanationResolution
      Docstring Coverage⚠️ WarningDocstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files.Write docstrings for the functions missing them to satisfy the coverage threshold.
      ✅ Passed checks (4 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly summarizes the primary change: opening a new OpenCode turn after the provider becomes idle.
      Description check✅ PassedThe description includes complete What Changed, Why, UI Changes, and Checklist sections. It explains the fix, preserves steering behavior, describes regression coverage, and reports verification resul…
      Linked Issues check✅ PassedThe implementation directly addresses issue #10973. It prevents completed turns from being reused, preserves valid mid-turn steering, handles status and event races, and adds regression tests for both…
      Out of Scope Changes check✅ PassedThe changes are limited to the OpenCode adapter and focused concurrency regression tests. They support the linked issue and do not introduce unrelated functionality.
      • Fix all pre-merge checks with AI
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests

      Comment `@coderabbitai help` to get the list of available commands.
    4. juliusmarminge added labelsmacroscope-review

    5. Approvability

      Verdict: Approved at 0596dc4

      Macroscope's review found this PR approvable — This is a focused OpenCode server bug fix that prevents stale turn IDs from absorbing follow-up prompts, with targeted coverage for both relevant event-ordering races. Production changes are confined to the existing adapter and do not alter public schemas, defaults, infrastructure, or sensitive-data handling.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    6. Note

      Grok responding on behalf of Julius.

      Thanks for working on this, @jmfrank63! #10973 is now closed as completed by the merge of #2829 (the new orchestrator), which this PR's fix is superseded by, so I'm closing this one. If you still see the follow-up getting folded into the previous turn on current main, please open a fresh issue with repro steps.

    7. juliusmarminge closed this

Sign in to join this conversation.