Repository navigation
[Bug] TUI model selection gets overwritten by agent default model #13456
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)
Activity
github-actions commentedon Feb 13, 2026
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.
fix(opencode): stop overwriting manual model selection on agent data …
art-shen commentedon Feb 19, 2026
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.
EruditeCoder108 commentedon Mar 21, 2026
The
agent.setfunction inopencode/packages/app/src/context/local.tsxat L226 incorrectly prioritizes the new agent's default model over the existing session-specific model. The linemodel: item.model ?? prev?.model,means that if theitem(the new agent) has a default model (item.modelis defined), it will unconditionally overwrite theprev?.model(which holds the user's manual model selection for the current session) whensetSavedis called. This leads to manual selections being lost upon agent switches.Location:opencode/packages/app/src/context/local.tsx
Modify the
agent.setfunction inopencode/packages/app/src/context/local.tsxat line 226. Change the expression for themodelproperty within thenextstate object fromitem.model ?? prev?.modeltoprev?.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?.modelis undefined).The original code
model: item.model ?? prev?.modelwithin theagent.setfunction meant that if the newly selected agent (item) had a default model (item.modelwas notundefined), that default would always be selected. This unconditionally overwrote any previous model inprev?.modelthat was manually set by the user for the current session. The fixmodel: prev?.model ?? item.modelreverses 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. Ifprev?.modelisundefined(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.dhruvkej9 commentedon Mar 21, 2026
@rekram1-node
EruditeCoder108 commentedon Mar 23, 2026
Looks like a duplicate of my earlier observation 👍
github-actions commentedon May 22, 2026
To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.
dhruvkej9 commentedon Jun 1, 2026
@rekram1-node does closing issues with bugs fix them?
rekram1-node commentedon Jun 1, 2026
@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.