#12956: fix: guard .trim() calls on potentially undefined workspaceDir
agents
size: XS
Cluster:
Workspace Path Fixes
## Summary
Fixes sub-agent spawning crashing with `TypeError: Cannot read properties of undefined (reading 'trim')` when agent config includes `workspace: null`.
## Problem
When an agent is configured with `workspace: null`, `resolveAgentConfig()` correctly resolves it to `undefined`. However, downstream functions in the skills subsystem call `.trim()` directly on the workspace string without null guards, causing a crash during sub-agent initialization.
## Changes
**`src/agents/skills/plugin-skills.ts`**
- `resolvePluginSkillDirs()`: `(params.workspaceDir ?? "").trim()`
**`src/agents/skills/refresh.ts`**
- `resolveWatchPaths()`: `(workspaceDir ?? "").trim()`
- `ensureSkillsWatcher()`: `(params.workspaceDir ?? "").trim()`
**`src/agents/skills/refresh.test.ts`**
- New test: verifies `ensureSkillsWatcher` doesn't crash with `undefined` workspaceDir
## Testing
- All 2 skills refresh tests pass ✅
- 🤖 AI-assisted (Claude) — fully tested
Closes #12888
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR prevents crashes in the skills subsystem when an agent config resolves `workspace` to `undefined` by guarding `.trim()` calls in skills refresh and plugin skill directory resolution.
Changes are localized to the skills watcher setup (`src/agents/skills/refresh.ts`) and plugin skill dir resolution (`src/agents/skills/plugin-skills.ts`), plus a new Vitest case asserting `ensureSkillsWatcher` doesn’t throw when `workspaceDir` is undefined.
<h3>Confidence Score: 4/5</h3>
- This PR is likely safe to merge, but the public function types no longer match the new optional-handling behavior.
- Runtime crash is addressed by null-guarding `.trim()`; however, keeping parameters typed as `string` while treating them as optional reduces compile-time protection and may allow similar bugs to recur without TypeScript catching them.
- src/agents/skills/refresh.ts, src/agents/skills/plugin-skills.ts, src/agents/skills/refresh.test.ts
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#22917: fix: Use agent's configured workspace when spawned as subagent
by jriff · 2026-02-21
81.4%
#7569: fix: add null checks to prevent TypeError in subagent spawn
by kaigritun · 2026-02-03
80.6%
#22198: fix(skills): treat empty allowBundled array as block-all
by haitao-sjsu · 2026-02-20
80.5%
#19707: fix(agents): apply per-agent skills filter to all run paths
by mcaxtr · 2026-02-18
80.3%
#20851: fix: harden resolveUserPath and compact against undefined workspaceDir
by davidrudduck · 2026-02-19
80.1%
#10016: fix: prevent FD exhaustion from skill watcher scanning artifact trees
by oldeucryptoboi · 2026-02-06
79.8%
#11250: fix: expand skills watcher ignore list and improve session repair l...
by zhangzhefang-github · 2026-02-07
79.7%
#12076: fix(skills): recursive directory filtering to actually exclude venv...
by xiaoyaner0201 · 2026-02-08
79.5%
#18647: fix(agents): guard against undefined paths in injectedFiles
by kleinpanic · 2026-02-16
79.3%
#21839: fix(skills): allowBundled: [] now blocks all bundled skills
by hydro13 · 2026-02-20
78.8%