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(setup): fail-closed vault path + honor dry_run in existing-mode register

Ruben Lütke1e7734e85007
diff --git a/jarvis/ui/web/setup_routes.py b/jarvis/ui/web/setup_routes.py
index 5ea824d59d..91a2e54625 100644
--- a/jarvis/ui/web/setup_routes.py
+++ b/jarvis/ui/web/setup_routes.py
@@ -78,7 +78,9 @@ class ObsidianRegisterResponse(BaseModel):
can drive its toast / banner from one place. ``active_vault_root`` and
``restart_required`` (spec A6) are populated for both ``mode``s so the
UI always has a stable field to show, regardless of which vault choice
- the user made.
+ the user made. ``active_vault_root`` is only guaranteed on the HTTP 200
+ responses returned here; the HTTP 409/500 error branches raise an
+ ``HTTPException`` whose detail payload does not carry it.
"""
status: Literal["added", "already_registered", "config_missing", "rolled_back"]
@@ -274,8 +276,13 @@ def obsidian_register(
req = body or ObsidianRegisterRequest()
if req.mode == "existing":
- target_vault = Path(req.existing_vault_path or "")
- if not target_vault.is_dir():
+ # Fail closed: an omitted/empty path must be rejected, never coerced
+ # to ``Path("")`` -> ``Path(".")`` (the server's own CWD, which IS a
+ # directory) — that would silently repoint the vault into the working
+ # directory instead of surfacing the user-input error.
+ raw_path = (req.existing_vault_path or "").strip()
+ target_vault = Path(raw_path) if raw_path else None
+ if target_vault is None or not target_vault.is_dir():
return ObsidianRegisterResponse(
status="config_missing",
error="existing vault path not found",
@@ -283,10 +290,13 @@ def obsidian_register(
restart_required=False,
)
jarvis_root = target_vault / "Jarvis"
- jarvis_root.mkdir(parents=True, exist_ok=True)
- _write_vault_root_config(
- {"wiki_integration": {"vault_root": str(jarvis_root)}}
- )
+ # ``dry_run`` previews the would-be vault root without touching disk
+ # or config — same contract the ``separate`` branch gives the flag.
+ if not dry_run:
+ jarvis_root.mkdir(parents=True, exist_ok=True)
+ _write_vault_root_config(
+ {"wiki_integration": {"vault_root": str(jarvis_root)}}
+ )
return ObsidianRegisterResponse(
status="added",
active_vault_root=str(jarvis_root),
diff --git a/tests/unit/memory/wiki/test_fts_index.py b/tests/unit/memory/wiki/test_fts_index.py
index 52c7a27206..1e590ac2d8 100644
--- a/tests/unit/memory/wiki/test_fts_index.py
+++ b/tests/unit/memory/wiki/test_fts_index.py
@@ -23,6 +23,7 @@ import pytest
from jarvis.memory.wiki.fts_index import (
ensure_schema,
index_vault,
+ rebuild_index,
remove_page,
upsert_page,
)
@@ -125,6 +126,43 @@ def test_index_vault_idempotent(tmp_path: Path) -> None:
conn.close()
+# ---------------------------------------------------------------------------
+# Test: rebuild_index clears the old vault's rows before reindexing
+# ---------------------------------------------------------------------------
+
+
+def test_rebuild_index_replaces_old_vault_rows(tmp_path: Path) -> None:
+ """rebuild_index must drop the previously indexed vault's rows and index
+ only the new vault — the vault-switch contract (spec A6)."""
+ vault_a = tmp_path / "vault_a"
+ _write_md(vault_a / "entities" / "alpha.md", "# Alpha\nOnly in vault A.\n")
+ _write_md(vault_a / "entities" / "beta.md", "# Beta\nAlso vault A.\n")
+
+ vault_b = tmp_path / "vault_b"
+ _write_md(vault_b / "entities" / "gamma.md", "# Gamma\nOnly in vault B.\n")
+
+ conn = _mem_conn()
+ index_vault(vault_a, conn)
+ assert _row_count(conn) == 2
+
+ count = rebuild_index(vault_b, conn)
+
+ assert count == 1
+ assert _row_count(conn) == 1
+ # Old vault-A rows are gone; the new vault-B page is present. Paths are
+ # stored posix-normalized (fts_index._relative_posix), so assert on the
+ # forward-slash form on every OS.
+ assert conn.execute(
+ "SELECT COUNT(*) FROM wiki_fts WHERE path = ?",
+ ("entities/alpha.md",),
+ ).fetchone()[0] == 0
+ assert conn.execute(
+ "SELECT COUNT(*) FROM wiki_fts WHERE path = ?",
+ ("entities/gamma.md",),
+ ).fetchone()[0] == 1
+ conn.close()
+
+
# ---------------------------------------------------------------------------
# Test: upsert_page replaces, does not duplicate
# ---------------------------------------------------------------------------
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 c98aadfc68..2577727c72 100644
--- a/tests/unit/ui/web/test_setup_routes_vault_choice.py
+++ b/tests/unit/ui/web/test_setup_routes_vault_choice.py
@@ -77,3 +77,43 @@ def test_register_existing_mode_rejects_unknown_path(tmp_path):
)
assert resp.status_code == 200
assert resp.json()["status"] == "config_missing"
+
+
+def test_register_existing_mode_rejects_missing_path(tmp_path, monkeypatch):
+ """Omitted path must fail closed, never default to Path('.') == server CWD."""
+ monkeypatch.chdir(tmp_path) # so an accidental Path(".") hit is observable
+ app = _app(tmp_path, {"vaults": {}})
+
+ written: dict = {}
+ monkeypatch.setattr(setup_routes, "_write_vault_root_config", written.update)
+ resp = TestClient(app).post(
+ "/api/setup/obsidian/register",
+ json={"mode": "existing"},
+ )
+ assert resp.status_code == 200
+ assert resp.json()["status"] == "config_missing"
+ assert not (tmp_path / "Jarvis").exists() # no folder in the server CWD
+ assert not written # config was never touched
+
+
+def test_register_existing_mode_dry_run_previews_without_side_effects(
+ tmp_path, monkeypatch,
+):
+ """?dry_run=true previews the would-be vault root: no mkdir, no config write."""
+ user_vault = tmp_path / "MyVault"
+ user_vault.mkdir()
+ app = _app(tmp_path, {"vaults": {"abc123": {"path": str(user_vault)}}})
+
+ written: dict = {}
+ monkeypatch.setattr(setup_routes, "_write_vault_root_config", written.update)
+ resp = TestClient(app).post(
+ "/api/setup/obsidian/register?dry_run=true",
+ json={"mode": "existing", "existing_vault_path": str(user_vault)},
+ )
+ assert resp.status_code == 200
+ body = resp.json()
+ assert body["status"] == "added"
+ assert body["active_vault_root"] == str(user_vault / "Jarvis")
+ assert body["restart_required"] is True
+ assert not (user_vault / "Jarvis").exists() # nothing created
+ assert not written # nothing persisted