Skip to content

fix(agent): pass config_context_length in fallback activation path#14727

Closed
CruxExperts wants to merge 1 commit into
NousResearch:mainfrom
CruxExperts:fix/fallback-context-length-config-override
Closed

fix(agent): pass config_context_length in fallback activation path#14727
CruxExperts wants to merge 1 commit into
NousResearch:mainfrom
CruxExperts:fix/fallback-context-length-config-override

Conversation

@CruxExperts

@CruxExperts CruxExperts commented Apr 23, 2026

Copy link
Copy Markdown

What does this PR do?

Fixes a bug where _try_activate_fallback() calls get_model_context_length() without the config_context_length parameter. This causes the fallback activation to ignore the explicit model.context_length value in config.yaml and instead fall through to DEFAULT_FALLBACK_CONTEXT (128K).

Root cause

In run_agent.py, the switch_model() method (line 1988) correctly passes config_context_length=getattr(self, '_config_context_length', None) when calling get_model_context_length(). However, _try_activate_fallback() (line 6435) was missing this parameter.

The resolution chain in get_model_context_length() is:

  • Step 0: If config_context_length is passed → return it immediately (respects config.yaml)
  • Steps 1-8: Various probes (cache, endpoint, providers)
  • Step 9: DEFAULT_FALLBACK_CONTEXT (128K) — the fallback for everything that fails

Without config_context_length passed, a fallback activation that fails all probes lands at step 9 → 128K, overwriting the correct 204800 value from config.

The crash cascade

  1. Fallback model activates
  2. get_model_context_length() called without config override → returns 128K
  3. context_compressor.update_model(128K) overwrites the correct 204800
  4. Status bar now shows 128K instead of 204.8K
  5. Actual context grows past 128K → context exceeded error
  6. Auto-compression fires on a corrupted session state → amnesia

Changes

run_agent.py — line ~6438

 fb_context_length = get_model_context_length(
     self.model, base_url=self.base_url,
     api_key=self.api_key, provider=self.provider,
+    config_context_length=getattr(self, "_config_context_length", None),
 )

This mirrors the existing correct behavior in switch_model() at line 1988.

Risk

Very low:

  • One-line addition, identical to what switch_model() already does
  • Only affects the fallback activation path (error recovery)
  • Does not change behavior for successful model switches

Testing

  • Manual: Switch to MiniMax-M2.7, trigger a fallback (e.g. API error), verify status bar still shows 204.8K not 128K
  • Automated: Add a unit test that calls _try_activate_fallback() with a mocked _config_context_length and verifies get_model_context_length is called with that parameter

Fixes: context_length forced to 128K on fallback activation even when config.yaml has a higher value

Try to activate fallback model after errors was calling get_model_context_length()
without the config_context_length parameter, causing it to fall through to
DEFAULT_FALLBACK_CONTEXT (128K) even when config.yaml has an explicit
model.context_length value (e.g. 204800 for MiniMax-M2.7).

This mirrors the fix already present in switch_model() at line 1988, which
correctly passes config_context_length. The fallback path was missed.

Fixes: context_length forced to 128K on fallback activation
@alt-glitch alt-glitch added type/bug Something isn't working P1 High — major feature broken, no workaround comp/agent Core agent loop, run_agent.py, prompt builder labels Apr 23, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Related: #12630 (broader fix propagating config_context_length to all 7 missed call sites). This PR fixes only the fallback activation path.

@teknium1

Copy link
Copy Markdown
Contributor

This fix has already landed on main. 🎉

Automated hermes-sweeper review confirmed the change is present:

  • Commit 46451528afix(agent): pass config_context_length in fallback activation path — is on main, 21 commits behind HEAD.
  • run_agent.py line 6892 contains config_context_length=getattr(self, "_config_context_length", None) inside _try_activate_fallback(), exactly as described in this PR.
  • The accompanying comment at lines 6884–6886 documents the rationale (respecting model.context_length from config.yaml).

No action needed — this PR's change is already incorporated. Thanks for the contribution, @CruxExperts!

@teknium1 teknium1 closed this Apr 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent loop, run_agent.py, prompt builder P1 High — major feature broken, no workaround type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants