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.

65.3 KB
Raw Blame
"""In-app updater — one "Update Now" button over two very different installs.

Dodaios reaches a machine in one of two supported ways, and each one
updates itself differently:

* **frozen** — the visitor downloaded a native installer from the website
  (``Setup.exe`` / ``.dmg`` / ``.AppImage``). There is no git checkout here. The
  update is the NEXT installer asset on the GitHub Release: downloaded, proven
  against that same release's ``installers-SHA256SUMS.txt`` — which is trusted
  only once the release's Ed25519-signed, version-bound update manifest vouches
  for it (``dodaios/core/update_manifest.py``) — then handed to the platform's
  own upgrade mechanism (``dodaios/core/installer_update.py``).
  ``dodaios.core.frozen.is_frozen()`` is the ONLY probe that selects this path,
  so a dev tree and a managed checkout are structurally unaffected by it.
* **managed** — the one-line installer's git checkout under
  ``~/.dodaios`` (``~/.personal-jarvis`` before the rename). Updates are a
  ``git fetch`` of the published tag plus
  a reinstall; everything below describes that path.

Anything else (a maintainer checkout, a fork, a plain ``pip install``) is
``dev``: ``status`` reports ``managed: false``, the button never renders, and
``apply`` refuses.

The managed path
----------------
An end user installs Dodaios with the one-line installer, which clones
the public flagship repo into ``~/.dodaios`` and runs the app from that
checkout. This module lets the running desktop app offer a one-click "Update
Now" button so the user never has to re-run the installer from a terminal:

* ``GET  /api/update/status`` — compares the running version against the latest
  published GitHub Release and reports whether an update is available (plus its
  release notes). It is **fail-open**: any network or parse error reports "no
  update" rather than erroring, so a flaky connection never breaks the UI.
* ``POST /api/update/apply``  — fetches and pins the exact target revision, then
  writes a pending-update manifest without changing the running checkout. The
  caller hits ``POST /api/settings/restart-app``; after the old process exits,
  the detached relauncher applies the revision and re-runs the full installer
  before it starts the new app.

Safety-critical guard (the single most important thing here):
``git reset --hard`` destroys uncommitted local changes. That is fine for an
end user's managed checkout but catastrophic in a maintainer's dev tree or any
manual clone. So the updater is active **only** on a *managed install*, proven
by BOTH:
  1. a marker file (``.dodaios-managed-install``, or ``.jarvis-managed-install``
     from before the rename) that the installer writes into the checkout root,
     and
  2. the checkout's ``origin`` remote resolving to the official public repo.
If either check fails, ``status`` reports ``managed: false`` (the button never
renders) and ``apply`` refuses with HTTP 403. This makes the dev tree and any
fork structurally immune to the self-update.

Cross-platform: git runs via ``asyncio`` subprocess with
``NO_WINDOW_CREATIONFLAGS`` (AP-1, no console flash under ``pythonw.exe``).
Dependency and desktop files are changed only by the detached relauncher after
the live process has released imported modules. On a headless VPS the fetch
works and the caller's restart step degrades honestly (``restart-app`` returns
503).
"""

from __future__ import annotations

import asyncio
import json
import logging
import os
import platform as platform_module
import re
import shutil
import sys
import time
from collections.abc import Callable
from pathlib import Path
from typing import Any, Literal

from fastapi import APIRouter, HTTPException, Request

from dodaios.core.branding import (
    DEFAULT_OFFICIAL_REPO_SLUG,
    LEGACY_OFFICIAL_REPO_SLUGS,
    MANAGED_INSTALL_MARKER,
    MANAGED_INSTALL_MARKERS,
    OFFICIAL_RELEASES_LATEST_API_URL,
    OFFICIAL_REPO_GIT_URL,
    OFFICIAL_REPO_SLUG,
    UPDATER_USER_AGENT,
)
from dodaios.core.frozen import is_frozen
from dodaios.core.installer_update import (
    CHECKSUMS_ASSET_NAME,
    InstallerUpdateError,
    SignedManifestAssets,
    apply_installer,
    download_and_verify,
    download_workdir,
    installer_asset_names,
    machine_for_update,
    select_asset,
    select_installer_assets,
    sweep_stale_downloads,
    update_blocker,
)
from dodaios.core.process_utils import NO_WINDOW_CREATIONFLAGS
from dodaios.core.update_contract import (
    CONTRACT_PATH,
    DEFAULT_CONTRACT,
    VERSION_PATTERN,
    UpdateContract,
    parse_update_contract,
    read_update_contract,
)
from dodaios.core.update_manifest import (
    UPDATE_MANIFEST_ASSET_NAME,
    UPDATE_MANIFEST_SIGNATURE_ASSET_NAME,
)

log = logging.getLogger(__name__)

router = APIRouter(prefix="/api/update", tags=["update"])

# The ONE official public repo this updater will ever pull from. The managed
# guard verifies the installed checkout's ``origin`` resolves here before any
# ``git reset --hard`` runs, so a dev checkout or a fork can never be self-reset.
_OFFICIAL_REPO_SLUG = OFFICIAL_REPO_SLUG
_LEGACY_OFFICIAL_REPO_SLUGS = LEGACY_OFFICIAL_REPO_SLUGS
_RELEASES_LATEST_API = OFFICIAL_RELEASES_LATEST_API_URL
# Written by install/installer.py into the checkout root. Its presence is one
# half of "this copy is safe to self-update".
_MARKER_NAME = MANAGED_INSTALL_MARKER
# Every name the marker has had; an install made before the rename and updated
# in-app since carries only the earlier one.
_MARKER_NAMES = MANAGED_INSTALL_MARKERS
_PENDING_UPDATE_NAME = ".jarvis-update-pending.json"
_UPDATE_RESULT_NAME = ".jarvis-update-result.json"

_NETWORK_TIMEOUT_S = 6.0
_STATUS_CACHE_TTL_S = 1800.0  # 30 min — don't hit GitHub on every poll.
_STATUS_RETRY_S = 120.0  # after a failed network check, retry sooner than the TTL.
_RELEASE_TAG_RE = re.compile(r"^v?\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?$")

# In-process cache of the last status result. The managed state is stable for a
# process lifetime; the network result is what the TTL protects. The resolved
# managed root is cached alongside so a cache hit (every focus-triggered poll)
# costs only two tiny local file reads — not a git subprocess.
_status_cache: dict[str, Any] | None = None
_status_cache_until: float = 0.0
_status_cache_root: Path | None = None

# Last release metadata that was successfully fetched from GitHub, kept for the
# apply path. The unauthenticated releases API is rate-limited PER IP (60/h) —
# on carrier-grade NAT / DS-Lite connections that budget is shared with other
# households, so the refetch inside ``apply`` can 403 minutes after ``status``
# succeeded. Falling back to the last good answer keeps the one-click update
# working instead of failing with an opaque 502.
_last_good_release: dict[str, Any] | None = None

_REVISION_RE = re.compile(r"^[0-9a-f]{40}$")


# --------------------------------------------------------------------------- #
# Version helpers
# --------------------------------------------------------------------------- #
def _running_version() -> str:
    """The version of the CURRENTLY running process (in-memory module)."""
    try:
        import dodaios

        return str(dodaios.__version__)
    except (ImportError, AttributeError):
        # A source checkout without __version__ is normal; the caller falls
        # back to reading the version files.
        return "unknown"


def _version_sources(contract: UpdateContract) -> tuple[tuple[str, str], ...]:
    """``(path, pattern)`` pairs that can name a tree's version, best first.

    The version file is the one the tree itself names in its update contract,
    so a release that moved its package is still read correctly by the version
    before it. ``pyproject.toml`` never moves and stays the fallback.
    """
    return (
        (contract.version_file, VERSION_PATTERN),
        ("pyproject.toml", r'^version\s*=\s*"([^"]+)"'),
    )


def _version_on_disk(root: Path) -> str | None:
    """Parse the version from the freshly-pulled files on disk.

    After ``apply`` the checkout is new but the imported ``jarvis`` module in
    memory is still the OLD version, so the post-update version must be read
    from disk, not from ``dodaios.__version__``.
    """
    for rel, pattern in _version_sources(read_update_contract(root)):
        try:
            text = root.joinpath(*rel.split("/")).read_text(encoding="utf-8")
        except OSError:
            # Not "file absent" alone — an unreadable version file makes the
            # installed version look older than it is, which shows up as an
            # update that keeps re-offering itself. Worth a line before moving on.
            log.debug("[update] version file %s unreadable; trying the next", rel)
            continue
        m = re.search(pattern, text, re.MULTILINE)
        if m:
            return m.group(1)
    return None


def _naive_version_gt(a: str, b: str) -> bool:
    """Fallback dotted-int compare when ``packaging`` is unavailable."""

    def parts(v: str) -> list[int]:
        out: list[int] = []
        for chunk in v.split("."):
            m = re.match(r"\d+", chunk)
            out.append(int(m.group()) if m else 0)
        return out

    try:
        return parts(a) > parts(b)
    except Exception:  # noqa: BLE001 - unparseable version is never "newer"
        # Fail closed: an update is offered only when we can actually prove the
        # remote version is higher. Never blind-update on a parse failure.
        return False


def _is_newer(latest: str, current: str) -> bool:
    """True iff ``latest`` is a strictly newer version than ``current``.

    Fail-closed on an unknown running version: if we can't tell what we're on,
    we do NOT offer an update (never blind-update).
    """
    if not latest or current in ("", "unknown"):
        return False
    try:
        from packaging.version import Version

        return Version(latest) > Version(current)
    except ImportError:
        # `packaging` is not in the torch-free base install on every host; the
        # dotted-int compare below covers the SemVer tags releases actually use.
        return _naive_version_gt(latest, current)
    except Exception:  # noqa: BLE001 - malformed versions are never newer
        return False


def _versions_equal(left: str, right: str) -> bool:
    """Compare release versions and fail closed on malformed metadata."""
    if not left or not right:
        return False
    try:
        from packaging.version import Version

        return Version(left) == Version(right)
    except ImportError:
        # Same as _is_newer: no `packaging` on a minimal install, so fall back
        # to an exact match on a strict dotted shape.
        dotted = re.compile(r"^\d+(?:\.\d+){2,3}$")
        return bool(dotted.fullmatch(left) and left == right)
    except Exception:  # noqa: BLE001 - malformed versions never compare equal
        return False


# --------------------------------------------------------------------------- #
# Subprocess helpers (git + pip), NO_WINDOW_CREATIONFLAGS + clean teardown
# --------------------------------------------------------------------------- #
async def _terminate(proc: asyncio.subprocess.Process) -> None:
    """Guarantee a dead subprocess: terminate -> wait 50 ms -> kill -> wait."""
    if proc.returncode is not None:
        return
    try:
        proc.terminate()
        try:
            await asyncio.wait_for(proc.wait(), timeout=0.05)
            return
        except (TimeoutError, asyncio.CancelledError):
            # It ignored SIGTERM within the grace period; escalate to kill below.
            pass
        proc.kill()
        await proc.wait()
    except (ProcessLookupError, OSError):
        # The child is already gone — which is exactly what this function is
        # for. Nothing to report.
        pass


# git writes its progress counters to stderr and separates the redraws with a
# carriage return, so "one update" is a CR- or LF-delimited segment, not a line.
_PROGRESS_SEGMENT_RE = re.compile(rb"[\r\n]")


async def _pump(stream: asyncio.StreamReader, on_line: Callable[[str], None] | None) -> bytes:
    """Read ``stream`` to EOF, handing each finished segment to ``on_line``.

    Returns everything that was read, so the caller still gets the complete
    output it would have gotten from ``communicate()``.
    """
    chunks: list[bytes] = []
    pending = b""
    while True:
        chunk = await stream.read(4096)
        if not chunk:
            break
        chunks.append(chunk)
        if on_line is None:
            continue
        segments = _PROGRESS_SEGMENT_RE.split(pending + chunk)
        pending = segments.pop()
        for segment in segments:
            text = segment.decode(errors="replace").strip()
            if text:
                on_line(text)
    return b"".join(chunks)


async def _run(
    cmd: list[str],
    *,
    cwd: Path,
    timeout_s: float,
    on_stderr_line: Callable[[str], None] | None = None,
    env: dict[str, str] | None = None,
) -> tuple[int, str, str]:
    """Run ``cmd`` in ``cwd``. Returns ``(returncode, stdout, stderr)``.

    ``returncode == -1`` signals the process could not run at all (missing
    binary) or timed out — ``stderr`` then carries a human reason. Cleans up the
    child on timeout/cancel so no zombie is left behind.

    ``on_stderr_line`` taps stderr as it arrives instead of only at the end,
    which is the only way to read git's progress counters while the fetch is
    still running. It must not raise; :func:`_git_progress_tap` wraps the one
    caller that uses it.
    """
    try:
        proc = await asyncio.create_subprocess_exec(
            *cmd,
            cwd=str(cwd),
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE,
            creationflags=NO_WINDOW_CREATIONFLAGS,
            env=env,
        )
    except (FileNotFoundError, OSError, NotImplementedError) as exc:
        # Not swallowed: the reason travels back to the caller in stderr and
        # ends up in the update status the user sees.
        return -1, "", f"could not run {cmd[0]}: {exc}"

    async def collect() -> tuple[bytes, bytes]:
        # Both pipes must be drained concurrently: a child that fills one while
        # we read the other deadlocks, which is exactly what a chatty
        # ``git fetch --progress`` would do.
        assert proc.stdout is not None and proc.stderr is not None
        raw_out, raw_err = await asyncio.gather(
            _pump(proc.stdout, None), _pump(proc.stderr, on_stderr_line)
        )
        await proc.wait()
        return raw_out, raw_err

    try:
        try:
            raw_out, raw_err = await asyncio.wait_for(collect(), timeout=timeout_s)
        except TimeoutError:
            # Same contract: the timeout is reported through stderr, and the
            # child is reaped first so no zombie survives the request.
            await _terminate(proc)
            return -1, "", f"{cmd[0]} timed out after {timeout_s:.0f}s"
        except asyncio.CancelledError:
            await _terminate(proc)
            raise
    finally:
        if proc.returncode is None:
            await _terminate(proc)

    out = raw_out.decode(errors="replace").strip()
    err = raw_err.decode(errors="replace").strip()
    return proc.returncode if proc.returncode is not None else -1, out, err


async def _git(
    args: list[str],
    *,
    cwd: Path,
    timeout_s: float = 60.0,
    on_stderr_line: Callable[[str], None] | None = None,
) -> tuple[int, str, str]:
    # An update never downloads the Unreal Verse's LFS binaries (``unreal/**``,
    # docs/verse/unreal/architecture.md 8.4); the Verse arrives as its own package.
    env = {**os.environ, "GIT_LFS_SKIP_SMUDGE": "1"}
    return await _run(
        ["git", *args], cwd=cwd, timeout_s=timeout_s, on_stderr_line=on_stderr_line, env=env
    )


async def _git_output(args: list[str], *, cwd: Path, timeout_s: float = 15.0) -> str | None:
    rc, out, _err = await _git(args, cwd=cwd, timeout_s=timeout_s)
    return out if rc == 0 else None


# --------------------------------------------------------------------------- #
# Managed-install guard
# --------------------------------------------------------------------------- #
def _repo_root() -> Path | None:
    """The checkout root, derived unambiguously from the running package."""
    try:
        import dodaios

        # .../repo/dodaios/__init__.py -> .../repo
        return Path(dodaios.__file__).resolve().parent.parent
    except Exception:  # noqa: BLE001 - an unlocatable checkout is not updatable
        # Returning None makes the managed-install guard fail closed: no root,
        # no update. Silent on purpose — this runs on every status poll, and a
        # namespace package without __file__ is a legitimate deployment shape.
        return None


def _normalize_remote(url: str) -> str:
    """Reduce a git remote URL to a comparable ``.../owner/name`` tail.

    Handles https (``https://github.com/Owner/Name.git``), ssh
    (``[email protected]:Owner/Name.git``), and local file paths on either slash
    style — so the comparison is robust across platforms and remote forms.
    """
    tail = url.strip()
    if tail.endswith(".git"):
        tail = tail[:-4]
    tail = tail.replace("\\", "/").replace(":", "/").rstrip("/")
    return tail


def _remote_matches_slug(url: str, slug: str) -> bool:
    """True when ``url`` resolves to exactly ``slug``, not a look-alike name."""
    norm = _normalize_remote(url).lower()
    needle = slug.lower()
    return norm == needle or norm.endswith("/" + needle)


def _accepted_official_slugs() -> tuple[str, ...]:
    """Current slug, plus the pre-rename address while that slug is still ours.

    An environment override points the updater at a different repository on
    purpose, so the legacy address is accepted only for the stock slug.
    """
    if _OFFICIAL_REPO_SLUG != DEFAULT_OFFICIAL_REPO_SLUG:
        return (_OFFICIAL_REPO_SLUG,)
    return (_OFFICIAL_REPO_SLUG, *_LEGACY_OFFICIAL_REPO_SLUGS)


def _remote_is_official(url: str) -> bool:
    """True only if ``url`` resolves to an accepted official ``owner/name``.

    Must MATCH the last two path segments, not merely contain the slug — so a
    look-alike fork (``.../Dodaios/DodaiosEvil``) is rejected. A checkout whose
    origin is still ``PersonalJarvis/PersonalJarvis`` stays official; applying
    an update rewrites that remote to the current URL.
    """
    return any(_remote_matches_slug(url, slug) for slug in _accepted_official_slugs())


async def _retarget_legacy_origin(root: Path) -> None:
    """Point a pre-rename origin at the current official URL.

    Git still fetches through GitHub's redirect, but the next release of this
    app identifies the checkout by the stored remote. Rewriting it here is what
    keeps one-click updates working after the legacy slug is no longer enough.
    """
    origin = await _git_output(["remote", "get-url", "origin"], cwd=root)
    if origin is None or _remote_matches_slug(origin, _OFFICIAL_REPO_SLUG):
        return
    if not _remote_is_official(origin):
        return
    rc, _out, err = await _git(
        ["remote", "set-url", "origin", OFFICIAL_REPO_GIT_URL],
        cwd=root,
        timeout_s=30.0,
    )
    if rc != 0:
        raise HTTPException(
            status_code=500,
            detail=(
                "could not retarget origin to the current repository: "
                f"{err[:300] or 'unknown error'}"
            ),
        )


def _marker_path(root: Path) -> Path | None:
    """The installer's marker in ``root`` under any of its names, current name first."""
    for name in _MARKER_NAMES:
        candidate = root / name
        if candidate.exists():
            return candidate
    return None


async def _resolve_managed_repo() -> Path | None:
    """Return the checkout root IFF this is a managed, self-updatable install.

    Requires BOTH the installer marker AND an ``origin`` that resolves to the
    official public repo. Any doubt returns ``None`` (fail-closed).
    """
    root = _repo_root()
    if root is None or _marker_path(root) is None:
        return None
    if not (root / ".git").exists():
        return None
    origin = await _git_output(["remote", "get-url", "origin"], cwd=root)
    if origin is None or not _remote_is_official(origin):
        return None
    return root


# --------------------------------------------------------------------------- #
# GitHub release check (fail-open)
# --------------------------------------------------------------------------- #
async def _fetch_latest_release() -> dict[str, Any] | None:
    """GET the latest GitHub Release. Fail-open: any error returns ``None``.

    A successful answer is also remembered in ``_last_good_release`` so the
    apply path can survive a transient API failure (rate limit, blip) that
    happens between the status check and the button click.
    """
    global _last_good_release
    try:
        import httpx

        async def _stay_on_github_api(response: httpx.Response) -> None:
            # A renamed repository answers 301. Follow that hop, and refuse any
            # hop that leaves GitHub's API host: the JSON decides which
            # installer the button will download.
            if not response.has_redirect_location:
                return
            target = response.url.join(response.headers.get("location") or "")
            if target.scheme != "https" or target.host != "api.github.com":
                raise httpx.HTTPError(f"refusing a release-check redirect to {target}")

        async with httpx.AsyncClient(
            timeout=_NETWORK_TIMEOUT_S,
            follow_redirects=True,
            event_hooks={"response": [_stay_on_github_api]},
        ) as client:
            resp = await client.get(
                _RELEASES_LATEST_API,
                headers={
                    "Accept": "application/vnd.github+json",
                    "User-Agent": UPDATER_USER_AGENT,
                },
            )
        if resp.status_code != 200:
            log.debug("update check: releases/latest HTTP %s", resp.status_code)
            return None
        data = resp.json()
        release_tag = str(data.get("tag_name") or "").strip()
        if not _RELEASE_TAG_RE.fullmatch(release_tag):
            return None
        raw_assets = data.get("assets")
        release = {
            "version": release_tag.lstrip("vV"),
            "tag": release_tag,
            "notes": (data.get("body") or "").strip(),
            "published_at": data.get("published_at"),
            "release_url": data.get("html_url"),
            # Only the frozen path reads this; the managed path resolves its
            # target from the git tag and ignores the attachments entirely.
            "assets": raw_assets if isinstance(raw_assets, list) else [],
        }
        _last_good_release = release
        return release
    except Exception as exc:  # noqa: BLE001 — fail-open on any network/parse error
        log.debug("update check: latest-release fetch failed: %s", exc)
        return None


# --------------------------------------------------------------------------- #
# Managed install profile + deferred transaction manifest
# --------------------------------------------------------------------------- #
InstallProfile = Literal["full", "headless"]


def _managed_install_profile(root: Path) -> InstallProfile:
    """Resolve the installer profile, including pre-profile marker fallback.

    New installers persist the decision in the managed marker. Older markers
    predate that field, so desktop sessions retain the advertised ``[full]``
    profile while a display-less Linux host keeps the torch-free base floor.
    """

    marker = _marker_path(root) or root / _MARKER_NAME
    try:
        payload = json.loads(marker.read_text(encoding="utf-8"))
    except (OSError, UnicodeDecodeError, json.JSONDecodeError, TypeError):
        # A source checkout has no install marker; the defaults below apply.
        payload = {}
    if isinstance(payload, dict):
        profile = payload.get("profile")
        if profile in {"full", "headless"}:
            return profile
        desktop = payload.get("desktop")
        if isinstance(desktop, bool):
            return "full" if desktop else "headless"

    if sys.platform in {"win32", "darwin"}:
        return "full"
    if sys.platform.startswith("linux") and (
        os.environ.get("DISPLAY") or os.environ.get("WAYLAND_DISPLAY")
    ):
        return "full"
    return "headless"


def _write_pending_update(
    root: Path,
    *,
    previous_revision: str,
    target_revision: str,
    profile: InstallProfile,
) -> None:
    """Atomically stage the post-exit update transaction for the relauncher."""

    path = root / _PENDING_UPDATE_NAME
    temp = path.with_suffix(path.suffix + ".tmp")
    payload = {
        "schema": 1,
        "previous_revision": previous_revision,
        "target_revision": target_revision,
        "profile": profile,
        "created_at": int(time.time()),
    }
    temp.write_text(json.dumps(payload, sort_keys=True) + "\n", encoding="utf-8")
    os.replace(temp, path)
    try:
        (root / _UPDATE_RESULT_NAME).unlink(missing_ok=True)
    except OSError:
        # A result file we failed to clear is read again on the next boot and
        # reports the PREVIOUS run's outcome as if it were this one's.
        log.warning(
            "[update] could not clear %s; a stale result may be reported next boot",
            _UPDATE_RESULT_NAME,
        )


def _read_pending_manifest(root: Path) -> dict[str, Any] | None:
    """Read + validate a staged-but-not-yet-installed update transaction.

    Mirrors the relauncher's strict validation (same file, same schema) without
    importing its private helper. Any doubt returns ``None``.
    """
    try:
        payload = json.loads((root / _PENDING_UPDATE_NAME).read_text(encoding="utf-8"))
    except (OSError, UnicodeDecodeError, json.JSONDecodeError, TypeError):
        # No pending update is the normal case, and a half-written marker is
        # indistinguishable from it here; either way there is nothing to resume.
        return None
    if not isinstance(payload, dict) or payload.get("schema") != 1:
        return None
    target = payload.get("target_revision")
    previous = payload.get("previous_revision")
    profile = payload.get("profile")
    if not isinstance(target, str) or not _REVISION_RE.fullmatch(target):
        return None
    if not isinstance(previous, str) or not _REVISION_RE.fullmatch(previous):
        return None
    if profile not in {"full", "headless"}:
        return None
    return payload


def _read_update_result(root: Path) -> dict[str, Any] | None:
    """The relauncher's verdict on the LAST finalized update, if any.

    ``ok: false`` means the target install failed after the restart and the
    checkout was reset back — without surfacing this, a rolled-back update is
    indistinguishable from "the button silently did nothing".
    """
    try:
        payload = json.loads((root / _UPDATE_RESULT_NAME).read_text(encoding="utf-8"))
    except (OSError, UnicodeDecodeError, json.JSONDecodeError, TypeError):
        # Absent on every boot that did not just apply an update — the common
        # path, not a failure.
        return None
    if not isinstance(payload, dict) or not isinstance(payload.get("ok"), bool):
        return None
    return {
        "ok": payload["ok"],
        "rolled_back": bool(payload.get("rolled_back", False)),
        "completed_at": payload.get("completed_at"),
    }


async def _fresh_staged_manifest(root: Path) -> tuple[dict[str, Any], str] | None:
    """The staged manifest + its target version, IFF still worth finishing.

    "Worth finishing" means the target is strictly newer than the RUNNING
    version — the same fail-closed rule the apply route enforces. A manifest
    left behind by a crashed finalize (the checkout already reset to the
    target) or by an already-installed update must never be re-offered, in
    the status overlay any more than in the apply fallback.
    """
    manifest = _read_pending_manifest(root)
    if manifest is None:
        return None
    version = await _version_at_revision(root, str(manifest["target_revision"]))
    if version is None or not _is_newer(version, _running_version()):
        return None
    return manifest, version


async def _pending_update_overlay(root: Path) -> dict[str, Any]:
    """Live (never cached) status fields: staged transaction + last verdict."""
    overlay: dict[str, Any] = {"pending_update": None, "last_result": None}
    staged = await _fresh_staged_manifest(root)
    if staged is not None:
        manifest, version = staged
        overlay["pending_update"] = {
            "version": version,
            "target_revision": manifest["target_revision"],
        }
    overlay["last_result"] = _read_update_result(root)
    return overlay


async def _version_at_revision(root: Path, revision: str) -> str | None:
    """Read a target version without checking that target out over the live app.

    Where the version is written is asked of the TARGET revision's own update
    contract, not assumed from this version's layout. A revision without a
    contract is read as laid out like this version; ``pyproject.toml`` names
    its version wherever the package is.
    """

    raw_contract = await _git_output(["show", f"{revision}:{CONTRACT_PATH}"], cwd=root)
    contract = DEFAULT_CONTRACT if raw_contract is None else parse_update_contract(raw_contract)
    for rel, pattern in _version_sources(contract):
        raw = await _git_output(["show", f"{revision}:{rel}"], cwd=root)
        if raw is None:
            continue
        match = re.search(pattern, raw, re.MULTILINE)
        if match:
            return match.group(1)
    return None


async def _staged_update_response(root: Path) -> dict[str, object] | None:
    """Re-offer an already-staged transaction when GitHub is unreachable.

    Only accepted when the staged target is still strictly newer than the
    running version (the manifest was written by a fully validated apply, so
    the revision itself is trusted). Returns ``None`` when nothing usable is
    staged — the caller then reports the network failure honestly.
    """
    staged = await _fresh_staged_manifest(root)
    if staged is None:
        return None
    manifest, version = staged
    profile = manifest["profile"]
    return {
        "ok": True,
        "prepared": True,
        "restart_required": True,
        "kind": INSTALL_KIND_MANAGED,
        "version": version,
        "release_tag": f"v{version}",
        "install_profile": profile,
        "deps_refreshed": False,
        "deps_pending": True,
        "deps_warning": None,
        "ui_bundle_pending": True,
        "desktop_integration_ok": None,
        "desktop_integration_pending": profile == "full",
        "desktop_integration_warning": None,
    }


# --------------------------------------------------------------------------- #
# Frozen install (native installer) — download, verify, hand over
# --------------------------------------------------------------------------- #
INSTALL_KIND_FROZEN = "frozen"
INSTALL_KIND_MANAGED = "managed"
INSTALL_KIND_DEV = "dev"


# --------------------------------------------------------------------------- #
# Live progress of the one in-flight update
# --------------------------------------------------------------------------- #
# ``apply`` is a single long request (a few hundred MB on a frozen install), so
# the UI cannot learn anything from its return value until it is over. This
# tracker is what ``GET /api/update/progress`` publishes while that request is
# still running, turning "Updating…" into "Updating 70%".
#
# The percentages are a PHASE MODEL, never a timer. Each phase owns a fixed
# window, and inside a window the number moves only on evidence we actually
# have: bytes off the socket on a frozen install, git's own object counters on
# a managed one. A phase with no measurable inside (writing the transaction
# manifest) simply lands on its window's end when it completes. That makes a
# jump possible, but never a lie — which is the right trade for a bar the user
# is deciding "is this stuck?" from.
PHASE_IDLE = "idle"
PHASE_RESOLVING = "resolving"
PHASE_DOWNLOADING = "downloading"
PHASE_VERIFYING = "verifying"
PHASE_INSTALLING = "installing"
PHASE_READY = "ready"
PHASE_FAILED = "failed"

# ``{install kind: {phase: (window start %, window end %)}}``. The two kinds do
# very different work, so they weight the phases differently: a frozen install
# is dominated by the download, a managed one by the git fetch.
_PHASE_SPANS: dict[str, dict[str, tuple[int, int]]] = {
    INSTALL_KIND_FROZEN: {
        PHASE_RESOLVING: (0, 6),
        PHASE_DOWNLOADING: (6, 86),
        PHASE_VERIFYING: (86, 94),
        PHASE_INSTALLING: (94, 100),
    },
    INSTALL_KIND_MANAGED: {
        PHASE_RESOLVING: (0, 10),
        PHASE_DOWNLOADING: (10, 78),
        PHASE_VERIFYING: (78, 92),
        PHASE_INSTALLING: (92, 100),
    },
}


class _UpdateProgress:
    """Mutable snapshot of the update currently being applied.

    One instance per process: only one update may run at a time (the apply
    route rejects a second with 409), so a single slot is the whole model.
    Every method is total — a tracker that raised would take the update down
    with it, and progress is cosmetic.
    """

    def __init__(self) -> None:
        self.kind: str = INSTALL_KIND_DEV
        self.phase: str = PHASE_IDLE
        self.percent: int = 0
        self.detail: str | None = None
        self.version: str | None = None
        self.error: str | None = None
        self.restart_required: bool = False
        self.started_at: float | None = None
        self.updated_at: float | None = None

    # -- lifecycle ---------------------------------------------------------- #
    def begin(self, kind: str, *, version: str | None = None) -> None:
        """Start a run. The ONLY point at which the percentage may go down."""
        self.kind = kind
        self.phase = PHASE_RESOLVING
        self.percent = 0
        self.detail = None
        self.version = version
        self.error = None
        self.restart_required = False
        self.started_at = self.updated_at = time.time()

    def enter(self, phase: str, *, detail: str | None = None) -> None:
        """Move to ``phase``, landing on the start of its window."""
        self.advance(phase, 0.0, detail=detail)

    def advance(self, phase: str, fraction: float, *, detail: str | None = None) -> None:
        """Report being ``fraction`` (0..1) through ``phase``.

        Monotonic on purpose: git reports "Receiving objects" and "Resolving
        deltas" as two separate 0→100 % counters, and a bar that rewinds reads
        as a restart to anyone watching it.
        """
        low, high = _PHASE_SPANS.get(self.kind, {}).get(phase, (self.percent, self.percent))
        clamped = 0.0 if fraction < 0 else 1.0 if fraction > 1 else fraction
        self.phase = phase
        self.percent = max(self.percent, int(low + (high - low) * clamped))
        if detail is not None:
            self.detail = detail
        self.updated_at = time.time()

    def finish(self, *, version: str | None, restart_required: bool) -> None:
        """The server's work is done; what remains is the restart."""
        self.phase = PHASE_READY
        self.percent = 100
        self.version = version or self.version
        self.restart_required = restart_required
        self.error = None
        self.updated_at = time.time()

    def fail(self, message: str) -> None:
        """Stop, keeping the percentage where it died so the UI can say where."""
        self.phase = PHASE_FAILED
        self.error = message[:300]
        self.updated_at = time.time()

    # -- readers ------------------------------------------------------------ #
    def snapshot(self) -> dict[str, object]:
        """The shape ``GET /api/update/progress`` returns."""
        return {
            "active": self.phase not in (PHASE_IDLE, PHASE_READY, PHASE_FAILED),
            "phase": self.phase,
            "percent": self.percent,
            "detail": self.detail,
            "version": self.version,
            "kind": self.kind,
            "error": self.error,
            "restart_required": self.restart_required,
            "started_at": self.started_at,
            "updated_at": self.updated_at,
        }


_progress = _UpdateProgress()


def _human_bytes(count: int) -> str:
    """``134217728`` -> ``"128.0 MB"``.

    Short on purpose: this string sits inside a top-bar pill, where fitting
    matters far more than precision.
    """
    step = 1024.0
    size = float(count)
    for unit in ("B", "KB", "MB", "GB"):
        if size < step or unit == "GB":
            return f"{size:.0f} {unit}" if unit in ("B", "KB") else f"{size:.1f} {unit}"
        size /= step
    return f"{size:.1f} GB"


# Serialises applies. A second click while the first download runs would fight
# over the same temp directory and scramble the percentage; the route answers
# 409 instead of queueing, because the user is already getting what they asked
# for.
_apply_lock = asyncio.Lock()

# git's own progress lines on stderr: "Receiving objects:  43% (430/1000)".
# Receiving is where the bytes are, so it owns most of the fetch window and
# delta resolution finishes it off.
_GIT_PROGRESS_RE = re.compile(r"(Receiving objects|Resolving deltas):\s+(\d+)%")
_GIT_PROGRESS_WEIGHTS = {"Receiving objects": (0.0, 0.85), "Resolving deltas": (0.85, 1.0)}


def _on_git_progress(line: str) -> None:
    """Translate one git stderr line into a fetch-window fraction."""
    match = _GIT_PROGRESS_RE.search(line)
    if match is None:
        return
    low, high = _GIT_PROGRESS_WEIGHTS[match.group(1)]
    share = int(match.group(2)) / 100.0
    _progress.advance(PHASE_DOWNLOADING, low + (high - low) * share)


def _frozen_asset_names() -> tuple[str, ...]:
    """The installer assets this machine can install, best first; empty if none."""
    return installer_asset_names(sys.platform, machine_for_update())


# Old update downloads are reclaimed once per process, on the first status
# check — not on every poll, and never on the boot path itself (AP-26).
_downloads_swept = False


async def _sweep_old_downloads_once() -> None:
    global _downloads_swept
    if _downloads_swept:
        return
    _downloads_swept = True
    try:
        await asyncio.to_thread(sweep_stale_downloads)
    except Exception:  # noqa: BLE001 - housekeeping never fails the fail-open status check
        log.warning("[update] could not reclaim old update downloads", exc_info=True)


def _frozen_release_assets(
    release: dict[str, Any], asset_names: tuple[str, ...]
) -> tuple[tuple[Any, ...], Any | None]:
    """``(installer assets, checksum manifest asset)`` from a release payload.

    The installers are this machine's file under every accepted name the
    release carries, best first — one entry for any release published so far.
    """
    assets = release.get("assets") or []
    if not isinstance(assets, list):
        return (), None
    return (
        select_installer_assets(assets, asset_names),
        select_asset(assets, CHECKSUMS_ASSET_NAME),
    )


def _signed_manifest_assets(release: dict[str, Any]) -> tuple[Any | None, Any | None]:
    """``(update manifest, its signature)`` from a release payload; ``None`` when absent.

    Without both, a frozen install refuses the release: an update whose
    checksums the release key did not sign is not installed (issue #810).
    """
    assets = release.get("assets") or []
    if not isinstance(assets, list):
        return None, None
    return (
        select_asset(assets, UPDATE_MANIFEST_ASSET_NAME),
        select_asset(assets, UPDATE_MANIFEST_SIGNATURE_ASSET_NAME),
    )


async def _frozen_status(current: str) -> dict[str, object]:
    """Status for a natively installed app. Fail-OPEN, exactly like the managed one.

    ``managed`` stays the field the UI gates the button on — it means "this
    install can update itself", which a frozen install can. ``kind`` says HOW.

    An update is offered only when the release actually carries BOTH this
    platform's installer and the checksum manifest, because without either one
    ``apply`` would have to refuse: offering a button that cannot work is worse
    than staying quiet.
    """
    result: dict[str, object] = {
        "managed": True,
        "kind": INSTALL_KIND_FROZEN,
        "current": current,
        "latest": None,
        "update_available": False,
        "notes": None,
        "published_at": None,
        "asset": None,
        # Frozen installs have no staged git transaction and no relauncher
        # verdict; the fields stay present so the UI reads one shape.
        "pending_update": None,
        "last_result": None,
        # Why THIS install cannot replace itself (run from a disk image, a
        # read-only folder, outside its AppImage), or None. The update is still
        # announced — the user should learn it exists — and apply refuses with
        # this sentence before downloading anything.
        "blocked_reason": None,
        # The same verdict as a stable code the UI translates.
        "blocked_code": None,
    }
    await _sweep_old_downloads_once()

    asset_names = _frozen_asset_names()
    if not asset_names:
        # No installer is published for this OS/CPU pair (a Windows ARM64 box,
        # a Linux aarch64 box). Never offer what cannot be installed.
        log.info(
            "[update] no installer asset exists for %s/%s",
            sys.platform,
            platform_module.machine(),
        )
        result["managed"] = False
        result["unsupported_platform"] = True
        return result

    latest = await _fetch_latest_release()
    if latest is None:
        result["check_failed"] = True
        return result

    version = str(latest.get("version") or "")
    installers, checksums = _frozen_release_assets(latest, asset_names)
    asset = installers[0] if installers else None
    result["latest"] = version or None
    result["published_at"] = latest.get("published_at")
    result["release_url"] = latest.get("release_url")

    if asset is None or checksums is None:
        # A release without installers is a code-only release (or one whose
        # installer job failed). Report honestly instead of half-offering.
        log.info(
            "[update] release %s carries no %s / %s — not offering a frozen update",
            version or "?",
            " / ".join(asset_names),
            CHECKSUMS_ASSET_NAME,
        )
        return result
    signed_manifest, signature = _signed_manifest_assets(latest)
    if signed_manifest is None or signature is None:
        # apply would refuse it: the checksums are not signed by the release key.
        log.info(
            "[update] release %s carries no signed %s — not offering a frozen update",
            version or "?",
            UPDATE_MANIFEST_ASSET_NAME,
        )
        return result

    result["asset"] = asset.as_dict()
    if _is_newer(version, current):
        result["update_available"] = True
        result["notes"] = latest.get("notes")
        blocker = update_blocker()
        if blocker is not None:
            result["blocked_reason"] = blocker.message
            result["blocked_code"] = blocker.code
    return result


def _running_missions(request: Request | None) -> list[str]:
    """IDs of the missions an app quit would kill right now (empty on doubt)."""
    if request is None:
        return []
    kontrollierer = getattr(request.app.state, "kontrollierer", None)
    list_running = getattr(kontrollierer, "running_mission_ids", None)
    if not callable(list_running):
        return []
    try:
        return [str(mid) for mid in list_running()]
    except Exception:  # noqa: BLE001 — a wedged guard must not block the update
        log.warning("[update] could not list running missions", exc_info=True)
        return []


def _refuse_if_missions_run(request: Request | None, *, force: bool) -> None:
    """Raise the restart route's 409 when quitting now would kill missions."""
    if force:
        return
    running = _running_missions(request)
    if running:
        raise HTTPException(
            status_code=409,
            detail={
                "error": "missions_running",
                "missions": [{"id": mid, "title": ""} for mid in running],
            },
        )


def _desktop_can_quit(request: Request | None) -> bool:
    """True when a desktop window exists that ``_quit_for_update`` can close."""
    if request is None:
        return False
    desktop = getattr(request.app.state, "desktop_app", None)
    return callable(getattr(desktop, "request_quit", None))


def _quit_for_update(request: Request | None) -> bool:
    """Close the desktop app so the handed-over installer can take its place.

    The native handover only starts the new version once THIS process is gone
    (the single-instance lock), so a successful handover must be followed by a
    quit. Returns False on a host without a desktop window (``jarvis serve``):
    the user then restarts by hand, and the response says so.
    """
    if request is None:
        return False
    desktop = getattr(request.app.state, "desktop_app", None)
    quit_fn = getattr(desktop, "request_quit", None)
    if not callable(quit_fn):
        return False
    try:
        return bool(quit_fn())
    except Exception:  # noqa: BLE001 — reported to the caller as "restart by hand"
        log.warning("[update] could not quit for the update", exc_info=True)
        return False


async def _apply_frozen(
    request: Request | None = None, *, force: bool = False
) -> dict[str, object]:
    """Download, verify and install the native installer for this machine.

    Fail-CLOSED at every step: an unresolvable release, a missing asset, a
    missing or mismatching SHA-256 all raise before anything is executed. The
    running app keeps working on the old version in every failure case.

    A successful handover ends this app (the installer replaces it), so the
    same mission guard as ``/api/settings/restart-app`` runs FIRST — before a
    single byte is downloaded — and refuses with 409 unless ``force`` is set.
    """
    _progress.begin(INSTALL_KIND_FROZEN)
    _refuse_if_missions_run(request, force=force)
    current = _running_version()
    asset_names = _frozen_asset_names()
    if not asset_names:
        raise HTTPException(
            status_code=501,
            detail=(
                f"no Dodaios installer is published for {sys.platform}/{platform_module.machine()}"
            ),
        )

    latest = await _fetch_latest_release()
    if latest is None and _last_good_release is not None:
        # Same reasoning as the managed path: the unauthenticated releases API
        # is rate-limited per IP, so a blip between status and click must not
        # brick the button. The SHA-256 check below still proves the bytes.
        log.info("[update] live release check failed — using the last good answer")
        latest = _last_good_release
    if latest is None:
        raise HTTPException(
            status_code=502,
            detail=(
                "could not reach GitHub to resolve the latest published release "
                "(offline or rate-limited) — try again in a few minutes"
            ),
        )

    release_tag = str(latest.get("tag") or "")
    release_version = str(latest.get("version") or "")
    if not _RELEASE_TAG_RE.fullmatch(release_tag):
        raise HTTPException(status_code=502, detail="latest release tag is invalid")
    if not _is_newer(release_version, current):
        raise HTTPException(status_code=409, detail="no newer published release exists")

    installers, checksums = _frozen_release_assets(latest, asset_names)
    if not installers:
        raise HTTPException(
            status_code=502,
            detail=f"release {release_tag} does not contain {' or '.join(asset_names)}",
        )
    asset = installers[0]
    if checksums is None:
        raise HTTPException(
            status_code=502,
            detail=(
                f"release {release_tag} has no {CHECKSUMS_ASSET_NAME} — refusing to "
                "install an installer that cannot be verified"
            ),
        )

    signed_manifest, signature = _signed_manifest_assets(latest)
    if signed_manifest is None or signature is None:
        raise HTTPException(
            status_code=502,
            detail=(
                f"release {release_tag} has no signed {UPDATE_MANIFEST_ASSET_NAME} — "
                "refusing to install an update the release key did not sign"
            ),
        )
    signed = SignedManifestAssets(
        manifest=signed_manifest, signature=signature, version=release_version
    )

    blocker = update_blocker()
    if blocker is not None:
        # Asked before the download: otherwise the user waits for several
        # hundred MB only to learn the app cannot replace itself where it is.
        raise HTTPException(status_code=409, detail=blocker.message)

    _progress.version = release_version

    def on_bytes(written: int, total: int | None) -> None:
        """One download tick. Without a total there is no honest fraction."""
        if not total:
            _progress.advance(PHASE_DOWNLOADING, 0.0, detail=_human_bytes(written))
            return
        _progress.advance(
            PHASE_DOWNLOADING,
            written / total,
            detail=f"{_human_bytes(written)} / {_human_bytes(total)}",
        )

    workdir = download_workdir()
    try:
        _progress.enter(PHASE_DOWNLOADING, detail=asset.name)
        if len(installers) > 1:
            # The release carries this installer under more than one accepted
            # name; the checksum manifest decides which of them is downloaded.
            installer = await download_and_verify(
                asset,
                checksums,
                dest_dir=workdir,
                on_progress=on_bytes,
                alternatives=installers[1:],
                signed=signed,
            )
            asset = next((a for a in installers if a.name == installer.name), asset)
        else:
            installer = await download_and_verify(
                asset, checksums, dest_dir=workdir, on_progress=on_bytes, signed=signed
            )
        # download_and_verify hashes the file after the last byte lands, so by
        # the time it returns the verify phase is already over — the window is
        # closed here rather than announced, to keep the bar truthful.
        _progress.advance(PHASE_VERIFYING, 1.0, detail=None)
        # The download can take minutes; a mission started meanwhile must get
        # the same protection, so the guard runs again right before the point
        # of no return.
        _refuse_if_missions_run(request, force=force)
        _progress.enter(PHASE_INSTALLING)
        # Without a window to close (``jarvis serve``, a browser-only AppImage)
        # this process keeps running, so a relaunch waiter would only start a
        # second instance that bounces off the lock: the user restarts instead.
        relaunch = _desktop_can_quit(request)
        # hdiutil, a directory swap and a detached spawn all block; keep the
        # event loop (and therefore the UI this answer travels back over) free.
        handover = await asyncio.to_thread(lambda: apply_installer(installer, relaunch=relaunch))
    except HTTPException:
        shutil.rmtree(workdir, ignore_errors=True)
        raise
    except InstallerUpdateError as exc:
        shutil.rmtree(workdir, ignore_errors=True)
        _progress.fail(str(exc))
        log.warning("[update] frozen update refused: %s", exc)
        raise HTTPException(status_code=502, detail=str(exc)) from exc
    except Exception as exc:  # noqa: BLE001 — the user must see WHY it stopped
        shutil.rmtree(workdir, ignore_errors=True)
        _progress.fail(f"the update could not be installed: {exc}")
        log.exception("[update] frozen update failed unexpectedly")
        raise HTTPException(
            status_code=500, detail=f"the update could not be installed: {exc}"
        ) from exc

    # The native installer restarts the app itself, so this run is complete.
    _progress.finish(version=release_version, restart_required=False)
    # The new version starts once this process is gone — so go.
    quitting = _quit_for_update(request)
    if not quitting:
        handover = f"{handover} — restart Dodaios to finish the update"

    # On Windows the installer that replaces this app is running from the
    # download right now, so it stays (deleting it would kill the update). On
    # macOS and Linux the swap is already done, and nothing reclaims a
    # hundreds-of-MB file in the temp directory on its own.
    if sys.platform != "win32":
        shutil.rmtree(workdir, ignore_errors=True)
    log.info("[update] %s installed from %s (%s)", release_tag, asset.name, workdir)

    global _status_cache, _status_cache_until, _status_cache_root
    _status_cache, _status_cache_until, _status_cache_root = None, 0.0, None

    return {
        "ok": True,
        "prepared": True,
        # The handover relaunches the app itself once this process exits
        # (the installer's /RELAUNCH=1 run on Windows, a wait-then-exec helper
        # on macOS and Linux), and this route already scheduled that exit. A
        # caller must NOT restart on top: on a frozen build that would race
        # the installer for the program files.
        "restart_required": False,
        "quitting": quitting,
        "kind": INSTALL_KIND_FROZEN,
        "version": release_version,
        "release_tag": release_tag,
        "install_profile": "frozen",
        "asset": asset.as_dict(),
        "handover": handover,
        "deps_refreshed": False,
        "deps_pending": False,
        "deps_warning": None,
        "ui_bundle_pending": False,
        "desktop_integration_ok": None,
        # The native installer owns Start Menu / Applications / .desktop
        # registration, so there is nothing for the app to write.
        "desktop_integration_pending": False,
        "desktop_integration_warning": None,
    }


# --------------------------------------------------------------------------- #
# Routes
# --------------------------------------------------------------------------- #
@router.get("/status")
async def update_status(force: bool = False) -> dict[str, object]:
    """Report whether a newer published version is available.

    ``force=true`` bypasses the in-process cache (a manual "check now").
    """
    global _status_cache, _status_cache_until, _status_cache_root
    now = time.monotonic()
    if not force and _status_cache is not None and now < _status_cache_until:
        # The network part (and the resolved managed root, one git subprocess)
        # is cached; the staged-transaction fields are cheap local file reads
        # and must always be live (an apply invalidates the cache, but a
        # relauncher result appears while the cache is warm). A frozen install
        # has no such local state, so its cached answer is complete.
        if _status_cache.get("managed") and _status_cache_root is not None:
            return {
                **_status_cache,
                **(await _pending_update_overlay(_status_cache_root)),
            }
        return _status_cache

    current = _running_version()

    if is_frozen():
        # A native install has no checkout to resolve and must never enter the
        # git path below (there is no `origin`, no marker, and no `git`).
        frozen_result = await _frozen_status(current)
        _status_cache_root = None
        _status_cache = frozen_result
        _status_cache_until = now + (
            _STATUS_RETRY_S if frozen_result.get("check_failed") else _STATUS_CACHE_TTL_S
        )
        return frozen_result

    root = await _resolve_managed_repo()
    _status_cache_root = root

    if root is None:
        result: dict[str, object] = {
            "managed": False,
            "kind": INSTALL_KIND_DEV,
            "current": current,
            "latest": None,
            "update_available": False,
            "notes": None,
            "published_at": None,
        }
        _status_cache, _status_cache_until = result, now + _STATUS_CACHE_TTL_S
        return result

    latest = await _fetch_latest_release()
    if latest is None:
        # Fail-open: we are managed but couldn't reach GitHub. Offer no update,
        # and retry sooner than the full TTL.
        result = {
            "managed": True,
            "kind": INSTALL_KIND_MANAGED,
            "current": current,
            "latest": None,
            "update_available": False,
            "notes": None,
            "published_at": None,
            "check_failed": True,
        }
        _status_cache, _status_cache_until = result, now + _STATUS_RETRY_S
        return {**result, **(await _pending_update_overlay(root))}

    available = _is_newer(str(latest["version"]), current)
    result = {
        "managed": True,
        "kind": INSTALL_KIND_MANAGED,
        "current": current,
        "latest": latest["version"],
        "update_available": available,
        "notes": latest["notes"] if available else None,
        "published_at": latest["published_at"],
        "release_url": latest.get("release_url"),
    }
    _status_cache, _status_cache_until = result, now + _STATUS_CACHE_TTL_S
    return {**result, **(await _pending_update_overlay(root))}


@router.get("/progress")
async def update_progress() -> dict[str, object]:
    """The live percentage of the update currently being applied.

    ``POST /api/update/apply`` is one long request — several hundred megabytes
    on a frozen install — so its return value tells the UI nothing until it is
    over. This is the side channel the button polls meanwhile, which is what
    turns "Updating…" into "Updating 70%".

    Always answers, whether or not an update is running: ``phase: "idle"`` is
    the resting state, ``"ready"`` means the server's work is done and only the
    restart remains, and ``"failed"`` carries the reason in ``error``. Reading
    it costs nothing but a dict copy, so a 300 ms poll is fine.
    """
    return _progress.snapshot()


@router.post("/apply", openapi_extra={"x-jarvis-dangerous": True})
async def update_apply(request: Request, force: bool = False) -> dict[str, object]:
    """Prepare the latest version and report progress while doing it.

    Dispatches on the install kind: a FROZEN install downloads and hands over
    the platform installer (``_apply_frozen``), a MANAGED checkout stages a git
    transaction the relauncher finishes (``_apply_managed``).

    Two things wrap both branches. The lock rejects a second concurrent apply
    with 409 rather than queueing it — two downloads would fight over the temp
    directory and scramble the shared progress state, and the user is already
    getting what they clicked for. The handler also guarantees that ANY failure
    lands in the progress tracker: a run that ends without ``fail`` leaves the
    button reading "Updating 42%" forever.
    """
    if _apply_lock.locked():
        raise HTTPException(status_code=409, detail="an update is already being applied")
    async with _apply_lock:
        try:
            if is_frozen():
                return await _apply_frozen(request, force=force)
            return await _apply_managed()
        except HTTPException as exc:
            detail = exc.detail
            if isinstance(detail, dict) and detail.get("error") == "missions_running":
                detail = "missions are running — confirm to update anyway"
            _progress.fail(str(detail))
            raise
        except Exception as exc:  # noqa: BLE001 — re-raised; this only records it
            _progress.fail(f"{type(exc).__name__}: {exc}")
            raise


async def managed_checkout_root() -> Path | None:
    """The managed checkout this process runs from, or ``None``.

    The public face of the managed-install guard for ``jarvis update``
    (``dodaios/cli/app_update.py``), which finalizes the update itself.
    """
    return await _resolve_managed_repo()


async def stage_managed_update() -> dict[str, object]:
    """Stage the newest release for a managed checkout from the terminal.

    The twin of ``POST /api/update/apply`` for ``jarvis update`` while the app
    is closed: same guard, same pinned tag, same pending-update manifest. The
    caller then runs ``relauncher.finalize_pending_update`` instead of a
    restart. Raises ``HTTPException`` exactly like the route does.
    """
    async with _apply_lock:
        return await _apply_managed()


async def _apply_managed() -> dict[str, object]:
    """Stage the latest published tag for a managed checkout. Does NOT restart.

    The live checkout remains untouched. The caller then invokes
    ``POST /api/settings/restart-app``; its detached relauncher applies the
    pinned revision and completes installation after this process exits.
    """
    global _status_cache, _status_cache_until, _status_cache_root
    _progress.begin(INSTALL_KIND_MANAGED)

    root = await _resolve_managed_repo()
    if root is None:
        raise HTTPException(
            status_code=403,
            detail="not a managed install — in-app update is disabled here",
        )

    latest = await _fetch_latest_release()
    if latest is None and _last_good_release is not None:
        # The status check knew the target minutes ago; a transient API failure
        # (shared-IP rate limit, blip) must not brick the one-click update. The
        # tag fetch + version-at-revision equality check below still verify the
        # actual bytes, so a stale answer can never install the wrong thing.
        log.info("update apply: live release check failed — using last good answer")
        latest = _last_good_release
    if latest is None:
        staged = await _staged_update_response(root)
        if staged is not None:
            # GitHub is unreachable but a validated transaction is already on
            # disk from an earlier click — restarting can finish it offline.
            # Nothing was downloaded, so the run is complete at once.
            _progress.finish(version=str(staged.get("version") or ""), restart_required=True)
            return staged
        raise HTTPException(
            status_code=502,
            detail=(
                "could not reach GitHub to resolve the latest published release "
                "(offline or rate-limited) — try again in a few minutes"
            ),
        )
    release_version = str(latest.get("version") or "")
    release_tag = str(latest.get("tag") or "")
    if not _RELEASE_TAG_RE.fullmatch(release_tag):
        raise HTTPException(status_code=502, detail="latest release tag is invalid")
    if not _is_newer(release_version, _running_version()):
        raise HTTPException(status_code=409, detail="no newer published release exists")

    _progress.version = release_version
    _progress.advance(PHASE_RESOLVING, 1.0)

    previous_revision = await _git_output(["rev-parse", "HEAD"], cwd=root)
    if not previous_revision:
        raise HTTPException(
            status_code=500,
            detail="could not identify the currently installed revision",
        )

    await _retarget_legacy_origin(root)

    # Fetch the published tag, never the moving main branch. The update button
    # promises a specific GitHub Release; applying an unreleased main commit
    # would make the displayed version and installed bytes disagree.
    #
    # ``--progress`` forces git to emit its object counters even though stderr
    # is a pipe rather than a terminal; without it the download phase would
    # have no measurable inside at all.
    _progress.enter(PHASE_DOWNLOADING, detail=release_tag)
    rc, _out, err = await _git(
        ["fetch", "--progress", "--depth", "1", "origin", f"refs/tags/{release_tag}"],
        cwd=root,
        timeout_s=120.0,
        on_stderr_line=_on_git_progress,
    )
    if rc != 0:
        raise HTTPException(
            status_code=502, detail=f"git fetch failed: {err[:300] or 'unknown error'}"
        )

    _progress.enter(PHASE_VERIFYING)
    target_revision = await _git_output(["rev-parse", "FETCH_HEAD^{commit}"], cwd=root)
    if not target_revision:
        raise HTTPException(
            status_code=500,
            detail="could not identify the fetched update revision",
        )

    new_version = await _version_at_revision(root, target_revision)
    if new_version is None or not _versions_equal(new_version, release_version):
        raise HTTPException(
            status_code=502,
            detail="published tag version does not match its release metadata",
        )

    _progress.advance(PHASE_VERIFYING, 1.0)
    _progress.enter(PHASE_INSTALLING)
    profile = _managed_install_profile(root)
    try:
        _write_pending_update(
            root,
            previous_revision=previous_revision,
            target_revision=target_revision,
            profile=profile,
        )
    except OSError as exc:
        log.warning("Could not stage the pending update manifest: %s", type(exc).__name__)
        raise HTTPException(
            status_code=500,
            detail="could not stage the update transaction",
        ) from exc

    # The next status poll must see the staged transaction immediately.
    _status_cache, _status_cache_until, _status_cache_root = None, 0.0, None
    # Everything this process can do is done; the relauncher does the rest,
    # which is why the caller must now restart.
    _progress.finish(version=new_version, restart_required=True)

    return {
        "ok": True,
        "prepared": True,
        "restart_required": True,
        "kind": INSTALL_KIND_MANAGED,
        "version": new_version,
        "release_tag": release_tag,
        "install_profile": profile,
        "deps_refreshed": False,
        "deps_pending": True,
        "deps_warning": None,
        "ui_bundle_pending": True,
        "desktop_integration_ok": None,
        "desktop_integration_pending": profile == "full",
        "desktop_integration_warning": None,
    }