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:
1 parent
76da55994b
commit
fbb61a373f
216 files changed
+27948
-1084
No files matched your search
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from contextlib import nullcontext
|
||||
from pathlib import Path
|
||||
|
||||
from nanobot.webui.build import (
|
||||
@@ -100,7 +101,8 @@ def test_inspect_webui_bundle_accepts_fresh_dist(tmp_path: Path) -> None:
|
||||
assert status.reason == "fresh"
|
||||
|
||||
|
||||
def test_ensure_webui_bundle_auto_builds_stale_dist(tmp_path: Path) -> None:
|
||||
def test_ensure_webui_bundle_auto_builds_stale_dist(tmp_path: Path, monkeypatch) -> None:
|
||||
monkeypatch.setattr("nanobot.webui.build.bun_environment", lambda _: nullcontext({"PATH": "bun"}))
|
||||
source = tmp_path / "webui"
|
||||
dist = tmp_path / "nanobot" / "web" / "dist"
|
||||
_touch(source / "package.json", mtime_ns=10)
|
||||
@@ -108,11 +110,13 @@ def test_ensure_webui_bundle_auto_builds_stale_dist(tmp_path: Path) -> None:
|
||||
_touch(dist / "index.html", mtime_ns=20)
|
||||
commands: list[tuple[str, ...]] = []
|
||||
|
||||
def fake_run(command, *, cwd: Path, check: bool) -> None:
|
||||
def fake_run(command, *, cwd: Path, check: bool, env: dict[str, str], timeout: int) -> None:
|
||||
commands.append(tuple(command))
|
||||
assert cwd == source
|
||||
assert check is True
|
||||
if command == ["bun", "run", "build"]:
|
||||
assert "PATH" in env
|
||||
assert timeout == 900
|
||||
if command == ["bun", "--bun", "run", "build"]:
|
||||
_touch(dist / "index.html", mtime_ns=40)
|
||||
|
||||
status = ensure_webui_bundle(
|
||||
@@ -124,7 +128,7 @@ def test_ensure_webui_bundle_auto_builds_stale_dist(tmp_path: Path) -> None:
|
||||
)
|
||||
|
||||
assert status.stale is False
|
||||
assert commands == [("bun", "install"), ("bun", "run", "build")]
|
||||
assert commands == [("bun", "install", "--frozen-lockfile"), ("bun", "--bun", "run", "build")]
|
||||
|
||||
|
||||
def test_pick_webui_build_runner_returns_resolved_executable(monkeypatch) -> None:
|
||||
|
||||
@@ -5,6 +5,7 @@ import asyncio
|
||||
import pytest
|
||||
|
||||
from nanobot.config.schema import MCPServerConfig
|
||||
from nanobot.runtime_admission import RuntimeAdmission, RuntimeBusyError, RuntimeMaintenanceError
|
||||
from nanobot.webui.mcp_oauth_api import (
|
||||
McpOAuthError,
|
||||
McpOAuthManager,
|
||||
@@ -45,7 +46,7 @@ def _config() -> MCPServerConfig:
|
||||
async def test_browser_flow_retries_current_server_and_ignores_unrelated_reload_failure(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
manager = McpOAuthManager()
|
||||
manager = McpOAuthManager(RuntimeAdmission())
|
||||
connection = _Connection()
|
||||
received: dict[str, object] = {}
|
||||
reload_calls = 0
|
||||
@@ -116,6 +117,7 @@ async def test_browser_flow_retries_current_server_and_ignores_unrelated_reload_
|
||||
assert first["hot_reload"]["failed"] == ["notion"]
|
||||
assert received["callback"] == ("oauth-code", "state-123")
|
||||
assert reload_calls == 2
|
||||
await manager.close()
|
||||
assert connection.closed is True
|
||||
|
||||
|
||||
@@ -123,7 +125,8 @@ async def test_browser_flow_retries_current_server_and_ignores_unrelated_reload_
|
||||
async def test_browser_flow_activates_tools_before_probe_cleanup_completes(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
manager = McpOAuthManager()
|
||||
admission = RuntimeAdmission()
|
||||
manager = McpOAuthManager(admission)
|
||||
connection = _BlockingConnection()
|
||||
reload_started = asyncio.Event()
|
||||
|
||||
@@ -159,27 +162,92 @@ async def test_browser_flow_activates_tools_before_probe_cleanup_completes(
|
||||
)
|
||||
manager.submit_callback(state="blocked-close", code="oauth-code", error=None)
|
||||
|
||||
shutdown = None
|
||||
try:
|
||||
await asyncio.wait_for(reload_started.wait(), timeout=0.5)
|
||||
await asyncio.wait_for(connection.close_started.wait(), timeout=0.5)
|
||||
result = await manager.status(started["flow_id"])
|
||||
assert result["status"] == "connected"
|
||||
assert connection.closed is False
|
||||
with pytest.raises(RuntimeBusyError), admission.pause_if_idle(lambda: False):
|
||||
pytest.fail("MCP transport cleanup is still running")
|
||||
shutdown = asyncio.create_task(manager.close())
|
||||
await asyncio.wait({shutdown}, timeout=0.01)
|
||||
assert not shutdown.done(), "Shutdown must join the in-flight transport cleanup"
|
||||
finally:
|
||||
connection.allow_close.set()
|
||||
if shutdown is not None:
|
||||
await shutdown
|
||||
|
||||
for _ in range(10):
|
||||
await asyncio.sleep(0)
|
||||
if connection.closed:
|
||||
break
|
||||
assert connection.closed is True
|
||||
await manager.close()
|
||||
with admission.pause_if_idle(lambda: False):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.parametrize("expire", [False, True])
|
||||
@pytest.mark.asyncio
|
||||
async def test_browser_flow_remains_busy_through_cancelled_or_expired_cleanup(
|
||||
monkeypatch: pytest.MonkeyPatch, expire: bool,
|
||||
) -> None:
|
||||
admission = RuntimeAdmission()
|
||||
manager = McpOAuthManager(admission)
|
||||
cleanup_started = asyncio.Event()
|
||||
allow_cleanup = asyncio.Event()
|
||||
monkeypatch.setattr("nanobot.webui.mcp_oauth_api.validate_url_target", lambda _: (True, ""))
|
||||
if expire:
|
||||
monkeypatch.setattr("nanobot.webui.mcp_oauth_api._FLOW_TTL_S", 0)
|
||||
|
||||
async def connect(_servers, _registry, *, oauth_handlers):
|
||||
handlers = oauth_handlers["linear"]
|
||||
await handlers.redirect_handler("https://accounts.example.com/auth?state=pending")
|
||||
try:
|
||||
await handlers.callback_handler()
|
||||
finally:
|
||||
cleanup_started.set()
|
||||
await allow_cleanup.wait()
|
||||
return {}
|
||||
|
||||
monkeypatch.setattr("nanobot.webui.mcp_oauth_api.connect_mcp_servers", connect)
|
||||
started = await manager.start(
|
||||
"linear", _config(), "https://agent.example.com/auth/mcp/callback",
|
||||
reload_mcp=lambda: asyncio.sleep(0, result={"ok": True}),
|
||||
)
|
||||
with pytest.raises(RuntimeBusyError), admission.pause_if_idle(lambda: False):
|
||||
pytest.fail("The browser response does not finish authorization")
|
||||
if expire:
|
||||
with pytest.raises(McpOAuthError, match="expired"):
|
||||
await manager.status(started["flow_id"])
|
||||
await asyncio.wait_for(cleanup_started.wait(), timeout=0.5)
|
||||
|
||||
shutdown = asyncio.create_task(manager.close())
|
||||
try:
|
||||
await asyncio.wait_for(cleanup_started.wait(), timeout=0.5)
|
||||
await asyncio.wait({shutdown}, timeout=0.01)
|
||||
assert not shutdown.done()
|
||||
with pytest.raises(RuntimeBusyError), admission.pause_if_idle(lambda: False):
|
||||
pytest.fail("An expired or cancelled flow still owns its cleanup")
|
||||
finally:
|
||||
allow_cleanup.set()
|
||||
await shutdown
|
||||
|
||||
with admission.pause_if_idle(lambda: False):
|
||||
with pytest.raises(RuntimeMaintenanceError):
|
||||
await manager.start(
|
||||
"linear", _config(), "https://agent.example.com/auth/mcp/callback",
|
||||
reload_mcp=lambda: asyncio.sleep(0, result={"ok": True}),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_remote_http_flow_accepts_a_pasted_loopback_callback(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
manager = McpOAuthManager()
|
||||
manager = McpOAuthManager(RuntimeAdmission())
|
||||
connection = _Connection()
|
||||
received: dict[str, object] = {}
|
||||
|
||||
@@ -247,6 +315,7 @@ async def test_remote_http_flow_accepts_a_pasted_loopback_callback(
|
||||
assert result["status"] == "connected"
|
||||
assert result["completion_input"] == "callback_url"
|
||||
assert received["callback"] == ("oauth-code", "manual-state")
|
||||
await manager.close()
|
||||
assert connection.closed is True
|
||||
|
||||
|
||||
@@ -254,7 +323,7 @@ async def test_remote_http_flow_accepts_a_pasted_loopback_callback(
|
||||
async def test_browser_flow_surfaces_provider_denial_without_callback_description(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
manager = McpOAuthManager()
|
||||
manager = McpOAuthManager(RuntimeAdmission())
|
||||
monkeypatch.setattr(
|
||||
"nanobot.webui.mcp_oauth_api.validate_url_target",
|
||||
lambda _url: (True, ""),
|
||||
@@ -300,7 +369,7 @@ async def test_browser_flow_blocks_unsafe_authorization_url(
|
||||
url_is_safe: bool,
|
||||
state: str,
|
||||
) -> None:
|
||||
manager = McpOAuthManager()
|
||||
manager = McpOAuthManager(RuntimeAdmission())
|
||||
monkeypatch.setattr(
|
||||
"nanobot.webui.mcp_oauth_api.validate_url_target",
|
||||
lambda _url: (url_is_safe, "private address"),
|
||||
|
||||
@@ -1941,6 +1941,13 @@ def test_openai_codex_oauth_login_passes_configured_proxy(
|
||||
authorization_url = "https://auth.openai.com/oauth/authorize?state=test"
|
||||
remaining_seconds = 600
|
||||
expired = False
|
||||
has_pending_work = False
|
||||
|
||||
def start(self):
|
||||
pass
|
||||
|
||||
def wait_closed(self):
|
||||
pass
|
||||
|
||||
def cancel(self) -> None:
|
||||
captured["cancelled"] = True
|
||||
@@ -1954,7 +1961,7 @@ def test_openai_codex_oauth_login_passes_configured_proxy(
|
||||
return FakeFlow()
|
||||
|
||||
monkeypatch.setattr(
|
||||
"nanobot.providers.openai_codex_oauth.start_openai_codex_oauth_login",
|
||||
"nanobot.providers.openai_codex_oauth.OpenAICodexOAuthLoginFlow",
|
||||
fake_start,
|
||||
)
|
||||
|
||||
@@ -1981,10 +1988,7 @@ def test_openai_codex_oauth_login_passes_configured_proxy(
|
||||
return None
|
||||
return SimpleNamespace(access="access-token")
|
||||
|
||||
monkeypatch.setattr(
|
||||
"nanobot.providers.openai_codex_oauth.complete_openai_codex_oauth_login",
|
||||
fake_complete,
|
||||
)
|
||||
monkeypatch.setattr(FakeFlow, "complete", fake_complete, raising=False)
|
||||
monkeypatch.setattr(
|
||||
"nanobot.webui.settings_api.settings_payload",
|
||||
lambda **_kwargs: {"settings": "ready"},
|
||||
@@ -2026,6 +2030,13 @@ def test_openai_codex_remote_login_uses_headless_dependency_mode(
|
||||
authorization_url = "https://auth.openai.com/oauth/authorize?state=test"
|
||||
remaining_seconds = 600
|
||||
expired = False
|
||||
has_pending_work = False
|
||||
|
||||
def start(self):
|
||||
pass
|
||||
|
||||
def wait_closed(self):
|
||||
pass
|
||||
|
||||
def cancel(self) -> None:
|
||||
captured["cancelled"] = True
|
||||
@@ -2035,7 +2046,7 @@ def test_openai_codex_remote_login_uses_headless_dependency_mode(
|
||||
return FakeFlow()
|
||||
|
||||
monkeypatch.setattr(
|
||||
"nanobot.providers.openai_codex_oauth.start_openai_codex_oauth_login",
|
||||
"nanobot.providers.openai_codex_oauth.OpenAICodexOAuthLoginFlow",
|
||||
fake_start,
|
||||
)
|
||||
|
||||
@@ -2109,6 +2120,13 @@ def test_xai_grok_login_starts_fresh_browser_flow_with_proxy(
|
||||
authorization_url = "https://auth.x.ai/oauth2/authorize?state=test"
|
||||
remaining_seconds = 600
|
||||
expired = False
|
||||
has_pending_work = False
|
||||
|
||||
def start(self):
|
||||
pass
|
||||
|
||||
def wait_closed(self):
|
||||
pass
|
||||
|
||||
def cancel(self) -> None:
|
||||
captured["cancelled"] = True
|
||||
@@ -2117,7 +2135,7 @@ def test_xai_grok_login_starts_fresh_browser_flow_with_proxy(
|
||||
captured.update(proxy=proxy, timeout_s=timeout_s)
|
||||
return FakeFlow()
|
||||
|
||||
monkeypatch.setattr("nanobot.providers.xai_oauth.start_xai_oauth_login", fake_start)
|
||||
monkeypatch.setattr("nanobot.providers.xai_oauth.create_xai_oauth_login", fake_start)
|
||||
|
||||
payload = login_oauth_provider(
|
||||
{"provider": ["xai-grok"]},
|
||||
@@ -2140,10 +2158,7 @@ def test_xai_grok_login_starts_fresh_browser_flow_with_proxy(
|
||||
return None
|
||||
return SimpleNamespace(access="access-token")
|
||||
|
||||
monkeypatch.setattr(
|
||||
"nanobot.providers.xai_oauth.complete_xai_oauth_login",
|
||||
fake_complete,
|
||||
)
|
||||
monkeypatch.setattr(FakeFlow, "complete", fake_complete, raising=False)
|
||||
monkeypatch.setattr(
|
||||
"nanobot.webui.settings_api.settings_payload",
|
||||
lambda **_kwargs: {"settings": "ready"},
|
||||
@@ -2181,7 +2196,7 @@ def test_xai_grok_login_reports_upstream_failure_as_bad_gateway(
|
||||
def fake_start(**_kwargs):
|
||||
raise failure
|
||||
|
||||
monkeypatch.setattr("nanobot.providers.xai_oauth.start_xai_oauth_login", fake_start)
|
||||
monkeypatch.setattr("nanobot.providers.xai_oauth.create_xai_oauth_login", fake_start)
|
||||
|
||||
with pytest.raises(WebUISettingsError) as exc:
|
||||
login_oauth_provider(
|
||||
@@ -2391,7 +2406,7 @@ def test_copilot_explicit_login_does_not_reuse_revoked_credentials(monkeypatch,
|
||||
lambda: SimpleNamespace(access="revoked-token"),
|
||||
)
|
||||
monkeypatch.setattr(GitHubCopilotOAuthFlow, "start", start)
|
||||
monkeypatch.setattr(GitHubCopilotOAuthFlow, "complete", lambda _: SimpleNamespace(access="new-token"))
|
||||
monkeypatch.setattr(GitHubCopilotOAuthFlow, "complete", lambda *_: SimpleNamespace(access="new-token"))
|
||||
monkeypatch.setattr("nanobot.webui.settings_models.invalidate_oauth_model_catalog", invalidate)
|
||||
monkeypatch.setattr("nanobot.webui.settings_api.settings_payload", lambda **_: {"ready": True})
|
||||
payload = login_oauth_provider({"provider": ["github-copilot"]}, oauth_flows=oauth_flows)
|
||||
@@ -2429,17 +2444,18 @@ def test_oauth_completion_clears_same_account_catalog_failure(monkeypatch, oauth
|
||||
|
||||
class Flow:
|
||||
expired = False
|
||||
has_pending_work = False
|
||||
|
||||
def cancel(self):
|
||||
pass
|
||||
|
||||
def complete(*_args):
|
||||
signed_in[0] = True
|
||||
return SimpleNamespace(access="fixture")
|
||||
def wait_closed(self):
|
||||
pass
|
||||
|
||||
def complete(self, _response=None):
|
||||
signed_in[0] = True
|
||||
return SimpleNamespace(access="fixture")
|
||||
|
||||
owner = "openai_codex_oauth" if provider == "openai_codex" else "xai_oauth"
|
||||
completion = "complete_openai_codex_oauth_login" if provider == "openai_codex" else "complete_xai_oauth_login"
|
||||
monkeypatch.setattr(f"nanobot.providers.{owner}.{completion}", complete)
|
||||
oauth_flows.register(provider, "flow-test", Flow())
|
||||
before = provider_models_payload({"provider": [provider]})
|
||||
assert before["error_kind"] == "auth_required"
|
||||
|
||||
@@ -12,7 +12,10 @@ from urllib.parse import parse_qs, urlsplit
|
||||
import pytest
|
||||
from websockets.datastructures import Headers
|
||||
|
||||
from nanobot.bus.queue import MessageBus
|
||||
from nanobot.config.loader import get_config_path
|
||||
from nanobot.config.schema import MCPServerConfig
|
||||
from nanobot.runtime_admission import RuntimeBusyError
|
||||
from nanobot.webui.http_utils import http_json_response
|
||||
from nanobot.webui.mcp_presets_api import custom_mcp_action
|
||||
from nanobot.webui.settings_routes import WebUISettingsRouter
|
||||
@@ -35,8 +38,8 @@ def _router(
|
||||
rename_model_preset=rename_model_preset,
|
||||
refresh_runtime_config=refresh_runtime_config,
|
||||
),
|
||||
bus=SimpleNamespace(),
|
||||
logger=SimpleNamespace(exception=lambda *_args: None),
|
||||
bus=MessageBus(),
|
||||
logger=SimpleNamespace(exception=lambda *_args: None, warning=lambda *_args: None),
|
||||
check_api_token=lambda _request: authorized,
|
||||
parse_query=lambda path: parse_qs(urlsplit(path).query),
|
||||
json_response=http_json_response,
|
||||
@@ -61,12 +64,37 @@ def _mutation_request(path: str, payload: dict[str, object]) -> SimpleNamespace:
|
||||
return request
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_nanobot_update_requires_auth_local_permission_and_websocket(tmp_path, monkeypatch):
|
||||
router = _router(config_path=tmp_path / "config.json")
|
||||
path = "/api/settings/nanobot-update/job/prepare"
|
||||
request = SimpleNamespace(path=path, headers=Headers())
|
||||
response = await router.dispatch(None, request, path)
|
||||
assert response.status_code == 405
|
||||
response = await router.dispatch(None, _mutation_request(path, {}), path)
|
||||
assert response.status_code == 403
|
||||
unauthorized = _router(authorized=False, config_path=tmp_path / "config.json")
|
||||
response = await unauthorized.dispatch(None, _mutation_request(path, {}), path)
|
||||
assert response.status_code == 401
|
||||
monkeypatch.setattr(router._system, "allow_feature_package_install", lambda _: True)
|
||||
response = await router.dispatch(None, _mutation_request(path, {"id": "invalid"}), path)
|
||||
assert response.status_code == 400
|
||||
router._runtime_surface = "native"
|
||||
response = await router.dispatch(None, _mutation_request(path, {}), path)
|
||||
assert response.status_code == 409
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_close_releases_channel_connectors() -> None:
|
||||
router = _router()
|
||||
closed = False
|
||||
|
||||
class Connector:
|
||||
has_pending_work = False
|
||||
|
||||
async def handle(self, action, query):
|
||||
return {}
|
||||
|
||||
async def close(self) -> None:
|
||||
nonlocal closed
|
||||
closed = True
|
||||
@@ -79,6 +107,162 @@ async def test_close_releases_channel_connectors() -> None:
|
||||
assert router._system._channel_connectors == {}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("finish", ["cancel", "logout", "shutdown"])
|
||||
async def test_provider_login_keeps_maintenance_busy_until_worker_exits(tmp_path, monkeypatch, finish):
|
||||
router = _router(config_path=tmp_path / "config.json")
|
||||
stopping, release = threading.Event(), threading.Event()
|
||||
saved = []
|
||||
|
||||
def login(*, on_authorization, cancelled, persist, **_kwargs):
|
||||
assert persist is False
|
||||
on_authorization("https://github.com/login/device", "FIXTURE", 60)
|
||||
assert cancelled.wait(3)
|
||||
stopping.set()
|
||||
assert release.wait(3)
|
||||
return SimpleNamespace(access="late-fixture-token")
|
||||
|
||||
monkeypatch.setattr("nanobot.providers.github_copilot_oauth.login_github_copilot", login)
|
||||
monkeypatch.setattr("nanobot.providers.github_copilot_provider.get_storage", lambda: SimpleNamespace(
|
||||
get_token_path=lambda: tmp_path / "copilot.json", save=saved.append,
|
||||
))
|
||||
monkeypatch.setattr("nanobot.webui.settings_api.settings_payload", lambda **_: {})
|
||||
path = "/api/settings/provider/oauth-login"
|
||||
response = await router.dispatch(None, _mutation_request(path, {"provider": "github_copilot"}), path)
|
||||
assert response.status_code == 200
|
||||
payload = json.loads(response.body)
|
||||
with pytest.raises(RuntimeBusyError), router.bus.admission.pause_if_idle(lambda: False):
|
||||
pytest.fail("The authorization prompt still has a live worker")
|
||||
try:
|
||||
if finish == "shutdown":
|
||||
shutdown = asyncio.create_task(router.close())
|
||||
assert await asyncio.to_thread(stopping.wait, 1)
|
||||
shutdown.cancel()
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await shutdown
|
||||
else:
|
||||
path = (
|
||||
"/api/settings/provider/oauth-logout" if finish == "logout"
|
||||
else "/api/settings/provider/oauth-login/complete"
|
||||
)
|
||||
response = await router.dispatch(None, _mutation_request(path, {
|
||||
"provider": "github_copilot", "flow_id": payload["flow_id"], "cancel": "true",
|
||||
}), path)
|
||||
assert response.status_code == 200
|
||||
assert await asyncio.to_thread(stopping.wait, 1)
|
||||
assert router.settings.oauth_flows.get("github_copilot", payload["flow_id"]) is None
|
||||
with pytest.raises(RuntimeBusyError), router.bus.admission.pause_if_idle(lambda: False):
|
||||
pytest.fail("Removing a login or cancelling its close awaiter cannot forget the thread")
|
||||
finally:
|
||||
release.set()
|
||||
await router.close()
|
||||
assert saved == []
|
||||
assert not router.settings.oauth_flows.has_pending_work
|
||||
with router.bus.admission.pause_if_idle(lambda: False):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_failed_codex_login_start_still_owns_late_worker(tmp_path, monkeypatch):
|
||||
import nanobot.providers.openai_codex_oauth as oauth
|
||||
|
||||
router = _router(config_path=tmp_path / "config.json")
|
||||
entered, release = threading.Event(), threading.Event()
|
||||
target = tmp_path / "codex.json"
|
||||
monkeypatch.setenv("OAUTH_CLI_KIT_TOKEN_PATH", str(target))
|
||||
monkeypatch.setattr(oauth, "_AUTHORIZATION_URL_TIMEOUT_S", 0.02)
|
||||
|
||||
def login(*, storage, **_kwargs):
|
||||
entered.set()
|
||||
assert release.wait(3)
|
||||
# The actual SDK can finish its token exchange after start() timed out.
|
||||
storage.save(SimpleNamespace(access="late-fixture-token"))
|
||||
|
||||
monkeypatch.setattr(oauth, "login_oauth_interactive", login)
|
||||
path = "/api/settings/provider/oauth-login"
|
||||
try:
|
||||
response = await router.dispatch(None, _mutation_request(path, {
|
||||
"provider": "openai_codex", "remote_browser": "true",
|
||||
}), path)
|
||||
assert response.status_code == 502
|
||||
assert entered.is_set()
|
||||
with pytest.raises(RuntimeBusyError), router.bus.admission.pause_if_idle(lambda: False):
|
||||
pytest.fail("A failed start must retain its background worker")
|
||||
finally:
|
||||
release.set()
|
||||
await router.close()
|
||||
assert not target.exists()
|
||||
with router.bus.admission.pause_if_idle(lambda: False):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("close_fails", [False, True])
|
||||
async def test_channel_setup_blocks_maintenance_until_confirmed_cleanup(
|
||||
tmp_path, monkeypatch, close_fails,
|
||||
) -> None:
|
||||
router = _router(config_path=tmp_path / "config.json")
|
||||
cleanup_started, allow_cleanup = asyncio.Event(), asyncio.Event()
|
||||
|
||||
class Connector:
|
||||
has_pending_work = True
|
||||
|
||||
async def handle(self, action, query):
|
||||
return {"status": "pending", "session_id": "test"}
|
||||
|
||||
async def close(self):
|
||||
cleanup_started.set()
|
||||
await allow_cleanup.wait()
|
||||
if close_fails:
|
||||
raise RuntimeError("native writer has not stopped")
|
||||
self.has_pending_work = False
|
||||
|
||||
connector = Connector()
|
||||
monkeypatch.setattr(
|
||||
"nanobot.webui.settings_routes.load_channel_plugin",
|
||||
lambda _: SimpleNamespace(load_connector=lambda: connector),
|
||||
)
|
||||
path = "/api/settings/channels/whatsapp/connect/start"
|
||||
response = await router.dispatch(None, _mutation_request(path, {}), path)
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.body)["status"] == "pending"
|
||||
with pytest.raises(RuntimeBusyError), router.bus.admission.pause_if_idle(lambda: False):
|
||||
pytest.fail("Returning a QR code does not finish its connection")
|
||||
shutdown = asyncio.create_task(router.close())
|
||||
try:
|
||||
await asyncio.wait_for(cleanup_started.wait(), timeout=0.5)
|
||||
with pytest.raises(RuntimeBusyError), router.bus.admission.pause_if_idle(lambda: False):
|
||||
pytest.fail("Cleanup still owns its pending connection")
|
||||
finally:
|
||||
allow_cleanup.set()
|
||||
await shutdown
|
||||
if close_fails:
|
||||
assert router._system._channel_connectors["whatsapp"] is connector
|
||||
with pytest.raises(RuntimeBusyError), router.bus.admission.pause_if_idle(lambda: False):
|
||||
pytest.fail("A failed close must not release ownership")
|
||||
close_fails = False
|
||||
await router.close()
|
||||
assert router._system._channel_connectors == {}
|
||||
with router.bus.admission.pause_if_idle(lambda: False):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_legacy_connector_can_connect_but_cannot_claim_maintenance_idle(tmp_path, monkeypatch):
|
||||
router = _router(config_path=tmp_path / "config.json")
|
||||
connector = SimpleNamespace(handle=AsyncMock(return_value={"status": "pending"}))
|
||||
monkeypatch.setattr(
|
||||
"nanobot.webui.settings_routes.load_channel_plugin",
|
||||
lambda _: SimpleNamespace(load_connector=lambda: connector),
|
||||
)
|
||||
path = "/api/settings/channels/custom/connect/start"
|
||||
response = await router.dispatch(None, _mutation_request(path, {}), path)
|
||||
assert response.status_code == 200
|
||||
await router.close()
|
||||
with pytest.raises(RuntimeBusyError), router.bus.admission.pause_if_idle(lambda: False):
|
||||
pytest.fail("An untracked extension must not be assumed idle")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_mcp_list_serializes_local_runtime_failure_snapshot(tmp_path) -> None:
|
||||
config_path = tmp_path / "config.json"
|
||||
@@ -222,6 +406,50 @@ async def test_mcp_reload_callback_is_bounded(
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_settings_shutdown_joins_browser_authorization_and_releases_admission(
|
||||
tmp_path, monkeypatch,
|
||||
) -> None:
|
||||
config = MCPServerConfig(type="streamableHttp", auth="oauth", url="https://mcp.example/mcp")
|
||||
monkeypatch.setattr(
|
||||
"nanobot.webui.settings_routes.ensure_mcp_oauth_server",
|
||||
lambda _query, *, config_path=None: ("linear", config),
|
||||
)
|
||||
monkeypatch.setattr("nanobot.webui.mcp_oauth_api.validate_url_target", lambda _: (True, ""))
|
||||
cleanup_started, allow_cleanup = asyncio.Event(), asyncio.Event()
|
||||
|
||||
async def connect(_servers, _registry, *, oauth_handlers):
|
||||
handlers = oauth_handlers["linear"]
|
||||
await handlers.redirect_handler("https://accounts.example/authorize?state=settings")
|
||||
try:
|
||||
await handlers.callback_handler()
|
||||
finally:
|
||||
cleanup_started.set()
|
||||
await allow_cleanup.wait()
|
||||
return {}
|
||||
|
||||
monkeypatch.setattr("nanobot.webui.mcp_oauth_api.connect_mcp_servers", connect)
|
||||
router = _router(config_path=tmp_path / "config.json")
|
||||
path = "/api/settings/mcp-oauth/start"
|
||||
response = await router.dispatch(None, _mutation_request(path, {"name": "linear"}), path)
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.body)["status"] == "authorization_required"
|
||||
with pytest.raises(RuntimeBusyError), router.bus.admission.pause_if_idle(lambda: False):
|
||||
pytest.fail("Settings returned, but authorization has not finished")
|
||||
|
||||
shutdown = asyncio.create_task(router.close())
|
||||
try:
|
||||
await asyncio.wait_for(cleanup_started.wait(), timeout=0.5)
|
||||
assert not shutdown.done()
|
||||
with pytest.raises(RuntimeBusyError), router.bus.admission.pause_if_idle(lambda: False):
|
||||
pytest.fail("Settings shutdown must retain ownership through cleanup")
|
||||
finally:
|
||||
allow_cleanup.set()
|
||||
await shutdown
|
||||
with router.bus.admission.pause_if_idle(lambda: False):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_mcp_oauth_start_uses_gateway_callback_and_requires_api_auth(monkeypatch) -> None:
|
||||
config = SimpleNamespace(
|
||||
@@ -554,7 +782,7 @@ async def test_version_check_route_returns_stable_payload(
|
||||
) -> None:
|
||||
monkeypatch.setattr(
|
||||
"nanobot.webui.settings_routes.check_for_update",
|
||||
lambda: update_info,
|
||||
lambda: {"updateAvailable": update_info, "releaseStatus": {"state": "available" if update_info else "current"}},
|
||||
)
|
||||
request = SimpleNamespace(path="/api/settings/version-check", headers=Headers())
|
||||
|
||||
@@ -585,6 +813,45 @@ async def test_version_check_route_enforces_auth_and_bounds_failures(
|
||||
assert "upstream secret body" not in failed.body.decode()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("state", ["waiting_stable", "withdrawn", "unavailable", "publishing", "unsupported"])
|
||||
async def test_channel_status_is_explicit_and_old_clients_never_report_false_success(monkeypatch, state):
|
||||
result = {"updateAvailable": None, "releaseStatus": {
|
||||
"channel": "stable", "revision": 2, "currentVersion": "0.3.7.dev42", "state": state,
|
||||
}}
|
||||
monkeypatch.setattr("nanobot.webui.settings_routes.check_for_update", lambda: result)
|
||||
request = SimpleNamespace(path="/api/settings/version-check", headers=Headers())
|
||||
router = _router()
|
||||
response = await router.dispatch(None, request, request.path)
|
||||
assert response.status_code == 409
|
||||
request.path += "?channel_status=1"
|
||||
response = await router.dispatch(None, request, "/api/settings/version-check")
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.body)["releaseStatus"] == result["releaseStatus"]
|
||||
assert response.headers["Cache-Control"] == "no-store"
|
||||
|
||||
|
||||
async def test_version_check_owner_is_additive_read_only_and_failure_is_not_a_release_failure(monkeypatch):
|
||||
from nanobot.webui import settings_system
|
||||
|
||||
result = {"updateAvailable": None, "releaseStatus": {"state": "current"}}
|
||||
monkeypatch.setattr("nanobot.webui.settings_routes.check_for_update", lambda: result)
|
||||
inspect_owner = MagicMock(return_value=SimpleNamespace(owner="pipx", records={}))
|
||||
monkeypatch.setattr(settings_system, "inspect_update_installation", inspect_owner)
|
||||
request = SimpleNamespace(path="/api/settings/version-check", headers=Headers())
|
||||
router = _router()
|
||||
legacy = await router.dispatch(None, request, "/api/settings/version-check")
|
||||
assert json.loads(legacy.body) == {"updateAvailable": None}
|
||||
inspect_owner.assert_not_called()
|
||||
request.path += "?channel_status=1"
|
||||
modern = await router.dispatch(None, request, "/api/settings/version-check")
|
||||
assert json.loads(modern.body)["installation_method"] == "pipx"
|
||||
inspect_owner.side_effect = ValueError("private receipt secret")
|
||||
unavailable = await router.dispatch(None, request, "/api/settings/version-check")
|
||||
body = json.loads(unavailable.body)
|
||||
assert body["installation_method"] is None and body["releaseStatus"] == result["releaseStatus"]
|
||||
assert "secret" not in unavailable.body.decode()
|
||||
|
||||
|
||||
async def test_runtime_config_route_authentication_validation_and_restart(tmp_path):
|
||||
from nanobot.config.loader import load_config
|
||||
|
||||
|
||||
@@ -19,6 +19,8 @@ from nanobot.webui.settings_services import (
|
||||
|
||||
|
||||
class _Flow:
|
||||
has_pending_work = False
|
||||
|
||||
def __init__(self, *, expired: bool = False) -> None:
|
||||
self.expired = expired
|
||||
self.cancel_count = 0
|
||||
@@ -26,6 +28,12 @@ class _Flow:
|
||||
def cancel(self) -> None:
|
||||
self.cancel_count += 1
|
||||
|
||||
def wait_closed(self) -> None:
|
||||
pass
|
||||
|
||||
def complete(self, response=None):
|
||||
return None
|
||||
|
||||
|
||||
def _gateway(config_path: Path, workspace: Path):
|
||||
return build_gateway_services(
|
||||
@@ -243,3 +251,87 @@ def test_oauth_registry_preserves_expiry_capacity_completion_and_cancel() -> Non
|
||||
registry.clear("openai_codex")
|
||||
assert replacement.cancel_count == 1
|
||||
assert registry.get("openai_codex", "replacement") is None
|
||||
|
||||
|
||||
def test_oauth_registry_capacity_counts_cancelled_workers_until_exit():
|
||||
release = threading.Event()
|
||||
cancelled = threading.Event()
|
||||
|
||||
class ThreadFlow(_Flow):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.thread = threading.Thread(target=lambda: release.wait(3))
|
||||
|
||||
@property
|
||||
def has_pending_work(self):
|
||||
return self.thread.is_alive()
|
||||
|
||||
def cancel(self):
|
||||
cancelled.set()
|
||||
|
||||
def wait_closed(self):
|
||||
self.thread.join()
|
||||
|
||||
registry = WebUIOAuthFlowRegistry(max_flows=1)
|
||||
flow = ThreadFlow()
|
||||
registry.register("github_copilot", "first", flow)
|
||||
flow.thread.start()
|
||||
replacement = _Flow()
|
||||
try:
|
||||
with pytest.raises(RuntimeError, match="still stopping"):
|
||||
registry.register("github_copilot", "next", replacement)
|
||||
assert cancelled.is_set()
|
||||
assert registry.get("github_copilot", "first") is None
|
||||
assert registry.get("github_copilot", "next") is None
|
||||
assert registry.has_pending_work
|
||||
finally:
|
||||
release.set()
|
||||
flow.wait_closed()
|
||||
assert not registry.has_pending_work
|
||||
registry.register("github_copilot", "next", replacement)
|
||||
registry.close()
|
||||
with pytest.raises(RuntimeError, match="closing"):
|
||||
registry.register("github_copilot", "after-close", _Flow())
|
||||
|
||||
|
||||
def test_oauth_registry_keeps_flow_that_starts_while_cancellation_is_pending():
|
||||
cancelling, allow_cancel, release_worker = (threading.Event() for _ in range(3))
|
||||
|
||||
class StartingFlow(_Flow):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.thread = threading.Thread(target=lambda: release_worker.wait(3))
|
||||
|
||||
@property
|
||||
def has_pending_work(self):
|
||||
return self.thread.is_alive()
|
||||
|
||||
def cancel(self):
|
||||
cancelling.set()
|
||||
assert allow_cancel.wait(3)
|
||||
|
||||
def wait_closed(self):
|
||||
if self.thread.ident is not None:
|
||||
self.thread.join()
|
||||
|
||||
registry = WebUIOAuthFlowRegistry(max_flows=2)
|
||||
flow = StartingFlow()
|
||||
registry.register("github_copilot", "starting", flow)
|
||||
removal = threading.Thread(target=lambda: registry.remove("github_copilot", "starting", flow))
|
||||
removal.start()
|
||||
try:
|
||||
assert cancelling.wait(1)
|
||||
# Another request can register between register/start in the first login,
|
||||
# while its cancellation is waiting for the provider's commit lock.
|
||||
registry.register("xai_grok", "another", _Flow())
|
||||
flow.thread.start()
|
||||
allow_cancel.set()
|
||||
removal.join(timeout=1)
|
||||
assert not removal.is_alive()
|
||||
assert registry.has_pending_work
|
||||
finally:
|
||||
allow_cancel.set()
|
||||
release_worker.set()
|
||||
removal.join(timeout=2)
|
||||
flow.wait_closed()
|
||||
registry.close()
|
||||
@@ -0,0 +1,925 @@
|
||||
"""Authenticated, refresh-safe review and job reads; no installer or server process runs."""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import platform
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from uuid import uuid4
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from packaging.tags import Tag
|
||||
from websockets.datastructures import Headers
|
||||
from websockets.http11 import Request
|
||||
|
||||
import nanobot
|
||||
from nanobot import update_plan, update_service
|
||||
from nanobot.bus.queue import MessageBus
|
||||
from nanobot.channels.websocket.runtime import WebSocketConfig
|
||||
from nanobot.config.home import get_selected_home_path, set_home_path
|
||||
from nanobot.installation_lock import (
|
||||
InstallationBusyError,
|
||||
file_access,
|
||||
installation_access,
|
||||
installation_state_dir,
|
||||
pending_update_path,
|
||||
)
|
||||
from nanobot.release_feed import ReleaseFeed
|
||||
from nanobot.releases import release_filenames
|
||||
from nanobot.runtime_admission import RuntimeBusyError
|
||||
from nanobot.update_manager import ManagerPolicy
|
||||
from nanobot.update_plan import InstallationSnapshot, UpdatePlans
|
||||
from nanobot.update_process import PreparationProcess
|
||||
from nanobot.update_profiles import ObservedProfile, ProfileLaunch, ProfileReceipt
|
||||
from nanobot.update_worker import ExecutionConfirmation, JobStatus, Phase
|
||||
from nanobot.webui.client_contract import webui_contract
|
||||
from nanobot.webui.gateway_services import build_gateway_services
|
||||
|
||||
CREATE = "/api/settings/nanobot-update/plan/create"
|
||||
READ = "/api/settings/nanobot-update/plan"
|
||||
JOB = "/api/settings/nanobot-update/job"
|
||||
PREPARE = "settings.nanobot.update.prepare"
|
||||
SETTLE = "settings.nanobot.update.settle"
|
||||
EXECUTE = "settings.nanobot.update.execute"
|
||||
RETRY = "settings.nanobot.update.retry"
|
||||
CHANNEL = "settings.nanobot.update.channel"
|
||||
|
||||
|
||||
async def test_channel_mutation_preserves_authority_and_invalidates_old_reviews(preparation_gateway):
|
||||
from nanobot.update_channel import read_update_channel
|
||||
|
||||
fixture = preparation_gateway
|
||||
gateway, plan = await preparation_plan(fixture)
|
||||
# Plain HTTP cannot mutate; remote callers do not acquire install authority.
|
||||
response = await fetch(gateway, "/api/settings/nanobot-update/channel")
|
||||
assert response.status_code == 405
|
||||
response = await gateway.http.dispatch_webui_mutation(connection(), CHANNEL, {"channel": "preview", "revision": 0})
|
||||
assert response.status_code == 403
|
||||
assert read_update_channel().revision == 0
|
||||
response = await gateway.http.dispatch_webui_mutation(connection(local=True), CHANNEL, {"channel": "preview", "revision": 0})
|
||||
assert response.status_code == 200, response.body
|
||||
assert json.loads(response.body) == {"channel": "preview", "revision": 1}
|
||||
assert "webui.update-channels.v1" in webui_contract()["capabilities"]
|
||||
reviewed = await fetch(gateway, READ + "?id=" + plan.id)
|
||||
assert json.loads(reviewed.body)["plan"]["validity"] == "channel_changed"
|
||||
prepared = await gateway.http.dispatch_webui_mutation(connection(local=True), PREPARE, {"id": plan.id})
|
||||
assert prepared.status_code == 409
|
||||
assert not (fixture.directory / "jobs" / plan.id / "status.json").exists()
|
||||
# Stale tabs cannot overwrite the newer selection; native uses its distributor.
|
||||
assert (await gateway.http.dispatch_webui_mutation(connection(local=True), CHANNEL,
|
||||
{"channel": "stable", "revision": 0})).status_code == 409
|
||||
assert (await fixture.gateway("native").http.dispatch_webui_mutation(connection(local=True), CHANNEL,
|
||||
{"channel": "stable", "revision": 1})).status_code == 409
|
||||
|
||||
|
||||
@pytest.mark.parametrize("payload", [{"channel": "preview"}, {"channel": "beta", "revision": 0},
|
||||
{"channel": "preview", "revision": True},
|
||||
{"channel": "preview", "revision": 0, "url": "https://other.example"}])
|
||||
async def test_channel_mutation_validates_untrusted_requests(preparation_gateway, payload):
|
||||
gateway = preparation_gateway.gateway()
|
||||
response = await gateway.http.dispatch_webui_mutation(connection(local=True), CHANNEL, payload)
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
async def test_review_uses_saved_channel_and_rejects_a_change_during_discovery(preparation_gateway, monkeypatch):
|
||||
from nanobot.update_channel import UpdateChannelRequest
|
||||
|
||||
fixture = preparation_gateway
|
||||
gateway, stable = await preparation_plan(fixture)
|
||||
service = update_service.UpdateJobService()
|
||||
service.change_channel(UpdateChannelRequest(channel="preview", revision=0))
|
||||
observed = []
|
||||
race = False
|
||||
|
||||
class Feed:
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, *_):
|
||||
pass
|
||||
|
||||
async def latest(self, channel):
|
||||
observed.append(channel)
|
||||
if race:
|
||||
service.change_channel(UpdateChannelRequest(channel="stable", revision=1))
|
||||
# Preview subscriptions can receive a final release too.
|
||||
return stable.target
|
||||
|
||||
monkeypatch.setattr(update_plan, "ReleaseFeed", lambda _: Feed())
|
||||
response = await gateway.http.dispatch_webui_mutation(connection(local=True), "settings.nanobot.update.plan", {"extras": []})
|
||||
assert response.status_code == 200
|
||||
saved = UpdatePlans(fixture.directory / "plans").load(json.loads(response.body)["plan"]["id"])
|
||||
assert (saved.channel, saved.channel_revision) == ("preview", 1)
|
||||
race = True
|
||||
response = await gateway.http.dispatch_webui_mutation(connection(local=True), "settings.nanobot.update.plan", {"extras": []})
|
||||
assert response.status_code == 409
|
||||
assert observed == ["preview", "preview"]
|
||||
assert not (fixture.directory / "jobs").exists()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def review_gateway(tmp_path, monkeypatch):
|
||||
account = tmp_path / "private account"
|
||||
monkeypatch.setattr(Path, "home", classmethod(lambda cls: account))
|
||||
previous_home = get_selected_home_path()
|
||||
set_home_path(tmp_path / "profile")
|
||||
environment = tmp_path / "tool environment"
|
||||
package = str(environment / "lib/nanobot/__init__.py")
|
||||
snapshot = InstallationSnapshot(
|
||||
environment=str(environment), python=str(environment / "bin/python"),
|
||||
package_path=package, running_package=package, installed_version="0.3.4",
|
||||
running_version="0.3.4", owner="uv", manager_version="0.11.16", records={"uv-receipt.toml": "a" * 64},
|
||||
manager=ManagerPolicy(manager="uv", backend="uv", source="registry", extras=["webui"]),
|
||||
)
|
||||
observed = []
|
||||
for number, state in enumerate(("running", "stopped")):
|
||||
root = tmp_path / f"profile-{number}"
|
||||
receipt = ProfileReceipt(
|
||||
id=str(number + 1) * 64, run_id=uuid4().hex, environment=str(environment),
|
||||
python=snapshot.python, package=package, version="0.3.4", home=str(root),
|
||||
data_dir=str(root), config=str(root / "config.json"), workspace=str(root / "project"),
|
||||
state_path=str(root / "run/gateway.json"), log_path=str(root / "log/gateway.log"),
|
||||
port=19000 + number, launch_mode="background", pid=os.getpid() + 10000 + number,
|
||||
process_identity=f"fixture-{number}", active=state == "running", stop_protocol=1,
|
||||
launch=ProfileLaunch(home=str(root), config_path=None, workspace=None,
|
||||
working_directory=str(root), verbose=False, lifetime="explicit"),
|
||||
)
|
||||
observed.append(ObservedProfile(receipt=receipt, state=state))
|
||||
monkeypatch.setattr(update_plan, "inspect_installation", lambda: snapshot)
|
||||
monkeypatch.setattr(update_plan, "inspect_profiles", lambda _: list(observed))
|
||||
requests = []
|
||||
|
||||
def release(request):
|
||||
requests.append(str(request.url))
|
||||
assert request.url.path == "/pypi/nanobot-ai/json"
|
||||
return httpx.Response(200, json={
|
||||
"info": {"name": "nanobot-ai", "version": "0.3.5", "requires_python": ">=3.11"},
|
||||
"urls": [{"filename": name, "size": 123, "digests": {"sha256": "b" * 64},
|
||||
"url": f"https://files.pythonhosted.org/packages/{name}", "yanked": False}
|
||||
for name in release_filenames("0.3.5") if name.endswith((".whl", ".tar.gz"))],
|
||||
})
|
||||
|
||||
monkeypatch.setattr(update_plan, "ReleaseFeed", lambda directory: ReleaseFeed(
|
||||
directory, transport=httpx.MockTransport(release), supported_tags=[Tag("py3", "none", "win_amd64")],
|
||||
))
|
||||
def gateway(surface="gateway"):
|
||||
return build_gateway_services(
|
||||
config=WebSocketConfig(), bus=MessageBus(), session_manager=None,
|
||||
static_dist_path=None, workspace_path=tmp_path / "workspace",
|
||||
default_restrict_to_workspace=True, config_path=tmp_path / "config.json",
|
||||
runtime_model_name=None, runtime_surface=surface, runtime_capabilities_overrides=None,
|
||||
)
|
||||
|
||||
try:
|
||||
yield SimpleNamespace(gateway=gateway, snapshot=snapshot, observed=observed,
|
||||
requests=requests, directory=installation_state_dir(environment))
|
||||
finally:
|
||||
set_home_path(previous_home)
|
||||
|
||||
|
||||
def connection(*, local=False):
|
||||
host = "127.0.0.1:8765" if local else "remote.example"
|
||||
return SimpleNamespace(remote_address=("127.0.0.1" if local else "192.0.2.1", 12345),
|
||||
request=Request("/ws", Headers({"Host": host, "Origin": f"http://{host}"})))
|
||||
|
||||
|
||||
async def fetch(gateway, path, *, authorized=True):
|
||||
headers = Headers()
|
||||
if authorized:
|
||||
headers["Authorization"] = f"Bearer {gateway.tokens.issue_api_token(60)}"
|
||||
return await gateway.http.dispatch(connection(), Request(path, headers))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def preparation_gateway(review_gateway, monkeypatch):
|
||||
fixture = review_gateway
|
||||
snapshot = fixture.snapshot.model_copy(update={"owner": "venv", "manager": None})
|
||||
monkeypatch.setattr(update_plan, "inspect_installation", lambda: snapshot)
|
||||
monkeypatch.setattr(sys, "prefix", snapshot.environment)
|
||||
fixture.snapshot = snapshot
|
||||
return fixture
|
||||
|
||||
|
||||
async def preparation_plan(fixture):
|
||||
gateway = fixture.gateway()
|
||||
response = await gateway.http.dispatch_webui_mutation(
|
||||
connection(local=True), "settings.nanobot.update.plan", {"extras": []},
|
||||
)
|
||||
assert response.status_code == 200, response.body
|
||||
return gateway, UpdatePlans(fixture.directory / "plans").load(json.loads(response.body)["plan"]["id"])
|
||||
|
||||
|
||||
async def execution_plan(fixture):
|
||||
gateway, original = await preparation_plan(fixture)
|
||||
active = original.profiles[0].model_copy(update={"receipt": original.profiles[0].receipt.model_copy(
|
||||
update={"pid": os.getpid()},
|
||||
)})
|
||||
plan = original.model_copy(update={"id": str(uuid4()), "profiles": [active, original.profiles[1]],
|
||||
"stop_profiles": [active.receipt.id], "restart_profiles": [active.receipt.id]})
|
||||
directory = fixture.directory / "jobs" / plan.id
|
||||
directory.mkdir(parents=True)
|
||||
(directory / "plan.json").write_text(plan.model_dump_json())
|
||||
JobStatus(plan.id, Phase.READY, 0, False, "Staged routing fixture", int(time.time())).save(directory)
|
||||
return gateway, plan, directory
|
||||
|
||||
|
||||
async def test_execution_consent_survives_cancelled_response_and_duplicate_requests(preparation_gateway, monkeypatch):
|
||||
fixture = preparation_gateway
|
||||
gateway, plan, directory = await execution_plan(fixture)
|
||||
entered, release = threading.Event(), threading.Event()
|
||||
launches = []
|
||||
children = []
|
||||
confirmation = ExecutionConfirmation(uuid4().hex, plan.id, "a" * 64, plan.created_at, plan.expires_at)
|
||||
|
||||
def launch(path, *, once):
|
||||
assert path == directory and once
|
||||
launches.append(plan.id)
|
||||
confirmation.save(path)
|
||||
JobStatus(plan.id, Phase.CHECKING, 0, False, "Controlled launch", int(time.time())).save(path)
|
||||
entered.set()
|
||||
assert release.wait(5)
|
||||
child = subprocess.Popen([sys.executable, "-I", "-c", "import sys; sys.stdin.buffer.read()"],
|
||||
stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
children.append(child)
|
||||
return child
|
||||
|
||||
monkeypatch.setattr(update_service, "start_handoff", launch)
|
||||
payload = {"id": plan.id, "confirm_restart": True}
|
||||
operation = asyncio.create_task(gateway.http.dispatch_webui_mutation(connection(local=True), EXECUTE, payload))
|
||||
try:
|
||||
async with asyncio.timeout(5):
|
||||
while not entered.is_set():
|
||||
await asyncio.sleep(0.001)
|
||||
operation.cancel()
|
||||
await asyncio.sleep(0)
|
||||
assert not operation.done(), "A lost requester cannot abandon a still-running launch thread"
|
||||
fresh = fixture.gateway()
|
||||
duplicate = await fresh.http.dispatch_webui_mutation(connection(local=True), EXECUTE, payload)
|
||||
assert duplicate.status_code == 200, duplicate.body
|
||||
assert duplicate.headers["Cache-Control"] == "no-store"
|
||||
saved = json.loads(duplicate.body)["job"]
|
||||
assert saved["execution_confirmation_id"] == confirmation.id
|
||||
assert saved["recorded_phase"] == "checking" and not saved["mutation_started"]
|
||||
restored = await fetch(fresh, JOB + "?id=" + plan.id)
|
||||
assert json.loads(restored.body)["job"] == saved
|
||||
finally:
|
||||
release.set()
|
||||
try:
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await operation
|
||||
assert gateway.http.settings_routes._system._update_jobs._reapers
|
||||
await asyncio.wait_for(gateway.http.settings_routes.close(), 5)
|
||||
assert children[0].poll() is None, "Gateway shutdown must not wait for its independent updater"
|
||||
finally:
|
||||
for child in children:
|
||||
child.stdin.close()
|
||||
assert await asyncio.to_thread(child.wait, 5) == 0
|
||||
assert launches == [plan.id]
|
||||
assert "webui.update-jobs.execute.v1" in webui_contract()["capabilities"]
|
||||
|
||||
|
||||
async def test_execution_requires_explicit_restart_and_existing_install_authority(preparation_gateway, monkeypatch):
|
||||
fixture = preparation_gateway
|
||||
gateway, plan, directory = await execution_plan(fixture)
|
||||
|
||||
def forbidden(*args, **kwargs):
|
||||
pytest.fail("Unconfirmed or unauthorized requests cannot launch an updater")
|
||||
|
||||
monkeypatch.setattr(update_service, "start_handoff", forbidden)
|
||||
assert (await fetch(gateway, JOB + "/execute")).status_code == 405
|
||||
payload = {"id": plan.id, "confirm_restart": True}
|
||||
assert (await gateway.http.dispatch_webui_mutation(connection(), EXECUTE, payload)).status_code == 403
|
||||
assert (await fixture.gateway("native").http.dispatch_webui_mutation(connection(local=True), EXECUTE, payload)).status_code == 409
|
||||
for bad in ({"id": plan.id}, {"id": plan.id, "confirm_restart": False},
|
||||
{"id": plan.id, "confirm_restart": "true"}, {"id": plan.id, "confirm_restart": 1},
|
||||
{**payload, "repair": True}, {**payload, "command": "pip install"}):
|
||||
result = await gateway.http.dispatch_webui_mutation(connection(local=True), EXECUTE, bad)
|
||||
assert result.status_code == 400
|
||||
assert ExecutionConfirmation.read(directory) is None
|
||||
|
||||
|
||||
async def test_retry_is_explicit_authorized_and_old_submissions_only_read_history(preparation_gateway, monkeypatch):
|
||||
fixture = preparation_gateway
|
||||
gateway, plan, directory = await execution_plan(fixture)
|
||||
previous = ExecutionConfirmation(uuid4().hex, plan.id, "a" * 64, plan.created_at, plan.expires_at)
|
||||
current = ExecutionConfirmation(uuid4().hex, plan.id, "a" * 64, plan.created_at, plan.expires_at)
|
||||
previous.save(directory)
|
||||
JobStatus(plan.id, Phase.HANDOFF_FAILED, 0, False, "Stopped before mutation", int(time.time())).save(directory)
|
||||
calls = []
|
||||
|
||||
def retry(path, previous_id):
|
||||
assert path == directory and previous_id == previous.id
|
||||
calls.append(previous_id)
|
||||
current.save(path)
|
||||
JobStatus(plan.id, Phase.CHECKING, 0, False, "Retry confirmed", int(time.time())).save(path)
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(update_service, "retry_handoff", retry)
|
||||
payload = {"id": plan.id, "previous_confirmation_id": previous.id, "confirm_restart": True}
|
||||
assert (await fetch(gateway, JOB + "/retry")).status_code == 405
|
||||
assert (await gateway.http.dispatch_webui_mutation(connection(), RETRY, payload)).status_code == 403
|
||||
assert (await fixture.gateway("native").http.dispatch_webui_mutation(connection(local=True), RETRY, payload)).status_code == 409
|
||||
for bad in ({"id": plan.id, "confirm_restart": True}, {**payload, "confirm_restart": False},
|
||||
{**payload, "previous_confirmation_id": "bad-id"}, {**payload, "repair": True},
|
||||
{**payload, "confirm_restart": "true"}):
|
||||
assert (await gateway.http.dispatch_webui_mutation(connection(local=True), RETRY, bad)).status_code == 400
|
||||
assert not calls
|
||||
first = await gateway.http.dispatch_webui_mutation(connection(local=True), RETRY, payload)
|
||||
assert first.status_code == 200, first.body
|
||||
assert first.headers["Cache-Control"] == "no-store"
|
||||
assert json.loads(first.body)["job"]["execution_confirmation_id"] == current.id
|
||||
fresh = fixture.gateway()
|
||||
for action, body in ((RETRY, payload), (EXECUTE, {"id": plan.id, "confirm_restart": True})):
|
||||
repeated = await fresh.http.dispatch_webui_mutation(connection(local=True), action, body)
|
||||
assert repeated.status_code == 200 and json.loads(repeated.body) == json.loads(first.body)
|
||||
assert calls == [previous.id]
|
||||
assert "webui.update-jobs.retry-handoff.v1" in webui_contract()["capabilities"]
|
||||
|
||||
|
||||
async def test_busy_retry_does_not_look_like_an_accepted_new_confirmation(preparation_gateway, monkeypatch):
|
||||
gateway, plan, directory = await execution_plan(preparation_gateway)
|
||||
previous = ExecutionConfirmation(uuid4().hex, plan.id, "a" * 64, plan.created_at, plan.expires_at)
|
||||
previous.save(directory)
|
||||
|
||||
def busy(*args):
|
||||
raise BlockingIOError("private lease path must not reach the response")
|
||||
|
||||
monkeypatch.setattr(update_service, "retry_handoff", busy)
|
||||
response = await gateway.http.dispatch_webui_mutation(connection(local=True), RETRY,
|
||||
{"id": plan.id, "previous_confirmation_id": previous.id, "confirm_restart": True})
|
||||
assert response.status_code == 409
|
||||
assert b"private lease" not in response.body
|
||||
assert ExecutionConfirmation.read(directory) == previous
|
||||
|
||||
|
||||
async def test_handoff_settlement_route_preserves_permission_and_reads_cancelled_job(preparation_gateway, monkeypatch):
|
||||
from nanobot import update_stop
|
||||
|
||||
gateway, plan, directory = await execution_plan(preparation_gateway)
|
||||
previous = ExecutionConfirmation(uuid4().hex, plan.id, "a" * 64, plan.created_at, plan.expires_at)
|
||||
previous.save(directory)
|
||||
JobStatus(plan.id, Phase.HANDOFF_FAILED, 0, False, "Refused shutdown", plan.created_at).save(directory)
|
||||
calls = []
|
||||
|
||||
def settle(path):
|
||||
calls.append(path)
|
||||
assert path == directory
|
||||
state = JobStatus(plan.id, Phase.CANCELLED, 0, False, "Settled fixture", plan.created_at)
|
||||
state.save(path)
|
||||
return state
|
||||
|
||||
monkeypatch.setattr(update_stop, "settle_uncommitted_handoff", settle)
|
||||
assert (await gateway.http.dispatch_webui_mutation(connection(), SETTLE, {"id": plan.id})).status_code == 403
|
||||
assert calls == []
|
||||
response = await gateway.http.dispatch_webui_mutation(connection(local=True), SETTLE, {"id": plan.id})
|
||||
assert response.status_code == 200, response.body
|
||||
assert json.loads(response.body)["job"]["recorded_phase"] == "cancelled"
|
||||
assert json.loads(response.body)["job"]["execution_confirmation_id"] == previous.id
|
||||
assert (await fetch(gateway, JOB + "?id=" + plan.id)).body == response.body
|
||||
assert calls == [directory]
|
||||
assert "webui.update-jobs.settle-handoff.v1" in webui_contract()["capabilities"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("problem", ["self_not_selected", "peer_not_selected", "no_restart"])
|
||||
async def test_execution_refuses_unreviewed_gateway_lifecycle(preparation_gateway, monkeypatch, problem):
|
||||
fixture = preparation_gateway
|
||||
gateway, plan, directory = await execution_plan(fixture)
|
||||
changes = {}
|
||||
if problem == "self_not_selected":
|
||||
changes["profiles"] = [plan.profiles[0].model_copy(update={"receipt": plan.profiles[0].receipt.model_copy(
|
||||
update={"pid": os.getpid() + 10000},
|
||||
)}), plan.profiles[1]]
|
||||
elif problem == "peer_not_selected":
|
||||
changes["profiles"] = [plan.profiles[0], plan.profiles[1].model_copy(update={"state": "running"})]
|
||||
else:
|
||||
changes["restart_profiles"] = []
|
||||
(directory / "plan.json").write_text(plan.model_copy(update=changes).model_dump_json())
|
||||
|
||||
def forbidden(*args, **kwargs):
|
||||
pytest.fail("Unsupported lifecycle cannot launch an updater")
|
||||
|
||||
monkeypatch.setattr(update_service, "start_handoff", forbidden)
|
||||
result = await gateway.http.dispatch_webui_mutation(connection(local=True), EXECUTE,
|
||||
{"id": plan.id, "confirm_restart": True})
|
||||
assert result.status_code == 409, result.body
|
||||
assert ExecutionConfirmation.read(directory) is None
|
||||
|
||||
|
||||
async def test_preparation_is_durable_idempotent_and_independent_of_browser(preparation_gateway, monkeypatch):
|
||||
fixture = preparation_gateway
|
||||
gateway, plan = await preparation_plan(fixture)
|
||||
entered, release = asyncio.Event(), asyncio.Event()
|
||||
validated, prepared = [], []
|
||||
|
||||
async def revalidate(self, target):
|
||||
validated.append(target)
|
||||
|
||||
async def stage(exact, feed):
|
||||
prepared.append(exact)
|
||||
directory = fixture.directory / "jobs" / exact.id
|
||||
JobStatus(exact.id, Phase.PREPARING, 0, False, "Preparing", int(time.time())).save(directory)
|
||||
entered.set()
|
||||
await release.wait()
|
||||
JobStatus(exact.id, Phase.READY, 0, False, "Staged fixture", int(time.time())).save(directory)
|
||||
return directory
|
||||
|
||||
monkeypatch.setattr(update_service.ReleaseFeed, "revalidate", revalidate)
|
||||
monkeypatch.setattr(update_service, "prepare_job", stage)
|
||||
close = None
|
||||
try:
|
||||
response = await gateway.http.dispatch_webui_mutation(connection(local=True), PREPARE, {"id": plan.id})
|
||||
assert response.status_code == 200, response.body
|
||||
assert json.loads(response.body)["job"]["recorded_phase"] == "preparation_requested"
|
||||
assert response.headers["Cache-Control"] == "no-store"
|
||||
directory = fixture.directory / "jobs" / plan.id
|
||||
assert JobStatus.read(directory).phase == Phase.PREPARATION_REQUESTED
|
||||
await asyncio.wait_for(entered.wait(), 5)
|
||||
# A recreated router has no reference to the original browser's task.
|
||||
fresh = fixture.gateway()
|
||||
duplicate = await fresh.http.dispatch_webui_mutation(connection(local=True), PREPARE, {"id": plan.id})
|
||||
assert duplicate.status_code == 200
|
||||
assert json.loads(duplicate.body)["job"]["recorded_phase"] == "preparing"
|
||||
assert (await fetch(fresh, JOB + "?id=" + plan.id)).status_code == 200
|
||||
assert (await fresh.http.dispatch_webui_mutation(connection(local=True), SETTLE, {"id": plan.id})).status_code == 409
|
||||
other = plan.model_copy(update={"id": str(uuid4())})
|
||||
UpdatePlans(fixture.directory / "plans").save(other)
|
||||
busy = await fresh.http.dispatch_webui_mutation(connection(local=True), PREPARE, {"id": other.id})
|
||||
assert busy.status_code == 409
|
||||
assert not (fixture.directory / "jobs" / other.id).exists()
|
||||
with pytest.raises(RuntimeBusyError):
|
||||
with gateway.http.settings_routes._system._admission.pause_if_idle(lambda: False):
|
||||
pytest.fail("Preparation must be counted as runtime work")
|
||||
close = asyncio.create_task(gateway.http.settings_routes.close())
|
||||
async with asyncio.timeout(5):
|
||||
while not gateway.http.settings_routes._system._update_jobs._closing:
|
||||
await asyncio.sleep(0.001)
|
||||
close.cancel()
|
||||
await asyncio.sleep(0)
|
||||
assert not close.done(), "Shutdown cancellation must not release a running preparation"
|
||||
with pytest.raises(InstallationBusyError):
|
||||
with installation_access(updating=True):
|
||||
pytest.fail("Preparation lost its shared installation lease")
|
||||
finally:
|
||||
release.set()
|
||||
if close is not None:
|
||||
await asyncio.wait_for(close, 5)
|
||||
else:
|
||||
await gateway.http.settings_routes.close()
|
||||
final = await fetch(fixture.gateway(), JOB + "?id=" + plan.id)
|
||||
assert json.loads(final.body)["job"]["recorded_phase"] == "ready"
|
||||
assert validated == [plan.target] and prepared == [plan]
|
||||
assert len(fixture.requests) == 1, "The staging controller must not reselect latest"
|
||||
for name in ("approval.json", "handoff-approval.json"):
|
||||
assert not (directory / name).exists()
|
||||
assert not (fixture.directory / "stop.json").exists()
|
||||
assert "webui.update-jobs.prepare.v1" in webui_contract()["capabilities"]
|
||||
|
||||
|
||||
async def test_preparation_revalidation_failure_is_saved_and_never_retried_by_refresh(preparation_gateway, monkeypatch):
|
||||
fixture = preparation_gateway
|
||||
gateway, plan = await preparation_plan(fixture)
|
||||
targets = []
|
||||
|
||||
async def withdrawn(self, target):
|
||||
targets.append(target)
|
||||
raise ValueError("private-index-user:secret")
|
||||
|
||||
async def forbidden(*args):
|
||||
pytest.fail("A withdrawn target must not reach staging")
|
||||
|
||||
monkeypatch.setattr(update_service.ReleaseFeed, "revalidate", withdrawn)
|
||||
monkeypatch.setattr(update_service, "prepare_job", forbidden)
|
||||
accepted = await gateway.http.dispatch_webui_mutation(connection(local=True), PREPARE, {"id": plan.id})
|
||||
assert accepted.status_code == 200
|
||||
await gateway.http.settings_routes.close()
|
||||
duplicate = await fixture.gateway().http.dispatch_webui_mutation(connection(local=True), PREPARE, {"id": plan.id})
|
||||
assert duplicate.status_code == 200
|
||||
assert json.loads(duplicate.body)["job"]["recorded_phase"] == "preparation_failed"
|
||||
assert b"secret" not in duplicate.body
|
||||
directory = fixture.directory / "jobs" / plan.id
|
||||
assert "secret" not in (directory / "status.json").read_text()
|
||||
assert targets == [plan.target]
|
||||
assert not (directory / "request.json").exists()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("operation", [PREPARE, SETTLE])
|
||||
async def test_preparation_mutations_require_authority_and_exact_id(preparation_gateway, operation):
|
||||
fixture = preparation_gateway
|
||||
gateway, plan = await preparation_plan(fixture)
|
||||
path = JOB + ("/prepare" if operation == PREPARE else "/settle")
|
||||
assert (await fetch(gateway, path)).status_code == 405
|
||||
remote = await gateway.http.dispatch_webui_mutation(connection(), operation, {"id": plan.id})
|
||||
assert remote.status_code == 403
|
||||
desktop = await fixture.gateway("native").http.dispatch_webui_mutation(connection(local=True), operation, {"id": plan.id})
|
||||
assert desktop.status_code == 409
|
||||
for payload in ({}, {"id": "../plan"}, {"id": 12}, {"id": plan.id, "target": "new-version"},
|
||||
{"id": plan.id, "repair": True}):
|
||||
response = await gateway.http.dispatch_webui_mutation(connection(local=True), operation, payload)
|
||||
assert response.status_code == 400
|
||||
assert not (fixture.directory / "jobs").exists()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("problem", ["expired", "source", "unknown_extras", "foreign"])
|
||||
async def test_preparation_rejects_unexecutable_reviews_before_creating_work(preparation_gateway, problem):
|
||||
fixture = preparation_gateway
|
||||
gateway, plan = await preparation_plan(fixture)
|
||||
changes = {"id": str(uuid4())}
|
||||
if problem == "expired":
|
||||
changes.update(created_at=plan.created_at - 1800, expires_at=plan.expires_at - 1800)
|
||||
elif problem == "source":
|
||||
changes["installation"] = plan.installation.model_copy(update={"owner": "source"})
|
||||
elif problem == "unknown_extras":
|
||||
changes["extras"] = None
|
||||
else:
|
||||
changes["installation"] = plan.installation.model_copy(update={"environment": str(fixture.directory / "foreign")})
|
||||
invalid = plan.model_copy(update=changes)
|
||||
UpdatePlans(fixture.directory / "plans").save(invalid)
|
||||
response = await gateway.http.dispatch_webui_mutation(connection(local=True), PREPARE, {"id": invalid.id})
|
||||
assert response.status_code == 409
|
||||
assert not (fixture.directory / "jobs").exists()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("phase", [Phase.PREPARATION_REQUESTED, Phase.PREPARING])
|
||||
async def test_explicit_settlement_checks_ownership_and_does_not_retry(preparation_gateway, monkeypatch, phase):
|
||||
fixture = preparation_gateway
|
||||
gateway, plan = await preparation_plan(fixture)
|
||||
directory = fixture.directory / "jobs" / plan.id
|
||||
directory.mkdir(parents=True)
|
||||
(directory / "plan.json").write_text(plan.model_dump_json())
|
||||
JobStatus(plan.id, phase, 0, False, "Interrupted fixture", int(time.time())).save(directory)
|
||||
if phase == Phase.PREPARING:
|
||||
PreparationProcess.create(directory, plan.installation.environment)
|
||||
|
||||
async def forbidden(*args):
|
||||
pytest.fail("Reading or settling interrupted staging cannot restart it")
|
||||
|
||||
monkeypatch.setattr(update_service.ReleaseFeed, "revalidate", forbidden)
|
||||
restored = await gateway.http.dispatch_webui_mutation(connection(local=True), PREPARE, {"id": plan.id})
|
||||
assert json.loads(restored.body)["job"]["recorded_phase"] == phase.value
|
||||
lock = "prepare-native.lock" if phase == Phase.PREPARING else "preparation-controller.lock"
|
||||
with file_access(fixture.directory / lock, exclusive=True):
|
||||
blocked = await gateway.http.dispatch_webui_mutation(connection(local=True), SETTLE, {"id": plan.id})
|
||||
assert blocked.status_code == 409
|
||||
assert JobStatus.read(directory).phase == phase
|
||||
settled = await gateway.http.dispatch_webui_mutation(connection(local=True), SETTLE, {"id": plan.id})
|
||||
assert settled.status_code == 200, settled.body
|
||||
assert json.loads(settled.body)["job"]["recorded_phase"] == "preparation_failed"
|
||||
if phase == Phase.PREPARING:
|
||||
assert PreparationProcess.load(directory).active is False
|
||||
assert not (directory / "request.json").exists()
|
||||
duplicate = await fixture.gateway().http.dispatch_webui_mutation(connection(local=True), PREPARE, {"id": plan.id})
|
||||
assert json.loads(duplicate.body)["job"]["recorded_phase"] == "preparation_failed"
|
||||
|
||||
|
||||
async def test_exact_review_survives_router_recreation_without_reselection_or_install(review_gateway):
|
||||
fixture = review_gateway
|
||||
gateway = fixture.gateway()
|
||||
denied = await fetch(gateway, READ + "?id=" + str(uuid4()), authorized=False)
|
||||
assert denied.status_code == 401
|
||||
denied = await fetch(gateway, CREATE)
|
||||
assert denied.status_code == 405
|
||||
response = await gateway.http.dispatch_webui_mutation(
|
||||
connection(), "settings.nanobot.update.plan", {"channel": "stable", "restart_running": True},
|
||||
)
|
||||
assert response.status_code == 200, response.body
|
||||
assert response.headers["Cache-Control"] == "no-store"
|
||||
payload = json.loads(response.body)
|
||||
review = payload["plan"]
|
||||
assert payload["installation_allowed"] is False
|
||||
assert payload["execution_available"] is False
|
||||
assert (review["running_version"], review["target_version"], review["owner"]) == ("0.3.4", "0.3.5", "uv")
|
||||
assert review["extras"] == ["webui"] and review["validity"] == "current"
|
||||
assert review["guide"]["method"] == "uv"
|
||||
assert review["guide"]["host"] == review["host"]
|
||||
assert review["guide"]["environment"] == fixture.snapshot.environment
|
||||
assert [(item["reviewed_state"], item["stop"], item["restart"]) for item in review["profiles"]] == [
|
||||
("running", True, True), ("stopped", False, False),
|
||||
]
|
||||
assert {"records", "manager", "python", "url", "target"}.isdisjoint(review)
|
||||
saved = UpdatePlans(fixture.directory / "plans").load(review["id"])
|
||||
assert saved.stop_profiles == saved.restart_profiles == [fixture.observed[0].receipt.id]
|
||||
assert not (fixture.directory / "jobs").exists()
|
||||
assert not (fixture.directory / "stop.json").exists()
|
||||
assert fixture.requests == ["https://pypi.org/pypi/nanobot-ai/json"]
|
||||
|
||||
fresh_gateway = fixture.gateway()
|
||||
restored = await fetch(fresh_gateway, READ + "?id=" + review["id"])
|
||||
assert restored.headers["Cache-Control"] == "no-store"
|
||||
assert json.loads(restored.body) == payload
|
||||
assert len(fixture.requests) == 1, "Reading an existing review must not resolve a different latest target"
|
||||
assert "webui.update-plans.v1" in webui_contract()["capabilities"]
|
||||
|
||||
|
||||
async def test_live_update_review_requires_this_gateway_and_preserves_owner_policy(review_gateway):
|
||||
fixture = review_gateway
|
||||
running = fixture.observed[0]
|
||||
fixture.observed[0] = running.model_copy(update={"receipt": running.receipt.model_copy(update={"pid": os.getpid()})})
|
||||
gateway = fixture.gateway()
|
||||
response = await gateway.http.dispatch_webui_mutation(
|
||||
connection(local=True), "settings.nanobot.update.plan", {"restart_running": True},
|
||||
)
|
||||
payload = json.loads(response.body)
|
||||
assert payload["installation_allowed"] is True
|
||||
assert payload["execution_available"] is (os.name == "posix")
|
||||
assert payload["plan"]["extras"] == ["webui"]
|
||||
plan = UpdatePlans(fixture.directory / "plans").load(payload["plan"]["id"])
|
||||
assert update_plan.inline_update_blocked_reason(plan, "expired") == "review_changed"
|
||||
assert update_plan.inline_update_blocked_reason(plan.model_copy(update={"restart_profiles": []})) == (
|
||||
"external_restart" if os.name == "posix" else "installation_owner"
|
||||
)
|
||||
assert not (fixture.directory / "jobs").exists(), "Review must not prepare or install"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("manager_version", [None, "0.11.15", "0.11.17"])
|
||||
async def test_review_routes_unverified_uv_versions_to_the_installation_owner(review_gateway, monkeypatch, manager_version):
|
||||
fixture = review_gateway
|
||||
snapshot = fixture.snapshot.model_copy(update={"manager_version": manager_version})
|
||||
monkeypatch.setattr(update_plan, "inspect_installation", lambda: snapshot)
|
||||
running = fixture.observed[0]
|
||||
fixture.observed[0] = running.model_copy(update={"receipt": running.receipt.model_copy(update={"pid": os.getpid()})})
|
||||
response = await fixture.gateway().http.dispatch_webui_mutation(
|
||||
connection(local=True), "settings.nanobot.update.plan", {"restart_running": True},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
payload = json.loads(response.body)
|
||||
assert payload["execution_available"] is False
|
||||
plan = UpdatePlans(fixture.directory / "plans").load(payload["plan"]["id"])
|
||||
assert update_plan.inline_update_blocked_reason(plan) == "installation_owner"
|
||||
assert not (fixture.directory / "jobs").exists()
|
||||
|
||||
|
||||
async def test_review_reports_profile_and_installation_changes_without_replacing_plan(review_gateway, monkeypatch):
|
||||
fixture = review_gateway
|
||||
gateway = fixture.gateway()
|
||||
created = await gateway.http.dispatch_webui_mutation(connection(local=True), "settings.nanobot.update.plan", {})
|
||||
assert created.status_code == 200
|
||||
payload = json.loads(created.body)
|
||||
assert payload["installation_allowed"] is True
|
||||
review = payload["plan"]
|
||||
assert not any(item["stop"] or item["restart"] for item in review["profiles"])
|
||||
saved_bytes = (fixture.directory / "plans" / f"{review['id']}.json").read_bytes()
|
||||
fixture.observed[0] = fixture.observed[0].model_copy(update={
|
||||
"receipt": fixture.observed[0].receipt.model_copy(update={"run_id": uuid4().hex}),
|
||||
})
|
||||
response = await fetch(gateway, READ + "?id=" + review["id"])
|
||||
assert json.loads(response.body)["plan"]["validity"] == "profiles_changed"
|
||||
monkeypatch.setattr(update_plan, "inspect_installation", lambda: fixture.snapshot.model_copy(update={
|
||||
"installed_version": "0.3.5", "records": {"uv-receipt.toml": "c" * 64},
|
||||
}))
|
||||
response = await fetch(gateway, READ + "?id=" + review["id"])
|
||||
changed = json.loads(response.body)["plan"]
|
||||
assert changed["validity"] == "installation_changed"
|
||||
assert changed["installed_version"] == "0.3.5"
|
||||
assert (fixture.directory / "plans" / f"{review['id']}.json").read_bytes() == saved_bytes
|
||||
|
||||
|
||||
@pytest.mark.parametrize("payload", [
|
||||
{"channel": "dev"}, {"restart_running": "true"}, {"extras": ["--index-url"]},
|
||||
{"target": "https://untrusted.example/wheel"}, {"installation": "/another/tool"},
|
||||
])
|
||||
async def test_invalid_client_request_cannot_select_code_or_environment(review_gateway, payload):
|
||||
fixture = review_gateway
|
||||
response = await fixture.gateway().http.dispatch_webui_mutation(
|
||||
connection(), "settings.nanobot.update.plan", payload,
|
||||
)
|
||||
assert response.status_code == 400
|
||||
assert fixture.requests == []
|
||||
assert not (fixture.directory / "plans").exists()
|
||||
|
||||
|
||||
async def test_expired_unknown_and_foreign_plans_have_explicit_results(review_gateway, monkeypatch):
|
||||
fixture = review_gateway
|
||||
gateway = fixture.gateway()
|
||||
response = await gateway.http.dispatch_webui_mutation(connection(), "settings.nanobot.update.plan", {})
|
||||
review = json.loads(response.body)["plan"]
|
||||
now = time.time()
|
||||
monkeypatch.setattr(update_plan.time, "time", lambda: now + 901)
|
||||
response = await fetch(gateway, READ + "?id=" + review["id"])
|
||||
assert json.loads(response.body)["plan"]["validity"] == "expired"
|
||||
missing = await fetch(gateway, READ + "?id=" + str(uuid4()))
|
||||
assert missing.status_code == 404
|
||||
traversal = await fetch(gateway, READ + "?id=../../config")
|
||||
assert traversal.status_code == 400
|
||||
other = fixture.snapshot.model_copy(update={"environment": str(fixture.directory / "other")})
|
||||
monkeypatch.setattr(update_plan, "inspect_installation", lambda: other)
|
||||
missing = await fetch(gateway, READ + "?id=" + review["id"])
|
||||
assert missing.status_code == 404
|
||||
|
||||
|
||||
async def test_desktop_and_failed_discovery_do_not_claim_executable_or_current(review_gateway, monkeypatch):
|
||||
fixture = review_gateway
|
||||
desktop = await fixture.gateway("native").http.dispatch_webui_mutation(
|
||||
connection(), "settings.nanobot.update.plan", {},
|
||||
)
|
||||
assert desktop.status_code == 409 and fixture.requests == []
|
||||
|
||||
def failed_probe():
|
||||
raise ValueError("https://user:secret@index.example/private")
|
||||
|
||||
monkeypatch.setattr(update_plan, "inspect_installation", failed_probe)
|
||||
failed = await fixture.gateway().http.dispatch_webui_mutation(connection(), "settings.nanobot.update.plan", {})
|
||||
assert failed.status_code == 409
|
||||
assert b"secret" not in failed.body
|
||||
assert not (fixture.directory / "plans").exists()
|
||||
|
||||
|
||||
async def test_environment_replaced_during_discovery_does_not_return_a_valid_review(review_gateway, monkeypatch):
|
||||
fixture = review_gateway
|
||||
calls = 0
|
||||
|
||||
def changed_probe():
|
||||
nonlocal calls
|
||||
calls += 1
|
||||
if calls == 1:
|
||||
return fixture.snapshot
|
||||
return fixture.snapshot.model_copy(update={"installed_version": "0.3.6"})
|
||||
|
||||
monkeypatch.setattr(update_plan, "inspect_installation", changed_probe)
|
||||
response = await fixture.gateway().http.dispatch_webui_mutation(
|
||||
connection(), "settings.nanobot.update.plan", {},
|
||||
)
|
||||
assert response.status_code == 409
|
||||
assert response.body == b"Could not verify an update plan for this installation. No update was started."
|
||||
assert calls == 2
|
||||
assert not (fixture.directory / "jobs").exists()
|
||||
|
||||
|
||||
async def test_missing_interpreter_is_not_misreported_as_a_missing_plan(review_gateway, monkeypatch):
|
||||
fixture = review_gateway
|
||||
gateway = fixture.gateway()
|
||||
response = await gateway.http.dispatch_webui_mutation(connection(), "settings.nanobot.update.plan", {})
|
||||
plan_id = json.loads(response.body)["plan"]["id"]
|
||||
|
||||
def removed_python():
|
||||
raise FileNotFoundError("/private/installation/bin/python")
|
||||
|
||||
monkeypatch.setattr(update_plan, "inspect_installation", removed_python)
|
||||
failed = await fetch(gateway, READ + "?id=" + plan_id)
|
||||
assert failed.status_code == 409
|
||||
assert b"/private/installation" not in failed.body
|
||||
assert (fixture.directory / "plans" / f"{plan_id}.json").exists()
|
||||
|
||||
|
||||
async def test_job_reads_survive_refresh_expiry_and_unavailable_installation(review_gateway, monkeypatch):
|
||||
fixture = review_gateway
|
||||
gateway = fixture.gateway()
|
||||
response = await gateway.http.dispatch_webui_mutation(connection(), "settings.nanobot.update.plan", {})
|
||||
plan_id = json.loads(response.body)["plan"]["id"]
|
||||
plan = UpdatePlans(fixture.directory / "plans").load(plan_id)
|
||||
directory = fixture.directory / "jobs" / plan_id
|
||||
directory.mkdir(parents=True)
|
||||
(directory / "plan.json").write_text(plan.model_dump_json())
|
||||
JobStatus(plan_id, Phase.PREPARING, 0, False, "private diagnostic", plan.created_at).save(directory)
|
||||
monkeypatch.setattr(sys, "prefix", fixture.snapshot.environment)
|
||||
|
||||
def unavailable_probe():
|
||||
pytest.fail("Reading a saved job must not launch the changing installation")
|
||||
|
||||
monkeypatch.setattr(update_plan, "inspect_installation", unavailable_probe)
|
||||
response = await fetch(gateway, JOB + "?id=" + plan_id, authorized=False)
|
||||
assert response.status_code == 401
|
||||
response = await fetch(gateway, JOB + "?id=" + plan_id)
|
||||
assert response.status_code == 200
|
||||
job = json.loads(response.body)["job"]
|
||||
recovery = job.pop("recovery_command")
|
||||
assert plan.id in recovery and recovery.endswith(" --repair")
|
||||
assert "recover.py" in recovery
|
||||
assert job == {
|
||||
"id": plan_id, "installation_id": fixture.snapshot.installation_id,
|
||||
"host": platform.node(), "previous_version": "0.3.4",
|
||||
"target_version": "0.3.5", "channel": "stable", "recorded_phase": "preparing",
|
||||
"attempt": 0, "mutation_started": False, "updated_at": plan.created_at,
|
||||
"execution_confirmation_id": None,
|
||||
"running_version": nanobot.__version__,
|
||||
"recovery_active": False,
|
||||
"service_restored": False,
|
||||
"automatic_recovery": False,
|
||||
}
|
||||
assert not (directory / "request.json").exists(), "An early journal must not require committed staging"
|
||||
assert "webui.update-jobs.read.v1" in webui_contract()["capabilities"]
|
||||
|
||||
# A journal is recoverable without revalidating the expired plan or reading
|
||||
# package metadata; it is not permission to retry or proof a worker is live.
|
||||
monkeypatch.setattr(update_plan.time, "time", lambda: plan.expires_at + 1)
|
||||
JobStatus(plan_id, Phase.FAILED, 1, True, "https://user:secret@private.example", plan.expires_at).save(directory)
|
||||
pending = pending_update_path()
|
||||
pending.parent.mkdir(parents=True)
|
||||
pending.write_text("interrupted update")
|
||||
with installation_access(updating=True):
|
||||
response = await fetch(fixture.gateway(), JOB + "?id=" + plan_id)
|
||||
assert response.status_code == 200
|
||||
assert response.headers["Cache-Control"] == "no-store"
|
||||
job = json.loads(response.body)["job"]
|
||||
assert job["recorded_phase"] == "failed" and job["mutation_started"] is True
|
||||
assert job["attempt"] == 1 and job["updated_at"] == plan.expires_at
|
||||
assert b"secret" not in response.body
|
||||
# Only the explicit recovery command exposes the launcher's exact location;
|
||||
# job diagnostics and profile paths must remain out of the status payload.
|
||||
assert job.pop("recovery_command") == recovery
|
||||
assert "private account" not in json.dumps(job)
|
||||
assert {"message", "running", "can_retry", "python", "url", "profiles"}.isdisjoint(job)
|
||||
assert len(fixture.requests) == 1
|
||||
assert not (directory / "approval.json").exists()
|
||||
|
||||
# A completed program restoration is not a successful installation. Keep
|
||||
# the legacy wire phase readable while exposing the precise new result.
|
||||
JobStatus(plan_id, Phase.RESTORED, 2, True, "Private recovery detail", plan.expires_at).save(directory)
|
||||
response = await fetch(fixture.gateway(), JOB + "?id=" + plan_id)
|
||||
job = json.loads(response.body)["job"]
|
||||
assert job["recorded_phase"] == "failed" and job["service_restored"] is True
|
||||
assert b"Private recovery detail" not in response.body
|
||||
|
||||
|
||||
async def test_job_reads_reject_missing_foreign_and_corrupt_records(review_gateway, monkeypatch):
|
||||
fixture = review_gateway
|
||||
gateway = fixture.gateway()
|
||||
response = await gateway.http.dispatch_webui_mutation(connection(), "settings.nanobot.update.plan", {})
|
||||
plan_id = json.loads(response.body)["plan"]["id"]
|
||||
plan = UpdatePlans(fixture.directory / "plans").load(plan_id)
|
||||
monkeypatch.setattr(sys, "prefix", fixture.snapshot.environment)
|
||||
for query in ("", "?id=../../config", "?id=not-a-uuid"):
|
||||
assert (await fetch(gateway, JOB + query)).status_code == 400
|
||||
assert (await fetch(gateway, JOB + "?id=" + plan_id)).status_code == 404
|
||||
directory = fixture.directory / "jobs" / plan_id
|
||||
directory.mkdir(parents=True)
|
||||
(directory / "plan.json").write_text(plan.model_dump_json())
|
||||
(directory / "status.json").write_text('{"private":"secret"}')
|
||||
response = await fetch(gateway, JOB + "?id=" + plan_id)
|
||||
assert response.status_code == 409 and b"secret" not in response.body
|
||||
assert response.headers["Cache-Control"] == "no-store"
|
||||
JobStatus(plan_id, Phase.READY, 0, False, "Prepared", plan.created_at).save(directory)
|
||||
foreign = plan.model_copy(update={"installation": fixture.snapshot.model_copy(
|
||||
update={"environment": str(fixture.directory / "another installation")},
|
||||
)})
|
||||
(directory / "plan.json").write_text(foreign.model_dump_json())
|
||||
assert (await fetch(gateway, JOB + "?id=" + plan_id)).status_code == 409
|
||||
(directory / "plan.json").write_text(plan.model_dump_json())
|
||||
assert (await fetch(fixture.gateway("native"), JOB + "?id=" + plan_id)).status_code == 409
|
||||
monkeypatch.setattr(sys, "prefix", str(fixture.directory / "another installation"))
|
||||
assert (await fetch(gateway, JOB + "?id=" + plan_id)).status_code == 404
|
||||
|
||||
|
||||
@pytest.mark.parametrize("reading", [False, True])
|
||||
async def test_cancelled_request_keeps_probe_lease_until_native_work_exits(review_gateway, monkeypatch, reading):
|
||||
fixture = review_gateway
|
||||
entered, release = threading.Event(), threading.Event()
|
||||
|
||||
def native_probe():
|
||||
entered.set()
|
||||
assert release.wait(5), "The test did not settle its native probe"
|
||||
return fixture.snapshot
|
||||
|
||||
monkeypatch.setattr(update_plan, "inspect_installation", native_probe)
|
||||
task = asyncio.create_task(update_plan.read_release_plan(str(uuid4())) if reading
|
||||
else update_plan.preview_release_update("stable"))
|
||||
try:
|
||||
assert await asyncio.to_thread(entered.wait, 5)
|
||||
task.cancel()
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await task
|
||||
with pytest.raises(InstallationBusyError):
|
||||
with installation_access(updating=True):
|
||||
pytest.fail("Native inspection outlived its installation lease")
|
||||
assert fixture.requests == []
|
||||
finally:
|
||||
release.set()
|
||||
# Wait for the probe's actual lease release before restoring Path.home.
|
||||
async with asyncio.timeout(5):
|
||||
while True:
|
||||
try:
|
||||
with installation_access(updating=True):
|
||||
break
|
||||
except InstallationBusyError:
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("problem", ["unknown", "supervisor", "extras"])
|
||||
async def test_plan_cannot_invent_restart_ownership_or_replace_manager_extras(review_gateway, problem):
|
||||
fixture = review_gateway
|
||||
payload = {"restart_running": True}
|
||||
if problem == "unknown":
|
||||
fixture.observed[0] = fixture.observed[0].model_copy(update={"state": "unconfirmed"})
|
||||
elif problem == "supervisor":
|
||||
fixture.observed[0] = fixture.observed[0].model_copy(update={
|
||||
"receipt": fixture.observed[0].receipt.model_copy(update={"launch_mode": "foreground"}),
|
||||
})
|
||||
else:
|
||||
payload["extras"] = []
|
||||
response = await fixture.gateway().http.dispatch_webui_mutation(connection(), "settings.nanobot.update.plan", payload)
|
||||
if problem == "extras":
|
||||
assert response.status_code == 409
|
||||
assert not (fixture.directory / "plans").exists()
|
||||
else:
|
||||
assert response.status_code == 200
|
||||
payload = json.loads(response.body)
|
||||
assert payload["execution_available"] is False
|
||||
assert payload["plan"]["execution_blocked_reason"] == (
|
||||
"external_restart" if os.name == "posix" else "installation_owner"
|
||||
)
|
||||
assert not any(profile["restart"] for profile in payload["plan"]["profiles"])
|
||||
@@ -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"
|
||||
Reference in new issue
Block a user