Skip to content

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

@lxw322

Description

@lxw322

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

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)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions