Skip to content

fix(gateway): safely parse HERMES_MAX_ITERATIONS in runtime paths - #24143

Closed
Frowtek wants to merge 1 commit into
NousResearch:mainfrom
Frowtek:fix/gateway-max-iterations-hardening
Closed

Frowtek wants to merge 1 commit into
NousResearch:mainfrom
Frowtek:fix/gateway-max-iterations-hardening

Conversation

@Frowtek

@Frowtek Frowtek commented May 12, 2026

Copy link
Copy Markdown
Contributor

Summary

This fixes a gateway crash when HERMES_MAX_ITERATIONS contains an invalid value.

The gateway still had a few runtime paths using raw int(os.getenv(...)) parsing for HERMES_MAX_ITERATIONS. If the env var was malformed, startup logging, background-task execution, or normal message handling could fail with ValueError instead of falling back safely.

This patch aligns those gateway paths with the already-hardened behavior used elsewhere in the repo.

What Changed

  • added a small _max_iterations_env() helper in gateway/run.py
  • switched the remaining gateway runtime call-sites to use the helper
  • invalid, empty, or whitespace-only values now fall back to the default 90
  • malformed values now emit a warning instead of crashing the gateway

Validation

Scenario Before After
HERMES_MAX_ITERATIONS=not-a-number during gateway runtime ValueError could break startup log path or turn execution falls back to 90 and logs a warning
HERMES_MAX_ITERATIONS=" 42 " behavior depended on raw parsing at each call-site consistently resolves to 42
Gateway startup budget log could silently skip logging on parse failure always logs resolved budget using safe fallback
Background task / normal turn agent creation could crash on malformed env uses shared safe parser

Files Changed

  • gateway/run.py
  • tests/gateway/test_runtime_env_reload_config_authority.py

Tests

Added regression coverage for:

  • invalid HERMES_MAX_ITERATIONS values falling back to 90
  • valid whitespace-wrapped values still parsing correctly

Verification

pytest tests/gateway/test_runtime_env_reload_config_authority.py -q
4 passed in 3.55s

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery area/config Config system, migrations, profiles labels May 12, 2026
@kshitijk4poor

Copy link
Copy Markdown
Contributor

Closing as already resolved on main.

The HERMES_MAX_ITERATIONS casts this PR targets are already guarded on current main via the _current_max_iterations() helper in gateway/run.py, which wraps the parse in try/except (TypeError, ValueError) and is wired into all three gateway/run.py sites plus api_server.py. The malformed-value crash this PR fixes no longer reproduces there.

The remaining malformed-env-var crash sites across the rest of the gateway/agent were just swept up in #49558 (canonical utils.env_int/env_float conversion):
#49558

Thanks for the fix — appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/config Config system, migrations, profiles comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants