Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(search): invalidate stale unified results (#5440)

Ziyang Guob17f599e5ca2
diff --git a/changelog.d/+unified-search-input-invalidation.bugfix.md b/changelog.d/+unified-search-input-invalidation.bugfix.md
new file mode 100644
index 0000000000..9392a74133
--- /dev/null
+++ b/changelog.d/+unified-search-input-invalidation.bugfix.md
@@ -0,0 +1 @@
+Prevent an older Unified Search response from rendering stale clickable results after the query changes.
diff --git a/src/local_deep_research/web/static/js/pages/unified_search.js b/src/local_deep_research/web/static/js/pages/unified_search.js
index ff43d82eb6..0022906dd6 100644
--- a/src/local_deep_research/web/static/js/pages/unified_search.js
+++ b/src/local_deep_research/web/static/js/pages/unified_search.js
@@ -130,6 +130,24 @@ function setupUnifiedSearchListeners() {
if (unifiedSearchInput) {
unifiedSearchInput.addEventListener('input', function() {
clearTimeout(unifiedSearchDebounceTimer);
+
+ // Invalidate immediately, not when the replacement debounce
+ // expires. Otherwise a response for the previous input can render
+ // stale clickable results during this 300-500 ms window.
+ if (unifiedSearchAbortController) {
+ unifiedSearchAbortController.abort();
+ unifiedSearchAbortController = null;
+ }
+ unifiedSearchRunId += 1;
+ clearUnifiedSearchNotice();
+
+ const query = unifiedSearchInput.value.trim();
+ if (query.length < window.SemanticSearch.MIN_QUERY_LENGTH) {
+ showUnifiedSearchIdleState();
+ return;
+ }
+ showUnifiedSearchLoading('Searching...');
+
unifiedSearchDebounceTimer = setTimeout(
() => runUnifiedSearch(),
unifiedSearchMode === 'text' ? 300 : 500
@@ -533,6 +551,7 @@ function renderUnifiedSearchResults(results) {
if (typeof window !== 'undefined' && window.__VITEST_TEST__) {
window.__unifiedSearchTest = {
SEARCH_MODE_REGISTRY,
+ setupUnifiedSearchListeners,
runUnifiedSearch,
runKeywordMode,
runSemanticMode,
diff --git a/tests/js/pages/unified-search.test.js b/tests/js/pages/unified-search.test.js
index 9a02c0dc20..0e9f99c068 100644
--- a/tests/js/pages/unified-search.test.js
+++ b/tests/js/pages/unified-search.test.js
@@ -103,6 +103,100 @@ describe('mode registry drives the selector UI', () => {
});
});
+describe('input invalidation', () => {
+ it('aborts an in-flight search before the replacement debounce fires', async () => {
+ vi.useFakeTimers();
+ let resolveOld;
+ const oldResponse = new Promise((resolve) => {
+ resolveOld = resolve;
+ });
+
+ try {
+ const dom = makeDom();
+ hook.setDomRefs(dom);
+ hook.setUnifiedSearchMode('text');
+ hook.setupUnifiedSearchListeners();
+
+ globalThis.safeFetch = vi.fn((_url, options) =>
+ Promise.resolve({
+ json: () => oldResponse,
+ signal: options.signal,
+ })
+ );
+
+ dom.input.value = 'old query';
+ const oldRun = hook.runUnifiedSearch();
+ await Promise.resolve();
+ await Promise.resolve();
+
+ const oldSignal = globalThis.safeFetch.mock.calls[0][1].signal;
+ dom.input.value = 'new query';
+ dom.input.dispatchEvent(new Event('input'));
+ const abortedImmediately = oldSignal.aborted;
+
+ resolveOld({
+ success: true,
+ results: [{
+ id: 'old',
+ title: 'Old result',
+ content_preview: 'stale',
+ url: '/notes/old',
+ source_type: 'note',
+ }],
+ });
+ await oldRun;
+
+ expect(abortedImmediately).toBe(true);
+ expect(dom.results.textContent).not.toContain('Old result');
+ // The replacement request remains debounced.
+ expect(globalThis.safeFetch).toHaveBeenCalledTimes(1);
+ await vi.advanceTimersByTimeAsync(300);
+ expect(globalThis.safeFetch).toHaveBeenCalledTimes(2);
+ expect(globalThis.safeFetch.mock.calls[1][0]).toContain(
+ 'q=new+query'
+ );
+ } finally {
+ vi.clearAllTimers();
+ vi.useRealTimers();
+ }
+ });
+
+ it('clears results when the query shrinks below the minimum length', async () => {
+ const dom = makeDom();
+ hook.setDomRefs(dom);
+ hook.setUnifiedSearchMode('text');
+ hook.setupUnifiedSearchListeners();
+
+ globalThis.safeFetch = vi.fn(() =>
+ Promise.resolve({
+ json: () => Promise.resolve({
+ success: true,
+ results: [{
+ id: 'rendered',
+ title: 'Rendered result',
+ content_preview: 'current',
+ url: '/notes/rendered',
+ source_type: 'note',
+ }],
+ }),
+ })
+ );
+
+ dom.input.value = 'current query';
+ await hook.runUnifiedSearch();
+ expect(dom.results.textContent).toContain('Rendered result');
+
+ dom.input.value = '';
+ dom.input.dispatchEvent(new Event('input'));
+
+ expect(dom.results.childElementCount).toBe(0);
+ expect(dom.results.style.display).toBe('none');
+ expect(dom.empty.style.display).toBe('block');
+ expect(dom.empty.textContent).toContain('Search everything');
+ expect(globalThis.safeFetch).toHaveBeenCalledTimes(1);
+ });
+});
+
describe('hybrid mode — leg degradation', () => {
it('shows the error state (with retry) when BOTH legs fail', async () => {
const dom = makeDom();