← Back to PRs

#22917: fix: Use agent's configured workspace when spawned as subagent

by jriff open 2026-02-21 20:29 View on GitHub →
agents size: S
## 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