Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(opencode): settle pending requests during teardown #8441

ClosedPranshuRaj1(GitHub profile, opens in a new tab)wants to merge 5 commits intomain fromrefs/pull/8441/headopened View on GitHub

What Changed

  • Settle pending OpenCode permissions as cancelled and pending questions with empty answers when an OpenCode session or active turn ends.
  • Apply cleanup on stop, interrupt, session replacement, unexpected exit, adapter shutdown, and provider session errors.
  • Abort OpenCode before locally cancelling an interrupted turn; if abort fails, preserve pending requests so they remain answerable.
  • Serialize request creation, responder check-and-reply sequences, interruption, and teardown with a per-session semaphore so each request resolves once.
  • Ignore late permission and question events after teardown starts.
  • Map unknown OpenCode permissions to dynamic_tool_call so they remain actionable.
  • Show unknown approval requests through the generic approval UI on web and mobile.
  • Scope the web fallback to approval.requested activities so unrelated work-log entries keep their original grouping and timeline behavior.
  • Add focused tests for stop, interrupt, provider errors, abort failures, unknown permission types, and deterministic races.

Why

OpenCode only removed pending requests after receiving a provider reply. If a session ended first, the request remained in projection_pending_approvals, kept pending_approval_count above zero, and prevented the thread from settling.

This closes the teardown parity gap described in #7113 and follows the cleanup behavior used by the other provider adapters.

The semaphore gives request events, responses, interruption, and teardown one serialized order. A request is either resolved normally or cancelled, never both.

Unknown OpenCode permission types now use the generic approval flow. The fallback is applied only to approval requests, avoiding changes to search grouping, work-log labels, and timeline-anchor behavior.

Session teardown cleanup is best-effort, so cancellation failures do not prevent the OpenCode process and local session scope from closing.

User interruption only settles pending requests after OpenCode confirms the abort. If the abort fails, those requests remain actionable.

This PR does not migrate historical pending rows already stored in existing databases. That recovery path can be handled separately.

Verification

  • OpenCode adapter tests: 38 passed
  • Focused web and mobile approval derivation tests
  • Deterministic abort-failure, reply-versus-interruption, and request-open race coverage
  • Server typecheck
  • Targeted formatting and lint checks

Closes #7113 Closes #4795 Related to #5119 Related to #7173

UI Changes

Previously, unsupported OpenCode permission types could appear as unknown requests without actionable approval UI. They now use the generic approval flow on web and mobile.

Before

https://github.com/user-attachments/assets/b2c79de2-29ac-46e7-85f4-7dc497944dfa

After

https://github.com/user-attachments/assets/dd9c6605-74d9-447a-9217-c589602bd853

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes
  • I included a video for animation/interaction changes

Built and verified with Codex (GPT-5).


Note

Medium Risk Changes OpenCode session teardown, approval projection, and concurrency around permission/question handling; incorrect settlement could leave threads stuck or double-resolve requests, but behavior is covered by new race and lifecycle tests.

Overview Fixes threads that stayed stuck with pending approvals when an OpenCode session ended before the user replied.

OpenCode adapter now cancels open permission prompts (request.resolved with cancel) and closes open questions (user-input.resolved with empty answers) during stop, interrupt, session replacement, unexpected exit, layer shutdown, and session.error. A per-session pendingGate serializes permission/question events with replies and teardown so each request resolves once; late permission/question events are ignored after stop starts. Unknown OpenCode permissions map to dynamic_tool_call instead of unknown, and permission.replied uses the stored permission for the resolved request type.

Web and mobile treat approval.requested activities with unknown requestType as actionable generic command approvals (only for that activity kind, so work-log entries are not mislabeled).

Adds adapter tests for stop/interrupt/error paths, failed abort, reply-vs-interrupt races, and teardown event races.

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

Note

Settle pending permission/question requests during OpenCode teardown and interruptions

  • Adds settlePendingRequests to the OpenCode adapter, which emits request.resolved (cancel) for pending permissions and user-input.resolved (empty answers) for pending questions, then clears them. Wired into session stop, interruptTurn, session.error, emitUnexpectedExit, and replacing an existing session in startSession.
  • Introduces a pendingGate semaphore on OpenCodeSessionContext to serialize permission/question operations with interrupts and stops. respondToRequest and respondToUserInput now validate presence under the gate and return ProviderAdapterRequestError for already-consumed requests.
  • handleSubscribedEvent drops pending-request events after stop, and now emits request.resolved/user-input.resolved only when a matching pending entry exists, using the stored permission's requestType instead of 'unknown'.
  • Maps unknown permission types to 'dynamic_tool_call' in mapPermissionToRequestType; derivePendingApprovals in both web and mobile now falls back to 'command' for unknown approval.requested kinds.
  • Risk: mapPermissionToRequestType default change means request.resolved events for non-bash/read/edit permissions now report 'dynamic_tool_call' instead of 'unknown'; downstream consumers keyed on 'unknown' in OpenCodeAdapter.ts and session-logic.ts may need updating.
Macroscope summarized 9193685.
    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: 16f7cfd5-767b-4759-842b-b0a4366b1f9e

      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

      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. One finding in apps/web/src/session-logic.ts: the new "command" default leaks into work-log derivation and changes tool-group labelling and timeline anchor behavior for unrelated entries.

      Posted via Macroscope — UI Consistency
    4. Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

      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).

      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 f71fa8ce77a86f85796b1cd796091b4dc743eb76. Configure [here](https://www.cursor.com/dashboard/bugbot).
    5. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Not approved

      Macroscope's review found this PR not approvable — This is a focused OpenCode teardown fix, but it introduces semaphore-based coordination and changes production event ordering and abort behavior across interruption, error, replacement, and shutdown paths. The tests provide useful coverage, yet the lifecycle and concurrency impact is substantial enough to merit direct review.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    6. Superseded by merged #9653, which already closed #4795 and #7113 (OpenCode unknown/skill permission approvals + pending-request teardown/stop).

    7. juliusmarminge closed this

Sign in to join this conversation.