Skip to content

The agent that grows with you

fix(gateway): startup "Agent budget" line reports the enforced max_turns

teknium1e987003f1c1c
diff --git a/gateway/run_startup.py b/gateway/run_startup.py
index 8baedc72892..c151df0f15b 100644
--- a/gateway/run_startup.py
+++ b/gateway/run_startup.py
@@ -40,6 +40,17 @@ logger = logging.getLogger("gateway.run")
class GatewayStartupMixin:
"""Startup sequence, resume/restore and handoff methods for GatewayRunner."""
+ @staticmethod
+ def _log_agent_budget() -> None:
+ """Report the ENFORCED per-turn budget: ``agent.max_turns`` is bridged into
+ ``HERMES_MAX_ITERATIONS`` before this runs, so the env slot already carries the config value;
+ resolve it the same way the turn loop does (``none``/``unlimited`` spellings included) instead of
+ ``int()`` on the raw string with an invented ``500`` default (#116888)."""
+ from hermes_cli.config import TURN_LIMIT_UNLIMITED, resolve_turn_limit
+ limit = resolve_turn_limit(os.getenv("HERMES_MAX_ITERATIONS"))
+ logger.info("Agent budget: max_iterations=%s (agent.max_turns from config.yaml, else the HERMES_MAX_ITERATIONS bridge)",
+ "unlimited" if limit == TURN_LIMIT_UNLIMITED else limit)
+
# A configured platform failed non-retryably this boot and is parked: every "we are serving"
# status stamp (startup, drain release, scale-to-zero wake) must say ``degraded``, not ``running``.
_startup_parked_platforms: bool = False
@@ -803,13 +814,7 @@ class GatewayStartupMixin:
disarm_startup_watchdog()
logger.info("Session storage: %s", self.config.sessions_dir)
self._start_log_systemd_timing_alignment()
- # Log the resolved max_iterations so operators can verify the config.yaml → env bridge.
- with suppress(Exception):
- logger.info(
- "Agent budget: max_iterations=%d (agent.max_turns from config.yaml, "
- "or HERMES_MAX_ITERATIONS from .env, or default 500)",
- int(os.getenv("HERMES_MAX_ITERATIONS", "500")),
- )
+ self._log_agent_budget()
# Warn prominently when redaction is opted out; the redactor snapshots its state at import time,
# so this line is the source of truth for the process lifetime.
with suppress(Exception):
diff --git a/tests/gateway/test_startup_agent_budget_log.py b/tests/gateway/test_startup_agent_budget_log.py
new file mode 100644
index 00000000000..d14c8585ea0
--- /dev/null
+++ b/tests/gateway/test_startup_agent_budget_log.py
@@ -0,0 +1,36 @@
+"""The startup "Agent budget" line reports the budget the turn loop enforces (#116888)."""
+
+import logging
+
+import pytest
+
+from gateway.run import GatewayRunner, _bridge_max_turns_to_env, _current_max_iterations
+from hermes_cli.config import TURN_LIMIT_UNLIMITED
+
+
+ ("max_turns", "expected"),
+ [({"max_turns": "unlimited"}, "unlimited"), ({"max_turns": 50}, "50"), ({}, "90")],
+)
+def test_agent_budget_line_matches_enforced_limit(monkeypatch, max_turns, expected):
+ monkeypatch.setenv("HERMES_MAX_ITERATIONS", "90") # .env value: config wins when set, else it is the legacy bridge
+ monkeypatch.setattr("gateway.run._reload_runtime_env_preserving_config_authority", lambda: None)
+ monkeypatch.setattr("gateway.run.get_hermes_home_override", lambda: None)
+ _bridge_max_turns_to_env(max_turns)
+ enforced = _current_max_iterations()
+ records = []
+ handler = logging.Handler()
+ handler.emit = records.append
+ startup_logger = logging.getLogger("gateway.run")
+ startup_logger.addHandler(handler)
+ old_level = startup_logger.level
+ startup_logger.setLevel(logging.INFO)
+ try:
+ GatewayRunner._log_agent_budget()
+ finally:
+ startup_logger.removeHandler(handler)
+ startup_logger.setLevel(old_level)
+ line = [r.getMessage() for r in records if "Agent budget" in r.getMessage()]
+ assert line, records
+ assert f"max_iterations={expected}" in line[0]
+ assert ("unlimited" in line[0]) == (enforced == TURN_LIMIT_UNLIMITED)