From f371072d3f927e8f554d557d95b587cd63c9af49 Mon Sep 17 00:00:00 2001 From: Evan Luo Date: Thu, 19 Mar 2026 15:51:29 +0000 Subject: [PATCH] fix(config): keep missing env refs unresolved --- nanobot/config/secret_resolver.py | 7 +++++-- tests/test_config_migration.py | 5 ++--- tests/test_secret_resolver.py | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/nanobot/config/secret_resolver.py b/nanobot/config/secret_resolver.py index c81a69923..370e7d4a4 100644 --- a/nanobot/config/secret_resolver.py +++ b/nanobot/config/secret_resolver.py @@ -19,12 +19,15 @@ def resolve_env_vars(value: str) -> str: Returns: String with all {env:VAR} references replaced by their values. - Missing env vars resolve to empty string. + Missing env vars are left unchanged. """ def replacer(match: re.Match[str]) -> str: var_name = match.group(1) - return os.environ.get(var_name, "") + env_value = os.environ.get(var_name) + if env_value is None: + return match.group(0) + return env_value return _REF_PATTERN.sub(replacer, value) diff --git a/tests/test_config_migration.py b/tests/test_config_migration.py index 42b19806d..6d7b10962 100644 --- a/tests/test_config_migration.py +++ b/tests/test_config_migration.py @@ -216,7 +216,7 @@ def test_save_keeps_intentional_in_memory_override_of_env_ref(tmp_path, monkeypa assert saved["providers"]["openai"]["apiKey"] == "sk-manual-override" -def test_missing_env_ref_resolves_empty_at_runtime_but_persists_placeholder(tmp_path) -> None: +def test_missing_env_ref_stays_unresolved_and_persists_placeholder(tmp_path) -> None: config_path = tmp_path / "config.json" config_path.write_text( json.dumps( @@ -232,8 +232,7 @@ def test_missing_env_ref_resolves_empty_at_runtime_but_persists_placeholder(tmp_ ) config = load_config(config_path) - assert config.providers.openai.api_key == "" - assert config.get_provider_name("openai/gpt-4.1") is None + assert config.providers.openai.api_key == "{env:MISSING_OPENAI_KEY}" save_config(config, config_path) saved = json.loads(config_path.read_text(encoding="utf-8")) diff --git a/tests/test_secret_resolver.py b/tests/test_secret_resolver.py index bc079ed18..0cb01c31f 100644 --- a/tests/test_secret_resolver.py +++ b/tests/test_secret_resolver.py @@ -21,8 +21,8 @@ class TestResolveEnvVars: monkeypatch.setenv("HOST", "example.com") assert resolve_env_vars("{env:USER}@{env:HOST}") == "alice@example.com" - def test_unresolved_var_becomes_empty(self) -> None: - assert resolve_env_vars("{env:NONEXISTENT_VAR_XYZ}") == "" + def test_unresolved_var_kept_unchanged(self) -> None: + assert resolve_env_vars("{env:NONEXISTENT_VAR_XYZ}") == "{env:NONEXISTENT_VAR_XYZ}" def test_empty_env_var(self, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("EMPTY_VAR", "")