Repository navigation
Conversation
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.
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.
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.
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:
Model unavailableerror, andBoth happen before a request is built, so the
retryhook cannot see them. Theprompthook is the only cross-provider routing point that runs early enough - thecontexthook'smodelfield is readonly, andexperimental.model.resolveis same-provider only.What this adds
route_unavailable(on by default): theprompthook 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. Setroute_unavailable: falseto keep failure-driven (cooldown-only) routing.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, elsefrom 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.