Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

:bug: fix: fix compression UI (#13113)

Rylan Cai4d360714add7
diff --git a/packages/conversation-flow/src/__tests__/indexing.test.ts b/packages/conversation-flow/src/__tests__/indexing.test.ts
index 45fdbfd9afc..38ac83aa250 100644
--- a/packages/conversation-flow/src/__tests__/indexing.test.ts
+++ b/packages/conversation-flow/src/__tests__/indexing.test.ts
@@ -502,6 +502,91 @@ describe('buildHelperMaps', () => {
expect(result.childrenMap.get('comp-group-1')).toEqual(['msg-new-1']);
expect(result.childrenMap.get('comp-group-2')).toEqual(['msg-new-2']);
});
+
+ it('should resolve lastMessageId from compressedMessages recursively', () => {
+ // Data provenance:
+ // - The nested `compressedMessages` shape comes from real frontend chat exports
+ // after context compression, where the visible compressedGroup keeps hidden
+ // user/assistant history inside display-only arrays.
+ // - This represents the current synchronous chat path rather than eval tasks.
+ const messages: Message[] = [
+ {
+ compressedMessages: [
+ {
+ id: 'msg-hidden-1',
+ role: 'user',
+ },
+ {
+ id: 'msg-hidden-2',
+ role: 'assistant',
+ },
+ ],
+ content: 'Compressed history',
+ createdAt: 1000,
+ id: 'comp-group-1',
+ pinnedMessages: [],
+ role: 'compressedGroup',
+ updatedAt: 1000,
+ } as any,
+ {
+ content: 'Follow-up after compression',
+ createdAt: 2000,
+ id: 'msg-new',
+ parentId: 'msg-hidden-2',
+ role: 'assistant',
+ updatedAt: 2000,
+ },
+ ];
+
+ const result = buildHelperMaps(messages);
+
+ expect(result.childrenMap.get('comp-group-1')).toEqual(['msg-new']);
+ expect(result.childrenMap.get('msg-hidden-2')).toBeUndefined();
+ });
+
+ it('should resolve lastMessageId from assistant children tools result', () => {
+ // Data provenance:
+ // - The nested assistant child with `tools[].result_msg_id` is abstracted from the
+ // real eval/compression export we inspected through `lh eval message list`.
+ // - In that async eval flow, the compressed history contains assistant/tool chains
+ // instead of only plain lastMessageId markers.
+ // - This represents the eval side of the system: long-running async task/search chains.
+ const messages: Message[] = [
+ {
+ children: [
+ {
+ content: 'Search for clues',
+ id: 'msg-assistant-1',
+ tools: [
+ {
+ id: 'tool-call-1',
+ result_msg_id: 'msg-tool-result-1',
+ },
+ ],
+ },
+ ],
+ content: '',
+ createdAt: 1000,
+ id: 'comp-group-1',
+ pinnedMessages: [],
+ role: 'compressedGroup',
+ updatedAt: 1000,
+ } as any,
+ {
+ content: 'Continue searching',
+ createdAt: 2000,
+ id: 'msg-new',
+ parentId: 'msg-tool-result-1',
+ role: 'assistant',
+ updatedAt: 2000,
+ },
+ ];
+
+ const result = buildHelperMaps(messages);
+
+ expect(result.childrenMap.get('comp-group-1')).toEqual(['msg-new']);
+ expect(result.childrenMap.get('msg-tool-result-1')).toBeUndefined();
+ });
});
describe('integration scenarios', () => {
diff --git a/packages/conversation-flow/src/__tests__/parse.test.ts b/packages/conversation-flow/src/__tests__/parse.test.ts
index 7b8a88055bc..8464487a48f 100644
--- a/packages/conversation-flow/src/__tests__/parse.test.ts
+++ b/packages/conversation-flow/src/__tests__/parse.test.ts
@@ -286,6 +286,138 @@ describe('parse', () => {
});
});
+ describe('Compression', () => {
+ it('should keep follow-up chain visible after compressedGroup from recursive tool result', () => {
+ // Data provenance:
+ // - The compressedGroup + nested assistant/tool structure is abstracted from the
+ // real `lh eval message list` output after we fixed the CLI/router to expose
+ // full compression data.
+ // - That output models the async eval path: long-running search/tool chains that
+ // later get compressed by the backend before follow-up steps continue.
+ // - We intentionally keep the sample minimal while preserving the real eval traits:
+ // compressed history, assistant/tool chaining, and tool result message redirection.
+ const messages = [
+ {
+ compressedMessages: [
+ {
+ content:
+ 'I was reviewing the list of winners of a prestigious international prize...',
+ id: 'msg-user-hidden',
+ role: 'user',
+ },
+ {
+ content: '',
+ id: 'msg-assistant-hidden',
+ role: 'assistantGroup',
+ tools: [
+ {
+ id: 'tool-call-1',
+ result_msg_id: 'msg-tool-hidden',
+ },
+ ],
+ },
+ ],
+ content: 'Compressed summary of earlier search steps',
+ createdAt: 1000,
+ id: 'comp-group-1',
+ pinnedMessages: [],
+ role: 'compressedGroup',
+ updatedAt: 1000,
+ },
+ {
+ content: 'John Clarke was born in the United Kingdom, not the USA.',
+ createdAt: 2000,
+ id: 'msg-follow-up-1',
+ parentId: 'msg-tool-hidden',
+ role: 'assistant',
+ updatedAt: 2000,
+ },
+ {
+ content: '',
+ createdAt: 3000,
+ id: 'msg-follow-up-2',
+ parentId: 'msg-follow-up-1',
+ role: 'assistant',
+ tools: [
+ {
+ apiName: 'search',
+ arguments: '{}',
+ id: 'tool-call-2',
+ identifier: 'lobe-web-browsing',
+ type: 'builtin',
+ },
+ ],
+ updatedAt: 3000,
+ },
+ {
+ content: '<searchResults><item title="MIT Nobel Prize winners" /></searchResults>',
+ createdAt: 4000,
+ id: 'msg-tool-2',
+ parentId: 'msg-follow-up-2',
+ role: 'tool',
+ tool_call_id: 'tool-call-2',
+ updatedAt: 4000,
+ },
+ ] as any[];
+
+ const result = parse(messages);
+
+ expect(result.flatList).toHaveLength(3);
+ expect(result.flatList[0].id).toBe('comp-group-1');
+ expect(result.flatList[0].role).toBe('compressedGroup');
+ expect(result.flatList[1].id).toBe('msg-follow-up-1');
+ expect(result.flatList[2].role).toBe('assistantGroup');
+ expect((result.flatList[2] as any).children).toHaveLength(1);
+ expect((result.flatList[2] as any).children[0].id).toBe('msg-follow-up-2');
+ expect((result.flatList[2] as any).children[0].tools[0].result_msg_id).toBe('msg-tool-2');
+
+ expect(result.contextTree.map((node) => node.id)).toEqual([
+ 'comp-group-1',
+ 'msg-follow-up-1',
+ 'msg-follow-up-2',
+ ]);
+ expect(result.messageMap['msg-follow-up-2']).toBeDefined();
+ });
+
+ it('should keep orphan follow-up chain as root when compressed parent is missing', () => {
+ // Data provenance:
+ // - This case is derived from the current frontend chat continuation behavior:
+ // after compression, a follow-up request may be queried without the original
+ // compressed parent message still being present in the current message slice.
+ // - It represents the synchronous chat path, where UI queries a partial window and
+ // still needs the remaining visible chain instead of dropping it as an orphan.
+ const messages = [
+ {
+ content: 'Continue the Nobel Prize search',
+ createdAt: 1000,
+ id: 'msg-follow-up-1',
+ parentId: 'msg-compressed-hidden',
+ role: 'user',
+ updatedAt: 1000,
+ },
+ {
+ content: 'I will check the laureates by institution.',
+ createdAt: 2000,
+ id: 'msg-follow-up-2',
+ parentId: 'msg-follow-up-1',
+ role: 'assistant',
+ updatedAt: 2000,
+ },
+ ] as any[];
+
+ const result = parse(messages);
+
+ expect(result.flatList).toHaveLength(2);
+ expect(result.flatList[0].id).toBe('msg-follow-up-1');
+ expect(result.flatList[1].id).toBe('msg-follow-up-2');
+ expect(result.contextTree.map((node) => node.id)).toEqual([
+ 'msg-follow-up-1',
+ 'msg-follow-up-2',
+ ]);
+ expect(result.messageMap['msg-follow-up-1'].parentId).toBe('msg-compressed-hidden');
+ });
+ });
+
describe('Performance', () => {
it('should parse 10000 items within 100ms', () => {
// Generate 10000 messages as flat siblings (no deep nesting to avoid stack overflow)
diff --git a/packages/conversation-flow/src/__tests__/structuring.test.ts b/packages/conversation-flow/src/__tests__/structuring.test.ts
index df0a76cf53b..49b76522415 100644
--- a/packages/conversation-flow/src/__tests__/structuring.test.ts
+++ b/packages/conversation-flow/src/__tests__/structuring.test.ts
@@ -384,8 +384,13 @@ describe('buildIdTree', () => {
const helperMaps = buildHelperMaps(messages);
const result = buildIdTree(helperMaps);
- // Orphan messages without valid parents are not in main flow
- expect(result).toEqual([]);
+ // Orphan messages are promoted to roots by the indexing fallback
+ expect(result).toEqual([
+ {
+ children: [],
+ id: 'msg-1',
+ },
+ ]);
});
it('should build correct tree when all messages are threaded', () => {
diff --git a/packages/conversation-flow/src/indexing.ts b/packages/conversation-flow/src/indexing.ts
index 823d9c04520..b9752184cf1 100644
--- a/packages/conversation-flow/src/indexing.ts
+++ b/packages/conversation-flow/src/indexing.ts
@@ -1,5 +1,36 @@
import type { HelperMaps, Message, MessageGroupMetadata } from './types';
+interface CompressedDisplayNode {
+ children?: CompressedDisplayNode[];
+ compressedMessages?: CompressedDisplayNode[];
+ id?: string;
+ lastMessageId?: string;
+ tools?: Array<{ result_msg_id?: string }>;
+}
+
+const getLastMessageIdFromCompressedDisplay = (
+ message: CompressedDisplayNode | undefined,
+): string | undefined => {
+ if (!message) return;
+
+ if (message.lastMessageId) return message.lastMessageId;
+
+ const compressedMessages = message.compressedMessages;
+ if (compressedMessages && compressedMessages.length > 0) {
+ return getLastMessageIdFromCompressedDisplay(compressedMessages.at(-1));
+ }
+
+ if (message.children && message.children.length > 0) {
+ return getLastMessageIdFromCompressedDisplay(message.children.at(-1));
+ }
+
+ if (message.tools && message.tools.length > 0) {
+ return message.tools.at(-1)?.result_msg_id ?? message.id;
+ }
+
+ return message.id;
+};
+
/**
* Phase 1: Indexing
* Builds helper maps for efficient querying during parsing
@@ -16,6 +47,7 @@ export function buildHelperMaps(
const childrenMap = new Map<string | null, string[]>();
const threadMap = new Map<string, Message[]>();
const messageGroupMap = new Map<string, MessageGroupMetadata>();
+ const messageIds = new Set(messages.map((message) => message.id));
// Build a map of lastMessageId -> compressedGroup.id for parent redirection
// This handles the case where messages are created after compression:
@@ -24,8 +56,12 @@ export function buildHelperMaps(
// - We need to redirect children of lastMessageId to be children of compressedGroup
const lastMessageIdToGroupId = new Map<string, string>();
for (const message of messages) {
- if (message.role === 'compressedGroup' && (message as any).lastMessageId) {
- lastMessageIdToGroupId.set((message as any).lastMessageId, message.id);
+ if (message.role === 'compressedGroup') {
+ const lastMessageId = getLastMessageIdFromCompressedDisplay(message);
+
+ if (lastMessageId) {
+ lastMessageIdToGroupId.set(lastMessageId, message.id);
+ }
}
}
@@ -41,6 +77,12 @@ export function buildHelperMaps(
// This ensures messages created after compression become children of the compressedGroup
if (parentId && lastMessageIdToGroupId.has(parentId)) {
parentId = lastMessageIdToGroupId.get(parentId)!;
+ } else if (parentId && !messageIds.has(parentId)) {
+ // Orphan fallback:
+ // If parent is not present in the current queried message set,
+ // treat this message as a root so post-compression follow-up chains
+ // remain visible instead of being dropped entirely.
+ parentId = null;
}
const siblings = childrenMap.get(parentId);