{"repo":"NousResearch/hermes-agent","public_id":"7100","display_label":"Persist user messages during transient gateway failures","summary":"These issues and fixes address a regression where rate limit errors, timeouts, or other transient agent failures could silently drop user messages from the transcript. The fixes ensure user turns are preserved even when the agent fails to respond.","subsystem":null,"counts":{"members":4,"open_issues":0,"open_issues_distinct":0,"open_prs":0,"closed_issues":1,"merged_prs":1,"attempt_prs":2},"landed_fix":{"status":"merged_to_main","basis":"structural","pr":17900,"merged_at":"2026-04-30T11:32:33+00:00"},"resolution":{"state":"resolved","by":"pr-17900"},"freshness":{"state":"verified","last_verified_at":"2026-04-30T11:32:33+00:00","is_stale":false},"grouping":{"backbone":"STRUCTURAL_SPANNING","link_density":0.5},"fix_edges":[{"issue_number":7100,"pr_number":17900,"tier":"verified","basis":"closing_ref","confidence":0.95,"recoverable_from_timeline":true,"pr_merged_at":"2026-04-30T11:32:33+00:00"}],"members":[{"kind":"issue","number":7100,"title":"[Bug] Rate limit (429/timeout) failures silently drop user messages from transcript — #1630 regression","url":null,"state":"closed"},{"kind":"pr","number":7134,"title":"fix(gateway): preserve failed turns outside context overflow","url":null,"state":"closed","close_reason":{"refs":[{"kind":"pr","value":"#7100","number":7100},{"kind":"pr","value":"#7134","number":7134}],"category":"uncategorized","implication":"needs_llm"},"merged":false},{"kind":"pr","number":11761,"title":"fix(gateway): persist user message on transient agent failures (#7100)","url":null,"state":"closed","close_reason":{"category":"volume_quota","implication":"active"},"merged":false},{"kind":"pr","number":17900,"title":"fix(gateway): persist user message on transient agent failures (#7100)","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_7100_transient_failure_transcript.py"],"fix_hunks":[{"file":"gateway/run.py","hunks":["@@ -5542,15 +5542,43 @@ async def _handle_message_with_agent(self, event, source, _quick_key: str, run_g\n             # intermediate reasoning) so sessions can be resumed with full context\n             # and transcripts are useful for debugging and training data.\n             #\n-            # IMPORTANT: When the agent failed (e.g. context-overflow 400,\n-            # compression exhausted), do NOT persist the user's message.\n-            # Persisting it would make the session even larger, causing the\n-            # same failure on the next attempt — an infinite loop. (#1630, #9893)\n+            # IMPORTANT: For context-overflow failures (compression exhausted,\n+            # generic 400 on large sessions) we must NOT persist the user's\n+            # message — doing so would grow the session further and cause the\n+            # same failure on the next attempt, an infinite loop. (#1630, #9893)\n+            #\n+            # Transient failures (429, timeout, connection error, provider 5xx)\n+            # are different: the session is not oversized, and silently dropping\n+            # the user message causes severe context loss on retry — the agent\n+            # forgets what was just asked.  Persist the user turn so the\n+            # conversation is preserved. (#7100)\n             agent_failed_early = bool(agent_result.get(\"failed\"))\n-            if agent_failed_early:\n+            _err_str_for_classify = str(agent_result.get(\"error\", \"\")).lower()\n+            # Use specific multi-word phrases (not bare \"exceed\" or \"token\")\n+            # to avoid false positives on transient errors like \"rate limit\n+            # exceeded\" or \"invalid auth token\". Matches run_agent.py's\n+            # own context-length classifier.\n+            is_context_overflow_failure = agent_failed_early and (\n+                bool(agent_result.get(\"compression_exhausted\"))\n+                or any(p in _err_str_for_classify for p in (\n+                    \"context length\", \"context size\", \"context window\",\n+                    \"maximum context\", \"token limit\", \"too many tokens\",\n+                    \"reduce the length\", \"exceeds the limit\",\n+                    \"request entity too large\", \"prompt is too long\",\n+                    \"payload too large\", \"input is too long\",\n+                ))\n+                or (\"400\" in _err_str_for_classify and len(history) > 50)\n+            )\n+            if is_context_overflow_failure:\n+                logger.info(\n+                    \"Skipping transcript persistence for context-overflow \"\n+                    \"failure in session %s to prevent session growth loop.\",\n+                    session_entry.session_id,\n+                )\n+            elif agent_failed_early:\n                 logger.info(\n-                    \"Skipping transcript persistence for failed request in \"\n-                    \"session %s to prevent session growth loop.\",\n+                    \"Transient agent failure in session %s — persisting user \"\n+                    \"message so conversation context is preserved on retry.\",\n                     session_entry.session_id,\n                 )\n ","@@ -5580,7 +5608,7 @@ async def _handle_message_with_agent(self, event, source, _quick_key: str, run_g\n             # If this is a fresh session (no history), write the full tool\n             # definitions as the first entry so the transcript is self-describing\n             # -- the same list of dicts sent as tools=[...] in the API request.\n-            if agent_failed_early:\n+            if is_context_overflow_failure:\n                 pass  # Skip all transcript writes — don't grow a broken session\n             elif not history:\n                 tool_defs = agent_result.get(\"tools\", [])","@@ -5599,10 +5627,21 @@ async def _handle_message_with_agent(self, event, source, _quick_key: str, run_g\n             # Use the filtered history length (history_offset) that was actually\n             # passed to the agent, not len(history) which includes session_meta\n             # entries that were stripped before the agent saw them.\n-            if not agent_failed_early:\n+            if is_context_overflow_failure:\n+                pass  # handled above — skip all transcript writes\n+            elif agent_failed_early:\n+                # Transient failure (429/timeout/5xx): persist only the user\n+                # message so the next message can load a transcript that\n+                # reflects what was said.  Skip the assistant error text since\n+                # it's a gateway-generated hint, not model output. (#7100)\n+                self.session_store.append_to_transcript(\n+                    session_entry.session_id,\n+                    {\"role\": \"user\", \"content\": message_text, \"timestamp\": ts},\n+                )\n+            else:\n                 history_len = agent_result.get(\"history_offset\", len(history))\n                 new_messages = agent_messages[history_len:] if len(agent_messages) > history_len else []\n-                \n+\n                 # If no new messages found (edge case), fall back to simple user/assistant\n                 if not new_messages:\n                     self.session_store.append_to_transcript("]},{"file":"tests/gateway/test_7100_transient_failure_transcript.py","hunks":["@@ -0,0 +1,137 @@\n+\"\"\"Tests for #7100 — transient failures (429/timeout) must not drop the\n+user message from the transcript.\n+\n+The #1630 fix introduced a blanket skip of transcript writes on any\n+``failed`` agent result.  That was correct for context-overflow failures\n+(which would otherwise cause a session-growth loop), but it also caused\n+transient provider failures (rate limits, read timeouts, connection\n+resets) to silently drop the user's message — so the agent had no memory\n+of the last turn on the next attempt.\n+\n+The gateway classifier must distinguish:\n+\n+* ``compression_exhausted=True`` OR context-keyword errors OR a generic\n+  ``400`` on a long history  → context-overflow → skip transcript\n+* everything else that fails → transient → persist the user message\n+\"\"\"\n+\n+import pytest\n+\n+\n+def _classify(agent_result: dict, history_len: int) -> tuple[bool, bool]:\n+    \"\"\"Replicate the gateway classifier from GatewayRunner._run_agent.\n+\n+    Returns ``(agent_failed_early, is_context_overflow_failure)``.\n+    \"\"\"\n+    agent_failed_early = bool(agent_result.get(\"failed\"))\n+    err = str(agent_result.get(\"error\", \"\")).lower()\n+    is_context_overflow_failure = agent_failed_early and (\n+        bool(agent_result.get(\"compression_exhausted\"))\n+        or any(p in err for p in (\n+            \"context length\", \"context size\", \"context window\",\n+            \"maximum context\", \"token limit\", \"too many tokens\",\n+            \"reduce the length\", \"exceeds the limit\",\n+            \"request entity too large\", \"prompt is too long\",\n+            \"payload too large\", \"input is too long\",\n+        ))\n+        or (\"400\" in err and history_len > 50)\n+    )\n+    return agent_failed_early, is_context_overflow_failure\n+\n+\n+class TestContextOverflowStillSkipsTranscript:\n+    \"\"\"#1630 behavior must be preserved for real context-overflow cases.\"\"\"\n+\n+    def test_compression_exhausted_is_context_overflow(self):\n+        agent_result = {\n+            \"failed\": True,\n+            \"compression_exhausted\": True,\n+            \"error\": \"Request payload too large: max compression attempts reached.\",\n+        }\n+        failed, ctx_overflow = _classify(agent_result, history_len=100)\n+        assert failed\n+        assert ctx_overflow\n+\n+    def test_explicit_context_length_error_is_context_overflow(self):\n+        agent_result = {\n+            \"failed\": True,\n+            \"error\": \"prompt is too long: 250000 tokens > 200000 maximum\",\n+        }\n+        failed, ctx_overflow = _classify(agent_result, history_len=10)\n+        assert failed\n+        assert ctx_overflow\n+\n+    def test_generic_400_on_large_session_is_context_overflow(self):\n+        agent_result = {\n+            \"failed\": True,\n+            \"error\": \"error code: 400 - {'type': 'error', 'message': 'Error'}\",\n+        }\n+        failed, ctx_overflow = _classify(agent_result, history_len=100)\n+        assert failed\n+        assert ctx_overflow\n+\n+\n+class TestTransientFailureKeepsUserMessage:\n+    \"\"\"Transient provider failures must NOT skip the transcript — doing so\n+    drops the user message and the agent forgets the turn. (#7100)\"\"\"\n+\n+    def test_rate_limit_429_is_not_context_overflow(self):\n+        agent_result = {\n+            \"failed\": True,\n+            \"error\": (\n+                \"API call failed after 3 retries: 429 Too Many Requests \"\n+                \"— rate limit exceeded\"\n+            ),\n+        }\n+        failed, ctx_overflow = _classify(agent_result, history_len=10)\n+        assert failed\n+        assert not ctx_overflow\n+\n+    def test_read_timeout_is_not_context_overflow(self):\n+        agent_result = {\n+            \"failed\": True,\n+            \"error\": \"ReadTimeout: HTTPSConnectionPool(host='api.z.ai'): Read timed out.\",\n+        }\n+        failed, ctx_overflow = _classify(agent_result, history_len=10)\n+        assert failed\n+        assert not ctx_overflow\n+\n+    def test_connection_reset_is_not_context_overflow(self):\n+        agent_result = {\n+            \"failed\": True,\n+            \"error\": \"ConnectionError: [Errno 54] Connection reset by peer\",\n+        }\n+        failed, ctx_overflow = _classify(agent_result, history_len=10)\n+        assert failed\n+        assert not ctx_overflow\n+\n+    def test_provider_500_is_not_context_overflow(self):\n+        agent_result = {\n+            \"failed\": True,\n+            \"error\": \"API call failed after 3 retries: 500 Internal Server Error\",\n+        }\n+        failed, ctx_overflow = _classify(agent_result, history_len=10)\n+        assert failed\n+        assert not ctx_overflow\n+\n+    def test_generic_400_on_short_session_is_not_context_overflow(self):\n+        \"\"\"A 400 on a short session is a real client error, not context\n+        overflow — still not a reason to drop the user turn.\"\"\"\n+        agent_result = {\n+            \"failed\": True,\n+            \"error\": \"error code: 400 - invalid model\",\n+        }\n+        failed, ctx_overflow = _classify(agent_result, history_len=5)\n+        assert failed\n+        assert not ctx_overflow\n+\n+\n+class TestSuccessfulResultUnaffected:\n+    def test_successful_result_neither_failed_nor_overflow(self):\n+        agent_result = {\n+            \"final_response\": \"Hello!\",\n+            \"messages\": [{\"role\": \"assistant\", \"content\": \"Hello!\"}],\n+        }\n+        failed, ctx_overflow = _classify(agent_result, history_len=10)\n+        assert not failed\n+        assert not ctx_overflow\n"]}]}],"trust":{"default_tier":"verified","includes_suggested":false}}