fix: prefer ~/.hermes/.env over os.environ when seeding credential pool#18256
Closed
franksong2702 wants to merge 1 commit into
Closed
Conversation
When _seed_from_env() reads API keys to populate the credential pool, it should treat ~/.hermes/.env as the authoritative source — not os.environ. Stale env vars inherited from parent shell processes (Codex CLI, test scripts, etc.) can shadow deliberate changes to the .env file, causing auth.json to cache an outdated key that leads to silent 401 errors. This is especially visible with OpenRouter: if a parent process exported OPENROUTER_API_KEY=test-key-fresh and the user later updates .env with a valid key, restarting Hermes still picks up the stale os.environ value, writes it back to auth.json, and all API calls fail with 401. Fixes NousResearch#18254
teknium1
added a commit
that referenced
this pull request
May 2, 2026
Follow-up for PR #18256 salvage.
teknium1
added a commit
that referenced
this pull request
May 2, 2026
…cedence Covers PR #18256 fix for issue #18254 — when OPENROUTER_API_KEY is set in BOTH os.environ (stale from parent shell) and ~/.hermes/.env (fresh), _seed_from_env must prefer the .env value. Also guards the fallback case where .env omits the key entirely (Docker/K8s/systemd deployments that only inject via runtime env).
Contributor
|
Merged via #18755 — your commit cherry-picked onto current main with authorship preserved via rebase-merge. Added regression tests for both the bug case (stale shell export shadowing fresh .env) and the fallback case (runtime-injected env in Docker/K8s deployments without a .env file). Thanks for the clean narrow fix! |
nickdlkk
pushed a commit
to nickdlkk/hermes-agent
that referenced
this pull request
May 11, 2026
Follow-up for PR NousResearch#18256 salvage.
nickdlkk
pushed a commit
to nickdlkk/hermes-agent
that referenced
this pull request
May 11, 2026
…cedence Covers PR NousResearch#18256 fix for issue NousResearch#18254 — when OPENROUTER_API_KEY is set in BOTH os.environ (stale from parent shell) and ~/.hermes/.env (fresh), _seed_from_env must prefer the .env value. Also guards the fallback case where .env omits the key entirely (Docker/K8s/systemd deployments that only inject via runtime env).
jsboige
pushed a commit
to jsboige/hermes-agent
that referenced
this pull request
May 14, 2026
Follow-up for PR NousResearch#18256 salvage.
jsboige
pushed a commit
to jsboige/hermes-agent
that referenced
this pull request
May 14, 2026
…cedence Covers PR NousResearch#18256 fix for issue NousResearch#18254 — when OPENROUTER_API_KEY is set in BOTH os.environ (stale from parent shell) and ~/.hermes/.env (fresh), _seed_from_env must prefer the .env value. Also guards the fallback case where .env omits the key entirely (Docker/K8s/systemd deployments that only inject via runtime env).
dannyJ848
pushed a commit
to dannyJ848/hermes-agent
that referenced
this pull request
May 17, 2026
Follow-up for PR NousResearch#18256 salvage.
dannyJ848
pushed a commit
to dannyJ848/hermes-agent
that referenced
this pull request
May 17, 2026
…cedence Covers PR NousResearch#18256 fix for issue NousResearch#18254 — when OPENROUTER_API_KEY is set in BOTH os.environ (stale from parent shell) and ~/.hermes/.env (fresh), _seed_from_env must prefer the .env value. Also guards the fallback case where .env omits the key entirely (Docker/K8s/systemd deployments that only inject via runtime env).
gweeteve
pushed a commit
to gweeteve/hermes-agent
that referenced
this pull request
Jun 2, 2026
Follow-up for PR NousResearch#18256 salvage.
gweeteve
pushed a commit
to gweeteve/hermes-agent
that referenced
this pull request
Jun 2, 2026
…cedence Covers PR NousResearch#18256 fix for issue NousResearch#18254 — when OPENROUTER_API_KEY is set in BOTH os.environ (stale from parent shell) and ~/.hermes/.env (fresh), _seed_from_env must prefer the .env value. Also guards the fallback case where .env omits the key entirely (Docker/K8s/systemd deployments that only inject via runtime env).
Egavasyug
pushed a commit
to Egavasyug/hermes-agent
that referenced
this pull request
Jun 10, 2026
Follow-up for PR NousResearch#18256 salvage.
Egavasyug
pushed a commit
to Egavasyug/hermes-agent
that referenced
this pull request
Jun 10, 2026
…cedence Covers PR NousResearch#18256 fix for issue NousResearch#18254 — when OPENROUTER_API_KEY is set in BOTH os.environ (stale from parent shell) and ~/.hermes/.env (fresh), _seed_from_env must prefer the .env value. Also guards the fallback case where .env omits the key entirely (Docker/K8s/systemd deployments that only inject via runtime env).
Seven74AI
pushed a commit
to Seven74AI/hermes-agent
that referenced
this pull request
Jun 13, 2026
Follow-up for PR NousResearch#18256 salvage.
Seven74AI
pushed a commit
to Seven74AI/hermes-agent
that referenced
this pull request
Jun 13, 2026
…cedence Covers PR NousResearch#18256 fix for issue NousResearch#18254 — when OPENROUTER_API_KEY is set in BOTH os.environ (stale from parent shell) and ~/.hermes/.env (fresh), _seed_from_env must prefer the .env value. Also guards the fallback case where .env omits the key entirely (Docker/K8s/systemd deployments that only inject via runtime env).
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.
Summary
When
_seed_from_env()reads API keys to populate the credential pool inauth.json, it was usingget_env_value()which prefersos.environover~/.hermes/.env. This means stale env vars inherited from parent shell processes (Codex CLI, test scripts, etc.) can shadow deliberate changes to the.envfile.If a parent process exported
OPENROUTER_API_KEY=test-key-freshand the user later updates.envwith a valid key, restarting Hermes still picks up the staleos.environvalue, writes it back toauth.json, and all OpenRouter API calls silently fail with 401.Changes
_get_env_prefer_dotenv()in_seed_from_env()that reads from~/.hermes/.env(viaload_env()) first, then falls back toos.environget_env_value()call sites in_seed_from_env()with the new helperimport osandload_envimportsRoot Cause
get_env_value()inhermes_cli/config.pychecksos.environFIRST, then falls back to the.envfile:This is a reasonable default for most call sites (runtime config), but for
_seed_from_env()— which populates the persistent credential cache — the.envfile should be authoritative. The credential cache already persists the snapshot, so the env var is only relevant during seeding; using a potentially staleos.environvalue here defeats the purpose of the persistent cache.Additional Discovery
During testing, another source of stale keys was identified: Hermes profile
.envfiles at~/.hermes/profiles/<name>/.env. These are created byhermes profile create --clonewhich copies the main.envat creation time. If the main.envhad a test/stale key at that point, the profile retains it even after the main.envis updated. This is a separate concern from theos.environpriority issue, but users should be aware that profile.envfiles may also need manual sync when rotating keys.Test Plan
python3 -c "import py_compile; py_compile.compile('agent/credential_pool.py', doraise=True)".env, export a different value in shell, restart Hermes → should use.envvalueRelated Issues
Closes #18254