From 3d3a00c2be38c75646da2e7e9f1edc7e214f02a1 Mon Sep 17 00:00:00 2001 From: Roland Tannous Date: Wed, 27 May 2026 21:46:52 +0400 Subject: [PATCH] Studio: stop ActiveThreadSync clearing persisted draft on reload MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../src/features/chat/runtime-provider.tsx | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/studio/frontend/src/features/chat/runtime-provider.tsx b/studio/frontend/src/features/chat/runtime-provider.tsx index bdf4e528d8..716fa1d633 100644 --- a/studio/frontend/src/features/chat/runtime-provider.tsx +++ b/studio/frontend/src/features/chat/runtime-provider.tsx @@ -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;