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.

perf(logpanel): optimize DOM query overhead during live log insertion and unit tests (#5299)

Sdac5c22a2c93
diff --git a/src/local_deep_research/web/static/js/components/logpanel.js b/src/local_deep_research/web/static/js/components/logpanel.js
index 7f5c089529..f4567e13b8 100644
--- a/src/local_deep_research/web/static/js/components/logpanel.js
+++ b/src/local_deep_research/web/static/js/components/logpanel.js
@@ -41,6 +41,7 @@
// Shape lives in emptyCounts() (utils/log-helpers.js) so the
// state init, batch-load reset, and test beforeEach can't drift.
counts: emptyCounts(),
+ renderedIds: new Set(),
initialized: false, // Track initialization state
connectedResearchId: null, // Track which research we're connected to
currentFilter: 'all', // Track current filter type
@@ -101,6 +102,7 @@
// still loading. The next loadLogsForResearch call will
// recompute from B's DOM. See issue #5151 (Gap 2).
window._logPanelState.counts = emptyCounts();
+ window._logPanelState.renderedIds = new Set();
window._logPanelState._countRequestGen =
(window._logPanelState._countRequestGen || 0) + 1;
if (previousResearchId && previousResearchId !== researchId) {
@@ -681,7 +683,9 @@
* the panel. Explicit unknown types retain their real lowercase value so
* callers never decrement Info for an untracked DEBUG/NOTICE row.
*/
- function pruneToCap(container, cap) {
+ function pruneToCap(container, cap, knownCount) {
+ if (typeof knownCount === 'number' && knownCount <= cap) return [];
+
const entries = container.querySelectorAll('.ldr-console-log-entry');
// A fractional cap still means no more than floor(cap) entries.
// Keep the removal quota integral so the exact-zero stop condition
@@ -711,6 +715,9 @@
entry.remove();
for (const removedEntry of removalGroup) {
removed.push(getRenderedLogType(removedEntry));
+ if (removedEntry.dataset?.logId) {
+ window._logPanelState.renderedIds?.delete(removedEntry.dataset.logId);
+ }
}
stillNeeded -= removalGroup.length;
if (stillNeeded <= 0) break;
@@ -1191,6 +1198,7 @@
element.dataset.logTimeMs = Number.isNaN(timestamp.getTime())
? String(Date.now())
: String(timestamp.getTime());
+ element.dataset.logMessage = message;
}
// Set content
@@ -1211,6 +1219,7 @@
element.dataset.logTimeMs = Number.isNaN(timestamp.getTime())
? String(Date.now())
: String(timestamp.getTime());
+ element.dataset.logMessage = message;
// Create log content
// bearer:disable javascript_lang_dangerous_insert_html
@@ -1277,7 +1286,12 @@
// More robust deduplication: First check by ID if available
if (logEntry.id) {
- const existingEntryById = consoleLogContainer.querySelector(`.ldr-console-log-entry[data-log-id="${logEntry.id}"]`);
+ const maybePresent = window._logPanelState.renderedIds
+ ? window._logPanelState.renderedIds.has(logEntry.id)
+ : true;
+ const existingEntryById = maybePresent
+ ? consoleLogContainer.querySelector(`.ldr-console-log-entry[data-log-id="${logEntry.id}"]`)
+ : null;
if (existingEntryById) {
SafeLogger.log('Skipping duplicate log entry by ID:', logEntry.id);
@@ -1327,7 +1341,7 @@
const start = Math.max(0, existingEntries.length - 10);
for (let i = existingEntries.length - 1; i >= start; i--) {
const entry = existingEntries[i];
- const entryMessage = entry.querySelector('.ldr-log-message')?.textContent;
+ const entryMessage = entry.dataset.logMessage || entry.querySelector('.ldr-log-message')?.textContent;
const entryType = entry.dataset.logType;
// If message and type match, consider it a duplicate
@@ -1370,12 +1384,28 @@
// flex-direction: column-reverse, so the newest entry renders at
// the visual top while keyboard/DOM traversal stays chronological.
const newTime = Number(element.dataset.logTimeMs || Date.now());
- const entries = consoleLogContainer.querySelectorAll('.ldr-console-log-entry');
- const nextNewerEntry = Array.from(entries).find(entry => {
- const entryTime = Number(entry.dataset.logTimeMs || 0);
- return entryTime > newTime;
- });
- consoleLogContainer.insertBefore(element, nextNewerEntry || null);
+ let nextNewerEntry = null;
+ const len = existingEntries ? existingEntries.length : 0;
+ if (len > 0) {
+ const lastEntry = existingEntries[len - 1];
+ const lastTime = Number(lastEntry.dataset.logTimeMs || 0);
+ if (newTime < lastTime) {
+ // Out of order: scan backwards from the newest end
+ for (let i = len - 1; i >= 0; i--) {
+ const entry = existingEntries[i];
+ const entryTime = Number(entry.dataset.logTimeMs || 0);
+ if (entryTime > newTime) {
+ nextNewerEntry = entry;
+ } else {
+ break;
+ }
+ }
+ }
+ }
+ consoleLogContainer.insertBefore(element, nextNewerEntry);
+ if (logEntry.id) {
+ window._logPanelState.renderedIds?.add(logEntry.id);
+ }
}
// Account for the inserted row before pruning. If the new row itself
@@ -1399,7 +1429,8 @@
// preserves an expanded "Load older" window up to the 5000-row cap.
const renderCap =
window._logPanelState.renderedLimit ?? MAX_LOG_ENTRIES;
- const removed = pruneToCap(consoleLogContainer, renderCap);
+ const totalCount = existingEntries.length + 1;
+ const removed = pruneToCap(consoleLogContainer, renderCap, totalCount);
if (removed.length > 0) {
for (const prunedType of removed) {
if (Object.prototype.hasOwnProperty.call(
@@ -1668,6 +1699,7 @@
const logContent = document.getElementById('console-log-container');
if (!logContent) return;
const counts = emptyCounts();
+ const renderedIds = new Set();
let total = 0;
logContent.querySelectorAll('.ldr-console-log-entry').forEach((entry) => {
const t = (entry.dataset.logType || 'info').toLowerCase();
@@ -1682,7 +1714,11 @@
if (Object.prototype.hasOwnProperty.call(counts, t)) {
counts[t]++;
}
+ if (entry.dataset.logId) {
+ renderedIds.add(entry.dataset.logId);
+ }
});
+ window._logPanelState.renderedIds = renderedIds;
// After the bulk-merge dedup in addLogEntryToPanel, the live DOM
// can hold fewer nodes than the server returned (routine info
// repeats are collapsed into (N×) badges). Floor the reported
diff --git a/tests/js/components/logpanel.test.js b/tests/js/components/logpanel.test.js
index 25d8d0e8c2..030ccdb417 100644
--- a/tests/js/components/logpanel.test.js
+++ b/tests/js/components/logpanel.test.js
@@ -78,6 +78,7 @@ beforeEach(() => {
window._logPanelState.autoscroll = true;
// Force re-binding of click handlers in tests that call initialize();
// tests that only exercise loadLogs/addLog don't rely on this.
+ window._logPanelState.renderedIds = new Set();
window._logPanelState.initialized = false;
window._logPanelState.connectedResearchId = null;
window._logPanelState.totalLogs = null;
@@ -2614,4 +2615,33 @@ describe('addLog — live pruning integration', () => {
expect(window._logPanelState.counts.error).toBe(1);
expect(getBadge('all')).toBe(501);
});
+
+ it('bypasses querySelectorAll in pruneToCap when knownCount <= cap', () => {
+ const container = document.createElement('div');
+ const entry = makeLiveEntry('entry-1', 'info');
+ container.appendChild(entry);
+
+ const querySpy = vi.spyOn(container, 'querySelectorAll');
+ const removed = window.logPanel._pruneToCap(container, 5, 1);
+
+ expect(removed).toEqual([]);
+ expect(querySpy).not.toHaveBeenCalled();
+ });
+
+ it('queries DOM and prunes in pruneToCap when knownCount > cap or omitted', () => {
+ const container = document.createElement('div');
+ for (let i = 0; i < 3; i++) {
+ container.appendChild(makeLiveEntry(`entry-${i}`, 'info'));
+ }
+
+ const removed = window.logPanel._pruneToCap(container, 2, 3);
+ expect(removed).toEqual(['info']);
+ expect(container.querySelectorAll('.ldr-console-log-entry')).toHaveLength(2);
+ });
+
+ it('resets renderedIds when switching research', () => {
+ window._logPanelState.renderedIds.add('old-log-id');
+ window.logPanel.initialize('new-research-id');
+ expect(window._logPanelState.renderedIds.has('old-log-id')).toBe(false);
+ });
});
diff --git a/tests/utilities/test_close_base_llm.py b/tests/utilities/test_close_base_llm.py
index ca9a561d41..2f08b27b57 100644
--- a/tests/utilities/test_close_base_llm.py
+++ b/tests/utilities/test_close_base_llm.py
@@ -11,7 +11,11 @@ FDs in the issue's lsof dump.
import asyncio
import gc
import os
-import resource
+
+try:
+ import resource
+except ImportError:
+ resource = None
import sys
from unittest.mock import Mock
@@ -26,11 +30,17 @@ def _open_fd_count() -> int:
Inlined here to avoid coupling these tests to a private helper in
an unrelated production module. On Linux uses ``/proc/self/fd``
- (fast); on macOS falls back to scanning ``RLIMIT_NOFILE``.
+ (fast); on macOS falls back to scanning ``RLIMIT_NOFILE``. On non-POSIX
+ platforms (e.g. Windows) where resource is unavailable, returning 0
+ makes calls a no-op for non-skipped test callers.
"""
try:
return len(os.listdir("/proc/self/fd"))
except (FileNotFoundError, OSError):
+ if resource is None:
+ # Non-POSIX fallback (Windows): returning 0 makes FD assertions
+ # no-ops for any non-skipped callers.
+ return 0
soft_limit = resource.getrlimit(resource.RLIMIT_NOFILE)[0]
count = 0
for fd in range(soft_limit):