Skip to content

feat(wrapper): add <opts> and <vars> placeholders for wrapper patterns#84

Merged
fohte merged 12 commits intomainfrom
fohte/fix-wrapper-wildcard-greedy
Feb 24, 2026
Merged

feat(wrapper): add <opts> and <vars> placeholders for wrapper patterns#84
fohte merged 12 commits intomainfrom
fohte/fix-wrapper-wildcard-greedy

Conversation

@fohte
Copy link
Owner

@fohte fohte commented Feb 23, 2026

Why

  • * <cmd> combinations in wrapper patterns (e.g., xargs * <cmd>) cause * to consume 0 tokens, leaving <cmd> to capture the entire remaining input including flags that are then misinterpreted as the command name
    • e.g., xargs -I{} echo hello results in <cmd> capturing -I{} echo hello, evaluating -I{} as the command name
  • The * + <cmd> combination is fundamentally ambiguous — even trying all candidates produces false matches under defaults.action settings
  • Similarly, env * <cmd> has no way to distinguish FOO=bar variable assignment tokens from flags

What

  • Add <opts> placeholder for wrapper patterns to deterministically consume hyphen-prefixed flag tokens
    • Short single-char flags (e.g., -n) consume the next token as their argument, while multi-char flags (-I{}, --verbose) are treated as self-contained
  • Add <vars> placeholder for wrapper patterns to deterministically consume KEY=VALUE tokens
    • Combined with <opts> as env <opts> <vars> <cmd>, this correctly parses commands like env -i FOO=bar echo hello
  • Change extract_placeholder to collect all candidates so * <cmd> patterns still work for simpler cases

…er patterns

When a wrapper pattern like `xargs * <cmd>` matched `xargs -I{} echo hello`,
the wildcard `*` consumed 0 tokens (minimum match) and `<cmd>` captured
`-I{} echo hello` as a whole, causing the inner command to be evaluated as
`-I{}` (not a real command) and resulting in "No rules matched".

The root cause was that `extract_placeholder_inner` returned on the first
successful alignment without considering whether the captured command would
actually match any rules. Change the extractor to collect all valid capture
candidates, then let `try_unwrap_wrapper` evaluate each candidate and pick
the one with the highest action priority (Deny > Ask > Allow > Default).
@gemini-code-assist
Copy link

Summary of Changes

Hello @fohte, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical flaw in how wrapper patterns handle the interaction between wildcards and command placeholders. By modifying the pattern matching logic to identify all possible interpretations of the wrapped command and then using the rule engine to prioritize the most relevant one, the system can now correctly parse and apply rules to complex commands like xargs -I{} echo hello, preventing misinterpretations and improving the robustness of command evaluation.

Highlights

  • Ambiguous Wrapper Pattern Resolution: Resolved an issue where wrapper patterns combining wildcards and placeholders (e.g., 'xargs * ') could lead to ambiguous command captures, as the wildcard previously consumed zero tokens, causing the placeholder to capture the entire remaining input.
  • Multiple Candidate Capture: The extract_placeholder function was refactored to return all possible token sequences that could be captured by the <cmd> placeholder, rather than just the first successful match.
  • Action Priority-Based Selection: The rule engine now evaluates each of these potential <cmd> capture candidates and selects the one with the highest action priority, ensuring the most appropriate command interpretation.
Changelog
  • src/rules/pattern_matcher.rs
    • Refactored extract_placeholder to return a vector of all possible <cmd> placeholder captures instead of a single optional capture.
    • Renamed extract_placeholder_inner to extract_placeholder_all and updated its recursive logic to explore all valid token alignments and collect all successful captures.
    • Adjusted the matching logic for Placeholder, Literal, Alternation, FlagWithValue, Wildcard, and Negation tokens to support the new all-candidate collection approach.
  • src/rules/rule_engine.rs
    • Modified try_unwrap_wrapper to process all candidate <cmd> captures returned by extract_placeholder.
    • Implemented logic to evaluate each candidate capture and select the one that yields the highest action priority.
  • tests/integration/wrapper_recursive_evaluation.rs
    • Added new integration tests to validate the correct behavior of wildcard-with-placeholder wrappers, specifically ensuring greedy wildcard consumption of flags is handled properly.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 5 additional findings.

Open in Devin Review

gemini-code-assist[bot]

This comment was marked as resolved.

- Pass named `captured` variable instead of inline `&mut Vec::new()`
- Add comment explaining why empty captures are filtered (patterns
  without `<cmd>` placeholder produce empty captures on match)
- Add comment on non-`<cmd>` placeholder behavior
- Improve `extract_placeholder_all` doc comment
@codecov
Copy link

codecov bot commented Feb 23, 2026

Codecov Report

❌ Patch coverage is 92.99363% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.90%. Comparing base (2fdea25) to head (a9dd560).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/rules/pattern_matcher.rs 92.36% 10 Missing ⚠️
src/rules/rule_engine.rs 95.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #84      +/-   ##
==========================================
+ Coverage   89.09%   89.90%   +0.80%     
==========================================
  Files          29       29              
  Lines        6025     6129     +104     
==========================================
+ Hits         5368     5510     +142     
+ Misses        657      619      -38     
Flag Coverage Δ
Linux 89.74% <92.99%> (+0.80%) ⬆️
macOS 91.89% <92.99%> (+0.80%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

The `* <cmd>` combination in wrapper patterns is fundamentally ambiguous
because `*` greedily consumes tokens, making it impossible to
deterministically separate flags from the inner command. `<opts>` solves
this by consuming only hyphen-prefixed flag tokens, leaving the rest for
`<cmd>`.

`consume_opts` treats short single-char flags (e.g., `-n`) as
potentially taking a separate argument, while multi-char flags
(`-I{}`, `-0`, `--verbose`) are self-contained.
@fohte fohte changed the title fix(wrapper): resolve ambiguous wildcard-placeholder capture in wrapper patterns feat(wrapper): add <opts> placeholder for deterministic flag consumption in wrapper patterns Feb 24, 2026
Gemini review pointed out that the ordering of candidates (shortest to
longest) was not documented. This ordering matters for the rule engine's
priority-based selection.
devin-ai-integration[bot]

This comment was marked as resolved.

…r_all

Empty captures (from patterns without `<cmd>` or where `<cmd>` matched
nothing) were previously filtered out at the call site. Move the guard
into `extract_placeholder_all` itself so invalid candidates are never
produced, making the logic easier to follow.
Cover previously untested code paths in `extract_placeholder_all`:
Literal, Alternation, FlagWithValue, Negation, Wildcard branches, the
base-case candidate push when `captured` is non-empty, non-`<cmd>`
trailing placeholders, and `<cmd>` in middle position followed by a
literal sentinel. Also cover `<opts>` in non-wrapper pattern matching
context.

Patch coverage improved from 63% to 93%.
devin-ai-integration[bot]

This comment was marked as resolved.

`env <opts> <cmd>` cannot deterministically separate `FOO=bar` variable
assignments from the inner command because `<opts>` only consumes
hyphen-prefixed flags. `<vars>` consumes consecutive tokens containing
`=`, allowing wrapper patterns like `env <opts> <vars> <cmd>` to
unambiguously parse `env -i FOO=bar echo hello`.

Also update existing `env * <cmd>` test to use `env <opts> <vars> <cmd>`
since the new placeholders provide deterministic parsing.
@fohte fohte changed the title feat(wrapper): add <opts> placeholder for deterministic flag consumption in wrapper patterns feat(wrapper): add <opts> and <vars> placeholders for wrapper patterns Feb 24, 2026
`consume_opts` incorrectly treated `-0` as a short flag that consumes
the next token as its argument. Since `-0` is a self-contained boolean
flag (like `xargs -0`), only flags whose second character is an ASCII
letter should consume a separate argument.

Also, `--` (POSIX end-of-options marker) was not handled; it was
consumed as a regular flag. Now `--` terminates option scanning and
is itself consumed so it does not leak into `<cmd>`.
devin-ai-integration[bot]

This comment was marked as resolved.

…ume_opts`

The doc comment for `consume_opts` was placed directly above
`consume_vars`, causing Rust to attach it to the wrong function.
Move it to its intended location and update the wording to reflect
the ASCII-letter check and `--` handling.
…dcard-greedy

# Conflicts:
#	src/rules/pattern_matcher.rs
#	src/rules/rule_engine.rs
@fohte fohte merged commit 0d41c66 into main Feb 24, 2026
6 checks passed
@fohte fohte deleted the fohte/fix-wrapper-wildcard-greedy branch February 24, 2026 16:52
@fohte-bot fohte-bot bot mentioned this pull request Feb 24, 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