From fd0ea884e014a1b0549964e0e1d0da2c18b19aa0 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Thu, 12 Nov 2026 07:20:10 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B6=20fix:=20Close=20Two=20Stream=20Ra?= =?UTF-8?q?ces=20That=20Untraced=20Mock=20E2E=20Attempts=20Expose=20(#1807?= =?UTF-8?q?5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ๐Ÿ‘๏ธ 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. --- .../scenarios/subagent-late-files.spec.ts | 17 +++++++- .../__tests__/useConversationSeen.spec.tsx | 41 +++++++++++++++---- .../src/react/messages/useConversationSeen.ts | 8 ++++ 3 files changed, 57 insertions(+), 9 deletions(-) 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;