Subagent spawn crashes with 'Cannot read properties of undefined (reading trim)'
Resolved 💬 2 comments Opened Feb 3, 2026 by heybeaux Closed Mar 3, 2026
Bug Description
When spawning subagents via sessions_spawn, the agent crashes with:
TypeError: Cannot read properties of undefined (reading 'trim')
The error occurs in buildAgentSystemPrompt() when processing context files where file.path is undefined.
Stack Trace
TypeError: Cannot read properties of undefined (reading 'trim')
at file:///...dist/agents/system-prompt.js:437:46
at Array.some (<anonymous>)
at buildAgentSystemPrompt (file:///...dist/agents/system-prompt.js:436:42)
at buildEmbeddedSystemPrompt (file:///...dist/agents/pi-embedded-runner/system-prompt.js:4:12)
at runEmbeddedAttempt (file:///...dist/agents/pi-embedded-runner/run/attempt.js:280:30)
Root Cause
In src/agents/system-prompt.ts around line 538:
const hasSoulFile = contextFiles.some((file) => {
const normalizedPath = file.path.trim().replace(/\\/g, "/"); // <-- crashes if file.path is undefined
// ...
});
Fix
Change to:
const normalizedPath = (file.path ?? "").trim().replace(/\\/g, "/");
Environment
- OpenClaw version: 2026.2.1
- OS: macOS (Darwin 24.6.0 arm64)
- Node: v24.13.0
Impact
All sessions_spawn calls fail immediately (~20-30ms after start), making subagent functionality completely broken.
Additional Notes
During investigation, I also found other locations with similar patterns that may need guards:
src/agents/subagent-registry.ts:420-requesterSessionKey.trim()src/agents/subagent-announce.ts:152-requesterSessionKey.trim()src/agents/pi-embedded-runner/lanes.ts:4-key.trim()
These may not be the primary cause but are similar unsafe patterns.
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗