Skip to content

The agent that grows with you

fix(gateway): isolate post-turn loop failures

fangliquanflq9219cd39445f
diff --git a/gateway/run.py b/gateway/run.py
index dd16dce8ff4..f85febca425 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -21048,22 +21048,29 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
source,
touch_activity=not is_internal,
)
- except Exception:
+ except Exception as exc:
+ logger.debug("post-turn session resolution failed: %s", exc)
return
# Empty interrupted/errored responses must not drive /goal, but an
# in-flight /loop tick still needs to be released and rescheduled.
if final_text.strip():
- await self._post_turn_goal_continuation(
+ try:
+ await self._post_turn_goal_continuation(
+ session_entry=session_entry,
+ source=source,
+ final_response=final_text,
+ )
+ except Exception as exc:
+ logger.debug("goal continuation hook failed: %s", exc)
+ try:
+ await self._post_turn_loop_completion(
session_entry=session_entry,
source=source,
final_response=final_text,
)
- await self._post_turn_loop_completion(
- session_entry=session_entry,
- source=source,
- final_response=final_text,
- )
+ except Exception as exc:
+ logger.debug("loop completion hook failed: %s", exc)
diff --git a/tests/gateway/test_loop_command.py b/tests/gateway/test_loop_command.py
index 4a8cfdbf9db..1720086f359 100644
--- a/tests/gateway/test_loop_command.py
+++ b/tests/gateway/test_loop_command.py
@@ -1,7 +1,8 @@
"""Gateway /loop command tests — dispatch, routing capture, mid-run guard."""
+import logging
import time
-from unittest.mock import AsyncMock
+from unittest.mock import AsyncMock, Mock
import pytest
@@ -163,3 +164,42 @@ async def test_empty_agent_result_releases_inflight_loop_tick(loop_env):
assert reloaded.awaiting_response is False
assert reloaded.status == "active"
assert reloaded.next_due_at > time.time()
+
+
+async def test_goal_hook_failure_does_not_block_loop_completion(loop_env, caplog):
+ runner = _make_runner()
+ await GatewayRunner._handle_loop_command(runner, _make_event("/loop 5m poll CI"))
+
+ mgr = loops.LoopManager(session_id="sid-gateway-loop")
+ mgr.state.next_due_at = time.time() - 1
+ assert mgr.fire_tick() is not None
+
+ runner._post_turn_goal_continuation = AsyncMock(side_effect=RuntimeError("judge failed"))
+ with caplog.at_level(logging.DEBUG, logger="gateway.run"):
+ await GatewayRunner._run_post_turn_hooks(
+ runner,
+ agent_result={"final_response": "still working"},
+ source=_make_event("wakeup").source,
+ is_internal=True,
+ )
+
+ reloaded = loops.load_loop("sid-gateway-loop")
+ assert reloaded.awaiting_response is False
+ assert "goal continuation hook failed: judge failed" in caplog.text
+
+
+async def test_post_turn_session_resolution_failure_is_logged(loop_env, caplog):
+ runner = _make_runner()
+ runner.session_store.get_or_create_session = Mock(side_effect=RuntimeError("store unavailable"))
+
+ with caplog.at_level(logging.DEBUG, logger="gateway.run"):
+ await GatewayRunner._run_post_turn_hooks(
+ runner,
+ agent_result={"final_response": ""},
+ source=_make_event("wakeup").source,
+ is_internal=True,
+ )
+
+ assert "post-turn session resolution failed: store unavailable" in caplog.text