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-07 17:52)

Ruben Lütke7b05d6201ed8
diff --git a/docs/jarvis-cli-reference.md b/docs/jarvis-cli-reference.md
index cce9d9953f..0326ffc7e6 100644
--- a/docs/jarvis-cli-reference.md
+++ b/docs/jarvis-cli-reference.md
@@ -180,6 +180,7 @@ _Generated from the curated command tree by `scripts/ci/gen_cli_reference.py`
- `jarvis wiki page <slug>` — Read a wiki page by vault path / slug.
- `jarvis wiki recall <query>` — Full-text search the wiki.
- `jarvis wiki tree` — Show the vault folder tree + stats.
+- `jarvis wiki vaults` — List the user's registered Obsidian vaults (connect picker, spec A6).
## workflows
diff --git a/jarvis/cli_ctl/commands/wiki.py b/jarvis/cli_ctl/commands/wiki.py
index 8cc0a3d49d..3201b9c35c 100644
--- a/jarvis/cli_ctl/commands/wiki.py
+++ b/jarvis/cli_ctl/commands/wiki.py
@@ -29,3 +29,9 @@ def page(slug: str = typer.Argument(..., help="Vault path / slug, e.g. people/ja
def tree() -> None:
"""Show the vault folder tree + stats."""
invoke.run("GET", "/api/wiki/tree")
+
+
+def vaults() -> None:
+ """List the user's registered Obsidian vaults (connect picker, spec A6)."""
+ invoke.run("GET", "/api/setup/obsidian/vaults")
diff --git a/jarvis/memory/wiki/fts_index.py b/jarvis/memory/wiki/fts_index.py
index 487ee20c38..33fbb01143 100644
--- a/jarvis/memory/wiki/fts_index.py
+++ b/jarvis/memory/wiki/fts_index.py
@@ -122,6 +122,30 @@ def index_vault(vault_root: Path, conn: sqlite3.Connection) -> int:
return count
+def rebuild_index(vault_root: Path, conn: sqlite3.Connection) -> int:
+ """Clear ``wiki_fts`` and re-index ``vault_root`` from scratch.
+
+ Used after a vault switch (spec A6 "connect to an existing vault") so
+ search never serves stale rows from the previously active vault.
+
+ Parameters
+ ----------
+ vault_root:
+ Absolute path to the (new) Obsidian vault root.
+ conn:
+ Open, writable ``sqlite3.Connection``.
+
+ Returns
+ -------
+ int
+ Number of pages indexed (same contract as :func:`index_vault`).
+ """
+ ensure_schema(conn)
+ conn.execute("DELETE FROM wiki_fts")
+ conn.commit()
+ return index_vault(vault_root, conn)
+
+
def upsert_page(
conn: sqlite3.Connection,
vault_root: Path,
@@ -308,6 +332,7 @@ def _upsert_one(
__all__ = [
"ensure_schema",
"index_vault",
+ "rebuild_index",
"upsert_page",
"remove_page",
]
diff --git a/jarvis/ui/web/setup_routes.py b/jarvis/ui/web/setup_routes.py
index a05bd8810a..c94fc551ca 100644
--- a/jarvis/ui/web/setup_routes.py
+++ b/jarvis/ui/web/setup_routes.py
@@ -68,13 +68,48 @@ class ObsidianRegisterResponse(BaseModel):
"""Response payload for ``POST /api/setup/obsidian/register``.
Mirrors :class:`jarvis.setup.obsidian.RegisterResult` 1:1 so the UI
- can drive its toast / banner from one place.
+ 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.
"""
status: Literal["added", "already_registered", "config_missing", "rolled_back"]
vault_uuid: str | None = None
backup_path: str | None = None
error: str | None = None
+ active_vault_root: str | None = None
+ restart_required: bool = False
+
+
+class ObsidianRegisterRequest(BaseModel):
+ """Request body for ``POST /api/setup/obsidian/register`` (spec A6).
+
+ ``mode="separate"`` (the default — an absent body means this too) is
+ today's behavior, unchanged: register the Jarvis-owned vault in
+ Obsidian's own vault index. ``mode="existing"`` instead writes INTO the
+ user's own already-registered vault: Jarvis's vault root is repointed
+ to ``<existing_vault_path>/Jarvis`` so every wiki write stays
+ contained inside that subtree (containment by construction).
+ """
+
+ mode: Literal["separate", "existing"] = "separate"
+ existing_vault_path: str | None = None
+
+
+class ObsidianVaultInfo(BaseModel):
+ """One entry in the vault picker list (spec A6)."""
+
+ path: str
+ name: str
+
+
+class ObsidianVaultListResponse(BaseModel):
+ """Response payload for ``GET /api/setup/obsidian/vaults``."""
+
+ ok: bool
+ config_exists: bool
+ vaults: list[ObsidianVaultInfo]
class SetupStateResponse(BaseModel):
@@ -111,6 +146,20 @@ def _resolve_vault_path(request: Request) -> Path:
return resolve_vault_root(raw).path
+def _write_vault_root_config(values: dict) -> None:
+ """Persist ``[wiki_integration].vault_root`` atomically (AP-7).
+
+ Thin seam over :mod:`jarvis.core.config_writer` so tests can stub the
+ disk write without touching the real ``jarvis.toml``. ``values`` is
+ ``{"wiki_integration": {"vault_root": "<abs path>"}}`` — the shape the
+ "existing vault" register flow (spec A6) writes.
+ """
+ from jarvis.core import config_writer
+
+ vault_root = values["wiki_integration"]["vault_root"]
+ config_writer.set_wiki_vault_root(vault_root)
+
+
# ---------------------------------------------------------------------------
# Endpoints
# ---------------------------------------------------------------------------
@@ -158,22 +207,85 @@ def obsidian_status(request: Request) -> ObsidianStatusResponse:
)
[email protected]("/obsidian/vaults", response_model=ObsidianVaultListResponse)
+def obsidian_vaults(request: Request) -> ObsidianVaultListResponse:
+ """List the user's registered Obsidian vaults for the connect picker (spec A6).
+
+ ``app.state.obsidian_config_path`` is an override hook for tests (and
+ any future multi-user host) — production requests leave it unset and
+ fall back to the platform default via :func:`read_obsidian_vaults`.
+ Never raises a 5xx — a corrupt ``obsidian.json`` degrades to an empty
+ list with ``ok=False`` so the picker UI stays responsive.
+ """
+ override = getattr(request.app.state, "obsidian_config_path", None)
+ try:
+ state = read_obsidian_vaults(override)
+ except ValueError as exc:
+ log.warning("obsidian_vaults: corrupt obsidian.json: %s", exc)
+ return ObsidianVaultListResponse(ok=False, config_exists=True, vaults=[])
+ return ObsidianVaultListResponse(
+ ok=True,
+ config_exists=state.config_exists,
+ vaults=[
+ ObsidianVaultInfo(path=str(v.path), name=v.path.name)
+ for v in state.vaults
+ ],
+ )
+
+
@router.post("/obsidian/register", response_model=ObsidianRegisterResponse)
def obsidian_register(
request: Request,
+ body: ObsidianRegisterRequest | None = None,
dry_run: bool = Query(default=False),
) -> ObsidianRegisterResponse:
- """Register the Jarvis vault in ``obsidian.json``.
+ """Register a vault for Jarvis's wiki (spec A6: vault choice).
+
+ ``body`` is optional and backward compatible: no body (or an explicit
+ ``{"mode": "separate"}``) is today's unchanged behavior — register the
+ Jarvis-owned vault in Obsidian's own vault index.
+
+ ``mode="existing"`` instead writes INTO the user's own vault: creates
+ ``<existing_vault_path>/Jarvis`` and repoints
+ ``[wiki_integration].vault_root`` there via :func:`_write_vault_root_config`
+ (AP-7). Containment by construction — every subsequent wiki write is
+ physically confined to that subtree, no ``AtomicWriter`` change needed.
+ ``restart_required=True`` because the running curator/FTS index still
+ targets the old vault until ``POST /api/settings/restart-app``.
Status mapping:
* ``added`` -> HTTP 200
- * ``already_registered`` -> HTTP 200
- * ``config_missing`` -> HTTP 409 (Obsidian was never started)
+ * ``already_registered`` -> HTTP 200 (``separate`` mode only)
+ * ``config_missing`` -> HTTP 409 for ``separate`` (Obsidian was
+ never started); HTTP 200 for ``existing`` (the given path does not
+ exist — a user-input error the UI shows inline, not a server fault)
* ``rolled_back`` -> HTTP 500 (write failure, restored)
Unexpected exceptions are translated to HTTP 500 with a
``rolled_back`` payload so the UI shows a consistent error toast.
"""
+ req = body or ObsidianRegisterRequest()
+
+ if req.mode == "existing":
+ target_vault = Path(req.existing_vault_path or "")
+ if not target_vault.is_dir():
+ return ObsidianRegisterResponse(
+ status="config_missing",
+ error="existing vault path not found",
+ active_vault_root=str(_resolve_vault_path(request)),
+ 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)}}
+ )
+ return ObsidianRegisterResponse(
+ status="added",
+ active_vault_root=str(jarvis_root),
+ restart_required=True,
+ )
+
vault_path = _resolve_vault_path(request)
try:
@@ -198,6 +310,8 @@ def obsidian_register(
vault_uuid=result.vault_uuid,
backup_path=backup_str,
error=result.error,
+ active_vault_root=str(vault_path),
+ restart_required=False,
)
if result.status == "config_missing":
@@ -261,6 +375,9 @@ async def post_obsidian_seen(request: Request) -> dict[str, bool]:
__all__ = [
"router",
"ObsidianStatusResponse",
+ "ObsidianRegisterRequest",
"ObsidianRegisterResponse",
+ "ObsidianVaultInfo",
+ "ObsidianVaultListResponse",
"SetupStateResponse",
]
diff --git a/tests/unit/cli_ctl/test_commands_wiki.py b/tests/unit/cli_ctl/test_commands_wiki.py
index ac4946424c..0e26059582 100644
--- a/tests/unit/cli_ctl/test_commands_wiki.py
+++ b/tests/unit/cli_ctl/test_commands_wiki.py
@@ -22,3 +22,8 @@ def test_page(capture_api):
def test_tree(capture_api):
runner.invoke(app, ["wiki", "tree"])
assert capture_api["calls"][-1]["path"] == "/api/wiki/tree"
+
+
+def test_vaults(capture_api):
+ runner.invoke(app, ["wiki", "vaults"])
+ assert capture_api["calls"][-1]["path"] == "/api/setup/obsidian/vaults"
diff --git a/tests/unit/ui/web/test_setup_routes_vault_choice.py b/tests/unit/ui/web/test_setup_routes_vault_choice.py
new file mode 100644
index 0000000000..c98aadfc68
--- /dev/null
+++ b/tests/unit/ui/web/test_setup_routes_vault_choice.py
@@ -0,0 +1,79 @@
+"""Obsidian connect offers a vault choice (spec A6).
+
+Uses the same TestClient + app.state.config stubbing conventions as
+tests/unit/ui/web/test_wiki_routes.py — copy its app fixture setup.
+"""
+from __future__ import annotations
+
+import json
+from pathlib import Path
+
+from fastapi import FastAPI
+from fastapi.testclient import TestClient
+
+from jarvis.ui.web import setup_routes
+
+
+def _app(tmp_path: Path, obsidian_json: dict | None) -> FastAPI:
+ app = FastAPI()
+ app.include_router(setup_routes.router)
+
+ class _WikiCfg:
+ vault_root = tmp_path / "jarvis-vault"
+
+ class _Cfg:
+ wiki_integration = _WikiCfg()
+
+ app.state.config = _Cfg()
+ cfg_path = tmp_path / "obsidian" / "obsidian.json"
+ if obsidian_json is not None:
+ cfg_path.parent.mkdir(parents=True)
+ cfg_path.write_text(json.dumps(obsidian_json), encoding="utf-8")
+ app.state.obsidian_config_path = cfg_path # route override hook
+ return app
+
+
+def test_vault_list_returns_registered_vaults(tmp_path):
+ user_vault = tmp_path / "MyVault"
+ user_vault.mkdir()
+ app = _app(tmp_path, {"vaults": {"abc123": {"path": str(user_vault)}}})
+ resp = TestClient(app).get("/api/setup/obsidian/vaults")
+ assert resp.status_code == 200
+ body = resp.json()
+ assert body["ok"] is True
+ assert body["vaults"][0]["path"] == str(user_vault)
+
+
+def test_register_existing_mode_points_vault_root_into_jarvis_subfolder(
+ tmp_path, monkeypatch,
+):
+ user_vault = tmp_path / "MyVault"
+ user_vault.mkdir()
+ app = _app(tmp_path, {"vaults": {"abc123": {"path": str(user_vault)}}})
+
+ written: dict = {}
+
+ def _fake_update(values): # captures the config_writer call
+ written.update(values)
+
+ monkeypatch.setattr(setup_routes, "_write_vault_root_config", _fake_update)
+ resp = TestClient(app).post(
+ "/api/setup/obsidian/register",
+ json={"mode": "existing", "existing_vault_path": str(user_vault)},
+ )
+ assert resp.status_code == 200
+ body = resp.json()
+ assert body["active_vault_root"] == str(user_vault / "Jarvis")
+ assert body["restart_required"] is True
+ assert (user_vault / "Jarvis").is_dir() # subfolder created
+ assert written # config write happened
+
+
+def test_register_existing_mode_rejects_unknown_path(tmp_path):
+ app = _app(tmp_path, {"vaults": {}})
+ resp = TestClient(app).post(
+ "/api/setup/obsidian/register",
+ json={"mode": "existing", "existing_vault_path": str(tmp_path / "nope")},
+ )
+ assert resp.status_code == 200
+ assert resp.json()["status"] == "config_missing"