From e3e7dc6d7cac434deadef77b143aebb4005e69e6 Mon Sep 17 00:00:00 2001 From: "lia-by-librechat[bot]" <328778573+lia-by-librechat[bot]@users.noreply.github.com> Date: Sat, 31 Oct 2026 16:15:46 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=AA=81=20fix:=20Keep=20Manual=20Compactio?= =?UTF-8?q?n=20Anchored=20and=20Visible=20Across=20Reloads=20(#17689)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: preserve manual compaction identity and pending state * fix: align resume restoration with shared lifecycle types * test: narrow the optional compaction response assertion --------- Co-authored-by: Lia --- .../useChatFunctions.regenerate.spec.tsx | 9 ++- e2e/specs/mock/thread-fold.spec.ts | 23 ++++++ .../chat/src/core/__tests__/dispatch.spec.ts | 41 ++++++++++- .../chat/src/core/__tests__/resume.spec.ts | 72 ++++++++++++++++++- .../chat/src/core/__tests__/turns.spec.ts | 28 ++++++++ packages/chat/src/core/resumable/resume.ts | 32 ++++++++- packages/chat/src/core/resumable/sync.ts | 7 +- packages/chat/src/core/turns/build.ts | 2 + 8 files changed, 204 insertions(+), 10 deletions(-) diff --git a/client/src/hooks/Chat/__tests__/useChatFunctions.regenerate.spec.tsx b/client/src/hooks/Chat/__tests__/useChatFunctions.regenerate.spec.tsx index f0b301959f..64744e9f79 100644 --- a/client/src/hooks/Chat/__tests__/useChatFunctions.regenerate.spec.tsx +++ b/client/src/hooks/Chat/__tests__/useChatFunctions.regenerate.spec.tsx @@ -1347,7 +1347,8 @@ describe('useChatFunctions ask compaction', () => { expect(submission.compact).toBe(true); expect(submission.isRegenerate).toBe(true); expect(submission.initialResponse?.parentMessageId).toBe('a1'); - expect(submission.initialResponse?.messageId).toBe('a1_'); + expect(submission.initialResponse?.messageId).toMatch(/_$/); + expect(submission.initialResponse?.messageId).not.toBe('a1_'); /** The leaf stands in for the user message so an error lands under it. */ expect(submission.userMessage.messageId).toBe('a1'); expect(submission.userMessage.overrideParentMessageId).toBeNull(); @@ -1359,7 +1360,11 @@ describe('useChatFunctions ask compaction', () => { ]); const rendered = setMessages.mock.calls[0][0] as TMessage[]; - expect(rendered.map((message) => message.messageId)).toEqual(['u1', 'a1', 'a1_']); + expect(rendered.map((message) => message.messageId)).toEqual([ + 'u1', + 'a1', + submission.initialResponse?.messageId, + ]); }); it('does not re-attach the leaf files a user-message leaf carries', () => { diff --git a/e2e/specs/mock/thread-fold.spec.ts b/e2e/specs/mock/thread-fold.spec.ts index 7ccd5d8de4..c40422238c 100644 --- a/e2e/specs/mock/thread-fold.spec.ts +++ b/e2e/specs/mock/thread-fold.spec.ts @@ -10,6 +10,7 @@ import { sendMessage, } from './helpers'; import { sendCompactionHistory } from './compaction.helpers'; +import { withMongo } from './db'; /** * Regression suite for the "folded thread" incident (PR: order-robust message @@ -55,6 +56,22 @@ async function sendAndExpectReply(page: Page, prompt: string, reply: string) { await expect(messagesView(page).getByText(reply)).toBeVisible({ timeout: 30000 }); } +/** Restored and continued replies may retain their streaming underscore suffix. */ +async function restoreUnderscoredLeaf(page: Page) { + const conversationId = new URL(page.url()).pathname.split('/').pop(); + await withMongo(async (db) => { + const leaf = await db + .collection('messages') + .findOne({ conversationId, isCreatedByUser: false }, { sort: { createdAt: -1 } }); + expect(leaf).not.toBeNull(); + await db + .collection('messages') + .updateOne({ _id: leaf!._id }, { $set: { messageId: `${leaf!.messageId}_` } }); + }); + await page.reload({ timeout: 15000 }); + await expect(page.getByTestId('token-usage')).toBeVisible({ timeout: 30000 }); +} + async function clickSibling(page: Page, messageTextValue: string, direction: 'Previous' | 'Next') { const render = messagesView(page) .locator('.message-render') @@ -211,6 +228,8 @@ test.describe('thread fold regressions', () => { await sendAndExpectReply(page, latestPrompt, latestReply); await expect(page).toHaveURL(/\/c\/[0-9a-fA-F-]{36}$/, { timeout: 15000 }); + await restoreUnderscoredLeaf(page); + const behavior = await request.post(`${LABEL_SERVER}/__e2e/behavior`, { data: { delayMs: 10000 }, }); @@ -275,6 +294,8 @@ test.describe('thread fold regressions', () => { await sendAndExpectReply(page, setupPrompt, setupReply); await expect(page).toHaveURL(/\/c\/[0-9a-fA-F-]{36}$/, { timeout: 15000 }); + await restoreUnderscoredLeaf(page); + /** Hold the summarizer so the compaction is still running when the page * comes back and the pane resumes it. */ const behavior = await request.post(`${LABEL_SERVER}/__e2e/behavior`, { @@ -304,6 +325,8 @@ test.describe('thread fold regressions', () => { 0, ); + await expect(messagesView(page).getByText('Summarizing...')).toBeVisible(); + /** The summary then settles under that answer, thread intact. */ await expect(messagesView(page).getByText('You compacted the context')).toBeVisible({ timeout: 60000, diff --git a/packages/chat/src/core/__tests__/dispatch.spec.ts b/packages/chat/src/core/__tests__/dispatch.spec.ts index 50ba94af9f..48e9a7bbd0 100644 --- a/packages/chat/src/core/__tests__/dispatch.spec.ts +++ b/packages/chat/src/core/__tests__/dispatch.spec.ts @@ -1,4 +1,4 @@ -import { StepEvents } from 'librechat-data-provider'; +import { StepEvents, StepTypes, ContentTypes } from 'librechat-data-provider'; import type { ChatEvent, TMessage, @@ -145,6 +145,45 @@ describe('handleFrame', () => { }); describe('handleSync', () => { + it('keeps the summary placeholder pending when sync replaces the replayed run step', async () => { + const { host, emit } = attach({ isResume: true }); + host.messages.set([userMessage, submission.initialResponse as TMessage]); + await emit({ + type: 'sync', + data: { + sync: true, + resumeState: { + runSteps: [ + { + id: 'summary-step', + runId: 'r-1', + index: 0, + stepIndex: 0, + type: StepTypes.MESSAGE_CREATION, + stepDetails: { + type: StepTypes.MESSAGE_CREATION, + message_creation: { message_id: 'summary-step' }, + }, + summary: { type: ContentTypes.SUMMARY, model: 'summary-model' }, + status: 'in_progress', + }, + ], + aggregatedContent: [{ type: ContentTypes.SUMMARY, model: 'summary-model' }], + userMessage: { messageId: 'u-1', parentMessageId: 'root', text: 'Hello' }, + responseMessageId: 'r-1', + conversationId: 'c-1', + }, + }, + } as ChatEvent); + const response = host.messages.get()?.find((message) => message.messageId === 'r-1'); + expect(response?.content?.[0]).toMatchObject({ + type: ContentTypes.SUMMARY, + model: 'summary-model', + summarizing: true, + }); + expect(host.events.syncStepMessage).toHaveBeenCalledWith(response); + }); + it('rebuilds the response from the resume snapshot', async () => { const { host, emit, run } = attach({ isResume: true }); host.messages.set([userMessage, submission.initialResponse as TMessage]); diff --git a/packages/chat/src/core/__tests__/resume.spec.ts b/packages/chat/src/core/__tests__/resume.spec.ts index bcc7fd1d60..3b40fb89f7 100644 --- a/packages/chat/src/core/__tests__/resume.spec.ts +++ b/packages/chat/src/core/__tests__/resume.spec.ts @@ -1,4 +1,10 @@ -import { Constants, StepEvents, tMessageSchema } from 'librechat-data-provider'; +import { + Constants, + StepTypes, + StepEvents, + ContentTypes, + tMessageSchema, +} from 'librechat-data-provider'; import type { Agents, TMessage, TSubmission } from 'librechat-data-provider'; import { getStepEventId, @@ -80,6 +86,70 @@ describe('buildMinimalResumeSubmission', () => { }); describe('buildSubmissionFromResumeState', () => { + it.each([undefined, 'in_progress', 'completed', 'failed', 'cancelled'] as const)( + 'restores a metadata-only summary from a %s run step', + (status) => { + const snapshot = resumeState({ + aggregatedContent: [{ type: ContentTypes.SUMMARY, model: 'summary-model' }], + runSteps: [ + { + id: 'summary-step', + runId: 'r-1', + index: 0, + stepIndex: 0, + type: StepTypes.MESSAGE_CREATION, + stepDetails: { + type: StepTypes.MESSAGE_CREATION, + message_creation: { message_id: 'summary-step' }, + }, + summary: { type: ContentTypes.SUMMARY, model: 'summary-model' }, + status, + }, + ], + }); + const original = structuredClone(snapshot); + const submission = buildSubmissionFromResumeState(snapshot, 'c-1', [user], 'c-1'); + const pending = status == null || status === 'in_progress'; + expect(submission.initialResponse?.content?.[0]).toEqual({ + ...snapshot.aggregatedContent?.[0], + ...(pending && { summarizing: true }), + }); + const synced = buildResumeEventSubmission(submission, user, snapshot); + expect(synced.initialResponse.content).toEqual(submission.initialResponse?.content); + expect(snapshot).toEqual(original); + }, + ); + + it('leaves settled summaries and other response steps untouched during sync', () => { + const content = [ + { + type: ContentTypes.SUMMARY, + boundary: { messageId: 'u-1', contentIndex: 0 }, + content: [{ type: ContentTypes.TEXT, text: 'Saved summary' }], + }, + { type: ContentTypes.SUMMARY, failed: true }, + { type: ContentTypes.SUMMARY, summarizing: false }, + { type: ContentTypes.SUMMARY }, + ]; + const snapshot = resumeState({ + aggregatedContent: content, + runSteps: content.map((_, index) => ({ + id: `summary-${index}`, + runId: index === 3 ? 'other-response' : 'r-1', + index, + stepIndex: index, + type: StepTypes.MESSAGE_CREATION, + stepDetails: { + type: StepTypes.MESSAGE_CREATION, + message_creation: { message_id: `summary-${index}` }, + }, + summary: { type: ContentTypes.SUMMARY }, + })), + }); + const submission = buildSubmissionFromResumeState(snapshot, 'c-1', [user], 'c-1'); + expect(submission.initialResponse?.content).toBe(content); + }); + it('strips the persisted pair a fresh resume re-supplies', () => { const submission = buildSubmissionFromResumeState( resumeState(), diff --git a/packages/chat/src/core/__tests__/turns.spec.ts b/packages/chat/src/core/__tests__/turns.spec.ts index d0cdbe34dc..5ce664780e 100644 --- a/packages/chat/src/core/__tests__/turns.spec.ts +++ b/packages/chat/src/core/__tests__/turns.spec.ts @@ -67,6 +67,34 @@ const input = (overrides: Partial = {}): BuildTurnInput => ({ }); describe('buildTurn', () => { + it.each(['response-1', 'response-1_', 'response-1__', 'a'.repeat(256)])( + 'keeps the compaction placeholder distinct from the saved leaf %s', + (messageId) => { + const anchor = { ...response, messageId, createdAt: '2026-10-31T19:00:00Z' }; + const turn = buildTurn( + input({ + text: '', + params: { text: '', messageId, parentMessageId: messageId }, + options: { compact: true }, + latestMessage: anchor, + cachedMessages: [user, anchor], + }), + ); + + expect(turn.initialResponse.messageId).not.toBe(messageId); + expect(turn.initialResponse.messageId.replace(/_+$/, '')).not.toBe( + messageId.replace(/_+$/, ''), + ); + expect(turn.initialResponse.messageId).toMatch(/_$/); + expect(turn.initialResponse.messageId.length).toBeLessThanOrEqual( + Constants.MAX_RESPONSE_MESSAGE_ID_LENGTH, + ); + expect(turn.initialResponse.parentMessageId).toBe(messageId); + expect(turn.submissionMessages).toEqual([user, anchor]); + expect(turn.submission.userMessage.messageId).toBe(messageId); + }, + ); + it('appends a user message and a response placeholder to the branch tail', () => { const turn = buildTurn(input()); diff --git a/packages/chat/src/core/resumable/resume.ts b/packages/chat/src/core/resumable/resume.ts index af82855b7a..ad6cdc700f 100644 --- a/packages/chat/src/core/resumable/resume.ts +++ b/packages/chat/src/core/resumable/resume.ts @@ -1,4 +1,4 @@ -import { Constants, tMessageSchema } from 'librechat-data-provider'; +import { Constants, ContentTypes, tMessageSchema } from 'librechat-data-provider'; import type { Agents, TMessage, @@ -46,6 +46,32 @@ export const isCompactionAnchorProjection = ( userMessage.parentMessageId == null && (userMessage.text == null || userMessage.text === ''); +/** The SDK snapshot's summary placeholder lacks the live `summarizing` marker. */ +function restoreSummaryState(resumeState: Agents.ResumeState): TMessage['content'] { + const content = resumeState.aggregatedContent as TMessage['content']; + if (!content?.length) { + return content; + } + let restored: TMessage['content']; + for (const step of resumeState.runSteps ?? []) { + const part = content[step.index]; + if ( + step.summary == null || + (step.status != null && step.status !== 'in_progress') || + (step.runId != null && step.runId !== resumeState.responseMessageId) || + part?.type !== ContentTypes.SUMMARY || + part.summarizing != null || + part.boundary != null || + part.failed === true + ) { + continue; + } + restored ??= [...content]; + restored[step.index] = { ...part, summarizing: true }; + } + return restored ?? content; +} + export const buildResumeEventSubmission = ( currentSubmission: TSubmission, currentUserMessage: TMessage, @@ -95,7 +121,7 @@ export const buildResumeEventSubmission = ( parentMessageId: userMessage.messageId, conversationId, content: - resumeState.aggregatedContent ?? + restoreSummaryState(resumeState) ?? (currentSubmission.initialResponse as TMessage | undefined)?.content, sender: resumeState.sender ?? currentSubmission.initialResponse?.sender, iconURL: preferDefinedString(currentSubmission.initialResponse?.iconURL, resumeState.iconURL), @@ -201,7 +227,7 @@ export function buildSubmissionFromResumeState( conversationId, text: '', // aggregatedContent is authoritative - it reflects actual job state - content: (resumeState.aggregatedContent as TMessage['content']) ?? [], + content: restoreSummaryState(resumeState) ?? [], isCreatedByUser: false, role: 'assistant', sender: responseMetadataMessage?.sender ?? resumeState.sender, diff --git a/packages/chat/src/core/resumable/sync.ts b/packages/chat/src/core/resumable/sync.ts index d242a0eb0d..578ba51586 100644 --- a/packages/chat/src/core/resumable/sync.ts +++ b/packages/chat/src/core/resumable/sync.ts @@ -99,7 +99,8 @@ export function handleSync(ctx: StreamContext, data: ChatSyncFrame): void { if (data.resumeState?.aggregatedContent && sub.userMessage?.messageId) { const messages = host.messages.get() ?? []; const userMsgId = sub.userMessage.messageId; - const hasResumedContent = data.resumeState.aggregatedContent.length > 0; + const resumedContent = resumeSubmission.initialResponse.content; + const hasResumedContent = (resumedContent?.length ?? 0) > 0; const responseId = resumeSubmission.initialResponse.messageId; const messageIndexes = getResumeMessageIndexes( messages, @@ -148,7 +149,7 @@ export function handleSync(ctx: StreamContext, data: ChatSyncFrame): void { ...messages[responseIdx], messageId: responseId, parentMessageId: userMsgId, - content: preserveLoadedContent ? oldContent : data.resumeState.aggregatedContent, + content: preserveLoadedContent ? oldContent : resumedContent, sender: messages[responseIdx]?.sender ?? resumeSubmission.initialResponse.sender, iconURL: preferDefinedString( messages[responseIdx]?.iconURL, @@ -188,7 +189,7 @@ export function handleSync(ctx: StreamContext, data: ChatSyncFrame): void { ...resumeSubmission.initialResponse, messageId: responseId, parentMessageId: userMsgId, - content: data.resumeState.aggregatedContent, + content: resumedContent, isCreatedByUser: false, } as TMessage; host.messages.set( diff --git a/packages/chat/src/core/turns/build.ts b/packages/chat/src/core/turns/build.ts index 3ed0e7ab1d..b4645be4a1 100644 --- a/packages/chat/src/core/turns/build.ts +++ b/packages/chat/src/core/turns/build.ts @@ -316,7 +316,9 @@ export function buildTurn({ null; /** Set only for edited resubmissions; see `TSubmission.editPrefixLength`. */ let editPrefixLength: number | undefined; + /** Compaction appends a new turn; a regeneration alias can equal its saved anchor. */ const initialResponseId = + (compact ? `${v4()}_` : null) ?? responseMessageId ?? getPreliminaryRegenerateResponseMessageId(`${regenerateShaped ? messageId : intermediateId}`) ?? `${v4()}_`;