From d5daedd56c4b3875c1659daeefee1772a9995967 Mon Sep 17 00:00:00 2001 From: altperfect <32578434+altperfect@users.noreply.github.com> Date: Sat, 8 Aug 2026 15:53:16 +0500 Subject: [PATCH] fix: preserve custom config --- strix/config/loader.py | 12 ++++++++---- tests/test_config_loader.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/strix/config/loader.py b/strix/config/loader.py index fbcde898..6bde3b32 100644 --- a/strix/config/loader.py +++ b/strix/config/loader.py @@ -54,22 +54,26 @@ def apply_config_override(path: Path) -> None: def persist_current() -> None: - """Write currently-set env vars to the active config file (0o600).""" + """Write explicitly configured values to the active config file (0o600).""" s = load_settings() target = _override or _DEFAULT_PATH target.parent.mkdir(parents=True, exist_ok=True) - env_block: dict[str, str] = {} + env_block: dict[str, Any] = {} for sub_name in s.model_fields: sub_model = getattr(s, sub_name) if not isinstance(sub_model, BaseModel): continue - for finfo in type(sub_model).model_fields.values(): - for alias in _aliases_for(finfo): + for field_name, finfo in type(sub_model).model_fields.items(): + aliases = _aliases_for(finfo) + for alias in aliases: value = os.environ.get(alias.upper()) if value: env_block[alias.upper()] = value break + else: + if aliases and field_name in sub_model.model_fields_set: + env_block[aliases[0].upper()] = getattr(sub_model, field_name) write_secret_text(target, json.dumps({"env": env_block}, indent=2)) diff --git a/tests/test_config_loader.py b/tests/test_config_loader.py index e83ab119..40f3c329 100644 --- a/tests/test_config_loader.py +++ b/tests/test_config_loader.py @@ -208,6 +208,36 @@ def test_persist_current_writes_env_block(tmp_path: Path, monkeypatch: pytest.Mo } +def test_persist_current_preserves_file_backed_values( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + target = tmp_path / "cli-config.json" + target.write_text( + json.dumps( + { + "env": { + "STRIX_LLM": "file-model", + "LLM_API_KEY": "file-key", + "LLM_API_BASE": "https://example.invalid/v1", + } + } + ), + encoding="utf-8", + ) + monkeypatch.setenv("OPENAI_BASE_URL", "https://example.invalid/v1") + loader.apply_config_override(target) + + loader.persist_current() + + assert json.loads(target.read_text(encoding="utf-8")) == { + "env": { + "STRIX_LLM": "file-model", + "LLM_API_KEY": "file-key", + "OPENAI_BASE_URL": "https://example.invalid/v1", + } + } + + def test_persist_current_sets_0600_mode(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("STRIX_LLM", "persisted-model") target = tmp_path / "cli-config.json"