#16609: fix: resolve session store race condition and contextTokens updates
channel: telegram
commands
agents
size: S
Cluster:
Session Management Improvements
## Summary
Two focused fixes for session state correctness:
- **Session store race condition (#14979):** Moved session entry construction inside the `updateSessionStore` lock callback so it reads fresh data from disk, preventing compaction or concurrent agent runs from silently overwriting updates.
- **contextTokens not updated on model override (#14969):** `applyModelOverrideToSessionEntry` now calls `lookupContextTokens` for the selected model, keeping `entry.contextTokens` in sync when the user switches models via `/model`.
## Test plan
- [x] `pnpm build` passes
- [x] `pnpm lint` clean
- [x] Existing session-store and model-override tests pass
Closes #14979
Closes #14969
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Fixes two session state correctness issues: prevents race conditions during concurrent session updates by moving entry construction inside the lock callback, and ensures `contextTokens` stays in sync when users override the model via `/model` command. Both changes are minimal, focused, and follow existing patterns in the codebase.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- Both fixes are minimal, focused, and correct. The race condition fix properly moves the entry construction inside the lock callback to read fresh data from disk. The contextTokens fix ensures consistency when models are overridden. The changes follow existing patterns, existing tests cover the locking mechanism well, and the test plan shows build and lint passing.
- No files require special attention
<sub>Last reviewed commit: 710d093</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#20770: fix: prevent stale session-entry overwrite during reset-model persi...
by coygeek · 2026-02-19
82.4%
#19328: Fix: preserve modelOverride in agent handler (#5369)
by CodeReclaimers · 2026-02-17
81.5%
#15726: fix(sessions): use model contextWindow instead of agent contextToke...
by lailoo · 2026-02-13
81.3%
#16478: fix(gateway): fall back to lookupContextTokens on model switch
by colddonkey · 2026-02-14
81.1%
#17414: fix(sessions): refresh contextTokens when model override changes
by michaelbship · 2026-02-15
81.0%
#17375: fix(session): don't carry stale model info into reset welcome message
by BinHPdev · 2026-02-15
80.6%
#10725: fix: re-read session store inside lock in updateSessionStoreEntry
by Yida-Dev · 2026-02-06
80.6%
#21932: fix(tui): eliminate stale model indicator lag in TUI
by graysurf · 2026-02-20
79.8%
#15882: fix: move session entry computation inside store lock to prevent ra...
by cloorus · 2026-02-14
79.8%
#16987: fix(config): add skipCache to updateSessionStoreEntry and updateLas...
by AI-Reviewer-QS · 2026-02-15
79.5%