Skip to content

The agent that grows with you

fix(gateway): forward failure_reason through the empty-response return path

Teknium41233e19c690
diff --git a/gateway/run.py b/gateway/run.py
index bad550857e5..aa999b53a99 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -20875,8 +20875,13 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
resolve_display_setting(user_config, platform_key, "cleanup_progress")
)
_cleanup_adapter = self._adapter_for_source(source) if _cleanup_progress else None
+ # getattr, not attribute access — same duck-typed-adapter guard as the
+ # edit_message check in send_progress_messages below: a fake/minimal
+ # adapter without delete_message means "can't delete", not a crash.
+ _cleanup_delete = getattr(type(_cleanup_adapter), "delete_message", None) if _cleanup_adapter is not None else None
if _cleanup_adapter is not None and (
- type(_cleanup_adapter).delete_message is BasePlatformAdapter.delete_message
+ _cleanup_delete is None
+ or _cleanup_delete is BasePlatformAdapter.delete_message
):
# Adapter doesn't support deletion — silently disable.
_cleanup_progress = False
@@ -21254,7 +21259,11 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
# Skip tool progress for platforms that don't support message
# editing (e.g. iMessage/BlueBubbles) — each progress update
# would become a separate message bubble, which is noisy.
- if type(adapter).edit_message is BasePlatformAdapter.edit_message:
+ # getattr, not attribute access: duck-typed adapters (test fakes,
+ # minimal plugin adapters) may not define edit_message at all —
+ # "missing" means the same thing as "base no-op": can't edit.
+ _adapter_edit = getattr(type(adapter), "edit_message", None)
+ if _adapter_edit is None or _adapter_edit is BasePlatformAdapter.edit_message:
while not progress_queue.empty():
try:
progress_queue.get_nowait()
@@ -22881,6 +22890,13 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
"messages": result.get("messages", []),
"api_calls": result.get("api_calls", 0),
"failed": result.get("failed", False),
+ # Sibling of the non-empty-response return below (#64686):
+ # the classifier's failure_reason must survive the
+ # empty-response normalization path too, or downstream
+ # consumers (TUI billing surface, transient-failure
+ # persistence) lose the structured reason exactly when
+ # the run produced no text.
+ "failure_reason": result.get("failure_reason"),
"partial": result.get("partial", False),
"completed": result.get("completed"),
"interrupted": result.get("interrupted", False),
diff --git a/tests/gateway/test_compression_failure_session_sync.py b/tests/gateway/test_compression_failure_session_sync.py
index 022a490f2ea..3c9162660bd 100644
--- a/tests/gateway/test_compression_failure_session_sync.py
+++ b/tests/gateway/test_compression_failure_session_sync.py
@@ -268,6 +268,45 @@ def test_nonempty_rate_limit_response_preserves_failure_metadata(monkeypatch):
assert result["failure_reason"] == "rate_limit"
assert result["completed"] is False
+
+class _EmptyRateLimitFailureAgent(_CompressionThenFailureAgent):
+ def run_conversation(self, user_message, conversation_history=None, task_id=None, **_kwargs):
+ return {
+ "final_response": "",
+ "failed": True,
+ "completed": False,
+ "error": "429 Too Many Requests",
+ "failure_reason": "rate_limit",
+ "messages": [
+ *(conversation_history or []),
+ {"role": "user", "content": user_message},
+ ],
+ "api_calls": 3,
+ }
+
+
+def test_empty_rate_limit_response_preserves_failure_metadata(monkeypatch):
+ """Sibling of the non-empty path (#64686): the empty-response return
+ branch in _run_agent must also forward failure_reason, or downstream
+ consumers lose the structured reason exactly when the run produced no
+ text at all."""
+ _install_compression_failure_agent(monkeypatch, _EmptyRateLimitFailureAgent)
+
+ session_store = _SessionStore()
+ runner = _runner(session_store)
+ source = SessionSource(
+ platform=Platform.TELEGRAM,
+ chat_id="12345",
+ chat_type="dm",
+ user_id="user-1",
+ )
+
+ result = _run_compression_failure_turn(runner, source)
+
+ assert result["failed"] is True
+ assert result["failure_reason"] == "rate_limit"
+ assert result["completed"] is False
+
class _ProviderSwitchAgent(_CompressionThenFailureAgent):
created_providers = []
second_turn_history = None