#7569: fix: add null checks to prevent TypeError in subagent spawn
agents
Cluster:
Subagent Task Management Fixes
Add null coalescing operators to prevent 'Cannot read properties of undefined (reading trim)' errors when spawning subagents:
- lanes.ts: Guard key.trim() with ?? ""
- system-prompt.ts: Guard file.path.trim() with ?? ""
These crashes occur when optional string parameters are undefined, causing subagents to fail immediately on spawn.
Fixes #7443
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR adds defensive nullish-coalescing guards around `.trim()` calls that can receive `undefined` at runtime, preventing subagent spawn failures (notably when normalizing session lane keys and when scanning injected context file paths for `soul.md`). The change is localized to `resolveSessionLane` in `src/agents/pi-embedded-runner/lanes.ts` and the `hasSoulFile` detection logic inside `buildAgentSystemPrompt` in `src/agents/system-prompt.ts`.
<h3>Confidence Score: 4/5</h3>
- This PR is low-risk and primarily adds defensive guards to prevent runtime crashes.
- Changes are small and localized, but there may be a minor type-safety concern where a function typed to accept `string` is now handling `undefined` at runtime (suggesting an upstream typing mismatch).
- src/agents/pi-embedded-runner/lanes.ts (type signature vs runtime inputs)
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#8471: fix(subagent): add defensive checks for undefined string fields
by adam-smeth · 2026-02-04
85.8%
#12956: fix: guard .trim() calls on potentially undefined workspaceDir
by omair445 · 2026-02-10
80.6%
#18647: fix(agents): guard against undefined paths in injectedFiles
by kleinpanic · 2026-02-16
78.6%
#6577: fix: add null checks for stdout/stderr when using inherit-stdio fal...
by ncmalan · 2026-02-01
77.5%
#15999: fix: handle null/undefined in activity feed filters
by pamnlambert · 2026-02-14
77.2%
#20851: fix: harden resolveUserPath and compact against undefined workspaceDir
by davidrudduck · 2026-02-19
76.8%
#14734: test(agents): guard against stale allowAgents in existing sessions
by davidahmann · 2026-02-12
76.4%
#17788: fix: correct config path in sessions_spawn error hint (v2)
by Limitless2023 · 2026-02-16
76.2%
#14136: feat: add agent collapse safeguards and fix TUI display on abort
by liangweigain-create · 2026-02-11
75.7%
#2541: fix(agents): add error handling to orphaned message cleanup
by Episkey-G · 2026-01-27
75.3%