Skip to content

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

Closed as not planned

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)

Activity

  1. github-actions commented on Feb 13, 2026

    @github-actions
    Contributor

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

    Related feature requests:

    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.

  2. added a commit that references this issue on Feb 15, 2026
    a7e8a73
  3. self-assigned this
    on Feb 16, 2026
  4. art-shen commented on Feb 19, 2026

    @art-shen

    Yes, it's really annoying. And when I switch through the agents, the manually chosen model is also overwritten. I think it should be saved for the current session when I choose one manually.

  5. EruditeCoder108 commented on Mar 21, 2026

    @EruditeCoder108

    The agent.set function in opencode/packages/app/src/context/local.tsx at L226 incorrectly prioritizes the new agent's default model over the existing session-specific model. The line model: item.model ?? prev?.model, means that if the item (the new agent) has a default model (item.model is defined), it will unconditionally overwrite the prev?.model (which holds the user's manual model selection for the current session) when setSaved is called. This leads to manual selections being lost upon agent switches.

    Location:opencode/packages/app/src/context/local.tsx

    Modify the agent.set function in opencode/packages/app/src/context/local.tsx at line 226. Change the expression for the model property within the next state object from item.model ?? prev?.model to prev?.model ?? item.model. This simple change reverses the priority, ensuring that any existing session-specific model (which would include a user's manual selection) is preserved when an agent is switched. The new agent's default model (item.model) will only be applied if no model was previously set for the current session (prev?.model is undefined).

    The original code model: item.model ?? prev?.model within the agent.set function meant that if the newly selected agent (item) had a default model (item.model was not undefined), that default would always be selected. This unconditionally overwrote any previous model in prev?.model that was manually set by the user for the current session. The fix model: prev?.model ?? item.model reverses this priority. It now instructs the system to first check if there's already a model set in the current session's state (prev?.model). If a model exists there, it will be used, thereby preserving the user's manual selection. If prev?.model is undefined (meaning no model has been explicitly set for the session yet), it will then fall back to using the new agent's default model (item.model). This ensures that user's explicit model choices are respected and not overwritten when merely switching agents, while still providing a sensible default if no specific model has been chosen for a session.

  6. dhruvkej9 commented on Mar 21, 2026

    @dhruvkej9

    The agent.set function in opencode/packages/app/src/context/local.tsx at L226 incorrectly prioritizes the new agent's default model over the existing session-specific model. The line model: item.model ?? prev?.model, means that if the item (the new agent) has a default model (item.model is defined), it will unconditionally overwrite the prev?.model (which holds the user's manual model selection for the current session) when setSaved is called. This leads to manual selections being lost upon agent switches.

    Location:opencode/packages/app/src/context/local.tsx

    Modify the agent.set function in opencode/packages/app/src/context/local.tsx at line 226. Change the expression for the model property within the next state object from item.model ?? prev?.model to prev?.model ?? item.model. This simple change reverses the priority, ensuring that any existing session-specific model (which would include a user's manual selection) is preserved when an agent is switched. The new agent's default model (item.model) will only be applied if no model was previously set for the current session (prev?.model is undefined).

    The original code model: item.model ?? prev?.model within the agent.set function meant that if the newly selected agent (item) had a default model (item.model was not undefined), that default would always be selected. This unconditionally overwrote any previous model in prev?.model that was manually set by the user for the current session. The fix model: prev?.model ?? item.model reverses this priority. It now instructs the system to first check if there's already a model set in the current session's state (prev?.model). If a model exists there, it will be used, thereby preserving the user's manual selection. If prev?.model is undefined (meaning no model has been explicitly set for the session yet), it will then fall back to using the new agent's default model (item.model). This ensures that user's explicit model choices are respected and not overwritten when merely switching agents, while still providing a sensible default if no specific model has been chosen for a session.

    @rekram1-node

  7. EruditeCoder108 commented on Mar 23, 2026

    @EruditeCoder108

    Looks like a duplicate of my earlier observation 👍

  8. github-actions commented on May 22, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

  9. dhruvkej9 commented on Jun 1, 2026

    @dhruvkej9

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

    @rekram1-node does closing issues with bugs fix them?

  10. rekram1-node commented on Jun 1, 2026

    @rekram1-node
    Contributor

    @dhruvkej9 can u reproduce this issue? I cannot

    also this wasnt marked as completed so I dont understand ur msg here, it was marked as stale, which it is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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