fix(config): make env vars win over persisted JSON across all aliases#689
Open
sean-kim05 wants to merge 1 commit into
Open
fix(config): make env vars win over persisted JSON across all aliases#689sean-kim05 wants to merge 1 commit into
sean-kim05 wants to merge 1 commit into
Conversation
_read_json_overrides is documented to let env vars outrank the persisted cli-config.json, but it decided per-alias and broke on the first alias found in either env or the file. When a multi-alias field (e.g. api_key via LLM_API_KEY/OPENAI_API_KEY) was set in the env under one alias but stored in the file under another, the stale file value was surfaced as an init kwarg and overrode the live env var. A lowercase env var was also missed (settings use case_sensitive=False). Decide whether a field is already set in the environment by checking all of its aliases case-insensitively before consulting the file. Add regression tests for the cross-alias and case-insensitive cases. Closes usestrix#688
Contributor
Greptile SummaryThis PR updates config loading so live environment values take precedence over persisted JSON. The main changes are:
Confidence Score: 5/5This looks safe to merge.
Important Files Changed
Reviews (1): Last reviewed commit: "fix(config): make env vars win over pers..." | Re-trigger Greptile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #688
What
_read_json_overridesinstrix/config/loader.pyis documented to let environment variables outrank the persistedcli-config.json(load_settings: "env vars win, then the JSON file"). Since its result is passed toSettings(**init_kwargs)and pydantic-settings ranks the init source above env, it must not emit a field whose env var is already set.The old per-field loop decided per-alias and
breakd on the first alias found in either env or the file, so it missed two cases:1. Multi-alias fields.
api_key=AliasChoices("LLM_API_KEY", "OPENAI_API_KEY"). With envOPENAI_API_KEY=sk-envand file{"env": {"LLM_API_KEY": "sk-file"}}, the loop hit the file alias first and emitted{"llm": {"api_key": "sk-file"}}→load_settings().llm.api_key == "sk-file"instead of the livesk-env.OPENAI_API_KEY/OPENAI_BASE_URLare commonly exported, andpersist_currentcan save the other alias, so a stale key silently wins.2. Case sensitivity. Settings use
case_sensitive=False, butkey in os.environis exact-case, so a lowercase env var (strix_llm) was not seen as set and the file value won.Change
Decide whether a field is already set in the environment by checking all of its aliases case-insensitively (
env_present = {k.upper() for k in os.environ}) before consulting the file:Tests
Added to
tests/test_config_loader.py:env_wins_across_field_aliases— envOPENAI_API_KEY, fileLLM_API_KEY→{}(env wins)env_wins_case_insensitively— lowercasestrix_llmset →{}uses_json_when_no_alias_in_environ— no env alias set → file value still used (guard)The first two fail on the old code and pass with the fix; existing precedence/mapping tests still pass. Full suite: 86 passed;
ruff/mypyclean.Note
Same root cause, left out to keep this focused:
persist_currentusesos.environ.get(alias.upper()), so a lowercase-set env var is silently not persisted. Happy to address separately if desired.