Skip to content

Personal AI, On Personal Devices

Python

fix(knowledge): handle empty filter strings and guard top_k parsing in knowledge search (#1054)

Arush Wadhawan037df7113364
diff --git a/src/openjarvis/connectors/store.py b/src/openjarvis/connectors/store.py
index 2b6d6319..d269d66f 100644
--- a/src/openjarvis/connectors/store.py
+++ b/src/openjarvis/connectors/store.py
@@ -369,22 +369,40 @@ class KnowledgeStore(MemoryBackend):
return []
fts_query = " OR ".join(f'"{term}"' for term in terms)
- since_str = _to_iso(since) if since is not None else None
- until_str = _to_iso(until) if until is not None else None
+ def _clean_str(val: Optional[str]) -> Optional[str]:
+ if isinstance(val, str):
+ return val.strip() or None
+ return val
+
+ source_val = _clean_str(source)
+ doc_type_val = _clean_str(doc_type)
+ author_val = _clean_str(author)
+
+ def _clean_ts(val: Union[datetime, str, None]) -> Union[datetime, str, None]:
+ if isinstance(val, str):
+ s = val.strip()
+ return s if s else None
+ return val
+
+ since_val = _clean_ts(since)
+ until_val = _clean_ts(until)
+
+ since_str = _to_iso(since_val) if since_val is not None else None
+ until_str = _to_iso(until_val) if until_val is not None else None
# Build the WHERE clause for filter columns
filters: List[str] = []
params: List[Any] = []
- if source is not None:
+ if source_val is not None:
filters.append("kc.source = ?")
- params.append(source)
- if doc_type is not None:
+ params.append(source_val)
+ if doc_type_val is not None:
filters.append("kc.doc_type = ?")
- params.append(doc_type)
- if author is not None:
+ params.append(doc_type_val)
+ if author_val is not None:
filters.append("kc.author = ?")
- params.append(author)
+ params.append(author_val)
if since_str:
filters.append("kc.timestamp >= ?")
params.append(since_str)
@@ -415,9 +433,12 @@ class KnowledgeStore(MemoryBackend):
try:
rows = self._conn.execute(sql, [fts_query] + params + [top_k]).fetchall()
- except sqlite3.OperationalError:
- # Malformed FTS query — return empty rather than crash
- return []
+ except sqlite3.OperationalError as exc:
+ # Only malformed FTS expressions are empty searches. Other SQLite
+ # failures must reach callers rather than masquerade as no matches.
+ if str(exc).lower().startswith("fts5: syntax error"):
+ return []
+ raise
results: List[RetrievalResult] = []
for row in rows:
diff --git a/src/openjarvis/tools/knowledge_search.py b/src/openjarvis/tools/knowledge_search.py
index f35b554b..3525d570 100644
--- a/src/openjarvis/tools/knowledge_search.py
+++ b/src/openjarvis/tools/knowledge_search.py
@@ -109,32 +109,56 @@ class KnowledgeSearchTool(BaseTool):
success=False,
)
- top_k: int = int(params.get("top_k", 10))
- source: Optional[str] = params.get("source")
- doc_type: Optional[str] = params.get("doc_type")
- author: Optional[str] = params.get("author")
- since: Optional[str] = params.get("since")
- until: Optional[str] = params.get("until")
-
- if self._retriever is not None:
- results = self._retriever.retrieve(
- query,
- top_k=top_k,
- source=source or "",
- doc_type=doc_type or "",
- author=author or "",
- since=since or "",
- until=until or "",
- )
- else:
- results = self._store.retrieve( # type: ignore[union-attr]
- query,
- top_k=top_k,
- source=source,
- doc_type=doc_type,
- author=author,
- since=since,
- until=until,
+ raw_top_k = params.get("top_k")
+ try:
+ top_k: int = int(raw_top_k) if raw_top_k not in (None, "") else 10
+ except (ValueError, TypeError, OverflowError):
+ top_k = 10
+ top_k = max(1, top_k)
+
+ filters: dict[str, Optional[str]] = {}
+ for name in ("source", "doc_type", "author", "since", "until"):
+ value = params.get(name)
+ if value is not None and not isinstance(value, str):
+ return ToolResult(
+ tool_name="knowledge_search",
+ content=f"Invalid {name} filter: expected a string.",
+ success=False,
+ )
+ filters[name] = value.strip() or None if value else None
+
+ source = filters["source"]
+ doc_type = filters["doc_type"]
+ author = filters["author"]
+ since = filters["since"]
+ until = filters["until"]
+
+ try:
+ if self._retriever is not None:
+ results = self._retriever.retrieve(
+ query,
+ top_k=top_k,
+ source=source or "",
+ doc_type=doc_type or "",
+ author=author or "",
+ since=since or "",
+ until=until or "",
+ )
+ else:
+ results = self._store.retrieve( # type: ignore[union-attr]
+ query,
+ top_k=top_k,
+ source=source,
+ doc_type=doc_type,
+ author=author,
+ since=since,
+ until=until,
+ )
+ except Exception as exc:
+ return ToolResult(
+ tool_name="knowledge_search",
+ content=f"Knowledge search failed: {exc}",
+ success=False,
)
if not results:
diff --git a/tests/connectors/test_store.py b/tests/connectors/test_store.py
index 697cb65c..818f0934 100644
--- a/tests/connectors/test_store.py
+++ b/tests/connectors/test_store.py
@@ -380,6 +380,39 @@ def test_retrieve_filter_by_until(ks: KnowledgeStore) -> None:
assert len(results) >= 1
+def test_retrieve_empty_string_filters_do_not_suppress_results(
+ ks: KnowledgeStore,
+) -> None:
+ """Empty or whitespace-only filter strings are ignored instead of matching
+ literal empty values.
+ """
+ _store(
+ ks,
+ content="Important deployment documentation for project alpha",
+ source="notion",
+ doc_type="doc",
+ author="[email protected]",
+ )
+
+ results = ks.retrieve(
+ "deployment documentation",
+ source="",
+ doc_type=" ",
+ author="",
+ since="",
+ until=" ",
+ )
+ assert len(results) >= 1
+ assert results[0].metadata.get("source") == "notion"
+
+
+def test_retrieve_non_string_source_does_not_drop_filter(ks: KnowledgeStore) -> None:
+ """Unexpected filter types must not broaden a source-restricted query."""
+ _store(ks, content="Deployment notes for project alpha", source="notion")
+
+ assert ks.retrieve("deployment", source=42) == [] # type: ignore[arg-type]
+
+
def test_memory_store_event_emitted(tmp_path: Path) -> None:
"""MEMORY_STORE event is published on store()."""
from openjarvis.core.events import EventType, get_event_bus, reset_event_bus
diff --git a/tests/tools/test_knowledge_search.py b/tests/tools/test_knowledge_search.py
index 9fced764..de292ff1 100644
--- a/tests/tools/test_knowledge_search.py
+++ b/tests/tools/test_knowledge_search.py
@@ -114,6 +114,65 @@ class TestKnowledgeSearchTool:
assert result.success is False
assert "No knowledge store configured" in result.content
+ def test_filter_empty_string_parameters_ignored(self, store):
+ """Empty or whitespace strings in filter parameters are ignored."""
+ tool = KnowledgeSearchTool(store=store)
+ result = tool.execute(
+ query="Kubernetes migration",
+ source="",
+ doc_type=" ",
+ author="",
+ since="",
+ until=" ",
+ )
+ assert result.success is True
+ assert "Kubernetes migration" in result.content
+ assert result.metadata["num_results"] >= 1
+
+ def test_top_k_none_or_invalid_handled_gracefully(self, store):
+ """top_k as None, empty string, or invalid falls back to default safely."""
+ tool = KnowledgeSearchTool(store=store)
+ for bad_top_k in (None, "", "invalid", -5, 0, float("inf")):
+ result = tool.execute(query="Kubernetes", top_k=bad_top_k)
+ assert result.success is True
+ assert result.metadata["num_results"] >= 1
+
+ @pytest.mark.parametrize(
+ ("name", "value"),
+ (
+ ("source", 42),
+ ("doc_type", ["email"]),
+ ("author", True),
+ ("since", ["2026-01-01"]),
+ ("until", {"date": "2026-02-01"}),
+ ),
+ )
+ def test_non_string_filter_returns_failure(self, store, name, value):
+ tool = KnowledgeSearchTool(store=store)
+ result = tool.execute(query="Kubernetes", **{name: value})
+ assert result.success is False
+ assert f"Invalid {name} filter" in result.content
+
+ def test_missing_fts_table_surfaces_database_error(self, store):
+ tool = KnowledgeSearchTool(store=store)
+ store._conn.execute("DROP TABLE knowledge_fts")
+
+ result = tool.execute(query="Kubernetes")
+ assert result.success is False
+ assert "no such table: knowledge_fts" in result.content
+
+ def test_retrieval_failure_surfaces_error_tool_result(self, monkeypatch, store):
+ """Exceptions raised during retrieval return a failed ToolResult."""
+ tool = KnowledgeSearchTool(store=store)
+
+ def _boom(*args, **kwargs):
+ raise RuntimeError("database corrupted")
+
+ monkeypatch.setattr(store, "retrieve", _boom)
+ result = tool.execute(query="Kubernetes")
+ assert result.success is False
+ assert "Knowledge search failed: database corrupted" in result.content
+
def test_spec_has_filter_params(self):
"""ToolSpec.parameters includes all required and optional filter fields."""
tool = KnowledgeSearchTool()