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
+105
-21
@@ -3,11 +3,11 @@
|
||||
import asyncio
|
||||
import signal
|
||||
from collections.abc import Awaitable, Callable, Coroutine, Iterable
|
||||
from contextlib import suppress
|
||||
from dataclasses import replace
|
||||
from contextlib import ExitStack, suppress
|
||||
from dataclasses import asdict, replace
|
||||
from functools import partial
|
||||
from pathlib import Path
|
||||
from typing import Any, cast
|
||||
from typing import TYPE_CHECKING, Any, cast
|
||||
|
||||
import typer
|
||||
from loguru import logger
|
||||
@@ -26,6 +26,7 @@ from nanobot.agent.tools.registry import ToolRegistry
|
||||
from nanobot.bus.events import OutboundMessage
|
||||
from nanobot.bus.queue import MessageBus
|
||||
from nanobot.cli import terminal as cli_terminal
|
||||
from nanobot.cli.dock import watch_gateway_icon
|
||||
from nanobot.cli.runtime_config import _migrate_cron_store
|
||||
from nanobot.cli.webui_support import (
|
||||
_gateway_health_bind_note,
|
||||
@@ -40,9 +41,10 @@ from nanobot.cli.webui_support import (
|
||||
_webui_display_url,
|
||||
_webui_endpoint_reachable,
|
||||
)
|
||||
from nanobot.config.paths import is_default_workspace
|
||||
from nanobot.config.paths import get_data_dir, is_default_workspace
|
||||
from nanobot.config.schema import Config
|
||||
from nanobot.gateway.runtime import GatewayInstance
|
||||
from nanobot.gateway.runtime import GatewayHealthIdentity, GatewayInstance, GatewayRuntime
|
||||
from nanobot.installation_lock import installation_access
|
||||
from nanobot.security.network import is_loopback_host
|
||||
from nanobot.session.keys import (
|
||||
HEARTBEAT_SESSION_KEY,
|
||||
@@ -58,6 +60,10 @@ from nanobot.webui.build import BuildMode
|
||||
from nanobot.webui.dev import WebUIDevError, WebUIDevServer
|
||||
from nanobot.webui.sidebar_state import read_webui_sidebar_state
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from nanobot.update_profiles import ProfileReceipt
|
||||
from nanobot.update_restart import GatewayStartup
|
||||
|
||||
__all__ = ["_run_gateway"]
|
||||
|
||||
console = Console()
|
||||
@@ -414,6 +420,7 @@ def _run_gateway(
|
||||
config: Config,
|
||||
*,
|
||||
port: int | None = None,
|
||||
verbose: bool = False,
|
||||
open_browser_url: str | None = None,
|
||||
open_browser_ready_url: str | None = None,
|
||||
webui_static_dist: bool = True,
|
||||
@@ -424,8 +431,61 @@ def _run_gateway(
|
||||
unconfigured_provider_error: str | None = None,
|
||||
webui_dev_server: WebUIDevServer | None = None,
|
||||
gateway_instance: GatewayInstance | None = None,
|
||||
update_startup: "GatewayStartup | None" = None,
|
||||
) -> None:
|
||||
"""Shared gateway runtime; ``open_browser_url`` opens a tab once channels are up."""
|
||||
"""Claim the installation and profile before initializing any runtime writers."""
|
||||
from nanobot.config.loader import get_config_path
|
||||
from nanobot.update_profiles import record_gateway_profile
|
||||
|
||||
port = port if port is not None else config.gateway.port
|
||||
instance = gateway_instance or GatewayInstance.resolve(config_path=get_config_path())
|
||||
gateway_runtime = GatewayRuntime(paths=instance.paths)
|
||||
with installation_access(allow_incomplete=update_startup is not None):
|
||||
if update_startup is not None:
|
||||
update_startup.validate_installation()
|
||||
with (
|
||||
gateway_runtime.foreground_instance(instance.start_options(port=port, verbose=verbose)),
|
||||
record_gateway_profile(instance, gateway_runtime, data_dir=get_data_dir(),
|
||||
workspace=config.workspace_path, port=port, verbose=verbose,
|
||||
grant=update_startup.grant if update_startup else None,
|
||||
cooperative_stop=True) as profile,
|
||||
):
|
||||
if update_startup is not None:
|
||||
update_startup.validate_profile(profile)
|
||||
_run_claimed_gateway(
|
||||
config, port=port, gateway_instance=instance, gateway_runtime=gateway_runtime,
|
||||
health_identity=profile.health_identity, update_startup=update_startup,
|
||||
update_profile=profile,
|
||||
open_browser_url=open_browser_url, open_browser_ready_url=open_browser_ready_url,
|
||||
webui_static_dist=webui_static_dist, webui_bundle_mode=webui_bundle_mode,
|
||||
webui_runtime_surface=webui_runtime_surface,
|
||||
webui_runtime_capabilities=webui_runtime_capabilities,
|
||||
health_server_enabled=health_server_enabled,
|
||||
unconfigured_provider_error=unconfigured_provider_error,
|
||||
webui_dev_server=webui_dev_server,
|
||||
)
|
||||
|
||||
|
||||
def _run_claimed_gateway(
|
||||
config: Config,
|
||||
*,
|
||||
port: int,
|
||||
gateway_instance: GatewayInstance,
|
||||
gateway_runtime: GatewayRuntime,
|
||||
health_identity: GatewayHealthIdentity,
|
||||
open_browser_url: str | None,
|
||||
open_browser_ready_url: str | None,
|
||||
webui_static_dist: bool,
|
||||
webui_bundle_mode: BuildMode,
|
||||
webui_runtime_surface: str,
|
||||
webui_runtime_capabilities: dict[str, Any] | None,
|
||||
health_server_enabled: bool,
|
||||
unconfigured_provider_error: str | None,
|
||||
webui_dev_server: WebUIDevServer | None,
|
||||
update_startup: "GatewayStartup | None" = None,
|
||||
update_profile: "ProfileReceipt | None" = None,
|
||||
) -> None:
|
||||
"""Initialize and run only while the caller holds the gateway lifecycle claim."""
|
||||
from nanobot.agent.model_presets import load_model_preset_catalog
|
||||
from nanobot.agent.tools.message import MessageTool
|
||||
from nanobot.agent.turn_delivery import TurnDeliveryFactory
|
||||
@@ -455,7 +515,6 @@ def _run_gateway(
|
||||
from nanobot.triggers.local_store import LocalTriggerStore
|
||||
from nanobot.webui.recovery import RecoveryCoordinator
|
||||
|
||||
port = port if port is not None else config.gateway.port
|
||||
webui_url = _webui_browser_url(config)
|
||||
gateway_host_for_browser = _host_for_local_browser(config.gateway.host)
|
||||
if health_server_enabled and _tcp_endpoint_reachable(gateway_host_for_browser, port):
|
||||
@@ -514,19 +573,12 @@ def _run_gateway(
|
||||
session_manager = SessionManager(config.workspace_path)
|
||||
|
||||
# Use the same runtime identity for foreground and managed gateway processes.
|
||||
from nanobot.config.loader import get_config_path
|
||||
from nanobot.gateway.runtime import (
|
||||
GatewayClientLease,
|
||||
GatewayRuntime,
|
||||
monitor_gateway_clients,
|
||||
)
|
||||
|
||||
instance = gateway_instance or GatewayInstance.resolve(
|
||||
config_path=get_config_path(),
|
||||
)
|
||||
config_path = str(instance.config_path)
|
||||
gateway_runtime = GatewayRuntime(paths=instance.paths)
|
||||
gateway_start_options = instance.start_options(port=port)
|
||||
config_path = str(gateway_instance.config_path)
|
||||
|
||||
# Preserve existing single-workspace installs, but keep custom workspaces clean.
|
||||
if is_default_workspace(config.workspace_path):
|
||||
@@ -534,7 +586,7 @@ def _run_gateway(
|
||||
|
||||
# Create cron service with workspace-scoped store
|
||||
cron_store_path = config.workspace_path / "cron" / "jobs.json"
|
||||
cron = CronService(cron_store_path)
|
||||
cron = CronService(cron_store_path, admission=bus.admission)
|
||||
trigger_store = LocalTriggerStore(config.workspace_path)
|
||||
|
||||
webui_route_policy = WebuiTurnRoutePolicy(session_manager)
|
||||
@@ -851,6 +903,8 @@ def _run_gateway(
|
||||
|
||||
if method == "GET" and path == "/health":
|
||||
ready, payload = _gateway_readiness_payload(channels)
|
||||
payload["runtime"] = asdict(health_identity)
|
||||
payload["maintenance"] = bus.admission.paused
|
||||
body = _json.dumps(payload)
|
||||
status = "200 OK" if ready else "503 Service Unavailable"
|
||||
content_type = "application/json"
|
||||
@@ -957,6 +1011,8 @@ def _run_gateway(
|
||||
console.print(f"[yellow]Could not open browser ({e}); visit {display_url}[/yellow]")
|
||||
|
||||
async def run() -> None:
|
||||
startup_gate = ExitStack()
|
||||
desktop_activated = asyncio.Event()
|
||||
tasks: list[asyncio.Task[Any]] = []
|
||||
shutdown_task: asyncio.Task[Any] | None = None
|
||||
runtime_tasks: asyncio.Future[list[Any]] | None = None
|
||||
@@ -971,15 +1027,20 @@ def _run_gateway(
|
||||
)
|
||||
try:
|
||||
await agent.subagents.recover_interrupted()
|
||||
await cron.start()
|
||||
# Re-read once on first admission to close the watcher subscription window.
|
||||
agent.runtime_resolver.invalidate()
|
||||
# Recovery must finish before WebSocket and other channels begin
|
||||
# accepting new input. That makes a new user message reliably
|
||||
# supersede an old recoverable turn instead of racing its queue.
|
||||
await recovery.scan()
|
||||
if update_startup is not None:
|
||||
# Recovery may reconstruct queued work, but no consumer or
|
||||
# external listener has started. Keep that work parked too.
|
||||
startup_gate.enter_context(bus.admission.pause_before_start())
|
||||
await cron.start()
|
||||
async def _run_agent() -> None:
|
||||
try:
|
||||
await bus.admission.wait_until_open()
|
||||
await mcp_provider.connect()
|
||||
await agent.run()
|
||||
finally:
|
||||
@@ -1012,6 +1073,7 @@ def _run_gateway(
|
||||
store=trigger_store,
|
||||
submit_turn=agent.submit_local_trigger_turn,
|
||||
is_channel_enabled=lambda name: channels.get_channel(name) is not None,
|
||||
admission=bus.admission,
|
||||
),
|
||||
name="nanobot-local-triggers",
|
||||
),
|
||||
@@ -1020,6 +1082,28 @@ def _run_gateway(
|
||||
name="nanobot-gateway-client-monitor",
|
||||
),
|
||||
])
|
||||
if update_startup is not None:
|
||||
async def activate(startup: "GatewayStartup") -> None:
|
||||
await startup.wait_for_activation(health_identity)
|
||||
startup_gate.close()
|
||||
desktop_activated.set()
|
||||
|
||||
tasks.append(asyncio.create_task(activate(update_startup), name="nanobot-update-activation"))
|
||||
else:
|
||||
desktop_activated.set()
|
||||
if _webui_channel_enabled(config):
|
||||
tasks.append(asyncio.create_task(
|
||||
watch_gateway_icon(webui_url, instance=gateway_instance.paths.state_path,
|
||||
activated=desktop_activated),
|
||||
name="nanobot-dock",
|
||||
))
|
||||
if update_profile is not None:
|
||||
from nanobot.update_stop import watch_gateway_stop
|
||||
|
||||
tasks.append(asyncio.create_task(
|
||||
watch_gateway_stop(update_profile, agent.maintenance, shutdown_event, startup_gate),
|
||||
name="nanobot-update-stop",
|
||||
))
|
||||
if health_server_enabled:
|
||||
tasks.append(asyncio.create_task(
|
||||
_health_server(config.gateway.host, port),
|
||||
@@ -1085,12 +1169,12 @@ def _run_gateway(
|
||||
)
|
||||
await bus.drain()
|
||||
finally:
|
||||
# On a failed/aborted startup, keep intake closed until all
|
||||
# runtime consumers have been cancelled and cleanup finished.
|
||||
startup_gate.close()
|
||||
restore_shutdown_handlers()
|
||||
|
||||
with (
|
||||
gateway_runtime.foreground_instance(gateway_start_options),
|
||||
webui_turn_coordinator.connected(),
|
||||
):
|
||||
with webui_turn_coordinator.connected():
|
||||
if health_server_enabled:
|
||||
gateway_runtime.publish_health_host(config.gateway.host)
|
||||
asyncio.run(run())
|
||||
Reference in new issue
Block a user