🎍 fix: Preserve Every Conversation Branch When Duplicating (#18257)

* 🎍 fix: Preserve Every Conversation Branch When Duplicating

* style: Normalize Regression Fixture Formatting

---------

Co-authored-by: Lia <lia@librechat.ai>
This commit is contained in:
lia-by-librechat[bot] 2026-11-17 09:54:40 -05:00 • committed by GitHub
parent 8a7fe7b9f3
commit 7608737d76
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 227 additions and 36 deletions

View file

@ -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);

View file

@ -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',