Skip to content

fix(gateway): refresh max_turns for fresh and cached agents without restart - #48834

Merged
teknium1 merged 4 commits into
mainfrom
hermes/hermes-dde3aaa6
Jun 19, 2026
Merged

fix(gateway): refresh max_turns for fresh and cached agents without restart#48834
teknium1 merged 4 commits into
mainfrom
hermes/hermes-dde3aaa6

Conversation

@teknium1

Copy link
Copy Markdown
Contributor

Summary

agent.max_turns bumps now take effect on a running gateway without a restart, for both freshly-created and cache-reused agents.

Root cause: two paths let a stale 90-turn cap survive a config.yaml change.

  1. run_sync() (and _run_background_task / api_server) read HERMES_MAX_ITERATIONS before _reload_runtime_env_preserving_config_authority() ran, so a stale .env ghost or pre-edit value shadowed config.yaml.
  2. A cache-reused agent never had its budget refreshed — it kept the max_iterations it was built with on the session's first turn.

Salvaged from #48127 (@infinitycrew39); the tautological cached-agent test was replaced with a real one.

Changes

  • gateway/run.py: new _current_max_iterations() (reload-then-read); both gateway agent-creation paths use it; cache-reuse refreshes agent.max_iterations (rebuilt into IterationBudget per turn by turn_context.py).
  • gateway/platforms/api_server.py: same reload-then-read.
  • tests/gateway/test_cached_agent_max_iterations.py: rewritten to exercise the real contracts (_init_cached_agent_for_turn leaves max_iterations alone; budget rebuild propagates a refreshed cap) instead of asserting a self-performed assignment.

Validation

  • E2E with real imports: stale HERMES_MAX_ITERATIONS=90 in env + config.yaml max_turns: 200_current_max_iterations() returns 200.
  • tests/gateway/test_cached_agent_max_iterations.py, test_runtime_env_reload_config_authority.py, test_api_server.py: 164 passed.

Closes #48127.

Infographic

gateway-max-turns-refresh

infinitycrew39 and others added 4 commits June 18, 2026 21:43
When a gateway agent is reused from cache, it retains the max_iterations
from its initial creation. If config.yaml agent.max_turns or HERMES_MAX_ITERATIONS
changed between turns, the cached agent's budget becomes stale.

Before reusing a cached agent, refresh agent.max_iterations from the
freshly-resolved value (read from env/config at line 14585).

Fixes partial issue from PR #48127: handles fresh agent creation + cached agent reuse.
Replaces the tautological test from the original PR (which asserted a
plain assignment it performed itself in the test body) with one that
exercises the actual contracts: _init_cached_agent_for_turn leaves
max_iterations untouched, and the per-turn IterationBudget rebuild
(turn_context.py) propagates a refreshed cap.
@github-actions

Copy link
Copy Markdown
Contributor

🔎 Lint report: hermes/hermes-dde3aaa6 vs origin/main

ruff

Total: 0 on HEAD, 0 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 0 pre-existing issues carried over.

ty (type checker)

Total: 11039 on HEAD, 11037 on base (🆕 +2)

🆕 New issues (2):

Rule Count
unresolved-attribute 2
First entries
run_agent.py:2971: [unresolved-attribute] unresolved-attribute: Object of type `Self@get_credits_spent_micros` has no attribute `_credits_session_start_micros`
tests/run_agent/test_credits_notices_toggle.py:76: [unresolved-attribute] unresolved-attribute: Unresolved attribute `_credits_session_start_micros` on type `AIAgent`

✅ Fixed issues (1):

Rule Count
invalid-assignment 1
First entries
tests/run_agent/test_credits_notices_toggle.py:76: [invalid-assignment] invalid-assignment: Object of type `None` is not assignable to attribute `_credits_session_start_micros` of type `int`

Unchanged: 5774 pre-existing issues carried over.

Diagnostics are surfaced as warnings — this check never fails the build.

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

@tonydwb tonydwb left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review Summary

Verdict: Approved — small gateway fix, no regressions.

  • 5 files changed (+163/-10)
  • Properly re-reads max_turns from config on fresh and cached agents without requiring restart
  • Clean duplication of config initialization path
  • No security concerns, no debug artifacts, tests updated appropriately

Reviewed by Hermes Agent

@teknium1
teknium1 merged commit 144834b into main Jun 19, 2026
35 checks passed
@teknium1
teknium1 deleted the hermes/hermes-dde3aaa6 branch June 19, 2026 13:31
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.

4 participants