-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fix(command): keep an argument that equals another alias of the same command #9294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
he-yufeng
wants to merge
2
commits into
AstrBotDevs:master
Choose a base branch
from
he-yufeng:fix/command-filter-alias-arg
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+57
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| from types import SimpleNamespace | ||
|
|
||
| from astrbot.core.star.filter.command import CommandFilter, GreedyStr | ||
|
|
||
|
|
||
| def _run_filter(command_name: str, alias: set, message: str): | ||
| cmd_filter = CommandFilter(command_name=command_name, alias=set(alias)) | ||
| # A single greedy parameter captures everything after the command name. | ||
| cmd_filter.handler_params = {"query": GreedyStr} | ||
| extras: dict = {} | ||
| event = SimpleNamespace( | ||
| is_at_or_wake_command=True, | ||
| get_message_str=lambda: message, | ||
| set_extra=lambda key, value: extras.__setitem__(key, value), | ||
| ) | ||
| ok = cmd_filter.filter(event, None) | ||
| return ok, extras.get("parsed_params") | ||
|
|
||
|
|
||
| def test_command_filter_keeps_argument_matching_an_alias(): | ||
| # Invoking a command by one name with a first argument that happens to equal | ||
| # another alias of the same command must not strip that argument a second time. | ||
| ok, params = _run_filter("search", {"find"}, "search find keyword") | ||
| assert ok | ||
| assert params == {"query": "find keyword"} | ||
|
|
||
|
|
||
| def test_command_filter_keeps_sole_argument_matching_an_alias(): | ||
| ok, params = _run_filter("add", {"new"}, "add new") | ||
| assert ok | ||
| assert params == {"query": "new"} | ||
|
|
||
|
|
||
| def test_command_filter_normal_argument_unaffected(): | ||
| ok, params = _run_filter("search", {"find"}, "search cat photo") | ||
| assert ok | ||
| assert params == {"query": "cat photo"} | ||
|
|
||
|
|
||
| def test_command_filter_prefers_longest_overlapping_command_name(): | ||
| # When a command name and one of its aliases share a prefix ("show" vs | ||
| # "show all"), the most specific one must win regardless of the set's | ||
| # iteration order, so only the longer name is stripped and the rest is the | ||
| # argument. Without the longest-first ordering, "show" would match first and | ||
| # leak "all" into the argument. | ||
| ok, params = _run_filter("show", {"show all"}, "show all photos") | ||
| assert ok | ||
| assert params == {"query": "photos"} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the introduction of
break, the matching process now stops at the first match. This makes the iteration order ofget_complete_command_names()extremely critical.Currently,
get_complete_command_names()constructs the list usinglist(self.alias). Sinceself.aliasis aset, its iteration order is non-deterministic in Python due to hash randomization. If a command has overlapping aliases (e.g., one alias is a prefix of another, such as"show"and"show all"), the matched command will be non-deterministic across different runs or environments.To ensure deterministic matching and that the most specific (longest) command/alias is matched first, the list of complete command names should be sorted by length in descending order. Since
get_complete_command_namesis outside the modified diff hunk, please update it inastrbot/core/star/filter/command.pyas follows:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — the
breakdid make the candidate order decide the winner. I fixed it in ab98891 by matching longest-first in the loop (sorted(..., key=lambda name: (-len(name), name))), so the most specific of any prefix-sharing names (e.g. an aliasshowvsshow all) always wins deterministically, independent of the set's iteration order.I deliberately sorted in the match loop rather than in
get_complete_command_names()itself: a few dashboard callers (plugin_service.py:828/862) rely onget_complete_command_names()[0]being the primary command name, which a length sort there would break. Addedtest_command_filter_prefers_longest_overlapping_command_nameto pin it — it fails on the unsorted order and passes with the fix.