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): discard stale panel refreshes (#5449)

Ziyang Guoeb4fb68acac1
diff --git a/changelog.d/+note-panel-refresh-generation.bugfix.md b/changelog.d/+note-panel-refresh-generation.bugfix.md
new file mode 100644
index 0000000000..0d6e835292
--- /dev/null
+++ b/changelog.d/+note-panel-refresh-generation.bugfix.md
@@ -0,0 +1 @@
+Prevent stale document and research note-panel requests from overwriting newer refresh results.
diff --git a/src/local_deep_research/web/static/js/components/document_notes.js b/src/local_deep_research/web/static/js/components/document_notes.js
index c8fae58f32..6d51f218c4 100644
--- a/src/local_deep_research/web/static/js/components/document_notes.js
+++ b/src/local_deep_research/web/static/js/components/document_notes.js
@@ -17,6 +17,7 @@
*/
(function() {
let documentId = null;
+ let loadGeneration = 0;
// Component-local wrappers over the shared trio (window.NotesShared) —
// local names kept for shadowing-safety; the bodies live in one place.
@@ -56,11 +57,13 @@
const list = document.getElementById('document-notes-list');
const empty = document.getElementById('document-notes-empty');
if (!list) return;
+ const generation = ++loadGeneration;
try {
const response = await safeFetchWithAuth(`/notes/api/documents/${encodeURIComponent(documentId)}/notes`, {
credentials: 'same-origin'
});
const data = await response.json();
+ if (generation !== loadGeneration) return;
if (!data.success) throw new Error(data.error || 'load failed');
list.replaceChildren();
@@ -70,6 +73,7 @@
list.appendChild(window.NotesShared.renderNoteRow(note));
}
} catch (error) {
+ if (generation !== loadGeneration) return;
SafeLogger.error('Error loading document notes:', error);
if (empty) {
empty.textContent = "Couldn't load notes for this document.";
@@ -97,4 +101,11 @@
} else {
initDocumentNotes();
}
+
+ if (window.__VITEST_TEST__) {
+ window.__documentNotesTest = {
+ loadDocumentNotes,
+ setDocumentId: (value) => { documentId = value; }
+ };
+ }
})();
diff --git a/src/local_deep_research/web/static/js/components/research_notes.js b/src/local_deep_research/web/static/js/components/research_notes.js
index a422ea8715..7ef841a7f2 100644
--- a/src/local_deep_research/web/static/js/components/research_notes.js
+++ b/src/local_deep_research/web/static/js/components/research_notes.js
@@ -18,6 +18,7 @@
// — no external URLs are handled. External URL validation is available
// via URLValidator.isSafeUrl if that ever changes.
let researchId = null;
+ let loadGeneration = 0;
// Cap clipped selections: a note exists to hold a *quote*, not a
// second copy of the report (that's what Save-as-note is for).
@@ -78,11 +79,13 @@
const list = document.getElementById('research-notes-list');
const empty = document.getElementById('research-notes-empty');
if (!list) return;
+ const generation = ++loadGeneration;
try {
const response = await safeFetchWithAuth(`/notes/api/research/${encodeURIComponent(researchId)}/notes`, {
credentials: 'same-origin'
});
const data = await response.json();
+ if (generation !== loadGeneration) return;
if (!data.success) throw new Error(data.error || 'load failed');
list.replaceChildren();
@@ -92,6 +95,7 @@
list.appendChild(window.NotesShared.renderNoteRow(note));
}
} catch (error) {
+ if (generation !== loadGeneration) return;
SafeLogger.error('Error loading research notes:', error);
// Leave the section usable: the buttons still work, and a
// failed list load shouldn't block reading the report.
@@ -198,4 +202,11 @@
} else {
initResearchNotes();
}
+
+ if (window.__VITEST_TEST__) {
+ window.__researchNotesTest = {
+ loadResearchNotes,
+ setResearchId: (value) => { researchId = value; }
+ };
+ }
})();
diff --git a/tests/js/components/note-panel-refresh.test.js b/tests/js/components/note-panel-refresh.test.js
new file mode 100644
index 0000000000..b8a906bb82
--- /dev/null
+++ b/tests/js/components/note-panel-refresh.test.js
@@ -0,0 +1,138 @@
+/**
+ * Regression coverage for out-of-order note-panel refreshes.
+ *
+ * Document and research panels share the same list-loading contract. A
+ * mutation-triggered refresh can finish before an older page-load request;
+ * only the newest request may update the DOM or surface an error.
+ */
+
+let documentHook;
+let researchHook;
+
+beforeAll(async () => {
+ document.body.replaceChildren();
+ window.__VITEST_TEST__ = true;
+ window.NotesShared = {
+ toast: vi.fn(),
+ postJson: vi.fn(),
+ renderNoteRow: (note) => {
+ const row = document.createElement('div');
+ row.dataset.noteId = note.id;
+ row.textContent = note.title;
+ return row;
+ }
+ };
+ window.LDRAnnotationSurface = { init: vi.fn() };
+ globalThis.URLBuilder = {
+ extractResearchIdFromPattern: vi.fn(() => null)
+ };
+ globalThis.SafeLogger = { error: vi.fn() };
+ globalThis.safeFetchWithAuth = vi.fn();
+
+ await import('@js/components/document_notes.js');
+ await import('@js/components/research_notes.js');
+ documentHook = window.__documentNotesTest;
+ researchHook = window.__researchNotesTest;
+});
+
+afterAll(() => {
+ delete window.__VITEST_TEST__;
+ delete window.__documentNotesTest;
+ delete window.__researchNotesTest;
+});
+
+beforeEach(() => {
+ document.body.innerHTML = `
+ <div id="document-notes-list"></div>
+ <p id="document-notes-empty">No document notes.</p>
+ <div id="research-notes-list"></div>
+ <p id="research-notes-empty">No research notes.</p>
+ `;
+ globalThis.safeFetchWithAuth = vi.fn();
+ SafeLogger.error.mockClear();
+ documentHook.setDocumentId('document-1');
+ researchHook.setResearchId('research-1');
+});
+
+function deferredResponse() {
+ let resolve;
+ const promise = new Promise((done) => {
+ resolve = (data) => {
+ done({ json: () => Promise.resolve(data) });
+ };
+ });
+ return { promise, resolve };
+}
+
+const PANELS = [
+ {
+ name: 'document',
+ getHook: () => documentHook,
+ loadMethod: 'loadDocumentNotes',
+ listId: 'document-notes-list',
+ emptyId: 'document-notes-empty'
+ },
+ {
+ name: 'research',
+ getHook: () => researchHook,
+ loadMethod: 'loadResearchNotes',
+ listId: 'research-notes-list',
+ emptyId: 'research-notes-empty'
+ }
+];
+
+describe.each(PANELS)('$name note panel request sequencing', ({
+ getHook,
+ loadMethod,
+ listId,
+ emptyId
+}) => {
+ it('does not let an older success overwrite a newer refresh', async () => {
+ const older = deferredResponse();
+ const newer = deferredResponse();
+ safeFetchWithAuth
+ .mockImplementationOnce(() => older.promise)
+ .mockImplementationOnce(() => newer.promise);
+
+ const olderLoad = getHook()[loadMethod]();
+ const newerLoad = getHook()[loadMethod]();
+
+ newer.resolve({
+ success: true,
+ notes: [{ id: 'fresh', title: 'Fresh note' }]
+ });
+ await newerLoad;
+ older.resolve({
+ success: true,
+ notes: [{ id: 'stale', title: 'Stale note' }]
+ });
+ await olderLoad;
+
+ const list = document.getElementById(listId);
+ expect([...list.children].map((row) => row.dataset.noteId))
+ .toEqual(['fresh']);
+ expect(list.textContent).toBe('Fresh note');
+ });
+
+ it('does not let an older failure replace a newer success state', async () => {
+ const older = deferredResponse();
+ const newer = deferredResponse();
+ safeFetchWithAuth
+ .mockImplementationOnce(() => older.promise)
+ .mockImplementationOnce(() => newer.promise);
+
+ const olderLoad = getHook()[loadMethod]();
+ const newerLoad = getHook()[loadMethod]();
+
+ newer.resolve({
+ success: true,
+ notes: [{ id: 'fresh', title: 'Fresh note' }]
+ });
+ await newerLoad;
+ older.resolve({ success: false, error: 'stale failure' });
+ await olderLoad;
+
+ expect(document.getElementById(emptyId).style.display).toBe('none');
+ expect(SafeLogger.error).not.toHaveBeenCalled();
+ });
+});