{"repo":"NousResearch/hermes-agent","public_id":"4493","display_label":"Auto-continue agent execution after gateway restart","summary":"These members address a feature or fix enabling agents to automatically resume interrupted work when a gateway restarts and conversation history ends on a tool response.","subsystem":"gateway","counts":{"members":3,"open_issues":0,"open_issues_distinct":0,"open_prs":0,"closed_issues":1,"merged_prs":1,"attempt_prs":1},"landed_fix":{"status":"merged_to_main","basis":"structural","pr":9934,"merged_at":"2026-04-14T23:56:49+00:00"},"resolution":{"state":"resolved","by":"pr-9934"},"freshness":{"state":"stale","last_verified_at":"2026-04-14T23:56:49+00:00","is_stale":true},"grouping":{"backbone":"STRUCTURAL_SPANNING","link_density":0.6666666666666666},"fix_edges":[{"issue_number":4493,"pr_number":9934,"tier":"verified","basis":"closing_ref","confidence":0.95,"recoverable_from_timeline":true,"pr_merged_at":"2026-04-14T23:56:49+00:00"}],"members":[{"kind":"issue","number":4493,"title":"[Enhancement] Auto-continue execution after gateway restart when history ends on tool response","url":null,"state":"closed"},{"kind":"pr","number":4509,"title":"fix(gateway): auto-continue execution when history ends on tool response after restart","url":null,"state":"closed","close_reason":{"refs":[{"kind":"commit","value":"e7475b158","number":0},{"kind":"pr","value":"#4493","number":4493}],"category":"implemented_elsewhere","implication":"superseded"},"merged":false,"is_fix_pr":true,"fix_tier":"suggested","fix_detail_withheld":true},{"kind":"pr","number":9934,"title":"feat: auto-continue interrupted agent work after gateway restart (#4493)","url":null,"state":"merged","close_reason":{"category":"merged"},"merged":true,"is_fix_pr":true,"fix_tier":"verified","files_changed":["gateway/run.py","tests/gateway/test_auto_continue.py","tests/gateway/test_restart_drain.py"],"fix_hunks":[{"file":"gateway/run.py","hunks":["@@ -1405,7 +1405,7 @@ async def _notify_active_sessions_of_shutdown(self) -> None:\n         action = \"restarting\" if self._restart_requested else \"shutting down\"\n         hint = (\n             \"Your current task will be interrupted. \"\n-            \"Use /retry after restart to continue.\"\n+            \"Send any message after restart to resume where it left off.\"\n             if self._restart_requested\n             else \"Your current task will be interrupted.\"\n         )","@@ -8450,6 +8450,21 @@ def _approval_notify_sync(approval_data: dict) -> None:\n             if _msn:\n                 message = _msn + \"\\n\\n\" + message\n \n+            # Auto-continue: if the loaded history ends with a tool result,\n+            # the previous agent turn was interrupted mid-work (gateway\n+            # restart, crash, SIGTERM).  Prepend a system note so the model\n+            # finishes processing the pending tool results before addressing\n+            # the user's new message.  (#4493)\n+            if agent_history and agent_history[-1].get(\"role\") == \"tool\":\n+                message = (\n+                    \"[System note: Your previous turn was interrupted before you could \"\n+                    \"process the last tool result(s). The conversation history contains \"\n+                    \"tool outputs you haven't responded to yet. Please finish processing \"\n+                    \"those results and summarize what was accomplished, then address the \"\n+                    \"user's new message below.]\\n\\n\"\n+                    + message\n+                )\n+\n             _approval_session_key = session_key or \"\"\n             _approval_session_token = [redacted:secret-assignment]\n             register_gateway_notify(_approval_session_key, _approval_notify_sync)"]},{"file":"tests/gateway/test_auto_continue.py","hunks":["@@ -0,0 +1,95 @@\n+\"\"\"Tests for the auto-continue feature (#4493).\n+\n+When the gateway restarts mid-agent-work, the session transcript ends on a\n+tool result that the agent never processed.  The auto-continue logic detects\n+this and prepends a system note to the next user message so the model\n+finishes the interrupted work before addressing the new input.\n+\"\"\"\n+\n+import pytest\n+\n+\n+def _simulate_auto_continue(agent_history: list, user_message: str) -> str:\n+    \"\"\"Reproduce the auto-continue injection logic from _run_agent().\n+\n+    This mirrors the exact code in gateway/run.py so we can test the\n+    detection and message transformation without spinning up a full\n+    gateway runner.\n+    \"\"\"\n+    message = user_message\n+    if agent_history and agent_history[-1].get(\"role\") == \"tool\":\n+        message = (\n+            \"[System note: Your previous turn was interrupted before you could \"\n+            \"process the last tool result(s). The conversation history contains \"\n+            \"tool outputs you haven't responded to yet. Please finish processing \"\n+            \"those results and summarize what was accomplished, then address the \"\n+            \"user's new message below.]\\n\\n\"\n+            + message\n+        )\n+    return message\n+\n+\n+class TestAutoDetection:\n+    \"\"\"Test that trailing tool results are correctly detected.\"\"\"\n+\n+    def test_trailing_tool_result_triggers_note(self):\n+        history = [\n+            {\"role\": \"user\", \"content\": \"deploy the app\"},\n+            {\"role\": \"assistant\", \"content\": None, \"tool_calls\": [\n+                {\"id\": \"call_1\", \"function\": {\"name\": \"terminal\", \"arguments\": \"{}\"}}\n+            ]},\n+            {\"role\": \"tool\", \"tool_call_id\": \"call_1\", \"content\": \"deployed successfully\"},\n+        ]\n+        result = _simulate_auto_continue(history, \"what happened?\")\n+        assert \"[System note:\" in result\n+        assert \"interrupted\" in result\n+        assert \"what happened?\" in result\n+\n+    def test_trailing_assistant_message_no_note(self):\n+        history = [\n+            {\"role\": \"user\", \"content\": \"hello\"},\n+            {\"role\": \"assistant\", \"content\": \"Hi there!\"},\n+        ]\n+        result = _simulate_auto_continue(history, \"how are you?\")\n+        assert \"[System note:\" not in result\n+        assert result == \"how are you?\"\n+\n+    def test_empty_history_no_note(self):\n+        result = _simulate_auto_continue([], \"hello\")\n+        assert result == \"hello\"\n+\n+    def test_trailing_user_message_no_note(self):\n+        \"\"\"Shouldn't happen in practice, but ensure no false positive.\"\"\"\n+        history = [\n+            {\"role\": \"user\", \"content\": \"hello\"},\n+        ]\n+        result = _simulate_auto_continue(history, \"hello again\")\n+        assert result == \"hello again\"\n+\n+    def test_multiple_tool_results_still_triggers(self):\n+        \"\"\"Multiple tool calls in a row — last one is still role=tool.\"\"\"\n+        history = [\n+            {\"role\": \"user\", \"content\": \"search and read\"},\n+            {\"role\": \"assistant\", \"content\": None, \"tool_calls\": [\n+                {\"id\": \"call_1\", \"function\": {\"name\": \"search\", \"arguments\": \"{}\"}},\n+                {\"id\": \"call_2\", \"function\": {\"name\": \"read\", \"arguments\": \"{}\"}},\n+            ]},\n+            {\"role\": \"tool\", \"tool_call_id\": \"call_1\", \"content\": \"found it\"},\n+            {\"role\": \"tool\", \"tool_call_id\": \"call_2\", \"content\": \"file content here\"},\n+        ]\n+        result = _simulate_auto_continue(history, \"continue\")\n+        assert \"[System note:\" in result\n+\n+    def test_original_message_preserved_after_note(self):\n+        \"\"\"The user's actual message must appear after the system note.\"\"\"\n+        history = [\n+            {\"role\": \"assistant\", \"content\": None, \"tool_calls\": [\n+                {\"id\": \"c1\", \"function\": {\"name\": \"t\", \"arguments\": \"{}\"}}\n+            ]},\n+            {\"role\": \"tool\", \"tool_call_id\": \"c1\", \"content\": \"done\"},\n+        ]\n+        result = _simulate_auto_continue(history, \"now do X\")\n+        # System note comes first, then user's message\n+        note_end = result.index(\"]\\n\\n\")\n+        user_msg_start = result.index(\"now do X\")\n+        assert user_msg_start > note_end"]},{"file":"tests/gateway/test_restart_drain.py","hunks":["@@ -193,7 +193,7 @@ async def test_shutdown_notification_says_restarting_when_restart_requested():\n \n     assert len(adapter.sent) == 1\n     assert \"restarting\" in adapter.sent[0]\n-    assert \"/retry\" in adapter.sent[0]\n+    assert \"resume\" in adapter.sent[0]\n \n \n @pytest.mark.asyncio\n"]}]}],"trust":{"default_tier":"verified","includes_suggested":false}}