diff --git a/e2e/specs/mock/scenarios/subagent-late-files.spec.ts b/e2e/specs/mock/scenarios/subagent-late-files.spec.ts index 655cbb5cbc..891b08ef53 100644 --- a/e2e/specs/mock/scenarios/subagent-late-files.spec.ts +++ b/e2e/specs/mock/scenarios/subagent-late-files.spec.ts @@ -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, }); diff --git a/packages/chat/src/react/messages/__tests__/useConversationSeen.spec.tsx b/packages/chat/src/react/messages/__tests__/useConversationSeen.spec.tsx index 104ecad87b..c479896e40 100644 --- a/packages/chat/src/react/messages/__tests__/useConversationSeen.spec.tsx +++ b/packages/chat/src/react/messages/__tests__/useConversationSeen.spec.tsx @@ -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) { {children} ); 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((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((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); + }); }); diff --git a/packages/chat/src/react/messages/useConversationSeen.ts b/packages/chat/src/react/messages/useConversationSeen.ts index 2d0b034feb..c2f38071f4 100644 --- a/packages/chat/src/react/messages/useConversationSeen.ts +++ b/packages/chat/src/react/messages/useConversationSeen.ts @@ -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;