Skip to content

The agent that grows with you

fix(gateway): keep delegate completions on parent session

wodesikudc29b0d2e927
diff --git a/gateway/run_notifications.py b/gateway/run_notifications.py
index bf0a9b949a9..76f048249ab 100644
--- a/gateway/run_notifications.py
+++ b/gateway/run_notifications.py
@@ -287,6 +287,53 @@ class GatewayNotificationsMixin:
"dropping injection (#55578 fail-closed).", pinned_session_id,
)
return None
+ # Delegate rows are execution transcripts, not gateway route owners. A process completion can
+ # be stamped with the child session id even though the human-facing conversation belongs to
+ # `_delegate_from`. Follow that durable provenance before any route verification or mutation;
+ # otherwise switch_session() can end the real parent and rebind the platform chat to an
+ # internal child (#92611).
+ delegate_chain: set[str] = set()
+ while True:
+ if pinned_session_id in delegate_chain:
+ logger.warning(
+ "Async-delegation completion has cyclic delegate provenance at session %s; "
+ "dropping injection (#92611).", pinned_session_id,
+ )
+ return None
+ delegate_chain.add(pinned_session_id)
+ model_config = pinned_row.get("model_config")
+ if isinstance(model_config, str):
+ try:
+ model_config = json.loads(model_config)
+ except (TypeError, ValueError):
+ logger.warning(
+ "Async-delegation completion has malformed model_config for session %s; "
+ "dropping injection (#92611).", pinned_session_id,
+ )
+ return None
+ if model_config is not None and not isinstance(model_config, dict):
+ logger.warning(
+ "Async-delegation completion has non-object model_config for session %s; "
+ "dropping injection (#92611).", pinned_session_id,
+ )
+ return None
+ if not isinstance(model_config, dict):
+ model_config = {}
+ delegate_parent_id = str(model_config.get("_delegate_from") or "").strip()
+ if not delegate_parent_id:
+ break
+ try:
+ delegate_parent_row = await session_db.get_session(delegate_parent_id)
+ except Exception:
+ delegate_parent_row = None
+ if delegate_parent_row is None:
+ logger.warning(
+ "Async-delegation completion has missing delegate parent %s for child %s; "
+ "dropping injection (#92611).", delegate_parent_id, pinned_session_id,
+ )
+ return None
+ pinned_session_id = delegate_parent_id
+ pinned_row = delegate_parent_row
target_session_id = pinned_session_id
follows_compression = False
if pinned_row.get("ended_at"):
diff --git a/tests/gateway/test_async_delegation_session_binding.py b/tests/gateway/test_async_delegation_session_binding.py
index ebafac641d7..2380d6662aa 100644
--- a/tests/gateway/test_async_delegation_session_binding.py
+++ b/tests/gateway/test_async_delegation_session_binding.py
@@ -115,6 +115,108 @@ class TestGatewayPinningFailsClosed:
current.session_key, "sess_live", expected_session_id=current.session_id,
)
+ @pytest.mark.asyncio
+ async def test_live_delegate_child_resolves_to_parent_without_rebinding_route(self):
+ current = self._entry("sess_parent")
+ runner = self._make_runner(
+ {
+ "sess_parent": {"id": "sess_parent", "ended_at": None},
+ "sess_child": {
+ "id": "sess_child",
+ "ended_at": None,
+ "model_config": {"_delegate_from": "sess_parent"},
+ },
+ },
+ )
+
+ resolved = await runner._resolve_async_delegation_session(current, "sess_child")
+
+ assert resolved is current
+ self._assert_no_route_change(runner)
+
+ @pytest.mark.asyncio
+ async def test_nested_delegate_child_resolves_to_gateway_parent(self):
+ current = self._entry("sess_parent")
+ runner = self._make_runner(
+ {
+ "sess_parent": {"id": "sess_parent", "ended_at": None},
+ "sess_child": {
+ "id": "sess_child",
+ "ended_at": None,
+ "model_config": {"_delegate_from": "sess_parent"},
+ },
+ "sess_grandchild": {
+ "id": "sess_grandchild",
+ "ended_at": None,
+ "model_config": {"_delegate_from": "sess_child"},
+ },
+ },
+ )
+
+ resolved = await runner._resolve_async_delegation_session(current, "sess_grandchild")
+
+ assert resolved is current
+ self._assert_no_route_change(runner)
+
+ @pytest.mark.asyncio
+ async def test_delegate_provenance_cycle_fails_closed_without_route_change(self):
+ current = self._entry("sess_current")
+ runner = self._make_runner(
+ {
+ "sess_a": {
+ "id": "sess_a",
+ "ended_at": None,
+ "model_config": {"_delegate_from": "sess_b"},
+ },
+ "sess_b": {
+ "id": "sess_b",
+ "ended_at": None,
+ "model_config": {"_delegate_from": "sess_a"},
+ },
+ },
+ )
+
+ resolved = await runner._resolve_async_delegation_session(current, "sess_a")
+
+ assert resolved is None
+ self._assert_no_route_change(runner)
+
+ @pytest.mark.asyncio
+ async def test_delegate_with_missing_parent_fails_closed_without_route_change(self):
+ current = self._entry("sess_current")
+ runner = self._make_runner(
+ {
+ "sess_child": {
+ "id": "sess_child",
+ "ended_at": None,
+ "model_config": {"_delegate_from": "sess_missing"},
+ },
+ },
+ )
+
+ resolved = await runner._resolve_async_delegation_session(current, "sess_child")
+
+ assert resolved is None
+ self._assert_no_route_change(runner)
+
+ @pytest.mark.asyncio
+ async def test_malformed_delegate_config_fails_closed_without_route_change(self):
+ current = self._entry("sess_current")
+ runner = self._make_runner(
+ {
+ "sess_child": {
+ "id": "sess_child",
+ "ended_at": None,
+ "model_config": "{not-json}",
+ },
+ },
+ )
+
+ resolved = await runner._resolve_async_delegation_session(current, "sess_child")
+
+ assert resolved is None
+ self._assert_no_route_change(runner)
+
@pytest.mark.asyncio
async def test_non_compression_ended_parent_drops(self):
current = self._entry("sess_old")