Skip to content

[Bug]: OpenCode 2 thread stays stuck in running forever when turn.steers misses a session.inbox.delivered event (unbounded turn.heldEnd) #41261

Closed as not planned

Description

@Realauster

Before submitting

  • I searched existing issues and did not find a duplicate.
  • I included enough detail to reproduce or investigate the problem.

Area

apps/server

Steps to reproduce

  1. Run a thread on the OpenCode 2 provider (opencode v2.3.2).
  2. During an active turn, inject multiple mid-turn steers (for example, launching multiple async delegate_task children while the parent turn remains active so T3 delivers delegated-completion notifications via provider-turn.steer -> OpenCode2Adapter.steerTurn, back-to-back across a step boundary).
  3. Wait for OpenCode to deliver all steers, finish generating its final assistant reply, and emit session.execution.succeeded.

Expected behavior

Once OpenCode finishes (session.execution.succeeded) and has no pending inbox items left for that session, OpenCode2Adapter should finish the turn and transition the thread from running to completed.

If turn.steers.size > 0 when session.execution.succeeded arrives, the adapter should reconcile turn.steers against OpenCode's actual inbox / message history (similar to backfill on reconnect) or bound the turn.heldEnd hold instead of waiting forever.

Actual behavior

OpenCode delivers all steers, emits the full final assistant message, and emits session.execution.succeeded cleanly — the OpenCode session is completely idle.

However, T3 Code keeps the thread stuck in status: "running" indefinitely (observed 15+ minutes with zero further activity) because two already-delivered steers remained in turn.steers.

In packages/provider-opencode/src/server/v2/OpenCode2Adapter.ts:

  • endExecution withholds completion whenever turn.steers.size > 0:
    if (end.status === "completed" && (turn.steers.size > 0 || goalHoldsTurn(state))) {
      turn.heldEnd = end;
      if (goalHoldsTurn(state) && !state.goalRefreshing) yield* refreshGoal(state);
      return;
    }
  • turn.steers is only pruned by live SSE session.inbox.delivered / session.inbox.cancelled events (or backfill if the SSE stream disconnects and reconnects).
  • When session.execution.succeeded arrives while turn.steers still holds stale ID(s), there is no inbox/message-list reconciliation check and no timer bounding turn.heldEnd (unlike the bounded hold added for Antigravity in fix(antigravity): bound silent post-settle background command holds #40858).

Interrupting the stuck thread proves the exact state:

  1. POST /api/session/<sid>/interrupt returns HTTP 200 with interrupted: false (OpenCode is already idle).
  2. OpenCode2Adapter.interruptTurn takes its !reply.value.interrupted && state.active === turn && turn.heldEnd !== undefined branch.
  3. finishTurn moves turn.steers into state.strandedSteers and cancelStrandedSteers sends DELETE /api/session/<sid>/inbox/<inboxID> for the two stale delegated-completion steer IDs — both return 404 Not Found because OpenCode had already delivered and removed them from its inbox.

Impact

Major degradation or frequent failure

Version or commit

v0.1.12-nightly.20261122.5098 (also checked 5098...5111 diff; no OpenCode2Adapter changes)

Environment

Linux x64 (Bazzite / Fedora Atomic), T3 Code Nightly desktop app (v0.1.12-nightly.20261122.5098), OpenCode 2.3.2, model opencode-go/deepseek-v4.1-flash.

Logs or stack traces

# 1) Back-to-back delegated-completion steers during active turn (UTC):
01:28:33.900Z OpenCode2Adapter.steerTurn -> POST /api/session/<sid>/prompt (2990ms, 200)  # ordinal:4
01:28:37.061Z OpenCode2Adapter.steerTurn -> POST /api/session/<sid>/prompt (7.8ms, 200)   # ordinal:5
01:29:16.769Z OpenCode2Adapter.steerTurn -> POST /api/session/<sid>/prompt (38.8ms, 200)  # ordinal:6 (cleared normally)

# 2) OpenCode finished cleanly (confirmed via OpenCode plugin event subscription):
01:30:10.761Z final assistant turn-item.updated written to T3
01:30:10.782Z OpenCode emitted session.execution.succeeded (session now idle)
# Thread remained status="running", activeRunId!=null for 15+ minutes until manually interrupted.

# 3) Interrupt trace proving turn.heldEnd was set and both stale steers were already gone from OpenCode's inbox:
01:45:14.801Z POST   /api/session/<sid>/interrupt -> 200 (interrupted: false)
01:45:15.357Z DELETE /api/session/<sid>/inbox/msg_t3_steer_<sid>%3Amessage%3Athread%3A<tid>%3Aordinal%3A4%3A<uuid> -> 404
01:45:15.452Z DELETE /api/session/<sid>/inbox/msg_t3_steer_<sid>%3Amessage%3Athread%3A<tid>%3Aordinal%3A5%3A<uuid> -> 404
01:45:14.800Z OpenCode2Adapter.interruptTurn -> Success (thread transitioned to interrupted)

Workaround

Clicking Stop (t3_thread_interrupt) immediately hits the turn.heldEnd !== undefined branch in OpenCode2Adapter.interruptTurn, clears the stuck running state, and preserves the already-completed assistant reply.

Suggested fix: in endExecution (when end.status === "completed" and turn.steers.size > 0), check OpenCode's actual inbox/messages (or client.session.active() + inbox list) before holding turn.heldEnd indefinitely, and clear any steer IDs that are no longer pending in OpenCode's inbox.

Activity

  1. juliusmarminge commented on Nov 23, 2026

    @juliusmarminge
    Member

    I tried to reproduce this on the current build and could not.

    Commit b1ce608 (origin/main). Installed OpenCode 2.3.2 (npm @opencode/cli@2.3.2) in a scratch dir and pointed T3's OpenCode provider binaryPath at a wrapper that gives it isolated XDG data/state/cache dirs. Ran T3CODE_DEV_SEED=0 vp run dev (fresh DB) and drove the web client in a browser with model Claude Haiku 5.5 via OpenCode. Run 1: prompt running two sleep 20 bash calls; sent two steer messages back to back during the first sleep. Both showed as Steer, the agent answered 'DONE ALPHA BRAVO' and the thread completed. Run 2 (the reporter's path): agent used T3's delegate_task to start two background children (sleep 12 / sleep 14), then ran sleep 25 twice. Both children finished during the parent's sleeps, T3 steered their completions into the active OpenCode turn, the parent replied 'FINISHED' listing C1 and C2, and the thread settled (not stuck running). Runs 3 and 4 drove the OpenCode 2.3.2 HTTP API directly with T3-style steer ids (msg_t3_steer_:...): two back-to-back steers sent when the final step started streaming text, and again right as the final step ended. In both, OpenCode emitted session.inbox.enqueued and then session.inbox.delivered for every steer id, steer POSTs took 5-30 ms, and GET /inbox was empty afterwards. Not tried: the reporter's model opencode-go/deepseek-v4.1-flash, which the hub does not offer, and the reporter's 2990 ms steer POST latency, which suggests OpenCode was under load.

    Closing as not planned. If it still happens for you, reply with the exact steps and the version you are on and it will be looked at again.

  2. Realauster commented on Nov 23, 2026

    @Realauster
    Author

    Thanks for testing this. Two details that explain why the quick repro did not hit it — and why the bug is still present in b1ce608be3:

    1. Why the repro missed it:

      • In our trace, the third steer (ordinal:6, sent during a long-running shell tool call, 38.8 ms POST) did clear normally — matching your Run 2 (sleep 25).
      • The two steers that wedged the turn (ordinal:4 at 2990 ms right across a tool-failure/step boundary, followed immediately by ordinal:5 at 7.8 ms) arrived while OpenCode was transitioning between steps under load. Both were delivered to the model (confirmed in the model's subsequent reasoning and by DELETE /api/session/<sid>/inbox/<id> returning 404 for both on interrupt), yet turn.steers still held both IDs when session.execution.succeeded arrived.
    2. The code-level bug is still in b1ce608be3 (packages/provider-opencode/src/server/v2/adapter.ts):

      • In endExecution (lines 3286–3292):
        if (end.status === "completed" && (turn.steers.size > 0 || goalHoldsTurn(state))) {
          turn.heldEnd = end;
          if (goalHoldsTurn(state) && !state.goalRefreshing) yield* refreshGoal(state);
          return;
        }
      • Notice goalHoldsTurn(state) right next to it already calls refreshGoal(state) (with INVENTORY_TIMEOUT) because OpenCode can change goal state without notifying T3.
      • For turn.steers.size > 0, there is no equivalent reconciliation against OpenCode's inbox / client.message.list (even though backfill at lines 3624–3628 already does this on SSE reconnect) and no timeout bounding turn.heldEnd. So any single missed/dropped session.inbox.delivered event permanently wedges the thread in running until manual Stop.
      • This is directly reproducible in a unit test (OpenCode2AdapterV2.test.ts): call steerTurn, omit session.inbox.delivered (while the steer message is already present in message.list / absent from inbox), then emit session.execution.succeeded — the turn stays running forever.

    Version: v0.1.12-nightly.20261122.5098 (confirmed identical endExecution / turn.heldEnd logic on b1ce608be3). Could this be reopened to add inbox/message reconciliation (or a bounded hold) in endExecution when turn.steers.size > 0?

  3. vtsixthai commented on Nov 24, 2026

    @vtsixthai

    Independent confirmation from a different install: same model family on different builds, plus a second model family. Taking the close comment at its word and replying here with the exact steps and the version, since the follow-up above already covers the code side. Keeping to our evidence.

    Version and environment. T3 Code v0.1.11 release, Linux x64 headless server, background service; the parent thread runs on Claude and drives OpenCode children through delegate_task and t3_thread_send. OpenCode 2.4.0 for the case below; the seven earlier occurrences ran OpenCode 2.3.2 on v0.1.10 and v0.1.11. Child models: deepseek-v4.1-flash in 5 cases, muse-spark-1.3-contributor in 3, so this is not tied to one model.

    Steps, as observed. The failure is intermittent here (5 stalls in 76 delegated OpenCode/DeepSeek threads over four days), so this is the observed sequence rather than a deterministic script:

    1. A delegated OpenCode child runs a long multi-step task.
    2. The parent sends follow-ups mid-turn; steerTurn delivers them as inbox prompts with delivery: "steer". OpenCode consumes each one, leaving a user message whose id equals the steer prompt id, and the inbox ends up empty.
    3. OpenCode writes its final assistant message (finish=stop) and the session goes idle with outcome=succeeded.
    4. The T3 run stays running forever. A queued successor run never starts and no wake reaches the parent, until someone presses Stop.

    Longest case (UTC, v0.1.11, OpenCode 2.4.0).

    • 19:14:00 run 2 starts. Steers consumed at 19:16:01, 19:16:43 and 19:45:03; the last was sent by the parent at 19:44:58.
    • 20:11:50 T3 ingests the final assistant text. OpenCode completes the terminal message at 20:12:01 and reports idle succeeded the same second.
    • The run is still running at 01:38:12 the next day, 326 min after OpenCode finished. A run 3 requested at 19:52:59 never started (startedAt null) and was cancelled by the same interrupt.
    • 01:38:14 the interrupt produces the exact footprint of the turn.heldEnd branch (L5300): Could not take back an OpenCode steer with cause StatusCode error (404 DELETE /api/session/<sid>/inbox/<id>). A 404 here means the steer had been delivered 5 h 53 m earlier and OpenCode no longer held it.

    The same footprint in all eight cases. The service log holds exactly eight Could not take back an OpenCode steer warnings between Nov 21 and Nov 24, each with a 404 DELETE cause and a stack through OpenCode2Adapter.interruptTurn, each on a different session: the five DeepSeek stalls (3.4, 146.7, 326.2, 950.8 and 963.6 min) plus three muse-spark-1.3-contributor siblings the same parents cleared in the same manual sweeps. In every case OpenCode's stored history ends with the final assistant message and idle succeeded well before the interrupt. Steering alone does not trigger it: 10 of 15 steered DeepSeek sessions completed normally, including some with 5, 7 and 9 steers, and 58 of 59 completed threads have T3 completion within 0.1 min of OpenCode idle. The trigger is the one missed session.inbox.delivered, which also explains why the scripted repro above passes: healthy drains are instant, and the event has to be lost, which we cannot trigger on demand either.

    Still present on main at f981b42dc49 (checked today, packages/provider-opencode/src/server/v2/adapter.ts): endExecution (L3342) parks turn.heldEnd whenever turn.steers.size > 0; the live prune is only the session.inbox.cancelled / session.inbox.delivered handler (L3577) or a failed steer POST; backfill (L3662) already contains the authoritative check at L3686 (message.type === "user" && turn.steers.delete(message.id)) but only runs from reconcile inside reconnect (L3927, L3970). The goal hold parked by the same line already re-reads OpenCode state through refreshGoal because OpenCode can change state without notifying T3, and #40858 added a bounded hold for the analogous Antigravity case.

    What is not proven: why the event is missed in any given case. Candidates in the same file: events for a session with a running wake are buffered (held.events.push, L3472) and only reach route when a continuation replays them (replay, L4566), so a dropped wake drops its buffer; otherwise a plain SSE gap between heartbeats.

    Ask, same as the follow-up above plus one. Reconcile against authoritative OpenCode state before parking turn.heldEnd (the message.list check backfill already has, or the inbox listing), add a bounded settle timer as a backstop, and treat a 404 from steer take-back as "already delivered" instead of an ignorable warning; it is direct evidence the steer was consumed.

    Regression shape that would have caught this: steerTurn with the steer user message present in history and session.inbox.delivered suppressed, plus a variant where it arrives late, after a wake buffer holds it, then session.execution.succeeded; assert the turn completes, a queued successor starts, and the parent wakes. Our longest case also has an idle marker with outcome=failed ("Execution was interrupted repeatedly") 7 s before the final idle succeeded; only the succeeded marker should settle the turn.

    Collected and written by a Claude Opus agent at the owner's request, from read-only service logs, T3 thread projections and OpenCode storage on the affected server. Session, thread and task ids and local paths are redacted.

  4. Realauster commented on Nov 24, 2026

    @Realauster
    Author

    Thanks @vtsixthai for the independent confirmation and detailed logs across 8 cases, two model families (deepseek-v4.1-flash and muse-spark-1.3-contributor), and both OpenCode 2.3.2 and 2.4.0.

    We've had to keep a local workaround in place (ending OpenCode turns immediately after launching async delegates so completions don't arrive as mid-turn steers) while waiting on a fix.

    @juliusmarminge — since two independent installs now show the exact same 404 DELETE /api/session/<sid>/inbox/<id> + unbounded turn.heldEnd stall on main (f981b42dc49), could this issue be reopened to add inbox/message reconciliation and a bounded settle timer in endExecution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions