Skip to content

fix(tools): expand brace patterns in the grep include filter - #6681

Closed
lxcxjxhx wants to merge 1 commit into
OpenHands:mainfrom
lxcxjxhx:fix/grep-expand-brace-include
Closed

lxcxjxhx wants to merge 1 commit into
OpenHands:mainfrom
lxcxjxhx:fix/grep-expand-brace-include

Conversation

@lxcxjxhx

@lxcxjxhx lxcxjxhx commented Nov 9, 2026 •

Copy link
Copy Markdown

HUMAN:

AGENT:
The glob Python fallback got brace expansion in #6615; grep's include never did, so the same search returned files through ripgrep and nothing through the fallback.

GrepAction.include is documented with brace examples:

    include: str | None = Field(
        default=None,
        description=(
            "Optional file pattern to filter which files to search "
            '(e.g., "*.js", "*.{ts,tsx}")'
        ),
    )

_path_matches_filters matched the pattern with fnmatch.fnmatch(filename, include_pattern), which has no brace support, so *.{ts,tsx} matched no filename at all. On a machine with ripgrep the search works; on one without it the same call returns an empty result, which reads as "no matches" rather than "the filter cannot match anything".

Measured before the change, Python backend, three files containing the pattern:

include='*.ts'         -> 1 match(es): ['a.ts']
include='*.{ts,tsx}'   -> 0 match(es): []
include='*.{js,tsx}'   -> 0 match(es): []

and after:

include='*.ts'         -> 1 match(es): ['a.ts']
include='*.{ts,tsx}'   -> 2 match(es): ['a.ts', 'b.tsx']
include='*.{js,tsx}'   -> 2 match(es): ['b.tsx', 'c.js']

What changed

  • expand_braces() moves from openhands/tools/glob/impl.py to the shared openhands/tools/utils/ package, so both tools use one implementation instead of two. glob keeps its private name as an alias, which leaves its existing tests untouched.
  • grep's _path_matches_filters expands the pattern before matching, so include behaves the same on both backends.
  • Regression test test_python_backend_expands_brace_include_patterns forces the Python backend (both availability probes return False) and asserts *.{ts,tsx} finds a.ts and b.tsx and not c.js.

tests/tools/grep + tests/tools/glob: 83 passed, 32 skipped. ruff check and ruff format clean on the four touched files.

The include examples are brace patterns ("*.{ts,tsx}"), which ripgrep expands
and fnmatch does not, so the Python fallback returned an empty result for a
search ripgrep answers correctly. The expansion helper moves to the shared
tools utils so glob and grep use one implementation.
@lxcxjxhx

lxcxjxhx commented Nov 9, 2026

Copy link
Copy Markdown
Author

Parity check between the two backends, on a tree with a.yml, .env and .github/w.yml, all containing the search pattern. ripgrep 14.1.1 on this machine, so the comparison is real rather than read off the source:

include ripgrep Python backend
None ['a.yml'] ['a.yml']
.env ['.env'] ['.env']
*.yml ['a.yml'] ['a.yml']
*.{yml,env} ['.env', 'a.yml'] ['.env', 'a.yml']

Two things worth noting for review:

  • Before this change the last row was rg=['.env', 'a.yml'] and python=[], so the fallback silently disagreed with ripgrep on a documented input.
  • The hidden-file behaviour already agreed, and still does: ripgrep's -g does reach .env, and the fallback's include branch matches dotfiles while still skipping hidden directories (.github/w.yml is absent from both). So the brace expansion is the only difference that was left, and *.{yml,env} is the row that proves the fix lands in the same place ripgrep does, hidden files included.

Reproducer is the same four-call loop the PR describes; I can add it as a test that runs only when rg is on PATH if you would rather have the cross-backend assertion in CI than the single-backend one.

@lxcxjxhx

lxcxjxhx commented Nov 9, 2026

Copy link
Copy Markdown
Author

Closing: #6636 landed the same fix (fnmatch over _expand_braces in _path_matches_filters) together with a test for it, so nothing here is still needed.

For the record, the two things this branch had that #6636 does not: the helper moved to openhands/tools/utils so glob and grep share one implementation, and a test that forces the Python backend by making both availability probes return False. Neither is worth a PR on top of a fix that already works — say the word if you want the deduplication on its own and I will send it separately.

@lxcxjxhx lxcxjxhx closed this Nov 9, 2026
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.

1 participant