The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(chat): --max-turns 0 / -1 means unlimited again on hermes chat and the TUI
Teknium395e5cc6590a
diff --git a/gateway/session_policy.py b/gateway/session_policy.pyindex b2a66cee138..ffb4c39f1d0 100644--- a/gateway/session_policy.py+++ b/gateway/session_policy.py@@ -128,6 +128,7 @@ def build_policy(params, config, *, private_secrets=None, profile_terminal=True)def _apply_launch_overrides(params, config):"""Validate the explicit model/agent launch overrides and fold them into *config*."""from urllib.parse import urlsplit+ from hermes_cli.config import resolve_turn_limitfrom hermes_constants import parse_reasoning_effortfor key in ('provider', 'base_url'):if key in params:@@ -144,10 +145,13 @@ def _apply_launch_overrides(params, config):if flag in params and type(params[flag]) is not bool:raise RuntimeStoreError('invalid_params')if 'max_turns' in params:+ # The spellings `--max-turns` always took: a positive cap, or 0 / -1 / "none" / "unlimited"+ # for no cap (resolve_turn_limit). Frozen normalized; anything unreadable is refused.value = params['max_turns']- if type(value) is not int or value <= 0:+ limit = resolve_turn_limit(value, default=0) if isinstance(value, (int, str)) else 0+ if not limit:raise RuntimeStoreError('invalid_params')- config.setdefault('agent', {})['max_turns'] = value+ config.setdefault('agent', {})['max_turns'] = limitif 'reasoning' in params:if not isinstance(params['reasoning'], str) or parse_reasoning_effort(params['reasoning']) is None:raise RuntimeStoreError('invalid_params')diff --git a/hermes_cli/gateway_chat.py b/hermes_cli/gateway_chat.pyindex 1cbdc869743..e17eae43913 100644--- a/hermes_cli/gateway_chat.py+++ b/hermes_cli/gateway_chat.py@@ -110,7 +110,7 @@ async def run_gateway_chat(args, emitter=None):if source not in contract.get("sources", []):raise GatewayClientError(f"Gateway does not support source {source!r}")parameters = contract.get("parameters", [])- policy = {key: getattr(args, key) for key in _POLICY if getattr(args, key, None) not in (None, False)}+ policy = _launch_flags(args)if create_if_missing:policy["title"] = titleif isinstance(policy.get("toolsets"), str):@@ -149,8 +149,14 @@ async def run_gateway_chat(args, emitter=None):_RESUME_POLICY_MISMATCH = "Resume retains gateway session policy; creation overrides are unsupported on resume."+def _launch_flags(args):+ """The creation flags the user passed. Identity, not equality: ``--max-turns 0`` (unlimited)+ is a value, while ``0 == False`` would drop it to the profile default."""+ return {key: value for key in _POLICY if (value := getattr(args, key, None)) is not None and value is not False}++def _requested_policy(args):- policy = {key: getattr(args, key) for key in _POLICY if getattr(args, key, None) not in (None, False)}+ policy = _launch_flags(args)if isinstance(policy.get("toolsets"), str):policy["toolsets"] = [name.strip() for name in policy["toolsets"].split(",") if name.strip()]if getattr(args, "source", None):diff --git a/tests/gateway/test_session_policy.py b/tests/gateway/test_session_policy.pyindex 9bea3825514..ceff0edd0fc 100644--- a/tests/gateway/test_session_policy.py+++ b/tests/gateway/test_session_policy.py@@ -73,12 +73,28 @@ def test_launch_options_are_frozen_and_validated(tmp_path):cfg['agent']['reasoning_effort'] = 'none'assert policy.reasoning_config == parse_reasoning_effort('high')assert build_policy(dict(cwd=str(tmp_path), ignore_rules=False), cfg).ignore_rules is False- for bad in ({'max_turns': True}, {'max_turns': 0}, {'reasoning': 'garbage'},+ for bad in ({'max_turns': True}, {'max_turns': 'garbage'}, {'max_turns': 2.5}, {'reasoning': 'garbage'},{'ignore_rules': 'false'}, {'base_url': 'http://user:secret@localhost/v1'}):with pytest.raises(RuntimeStoreError, match='invalid_params'):build_policy(dict(cwd=str(tmp_path), **bad), cfg)+def test_unlimited_max_turns_spellings_create_an_uncapped_session(tmp_path):+ """`--max-turns 0` / -1 / none / unlimited meant "no cap" before the gateway cutover: session.create+ freezes them as the unlimited budget, and `hermes chat` sends 0 instead of dropping it as falsy."""+ import argparse+ import sys+ from gateway.session_policy import build_policy+ from hermes_cli.gateway_chat import _launch_flags, _requested_policy+ cfg = {'agent': {'max_turns': 8}}+ for spelling in (0, -1, 'none', 'unlimited', '0'):+ assert build_policy(dict(cwd=str(tmp_path), max_turns=spelling), cfg).max_turns == sys.maxsize, spelling+ assert build_policy(dict(cwd=str(tmp_path), max_turns='12'), cfg).max_turns == 12+ for zero in (0, -1):+ args = argparse.Namespace(max_turns=zero, model=None, ignore_rules=False, yolo=False)+ assert _launch_flags(args) == _requested_policy(args) == {'max_turns': zero}++def test_explicit_key_is_private_and_missing_after_restart_fails_closed(tmp_path):import jsonfrom dataclasses import asdictdiff --git a/ui-tui/src/__tests__/canonicalGateway.test.ts b/ui-tui/src/__tests__/canonicalGateway.test.tsindex a1560e9c12a..d5d5e70947e 100644--- a/ui-tui/src/__tests__/canonicalGateway.test.ts+++ b/ui-tui/src/__tests__/canonicalGateway.test.ts@@ -17,6 +17,9 @@ it('rebuilds --max-turns from the launcher environment as the integer the sessioconst options = localCreationOptions({ HERMES_TUI_MAX_TURNS: '5', HERMES_MODEL: 'local-model' } as NodeJS.ProcessEnv)expect(options.max_turns).toBe(5)expect(localCreationOptions({} as NodeJS.ProcessEnv)).not.toHaveProperty('max_turns')+ // The pre-gateway "unlimited" spellings reach session.create as values the policy reads, never NaN/null.+ const wire = (value: string) => JSON.parse(JSON.stringify(localCreationOptions({ HERMES_TUI_MAX_TURNS: value } as NodeJS.ProcessEnv))).max_turns+ expect(['0', '-1', 'none', 'unlimited'].map(wire)).toEqual([0, -1, 'none', 'unlimited'])})it('carries `hermes --tui --yolo` (HERMES_YOLO_MODE) onto session.create as the frozen launch flag', () => {diff --git a/ui-tui/src/canonicalGateway.ts b/ui-tui/src/canonicalGateway.tsindex 83c252c7e4f..287e365cd4a 100644--- a/ui-tui/src/canonicalGateway.ts+++ b/ui-tui/src/canonicalGateway.ts@@ -86,9 +86,13 @@ export function localCreationOptions(env = process.env): Record<string, unknown>if (env.HERMES_TUI_TOOLSETS) { options.toolsets = env.HERMES_TUI_TOOLSETS.split(',') }- // The launcher exports `--max-turns 5` as the string "5"; the policy- // requires an int, so the environment's text form is undone here.- if (env.HERMES_TUI_MAX_TURNS) { options.max_turns = Number(env.HERMES_TUI_MAX_TURNS) }+ // The launcher exports `--max-turns 5` as the string "5": numbers ride as+ // ints (0 / -1 = unlimited), and a word ("none", "unlimited") stays text+ // for the policy to read — `Number` would turn it into NaN, sent as null.+ if (env.HERMES_TUI_MAX_TURNS) {+ const turns = Number(env.HERMES_TUI_MAX_TURNS)+ options.max_turns = Number.isInteger(turns) ? turns : env.HERMES_TUI_MAX_TURNS.trim()+ }// `hermes --tui --yolo` / `--ignore-rules` export HERMES_YOLO_MODE / HERMES_IGNORE_RULES (the in-process// TUI read them); over the canonical gateway they must ride session.create as frozen launch flags or the