#16478: fix(gateway): fall back to lookupContextTokens on model switch
gateway
stale
size: XS
Cluster:
Context Management Fixes
## Summary
- When a TUI session switches models, `listSessionsFromStore` returns `contextTokens` from the stored session entry. If the entry was created before the model switch (or the field was never persisted), `entry.contextTokens` is `undefined` and the UI shows no context window info.
- This adds a `?? lookupContextTokens(model)` fallback so the display always reflects the current model's context window, even if the session entry lacks the field.
## Test plan
- [x] Verified in local TUI: switch model via `/model`, then check session info - context window now shows correct max tokens for the new model.
- [x] `pnpm tsgo` passes cleanly.
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Single-line fix in `listSessionsFromStore` that adds a `?? lookupContextTokens(model)` fallback when building session rows. Previously, if `entry.contextTokens` was `undefined` (e.g., after switching models via `/model`, or for session entries that never persisted the field), the TUI would show no context window info. Now, the gateway resolves the context window from the model catalog for the active model before returning it to the UI.
- The fix aligns `listSessionsFromStore` with the same fallback pattern used consistently elsewhere in the codebase (`status.summary.ts`, `sessions.ts`, `auto-reply/status.ts`, etc.)
- `lookupContextTokens` was already imported; no new dependencies introduced
- The `model` variable is always a string (guaranteed by `?? DEFAULT_MODEL` on line 758), so the lookup will always receive a valid model ID
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge — it's a minimal, well-scoped one-line fix that follows an established pattern in the codebase.
- The change is a single-line addition of a nullish coalescing fallback that mirrors the exact same pattern used in multiple other files across the codebase. The `lookupContextTokens` function was already imported and widely used. The fix correctly addresses the scenario where session entries lack a `contextTokens` field after a model switch. No new code paths, dependencies, or behavioral changes beyond the intended fallback.
- No files require special attention.
<sub>Last reviewed commit: 6648683</sub>
<!-- greptile_other_comments_section -->
<sub>(5/5) You can turn off certain types of comments like style [here](https://app.greptile.com/review/github)!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#17414: fix(sessions): refresh contextTokens when model override changes
by michaelbship · 2026-02-15
83.9%
#15726: fix(sessions): use model contextWindow instead of agent contextToke...
by lailoo · 2026-02-13
83.3%
#11109: fix(tui): prefer config contextTokens over persisted session value
by marezgui · 2026-02-07
83.2%
#17375: fix(session): don't carry stale model info into reset welcome message
by BinHPdev · 2026-02-15
81.4%
#6750: fix(tui): show session model overrides in status bar
by ewijaya · 2026-02-02
81.4%
#23136: fix: lookupContextTokens should handle provider/model refs
by patchguardio · 2026-02-22
81.4%
#16609: fix: resolve session store race condition and contextTokens updates
by battman21 · 2026-02-14
81.1%
#21932: fix(tui): eliminate stale model indicator lag in TUI
by graysurf · 2026-02-20
81.0%
#23299: fix(status): show runtime model context limit instead of stale sess...
by SidQin-cyber · 2026-02-22
80.1%
#17604: fix(context): use getAvailable() to prevent cross-provider model ID...
by aldoeliacim · 2026-02-16
79.1%