mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-16 13:00:55 +00:00
🪁 fix: Keep Manual Compaction Anchored and Visible Across Reloads (#17689)
* 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 <lia@librechat.ai>
This commit is contained in:
parent
f4154b649e
commit
e3e7dc6d7c
8 changed files with 204 additions and 10 deletions
|
|
@ -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', () => {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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]);
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -67,6 +67,34 @@ const input = (overrides: Partial<BuildTurnInput> = {}): 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());
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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()}_`;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue