Personal AI, On Personal Devices
Python
fix(traces): sanitize plain-text FTS queries in TraceStore.search() (#1045)
Arush Wadhawan0abaca8023c4
diff --git a/src/openjarvis/traces/store.py b/src/openjarvis/traces/store.pyindex ab3301d3..6847af5b 100644--- a/src/openjarvis/traces/store.py+++ b/src/openjarvis/traces/store.py@@ -3,6 +3,7 @@from __future__ import annotationsimport json+import reimport sqlite3import threadingfrom functools import wraps@@ -231,14 +232,26 @@ class TraceStore:agent: str | None = None,limit: int = 20,) -> list[dict[str, Any]]:- """Full-text search across traces. Optionally filter by agent."""+ """Full-text search across traces. Optionally filter by agent.++ Parameters+ ----------+ query: Plain-text search query (FTS5 punctuation is handled internally).+ agent: Restrict results to traces recorded by this agent.+ limit: Maximum number of results to return.+ """+ terms = re.findall(r"\w+", query)+ if not terms:+ return []+ fts_query = " OR ".join(f'"{term}"' for term in terms)+sql = ("SELECT t.trace_id, t.query, t.result, t.agent, t.model, t.outcome,"" t.started_at ""FROM traces_fts f JOIN traces t ON f.rowid = t.rowid ""WHERE traces_fts MATCH ?")- params: list[Any] = [query]+ params: list[Any] = [fts_query]if agent:sql += " AND t.agent = ?"params.append(agent)diff --git a/tests/traces/test_store_fts.py b/tests/traces/test_store_fts.pyindex 75441b00..64ca4d1b 100644--- a/tests/traces/test_store_fts.py+++ b/tests/traces/test_store_fts.py@@ -2,6 +2,7 @@from __future__ import annotations+import sqlite3import tempfilefrom pathlib import Path@@ -89,3 +90,48 @@ class TestFTS5Search:def test_search_empty(self, store):results = store.search("nonexistent gibberish xyzzy")assert results == []++ @pytest.mark.parametrize(+ "query",+ [+ "Alice's error",+ "When did task-101 fail?",+ "task-101",+ "fix-traces-fts",+ ],+ )+ def test_search_accepts_plain_text_punctuation(self, store, query):+ store.save(+ _make_trace(+ "t_punct",+ "investigate Alice's error in task-101",+ "fixed by fix-traces-fts",+ agent="debugger",+ )+ )+ results = store.search(query)+ assert any(r["trace_id"] == "t_punct" for r in results)++ def test_search_accepts_symbol_heavy_terms(self, store):+ store.save(+ _make_trace(+ "t_cpp",+ "The C++ migration guide",+ "success",+ agent="coder",+ )+ )+ assert store.search("C++")+ assert store.search("++") == []++ def test_search_empty_and_punctuation_only(self, store):+ assert store.search("") == []+ assert store.search(" ") == []+ assert store.search("---") == []+ assert store.search("???") == []++ def test_search_surfaces_database_errors(self, store):+ store._conn.execute("DROP TABLE traces_fts")++ with pytest.raises(sqlite3.OperationalError, match="no such table"):+ store.search("trace")