Skip to content

The agent that grows with you

fix(agent): cap implicit cloud stream patience to run budget

1052326311042103620642
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index 68a7b1291a9..cbe8dc83d9e 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -661,7 +661,20 @@ def _cloud_stale_timeout_for(agent, api_kwargs: dict) -> float:
explicit = get_provider_stale_timeout(agent.provider, agent.model)
if explicit is not None:
return explicit
- return _cloud_stale_timeout(env_float("HERMES_STREAM_STALE_TIMEOUT", 180.0), api_kwargs)
+ timeout = _cloud_stale_timeout(env_float("HERMES_STREAM_STALE_TIMEOUT", 180.0), api_kwargs)
+ if os.getenv("HERMES_STREAM_STALE_TIMEOUT") is not None:
+ return timeout
+ # Match non-streaming patience: leave time for recovery near the run deadline,
+ # without changing explicit settings or the separate local-provider branch.
+ run_budget = getattr(agent, "run_budget_seconds", None)
+ started = getattr(agent, "_run_budget_started_at", None)
+ if not run_budget or not started:
+ return timeout
+ try:
+ remaining = float(run_budget) - (time.time() - float(started))
+ except (TypeError, ValueError):
+ return timeout
+ return min(timeout, max(60.0, remaining * 0.5))
def _bedrock_reasoning_stale_floor(model_id: object) -> "float | None":
diff --git a/tests/agent/test_run_budget.py b/tests/agent/test_run_budget.py
index 44858a3983c..50b4bd15b9f 100644
--- a/tests/agent/test_run_budget.py
+++ b/tests/agent/test_run_budget.py
@@ -37,6 +37,8 @@ def _make_agent(tmp_path, monkeypatch, config_body: str = "", **overrides):
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
(tmp_path / ".env").write_text("", encoding="utf-8")
monkeypatch.delenv("HERMES_API_CALL_STALE_TIMEOUT", raising=False)
+ monkeypatch.delenv("HERMES_STREAM_STALE_TIMEOUT", raising=False)
+ monkeypatch.delenv("HERMES_LOCAL_STREAM_STALE_TIMEOUT", raising=False)
_write_config(tmp_path, config_body)
from run_agent import AIAgent
@@ -168,6 +170,65 @@ def test_budget_without_started_clock_is_inert(monkeypatch, tmp_path):
base, _implicit = agent._resolved_api_call_stale_timeout_base()
assert agent._compute_non_stream_stale_timeout({"input": "hi"}) == base
+
[email protected]("path", ["chat", "bedrock"])
+ "config_body,env_value,elapsed,expected",
+ [
+ ("", None, 800, 60.0),
+ ("", None, 100, 400.0),
+ ("", None, None, 600.0),
+ ("providers:\n openai:\n stale_timeout_seconds: 30\n", None, 800, 30.0),
+ ("providers:\n openai:\n stale_timeout_seconds: 1200\n", None, 800, 1200.0),
+ ("", "1200", 800, 1200.0),
+ ("", "30", 800, 600.0),
+ ],
+)
+def test_cloud_stream_budget_preserves_explicit_deadlines(
+ monkeypatch, tmp_path, path, config_body, env_value, elapsed, expected
+):
+ """Both stream owners cap implicit patience without changing explicit precedence."""
+ from agent import chat_completion_helpers as helpers
+
+ agent = _make_agent(
+ tmp_path,
+ monkeypatch,
+ config_body,
+ model="deepseek/deepseek-v4-pro",
+ run_budget_seconds=900,
+ )
+ if env_value is not None:
+ monkeypatch.setenv("HERMES_STREAM_STALE_TIMEOUT", env_value)
+ monkeypatch.setattr(helpers.time, "time", lambda: 1000.0)
+ agent._run_budget_started_at = None if elapsed is None else 1000.0 - elapsed
+ payload = {"model": agent.model, "messages": [{"role": "user", "content": "hi"}]}
+ if path == "bedrock":
+ timeout = helpers._derive_stream_stale_timeout(agent, payload)
+ else:
+ call = helpers._StreamingCall(agent, payload, None)
+ call._resolve_stale_timeout()
+ timeout = call._stream_stale_timeout
+ assert timeout == expected
+
+
+def test_local_stream_patience_is_independent_of_run_budget(monkeypatch, tmp_path):
+ """A short remaining run budget does not shorten local model prefill grace."""
+ from agent import chat_completion_helpers as helpers
+
+ agent = _make_agent(
+ tmp_path,
+ monkeypatch,
+ base_url="http://127.0.0.1:11434/v1",
+ run_budget_seconds=900,
+ )
+ call = helpers._StreamingCall(agent, {"model": agent.model}, None)
+ call._resolve_stale_timeout()
+ without_clock = call._stream_stale_timeout
+ agent._run_budget_started_at = time.time() - 800
+ call._resolve_stale_timeout()
+ assert call._stream_stale_timeout == without_clock == 900.0
+
+
# ── wrap-up injection one-time-ness ────────────────────────────────────────
class _StubAgent: