Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(memory/sqlite): match numeric and boolean metadata filters, not just strings (#3240)

Abhay Singhe91149646335
diff --git a/headroom/memory/adapters/sqlite.py b/headroom/memory/adapters/sqlite.py
index c5fcdf28c..4a3a0bae8 100644
--- a/headroom/memory/adapters/sqlite.py
+++ b/headroom/memory/adapters/sqlite.py
@@ -550,9 +550,22 @@ class SQLiteMemoryStore:
# while blocking malicious attempts like "'] OR 1=1--"
if not _validate_metadata_key(key):
continue
- # Use JSON extraction for metadata filtering
+ # Use JSON extraction for metadata filtering.
+ #
+ # ``json_extract`` returns a NATIVE SQLite value (INTEGER / REAL /
+ # TEXT), so a scalar filter must bind the native Python value.
+ # Binding ``json.dumps(value)`` instead compared the numeric/boolean
+ # column against its text form ("5", "true") — and SQLite never
+ # equates ``5 = '5'`` — so an int/float/bool metadata filter matched
+ # nothing. ``bool`` is a subclass of ``int``, so it is covered here
+ # (a JSON ``true`` extracts to 1, and Python ``True`` binds to 1).
+ # A non-scalar value (dict/list) is not a bindable SQLite type, so it
+ # keeps the JSON-text comparison rather than raising.
conditions.append(f"json_extract(metadata, '$.{key}') = ?")
- params.append(json.dumps(value) if not isinstance(value, str) else value)
+ if isinstance(value, (str, int, float)):
+ params.append(value)
+ else:
+ params.append(json.dumps(value))
return conditions, params
diff --git a/tests/test_memory/test_query_conditions.py b/tests/test_memory/test_query_conditions.py
index 83b38ad68..f68910116 100644
--- a/tests/test_memory/test_query_conditions.py
+++ b/tests/test_memory/test_query_conditions.py
@@ -38,3 +38,37 @@ def test_agent_id_only_still_applied():
assert "agent_id = ?" in conditions
assert "turn_id = ?" not in conditions
+
+
+def test_metadata_scalar_filters_bind_native_values():
+ """A metadata filter on a numeric/boolean value must bind the NATIVE value.
+
+ ``json_extract`` returns a native SQLite value, so binding ``json.dumps(value)``
+ ("5", "true") compared the column against text and matched nothing (SQLite
+ never equates ``5 = '5'``). Scalars must be bound as-is; only non-scalar
+ (dict/list) values keep the JSON-text form.
+ """
+ conditions, params = _conditions(
+ user_id="u",
+ metadata_filters={"priority": 5, "archived": True, "score": 1.5, "tag": "x"},
+ )
+
+ assert any("json_extract(metadata, '$.priority')" in c for c in conditions)
+ # Native scalars, not their json.dumps text forms.
+ assert 5 in params and "5" not in params
+ assert 1.5 in params
+ assert "x" in params
+ # bool binds as its native value (a JSON ``true`` extracts to 1).
+ assert True in params
+ assert "true" not in params
+
+
+def test_metadata_non_scalar_filter_keeps_json_text():
+ """A dict/list metadata value is not a bindable SQLite type, so it keeps the
+ JSON-text comparison rather than raising when the query runs."""
+ import json
+
+ conditions, params = _conditions(user_id="u", metadata_filters={"tags": ["a", "b"]})
+
+ assert any("json_extract(metadata, '$.tags')" in c for c in conditions)
+ assert json.dumps(["a", "b"]) in params