Failed OpenRouter switch clears the active named compatible profile
Problem
A failed switch from a named OpenAI-compatible profile to OpenRouter clears the active named profile before the target OpenRouter runtime has been validated.
This is a failure-path regression of #278. That issue established that provider switches should atomically update the runtime, endpoint, authentication, and model. The successful path was fixed, but the current OpenRouter branch clears active_openai_compatible_profile before constructing the OpenRouter runtime or setting its model. If either operation fails, the previous working profile has already been discarded.
Straightforward reproduction
- Configure and activate a named OpenAI-compatible profile with a static model:
[provider]
default_provider = "my-gateway"
default_model = "vendor/my-model"
[providers.my-gateway]
type = "openai-compatible"
base_url = "https://gateway.example/v1"
auth = "none"
default_model = "vendor/my-model"
[[providers.my-gateway.models]]
id = "vendor/my-model"
- Ensure
OPENROUTER_API_KEYis unset. - Select
my-gateway:vendor/my-model; the active named profile is nowmy-gateway. - Attempt a structured model-route switch whose runtime is OpenRouter.
- The switch correctly returns a missing-
OPENROUTER_API_KEYerror. - Inspect the active compatible profile.
Actual: The active named profile is now None, even though the switch failed.
Expected: The error is returned and my-gateway remains active and unchanged.
A focused regression on current master (ccf6153e) fails as follows:
assertion `left == right` failed: a failed switch must preserve the active named profile
left: None
right: Some("my-gateway")
The reproduction uses the existing test_named_config_provider_models_appear_in_picker_and_are_selectable setup, then calls set_route_selection with RuntimeKey::OpenRouter while no OpenRouter key exists.
Likely root cause
In the ActiveProvider::OpenRouter switch path, clear_active_openai_compatible_profile() runs before target runtime construction and set_model(). Those operations are fallible.
Acceptance criteria
- Resolve credentials, construct/reuse the target runtime, and validate/set its model before mutating active provider/profile state.
- A failed OpenRouter switch preserves the previous named profile, endpoint, auth, and model.
- A successful switch installs the real OpenRouter runtime and clears the named profile exactly once.
- Regression tests cover both missing-credential failure and successful transition.
Related: #278 and the broader runtime-identity discussion in #380.
I traced this to provider state being mutated before OpenRouter credentials and model selection were validated. PR #537 stages and validates the target runtime first, so failed switches preserve the active named profile and do not install a partial runtime.
Regression coverage includes missing credentials, an invalid model, and a successful transition. The combined TUI selfdev build also passes.
--- — Jcode agent (automated triage), on behalf of @1jehuang