⏹️ fix: Reconcile Stopped Generations Before Detaching the Stream (#16788)

* fix: Reconcile stopped generations before detaching the stream

* test: Cover immediate Stop and delayed terminal reconciliation

* test: Accept both terminal outcomes of an immediate Stop

---------

Co-authored-by: Lia <lia@librechat.ai>
This commit is contained in:
lia-by-librechat[bot] 2026-10-06 07:40:59 -04:00 • committed by GitHub
parent 6b5cf0a231
commit abeb255a96
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 173 additions and 82 deletions

View file

@ -180,10 +180,13 @@ describe('useChatHelpers stopGenerating (abort steer targeting)', () => {
runEnd: null,
drainAfterAbort: false,
};
let currentSubmission: TSubmission | null = null;
function setupStop(conversationId: string, generationCreatedAt: number | null = 41) {
let setDrainAfterAbort: ((value: DrainAfterAbort | false) => void) | undefined;
const stoppedSubmission = submission('stopped-run');
function RunEndProbe() {
currentSubmission = useRecoilValue(store.submissionByIndex(INDEX));
observed.runEnd = useAtomValue(runEndByIndex(INDEX));
observed.drainAfterAbort = useAtomValue(drainAfterAbortByIndex(INDEX));
setDrainAfterAbort = useSetAtom(drainAfterAbortByIndex(INDEX));
@ -194,6 +197,8 @@ describe('useChatHelpers stopGenerating (abort steer targeting)', () => {
<QueryClientProvider client={queryClient}>
<RecoilRoot
initializeState={({ set }) => {
set(store.conversationKeysAtom, [INDEX]);
set(store.submissionByIndex(INDEX), stoppedSubmission);
set(store.conversationByIndex(INDEX), {
conversationId,
endpoint: 'agents',
@ -216,6 +221,7 @@ describe('useChatHelpers stopGenerating (abort steer targeting)', () => {
);
return {
...renderHook(() => useChatHelpers(INDEX), { wrapper }),
stoppedSubmission,
setDrainAfterAbort: (value: DrainAfterAbort | false) => setDrainAfterAbort?.(value),
};
}
@ -232,7 +238,27 @@ describe('useChatHelpers stopGenerating (abort steer targeting)', () => {
jest.restoreAllMocks();
});
it('keeps chips and the drain signal on the new composer while claiming under the resolved id', async () => {
it.each([String(Constants.NEW_CONVO), 'convo-held'])(
'keeps the stopped submission attached for terminal reconciliation in %s',
async (conversationId) => {
mockAbortMutateAsync.mockResolvedValue({
success: true,
aborted: 'convo-resolved',
generationProtocolVersion: 2,
});
const { result, stoppedSubmission } = setupStop(conversationId);
await act(async () => {
await result.current.stopGenerating();
});
expect(currentSubmission).toBe(stoppedSubmission);
expect(observed.runEnd).toBeNull();
expect(observed.drainAfterAbort).toEqual({ conversationId, generationCreatedAt: 41 });
},
);
it('keeps chips on the new composer while waiting for FINAL to drain', async () => {
const pendingSteers = [{ steerId: 's1', text: 'leftover words' }];
mockAbortMutateAsync.mockResolvedValue({
success: true,
@ -262,11 +288,7 @@ describe('useChatHelpers stopGenerating (abort steer targeting)', () => {
generationProtocolVersion: 2,
},
);
expect(observed.runEnd).toMatchObject({
conversationId: String(Constants.NEW_CONVO),
outcome: 'aborted',
generationCreatedAt: 41,
});
expect(observed.runEnd).toBeNull();
});
it('prefers the response resolved id over the client-held id for an existing conversation', async () => {
@ -288,11 +310,7 @@ describe('useChatHelpers stopGenerating (abort steer targeting)', () => {
claimConversationId: 'convo-resolved',
generationProtocolVersion: 2,
});
expect(observed.runEnd).toMatchObject({
conversationId: 'convo-resolved',
outcome: 'aborted',
generationCreatedAt: 41,
});
expect(observed.runEnd).toBeNull();
});
it('fences abort to the attached generation epoch', async () => {
@ -373,12 +391,13 @@ describe('useChatHelpers stopGenerating (abort steer targeting)', () => {
terminalStatus: 'complete',
generationProtocolVersion: 2,
});
const { result } = setupStop('convo-held');
const { result, stoppedSubmission } = setupStop('convo-held');
await act(async () => {
await result.current.stopGenerating();
});
expect(currentSubmission).toBe(stoppedSubmission);
expect(mockConvertSteersToQueued).not.toHaveBeenCalled();
expect(observed.runEnd).toBeNull();
expect(observed.drainAfterAbort).toBe(false);
@ -464,6 +483,6 @@ describe('useChatHelpers stopGenerating (abort steer targeting)', () => {
claimConversationId: 'convo-held',
generationProtocolVersion: 2,
});
expect(observed.runEnd).toMatchObject({ conversationId: 'convo-held', outcome: 'aborted' });
expect(observed.runEnd).toBeNull();
});
});

View file

@ -52,15 +52,7 @@ export default function useChatHelpers(index = 0, paramId?: string): ChatContrac
[index, queueStore],
);
/**
* Interrupt & send fallback: clearing submissions below can tear down the
* SSE before its aborted-final event is processed, and only that event
* writes the run-end signal the queue drain consumes. When the one-shot
* interrupt flag is armed and no run-end has landed yet, write it here from
* the abort response so the queued follow-up still auto-sends. If the SSE
* final DOES arrive later, its signal finds the flag already consumed and
* an `aborted` outcome drains nothing, so there is no double fire.
*/
/** A missing job cannot deliver FINAL, so release its armed interrupt queue. */
const signalInterruptDrain = useCallback(
(convoId: string, generationCreatedAt: number, armedConversationId = convoId) => {
const armed = queueStore.get(drainAfterAbortByIndex(index));
@ -266,7 +258,6 @@ export default function useChatHelpers(index = 0, paramId?: string): ChatContrac
refetchType: 'all',
});
queryClient.invalidateQueries({ queryKey: ['streamStatus', conversationId] });
clearSubmissionsUnlessReplaced(submissionAtAbort);
return;
}
if (canUseV2AbortResponse && response?.persistenceFailed === true) {
@ -292,11 +283,12 @@ export default function useChatHelpers(index = 0, paramId?: string): ChatContrac
conversationId,
resolvedId: response?.aborted,
});
// Steers the run never injected ride the abort response. Consume them
// here as well as on the SSE final event: clearing submissions below
// can close the stream before that event lands, and conversion
// dedupes by steer id so double delivery is a no-op. `claimParked`
// reconciles the replayable parked copy if the final raced this response.
/** Keep the submission attached until FINAL/status reconciles history.
* Clearing on the ACK can close SSE during terminal authorization and
* strand the preliminary response as an unsaved follow-up parent.
* Only terminal reconciliation may release the interrupt queue. */
// Restore uninjected steers from the ACK as well as FINAL. Conversion
// dedupes by steer id; `claimParked` reconciles the replayable copy.
if (Array.isArray(response?.pendingSteers)) {
convertSteersToQueued(chipConvoId, response.pendingSteers, {
claimParked: true,
@ -304,10 +296,6 @@ export default function useChatHelpers(index = 0, paramId?: string): ChatContrac
generationProtocolVersion: canUseV2AbortResponse ? 2 : 1,
});
}
signalInterruptDrain(chipConvoId, activeGenerationCreatedAt, conversationId);
// The SSE will receive a `done` event with `aborted: true` and clean up
// We still clear submissions as a fallback
clearSubmissionsUnlessReplaced(submissionAtAbort);
} catch (error) {
console.error('[useChatHelpers] Abort failed:', error);
const errorData = (

View file

@ -45,6 +45,7 @@ const COUNTED_REPLY_MARKER = 'E2E_COUNTED_REPLY:';
const ORDERED_REPLY_MARKER = 'E2E_ORDERED_REPLY:';
const SLOW_REPLY_MARKER = 'E2E_SLOW_REPLY:';
const EMPTY_SLOW_REPLY_MARKER = 'E2E_EMPTY_SLOW_REPLY:';
const PRE_TOKEN_REPLY_MARKER = 'E2E_PRE_TOKEN_REPLY:';
/** A run that completes having produced no content at all: the shape a
* summarizer takes when it returns nothing for a manual compaction. */
const EMPTY_REPLY_MARKER = 'E2E_EMPTY_REPLY:';
@ -754,6 +755,11 @@ function replyResponses(text) {
return slowReplyResponses(slowName);
}
const preTokenName = getMarkerValue(text, PRE_TOKEN_REPLY_MARKER);
if (preTokenName) {
return { responses: [`E2E pre-token reply ${preTokenName}`], sleep: 10_000 };
}
/** Keep a generation live after `created` without producing any content
* that the abort persistence filter accepts. The browser regression waits
* for the user row, then interrupts this whitespace-only stream. */

View file

@ -793,42 +793,24 @@ test.describe('mid-run steering and queuing', () => {
await expect(queuedRows(page)).toHaveCount(0);
});
test('Stop preserves an empty response parent for the next turn', async ({ page }) => {
test.setTimeout(120000);
const label = uniqueLabel('stop-empty');
const emptyRunPrompt = `E2E_EMPTY_SLOW_REPLY:${label}`;
const interruptText = `Interrupt empty follow-up ${label}`;
test('Stop before the first token saves the follow-up from the new composer', async ({
page,
}) => {
const label = uniqueLabel('stop-before-token');
await page.goto(NEW_CHAT_PATH, { timeout: 10000 });
await selectMockEndpoint(page, MOCK_ENDPOINTS[0]);
await establishConversation(page, `interrupt-empty-setup-${label}`);
const conversationId = new URL(page.url()).pathname.split('/').pop();
expect(conversationId).toBeTruthy();
const accessToken = await getAccessToken(page);
const messagesPath = `/api/messages/${encodeURIComponent(conversationId as string)}`;
const run = await sendMessage(page, emptyRunPrompt);
expect(run.ok()).toBeTruthy();
/** BaseClient starts its user-row write only after `onStart` emitted
* `created`. Waiting for that row proves the server is in the exact
* created-but-still-whitespace state, without relying on a sleep. */
await expect
.poll(
async () => {
const persisted = await requestJson<PersistedMessage[]>(page, {
path: messagesPath,
token: accessToken,
});
return persisted.some(
(message) => message.isCreatedByUser === true && message.text === emptyRunPrompt,
);
},
{ timeout: 30000 },
)
.toBe(true);
let releaseStatus = () => {};
const statusGate = new Promise<void>((resolve) => {
releaseStatus = resolve;
});
await page.route('**/api/agents/chat/status/**', async (route) => {
const response = await route.fetch();
await statusGate;
await route.fulfill({ response });
});
await sendMessage(page, `E2E_PRE_TOKEN_REPLY:${label}`);
const [abortResponse] = await Promise.all([
page.waitForResponse(
(response) =>
@ -837,30 +819,126 @@ test.describe('mid-run steering and queuing', () => {
),
page.getByTestId('stop-generation-button').click(),
]);
expect(abortResponse.ok()).toBeTruthy();
await expect(page.getByTestId('stop-generation-button')).toHaveCount(0);
await sendMessage(page, interruptText);
await expect(messageTurns(page)).toHaveCount(6);
await expect(messageTurns(page).nth(5)).toContainText(MOCK_REPLY_TEXT);
try {
expect(abortResponse.ok()).toBeTruthy();
await expect(page.getByText('Generating a reply…', { exact: true })).toBeVisible();
} finally {
releaseStatus();
}
await expect(page.getByText('Generating a reply…', { exact: true })).toHaveCount(0);
const stoppedTurns = await messageTurns(page).count();
expect([0, 2]).toContain(stoppedTurns);
await expect(page).toHaveURL(stoppedTurns === 0 ? /\/c\/new$/ : /\/c\/[0-9a-fA-F-]{36}$/);
const followUp = replyPrompt(`after-${label}`);
const followUpStart = await sendMessage(page, followUp);
const { streamId: conversationId } = (await followUpStart.json()) as { streamId: string };
await expect(messagesView(page).getByText(replyText(`after-${label}`))).toBeVisible();
const persisted = await requestJson<PersistedMessage[]>(page, {
path: messagesPath,
token: accessToken,
path: `/api/messages/${encodeURIComponent(conversationId)}`,
token: await getAccessToken(page),
});
const interruptedUser = persisted.find(
(message) => message.isCreatedByUser === true && message.text === emptyRunPrompt,
expect(persisted.some((message) => message.isCreatedByUser && message.text === followUp)).toBe(
true,
);
expect(interruptedUser).toBeTruthy();
expect(
persisted.find(
(message) =>
message.isCreatedByUser === false &&
message.parentMessageId === interruptedUser?.messageId,
),
).toMatchObject({ content: [], unfinished: true });
await expect(queuedRows(page)).toHaveCount(0);
await page.reload();
await expect(messageTurns(page)).toHaveCount(stoppedTurns + 2);
await expect(messagesView(page).getByText(replyText(`after-${label}`))).toBeVisible();
});
for (const startingPoint of ['new', 'existing'] as const) {
test(`Stop preserves an empty response parent in a ${startingPoint} chat`, async ({ page }) => {
test.setTimeout(120000);
const label = uniqueLabel('stop-empty');
const emptyRunPrompt = `E2E_EMPTY_SLOW_REPLY:${label}`;
const interruptText = `Interrupt empty follow-up ${label}`;
await page.goto(NEW_CHAT_PATH, { timeout: 10000 });
await selectMockEndpoint(page, MOCK_ENDPOINTS[0]);
if (startingPoint === 'existing') {
await establishConversation(page, `interrupt-empty-setup-${label}`);
}
const run = await sendMessage(page, emptyRunPrompt);
expect(run.ok()).toBeTruthy();
const { streamId: conversationId } = (await run.json()) as { streamId: string };
const accessToken = await getAccessToken(page);
const messagesPath = `/api/messages/${encodeURIComponent(conversationId)}`;
/** BaseClient starts its user-row write only after `onStart` emitted
* `created`. Waiting for that row proves the server is in the exact
* created-but-still-whitespace state, without relying on a sleep. */
await expect
.poll(
async () => {
const persisted = await requestJson<PersistedMessage[]>(page, {
path: messagesPath,
token: accessToken,
});
return persisted.some(
(message) => message.isCreatedByUser === true && message.text === emptyRunPrompt,
);
},
{ timeout: 30000 },
)
.toBe(true);
let releaseStatus = () => {};
const statusGate = new Promise<void>((resolve) => {
releaseStatus = resolve;
});
await page.route('**/api/agents/chat/status/**', async (route) => {
const response = await route.fetch();
await statusGate;
await route.fulfill({ response });
});
const [abortResponse] = await Promise.all([
page.waitForResponse(
(response) =>
response.request().method() === 'POST' &&
new URL(response.url()).pathname === '/api/agents/chat/abort',
),
page.getByTestId('stop-generation-button').click(),
]);
try {
expect(abortResponse.ok()).toBeTruthy();
await expect(page.getByText('Generating a reply…', { exact: true })).toBeVisible();
} finally {
releaseStatus();
}
await expect(page.getByText('Generating a reply…', { exact: true })).toHaveCount(0);
await sendMessage(page, interruptText);
const expectedTurns = startingPoint === 'existing' ? 6 : 4;
await expect(messageTurns(page)).toHaveCount(expectedTurns);
await expect(messageTurns(page).last()).toContainText(MOCK_REPLY_TEXT);
const persisted = await requestJson<PersistedMessage[]>(page, {
path: messagesPath,
token: accessToken,
});
const interruptedUser = persisted.find(
(message) => message.isCreatedByUser === true && message.text === emptyRunPrompt,
);
expect(interruptedUser).toBeTruthy();
expect(
persisted.find(
(message) =>
message.isCreatedByUser === false &&
message.parentMessageId === interruptedUser?.messageId,
),
).toMatchObject({ content: [], unfinished: true });
const followUp = persisted.find(
(message) => message.isCreatedByUser === true && message.text === interruptText,
);
expect(followUp).toBeTruthy();
await page.reload();
await expect(messageTurns(page)).toHaveCount(expectedTurns);
await expect(messageTurns(page).last()).toContainText(MOCK_REPLY_TEXT);
await expect(queuedRows(page)).toHaveCount(0);
});
}
/** The other interrupt chord uses the same no-tool-boundary continuation. */
test('interrupt & steer (Cmd/Ctrl+Shift+Enter) seals mid-stream and injects with no tool boundary', async ({
page,