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.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
"""The "Local models" overview: one payload, painted from disk first.
Opening the section used to mean four cold requests (server, roles,
inventory, shortlist) that each ran their own probes, and two to three
seconds of nothing on screen. This module gives the section ONE payload —
:func:`build_overview` composes the four from the shared inventory snapshot
(:mod:`dodaios.brain.ollama_inventory`) — and keeps the last good one on disk
so the next open paints at once:
* :func:`get_overview` answers ``(payload, source)``. A fresh in-memory memo
is ``"live"``. Otherwise the disk snapshot is returned immediately as
``"cache"`` and ONE background refresh is scheduled (its task reference is
kept, never created at import — AP-26); a snapshot older than a day is
skipped in favour of a live build, and ``fresh=True`` forces one.
* :func:`load_snapshot` / :func:`save_snapshot` keep
``DATA_DIR/local_models_snapshot.json`` (atomic ``.part`` + ``os.replace``).
The dict builders here (:func:`model_row`, :func:`role_row`, ...) are the
ONE place the wire shape of the section's rows is spelled out; the routes
wrap them in their Pydantic models (AP-4), so the single endpoints and the
overview cannot drift apart.
"""
from __future__ import annotations
import asyncio
import functools
import json
import logging
import os
import time
from dataclasses import asdict
from pathlib import Path
from typing import Any
from dodaios.brain import ollama_inventory as inventory
from dodaios.brain import ollama_names as names
from dodaios.brain import ollama_pull, ollama_roles, ollama_runtime
from dodaios.brain.ollama_inventory import (
OllamaModelInfo,
OllamaRunningModel,
OllamaServerError,
same_model,
)
from dodaios.plugins.brain.ollama import normalize_server_root
log = logging.getLogger(__name__)
__all__ = [
"SNAPSHOT_FILE_NAME",
"DEFAULT_MAX_AGE_S",
"STALE_AFTER_S",
"build_overview",
"forget",
"get_overview",
"inventory_payload",
"is_paintable",
"load_snapshot",
"model_row",
"resident_payload",
"role_row",
"roles_payload",
"running_row",
"save_snapshot",
"server_payload",
"snapshot_path",
]
SNAPSHOT_FILE_NAME = "local_models_snapshot.json"
#: How long an in-memory overview counts as live. The section refetches on
#: this cadence too, so two opens within it cost nothing.
DEFAULT_MAX_AGE_S = 15.0
#: A disk snapshot older than this is not worth painting: a day is long
#: enough for a server to have been reinstalled or its downloads pruned.
STALE_AFTER_S = 24 * 3600.0
# ── Row builders (the wire shape, spelled out once) ──────────────────────
def _live_entry(
info: OllamaModelInfo, running: dict[str, OllamaRunningModel]
) -> OllamaRunningModel | None:
"""The ``/api/ps`` entry holding ``info``'s weights — the download itself
or one of its aliases (a Tune profile, the voice brain's context alias),
which is what is actually resident during a call or a tuned chat."""
for key, entry in running.items():
if same_model(key, info.name) or same_model(names.base_of(key, [info.name]), info.name):
return entry
return None
def model_row(
info: OllamaModelInfo, running: dict[str, OllamaRunningModel], used_by: list[str]
) -> dict[str, Any]:
"""One inventory row: the download's facts plus what ``/api/ps`` says."""
live = _live_entry(info, running)
shown = names.describe(info.name, info.parameter_size, info.quantization_level)
return {
"name": info.name,
"display_name": shown.name,
"display_label": shown.label,
"params_label": shown.params,
"quant_label": shown.quant,
"source": shown.source,
"variant": shown.variant,
"size_bytes": info.size_bytes,
"digest": info.digest,
"modified_at": info.modified_at,
"family": info.family,
"parameter_size": info.parameter_size,
"quantization_level": info.quantization_level,
"context_length": info.context_length,
"capabilities": list(info.capabilities),
"license": info.license,
"probed": info.probed,
"used_by": used_by,
"loaded": live is not None,
"loaded_as": live.name if live else "",
"size_vram_bytes": live.size_vram_bytes if live else 0,
"loaded_size_bytes": live.size_bytes if live else 0,
"expires_at": live.expires_at if live else "",
"running_context_length": live.context_length if live else None,
}
def running_row(model: OllamaRunningModel, candidates: list[str] | None = None) -> dict[str, Any]:
row = asdict(model)
row["base_tag"] = names.base_of(model.name, candidates or [])
row["kind"] = names.alias_kind(model.name) or "model"
return row
def role_row(state: ollama_roles.RoleState) -> dict[str, Any]:
spec = state.spec
return {
"id": spec.id,
"label_key": spec.label_key,
"config_key": spec.config_key,
"layout": spec.layout,
"current": state.current,
"installed": state.installed,
"current_fit": state.current_fit,
"current_reason": state.current_reason,
"required": list(spec.required),
"recommended_capabilities": list(spec.recommended),
"qualifying": list(state.qualifying),
"choices": [
{"tag": tag, "fit": fit, "reason": reason} for tag, fit, reason in state.choices
],
"downloads": [
{"tag": tag, "label": label, "size_gb": size, "fit": fit, "note": note}
for tag, label, size, fit, note in state.downloads
],
"recommended": state.recommended,
"recommended_reason": state.recommended_reason,
"writable": spec.writable,
"advanced": spec.advanced,
"note": state.note,
"context_tokens": state.context_tokens,
"context_source": state.context_source,
# The size class the job prefers (the voice brain answers within a
# breath, so it stays under 6 GB); ``None`` when the job has none.
"max_size_gb": spec.max_size_gb,
"min_context_tokens": spec.min_context_tokens,
}
#: Rough extra memory a context window costs, in GiB per 1k tokens per GiB of
#: weights — the rule of thumb the Tune suggestion and the frontend chip use.
_CONTEXT_GB_PER_K_PER_GB = 0.03
#: The window an unloaded, untuned model is assumed to open with.
_DEFAULT_CONTEXT_TOKENS = 8192
def _context_gb(tokens: int | None, weights_gb: float) -> float:
return (
(float(tokens or _DEFAULT_CONTEXT_TOKENS) / 1000.0)
* _CONTEXT_GB_PER_K_PER_GB
* max(weights_gb, 0.5)
)
def resident_payload(
states: list[ollama_roles.RoleState],
models: list[OllamaModelInfo],
running: dict[str, OllamaRunningModel],
machine: ollama_roles.Machine,
*,
voice_reserve_gb: float = 0.0,
) -> dict[str, Any]:
"""What sits in graphics memory when every job is loaded at once.
The per-card bar answers "does THIS model fit"; this answers the question
that decides a local setup — whether the chat model, the voice brain and
the embedder fit TOGETHER, because a call arrives while a chat is loaded.
One item per distinct download across the writable roles (a model on
three jobs is loaded once), each with its weights, its context estimate
(the live ``/api/ps`` figure when loaded, the rule of thumb otherwise)
and the roles it serves; plus the reserve the local speech stack keeps
free beside the voice brain. ``over`` says the total exceeds the card.
"""
by_name = {m.name: m for m in models}
items: list[dict[str, Any]] = []
seen: dict[str, dict[str, Any]] = {}
for state in states:
if not state.spec.writable or not state.current:
continue
info = next((m for m in models if same_model(m.name, state.current)), None)
if info is None:
continue
key = info.name
if key in seen:
seen[key]["roles"].append(state.spec.id)
continue
weights_gb = info.size_bytes / (1024**3)
live = _live_entry(info, running)
tokens = state.context_tokens if state.spec.id == "voice" else None
if live is not None:
context_gb = max(0.0, (live.size_bytes - info.size_bytes) / (1024**3))
tokens = live.context_length or tokens
else:
context_gb = _context_gb(tokens, weights_gb)
shown = names.describe(info.name, info.parameter_size, info.quantization_level)
item = {
"tag": info.name,
"display_label": shown.label,
"roles": [state.spec.id],
"weights_gb": round(weights_gb, 2),
"context_gb": round(context_gb, 2),
"context_tokens": tokens,
"loaded": live is not None,
}
seen[key] = item
items.append(item)
del by_name
total = sum(i["weights_gb"] + i["context_gb"] for i in items) + voice_reserve_gb
accelerator = machine.accelerator_gb
return {
"items": items,
"reserve_gb": round(voice_reserve_gb, 2),
"total_gb": round(total, 2),
"accelerator_gb": accelerator,
"over": bool(accelerator > 0 and total > accelerator),
}
# ── Payloads (what the single endpoints answer) ──────────────────────────
async def _snapshot(root: str) -> tuple[inventory.InventorySnapshot | None, str | None]:
"""The shared snapshot, or ``(None, sentence)`` when the server is down."""
try:
return await inventory.cached_snapshot(root), None
except OllamaServerError as exc:
return None, str(exc)
def _inventory_from(
provider_id: str,
root: str,
cfg: Any,
snapshot: inventory.InventorySnapshot | None,
error: str | None,
) -> dict[str, Any]:
if snapshot is None:
return {
"provider": provider_id,
"server": root,
"models": [],
"running": [],
"disk_bytes": 0,
"loaded_vram_bytes": 0,
"error": error,
}
running = {r.name: r for r in snapshot.running}
candidates = [m.name for m in snapshot.models]
return {
"provider": provider_id,
"server": root,
"models": [
model_row(m, running, ollama_roles.roles_using(cfg, m.name) if cfg else [])
for m in snapshot.models
],
"running": [running_row(r, candidates) for r in snapshot.running],
"disk_bytes": sum(m.size_bytes for m in snapshot.models),
"loaded_vram_bytes": sum(r.size_vram_bytes for r in snapshot.running),
"error": None,
}
async def inventory_payload(provider_id: str, root: str, cfg: Any) -> dict[str, Any]:
"""Every download with its facts, what is loaded, and the disk total —
the body of ``GET .../inventory``."""
snapshot, error = await _snapshot(root)
return _inventory_from(provider_id, root, cfg, snapshot, error)
async def _roles_from(
provider_id: str,
root: str,
cfg: Any,
snapshot: inventory.InventorySnapshot | None,
error: str | None,
shortlist: list[dict[str, Any]] | None,
machine: ollama_roles.Machine | None = None,
) -> dict[str, Any]:
models = list(snapshot.models) if snapshot is not None else []
states, _unused = await ollama_roles.list_roles(
root, cfg, models=models, shortlist=shortlist, machine=machine
)
running = {r.name: r for r in snapshot.running} if snapshot is not None else {}
return {
"provider": provider_id,
"server": root,
"roles": [role_row(s) for s in states],
"resident": resident_payload(
states,
models,
running,
machine or ollama_roles.Machine(),
voice_reserve_gb=_voice_reserve_gb(states),
),
"error": error,
}
def _voice_reserve_gb(states: list[ollama_roles.RoleState]) -> float:
"""The memory the local speech stack keeps free beside the voice brain —
only when a voice brain is picked (the stack is installed and configured)."""
voice = next((s for s in states if s.spec.id == "voice"), None)
if voice is None or not voice.current:
return 0.0
try:
from dodaios.realtime.local_server.supervisor import VOICE_STACK_RESERVE_GB
except Exception: # noqa: BLE001 — the reserve is an estimate, never a blocker
return 0.0
return float(VOICE_STACK_RESERVE_GB)
async def roles_payload(provider_id: str, root: str, cfg: Any) -> dict[str, Any]:
"""Every role with its pick, what qualifies, and the recommendation —
the body of ``GET .../roles``."""
snapshot, error = await _snapshot(root)
return await _roles_from(provider_id, root, cfg, snapshot, error, None)
async def _server_from(status: dict[str, object], fallback_root: str) -> dict[str, Any]:
root = str(status.get("base_url") or fallback_root)
running: list[dict[str, Any]] = []
disk = 0
error: str | None = None
if status.get("running"):
snapshot, error = await _snapshot(root)
if snapshot is not None:
disk = sum(m.size_bytes for m in snapshot.models)
running = [running_row(r) for r in snapshot.running]
else:
log.info("local-models: inventory unavailable at %s: %s", root, error)
return {
"installed": bool(status.get("installed")),
"binary": str(status.get("binary") or ""),
"running": bool(status.get("running")),
"starting": bool(status.get("starting")),
"version": str(status.get("version") or ""),
"detail": str(status.get("detail") or ""),
"base_url": root,
"host_kind": str(status.get("host_kind") or "local"),
"models_dir": str(status.get("models_dir") or ""),
"running_models": running,
"disk_bytes": disk,
"loaded_vram_bytes": sum(int(r.get("size_vram_bytes") or 0) for r in running),
"error": error,
}
async def server_payload(root: str) -> dict[str, Any]:
"""Runtime picture plus what is loaded and the disk total — the body of
``GET .../server``. The runtime probe is synchronous and runs off-loop."""
status = await asyncio.to_thread(ollama_runtime.runtime_status)
return await _server_from(status, root)
async def _recommendations() -> dict[str, Any]:
try:
return await ollama_pull.recommendations()
except Exception as exc: # noqa: BLE001 — the shortlist is advisory, the overview is not
log.warning("local-models: shortlist unavailable: %s", exc)
return {"models": [], "error": str(exc)}
async def build_overview(root: str, cfg: Any, *, provider_id: str = "ollama") -> dict[str, Any]:
"""Compose server, roles, inventory and the shortlist from ONE sweep.
The runtime probe, the shortlist (registry + hardware) and the inventory
snapshot run concurrently; the payload carries ``fetched_at`` (epoch
seconds) so a reader can say how old it is.
"""
root = normalize_server_root(root)
status, recommended, (snapshot, error) = await asyncio.gather(
asyncio.to_thread(ollama_runtime.runtime_status),
_recommendations(),
_snapshot(root),
)
shortlist = recommended.get("models") if isinstance(recommended, dict) else None
roles = await _roles_from(
provider_id,
root,
cfg,
snapshot,
error,
shortlist if isinstance(shortlist, list) else [],
machine=ollama_roles.machine_from(recommended),
)
server = await _server_from(status, root)
return {
"server": server,
"roles": roles,
"inventory": _inventory_from(provider_id, root, cfg, snapshot, error),
"recommended": recommended,
"fetched_at": time.time(),
}
# ── Disk snapshot ────────────────────────────────────────────────────────
def _data_dir() -> Path:
env_dir = os.environ.get("JARVIS_DATA_DIR")
if env_dir and env_dir.strip():
return Path(env_dir.strip())
from dodaios.core.config import DATA_DIR # lazy (AP-26)
return DATA_DIR
def snapshot_path() -> Path:
return _data_dir() / SNAPSHOT_FILE_NAME
def load_snapshot(path: Path | None = None) -> dict[str, Any] | None:
"""The saved overview ``{"root", "fetched_at", "payload"}`` or ``None``.
A missing file is the normal first-run state; an unreadable or malformed
one is logged and treated the same — the next live build overwrites it.
"""
target = path or snapshot_path()
try:
raw = target.read_text(encoding="utf-8")
except FileNotFoundError:
return None
except OSError:
log.warning("local-models: snapshot %s unreadable", target, exc_info=True)
return None
try:
data = json.loads(raw)
except ValueError:
log.warning("local-models: snapshot %s is not JSON; ignoring it", target)
return None
if (
not isinstance(data, dict)
or not isinstance(data.get("payload"), dict)
or not isinstance(data.get("fetched_at"), (int, float))
):
log.warning("local-models: snapshot %s has an unexpected shape; ignoring it", target)
return None
return data
def is_paintable(payload: dict[str, Any]) -> bool:
"""Whether ``payload`` may be cached as the next open's head start.
A sweep taken while the server was unreachable — Ollama still booting,
a stopped service, a remote host off the network — is a truthful answer
to "what can I see right now", but a ruinous thing to paint later: its
inventory is empty, so every role reads "not installed", every shortlist
reads "nothing installed fits this job yet", and each role's picker
offers only the tag already configured. Keeping the previous, honest
snapshot instead means the next open paints stale facts rather than a
machine that looks wiped (BUG-188).
Only that one condition disqualifies a payload: this is a guard against a
known lie, not a schema check, so anything else is cached as before.
"""
inv = payload.get("inventory")
return not (isinstance(inv, dict) and inv.get("error"))
def save_snapshot(root: str, payload: dict[str, Any], path: Path | None = None) -> None:
"""Persist ``payload`` atomically (``.part`` + ``os.replace``).
A crash mid-write leaves the previous snapshot intact; a write failure
is logged and swallowed on purpose — the overview was already answered
live, the cache is only the next open's head start. A payload that fails
:func:`is_paintable` is not written at all.
"""
if not is_paintable(payload):
log.info(
"local-models: not caching the overview for %s — the server was "
"unreachable, so its inventory is empty; the previous snapshot stays",
root,
)
return
target = path or snapshot_path()
part = target.with_suffix(target.suffix + ".part")
data = {"root": root, "fetched_at": float(payload.get("fetched_at") or time.time())}
data["payload"] = payload
try:
target.parent.mkdir(parents=True, exist_ok=True)
part.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
os.replace(part, target)
except OSError:
log.warning("local-models: could not save the snapshot to %s", target, exc_info=True)
try:
part.unlink(missing_ok=True)
except OSError:
log.debug("local-models: stale .part left at %s", part, exc_info=True)
# ── Stale-while-revalidate ───────────────────────────────────────────────
_memo: dict[str, tuple[float, dict[str, Any]]] = {} # root -> (monotonic, payload)
#: Roots whose memo was dropped by a write and whose disk snapshot is
#: therefore known-stale: the next open builds live instead of painting it.
_dirty: set[str] = set()
#: The one background refresh per root; the reference is kept so the task
#: cannot be garbage-collected mid-flight, and a second open joins it.
_refresh_tasks: dict[str, asyncio.Task[dict[str, Any]]] = {}
def _reset_for_tests() -> None:
"""Drop the in-memory overview memo and refresh bookkeeping (tests only)."""
_memo.clear()
_refresh_tasks.clear()
_dirty.clear()
def forget(root: str) -> None:
"""Drop the memoised overview for ``root`` after a write.
A role pick, a tune, an unload or a delete changes what the next open
must show; without this the memo answered the OLD payload for up to
:data:`DEFAULT_MAX_AGE_S` after the write and the row the user had just
changed stayed where it was. The disk snapshot is known-stale from here
on too, so the next plain read builds live rather than painting it —
every client sees the write, not only one that asks for ``fresh``.
"""
key = normalize_server_root(root)
_memo.pop(key, None)
_dirty.add(key)
def _fresh(root: str, max_age_s: float) -> dict[str, Any] | None:
hit = _memo.get(root)
if hit is not None and time.monotonic() - hit[0] < max_age_s:
return hit[1]
return None
async def _refresh(root: str, cfg: Any, provider_id: str) -> dict[str, Any]:
"""Build live, memoise, save to disk (off-loop); returns the payload."""
payload = await build_overview(root, cfg, provider_id=provider_id)
_memo[root] = (time.monotonic(), payload)
_dirty.discard(root)
await asyncio.to_thread(save_snapshot, root, payload)
return payload
def _refresh_done(root: str, task: asyncio.Task[dict[str, Any]]) -> None:
_refresh_tasks.pop(root, None)
if task.cancelled():
log.info("local-models: background refresh for %s was cancelled", root)
return
exc = task.exception()
if exc is not None:
log.warning("local-models: background refresh for %s failed: %s", root, exc)
def _schedule_refresh(root: str, cfg: Any, provider_id: str) -> asyncio.Task[dict[str, Any]]:
"""ONE refresh per root: a second open while one runs joins it."""
running = _refresh_tasks.get(root)
if running is not None and not running.done():
return running
task = asyncio.create_task(
_refresh(root, cfg, provider_id), name=f"local-models-overview-refresh-{root}"
)
_refresh_tasks[root] = task
task.add_done_callback(functools.partial(_refresh_done, root))
return task
async def get_overview(
root: str,
cfg: Any,
*,
provider_id: str = "ollama",
max_age_s: float = DEFAULT_MAX_AGE_S,
fresh: bool = False,
) -> tuple[dict[str, Any], str]:
"""``(payload, source)`` — ``"live"`` or ``"cache"``.
Order of preference: a live payload younger than ``max_age_s`` in memory;
the disk snapshot (younger than :data:`STALE_AFTER_S`, same server) —
returned at once with ONE background refresh scheduled; a live build,
which also seeds memory and disk. ``fresh`` skips straight to the build.
When the build itself fails (a bug, not an offline server — that is a
normal payload) and a disk snapshot exists, the snapshot is the answer.
"""
root = normalize_server_root(root)
disk: dict[str, Any] | None = None
# After a write (`forget`) both the memo and the disk snapshot are known
# stale, so a plain read goes straight to the live build as well.
if not fresh and root not in _dirty:
if (hit := _fresh(root, max_age_s)) is not None:
return hit, "live"
disk = await asyncio.to_thread(load_snapshot)
if disk is not None and disk.get("root") != root:
disk = None
# A snapshot written before `is_paintable` existed can still hold an
# offline sweep; painting it would show an empty machine for up to
# STALE_AFTER_S. Build live instead and let that answer replace it.
if disk is not None and not is_paintable(disk["payload"]):
log.info(
"local-models: the saved overview for %s is an offline sweep; rebuilding", root
)
disk = None
if disk is not None and time.time() - float(disk["fetched_at"]) < STALE_AFTER_S:
_schedule_refresh(root, cfg, provider_id)
return dict(disk["payload"]), "cache"
try:
return await _refresh(root, cfg, provider_id), "live"
except Exception:
if disk is None:
raise
log.warning(
"local-models: live overview failed; answering the disk snapshot", exc_info=True
)
return dict(disk["payload"]), "cache"