Skip to content

The agent that grows with you

fix(plugins): never pass a context keyword that names the raw-args parameter

Teknium9cfcfcc8d409
diff --git a/hermes_cli/plugin_command_dispatch.py b/hermes_cli/plugin_command_dispatch.py
index d0be9362520..c550908337f 100644
--- a/hermes_cli/plugin_command_dispatch.py
+++ b/hermes_cli/plugin_command_dispatch.py
@@ -20,10 +20,12 @@ def invoke_plugin_command(handler: Callable, raw_args: str, **context: Any) -> A
parameters = list(inspect.signature(handler).parameters.values())
except (TypeError, ValueError):
return handler(raw_args)
- if any(p.kind is inspect.Parameter.VAR_KEYWORD for p in parameters):
- return handler(raw_args, **context)
- # The first positional parameter receives raw_args whatever its name.
+ # The first positional parameter receives raw_args whatever its name (``def h(source, **kw)``
+ # must not get ``source`` twice).
if parameters and parameters[0].kind is not inspect.Parameter.KEYWORD_ONLY:
+ context = {k: v for k, v in context.items() if k != parameters[0].name}
parameters = parameters[1:]
+ if any(p.kind is inspect.Parameter.VAR_KEYWORD for p in parameters):
+ return handler(raw_args, **context)
accepted = {p.name for p in parameters if p.kind in _KEYWORD_KINDS}
return handler(raw_args, **{k: v for k, v in context.items() if k in accepted})
diff --git a/tests/hermes_cli/test_plugin_command_source.py b/tests/hermes_cli/test_plugin_command_source.py
index f266db25106..2b5ff062ce5 100644
--- a/tests/hermes_cli/test_plugin_command_source.py
+++ b/tests/hermes_cli/test_plugin_command_source.py
@@ -18,7 +18,11 @@ def test_handlers_get_only_the_context_they_declare():
def positional_only(raw_args, session_id=None, /): # cannot be passed by keyword: no opt-in
return raw_args, session_id
+ def first_named_source(source, **kwargs): # raw_args fills `source`; it is never passed twice
+ return source, sorted(kwargs)
+
assert invoke_plugin_command(legacy, "a", **CONTEXT) == ("a",)
assert invoke_plugin_command(named, "a", **CONTEXT) == ("a", "sid", "telegram")
assert invoke_plugin_command(catch_all, "a", **CONTEXT) == ("a", CONTEXT)
assert invoke_plugin_command(positional_only, "a", **CONTEXT) == ("a", None)
+ assert invoke_plugin_command(first_named_source, "a", **CONTEXT) == ("a", ["platform", "session_id", "session_key"])