The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(delegation): a first-use default never outlives plugins.disabled
Teknium4eb1ed7c4240
diff --git a/agent/delegation_registry.py b/agent/delegation_registry.pyindex f1313d8bc0d..12cc9294abd 100644--- a/agent/delegation_registry.py+++ b/agent/delegation_registry.py@@ -38,24 +38,38 @@ def configured_backend(config: Mapping[str, Any]) -> str:return lower_key(raw) if isinstance(raw, str) and raw.strip() else DEFAULT_BACKEND+_first_use: dict[str, DelegationProvider] = {}++class _BundledRegistrar:- """The one ``ctx`` method a bundled strategy's ``register()`` calls."""+ """The one ``ctx`` method a bundled strategy's ``register()`` calls on first use. It keeps the provider+ outside the plugin registry, whose entries only the plugin manager adds and removes."""++ def __init__(self, name: str) -> None:+ self.name = namedef register_delegation_provider(self, provider: DelegationProvider) -> None:- _registry.register(provider)+ _first_use[self.name] = provider+++def _bundled_disabled(name: str) -> bool:+ from hermes_cli.plugins_discovery import _get_disabled_plugins+ return bool({name, f"delegation/{name}"} & _get_disabled_plugins())def _load_bundled(name: str) -> Optional[DelegationProvider]:- """Load the bundled strategy ``plugins/delegation/<name>/`` on first use when the plugin sweep has- not registered it (a runtime that never ran discovery, or pinned it with zero plugins): delegation- is a core tool and must not depend on the sweep. ``plugins.disabled`` still wins."""+ """The bundled strategy ``plugins/delegation/<name>/`` on first use when the plugin sweep has not+ registered it (a runtime that never ran discovery, or pinned it with zero plugins): delegation is a+ core tool and must not depend on the sweep. ``plugins.disabled`` wins on every call, so a later+ disable takes effect without a restart."""import importlib.utilfrom hermes_cli.plugins import get_bundled_plugins_dir- from hermes_cli.plugins_discovery import _get_disabled_plugins- if not _BUNDLED_NAME_RE.match(name): # the name comes from config.yaml: never a path+ if not _BUNDLED_NAME_RE.match(name) or _bundled_disabled(name): # the name comes from config.yaml: never a pathreturn None+ if name in _first_use:+ return _first_use[name]plugin_dir = get_bundled_plugins_dir() / "delegation" / name- if not (plugin_dir / "plugin.yaml").is_file() or {name, f"delegation/{name}"} & _get_disabled_plugins():+ if not (plugin_dir / "plugin.yaml").is_file():return Nonespec = importlib.util.spec_from_file_location(f"hermes_bundled_delegation_{name.replace('-', '_')}", plugin_dir / "__init__.py",@@ -64,8 +78,8 @@ def _load_bundled(name: str) -> Optional[DelegationProvider]:return Nonemodule = importlib.util.module_from_spec(spec)spec.loader.exec_module(module)- module.register(_BundledRegistrar())- return _registry.get_provider(name)+ module.register(_BundledRegistrar(name))+ return _first_use.get(name)def _usable(name: str) -> Optional[DelegationProvider]:diff --git a/tests/tools/test_delegation_backends.py b/tests/tools/test_delegation_backends.pyindex 5c4c999d166..e9035dcd91f 100644--- a/tests/tools/test_delegation_backends.py+++ b/tests/tools/test_delegation_backends.py@@ -422,9 +422,12 @@ def test_default_strategy_is_the_bundled_plugin_through_the_same_seam(tmp_path,def test_runtime_with_zero_plugins_loaded_still_delegates_on_the_bundled_default(tmp_path, monkeypatch, recorded):"""Delegation is a core tool: a runtime whose plugin sweep never ran (or was pinned empty) loads thebundled default strategy on first use instead of losing delegate_task."""- from agent.delegation_registry import _load_bundled, _registry+ from agent import delegation_registry+ from agent.delegation_registry import _load_bundled, _registry, get_active_providerfrom tools.delegate_tool import delegate_task+ monkeypatch.setattr(delegation_registry, "_first_use", {})+home = tmp_path / ".hermes"home.mkdir(parents=True)(home / "config.yaml").write_text(yaml.safe_dump({"delegation": {"model": "pinned-model"}}), encoding="utf-8")@@ -438,9 +441,63 @@ def test_runtime_with_zero_plugins_loaded_still_delegates_on_the_bundled_defaulttry:delegate_task(tasks=[{"goal": "one plain delegated task"}], parent_agent=_parent())assert recorded[-1]["model"] == "pinned-model"- assert _registry.get_provider("default").name == "default"+ assert get_active_provider({}).name == "default"assert _load_bundled("../../tools") is None # config text never becomes a pathfinally:current = _registry.snapshot_registration("default")if current is not None:_registry.restore_registration("default", current, previous)+++def test_first_use_default_never_outlives_a_later_disable(tmp_path, monkeypatch, recorded):+ """The first-use load of the bundled default is not a second, permanent registration: once+ ``delegation/default`` is disabled, the next delegation refuses even in the runtime that loaded it."""+ from agent import delegation_registry+ from agent.delegation_registry import _registry+ from tools.delegate_tool import delegate_task++ monkeypatch.setattr(delegation_registry, "_first_use", {})+ home = tmp_path / ".hermes"+ home.mkdir(parents=True)+ config = home / "config.yaml"+ config.write_text(yaml.safe_dump({"delegation": {"model": "pinned-model"}}), encoding="utf-8")+ monkeypatch.setenv("HERMES_HOME", str(home))+ pinned = PluginManager()+ pinned._discovered = True # zero plugins, discovery never runs+ monkeypatch.setattr(plugins_mod, "_plugin_manager", pinned)+ previous = _registry.snapshot_registration("default")+ if previous is not None:+ _registry.restore_registration("default", previous, None)+ try:+ delegate_task(tasks=[{"goal": "one plain delegated task"}], parent_agent=_parent())+ assert recorded[-1]["model"] == "pinned-model"+ config.write_text(yaml.safe_dump({"plugins": {"disabled": ["delegation/default"]},+ "delegation": {"model": "pinned-model"}}), encoding="utf-8")+ out = json.loads(delegate_task(tasks=[{"goal": "one plain delegated task"}], parent_agent=_parent()))+ assert "delegation/default is disabled" in out.get("error", "")+ assert len(recorded) == 1+ finally:+ current = _registry.snapshot_registration("default")+ if current is not None:+ _registry.restore_registration("default", current, previous)+++def test_a_replacement_default_strategy_gets_the_full_route_contract(monkeypatch):+ """Rules follow the route, not the name: a provider registered as ``default`` that returns a speed tier and+ a label has the tier checked and applied, exactly like any other backend."""+ from agent.delegation_provider import DelegationProvider+ from tools.delegate_tool_routes import resolve_task_routes++ class Replacement(DelegationProvider):+ @property+ def name(self):+ return "default"++ def route_task(self, task, config):+ return {"base_url": "https://chatgpt.com/backend-api/codex", "api_key": "k", "model": "gpt-6-astra",+ "service_tier": "fast", "label": "codex-fast"}++ monkeypatch.setattr("tools.delegate_tool_routes._active_provider", lambda config: Replacement())+ (route,), error = resolve_task_routes([{"goal": "one plain delegated task"}], {}, _parent())+ assert error is None and route.label == "codex-fast"+ assert route.creds["service_tier"] == "priority" and route.creds["request_overrides"]["service_tier"] == "priority"