Skip to content

fix: update sessionEntry.contextTokens on model switch#35541

Open
ethanperezz wants to merge 1 commit intoopenclaw:mainfrom
ethanperezz:fix/context-tokens-stuck-after-model-switch
Open

fix: update sessionEntry.contextTokens on model switch#35541
ethanperezz wants to merge 1 commit intoopenclaw:mainfrom
ethanperezz:fix/context-tokens-stuck-after-model-switch

Conversation

@ethanperezz
Copy link
Copy Markdown

Summary

  • When switching models via inline directive (e.g., /model), applyModelOverrideToSessionEntry updated the model/provider override but left sessionEntry.contextTokens unchanged
  • This caused contextTokens to get stuck at a lower value after switching to a model with a smaller context window and back (e.g., 198k → 160k → stays 160k)
  • Now persistInlineDirectives explicitly updates sessionEntry.contextTokens to match the new model's context window before persisting the session store

Fixes #35372

Test plan

  • Start a session with Model A (e.g., 198k context window)
  • Switch to Model B (e.g., 160k context window) via /model
  • Verify /status shows the correct context window for Model B
  • Switch back to Model A
  • Verify /status shows Model A's context window (198k), not Model B's (160k)
  • Check session store file to confirm contextTokens is updated correctly

🤖 Generated with Claude Code

When switching models via inline directive, applyModelOverrideToSessionEntry
updated the model/provider override but left contextTokens unchanged.
This caused contextTokens to get stuck at a lower value after switching
to a model with a smaller context window and back.

Now persistInlineDirectives explicitly updates sessionEntry.contextTokens
to match the new model's context window, keeping session state consistent.

Fixes openclaw#35372

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 5, 2026

Greptile Summary

This PR fixes a stale-contextTokens bug in persistInlineDirectives: after an inline /model switch, applyModelOverrideToSessionEntry updated the provider/model override fields but left sessionEntry.contextTokens unchanged, causing the context window to remain at a lower value even after switching back to a larger-context model.

The fix adds a targeted block immediately after the provider/model fields are updated to recompute contextTokens with the formula agentCfg?.contextTokens ?? lookupContextTokens(model) ?? DEFAULT_CONTEXT_TOKENS, which is consistent with the existing return-value formula at line 224. The updated flag and session-store persistence logic interact correctly.

The change is small, well-contained, and correct for the stated issue.

Confidence Score: 5/5

  • Safe to merge — the fix is correct, focused, and solves the stated problem with no identified issues.
  • The PR correctly fixes the stale-contextTokens bug. The implementation is small, well-scoped, and uses the same formula pattern already in the codebase. No blocking issues were found during verification. The fix properly sets the updated flag only when values change and correctly persists to the session store.
  • No files require special attention.

Last reviewed commit: 3af6e63

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Session contextTokens gets stuck at lower model limit after model switch (e.g. 198k -> 160k -> stays 160k)

1 participant