opencode icon indicating copy to clipboard operation
opencode copied to clipboard

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

Open ykswang opened this issue 3 days ago • 1 comments

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

"[email protected]", "@franlol/[email protected]"

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

ykswang avatar Jan 12 '26 08:01 ykswang