Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

12.6 KB
Raw Blame
"""Prepare and activate a native Pixel update without replacing owner data."""
import argparse
import contextlib
import importlib.util
import io
import json
import os
from pathlib import Path
import pwd
import shutil
import subprocess
import sys
import tempfile

HERE = Path(__file__).resolve().parent
# Preparation builds and checks the new sandbox image before activation, so
# these failures leave the installed Pixel running unchanged. The recovery is
# to run the update again, never a reinstall (see the trailer in main()).
SANDBOX_GUIDANCE = {
    'native-sandbox-build-failed':
        'Pixel could not build the agent sandbox image for this update. The build downloads Debian '
        'packages from deb.debian.org and the pinned Node.js runtime from nodejs.org. Check that this '
        'Mac can reach both and that Docker has free disk space, then run the update again.',
    'native-sandbox-execution-proof-failed':
        'The new agent sandbox image failed its offline check: workspace write, the pinned Node.js for '
        'linux/arm64, and no npm, npx or corepack. Check that Docker runs linux/arm64 images. Removing '
        'the ods-pixel-native-sandbox image for this Pixel version (docker image ls '
        'ods-pixel-native-sandbox) is safe because it holds no Pixel state. Then run the update again.',
    'invalid-sandbox-node-runtime':
        'The selected Pixel source does not pin the Node.js runtime this ODS version expects in the '
        'agent sandbox. Update with the Pixel source bundled with ODS (leave PIXEL_SOURCE_REF unset).',
}
SANDBOX_ERRORS = frozenset(SANDBOX_GUIDANCE)
HELP = 'Need help? https://discord.gg/4ntNp9MAwC'


def helper(name):
    spec = importlib.util.spec_from_file_location('native_update_' + name, HERE / (name + '.py'))
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module


def activation_command(preparation, prepared, *, install_dir, ods_source, owner, transport):
    if prepared.get('status') != 'prepared' or prepared.get('installDir') != str(install_dir):
        raise ValueError('prepared-native-update-required')
    if prepared.get('runtimeDigest') == prepared.get('currentDigest'):
        raise ValueError('native-update-has-identical-runtime')
    command = ['/usr/bin/sudo', '/usr/bin/python3', str(HERE / 'pixel-macos-access-install.py'), 'migrate-native',
        '--source', str(ods_source), '--install-dir', str(install_dir), '--owner', owner,
        '--candidate', str(preparation / 'candidate'), '--runtime-bundle', str(preparation / 'runtime'),
        '--bundle-digest', prepared['runtimeDigest'], '--current-bundle-digest', prepared['currentDigest'],
        '--services-bundle', str(preparation / 'services'), '--services-digest', prepared['serviceDigest'],
        '--pixel-source-ref', prepared['pixelSourceRef'], '--gateway-port', str(prepared['gatewayPort']),
        '--access-port', str(prepared['accessPort']), '--activate']
    for flag, key in (('docker', 'docker'), ('compose-project', 'project'),
                      ('ingress-image', 'image'), ('ingress-user', 'user')):
        command.extend(['--' + flag, transport[key]])
    return command


def _portal_agent(document):
    """The candidate profile's one Portal agent entry.

    A malformed document (agents not a list, or an entry that is not an
    object) is a ValueError like any other wrong profile, never an
    AttributeError that would escape after the proved update.
    """
    agents = document['agents']['list'] if isinstance(document, dict) and isinstance(document.get('agents'), dict) else None
    if (not isinstance(agents, list) or len(agents) != 1 or not isinstance(agents[0], dict)
            or agents[0].get('id') != 'pixel'):
        raise ValueError('native-portal-profile-agent-required')
    return agents[0]


def migrate_public_identity(*, source, preparation, node):
    """Refresh only unmodified legacy profiles after a proved native update.

    This owner-side profile repair is intentionally outside the protected runtime
    activation transaction: failure leaves the activated runtime and owner data
    untouched, and is reported for manual review instead of claiming rollback.
    """
    try:
        candidate = preparation / 'candidate'
        document = helper('pixel-native-config').private_json(candidate / 'openclaw.json')
        workspace = Path(_portal_agent(document)['workspace'])
        if not workspace.is_absolute() or not workspace.is_dir() or workspace.resolve(strict=True) != workspace:
            raise ValueError('native-portal-profile-workspace-required')
        generated = candidate / 'workspace'
        script = source / 'scripts/migrate-portal-identity.mjs'
        if not generated.is_dir() or not script.is_file():
            raise ValueError('native-portal-profile-candidate-required')
        result = subprocess.run([str(node), str(script), str(workspace), str(generated)],
            capture_output=True, text=True, timeout=30, check=False)
        if result.returncode:
            raise ValueError('native-portal-profile-migration-failed')
        guidance = helper('pixel-native-config').migrate_workspace_guidance(workspace)
        return {'status': 'manual-review-required' if 'manual-review-required' in guidance.values() else 'checked',
                'detail': result.stdout.strip(), 'workspaceGuidance': guidance}
    except (ValueError, OSError, KeyError, TypeError, subprocess.SubprocessError):
        return {'status': 'manual-review-required'}


def add_example_projects(*, preparation, state_dir):
    """Add the shipped example projects whenever Playground/elf-of-doom is absent
    and this workspace has never received it.

    Like the profile repair, this owner-side step runs after the proved update
    and outside the protected activation. It only creates an absent
    Playground/<name> and the record of received examples in Portal's state
    folder (state_dir), so an example the owner deleted or renamed is never
    added again; failure changes no owner content. Nothing reaches the
    terminal: the helper's own notes are captured, and the outcome with them
    goes only to the update's private log (see log_example_projects).
    """
    notes = io.StringIO()
    try:
        with contextlib.redirect_stdout(notes), contextlib.redirect_stderr(notes):
            document = helper('pixel-native-config').private_json(preparation / 'candidate/openclaw.json')
            agent = _portal_agent(document)
            spec = importlib.util.spec_from_file_location('native_update_example_projects',
                HERE.parents[1] / 'lib/pixel-example-projects.py')
            module = importlib.util.module_from_spec(spec)
            spec.loader.exec_module(module)
            outcome = module.add_projects(Path(agent['workspace']), Path(state_dir))
    except (ValueError, OSError, KeyError, TypeError) as error:
        outcome = {'status': 'manual-review-required', 'cause': f'{type(error).__name__}: {error}'}
    if notes.getvalue().strip():
        outcome = {**outcome, 'notes': notes.getvalue().strip().splitlines()}
    return outcome


def log_example_projects(work, outcome):
    """Keep the example step's outcome in this update's private folder only.

    Nothing about the example is printed: Portal's sidebar simply lists it.
    The folder is the one the update names as its preparation.
    """
    try:
        with (Path(work) / 'example-projects.log').open('x', encoding='utf-8') as stream:
            os.fchmod(stream.fileno(), 0o600)
            stream.write(json.dumps(outcome, sort_keys=True) + '\n')
    except OSError:
        # The update itself succeeded; this outcome is diagnostic only, and
        # failing here would report a finished update as stopped.
        return


def update(*, install_dir, ods_source, prepare_only=False, prompt_for_sudo=False):
    if sys.platform != 'darwin' or os.geteuid() == 0:
        raise ValueError('native-macos-owner-required')
    install_dir, ods_source = Path(install_dir).resolve(strict=True), Path(ods_source).resolve(strict=True)
    stack = helper('pixel-native-stack')
    stack.resolve_files(install_dir, [])
    previous, _ = stack.read_selection(install_dir / 'data/pixel-native/preparation')
    installer = helper('pixel-macos-access-install')
    owner = pwd.getpwuid(os.getuid()).pw_name
    _, environment, _, _, node, _ = installer._source_gateway(installer._launchd.GATEWAY_PLIST, owner, 18789)
    if node.parent.name != previous['runtimeDigest']:
        raise ValueError('installed-native-selection-drift')
    transport = {key: environment[name] for key, name in (
        ('docker', 'PIXEL_HISTORY_DOCKER'), ('project', 'PIXEL_HISTORY_PROJECT'),
        ('image', 'PIXEL_HISTORY_IMAGE'), ('user', 'PIXEL_HISTORY_USER'))}
    installer._native_transport_environment(transport, pwd.getpwuid(os.getuid()))
    if not shutil.which(transport['docker']):
        raise ValueError('installed-docker-required')
    # Pin all subprocesses to the gateway's selected local Docker endpoint.
    endpoint = environment.get('DOCKER_HOST', '')
    if not endpoint.startswith('unix:///') or not Path(endpoint[7:]).is_socket():
        raise ValueError('installed-local-docker-socket-required')
    saved = {key: os.environ.get(key) for key in ('DOCKER_HOST', 'DOCKER_CONTEXT', 'DOCKER_TLS_VERIFY', 'DOCKER_CERT_PATH')}
    for key in saved:
        os.environ.pop(key, None)
    os.environ['DOCKER_HOST'] = endpoint
    try:
        initial = helper('pixel-native-install')
        node, npm = initial.node_tools()
        root = install_dir / 'data/pixel-native'
        work = Path(tempfile.mkdtemp(prefix='update-', dir=root))
        print('Native update preparation: ' + str(work), flush=True)
        config = helper('pixel-native-config')
        source = config.bootstrap.acquire_source(ref=initial.DEFAULT_REF,
            destination=work / 'source',
            source_url=str(ods_source / 'vendor/pixel.bundle'))
        runtime = work / 'acquired-runtime'
        config.bootstrap.stage(source=source, ref=initial.DEFAULT_REF, destination=runtime, node=node, npm=npm)
        preparation = work / 'preparation'
        helper('pixel-native-prepare').prepare_migration(source=source, ref=initial.DEFAULT_REF,
            node=node, runtime=runtime, docker=transport['docker'], ods_source=ods_source,
            install_dir=install_dir, destination=preparation, prompt_for_sudo=prompt_for_sudo)
        prepared = config.private_json(preparation / 'preparation.json')
        command = activation_command(preparation, prepared, install_dir=install_dir,
            ods_source=ods_source, owner=owner, transport=transport)
        if prepare_only:
            return {'status': 'prepared', 'preparation': str(preparation)}
        subprocess.run(command, check=True, timeout=1800)
        outcome = helper('pixel-native-finalize').finalize_update(preparation)
        outcome['portalIdentityMigration'] = migrate_public_identity(
            source=source, preparation=preparation, node=node)
        log_example_projects(work, add_example_projects(preparation=preparation,
            state_dir=install_dir / 'data/pixel-native/home/.openclaw'))
        return outcome
    finally:
        for key, value in saved.items():
            if value is None:
                os.environ.pop(key, None)
            else:
                os.environ[key] = value


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--install-dir', required=True)
    parser.add_argument('--ods-source', required=True)
    parser.add_argument('--prepare-only', action='store_true')
    parser.add_argument('--prompt-for-sudo', action='store_true',
        help='Allow read-only protected verification to prompt on a controlling terminal')
    args = parser.parse_args()
    try:
        print(json.dumps(update(**vars(args))))
    except (ValueError, OSError, KeyError, TypeError, subprocess.SubprocessError) as error:
        if str(error) == 'native-migration-proof-or-authorization-failed':
            print('Protected runtime verification or sudo authorization failed. Retain the preparation; '
                'review the protected runtime, or run sudo -v and retry from the same terminal. '
                'Interactive updates may use --prompt-for-sudo.', file=sys.stderr)
        if str(error) in SANDBOX_ERRORS:
            print(SANDBOX_GUIDANCE[str(error)] + ' The installed Pixel keeps running unchanged. ' + HELP,
                file=sys.stderr)
        print('Native update stopped (' + type(error).__name__ +
            '); retain its preparation and protected recovery journal. Do not reinstall or delete state.', file=sys.stderr)
        return 1
    return 0


if __name__ == '__main__':
    raise SystemExit(main())