Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
fix(update): make the native-installer update survive a slow quit, stalls and unfixable installs (#312)
Ruben Lütkee33be6b91cfc
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 1504c396e7..f3c06631dd 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -595,28 +595,54 @@ jobs:# app differently (Inno handover, .app swap, AppImage swap, git transaction# + restart helper), and the POSIX-only tests run the real relaunch waiter,# so this lane is never narrowed to one runner the way PR test jobs are.+ #+ # Python: 3.11 is the floor, 3.12 is what the native installers freeze.+ # macos-15-intel adds the x86_64 Mac (the Intel .dmg and its Rosetta probe).+ # The real-OS handover tests build a real .dmg on macOS and, in the 3.12+ # Windows job, compile the real Inno script and upgrade a RUNNING install+ # with it (JARVIS_INSTALLER_E2E) — the only proof the installer actually+ # waits for the old app and brings the new one back.updater:- name: updater (${{ matrix.os }})+ name: updater (${{ matrix.os }}, Python ${{ matrix.python }})needs: detectif: needs.detect.outputs.updater == 'true'strategy:fail-fast: falsematrix:os: [ubuntu-latest, windows-latest, macos-latest]+ python: ["3.11", "3.12"]+ include:+ - {os: macos-15-intel, python: "3.12"}runs-on: ${{ matrix.os }}- timeout-minutes: 25+ timeout-minutes: 30steps:- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2with:persist-credentials: false- uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0with:- python-version: "3.11"+ python-version: ${{ matrix.python }}- name: Portable base + focused runnershell: bashrun: |python -m pip install -e .python -m pip install pytest pytest-asyncio+ - name: Inno Setup for the real installer upgrade+ if: runner.os == 'Windows' && matrix.python == '3.12'+ shell: pwsh+ run: |+ $candidates = @(+ "${env:ProgramFiles(x86)}\Inno Setup 6\ISCC.exe",+ "$env:ProgramFiles\Inno Setup 6\ISCC.exe",+ "$env:LOCALAPPDATA\Programs\Inno Setup 6\ISCC.exe"+ )+ $found = $candidates | Where-Object { Test-Path $_ } | Select-Object -First 1+ if ($found) {+ Write-Host "Inno Setup already present: $found"+ } else {+ choco install innosetup --no-progress -y+ }+ "JARVIS_INSTALLER_E2E=1" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8- name: Updater suite (status, verify, handover, restart, rollback)shell: bashrun: |@@ -624,6 +650,7 @@ jobs:git config --global user.name cipython -m pytest -q -p no:cacheprovider -rs \tests/unit/core/test_installer_update.py \+ tests/unit/core/test_installer_update_http.py \tests/unit/install/test_installer_update_contract.py \tests/unit/ui/test_relauncher.py \tests/unit/ui/test_relauncher_frozen.py \@@ -635,7 +662,8 @@ jobs:tests/unit/web/test_update_progress.py \tests/unit/web/test_update_progress_route.py \tests/unit/web/test_update_subprocess_stream.py \- tests/integration/test_update_apply_real_git.py+ tests/integration/test_update_apply_real_git.py \+ tests/integration/test_update_handover_real_os.pyrealtime-slim:name: realtime on python:3.11-slim (no PortAudio)diff --git a/docs/os-parity.md b/docs/os-parity.mdindex 830ca5854e..963b6dfda3 100644--- a/docs/os-parity.md+++ b/docs/os-parity.md@@ -832,10 +832,34 @@ build re-enters its own executable with `--relauncher` (a frozen build has no`python -m`), and relaunches through `$APPIMAGE` on Linux and `open -n` onmacOS. A frozen apply runs the mission guard before downloading. A host withno desktop window (`jarvis serve`) keeps running and is told to restart by-hand. Covered by `tests/unit/core/test_installer_update.py` (the waiter is-executed for real on macOS and Linux), `tests/unit/ui/test_relauncher_frozen.py`-and `tests/unit/ui/web/test_update_routes_frozen.py`, run on all three OSes by-the CI `updater` lane. Not yet run against a real installed build on any OS.+hand.++Hardening (2026-10-02): Windows also passes `/WAITPID=<pid>` and+`/LOG=<user logs>/update-installer.log`; the installer's `InitializeSetup`+waits up to 120 s for that process before touching a file, because a silent+install that meets the still-running app answers "Abort" and the app never+comes back (reproduced by the real-installer test). Every handover child gets+`PYINSTALLER_RESET_ENVIRONMENT=1`, and on Linux the user's `LD_LIBRARY_PATH`+(from `LD_LIBRARY_PATH_ORIG`) instead of the old AppImage mount. A preflight+(`update_blocker`) refuses BEFORE downloading when the install cannot replace+itself: a macOS app run from App Translocation, a disk image or an unwritable+folder, or a Linux process not started from a writable AppImage; the status+route reports it as `blocked_reason`. An Intel build under Rosetta updates to+the arm64 DMG. Downloads only follow https redirects, are size-capped while+streaming and time out as a whole after 30 minutes; Windows installer+downloads left in the temp folder are reclaimed on the next run.++Covered by `tests/unit/core/test_installer_update.py` (the waiter is executed+for real on macOS and Linux), `tests/unit/core/test_installer_update_http.py`+(the real httpx client against a scripted GitHub),+`tests/unit/ui/test_relauncher_frozen.py`,+`tests/unit/ui/web/test_update_routes_frozen.py` and+`tests/integration/test_update_handover_real_os.py`: a real AppImage swap and+relaunch on macOS/Linux, a real `.dmg` on macOS, and on Windows the real Inno+script compiled twice and upgraded over a running install+(`JARVIS_INSTALLER_E2E=1`). The CI `updater` lane runs all of it on Linux,+Windows, Apple Silicon and Intel macOS. Still not run against a real released+build: the first release with this code needs one live update per OS.| # | Impact | Area | Gap | Evidence | Behavior off-Windows ||---|---|---|---|---|---|diff --git a/jarvis/core/installer_update.py b/jarvis/core/installer_update.pyindex 0f97e0f8ec..a6856cca1d 100644--- a/jarvis/core/installer_update.py+++ b/jarvis/core/installer_update.py@@ -40,6 +40,7 @@ import shutilimport subprocessimport sysimport tempfile+import timefrom collections.abc import Callable, Iterable, Sequencefrom dataclasses import dataclassfrom pathlib import Path@@ -56,15 +57,20 @@ __all__ = ["AssetFetcher","CommandRunner","DownloadProgress",+ "HttpxAssetFetcher","InstallerAsset","InstallerUpdateError","SubprocessCommandRunner","apply_installer","download_and_verify",+ "handover_env","installer_asset_name",+ "machine_for_update","parse_sha256sums","select_asset","select_installer_asset",+ "sweep_stale_downloads",+ "update_blocker",]# The checksum manifest published alongside the installers. Its lines are plain@@ -158,6 +164,56 @@ def installer_asset_name(platform_name: str, machine: str) -> str | None:return None+def _running_under_rosetta() -> bool:+ """True when this process is an x86_64 build translated by Rosetta 2.++ ``sysctl.proc_translated`` is 1 for a translated process, 0 for a native+ one, and absent on an Intel Mac (the call then fails, which reads as+ "not translated"). Read through ``sysctlbyname`` so the status poll spawns+ no process.+ """+ if sys.platform != "darwin":+ return False+ try:+ import ctypes++ libc = ctypes.CDLL("/usr/lib/libSystem.B.dylib")+ value = ctypes.c_int(0)+ size = ctypes.c_size_t(ctypes.sizeof(value))+ rc = libc.sysctlbyname(+ b"sysctl.proc_translated", ctypes.byref(value), ctypes.byref(size), None, 0+ )+ except (OSError, AttributeError) as exc:+ log.debug("[update] could not ask whether Rosetta translates this process: %s", exc)+ return False+ return rc == 0 and value.value == 1+++def machine_for_update(+ platform_name: str | None = None,+ machine: str | None = None,+ *,+ translated: Callable[[], bool] = _running_under_rosetta,+) -> str:+ """The CPU whose installer this machine should receive.++ Normally ``platform.machine()``. The exception is an Intel build running on+ Apple Silicon: Rosetta makes it report ``x86_64``, so it would keep+ updating to the Intel DMG forever and never leave emulation. Asking the+ kernel whether this process is translated moves it to the native arm64+ build on its next update instead.+ """+ active_platform = sys.platform if platform_name is None else platform_name+ active_machine = platform_module.machine() if machine is None else machine+ if (+ active_platform == "darwin"+ and active_machine.strip().lower() in {"x86_64", "amd64"}+ and translated()+ ):+ return "arm64"+ return active_machine++def select_asset(assets: Iterable[Any], name: str) -> InstallerAsset | None:"""Find one asset by exact name in a GitHub Release ``assets`` array.@@ -193,9 +249,10 @@ def select_installer_asset(machine: str | None = None,) -> InstallerAsset | None:"""The installer asset for the running machine, or ``None``."""+ active_platform = sys.platform if platform_name is None else platform_namename = installer_asset_name(- sys.platform if platform_name is None else platform_name,- platform_module.machine() if machine is None else machine,+ active_platform,+ machine_for_update(active_platform) if machine is None else machine,)if name is None:return None@@ -282,23 +339,43 @@ class HttpxAssetFetcher:``httpx`` is imported lazily: the base install must boot on a host where theupdater never runs, and this module sits on an import path the desktop apptouches at startup (AP-26).++ GitHub answers an asset URL with a redirect to its download CDN, so+ redirects are followed — but every hop, the first request included, must+ stay on ``https``. A hop to plain ``http`` is refused before it is sent.++ ``transport`` exists for tests (an ``httpx.MockTransport``); production+ leaves it ``None`` and gets httpx's default network transport."""- async def get_text(self, url: str, *, max_bytes: int) -> str:+ def __init__(self, *, transport: Any | None = None) -> None:+ self._transport = transport++ def _client(self) -> Any:import httpx- async with httpx.AsyncClient(+ return httpx.AsyncClient(timeout=httpx.Timeout(READ_TIMEOUT_S, connect=CONNECT_TIMEOUT_S),follow_redirects=True,- ) as client:- response = await client.get(url, headers=self._headers())- response.raise_for_status()- body = response.content- if len(body) > max_bytes:- raise InstallerUpdateError(- f"checksum manifest is larger than {max_bytes} bytes — refusing it"- )- return body.decode("utf-8", errors="replace")+ transport=self._transport,+ event_hooks={"request": [_require_https]},+ )++ async def get_text(self, url: str, *, max_bytes: int) -> str:+ body = bytearray()+ async with self._client() as client:+ async with client.stream("GET", url, headers=self._headers()) as response:+ response.raise_for_status()+ # Streamed with the cap applied per chunk: reading the whole+ # body first would let a hostile endpoint fill memory before+ # the size check ever ran.+ async for chunk in response.aiter_bytes():+ body.extend(chunk)+ if len(body) > max_bytes:+ raise InstallerUpdateError(+ f"checksum manifest is larger than {max_bytes} bytes — refusing it"+ )+ return bytes(body).decode("utf-8", errors="replace")async def download(self,@@ -308,13 +385,8 @@ class HttpxAssetFetcher:max_bytes: int,on_progress: DownloadProgress | None = None,) -> int:- import httpx-written = 0- async with httpx.AsyncClient(- timeout=httpx.Timeout(READ_TIMEOUT_S, connect=CONNECT_TIMEOUT_S),- follow_redirects=True,- ) as client:+ async with self._client() as client:async with client.stream("GET", url, headers=self._headers()) as response:response.raise_for_status()# A GitHub asset redirect always answers with a Content-Length,@@ -338,6 +410,18 @@ class HttpxAssetFetcher:return {"User-Agent": UPDATER_USER_AGENT, "Accept": "application/octet-stream"}+async def _require_https(request: Any) -> None:+ """httpx request hook: refuse any hop that is not ``https``.++ Runs for the first request and for every redirect httpx follows, so a+ redirect chain that ends on plain ``http`` never sends that request.+ """+ if request.url.scheme != "https":+ raise InstallerUpdateError(+ f"refusing a non-https download hop to {request.url.host or 'an unknown host'}"+ )++async def download_and_verify(asset: InstallerAsset,checksums: InstallerAsset,@@ -346,6 +430,7 @@ async def download_and_verify(fetcher: AssetFetcher | None = None,max_bytes: int = MAX_INSTALLER_BYTES,on_progress: DownloadProgress | None = None,+ download_timeout_s: float = DOWNLOAD_TIMEOUT_S,) -> Path:"""Download ``asset`` and prove it against ``checksums``. Fail closed.@@ -357,6 +442,11 @@ async def download_and_verify(``total`` falls back to the release metadata's ``asset.size`` when the serverstates no Content-Length, and stays ``None`` when neither source knows it.+ ``download_timeout_s`` bounds the WHOLE transfer, not one read: httpx's+ read timeout restarts with every chunk, so a server trickling one byte a+ minute would otherwise hold the update — and the apply lock with it —+ forever, leaving the button stuck at the same percentage until a restart.+Returns the path of the verified file inside ``dest_dir``. Raises:class:`InstallerUpdateError` on every failure, leaving nothing executablebehind.@@ -372,9 +462,16 @@ async def download_and_verify()try:- manifest_text = await active.get_text(checksums.url, max_bytes=MAX_CHECKSUMS_BYTES)+ manifest_text = await asyncio.wait_for(+ active.get_text(checksums.url, max_bytes=MAX_CHECKSUMS_BYTES),+ timeout=READ_TIMEOUT_S,+ )except InstallerUpdateError:raise+ except TimeoutError as exc:+ raise InstallerUpdateError(+ f"{CHECKSUMS_ASSET_NAME} did not arrive within {READ_TIMEOUT_S:.0f}s"+ ) from excexcept Exception as exc: # noqa: BLE001 — every transport error is the same refusalraise InstallerUpdateError(f"could not download {CHECKSUMS_ASSET_NAME}: {exc}") from exc@@ -388,10 +485,19 @@ async def download_and_verify(target = _prepare_target(dest_dir, asset.name)try:- written = await active.download(asset.url, target, max_bytes=max_bytes, on_progress=tick)+ written = await asyncio.wait_for(+ active.download(asset.url, target, max_bytes=max_bytes, on_progress=tick),+ timeout=download_timeout_s,+ )except InstallerUpdateError:_discard(target)raise+ except TimeoutError as exc:+ _discard(target)+ raise InstallerUpdateError(+ f"{asset.name} did not finish downloading within "+ f"{download_timeout_s / 60:.0f} minutes — the connection is too slow or stalled"+ ) from excexcept Exception as exc: # noqa: BLE001 — same refusal for any transport error_discard(target)raise InstallerUpdateError(f"could not download {asset.name}: {exc}") from exc@@ -453,6 +559,40 @@ class CommandRunner(Protocol):"""Start ``command`` and return immediately, outliving this process."""+def handover_env(+ base: dict[str, str] | None = None, *, frozen: bool | None = None+) -> dict[str, str]:+ """Environment for every process the handover starts.++ The installer, ``hdiutil`` and the relaunch waiter are spawned by a+ PyInstaller build, and they — or the new version they start — inherit its+ environment. Two parts of that environment belong to the OLD process only:++ * PyInstaller's own bookkeeping (``_PYI_*``). The new version's bootloader+ would read it as "I am a child of that build" — on Windows the install+ path is identical, so the match is exact. ``PYINSTALLER_RESET_ENVIRONMENT``+ (PyInstaller >= 6.10) tells it to start as a fresh top-level process.+ * ``LD_LIBRARY_PATH`` on Linux, which the bootloader pointed into the old+ AppImage's private mount. PyInstaller keeps the user's value in+ ``LD_LIBRARY_PATH_ORIG``; that is what a system program must see, and+ the old mount is gone by the time the new AppImage starts anyway.++ Outside a frozen build nothing was rewritten, so only the reset flag is+ added and the library path is left exactly as the user set it.+ """+ env = dict(os.environ if base is None else base)+ env["PYINSTALLER_RESET_ENVIRONMENT"] = "1"+ if not (is_frozen() if frozen is None else frozen):+ return env+ for var in ("LD_LIBRARY_PATH", "LIBPATH"):+ original = env.pop(f"{var}_ORIG", None)+ if original is not None:+ env[var] = original+ else:+ env.pop(var, None)+ return env++class SubprocessCommandRunner:"""Default :class:`CommandRunner`. Every spawn is console-free (AP-1)."""@@ -466,6 +606,8 @@ class SubprocessCommandRunner:errors="replace",timeout=timeout_s,check=False,+ stdin=subprocess.DEVNULL,+ env=handover_env(),creationflags=NO_WINDOW_CREATIONFLAGS,)except FileNotFoundError as exc: # returned to the caller, not swallowed@@ -480,6 +622,7 @@ class SubprocessCommandRunner:"stdout": subprocess.DEVNULL,"stderr": subprocess.DEVNULL,"close_fds": True,+ "env": handover_env(),}if sys.platform == "win32":detached = getattr(subprocess, "DETACHED_PROCESS", 0x00000008)@@ -503,6 +646,7 @@ def apply_installer(appimage_path: Path | None = None,relaunch: bool = True,wait_for_pid: int | None = None,+ installer_log: Path | None = None,) -> str:"""Install the verified ``installer`` and hand control to the new version.@@ -524,7 +668,9 @@ def apply_installer(raise InstallerUpdateError(f"{installer} does not exist")if active_platform == "win32":- return _handover_windows(installer, runner=active_runner)+ return _handover_windows(+ installer, runner=active_runner, wait_for_pid=old_pid, log_path=installer_log+ )if active_platform == "darwin":return _handover_macos(installer,@@ -548,16 +694,46 @@ def apply_installer(# once the files are in place (see the RelaunchRequested check in# packaging/windows/PersonalJarvis.iss).WINDOWS_RELAUNCH_PARAM = "/RELAUNCH=1"+# The Inno Setup parameter naming the process the installer must outlive (see+# InitializeSetup in packaging/windows/PersonalJarvis.iss).+WINDOWS_WAIT_PID_PARAM = "/WAITPID"+# Where the silent installer writes its log. A silent upgrade that fails shows+# nothing at all, so this file is the only trace of what went wrong.+WINDOWS_INSTALLER_LOG_NAME = "update-installer.log"+++def _windows_installer_log() -> Path | None:+ """The installer log path in the user's log directory, or ``None``."""+ try:+ from jarvis.core.paths import user_logs_dir+ logs = user_logs_dir()+ logs.mkdir(parents=True, exist_ok=True)+ except OSError as exc:+ # The update still works without a log; it is only harder to debug.+ log.warning("[update] no log directory for the installer: %s", exc)+ return None+ return logs / WINDOWS_INSTALLER_LOG_NAME-def _handover_windows(installer: Path, *, runner: CommandRunner) -> str:++def _handover_windows(+ installer: Path,+ *,+ runner: CommandRunner,+ wait_for_pid: int,+ log_path: Path | None = None,+) -> str:"""Start the Inno Setup wizard silently and let it replace the running app.- The app quits right after this returns; ``/CLOSEAPPLICATIONS`` lets- Restart Manager close anything still holding a file (the detached terminal- host, a slow shutdown). ``/SUPPRESSMSGBOXES`` keeps a silent upgrade from- stopping on a dialog nobody asked for, and ``/NORESTART`` guarantees the- machine is never rebooted behind the user's back.+ The app quits right after this returns. ``/WAITPID`` makes the installer+ wait until this process has actually exited before it touches a file: a+ shutdown that takes longer than Setup's start-up would otherwise meet+ files still in use, and a silent install answers that with Abort — the+ app gone, nothing installed, nothing relaunched. ``/CLOSEAPPLICATIONS``+ stays as the fallback for anything else holding a file.+ ``/SUPPRESSMSGBOXES`` keeps a silent upgrade from stopping on a dialog+ nobody asked for, and ``/NORESTART`` guarantees the machine is never+ rebooted behind the user's back.The app comes back through ``/RELAUNCH=1``, which the installer's own``[Run]`` entry honours. ``/RESTARTAPPLICATIONS`` is deliberately absent: it@@ -571,15 +747,46 @@ def _handover_windows(installer: Path, *, runner: CommandRunner) -> str:"/CLOSEAPPLICATIONS","/NORESTART",WINDOWS_RELAUNCH_PARAM,+ f"{WINDOWS_WAIT_PID_PARAM}={wait_for_pid}",]+ log_file = _windows_installer_log() if log_path is None else log_path+ if log_file is not None:+ command.append(f"/LOG={log_file}")try:runner.spawn_detached(command)except OSError as exc:raise InstallerUpdateError(f"could not start {installer.name}: {exc}") from exc- log.info("[update] handed over to %s", installer.name)+ log.info("[update] handed over to %s (log: %s)", installer.name, log_file)return "the Windows installer is running; Personal Jarvis restarts by itself"+# Absolute, so the relaunch never depends on the PATH a Finder-started app+# inherits — the same binary the restart helper uses.+MACOS_OPEN = "/usr/bin/open"+++def _is_mounted(path: Path) -> bool:+ """Whether a volume is mounted at ``path`` (a seam for the tests)."""+ return os.path.ismount(path)+++def _can_write(folder: Path) -> bool:+ """Whether this process may create and rename entries in ``folder``."""+ return os.access(folder, os.W_OK)+++def _remove_empty_mountpoint(mountpoint: Path) -> None:+ """Delete the temporary mountpoint directory — only if it is empty.++ ``rmdir`` instead of ``rmtree``: when a detach failed the volume is still+ mounted there, and walking into it to "clean up" is never what we want.+ """+ try:+ mountpoint.rmdir()+ except OSError as exc:+ log.warning("[update] left %s in place: %s", mountpoint, exc)++def _handover_macos(dmg: Path,*,@@ -626,7 +833,10 @@ def _handover_macos(_replace_directory(source, app_path)finally:- if attached:+ # ``attached`` misses one case: hdiutil mounted the image and then+ # reported failure (or timed out after mounting). The mountpoint is the+ # proof either way, so a mounted one is always released.+ if attached or _is_mounted(mountpoint):rc, _out, err = runner.run(["hdiutil", "detach", str(mountpoint), "-force"], timeout_s=120.0)@@ -634,12 +844,12 @@ def _handover_macos(# Not fatal — the swap already happened. Say it so a stuck mount# in a support log has an explanation.log.warning("[update] could not detach %s: %s", mountpoint, err.strip())- shutil.rmtree(mountpoint, ignore_errors=True)+ _remove_empty_mountpoint(mountpoint)if relaunch:try:runner.spawn_detached(- relaunch_after_exit_command(wait_for_pid, ["open", "-n", str(app_path)])+ relaunch_after_exit_command(wait_for_pid, [MACOS_OPEN, "-n", str(app_path)]))except OSError as exc:raise InstallerUpdateError(@@ -682,9 +892,7 @@ def _handover_linux(if relaunch:try:- runner.spawn_detached(- relaunch_after_exit_command(wait_for_pid, [str(appimage_path)])- )+ runner.spawn_detached(relaunch_after_exit_command(wait_for_pid, [str(appimage_path)]))except OSError as exc:raise InstallerUpdateError(f"the update was installed but could not be relaunched: {exc}"@@ -785,4 +993,105 @@ def supported_here() -> bool:"""True when this process is frozen AND an installer exists for its platform."""if not is_frozen():return False- return installer_asset_name(sys.platform, platform_module.machine()) is not None+ return installer_asset_name(sys.platform, machine_for_update()) is not None+++def update_blocker(+ platform_name: str | None = None,+ *,+ app_path: Path | None = None,+ appimage_path: Path | None = None,+) -> str | None:+ """Why the handover would fail on THIS install, or ``None`` when it can run.++ Asked before a single byte is downloaded: every case below otherwise+ surfaces only after a several-hundred-MB download, as a refusal the user+ cannot act on. Each answer is a user-facing sentence that says what to do.++ Windows has no such case — the per-user installer replaces its own+ directory — so it always answers ``None``.+ """+ active_platform = sys.platform if platform_name is None else platform_name+ if active_platform == "darwin":+ app = app_path if app_path is not None else _running_macos_app()+ if app is None:+ return "Personal Jarvis could not find its own app bundle, so it cannot replace it."+ if "/AppTranslocation/" in app.as_posix():+ # Gatekeeper runs a quarantined app that was never moved from a+ # read-only, randomised copy; nothing there can be replaced.+ return (+ "macOS is running Personal Jarvis from a temporary copy. Move it to "+ "the Applications folder, open it from there, then update."+ )+ if not _can_write(app.parent):+ return (+ f"Personal Jarvis cannot write to {app.parent} (a disk image or a "+ "protected folder). Move it to the Applications folder, then update."+ )+ return None+ if active_platform.startswith("linux"):+ appimage = appimage_path if appimage_path is not None else _running_appimage()+ if appimage is None:+ return (+ "This copy of Personal Jarvis was not started from its AppImage file, "+ "so there is no file to update. Download the new AppImage instead."+ )+ if not _can_write(appimage.parent):+ return (+ f"Personal Jarvis cannot write to {appimage.parent}. Move the AppImage "+ "to a folder you own (for example ~/Applications), then update."+ )+ return None+ return None+++# A download directory younger than this may belong to an update still in+# flight in another instance (the whole download may take DOWNLOAD_TIMEOUT_S).+STALE_DOWNLOAD_AGE_S = 6 * 3600.0+_DOWNLOAD_DIR_PREFIX = "jarvis-update-"+++def download_workdir() -> Path:+ """A fresh private directory for one update's download."""+ return Path(tempfile.mkdtemp(prefix=_DOWNLOAD_DIR_PREFIX))+++def sweep_stale_downloads(+ temp_root: Path | None = None,+ *,+ max_age_s: float = STALE_DOWNLOAD_AGE_S,+ now: float | None = None,+) -> int:+ """Delete download directories earlier updates left behind; return how many.++ On Windows the installer runs FROM its download, so that directory cannot+ be removed while the update happens — and nothing removes it later, which+ left one installer of several hundred MB in the temp folder per update.+ The next run of the app reclaims them. Never raises: this is housekeeping.+ """+ root = Path(tempfile.gettempdir()) if temp_root is None else temp_root+ current = time.time() if now is None else now+ removed = 0+ try:+ candidates = [p for p in root.glob(f"{_DOWNLOAD_DIR_PREFIX}*") if p.is_dir()]+ except OSError as exc:+ log.debug("[update] could not list %s for old downloads: %s", root, exc)+ return 0+ for folder in candidates:+ try:+ newest = max(+ [folder.stat().st_mtime, *(entry.stat().st_mtime for entry in folder.iterdir())]+ )+ except OSError as exc:+ log.debug("[update] skipping %s: %s", folder, exc)+ continue+ if current - newest < max_age_s:+ continue+ shutil.rmtree(folder, ignore_errors=True)+ if folder.exists():+ log.debug("[update] %s is still in use; leaving it", folder)+ continue+ removed += 1+ if removed:+ log.info("[update] removed %d old update download(s) from %s", removed, root)+ return removeddiff --git a/jarvis/ui/web/update_routes.py b/jarvis/ui/web/update_routes.pyindex 054b1b7ca8..1af2bb8216 100644--- a/jarvis/ui/web/update_routes.py+++ b/jarvis/ui/web/update_routes.py@@ -65,7 +65,6 @@ import platform as platform_moduleimport reimport shutilimport sys-import tempfileimport timefrom collections.abc import Callablefrom pathlib import Path@@ -85,8 +84,12 @@ from jarvis.core.installer_update import (InstallerUpdateError,apply_installer,download_and_verify,+ download_workdir,installer_asset_name,+ machine_for_update,select_asset,+ sweep_stale_downloads,+ update_blocker,)from jarvis.core.process_utils import NO_WINDOW_CREATIONFLAGS@@ -829,7 +832,23 @@ def _on_git_progress(line: str) -> None:def _frozen_asset_name() -> str | None:"""The installer asset this machine installs, or ``None`` if there is none."""- return installer_asset_name(sys.platform, platform_module.machine())+ return installer_asset_name(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(@@ -866,7 +885,13 @@ async def _frozen_status(current: str) -> dict[str, object]:# 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,}+ await _sweep_old_downloads_once()asset_name = _frozen_asset_name()if asset_name is None:@@ -907,6 +932,7 @@ async def _frozen_status(current: str) -> dict[str, object]:if _is_newer(version, current):result["update_available"] = Trueresult["notes"] = latest.get("notes")+ result["blocked_reason"] = update_blocker()return result@@ -1033,6 +1059,12 @@ async def _apply_frozen(),)+ 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)+_progress.version = release_versiondef on_bytes(written: int, total: int | None) -> None:@@ -1046,7 +1078,7 @@ async def _apply_frozen(detail=f"{_human_bytes(written)} / {_human_bytes(total)}",)- workdir = Path(tempfile.mkdtemp(prefix="jarvis-update-"))+ workdir = download_workdir()try:_progress.enter(PHASE_DOWNLOADING, detail=asset.name)installer = await download_and_verify(@@ -1067,9 +1099,7 @@ async def _apply_frozen(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)- )+ handover = await asyncio.to_thread(lambda: apply_installer(installer, relaunch=relaunch))except HTTPException:shutil.rmtree(workdir, ignore_errors=True)raisediff --git a/packaging/windows/PersonalJarvis.iss b/packaging/windows/PersonalJarvis.issindex 7850214c49..b816a78015 100644--- a/packaging/windows/PersonalJarvis.iss+++ b/packaging/windows/PersonalJarvis.iss@@ -9,9 +9,11 @@; new setup as the SAME application and upgrades in place instead of leaving; two entries in "Installed apps".; * CloseApplications=yes. The in-app updater quits the app and runs this-; file with /SILENT /SUPPRESSMSGBOXES /CLOSEAPPLICATIONS /RELAUNCH=1, so-; Restart Manager closes anything still holding a file, the files are-; replaced, and the second [Run] entry starts Personal Jarvis again.+; file with /SILENT /SUPPRESSMSGBOXES /CLOSEAPPLICATIONS /RELAUNCH=1+; /WAITPID=<pid> /LOG=<file>, so Setup first waits for the old app to exit+; (InitializeSetup), Restart Manager closes anything still holding a file,+; the files are replaced, and the second [Run] entry starts Personal+; Jarvis again.; (/RESTARTAPPLICATIONS would only restart apps registered with; RegisterApplicationRestart, which this one is not.); * The uninstaller removes the program directory ONLY. Settings, memory,@@ -33,6 +35,12 @@#ifndef IconFile#define IconFile "..\..\assets\icons\jarvis.ico"#endif+; NEVER change this default. It is the identity Windows upgrades against. Only+; the installer end-to-end test overrides it (/DAppGuid=...), so its throwaway+; install can never touch, upgrade or uninstall a real Personal Jarvis.+#ifndef AppGuid+ #define AppGuid "7F1C4E42-2E5B-4F0A-9E1B-1A7B6C0D5E88"+#endif#define AppName "Personal Jarvis"#define AppPublisher "Personal Jarvis"@@ -42,8 +50,8 @@#define UserDataDirDisplay "%LOCALAPPDATA%\Jarvis"[Setup]-; NEVER change this GUID. It is the identity Windows upgrades against.-AppId={{7F1C4E42-2E5B-4F0A-9E1B-1A7B6C0D5E88}+; NEVER change AppGuid's default (see its #define above).+AppId={{{#AppGuid}}AppName={#AppName}AppVersion={#AppVersion}AppVerName={#AppName} {#AppVersion}@@ -119,11 +127,54 @@ constJarvisWmSettingChange = $001A;JarvisSmtoAbortIfHung = $0002;+ JarvisSynchronize = $00100000;+ { How long Setup waits for the app it replaces to exit (milliseconds). }+ JarvisWaitForAppMs = 120000;+function SendMessageTimeout(hWnd: Longint; Msg: Cardinal; wParam: Longint;lParam: string; fuFlags: Cardinal; uTimeout: Cardinal;var lpdwResult: Cardinal): Longint;external '[email protected] stdcall';+function JarvisOpenProcess(dwDesiredAccess: Cardinal; bInheritHandle: BOOL;+ dwProcessId: Cardinal): THandle;+ external '[email protected] stdcall';++function JarvisWaitForSingleObject(hHandle: THandle; dwMilliseconds: Cardinal): Cardinal;+ external '[email protected] stdcall';++function JarvisCloseHandle(hObject: THandle): BOOL;+ external '[email protected] stdcall';++{ The in-app updater passes /WAITPID=<pid of the running app> and quits right+ after starting this file. Setup starts faster than the app shuts down, and a+ silent install that meets files still in use answers with Abort - the app+ gone, nothing installed, nothing relaunched. So the first thing Setup does is+ wait for that process to end. A pid that no longer exists (already exited)+ opens no handle and costs nothing; a normal interactive install passes no+ /WAITPID at all. }+procedure WaitForReplacedApp;+var+ Pid: Integer;+ Handle: THandle;+begin+ Pid := StrToIntDef(ExpandConstant('{param:WAITPID|0}'), 0);+ if Pid <= 0 then+ Exit;+ Handle := JarvisOpenProcess(JarvisSynchronize, False, Pid);+ if Handle = 0 then+ Exit;+ Log(Format('Waiting up to %d s for process %d to exit.', [JarvisWaitForAppMs div 1000, Pid]));+ JarvisWaitForSingleObject(Handle, JarvisWaitForAppMs);+ JarvisCloseHandle(Handle);+end;++function InitializeSetup: Boolean;+begin+ WaitForReplacedApp;+ Result := True;+end;+{ Windows only re-reads the Environment key when it is told to. Without thisbroadcast the new PATH is invisible until the user signs out and back in. }procedure BroadcastEnvironmentChange;diff --git a/tests/integration/test_update_handover_real_os.py b/tests/integration/test_update_handover_real_os.pynew file mode 100644index 0000000000..f353230828--- /dev/null+++ b/tests/integration/test_update_handover_real_os.py@@ -0,0 +1,419 @@+"""The native-installer handover against the REAL operating system.++The unit tests fake the process table and the disk image tool; these do not.+Each one runs where its mechanism exists and skips honestly elsewhere:++* every OS — :class:`SubprocessCommandRunner` really spawns, really detaches,+ and hands the child the reset PyInstaller environment;+* macOS and Linux — the AppImage swap with the real ``/bin/sh`` relaunch+ waiter, which must start the new file only after the old process exited;+* macOS — a real ``.dmg`` built with ``hdiutil``, mounted, copied out of and+ detached by the real handover;+* Windows — the real Inno Setup script, compiled twice, installed, then+ upgraded over a RUNNING copy through the exact command the app sends. This+ one installs software, so it runs only when ``JARVIS_INSTALLER_E2E=1`` (the+ CI updater lane sets it). It uses its own AppId and a throwaway folder, so+ even a local run can never touch a real Personal Jarvis install.+"""++from __future__ import annotations++import os+import shutil+import subprocess+import sys+import textwrap+import time+from collections.abc import Callable+from pathlib import Path++import pytest++import jarvis.core.installer_update as iu+from jarvis.core.installer_update import (+ SubprocessCommandRunner,+ apply_installer,+)++REPO = Path(__file__).resolve().parents[2]+++def _wait_until(condition: Callable[[], bool], *, timeout_s: float, poll_s: float = 0.2) -> bool:+ deadline = time.monotonic() + timeout_s+ while time.monotonic() < deadline:+ if condition():+ return True+ time.sleep(poll_s)+ return condition()+++# --------------------------------------------------------------------------- #+# Every OS: the real runner+# --------------------------------------------------------------------------- #+def test_the_real_runner_reports_exit_code_and_output() -> None:+ rc, out, _err = SubprocessCommandRunner().run(+ [sys.executable, "-c", "import sys; print('ok'); sys.exit(3)"], timeout_s=60+ )+ assert rc == 3+ assert out.strip() == "ok"+++def test_the_real_runner_reports_a_missing_binary() -> None:+ rc, _out, err = SubprocessCommandRunner().run(+ ["jarvis-no-such-binary-for-the-updater"], timeout_s=10+ )+ assert rc == -1+ assert "not available" in err+++def test_the_real_runner_reports_a_timeout() -> None:+ rc, _out, err = SubprocessCommandRunner().run(+ [sys.executable, "-c", "import time; time.sleep(30)"], timeout_s=0.5+ )+ assert rc == -1+ assert "timed out" in err+++def test_the_real_runner_never_waits_for_input() -> None:+ """hdiutil asks for a licence on some images; a prompt must read EOF, not hang."""+ started = time.monotonic()+ rc, out, _err = SubprocessCommandRunner().run(+ [sys.executable, "-c", "import sys; print(repr(sys.stdin.read()))"], timeout_s=30+ )+ assert rc == 0+ assert out.strip() == "''"+ assert time.monotonic() - started < 25+++def test_a_detached_child_gets_a_fresh_pyinstaller_environment(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ monkeypatch.setenv("_PYI_ARCHIVE_FILE", str(tmp_path / "old" / "PersonalJarvis"))+ record = tmp_path / "env.txt"+ script = tmp_path / "child.py"+ script.write_text(+ "import os, pathlib, sys\n"+ "pathlib.Path(sys.argv[1]).write_text(\n"+ " os.environ.get('PYINSTALLER_RESET_ENVIRONMENT', '<unset>'), encoding='utf-8')\n",+ encoding="utf-8",+ )++ SubprocessCommandRunner().spawn_detached([sys.executable, str(script), str(record)])++ assert _wait_until(record.exists, timeout_s=30)+ assert _wait_until(lambda: record.read_text(encoding="utf-8") == "1", timeout_s=5)+++# --------------------------------------------------------------------------- #+# macOS + Linux: the AppImage swap and the real relaunch waiter+# --------------------------------------------------------------------------- #+_HAS_SH = os.name == "posix" and Path("/bin/sh").exists()++[email protected](not _HAS_SH, reason="the relaunch waiter is a POSIX sh script")+def test_appimage_handover_swaps_and_relaunches_after_the_old_app_exits(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ # Pretend to be the frozen build whose bootloader rewrote the library path.+ monkeypatch.setattr(iu, "is_frozen", lambda: True)+ monkeypatch.setenv("LD_LIBRARY_PATH", str(tmp_path / ".mount_old" / "_internal"))+ monkeypatch.setenv("LD_LIBRARY_PATH_ORIG", "/usr/local/lib/from-the-user")++ record = tmp_path / "relaunched.txt"+ live = tmp_path / "My Apps" / "Personal Jarvis.AppImage"+ live.parent.mkdir()+ live.write_text("#!/bin/sh\nexit 0\n", encoding="utf-8")+ live.chmod(0o755)++ downloaded = tmp_path / "download" / "PersonalJarvis-Linux-x86_64.AppImage"+ downloaded.parent.mkdir()+ downloaded.write_text(+ textwrap.dedent(+ f"""\+ #!/bin/sh+ printf '%s\\n%s\\n%s\\n' new "${{PYINSTALLER_RESET_ENVIRONMENT:-}}" \\+ "${{LD_LIBRARY_PATH:-}}" > '{record}'+ """+ ),+ encoding="utf-8",+ )+ downloaded.chmod(0o644) # the download is not executable; the swap makes it so++ old_app = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(2)"])+ try:+ message = apply_installer(+ downloaded,+ platform_name="linux",+ runner=SubprocessCommandRunner(),+ appimage_path=live,+ wait_for_pid=old_app.pid,+ )+ assert "replaced" in message+ time.sleep(0.8)+ assert not record.exists(), "the new version started while the old one still ran"+ old_app.wait(timeout=30)+ finally:+ if old_app.poll() is None:+ old_app.kill()++ assert _wait_until(+ lambda: record.exists() and len(record.read_text(encoding="utf-8").splitlines()) == 3,+ timeout_s=30,+ )+ assert record.read_text(encoding="utf-8").splitlines() == [+ "new",+ "1",+ "/usr/local/lib/from-the-user",+ ]+ assert os.access(live, os.X_OK)+ assert not (live.parent / f".{live.name}.new").exists()+++# --------------------------------------------------------------------------- #+# macOS: a real disk image+# --------------------------------------------------------------------------- #+_HAS_HDIUTIL = sys.platform == "darwin" and shutil.which("hdiutil") is not None+++def _make_dmg(source: Path, dmg: Path) -> None:+ """``hdiutil create`` is occasionally 'Resource busy' on CI; retry briefly."""+ last: subprocess.CompletedProcess[str] | None = None+ for _attempt in range(3):+ last = subprocess.run(+ [+ "hdiutil",+ "create",+ "-volname",+ "Personal Jarvis",+ "-srcfolder",+ str(source),+ "-format",+ "UDZO",+ "-ov",+ str(dmg),+ ],+ capture_output=True,+ text=True,+ timeout=300,+ check=False,+ )+ if last.returncode == 0:+ return+ time.sleep(2)+ pytest.fail(f"hdiutil create failed: {last.stderr if last else ''}")++[email protected](not _HAS_HDIUTIL, reason="real disk images exist on macOS only")+def test_macos_handover_swaps_the_bundle_out_of_a_real_disk_image(tmp_path: Path) -> None:+ source = tmp_path / "dmg-root"+ bundle = source / "Personal Jarvis.app" / "Contents"+ (bundle / "MacOS").mkdir(parents=True)+ (bundle / "MacOS" / "PersonalJarvis").write_text("new", encoding="utf-8")+ (bundle / "Frameworks").mkdir()+ # Framework bundles are full of relative symlinks; they must survive the copy.+ os.symlink("../MacOS/PersonalJarvis", bundle / "Frameworks" / "Current")+ # Real DMGs carry a drag-to-install link next to the app.+ os.symlink("/Applications", source / "Applications")+ dmg = tmp_path / "PersonalJarvis-macOS-arm64.dmg"+ _make_dmg(source, dmg)++ app = tmp_path / "Applications" / "Personal Jarvis.app"+ (app / "Contents" / "MacOS").mkdir(parents=True)+ (app / "Contents" / "MacOS" / "PersonalJarvis").write_text("old", encoding="utf-8")+ (app / "Contents" / "stale-from-the-old-version.txt").write_text("x", encoding="utf-8")++ message = apply_installer(+ dmg,+ platform_name="darwin",+ runner=SubprocessCommandRunner(),+ app_path=app,+ relaunch=False,+ )++ assert "replaced" in message+ assert (app / "Contents" / "MacOS" / "PersonalJarvis").read_text(encoding="utf-8") == "new"+ assert not (app / "Contents" / "stale-from-the-old-version.txt").exists()+ link = app / "Contents" / "Frameworks" / "Current"+ assert link.is_symlink()+ assert link.read_text(encoding="utf-8") == "new"+ assert not (app.parent / ".Personal Jarvis.app.old").exists()+ assert not (app.parent / ".Personal Jarvis.app.new").exists()+ # The volume was released: no mount of ours is left behind.+ info = subprocess.run(+ ["hdiutil", "info"], capture_output=True, text=True, timeout=60, check=False+ )+ assert "jarvis-dmg-" not in info.stdout+++# --------------------------------------------------------------------------- #+# Windows: the real Inno Setup upgrade over a running app+# --------------------------------------------------------------------------- #+_E2E = os.environ.get("JARVIS_INSTALLER_E2E") == "1"+# NOT the product's AppId: this throwaway install must never upgrade, or be+# uninstalled as, a real Personal Jarvis.+TEST_APP_GUID = "D0C7E2A1-5F4B-4E1C-9A3D-7E6B5C4A3F21"++_FAKE_APP_SOURCE = r"""+using System;+using System.IO;+using System.Threading;++class FakeJarvis {+ static int Main(string[] args) {+ string home = AppDomain.CurrentDomain.BaseDirectory;+ if (args.Length == 2 && args[0] == "hold") {+ // The "running old app": holds its own image locked, then quits slowly.+ File.WriteAllText(Path.Combine(home, "holding.txt"), "1");+ Thread.Sleep(int.Parse(args[1]) * 1000);+ return 0;+ }+ string version = File.ReadAllText(Path.Combine(home, "version.txt")).Trim();+ File.AppendAllText(Path.Combine(home, "launches.txt"), version + Environment.NewLine);+ return 0;+ }+}+"""+++def _iscc() -> Path | None:+ found = shutil.which("ISCC.exe") or shutil.which("ISCC")+ candidates = [Path(found)] if found else []+ for base in ("ProgramFiles(x86)", "ProgramFiles", "LOCALAPPDATA"):+ root = os.environ.get(base)+ if root:+ sub = ("Programs", "Inno Setup 6") if base == "LOCALAPPDATA" else ("Inno Setup 6",)+ candidates.append(Path(root, *sub, "ISCC.exe"))+ return next((c for c in candidates if c.is_file()), None)+++def _csc() -> Path | None:+ windir = os.environ.get("WINDIR", r"C:\Windows")+ csc = Path(windir) / "Microsoft.NET" / "Framework64" / "v4.0.30319" / "csc.exe"+ return csc if csc.is_file() else None+++def _uninstall_key_exists() -> bool:+ import winreg++ key = (+ r"Software\Microsoft\Windows\CurrentVersion\Uninstall"+ "\\{" + TEST_APP_GUID + "}_is1"+ )+ try:+ winreg.CloseKey(winreg.OpenKey(winreg.HKEY_CURRENT_USER, key))+ except OSError:+ return False+ return True+++def _build_setup(iscc: Path, fake_exe: Path, version: str, work: Path) -> Path:+ source = work / f"src-{version}"+ source.mkdir()+ shutil.copy2(fake_exe, source / "PersonalJarvis.exe")+ (source / "version.txt").write_text(version, encoding="utf-8")+ out = work / f"out-{version}"+ subprocess.run(+ [+ str(iscc),+ "/Q",+ f"/DAppVersion={version}",+ f"/DSourceDir={source}",+ f"/DOutputDir={out}",+ f"/DAppGuid={TEST_APP_GUID}",+ str(REPO / "packaging" / "windows" / "PersonalJarvis.iss"),+ ],+ check=True,+ timeout=600,+ )+ setup = out / "PersonalJarvis-Setup-x64.exe"+ assert setup.is_file()+ return setup+++ sys.platform != "win32" or not _E2E,+ reason="installs software: Windows only, opt in with JARVIS_INSTALLER_E2E=1",+)+def test_windows_installer_upgrades_a_running_app_and_brings_it_back(tmp_path: Path) -> None:+ iscc, csc = _iscc(), _csc()+ if iscc is None or csc is None:+ pytest.skip("needs Inno Setup 6 (ISCC.exe) and the .NET Framework C# compiler")++ work = tmp_path / "work"+ work.mkdir()+ (work / "FakeJarvis.cs").write_text(_FAKE_APP_SOURCE, encoding="utf-8")+ fake_exe = work / "FakeJarvis.exe"+ subprocess.run(+ [str(csc), "/nologo", "/target:winexe", f"/out:{fake_exe}", str(work / "FakeJarvis.cs")],+ check=True,+ timeout=300,+ )+ setup_v1 = _build_setup(iscc, fake_exe, "1.0.0", work)+ setup_v2 = _build_setup(iscc, fake_exe, "2.0.0", work)++ app_dir = tmp_path / "Personal Jarvis Test"+ logs = tmp_path / "log dir" # a space: the /LOG= argument must survive quoting+ logs.mkdir()+ holder: subprocess.Popen[bytes] | None = None+ try:+ first = subprocess.run(+ [+ str(setup_v1),+ "/VERYSILENT",+ "/SUPPRESSMSGBOXES",+ "/NORESTART",+ "/NOICONS",+ f"/DIR={app_dir}",+ "/MERGETASKS=!addtopath,!desktopicon",+ f"/LOG={logs / 'first install.log'}",+ ],+ timeout=600,+ check=False,+ )+ assert first.returncode == 0, "the first install failed"+ assert (app_dir / "version.txt").read_text(encoding="utf-8") == "1.0.0"++ # The old version is running and holds its executable locked, and it+ # needs a few seconds to quit — longer than Setup takes to start.+ holder = subprocess.Popen([str(app_dir / "PersonalJarvis.exe"), "hold", "6"])+ assert _wait_until((app_dir / "holding.txt").exists, timeout_s=30)++ installer_log = logs / "update installer.log"+ message = apply_installer(+ setup_v2,+ platform_name="win32",+ runner=SubprocessCommandRunner(),+ wait_for_pid=holder.pid,+ installer_log=installer_log,+ )+ assert "restarts by itself" in message+ holder.wait(timeout=60)++ launches = app_dir / "launches.txt"+ assert _wait_until(launches.exists, timeout_s=300, poll_s=1.0), (+ "the upgraded app never came back; installer log:\n"+ + (installer_log.read_text(errors="replace") if installer_log.exists() else "-")+ )+ assert _wait_until(+ lambda: "Log closed." in installer_log.read_text(errors="replace"),+ timeout_s=120,+ poll_s=1.0,+ )+ log_text = installer_log.read_text(errors="replace")+ assert (app_dir / "version.txt").read_text(encoding="utf-8") == "2.0.0"+ # Came back exactly once, as the NEW version.+ assert launches.read_text(encoding="utf-8").split() == ["2.0.0"]+ assert f"for process {holder.pid} to exit" in log_text+ finally:+ if holder is not None and holder.poll() is None:+ holder.kill()+ uninstaller = app_dir / "unins000.exe"+ if uninstaller.exists():+ subprocess.run(+ [str(uninstaller), "/VERYSILENT", "/SUPPRESSMSGBOXES", "/NORESTART"],+ timeout=300,+ check=False,+ )+ # The uninstaller hands off to a temp copy and returns at once.+ _wait_until(lambda: not _uninstall_key_exists(), timeout_s=120, poll_s=1.0)+ assert not _uninstall_key_exists(), "the throwaway install was not removed"diff --git a/tests/unit/core/test_installer_update.py b/tests/unit/core/test_installer_update.pyindex a06b27243f..14f718a6fe 100644--- a/tests/unit/core/test_installer_update.py+++ b/tests/unit/core/test_installer_update.py@@ -9,8 +9,10 @@ so every case below runs on Windows, macOS and Linux alike.from __future__ import annotations+import asyncioimport hashlibimport os+import shutilimport statimport subprocessimport sys@@ -19,18 +21,25 @@ from pathlib import Pathimport pytest+import jarvis.core.installer_update as iufrom jarvis.core.installer_update import (CHECKSUMS_ASSET_NAME,+ MACOS_OPEN,WINDOWS_RELAUNCH_PARAM,+ WINDOWS_WAIT_PID_PARAM,InstallerAsset,InstallerUpdateError,apply_installer,download_and_verify,+ handover_env,installer_asset_name,+ machine_for_update,parse_sha256sums,relaunch_after_exit_command,select_asset,select_installer_asset,+ sweep_stale_downloads,+ update_blocker,)from tests.fakes.fake_installer_update import (FakeAssetFetcher,@@ -226,7 +235,13 @@ def test_windows_handover_runs_the_silent_in_place_upgrade(tmp_path: Path) -> Noinstaller.write_bytes(PAYLOAD)runner = FakeCommandRunner()- message = apply_installer(installer, platform_name="win32", runner=runner)+ message = apply_installer(+ installer,+ platform_name="win32",+ runner=runner,+ wait_for_pid=4242,+ installer_log=tmp_path / "update-installer.log",+ )assert runner.spawned == [[@@ -236,6 +251,8 @@ def test_windows_handover_runs_the_silent_in_place_upgrade(tmp_path: Path) -> No"/CLOSEAPPLICATIONS","/NORESTART",WINDOWS_RELAUNCH_PARAM,+ f"{WINDOWS_WAIT_PID_PARAM}=4242",+ f"/LOG={tmp_path / 'update-installer.log'}",]]assert "restarts by itself" in message@@ -301,7 +318,18 @@ def _mounting_runner(app_name: str, payload: str) -> FakeCommandRunner:bundle.mkdir(parents=True, exist_ok=True)(bundle / "PersonalJarvis").write_text(payload, encoding="utf-8")- return FakeCommandRunner(on_run=_mount)+ def _detach(command: list[str]) -> None:+ # A real detach empties the mountpoint; the updater then removes the+ # bare directory (and only an empty one).+ if len(command) > 2 and command[1] == "detach":+ for child in Path(command[2]).iterdir():+ shutil.rmtree(child)++ def _on_run(command: list[str]) -> None:+ _mount(command)+ _detach(command)++ return FakeCommandRunner(on_run=_on_run)def test_macos_handover_replaces_the_running_app_and_relaunches(@@ -321,7 +349,7 @@ def test_macos_handover_replaces_the_running_app_and_relaunches(assert (app / "Contents" / "MacOS" / "PersonalJarvis").read_text(encoding="utf-8") == "new"# The relaunch waits for the old app: started earlier it would bounce off# the single-instance lock and leave the user on the old version.- assert runner.spawned == [relaunch_after_exit_command(4242, ["open", "-n", str(app)])]+ assert runner.spawned == [relaunch_after_exit_command(4242, [MACOS_OPEN, "-n", str(app)])]# The volume is always released, success or not.assert any(cmd[:2] == ["hdiutil", "detach"] for cmd in runner.ran)assert "replaced" in message@@ -547,10 +575,395 @@ def test_relaunch_waiter_passes_paths_with_spaces_verbatim(tmp_path: Path) -> Nogone = subprocess.Popen([sys.executable, "-c", "pass"])gone.wait(timeout=10)subprocess.run(- relaunch_after_exit_command(- gone.pid, [sys.executable, str(script), str(target), "a b c"]- ),+ relaunch_after_exit_command(gone.pid, [sys.executable, str(script), str(target), "a b c"]),timeout=15,check=True,)assert target.read_text(encoding="utf-8") == "a b c"+++# --------------------------------------------------------------------------- #+# Which CPU's installer — Rosetta moves an Intel build to native arm64+# --------------------------------------------------------------------------- #+def test_an_intel_build_under_rosetta_updates_to_the_native_arm64_dmg() -> None:+ machine = machine_for_update("darwin", "x86_64", translated=lambda: True)+ assert machine == "arm64"+ assert installer_asset_name("darwin", machine) == "PersonalJarvis-macOS-arm64.dmg"+++def test_a_real_intel_mac_keeps_the_intel_dmg() -> None:+ assert machine_for_update("darwin", "x86_64", translated=lambda: False) == "x86_64"+++ ("platform_name", "machine"),+ [("win32", "AMD64"), ("linux", "x86_64"), ("darwin", "arm64")],+)+def test_only_an_intel_mac_process_asks_about_rosetta(platform_name: str, machine: str) -> None:+ def _never() -> bool:+ raise AssertionError("only an x86_64 process on a Mac can be translated")++ assert machine_for_update(platform_name, machine, translated=_never) == machine+++def test_the_rosetta_probe_answers_on_every_ci_host() -> None:+ """Runs the real probe: ctypes + sysctlbyname on macOS, a no-op elsewhere.++ CI interpreters are native, so the honest answer everywhere is False — and+ the call must never raise, because the status poll makes it.+ """+ assert iu._running_under_rosetta() is False+++# --------------------------------------------------------------------------- #+# The environment the handover hands on+# --------------------------------------------------------------------------- #+def test_handover_env_starts_the_new_build_as_a_fresh_pyinstaller_process() -> None:+ base = {"PATH": "/usr/bin", "_PYI_ARCHIVE_FILE": "/old/PersonalJarvis"}+ env = handover_env(base, frozen=True)+ assert env["PYINSTALLER_RESET_ENVIRONMENT"] == "1"+ assert env["PATH"] == "/usr/bin"+ assert "PYINSTALLER_RESET_ENVIRONMENT" not in base, "the caller's mapping was mutated"+++def test_handover_env_restores_the_users_library_path_in_a_frozen_build() -> None:+ env = handover_env(+ {+ "LD_LIBRARY_PATH": "/run/user/1000/.mount_Jarvis/usr/bin/_internal:/opt/lib",+ "LD_LIBRARY_PATH_ORIG": "/opt/lib",+ },+ frozen=True,+ )+ assert env["LD_LIBRARY_PATH"] == "/opt/lib"+ assert "LD_LIBRARY_PATH_ORIG" not in env+++def test_handover_env_drops_a_library_path_only_the_bundle_set() -> None:+ bundle_only = "/run/user/1000/.mount_Jarvis/usr/bin/_internal"+ env = handover_env({"LD_LIBRARY_PATH": bundle_only}, frozen=True)+ assert "LD_LIBRARY_PATH" not in env+++def test_handover_env_leaves_a_source_install_library_path_alone() -> None:+ env = handover_env({"LD_LIBRARY_PATH": "/opt/lib"}, frozen=False)+ assert env["LD_LIBRARY_PATH"] == "/opt/lib"+++# --------------------------------------------------------------------------- #+# Windows: wait for the old app, leave a log+# --------------------------------------------------------------------------- #+def test_windows_handover_waits_for_this_process_by_default(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ monkeypatch.setattr(iu, "_windows_installer_log", lambda: None)+ installer = tmp_path / "PersonalJarvis-Setup-x64.exe"+ installer.write_bytes(PAYLOAD)+ runner = FakeCommandRunner()++ apply_installer(installer, platform_name="win32", runner=runner)++ assert f"{WINDOWS_WAIT_PID_PARAM}={os.getpid()}" in runner.spawned[0]+ # No log directory: the update still runs, just without a log flag.+ assert not any(arg.startswith("/LOG=") for arg in runner.spawned[0])+++def test_windows_installer_log_lives_in_the_user_log_directory(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ from jarvis.core.paths import user_logs_dir++ monkeypatch.setenv("LOCALAPPDATA", str(tmp_path))+ monkeypatch.setenv("HOME", str(tmp_path))+ log_path = iu._windows_installer_log()+ assert log_path == user_logs_dir() / iu.WINDOWS_INSTALLER_LOG_NAME+ assert log_path is not None and log_path.parent.is_dir()+++def test_windows_handover_reports_an_installer_that_cannot_start(tmp_path: Path) -> None:+ installer = tmp_path / "PersonalJarvis-Setup-x64.exe"+ installer.write_bytes(PAYLOAD)+ runner = FakeCommandRunner(spawn_error=OSError("blocked by policy"))+ with pytest.raises(InstallerUpdateError, match="blocked by policy"):+ apply_installer(+ installer,+ platform_name="win32",+ runner=runner,+ installer_log=tmp_path / "update-installer.log",+ )+++def _iss_text() -> str:+ iss = Path(__file__).resolve().parents[3] / "packaging" / "windows" / "PersonalJarvis.iss"+ return iss.read_text(encoding="utf-8")+++def test_windows_installer_script_waits_for_the_replaced_app() -> None:+ """The flag the app passes must be the one the installer waits on."""+ text = _iss_text()+ name = WINDOWS_WAIT_PID_PARAM.lstrip("/")+ assert f"{{param:{name}|0}}" in text+ init = text.split("function InitializeSetup", 1)[1].split("\nend;", 1)[0]+ assert "WaitForReplacedApp" in init+ wait = text.split("procedure WaitForReplacedApp", 1)[1].split("\nend;", 1)[0]+ assert "JarvisOpenProcess" in wait+ assert "JarvisWaitForSingleObject" in wait+ assert "JarvisCloseHandle" in wait+ # Bounded: a pid that never exits must not keep Setup waiting forever.+ assert "JarvisWaitForAppMs = 120000;" in text+++def test_windows_installer_identity_never_changes() -> None:+ """Every release must upgrade the same AppId, or users get two installs."""+ text = _iss_text()+ assert '#define AppGuid "7F1C4E42-2E5B-4F0A-9E1B-1A7B6C0D5E88"' in text+ assert "AppId={{{#AppGuid}}" in text+++# --------------------------------------------------------------------------- #+# A stalled transfer ends instead of holding the update forever+# --------------------------------------------------------------------------- #+def _write_partial(dest: Path) -> None:+ """Blocking write, kept out of the async body (ruff ASYNC240)."""+ dest.write_bytes(PAYLOAD[:8])+++class _StallingFetcher(FakeAssetFetcher):+ """Sends a few bytes, then nothing — the trickle a read timeout never ends."""++ stall_manifest: bool = False++ async def get_text(self, url: str, *, max_bytes: int) -> str:+ if self.stall_manifest:+ await asyncio.sleep(30)+ return await super().get_text(url, max_bytes=max_bytes)++ async def download(self, url: str, dest: Path, **_kwargs: object) -> int:+ _write_partial(dest)+ await asyncio.sleep(30)+ return 8+++async def test_download_and_verify_gives_up_on_a_stalled_download(tmp_path: Path) -> None:+ asset = _asset("PersonalJarvis-Setup-x64.exe")+ fetcher = _StallingFetcher(manifest=f"{PAYLOAD_SHA256} {asset.name}\n", payload=PAYLOAD)+ started = time.monotonic()+ with pytest.raises(InstallerUpdateError, match="did not finish downloading"):+ await download_and_verify(+ asset,+ _asset(CHECKSUMS_ASSET_NAME),+ dest_dir=tmp_path,+ fetcher=fetcher,+ download_timeout_s=0.3,+ )+ assert time.monotonic() - started < 10+ # The partial file is never left where a later step could run it.+ assert not (tmp_path / asset.name).exists()+++async def test_download_and_verify_gives_up_on_a_stalled_manifest(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ monkeypatch.setattr(iu, "READ_TIMEOUT_S", 0.3)+ asset = _asset("PersonalJarvis-Setup-x64.exe")+ fetcher = _StallingFetcher(manifest=f"{PAYLOAD_SHA256} {asset.name}\n", payload=PAYLOAD)+ fetcher.stall_manifest = True+ with pytest.raises(InstallerUpdateError, match=CHECKSUMS_ASSET_NAME):+ await download_and_verify(+ asset, _asset(CHECKSUMS_ASSET_NAME), dest_dir=tmp_path, fetcher=fetcher+ )+ assert fetcher.download_urls == []+++# --------------------------------------------------------------------------- #+# macOS: the disk image is always released, never walked into+# --------------------------------------------------------------------------- #+def test_macos_handover_releases_a_volume_hdiutil_mounted_while_failing(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """hdiutil can mount the image and still report failure (or time out)."""+ dmg = tmp_path / "PersonalJarvis-macOS-arm64.dmg"+ dmg.write_bytes(PAYLOAD)+ app = tmp_path / "Applications" / "Personal Jarvis.app"+ (app / "Contents").mkdir(parents=True)+ monkeypatch.setattr(iu, "_is_mounted", lambda _path: True)+ runner = FakeCommandRunner(+ results={"hdiutil attach": RunResult(returncode=1, stderr="hdiutil timed out")}+ )++ with pytest.raises(InstallerUpdateError, match="could not mount"):+ apply_installer(dmg, platform_name="darwin", runner=runner, app_path=app)++ assert any(cmd[:2] == ["hdiutil", "detach"] for cmd in runner.ran)+++def test_macos_handover_never_deletes_inside_a_volume_it_could_not_detach(+ tmp_path: Path,+) -> None:+ dmg = tmp_path / "PersonalJarvis-macOS-arm64.dmg"+ dmg.write_bytes(PAYLOAD)+ app = tmp_path / "Applications" / "Personal Jarvis.app"+ (app / "Contents").mkdir(parents=True)++ def _mount_only(command: list[str]) -> None:+ if command[1] == "attach":+ mountpoint = Path(command[command.index("-mountpoint") + 1])+ (mountpoint / "Personal Jarvis.app" / "Contents").mkdir(parents=True)++ runner = FakeCommandRunner(+ on_run=_mount_only,+ results={"hdiutil detach": RunResult(returncode=16, stderr="resource busy")},+ )+ apply_installer(dmg, platform_name="darwin", runner=runner, app_path=app, relaunch=False)++ attach = next(cmd for cmd in runner.ran if cmd[1] == "attach")+ mountpoint = Path(attach[attach.index("-mountpoint") + 1])+ try:+ # Still "mounted": its contents must be exactly as the volume left them.+ assert (mountpoint / "Personal Jarvis.app" / "Contents").is_dir()+ finally:+ shutil.rmtree(mountpoint, ignore_errors=True)+++def test_macos_handover_removes_its_mountpoint_after_a_clean_detach(tmp_path: Path) -> None:+ dmg = tmp_path / "PersonalJarvis-macOS-arm64.dmg"+ dmg.write_bytes(PAYLOAD)+ app = tmp_path / "Applications" / "Personal Jarvis.app"+ (app / "Contents").mkdir(parents=True)+ runner = _mounting_runner("Personal Jarvis.app", "new")++ apply_installer(dmg, platform_name="darwin", runner=runner, app_path=app, relaunch=False)++ attach = next(cmd for cmd in runner.ran if cmd[1] == "attach")+ assert not Path(attach[attach.index("-mountpoint") + 1]).exists()+ assert runner.spawned == []+++# --------------------------------------------------------------------------- #+# Preflight — refuse BEFORE downloading what cannot be installed here+# --------------------------------------------------------------------------- #+def test_update_blocker_lets_a_writable_app_bundle_update(tmp_path: Path) -> None:+ app = tmp_path / "Applications" / "Personal Jarvis.app"+ app.mkdir(parents=True)+ assert update_blocker("darwin", app_path=app) is None+++def test_update_blocker_explains_app_translocation() -> None:+ app = Path("/private/var/folders/xy/T/AppTranslocation/0A1B/d/Personal Jarvis.app")+ reason = update_blocker("darwin", app_path=app)+ assert reason is not None+ assert "Applications folder" in reason+++def test_update_blocker_refuses_a_read_only_app_location(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ monkeypatch.setattr(iu, "_can_write", lambda _folder: False)+ app = tmp_path / "Volumes" / "Personal Jarvis" / "Personal Jarvis.app"+ reason = update_blocker("darwin", app_path=app)+ assert reason is not None+ assert str(app.parent) in reason+++def test_update_blocker_without_an_app_bundle_says_so() -> None:+ # A source run has no bundle; the real lookup must answer, not raise.+ reason = update_blocker("darwin")+ assert reason is not None+ assert "app bundle" in reason+++def test_update_blocker_lets_a_writable_appimage_update(tmp_path: Path) -> None:+ appimage = tmp_path / "PersonalJarvis.AppImage"+ appimage.write_bytes(b"old")+ assert update_blocker("linux", appimage_path=appimage) is None+++def test_update_blocker_outside_an_appimage_points_to_the_download(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.delenv("APPIMAGE", raising=False)+ reason = update_blocker("linux")+ assert reason is not None+ assert "AppImage" in reason+++def test_update_blocker_refuses_an_appimage_in_a_protected_folder(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ monkeypatch.setattr(iu, "_can_write", lambda _folder: False)+ appimage = tmp_path / "opt" / "PersonalJarvis.AppImage"+ reason = update_blocker("linux", appimage_path=appimage)+ assert reason is not None+ assert "folder you own" in reason+++ os.name != "posix" or getattr(os, "geteuid", lambda: 0)() == 0,+ reason="POSIX permission bits; root may write anywhere",+)+def test_update_blocker_sees_a_real_read_only_folder(tmp_path: Path) -> None:+ folder = tmp_path / "opt"+ folder.mkdir()+ appimage = folder / "PersonalJarvis.AppImage"+ appimage.write_bytes(b"old")+ folder.chmod(0o555)+ try:+ assert update_blocker("linux", appimage_path=appimage) is not None+ finally:+ folder.chmod(0o755)+++def test_update_blocker_never_blocks_windows() -> None:+ assert update_blocker("win32") is None+++# --------------------------------------------------------------------------- #+# Old downloads are reclaimed+# --------------------------------------------------------------------------- #+def _age(path: Path, seconds: float) -> None:+ stamp = time.time() - seconds+ os.utime(path, (stamp, stamp))+++def test_sweep_removes_only_old_update_downloads(tmp_path: Path) -> None:+ old = tmp_path / "jarvis-update-old"+ old.mkdir()+ (old / "PersonalJarvis-Setup-x64.exe").write_bytes(b"x")+ fresh = tmp_path / "jarvis-update-fresh"+ fresh.mkdir()+ (fresh / "PersonalJarvis-Setup-x64.exe").write_bytes(b"x")+ stranger = tmp_path / "someone-elses-folder"+ stranger.mkdir()+ for path in (old / "PersonalJarvis-Setup-x64.exe", old, stranger):+ _age(path, 7 * 3600)++ assert sweep_stale_downloads(tmp_path) == 1+ assert not old.exists()+ assert fresh.exists()+ assert stranger.exists()+++def test_sweep_keeps_a_folder_whose_download_is_still_growing(tmp_path: Path) -> None:+ folder = tmp_path / "jarvis-update-busy"+ folder.mkdir()+ (folder / "PersonalJarvis-Setup-x64.exe").write_bytes(b"x")+ _age(folder, 7 * 3600) # the folder is old, the file inside is not+ assert sweep_stale_downloads(tmp_path) == 0+ assert folder.exists()+++def test_sweep_never_raises_on_a_missing_temp_root(tmp_path: Path) -> None:+ assert sweep_stale_downloads(tmp_path / "missing") == 0++[email protected](sys.platform != "win32", reason="Windows refuses to delete an open file")+def test_sweep_leaves_an_installer_that_is_still_open(tmp_path: Path) -> None:+ folder = tmp_path / "jarvis-update-running"+ folder.mkdir()+ exe = folder / "PersonalJarvis-Setup-x64.exe"+ exe.write_bytes(b"x")+ _age(exe, 7 * 3600)+ _age(folder, 7 * 3600)+ with exe.open("rb"):+ assert sweep_stale_downloads(tmp_path) == 0+ assert exe.exists()+ assert sweep_stale_downloads(tmp_path) == 1diff --git a/tests/unit/core/test_installer_update_http.py b/tests/unit/core/test_installer_update_http.pynew file mode 100644index 0000000000..89cd4f78f7--- /dev/null+++ b/tests/unit/core/test_installer_update_http.py@@ -0,0 +1,274 @@+"""The REAL network edge of the frozen updater: ``HttpxAssetFetcher``.++``test_installer_update.py`` fakes the fetcher wholesale; this file keeps the+real httpx client and fakes only the wire, through ``httpx.MockTransport``.+That is where redirects, size caps and progress actually happen, so this is+where they are proven:++* GitHub answers an asset URL with a redirect to its download CDN — followed,+ but only ever over https.+* The checksum manifest and the installer are both size-capped WHILE they+ stream, never after the whole body sits in memory or on disk.+* Progress reports a real total when the server states one and falls back to+ the release metadata when it does not.++No socket is opened; every case runs on Windows, macOS and Linux alike.+"""++from __future__ import annotations++import hashlib+from collections.abc import AsyncIterator, Callable+from pathlib import Path++import httpx+import pytest++from jarvis.core.installer_update import (+ CHECKSUMS_ASSET_NAME,+ MAX_CHECKSUMS_BYTES,+ HttpxAssetFetcher,+ InstallerAsset,+ InstallerUpdateError,+ download_and_verify,+)++ASSET_NAME = "PersonalJarvis-Setup-x64.exe"+PAYLOAD = b"a real installer would be a few hundred MB " * 2048+DIGEST = hashlib.sha256(PAYLOAD).hexdigest()+RELEASE = "https://github.com/PersonalJarvis/PersonalJarvis/releases/download/v9.9.9"+CDN = "https://release-assets.githubusercontent.com/github-production-release-asset"+++def _asset(name: str, size: int = len(PAYLOAD)) -> InstallerAsset:+ return InstallerAsset(name=name, url=f"{RELEASE}/{name}", size=size)+++class _Wire:+ """A scripted GitHub: URL -> response factory, plus a log of every hop."""++ def __init__(self, routes: dict[str, Callable[[], httpx.Response]]) -> None:+ self.routes = routes+ self.requested: list[str] = []++ def handler(self, request: httpx.Request) -> httpx.Response:+ url = str(request.url)+ self.requested.append(url)+ factory = self.routes.get(url)+ return factory() if factory is not None else httpx.Response(404)++ def fetcher(self) -> HttpxAssetFetcher:+ return HttpxAssetFetcher(transport=httpx.MockTransport(self.handler))+++def _redirect(target: str) -> Callable[[], httpx.Response]:+ return lambda: httpx.Response(302, headers={"Location": target})+++def _body(payload: bytes) -> Callable[[], httpx.Response]:+ return lambda: httpx.Response(200, content=payload)+++def _github_release(+ *, payload: bytes = PAYLOAD, manifest: str | None = None+) -> dict[str, Callable[[], httpx.Response]]:+ """The two hops a real release download takes for each asset."""+ text = manifest if manifest is not None else f"{DIGEST} {ASSET_NAME}\n"+ return {+ f"{RELEASE}/{ASSET_NAME}": _redirect(f"{CDN}/installer"),+ f"{CDN}/installer": _body(payload),+ f"{RELEASE}/{CHECKSUMS_ASSET_NAME}": _redirect(f"{CDN}/manifest"),+ f"{CDN}/manifest": _body(text.encode("utf-8")),+ }+++async def test_the_real_client_follows_githubs_redirect_and_verifies(tmp_path: Path) -> None:+ wire = _Wire(_github_release())+ ticks: list[tuple[int, int | None]] = []++ installer = await download_and_verify(+ _asset(ASSET_NAME),+ _asset(CHECKSUMS_ASSET_NAME, size=0),+ dest_dir=tmp_path,+ fetcher=wire.fetcher(),+ on_progress=lambda written, total: ticks.append((written, total)),+ )++ assert installer.read_bytes() == PAYLOAD+ assert f"{CDN}/installer" in wire.requested+ # The bar starts at zero and ends on exactly the published size.+ assert ticks[0] == (0, len(PAYLOAD))+ assert ticks[-1] == (len(PAYLOAD), len(PAYLOAD))+ assert [written for written, _total in ticks] == sorted(written for written, _ in ticks)+++async def test_a_tampered_download_is_refused_through_the_real_client(tmp_path: Path) -> None:+ wire = _Wire(_github_release(payload=PAYLOAD + b"one injected byte"))+ with pytest.raises(InstallerUpdateError, match="SHA-256"):+ await download_and_verify(+ _asset(ASSET_NAME),+ _asset(CHECKSUMS_ASSET_NAME),+ dest_dir=tmp_path,+ fetcher=wire.fetcher(),+ )+ assert not (tmp_path / ASSET_NAME).exists()+++async def test_a_redirect_to_plain_http_is_refused_before_it_is_sent(tmp_path: Path) -> None:+ routes = _github_release()+ routes[f"{RELEASE}/{ASSET_NAME}"] = _redirect("http://downgrade.invalid/installer")+ routes["http://downgrade.invalid/installer"] = _body(PAYLOAD)+ wire = _Wire(routes)++ with pytest.raises(InstallerUpdateError, match="non-https"):+ await download_and_verify(+ _asset(ASSET_NAME),+ _asset(CHECKSUMS_ASSET_NAME),+ dest_dir=tmp_path,+ fetcher=wire.fetcher(),+ )++ assert "http://downgrade.invalid/installer" not in wire.requested+ assert not (tmp_path / ASSET_NAME).exists()+++async def test_a_manifest_redirected_to_plain_http_is_refused(tmp_path: Path) -> None:+ routes = _github_release()+ routes[f"{RELEASE}/{CHECKSUMS_ASSET_NAME}"] = _redirect("http://downgrade.invalid/sums")+ wire = _Wire(routes)++ with pytest.raises(InstallerUpdateError, match="non-https"):+ await download_and_verify(+ _asset(ASSET_NAME),+ _asset(CHECKSUMS_ASSET_NAME),+ dest_dir=tmp_path,+ fetcher=wire.fetcher(),+ )+ # Nothing was downloaded without a trustworthy manifest.+ assert f"{CDN}/installer" not in wire.requested+++async def test_an_http_error_status_is_a_refusal(tmp_path: Path) -> None:+ routes = _github_release()+ routes[f"{CDN}/installer"] = lambda: httpx.Response(503)+ with pytest.raises(InstallerUpdateError, match="could not download"):+ await download_and_verify(+ _asset(ASSET_NAME),+ _asset(CHECKSUMS_ASSET_NAME),+ dest_dir=tmp_path,+ fetcher=_Wire(routes).fetcher(),+ )+ assert not (tmp_path / ASSET_NAME).exists()+++async def test_an_oversized_manifest_is_refused_while_it_streams(tmp_path: Path) -> None:+ """The cap must bite mid-stream: a hostile endpoint never fills memory."""+ produced = 0++ async def endless() -> AsyncIterator[bytes]:+ nonlocal produced+ # Bounded only so a regression fails the test instead of hanging it.+ while produced < 50 * MAX_CHECKSUMS_BYTES:+ produced += 4096+ yield b"#" * 4096++ routes = _github_release()+ routes[f"{CDN}/manifest"] = lambda: httpx.Response(200, content=endless())++ with pytest.raises(InstallerUpdateError, match="larger than"):+ await download_and_verify(+ _asset(ASSET_NAME),+ _asset(CHECKSUMS_ASSET_NAME),+ dest_dir=tmp_path,+ fetcher=_Wire(routes).fetcher(),+ )+ assert produced < 2 * MAX_CHECKSUMS_BYTES+++async def test_an_oversized_installer_is_refused_while_it_streams(tmp_path: Path) -> None:+ produced = 0++ async def endless() -> AsyncIterator[bytes]:+ nonlocal produced+ while produced < 50 * len(PAYLOAD):+ produced += 65536+ yield b"\0" * 65536++ routes = _github_release()+ routes[f"{CDN}/installer"] = lambda: httpx.Response(200, content=endless())++ with pytest.raises(InstallerUpdateError, match="exceeded"):+ await download_and_verify(+ # The release metadata does not know the size, so only the stream+ # guard can stop it.+ _asset(ASSET_NAME, size=0),+ _asset(CHECKSUMS_ASSET_NAME),+ dest_dir=tmp_path,+ fetcher=_Wire(routes).fetcher(),+ max_bytes=len(PAYLOAD),+ )+ assert produced < 4 * len(PAYLOAD)+ assert not (tmp_path / ASSET_NAME).exists()+++async def test_progress_without_a_content_length_uses_the_release_size(tmp_path: Path) -> None:+ async def chunked() -> AsyncIterator[bytes]:+ for start in range(0, len(PAYLOAD), 10_000):+ yield PAYLOAD[start : start + 10_000]++ routes = _github_release()+ # An async body has no Content-Length: a proxy's chunked transfer.+ routes[f"{CDN}/installer"] = lambda: httpx.Response(200, content=chunked())+ ticks: list[tuple[int, int | None]] = []++ await download_and_verify(+ _asset(ASSET_NAME),+ _asset(CHECKSUMS_ASSET_NAME),+ dest_dir=tmp_path,+ fetcher=_Wire(routes).fetcher(),+ on_progress=lambda written, total: ticks.append((written, total)),+ )++ assert ticks[-1] == (len(PAYLOAD), len(PAYLOAD))+ assert {total for _written, total in ticks} == {len(PAYLOAD)}+++async def test_progress_without_any_known_size_stays_indeterminate(tmp_path: Path) -> None:+ async def chunked() -> AsyncIterator[bytes]:+ yield PAYLOAD++ routes = _github_release()+ routes[f"{CDN}/installer"] = lambda: httpx.Response(200, content=chunked())+ ticks: list[tuple[int, int | None]] = []++ await download_and_verify(+ _asset(ASSET_NAME, size=0),+ _asset(CHECKSUMS_ASSET_NAME),+ dest_dir=tmp_path,+ fetcher=_Wire(routes).fetcher(),+ on_progress=lambda written, total: ticks.append((written, total)),+ )++ # Never a guessed total: the UI shows bytes, not an invented percentage.+ assert {total for _written, total in ticks} == {None}+++async def test_the_fetcher_identifies_itself(tmp_path: Path) -> None:+ seen: list[httpx.Request] = []+ routes = _github_release()++ def handler(request: httpx.Request) -> httpx.Response:+ seen.append(request)+ factory = routes.get(str(request.url))+ return factory() if factory is not None else httpx.Response(404)++ await download_and_verify(+ _asset(ASSET_NAME),+ _asset(CHECKSUMS_ASSET_NAME),+ dest_dir=tmp_path,+ fetcher=HttpxAssetFetcher(transport=httpx.MockTransport(handler)),+ )++ assert seen+ assert all(request.headers["User-Agent"] for request in seen)+ assert all(request.url.scheme == "https" for request in seen)diff --git a/tests/unit/ui/web/test_update_routes_frozen.py b/tests/unit/ui/web/test_update_routes_frozen.pyindex 874a61e1c4..ff9634bc89 100644--- a/tests/unit/ui/web/test_update_routes_frozen.py+++ b/tests/unit/ui/web/test_update_routes_frozen.py@@ -33,6 +33,7 @@ def _reset_cache() -> None:u._status_cache_until = 0.0u._status_cache_root = Noneu._last_good_release = None+ u._downloads_swept = False@pytest.fixture@@ -78,6 +79,10 @@ def _patch_frozen(monkeypatch.setattr(u, "is_frozen", lambda: frozen)monkeypatch.setattr(u, "_running_version", lambda: running)monkeypatch.setattr(u, "installer_asset_name", lambda _p, _m: asset_name)+ # The real preflight inspects THIS process (no AppImage on a CI runner, no+ # app bundle in a test run); the cases below opt into a blocker explicitly.+ monkeypatch.setattr(u, "update_blocker", lambda: None)+ monkeypatch.setattr(u, "sweep_stale_downloads", lambda: 0)def _patch_latest(monkeypatch: pytest.MonkeyPatch, release: dict[str, Any] | None) -> None:@@ -397,9 +402,7 @@ def test_apply_refuses_before_downloading_while_missions_run(monkeypatch.setattr(u, "download_and_verify", _never)desktop = _FakeDesktop()- response = _client_with(missions=["m-1", "m-2"], desktop=desktop).post(- "/api/update/apply"- )+ response = _client_with(missions=["m-1", "m-2"], desktop=desktop).post("/api/update/apply")assert response.status_code == 409detail = response.json()["detail"]@@ -416,9 +419,7 @@ def test_apply_with_force_overrides_the_mission_guard(seen = _capture_install(monkeypatch)desktop = _FakeDesktop()- response = _client_with(missions=["m-1"], desktop=desktop).post(- "/api/update/apply?force=true"- )+ response = _client_with(missions=["m-1"], desktop=desktop).post("/api/update/apply?force=true")assert response.status_code == 200assert seen["installer"].name == SETUP_NAME@@ -527,3 +528,115 @@ def test_a_mission_started_during_the_download_still_stops_the_handover(assert desktop.quit_calls == 0progress = TestClient(app).get("/api/update/progress").json()assert "missions are running" in str(progress["error"])+++# --------------------------------------------------------------------------- #+# Preflight: an install that cannot replace itself is told so BEFORE download+# --------------------------------------------------------------------------- #+BLOCKED = "macOS is running Personal Jarvis from a temporary copy. Move it first."+++def test_status_announces_the_update_and_says_why_it_cannot_install(+ client: TestClient, monkeypatch: pytest.MonkeyPatch+) -> None:+ _patch_frozen(monkeypatch)+ _patch_latest(monkeypatch, _release("1.6.0"))+ monkeypatch.setattr(u, "update_blocker", lambda: BLOCKED)++ body = client.get("/api/update/status").json()++ # The user still learns that 1.6.0 exists...+ assert body["update_available"] is True+ # ...and the reason travels with it.+ assert body["blocked_reason"] == BLOCKED+++def test_status_does_not_run_the_preflight_without_an_update(+ client: TestClient, monkeypatch: pytest.MonkeyPatch+) -> None:+ _patch_frozen(monkeypatch, running="1.6.0")+ _patch_latest(monkeypatch, _release("1.6.0"))++ def _never() -> str:+ raise AssertionError("nothing to install, nothing to preflight")++ monkeypatch.setattr(u, "update_blocker", _never)+ body = client.get("/api/update/status").json()+ assert body["blocked_reason"] is None+++def test_apply_refuses_a_blocked_install_before_downloading(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ _patch_frozen(monkeypatch)+ _patch_latest(monkeypatch, _release("1.6.0"))+ monkeypatch.setattr(u, "update_blocker", lambda: BLOCKED)++ async def _never(*_args: Any, **_kwargs: Any) -> Path:+ raise AssertionError("a blocked install must not download several hundred MB")++ monkeypatch.setattr(u, "download_and_verify", _never)+ desktop = _FakeDesktop()+ client = _client_with(desktop=desktop)++ response = client.post("/api/update/apply")++ assert response.status_code == 409+ assert response.json()["detail"] == BLOCKED+ assert desktop.quit_calls == 0+ progress = client.get("/api/update/progress").json()+ assert progress["phase"] == "failed"+ assert progress["error"] == BLOCKED+++def test_status_reclaims_old_downloads_once_per_process(+ client: TestClient, monkeypatch: pytest.MonkeyPatch+) -> None:+ _patch_frozen(monkeypatch)+ _patch_latest(monkeypatch, _release("1.6.0"))+ sweeps: list[int] = []+ monkeypatch.setattr(u, "sweep_stale_downloads", lambda: sweeps.append(1) or 0)++ client.get("/api/update/status?force=true")+ client.get("/api/update/status?force=true")++ assert sweeps == [1]+++def test_a_failing_sweep_never_breaks_the_status_check(+ client: TestClient, monkeypatch: pytest.MonkeyPatch+) -> None:+ _patch_frozen(monkeypatch)+ _patch_latest(monkeypatch, _release("1.6.0"))++ def _explode() -> int:+ raise OSError("temp folder vanished")++ monkeypatch.setattr(u, "sweep_stale_downloads", _explode)+ body = client.get("/api/update/status").json()+ assert body["update_available"] is True+++def test_the_frozen_asset_follows_the_native_cpu(monkeypatch: pytest.MonkeyPatch) -> None:+ """An Intel build under Rosetta asks for the arm64 installer."""+ seen: list[tuple[str, str]] = []+ monkeypatch.setattr(u, "machine_for_update", lambda: "arm64")+ monkeypatch.setattr(+ u, "installer_asset_name", lambda platform, machine: seen.append((platform, machine))+ )+ u._frozen_asset_name()+ assert seen and seen[0][1] == "arm64"+++def test_apply_downloads_into_a_fresh_private_folder(+ client: TestClient, monkeypatch: pytest.MonkeyPatch, tmp_path: Path+) -> None:+ _patch_frozen(monkeypatch)+ _patch_latest(monkeypatch, _release("1.6.0"))+ seen = _capture_install(monkeypatch)+ workdir = tmp_path / "jarvis-update-test"+ workdir.mkdir()+ monkeypatch.setattr(u, "download_workdir", lambda: workdir)++ assert client.post("/api/update/apply").status_code == 200+ assert seen["installer"].parent == workdir