feat: auto-detect provider from API key env vars (#53 rebased + test fix) - #95
Merged
Conversation
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).
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>
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.
Rebased version of @allen-munsch's #53 with the test-flakiness fix.
Original PR
If no provider is set via CLI flag,
DIRGE_PROVIDERenv var, or config file, dirge now checks for known API key environment variables and picks the matching provider automatically.Resolution order:
--providerCLI flag /DIRGE_PROVIDERenv varproviderin config file*_API_KEYenv vars (new!)"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 undercargo 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 passesstd::env::var; tests pass a HashMap-backed closure viamock_env(&[...]). No process env mutation, no race.Also extracted the candidate list to a module-level
PROVIDER_AUTODETECT_ORDERso tests reference the same source of truth, plus addedauto_detect_each_provider_in_isolationthat 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