-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Description
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
- Launch OpenCode TUI (
opencode) - Open the model switcher dialog (usually via keyboard shortcut or UI button)
- Select a different model than the current default
- Send a message or wait for a server sync
- Observed: The model reverts to the agent's default model
- 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:
- CLI flag:
opencode --model <provider>/<model-id> - Config file: Set
"model": "<provider>/<model-id>"in~/.config/opencode/opencode.jsonc
Related Issues
- Model selection is "lost" on continuation and Agent selection is "lost" on compaction #8349 - Model selection "lost" on continuation
- Model switches back to the original selection after prompting #4281 - Model switches back after prompting
- Selected model for agent in TUI doesn't stick between Agent Alternation #4344 - Model doesn't stick between Agent Alternation
Suggested Fix
- 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,
})
}
}
})- Persist the
modelmap in thesave()function to retain manual selections across sessions.
Environment
- OpenCode version: 1.1.65
- OS: macOS (Darwin)