#22917: fix: Use agent's configured workspace when spawned as subagent
agents
size: S
Cluster:
Workspace Path Fixes
## Summary
- Subagents spawned via `sessions_spawn` inherit the parent agent's `workspaceDir`, which is forwarded as a parameter to `resolveRunWorkspaceDir`
- The function accepted this inherited value without checking whether the child agent has its own workspace configured in `agents.list`
- Result: subagents run in the parent's workspace, missing their own workspace files (skills, AGENTS.md, memory, scripts)
## Problem
In `resolveRunWorkspaceDir`, when a non-empty `workspaceDir` string is provided, it was returned immediately without consulting the agent's configuration. The fallback path (null/undefined workspaceDir) correctly calls `resolveAgentWorkspaceDir` which checks `agents.list`, but the explicit-string path did not.
## Solution
Before accepting a provided `workspaceDir`, check whether the resolved agent has an explicitly configured workspace in `agents.list`. If it does, prefer that over the provided value. This only activates when both conditions are true — the agent has a configured workspace AND a non-empty `workspaceDir` was provided — leaving existing behavior unchanged for all other cases.
## Testing
- Added `src/agents/workspace-run.test.ts` with 7 tests
- Two new tests cover the subagent inheritance scenario (fail without fix, pass with fix)
- Five tests verify existing behavior is preserved (no configured workspace, blank/missing/invalid workspaceDir)
- All tests pass
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR fixes a workspace inheritance bug where subagents spawned via `sessions_spawn` incorrectly used their parent agent's workspace instead of their own configured workspace. The fix adds a check in `resolveRunWorkspaceDir` to prefer an agent's explicitly configured workspace (from `agents.list`) over any inherited `workspaceDir` parameter.
- Fixed workspace resolution logic to check agent config before accepting provided `workspaceDir`
- Added comprehensive test coverage (7 tests) covering the subagent scenario and existing behavior
- Maintains backward compatibility for agents without configured workspaces
- Applies security hardening via `sanitizeForPromptLiteral` to prevent prompt injection
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- The fix is well-targeted, adds proper test coverage for both the bug scenario and existing behavior, maintains backward compatibility, and follows existing code patterns. The implementation correctly handles all edge cases (missing workspace, blank strings, invalid types) and applies security hardening consistently.
- No files require special attention
<sub>Last reviewed commit: bf68fe3</sub>
<!-- greptile_other_comments_section -->
<sub>(5/5) You can turn off certain types of comments like style [here](https://app.greptile.com/review/github)!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#17757: fix(agents): resolve relative workspace paths against state dir, no...
by Phineas1500 · 2026-02-16
85.1%
#17488: fix(agents): skip workspace deletion when shared with other agents
by soumikbhatta · 2026-02-15
81.8%
#12956: fix: guard .trim() calls on potentially undefined workspaceDir
by omair445 · 2026-02-10
81.4%
#18914: fix: use per-session workspaceDir instead of process.cwd() for post...
by irchelper · 2026-02-17
81.0%
#20712: fix(subagents): prioritize agent runtime default model over global ...
by sourcesavant · 2026-02-19
80.7%
#20851: fix: harden resolveUserPath and compact against undefined workspaceDir
by davidrudduck · 2026-02-19
79.5%
#19560: docs: refactor agent workspace default location & bootstrap configu...
by ashinkuniyil · 2026-02-17
79.3%
#10185: fix: pass effective workspace as cwd to Pi SDK createAgentSession
by Yida-Dev · 2026-02-06
78.9%
#17788: fix: correct config path in sessions_spawn error hint (v2)
by Limitless2023 · 2026-02-16
78.8%
#16964: test: add regression tests for sub-agent session path resolution
by StressTestor · 2026-02-15
78.3%