Skip to content

fix(agent): restore global model.context_length override on /model switch - #41010

Closed
ech0hol wants to merge 1 commit into
NousResearch:mainfrom
ech0hol:fix/40979-restore-context-length-on-switch
Closed

fix(agent): restore global model.context_length override on /model switch#41010
ech0hol wants to merge 1 commit into
NousResearch:mainfrom
ech0hol:fix/40979-restore-context-length-on-switch

Conversation

@ech0hol

@ech0hol ech0hol commented Jun 7, 2026

Copy link
Copy Markdown

What does this PR do?

When a user sets a global model.context_length in config.yaml and 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 clears agent._config_context_length = None at line 1411 to prevent stale per-model overrides from leaking to the new model. But after the swap, the code only re-reads custom_providers (per-model overrides, priority 0b) from config — it never restores the global model.context_length (priority 0).

Fix: After load_config() and get_compatible_custom_providers() in the existing try/except block, read model.context_length from the fresh config and write it back to agent._config_context_length. This is the same value that agent_init.py passes to the agent at startup, so the behaviour after a switch matches a fresh session.

Related Issue

Fixes #40979

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)

Changes Made

  • agent/agent_runtime_helpers.py — in switch_model(), restore the global model.context_length override from load_config() after the per-model custom_providers overrides have been re-read (lines 1523–1534, +12 lines, no deletions)

The new code sits inside the existing try/except Exception block so a malformed config falls through gracefully without blocking the switch.

How to Test

  1. Add a global context length override to ~/.hermes/config.yaml:
    model:
      context_length: 64000
  2. Start a session with any model (e.g. gpt-4o).
  3. Verify the compressor has the override (/debug or check state): context length = 64000.
  4. Run /model claude-sonnet-4-20250514 (or any other model).
  5. Verify the compressor still reports context length = 64000 — before this fix, it would fall back to the model's default.

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 -v

All 10 tests pass with no regressions.

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix:, feat:, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/run_agent/test_switch_model_*.py and all tests pass
  • I've added tests for my changes — existing tests already cover both the "old override must not leak" and "no override = None" paths; the fix is a config-read restoration that is transparent to the test suite
  • I've tested on my platform: Windows 11

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.
@alt-glitch alt-glitch added type/bug Something isn't working comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint area/config Config system, migrations, profiles P2 Medium — degraded but workaround exists labels Jun 7, 2026
@ech0hol ech0hol changed the title fix: restore global model.context_length override on /model switch fix(agent): restore global model.context_length override on /model switch Jun 7, 2026
@ech0hol ech0hol closed this Jun 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/config Config system, migrations, profiles comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint 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.

model.context_length global config override is not re-read after /model switch

2 participants