#8471: fix(subagent): add defensive checks for undefined string fields
agents
stale
Cluster:
Subagent Task Management Fixes
## Problem
`sessions_spawn` calls fail with:
```
TypeError: Cannot read properties of undefined (reading 'trim')
```
This happens immediately with `runtime 0s`, suggesting the issue is in session key handling before the agent run actually starts.
## Root Cause
Several functions call `.trim()` on string parameters without nullish checks:
- `resolveRequesterStoreKey()` - `requesterSessionKey.trim()`
- `listSubagentRunsForRequester()` - `requesterSessionKey.trim()`
While these have TypeScript types declaring the parameter as `string`, at runtime the value could be undefined from:
- Corrupted persisted data in `runs.json`
- Edge cases in optional chaining paths
- Cross-process RPC data that isn't validated
## Solution
1. **Added nullish coalescing** to `.trim()` calls:
```typescript
const raw = (requesterSessionKey ?? "").trim();
```
2. **Added validation** when loading subagent registry from disk to skip entries with missing required string fields (`childSessionKey`, `requesterSessionKey`, `requesterDisplayKey`, `task`)
## Changes
- `src/agents/subagent-announce.ts`: Add `?? ""` guard in `resolveRequesterStoreKey()`
- `src/agents/subagent-registry.ts`: Add `?? ""` guard in `listSubagentRunsForRequester()`
- `src/agents/subagent-registry.store.ts`: Validate required fields when loading from disk
Fixes #8445
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR hardens subagent session-key handling and registry restore logic to prevent crashes when persisted or RPC-provided fields are unexpectedly `undefined` at runtime.
- `resolveRequesterStoreKey` and `listSubagentRunsForRequester` now guard `.trim()` calls with nullish coalescing to avoid `TypeError` during `sessions_spawn` and status/listing flows.
- `loadSubagentRegistryFromDisk` now skips corrupted/partial registry entries missing required string fields (`childSessionKey`, `requesterSessionKey`, `requesterDisplayKey`, `task`) so restore/resume paths don’t blow up on malformed `runs.json`.
These changes fit into the existing subagent lifecycle/persistence system by making the restore and query surface resilient to invalid stored data while keeping in-memory types unchanged.
<h3>Confidence Score: 4/5</h3>
- This PR is low-risk and primarily adds defensive guards, but there’s a small behavior-consistency concern around key normalization when filtering runs.
- Changes are localized and aimed at preventing a known runtime exception (`.trim()` on undefined) and skipping malformed persisted records. The main remaining risk is subtle behavior mismatch: trimming only the query key may cause legitimate runs with whitespace in stored keys to be unlistable, which can be hard to diagnose.
- src/agents/subagent-registry.ts (key normalization/filter semantics)
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#7569: fix: add null checks to prevent TypeError in subagent spawn
by kaigritun · 2026-02-03
85.8%
#13105: fix: debounce subagent lifecycle events to prevent premature announ...
by mcaxtr · 2026-02-10
80.9%
#20072: feat(sessions_spawn): add sessionKey param to reuse sub-agent sessions
by Be1Human · 2026-02-18
80.9%
#8893: fix: enhance subagent error reporting with diagnostic context
by joetomasone · 2026-02-04
80.7%
#23166: fix(agents): restore subagent announce chain from #22223
by tyler6204 · 2026-02-22
79.5%
#15982: fix: pass agentId to resolveSessionFilePath in reply flow (NX-003)
by automagik-genie · 2026-02-14
78.7%
#20712: fix(subagents): prioritize agent runtime default model over global ...
by sourcesavant · 2026-02-19
78.6%
#19328: Fix: preserve modelOverride in agent handler (#5369)
by CodeReclaimers · 2026-02-17
78.5%
#8774: Fix/frontend session key normalization
by zhaodageng · 2026-02-04
78.4%
#15792: fix: pass agentId to resolveSessionFilePath in additional call sites
by MisterGuy420 · 2026-02-13
78.4%