From 7608737d76cec911a14ea5523db0989284f63d4e Mon Sep 17 00:00:00 2001 From: "lia-by-librechat[bot]" <328778573+lia-by-librechat[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:54:40 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=8D=20fix:=20Preserve=20Every=20Conver?= =?UTF-8?q?sation=20Branch=20When=20Duplicating=20(#18257)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🎍 fix: Preserve Every Conversation Branch When Duplicating * style: Normalize Regression Fixture Formatting --------- Co-authored-by: Lia --- api/server/utils/import/fork.js | 7 +- api/server/utils/import/fork.spec.js | 118 ++++++++++++++++++ .../api/src/conversations/lineage.spec.ts | 101 +++++++++++++-- packages/api/src/conversations/lineage.ts | 37 +++--- 4 files changed, 227 insertions(+), 36 deletions(-) diff --git a/api/server/utils/import/fork.js b/api/server/utils/import/fork.js index f39ed02ccb..cc554a7e37 100644 --- a/api/server/utils/import/fork.js +++ b/api/server/utils/import/fork.js @@ -542,11 +542,6 @@ async function duplicateConversation({ '+privateTextTokens', ); - const messagesToClone = getMessagesUpToTargetLevel( - originalMessages, - originalMessages[originalMessages.length - 1].messageId, - ); - const importBatchBuilder = legacyPii == null ? builderFactory(userId, interfaceConfig, filters) @@ -554,7 +549,7 @@ async function duplicateConversation({ importBatchBuilder.sourceIsTemporary = isTemporaryRecord(originalConvo); importBatchBuilder.startConversation(originalConvo.endpoint ?? EModelEndpoint.openAI); - cloneMessagesWithTimestamps(messagesToClone, importBatchBuilder, { nativeCopy: true }); + cloneMessagesWithTimestamps(originalMessages, importBatchBuilder, { nativeCopy: true }); const duplicateTitle = title || originalConvo.title; const result = importBatchBuilder.finishConversation(duplicateTitle, new Date(), originalConvo); diff --git a/api/server/utils/import/fork.spec.js b/api/server/utils/import/fork.spec.js index 8bf578fcfe..8eed3b5148 100644 --- a/api/server/utils/import/fork.spec.js +++ b/api/server/utils/import/fork.spec.js @@ -478,6 +478,20 @@ describe('forkConversation', () => { }); describe('duplicateConversation', () => { + const branch = [ + ['q1', Constants.NO_PARENT], + ['a1', 'q1'], + ['q2', 'a1'], + ['a2', 'q2'], + ['q3', 'a2'], + ['a3', 'q3'], + ].map(([messageId, parentMessageId], index) => ({ + messageId, + parentMessageId, + text: messageId, + createdAt: new Date(Date.UTC(2024, 0, 1, 0, 0, index)), + })); + beforeEach(() => { jest.clearAllMocks(); mockIdCounter = 0; @@ -488,6 +502,110 @@ describe('duplicateConversation', () => { bulkIncrementTagCounts.mockResolvedValue(null); }); + test.each([ + [ + 'regenerated early reply', + [ + ...branch, + { ...branch[1], messageId: 'a1-new', text: 'a1-new', createdAt: new Date('2024-01-02') }, + ], + ], + [ + 'edited first message', + [ + ...branch, + { ...branch[0], messageId: 'q1-edit', text: 'q1-edit', createdAt: new Date('2024-01-02') }, + ], + ], + [ + 'unequal branch depths', + [ + ...branch, + { + ...branch[1], + messageId: 'alternate', + text: 'alternate', + createdAt: new Date('2024-01-02'), + }, + { + ...branch[2], + messageId: 'alternate-child', + parentMessageId: 'alternate', + text: 'alternate-child', + createdAt: new Date('2024-01-03'), + }, + ], + ], + [ + 'tied timestamps with children first', + [...branch].reverse().map((entry) => ({ ...entry, createdAt: new Date('2024-01-01') })), + ], + [ + 'children timestamped before parents', + [...branch].reverse().map((entry, index) => ({ + ...entry, + createdAt: new Date(Date.UTC(2024, 0, 1, 0, 0, index)), + })), + ], + ['ordinary linear history', branch], + ])('preserves the complete tree for %s', async (_name, sources) => { + const original = structuredClone(sources); + const conversation = { ...mockConversation, conversationId: 'source' }; + getConvo + .mockResolvedValueOnce(conversation) + .mockImplementation(async () => bulkSaveConvos.mock.calls[0][0][0]); + getMessages + .mockResolvedValueOnce(sources) + .mockImplementation(async () => bulkSaveMessages.mock.calls[0][0]); + + const result = await duplicateConversation({ userId: 'user1', conversationId: 'source' }); + + expect(result.messages).toHaveLength(sources.length); + expect(bulkSaveMessages).toHaveBeenCalledTimes(1); + expect(result.conversation.conversationId).not.toBe(conversation.conversationId); + const byText = new Map(result.messages.map((entry) => [entry.text, entry])); + expect(new Set(result.messages.map((entry) => entry.messageId)).size).toBe(sources.length); + for (const source of sources) { + const clone = byText.get(source.text); + expect(clone).toBeDefined(); + expect(clone.messageId).not.toBe(source.messageId); + expect(clone.conversationId).toBe(result.conversation.conversationId); + expect(clone.user).toBe('user1'); + const parent = sources.find((entry) => entry.messageId === source.parentMessageId); + const clonedParent = parent && byText.get(parent.text); + expect(clone.parentMessageId).toBe(clonedParent?.messageId ?? Constants.NO_PARENT); + if (clonedParent) { + expect(new Date(clone.createdAt).getTime()).toBeGreaterThan( + new Date(clonedParent.createdAt).getTime(), + ); + } + } + expect(sources).toEqual(original); + expect(conversation).toEqual({ ...mockConversation, conversationId: 'source' }); + expect(getMessages.mock.calls[0][0]).toEqual({ user: 'user1', conversationId: 'source' }); + }); + + test('duplicates an empty conversation without reading a newest message', async () => { + getMessages.mockResolvedValue([]); + + const result = await duplicateConversation({ userId: 'user1', conversationId: 'source' }); + + expect(result.messages).toEqual([]); + expect(bulkSaveConvos).toHaveBeenCalledTimes(1); + expect(bulkSaveMessages).toHaveBeenCalledWith([], true); + }); + + test('does not attach an orphan to the previously cloned message', async () => { + getMessages.mockResolvedValueOnce([branch[0], { ...branch[1], parentMessageId: 'missing' }]); + + await duplicateConversation({ userId: 'user1', conversationId: 'source' }); + + expect(bulkSaveMessages.mock.calls[0][0].map((entry) => entry.parentMessageId)).toEqual([ + Constants.NO_PARENT, + Constants.NO_PARENT, + ]); + }); + test('applies ephemeral retention to duplicated conversation and messages', async () => { await duplicateConversation({ userId: 'user1', diff --git a/packages/api/src/conversations/lineage.spec.ts b/packages/api/src/conversations/lineage.spec.ts index 612570acbc..683051be4d 100644 --- a/packages/api/src/conversations/lineage.spec.ts +++ b/packages/api/src/conversations/lineage.spec.ts @@ -208,17 +208,98 @@ describe('cloneLineage', () => { ]); }); - it('leaves the parent link undefined when the parent is cloned later or absent', () => { - const { entries } = cloneLineage( - [ - { messageId: 'early', parentMessageId: 'late', createdAt: at(0) }, - { messageId: 'late', parentMessageId: 'missing', createdAt: at(10) }, - ], - sequentialIds(), - ); + it('preserves every parent link for every listing order of a branched conversation', () => { + const sources = [ + message('root', ROOT, 100), + message('parent', 'root', 100), + message('child', 'parent', 0), + message('sibling', 'root', 200), + message('leaf', 'child', 0), + ].map((source) => Object.freeze(source)); + const original = sources.map((source) => ({ ...source })); + let checked = 0; + const checkOrders = (prefix: LineageMessage[], remaining: LineageMessage[]): void => { + if (remaining.length > 0) { + for (let i = 0; i < remaining.length; i++) { + checkOrders( + [...prefix, remaining[i]], + remaining.filter((_, index) => index !== i), + ); + } + return; + } + const { entries, idMapping } = cloneLineage(Object.freeze(prefix), sequentialIds()); + const byId = new Map(entries.map((entry) => [entry.messageId, entry])); + expect(entries).toHaveLength(sources.length); + expect(new Set(idMapping.values()).size).toBe(sources.length); + for (const entry of entries) { + const parent = entry.source.parentMessageId; + expect(entry.messageId).not.toBe(entry.source.messageId); + expect(entry.parentMessageId).toBe(parent === ROOT ? ROOT : idMapping.get(parent ?? '')); + const parentEntry = byId.get(entry.parentMessageId); + if (parentEntry) { + expect(entry.createdAt.getTime()).toBeGreaterThan(parentEntry.createdAt.getTime()); + } + } + checked++; + }; - expect(entries.map((entry) => entry.parentMessageId)).toEqual([undefined, undefined]); - expect(entries.map((entry) => entry.createdAt.getTime())).toEqual([BASE, BASE + 10]); + checkOrders([], sources); + + expect(checked).toBe(120); + expect(sources).toEqual(original); + }); + + it.each([undefined, null, '', 'missing'])( + 'roots a message whose parent is %p instead of leaving a sequential fallback', + (parentMessageId) => { + const { entries } = cloneLineage( + [message('root', ROOT), { messageId: 'orphan', parentMessageId, createdAt: at(10) }], + sequentialIds(), + ); + + expect(entries.map((entry) => entry.parentMessageId)).toEqual([ROOT, ROOT]); + }, + ); + + it.each([ + { sources: [message('a', 'a')] }, + { sources: [message('a', 'b'), message('b', 'a'), message('tail', 'a')] }, + ])('breaks cyclic clone links without changing their sources: %p', ({ sources }) => { + const original = sources.map((source) => ({ ...source })); + const warn = jest.spyOn(logger, 'warn').mockImplementation(() => logger); + const { entries } = cloneLineage(sources, sequentialIds()); + + expect(entries).toHaveLength(sources.length); + expect(entries.filter((entry) => entry.parentMessageId === ROOT)).toHaveLength(1); + const byId = new Map(entries.map((entry) => [entry.messageId, entry])); + for (const entry of entries) { + const parent = byId.get(entry.parentMessageId); + if (parent) { + expect(entry.createdAt.getTime()).toBeGreaterThan(parent.createdAt.getTime()); + } + } + expect(sources).toEqual(original); + expect(warn).toHaveBeenCalledTimes(1); + warn.mockRestore(); + }); + + it('clones a deep reverse-listed chain without recursion or quadratic walks', () => { + const length = 20_000; + const sources = reverseChain(length); + const startedAt = performance.now(); + const { entries, idMapping } = cloneLineage(sources, sequentialIds()); + + expect(performance.now() - startedAt).toBeLessThan(1000); + expect(entries).toHaveLength(length); + const leaf = entries.find((entry) => entry.source.messageId === `m${length - 1}`); + expect(leaf?.createdAt.getTime()).toBe(BASE + length - 1); + expect(leaf?.parentMessageId).toBe(idMapping.get(`m${length - 2}`)); + expect(sources.every((source) => source.createdAt.getTime() === BASE)).toBe(true); + }); + + it('clones an empty conversation', () => { + expect(cloneLineage([], sequentialIds())).toEqual({ entries: [], idMapping: new Map() }); }); it('stamps a missing timestamp with the current time', () => { diff --git a/packages/api/src/conversations/lineage.ts b/packages/api/src/conversations/lineage.ts index 65d2b2500c..7bf717a525 100644 --- a/packages/api/src/conversations/lineage.ts +++ b/packages/api/src/conversations/lineage.ts @@ -19,8 +19,7 @@ export interface LineageSource { export interface ClonedLineageEntry { source: T; messageId: string; - /** Undefined when the source's parent had not been cloned yet in clone order. */ - parentMessageId: string | undefined; + parentMessageId: string; createdAt: Date; } @@ -129,16 +128,14 @@ const toDate = (value: LineageSource['createdAt']): Date => { }; /** - * Assigns each message a new id, re-links it to its parent's clone and moves its timestamp - * after that clone's. Root messages are cloned first; a message whose parent is cloned later - * keeps an undefined parent link, as the caller's persistence decides where it attaches. + * Assigns all clone ids before re-linking parents, independent of source order. + * Missing parents become roots; cycles and timestamps are normalized on the clones only. */ export function cloneLineage( messages: readonly T[], createId: () => string, ): ClonedLineage { const idMapping = new Map(); - const clonedCreatedAt = new Map(); const ordered = [...messages].sort((a, b) => { if (a.parentMessageId === Constants.NO_PARENT) { return -1; @@ -153,22 +150,22 @@ export function cloneLineage( const messageId = createId(); idMapping.set(source.messageId, messageId); - const parentMessageId = - source.parentMessageId && source.parentMessageId !== Constants.NO_PARENT - ? idMapping.get(source.parentMessageId) - : Constants.NO_PARENT; - - let createdAt = toDate(source.createdAt); - const parentCreatedAt = - parentMessageId == null ? undefined : clonedCreatedAt.get(parentMessageId); - if (parentCreatedAt != null && createdAt <= parentCreatedAt) { - createdAt = new Date(parentCreatedAt.getTime() + 1); - } - clonedCreatedAt.set(messageId, createdAt); - - return { source, messageId, parentMessageId, createdAt }; + return { + source, + messageId, + parentMessageId: source.parentMessageId || Constants.NO_PARENT, + createdAt: toDate(source.createdAt), + }; }); + for (const entry of entries) { + entry.parentMessageId = + entry.parentMessageId === Constants.NO_PARENT + ? Constants.NO_PARENT + : (idMapping.get(entry.parentMessageId) ?? Constants.NO_PARENT); + } + orderMessageLineage(entries); + return { entries, idMapping }; }