mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-16 13:00:55 +00:00
* feat: add ephemeral retention mode for forced temporary chats
Adds RetentionMode.EPHEMERAL, which forces every newly saved chat
temporary: hidden from history and search, given the temporary-chat
expiration, and with the toggle locked on.
Forces isTemporary and applies the temporary expiration in the normal
conversation and message save methods, routes Assistants/Threads
messages through the normal message save, applies the all-data
retention policy to file uploads and shared links, and stamps imports,
forks, and duplicates. Locks the frontend toggle and keyboard shortcut
behind an administrator-enforced badge.
The default temporary mode and the existing all mode are unchanged.
* fix: enforce ephemeral retention when resuming paused chats
* fix: preserve forced retention across pauses and assistant turns
* test: Use Real Module IDs for Installed Package Mocks
* test: Wait for Resumed Retention Persistence
* test: Control Mongo Index Setup in OpenID Fixture
* test: Close Mobile Drawer Before Retention Resume Scenario
* fix: Store Responses API Messages Under the Retention Write Context
Input and output messages were saved with the raw request, so under
ephemeral retention they missed the forced-temporary classification the
conversation received. Route both through getConversationWriteContext
and announce the stored reply as saved.
* fix: Align Resumed Responses With the Parent Retention Deadline
A resumed response was saved with a fresh forced-temporary window while
the re-stamp kept the conversation's earlier deadline, so a chat that
paused again could leave its response stored after the conversation
expired. Stamp the saved response with the parent's deadline.
* fix: Clear Released Bookmark Tags When Forcing a Chat Temporary
stampForcedRetention released a converted chat's bookmark counts but
kept its tags, so deleting that chat before TTL removal decremented the
same tags again and reduced counts owned by other visible chats. Clear
the tags in the conditional conversion update that releases them.
* fix: Store No Bookmark Tags on Forced-Temporary Imports
An ephemeral import, fork or duplicate skipped its tag count increment
but kept the source tags, so deleting the copy before TTL removal
decremented counts owned by other visible chats. Store the same tags the
import counts, which is none for a forced-temporary copy.
* test: Provide the Forced-Temporary Retention Helper to the Header Stacking Mock
* test: Reuse One Admin Login per Worker in Ephemeral Retention Scenarios
* fix: Keep Legacy Temporary Chats Temporary When Forking Under All-Data Retention
* fix: Convert a Resumed Chat Under Ephemeral Retention Before It Can Re-Pause Empty
* fix: Treat a Reloaded Pre-Policy Run as Temporary Once Retention Is Forced
* fix: Stamp Messages with the Deadline a Concurrent Retention Stamp Stored
* fix: Wait for the Startup Config Before Rebuilding a Resumed Submission
* test: Wait for the Resumed Response Row in the Ephemeral Resume Scenario
* test: Provide the Real Command Executor Stamp to the Callbacks Mock
(cherry picked from commit 02a70e9c58)
Original-PR: https://github.com/LibreChat-AI/LibreChat/pull/13811
218 lines
7.4 KiB
JavaScript
218 lines
7.4 KiB
JavaScript
const mongoose = require('mongoose');
|
|
const { v4 } = require('uuid');
|
|
const { MongoMemoryServer } = require('mongodb-memory-server');
|
|
|
|
jest.mock('@librechat/api', () => ({
|
|
...jest.requireActual('@librechat/api'),
|
|
countTokens: jest.fn().mockResolvedValue(1),
|
|
}));
|
|
|
|
jest.mock('~/server/services/Files/process', () => ({ retrieveAndProcessFile: jest.fn() }));
|
|
|
|
const { Message, Conversation, ChatProject } = require('~/db/models');
|
|
const { createChatProject, assignConversationToProject } = require('~/models');
|
|
const { saveUserMessage, saveAssistantMessage, checkMessageGaps } = require('./manage');
|
|
|
|
describe('Assistants message retention', () => {
|
|
let mongoServer;
|
|
|
|
beforeAll(async () => {
|
|
mongoServer = await MongoMemoryServer.create();
|
|
await mongoose.connect(mongoServer.getUri());
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await mongoose.disconnect();
|
|
await mongoServer.stop();
|
|
});
|
|
|
|
it('seeds Project membership once and preserves a concurrent move without revalidation', async () => {
|
|
const user = new mongoose.Types.ObjectId().toString();
|
|
const conversationId = v4();
|
|
const firstProject = await createChatProject(user, { name: 'Original project' });
|
|
const nextProject = await createChatProject(user, { name: 'Moved project' });
|
|
const firstProjectId = firstProject._id.toString();
|
|
const nextProjectId = nextProject._id.toString();
|
|
const req = {
|
|
user: { id: user },
|
|
body: { conversationId },
|
|
resolvedConversation: null,
|
|
chatProjectContext: { projectId: firstProjectId },
|
|
config: {},
|
|
};
|
|
const params = {
|
|
user,
|
|
conversationId,
|
|
endpoint: 'assistants',
|
|
assistant_id: 'asst_project',
|
|
thread_id: 'thread_project',
|
|
text: 'Continue with the authorized context.',
|
|
};
|
|
await saveUserMessage(req, { ...params, messageId: v4() });
|
|
expect(req.resolvedConversation.chatProjectId).toBe(firstProjectId);
|
|
|
|
await assignConversationToProject(user, conversationId, nextProjectId);
|
|
const projectLookup = jest.spyOn(ChatProject, 'exists');
|
|
try {
|
|
await saveUserMessage(req, { ...params, messageId: v4() });
|
|
const saved = await Conversation.findOne({ user, conversationId }).lean();
|
|
expect(saved.chatProjectId).toBe(nextProjectId);
|
|
expect(projectLookup).not.toHaveBeenCalled();
|
|
} finally {
|
|
projectLookup.mockRestore();
|
|
}
|
|
});
|
|
|
|
it.each([false, true])(
|
|
'retains the full turn and gap sync with isTemporary=%s',
|
|
async (isTemporary) => {
|
|
const user = new mongoose.Types.ObjectId().toString();
|
|
const conversationId = v4();
|
|
const req = {
|
|
user: { id: user },
|
|
body: { conversationId, isTemporary },
|
|
resolvedConversation: null,
|
|
config: {
|
|
interfaceConfig: {
|
|
retentionMode: 'all',
|
|
generalChatRetention: 24,
|
|
temporaryChatRetention: 1,
|
|
},
|
|
},
|
|
};
|
|
const params = {
|
|
user,
|
|
conversationId,
|
|
endpoint: 'assistants',
|
|
assistant_id: 'asst_test',
|
|
thread_id: 'thread_test',
|
|
text: 'hello',
|
|
};
|
|
const startedAt = Date.now();
|
|
const userMessage = await saveUserMessage(req, { ...params, messageId: v4() });
|
|
expect(userMessage.expiredAt.getTime()).toBeGreaterThanOrEqual(
|
|
startedAt + (isTemporary ? 1 : 24) * 3600000,
|
|
);
|
|
expect(userMessage.expiredAt.getTime()).toBeLessThan(
|
|
startedAt + (isTemporary ? 1 : 24) * 3600000 + 5000,
|
|
);
|
|
// A response must retain the admission policy even if request fields change.
|
|
req.body.isTemporary = !isTemporary;
|
|
const attachments = [
|
|
{
|
|
type: 'ui_resources',
|
|
messageId: 'assistant-app-message',
|
|
conversationId,
|
|
toolCallId: 'call_1',
|
|
ui_resources: [{ uri: 'ui://app', mimeType: 'text/html;profile=mcp-app' }],
|
|
},
|
|
];
|
|
await saveAssistantMessage(req, {
|
|
...params,
|
|
messageId: v4(),
|
|
parentMessageId: userMessage.messageId,
|
|
content: [],
|
|
attachments,
|
|
});
|
|
await checkMessageGaps({
|
|
openai: {
|
|
req,
|
|
beta: {
|
|
threads: {
|
|
messages: {
|
|
update: jest.fn().mockResolvedValue({}),
|
|
list: jest.fn().mockResolvedValue({
|
|
data: [{ id: 'msg_user', role: 'user', content: [], created_at: 1 }],
|
|
}),
|
|
},
|
|
runs: { steps: { list: jest.fn().mockResolvedValue({ data: [] }) } },
|
|
},
|
|
},
|
|
},
|
|
endpoint: 'assistants',
|
|
thread_id: 'thread_test',
|
|
conversationId,
|
|
latestMessageId: v4(),
|
|
run_id: 'run_test',
|
|
});
|
|
const rows = await Message.find({ user, conversationId }).lean();
|
|
const convo = await Conversation.findOne({ user, conversationId }).lean();
|
|
expect(rows).toHaveLength(4);
|
|
for (const row of [...rows, convo]) {
|
|
expect(row.isTemporary).toBe(isTemporary);
|
|
expect(row.expiredAt).toEqual(userMessage.expiredAt);
|
|
}
|
|
expect(rows.find((row) => row.isCreatedByUser === false)?.attachments).toEqual(attachments);
|
|
expect(convo.lastResponseAt != null).toBe(!isTemporary);
|
|
},
|
|
);
|
|
|
|
it('appends the saved turn IDs without reloading message history', async () => {
|
|
const user = new mongoose.Types.ObjectId().toString();
|
|
const conversationId = v4();
|
|
const req = { user: { id: user }, body: {}, config: {} };
|
|
const params = {
|
|
user,
|
|
conversationId,
|
|
endpoint: 'assistants',
|
|
assistant_id: 'asst_test',
|
|
thread_id: 'thread_test',
|
|
text: 'hello',
|
|
};
|
|
const history = jest.spyOn(Message, 'find');
|
|
try {
|
|
const userMessage = await saveUserMessage(req, { ...params, messageId: v4() });
|
|
const { message, conversation } = await saveAssistantMessage(req, {
|
|
...params,
|
|
messageId: v4(),
|
|
parentMessageId: userMessage.messageId,
|
|
content: [],
|
|
});
|
|
|
|
expect(history).not.toHaveBeenCalled();
|
|
expect(new Set(conversation.messages.map(String))).toEqual(
|
|
new Set([String(userMessage._id), String(message._id)]),
|
|
);
|
|
} finally {
|
|
history.mockRestore();
|
|
}
|
|
});
|
|
|
|
it('forces the whole turn temporary under ephemeral retention', async () => {
|
|
const user = new mongoose.Types.ObjectId().toString();
|
|
const conversationId = v4();
|
|
const req = {
|
|
user: { id: user },
|
|
body: { conversationId, isTemporary: false },
|
|
resolvedConversation: null,
|
|
config: {
|
|
interfaceConfig: { retentionMode: 'ephemeral', temporaryChatRetention: 1 },
|
|
},
|
|
};
|
|
const params = {
|
|
user,
|
|
conversationId,
|
|
endpoint: 'assistants',
|
|
assistant_id: 'asst_test',
|
|
thread_id: 'thread_test',
|
|
text: 'hello',
|
|
};
|
|
const startedAt = Date.now();
|
|
const userMessage = await saveUserMessage(req, { ...params, messageId: v4() });
|
|
await saveAssistantMessage(req, {
|
|
...params,
|
|
messageId: v4(),
|
|
parentMessageId: userMessage.messageId,
|
|
content: [],
|
|
});
|
|
|
|
const rows = await Message.find({ user, conversationId }).lean();
|
|
const convo = await Conversation.findOne({ user, conversationId }).lean();
|
|
expect(rows).toHaveLength(2);
|
|
for (const row of [...rows, convo]) {
|
|
expect(row.isTemporary).toBe(true);
|
|
expect(row.expiredAt.getTime()).toBeGreaterThanOrEqual(startedAt + 3600000);
|
|
expect(row.expiredAt.getTime()).toBeLessThan(startedAt + 3600000 + 5000);
|
|
}
|
|
});
|
|
});
|