-
Notifications
You must be signed in to change notification settings - Fork 52.9k
feat(plugins): register_command(override=True) to shadow a built-in command #50054
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
arminanton
wants to merge
2
commits into
NousResearch:main
Choose a base branch
from
arminanton:feat/plugin-register-command-override
base: main
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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,64 @@ | ||
| """Behavioral coverage for plugin overrides through CLI dispatch.""" | ||
|
|
||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
| import yaml | ||
|
|
||
| from hermes_cli.plugins import PluginCommandOverrideError, PluginContext, PluginManager, PluginManifest | ||
|
|
||
|
|
||
| def _register_help_override(tmp_path, monkeypatch, *, granted: bool): | ||
| from hermes_cli import plugins as plugins_mod | ||
|
|
||
| home = tmp_path / "home" | ||
| home.mkdir() | ||
| entry = {"granted_capabilities": ["commands.override"]} if granted else {} | ||
| (home / "config.yaml").write_text( | ||
| yaml.safe_dump({"plugins": {"entries": {"help-plugin": entry}}}), encoding="utf-8" | ||
| ) | ||
| monkeypatch.setenv("HERMES_HOME", str(home)) | ||
| manager = PluginManager(scope_key=str(home)) | ||
| context = PluginContext( | ||
| PluginManifest(name="help-plugin", key="help-plugin", source="user"), manager | ||
| ) | ||
| handler = MagicMock(return_value="plugin help result") | ||
| if granted: | ||
| context.register_command("help", handler, override=True) | ||
| else: | ||
| with pytest.raises(PluginCommandOverrideError): | ||
| context.register_command("help", handler, override=True) | ||
| monkeypatch.setattr(plugins_mod, "_ensure_plugins_discovered", lambda: manager) | ||
| return handler | ||
|
|
||
|
|
||
| def _make_cli(): | ||
| import cli as cli_mod | ||
|
|
||
| instance = object.__new__(cli_mod.HermesCLI) | ||
| instance.session_id = "plugin-override-cli" | ||
| instance._pending_resume_sessions = None | ||
| return instance | ||
|
|
||
|
|
||
| def test_authorized_plugin_help_override_wins_cli_dispatch(tmp_path, monkeypatch, capsys): | ||
| handler = _register_help_override(tmp_path, monkeypatch, granted=True) | ||
| cli = _make_cli() | ||
| cli.show_help = MagicMock(side_effect=AssertionError("built-in /help ran")) | ||
|
|
||
| assert cli.process_command("/help raw arguments") is True | ||
|
|
||
| handler.assert_called_once_with("raw arguments") | ||
| cli.show_help.assert_not_called() | ||
| assert "plugin help result" in capsys.readouterr().out | ||
|
|
||
|
|
||
| def test_ungranted_plugin_cannot_replace_cli_help(tmp_path, monkeypatch): | ||
| handler = _register_help_override(tmp_path, monkeypatch, granted=False) | ||
| cli = _make_cli() | ||
| cli.show_help = MagicMock() | ||
|
|
||
| assert cli.process_command("/help") is True | ||
|
|
||
| handler.assert_not_called() | ||
| cli.show_help.assert_called_once() |
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,59 @@ | ||
| """Behavioral coverage for plugin overrides through gateway dispatch.""" | ||
|
|
||
| from unittest.mock import AsyncMock, MagicMock | ||
|
|
||
| import pytest | ||
| import yaml | ||
|
|
||
| from hermes_cli.plugins import PluginCommandOverrideError, PluginContext, PluginManager, PluginManifest | ||
| from tests.gateway.test_gateway_command_dispatch_minimal import _make_event, _make_runner | ||
|
|
||
|
|
||
| def _register_help_override(tmp_path, monkeypatch, *, granted: bool): | ||
| from hermes_cli import plugins as plugins_mod | ||
|
|
||
| home = tmp_path / "home" | ||
| home.mkdir() | ||
| entry = {"granted_capabilities": ["commands.override"]} if granted else {} | ||
| (home / "config.yaml").write_text( | ||
| yaml.safe_dump({"plugins": {"entries": {"help-plugin": entry}}}), encoding="utf-8" | ||
| ) | ||
| monkeypatch.setenv("HERMES_HOME", str(home)) | ||
| manager = PluginManager(scope_key=str(home)) | ||
| context = PluginContext( | ||
| PluginManifest(name="help-plugin", key="help-plugin", source="user"), manager | ||
| ) | ||
| handler = MagicMock(return_value="gateway plugin help") | ||
| if granted: | ||
| context.register_command("help", handler, override=True) | ||
| else: | ||
| with pytest.raises(PluginCommandOverrideError): | ||
| context.register_command("help", handler, override=True) | ||
| monkeypatch.setattr(plugins_mod, "_ensure_plugins_discovered", lambda: manager) | ||
| return handler | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_authorized_plugin_help_override_wins_gateway_dispatch(tmp_path, monkeypatch): | ||
| handler = _register_help_override(tmp_path, monkeypatch, granted=True) | ||
| runner, _adapter = _make_runner() | ||
| runner._handle_help_command = AsyncMock(side_effect=AssertionError("built-in /help ran")) | ||
|
|
||
| result = await runner._handle_message(_make_event("/help raw arguments")) | ||
|
|
||
| assert result == "gateway plugin help" | ||
| handler.assert_called_once_with("raw arguments") | ||
| runner._handle_help_command.assert_not_called() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_ungranted_plugin_cannot_replace_gateway_help(tmp_path, monkeypatch): | ||
| handler = _register_help_override(tmp_path, monkeypatch, granted=False) | ||
| runner, _adapter = _make_runner() | ||
| runner._handle_help_command = AsyncMock(return_value="built-in gateway help") | ||
|
|
||
| result = await runner._handle_message(_make_event("/help")) | ||
|
|
||
| assert result == "built-in gateway help" | ||
| handler.assert_not_called() | ||
| runner._handle_help_command.assert_called_once() |
Oops, something went wrong.
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.
override=Trueonly changes registration here. CLI and gateway dispatch built-ins before their plugin fallback (cli.py:8921-8985,gateway/run.py:10057-10072), while TUI checks plugins first (tui_gateway/server.py:11892-11901). Please add a shared, authorized resolution path and cross-surface tests before exposing this option.