fix(config): dedupe duplicate KEY= lines in save_env_value#20100
Open
0xDevNinja wants to merge 1 commit intoNousResearch:mainfrom
Open
fix(config): dedupe duplicate KEY= lines in save_env_value#201000xDevNinja wants to merge 1 commit intoNousResearch:mainfrom
0xDevNinja wants to merge 1 commit intoNousResearch:mainfrom
Conversation
python-dotenv resolves duplicate keys with last-occurrence-wins, but save_env_value previously updated only the first occurrence and left later duplicates intact. So writing a fresh OPENROUTER_API_KEY at the top of ~/.hermes/.env did not take effect when a stale entry lingered at the bottom — startup re-loaded the stale value and user-visible behaviour was an HTTP 400 against a key the user had just "saved". Replace the first match in-place and drop subsequent occurrences so the written value is the one that loads. Three regression tests cover the collapse-existing-duplicates, position-preserving, and no-op-when-clean paths. Refs NousResearch#8270
Collaborator
1 similar comment
Collaborator
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.
What does this PR do?
Hardens
hermes_cli.config.save_env_valueto remove stale duplicateKEY=lines after replacing the first match. python-dotenv resolves duplicate keys with last-occurrence-wins, so updating only the first match (current behaviour) and leaving older duplicates further down the file means the wrong value still loads at startup.Reproducer:
This surfaced in #8270 — multiple users (urpid, healthymecd, dreamsmaner) reported
HTTP 400 on all OpenRouter modelsdespite a freshly-saved key, traced to a stale entry left at the bottom of.envby an earlier setup attempt or byhermes modelre-adding the same key. The visible top-of-file entry looked correct; the invisible bottom-of-file entry was the one that loaded.Fix: walk the file once, replace the first hit with the new value, drop the rest. Append-when-not-found path is unchanged. No behaviour change for the (overwhelming) clean-file case.
Scope note: #8270 has several distinct root causes in its comment thread (mis-pasted keys, OpenRouter tool-format 400s on certain models, etc.). This PR only addresses the duplicate-key class — the others are separate.
Related Issue
Refs #8270
Type of Change
Changes Made
hermes_cli/config.py: rewrite the find-or-append loop insave_env_value(lines 4232-4259) to replace the first match in place, drop subsequent occurrences of the same key, and only append when no match exists at all. Comment captures the dotenv last-wins rationale.tests/hermes_cli/test_config.py: newTestSaveEnvValueDeduplicationwith three cases — collapse-existing-duplicates (writes load back as the new value), preserve-first-position (other keys keep their order), and no-op-when-clean.How to Test
Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests pass (pre-existing flakes in gateway_service / web_server / bedrock unrelated to this change)Documentation & Housekeeping
docs/, docstrings) — N/A (rationale captured inline)cli-config.yaml.exampleif I added/changed config keys — N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — N/A