Skip to content

fix(agents): expand ~ in path for read/write/edit tools#30744

Open
jackjin1997 wants to merge 9 commits intoopenclaw:mainfrom
jackjin1997:fix-edit-tool-tilde-expansion
Open

fix(agents): expand ~ in path for read/write/edit tools#30744
jackjin1997 wants to merge 9 commits intoopenclaw:mainfrom
jackjin1997:fix-edit-tool-tilde-expansion

Conversation

@jackjin1997
Copy link

Fixes #30669. Standardizes tilde expansion across all filesystem tools by expanding ~ in normalizeToolParams (src/agents/pi-tools.read.ts).

@openclaw-barnacle openclaw-barnacle bot added agents Agent runtime and tooling size: XS labels Mar 1, 2026
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9ea935549e

ℹ️ 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".

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 1, 2026

Greptile Summary

This PR implements tilde (~) expansion for filesystem tools (read/write/edit) by adding expandHomePrefix in normalizeToolParams. The implementation correctly handles path normalization and applies to all three filesystem tools through a single centralized function.

Additionally includes:

  • Brave search baseUrl configuration support (mirrors existing Perplexity pattern)
  • Inter-session message role attribution fix (sets role to "assistant" for inter-session provenance)

Key changes:

  • normalizeToolParams now expands ~ prefix using the expandHomePrefix utility
  • Type-safe with proper string checking before expansion
  • Falls back gracefully if home directory cannot be resolved
  • Brave search cache key updated to include baseUrl for proper cache segmentation

Confidence Score: 4/5

  • Safe to merge - changes are technically sound with proper type safety and error handling
  • Score reflects solid implementation of tilde expansion using well-tested utility functions, proper type checking, and graceful fallbacks. The changes follow existing codebase patterns and include multiple logical fixes. Deducted one point because the PR bundles three separate concerns (tilde expansion, Brave search baseUrl, inter-session message roles) which ideally should be separate PRs for easier review and rollback if needed.
  • No files require special attention - all changes are straightforward and follow existing patterns

Last reviewed commit: 9ea9355

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 40a2a59db2

ℹ️ 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".

thomasxm pushed a commit to thomasxm/openclaw that referenced this pull request Mar 1, 2026
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
thomasxm pushed a commit to thomasxm/openclaw that referenced this pull request Mar 1, 2026
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
thomasxm pushed a commit to thomasxm/openclaw that referenced this pull request Mar 1, 2026
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
thomasxm pushed a commit to thomasxm/openclaw that referenced this pull request Mar 1, 2026
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
thomasxm pushed a commit to thomasxm/openclaw that referenced this pull request Mar 1, 2026
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
thomasxm pushed a commit to thomasxm/openclaw that referenced this pull request Mar 1, 2026
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
thomasxm pushed a commit to thomasxm/openclaw that referenced this pull request Mar 3, 2026
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
thomasxm pushed a commit to thomasxm/openclaw that referenced this pull request Mar 3, 2026
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
@jackjin1997 jackjin1997 force-pushed the fix-edit-tool-tilde-expansion branch from 0238fcf to 97b9410 Compare March 5, 2026 16:56
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 97b94100de

ℹ️ 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".

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 80189696ec

ℹ️ 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".

jackjin1997 added a commit to jackjin1997/openclaw that referenced this pull request Mar 5, 2026
- pi-tools.params.ts: add expandHomePrefix to normalizeToolParams so ~
  expansion is preserved after upstream refactored the function out of
  pi-tools.read.ts (the PR's core feature was silently dropped in the
  conflict resolution)

- input-provenance.ts: remove inter-session role rewrite (user→assistant)
  that broke hasInterSessionUserProvenance filters downstream; session-memory
  and session-utils relied on role=user to detect and skip inter-session
  content, so the rewrite caused cross-session content to leak into memory
  and history exports; provenance field alone is sufficient to identify
  inter-session messages

Addresses Codex review P1/P2 feedback on PR openclaw#30744.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a620fdbe14

ℹ️ 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".

@jackjin1997 jackjin1997 force-pushed the fix-edit-tool-tilde-expansion branch from a620fdb to 61d2d49 Compare March 6, 2026 03:04
jackjin1997 added a commit to jackjin1997/openclaw that referenced this pull request Mar 6, 2026
- pi-tools.params.ts: add expandHomePrefix to normalizeToolParams so ~
  expansion is preserved after upstream refactored the function out of
  pi-tools.read.ts (the PR's core feature was silently dropped in the
  conflict resolution)

- input-provenance.ts: remove inter-session role rewrite (user→assistant)
  that broke hasInterSessionUserProvenance filters downstream; session-memory
  and session-utils relied on role=user to detect and skip inter-session
  content, so the rewrite caused cross-session content to leak into memory
  and history exports; provenance field alone is sufficient to identify
  inter-session messages

Addresses Codex review P1/P2 feedback on PR openclaw#30744.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 61d2d49041

ℹ️ 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".

jackjin1997 and others added 6 commits March 6, 2026 20:23
…nter-session role

- Fix inter-session provenance role rewrite to only apply to user messages
- Add baseUrl to ToolsWebSearchSchema zod validation for Brave baseUrl support
- Fix whitespace-only braveBaseUrl causing crash by trimming before fallback
- pi-tools.params.ts: add expandHomePrefix to normalizeToolParams so ~
  expansion is preserved after upstream refactored the function out of
  pi-tools.read.ts (the PR's core feature was silently dropped in the
  conflict resolution)

- input-provenance.ts: remove inter-session role rewrite (user→assistant)
  that broke hasInterSessionUserProvenance filters downstream; session-memory
  and session-utils relied on role=user to detect and skip inter-session
  content, so the rewrite caused cross-session content to leak into memory
  and history exports; provenance field alone is sufficient to identify
  inter-session messages

Addresses Codex review P1/P2 feedback on PR openclaw#30744.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…read

After rebasing onto upstream/main, the tilde-expansion cherry-pick
re-introduced the normalizeToolParams/CLAUDE_PARAM_GROUPS/etc. block
inline into pi-tools.read.ts. Upstream already moved these to
pi-tools.params.ts (re-exported via pi-tools.read.ts), so the inline
copy caused redeclared-export TS errors and an unused import.

Remove the duplicate block and the now-unused expandHomePrefix import.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@jackjin1997 jackjin1997 force-pushed the fix-edit-tool-tilde-expansion branch from c8a1567 to 02dc5a5 Compare March 6, 2026 12:29
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 02dc5a52be

ℹ️ 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".

}
// Expand ~ in path
if (typeof normalized.path === "string" && normalized.path.startsWith("~")) {
normalized.path = expandHomePrefix(normalized.path);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Expand filesystem tool tildes using OS home, not OPENCLAW_HOME

normalizeToolParams now expands path with expandHomePrefix, but this helper defaults to resolveEffectiveHomeDir, which prefers OPENCLAW_HOME; in deployments that set OPENCLAW_HOME for app-state isolation, a user input like ~/notes.txt is rewritten to <OPENCLAW_HOME>/notes.txt instead of the account home directory, so read/write/edit calls can hit the wrong file tree (or fail workspace guards unexpectedly). This expansion should use the OS user home source (HOME/USERPROFILE/os.homedir()) for tool paths, similar to the explicit-home expansion path used in fs-safe.

Useful? React with 👍 / 👎.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ae8ba6eedb

ℹ️ 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".

Comment on lines +101 to +102
if (typeof normalized.path === "string" && normalized.path.startsWith("~")) {
normalized.path = expandHomePrefix(normalized.path);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Expand fs-tool tilde paths with OS home

normalizeToolParams now expands ~ via expandHomePrefix without a home override, which makes ~/... resolve to OPENCLAW_HOME when that env var is set. In deployments that set OPENCLAW_HOME for app-state isolation, read/write/edit tool calls will target that state directory instead of the actual account home (or be blocked by workspace guards), so user file paths are rewritten to the wrong tree. This should use OS-home expansion for tool paths (as expandRelativePathWithHome does in src/infra/fs-safe.ts).

Useful? React with 👍 / 👎.

jackjin1997 added a commit to jackjin1997/openclaw that referenced this pull request Mar 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Edit tool does not expand ~ (tilde) in file paths

1 participant