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(settings): filter malformed rows on read so they can't wrap a leaf (#4840) (#4946)

LearningCircuit997ab889399a
diff --git a/changelog.d/4946.bugfix.md b/changelog.d/4946.bugfix.md
new file mode 100644
index 0000000000..2b34ddf33e
--- /dev/null
+++ b/changelog.d/4946.bugfix.md
@@ -0,0 +1 @@
+Both settings prefix-read paths — `SettingsManager.get_setting` (DB) and `get_setting_from_snapshot` (the snapshot path used by research threads) — now filter out malformed rows (e.g. a legacy `foo..` or `foo. ` key), so a stray malformed row can no longer turn a leaf read into a `{".": value}` wrapper dict that renders as `[object Object]`. Completes the read side of #4840 beyond #4852's exact single-trailing-dot exclusion; existing malformed rows remain harmless and are not deleted.
diff --git a/src/local_deep_research/config/thread_settings.py b/src/local_deep_research/config/thread_settings.py
index 05c0a3a320..59217d0a5c 100644
--- a/src/local_deep_research/config/thread_settings.py
+++ b/src/local_deep_research/config/thread_settings.py
@@ -7,7 +7,7 @@ shared across different modules to maintain settings context in threads.
import threading
from contextlib import contextmanager
-from ..settings.manager import get_typed_setting_value
+from ..settings.manager import get_typed_setting_value, is_valid_setting_key
from ..utilities.type_utils import to_bool
@@ -105,19 +105,26 @@ def get_setting_from_snapshot(
value = raw
# Search for child keys.
elif settings_snapshot:
- for k, v in settings_snapshot.items():
- if k.startswith(f"{key}."):
- k = k.removeprefix(f"{key}.")
- # Handle both full format {"value": x} and simplified format (just x)
- if isinstance(v, dict) and "value" in v:
- v = get_typed_setting_value(
- k, v["value"], v.get("ui_element", "text")
- )
- # else: v is already the raw value from simplified snapshot
- if value is _NOT_FOUND:
- value = {k: v}
- else:
- value[k] = v
+ for full_key, v in settings_snapshot.items():
+ if not full_key.startswith(f"{key}."):
+ continue
+ # Skip malformed rows (e.g. legacy "foo." / "foo..") so a stray
+ # legacy key can't wrap a leaf read into an [object Object] dict
+ # (#4840) — mirrors SettingsManager.get_setting's filter on the DB
+ # read path.
+ if not is_valid_setting_key(full_key):
+ continue
+ child = full_key.removeprefix(f"{key}.")
+ # Handle both full format {"value": x} and simplified format (just x)
+ if isinstance(v, dict) and "value" in v:
+ v = get_typed_setting_value(
+ child, v["value"], v.get("ui_element", "text")
+ )
+ # else: v is already the raw value from simplified snapshot
+ if value is _NOT_FOUND:
+ value = {child: v}
+ else:
+ value[child] = v
if value is not _NOT_FOUND:
# Extract value from dict structure if needed
diff --git a/src/local_deep_research/settings/manager.py b/src/local_deep_research/settings/manager.py
index 3481b39e39..1a4dc6ec30 100644
--- a/src/local_deep_research/settings/manager.py
+++ b/src/local_deep_research/settings/manager.py
@@ -186,9 +186,11 @@ def is_valid_setting_key(key: Any) -> bool:
``get_setting("foo")`` return a ``{"": value}`` wrapper dict, which the
UI renders as ``[object Object]``.
- This is the write-side complement to the read-side prefix-query guard:
- reads tolerate any pre-existing malformed rows, while this stops new
- ones from being created.
+ Used on both sides: writes reject new malformed keys, and both read paths
+ (``SettingsManager.get_setting`` on the DB and ``get_setting_from_snapshot``
+ on a settings snapshot) filter malformed rows out of a prefix read so a
+ stray legacy row can never wrap a leaf value into an ``[object Object]``
+ dict.
"""
if not isinstance(key, str) or not key:
return False
@@ -640,6 +642,17 @@ class SettingsManager(ISettingsManager):
# If using database first approach and session available, check database
try:
settings = self.__query_settings(key)
+ # Drop malformed subkey rows (e.g. legacy "foo." / "foo.." keys)
+ # that slip past the SQL prefix filter. #4852 excludes only the
+ # exact single-trailing-dot row; a "foo.." / "foo. " row would
+ # otherwise turn a leaf read into a `{".": v}`-style wrapper dict
+ # rendered as `[object Object]` (#4840). The exact-match row is
+ # always kept so a direct read of a malformed key still works.
+ settings = [
+ s
+ for s in settings
+ if str(s.key) == key or is_valid_setting_key(str(s.key))
+ ]
if len(settings) == 1:
# This is a bottom-level key.
return self.__get_typed_setting_value(
diff --git a/tests/config/test_thread_settings_extended.py b/tests/config/test_thread_settings_extended.py
index f830a7a4fb..17da96090e 100644
--- a/tests/config/test_thread_settings_extended.py
+++ b/tests/config/test_thread_settings_extended.py
@@ -193,6 +193,28 @@ class TestGetSettingFromSnapshotChildKeys:
result = get_setting_from_snapshot("search", settings_snapshot=snapshot)
assert result == {"engine": "yes"}
+ def test_malformed_child_keys_are_skipped(self, clean_thread_local):
+ """Malformed child rows (legacy 'parent.' / 'parent..' / 'parent. ')
+ must not be aggregated — otherwise a stray key wraps a leaf read into
+ an [object Object] dict (#4840). Mirrors the DB read path."""
+ snapshot = {
+ "parent.real": "v",
+ "parent.": "junk", # trailing dot
+ "parent..": "junk", # empty segment
+ "parent. ": "junk", # whitespace segment
+ }
+ result = get_setting_from_snapshot("parent", settings_snapshot=snapshot)
+ assert result == {"real": "v"}
+
+ def test_only_malformed_child_returns_default(self, clean_thread_local):
+ """A prefix whose only matching row is malformed resolves to the
+ default, not a `{'.': ...}` wrapper dict."""
+ snapshot = {"parent..": "junk"}
+ result = get_setting_from_snapshot(
+ "parent", settings_snapshot=snapshot, default="fallback"
+ )
+ assert result == "fallback"
+
class TestGetSettingFromSnapshotThreadContext:
"""Tests for falling back to the thread-local settings context."""
diff --git a/tests/settings/test_settings_manager.py b/tests/settings/test_settings_manager.py
index 70e539fcec..88a72fcf35 100644
--- a/tests/settings/test_settings_manager.py
+++ b/tests/settings/test_settings_manager.py
@@ -2407,3 +2407,88 @@ class TestSettingsManagerPrefixQueriesRealDB:
assert result.get("temperature") == 0.7
# The key "llm." should NOT match as a subkey of "llm" (i.e. we should not have an empty string key in result)
assert "" not in result
+
+ def test_malformed_subkey_rows_dont_wrap_a_leaf_read(self, session):
+ """A pathological pre-existing malformed row (``foo..`` / ``foo. ``)
+ must not turn a leaf read into a wrapper dict rendered as
+ ``[object Object]``. #4852 excludes only the exact single-trailing-dot
+ row; this covers the residual double-dot / dot-space shapes (#4840)."""
+ from local_deep_research.database.models import Setting
+
+ session.add_all(
+ [
+ Setting(
+ key="local_search_chunk_size",
+ name="Chunk size",
+ value="1000",
+ ui_element="number",
+ type="SEARCH",
+ ),
+ # Double-dot: slips past #4852's `!= "foo."` exclusion.
+ Setting(
+ key="local_search_chunk_size..",
+ name="Malformed dd",
+ value="junk",
+ ui_element="text",
+ type="SEARCH",
+ ),
+ # Dot-space: also slips past #4852.
+ Setting(
+ key="local_search_chunk_size. ",
+ name="Malformed ds",
+ value="junk",
+ ui_element="text",
+ type="SEARCH",
+ ),
+ ]
+ )
+ session.commit()
+
+ manager = SettingsManager(db_session=session)
+ result = manager.get_setting("local_search_chunk_size")
+
+ # Resolves to the scalar value, not a `{".": ...}` wrapper dict.
+ assert not isinstance(result, dict)
+ assert result == 1000
+
+ def test_prefix_read_with_only_malformed_rows_returns_default(
+ self, session
+ ):
+ """A prefix whose only matching rows are malformed resolves to the
+ default — mirrors test_only_malformed_child_returns_default on the
+ snapshot path."""
+ from local_deep_research.database.models import Setting
+
+ session.add(
+ Setting(
+ key="orphan..",
+ name="Malformed only",
+ value="junk",
+ ui_element="text",
+ type="SEARCH",
+ )
+ )
+ session.commit()
+
+ manager = SettingsManager(db_session=session)
+ assert manager.get_setting("orphan", default="fallback") == "fallback"
+
+ def test_direct_read_of_malformed_key_still_works(self, session):
+ """The read filter's exact-match clause keeps a malformed row readable
+ by its own key (legacy rows stay accessible, just invisible to prefix
+ reads)."""
+ from local_deep_research.database.models import Setting
+
+ session.add(
+ Setting(
+ key="legacy. ",
+ name="Malformed direct",
+ value="direct",
+ ui_element="text",
+ type="SEARCH",
+ )
+ )
+ session.commit()
+
+ manager = SettingsManager(db_session=session)
+ assert manager.get_setting("legacy. ") == "direct"