Files
semif-agent/tests/test_engine_config.py
gabby addbe53392 engine: add the ollama System One provider
A third decision-engine provider alongside the local SemIf GGUF and the
remote Winnow server: `engine.provider == "ollama"` talks to ollama's
native `/v1/systemone` route via `semif_agent/ollama_systemone.py`.

It reuses the Winnow DecisionRequest -> choice mapping and the same strict
distribution validation over exactly the requested option ids, and adds
the edges only ollama has:

- no `/health`: `warm()` probes `GET /api/version` + `POST /api/show`,
  which also proves the model is pulled and advertises the `decision`
  capability;
- `engine.ollama.context_tokens: 0` follows the model's real `num_ctx`
  from `/api/show`;
- `keep_alive` rides on the request, criteria descriptions use the ollama
  subset `string | null`, and a choice question needs >= 2 options;
- decision models do not generate text, so `generate()` raises
  `EngineUnavailable` (use `llm.provider == "ollama"` for chat).

Every failure is an `EngineUnavailable` (fatal, like the local engine) and
the engine lock still serializes on the single-slot server.

`cli.build_engine` selects it, the setup interview and `--answers` accept
it, and the install plan skips the engine clone/GGUF/tokenizer for it.
Docs, config.example.json, and tests updated.
2026-11-11 02:57:53 +00:00

144 lines
4.3 KiB
Python

"""Engine config wiring: the per-machine GPU-offload knobs and the loader patch.
Pure stdlib: no SemIf, no llama.cpp, no GGUF. The SemIf backend is CPU-only by
construction, so the patch that lets it offload is exercised against a fake
backend module.
"""
from __future__ import annotations
from types import SimpleNamespace
import pytest
from semif_agent.cli import build_engine, build_engine_config
from semif_agent.engine import (
DecisionEngine,
EngineConfig,
SemIfEngine,
_install_gpu_state_reset,
_offloading_params,
)
from semif_agent.ollama_systemone import OllamaSystemOneEngine
from semif_agent.winnow import WinnowEngine
PINS = {
"engine": {
"source": "Qwen/Qwen3.5-4B",
"revision": "0" * 40,
"gguf_url": "https://example.com/Qwen_Qwen3.5-4B-Q4_K_M.gguf",
}
}
def test_engine_config_defaults_to_cpu():
cfg = build_engine_config({"engine": {"backend": "llamacpp"}}, PINS)
assert cfg.gpu_layers == 0
assert cfg.gpu_device is None
assert cfg.gpu_split == 0
def test_engine_config_reads_gpu_knobs():
cfg = build_engine_config(
{"engine": {"gpu_layers": -1, "gpu_device": 1, "gpu_split": 0}}, PINS
)
assert cfg.gpu_layers == -1
assert cfg.gpu_device == 1
assert cfg.gpu_split == 0
def test_gpu_offload_wrapper_rewrites_model_params():
def original(library):
return SimpleNamespace(n_gpu_layers=0, main_gpu=0, split_mode=1)
offloading = _offloading_params(
original, EngineConfig(gpu_layers=99, gpu_device=1, gpu_split=0)
)
params = offloading(object())
assert params.n_gpu_layers == 99
assert params.main_gpu == 1
assert params.split_mode == 0
def test_gpu_offload_wrapper_leaves_main_gpu_when_device_unset():
def original(library):
return SimpleNamespace(n_gpu_layers=0, main_gpu=7, split_mode=1)
offloading = _offloading_params(original, EngineConfig(gpu_layers=-1))
params = offloading(object())
assert params.n_gpu_layers == -1
assert params.main_gpu == 7 # untouched
assert params.split_mode == 0
def test_gpu_state_reset_clears_cache_data():
calls = []
class FakeLib:
def llama_memory_clear(self, memory, data):
calls.append((memory, data))
engine = SimpleNamespace(lib=FakeLib(), memory="mem")
engine.clear = lambda: engine.lib.llama_memory_clear(engine.memory, False)
_install_gpu_state_reset(SimpleNamespace(engine=engine))
engine.clear()
assert calls == [("mem", True)]
def test_build_engine_defaults_to_the_local_semif_provider():
engine = build_engine({"engine": {"backend": "llamacpp"}}, PINS)
assert isinstance(engine, SemIfEngine)
assert isinstance(engine, DecisionEngine)
assert engine.label == "semif"
def test_build_engine_selects_the_winnow_provider():
engine = build_engine(
{
"engine": {
"provider": "winnow",
"winnow": {"base_url": "http://example:8091", "model": "Winnow-12B"},
}
},
PINS,
)
assert isinstance(engine, WinnowEngine)
assert isinstance(engine, DecisionEngine)
assert engine.label == "winnow"
assert engine.config.base_url == "http://example:8091"
assert engine.config.model == "Winnow-12B"
def test_build_engine_winnow_requires_a_model():
with pytest.raises(SystemExit):
build_engine({"engine": {"provider": "winnow", "winnow": {}}}, PINS)
def test_build_engine_selects_the_ollama_systemone_provider():
engine = build_engine(
{
"engine": {
"provider": "ollama",
"ollama": {
"base_url": "http://example:11434",
"model": "clef-flash",
"context_tokens": 8192,
"keep_alive": "5m",
},
}
},
PINS,
)
assert isinstance(engine, OllamaSystemOneEngine)
assert isinstance(engine, DecisionEngine)
assert engine.label == "ollama"
assert engine.config.base_url == "http://example:11434"
assert engine.config.model == "clef-flash"
assert engine.context_tokens == 8192
assert engine.config.keep_alive == "5m"
def test_build_engine_ollama_requires_a_model():
with pytest.raises(SystemExit):
build_engine({"engine": {"provider": "ollama", "ollama": {}}}, PINS)