From 11a56c1f67e5916697a58609347d719dadaf5e4f Mon Sep 17 00:00:00 2001 From: Benjamin Shafii Date: Tue, 3 Feb 2026 12:13:36 -0800 Subject: [PATCH] fix: guard stale mention search results --- .../src/app/components/session/composer.tsx | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/app/src/app/components/session/composer.tsx b/packages/app/src/app/components/session/composer.tsx index e949e39e..ccb102ba 100644 --- a/packages/app/src/app/components/session/composer.tsx +++ b/packages/app/src/app/components/session/composer.tsx @@ -258,6 +258,7 @@ export default function Composer(props: ComposerProps) { let editorRef: HTMLDivElement | undefined; let fileInputRef: HTMLInputElement | undefined; let variantPickerRef: HTMLDivElement | undefined; + let mentionSearchRun = 0; let suppressPromptSync = false; const [commandIndex, setCommandIndex] = createSignal(0); const [mentionIndex, setMentionIndex] = createSignal(0); @@ -734,21 +735,40 @@ export default function Composer(props: ComposerProps) { }); createEffect(() => { - if (!mentionOpen()) return; + if (!mentionOpen()) { + setSearchResults([]); + setSearchLoading(false); + return; + } const query = mentionQuery().trim(); if (!query) { setSearchResults([]); return; } + const runId = (mentionSearchRun += 1); setSearchLoading(true); const timeout = window.setTimeout(() => { props .searchFiles(query) - .then((results) => setSearchResults(results)) - .catch(() => setSearchResults([])) - .finally(() => setSearchLoading(false)); + .then((results) => { + if (runId !== mentionSearchRun) return; + setSearchResults(results); + }) + .catch(() => { + if (runId !== mentionSearchRun) return; + setSearchResults([]); + }) + .finally(() => { + if (runId !== mentionSearchRun) return; + setSearchLoading(false); + }); }, 150); - onCleanup(() => window.clearTimeout(timeout)); + onCleanup(() => { + window.clearTimeout(timeout); + if (runId === mentionSearchRun) { + setSearchLoading(false); + } + }); }); createEffect(() => {