[Bug]: Runtime-mode, instance and worktree changes detach the Claude session and kill live subagents without the background-work check #16000
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#
- Start a Claude thread in
full-accessruntime mode. - In one turn, start two pieces of background work and confirm both are progressing:
- a background Bash command (
run_in_background) that appends a counter line to a file every 5 s; - a background subagent (Agent tool,
run_in_background) that runs 24 short timed steps (about 4 min) and appends one line per step to a second file.
- a background Bash command (
- While both are still running, change the thread's runtime mode in the composer (Full access → Auto-accept edits) and send any message.
- Watch the thread, the two files, and the thread's orchestration events.
The worktree-change and provider-instance-switch paths go through the same detach (see Actual behavior); their trial results will follow in a comment.
Expected behavior#
The same protection #14726 added for model and effort changes: while Claude still has background agents or commands running, a change that ends the Claude CLI process is refused or deferred with a clear, retryable message, and the background work is not lost. The guard's own comment gives the reason: "Background agents and shells run inside the CLI process, so a new selection would kill them and lose their results" (https://github.com/pingdotgg/t3code/blob/efecd3cf8bcec3d1891b5f5a27dc2f6d797c6448/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts#L6939-L6952).
Actual behavior#
Sending the message with the new runtime mode ends the Claude session at once, with no refusal or warning, and the running subagent is killed. In one controlled trial (times UTC, from the thread's events and the two progress files):
- 04:21:59.207 the message is sent:
provider-session.detached, reason "Runtime mode changed." - 04:21:59.291 the subagent goes to
failed, 84 ms later. Its progress file stopped at step 18 of 24 and its completion marker never appeared (checked at T+305 s). - 04:22:01.256 a new provider session attaches. The UI shows "Provider error — The provider event stream closed unexpectedly", the background agent as Failed mid-run, and the new session says the background command and subagent "were reported stopped when the previous session ended."
A note on background shells (not a confirmed T3 defect): in the same trial, our background Bash loop kept writing to its file after T3 and the new session reported it stopped (40 lines at the change, 64 at +120 s). We believe this is an artifact of our harness: the loop's timeout child is reparented and escapes the CLI teardown. We did not test a background command that stays inside the CLI process tree, so we report it only as an observation.
Source at efecd3cf:
- Runtime-mode, worktree and provider/instance changes go through
dispatchThreadMutation, which emitsprovider-session.detachedand a detach effect with no pending-background check (https://github.com/pingdotgg/t3code/blob/efecd3cf8bcec3d1891b5f5a27dc2f6d797c6448/apps/server/src/orchestration-v2/Orchestrator.ts#L3209-L3290). Claude declaressupportsRuntimeModeSwitchInSession: false(https://github.com/pingdotgg/t3code/blob/efecd3cf8bcec3d1891b5f5a27dc2f6d797c6448/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts#L183). - An instance/runtime/workspace change returns
restart_and_resume(https://github.com/pingdotgg/t3code/blob/efecd3cf8bcec3d1891b5f5a27dc2f6d797c6448/apps/server/src/orchestration-v2/ProviderSessionTransitionPolicy.ts#L55-L77); the switch plan releases the session (https://github.com/pingdotgg/t3code/blob/efecd3cf8bcec3d1891b5f5a27dc2f6d797c6448/apps/server/src/orchestration-v2/ProviderSwitchService.ts#L186-L199). - Claude
detachcallsreleaseEntry(reason: "manual_shutdown"), closing the CLI process (https://github.com/pingdotgg/t3code/blob/efecd3cf8bcec3d1891b5f5a27dc2f6d797c6448/apps/server/src/orchestration-v2/ProviderSessionManager.ts#L1912-L2016).hasPendingBackgroundWorkis consulted only on idle release (https://github.com/pingdotgg/t3code/blob/efecd3cf8bcec3d1891b5f5a27dc2f6d797c6448/apps/server/src/orchestration-v2/ProviderSessionManager.ts#L1005-L1061); the #14726 guard covers query replacement only.
Searched before filing (open and closed): "runtime mode background subagent", "worktree handoff background", "kills background agents", "BackgroundWorkBlocks", "provider-session.detached" (only #15695, unrelated). The closest report is #12694, closed by #14726, which guards model and effort replacement; this is the adjacent unguarded set (runtime mode, instance, worktree).
Suggested fix: apply the pending-background-work check before the session detach/restart, as #14726 does for query replacement (refuse with the same retryable message, or defer until the background roster is empty); on a detach, stop or reattach tracked background shells rather than reporting them stopped.
Impact#
Major degradation or frequent failure
Version or commit#
v0.0.46-nightly.20261004.2657 (efecd3cf8bcec3d1891b5f5a27dc2f6d797c6448)
Environment#
Linux x64 container, self-hosted t3 server, Node 24.13.1, Claude Code 2.1.289, Codex 0.160.0; Claude via an API-compatible proxy (not involved)
Logs or stack traces#
04:18:33.352Z provider-session.attached status=ready
04:21:59.096Z subagent.updated status=running (last running update)
04:21:59.207Z provider-session.detached reason="Runtime mode changed."
04:21:59.291Z subagent.updated status=failed
04:22:01.256Z provider-session.attached status=ready (new session)
subagent progress file: 18 of 24 steps; no completion marker at T+305 s
background bash counter: 40 lines at the change, 64 at T+120 s, ran to its timeout (reported stopped)
Screenshots, recordings, or supporting files#
No response
Workaround#
Wait for background agents and commands to finish, or stop them, before changing the runtime mode, the provider instance or the worktree.
OscarsOrbit(GitHub profile, opens in a new tab) added labelsbug
OscarsOrbit(GitHub profile, opens in a new tab) added labelsneeds-triage
Note
Grok responding on behalf of Julius.
Triage#
Thanks for the careful trial and the source pointers, @OscarsOrbit. Your event reasons and timings line up exactly with the code on current
main(cf3e714b0).What I found#
This is the same process kill as #12694, on paths that #14726 didn't cover. It isn't a duplicate of #12694 or #15695.
#14726 only refuses a Claude query replacement (
ClaudeBackgroundWorkBlocksQueryReplacementErrorinopenQuery) while the live process still has background shells or running subagents. Model-only and effort-only changes stay on that path, which matches your control runs.Runtime mode, provider instance, and worktree changes never reach that check.
dispatchThreadMutationemitsprovider-session.detachedand a detach effect without any pending-background test:thread.runtime-mode.setdetaches every live session whose capabilities havesupportsRuntimeModeSwitchInSession: false. Claude sets that to false, and the reason isRuntime mode changed.- A provider-instance change plans
restart_and_resumefor another Claude instance, orcreate_with_handofffor a different driver. Both put the live Claude session inreleaseProviderSessionIds, with the reasonProvider or model selection changed. - A worktree change (
thread.metadata.updatewith a newworktreePath) detaches every live session on the thread, with the reasonWorkspace changed.
Claude runs one thread per process, so the detach calls
releaseEntrywithmanual_shutdownand closes the CLI.hasPendingBackgroundWorkis only checked on idle release, so nothing holds the process here. The subagent goingfailedabout 84 ms later, and the new session's "provider event stream closed unexpectedly", both follow from that close.The background Bash loop that kept writing after being reported stopped is still just an observation. A
timeoutchild can get reparented out of the CLI tree. The in-process subagent is the case the guard was built for.Workaround: as you wrote, wait for background agents and commands to finish (or stop them) before changing runtime mode, provider instance, or worktree.
Likely fix area#
dispatchThreadMutationinOrchestrator.ts, plusProviderSwitchService.ts/ProviderSessionTransitionPolicy.ts. One option is to apply the #14726-style refusal before these detaches: if Claude still has background agents or commands, reject the change with the same retryable message and leave the process running. Another option is to defer the detach until the background work is gone.ProviderSessionManager.ts(releaseEntry). A related question is how tracked background shells should be handled on a detach.
A maintainer will decide on the fix direction.
juliusmarminge removed labelsneeds-triage
juliusmarminge added labelsvia-triage
I started a fix for this and stopped, because the direction is still open. These are the findings that bear on that choice. They come from reading
mainate22c88043. I ran nothing, so each one is a code reading and not a test result.F1. The worktree handoff detach is intended. The
t3_worktree_handoffdescription tells the agent that the detach ends its turn and to call the tool last (tools.ts#L27). The service queues the continuation because "the detach that the metadata update schedules will terminate the calling session" (WorktreeMcpService.ts#L357-L362). For this trigger the choice is whether a handoff waits for, or refuses on, the calling agent's own background work.F2. The source a check reads decides what it covers.
derivePendingBackgroundWorkreturns an empty list while any run is preparing, starting, or running (orchestrationV2PendingBackgroundWork.ts#L224-L232). A check built on it would miss a change made during a running turn. That includes every handoff, and at3_thread_configurecall an agent makes on its own thread.- The live Claude session's
hasPendingBackgroundWorkdoes not depend on run state (ClaudeAdapterV2.ts#L7477-L7503). It reads the same in-process roster and subagent state as the #14726 guard (#L6880-L6907). #16073 reports that guard refusing sends while the client showed no Stop, so a check on this source can produce the same stuck state.
F3. Three more detach sites have no check. A check placed only in
dispatchThreadMutationleaves them open.- When the worktree changes, the web branch picker starts
stopThreadSessionwithout awaiting it and separately updates the worktree.stopThreadSessionsendsprovider-session.detachfor every session (BranchToolbarBranchSelector.tsx#L170-L184,commands.ts#L897-L913). The handler only checks that the session belongs to the thread (Orchestrator.ts#L3301-L3352). - A queued run that starts with a different selection detaches the switch plan's sessions (
Orchestrator.ts#L1659-L1664,#L1796-L1805). - A send that moves to another instance and resumes the same native thread detaches the old session (
Orchestrator.ts#L6282-L6309).
F4. Three callers would handle a refusal badly today.
- Mobile sends the runtime-mode change with a command id derived from the queued message, and always retries a settings failure (
use-thread-outbox-drain.ts#L202-L204,#L810-L823,thread-outbox-model.ts#L297-L310). A refused command leaves a rejected receipt, and the same id then returnsOrchestratorCommandPreviouslyRejectedError(Orchestrator.ts#L9710-L9717,#L9766-L9786). After one refusal, the queued message would keep failing after the background work ends. t3_thread_configuremaps every dispatch failure to "The operation could not be completed." (handlers.ts#L187-L194,threadAccess.ts#L18-L22).t3_worktree_handoffreports the error's.message(WorktreeMcpService.ts#L337-L341).OrchestratorDispatchError.messageis generic, so a refusal that carries its text incausewould lose it (Orchestrator.ts#L131-L133).
F5. The runtime-mode detach is not Claude-only. It applies to every session whose adapter sets
supportsRuntimeModeSwitchInSession: false(Orchestrator.ts#L3216-L3221). That is also the ACP base, Grok, Cursor, Pi, andOpenCodeAdapterV2. Each is a single-thread session, so the detach releases it (ProviderSessionManager.ts#L2009-L2018). I did not trace what background work each of those processes holds.Written by Claude Fable 5.1 on behalf of ScottN-PV.
Another case on the instance-switch path. It differs from the trials above in one way: the killed task never leaves
running.Setup. A Claude thread and two Claude provider instances that share one Claude home, so the switch resumes the same native thread. A Workflow tool task (
task_type: local_workflow) was running in the background on instance A. The run that launched it had already completed. The next message went to instance B.Timeline (UTC, from the provider event log):
- 02:48:33
task_startedfor the workflow on instance A. Its run completed at 02:48:44, and the workflow kept going in the background. - 03:01:45.973 last
task_progressfor the task. - 03:01:47 a new run starts on instance B (instance switch, same native thread).
- 03:01:49.141
query.closeon instance A. 03:01:49.249query.openwithresumeon instance B. - No
task_notificationfor the task ever arrives.
State 11+ hours later:
- The task's
subagentturn item and itsprojection_subagentsrow are stillrunning, last updated 03:01:45.974. - The latest run is
completed, and the provider thread's background roster is empty. Instance A's old session row still readsready. - The composer shows "Waiting on subagent …" with a Stop button, and the thread never settles on its own.
- Two later
TaskStopcalls from the agent returned "No task found". Five later session detaches did not clear it.
In the trials above, the subagent went to
failed84 ms after the detach. I don't know why this one didn't. One difference is the task type: a Workflow, not an Agent call.I saw this on a source build from 2026-10-09, which is older than main. The reading below is on current main (
98a6246d0, rechecked ona01c48f86), where I found nothing that would end this item. I have not reproduced it on main.Why nothing ends it:
- The send that moves the thread to another instance detaches the old session without a background-work check (Orchestrator.ts#L6511-L6538). This is the gap this issue describes.
- When the stream fails, the launching run's ingestion only logs it if the run is already finalized. So the fallback that closes the run's open subagents doesn't run (RunExecutionService.ts#L1324-L1333).
- The cleanup that runs when a query's stream ends only ends tool calls that subagents left open. It does not end the subagent item itself (ClaudeAdapterV2.ts#L5718-L5735).
- Each session keeps its own subagent registry (ClaudeAdapterV2.ts#L3470), so the session on instance B doesn't know the task. That is why
TaskStopsays "No task found". It is also why #16814 wouldn't reach it, since that PR only cleans up subagents from an earlier process in the same session. - The pending background work list keeps any active
subagentitem. It doesn't check the item's provider instance or whether its session is still alive (orchestrationV2PendingBackgroundWork.ts#L186-L203), so the strip stays.
Stop on the strip does nothing, as in #16586. There are 515 newer turn items, so the task falls outside the 77-row snapshot that
interruptThreadTurnreads. Stop then returns{sequence: 0}and sends norun.interrupt. #16867 keeps unfinishedsubagentitems in that snapshot, so Stop would reach this one. That gives a manual way out, but the item staysrunninguntil someone presses Stop.Workaround today: restart the app. Startup recovery marks the item
cancelled.Suggestion: whichever direction this issue takes (refuse or defer), if a switch still detaches the old session, that session's background items should end as
interruptedonce the detach goes through. Then a killed task doesn't stayrunning.Note
Responses generated with Claude (model: Claude Opus 5.5)
- 02:48:33
Triage on main a12d6ed942: the runtime-mode change path still schedules provider-session detach in Orchestrator.ts. No runtime reproduction or fix attempted: changing this behavior crosses the automated triage hard stop for runtime modes. Leaving open for maintainer review.
#23082 takes the runtime-mode path only. Changing the mode no longer detaches the session. The new mode applies when the next turn starts, and that restart is refused while background agents or commands are still running (the #14726 guard). Instance and worktree handoff are unchanged.
I also have a follow-up that applies a mode change to the turn that's already running (Claude, ACP/Grok, OpenCode 2.x, Muse), and makes the picker send the change right away. I'll open an Ideas discussion for it once the direction here is settled.
Another case on the runtime-mode path, on the v0.1.6 Windows desktop app (released 2026-10-31). PR #23082 is still open and this build doesn't have it. This one adds a mid-tool-call failure and a comparison of which subagents survive.
Setup. A long Claude thread (Opus 5.5) acting as coordinator, with an active run that had just started a native
Agentsubagent, plus other native background subagents, and fivedelegate_taskchildren running in their own sessions. The thread's runtime mode then changed fromautotofull-accessfrom the client, followed by a new message.What happened (server trace and thread activity, all 2026-11-02 UTC).
Time Event 22:59:57.888 EnvironmentCommands.setThreadRuntimeMode22:59:57.908 ProviderSessionManager.detach(20 ms later, exitSuccess)22:59:57.935 The active run ends as failed: "The provider event stream closed unexpectedly. Retry the turn; if it keeps failing, check the provider and server logs."22:59:57.935 The native Agentsubagent started in that run ends asfailedWhat the agent sees afterwards.
- The call that started that subagent returns only: "Tool call interrupted: the session ended before this call's result was recorded, so its outcome is unknown."
- A native background subagent from the same session later arrives as a task notification with status
stopped: "didn't finish before the previous session ended". Its transcript is kept, but its work stops. - Nothing tells the user or the agent that the mode change was the cause. The error reads like a provider fault, and suggests retrying.
What survived. All five
delegate_taskchildren kept running, because they have their own sessions. So the loss only affects nativeAgentwork in the session whose mode changed, which is the same split the guard's comment describes.Expected. As #16000 asks: refuse or defer while background work is live, or apply the mode without ending the process, as #23082 proposes. At minimum, say that the mode change ended the session instead of showing a generic stream error.
Another observation on the same detach, on v0.1.8-nightly.20261106.4262 (macOS, Claude provider), shared as data in case it helps. We are not sure whether parts of it are intended: the code comment above the detach reads "Archive and settle stop idle background work", which may be deliberate for settle and archive.
What we saw in practice. An external client moved several idle Claude threads to another Claude instance with
thread.model-selection.set, once each thread's turn had ended. Four threads had a background Bash command in flight, registered witht3_background_wait. In each one the command ended with<status>killed</status>a few seconds after the switch. The thread did not receive the result and stayed idle until someone wrote to it.Minimal reproduction (v0.1.8-nightly.20261106.4262). Two throwaway threads, each running
sleep 900; echo …in the background witht3_background_waitarmed, with the task visible in the shell snapshot'spendingBackgroundTasksbefore the action:Command sent to an idle thread Accepted Background task thread.model-selection.setto another Claude instanceyes, no refusal <status>killed</status>within seconds; the new session got the killed notice through the handoff and started the command againthread.settle(through the MCPt3_thread_organize)yes <status>killed</status>; thread settled withpendingBackgroundTasksemptyWhere it differs from message dispatch. When the same instance switch comes with a message, the server refuses it with "The previous provider is still running background agents or commands, and switching would end them…" (
sessionHasBackgroundWork,apps/server/src/orchestration-v2/Orchestrator.tsaround line 6529). The direct commands reach the detach around line 4327 (thread.archive/thread.settletake every provider session,thread.model-selection.set/provider.switchtakereleaseProviderSessionIds) without that check. Settlement does refuse active or blocked runs, so this only shows up after the run has completed while background work is still running.Related: #30790 (auto-settle right after an awaited task ends), #30430 (provider switch with OpenCode leaves the work running instead), PRs #24446 and #30447.
For the record, from a different angle: we tried telling the thread instead of refusing (let the change through, then deliver one "its provider session was closed when the thread changed (runtime mode changed)" notice naming the awaited tasks). It works, but #24446's refusal keeps the work alive, which is the better outcome, so we are dropping ours in favour of it. Workspace changes and model or provider switches through
thread.model-selection.setandprovider.switchwent through the same silent removal in our tests; worth confirming #24446 covers both.juliusmarminge closed this
Sign in to join this conversation.
Further results from the same controlled setup (v0.0.46-nightly.20261004.2657, efecd3cf), each trial with a background subagent and a background shell confirmed live before the change:
t3_thread_configure)t3_worktree_handoff)So the #14726 guard holds for model and effort changes, and every other change that ends the session (instance, worktree, runtime mode) kills live background subagents without a check. One more detail: the Claude-to-Claude instance switch took the same release path and reason as the switch to Codex, not a restart-and-resume path.