Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(docs): isolate the generated search index

Ruben Lütke7f74e0e58915
diff --git a/jarvis/docs/registry.py b/jarvis/docs/registry.py
index 32fa3d205f..5dadcbe306 100644
--- a/jarvis/docs/registry.py
+++ b/jarvis/docs/registry.py
@@ -51,7 +51,7 @@ class DocRegistry:
def __init__(
self,
roots: list[Path],
- index_db: Path,
+ index_db: Path | None,
bus: Any | None = None,
debounce_ms: int = 500,
) -> None:
diff --git a/jarvis/docs/search.py b/jarvis/docs/search.py
index 1013607d75..c1c693538f 100644
--- a/jarvis/docs/search.py
+++ b/jarvis/docs/search.py
@@ -5,7 +5,10 @@ account required, no frontend index bundle bloat. ~50-100 Markdown files in a
single-user app fit easily into one SQLite file (~500 KB).
A single-connection pattern is fine for SQLite (WAL mode + multi-thread). The
-registry holds one instance; REST routes read through its methods.
+registry holds one instance; REST routes read through its methods. Production
+uses an in-memory database because this is a derived index rebuilt at every
+start. Tests and offline tooling may still pass a path when persistence itself
+is the behavior under test.
"""
from __future__ import annotations
@@ -41,9 +44,10 @@ class DocSearch:
thread.
"""
- def __init__(self, db_path: Path) -> None:
- self.db_path = Path(db_path)
- self.db_path.parent.mkdir(parents=True, exist_ok=True)
+ def __init__(self, db_path: Path | None) -> None:
+ self.db_path = Path(db_path) if db_path is not None else None
+ if self.db_path is not None:
+ self.db_path.parent.mkdir(parents=True, exist_ok=True)
self._lock = threading.Lock()
# ``check_same_thread=False`` because watchdog wants to upsert from a
# different thread. We serialise access via the lock.
@@ -55,9 +59,11 @@ class DocSearch:
# ------------------------------------------------------------------
@staticmethod
- def _connect(path: Path) -> sqlite3.Connection:
+ def _connect(path: Path | None) -> sqlite3.Connection:
return sqlite3.connect(
- str(path), check_same_thread=False, isolation_level=None,
+ ":memory:" if path is None else str(path),
+ check_same_thread=False,
+ isolation_level=None,
)
@staticmethod
@@ -87,7 +93,10 @@ class DocSearch:
def _init_schema(self) -> None:
with self._lock:
- self._configure_connection(self._conn, use_wal=True)
+ self._configure_connection(
+ self._conn,
+ use_wal=self.db_path is not None,
+ )
# ------------------------------------------------------------------
# Upsert / Delete
@@ -120,11 +129,11 @@ class DocSearch:
self._conn.execute("DELETE FROM docs_index WHERE slug = ?", (slug,))
def replace_all(self, docs: list[Doc]) -> None:
- """Atomically replace the index with a newly built SQLite file.
+ """Atomically replace the index with a newly built SQLite database.
- Deleting FTS rows leaves their previous pages recoverable in SQLite's
- free list and WAL. Building a fresh file prevents retired engineering
- docs or local paths from surviving a scope reduction in raw bytes.
+ Deleting FTS rows leaves previous pages recoverable in SQLite's free
+ list and WAL. Building a fresh connection or file prevents retired
+ engineering docs or local paths from surviving a scope reduction.
"""
rows = [
(
@@ -139,10 +148,27 @@ class DocSearch:
for doc in docs
]
with self._lock:
+ if self.db_path is None:
+ # The production index is derived and rebuilt on every start.
+ # Swap a fully populated in-memory connection so concurrent
+ # desktop starts, test runs, and dev worktrees never contend
+ # for one user-global SQLite WAL on Windows.
+ replacement = self._connect(None)
+ try:
+ self._populate_replacement(replacement, rows)
+ except Exception:
+ replacement.close()
+ raise
+ previous = self._conn
+ self._conn = replacement
+ previous.close()
+ return
+
+ db_path = self.db_path
descriptor, raw_temp_path = tempfile.mkstemp(
- prefix=f".{self.db_path.name}.",
+ prefix=f".{db_path.name}.",
suffix=".tmp",
- dir=self.db_path.parent,
+ dir=db_path.parent,
)
os.close(descriptor)
temp_path = Path(raw_temp_path)
@@ -150,19 +176,7 @@ class DocSearch:
current_connection_closed = False
try:
temp_connection = self._connect(temp_path)
- # DELETE mode keeps the completed replacement self-contained;
- # no temporary WAL can be orphaned during the rename.
- self._configure_connection(temp_connection, use_wal=False)
- temp_connection.execute("BEGIN IMMEDIATE")
- temp_connection.executemany(
- """
- INSERT INTO docs_index
- (slug, title, diataxis, phase, tags, headings, body)
- VALUES (?, ?, ?, ?, ?, ?, ?)
- """,
- rows,
- )
- temp_connection.execute("COMMIT")
+ self._populate_replacement(temp_connection, rows)
temp_connection.close()
temp_connection = None
@@ -176,10 +190,10 @@ class DocSearch:
self._conn.close()
finally:
current_connection_closed = True
- self._remove_sidecars(self.db_path)
- os.replace(temp_path, self.db_path)
+ self._remove_sidecars(db_path)
+ os.replace(temp_path, db_path)
- self._conn = self._connect(self.db_path)
+ self._conn = self._connect(db_path)
current_connection_closed = False
self._configure_connection(self._conn, use_wal=True)
except Exception:
@@ -190,7 +204,7 @@ class DocSearch:
pass
if current_connection_closed:
# A failed swap must leave the previous database usable.
- self._conn = self._connect(self.db_path)
+ self._conn = self._connect(db_path)
current_connection_closed = False
self._configure_connection(self._conn, use_wal=True)
raise
@@ -200,6 +214,32 @@ class DocSearch:
temp_path.unlink(missing_ok=True)
self._remove_sidecars(temp_path)
+ @classmethod
+ def _populate_replacement(
+ cls,
+ connection: sqlite3.Connection,
+ rows: list[tuple[str, str, str, str, str, str, str]],
+ ) -> None:
+ """Build a complete replacement without exposing a partial index."""
+ # DELETE mode keeps a file-backed replacement self-contained; no
+ # temporary WAL can be orphaned during its rename. It is harmless for
+ # an in-memory database.
+ cls._configure_connection(connection, use_wal=False)
+ connection.execute("BEGIN IMMEDIATE")
+ try:
+ connection.executemany(
+ """
+ INSERT INTO docs_index
+ (slug, title, diataxis, phase, tags, headings, body)
+ VALUES (?, ?, ?, ?, ?, ?, ?)
+ """,
+ rows,
+ )
+ connection.execute("COMMIT")
+ except Exception:
+ connection.execute("ROLLBACK")
+ raise
+
@staticmethod
def _remove_sidecars(path: Path) -> None:
for suffix in ("-wal", "-shm", "-journal"):
diff --git a/jarvis/ui/web/server.py b/jarvis/ui/web/server.py
index ab88b786f7..a37100fc93 100644
--- a/jarvis/ui/web/server.py
+++ b/jarvis/ui/web/server.py
@@ -102,8 +102,8 @@ class WebServer:
# Skill registry: watched on user_skills_dir() after first-run bootstrap.
# The watcher starts in ``start()`` once the event loop is running.
self._skill_registry: Any | None = None
- # Doc registry: watched on default_doc_roots(); FTS5 index under
- # docs_index_db_path(). Watcher also starts in ``start()``.
+ # Doc registry: watched on default_doc_roots(); its derived FTS5 index
+ # is process-local. Watcher also starts in ``start()``.
self._doc_registry: Any | None = None
self._cli_registry: Any | None = None
self._plugin_registry: Any | None = None
@@ -762,19 +762,21 @@ class WebServer:
"""Bring up the doc registry + populate the FTS5 index initially.
Roots = ``default_doc_roots()`` (see ``jarvis/core/paths.py``).
- The index DB lives under ``user_data_dir()/data/docs_index.sqlite``.
+ The derived FTS index is process-local and rebuilt on every start. A
+ shared on-disk WAL made unrelated dev worktrees and overlapping restart
+ processes block the Docs view indefinitely on Windows.
- Failure cases (read-only FS, index-DB lock) are not fatal — the UI
- surfaces a retryable load error instead of crashing.
+ Failure cases (unreadable roots, unavailable FTS5 support) are not
+ fatal — the UI surfaces a retryable load error instead of crashing.
"""
try:
- from jarvis.core.paths import default_doc_roots, docs_index_db_path
+ from jarvis.core.paths import default_doc_roots
from jarvis.docs.registry import DocRegistry
roots = default_doc_roots()
registry = DocRegistry(
roots=roots,
- index_db=docs_index_db_path(),
+ index_db=None,
bus=self.bus,
)
self._doc_registry = registry
diff --git a/tests/unit/docs/test_registry.py b/tests/unit/docs/test_registry.py
index aee83d6bee..83ffacbac2 100644
--- a/tests/unit/docs/test_registry.py
+++ b/tests/unit/docs/test_registry.py
@@ -231,6 +231,20 @@ async def test_ensure_loaded_deduplicates_concurrent_first_requests(
reg.close()
+async def test_process_local_index_loads_without_a_database_path(doc_root: Path) -> None:
+ reg = DocRegistry(
+ roots=[doc_root / "docs"],
+ index_db=None,
+ )
+ try:
+ await reg.ensure_loaded()
+ assert reg.is_loaded is True
+ assert len(reg.search_query("Jarvis-Agent")) == 1
+ finally:
+ reg.close()
+
+
@pytest.mark.asyncio
async def test_debounce_keeps_newer_reload_deadline(
doc_root: Path,
diff --git a/tests/unit/docs/test_search.py b/tests/unit/docs/test_search.py
index a681fc63ae..a7f2ba5424 100644
--- a/tests/unit/docs/test_search.py
+++ b/tests/unit/docs/test_search.py
@@ -78,6 +78,26 @@ def test_replace_all_atomic(search: DocSearch) -> None:
assert len(search.query("Beta")) == 1
+def test_in_memory_replace_all_isolated_from_a_shared_disk_index(
+ tmp_path: Path,
+) -> None:
+ """Production indexing must not contend with another process or worktree."""
+ shared = DocSearch(tmp_path / "shared.sqlite")
+ process_local = DocSearch(None)
+ try:
+ shared.upsert(_doc("other", "Other", "Held by another process"))
+ process_local.replace_all(
+ [_doc("local", "Local", "Available in this desktop process")]
+ )
+
+ assert len(process_local.query("Available")) == 1
+ assert process_local.query("Held") == []
+ assert len(shared.query("Held")) == 1
+ finally:
+ process_local.close()
+ shared.close()
+
+
def test_replace_all_removes_retired_content_from_raw_database_files(
tmp_path: Path,
) -> None: