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.

chore: auto-save WIP via Claude stop-hook safety net (2026-07-08 09:15)

Ruben Lütkec8b75a087cf6
diff --git a/jarvis/memory/wiki/integration.py b/jarvis/memory/wiki/integration.py
index d9bf29a487..a7fc17827c 100644
--- a/jarvis/memory/wiki/integration.py
+++ b/jarvis/memory/wiki/integration.py
@@ -439,7 +439,11 @@ async def bootstrap_wiki_integration(
getattr(
getattr(root_cfg, "wiki_scheduler", None),
"consolidate_after_candidates",
- 8,
+ # Fallback must match SchedulerConfig's own default (3,
+ # spec A4). The old 8 here could resurrect the
+ # pre-A4 threshold if the [wiki_scheduler] section were
+ # ever absent, silently undermining ambient capture.
+ 3,
)
)
extractor.attach_scheduler(scheduler, consolidate_after=threshold)
diff --git a/jarvis/ui/web/setup_routes.py b/jarvis/ui/web/setup_routes.py
index 91a2e54625..60bdd76df6 100644
--- a/jarvis/ui/web/setup_routes.py
+++ b/jarvis/ui/web/setup_routes.py
@@ -169,6 +169,49 @@ def _write_vault_root_config(values: dict) -> None:
config_writer.set_wiki_vault_root(vault_root)
+def _reindex_vault_fts(request: Request, jarvis_root: Path) -> None:
+ """Rebuild the FTS search index against a newly-connected vault (spec A6).
+
+ Switching to an existing vault repoints ``vault_root`` into
+ ``<vault>/Jarvis`` and asks for a restart — but the boot-time index
+ (``server._init_wiki_boot_index``) only builds when the FTS table is
+ EMPTY, so on restart it would skip and ``wiki-recall`` / the pre-answer
+ injector would keep serving the PREVIOUS vault's stale rows. Reindexing
+ here makes search reflect the new vault immediately.
+
+ Best-effort and fail-open: a reindex failure must never 500 the register
+ call (the vault switch itself already succeeded), but it must not be
+ silent either — it is logged and recorded on the wiki health surface.
+ """
+ import sqlite3
+
+ from jarvis.memory.wiki.fts_index import rebuild_index
+
+ try:
+ config = getattr(request.app.state, "config", None)
+ data_dir = Path(getattr(getattr(config, "memory", None), "data_dir", "./data"))
+ db_path = data_dir / "jarvis.db"
+ db_path.parent.mkdir(parents=True, exist_ok=True)
+ conn = sqlite3.connect(str(db_path), check_same_thread=False)
+ try:
+ indexed = rebuild_index(jarvis_root, conn)
+ log.info(
+ "obsidian_register: reindexed FTS for %d page(s) from %s",
+ indexed,
+ jarvis_root,
+ )
+ finally:
+ conn.close()
+ except Exception as exc: # noqa: BLE001 — reindex must never break the switch
+ log.warning("obsidian_register: FTS reindex after vault switch failed: %s", exc)
+ try:
+ from jarvis.memory.wiki.health import health
+
+ health.record_chain_failure(f"vault-switch reindex failed: {exc}")
+ except Exception: # noqa: BLE001
+ log.debug("obsidian_register: health record of reindex failure failed")
+
+
# ---------------------------------------------------------------------------
# Endpoints
# ---------------------------------------------------------------------------
@@ -297,6 +340,10 @@ def obsidian_register(
_write_vault_root_config(
{"wiki_integration": {"vault_root": str(jarvis_root)}}
)
+ # Reindex search against the new vault NOW so it never serves the
+ # previous vault's stale rows (spec A6); the restart realigns the
+ # curator/watcher. Best-effort — never fails the switch.
+ _reindex_vault_fts(request, jarvis_root)
return ObsidianRegisterResponse(
status="added",
active_vault_root=str(jarvis_root),
diff --git a/tests/unit/ui/web/test_setup_routes_vault_choice.py b/tests/unit/ui/web/test_setup_routes_vault_choice.py
index 2577727c72..704a33830a 100644
--- a/tests/unit/ui/web/test_setup_routes_vault_choice.py
+++ b/tests/unit/ui/web/test_setup_routes_vault_choice.py
@@ -6,6 +6,8 @@ tests/unit/ui/web/test_wiki_routes.py — copy its app fixture setup.
from __future__ import annotations
import json
+import sqlite3
+import types
from pathlib import Path
from fastapi import FastAPI
@@ -117,3 +119,92 @@ def test_register_existing_mode_dry_run_previews_without_side_effects(
assert body["restart_required"] is True
assert not (user_vault / "Jarvis").exists() # nothing created
assert not written # nothing persisted
+
+
+# ---------------------------------------------------------------------------
+# Reindex-on-switch (spec A6): search must reflect the NEW vault immediately,
+# never keep serving the previous vault's stale rows.
+# ---------------------------------------------------------------------------
+def _seed_fts(db_path: Path, vault_root: Path) -> None:
+ """Index an existing vault into a fresh jarvis.db FTS table."""
+ from jarvis.memory.wiki.fts_index import index_vault
+
+ db_path.parent.mkdir(parents=True, exist_ok=True)
+ conn = sqlite3.connect(str(db_path))
+ try:
+ index_vault(vault_root, conn)
+ finally:
+ conn.close()
+
+
+def _fts_paths(db_path: Path) -> set[str]:
+ conn = sqlite3.connect(str(db_path))
+ try:
+ return {row[0] for row in conn.execute("SELECT path FROM wiki_fts")}
+ finally:
+ conn.close()
+
+
+def _write_page(root: Path, rel: str, title: str) -> None:
+ p = root / rel
+ p.parent.mkdir(parents=True, exist_ok=True)
+ p.write_text(f"# {title}\nBody of {title}.\n", encoding="utf-8")
+
+
+def test_register_existing_mode_reindexes_search_to_the_new_vault(
+ tmp_path, monkeypatch,
+):
+ """After switching to an existing vault, the FTS index must contain the
+ NEW vault's pages and none of the previous vault's stale rows (spec A6)."""
+ data_dir = tmp_path / "data"
+ db_path = data_dir / "jarvis.db"
+
+ # Previously-connected vault, already indexed into the shared FTS db.
+ old_vault = tmp_path / "OldVault" / "Jarvis"
+ _write_page(old_vault, "entities/old_note.md", "Old Note")
+ _seed_fts(db_path, old_vault)
+ assert "entities/old_note.md" in _fts_paths(db_path) # precondition
+
+ # The user's newly-chosen vault carries a different page under Jarvis/.
+ new_vault = tmp_path / "NewVault"
+ _write_page(new_vault / "Jarvis", "entities/new_note.md", "New Note")
+
+ app = _app(tmp_path, {"vaults": {"abc": {"path": str(new_vault)}}})
+ app.state.config.memory = types.SimpleNamespace(data_dir=str(data_dir))
+ monkeypatch.setattr(setup_routes, "_write_vault_root_config", lambda values: None)
+
+ resp = TestClient(app).post(
+ "/api/setup/obsidian/register",
+ json={"mode": "existing", "existing_vault_path": str(new_vault)},
+ )
+ assert resp.status_code == 200
+ assert resp.json()["status"] == "added"
+
+ paths = _fts_paths(db_path)
+ assert "entities/new_note.md" in paths # new vault indexed
+ assert "entities/old_note.md" not in paths # stale rows cleared
+
+
+def test_register_existing_mode_dry_run_does_not_reindex(tmp_path, monkeypatch):
+ """A dry-run preview must not touch the FTS index either."""
+ data_dir = tmp_path / "data"
+ db_path = data_dir / "jarvis.db"
+
+ old_vault = tmp_path / "OldVault" / "Jarvis"
+ _write_page(old_vault, "entities/old_note.md", "Old Note")
+ _seed_fts(db_path, old_vault)
+
+ new_vault = tmp_path / "NewVault"
+ _write_page(new_vault / "Jarvis", "entities/new_note.md", "New Note")
+
+ app = _app(tmp_path, {"vaults": {"abc": {"path": str(new_vault)}}})
+ app.state.config.memory = types.SimpleNamespace(data_dir=str(data_dir))
+ monkeypatch.setattr(setup_routes, "_write_vault_root_config", lambda values: None)
+
+ resp = TestClient(app).post(
+ "/api/setup/obsidian/register?dry_run=true",
+ json={"mode": "existing", "existing_vault_path": str(new_vault)},
+ )
+ assert resp.status_code == 200
+ # dry-run left the old index untouched — no reindex side effect.
+ assert _fts_paths(db_path) == {"entities/old_note.md"}