fix: preserve externally-added settings when saving

When a user edits settings.json while pi is running (e.g., adding
enabledModels), those settings would be lost when pi saved other
changes (e.g., changing thinking level via Shift+Tab).

The fix re-reads the file before saving and merges the current file
contents with in-memory changes, so external edits are preserved.

Adds test coverage for SettingsManager.
This commit is contained in:
ferologics 2026-01-07 11:31:14 +01:00
parent 1f2dbb57f6
commit e0dbdc56d6
2 changed files with 115 additions and 1 deletions

View file

@ -178,7 +178,13 @@ export class SettingsManager {
mkdirSync(dir, { recursive: true });
}
// Save only global settings (project settings are read-only)
// Re-read current file to preserve any settings added externally while running
const currentFileSettings = SettingsManager.loadFromFile(this.settingsPath);
// Merge: file settings as base, globalSettings (in-memory changes) as overrides
const mergedSettings = deepMergeSettings(currentFileSettings, this.globalSettings);
this.globalSettings = mergedSettings;
// Save merged settings (project settings are read-only)
writeFileSync(this.settingsPath, JSON.stringify(this.globalSettings, null, 2), "utf-8");
} catch (error) {
console.error(`Warning: Could not save settings file: ${error}`);