#17021: fix(agents): read models from gateway config first
agents
stale
size: S
Cluster:
Wizard Enhancements and Config Fixes
Fixes #16976
## Problem
Per-agent models.json is a static snapshot written at agent creation time. When gateway config adds new models (e.g., new Ollama models), agents fall back to default model instead of using the newly configured models.
## Root Cause
resolveModel() checked models.json FIRST before checking the gateway config. The gateway config is always fresh, but the per-agent models.json is stale.
## Solution
Check gateway config (cfg.models.providers) FIRST before falling back to models.json. This ensures agents always use the latest models from the gateway config.
## Impact
- Agents automatically use newly added models without manual models.json updates
- No need to restart gateway or recreate agents after config changes
- Models.json becomes a secondary fallback (for backward compatibility)
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Refactors `resolveModel()` to check gateway config (`cfg.models.providers`) before falling back to per-agent `models.json`, ensuring agents always use fresh models from gateway config instead of stale snapshots.
**Major changes:**
- Gateway config now checked first (lines 77-90), so newly added models are immediately available
- `models.json` becomes secondary fallback (lines 93-96)
- Forward-compat and generic provider fallbacks remain in correct order
**Critical issue:**
- Line 95 drops the `normalizeModelCompat()` wrapper from the models.json path (previously line 118 in old code). This breaks ZAI models resolved from models.json—they won't have `supportsDeveloperRole: false` set and will attempt to use the developer role, causing API failures.
<h3>Confidence Score: 2/5</h3>
- This PR contains a critical logical error that will break ZAI models
- The refactor correctly inverts the priority (gateway config first, models.json second), but introduces a regression by dropping `normalizeModelCompat()` on the models.json path. This will cause ZAI `openai-completions` models to fail at runtime when resolved from models.json.
- src/agents/pi-embedded-runner/model.ts line 95 requires immediate attention
<sub>Last reviewed commit: 6ab83b6</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#16988: fix: always regenerate models.json from gateway config
by MisterGuy420 · 2026-02-15
81.6%
#13376: fix: pass model directly to agent for sub-agent runs
by jrbobbyhansen-pixel · 2026-02-10
81.2%
#9064: fix: validate model references against catalog in config.set/patch/...
by joetomasone · 2026-02-04
79.9%
#10608: fix(agents): honor spawn model override in gateway and session spaw...
by 1kuna · 2026-02-06
79.7%
#21882: feat(gateway): filter models.list by agents.defaults.models allowlist
by kckylechen1 · 2026-02-20
79.0%
#11349: fix(agents): do not filter fallback models by models allowlist
by liuxiaopai-ai · 2026-02-07
78.5%
#20712: fix(subagents): prioritize agent runtime default model over global ...
by sourcesavant · 2026-02-19
78.2%
#20954: feat: per-agent model allowlist
by coletebou · 2026-02-19
78.1%
#9583: fix(models): allow models in agents.defaults.models even if not in ...
by hotzen100 · 2026-02-05
77.8%
#11562: Fix #10883: Enforce subagent model configuration
by divol89 · 2026-02-08
77.7%