🔌 fix: Reattach a Resume Stream Whose Body Ends Before Final (#16438)

(cherry picked from commit 9ac96c5ca9)

Original-PR: https://github.com/LibreChat-AI/LibreChat/pull/16438
This commit is contained in:
Marco Beretta 2026-09-28 12:32:10 +02:00 • committed by Lia
parent f18db2e84e
commit ca8fb372e7
5 changed files with 201 additions and 14 deletions

View file

@ -375,7 +375,7 @@ describe('createSSETransport().reconnectToStream', () => {
expect(connection.closed).toBe(true);
});
it('reports closed once the response body ends without a terminal event', () => {
it('reports a body that ends before the terminal frame as a dropped connection', () => {
const connection = attach();
current().receiveHeaders();
current().write(message({ created: true, message: {} }));
@ -384,8 +384,63 @@ describe('createSSETransport().reconnectToStream', () => {
current().emit('load');
expect(connection.closed).toBe(true);
expect(events).toEqual([
{ type: 'open' },
{ type: 'created', data: { created: true, message: {} } },
{ type: 'error', status: 0 },
]);
controller.abort();
expect(events.map((event) => event.type)).toEqual(['open', 'created']);
expect(events).toHaveLength(3);
});
it('ends quietly once the terminal frame arrived before the body ended', () => {
attach();
current().receiveHeaders();
current().write(message({ final: true, terminalStatus: 'complete' }));
current().emit('load');
expect(events.map((event) => event.type)).toEqual(['open', 'final']);
});
it('reports a body that ends without carrying any frame', () => {
attach();
current().receiveHeaders();
current().emit('load');
expect(events).toEqual([{ type: 'open' }, { type: 'error', status: 0 }]);
});
it('counts the terminal frame parsed out of the closing chunk', () => {
attach();
current().receiveHeaders();
/** The trailing frame has no blank line yet, so `progress` keeps it buffered
* and only the closing `load` parses and dispatches it. */
current().write('data: ' + JSON.stringify({ final: true }) + '\n');
current().emit('load');
expect(events.map((event) => event.type)).toEqual(['open', 'final']);
});
it('reports a body that ends without the final frame after a 401 reattachment', async () => {
jest.spyOn(request, 'refreshToken').mockResolvedValue({ token: 'token-2' } as never);
jest.spyOn(request, 'dispatchTokenUpdatedEvent').mockImplementation(() => undefined);
attach();
current().status = 401;
current().write('Unauthorized');
await new Promise(process.nextTick);
current().receiveHeaders();
current().write(message({ created: true, message: {} }));
current().emit('load');
expect(events).toEqual([
{ type: 'open' },
{ type: 'created', data: { created: true, message: {} } },
{ type: 'error', status: 0 },
]);
});
it('refreshes the token on a 401 and reattaches with the request headers', async () => {

View file

@ -33,7 +33,10 @@ interface MockSSEInstance {
headers: Record<string, string>;
readyState: number;
_listeners: Record<string, SSEEventListener>;
_emit: (event: string, data?: Partial<MessageEvent> & { responseCode?: number }) => void;
_emit: (
event: string,
data?: Partial<MessageEvent> & { responseCode?: number; readyState?: number },
) => void;
}
const mockSSEInstances: MockSSEInstance[] = [];
@ -1631,6 +1634,34 @@ describe('useResumableSSE', () => {
unmount();
});
it('reattaches without waiting for the foreground once the resume body ends before final', async () => {
jest.useFakeTimers();
const submission = buildSubmission();
const chatHelpers = buildChatHelpers();
const { unmount } = renderHook(() => useResumableSSE(submission, chatHelpers));
await flushMicrotasks();
const initialSSE = getLastSSE();
await act(async () => {
initialSSE._emit('message', {
data: JSON.stringify({ event: 'on.message.delta', data: { text: 'part' } }),
});
});
await act(async () => {
initialSSE._emit('readystatechange', { readyState: 2 });
});
await advanceRetryTimer(1000);
expect(mockSSEInstances).toHaveLength(2);
expect(getLastSSE()._url).toBe(
'/api/agents/chat/stream/stream-123?resume=true&generationCreatedAt=1000&generationProtocolVersion=2',
);
expect(mockErrorHandler).not.toHaveBeenCalled();
unmount();
});
it('preserves the generation protocol header across a 401 token refresh', async () => {
(request.refreshToken as jest.Mock).mockResolvedValueOnce({ token: 'refreshed-token' });
const submission = buildSubmission();

View file

@ -10,6 +10,7 @@ import type {
import { normalizeFrame } from './frames';
type StreamErrorEvent = MessageEvent & { responseCode?: number };
type ReadyStateChangeEvent = Event & { readyState?: number };
type EventCallback = (event: ChatEvent) => void;
const jsonHeaders = (token?: string) => ({
@ -156,14 +157,32 @@ export function createSSETransport({ token }: { token?: string }): ChatTransport
method: 'GET',
});
/**
* sse.js closes a 2xx body that simply ends without dispatching
* anything, so the frames decide what that means: after `final` it is
* the ordinary end of the stream, before it an intermediary dropped the
* connection underneath the attachment. `failureSeen` is set by every
* `error` and `abort` (including a 401 whose refresh restreams), so the
* silent close below does not add a second verdict to one they already
* accounted for; `open` clears it for the refreshed reattachment.
*/
let final = false;
let failureSeen = false;
const dispatch = (event: ChatEvent) => {
final ||= event.type === 'final';
onEvent(event);
};
sse.addEventListener('open', () => {
onEvent({ type: 'open' });
failureSeen = false;
dispatch({ type: 'open' });
});
sse.addEventListener('message', emitFrame(onEvent));
sse.addEventListener('message', emitFrame(dispatch));
let refreshed = false;
sse.addEventListener('error', async (e: StreamErrorEvent) => {
failureSeen = true;
if (e.responseCode === 401 && !refreshed) {
refreshed = true;
const refreshedToken = await refreshToken();
@ -177,7 +196,7 @@ export function createSSETransport({ token }: { token?: string }): ChatTransport
return;
}
}
onEvent({
dispatch({
type: 'error',
status: e.responseCode,
data: parseErrorBody(e.data, e.responseCode),
@ -186,7 +205,15 @@ export function createSSETransport({ token }: { token?: string }): ChatTransport
/** sse.js dispatches `abort` when the XHR is cancelled, by our close or by the user agent. */
sse.addEventListener('abort', () => {
onEvent(signal.aborted ? { type: 'abort' } : { type: 'error', status: 0 });
failureSeen = true;
dispatch(signal.aborted ? { type: 'abort' } : { type: 'error', status: 0 });
});
sse.addEventListener('readystatechange', (e: ReadyStateChangeEvent) => {
if (e.readyState !== SSE.CLOSED || final || failureSeen) {
return;
}
dispatch({ type: 'error', status: 0 });
});
signal.addEventListener('abort', () => sse.close(), { once: true });

View file

@ -0,0 +1,71 @@
import { expect, test } from '@playwright/test';
import {
MOCK_ENDPOINTS,
NEW_CHAT_PATH,
messagesView,
selectMockEndpoint,
sendMessage,
} from '../helpers';
/** Last chunk streamed by the fake model's slow replies (160 chunks, 0-indexed). */
const SLOW_REPLY_LAST_CHUNK = 'chunk-159';
const STREAM_ROUTE = /\/api\/agents\/chat\/stream\//;
test.describe('resume stream body end', () => {
test('reattaches and completes when the resume body ends before the terminal frame @scenario:resume-stream-reattaches-when-body-ends-before-final', async ({
page,
}) => {
test.setTimeout(120_000);
const label = `body-end-${Date.now()}`;
await page.goto(NEW_CHAT_PATH, { timeout: 10_000 });
await selectMockEndpoint(page, MOCK_ENDPOINTS[0]);
/** The first attachment is answered with a complete 2xx body that carries
* no terminal frame: what an intermediary closing the connection leaves
* the client holding. Every later request passes through untouched, so
* the reconnect the transport schedules attaches to the still-running
* turn instead of reconciling a finished one. */
let intercepted = false;
await page.route(STREAM_ROUTE, async (route) => {
if (intercepted) {
await route.continue();
return;
}
intercepted = true;
await route.fulfill({
status: 200,
headers: { 'content-type': 'text/event-stream' },
body: ': connection cut\n\n',
});
});
/** The initial attachment carries no `resume` cursor; only the reconnect
* the dropped body triggers does, so this waiter sees the recovery and
* not the connection that was cut. */
const reattached = page.waitForResponse(
(response) =>
response.request().method() === 'GET' &&
STREAM_ROUTE.test(new URL(response.url()).pathname) &&
response.url().includes('resume=true'),
{ timeout: 30_000 },
);
const response = await sendMessage(page, `E2E_SLOW_REPLY:${label}`);
expect(response.ok()).toBeTruthy();
await expect(page.getByRole('button', { name: 'Stop generating' })).toBeVisible({
timeout: 30_000,
});
const resumeResponse = await reattached;
expect(resumeResponse.status()).toBe(200);
const assistantMessage = messagesView(page).locator('.message-render').last();
await expect(assistantMessage).toContainText(SLOW_REPLY_LAST_CHUNK, {
timeout: 90_000,
});
await expect(page.getByRole('button', { name: 'Stop generating' })).toBeHidden();
});
});

View file

@ -263,8 +263,10 @@ export type ChatStreamRequest = {
/** The handle `reconnectToStream` returns for one attachment. */
export interface ChatStreamConnection {
/**
* Whether the connection has closed. A response body that simply ends
* dispatches no event, so this is the only way to see that it did.
* Whether the connection has closed. A response body that ends before the
* terminal frame reports itself as `{ type: 'error', status: 0 }`; this is
* what a check that cannot listen for events (the foreground reattach)
* reads to notice a body that ended after `final`.
*/
readonly closed: boolean;
}
@ -283,11 +285,12 @@ export interface ChatTransport<TRequest = ChatTransportRequest> {
* Attaches to a running generation and reports it through the same events
* as `send`. Aborting the signal while the stream is open emits
* `{ type: 'abort' }`; a cancel the caller did not issue (a backgrounded or
* frozen tab) is a dropped connection and emits `{ type: 'error', status: 0 }`.
* The first 401 refreshes the token and reattaches on the same handle; a
* failed refresh or a second 401 is reported as the 401, so the caller's own
* retry budget bounds it. A server-written error that is not JSON arrives as
* its raw text.
* frozen tab) is a dropped connection and emits `{ type: 'error', status: 0 }`,
* and so does a response body that ends before the terminal frame; after it
* the ordinary end of the body dispatches nothing. The first 401 refreshes
* the token and reattaches on the same handle; a failed refresh or a second
* 401 is reported as the 401, so the caller's own retry budget bounds it. A
* server-written error that is not JSON arrives as its raw text.
*
* AI SDK: `reconnectToStream`, which resolves to a stream (or `null` when
* nothing is running); here the caller learns that from a 404 `error`.