Skip to content

The agent that grows with you

fix(delegation): a profile scope that fails to enter never strands the parent in the child profile

Teknium170122103391
diff --git a/tests/tools/test_delegation_profile_route.py b/tests/tools/test_delegation_profile_route.py
index 1189ad86397..2ed42ddd659 100644
--- a/tests/tools/test_delegation_profile_route.py
+++ b/tests/tools/test_delegation_profile_route.py
@@ -238,3 +238,19 @@ def test_profile_child_lease_and_schema_retry_run_inside_the_target_profile(two_
assert seen["lease"]["home"] == str(coder) and seen["lease"]["model"] == "coder-model"
assert seen["retry"] == {"home": str(coder), "secret": "coder-secret", "model": "coder-model"}
assert _snapshot() == {"home": str(launch), "secret": "launch-secret", "model": "launch-model"}
+
+
[email protected]("failing", ["agent.secret_scope.set_secret_scope",
+ "tools.terminal_scope.install_profile_terminal_scope"])
+def test_profile_scope_that_fails_to_enter_leaves_the_parent_in_its_own_profile(two_profiles, failing):
+ """A step of the scope binding that raises unwinds the steps already bound: the parent never stays
+ inside the child's profile (home override, secret scope, multiplex flag)."""
+ from agent.secret_scope import current_secret_scope_home, is_multiplex_active
+ from tools.delegate_tool_routes import delegated_profile_scope
+
+ _, coder = two_profiles
+ before = (_snapshot(), current_secret_scope_home(), is_multiplex_active())
+ with patch(failing, side_effect=RuntimeError("scope step failed")), pytest.raises(RuntimeError):
+ with delegated_profile_scope(coder):
+ pass
+ assert (_snapshot(), current_secret_scope_home(), is_multiplex_active()) == before
diff --git a/tools/delegate_tool_routes.py b/tools/delegate_tool_routes.py
index 0f28a772b0b..6a23fdc3565 100644
--- a/tools/delegate_tool_routes.py
+++ b/tools/delegate_tool_routes.py
@@ -121,18 +121,15 @@ def delegated_profile_scope(home: Any) -> Iterator[None]:
routed = hermes_home_key(home) != hermes_home_key(get_routing_process_hermes_home())
hydrate_profile_secret_sources(home)
- home_token = set_hermes_home_override(str(home))
- secret_token = set_secret_scope(owner_secret_scope(home), profile_home=str(home))
- mux_token = set_multiplex_context(True) if routed else None
- terminal_token = install_profile_terminal_scope(home)
- try:
+ # Each step is entered inside the stack, so one that raises unwinds the ones already bound and the
+ # parent never stays in the child's profile (same discipline as the gateway's routed-turn scope).
+ with contextlib.ExitStack() as stack:
+ stack.callback(reset_hermes_home_override, set_hermes_home_override(str(home)))
+ stack.callback(reset_secret_scope, set_secret_scope(owner_secret_scope(home), profile_home=str(home)))
+ if routed:
+ stack.callback(reset_multiplex_context, set_multiplex_context(True))
+ stack.callback(reset_terminal_scope, install_profile_terminal_scope(home))
yield
- finally:
- reset_terminal_scope(terminal_token)
- if mux_token is not None:
- reset_multiplex_context(mux_token)
- reset_secret_scope(secret_token)
- reset_hermes_home_override(home_token)
def child_profile_scope(child: Any) -> contextlib.AbstractContextManager: