Skip to content

Personal AI, On Personal Devices

Python

fix(traces): serialize shared store access (#930)

Haotian Zheng5d3ff685083d
diff --git a/src/openjarvis/traces/store.py b/src/openjarvis/traces/store.py
index 493ebd9d..ab3301d3 100644
--- a/src/openjarvis/traces/store.py
+++ b/src/openjarvis/traces/store.py
@@ -4,6 +4,8 @@ from __future__ import annotations
import json
import sqlite3
+import threading
+from functools import wraps
from pathlib import Path
from typing import Any, List, Optional
@@ -77,6 +79,17 @@ INSERT INTO trace_steps (
"""
+def _db_locked(func):
+ """Serialize access to the connection shared by trace worker threads."""
+
+ @wraps(func)
+ def wrapped(self, *args, **kwargs):
+ with self._db_lock:
+ return func(self, *args, **kwargs)
+
+ return wrapped
+
+
class TraceStore:
"""Append-only SQLite store for interaction traces."""
@@ -90,10 +103,12 @@ class TraceStore:
from openjarvis.security.file_utils import secure_create
secure_create(Path(self._db_path))
- # check_same_thread=False is safe with WAL mode. The
- # AgenticRunner dispatches agent work to a ThreadPoolExecutor
- # (for Playwright compat), so trace writes may originate from
- # a different thread than the one that opened the connection.
+ # The AgenticRunner dispatches agent work to a ThreadPoolExecutor
+ # (for Playwright compat), so trace writes may originate from a
+ # different thread than the one that opened the connection.
+ # check_same_thread=False permits that use, but WAL does not serialize
+ # operations on one connection, so use a lock too.
+ self._db_lock = threading.RLock()
self._conn = sqlite3.connect(self._db_path, check_same_thread=False)
self._conn.execute("PRAGMA journal_mode=WAL")
self._conn.execute(_CREATE_TRACES)
@@ -109,6 +124,7 @@ class TraceStore:
pass # Column already exists
self._conn.commit()
+ @_db_locked
def save(self, trace: Trace) -> None:
"""Persist a complete trace with all its steps.
@@ -156,6 +172,7 @@ class TraceStore:
)
self._conn.commit()
+ @_db_locked
def get(self, trace_id: str) -> Optional[Trace]:
"""Retrieve a trace by id, or ``None`` if not found."""
row = self._conn.execute(
@@ -165,6 +182,7 @@ class TraceStore:
return None
return self._row_to_trace(row)
+ @_db_locked
def list_traces(
self,
*,
@@ -199,11 +217,13 @@ class TraceStore:
rows = self._conn.execute(sql, params).fetchall()
return [self._row_to_trace(r) for r in rows]
+ @_db_locked
def count(self) -> int:
"""Return the total number of stored traces."""
row = self._conn.execute("SELECT COUNT(*) FROM traces").fetchone()
return row[0] if row else 0
+ @_db_locked
def search(
self,
query: str,
@@ -247,6 +267,7 @@ class TraceStore:
if isinstance(trace, Trace):
self.save(trace)
+ @_db_locked
def update_feedback(self, trace_id: str, score: float) -> bool:
"""Update the feedback score for a trace.
@@ -259,12 +280,14 @@ class TraceStore:
self._conn.commit()
return cursor.rowcount > 0
+ @_db_locked
def close(self) -> None:
"""Close the underlying SQLite connection."""
self._conn.close()
# -- internal helpers ------------------------------------------------------
+ @_db_locked
def _row_to_trace(self, row: tuple) -> Trace:
"""Convert a traces table row + its steps into a Trace object."""
trace_id = row[1]
@@ -304,6 +327,7 @@ class TraceStore:
steps=steps,
)
+ @_db_locked
def _fetchall(self, sql: str = "SELECT * FROM traces") -> list:
return self._conn.execute(sql).fetchall()
diff --git a/tests/traces/test_store.py b/tests/traces/test_store.py
index 138f0471..23000fb5 100644
--- a/tests/traces/test_store.py
+++ b/tests/traces/test_store.py
@@ -101,6 +101,25 @@ class TestTraceStore:
assert store.count() == 3
store.close()
+ def test_concurrent_saves_are_serialized(self, tmp_path: Path) -> None:
+ """Concurrent trace writes must not be lost on the shared connection."""
+ from concurrent.futures import ThreadPoolExecutor
+
+ store = TraceStore(tmp_path / "test.db")
+ traces = []
+ for i in range(64):
+ trace = _make_trace(query=f"q{i}")
+ trace.trace_id = f"trace-{i}"
+ traces.append(trace)
+
+ with ThreadPoolExecutor(max_workers=8) as pool:
+ futures = [pool.submit(store.save, trace) for trace in traces]
+ for future in futures:
+ future.result()
+
+ assert store.count() == len(traces)
+ store.close()
+
def test_list_traces_no_filter(self, tmp_path: Path) -> None:
store = TraceStore(tmp_path / "test.db")
store.save(_make_trace(query="q1"))