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

Closed
opened 2026-02-16 17:54:50 -05:00 by yindo · 0 comments
Owner

Originally created by @leeweisern on GitHub (Jan 14, 2026).

Originally assigned to: @rekram1-node on GitHub.

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
Originally created by @leeweisern on GitHub (Jan 14, 2026). Originally assigned to: @rekram1-node on GitHub. ## 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
yindo closed this issue 2026-02-16 17:54:50 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#5617