Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend/copilot): pause idle timer during pending tools (#12927)

Zamil Majdy4a567a55a4ff
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.py
index 12a67e9a43..a1220658f0 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py
@@ -51,7 +51,6 @@ from ..constants import (
COPILOT_RETRYABLE_ERROR_PREFIX,
FRIENDLY_TRANSIENT_MSG,
STOPPED_BY_USER_MARKER,
- STREAM_IDLE_TIMEOUT_SECONDS,
is_transient_api_error,
)
from ..session_cleanup import prune_orphan_tool_calls
@@ -185,13 +184,24 @@ _CIRCUIT_BREAKER_ERROR_MSG = (
"Try breaking your request into smaller parts."
)
-# Idle timeout: abort the stream if no meaningful SDK message (only heartbeats)
-# arrives for this many seconds. Derived from MAX_TOOL_WAIT_SECONDS so the
-# invariant "no single tool blocks close to this long" holds by construction —
-# long-running tools use the async "start + poll" pattern (initial tool returns
-# with a handle, polling tool waits in ≤MAX_TOOL_WAIT_SECONDS chunks), so an
-# idle of 2× that genuinely means the SDK itself is stuck.
-_IDLE_TIMEOUT_SECONDS = STREAM_IDLE_TIMEOUT_SECONDS
+# Two regimes: no tool pending → 30 min (SDK genuinely idle); tool pending →
+# 2 h hard cap (lets long sub-AutoPilots run, still backstops a hung tool).
+_IDLE_TIMEOUT_SECONDS = 30 * 60
+_HUNG_TOOL_CAP_SECONDS = 2 * 60 * 60
+
+
+def _idle_timeout_threshold(adapter: SDKResponseAdapter) -> int:
+ """Pick the idle-timeout threshold for the current heartbeat.
+
+ Returns ``_HUNG_TOOL_CAP_SECONDS`` (longer) whenever any tool call is
+ still pending, so a legitimately long operation isn't killed. Returns
+ ``_IDLE_TIMEOUT_SECONDS`` (shorter) when nothing is pending — the SDK
+ itself is idle with no work in flight.
+ """
+ if adapter.has_unresolved_tool_calls:
+ return _HUNG_TOOL_CAP_SECONDS
+ return _IDLE_TIMEOUT_SECONDS
+
# StreamError codes that should render as a retryable error in the UI (retry
# button) rather than a terminal ErrorCard. Codes appended via
@@ -2396,16 +2406,17 @@ async def _run_stream_attempt(
yield ev
yield StreamHeartbeat()
- # Idle timeout: abort if the SDK has been silent for too long.
- # Long-running tools use the async "start + poll" pattern so
- # the MCP handler never blocks longer than the poll cap (5 min)
- # — a 10-min gap here means the SDK itself is stuck.
+ # Threshold flips to the long cap while a tool is pending; clock never resets.
idle_seconds = time.monotonic() - _last_real_msg_time
- if idle_seconds >= _IDLE_TIMEOUT_SECONDS:
+ threshold = _idle_timeout_threshold(state.adapter)
+ if idle_seconds >= threshold:
logger.error(
- "%s Idle timeout after %.0fs — aborting stream",
+ "%s Idle timeout after %.0fs (threshold=%ds, "
+ "tool_pending=%s) — aborting stream",
ctx.log_prefix,
idle_seconds,
+ threshold,
+ state.adapter.has_unresolved_tool_calls,
)
stream_error_msg = (
"The session has been idle for too long. Please try again."
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service_test.py b/autogpt_platform/backend/backend/copilot/sdk/service_test.py
index 3fe244e4fe..3e02e99fee 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service_test.py
@@ -12,8 +12,10 @@ import pytest
from backend.copilot import config as cfg_mod
from .service import (
+ _HUNG_TOOL_CAP_SECONDS,
_IDLE_TIMEOUT_SECONDS,
_build_system_prompt_value,
+ _idle_timeout_threshold,
_is_sdk_disconnect_error,
_normalize_model_name,
_prepare_file_attachments,
@@ -907,16 +909,6 @@ class TestSystemPromptPreset:
assert cfg.claude_agent_cross_user_prompt_cache is False
-class TestIdleTimeoutConstant:
- """SECRT-2247: long-running work now uses async start+poll pattern
- (run_sub_session / run_agent), so no single MCP tool call ever blocks
- the stream close to the idle limit. The plain 10-min cap from the
- original code is restored."""
-
- def test_idle_timeout_is_10_min(self):
- assert _IDLE_TIMEOUT_SECONDS == 10 * 60
-
-
# ---------------------------------------------------------------------------
# _RetryState.observed_model — Moonshot cost-override input
# ---------------------------------------------------------------------------
@@ -1137,3 +1129,61 @@ class TestMoonshotHelperReexports:
from .service import _override_cost_for_moonshot
assert _override_cost_for_moonshot is canonical
+
+
+class TestIdleTimeoutThreshold:
+ """SECRT-2247: stream uses two idle thresholds. The shorter 30-min threshold
+ fires when the SDK is idle with no tool pending. The longer 2-hour cap
+ applies while any tool call is pending so a 45-min sub-AutoPilot isn't
+ killed, but a truly hung tool still eventually frees session resources."""
+
+ def _make_adapter(self, current: dict, resolved: set):
+ from backend.copilot.sdk.response_adapter import SDKResponseAdapter
+
+ adapter = SDKResponseAdapter(session_id="test")
+ adapter.current_tool_calls = current
+ adapter.resolved_tool_calls = resolved
+ return adapter
+
+ def test_threshold_uses_long_cap_with_unresolved_tool_call(self):
+ adapter = self._make_adapter(
+ current={"t1": {"name": "run_block"}},
+ resolved=set(),
+ )
+ assert _idle_timeout_threshold(adapter) == _HUNG_TOOL_CAP_SECONDS
+
+ def test_threshold_uses_short_cap_when_all_tools_resolved(self):
+ adapter = self._make_adapter(
+ current={"t1": {"name": "find_agent"}},
+ resolved={"t1"},
+ )
+ assert _idle_timeout_threshold(adapter) == _IDLE_TIMEOUT_SECONDS
+
+ def test_threshold_uses_short_cap_with_no_tool_calls(self):
+ adapter = self._make_adapter(current={}, resolved=set())
+ assert _idle_timeout_threshold(adapter) == _IDLE_TIMEOUT_SECONDS
+
+ def test_threshold_uses_long_cap_with_mixed_resolved_and_pending(self):
+ adapter = self._make_adapter(
+ current={
+ "t1": {"name": "find_agent"},
+ "t2": {"name": "run_block"},
+ },
+ resolved={"t1"},
+ )
+ assert _idle_timeout_threshold(adapter) == _HUNG_TOOL_CAP_SECONDS
+
+ def test_idle_timeout_is_30_min_not_the_old_10(self):
+ # Regression guard: the old 10-min value killed long tool calls
+ # (SECRT-2247). New idle-without-tools cap is 30 min.
+ assert _IDLE_TIMEOUT_SECONDS == 30 * 60
+
+ def test_hung_tool_cap_is_2_hours(self):
+ # Hard cap protects against a hung tool leaking resources forever.
+ # 2 hours is plenty for any legitimate sub-AutoPilot or graph run.
+ assert _HUNG_TOOL_CAP_SECONDS == 2 * 60 * 60
+
+ def test_long_cap_is_strictly_longer_than_short_cap(self):
+ # The whole point of the two-regime design: pending tools get more
+ # patience than pure idle.
+ assert _HUNG_TOOL_CAP_SECONDS > _IDLE_TIMEOUT_SECONDS