Files
nanobot/nanobot/update_handoff.py
T
Xubin Renandchengyongru fbb61a373f 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>
2026-10-21 19:16:56 +08:00

58 lines
2.4 KiB
Python

"""Old-runtime preflight for the immutable, detached update owner.
Entered by the frozen worker only after taking this installation's shared lease.
This child must exit before the base-Python parent approves package mutation.
No client route calls it, and it never launches an installer itself.
"""
from __future__ import annotations
import asyncio
from pathlib import Path
from nanobot.data_access import require_no_pending_data_update
from nanobot.installation_lock import installation_state_dir
from nanobot.release_feed import ReleaseFeed
from nanobot.update_plan import UpdatePlan
from nanobot.update_stop import stop_reviewed_gateways
from nanobot.update_worker import (
JobStatus,
Phase,
WorkerSpec,
check_environment,
current_environment,
)
async def _prepare(directory: Path) -> None:
spec = WorkerSpec.read(directory)
plan = UpdatePlan.model_validate_json((directory / "plan.json").read_bytes())
status = JobStatus.read(directory)
if status.phase != Phase.CHECKING or status.mutation_started:
raise ValueError("Shutdown preflight requires an unchanged prepared job")
try:
plan.validate_for(plan.installation)
# This process already owns the old runtime's lease, including startup
# and atexit. Do not create an unprotected nested interpreter here.
check_environment(spec, repairing=False, current=current_environment(uv=spec.uv is not None))
async with ReleaseFeed(installation_state_dir(spec.baseline.environment) / "releases") as feed:
await feed.revalidate(plan.target)
plan.validate_for(plan.installation)
require_no_pending_data_update()
status.advance(directory, Phase.STOPPING, "Waiting for the reviewed idle gateways to exit normally", mutation_started=False)
await stop_reviewed_gateways(directory)
except Exception:
current = JobStatus.read(directory)
message = (
"Release or installation revalidation failed; gateway shutdown was not requested"
if current.phase == Phase.CHECKING else
"Gateway shutdown did not complete; review the saved shutdown state before retrying"
)
status.advance(directory, Phase.HANDOFF_FAILED, message, mutation_started=False)
raise
def prepare_handoff(directory: Path) -> None:
"""Revalidate and cooperatively stop; installation remains unapproved."""
asyncio.run(_prepare(directory))