#6603: fix: use allowAny flag instead of size check for model override validation (#6573)
commands
Cluster:
Model Management Enhancements
## Summary
Fixes #6573: `sessions_spawn` ignores the `model` parameter, always using the default model.
## Root Cause
In `src/commands/agent.ts`, the condition for accepting a stored model override was:
```ts
allowedModelKeys.size === 0 || allowedModelKeys.has(key)
```
When there's no explicit allowlist, `buildAllowedModelSet` returns `allowAny=true` but **still populates** `allowedKeys` with all catalog models. This means:
- `allowedModelKeys.size === 0` is FALSE (size > 0)
- `allowedModelKeys.has(key)` might be FALSE for a valid model not in the catalog
- Result: valid model overrides get silently ignored
## Fix
Capture the `allowAny` flag from `buildAllowedModelSet` and use it instead of checking `allowedModelKeys.size === 0`:
```ts
// Before:
if (allowedModelKeys.size === 0 || allowedModelKeys.has(key))
// After:
if (allowAnyModel || allowedModelKeys.has(key))
```
## Testing
- Added new test file: `src/commands/agent.model-override-respects-stored-override.test.ts`
- All 26 related tests pass
- TypeScript compiles without errors
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR fixes model override validation in `src/commands/agent.ts` by using the `allowAny` flag from `buildAllowedModelSet()` rather than inferring “no restrictions” from `allowedModelKeys.size === 0`. This ensures stored model overrides are respected when there’s no explicit allowlist, even if the model isn’t present in the static catalog.
A new vitest file was added to cover the `allowAny` behavior and the intended acceptance/rejection rules depending on whether an explicit allowlist is configured.
<h3>Confidence Score: 3/5</h3>
- This PR is likely safe to merge; the functional change is small and targeted, but the added test coverage appears partially miswired.
- The production change in `agent.ts` is straightforward (use `allowAny` instead of `Set.size` heuristics) and aligns with the described root cause. However, the new test file contains issues (wrong module mocked for `runEmbeddedPiAgent`, and the key regression test doesn’t actually exercise `agentCommand`), which reduces confidence in the PR’s ability to prevent regressions.
- src/commands/agent.model-override-respects-stored-override.test.ts
<!-- greptile_other_comments_section -->
<sub>(4/5) You can add custom instructions or style guidelines for the agent [here](https://app.greptile.com/review/github)!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#9583: fix(models): allow models in agents.defaults.models even if not in ...
by hotzen100 · 2026-02-05
87.8%
#6673: fix: preserve allowAny flag in createModelSelectionState for custom...
by tenor0 · 2026-02-01
87.2%
#21088: fix: sessions_sspawn model override ignored for sub-agents
by Slats24 · 2026-02-19
85.1%
#13376: fix: pass model directly to agent for sub-agent runs
by jrbobbyhansen-pixel · 2026-02-10
83.8%
#19328: Fix: preserve modelOverride in agent handler (#5369)
by CodeReclaimers · 2026-02-17
83.2%
#10608: fix(agents): honor spawn model override in gateway and session spaw...
by 1kuna · 2026-02-06
82.8%
#9822: fix: allow local/custom model providers for sub-agent inference
by stammtobias91 · 2026-02-05
82.6%
#20712: fix(subagents): prioritize agent runtime default model over global ...
by sourcesavant · 2026-02-19
82.2%
#21556: fix(agents): graceful fallback when spawned model is not in allowlist
by irchelper · 2026-02-20
81.7%
#11349: fix(agents): do not filter fallback models by models allowlist
by liuxiaopai-ai · 2026-02-07
80.8%