mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-21 15:33:45 +00:00
🧶 fix: Close Two Stream Races That Untraced Mock E2E Attempts Expose (#18075)
* 👁️ fix: Hold the Seen Check's History Refetch While a Run Streams The seen check revalidates a conversation's history when the list reports a reply that no fetch has shown yet, and it asked for that refetch whenever the messages query was idle, including under a run the pane is attached to. A resumed run writes its response into the cache from the stream's sync. A refetch that starts after that sync returns the stored row for the same message, and the cache takes it because both lists are the same length, so the live response is replaced by what was persisted when the run began until the stream's next event rewrites it. A manual compaction resumed after a reload shows it for as long as the summarizer takes: its stored row is an empty summary part with no running marker, so "Summarizing..." disappears and nothing stands in for it. The refetch now waits while a run is in flight. The check already re-runs when the run ends, which is when revalidating is safe. * 🧪 test: Wait for the Late Subagent File's Stream Before Reading Its Turn as Over Stop is withheld between a turn's admission and its stream attaching: no generation epoch exists yet for it to stop. The scenario read Stop's absence right after the POST as the end of the turn, then asserted without waiting that its route had injected the late file into that stream. When the check ran inside that interval the stream had not been requested yet, so the assertion failed on a turn that went on to deliver the file. It now waits for the reply only the stream carries, then for Stop to go.
This commit is contained in:
parent
4b024ffd21
commit
fd0ea884e0
3 changed files with 57 additions and 9 deletions
|
|
@ -8,7 +8,14 @@ import {
|
|||
deleteConversations,
|
||||
deleteMessagesByConversation,
|
||||
} from '../db';
|
||||
import { MOCK_ENDPOINTS, selectMockEndpoint, sendMessage } from '../helpers';
|
||||
import {
|
||||
MOCK_ENDPOINTS,
|
||||
replyText,
|
||||
replyPrompt,
|
||||
sendMessage,
|
||||
messagesView,
|
||||
selectMockEndpoint,
|
||||
} from '../helpers';
|
||||
import { getE2EUser } from '../../../setup/user';
|
||||
import { expect, test } from '../fixtures';
|
||||
|
||||
|
|
@ -173,8 +180,14 @@ async function deliverLateFile(
|
|||
});
|
||||
|
||||
await selectMockEndpoint(page, MOCK_ENDPOINTS[0]);
|
||||
const response = await sendMessage(page, `E2E_REPLY:late-file-${suffix}`);
|
||||
const label = `late-file-${suffix}`;
|
||||
const response = await sendMessage(page, replyPrompt(label));
|
||||
expect(response.ok()).toBeTruthy();
|
||||
/** Stop is withheld from admission until the stream attaches, so its absence right after
|
||||
* the POST is not the end of the turn. The reply only arrives on that stream. */
|
||||
await expect(messagesView(page).getByText(replyText(label), { exact: true })).toBeVisible({
|
||||
timeout: 60_000,
|
||||
});
|
||||
await expect(page.getByRole('button', { name: 'Stop generating' })).toBeHidden({
|
||||
timeout: 60_000,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -11,14 +11,14 @@ import useConversationSeen from '../useConversationSeen';
|
|||
const CONVO_ID = 'convo-seen';
|
||||
const RESPONDED_AT = '2026-11-03T10:00:00.000Z';
|
||||
|
||||
function setup(reply: SeenReply | undefined) {
|
||||
function setup(reply: SeenReply | undefined, { proven = true, submitting = false } = {}) {
|
||||
const queryClient = new QueryClient();
|
||||
queryClient.setQueryData([QueryKeys.messages, CONVO_ID], []);
|
||||
const markSeen = jest.fn();
|
||||
const consumeFocusSuppression = jest.fn(() => false);
|
||||
const source: ConversationSeenSource = {
|
||||
readReply: (conversationId) => (conversationId === CONVO_ID ? reply : undefined),
|
||||
hasReplyProof: (_conversationId, _stamp, from) => from === 'server',
|
||||
hasReplyProof: (_conversationId, _stamp, from) => proven && from === 'server',
|
||||
markSeen,
|
||||
consumeFocusSuppression,
|
||||
};
|
||||
|
|
@ -26,10 +26,11 @@ function setup(reply: SeenReply | undefined) {
|
|||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
);
|
||||
const view = renderHook(
|
||||
({ id }: { id: string }) => useConversationSeen(source, id, false, undefined, () => true),
|
||||
{ initialProps: { id: CONVO_ID }, wrapper },
|
||||
({ id, isSubmitting }: { id: string; isSubmitting: boolean }) =>
|
||||
useConversationSeen(source, id, isSubmitting, undefined, () => true),
|
||||
{ initialProps: { id: CONVO_ID, isSubmitting: submitting }, wrapper },
|
||||
);
|
||||
return { ...view, markSeen, consumeFocusSuppression };
|
||||
return { ...view, markSeen, consumeFocusSuppression, queryClient };
|
||||
}
|
||||
|
||||
const unseenReply: SeenReply = {
|
||||
|
|
@ -104,11 +105,37 @@ describe('useConversationSeen', () => {
|
|||
act(() => result.current(true));
|
||||
await waitFor(() => expect(markSeen).toHaveBeenCalledTimes(1));
|
||||
|
||||
rerender({ id: 'convo-next' });
|
||||
rerender({ id: CONVO_ID });
|
||||
rerender({ id: 'convo-next', isSubmitting: false });
|
||||
rerender({ id: CONVO_ID, isSubmitting: false });
|
||||
await act(() => new Promise<void>((resolve) => setTimeout(resolve, 50)));
|
||||
|
||||
/** Returning re-arms the attempt, but nothing reported the end in view since. */
|
||||
expect(markSeen).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('revalidates history for a reply no fetch has shown yet', () => {
|
||||
const { result, queryClient } = setup(unseenReply, { proven: false });
|
||||
const invalidate = jest.spyOn(queryClient, 'invalidateQueries');
|
||||
|
||||
act(() => result.current(true));
|
||||
|
||||
expect(invalidate).toHaveBeenCalledWith([QueryKeys.messages, CONVO_ID]);
|
||||
});
|
||||
|
||||
it('leaves history alone under a run in flight and revalidates once it ends', async () => {
|
||||
const { result, rerender, queryClient } = setup(unseenReply, {
|
||||
proven: false,
|
||||
submitting: true,
|
||||
});
|
||||
const invalidate = jest.spyOn(queryClient, 'invalidateQueries');
|
||||
|
||||
act(() => result.current(true));
|
||||
await act(() => new Promise<void>((resolve) => setTimeout(resolve, 50)));
|
||||
expect(invalidate).not.toHaveBeenCalled();
|
||||
|
||||
rerender({ id: CONVO_ID, isSubmitting: false });
|
||||
|
||||
await waitFor(() => expect(invalidate).toHaveBeenCalledWith([QueryKeys.messages, CONVO_ID]));
|
||||
expect(invalidate).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -32,6 +32,8 @@ export default function useConversationSeen(
|
|||
const queryClient = useQueryClient();
|
||||
const { readReply, hasReplyProof, markSeen, consumeFocusSuppression } = source;
|
||||
const isNearBottomRef = useRef(false);
|
||||
const isSubmittingRef = useRef(isSubmitting);
|
||||
isSubmittingRef.current = isSubmitting;
|
||||
/** Read from the deferred revalidation check, which outlives the render that created it. */
|
||||
const measureRef = useRef(measureNearBottom);
|
||||
measureRef.current = measureNearBottom;
|
||||
|
|
@ -186,6 +188,12 @@ export default function useConversationSeen(
|
|||
/* The requested fetch succeeded, but its cache event has not painted yet. */
|
||||
return;
|
||||
}
|
||||
if (isSubmittingRef.current) {
|
||||
/* A run in flight writes the tail of this history itself. A refetch landing under it
|
||||
* replaces the response it is streaming with the stored row, which stays on screen
|
||||
* until the stream's next event. The end of the run re-checks. */
|
||||
return;
|
||||
}
|
||||
requestedMessagesRef.current.set(conversationId, lastResponseAt);
|
||||
void queryClient.invalidateQueries(messagesKey).catch(() => undefined);
|
||||
return;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue