diff --git a/tests/tools/test_credential_pool_env_fallback.py b/tests/tools/test_credential_pool_env_fallback.py index 938484f0..b157bb87 100644 --- a/tests/tools/test_credential_pool_env_fallback.py +++ b/tests/tools/test_credential_pool_env_fallback.py @@ -106,10 +106,20 @@ class TestCredentialPoolSeedsFromDotEnv: assert active_sources == set() assert entries == [] - def test_os_environ_still_wins_over_dotenv(self, isolated_hermes_home, monkeypatch): - """get_env_value checks os.environ first — verify seeding picks that up.""" - _write_env_file(isolated_hermes_home, DEEPSEEK_API_KEY="sk-dotenv-stale") - monkeypatch.setenv("DEEPSEEK_API_KEY", "sk-env-fresh-xyz") + def test_dotenv_wins_over_stale_os_environ(self, isolated_hermes_home, monkeypatch): + """.env should win over a stale os.environ value. + + Inverted from the pre-#18254 behaviour. Stale env vars inherited + from parent shells (Codex CLI, test harnesses) used to shadow + deliberate updates to ~/.hermes/.env, causing auth.json to cache + an outdated key and silent 401 errors. The invariant now is: + when a key appears in both sources, .env wins. + + Sister coverage in tests/agent/test_credential_pool.py exercises + the load_pool path; this case exercises _seed_from_env directly. + """ + _write_env_file(isolated_hermes_home, DEEPSEEK_API_KEY="sk-dotenv-fresh") + monkeypatch.setenv("DEEPSEEK_API_KEY", "sk-env-stale-xyz") from agent.credential_pool import _seed_from_env entries = [] @@ -118,7 +128,7 @@ class TestCredentialPoolSeedsFromDotEnv: assert changed is True seeded = [e for e in entries if e.source == "env:DEEPSEEK_API_KEY"] assert len(seeded) == 1 - assert seeded[0].access_token == "sk-env-fresh-xyz" + assert seeded[0].access_token == "sk-dotenv-fresh" class TestAuthResolvesFromDotEnv: