From 50f8dd972020b09673d3cfed73e27f35ff4cd054 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Thu, 12 Nov 2026 07:20:45 -0500 Subject: [PATCH] =?UTF-8?q?=E2=8F=B3=20fix:=20Stop=20the=20Settings=20Time?= =?UTF-8?q?r=20From=20Expiring=20a=20URL=20Auto-Submit=20That=20Waits=20fo?= =?UTF-8?q?r=20the=20Composer=20(#18072)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ๐Ÿ›‹ fix: Preserve Queued URL Submissions Until Composer Release Since #18060 a URL auto-submit on a new chat waits until the composer has dropped the run of the conversation it just left. That wait returned from `processSubmission` before the settings timer was cleared. When the URL also carried settings (an agent, a model) that had armed the three-second settings timer, the timer kept running after the settings had applied, then marked them failed and consumed `submit=true`. The composer going idle later found the submission already handled, so nothing was sent. Reaching `processSubmission` means the requested settings are in place, so the settings timer now ends there, and the composer wait follows it the same way the history wait does. The timer also checks its own premise when it fires: settings that have applied by then continue to the submission instead of being reported as failed. Settings that never apply still time out while the composer is busy, and leaving the route during the wait still cancels the request. * ๐Ÿงช test: Pin Each Half of the URL Settings Wait Against Its Own Failure The fix holds in two places, and the cases added with it pass with either one alone. Each now has a case that fails without it: - Clearing the settings timer once the settings are ready: settings that applied and were then replaced during the composer wait get a full wait of their own, and still time out with nothing sent. - The timer reading the settings again when it fires: a timer that elapses between the commit that applied the settings and that commit's effects sends once, with the composer idle or busy, instead of reporting a failure. An existing conversation that receives a URL submission during its own run is also pinned to submit immediately, which is the behaviour #18060 kept for it. The hook documents what the settings timer bounds and why it ends where it does. --------- Co-authored-by: Lia --- .../__tests__/ChatForm.queryParams.spec.tsx | 133 +++++++++++++++++- client/src/hooks/Input/useQueryParams.ts | 29 +++- 2 files changed, 158 insertions(+), 4 deletions(-) diff --git a/client/src/components/Chat/Input/__tests__/ChatForm.queryParams.spec.tsx b/client/src/components/Chat/Input/__tests__/ChatForm.queryParams.spec.tsx index 329c6e944f..4795aa986d 100644 --- a/client/src/components/Chat/Input/__tests__/ChatForm.queryParams.spec.tsx +++ b/client/src/components/Chat/Input/__tests__/ChatForm.queryParams.spec.tsx @@ -1,4 +1,4 @@ -import React, { useMemo, useState } from 'react'; +import React, { useLayoutEffect, useMemo, useState } from 'react'; import { RecoilRoot } from 'recoil'; import { DndProvider } from 'react-dnd'; import { useForm } from 'react-hook-form'; @@ -34,6 +34,13 @@ const initialConversation = { title: 'New Chat', } as TConversation; +const agentConversation = { + ...initialConversation, + endpoint: EModelEndpoint.agents, + agent_id: 'agent_test', + model: undefined, +} as TConversation; + const ask = jest.fn(); const newConversation = jest.fn(); const stopGeneration = jest.fn(); @@ -55,6 +62,7 @@ function Harness({ onAsk = ask, refusesWhileSubmitting = false, speechSettingsInitialized = false, + onCommit, }: { conversation: TConversation; routePending?: boolean; @@ -63,7 +71,10 @@ function Harness({ /** Mirrors the real `ask`, which reads `isSubmitting` from the render it was created in. */ refusesWhileSubmitting?: boolean; speechSettingsInitialized?: boolean; + /** Runs in the commit phase, ahead of the passive effects of the same commit. */ + onCommit?: () => void; }) { + useLayoutEffect(() => onCommit?.()); const [files, setFiles] = useAtom(filesFamily(0)); const [isSubmitting] = useAtom(isSubmittingFamily(0)); const [, setFilesLoading] = useState(false); @@ -349,6 +360,126 @@ describe('ChatForm URL submission', () => { expect(view.getLocation().search).toBe(''); }); + it.each([500, 3500])( + 'keeps applied URL settings pending until composer release after %i ms', + async (releaseDelay) => { + const view = mountComposer(initialConversation, { liveRun: true }); + await act(async () => jest.advanceTimersByTime(100)); + view.rerender(); + await act(async () => jest.advanceTimersByTime(releaseDelay)); + expect(screen.queryByText(/Chat settings could not be applied/)).not.toBeInTheDocument(); + expect(screen.getByTestId('text-input')).toHaveValue('hi'); + expect(view.getLocation().search).toContain('submit=true'); + expect(ask).not.toHaveBeenCalled(); + + await act(async () => view.setSubmitting(false)); + expect(ask).toHaveBeenCalledTimes(1); + expect(ask).toHaveBeenCalledWith(expect.objectContaining({ text: 'hi' }), expect.anything()); + expect(view.getLocation().search).toBe(''); + await act(async () => jest.advanceTimersByTime(4000)); + expect(ask).toHaveBeenCalledTimes(1); + }, + ); + + it('still expires unapplied URL settings while the composer is busy', async () => { + const view = mountComposer(initialConversation, { liveRun: true }); + await act(async () => jest.advanceTimersByTime(100)); + await act(async () => jest.advanceTimersByTime(3000)); + expect(screen.getByText(/Chat settings could not be applied/)).toBeInTheDocument(); + expect(screen.getByTestId('text-input')).toHaveValue('hi'); + expect(view.getLocation().search).toBe(''); + await act(async () => view.setSubmitting(false)); + expect(ask).not.toHaveBeenCalled(); + }); + + it('restarts the settings wait when applied URL settings are replaced before composer release', async () => { + const view = mountComposer(initialConversation, { liveRun: true }); + await act(async () => jest.advanceTimersByTime(100)); + view.rerender(); + await act(async () => jest.advanceTimersByTime(1000)); + view.rerender(); + await act(async () => jest.advanceTimersByTime(2999)); + expect(screen.queryByText(/Chat settings could not be applied/)).not.toBeInTheDocument(); + expect(view.getLocation().search).toContain('submit=true'); + await act(async () => jest.advanceTimersByTime(1)); + expect(screen.getByText(/Chat settings could not be applied/)).toBeInTheDocument(); + expect(screen.getByTestId('text-input')).toHaveValue('hi'); + expect(view.getLocation().search).toBe(''); + await act(async () => view.setSubmitting(false)); + expect(ask).not.toHaveBeenCalled(); + }); + + it.each([false, true])( + 'sends once when the settings wait elapses before the effects of the commit that applied them (busy composer: %p)', + async (liveRun) => { + const view = mountComposer(initialConversation, { liveRun }); + await act(async () => jest.advanceTimersByTime(100)); + await act(async () => jest.advanceTimersByTime(2900)); + expect(ask).not.toHaveBeenCalled(); + let elapsed = false; + const elapseSettingsWait = () => { + if (elapsed) { + return; + } + elapsed = true; + jest.advanceTimersByTime(100); + }; + await act(async () => { + view.rerender( + , + ); + }); + expect(elapsed).toBe(true); + expect(screen.queryByText(/Chat settings could not be applied/)).not.toBeInTheDocument(); + if (liveRun) { + expect(ask).not.toHaveBeenCalled(); + expect(view.getLocation().search).toContain('submit=true'); + await act(async () => view.setSubmitting(false)); + } + expect(ask).toHaveBeenCalledTimes(1); + expect(ask).toHaveBeenCalledWith(expect.objectContaining({ text: 'hi' }), expect.anything()); + expect(view.getLocation().search).toBe(''); + await act(async () => jest.advanceTimersByTime(4000)); + expect(ask).toHaveBeenCalledTimes(1); + }, + ); + + it('submits to an existing conversation during its own run without waiting for the composer', async () => { + const view = mountComposer( + { ...agentConversation, conversationId: 'existing-live-chat' }, + { liveRun: true }, + ); + await act(async () => jest.advanceTimersByTime(100)); + expect(ask).toHaveBeenCalledTimes(1); + expect(ask).toHaveBeenCalledWith(expect.objectContaining({ text: 'hi' }), expect.anything()); + expect(view.getLocation().search).toBe(''); + await act(async () => view.setSubmitting(false)); + await act(async () => jest.advanceTimersByTime(4000)); + expect(ask).toHaveBeenCalledTimes(1); + }); + + it('cancels an applied URL submission if its route changes before composer release', async () => { + const view = mountComposer(initialConversation, { liveRun: true }); + await act(async () => jest.advanceTimersByTime(100)); + view.rerender(); + await act(async () => jest.advanceTimersByTime(3500)); + await act(async () => { + view.navigate('/c/other-chat'); + view.rerender( + , + ); + }); + await act(async () => view.setSubmitting(false)); + await act(async () => jest.advanceTimersByTime(4000)); + expect(ask).not.toHaveBeenCalled(); + expect(view.getLocation().pathname).toBe('/c/other-chat'); + expect(screen.queryByText(/Chat settings could not be applied/)).not.toBeInTheDocument(); + }); + it('processes a new agent URL once after a previous URL request has completed', async () => { const view = mountComposer(initialConversation, { query: 'q=first&submit=true' }); await act(async () => jest.advanceTimersByTime(100)); diff --git a/client/src/hooks/Input/useQueryParams.ts b/client/src/hooks/Input/useQueryParams.ts index 3199891877..69fc0bc26c 100644 --- a/client/src/hooks/Input/useQueryParams.ts +++ b/client/src/hooks/Input/useQueryParams.ts @@ -478,12 +478,15 @@ export default function useQueryParams({ ); }, [methods, ownsRequest]); - /** Consumes an auto-submit once, leaving a refused submission in the composer. */ + /** + * Consumes an auto-submit once, leaving a refused submission in the composer. Callers reach it + * only with the requested settings in place, so the settings wait ends here even when the send + * itself still waits for the composer or the history. + */ const processSubmission = useCallback(() => { if ( routePendingRef.current || !targetReadyRef.current || - awaitsComposerRelease() || submissionHandledRef.current || !pendingSubmitRef.current || !promptTextRef.current || @@ -497,6 +500,9 @@ export default function useQueryParams({ clearTimeout(settingsTimeoutRef.current); settingsTimeoutRef.current = null; } + if (awaitsComposerRelease()) { + return; + } awaitingHistoryRef.current = isHistoryPending(); if (awaitingHistoryRef.current) { @@ -556,6 +562,11 @@ export default function useQueryParams({ settlePrompt, ]); + /** + * Bounds the wait for the requested settings, never the wait for the composer. The timer can + * fire between the commit that applied them and that commit's effects, so it reads them again + * before reporting a failure. + */ const armSettingsTimeout = useCallback(() => { if (settingsTimeoutRef.current || routePendingRef.current || !targetReadyRef.current) return; const requestId = requestIdRef.current; @@ -570,6 +581,10 @@ export default function useQueryParams({ return; } if (!submissionHandledRef.current && pendingSubmitRef.current) { + if (areSettingsApplied()) { + processSubmission(); + return; + } restoreUrlPrompt(); pendingSubmitRef.current = false; submissionHandledRef.current = true; @@ -579,7 +594,15 @@ export default function useQueryParams({ clearConsumedSearchParams(); } }, MAX_SETTINGS_WAIT_MS); - }, [ownsRequest, cancelRequest, restoreUrlPrompt, settlePrompt, clearConsumedSearchParams]); + }, [ + ownsRequest, + cancelRequest, + areSettingsApplied, + processSubmission, + restoreUrlPrompt, + settlePrompt, + clearConsumedSearchParams, + ]); /** Observes the history the composer sends from, so a retry runs after the * render that derives the latest message from it, not during the cache write. */