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.

98.5 KB
Raw Blame
/**
 * LogPanel Component
 * Handles the display and interaction with the research log panel
 * Used by both progress.js and results.js
 */
(function() {
    // XSS protection for values rendered via innerHTML
    // bearer:disable javascript_lang_manual_html_sanitization
    const escapeHtmlFallback = (str) => String(str || '').replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);
    const escapeHtml = window.escapeHtml || escapeHtmlFallback;

    // Shared log helpers extracted to utils/log-helpers.js for testability
    const {
        bumpCount,
        checkLogVisibility,
        emptyCounts,
        getDisplayLogCategory,
        hashString,
        normalizeMessage,
        normalizeTimestamps,
    } = window.LdrLogHelpers;

    // Maximum number of log entries to keep in DOM to prevent unbounded growth.
    // Seeded from window.LDR_LOG_LIMITS (set in base.html from Python's
    // HISTORY_LOGS_DEFAULT_LIMIT in constants.py) so this DOM cap and the
    // shared pagination default come from one source instead of drifting.
    // Falls back to 500 if the injection is missing (e.g. in a unit-test
    // harness without templates).
    const MAX_LOG_ENTRIES = window.LDR_LOG_LIMITS?.default ?? 500;
    const COLLAPSIBLE_LOG_TYPES = new Set(['info', 'debug']);

    // Shared state for log panel
    window._logPanelState ||= {
        expanded: false,
        queuedLogs: [],
        logCount: 0,
        // Per-category counts derived from entries currently in the DOM.
        // Updated on insert (addLogEntryToPanel) and prune (see the
        // ordered-prune path in addLogEntryToPanel). Used to render the
        // per-filter count badges next to the filter button labels, so
        // users can see at a glance which categories still have entries
        // after the global DOM cap starts flushing the head of the log.
        // 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
        autoscroll: true, // Track whether autoscroll is enabled.
        // Total number of log rows persisted server-side for the connected
        // research. Sourced from /api/research/<id>/log_count and refreshed
        // on every loadLogs call. Drives the "Showing X of Y" badge in the
        // panel header so users can see at a glance when the DOM is showing
        // a truncated slice of a long run (see issue #4878). Null until the
        // first fetch resolves — distinct from 0, which means "research has
        // zero rows".
        totalLogs: null,
        // Number of raw persisted rows returned by the latest /logs request.
        // This, rather than the post-dedup DOM count, decides whether older
        // rows remain available on the server.
        fetchedLogs: null,
        // Currently rendered limit for this research. Starts at MAX_LOG_ENTRIES
        // and bumps to HISTORY_LOGS_HARD_CAP after the user clicks "Load older",
        // so the panel can show up to the hard cap without a full re-init.
        renderedLimit: null,
        // High-water mark of ERROR-category entries seen for the connected
        // research. The header badge's severity signal reads this rather
        // than a live DOM count: the DOM holds only the rendered window
        // (older rows are paged out and the ordered prune can evict rows),
        // so a raw count can silently drop back to 0 and un-red a badge
        // for an error that really happened. Monotonic within a research;
        // reset by resetForResearchSwitch. Null/0 means "no error known".
        errorHighWater: 0,
    };

    /**
     * Reset per-research display state when the connected research
     * changes (or on a fresh mount that supersedes a prior ID set on
     * state). Shared between the early-return "already initialized"
     * branch and the fresh-init branch of initializeLogPanel so the
     * two paths can't drift in which fields they clear.
     *
     * Bumps `_countRequestGen` so any in-flight
     * `fetchAndCacheLogCount` promise for the previous research is
     * treated as stale and refused at the response write site
     * (logpanel.js — fetchAndCacheLogCount). Without this guard a
     * late count response for research A could land after research B
     * has loaded and silently overwrite B's badge with A's total.
     *
     * Tears down the "X of Y" / "Load older" cluster that the
     * previous research painted. The button click handler captures
     * `connectedResearchId` at click time, but the element itself is
     * stale — leaving it in the header would either (a) keep the
     * wrong total visible, or (b) trigger a hard-cap fetch against
     * the new research on a stale click. Drop it now so
     * updateLogCountIndicator repaints it fresh for the new research
     * on the next load.
     *
     * @param {string} researchId - New research ID.
     * @param {string|null} previousResearchId - Research ID set on
     *   state before the call. Used to decide whether the controls
     *   are actually stale (no-op when there is no prior research).
     */
    function resetForResearchSwitch(researchId, previousResearchId) {
        window._logPanelState.connectedResearchId = researchId;
        window._logPanelState.totalLogs = null;
        window._logPanelState.fetchedLogs = null;
        window._logPanelState.renderedLimit = null;
        // Per-category counts belong to the previous research; zero them
        // here so the badges can't keep showing A's totals while B is
        // still loading. The next loadLogsForResearch call will
        // recompute from B's DOM. See issue #5151 (Gap 2).
        window._logPanelState.counts = emptyCounts();
        // The error high-water mark is per-research too — B must not
        // inherit A's red badge.
        window._logPanelState.errorHighWater = 0;
        const renderedIds = new Set();
        document
            .querySelectorAll(
                '#console-log-container .ldr-console-log-entry[data-log-id]'
            )
            .forEach((entry) => {
                if (entry.dataset.logId) {
                    renderedIds.add(entry.dataset.logId);
                }
            });
        window._logPanelState.renderedIds = renderedIds;
        window._logPanelState._countRequestGen =
            (window._logPanelState._countRequestGen || 0) + 1;
        if (previousResearchId && previousResearchId !== researchId) {
            document
                .querySelectorAll('.ldr-log-of-total, .ldr-load-older')
                .forEach((el) => el.remove());
            const panelEl = document.getElementById('log-panel-content') ||
                            document.getElementById('logPanel');
            if (panelEl) {
                delete panelEl.dataset.loading;
            }
        }
    }

    /**
     * Initialize the log panel
     * @param {string} researchId - Optional research ID to load logs for
     */
    function initializeLogPanel(researchId = null) {
        // Check if already initialized
        if (window._logPanelState.initialized) {
            SafeLogger.log('Log panel already initialized, checking if research ID has changed');

            // If we're already connected to this research, do nothing
            if (window._logPanelState.connectedResearchId === researchId) {
                SafeLogger.log('Already connected to research ID:', researchId);
                return;
            }

            // If the research ID has changed, we'll update our connection
            SafeLogger.log('Research ID changed from', window._logPanelState.connectedResearchId, 'to', researchId);
            const previousResearchId = window._logPanelState.connectedResearchId;
            resetForResearchSwitch(researchId, previousResearchId);

            // Reset per-research state for the new research. queuedLogs is
            // cleared because any queued entries belong to the previous
            // research_id and would mis-attribute to the new one. expanded
            // is synced from the DOM (not reset to false) so a panel the
            // user had open for research N stays open for research N+1 —
            // otherwise new socket entries would queue invisibly until the
            // user manually re-toggled.
            window._logPanelState.queuedLogs = [];
            window._logPanelState.currentFilter = 'all';

            const logPanelContentEl = document.getElementById('log-panel-content') ||
                                       document.getElementById('logPanel');
            window._logPanelState.expanded = logPanelContentEl
                ? !logPanelContentEl.classList.contains('collapsed')
                : false;

            // Reset filter buttons visual state
            const filterBtns = document.querySelectorAll('.ldr-log-filter .ldr-filter-buttons button');
            filterBtns.forEach(btn => btn.classList.remove('ldr-selected'));
            const allBtn = Array.from(filterBtns).find(btn => btn.textContent.toLowerCase() === 'all');
            if (allBtn) allBtn.classList.add('ldr-selected');

            // Clear container of the previous research's log entries (they
            // are stale for the new research) and reset the loaded marker
            // so the next expand triggers a fresh fetch. Then bail out:
            // toggle/visibility handlers from the first init still apply
            // and re-running the rest of init would either duplicate
            // handlers or wipe socket entries that have already arrived
            // for the new research.
            const consoleLogContainer = document.getElementById('console-log-container');
            if (consoleLogContainer) {
                consoleLogContainer.innerHTML = '<div class="ldr-empty-log-message">No logs available. Expand panel to load logs.</div>';
                recomputeCountersFromDom();
                updateLogCountIndicator(consoleLogContainer);
            }
            if (logPanelContentEl) {
                delete logPanelContentEl.dataset.loaded;
            }
            return;
        }

        // The DOMContentLoaded path stores the ID before calling initialize,
        // but callers such as progress.js and chat.js can invoke this public
        // method directly. Keep the shared ID and per-research display state
        // correct for both entry points.
        const previousResearchId =
            window._logPanelState.connectedResearchId;
        resetForResearchSwitch(researchId, previousResearchId);

        // Add callback for log download button.
        const downloadButton = document.getElementById('log-download-button');
        if (downloadButton) {
            downloadButton.addEventListener('click', downloadLogs);
        }

        SafeLogger.log('Initializing shared log panel, research ID:', researchId);

        // Check if we're on a research-specific page (progress, results)
        const isResearchPage = window.location.pathname.includes('/progress/') ||
                              window.location.pathname.includes('/results/') ||
                              window.location.pathname.includes('/chat/') ||
                              document.getElementById('research-progress') ||
                              document.getElementById('research-results');

        // Get all log panels on the page (there might be duplicates)
        const logPanels = document.querySelectorAll('.ldr-collapsible-log-panel');

        if (logPanels.length > 1) {
            SafeLogger.warn(`Found ${logPanels.length} log panels, removing duplicates`);

            // Keep only the first one and remove others
            for (let i = 1; i < logPanels.length; i++) {
                SafeLogger.log(`Removing duplicate log panel #${i}`);
                logPanels[i].remove();
            }
        } else if (logPanels.length === 0) {
            SafeLogger.error('No log panel found in the DOM!');
            return;
        }

        // Get log panel elements with both old and new names for compatibility
        let logPanelToggle = document.getElementById('log-panel-toggle');
        let logPanelContent = document.getElementById('log-panel-content');

        // Fallback to the old element IDs if needed
        if (!logPanelToggle) logPanelToggle = document.getElementById('logToggle');
        if (!logPanelContent) logPanelContent = document.getElementById('logPanel');

        if (!logPanelToggle || !logPanelContent) {
            SafeLogger.warn('Log panel elements not found, skipping initialization');
            return;
        }

        // Clear loaded flag so logs are re-fetched for the new research ID
        if (window._logPanelState.initialized) {
            delete logPanelContent.dataset.loaded;
        }

        const autoscrollButton = document.querySelector('#log-autoscroll-button');

        // Handle visibility based on page type
        if (!isResearchPage) {
            SafeLogger.log('Not on a research-specific page, hiding log panel');

            // Hide the log panel on non-research pages
            const panel = logPanelContent.closest('.ldr-collapsible-log-panel');
            if (panel) {
                panel.style.display = 'none';
            } else if (logPanelContent.parentElement) {
                logPanelContent.parentElement.style.display = 'none';
            } else {
                logPanelContent.style.display = 'none';
            }
            return;
        }
        // Ensure log panel is visible on research pages
        SafeLogger.log('On a research page, ensuring log panel is shown');
        const panel = logPanelContent.closest('.ldr-collapsible-log-panel');
        if (panel) {
            panel.style.display = 'flex';
        }

        SafeLogger.log('Log panel elements found, setting up handlers');

        // Mark as initialized to prevent double initialization
        window._logPanelState.initialized = true;

        // Check for CSS issue - if the panel's computed style has display:none, the panel won't be visible
        const computedStyle = window.getComputedStyle(logPanelContent);
        SafeLogger.log('Log panel CSS visibility:', {
            display: computedStyle.display,
            visibility: computedStyle.visibility,
            height: computedStyle.height,
            overflow: computedStyle.overflow
        });

        // Ensure the panel is visible in the DOM
        if (computedStyle.display === 'none') {
            SafeLogger.warn('Log panel has display:none - forcing display:flex');
            logPanelContent.style.display = 'flex';
        }

        // Ensure we have a console log container
        const consoleLogContainer = document.getElementById('console-log-container');
        if (!consoleLogContainer) {
            SafeLogger.error('Console log container not found, logs will not be displayed');
        } else {
            // Add placeholder message
            consoleLogContainer.innerHTML = '<div class="ldr-empty-log-message">No logs available. Expand panel to load logs.</div>';
            recomputeCountersFromDom();
            updateLogCountIndicator(consoleLogContainer);
        }

        // Abort previous event handlers to prevent stacking on re-init
        if (window._logPanelState._handlersAbort) {
            window._logPanelState._handlersAbort.abort();
        }
        const handlersAbort = new AbortController();
        window._logPanelState._handlersAbort = handlersAbort;

        // Set up toggle click handler
        logPanelToggle.addEventListener('click', function() {
            SafeLogger.log('Log panel toggle clicked');

            // Toggle collapsed state
            logPanelContent.classList.toggle('collapsed');
            logPanelToggle.classList.toggle('collapsed');

            const collapsed = logPanelContent.classList.contains('collapsed');
            logPanelToggle.setAttribute('aria-expanded', String(!collapsed));

            const toggleIcon = logPanelToggle.querySelector('.ldr-toggle-icon');
            if (toggleIcon && !collapsed) {
                // Load logs if not already loaded. dataset.loaded is set by
                // loadLogsForResearch only on a successful non-empty fetch,
                // so an earlier empty response does not suppress retries.
                // Read the id live from _logPanelState rather than the closure:
                // on /chat/ pages the panel is first initialized with a null id
                // (the URL carries a session id, not a research id) and the real
                // id only arrives later via window.logPanel.initialize(), whose
                // re-init path updates connectedResearchId without rebinding this
                // handler. Using the stale closure id meant chat pages never
                // loaded historical logs when the panel was expanded.
                const activeResearchId = researchId || window._logPanelState.connectedResearchId;
                if (!logPanelContent.dataset.loaded && activeResearchId) {
                    SafeLogger.log('First expansion of log panel, loading logs');
                    loadLogsForResearch(activeResearchId);
                }

                // Process any queued logs
                if (window._logPanelState.queuedLogs.length > 0) {
                    SafeLogger.log(`Processing ${window._logPanelState.queuedLogs.length} queued logs`);
                    window._logPanelState.queuedLogs.forEach(logEntry => {
                        addLogEntryToPanel(logEntry, false);
                    });
                    window._logPanelState.queuedLogs = [];
                    recomputeCountersFromDom();
                    updateLogCountIndicator(consoleLogContainer);
                }
            }

            // Default to showing the autoscroll button.
            if (autoscrollButton !== null) {
                autoscrollButton.style.display = 'inline';
            }

            const logPanel = document.querySelector('.ldr-collapsible-log-panel');
            const isProgressPage = document.querySelector('#research-progress') !== null;
            if (logPanel !== null) {
                logPanel.classList.toggle('ldr-expanded', !collapsed && isProgressPage);
            }
            if (!collapsed && logPanel !== null && isProgressPage) {
                logPanel.style.height = '';
                // Start with autoscroll on when expanding.
                window._logPanelState.autoscroll = false;
                toggleAutoscroll();
            } else if (logPanel !== null) {
                // Use the default height.
                logPanel.style.height = 'auto';
                // Hide the autoscroll button since it doesn't make
                // sense in this context.
                if (autoscrollButton !== null) {
                    autoscrollButton.style.display = 'none';
                }
            }

            // Track expanded state
            window._logPanelState.expanded = !collapsed;
        }, { signal: handlersAbort.signal });

        if (autoscrollButton) {
            // Set up autoscroll handler for the log panel. When autoscroll is
            // enabled, it will automatically scroll as new logs are added.
            autoscrollButton.addEventListener('click', toggleAutoscroll, { signal: handlersAbort.signal });
        }

        // Set up filter button click handlers
        const filterButtons = document.querySelectorAll('.ldr-log-filter .ldr-filter-buttons button');
        filterButtons.forEach(button => {
            button.addEventListener('click', function() {
                // Prefer the explicit data-filter-type attribute so the
                // click target is decoupled from the button label text
                // (which now includes a live count badge). The fallback
                // must read the label's text node only — pulling the
                // whole textContent would pull the badge text too, e.g.
                // "Errors 0" or "Info 12", which would never match a
                // filter case and would silently fall through the
                // checkLogVisibility default to "show everything".
                const type = this.dataset.filterType ||
                    (this.firstChild &&
                        this.firstChild.textContent.trim().toLowerCase());
                SafeLogger.log(`Filtering logs by type: ${type}`);

                // Update active state
                filterButtons.forEach(btn => btn.classList.remove('ldr-selected'));
                this.classList.add('ldr-selected');

                // Apply filtering
                filterLogsByType(type);
            }, { signal: handlersAbort.signal });
        });

        // Start with panel collapsed and fix initial chevron direction
        logPanelContent.classList.add('collapsed');
        const initialToggleIcon = logPanelToggle.querySelector('.ldr-toggle-icon');
        if (initialToggleIcon) {
            initialToggleIcon.className = 'fas fa-chevron-right ldr-toggle-icon';
        }

        // Initialize the log count
        const logIndicators = document.querySelectorAll('.ldr-log-indicator');
        if (logIndicators.length > 0) {
            // Set count on all indicators. Route through paintLogIndicators
            // rather than writing textContent directly so the severity
            // class/tooltip/ARIA are re-derived here too — otherwise a
            // re-init would repaint a fresh count under a stale red badge.
            const logContainerEl =
                document.getElementById('console-log-container');
            paintLogIndicators('0', logContainerEl, false);

            // Skip the API call when there is no researchId (e.g. on a
            // freshly-loaded /chat/ page before a research has started).
            // URLBuilder.historyLogCount(null) would otherwise produce a
            // /history/log_count/null request that 404s on every load.
            // The fetch is best-effort; on failure the indicator stays at
            // "0" and loadLogsForResearch will refresh the cached total
            // (or degrade gracefully) once it runs.
            if (researchId) {
                fetchAndCacheLogCount(researchId).then((total) => {
                    if (typeof total === 'number') {
                        // The server total counts rows the DOM does not
                        // hold, so the error signal is a lower bound.
                        const domCount = logContainerEl
                            ? logContainerEl.querySelectorAll(
                                  '.ldr-console-log-entry'
                              ).length
                            : 0;
                        paintLogIndicators(
                            formatNumber(total),
                            logContainerEl,
                            domCount < total
                        );
                    }
                });
            }
        } else {
            SafeLogger.warn('No log indicators found for initialization');
        }

        // Check CSS display property of the log panel
        const logPanel = document.querySelector('.ldr-collapsible-log-panel');
        if (logPanel) {
            const panelStyle = window.getComputedStyle(logPanel);
            SafeLogger.log('Log panel CSS display:', panelStyle.display);

            if (panelStyle.display === 'none') {
                SafeLogger.warn('Log panel has CSS display:none - forcing display:flex');
                logPanel.style.display = 'flex';
            }
        }

        // Pre-fetch logs in the background so an opened panel has historical
        // entries ready, and so the API races (empty response in 0-100ms
        // window after research start) self-heal once entries exist.
        // dataset.loaded is set inside loadLogsForResearch only on success;
        // an empty response leaves it unset so a later toggle re-fetches.
        if (researchId && !logPanelContent.dataset.loaded) {
            loadLogsForResearch(researchId);
        }

        // Pre-load logs if hash includes #logs
        // timing comparison on URL hash, not secrets
        // bearer:disable javascript_lang_observable_timing
        if (window.location.hash === '#logs' && researchId) {
            SafeLogger.log('Auto-loading logs due to #logs in URL');
            setTimeout(() => {
                logPanelToggle.click();
            }, 500);
        }

        // DEBUG: Force expand the log panel if URL has debug parameter
        if (window.location.search.includes('debug=logs') || window.location.hash.includes('debug')) {
            SafeLogger.log('DEBUG: Force-expanding log panel');
            setTimeout(() => {
                if (logPanelContent.classList.contains('collapsed')) {
                    logPanelToggle.click();
                }
            }, 800);
        }

        // Register global functions to ensure they work across modules
        window.addConsoleLog = addConsoleLog;
        window.filterLogsByType = filterLogsByType;

        // Add a connector to socket.js
        // Track when we last received this exact message to avoid re-adding within 10 seconds
        const processedMessages = new Map();
        window._socketAddLogEntry = function(logEntry) {
            // Simple message deduplication for socket events
            const message = logEntry.message || logEntry.content || '';
            const messageKey = `${message}-${logEntry.type || 'info'}`;
            const now = Date.now();

            // Check if we've seen this message recently (within 10 seconds)
            if (processedMessages.has(messageKey)) {
                const lastProcessed = processedMessages.get(messageKey);
                const timeDiff = now - lastProcessed;

                if (timeDiff < 10000) { // 10 seconds
                    SafeLogger.log(`Skipping duplicate socket message received within ${timeDiff}ms:`, message);
                    return;
                }
            }

            // Update our tracking
            processedMessages.set(messageKey, now);

            // Clean up old entries (keep map from growing indefinitely)
            if (processedMessages.size > 100) {
                // Remove entries older than 60 seconds
                for (const [key, timestamp] of processedMessages.entries()) {
                    if (now - timestamp > 60000) {
                        processedMessages.delete(key);
                    }
                }
            }

            // Process the log entry
            addLogEntryToPanel(logEntry);
        };

        SafeLogger.log('Log panel initialized');
    }

    /**
     * @brief Toggles autoscroll on or off.
     */
    function toggleAutoscroll() {
        window._logPanelState.autoscroll = !window._logPanelState.autoscroll;

        const autoscrollButton = document.querySelector('#log-autoscroll-button');
        const consoleLogContainer = document.getElementById('console-log-container');
        if (!autoscrollButton || !consoleLogContainer) {
            SafeLogger.error("Autoscroll button or console log container not found.");
            return;
        }

        // Highlight the autoscroll button in purple when it's
        // enabled to make that clear.
        if (window._logPanelState.autoscroll) {
            autoscrollButton.classList.add('ldr-selected');
            // Immediately scroll to the top of the panel (newest logs are at top).
            consoleLogContainer.scrollTop = 0;
        } else {
            autoscrollButton.classList.remove('ldr-selected');
        }
    }

    /**
     * @brief Fetches all the logs for a research instance from the API.
     * @param researchId The ID of the research instance.
     * @returns {Promise<any>} The logs.
     */
    async function fetchLogsForResearch(researchId, limit) {
        // Pass an explicit limit to the API so the server doesn't return
        // (and we don't have to parse) more rows than the panel will keep.
        // Live load uses MAX_LOG_ENTRIES; download uses the server-side
        // hard cap (5000) so users still get the full tail.
        const url = URLBuilder.researchLogs(researchId, limit);
        const separator = url.includes('?') ? '&' : '?';
        const response = await safeFetchWithAuth(`${url}${separator}priority=diagnostic`);
        return await response.json();
    }

    /**
     * Fetch the persisted total row count for a research and cache it on
     * `window._logPanelState.totalLogs`. Shared by initializeLogPanel (initial
     * indicator paint) and loadLogsForResearch (per-load refresh before the
     * "X of Y" badge is rendered).
     *
     * Always clears any previously cached total before re-reading so a
     * transient server error or a research switch leaves the badge
     * honest — the indicator degrades to the rendered count rather than
     * showing a stale "of N" suffix from a previous research. Swallows
     * non-2xx and parse errors: the count endpoint is best-effort, and
     * the panel must keep working when it is unavailable.
     *
     * Generation-guarded: each call snapshots
     * `window._logPanelState._countRequestGen`. If the user switches
     * research while the fetch is in flight, the increment in
     * `initializeLogPanel`'s research-switch branch bumps the
     * generation; this response is then treated as stale and the
     * `totalLogs` write is skipped. Without this guard, a slow count
     * for research A can land after research B has loaded and overwrite
     * B's badge with A's total (PR #5115 follow-up review).
     *
     * @param {string} researchId
     * @returns {Promise<number|null>} The total, or null if the fetch
     *   failed / returned a malformed payload / was invalidated by a
     *   research switch.
     */
    async function fetchAndCacheLogCount(researchId) {
        window._logPanelState.totalLogs = null;
        if (!researchId) return null;
        const generation = window._logPanelState._countRequestGen || 0;
        try {
            // Shared JSON client: sends Accept: application/json so an
            // expired session on this /history/* route comes back as a
            // JSON 401 that redirects to sign-in instead of login HTML
            // that throws out of response.json(). Every other failure
            // rejects into the catch below — the badge still degrades
            // to the rendered count with no unhandled rejection.
            const data = await window.api.fetchWithErrorHandling(
                URLBuilder.historyLogCount(researchId)
            );
            // Bail out if the user switched research while this fetch
            // was in flight. The next load (for the new research) will
            // refetch and overwrite totalLogs itself.
            if ((window._logPanelState._countRequestGen || 0) !== generation) {
                SafeLogger.log(
                    'Discarding stale log count response for',
                    researchId,
                    '— research has changed'
                );
                return null;
            }
            if (data && typeof data.total_logs === 'number') {
                window._logPanelState.totalLogs = data.total_logs;
                return data.total_logs;
            }
            SafeLogger.error('Invalid log count data received from API');
        } catch (e) {
            SafeLogger.warn('Failed to fetch log count:', e);
        }
        return null;
    }

    const PRUNE_REMOVABLE_ORDER = Object.freeze(['info', 'milestone', 'warning', 'error']);

    /**
     * Map a rendered log type to its pruning priority without changing the
     * type stored in the DOM. Loguru alias levels are first folded into
     * their display category by getDisplayLogCategory (utils/log-helpers.js),
     * so CRITICAL/FATAL remain as diagnostic as ERROR and SUCCESS is treated
     * like a milestone: it represents a completed operation and should
     * outlive routine TRACE/DEBUG/INFO noise, but not warnings/errors.
     * Future types fall into the routine tier so they cannot bypass the cap.
     *
     * @param {string} type - Lowercase rendered log type.
     * @returns {'info'|'milestone'|'warning'|'error'} Pruning priority.
     */
    function getPruneTier(type) {
        switch (getDisplayLogCategory(type)) {
            case 'trace':
            case 'debug':
            case 'info':
                return 'info';
            case 'milestone':
                return 'milestone';
            case 'warning':
                return 'warning';
            case 'error':
                return 'error';
            default:
                return 'info';
        }
    }

    /**
     * Read the normalized type stored on a rendered log row.
     *
     * @param {Element} entry - Rendered log entry.
     * @returns {string} Lowercase DOM type, defaulting to "info".
     */
    function getRenderedLogType(entry) {
        const rawType = entry.dataset?.logType;
        const normalizedType = typeof rawType === 'string'
            ? rawType.toLowerCase()
            : '';
        return normalizedType || 'info';
    }

    /**
     * Trim log entries from the container down to `cap`, preferring to drop
     * the least-actionable categories first.
     *
     * A single static NodeList is scanned once per priority tier, making the
     * prune O(N) rather than re-querying and re-scanning the DOM after every
     * removal. Within each tier entries are removed in DOM (chronological)
     * order. Surviving warnings/errors may therefore be older than routine
     * entries, which is intentional.
     *
     * Only `.ldr-console-log-entry` descendants are considered; transient
     * placeholders such as `.ldr-empty-log-message`, `.ldr-loading-spinner`,
     * and `.ldr-error-message` are left alone.
     *
     * @param {Element} container - The log container element.
     * @param {number} cap - The maximum allowed entry count after pruning.
     * @param {number} [knownCount] - Known entry count after insertion. When
     *   at or below `cap`, pruning can return without querying the DOM.
     * @returns {string[]} Normalized DOM types of removed entries, in removal
     *   order. A missing type is normalized to "info", matching the rest of
     *   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, 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
        // cannot be skipped (e.g. 3 entries at cap=2.5 needs one removal).
        const excess = Math.ceil(entries.length - cap);
        if (excess <= 0) return [];

        const removed = [];
        let stillNeeded = excess;
        for (const targetTier of PRUNE_REMOVABLE_ORDER) {
            if (stillNeeded <= 0) break;
            for (const entry of entries) {
                // querySelectorAll() is static. Removed nodes remain in it, so
                // skip nodes no longer contained after an earlier tier scan.
                // contains() also works when the container itself is detached.
                if (!container.contains(entry)) continue;
                const type = getRenderedLogType(entry);
                if (getPruneTier(type) !== targetTier) continue;

                // Removing an entry also detaches any nested log rows. Include
                // every one in both the quota and returned types so callers'
                // counters stay aligned with the actual DOM mutation.
                const removalGroup = [
                    entry,
                    ...entry.querySelectorAll('.ldr-console-log-entry'),
                ].filter((row) => container.contains(row));
                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;
            }
        }
        return removed;
    }

    /**
     * Load logs for a specific research
     * @param {string} researchId - The research ID to load logs for
     */
    async function loadLogsForResearch(researchId, limit = MAX_LOG_ENTRIES) {
        // In-flight guard: if a fetch for this research is already pending
        // (e.g. pre-fetch from initializeLogPanel hasn't resolved yet and the
        // user expanded the panel), don't fire a second request.
        const panelEl = document.getElementById('log-panel-content') || document.getElementById('logPanel');
        if (panelEl && panelEl.dataset.loading === 'true') {
            SafeLogger.log('loadLogsForResearch already in flight, skipping duplicate');
            return;
        }
        if (panelEl) {
            panelEl.dataset.loading = 'true';
        }
        // Track the requested limit on shared state so the "Showing X of Y"
        // header can compare against the persisted total and expose "Load
        // older" only when there is more to load. The "Load older" button
        // re-enters loadLogsForResearch with hard_cap (5000) — we want the
        // header to reflect the *current* limit, not the original default.
        window._logPanelState.renderedLimit = limit;
        window._logPanelState.fetchedLogs = null;
        const generation = window._logPanelState._countRequestGen || 0;

        try {
            // Show loading state, but only if the container has no live
            // entries yet — otherwise we'd clobber socket-driven logs that
            // arrived before this fetch completes.
            const logContent = document.getElementById('console-log-container');
            if (logContent && !logContent.querySelector('.ldr-console-log-entry')) {
                logContent.innerHTML = '<div class="ldr-loading-spinner ldr-centered"><div class="ldr-spinner"></div><div style="margin-left: 10px;">Loading logs...</div></div>';
            }

            SafeLogger.log('Loading logs for research ID:', researchId);

            // Fetch the persisted total for every load. Long-running research
            // can add rows between the initial prefetch and a later "Load
            // older" click, so reusing an old total would make the badge lie.
            // A failed or malformed count response clears the cached value so
            // the indicator degrades to the rendered count only.
            await fetchAndCacheLogCount(researchId);

            if ((window._logPanelState._countRequestGen || 0) !== generation) {
                SafeLogger.log('Discarding stale logs load (post-count) for', researchId);
                return;
            }

            // Use the caller's requested limit (MAX_LOG_ENTRIES by default,
            // window.LDR_LOG_LIMITS.hard_cap from the "Load older" button).
            // fetchLogsForResearch clamps ?limit server-side to the hard cap,
            // so a malicious caller can't bypass the safety ceiling.
            const data = await fetchLogsForResearch(researchId, limit);
            SafeLogger.log('Logs API response:', data);

            if ((window._logPanelState._countRequestGen || 0) !== generation) {
                SafeLogger.log('Discarding stale logs response (post-fetch) for', researchId);
                return;
            }

            // Initialize array to hold all logs from different sources
            const allLogs = [];
            let fetchedLogs = 0;

            // Track seen messages to avoid duplicate content with different timestamps
            const seenMessages = new Map();

            // Process progress_log if available
            if (data.progress_log && typeof data.progress_log === 'string') {
                try {
                    const progressLogs = JSON.parse(data.progress_log);
                    if (Array.isArray(progressLogs) && progressLogs.length > 0) {
                        fetchedLogs += progressLogs.length;
                        SafeLogger.log(`Found ${progressLogs.length} logs in progress_log`);

                        // Process progress logs
                        progressLogs.forEach(logItem => {
                            if (!logItem.time || !logItem.message) return; // Skip invalid logs

                            const messageKey = normalizeMessage(logItem.message);

                            // Determine log type before applying content dedup.
                            // Only routine info entries are safe to collapse;
                            // repeated errors and milestones carry diagnostic
                            // and progress information even when their text is
                            // identical.
                            let logType = 'info';
                            if (logItem.metadata) {
                                if (logItem.metadata.phase === 'iteration_complete' ||
                                    logItem.metadata.phase === 'report_complete' ||
                                    logItem.metadata.phase === 'complete' ||
                                    logItem.metadata.is_milestone === true) {
                                    logType = 'milestone';
                                } else if (logItem.metadata.phase === 'error') {
                                    logType = 'error';
                                }
                            }

                            // Add message keywords for better type detection
                            if (logType !== 'milestone') {
                                const msg = logItem.message.toLowerCase();
                                if (msg.includes('complete') ||
                                    msg.includes('finished') ||
                                    msg.includes('starting phase') ||
                                    msg.includes('generated report')) {
                                    logType = 'milestone';
                                } else if (msg.includes('error') || msg.includes('failed')) {
                                    logType = 'error';
                                }
                            }

                            // Collapse only repeated routine messages within one
                            // minute. Keep the previous-type check so a routine
                            // message cannot be collapsed into a diagnostic
                            // error or milestone with the same text.
                            if (COLLAPSIBLE_LOG_TYPES.has(logType) && seenMessages.has(messageKey)) {
                                const previousLog = seenMessages.get(messageKey);
                                if (previousLog.type === logType) {
                                    const previousTime = new Date(previousLog.time);
                                    const currentTime = new Date(logItem.time);
                                    const timeDiff = Math.abs(currentTime - previousTime) / 1000; // in seconds

                                    if (timeDiff < 60) { // Within 1 minute
                                        // Use the newer timestamp if available
                                        if (currentTime > previousTime) {
                                            previousLog.time = logItem.time;
                                        }
                                        previousLog.repeatCount =
                                            (previousLog.repeatCount || 1) + 1;
                                        return; // Skip this duplicate
                                    }
                                }
                            }

                            // Create a log entry object with a unique ID for deduplication
                            const logEntry = {
                                id: `${logItem.time}-${hashString(logItem.message)}`,
                                time: logItem.time,
                                message: logItem.message,
                                type: logType,
                                metadata: logItem.metadata || {},
                                source: 'progress_log'
                            };

                            // Track this message to avoid showing exact duplicates with different timestamps
                            seenMessages.set(messageKey, logEntry);

                            // Add to all logs array
                            allLogs.push(logEntry);
                        });
                    }
                } catch (e) {
                    SafeLogger.error('Error parsing progress_log:', e);
                }
            }

            // Standard logs array processing
            // Check if data is directly an array (new format) or has a logs property (old format)
            const logsArray = Array.isArray(data) ? data : (data && data.logs);

            if (logsArray && Array.isArray(logsArray)) {
                fetchedLogs += logsArray.length;
                SafeLogger.log(`Processing ${logsArray.length} standard logs`);

                // Process each standard log
                logsArray.forEach(log => {
                    if (!log.timestamp && !log.time) return; // Skip invalid logs

                    // Explicit server metadata is authoritative. Only infer a
                    // type from message text for legacy payloads that omitted
                    // severity entirely; otherwise an INFO message mentioning
                    // "error handling" would be misclassified as an error.
                    const explicitLogType = log.log_type || log.type || log.level;
                    let logType = (explicitLogType || 'info').toLowerCase();
                    const messageText = (log.message || log.content || '').toLowerCase();
                    if (!explicitLogType) {
                        if (messageText.includes('complete') ||
                            messageText.includes('finished') ||
                            messageText.includes('starting phase') ||
                            messageText.includes('generated report')) {
                            logType = 'milestone';
                        } else if (messageText.includes('error') ||
                                   messageText.includes('failed')) {
                            logType = 'error';
                        }
                    }

                    const messageKey = normalizeMessage(log.message || log.content || '');

                    // Collapse only repeated info/debug messages within one
                    // minute, and only when the previous type also matches.
                    if (COLLAPSIBLE_LOG_TYPES.has(logType) && seenMessages.has(messageKey)) {
                        const previousLog = seenMessages.get(messageKey);
                        if (previousLog.type === logType) {
                            const previousTime = new Date(previousLog.time);
                            const currentTime = new Date(log.timestamp || log.time);
                            const timeDiff = Math.abs(currentTime - previousTime) / 1000; // in seconds

                            if (timeDiff < 60) { // Within 1 minute
                                // Use the newer timestamp if available
                                if (currentTime > previousTime) {
                                    previousLog.time = log.timestamp || log.time;
                                }
                                previousLog.repeatCount =
                                    (previousLog.repeatCount || 1) + 1;
                                return; // Skip this duplicate
                            }
                        }
                    }

                    // Create standardized log entry. Prefer the persisted
                    // database id when the server provides one — the
                    // /api/research/<id>/logs endpoint returns a stable
                    // row id and deliberately orders equal timestamps by
                    // it, so two distinct rows with identical timestamp
                    // + message must remain distinguishable here. The
                    // timestamp+hash fallback covers payloads that lack
                    // an id (older socket events, hand-rolled fixtures).
                    const logEntry = {
                        id: log.id != null && log.id !== ''
                            ? String(log.id)
                            : `${log.timestamp || log.time}-${hashString(log.message || log.content || '')}`,
                        time: log.timestamp || log.time,
                        message: log.message || log.content || 'No message',
                        type: logType,
                        metadata: log.metadata || {},
                        source: 'standard_logs'
                    };

                    // Track this message
                    seenMessages.set(messageKey, logEntry);

                    // Add to all logs array
                    allLogs.push(logEntry);
                });
            }
            window._logPanelState.fetchedLogs = fetchedLogs;

            const panelContent = document.getElementById('log-panel-content') || document.getElementById('logPanel');

            // Clear container
            if (logContent) {
                if (allLogs.length === 0) {
                    // If socket events populated logs while this fetch was
                    // in flight, don't clobber them with the empty placeholder.
                    const hasLiveEntries = logContent.querySelector('.ldr-console-log-entry');
                    if (!hasLiveEntries) {
                        logContent.innerHTML = '<div class="ldr-empty-log-message">No logs available for this research.</div>';
                    }
                    // Leave dataset.loaded unset so a future toggle re-fetches
                    // once the backend has flushed log rows.
                    if (panelContent) {
                        delete panelContent.dataset.loaded;
                    }
                    recomputeCountersFromDom();
                    updateLogCountIndicator(logContent);
                    return;
                }

                normalizeTimestamps(allLogs);

                // Deduplicate logs by ID and sort by timestamp (oldest first)
                const uniqueLogsMap = new Map();
                allLogs.forEach(log => {
                    uniqueLogsMap.set(log.id, log);
                });
                const uniqueLogs = Array.from(uniqueLogsMap.values());
                const sortedLogs = uniqueLogs.sort((a, b) => {
                    return new Date(b.time) - new Date(a.time);
                });

                SafeLogger.log(`Displaying ${sortedLogs.length} logs after deduplication (from original ${allLogs.length})`);

                // If socket events populated entries while this fetch was
                // in flight, append via addLogEntryToPanel (which dedupes by
                // id and message) instead of clobbering with innerHTML = ''.
                const hasLiveEntries = logContent.querySelector('.ldr-console-log-entry');
                if (hasLiveEntries) {
                    sortedLogs.forEach(logEntry => addLogEntryToPanel(logEntry, false));
                    // Bulk-merge path: addLogEntryToPanel(..., false)
                    // emits a prune decrement for every insert above the
                    // cap but never issues a compensating increment, so
                    // _logPanelState.counts and the header indicator
                    // drift below zero even though the DOM ends up at
                    // the cap. Re-derive from the rendered DOM here so
                    // badges / indicator / state always reflect what's
                    // actually shown. See the comment in
                    // recomputeCountersFromDom() for why the DOM is the
                    // single source of truth.
                    recomputeCountersFromDom();
                    if (panelContent) {
                        panelContent.dataset.loaded = 'true';
                    }
                    updateLogCountIndicator(logContent);
                    return;
                }

                logContent.innerHTML = '';

                // Append one DocumentFragment to minimize batch-insert reflows.
                // sortedLogs is newest-first, but DOM needs [oldest, ..., newest]
                // for column-reverse CSS to show newest at visual top
                const fragment = document.createDocumentFragment();
                for (let i = sortedLogs.length - 1; i >= 0; i--) {
                    const element = createLogEntryElement(sortedLogs[i]);
                    if (element) {
                        fragment.appendChild(element);
                    }
                }
                logContent.appendChild(fragment);

                const renderCap =
                    window._logPanelState.renderedLimit ?? MAX_LOG_ENTRIES;
                pruneToCap(logContent, renderCap);

                // Reset and recompute per-category counts and the header
                // indicator from the rendered DOM after the batch insert
                // + prune. The DOM is the single source of truth; the
                // helper handles badge + indicator writes too, so any
                // future insertion path that bypasses addLogEntryToPanel
                // can't desync the counters so long as it lands here
                // before any badge / indicator render.
                recomputeCountersFromDom();

                // Update log count indicator and (if truncated) render a
                // "Showing X of Y" badge with a "Load older" button. The prior
                // implementation always wrote the DOM child count, which
                // overwrote the total set by initializeLogPanel and hid the
                // fact that the user was looking at a truncated slice of a
                // long run (issue #4878 — "500 of 9,002, no indication").
                updateLogCountIndicator(logContent);

                // Mark loaded only after a successful non-empty fetch so an
                // empty initial response doesn't permanently suppress retries.
                if (panelContent) {
                    panelContent.dataset.loaded = 'true';
                }
            }

        } catch (error) {
            SafeLogger.error('Error loading logs:', error);

            if ((window._logPanelState._countRequestGen || 0) !== generation) {
                SafeLogger.log(
                    'Discarding error DOM write for stale research ID:',
                    researchId,
                    '— research has changed'
                );
                return;
            }

            // Show error in log panel
            // SECURITY: error.message can contain arbitrary text — must escape before innerHTML
            const logContent = document.getElementById('console-log-container');
            if (logContent) {
                // bearer:disable javascript_lang_dangerous_insert_html
                logContent.innerHTML = `<div class="ldr-error-message">Error loading logs: ${escapeHtml(error.message)}</div>`;
                recomputeCountersFromDom();
                updateLogCountIndicator(logContent);
            }
        } finally {
            if (panelEl && (window._logPanelState._countRequestGen || 0) === generation) {
                delete panelEl.dataset.loading;
            }
        }
    }

    /**
     * Add a log entry to the console - public API
     * @param {string} message - Log message
     * @param {string} level - Log level (info, milestone, error)
     * @param {Object} metadata - Optional metadata
     */
    function addConsoleLog(message, level = 'info', metadata = null) {
        SafeLogger.log(`[${level.toUpperCase()}] ${message}`);

        const timestamp = new Date().toISOString();
        const logEntry = {
            id: `${timestamp}-${hashString(message)}`,
            time: timestamp,
            message,
            type: level,
            metadata: metadata || { type: level }
        };

        // Queue log entries if panel is not expanded yet
        if (!window._logPanelState.expanded) {
            window._logPanelState.queuedLogs.push(logEntry);
            SafeLogger.log('Queued log entry for later display');

            // Update log count even if not displaying yet
            updateLogCounter(1);

            // Auto-expand log panel on first log
            const logPanelToggle = document.getElementById('log-panel-toggle');
            if (logPanelToggle) {
                SafeLogger.log('Auto-expanding log panel because logs are available');
                logPanelToggle.click();
            }

            return;
        }

        // Add directly to panel if it's expanded
        addLogEntryToPanel(logEntry, true);
    }

    /**
     * Create a DOM element for a log entry without inserting it.
     * Used by both addLogEntryToPanel() for live logs and batch loading via DocumentFragment.
     * @param {Object} logEntry - The log entry data
     * @returns {HTMLElement|null} - The created element, or null on failure
     */
    function createLogEntryElement(logEntry) {
        // Ensure the log entry has an ID
        if (!logEntry.id) {
            const timestamp = logEntry.time || logEntry.timestamp || new Date().toISOString();
            const message = logEntry.message || logEntry.content || 'No message';
            logEntry.id = `${timestamp}-${hashString(message)}`;
        }

        // Get the log template
        const template = document.getElementById('console-log-entry-template');

        // Determine log level - CHECK FOR DIRECT TYPE FIELD FIRST
        let logLevel = 'info';
        if (logEntry.type) {
            logLevel = logEntry.type;
        } else if (logEntry.metadata && logEntry.metadata.type) {
            logLevel = logEntry.metadata.type;
        } else if (logEntry.level) {
            logLevel = logEntry.level;
        }

        // Format timestamp
        const timestamp = new Date(logEntry.time || logEntry.timestamp || new Date());
        const timeStr = timestamp.toLocaleTimeString();

        // Get message
        const message = logEntry.message || logEntry.content || 'No message';
        const repeatCount = Math.max(1, Number(logEntry.repeatCount) || 1);

        let element;

        if (template) {
            // Create a new log entry from the template
            const entry = document.importNode(template.content, true);
            element = entry.querySelector('.ldr-console-log-entry');

            // Add the log type as data attribute for filtering
            if (element) {
                element.dataset.logType = logLevel.toLowerCase();
                element.classList.add(`ldr-log-${logLevel.toLowerCase()}`);
                // Initialize counter for duplicate tracking
                element.dataset.counter = String(repeatCount);
                // Store log ID for deduplication
                if (logEntry.id) {
                    element.dataset.logId = logEntry.id;
                }

                // Add special attribute for engine selection events
                if (logEntry.metadata && logEntry.metadata.phase === 'engine_selected') {
                    element.dataset.engineSelected = 'true';
                    // Store engine name as a data attribute
                    if (logEntry.metadata.engine) {
                        element.dataset.engine = logEntry.metadata.engine;
                    }
                }

                element.dataset.logTimeMs = Number.isNaN(timestamp.getTime())
                    ? String(Date.now())
                    : String(timestamp.getTime());
                element.dataset.logMessage = message;
            }

            // Set content
            entry.querySelector('.ldr-log-timestamp').textContent = timeStr;
            entry.querySelector('.ldr-log-badge').textContent = logLevel.charAt(0).toUpperCase() + logLevel.slice(1);
            entry.querySelector('.ldr-log-message').textContent = message;
        } else {
            // Create a simple log entry without template
            element = document.createElement('div');
            element.className = 'ldr-console-log-entry';
            element.dataset.logType = logLevel.toLowerCase();
            element.classList.add(`ldr-log-${logLevel.toLowerCase()}`);
            element.dataset.counter = String(repeatCount);
            if (logEntry.id) {
                element.dataset.logId = logEntry.id;
            }

            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
            element.innerHTML = `
                <span class="ldr-log-timestamp">${escapeHtml(timeStr)}</span>
                <span class="ldr-log-badge">${escapeHtml(logLevel.charAt(0).toUpperCase() + logLevel.slice(1))}</span>
                <span class="ldr-log-message">${escapeHtml(message)}</span>
            `;
        }

        if (element && repeatCount > 1) {
            const counterBadge = document.createElement('span');
            counterBadge.className = 'ldr-duplicate-counter';
            counterBadge.textContent = `(${repeatCount}×)`;
            element.appendChild(counterBadge);
        }

        // Apply visibility based on current filter
        if (element) {
            const currentFilter = window._logPanelState.currentFilter || 'all';
            const shouldShow = checkLogVisibility(logLevel.toLowerCase(), currentFilter);
            element.style.display = shouldShow ? '' : 'none';
        }

        return element;
    }

    /**
     * Add a log entry directly to the panel
     * @param {Object} logEntry - The log entry to add
     * @param {boolean} incrementCounter - Whether to increment the log counter
     */
    function addLogEntryToPanel(logEntry, incrementCounter = true) {
        SafeLogger.log('Adding log entry to panel:', logEntry);

        const consoleLogContainer = document.getElementById('console-log-container');
        if (!consoleLogContainer) {
            SafeLogger.warn('Console log container not found');
            return;
        }

        // Clear empty message if present
        const emptyMessage = consoleLogContainer.querySelector('.ldr-empty-log-message');
        if (emptyMessage) {
            emptyMessage.remove();
        }

        // Clear the "Loading logs..." spinner if it's still showing. The
        // initial /logs fetch may have returned empty (research just
        // started, no rows yet) and left the spinner in place; once
        // socket-driven entries start arriving we want them visible
        // instead of accumulating beneath a stuck spinner.
        const loadingSpinner = consoleLogContainer.querySelector('.ldr-loading-spinner');
        if (loadingSpinner) {
            loadingSpinner.remove();
        }

        // Ensure the log entry has an ID
        if (!logEntry.id) {
            const timestamp = logEntry.time || logEntry.timestamp || new Date().toISOString();
            const message = logEntry.message || logEntry.content || 'No message';
            logEntry.id = `${timestamp}-${hashString(message)}`;
        }

        // More robust deduplication: First check by ID if available
        if (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);

                if (incrementCounter) {
                    // Increment counter on existing entry
                    let counter = parseInt(existingEntryById.dataset.counter || '1', 10);
                    counter++;
                    existingEntryById.dataset.counter = counter;

                    // Update visual counter badge
                    if (counter > 1) {
                        let counterBadge = existingEntryById.querySelector('.ldr-duplicate-counter');
                        if (!counterBadge) {
                            counterBadge = document.createElement('span');
                            counterBadge.className = 'ldr-duplicate-counter';
                            existingEntryById.appendChild(counterBadge);
                        }
                        counterBadge.textContent = `(${counter}×)`;
                    }
                }

                return;
            }
        }

        // Secondary check for duplicate by message content (for backward
        // compatibility with logs that lack a stable id, e.g. older socket
        // payloads). The 10-newest scan applies only to routine info/debug:
        // folding identical repeats into a (N×) badge reduces noise without
        // losing signal.
        //   - warnings / errors / milestones are diagnostic -- collapsing
        //     repeated retries or repeated failures into a single counter
        //     strips the recency signal (you can't tell *when* the last
        //     failure occurred) and can hide progress. Always insert.
        const existingEntries = consoleLogContainer.querySelectorAll('.ldr-console-log-entry');
        if (existingEntries.length > 0) {
            const message = logEntry.message || logEntry.content || '';
            const logType = (logEntry.type || 'info').toLowerCase();

            if (!COLLAPSIBLE_LOG_TYPES.has(logType)) {
                // Non-info categories always render, even when the message
                // duplicates a recent entry. The id-based dedup above still
                // catches exact retransmits with the same id.
            } else {
                // Check 10 most recent entries. DOM order is oldest -> newest so
                // column-reverse CSS can render the newest entry at the visual top.
                const start = Math.max(0, existingEntries.length - 10);
                for (let i = existingEntries.length - 1; i >= start; i--) {
                    const entry = existingEntries[i];
                    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
                    // (only ever reached for info/debug entries; the outer
                    // if/else above already short-circuited warnings,
                    // errors, and milestones).
                    if (entryMessage === message &&
                        entryType === logType) {

                        SafeLogger.log('Skipping duplicate log entry by content:', message);

                        if (incrementCounter) {
                            // Increment counter on existing entry
                            let counter = parseInt(entry.dataset.counter || '1', 10);
                            counter++;
                            entry.dataset.counter = counter;

                            // Update visual counter badge
                            if (counter > 1) {
                                let counterBadge = entry.querySelector('.ldr-duplicate-counter');
                                if (!counterBadge) {
                                    counterBadge = document.createElement('span');
                                    counterBadge.className = 'ldr-duplicate-counter';
                                    entry.appendChild(counterBadge);
                                }
                                counterBadge.textContent = `(${counter}×)`;
                            }
                        }

                        return;
                    }
                }
            }
        }

        const element = createLogEntryElement(logEntry);

        if (element) {
            // Keep DOM order oldest -> newest. The container uses
            // 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());
            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
                    // The early break relies on every insertion path preserving
                    // oldest-to-newest DOM order.
                    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
        // is the least-actionable entry and is immediately pruned, this order
        // lets the matching decrement return the counter to zero without a
        // transient negative value.
        let countersChanged = false;
        if (incrementCounter && element) {
            const logType = (element.dataset.logType || 'info').toLowerCase();
            bumpCount(
                window._logPanelState.counts,
                getDisplayLogCategory(logType),
                1
            );
            countersChanged = true;
        }

        // Prune oldest entries if over the current rendered limit; see
        // pruneToCap for the severity ordering. Keeping renderedLimit here
        // preserves an expanded "Load older" window up to the 5000-row cap.
        const renderCap =
            window._logPanelState.renderedLimit ?? MAX_LOG_ENTRIES;
        const totalCount = existingEntries.length + 1;
        const removed = pruneToCap(consoleLogContainer, renderCap, totalCount);
        if (removed.length > 0) {
            for (const prunedType of removed) {
                bumpCount(
                    window._logPanelState.counts,
                    getDisplayLogCategory(prunedType),
                    -1
                );
            }
            countersChanged = true;
        }

        if (countersChanged) {
            updateLogCounter(0);
            updateFilterCounters();
        }

        // No need to scroll when loading all logs
        // Scroll will be handled after all logs are loaded
        if (incrementCounter && element && window._logPanelState.autoscroll) {
            // Auto-scroll to newest log (at the top)
            setTimeout(() => {
                consoleLogContainer.scrollTop = 0;
            }, 0);
        }
    }

    /**
     * Count ERROR-severity entries currently rendered in `containerEl`.
     *
     * Reads `data-log-type` (the RAW level, which is what the row carries)
     * and folds it through `getDisplayLogCategory` — the same normalizer
     * `recomputeCountersFromDom` and the filter buttons use. A selector on
     * `[data-log-type="error"]` alone would miss CRITICAL and FATAL, the
     * two severities the badge most needs to surface, while the "Error"
     * filter badge counted them (they map to the `error` category).
     *
     * @param {Element|null} containerEl - The log container.
     * @returns {number} Rendered ERROR-category entry count.
     */
    function countRenderedErrors(containerEl) {
        if (!containerEl) return 0;
        let count = 0;
        containerEl
            .querySelectorAll('.ldr-console-log-entry')
            .forEach((entry) => {
                const raw = (entry.dataset.logType || 'info');
                if (getDisplayLogCategory(raw) === 'error') count++;
            });
        return count;
    }

    /**
     * Paint the header count badge(s): the numeric label plus the severity
     * signal, in ONE place.
     *
     * Every writer of `.ldr-log-indicator`'s textContent inside this module
     * must go through here. Earlier revisions of this branch had
     * `initializeLogPanel` and `recomputeCountersFromDom` write the number
     * directly while only `updateLogCountIndicator` toggled the error
     * class/title, so a re-init or a recompute left a stale red badge (and
     * stale tooltip) attached to a fresh count. The one writer outside this
     * module is `services/socket.js`'s no-log-panel fallback (it increments
     * `#log-indicator` directly around socket.js:1079-1083); it is reached
     * only when `window.logPanel` is absent, i.e. when this function does
     * not exist either.
     *
     * The error signal is a HIGH-WATER mark, not a live DOM count. The DOM
     * holds only the rendered window: the server returns the most recent
     * `limit` rows (errors are fetched first — `priority=diagnostic` sorts
     * ERROR/CRITICAL/FATAL to the front — but a run with more errors than
     * the limit still loses the oldest), and the ordered prune can evict
     * error rows once every routine row is gone. Flooring at the high-water
     * mark keeps a badge that has legitimately gone red from silently
     * going purple again. It also means the number is a LOWER BOUND
     * whenever the panel is showing a truncated slice, which the tooltip
     * says out loud rather than implying a total it cannot know.
     *
     * Accessibility: colour alone must not carry the state. The badge also
     * gets an `!` glyph (CSS `::after`, so `textContent` — which
     * `updateFilterCounters` parses — is untouched) and a heavier
     * weight, plus `role="img"` + `aria-label` so the severity is
     * exposed to assistive tech instead of living only in a hover `title`.
     *
     * @param {string} labelText - The already-formatted count label.
     * @param {Element|null} containerEl - Log container to count errors in.
     * @param {boolean} [truncated] - True when the panel is showing fewer
     *   rows than exist server-side, making the error count a lower bound.
     * @returns {number} The error count applied (the high-water mark).
     */
    function paintLogIndicators(labelText, containerEl, truncated) {
        const indicators = document.querySelectorAll('.ldr-log-indicator');
        if (indicators.length === 0) return 0;

        const seen = countRenderedErrors(containerEl);
        const previous = window._logPanelState.errorHighWater || 0;
        const errorCount = Math.max(previous, seen);
        window._logPanelState.errorHighWater = errorCount;

        const plural = errorCount === 1 ? '' : 's';
        const qualifier = truncated ? 'at least ' : '';
        const description = errorCount > 0
            ? `${qualifier}${errorCount} error${plural} in the log — ` +
              'expand the panel for details'
            : '';

        indicators.forEach((indicator) => {
            indicator.textContent = labelText;
            indicator.classList.toggle(
                'ldr-log-indicator--has-error',
                errorCount > 0
            );
            if (errorCount > 0) {
                indicator.title = description;
                indicator.setAttribute('role', 'img');
                indicator.setAttribute(
                    'aria-label',
                    `${labelText} log entries, ${description}`
                );
            } else {
                indicator.title = '';
                indicator.removeAttribute('role');
                indicator.removeAttribute('aria-label');
            }
        });
        return errorCount;
    }

    /**
     * Render the log-panel-header indicator (`.ldr-log-indicator`) with the
     * current DOM count, and append a "Load older" button when the persisted
     * total exceeds the raw row count returned by the latest request.
     *
     * Layout:
     *   <span class="ldr-log-indicator" id="log-indicator">500</span>
     *   <span class="ldr-log-of-total"> of 9,002</span>
     *   <button class="ldr-load-older">Load older</button>
     *
     * The button is dynamically created/removed so the header stays compact
     * for short runs. Clicking it calls loadLogsForResearch with the shared
     * hard cap (window.LDR_LOG_LIMITS.hard_cap), which is the same ceiling
     * the existing "Download Logs" button uses. Cursor pagination is out of
     * scope for this fix; the most recent `hard_cap` rows is the largest
     * window the current API can return in a single round trip.
     *
     * The button is suppressed — but the "X of Y" badge is kept — once the
     * requested `renderedLimit` reaches the shared hard cap. Beyond that
     * ceiling the server returns the same window and a second click would
     * be a no-op; hiding the button is a clearer signal than letting users
     * click into a dead end.
     *
     * Safe to call repeatedly — it de-duplicates itself by removing any
     * pre-existing "of Y" suffix / "Load older" button before re-appending.
     *
     * @param {Element} [container] - The log container element. When
     *   omitted, falls back to document.getElementById('console-log-container')
     *   so the live-insert path doesn't need to pass it.
     */
    function updateLogCountIndicator(container) {
        const logIndicators = document.querySelectorAll('.ldr-log-indicator');
        if (logIndicators.length === 0) return;

        const containerEl =
            container ||
            document.getElementById('console-log-container');
        const rendered = containerEl
            ? containerEl.querySelectorAll('.ldr-console-log-entry').length
            : 0;
        const total = window._logPanelState.totalLogs;
        const fetched = window._logPanelState.fetchedLogs;
        const truncated =
            typeof total === 'number' &&
            total > (typeof fetched === 'number' ? fetched : rendered);

        // Prefer the server-known fetched count over the deduped DOM
        // count so the indicator math stays consistent across "Load
        // older" clicks: a panel that starts at "500 of 973" and bumps
        // to "973" after Load older reads as a clean +473 delta,
        // whereas reporting the DOM count would jump "353" -> "515"
        // and drop the "of 973" suffix — leaving 458 rows silently
        // absorbed into (N×) badges with no explanation (LearningCircuit
        // review, 2026-07-22, run a96e85ed). Fall back to the rendered
        // count for socket-insert-only paths where no fetch has run
        // yet, and never let the indicator fall below the live DOM
        // count in case socket inserts outpace the latest fetch.
        const indicatorValue =
            typeof fetched === 'number'
                ? Math.max(rendered, fetched)
                : rendered;
        const indicatorLabel = formatNumber(indicatorValue);
        // Surface severity on the always-visible header badge: the panel
        // starts collapsed, so without this an ERROR (e.g. an engine
        // self-disabling) is invisible until the user happens to expand
        // and filter the log. The count is a lower bound whenever the
        // panel is showing a truncated slice (server truncation, or the
        // post-dedup DOM holding fewer rows than were fetched), so tell
        // paintLogIndicators to word the tooltip as "at least N".
        paintLogIndicators(
            indicatorLabel,
            containerEl,
            truncated || rendered < indicatorValue
        );
        // updateFilterCounters reads the indicator textContent to set the
        // "All" badge — refresh it here so a higher fetched count (which
        // floors the indicator after message-content dedup) propagates to
        // the All badge too. Without this, recomputeCountersFromDom writes
        // All=domCount, then updateLogCountIndicator bumps the indicator,
        // and the two drift apart (LearningCircuit review, 2026-07-22,
        // run a96e85ed: header "973" / "All 515" mismatch).
        updateFilterCounters(indicatorValue);

        // Append / refresh the "of Y · Load older" cluster. We look up the
        // existing button by class so a second updateLogCountIndicator call
        // (e.g. after a live insert) doesn't pile up duplicate buttons.
        const headerEl = logIndicators[0].parentElement;
        if (!headerEl) return;

        // Remove any prior "of Y" suffix / "Load older" button so a count
        // change (live insert, "Load older" click) renders cleanly.
        headerEl.querySelectorAll('.ldr-log-of-total, .ldr-load-older').forEach((el) => {
            el.remove();
        });

        if (!truncated) {
            return;
        }

        const ofTotal = document.createElement('span');
        ofTotal.className = 'ldr-log-of-total';
        ofTotal.textContent = ` of ${formatNumber(total)}`;
        headerEl.appendChild(ofTotal);

        // Hide the button — but keep the "X of Y" badge — once the current
        // request is already at the server-side ceiling. A re-click would
        // re-fetch the same window (server clamps ?limit) and is therefore
        // a silent no-op, which surfaced as a confusing UX after PR #5115
        // for long runs (LearningCircuit review, 2026-07-16).
        const hardCap = window.LDR_LOG_LIMITS?.hard_cap ?? 5000;
        const currentLimit = window._logPanelState.renderedLimit;
        if (typeof currentLimit === 'number' && currentLimit >= hardCap) {
            return;
        }

        const loadOlder = document.createElement('button');
        loadOlder.type = 'button';
        loadOlder.className = 'ldr-small-btn ldr-load-older';
        loadOlder.textContent = 'Load older';
        loadOlder.title =
            'Load the full server-side cap (' +
            hardCap +
            ' most recent rows)';
        loadOlder.addEventListener('click', (event) => {
            // The button is appended to the same header that owns the
            // collapse/expand toggle. Without stopping propagation the
            // click would bubble up and toggle the panel, hiding the
            // expanded log list the user just asked to load. PR #5115
            // follow-up review (LearningCircuit, 2026-07-19).
            event.stopPropagation();
            const researchId = window._logPanelState.connectedResearchId;
            if (!researchId) return;
            // loadLogsForResearch is the same entry point used by the
            // panel toggle and the live socket path; it handles the
            // in-flight guard, batch-insert, and per-category counter
            // recompute. Re-entering it with `hardCap` re-fetches the
            // most-recent hardCap rows and re-renders "X of Y" (with X
            // now bumped). Once it returns, the renderedLimit-vs-hardCap
            // check above suppresses this same button so the user can't
            // click it again into a no-op.
            loadLogsForResearch(researchId, hardCap);
        });
        headerEl.appendChild(loadOlder);
    }

    /**
     * Localized thousands separator for the log count badge and the
     * "of Y" suffix. Falls back to Intl.NumberFormat with the document's
     * language if available, otherwise comma-grouped English.
     * @param {number} n
     * @returns {string}
     */
    function formatNumber(n) {
        try {
            return new Intl.NumberFormat(document.documentElement.lang || undefined).format(n);
        } catch {
            return String(n).replace(/\B(?=(?:\d{3})+(?!\d))/g, ',');
        }
    }

    /**
     * Helper function to update the log counter (used by live socket
     * insert and prune paths). Unlike the original implementation, this
     * now defers to updateLogCountIndicator so the "X of Y" badge and
     * "Load older" button stay in sync with the DOM. The `increment`
     * parameter is retained for callers (addLog, addLogEntryToPanel)
     * but is recomputed from the actual DOM child count to avoid
     * drift between the indicator and the entries.
     * @param {number} _increment - Unused; recomputed from the DOM.
     */
    function updateLogCounter(_increment) {
        updateLogCountIndicator();
    }

    /**
     * Refresh the per-filter count badges (and the "All" total derived
     * from them) from window._logPanelState.counts. Called whenever the
     * per-category counts change — on insert, prune, or batch load —
     * so the badges always reflect what's currently in the DOM. Safe to
     * call before the filter buttons are rendered: the querySelectorAll
     * returns an empty NodeList and the loop is a no-op.
     */
    function updateFilterCounters(allCount) {
        const counts = window._logPanelState.counts || {};
        // Defensive floor: the bulk-merge path in loadLogsForResearch
        // recomputes counts from the DOM (see recomputeCountersFromDom),
        // so this badge read shouldn't render a negative number in
        // practice. The clamp stays so a future insertion path that
        // bypasses the recompute can't surface as "Info -1" before a
        // follow-up fixes it.
        const safe = (n) => Math.max(0, n | 0);
        // The 'All' badge has to reflect EVERY rendered entry, not just
        // the four tracked buckets — DEBUG (and any other future category
        // the API emits but no filter button exists for) is valid
        // persisted output that renders in the DOM but never bumps
        // `counts`. Summing the four buckets would under-count.
        // updateLogCounter / recomputeCountersFromDom drive the
        // .ldr-log-indicator text in lockstep with the actual rendered
        // count, so reading the first indicator is the cheapest single
        // source of truth that stays consistent across the live-insert,
        // prune, and bulk-load paths. Falls back to the bucket sum
        // (which is wrong for untracked categories but at least never
        // negative) when no indicator exists in the DOM yet — e.g. in
        // a test harness that builds the badges before the indicator.
        let total;
        if (typeof allCount === 'number') {
            total = safe(allCount);
        } else {
            const indicatorEl = document.querySelector('.ldr-log-indicator');
            total = indicatorEl
                ? safe(parseInt(indicatorEl.textContent.replace(/\D/g, ''), 10) || 0)
                : safe(counts.info || 0) +
                  safe(counts.milestone || 0) +
                  safe(counts.warning || 0) +
                  safe(counts.error || 0);
        }
        const badges = document.querySelectorAll('.ldr-filter-count');
        badges.forEach((badge) => {
            const key = badge.dataset.filterCount;
            // String() coercion is required: happy-dom (the test DOM)
            // drops numeric 0 when assigned to textContent, so a "0"
            // count would render as an empty badge. Real browsers
            // coerce to "0" automatically, but the explicit String()
            // is harmless there and keeps the test environment honest.
            if (key === 'all') {
                badge.textContent = String(total);
            } else if (Object.prototype.hasOwnProperty.call(counts, key)) {
                badge.textContent = String(safe(counts[key]));
            }
        });
    }

    /**
     * Re-derive per-category counts and the header indicator from the
     * rendered DOM. Called at the end of every bulk-load path in
     * `loadLogsForResearch` (the empty-DOM batch path and the
     * hasLiveEntries merge path) so the counters always match the DOM
     * rather than the accumulated +-1 deltas, which can drift during a
     * large merge that exercises prune multiple times. The DOM is the
     * single source of truth: any future insertion path that bypasses
     * `addLogEntryToPanel` cannot desync the counters so long as it
     * lands here before any badge / indicator render.
     */
    function recomputeCountersFromDom() {
        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();
            const displayCategory = getDisplayLogCategory(t);
            // Count every rendered entry toward the total so the header
            // indicator and All badge stay accurate for categories
            // outside the four tracked buckets (e.g. DEBUG, which is
            // valid persisted API output and renders in the DOM but
            // has no corresponding per-filter button). Per-category
            // increments stay conditional on the bucket being tracked
            // so unknown types don't pollute the filter badges.
            total++;
            bumpCount(counts, displayCategory, 1);
            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
        // total at the server-known fetchedLogs so the indicator and
        // "All" badge stay consistent with the "X of Y" header — the
        // indicator math then reads as a clean delta across Load older
        // clicks instead of silently absorbing rows into badges
        // (LearningCircuit review, 2026-07-22, run a96e85ed: 500 ->
        // 973 of 973, not 353 -> 515 with 458 missing).
        const fetched = window._logPanelState.fetchedLogs;
        const reportedTotal =
            typeof fetched === 'number'
                ? Math.max(total, fetched)
                : total;
        window._logPanelState.counts = counts;
        // String() coercion matches updateFilterCounters' ""→"0" guard
        // for happy-dom (real browsers coerce to "0" automatically).
        // paintLogIndicators (not a bare textContent write) so the
        // severity class/tooltip/ARIA are recomputed with the count
        // instead of being left behind from the previous paint.
        paintLogIndicators(
            String(reportedTotal),
            logContent,
            total < reportedTotal
        );
        updateFilterCounters(reportedTotal);
    }

    /**
     * Filter logs by type
     * @param {string} filterType - The type to filter by (all, info, milestone, error)
     */
    function filterLogsByType(filterType = 'all') {
        SafeLogger.log('Filtering logs by type:', filterType);

        filterType = filterType.toLowerCase();

        // Store current filter in shared state
        window._logPanelState.currentFilter = filterType;

        // Get all log entries from the DOM
        const logEntries = document.querySelectorAll('.ldr-console-log-entry');
        SafeLogger.log(`Found ${logEntries.length} log entries to filter`);

        let visibleCount = 0;

        // Apply filters
        logEntries.forEach(entry => {
            // Use data attribute for log type
            const logType = entry.dataset.logType || 'info';

            // Determine visibility based on filter type
            const shouldShow = checkLogVisibility(logType, filterType);

            // Set display style based on filter result
            entry.style.display = shouldShow ? '' : 'none';

            if (shouldShow) {
                visibleCount++;
            }
        });

        SafeLogger.log(`Filtering complete. Showing ${visibleCount} of ${logEntries.length} logs`);

        // Show 'no logs' message if all logs are filtered out
        const consoleContainer = document.getElementById('console-log-container');
        if (consoleContainer && logEntries.length > 0) {
            // Remove any existing empty message
            const existingEmptyMessage = consoleContainer.querySelector('.ldr-empty-log-message');
            if (existingEmptyMessage) {
                existingEmptyMessage.remove();
            }

            // Add empty message if needed
            if (visibleCount === 0) {
                SafeLogger.log(`Adding 'no logs' message for filter: ${filterType}`);
                const newEmptyMessage = document.createElement('div');
                newEmptyMessage.className = 'ldr-empty-log-message';
                newEmptyMessage.textContent = `No ${filterType} logs to display.`;
                consoleContainer.appendChild(newEmptyMessage);
            }
        }
    }

    /**
     * @brief Handler for the log download button which downloads all the
     * saved logs to the user's computer.
     */
    async function downloadLogs() {
        const researchId = window._logPanelState.connectedResearchId;
        if (!researchId) {
            // No active research yet (e.g. on a freshly-loaded /chat/ page).
            // Without this guard, a fetchLogsForResearch(null) call would
            // request /api/research/null/logs and fail silently.
            SafeLogger.warn('downloadLogs called without researchId; skipping');
            return;
        }
        // Stream the full export rather than the capped in-page API:
        //   * /api/research/<id>/logs is bounded by HISTORY_LOGS_HARD_CAP
        //     (5 000) on the server so the on-screen panel's DOM and
        //     JSON-parsing budget stays sane. That cap is wrong for a
        //     download — the only memory touched is the browser's
        //     download manager writing the response to disk.
        //   * /api/research/<id>/logs/export streams NDJSON
        //     (Content-Disposition: attachment) so the browser pulls
        //     chunks straight to disk without buffering the full body in
        //     a JS Blob, and the server uses yield_per(500) so it never
        //     holds the full result either.
        //
        // We trigger the download with a direct anchor click rather than
        // fetch()+Blob: the latter would materialise the whole response
        // in JS memory before handing it to the download manager, which
        // is exactly what the streaming endpoint exists to avoid.
        const exportUrl = URLBuilder.researchLogsExport(researchId);

        try {
            // Perform a fast HEAD pre-flight to verify endpoint status (e.g. catch 404/429/500)
            // before creating the download anchor, preventing the browser from saving JSON errors as .jsonl files.
            const res = await safeFetchWithAuth(exportUrl, { method: 'HEAD' });
            if (!res.ok) {
                const errorMsg = res.status === 404
                    ? 'Research logs not found.'
                    : res.status === 429
                    ? 'Log export rate limit exceeded. Please wait a moment.'
                    : `Failed to export logs (HTTP ${res.status}).`;
                if (window.ui?.showAlert) {
                    window.ui.showAlert(errorMsg, 'error');
                } else if (window.ui?.showError) {
                    window.ui.showError(errorMsg);
                } else {
                    SafeLogger.error(errorMsg);
                }
                return;
            }
        } catch (err) {
            SafeLogger.warn('Export pre-flight check failed, proceeding with download', err);
        }

        const a = document.createElement('a');
        a.style.display = 'none';
        if (typeof URLValidator !== 'undefined' && URLValidator.safeAssign) {
            URLValidator.safeAssign(a, 'href', exportUrl);
        } else {
            a.href = exportUrl;
        }
        // ``download`` is a hint to the browser to save rather than
        // navigate; the server's Content-Disposition header is the
        // authoritative filename, so they match either way.
        a.download = `research_logs_${researchId}.jsonl`;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
    }

    // Expose public API
    window.logPanel = {
        initialize: initializeLogPanel,
        addLog: addConsoleLog,
        filterLogs: filterLogsByType,
        loadLogs: loadLogsForResearch,
        // Exposed for unit tests so the per-category prune ordering can be
        // exercised in isolation from the rest of the panel pipeline.
        _pruneToCap: pruneToCap,
        // Exposed for unit tests: the header badge's count + severity
        // paint is otherwise only reachable through a full load/insert
        // cycle, which makes the CRITICAL/FATAL and paged-out cases
        // awkward to drive directly.
        _updateLogCountIndicator: updateLogCountIndicator,
        _recomputeCountersFromDom: recomputeCountersFromDom
    };

    // Self-invoke to initialize when DOM content is loaded
    document.addEventListener('DOMContentLoaded', function() {
        SafeLogger.log('DOM ready - checking if log panel should be initialized');

        // Find research ID from URL if available (supports both integer and UUID)
        let researchId = null;
        const urlMatch = window.location.pathname.match(/\/(progress|results)\/([a-zA-Z0-9-]+)/);
        if (urlMatch && urlMatch[2]) {
            researchId = urlMatch[2];
            SafeLogger.log('Found research ID in URL:', researchId);

            // Store the current research ID in the state
            window._logPanelState.connectedResearchId = researchId;
        }

        // Check for research page elements
        const isResearchPage = window.location.pathname.includes('/progress/') ||
                              window.location.pathname.includes('/results/') ||
                              window.location.pathname.includes('/chat/') ||
                              document.getElementById('research-progress') ||
                              document.getElementById('research-results');

        // Initialize log panel if on a research page
        if (isResearchPage) {
            SafeLogger.log('On a research page, initializing log panel for research ID:', researchId);
            initializeLogPanel(researchId);

            // Extra check: If we have a research ID but panel not initialized properly
            setTimeout(() => {
                if (researchId && !window._logPanelState.initialized) {
                    SafeLogger.log('Log panel not initialized properly, retrying...');
                    initializeLogPanel(researchId);
                }
            }, 1000);
        } else {
            SafeLogger.log('Not on a research page, skipping log panel initialization');
        }
    });
})();