fix(agents): expand tilde in tool file paths#30866
fix(agents): expand tilde in tool file paths#30866thomasxm wants to merge 1 commit intoopenclaw:mainfrom
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fe73537d2d
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
src/agents/bash-tools.shared.ts
Outdated
| export function resolveWorkdir(workdir: string, warnings: string[]) { | ||
| const current = safeCwd(); | ||
| const fallback = current ?? homedir(); | ||
| const expanded = path.resolve(expandHomePrefix(workdir, { home: homedir() })); |
There was a problem hiding this comment.
Handle missing cwd before resolving relative workdir
If the process current directory has been deleted/unmounted, safeCwd() correctly falls back, but path.resolve(...) now runs before the try block and throws ENOENT for relative workdir values, so this function exits with an uncaught error instead of emitting the warning and using the fallback. This regresses resolveWorkdir in the exact scenario it was already designed to tolerate, and it can break tools.exec requests that pass relative paths after cwd loss.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed — moved path.resolve(expandHomePrefix(...)) inside the try block so ENOENT from a deleted cwd is caught by the existing catch handler.
Greptile SummaryThis PR correctly fixes tilde ( Key changes:
Implementation quality:
Confidence Score: 5/5
Last reviewed commit: fe73537 |
path.resolve() treats ~ as a literal directory name, so ~/file.txt resolved to <cwd>/~/file.txt instead of /home/user/file.txt. Add expandHomePrefix() before path.resolve() in all affected tool path resolution points: - Host write operations (mkdir, writeFile) in non-workspace mode - Host edit operations (readFile, writeFile, access) in non-workspace mode - toRelativePathInRoot() workspace boundary validator (affects all tools) - resolveWorkdir() for non-sandbox exec/bash working directory - parseSandboxBindMount() for Docker bind mount host paths The host read tool and sandbox tools already handled tilde via the upstream library's expandPath(). Closes openclaw#30669 Related: openclaw#30782, openclaw#30788, openclaw#30744, openclaw#30770, openclaw#30756, openclaw#30753, openclaw#30752, openclaw#30747
fe73537 to
991d782
Compare
|
Thanks for the PR! Multiple PRs address the same fix. Keeping #30431 as the earliest submission. Closing to reduce noise. This is an AI-assisted triage review. If we got this wrong, feel free to reopen — happy to revisit. |
|
@steipete Thanks for the note — reopening as #33501 (force-push during rebase prevented reopening this one). This PR is not a duplicate of #30431. The two PRs have significantly different scope: #30431 touches 2 files and fixes tilde expansion in one location: This PR (#30866, now #33501) touches 6 files and fixes 5 distinct path resolution points, three of which are completely independent of
Additionally, this PR includes 11 test cases vs 1 in #30431, and fixes a pre-existing Windows CI failure where a hardcoded See the detailed comparison table in #33501. |
Summary
path.resolve("~/file.txt")treats~as a literal directory name, resolving to<cwd>/~/file.txtinstead of/home/user/file.txtexpandHomePrefix(..., { home: os.homedir() })beforepath.resolve()at all affected tool path resolution points, ensuring~always expands to the real OS home (notOPENCLAW_HOME)fs-safe.test.tswhere a hardcoded Unix path/tmp/fake-home-testbroke on WindowsPath resolution points covered
toRelativePathInRoot()workspace boundary (all tools)resolveWorkdir()non-sandbox exec/bash workdirparseSandboxBindMount()Docker bind mount host pathexpandPath()expandPath()assertSandboxPath()Files changed
Production (4):
src/agents/pi-tools.read.ts— 1 import + 6 call sitessrc/agents/bash-tools.shared.ts— 1 import, tilde expansion + path normalization inresolveWorkdir()src/agents/sandbox/fs-paths.ts— 1 import + 1 call site inparseSandboxBindMount()src/infra/fs-safe.test.ts— fix hardcoded Unix path for Windows CITest (2):
src/agents/pi-tools.read.tilde-expansion.test.ts— 10 tests (new file)src/agents/sandbox/fs-paths.test.ts— 1 new testTest plan
~path creates file in home directorymkdirwith~creates directory in homereadFilewith~reads from homeaccesswith~checks home path~paths outside workspace root~paths inside workspace rootresolveWorkdir("~")returns home directoryresolveWorkdir("~/subdir")returns home subdirectoryresolveWorkdirfalls back with warning for nonexistent~subdirparseSandboxBindMount("~/mydata:/data:rw")expands host pathexpandHomePrefixtest uses platform-correct paths (Windows CI fix)Closes #30669
Related: #30782, #30788, #30744, #30770, #30756, #30753, #30752, #30747