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.

23.6 KB
Raw Blame
import hashlib
import base64
import importlib.util
import json
import sys
import subprocess
import shutil
from types import SimpleNamespace
from pathlib import Path
# Initialize urllib's host-specific proxy backend before tests emulate Darwin.
import urllib.request  # noqa: F401

import pytest

SPEC = importlib.util.spec_from_file_location('native_finalize',
    Path(__file__).resolve().parents[1] / 'installers/macos/lib/pixel-native-finalize.py')
module = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(module)


@pytest.mark.parametrize('fault', [None, 'current', 'install', 'runtime', 'services', 'pending', 'phase'])
def test_update_retains_storage_and_supports_verified_replay(fault):
    previous = dict(runtimeDigest='a' * 64, serviceDigest='b' * 64,
        storageDigest='retained', home='/owner/home', kind='legacy-native')
    activation = dict(status='ready', phase='services-ready',
        runtimeDigest='a' * 64, serviceDigest='b' * 64, storageDigest='retained')
    prepared = dict(kind='legacy-native', status='prepared', phase='awaiting-joint-activation',
        currentDigest='a' * 64, runtimeDigest='c' * 64, serviceDigest='d' * 64,
        pixelSourceRef='e' * 40, installDir='/owner/ods')
    proof = dict(status='active', runtimeDigest='c' * 64, serviceDigest='d' * 64)
    if fault == 'current': prepared['currentDigest'] = 'e' * 64
    if fault == 'install': prepared['installDir'] = '/other'
    if fault == 'runtime': proof['runtimeDigest'] = 'e' * 64
    if fault == 'services': proof['serviceDigest'] = 'e' * 64
    if fault == 'pending': proof['status'] = 'pending'
    if fault == 'phase': activation['phase'] = 'error'
    if fault:
        with pytest.raises(ValueError):
            module.update_selection_records(previous, activation, prepared, proof, '/owner/ods')
    else:
        result = module.update_selection_records(previous, activation, prepared, proof, '/owner/ods')
        assert result['preparation']['home'] == previous['home']
        assert result['preparation']['pixelSourceRef'] == prepared['pixelSourceRef']
        assert result['preparation']['storageDigest'] == result['activation']['storageDigest'] == 'retained'
        assert result['activation']['runtimeDigest'] == 'c' * 64
        assert module.update_selection_records(result['preparation'], result['activation'],
            prepared, proof, '/owner/ods') == result
        assert previous['runtimeDigest'] == 'a' * 64


@pytest.mark.parametrize('fault', [None, 'proof', 'replace', 'symlink-lock', 'directory-sync', 'clients'])
def test_update_publication_is_atomic_and_replayable(tmp_path, monkeypatch, fault):
    stack = module.helper('pixel-native-stack')
    installed = tmp_path / 'ods'
    directory = installed / 'data/pixel-native/preparation'
    directory.mkdir(parents=True)
    previous = dict(status='prepared', phase='awaiting-protected-activation',
        runtimeDigest='a' * 64, serviceDigest='b' * 64, home=str(installed / 'data/pixel-native/home'))
    activation = dict(status='ready', phase='services-ready', runtimeDigest='a' * 64, serviceDigest='b' * 64)
    for name, value in [('preparation.json', previous), ('activation.json', activation)]:
        (directory / name).write_text(json.dumps(value))
        (directory / name).chmod(0o600)
    for fragment in stack.installer.FRAGMENTS:
        path = installed / fragment
        path.parent.mkdir(parents=True, exist_ok=True)
        path.write_text('services: {}\n')
    candidate = tmp_path / 'candidate'
    candidate.mkdir()
    prepared = dict(kind='legacy-native', status='prepared', phase='awaiting-joint-activation',
        currentDigest='a' * 64, runtimeDigest='c' * 64, serviceDigest='d' * 64,
        pixelSourceRef='e' * 40, installDir=str(installed))
    (candidate / 'preparation.json').write_text(json.dumps(prepared))
    (candidate / 'preparation.json').chmod(0o600)
    proof = dict(status='active', runtimeDigest='c' * 64, serviceDigest='d' * 64)
    if fault == 'proof': proof['status'] = 'pending'
    monkeypatch.setattr(module.sys, 'platform', 'darwin')
    calls = []
    def verify(args, **kwargs):
        calls.append(args)
        assert '--verify-protected' in args
        return SimpleNamespace(stdout=json.dumps(proof))
    monkeypatch.setattr(module.subprocess, 'run', verify)
    refreshed = []
    held = []
    def refresh(path):
        assert path == installed
        assert held == [True]
        assert stack.read_selection(directory)[0]['runtimeDigest'] == 'c' * 64
        refreshed.append(True)
        if fault == 'clients' and len(refreshed) == 1: raise OSError('client recreation failed')
    monkeypatch.setattr(module, '_refresh_clients_held', refresh)
    original_helper = module.helper
    def transaction(path, **kwargs):
        assert path == installed and kwargs['selection_locked'] is True
        held.append(True)
        try:
            return kwargs['operation']({}, {}, lambda value: None)
        finally:
            held.pop()
    monkeypatch.setattr(module, 'helper', lambda name: SimpleNamespace(refresh_installed=transaction)
        if name == 'pixel-native-ingress-refresh' else original_helper(name))
    if fault == 'replace':
        def fail(*args): raise OSError('fixture publication failure')
        monkeypatch.setattr(module.os, 'replace', fail)
    if fault == 'symlink-lock':
        (directory / '.selection.lock').symlink_to(directory / 'preparation.json')
    if fault == 'clients':
        with pytest.raises(OSError): module.finalize_update(candidate)
        assert stack.read_selection(directory)[0]['runtimeDigest'] == 'c' * 64
        assert module.finalize_update(candidate)['status'] == 'selection-ready'
        assert len(refreshed) == 2
    elif fault == 'directory-sync':
        fsync = module.os.fsync
        interrupted = []
        def interrupt_once(fd):
            if (directory / stack.UPDATE_SELECTION).exists() and not interrupted:
                interrupted.append(True)
                raise OSError('fixture durability interruption')
            return fsync(fd)
        monkeypatch.setattr(module.os, 'fsync', interrupt_once)
        with pytest.raises(OSError): module.finalize_update(candidate)
        assert stack.read_selection(directory)[0]['runtimeDigest'] == 'c' * 64
        assert module.finalize_update(candidate)['status'] == 'selection-ready'
        assert len(calls) == 3
    elif fault:
        with pytest.raises((ValueError, OSError)):
            module.finalize_update(candidate)
        assert not (directory / stack.UPDATE_SELECTION).exists()
        assert stack.read_selection(directory)[0] == previous
    else:
        result = module.finalize_update(candidate)
        assert result['status'] == 'selection-ready'
        assert module.finalize_update(candidate) == result
        assert len(calls) == 4
        assert stack.read_selection(directory)[0]['runtimeDigest'] == 'c' * 64
        assert (directory / stack.UPDATE_SELECTION).stat().st_mode & 0o777 == 0o600
    assert json.loads((directory / 'preparation.json').read_text()) == previous
    assert json.loads((directory / 'activation.json').read_text()) == activation
    assert not list(directory.glob('.selection-*'))


@pytest.mark.parametrize('cached', [True, False])
@pytest.mark.parametrize('fault', [None, 'socket', 'flags', 'project', 'missing-dashboard', 'no-webui', 'legacy-map',
    'legacy-list', 'start', 'probe', 'escape', 'unsafe-recipe', 'recipe-alias'])
def test_refresh_clients_uses_native_stack_and_verifies_from_dashboard(tmp_path, monkeypatch, fault, cached):
    installed = tmp_path / 'ods'
    installed.mkdir()
    fragment = 'data/user-extensions/example/compose.yaml'
    flags = '--invalid' if fault == 'flags' else '-f base.yaml -f legacy.yaml -f ' + fragment
    if cached: (installed / '.compose-flags').write_text(flags)
    (installed / '.env').write_text('GPU_BACKEND="apple" # saved hardware\nODS_MODE=local\n')
    (installed / '.env').chmod(0o600)
    resolver = installed / 'scripts/resolve-compose-stack.sh'
    resolver.parent.mkdir()
    shutil.copyfile(Path(__file__).resolve().parents[1] / 'scripts/resolve-compose-stack.sh', resolver)
    recipe = installed / fragment
    recipe.parent.mkdir(parents=True)
    recipe.write_text(json.dumps({'services': {'example': {'image': 'example/app:1',
        **({'privileged': True} if fault in ('unsafe-recipe', 'recipe-alias') else {})}}}))
    if fault == 'recipe-alias':
        target = installed / 'unreviewed.yaml'
        recipe.rename(target)
        recipe.symlink_to(target)
    (tmp_path / 'outside.yaml').write_text('services: {}')
    for name in ('base.yaml', 'native.yaml'): (installed / name).write_text('services: {}')
    environment = dict(PIXEL_HISTORY_DOCKER='/docker', PIXEL_HISTORY_PROJECT='ods',
        PIXEL_HISTORY_IMAGE='fixture', PIXEL_HISTORY_USER='501:20', DOCKER_HOST='unix:///local.sock')
    installer = SimpleNamespace(_source_gateway=lambda *a: (None, environment, None, None, None, None),
        _native_transport_environment=lambda *a: None, _launchd=SimpleNamespace(GATEWAY_PLIST='/gateway.plist'))
    def resolve(path, files):
        assert path == installed and files == ['base.yaml', 'legacy.yaml', fragment]
        return ['../outside.yaml'] if fault == 'escape' else ['base.yaml', 'native.yaml', fragment]
    stack = SimpleNamespace(resolve_files=resolve, read_selection=lambda path: ({'runtimeDigest': 'a' * 64}, {}))
    native_env = module.helper('pixel-native-env')
    native_compose = module.helper('pixel-native-compose')
    monkeypatch.setattr(module, 'helper', lambda name: {
        'pixel-macos-access-install': installer, 'pixel-native-stack': stack,
        'pixel-native-env': native_env, 'pixel-native-compose': native_compose,
        'pixel-native-ingress-refresh': SimpleNamespace(refresh_installed=lambda *args, **kwargs:
            kwargs['operation']({}, {}, lambda value: None))}[name])
    monkeypatch.setattr(module.Path, 'is_socket', lambda path: fault != 'socket')
    monkeypatch.setenv('DOCKER_CONTEXT', 'remote')
    monkeypatch.setenv('DOCKER_TLS_VERIFY', '1')
    monkeypatch.setenv('DOCKER_CERT_PATH', '/remote/cert')
    document = dict(name='wrong' if fault == 'project' else 'ods',
        services={'dashboard-api': {}, 'open-webui': {}})
    if fault == 'missing-dashboard': del document['services']['dashboard-api']
    if fault == 'no-webui': del document['services']['open-webui']
    if fault == 'legacy-map': document['services']['dashboard-api']['extra_hosts'] = {'pixel-edge': 'host-gateway'}
    if fault == 'legacy-list': document['services']['open-webui']['extra_hosts'] = ['Pixel-Edge=host-gateway']
    calls = []
    resolutions = []
    def run(command, **kwargs):
        if command[0] == '/bin/bash':
            resolutions.append(command)
            assert not cached
            assert command == ['/bin/bash', str(resolver), '--script-dir', str(installed),
                '--tier', '1', '--gpu-backend', 'apple', '--gpu-count', '1', '--ods-mode', 'local']
            assert kwargs['env']['GPU_BACKEND'] == 'apple'
            assert kwargs['env']['DOCKER_HOST'] == 'unix:///local.sock'
            assert kwargs['check'] is True and kwargs['timeout'] == 30
            return SimpleNamespace(stdout=flags)
        calls.append(command)
        assert str(installed / 'native.yaml') in command
        assert str(installed / 'legacy.yaml') not in command
        assert kwargs['env']['DOCKER_HOST'] == 'unix:///local.sock'
        assert not {'DOCKER_CONTEXT', 'DOCKER_TLS_VERIFY', 'DOCKER_CERT_PATH'} & set(kwargs['env'])
        if (fault == 'start' and 'up' in command) or (fault == 'probe' and 'exec' in command):
            raise subprocess.CalledProcessError(1, command)
        return SimpleNamespace(stdout=json.dumps(document))
    monkeypatch.setattr(module.subprocess, 'run', run)
    if fault and fault != 'no-webui':
        with pytest.raises((ValueError, subprocess.CalledProcessError)): module.refresh_clients(installed)
        if fault not in ('start', 'probe'): assert not any('up' in call for call in calls)
        if fault in ('unsafe-recipe', 'recipe-alias'):
            assert not calls
    else:
        module.refresh_clients(installed)
        assert len(calls) == 3
        if fault == 'no-webui':
            assert calls[1][-7:] == ['up', '-d', '--no-deps', '--wait', '--wait-timeout',
                '120', 'dashboard-api']
        else:
            assert calls[1][-8:] == ['up', '-d', '--no-deps', '--wait', '--wait-timeout',
                '120', 'dashboard-api', 'open-webui']
        assert calls[2][-6:-1] == ['exec', '-T', 'dashboard-api', 'python3', '-c']
        assert 'http://pixel-edge:9595/health' in calls[2][-1]
    assert len(resolutions) == (0 if cached or fault == 'socket' else 1)
    assert (installed / '.compose-flags').exists() == cached
    if cached: assert (installed / '.compose-flags').read_text() == flags


@pytest.mark.parametrize('fault', [None, 'missing-gpu', 'invalid-gpu', 'duplicate', 'bad-count',
    'bad-mode', 'resolver-missing', 'resolver-error', 'resolver-timeout', 'empty', 'flags',
    'corrupt-cache', 'dangling-cache', 'none', 'arc', 'suppressed-env', 'legacy-skip'])
def test_missing_cache_resolves_saved_selection_fail_closed(tmp_path, monkeypatch, fault):
    values = dict(TIER='AP_PRO', GPU_BACKEND='apple', GPU_COUNT='2', ODS_MODE='cloud',
        WHISPER_ACCELERATION='cpu', EXTERNAL_LLM_URL='http://localhost:1234',
        ODS_SKIP_GPU_OVERLAYS='whisper')
    if fault == 'missing-gpu': del values['GPU_BACKEND']
    if fault == 'invalid-gpu': values['GPU_BACKEND'] = '$(false)'
    if fault in ('none', 'arc'): values['GPU_BACKEND'] = fault
    if fault in ('suppressed-env', 'legacy-skip'):
        del values['ODS_SKIP_GPU_OVERLAYS']
        del values['EXTERNAL_LLM_URL']
    if fault == 'legacy-skip': values['ODS_SKIP_GPU_OVERLAYS_FOR'] = 'whisper'
    if fault == 'bad-count': values['GPU_COUNT'] = 'many'
    if fault == 'bad-mode': values['ODS_MODE'] = 'unknown'
    text = ''.join(key + '=' + value + '\n' for key, value in values.items())
    if fault == 'duplicate': text += 'GPU_BACKEND=cpu\n'
    marker = tmp_path / 'must-not-exist'
    text += 'UNRELATED=$(touch ' + str(marker) + ')\nBASH_ENV=/untrusted\n'
    env_path = tmp_path / '.env'
    env_path.write_text(text)
    env_path.chmod(0o600)
    resolver = tmp_path / 'scripts/resolve-compose-stack.sh'
    resolver.parent.mkdir()
    if fault != 'resolver-missing': resolver.write_text('# fixture')
    cache = tmp_path / '.compose-flags'
    if fault == 'corrupt-cache': cache.write_text('--bad')
    if fault == 'dangling-cache': cache.symlink_to(tmp_path / 'absent')
    calls = []
    def run(command, **kwargs):
        calls.append(command)
        assert command[-8:] == ['--tier', 'AP_PRO', '--gpu-backend', values['GPU_BACKEND'],
            '--gpu-count', '2', '--ods-mode', 'cloud']
        assert kwargs['env']['GPU_BACKEND'] == values['GPU_BACKEND']
        assert kwargs['env']['WHISPER_ACCELERATION'] == 'cpu'
        for key in ('EXTERNAL_LLM_URL', 'ODS_SKIP_GPU_OVERLAYS', 'ODS_SKIP_GPU_OVERLAYS_FOR'):
            assert kwargs['env'][key] == values.get(key, '')
        assert kwargs['env']['NATIVE_LLM_BASE_URL'] == ''
        assert kwargs['env']['AMD_INFERENCE_BACKEND'] == ''
        assert 'UNRELATED' not in kwargs['env'] and 'BASH_ENV' not in kwargs['env']
        if fault == 'resolver-error': raise subprocess.CalledProcessError(1, command)
        if fault == 'resolver-timeout': raise subprocess.TimeoutExpired(command, 30)
        return SimpleNamespace(stdout='' if fault == 'empty' else
            '--bad' if fault == 'flags' else '-f base.yaml -f native.yaml')
    monkeypatch.setattr(module.subprocess, 'run', run)
    process_env = {'GPU_BACKEND': 'nvidia', 'NATIVE_LLM_BASE_URL': 'http://stale:8080',
        'AMD_INFERENCE_BACKEND': 'rocm',
        'EXTERNAL_LLM_URL': 'http://stale:1234', 'ODS_SKIP_GPU_OVERLAYS': 'stale',
        'ODS_SKIP_GPU_OVERLAYS_FOR': 'stale'}
    successes = (None, 'none', 'arc', 'suppressed-env', 'legacy-skip')
    if fault not in successes:
        with pytest.raises((ValueError, OSError, subprocess.SubprocessError)):
            module.compose_flags(tmp_path, process_env)
    else:
        assert module.compose_flags(tmp_path, process_env) == ['-f', 'base.yaml', '-f', 'native.yaml']
    assert len(calls) == (1 if fault in (*successes, 'resolver-error', 'resolver-timeout', 'empty', 'flags') else 0)
    assert env_path.read_text() == text
    assert not marker.exists()
    assert module.os.path.lexists(cache) == (fault in ('corrupt-cache', 'dangling-cache'))
    if fault == 'corrupt-cache': assert cache.read_text() == '--bad'


@pytest.mark.parametrize('scenario', [
    'absent', 'added', 'relative', 'absolute', 'lookalike', 'malformed',
])
def test_native_lifecycle_discovers_override_added_after_cache(tmp_path, monkeypatch, scenario):
    override = tmp_path / 'docker-compose.override.yml'
    if scenario != 'absent':
        override.write_text('services: {}\n')
    cached = '-f base.yaml'
    if scenario == 'relative':
        cached += ' -f docker-compose.override.yml'
    elif scenario == 'absolute':
        cached += ' -f ' + str(override)
    elif scenario == 'lookalike':
        cached += ' -f custom-docker-compose.override.yml'
    elif scenario == 'malformed':
        cached = '--bad'
    cache = tmp_path / '.compose-flags'
    cache.write_text(cached)
    env_file = tmp_path / '.env'
    env_file.write_text('GPU_BACKEND=apple\nTIER=1\nENABLE_OPEN_WEBUI=false\n')
    env_file.chmod(0o600)
    resolver = tmp_path / 'scripts/resolve-compose-stack.sh'
    resolver.parent.mkdir()
    resolver.write_text('# test resolver')
    calls = []

    def run(command, **kwargs):
        calls.append(command)
        assert command[:2] == ['/bin/bash', str(resolver)]
        assert kwargs['env']['ENABLE_OPEN_WEBUI'] == 'false'
        return SimpleNamespace(stdout='-f base.yaml -f docker-compose.override.yml')

    monkeypatch.setattr(module.subprocess, 'run', run)
    if scenario == 'malformed':
        with pytest.raises(ValueError):
            module.compose_flags(tmp_path, {})
        assert not calls
    else:
        flags = module.compose_flags(tmp_path, {'ENABLE_OPEN_WEBUI': 'true'})
        if scenario in ('added', 'lookalike'):
            assert flags == ['-f', 'base.yaml', '-f', 'docker-compose.override.yml']
            assert len(calls) == 1
        else:
            assert flags == cached.split()
            assert not calls
    assert cache.read_text() == cached
    assert env_file.read_text() == 'GPU_BACKEND=apple\nTIER=1\nENABLE_OPEN_WEBUI=false\n'


@pytest.mark.parametrize('saved,expected', [
    ('false', 'false'), ('true', 'true'), ('"false"', 'false'),
    (' FALSE ', 'false'), (None, 'true'), ('', None), ('maybe', None),
    ('false\nENABLE_OPEN_WEBUI=true', None),
])
def test_uncached_selection_retains_webui_choice(tmp_path, monkeypatch, saved, expected):
    env_file = tmp_path / '.env'
    body = 'GPU_BACKEND=apple\nTIER=1\n'
    if saved is not None:
        body += 'ENABLE_OPEN_WEBUI=' + saved + '\n'
    env_file.write_text(body)
    env_file.chmod(0o600)
    resolver = tmp_path / 'scripts/resolve-compose-stack.sh'
    resolver.parent.mkdir()
    resolver.write_text('# test resolver')
    calls = []

    def run(command, **kwargs):
        calls.append(command)
        assert kwargs['env']['ENABLE_OPEN_WEBUI'] == expected
        return SimpleNamespace(stdout='-f base.yaml' +
            (' -f docker-compose.gateway-only.yml' if expected == 'false' else ''))

    monkeypatch.setattr(module.subprocess, 'run', run)
    # A stale caller must not reverse either the saved Core or Full selection.
    process_env = {'ENABLE_OPEN_WEBUI': 'true' if expected == 'false' else 'false'}
    if expected is None:
        with pytest.raises(ValueError):
            module.compose_flags(tmp_path, process_env)
        assert not calls
    else:
        flags = module.compose_flags(tmp_path, process_env)
        assert ('docker-compose.gateway-only.yml' in flags) == (expected == 'false')
        assert len(calls) == 1
    assert env_file.read_text() == body
    assert not (tmp_path / '.compose-flags').exists()


@pytest.mark.parametrize('fault', [None, 'runtime', 'services', 'storage', 'install', 'pending', 'inactive'])
def test_selection_binds_corrected_services_to_completed_docker_handover(fault):
    prepared = dict(kind='legacy-native', status='prepared', phase='awaiting-joint-activation',
        runtimeDigest='a' * 64, currentDigest='b' * 64, serviceDigest='c' * 64, installDir='/owners/test/ods')
    storage = {'volumes': {'pixel-native-runtime': {'external': True, 'name': 'retained-history'}}}
    digest = hashlib.sha256(json.dumps(storage, sort_keys=True, separators=(',', ':')).encode()).hexdigest()
    docker = {**prepared, 'serviceDigest': 'd' * 64, 'storageDigest': digest,
        'environmentStatus': 'configured'}
    journal = {'phase': 'infrastructure-ready', 'requiresRecovery': False}
    proof = {'status': 'active', 'runtimeDigest': prepared['runtimeDigest'], 'serviceDigest': prepared['serviceDigest']}
    if fault == 'runtime': docker['runtimeDigest'] = 'e' * 64
    if fault == 'services': proof['serviceDigest'] = docker['serviceDigest']
    if fault == 'storage': storage['volumes']['other'] = {}
    if fault == 'install': docker['installDir'] = '/other'
    if fault == 'pending': journal['requiresRecovery'] = True
    if fault == 'inactive': proof['status'] = 'restored'
    if fault:
        with pytest.raises(ValueError):
            module.selection_records(prepared, docker, journal, storage, proof)
    else:
        receipt, activation = module.selection_records(prepared, docker, journal, storage, proof)
        assert receipt['serviceDigest'] == activation['serviceDigest'] == prepared['serviceDigest']
        assert receipt['storageDigest'] == activation['storageDigest'] == digest
        assert 'environmentStatus' not in receipt
        assert activation['status'] == 'ready'


@pytest.mark.parametrize('fault', [None, 'pending', 'drift', 'service', 'stopped'])
def test_protected_proof_reads_completed_files_and_checks_live_services(monkeypatch, fault):
    sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'bin'))
    import pixel_access_bridge
    import pixel_macos_custody
    runtime, services, ref = 'a' * 64, 'b' * 64, 'c' * 40
    body = b'activated'
    completed = {'phase': 'active', 'candidateDigest': runtime, 'files': [{
        'path': '/protected/file', 'after': base64.b64encode(body).decode(),
        'afterSha256': hashlib.sha256(body).hexdigest()}]}
    selection = {'expected_digest': services if fault != 'service' else 'wrong', 'expected_ref': ref}
    monkeypatch.setattr(module.sys, 'platform', 'darwin')
    monkeypatch.setattr(module.os, 'geteuid', lambda: 0)
    monkeypatch.setattr(module.os.path, 'lexists', lambda path: fault == 'pending')
    monkeypatch.setattr(pixel_access_bridge, 'private_json', lambda path, *args:
        {'selection': selection} if path.name == 'service-installation.json' else completed)
    monkeypatch.setattr(pixel_macos_custody, 'protected_bytes',
        lambda *args, **kwargs: b'changed' if fault == 'drift' else body)
    monkeypatch.setattr(module.pwd, 'getpwnam', lambda owner: SimpleNamespace(pw_uid=501))
    checked = []
    monkeypatch.setattr(module, 'helper', lambda name: SimpleNamespace(
        _controller_repair_path=lambda digest: Path('/private/var/lib/ods-pixel-access') / ('runtime-controller-repair-' + digest + '.json'),
        _verify_new_services=lambda plan: checked.append(plan)))
    monkeypatch.setattr(module.subprocess, 'run', lambda *args, **kwargs:
        SimpleNamespace(stdout='state = waiting' if fault == 'stopped' else 'state = running\n'))
    if fault:
        with pytest.raises(ValueError): module.protected_proof('owner', runtime, services, ref)
    else:
        assert module.protected_proof('owner', runtime, services, ref)['status'] == 'active'
        assert len(checked) == 1