fix: preserve model.context_length on model switch - #62552
Closed
yingliang-zhang wants to merge 2 commits into
Closed
fix: preserve model.context_length on model switch#62552yingliang-zhang wants to merge 2 commits into
yingliang-zhang wants to merge 2 commits into
Conversation
Collaborator
teknium1
reviewed
Jul 11, 2026
teknium1
left a comment
Contributor
There was a problem hiding this comment.
Thanks — the runtime premise is confirmed: current main clears agent._config_context_length at agent/agent_runtime_helpers.py:1814, while get_model_context_length() honors that value before every detection/default path at agent/model_metadata.py:2051-2053.
Problems
agent/agent_runtime_helpers.py:1820only preserves values already typedintorfloat. Startup accepts numeric strings viaint(value)atagent/agent_init.py:1635-1651; thereforecontext_length: "131072"works initially but would be cleared after this PR's switch path.- The PR closes #41944 although the separate Desktop write path still removes
context_lengthon current main athermes_cli/web_server.py:1113; companion PR #41965 remains open.
Suggested changes
- Match startup coercion at
agent/agent_runtime_helpers.py:1820and add a quoted-numeric regression test. - Reference #41944 without closing it unless #41965 lands together.
Automated hermes-sweeper review.
switch_model() cleared _config_context_length to None, causing get_model_context_length() to fall through to hardcoded catalog defaults instead of honoring the user's explicit config. model.context_length is a global setting (endpoint capacity), not a per-model attribute. Re-read it from config on every model switch so the override survives. When config has no model.context_length, fall back to None so per-model custom_providers resolution still works. Closes NousResearch#41944
yingliang-zhang
force-pushed
the
fix/preserve-config-context-length-on-model-switch
branch
from
July 23, 2026 07:11
eb2d50e to
3a28c0a
Compare
Contributor
Author
|
Closing as fixed on main via a different mechanism: model switch now clears the stale per-config context_length override so the new model's window is resolved fresh ( |
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
Root Cause
agent_runtime_helpers.py line 1814 unconditionally set agent._config_context_length = None during model switch. This cleared the config-level override, causing get_model_context_length() step 0 (explicit config override) to be skipped, falling through to step 8 (hardcoded defaults).
For example, a user with model.context_length: 340000 and model.default: glm-5.2-heavy would see the context length jump to 1,048,576 (hardcoded catalog match) after any model switch, completely ignoring their config.
This differs from PR #41965 which addresses the Desktop model picker clearing context_length in web_server.py. This PR fixes the agent-side switch_model() path.
Testing
scripts/run_tests.sh tests/run_agent/test_switch_model_context.py -v --tb=short
2 tests passed, 0 failed
Updated test_switch_model_clears_previous_config_context_length to test_switch_model_preserves_config_context_length to verify config re-read behavior.
Closes #41944