Skip to content

agent: Stop over-escaping dashes in tool_permissions regex patterns - #51603

Merged
ConradIrwin merged 2 commits into
zed-industries:mainfrom
mvanhorn:osc/51537-fix-regex-dash-overescaping
Apr 28, 2026
Merged

agent: Stop over-escaping dashes in tool_permissions regex patterns#51603
ConradIrwin merged 2 commits into
zed-industries:mainfrom
mvanhorn:osc/51537-fix-regex-dash-overescaping

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

Fixes #51537

regex::escape() escapes dashes, but dashes are only special inside [] character classes in regex. This means tool_permissions patterns end up with unnecessary backslashes:

Before: ^https?://typescript\-eslint\.io, ^git\-lfs\s+pull(\s|$)
After: ^https?://typescript-eslint\.io, ^git-lfs\s+pull(\s|$)

The fix adds a small escape_for_pattern() helper that calls regex::escape() then strips the unnecessary dash escaping via .replace("\\-", "-"). This is applied to all five call sites in pattern_extraction.rs.

Tests updated to expect unescaped dashes, plus a new test_dashes_are_not_escaped test covering terminal commands, URLs, and paths with dashes.

This PR was developed with AI assistance.

Release Notes:

  • Fixed unnecessary escaping of dashes in agent tool permission patterns (e.g. typescript\-eslint is now typescript-eslint)

@cla-bot

cla-bot Bot commented Mar 15, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement, and we don't have @mvanhorn on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@zed-community-bot zed-community-bot Bot added the first contribution the author's first pull request to Zed. NOTE: the label application is automated via github actions label Mar 15, 2026
@mvanhorn

Copy link
Copy Markdown
Contributor Author

@cla-bot check

@cla-bot

cla-bot Bot commented Mar 15, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement, and we don't have @mvanhorn on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@cla-bot

cla-bot Bot commented Mar 15, 2026

Copy link
Copy Markdown

The cla-bot has been summoned, and re-checked this pull request!

@mvanhorn

Copy link
Copy Markdown
Contributor Author

@cla-bot check

@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label Mar 15, 2026
@cla-bot

cla-bot Bot commented Mar 15, 2026

Copy link
Copy Markdown

The cla-bot has been summoned, and re-checked this pull request!

@SomeoneToIgnore SomeoneToIgnore added the area:ai Related to Agent Panel, Edit Prediction, Copilot, or other AI features label Mar 15, 2026
@ConradIrwin

Copy link
Copy Markdown
Member

Thanks for adding this, and sorry for the slow reply. Are you able to rebase this?

@ConradIrwin ConradIrwin self-assigned this Apr 23, 2026
Fixes zed-industries#51537

regex::escape() escapes dashes which are only special inside [] character
classes. This produces unnecessarily noisy patterns like
^https?://typescript\-eslint\.io when ^https?://typescript-eslint\.io
is correct and more readable.
@mvanhorn
mvanhorn force-pushed the osc/51537-fix-regex-dash-overescaping branch from 6590b81 to 1623df7 Compare April 24, 2026 02:39
@mvanhorn

Copy link
Copy Markdown
Contributor Author

Rebased on main in 1623df7c. Upstream had refactored extract_command_prefix to return normalized_tokens: Vec<String> (replacing the old command + subcommand split), so I kept the new match-on-tokens structure and swapped the three regex::escape call sites for escape_for_pattern, preserving the PR's original intent (leave dashes unescaped outside [] character classes).

Verified: cargo check -p agent clean. Full tests need Metal toolchain which I don't have locally, so deferring to CI.

@ConradIrwin

Copy link
Copy Markdown
Member

Thank you!

Also may be worth sending a PR upstream (though they may want this behavior intentionally so you can escape a set of characters, it seems a bit odd)

@ConradIrwin
ConradIrwin enabled auto-merge April 24, 2026 02:48
@mvanhorn

Copy link
Copy Markdown
Contributor Author

Thanks @ConradIrwin! Worth a look upstream - my read is regex is being conservative since - is meta inside character classes, so regex::escape plays it safe by escaping it everywhere. Will think about whether a PR there is worth the noise.

Separately, the failing CI runs all blow up on role "root" does not exist in the postgres setup, which doesn't look related to this patch. Happy to rebase again or push an empty commit if a fresh run would help.

@ConradIrwin

ConradIrwin commented Apr 27, 2026

Copy link
Copy Markdown
Member

I see tests relating to this failing on the macOS CI:
Screenshot 2026-04-27 at 11 24 58 PM

You should be able to run it locally with cargo test -p agent test_extract_terminal_pattern

…pattern

Per @ConradIrwin's review on PR zed-industries#51603: the escape_for_pattern() helper
introduced in 1623df7c strips \\- back to - everywhere, including inside
single-quoted env var values. The test_extract_terminal_pattern fixture
for "PAGER='less -R' git log" was still asserting the old escaped form,
which broke macOS / Linux / Windows CI.
auto-merge was automatically disabled April 28, 2026 15:59

Head branch was pushed to by a user without write access

@mvanhorn

Copy link
Copy Markdown
Contributor Author

Thanks @ConradIrwin. That test expectation hadn't been updated when 1623df7c introduced escape_for_pattern. Pushed 34efb94 with the fix on test_extract_terminal_pattern: the PAGER='less -R' git log fixture now expects the unescaped dash inside the env var value.

Couldn't reproduce locally (this Mac is missing the Metal toolchain so the agent crate won't build), so this is verified by reading the diff. The single-quoted env-var path runs through the same escape_for_pattern as everything else, which strips \- back to -. CI should be green on the next run.

@ConradIrwin
ConradIrwin enabled auto-merge April 28, 2026 21:37
@ConradIrwin

Copy link
Copy Markdown
Member

Thanks!

@ConradIrwin
ConradIrwin added this pull request to the merge queue Apr 28, 2026
Merged via the queue into zed-industries:main with commit a8ca954 Apr 28, 2026
31 checks passed
ebaah46 pushed a commit to ebaah46/zed that referenced this pull request May 6, 2026
…ed-industries#51603)

Fixes zed-industries#51537

`regex::escape()` escapes dashes, but dashes are only special inside
`[]` character classes in regex. This means tool_permissions patterns
end up with unnecessary backslashes:

**Before:** `^https?://typescript\-eslint\.io`, `^git\-lfs\s+pull(\s|$)`
**After:** `^https?://typescript-eslint\.io`, `^git-lfs\s+pull(\s|$)`

The fix adds a small `escape_for_pattern()` helper that calls
`regex::escape()` then strips the unnecessary dash escaping via
`.replace("\\-", "-")`. This is applied to all five call sites in
`pattern_extraction.rs`.

Tests updated to expect unescaped dashes, plus a new
`test_dashes_are_not_escaped` test covering terminal commands, URLs, and
paths with dashes.

This PR was developed with AI assistance.

Release Notes:

- Fixed unnecessary escaping of dashes in agent tool permission patterns
(e.g. `typescript\-eslint` is now `typescript-eslint`)

---------

Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
jonx pushed a commit to jonx/zed-aros that referenced this pull request Jul 17, 2026
…ed-industries#51603)

Fixes zed-industries#51537

`regex::escape()` escapes dashes, but dashes are only special inside
`[]` character classes in regex. This means tool_permissions patterns
end up with unnecessary backslashes:

**Before:** `^https?://typescript\-eslint\.io`, `^git\-lfs\s+pull(\s|$)`
**After:** `^https?://typescript-eslint\.io`, `^git-lfs\s+pull(\s|$)`

The fix adds a small `escape_for_pattern()` helper that calls
`regex::escape()` then strips the unnecessary dash escaping via
`.replace("\\-", "-")`. This is applied to all five call sites in
`pattern_extraction.rs`.

Tests updated to expect unescaped dashes, plus a new
`test_dashes_are_not_escaped` test covering terminal commands, URLs, and
paths with dashes.

This PR was developed with AI assistance.

Release Notes:

- Fixed unnecessary escaping of dashes in agent tool permission patterns
(e.g. `typescript\-eslint` is now `typescript-eslint`)

---------

Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
jolutz pushed a commit to jolutz/zed that referenced this pull request Aug 8, 2026
…ed-industries#51603)

Fixes zed-industries#51537

`regex::escape()` escapes dashes, but dashes are only special inside
`[]` character classes in regex. This means tool_permissions patterns
end up with unnecessary backslashes:

**Before:** `^https?://typescript\-eslint\.io`, `^git\-lfs\s+pull(\s|$)`
**After:** `^https?://typescript-eslint\.io`, `^git-lfs\s+pull(\s|$)`

The fix adds a small `escape_for_pattern()` helper that calls
`regex::escape()` then strips the unnecessary dash escaping via
`.replace("\\-", "-")`. This is applied to all five call sites in
`pattern_extraction.rs`.

Tests updated to expect unescaped dashes, plus a new
`test_dashes_are_not_escaped` test covering terminal commands, URLs, and
paths with dashes.

This PR was developed with AI assistance.

Release Notes:

- Fixed unnecessary escaping of dashes in agent tool permission patterns
(e.g. `typescript\-eslint` is now `typescript-eslint`)

---------

Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ai Related to Agent Panel, Edit Prediction, Copilot, or other AI features cla-signed The user has signed the Contributor License Agreement first contribution the author's first pull request to Zed. NOTE: the label application is automated via github actions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Over-escaping of characters (e.g. dash) in tool_permissions pattern's

3 participants