Skip to content

fix(#701): persist thread messages before switching to new chat#710

Open
shogun444 wants to merge 1 commit into
thesysdev:mainfrom
shogun444:fix/cache-messages-701
Open

fix(#701): persist thread messages before switching to new chat#710
shogun444 wants to merge 1 commit into
thesysdev:mainfrom
shogun444:fix/cache-messages-701

Conversation

@shogun444

@shogun444 shogun444 commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Closes #701

What

This PR addresses the message persistence issue when switching to a new chat by introducing an optional cacheMessages method to ThreadStorage.

The default in-memory storage implements this method by caching the current messages before the local state is cleared, allowing completed conversations to be restored when switching back to the thread.

Changes

  • Add optional cacheMessages(threadId, messages) to ThreadStorage
  • Implement cacheMessages in the default in-memory storage
  • Cache current messages before switchToNewThread() clears the local state
  • Persist messages after successful LLM streaming completes
  • Add unit tests covering both persistence paths

Note: While validating this change, I noticed another reproducible issue when switching threads during an active streaming response. I've documented that separately in the issue discussion since it appears to be a different edge case.

Test Plan

  • Verified locally
  • Added unit tests covering both persistence paths

Checklist

  • I linked a related issue
  • I updated docs/README when needed (not required)
  • I considered backwards compatibility

Add optional cacheMessages method to ThreadStorage to allow storages
to cache current messages before a thread switch. The default in-memory
storage implements it by saving to the internal messages map.

- ThreadStorage: add cacheMessages?(threadId, messages) — optional
- _defaultStorage: implement cacheMessages to persist to in-memory Map
- switchToNewThread: save current messages before clearing state
- processMessage: persist messages after successful LLM streaming
- Tests: verify cacheMessages is called in both paths with correct data
@shogun444

shogun444 commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

While testing this a bit more, I noticed another case that may be related.

If I send a message and the assistant starts streaming a response (for example, generating a component), then quickly switch to another chat or click New Chat before the response finishes, and later come back to the original thread, the message I originally sent is sometimes gone as well.

From what I've seen:

  • Start a new conversation.
  • Send a prompt that produces a longer streamed response.
  • While it's still streaming, switch to another thread or click New Chat.
  • Return to the original thread.

Actual:

  • In some cases, the user message itself disappears from the conversation.

I'm not sure if this is the same underlying issue or a separate race condition, but I was able to reproduce it multiple times while testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New Chat button clears current session instead of preserving it as a thread

1 participant