Skip to content

Route unavailable models to their mapped fallback - #2

Open
KPhi wants to merge 3 commits into
renjfk:mainfrom
KPhi:feat/availability-routing
Open

KPhi wants to merge 3 commits into
renjfk:mainfrom
KPhi:feat/availability-routing

Conversation

@KPhi

@KPhi KPhi commented Nov 11, 2026 •

Copy link
Copy Markdown

The plugin currently only catches temporary failures for models on providers that can normally be employed by the user. This PR adds routing for unavailable models - models on providers that are disabled by policy or have never been configured with credentials in the first place.

Motivation

The problem this feature solves is multi-user collaboration: the OpenCode configuration and agent definitions can be checked into a workspace with default models to be used by any developer working on it. For developers on the team that don't have access to a given model (e.g. because only lead developers get access to Claude and juniors have to work with the shared Kimi account), the plugin ensures requests are always routed to a well-defined fallback model instead of OpenCode's arbitrary default substitution.

The gap

OpenCode resolves such models at resolution time, before any provider request exists:

  • an explicitly selected unavailable model fails the turn with a hard Model unavailable error, and
  • an unavailable agent-pinned model is silently substituted with OpenCode's default model.

Both happen before a request is built, so the retry hook cannot see them. The prompt hook is the only cross-provider routing point that runs early enough - the context hook's model field is readonly, and experimental.model.resolve is same-provider only.

What this adds

  • route_unavailable (on by default): the prompt hook consults OpenCode's enabled model inventory before the request is built. A mapped original that is not in the inventory is routed to its fallback without waiting for a failure. Set route_unavailable: false to keep failure-driven (cooldown-only) routing.
  • Agent pins are covered: sessions without an explicit selection (e.g. subagents) run on their agent's pinned model; an unavailable mapped original pin is routed away, while a deliberately pinned fallback is respected.
  • Recovery: once the original is available again, the next prompt switches the session back. The session's own model ref carries the state - no per-session plugin state is involved.
  • Safety: the inventory snapshot is cached for 30s and invalidated on provider.updated/model.updated; an inventory that cannot be read never routes, preserving the previous behavior.

Reasoning effort is preserved across switches (second commit)

Previously the effort was dropped entirely on fallback: the fallback model ran at its default even when it supported the same level. Now the session's current variant is kept when the target lists it in its catalog, and falls back to the target's default otherwise. A mapping value may pin the effort explicitly ("azure/gpt-5.5#high"), which wins over matching; mapping keys are normalized variant-less. When the fallback drops the effort (e.g. a non-reasoning model), recovery restores it without plugin-side state - from the session agent's pinned model when it points at the original, else
from the original's most recent recorded response - so it survives plugin restarts.

Tests

Unit tests cover routing, recovery, agent pins, and effort matching. The e2e suite gains two scenarios against a real opencode serve: an unavailable model routed to its fallback before any request is built, and a routed fallback keeping the session's reasoning effort.

KPhi added 2 commits November 11, 2026 10:45
OpenCode resolves an explicitly selected unavailable model to a hard 'Model unavailable' error and silently substitutes its default model for an unavailable agent pin - both at resolution time, before any provider request exists, so the retry hooks cannot see them.

route_unavailable (on by default) extends the prompt hook to consult OpenCode's enabled model inventory before the request is built: a mapped original that is not in the inventory is routed to its fallback without waiting for a failure, and sessions return to the original once it is available again. Sessions without an explicit selection (e.g. subagents running on their agent's pinned model) are covered too: an unavailable mapped original pin is routed away, while a deliberately pinned fallback is respected. The inventory snapshot is cached for 30 seconds and invalidated by provider.updated/model.updated events; an inventory that cannot be read never routes.
Switches dropped the session's reasoning effort entirely: the fallback model ran at its default even when it supported the same effort level. switchSessionModel now keeps the session's current variant when the target lists it in its catalog, and falls back to the target's default otherwise (e.g. a non-reasoning fallback or one without the specific level). A mapping value may pin the effort explicitly ('azure/gpt-5.5#high'), which always wins over matching; mapping keys are normalized to be variant-less. When the fallback drops the effort, recovery restores it without plugin-side state: from the session agent's pinned model when it points at the original, else from the original's most recent recorded response (assistant messages persist the model ref with variant). Both sources survive plugin restarts. An untrustworthy inventory never reports support, keeping the previous default-effort behavior.
@KPhi KPhi changed the title Routing for unavailable models Route unavailable models to their mapped fallback Nov 11, 2026
Model.Info carries both the catalog id (the selectable reference used by sessions, agent pins and mappings) and modelID (the ID sent to the provider). A model renamed through a modelID config attribute - e.g. a provider config key 'mistral-medium-latest' with modelID 'magistral-medium-latest' - made the inventory lookup miss it, so availability routing treated the model as unavailable and silently rerouted sessions to the fallback without any failed request. The inventory now keys by the catalog id, falling back to modelID for entries without one.
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.

1 participant