fix(session): bypass child-session guard during mode initialization#401
Merged
sudo-tee merged 1 commit intoJun 5, 2026
Merged
Conversation
ensure_current_mode delegates to switch_to_mode, which guards against mode changes in child sessions. During session switching, model.clear() nils current_mode, then ensure_current_mode tries to re-initialize it -- but the guard sees the still-active child session and rejects the change. Extract the mode+model application into a private apply_mode helper. switch_to_mode validates and guards, then delegates to it. ensure_current_mode selects the default mode and calls apply_mode directly, bypassing the guard that is only meant for user-initiated mode switches.
Owner
|
Thanks for the fix :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue
Switching from a child session to a parent session shows
"Cannot switch agent in child session"and fails to initialize the agent mode. This happens becausesession_runtime.switch_sessioncallsstate.model.clear()followed byagent_model.ensure_current_mode(). Theclear()wipes the current mode, andensure_current_modepreviously delegated toswitch_to_mode, which contains a guard that rejects mode changes when the active session has aparentID. During session switching, the old child session is still the active session whenensure_current_moderuns, so the guard fires incorrectly.Solution
Extract the mode+model application logic from
switch_to_modeinto a privateapply_modehelper.switch_to_moderetains the child-session guard for user-initiated mode changes, then delegates toapply_mode.ensure_current_modecallsapply_modedirectly, bypassing the guard that is only relevant for interactive mode switching, not system initialization during session transitions.Changes
Extract
apply_modehelper (@agent_model.lua) — Private async function that sets the mode on state and resolves the associated model from config (user override, agent config, or global fallback). No session guards; callers are responsible for validation.switch_to_mode(agent_model.lua) — Keeps the child-session guard and mode validation, then delegates toapply_modeinstead of inlining the mode/model logic.ensure_current_mode(agent_model.lua) — Selects the default or first available agent, then callsapply_modedirectly instead of going throughswitch_to_mode.