Skip to content

fix(gateway): refresh runtime max_turns before agent creation - #48127

Closed
infinitycrew39 wants to merge 4 commits into
NousResearch:mainfrom
infinitycrew39:fix/max-turns-runtime-budget
Closed

fix(gateway): refresh runtime max_turns before agent creation#48127
infinitycrew39 wants to merge 4 commits into
NousResearch:mainfrom
infinitycrew39:fix/max-turns-runtime-budget

Conversation

@infinitycrew39

Copy link
Copy Markdown
Contributor

What does this PR do?

This PR fixes a runtime budget bug where long-lived gateway and api_server processes could continue using a stale 90-turn iteration cap even after agent.max_turns was increased in config.yaml.

The root cause was that some request paths read HERMES_MAX_ITERATIONS before refreshing the config-authoritative runtime env, while api_server bypassed that refresh path entirely.

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✅ Tests (adding or improving test coverage)

Changes Made

  • Added a shared runtime helper in gateway/run.py that refreshes config/.env before resolving the current iteration budget.
  • Updated the main gateway run path and background-task path to use the refreshed budget helper.
  • Updated gateway/platforms/api_server.py to use the same helper instead of reading a stale env snapshot directly.
  • Added regression tests covering helper ordering and api_server agent creation.

How to Test

  1. Start a long-lived gateway/api_server process with stale HERMES_MAX_ITERATIONS=90.
  2. Set agent.max_turns: 200 in ~/.hermes/config.yaml without restarting the process.
  3. Send a new request and verify the agent budget resolves to 200 instead of staying at 90.

Notes

Targeted pytest execution was not possible in this workspace because no project virtualenv is present; modified files were validated with python3 -m py_compile.

@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 18, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Related: #32544 — both fix stale HERMES_MAX_ITERATIONS shadowing config.yaml agent.max_turns, but via different code paths. #32544 fixes the cached-agent refresh path (_init_cached_agent_for_turn); this PR adds a shared gateway/run.py budget helper and fixes the api_server.py path (max_iterations = int(os.getenv("HERMES_MAX_ITERATIONS", "90")) with no prior reload — still live on main). Same goal, complementary mechanisms — not a duplicate. Lineage: #21240, #18764.

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 NousResearch#48127: handles fresh agent creation + cached agent reuse.
Add 5 test cases covering:
- Cached agent max_iterations refreshed from HERMES_MAX_ITERATIONS env
- Refresh works even for interrupt-recursive turns (depth > 0)
- Session state is preserved during refresh
- Integration: env config changes propagate to cached agent on reuse

Tests can be run via: scripts/run_tests.sh tests/gateway/test_cached_agent_max_iterations.py

Fix: correct static method access in fixture (remove __func__)
@infinitycrew39
infinitycrew39 force-pushed the fix/max-turns-runtime-budget branch from 5f83677 to 6915c9a Compare June 19, 2026 04:08
teknium1 pushed a commit that referenced this pull request Jun 19, 2026
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.
@teknium1

Copy link
Copy Markdown
Contributor

Merged via PR #48834 — your two fix commits were cherry-picked onto current main with your authorship preserved (rebase-merge). The tautological cached-agent test was swapped for one that exercises the real contracts. Live-validated against origin/main: a cache-reused agent was stuck at 90 despite config.yaml max_turns=200; the fix refreshes it to 200. Thanks for the fix and the clear root-cause writeup! #48834

xyshanren pushed a commit to xyshanren/hermes-agent-cn that referenced this pull request Jun 25, 2026
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 NousResearch#48127: handles fresh agent creation + cached agent reuse.
waefrebeorn pushed a commit to waefrebeorn/slermes that referenced this pull request Jul 2, 2026
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 NousResearch#48127: handles fresh agent creation + cached agent reuse.
habarmc1223-sudo pushed a commit to habarmc1223-sudo/hermes-agent-fluxmem that referenced this pull request Jul 8, 2026
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 NousResearch#48127: handles fresh agent creation + cached agent reuse.
santhreal pushed a commit to santhreal/hermes-agent that referenced this pull request Jul 13, 2026
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 NousResearch#48127: handles fresh agent creation + cached agent reuse.
Gravezzz pushed a commit to Gravezzz/hermes-agent that referenced this pull request Jul 21, 2026
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 NousResearch#48127: handles fresh agent creation + cached agent reuse.
leewenjie pushed a commit to leewenjie/hermes-agent that referenced this pull request Aug 7, 2026
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 NousResearch#48127: handles fresh agent creation + cached agent reuse.
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