ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.
Python ai-agentsamdcomfyuidockerllama-cppllmlocal-ain8nnvidiaopen-webuiragself-hostedspeech-to-textstrix-halotext-to-speechworkflow-automation
"""Version checking and update endpoints."""
import asyncio
import json
import logging
import math
import os
import re
import time
from datetime import datetime, timezone
from pathlib import Path
from typing import Optional
import httpx
from fastapi import APIRouter, Depends, HTTPException
from config import INSTALL_DIR
from env_values import parse_env_value
from host_agent_client import (
AgentHTTPError,
AgentUnavailable,
request_text as request_agent_text,
)
from models import VersionInfo, UpdateAction
from security import verify_api_key
logger = logging.getLogger(__name__)
router = APIRouter(tags=["updates"])
_VALID_ACTIONS = {"check", "backup", "update"}
_GITHUB_REPOSITORY = "Osmantic/ODS"
_GITHUB_RELEASES_API = f"https://api.github.com/repos/{_GITHUB_REPOSITORY}/releases"
_GITHUB_RELEASES_PAGE = f"https://github.com/{_GITHUB_REPOSITORY}/releases"
_GITHUB_HEADERS = {"Accept": "application/vnd.github.v3+json"}
_VERSION_CACHE_TTL = 300.0
_version_cache: dict[str, object] = {"expires_at": 0.0, "payload": None}
_version_refresh_task: Optional[asyncio.Task] = None
# A failed check holds further GitHub requests. The dashboard asks again every
# minute while a check is unavailable, and GitHub allows 60 unauthenticated
# requests an hour per address, which every machine behind a shared NAT spends.
_VERSION_RETRY_SECONDS = 300.0
_VERSION_MAX_HOLD_SECONDS = 3600.0
_version_hold: dict[str, object] = {"until": 0.0, "rate_limited": False}
def _read_utf8(path: Path) -> str:
"""Read repository text files consistently across Windows and Linux."""
return path.read_text(encoding="utf-8")
def _read_current_version() -> str:
"""Read installed version from .env (preferred) or .version file."""
env_file = Path(INSTALL_DIR) / ".env"
if env_file.exists():
try:
for line in _read_utf8(env_file).splitlines():
if line.startswith("ODS_VERSION="):
version = parse_env_value(line.split("=", 1)[1])
if version.strip():
return version
except OSError:
pass
version_file = Path(INSTALL_DIR) / ".version"
if version_file.exists():
try:
raw = _read_utf8(version_file).strip()
if raw:
if raw.startswith("{"):
data = json.loads(raw)
if isinstance(data, dict) and data.get("version"):
return str(data["version"])
else:
return raw
except (OSError, json.JSONDecodeError, ValueError):
pass
manifest_file = Path(INSTALL_DIR) / "manifest.json"
if manifest_file.exists():
try:
data = json.loads(_read_utf8(manifest_file))
version = (
data.get("release", {}).get("version")
or data.get("ods_version")
)
if version:
return str(version)
except (OSError, json.JSONDecodeError, ValueError, AttributeError):
pass
main_file = Path(__file__).resolve().parents[1] / "main.py"
if main_file.exists():
try:
match = re.search(r'version\s*=\s*"([^"]+)"', _read_utf8(main_file))
if match:
return match.group(1)
except OSError:
pass
return "0.0.0"
def _call_update_agent(endpoint_action: str, payload: dict, timeout: int) -> dict:
"""Call the host agent for update execution."""
try:
raw = request_agent_text(
"POST",
f"/v1/update/{endpoint_action}",
payload=payload,
timeout=timeout,
)
except AgentHTTPError as exc:
raise HTTPException(status_code=exc.status_code, detail=exc.detail) from exc
except AgentUnavailable as exc:
raise HTTPException(status_code=503, detail=f"Host agent unreachable: {exc}") from exc
try:
parsed = json.loads(raw or "{}")
except json.JSONDecodeError:
parsed = {"success": False, "output": raw}
return parsed if isinstance(parsed, dict) else {"success": False, "output": raw}
def _get_update_agent_status(timeout: int = 5) -> dict:
try:
raw = request_agent_text("GET", "/v1/update/status", timeout=timeout)
except AgentHTTPError as exc:
raise HTTPException(status_code=exc.status_code, detail=exc.detail) from exc
except AgentUnavailable as exc:
raise HTTPException(status_code=503, detail=f"Host agent unreachable: {exc}") from exc
try:
parsed = json.loads(raw or "{}")
except json.JSONDecodeError:
parsed = {"status": "unknown", "output": raw}
return parsed if isinstance(parsed, dict) else {"status": "unknown", "output": raw}
def _get_cached_release_payload(allow_stale: bool = False) -> Optional[dict]:
payload = _version_cache.get("payload")
if payload is None:
return None
if allow_stale or time.monotonic() < float(_version_cache.get("expires_at", 0.0)):
return payload # type: ignore[return-value]
return None
def _update_check_disabled() -> bool:
"""Honor DISABLE_UPDATE_CHECK=true in .env (written by `--offline`)."""
env_file = Path(INSTALL_DIR) / ".env"
disabled = False
try:
for line in _read_utf8(env_file).splitlines():
if line.startswith("DISABLE_UPDATE_CHECK="):
value = parse_env_value(line.split("=", 1)[1]).strip().lower()
disabled = value in {"true", "1", "yes"}
except PermissionError:
# An install owner other than UID 1000 leaves the 0600 .env unreadable
# here; compose passes the value it read instead.
return os.environ.get("DISABLE_UPDATE_CHECK", "").strip().lower() in {"true", "1", "yes"}
except OSError:
return False
return disabled
def _normalize_version(value: Optional[str]) -> str:
"""Normalize a version string for comparison and display.
GitHub release tags are ``vX.Y.Z`` while ``.env``/``.version`` may store
either form, so strip a leading ``v`` (and surrounding whitespace) to keep
``current`` and ``latest`` on the same footing.
"""
if not isinstance(value, str):
return ""
return value.strip().removeprefix("v")
def _version_order(value: str) -> Optional[tuple]:
"""SemVer precedence; unknown builds cannot prove that an update is newer."""
match = re.fullmatch(
r"(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)"
r"(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?"
r"(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?", _normalize_version(value),
)
if not match:
return None
core = tuple(int(match[i]) for i in (1, 2, 3))
prerelease = match[4]
if prerelease is None:
return core + (1, ())
identifiers = prerelease.split(".")
if any(part.isdigit() and len(part) > 1 and part.startswith("0") for part in identifiers):
return None
return core + (0, tuple((0, int(part)) if part.isdigit() else (1, part) for part in identifiers))
def _build_version_result(current: str, payload: Optional[dict], check_status: str = "checked") -> dict:
current = _normalize_version(current)
result = {
"current": current,
"latest": None,
"update_available": False,
"changelog_url": None,
"checked_at": None,
"check_status": check_status if payload else (check_status if check_status in {"checking", "disabled"} else "unavailable"),
}
if not payload:
return result
latest = _normalize_version(payload.get("latest"))
latest_order = _version_order(latest)
if latest_order is None:
result["check_status"] = "unavailable"
return result
result["latest"] = latest
result["changelog_url"] = payload.get("changelog_url")
result["checked_at"] = payload.get("checked_at") or result["checked_at"]
current_order = _version_order(current)
if current_order is None or current == "0.0.0":
result["check_status"] = "current-unknown"
else:
result["update_available"] = check_status == "checked" and latest_order > current_order
return result
def _release_payload(response: httpx.Response) -> dict:
response.raise_for_status()
data = response.json()
if (not isinstance(data, dict) or data.get("draft") or data.get("prerelease")
or _version_order(data.get("tag_name")) is None
or _version_order(data.get("tag_name"))[3] != 1):
raise ValueError("GitHub did not return a stable release")
return {
"latest": _normalize_version(data["tag_name"]),
"changelog_url": data.get("html_url"),
"checked_at": datetime.now(timezone.utc).isoformat(),
}
def _rate_limit_seconds(response: httpx.Response) -> Optional[float]:
"""Seconds GitHub asks this address to wait, or None when it set no limit."""
if response.status_code not in (403, 429):
return None
retry_after = response.headers.get("retry-after", "").strip()
if retry_after.isdigit():
return float(retry_after)
if response.headers.get("x-ratelimit-remaining") == "0":
reset = response.headers.get("x-ratelimit-reset", "").strip()
return max(0.0, int(reset) - time.time()) if reset.isdigit() else 0.0
return 0.0 if response.status_code == 429 else None
def _hold_release_checks(rate_limit_seconds: Optional[float], reason: str) -> None:
global _version_hold
wait = min(max(rate_limit_seconds or 0.0, _VERSION_RETRY_SECONDS), _VERSION_MAX_HOLD_SECONDS)
_version_hold = {"until": time.monotonic() + wait, "rate_limited": rate_limit_seconds is not None}
logger.info("Release check failed (%s); next GitHub request in %d s", reason, wait)
def _release_checks_held(force: bool) -> bool:
"""A manual check retries an ordinary failure, but not a rate limit GitHub would refuse."""
held = time.monotonic() < float(_version_hold["until"]) # type: ignore[arg-type]
return held and (bool(_version_hold["rate_limited"]) or not force)
def _held_check_error() -> str:
if not _version_hold["rate_limited"]:
return "Could not reach GitHub"
minutes = max(1, math.ceil((float(_version_hold["until"]) - time.monotonic()) / 60)) # type: ignore[arg-type]
return f"GitHub's request limit for this network is used up; try again in {minutes} min"
async def _refresh_release_cache() -> Optional[dict]:
global _version_cache, _version_hold
try:
async with httpx.AsyncClient(timeout=5.0) as client:
response = await client.get(
f"{_GITHUB_RELEASES_API}/latest",
headers=_GITHUB_HEADERS,
)
payload = _release_payload(response)
except httpx.HTTPStatusError as exc:
_hold_release_checks(_rate_limit_seconds(exc.response), f"GitHub answered {exc.response.status_code}")
return None
except (httpx.HTTPError, httpx.TimeoutException, json.JSONDecodeError, OSError, ValueError) as exc:
_hold_release_checks(None, type(exc).__name__)
return None
_version_cache = {
"expires_at": time.monotonic() + _VERSION_CACHE_TTL,
"payload": payload,
}
_version_hold = {"until": 0.0, "rate_limited": False}
return payload
def _ensure_release_refresh(force: bool = False) -> Optional[asyncio.Task]:
"""Start one GitHub check, or return None while a failed one holds them."""
global _version_refresh_task
if _version_refresh_task is not None and not _version_refresh_task.done():
return _version_refresh_task
if _release_checks_held(force):
return None
_version_refresh_task = asyncio.create_task(_refresh_release_cache())
return _version_refresh_task
@router.get("/api/version", response_model=VersionInfo, dependencies=[Depends(verify_api_key)])
async def get_version(force: bool = False):
"""Get current ODS version without blocking page load on GitHub."""
result = await _version_result(force)
# While a failed check holds requests, say why there is no confirmed
# release: "try again" would be wrong for up to an hour.
if result["check_status"] in ("unavailable", "stale") and _release_checks_held(force=False):
result["check_error"] = _held_check_error()
return result
async def _version_result(force: bool) -> dict:
current = await asyncio.to_thread(_read_current_version)
if await asyncio.to_thread(_update_check_disabled):
return _build_version_result(current, None, "disabled")
cached = _get_cached_release_payload()
if cached and not force:
return _build_version_result(current, cached)
stale = _get_cached_release_payload(allow_stale=True)
refresh_task = _ensure_release_refresh(force)
if stale and not force:
return _build_version_result(current, stale, "stale")
if refresh_task is None:
return _build_version_result(current, stale, "stale" if stale else "unavailable")
try:
payload = await asyncio.wait_for(asyncio.shield(refresh_task), timeout=6.0 if force else 1.25)
return _build_version_result(current, payload or stale, "checked" if payload else "stale" if stale else "unavailable")
except asyncio.TimeoutError:
logger.debug("Version refresh still in progress; returning local version immediately")
return _build_version_result(current, stale, "stale" if stale else "checking")
@router.get("/api/releases/manifest", dependencies=[Depends(verify_api_key)])
async def get_release_manifest():
"""Get release manifest with version history (non-blocking)."""
try:
async with httpx.AsyncClient(timeout=5.0) as client:
resp = await client.get(
f"{_GITHUB_RELEASES_API}?per_page=5",
headers=_GITHUB_HEADERS,
)
resp.raise_for_status()
releases = resp.json()
if not isinstance(releases, list):
raise httpx.HTTPError(f"unexpected releases response: {type(releases).__name__}")
return {
"releases": [
{"version": r.get("tag_name", "").lstrip("v"), "date": r.get("published_at", ""), "title": r.get("name", ""), "changelog": r.get("body", "")[:500] + "..." if len(r.get("body", "")) > 500 else r.get("body", ""), "url": r.get("html_url", ""), "prerelease": r.get("prerelease", False)}
for r in releases
],
"checked_at": datetime.now(timezone.utc).isoformat()
}
except (httpx.HTTPError, httpx.TimeoutException, json.JSONDecodeError, OSError):
current = await asyncio.to_thread(_read_current_version)
return {
"releases": [{"version": current, "date": datetime.now(timezone.utc).isoformat(), "title": f"ODS {current}", "changelog": "Release information unavailable. Check GitHub directly.", "url": _GITHUB_RELEASES_PAGE, "prerelease": False}],
"checked_at": datetime.now(timezone.utc).isoformat(),
"error": "Could not fetch release information"
}
_UPDATE_ENV_KEYS = {
"ODS_VERSION", "TIER", "LLM_MODEL", "GGUF_FILE",
"CTX_SIZE", "GPU_BACKEND", "N_GPU_LAYERS",
}
@router.get("/api/update/dry-run", dependencies=[Depends(verify_api_key)])
async def get_update_dry_run():
"""Preview what a ods update would change without applying anything.
Returns version comparison, configured image tags, and the .env keys
that the update process reads or writes. No containers are started,
stopped, or re-created.
"""
install_path = Path(INSTALL_DIR)
# ── current version ───────────────────────────────────────────────────────
current = _normalize_version(await asyncio.to_thread(_read_current_version))
env_file = install_path / ".env"
# ── latest version from GitHub ────────────────────────────────────────────
latest: Optional[str] = None
changelog_url: Optional[str] = None
update_available = False
version_check_error: Optional[str] = None
if await asyncio.to_thread(_update_check_disabled):
version_check_error = "Update checks are off (DISABLE_UPDATE_CHECK)"
else:
# A dry run is a manual check that shares the dashboard's cache and hold.
payload = _get_cached_release_payload()
refresh_task = None if payload else _ensure_release_refresh(force=True)
if refresh_task is not None:
payload = await asyncio.shield(refresh_task)
if payload is None:
version_check_error = _held_check_error()
else:
version = _build_version_result(current, payload)
latest = version["latest"]
changelog_url = version["changelog_url"]
update_available = version["update_available"]
if version["check_status"] == "current-unknown":
version_check_error = "Installed version is unknown; automatic version comparison is unavailable."
# ── configured image tags from compose files ──────────────────────────────
images: list[str] = []
for compose_file in sorted(install_path.glob("docker-compose*.yml")):
try:
for line in _read_utf8(compose_file).splitlines():
stripped = line.strip()
if stripped.startswith("image:"):
tag = stripped.split(":", 1)[1].strip()
if tag and tag not in images:
images.append(tag)
except OSError:
pass
# ── .env keys relevant to the update path ────────────────────────────────
env_snapshot: dict[str, str] = {}
env_readable = True
try:
env_lines = _read_utf8(env_file).splitlines() if env_file.exists() else []
except PermissionError:
env_readable = False
env_lines = []
for line in env_lines:
line = line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, _, val = line.partition("=")
if key in _UPDATE_ENV_KEYS:
env_snapshot[key] = val
return {
"dry_run": True,
"current_version": current,
"latest_version": latest,
"update_available": update_available,
"changelog_url": changelog_url,
"images": images,
# An install owner other than UID 1000 leaves the 0600 .env unreadable
# here. Null keys make `ods dry-run` read the file as the owner instead.
"env_keys": env_snapshot if env_readable else None,
"env_keys_error": None if env_readable else "The install's .env is readable only by its owner",
"version_check_error": version_check_error,
}
@router.get("/api/update/status", dependencies=[Depends(verify_api_key)])
async def get_update_status():
"""Return host-agent managed update status."""
return await asyncio.to_thread(_get_update_agent_status)
@router.post("/api/update")
async def trigger_update(action: UpdateAction, api_key: str = Depends(verify_api_key)):
"""Trigger update actions via dashboard."""
if action.action not in _VALID_ACTIONS:
raise HTTPException(status_code=400, detail=f"Unknown action: {action.action}")
if action.action == "check":
return await asyncio.to_thread(_call_update_agent, "check", {}, 35)
if action.action == "backup":
payload = {"backup_id": f"dashboard-{datetime.now().strftime('%Y%m%d-%H%M%S')}"}
return await asyncio.to_thread(_call_update_agent, "backup", payload, 65)
return await asyncio.to_thread(_call_update_agent, "start", {}, 10)