TUI: User selected agent/model is overwritten by server defaults when creating a new session #4830

Open
opened 2026-02-16 17:45:36 -05:00 by yindo · 2 comments
Owner

Originally created by @ykswang on GitHub (Jan 12, 2026).

Originally assigned to: @rekram1-node on GitHub.

Description

Title:
TUI: User selected agent/model is overwritten by server defaults when creating a new session
Description:
When creating a new session in the TUI (via the Home screen), the user's manually selected Agent (e.g., "Plan") or Model is unexpectedly reset to the server's default configuration (e.g., "Build" / "Google") immediately after sending the first message.
This regression appears to be introduced in v1.1.13.

What happened?

  1. On the TUI Home screen, I manually selected the "Plan" agent (or a specific model like Claude).
  2. I typed a prompt and pressed Enter to start the session.
  3. The TUI navigated to the new session view.
  4. Immediately upon loading the session view, the Agent/Model indicator switched back to the default "Build" (and often "Google" as the provider), ignoring my initial selection.

Root Cause Analysis:
The issue is caused by a race condition in packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx.
A createEffect hook monitors sessionID changes and attempts to restore the state (Agent/Model) from the last user message of the session:

// packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx
createEffect(() => {
  const sessionID = props.sessionID
  // ...
  const msg = lastUserMessage() // Fetches the last message with role="user"
  if (sessionID !== syncedSessionID) {
    // ...
    // PROBLEM: This logic aggressively overwrites local state with message metadata
    if (msg.agent) local.agent.set(msg.agent)
    if (msg.model) local.model.set(msg.model)
  }
})

In v1.1.13, changes to the component's lifecycle (moving ref initialization to onMount) seemingly altered the timing of rendering vs. data synchronization.
When navigating to a newly created session:

  1. The first message is synced from the server.
  2. Since it's a new session, the server metadata for this message might still reflect default values (or be initialized with defaults like "Build").
  3. The createEffect triggers, sees this "default" metadata on the message, and forcefully resets the local TUI state, overwriting the user's manual selection.

Expected behavior:
When creating a new session from the Home screen, the TUI should respect the Agent/Model selected by the user during creation and not overwrite it with the server's initial default state.

Suggested Fix:
Modify the navigation logic to pass a flag (e.g., created: true) when transitioning from Home to Session. The Prompt component should check this flag and skip the state restoration logic for newly created sessions.

Plugins

"opencode-antigravity-auth@1.2.8", "@franlol/opencode-md-table-formatter@0.0.3"

OpenCode version

1.1.13

Steps to reproduce

  1. Start OpenCode TUI (opencode).
  2. On the Home screen, use Ctrl+x a to switch the agent to "Plan" (or any non-default agent).
  3. Type a message and submit.
  4. Observe that as soon as the session view loads, the agent reverts to "Build".

Screenshot and/or share link

Image Image

Operating System

macOS

Terminal

iTerm2

Originally created by @ykswang on GitHub (Jan 12, 2026). Originally assigned to: @rekram1-node on GitHub. ### Description **Title:** TUI: User selected agent/model is overwritten by server defaults when creating a new session **Description:** When creating a new session in the TUI (via the Home screen), the user's manually selected Agent (e.g., "Plan") or Model is unexpectedly reset to the server's default configuration (e.g., "Build" / "Google") immediately after sending the first message. This regression appears to be introduced in **v1.1.13**. **What happened?** 1. On the TUI Home screen, I manually selected the **"Plan"** agent (or a specific model like Claude). 2. I typed a prompt and pressed Enter to start the session. 3. The TUI navigated to the new session view. 4. Immediately upon loading the session view, the Agent/Model indicator switched back to the default **"Build"** (and often **"Google"** as the provider), ignoring my initial selection. **Root Cause Analysis:** The issue is caused by a race condition in `packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx`. A `createEffect` hook monitors `sessionID` changes and attempts to restore the state (Agent/Model) from the **last user message** of the session: ```typescript // packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx createEffect(() => { const sessionID = props.sessionID // ... const msg = lastUserMessage() // Fetches the last message with role="user" if (sessionID !== syncedSessionID) { // ... // PROBLEM: This logic aggressively overwrites local state with message metadata if (msg.agent) local.agent.set(msg.agent) if (msg.model) local.model.set(msg.model) } }) ``` In v1.1.13, changes to the component's lifecycle (moving ref initialization to onMount) seemingly altered the timing of rendering vs. data synchronization. When navigating to a newly created session: 1. The first message is synced from the server. 2. Since it's a new session, the server metadata for this message might still reflect default values (or be initialized with defaults like "Build"). 3. The createEffect triggers, sees this "default" metadata on the message, and forcefully resets the local TUI state, overwriting the user's manual selection. Expected behavior: When creating a new session from the Home screen, the TUI should respect the Agent/Model selected by the user during creation and not overwrite it with the server's initial default state. Suggested Fix: Modify the navigation logic to pass a flag (e.g., created: true) when transitioning from Home to Session. The Prompt component should check this flag and skip the state restoration logic for newly created sessions. ### Plugins "opencode-antigravity-auth@1.2.8", "@franlol/opencode-md-table-formatter@0.0.3" ### OpenCode version 1.1.13 ### Steps to reproduce 1. Start OpenCode TUI (`opencode`). 2. On the Home screen, use `Ctrl+x a` to switch the agent to "Plan" (or any non-default agent). 3. Type a message and submit. 4. Observe that as soon as the session view loads, the agent reverts to "Build". ### Screenshot and/or share link <img width="801" height="651" alt="Image" src="https://github.com/user-attachments/assets/08f0c4ac-71e8-4016-b48b-8145450c9163" /> <img width="803" height="658" alt="Image" src="https://github.com/user-attachments/assets/cedacb01-ea26-4938-bfae-d98d4c715b43" /> ### Operating System macOS ### Terminal iTerm2
yindo added the opentuibug labels 2026-02-16 17:45:36 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Jan 12, 2026):

This issue might be a duplicate of existing issues. Please check:

  • #7804: ACP loadSessionMode doesn't restore persisted model/agent from session messages - Similar root cause where model/agent selection resets to defaults when loading sessions
  • #4930: Feature Request: Retain current model and agent selection when switching sessions - Related issue about model and agent selection not persisting across session switches
  • #4901: Bug: SDK Messages cause switch back to default model - SDK messages trigger model switching back to defaults
  • #4344: Selected model for agent in TUI doesn't stick between Agent Alternation - Agent model selection not persisting when switching between agents

Feel free to ignore if none of these address your specific case.

@github-actions[bot] commented on GitHub (Jan 12, 2026): This issue might be a duplicate of existing issues. Please check: - #7804: ACP loadSessionMode doesn't restore persisted model/agent from session messages - Similar root cause where model/agent selection resets to defaults when loading sessions - #4930: Feature Request: Retain current model and agent selection when switching sessions - Related issue about model and agent selection not persisting across session switches - #4901: Bug: SDK Messages cause switch back to default model - SDK messages trigger model switching back to defaults - #4344: Selected model for agent in TUI doesn't stick between Agent Alternation - Agent model selection not persisting when switching between agents Feel free to ignore if none of these address your specific case.
Author
Owner

@nexxai commented on GitHub (Jan 30, 2026):

This is so frustrating. Really hope it gets solved soon.

@nexxai commented on GitHub (Jan 30, 2026): This is so frustrating. Really hope it gets solved soon.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#4830