#9822: fix: allow local/custom model providers for sub-agent inference
agents
stale
Cluster:
Model Management Enhancements
## Summary
Fixes #7211.
When a local model provider (Ollama or any OpenAI-compatible endpoint) is explicitly configured in `models.providers`, session model overrides pointing to those providers were silently rejected because the models did not appear in the piSdk model catalog.
This caused `sessions_spawn` with `model: 'ollama/qwen2.5:7b'` (or any custom provider) to return `modelApplied: true` but actually fall back to the default Anthropic model at inference time.
## Root Cause
Two code paths validated session model overrides against the model catalog without considering explicitly configured providers:
1. **`buildAllowedModelSet()`** (`model-selection.ts`): When `allowAny=true` (no explicit model allowlist), only catalog-discovered models were included in `allowedKeys`. Models from `cfg.models.providers` entries (e.g. Ollama, LM Studio, custom OpenAI-compatible endpoints) were missing.
2. **`createModelSelectionState()`** (`auto-reply/reply/model-selection.ts`): When a stored model override was not in `allowedKeys`, it was silently reset to the default model — even when the override pointed to a model from an explicitly configured provider.
## Fix
- **`buildAllowedModelSet`**: In `allowAny` mode, also add model keys from `cfg.models.providers` so configured provider models are recognized as valid.
- **`createModelSelectionState`**: Before resetting a session override, check if the override's provider is explicitly configured. If so, keep the override instead of resetting to default.
Both changes are backwards-compatible: behavior is unchanged for hosted providers and for users with explicit model allowlists (the allowlist path already handled configured providers correctly).
## Test
Added `model-selection.build-allowed-model-set-includes-configured-provider-models.test.ts` covering:
- Ollama provider models included in `allowAny` mode
- Custom-named providers (e.g. `local`) included in `allowAny` mode
- Graceful handling of providers without `models` array
## Reproduction Config (from #7211)
```json5
{
models: {
mode: "merge",
providers: {
local: {
baseUrl: "http://127.0.0.1:11434/v1",
apiKey: "ollama-local",
api: "openai-responses",
models: [{ id: "qwen2.5:7b", name: "Qwen 2.5 7B", ... }]
}
}
}
}
```
Before this fix: `sessions_spawn(model: 'local/qwen2.5:7b')` → `modelApplied: true` but inference used `claude-opus-4-5`.
After this fix: inference correctly uses the local model via the configured endpoint.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR fixes session model overrides for local/custom providers by expanding the allowed-model key set and by preventing auto-reset of overrides when the override’s provider is explicitly configured in `cfg.models.providers`.
Concretely:
- `src/agents/model-selection.ts` extends `buildAllowedModelSet()` so that in `allowAny` mode it also includes model IDs declared under `models.providers.*.models[]`, not just catalog-discovered entries.
- `src/auto-reply/reply/model-selection.ts` updates `createModelSelectionState()` to preserve/accept stored session overrides when the override’s provider is configured, even if the model key is missing from the curated catalog.
- Adds a Vitest file to cover configured-provider models being present in `allowAny` mode and the “providers without models array” edge case.
<h3>Confidence Score: 3/5</h3>
- This PR is likely safe to merge after addressing a couple of correctness issues in provider-configuration detection and test imports.
- Core logic change is small and covered by tests, but the configured-provider detection in `createModelSelectionState` appears to assume provider config keys are already normalized, which isn’t guaranteed, and the new test’s import path may not resolve depending on the repo’s module resolution settings.
- src/auto-reply/reply/model-selection.ts, src/agents/model-selection.build-allowed-model-set-includes-configured-provider-models.test.ts
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#6673: fix: preserve allowAny flag in createModelSelectionState for custom...
by tenor0 · 2026-02-01
89.8%
#9583: fix(models): allow models in agents.defaults.models even if not in ...
by hotzen100 · 2026-02-05
88.1%
#7570: fix: allow models from providers with auth profiles configured
by DonSqualo · 2026-02-03
85.0%
#21088: fix: sessions_sspawn model override ignored for sub-agents
by Slats24 · 2026-02-19
83.8%
#14508: fix(models): allow forward-compat models in allowlist check
by jonisjongithub · 2026-02-12
83.4%
#2353: fix: ensure api field is set for inline provider models
by sbknana · 2026-01-26
83.2%
#15632: fix: use provider-qualified key in MODEL_CACHE for context window l...
by linwebs · 2026-02-13
83.2%
#6603: fix: use allowAny flag instead of size check for model override val...
by gavinbmoore · 2026-02-01
82.6%
#9212: fix: ensure model.input is always an array for custom providers
by sparck75 · 2026-02-05
82.1%
#13626: fix(model): propagate provider model properties in fallback resolution
by mcaxtr · 2026-02-10
81.8%