Skip to content

fix(session): bypass child-session guard during mode initialization#401

Merged
sudo-tee merged 1 commit into
sudo-tee:mainfrom
hermandavid:fix/child-to-parent-session-switch
Jun 5, 2026
Merged

fix(session): bypass child-session guard during mode initialization#401
sudo-tee merged 1 commit into
sudo-tee:mainfrom
hermandavid:fix/child-to-parent-session-switch

Conversation

@hermandavid
Copy link
Copy Markdown
Contributor

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 because session_runtime.switch_session calls state.model.clear() followed by agent_model.ensure_current_mode(). The clear() wipes the current mode, and ensure_current_mode previously delegated to switch_to_mode, which contains a guard that rejects mode changes when the active session has a parentID. During session switching, the old child session is still the active session when ensure_current_mode runs, so the guard fires incorrectly.

Solution

Extract the mode+model application logic from switch_to_mode into a private apply_mode helper. switch_to_mode retains the child-session guard for user-initiated mode changes, then delegates to apply_mode. ensure_current_mode calls apply_mode directly, bypassing the guard that is only relevant for interactive mode switching, not system initialization during session transitions.

Changes

  • Extract apply_mode helper (@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 to apply_mode instead of inlining the mode/model logic.

  • ensure_current_mode (agent_model.lua) — Selects the default or first available agent, then calls apply_mode directly instead of going through switch_to_mode.

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.
@sudo-tee
Copy link
Copy Markdown
Owner

sudo-tee commented Jun 5, 2026

Thanks for the fix :)

@sudo-tee sudo-tee merged commit 33baadf into sudo-tee:main Jun 5, 2026
5 checks passed
@hermandavid hermandavid deleted the fix/child-to-parent-session-switch branch June 5, 2026 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants