Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
🚜 fix: Release Heavy Chat State on Conversation Switches (#17291)
lia-by-librechat[bot]4fad3d75a9fc
diff --git a/client/src/components/Chat/Messages/MessageNav.tsx b/client/src/components/Chat/Messages/MessageNav.tsxindex f0117d2a1a..b3e7aa11e3 100644--- a/client/src/components/Chat/Messages/MessageNav.tsx+++ b/client/src/components/Chat/Messages/MessageNav.tsx@@ -367,9 +367,16 @@ const chevronButtonClasses = cn('focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-border-xheavy',);-function MessageNav({ scrollableRef }: { scrollableRef: React.RefObject<HTMLDivElement> }) {+function MessageNav({+ scrollableRef,+ conversationId: renderedConversationId,+}: {+ scrollableRef: React.RefObject<HTMLDivElement>;+ conversationId?: string | null;+}) {const localize = useLocalize();- const { conversationId } = useMessagesConversation();+ const { conversationId: contextConversationId } = useMessagesConversation();+ const conversationId = renderedConversationId ?? contextConversationId;const { isSubmitting } = useMessagesSubmission();const { data: messages } = useGetMessagesByConvoId(conversationId ?? '',diff --git a/client/src/components/Chat/Messages/MessagesView.tsx b/client/src/components/Chat/Messages/MessagesView.tsxindex 7a028d4ac8..60777f335e 100644--- a/client/src/components/Chat/Messages/MessagesView.tsx+++ b/client/src/components/Chat/Messages/MessagesView.tsx@@ -50,9 +50,15 @@ function MessagesViewContent({useScrollbarGutter(scrollableRef);const { conversationId } = conversation ?? {};- const fileMap = useFileMapContext();- const threadRows = useThreadRows(FLAT_THREAD ? messages : null, conversationId, fileMap);const { index, latestMessageId, latestMessageDepth, messagesKey } = useChatContext();+ /** Re-arm from the conversation that owns the RENDERED tree: the Recoil+ * conversation id lags the route during warm-cache navigation, and keying+ * off it would first mount the new tree unwindowed, then narrow it after+ * the fact, visibly unmounting rows the user is already reading. An empty+ * tree has no message to name its owner, so the route's key stands in. */+ const treeConversationId = _messagesTree?.[0]?.conversationId ?? (messagesKey || conversationId);+ const fileMap = useFileMapContext();+ const threadRows = useThreadRows(FLAT_THREAD ? messages : null, treeConversationId, fileMap);const isSubmitting = useIsSubmitting(index);const { showScrollButton, maximizeChatSpace } = useChatSurface();const autoScroll = useAtomValue(autoScrollAtom);@@ -104,12 +110,6 @@ function MessagesViewContent({[handleNearBottomChange, reportNearBottom],);- /** Re-arm from the conversation that owns the RENDERED tree: the Recoil- * conversation id lags the route during warm-cache navigation, and keying- * off it would first mount the new tree unwindowed, then narrow it after- * the fact, visibly unmounting rows the user is already reading. An empty- * tree has no message to name its owner, so the route's key stands in. */- const treeConversationId = _messagesTree?.[0]?.conversationId ?? (messagesKey || conversationId);const mountWindow = useProgressiveRowMount({tailDepth: latestMessageDepth,anchorBottom: autoScroll || isSubmitting,@@ -175,7 +175,7 @@ function MessagesViewContent({) : (<MultiMessagemessagesTree={_messagesTree}- messageId={conversationId ?? null}+ messageId={treeConversationId ?? null}setCurrentEditId={setCurrentEditId}currentEditId={currentEditId ?? null}/>@@ -220,7 +220,11 @@ function MessagesViewContent({onNearBottomChange={handleNearBottom}/>- <MessageNav scrollableRef={scrollableRef} />+ <MessageNav+ key={treeConversationId}+ conversationId={treeConversationId}+ scrollableRef={scrollableRef}+ /></div></div></>diff --git a/client/src/components/Chat/Messages/__tests__/MessageNav.spec.tsx b/client/src/components/Chat/Messages/__tests__/MessageNav.spec.tsxindex 642b929fc7..26af69b3c9 100644--- a/client/src/components/Chat/Messages/__tests__/MessageNav.spec.tsx+++ b/client/src/components/Chat/Messages/__tests__/MessageNav.spec.tsx@@ -266,6 +266,17 @@ afterEach(() => {});describe('MessageNav', () => {+ it('queries the rendered destination instead of the lagging conversation context', () => {+ const { scrollable } = buildDom([]);+ render(<MessageNav conversationId="destination" scrollableRef={{ current: scrollable }} />);++ expect(mockUseGetMessagesByConvoId).toHaveBeenCalledWith(+ 'destination',+ { enabled: true },+ { isStreaming: false },+ );+ });+describe('rendering threshold', () => {it('renders nothing when there are 0 messages', () => {const { container } = renderNav([]);diff --git a/client/src/components/Chat/Messages/__tests__/MessagesView.spec.tsx b/client/src/components/Chat/Messages/__tests__/MessagesView.spec.tsxindex bdfb6c24c1..a780d74298 100644--- a/client/src/components/Chat/Messages/__tests__/MessagesView.spec.tsx+++ b/client/src/components/Chat/Messages/__tests__/MessagesView.spec.tsx@@ -1,10 +1,16 @@import React from 'react';+import { Provider, createStore } from 'jotai';import { render, screen } from '@testing-library/react';import type { TMessage } from 'librechat-data-provider';+import { siblingIdxFamily } from '~/components/Chat/Messages/Thread/state';+import useThreadRows from '~/hooks/Messages/useThreadRows';+import * as thread from '~/utils/thread';let mockMessagesKey = 'convo-1';let mockMaximizeChatSpace = false;let mockFlatThread = false;+const mockNavMount = jest.fn();+const mockNavUnmount = jest.fn();jest.mock('jotai', () => ({...jest.requireActual('jotai'),@@ -44,7 +50,7 @@ jest.mock('~/hooks/Messages', () => ({jest.mock('~/hooks/Messages/useThreadRows', () => ({__esModule: true,- default: () => [],+ default: jest.fn(() => []),}));jest.mock('~/components/Chat/Subagents/surface', () => ({@@ -66,7 +72,9 @@ jest.mock('../Thread', () => ({jest.mock('../MultiMessage', () => ({__esModule: true,- default: () => <div data-testid="multi-message" />,+ default: ({ messageId }: { messageId: string | null }) => (+ <div data-testid="multi-message" data-parent-id={messageId} />+ ),}));jest.mock('../Content/Parts/PendingSteers', () => ({@@ -88,10 +96,19 @@ jest.mock('../ScrollButton', () => ({__esModule: true,default: () => null,}));-jest.mock('../MessageNav', () => ({- __esModule: true,- default: () => null,-}));+jest.mock('../MessageNav', () => {+ const { useEffect } = jest.requireActual('react');+ return {+ __esModule: true,+ default: function MessageNav({ conversationId }: { conversationId?: string | null }) {+ useEffect(() => {+ mockNavMount();+ return () => mockNavUnmount();+ }, []);+ return <div data-testid="message-nav" data-conversation-id={conversationId} />;+ },+ };+});jest.mock('~/utils', () => ({cn: (...classes: Array<string | false | null | undefined>) => classes.filter(Boolean).join(' '),@@ -111,6 +128,7 @@ describe('MessagesView pending steers', () => {mockMessagesKey = 'convo-1';mockMaximizeChatSpace = false;mockFlatThread = false;+ jest.mocked(useThreadRows).mockReset().mockReturnValue([]);});it('keeps the failed-steer surface mounted in the recursive renderer', () => {@@ -157,6 +175,64 @@ describe('MessagesView pending steers', () => {expect(screen.getByTestId('pending-steers')).toHaveAttribute('data-conversation-id', 'convo-2');});+ it('does not reuse outgoing rows while the conversation context still names the source', () => {+ mockFlatThread = true;+ jest+ .mocked(useThreadRows)+ .mockImplementation(jest.requireActual('~/hooks/Messages/useThreadRows').default);+ const resolve = jest.spyOn(thread, 'resolveThreadRows');+ const large = Array.from({ length: 12_000 }, (_, index) => ({+ ...messageTree[0],+ messageId: `large-${index}`,+ parentMessageId: index === 0 ? null : `large-${index - 1}`,+ }));+ const view = render(<MessagesView messagesTree={large} messages={large} />);+ const small = [{ ...messageTree[0], conversationId: 'convo-2', messageId: 'small' }];+ view.rerender(<MessagesView messagesTree={small} messages={small} />);++ expect(resolve).toHaveBeenLastCalledWith(expect.anything(), 'convo-2', expect.anything(), null);+ view.unmount();+ jest.mocked(useThreadRows).mockReset().mockReturnValue([]);+ });++ it('preserves the destination root branch while the context still names the source', () => {+ mockFlatThread = true;+ jest+ .mocked(useThreadRows)+ .mockImplementation(jest.requireActual('~/hooks/Messages/useThreadRows').default);+ const resolve = jest.spyOn(thread, 'resolveThreadRows');+ const jotaiStore = createStore();+ jotaiStore.set(siblingIdxFamily('convo-2'), 1);+ const destination = [+ { ...messageTree[0], messageId: 'older-root', conversationId: 'convo-2' },+ { ...messageTree[0], messageId: 'newer-root', conversationId: 'convo-2' },+ ];+ render(+ <Provider store={jotaiStore}>+ <MessagesView messagesTree={destination} messages={destination} />+ </Provider>,+ );++ expect(resolve.mock.results.at(-1)?.value[0].source.messageId).toBe('older-root');+ expect(jotaiStore.get(siblingIdxFamily('convo-2'))).toBe(1);+ expect(screen.getByTestId('message-nav')).toHaveAttribute('data-conversation-id', 'convo-2');+ });++ it('uses the rendered owner for recursive root sibling choices too', () => {+ const destination = [{ ...messageTree[0], conversationId: 'convo-2' }];+ render(<MessagesView messagesTree={destination} messages={destination} />);++ expect(screen.getByTestId('multi-message')).toHaveAttribute('data-parent-id', 'convo-2');+ });++ it('uses the routed owner for flat rows when the destination tree is empty', () => {+ mockFlatThread = true;+ mockMessagesKey = 'convo-2';+ render(<MessagesView messagesTree={[]} messages={[]} />);++ expect(useThreadRows).toHaveBeenLastCalledWith([], 'convo-2', expect.anything());+ });+it('pins the screenshot identity to the rendered transcript and replaces it on navigation', () => {const view = render(<MessagesView messagesTree={messageTree} messages={messageTree} />);const initialTarget = screen.getByTestId('screenshot-target');@@ -169,6 +245,21 @@ describe('MessagesView pending steers', () => {expect(initialTarget.isConnected).toBe(false);});+ it('releases the outgoing navigation rail on a warm switch, not on same-chat updates', () => {+ const view = render(<MessagesView messagesTree={messageTree} messages={messageTree} />);+ expect(mockNavMount).toHaveBeenCalledTimes(1);+ const updated = [{ ...messageTree[0], text: 'updated' }];+ view.rerender(<MessagesView messagesTree={updated} messages={updated} />);+ expect(mockNavMount).toHaveBeenCalledTimes(1);+ expect(mockNavUnmount).not.toHaveBeenCalled();++ const destination = [{ ...messageTree[0], conversationId: 'convo-2' }];+ view.rerender(<MessagesView messagesTree={destination} messages={destination} />);++ expect(mockNavUnmount).toHaveBeenCalledTimes(1);+ expect(mockNavMount).toHaveBeenCalledTimes(2);+ });+it('keeps recovery visible while the message tree is temporarily empty', () => {render(<MessagesView messagesTree={[]} messages={[]} />);diff --git a/client/src/data-provider/Messages/__tests__/retention.spec.ts b/client/src/data-provider/Messages/__tests__/retention.spec.tsindex 9b89506f86..70225f3b96 100644--- a/client/src/data-provider/Messages/__tests__/retention.spec.ts+++ b/client/src/data-provider/Messages/__tests__/retention.spec.ts@@ -60,6 +60,170 @@ describe('retainMessages', () => {expect(isCached(queryClient, 'a')).toBe(false);});+ it('releases an oversized history instead of keeping it for the warm-cache grace', () => {+ stop();+ stop = retainMessages(queryClient, { isPinned: () => false, maxBytes: 1024 });+ queryClient.setQueryData(+ [QueryKeys.messages, 'large'],+ [{ ...history('large')[0], text: 'x'.repeat(4096) }],+ );+ observe(queryClient, 'large')();++ jest.advanceTimersByTime(RELEASE_SETTLE_MS);++ expect(isCached(queryClient, 'large')).toBe(false);+ });++ it('rechecks the budget when a late cache write grows a previously measured history', () => {+ stop();+ stop = retainMessages(queryClient, { isPinned: () => false, maxBytes: 1024 });+ visit(queryClient, 'a');+ jest.advanceTimersByTime(RELEASE_SETTLE_MS);+ expect(isCached(queryClient, 'a')).toBe(true);++ queryClient.setQueryData(+ [QueryKeys.messages, 'a'],+ [{ ...history('a')[0], text: 'x'.repeat(4096) }],+ );+ jest.advanceTimersByTime(RELEASE_SETTLE_MS);++ expect(isCached(queryClient, 'a')).toBe(false);+ });++ it('remeasures same-reference writes even when their timestamp is unchanged', () => {+ stop();+ stop = retainMessages(queryClient, { isPinned: () => false, maxBytes: 1024 });+ visit(queryClient, 'a');+ jest.advanceTimersByTime(RELEASE_SETTLE_MS);+ const key = [QueryKeys.messages, 'a'];+ const messages = queryClient.getQueryData<TMessage[]>(key)!;+ const updatedAt = queryClient.getQueryState(key)!.dataUpdatedAt;+ const writes = queryClient.getQueryState(key)!.dataUpdateCount;+ messages[0].text = 'x'.repeat(4096);++ queryClient.setQueryData(key, messages, { updatedAt });+ expect(queryClient.getQueryData(key)).toBe(messages);+ expect(queryClient.getQueryState(key)!.dataUpdatedAt).toBe(updatedAt);+ expect(queryClient.getQueryState(key)!.dataUpdateCount).toBe(writes + 1);+ jest.advanceTimersByTime(RELEASE_SETTLE_MS);++ expect(isCached(queryClient, 'a')).toBe(false);+ });++ it('remeasures an observed in-place write after its history becomes inactive again', () => {+ stop();+ stop = retainMessages(queryClient, { isPinned: () => false, maxBytes: 1024 });+ visit(queryClient, 'a');+ jest.advanceTimersByTime(RELEASE_SETTLE_MS);+ const unmount = observe(queryClient, 'a');+ const key = [QueryKeys.messages, 'a'];+ const messages = queryClient.getQueryData<TMessage[]>(key)!;+ messages[0].text = 'x'.repeat(4096);+ queryClient.setQueryData(key, messages);+ jest.advanceTimersByTime(RELEASE_SETTLE_MS);+ expect(isCached(queryClient, 'a')).toBe(true);++ unmount();+ jest.advanceTimersByTime(RELEASE_SETTLE_MS);++ expect(isCached(queryClient, 'a')).toBe(false);+ });++ it('reuses an estimate across observer and fetch-status events without data writes', () => {+ stop();+ stop = retainMessages(queryClient, { isPinned: () => false, maxBytes: 1024 });+ const key = [QueryKeys.messages, 'a'];+ const message = history('a')[0];+ queryClient.setQueryData(key, [message]);+ const readText = jest.fn(() => 'small');+ Object.defineProperty(message, 'text', { enumerable: true, get: readText });+ jest.advanceTimersByTime(RELEASE_SETTLE_MS);+ expect(readText).toHaveBeenCalledTimes(1);++ const unmount = observe(queryClient, 'a');+ unmount();+ queryClient.getQueryCache().find(key)!.setState({ fetchStatus: 'idle' });+ jest.advanceTimersByTime(RELEASE_SETTLE_MS);++ expect(isCached(queryClient, 'a')).toBe(true);+ expect(readText).toHaveBeenCalledTimes(1);+ });++ it('applies the payload budget across all recently left histories, newest first', () => {+ stop();+ stop = retainMessages(queryClient, { isPinned: () => false, recent: 3, maxBytes: 1024 });+ for (const id of ['a', 'b', 'c']) {+ queryClient.setQueryData(+ [QueryKeys.messages, id],+ [{ ...history(id)[0], text: 'x'.repeat(300) }],+ );+ observe(queryClient, id)();+ jest.advanceTimersByTime(10);+ }++ jest.advanceTimersByTime(RELEASE_SETTLE_MS);++ expect(isCached(queryClient, 'a')).toBe(false);+ expect(isCached(queryClient, 'b')).toBe(false);+ expect(isCached(queryClient, 'c')).toBe(true);+ });++ it('does not inspect observed, pinned or exempt histories to enforce the budget', () => {+ stop();+ stop = retainMessages(queryClient, {+ isPinned: (id) => pinned.has(id),+ isExempt: (id) => id === 'assistant',+ maxBytes: 0,+ });+ seed(queryClient, 'shown');+ const unmount = observe(queryClient, 'shown');+ pinned.add('running');+ visit(queryClient, 'running');+ visit(queryClient, 'assistant');+ visit(queryClient, 'left');++ jest.advanceTimersByTime(RELEASE_SETTLE_MS);++ expect(isCached(queryClient, 'shown')).toBe(true);+ expect(isCached(queryClient, 'running')).toBe(true);+ expect(isCached(queryClient, 'assistant')).toBe(true);+ expect(isCached(queryClient, 'left')).toBe(false);+ unmount();+ });++ it('stops measuring once a payload exceeds the budget without serializing it', () => {+ stop();+ stop = retainMessages(queryClient, { isPinned: () => false, maxBytes: 1024 });+ const message = { ...history('large')[0], text: 'x'.repeat(4096) };+ const toJSON = jest.fn(() => {+ throw new Error('must not serialize the transcript');+ });+ Object.defineProperty(message, 'toJSON', { value: toJSON });+ queryClient.setQueryData([QueryKeys.messages, 'large'], [message]);+ const trailing = jest.fn(() => {+ throw new Error('must stop at the oversized text');+ });+ Object.defineProperty(message, 'trailing', { enumerable: true, get: trailing });+ observe(queryClient, 'large')();++ jest.advanceTimersByTime(RELEASE_SETTLE_MS);++ expect(isCached(queryClient, 'large')).toBe(false);+ expect(toJSON).not.toHaveBeenCalled();+ expect(trailing).not.toHaveBeenCalled();+ });++ it('releases unobserved full tool parts even when the small history keeps its grace', () => {+ visit(queryClient, 'a');+ const key = [QueryKeys.toolCallPart, 'a', 'message', 'call'];+ queryClient.setQueryData(key, { tool_call: { output: 'x'.repeat(4096) } });++ jest.advanceTimersByTime(RELEASE_SETTLE_MS);++ expect(isCached(queryClient, 'a')).toBe(true);+ expect(queryClient.getQueryData(key)).toBeUndefined();+ });+it('keeps only the most recently left conversation across a burst of switches', () => {visit(queryClient, 'a');jest.advanceTimersByTime(10);diff --git a/client/src/data-provider/Messages/retention.ts b/client/src/data-provider/Messages/retention.tsindex c868fad15a..210e9eafdd 100644--- a/client/src/data-provider/Messages/retention.ts+++ b/client/src/data-provider/Messages/retention.ts@@ -4,6 +4,7 @@ import {QueryKeys,DEFAULT_HISTORY_CACHE_RECENT,DEFAULT_HISTORY_CACHE_TTL_MS,+ DEFAULT_HISTORY_CACHE_MAX_BYTES,} from 'librechat-data-provider';import type { Query, QueryClient } from '@tanstack/react-query';@@ -24,6 +25,8 @@ export type MessagesRetentionOptions = {recent?: number;/** How long a left conversation's history stays cached. */ttlMs?: number;+ /** Aggregate estimated payload budget for histories kept during the grace period. */+ maxBytes?: number;};type IdleHistory = {@@ -66,6 +69,47 @@ const isUnobservedHistory = (query: Query): boolean =>const newestFirst = (a: IdleHistory, b: IdleHistory): number => b.leftAt - a.leftAt;+/** Walk untyped cache payloads lazily: JSON.stringify would allocate another transcript.+ * Count UTF-16 strings and scalar slots, not engine-specific object/DOM overhead. */+function estimatePayloadBytes(data: unknown, limit: number): number {+ function* fields(value: object): Generator<unknown> {+ for (const key in value) {+ if (Object.hasOwn(value, key)) {+ yield key;+ yield Reflect.get(value, key);+ }+ }+ }++ const seen = new WeakSet<object>();+ const stack: Iterator<unknown>[] = [[data].values()];+ let bytes = 0;+ while (stack.length > 0) {+ const next = stack[stack.length - 1].next();+ if (next.done) {+ stack.pop();+ continue;+ }+ const value = next.value;+ if (typeof value === 'string') {+ bytes += value.length * 2;+ } else if (value != null && typeof value === 'object') {+ if (seen.has(value)) {+ continue;+ }+ seen.add(value);+ bytes += 8;+ stack.push(Array.isArray(value) ? value.values() : fields(value));+ } else {+ bytes += 8;+ }+ if (bytes > limit) {+ return bytes;+ }+ }+ return bytes;+}+/*** Releases the message histories of conversations the user has left, so long transcripts do* not pile up in memory across conversation switches. React Query's own `cacheTime` cannot@@ -76,7 +120,7 @@ const newestFirst = (a: IdleHistory, b: IdleHistory): number => b.leftAt - a.lef** A history is released once it has no observers, is not fetching, is neither pinned nor exempt,* and is either older than `ttlMs` since it was left or beyond the `recent` most recently left- * conversations.+ * conversations, or would exceed the aggregate payload budget.* Releases wait while any mutation is pending, since mutation callbacks write into these caches.* Returning to a released conversation mounts a fresh query, which fetches the history again.*@@ -90,10 +134,12 @@ export function retainMessages(isExempt = () => false,recent = DEFAULT_HISTORY_CACHE_RECENT,ttlMs = DEFAULT_HISTORY_CACHE_TTL_MS,+ maxBytes = DEFAULT_HISTORY_CACHE_MAX_BYTES,}: MessagesRetentionOptions,): () => void {const cache = queryClient.getQueryCache();const leftAt = new Map<string, number>();+ const payloadSizes = new WeakMap<Query, { data: object; writes: number; bytes: number }>();const previousDefaults = queryClient.getQueryDefaults([QueryKeys.messages]);queryClient.setQueryDefaults([QueryKeys.messages], { ...previousDefaults, cacheTime: Infinity });cache.findAll([QueryKeys.messages]).forEach((query) => {@@ -152,19 +198,34 @@ export function retainMessages(schedule(PIN_RECHECK_MS);return false;});+ let retainedBytes = 0;candidates.sort(newestFirst).forEach((entry, index) => {+ releaseToolCallParts(entry.conversationId);const expiresAt = entry.leftAt + ttlMs;- if (index < recent && expiresAt > now) {- schedule(expiresAt - now);- return;+ if (index < recent && expiresAt > now && maxBytes > 0) {+ const data = entry.query.state.data;+ let bytes = 0;+ if (data != null && typeof data === 'object') {+ const writes = entry.query.state.dataUpdateCount;+ const cached = payloadSizes.get(entry.query);+ bytes =+ cached?.data === data && cached.writes === writes+ ? cached.bytes+ : estimatePayloadBytes(data, maxBytes);+ payloadSizes.set(entry.query, { data, writes, bytes });+ }+ if (retainedBytes + bytes <= maxBytes) {+ retainedBytes += bytes;+ schedule(expiresAt - now);+ return;+ }}leftAt.delete(entry.conversationId);cache.remove(entry.query);});}- /** Full tool-call parts fetched for a conversation's previews leave with its history, so a- * released conversation does not keep its largest content alive under another key. */+ /** Full preview expansions are cheap to refetch and need not occupy the history grace. */const releaseToolCallParts = (conversationId: string) => {queryClient.removeQueries({queryKey: [QueryKeys.toolCallPart, conversationId],@@ -175,6 +236,7 @@ export function retainMessages(const unsubscribe = cache.subscribe((event) => {if (event.type !== 'added' &&+ event.type !== 'updated' &&event.type !== 'observerAdded' &&event.type !== 'observerRemoved' &&event.type !== 'removed'@@ -185,8 +247,10 @@ export function retainMessages(if (conversationId == null) {return;}- if (event.type === 'added') {- schedule(RELEASE_SETTLE_MS);+ if (event.type === 'added' || event.type === 'updated') {+ if (event.type === 'added' || event.query.getObserversCount() === 0) {+ schedule(RELEASE_SETTLE_MS);+ }return;}if (event.type === 'removed') {diff --git a/client/src/hooks/Messages/__tests__/useLatestMessage.spec.tsx b/client/src/hooks/Messages/__tests__/useLatestMessage.spec.tsxindex f1a260da0a..d8c2cdda7c 100644--- a/client/src/hooks/Messages/__tests__/useLatestMessage.spec.tsx+++ b/client/src/hooks/Messages/__tests__/useLatestMessage.spec.tsx@@ -465,3 +465,100 @@ describe('getBranchSiblingIndexesForTarget', () => {]);});});++describe('rendered root branch ownership', () => {+ it.each(['routed', 'override', 'promoted'] as const)(+ 'uses the rendered root selection for all tail readers during %s navigation',+ (mode) => {+ const queryClient = createQueryClient();+ const jotaiStore = createStore();+ const owner = mode === 'promoted' ? 'server-conversation' : 'destination';+ const queryId = mode === 'promoted' ? 'new' : owner;+ const messages = [+ { ...userMessage, messageId: 'older-root', conversationId: owner, parentMessageId: null },+ {+ ...assistantMessage,+ messageId: 'older-tail',+ conversationId: owner,+ parentMessageId: 'older-root',+ },+ { ...userMessage, messageId: 'newer-root', conversationId: owner, parentMessageId: null },+ ];+ queryClient.setQueryData([QueryKeys.messages, queryId], messages);+ jotaiStore.set(siblingIdxFamily(owner), 1);+ const override = mode === 'override' ? queryId : undefined;+ const { result } = renderHook(+ () => ({+ message: useLatestMessage(0, override),+ id: useLatestMessageId(0, override),+ meta: useLatestMessageMeta(0, override),+ read: useGetLatestMessage(0, override),+ }),+ {+ wrapper: createWrapper(+ queryClient,+ ({ set }) => set(store.conversationByIndex(0), conversation),+ mode === 'override' ? '/c/source' : `/c/${queryId}`,+ jotaiStore,+ ),+ },+ );++ expect(result.current.message?.messageId).toBe('older-tail');+ expect(result.current.id).toBe('older-tail');+ expect(result.current.meta?.messageId).toBe('older-tail');+ expect(result.current.read()?.messageId).toBe('older-tail');+ },+ );+});++describe('deep conversation branches', () => {+ const messages = Array.from({ length: 12_000 }, (_, index) => ({+ ...userMessage,+ messageId: `deep-${index}`,+ parentMessageId: index === 0 ? null : `deep-${index - 1}`,+ }));+ const olderTail = {+ ...assistantMessage,+ messageId: 'older-tail',+ parentMessageId: 'deep-11999',+ };+ const newerTail = { ...olderTail, messageId: 'newer-tail' };+ const branched = [...messages, olderTail, newerTail];++ it('collects deep branch parents without using the JavaScript call stack', () => {+ expect(getMessageBranchSiblingParentIds(branched, 'deep')).toEqual(['deep-11999']);+ expect(getMessageBranchSiblingParentIds([userMessage, assistantMessage], 'small')).toEqual([]);+ });++ it('restores deep older branches and handles a missing target without overflowing', () => {+ expect(getBranchSiblingIndexesForTarget(branched, 'older-tail', 'deep')).toEqual([+ { parentMessageId: 'deep-11999', siblingIdx: 1 },+ ]);+ expect(getBranchSiblingIndexesForTarget(branched, 'missing', 'deep')).toEqual([]);+ });++ it('stops at an ancestor target without descending through its entire history', () => {+ expect(getBranchSiblingIndexesForTarget(branched, 'deep-0', 'deep')).toEqual([]);+ });++ it('discards branch choices from an unrelated subtree', () => {+ const roots = [+ { ...userMessage, messageId: 'root-old', parentMessageId: null },+ { ...assistantMessage, messageId: 'old-first', parentMessageId: 'root-old' },+ { ...assistantMessage, messageId: 'old-second', parentMessageId: 'root-old' },+ { ...userMessage, messageId: 'root-new', parentMessageId: null },+ { ...assistantMessage, messageId: 'new-first', parentMessageId: 'root-new' },+ { ...assistantMessage, messageId: 'new-second', parentMessageId: 'root-new' },+ ];+ expect(getMessageBranchSiblingParentIds(roots, 'roots')).toEqual([+ 'roots',+ 'root-old',+ 'root-new',+ ]);+ expect(getBranchSiblingIndexesForTarget(roots, 'new-first', 'roots')).toEqual([+ { parentMessageId: 'roots', siblingIdx: 0 },+ { parentMessageId: 'root-new', siblingIdx: 1 },+ ]);+ });+});diff --git a/client/src/hooks/Messages/__tests__/useMessagesRetention.spec.tsx b/client/src/hooks/Messages/__tests__/useMessagesRetention.spec.tsxindex ccbe0df14e..1f958b6649 100644--- a/client/src/hooks/Messages/__tests__/useMessagesRetention.spec.tsx+++ b/client/src/hooks/Messages/__tests__/useMessagesRetention.spec.tsx@@ -100,7 +100,7 @@ describe('useMessagesRetention', () => {jest.useFakeTimers();getMessagesByConvoId.mockImplementation(async (id: string) => history(id));getStartupConfig.mockResolvedValue({- interface: { historyCacheTtlMs: TTL, historyCacheRecent: 1 },+ interface: { historyCacheTtlMs: TTL, historyCacheRecent: 1, historyCacheMaxBytes: 4096 },} as TStartupConfig);queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });render(@@ -151,6 +151,50 @@ describe('useMessagesRetention', () => {await waitFor(() => expect(isCached(queryClient, 'idle')).toBe(true));});+ it('releases a very large chat after switching to an existing small chat and refetches on return', async () => {+ expect(await screen.findByText('transcript of idle')).toBeInTheDocument();+ const largeHistory = history('large');+ largeHistory.push({+ ...largeHistory[0],+ messageId: 'large-2',+ text: 'x'.repeat(10_000),+ });+ let finishRefresh: (() => void) | undefined;+ const smallRefresh = new Promise<TMessage[]>((resolve) => {+ finishRefresh = () => resolve(history('small'));+ });+ let smallFetches = 0;+ getMessagesByConvoId.mockImplementation(async (id: string) => {+ if (id === 'small' && ++smallFetches === 2) {+ return smallRefresh;+ }+ return id === 'large' ? largeHistory : history(id);+ });+ await open('small');+ await open('large');+ expect(isCached(queryClient, 'large')).toBe(true);+ const partsKey = [QueryKeys.toolCallPart, 'large', 'large-2', 'call'];+ queryClient.setQueryData(partsKey, { tool_call: { output: 'full output' } });++ act(() => {+ navigate('/c/small');+ });+ expect(screen.getByText('transcript of small')).toBeInTheDocument();+ await waitFor(() => expect(fetchesOf('small')).toBe(2));+ elapse(RELEASE_SETTLE_MS);++ expect(screen.getByText('transcript of small')).toBeInTheDocument();+ expect(isCached(queryClient, 'large')).toBe(false);+ expect(isCached(queryClient, 'small')).toBe(true);+ expect(queryClient.getQueryData(partsKey)).toBeUndefined();+ await act(async () => {+ finishRefresh?.();+ await smallRefresh;+ });+ await open('large');+ expect(fetchesOf('large')).toBe(2);+ });+it('keeps the routed conversation while its view has not subscribed yet', async () => {expect(await screen.findByText('transcript of idle')).toBeInTheDocument();await open('other');diff --git a/client/src/hooks/Messages/__tests__/useThreadRows.spec.tsx b/client/src/hooks/Messages/__tests__/useThreadRows.spec.tsxnew file mode 100644index 0000000000..3580213802--- /dev/null+++ b/client/src/hooks/Messages/__tests__/useThreadRows.spec.tsx@@ -0,0 +1,72 @@+import React from 'react';+import { Provider, createStore } from 'jotai';+import { renderHook } from '@testing-library/react';+import type { TMessage } from 'librechat-data-provider';+import useThreadRows from '../useThreadRows';+import * as thread from '~/utils/thread';++function history(conversationId: string, count: number): TMessage[] {+ return Array.from({ length: count }, (_, index) => ({+ messageId: `${conversationId}-${index}`,+ parentMessageId: index === 0 ? null : `${conversationId}-${index - 1}`,+ conversationId,+ text: `${index}`,+ })) as TMessage[];+}++function setup(messages: TMessage[] | null, conversationId: string) {+ const store = createStore();+ return renderHook(+ (props: { messages: TMessage[] | null; conversationId: string }) =>+ useThreadRows(props.messages, props.conversationId),+ {+ initialProps: { messages, conversationId },+ wrapper: ({ children }: { children: React.ReactNode }) => (+ <Provider store={store}>{children}</Provider>+ ),+ },+ );+}++describe('useThreadRows ownership', () => {+ it('does not index the departing large thread for row reuse in another conversation', () => {+ const resolve = jest.spyOn(thread, 'resolveThreadRows');+ const large = history('large', 12_000);+ const small = history('small', 2);+ const { result, rerender } = setup(large, 'large');+ expect(result.current).toHaveLength(12_000);++ rerender({ messages: small, conversationId: 'small' });++ expect(result.current).toHaveLength(2);+ expect(resolve.mock.calls[resolve.mock.calls.length - 1][3]).toBeNull();+ expect(result.current?.[0].source).toBe(small[0]);+ });++ it('preserves row identity for unchanged messages within the same conversation', () => {+ const messages = history('same', 3);+ const { result, rerender } = setup(messages, 'same');+ const first = result.current?.[0];+ const tail = result.current?.[2];++ rerender({+ messages: [messages[0], messages[1], { ...messages[2], text: 'updated' }],+ conversationId: 'same',+ });++ expect(result.current?.[0]).toBe(first);+ expect(result.current?.[2]).not.toBe(tail);+ });++ it('releases the previous rows when the transcript is absent', () => {+ const resolve = jest.spyOn(thread, 'resolveThreadRows');+ const messages = history('same', 3);+ const { result, rerender } = setup(messages, 'same');++ rerender({ messages: null, conversationId: 'same' });+ expect(result.current).toBeNull();+ rerender({ messages, conversationId: 'same' });++ expect(resolve.mock.calls[resolve.mock.calls.length - 1][3]).toBeNull();+ });+});diff --git a/client/src/hooks/Messages/useLatestMessage.ts b/client/src/hooks/Messages/useLatestMessage.tsindex 1fb8483fb9..0fdb076084 100644--- a/client/src/hooks/Messages/useLatestMessage.ts+++ b/client/src/hooks/Messages/useLatestMessage.ts@@ -66,7 +66,8 @@ function useLatestMessageSiblingIndexes(rootSiblingKey: string | null,) {const selectParentIds = useCallback(- (messages: TMessage[]) => getMessageBranchSiblingParentIds(messages, rootSiblingKey),+ (messages: TMessage[]) =>+ getMessageBranchSiblingParentIds(messages, messages[0]?.conversationId ?? rootSiblingKey),[rootSiblingKey],);const parentIds = useMessagesCacheSelect(messagesQueryId, selectParentIds) ?? EMPTY_PARENT_IDS;@@ -85,7 +86,7 @@ export function useLatestMessage((messages: TMessage[]) =>selectActiveBranchTail(messages,- conversationId,+ messages[0]?.conversationId ?? conversationId,(parentId) => siblingIndexes[getParentLookupKey(parentId)] ?? 0,),[conversationId, siblingIndexes],@@ -105,7 +106,7 @@ export function useLatestMessageId((messages: TMessage[]) =>selectActiveBranchTail(messages,- conversationId,+ messages[0]?.conversationId ?? conversationId,(parentId) => siblingIndexes[getParentLookupKey(parentId)] ?? 0,)?.messageId ?? null,[conversationId, siblingIndexes],@@ -133,7 +134,7 @@ export function useLatestMessageMeta((messages: TMessage[]): TLatestMessageMeta | null => {const tail = selectActiveBranchTail(messages,- conversationId,+ messages[0]?.conversationId ?? conversationId,(parentId) => siblingIndexes[getParentLookupKey(parentId)] ?? 0,);if (!tail) {@@ -171,8 +172,10 @@ export function useGetLatestMessage(}const messages =queryClient.getQueryData<TMessage[]>([QueryKeys.messages, messagesQueryId]) ?? [];- return selectActiveBranchTail(messages, conversationId, (parentId) =>- jotaiStore.get(siblingIdxFamily(siblingKey(parentId))),+ return selectActiveBranchTail(+ messages,+ messages[0]?.conversationId ?? conversationId,+ (parentId) => jotaiStore.get(siblingIdxFamily(siblingKey(parentId))),);}, [queryClient, jotaiStore, conversationId, messagesQueryId]);}diff --git a/client/src/hooks/Messages/useMessagesRetention.ts b/client/src/hooks/Messages/useMessagesRetention.tsindex d60b89fd6b..61dd5a22a1 100644--- a/client/src/hooks/Messages/useMessagesRetention.ts+++ b/client/src/hooks/Messages/useMessagesRetention.ts@@ -25,13 +25,14 @@ const carriesAssistantThread = (queryClient: QueryClient, conversationId: string* Releases left conversations' message histories from the query cache (see `retainMessages`),* keeping the routed conversation, any conversation with a running job, and Assistants* conversations. The grace period and how many conversations keep it come from- * `interface.historyCacheTtlMs` and `interface.historyCacheRecent`.+ * `interface.historyCacheTtlMs`, `interface.historyCacheRecent` and `interface.historyCacheMaxBytes`.*/export default function useMessagesRetention(): void {const queryClient = useQueryClient();const { data: startupConfig } = useGetStartupConfig();const ttlMs = startupConfig?.interface?.historyCacheTtlMs;const recent = startupConfig?.interface?.historyCacheRecent;+ const maxBytes = startupConfig?.interface?.historyCacheMaxBytes;const routeConversationId = useMatch('/c/:conversationId')?.params.conversationId;const routeConversationIdRef = useRef(routeConversationId);routeConversationIdRef.current = routeConversationId;@@ -41,11 +42,12 @@ export default function useMessagesRetention(): void {retainMessages(queryClient, {ttlMs,recent,+ maxBytes,isPinned: (conversationId) =>conversationId === routeConversationIdRef.current ||hasActiveJob(queryClient, conversationId),isExempt: (conversationId) => carriesAssistantThread(queryClient, conversationId),}),- [queryClient, ttlMs, recent],+ [queryClient, ttlMs, recent, maxBytes],);}diff --git a/client/src/hooks/Messages/useThreadRows.ts b/client/src/hooks/Messages/useThreadRows.tsindex beae49b4ed..e119aa7b53 100644--- a/client/src/hooks/Messages/useThreadRows.ts+++ b/client/src/hooks/Messages/useThreadRows.ts@@ -47,18 +47,20 @@ export default function useThreadRows();const siblingIndexes = useSiblingIndexes(branchKeys);- const previousRowsRef = useRef<ThreadRow[] | null>(null);+ const previousRowsRef = useRef<{ rootKey: string; rows: ThreadRow[] } | null>(null);const rows = useMemo(() => {if (!index) {+ previousRowsRef.current = null;return null;}+ const previous = previousRowsRef.current;const next = resolveThreadRows(index,rootSiblingKey,(key) => siblingIndexes[key] ?? 0,- previousRowsRef.current,+ previous?.rootKey === rootSiblingKey ? previous.rows : null,);- previousRowsRef.current = next;+ previousRowsRef.current = { rootKey: rootSiblingKey, rows: next };return next;}, [index, rootSiblingKey, siblingIndexes]);diff --git a/client/src/utils/messages.ts b/client/src/utils/messages.tsindex 8f79277dca..55794853ee 100644--- a/client/src/utils/messages.ts+++ b/client/src/utils/messages.ts@@ -75,24 +75,21 @@ export const getMessageBranchSiblingParentIds = (}const parentIds = new Set<string | null>();- const collectBranchParents = (- siblings: TMessage[] | undefined,- parentMessageId: string | null | undefined,- ) => {- if (!siblings?.length) {- return;+ const stack = [{ siblings: messagesTree, parentMessageId: rootSiblingKey, index: 0 }];+ while (stack.length > 0) {+ const frame = stack[stack.length - 1];+ if (frame.index === 0 && frame.siblings.length > 1) {+ parentIds.add(frame.parentMessageId ?? null);}-- if (siblings.length > 1) {- parentIds.add(parentMessageId ?? null);+ const message = frame.siblings[frame.index++];+ if (!message) {+ stack.pop();+ continue;}-- for (const message of siblings) {- collectBranchParents(message.children, message.messageId);+ if (message.children?.length) {+ stack.push({ siblings: message.children, parentMessageId: message.messageId, index: 0 });}- };-- collectBranchParents(messagesTree, rootSiblingKey);+ }return Array.from(parentIds);};@@ -111,38 +108,37 @@ export const getBranchSiblingIndexesForTarget = (}const branchIndexes: BranchSiblingIndex[] = [];- const findTargetPath = (- siblings: TMessage[] | undefined,- parentMessageId: string | null | undefined,- ): boolean => {- if (!siblings?.length) {- return false;+ const stack = [+ { siblings: messagesTree, parentMessageId: rootSiblingKey, index: 0, branchCount: 0 },+ ];+ while (stack.length > 0) {+ const frame = stack[stack.length - 1];+ const index = frame.index++;+ const message = frame.siblings[index];+ if (!message) {+ stack.pop();+ continue;}-- for (let index = 0; index < siblings.length; index++) {- const message = siblings[index];- if (!message) {- continue;- }-- const isTarget = message.messageId === targetMessageId;- const childHasTarget = findTargetPath(message.children, message.messageId);- if (isTarget || childHasTarget) {- if (siblings.length > 1) {- branchIndexes.unshift({- parentMessageId,- siblingIdx: siblings.length - index - 1,- });- }- return true;- }+ branchIndexes.length = frame.branchCount;+ if (frame.siblings.length > 1) {+ branchIndexes.push({+ parentMessageId: frame.parentMessageId,+ siblingIdx: frame.siblings.length - index - 1,+ });}-- return false;- };-- findTargetPath(messagesTree, rootSiblingKey);- return branchIndexes;+ if (message.messageId === targetMessageId) {+ return branchIndexes;+ }+ if (message.children?.length) {+ stack.push({+ siblings: message.children,+ parentMessageId: message.messageId,+ index: 0,+ branchCount: branchIndexes.length,+ });+ }+ }+ return [];};export const getLatestText = (message?: TMessage | null, includeIndex?: boolean): string => {diff --git a/packages/data-provider/src/config.spec.ts b/packages/data-provider/src/config.spec.tsindex dee3a3ed83..839b956e65 100644--- a/packages/data-provider/src/config.spec.ts+++ b/packages/data-provider/src/config.spec.ts@@ -4,6 +4,7 @@ import {allowedAddressesSchema,agentsEndpointSchema,DEFAULT_RETAINED_ANSWER_TOKENS,+ DEFAULT_HISTORY_CACHE_MAX_BYTES,DEFAULT_MAX_RETAINED_TOOL_COUNT_CHARS,bedrockModels,configSchema,@@ -257,6 +258,26 @@ describe('repository instruction configuration', () => {});});+describe('inactive history payload budget', () => {+ it('defaults the budget with and without an interface block', () => {+ expect(interfaceSchema.parse({}).historyCacheMaxBytes).toBe(DEFAULT_HISTORY_CACHE_MAX_BYTES);+ expect(interfaceSchema.parse(undefined).historyCacheMaxBytes).toBe(+ DEFAULT_HISTORY_CACHE_MAX_BYTES,+ );+ });++ it('accepts zero to disable the inactive-history grace', () => {+ expect(interfaceSchema.parse({ historyCacheMaxBytes: 0 }).historyCacheMaxBytes).toBe(0);+ });++ it.each([-1, 1.5, Number.MAX_SAFE_INTEGER + 1, '8MB'])(+ 'rejects an invalid payload budget %p',+ (historyCacheMaxBytes) => {+ expect(interfaceSchema.safeParse({ historyCacheMaxBytes }).success).toBe(false);+ },+ );+});+describe('steer escalation confirmation timeout', () => {it('defaults to the existing ten-second confirmation window', () => {expect(interfaceSchema.parse({}).steerArmConfirmationTimeoutMs).toBe(diff --git a/packages/data-provider/src/config.ts b/packages/data-provider/src/config.tsindex 3d75db9717..352869f1f2 100644--- a/packages/data-provider/src/config.ts+++ b/packages/data-provider/src/config.ts@@ -2622,6 +2622,8 @@ export const DEFAULT_COMPOSER_RECENT_FILES = 5;export const DEFAULT_HISTORY_CACHE_TTL_MS = 60_000;/** Most recently left conversations whose message history keeps that grace. */export const DEFAULT_HISTORY_CACHE_RECENT = 1;+/** Estimated payload bytes retained across inactive conversation histories. */+export const DEFAULT_HISTORY_CACHE_MAX_BYTES = 8 * 1024 * 1024;const mcpServersSchema = z.object({@@ -3104,6 +3106,15 @@ export const interfaceSchema = z/** How many of the most recently left conversations keep that grace; older ones are* released at the next conversation switch. */historyCacheRecent: z.number().int().min(0).max(20).default(DEFAULT_HISTORY_CACHE_RECENT),+ /** Aggregate estimated payload budget for histories kept during the grace period.+ * Oversized histories reload on return; observed, running and Assistants histories stay.+ * Zero disables the inactive-history grace. This is not a browser heap limit. */+ historyCacheMaxBytes: z+ .number()+ .int()+ .min(0)+ .max(Number.MAX_SAFE_INTEGER)+ .default(DEFAULT_HISTORY_CACHE_MAX_BYTES),}).default({modelSelect: true,@@ -3194,6 +3205,7 @@ export const interfaceSchema = zcomposerRecentFiles: DEFAULT_COMPOSER_RECENT_FILES,historyCacheTtlMs: DEFAULT_HISTORY_CACHE_TTL_MS,historyCacheRecent: DEFAULT_HISTORY_CACHE_RECENT,+ historyCacheMaxBytes: DEFAULT_HISTORY_CACHE_MAX_BYTES,});export type TInterfaceConfig = z.infer<typeof interfaceSchema>;diff --git a/packages/data-schemas/src/app/interface.spec.ts b/packages/data-schemas/src/app/interface.spec.tsindex 5441ca4b3a..e914f7b8ff 100644--- a/packages/data-schemas/src/app/interface.spec.ts+++ b/packages/data-schemas/src/app/interface.spec.ts@@ -125,13 +125,17 @@ describe('loadDefaultInterface', () => {});expect(defaultInterface?.historyCacheTtlMs).toBe(60_000);expect(defaultInterface?.historyCacheRecent).toBe(1);+ expect(defaultInterface?.historyCacheMaxBytes).toBe(8 * 1024 * 1024);const configuredInterface = await loadDefaultInterface({- config: { interface: { historyCacheTtlMs: 0, historyCacheRecent: 3 } },+ config: {+ interface: { historyCacheTtlMs: 0, historyCacheRecent: 3, historyCacheMaxBytes: 0 },+ },configDefaults,});expect(configuredInterface?.historyCacheTtlMs).toBe(0);expect(configuredInterface?.historyCacheRecent).toBe(3);+ expect(configuredInterface?.historyCacheMaxBytes).toBe(0);});it('uses and preserves the schema default for steer arm confirmation', async () => {diff --git a/packages/data-schemas/src/app/interface.ts b/packages/data-schemas/src/app/interface.tsindex db2a63537b..c9b3551eb0 100644--- a/packages/data-schemas/src/app/interface.ts+++ b/packages/data-schemas/src/app/interface.ts@@ -64,6 +64,7 @@ export async function loadDefaultInterface({composerRecentFiles: interfaceConfig?.composerRecentFiles ?? defaults.composerRecentFiles,historyCacheTtlMs: interfaceConfig?.historyCacheTtlMs ?? defaults.historyCacheTtlMs,historyCacheRecent: interfaceConfig?.historyCacheRecent ?? defaults.historyCacheRecent,+ historyCacheMaxBytes: interfaceConfig?.historyCacheMaxBytes ?? defaults.historyCacheMaxBytes,// Permissions and related settings - only include if explicitly configuredbookmarks: interfaceConfig?.bookmarks,