From 56cd8add18b94804bfd8d5accc35d9ab67d1a67a Mon Sep 17 00:00:00 2001 From: red person Date: Wed, 3 Jun 2026 08:12:31 +0300 Subject: [PATCH] Fall back from invalid preset stores (#1402) --- src/preset_manager.py | 3 +++ tests/test_preset_store_shape.py | 12 ++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 tests/test_preset_store_shape.py diff --git a/src/preset_manager.py b/src/preset_manager.py index 34233f1..6364b8a 100644 --- a/src/preset_manager.py +++ b/src/preset_manager.py @@ -77,6 +77,9 @@ Use precise language. Show causal relationships explicitly. Quantify uncertainty try: with open(self.presets_file, 'r', encoding="utf-8") as f: presets = json.load(f) + if not isinstance(presets, dict): + logger.error("Error loading presets: expected an object") + return self.DEFAULT_PRESETS.copy() custom = presets.get("custom") if isinstance(presets, dict) else None if isinstance(custom, dict) and "enabled" not in custom: legacy_prompt = "You are a helpful, balanced assistant. Match your response style to the user's needs." diff --git a/tests/test_preset_store_shape.py b/tests/test_preset_store_shape.py new file mode 100644 index 0000000..9d52d91 --- /dev/null +++ b/tests/test_preset_store_shape.py @@ -0,0 +1,12 @@ +import json + +from src.preset_manager import PresetManager + + +def test_non_object_preset_store_falls_back_to_defaults(tmp_path): + (tmp_path / "presets.json").write_text(json.dumps([])) + + manager = PresetManager(str(tmp_path)) + + assert manager.presets == PresetManager.DEFAULT_PRESETS + assert manager.get("custom")["enabled"] is False