#15130: fix: guard against undefined modelId to prevent .trim() crash in subagent spawn (#15125)
agents
size: XS
Cluster:
Model Management Enhancements
## Problem
All subagent spawns crash with:
```
TypeError: Cannot read properties of undefined (reading 'trim')
```
100% reproducible. The crash occurs in `resolveModel()` and `normalizeCliModel()` when `modelId` is `undefined`.
## Root Cause
Session model overrides or misconfigured model references can pass `undefined` as `modelId` to functions that call `.trim()` without checking.
## Fix
Add defensive guards with `console.warn()` in both `resolveModel()` and `normalizeCliModel()` so:
1. The crash is prevented (returns error/empty string instead)
2. A warning is logged with the invalid value, making it easy to trace the upstream caller
## Changes (v2)
- **Removed** unrelated compaction reset changes (now covered by the `totalTokensFresh` approach in #15133)
- **Added** `console.warn()` to both guards so the root cause caller can be traced in logs
- **Added** DCO sign-off
- Rebased on latest `main`
Fixes #15125
Most Similar PRs
#23568: fix(agents): preserve multi-segment model IDs in splitModelRef
by arosstale · 2026-02-22
67.7%
#21088: fix: sessions_sspawn model override ignored for sub-agents
by Slats24 · 2026-02-19
65.5%
#13376: fix: pass model directly to agent for sub-agent runs
by jrbobbyhansen-pixel · 2026-02-10
64.6%
#11562: Fix #10883: Enforce subagent model configuration
by divol89 · 2026-02-08
64.3%
#17021: fix(agents): read models from gateway config first
by Limitless2023 · 2026-02-15
64.0%
#7569: fix: add null checks to prevent TypeError in subagent spawn
by kaigritun · 2026-02-03
63.8%
#21556: fix(agents): graceful fallback when spawned model is not in allowlist
by irchelper · 2026-02-20
63.7%
#8471: fix(subagent): add defensive checks for undefined string fields
by adam-smeth · 2026-02-04
63.6%
#13857: fix: warn when local agent models.json silently overrides central c...
by itsGustav · 2026-02-11
63.1%
#20712: fix(subagents): prioritize agent runtime default model over global ...
by sourcesavant · 2026-02-19
63.0%