feat(update): integrate managed installs and recoverable in-app updates (#6168)

Add owner-aware Stable and Preview updates with explicit consent, verified release plans, protected process handoff and bounded service recovery. Keep configuration and conversations separate from program rollback.

Include the compact About controls, Windows long-path support, native package recovery checks and optional macOS Dock lifecycle integration. Preserve documented guidance for installations owned by other managers.

This code integration does not publish packages, enable signing or advance the public update channel.

Co-authored-by: chengyongru <61816729+chengyongru@users.noreply.github.com>
This commit is contained in:
Xubin Renandchengyongru authored and GitHub committed 2026-10-21 19:16:56 +08:00
1 parent 76da55994b
commit fbb61a373f
216 files changed
+27948 -1084

No files matched your search

+145 -46
View File
@@ -1,64 +1,163 @@
from __future__ import annotations
from unittest.mock import MagicMock
import hashlib
import sys
from pathlib import Path
from types import SimpleNamespace
import httpx
import pytest
from packaging.tags import Tag
import nanobot
import nanobot.webui.version_check as version_check
from nanobot.release_feed import CATALOGUE_URL, PYPI_URL, ReleaseFeed
from nanobot.releases import (
ReleaseArtifact,
ReleaseCatalogue,
ReleaseManifest,
ReleasePointer,
release_filenames,
)
from nanobot.update_channel import UpdateChannelRequest, read_update_channel
from nanobot.update_service import UpdateJobService
@pytest.fixture(autouse=True)
def _reset_version_cache(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(version_check, "_cache", (0.0, None))
monkeypatch.setattr(version_check.time, "monotonic", lambda: 1_000.0)
@pytest.fixture
def releases(tmp_path, monkeypatch):
monkeypatch.setattr(Path, "home", classmethod(lambda cls: tmp_path / "account"))
monkeypatch.setattr(sys, "prefix", str(tmp_path / "installation"))
monkeypatch.setattr(nanobot, "__version__", "0.3.6")
state = SimpleNamespace(stable="0.3.6", preview="0.3.7.dev42", generation=1,
withdrawn={}, partial=False, offline=False, incompatible=False, missing=False, requests=[])
def manifest(version):
value = ReleaseManifest(version=version, source_commit="a" * 40, candidate_commit="b" * 40,
requires_python=">=3.11", artifacts=[
ReleaseArtifact(filename=name, size=123, sha256="c" * 64) for name in sorted(release_filenames(version))])
payload = value.model_dump_json().encode()
return ReleasePointer(version=version, candidate_commit=value.candidate_commit,
manifest_sha256=hashlib.sha256(payload).hexdigest()), payload
def handler(request):
state.requests.append(str(request.url))
if state.offline:
raise httpx.ConnectError("private proxy credential", request=request)
if str(request.url) == CATALOGUE_URL:
if state.missing:
return httpx.Response(404)
catalogue = ReleaseCatalogue(generation=state.generation, stable=manifest(state.stable)[0],
preview=manifest(state.preview)[0], withdrawn=state.withdrawn)
return httpx.Response(200, content=catalogue.model_dump_json())
if str(request.url).startswith(PYPI_URL):
version = state.stable
files = [name for name in sorted(release_filenames(version)) if name.endswith((".whl", ".tar.gz"))]
return httpx.Response(200, json={
"info": {"name": "nanobot-ai", "version": version, "requires_python": ">=3.11"},
"urls": [{"filename": name, "size": 123, "yanked": False, "digests": {"sha256": "c" * 64},
"url": f"https://files.pythonhosted.org/packages/{name}"}
for name in (files[:-1] if state.partial else files)],
})
version = request.url.path.split("/v", 1)[1].split("/", 1)[0]
return httpx.Response(200, content=manifest(version)[1])
monkeypatch.setattr(version_check, "ReleaseFeed", lambda directory: ReleaseFeed(
directory, transport=httpx.MockTransport(handler), python_version="3.11.4",
supported_tags=[] if state.incompatible else [Tag("py3", "none", "win_amd64")],
))
return state
def _pypi_response(latest: object) -> MagicMock:
response = MagicMock()
response.json.return_value = {"info": {"version": latest}}
return response
def select(channel):
current = read_update_channel()
return UpdateJobService().change_channel(UpdateChannelRequest(channel=channel, revision=current.revision))
def test_version_check_reports_only_a_newer_release_and_caches_it(
monkeypatch: pytest.MonkeyPatch,
) -> None:
get = MagicMock(return_value=_pypi_response("1.3.0"))
monkeypatch.setattr(version_check, "__version__", "1.2.0")
monkeypatch.setattr(version_check.httpx, "get", get)
expected = {
"currentVersion": "1.2.0",
"latestVersion": "1.3.0",
"pypiUrl": "https://pypi.org/project/nanobot-ai/",
}
assert version_check.check_for_update() == expected
assert version_check.check_for_update() == expected
get.assert_called_once_with(
"https://pypi.org/pypi/nanobot-ai/json",
timeout=5.0,
follow_redirects=True,
)
def test_stable_preview_exit_wait_and_final_release(releases, monkeypatch):
assert version_check.check_for_update()["releaseStatus"]["state"] == "current"
assert CATALOGUE_URL not in releases.requests
select("preview")
result = version_check.check_for_update()
assert result["updateAvailable"]["latestVersion"] == "0.3.7.dev42"
monkeypatch.setattr(nanobot, "__version__", "0.3.7.dev42")
releases.preview, releases.generation = "0.3.7.dev43", 2
assert version_check.check_for_update()["updateAvailable"]["latestVersion"] == "0.3.7.dev43"
select("stable")
result = version_check.check_for_update()
assert result["releaseStatus"]["state"] == "waiting_stable"
assert result["updateAvailable"] is None
releases.stable, releases.generation = "0.3.7", 3
assert version_check.check_for_update()["updateAvailable"]["latestVersion"] == "0.3.7"
assert read_update_channel().channel == "stable"
@pytest.mark.parametrize("latest", ["1.2.0", "1.1.9", "not-a-version", 42, None])
def test_version_check_ignores_non_newer_or_invalid_releases(
monkeypatch: pytest.MonkeyPatch,
latest: object,
) -> None:
monkeypatch.setattr(version_check, "__version__", "1.2.0")
monkeypatch.setattr(version_check.httpx, "get", lambda *_args, **_kwargs: _pypi_response(latest))
assert version_check.check_for_update() is None
def test_preview_subscription_survives_final_release_and_next_cycle(releases, monkeypatch):
select("preview")
releases.stable = "0.3.7"
monkeypatch.setattr(nanobot, "__version__", "0.3.7.dev42")
assert version_check.check_for_update()["updateAvailable"]["latestVersion"] == "0.3.7"
monkeypatch.setattr(nanobot, "__version__", "0.3.7")
assert version_check.check_for_update()["releaseStatus"]["state"] == "current"
assert read_update_channel().channel == "preview"
releases.preview, releases.generation = "0.3.8.dev1", 2
assert version_check.check_for_update()["updateAvailable"]["latestVersion"] == "0.3.8.dev1"
def test_version_check_treats_network_failure_as_best_effort(
monkeypatch: pytest.MonkeyPatch,
) -> None:
get = MagicMock(side_effect=TimeoutError("offline"))
monkeypatch.setattr(version_check.httpx, "get", get)
def test_leaving_preview_can_find_a_newer_stable_when_catalogue_is_unavailable(releases, monkeypatch):
select("preview")
monkeypatch.setattr(nanobot, "__version__", "0.3.7.dev42")
select("stable")
releases.missing = True
releases.stable = "0.3.7"
result = version_check.check_for_update()
assert result["updateAvailable"]["latestVersion"] == "0.3.7"
assert result["releaseStatus"]["state"] == "available"
assert read_update_channel().channel == "stable"
# A broken Preview index cannot authorize a partial release or a downgrade,
# or turn unverified withdrawal status into a green 'up to date'.
releases.partial = True
assert version_check.check_for_update()["updateAvailable"] is None
releases.partial = False
releases.stable = "0.3.6"
result = version_check.check_for_update()
assert result["updateAvailable"] is None
assert result["releaseStatus"]["state"] == "unavailable"
assert version_check.check_for_update() is None
# Failures are not cached, so a later explicit check can recover.
assert version_check.check_for_update() is None
assert get.call_count == 2
@pytest.mark.parametrize("replacement", ["older", "newer", "incompatible"])
@pytest.mark.parametrize("channel", ["stable", "preview"])
def test_withdrawn_running_preview_is_not_reported_up_to_date(releases, monkeypatch, replacement, channel):
select(channel)
monkeypatch.setattr(nanobot, "__version__", "0.3.7.dev42")
releases.withdrawn = {"0.3.7.dev42": "Startup regression"}
if replacement != "older":
releases.preview = "0.3.7.dev43"
releases.stable = "0.3.7"
releases.incompatible = replacement == "incompatible"
result = version_check.check_for_update()
assert result["releaseStatus"]["state"] == "withdrawn"
assert (result["updateAvailable"] is not None) == (replacement == "newer")
@pytest.mark.parametrize("problem,state", [("partial", "publishing"), ("offline", "unavailable"),
("incompatible", "unsupported"), ("missing", "unavailable")])
def test_unavailable_checks_have_feedback_and_can_recover(releases, problem, state):
if problem == "missing":
select("preview")
setattr(releases, problem, True)
result = version_check.check_for_update()
assert result["updateAvailable"] is None
assert result["releaseStatus"]["state"] == state
assert "credential" not in str(result)
setattr(releases, problem, False)
assert version_check.check_for_update()["releaseStatus"]["state"] in {"current", "available"}
def test_current_dev_does_not_implicitly_opt_in_and_older_target_is_not_offered(releases, monkeypatch):
monkeypatch.setattr(nanobot, "__version__", "0.3.8.dev1")
assert read_update_channel().channel == "stable"
assert version_check.check_for_update()["releaseStatus"]["state"] == "waiting_stable"
select("preview")
result = version_check.check_for_update()
assert result["updateAvailable"] is None
assert result["releaseStatus"]["state"] == "ahead"