#17757: fix(agents): resolve relative workspace paths against state dir, not CWD
agents
stale
size: S
Cluster:
Workspace Path Fixes
## Summary
- Fixes #17698 — `agents.list[].workspace` was ignored for sub-agent session CWD
- `resolveAgentWorkspaceDir` used `resolveUserPath` which calls `path.resolve()` against `process.cwd()`. After a parent agent calls `process.chdir()`, relative workspace paths resolved against the wrong directory
- Adds a `resolveWorkspacePath` helper that resolves relative paths against the stable state dir (`~/.openclaw/`) instead, making workspace resolution deterministic regardless of concurrent `process.chdir` calls
- Absolute and `~`-prefixed paths continue to use `resolveUserPath` unchanged
## Test plan
- [x] New tests: relative workspace resolves against state dir, not CWD
- [x] New tests: absolute workspace paths unaffected by CWD changes
- [x] New tests: tilde workspace paths expand correctly
- [x] New tests: defaults workspace resolves relative paths against state dir
- [x] All existing agent-scope, workspace-run, workspace e2e tests pass (31 tests)
- [x] All sessions-spawn e2e tests pass (18 tests)
- [x] All agent unit tests pass (253 tests)
- [x] Build passes
- [x] Lint + format clean
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Fixed agent workspace path resolution to be deterministic by resolving relative paths against the stable state directory (`~/.openclaw/`) instead of `process.cwd()`. This prevents race conditions when parent agents call `process.chdir()` before spawning sub-agents. The new `resolveWorkspacePath` helper maintains backward compatibility for absolute and tilde-prefixed paths while ensuring relative workspace configurations are no longer affected by concurrent directory changes.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- The fix is well-isolated to workspace path resolution logic, maintains backward compatibility for absolute and tilde paths, and includes comprehensive test coverage for all path types (relative, absolute, tilde) and both agent-specific and default workspace configurations. The implementation correctly addresses the race condition without introducing breaking changes.
- No files require special attention
<sub>Last reviewed commit: 153d4c8</sub>
<!-- 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
85.1%
#23085: fix(workspace): respect OPENCLAW_STATE_DIR for workspace paths, fix...
by charojo · 2026-02-22
83.6%
#10185: fix: pass effective workspace as cwd to Pi SDK createAgentSession
by Yida-Dev · 2026-02-06
79.3%
#17488: fix(agents): skip workspace deletion when shared with other agents
by soumikbhatta · 2026-02-15
78.4%
#20851: fix: harden resolveUserPath and compact against undefined workspaceDir
by davidrudduck · 2026-02-19
78.1%
#20336: fix(sessions): resolve transcriptPath using agentId when storePath ...
by Limitless2023 · 2026-02-18
78.1%
#18914: fix: use per-session workspaceDir instead of process.cwd() for post...
by irchelper · 2026-02-17
78.1%
#16135: fix: handle session file paths from other agents in doctor command
by MisterGuy420 · 2026-02-14
78.0%
#21553: fix: resolve workspace template dir in bundled dist/ layout
by echoVic · 2026-02-20
78.0%
#11529: fix(wizard): strip shell-style backslash escapes from workspace paths
by mcaxtr · 2026-02-07
77.9%