Skip to content

refactor: use TanStack Query cache as single source of truth for chat messages#218

Merged
chubes4 merged 1 commit into
Extra-Chill:mainfrom
saraichinwag:fix/210-chat-tanstack-cache
Feb 17, 2026
Merged

refactor: use TanStack Query cache as single source of truth for chat messages#218
chubes4 merged 1 commit into
Extra-Chill:mainfrom
saraichinwag:fix/210-chat-tanstack-cache

Conversation

@saraichinwag
Copy link
Copy Markdown
Contributor

Summary

Eliminates dual-state race conditions in ChatSidebar by using TanStack Query cache as the single source of truth for chat messages.

Changes

ChatSidebar.jsx:

  • Removed useState for messages — reads directly from useChatSession() query data
  • Optimistic updates use queryClient.setQueryData instead of setMessages
  • New sessions use a temporary pendingUserMessage state until the session is created and cache is seeded
  • Removed useEffect syncing sessionQuery.data.conversation → setMessages
  • Removed useEffect for sessionQuery.error (handled inline)

useChatTurn.js:

  • processToCompletion now accepts queryClient instead of onNewMessages callback
  • continueTurn updates query cache directly via appendMessagesToCache helper
  • No more stale closure issues from callback-based state updates

queries/chat.js: No changes needed — staleTime: Infinity already prevents cache overwrites.

Why This Works

  • Messages come from ONE place: the query cache for ['chat-session', sessionId]
  • staleTime: Infinity ensures our optimistic updates aren't overwritten by refetches
  • Server responses seed the cache directly, so there's no sync delay
  • Turn-by-turn polling appends to cache atomically

Fixes #210
Fixes #4
Fixes #5

@chubes4 chubes4 merged commit bb8924d into Extra-Chill:main Feb 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants