Eason09053360 opened a new pull request, #72941:
URL: https://github.com/apache/airflow/pull/72941

   `airflow tasks clear --task-regex` matches task ids as a regular expression 
again, as its name, help text and docs promise. An invalid pattern now exits 
with a one-line error instead of a traceback.
   
   ## Why
   
   The `-t/--task-regex` flag of `airflow tasks clear` is documented as "The 
regex to filter specific task_ids", and the Dag run docs show it used as a 
regex. Since #47360 it has silently been a plain substring match.
   
   That PR removed regex support from `partial_subset` so that the REST API 
`root` parameter could no longer be parsed as a regex (ReDoS concern raised on 
the dev list). The CLI call site was only renamed from `task_ids_or_regex` to 
`task_ids`, so the raw pattern string kept flowing into `partial_subset`, whose 
`str` branch does `task_ids in t.task_id`. Any pattern with metacharacters 
matched nothing:
   
       airflow tasks clear example_bash_operator -t '^runme_[02]$' -y
   
   cleared zero task instances and printed nothing, while the user expected 
`runme_0` and `runme_2` to be cleared. A pattern like `runme_0|also_run_this` 
behaved the same way. The sibling `--dag-regex` flag still uses `re.search`, so 
the two flags on the same command had different semantics.
   
   ## What
   
   The regex is resolved in the CLI, the only `partial_subset` caller that 
promises one. `task_clear` compiles the pattern once, filters `dag.task_ids` 
with `re.search` (unanchored, same as `--dag-regex`), and passes the resulting 
set of task ids to `partial_subset`, which then takes its exact-match branch. 
`partial_subset` itself is untouched, so the UI and REST API callers that rely 
on its current non-regex contract are unaffected.
   
   A pattern that fails to compile now raises `SystemExit` with `Invalid 
--task-regex '<pattern>': <reason>` before any Dag is touched, following the 
`dags test --conf` idiom in this CLI. Previously the substring path accepted 
any string, so this is the only new failure mode introduced here.
   
   Deliberately left out of this PR:
   
   - `--dag-regex` combined with `--task-regex` still crashes in `clear_dags` 
because `get_dags(use_regex=True)` ignores `from_db`; that is tracked by #66884.
   - A pattern that matches no task still exits 0 without output, as the 
substring path did. Turning that into an error changes the exit-code contract, 
so it is raised here as a question rather than changed.
   - The `partial_subset` docstring in the Task SDK still says "based on regex 
matching"; fixing that wording is a separate small change.
   - An unescaped `.` in a pattern now matches any character, e.g. `group.load` 
also matches `group_load`. That is what the flag documents and what Airflow 2 
did; whether a newsfragment is wanted for restoring it is a maintainer call.
   
   ## How to test
   
   Added to `airflow-core/tests/unit/cli/commands/test_task_command.py` in 
`TestCliTasks`:
   
   - `test_task_clear_task_regex_matches_as_regex`, parametrized over 
`^runme_[02]$`, `unme_[02]` and `runme_0|also_run_this`. Each pattern matches 
nothing under substring, `re.match` or `re.fullmatch` semantics and only passes 
with `re.search`.
   - `test_task_clear_task_regex_invalid_pattern_exits_cleanly`, asserting 
`SystemExit` with the new message and that `clear_dags` is never called.
   
   Run with:
   
       uv run --project airflow-core pytest 
airflow-core/tests/unit/cli/commands/test_task_command.py -k task_clear -xvs
   
   Verified: all four cases pass with the fix and all four fail with the 
`task_command.py` change reverted (whole file run both ways, 26 passed with the 
fix). `prek run --files` on both files and `prek run mypy-airflow-core --files` 
pass.
   
   Manual reproduction (not run by the author; expected behaviour follows from 
the tests), with the example Dags loaded and serialized:
   
       airflow tasks clear example_bash_operator -t '^runme_[02]$'
   
   Expected before: no prompt, nothing cleared. Expected after: the 
confirmation prompt lists the `runme_0` and `runme_2` task instances.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes — Claude Code (Fable 5.1)
   
   Generated-by: Claude Code (Fable 5.1) following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to