Skip to content

The agent that grows with you

feat(gateway,cli): `--run-budget SECONDS` reaches the gateway session's agent

Teknium2a33f64c7d3d
diff --git a/gateway/session_execution.py b/gateway/session_execution.py
index 80d0da46479..d8497a4dc51 100644
--- a/gateway/session_execution.py
+++ b/gateway/session_execution.py
@@ -180,6 +180,9 @@ class GatewaySessionAgentMixin:
load_soul_identity=not bool(policy and policy.ignore_rules),
skip_memory=bool(policy and policy.ignore_rules),
pass_session_id=bool(policy and policy.pass_session_id),
+ # A local session's frozen budget (`--run-budget` / its profile's agent.run_budget_seconds);
+ # None leaves the agent reading the owner's config, as before.
+ run_budget_seconds=policy.run_budget_seconds if policy else None,
)
def _resolve_turn_agent(self, turn_route, platform_key, combined_ephemeral, max_iterations, reasoning_config, pr):
diff --git a/gateway/session_policy.py b/gateway/session_policy.py
index eb31fd2f683..a86608319d9 100644
--- a/gateway/session_policy.py
+++ b/gateway/session_policy.py
@@ -11,7 +11,7 @@ CREATE_FIELDS = frozenset({'request_id', 'source', 'cwd', 'model', 'toolsets',
'provider', 'base_url', 'reasoning', 'max_turns', 'ignore_rules', 'api_key', 'editor',
'yolo', 'safe_mode', 'ignore_user_config',
'skills', 'checkpoints', 'accept_hooks', 'pass_session_id', 'follow_profile_config',
- 'service_tier', 'cwd_explicit'})
+ 'service_tier', 'cwd_explicit', 'run_budget'})
# Sections a ``follow_profile_config`` session re-reads from the owning profile's CURRENT config each
# turn (main's Bot Chat row), credentials included: a frozen key never crosses a profile route change.
FOLLOW_SECTIONS = ('model', 'providers', 'custom_providers', 'fallback_providers', 'fallback_model', 'model_aliases',
@@ -190,6 +190,12 @@ class LocalSessionPolicy:
cfg = self.config()
return resolve_turn_limit(cfg.get('agent', {}).get('max_turns', cfg.get('max_turns')))
+ @property
+ def run_budget_seconds(self):
+ """Frozen `agent.run_budget_seconds` (`--run-budget` folds into it); None = no budget."""
+ from agent.agent_init import _normalize_run_budget_seconds
+ return _normalize_run_budget_seconds(self.config().get('agent', {}).get('run_budget_seconds'))
+
@property
def reasoning_config(self):
from hermes_constants import resolve_reasoning_config
@@ -321,6 +327,13 @@ def _apply_launch_overrides(params, config):
if tier is None:
raise RuntimeStoreError('invalid_params')
config.setdefault('agent', {})['service_tier'] = tier or 'normal'
+ if 'run_budget' in params:
+ # `--run-budget SECONDS`: the wall-clock budget per run (80% wrap-up notice, capped provider
+ # timeouts), frozen as the session's `agent.run_budget_seconds` like main's CLI flag over config.
+ value = params['run_budget']
+ if isinstance(value, bool) or not isinstance(value, (int, float)) or not value > 0:
+ raise RuntimeStoreError('invalid_params')
+ config.setdefault('agent', {})['run_budget_seconds'] = float(value)
if params.get('checkpoints'):
section = config.get('checkpoints')
section = dict(section) if isinstance(section, dict) else {'enabled': bool(section)} if section else {}
diff --git a/hermes_cli/cli_init_mixin.py b/hermes_cli/cli_init_mixin.py
index 440dcfd85ce..f966d24be1c 100644
--- a/hermes_cli/cli_init_mixin.py
+++ b/hermes_cli/cli_init_mixin.py
@@ -208,6 +208,7 @@ class CLIInitMixin:
os.getenv("HERMES_MAX_ITERATIONS"),
))
self.run_budget_seconds = run_budget if run_budget is not None else CLI_CONFIG["agent"].get("run_budget_seconds")
+ self._explicit_run_budget = run_budget # `--run-budget`: session.create policy (the gateway runs the turn)
def _init_toolsets(self, toolsets):
from cli import CLI_CONFIG, validate_toolset
@@ -368,7 +369,8 @@ class CLIInitMixin:
"base_url": getattr(self, "_explicit_base_url", None),
"toolsets": self.enabled_toolsets,
"pass_session_id": getattr(self, "pass_session_id", False) or None,
- "ignore_rules": getattr(self, "ignore_rules", False) or None}
+ "ignore_rules": getattr(self, "ignore_rules", False) or None,
+ "run_budget": getattr(self, "_explicit_run_budget", None)}
sid = store.open_session(self, launch)
self._gateway_session_open = True
if sid and sid != self.session_id:
diff --git a/hermes_cli/gateway_session_store.py b/hermes_cli/gateway_session_store.py
index c690767dd67..92a6d9461d4 100644
--- a/hermes_cli/gateway_session_store.py
+++ b/hermes_cli/gateway_session_store.py
@@ -359,6 +359,8 @@ def _creation_policy(launch):
fields["toolsets"] = ",".join(str(item) for item in fields["toolsets"])
policy = _requested_policy(argparse.Namespace(**fields, source=None))
policy.pop("source", None)
+ if launch.get("run_budget") is not None: # main's `--run-budget` (the thin client refuses it)
+ policy["run_budget"] = float(launch["run_budget"])
policy.update(env_launch_flags())
return policy
diff --git a/tests/gateway/test_session_policy.py b/tests/gateway/test_session_policy.py
index f3f3cbb24a7..6b316fc9a95 100644
--- a/tests/gateway/test_session_policy.py
+++ b/tests/gateway/test_session_policy.py
@@ -346,3 +346,17 @@ def test_lazy_info_reports_the_free_tier_pinned_model(tmp_path, monkeypatch):
assert _lazy_model(authority, build_policy({'cwd': str(tmp_path), 'model': 'pick'}, {})) == 'pick'
monkeypatch.setattr(anon_auth, 'free_tier_route', lambda: False)
assert _lazy_model(authority, configured) == 'cfg-model'
+
+
+def test_run_budget_is_a_frozen_launch_option(tmp_path):
+ """`hermes chat --run-budget N` (main: AIAgent(run_budget_seconds=N)): frozen as the session's
+ agent.run_budget_seconds; the profile's own value applies without the flag; junk is refused."""
+ from gateway.session_policy import build_policy
+ from hermes_state_runtime import RuntimeStoreError
+ cfg = {'agent': {'run_budget_seconds': 300}}
+ assert build_policy(dict(cwd=str(tmp_path), run_budget=120), cfg).run_budget_seconds == 120.0
+ assert build_policy(dict(cwd=str(tmp_path)), cfg).run_budget_seconds == 300.0
+ assert build_policy(dict(cwd=str(tmp_path)), {}).run_budget_seconds is None
+ for bad in (0, -5, True, 'soon'):
+ with pytest.raises(RuntimeStoreError, match='invalid_params'):
+ build_policy(dict(cwd=str(tmp_path), run_budget=bad), cfg)
diff --git a/tests/hermes_cli/test_gateway_session_store.py b/tests/hermes_cli/test_gateway_session_store.py
index e4dfe71e480..6ebd3c4be71 100644
--- a/tests/hermes_cli/test_gateway_session_store.py
+++ b/tests/hermes_cli/test_gateway_session_store.py
@@ -337,3 +337,9 @@ def test_create_if_missing_titles_a_gateway_created_session(store, monkeypatch):
assert gss.create_titled_session("Fresh Named") == "local-new"
method, params = [c for c in client.calls if c[0] == "session.create"][-1]
assert params["title"] == "Fresh Named" and params["source"] and params["cwd"]
+
+
+def test_run_budget_flag_rides_session_create():
+ from hermes_cli.gateway_session_store import _creation_policy
+ assert _creation_policy({"run_budget": 90})["run_budget"] == 90.0
+ assert "run_budget" not in _creation_policy({"run_budget": None})