[Bug] TUI model selection gets overwritten by agent default model #9244

Open
opened 2026-02-16 18:11:59 -05:00 by yindo · 1 comment
Owner

Originally created by @lxw322 on GitHub (Feb 13, 2026).

Originally assigned to: @rekram1-node on GitHub.

Description

When a user manually selects a different model in the TUI's model switcher dialog, the selection is overwritten by the agent's default model shortly after. This happens due to a createEffect that automatically enforces the agent's default model whenever the agent state is refreshed.

Steps to Reproduce

  1. Launch OpenCode TUI (opencode)
  2. Open the model switcher dialog (usually via keyboard shortcut or UI button)
  3. Select a different model than the current default
  4. Send a message or wait for a server sync
  5. Observed: The model reverts to the agent's default model
  6. Expected: The manually selected model should persist for the current session

Root Cause Analysis

1. Effect Overwrites Manual Selection

File: packages/opencode/src/cli/cmd/tui/context/local.tsx#L385-L400

// Automatically update model when agent changes
createEffect(() => {
  const value = agent.current()
  if (value.model) {
    if (isModelValid(value.model))
      model.set({
        providerID: value.model.providerID,
        modelID: value.model.modelID,
      })
    // ...
  }
})

This effect triggers whenever agent.current() returns a new object reference (e.g., during sync/reconcile operations), and it overwrites the user's manual model selection stored in modelStore.model[agentName].

2. Model Selection Not Persisted

File: packages/opencode/src/cli/cmd/tui/context/local.tsx#L132-L141

function save() {
  // ...
  Bun.write(
    file,
    JSON.stringify({
      recent: modelStore.recent,
      favorite: modelStore.favorite,
      variant: modelStore.variant,
      // BUG: modelStore.model (per-agent selection) is missing!
    }),
  )
}

The model map (containing user's per-agent model overrides) is not included in the persisted state, causing selections to be lost on restart.

Workaround

Users can force a specific model by:

  1. CLI flag: opencode --model <provider>/<model-id>
  2. Config file: Set "model": "<provider>/<model-id>" in ~/.config/opencode/opencode.jsonc

Related Issues

  • #8349 - Model selection "lost" on continuation
  • #4281 - Model switches back after prompting
  • #4344 - Model doesn't stick between Agent Alternation

Suggested Fix

  1. Modify the effect to check if a user has already set a model override before applying the agent default:
createEffect(() => {
  const value = agent.current()
  if (value.model) {
    // Only set default if user hasn't made a manual selection
    if (!modelStore.model[value.name] && isModelValid(value.model)) {
      model.set({
        providerID: value.model.providerID,
        modelID: value.model.modelID,
      })
    }
  }
})
  1. Persist the model map in the save() function to retain manual selections across sessions.

Environment

  • OpenCode version: 1.1.65
  • OS: macOS (Darwin)
Originally created by @lxw322 on GitHub (Feb 13, 2026). Originally assigned to: @rekram1-node on GitHub. ## Description When a user manually selects a different model in the TUI's model switcher dialog, the selection is overwritten by the agent's default model shortly after. This happens due to a `createEffect` that automatically enforces the agent's default model whenever the agent state is refreshed. ## Steps to Reproduce 1. Launch OpenCode TUI (`opencode`) 2. Open the model switcher dialog (usually via keyboard shortcut or UI button) 3. Select a different model than the current default 4. Send a message or wait for a server sync 5. **Observed**: The model reverts to the agent's default model 6. **Expected**: The manually selected model should persist for the current session ## Root Cause Analysis ### 1. Effect Overwrites Manual Selection **File**: [`packages/opencode/src/cli/cmd/tui/context/local.tsx#L385-L400`](https://github.com/anomalyco/opencode/blob/b8ee88212639ec63f4fe87555b5e87f74643e76b/packages/opencode/src/cli/cmd/tui/context/local.tsx#L385-L400) ```typescript // Automatically update model when agent changes createEffect(() => { const value = agent.current() if (value.model) { if (isModelValid(value.model)) model.set({ providerID: value.model.providerID, modelID: value.model.modelID, }) // ... } }) ``` This effect triggers whenever `agent.current()` returns a new object reference (e.g., during sync/reconcile operations), and it overwrites the user's manual model selection stored in `modelStore.model[agentName]`. ### 2. Model Selection Not Persisted **File**: [`packages/opencode/src/cli/cmd/tui/context/local.tsx#L132-L141`](https://github.com/anomalyco/opencode/blob/b8ee88212639ec63f4fe87555b5e87f74643e76b/packages/opencode/src/cli/cmd/tui/context/local.tsx#L132-L141) ```typescript function save() { // ... Bun.write( file, JSON.stringify({ recent: modelStore.recent, favorite: modelStore.favorite, variant: modelStore.variant, // BUG: modelStore.model (per-agent selection) is missing! }), ) } ``` The `model` map (containing user's per-agent model overrides) is not included in the persisted state, causing selections to be lost on restart. ## Workaround Users can force a specific model by: 1. **CLI flag**: `opencode --model <provider>/<model-id>` 2. **Config file**: Set `"model": "<provider>/<model-id>"` in `~/.config/opencode/opencode.jsonc` ## Related Issues - #8349 - Model selection "lost" on continuation - #4281 - Model switches back after prompting - #4344 - Model doesn't stick between Agent Alternation ## Suggested Fix 1. **Modify the effect** to check if a user has already set a model override before applying the agent default: ```typescript createEffect(() => { const value = agent.current() if (value.model) { // Only set default if user hasn't made a manual selection if (!modelStore.model[value.name] && isModelValid(value.model)) { model.set({ providerID: value.model.providerID, modelID: value.model.modelID, }) } } }) ``` 2. **Persist the `model` map** in the `save()` function to retain manual selections across sessions. ## Environment - OpenCode version: 1.1.65 - OS: macOS (Darwin)
Author
Owner

@github-actions[bot] commented on GitHub (Feb 13, 2026):

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

  • #8349: Model selection is "lost" on continuation and Agent selection is "lost" on compaction
  • #7948: TUI: User selected agent/model is overwritten by server defaults when creating a new session
  • #12250: Selected model changes for some reason, even the child processes don't take the selected model

Related feature requests:

  • #12669: Session-persistent model override via Ctrl+M
  • #4930: Retain current model and agent selection when switching sessions

If this addresses a slightly different case or provides more comprehensive analysis than the existing issues, feel free to keep it open and reference the related issues for context.

@github-actions[bot] commented on GitHub (Feb 13, 2026): This issue might be a duplicate of existing issues. Please check: - #8349: Model selection is "lost" on continuation and Agent selection is "lost" on compaction - #7948: TUI: User selected agent/model is overwritten by server defaults when creating a new session - #12250: Selected model changes for some reason, even the child processes don't take the selected model Related feature requests: - #12669: Session-persistent model override via Ctrl+M - #4930: Retain current model and agent selection when switching sessions If this addresses a slightly different case or provides more comprehensive analysis than the existing issues, feel free to keep it open and reference the related issues for context.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#9244