fix(gateway): refresh max_turns for fresh and cached agents without restart - #48834
Merged
Conversation
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.
Contributor
🔎 Lint report:
|
| 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.
tonydwb
approved these changes
Jun 19, 2026
tonydwb
left a comment
There was a problem hiding this comment.
Code Review Summary
Verdict: Approved — small gateway fix, no regressions.
- 5 files changed (+163/-10)
- Properly re-reads
max_turnsfrom 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
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
agent.max_turnsbumps 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.yamlchange.run_sync()(and_run_background_task/ api_server) readHERMES_MAX_ITERATIONSbefore_reload_runtime_env_preserving_config_authority()ran, so a stale.envghost or pre-edit value shadowedconfig.yaml.max_iterationsit 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 refreshesagent.max_iterations(rebuilt intoIterationBudgetper turn byturn_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_turnleavesmax_iterationsalone; budget rebuild propagates a refreshed cap) instead of asserting a self-performed assignment.Validation
HERMES_MAX_ITERATIONS=90in 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