Skip to content

Fix narrowing for concatenated alternations - #230

Merged
Shengyu Fu (shengyfu) merged 4 commits into
mainfrom
shengyfu-regex-alternation-narrowing
Nov 25, 2026
Merged

Shengyu Fu (shengyfu) merged 4 commits into
mainfrom
shengyfu-regex-alternation-narrowing

Conversation

@shengyfu

@shengyfu Shengyu Fu (shengyfu) commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 8d996c95291850e8500ca1156c321fd4c21c4d3b

Dimension Assessment Rationale
Change confidence 9/10 ↑ CI passes on Linux, macOS and Windows; query regressions cover nested-capture short-circuiting and scan parity.
↓ macOS initially hit an intermittent managed collection race; its rerun passed.
Diagnosis confidence 10/10 ↑ The pre-fix regression reproduced prefix-only candidates; concat planning dropped OR children, and follow-up review identified literal-order regressions after flattening nested captures.
Evidence fit 9/10 ↑ The issue's exact regex now returns two indexed candidates; regression tests confirm no-hit patterns skip alternation lookups even when literals are nested in captures.
↓ The original 700k-file monorepo was not available.

Summary

  • Preserve alternation constraints in concatenations using nested conjunctions without distributing branch combinations.
  • Evaluate literal constraints first, including after flattening nested captures, so no-hit searches short-circuit before fetching alternation postings.
  • Cover query planning and indexed, server and scan behavior; update query-plan diagnostics.

Closes #229

Validation

  • cargo test --locked -p tgrep-core --lib query::tests
  • cargo test --locked -p tgrep-cli --test ripgrep_compat indexed_ -- --test-threads=2
  • cargo test --locked -p tgrep-cli --test max_results
  • cargo test --locked -p tgrep-cli --test ripgrep_compat match_context -- --test-threads=2
  • cargo fmt --all -- --check
  • cargo metadata --locked --format-version 1 --no-deps --manifest-path fuzz\Cargo.toml

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 02:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Evaluating alternation branches before the literal conjunction can unnecessarily materialize large posting lists and slow no-hit searches that could otherwise short-circuit.

1 open issue
What changed in this PR

Addresses #229 by preserving alternation constraints in concatenated regex query plans, reducing unnecessary candidate reads without expanding branch combinations.

Changes:

  • Adds nested conjunction planning and execution, with updated query-plan diagnostics.
  • Adds planner and backend regressions for candidate selectivity and scan parity.
  • Documents the behavior and bumps the release version to 1.2.9.
File Description
tgrep-core/​src/​query.rs Adds nested conjunctions, candidate intersection, and planner regressions.
tgrep-cli/​tests/​ripgrep_compat.rs Tests selectivity and parity across scan, indexed, and server backends.
tgrep-cli/​src/​search.rs Displays nested conjunctions in plan summaries.
README.md Explains narrowing for concatenated alternations.
fuzz/​Cargo.lock Updates the core package version to 1.2.9.
CONTRIBUTING.md Updates release-tag examples.
Cargo.toml Bumps the workspace version to 1.2.9.
Cargo.lock Updates CLI and core package versions.

Review effort: Balanced

Comment thread tgrep-core/src/query.rs Resolved
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 02:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Flattening nested conjunctions can undo literal-first ordering, causing both executors to fetch and union common postings before detecting a missing literal; ordering must be restored after flattening and the nested-capture case added to regression coverage.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Prioritize literal-first ordering after flattening nested conjunctions

tgrep-core/​src/​query.rs:563

Flattening nested conjunctions can undo literal-first ordering: (the|and)(QZX(foo|bar)) produces All([Or(the,and), And(QZX), Or(foo,bar)]). Even when QZX has no postings, both executors fetch and union the common the/and postings before discovering the miss, so the nested-capture case still regresses. Prioritize And children after flattening, and add this pattern to the short-circuit regression.

1 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Nested captures can place mandatory literals after alternations, causing both executors to fetch and union common-term postings unnecessarily even when the mandatory literal has no hits.

1 open issue

Review effort: Balanced

Comment thread tgrep-core/src/query.rs Resolved
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 06:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block approval of the query-narrowing change.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 07:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The concatenated-alternation query-narrowing changes have no active review issues.

No open issues 🎉

Review effort: Balanced

@shengyfu
Shengyu Fu (shengyfu) merged commit ca8a344 into main Nov 25, 2026
24 of 25 checks passed
@shengyfu
Shengyu Fu (shengyfu) deleted the shengyfu-regex-alternation-narrowing branch November 25, 2026 17:14
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.

A literal followed by an alternation narrows only on the literal: React\.(useImperativeHandle|useReducer) reads 45k files instead of ~230

2 participants