Studio: stop ActiveThreadSync clearing persisted draft on reload

ActiveThreadSync was reacting to aui's mainThreadId === null on mount
(aui hasn't booted yet) by calling setActiveThreadId(null), which
wiped the just-restored persisted draft id from localStorage and
emptied the doc panel for the user's thread. The previous fix only
covered the 'aui minted a different LOCALID' branch; it missed the
'mainThreadId is null while aui boots' branch.

Treat a null mainThreadId as a no-op for the sync. Explicit clears
(new chat, sidebar delete) keep going through setActiveThreadId(null)
directly, so this guard doesn't trap stale state — it just gives the
persisted draft id a chance to survive until aui finishes booting.
This commit is contained in:
Roland Tannous 2026-05-27 21:46:52 +04:00
commit 3d3a00c2be

View file

@ -1017,6 +1017,15 @@ function ActiveThreadSync({
if (!enabled) {
return;
}
// Don't clobber a persisted activeThreadId while aui is still booting
// up. mainThreadId starts null on every page load before aui's async
// thread init finishes; without this guard we'd reset our persisted
// draft id to null and lose the doc chip mapping for that thread.
// Explicit clears (new chat / thread delete) go through
// setActiveThreadId(null) directly and bypass this effect.
if (!mainThreadId) {
return;
}
const persisted = useChatRuntimeStore.getState().activeThreadId;
// On page reload aui mints a fresh `__LOCALID_*` for its draft thread.
// If we already have a persisted draft id, keep it so any RAG docs
@ -1024,15 +1033,14 @@ function ActiveThreadSync({
// chat history for the new mainThreadId starts empty either way, so
// this only affects what we treat as the active id for doc/RAG lookup.
if (
mainThreadId
&& persisted
persisted
&& persisted !== mainThreadId
&& persisted.startsWith("__LOCALID_")
&& mainThreadId.startsWith("__LOCALID_")
) {
return;
}
setActiveThreadId(mainThreadId ?? null);
setActiveThreadId(mainThreadId);
}, [enabled, mainThreadId, setActiveThreadId]);
return null;