Skip to content

fix(cli): reinitialize context compressor on /new to pick up updated context length#31067

Open
gneiss09 wants to merge 1 commit into
NousResearch:mainfrom
gneiss09:fix/cli-new-context-length-refresh
Open

fix(cli): reinitialize context compressor on /new to pick up updated context length#31067
gneiss09 wants to merge 1 commit into
NousResearch:mainfrom
gneiss09:fix/cli-new-context-length-refresh

Conversation

@gneiss09

Copy link
Copy Markdown

Problem

When running Hermes Agent against LM Studio (or similar local providers), the CLI status bar displays stale context window information after /new. Two root causes:

  1. reset_session_state() does not recreate context_compressor — the compressor retains its old context_length value across sessions, so the TUI/CLI shows the wrong context window size.

  2. In-memory endpoint metadata cache has a 300s TTL_endpoint_model_metadata_cache in agent/model_metadata.py caches model info (including config.context_length) per base URL. Within the 5-minute TTL, stale values mask updated context lengths even if the provider changes its configuration.

Fix

On /new, before calling reset_session_state():

  1. If the provider is LM Studio, clear the in-memory endpoint metadata cache entry for that base URL (both _endpoint_model_metadata_cache and _endpoint_model_metadata_cache_time). This forces a fresh query to /api/v1/models on next use.

  2. Re-create the ContextCompressor with config_context_length=None, letting it probe the provider fresh instead of reusing the stale value from the old instance.

Both operations are wrapped in a try/except — failures are non-fatal and do not block session creation.

Changes

  • cli.py: Added context compressor reinitialization logic in new_session() (~37 lines)
    • Clears LM Studio endpoint metadata cache entry for the current base URL
    • Re-creates ContextCompressor with fresh config probe
    • Graceful degradation on any error

Testing

  • Confirmed locally: updated model in LM Studio from default to 119,128 tokens → /new now correctly shows the new context length in the TUI status bar.
  • No impact on non-LMStudio providers (cache clear only triggers for lmstudio provider).
  • Graceful fallback if any import or operation fails.

…updated context length

The in-memory _endpoint_model_metadata_cache has a 300s TTL, so stale
values persisted across /new commands within that window. Clear the cache
for LM Studio before re-creating the ContextCompressor so it queries the
live server and reads the new context_length.
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/cli CLI entry point, hermes_cli/, setup wizard comp/agent Core agent loop, run_agent.py, prompt builder labels May 23, 2026

@jsboige jsboige left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Targeted fix for stale context window display after /new. The context compressor retained its old context_length across session resets — now reinitialized with fresh provider probing. Also clears LM Studio endpoint metadata cache (300s TTL) to force re-read. Wrapped in try/except so session reset never fails. LGTM.

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 comp/cli CLI entry point, hermes_cli/, setup wizard 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.

3 participants