Skip to content

fix(gateway): refresh cached agent max_iterations after per-turn config reload - #32544

Closed
Zyrixtrex wants to merge 1 commit into
NousResearch:mainfrom
Zyrixtrex:fix/gateway-refresh-cached-agent-max-turns
Closed

fix(gateway): refresh cached agent max_iterations after per-turn config reload#32544
Zyrixtrex wants to merge 1 commit into
NousResearch:mainfrom
Zyrixtrex:fix/gateway-refresh-cached-agent-max-turns

Conversation

@Zyrixtrex

Copy link
Copy Markdown
Contributor

Summary

This fixes a gateway stale-state bug where agent.max_turns changes were not applied reliably on the next turn.

In the cached-agent path, the gateway could continue using an outdated max_iterations value even after the per-turn config/env reload completed.

Fixes #

Changes

  • read HERMES_MAX_ITERATIONS after _reload_runtime_env_preserving_config_authority()
  • refresh max_iterations when reusing a cached AIAgent
  • add regression coverage for cached-turn budget refresh

Files

  • gateway/run.py
  • tests/gateway/test_agent_cache.py

Validation

Ran locally:

python -m pytest tests/gateway/test_agent_cache.py -q --timeout-method=thread
python -m pytest tests/gateway/test_runtime_env_reload_config_authority.py -q --timeout-method=thread

Passed:

tests/gateway/test_agent_cache.py
tests/gateway/test_runtime_env_reload_config_authority.py

@alt-glitch alt-glitch added P2 Medium — degraded but workaround exists type/bug Something isn't working comp/gateway Gateway runner, session dispatch, delivery labels May 26, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Related to merged #21240 (preserve max_turns after env reload). This PR addresses a remaining edge case: the cached-agent path where max_iterations isn't refreshed after per-turn config reload, even though the env reload itself was fixed. Also related to open #17306 and #18230.

@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Closing as already resolved on current main.

This PR's specific fix — refreshing a reused cached agent's max_iterations from current config each turn — is already implemented in the cached-agent reuse path of gateway/run.py:

self._init_cached_agent_for_turn(agent, _interrupt_depth)
# Refresh agent max_iterations from current config
# (cached agent may have been created with old config)
agent.max_iterations = max_iterations

where max_iterations comes from _current_max_iterations() (which reloads runtime env while preserving config.yaml's agent.max_turns). So a cached agent no longer keeps a stale budget after a config/env reload. Thanks for catching and documenting the cached-turn staleness.

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

Labels

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