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.

Clarify database_sink comment and test denial log persistence (#5354)

Ziyang Guo07ed7f03836a
diff --git a/src/local_deep_research/security/egress/policy.py b/src/local_deep_research/security/egress/policy.py
index ae9033100a..5a4f122673 100644
--- a/src/local_deep_research/security/egress/policy.py
+++ b/src/local_deep_research/security/egress/policy.py
@@ -1124,12 +1124,13 @@ def _record_denial(ctx: EgressContext, url: object, reason: str) -> Decision:
else:
audit_url = redact_url_for_log(url)
# The URL / reason / scope / count / counted must ride on the message
- # body, NOT as logger.warning kwargs. database_sink builds log_entry
- # only from record["message"] and the few fixed fields it copies
- # manually (module/function/line/level/timestamp/research_id) — it
- # never reads record["extra"], so any kwarg binding here is dropped
- # at the persistence boundary and never reaches app_logs.message
- # (or the JSONL exporter that reads it). The pinned
+ # body, NOT as logger.warning kwargs. database_sink persists
+ # record["message"] into ``app_logs.message`` verbatim and only copies a
+ # small fixed set of structured fields into the DB row. It *does* read
+ # selected routing metadata from ``record["extra"]`` (for example
+ # ``research_id`` / ``username``), but arbitrary extras or warning kwargs
+ # are not serialized into ``app_logs.message`` (or the JSONL exporter that
+ # reads it). The pinned
# ``test_record_denial_warning_message_carries_redacted_url`` and
# ``test_record_denial_warning_message_reaches_persisted_log`` tests
# assert this invariant.
diff --git a/tests/security/test_egress_url_edgecases_followup.py b/tests/security/test_egress_url_edgecases_followup.py
index 7bd09de49c..97f163f9fc 100644
--- a/tests/security/test_egress_url_edgecases_followup.py
+++ b/tests/security/test_egress_url_edgecases_followup.py
@@ -422,20 +422,18 @@ class TestDenialLogCarriesUrl:
assert "counted=True" in message
def test_record_denial_warning_message_reaches_persisted_log(self):
- """End-to-end: drive ``_record_denial`` for real and confirm the
- URL survives the path that produces ``research_logs_*.jsonl``:
- the WARNING message is what ``database_sink`` writes to
- ``app_logs.message``, so a triager reading the JSONL sees the URL.
-
- Previous fixes passed the URL as a ``logger.warning(..., url=...)``
- kwarg, which is dropped by ``database_sink`` because it does not
- consult ``record['extra']`` — that was the silent gap behind #5278.
- Inlining the URL into the message body is what makes the URL
- actually reach the persisted log; this test pins that contract.
- """
- from unittest.mock import MagicMock
+ """End-to-end: drive ``_record_denial`` to produce the WARNING text,
+ then pass that text through ``database_sink``'s direct-write path and
+ confirm the persisted ``app_logs.message`` still carries the redacted
+ URL. ``database_sink`` may read selected routing metadata from
+ ``record['extra']`` (for example ``username``), but the persisted log
+ body still comes from the positional message string itself."""
+ from datetime import datetime
+ from types import SimpleNamespace
+ from unittest.mock import MagicMock, Mock
from local_deep_research.security.egress import policy as policy_mod
+ from local_deep_research.utilities.log_utils import database_sink
bound = MagicMock()
with patch.object(policy_mod, "logger") as mock_logger:
@@ -447,13 +445,60 @@ class TestDenialLogCarriesUrl:
)
message = bound.warning.call_args[0][0]
+
+ mock_message = Mock()
+ mock_message.record = {
+ "time": datetime.now(),
+ "message": message,
+ "name": "local_deep_research.security.egress.policy",
+ "function": "_record_denial",
+ "line": 0,
+ "level": SimpleNamespace(name="WARNING"),
+ "extra": {
+ "research_id": "rid-denial",
+ "username": "triager",
+ },
+ }
+
+ mock_thread = Mock()
+ mock_thread.name = "MainThread"
+ mock_session = MagicMock()
+ mock_cm = MagicMock()
+ mock_cm.__enter__ = Mock(return_value=mock_session)
+ mock_cm.__exit__ = Mock(return_value=None)
+
+ with patch(
+ "local_deep_research.utilities.log_utils.has_app_context",
+ return_value=True,
+ ):
+ with patch(
+ "local_deep_research.utilities.log_utils.g",
+ SimpleNamespace(),
+ ):
+ with patch(
+ "local_deep_research.utilities.log_utils.threading.current_thread",
+ return_value=mock_thread,
+ ):
+ with patch(
+ "local_deep_research.database.session_context.get_user_db_session",
+ return_value=mock_cm,
+ ) as mock_get_session:
+ database_sink(mock_message)
+
+ mock_get_session.assert_called_once()
+ assert mock_get_session.call_args.args[0] == "triager"
+ persisted = mock_session.add.call_args[0][0]
+ assert persisted.research_id == "rid-denial"
+ assert persisted.message == message
assert (
- "url=https://southasiascholaractivistcollective.org" in message
+ "url=https://southasiascholaractivistcollective.org"
+ in persisted.message
), (
"Inlined URL must reach the message body that database_sink "
"persists into app_logs.message (and the JSONL exporter "
- "emits). Got: " + repr(message)
+ "emits). Got: " + repr(persisted.message)
)
+ mock_session.commit.assert_called_once()
@pytest.mark.parametrize(
"bad_url",
diff --git a/tests/utilities/test_log_utils.py b/tests/utilities/test_log_utils.py
index 943566dd3b..ee54d7f3b3 100644
--- a/tests/utilities/test_log_utils.py
+++ b/tests/utilities/test_log_utils.py
@@ -223,6 +223,8 @@ class TestDatabaseSink:
def test_writes_to_database_in_main_thread(self):
"""Should write to database when in main thread with app context."""
+ from types import SimpleNamespace
+
from local_deep_research.utilities.log_utils import database_sink
import local_deep_research.utilities.log_utils as module
@@ -245,8 +247,7 @@ class TestDatabaseSink:
mock_cm.__enter__ = Mock(return_value=mock_session)
mock_cm.__exit__ = Mock(return_value=None)
- mock_g = Mock()
- mock_g.get.return_value = None
+ mock_g = SimpleNamespace()
with patch.object(module, "has_app_context", return_value=True):
with patch.object(module, "g", mock_g):
@@ -256,10 +257,16 @@ class TestDatabaseSink:
with patch(
"local_deep_research.database.session_context.get_user_db_session",
return_value=mock_cm,
- ):
+ ) as mock_get_session:
database_sink(mock_message)
# Should write to database
+ mock_get_session.assert_called_once()
+ assert mock_get_session.call_args.args[0] == "testuser"
+ assert (
+ mock_get_session.call_args.kwargs["password"]
+ is None
+ )
mock_session.add.assert_called_once()
mock_session.commit.assert_called_once()