jcode icon indicating copy to clipboard operation
jcode copied to clipboard

Failed OpenRouter switch clears the active named compatible profile

Open apierron opened this issue 4 months ago • 1 comments

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

  1. 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"
  1. Ensure OPENROUTER_API_KEY is unset.
  2. Select my-gateway:vendor/my-model; the active named profile is now my-gateway.
  3. Attempt a structured model-route switch whose runtime is OpenRouter.
  4. The switch correctly returns a missing-OPENROUTER_API_KEY error.
  5. 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.

apierron avatar Jul 21 '26 18:07 apierron

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

1jehuang avatar Jul 21 '26 19:07 1jehuang