fix(agent): restore global model.context_length override on /model switch - #41010
Closed
ech0hol wants to merge 1 commit into
Closed
fix(agent): restore global model.context_length override on /model switch#41010ech0hol wants to merge 1 commit into
ech0hol wants to merge 1 commit into
Conversation
switch_model() clears _config_context_length at line 1411 to prevent stale per-model overrides from leaking to the new model. However it never re-reads the global model.context_length from config.yaml, causing the user-configured override to be silently dropped and never take effect again for the remainder of the session. Restore the global override from load_config() inside the existing try/except block that already re-reads custom_providers. The restore runs after get_compatible_custom_providers() so the priority order in get_model_context_length remains correct: global override (prio 0) takes precedence over per-model custom_providers overrides (prio 0b), matching agent_init.py behaviour. Closes NousResearch#40979.
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.
What does this PR do?
When a user sets a global
model.context_lengthinconfig.yamland later switches models mid-session via/model, the global override is silently dropped and never takes effect again for the remainder of the session.Root cause:
switch_model()unconditionally clearsagent._config_context_length = Noneat line 1411 to prevent stale per-model overrides from leaking to the new model. But after the swap, the code only re-readscustom_providers(per-model overrides, priority 0b) from config — it never restores the globalmodel.context_length(priority 0).Fix: After
load_config()andget_compatible_custom_providers()in the existingtry/exceptblock, readmodel.context_lengthfrom the fresh config and write it back toagent._config_context_length. This is the same value thatagent_init.pypasses to the agent at startup, so the behaviour after a switch matches a fresh session.Related Issue
Fixes #40979
Type of Change
Changes Made
agent/agent_runtime_helpers.py— inswitch_model(), restore the globalmodel.context_lengthoverride fromload_config()after the per-modelcustom_providersoverrides have been re-read (lines 1523–1534, +12 lines, no deletions)The new code sits inside the existing
try/except Exceptionblock so a malformed config falls through gracefully without blocking the switch.How to Test
~/.hermes/config.yaml:gpt-4o)./debugor check state): context length = 64000./model claude-sonnet-4-20250514(or any other model).Run the relevant tests:
pytest tests/run_agent/test_switch_model_context.py \ tests/run_agent/test_switch_model_rollback.py \ tests/run_agent/test_switch_model_fallback_prune.py -vAll 10 tests pass with no regressions.
Checklist
Code
fix:,feat:, etc.)pytest tests/run_agent/test_switch_model_*.pyand all tests pass