Add env-var fallback for provider API keys ([E4] / #37) - #60
Merged
Conversation
Headless contexts (CLI, MCP server, CI) have no OS keychain, so the
agent harness needs a documented env-var path. `provider_key_from_env`
centralises the mapping:
- `gemini` → `GEMINI_API_KEY`, falling back to `GOOGLE_API_KEY`
(matching the existing live-smoke test in
`provider::gemini`).
- `anthropic` → `ANTHROPIC_API_KEY`.
Empty values are treated as unset so a misconfigured `KEY=` shell
doesn't get forwarded only to come back as `ProviderError::Auth`.
Unknown ids return `None` — no silent cross-provider key reuse.
Once the Tauri keychain plugin lands ([E1] / #34), session bootstrap
should follow the precedence documented at the top of `agent::keys`:
keychain first, then this helper, then unauthenticated. Keeping the
env-var map in one place gives [E2] / #35 a single function to grep
for the "never log this string" audit.
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.
Closes #37.
Summary
core::agent::keys::provider_key_from_env(provider_id)resolves the API key forgemini/anthropicfrom the process environment. Centralises the env-var → provider mapping so the agent harness, the headless CLI/MCP paths, and the upcoming keychain fallback agree on one set of names.Design
"gemini"→GEMINI_API_KEY, falling back toGOOGLE_API_KEY(matching the existing live-smoke test inprovider::gemini)."anthropic"→ANTHROPIC_API_KEY.KEY=shell doesn't get forwarded only to come back asProviderError::Auth.None— no silent cross-provider key reuse.Once the Tauri keychain plugin lands ([E1] / #34), session bootstrap should follow the precedence documented at the top of
agent::keys: keychain first, then this helper, then unauthenticated. Keeping the env-var map in one place gives [E2] / #35 a single function to grep for the "never log this string" audit.Test plan
cargo test -p core keys— 6 passed (unknown provider, both gemini precedence paths, anthropic, empty-value, missing-env). Tests serialize on a module-local mutex so env mutation is safe across cargo's parallel test runner.cargo test -p core— 102 passed, 1 ignored.Notes
core/src/chat/anthropic.rsandcore/src/chat/mod.rsare left alone — those are the legacy chat surface, not the agent harness. Swapping them over is a follow-up if we want one canonical key path across both.Generated by Claude Code