Skip to content

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

fix(logpanel): keep header indicator math consistent across Load older (#5181)

S5fccdbc981ae
diff --git a/src/local_deep_research/security/module_whitelist.py b/src/local_deep_research/security/module_whitelist.py
index e42e8e374b..badf23ed2b 100644
--- a/src/local_deep_research/security/module_whitelist.py
+++ b/src/local_deep_research/security/module_whitelist.py
@@ -240,5 +240,8 @@ def get_safe_module_class(
)
raise
- logger.debug(f"Successfully loaded {class_name} from {module_path}")
+ # Successful imports are routine and may occur several times while policy
+ # and factory layers validate one engine. Keep denials at warning/error,
+ # but do not persist this success chatter into research logs.
+ logger.trace(f"Successfully loaded {class_name} from {module_path}")
return engine_class
diff --git a/src/local_deep_research/web/routes/research_routes.py b/src/local_deep_research/web/routes/research_routes.py
index ef74a80e10..37e57c1395 100644
--- a/src/local_deep_research/web/routes/research_routes.py
+++ b/src/local_deep_research/web/routes/research_routes.py
@@ -14,7 +14,7 @@ from flask import (
)
from loguru import logger
from ...settings.logger import log_settings
-from sqlalchemy import func
+from sqlalchemy import case, func
from sqlalchemy.orm import Session
# Security imports
@@ -1674,20 +1674,26 @@ def get_research_details(research_id):
def get_research_logs(research_id):
"""Get logs for a specific research.
- Accepts an optional ``?limit=N`` that bounds the response to the newest
- ``N`` rows (returned oldest-first, matching the default ordering) and is
+ Accepts an optional ``?limit=N`` that bounds the response to ``N`` rows
+ (returned oldest-first, matching the default ordering) and is
clamped to ``[1, HISTORY_LOGS_HARD_CAP]`` so a client cannot force an
unbounded load — a long langgraph run can persist thousands of rows. When
``?limit`` is absent or not a valid integer (Flask ``type=int`` yields
``None``) the historical contract is preserved: every row is returned. The
- frontend log panel always sends a valid limit; this only affects direct
- API callers that omit or malform one.
+ frontend log panel always sends a valid limit and
+ ``priority=diagnostic``. That mode does a best-effort newest-first
+ selection prioritizing errors, then warnings, then milestones, before
+ filling the remaining window with routine rows. If the number of higher-
+ priority rows exceeds the limit, the oldest diagnostics in those categories
+ and all lower-priority/routine rows are dropped.
+ Direct API callers retain newest-N behavior unless they opt in.
"""
username = session["username"]
limit = request.args.get("limit", type=int)
if limit is not None:
limit = max(1, min(limit, HISTORY_LOGS_HARD_CAP))
+ prioritize_diagnostics = request.args.get("priority") == "diagnostic"
try:
# First check if the research exists
@@ -1708,6 +1714,23 @@ def get_research_logs(research_id):
log_results = log_query.order_by(
ResearchLog.timestamp, ResearchLog.id
).all()
+ elif prioritize_diagnostics:
+ diagnostic_priority = case(
+ (func.lower(ResearchLog.level) == "error", 0),
+ (func.lower(ResearchLog.level) == "warning", 1),
+ (func.lower(ResearchLog.level) == "milestone", 2),
+ else_=3,
+ )
+ log_results = (
+ log_query.order_by(
+ diagnostic_priority,
+ ResearchLog.timestamp.desc(),
+ ResearchLog.id.desc(),
+ )
+ .limit(limit)
+ .all()
+ )
+ log_results.sort(key=lambda row: (row.timestamp, row.id))
else:
# Take the newest ``limit`` rows at the SQL layer, then flip
# back to oldest-first so the response ordering is unchanged.
diff --git a/src/local_deep_research/web/static/js/components/logpanel.js b/src/local_deep_research/web/static/js/components/logpanel.js
index 63cd084ab9..889bfd011d 100644
--- a/src/local_deep_research/web/static/js/components/logpanel.js
+++ b/src/local_deep_research/web/static/js/components/logpanel.js
@@ -25,6 +25,7 @@
// 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 ||= {
@@ -52,6 +53,10 @@
// 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.
@@ -89,7 +94,13 @@
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();
window._logPanelState._countRequestGen =
(window._logPanelState._countRequestGen || 0) + 1;
if (previousResearchId && previousResearchId !== researchId) {
@@ -156,6 +167,8 @@
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;
@@ -271,6 +284,8 @@
} 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
@@ -316,6 +331,8 @@
addLogEntryToPanel(logEntry, false);
});
window._logPanelState.queuedLogs = [];
+ recomputeCountersFromDom();
+ updateLogCountIndicator(consoleLogContainer);
}
}
@@ -534,7 +551,9 @@
// (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 response = await fetch(URLBuilder.researchLogs(researchId, limit));
+ const url = URLBuilder.researchLogs(researchId, limit);
+ const separator = url.includes('?') ? '&' : '?';
+ const response = await fetch(`${url}${separator}priority=diagnostic`);
return await response.json();
}
@@ -683,6 +702,7 @@
// 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 {
@@ -722,6 +742,7 @@
// 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();
@@ -731,6 +752,7 @@
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
@@ -769,13 +791,13 @@
}
}
- // Collapse only repeated info messages within one
- // minute. Keep the previous-type check so an info
+ // 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 (logType === 'info' && seenMessages.has(messageKey)) {
+ if (COLLAPSIBLE_LOG_TYPES.has(logType) && seenMessages.has(messageKey)) {
const previousLog = seenMessages.get(messageKey);
- if (previousLog.type === 'info') {
+ 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
@@ -785,6 +807,8 @@
if (currentTime > previousTime) {
previousLog.time = logItem.time;
}
+ previousLog.repeatCount =
+ (previousLog.repeatCount || 1) + 1;
return; // Skip this duplicate
}
}
@@ -817,6 +841,7 @@
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
@@ -842,11 +867,11 @@
const messageKey = normalizeMessage(log.message || log.content || '');
- // Collapse only repeated info messages within one minute, and
- // only when the previously-seen entry is also info.
- if (logType === 'info' && seenMessages.has(messageKey)) {
+ // 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 === 'info') {
+ 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
@@ -856,6 +881,8 @@
if (currentTime > previousTime) {
previousLog.time = log.timestamp || log.time;
}
+ previousLog.repeatCount =
+ (previousLog.repeatCount || 1) + 1;
return; // Skip this duplicate
}
}
@@ -887,6 +914,7 @@
allLogs.push(logEntry);
});
}
+ window._logPanelState.fetchedLogs = fetchedLogs;
const panelContent = document.getElementById('log-panel-content') || document.getElementById('logPanel');
@@ -904,6 +932,8 @@
if (panelContent) {
delete panelContent.dataset.loaded;
}
+ recomputeCountersFromDom();
+ updateLogCountIndicator(logContent);
return;
}
@@ -1005,6 +1035,8 @@
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) {
@@ -1086,6 +1118,7 @@
// Get message
const message = logEntry.message || logEntry.content || 'No message';
+ const repeatCount = Math.max(1, Number(logEntry.repeatCount) || 1);
let element;
@@ -1099,7 +1132,7 @@
element.dataset.logType = logLevel.toLowerCase();
element.classList.add(`ldr-log-${logLevel.toLowerCase()}`);
// Initialize counter for duplicate tracking
- element.dataset.counter = '1';
+ element.dataset.counter = String(repeatCount);
// Store log ID for deduplication
if (logEntry.id) {
element.dataset.logId = logEntry.id;
@@ -1129,7 +1162,7 @@
element.className = 'ldr-console-log-entry';
element.dataset.logType = logLevel.toLowerCase();
element.classList.add(`ldr-log-${logLevel.toLowerCase()}`);
- element.dataset.counter = '1';
+ element.dataset.counter = String(repeatCount);
if (logEntry.id) {
element.dataset.logId = logEntry.id;
}
@@ -1147,6 +1180,13 @@
`;
}
+ 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';
@@ -1222,10 +1262,9 @@
// 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 only ever applies to info entries:
- // - info is high-volume / repetitive by nature ("status: ok",
- // "heartbeat") so folding identical repeats into a (N×) badge
- // actually reduces noise without losing signal.
+ // 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
@@ -1235,7 +1274,7 @@
const message = logEntry.message || logEntry.content || '';
const logType = (logEntry.type || 'info').toLowerCase();
- if (logType !== 'info') {
+ 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.
@@ -1249,7 +1288,7 @@
const entryType = entry.dataset.logType;
// If message and type match, consider it a duplicate
- // (only ever reached for info entries; the outer
+ // (only ever reached for info/debug entries; the outer
// if/else above already short-circuited warnings,
// errors, and milestones).
if (entryMessage === message &&
@@ -1339,8 +1378,7 @@
/**
* 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 rendered slice (issue #4878 — user sees "500" with
- * no indication they're looking at ~5% of a 9,002-row run).
+ * total exceeds the raw row count returned by the latest request.
*
* Layout:
* <span class="ldr-log-indicator" id="log-indicator">500</span>
@@ -1378,17 +1416,38 @@
? containerEl.querySelectorAll('.ldr-console-log-entry').length
: 0;
const total = window._logPanelState.totalLogs;
+ const fetched = window._logPanelState.fetchedLogs;
const truncated =
typeof total === 'number' &&
- total > rendered;
-
- // Write the rendered count (the "X" in "X of Y"). The formatNumber
- // helper groups thousands so a 9,002-row run renders as "9,002"
- // instead of "9002".
- const renderedLabel = formatNumber(rendered);
+ 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);
logIndicators.forEach(indicator => {
- indicator.textContent = renderedLabel;
+ indicator.textContent = indicatorLabel;
});
+ // 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
@@ -1489,7 +1548,7 @@
* call before the filter buttons are rendered: the querySelectorAll
* returns an empty NodeList and the loop is a no-op.
*/
- function updateFilterCounters() {
+ function updateFilterCounters(allCount) {
const counts = window._logPanelState.counts || {};
// Defensive floor: the bulk-merge path in loadLogsForResearch
// recomputes counts from the DOM (see recomputeCountersFromDom),
@@ -1511,13 +1570,18 @@
// (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.
- const indicatorEl = document.querySelector('.ldr-log-indicator');
- const total = indicatorEl
- ? safe(parseInt(indicatorEl.textContent, 10) || 0)
- : safe(counts.info || 0) +
- safe(counts.milestone || 0) +
- safe(counts.warning || 0) +
- safe(counts.error || 0);
+ 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;
@@ -1560,17 +1624,31 @@
// increments stay conditional on the bucket being tracked
// so unknown types don't pollute the filter badges.
total++;
- if (counts[t] !== undefined) {
+ if (Object.prototype.hasOwnProperty.call(counts, t)) {
counts[t]++;
}
});
+ // 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).
document.querySelectorAll('.ldr-log-indicator').forEach((indicator) => {
- indicator.textContent = String(total);
+ indicator.textContent = String(reportedTotal);
});
- updateFilterCounters();
+ updateFilterCounters(reportedTotal);
}
/**
diff --git a/tests/js/components/logpanel.test.js b/tests/js/components/logpanel.test.js
index dcd43b63d9..eb85ba11c8 100644
--- a/tests/js/components/logpanel.test.js
+++ b/tests/js/components/logpanel.test.js
@@ -81,6 +81,7 @@ beforeEach(() => {
window._logPanelState.initialized = false;
window._logPanelState.connectedResearchId = null;
window._logPanelState.totalLogs = null;
+ window._logPanelState.fetchedLogs = null;
window._logPanelState.renderedLimit = null;
}
});
@@ -207,6 +208,29 @@ describe('loadLogsForResearch — empty API response', () => {
// Empty response must leave dataset.loaded unset so a retry can happen.
expect(panelContent.dataset.loaded).toBeUndefined();
});
+
+ it('resets stale category counters when an empty response leaves only a placeholder', async () => {
+ const indicator = document.createElement('span');
+ indicator.className = 'ldr-log-indicator';
+ indicator.textContent = '9';
+ document.getElementById('log-panel-toggle').appendChild(indicator);
+ const badge = document.createElement('span');
+ badge.className = 'ldr-filter-count';
+ badge.dataset.filterCount = 'warning';
+ badge.textContent = '9';
+ document.body.appendChild(badge);
+ window._logPanelState.counts.warning = 9;
+
+ globalThis.fetch = vi.fn(() =>
+ Promise.resolve({ json: () => Promise.resolve([]) })
+ );
+
+ await logPanel.loadLogs('test-research-empty-counters');
+
+ expect(window._logPanelState.counts).toEqual(emptyCounts());
+ expect(indicator.textContent).toBe('0');
+ expect(badge.textContent).toBe('0');
+ });
});
describe('loadLogsForResearch — non-empty API response', () => {
@@ -313,6 +337,21 @@ describe('loadLogsForResearch — in-flight deduplication', () => {
// 2 fetch calls per loadLogs (log_count + logs).
expect(globalThis.fetch).toHaveBeenCalledTimes(2);
});
+
+ it('resets stale counters when a load error replaces the log DOM', async () => {
+ const indicator = document.createElement('span');
+ indicator.className = 'ldr-log-indicator';
+ indicator.textContent = '4';
+ document.getElementById('log-panel-toggle').appendChild(indicator);
+ window._logPanelState.counts.error = 4;
+ globalThis.fetch = vi.fn(() => Promise.reject(new Error('net down')));
+
+ await logPanel.loadLogs('test-research-error-counters');
+
+ expect(document.querySelector('.ldr-error-message')).not.toBeNull();
+ expect(window._logPanelState.counts).toEqual(emptyCounts());
+ expect(indicator.textContent).toBe('0');
+ });
});
describe('addConsoleLog — placeholder removal', () => {
@@ -1120,6 +1159,9 @@ describe('queued logs', () => {
});
it('drains the queue when the panel is expanded', () => {
+ globalThis.fetch = vi.fn(() =>
+ Promise.resolve({ json: () => Promise.resolve([]) })
+ );
setupPanelDom({ page: 'progress' });
// Pre-seed a queued entry, simulating a log that arrived while the
// panel was still collapsed.
@@ -1137,6 +1179,8 @@ describe('queued logs', () => {
expect(window._logPanelState.queuedLogs.length).toBe(0);
const container = document.getElementById('console-log-container');
expect(container.querySelector('.ldr-console-log-entry')).not.toBeNull();
+ expect(window._logPanelState.counts.info).toBe(1);
+ expect(document.querySelector('[data-filter-count="info"]').textContent).toBe('1');
});
it('bypasses the queue when the panel is already expanded', () => {
@@ -1377,6 +1421,31 @@ describe('log count indicator — persisted total and Load older', () => {
expect(document.querySelector('.ldr-load-older')).not.toBeNull();
});
+ it('does not offer Load older when all rows were fetched but routine duplicates were grouped', async () => {
+ const researchId = 'log-count-grouped';
+ const indicator = addLogIndicator(researchId);
+ const repeatedLogs = [
+ { id: 1, timestamp: '2026-05-08T12:00:00Z', message: 'routine debug', log_type: 'debug' },
+ { id: 2, timestamp: '2026-05-08T12:00:01Z', message: 'routine debug', log_type: 'debug' },
+ ];
+ mockLogFetch(2, repeatedLogs);
+
+ await logPanel.loadLogs(researchId);
+
+ // Indicator reports the server-known fetched count so it stays
+ // consistent with the "X of Y" header across Load older clicks —
+ // a run whose visible DOM is smaller than the server row count
+ // (because identical messages are folded into (N×) badges)
+ // would otherwise show "1 of 2" and lose its 473-row delta on
+ // the next click (LearningCircuit review, 2026-07-22, run
+ // a96e85ed: 353 -> 515 didn't add up to 973).
+ expect(indicator.textContent).toBe('2');
+ expect(document.querySelector('.ldr-duplicate-counter').textContent).toBe('(2×)');
+ expect(document.querySelector('.ldr-log-of-total')).toBeNull();
+ expect(document.querySelector('.ldr-load-older')).toBeNull();
+ expect(window._logPanelState.fetchedLogs).toBe(2);
+ });
+
it('degrades to the rendered count when the persisted total cannot be fetched', async () => {
const researchId = 'log-count-unavailable';
const indicator = addLogIndicator(researchId);
@@ -1409,7 +1478,7 @@ describe('log count indicator — persisted total and Load older', () => {
loadOlder.click();
await vi.waitFor(() => {
expect(fetchSpy).toHaveBeenCalledWith(
- `/api/research/${researchId}/logs?limit=5000`
+ `/api/research/${researchId}/logs?limit=5000&priority=diagnostic`
);
expect(
document.querySelectorAll('.ldr-console-log-entry').length
@@ -1456,7 +1525,7 @@ describe('log count indicator — persisted total and Load older', () => {
loadOlder.click();
await vi.waitFor(() => {
expect(fetchSpy).toHaveBeenCalledWith(
- `/api/research/${researchId}/logs?limit=5000`
+ `/api/research/${researchId}/logs?limit=5000&priority=diagnostic`
);
});
@@ -1532,6 +1601,7 @@ describe('log count indicator — persisted total and Load older', () => {
expect(document.querySelector('.ldr-load-older')).toBeNull();
expect(window._logPanelState.connectedResearchId).toBe(researchB);
expect(window._logPanelState.totalLogs).toBeNull();
+ expect(window._logPanelState.counts).toEqual(emptyCounts());
// Generation must have been bumped so any in-flight count
// response for A is treated as stale.
expect(typeof window._logPanelState._countRequestGen).toBe('number');
@@ -1744,6 +1814,65 @@ describe('log count indicator — persisted total and Load older', () => {
expect(panelContent.dataset.loaded).toBe('true');
expect(panelContent.dataset.loading).toBeUndefined();
});
+
+ it('does not corrupt the All filter badge when the log count exceeds 1,000 and uses comma formatting', async () => {
+ // Regression test for blocker 1: updateLogCountIndicator writes a comma-grouped
+ // label like "9,002" to .ldr-log-indicator, and updateFilterCounters must not
+ // truncate this to 9 when updating the All badge.
+ setupPanelDom({ researchId: null });
+
+ const researchId = 'log-count-large-run';
+ const indicator = addLogIndicator(researchId);
+ // Mock a large run with 9,002 logs. Initial fetch retrieves 2 logs.
+ const fetchSpy = mockLogFetch(9002, makeLogs(2), makeLogs(4));
+
+ await logPanel.loadLogs(researchId);
+
+ // Click "Load older" to load more logs.
+ const loadOlder = document.querySelector('.ldr-load-older');
+ expect(loadOlder).not.toBeNull();
+
+ loadOlder.click();
+ await vi.waitFor(() => {
+ expect(fetchSpy).toHaveBeenCalledWith(
+ `/api/research/${researchId}/logs?limit=5000&priority=diagnostic`
+ );
+ });
+
+ // The indicator reads "4" since 4 were fetched (and less than total).
+ // Let's manually trigger updateLogCountIndicator with a total that is formatted with a comma in the indicator.
+ // We can do this by setting window._logPanelState.fetchedLogs = 9002 so it floors the indicator value at Math.max(rendered, fetched) which is 9002.
+ window._logPanelState.fetchedLogs = 9002;
+ logPanel.addLog('trigger-indicator-refresh', 'info');
+
+ // Header indicator should read "9,002"
+ expect(indicator.textContent).toBe('9,002');
+ // The All filter badge should correctly read "9002" (as string/integer), not "9"
+ const allBadge = document.querySelector('.ldr-filter-count[data-filter-count="all"]');
+ expect(allBadge.textContent).toBe('9002');
+ });
+
+ it('preserves the "of Y" and "Load older" controls in transient states when fetchedLogs is null', async () => {
+ setupPanelDom({ researchId: null });
+
+ const researchId = 'log-count-transient-null';
+ const indicator = addLogIndicator(researchId);
+ mockLogFetch(9002, makeLogs(2));
+
+ await logPanel.loadLogs(researchId);
+
+ // Before transient state: controls are visible
+ expect(document.querySelector('.ldr-log-of-total')).not.toBeNull();
+ expect(document.querySelector('.ldr-load-older')).not.toBeNull();
+
+ // Simulate transient state: fetchedLogs is set to null
+ window._logPanelState.fetchedLogs = null;
+ logPanel.addLog('transient-socket-log', 'info');
+
+ // The controls should still be visible because totalLogs (9002) > rendered (3)
+ expect(document.querySelector('.ldr-log-of-total')).not.toBeNull();
+ expect(document.querySelector('.ldr-load-older')).not.toBeNull();
+ });
});
describe('loadLogsForResearch — counter drift when live entries already exist', () => {
diff --git a/tests/security/test_module_whitelist_high_value.py b/tests/security/test_module_whitelist_high_value.py
index 1a8cdd3bd7..cb1eaf9543 100644
--- a/tests/security/test_module_whitelist_high_value.py
+++ b/tests/security/test_module_whitelist_high_value.py
@@ -234,3 +234,54 @@ class TestGetSafeModuleClass:
get_safe_module_class(
".engines.search_engine_brave", "BraveSearchEngine"
)
+
+ def test_success_log_emitted_below_debug(self):
+ """Successful imports must log below the database sink's DEBUG
+ floor so the routine noise does not get persisted into research
+ logs. The database sink only persists levels ≥ DEBUG, so dropping
+ the level to TRACE keeps these lines out of /api/research/<id>/logs.
+
+ Note: both patches use ``with patch`` (not decorators) and
+ ``logger`` is patched first. ``unittest.mock`` silently fails to
+ replace ``module_whitelist.logger`` when ``module_whitelist
+ .importlib.import_module`` is already being patched in the
+ same scope — the import-module patch interacts with the
+ module's namespace lookups in a way that breaks the subsequent
+ attribute assignment. Inverting the order (logger first, then
+ import_module) avoids the interaction."""
+ mock_module = MagicMock()
+ mock_module.BraveSearchEngine = type("BraveSearchEngine", (), {})
+
+ with patch(
+ "local_deep_research.security.module_whitelist.logger"
+ ) as mock_logger:
+ with patch(
+ "local_deep_research.security.module_whitelist.importlib.import_module"
+ ) as mock_import:
+ mock_import.return_value = mock_module
+ get_safe_module_class(
+ ".engines.search_engine_brave", "BraveSearchEngine"
+ )
+
+ # The "successfully loaded" line must be logged at trace level, not
+ # debug, so it cannot be persisted to ResearchLog.
+ trace_calls = [
+ call_args
+ for call_args in mock_logger.trace.call_args_list
+ if call_args.args
+ and "Successfully loaded BraveSearchEngine" in call_args.args[0]
+ ]
+ debug_calls = [
+ call_args
+ for call_args in mock_logger.debug.call_args_list
+ if call_args.args
+ and "Successfully loaded BraveSearchEngine" in call_args.args[0]
+ ]
+ assert trace_calls, (
+ "Successful import must be logged at trace level so it is below"
+ " the database sink's DEBUG floor"
+ )
+ assert not debug_calls, (
+ "Successful import must NOT be logged at debug level — that would"
+ " get persisted to ResearchLog"
+ )
diff --git a/tests/web/routes/test_research_routes.py b/tests/web/routes/test_research_routes.py
index 8406adaf7b..acb922f2a4 100644
--- a/tests/web/routes/test_research_routes.py
+++ b/tests/web/routes/test_research_routes.py
@@ -514,6 +514,84 @@ class TestGetResearchLogsApi:
finally:
self._close_session(session)
+ def test_diagnostic_priority_preserves_old_warnings_and_errors(
+ self, authenticated_client
+ ):
+ """The log-panel window keeps diagnostics even when they predate the
+ newest routine rows, then returns the selected rows oldest-first."""
+ from local_deep_research.database.models import ResearchLog
+
+ session = self._seed_real_session(10)
+ try:
+ rows = session.query(ResearchLog).order_by(ResearchLog.id).all()
+ rows[0].level = "WARNING"
+ rows[1].level = "ERROR"
+ session.commit()
+
+ resp = self._get_logs(
+ authenticated_client,
+ session,
+ "?limit=4&priority=diagnostic",
+ )
+
+ assert resp.status_code == 200, resp.status_code
+ messages = [r["message"] for r in resp.get_json()]
+ assert messages == ["Log 0", "Log 1", "Log 8", "Log 9"]
+ finally:
+ self._close_session(session)
+
+ def test_diagnostic_priority_milestones(self, authenticated_client):
+ """Verifies that milestone logs are prioritized above routine logs."""
+ from local_deep_research.database.models import ResearchLog
+
+ session = self._seed_real_session(10)
+ try:
+ rows = session.query(ResearchLog).order_by(ResearchLog.id).all()
+ rows[0].level = "MILESTONE"
+ session.commit()
+
+ resp = self._get_logs(
+ authenticated_client,
+ session,
+ "?limit=3&priority=diagnostic",
+ )
+
+ assert resp.status_code == 200, resp.status_code
+ messages = [r["message"] for r in resp.get_json()]
+ assert messages == ["Log 0", "Log 8", "Log 9"]
+ finally:
+ self._close_session(session)
+
+ def test_diagnostic_priority_overflow(self, authenticated_client):
+ """When a single tier (e.g. errors) alone exceeds limit, the oldest
+ errors and all other lower-priority categories are dropped."""
+ from local_deep_research.database.models import ResearchLog
+
+ session = self._seed_real_session(10)
+ try:
+ rows = session.query(ResearchLog).order_by(ResearchLog.id).all()
+ # Mark 5 errors
+ for i in range(5):
+ rows[i].level = "ERROR"
+ # Mark a warning that should get dropped because the limit is 3
+ rows[5].level = "WARNING"
+ session.commit()
+
+ resp = self._get_logs(
+ authenticated_client,
+ session,
+ "?limit=3&priority=diagnostic",
+ )
+
+ assert resp.status_code == 200, resp.status_code
+ messages = [r["message"] for r in resp.get_json()]
+ # Since errors are at the highest priority, and we have 5 errors,
+ # under limit=3, only the 3 newest errors (Log 2, Log 3, Log 4)
+ # are returned. The warning and routine logs are dropped.
+ assert messages == ["Log 2", "Log 3", "Log 4"]
+ finally:
+ self._close_session(session)
+
def test_limit_is_clamped_to_at_least_one(self, authenticated_client):
"""?limit=0 clamps up to 1 -> just the single newest row."""
session = self._seed_real_session(10)