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(notes): invalidate stale wiki-link results (#5451)

Ziyang Guo13af59506bec
diff --git a/changelog.d/+wikilink-input-invalidation.bugfix.md b/changelog.d/+wikilink-input-invalidation.bugfix.md
new file mode 100644
index 0000000000..e3d6c19511
--- /dev/null
+++ b/changelog.d/+wikilink-input-invalidation.bugfix.md
@@ -0,0 +1 @@
+Invalidate stale wiki-link autocomplete requests and results immediately when the query changes.
diff --git a/src/local_deep_research/web/static/js/pages/note-detail.js b/src/local_deep_research/web/static/js/pages/note-detail.js
index 7d38bf3637..32364142ad 100644
--- a/src/local_deep_research/web/static/js/pages/note-detail.js
+++ b/src/local_deep_research/web/static/js/pages/note-detail.js
@@ -4059,26 +4059,24 @@ function handleWikiLinkInput(textarea) {
showWikiLinkDropdown(textarea);
+ // Invalidate the previous query immediately. Waiting for the replacement
+ // debounce to expire leaves old results selectable and lets an older
+ // response render against the new input for another 300 ms.
+ wikiLinkResults = [];
+ wikiLinkSelectedIndex = 0;
+ clearTimeout(wikiLinkSearchTimeout);
+ if (wikiLinkSearchController) {
+ wikiLinkSearchController.abort();
+ wikiLinkSearchController = null;
+ }
+
if (query.length === 0) {
- // Reset all search state alongside the placeholder. Stale results
- // from a previous query must not survive here: Enter/Tab gate only
- // on wikiLinkResults.length, so leftovers would silently insert an
- // old suggestion while the dropdown visibly shows the placeholder.
- // Also cancel the pending debounce and any in-flight fetch so a
- // search for the just-deleted query can't re-render over it.
- wikiLinkResults = [];
- wikiLinkSelectedIndex = 0;
- clearTimeout(wikiLinkSearchTimeout);
- if (wikiLinkSearchController) {
- wikiLinkSearchController.abort();
- wikiLinkSearchController = null;
- }
renderWikiLinkPlaceholder('Type to search notes...');
return;
}
// Debounced search
- clearTimeout(wikiLinkSearchTimeout);
+ renderWikiLinkPlaceholder('Searching...');
wikiLinkSearchTimeout = setTimeout(
() => searchNotesForLinking(query),
300
@@ -4139,6 +4137,10 @@ async function searchNotesForLinking(query) {
}
} catch (error) {
if (error.name === 'AbortError') return;
+ if (wikiLinkSearchController !== controller) return;
+ wikiLinkResults = [];
+ wikiLinkSelectedIndex = 0;
+ renderWikiLinkPlaceholder('Could not load suggestions');
SafeLogger.error('Wiki-link search error:', error);
} finally {
if (wikiLinkSearchController === controller) {
diff --git a/tests/js/pages/note-detail.test.js b/tests/js/pages/note-detail.test.js
index b0c037cbed..297692ad1a 100644
--- a/tests/js/pages/note-detail.test.js
+++ b/tests/js/pages/note-detail.test.js
@@ -26,6 +26,7 @@ beforeAll(async () => {
globalThis.safeFetch = vi.fn(() =>
Promise.resolve({ json: () => Promise.resolve({ success: true, notes: [] }) })
);
+ globalThis.escapeHtml = (value) => String(value ?? '');
await import('@js/pages/note-detail.js');
hook = window.__noteDetailTest;
@@ -149,4 +150,83 @@ describe('searchNotesForLinking — stale-response last-writer-wins', () => {
await flush();
expect(hook.getWikiLinkResults()).toEqual([]);
});
+
+ it('invalidates an in-flight search immediately on non-empty input changes', async () => {
+ vi.useFakeTimers();
+ try {
+ let resolveOld;
+ const signals = [];
+ globalThis.safeFetch = vi
+ .fn()
+ .mockImplementationOnce((url, opts) => {
+ signals.push(opts.signal);
+ return new Promise((resolve) => { resolveOld = resolve; });
+ })
+ .mockImplementationOnce((url, opts) => {
+ signals.push(opts.signal);
+ return Promise.resolve({
+ json: () => Promise.resolve({
+ success: true,
+ notes: [{ id: 'new', title: 'New result' }],
+ }),
+ });
+ });
+
+ const oldSearch = hook.searchNotesForLinking('old');
+ hook.setWikiLinkState({
+ active: true,
+ results: [{ id: 'old', title: 'Old result' }],
+ index: 0,
+ start: 0,
+ end: 5,
+ });
+ document.body.innerHTML =
+ '<textarea id="note-content">[[new</textarea>';
+ const textarea = document.getElementById('note-content');
+ textarea.setSelectionRange(5, 5);
+
+ hook.handleWikiLinkInput(textarea);
+
+ // Input state changes immediately; replacement network work stays
+ // debounced. The old response cannot remain selectable meanwhile.
+ expect(signals[0].aborted).toBe(true);
+ expect(hook.getWikiLinkResults()).toEqual([]);
+ expect(globalThis.safeFetch).toHaveBeenCalledTimes(1);
+
+ resolveOld({
+ json: () => Promise.resolve({
+ success: true,
+ notes: [{ id: 'old', title: 'Old result' }],
+ }),
+ });
+ await oldSearch;
+ expect(hook.getWikiLinkResults()).toEqual([]);
+
+ await vi.advanceTimersByTimeAsync(299);
+ expect(globalThis.safeFetch).toHaveBeenCalledTimes(1);
+ await vi.advanceTimersByTimeAsync(1);
+ expect(globalThis.safeFetch).toHaveBeenCalledTimes(2);
+ } finally {
+ vi.clearAllTimers();
+ vi.useRealTimers();
+ }
+ });
+
+ it('clears selectable results after a current non-abort failure', async () => {
+ hook.setWikiLinkState({
+ active: true,
+ results: [{ id: 'old', title: 'Old result' }],
+ index: 0,
+ start: 0,
+ end: 5,
+ });
+ globalThis.safeFetch = vi.fn(() =>
+ Promise.reject(new Error('network unavailable'))
+ );
+
+ await expect(hook.searchNotesForLinking('new'))
+ .resolves.toBeUndefined();
+
+ expect(hook.getWikiLinkResults()).toEqual([]);
+ });
});