#13376: fix: pass model directly to agent for sub-agent runs
app: macos
app: web-ui
gateway
scripts
commands
agents
stale
Cluster:
Model Management Enhancements
## Problem
The `sessions_spawn` tool was setting the model via `sessions.patch` but the model override wasn't reliably persisting to the session store before the agent run started. This caused sub-agents to fall back to the default model instead of using the configured subagent model.
**Evidence from investigation:**
- `sessions_spawn` returns `modelApplied: true`
- But the session store shows `modelOverride: null`
- Sub-agents run on default model (Opus) instead of configured model
## Solution
Pass the resolved model directly through the agent call chain, bypassing the session store timing issue:
1. **sessions-spawn-tool.ts**: Add `model` param to gateway agent call
2. **schema/agent.ts**: Add `model` to `AgentParamsSchema`
3. **server-methods/agent.ts**: Accept and forward `model` to `agentCommand`
4. **commands/agent.ts**: Use `opts.model` override (takes precedence over session store)
5. **commands/agent/types.ts**: Add `model` to `AgentCommandOpts`
## Changes
- 5 files changed, 28 insertions
- No breaking changes - the model is optional and existing behavior is preserved
Fixes #13372
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR threads an optional `model` override through the gateway `agent` request path so sub-agent runs can use the intended model without relying on the session store write timing (`sessions.patch`). Concretely: `sessions_spawn` now passes `model` in the `agent` call, the gateway protocol schema accepts it, the gateway handler forwards it into `agentCommand`, and `agentCommand` prefers this per-call override over the session-stored override.
The main issue is in `src/commands/agent.ts`: the new parsing logic for `opts.model` uses `split("/", 2)`, which truncates hierarchical model IDs (explicitly used in this repo for OpenRouter, e.g. `openrouter/moonshotai/kimi-k2`). That will select the wrong model key and can break allowlist enforcement or cause unexpected default-model fallback.
<h3>Confidence Score: 3/5</h3>
- This PR is not safe to merge as-is due to incorrect parsing of hierarchical model IDs in the new direct model override path.
- While the overall wiring is small and coherent, the direct model override parsing in agentCommand will definitely truncate OpenRouter-style hierarchical model refs (which the repo already uses/tests for). That would cause incorrect model selection and may break allowlist behavior for sub-agent runs.
- src/commands/agent.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
#10608: fix(agents): honor spawn model override in gateway and session spaw...
by 1kuna · 2026-02-06
90.0%
#20712: fix(subagents): prioritize agent runtime default model over global ...
by sourcesavant · 2026-02-19
87.2%
#11562: Fix #10883: Enforce subagent model configuration
by divol89 · 2026-02-08
86.2%
#21088: fix: sessions_sspawn model override ignored for sub-agents
by Slats24 · 2026-02-19
85.8%
#9583: fix(models): allow models in agents.defaults.models even if not in ...
by hotzen100 · 2026-02-05
85.2%
#6603: fix: use allowAny flag instead of size check for model override val...
by gavinbmoore · 2026-02-01
83.8%
#7292: feat: Implement subagent model inheritance system
by levineam · 2026-02-02
83.5%
#19328: Fix: preserve modelOverride in agent handler (#5369)
by CodeReclaimers · 2026-02-17
82.5%
#21556: fix(agents): graceful fallback when spawned model is not in allowlist
by irchelper · 2026-02-20
82.1%
#7570: fix: allow models from providers with auth profiles configured
by DonSqualo · 2026-02-03
81.6%