Skip to content

feat(grep): add -P for Perl regexes, rejecting unsupported constructs - #219

Merged
ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:feat/grep-perl-regex
Nov 12, 2026
Merged

ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:feat/grep-perl-regex

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Agents reach for grep -P all the time, but we rejected it outright:

$ echo 'a1 b22' | grep -oP '\d+'
grep: invalid option '-P'

This adds -P / --perl-regexp. It uses the Rust regex engine, so PCRE-only features like lookaround, backreferences, atomic groups, possessive quantifiers and (?x) fail loudly with exit 2 instead of mis-matching. A few PCRE literals the engine would otherwise read as syntax (\<, \>, and && / ~~ inside a class) are kept literal, and -P with -E or -F is an error, like in GNU grep. It works with -i, -w, -x, -o and the rest.

I compared it against GNU grep 3.8 for \d, \s, \b, \t, lazy quantifiers, (?i), named groups and the flag combinations, and the output matches. There are two differences I know of: we accept several -e patterns with -P, where GNU 3.8 refuses them, and \d also matches non-ASCII digits. Separately, BRE patterns still get ERE semantics. That's a different issue and I didn't touch it here.

Related Issues

Part of #207

Documentation PR

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@ferdingler
ferdingler force-pushed the feat/grep-perl-regex branch from bbe2a79 to 76c66c7 Compare November 6, 2026 05:37
@ferdingler
ferdingler marked this pull request as ready for review November 12, 2026 20:13
@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 20:13
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Verdict: two small fixes before merge. The design holds up. I ran ~50 differential probes against GNU grep 3.8 -P, and nearly every divergence is a loud exit-2 refusal, as the PR promises. I found one silent wrong answer.

  • 🟡 \v reaches the crate unchanged, so it matches VT only. grep -cP 'a\vb' misses \f/\r lines (1 vs GNU's 3, exit 0). Fix is inline and I've tested it.
  • 🟡 The refusal message gives an agent no way forward. \K and lookbehind are the most common reasons agents reach for -P, and both hit this message. Suggested wording is inline.
  • Docs follow-up: harness-sdk user-guide/shell/commands.mdx (General callouts) still says "grep -P is unsupported".
  • Question (non-blocking): did you consider fancy-regex? It's pure Rust, works on &[u8], and supports \K, lookaround and backrefs. It would shrink the translator, though class operators and the literal { would still need translating. The current surface would let us swap it in later, so this doesn't block. A line on why it was ruled out would help the next reviewer.

The full adversarial pass timed out twice. A focused 15-probe adversarial pass plus my own probes cover it, so treat that pass as partial.

✅ What I verified
  • Head 2cbae54. cargo test --test shell_integration grep: 103 passed.
  • Applied the \v suggestion in a scratch copy. a\vb, a[\v]b and a[^\v]b give 3, 3 and 1, the same as GNU. The 103 tests still pass.
  • A 37-pattern differential harness gave 22 identical and 15 differing. Of the 15, 13 are loud refusals and 2 are listed below. A focused probe set (\p{L}, [a-], [-a], [^]a], [[:^alpha:]], a{2,3}?, -i é/É, unclosed (?, \x{41) found no panics and no new silent mismatches.
Appendix: non-blocking (8)
  • These fail loudly where GNU accepts them, with a raw crate error rather than "-P does not support": \Q…\E, \h, \Z, \N, \o{…}, (?#…), a{,2}. \Q…\E and \h would be cheap to translate. \K, lookaround, backrefs, possessive quantifiers and (?x) are refused by design.
  • a** and a*?+ are accepted and matched; GNU exits 2 with "quantifier does not follow a repeatable item" (grep.rs:311-321). Only a typo produces these.
  • (?-x)a and (?i-x)A are refused as (?x) (grep.rs:299). The check should fire only when x comes before any -.
  • \101 (octal A in PCRE) is reported as "backreference" (grep.rs:210).
  • The help text (grep.rs:19-20) leaves out \K and (?x), which are both refused. \K is the one agents will try.
  • With several -e patterns, (?i) in one leaks into the next because of the bare | join (grep.rs:175): -P -e '(?i)a' -e B matches b. GNU refuses multiple -e with -P, so this only bites because the PR accepts them. Wrapping each pattern in (?:…) fixes it. Same behaviour on -E.
  • Pre-existing, also on main: -w wraps in \b, while GNU uses non-word lookaround, so echo '@b x' | grep -owP '@b' prints nothing here. -o prints a blank line for each empty match (grep.rs:566-578; grep -oP 'b*' on abc). grep -E 'a(' exits 1, where -P exits 2.
  • Not a finding: piping host stdin into strands-shell -c "grep …" gives bad fd 0. This is a documented CLI limitation, and plain grep and wc fail the same way.
Reading order

grep.rs:190-353 (translator), then :355-372, then :649-661 (wiring/exit 2), then the tests in tests/shell_integration.rs.

Comment thread src/commands/grep.rs Resolved
Comment thread src/commands/grep.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

I checked fancy-regex 0.20.2: you’re right that its input API supports byte slices. I’m keeping the current non-backtracking engine for this PR because moving to backtracking would also require choosing execution limits and handling runtime match errors. That deserves a separate change; the command’s interface leaves room for it.

For the optional findings, I left the extra unsupported escapes, disabled-x rejection, octal diagnostic and malformed repeated quantifiers unchanged. Those are either loud refusals or malformed-pattern handling. Isolating flags across several -e patterns should be handled with the existing -E behavior too. The existing -w, empty-match -o, -E error-status and CLI-stdin behaviors are separate from these fixes. The help omission is fixed.

The harness-sdk command-reference text still needs a separate docs update to describe the supported subset. I haven’t changed that repository here.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approve. Both 🟡 findings are fixed in 4a85886 and I didn't find any new problems.

  • ✅ \v: a\vb, a[\v]b, a[^\v]b, a[x\v]b, a\v+b and escaped a\\vb all match GNU grep 3.8 (count and exit code).
  • ✅ The refusal message now points at -o … | sed/cut. --help lists \K and (?x).
  • Agreed on keeping fancy-regex for a separate change, since the execution limits and runtime errors it brings need their own design. Docs follow-up in harness-sdk is still open.
Verified / one nit
  • Head 4a85886. cargo test --test shell_integration grep: 104 passed, including the new grep_perl_vertical_whitespace.
  • ⚪ [\v-z] now matches - and z (the crate reads [[…]-z] as a nested class plus literals), while GNU exits 2 with "invalid range". It's a malformed pattern, so it falls in the same bucket you've already decided to leave alone. No action needed.

@ferdingler
ferdingler merged commit 7b946fc into strands-agents:main Nov 12, 2026
31 checks passed
@ferdingler
ferdingler deleted the feat/grep-perl-regex branch November 12, 2026 23:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants