fix(credentials): prefer ~/.hermes/.env over stale os.environ on key rotation#20602
Open
0xDevNinja wants to merge 1 commit into
Open
fix(credentials): prefer ~/.hermes/.env over stale os.environ on key rotation#206020xDevNinja wants to merge 1 commit into
0xDevNinja wants to merge 1 commit into
Conversation
ef6eeaa to
d8a6b4a
Compare
…rotation
A deliberate edit to ~/.hermes/.env must win over a value inherited from
the parent shell (Codex CLI, login profile, test runner). Previously the
auth resolve path called get_env_value, which checks os.environ first,
so rotating a key in .env mid-session left both the live request path
and the credential pool seeded with the stale shell export — producing
persistent 401s after rotation.
Add get_env_value_prefer_dotenv in hermes_cli/config.py and route the
two Hermes-managed credential paths through it:
- agent/credential_pool.py::_seed_from_env (was already inline; now
centralised)
- hermes_cli/auth.py::_resolve_api_key_provider_secret
Existing get_env_value is unchanged — generic env reads keep their
os.environ-first behaviour.
Tests:
- flip test_os_environ_still_wins_over_dotenv to
test_dotenv_wins_over_stale_os_environ (the prior expectation
encoded the bug)
- add test_dotenv_wins_over_stale_os_environ_on_resolve covering the
auth resolve path symmetric with the pool seeding rule
Fixes NousResearch#20591
d8a6b4a to
da0158f
Compare
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?
Fixes a credential-pool / auth-resolve bug where a deliberate key rotation in
~/.hermes/.envis shadowed by a stale shell export inherited from the parent process (Codex CLI, login profile, test runner). The result was persistent 401s after rotation: the user edits.env, butos.environstill wins and the live request path keeps using the dead key.The issue's root-cause analysis pointed at
_seed_from_envinagent/credential_pool.py, but the real divergence was on the auth resolve path —_resolve_api_key_provider_secretwas callingget_env_value, which checksos.environfirst. So even though the pool's inline_get_env_prefer_dotenvhad the right intent, the live request resolution didn't share that policy. Symmetric fix on both sides.Related Issue
Fixes #20591
Type of Change
Changes Made
hermes_cli/config.py: addget_env_value_prefer_dotenv(key)— checks~/.hermes/.envfirst, falls back toos.environ. Existingget_env_valueis unchanged so generic env reads keepos.environ-first semantics.hermes_cli/auth.py::_resolve_api_key_provider_secret: route Hermes-managed credential lookup through the new helper so the resolve path matches the pool seeding policy.agent/credential_pool.py::_seed_from_env: collapse the inline_get_env_prefer_dotenvonto the shared helper (single source of truth).tests/tools/test_credential_pool_env_fallback.py:test_os_environ_still_wins_over_dotenv→test_dotenv_wins_over_stale_os_environ(the prior expectation encoded the bug — it asserted stale shell export wins, which is exactly what users hit on rotation).test_dotenv_wins_over_stale_os_environ_on_resolvecovering the auth resolve path so the pool/auth symmetry is locked in by tests.How to Test
pytest tests/tools/test_credential_pool_env_fallback.py tests/agent/test_credential_pool.py -q→ 51 passed.export OPENROUTER_API_KEY=sk-or-stale-from-shell~/.hermes/.env→OPENROUTER_API_KEY=sk-or-fresh-rotatedhermes— pre-fix the request used the stale shell key (401), post-fix it uses the rotated.envvalue.Checklist
Code
fix(credentials): …)pytest tests/ -qand all tests passDocumentation & Housekeeping
get_env_value)cli-config.yaml.example— N/A (no config keys changed)CONTRIBUTING.md/AGENTS.md— N/Aos.environ+ dotenv parsing