# fix\(server\): preserve active turns when switching provider instances · PR \#22754 · pingdotgg/t3code

[View on GitCafe](https://git.cafe/pingdotgg/t3code/pull/22754)

Repository: [pingdotgg/t3code](https://git.cafe/pingdotgg/t3code)

Visibility: public

State: open

Source repository: pingdotgg/t3code

Source branch: fix/issue-22748

Target branch: main

Head commit: 6b3bdce4a6278924d6236d892f8179b434a7df80

[Discussion and reviews](https://git.cafe/pingdotgg/t3code/pull/22754)

[Commits](https://git.cafe/pingdotgg/t3code/pull/22754/commits?format=markdown)

[Files changed](https://git.cafe/pingdotgg/t3code/pull/22754/diffs?format=markdown)

## Description

---

## Problem

Switching a thread to another provider instance during a running turn closes its live session and fails the turn with “The provider event stream closed unexpectedly.” Reproduced on Linux with two Claude instances while a foreground `sleep 90` was running.

## Change

The shared orchestration selection path refuses an instance change or a selection that releases a session while the thread has a blocking run. The error tells the caller to wait for the active turn and retry. Idle switches and same-instance selections that apply on the next turn remain available.

## Scope and approval

Fixes #22748
GitCafe: https://git.cafe/pingdotgg/t3code/issues/22748
Triaged bug via the automated issue flow. GitCafe issue [#22748](https://git.cafe/pingdotgg/t3code/issues/22748); fix mirror [#22754](https://git.cafe/pingdotgg/t3code/pulls/22754).

## Verification

At merged head [896c0e10d3](https://github.com/pingdotgg/t3code/commit/896c0e10d30844090740cceec7ba509628c1cbf7):

- `vp test run src/orchestration-v2/Orchestrator.control-reads.test.ts src/orchestration-v2/ProviderSwitchService.test.ts src/orchestration-v2/runtimeLayer.test.ts` from `apps/server`: 109 passed, exit 0. Both selection commands cover active instance changes, session restarts, allowed next-turn changes, and idle switches. Queued monitor delivery is exercised after completing the active run with the queue held.
- `vp run typecheck` from `apps/server`: exit 0; compiler suggestions only.
- Targeted `vp lint` for the three changed files: exit 0; existing `layerUnavailable` unused-variable warning.
- Independent GPT-6.1-Sol and Claude Opus 5.5 reviews both returned `MERGE` with no confirmed bugs; their scoped runs passed 152 and 187 tests respectively. Proof audit returned `SUFFICIENT`.
- [Red test output](https://gh-file-drop-api-prod-mi5fy3sowv63ufte.pinglabs.workers.dev/f/5d2162a8c565f154/red-tests.txt): four preservation assertions failed before the implementation; session and selection were mutated.

[Evidence index](https://gh-file-drop-api-prod-mi5fy3sowv63ufte.pinglabs.workers.dev/f/bab0c3dd3a000c15/index.md). Server-only change; render fixture matrix is empty. No rendering or layout changes.

| Runtime proof | Before | After |
| --- | --- | --- |
| Linux web dev server, Claude Opus 5.5, same-driver instance switch during foreground `sleep 90` | [Transcript](https://gh-file-drop-api-prod-mi5fy3sowv63ufte.pinglabs.workers.dev/f/2c41b575a81c8cb6/before-current-main.txt), main [8acbefefc5](https://github.com/pingdotgg/t3code/commit/8acbefefc5): run failed with the reported stream error | [Transcript](https://gh-file-drop-api-prod-mi5fy3sowv63ufte.pinglabs.workers.dev/f/e7a5d6b6bee7b3a2/after-runtime.txt), [9362d8b716](https://github.com/pingdotgg/t3code/commit/9362d8b716): switch refused; original run and checkpoint completed |

The runtime refusal response is recorded narratively rather than as a timestamped raw RPC response. The after attempt experienced VM load, an exit-137 retry, and a long checkpoint delay; it demonstrates preservation, not timing. The implementation is unchanged between the after capture and merged head. Automated assertions provide the precise mutation and outbox proof.

Unverified: native desktop and mobile clients, separate provider credentials, and an exhaustive matrix of providers. The guard is in the shared server command path.

Model/harness: GPT-6.1-Sol through Codex in T3 Code; independent reviews by GPT-6.1-Sol and Claude Opus 5.5.

