Skip to content

The agent that grows with you

fix(delegation): a hosted plugin switches the owner's backends (ctx.delegation_backends)

Teknium0c5377318a26
diff --git a/agent/delegation_backends.py b/agent/delegation_backends.py
index e19ac9dec3a..80e21b96e53 100644
--- a/agent/delegation_backends.py
+++ b/agent/delegation_backends.py
@@ -15,7 +15,7 @@ session context, else from the parent agent's session id.
from __future__ import annotations
import threading
-from dataclasses import dataclass
+from dataclasses import asdict, dataclass
from typing import Any, Mapping, Optional
from agent.delegation_registry import DEFAULT_BACKEND, _registry, backend_status, configured_backend
@@ -108,3 +108,21 @@ def session_delegation_config(config: Mapping[str, Any], parent_agent: Any) -> M
if override is None or override == configured_backend(config):
return config
return {**config, "backend": override}
+
+
+class DelegationBackends:
+ """``ctx.delegation_backends``: this API for plugins, as plain data. It is a host remote facade, so a plugin
+ running under ``plugins.isolation: host`` lists and switches the owner process's backends and sessions, not
+ copies in its own process."""
+
+ def list_backends(self) -> list[dict[str, Any]]:
+ return [asdict(info) for info in list_backends()]
+
+ def get_session_backend(self, session_key: str) -> Optional[str]:
+ return get_session_backend(session_key)
+
+ def set_session_backend(self, session_key: str, name: Optional[str]) -> None:
+ set_session_backend(session_key, name)
+
+ def set_global_backend(self, name: str, session_key: str = "") -> None:
+ set_global_backend(name, session_key=session_key)
diff --git a/hermes_cli/plugin_isolation.py b/hermes_cli/plugin_isolation.py
index e8d10ca251a..1aa406e9972 100644
--- a/hermes_cli/plugin_isolation.py
+++ b/hermes_cli/plugin_isolation.py
@@ -50,7 +50,9 @@ HOST_SKIPPED_CTX_METHODS: dict[str, str] = {
}
# Live ctx facades the host reaches by method call; each call returns plain data.
-HOST_REMOTE_FACADES: frozenset[str] = frozenset({"state", "llm", "platform_actions", "subagent_lifecycle"})
+HOST_REMOTE_FACADES: frozenset[str] = frozenset({
+ "state", "llm", "platform_actions", "subagent_lifecycle", "delegation_backends",
+})
# Object-taking ctx methods and the ABC the parent checks. The host sends the object's method table
# and the parent builds a subclass of that ABC whose methods run in the host.
diff --git a/hermes_cli/plugins.py b/hermes_cli/plugins.py
index 667eda53d8a..14e13ab00e3 100644
--- a/hermes_cli/plugins.py
+++ b/hermes_cli/plugins.py
@@ -389,6 +389,13 @@ class PluginContext:
from agent.subagent_lifecycle import SubagentLifecycleService, get_active_subagent_parent
return SubagentLifecycleService(get_active_subagent_parent)
+ @cached_property
+ def delegation_backends(self) -> Any:
+ """``agent.delegation_backends.DelegationBackends``: list delegation backends and switch them per session or
+ globally (a ``/delegation`` command); a host remote facade, so it acts on the owner process."""
+ from agent.delegation_backends import DelegationBackends
+ return DelegationBackends()
+
@property
def profile_name(self) -> str:
"""Active profile name (``"default"``, the ``~/.hermes/profiles/<name>`` id, or ``"custom"``),
diff --git a/tests/hermes_cli/test_plugin_host_delegation.py b/tests/hermes_cli/test_plugin_host_delegation.py
index 40208cb693c..54b49a3064c 100644
--- a/tests/hermes_cli/test_plugin_host_delegation.py
+++ b/tests/hermes_cli/test_plugin_host_delegation.py
@@ -29,6 +29,12 @@ class Hosted(DelegationProvider):
def register(ctx):
ctx.register_delegation_provider(Hosted())
+
+ def switch(raw_args, session_key=""):
+ names = [b["name"] for b in ctx.delegation_backends.list_backends()]
+ ctx.delegation_backends.set_session_backend(session_key, raw_args.strip())
+ return f"{os.getpid()} {','.join(names)} -> {ctx.delegation_backends.get_session_backend(session_key)}"
+ ctx.register_command("deleg-switch", switch)
'''
@@ -69,6 +75,16 @@ def test_hosted_delegation_provider_extends_schema_and_routes_from_the_host(tmp_
refused, err = resolve_task_routes([{"goal": "refused by the hosted plugin", "option": "b"}], config, None)
assert err is None and "b is closed" in refused[0].error
assert json.dumps(params) == json.dumps(schema_extension(config)[0])
+
+ # The /delegation seam acts on THIS process's sessions (ctx.delegation_backends is a host remote facade).
+ from agent.delegation_backends import get_session_backend, set_session_backend
+ from hermes_cli.plugin_command_dispatch import invoke_plugin_command
+ out = invoke_plugin_command(plugins_mod.get_plugin_command_handler("deleg-switch"), "default",
+ session_key="chat-a")
+ pid, listed = out.split(" ", 1)
+ assert int(pid) != os.getpid() and listed == "default,hosted-map -> default"
+ assert get_session_backend("chat-a") == "default"
+ set_session_backend("chat-a", None)
finally:
manager.unload()
manager._plugin_host().shutdown()