Python
What this machine is, and what Ollama's models can do
Devika Verma5d9e1fc38ad3
diff --git a/coworker/providers/local_machine.py b/coworker/providers/local_machine.pyindex 8c542139..c6553036 100644--- a/coworker/providers/local_machine.py+++ b/coworker/providers/local_machine.py@@ -18,10 +18,11 @@ from __future__ import annotationsimport functoolsimport os+import reimport shutilimport subprocessimport sys-from typing import Optional+from typing import Any, OptionalGB = 1024**3@@ -118,3 +119,111 @@ def recommended_context(if model_max:tier = min(tier, model_max)return max(tier, min(MIN_AGENT_CONTEXT, model_max or MIN_AGENT_CONTEXT))+++# -- what this machine is -----------------------------------------------------------------+# For the "Your system" section and the fit advice next to each model. Read on request+# and cached: none of it changes while OpenWorker runs except free storage, which is+# re-read each time.++# A model "runs well" when its file leaves this much of the memory for context and the+# rest of the system; "tight" up to the second share; beyond that it is too large.+RUNS_WELL_SHARE = 0.65+TIGHT_SHARE = 0.85+++def _read(path: str) -> str:+ try:+ with open(path, encoding="utf-8", errors="replace") as fh:+ return fh.read().strip().strip("\x00")+ except OSError:+ return ""+++def _run(args: list[str]) -> str:+ try:+ return subprocess.run(args, capture_output=True, text=True, timeout=5, check=False).stdout.strip()+ except (OSError, subprocess.SubprocessError):+ return ""+++def _nvidia_gpu_name() -> str:+ exe = shutil.which("nvidia-smi")+ if not exe:+ return ""+ out = _run([exe, "--query-gpu=name", "--format=csv,noheader"])+ return out.splitlines()[0].strip() if out else ""++[email protected]_cache(maxsize=1)+def _static_facts() -> dict[str, Any]:+ """Processor, graphics, memory and the machine's kind. Cached for the process."""+ import platform++ processor = ""+ graphics = ""+ kind = "cpu"+ if sys.platform == "darwin":+ processor = _run(["sysctl", "-n", "machdep.cpu.brand_string"])+ if processor.startswith("Apple"):+ kind = "apple_silicon"+ graphics = "Built into the chip, shares the memory"+ elif sys.platform.startswith("linux"):+ for line in _read("/proc/cpuinfo").splitlines():+ if line.lower().startswith("model name"):+ processor = line.split(":", 1)[1].strip()+ break+ product = " ".join(+ _read(p) for p in ("/sys/class/dmi/id/product_name", "/sys/firmware/devicetree/base/model")+ )+ if re.search(r"DGX[ _-]*Spark", product, re.IGNORECASE):+ kind = "dgx_spark"+ graphics = "NVIDIA GB10, shares the memory"+ elif re.search(r"Jetson|Tegra|Orin|Thor", product, re.IGNORECASE):+ kind = "jetson"+ graphics = product.strip() + ", shares the memory"+ else:+ processor = platform.processor() or ""+ if kind == "cpu":+ name = _nvidia_gpu_name()+ vram = nvidia_gpu_memory_bytes()+ if name:+ kind = "nvidia"+ graphics = f"{name} · {round(vram / GB)} GB" if vram else name+ return {+ "processor": processor or platform.machine() or "Unknown",+ "graphics": graphics or "None found",+ "kind": kind,+ "memory_bytes": system_memory_bytes(),+ "gpu_memory_bytes": nvidia_gpu_memory_bytes() if kind == "nvidia" else None,+ "platform": sys.platform,+ }+++def system_facts() -> dict[str, Any]:+ """Facts for the "Your system" section, with the memory models load into and the+ largest model file that runs well here."""+ facts = dict(_static_facts())+ try:+ usage = shutil.disk_usage(os.path.expanduser("~"))+ facts["storage_free_bytes"] = int(usage.free)+ facts["storage_total_bytes"] = int(usage.total)+ except OSError:+ facts["storage_free_bytes"] = facts["storage_total_bytes"] = None+ memory = model_memory_bytes()+ facts["model_memory_bytes"] = memory+ facts["runs_well_up_to_bytes"] = int(memory * RUNS_WELL_SHARE) if memory else None+ return facts+++def fit_for(model_bytes: Optional[int], memory_bytes: Optional[int] = None) -> str:+ """How a model file of this size sits on this machine: `runs_well`, `tight`,+ `too_large`, or `unknown`."""+ memory = memory_bytes if memory_bytes is not None else model_memory_bytes()+ if not model_bytes or not memory:+ return "unknown"+ if model_bytes <= memory * RUNS_WELL_SHARE:+ return "runs_well"+ if model_bytes <= memory * TIGHT_SHARE:+ return "tight"+ return "too_large"diff --git a/coworker/providers/ollama_facts.py b/coworker/providers/ollama_facts.pynew file mode 100644index 00000000..1d753522--- /dev/null+++ b/coworker/providers/ollama_facts.py@@ -0,0 +1,105 @@+"""What Ollama knows about the models on this machine, for the models table.++`/api/tags` lists the pulled models with their file size; `/api/show` says what each can+do (`capabilities`: `tools`, `thinking`, `vision`, ...) and the window it was trained for.+OpenWorker used to guess both. Each row also carries how the model sits on this machine+(local_machine.fit_for), the window it would be loaded with (ollama_context.resolve_num_ctx,+or the size the user saved), and the recommended-settings entry if there is one.++Answers are cached for a minute per server: the settings page asks often, the list+changes rarely, and `/api/show` is one request per model.+"""++from __future__ import annotations++import threading+import time+from typing import Any, Optional++import httpx++from .. import model_config+from . import local_machine+from .ollama_context import context_length_from_show, resolve_num_ctx+from .recommended import recommendation_for++CACHE_SECONDS = 60.0+_cache: dict[str, tuple[float, list[dict[str, Any]]]] = {}+_lock = threading.Lock()+++def native_base(base_url: Optional[str]) -> str:+ base = (base_url or "http://localhost:11434").strip().rstrip("/")+ return base[: -len("/v1")] if base.endswith("/v1") else base+++def model_facts(base_url: Optional[str] = None, *, fresh: bool = False) -> list[dict[str, Any]]:+ """One row per pulled model. Empty when Ollama does not answer. Never raises."""+ base = native_base(base_url)+ now = time.monotonic()+ with _lock:+ hit = _cache.get(base)+ if hit and not fresh and now - hit[0] < CACHE_SECONDS:+ return [dict(r) for r in hit[1]]+ rows = _fetch(base)+ with _lock:+ _cache[base] = (now, rows)+ return [dict(r) for r in rows]+++def forget(base_url: Optional[str] = None) -> None:+ with _lock:+ _cache.pop(native_base(base_url), None)+++def forget_all() -> None:+ with _lock:+ _cache.clear()+++def _fetch(base: str) -> list[dict[str, Any]]:+ # Module-level httpx calls, so a test can stand the server in with two fakes.+ try:+ tags = httpx.get(base + "/api/tags", timeout=4.0).json()+ except (httpx.HTTPError, ValueError, OSError):+ return []+ rows = []+ for item in tags.get("models") or []:+ name = item.get("name")+ if not name:+ continue+ show: dict[str, Any] = {}+ try:+ show = httpx.post(base + "/api/show", json={"model": name}, timeout=4.0).json()+ except (httpx.HTTPError, ValueError, OSError):+ show = {}+ rows.append(_row(name, item, show if isinstance(show, dict) else {}))+ return rows+++def _row(name: str, tag: dict[str, Any], show: dict[str, Any]) -> dict[str, Any]:+ raw_caps = show.get("capabilities") or tag.get("capabilities")+ caps = [str(c) for c in raw_caps] if isinstance(raw_caps, list) else None+ details = show.get("details") or tag.get("details") or {}+ size = tag.get("size") if isinstance(tag.get("size"), int) else None+ model_id = f"ollama:{name}"+ rec = recommendation_for(model_id)+ context_max = context_length_from_show(show) if show else None+ chosen = model_config.context_size_for(model_id)+ return {+ "model": model_id,+ "name": name,+ "size_bytes": size,+ # None when the server did not say (an older Ollama): not known, not "no".+ "tools": ("tools" in caps) if caps is not None else None,+ "thinking": ("thinking" in caps) if caps is not None else None,+ "vision": ("vision" in caps) if caps is not None else None,+ "remote": bool(tag.get("remote_model") or tag.get("remote_host")),+ "parameter_size": details.get("parameter_size"),+ "quantization": details.get("quantization_level"),+ "context_max": context_max,+ "context": chosen or (resolve_num_ctx(show) if show else None),+ "context_from": "user" if chosen else "machine",+ "fit": "cloud" if tag.get("remote_model") else local_machine.fit_for(size),+ "recommendation": rec.name if rec else None,+ }diff --git a/coworker/server/app.py b/coworker/server/app.pyindex 7bbbb6f6..da1e0fdb 100644--- a/coworker/server/app.py+++ b/coworker/server/app.py@@ -2318,6 +2318,14 @@ def create_app(manager: SessionManager) -> FastAPI:def providers_remove(name: str) -> dict[str, Any]:return manager.remove_provider(name)+ @app.get("/v1/providers/{name}/models")+ def providers_local_models(name: str) -> dict[str, Any]:+ return manager.local_model_facts(name)++ @app.get("/v1/system")+ def system_get() -> dict[str, Any]:+ return manager.system_facts()+@app.post("/v1/providers/verify")async def providers_verify(body: dict) -> dict[str, Any]:# Live read-only credential check (sync httpx) — run off the event loop.diff --git a/coworker/server/manager.py b/coworker/server/manager.pyindex 9e49dfed..e8c7d790 100644--- a/coworker/server/manager.py+++ b/coworker/server/manager.py@@ -4401,19 +4401,36 @@ class SessionManager:`/api/tags`), as `ollama:<name>` so they're directly selectable. Empty if Ollama isunreachable — best-effort, never raises. No stored profile → the default localhostendpoint, same as `_ollama_alive` (keyless Ollama needs no saved form)."""+ from ..providers import ollama_facts+profile = self.secrets.get("provider:ollama") or {}- base = (profile.get("base_url") or "http://localhost:11434").strip().rstrip("/")- if base.endswith("/v1"):- base = base[: -len("/v1")]- try:- import httpx+ # Only models that can use tools: a coworker cannot work with the others, so+ # they stay off the picker (the settings page still lists them, greyed). A+ # server that does not say (an older Ollama) keeps every model in.+ return [+ row["model"]+ for row in ollama_facts.model_facts(profile.get("base_url"))+ if row.get("tools") is not False+ ]- data = httpx.get(base + "/api/tags", timeout=2.0).json()- return [- f"ollama:{m['name']}" for m in data.get("models", []) if m.get("name")- ]- except Exception:- return []+ def local_model_facts(self, name: str) -> dict[str, Any]:+ """The models a local provider has, with size, tool support, thinking, context+ and how each sits on this machine. Ollama today; llama.cpp and vLLM report+ their own single model the same way once connected."""+ from ..providers import local_machine, ollama_facts++ if name == "ollama":+ profile = self.secrets.get("provider:ollama") or {}+ rows = ollama_facts.model_facts(profile.get("base_url"), fresh=True)+ return {"provider": name, "models": rows, "alive": self._ollama_alive()}+ return {"provider": name, "models": [], "error": "no local model list for this provider"}++ def system_facts(self) -> dict[str, Any]:+ """This machine, for the "Your system" section: processor, graphics, memory,+ storage, and the largest model file that runs well here."""+ from ..providers import local_machine++ return local_machine.system_facts()def model_selectable(self, model: str) -> bool:"""Can this machine run `model` right now? Its provider has a key — or, for thediff --git a/tests/conftest.py b/tests/conftest.pyindex 392a40df..871de690 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -48,3 +48,14 @@ async def fake_slack(monkeypatch):yield fakefinally:await fake.stop()++[email protected](autouse=True)+def _no_cached_ollama_facts():+ """Model facts are cached per Ollama server for a minute (providers/ollama_facts.py);+ a test must never see another test's fake server."""+ from coworker.providers import ollama_facts++ ollama_facts.forget_all()+ yield+ ollama_facts.forget_all()diff --git a/tests/test_model_config.py b/tests/test_model_config.pyindex ec061cdf..8d46746f 100644--- a/tests/test_model_config.py+++ b/tests/test_model_config.py@@ -160,3 +160,71 @@ def test_manager_saves_reads_and_removes_model_settings(tmp_path, monkeypatch):assert mgr.set_model_config("x", {"temperature": 9}) == {"ok": False, "error": "temperature must be between 0 and 2"}assert mgr.remove_model_config("ollama:qwen3-coder:30b") == {"ok": True}assert mgr.get_model_config()["models"] == {}+++# -- the machine and the models on it -------------------------------------------------------+++def test_fit_and_system_facts(monkeypatch):+ GB = local_machine.GB+ assert local_machine.fit_for(25 * GB, 96 * GB) == "runs_well"+ assert local_machine.fit_for(70 * GB, 96 * GB) == "tight"+ assert local_machine.fit_for(87 * GB, 96 * GB) == "too_large"+ assert local_machine.fit_for(None, 96 * GB) == "unknown"+ facts = local_machine.system_facts()+ for key in ("processor", "graphics", "kind", "memory_bytes", "storage_free_bytes", "runs_well_up_to_bytes"):+ assert key in facts+ assert facts["kind"] in ("apple_silicon", "nvidia", "dgx_spark", "jetson", "cpu")+++def test_ollama_model_facts_read_tools_thinking_and_context(monkeypatch):+ from coworker.providers import ollama_facts++ tags = {"models": [+ {"name": "nemotron-3.5-lightning:30b", "size": 25 * local_machine.GB, "details": {"parameter_size": "30.5B", "quantization_level": "Q4_K_M"}},+ {"name": "gemma3:12b", "size": 8 * local_machine.GB, "details": {}},+ {"name": "nemotron-3-super:cloud", "size": 0, "remote_model": "nemotron-3-super:cloud", "remote_host": "https://ollama.com"},+ ]}+ shows = {+ "nemotron-3.5-lightning:30b": {"capabilities": ["completion", "tools", "thinking"], "model_info": {"general.architecture": "nemotron", "nemotron.context_length": 1048576}},+ "gemma3:12b": {"capabilities": ["completion", "vision"], "model_info": {"general.architecture": "gemma3", "gemma3.context_length": 131072}},+ "nemotron-3-super:cloud": {"capabilities": ["completion", "tools", "thinking"], "model_info": {}},+ }++ def handler(request: httpx.Request) -> httpx.Response:+ if request.url.path == "/api/tags":+ return httpx.Response(200, json=tags)+ name = json.loads(request.content)["model"]+ return httpx.Response(200, json=shows[name])++ fake = httpx.Client(transport=httpx.MockTransport(handler))+ monkeypatch.setattr(httpx, "get", lambda url, timeout=None: fake.get(url))+ monkeypatch.setattr(httpx, "post", lambda url, json=None, timeout=None: fake.post(url, json=json))+ monkeypatch.setattr(local_machine, "model_memory_bytes", lambda: 32 * local_machine.GB)+ ollama_facts.forget_all()+ rows = {r["name"]: r for r in ollama_facts.model_facts("http://localhost:11434/v1", fresh=True)}+ lightning = rows["nemotron-3.5-lightning:30b"]+ assert lightning["tools"] and lightning["thinking"] and not lightning["vision"]+ assert lightning["context_max"] == 1048576 and lightning["context"] == 65536 # the 32 GB tier+ assert lightning["fit"] == "tight" and lightning["recommendation"] == "NVIDIA Nemotron 3.5 Lightning" # 25 of 32 GB+ gemma = rows["gemma3:12b"]+ assert not gemma["tools"] and gemma["vision"] and gemma["fit"] == "runs_well"+ cloud = rows["nemotron-3-super:cloud"]+ assert cloud["remote"] and cloud["fit"] == "cloud"+ # A saved context size shows as the user's.+ model_config.set("ollama:nemotron-3.5-lightning:30b", {"context_size": 32768})+ row = {r["name"]: r for r in ollama_facts.model_facts("http://localhost:11434/v1", fresh=True)}["nemotron-3.5-lightning:30b"]+ assert row["context"] == 32768 and row["context_from"] == "user"+++def test_only_tool_capable_ollama_models_join_the_picker(tmp_path, monkeypatch):+ from coworker.providers import ollama_facts+ from coworker.server.manager import SessionManager++ monkeypatch.setattr(ollama_facts, "model_facts", lambda base=None, fresh=False: [+ {"model": "ollama:qwen3-coder:30b", "tools": True},+ {"model": "ollama:gemma3:12b", "tools": False},+ {"model": "ollama:old-server-model", "tools": None}, # an older Ollama does not say+ ])+ mgr = SessionManager(data_dir=tmp_path)+ assert mgr._ollama_models() == ["ollama:qwen3-coder:30b", "ollama:old-server-model"]diff --git a/tests/test_settings.py b/tests/test_settings.pyindex 8161e7ba..0ce9bcbc 100644--- a/tests/test_settings.py+++ b/tests/test_settings.py@@ -187,13 +187,23 @@ def _ollama_manager(tmp_path, monkeypatch, pulled=("qwen2.5:7b-instruct",)):monkeypatch.delenv("OPENAI_API_KEY", raising=False)monkeypatch.setenv("COWORKER_STATE_DIR", str(tmp_path / "state"))+ from coworker.providers import ollama_facts+class _Tags:status_code = 200def json(self):return {"models": [{"name": n} for n in pulled]}+ class _Show:+ status_code = 200++ def json(self):+ return {"capabilities": ["completion", "tools"]}+monkeypatch.setattr(httpx, "get", lambda url, timeout=None: _Tags())+ monkeypatch.setattr(httpx, "post", lambda url, json=None, timeout=None: _Show())+ ollama_facts.forget_all() # answers are cached per server; never across testsreturn SessionManager(data_dir=tmp_path / "data")