fix(P1): restore historical thread message loading after thread switch
The threadJustSwitched suppression was too aggressive — it cleared lastMessagesRef on every thread switch, then relied on historicalMessages which loads async and may still be empty. Historical thread messages would never appear. Simplified fix: thread.switchThread() already clears thread.messages when switching, so thread.messages is always safe to trust directly. Use thread.messages when non-empty, else fall back to historicalMessages. Removes threadJustSwitched / lastMessagesRef / lastThreadIdRef entirely. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
b27424e3a0
commit
23527b3bdc
+5
-19
@@ -153,26 +153,12 @@ function App() {
|
||||
useEffect(() => { autoResizeTextarea(textareaRef); }, [input]);
|
||||
|
||||
// ── Active messages ──────────────────────────────────────────────────────
|
||||
const lastMessagesRef = useRef<Message[]>([]);
|
||||
const lastThreadIdRef = useRef<string | null>(null);
|
||||
// Detect thread switch — when threadId changes, thread.messages still holds stale data
|
||||
// for the current render cycle (useStream is async). Track this to suppress stale data.
|
||||
const threadJustSwitched = currentThreadId !== lastThreadIdRef.current;
|
||||
if (threadJustSwitched) {
|
||||
lastMessagesRef.current = [];
|
||||
lastThreadIdRef.current = currentThreadId;
|
||||
}
|
||||
// Only trust thread.messages when useStream has caught up to the current threadId
|
||||
const threadMessagesValid = !threadJustSwitched && thread.messages.length > 0;
|
||||
if (threadMessagesValid) {
|
||||
lastMessagesRef.current = thread.messages;
|
||||
}
|
||||
// thread.switchThread() is called in useEffect whenever currentThreadId changes,
|
||||
// which clears thread.messages. So thread.messages is always safe to trust:
|
||||
// - empty = thread just switched or genuinely empty → fall back to historicalMessages
|
||||
// - non-empty = useStream has loaded messages for the current thread
|
||||
const activeMessages: Message[] = deduplicateMessages(
|
||||
threadMessagesValid
|
||||
? thread.messages
|
||||
: lastMessagesRef.current.length > 0
|
||||
? lastMessagesRef.current
|
||||
: historicalMessages
|
||||
thread.messages.length > 0 ? thread.messages : historicalMessages
|
||||
);
|
||||
|
||||
// Completed / failed tool IDs
|
||||
|
||||
Reference in New Issue
Block a user