mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-18 22:14:27 +00:00
🚸 fix: Scope Queued-Turn Deletion to Selected Conversations (#16270)
Co-authored-by: Lia <lia@librechat.ai>
This commit is contained in:
parent
7979bdbfd4
commit
b28a89e86d
3 changed files with 188 additions and 9 deletions
|
|
@ -191,6 +191,64 @@ describe('queued-turn conversation deletion through createMethods', () => {
|
|||
},
|
||||
);
|
||||
|
||||
it.each([false, true])(
|
||||
'deletes only the selected conversation (has queued turn: %s) and preserves unrelated work',
|
||||
async (hasQueuedTurn) => {
|
||||
const admitted = await admitQueuedTurn();
|
||||
const owner = { user: admitted.user, tenantId: admitted.tenantId };
|
||||
const enqueue = (conversationId: string) =>
|
||||
methods.enqueueAgentQueuedTurn({
|
||||
...owner,
|
||||
conversationId,
|
||||
agentId: 'agent-1',
|
||||
parentMessageId: 'parent-1',
|
||||
clientRequestId: 'pending-turn',
|
||||
text: 'keep this queued turn',
|
||||
availableAt: admitted.now,
|
||||
});
|
||||
await enqueue(admitted.conversationId);
|
||||
const selected = await models.Conversation.create({
|
||||
...owner,
|
||||
user: owner.user.toString(),
|
||||
conversationId: 'selected-conversation',
|
||||
endpoint: 'agents',
|
||||
});
|
||||
if (hasQueuedTurn) {
|
||||
await enqueue(selected.conversationId);
|
||||
}
|
||||
const unrelated = { conversationId: admitted.conversationId };
|
||||
const before = {
|
||||
turns: await models.AgentQueuedTurn.find(unrelated).sort('_id').lean(),
|
||||
lane: await models.AgentQueuedTurnSequence.findOne(unrelated).lean(),
|
||||
conversation: await models.Conversation.findOne(unrelated).lean(),
|
||||
delivery: await models.AgentTriggerDelivery.findOne({
|
||||
deliveryKey: admitted.deliveryKey,
|
||||
}).lean(),
|
||||
};
|
||||
|
||||
await expect(
|
||||
methods.deleteConvos(owner.user.toString(), { conversationId: selected.conversationId }),
|
||||
).resolves.toMatchObject({ deletedCount: 1, conversationIds: [selected.conversationId] });
|
||||
|
||||
expect(await models.Conversation.findById(selected._id)).toBeNull();
|
||||
expect(
|
||||
await models.AgentQueuedTurn.countDocuments({ conversationId: selected.conversationId }),
|
||||
).toBe(0);
|
||||
await expect(models.AgentQueuedTurn.find(unrelated).sort('_id').lean()).resolves.toEqual(
|
||||
before.turns,
|
||||
);
|
||||
await expect(models.AgentQueuedTurnSequence.findOne(unrelated).lean()).resolves.toEqual(
|
||||
before.lane,
|
||||
);
|
||||
await expect(models.Conversation.findOne(unrelated).lean()).resolves.toEqual(
|
||||
before.conversation,
|
||||
);
|
||||
await expect(
|
||||
models.AgentTriggerDelivery.findOne({ deliveryKey: admitted.deliveryKey }).lean(),
|
||||
).resolves.toEqual(before.delivery);
|
||||
},
|
||||
);
|
||||
|
||||
it('keeps explicit terminal-handling fences and recovers a failed retirement on retry', async () => {
|
||||
const turn = await admitQueuedTurn(true);
|
||||
const remove = () =>
|
||||
|
|
|
|||
|
|
@ -2386,6 +2386,9 @@ describe('agent queued turn methods', () => {
|
|||
await methods.enqueueAgentQueuedTurn(
|
||||
enqueueInput({ conversationId: 'conversation-2', clientRequestId: 'keep-conversation' }),
|
||||
);
|
||||
const untouched = await Turn.find({ _id: { $ne: tenantOne.turn.queuedTurnId } })
|
||||
.sort('_id')
|
||||
.lean();
|
||||
|
||||
await expect(
|
||||
methods.prepareAgentQueuedTurnConversationDeletion({
|
||||
|
|
@ -2435,6 +2438,7 @@ describe('agent queued turn methods', () => {
|
|||
}),
|
||||
).resolves.toBe(1);
|
||||
expect(await Turn.countDocuments({ user, conversationId: 'conversation-1' })).toBe(1);
|
||||
await expect(Turn.find().sort('_id').lean()).resolves.toEqual(untouched);
|
||||
await expect(
|
||||
methods.enqueueAgentQueuedTurn(
|
||||
enqueueInput({ clientRequestId: 'cannot-reopen-deleted-lane' }),
|
||||
|
|
@ -2452,8 +2456,117 @@ describe('agent queued turn methods', () => {
|
|||
});
|
||||
expect(await Turn.countDocuments({ user, conversationId: 'conversation-1' })).toBe(0);
|
||||
expect(await Turn.countDocuments({ user, conversationId: 'conversation-2' })).toBe(1);
|
||||
await expect(Turn.find({ conversationId: 'conversation-2' }).lean()).resolves.toEqual(
|
||||
untouched.filter((turn) => turn.conversationId === 'conversation-2'),
|
||||
);
|
||||
});
|
||||
|
||||
it.each(['reserving', 'queued', 'claimed'] as const)(
|
||||
'only cancels selected conversation lanes with %s turns',
|
||||
async (status) => {
|
||||
const otherUser = new mongoose.Types.ObjectId();
|
||||
const scopes = [
|
||||
{ conversationId: 'conversation-1', tenantId: 'tenant-1' },
|
||||
{ conversationId: 'conversation-3', tenantId: undefined },
|
||||
{ conversationId: 'conversation-2', tenantId: 'tenant-1' },
|
||||
{ conversationId: 'conversation-1', tenantId: 'tenant-2' },
|
||||
{ conversationId: 'conversation-1', tenantId: undefined },
|
||||
{ conversationId: 'conversation-1', tenantId: 'tenant-1', user: otherUser },
|
||||
];
|
||||
const turns = await Promise.all(
|
||||
scopes.map((scope, index) =>
|
||||
methods.enqueueAgentQueuedTurn(
|
||||
enqueueInput({ ...scope, clientRequestId: `isolation-${index}` }),
|
||||
),
|
||||
),
|
||||
);
|
||||
if (status === 'reserving') {
|
||||
await Turn.updateMany({}, { $set: { status, reservationWriterId: 'pending-writer' } });
|
||||
} else if (status === 'claimed') {
|
||||
for (const [index, scope] of scopes.entries()) {
|
||||
await expect(
|
||||
methods.claimNextAgentQueuedTurn(claimInput(turns[index].turn.queuedTurnId, scope)),
|
||||
).resolves.toMatchObject({ outcome: 'acquired' });
|
||||
}
|
||||
}
|
||||
const untouchedIds = turns.slice(2).map(({ turn }) => turn.queuedTurnId);
|
||||
const untouched = await Turn.find({ _id: { $in: untouchedIds } })
|
||||
.sort('_id')
|
||||
.lean();
|
||||
const untouchedLanes = await Sequence.find({
|
||||
laneId: { $in: untouched.map((turn) => turn.laneId) },
|
||||
})
|
||||
.sort('_id')
|
||||
.lean();
|
||||
const input = { user, targets: scopes.slice(0, 2), settledAt: START };
|
||||
|
||||
await expect(methods.prepareAgentQueuedTurnConversationDeletion(input)).resolves.toEqual([]);
|
||||
for (const { turn } of turns.slice(0, 2)) {
|
||||
await expect(Turn.findById(turn.queuedTurnId).lean()).resolves.toMatchObject({
|
||||
status: 'cancelled',
|
||||
deliveryState: 'retired',
|
||||
terminalReceipt: { failure: { code: 'OWNER_DRAINED' } },
|
||||
});
|
||||
}
|
||||
await expect(
|
||||
Turn.find({ _id: { $in: untouchedIds } })
|
||||
.sort('_id')
|
||||
.lean(),
|
||||
).resolves.toEqual(untouched);
|
||||
await expect(methods.deletePreparedAgentQueuedTurnConversations(input)).resolves.toBe(2);
|
||||
await expect(Turn.find().sort('_id').lean()).resolves.toEqual(untouched);
|
||||
await expect(
|
||||
Sequence.find({ laneId: { $in: untouched.map((turn) => turn.laneId) } })
|
||||
.sort('_id')
|
||||
.lean(),
|
||||
).resolves.toEqual(untouchedLanes);
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
{ name: 'tenant-scoped', targets: [{ conversationId: 'empty-chat', tenantId: 'tenant-1' }] },
|
||||
{ name: 'legacy tenantless', targets: [{ conversationId: 'conversation-1' }] },
|
||||
{
|
||||
name: 'another tenant',
|
||||
targets: [{ conversationId: 'conversation-1', tenantId: 'tenant-2' }],
|
||||
},
|
||||
{ name: 'all tenants', targets: [{ conversationId: 'empty-chat', allTenants: true as const }] },
|
||||
])(
|
||||
'deletes an empty $name conversation despite an unrelated published delivery',
|
||||
async ({ targets }) => {
|
||||
const queued = await methods.enqueueAgentQueuedTurn(enqueueInput());
|
||||
const deliveryKey = 'unrelated-published-delivery';
|
||||
const delivery = { ...claimInput(queued.turn.queuedTurnId), deliveryKey };
|
||||
await methods.reserveAgentQueuedTurnDelivery(delivery);
|
||||
await methods.markQueuedTurnScheduled({ ...delivery, scheduledAt: START });
|
||||
await methods.claimNextAgentQueuedTurn(delivery);
|
||||
await methods.beginAgentQueuedTurnAdmission({
|
||||
...delivery,
|
||||
admissionId: deliveryKey,
|
||||
startedAt: START,
|
||||
});
|
||||
await methods.markAgentQueuedTurnAdmitted({
|
||||
...delivery,
|
||||
admissionId: deliveryKey,
|
||||
admissionMode: 'ordinary',
|
||||
generationCreatedAt: 42,
|
||||
lineagePredecessorId: rootLineageId(),
|
||||
settledAt: LATER,
|
||||
});
|
||||
const untouched = await Turn.findById(queued.turn.queuedTurnId).lean();
|
||||
expect(untouched).toMatchObject({ status: 'admitted', deliveryState: 'published' });
|
||||
const lane = await Sequence.findOne({ laneId: untouched?.laneId }).lean();
|
||||
const input = { user, targets, settledAt: START };
|
||||
|
||||
await expect(methods.prepareAgentQueuedTurnConversationDeletion(input)).resolves.toEqual([]);
|
||||
await expect(methods.deletePreparedAgentQueuedTurnConversations(input)).resolves.toBe(0);
|
||||
await expect(methods.prepareAgentQueuedTurnConversationDeletion(input)).resolves.toEqual([]);
|
||||
await expect(methods.deletePreparedAgentQueuedTurnConversations(input)).resolves.toBe(0);
|
||||
await expect(Turn.findById(queued.turn.queuedTurnId).lean()).resolves.toEqual(untouched);
|
||||
await expect(Sequence.findOne({ laneId: untouched?.laneId }).lean()).resolves.toEqual(lane);
|
||||
},
|
||||
);
|
||||
|
||||
it('includes preexisting dead deliveries in conversation retirement', async () => {
|
||||
const queued = await methods.enqueueAgentQueuedTurn(
|
||||
enqueueInput({ clientRequestId: 'dead-before-delete' }),
|
||||
|
|
|
|||
|
|
@ -2773,10 +2773,14 @@ export function createAgentQueuedTurnMethods(
|
|||
}
|
||||
await Turn().updateMany(
|
||||
{
|
||||
...scope,
|
||||
$or: [
|
||||
{ status: { $in: ['reserving', 'queued'] } },
|
||||
{ status: 'claimed', admissionStartedAt: { $exists: false } },
|
||||
$and: [
|
||||
scope,
|
||||
{
|
||||
$or: [
|
||||
{ status: { $in: ['reserving', 'queued'] } },
|
||||
{ status: 'claimed', admissionStartedAt: { $exists: false } },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
|
|
@ -2838,11 +2842,15 @@ export function createAgentQueuedTurnMethods(
|
|||
}): Promise<number> {
|
||||
const scope = deletionScope(input);
|
||||
const blocker = await Turn().exists({
|
||||
...scope,
|
||||
$or: [
|
||||
{ deliveryKey: { $exists: true }, deliveryState: { $ne: 'retired' } },
|
||||
{ status: { $in: ['reserving', 'queued', 'claimed'] } },
|
||||
{ admissionStartedAt: { $exists: true } },
|
||||
$and: [
|
||||
scope,
|
||||
{
|
||||
$or: [
|
||||
{ deliveryKey: { $exists: true }, deliveryState: { $ne: 'retired' } },
|
||||
{ status: { $in: ['reserving', 'queued', 'claimed'] } },
|
||||
{ admissionStartedAt: { $exists: true } },
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
if (blocker != null) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue