fix(config): re-read model.context_length from config after /model switch (#40979) - #41270
Open
rodboev wants to merge 4 commits into
Open
fix(config): re-read model.context_length from config after /model switch (#40979)#41270rodboev wants to merge 4 commits into
rodboev wants to merge 4 commits into
Conversation
tonydwb
approved these changes
Jun 7, 2026
tonydwb
left a comment
There was a problem hiding this comment.
Code Review Summary (PR #41270)
Verdict: Approved
Looks Good
- Fixes issue #40979:
model.context_lengthfrom config.yaml is re-read after/modelswitch and persisted into primary runtime state - Handles both global
model.context_lengthand per-modelcustom_providerscontext_length restore_primary_runtimenow restores_config_context_lengthand_custom_providers(deep copy)- Tests cover: restore after fallback, re-read on switch, custom_providers persistence
Reviewed by Hermes Agent
rodboev
force-pushed
the
pr/config-context-length-model-switch
branch
from
June 28, 2026 19:56
9c5daa7 to
2c4ab7a
Compare
rodboev
force-pushed
the
pr/config-context-length-model-switch
branch
from
July 7, 2026 05:20
aba4990 to
ba94f5e
Compare
teknium1
reviewed
Jul 14, 2026
teknium1
left a comment
Contributor
There was a problem hiding this comment.
Thanks for tracing the global override through the /model path; current main still clears _config_context_length at agent/agent_runtime_helpers.py:1848 and then passes the unset value at :2056.
Problems
- The new restore assignments need corresponding initial snapshot entries.
agent/agent_init.py:2105-2123does not storeconfig_context_lengthorcustom_providers. Since fallback activation leaves_custom_providersintact (agent/chat_completion_helpers.py:1502-1636), restoringrt.get("custom_providers")after a fallback before any/modelwould replace it withNone. - The added restore test pre-populates those keys, so it misses that initial-session path. The config test also mocks
load_config; a temporary-HERMES_HOMEintegration test would cover the required propagation path.
Suggested changes
- Snapshot both fields during initialization and add a fallback-without-
/modelpreservation test. - Add a real config-loading regression test for the global override.
Automated hermes-sweeper review.
rodboev
force-pushed
the
pr/config-context-length-model-switch
branch
from
July 14, 2026 13:06
1a675c4 to
e62c894
Compare
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
When a user sets
model.context_lengthinconfig.yaml, the override is read at session start inagent_init.pyand stored asagent._config_context_length. This value controls the compression threshold, how aggressively Hermes compresses history to stay within the context window.When
/modelis used to switch models mid-session,switch_model()inagent/agent_runtime_helpers.pyclears_config_context_lengthtoNone, correctly discarding any stale value from the previous model. It then re-readscustom_providersper-model context lengths from config, but did not re-read the globalmodel.context_lengthkey. The override was silently lost: the session continued with auto-detected context lengths for all subsequent turns, ignoring the user's explicit setting.This PR extends the config re-read block inside
switch_model()to also extractmodel.context_lengthand re-apply it before callingget_model_context_length(). The follow-up fixes keep that same context state consistent across the runtime lifecycle:/modelpersists the refreshed_config_context_lengthand live_custom_providersinto_primary_runtime, initialization snapshots those fields before any/modelswitch can happen, andrestore_primary_runtime()restores both fields when the agent exits fallback.Changes
agent/agent_runtime_helpers.py: re-readmodel.context_lengthduringswitch_model(), persist the refreshed_config_context_lengthand_custom_providersinto_primary_runtime, and restore those fields inrestore_primary_runtime()agent/agent_init.py: include the init-timeconfig_context_lengthand a deep copy ofcustom_providersin the primary runtime snapshottests/run_agent/test_switch_model_context.py: cover real temporary-HERMES_HOMEconfig loading for the global override and refreshed custom-provider persistence after/modeltests/run_agent/test_primary_runtime_restore.py: cover init snapshot contents, copy isolation, fallback before any/modelswitch, restore success, fallback reset, and restored context stateValidation
model.context_length: 65536set, no/modelswitchmodel.context_length: 65536set,/model new-modelcalledconfig.yaml, passed toget_model_context_length, and persisted into_primary_runtimemodel.context_lengthnot set,/modelcalledNonepassedNonepassed, unchangedcustom_providersper-modelcontext_length,/modelcalled/modelswitch_config_context_lengthand a copied_custom_providerslist/modelswitch followed by fallback/restore_config_context_lengthand_custom_providersrestored correctlyTest plan
pytest tests/run_agent/test_switch_model_context.py tests/run_agent/test_primary_runtime_restore.py -v --timeout=0— 41 focused tests passWhy it's low-risk
The change stays scoped to
/model, initialization, and runtime restore paths, and it only preserves state the branch already computes. Sessions withoutmodel.context_lengthset keepNone; sessions without live custom providers snapshot and restoreNone.Upstream
Closes #40979.
Reported by @bonaluo.