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.

8.7 KB
Raw Blame
"""Installer route changes must end obsolete observational model waits."""
import subprocess
import threading

import pytest
import test_model_activate as tma
from model_switchboard import state as sb


@pytest.fixture
def route(tmp_path, monkeypatch):
    install, env_path = tma._write_model_activation_fixture(tmp_path)[:2]
    monkeypatch.setattr(tma._mod, 'INSTALL_DIR', install)
    monkeypatch.setattr(tma._mod, '_model_lifecycle_operation', None)
    monkeypatch.setattr(tma._mod, '_switchboard_initial_verify_cancel', threading.Event())
    monkeypatch.delenv('ODS_HOST_INSTALL_DIR', raising=False)
    monkeypatch.setattr(tma._mod, '_chat_completion_ready', lambda *_a, **_k: True)
    monkeypatch.setattr(tma._mod, '_runtime_health', lambda _env: 'ok')
    monkeypatch.setattr(tma._mod, '_llama_runtime_props', lambda _env: (65536, '', None))
    monkeypatch.setattr(tma._mod, '_load_model_library_records', lambda: [])
    monkeypatch.setattr(tma._mod.time, 'sleep', lambda _s: None)
    state_path = install / 'data' / 'model-state.json'
    sb.initialize_if_missing(state_path, tma._mod.load_env(env_path))
    return env_path, state_path


def set_model(path, gguf, context='2048'):
    path.write_text('GPU_BACKEND=nvidia\nOLLAMA_PORT=8080\n'
                    f'GGUF_FILE={gguf}\nLLM_MODEL={gguf.removesuffix(".gguf")}\n'
                    f'CTX_SIZE={context}\n')


def fake_response(command, model):
    return subprocess.CompletedProcess(command, 0,
        stdout=tma._llama_identity_response(model), stderr='')


def test_initial_wait_exits_after_installer_selects_bootstrap_then_new_proof_binds(route, monkeypatch):
    env_path, state_path = route
    before = state_path.read_bytes()
    calls = []

    def probe(command, **_kwargs):
        calls.append(command)
        set_model(env_path, 'bootstrap-2b.gguf')
        return fake_response(command, 'bootstrap-2b.gguf')

    monkeypatch.setattr(tma._mod.subprocess, 'run', probe)
    assert not tma._mod._publish_verified_initial_switchboard_route(
        reason='startup', attempts=3, initial_delay=0, interval=0)
    assert len(calls) == 1
    assert state_path.read_bytes() == before
    assert not tma._mod._switchboard_initial_verify_cancel.is_set()

    # The existing status scheduler can admit a fresh observation after the
    # stale one exits. Exercise the publisher against the newly selected route.
    monkeypatch.setattr(tma._mod.subprocess, 'run',
                        lambda command, **_k: fake_response(command, 'bootstrap-2b.gguf'))
    assert tma._mod._publish_verified_initial_switchboard_route(
        reason='status', attempts=1, initial_delay=0, interval=0)
    doc, errors = sb.read_state(state_path)
    assert not errors
    assert doc['active']['runtimeModelId'] == 'bootstrap-2b.gguf'
    assert doc['active']['proof'] == {'identity': 'bootstrap-2b.gguf', 'completion': True}


@pytest.mark.parametrize('contract,empty', [({}, False), ({'return_identity': True}, ''), ({'return_proof': True}, {})])
def test_stale_snapshot_never_probes_or_warms(contract, empty, route, monkeypatch):
    env_path, _ = route
    env = tma._mod.load_env(env_path)
    set_model(env_path, 'bootstrap-2b.gguf')
    monkeypatch.setattr(tma._mod.subprocess, 'run',
                        lambda *_a, **_k: pytest.fail('stale runtime must not be probed'))
    result = tma._mod._wait_for_model_readiness(
        env, model_id='old-model', gguf_file='old-model.gguf', llm_model_name='old-model',
        attempts=2, initial_delay=0, interval=0,
        env_still_current=lambda: tma._mod._initial_switchboard_route_env_matches(env), **contract)
    assert result == empty


def test_context_change_during_proof_does_not_publish_obsolete_binding(route, monkeypatch):
    env_path, state_path = route
    before = state_path.read_bytes()

    def completed_proof(*_args, **_kwargs):
        set_model(env_path, 'old-model.gguf', context='32768')
        return {'identity': 'old-model.gguf', 'contextLength': 65536,
                'contextVerified': True, 'verifiedAt': '2026-09-26T17:00:00Z'}

    monkeypatch.setattr(tma._mod, '_wait_for_model_readiness', completed_proof)
    assert not tma._mod._publish_verified_initial_switchboard_route(reason='test')
    assert state_path.read_bytes() == before


def test_mismatched_runtime_remains_unverified(route, monkeypatch):
    _, state_path = route
    before = state_path.read_bytes()
    monkeypatch.setattr(tma._mod.subprocess, 'run',
                        lambda command, **_k: fake_response(command, 'wrong-model.gguf'))
    assert not tma._mod._publish_verified_initial_switchboard_route(
        reason='test', attempts=2, initial_delay=0, interval=0)
    assert state_path.read_bytes() == before


def test_explicit_wait_retains_requested_model_contract(route, monkeypatch):
    env_path, _ = route
    snapshot = tma._mod.load_env(env_path)
    set_model(env_path, 'bootstrap-2b.gguf')
    monkeypatch.setattr(tma._mod.subprocess, 'run',
                        lambda command, **_k: fake_response(command, 'old-model.gguf'))
    assert tma._mod._wait_for_model_readiness(
        snapshot, model_id='old-model', gguf_file='old-model.gguf', llm_model_name='old-model',
        attempts=1, initial_delay=0, interval=0, return_identity=True) == 'old-model.gguf'


def test_fast_poll_preserves_stale_route_predicate(route, monkeypatch):
    env_path, _ = route
    snapshot = tma._mod.load_env(env_path)
    calls = []

    def probe(command, **_kwargs):
        calls.append(command)
        set_model(env_path, 'bootstrap-2b.gguf')
        return fake_response(command, 'bootstrap-2b.gguf')

    monkeypatch.setattr(tma._mod.subprocess, 'run', probe)
    result = tma._mod._wait_for_model_readiness(
        snapshot, model_id='old-model', gguf_file='old-model.gguf', llm_model_name='old-model',
        attempts=2, initial_delay=0, interval=0, fast_poll_seconds=.1, fast_poll_interval=.05,
        return_proof=True,
        env_still_current=lambda: tma._mod._initial_switchboard_route_env_matches(snapshot))
    assert result == {}
    assert len(calls) == 1


def test_env_change_during_identity_probe_never_runs_the_old_completion(route, monkeypatch):
    env_path, _ = route
    snapshot = tma._mod.load_env(env_path)
    monkeypatch.setattr(tma._mod, '_chat_completion_ready',
                        lambda *_a, **_k: pytest.fail('installer selected bootstrap; old model must not be proved'))

    def probe(command, **_kwargs):
        set_model(env_path, 'bootstrap-2b.gguf')
        return fake_response(command, 'old-model.gguf')

    monkeypatch.setattr(tma._mod.subprocess, 'run', probe)
    assert tma._mod._wait_for_model_readiness(
        snapshot, model_id='old-model', gguf_file='old-model.gguf', llm_model_name='old-model',
        attempts=2, initial_delay=0, interval=0,
        return_proof=True,
        env_still_current=lambda: tma._mod._initial_switchboard_route_env_matches(snapshot)) == {}


@pytest.mark.parametrize('mode, tools', [('enabled', True), ('observe', False)])
def test_initial_route_republishes_measured_capabilities_only_when_enabled(route, monkeypatch, mode, tools):
    # Any-model WP4.1: after an install or upgrade the re-proved route keeps what
    # the model's last switch measured, not the catalog booleans (enabled only).
    env_path, state_path = route
    set_model(env_path, 'bootstrap-2b.gguf')
    env_path.write_text(env_path.read_text() + f'ODS_MODEL_PROFILES={mode}\n')
    store = tma._mod._model_profile_store
    key = store.profile_key(gguf_sha256=['a' * 64], projector_sha256=None, build_info='b11429-x',
                            backend='nvidia', template_sha256='b' * 64, template_source='embedded',
                            suite='3', host='host')
    summary = {'chat': True, 'tools': True, 'toolsStreamed': True, 'vision': None, 'tokensPerSecond': 1.0,
               'thinking': {'control': 'enable_thinking', 'separated': True, 'works': True}}
    profile = store.recorded_profile(key, model_id='bootstrap-2b', gguf_file='bootstrap-2b.gguf',
                                     result={'probes': {}, 'summary': summary, 'elapsedMs': 1},
                                     product_version='test')
    doc = store.with_last_activation(store.with_profile(store.empty(), profile), 'bootstrap-2b', profile['keyHash'])
    store.atomic_write(state_path.parent / 'model-profiles.json', doc)
    monkeypatch.setattr(tma._mod.subprocess, 'run',
                        lambda command, **_k: fake_response(command, 'bootstrap-2b.gguf'))

    assert tma._mod._publish_verified_initial_switchboard_route(
        reason='status', attempts=1, initial_delay=0, interval=0)
    state, errors = sb.read_state(state_path)
    assert not errors
    assert state['active']['catalogId'] == 'bootstrap-2b'
    assert state['active']['capabilities']['tools'] is tools