Skip to content

fix(search): match brace alternation and character classes in globs - #1111

Open
buguoshixc wants to merge 1 commit into
T-Auto:mainfrom
buguoshixc:fix/1110-glob-alternation-and-classes
Open

buguoshixc wants to merge 1 commit into
T-Auto:mainfrom
buguoshixc:fix/1110-glob-alternation-and-classes

Conversation

@buguoshixc

@buguoshixc buguoshixc commented Dec 4, 2026 •

Copy link
Copy Markdown

Closes #1110

What was wrong

ops_glob.pattern, ops_grep.glob and ops_replace.glob shared one matcher that compared bytes and recognised only ? and *: segment_match in native/crates/ops-search/src/lib.rs compared every other byte literally, and Patterns::new split on / without expanding anything. So {, ,, }, [, ] and \ were literal bytes that no file name contains — and the call reported success.

The issue's two reproductions on this machine before the change (a fixture with README.md, links.json, src/README.md, src/links.json, each containing the word links):

call before after
ops_glob {"pattern": ["**/{README*,links.json}"]} matchedTotal: 0, scanComplete: true matchedTotal: 4, same items as the split form
ops_glob {"pattern": ["**/README*", "**/links.json"]} matchedTotal: 4 matchedTotal: 4
ops_grep {"pattern": "links", "glob": ["**/{README.md,links.json}"], "outputMode": "summary"} files: 0; matches: 0 files: 4; matches: 5
ops_grep {"pattern": "links", "glob": ["**/README.md", "**/links.json"], "outputMode": "summary"} files: 4; matches: 5 files: 4; matches: 5

The receipt for the first row was scanComplete: true, matchedTotal: 0 and skipReasons.pattern — indistinguishable from "this repository has no such file", which is what made the bug costly.

The change

One matcher, compiled once per request, shared by all three entries. Nothing is compiled per file and nothing is allocated while matching.

File Why
native/crates/ops-search/src/glob.rs (new, 432 implementation lines) Brace expansion (nestable, a branch may carry /, empty branches allowed, a 256-alternative budget that aborts the expansion instead of computing it), segment compilation ([abc], [a-z], []], [!abc]/[^abc], \ escapes, **), and the byte matcher that lib.rs used to hold. A malformed entry becomes a Fault, which is the only thing that knows the field name and index.
native/crates/ops-search/src/patterns.rs Patterns now owns its compiled alternatives instead of borrowing &'a [String]; compile(field, patterns) -> Result<Self> replaces the infallible new; shallow() is judged on the expanded alternatives, so {*.md,src/*.ts} no longer prunes src/.
native/crates/ops-search/src/lib.rs Drops the old glob_segments/segment_match; files_for takes &Patterns; replace_with compiles before it walks.
native/crates/ops-search/src/scan.rs walk, walk_indexed, apply_batch and visit take &Patterns. The globCompile profile bucket is unchanged; it is recorded where the compile now happens (the entry), exactly once per request.
native/crates/ops-search/src/search.rs glob_with and grep_with compile before target(), before authorize_path and before any traversal; walk_request passes the compiled value down.
native/crates/ops-search/src/scan_tests.rs, candidates_tests.rs Call sites compile first. New assertion: a brace selection that skips a name still reports only skipReasons.pattern, with no new vocabulary.
src/capabilities/file.ts The three descriptions now state the syntax and that a malformed glob is INVALID_ARGUMENT, not an empty result.
test/search-coverage.test.mjs, test/linux-args.test.mjs, test/file-contract.test.mjs The end-to-end and contract assertions below.
docs/component-reference.md, docs/组件参考.md, docs/CHANGELOG.md Full syntax table, the byte-sized ?/class caveat, the refusal contract, and the Unreleased entry.

Behaviour changes worth stating in the changelog and in the docs:

  • {, [ and \ now have meaning. A pattern that used to match the literal text {a} has to say \{a\}.
  • A malformed pattern is refused in preflight as INVALID_ARGUMENT, naming the field and index (pattern[0], glob[1]) and carrying nextAction: Escape a literal "{", "}", "[", "]", "," or "\" with a backslash, or split the entry into separate array items.
    • pattern[0] has an unclosed "{" (escape a literal "{" as "\{")
    • pattern[1] has an unclosed "["
    • pattern[0] has an invalid character range "z-a"
    • pattern[2] ends with a dangling "\"
    • pattern[0] expands to more than 256 alternatives; split it into separate array items
    • pattern[0] would make the request expand to more than 512 alternatives; split it into separate array items (the request-wide ceiling)

Deliberately not done: ? and character classes still consume one byte, not one character (so non-ASCII names need *); a class is byte-based, not a Unicode property; an escaped / cannot match because names are split on / before a segment is matched; empty entries, empty segments and ./.. are left exactly as they are (that surface belongs to the in-flight #802); result fields, scanComplete/matchedTotal/matchedObserved, the skipReasons vocabulary, paging and continuation are untouched; no new dependency, no build/CI/manifest change, src/ptc/ops-tools.json (which carries no descriptions) was not regenerated.

Relationship to the in-flight #802

#802 (fix/797-unmatchable-glob-entries) is not merged into origin/main (checked: git merge-base --is-ancestor origin/fix/797-unmatchable-glob-entries origin/main fails; the branch is one commit ahead). It reserves the same seam — patterns.rs validation plus pattern[i] must be … messages raised from the glob, grep and replace entries.

This PR keeps a single validation entry point, Patterns::compile, and raises its own faults through one helper (Fault::failure) that already knows the field name and index. #802's first_invalid/EntryFault/invalid_message checks can run inside that entry (or immediately before it) and keep their message shape untouched; whoever merges second extends this one path rather than adding a second, parallel check. There is no other preflight for glob syntax anywhere in the tree.

Validation

  • docs/CHANGELOG.md ## [Unreleased] has one English and one Chinese entry
  • behaviour changes are in both references (component-reference.md, 组件参考.md)
  • no new dependency, no package.json version or optionalDependencies change
  • the three model-facing descriptions changed; test/file-contract.test.mjs pins what they now say (the field help stays under the 200-byte cap enforced there)
  • a pull_request-triggered CI required run exists but is parked at action_required — the first run from a fork waits for a maintainer to approve it. Its URL is below; it has no conclusion, and every number above is a local reading, not CI.
$ npm run build
build: bundled 11 browser modules (102620 bytes)
build: compiled src/ to lib/ and copied 60 assets

$ npm run build:ptc
win32-x64: 8144379 bytes, sha256 abdda75afb4ffa1d97b5d82b277dff0fcc4c4e79edc23ba70af8703829189834

$ npm run typecheck            # no output, exit 0

$ npm run check:size
check-file-size: 888 code file(s) checked, all at or under 1000 lines (longest: native/crates/ops-search/src/search.rs (939))

$ cargo test --workspace --locked --manifest-path native/Cargo.toml
# summed over every crate: 694 passed, 0 failed, 1 ignored
# (the ignored one is the UNC test, which needs DSH_OPS_UNC_TEST_ROOT)

$ cargo fmt --all --check --manifest-path native/Cargo.toml     # no output, exit 0
$ cargo clippy --workspace --all-targets --locked --manifest-path native/Cargo.toml   # 0 warnings; CI adds -D warnings

$ node test/run.mjs search-coverage
dsh-ops search-coverage: all 9 checks passed      # includes the two #1110 reproductions
$ node test/run.mjs linux-args
dsh-ops linux-args: all 14 checks passed          # includes the five malformed-pattern refusals
$ node test/run.mjs file-contract
dsh-ops file-contract: all 8 checks passed
$ node test/run.mjs grep
dsh-ops grep-definitions: all 4 checks passed
dsh-ops grep-multiline: all 6 checks passed
dsh-ops grep-pagination: all 4 checks passed
$ node test/run.mjs search-profiling
dsh-ops search-profiling: all 1 checks passed     # globCompile.calls is still exactly 1 per request

$ npm run verify
dsh-ops test: 5 of 295 suite(s) failed, 7 skipped in 111s   # 283 suites passed
$ gh run view 47379263624 --repo T-Auto/dsh-ops --json url,event,conclusion
{"conclusion":"action_required","event":"pull_request","url":"https://github.com/T-Auto/dsh-ops/actions/runs/47379263624"}

That is the required pull_request event for this branch. It is parked at action_required — the first run from a fork waits for a maintainer to approve it — so it has no conclusion and this PR is not claiming one. Everything quoted above was measured locally on the commit that was pushed.

All five failures are pre-existing on this host and reproduce identically on an untouched origin/main worktree (same commit 1caf4df2, built and run side by side): bash-ipc 3/8 and bash-ipc-roots 4/4 fail with Pinned Bash is unavailable … set DSH_OPS_TEST_BASH (no provisioned MSYS payload on this machine), linux-paths 1/8 and wsl-prepare-merge 1/3 fail on Windows path/WSL fixtures, and the seven skips need frozen archives or POSIX-only subjects. None of them touches ops-search, and none is searched by this change. Every suite this PR touches passes.

What fails without this change

  • glob::tests::braces_alternate_including_nested_and_empty_branches — revert the expansion and {a,b} is one literal segment again.
  • glob::tests::escapes_and_classes_are_literal_where_the_syntax_needs_them — revert class parsing or the escape arm and []], [!b], [a-z], \[, \{ stop selecting what they select.
  • glob::tests::malformed_patterns_name_the_entry_and_carry_advice — revert any fault mapping and the exact message, nextAction or code stops matching.
  • glob::tests::the_budget_stops_nested_expansion_before_it_explodes — remove the budget and 20 groups of two either hang or produce 1 048 576 alternatives instead of the refusal.
  • glob::tests::a_brace_inside_a_class_is_a_member_not_a_group — make the expander ignore classes and [{] becomes an alternation.
  • glob::tests::recursive_and_literal_double_star_stay_distinct — treat every * run as ** and the escaped form starts matching paths.
  • patterns::tests::compiled_patterns_match_original_inclusions_exclusions_and_path_segments — the old matcher is kept verbatim in the test as a reference over every pattern that carries no new syntax; any drift in ordinary matching turns it red.
  • patterns::tests::shallow_is_judged_on_expanded_alternatives — judge shallow() on the raw text again and {*.md,src/*.ts} claims one segment, so the walk prunes src/ and deep files silently disappear.
  • patterns::tests::compiles_once_and_reports_the_field_it_was_given — drop the field/index argument and glob[0] becomes pattern[0].
  • patterns::tests::the_request_budget_counts_every_entry_not_only_one — remove the 512 ceiling and three 256-alternative entries compile.
  • scan::scan_tests::brace_selections_still_report_only_the_existing_pattern_reason — invent a skip reason for expanded entries and both the count and the vocabulary assertion fail.
  • search-coverage “brace alternation, character classes and escapes select exactly what separate entries select” — this is the issue's own reproduction; before the change the first row is 0 !== 4.
  • linux-args “malformed glob syntax is refused in preflight instead of matching nothing” — before the change these five calls return completed with zero items; the test asserts the code, the exact message (with the glob:/grep:/replace: prefix dispatch adds), phase: "preflight", effectState: "none" and nextAction.
  • file-contract — the new assertions fail if any of the three descriptions loses the syntax list or the refusal clause.
  • search-profiling (existing) — move the compile out of the profiled bucket and globCompile.calls stops being 1.

Environment

  • Windows 11 x64 (10.0.26100), Git Bash (MSYS), Node v24.13.1, rustc/cargo 1.97.1
  • Upstream base: origin/main at 1caf4df2 (Merge pull request #1106), branch fix/1110-glob-alternation-and-classes
  • Install channel: npm ci --ignore-scripts from the npm registry, then npm run build / npm run build:ptc from this checkout
  • Two host quirks worth recording, because they change the readings above rather than the code: the toolchain lives on an ASCII path (D:/rust) because this account's profile path is non-ASCII and MinGW ld cannot open C:\Users\…\.rustup\… from a linker response file; and TEMP/TMP point at D:/tmp for the same reason, since fs.cpSync into C:\Users\…\AppData\Local\Temp fails with EIO, Access is denied for the packaging suites. With the system temp path, 12 suites fail for that reason alone; with an ASCII one, 5 do.

A glob entry now expands `{a,b}` (nestable, a branch may carry `/`), reads
`[abc]`, `[a-z]`, `[!abc]`/`[^abc]` and backslash-escaped bytes, and is
compiled once per request before any path is opened. A pattern that cannot be
parsed is refused as INVALID_ARGUMENT with the field, its index and a
nextAction, instead of being matched literally and reported as zero results.

The old matcher recognised only `?` and `*`, so `{`, `[` and `\` were literal
bytes no name contains: `**/{README*,links.json}` returned matchedTotal 0 with
scanComplete true while the same two globs as separate entries returned every
file.

Refs T-Auto#1110
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.

[BUG] ops_glob / ops_grep 的 glob 不支持 {a,b} 花括号,且静默返回 0 条(scanComplete=true),模型会误判文件不存在

1 participant