From f1abed5c502209d5d80c478e1ddd5a8bb3f05aee Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 30 Apr 2026 15:23:21 +0000 Subject: [PATCH] fix(tui): retain current model and agent across session switches (#4930) Only initialize the agent/model/variant from a session's last user message on the very first session sync. On subsequent session switches, preserve the user's current selection instead of reverting to the session's historical configuration. --- .../cli/cmd/tui/component/prompt/index.tsx | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx b/packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx index cd47e917085b..ced6465ed3cd 100644 --- a/packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx +++ b/packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx @@ -241,26 +241,32 @@ export function Prompt(props: PromptProps) { ), ) - // Initialize agent/model/variant from last user message when session changes + // Initialize agent/model/variant from the session's last user message on the + // first session that loads. On subsequent session switches, preserve the + // user's current agent/model selection rather than reverting to the session's + // historical configuration. let syncedSessionID: string | undefined + let initialized = false createEffect(() => { const sessionID = props.sessionID const msg = lastUserMessage() - if (sessionID !== syncedSessionID) { - if (!sessionID || !msg) return + if (sessionID === syncedSessionID) return + if (!sessionID || !msg) return - syncedSessionID = sessionID + syncedSessionID = sessionID - // Only set agent if it's a primary agent (not a subagent) - const isPrimaryAgent = local.agent.list().some((x) => x.name === msg.agent) - if (msg.agent && isPrimaryAgent) { - // Keep command line --agent if specified. - if (!args.agent) local.agent.set(msg.agent) - if (msg.model) { - local.model.set(msg.model) - local.model.variant.set(msg.model.variant) - } + if (initialized) return + initialized = true + + // Only set agent if it's a primary agent (not a subagent) + const isPrimaryAgent = local.agent.list().some((x) => x.name === msg.agent) + if (msg.agent && isPrimaryAgent) { + // Keep command line --agent if specified. + if (!args.agent) local.agent.set(msg.agent) + if (msg.model) { + local.model.set(msg.model) + local.model.variant.set(msg.model.variant) } } })