Repository navigation
fix(cli,auth): resolve base_url_env_var from ~/.hermes/.env #20656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
haosenwang1018
wants to merge
1
commit into
NousResearch:main
Choose a base branch
from
haosenwang1018:fix/18757-base-url-dotenv-resolution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| """Regression tests for #18757. | ||
|
|
||
| Provider base URLs declared via ``base_url_env_var`` must resolve from | ||
| ``~/.hermes/.env`` (via :func:`hermes_cli.config.get_env_value`) — not only | ||
| from the process environment (``os.getenv``). API keys already used the | ||
| ``.env``-aware lookup; base URLs were previously inconsistent. | ||
| """ | ||
|
|
||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from hermes_cli.auth import ( | ||
| PROVIDER_REGISTRY, | ||
| get_api_key_provider_status, | ||
| resolve_api_key_provider_credentials, | ||
| ) | ||
|
|
||
|
|
||
| # Use Arcee as a representative API-key provider with a base_url_env_var. | ||
| # The fix is provider-agnostic — we just need any provider whose registry | ||
| # entry has base_url_env_var set. | ||
| _PROVIDER = "arcee" | ||
| _BASE_URL_ENV = "ARCEE_BASE_URL" | ||
| _API_KEY_ENV = "ARCEEAI_API_KEY" | ||
| _DOTENV_BASE_URL = "https://custom-arcee.example.com/v1" | ||
| _API_KEY_VALUE = "arc-test-from-dotenv" | ||
|
|
||
|
|
||
| def _stub_get_env_value(values): | ||
| """Build a get_env_value replacement that reads from ``values`` only.""" | ||
| def _impl(key, default=""): | ||
| return values.get(key, default) | ||
| return _impl | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def _registry_has_base_url_env_var(): | ||
| """Sanity-guard: the test only makes sense if the provider declares a | ||
| base_url_env_var. If the registry changes, fail loudly.""" | ||
| assert PROVIDER_REGISTRY[_PROVIDER].base_url_env_var == _BASE_URL_ENV | ||
|
|
||
|
|
||
| class TestResolveApiKeyProviderCredentialsReadsDotenvBaseUrl: | ||
| """``resolve_api_key_provider_credentials()`` must consult ``.env`` for | ||
| the base URL — not only the shell environment.""" | ||
|
|
||
| def test_dotenv_base_url_used_when_shell_env_unset(self, monkeypatch): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This mocks the accessor being validated, so it cannot verify real |
||
| # Clear the shell env so ``os.getenv`` returns nothing for the base URL. | ||
| monkeypatch.delenv(_BASE_URL_ENV, raising=False) | ||
|
|
||
| env_values = { | ||
| _BASE_URL_ENV: _DOTENV_BASE_URL, | ||
| _API_KEY_ENV: _API_KEY_VALUE, | ||
| } | ||
| with patch( | ||
| "hermes_cli.config.get_env_value", | ||
| side_effect=_stub_get_env_value(env_values), | ||
| ): | ||
| result = resolve_api_key_provider_credentials(_PROVIDER) | ||
|
|
||
| # Pre-fix this would be the registry default ("https://api.arcee.ai/api/v1") | ||
| # because os.getenv could not see the .env value. | ||
| assert result["base_url"] == _DOTENV_BASE_URL.rstrip("/") | ||
|
|
||
| def test_shell_env_still_works_when_dotenv_empty(self, monkeypatch): | ||
| # Confirm we did not regress the shell-env path. | ||
| monkeypatch.setenv(_BASE_URL_ENV, _DOTENV_BASE_URL) | ||
|
|
||
| env_values = {_API_KEY_ENV: _API_KEY_VALUE} # no BASE_URL in .env | ||
| with patch( | ||
| "hermes_cli.config.get_env_value", | ||
| side_effect=_stub_get_env_value(env_values), | ||
| ): | ||
| result = resolve_api_key_provider_credentials(_PROVIDER) | ||
|
|
||
| assert result["base_url"] == _DOTENV_BASE_URL.rstrip("/") | ||
|
|
||
|
|
||
| class TestApiKeyProviderStatusReadsDotenvBaseUrl: | ||
| """``get_api_key_provider_status()`` (used by ``hermes doctor``) must also | ||
| surface the .env base URL — otherwise users get inconsistent reporting: | ||
| runtime works but ``doctor`` shows the wrong endpoint.""" | ||
|
|
||
| def test_doctor_status_reflects_dotenv_base_url(self, monkeypatch): | ||
| monkeypatch.delenv(_BASE_URL_ENV, raising=False) | ||
|
|
||
| env_values = { | ||
| _BASE_URL_ENV: _DOTENV_BASE_URL, | ||
| _API_KEY_ENV: _API_KEY_VALUE, | ||
| } | ||
| with patch( | ||
| "hermes_cli.config.get_env_value", | ||
| side_effect=_stub_get_env_value(env_values), | ||
| ): | ||
| status = get_api_key_provider_status(_PROVIDER) | ||
|
|
||
| # The status should reflect the .env base URL — pre-fix it returned | ||
| # the registry default. | ||
| assert status.get("base_url") == _DOTENV_BASE_URL or status.get("base_url", "").rstrip("/") == _DOTENV_BASE_URL.rstrip("/") | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current main routes this read through
_getenv()so multiplexed gateway turns useagent.secret_scope.get_secret()rather than raw process/environment access. Please rework this hunk against the current resolver instead of importingget_env_value()directly.