#11109: fix(tui): prefer config contextTokens over persisted session value
stale
Cluster:
Context Management Fixes
## Summary
Fixes #11067
The TUI footer was showing `0/200k` tokens instead of the user's configured `contextTokens` (e.g., 1M) because the persisted session value from previous runs was taking precedence over the config.
## Problem
When a session runs, `contextTokensUsed` is calculated (often 200k from model lookup or `DEFAULT_CONTEXT_TOKENS`) and saved to the session entry. On subsequent TUI refreshes, this persisted `entry.contextTokens` was being used instead of `defaults.contextTokens` from the user's config.
## Solution
Changed the priority order in `applySessionInfo` to prefer config-level `contextTokens` over the persisted session value:
```typescript
// Before: entry?.contextTokens ?? defaults?.contextTokens
// After: defaults?.contextTokens ?? entry?.contextTokens
```
This ensures the user's explicit configuration (`contextTokens: 1000000`) is respected in the TUI display.
## Testing
- [x] Verified the logic change
- [ ] Manual testing with 1M context config
---
🤖 AI-assisted: This fix was developed with Claude. The bug was identified by tracing the data flow from config → session storage → TUI display.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR updates the TUI session-info merge logic so that `agents.defaults.contextTokens` from the current config is preferred over a persisted `entry.contextTokens` value stored in the sessions file. The change is localized to `src/tui/tui-session-actions.ts` in `applySessionInfo`, which feeds `state.sessionInfo.contextTokens` used by the footer token display (`src/tui/tui.ts`).
Net effect: users who set a larger context window in config (e.g. 1M) will see that reflected in the footer instead of an older model-derived/persisted context value (often 200k).
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk.
- The change is a one-line precedence swap in a nullish-coalescing chain that only affects how the TUI displays the context window size; it doesn’t alter persistence or core session behavior. No new control flow, side effects, or API contracts are introduced.
- No files require special attention
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#19412: fix(status): prefer configured contextTokens over session entry
by rafaelipuente · 2026-02-17
85.1%
#15726: fix(sessions): use model contextWindow instead of agent contextToke...
by lailoo · 2026-02-13
84.2%
#16478: fix(gateway): fall back to lookupContextTokens on model switch
by colddonkey · 2026-02-14
83.2%
#21932: fix(tui): eliminate stale model indicator lag in TUI
by graysurf · 2026-02-20
82.4%
#18886: fix(status): prefer configured contextTokens over model metadata
by BinHPdev · 2026-02-17
81.5%
#7537: fix(sessions): reset token counts on /new for channel sessions
by SoniAssist · 2026-02-02
81.0%
#6750: fix(tui): show session model overrides in status bar
by ewijaya · 2026-02-02
81.0%
#21791: feat(TUI): show main agent model in status footer
by chansuke · 2026-02-20
79.7%
#17414: fix(sessions): refresh contextTokens when model override changes
by michaelbship · 2026-02-15
79.6%
#22387: fix: session_status context tracking undercount for cached providers
by 1ucian · 2026-02-21
79.5%