Skip to content

The agent that grows with you

fix(delegation): refuse profile routes inside a managed worker

Teknium1d03183e5a3a
diff --git a/tests/tools/test_delegation_profile_route.py b/tests/tools/test_delegation_profile_route.py
index 2ed42ddd659..e2862ddf417 100644
--- a/tests/tools/test_delegation_profile_route.py
+++ b/tests/tools/test_delegation_profile_route.py
@@ -254,3 +254,40 @@ def test_profile_scope_that_fails_to_enter_leaves_the_parent_in_its_own_profile(
with delegated_profile_scope(coder):
pass
assert (_snapshot(), current_secret_scope_home(), is_multiplex_active()) == before
+
+
+class _ProfileProvider:
+ """A backend that routes every task to the ``coder`` profile."""
+ name = "profile_routes"
+
+ def task_parameters(self, config):
+ return {}
+
+ def route_task(self, task, config):
+ return {"profile": "coder"}
+
+
+def _resolve_coder_task():
+ from tools.delegate_tool_routes import resolve_task_routes
+ with patch("tools.delegate_tool_routes._active_provider", return_value=_ProfileProvider()):
+ return resolve_task_routes([{"goal": "work as coder"}], {}, _parent())
+
+
+def test_profile_route_is_refused_under_a_managed_worker_config_snapshot(two_profiles, monkeypatch):
+ """A managed worker's config readers all return its frozen session config, so the target profile's
+ config.yaml is unreachable there: refuse instead of pairing the launch model with the target's keys."""
+ import os
+
+ from agent import safe_worker_policy
+ from agent.delegation_provider import DelegationRouteError
+ from tools.delegate_tool_routes import _route_to_task_route
+
+ launch, _ = two_profiles
+ frozen = yaml.safe_load((launch / "config.yaml").read_text(encoding="utf-8"))
+ monkeypatch.setattr(safe_worker_policy, "_policy", safe_worker_policy._SafeWorkerPolicy(
+ os.getpid(), False, False, json.dumps(frozen)))
+
+ routes, error = _resolve_coder_task()
+ assert routes == [] and "managed worker" in (error or "")
+ with pytest.raises(DelegationRouteError):
+ _route_to_task_route({"profile": "coder"}, _parent(), {})
diff --git a/tools/delegate_tool_routes.py b/tools/delegate_tool_routes.py
index 6a23fdc3565..bb5f56684bf 100644
--- a/tools/delegate_tool_routes.py
+++ b/tools/delegate_tool_routes.py
@@ -161,6 +161,18 @@ def _profile_model_request(cfg: Mapping[str, Any]) -> dict[str, Any]:
return request
+def _refuse_under_worker_snapshot(home: Path) -> None:
+ """A managed worker (kanban, safe mode, managed sessions) runs on the session config frozen at creation:
+ every config reader returns that snapshot and the process never opens profile files, so another profile's
+ config.yaml is unreachable here. Refuse rather than pair the launch profile's model with the target's keys."""
+ from agent.safe_worker_policy import worker_config_snapshot
+ from hermes_constants import get_hermes_home, hermes_home_key
+ if worker_config_snapshot() is not None and hermes_home_key(home) != hermes_home_key(get_hermes_home()):
+ raise DelegationRouteError(
+ f"Profile routes are unavailable in a managed worker session (its config is frozen at creation): "
+ f"cannot run as the profile at {home}. Delegate without a profile.")
+
+
def _profile_route(home: Path, label: Optional[str]) -> TaskRoute:
"""The profile's own primary route, resolved inside its scope (its config.yaml + secrets). The
child never borrows the launching profile's key, pool, fallback chain or reasoning level."""
@@ -168,6 +180,7 @@ def _profile_route(home: Path, label: Optional[str]) -> TaskRoute:
from hermes_cli.runtime_provider import resolve_runtime_provider
from hermes_constants import resolve_reasoning_config
from tools.delegate_tool_config import ResolvedReasoning, _credential_bundle
+ _refuse_under_worker_snapshot(home)
with delegated_profile_scope(home):
cfg = load_config_readonly() or {}
request = _profile_model_request(cfg)