Skip to content

The agent that grows with you

fix(resolve_turn_limit): gateway bridge null handling, TUI resolver, docs

Chris Fontes64505a2b83df
diff --git a/gateway/run.py b/gateway/run.py
index d23ab30a9dc..2fff508784e 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -2049,10 +2049,18 @@ def _bridge_max_turns_from_config(home: "Path") -> None:
agent_cfg = cfg.get("agent", {})
if isinstance(agent_cfg, dict) and "max_turns" in agent_cfg:
+ raw = agent_cfg["max_turns"]
# Preserve the raw value's spelling (e.g. "none", "unlimited", "120")
# so resolve_turn_limit() in _current_max_iterations can interpret it.
- # str() round-trips both Python None (→ "None") and YAML strings.
- os.environ["HERMES_MAX_ITERATIONS"] = str(agent_cfg["max_turns"])
+ # Skip bridging when the YAML value is Python None (from `null` or bare
+ # `key:`) — this preserves "absent = default" semantics downstream.
+ # Without this guard, str(None) → "None" → resolve_turn_limit maps it
+ # to the unlimited sentinel instead of the default (90/500).
+ if raw is not None:
+ os.environ["HERMES_MAX_ITERATIONS"] = str(raw)
+ elif "HERMES_MAX_ITERATIONS" in os.environ:
+ # Clear stale bridge so downstream resolver applies its default.
+ del os.environ["HERMES_MAX_ITERATIONS"]
# config-authoritative knobs for the session-search index (config.yaml
# sessions.* wins over stale env; env stays the cross-process carrier).
sessions_cfg = cfg.get("sessions", {})
@@ -2362,7 +2370,13 @@ if _config_path.exists():
_agent_cfg = _cfg.get("agent", {})
if _agent_cfg and isinstance(_agent_cfg, dict):
if "max_turns" in _agent_cfg:
- os.environ["HERMES_MAX_ITERATIONS"] = str(_agent_cfg["max_turns"])
+ _raw_mt = _agent_cfg["max_turns"]
+ # Same None-guard as _bridge_max_turns_from_config: str(None)
+ # → "None" → resolve_turn_limit maps to unlimited, not default.
+ if _raw_mt is not None:
+ os.environ["HERMES_MAX_ITERATIONS"] = str(_raw_mt)
+ elif "HERMES_MAX_ITERATIONS" in os.environ:
+ del os.environ["HERMES_MAX_ITERATIONS"]
if "gateway_timeout" in _agent_cfg:
os.environ["HERMES_AGENT_TIMEOUT"] = str(_agent_cfg["gateway_timeout"])
if "gateway_turn_lease_timeout" in _agent_cfg:
diff --git a/tests/hermes_cli/test_resolve_turn_limit.py b/tests/hermes_cli/test_resolve_turn_limit.py
index f4675dec154..417535ffb64 100644
--- a/tests/hermes_cli/test_resolve_turn_limit.py
+++ b/tests/hermes_cli/test_resolve_turn_limit.py
@@ -5,6 +5,7 @@ Covers the full spelling table (int, float, numeric string, ``"none"``,
``"unlimited"``, ``"infinite"``, ``"-1"``, ``"0"``, YAML ``None``, bool,
garbage) and the str→int env-var round-trip that the gateway bridge relies on.
"""
+import os
import sys
import pytest
@@ -130,3 +131,100 @@ class TestEnvVarBridgeSimulation:
def test_unlimited_string_round_trip(self):
env_val = str("unlimited")
assert resolve_turn_limit(env_val) == TURN_LIMIT_UNLIMITED
+
+
+class TestGatewayBridgeNullHandling:
+ """Verify that _bridge_max_turns_from_config does NOT serialize Python None
+ (from YAML ``null`` or bare ``key:``) into the env var as the string
+ "None", which would resolve to unlimited instead of the default."""
+
+ def test_none_value_not_bridged(self, monkeypatch, tmp_path):
+ """YAML ``max_turns: null`` should not set HERMES_MAX_ITERATIONS."""
+ import yaml
+ cfg_file = tmp_path / "config.yaml"
+ cfg_file.write_text("agent:\n max_turns: null\n", encoding="utf-8")
+ monkeypatch.setenv("HERMES_MAX_ITERATIONS", "stale-120")
+ # Import here to avoid module-level gateway dependency
+ from gateway.run import _bridge_max_turns_from_config
+ _bridge_max_turns_from_config(tmp_path)
+ # The stale env var should be cleared so downstream resolver
+ # applies the default rather than using the stale value.
+ assert "HERMES_MAX_ITERATIONS" not in os.environ
+
+ def test_string_none_bridged_correctly(self, monkeypatch, tmp_path):
+ """YAML ``max_turns: "none"`` should bridge as the string "none"."""
+ cfg_file = tmp_path / "config.yaml"
+ cfg_file.write_text('agent:\n max_turns: "none"\n', encoding="utf-8")
+ monkeypatch.delenv("HERMES_MAX_ITERATIONS", raising=False)
+ from gateway.run import _bridge_max_turns_from_config
+ _bridge_max_turns_from_config(tmp_path)
+ assert os.environ.get("HERMES_MAX_ITERATIONS") == "none"
+
+ def test_int_bridged_correctly(self, monkeypatch, tmp_path):
+ """YAML ``max_turns: 120`` should bridge as "120"."""
+ cfg_file = tmp_path / "config.yaml"
+ cfg_file.write_text("agent:\n max_turns: 120\n", encoding="utf-8")
+ monkeypatch.delenv("HERMES_MAX_ITERATIONS", raising=False)
+ from gateway.run import _bridge_max_turns_from_config
+ _bridge_max_turns_from_config(tmp_path)
+ assert os.environ.get("HERMES_MAX_ITERATIONS") == "120"
+
+ def test_bare_key_treated_as_null(self, monkeypatch, tmp_path):
+ """YAML ``max_turns:`` (bare key, no value) parses as Python None."""
+ import yaml
+ cfg_file = tmp_path / "config.yaml"
+ cfg_file.write_text("agent:\n max_turns:\n", encoding="utf-8")
+ monkeypatch.setenv("HERMES_MAX_ITERATIONS", "stale-90")
+ from gateway.run import _bridge_max_turns_from_config
+ _bridge_max_turns_from_config(tmp_path)
+ assert "HERMES_MAX_ITERATIONS" not in os.environ
+
+
+class TestTUIResolver:
+ """Verify that _cfg_max_turns in tui_gateway/server.py routes through
+ resolve_turn_limit instead of bare int()."""
+
+ def test_string_none_resolves_to_unlimited(self):
+ from tui_gateway.server import _cfg_max_turns
+ cfg = {"agent": {"max_turns": "none"}}
+ assert _cfg_max_turns(cfg, default=25) == TURN_LIMIT_UNLIMITED
+
+ def test_int_zero_resolves_to_unlimited(self):
+ from tui_gateway.server import _cfg_max_turns
+ cfg = {"agent": {"max_turns": 0}}
+ assert _cfg_max_turns(cfg, default=25) == TURN_LIMIT_UNLIMITED
+
+ def test_string_unlimited_resolves_to_unlimited(self):
+ from tui_gateway.server import _cfg_max_turns
+ cfg = {"agent": {"max_turns": "unlimited"}}
+ assert _cfg_max_turns(cfg, default=25) == TURN_LIMIT_UNLIMITED
+
+ def test_int_passthrough(self):
+ from tui_gateway.server import _cfg_max_turns
+ cfg = {"agent": {"max_turns": 50}}
+ assert _cfg_max_turns(cfg, default=25) == 50
+
+ def test_none_falls_through_to_default(self):
+ from tui_gateway.server import _cfg_max_turns
+ cfg = {"agent": {}}
+ assert _cfg_max_turns(cfg, default=25) == 25
+
+ def test_env_var_override(self, monkeypatch):
+ from tui_gateway.server import _cfg_max_turns
+ monkeypatch.setenv("HERMES_TUI_MAX_TURNS", "none")
+ cfg = {"agent": {"max_turns": 50}}
+ assert _cfg_max_turns(cfg, default=25) == TURN_LIMIT_UNLIMITED
+
+ def test_env_var_zero_is_unlimited(self, monkeypatch):
+ """Old code swallowed 0 via `int(0) > 0` → False. Now it routes
+ through resolve_turn_limit which treats 0 as unlimited."""
+ from tui_gateway.server import _cfg_max_turns
+ monkeypatch.setenv("HERMES_TUI_MAX_TURNS", "0")
+ cfg = {"agent": {"max_turns": 50}}
+ assert _cfg_max_turns(cfg, default=25) == TURN_LIMIT_UNLIMITED
+
+ def test_root_level_max_turns(self):
+ """Legacy root-level max_turns still works."""
+ from tui_gateway.server import _cfg_max_turns
+ cfg = {"max_turns": "none"}
+ assert _cfg_max_turns(cfg, default=25) == TURN_LIMIT_UNLIMITED
diff --git a/tui_gateway/server.py b/tui_gateway/server.py
index e8e76e0f299..d9abe905014 100644
--- a/tui_gateway/server.py
+++ b/tui_gateway/server.py
@@ -6618,14 +6618,20 @@ def _apply_personality_to_session(
def _cfg_max_turns(cfg: dict, default: int) -> int:
- try:
- env_max = int(os.environ.get("HERMES_TUI_MAX_TURNS", "") or 0)
- if env_max > 0:
- return env_max
- except (TypeError, ValueError):
- pass
+ from hermes_cli.config import resolve_turn_limit as _resolve_turn_limit
+ # Env var override (highest priority)
+ env_val = os.environ.get("HERMES_TUI_MAX_TURNS")
+ if env_val:
+ return _resolve_turn_limit(env_val, default=default)
+ # Config file value — route through resolve_turn_limit so that
+ # "none"/"unlimited"/0 are first-class spellings, not int() crashes.
agent_cfg = cfg.get("agent") or {}
- return int(agent_cfg.get("max_turns") or cfg.get("max_turns") or default)
+ raw = agent_cfg.get("max_turns")
+ if raw is None:
+ raw = cfg.get("max_turns")
+ if raw is not None:
+ return _resolve_turn_limit(raw, default=default)
+ return default
def _parse_tui_skills_env() -> list[str]:
diff --git a/website/docs/user-guide/configuration.md b/website/docs/user-guide/configuration.md
index 05c70aafa6a..f64672c850b 100644
--- a/website/docs/user-guide/configuration.md
+++ b/website/docs/user-guide/configuration.md
@@ -1023,12 +1023,15 @@ Instead, when the budget is actually exhausted (500/500), Hermes injects one mes
```yaml
agent:
- max_turns: 500 # Max iterations per conversation turn (default: 500)
+ max_turns: 90 # Max iterations per conversation turn (default: 90)
+ # Set to "none", "unlimited", or 0 for no limit
api_max_retries: 3 # Retries per provider before fallback engages (default: 3)
```
When the iteration budget is fully exhausted, the CLI shows a notification to the user: `⚠ Iteration budget reached (500/500) — response may be incomplete`.
+`agent.max_turns` accepts the following "unlimited" spellings (case-insensitive): `"none"`, `"unlimited"`, `"infinite"`, `0`, `-1`. These resolve to a sentinel value (`sys.maxsize`) so the loop never exits on a turn count. Use this for long-running autonomous sessions where the turn budget would otherwise interrupt work.
+
`agent.api_max_retries` controls how many times Hermes retries a provider API call on transient errors (rate limits, connection drops, 5xx) **before** fallback-provider switching engages. The default is `3` — four attempts total. If you have [fallback providers](/user-guide/features/fallback-providers) configured and want to fail over faster, drop this to `0` so the first transient error on your primary immediately hands off to the fallback instead of churning retries against the flaky endpoint.
## Wall-Clock Run Budget