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.

feat(brain): ollama runtime module — detect, install, start without a terminal

rubenluetke10-beepb8b3ebbd182d
diff --git a/jarvis/brain/ollama_runtime.py b/jarvis/brain/ollama_runtime.py
new file mode 100644
index 0000000000..277ad9ebf0
--- /dev/null
+++ b/jarvis/brain/ollama_runtime.py
@@ -0,0 +1,474 @@
+"""Ollama runtime lifecycle: detect, install, and start — without a terminal.
+
+The local-model path was one terminal away from plug-and-go: every blocker
+ended in "install Ollama and run `ollama pull ...`" (maintainer complaint
+2026-08-08). This module owns the runtime itself so the cards can offer a
+button instead of a command line:
+
+- :func:`runtime_status` — the honest three-state answer ("not installed" /
+ "installed but not running" / "running"), which pure HTTP probes cannot
+ distinguish.
+- :func:`start_server` — spawn ``ollama serve`` detached and wait for its
+ port.
+- :func:`start_install` / :func:`install_snapshot` — a poll-shaped installer
+ (same skeleton as the managed realtime server install): winget or the
+ official per-user installer on Windows, Homebrew on macOS, the official
+ script on Linux when non-interactive sudo exists. Everything else fails
+ honestly with the one action that would fix it (§3: honest degradation,
+ never a hang on a hidden password prompt).
+
+Nothing here runs without an explicit user action: the REST route that calls
+:func:`start_install` is dangerous-flagged and sits behind a confirm button.
+"""
+
+from __future__ import annotations
+
+import json
+import logging
+import os
+import shutil
+import socket
+import subprocess
+import sys
+import threading
+import time
+from collections import deque
+from dataclasses import dataclass, field
+from pathlib import Path
+from urllib.parse import urlsplit
+
+from jarvis.core.process_utils import NO_WINDOW_CREATIONFLAGS
+
+log = logging.getLogger(__name__)
+
+_VERSION_TIMEOUT_S = 1.5
+#: How long a freshly spawned ``ollama serve`` may take to bind its port.
+_START_WAIT_S = 15.0
+_START_POLL_S = 0.5
+
+_WINGET_TIMEOUT_S = 1200
+_INSTALLER_TIMEOUT_S = 900
+_DOWNLOAD_TIMEOUT_S = 1800
+
+#: Official artifacts only — never a mirror.
+_WINDOWS_INSTALLER_URL = "https://ollama.com/download/OllamaSetup.exe"
+_LINUX_INSTALL_SCRIPT_URL = "https://ollama.com/install.sh"
+
+
+# ── Detection ────────────────────────────────────────────────────────────
+
+
+def _known_binaries() -> list[Path]:
+ """Places the official installers put the binary, per platform."""
+ candidates: list[Path] = []
+ if os.name == "nt":
+ local = os.environ.get("LOCALAPPDATA", "")
+ if local:
+ candidates.append(Path(local) / "Programs" / "Ollama" / "ollama.exe")
+ else:
+ candidates.extend(
+ [
+ Path("/usr/local/bin/ollama"),
+ Path("/opt/homebrew/bin/ollama"),
+ Path("/usr/bin/ollama"),
+ Path("/Applications/Ollama.app/Contents/Resources/ollama"),
+ ]
+ )
+ return candidates
+
+
+def find_binary() -> str:
+ """Absolute path of the Ollama binary, or ``""`` when none exists.
+
+ PATH first (after the well-known-dir refresh, so a binary installed a
+ minute ago by this very process is visible), then the official install
+ locations directly — a GUI-launched app misses registry-PATH updates.
+ """
+ try:
+ from jarvis.core.path_augment import ensure_cli_paths # lazy (AP-26)
+
+ ensure_cli_paths()
+ except Exception: # noqa: BLE001 — a PATH refresh failure must not block detection
+ log.debug("ollama-runtime: PATH refresh failed", exc_info=True)
+ resolved = shutil.which("ollama")
+ if resolved:
+ return resolved
+ for candidate in _known_binaries():
+ try:
+ if candidate.exists():
+ return str(candidate)
+ except OSError: # pragma: no cover — unreadable mount
+ continue
+ return ""
+
+
+def _server_root() -> str:
+ from jarvis.brain.ollama_pull import server_root # lazy (AP-26)
+
+ return server_root()
+
+
+def _server_version(timeout: float = _VERSION_TIMEOUT_S) -> str | None:
+ """The running server's version string, or ``None`` when it is not up."""
+ import urllib.error
+ import urllib.request
+
+ url = f"{_server_root()}/api/version"
+ if not url.startswith(("http://", "https://")):
+ return None
+ try:
+ with urllib.request.urlopen(url, timeout=timeout) as resp: # noqa: S310
+ payload = json.load(resp)
+ except (urllib.error.URLError, OSError, ValueError):
+ return None
+ version = str(payload.get("version", "") or "")
+ return version or "unknown"
+
+
+def runtime_status() -> dict[str, object]:
+ """The honest runtime picture: ``{installed, binary, running, version, detail}``.
+
+ A pure HTTP probe cannot tell "not installed" from "installed but
+ stopped" — and those two states need OPPOSITE buttons (install vs
+ start), so the distinction is the whole point of this function.
+ """
+ binary = find_binary()
+ version = _server_version()
+ running = version is not None
+ installed = bool(binary) or running
+ if running:
+ detail = f"Ollama is running (version {version})."
+ elif installed:
+ detail = "Ollama is installed but not running."
+ else:
+ detail = "Ollama is not installed on this machine."
+ return {
+ "installed": installed,
+ "binary": binary,
+ "running": running,
+ "version": version or "",
+ "detail": detail,
+ }
+
+
+# ── Start ────────────────────────────────────────────────────────────────
+
+
+def _server_port() -> int:
+ parsed = urlsplit(_server_root())
+ try:
+ return parsed.port or 11434
+ except ValueError:
+ return 11434
+
+
+def _port_open(port: int, timeout: float = 1.0) -> bool:
+ try:
+ with socket.create_connection(("127.0.0.1", port), timeout=timeout):
+ return True
+ except OSError:
+ return False
+
+
+def _data_dir() -> Path:
+ env_dir = os.environ.get("JARVIS_DATA_DIR")
+ if env_dir and env_dir.strip():
+ return Path(env_dir.strip()).resolve()
+ from jarvis.core.config import DATA_DIR # lazy (AP-26)
+
+ return DATA_DIR
+
+
+def _install_marker() -> Path:
+ return _data_dir() / "ollama_installed_by_jarvis.json"
+
+
+def start_server() -> tuple[bool, str]:
+ """Spawn ``ollama serve`` detached and wait for its port. ``(ok, detail)``.
+
+ Detached + window-less (AP-1), log into the data dir so the NEXT failure
+ leaves forensics. On POSIX the child gets its own session so it survives
+ the app exactly like the managed realtime server does.
+ """
+ if _server_version() is not None:
+ return True, "Ollama is already running."
+ binary = find_binary()
+ if not binary:
+ return False, "Ollama is not installed — install it first."
+ sink = None
+ try:
+ log_path = _data_dir() / "ollama_server.log"
+ log_path.parent.mkdir(parents=True, exist_ok=True)
+ sink = open(log_path, "ab") # noqa: SIM115 — handed to the child
+ except OSError:
+ log.debug("ollama-runtime: server log unavailable", exc_info=True)
+ popen_kwargs: dict[str, object] = {}
+ if os.name != "nt":
+ popen_kwargs["start_new_session"] = True
+ try:
+ subprocess.Popen( # noqa: S603 — fixed argv, resolved binary
+ [binary, "serve"],
+ stdin=subprocess.DEVNULL,
+ stdout=sink or subprocess.DEVNULL,
+ stderr=subprocess.STDOUT,
+ creationflags=NO_WINDOW_CREATIONFLAGS,
+ **popen_kwargs, # type: ignore[arg-type]
+ )
+ except OSError as exc:
+ return False, f"Could not start Ollama ({exc})."
+ finally:
+ if sink is not None:
+ sink.close()
+ port = _server_port()
+ deadline = time.monotonic() + _START_WAIT_S
+ while time.monotonic() < deadline:
+ if _port_open(port):
+ return True, "Ollama started."
+ time.sleep(_START_POLL_S)
+ return False, (
+ f"Ollama did not come up within {_START_WAIT_S:.0f} seconds — "
+ "see ollama_server.log in the Jarvis data folder."
+ )
+
+
+# ── Poll-shaped installer ────────────────────────────────────────────────
+
+_PHASES = ("idle", "downloading", "installing", "starting", "done", "error")
+
+
+@dataclass
+class _State:
+ phase: str = "idle"
+ percent: int = 0
+ detail: str = ""
+ error: str = ""
+ log_tail: deque[str] = field(default_factory=lambda: deque(maxlen=20))
+ thread: threading.Thread | None = None
+
+
+_STATE = _State()
+_LOCK = threading.Lock()
+
+
+def _set(phase: str, percent: int, detail: str = "") -> None:
+ with _LOCK:
+ _STATE.phase = phase
+ _STATE.percent = percent
+ if detail:
+ _STATE.detail = detail
+ _STATE.log_tail.append(detail)
+
+
+def _fail(message: str) -> None:
+ log.error("ollama-runtime install: %s", message)
+ with _LOCK:
+ _STATE.phase = "error"
+ _STATE.error = message
+
+
+def _reset_for_tests() -> None:
+ with _LOCK:
+ _STATE.phase = "idle"
+ _STATE.percent = 0
+ _STATE.detail = ""
+ _STATE.error = ""
+ _STATE.log_tail.clear()
+ _STATE.thread = None
+
+
+def install_snapshot() -> dict[str, object]:
+ """Poll-shaped view of the running (or last) install."""
+ with _LOCK:
+ return {
+ "phase": _STATE.phase,
+ "percent": _STATE.percent,
+ "detail": _STATE.detail,
+ "error": _STATE.error,
+ "running": _STATE.thread is not None and _STATE.thread.is_alive(),
+ "log_tail": list(_STATE.log_tail),
+ }
+
+
+def start_install() -> tuple[bool, str]:
+ """Kick off the platform-appropriate Ollama install. Returns immediately."""
+ with _LOCK:
+ if _STATE.thread is not None and _STATE.thread.is_alive():
+ return False, "an install is already running"
+ _STATE.phase = "downloading"
+ _STATE.percent = 0
+ _STATE.error = ""
+ _STATE.detail = ""
+ thread = threading.Thread(
+ target=_run_install, name="ollama-runtime-install", daemon=True
+ )
+ _STATE.thread = thread
+ thread.start()
+ return True, "install started"
+
+
+def _record_marker(method: str) -> None:
+ """Remember that JARVIS put Ollama here (enables a clean uninstall later)."""
+ try:
+ _install_marker().parent.mkdir(parents=True, exist_ok=True)
+ _install_marker().write_text(
+ json.dumps({"at": time.time(), "method": method}, indent=2),
+ encoding="utf-8",
+ )
+ except OSError: # pragma: no cover — bookkeeping only
+ log.debug("ollama-runtime: marker write failed", exc_info=True)
+
+
+def _run_command(cmd: list[str], *, timeout: int) -> None:
+ """Run one install step; stdout tail lands in the snapshot."""
+ result = subprocess.run( # noqa: S603 — fixed argv assembled above
+ cmd,
+ capture_output=True,
+ text=True,
+ encoding="utf-8",
+ errors="replace",
+ timeout=timeout,
+ creationflags=NO_WINDOW_CREATIONFLAGS,
+ )
+ tail = (result.stdout or "") + (result.stderr or "")
+ for line in tail.strip().splitlines()[-5:]:
+ with _LOCK:
+ _STATE.log_tail.append(line[:200])
+ if result.returncode != 0:
+ raise RuntimeError(
+ f"step failed (exit {result.returncode}): {' '.join(cmd[:2])}…"
+ )
+
+
+def _download(url: str, target: Path) -> None:
+ """Stream an official artifact to disk (atomic: temp name + rename)."""
+ if not url.startswith("https://ollama.com/"):
+ raise RuntimeError(f"refusing non-official download URL: {url}")
+ import httpx # lazy (AP-26)
+
+ target.parent.mkdir(parents=True, exist_ok=True)
+ staging = target.with_suffix(target.suffix + ".part")
+ with httpx.stream(
+ "GET", url, follow_redirects=True, timeout=_DOWNLOAD_TIMEOUT_S
+ ) as response:
+ response.raise_for_status()
+ total = int(response.headers.get("Content-Length", 0) or 0)
+ done = 0
+ with open(staging, "wb") as sink:
+ for chunk in response.iter_bytes(1024 * 1024):
+ sink.write(chunk)
+ done += len(chunk)
+ if total > 0:
+ _set(
+ "downloading",
+ min(40, int(40 * done / total)),
+ f"downloading Ollama ({done // (1024 * 1024)} MB)",
+ )
+ os.replace(staging, target)
+
+
+def _install_windows() -> str:
+ """winget when present (per-user, no UAC), else the official installer."""
+ winget = shutil.which("winget")
+ if winget:
+ _set("installing", 45, "installing Ollama via winget")
+ _run_command(
+ [
+ winget,
+ "install",
+ "--id",
+ "Ollama.Ollama",
+ "--silent",
+ "--accept-package-agreements",
+ "--accept-source-agreements",
+ "--disable-interactivity",
+ ],
+ timeout=_WINGET_TIMEOUT_S,
+ )
+ return "winget"
+ _set("downloading", 5, "downloading the official Ollama installer")
+ installer = _data_dir() / "downloads" / "OllamaSetup.exe"
+ _download(_WINDOWS_INSTALLER_URL, installer)
+ _set("installing", 55, "running the Ollama installer (silent)")
+ # Inno Setup switches; the Ollama installer is per-user, so no UAC.
+ _run_command(
+ [str(installer), "/VERYSILENT", "/NORESTART", "/SP-"],
+ timeout=_INSTALLER_TIMEOUT_S,
+ )
+ return "installer-exe"
+
+
+def _install_macos() -> str:
+ """Homebrew is the one automatable path; a dmg drag cannot be scripted honestly."""
+ brew = shutil.which("brew") or (
+ "/opt/homebrew/bin/brew"
+ if Path("/opt/homebrew/bin/brew").exists()
+ else ""
+ )
+ if not brew:
+ raise RuntimeError(
+ "No Homebrew on this Mac, and the Ollama.dmg needs a manual "
+ "drag-install — download it from ollama.com/download, open it "
+ "once, then come back here."
+ )
+ _set("installing", 45, "installing Ollama via Homebrew")
+ _run_command([brew, "install", "ollama"], timeout=_WINGET_TIMEOUT_S)
+ return "homebrew"
+
+
+def _install_linux() -> str:
+ """The official script — but only when sudo works WITHOUT a prompt.
+
+ The script escalates internally; running it without usable sudo would
+ hang this daemon thread on an invisible password prompt forever, which
+ is worse than an honest refusal.
+ """
+ sudo = shutil.which("sudo")
+ if sudo:
+ probe = subprocess.run( # noqa: S603 — fixed argv
+ [sudo, "-n", "true"], capture_output=True, timeout=15
+ )
+ sudo_ok = probe.returncode == 0
+ else:
+ sudo_ok = os.geteuid() == 0 if hasattr(os, "geteuid") else False
+ if not sudo_ok:
+ raise RuntimeError(
+ "Installing Ollama on Linux needs administrator rights, and "
+ "passwordless sudo is not available here. Run once in a "
+ "terminal: curl -fsSL https://ollama.com/install.sh | sh"
+ )
+ _set("downloading", 5, "downloading the official install script")
+ script = _data_dir() / "downloads" / "ollama_install.sh"
+ _download(_LINUX_INSTALL_SCRIPT_URL, script)
+ _set("installing", 45, "running the official Ollama install script")
+ shell = shutil.which("sh") or "/bin/sh"
+ _run_command([shell, str(script)], timeout=_WINGET_TIMEOUT_S)
+ return "install-script"
+
+
+def _run_install() -> None:
+ try:
+ status = runtime_status()
+ if status["running"]:
+ _set("done", 100, "Ollama is already installed and running")
+ return
+ if not status["installed"]:
+ if os.name == "nt":
+ method = _install_windows()
+ elif sys.platform == "darwin":
+ method = _install_macos()
+ else:
+ method = _install_linux()
+ _record_marker(method)
+ if not find_binary():
+ raise RuntimeError(
+ "the installer finished but no Ollama binary was found — "
+ "see the log tail above"
+ )
+ _set("starting", 85, "starting Ollama")
+ ok, detail = start_server()
+ if not ok:
+ raise RuntimeError(detail)
+ _set("done", 100, "Ollama is installed and running")
+ log.info("ollama-runtime: install completed")
+ except Exception as exc: # noqa: BLE001 — every failure must land in the state
+ _fail(str(exc))
diff --git a/jarvis/core/path_augment.py b/jarvis/core/path_augment.py
index c79266fa7b..a97d4f4ddb 100644
--- a/jarvis/core/path_augment.py
+++ b/jarvis/core/path_augment.py
@@ -93,6 +93,10 @@ def _windows_candidates() -> list[str]:
# winget's shim dir lands on the *registry* PATH only — a running
# process (and every subprocess it spawns) misses fresh installs.
dirs.append(os.path.join(local, "Microsoft", "WinGet", "Links"))
+ # Ollama's per-user installer (OllamaSetup.exe / winget) writes here
+ # and appends it to the registry PATH only — a Jarvis that just
+ # installed Ollama would otherwise not find the binary it installed.
+ dirs.append(os.path.join(local, "Programs", "Ollama"))
appdata = os.environ.get("APPDATA")
if appdata:
dirs.append(os.path.join(appdata, "npm"))
diff --git a/tests/unit/brain/test_ollama_runtime.py b/tests/unit/brain/test_ollama_runtime.py
new file mode 100644
index 0000000000..fc8b220e29
--- /dev/null
+++ b/tests/unit/brain/test_ollama_runtime.py
@@ -0,0 +1,255 @@
+"""Ollama runtime lifecycle: detect / install / start without a terminal.
+
+What these tests pin: the three-state truth an HTTP probe cannot give
+(not-installed vs installed-but-stopped vs running), the honest per-OS
+refusals (no hidden password prompt, no unofficial download URL), and the
+poll-shaped installer contract shared with the managed-server install.
+"""
+
+from __future__ import annotations
+
+import subprocess
+from pathlib import Path
+from types import SimpleNamespace
+from typing import Any
+
+import pytest
+
+from jarvis.brain import ollama_runtime
+
+
[email protected](autouse=True)
+def _fresh_state(monkeypatch, tmp_path):
+ monkeypatch.setenv("JARVIS_DATA_DIR", str(tmp_path))
+ ollama_runtime._reset_for_tests()
+ yield
+ ollama_runtime._reset_for_tests()
+
+
+# ── runtime_status: the three states ─────────────────────────────────────
+def test_running_server_reports_running(monkeypatch) -> None:
+ monkeypatch.setattr(ollama_runtime, "find_binary", lambda: "/usr/bin/ollama")
+ monkeypatch.setattr(ollama_runtime, "_server_version", lambda timeout=1.5: "0.9.1")
+ status = ollama_runtime.runtime_status()
+ assert status["installed"] is True
+ assert status["running"] is True
+ assert "running" in str(status["detail"])
+ assert status["version"] == "0.9.1"
+
+
+def test_installed_but_stopped_is_its_own_state(monkeypatch) -> None:
+ """This state needs a START button, not an INSTALL button — a pure HTTP
+ probe collapses it into 'unreachable' and offers the wrong fix."""
+ monkeypatch.setattr(ollama_runtime, "find_binary", lambda: "C:\\x\\ollama.exe")
+ monkeypatch.setattr(ollama_runtime, "_server_version", lambda timeout=1.5: None)
+ status = ollama_runtime.runtime_status()
+ assert status["installed"] is True
+ assert status["running"] is False
+ assert "not running" in str(status["detail"])
+
+
+def test_absent_binary_reports_not_installed(monkeypatch) -> None:
+ monkeypatch.setattr(ollama_runtime, "find_binary", lambda: "")
+ monkeypatch.setattr(ollama_runtime, "_server_version", lambda timeout=1.5: None)
+ status = ollama_runtime.runtime_status()
+ assert status["installed"] is False
+ assert status["running"] is False
+ assert "not installed" in str(status["detail"])
+
+
+def test_a_running_server_counts_as_installed_even_without_a_binary(
+ monkeypatch,
+) -> None:
+ """A server on a custom OLLAMA_HOST (or a nonstandard install) is real:
+ running implies installed, whatever PATH says."""
+ monkeypatch.setattr(ollama_runtime, "find_binary", lambda: "")
+ monkeypatch.setattr(ollama_runtime, "_server_version", lambda timeout=1.5: "0.9.1")
+ status = ollama_runtime.runtime_status()
+ assert status["installed"] is True
+ assert status["running"] is True
+
+
+# ── start_server ─────────────────────────────────────────────────────────
+def test_start_is_a_noop_when_already_running(monkeypatch) -> None:
+ monkeypatch.setattr(ollama_runtime, "_server_version", lambda timeout=1.5: "0.9.1")
+ ok, detail = ollama_runtime.start_server()
+ assert ok is True
+ assert "already running" in detail
+
+
+def test_start_without_a_binary_names_the_fix(monkeypatch) -> None:
+ monkeypatch.setattr(ollama_runtime, "_server_version", lambda timeout=1.5: None)
+ monkeypatch.setattr(ollama_runtime, "find_binary", lambda: "")
+ ok, detail = ollama_runtime.start_server()
+ assert ok is False
+ assert "install" in detail.lower()
+
+
+def test_start_spawns_detached_and_waits_for_the_port(monkeypatch) -> None:
+ monkeypatch.setattr(ollama_runtime, "_server_version", lambda timeout=1.5: None)
+ monkeypatch.setattr(ollama_runtime, "find_binary", lambda: "/usr/bin/ollama")
+ spawned: list[dict[str, Any]] = []
+
+ def fake_popen(argv: Any, **kwargs: Any) -> SimpleNamespace:
+ spawned.append({"argv": argv, **kwargs})
+ return SimpleNamespace(pid=4711)
+
+ monkeypatch.setattr(subprocess, "Popen", fake_popen)
+ monkeypatch.setattr(ollama_runtime, "_port_open", lambda port, timeout=1.0: True)
+ ok, detail = ollama_runtime.start_server()
+ assert ok is True
+ assert spawned[0]["argv"] == ["/usr/bin/ollama", "serve"]
+ from jarvis.core.process_utils import NO_WINDOW_CREATIONFLAGS
+
+ assert spawned[0]["creationflags"] == NO_WINDOW_CREATIONFLAGS # AP-1
+
+
+def test_start_reports_a_server_that_never_binds(monkeypatch) -> None:
+ monkeypatch.setattr(ollama_runtime, "_server_version", lambda timeout=1.5: None)
+ monkeypatch.setattr(ollama_runtime, "find_binary", lambda: "/usr/bin/ollama")
+ monkeypatch.setattr(
+ subprocess, "Popen", lambda argv, **kwargs: SimpleNamespace(pid=1)
+ )
+ monkeypatch.setattr(ollama_runtime, "_port_open", lambda port, timeout=1.0: False)
+ monkeypatch.setattr(ollama_runtime, "_START_WAIT_S", 0.1)
+ ok, detail = ollama_runtime.start_server()
+ assert ok is False
+ assert "ollama_server.log" in detail
+
+
+# ── installer: refusals and honesty ──────────────────────────────────────
+def test_download_refuses_unofficial_urls(tmp_path: Path) -> None:
+ with pytest.raises(RuntimeError, match="non-official"):
+ ollama_runtime._download("https://evil.example/o.exe", tmp_path / "o.exe")
+
+
+def test_linux_without_sudo_refuses_with_the_one_command(monkeypatch) -> None:
+ """A hidden password prompt would hang the daemon thread forever; the
+ honest refusal names the exact terminal command instead."""
+ monkeypatch.setattr(ollama_runtime.shutil, "which", lambda name: None)
+ monkeypatch.setattr(ollama_runtime.os, "geteuid", lambda: 1000, raising=False)
+ with pytest.raises(RuntimeError, match="install.sh"):
+ ollama_runtime._install_linux()
+
+
+def test_macos_without_brew_refuses_with_the_dmg_pointer(monkeypatch) -> None:
+ monkeypatch.setattr(ollama_runtime.shutil, "which", lambda name: None)
+ monkeypatch.setattr(
+ ollama_runtime.Path, "exists", lambda self: False, raising=False
+ )
+ with pytest.raises(RuntimeError, match="ollama.com/download"):
+ ollama_runtime._install_macos()
+
+
+def test_windows_prefers_winget(monkeypatch) -> None:
+ monkeypatch.setattr(
+ ollama_runtime.shutil,
+ "which",
+ lambda name: "C:\\winget.exe" if name == "winget" else None,
+ )
+ commands: list[list[str]] = []
+ monkeypatch.setattr(
+ ollama_runtime,
+ "_run_command",
+ lambda cmd, timeout: commands.append(cmd),
+ )
+ assert ollama_runtime._install_windows() == "winget"
+ assert commands[0][0] == "C:\\winget.exe"
+ assert "Ollama.Ollama" in commands[0]
+ assert "--silent" in commands[0]
+
+
+def test_windows_falls_back_to_the_official_installer(monkeypatch, tmp_path) -> None:
+ monkeypatch.setattr(ollama_runtime.shutil, "which", lambda name: None)
+ downloads: list[str] = []
+ commands: list[list[str]] = []
+ monkeypatch.setattr(
+ ollama_runtime,
+ "_download",
+ lambda url, target: downloads.append(url) or target.parent.mkdir(
+ parents=True, exist_ok=True
+ )
+ or target.write_bytes(b"exe"),
+ )
+ monkeypatch.setattr(
+ ollama_runtime, "_run_command", lambda cmd, timeout: commands.append(cmd)
+ )
+ assert ollama_runtime._install_windows() == "installer-exe"
+ assert downloads == [ollama_runtime._WINDOWS_INSTALLER_URL]
+ assert "/VERYSILENT" in commands[0]
+
+
+# ── installer: the poll-shaped job ───────────────────────────────────────
+def test_install_snapshot_shape() -> None:
+ snap = ollama_runtime.install_snapshot()
+ assert set(snap) == {"phase", "percent", "detail", "error", "running", "log_tail"}
+
+
+def test_already_running_short_circuits_to_done(monkeypatch) -> None:
+ monkeypatch.setattr(
+ ollama_runtime,
+ "runtime_status",
+ lambda: {"installed": True, "running": True, "detail": "", "version": "x", "binary": "b"},
+ )
+ ollama_runtime._run_install()
+ snap = ollama_runtime.install_snapshot()
+ assert snap["phase"] == "done"
+ assert snap["error"] == ""
+
+
+def test_installed_but_stopped_only_starts(monkeypatch) -> None:
+ monkeypatch.setattr(
+ ollama_runtime,
+ "runtime_status",
+ lambda: {"installed": True, "running": False, "detail": "", "version": "", "binary": "b"},
+ )
+ installs: list[str] = []
+ monkeypatch.setattr(
+ ollama_runtime, "_install_windows", lambda: installs.append("x") or "winget"
+ )
+ monkeypatch.setattr(
+ ollama_runtime, "start_server", lambda: (True, "Ollama started.")
+ )
+ ollama_runtime._run_install()
+ assert installs == [] # nothing was installed — it only needed a start
+ assert ollama_runtime.install_snapshot()["phase"] == "done"
+
+
+def test_a_failing_step_lands_in_the_error_state(monkeypatch) -> None:
+ monkeypatch.setattr(
+ ollama_runtime,
+ "runtime_status",
+ lambda: {"installed": True, "running": False, "detail": "", "version": "", "binary": "b"},
+ )
+ monkeypatch.setattr(
+ ollama_runtime, "start_server", lambda: (False, "did not bind")
+ )
+ ollama_runtime._run_install()
+ snap = ollama_runtime.install_snapshot()
+ assert snap["phase"] == "error"
+ assert "did not bind" in str(snap["error"])
+
+
+def test_second_start_install_joins_instead_of_duplicating(monkeypatch) -> None:
+ import threading
+
+ release = threading.Event()
+ monkeypatch.setattr(
+ ollama_runtime, "_run_install", lambda: release.wait(timeout=5)
+ )
+ started, _ = ollama_runtime.start_install()
+ assert started is True
+ joined, message = ollama_runtime.start_install()
+ assert joined is False
+ assert "already running" in message
+ release.set()
+
+
+def test_marker_records_the_method(tmp_path: Path) -> None:
+ ollama_runtime._record_marker("winget")
+ import json
+
+ payload = json.loads(
+ (tmp_path / "ollama_installed_by_jarvis.json").read_text(encoding="utf-8")
+ )
+ assert payload["method"] == "winget"