Skip to content

Add env-var fallback for provider API keys ([E4] / #37) - #60

Merged
ammar-s847 merged 1 commit into
mainfrom
claude/hebb-issues-prs-FQagq-e4-env-fallback
May 28, 2026
Merged

Add env-var fallback for provider API keys ([E4] / #37)#60
ammar-s847 merged 1 commit into
mainfrom
claude/hebb-issues-prs-FQagq-e4-env-fallback

Conversation

@ammar-s847

Copy link
Copy Markdown
Contributor

Closes #37.

Summary

core::agent::keys::provider_key_from_env(provider_id) resolves the API key for gemini / anthropic from 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 to GOOGLE_API_KEY (matching the existing live-smoke test in provider::gemini).
  • "anthropic"ANTHROPIC_API_KEY.
  • Empty values are treated as unset — 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.

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

  • Existing direct env reads in core/src/chat/anthropic.rs and core/src/chat/mod.rs are 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

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.
@ammar-s847
ammar-s847 merged commit 6457cb0 into main May 28, 2026
2 checks passed
@ammar-s847
ammar-s847 deleted the claude/hebb-issues-prs-FQagq-e4-env-fallback branch May 28, 2026 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[E4] Env-var fallback for headless/CLI

2 participants