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(logging): cap log message size sent to the frontend (#4004)

LearningCircuitd33653f37d99
diff --git a/src/local_deep_research/utilities/log_utils.py b/src/local_deep_research/utilities/log_utils.py
index a72b6ede1b..1c3c87e586 100644
--- a/src/local_deep_research/utilities/log_utils.py
+++ b/src/local_deep_research/utilities/log_utils.py
@@ -36,6 +36,17 @@ _stop_queue = threading.Event()
Default log directory to use.
"""
+# Cap how much of a single log record's message we ship to the browser over
+# socket.io. Some diagnostic log lines (e.g. ``[FETCH] page_text``) inline
+# the full extracted page body — up to ~10 KB per call — which is useless
+# in the UI (a single massive blob fills the viewport) and inflates both
+# wire traffic and client-side state. Container-log/stderr, file, and DB
+# sinks remain unchanged, so full diagnostics are preserved for grep/DB
+# queries. The cap bounds the *prefix* preserved from the original message;
+# the wire payload is the prefix plus a short truncation indicator (~100
+# bytes), so it can exceed this value by that fixed overhead.
+FRONTEND_MESSAGE_MAX_LENGTH = 2000
+
class InterceptHandler(logging.Handler):
"""
@@ -366,6 +377,28 @@ def database_sink(message: loguru.Message) -> None:
_write_log_to_database(log_entry)
+def _truncate_for_frontend(message: str) -> str:
+ """Bound the wire size of an outbound log message.
+
+ ``FRONTEND_MESSAGE_MAX_LENGTH`` caps the *preserved prefix* of the
+ original message. When truncation kicks in, a short indicator is
+ appended that names the original length and points the user at the
+ server-side logs for the full text, so the returned string is
+ ``FRONTEND_MESSAGE_MAX_LENGTH`` plus the fixed indicator overhead
+ (~100 bytes). Verbose diagnostic logs (e.g. ``[FETCH] page_text``
+ which inlines the full extracted page body) are useless in the UI
+ when displayed in full and inflate socket payloads + client-side
+ memory; container-log/stderr, file, and DB sinks remain unchanged.
+ """
+ if len(message) <= FRONTEND_MESSAGE_MAX_LENGTH:
+ return message
+ suffix = (
+ f"… (truncated; full message in server logs; "
+ f"original length: {len(message)} chars)"
+ )
+ return message[:FRONTEND_MESSAGE_MAX_LENGTH] + suffix
+
+
def frontend_progress_sink(message: loguru.Message) -> None:
"""
Sink that sends messages to the frontend.
@@ -383,7 +416,7 @@ def frontend_progress_sink(message: loguru.Message) -> None:
frontend_log = {
"log_entry": {
- "message": record["message"],
+ "message": _truncate_for_frontend(record["message"]),
"type": record["level"].name, # Keep original case
"time": record["time"].isoformat(),
},
diff --git a/tests/utilities/test_log_utils.py b/tests/utilities/test_log_utils.py
index 57fa800059..b1998d0c19 100644
--- a/tests/utilities/test_log_utils.py
+++ b/tests/utilities/test_log_utils.py
@@ -513,6 +513,102 @@ class TestFrontendProgressSink:
assert call_args[0][0] == "progress"
assert call_args[0][1] == "test-uuid"
+ def test_short_messages_pass_through_unchanged(self):
+ """Messages under the cap must reach the wire byte-for-byte."""
+ from local_deep_research.utilities.log_utils import (
+ FRONTEND_MESSAGE_MAX_LENGTH,
+ frontend_progress_sink,
+ )
+
+ short_msg = "small payload"
+ assert len(short_msg) < FRONTEND_MESSAGE_MAX_LENGTH
+ mock_message = Mock()
+ mock_message.record = {
+ "message": short_msg,
+ "level": Mock(name="INFO"),
+ "time": Mock(isoformat=Mock(return_value="2024-01-01T00:00:00")),
+ "extra": {"research_id": "rid"},
+ }
+ with (
+ patch(
+ "local_deep_research.utilities.log_utils._get_research_id",
+ return_value="rid",
+ ),
+ patch(
+ "local_deep_research.utilities.log_utils.SocketIOService"
+ ) as mock_socket,
+ ):
+ frontend_progress_sink(mock_message)
+ payload = mock_socket.return_value.emit_to_subscribers.call_args[0][2]
+ assert payload["log_entry"]["message"] == short_msg
+
+ def test_long_messages_truncated_with_indicator(self):
+ """Messages exceeding the cap must be truncated and carry a clear
+ indicator that points the user at the server logs for the full
+ text. Other log levels / sinks are unaffected by this sink."""
+ from local_deep_research.utilities.log_utils import (
+ FRONTEND_MESSAGE_MAX_LENGTH,
+ frontend_progress_sink,
+ )
+
+ big_msg = "X" * (FRONTEND_MESSAGE_MAX_LENGTH + 5000)
+ mock_message = Mock()
+ mock_message.record = {
+ "message": big_msg,
+ "level": Mock(name="INFO"),
+ "time": Mock(isoformat=Mock(return_value="2024-01-01T00:00:00")),
+ "extra": {"research_id": "rid"},
+ }
+ with (
+ patch(
+ "local_deep_research.utilities.log_utils._get_research_id",
+ return_value="rid",
+ ),
+ patch(
+ "local_deep_research.utilities.log_utils.SocketIOService"
+ ) as mock_socket,
+ ):
+ frontend_progress_sink(mock_message)
+
+ payload = mock_socket.return_value.emit_to_subscribers.call_args[0][2]
+ out = payload["log_entry"]["message"]
+ # Truncated to the cap plus indicator
+ assert out.startswith("X" * FRONTEND_MESSAGE_MAX_LENGTH)
+ assert "truncated" in out
+ assert str(len(big_msg)) in out # original length surfaced
+ # Sanity: cap + indicator is still much smaller than the original
+ assert len(out) < len(big_msg)
+
+ def test_message_at_exact_cap_is_not_truncated(self):
+ """A message of exactly ``FRONTEND_MESSAGE_MAX_LENGTH`` chars
+ must pass through unchanged — the cap is inclusive."""
+ from local_deep_research.utilities.log_utils import (
+ FRONTEND_MESSAGE_MAX_LENGTH,
+ frontend_progress_sink,
+ )
+
+ exact = "Y" * FRONTEND_MESSAGE_MAX_LENGTH
+ mock_message = Mock()
+ mock_message.record = {
+ "message": exact,
+ "level": Mock(name="INFO"),
+ "time": Mock(isoformat=Mock(return_value="2024-01-01T00:00:00")),
+ "extra": {"research_id": "rid"},
+ }
+ with (
+ patch(
+ "local_deep_research.utilities.log_utils._get_research_id",
+ return_value="rid",
+ ),
+ patch(
+ "local_deep_research.utilities.log_utils.SocketIOService"
+ ) as mock_socket,
+ ):
+ frontend_progress_sink(mock_message)
+ payload = mock_socket.return_value.emit_to_subscribers.call_args[0][2]
+ assert payload["log_entry"]["message"] == exact
+ assert "truncated" not in payload["log_entry"]["message"]
+
class TestFlushLogQueue:
"""Tests for flush_log_queue function."""