SIGN IN SIGN UP

agent: Improve working directory path resolution in the terminal tool (#59937)

# Objective

This PR aims to fix these related deficiencies with the terminal tool's
working directory path resolution:

- Fix #60014
- Fix #60040
- Fix #60043

## Solution

- Use `project.path_style(cx).is_absolute(dir)` instead of
`Path::is_absolute`. The latter follows the path semantics of the host
which fails to recognize `/home/...` etc as an absolute path on Windows,
we want to follow the path semantics of the *project*.
- Splits out path resolution into a separate pure function
`resolve_cd_in_worktrees` to enable comprehensive cross-PathStyle
testing
- The function unifies path prefix checking across both absolute and
relative modes, using the `path_style.strip_prefix` (which uses the
RelPath util internally) which fails when subdirs try to escape with
`..`.
- Add comprehensive tests for absolute/relative modes, path styles, and
potential `..` escapes
- Enables targeting subdirectories with both absolute and relative path
modes (separate commit)
- Also makes both path modes use `project.worktrees`. Before, the
relative path mode used `project.visible_worktrees`.

## Testing

Here's how I tested these changes:

- Added a new test: `test_resolve_cd_uses_project_path_style` with a
comprehensive collection of assertions for `..` edge cases
- Run `cargo test -p agent` and `./script/clippy -p agent` on:
   - Linux
   - Windows in Ubuntu WSL
   - Windows native
- Build and run Zed on:
   - Linux
   - Windows
- Verify that the three issues are fixed:
   - `cd` param path traversal escapes are blocked
      - Linux local project
      - Windows local project
      - Windows remote WSL project
   - Allows absolute paths when running on Windows in a WSL project
- `cd` param may target subdirectories in both absolute and relative
path modes
      - Linux local project
      - Windows local project
      - Windows WSL project
- Check that different models find the updated tool descriptions to be
understandable and usable the first time. Used the prompt below to get
the following models to test path resolution on each of: Windows local,
Windows WSL, Linux. All models except GPT-5-nano one-shot the below
prompt.
    - Claude Sonnet 4.6
    - Claude Opus 4.8
    - Claude Haiku 4.5
    - GPT-5.5 pro
- GPT-5-nano - got there eventually but is confused by the design of the
cd param, it expects that if you set cd to `my-project` that it will
target the directory `my-project/my-project`.
    - Gemini 3.1 Pro
    - Gemini 3.5 Flash
    - Grok 4.3
    - Grok 0.1 build

<details>
<summary>Agent test prompt</summary>

<blockquote>

List the current directory contents and pick one subdir.

Then use that information and the project path details you were provided
to execute `pwd` with the terminal tool's cd parameter set to the
following values:
- current project absolute path
- current project name
- current project absolute path + an existing subdir
- current project name + an existing subdir
- absolute path to user's .ssh dir
- current project absolute path + whatever necessary path traversal .. +
.ssh segments to target the user's .ssh dir

Then tell me what happened.
Then stop.

</blockquote>

</details>

## Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content adheres to Zed's UI standards
([UX/UI](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
and
[icon](https://github.com/zed-industries/zed/blob/main/crates/icons/README.md)
guidelines)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

---

Release Notes:

- Fixed terminal tool targeting absolute directories on Windows host
connected to a remote SSH / WSL project
- Fixed terminal tool not blocking path traversal escapes
- Improved terminal tool targeting project subdirectories

---------

Co-authored-by: Richard Feldman <richard@zed.dev>
Co-authored-by: Richard Feldman <oss@rtfeldman.com>
J
Joe Taber committed
b5796233bd92695da23b7965cd991552a9dbc0c6
Parent: d61e80b
Committed by GitHub <noreply@github.com> on 8/6/2026, 12:59:28 PM