Skip to content

fix(cli): preserve runtime-added models when saving settings#2455

Merged
tanzhenxin merged 2 commits intoQwenLM:mainfrom
Sakuranda:fix/2454-model-settings-preservation
Apr 5, 2026
Merged

fix(cli): preserve runtime-added models when saving settings#2455
tanzhenxin merged 2 commits intoQwenLM:mainfrom
Sakuranda:fix/2454-model-settings-preservation

Conversation

@Sakuranda
Copy link
Copy Markdown
Contributor

TLDR

This PR fixes #2454 by changing settings persistence to write only the updated setting patch instead of rewriting the entire in-memory settings snapshot. This preserves models manually added to settings.json while Qwen Code is already running and a later /model change updates model.name.

Dive Deeper

Before this change, LoadedSettings.setValue() updated the in-memory settings object and then called saveSettings() with the full originalSettings payload that was loaded at startup. If the user manually edited ~/.qwen/settings.json while the CLI was still running, any newly added modelProviders entries only existed on disk, not in memory. A later /model switch rewrote the file from the stale snapshot and silently dropped those externally added models.

This change narrows persistence to the key being updated:

  • LoadedSettings.setValue() now builds a nested patch for the changed path, such as model.name
  • saveSettings() applies that patch with updateSettingsFilePreservingFormat()
  • the on-disk file remains the merge source, so unrelated settings added after startup are preserved

I also added a regression test that simulates:

  1. loading settings
  2. externally adding a new model to settings.json
  3. calling settings.setValue(SettingScope.User, 'model.name', ...)
  4. asserting the manually added model entry is still present after the write

Reviewer Test Plan

  1. While Qwen Code is still running, manually edit ~/.qwen/settings.json and append a second model entry under the same provider.
  2. In the running session, use /model to switch to the newly added model.
  3. Confirm ~/.qwen/settings.json still contains both model entries and only model.name changed.

Automated verification run for this patch:

  • npx vitest run packages/cli/src/config/settings.test.ts
  • npx vitest run packages/cli/src/utils/commentJson.test.ts
  • npx eslint packages/cli/src/config/settings.ts packages/cli/src/config/settings.test.ts
  • npm run build --workspace=@qwen-code/qwen-code

Testing Matrix

🍏 🪟 🐧
npm run
npx
Docker
Podman - - -
Seatbelt - -

Linked issues / bugs

Fixes #2454

Copy link
Copy Markdown
Collaborator

@LaZzyMan LaZzyMan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

This PR correctly fixes #2454 by changing the settings persistence strategy from rewriting the entire snapshot to applying incremental patches.

Strengths

  • Correct fix: The patch-based approach preserves externally added models
  • Good test coverage: Regression test validates the fix
  • Minimal changes: Only touches necessary code paths
  • Clear documentation: PR description explains the problem and solution well

Code Quality

  • Clean implementation of helper
  • Proper type safety maintained
  • No security concerns

LGTM - Approved.

Copy link
Copy Markdown
Collaborator

@LaZzyMan LaZzyMan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

This PR correctly fixes #2454 by changing the settings persistence strategy from rewriting the entire snapshot to applying incremental patches.

Strengths

  • Correct fix: The patch-based approach preserves externally added models
  • Good test coverage: Regression test validates the fix
  • Minimal changes: Only touches necessary code paths
  • Clear documentation: PR description explains the problem and solution well

Code Quality

  • Clean implementation of createSettingsUpdate() helper
  • Proper type safety maintained
  • No security concerns

LGTM - Approved.

@tanzhenxin tanzhenxin merged commit 4c594e2 into QwenLM:main Apr 5, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: /model command silently removes manually-added models from settings.json

3 participants