mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-19 22:43:38 +00:00
fix: Preserve Durable Parents for Queued Turns (#15787)
* fix: resolve queued turn placeholder anchors * fix: anchor queued turns to durable message parents * fix: preserve durable queued turn anchors
This commit is contained in:
parent
3cab71c48a
commit
f0c9dee669
6 changed files with 214 additions and 8 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { renderHook, act } from '@testing-library/react';
|
||||
import { Constants, EModelEndpoint } from 'librechat-data-provider';
|
||||
import { Constants, ContentTypes, EModelEndpoint, createPayload } from 'librechat-data-provider';
|
||||
import type {
|
||||
CodeWorkspaceSelection,
|
||||
TConversation,
|
||||
|
|
@ -167,6 +167,24 @@ describe('useChatFunctions ask', () => {
|
|||
expect(submission.codeApprovalMode).toBe('acceptEdits');
|
||||
});
|
||||
|
||||
it('preallocates a durable Agents user id for the optimistic response anchor', () => {
|
||||
const { result, setSubmission } = renderAsk([]);
|
||||
|
||||
act(() => {
|
||||
result.current.ask({ text: 'Queue safely', conversationId: 'conversation-1' });
|
||||
});
|
||||
|
||||
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission;
|
||||
const userMessageId = submission.userMessage.messageId;
|
||||
expect(submission.endpointOption.overrideUserMessageId).toBe(
|
||||
`${userMessageId}${Constants.COMMON_DIVIDER}0`,
|
||||
);
|
||||
expect(createPayload(submission).payload.overrideUserMessageId).toBe(
|
||||
`${userMessageId}${Constants.COMMON_DIVIDER}0`,
|
||||
);
|
||||
expect(submission.initialResponse?.clientQueueParentMessageId).toBe(userMessageId);
|
||||
});
|
||||
|
||||
it('submits the latest validated workspace selection', () => {
|
||||
const selection = { environmentId: 'personal-vm', workspaceId: 'project-a' };
|
||||
mockResolveCodeWorkspaceSubmission.mockReturnValue({ codeWorkspaces: [selection] });
|
||||
|
|
@ -346,6 +364,7 @@ describe('useChatFunctions regenerate', () => {
|
|||
expect(submission.userMessage.responseMessageId).toBe('assistant-1_');
|
||||
expect(submission.initialResponse?.messageId).toBe('assistant-1_');
|
||||
expect(submission.initialResponse?.parentMessageId).toBe('user-1');
|
||||
expect(submission.initialResponse?.clientQueueParentMessageId).toBe('user-1');
|
||||
expect(submission.messages.map((message) => message.messageId)).toEqual(['user-1']);
|
||||
expect(submission.regenerateMessages?.map((message) => message.messageId)).toEqual([
|
||||
'user-1',
|
||||
|
|
@ -360,6 +379,38 @@ describe('useChatFunctions regenerate', () => {
|
|||
).toEqual(['user-1', 'assistant-1_']);
|
||||
expect(messages.at(-1)?.messageId).toBe('assistant-1_');
|
||||
});
|
||||
|
||||
it.each(['assistant-1', 'assistant_1_'])(
|
||||
'marks an edited optimistic response %s with its durable user anchor',
|
||||
(responseMessageId) => {
|
||||
const parent = userMessage('user-1');
|
||||
const response = {
|
||||
...assistantMessage(responseMessageId, parent.messageId),
|
||||
content: [{ type: ContentTypes.TEXT, [ContentTypes.TEXT]: 'before' }],
|
||||
} as TMessage;
|
||||
const { result, setSubmission } = renderAsk([parent, response]);
|
||||
|
||||
act(() => {
|
||||
result.current.ask(
|
||||
{ ...parent },
|
||||
{
|
||||
isRegenerate: true,
|
||||
isEdited: true,
|
||||
editedMessageId: responseMessageId,
|
||||
editedContent: {
|
||||
index: 0,
|
||||
type: ContentTypes.TEXT,
|
||||
[ContentTypes.TEXT]: 'after',
|
||||
},
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission;
|
||||
expect(submission.initialResponse?.messageId).toBe(responseMessageId);
|
||||
expect(submission.initialResponse?.clientQueueParentMessageId).toBe('user-1');
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe('useChatFunctions ask attachments', () => {
|
||||
|
|
|
|||
|
|
@ -338,6 +338,77 @@ describe('useSteering', () => {
|
|||
);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['new response', 'user-message_'],
|
||||
['regenerated response', 'old-assistant_'],
|
||||
['edited response', 'old-assistant'],
|
||||
])('anchors an optimistic %s through its durable user parent', async (_label, messageId) => {
|
||||
mockMessages = [
|
||||
{
|
||||
messageId: 'user-message',
|
||||
parentMessageId: Constants.NO_PARENT,
|
||||
isCreatedByUser: true,
|
||||
createdAt: '2026-09-09T12:00:00.000Z',
|
||||
updatedAt: '2026-09-09T12:00:00.000Z',
|
||||
} as TMessage,
|
||||
...(messageId === 'old-assistant_'
|
||||
? [
|
||||
{
|
||||
messageId: 'old-assistant',
|
||||
parentMessageId: 'user-message',
|
||||
isCreatedByUser: false,
|
||||
createdAt: '2026-09-09T12:00:01.000Z',
|
||||
updatedAt: '2026-09-09T12:00:01.000Z',
|
||||
} as TMessage,
|
||||
]
|
||||
: []),
|
||||
{
|
||||
messageId,
|
||||
parentMessageId: 'user-message',
|
||||
isCreatedByUser: false,
|
||||
clientQueueParentMessageId: 'user-message',
|
||||
} as TMessage,
|
||||
];
|
||||
const { result } = setupServerQueue();
|
||||
|
||||
await act(async () => {
|
||||
expect(result.current.steering.queueFromComposer('follow the pending answer')).toBe(true);
|
||||
await Promise.resolve();
|
||||
});
|
||||
|
||||
expect(mockEnqueueQueuedTurn).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ parentMessageId: 'user-message' }),
|
||||
expect.any(Object),
|
||||
);
|
||||
expect(result.current.queue).toEqual([
|
||||
expect.objectContaining({ parentMessageId: 'user-message' }),
|
||||
]);
|
||||
});
|
||||
|
||||
it('preserves an exact persisted assistant id ending in an underscore', async () => {
|
||||
mockMessages = [
|
||||
{
|
||||
messageId: 'persisted-assistant_',
|
||||
parentMessageId: 'user-message',
|
||||
isCreatedByUser: false,
|
||||
} as TMessage,
|
||||
];
|
||||
const { result } = setupServerQueue();
|
||||
|
||||
await act(async () => {
|
||||
expect(result.current.steering.queueFromComposer('keep the exact branch')).toBe(true);
|
||||
await Promise.resolve();
|
||||
});
|
||||
|
||||
expect(mockEnqueueQueuedTurn).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ parentMessageId: 'persisted-assistant_' }),
|
||||
expect.any(Object),
|
||||
);
|
||||
expect(result.current.queue).toEqual([
|
||||
expect.objectContaining({ parentMessageId: 'persisted-assistant_' }),
|
||||
]);
|
||||
});
|
||||
|
||||
it('applies an admitted POST replay to the consumed predecessor set', async () => {
|
||||
mockEnqueueQueuedTurn.mockImplementation((input, options) => {
|
||||
options.onSuccess({
|
||||
|
|
|
|||
|
|
@ -527,7 +527,11 @@ export default function useChatFunctions({
|
|||
endpoint,
|
||||
endpointType,
|
||||
overrideConvoId,
|
||||
overrideUserMessageId,
|
||||
overrideUserMessageId:
|
||||
overrideUserMessageId ??
|
||||
(endpoint === EModelEndpoint.agents && !regenerateShaped && !isContinued
|
||||
? `${intermediateId}${Constants.COMMON_DIVIDER}0`
|
||||
: undefined),
|
||||
},
|
||||
convo,
|
||||
chatProjectId ? { chatProjectId } : {},
|
||||
|
|
@ -643,6 +647,7 @@ export default function useChatFunctions({
|
|||
model: convo?.model,
|
||||
error: false,
|
||||
iconURL,
|
||||
clientQueueParentMessageId: regenerateShaped ? messageId : intermediateId,
|
||||
/**
|
||||
* Seed the assistant placeholder with the turn's manually-invoked
|
||||
* skill names so `ContentParts` can render interim `SkillCall` cards
|
||||
|
|
|
|||
|
|
@ -147,10 +147,10 @@ interface LiveMessageState {
|
|||
}
|
||||
|
||||
function selectLiveMessageState(message: TMessage | null): LiveMessageState {
|
||||
const parentMessageId =
|
||||
message?.isCreatedByUser === false && typeof message.messageId === 'string'
|
||||
? message.messageId
|
||||
: undefined;
|
||||
let parentMessageId: string | undefined;
|
||||
if (message?.isCreatedByUser === false && typeof message.messageId === 'string') {
|
||||
parentMessageId = message.clientQueueParentMessageId ?? message.messageId;
|
||||
}
|
||||
return { approval: hasLiveToolApproval(message), parentMessageId };
|
||||
}
|
||||
|
||||
|
|
@ -695,8 +695,9 @@ export default function useSteering({
|
|||
}
|
||||
}, [pendingSteers, queueKey]);
|
||||
|
||||
/** The exact visible assistant tail is captured into a server queued turn,
|
||||
* while the approval bit keeps the steering controls honest. */
|
||||
/** Capture a durable branch anchor into a server queued turn. Optimistic
|
||||
* assistant placeholders are siblings of their eventual persisted response,
|
||||
* so they anchor through their stable user parent. */
|
||||
const latestMessage = useLatestMessage(index, hasRealConvoId ? conversationId : null);
|
||||
const liveMessageState = useMemo(() => selectLiveMessageState(latestMessage), [latestMessage]);
|
||||
/** Both approval cards and `ask_user_question` suspend the current
|
||||
|
|
|
|||
|
|
@ -492,6 +492,82 @@ describe('Agent queued-turn continuation', () => {
|
|||
);
|
||||
});
|
||||
|
||||
it('does not reinterpret a missing underscore-suffixed anchor', async () => {
|
||||
const { methods, spies } = resolverMethods();
|
||||
const queued = claim();
|
||||
queued.parentMessageId = 'user-1_';
|
||||
spies.claimNextAgentQueuedTurn.mockResolvedValueOnce({ outcome: 'acquired', claim: queued });
|
||||
spies.getMessages.mockResolvedValueOnce([
|
||||
persistedMessage({
|
||||
messageId: 'user-1',
|
||||
parentMessageId: 'root',
|
||||
isCreatedByUser: true,
|
||||
createdAt: new Date(NOW - 1_000),
|
||||
}),
|
||||
persistedMessage({
|
||||
messageId: 'assistant-1',
|
||||
parentMessageId: 'user-1',
|
||||
isCreatedByUser: false,
|
||||
createdAt: new Date(NOW),
|
||||
}),
|
||||
]);
|
||||
const resolve = createAgentQueuedTurnResolver({
|
||||
methods,
|
||||
getGenerationJob: async () => null,
|
||||
now: () => NOW,
|
||||
claimBy: 'worker-1',
|
||||
});
|
||||
|
||||
await expect(resolve(envelope(), { idempotencyKey: 'trigger-1' })).rejects.toMatchObject({
|
||||
code: 'PARENT_NOT_FOUND',
|
||||
});
|
||||
expect(spies.releaseAgentQueuedTurn).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
queuedTurnId: queued.queuedTurnId,
|
||||
disposition: 'dead',
|
||||
failure: expect.objectContaining({ code: 'PARENT_NOT_FOUND' }),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('continues after the newest regenerated response under a durable user anchor', async () => {
|
||||
const { methods, spies } = resolverMethods();
|
||||
const queued = claim();
|
||||
queued.parentMessageId = 'user-1';
|
||||
spies.claimNextAgentQueuedTurn.mockResolvedValueOnce({ outcome: 'acquired', claim: queued });
|
||||
spies.getMessages.mockResolvedValueOnce([
|
||||
persistedMessage({
|
||||
messageId: 'user-1',
|
||||
parentMessageId: 'root',
|
||||
isCreatedByUser: true,
|
||||
createdAt: new Date(NOW - 3_000),
|
||||
}),
|
||||
persistedMessage({
|
||||
messageId: 'old-assistant',
|
||||
parentMessageId: 'user-1',
|
||||
isCreatedByUser: false,
|
||||
createdAt: new Date(NOW - 2_000),
|
||||
}),
|
||||
persistedMessage({
|
||||
messageId: 'regenerated-assistant',
|
||||
parentMessageId: 'user-1',
|
||||
isCreatedByUser: false,
|
||||
createdAt: new Date(NOW),
|
||||
}),
|
||||
]);
|
||||
const resolve = createAgentQueuedTurnResolver({
|
||||
methods,
|
||||
getGenerationJob: async () => null,
|
||||
now: () => NOW,
|
||||
claimBy: 'worker-1',
|
||||
});
|
||||
|
||||
await expect(resolve(envelope(), { idempotencyKey: 'trigger-1' })).resolves.toMatchObject({
|
||||
status: 'ready',
|
||||
parentMessageId: 'regenerated-assistant',
|
||||
});
|
||||
});
|
||||
|
||||
it('dead-letters a legacy admission order that cannot be reconstructed safely', async () => {
|
||||
const { methods, spies } = resolverMethods();
|
||||
(
|
||||
|
|
|
|||
|
|
@ -1064,6 +1064,8 @@ export type TMessage = z.input<typeof tMessageSchema> & {
|
|||
siblingIndex?: number;
|
||||
attachments?: TAttachment[];
|
||||
clientTimestamp?: string;
|
||||
/** Client-only durable branch anchor while this message is an optimistic response. */
|
||||
clientQueueParentMessageId?: string;
|
||||
feedback?: TFeedback;
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue