Skip to content

Favorite models get wiped on startup due to race condition #8375

@leeweisern

Description

@leeweisern

Description

Favorite models saved via the TUI are lost after updates or restarts.

Steps to Reproduce

  1. Open the model selector and add models to favorites (ctrl+f)
  2. Close opencode
  3. Reopen opencode (or update to a new version)
  4. Favorites are gone

Root Cause

There is a race condition in packages/opencode/src/cli/cmd/tui/context/local.tsx:

  1. modelStore initializes with favorite: [] (empty array)
  2. model.json is read asynchronously via file.json().then(...)
  3. Before the file read completes, actions like --model flag or session variant changes call save()
  4. save() writes the empty in-memory state to disk, wiping persisted favorites

Key trigger points:

  • app.tsx:241-254 - --model flag triggers local.model.set(..., { recent: true }) in onMount
  • prompt/index.tsx:132-151 - Session changes trigger local.model.variant.set() early

Expected Behavior

Favorites should persist across restarts and updates.

Proposed Fix

  1. Defer model.json writes until state is ready (pendingSave flag)
  2. Merge persisted data with in-memory state on load (instead of overwriting)
  3. Flush any queued save after file load completes

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