Skip to content

feat: auto-detect provider from API key env vars (#53 rebased + test fix) - #95

Merged
yogthos merged 2 commits into
mainfrom
feat/auto-detect-provider-from-env
May 21, 2026
Merged

feat: auto-detect provider from API key env vars (#53 rebased + test fix)#95
yogthos merged 2 commits into
mainfrom
feat/auto-detect-provider-from-env

Conversation

@yogthos

@yogthos yogthos commented May 21, 2026

Copy link
Copy Markdown
Collaborator

Rebased version of @allen-munsch's #53 with the test-flakiness fix.

Original PR

If no provider is set via CLI flag, DIRGE_PROVIDER env var, or config file, dirge now checks for known API key environment variables and picks the matching provider automatically.

Resolution order:

  1. --provider CLI flag / DIRGE_PROVIDER env var
  2. provider in config file
  3. Auto-detect from *_API_KEY env vars (new!)
  4. Default to "openrouter"

Supported vars: DEEPSEEK_API_KEY, OPENAI_API_KEY, ANTHROPIC_API_KEY, GEMINI_API_KEY, GLM_API_KEY, OLLAMA_API_KEY, OPENROUTER_API_KEY.

Changes on top of #53

The original tests mutated process-wide env vars via std::env::set_var / remove_var. Rust runs tests in parallel by default, so they raced under cargo test — 2 of 5 failed intermittently on first run locally.

Refactored to a pure helper auto_detect_provider_from(env_lookup) that takes an env-lookup closure. Production passes std::env::var; tests pass a HashMap-backed closure via mock_env(&[...]). No process env mutation, no race.

Also extracted the candidate list to a module-level PROVIDER_AUTODETECT_ORDER so tests reference the same source of truth, plus added auto_detect_each_provider_in_isolation that exercises every entry as a regression guard against accidental drops/reorders.

6 deterministic tests (was 5 flaky); 693 pass total.

Co-authored-by: James Munsch james.a.munsch@gmail.com

allen-munsch and others added 2 commits May 21, 2026 01:31
When a user sets an API key env var (e.g. DEEPSEEK_API_KEY)
but doesn't explicitly configure a provider, dirge now
auto-detects the matching provider instead of falling back
to openrouter.

Resolution order: CLI flag > config file > env var detection > openrouter

Supports: DEEPSEEK_API_KEY, OPENAI_API_KEY, ANTHROPIC_API_KEY,
GEMINI_API_KEY, GLM_API_KEY, OLLAMA_API_KEY, OPENROUTER_API_KEY

Signed-off-by: allen-munsch <james.a.munsch@gmail.com>
The original tests used `std::env::set_var` / `remove_var` which
mutate process-wide state. Rust runs tests in parallel by default,
so the tests raced under `cargo test` and 2 of 5 failed
intermittently.

Refactor: extract `auto_detect_provider_from(env_lookup)` as a
pure helper. Production `auto_detect_provider()` passes
`std::env::var`; tests pass a HashMap-backed closure via
`mock_env(&[...])`. No process env mutation, no race.

The candidate list moves to a module-level
`PROVIDER_AUTODETECT_ORDER` so tests reference the same source
of truth — adds an isolation test
(`auto_detect_each_provider_in_isolation`) that exercises every
entry, guarding against accidental drops or reorders.

6 tests now pass deterministically (was 5 flaky).
@yogthos
yogthos merged commit 233c7a2 into main May 21, 2026
1 check passed
@yogthos
yogthos deleted the feat/auto-detect-provider-from-env branch May 21, 2026 05:33
allen-munsch added a commit to allen-munsch/dirge that referenced this pull request Jun 3, 2026
…ed + test fix) (dirge-code#95)

* feat: auto-detect provider from API key env vars

When a user sets an API key env var (e.g. DEEPSEEK_API_KEY)
but doesn't explicitly configure a provider, dirge now
auto-detects the matching provider instead of falling back
to openrouter.

Resolution order: CLI flag > config file > env var detection > openrouter

Supports: DEEPSEEK_API_KEY, OPENAI_API_KEY, ANTHROPIC_API_KEY,
GEMINI_API_KEY, GLM_API_KEY, OLLAMA_API_KEY, OPENROUTER_API_KEY

Signed-off-by: allen-munsch <james.a.munsch@gmail.com>

* test: replace env-var mutation with pure function + mock env lookup

The original tests used `std::env::set_var` / `remove_var` which
mutate process-wide state. Rust runs tests in parallel by default,
so the tests raced under `cargo test` and 2 of 5 failed
intermittently.

Refactor: extract `auto_detect_provider_from(env_lookup)` as a
pure helper. Production `auto_detect_provider()` passes
`std::env::var`; tests pass a HashMap-backed closure via
`mock_env(&[...])`. No process env mutation, no race.

The candidate list moves to a module-level
`PROVIDER_AUTODETECT_ORDER` so tests reference the same source
of truth — adds an isolation test
(`auto_detect_each_provider_in_isolation`) that exercises every
entry, guarding against accidental drops or reorders.

6 tests now pass deterministically (was 5 flaky).

---------

Signed-off-by: allen-munsch <james.a.munsch@gmail.com>
Co-authored-by: allen-munsch <james.a.munsch@gmail.com>
Co-authored-by: Yogthos <yogthos@gmail.com>
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.

2 participants