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: Drain a Follow-Up Queued in a Chat the User Left (#16632)
Marco Beretta81daae894701
diff --git a/client/src/hooks/Chat/__tests__/abort.spec.tsx b/client/src/hooks/Chat/__tests__/abort.spec.tsxindex 279bd3a984..ff30b0d457 100644--- a/client/src/hooks/Chat/__tests__/abort.spec.tsx+++ b/client/src/hooks/Chat/__tests__/abort.spec.tsx@@ -6,7 +6,12 @@ import { RecoilRoot, useSetRecoilState, useRecoilValue } from 'recoil';import { QueryClient, QueryClientProvider } from '@tanstack/react-query';import type { TSubmission, TConversation } from 'librechat-data-provider';import type { DrainAfterAbort, RunEnd } from '~/hooks/Chat/queue';-import { drainAfterAbortByIndex, runEndByIndex, resetQueueFamilies } from '~/hooks/Chat/queue';+import {+ stopRequestedByConvoId,+ drainAfterAbortByIndex,+ resetQueueFamilies,+ runEndByIndex,+} from '~/hooks/Chat/queue';import useChatHelpers from '../useChatHelpers';import { useAbortCleanup } from '../abort';import store from '~/store';@@ -380,6 +385,9 @@ describe('useChatHelpers stopGenerating (abort steer targeting)', () => {expect(mockConvertSteersToQueued).not.toHaveBeenCalled();expect(observed.runEnd).toBeNull();expect(observed.drainAfterAbort).toBe(false);+ expect(getDefaultStore().get(stopRequestedByConvoId('convo-held'))).toEqual({+ generationCreatedAt: 41,+ });});it('does not release the abort-drain queue when natural completion already settled the run', async () => {@@ -401,6 +409,29 @@ describe('useChatHelpers stopGenerating (abort steer targeting)', () => {expect(mockConvertSteersToQueued).not.toHaveBeenCalled();expect(observed.runEnd).toBeNull();expect(observed.drainAfterAbort).toBe(false);+ /** Stop lost the race, so the run's persisted outcome decides the queue if the user leaves. */+ expect(getDefaultStore().get(stopRequestedByConvoId('convo-held'))).toBeNull();+ });++ it('keeps Stop when another Stop already aborted the run', async () => {+ mockAbortMutateAsync.mockResolvedValue({+ success: false,+ settled: true,+ code: 'RUN_ALREADY_SETTLED',+ streamId: 'convo-held',+ terminalStatus: 'aborted',+ generationProtocolVersion: 2,+ });+ const { result } = setupStop('convo-held');++ await act(async () => {+ await result.current.stopGenerating();+ });++ expect(observed.runEnd).toBeNull();+ expect(getDefaultStore().get(stopRequestedByConvoId('convo-held'))).toEqual({+ generationCreatedAt: 41,+ });});it('does not let a late abort response clear another conversation epoch', async () => {diff --git a/client/src/hooks/Chat/__tests__/queue.spec.ts b/client/src/hooks/Chat/__tests__/queue.spec.tsnew file mode 100644index 0000000000..912032406a--- /dev/null+++ b/client/src/hooks/Chat/__tests__/queue.spec.ts@@ -0,0 +1,219 @@+import { createStore } from 'jotai';+import { ContentTypes } from 'librechat-data-provider';+import type { TMessage } from 'librechat-data-provider';+import type { DetachedRun } from '../queue';+import {+ settledQueuedTurnReceiptsByConvoId,+ queuedMessagesByConvoId,+ stopRequestedByConvoId,+ resolveDetachedRunEnd,+ isDetachedRunManual,+ isStoppedRun,+} from '../queue';++const CONVO_ID = 'convo-detached';+const USER_ID = 'user-1';+const EPOCH = 1000;++const run = (overrides: Partial<DetachedRun> = {}): DetachedRun => ({+ userMessageId: USER_ID,+ generationCreatedAt: EPOCH,+ ...overrides,+});++const response = (overrides: Partial<TMessage> = {}): TMessage =>+ ({+ messageId: 'response-1',+ parentMessageId: USER_ID,+ conversationId: CONVO_ID,+ isCreatedByUser: false,+ text: 'done',+ updatedAt: new Date(EPOCH + 1000).toISOString(),+ ...overrides,+ }) as TMessage;++const userMessage = { messageId: USER_ID, isCreatedByUser: true } as TMessage;++const writtenAt = (epoch: number) => new Date(epoch).toISOString();++describe('resolveDetachedRunEnd', () => {+ it('reports a completed run with its persisted response', () => {+ const end = resolveDetachedRunEnd(CONVO_ID, run(), [userMessage, response()]);+ expect(end).toEqual(+ expect.objectContaining({+ conversationId: CONVO_ID,+ outcome: 'completed',+ responseMessageId: 'response-1',+ }),+ );+ });++ it('reports a failed run as an error, so the queue waits for a manual send', () => {+ const end = resolveDetachedRunEnd(CONVO_ID, run(), [response({ error: true })]);+ expect(end?.outcome).toBe('error');+ expect(end?.responseMessageId).toBeUndefined();+ });++ it('reports an unfinished response as aborted', () => {+ const end = resolveDetachedRunEnd(CONVO_ID, run(), [response({ unfinished: true })]);+ expect(end?.outcome).toBe('aborted');+ });++ it('reports a compaction that ended without a summary as aborted, as a Stop and a failure persist alike', () => {+ const failedSummary = resolveDetachedRunEnd(CONVO_ID, run(), [+ response({+ content: [+ { type: ContentTypes.SUMMARY, content: [], failed: true, initiatedBy: 'user' },+ ] as TMessage['content'],+ }),+ ]);+ expect(failedSummary?.outcome).toBe('aborted');+ const errorPart = resolveDetachedRunEnd(CONVO_ID, run(), [+ response({+ content: [+ { type: ContentTypes.ERROR, error: 'stopped', initiatedBy: 'user' },+ ] as TMessage['content'],+ }),+ ]);+ expect(errorPart?.outcome).toBe('aborted');+ });++ it('reads a run as completed when only an ordinary failed round or a superseded failure is persisted', () => {+ const ordinaryRound = resolveDetachedRunEnd(CONVO_ID, run(), [+ response({+ content: [+ { type: ContentTypes.SUMMARY, content: [], failed: true },+ { type: ContentTypes.TEXT, text: 'answer' },+ ] as TMessage['content'],+ }),+ ]);+ expect(ordinaryRound?.outcome).toBe('completed');+ const laterUsable = resolveDetachedRunEnd(CONVO_ID, run(), [+ response({+ content: [+ { type: ContentTypes.SUMMARY, content: [], failed: true, initiatedBy: 'user' },+ {+ type: ContentTypes.SUMMARY,+ content: [{ type: ContentTypes.TEXT, text: 'summary' }],+ initiatedBy: 'user',+ },+ ] as TMessage['content'],+ }),+ ]);+ expect(laterUsable?.outcome).toBe('completed');+ });++ it('returns nothing while no response to the run is persisted', () => {+ expect(resolveDetachedRunEnd(CONVO_ID, run(), [userMessage])).toBeNull();+ expect(resolveDetachedRunEnd(CONVO_ID, run(), undefined)).toBeNull();+ });++ it('picks the response the run created among regenerated siblings', () => {+ const end = resolveDetachedRunEnd(CONVO_ID, run({ responseMessageId: 'response-2_' }), [+ response({ messageId: 'response-1', error: true }),+ response({ messageId: 'response-2' }),+ ]);+ expect(end).toEqual(+ expect.objectContaining({ outcome: 'completed', responseMessageId: 'response-2' }),+ );+ });++ it('matches a persisted response id that itself ends in an underscore', () => {+ const end = resolveDetachedRunEnd(CONVO_ID, run({ responseMessageId: 'response-2_' }), [+ response({ messageId: 'response-1' }),+ response({ messageId: 'response-2_' }),+ ]);+ expect(end?.responseMessageId).toBe('response-2_');+ });++ it('leaves a run unresolved when the response it named is not loaded', () => {+ expect(+ resolveDetachedRunEnd(CONVO_ID, run({ responseMessageId: 'response-2' }), [+ response({ messageId: 'response-1' }),+ ]),+ ).toBeNull();+ });++ it('does not stand a sibling in for the response a fresh run named', () => {+ const run = {+ userMessageId: USER_ID,+ responseMessageId: 'response-a',+ generationCreatedAt: 5000,+ };+ expect(+ resolveDetachedRunEnd(CONVO_ID, run, [+ response({ messageId: 'response-b', updatedAt: writtenAt(6000) }),+ ]),+ ).toBeNull();+ });++ it('treats the padded user id placeholder as naming no response', () => {+ const end = resolveDetachedRunEnd(CONVO_ID, run({ responseMessageId: `${USER_ID}_` }), [+ response({ messageId: 'server-response' }),+ ]);+ expect(end?.responseMessageId).toBe('server-response');+ });++ it('carries the run epoch so the drain can match server admission receipts', () => {+ const end = resolveDetachedRunEnd(+ CONVO_ID,+ { userMessageId: USER_ID, generationCreatedAt: 4200 },+ [response({ updatedAt: writtenAt(4300) })],+ );+ expect(end?.generationCreatedAt).toBe(4200);+ });++ it('ignores a row the run never wrote, even under the id it named', () => {+ expect(+ resolveDetachedRunEnd(+ CONVO_ID,+ { userMessageId: USER_ID, responseMessageId: 'response-1', generationCreatedAt: 5000 },+ [+ response({ messageId: 'response-1', updatedAt: writtenAt(4999) }),+ response({ messageId: 'other' }),+ ],+ ),+ ).toBeNull();+ });++ it('does not guess between siblings when the run named no response', () => {+ expect(+ resolveDetachedRunEnd(CONVO_ID, run(), [+ response({ messageId: 'response-1' }),+ response({ messageId: 'response-2' }),+ ]),+ ).toBeNull();+ });+});++describe('isStoppedRun', () => {+ it('holds a Stop to the run it was pressed on', () => {+ const stopped = { generationCreatedAt: 1000 };+ expect(isStoppedRun(null, 1000)).toBe(false);+ expect(isStoppedRun(stopped, 1000)).toBe(true);+ expect(isStoppedRun(stopped, 2000)).toBe(false);+ });+});++describe('isDetachedRunManual', () => {+ const CONVO = 'convo-manual';++ it('resolves an ordinary run from history, whatever the server queue holds', () => {+ const store = createStore();+ expect(isDetachedRunManual(store.get, CONVO, run())).toBe(false);+ store.set(settledQueuedTurnReceiptsByConvoId(CONVO), [+ { clientRequestId: 'queued-1', status: 'admitted', effectivePredecessorCreatedAt: EPOCH },+ ]);+ store.set(queuedMessagesByConvoId(CONVO), [+ { id: 'row-1', text: 'next', createdAt: 1, server: { status: 'sending' } },+ ]);+ expect(isDetachedRunManual(store.get, CONVO, run())).toBe(false);+ });++ it('leaves the run the user stopped, and only that run, for a manual send', () => {+ const store = createStore();+ store.set(stopRequestedByConvoId(CONVO), { generationCreatedAt: EPOCH });+ expect(isDetachedRunManual(store.get, CONVO, run())).toBe(true);+ expect(isDetachedRunManual(store.get, CONVO, run({ generationCreatedAt: 2000 }))).toBe(false);+ });+});diff --git a/client/src/hooks/Chat/queue.ts b/client/src/hooks/Chat/queue.tsindex bde2bf6468..fb9b824d94 100644--- a/client/src/hooks/Chat/queue.ts+++ b/client/src/hooks/Chat/queue.ts@@ -1,7 +1,8 @@import { atom } from 'jotai';import { atomFamily, RESET } from 'jotai/utils';+import { ContentTypes } from 'librechat-data-provider';import type { TMessage } from 'librechat-data-provider';-import type { PrimitiveAtom } from 'jotai';+import type { PrimitiveAtom, Getter } from 'jotai';import type { GenerationProtocolVersion } from '~/data-provider/SSE/protocol';/**@@ -226,6 +227,132 @@ export const drainAfterAbortByIndex = atomFamily((_index: string | number) =>atom<DrainAfterAbort | false>(false),);+/** A run whose stream this pane closed while it was still generating, because the user left its+ * chat. The server keeps going and deletes the job once it finishes, so the run's end is learned+ * from the persisted response when the user comes back instead of from the stream. Only a run+ * whose identity the server confirmed is remembered: its epoch, and the user message `created`+ * or a resume's sync named. A run left before then, a regeneration (whose new reply history+ * cannot tell from the one it replaces), or a run on a server that reports no epoch is not, and+ * its follow-up waits for a manual send. */+export type DetachedRun = {+ /** The user message the run answers, as the server named it. */+ userMessageId: string;+ /** The response placeholder's id, when the submission carried one. */+ responseMessageId?: string;+ /** The run's generation epoch. It dates the run's response and lets the queue drain match+ * server admission receipts. */+ generationCreatedAt: number;+};++export const detachedRunByConvoId = atomFamily((_conversationId: string) =>+ atom<DetachedRun | null>(null),+);++/** The run the user pressed Stop on, named by its generation epoch. A stopped run's response+ * persists like a completed one, so a run stopped and then left before its abort event arrived+ * must not be resolved as detached and drain the queue. Only a different run resets it, so a+ * stopped run the user leaves, reattaches to and leaves again stays stopped. */+export type StoppedRun = { generationCreatedAt: number };++export const stopRequestedByConvoId = atomFamily((_conversationId: string) =>+ atom<StoppedRun | null>(null),+);++/** Whether a Stop marker belongs to the run with this epoch. */+export function isStoppedRun(stopped: StoppedRun | null, generationCreatedAt: number): boolean {+ return stopped?.generationCreatedAt === generationCreatedAt;+}++/** Whether a detached run's end must be left for a manual send instead of read from history, which+ * retires its marker: the user stopped it, and a stopped response persists like a completed one+ * (a stopped compaction even settles with `unfinished: false`). */+export function isDetachedRunManual(+ get: Getter,+ conversationId: string,+ run: DetachedRun,+): boolean {+ return isStoppedRun(get(stopRequestedByConvoId(conversationId)), run.generationCreatedAt);+}++/** A compaction's outcome is the part the server stamps `initiatedBy: 'user'`. One that ended+ * without a usable summary persists the same failed summary or error part whether it failed or+ * was stopped, from this tab or another, and is settled rather than `unfinished`; history cannot+ * tell which, so it never drains the queue. Failed rounds of an ordinary run carry no stamp, and+ * a usable summary means the compaction succeeded. */+const endedWithoutSummary = (message: TMessage): boolean => {+ if (!Array.isArray(message.content)) {+ return false;+ }+ let failed = false;+ for (const part of message.content) {+ if (part?.type === ContentTypes.SUMMARY && part.initiatedBy === 'user') {+ if (part.failed !== true) {+ return false;+ }+ failed = true;+ } else if (part?.type === ContentTypes.ERROR && part.initiatedBy === 'user') {+ failed = true;+ }+ }+ return failed;+};++const writtenAt = (message: TMessage): number => {+ const stamp = message.updatedAt ?? message.createdAt;+ return stamp == null ? Number.NaN : Date.parse(stamp);+};++/**+ * The run end a detached run implies, read from persisted history: `completed` lets the queue+ * drain, while a stopped (`unfinished`) or failed response leaves the queue for a manual send, as+ * it would had the stream been attached. Only a response the server wrote at or after the run's+ * epoch can be its response, which rules out any row the run never touched. Returns `null` while+ * no such response is persisted, so nothing is drained on a guess.+ */+export function resolveDetachedRunEnd(+ conversationId: string,+ run: DetachedRun,+ messages: TMessage[] | undefined,+): RunEnd | null {+ const epoch = run.generationCreatedAt;+ const responses = (messages ?? []).filter(+ (message) =>+ message.isCreatedByUser === false &&+ message.parentMessageId === run.userMessageId &&+ writtenAt(message) >= epoch,+ );+ /** A fresh turn's placeholder is the user message id padded with `_`, which names no response;+ * a real id is matched exactly first, since persisted ids may themselves end in `_`. */+ const exact = run.responseMessageId;+ const unpadded = exact?.replace(/_+$/, '');+ const namesResponse = exact != null && unpadded !== run.userMessageId;+ let response: TMessage | undefined;+ if (namesResponse) {+ response =+ responses.find((message) => message.messageId === exact) ??+ responses.find((message) => message.messageId === unpadded);+ }+ if (response == null && !namesResponse && responses.length === 1) {+ response = responses[0];+ }+ if (response == null) {+ return null;+ }+ let outcome: RunEnd['outcome'] = 'completed';+ if (response.error === true) {+ outcome = 'error';+ } else if (response.unfinished === true || endedWithoutSummary(response)) {+ outcome = 'aborted';+ }+ return {+ conversationId,+ outcome,+ endedAt: Date.now(),+ generationCreatedAt: epoch,+ ...(outcome === 'completed' && { responseMessageId: response.messageId }),+ };+}+const clearFamily = <Param>(family: {getParams(): Iterable<Param>;remove(param: Param): void;@@ -246,4 +373,6 @@ export function resetQueueFamilies(): void {clearFamily(drainAfterAbortByIndex);clearFamily(runEndsByIndex);clearFamily(runEndByIndex);+ clearFamily(detachedRunByConvoId);+ clearFamily(stopRequestedByConvoId);}diff --git a/client/src/hooks/Chat/useChatHelpers.ts b/client/src/hooks/Chat/useChatHelpers.tsindex 234757d866..02a6905e00 100644--- a/client/src/hooks/Chat/useChatHelpers.ts+++ b/client/src/hooks/Chat/useChatHelpers.ts@@ -5,10 +5,10 @@ import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';import { Constants, QueryKeys, isAssistantsEndpoint } from 'librechat-data-provider';import type { TMessage } from 'librechat-data-provider';import type { ChatContract } from './contract';+import { stopRequestedByConvoId, drainAfterAbortByIndex, runEndByIndex } from '~/hooks/Chat/queue';import { useLatestMessage, useLatestMessageId } from '~/hooks/Messages/useLatestMessage';import { useAbortStreamMutation, supportsGenerationProtocolV2 } from '~/data-provider';import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/state';-import { drainAfterAbortByIndex, runEndByIndex } from '~/hooks/Chat/queue';import { useChatSettings } from '~/Providers/ChatSettingsContext';import useChatFunctions from '~/hooks/Chat/useChatFunctions';import useSteerConvert from '~/hooks/Chat/useSteerConvert';@@ -234,6 +234,9 @@ export default function useChatHelpers(index = 0, paramId?: string): ChatContrac// start the NEXT submission) while the abort response is in flight;// the fallback clear below must not tear down that new run.const submissionAtAbort = captureSubmission();+ const stoppedRun = { generationCreatedAt: activeGenerationCreatedAt };+ const stopFamily = stopRequestedByConvoId(conversationId);+ queueStore.set(stopFamily, stoppedRun);try {console.log('[useChatHelpers] Calling abort mutation for:', conversationId);const response = await abortStream({@@ -252,8 +255,13 @@ export default function useChatHelpers(index = 0, paramId?: string): ChatContracif (canUseV2AbortResponse && response?.settled === true) {/** Natural completion/error won the terminal CAS, so this request did* not emit an abort FINAL and must not release the abort-drain queue.- * The winning terminal event/status owns reconciliation. */+ * The winning terminal event/status owns reconciliation. A run that+ * completed or failed was not stopped, so its persisted outcome may+ * still drain; one another Stop already aborted stays stopped. */clearInterruptDrain(conversationId, activeGenerationCreatedAt);+ if (response.terminalStatus !== 'aborted' && queueStore.get(stopFamily) === stoppedRun) {+ queueStore.set(stopFamily, null);+ }queryClient.invalidateQueries({queryKey: [QueryKeys.messages, conversationId],refetchType: 'all',@@ -341,6 +349,7 @@ export default function useChatHelpers(index = 0, paramId?: string): ChatContracconversationId,endpoint,endpointType,+ queueStore,activeGenerationCreatedAt,activeGenerationProtocolVersion,abortStream,diff --git a/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts b/client/src/hooks/SSE/__tests__/useResumableSSE.spec.tsindex d641490d05..f334e13154 100644--- a/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts+++ b/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts@@ -325,7 +325,12 @@ import useResumableSSE, {ABORT_SWEEP_STATUSES,} from '~/hooks/SSE/useResumableSSE';import useSSE from '~/hooks/SSE/useSSE';-import { queuedMessagesByConvoId, resetQueueFamilies } from '~/hooks/Chat/queue';+import {+ queuedMessagesByConvoId,+ stopRequestedByConvoId,+ detachedRunByConvoId,+ resetQueueFamilies,+} from '~/hooks/Chat/queue';const CONV_ID = 'conv-abc-123';@@ -1012,6 +1017,358 @@ describe('useResumableSSE', () => {unmount();});+ describe('a run the user leaves mid-stream', () => {+ const renderLeavable = async () => {+ const chatHelpers = buildChatHelpers();+ const rendered = renderHook(+ ({ current }: { current: TSubmission | null }) => useResumableSSE(current, chatHelpers),+ { initialProps: { current: buildSubmission() as TSubmission | null } },+ );+ await flushMicrotasks();+ expect(mockSSEInstances.length).toBeGreaterThan(0);+ await confirmCreated();+ return rendered;+ };+ const detachedRun = () => getDefaultStore().get(detachedRunByConvoId(CONV_ID));+ /** The server names the run's user message, which differs from the optimistic one. */+ const confirmCreated = async () => {+ await act(async () => {+ getLastSSE()._emit('message', {+ data: JSON.stringify({+ created: true,+ message: { messageId: 'server-msg-1', conversationId: CONV_ID },+ }),+ });+ });+ };+ const confirmResume = async () => {+ await act(async () => {+ getLastSSE()._emit('message', {+ data: JSON.stringify({+ sync: true,+ resumeState: {+ runSteps: [],+ userMessage: { messageId: 'msg-1', conversationId: CONV_ID },+ },+ }),+ });+ });+ };+ const leftRun = expect.objectContaining({+ userMessageId: 'server-msg-1',+ generationCreatedAt: 1000,+ });++ it('remembers the run when navigation clears the submission, so its end is read on return', async () => {+ const { rerender, unmount } = await renderLeavable();+ rerender({ current: {} as TSubmission });+ expect(detachedRun()).toEqual(leftRun);+ unmount();+ });++ it('remembers the run when switching to a saved chat clears the submission to null', async () => {+ const { rerender, unmount } = await renderLeavable();+ rerender({ current: null });+ expect(detachedRun()).toEqual(leftRun);+ unmount();+ });++ it('records nothing for a run left before created names its user message', async () => {+ const chatHelpers = buildChatHelpers();+ const { unmount } = renderHook(() => useResumableSSE(buildSubmission(), chatHelpers));+ await flushMicrotasks();+ unmount();+ expect(detachedRun()).toBeNull();+ });++ it('records nothing for a run on a server that reports no epoch', async () => {+ (request.post as jest.Mock).mockResolvedValue({ streamId: 'stream-without-epoch' });+ const { unmount } = await renderLeavable();+ unmount();+ expect(detachedRun()).toBeNull();+ });++ it('records nothing for a regeneration, which waits for a manual send', async () => {+ const chatHelpers = buildChatHelpers();+ const resumed = { ...resumeOf(4200), isRegenerate: true } as TSubmission;+ const { unmount } = renderHook(() => useResumableSSE(resumed, chatHelpers));+ await flushMicrotasks();+ await confirmResume();+ unmount();+ expect(detachedRun()).toBeNull();+ });++ it('carries a resumed epoch once the resume names its user message', async () => {+ const chatHelpers = buildChatHelpers();+ const { unmount } = renderHook(() => useResumableSSE(resumeOf(4200), chatHelpers));+ await flushMicrotasks();+ await confirmResume();+ unmount();+ expect(detachedRun()).toEqual(+ expect.objectContaining({ userMessageId: 'msg-1', generationCreatedAt: 4200 }),+ );+ });++ it('remembers the run when the chat unmounts mid-stream', async () => {+ const { unmount } = await renderLeavable();+ unmount();+ expect(detachedRun()).toEqual(leftRun);+ });++ it('forgets a left run once a new run starts in that conversation', async () => {+ const { rerender, unmount } = await renderLeavable();+ rerender({ current: null });+ expect(detachedRun()).not.toBeNull();+ rerender({+ current: buildSubmission({+ userMessage: {+ messageId: 'msg-2',+ conversationId: CONV_ID,+ text: 'Next',+ isCreatedByUser: true,+ sender: 'User',+ parentMessageId: 'resp-1',+ },+ }),+ });+ expect(detachedRun()).toBeNull();+ unmount();+ });++ it('records no detached run once the run end already reached the drain', async () => {+ mockFetchStreamStatus.mockResolvedValue({ active: false });+ const { rerender, unmount } = await renderLeavable();+ await act(async () => {+ getLastSSE()._emit('error', { responseCode: 404 });+ });+ await waitFor(() => expect(mockSetRunEnd).toHaveBeenCalled());+ rerender({ current: {} as TSubmission });+ expect(detachedRun()).toBeNull();+ unmount();+ });++ const resumeOf = (generationCreatedAt?: number) =>+ ({+ ...buildSubmission(),+ resumeStreamId: CONV_ID,+ resumeGenerationCreatedAt: generationCreatedAt,+ }) as TSubmission;++ it('resolves a reattached run from history when its job is gone before the attach', async () => {+ getDefaultStore().set(detachedRunByConvoId(CONV_ID), {+ userMessageId: 'msg-1',+ responseMessageId: 'resp-1',+ generationCreatedAt: 4200,+ });+ mockFetchStreamStatus.mockResolvedValue({ active: false });+ mockGetQueryData.mockImplementation((queryKey: unknown[]) =>+ queryKey[0] === QueryKeys.messages+ ? [+ {+ messageId: 'resp-1',+ parentMessageId: 'msg-1',+ conversationId: CONV_ID,+ isCreatedByUser: false,+ text: 'finished while away',+ updatedAt: new Date(5000).toISOString(),+ },+ ]+ : undefined,+ );+ const resumed = resumeOf(4200);+ const chatHelpers = buildChatHelpers();+ const { unmount } = renderHook(() => useResumableSSE(resumed, chatHelpers));+ await flushMicrotasks();+ expect(detachedRun()).not.toBeNull();+ await act(async () => {+ getLastSSE()._emit('error', { responseCode: 404 });+ });+ await waitFor(() =>+ expect(mockSetRunEnd).toHaveBeenCalledWith(+ expect.objectContaining({+ outcome: 'completed',+ responseMessageId: 'resp-1',+ generationCreatedAt: 4200,+ }),+ ),+ );+ expect(mockSetRunEnd).toHaveBeenCalledTimes(1);+ expect(detachedRun()).toBeNull();+ unmount();+ });++ it('keeps the marker when 404 history has not caught up with the reattached run', async () => {+ const left = {+ userMessageId: 'msg-1',+ responseMessageId: 'resp-1',+ generationCreatedAt: 4200,+ };+ getDefaultStore().set(detachedRunByConvoId(CONV_ID), left);+ mockFetchStreamStatus.mockResolvedValue({ active: false });+ mockGetQueryData.mockImplementation((queryKey: unknown[]) =>+ queryKey[0] === QueryKeys.messages ? [] : undefined,+ );+ const resumed = resumeOf(4200);+ const chatHelpers = buildChatHelpers();+ const { unmount } = renderHook(() => useResumableSSE(resumed, chatHelpers));+ await flushMicrotasks();+ await act(async () => {+ getLastSSE()._emit('error', { responseCode: 404 });+ });+ await waitFor(() => expect(mockSetRunEnd).toHaveBeenCalledTimes(1));+ expect(mockSetRunEnd).toHaveBeenCalledWith(expect.objectContaining({ outcome: 'aborted' }));+ expect(detachedRun()).toEqual(left);+ unmount();+ });++ it('keeps the marker when 404 history holds only the unfinished disconnect snapshot', async () => {+ const left = {+ userMessageId: 'msg-1',+ responseMessageId: 'resp-1',+ generationCreatedAt: 4200,+ };+ getDefaultStore().set(detachedRunByConvoId(CONV_ID), left);+ mockFetchStreamStatus.mockResolvedValue({ active: false });+ mockGetQueryData.mockImplementation((queryKey: unknown[]) =>+ queryKey[0] === QueryKeys.messages+ ? [+ {+ messageId: 'resp-1',+ parentMessageId: 'msg-1',+ conversationId: CONV_ID,+ isCreatedByUser: false,+ text: 'partial saved when the pane left',+ unfinished: true,+ updatedAt: new Date(5000).toISOString(),+ },+ ]+ : undefined,+ );+ const resumed = resumeOf(4200);+ const chatHelpers = buildChatHelpers();+ const { unmount } = renderHook(() => useResumableSSE(resumed, chatHelpers));+ await flushMicrotasks();+ await act(async () => {+ getLastSSE()._emit('error', { responseCode: 404 });+ });+ await waitFor(() => expect(mockSetRunEnd).toHaveBeenCalledTimes(1));+ expect(mockSetRunEnd).toHaveBeenCalledWith(+ expect.objectContaining({ outcome: 'aborted', generationCreatedAt: 4200 }),+ );+ expect(detachedRun()).toEqual(left);+ unmount();+ });++ it('settles a reattached run the user stopped as stopped on 404, even if it persisted as done', async () => {+ getDefaultStore().set(detachedRunByConvoId(CONV_ID), {+ userMessageId: 'msg-1',+ responseMessageId: 'resp-1',+ generationCreatedAt: 4200,+ });+ getDefaultStore().set(stopRequestedByConvoId(CONV_ID), { generationCreatedAt: 4200 });+ mockFetchStreamStatus.mockResolvedValue({ active: false });+ mockGetQueryData.mockImplementation((queryKey: unknown[]) =>+ queryKey[0] === QueryKeys.messages+ ? [+ {+ messageId: 'resp-1',+ parentMessageId: 'msg-1',+ conversationId: CONV_ID,+ isCreatedByUser: false,+ text: 'stopped compaction settles as finished',+ updatedAt: new Date(5000).toISOString(),+ },+ ]+ : undefined,+ );+ const resumed = resumeOf(4200);+ const chatHelpers = buildChatHelpers();+ const { unmount } = renderHook(() => useResumableSSE(resumed, chatHelpers));+ await flushMicrotasks();+ await act(async () => {+ getLastSSE()._emit('error', { responseCode: 404 });+ });+ await waitFor(() => expect(mockSetRunEnd).toHaveBeenCalledTimes(1));+ expect(mockSetRunEnd).toHaveBeenCalledWith(expect.objectContaining({ outcome: 'aborted' }));+ expect(detachedRun()).toBeNull();+ unmount();+ });++ it('does not carry a resumed run into a fresh run left before created', async () => {+ const chatHelpers = buildChatHelpers();+ const rendered = renderHook(+ ({ current }: { current: TSubmission | null }) => useResumableSSE(current, chatHelpers),+ { initialProps: { current: resumeOf(4200) as TSubmission | null } },+ );+ await flushMicrotasks();+ await confirmResume();+ rendered.rerender({ current: null });+ expect(detachedRun()).toEqual(expect.objectContaining({ generationCreatedAt: 4200 }));+ rendered.rerender({+ current: buildSubmission({+ userMessage: {+ messageId: 'msg-2',+ conversationId: CONV_ID,+ text: 'Next',+ isCreatedByUser: true,+ sender: 'User',+ parentMessageId: 'resp-1',+ },+ }),+ });+ await flushMicrotasks();+ rendered.rerender({ current: null });+ expect(detachedRun()).toBeNull();+ rendered.unmount();+ });++ it('keeps a Stop through a reattach to the stopped run, so leaving again records nothing', async () => {+ getDefaultStore().set(stopRequestedByConvoId(CONV_ID), { generationCreatedAt: 4200 });+ const resumed = resumeOf(4200);+ const chatHelpers = buildChatHelpers();+ const { unmount } = renderHook(() => useResumableSSE(resumed, chatHelpers));+ await flushMicrotasks();+ await confirmResume();+ unmount();+ expect(detachedRun()).toBeNull();+ expect(getDefaultStore().get(stopRequestedByConvoId(CONV_ID))).toEqual({+ generationCreatedAt: 4200,+ });+ });++ it('drops a Stop for a reattach whose epoch the server does not report, and records nothing', async () => {+ getDefaultStore().set(stopRequestedByConvoId(CONV_ID), { generationCreatedAt: 4200 });+ const resumed = resumeOf();+ const chatHelpers = buildChatHelpers();+ const { unmount } = renderHook(() => useResumableSSE(resumed, chatHelpers));+ await flushMicrotasks();+ await confirmResume();+ expect(getDefaultStore().get(stopRequestedByConvoId(CONV_ID))).toBeNull();+ unmount();+ expect(detachedRun()).toBeNull();+ });++ it('drops a Stop once a different run reattaches, and remembers that run when left', async () => {+ getDefaultStore().set(stopRequestedByConvoId(CONV_ID), { generationCreatedAt: 4200 });+ const resumed = resumeOf(9000);+ const chatHelpers = buildChatHelpers();+ const { unmount } = renderHook(() => useResumableSSE(resumed, chatHelpers));+ await flushMicrotasks();+ expect(getDefaultStore().get(stopRequestedByConvoId(CONV_ID))).toBeNull();+ await confirmResume();+ unmount();+ expect(detachedRun()).toEqual(expect.objectContaining({ generationCreatedAt: 9000 }));+ });++ it('records no detached run for a run the user stopped before leaving', async () => {+ const { rerender, unmount } = await renderLeavable();+ getDefaultStore().set(stopRequestedByConvoId(CONV_ID), { generationCreatedAt: 1000 });+ rerender({ current: {} as TSubmission });+ expect(detachedRun()).toBeNull();+ unmount();+ });+ });+it('authorizes only the exact failed recovery source past the conversion tombstone', async () => {const parked = [{ steerId: 'failed-source', text: 'retry these words', createdAt: 1 },diff --git a/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx b/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsxindex 99e4aa0944..869956eced 100644--- a/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx+++ b/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx@@ -1,4 +1,4 @@-import { renderHook, act } from '@testing-library/react';+import { renderHook, act, waitFor } from '@testing-library/react';import { RecoilRoot, useRecoilValue, useSetRecoilState } from 'recoil';import { QueryClient, QueryClientProvider } from '@tanstack/react-query';import { Provider as JotaiProvider, createStore, useAtomValue } from 'jotai';@@ -6,10 +6,16 @@ import { Constants, ContentTypes, QueryKeys } from 'librechat-data-provider';import type { Agents, TMessage, TConversation, TSubmission } from 'librechat-data-provider';import type { MutableSnapshot } from 'recoil';import type { ReactNode } from 'react';-import type { QueuedMessage } from '~/hooks/Chat/queue';-import type { PendingSteer } from '~/hooks/Chat/queue';+import type { QueuedMessage, PendingSteer, DetachedRun } from '~/hooks/Chat/queue';+import {+ settledQueuedTurnReceiptsByConvoId,+ queuedMessagesByConvoId,+ stopRequestedByConvoId,+ pendingRunEndByConvoId,+ detachedRunByConvoId,+ resetQueueFamilies,+} from '~/hooks/Chat/queue';import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/state';-import { queuedMessagesByConvoId, resetQueueFamilies } from '~/hooks/Chat/queue';import { pendingApprovalActionFamily } from '~/components/Chat/approval/state';import { agentQueuedTurnsQueryKey } from '~/data-provider/SSE/queuedTurns';import { submissionStartFamily } from '~/store/generation';@@ -108,6 +114,9 @@ function renderUseResumeOnLoad({onQueuedMessages,submissionStart,onSubmissionStart,+ detachedRun,+ seedJotai,+ seedQueryClient,}: {messages?: TMessage[];getMessages?: () => TMessage[] | undefined;@@ -124,15 +133,23 @@ function renderUseResumeOnLoad({onQueuedMessages?: (queued: QueuedMessage[]) => void;submissionStart?: number;onSubmissionStart?: (submissionStart: number | null) => void;+ detachedRun?: DetachedRun;+ seedJotai?: (store: ReturnType<typeof createStore>) => void;+ seedQueryClient?: (client: QueryClient) => void;}) {const getMessages = jest.fn(getMessagesOverride ?? (() => messages));const jotaiStore = createStore();if (submissionStart != null) {jotaiStore.set(submissionStartFamily(0), submissionStart);}+ if (detachedRun != null) {+ jotaiStore.set(detachedRunByConvoId(conversationId), detachedRun);+ }+ seedJotai?.(jotaiStore);const queryClient = new QueryClient({defaultOptions: { queries: { retry: false } },});+ seedQueryClient?.(queryClient);let setSubmissionState: ((submission: TSubmission | null) => void) | undefined;let setIsSubmittingState: ((value: boolean) => void) | undefined;let setAttachedEpochState: ((value: number | null) => void) | undefined;@@ -2732,6 +2749,277 @@ describe('useResumeOnLoad', () => {expect(observedSteers[observedSteers.length - 1]).toEqual([failedChip]);});+ it('parks the end of a run that finished after the user left, so its queue drains', async () => {+ mockUseStreamStatus.mockReturnValue({+ isSuccess: true,+ isFetching: false,+ data: { active: false },+ });+ const completedResponse = {+ messageId: 'response-detached',+ parentMessageId: USER_MESSAGE_ID,+ conversationId: CONVERSATION_ID,+ isCreatedByUser: false,+ text: 'finished while away',+ updatedAt: new Date(42).toISOString(),+ } as TMessage;++ const { jotaiStore } = renderUseResumeOnLoad({+ messages: [buildUserMessage(CONVERSATION_ID), completedResponse],+ detachedRun: { userMessageId: USER_MESSAGE_ID, generationCreatedAt: 41 },+ });+ await act(async () => {+ await Promise.resolve();+ });++ expect(jotaiStore.get(pendingRunEndByConvoId(CONVERSATION_ID))).toEqual(+ expect.objectContaining({+ conversationId: CONVERSATION_ID,+ outcome: 'completed',+ responseMessageId: 'response-detached',+ }),+ );+ expect(jotaiStore.get(detachedRunByConvoId(CONVERSATION_ID))).toBeNull();+ });++ it('keeps the detached run until a history refetch shows its response', async () => {+ mockUseStreamStatus.mockReturnValue({+ isSuccess: true,+ isFetching: false,+ data: { active: false },+ });+ let history: TMessage[] = [buildUserMessage(CONVERSATION_ID)];+ const { jotaiStore } = renderUseResumeOnLoad({+ getMessages: () => history,+ detachedRun: { userMessageId: USER_MESSAGE_ID, generationCreatedAt: 41 },+ });+ /** The first read found no response; it is saved before the history refetch settles. */+ expect(jotaiStore.get(pendingRunEndByConvoId(CONVERSATION_ID))).toBeNull();+ expect(jotaiStore.get(detachedRunByConvoId(CONVERSATION_ID))).not.toBeNull();+ history = [+ ...history,+ {+ messageId: 'response-late',+ parentMessageId: USER_MESSAGE_ID,+ conversationId: CONVERSATION_ID,+ isCreatedByUser: false,+ text: 'saved after the first read',+ updatedAt: new Date(42).toISOString(),+ } as TMessage,+ ];++ await waitFor(() =>+ expect(jotaiStore.get(pendingRunEndByConvoId(CONVERSATION_ID))).toEqual(+ expect.objectContaining({ outcome: 'completed', responseMessageId: 'response-late' }),+ ),+ );+ expect(jotaiStore.get(detachedRunByConvoId(CONVERSATION_ID))).toBeNull();+ });++ it('resolves a detached run from refetched history, not the response it left cached', async () => {+ mockUseStreamStatus.mockReturnValue({+ isSuccess: true,+ isFetching: false,+ data: { active: false },+ });+ const historyKey = [QueryKeys.messages, CONVERSATION_ID];+ const leftResponse = {+ messageId: 'response-detached',+ parentMessageId: USER_MESSAGE_ID,+ conversationId: CONVERSATION_ID,+ isCreatedByUser: false,+ text: 'streamed before leaving',+ updatedAt: new Date(42).toISOString(),+ } as TMessage;+ let client: QueryClient | undefined;+ const { jotaiStore } = renderUseResumeOnLoad({+ getMessages: () => client?.getQueryData<TMessage[]>(historyKey),+ detachedRun: {+ userMessageId: USER_MESSAGE_ID,+ responseMessageId: 'response-detached',+ generationCreatedAt: 41,+ },+ seedQueryClient: (queryClient) => {+ client = queryClient;+ queryClient.setQueryDefaults(historyKey, {+ queryFn: async () => [+ buildUserMessage(CONVERSATION_ID),+ { ...leftResponse, unfinished: true },+ ],+ });+ queryClient.setQueryData(historyKey, [buildUserMessage(CONVERSATION_ID), leftResponse]);+ },+ });++ await waitFor(() =>+ expect(jotaiStore.get(pendingRunEndByConvoId(CONVERSATION_ID))).toEqual(+ expect.objectContaining({ outcome: 'aborted' }),+ ),+ );+ expect(jotaiStore.get(detachedRunByConvoId(CONVERSATION_ID))).toBeNull();+ });++ it('keeps the detached run for the next visit when the history refetch fails', async () => {+ mockUseStreamStatus.mockReturnValue({+ isSuccess: true,+ isFetching: false,+ data: { active: false },+ });+ const historyKey = [QueryKeys.messages, CONVERSATION_ID];+ const queryFn = jest.fn(async (): Promise<TMessage[]> => {+ throw new Error('history unavailable');+ });+ let client: QueryClient | undefined;+ const { jotaiStore } = renderUseResumeOnLoad({+ getMessages: () => client?.getQueryData<TMessage[]>(historyKey),+ detachedRun: {+ userMessageId: USER_MESSAGE_ID,+ responseMessageId: 'response-detached',+ generationCreatedAt: 41,+ },+ seedQueryClient: (queryClient) => {+ client = queryClient;+ queryClient.setQueryDefaults(historyKey, { queryFn });+ queryClient.setQueryData(historyKey, [+ buildUserMessage(CONVERSATION_ID),+ {+ messageId: 'response-detached',+ parentMessageId: USER_MESSAGE_ID,+ conversationId: CONVERSATION_ID,+ isCreatedByUser: false,+ text: 'streamed before leaving',+ updatedAt: new Date(42).toISOString(),+ } as TMessage,+ ]);+ },+ });++ await waitFor(() => expect(queryFn).toHaveBeenCalled());+ await act(async () => {+ await Promise.resolve();+ });+ expect(jotaiStore.get(pendingRunEndByConvoId(CONVERSATION_ID))).toBeNull();+ expect(jotaiStore.get(detachedRunByConvoId(CONVERSATION_ID))).not.toBeNull();+ });++ it('retires a retained marker for a run the user stopped on a later visit', async () => {+ mockUseStreamStatus.mockReturnValue({+ isSuccess: true,+ isFetching: false,+ data: { active: false },+ });+ const { jotaiStore } = renderUseResumeOnLoad({+ messages: [+ buildUserMessage(CONVERSATION_ID),+ {+ messageId: 'response-detached',+ parentMessageId: USER_MESSAGE_ID,+ conversationId: CONVERSATION_ID,+ isCreatedByUser: false,+ text: 'stopped compaction settles as finished',+ unfinished: false,+ updatedAt: new Date(42).toISOString(),+ } as TMessage,+ ],+ detachedRun: { userMessageId: USER_MESSAGE_ID, generationCreatedAt: 41 },+ seedJotai: (store) =>+ store.set(stopRequestedByConvoId(CONVERSATION_ID), { generationCreatedAt: 41 }),+ });+ await act(async () => {+ await Promise.resolve();+ });++ expect(jotaiStore.get(pendingRunEndByConvoId(CONVERSATION_ID))).toBeNull();+ expect(jotaiStore.get(detachedRunByConvoId(CONVERSATION_ID))).toBeNull();+ });++ it('parks a shared-queue run end with its epoch, so its admission receipt is matched', async () => {+ mockUseStreamStatus.mockReturnValue({+ isSuccess: true,+ isFetching: false,+ data: { active: false },+ });+ const { jotaiStore } = renderUseResumeOnLoad({+ messages: [+ buildUserMessage(CONVERSATION_ID),+ {+ messageId: 'response-detached',+ parentMessageId: USER_MESSAGE_ID,+ conversationId: CONVERSATION_ID,+ isCreatedByUser: false,+ text: 'finished while away',+ updatedAt: new Date(42).toISOString(),+ } as TMessage,+ ],+ detachedRun: { userMessageId: USER_MESSAGE_ID, generationCreatedAt: 41 },+ seedJotai: (store) =>+ store.set(settledQueuedTurnReceiptsByConvoId(CONVERSATION_ID), [+ { clientRequestId: 'queued-1', status: 'admitted', effectivePredecessorCreatedAt: 41 },+ ]),+ });+ await act(async () => {+ await Promise.resolve();+ });++ expect(jotaiStore.get(pendingRunEndByConvoId(CONVERSATION_ID))).toEqual(+ expect.objectContaining({ outcome: 'completed', generationCreatedAt: 41 }),+ );+ });++ it('parks nothing for a conversation this pane never left mid-run', async () => {+ mockUseStreamStatus.mockReturnValue({+ isSuccess: true,+ isFetching: false,+ data: { active: false },+ });+ const { jotaiStore } = renderUseResumeOnLoad({+ messages: [+ buildUserMessage(CONVERSATION_ID),+ {+ messageId: 'response-old',+ parentMessageId: USER_MESSAGE_ID,+ conversationId: CONVERSATION_ID,+ isCreatedByUser: false,+ text: 'history',+ } as TMessage,+ ],+ });+ await act(async () => {+ await Promise.resolve();+ });++ expect(jotaiStore.get(pendingRunEndByConvoId(CONVERSATION_ID))).toBeNull();+ });++ it('keeps the detached run while its job is active, so a job gone before the attach still resolves', async () => {+ mockUseStreamStatus.mockReturnValue({+ isSuccess: true,+ isFetching: false,+ data: {+ active: true,+ status: 'running',+ createdAt: 4242,+ streamId: CONVERSATION_ID,+ resumeState: {+ aggregatedContent: [{ type: ContentTypes.TEXT, text: 'partial' }],+ responseMessageId: RESPONSE_MESSAGE_ID,+ userMessage: { messageId: USER_MESSAGE_ID, conversationId: CONVERSATION_ID },+ },+ },+ });+ const detachedRun = { userMessageId: USER_MESSAGE_ID, generationCreatedAt: 4242 };+ const { jotaiStore } = renderUseResumeOnLoad({+ messages: [buildUserMessage(CONVERSATION_ID)],+ detachedRun,+ });+ await act(async () => {+ await Promise.resolve();+ });++ expect(jotaiStore.get(detachedRunByConvoId(CONVERSATION_ID))).toEqual(detachedRun);+ expect(jotaiStore.get(pendingRunEndByConvoId(CONVERSATION_ID))).toBeNull();+ });+it('converts resumeState.pendingSteers to queued when inactive (expired action, unparked queue)', async () => {const observedSteers: PendingSteer[][] = [];const observedQueues: QueuedMessage[][] = [];diff --git a/client/src/hooks/SSE/useResumableSSE.ts b/client/src/hooks/SSE/useResumableSSE.tsindex 5a7a97a9ed..8e5557ef90 100644--- a/client/src/hooks/SSE/useResumableSSE.ts+++ b/client/src/hooks/SSE/useResumableSSE.ts@@ -41,8 +41,8 @@ import type {TContextUsageEvent,ChatStreamConnection,} from 'librechat-data-provider';+import type { QueuedMessageOrigin, DrainAfterAbort, RunEnd } from '~/hooks/Chat/queue';import type { ActiveJobsResponse, StreamStatusResponse } from '~/data-provider';-import type { DrainAfterAbort, QueuedMessageOrigin } from '~/hooks/Chat/queue';import type { GenerationProtocolVersion } from '~/data-provider';import type { EventHandlerParams } from './useEventHandlers';import type { TResData, TFinalResData } from '~/common';@@ -87,6 +87,16 @@ import {fetchConversationMessages,GENERATION_PROTOCOL_VERSION,} from '~/data-provider';+import {+ resolveDetachedRunEnd,+ isDetachedRunManual,+ stopRequestedByConvoId,+ detachedRunByConvoId,+ isStoppedRun,+ drainAfterAbortByIndex,+ queuedMessagesByConvoId,+ runEndByIndex,+} from '~/hooks/Chat/queue';import {recoveryDispositionsFamily,canRestoreRecovery,@@ -96,7 +106,6 @@ import useEventHandlers, {buildCreatedInitialResponse,keepLocalCodeApprovalMode,} from './useEventHandlers';-import { drainAfterAbortByIndex, queuedMessagesByConvoId, runEndByIndex } from '~/hooks/Chat/queue';import { pendingApprovalActionFamily } from '~/components/Chat/approval/state';import { abortScrollFamily, showStopButtonByIndex } from '~/store/generation';import { withSubmittedCodeDecision } from '~/hooks/Agents/codeDecision';@@ -1336,6 +1345,9 @@ export default function useResumableSSE([convertSteersToQueued],);+ /** The generation epoch the pane's stream is attached to, kept so a run left mid-stream can+ * carry it. A server that reports no epoch leaves it unset. */+ const subscribedEpochRef = useRef<number | null>(null);/** Conversation ids are reused by successive agent turns. Keep the exact* live epoch beside the UI controls, and only clear it if the terminal event* still belongs to the epoch that installed it. */@@ -1361,7 +1373,34 @@ export default function useResumableSSE([],);- const setRunEnd = useSetAtom(runEndByIndex(runIndex));+ const publishRunEnd = useSetAtom(runEndByIndex(runIndex));+ /** Whether the current submission's run end already reached the queue drain. A run that+ * ended attached must not also be resolved as detached when the user later leaves. */+ const runEndPublishedRef = useRef(false);+ /** Whether the server named the current submission's user message (`created`, or a resume's+ * sync). Only a run whose identity it confirmed is remembered when the user leaves. */+ const identityConfirmedRef = useRef(false);+ const setRunEnd = useCallback(+ (end: RunEnd, options?: { retainDetached?: boolean }) => {+ runEndPublishedRef.current = true;+ /** The run's end reached the drain here, so a marker this pane left for the same run is+ * settled and must not be resolved a second time on a later visit. A provisional end that+ * cannot drain the queue keeps the marker, so a later read of history can still settle it. */+ if (end.conversationId != null && options?.retainDetached !== true) {+ const detachedFamily = detachedRunByConvoId(end.conversationId);+ const detached = jotaiStore.get(detachedFamily);+ if (+ detached != null &&+ (end.generationCreatedAt == null ||+ detached.generationCreatedAt === end.generationCreatedAt)+ ) {+ jotaiStore.set(detachedFamily, null);+ }+ }+ publishRunEnd(end);+ },+ [jotaiStore, publishRunEnd],+ );const setDrainAfterAbort = useSetAtom(drainAfterAbortByIndex(runIndex));const clearDrainAfterAbort = useCallback((conversationId: string, generationCreatedAt?: number) => {@@ -1465,6 +1504,7 @@ export default function useResumableSSE(if (lifecycleSignal?.aborted || submissionRef.current !== currentSubmission) {return;}+ subscribedEpochRef.current = generationCreatedAt ?? null;const startedAsNewConversation = optimisticStreamIdsRef.current.has(currentStreamId);/** Terminal handlers below are fenced by this subscription and its generation.* Clear every key the same run may have occupied, including the temporary@@ -2276,6 +2316,7 @@ export default function useResumableSSE(initialResponse: createdInitialResponse,};submissionRef.current = currentSubmission;+ identityConfirmedRef.current = true;bindResponse(currentSubmission);createdHandler(data, currentSubmission as EventSubmission);replayPreCreatedStepEvents();@@ -2379,6 +2420,9 @@ export default function useResumableSSE(currentSubmission = resumeSubmission;submissionRef.current = resumeSubmission;userMessage = resumeSubmission.userMessage;+ if (data.resumeState?.userMessage != null) {+ identityConfirmedRef.current = true;+ }/*** Totals rebuild from the persisted backfill at sync. Replayed or* gap usage events already represented in the snapshot are skipped@@ -3457,13 +3501,34 @@ export default function useResumableSSE(}// The true outcome is unknown here (job record already cleaned up):// a non-'completed' outcome releases parked interrupt flags without- // auto-sending queued messages the user may not want fired.- setRunEnd({- conversationId: recoveryConvoId,- outcome: 'aborted',- startedAsNewConvo: optimisticStreamIdsRef.current.has(currentStreamId),- endedAt: Date.now(),- });+ // auto-sending queued messages the user may not want fired. A run this+ // pane left and came back to is the exception: the history just read+ // names its response by epoch, which is the outcome it would have had.+ // History read before the job finished may not hold that response yet, or+ // may hold only the unfinished snapshot saved when the pane left, so a+ // marker that read leaves unresolved or stopped outlives this end for the+ // next visit, which reads history after the job is known to be done.+ const detachedRun = jotaiStore.get(detachedRunByConvoId(recoveryConvoId));+ const detachedResolvable =+ detachedRun != null &&+ !isDetachedRunManual(jotaiStore.get, recoveryConvoId, detachedRun);+ const detachedEnd =+ detachedResolvable && persistedMessages != null+ ? resolveDetachedRunEnd(recoveryConvoId, detachedRun, persistedMessages)+ : null;+ setRunEnd(+ {+ conversationId: recoveryConvoId,+ outcome: 'aborted',+ endedAt: Date.now(),+ ...detachedEnd,+ startedAsNewConvo: optimisticStreamIdsRef.current.has(currentStreamId),+ },+ {+ retainDetached:+ detachedResolvable && (detachedEnd == null || detachedEnd.outcome === 'aborted'),+ },+ );setStreamId(null);optimisticStreamIdsRef.current.delete(currentStreamId);createdStreamIdsRef.current.delete(currentStreamId);@@ -4294,6 +4359,29 @@ export default function useResumableSSE(});submissionRef.current = submission;+ runEndPublishedRef.current = false;+ identityConfirmedRef.current = false;+ subscribedEpochRef.current = null;+ if (submission.conversation?.conversationId != null) {+ /** A different run starting here owns the conversation's end from now on: an earlier run+ * this pane left is superseded, and a Stop belonged to that earlier run. Reattaching to+ * the same generation keeps both, since that run's end is still owed and it was stopped. */+ const startingConvoId = submission.conversation.conversationId;+ const sameGeneration = (generationCreatedAt?: number) =>+ resumeStreamId != null &&+ resumeGenerationCreatedAt != null &&+ generationCreatedAt === resumeGenerationCreatedAt;+ const stopFamily = stopRequestedByConvoId(startingConvoId);+ const stopped = jotaiStore.get(stopFamily);+ if (stopped != null && !sameGeneration(stopped.generationCreatedAt)) {+ jotaiStore.set(stopFamily, null);+ }+ const detachedFamily = detachedRunByConvoId(startingConvoId);+ const detached = jotaiStore.get(detachedFamily);+ if (detached != null && !sameGeneration(detached.generationCreatedAt)) {+ jotaiStore.set(detachedFamily, null);+ }+ }const startController = new AbortController();const { signal } = startController;const isCurrentEffect = () => !signal.aborted && submissionRef.current === submission;@@ -4823,6 +4911,33 @@ export default function useResumableSSE(stopForegroundReattachRef.current = null;// Reset reconnect counter before closing (so abort handler doesn't think we're reconnecting)reconnectAttemptRef.current = 0;+ /** The pane is moving off a run that is still generating (another chat, a new chat, or+ * an unmount): the server keeps going with no subscriber and deletes the job when done,+ * so no terminal event will reach the queue drain. Remember the run; on return its end is+ * read from history. Only a run whose identity the server confirmed (its epoch, and the+ * user message `created` or a resume's sync named) is remembered, and not a regeneration,+ * a run that already published its end, or one the user stopped. */+ const closing = submissionRef.current;+ const closingConvoId = closing?.conversation?.conversationId;+ const closingUserMessageId = closing?.userMessage?.messageId;+ const generationCreatedAt = subscribedEpochRef.current;+ if (+ streamRef.current != null &&+ identityConfirmedRef.current &&+ !runEndPublishedRef.current &&+ closing?.isRegenerate !== true &&+ closingConvoId != null &&+ closingConvoId !== Constants.NEW_CONVO &&+ closingUserMessageId != null &&+ generationCreatedAt != null &&+ !isStoppedRun(jotaiStore.get(stopRequestedByConvoId(closingConvoId)), generationCreatedAt)+ ) {+ jotaiStore.set(detachedRunByConvoId(closingConvoId), {+ userMessageId: closingUserMessageId,+ responseMessageId: closing?.initialResponse?.messageId,+ generationCreatedAt,+ });+ }streamRef.current?.abort();streamRef.current = null;// Clear handler maps to prevent memory leaks and stale statediff --git a/client/src/hooks/SSE/useResumeOnLoad.ts b/client/src/hooks/SSE/useResumeOnLoad.tsindex 01bb0c4c6a..e08d76681c 100644--- a/client/src/hooks/SSE/useResumeOnLoad.ts+++ b/client/src/hooks/SSE/useResumeOnLoad.ts@@ -34,6 +34,12 @@ import {extendActiveJobsGrace,ACTIVE_JOBS_SUCCESSOR_GRACE_MS,} from '~/data-provider';+import {+ isDetachedRunManual,+ resolveDetachedRunEnd,+ pendingRunEndByConvoId,+ detachedRunByConvoId,+} from '~/hooks/Chat/queue';import {getGenerationProtocolVersion,supportsGenerationProtocolV2,@@ -1002,10 +1008,48 @@ export default function useResumeOnLoad(// `unrecoveredSteers` above — same empty-list reconcile as the resume path.settleAppliedSteerParts(conversationId, getMessages());restoreSteerChips(conversationId, undefined);+ /** A run this pane stopped watching when the user left has ended on the server, which+ * deleted the job. Its persisted response tells how it ended; parking that end lets the+ * queue drain send a follow-up queued during the run, as an attached run would have.+ * The cached history still holds the optimistic response the pane left, which lacks the+ * persisted stop and error flags, so only a successful refetch of history may resolve it;+ * when that fails or predates the saved response, the next visit tries again. */+ const detachedFamily = detachedRunByConvoId(conversationId);+ const detachedRun = jotaiStore.get(detachedFamily);+ const parkDetachedEnd = () => {+ if (detachedRun == null || jotaiStore.get(detachedFamily) !== detachedRun) {+ return;+ }+ if (isDetachedRunManual(jotaiStore.get, conversationId, detachedRun)) {+ jotaiStore.set(detachedFamily, null);+ return;+ }+ const end = resolveDetachedRunEnd(conversationId, detachedRun, getMessages());+ if (end == null) {+ return;+ }+ jotaiStore.set(detachedFamily, null);+ jotaiStore.set(pendingRunEndByConvoId(conversationId), end);+ };+ /** A run left for a manual send is retired without a read instead of refetching on every+ * visit; the check repeats after the read, since a receipt can settle meanwhile. */+ if (detachedRun != null && isDetachedRunManual(jotaiStore.get, conversationId, detachedRun)) {+ jotaiStore.set(detachedFamily, null);+ } else if (detachedRun != null) {+ queryClient+ .refetchQueries(+ { queryKey: [QueryKeys.messages, conversationId], exact: true },+ { throwOnError: true },+ )+ .then(parkDetachedEnd, () => undefined);+ }processedConvoRef.current = conversationId;return;}+ /** Still generating: a marker left for this run stays until the resume's stream delivers its+ * end or its recovery reads it from history, and the resume's start retires a marker left+ * for any other run, so the job deleted between this read and the attach is not lost. */processedConvoRef.current = conversationId;if (handoffGenerationKey != null) {consumedHandoffGenerationRef.current = handoffGenerationKey;diff --git a/e2e/specs/mock/scenarios/queue-owners.spec.ts b/e2e/specs/mock/scenarios/queue-owners.spec.tsindex 825c466af5..ed8fe205bd 100644--- a/e2e/specs/mock/scenarios/queue-owners.spec.ts+++ b/e2e/specs/mock/scenarios/queue-owners.spec.ts@@ -1,8 +1,12 @@import { expect, test } from '@playwright/test';import type { Page } from '@playwright/test';+import type { TMessage } from 'librechat-data-provider';import {selectMockEndpoint,+ getAccessToken,messagesView,+ requestJson,+ fetchJson,sendMessage,replyPrompt,replyText,@@ -10,10 +14,14 @@ import {NEW_CHAT_PATH,} from '../helpers';-/** Interrupt preserves the response instead of draining a separate follow-up. */+/**+ * Interrupt preserves the response instead of draining a separate follow-up, and a follow-up+ * queued in a chat the user left drains once the run ends while the chat is off screen.+ */const messageInput = (page: Page) => page.getByRole('textbox', { name: 'Message input' });const duringRunSendButton = (page: Page) => page.getByTestId('during-run-send-button');+const queuedRows = (page: Page) => page.getByTestId('queued-message-row');const messageTurns = (page: Page) => messagesView(page).locator('.message-render');const uniqueLabel = (prefix: string) =>`${prefix}-${Date.now()}-${Math.floor(Math.random() * 1e6)}`;@@ -33,6 +41,29 @@ async function typeDuringRun(page: Page, text: string) {await expect(duringRunSendButton(page)).toBeVisible({ timeout: 5000 });}+/** Waits until the server has saved the slow run's complete reply. */+async function waitForServerToFinish(page: Page, conversationId: string) {+ const token = await getAccessToken(page);+ await expect+ .poll(+ async () => {+ const messages = await fetchJson<TMessage[]>(+ page,+ `/api/messages/${encodeURIComponent(conversationId)}`,+ token,+ );+ return messages.some(+ (message) =>+ !message.isCreatedByUser &&+ message.unfinished !== true &&+ JSON.stringify(message.content ?? message.text ?? '').includes('chunk-159'),+ );+ },+ { timeout: 60000 },+ )+ .toBe(true);+}+test.describe('chat-owned queue state', () => {test.beforeEach(async ({ page }) => {/** Steer is the plain-Enter default here, so Cmd/Ctrl+Enter is the queue path. */@@ -65,4 +96,120 @@ test.describe('chat-owned queue state', () => {await expect(messagesView(page).getByText('chunk-010')).toBeVisible();await expect(messagesView(page).getByText('chunk-159')).toHaveCount(0);});++ test('a follow-up queued in a chat the user left sends on return @scenario:parked-run-end-drains-on-return', async ({+ page,+ }) => {+ test.setTimeout(150000);+ const label = uniqueLabel('parked');+ const followUp = `Parked follow-up ${label}`;++ await page.goto(NEW_CHAT_PATH, { timeout: 10000 });+ await selectMockEndpoint(page, MOCK_ENDPOINTS[0]);+ const conversationId = await establishConversation(page, `parked-setup-${label}`);++ const run = await sendMessage(page, `E2E_SLOW_REPLY:${label}`);+ expect(run.ok()).toBeTruthy();+ await typeDuringRun(page, followUp);+ await messageInput(page).press('ControlOrMeta+Enter');+ await expect(queuedRows(page).filter({ hasText: followUp })).toBeVisible({ timeout: 10000 });++ /** Leave through the router, not a reload, so the in-memory queue survives the visit. */+ await page.evaluate((path) => {+ window.history.pushState({}, '', path);+ window.dispatchEvent(new PopStateEvent('popstate'));+ }, NEW_CHAT_PATH);+ await expect(page).toHaveURL(/\/c\/new$/);++ /** The run finishes while its chat is not on screen. */+ await waitForServerToFinish(page, conversationId);+ await expect(messagesView(page).getByText(followUp)).toHaveCount(0);++ await page.goBack();+ await expect(page).toHaveURL(new RegExp(`/c/${conversationId}(\\?.*)?$`));+ await expect(+ messagesView(page).locator('.user-turn').filter({ hasText: followUp }),+ ).toBeVisible({ timeout: 30000 });+ await expect(queuedRows(page).filter({ hasText: followUp })).toHaveCount(0);+ });++ test('a follow-up queued before the user stopped and left the run stays queued on return @scenario:stopped-run-left-keeps-follow-up-queued', async ({+ page,+ }) => {+ test.setTimeout(150000);+ const label = uniqueLabel('stopped');+ const followUp = `Stopped follow-up ${label}`;++ await page.goto(NEW_CHAT_PATH, { timeout: 10000 });+ await selectMockEndpoint(page, MOCK_ENDPOINTS[0]);+ const conversationId = await establishConversation(page, `stopped-setup-${label}`);++ const run = await sendMessage(page, `E2E_SLOW_REPLY:${label}`);+ expect(run.ok()).toBeTruthy();+ await expect(messagesView(page).getByText('chunk-010')).toBeVisible({ timeout: 15000 });+ await typeDuringRun(page, followUp);+ await messageInput(page).press('ControlOrMeta+Enter');+ await expect(queuedRows(page).filter({ hasText: followUp })).toBeVisible({ timeout: 10000 });++ const stop = page.getByRole('button', { name: 'Stop generating' });+ await stop.click();+ await expect(stop).toBeHidden({ timeout: 15000 });++ await page.evaluate((path) => {+ window.history.pushState({}, '', path);+ window.dispatchEvent(new PopStateEvent('popstate'));+ }, NEW_CHAT_PATH);+ await expect(page).toHaveURL(/\/c\/new$/);++ await page.goBack();+ await expect(page).toHaveURL(new RegExp(`/c/${conversationId}(\\?.*)?$`));+ await expect(queuedRows(page).filter({ hasText: followUp })).toBeVisible({ timeout: 15000 });+ await expect(+ messagesView(page).locator('.user-turn').filter({ hasText: followUp }),+ ).toHaveCount(0);+ });++ test('a follow-up queued in a chat the user left for a saved chat sends on return @scenario:parked-run-end-drains-after-switching-chats', async ({+ page,+ }) => {+ const width = page.viewportSize()?.width ?? 0;+ test.skip(width < 768, 'the conversation list is in the drawer below md');+ test.setTimeout(150000);+ const label = uniqueLabel('switched');+ const followUp = `Switched follow-up ${label}`;+ const otherTitle = `Other chat ${label}`;++ /** A saved chat to switch to, titled so its list row can be found. */+ await page.goto(NEW_CHAT_PATH, { timeout: 10000 });+ await selectMockEndpoint(page, MOCK_ENDPOINTS[0]);+ const otherId = await establishConversation(page, `switched-other-${label}`);+ await requestJson(page, {+ path: '/api/convos/update',+ token: await getAccessToken(page),+ method: 'POST',+ body: { arg: { conversationId: otherId, title: otherTitle } },+ });++ await page.goto(NEW_CHAT_PATH, { timeout: 10000 });+ await selectMockEndpoint(page, MOCK_ENDPOINTS[0]);+ const conversationId = await establishConversation(page, `switched-setup-${label}`);+ const run = await sendMessage(page, `E2E_SLOW_REPLY:${label}`);+ expect(run.ok()).toBeTruthy();+ await typeDuringRun(page, followUp);+ await messageInput(page).press('ControlOrMeta+Enter');+ await expect(queuedRows(page).filter({ hasText: followUp })).toBeVisible({ timeout: 10000 });++ /** Leave the way a user does: pick the other saved chat in the conversation list. */+ await page.getByTestId('convo-item').filter({ hasText: otherTitle }).click();+ await expect(page).toHaveURL(new RegExp(`/c/${otherId}(\\?.*)?$`));++ await waitForServerToFinish(page, conversationId);++ await page.goBack();+ await expect(page).toHaveURL(new RegExp(`/c/${conversationId}(\\?.*)?$`));+ await expect(+ messagesView(page).locator('.user-turn').filter({ hasText: followUp }),+ ).toBeVisible({ timeout: 30000 });+ await expect(queuedRows(page).filter({ hasText: followUp })).toHaveCount(0);+ });});