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;