fix(coach): replay the newest messages, not the oldest 40 - #69
Merged
saksham2001 merged 2 commits intoJul 12, 2026
Merged
Conversation
recentMessages built the model's conversation history with an ASCENDING (createdAt .forward) sort plus fetchLimit = 40, which returns the OLDEST 40 rows; .suffix(limit) then took messages ~31-40. For any conversation past 40 messages the replayed context froze at messages 31-40 and never included anything newer — the coach silently 'forgot' all recent turns (and re-sent the same stale block every turn). Conversations accumulate (the chat continues the latest thread), so an engaged user crosses 40 messages and hits this. Fetch the newest 40 (descending), reverse back to chronological, then suffix as before. historyTurns is 4/10, so 40 stays an ample buffer. The <=40-message case is unchanged. recentMessages is the only conversation history sent to the model.
saksham2001
approved these changes
Jul 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
CoachViewModel.recentMessagesbuilds the conversation history replayed to the model each turn. It fetched with:fetchLimitis applied after the sort, so ascending + limit 40 returns the oldest 40 rows..suffix(limit)then takes the last N of those oldest 40 → messages ~31–40.Impact
.suffixgenuinely is the newest N. Works — which is why it isn't caught in normal testing.The chat continues the most recent thread by default (
CoachViewpicksallMessages.last?.conversationId), so an engaged user's single conversation accumulates past 40 (~20 back-and-forth turns) and hits this.recentMessagesis the only conversation history passed to the model (verified inCoachOrchestrator), so nothing else compensates.Fix
Fetch the newest 40 (descending), restore chronological order, then
.suffix(limit)as before:historyTurnsis 4/10 so 40 remains an ample buffer after filtering the current turn + error bubbles. The ≤40-message case is unchanged. Latent bug (only bites long threads); builds clean.