Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Share builders and setup helpers in the studio backend tests, part 2 (#10498)
Daniel Han1f92c956d4e9
diff --git a/studio/backend/tests/data/refactor_guard/patch_targets.json b/studio/backend/tests/data/refactor_guard/patch_targets.jsonindex 4c15042379c..49d3e7f646a 100644--- a/studio/backend/tests/data/refactor_guard/patch_targets.json+++ b/studio/backend/tests/data/refactor_guard/patch_targets.json@@ -509,28 +509,6 @@"tests/test_llama_cpp_tool_loop.py","tests/test_llama_cpp_tool_loop.py","tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py",- "tests/test_llama_cpp_tool_loop.py","tests/test_no_progress_tool_results.py","tests/test_no_progress_tool_results.py","tests/test_no_progress_tool_results.py",@@ -539,7 +517,12 @@"tests/test_no_progress_tool_results.py","tests/test_no_progress_tool_results.py","tests/test_no_progress_tool_results.py",- "tests/test_tool_output_streaming.py"+ "tests/test_no_progress_tool_results.py",+ "tests/test_no_progress_tool_results.py",+ "tests/test_no_progress_tool_results.py",+ "tests/test_no_progress_tool_results.py",+ "tests/test_tool_output_streaming.py",+ "tests/test_truncated_answer_continuation.py"],"core.inference.tools.get_enabled_mcp_tools": ["tests/test_external_hosted_tool_passthrough.py",diff --git a/studio/backend/tests/test_hf_cache_settings.py b/studio/backend/tests/test_hf_cache_settings.pyindex 20ebf07f4f5..506b552a4cf 100644--- a/studio/backend/tests/test_hf_cache_settings.py+++ b/studio/backend/tests/test_hf_cache_settings.py@@ -145,12 +145,9 @@ def test_worker_environment_is_applied_before_import(monkeypatch, tmp_path):class Module:@staticmethoddef run():- import osreturn os.environ["HF_HUB_CACHE"], os.environ["HF_XET_CACHE"]def fake_import(name):- import os-observed["name"] = nameobserved["hub"] = os.environ.get("HF_HUB_CACHE")return Module@@ -172,7 +169,6 @@ def test_spawn_environment_is_applied_then_restored(monkeypatch, tmp_path):monkeypatch.delenv("HF_XET_CACHE", raising = False)with hf_cache_settings.child_environment_for_spawn({"HF_HUB_CACHE": hub, "HF_XET_CACHE": xet}):- import osassert os.environ["HF_HUB_CACHE"] == hubassert os.environ["HF_XET_CACHE"] == xetdiff --git a/studio/backend/tests/test_hf_optional_file_probe.py b/studio/backend/tests/test_hf_optional_file_probe.pyindex eff0c7c99db..828dc937446 100644--- a/studio/backend/tests/test_hf_optional_file_probe.py+++ b/studio/backend/tests/test_hf_optional_file_probe.py@@ -16,6 +16,7 @@ import pytestfrom huggingface_hub.errors import EntryNotFoundError, LocalEntryNotFoundErrorfrom utils.hf_probe import hf_file_definitely_absent+import huggingface_hubdef _http_error(name: str, *, fallback: str | None = None) -> Exception:@@ -65,7 +66,6 @@ def _raise(exc):def _patch_metadata(monkeypatch, behavior):- import huggingface_hubmonkeypatch.setattr(huggingface_hub, "get_hf_file_metadata", behavior)@@ -153,8 +153,6 @@ def test_the_probe_writes_nothing_to_the_cache(monkeypatch, tmp_path):def test_the_lora_base_probe_skips_the_download_when_the_file_is_absent(monkeypatch):- import huggingface_hub-from utils.models import model_configcalls = []@@ -170,8 +168,6 @@ def test_the_lora_base_probe_skips_the_download_when_the_file_is_absent(monkeypadef test_a_present_adapter_config_still_resolves_its_base(monkeypatch, tmp_path):- import huggingface_hub-from utils.models import model_configcfg = tmp_path / "adapter_config.json"@@ -184,8 +180,6 @@ def test_a_present_adapter_config_still_resolves_its_base(monkeypatch, tmp_path)def test_the_chat_template_search_skips_paths_the_listing_does_not_name(monkeypatch):"""The existing path lookup must gate absent template downloads."""- import huggingface_hub-from picker import servicelisted: list[str] = []diff --git a/studio/backend/tests/test_hf_xet_fallback.py b/studio/backend/tests/test_hf_xet_fallback.pyindex b0757d6db4a..78d6b686684 100644--- a/studio/backend/tests/test_hf_xet_fallback.py+++ b/studio/backend/tests/test_hf_xet_fallback.py@@ -39,6 +39,11 @@ except Exception: # noqa: BLE001 - still collect degraded-path tests when unsloshared = Noneimport utils.hf_xet_fallback as xf+import importlib+import os+import threading+import types+import utils.hf_xet_fallback as shimDL_REPO, FILE = "ztest/xet-dl", "model-Q4_K_XL.gguf"@@ -180,7 +185,6 @@ def test_shim_snapshot_injects_studio_prepare(monkeypatch):def test_degrades_gracefully_without_shared_helper(monkeypatch):"""On an older unsloth_zoo lacking the shared helper, the shim still imports (Unslothboots) and exposes stub API doing plain HF downloads with the watchdog disabled."""- import importlibclass _BlockShared:def find_spec(@@ -258,7 +262,6 @@ def test_degrades_when_unsloth_zoo_entirely_absent():"""When unsloth_zoo is absent entirely, the import raisesModuleNotFoundError(name='unsloth_zoo') (top-level package). Guard that the shim stilldegrades and does not re-raise, breaking every Unsloth import that pulls it in."""- import importlibclass _BlockZoo:def find_spec(@@ -300,7 +303,6 @@ def test_degrades_when_unsloth_zoo_entirely_absent():def test_degrades_when_shared_helper_import_raises_importerror():"""unsloth_zoo can be installed yet fail to import when torch is missing (llama.cpp/GGUF-onlyUnsloth), raising ImportError not ModuleNotFoundError. The shim must degrade for that too."""- import importlibclass _BlockWithImportError:def find_spec(@@ -343,9 +345,6 @@ def test_no_light_gpu_init_retry_on_an_accelerator_host(monkeypatch):stubs raise "called on Apple Silicon / MLX" from the first CUDA-only kernel a later GGUF orcompiled diffusion generation touches, so a healthy GPU starts 500ing. The shim must degradeinstead of retrying there."""- import importlib- import os-monkeypatch.delenv("UNSLOTH_ZOO_DISABLE_GPU_INIT", raising = False)attempts = []@@ -391,9 +390,6 @@ def test_retries_under_light_gpu_init_when_import_fails(monkeypatch):retries under UNSLOTH_ZOO_DISABLE_GPU_INIT=1, restores the env, and degrades if the retry fails.The backend loads lazily (first use of a heavy helper), so this triggers the load explicitlybefore asserting the retry/degrade behavior."""- import importlib- import os-monkeypatch.delenv("UNSLOTH_ZOO_DISABLE_GPU_INIT", raising = False)seen_env = []@@ -448,9 +444,6 @@ def test_a_worker_spawned_during_the_gpu_init_retry_does_not_inherit_the_overrid"""The shim sets UNSLOTH_ZOO_DISABLE_GPU_INIT=1 process-wide while it retries an optionalimport, and unsloth_zoo answers that flag with STUB triton and bitsandbytes, so a child thatinherited it would run for life against no-ops and never clear it."""- import importlib- import os-from utils.child_stdio import utf8_child_envmonkeypatch.delenv("UNSLOTH_ZOO_DISABLE_GPU_INIT", raising = False)@@ -505,8 +498,6 @@ def test_a_spawn_cannot_overlap_the_loader_env_override_window():spawn cannot start while a loader holds it; a child that inherits it silently trains againstunsloth_zoo's stub triton and bitsandbytes. Structural on purpose: it asserts the two share onelock rather than trying to hit a microsecond window by timing."""- import threading-from utils.hf_cache_settings import child_environment_for_spawnloader_holds = threading.Event()@@ -544,8 +535,6 @@ def test_the_spawn_barrier_is_reentrant():"""child_environment_for_spawn nests (an inference respawn inside a training start), which itsRLock _spawn_env_lock allows, so the barrier it now takes alongside must be reentrant too or theinner enter deadlocks. Bounded on purpose: a plain Lock hangs the suite rather than failing."""- import threading-from utils.hf_cache_settings import child_environment_for_spawndone = threading.Event()@@ -578,8 +567,6 @@ def test_importing_child_should_disable_xet_stays_light(monkeypatch):this at startup to decide the Xet env flip BEFORE activating the sidecar; an eager import herewould cache the default transformers 4.57.x in sys.modules, defeating the sidecar sys.path prependand breaking 5.x models (Qwen3.5/GLM/gemma-4)."""- import importlib-for name in [mfor m in list(sys.modules)@@ -602,8 +589,6 @@ def test_importing_child_should_disable_xet_stays_light(monkeypatch):def test_first_download_dispatch_loads_zoo_once(monkeypatch):"""Import stays light, but a real download dispatch activates the shared Zoo helper."""- import utils.hf_xet_fallback as shim-calls: list[str] = []class _FakeShared:@@ -627,10 +612,6 @@ def test_start_watchdog_drops_kwargs_the_installed_zoo_cannot_take(monkeypatch):**kwargs and no connect_timeout, so passing one raises TypeError into the caller's`except Exception` and the watchdog silently never starts. That is the feature entirely off."""- import threading-- import utils.hf_xet_fallback as shim-seen = {}def _old_signature_watchdog(@@ -670,10 +651,6 @@ def test_start_watchdog_drops_kwargs_the_installed_zoo_cannot_take(monkeypatch):def test_start_watchdog_passes_everything_to_a_zoo_that_accepts_it(monkeypatch):"""A newer zoo must still receive the newer knobs."""- import threading-- import utils.hf_xet_fallback as shim-seen = {}def _new_signature_watchdog(**kwargs):@@ -692,10 +669,6 @@ def test_start_watchdog_passes_everything_to_a_zoo_that_accepts_it(monkeypatch):def test_apply_xet_env_delegates_to_the_zoo(monkeypatch):"""One rule, in one place: Unsloth asks the zoo to size the worker rather than sizing it too."""- import types-- import utils.hf_xet_fallback as shim-seen = {}def _apply(env, **kwargs):@@ -718,10 +691,6 @@ def test_apply_xet_env_delegates_to_the_zoo(monkeypatch):def test_apply_xet_env_returns_none_when_the_zoo_cannot_size(monkeypatch):"""None, not {}: an empty write is a legitimate result, so the caller needs the two apart toknow whether to fall back to clearing the high-performance flag itself."""- import types-- import utils.hf_xet_fallback as shim-monkeypatch.setattr(shim, "_load_optional", lambda _name: None)assert shim.apply_xet_env({}) is None@@ -742,10 +711,6 @@ def test_a_zoo_that_can_resize_is_asked_for_the_workers_own_cache(monkeypatch):setdefault apply would keep. A zoo that can resize gets the worker's cache instead, so a backendwhose cache moved after startup does not size the worker for the volume it left behind. An olderzoo, with no resize to call, keeps the previous behaviour."""- import types-- import utils.hf_xet_fallback as shim-seen = {}def _resize(env, cache_dir, **kwargs):@@ -814,8 +779,6 @@ def _fake_tuning():"""Stand-in zoo sized like the real one (an eighth of total RAM), recording the profile it wasasked about so a test can prove the clamp re-asks rather than editing numbers itself."""- import types-profile_cls = _fake_profile_cls()def _overrides(profile, **kwargs):@@ -842,8 +805,6 @@ def _fake_tuning(def test_clamp_is_a_no_op_when_the_machine_has_room(monkeypatch):"""The design rests on this: an eighth of TOTAL cannot exceed a quarter of AVAILABLE unless RAMis already held, so an idle machine keeps the zoo's numbers and no download gets slower."""- import utils.hf_xet_fallback as shim-calls = []module = _fake_tuning(32 * _GB, 30 * _GB, calls = calls)sized = module.xet_env_overrides(module.system_profile())@@ -860,8 +821,6 @@ def test_clamp_is_a_no_op_when_the_machine_has_room(monkeypatch):def test_clamp_shrinks_a_budget_free_ram_cannot_afford(monkeypatch):"""Issue #9032: 32GB box, 27B GGUF resident, 8GB free. The zoo still hands out a 4GB bufferbecause total RAM has not changed, and that on top of the loaded weights is the swap."""- import utils.hf_xet_fallback as shim-calls = []module = _fake_tuning(32 * _GB, 8 * _GB, calls = calls)unclamped = module.xet_env_overrides(_fake_profile_cls()(32 * _GB, 8 * _GB))@@ -886,8 +845,6 @@ def test_clamp_shrinks_a_budget_free_ram_cannot_afford(monkeypatch):def test_clamp_bottoms_out_at_the_zoos_own_floor(monkeypatch):"""Below the floor the answer is a different transport, not a tinier buffer: Xet has a minimumit can work in, and ``_memory_pressure_reason`` routes a machine this tight to HTTP."""- import utils.hf_xet_fallback as shim-module = _fake_tuning(32 * _GB, 1 * _GB)unclamped = module.xet_env_overrides(_fake_profile_cls()(32 * _GB, 1 * _GB))@@ -900,8 +857,6 @@ def test_clamp_never_writes_a_key_the_user_set(monkeypatch):"""The zoo's apply is setdefault, so a user-set variable never reaches ``sized`` and must not bereintroduced here. Same mechanism covers HF_XET_HIGH_PERFORMANCE: the zoo drops its caps, nobudget key arrives, and the clamp stands down with it."""- import utils.hf_xet_fallback as shim-calls = []module = _fake_tuning(32 * _GB, 1 * _GB, calls = calls)@@ -923,10 +878,6 @@ def test_clamp_never_writes_a_key_the_user_set(monkeypatch):def test_clamp_stands_down_when_ram_cannot_be_measured(monkeypatch):"""No psutil, no cgroup, or a zoo too old to expose a profile: absence of evidence is notevidence of pressure, so the download runs as before."""- import types-- import utils.hf_xet_fallback as shim-sized = {_LIMIT: str(8 * _GB)}unmeasurable = _fake_tuning(0, 0)@@ -962,10 +913,6 @@ def test_clamp_holds_against_the_real_zoo_formulas():loaded = dataclasses.replace(idle, available_ram_bytes = 8 * _GB)- import types-- import utils.hf_xet_fallback as shim-sized = dict(tuning.xet_env_overrides(idle, fail_fast = True))def _module_for(profile):@@ -988,8 +935,6 @@ def test_clamp_holds_against_the_real_zoo_formulas():def test_clamp_never_raises_a_value_the_zoo_had_lowered():"""The recompute runs without the throttled flag that a 429 backoff sets, so the clamp must takethe smaller of the two per key. Otherwise shrinking buffers would restore the stream ceiling."""- import utils.hf_xet_fallback as shim-module = _fake_tuning(32 * _GB, 8 * _GB)unclamped = module.xet_env_overrides(_fake_profile_cls()(32 * _GB, 8 * _GB))# As if a 429 had halved the ceiling on the way in.@@ -1004,8 +949,6 @@ def test_clamp_never_raises_a_value_the_zoo_had_lowered():def test_free_ram_pressure_reason_applies_the_zoos_own_floor(monkeypatch):"""The threshold logic itself, now that both transport callers share this one helper."""- import utils.hf_xet_fallback as shim-monkeypatch.setattr(shim, "available_ram_bytes", lambda: (2 * _GB, 4 * _GB))reason = shim.free_ram_pressure_reason()assert reason is not None and "2.0GB RAM free" in reason@@ -1035,8 +978,6 @@ def test_free_ram_pressure_reason_applies_the_zoos_own_floor(monkeypatch):def clean_ledger():"""Autouse: sizing reserves RAM, so any clamp test leaves a reservation that would otherwisefollow the process into the next test and shrink its budget."""- import utils.hf_xet_fallback as shim-shim._budget_reservations.clear()shim._pending_reservation.token = Noneyield shim@@ -1047,8 +988,6 @@ def clean_ledger():def test_workers_starting_together_do_not_promise_the_same_ram_twice(clean_ledger):"""Four downloads queued at once each used to take a quarter of the same snapshot, promising thewhole machine before any of them had allocated a byte."""- import os-shim = clean_ledgermodule = _fake_tuning(32 * _GB, 8 * _GB)sized = module.xet_env_overrides(_fake_profile_cls()(32 * _GB, 8 * _GB))@@ -1121,8 +1060,6 @@ def test_the_transport_gate_counts_ram_promised_to_running_downloads(clean_ledgefourth; three that have finished allocating are already missing from `available`, which stops itwithout the ledger. `os.getpid()` stands in for all three, so its own RSS is stubbed out ratherthan credited three times against promises it has nothing to do with."""- import os-shim = clean_ledgermonkeypatch.setattr(shim, "available_ram_bytes", lambda: (8 * _GB, 4 * _GB))assert shim.free_ram_pressure_reason() is None, "8GB free is not pressure on its own"@@ -1156,8 +1093,6 @@ def test_a_resident_promise_is_not_charged_against_free_ram_twice(clean_ledger,promise on top of a reading that already reflects it counts the same bytes twice for theworker's entire lifetime. On an 8GB-free host that is the difference between the next Autodownload getting Xet and being told "only 2.0GB RAM free" while 4GB genuinely is."""- import os-shim = clean_ledgermodule = _fake_tuning(32 * _GB, 8 * _GB)sized = module.xet_env_overrides(_fake_profile_cls()(32 * _GB, 8 * _GB))@@ -1218,8 +1153,6 @@ def test_concurrent_sizings_cannot_all_claim_the_same_free_ram(clean_ledger):The barrier is in system_profile, which the clamp reads OUTSIDE the lock, so all four arrive atthe decision at once; the sleep in xet_env_overrides widens the read-to-reserve window that asplit critical section would leave open."""- import os- import threadingimport timeshim = clean_ledgerdiff --git a/studio/backend/tests/test_hosted_result_replay.py b/studio/backend/tests/test_hosted_result_replay.pyindex b573ef23f46..8315e06718d 100644--- a/studio/backend/tests/test_hosted_result_replay.py+++ b/studio/backend/tests/test_hosted_result_replay.py@@ -118,6 +118,28 @@ class FakeTransport:return _gen()+def _tool_end(+ result,+ *,+ tool_call_id = "hosted-1",+ tool_name = "web_search",+):+ """The hosted tool_end frame the replay cases send."""+ return _hosted_event(+ {+ "type": "tool_end",+ "tool_call_id": tool_call_id,+ "tool_name": tool_name,+ "result": result,+ }+ )+++def _one_turn_transport(*events):+ """One assistant turn of `events`, then the tool call, the finish and the close-out."""+ return FakeTransport([[*events, _call_line(), _finish()], [_finish("stop")], [_DONE]])++@pytest.fixturedef executed(monkeypatch):calls: list[str] = []@@ -183,14 +205,7 @@ def test_a_hosted_result_reaches_the_follow_up_request(executed):"arguments": {},}),- _hosted_event(- {- "type": "tool_end",- "tool_call_id": "hosted-1",- "tool_name": "web_search",- "result": "Title: Unsloth\nSnippet: gradient checkpointing lands",- }- ),+ _tool_end("Title: Unsloth\nSnippet: gradient checkpointing lands"),_text("Let me also compute that."),_call_line(),_finish(),@@ -210,14 +225,7 @@ def test_the_hosted_result_still_reaches_the_client(executed):transport = FakeTransport([[- _hosted_event(- {- "type": "tool_end",- "tool_call_id": "hosted-1",- "tool_name": "web_search",- "result": "hosted output",- }- ),+ _tool_end("hosted output"),_call_line(),_finish(),],@@ -230,24 +238,9 @@ def test_the_hosted_result_still_reaches_the_client(executed):def test_the_models_own_prose_is_kept_alongside(executed):- transport = FakeTransport(- [- [- _text("Searching now."),- _hosted_event(- {- "type": "tool_end",- "tool_call_id": "hosted-1",- "tool_name": "web_search",- "result": "hosted output",- }- ),- _call_line(),- _finish(),- ],- [_finish("stop")],- [_DONE],- ]+ transport = _one_turn_transport(+ _text("Searching now."),+ _tool_end("hosted output"),)_run(transport)replayed = _replayed(transport)@@ -259,31 +252,9 @@ def test_the_models_own_prose_is_kept_alongside(executed):def test_a_repeated_end_event_is_recorded_once(executed):- transport = FakeTransport(- [- [- _hosted_event(- {- "type": "tool_end",- "tool_call_id": "hosted-1",- "tool_name": "web_search",- "result": "once only",- }- ),- _hosted_event(- {- "type": "tool_end",- "tool_call_id": "hosted-1",- "tool_name": "web_search",- "result": "once only",- }- ),- _call_line(),- _finish(),- ],- [_finish("stop")],- [_DONE],- ]+ transport = _one_turn_transport(+ _tool_end("once only"),+ _tool_end("once only"),)_run(transport)assert _replayed(transport).count("once only") == 1@@ -291,24 +262,16 @@ def test_a_repeated_end_event_is_recorded_once(executed):def test_a_start_event_alone_adds_nothing(executed):"""A hosted tool that never reported a result has nothing to replay."""- transport = FakeTransport(- [- [- _hosted_event(- {- "type": "tool_start",- "tool_name": "web_search",- "tool_call_id": "hosted-1",- "arguments": {},- }- ),- _text("hello"),- _call_line(),- _finish(),- ],- [_finish("stop")],- [_DONE],- ]+ transport = _one_turn_transport(+ _hosted_event(+ {+ "type": "tool_start",+ "tool_name": "web_search",+ "tool_call_id": "hosted-1",+ "arguments": {},+ }+ ),+ _text("hello"),)_run(transport)replayed = _replayed(transport)@@ -322,41 +285,18 @@ def test_a_malformed_result_is_ignored(executed, result):nothing. An empty string IS an outcome, covered by``test_a_silent_hosted_execution_still_reaches_the_next_turn``."""- transport = FakeTransport(- [- [- _hosted_event(- {- "type": "tool_end",- "tool_call_id": "hosted-1",- "tool_name": "web_search",- "result": result,- }- ),- _text("hello"),- _call_line(),- _finish(),- ],- [_finish("stop")],- [_DONE],- ]+ transport = _one_turn_transport(+ _tool_end(result),+ _text("hello"),)_run(transport)assert "[web_search result]" not in _replayed(transport)def test_an_event_without_a_call_id_is_ignored(executed):- transport = FakeTransport(- [- [- _hosted_event({"type": "tool_end", "tool_name": "web_search", "result": "orphan"}),- _text("hello"),- _call_line(),- _finish(),- ],- [_finish("stop")],- [_DONE],- ]+ transport = _one_turn_transport(+ _hosted_event({"type": "tool_end", "tool_name": "web_search", "result": "orphan"}),+ _text("hello"),)_run(transport)assert "orphan" not in _replayed(transport)@@ -369,23 +309,10 @@ def test_a_frontend_image_sentinel_is_not_replayed(executed):results already go through the same stripper."""huge = "data:image/png;base64," + ("A" * 20000)- transport = FakeTransport(- [- [- _hosted_event(- {- "type": "tool_end",- "tool_call_id": "hosted-1",- "tool_name": "code_execution",- "result": '4\n__IMAGES__:["' + huge + '"]',- }- ),- _call_line(),- _finish(),- ],- [_finish("stop")],- [_DONE],- ]+ transport = _one_turn_transport(+ _tool_end(+ '4\n__IMAGES__:["' + huge + '"]', tool_call_id = "hosted-1", tool_name = "code_execution"+ ),)_run(transport)replayed = _replayed(transport)@@ -399,26 +326,18 @@ def test_the_start_events_operation_labels_the_result(executed):that ran is only in the start event, so a result recorded alone replays as anunlabelled value."""- transport = FakeTransport(- [- [- _hosted_event(- {- "type": "tool_start",- "tool_name": "code_execution",- "tool_call_id": "hosted-1",- "arguments": {"language": "python", "code": "print(2 + 2)"},- }- ),- _hosted_event(- {"type": "tool_end", "tool_call_id": "hosted-1", "result": "4"},- ),- _call_line(),- _finish(),- ],- [_finish("stop")],- [_DONE],- ]+ transport = _one_turn_transport(+ _hosted_event(+ {+ "type": "tool_start",+ "tool_name": "code_execution",+ "tool_call_id": "hosted-1",+ "arguments": {"language": "python", "code": "print(2 + 2)"},+ }+ ),+ _hosted_event(+ {"type": "tool_end", "tool_call_id": "hosted-1", "result": "4"},+ ),)_run(transport)replayed = _replayed(transport)@@ -433,32 +352,24 @@ def test_a_generated_image_is_noted_without_its_bytes(executed):Requiring non-empty text dropped it entirely, and replaying the base64 isthe sentinel mistake again, so record only that it happened."""- transport = FakeTransport(- [- [- _hosted_event(- {- "type": "tool_start",- "tool_name": "image_generation",- "tool_call_id": "hosted-1",- "arguments": {},- }- ),- _hosted_event(- {- "type": "tool_end",- "tool_call_id": "hosted-1",- "result": "",- "image_b64": "B" * 5000,- "image_mime": "image/png",- }- ),- _call_line(),- _finish(),- ],- [_finish("stop")],- [_DONE],- ]+ transport = _one_turn_transport(+ _hosted_event(+ {+ "type": "tool_start",+ "tool_name": "image_generation",+ "tool_call_id": "hosted-1",+ "arguments": {},+ }+ ),+ _hosted_event(+ {+ "type": "tool_end",+ "tool_call_id": "hosted-1",+ "result": "",+ "image_b64": "B" * 5000,+ "image_mime": "image/png",+ }+ ),)_run(transport)replayed = _replayed(transport)@@ -487,30 +398,22 @@ def test_a_plot_with_no_stdout_is_still_reported(executed):Stripping it leaves an empty string and image_b64 is unset on that path, sounnoticed the entry looks empty and the follow-up is told nothing was made."""- transport = FakeTransport(- [- [- _hosted_event(- {- "type": "tool_start",- "tool_name": "code_execution",- "tool_call_id": "hosted-1",- "arguments": {"code": "plt.plot(x)"},- }- ),- _hosted_event(- {- "type": "tool_end",- "tool_call_id": "hosted-1",- "result": '\n__IMAGES__:["data:image/png;base64,' + ("C" * 4000) + '"]',- }- ),- _call_line(),- _finish(),- ],- [_finish("stop")],- [_DONE],- ]+ transport = _one_turn_transport(+ _hosted_event(+ {+ "type": "tool_start",+ "tool_name": "code_execution",+ "tool_call_id": "hosted-1",+ "arguments": {"code": "plt.plot(x)"},+ }+ ),+ _hosted_event(+ {+ "type": "tool_end",+ "tool_call_id": "hosted-1",+ "result": '\n__IMAGES__:["data:image/png;base64,' + ("C" * 4000) + '"]',+ }+ ),)_run(transport)replayed = _replayed(transport)@@ -520,23 +423,8 @@ def test_a_plot_with_no_stdout_is_still_reported(executed):def test_a_large_hosted_result_is_capped(executed):"""Local execution caps what the model sees; the hosted copy must too."""- transport = FakeTransport(- [- [- _hosted_event(- {- "type": "tool_end",- "tool_call_id": "hosted-1",- "tool_name": "code_execution",- "result": "D" * 60000,- }- ),- _call_line(),- _finish(),- ],- [_finish("stop")],- [_DONE],- ]+ transport = _one_turn_transport(+ _tool_end("D" * 60000, tool_call_id = "hosted-1", tool_name = "code_execution"),)_run(transport)replayed = _replayed(transport)@@ -553,14 +441,7 @@ def test_a_stalled_turn_keeps_its_hosted_result(executed):transport = FakeTransport([[- _hosted_event(- {- "type": "tool_end",- "tool_call_id": "hosted-1",- "tool_name": "web_search",- "result": "Title: Unsloth\nSnippet: gradient checkpointing lands",- }- ),+ _tool_end("Title: Unsloth\nSnippet: gradient checkpointing lands"),_text("Let me check that."),_finish("stop"),],@@ -583,14 +464,7 @@ def test_a_stalled_continuation_stays_one_assistant_turn(executed):transport = FakeTransport([[- _hosted_event(- {- "type": "tool_end",- "tool_call_id": "hosted-1",- "tool_name": "web_search",- "result": "Title: Unsloth\nSnippet: gradient checkpointing lands",- }- ),+ _tool_end("Title: Unsloth\nSnippet: gradient checkpointing lands"),_text(" Let me check that."),_finish("stop"),],@@ -721,37 +595,29 @@ def test_openai_image_generation_replays_the_prompt_it_actually_used(executed):"encrypted_content": "E" * 4000,},}- transport = FakeTransport(- [- [- _hosted_event(- {- "type": "tool_start",- "tool_name": "image_generation",- "tool_call_id": "ig_abc",- "arguments": {"kind": "image", "prompt": "", **plumbing},- }- ),- _hosted_event(- {- "type": "tool_end",- "tool_call_id": "ig_abc",- "result": "",- "arguments": {- "kind": "image",- "prompt": "A photorealistic ginger cat",- **plumbing,- },- "image_b64": "B" * 5000,- "image_mime": "image/png",- }- ),- _call_line(),- _finish(),- ],- [_finish("stop")],- [_DONE],- ]+ transport = _one_turn_transport(+ _hosted_event(+ {+ "type": "tool_start",+ "tool_name": "image_generation",+ "tool_call_id": "ig_abc",+ "arguments": {"kind": "image", "prompt": "", **plumbing},+ }+ ),+ _hosted_event(+ {+ "type": "tool_end",+ "tool_call_id": "ig_abc",+ "result": "",+ "arguments": {+ "kind": "image",+ "prompt": "A photorealistic ginger cat",+ **plumbing,+ },+ "image_b64": "B" * 5000,+ "image_mime": "image/png",+ }+ ),)_run(transport)replayed = _replayed(transport)@@ -772,23 +638,8 @@ def test_the_hosted_cap_follows_the_configured_local_one(executed, monkeypatch):that on exactly those installs."""monkeypatch.setattr(loop_mod.tools_module, "_MAX_OUTPUT_CHARS", 500)- transport = FakeTransport(- [- [- _hosted_event(- {- "type": "tool_end",- "tool_call_id": "hosted-1",- "tool_name": "code_execution",- "result": "D" * 4000,- }- ),- _call_line(),- _finish(),- ],- [_finish("stop")],- [_DONE],- ]+ transport = _one_turn_transport(+ _tool_end("D" * 4000, tool_call_id = "hosted-1", tool_name = "code_execution"),)_run(transport)replayed = _replayed(transport)@@ -804,30 +655,22 @@ def test_a_hosted_page_keeps_a_files_line_of_its_own(executed):document is dropped before the follow-up turn sees it."""page = 'How the envelope looks\n__FILES__:[{"name": "plot.png", "size": 12}]'- transport = FakeTransport(- [- [- _hosted_event(- {- "type": "tool_start",- "tool_name": "web_fetch",- "tool_call_id": "hosted-1",- "arguments": {"url": "https://unsloth.ai/docs"},- }- ),- _hosted_event(- {- "type": "tool_end",- "tool_call_id": "hosted-1",- "result": page,- }- ),- _call_line(),- _finish(),- ],- [_finish("stop")],- [_DONE],- ]+ transport = _one_turn_transport(+ _hosted_event(+ {+ "type": "tool_start",+ "tool_name": "web_fetch",+ "tool_call_id": "hosted-1",+ "arguments": {"url": "https://unsloth.ai/docs"},+ }+ ),+ _hosted_event(+ {+ "type": "tool_end",+ "tool_call_id": "hosted-1",+ "result": page,+ }+ ),)_run(transport)replayed = _replayed(transport)@@ -845,24 +688,16 @@ def test_a_silent_hosted_execution_still_reaches_the_next_turn(executed):the code is carried on the tool_start alone, never as assistant text, soskipping an empty result drops the whole execution."""- transport = FakeTransport(- [- [- _hosted_event(- {- "type": "tool_start",- "tool_name": "code_execution",- "tool_call_id": "code_a",- "arguments": {"language": "python", "code": "df.to_parquet('s.pq')"},- }- ),- _hosted_event({"type": "tool_end", "tool_call_id": "code_a", "result": ""}),- _call_line(),- _finish(),- ],- [_finish("stop")],- [_DONE],- ]+ transport = _one_turn_transport(+ _hosted_event(+ {+ "type": "tool_start",+ "tool_name": "code_execution",+ "tool_call_id": "code_a",+ "arguments": {"language": "python", "code": "df.to_parquet('s.pq')"},+ }+ ),+ _hosted_event({"type": "tool_end", "tool_call_id": "code_a", "result": ""}),)_run(transport)replayed = _replayed(transport)@@ -876,23 +711,15 @@ def test_a_start_with_no_end_is_still_left_out(executed):A stream cut between the halves leaves a start alone, and reporting thatwould tell the next turn an execution completed that never did."""- transport = FakeTransport(- [- [- _hosted_event(- {- "type": "tool_start",- "tool_name": "code_execution",- "tool_call_id": "code_a",- "arguments": {"language": "python", "code": "df.to_parquet('s.pq')"},- }- ),- _call_line(),- _finish(),- ],- [_finish("stop")],- [_DONE],- ]+ transport = _one_turn_transport(+ _hosted_event(+ {+ "type": "tool_start",+ "tool_name": "code_execution",+ "tool_call_id": "code_a",+ "arguments": {"language": "python", "code": "df.to_parquet('s.pq')"},+ }+ ),)_run(transport)replayed = _replayed(transport)@@ -908,29 +735,21 @@ def test_a_long_hosted_argument_says_it_was_cut(executed):as the whole of it."""body = "# line\n" * 600- transport = FakeTransport(- [- [- _hosted_event(- {- "type": "tool_start",- "tool_name": "code_execution",- "tool_call_id": "code_a",- "arguments": {- "kind": "text_editor",- "command": "create",- "path": "/tmp/a.py",- "file_text": body,- },- }- ),- _hosted_event({"type": "tool_end", "tool_call_id": "code_a", "result": "Created"}),- _call_line(),- _finish(),- ],- [_finish("stop")],- [_DONE],- ]+ transport = _one_turn_transport(+ _hosted_event(+ {+ "type": "tool_start",+ "tool_name": "code_execution",+ "tool_call_id": "code_a",+ "arguments": {+ "kind": "text_editor",+ "command": "create",+ "path": "/tmp/a.py",+ "file_text": body,+ },+ }+ ),+ _hosted_event({"type": "tool_end", "tool_call_id": "code_a", "result": "Created"}),)_run(transport)replayed = _replayed(transport)@@ -949,14 +768,7 @@ def test_a_stalled_turn_keeps_its_thought_signature(executed):transport = FakeTransport([[- _hosted_event(- {- "type": "tool_end",- "tool_call_id": "hosted-1",- "tool_name": "web_search",- "result": "Title: Unsloth\nSnippet: gradient checkpointing lands",- }- ),+ _tool_end("Title: Unsloth\nSnippet: gradient checkpointing lands"),"data: "+ json.dumps({@@ -1034,30 +846,22 @@ def test_a_page_that_writes_the_image_marker_is_not_an_image(executed):A fetched page documenting the output protocol contains the literal text.Reading that as a picture reports an image the turn never produced."""- transport = FakeTransport(- [- [- _hosted_event(- {- "type": "tool_start",- "tool_name": "web_fetch",- "tool_call_id": "hosted-1",- "arguments": {"url": "https://unsloth.ai/docs/protocol"},- }- ),- _hosted_event(- {- "type": "tool_end",- "tool_call_id": "hosted-1",- "result": "The card reads a line beginning __IMAGES__: and renders it.",- }- ),- _call_line(),- _finish(),- ],- [_finish("stop")],- [_DONE],- ]+ transport = _one_turn_transport(+ _hosted_event(+ {+ "type": "tool_start",+ "tool_name": "web_fetch",+ "tool_call_id": "hosted-1",+ "arguments": {"url": "https://unsloth.ai/docs/protocol"},+ }+ ),+ _hosted_event(+ {+ "type": "tool_end",+ "tool_call_id": "hosted-1",+ "result": "The card reads a line beginning __IMAGES__: and renders it.",+ }+ ),)_run(transport)replayed = _replayed(transport)diff --git a/studio/backend/tests/test_hub_download_transport_auto.py b/studio/backend/tests/test_hub_download_transport_auto.pyindex 9ec24c004ce..6f94949246c 100644--- a/studio/backend/tests/test_hub_download_transport_auto.py+++ b/studio/backend/tests/test_hub_download_transport_auto.py@@ -35,6 +35,13 @@ except ImportError:from hub.services import download_lifecycle as dlfrom hub.utils import download_registry+import importlib+import os+import utils.hf_xet_fallback as shim+++def _boom(**kw):+ raise RuntimeError("no")# --------------------------------------------------------------------------------------------@@ -168,8 +175,6 @@ def test_xet_worker_is_sized_from_the_machine(monkeypatch):def test_the_zoo_decides_and_studio_does_not_second_guess_it(monkeypatch):"""Unsloth used to force the flag off here. Two copies of one rule drifted, and on a 2TB host theworker ended up with a 24GB laptop's buffer, 3.4x slower than the machine's own setting."""- import utils.hf_xet_fallback as shim-seen = {}def _apply(env, cache_dir = None):@@ -199,8 +204,6 @@ def test_high_performance_is_cleared_even_without_the_tuning_module(monkeypatch)"""An unsloth_zoo with no `hf_xet_tuning` is exactly the version that setsHF_XET_HIGH_PERFORMANCE=1 at import, so routing the clear through the (then empty) overrideswould hand the worker a 64GB buffer ceiling on the installs Unsloth alone cannot fix."""- import utils.hf_xet_fallback as shim-monkeypatch.setattr(shim, "apply_xet_env", lambda *a, **k: None)env = _spawn_env(monkeypatch, use_xet = True, parent_env = {"HF_XET_HIGH_PERFORMANCE": "1"})assert env["HF_XET_HIGH_PERFORMANCE"] == "0"@@ -210,8 +213,6 @@ def test_high_performance_is_cleared_even_without_the_tuning_module(monkeypatch)def test_the_legacy_opt_in_still_works_without_the_tuning_module(monkeypatch):"""Newer zoos honour the flag on their own, but this is the escape hatch on installs thatcannot, so it has to keep working there."""- import utils.hf_xet_fallback as shim-monkeypatch.setattr(shim, "apply_xet_env", lambda *a, **k: None)monkeypatch.setenv("UNSLOTH_XET_ALLOW_HIGH_PERFORMANCE", "1")env = _spawn_env(monkeypatch, use_xet = True, parent_env = {"HF_XET_HIGH_PERFORMANCE": "1"})@@ -382,9 +383,6 @@ def test_capabilities_report_what_auto_resolves_to(monkeypatch):def test_capabilities_stay_optimistic_when_health_raises(monkeypatch):fake = _types.ModuleType("utils.hf_xet_fallback")- def _boom(**kw):- raise RuntimeError("no")-fake.cached_xet_health = _boomfake.xet_health = _boom@@ -405,16 +403,10 @@ def test_optional_loader_retries_with_gpu_init_disabled(monkeypatch):"""unsloth_zoo.__init__ runs torch accelerator detection and raises on a CPU-only host, which isexactly the small machine these caps protect, so without the retry they switch off where theyare needed."""- import importlib-- import utils.hf_xet_fallback as shim-attempts: list[str | None] = []sentinel = _types.ModuleType("fake_zoo_module")def _fake_import(name):- import os-seen = os.environ.get("UNSLOTH_ZOO_DISABLE_GPU_INIT")attempts.append(seen)if seen != "1":@@ -427,16 +419,11 @@ def test_optional_loader_retries_with_gpu_init_disabled(monkeypatch):assert shim._load_optional("unsloth_zoo.hf_xet_tuning") is sentinelassert attempts == [None, "1"]# The flag is scoped to the retry: it must not leak into unrelated later imports.- import osassert "UNSLOTH_ZOO_DISABLE_GPU_INIT" not in os.environdef test_optional_loader_returns_none_when_truly_absent(monkeypatch):- import importlib-- import utils.hf_xet_fallback as shim-def _always_fail(name):raise ModuleNotFoundError(name)@@ -450,8 +437,6 @@ def test_optional_loader_returns_none_when_truly_absent(monkeypatch):def test_capabilities_read_does_not_load_zoo(monkeypatch):"""Opening Hub asks for capabilities; that read must not initialize optional GPU consumers."""- import utils.hf_xet_fallback as shim-monkeypatch.setattr(shim, "_optional_modules", {})loaded: list[str] = []@@ -514,12 +499,8 @@ def test_gpu_init_override_is_serialized(monkeypatch):not reproducible by thread timing, and is established by construction instead (both take thesame `_load_lock` around their save/set/restore) -- see the test below."""- import importlib- import osimport threading- import utils.hf_xet_fallback as shim-monkeypatch.delenv("UNSLOTH_ZOO_DISABLE_GPU_INIT", raising = False)def _always_fail(name):@@ -544,9 +525,6 @@ def test_both_loaders_share_one_env_lock():"""The cross-loader guarantee, checked structurally. Two separate locks would each be correct inisolation and still allow the interleave that leaves the override set permanently."""import inspect-- import utils.hf_xet_fallback as shim-for fn in (shim._load_shared, shim._load_optional):source = inspect.getsource(fn)assert "UNSLOTH_ZOO_DISABLE_GPU_INIT" in source@@ -702,9 +680,6 @@ def test_the_gate_runs_when_the_health_probe_raises(monkeypatch):"""Same for a health module that blows up: the failure is evidence about health, not about RAM."""monkeypatch.setattr(dl, "resolve_effective_use_xet", lambda requested: requested)- def _boom(**kw):- raise RuntimeError("no")-fake = _types.ModuleType("utils.hf_xet_fallback")fake.xet_health = _boomfake.free_ram_pressure_reason = lambda: "HTTP: only 2.0GB RAM free"@@ -717,9 +692,6 @@ def test_no_health_and_no_pressure_still_reads_as_xet(monkeypatch):"""The optimistic default survives when neither probe objects, including its wording."""monkeypatch.setattr(dl, "resolve_effective_use_xet", lambda requested: requested)- def _boom(**kw):- raise RuntimeError("no")-for health_fn, expected in ((lambda **kw: None, "Xet"),(_boom, "Xet (health check unavailable)"),@@ -735,9 +707,6 @@ def test_the_probe_reads_free_ram_even_when_health_raises(monkeypatch):"""Registry mirror of the above: the RAM read sits outside the health try, so a raising healthmodule cannot take the free-RAM verdict down with it."""- def _boom(**kw):- raise RuntimeError("no")-fake = _types.ModuleType("utils.hf_xet_fallback")fake.cached_xet_health = _boomfake.xet_health = _boom@@ -753,8 +722,6 @@ def test_the_probe_reads_free_ram_even_when_health_raises(monkeypatch):def test_spawn_binds_the_ram_reservation_to_the_worker(monkeypatch):"""The sizing reserves RAM for a spawn that has not happened yet. spawn_worker has to hand thatreservation the worker's pid, or it ages out and siblings oversubscribe the machine."""- import utils.hf_xet_fallback as shim-bound = []monkeypatch.setattr(shim, "bind_worker_budget", lambda pid: bound.append(pid))_spawn_env(monkeypatch, use_xet = True)@@ -767,8 +734,6 @@ def test_spawn_binds_the_ram_reservation_to_the_worker(monkeypatch):def test_a_failed_spawn_releases_its_reservation(monkeypatch):"""Popen raising must drop the reservation rather than pin RAM until it ages out."""- import utils.hf_xet_fallback as shim-bound = []monkeypatch.setattr(shim, "bind_worker_budget", lambda pid: bound.append(pid))diff --git a/studio/backend/tests/test_hub_token_caller_identity.py b/studio/backend/tests/test_hub_token_caller_identity.pyindex b3590a67cd2..22f12be7966 100644--- a/studio/backend/tests/test_hub_token_caller_identity.py+++ b/studio/backend/tests/test_hub_token_caller_identity.py@@ -33,6 +33,11 @@ from hub.utils.inventory_scan import token_fingerprintfrom routes import models as models_routesfrom utils.models.model_config import _token_fingerprint as capability_fingerprintfrom utils.transformers_version import _token_cache_key+from routes.data_recipe import seed as seed_routes+import asyncio+import fastapi+import inspect+import utils.models.model_config as model_config_moduledef _models_client(via_api_key: bool) -> TestClient:@@ -207,8 +212,6 @@ def test_gguf_variants_token_precedence_survives_the_conversion(header, query, edef test_seed_inspection_derives_its_policy_from_the_caller(monkeypatch):"""A UI session on an ambient-token install keeps gated seed inspection."""- from routes.data_recipe import seed as seed_routes-seen = {}def _fake_list(*, dataset_name, token):@@ -235,8 +238,6 @@ def test_seed_inspection_derives_its_policy_from_the_caller(monkeypatch):def test_an_explicit_seed_token_wins_for_either_caller(monkeypatch):- from routes.data_recipe import seed as seed_routes-seen = {}monkeypatch.setattr(seed_routes,@@ -418,7 +419,6 @@ def test_the_config_inspection_target_still_uses_the_cache_for_the_ambient_calleWith no snapshot on disk the resolver raises its own 404; reaching that proves thecache branch ran rather than short-circuiting back to the bare repo id."""- import fastapiwith pytest.raises(fastapi.HTTPException):models_routes._model_config_inspection_target("org/private", True, None, hf_token)@@ -479,8 +479,6 @@ def test_an_anonymous_config_read_does_not_strip_the_process_credential(monkeypaThat context deletes HF_TOKEN and moves the login token files process-wide, so aconcurrent download in another worker thread would lose the operator's credential."""- import utils.models.model_config as model_config_module-monkeypatch.setenv("HF_TOKEN", "ambient-operator-token")seen = {}@@ -538,8 +536,6 @@ def test_the_config_probes_do_not_go_local_only_for_an_anonymous_caller(lambda *_a, **_k: "org/private",)- import asyncio-try:asyncio.run(models_routes.get_model_config(@@ -562,8 +558,6 @@ def test_the_config_probes_do_not_go_local_only_for_an_anonymous_caller(def test_offline_embedding_detection_does_not_read_the_cache_anonymously(monkeypatch):"""The marker read answers for a private repo without ever authorizing."""- import utils.models.model_config as model_config_module-monkeypatch.setattr(model_config_module, "is_local_path", lambda _n: False)monkeypatch.setattr("utils.utils.hf_env_offline", lambda: True, raising = False)@@ -582,8 +576,6 @@ def test_gguf_variants_serve_the_hf_cache_only_to_an_authorized_caller(monkeypatThe listing carries variant filenames, sizes and the vision flag, so a caller deniedthe ambient token could name a private repo the UI had cached and read it back."""- import asyncio-from hub.services.models import gguf_variantsreads = {"snapshot": 0, "state": 0}@@ -629,8 +621,6 @@ def test_offline_capability_probes_do_not_read_the_cache_anonymously(monkeypatchSo passing local_files_only=False does not put the anonymous caller back on the wire:the probe reads the cached config.json off disk and never authorizes."""- import utils.models.model_config as model_config_module-monkeypatch.setattr(model_config_module, "_env_offline", lambda: True)reached = {"vision": 0, "audio": 0}@@ -692,10 +682,6 @@ def test_a_cache_only_gguf_listing_is_refused_for_an_anonymous_caller(monkeypatcDeclining to build a second cached response is not enough: falling through wouldserialize the first one."""- import asyncio-- import fastapi-from hub.services.models import gguf_variantsmonkeypatch.setattr(@@ -714,8 +700,6 @@ def test_a_cache_only_gguf_listing_is_refused_for_an_anonymous_caller(monkeypatcdef test_the_scan_route_derives_its_caller_rather_than_trusting_an_absent_body_token():"""An absent body token must not read as ambient-authorized."""- import inspect-signature = inspect.signature(models_routes.scan_model_remote_code)assert (@@ -733,12 +717,6 @@ def test_an_anonymous_seed_preview_is_refused_while_offline(monkeypatch):The sentinel never reaches an authorization check there, so a previously cachedprivate dataset would come back as rows."""- import asyncio-- import fastapi-- from routes.data_recipe import seed as seed_routes-monkeypatch.setattr(seed_routes, "hf_env_offline", lambda: True)def _never(*_a, **_k):@@ -763,8 +741,6 @@ def test_an_anonymous_seed_preview_is_refused_while_offline(monkeypatch):@pytest.mark.parametrize("hf_token", [None, "hf_tok", False])def test_the_lora_resolver_does_not_launder_the_sentinel(monkeypatch, hf_token):"""`hf_token if hf_token else None` turned the sentinel back into ambient access."""- import utils.models.model_config as model_config_module-seen = {}def _absent(@@ -790,8 +766,6 @@ def test_the_lora_resolver_does_not_launder_the_sentinel(monkeypatch, hf_token):@pytest.mark.parametrize("hf_token", [None, "hf_tok", False])def test_the_audio_tokenizer_probe_does_not_read_the_cache_anonymously(monkeypatch, hf_token):"""The cache root is walked before any network branch, online as well as offline."""- import utils.models.model_config as model_config_module-reads = {"n": 0}def _cache_path(_name):@@ -814,8 +788,6 @@ def test_the_audio_tokenizer_probe_does_not_read_the_cache_anonymously(monkeypatdef test_the_embedding_transient_fallback_is_denied_to_an_anonymous_caller(monkeypatch):"""The anonymous 404 for a private repo lands in the same except branch."""- import utils.models.model_config as model_config_module-monkeypatch.setattr(model_config_module, "is_local_path", lambda _n: False)monkeypatch.setattr("utils.utils.hf_env_offline", lambda: False, raising = False)monkeypatch.setattr(model_config_module, "_embedding_detection_cache", {})@@ -838,8 +810,6 @@ def test_a_public_model_keeps_its_size_when_the_cache_is_bypassed():Sizing it as one returns None, so public models lost model_size_bytes entirely."""- import inspect-source = inspect.getsource(models_routes.get_model_config)assert (@@ -852,8 +822,6 @@ def test_the_offline_autoconfig_read_is_denied_to_an_anonymous_caller(monkeypatc"""token=False disables authentication but not the local cache."""import transformers- import utils.models.model_config as model_config_module-monkeypatch.setattr(model_config_module, "_env_offline", lambda: True)monkeypatch.setattr(model_config_module, "active_hf_hub_cache", lambda: None, raising = False)reached = {"n": 0}@@ -875,8 +843,6 @@ def test_the_offline_autoconfig_read_is_denied_to_an_anonymous_caller(monkeypatcdef test_the_prefer_local_scan_branch_carries_the_anonymous_guard():"""Only the exact-snapshot branch was gated; the sibling resolved the cache anyway."""- import inspect-source = inspect.getsource(models_routes.scan_model_remote_code)marker = source.index("elif prefer_local_cache is True")branch = source[marker : marker + 200]@@ -911,10 +877,7 @@ def test_the_offline_anonymous_rule_is_stated_once(hf_token, offline, denied, modef test_every_offline_reachable_route_refuses_before_it_reads(monkeypatch):"""The three routes that reach disk offline all consult the shared rule."""- import inspect-from hub.services.datasets import formatting-for owner, name in ((models_routes.get_model_config, "/config"),(models_routes.scan_model_remote_code, "scan-remote-code"),diff --git a/studio/backend/tests/test_image_gallery.py b/studio/backend/tests/test_image_gallery.pyindex c499ca1d002..21c25279ece 100644--- a/studio/backend/tests/test_image_gallery.py+++ b/studio/backend/tests/test_image_gallery.py@@ -17,6 +17,7 @@ import core.inference.image_gallery as galleryPIL = pytest.importorskip("PIL")from PIL import Image # noqa: E402+import json as _json@pytest.fixture(autouse = True)@@ -355,7 +356,6 @@ def test_clear_all_still_works_with_an_unreadable_store():def test_clear_refuses_when_a_single_flag_entry_is_malformed():# The whole-file corruption case is not the only risk: one bad VALUE reads as "not archived",# which is enough to delete an archived image, so it must block the clear too.- import json as _jsonrecord = _save_with_mtime("shelved", 100.0)gallery.set_flags(record["id"], archived = True)@@ -370,7 +370,6 @@ def test_clear_refuses_when_a_single_flag_entry_is_malformed():def test_clear_refuses_when_an_archived_flag_is_not_a_boolean():# `{"archived": null}` is still a dict, so a container-only check trusted it, and every reader# turns it into "not archived" -- enough for the default clear to delete the archived image.- import json as _jsonrecord = _save_with_mtime("shelved", 100.0)gallery.set_flags(record["id"], archived = True)@@ -385,7 +384,6 @@ def test_clear_refuses_when_an_archived_flag_is_not_a_boolean():def test_a_repair_never_makes_a_damaged_archive_deletable():# An unrelated pin rewrites the store. If that repair dropped the unreadable archived flag, the# store would come back trusted with the image active, and the next default clear() removes it.- import json as _jsonshelved = _save_with_mtime("shelved", 100.0)other = _save_with_mtime("other", 200.0)diff --git a/studio/backend/tests/test_imatrix_gguf_filtering.py b/studio/backend/tests/test_imatrix_gguf_filtering.pyindex b09f7278f52..f1137be452b 100644--- a/studio/backend/tests/test_imatrix_gguf_filtering.py+++ b/studio/backend/tests/test_imatrix_gguf_filtering.py@@ -148,6 +148,7 @@ def test_the_models_dir_scanner_does_not_publish_an_imatrix_only_child(tmp_path)# from any .gguf, so an interrupted download that landed only the repo's smallest file# published the folder as a local model. mmproj and MTP still decide presence, as they# are companions of a real model; an imatrix is not a model artifact at all.+from routes.models import _scan_models_dirrepo = tmp_path / "Qwen3.8-27B-GGUF"@@ -163,6 +164,7 @@ def test_the_models_dir_scanner_does_not_publish_an_imatrix_only_child(tmp_path)def test_an_mmproj_only_child_still_decides_presence(tmp_path):# Guard the line this change must not cross: a lone vision projector keeps its row# (format None), which is what the scanner has always reported for a companion.+from routes.models import _scan_models_dirrepo = tmp_path / "gemma-4-GGUF"@@ -177,6 +179,8 @@ def test_a_recommended_folder_chip_matches_what_the_picker_would_show(tmp_path):# _dir_has_downloaded_model mirrors the scanners on purpose, so that a chip never leads# to an empty picker. Once the scanners stopped surfacing an imatrix-only folder, an# unfiltered probe here would have advertised exactly that.+ from routes.models import _scan_models_dir+from routes.models import _dir_has_downloaded_model, _scan_models_dir(tmp_path / "imatrix_unsloth.gguf").write_bytes(b"GGUF" + b"0" * 8)@@ -193,6 +197,7 @@ def test_a_config_beside_an_imatrix_lists_for_the_reason_a_lone_config_does(tmp_# checkpoint still downloading its weights stays visible -- and it lists whether or not# an imatrix sits beside it. So the config disjunct is not an imatrix filter to bypass:# suppressing it would hide in-flight downloads, which is a different change.+from routes.models import _scan_models_dirfor name in ("config-only", "config-and-imatrix"):diff --git a/studio/backend/tests/test_install_resolve_prebuilt.py b/studio/backend/tests/test_install_resolve_prebuilt.pyindex 2a887936a40..2a6e4a11d85 100644--- a/studio/backend/tests/test_install_resolve_prebuilt.py+++ b/studio/backend/tests/test_install_resolve_prebuilt.py@@ -1267,6 +1267,18 @@ def _windows_amd_host(**overrides):return ilp.HostInfo(**defaults)+def _windows_gfx803_host(+ *args,+ rocm_gfx_target = "gfx803",+ rocm_gfx_targets = ["gfx1201", "gfx803"],+ **kwargs,+):+ """_windows_amd_host with the gfx803 target list these cases share."""+ return _windows_amd_host(+ *args, rocm_gfx_target = rocm_gfx_target, rocm_gfx_targets = rocm_gfx_targets, **kwargs+ )++def test_route_to_vulkan_prebuilt_auto_fallback_for_legacy_amd_gfx():host = _windows_amd_host(rocm_gfx_target = "gfx803", rocm_gfx_targets = ["gfx803"])routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)@@ -1277,10 +1289,7 @@ def test_route_to_vulkan_prebuilt_auto_fallback_for_legacy_amd_gfx():def test_route_to_vulkan_prebuilt_keeps_hip_when_one_gpu_is_supported():- host = _windows_amd_host(- rocm_gfx_target = "gfx1201",- rocm_gfx_targets = ["gfx1201", "gfx803"],- )+ host = _windows_gfx803_host(rocm_gfx_target = "gfx1201")routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)assert routed is hostassert repo == FORK@@ -1291,10 +1300,7 @@ def test_route_to_vulkan_prebuilt_auto_fallback_skips_hip_masked_hosts():# A HIP mask can hide a HIP-capable dGPU, but the Vulkan runtime honours none of them,# so auto-routing would let the installed backend grab the gfx1201 the user masked# off.- host = _windows_amd_host(- rocm_gfx_target = "gfx803",- rocm_gfx_targets = ["gfx1201", "gfx803"],- )+ host = _windows_gfx803_host()routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)assert repo == FORKassert persist is None@@ -1304,10 +1310,7 @@ def test_route_to_vulkan_prebuilt_auto_fallback_skips_hip_masked_hosts():def test_route_to_vulkan_prebuilt_auto_fallback_when_no_amd_gpu_reaches_floor():# Every physical AMD device is below the floor, so no card can be exposed to HIP and# the #7357 auto-Vulkan fallback still fires.- host = _windows_amd_host(- rocm_gfx_target = "gfx900",- rocm_gfx_targets = ["gfx803", "gfx900"],- )+ host = _windows_gfx803_host(rocm_gfx_target = "gfx900", rocm_gfx_targets = ["gfx803", "gfx900"])routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)assert repo == FORKassert persist == "auto"@@ -1384,10 +1387,7 @@ def test_route_to_vulkan_prebuilt_hip_masked_host_still_honours_explicit_optin(m# The mask guard only suppresses the AUTOMATIC fallback; an explicit opt-in is the user# taking responsibility for the Vulkan device mask themselves.monkeypatch.delenv("UNSLOTH_FORCE_VULKAN", raising = False)- host = _windows_amd_host(- rocm_gfx_target = "gfx803",- rocm_gfx_targets = ["gfx1201", "gfx803"],- )+ host = _windows_gfx803_host()_routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False, llama_backend = "vulkan")@@ -1467,11 +1467,7 @@ def test_upstream_windows_hip_targets_are_a_subset_of_the_combined_floor():def test_route_to_vulkan_prebuilt_unknown_gfx_does_not_auto_fallback():- host = _windows_amd_host(- has_rocm = True,- rocm_gfx_target = None,- rocm_gfx_targets = [],- )+ host = _windows_gfx803_host(has_rocm = True, rocm_gfx_target = None, rocm_gfx_targets = [])routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)assert routed is hostassert repo == FORK@@ -1505,10 +1501,7 @@ def test_route_to_vulkan_prebuilt_gfx1034_keeps_rocm():def test_route_to_vulkan_prebuilt_explicit_opt_in_on_mixed_amd(monkeypatch):monkeypatch.setenv("UNSLOTH_LLAMA_CPP_BACKEND", "vulkan")- host = _windows_amd_host(- rocm_gfx_target = "gfx1201",- rocm_gfx_targets = ["gfx1201", "gfx803"],- )+ host = _windows_gfx803_host(rocm_gfx_target = "gfx1201")routed, repo, _tag, persist = ilp._route_to_vulkan_prebuilt(host, FORK, "pin", force_cpu = False)assert repo == FORKassert persist == "vulkan"@@ -1546,8 +1539,7 @@ def test_hip_backend_env_suppresses_auto_vulkan_fallback_on_unsupported_gfx(monkdef test_route_to_vulkan_prebuilt_hidden_physical_nvidia_amd_not_rerouted():# Vulkan ignores CUDA_VISIBLE_DEVICES, so a CUDA-masked NVIDIA card next to a legacy# AMD gfx must not auto-route: Vulkan could grab the reserved NVIDIA GPU.- host = _windows_amd_host(- rocm_gfx_target = "gfx803",+ host = _windows_gfx803_host(rocm_gfx_targets = ["gfx803"],has_physical_nvidia = True,has_usable_nvidia = False,@@ -1561,8 +1553,7 @@ def test_route_to_vulkan_prebuilt_hidden_physical_nvidia_amd_not_rerouted():def test_route_to_vulkan_prebuilt_explicit_opt_in_overrides_hidden_nvidia(monkeypatch):# The physical-NVIDIA guard only gates the AMD auto path; an explicit opt-in wins.monkeypatch.setenv("UNSLOTH_LLAMA_CPP_BACKEND", "vulkan")- host = _windows_amd_host(- rocm_gfx_target = "gfx803",+ host = _windows_gfx803_host(rocm_gfx_targets = ["gfx803"],has_physical_nvidia = True,has_usable_nvidia = False,diff --git a/studio/backend/tests/test_keyless_api_access.py b/studio/backend/tests/test_keyless_api_access.pyindex 11f0476ac0b..1a991cbc84c 100644--- a/studio/backend/tests/test_keyless_api_access.py+++ b/studio/backend/tests/test_keyless_api_access.py@@ -223,6 +223,7 @@ def test_concurrent_stale_cache_misses_coalesce_into_one_sqlite_read(monkeypatchdef test_concurrent_keyless_checks_through_the_real_entrypoint_stay_bounded(monkeypatch):import utils.keyless_api_access as keyless+from utils.keyless_api_access import asgi_request_is_keylessset_keyless_api_access("inference", tools = False)@@ -315,8 +316,9 @@ def test_async_stale_cache_miss_broadcasts_one_refresh_result(monkeypatch):def test_slow_refresh_does_not_exhaust_the_anyio_worker_pool(monkeypatch):- import anyio.to_threadimport utils.keyless_api_access as keyless++ import anyio.to_threadfrom starlette.concurrency import run_in_threadpoolset_keyless_api_access("inference", tools = False)@@ -427,9 +429,10 @@ def test_failed_refresh_does_not_reuse_permissive_stale_settings(monkeypatch):def test_async_settings_tasks_do_not_retain_closed_event_loops(monkeypatch):+ import utils.keyless_api_access as keyless+import gcimport weakref- import utils.keyless_api_access as keylessloop_refs = []diff --git a/studio/backend/tests/test_kv_cache_estimate_route.py b/studio/backend/tests/test_kv_cache_estimate_route.pyindex 72900603595..ae216573966 100644--- a/studio/backend/tests/test_kv_cache_estimate_route.py+++ b/studio/backend/tests/test_kv_cache_estimate_route.py@@ -156,16 +156,32 @@ def _call_route()+def _call_std_route(+ *args,+ repo_id = "org/repo",+ speculative_type = None,+ weights_bytes = 4096,+ **kwargs,+):+ """_call_route with the fixed repo and weight size shared by every case below."""+ return _call_route(+ *args,+ repo_id = repo_id,+ speculative_type = speculative_type,+ weights_bytes = weights_bytes,+ **kwargs,+ )++class TestMtpReserveFollowsTheLoader:"""The reserve is charged only when the loader could run MTP."""@pytest.mark.parametrize("mode", ["mtp", "auto", "mtp+ngram"])def test_headless_mla_is_not_charged_a_duplicate_kv(self, monkeypatch, tmp_path, mode):gguf = _write_gguf(tmp_path / "plain-model-Q4_K_M.gguf", _MLA_NO_HEAD)- out = _call_route(+ out = _call_std_route(monkeypatch,path = gguf,- weights_bytes = 4096,repo_id = "org/plain-model",speculative_type = mode,)@@ -181,10 +197,9 @@ class TestMtpReserveFollowsTheLoader:fields = dict(_MLA_NO_HEAD)fields["nextn_predict_layers"] = 1gguf = _write_gguf(tmp_path / "headed-Q4_K_M.gguf", fields)- out = _call_route(+ out = _call_std_route(monkeypatch,path = gguf,- weights_bytes = 4096,repo_id = "org/headed",speculative_type = "mtp",)@@ -193,10 +208,9 @@ class TestMtpReserveFollowsTheLoader:def test_ngram_costs_nothing(self, monkeypatch, tmp_path):"""ngram drafts from generated text, so it holds no VRAM."""gguf = _write_gguf(tmp_path / "plain-Q4_K_M.gguf", _MLA_NO_HEAD)- out = _call_route(+ out = _call_std_route(monkeypatch,path = gguf,- weights_bytes = 4096,repo_id = "org/plain",speculative_type = "ngram",)@@ -206,10 +220,9 @@ class TestMtpReserveFollowsTheLoader:fields = dict(_MLA_NO_HEAD)fields["nextn_predict_layers"] = 1gguf = _write_gguf(tmp_path / "headed-Q4_K_M.gguf", fields)- out = _call_route(+ out = _call_std_route(monkeypatch,path = gguf,- weights_bytes = 4096,repo_id = "org/headed",speculative_type = "mtp",mtp_token = False,@@ -509,20 +522,11 @@ class TestTheEstimateMatchesTheConfiguredLoad:"attention.sliding_window_pattern": [True, True, True, False],}gguf = _write_gguf(tmp_path / "swa-Q4_K_M.gguf", fields)- none = _call_route(- monkeypatch,- path = gguf,- weights_bytes = 4096,- repo_id = "org/swa",- speculative_type = None,- n_parallel = 4,- )- many = _call_route(+ none = _call_std_route(monkeypatch, path = gguf, repo_id = "org/swa", n_parallel = 4)+ many = _call_std_route(monkeypatch,path = gguf,- weights_bytes = 4096,repo_id = "org/swa",- speculative_type = None,n_parallel = 4,ctx_checkpoints = 32,)@@ -550,10 +554,9 @@ class TestTheEstimateMatchesTheConfiguredLoad:gguf = _write_gguf(tmp_path / "mamba-Q4_K_M.gguf", fields)def at(depth):- return _call_route(+ return _call_std_route(monkeypatch,path = gguf,- weights_bytes = 4096,repo_id = "org/mamba",speculative_type = "mtp",n_parallel = 4,@@ -597,10 +600,9 @@ class TestTheEstimateMatchesTheConfiguredLoad:"""Rather than a fit that omits the launch's largest allocation."""gguf = _write_gguf(tmp_path / "plain-Q4_K_M.gguf", _MLA_NO_HEAD)for mode in ("dspark", "dflash"):- out = _call_route(+ out = _call_std_route(monkeypatch,path = gguf,- weights_bytes = 4096,repo_id = "org/plain",speculative_type = mode,)@@ -610,10 +612,9 @@ class TestTheEstimateMatchesTheConfiguredLoad:def test_a_priceable_mode_is_not_marked_unpriced(self, monkeypatch, tmp_path):gguf = _write_gguf(tmp_path / "plain-Q4_K_M.gguf", _MLA_NO_HEAD)for mode in (None, "ngram", "mtp", "auto"):- out = _call_route(+ out = _call_std_route(monkeypatch,path = gguf,- weights_bytes = 4096,repo_id = "org/plain",speculative_type = mode,)@@ -624,24 +625,15 @@ class TestTheEstimateMatchesTheConfiguredLoad:mmproj = tmp_path / "mmproj-F16.gguf"mmproj.write_bytes(b"\x00" * 800_000)- on = _call_route(- monkeypatch,- path = gguf,- weights_bytes = 4096,- repo_id = str(tmp_path),- speculative_type = None,- is_local = True,- )+ on = _call_std_route(monkeypatch, path = gguf, repo_id = str(tmp_path), is_local = True)assert (on["projector_bytes"] and on["projector_bytes"] > mmproj.stat().st_size), "the projector is charged above its file size (_MMPROJ_VRAM_SAFETY)"- off = _call_route(+ off = _call_std_route(monkeypatch,path = gguf,- weights_bytes = 4096,repo_id = str(tmp_path),- speculative_type = None,is_local = True,disable_vision = True,)@@ -649,14 +641,7 @@ class TestTheEstimateMatchesTheConfiguredLoad:def test_a_model_with_no_projector_reports_none(self, monkeypatch, tmp_path):gguf = _write_gguf(tmp_path / "text-Q4_K_M.gguf", _MLA_NO_HEAD)- out = _call_route(- monkeypatch,- path = gguf,- weights_bytes = 4096,- repo_id = str(tmp_path),- speculative_type = None,- is_local = True,- )+ out = _call_std_route(monkeypatch, path = gguf, repo_id = str(tmp_path), is_local = True)assert out["projector_bytes"] is None@@ -668,20 +653,16 @@ class TestHostMemoryIsNotChargedToTheCard:def test_checkpoints_are_reported_as_their_own_share(self, monkeypatch, tmp_path):gguf = _write_gguf(tmp_path / "swa-model-Q4_K_M.gguf", _SWA_MODEL)- with_checkpoints = _call_route(+ with_checkpoints = _call_std_route(monkeypatch,path = gguf,- weights_bytes = 4096,repo_id = "org/swa",- speculative_type = None,ctx_checkpoints = 8,)- without = _call_route(+ without = _call_std_route(monkeypatch,path = gguf,- weights_bytes = 4096,repo_id = "org/swa",- speculative_type = None,ctx_checkpoints = 0,)share = with_checkpoints["kv_checkpoint_bytes"]@@ -696,14 +677,7 @@ class TestHostMemoryIsNotChargedToTheCard:def test_no_checkpoints_means_no_host_share(self, monkeypatch, tmp_path):gguf = _write_gguf(tmp_path / "swa-model-Q4_K_M.gguf", _SWA_MODEL)- out = _call_route(- monkeypatch,- path = gguf,- weights_bytes = 4096,- repo_id = "org/swa",- speculative_type = None,- ctx_checkpoints = None,- )+ out = _call_std_route(monkeypatch, path = gguf, repo_id = "org/swa", ctx_checkpoints = None)assert out["kv_checkpoint_bytes"] is None@@ -880,14 +854,7 @@ class TestTheInheritedEnvironmentIsPriced:# to native priced a 4k load at the header's length.gguf = _write_gguf(tmp_path / "model-Q4_K_M.gguf", _PLAIN_GQA)monkeypatch.setenv("LLAMA_ARG_CTX_SIZE", "4096")- out = _call_route(- monkeypatch,- path = gguf,- weights_bytes = 4096,- repo_id = "org/repo",- speculative_type = None,- n_ctx = None,- )+ out = _call_std_route(monkeypatch, path = gguf, n_ctx = None)assert out["n_ctx"] == 4096, ("the estimate used the header's native window for a launch the ""environment pins to 4096"@@ -897,14 +864,7 @@ class TestTheInheritedEnvironmentIsPriced:def test_no_inherited_context_still_uses_native(self, monkeypatch, tmp_path):gguf = _write_gguf(tmp_path / "model-Q4_K_M.gguf", _PLAIN_GQA)monkeypatch.delenv("LLAMA_ARG_CTX_SIZE", raising = False)- out = _call_route(- monkeypatch,- path = gguf,- weights_bytes = 4096,- repo_id = "org/repo",- speculative_type = None,- n_ctx = None,- )+ out = _call_std_route(monkeypatch, path = gguf, n_ctx = None)assert out["n_ctx"] == _PLAIN_GQA["context_length"]def test_an_inherited_cache_type_sizes_the_cache(self, monkeypatch, tmp_path):@@ -914,22 +874,10 @@ class TestTheInheritedEnvironmentIsPriced:gguf = _write_gguf(tmp_path / "model-Q4_K_M.gguf", _PLAIN_GQA)monkeypatch.delenv("LLAMA_ARG_CACHE_TYPE_K", raising = False)monkeypatch.delenv("LLAMA_ARG_CACHE_TYPE_V", raising = False)- default = _call_route(- monkeypatch,- path = gguf,- weights_bytes = 4096,- repo_id = "org/repo",- speculative_type = None,- )+ default = _call_std_route(monkeypatch, path = gguf)monkeypatch.setenv("LLAMA_ARG_CACHE_TYPE_K", "q8_0")monkeypatch.setenv("LLAMA_ARG_CACHE_TYPE_V", "q8_0")- inherited = _call_route(- monkeypatch,- path = gguf,- weights_bytes = 4096,- repo_id = "org/repo",- speculative_type = None,- )+ inherited = _call_std_route(monkeypatch, path = gguf)assert inherited["kv_bytes"] < default["kv_bytes"], ("an inherited q8_0 cache was priced as f16, so the KV segment was ""about twice the size the launch reserves"@@ -942,47 +890,17 @@ class TestTheInheritedEnvironmentIsPriced:# irreducible floor, which is a comfortable fit for a load that OOMs.gguf = _write_gguf(tmp_path / "model-Q4_K_M.gguf", _PLAIN_GQA)monkeypatch.setenv("LLAMA_ARG_CTX_SIZE", "4096")- assert (- _call_route(- monkeypatch,- path = gguf,- weights_bytes = 4096,- repo_id = "org/repo",- speculative_type = None,- n_ctx = None,- )["context_is_pinned"]- is True- )+ assert _call_std_route(monkeypatch, path = gguf, n_ctx = None)["context_is_pinned"] is Truedef test_an_omitted_context_with_no_inheritance_is_not_pinned(self, monkeypatch, tmp_path):gguf = _write_gguf(tmp_path / "model-Q4_K_M.gguf", _PLAIN_GQA)monkeypatch.delenv("LLAMA_ARG_CTX_SIZE", raising = False)- assert (- _call_route(- monkeypatch,- path = gguf,- weights_bytes = 4096,- repo_id = "org/repo",- speculative_type = None,- n_ctx = None,- )["context_is_pinned"]- is False- )+ assert _call_std_route(monkeypatch, path = gguf, n_ctx = None)["context_is_pinned"] is Falsedef test_an_explicit_context_is_pinned(self, monkeypatch, tmp_path):gguf = _write_gguf(tmp_path / "model-Q4_K_M.gguf", _PLAIN_GQA)monkeypatch.delenv("LLAMA_ARG_CTX_SIZE", raising = False)- assert (- _call_route(- monkeypatch,- path = gguf,- weights_bytes = 4096,- repo_id = "org/repo",- speculative_type = None,- n_ctx = 8192,- )["context_is_pinned"]- is True- )+ assert _call_std_route(monkeypatch, path = gguf, n_ctx = 8192)["context_is_pinned"] is Truedef test_an_inherited_device_pin_is_reported(self, monkeypatch, tmp_path):# The child is confined to the cards LLAMA_ARG_DEVICE names and an@@ -991,16 +909,7 @@ class TestTheInheritedEnvironmentIsPriced:# environment, so the route has to say.gguf = _write_gguf(tmp_path / "model-Q4_K_M.gguf", _PLAIN_GQA)monkeypatch.setenv("LLAMA_ARG_DEVICE", "CUDA0")- assert (- _call_route(- monkeypatch,- path = gguf,- weights_bytes = 4096,- repo_id = "org/repo",- speculative_type = None,- )["inherited_device_pin"]- is True- )+ assert _call_std_route(monkeypatch, path = gguf)["inherited_device_pin"] is True@pytest.mark.parametrize("value", ["", "none", "NONE"])def test_no_usable_pin_is_not_reported_as_one(self, monkeypatch, tmp_path, value):@@ -1009,13 +918,4 @@ class TestTheInheritedEnvironmentIsPriced:# would blank the row for a second, unrelated reason.gguf = _write_gguf(tmp_path / "model-Q4_K_M.gguf", _PLAIN_GQA)monkeypatch.setenv("LLAMA_ARG_DEVICE", value)- assert (- _call_route(- monkeypatch,- path = gguf,- weights_bytes = 4096,- repo_id = "org/repo",- speculative_type = None,- )["inherited_device_pin"]- is False- )+ assert _call_std_route(monkeypatch, path = gguf)["inherited_device_pin"] is Falsediff --git a/studio/backend/tests/test_kv_cache_estimation.py b/studio/backend/tests/test_kv_cache_estimation.pyindex c1b54b52798..f62bcf7207c 100644--- a/studio/backend/tests/test_kv_cache_estimation.py+++ b/studio/backend/tests/test_kv_cache_estimation.py@@ -77,6 +77,7 @@ except ImportError:sys.modules["httpx"] = _httpx_stubfrom core.inference.llama_cpp import _CTX_FIT_VRAM_FRACTION, _FIT_MIN_CTX, LlamaCppBackend+from core.inference import llama_cpp as lc# Helpers@@ -457,8 +458,6 @@ class TestDynamicSwaResolver:"""4-tier resolver: GGUF metadata, on-disk cache, bootstrap, HF fetch."""def _isolate_cache(self, monkeypatch, tmp_path):- from core.inference import llama_cpp as lc-monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path))monkeypatch.setattr(lc, "_SWA_CACHE", None)return tmp_path@@ -506,7 +505,6 @@ class TestDynamicSwaResolver:def test_bootstrap_tier_used_when_no_cache(self, monkeypatch, tmp_path):self._isolate_cache(monkeypatch, tmp_path)- from core.inference import llama_cpp as lcdef boom(*a, **kw):raise AssertionError("HF fetch must not run when bootstrap covers the arch")@@ -534,7 +532,6 @@ class TestDynamicSwaResolver:def test_hf_fetch_populates_cache(self, monkeypatch, tmp_path):self._isolate_cache(monkeypatch, tmp_path)- from core.inference import llama_cpp as lccalls = []@@ -555,7 +552,6 @@ class TestDynamicSwaResolver:def test_hf_fetch_falls_back_to_other_candidates(self, monkeypatch, tmp_path):self._isolate_cache(monkeypatch, tmp_path)- from core.inference import llama_cpp as lcmonkeypatch.setattr(lc,@@ -574,7 +570,6 @@ class TestDynamicSwaResolver:def test_offline_env_skips_network(self, monkeypatch, tmp_path):self._isolate_cache(monkeypatch, tmp_path)monkeypatch.setenv("UNSLOTH_STUDIO_OFFLINE", "1")- from core.inference import llama_cpp as lcdef boom(*a, **kw):raise AssertionError("HF fetch must not run when offline=1")@@ -589,7 +584,6 @@ class TestDynamicSwaResolver:def test_hf_fetch_failure_falls_through_silently(self, monkeypatch, tmp_path):self._isolate_cache(monkeypatch, tmp_path)- from core.inference import llama_cpp as lcmonkeypatch.setattr(lc, "_fetch_swa_entry_from_hf", lambda repo_id: None)# Force failure into Tier 3; bypass Tier 2.5.@@ -607,8 +601,6 @@ class TestTransformersIntrospection:"""Tier 2.5: default-init the matching Config; on failure, parse via inspect."""def _isolate_cache(self, monkeypatch, tmp_path):- from core.inference import llama_cpp as lc-monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path))monkeypatch.setattr(lc, "_SWA_CACHE", None)return tmp_path@@ -629,8 +621,6 @@ class TestTransformersIntrospection:assert _resolve_swa_entry_from_transformers("cohere2") == 4def test_falls_back_to_inspect_when_default_init_raises(self, monkeypatch):- from core.inference import llama_cpp as lc-class _FakeBrokenConfig:"""Class with sliding_window_pattern: int = 7 in its docstring."""@@ -650,7 +640,6 @@ class TestTransformersIntrospection:assert lc._resolve_swa_entry_from_transformers("brokenarch") == 7def test_returns_none_when_transformers_unavailable(self, monkeypatch):- from core.inference import llama_cpp as lcimport sysorig_import = (@@ -677,7 +666,6 @@ class TestTransformersIntrospection:def test_full_resolver_uses_transformers_before_hf_fetch(self, monkeypatch, tmp_path):# Bootstrap empty: Tier 2.5 must answer before Tier 3 fires.self._isolate_cache(monkeypatch, tmp_path)- from core.inference import llama_cpp as lcmonkeypatch.setattr(lc, "_BOOTSTRAP_SWA_DEFAULTS", {})diff --git a/studio/backend/tests/test_llama_admission_compat_matrix.py b/studio/backend/tests/test_llama_admission_compat_matrix.pyindex 3db55e1f44e..77155641382 100644--- a/studio/backend/tests/test_llama_admission_compat_matrix.py+++ b/studio/backend/tests/test_llama_admission_compat_matrix.py@@ -205,9 +205,8 @@ class TestOldCallers:assert queue._reparking == 0, "the non-blocking path must never touch the wait line"def test_the_route_recost_helper_accepts_no_cancel_event(self):- import routes.inference as routes_inference-# Reservation None is the "not admitted yet" case every call site can hit.+ import routes.inference as routes_inferenceroutes_inference._openai_llama_admission_recost(None,[{"role": "user", "content": "hi"}],@@ -240,9 +239,10 @@ class TestOldCallers:), f"the hook must be last; signature ends {names[-3:]}"def test_the_wait_timeout_has_a_sane_default(self):- assert DEFAULT_RECOST_WAIT_TIMEOUT_S > 0import inspect+ assert DEFAULT_RECOST_WAIT_TIMEOUT_S > 0+from core.inference.llama_admission import LlamaAdmissionLeasesignature = inspect.signature(LlamaAdmissionLease.recost_waiting)diff --git a/studio/backend/tests/test_llama_admission_kv_budget.py b/studio/backend/tests/test_llama_admission_kv_budget.pyindex c27a78e8dc9..32a98754b2f 100644--- a/studio/backend/tests/test_llama_admission_kv_budget.py+++ b/studio/backend/tests/test_llama_admission_kv_budget.py@@ -183,7 +183,6 @@ class TestBackwardsCompatibility:class TestTheRouteHelpers:def test_the_budget_is_the_backends_own_context_length(self):from types import SimpleNamespace-import routes.inference as routes_inferencebackend = SimpleNamespace(context_length = 2048)@@ -191,7 +190,6 @@ class TestTheRouteHelpers:def test_an_unreadable_context_length_means_no_budget(self):from types import SimpleNamespace-import routes.inference as routes_inferencefor value in (None, 0, -1, "nonsense"):backend = SimpleNamespace(context_length = value)@@ -199,7 +197,6 @@ class TestTheRouteHelpers:def test_the_cost_is_the_prompt_plus_the_output_allowance(self):from types import SimpleNamespace-import routes.inference as routes_inferencepayload = SimpleNamespace(@@ -215,7 +212,6 @@ class TestTheRouteHelpers:def test_the_cost_is_clamped_to_the_budget(self):from types import SimpleNamespace-import routes.inference as routes_inferencepayload = SimpleNamespace(@@ -232,7 +228,6 @@ class TestTheRouteHelpers:def test_a_shape_with_no_messages_reserves_a_fair_share(self):from types import SimpleNamespace-import routes.inference as routes_inferencepayload = SimpleNamespace(prompt = "raw completion text", max_tokens = 128)@@ -247,7 +242,6 @@ class TestTheRouteHelpers:def test_no_budget_means_no_cost(self):from types import SimpleNamespace-import routes.inference as routes_inferencepayload = SimpleNamespace(messages = [{"role": "user", "content": "hi"}], max_tokens = 8)@@ -302,7 +296,6 @@ class TestTheOutputAllowanceIsCounted:"""Generation honours max_completion_tokens through_effective_openai_max_tokens; admission must reserve the same allowance."""from types import SimpleNamespace-import routes.inference as routes_inferencemessages = [{"role": "user", "content": "x" * 400}]@@ -322,7 +315,6 @@ class TestTheOutputAllowanceIsCounted:"""Why the /v1/responses site now reserves against the translated chat_req: theraw model has `input` and `max_output_tokens`, so nothing here can size it."""from types import SimpleNamespace-import routes.inference as routes_inferenceraw = SimpleNamespace(input = "x" * 100_000, max_output_tokens = 4096)diff --git a/studio/backend/tests/test_llama_admission_kv_budget_media_and_tools.py b/studio/backend/tests/test_llama_admission_kv_budget_media_and_tools.pyindex 919b33bd5ca..94ffab4af43 100644--- a/studio/backend/tests/test_llama_admission_kv_budget_media_and_tools.py+++ b/studio/backend/tests/test_llama_admission_kv_budget_media_and_tools.py@@ -20,6 +20,9 @@ from routes.inference import (_OPENAI_LLAMA_ADMISSION_IMAGE_TOKENS,_openai_llama_admission_tokens,)+from core.inference.llama_admission import LlamaAdmissionConfig, LlamaAdmissionQueue+from routes.inference import _openai_llama_admission_budget+import asyncioclass _Payload:@@ -280,9 +283,6 @@ class TestMediaIsCharged:def test_two_large_studio_image_chats_can_be_admitted_together(self):"""A large base64 transport must not turn each vision request into a full-cache lease."""- import asyncio-- from core.inference.llama_admission import LlamaAdmissionConfig, LlamaAdmissionQueueasync def scenario():queue = LlamaAdmissionQueue("media")@@ -323,9 +323,6 @@ class TestMediaIsCharged:def test_two_image_chats_are_not_both_admitted(self):"""The live failure, with images instead of text."""- import asyncio-- from core.inference.llama_admission import LlamaAdmissionConfig, LlamaAdmissionQueueasync def scenario():queue = LlamaAdmissionQueue("media")@@ -436,26 +433,20 @@ class TestTheBudgetIsTheWholeCacheNotOneSlot:"""def test_the_partitioned_total_wins_over_one_slot(self):- from routes.inference import _openai_llama_admission_budgetbackend = _Payload(context_length = 4096, _kv_cache_context_total = 16384)assert _openai_llama_admission_budget(backend) == 16384def test_a_unified_cache_is_unchanged(self):- from routes.inference import _openai_llama_admission_budget-# slots == 1 under --kv-unified, so the total IS the per-request window.backend = _Payload(context_length = 8192, _kv_cache_context_total = 8192)assert _openai_llama_admission_budget(backend) == 8192def test_an_unread_backend_falls_back_to_context_length(self):- from routes.inference import _openai_llama_admission_budget-# Nothing read back yet: the two agree, so the fallback is not a guess.backend = _Payload(context_length = 8192, _kv_cache_context_total = None)assert _openai_llama_admission_budget(backend) == 8192def test_a_backend_that_cannot_say_keeps_slot_only_admission(self):- from routes.inference import _openai_llama_admission_budgetassert _openai_llama_admission_budget(_Payload()) is None@@ -486,9 +477,6 @@ class TestARoundIsCostedTheSameWayTheReservationWas:def _round_zero(self, payload, *, output_tokens):"""Open a tool lease from ``payload``, then re-cost it before it has grown."""- import asyncio-- from core.inference.llama_admission import LlamaAdmissionConfig, LlamaAdmissionQueuefrom routes.inference import (_openai_llama_admission_recost,_openai_llama_admission_tokens,diff --git a/studio/backend/tests/test_llama_backend_switch.py b/studio/backend/tests/test_llama_backend_switch.pyindex 75b4e9844e8..891f8f95d06 100644--- a/studio/backend/tests/test_llama_backend_switch.py+++ b/studio/backend/tests/test_llama_backend_switch.py@@ -94,6 +94,25 @@ def _write_install(dir_: Path, **marker_fields) -> str:return str(binary)+def _write_vulkan_install(+ *args,+ asset = "app-b9596-mix-abc-linux-x64-vulkan.tar.gz",+ backend = "vulkan",+ backend_request = "auto",+ install_kind = "linux-vulkan",+ **kwargs,+):+ """_write_install for the stock linux vulkan build."""+ return _write_install(+ *args,+ asset = asset,+ backend = backend,+ backend_request = backend_request,+ install_kind = install_kind,+ **kwargs,+ )++@pytest.fixture(autouse = True)def _clean_state(monkeypatch, tmp_path):freshness.reset_caches()@@ -209,13 +228,7 @@ def test_applying_a_migration_re_applies_auto_and_stays_an_update(monkeypatch, tdef _on_start(cmd, kwargs):seen["cmd"] = cmdseen["env"] = kwargs.get("env") or {}- _write_install(- install_dir,- asset = "app-b9596-mix-abc-linux-x64-vulkan.tar.gz",- install_kind = "linux-vulkan",- backend = "vulkan",- backend_request = "auto",- )+ _write_vulkan_install(install_dir)_patch_installer(monkeypatch, on_start = _on_start)@@ -253,13 +266,7 @@ def test_a_migration_that_lands_on_its_target_reports_the_new_backend(monkeypatc_drifted(monkeypatch)_patch_installer(monkeypatch,- on_start = lambda cmd, kwargs: _write_install(- install_dir,- asset = "app-b9596-mix-abc-linux-x64-vulkan.tar.gz",- install_kind = "linux-vulkan",- backend = "vulkan",- backend_request = "auto",- ),+ on_start = lambda cmd, kwargs: _write_vulkan_install(install_dir),)assert upd.start_update()["started"] is True@@ -292,13 +299,7 @@ def test_a_switch_names_the_backend_and_keeps_the_installed_release(monkeypatch,def _on_start(cmd, kwargs):seen["cmd"] = cmdseen["env"] = kwargs.get("env") or {}- _write_install(- install_dir,- asset = "app-b9596-mix-abc-linux-x64-vulkan.tar.gz",- install_kind = "linux-vulkan",- backend = "vulkan",- backend_request = "vulkan",- )+ _write_vulkan_install(install_dir, backend_request = "vulkan")_patch_installer(monkeypatch, on_start = _on_start)@@ -420,13 +421,11 @@ def test_a_switch_rejects_a_cross_repository_result(monkeypatch, tmp_path):install_dir = _install(monkeypatch, tmp_path)def _on_start(cmd, kwargs):- _write_install(+ _write_vulkan_install(install_dir,release_tag = "b9596",published_repo = "ggml-org/llama.cpp",asset = "llama-b9596-bin-ubuntu-vulkan-arm64.tar.gz",- install_kind = "linux-vulkan",- backend = "vulkan",backend_request = "vulkan",)@@ -1145,13 +1144,7 @@ def test_applying_a_migration_replays_the_arch_the_offer_was_made_with(monkeypatdef _on_start(cmd, kwargs):seen["cmd"] = cmd- _write_install(- install_dir,- asset = "app-b9596-mix-abc-linux-x64-vulkan.tar.gz",- install_kind = "linux-vulkan",- backend = "vulkan",- backend_request = "auto",- )+ _write_vulkan_install(install_dir)_patch_installer(monkeypatch, on_start = _on_start)@@ -1200,13 +1193,7 @@ def test_applying_a_migration_replays_an_arch_only_the_bundle_names(monkeypatch,assert upd.get_update_status()["backend_migration_available"] is Truedef _on_start(cmd, kwargs):- _write_install(- install_dir,- asset = "app-b9596-mix-abc-linux-x64-vulkan.tar.gz",- install_kind = "linux-vulkan",- backend = "vulkan",- backend_request = "auto",- )+ _write_vulkan_install(install_dir)_patch_installer(monkeypatch, on_start = _on_start)diff --git a/studio/backend/tests/test_llama_cpp_atexit_quiet.py b/studio/backend/tests/test_llama_cpp_atexit_quiet.pyindex 146937e816f..2595dad39c2 100644--- a/studio/backend/tests/test_llama_cpp_atexit_quiet.py+++ b/studio/backend/tests/test_llama_cpp_atexit_quiet.py@@ -22,6 +22,7 @@ _backend = os.path.join(os.path.dirname(__file__), "..")sys.path.insert(0, _backend)from core.inference.llama_cpp import LlamaCppBackend # noqa: E402+from core.inference import llama_cpp as moddef _stub() -> LlamaCppBackend:@@ -71,8 +72,6 @@ class _Reader:def test_a_process_that_cannot_be_terminated_is_not_an_error(monkeypatch, tmp_path):- from core.inference import llama_cpp as mod-recorder = _RecordingLogger()monkeypatch.setattr(mod, "logger", recorder)backend = _stub()@@ -114,8 +113,6 @@ class _RaisingLogger:def test_a_logger_that_raises_does_not_escape_the_atexit_handler(monkeypatch):- from core.inference import llama_cpp as mod-monkeypatch.setattr(mod, "logger", _RaisingLogger())backend = _stub()backend._process = _Unterminable()@@ -134,8 +131,6 @@ def test_the_atexit_handler_quiets_stdlib_loggers_too(monkeypatch, capsys):other.addHandler(handler)other.propagate = False- from core.inference import llama_cpp as mod-def kill_and_log():other.warning("something a dependency logs at exit")@@ -172,8 +167,6 @@ def test_sigkill_still_happens_when_the_log_write_fails(monkeypatch):warning, and reporting first meant the kill was skipped while the finallydropped the last reference to the process -- leaving the server running withnothing left to kill it."""- from core.inference import llama_cpp as mod-monkeypatch.setattr(mod, "logger", _RaisingLogger())backend = _stub()proc = _StubbornProcess()@@ -198,8 +191,6 @@ def test_an_unkillable_server_is_still_reported(monkeypatch):"""The second wait raises from inside the handler it was raised from, so it isnot caught there and escapes. If the warning came after it, the one case anoperator most needs to see would be reported by nothing at all."""- from core.inference import llama_cpp as mod-recorder = _RecordingLogger()monkeypatch.setattr(mod, "logger", recorder)backend = _stub()@@ -216,8 +207,6 @@ def test_an_unkillable_server_is_still_reported(monkeypatch):def test_the_handler_leaves_raise_exceptions_as_it_found_it(monkeypatch):"""Only atexit gets the quiet treatment; a live run must still surface abroken logging handler."""- from core.inference import llama_cpp as mod-monkeypatch.setattr(logging, "raiseExceptions", True)backend = _stub()backend._process = _Unterminable()diff --git a/studio/backend/tests/test_llama_cpp_context_fit.py b/studio/backend/tests/test_llama_cpp_context_fit.pyindex e6da59447b9..341ff2c90eb 100644--- a/studio/backend/tests/test_llama_cpp_context_fit.py+++ b/studio/backend/tests/test_llama_cpp_context_fit.py@@ -274,6 +274,16 @@ def _drive(}+def _drive_native(+ *args,+ n_ctx = 0,+ native_ctx = 131072,+ **kwargs,+):+ """_drive with n_ctx = 0, which is Auto context: the branch that caps to native_ctx."""+ return _drive(*args, n_ctx = n_ctx, native_ctx = native_ctx, **kwargs)++# ---------------------------------------------------------------------------# Auto mode, model weights exceed VRAM (Bug A guard)# ---------------------------------------------------------------------------@@ -283,12 +293,7 @@ class TestAutoModeWeightsExceedVRAM:"""``n_ctx == 0`` on a model whose weights don't fit anywhere."""def test_minimax_like_single_gpu(self):- plan = _drive(- n_ctx = 0,- model_gib = 131,- gpus = [(0, 97_000)],- native_ctx = 196608,- )+ plan = _drive_native(model_gib = 131, gpus = [(0, 97_000)], native_ctx = 196608)assert plan["c_arg"] == _AUTO_OFFLOAD_CTXassert plan["use_fit"] is Trueassert plan["gpu_indices"] is None@@ -297,11 +302,9 @@ class TestAutoModeWeightsExceedVRAM:assert plan["max_available_ctx"] == 196608def test_multi_gpu_all_subsets_fail(self):- plan = _drive(- n_ctx = 0,+ plan = _drive_native(model_gib = 400,gpus = [(0, 80_000), (1, 80_000), (2, 80_000), (3, 80_000)],- native_ctx = 131072,)assert plan["c_arg"] == _AUTO_OFFLOAD_CTXassert plan["use_fit"] is True@@ -309,8 +312,7 @@ class TestAutoModeWeightsExceedVRAM:def test_no_kv_metadata_auto(self):"""File-size-only fallback path uses the same 8192 default."""- plan = _drive(- n_ctx = 0,+ plan = _drive_native(model_gib = 131,gpus = [(0, 97_000)],native_ctx = 196608,@@ -331,23 +333,13 @@ class TestExplicitCtxRespectsUser:def test_fittable_weights_oversized_kv(self):# 8 GB weights + 131k ctx KV on 24 GB VRAM. Budget = 21.6 GB, KV# at 131k >> 13.6 GB remaining, so _select_gpus flips use_fit=True.- plan = _drive(- n_ctx = 131072,- model_gib = 8,- gpus = [(0, 24_000)],- native_ctx = 131072,- )+ plan = _drive_native(n_ctx = 131072, model_gib = 8, gpus = [(0, 24_000)])assert plan["c_arg"] == 131072assert plan["use_fit"] is Trueassert plan["gpu_indices"] is Nonedef test_explicit_that_fits_uses_ngl(self):- plan = _drive(- n_ctx = 8192,- model_gib = 8,- gpus = [(0, 24_000)],- native_ctx = 131072,- )+ plan = _drive_native(n_ctx = 8192, model_gib = 8, gpus = [(0, 24_000)])assert plan["c_arg"] == 8192assert plan["use_fit"] is Falseassert plan["gpu_indices"] == [0]@@ -390,8 +382,7 @@ class TestExplicitCtxRespectsUser:class TestExtraArgsCtxOverride:def test_ctx_size_extra_honored_over_auto(self):- plan = _drive(- n_ctx = 0,+ plan = _drive_native(model_gib = 131,gpus = [(0, 97_000)],native_ctx = 196608,@@ -403,8 +394,7 @@ class TestExtraArgsCtxOverride:assert plan["use_fit"] is Truedef test_ctx_size_short_alias_honored_over_auto(self):- plan = _drive(- n_ctx = 0,+ plan = _drive_native(model_gib = 131,gpus = [(0, 97_000)],native_ctx = 196608,@@ -414,11 +404,10 @@ class TestExtraArgsCtxOverride:assert plan["use_fit"] is Truedef test_ctx_size_extra_wins_over_first_class_field(self):- plan = _drive(+ plan = _drive_native(n_ctx = 4096,model_gib = 8,gpus = [(0, 24_000)],- native_ctx = 131072,extra_args = ["--ctx-size", "128000"],)assert plan["original_ctx"] == 128000@@ -432,36 +421,22 @@ class TestExtraArgsCtxOverride:class TestFittableAutoPickRegressions:def test_small_model_one_gpu(self):- plan = _drive(- n_ctx = 0,- model_gib = 8,- gpus = [(0, 24_000)],- native_ctx = 131072,- kv_per_token_bytes = 8192,- )+ plan = _drive_native(model_gib = 8, gpus = [(0, 24_000)], kv_per_token_bytes = 8192)assert plan["use_fit"] is Falseassert plan["gpu_indices"] == [0]assert plan["c_arg"] > FIT_MIN_CTXdef test_medium_model_needs_multi_gpu(self):- plan = _drive(- n_ctx = 0,+ plan = _drive_native(model_gib = 60,gpus = [(0, 40_000), (1, 40_000)],- native_ctx = 131072,kv_per_token_bytes = 8192,)assert plan["use_fit"] is Falseassert plan["gpu_indices"] == [0, 1]def test_no_kv_metadata_fittable_auto(self):- plan = _drive(- n_ctx = 0,- model_gib = 8,- gpus = [(0, 24_000)],- native_ctx = 131072,- can_estimate_kv = False,- )+ plan = _drive_native(model_gib = 8, gpus = [(0, 24_000)], can_estimate_kv = False)assert plan["use_fit"] is Falseassert plan["gpu_indices"] == [0]@@ -477,23 +452,16 @@ class TestTightFitPinsToGPU:def test_rtx_4090_qwen_24gb_class(self):# noahterbest's #5106 log: 20.8 GB model on 22805 MiB free GPU,# ctx=4096 -> ~94% utilization, ~1.4 GiB headroom.- plan = _drive(- n_ctx = 0,- model_gib = 20.8,- gpus = [(0, 22_805)],- native_ctx = 131072,- kv_per_token_bytes = 25_000,- )+ plan = _drive_native(model_gib = 20.8, gpus = [(0, 22_805)], kv_per_token_bytes = 25_000)assert plan["use_fit"] is Falseassert plan["gpu_indices"] == [0]def test_explicit_ctx_at_94_pct_pins_to_gpu(self):# Explicit-ctx branch must agree with auto-ctx on headroom.- plan = _drive(+ plan = _drive_native(n_ctx = 4096,model_gib = 20.8,gpus = [(0, 22_805)],- native_ctx = 131072,kv_per_token_bytes = 25_000,)assert plan["use_fit"] is False@@ -501,11 +469,10 @@ class TestTightFitPinsToGPU:def test_genuine_overflow_still_uses_fit(self):# Beyond 95% must still defer to --fit on.- plan = _drive(+ plan = _drive_native(n_ctx = 4096,model_gib = 23,gpus = [(0, 22_000)],- native_ctx = 131072,kv_per_token_bytes = 25_000,)assert plan["use_fit"] is True@@ -894,8 +861,7 @@ class TestAppleBranchEndToEnd:"""Drive the Apple elif glue (cap / floor / explicit) via _drive, no GPU."""def test_auto_context_capped_below_native(self):- plan = _drive(- n_ctx = 0,+ plan = _drive_native(model_gib = 15.7,gpus = [],native_ctx = 262144,@@ -909,8 +875,7 @@ class TestAppleBranchEndToEnd:def test_floors_to_fallback_when_weights_exceed_budget(self):# Weights alone exceed budget: ctx can't help, so floor to 4096.- plan = _drive(- n_ctx = 0,+ plan = _drive_native(model_gib = 100,gpus = [],native_ctx = 262144,@@ -979,8 +944,7 @@ class TestAppleNoKvMetadataFloor:"""Sparse KV metadata keeps Apple's safety floor instead of native context."""def test_sparse_kv_floors_auto_context(self):- plan = _drive(- n_ctx = 0,+ plan = _drive_native(model_gib = 15.7,gpus = [],native_ctx = 262144,diff --git a/studio/backend/tests/test_llama_cpp_load_progress.py b/studio/backend/tests/test_llama_cpp_load_progress.pyindex 530f0755fb9..cbe28067347 100644--- a/studio/backend/tests/test_llama_cpp_load_progress.py+++ b/studio/backend/tests/test_llama_cpp_load_progress.py@@ -81,6 +81,7 @@ except ImportError:sys.modules.setdefault("httpx", _httpx_stub)from core.inference.llama_cpp import LlamaCppBackend+import io# ---------------------------------------------------------------------------@@ -140,7 +141,6 @@ class TestLoadProgressSingleShard:# Patch /proc read to claim 10 GB RSS.def fake_open(path, *args, **kwargs):if str(path).startswith("/proc/"):- import ioreturn io.StringIO(f"Name:\ttest\nVmRSS:\t{10 * 1024 ** 2}\tkB\n")return open(path, *args, **kwargs) # fall through@@ -164,7 +164,6 @@ class TestLoadProgressSingleShard:def fake_open(path, *args, **kwargs):if str(path).startswith("/proc/"):- import ioreturn io.StringIO(f"VmRSS:\t{8 * 1024 ** 2}\tkB\n")return open(path, *args, **kwargs)@@ -198,7 +197,6 @@ class TestLoadProgressMultiShard:def fake_open(path, *args, **kwargs):if str(path).startswith("/proc/"):- import ioreturn io.StringIO("VmRSS:\t0\tkB\n")return open(path, *args, **kwargs)@@ -220,7 +218,6 @@ class TestLoadProgressDegradation:def fake_open(path, *args, **kwargs):if str(path).startswith("/proc/"):- import ioreturn io.StringIO("VmRSS:\t1024\tkB\n")return open(path, *args, **kwargs)diff --git a/studio/backend/tests/test_llama_cpp_mtp_detection.py b/studio/backend/tests/test_llama_cpp_mtp_detection.pyindex c444b09dd62..00232d0bc5a 100644--- a/studio/backend/tests/test_llama_cpp_mtp_detection.py+++ b/studio/backend/tests/test_llama_cpp_mtp_detection.py@@ -80,12 +80,43 @@ from core.inference.llama_cpp import (_mla_mtp_auto_enabled,_swa_full_from_args_or_env,)+from core.inference import llama_cpp as mod+import subprocess as _subprocessdef _matches(backend: LlamaCppBackend, **kwargs) -> bool:return backend.adopt_load_intent_if_matched(GgufLoadIntent(**kwargs))+def _matches_mtp(+ *args,+ cache_type_kv = None,+ chat_template_override = None,+ extra_args = None,+ gguf_path = None,+ hf_variant = "Q4_K_M",+ is_vision = False,+ model_identifier = "unsloth/Qwen3.6-27B-MTP-GGUF",+ n_ctx = 8192,+ speculative_type = None,+ **kwargs,+):+ """_matches against the MTP model with the request fields every caller below repeats."""+ return _matches(+ *args,+ cache_type_kv = cache_type_kv,+ chat_template_override = chat_template_override,+ extra_args = extra_args,+ gguf_path = gguf_path,+ hf_variant = hf_variant,+ is_vision = is_vision,+ model_identifier = model_identifier,+ n_ctx = n_ctx,+ speculative_type = speculative_type,+ **kwargs,+ )++# Synthetic GGUF helper (mirrors test_gguf_metadata.py)._GGUF_MAGIC = 0x46554747@@ -264,43 +295,36 @@ def _mtp_backend(**overrides):return backend+def _ngram_mod_backend(+ *args,+ _spec_draft_n_max = None,+ _speculative_type = "ngram-mod",+ **kwargs,+):+ """_mtp_backend already sitting on ngram-mod with no draft cap."""+ return _mtp_backend(+ *args, _spec_draft_n_max = _spec_draft_n_max, _speculative_type = _speculative_type, **kwargs+ )+++def _mtp_backend_default(+ *args,+ _speculative_type = "default",+ **kwargs,+):+ """_mtp_backend left on the default speculative type."""+ return _mtp_backend(*args, _speculative_type = _speculative_type, **kwargs)++def test_already_in_target_state_matches_when_request_omits_spec_for_mtp_model():# Duplicate /load with no spec must match a running draft-mtp backend.backend = _mtp_backend()- assert (- _matches(- backend,- gguf_path = None,- model_identifier = "unsloth/Qwen3.6-27B-MTP-GGUF",- hf_variant = "Q4_K_M",- n_ctx = 8192,- cache_type_kv = None,- speculative_type = None,- chat_template_override = None,- extra_args = None,- is_vision = False,- )- is True- )+ assert _matches_mtp(backend) is Truedef test_already_in_target_state_matches_when_request_uses_default_for_mtp_model():backend = _mtp_backend()- assert (- _matches(- backend,- gguf_path = None,- model_identifier = "unsloth/Qwen3.6-27B-MTP-GGUF",- hf_variant = "Q4_K_M",- n_ctx = 8192,- cache_type_kv = None,- speculative_type = "default",- chat_template_override = None,- extra_args = None,- is_vision = False,- )- is True- )+ assert _matches_mtp(backend, speculative_type = "default") is Truedef test_already_in_target_state_auto_request_matches_auto_backend_for_non_mtp_model():@@ -308,51 +332,26 @@ def test_already_in_target_state_auto_request_matches_auto_backend_for_non_mtp_m# of model name. The resolved emission (--spec-default vs draft-mtp) is# handled by the load path and reflected in _speculative_type; the# short-circuit only cares whether the *intent* changed.- backend = _mtp_backend(- _model_identifier = "unsloth/Qwen3.6-27B-GGUF",- _speculative_type = "default",- )- assert (- _matches(- backend,- gguf_path = None,- model_identifier = "unsloth/Qwen3.6-27B-GGUF",- hf_variant = "Q4_K_M",- n_ctx = 8192,- cache_type_kv = None,- speculative_type = None,- chat_template_override = None,- extra_args = None,- is_vision = False,- )- is True- )+ backend = _mtp_backend_default(_model_identifier = "unsloth/Qwen3.6-27B-GGUF")+ assert _matches_mtp(backend, model_identifier = "unsloth/Qwen3.6-27B-GGUF") is Truedef test_forced_dspark_without_a_sidecar_stops_reloading():"""drafter_not_found covers both "the fetch failed" and "this repo publishesnone". The second is the permanent state of every repo but one, so retryingit relaunched an identical server on every Apply."""- backend = _mtp_backend(+ backend = _mtp_backend_default(_model_identifier = "unsloth/Qwen3-7B-GGUF",- _speculative_type = "default",_requested_spec_mode = "dspark",_spec_fallback_reason = "drafter_not_found",_spec_drafter_kind = "dspark",_dspark_sidecar_absent = True,)assert (- _matches(+ _matches_mtp(backend,- gguf_path = None,model_identifier = "unsloth/Qwen3-7B-GGUF",- hf_variant = "Q4_K_M",- n_ctx = 8192,- cache_type_kv = None,speculative_type = "dspark",- chat_template_override = None,- extra_args = None,- is_vision = False,)is True)@@ -361,26 +360,18 @@ def test_forced_dspark_without_a_sidecar_stops_reloading():def test_forced_dspark_retries_when_the_fetch_failed_rather_than_the_repo():"""The other half: the repo does publish a sidecar and the download failed, sothe next Apply should reload and re-run _download_dspark."""- backend = _mtp_backend(+ backend = _mtp_backend_default(_model_identifier = "unsloth/DeepSeek-V4-Flash-0731-GGUF",- _speculative_type = "default",_requested_spec_mode = "dspark",_spec_fallback_reason = "drafter_not_found",_spec_drafter_kind = "dspark",_dspark_sidecar_absent = False,)assert (- _matches(+ _matches_mtp(backend,- gguf_path = None,model_identifier = "unsloth/DeepSeek-V4-Flash-0731-GGUF",- hf_variant = "Q4_K_M",- n_ctx = 8192,- cache_type_kv = None,speculative_type = "dspark",- chat_template_override = None,- extra_args = None,- is_vision = False,)is False)@@ -388,25 +379,17 @@ def test_forced_dspark_retries_when_the_fetch_failed_rather_than_the_repo():def test_mtp_without_a_drafter_still_reloads_to_retry_the_fetch():"""Negative control for the above: the MTP retry must survive."""- backend = _mtp_backend(+ backend = _mtp_backend_default(_model_identifier = "unsloth/gemma-4-12b-it-GGUF",- _speculative_type = "default",_requested_spec_mode = "mtp",_spec_fallback_reason = "drafter_not_found",_spec_drafter_kind = "mtp",)assert (- _matches(+ _matches_mtp(backend,- gguf_path = None,model_identifier = "unsloth/gemma-4-12b-it-GGUF",- hf_variant = "Q4_K_M",- n_ctx = 8192,- cache_type_kv = None,speculative_type = "mtp",- chat_template_override = None,- extra_args = None,- is_vision = False,)is False)@@ -424,17 +407,9 @@ def test_auto_resolved_dspark_reuses_its_server_instead_of_reloading(tmp_path):_mtp_draft_path = str(sidecar),)assert (- _matches(+ _matches_mtp(backend,- gguf_path = None,model_identifier = "unsloth/DeepSeek-V4-Flash-0731-GGUF",- hf_variant = "Q4_K_M",- n_ctx = 8192,- cache_type_kv = None,- speculative_type = None,- chat_template_override = None,- extra_args = None,- is_vision = False,dspark_draft_path = str(sidecar),compare_mtp_draft = True,)@@ -444,21 +419,7 @@ def test_auto_resolved_dspark_reuses_its_server_instead_of_reloading(tmp_path):def test_already_in_target_state_explicit_off_still_mismatches_mtp_backend():backend = _mtp_backend()- assert (- _matches(- backend,- gguf_path = None,- model_identifier = "unsloth/Qwen3.6-27B-MTP-GGUF",- hf_variant = "Q4_K_M",- n_ctx = 8192,- cache_type_kv = None,- speculative_type = "off",- chat_template_override = None,- extra_args = None,- is_vision = False,- )- is False- )+ assert _matches_mtp(backend, speculative_type = "off") is False# User override via extra_args (unsloth run / unsloth studio run).@@ -594,21 +555,7 @@ def test_already_in_target_state_user_spec_type_override_matches_clean_backend()_requested_spec_mode = None,_extra_args = ["--spec-type", "none"],)- assert (- _matches(- backend,- gguf_path = None,- model_identifier = "unsloth/Qwen3.6-27B-MTP-GGUF",- hf_variant = "Q4_K_M",- n_ctx = 8192,- cache_type_kv = None,- speculative_type = None,- chat_template_override = None,- extra_args = ["--spec-type", "none"],- is_vision = False,- )- is True- )+ assert _matches_mtp(backend, extra_args = ["--spec-type", "none"]) is Truedef test_already_in_target_state_local_file_mtp_match(tmp_path):@@ -621,17 +568,11 @@ def test_already_in_target_state_local_file_mtp_match(tmp_path):_hf_variant = None,)assert (- _matches(+ _matches_mtp(backend,gguf_path = str(gguf),model_identifier = "local-qwen-mtp",hf_variant = None,- n_ctx = 8192,- cache_type_kv = None,- speculative_type = None,- chat_template_override = None,- extra_args = None,- is_vision = False,)is True)@@ -641,40 +582,12 @@ def test_already_in_target_state_vision_mtp_match():# llama.cpp #22673: MTP is compatible with mmproj. A vision MTP load# with auto/default spec must match a backend already running draft-mtp.backend = _mtp_backend(_is_vision = True)- assert (- _matches(- backend,- gguf_path = None,- model_identifier = "unsloth/Qwen3.6-27B-MTP-GGUF",- hf_variant = "Q4_K_M",- n_ctx = 8192,- cache_type_kv = None,- speculative_type = None,- chat_template_override = None,- extra_args = None,- is_vision = True,- )- is True- )+ assert _matches_mtp(backend, is_vision = True) is Truedef test_already_in_target_state_vision_mtp_default_matches():backend = _mtp_backend(_is_vision = True)- assert (- _matches(- backend,- gguf_path = None,- model_identifier = "unsloth/Qwen3.6-27B-MTP-GGUF",- hf_variant = "Q4_K_M",- n_ctx = 8192,- cache_type_kv = None,- speculative_type = "default",- chat_template_override = None,- extra_args = None,- is_vision = True,- )- is True- )+ assert _matches_mtp(backend, speculative_type = "default", is_vision = True) is Truedef test_already_in_target_state_vision_off_matches_vision_backend():@@ -688,16 +601,10 @@ def test_already_in_target_state_vision_off_matches_vision_backend():_requested_spec_mode = "off",)assert (- _matches(+ _matches_mtp(backend,- gguf_path = None,model_identifier = "unsloth/Qwen3-VL-4B-Instruct-GGUF",- hf_variant = "Q4_K_M",- n_ctx = 8192,- cache_type_kv = None,speculative_type = "off",- chat_template_override = None,- extra_args = None,is_vision = True,)is True@@ -1286,19 +1193,7 @@ def _draft_n_max_matches(*,speculative_type = None,):- return _matches(- backend,- gguf_path = None,- model_identifier = "unsloth/Qwen3.6-27B-MTP-GGUF",- hf_variant = "Q4_K_M",- n_ctx = 8192,- cache_type_kv = None,- speculative_type = speculative_type,- spec_draft_n_max = requested,- chat_template_override = None,- extra_args = None,- is_vision = False,- )+ return _matches_mtp(backend, speculative_type = speculative_type, spec_draft_n_max = requested)def test_already_in_target_state_matches_when_draft_n_max_unset():@@ -1345,9 +1240,8 @@ def test_mtp_draft_n_max_compares_saved_runtime_fallback_intent(saved_draft_n_madef test_mtp_draft_n_max_ignored_when_binary_lacks_mtp():- backend = _mtp_backend(+ backend = _mtp_backend_default(_requested_spec_mode = "mtp",- _speculative_type = "default",_spec_draft_n_max = None,_spec_fallback_reason = "binary_no_mtp",)@@ -1376,11 +1270,7 @@ def test_partial_offload_stand_down_follows_the_draft_depth(decided_at, requestedef test_already_in_target_state_draft_n_max_ignored_when_not_mtp():# ngram-mod backend; spec_draft_n_max is MTP-only and must not force# a reload against a non-MTP active spec.- backend = _mtp_backend(- _speculative_type = "ngram-mod",- _requested_spec_mode = "ngram",- _spec_draft_n_max = None,- )+ backend = _ngram_mod_backend(_requested_spec_mode = "ngram")assert _draft_n_max_matches(backend, 8, speculative_type = "ngram-mod")@@ -1417,102 +1307,36 @@ def test_already_in_target_state_sub_3b_falls_back_to_ngram_mod_when_supported(m# 0.8B MTP request -- load_model would have promoted to ngram-mod (no MTP# head); reload check must match a ngram-mod backend._patch_probe(monkeypatch, ngram_supported = True)- backend = _mtp_backend(- _model_identifier = "unsloth/Qwen3.5-0.8B-MTP-GGUF",- _speculative_type = "ngram-mod",- _spec_draft_n_max = None,- )- assert (- _matches(- backend,- gguf_path = None,- model_identifier = "unsloth/Qwen3.5-0.8B-MTP-GGUF",- hf_variant = "Q4_K_M",- n_ctx = 8192,- cache_type_kv = None,- speculative_type = None,- chat_template_override = None,- extra_args = None,- is_vision = False,- )- is True- )+ backend = _ngram_mod_backend(_model_identifier = "unsloth/Qwen3.5-0.8B-MTP-GGUF")+ assert _matches_mtp(backend, model_identifier = "unsloth/Qwen3.5-0.8B-MTP-GGUF") is Truedef test_already_in_target_state_sub_3b_falls_back_to_off_when_no_ngram(monkeypatch):# 0.8B + binary lacks ngram-mod -> fall back to off._patch_probe(monkeypatch, ngram_supported = False)- backend = _mtp_backend(+ backend = _ngram_mod_backend(_model_identifier = "unsloth/Qwen3.5-0.8B-MTP-GGUF",_speculative_type = None,- _spec_draft_n_max = None,- )- assert (- _matches(- backend,- gguf_path = None,- model_identifier = "unsloth/Qwen3.5-0.8B-MTP-GGUF",- hf_variant = "Q4_K_M",- n_ctx = 8192,- cache_type_kv = None,- speculative_type = None,- chat_template_override = None,- extra_args = None,- is_vision = False,- )- is True)+ assert _matches_mtp(backend, model_identifier = "unsloth/Qwen3.5-0.8B-MTP-GGUF") is Truedef test_already_in_target_state_4b_mtp_request_promotes_as_before(monkeypatch):# 4B is above the 3B threshold -> auto-promote still applies._patch_probe(monkeypatch, ngram_supported = True)- backend = _mtp_backend(+ backend = _ngram_mod_backend(_model_identifier = "unsloth/Qwen3.5-4B-MTP-GGUF",_speculative_type = "draft-mtp",- _spec_draft_n_max = None,- )- assert (- _matches(- backend,- gguf_path = None,- model_identifier = "unsloth/Qwen3.5-4B-MTP-GGUF",- hf_variant = "Q4_K_M",- n_ctx = 8192,- cache_type_kv = None,- speculative_type = None,- chat_template_override = None,- extra_args = None,- is_vision = False,- )- is True)+ assert _matches_mtp(backend, model_identifier = "unsloth/Qwen3.5-4B-MTP-GGUF") is Truedef test_already_in_target_state_2b_falls_back_to_ngram_below_threshold(monkeypatch):# 2.0B is below the 3B threshold -> ngram-mod fallback, not draft-mtp.# Clean-bench shows 2B regresses with draft-mtp._patch_probe(monkeypatch, ngram_supported = True)- backend = _mtp_backend(- _model_identifier = "unsloth/Qwen3.5-2B-MTP-GGUF",- _speculative_type = "ngram-mod",- _spec_draft_n_max = None,- )- assert (- _matches(- backend,- gguf_path = None,- model_identifier = "unsloth/Qwen3.5-2B-MTP-GGUF",- hf_variant = "Q4_K_M",- n_ctx = 8192,- cache_type_kv = None,- speculative_type = None,- chat_template_override = None,- extra_args = None,- is_vision = False,- )- is True- )+ backend = _ngram_mod_backend(_model_identifier = "unsloth/Qwen3.5-2B-MTP-GGUF")+ assert _matches_mtp(backend, model_identifier = "unsloth/Qwen3.5-2B-MTP-GGUF") is True# usage backfill from timings (Unsloth UI t/s widget fix).@@ -2394,21 +2218,7 @@ def test_reload_skip_auto_mla_ngram_is_idempotent():_speculative_type = "ngram-mod",_requested_spec_mode = "auto",)- assert (- _matches(- backend,- gguf_path = None,- model_identifier = _GLM_MLA_MODEL,- hf_variant = "Q4_K_M",- n_ctx = 8192,- cache_type_kv = None,- speculative_type = "auto",- chat_template_override = None,- extra_args = None,- is_vision = False,- )- is True- )+ assert _matches_mtp(backend, model_identifier = _GLM_MLA_MODEL, speculative_type = "auto") is Truedef test_reload_forced_mtp_bounces_auto_mla():@@ -2419,21 +2229,7 @@ def test_reload_forced_mtp_bounces_auto_mla():_speculative_type = "ngram-mod",_requested_spec_mode = "auto",)- assert (- _matches(- backend,- gguf_path = None,- model_identifier = _GLM_MLA_MODEL,- hf_variant = "Q4_K_M",- n_ctx = 8192,- cache_type_kv = None,- speculative_type = "mtp",- chat_template_override = None,- extra_args = None,- is_vision = False,- )- is False- )+ assert _matches_mtp(backend, model_identifier = _GLM_MLA_MODEL, speculative_type = "mtp") is False# glm5next matches the MLA gate on metadata, but its MTP is 1.31x faster, not slower.@@ -3045,9 +2841,8 @@ def test_already_in_target_state_settles_a_dflash_listing_that_never_answered():_dflash_sidecar_absent stays False. The drafter_not_found arm read that as "worthanother go" and relaunched a healthy drafter-free server on every Apply. DFlashasks through _dflash_retry_needed instead, which a permanent error never sets."""- backend = _mtp_backend(+ backend = _mtp_backend_default(_model_identifier = "unsloth/Muse-Glimmer-30B-GGUF",- _speculative_type = "default",_gguf_path = None,_spec_fallback_reason = "drafter_not_found",_spec_drafter_kind = "dflash",@@ -3060,9 +2855,8 @@ def test_already_in_target_state_settles_a_dflash_listing_that_never_answered():def test_already_in_target_state_reloads_after_a_dflash_fetch_that_dropped():"""Under Auto a lost sidecar leaves no fallback reason at all -- the promotionnever ran -- so the flag is the only thing that can ask for one more attempt."""- backend = _mtp_backend(+ backend = _mtp_backend_default(_model_identifier = "unsloth/Muse-Glimmer-30B-GGUF",- _speculative_type = "default",_gguf_path = None,)assert _matches(backend, **_binary_fallback_kwargs()) is True@@ -3156,8 +2950,6 @@ def test_a_code_integrity_block_escalates_the_retry_window(tmp_path, monkeypatchdef test_inconclusive_probe_retries_after_a_bounded_cache_window(tmp_path, monkeypatch):"""A transient timeout may not be pinned for the whole process, while apersistent failure may not make every capability caller wait again (#8317)."""- import subprocess as _subprocess-fake = _make_fake_llama_server(tmp_path / "llama-server","--spec-type none,draft-mtp,ngram-mod",@@ -3210,7 +3002,6 @@ def test_inconclusive_probe_retries_after_a_bounded_cache_window(tmp_path, monke@_NEEDS_BASHdef test_concurrent_timeout_cannot_overwrite_a_successful_probe(tmp_path, monkeypatch):"""A late, lower-confidence result may not replace a conclusive result."""- import subprocess as _subprocessimport threading as _threadingfake = _make_fake_llama_server(@@ -3299,8 +3090,6 @@ def test_a_hanging_binary_is_probed_once_per_model_load(tmp_path, monkeypatch):"""The cost half of #8317, stated as the caller sees it. One model load makes sevencapability calls; a binary that hangs for a permanent reason must pay the --helptimeout once across all of them, not once each."""- import subprocess as _subprocess-fake = _make_fake_llama_server(tmp_path / "llama-server", "--spec-type none,draft-mtp")_clear_caps_cache()now = [100.0]@@ -3341,8 +3130,7 @@ def test_a_missing_binary_is_not_cached_so_it_is_seen_as_soon_as_it_lands(tmp_padef _inconclusive_fallback_backend():"""A backend that asked for MTP, got an inconclusive probe, and launched withoutspeculative decoding. _spec_fallback_reason stays None so the UI banner is suppressed."""- return _mtp_backend(- _speculative_type = "default",+ return _mtp_backend_default(_spec_fallback_reason = None,_capability_probe_inconclusive = True,_gguf_path = None,@@ -3374,26 +3162,29 @@ def _stub_caps(monkeypatch, **caps):)+def _stub_found_caps(+ *args,+ found = True,+ mtp_probe_inconclusive = False,+ **kwargs,+):+ """_stub_caps for a binary whose probe found a conclusive answer."""+ return _stub_caps(*args, found = found, mtp_probe_inconclusive = mtp_probe_inconclusive, **kwargs)++def test_apply_reloads_once_an_inconclusive_probe_starts_answering(monkeypatch):# The retry window is worth nothing if Apply dedupes against the fallback: nothing# re-probes, so MTP stays off for the life of the process, which is the symptom the# window exists to end (#8317).- _stub_caps(- monkeypatch,- found = True,- mtp_token = "draft-mtp",- supports_mtp = True,- mtp_probe_inconclusive = False,- )+ _stub_found_caps(monkeypatch, mtp_token = "draft-mtp", supports_mtp = True)assert _matches(_inconclusive_fallback_backend(), **_same_settings_apply()) is Falsedef test_apply_still_dedupes_while_the_probe_keeps_hanging(monkeypatch):# A binary that hangs for a permanent reason must not relaunch an identical server on# every Apply. Only a probe that has actually turned conclusive earns the reload.- _stub_caps(+ _stub_found_caps(monkeypatch,- found = True,mtp_token = None,supports_mtp = False,mtp_probe_inconclusive = True,@@ -3405,13 +3196,7 @@ def test_apply_reloads_once_even_when_the_build_turns_out_to_have_no_mtp(monkeyp# Conclusive-and-negative still earns exactly one reload: the degradation has to be# re-derived from a real answer rather than from a probe that never returned. That# reload records the conclusive probe, clearing the flag, so it does not loop.- _stub_caps(- monkeypatch,- found = True,- mtp_token = None,- supports_mtp = False,- mtp_probe_inconclusive = False,- )+ _stub_found_caps(monkeypatch, mtp_token = None, supports_mtp = False)assert _matches(_inconclusive_fallback_backend(), **_same_settings_apply()) is False# Cleared flag (what the reload leaves behind) dedupes from then on.settled = _mtp_backend(_speculative_type = "default", _gguf_path = None)@@ -3424,16 +3209,13 @@ def test_a_slot_clamp_from_an_inconclusive_probe_is_also_retried(monkeypatch):# 1 while _requested_n_parallel keeps the ASK. The two then compare equal and Apply# would never restore the slots once the probe recovers. No speculative decoding is# involved, so the spec-only version of this guard missed it entirely.- _stub_caps(+ _stub_found_caps(monkeypatch,- found = True,mtp_token = "draft-mtp",supports_mtp = True,supports_kv_unified = True,- mtp_probe_inconclusive = False,)- clamped = _mtp_backend(- _speculative_type = "default",+ clamped = _mtp_backend_default(_capability_probe_inconclusive = True,_requested_n_parallel = 4,_gguf_path = None,@@ -3452,11 +3234,6 @@ def test_the_probe_marker_is_committed_only_once_the_runtime_is_replaced():degraded, or set it on a diffusion runner that would then be torn down and reloadedfor no reason. Checked structurally because load_model is not unit-callable."""- import ast- import inspect-- from core.inference import llama_cpp as mod-source = inspect.getsource(mod)tree = ast.parse(source)load_model = next(@@ -3494,15 +3271,8 @@ def test_a_diffusion_runtime_is_never_reloaded_by_the_capability_recovery(monkey# A diffusion runner consumes no llama-server capability, so it cannot be degraded by# one. A marker left over from an earlier llama-server load must not make every# otherwise identical diffusion Apply tear it down and start it again.- _stub_caps(- monkeypatch,- found = True,- mtp_token = "draft-mtp",- supports_mtp = True,- mtp_probe_inconclusive = False,- )- diffusion = _mtp_backend(- _speculative_type = "default",+ _stub_found_caps(monkeypatch, mtp_token = "draft-mtp", supports_mtp = True)+ diffusion = _mtp_backend_default(_capability_probe_inconclusive = True,_is_diffusion = True,_gguf_path = None,@@ -3524,10 +3294,6 @@ def test_a_diffusion_load_never_pays_for_the_capability_probe():# The probe is read at the snapshot commit, which a diffusion load returns long# before. Reading it beside the capability gates instead made an independent# diffusion launch wait out the full --help timeout this change exists to bound.- import ast- import inspect-- from core.inference import llama_cpp as modload_model = next(node@@ -3598,11 +3364,6 @@ def test_the_marker_comes_from_the_launch_snapshot_not_a_probe_after_startup():for good -- the original bug, reintroduced. Checked structurally because load_model isnot unit-callable."""- import ast- import inspect-- from core.inference import llama_cpp as mod-load_model = next(nodefor node in ast.walk(ast.parse(inspect.getsource(mod)))@@ -3653,11 +3414,6 @@ def test_a_later_successful_probe_cannot_erase_an_earlier_degrading_one():Exercised on the accumulator itself: driving load_model would need a real download."""- import ast- import inspect-- from core.inference import llama_cpp as mod-load_model = next(nodefor node in ast.walk(ast.parse(inspect.getsource(mod)))@@ -3725,11 +3481,6 @@ def test_the_dspark_pre_download_gate_latches_into_the_launch_accumulator():launch probe can come back conclusive and the load is remembered as a good one --every identical Apply after it then dedupes against a server with no drafter."""- import ast- import inspect-- from core.inference import llama_cpp as mod-tree = ast.parse(inspect.getsource(mod))download = next(node@@ -3775,8 +3526,6 @@ def test_the_dspark_gate_uses_the_probe_it_is_given():drives the skip. A default is kept so the direct callers in the tests and the CLIkeep working unchanged."""- import inspect-from core.inference.llama_cpp import LlamaCppBackendsignature = inspect.signature(LlamaCppBackend._download_dspark)diff --git a/studio/backend/tests/test_llama_cpp_non_chat_gguf_preflight.py b/studio/backend/tests/test_llama_cpp_non_chat_gguf_preflight.pyindex 4c9359f3a49..15bc62ba008 100644--- a/studio/backend/tests/test_llama_cpp_non_chat_gguf_preflight.py+++ b/studio/backend/tests/test_llama_cpp_non_chat_gguf_preflight.py@@ -213,7 +213,10 @@ def test_the_shared_arch_verdict_matches_the_pickers(tmp_path):# The invariant: the refusal names the Images page exactly when# routes.models._arch_to_task says the row is selectable there, or the message points at# a list the model is not in.+ from routes.models import _arch_to_task+from routes.models import _AMBIGUOUS_DIFFUSION_GGUF_ARCHS, _arch_to_task+assert LlamaCppBackend._AMBIGUOUS_IMAGE_ARCHES == _AMBIGUOUS_DIFFUSION_GGUF_ARCHSfor identifier, name in (("unsloth/Z-Image-Turbo-GGUF", "z-image-turbo-Q2_K.gguf"),diff --git a/studio/backend/tests/test_llama_cpp_path_settings.py b/studio/backend/tests/test_llama_cpp_path_settings.pyindex 1b39c8f7d43..8b479cb019d 100644--- a/studio/backend/tests/test_llama_cpp_path_settings.py+++ b/studio/backend/tests/test_llama_cpp_path_settings.py@@ -219,6 +219,8 @@ def test_runtime_resolver_uses_studio_path_and_does_not_silently_fallback(def test_runtime_resolver_rejects_a_selected_binary_that_loses_execute_permission(settings_store, monkeypatch, tmp_path):+ from core.inference.llama_cpp import LlamaCppBackend+from core.inference import llama_cpp as llama_cpp_modulefrom core.inference.llama_cpp import (LLAMA_SERVER_NOT_FOUND_DETAIL,@@ -297,9 +299,10 @@ def test_settings_route_round_trips_the_selected_folder(settings_store, monkeypadef test_settings_route_reports_reload_while_old_binary_launch_is_pending(monkeypatch):- from routes import inference as inference_routefrom routes import settings as settings_route+ from routes import inference as inference_route+class _PendingBackend:is_active = False_binary_revision_pending = ("old-binary",)@@ -314,9 +317,10 @@ def test_settings_route_reports_reload_while_old_binary_launch_is_pending(monkeydef test_settings_route_rejects_api_key_writes_before_mutation(monkeypatch):- from fastapi import HTTPExceptionfrom routes import settings as settings_route+ from fastapi import HTTPException+mutated = Falsedef _unexpected_mutation(_path):@@ -337,9 +341,10 @@ def test_settings_route_rejects_api_key_writes_before_mutation(monkeypatch):def test_settings_route_returns_the_specific_validation_error(settings_store, tmp_path):- from fastapi import HTTPExceptionfrom routes import settings as settings_route+ from fastapi import HTTPException+empty = tmp_path / "empty"empty.mkdir()diff --git a/studio/backend/tests/test_llama_cpp_placement.py b/studio/backend/tests/test_llama_cpp_placement.pyindex 34c36f55019..f129d9cb886 100644--- a/studio/backend/tests/test_llama_cpp_placement.py+++ b/studio/backend/tests/test_llama_cpp_placement.py@@ -110,6 +110,15 @@ def _backend(tmp_path: Path, *, vulkan: bool, memory):return backend, gguf+def _backend_non_vulkan(+ *args,+ vulkan = False,+ **kwargs,+):+ """_backend on a non-vulkan host."""+ return _backend(*args, vulkan = vulkan, **kwargs)++def _launch(backend, gguf, **load_kwargs):captured = {}@@ -142,6 +151,33 @@ def _launch(backend, gguf, **load_kwargs):return captured+def _launch_auto_8k(+ *args,+ n_ctx = 8192,+ speculative_type = "auto",+ **kwargs,+):+ """_launch at 8k context with auto speculation.++ A caller passing ``n_ctx = 0`` means Auto context (the branch that caps); the native+ 8192 named here is then the target it caps down from, not a requested context.+ """+ return _launch(*args, n_ctx = n_ctx, speculative_type = speculative_type, **kwargs)+++def _launch_auto_spec(+ *args,+ n_ctx = 4096,+ n_parallel = 4,+ speculative_type = "auto",+ **kwargs,+):+ """_launch with the 4k/4-slot auto-speculative load the placement cases share."""+ return _launch(+ *args, n_ctx = n_ctx, n_parallel = n_parallel, speculative_type = speculative_type, **kwargs+ )++def _launch_warns(backend, gguf, **load_kwargs):"""A load whose weights outgrow fast memory: it LAUNCHES, and says so.@@ -225,11 +261,7 @@ def test_vulkan_fit_and_mtp_drafter_follow_placement_owner(@pytest.mark.parametrize("use_fit", [False, True])def test_dspark_composed_argv_respects_placement_fit_decision(tmp_path, use_fit):- backend, gguf = _backend(- tmp_path,- vulkan = False,- memory = [(0, 24_000, 24_000)],- )+ backend, gguf = _backend_non_vulkan(tmp_path, memory = [(0, 24_000, 24_000)])sidecar = tmp_path / "dspark-model-Q8_0.gguf"sidecar.write_bytes(b"draft")backend._select_gpus = lambda *args, **kwargs: (None, True) if use_fit else ([0], False)@@ -307,9 +339,8 @@ def test_pass_through_dspark_loads_under_an_auto_fit_placement(tmp_path):def test_cuda_selection_uses_visibility_and_removes_environment_placement(tmp_path, monkeypatch):monkeypatch.setenv("LLAMA_ARG_DEVICE", "CUDA0")monkeypatch.setenv("LLAMA_ARG_MAIN_GPU", "0")- backend, gguf = _backend(+ backend, gguf = _backend_non_vulkan(tmp_path,- vulkan = False,memory = [(0, 10_000, 16_000), (1, 8_000, 16_000)],)backend._select_gpus = lambda *args, **kwargs: ([1], False)@@ -385,9 +416,8 @@ def _hybrid_mtp_backend(partial_offload: bool,memory = None,):- backend, gguf = _backend(+ backend, gguf = _backend_non_vulkan(tmp_path,- vulkan = False,memory = [(0, 12 * 1024, 12 * 1024)] if memory is None else memory,)@@ -420,13 +450,7 @@ def _hybrid_mtp_backend(def test_auto_disables_embedded_hybrid_mtp_under_partial_offload(tmp_path):backend, gguf = _hybrid_mtp_backend(tmp_path, partial_offload = True)- result = _launch(- backend,- gguf,- speculative_type = "auto",- n_ctx = 4096,- n_parallel = 4,- )+ result = _launch_auto_spec(backend, gguf)cmd = result["cmd"]assert cmd[cmd.index("--fit") + 1] == "on"@@ -439,13 +463,7 @@ def test_auto_disables_embedded_hybrid_mtp_under_partial_offload(tmp_path):def test_forced_embedded_hybrid_mtp_survives_partial_offload(tmp_path):backend, gguf = _hybrid_mtp_backend(tmp_path, partial_offload = True)- result = _launch(- backend,- gguf,- speculative_type = "mtp",- n_ctx = 4096,- n_parallel = 4,- )+ result = _launch_auto_spec(backend, gguf, speculative_type = "mtp")cmd = result["cmd"]assert cmd[cmd.index("--fit") + 1] == "on"@@ -456,13 +474,7 @@ def test_forced_embedded_hybrid_mtp_survives_partial_offload(tmp_path):def test_auto_keeps_embedded_hybrid_mtp_when_fully_offloaded(tmp_path):backend, gguf = _hybrid_mtp_backend(tmp_path, partial_offload = False)- result = _launch(- backend,- gguf,- speculative_type = "auto",- n_ctx = 4096,- n_parallel = 4,- )+ result = _launch_auto_spec(backend, gguf)cmd = result["cmd"]assert cmd[cmd.index("--fit") + 1] == "off"@@ -473,15 +485,7 @@ def test_auto_keeps_embedded_hybrid_mtp_when_fully_offloaded(tmp_path):def test_auto_disables_embedded_hybrid_mtp_with_manual_partial_layers(tmp_path):backend, gguf = _hybrid_mtp_backend(tmp_path, partial_offload = False)- result = _launch(- backend,- gguf,- speculative_type = "auto",- gpu_memory_mode = "manual",- gpu_layers = 42,- n_ctx = 4096,- n_parallel = 4,- )+ result = _launch_auto_spec(backend, gguf, gpu_memory_mode = "manual", gpu_layers = 42)cmd = result["cmd"]assert cmd[cmd.index("--gpu-layers") + 1] == "42"@@ -494,15 +498,7 @@ def test_auto_disables_embedded_hybrid_mtp_with_manual_partial_layers(tmp_path):def test_auto_keeps_embedded_hybrid_mtp_without_manual_partial_layers(tmp_path, gpu_layers):backend, gguf = _hybrid_mtp_backend(tmp_path, partial_offload = False)- result = _launch(- backend,- gguf,- speculative_type = "auto",- gpu_memory_mode = "manual",- gpu_layers = gpu_layers,- n_ctx = 4096,- n_parallel = 4,- )+ result = _launch_auto_spec(backend, gguf, gpu_memory_mode = "manual", gpu_layers = gpu_layers)cmd = result["cmd"]assert cmd[cmd.index("--gpu-layers") + 1] == str(gpu_layers)@@ -517,13 +513,7 @@ def test_auto_keeps_embedded_hybrid_mtp_without_a_gpu(tmp_path):# CPU MTP policy stands.backend, gguf = _hybrid_mtp_backend(tmp_path, partial_offload = True, memory = [])- result = _launch(- backend,- gguf,- speculative_type = "auto",- n_ctx = 4096,- n_parallel = 4,- )+ result = _launch_auto_spec(backend, gguf)cmd = result["cmd"]assert cmd[cmd.index("--fit") + 1] == "on"@@ -537,14 +527,7 @@ def test_auto_keeps_embedded_hybrid_mtp_when_the_device_selection_is_cpu(tmp_pat# and the rollback copies cost no VRAM.backend, gguf = _hybrid_mtp_backend(tmp_path, partial_offload = True)- result = _launch(- backend,- gguf,- speculative_type = "auto",- extra_args = ["--device", "none"],- n_ctx = 4096,- n_parallel = 4,- )+ result = _launch_auto_spec(backend, gguf, extra_args = ["--device", "none"])cmd = result["cmd"]assert cmd[cmd.index("--fit") + 1] == "on"@@ -559,13 +542,10 @@ def test_a_hand_pinned_device_is_gpu_evidence_when_the_probe_found_none(tmp_path# answer, so the two sides agree.backend, gguf = _hybrid_mtp_backend(tmp_path, partial_offload = True, memory = [])- result = _launch(+ result = _launch_auto_spec(backend,gguf,- speculative_type = "auto",extra_args = ["--device", "Vulkan0", "--gpu-layers", "42"],- n_ctx = 4096,- n_parallel = 4,)cmd = result["cmd"]@@ -577,14 +557,7 @@ def test_a_hand_pinned_device_is_gpu_evidence_when_the_probe_found_none(tmp_pathdef test_partial_offload_stand_down_records_the_draft_depth_it_decided_at(tmp_path):backend, gguf = _hybrid_mtp_backend(tmp_path, partial_offload = True)- result = _launch(- backend,- gguf,- speculative_type = "auto",- spec_draft_n_max = 3,- n_ctx = 4096,- n_parallel = 4,- )+ result = _launch_auto_spec(backend, gguf, spec_draft_n_max = 3)cmd = result["cmd"]assert cmd[cmd.index("--spec-type") + 1] == "none"@@ -602,15 +575,7 @@ def test_manual_auto_layers_is_not_evidence_of_partial_offload(tmp_path):# partial offload disabled MTP on a card with room for every layer.backend, gguf = _hybrid_mtp_backend(tmp_path, partial_offload = True)- result = _launch(- backend,- gguf,- speculative_type = "auto",- gpu_memory_mode = "manual",- gpu_layers = -1,- n_ctx = 4096,- n_parallel = 4,- )+ result = _launch_auto_spec(backend, gguf, gpu_memory_mode = "manual", gpu_layers = -1)cmd = result["cmd"]assert cmd[cmd.index("--fit") + 1] == "on"@@ -624,15 +589,12 @@ def test_manual_auto_layers_still_reads_a_pass_through_layer_count(tmp_path):# user actually said where the layers go.backend, gguf = _hybrid_mtp_backend(tmp_path, partial_offload = True)- result = _launch(+ result = _launch_auto_spec(backend,gguf,- speculative_type = "auto",gpu_memory_mode = "manual",gpu_layers = -1,extra_args = ["--gpu-layers", "42"],- n_ctx = 4096,- n_parallel = 4,)cmd = result["cmd"]@@ -643,14 +605,7 @@ def test_manual_auto_layers_still_reads_a_pass_through_layer_count(tmp_path):def test_auto_disables_embedded_hybrid_mtp_for_final_partial_layer_override(tmp_path):backend, gguf = _hybrid_mtp_backend(tmp_path, partial_offload = False)- result = _launch(- backend,- gguf,- speculative_type = "auto",- extra_args = ["--gpu-layers", "42"],- n_ctx = 4096,- n_parallel = 4,- )+ result = _launch_auto_spec(backend, gguf, extra_args = ["--gpu-layers", "42"])cmd = result["cmd"]assert cmd[-2:] == ["--gpu-layers", "42"]@@ -670,13 +625,7 @@ def test_auto_reports_the_binary_not_the_placement_when_the_build_lacks_mtp(tmp_"spec_draft_n_max_flag": "--spec-draft-n-max",}- result = _launch(- backend,- gguf,- speculative_type = "auto",- n_ctx = 4096,- n_parallel = 4,- )+ result = _launch_auto_spec(backend, gguf)cmd = result["cmd"]assert "--spec-type" not in cmd@@ -690,15 +639,7 @@ def test_auto_classifies_placement_on_the_device_flags_the_child_gets(tmp_path):# would read CPU-only for a load that partially offloads.backend, gguf = _hybrid_mtp_backend(tmp_path, partial_offload = True)- result = _launch(- backend,- gguf,- speculative_type = "auto",- gpu_ids = [0],- extra_args = ["--device", "none"],- n_ctx = 4096,- n_parallel = 4,- )+ result = _launch_auto_spec(backend, gguf, gpu_ids = [0], extra_args = ["--device", "none"])cmd = result["cmd"]# The strip already ran: the child never sees the CPU device the classifier@@ -762,6 +703,25 @@ def _recorded_mtp_reserve(backend, gguf, **load_kwargs):return charged+def _recorded_mtp_reserve_std(+ *args,+ extra_args = ["--spec-type", "draft-mtp"],+ n_ctx = 8192,+ n_parallel = 4,+ speculative_type = "auto",+ **kwargs,+):+ """_recorded_mtp_reserve for the forced draft-mtp 8k/4-slot load."""+ return _recorded_mtp_reserve(+ *args,+ extra_args = extra_args,+ n_ctx = n_ctx,+ n_parallel = n_parallel,+ speculative_type = speculative_type,+ **kwargs,+ )++def _recorded_mtp_reserve_and_callbacks(backend, gguf, **load_kwargs):"""The reserve the fit saw, plus the callback objects it was handed."""charged = []@@ -787,13 +747,11 @@ def test_a_cpu_pinned_drafter_still_pays_the_hybrid_target_rollback(tmp_path):# spills.backend, gguf, sidecar = _hybrid_reserve_backend(tmp_path)- charged = _recorded_mtp_reserve(+ charged = _recorded_mtp_reserve_std(backend,gguf,dflash_draft_path = str(sidecar),speculative_type = "dflash",- n_ctx = 8192,- n_parallel = 4,extra_args = ["--spec-draft-ngl", "0"],)@@ -881,15 +839,7 @@ def test_a_pass_through_spec_block_budgets_the_depth_the_build_defaults_to("supports_kv_unified": True,},)- charged = _recorded_mtp_reserve(- backend,- gguf,- speculative_type = "auto",- spec_draft_n_max = requested_depth,- n_ctx = 8192,- n_parallel = 4,- extra_args = ["--spec-type", "draft-mtp"],- )+ charged = _recorded_mtp_reserve_std(backend, gguf, spec_draft_n_max = requested_depth)base = backend._mamba_recurrent_state_bytes(n_parallel = 4)assert base > 0@@ -912,14 +862,7 @@ def test_a_legacy_build_inherits_its_own_draft_depth_variable(tmp_path, monkeypa)monkeypatch.setenv("LLAMA_ARG_DRAFT_MAX", "32")- charged = _recorded_mtp_reserve(- backend,- gguf,- speculative_type = "auto",- n_ctx = 8192,- n_parallel = 4,- extra_args = ["--spec-type", "draft-mtp"],- )+ charged = _recorded_mtp_reserve_std(backend, gguf)base = backend._mamba_recurrent_state_bytes(n_parallel = 4)assert base > 0@@ -943,14 +886,7 @@ def test_a_post_rename_build_ignores_the_legacy_depth_variable(tmp_path, monkeypmonkeypatch.delenv("LLAMA_ARG_SPEC_DRAFT_N_MAX", raising = False)monkeypatch.setenv("LLAMA_ARG_DRAFT_MAX", "32")- charged = _recorded_mtp_reserve(- backend,- gguf,- speculative_type = "auto",- n_ctx = 8192,- n_parallel = 4,- extra_args = ["--spec-type", "draft-mtp"],- )+ charged = _recorded_mtp_reserve_std(backend, gguf)base = backend._mamba_recurrent_state_bytes(n_parallel = 4)assert base > 0@@ -971,14 +907,7 @@ def test_an_unreadable_help_budgets_the_deepest_shipped_draft_depth(tmp_path):},)- charged = _recorded_mtp_reserve(- backend,- gguf,- speculative_type = "auto",- n_ctx = 8192,- n_parallel = 4,- extra_args = ["--spec-type", "draft-mtp"],- )+ charged = _recorded_mtp_reserve_std(backend, gguf)base = backend._mamba_recurrent_state_bytes(n_parallel = 4)assert base > 0@@ -999,14 +928,7 @@ def test_an_explicit_pin_the_probe_cannot_see_is_not_a_partial_verdict(tmp_path)# says the placement is partial.backend, gguf = _hybrid_mtp_backend(tmp_path, partial_offload = True, memory = [])- result = _launch(- backend,- gguf,- speculative_type = "auto",- gpu_ids = [0],- n_ctx = 4096,- n_parallel = 4,- )+ result = _launch_auto_spec(backend, gguf, gpu_ids = [0])cmd = result["cmd"]assert cmd[cmd.index("--fit") + 1] == "on"@@ -1019,15 +941,7 @@ def test_an_unseen_pin_with_a_concrete_layer_count_still_stands_down(tmp_path):# evidence, so the empty probe costs the stand-down nothing here.backend, gguf = _hybrid_mtp_backend(tmp_path, partial_offload = True, memory = [])- result = _launch(- backend,- gguf,- speculative_type = "auto",- gpu_ids = [0],- n_ctx = 4096,- n_parallel = 4,- extra_args = ["--gpu-layers", "42"],- )+ result = _launch_auto_spec(backend, gguf, gpu_ids = [0], extra_args = ["--gpu-layers", "42"])cmd = result["cmd"]assert cmd[cmd.index("--spec-type") + 1] == "none"@@ -1072,13 +986,7 @@ def test_auto_drops_the_drafter_when_only_the_target_fits(tmp_path):"""backend, gguf, sidecar = _tight_vram_backend(tmp_path, drafter_gb = 12.0)- result = _launch(- backend,- gguf,- dspark_draft_path = str(sidecar),- speculative_type = "auto",- n_ctx = 8192,- )+ result = _launch_auto_8k(backend, gguf, dspark_draft_path = str(sidecar))cmd = result["cmd"]assert "--model-draft" not in cmd@@ -1095,13 +1003,7 @@ def test_auto_keeps_a_drafter_that_fits(tmp_path):"""The drop is scoped to the shortfall: with room for both, nothing changes."""backend, gguf, sidecar = _tight_vram_backend(tmp_path, drafter_gb = 1.5)- result = _launch(- backend,- gguf,- dspark_draft_path = str(sidecar),- speculative_type = "auto",- n_ctx = 8192,- )+ result = _launch_auto_8k(backend, gguf, dspark_draft_path = str(sidecar))cmd = result["cmd"]assert cmd[cmd.index("--model-draft") + 1] == str(sidecar)@@ -1114,12 +1016,11 @@ def test_forcing_the_drafter_overrides_the_vram_drop(tmp_path):lets the existing context reduction pay for it."""backend, gguf, sidecar = _tight_vram_backend(tmp_path, drafter_gb = 12.0)- result = _launch(+ result = _launch_auto_8k(backend,gguf,dspark_draft_path = str(sidecar),speculative_type = "dspark",- n_ctx = 8192,)cmd = result["cmd"]@@ -1184,12 +1085,10 @@ def test_a_standalone_model_draft_in_extras_is_not_auto_dropped(tmp_path):user_draft = tmp_path / "my-drafter.gguf"user_draft.write_bytes(b"draft")- result = _launch(+ result = _launch_auto_8k(backend,gguf,dspark_draft_path = str(sidecar),- speculative_type = "auto",- n_ctx = 8192,extra_args = ["--model-draft", str(user_draft)],)@@ -1216,13 +1115,7 @@ def test_a_busy_second_gpu_does_not_condemn_a_drafter_the_first_one_holds(tmp_pa]backend._get_gpu_free_memory = lambda _binary = None: [(0, 24_576), (1, 800)]- result = _launch(- backend,- gguf,- dspark_draft_path = str(sidecar),- speculative_type = "auto",- n_ctx = 8192,- )+ result = _launch_auto_8k(backend, gguf, dspark_draft_path = str(sidecar))cmd = result["cmd"]assert cmd[cmd.index("--model-draft") + 1] == str(sidecar)@@ -1255,12 +1148,10 @@ def test_a_cpu_offloaded_sidecar_releases_the_byte_accurate_reserve(tmp_path):backend._fit_context_to_vram = recording_fit- _launch(+ _launch_auto_8k(backend,gguf,dspark_draft_path = str(sidecar),- speculative_type = "auto",- n_ctx = 8192,extra_args = ("--spec-draft-ngl", "0"),)@@ -1306,13 +1197,11 @@ def test_tensor_parallel_keeps_its_own_sizing(tmp_path):backend._get_gpu_free_memory = lambda _binary = None: [(0, 12_288), (1, 12_288)]backend._tensor_split_aborts = lambda *args, **kwargs: False- result = _launch(+ result = _launch_auto_8k(backend,gguf,dspark_draft_path = str(sidecar),- speculative_type = "auto",tensor_parallel = True,- n_ctx = 8192,)assert backend.spec_fallback_reason != "drafter_no_vram"@@ -1327,13 +1216,11 @@ def test_a_tensor_request_that_aborted_before_is_probed_as_the_layer_load_it_is(backend, gguf, sidecar = _tight_vram_backend(tmp_path, drafter_gb = 12.0)backend._tensor_split_aborts = lambda *args, **kwargs: True- result = _launch(+ result = _launch_auto_8k(backend,gguf,dspark_draft_path = str(sidecar),- speculative_type = "auto",tensor_parallel = True,- n_ctx = 8192,)cmd = result["cmd"]@@ -1349,13 +1236,11 @@ def test_a_single_gpu_tensor_request_is_probed_as_the_layer_load_it_is(tmp_path)backend, gguf, sidecar = _tight_vram_backend(tmp_path, drafter_gb = 12.0)backend._tensor_split_aborts = lambda *args, **kwargs: False- result = _launch(+ result = _launch_auto_8k(backend,gguf,dspark_draft_path = str(sidecar),- speculative_type = "auto",tensor_parallel = True,- n_ctx = 8192,)cmd = result["cmd"]@@ -1387,9 +1272,8 @@ def test_a_dropped_tensor_request_launches_as_a_layer_split(server comes up in layer mode and the user's unrelated extras still reach it.Extras are appended last, so a --split-mode tensor left among them wouldre-engage the mode the downgrade just dropped."""- backend, gguf = _backend(+ backend, gguf = _backend_non_vulkan(tmp_path,- vulkan = False,memory = [(i, 24_000, 24_000) for i in range(n_gpus)],)backend._tensor_split_aborts = lambda *args, **kwargs: aborts@@ -1441,14 +1325,7 @@ def test_the_probe_prices_the_drafter_at_a_context_the_weakest_card_can_hold(tmpassert 1024 + 8192 * 83_886 / mib > 1_546 * 0.97assert 1024 + 5888 * 83_886 / mib <= 1_546 * 0.97- result = _launch(- backend,- gguf,- dspark_draft_path = str(sidecar),- speculative_type = "auto",- # 0 = Auto context (the branch that caps); the native 8192 above is the target.- n_ctx = 0,- )+ result = _launch_auto_8k(backend, gguf, dspark_draft_path = str(sidecar), n_ctx = 0)cmd = result["cmd"]assert cmd[cmd.index("--model-draft") + 1] == str(sidecar)@@ -1485,13 +1362,7 @@ def test_the_drop_actually_releases_the_reserve_the_fit_charges(tmp_path):"spec_draft_n_max_flag": "--spec-draft-n-max",}- result = _launch(- backend,- gguf,- dspark_draft_path = str(sidecar),- speculative_type = "auto",- n_ctx = 0,- )+ result = _launch_auto_8k(backend, gguf, dspark_draft_path = str(sidecar), n_ctx = 0)cmd = result["cmd"]# 16 GB + a 4 GB KV at 8192 clears the 23.3 GB pin budget; + 6 GB does not.@@ -1514,12 +1385,10 @@ def test_a_cpu_offloaded_sidecar_is_not_probed_because_a_head_also_exists(tmp_pabackend, gguf, sidecar = _tight_vram_backend(tmp_path, drafter_gb = 12.0)backend._read_gguf_metadata = lambda _path: setattr(backend, "_nextn_predict_layers", 1)- result = _launch(+ result = _launch_auto_8k(backend,gguf,dspark_draft_path = str(sidecar),- speculative_type = "auto",- n_ctx = 8192,extra_args = ["--spec-draft-ngl", "0"],)@@ -1546,11 +1415,10 @@ def test_a_cpu_offloaded_sidecar_reserves_no_gpu_despite_an_embedded_head(tmp_pareserved.append(k.get("mtp_engaged")) or requested)- _launch(+ _launch_auto_8k(backend,gguf,dspark_draft_path = str(sidecar),- speculative_type = "auto",n_ctx = 0,extra_args = ["--spec-draft-ngl", "0"],)@@ -1621,13 +1489,7 @@ def test_a_subset_that_can_shrink_to_hold_both_is_where_the_decision_lands(tmp_pmtp_mib_per_tok = 0.25,)- result = _launch(- backend,- gguf,- dspark_draft_path = str(sidecar),- speculative_type = "auto",- n_ctx = 0,- )+ result = _launch_auto_8k(backend, gguf, dspark_draft_path = str(sidecar), n_ctx = 0)cmd = result["cmd"]assert "--model-draft" not in cmd@@ -1662,13 +1524,7 @@ def test_widening_beats_shrinking_below_the_fit_floor(tmp_path):mtp_mib_per_tok = 0.75,)- result = _launch(- backend,- gguf,- dspark_draft_path = str(sidecar),- speculative_type = "auto",- n_ctx = 0,- )+ result = _launch_auto_8k(backend, gguf, dspark_draft_path = str(sidecar), n_ctx = 0)cmd = result["cmd"]assert "--model-draft" in cmd@@ -1699,22 +1555,31 @@ def _offload_backend(tmp_path, *, gguf_gb, free_mib, avail_mib, monkeypatch, **kreturn backend, gguf+def _offload_backend_std(+ *args,+ avail_mib = 10_000,+ free_mib = 4877,+ gguf_gb = 13.3,+ **kwargs,+):+ """_offload_backend with the standard 13.3 GB weights against a 4877 MiB free budget."""+ return _offload_backend(+ *args, avail_mib = avail_mib, free_mib = free_mib, gguf_gb = gguf_gb, **kwargs+ )++def test_weights_larger_than_vram_plus_ram_still_load_with_a_warning(tmp_path, monkeypatch):"""The field case: a 13.3 GB GGUF on a 6 GB laptop card holding 4877 MiB free needsabout 8.5 GB of host RAM, which a 10 GB host cannot hold. It loads anyway, pagingthe remainder from disk, and says so."""- backend, gguf = _offload_backend(- tmp_path, gguf_gb = 13.3, free_mib = 4877, avail_mib = 10_000, monkeypatch = monkeypatch- )+ backend, gguf = _offload_backend_std(tmp_path, monkeypatch = monkeypatch)_launch_warns(backend, gguf)def test_the_same_load_on_a_large_ram_host_still_launches(tmp_path, monkeypatch):"""Deliberate CPU offload stays supported; only a shortfall refuses."""- backend, gguf = _offload_backend(- tmp_path, gguf_gb = 13.3, free_mib = 4877, avail_mib = 64_000, monkeypatch = monkeypatch- )+ backend, gguf = _offload_backend_std(tmp_path, avail_mib = 64_000, monkeypatch = monkeypatch)assert "--fit" in _launch(backend, gguf)["cmd"]@@ -1723,9 +1588,7 @@ def test_free_vram_offsets_the_charge(tmp_path, monkeypatch):"""Same model and same host RAM as the refusal above, but a card big enough to holdit. The VRAM credit is what separates the two, so the charge is the shortfall andnot the model size."""- backend, gguf = _offload_backend(- tmp_path, gguf_gb = 13.3, free_mib = 20_000, avail_mib = 10_000, monkeypatch = monkeypatch- )+ backend, gguf = _offload_backend_std(tmp_path, free_mib = 20_000, monkeypatch = monkeypatch)assert "--fit" in _launch(backend, gguf)["cmd"]@@ -1740,11 +1603,7 @@ def test_free_vram_offsets_the_charge(tmp_path, monkeypatch):)def test_vulkan_igpu_shared_memory_is_not_counted_twice(tmp_path, monkeypatch, memory):"""Shared Vulkan rows and host RAM describe one pool."""- backend, gguf = _backend(- tmp_path,- vulkan = True,- memory = memory,- )+ backend, gguf = _backend_non_vulkan(tmp_path, vulkan = True, memory = memory)_restore_host_guard(backend)backend._get_gguf_size_bytes = lambda _path: 20 * 1024**3backend._select_gpus = lambda *args, **kwargs: (None, True)@@ -1758,11 +1617,7 @@ def test_vulkan_igpu_shared_memory_is_not_counted_twice(tmp_path, monkeypatch, mdef test_vulkan_igpu_heap_can_hold_weights_missing_from_host_available(tmp_path, monkeypatch):"""A firmware carve-out remains usable when host-available RAM is low."""- backend, gguf = _backend(- tmp_path,- vulkan = True,- memory = [(0, 107 * 1024, 0)],- )+ backend, gguf = _backend_non_vulkan(tmp_path, vulkan = True, memory = [(0, 107 * 1024, 0)])_restore_host_guard(backend)backend._get_gguf_size_bytes = lambda _path: int(16.5 * 1024**3)monkeypatch.setattr(@@ -2007,11 +1862,7 @@ def test_split_mode_none_still_credits_a_lone_shared_device(tmp_path, monkeypatcdef test_vulkan_igpu_heap_does_not_bypass_a_cgroup_limit(tmp_path, monkeypatch):"""A shared Vulkan heap remains subject to the process cgroup limit."""- backend, gguf = _backend(- tmp_path,- vulkan = True,- memory = [(0, 64 * 1024, 0)],- )+ backend, gguf = _backend_non_vulkan(tmp_path, vulkan = True, memory = [(0, 64 * 1024, 0)])_restore_host_guard(backend)backend._apu_ram_shortfall_message = LlamaCppBackend._apu_ram_shortfall_messagebackend._get_gguf_size_bytes = lambda _path: 20 * 1024**3@@ -2044,18 +1895,14 @@ def test_a_card_resident_model_is_not_refused_by_a_container_ceiling(tmp_path, mdef test_unknown_available_ram_abstains(tmp_path, monkeypatch):- backend, gguf = _offload_backend(- tmp_path, gguf_gb = 13.3, free_mib = 4877, avail_mib = None, monkeypatch = monkeypatch- )+ backend, gguf = _offload_backend_std(tmp_path, avail_mib = None, monkeypatch = monkeypatch)assert _launch(backend, gguf)["cmd"]def test_an_unsized_model_abstains(tmp_path, monkeypatch):"""A GGUF whose size cannot be read leaves nothing to price."""- backend, gguf = _offload_backend(- tmp_path, gguf_gb = 13.3, free_mib = 4877, avail_mib = 10_000, monkeypatch = monkeypatch- )+ backend, gguf = _offload_backend_std(tmp_path, monkeypatch = monkeypatch)backend._get_gguf_size_bytes = lambda _path: (_ for _ in ()).throw(OSError("stat failed"))assert _launch(backend, gguf)["cmd"]@@ -2079,9 +1926,7 @@ def test_placement_flags_never_turn_an_allowed_load_into_a_refusal(Each of these moves bytes onto the host or narrows the reachable VRAM, so a guardthat read them could only refuse MORE. Leaving them out cannot invent a refusal,which is the property that keeps this check free of llama.cpp placement modelling."""- backend, gguf = _offload_backend(- tmp_path, gguf_gb = 13.3, free_mib = 4877, avail_mib = 64_000, monkeypatch = monkeypatch- )+ backend, gguf = _offload_backend_std(tmp_path, avail_mib = 64_000, monkeypatch = monkeypatch)assert _launch(backend, gguf, extra_args = extra_args)["cmd"]@@ -2090,9 +1935,7 @@ def test_the_guard_reads_the_model_the_child_opens(tmp_path, monkeypatch):"""Sizing comes from the argv path, not from the planner's earlier pick, so afallback that rewrote -m is priced as launched."""seen = []- backend, gguf = _offload_backend(- tmp_path, gguf_gb = 13.3, free_mib = 4877, avail_mib = 10_000, monkeypatch = monkeypatch- )+ backend, gguf = _offload_backend_std(tmp_path, monkeypatch = monkeypatch)real_size = backend._get_gguf_size_bytesdef _record(path):@@ -2110,18 +1953,14 @@ def test_the_env_escape_is_now_a_no_op_that_only_silences_the_warning(tmp_path,Both arms must load. Unset, the load carries the advisory; set, it loads just thesame and says nothing. Kept honoured so existing scripts and docs keep working."""- warned, gguf = _offload_backend(- tmp_path, gguf_gb = 13.3, free_mib = 4877, avail_mib = 10_000, monkeypatch = monkeypatch- )+ warned, gguf = _offload_backend_std(tmp_path, monkeypatch = monkeypatch)monkeypatch.delenv("UNSLOTH_ALLOW_HOST_OFFLOAD", raising = False)assert "--fit" in _launch(warned, gguf)["cmd"]assert "does not fit in GPU memory" in (warned.last_load_warning or "")allowed_dir = tmp_path / "allowed"allowed_dir.mkdir()- allowed, gguf2 = _offload_backend(- allowed_dir, gguf_gb = 13.3, free_mib = 4877, avail_mib = 10_000, monkeypatch = monkeypatch- )+ allowed, gguf2 = _offload_backend_std(allowed_dir, monkeypatch = monkeypatch)monkeypatch.setenv("UNSLOTH_ALLOW_HOST_OFFLOAD", "1")assert "--fit" in _launch(allowed, gguf2)["cmd"]assert allowed.last_load_warning is None@@ -2167,9 +2006,7 @@ def test_an_oversized_unmapped_load_is_remapped_instead_of_refused():"""It launches, the argv the child gets is pageable, and the warning names theoverride -- a silent one would leave the user's own setting quietly undone."""- backend, gguf = _offload_backend(- tmp_path, gguf_gb = 13.3, free_mib = 4877, avail_mib = 10_000, monkeypatch = monkeypatch- )+ backend, gguf = _offload_backend_std(tmp_path, monkeypatch = monkeypatch)monkeypatch.delenv("UNSLOTH_ALLOW_HOST_OFFLOAD", raising = False)cmd = _launch(backend, gguf, extra_args = extra_args)["cmd"]@@ -2183,9 +2020,7 @@ def test_an_oversized_unmapped_load_is_remapped_instead_of_refused(def test_an_unmapped_load_that_fits_is_left_exactly_as_asked(tmp_path, monkeypatch, extra_args):"""The control. Same request on a host with room: no shortfall, so nothing isoverridden and no warning is invented. Loading unmapped is a legitimate choice."""- backend, gguf = _offload_backend(- tmp_path, gguf_gb = 13.3, free_mib = 4877, avail_mib = 64_000, monkeypatch = monkeypatch- )+ backend, gguf = _offload_backend_std(tmp_path, avail_mib = 64_000, monkeypatch = monkeypatch)cmd = _launch(backend, gguf, extra_args = extra_args)["cmd"]@@ -2199,9 +2034,7 @@ def test_the_override_keeps_a_lock_rather_than_dropping_it(tmp_path, monkeypatch"""`mlock` is unmapped too, but it also says "keep this in RAM". The pageableequivalent is `mmap+mlock`, which upstream mmaps, so the request survives theoverride instead of being silently discarded."""- backend, gguf = _offload_backend(- tmp_path, gguf_gb = 13.3, free_mib = 4877, avail_mib = 10_000, monkeypatch = monkeypatch- )+ backend, gguf = _offload_backend_std(tmp_path, monkeypatch = monkeypatch)monkeypatch.delenv("UNSLOTH_ALLOW_HOST_OFFLOAD", raising = False)cmd = _launch(backend, gguf, extra_args = ["--load-mode", "mlock"])["cmd"]@@ -2217,9 +2050,7 @@ def test_the_override_reaches_the_env_twin_llama_cpp_reads_first(tmp_path, monkestripping the tokens. Unsloth emits no load-mode flag of its own here, so withoutthe env half the child would still load unmapped with nothing in the argv to showit."""- backend, gguf = _offload_backend(- tmp_path, gguf_gb = 13.3, free_mib = 4877, avail_mib = 10_000, monkeypatch = monkeypatch- )+ backend, gguf = _offload_backend_std(tmp_path, monkeypatch = monkeypatch)monkeypatch.delenv("UNSLOTH_ALLOW_HOST_OFFLOAD", raising = False)monkeypatch.setenv("LLAMA_ARG_NO_MMAP", "1")@@ -2255,9 +2086,7 @@ def test_the_warning_opt_out_never_disables_the_pageable_override(escape turned a load that works into one that is killed, which is the opposite ofwhat an opt-out from a REFUSAL was ever meant to do. The message goes; the rewritestays, and the log still names it so the load is traceable."""- backend, gguf = _offload_backend(- tmp_path, gguf_gb = 13.3, free_mib = 4877, avail_mib = 10_000, monkeypatch = monkeypatch- )+ backend, gguf = _offload_backend_std(tmp_path, monkeypatch = monkeypatch)monkeypatch.setenv("UNSLOTH_ALLOW_HOST_OFFLOAD", "1")logged = _override_log(monkeypatch)@@ -2274,9 +2103,7 @@ def test_the_warning_opt_out_never_disables_the_pageable_override(def test_the_opt_out_on_a_fitting_unmapped_load_changes_nothing(tmp_path, monkeypatch, extra_args):"""The control for the case above. Silenced or not, a load with room to run isleft exactly as asked and nothing is logged about an override."""- backend, gguf = _offload_backend(- tmp_path, gguf_gb = 13.3, free_mib = 4877, avail_mib = 64_000, monkeypatch = monkeypatch- )+ backend, gguf = _offload_backend_std(tmp_path, avail_mib = 64_000, monkeypatch = monkeypatch)monkeypatch.setenv("UNSLOTH_ALLOW_HOST_OFFLOAD", "1")logged = _override_log(monkeypatch)@@ -2619,9 +2446,7 @@ def test_a_genuinely_cpu_only_layout_is_still_recognised(tmp_path):def test_an_rpc_launch_abstains(tmp_path, monkeypatch):"""--rpc places layers on remote devices this cannot size, so refusing on localcapacity alone would block a viable distributed launch."""- backend, gguf = _offload_backend(- tmp_path, gguf_gb = 13.3, free_mib = 4877, avail_mib = 10_000, monkeypatch = monkeypatch- )+ backend, gguf = _offload_backend_std(tmp_path, monkeypatch = monkeypatch)argv = ["llama-server", "-m", str(gguf)]assert backend._launch_host_shortfall_message(argv, [(0, 4877)]) is not None@@ -2635,9 +2460,7 @@ def test_an_rpc_launch_abstains(tmp_path, monkeypatch):def test_an_rpc_env_launch_abstains(tmp_path, monkeypatch):"""llama.cpp reads LLAMA_ARG_RPC as the environment twin of --rpc, so the guard hasto see the child environment or it refuses the same distributed launch."""- backend, gguf = _offload_backend(- tmp_path, gguf_gb = 13.3, free_mib = 4877, avail_mib = 10_000, monkeypatch = monkeypatch- )+ backend, gguf = _offload_backend_std(tmp_path, monkeypatch = monkeypatch)argv = ["llama-server", "-m", str(gguf)]assert backend._launch_host_shortfall_message(argv, [(0, 4877)], {}) is not None@@ -2767,9 +2590,8 @@ def test_a_fit_derived_load_mode_is_recorded_too(tmp_path, monkeypatch):def _tensor_backend(tmp_path):- backend, gguf = _backend(+ backend, gguf = _backend_non_vulkan(tmp_path,- vulkan = False,memory = [(0, 24_000, 24_000), (1, 24_000, 24_000)],)backend._tensor_split_aborts = lambda *args, **kwargs: False@@ -2887,10 +2709,8 @@ def _apu_and_discrete_shortfall_backend(tmp_path, monkeypatch, *, avail_mib):The overlap is the point: two guards, two messages, and _record_load_warning keepsthe first."""- backend, gguf = _offload_backend(+ backend, gguf = _offload_backend_std(tmp_path,- gguf_gb = 13.3,- free_mib = 4877,avail_mib = avail_mib,monkeypatch = monkeypatch,_amd_apu_wants_unified_memory = lambda *_a, **_kw: True,@@ -2943,9 +2763,7 @@ def test_the_note_is_appended_once_when_only_the_launch_guard_warned(tmp_path, m"""The control against a double append. With no APU notice recorded there isnothing to amend, so the note arrives exactly once, through the launch guard's ownmessage."""- backend, gguf = _offload_backend(- tmp_path, gguf_gb = 13.3, free_mib = 4877, avail_mib = 10_000, monkeypatch = monkeypatch- )+ backend, gguf = _offload_backend_std(tmp_path, monkeypatch = monkeypatch)monkeypatch.delenv("UNSLOTH_ALLOW_HOST_OFFLOAD", raising = False)_launch(backend, gguf, extra_args = ["--no-mmap"])diff --git a/studio/backend/tests/test_llama_cpp_reprompt_guard.py b/studio/backend/tests/test_llama_cpp_reprompt_guard.pyindex e2c30c78941..a27ad227d4b 100644--- a/studio/backend/tests/test_llama_cpp_reprompt_guard.py+++ b/studio/backend/tests/test_llama_cpp_reprompt_guard.py@@ -51,6 +51,7 @@ from core.inference.llama_cpp import ( # noqa: E402_has_answer_artifact,)from core.inference.tool_call_parser import INTENT_SIGNAL as _INTENT_SIGNAL # noqa: E402+import time# ── _INTENT_SIGNAL still matches plan-only stalls ──────────────────@@ -653,8 +654,6 @@ def test_no_backtrack_on_crlf_spam():O(n^2)-backtracked through embedded `\\r\\n` characters (~630 ms on10 KB). The current `[ \\t]*` indent restriction plus length-bounded`[\\s\\S]{...}?` runs keep every alternative linear."""- import time-payload = "\r\n" * 5000t0 = time.time()_has_answer_artifact(payload)@@ -666,8 +665,6 @@ def test_no_backtrack_on_open_html_spam():"""Many `<html ` openings without `</html>` close must still completequickly. Bounded `[\\s\\S]{0,4000}?` between the open and close capsthe scan per occurrence."""- import time-payload = "<html " * 200 # ~1200 chars, under _REPROMPT_MAX_CHARSt0 = time.time()_has_answer_artifact(payload)@@ -682,8 +679,6 @@ def test_no_backtrack_on_doctype_html_alternation_worst_case():gate the worst observed measurement was about 7 ms; assert agenerous budget so future quantifier changes that drop the inner``{0,4000}`` bound fail loudly."""- import time-payload = ("<!doctype html><html foo " * 60)[:1999]t0 = time.time()_has_answer_artifact(payload)@@ -693,8 +688,6 @@ def test_no_backtrack_on_doctype_html_alternation_worst_case():def test_no_backtrack_on_tilde_fence_spam():"""Open ``~~~`` fences without close must terminate quickly."""- import time-payload = "~~~a\n" * 400 # ~2000 chars, near _REPROMPT_MAX_CHARSt0 = time.time()_has_answer_artifact(payload)diff --git a/studio/backend/tests/test_llama_cpp_start_failure_classification.py b/studio/backend/tests/test_llama_cpp_start_failure_classification.pyindex c9045b61f13..030cc568216 100644--- a/studio/backend/tests/test_llama_cpp_start_failure_classification.py+++ b/studio/backend/tests/test_llama_cpp_start_failure_classification.py@@ -37,6 +37,7 @@ if not hasattr(sys.modules["structlog"], "get_logger"):sys.modules["structlog"].get_logger = _structlog_stub.get_loggerfrom core.inference.llama_cpp import LlamaCppBackend # noqa: E402+import time_classify = LlamaCppBackend._classify_llama_start_failure@@ -1020,7 +1021,6 @@ class TestDiagnosticsDoNotLeak:# measures the runner instead, which is why this exact assertion goes red on# the Windows runner for main as well as for a branch. The stopwatch stays# only as a catastrophic guard, loose enough that no runner can trip it.- import timeburied = "error: invalid argument: --nope\n" + "x" * 10_000_000 + "\nggml_metal_init: error"start = time.perf_counter()@@ -1211,7 +1211,6 @@ class TestTheDyldReasonIsBounded:def test_a_pathological_reason_does_not_stall_the_classifier(self):# 100KB of "'a' (" drove the candidate scan quadratic: 6.3s measured# before the cap, against 0.0s on main, on the thread serving the load.- import timeout = ("dyld[1]: Library not loaded: @rpath/libllama.dylib\n"@@ -1487,8 +1486,6 @@ class TestAnEncodedSecretIsStillRedacted:)def test_the_name_pass_stays_linear(self, blob):"""No nested quantifier: a crafted line must not be able to stall it."""- import time-start = time.monotonic()_classify(blob, "/m.gguf", "u/x", 1)assert time.monotonic() - start < 2.0@@ -1711,8 +1708,6 @@ class TestTheRedactionHolesCodexFound:ids = ["unterminated-quote", "dotted-names", "many-pairs"],)def test_the_widened_pattern_stays_linear(self, blob):- import time-start = time.monotonic()LlamaCppBackend._scrub_secret_values(blob, ())assert time.monotonic() - start < 2.0diff --git a/studio/backend/tests/test_llama_cpp_tool_loop.py b/studio/backend/tests/test_llama_cpp_tool_loop.pyindex 33b0efc908c..83a12f68cdd 100644--- a/studio/backend/tests/test_llama_cpp_tool_loop.py+++ b/studio/backend/tests/test_llama_cpp_tool_loop.py@@ -32,6 +32,87 @@ from core.inference.llama_cpp import (from core.inference.tool_call_parser import NUDGE_TOOL_CALLS_STATUSfrom state import tool_approvalsfrom state.tool_approvals import TOOL_REJECTED_MESSAGE, resolve_tool_decision+from core.inference import llama_cpp+from core.inference.llama_cpp import _should_suppress_forced_no_tool_output as suppress+import httpx+++def _web_search_tool():+ """The web_search tool schema the tool-loop tests advertise."""+ return {+ "type": "function",+ "function": {+ "name": "web_search",+ "description": "Search the web.",+ "parameters": {+ "type": "object",+ "properties": {"query": {"type": "string"}},+ "required": ["query"],+ },+ },+ }+++def _render_html_tools():+ """The one-entry render_html tool list the tool-loop tests advertise."""+ return [+ {+ "type": "function",+ "function": {+ "name": "render_html",+ "description": "Render HTML.",+ "parameters": {+ "type": "object",+ "properties": {"code": {"type": "string"}},+ "required": ["code"],+ },+ },+ }+ ]+++def fake_execute_tool(name, arguments, **_kwargs):+ raise AssertionError(f"unexpected tool execution: {name} {arguments}")+++def _tool_call_sse(+ tool_name: str,+ arguments: dict,+ call_id: str,+ index: int = 0,+) -> str:+ """One tool-call delta frame: the twelve-line literal the streams below repeat."""+ return _sse(+ {+ "tool_calls": [+ {+ "index": index,+ "id": call_id,+ "type": "function",+ "function": {"name": tool_name, "arguments": json.dumps(arguments)},+ }+ ]+ }+ )+++def _run_tool_loop(+ backend,+ messages,+ tools,+ *,+ max_tool_iterations = 1,+ **kwargs,+):+ """Drain the tool loop into a list, the seven-line call nearly every test below repeats."""+ return list(+ backend.generate_chat_completion_with_tools(+ messages = messages,+ tools = tools,+ max_tool_iterations = max_tool_iterations,+ **kwargs,+ )+ )def _sse(delta: dict) -> str:@@ -125,6 +206,27 @@ def _make_backend(return backend+def _backend_and_payloads(monkeypatch, streams):+ """_make_backend plus the payload list every caller threads through it."""+ payloads: list[dict] = []+ return _make_backend(monkeypatch, streams, payloads), payloads+++def _record_tool_calls(monkeypatch, result):+ """Pin execute_tool to record each (name, arguments) pair and answer `result`.++ A callable result is handed the tool name, for the cases that echo it back.+ """+ calls: list[tuple[str, dict]] = []++ def fake_execute_tool(name, arguments, **_kwargs):+ calls.append((name, arguments))+ return result(name) if callable(result) else result++ monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ return calls++def test_plain_stream_reports_request_scoped_live_prompt_and_generation_timings(monkeypatch):stream = ["data: "@@ -245,13 +347,11 @@ def test_tool_stream_reports_progress_without_leaking_a_content_event(monkeypatcsamples: list[dict] = []backend = _make_backend(monkeypatch, [stream], payloads)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "benchmark"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- perf_callback = samples.append,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "benchmark"}],+ [{"type": "function", "function": {"name": "web_search"}}],+ perf_callback = samples.append,)assert payloads[0]["return_progress"] is True@@ -304,21 +404,7 @@ def _patch_monotonic(monkeypatch, values: list[float]) -> None:def _structured_tool_call(tool_name: str, arguments: dict, call_id: str) -> list[str]:return [- _sse(- {- "tool_calls": [- {- "index": 0,- "id": call_id,- "type": "function",- "function": {- "name": tool_name,- "arguments": json.dumps(arguments),- },- }- ]- }- ),+ _tool_call_sse(tool_name, arguments, call_id),_done(),]@@ -332,8 +418,7 @@ def test_forced_web_search_tool_choice_is_sent_until_a_tool_runs(monkeypatch):"web_search", {"query": "current Linux kernel version"}, "call_search")second_stream = [_sse({"content": "The current version of the Linux kernel is 6.10."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, second_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, second_stream])monkeypatch.setattr("core.inference.tools.execute_tool",lambda name, arguments, **_kwargs: "Linux kernel 6.10",@@ -382,8 +467,7 @@ def test_forced_web_search_tool_choice_is_sent_until_a_tool_runs(monkeypatch):def test_forced_tool_choice_must_exist_in_the_catalog(monkeypatch):- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [])with pytest.raises(ValueError, match = "Forced tool 'python' is not enabled"):list(@@ -405,15 +489,8 @@ def test_forced_tool_choice_retries_after_other_structured_calls(monkeypatch):_structured_tool_call("web_search", {"query": "kernel version"}, "call_search"),[_sse({"content": "The search completed."}), _done()],]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "Linux kernel result"-- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ backend, payloads = _backend_and_payloads(monkeypatch, streams)+ calls = _record_tool_calls(monkeypatch, "Linux kernel result")events = list(backend.generate_chat_completion_with_tools(@@ -444,8 +521,7 @@ def test_none_tool_choice_never_executes_model_tool_calls(monkeypatch):_sse({"content": "I will answer without tools."}),_done(),]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [stream])def fail_execute_tool(name, arguments, **_kwargs):raise AssertionError(f"unexpected tool execution: {name} {arguments}")@@ -490,25 +566,10 @@ def test_structured_tool_call_after_visible_preface_is_executed(monkeypatch):tool_call_id = "call_render_late"first_stream = [_sse({"content": "Here is the canvas.\n\n"}),- _sse(- {- "tool_calls": [- {- "index": 0,- "id": tool_call_id,- "type": "function",- "function": {- "name": "render_html",- "arguments": json.dumps(- {- "code": "<html><body><div>red</div></body></html>",- "title": "Simple Red Square",- }- ),- },- }- ]- }+ _tool_call_sse(+ "render_html",+ {"code": "<html><body><div>red</div></body></html>", "title": "Simple Red Square"},+ tool_call_id,),_done(),]@@ -516,39 +577,13 @@ def test_structured_tool_call_after_visible_preface_is_executed(monkeypatch):_sse({"content": "Done."}),_done(),]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, second_stream], payloads)-- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "Rendered HTML canvas: Simple Red Square."+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, second_stream])- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ calls = _record_tool_calls(monkeypatch, "Rendered HTML canvas: Simple Red Square.")- tools = [- {- "type": "function",- "function": {- "name": "render_html",- "description": "Render HTML.",- "parameters": {- "type": "object",- "properties": {"code": {"type": "string"}},- "required": ["code"],- },- },- }- ]+ tools = _render_html_tools()- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "Make a red square."}],- tools = tools,- max_tool_iterations = 1,- )- )+ events = _run_tool_loop(backend, [{"role": "user", "content": "Make a red square."}], tools)content_events = [e for e in events if e.get("type") == "content"]assert content_events[0]["text"] == "Here is the canvas.\n\n"@@ -590,16 +625,13 @@ def test_streamed_reasoning_answer_emits_backend_summary(monkeypatch):_sse({"content": "Final answer."}),_done(),]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [stream])_patch_monotonic(monkeypatch, [100.0, 110.0, 172.0, 172.0])- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "answer"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "answer"}],+ [{"type": "function", "function": {"name": "web_search"}}],)content_texts = [e["text"] for e in events if e["type"] == "content"]@@ -629,16 +661,13 @@ def test_reasoning_streams_incrementally_with_tools(monkeypatch):_sse({"content": "Done."}),_done(),]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [stream])_patch_monotonic(monkeypatch, [1.0, 2.0, 3.0, 4.0, 4.0])- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "think then answer"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "think then answer"}],+ [{"type": "function", "function": {"name": "web_search"}}],)reasoning_stage = [@@ -667,16 +696,13 @@ def test_reasoning_only_reply_matches_no_tool_path_with_tools(monkeypatch):_sse({"reasoning_content": "The capital of France is Paris."}),_done(),]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [stream])_patch_monotonic(monkeypatch, [1.0, 5.0, 5.0])- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "just think"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "just think"}],+ [{"type": "function", "function": {"name": "web_search"}}],)content_texts = [e["text"] for e in events if e["type"] == "content"]@@ -695,13 +721,11 @@ def _assert_reasoning_only_raw_consumer_gets_one_balanced_think_block(monkeypatcbackend = _make_backend(monkeypatch, [stream], [])if with_tools:- items = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "capital of France?"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- promote_reasoning_only = False,- )+ items = _run_tool_loop(+ backend,+ [{"role": "user", "content": "capital of France?"}],+ [{"type": "function", "function": {"name": "web_search"}}],+ promote_reasoning_only = False,)cumulatives = [item["text"] for item in items if item.get("type") == "content"]else:@@ -740,20 +764,17 @@ def test_reasoning_before_structured_tool_closes_think_block(monkeypatch):_sse({"content": "It is sunny."}),_done(),]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [tool_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [tool_stream, final_stream])_patch_monotonic(monkeypatch, [1.0, 2.0, 3.0, 4.0, 4.0])monkeypatch.setattr("core.inference.tools.execute_tool", lambda name, arguments, **_kwargs: "sunny")- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "weather?"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "weather?"}],+ [{"type": "function", "function": {"name": "web_search"}}],)tool_start_index = next(i for i, e in enumerate(events) if e["type"] == "tool_start")@@ -876,20 +897,17 @@ def test_reasoning_before_bare_json_tool_closes_think_block(monkeypatch):_sse({"content": "It is sunny."}),_done(),]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [tool_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [tool_stream, final_stream])_patch_monotonic(monkeypatch, [1.0, 2.0, 3.0, 4.0, 4.0])monkeypatch.setattr("core.inference.tools.execute_tool", lambda name, arguments, **_kwargs: "sunny")- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "weather?"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "weather?"}],+ [{"type": "function", "function": {"name": "web_search"}}],)tool_start_index = next(i for i, e in enumerate(events) if e["type"] == "tool_start")@@ -912,8 +930,7 @@ def test_structured_tool_call_turn_replays_pre_tool_reasoning_in_next_payload(mo_sse({"content": "It is sunny."}),_done(),]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [tool_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [tool_stream, final_stream])monkeypatch.setattr("core.inference.tools.execute_tool", lambda name, arguments, **_kwargs: "sunny"@@ -957,15 +974,8 @@ def test_mixed_execute_and_noop_batch_keeps_structured_reasoning(monkeypatch):_done(),]final_stream = [_sse({"content": "It is sunny."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [tool_stream, final_stream], payloads)- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "sunny"-- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ backend, payloads = _backend_and_payloads(monkeypatch, [tool_stream, final_stream])+ calls = _record_tool_calls(monkeypatch, "sunny")list(backend.generate_chat_completion_with_tools(@@ -1014,8 +1024,7 @@ def test_textual_tool_call_turn_replays_reasoning_only_trace_in_next_payload(mon_sse({"content": "It is sunny."}),_done(),]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [tool_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [tool_stream, final_stream])monkeypatch.setattr("core.inference.tools.execute_tool", lambda name, arguments, **_kwargs: "sunny"@@ -1044,8 +1053,7 @@ def test_tool_call_turn_without_reasoning_adds_no_reasoning_content(monkeypatch)_sse({"content": "It is sunny."}),_done(),]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [tool_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [tool_stream, final_stream])monkeypatch.setattr("core.inference.tools.execute_tool", lambda name, arguments, **_kwargs: "sunny"@@ -1072,8 +1080,7 @@ def test_tool_call_turn_with_blank_reasoning_adds_no_reasoning_content(monkeypat"web_search", {"query": "weather"}, "call_blank")final_stream = [_sse({"content": "It is sunny."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [tool_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [tool_stream, final_stream])monkeypatch.setattr("core.inference.tools.execute_tool", lambda name, arguments, **_kwargs: "sunny"@@ -1099,29 +1106,11 @@ def test_blank_reasoning_noop_turn_adds_no_empty_assistant_message(monkeypatch):"""A blank trace on a suppressed call must not open an empty model turn."""tool_stream = [_sse({"reasoning_content": "\n\n"}),- _sse(- {- "tool_calls": [- {- "index": 0,- "id": "call_blank_noop",- "type": "function",- "function": {- "name": "python",- "arguments": json.dumps({"code": "print(1)"}),- },- }- ]- }- ),+ _tool_call_sse("python", {"code": "print(1)"}, "call_blank_noop"),_done(),]final_stream = [_sse({"content": "I cannot run Python here."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [tool_stream, final_stream], payloads)-- def fake_execute_tool(name, arguments, **_kwargs):- raise AssertionError(f"unexpected tool execution: {name} {arguments}")+ backend, payloads = _backend_and_payloads(monkeypatch, [tool_stream, final_stream])monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)@@ -1149,9 +1138,6 @@ def test_blank_reasoning_noop_turn_adds_no_empty_assistant_message(monkeypatch):def test_noop_reasoning_continuation_separates_partial_from_inlined_trace(monkeypatch):"""A suppressed call must not weld inlined reasoning onto a resumed partial."""- def fake_execute_tool(name, arguments, **_kwargs):- raise AssertionError(f"unexpected tool execution: {name} {arguments}")-monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)for partial in ("PARTIAL_TEXT", "PARTIAL_TEXT\n"):@@ -1159,8 +1145,7 @@ def test_noop_reasoning_continuation_separates_partial_from_inlined_trace(monkey"python", {"code": "print(1)"}, "call_continued_noop")final_stream = [_sse({"content": "I cannot run Python here."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [tool_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [tool_stream, final_stream])list(backend.generate_chat_completion_with_tools(@@ -1186,11 +1171,7 @@ def test_noop_reasoning_without_continuation_adds_clean_assistant_turn(monkeypat"python", {"code": "print(1)"}, "call_separate_noop")final_stream = [_sse({"content": "I cannot run Python here."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [tool_stream, final_stream], payloads)-- def fake_execute_tool(name, arguments, **_kwargs):- raise AssertionError(f"unexpected tool execution: {name} {arguments}")+ backend, payloads = _backend_and_payloads(monkeypatch, [tool_stream, final_stream])monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)@@ -1247,8 +1228,7 @@ def test_noop_feedback_is_not_folded_into_another_tool_s_result(monkeypatch):_done(),]final_stream = [_sse({"content": "It is sunny."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [tool_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [tool_stream, final_stream])monkeypatch.setattr("core.inference.tools.execute_tool",@@ -1302,8 +1282,7 @@ def test_noop_feedback_for_multiple_tools_is_not_folded_by_partial_name_match(mo_done(),]final_stream = [_sse({"content": "It is sunny."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [tool_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [tool_stream, final_stream])monkeypatch.setattr("core.inference.tools.execute_tool",@@ -1354,8 +1333,7 @@ def test_same_tool_noop_feedback_still_rides_its_own_result(monkeypatch):_done(),]final_stream = [_sse({"content": "It is sunny."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [tool_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [tool_stream, final_stream])monkeypatch.setattr("core.inference.tools.execute_tool", lambda name, arguments, **_kwargs: "sunny"@@ -1420,8 +1398,7 @@ def test_tool_loop_does_not_mutate_the_caller_s_messages(monkeypatch):_done(),]final_stream = [_sse({"content": "It is sunny."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [tool_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [tool_stream, final_stream])monkeypatch.setattr("core.inference.tools.execute_tool", lambda name, arguments, **_kwargs: "sunny"@@ -1453,8 +1430,7 @@ def test_consumed_tool_final_pass_emits_latest_reasoning_summary(monkeypatch):_sse({"content": "Final from tool."}),_done(),]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [tool_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [tool_stream, final_stream])_patch_monotonic(monkeypatch, [200.0, 201.0, 203.0, 300.0, 400.0, 405.0, 410.0])def fake_execute_tool(name, arguments, **_kwargs):@@ -1462,12 +1438,10 @@ def test_consumed_tool_final_pass_emits_latest_reasoning_summary(monkeypatch):monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "render then answer"}],- tools = [{"type": "function", "function": {"name": "render_html"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "render then answer"}],+ [{"type": "function", "function": {"name": "render_html"}}],)summaries = [event for event in events if event["type"] == "reasoning_summary"]@@ -1485,62 +1459,27 @@ def test_repeat_render_html_nudge_is_not_user_visible_error(monkeypatch):"""A repeated render_html call is an internal no-op, not a visible card."""first_stream = [- _sse(- {- "tool_calls": [- {- "index": 0,- "id": "call_first",- "type": "function",- "function": {- "name": "render_html",- "arguments": json.dumps(- {- "code": "<html><body>first</body></html>",- "title": "First",- }- ),- },- }- ]- }+ _tool_call_sse(+ "render_html",+ {"code": "<html><body>first</body></html>", "title": "First"},+ "call_first",),_done(),]repeat_stream = [- _sse(- {- "tool_calls": [- {- "index": 0,- "id": "call_repeat",- "type": "function",- "function": {- "name": "render_html",- "arguments": json.dumps(- {- "code": "<html><body>repeat</body></html>",- "title": "Repeat",- }- ),- },- }- ]- }+ _tool_call_sse(+ "render_html",+ {"code": "<html><body>repeat</body></html>", "title": "Repeat"},+ "call_repeat",),_done(),]final_stream = [_sse({"content": "Short note."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, repeat_stream, final_stream], payloads)-- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "Rendered HTML canvas: First."+ backend, payloads = _backend_and_payloads(+ monkeypatch, [first_stream, repeat_stream, final_stream]+ )- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ calls = _record_tool_calls(monkeypatch, "Rendered HTML canvas: First.")tools = [{@@ -1558,12 +1497,8 @@ def test_repeat_render_html_nudge_is_not_user_visible_error(monkeypatch):{"type": "function", "function": {"name": "web_search"}},]- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "Make a red square."}],- tools = tools,- max_tool_iterations = 2,- )+ events = _run_tool_loop(+ backend, [{"role": "user", "content": "Make a red square."}], tools, max_tool_iterations = 2)assert calls == [@@ -1605,38 +1540,22 @@ def test_repeat_render_html_nudge_is_not_user_visible_error(monkeypatch):def test_render_html_success_drops_tool_schema_before_final_pass(monkeypatch):first_stream = [- _sse(- {- "tool_calls": [- {- "index": 0,- "id": "call_first",- "type": "function",- "function": {- "name": "render_html",- "arguments": json.dumps({"code": "<html>ok</html>"}),- },- }- ]- }- ),+ _tool_call_sse("render_html", {"code": "<html>ok</html>"}, "call_first"),_done(),]final_stream = [_sse({"content": "Done."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, final_stream])def fake_execute_tool(name, arguments, **_kwargs):return "Rendered HTML canvas: Done."monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "Render this."}],- tools = [{"type": "function", "function": {"name": "render_html"}}],- max_tool_iterations = 3,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "Render this."}],+ [{"type": "function", "function": {"name": "render_html"}}],+ max_tool_iterations = 3,)assert len(payloads) == 2@@ -1650,57 +1569,15 @@ def test_render_html_success_drops_tool_schema_before_final_pass(monkeypatch):def test_non_consecutive_duplicate_web_search_is_internal_noop(monkeypatch):first_search = [- _sse(- {- "tool_calls": [- {- "index": 0,- "id": "call_search_1",- "type": "function",- "function": {- "name": "web_search",- "arguments": json.dumps({"query": "gpu prices 2026"}),- },- }- ]- }- ),+ _tool_call_sse("web_search", {"query": "gpu prices 2026"}, "call_search_1"),_done(),]python_call = [- _sse(- {- "tool_calls": [- {- "index": 0,- "id": "call_python",- "type": "function",- "function": {- "name": "python",- "arguments": json.dumps({"code": "print('ok')"}),- },- }- ]- }- ),+ _tool_call_sse("python", {"code": "print('ok')"}, "call_python"),_done(),]duplicate_search = [- _sse(- {- "tool_calls": [- {- "index": 0,- "id": "call_search_2",- "type": "function",- "function": {- "name": "web_search",- "arguments": json.dumps({"query": "gpu prices 2026"}),- },- }- ]- }- ),+ _tool_call_sse("web_search", {"query": "gpu prices 2026"}, "call_search_2"),_done(),]final_stream = [_sse({"content": "Final answer from gathered data."}), _done()]@@ -1711,25 +1588,18 @@ def test_non_consecutive_duplicate_web_search_is_internal_noop(monkeypatch):payloads,)- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return f"ok:{name}"-- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ calls = _record_tool_calls(monkeypatch, lambda name: f"ok:{name}")tools = [{"type": "function", "function": {"name": "web_search"}},{"type": "function", "function": {"name": "python"}},]- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "search gpus in 2026 prices and use python"}],- tools = tools,- max_tool_iterations = 3,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "search gpus in 2026 prices and use python"}],+ tools,+ max_tool_iterations = 3,)assert calls == [@@ -1765,57 +1635,15 @@ def test_non_consecutive_duplicate_web_search_is_internal_noop(monkeypatch):def test_duplicate_web_search_noop_allows_distinct_followup_tool(monkeypatch):first_search = [- _sse(- {- "tool_calls": [- {- "index": 0,- "id": "call_search_1",- "type": "function",- "function": {- "name": "web_search",- "arguments": json.dumps({"query": "gpu prices 2026"}),- },- }- ]- }- ),+ _tool_call_sse("web_search", {"query": "gpu prices 2026"}, "call_search_1"),_done(),]duplicate_search = [- _sse(- {- "tool_calls": [- {- "index": 0,- "id": "call_search_2",- "type": "function",- "function": {- "name": "web_search",- "arguments": json.dumps({"query": "gpu prices 2026"}),- },- }- ]- }- ),+ _tool_call_sse("web_search", {"query": "gpu prices 2026"}, "call_search_2"),_done(),]python_call = [- _sse(- {- "tool_calls": [- {- "index": 0,- "id": "call_python",- "type": "function",- "function": {- "name": "python",- "arguments": json.dumps({"code": "print('ok')"}),- },- }- ]- }- ),+ _tool_call_sse("python", {"code": "print('ok')"}, "call_python"),_done(),]final_stream = [_sse({"content": "Final answer from gathered data."}), _done()]@@ -1826,25 +1654,18 @@ def test_duplicate_web_search_noop_allows_distinct_followup_tool(monkeypatch):payloads,)- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return f"ok:{name}"-- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ calls = _record_tool_calls(monkeypatch, lambda name: f"ok:{name}")tools = [{"type": "function", "function": {"name": "web_search"}},{"type": "function", "function": {"name": "python"}},]- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "search gpus in 2026 prices and use python"}],- tools = tools,- max_tool_iterations = 4,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "search gpus in 2026 prices and use python"}],+ tools,+ max_tool_iterations = 4,)assert calls == [@@ -1880,57 +1701,15 @@ def test_duplicate_web_search_noop_allows_distinct_followup_tool(monkeypatch):def test_repeated_duplicate_noop_transitions_to_final_pass(monkeypatch):first_search = [- _sse(- {- "tool_calls": [- {- "index": 0,- "id": "call_search_1",- "type": "function",- "function": {- "name": "web_search",- "arguments": json.dumps({"query": "gpu prices 2026"}),- },- }- ]- }- ),+ _tool_call_sse("web_search", {"query": "gpu prices 2026"}, "call_search_1"),_done(),]duplicate_one = [- _sse(- {- "tool_calls": [- {- "index": 0,- "id": "call_search_2",- "type": "function",- "function": {- "name": "web_search",- "arguments": json.dumps({"query": "gpu prices 2026"}),- },- }- ]- }- ),+ _tool_call_sse("web_search", {"query": "gpu prices 2026"}, "call_search_2"),_done(),]duplicate_two = [- _sse(- {- "tool_calls": [- {- "index": 0,- "id": "call_search_3",- "type": "function",- "function": {- "name": "web_search",- "arguments": json.dumps({"query": "gpu prices 2026"}),- },- }- ]- }- ),+ _tool_call_sse("web_search", {"query": "gpu prices 2026"}, "call_search_3"),_done(),]final_stream = [_sse({"content": "Final answer from first search."}), _done()]@@ -1940,20 +1719,13 @@ def test_repeated_duplicate_noop_transitions_to_final_pass(monkeypatch):[first_search, duplicate_one, duplicate_two, final_stream],payloads,)- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "result"-- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ calls = _record_tool_calls(monkeypatch, "result")- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "search gpus"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 10,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "search gpus"}],+ [{"type": "function", "function": {"name": "web_search"}}],+ max_tool_iterations = 10,)assert calls == [("web_search", {"query": "gpu prices 2026"})]@@ -1997,23 +1769,15 @@ def test_same_turn_duplicate_web_search_is_internal_noop(monkeypatch):_done(),]final_stream = [_sse({"content": "Final answer."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [same_turn_duplicates, final_stream], payloads)-- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "search-result"+ backend, payloads = _backend_and_payloads(monkeypatch, [same_turn_duplicates, final_stream])- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ calls = _record_tool_calls(monkeypatch, "search-result")- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "search gpus"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 2,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "search gpus"}],+ [{"type": "function", "function": {"name": "web_search"}}],+ max_tool_iterations = 2,)assert calls == [("web_search", {"query": "gpu prices 2026"})]@@ -2060,8 +1824,7 @@ def test_same_turn_duplicate_does_not_drop_later_parallel_call(monkeypatch):_done(),]final_stream = [_sse({"content": "Final answer."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [batch, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [batch, final_stream])calls: list[dict] = []@@ -2071,12 +1834,11 @@ def test_same_turn_duplicate_does_not_drop_later_parallel_call(monkeypatch):monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "search"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 3,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "search"}],+ [{"type": "function", "function": {"name": "web_search"}}],+ max_tool_iterations = 3,)# Both distinct calls ran; the duplicate did not (old `break` dropped search(b)).@@ -2130,23 +1892,15 @@ def test_same_turn_repeated_render_html_does_not_emit_second_provisional_start(m_done(),]final_stream = [_sse({"content": "Final answer."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [same_turn_render_calls, final_stream], payloads)-- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "Rendered HTML canvas: One."+ backend, payloads = _backend_and_payloads(monkeypatch, [same_turn_render_calls, final_stream])- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ calls = _record_tool_calls(monkeypatch, "Rendered HTML canvas: One.")- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "render html"}],- tools = [{"type": "function", "function": {"name": "render_html"}}],- max_tool_iterations = 2,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "render html"}],+ [{"type": "function", "function": {"name": "render_html"}}],+ max_tool_iterations = 2,)assert calls == [("render_html", {"code": "<html>one</html>"})]@@ -2182,38 +1936,18 @@ def test_disabled_tool_call_is_internal_noop(monkeypatch):)}),- _sse(- {- "tool_calls": [- {- "index": 0,- "id": "call_python_disabled",- "type": "function",- "function": {- "name": "python",- "arguments": json.dumps({"code": "print(1)"}),- },- }- ]- }- ),+ _tool_call_sse("python", {"code": "print(1)"}, "call_python_disabled"),_done(),]final_stream = [_sse({"content": "I cannot run Python here."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [disabled_python, final_stream], payloads)-- def fake_execute_tool(name, arguments, **_kwargs):- raise AssertionError(f"unexpected tool execution: {name} {arguments}")+ backend, payloads = _backend_and_payloads(monkeypatch, [disabled_python, final_stream])monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "run python"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "run python"}],+ [{"type": "function", "function": {"name": "web_search"}}],)assert not [event for event in events if event.get("type") in {"tool_start", "tool_end"}]@@ -2254,25 +1988,10 @@ def test_render_html_success_does_not_reprompt_render_html_intent(monkeypatch):"""first_stream = [- _sse(- {- "tool_calls": [- {- "index": 0,- "id": "call_first",- "type": "function",- "function": {- "name": "render_html",- "arguments": json.dumps(- {- "code": "<html><body>first</body></html>",- "title": "First",- }- ),- },- }- ]- }+ _tool_call_sse(+ "render_html",+ {"code": "<html><body>first</body></html>", "title": "First"},+ "call_first",),_done(),]@@ -2280,39 +1999,13 @@ def test_render_html_success_does_not_reprompt_render_html_intent(monkeypatch):_sse({"content": "I will now use render_html again."}),_done(),]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, post_tool_stream], payloads)-- calls: list[tuple[str, dict]] = []+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, post_tool_stream])- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "Rendered HTML canvas: First."+ calls = _record_tool_calls(monkeypatch, "Rendered HTML canvas: First.")- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)-- tools = [- {- "type": "function",- "function": {- "name": "render_html",- "description": "Render HTML.",- "parameters": {- "type": "object",- "properties": {"code": {"type": "string"}},- "required": ["code"],- },- },- }- ]+ tools = _render_html_tools()- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "Make a red square."}],- tools = tools,- max_tool_iterations = 1,- )- )+ events = _run_tool_loop(backend, [{"role": "user", "content": "Make a red square."}], tools)assert len(payloads) == 2assert len(calls) == 1@@ -2331,36 +2024,14 @@ def test_internal_reprompt_attempts_do_not_duplicate_visible_text(monkeypatch):[_sse({"content": "Understood. I will use render_html now."}), _done()]for _ in range(_MAX_REPROMPTS)]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)-- def fake_execute_tool(name, arguments, **_kwargs):- raise AssertionError(f"unexpected tool execution: {name} {arguments}")+ backend, payloads = _backend_and_payloads(monkeypatch, streams)monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)- tools = [- {- "type": "function",- "function": {- "name": "render_html",- "description": "Render HTML.",- "parameters": {- "type": "object",- "properties": {"code": {"type": "string"}},- "required": ["code"],- },- },- }- ]+ tools = _render_html_tools()- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "Make a red square."}],- tools = tools,- max_tool_iterations = 1,- nudge_tool_calls = True,- )+ events = _run_tool_loop(+ backend, [{"role": "user", "content": "Make a red square."}], tools, nudge_tool_calls = True)content_texts = [event.get("text", "") for event in events if event.get("type") == "content"]@@ -2375,59 +2046,24 @@ def test_post_tool_stall_still_nudged_after_a_pre_tool_reprompt(monkeypatch):streams = [[_sse({"content": "I will search the web now."}), _done()],[- _sse(- {- "tool_calls": [- {- "index": 0,- "id": "call_first",- "type": "function",- "function": {- "name": "web_search",- "arguments": json.dumps({"query": "red square"}),- },- }- ]- }- ),+ _tool_call_sse("web_search", {"query": "red square"}, "call_first"),_done(),],[_sse({"content": "Let me summarize the results."}), _done()],[_sse({"content": "Final answer: the square is red."}), _done()],]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, streams)- calls: list[tuple[str, dict]] = []+ calls = _record_tool_calls(monkeypatch, "Search results: red is #f00.")- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "Search results: red is #f00."-- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ tools = [_web_search_tool()]- tools = [- {- "type": "function",- "function": {- "name": "web_search",- "description": "Search the web.",- "parameters": {- "type": "object",- "properties": {"query": {"type": "string"}},- "required": ["query"],- },- },- }- ]-- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "Make a red square."}],- tools = tools,- max_tool_iterations = 2,- nudge_tool_calls = True,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "Make a red square."}],+ tools,+ max_tool_iterations = 2,+ nudge_tool_calls = True,)assert len(payloads) == 4@@ -2447,48 +2083,20 @@ def test_post_tool_reprompt_budget_is_one(monkeypatch):streams = [[- _sse(- {- "tool_calls": [- {- "index": 0,- "id": "call_first",- "type": "function",- "function": {- "name": "web_search",- "arguments": json.dumps({"query": "red square"}),- },- }- ]- }- ),+ _tool_call_sse("web_search", {"query": "red square"}, "call_first"),_done(),],[_sse({"content": "Let me summarize the results."}), _done()],[_sse({"content": "Now I will check the sources."}), _done()],]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, streams)monkeypatch.setattr("core.inference.tools.execute_tool",lambda *_a, **_k: "Search results: red is #f00.",)- tools = [- {- "type": "function",- "function": {- "name": "web_search",- "description": "Search the web.",- "parameters": {- "type": "object",- "properties": {"query": {"type": "string"}},- "required": ["query"],- },- },- }- ]+ tools = [_web_search_tool()]list(backend.generate_chat_completion_with_tools(@@ -2513,56 +2121,27 @@ def test_repeat_guard_resets_after_a_tool_runs(monkeypatch):streams = [[_sse({"content": stall}), _done()],[- _sse(- {- "tool_calls": [- {- "index": 0,- "id": "call_first",- "type": "function",- "function": {- "name": "web_search",- "arguments": json.dumps({"query": "red square"}),- },- }- ]- }- ),+ _tool_call_sse("web_search", {"query": "red square"}, "call_first"),_done(),],[_sse({"content": stall}), _done()],[_sse({"content": "Final answer: the square is red."}), _done()],]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, streams)monkeypatch.setattr("core.inference.tools.execute_tool",lambda *_a, **_k: "Search results: red is #f00.",)- tools = [- {- "type": "function",- "function": {- "name": "web_search",- "description": "Search the web.",- "parameters": {- "type": "object",- "properties": {"query": {"type": "string"}},- "required": ["query"],- },- },- }- ]+ tools = [_web_search_tool()]- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "Make a red square."}],- tools = tools,- max_tool_iterations = 2,- nudge_tool_calls = True,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "Make a red square."}],+ tools,+ max_tool_iterations = 2,+ nudge_tool_calls = True,)assert len(payloads) == 4@@ -2574,7 +2153,6 @@ def test_restatement_keeps_deletions_that_change_the_answer():"""A dropped word can invert the meaning, so a subset is not a restatement."""from core.inference.tool_call_parser import is_reprompt_restatement- from core.inference.llama_cpp import _should_suppress_forced_no_tool_output as suppressprevious = "Now I think the feature is not supported in version 1."corrected = "Now I think the feature is supported in version 1."@@ -2588,7 +2166,6 @@ def test_restatement_keeps_deletions_that_change_the_answer():def test_forced_turn_suppression_covers_obligation_phrasing():- from core.inference.llama_cpp import _should_suppress_forced_no_tool_output as suppressfor stall in ("I need to use render_html now","Need to call web_search",@@ -2648,8 +2225,6 @@ def test_forced_turn_intent_lead_in_needs_a_restatement_to_be_dropped():``INTENT_SIGNAL`` fires on lead-ins that introduce a real answer ("Now Ihave the results. ..."), so matching it alone would discard the answer."""- from core.inference.llama_cpp import _should_suppress_forced_no_tool_output as suppress-stall = "I will summarize the results now"answer = "Now I have the search results. The capital of Japan is Tokyo."@@ -2676,56 +2251,27 @@ def test_forced_turn_answer_with_an_intent_lead_in_survives_after_a_tool(monkeypanswer = "Now I have the results. The capital of Japan is Tokyo."streams = [[- _sse(- {- "tool_calls": [- {- "index": 0,- "id": "call_first",- "type": "function",- "function": {- "name": "web_search",- "arguments": json.dumps({"query": "capital of Japan"}),- },- }- ]- }- ),+ _tool_call_sse("web_search", {"query": "capital of Japan"}, "call_first"),_done(),],[_sse({"content": "Let me summarize what I found."}), _done()],[_sse({"content": answer}), _done()],]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, streams)monkeypatch.setattr("core.inference.tools.execute_tool",lambda *_a, **_k: "Search results: Tokyo.",)- tools = [- {- "type": "function",- "function": {- "name": "web_search",- "description": "Search the web.",- "parameters": {- "type": "object",- "properties": {"query": {"type": "string"}},- "required": ["query"],- },- },- }- ]+ tools = [_web_search_tool()]- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "What is the capital of Japan?"}],- tools = tools,- max_tool_iterations = 2,- nudge_tool_calls = True,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "What is the capital of Japan?"}],+ tools,+ max_tool_iterations = 2,+ nudge_tool_calls = True,)assert len(payloads) == 3@@ -2746,36 +2292,18 @@ def test_forced_turn_answer_with_an_intent_lead_in_survives_pre_tool(monkeypatchanswer,)]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)-- def fake_execute_tool(name, arguments, **_kwargs):- raise AssertionError(f"unexpected tool execution: {name} {arguments}")+ backend, payloads = _backend_and_payloads(monkeypatch, streams)monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)- tools = [- {- "type": "function",- "function": {- "name": "web_search",- "description": "Search the web.",- "parameters": {- "type": "object",- "properties": {"query": {"type": "string"}},- "required": ["query"],- },- },- }- ]+ tools = [_web_search_tool()]- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "What is the capital of Japan?"}],- tools = tools,- max_tool_iterations = 2,- nudge_tool_calls = True,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "What is the capital of Japan?"}],+ tools,+ max_tool_iterations = 2,+ nudge_tool_calls = True,)# Initial turn plus the three pre-tool nudges.@@ -2795,31 +2323,14 @@ def test_forced_reprompt_plain_final_answer_is_visible(monkeypatch):_done(),],]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)-- def fake_execute_tool(name, arguments, **_kwargs):- raise AssertionError(f"unexpected tool execution: {name} {arguments}")+ backend, payloads = _backend_and_payloads(monkeypatch, streams)monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)events = list(backend.generate_chat_completion_with_tools(messages = [{"role": "user", "content": "Make a red square."}],- tools = [- {- "type": "function",- "function": {- "name": "render_html",- "description": "Render HTML.",- "parameters": {- "type": "object",- "properties": {"code": {"type": "string"}},- "required": ["code"],- },- },- }- ],+ tools = _render_html_tools(),max_tool_iterations = 1,nudge_tool_calls = True,)@@ -2846,36 +2357,17 @@ def test_forced_reprompt_plain_final_answer_is_visible(monkeypatch):def test_internal_reprompt_disabled_when_auto_heal_disabled(monkeypatch):streams = [[_sse({"content": "I will use render_html now."}), _done()]]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)-- def fake_execute_tool(name, arguments, **_kwargs):- raise AssertionError(f"unexpected tool execution: {name} {arguments}")+ backend, payloads = _backend_and_payloads(monkeypatch, streams)monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)- tools = [- {- "type": "function",- "function": {- "name": "render_html",- "description": "Render HTML.",- "parameters": {- "type": "object",- "properties": {"code": {"type": "string"}},- "required": ["code"],- },- },- }- ]+ tools = _render_html_tools()- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "Make a red square."}],- tools = tools,- max_tool_iterations = 1,- auto_heal_tool_calls = False,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "Make a red square."}],+ tools,+ auto_heal_tool_calls = False,)content_texts = [event.get("text", "") for event in events if event.get("type") == "content"]@@ -2887,37 +2379,18 @@ def test_internal_reprompt_disabled_when_nudge_tool_calls_false(monkeypatch):# Explicit nudge_tool_calls=False disables the plan-without-action# re-prompt even with Auto-Heal on (None keeps the default-on behavior).streams = [[_sse({"content": "I will use render_html now."}), _done()]]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)-- def fake_execute_tool(name, arguments, **_kwargs):- raise AssertionError(f"unexpected tool execution: {name} {arguments}")+ backend, payloads = _backend_and_payloads(monkeypatch, streams)monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)- tools = [- {- "type": "function",- "function": {- "name": "render_html",- "description": "Render HTML.",- "parameters": {- "type": "object",- "properties": {"code": {"type": "string"}},- "required": ["code"],- },- },- }- ]+ tools = _render_html_tools()- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "Make a red square."}],- tools = tools,- max_tool_iterations = 1,- auto_heal_tool_calls = True,- nudge_tool_calls = False,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "Make a red square."}],+ tools,+ auto_heal_tool_calls = True,+ nudge_tool_calls = False,)content_texts = [event.get("text", "") for event in events if event.get("type") == "content"]@@ -2937,23 +2410,14 @@ def test_auto_heal_disabled_parses_well_formed_xml_when_tools_enabled(monkeypatc],[_sse({"content": "done"}), _done()],]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "result"-- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ backend, payloads = _backend_and_payloads(monkeypatch, streams)+ calls = _record_tool_calls(monkeypatch, "result")- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "search"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- auto_heal_tool_calls = False,- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "search"}],+ [{"type": "function", "function": {"name": "web_search"}}],+ auto_heal_tool_calls = False,)assert calls == [("web_search", {"query": "x"})]@@ -2968,24 +2432,15 @@ def test_textual_mistral_marker_not_leaked_when_inline_with_preface(monkeypatch)# shared parser patterns (which know ``[TOOL_CALLS]``); the legacy set leaked the marker to clients.streams = [[_sse({"content": 'Let me search. [TOOL_CALLS]web_search{"query":"cats"}'}), _done()],- [_sse({"content": "done"}), _done()],- ]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "result"-- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ [_sse({"content": "done"}), _done()],+ ]+ backend, payloads = _backend_and_payloads(monkeypatch, streams)+ calls = _record_tool_calls(monkeypatch, "result")- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "search"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "search"}],+ [{"type": "function", "function": {"name": "web_search"}}],)assert calls == [("web_search", {"query": "cats"})]@@ -3010,22 +2465,13 @@ def test_textual_explicit_id_reuses_provisional_card(monkeypatch):[_sse({"content": c}) for c in chunks] + [_done()],[_sse({"content": "done"}), _done()],]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "result"-- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ backend, payloads = _backend_and_payloads(monkeypatch, streams)+ calls = _record_tool_calls(monkeypatch, "result")- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "search"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "search"}],+ [{"type": "function", "function": {"name": "web_search"}}],)assert calls == [("web_search", {"query": big_query})]@@ -3053,22 +2499,13 @@ def test_textual_llama_python_tag_marker_not_leaked(monkeypatch):[_sse({"content": '<|python_tag|>web_search.call(query="cats")'}), _done()],[_sse({"content": "done"}), _done()],]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "result"-- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ backend, payloads = _backend_and_payloads(monkeypatch, streams)+ calls = _record_tool_calls(monkeypatch, "result")- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "search"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "search"}],+ [{"type": "function", "function": {"name": "web_search"}}],)assert calls == [("web_search", {"query": "cats"})]@@ -3096,39 +2533,14 @@ def test_reprompted_tool_call_still_streams_final_answer(monkeypatch):],[_sse({"content": "Final note after tool."}), _done()],]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)-- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "Rendered HTML canvas: Forced."+ backend, payloads = _backend_and_payloads(monkeypatch, streams)- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ calls = _record_tool_calls(monkeypatch, "Rendered HTML canvas: Forced.")- tools = [- {- "type": "function",- "function": {- "name": "render_html",- "description": "Render HTML.",- "parameters": {- "type": "object",- "properties": {"code": {"type": "string"}},- "required": ["code"],- },- },- }- ]+ tools = _render_html_tools()- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "Make a red square."}],- tools = tools,- max_tool_iterations = 1,- nudge_tool_calls = True,- )+ events = _run_tool_loop(+ backend, [{"role": "user", "content": "Make a red square."}], tools, nudge_tool_calls = True)assert len(calls) == 1@@ -3142,18 +2554,7 @@ def _status_texts(events: list[dict]) -> list[str]:return [event["text"] for event in events if event.get("type") == "status"]-_WEB_SEARCH_TOOL = {- "type": "function",- "function": {- "name": "web_search",- "description": "Search the web.",- "parameters": {- "type": "object",- "properties": {"query": {"type": "string"}},- "required": ["query"],- },- },-}+_WEB_SEARCH_TOOL = _web_search_tool()def _nudge_then_search_streams() -> list[list[str]]:@@ -3162,21 +2563,7 @@ def _nudge_then_search_streams() -> list[list[str]]:return [[_sse({"content": "I will search the web now."}), _done()],[- _sse(- {- "tool_calls": [- {- "index": 0,- "id": "call_search",- "type": "function",- "function": {- "name": "web_search",- "arguments": json.dumps({"query": "red square"}),- },- }- ]- }- ),+ _tool_call_sse("web_search", {"query": "red square"}, "call_search"),_done(),],[_sse({"content": "Final answer: the square is red."}), _done()],@@ -3186,20 +2573,18 @@ def _nudge_then_search_streams() -> list[list[str]]:def test_plan_without_action_nudge_is_announced_on_the_status_channel(monkeypatch):"""The re-prompted turn is hidden, so without a badge the UI looks frozen."""- payloads: list[dict] = []- backend = _make_backend(monkeypatch, _nudge_then_search_streams(), payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, _nudge_then_search_streams())monkeypatch.setattr("core.inference.tools.execute_tool",lambda *_a, **_k: "Search results: red is #f00.",)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "What colour is the square?"}],- tools = [_WEB_SEARCH_TOOL],- max_tool_iterations = 2,- nudge_tool_calls = True,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "What colour is the square?"}],+ [_WEB_SEARCH_TOOL],+ max_tool_iterations = 2,+ nudge_tool_calls = True,)statuses = _status_texts(events)@@ -3217,16 +2602,14 @@ def test_plan_without_action_nudge_status_clears_when_the_retry_just_answers(mon[_sse({"content": "I will search the web now."}), _done()],[_sse({"content": "No search needed. Final answer: the square is red."}), _done()],]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, streams)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "What colour is the square?"}],- tools = [_WEB_SEARCH_TOOL],- max_tool_iterations = 2,- nudge_tool_calls = True,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "What colour is the square?"}],+ [_WEB_SEARCH_TOOL],+ max_tool_iterations = 2,+ nudge_tool_calls = True,)statuses = _status_texts(events)@@ -3242,12 +2625,11 @@ def test_direct_answer_never_shows_the_nudge_status(monkeypatch):payloads,)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "What colour is the square?"}],- tools = [_WEB_SEARCH_TOOL],- max_tool_iterations = 2,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "What colour is the square?"}],+ [_WEB_SEARCH_TOOL],+ max_tool_iterations = 2,)assert NUDGE_TOOL_CALLS_STATUS not in _status_texts(events)@@ -3273,12 +2655,8 @@ def test_clarification_request_is_not_nudged(monkeypatch):payloads,)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "Balls"}],- tools = [_WEB_SEARCH_TOOL],- max_tool_iterations = 2,- )+ events = _run_tool_loop(+ backend, [{"role": "user", "content": "Balls"}], [_WEB_SEARCH_TOOL], max_tool_iterations = 2)assert NUDGE_TOOL_CALLS_STATUS not in _status_texts(events)@@ -3289,20 +2667,18 @@ def test_clarification_request_is_not_nudged(monkeypatch):def test_nudge_status_absent_when_nudging_is_disabled(monkeypatch):- payloads: list[dict] = []- backend = _make_backend(monkeypatch, _nudge_then_search_streams(), payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, _nudge_then_search_streams())monkeypatch.setattr("core.inference.tools.execute_tool",lambda *_a, **_k: "Search results: red is #f00.",)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "What colour is the square?"}],- tools = [_WEB_SEARCH_TOOL],- max_tool_iterations = 2,- nudge_tool_calls = False,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "What colour is the square?"}],+ [_WEB_SEARCH_TOOL],+ max_tool_iterations = 2,+ nudge_tool_calls = False,)assert NUDGE_TOOL_CALLS_STATUS not in _status_texts(events)@@ -3310,15 +2686,13 @@ def test_nudge_status_absent_when_nudging_is_disabled(monkeypatch):def test_nudge_is_off_when_the_request_flag_is_omitted(monkeypatch):- payloads: list[dict] = []- backend = _make_backend(monkeypatch, _nudge_then_search_streams(), payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, _nudge_then_search_streams())- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "What colour is the square?"}],- tools = [_WEB_SEARCH_TOOL],- max_tool_iterations = 2,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "What colour is the square?"}],+ [_WEB_SEARCH_TOOL],+ max_tool_iterations = 2,)assert NUDGE_TOOL_CALLS_STATUS not in _status_texts(events)@@ -3330,16 +2704,9 @@ def test_confirm_tool_calls_allow_executes_gguf_tool(monkeypatch):_structured_tool_call("python", {"code": "print(1)"}, "call_py"),[_sse({"content": "Done."}), _done()],]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)-- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "OK"+ backend, payloads = _backend_and_payloads(monkeypatch, streams)- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ calls = _record_tool_calls(monkeypatch, "OK")monkeypatch.setattr("core.inference.llama_cpp.new_approval_id", lambda: "approval-1")monkeypatch.setattr("core.inference.llama_cpp.begin_tool_decision",@@ -3370,8 +2737,7 @@ def test_confirm_tool_calls_allow_executes_gguf_tool(monkeypatch):def test_confirm_tool_calls_close_after_prompt_cleans_gguf_slot(monkeypatch):approval_id = "approval-close"streams = [_structured_tool_call("python", {"code": "print(1)"}, "call_py")]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, streams)monkeypatch.setattr("core.inference.tools.execute_tool",@@ -3408,8 +2774,7 @@ def test_confirm_tool_calls_close_after_prompt_cleans_gguf_slot(monkeypatch):def test_confirm_tool_calls_skips_gguf_rag_autoinject(monkeypatch):streams = [[_sse({"content": "Done."}), _done()]]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, streams)def fail_autoinject(*_args, **_kwargs):raise AssertionError("RAG autoinject must not run before approval")@@ -3446,8 +2811,7 @@ def test_rag_autoinject_counts_as_a_prior_tool_execution(monkeypatch):[_sse({"content": "Still working on the summary."}), _done()],[_sse({"content": "Final answer: the passages describe Tokyo."}), _done()],]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, streams)monkeypatch.setattr("core.inference.tools.build_rag_autoinject",@@ -3457,14 +2821,13 @@ def test_rag_autoinject_counts_as_a_prior_tool_execution(monkeypatch):},)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "summarize the docs"}],- tools = [{"type": "function", "function": {"name": "search_knowledge_base"}}],- max_tool_iterations = 2,- nudge_tool_calls = True,- rag_scope = {"thread_id": "t1"},- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "summarize the docs"}],+ [{"type": "function", "function": {"name": "search_knowledge_base"}}],+ max_tool_iterations = 2,+ nudge_tool_calls = True,+ rag_scope = {"thread_id": "t1"},)# Initial turn plus one retry; read as pre-tool it would spend the full budget.@@ -3528,8 +2891,7 @@ def test_confirm_tool_calls_deny_skips_gguf_tool_and_retry_can_execute(monkeypat_structured_tool_call("python", {"code": "print(1)"}, "call_py_retry"),[_sse({"content": "Done."}), _done()],]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, streams)calls: list[tuple[str, dict]] = []@@ -3615,23 +2977,14 @@ def test_large_python_tool_call_emits_early_provisional_start(monkeypatch):first_stream = _streamed_structured_tool_call("python", {"code": big_code}, "call_py_big")final_stream = [_sse({"content": "Done."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, final_stream])- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "OK"-- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ calls = _record_tool_calls(monkeypatch, "OK")- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "write code"}],- tools = [{"type": "function", "function": {"name": "python"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "write code"}],+ [{"type": "function", "function": {"name": "python"}}],)tool_starts = [e for e in events if e.get("type") == "tool_start"]@@ -3666,20 +3019,17 @@ def test_gated_python_call_still_streams_its_arguments(monkeypatch):first_stream = _streamed_structured_tool_call("python", {"code": big_code}, "call_gated")final_stream = [_sse({"content": "Done."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, final_stream])monkeypatch.setattr("core.inference.tools.execute_tool", lambda name, arguments, **_k: "OK")monkeypatch.setattr("core.inference.llama_cpp.wait_tool_decision", lambda *_a, **_k: "allow")- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "write code"}],- tools = [{"type": "function", "function": {"name": "python"}}],- confirm_tool_calls = True,- permission_mode = "ask",- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "write code"}],+ [{"type": "function", "function": {"name": "python"}}],+ confirm_tool_calls = True,+ permission_mode = "ask",)tool_starts = [e for e in events if e.get("type") == "tool_start"]@@ -3705,19 +3055,16 @@ def test_auto_mode_render_html_suppresses_provisional_card_under_confirm(monkeypargs = {"code": "<html>" + "x" * 80 + "</html>"}first_stream = _streamed_structured_tool_call("render_html", args, "call_rh")final_stream = [_sse({"content": "Done."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, final_stream])monkeypatch.setattr("core.inference.tools.execute_tool", lambda name, arguments, **_k: "OK")- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "make a card"}],- tools = [{"type": "function", "function": {"name": "render_html"}}],- confirm_tool_calls = True,- permission_mode = "auto",- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "make a card"}],+ [{"type": "function", "function": {"name": "render_html"}}],+ confirm_tool_calls = True,+ permission_mode = "auto",)tool_starts = [e for e in events if e.get("type") == "tool_start"]@@ -3736,17 +3083,14 @@ def test_small_python_tool_call_has_no_provisional_start(monkeypatch):first_stream = _structured_tool_call("python", {"code": "print(1)"}, "call_py_small")final_stream = [_sse({"content": "Done."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, final_stream])monkeypatch.setattr("core.inference.tools.execute_tool", lambda *_a, **_k: "OK")- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "x"}],- tools = [{"type": "function", "function": {"name": "python"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "x"}],+ [{"type": "function", "function": {"name": "python"}}],)tool_starts = [e for e in events if e.get("type") == "tool_start"]@@ -3801,16 +3145,9 @@ def test_parallel_large_tool_calls_each_emit_provisional_start(monkeypatch):])final_stream = [_sse({"content": "Done."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, final_stream])- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "OK"-- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ calls = _record_tool_calls(monkeypatch, "OK")events = list(backend.generate_chat_completion_with_tools(@@ -3845,16 +3182,9 @@ def test_parallel_disabled_suppresses_provisional_for_later_calls(monkeypatch):])final_stream = [_sse({"content": "Done."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, final_stream])- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "OK"-- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ calls = _record_tool_calls(monkeypatch, "OK")events = list(backend.generate_chat_completion_with_tools(@@ -3883,8 +3213,6 @@ def test_connect_error_during_tool_call_closes_provisional_card(monkeypatch):"""If llama-server drops mid tool-call after a provisional card is shown, theloop must close that card before surfacing the error so the UI never leaves atool spinning forever."""- import httpx-big_code = "total = 0\n" + "\n".join(f"total += {i}" for i in range(120))fragments = _streamed_structured_tool_call("python", {"code": big_code}, "call_py_err")# Drop the trailing [DONE]; raise a connection error after the fragments@@ -3896,8 +3224,7 @@ def test_connect_error_during_tool_call_closes_provisional_card(monkeypatch):yield chunkraise httpx.ConnectError("connection lost mid stream")- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [raising_stream()], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [raising_stream()])respawn_calls: list[bool] = []monkeypatch.setattr(@@ -3940,8 +3267,6 @@ def test_connect_error_during_tool_call_closes_provisional_card(monkeypatch):def test_connect_error_before_tool_stream_respawns_and_retries(monkeypatch):"""A dead server before the first tool-loop response is opened is safe to retry."""- import httpx-payloads: list[dict] = []urls: list[str] = []backend = _make_backend(@@ -3955,12 +3280,10 @@ def test_connect_error_before_tool_stream_respawns_and_retries(monkeypatch):)respawn_calls = _patch_successful_respawn(monkeypatch, backend, port = 49999)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "hello"}],- tools = [{"type": "function", "function": {"name": "python"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "hello"}],+ [{"type": "function", "function": {"name": "python"}}],)assert respawn_calls == [True]@@ -3975,7 +3298,6 @@ def test_connect_error_before_tool_stream_respawns_and_retries(monkeypatch):def test_tool_loop_refits_each_preflight_path_after_context_shrinking_respawn(monkeypatch):"""Both an ordinary iteration and final synthesis refit without repeating old drops."""- import httpxfor max_tool_iterations in (1, 0):payloads: list[dict] = []backend = _make_backend(@@ -4030,8 +3352,7 @@ def test_tool_loop_refits_each_preflight_path_after_context_shrinking_respawn(modef test_tool_loop_compacts_text_history_around_latest_audio(monkeypatch):"""Unpriced media must not disable compaction that the text alone requires."""- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [[_sse({"content": "OK"}), _done()]], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [[_sse({"content": "OK"}), _done()]])backend._effective_context_length = 100counted: list[list[dict]] = []@@ -4135,10 +3456,7 @@ def test_tool_loop_secondary_counts_strip_media_but_payloads_keep_it(monkeypatch@pytest.mark.parametrize("with_tools", [False, True])def test_media_compaction_recall_recount_uses_the_stripped_view(monkeypatch, with_tools):- from core.inference import llama_cpp-- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [[_sse({"content": "OK"}), _done()]], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [[_sse({"content": "OK"}), _done()]])backend._effective_context_length = 100counted: list[list[dict]] = []recall_recounts: list[list[dict]] = []@@ -4214,10 +3532,6 @@ def test_media_compaction_recall_recount_uses_the_stripped_view(monkeypatch, witdef test_a_respawn_refit_that_misses_its_target_still_archives_and_reports(monkeypatch):"""A rescued respawn refit archives its evictions and emits metadata."""- import httpx-- from core.inference import llama_cpp-# Captured once: the second pass would otherwise wrap the first pass's spy.real_archive = llama_cpp._archive_and_recall@@ -4287,8 +3601,6 @@ def test_a_respawn_refit_that_misses_its_target_still_archives_and_reports(monkedef test_tool_loop_retries_preflight_when_counting_failed_on_the_dead_server(monkeypatch):- import httpx-payloads: list[dict] = []backend = _make_backend(monkeypatch,@@ -4328,10 +3640,10 @@ def test_tool_loop_retries_preflight_when_counting_failed_on_the_dead_server(mondef test_tool_loop_does_not_send_a_stale_payload_when_respawn_refit_fails(monkeypatch):- import httpxfor max_tool_iterations in (1, 0):- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [httpx.ConnectError("server is down")], payloads)+ backend, payloads = _backend_and_payloads(+ monkeypatch, [httpx.ConnectError("server is down")]+ )backend._effective_context_length = 100count_calls = 0@@ -4373,8 +3685,6 @@ def test_tool_loop_does_not_send_a_stale_payload_when_respawn_refit_fails(monkeydef test_connect_error_retry_reuses_rolling_preflight_without_duplicate_notice(monkeypatch):"""A respawn retries the fitted request without reporting its dropped turns twice."""- import httpx-payloads: list[dict] = []backend = _make_backend(monkeypatch,@@ -4420,8 +3730,7 @@ def test_connect_error_retry_reuses_rolling_preflight_without_duplicate_notice(mdef test_rolling_preflight_counts_the_sanitized_payload(monkeypatch):- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [[_done()]], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [[_done()]])backend._effective_context_length = 100counted: list[list[dict]] = []@@ -4455,9 +3764,6 @@ def test_a_respawn_refit_archives_what_it_evicts(monkeypatch):They evict more of the conversation, and without archiving there those turns aregone for good: unlike the ordinary preflight, nothing else sees them."""- import httpx- from core.inference import llama_cpp-archived: list = []def fake_archive(conversation, before, **kwargs):@@ -4513,9 +3819,6 @@ def test_the_respawn_retry_keeps_the_thread(monkeypatch):Without the thread those extra turns are archived nowhere and no reserve or boundaryapplies, on the one path that deliberately compacts a second time."""- import httpx- from core.inference import llama_cpp-payloads: list[dict] = []backend = _make_backend(monkeypatch,@@ -4564,8 +3867,6 @@ def test_the_respawn_retry_keeps_the_thread(monkeypatch):def test_rolling_respawn_retry_refits_when_the_effective_context_changes(monkeypatch):"""A smaller replacement window can evict more without repeating the first eviction."""- import httpx-payloads: list[dict] = []backend = _make_backend(monkeypatch,@@ -4618,7 +3919,6 @@ def test_rolling_respawn_retry_refits_when_the_effective_context_changes(monkeypdef test_connect_error_after_tool_result_recovers_both_generation_paths(monkeypatch):"""Recover either post-tool generation path without rerunning the tool."""- import httpxfor max_tool_iterations, final_text in ((2, "The result is 1."),(1, "Final answer."),@@ -4642,12 +3942,11 @@ def test_connect_error_after_tool_result_recovers_both_generation_paths(monkeypamonkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "print one"}],- tools = [{"type": "function", "function": {"name": "python"}}],- max_tool_iterations = max_tool_iterations,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "print one"}],+ [{"type": "function", "function": {"name": "python"}}],+ max_tool_iterations = max_tool_iterations,)assert respawn_calls == [True]@@ -4659,8 +3958,6 @@ def test_connect_error_after_tool_result_recovers_both_generation_paths(monkeypadef test_connect_error_retry_is_bounded(monkeypatch):"""A failed retry surfaces the error without another respawn attempt."""- import httpx-payloads: list[dict] = []backend = _make_backend(monkeypatch,@@ -4693,7 +3990,6 @@ def test_connect_error_retry_is_bounded(monkeypatch):def test_pre_header_transport_errors_also_respawn(monkeypatch):"""A child that dies during prefill already accepted the socket, so it doesnot surface as ConnectError. Nothing has streamed yet, so replay is safe."""- import httpxfor exc in (httpx.RemoteProtocolError("server disconnected without sending a response"),httpx.ReadError("connection reset by peer"),@@ -4705,12 +4001,10 @@ def test_pre_header_transport_errors_also_respawn(monkeypatch):)respawn_calls = _patch_successful_respawn(monkeypatch, backend)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "hello"}],- tools = [{"type": "function", "function": {"name": "python"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "hello"}],+ [{"type": "function", "function": {"name": "python"}}],)assert respawn_calls == [True], type(exc).__name__@@ -4722,7 +4016,6 @@ def test_a_not_yet_reaped_child_does_not_burn_the_retry(monkeypatch):"""A closing server can beat its own exit status, so poll() briefly reports italive. Without a grace wait _respawn_if_dead hands back the stale _healthy and thesingle retry is spent on the corpse rather than on a replacement."""- import httpxclass _Dying:# reapable only from the 4th poll, mimicking teardown lagging the socket close@@ -4737,8 +4030,7 @@ def test_a_not_yet_reaped_child_does_not_burn_the_retry(monkeypatch):return -9return None- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [])backend._process = _Dying()backend._healthy = Truebackend._respawn_lock = threading.RLock()@@ -4784,12 +4076,10 @@ def test_a_not_yet_reaped_child_does_not_burn_the_retry(monkeypatch):monkeypatch.setattr(backend, "_stream_with_retry", dead_until_respawned)monkeypatch.setattr(backend, "load_model", fake_load)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "hello"}],- tools = [{"type": "function", "function": {"name": "python"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "hello"}],+ [{"type": "function", "function": {"name": "python"}}],)assert len(loads) == 1@@ -4798,10 +4088,8 @@ def test_a_not_yet_reaped_child_does_not_burn_the_retry(monkeypatch):def test_prefill_timeout_is_not_retried(monkeypatch):"""A slow-but-alive server must not have its first-token budget spent twice."""- import httpxfor exc in (httpx.ReadTimeout("no first token"), httpx.PoolTimeout("pool")):- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [exc], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [exc])respawn_calls = _patch_successful_respawn(monkeypatch, backend)raised = False@@ -4823,10 +4111,8 @@ def test_prefill_timeout_is_not_retried(monkeypatch):def test_mtp_crash_recovery_wins_over_respawn(monkeypatch):"""An MTP crash reloads without MTP, so never respawn the same config on top."""- import httpxfor max_tool_iterations in (2, 1):- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [httpx.ConnectError("mtp crash")], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [httpx.ConnectError("mtp crash")])monkeypatch.setattr(backend, "_maybe_recover_from_mtp_crash", lambda *_a, **_k: True)respawn_calls = _patch_successful_respawn(monkeypatch, backend)@@ -4860,23 +4146,14 @@ def test_empty_tool_call_id_does_not_emit_provisional_card(monkeypatch):# Same large streamed call as the provisional test, but with an empty id.first_stream = _streamed_structured_tool_call("python", {"code": big_code}, "")final_stream = [_sse({"content": "Done."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, final_stream], payloads)-- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "OK"+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, final_stream])- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ calls = _record_tool_calls(monkeypatch, "OK")- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "write code"}],- tools = [{"type": "function", "function": {"name": "python"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "write code"}],+ [{"type": "function", "function": {"name": "python"}}],)# No provisional card (empty-args tool_start) was surfaced for the empty id.@@ -4899,23 +4176,14 @@ def test_bare_json_tool_call_streamed_is_not_leaked_and_executes(monkeypatch):bare_call = '{"name": "web_search", "parameters": {"query": "weather in Sydney"}}'first_stream = _streamed_content(bare_call)final_stream = [_sse({"content": "It is sunny in Sydney."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, final_stream], payloads)-- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "Weather: sunny, 22C."+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, final_stream])- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ calls = _record_tool_calls(monkeypatch, "Weather: sunny, 22C.")- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "weather in Sydney?"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "weather in Sydney?"}],+ [{"type": "function", "function": {"name": "web_search"}}],)# The tool ran with the parsed arguments.@@ -4938,8 +4206,7 @@ def test_ordinary_json_with_name_key_is_shown_not_treated_as_tool_call(monkeypatanswer = '{"name": "Alice", "parameters": {"age": 30}}'first_stream = _streamed_content(answer)- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream])calls: list[tuple[str, dict]] = []monkeypatch.setattr(@@ -4947,12 +4214,10 @@ def test_ordinary_json_with_name_key_is_shown_not_treated_as_tool_call(monkeypatlambda n, a, **_k: calls.append((n, a)) or "x",)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "give me a person record"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "give me a person record"}],+ [{"type": "function", "function": {"name": "web_search"}}],)assert calls == [], calls@@ -4966,20 +4231,17 @@ def test_incomplete_bare_json_truncation_is_not_leaked(monkeypatch):truncated = '{"name": "web_search", "parameters": {"query": "weather in S'stream = _streamed_content(truncated)- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [stream])monkeypatch.setattr("core.inference.tools.execute_tool",lambda *_a, **_k: (_ for _ in ()).throw(AssertionError("no complete call")),)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "weather?"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "weather?"}],+ [{"type": "function", "function": {"name": "web_search"}}],)content_texts = [e.get("text", "") for e in events if e.get("type") == "content"]@@ -4995,8 +4257,7 @@ def test_gguf_truncated_ordinary_json_with_name_key_is_shown_not_suppressed(monktruncated = '{"name": "Alice", "age": 30, "bio": "loves 'stream = _streamed_content(truncated)- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [stream])calls: list[tuple[str, dict]] = []monkeypatch.setattr(@@ -5004,12 +4265,10 @@ def test_gguf_truncated_ordinary_json_with_name_key_is_shown_not_suppressed(monklambda n, a, **_k: calls.append((n, a)) or "x",)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "start a person record"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "start a person record"}],+ [{"type": "function", "function": {"name": "web_search"}}],)assert calls == [], calls@@ -5022,8 +4281,7 @@ def test_gguf_truncated_disabled_name_json_is_preserved_when_tools_active(monkeytruncated = '{"name": "Alice", "parameters": {"age": 30'stream = _streamed_content(truncated)- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [stream])calls: list[tuple[str, dict]] = []monkeypatch.setattr(@@ -5031,12 +4289,10 @@ def test_gguf_truncated_disabled_name_json_is_preserved_when_tools_active(monkeylambda n, a, **_k: calls.append((n, a)) or "x",)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "give json"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "give json"}],+ [{"type": "function", "function": {"name": "web_search"}}],)assert calls == [], calls@@ -5050,20 +4306,17 @@ def test_gguf_truncated_enabled_name_json_is_still_suppressed(monkeypatch):truncated = '{"name": "web_search", "parameters": {"query": "weather in S'stream = _streamed_content(truncated)- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [stream])monkeypatch.setattr("core.inference.tools.execute_tool",lambda *_a, **_k: (_ for _ in ()).throw(AssertionError("no complete call")),)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "weather?"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "weather?"}],+ [{"type": "function", "function": {"name": "web_search"}}],)content_texts = [e.get("text", "") for e in events if e.get("type") == "content"]@@ -5079,8 +4332,7 @@ def test_gguf_oversized_disabled_name_json_is_preserved(monkeypatch):answer = '{"name":"Alice","parameters":{"bio":"' + big # never closesfirst_stream = [_sse({"content": answer[i : i + 2000]}) for i in range(0, len(answer), 2000)]first_stream.append(_done())- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream])calls: list[tuple[str, dict]] = []monkeypatch.setattr(@@ -5088,12 +4340,10 @@ def test_gguf_oversized_disabled_name_json_is_preserved(monkeypatch):lambda n, a, **_k: calls.append((n, a)) or "x",)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "long json"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "long json"}],+ [{"type": "function", "function": {"name": "web_search"}}],)assert calls == [], calls@@ -5110,23 +4360,14 @@ def test_gemma_wrapperless_call_streamed_is_not_leaked_and_executes(monkeypatch)gemma_call = 'call:web_search{query:"weather in Sydney"}'first_stream = _streamed_content(gemma_call)final_stream = [_sse({"content": "It is sunny in Sydney."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, final_stream])- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "Weather: sunny, 22C."+ calls = _record_tool_calls(monkeypatch, "Weather: sunny, 22C.")- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)-- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "weather in Sydney?"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "weather in Sydney?"}],+ [{"type": "function", "function": {"name": "web_search"}}],)assert calls == [("web_search", {"query": "weather in Sydney"})]@@ -5169,16 +4410,9 @@ def test_metadata_event_preserves_prompt_tokens_details(monkeypatch):),_done(),]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [stream])- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "hi"}],- tools = [],- max_tool_iterations = 1,- )- )+ events = _run_tool_loop(backend, [{"role": "user", "content": "hi"}], [])metadata = [e for e in events if e.get("type") == "metadata"]assert metadata, "expected a metadata event"@@ -5196,16 +4430,9 @@ def test_metadata_event_omits_prompt_tokens_details_when_absent(monkeypatch):_usage_done({"prompt_tokens": 5, "completion_tokens": 2}),_done(),]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [stream])- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "hi"}],- tools = [],- max_tool_iterations = 1,- )- )+ events = _run_tool_loop(backend, [{"role": "user", "content": "hi"}], [])metadata = [e for e in events if e.get("type") == "metadata"]assert metadata, "expected a metadata event"@@ -5224,23 +4451,14 @@ def test_gguf_rehearsal_name_split_before_args_is_not_leaked(monkeypatch):_done(),]final_stream = [_sse({"content": "Found cats."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, final_stream], payloads)-- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "result"+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, final_stream])- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ calls = _record_tool_calls(monkeypatch, "result")- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "search cats"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "search cats"}],+ [{"type": "function", "function": {"name": "web_search"}}],)assert calls == [("web_search", {"query": "cats"})], calls@@ -5262,8 +4480,7 @@ def test_gguf_initial_buffer_flush_holds_split_rehearsal_name(monkeypatch):_done(),]final_stream = [_sse({"content": "Found cats."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, final_stream])calls: list[tuple[str, dict]] = []monkeypatch.setattr(@@ -5271,12 +4488,10 @@ def test_gguf_initial_buffer_flush_holds_split_rehearsal_name(monkeypatch):lambda name, arguments, **_k: calls.append((name, arguments)) or "result",)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "search cats"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "search cats"}],+ [{"type": "function", "function": {"name": "web_search"}}],)assert calls == [("web_search", {"query": "cats"})], calls@@ -5299,8 +4514,7 @@ def test_gguf_rehearsal_name_after_prose_in_streaming_is_not_leaked(monkeypatch)_done(),]final_stream = [_sse({"content": "Found cats."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, final_stream])calls: list[tuple[str, dict]] = []monkeypatch.setattr(@@ -5308,12 +4522,10 @@ def test_gguf_rehearsal_name_after_prose_in_streaming_is_not_leaked(monkeypatch)lambda name, arguments, **_k: calls.append((name, arguments)) or "result",)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "search cats"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "search cats"}],+ [{"type": "function", "function": {"name": "web_search"}}],)assert calls == [("web_search", {"query": "cats"})], calls@@ -5332,8 +4544,7 @@ def test_gguf_plain_answer_ending_with_tool_name_word_is_preserved(monkeypatch):_sse({"content": "web_search"}),_done(),]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream])calls: list[tuple[str, dict]] = []monkeypatch.setattr(@@ -5341,12 +4552,10 @@ def test_gguf_plain_answer_ending_with_tool_name_word_is_preserved(monkeypatch):lambda name, arguments, **_k: calls.append((name, arguments)) or "result",)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "advise"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "advise"}],+ [{"type": "function", "function": {"name": "web_search"}}],)assert calls == [], calls@@ -5367,8 +4576,7 @@ def test_gguf_long_tool_name_split_rehearsal_is_not_capped_and_executes(monkeypa_done(),]final_stream = [_sse({"content": "done"}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, final_stream])calls: list[tuple[str, dict]] = []monkeypatch.setattr(@@ -5376,12 +4584,10 @@ def test_gguf_long_tool_name_split_rehearsal_is_not_capped_and_executes(monkeypalambda n, a, **_k: calls.append((n, a)) or "result",)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "go"}],- tools = [{"type": "function", "function": {"name": name}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "go"}],+ [{"type": "function", "function": {"name": name}}],)assert calls == [(name, {"x": 1})], calls@@ -5410,12 +4616,10 @@ def test_gguf_streaming_keeps_bare_args_before_think_block(monkeypatch):lambda name, arguments, **_k: calls.append((name, arguments)) or "result",)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "x"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "x"}],+ [{"type": "function", "function": {"name": "web_search"}}],)assert calls == [], calls@@ -5442,12 +4646,11 @@ def test_gguf_inactive_name_args_in_prose_is_not_drained(monkeypatch):lambda name, arguments, **_k: calls.append((name, arguments)) or "result",)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "x"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 2,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "x"}],+ [{"type": "function", "function": {"name": "web_search"}}],+ max_tool_iterations = 2,)# No tool executed for the inactive name; a spurious no-op re-prompt would exhaust the@@ -5476,12 +4679,10 @@ def test_gguf_inactive_rehearsal_before_active_call_executes_and_keeps_prose(monlambda name, arguments, **_k: calls.append((name, arguments)) or "result",)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "search cats"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "search cats"}],+ [{"type": "function", "function": {"name": "web_search"}}],)# The real call runs; ``foo`` is not executed as a phantom disabled call.@@ -5527,8 +4728,7 @@ def test_gguf_oversized_bare_json_not_leaked_and_executes(monkeypatch):first_stream = [_sse({"content": full[i : i + 2000]}) for i in range(0, len(full), 2000)]first_stream.append(_done())final_stream = [_sse({"content": "done"}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, final_stream])calls: list[tuple[str, dict]] = []monkeypatch.setattr(@@ -5536,12 +4736,10 @@ def test_gguf_oversized_bare_json_not_leaked_and_executes(monkeypatch):lambda name, arguments, **_k: calls.append((name, arguments)) or "OK",)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "run"}],- tools = [{"type": "function", "function": {"name": "python"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "run"}],+ [{"type": "function", "function": {"name": "python"}}],)content_texts = [e.get("text", "") for e in events if e.get("type") == "content"]@@ -5560,8 +4758,7 @@ def test_gguf_bare_json_call_not_replayed_in_next_turn_content(monkeypatch):_done(),]final_stream = [_sse({"content": "Found."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, final_stream])monkeypatch.setattr("core.inference.tools.execute_tool", lambda *_a, **_k: "RESULT")@@ -5591,8 +4788,7 @@ def test_gguf_textual_fallback_caps_distinct_tool_calls_per_turn(monkeypatch):)first_stream = [_sse({"content": blocks}), _done()]final_stream = [_sse({"content": "done"}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, final_stream])calls: list[tuple[str, dict]] = []monkeypatch.setattr(@@ -5618,8 +4814,7 @@ def test_gguf_textual_fallback_collapses_duplicate_tool_calls(monkeypatch):blocks = '<tool_call>{"name":"web_search","arguments":{"query":"cats"}}</tool_call>' * 5first_stream = [_sse({"content": blocks}), _done()]final_stream = [_sse({"content": "done"}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, final_stream])calls: list[tuple[str, dict]] = []monkeypatch.setattr(@@ -5651,13 +4846,11 @@ def test_gguf_drain_truncated_enabled_name_json_preserved_when_auto_heal_disable"core.inference.tools.execute_tool",lambda name, arguments, **_k: calls.append((name, arguments)) or "result",)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "x"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 1,- auto_heal_tool_calls = auto_heal,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "x"}],+ [{"type": "function", "function": {"name": "web_search"}}],+ auto_heal_tool_calls = auto_heal,)contents = "".join(e.get("text", "") for e in events if e.get("type") == "content")return calls, contents@@ -5678,8 +4871,7 @@ def test_gguf_valid_tool_calls_respect_max_tool_iterations(monkeypatch):streams = [_structured_tool_call("web_search", {"query": f"q{i}"}, f"call_{i}") for i in range(6)]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, streams)calls: list[tuple[str, dict]] = []monkeypatch.setattr(@@ -5757,8 +4949,7 @@ def test_structured_tool_args_stream_to_provisional_card(monkeypatch):_done(),]second_stream = [_sse({"content": "Done."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, second_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, second_stream])executed: list[tuple[str, dict]] = []@@ -5768,13 +4959,7 @@ def test_structured_tool_args_stream_to_provisional_card(monkeypatch):monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "run it"}],- tools = _python_tool_schema(),- max_tool_iterations = 1,- )- )+ events = _run_tool_loop(backend, [{"role": "user", "content": "run it"}], _python_tool_schema())starts = [e for e in events if e.get("type") == "tool_start"]assert starts and starts[0]["tool_call_id"] == call_id@@ -5806,8 +4991,7 @@ def test_text_tool_call_streams_args_and_reconciles_card(monkeypatch):chunks = [call_text[i : i + 48] for i in range(0, len(call_text), 48)]first_stream = [_sse({"content": chunk}) for chunk in chunks] + [_done()]second_stream = [_sse({"content": "Done."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, second_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, second_stream])executed: list[tuple[str, dict]] = []@@ -5817,13 +5001,7 @@ def test_text_tool_call_streams_args_and_reconciles_card(monkeypatch):monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "run it"}],- tools = _python_tool_schema(),- max_tool_iterations = 1,- )- )+ events = _run_tool_loop(backend, [{"role": "user", "content": "run it"}], _python_tool_schema())starts = [e for e in events if e.get("type") == "tool_start"]assert starts, "no tool_start emitted"@@ -5851,15 +5029,10 @@ def test_ordinary_json_answer_streams_no_tool_args(monkeypatch):answer = json.dumps({"result": "fine", "data": ["x" * 40] * 12, "note": "not a tool call"})chunks = [answer[i : i + 64] for i in range(0, len(answer), 64)]stream = [_sse({"content": chunk}) for chunk in chunks] + [_done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [stream])- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "give me json"}],- tools = _python_tool_schema(),- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend, [{"role": "user", "content": "give me json"}], _python_tool_schema())assert not [e for e in events if e.get("type") == "tool_args"]@@ -5880,8 +5053,7 @@ def test_provisional_text_card_closed_when_parse_fails(monkeypatch):)chunks = [call_text[i : i + 48] for i in range(0, len(call_text), 48)]stream = [_sse({"content": chunk}) for chunk in chunks] + [_done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [stream])executed: list[tuple[str, dict]] = []@@ -5891,13 +5063,11 @@ def test_provisional_text_card_closed_when_parse_fails(monkeypatch):monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "run it"}],- tools = _python_tool_schema(),- max_tool_iterations = 1,- auto_heal_tool_calls = False,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "run it"}],+ _python_tool_schema(),+ auto_heal_tool_calls = False,)starts = [e for e in events if e.get("type") == "tool_start"]@@ -5927,12 +5097,10 @@ def test_provisional_mcp_card_carries_server_display_name(tmp_path, monkeypatch)backend = _make_backend(monkeypatch, [first_stream, final_stream], [])monkeypatch.setattr("core.inference.tools.execute_tool", lambda *_a, **_k: "OK")- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "file a bug"}],- tools = [{"type": "function", "function": {"name": tool_name}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "file a bug"}],+ [{"type": "function", "function": {"name": tool_name}}],)tool_starts = [e for e in events if e.get("type") == "tool_start"]@@ -5955,12 +5123,10 @@ def test_provisional_non_mcp_card_omits_mcp_server(tmp_path, monkeypatch):backend = _make_backend(monkeypatch, [first_stream, final_stream], [])monkeypatch.setattr("core.inference.tools.execute_tool", lambda *_a, **_k: "OK")- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "write code"}],- tools = [{"type": "function", "function": {"name": "python"}}],- max_tool_iterations = 1,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "write code"}],+ [{"type": "function", "function": {"name": "python"}}],)provisional = [e for e in events if e.get("type") == "tool_start" and not e.get("arguments")]assert len(provisional) == 1, events@@ -6013,8 +5179,7 @@ def test_second_structured_call_at_one_index_keeps_its_own_fragments(monkeypatch_done(),]final_stream = [_sse({"content": "Final answer."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [stream, final_stream])calls: list[dict] = []@@ -6024,12 +5189,11 @@ def test_second_structured_call_at_one_index_keeps_its_own_fragments(monkeypatchmonkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "search twice"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 2,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "search twice"}],+ [{"type": "function", "function": {"name": "web_search"}}],+ max_tool_iterations = 2,)assert calls == [@@ -6068,8 +5232,7 @@ def test_structured_fragment_naming_its_call_goes_back_to_that_call(monkeypatch)_done(),]final_stream = [_sse({"content": "Final answer."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [stream, final_stream])calls: list[dict] = []@@ -6079,12 +5242,11 @@ def test_structured_fragment_naming_its_call_goes_back_to_that_call(monkeypatch)monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "search twice"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 2,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "search twice"}],+ [{"type": "function", "function": {"name": "web_search"}}],+ max_tool_iterations = 2,)assert calls == [@@ -6123,8 +5285,7 @@ def test_structured_call_id_arriving_after_the_opening_delta_updates_that_call(m_done(),]final_stream = [_sse({"content": "Final answer."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [stream, final_stream])calls: list[dict] = []@@ -6134,12 +5295,11 @@ def test_structured_call_id_arriving_after_the_opening_delta_updates_that_call(mmonkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "search"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 2,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "search"}],+ [{"type": "function", "function": {"name": "web_search"}}],+ max_tool_iterations = 2,)assert calls == [{"name": "web_search", "arguments": {"query": "late"}}]@@ -6162,8 +5322,7 @@ def test_structured_call_forked_onto_a_reused_index_executes_last(monkeypatch):_done(),]final_stream = [_sse({"content": "Final answer."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [stream, final_stream])calls: list[dict] = []@@ -6173,12 +5332,11 @@ def test_structured_call_forked_onto_a_reused_index_executes_last(monkeypatch):monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "search three times"}],- tools = [{"type": "function", "function": {"name": "web_search"}}],- max_tool_iterations = 2,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "search three times"}],+ [{"type": "function", "function": {"name": "web_search"}}],+ max_tool_iterations = 2,)assert calls == [{"query": "a"}, {"query": "b"}, {"query": "c"}]@@ -6212,16 +5370,9 @@ def test_parallel_disabled_suppresses_provisional_for_reused_index(monkeypatch):_done(),]final_stream = [_sse({"content": "Done."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [stream, final_stream], payloads)-- calls: list[tuple[str, dict]] = []-- def fake_execute_tool(name, arguments, **_kwargs):- calls.append((name, arguments))- return "OK"+ backend, payloads = _backend_and_payloads(monkeypatch, [stream, final_stream])- monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)+ calls = _record_tool_calls(monkeypatch, "OK")events = list(backend.generate_chat_completion_with_tools(@@ -6354,8 +5505,7 @@ def test_a_long_tool_run_reports_a_boundary_in_the_requests_own_terms(monkeypatc)streams.append([_sse({"content": "done."}), _finish("stop"), _done()])- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, streams)backend._effective_context_length = 4000monkeypatch.setattr(backend,@@ -6379,14 +5529,13 @@ def test_a_long_tool_run_reports_a_boundary_in_the_requests_own_terms(monkeypatc{"role": "assistant", "content": "a2" * 600},{"role": "user", "content": "keep going"},]- events = list(- backend.generate_chat_completion_with_tools(- messages = branch,- tools = [{"type": "function", "function": {"name": "python"}}],- max_tokens = 400,- max_tool_iterations = calls + 1,- context_overflow = "truncate_oldest",- )+ events = _run_tool_loop(+ backend,+ branch,+ [{"type": "function", "function": {"name": "python"}}],+ max_tool_iterations = calls + 1,+ max_tokens = 400,+ context_overflow = "truncate_oldest",)notices = [@@ -6619,13 +5768,12 @@ def test_an_unservable_tool_call_is_refused_before_it_runs(monkeypatch):monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": immovable}],- tools = [{"type": "function", "function": {"name": "edit_file"}}],- max_tokens = 512,- max_tool_iterations = 2,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": immovable}],+ [{"type": "function", "function": {"name": "edit_file"}}],+ max_tool_iterations = 2,+ max_tokens = 512,)assert executed == [], "the side effect was spent on an unservable turn"@@ -6676,8 +5824,7 @@ def test_compacting_an_earlier_call_lets_the_next_one_run(monkeypatch):),[_sse({"content": "Fixed."}), _done()],]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, streams)monkeypatch.setattr(backend, "count_chat_tokens", _count_from_size)executed: list[str] = []@@ -6690,13 +5837,12 @@ def test_compacting_an_earlier_call_lets_the_next_one_run(monkeypatch):monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)- events = list(- backend.generate_chat_completion_with_tools(- messages = history,- tools = [{"type": "function", "function": {"name": "edit_file"}}],- max_tokens = 512,- max_tool_iterations = 2,- )+ events = _run_tool_loop(+ backend,+ history,+ [{"type": "function", "function": {"name": "edit_file"}}],+ max_tool_iterations = 2,+ max_tokens = 512,)assert executed == ["edit_file"], [e.get("type") for e in events]@@ -6736,8 +5882,7 @@ def test_refusing_a_call_also_stops_it_costing_the_window(monkeypatch):),[_sse({"content": "I could not write that file."}), _done()],]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, streams)monkeypatch.setattr(backend, "count_chat_tokens", _count_from_size)executed: list[str] = []@@ -6838,8 +5983,7 @@ def test_an_oversized_call_is_run_and_compacted_rather_than_refused(monkeypatch)),[_sse({"content": "Wrote the game."}), _done()],]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, streams, payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, streams)monkeypatch.setattr(backend, "count_chat_tokens", _count_from_size)executed: list[str] = []@@ -6852,13 +5996,12 @@ def test_an_oversized_call_is_run_and_compacted_rather_than_refused(monkeypatch)monkeypatch.setattr("core.inference.tools.execute_tool", fake_execute_tool)- events = list(- backend.generate_chat_completion_with_tools(- messages = [{"role": "user", "content": "Create a Flappy Bird game in HTML"}],- tools = [{"type": "function", "function": {"name": "edit_file"}}],- max_tokens = 512,- max_tool_iterations = 2,- )+ events = _run_tool_loop(+ backend,+ [{"role": "user", "content": "Create a Flappy Bird game in HTML"}],+ [{"type": "function", "function": {"name": "edit_file"}}],+ max_tool_iterations = 2,+ max_tokens = 512,)assert executed == ["edit_file"], "the call was refused instead of run"@@ -6981,8 +6124,7 @@ def test_the_synthesized_final_pass_is_recosted_before_it_is_sent(monkeypatch):"""first_stream = _structured_tool_call("web_search", {"query": "kernel"}, "call_search")final_stream = [_sse({"content": "6.10."}), _done()]- payloads: list[dict] = []- backend = _make_backend(monkeypatch, [first_stream, final_stream], payloads)+ backend, payloads = _backend_and_payloads(monkeypatch, [first_stream, final_stream])monkeypatch.setattr("core.inference.tools.execute_tool",# Long enough that skipping it is a real under-count, not a rounding error.diff --git a/studio/backend/tests/test_llama_cpp_update.py b/studio/backend/tests/test_llama_cpp_update.pyindex d2860700ede..45440319751 100644--- a/studio/backend/tests/test_llama_cpp_update.py+++ b/studio/backend/tests/test_llama_cpp_update.py@@ -28,6 +28,13 @@ import utils.llama_cpp_freshness as freshness # noqa: E402import utils.llama_cpp_update as upd # noqa: E402import utils.process_lifetime as process_lifetime # noqa: E402++class _Proc:+ returncode = 0+ stdout = "installed"+ stderr = ""++MARKER = "UNSLOTH_PREBUILT_INFO.json"@@ -421,11 +428,6 @@ def test_start_update_source_build_installs_prebuilt(monkeypatch, tmp_path):captured = {}- class _Proc:- returncode = 0- stdout = "installed"- stderr = ""-def _fake_run(cmd, **kwargs):cmd = list(cmd)assert "--version" in cmd # only status polls still use run()@@ -465,11 +467,6 @@ def test_start_update_happy_path(monkeypatch, tmp_path):captured = {}- class _Proc:- returncode = 0- stdout = "installed"- stderr = ""-def _on_start(cmd):captured["cmd"] = cmd# Simulate the installer writing a new marker with the latest tag.@@ -943,11 +940,6 @@ def _capture_install_cmd(captured = {}- class _Proc:- returncode = 0- stdout = "installed"- stderr = ""-def _fake_run(cmd, **kwargs):cmd = list(cmd)assert "--version" in cmd # only status polls still use run()diff --git a/studio/backend/tests/test_llama_cpp_wait_for_health.py b/studio/backend/tests/test_llama_cpp_wait_for_health.pyindex 4ae2433fe4b..7eb2493e36e 100644--- a/studio/backend/tests/test_llama_cpp_wait_for_health.py+++ b/studio/backend/tests/test_llama_cpp_wait_for_health.py@@ -33,6 +33,8 @@ sys.modules.setdefault("structlog", _types.ModuleType("structlog"))import httpx # noqa: E402from core.inference.llama_cpp import LlamaCppBackend # noqa: E402+from core.inference.llama_cpp import GgufLoadIntent+import subprocess# Sibling tests install lightweight httpx stubs, so when collected together our `httpx`# may be a stub lacking `get`. Fill in the gaps so collection order does not matter.@@ -479,8 +481,6 @@ class TestCancelledWaitEndsTheLoad:raises a 500 at the caller before it can run its own cancellation handling."""def test_a_cancelled_health_wait_returns_false_instead_of_raising(self, tmp_path):- from core.inference.llama_cpp import GgufLoadIntent-gguf = tmp_path / "model.gguf"gguf.write_bytes(b"GGUF" + b"\0" * 4096)@@ -528,10 +528,6 @@ class TestCancelledWaitEndsTheLoad:def test_a_teardown_mid_load_ends_the_load_instead_of_raising(self, tmp_path, monkeypatch):"""The whole race through the real caller (#10353): the waiter tests abovenever reach _spawn_and_wait, which is where the traceback resurfaced."""- import subprocess-- from core.inference.llama_cpp import GgufLoadIntent-gguf = tmp_path / "model.gguf"gguf.write_bytes(b"GGUF" + b"\0" * 4096)@@ -586,8 +582,6 @@ class TestCancelledWaitEndsTheLoad:def test_a_cancelled_diffusion_start_reaps_the_runner():"""An automatic switch cancels without unloading, so nothing else reaps the shimand the visual server; they would keep loading and holding memory."""- import subprocess-b = LlamaCppBackend()kills = []b._kill_process = lambda *a, **kw: kills.append(1)@@ -625,8 +619,6 @@ def test_a_cancelled_diffusion_start_reaps_the_runner():def test_a_diffusion_cancel_after_health_reaps_the_runner():- import subprocess-b = LlamaCppBackend()kills = []b._kill_process = lambda *a, **kw: kills.append(1)@@ -701,8 +693,6 @@ def test_combined_download_cancel_wait_observes_the_scoped_event():@pytest.mark.parametrize("cancel_source", ["shared", "scoped"])def test_remote_download_observes_both_load_cancel_sources(tmp_path, cancel_source):- from core.inference.llama_cpp import GgufLoadIntent-kills = []b, gguf = _cancel_scaffold(tmp_path, kills)b._remote_non_chat_gguf_refusal = lambda **_kw: None@@ -736,8 +726,6 @@ def test_a_stale_cancel_marker_does_not_abort_the_next_load(tmp_path):"""The marker belongs to one attempt. Left set, a guard that runs before thisload's first health wait reads the previous request's cancellation and swallowsa genuine start failure."""- from core.inference.llama_cpp import GgufLoadIntent-kills = []b, gguf = _cancel_scaffold(tmp_path, kills)b._health_wait_cancelled = True # left over from an earlier cancelled load@@ -755,8 +743,6 @@ def test_a_stale_cancel_marker_does_not_abort_the_next_load(tmp_path):def test_cancelled_health_wait_removes_the_staged_cpu_runtime(tmp_path):- from core.inference.llama_cpp import GgufLoadIntent-kills = []cleaned = []waits = []@@ -798,8 +784,6 @@ def test_cancelled_health_wait_removes_the_staged_cpu_runtime(tmp_path):def test_a_cancel_after_the_server_is_healthy_does_not_publish_it(tmp_path):"""Cancelling during the post-health setup used to leave the child resident whilethe cancel route saw is_loaded and skipped teardown."""- from core.inference.llama_cpp import GgufLoadIntent-kills = []cleaned = []b, gguf = _cancel_scaffold(tmp_path, kills)@@ -832,8 +816,6 @@ def test_a_cancel_after_the_server_is_healthy_does_not_publish_it(tmp_path):def test_a_cancel_after_audio_setup_unloads_the_codec(tmp_path, monkeypatch):- from core.inference.llama_cpp import GgufLoadIntent-kills = []emptied = []b, gguf = _cancel_scaffold(tmp_path, kills)diff --git a/studio/backend/tests/test_llama_cpp_wait_for_vram_settle.py b/studio/backend/tests/test_llama_cpp_wait_for_vram_settle.pyindex ddadf418aa2..2823f64e4a7 100644--- a/studio/backend/tests/test_llama_cpp_wait_for_vram_settle.py+++ b/studio/backend/tests/test_llama_cpp_wait_for_vram_settle.py@@ -98,6 +98,8 @@ except ImportError:from core.inference import llama_cpp as llama_cpp_module # noqa: E402from core.inference.llama_cpp import LlamaCppBackend # noqa: E402+import os+import subprocess# ---------------------------------------------------------------------------@@ -391,8 +393,6 @@ _NO_PROCFS = "/unsloth-test-no-such-proc-root"def test_kill_orphaned_servers_returns_count():"""The reaper reports how many owned orphans it killed, so __init__ canarm the settle wait. Only Unsloth-owned llama-server procs count."""- import os-mypid = os.getpid()fake_path = "/tmp/unsloth-test-llama/llama-server"killed: list[int] = []@@ -441,8 +441,6 @@ def test_kill_orphaned_servers_spares_live_parent():"""An Unsloth-owned llama-server whose parent is still running is not anorphan (a live Unsloth or the user's shell owns it) and must never bekilled; only the true orphan (parent gone) is reaped."""- import os-mypid = os.getpid()fake_path = "/tmp/unsloth-test-llama/llama-server"killed: list[int] = []@@ -541,8 +539,6 @@ def test_kill_process_clears_pidfile(tmp_path):def test_reap_recorded_pid_kills_recorded_server(tmp_path):"""An orphaned recorded PID (parent gone) is killed and the pidfile clearedwhen it is still a llama-server."""- import subprocess-proc = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(30)"])pidfile = tmp_path / "llama-server.pid"pidfile.write_text(str(proc.pid))@@ -570,8 +566,6 @@ def test_reap_recorded_pid_kills_recorded_server(tmp_path):def test_record_then_reap_round_trip_identity_matches(tmp_path):"""Full round trip: _record_server_pid writes pid:starttime, and an orphanedreap whose recorded identity still matches DOES kill it."""- import subprocess-proc = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(30)"])pidfile = tmp_path / "llama-server.pid"try:@@ -599,8 +593,6 @@ def test_reap_recorded_pid_spares_live_server(tmp_path):reaped, and its pidfile is kept. This is the finding-3 guard: a helper backendconstructed in-process must not kill the active chat server. Uses the REAL_pid_parent_is_alive (the child's parent is this live test process)."""- import subprocess-proc = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(30)"])pidfile = tmp_path / "llama-server.pid"pidfile.write_text(str(proc.pid))@@ -622,8 +614,6 @@ def test_reap_recorded_pid_spares_live_server(tmp_path):def test_reap_recorded_pid_skips_pid_reuse(tmp_path):"""A recorded PID recycled to a non-llama-server must NOT be killed (only thestale pidfile is cleaned), so the user's vllm/games are never touched."""- import subprocess-proc = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(30)"])pidfile = tmp_path / "llama-server.pid"pidfile.write_text(str(proc.pid))@@ -645,8 +635,6 @@ def test_reap_recorded_pid_skips_pid_reuse(tmp_path):def test_reap_recorded_pid_skips_identity_mismatch(tmp_path):"""An orphaned PID whose recorded start-time identity no longer matches has beenrecycled; it must NOT be killed even if it now looks like a llama-server."""- import subprocess-proc = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(30)"])pidfile = tmp_path / "llama-server.pid"pidfile.write_text(f"{proc.pid}:0.0") # stale identity that cannot match@@ -728,8 +716,6 @@ def _write_fake_procfs(tmp_path, entries):def test_kill_orphaned_servers_procfs_matches_psutil_selection(tmp_path):"""The Linux /proc sweep must select exactly what the psutil sweep selects:the Unsloth-owned orphan, never a foreign llama-server or another program."""- import os-mypid = os.getpid()owned_dir = tmp_path / "unsloth-test-llama"owned_dir.mkdir()@@ -769,8 +755,6 @@ def test_kill_orphaned_servers_procfs_matches_psutil_selection(tmp_path):@pytest.mark.skipif(sys.platform != "linux", reason = "the procfs scan only runs on Linux")def test_kill_orphaned_servers_procfs_spares_live_parent(tmp_path):"""Same live-parent rule as the psutil sweep: only the true orphan is reaped."""- import os-mypid = os.getpid()owned_dir = tmp_path / "unsloth-test-llama"owned_dir.mkdir()@@ -809,8 +793,6 @@ def test_kill_orphaned_servers_procfs_handles_a_deleted_binary(tmp_path):"""An orphan left behind by an upgrade has " (deleted)" appended to its exelink. psutil strips that marker, so the procfs sweep must too, or theorphan stops being recognised as ours."""- import os-mypid = os.getpid()owned_dir = tmp_path / "unsloth-test-llama"owned_dir.mkdir()@@ -840,8 +822,6 @@ def test_kill_orphaned_servers_procfs_refuses_a_reused_pid(tmp_path):"""psutil.Process.kill() refuses to signal a PID that has been reused. Theprocfs sweep must do the same, or an orphan that exits between the scan andthe signal takes an unrelated replacement process with it."""- import os-mypid = os.getpid()owned_dir = tmp_path / "unsloth-test-llama"owned_dir.mkdir()@@ -880,8 +860,6 @@ def test_kill_orphaned_servers_procfs_refuses_a_reused_pid(tmp_path):def test_kill_orphaned_servers_procfs_still_kills_the_same_process(tmp_path):"""Control for the test above: an unchanged starttime is still reaped, sothe identity check is not simply refusing everything."""- import os-mypid = os.getpid()owned_dir = tmp_path / "unsloth-test-llama"owned_dir.mkdir()diff --git a/studio/backend/tests/test_llama_extra_args_compatibility.py b/studio/backend/tests/test_llama_extra_args_compatibility.pyindex 665654dd598..0f37b799c12 100644--- a/studio/backend/tests/test_llama_extra_args_compatibility.py+++ b/studio/backend/tests/test_llama_extra_args_compatibility.py@@ -20,6 +20,12 @@ from pathlib import Pathimport pytest++class _Config:+ is_gguf = True+ gguf_variant = ""++_BACKEND = Path(__file__).resolve().parent.parent_LSA_PATH = _BACKEND / "core" / "inference" / "llama_server_args.py"_spec = importlib.util.spec_from_file_location("_lsa_compat_test", _LSA_PATH)@@ -149,10 +155,6 @@ def test_the_inherited_load_path_drops_only_the_denied_flag(monkeypatch):# reaches the drop and the test proves nothing.extra_args_source = ("local/x", "")- class _Config:- is_gguf = True- gguf_variant = ""-class _Request:llama_extra_args = Nonegguf_variant = ""@@ -175,10 +177,6 @@ def _inherit_with_ctx_flag(monkeypatch, stored, fields_set, max_seq_length):extra_args = list(stored)extra_args_source = ("local/x", "")- class _Config:- is_gguf = True- gguf_variant = ""-class _Request:llama_extra_args = Nonegguf_variant = ""@@ -326,10 +324,10 @@ def test_validate_sizes_itself_with_the_arguments_the_caller_sent():# --ctx-size in the extras changes that estimate. The resolver hands back its# fourth argument unchanged for an explicit list, so passing None there meant the# preflight approved a different command from the one that runs.- import inspect-import routes.inference as inference_route+ import inspect+source = inspect.getsource(inference_route)assert ("_resolve_inherited_extra_args(\n request, config, model_identifier, None\n )"@@ -343,10 +341,6 @@ def test_validate_sizes_itself_with_the_arguments_the_caller_sent():class _Request:llama_extra_args = ["--ctx-size", "8192"]- class _Config:- is_gguf = True- gguf_variant = ""-# The helper's own contract: an explicit list is returned as given.assert inference_route._resolve_inherited_extra_args(_Request(), _Config(), "local/x", _Request.llama_extra_argsdiff --git a/studio/backend/tests/test_llama_flag_catalog.py b/studio/backend/tests/test_llama_flag_catalog.pyindex 72631742a30..e1703b08d30 100644--- a/studio/backend/tests/test_llama_flag_catalog.py+++ b/studio/backend/tests/test_llama_flag_catalog.py@@ -208,7 +208,6 @@ def test_the_denylist_can_be_read_without_probing(monkeypatch):# The panel sanitizes a stored list with this before turning it into an explicit# request, and a cold --help takes up to ten seconds. Waiting for the probe would# leave a flag denied since that list was saved sitting in the request.- import asyncioimport routes.inference as inference_route@@ -235,6 +234,7 @@ def test_the_published_slot_default_is_the_effective_one(monkeypatch):# A build without --kv-unified serves one slot however many are configured, and# load_model clamps to that before launch. An editor sizing its batch floor from# the raw default would refuse "--batch-size 2" against a command that runs it.+import routes.inference as inference_routemonkeypatch.setattr(@@ -258,6 +258,7 @@ def test_the_published_slot_default_is_the_effective_one(monkeypatch):def test_an_unreadable_probe_keeps_the_asked_for_slot_count(monkeypatch):# Refusing to answer is not a reason to clamp: every other caller of the probe# here keeps the ask when it cannot be read.+import routes.inference as inference_routedef _boom(*_a, **_k):@@ -276,11 +277,10 @@ def test_the_slot_probe_never_runs_on_the_event_loop(monkeypatch):# that is `llama-server --help` with a ten second timeout. Computed inline it# stalled every other request on the first open of the panel after an update, and# the managed-only answer too, which exists precisely to avoid waiting for a probe.- import asyncio- import threading-import routes.inference as inference_route+ import threading+loop_thread = Noneprobe_thread = None@@ -311,11 +311,10 @@ def test_a_single_slot_default_still_reports_the_clamp(monkeypatch):# this build clamps is a different question and still has to be answered: the# editor sizes an EXPLICIT Slots value the user may raise without re-reading# this route, and off the loop like the rest of it.- import asyncio- import threading-import routes.inference as inference_route+ import threading+loop_thread = Noneprobe_thread = Nonediff --git a/studio/backend/tests/test_llama_route_timeouts.py b/studio/backend/tests/test_llama_route_timeouts.pyindex b4666e3d182..10da8248278 100644--- a/studio/backend/tests/test_llama_route_timeouts.py+++ b/studio/backend/tests/test_llama_route_timeouts.py@@ -14,6 +14,11 @@ sys.path.insert(0, _backend)import routes.inference as inf_mod # noqa: E402+class _Request:+ async def is_disconnected(self):+ return False++def test_non_streaming_generation_timeout_has_read_deadline():timeout = inf_mod._llama_non_streaming_generation_timeout()assert timeout.read == inf_mod._DEFAULT_FIRST_TOKEN_TIMEOUT_S@@ -134,10 +139,6 @@ def test_stream_wait_does_not_shorten_upstream_read_for_disconnect_poll():response = SimpleNamespace(request = SimpleNamespace(extensions = {"timeout": {}}))seen_read_timeouts = []- class _Request:- async def is_disconnected(self):- return False-class _NoItem:async def __anext__(self):seen_read_timeouts.append(response.request.extensions["timeout"]["read"])@@ -214,10 +215,6 @@ def test_stream_stall_timeout_callable_re_resolved_each_read():values = iter([100.0, 2.0])seen = []- class _Request:- async def is_disconnected(self):- return False-class _Items:def __init__(self):self.count = 0@@ -258,10 +255,6 @@ def test_stream_stall_timeout_disabled_clears_read_timeout():response = SimpleNamespace(request = SimpleNamespace(extensions = {"timeout": {}}))seen = []- class _Request:- async def is_disconnected(self):- return False-class _Items:def __init__(self):self.count = 0diff --git a/studio/backend/tests/test_load_mode_fit_matrix.py b/studio/backend/tests/test_load_mode_fit_matrix.pyindex 21d89d4d633..d6ff8af2b39 100644--- a/studio/backend/tests/test_load_mode_fit_matrix.py+++ b/studio/backend/tests/test_load_mode_fit_matrix.py@@ -201,9 +201,8 @@ def test_the_launch_charges_the_pipeline_overhead_per_extra_device():"""max(0, n - 1), so a single-GPU load adds nothing, and ungated by whetherllama.cpp keeps the pipeline -- the per-device context is there either way,which is why the placement's own term is ungated too."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend as B+ import inspectcompact = "".join(inspect.getsource(B.load_model).split())assert "pipeline_overhead_bytes=(max(0,_fit_devices-1)*_pipeline_overhead_bytes)" in compact@@ -307,9 +306,8 @@ def test_the_launch_charges_the_cpu_pinned_drafter_to_the_fit():"""The budget nulls the drafter path before its weights are sized, so thefit has to keep its own copy. Checked at the source, like the other launchordering invariants here: the call site sits inside load_model's fit try."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend as B+ import inspectcompact = "".join(inspect.getsource(B.load_model).split())# Captured BEFORE the VRAM budget drops it.@@ -331,9 +329,8 @@ def test_a_weights_only_drafter_reserve_counts_as_unsized():that as a sized drafter understates a ctx-linear term the placement only coverswith a flat cushion the footprint has no room for. Re-narrowing to"mtp_overhead_fn is None" absorbs the arm away, which is what it used to do."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend as B+ import inspectcompact = "".join(inspect.getsource(B.load_model).split())assert (@@ -460,9 +457,8 @@ def test_the_fit_and_the_flag_read_the_same_margin(auto_fit, delta, supports, exdef test_the_launch_charges_the_fitters_margin_only_when_fit_stays_on():"""Checked at the source: reaching this call needs a real GPU probe."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend as B+ import inspectcompact = "".join(inspect.getsource(B.load_model).split())call = compact[compact.index("_fit_margin_mib=(") :]@@ -487,9 +483,9 @@ def test_the_effective_fitter_state_reads_the_launchs_own_fit_flag():`-ngl -1` is its own default, which the fitter is free to lower (common/fit.cppaborts only on a count the user really set). So the margin has to be charged."""+ from core.inference.llama_cpp import LlamaCppBackend as Bimport inspect- from core.inference.llama_cpp import LlamaCppBackend as Bfrom core.inference.llama_server_args import fit_is_effectively_oncompact = "".join(inspect.getsource(B.load_model).split())@@ -551,10 +547,9 @@ def test_a_pass_through_fit_target_is_the_margin_the_child_really_keeps(extras,@pytest.fixturedef toggles(monkeypatch):"""Drive the Model Memory settings the two policies read lazily."""+ import utils.model_memory_settings as mmdef _set(keep_resident, no_ram_reserve):- import utils.model_memory_settings as mm-monkeypatch.setattr(mm, "get_model_memory_settings", lambda: (keep_resident, no_ram_reserve))@@ -708,9 +703,8 @@ def test_a_hand_typed_flag_still_wins_by_last_arg(toggles):def test_the_cpu_fallback_drops_the_fits_load_mode(monkeypatch):"""A CPU replay runs on no GPU, so the VRAM half of the fit is void and thewhole model has to come out of host RAM. mmap is what makes that survivable."""- from unittest import mock-from core.inference import llama_cpp as lc+ from unittest import mockbackend = LlamaCppBackend.__new__(LlamaCppBackend)backend._fit_load_mode_flags = ["--load-mode", "none"]@@ -744,9 +738,8 @@ def test_the_cpu_fallback_drops_the_fits_load_mode(monkeypatch):def test_the_cpu_fallback_keeps_a_load_mode_the_user_asked_for(monkeypatch):"""Only Unsloth's own tokens are recorded, so a user's pick survives."""- from unittest import mock-from core.inference import llama_cpp as lc+ from unittest import mockbackend = LlamaCppBackend.__new__(LlamaCppBackend)backend._fit_load_mode_flags = [] # user pick: nothing recorded@@ -783,9 +776,8 @@ def test_the_fit_on_retry_drops_the_fits_load_mode():from that fit cannot ride along. Checked at the source, like the otherordering invariants in this launch path, because the retry only runs behind areal startup crash."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend as B+ import inspectsrc = inspect.getsource(B.load_model)retry = src[src.index("retrying once with --fit on so it can offload") :]@@ -827,9 +819,8 @@ def test_the_arch_crash_retry_drops_the_fits_load_mode():narrowing), so the mode that fit concluded cannot ride along. Checked at thesource, like the --fit on retry above, because this arm only runs behind a realkernel-image crash."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend as B+ import inspectsrc = inspect.getsource(B.load_model)retry = src[src.index("the llama.cpp build has no kernels") :]@@ -850,9 +841,8 @@ def test_the_no_flash_retry_drops_the_fits_load_mode():(gated on fully_gpu_offloaded) is not a second net. Checked at the source, likethe retries above, because this arm only runs behind a real signal crash."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend as B+ import inspectsrc = inspect.getsource(B.load_model)# Both sites, each bounded at its own respawn so the strip is proved to happen@@ -993,9 +983,8 @@ def test_the_launch_hands_the_fit_the_extras_the_child_will_get():"""The predicate is only worth anything if the call site feeds it. Checked atthe source, like the fallback ordering tests above, because reaching this callneeds a real GPU probe."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend as B+ import inspectsrc = inspect.getsource(B.load_model)call = src[src.index("_fit_extras = (") :]@@ -1498,9 +1487,8 @@ def test_a_tensor_split_charges_the_replicated_compute_buffer():and understating it on a pooled multi-GPU credit is the direction that claims afit that is not there."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend as B+ import inspectcompact = "".join(inspect.getsource(B.load_model).split())# The tensor-mode rate is measured alongside the layer one...@@ -1599,6 +1587,7 @@ def test_the_draft_whole_offload_threshold_matches_the_main_model():VRAM for a block that never reaches the card."""from core.inference.llama_cpp import LlamaCppBackend as B+from core.inference.llama_cpp import _draft_is_split_across_host# The boundary itself, from both spellings and the env twin.@@ -1627,9 +1616,8 @@ def test_a_partial_draft_offload_leaves_the_drafter_unsized():in host RAM. Nothing at the call site can say which slice stays on the card, soit has to abstain rather than credit VRAM for bytes that never reach it."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend as B+ import inspectcompact = "".join(inspect.getsource(B.load_model).split())assert (@@ -1672,9 +1660,8 @@ def test_the_cpu_projector_retry_drops_the_fits_load_mode():"""The projector respawn changes placement, so the fit's conclusion goes withit, like the --fit on, arch-crash and no-flash retries. Checked at the source,like those, because this arm only runs behind a real projector startup crash."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend as B+ import inspectsrc = inspect.getsource(B.load_model)# Bounded at the respawn, so the strip is proved to happen BEFORE it.@@ -1739,9 +1726,8 @@ def test_context_checkpoint_snapshots_move_the_fit_verdict(monkeypatch):def test_the_fit_prices_the_effective_checkpoint_count():"""...and the call site really passes it, while the closure default leaves theplacement paths -- which price the snapshots by their own route -- unmoved."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend as B+ import inspectcompact = "".join(inspect.getsource(B.load_model).split())assert "kv_cache_bytes=_kv_bytes(effective_ctx,_effective_ctx_checkpoints)," in compact@@ -1753,9 +1739,8 @@ def test_an_inherited_loader_mode_wins_over_the_fits_pick():beat an operator's inherited choice silently. The fit's mode stands aside forit, the way it stands aside for the per-model pick; a per-model pick stillwins, and so does a hand-typed flag, by last-arg."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend as B+ import inspectcompact = "".join(inspect.getsource(B.load_model).split())arm = compact[@@ -1796,9 +1781,8 @@ def test_the_fit_prices_a_projector_inherited_through_the_environment():through LLAMA_ARG_MMPROJ is resident but uncharged. Sized when it can be, andabstaining when it cannot: a URL names a download that has not happened, and anunreadable path cannot be sized."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend as B+ import inspectcompact = "".join(inspect.getsource(B.load_model).split())assert 'else(_fit_env.get("LLAMA_ARG_MMPROJ")or"").strip()' in compact@@ -1875,9 +1859,8 @@ def test_the_projector_allowance_flips_a_fit_that_only_looked_like_one(monkeypatdef test_the_launch_wires_the_projector_allowance_into_the_fit():"""Reachability only: driving load_model this far needs a real GPU probe."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend as B+ import inspectcompact = "".join(inspect.getsource(B.load_model).split())assert "_inherited_mmproj_soft_overhead" in compact@@ -2043,10 +2026,10 @@ def test_a_chained_retry_cannot_eat_the_users_own_load_mode():user's. The fit standing aside when the extras pick a mode is what keeps thestrips the no-ops their docstrings claim to be."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend as Bfrom core.inference.llama_cpp import _without_subsequence+ import inspect+from core.inference.llama_server_args import (apply_load_mode_policy,extra_args_select_load_mode,@@ -2113,8 +2096,9 @@ def test_a_stripped_load_mode_changes_what_the_reload_predicate_answers(monkeypareserve system RAM" that is a full model reload the running server alreadysatisfies."""- import utils.model_memory_settings as mmfrom core.inference.llama_cpp import _without_subsequence+ import utils.model_memory_settings as mm+from core.inference.llama_server_args import (memory_state_satisfies_settings,resolve_effective_memory_state,@@ -2146,6 +2130,7 @@ def test_the_recompute_reads_the_argv_not_the_managed_block():would read as satisfied while the reservation stands."""from core.inference.llama_cpp import _without_subsequence+from core.inference.llama_server_args import resolve_effective_memory_stateretried = ["llama-server", "-m", "m.gguf", "--load-mode", FIT_MODE, "--load-mode", "mmap+mlock"]@@ -2159,9 +2144,8 @@ def test_the_no_flash_rung_recomputes_the_memory_record():"""Reachability at the source, like the strip it sits next to: the arm onlyruns behind a real signal crash. Whitespace stripped, so a reformat thatwraps the call cannot break the pin."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend as B+ import inspectsrc = inspect.getsource(B.load_model)helper = src[src.index("_drop_fit_load_mode_for_no_flash") :]@@ -2175,9 +2159,8 @@ def test_the_no_flash_rung_recomputes_the_memory_record():def test_the_cpu_projector_rung_recomputes_the_memory_record():"""Same, for the projector respawn."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend as B+ import inspectsrc = inspect.getsource(B.load_model)arm = src[: src.index('"-mmproj-cpu"')]@@ -2196,9 +2179,9 @@ def test_the_arch_crash_rung_records_from_the_argv_not_the_parts():drops whatever those paths added, and dropping a page-lock records anunlocked child that is in fact locked -- the optimistic direction."""+ from core.inference.llama_cpp import LlamaCppBackend as Bimport inspect- from core.inference.llama_cpp import LlamaCppBackend as Bfrom core.inference.llama_server_args import resolve_effective_memory_state# The two inputs genuinely disagree, so which one is used is observable.@@ -2225,10 +2208,10 @@ def test_the_cpu_replay_and_the_launch_record_disagree(monkeypatch):"Don't reserve system RAM" that stale record is a full model reload the CPUserver already satisfies."""+ from core.inference import llama_cpp as lcfrom unittest import mock-import utils.model_memory_settings as mm- from core.inference import llama_cpp as lc+from core.inference.llama_server_args import (memory_state_satisfies_settings,resolve_effective_memory_state,@@ -2270,9 +2253,8 @@ def test_the_crash_path_cpu_fallback_recomputes_the_memory_record():argv that really started, so a page-lock _spawn_and_wait's own --fit retryappended is kept rather than recorded away."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend as B+ import inspect# Whitespace stripped before slicing, so a reformat that rewraps any of it# cannot break the pin.@@ -2287,9 +2269,8 @@ def test_the_crash_path_cpu_fallback_recomputes_the_memory_record():def test_the_replayed_cpu_fallback_recomputes_the_memory_record():"""Same, for a request that carries cpu_fallback and rebuilds the replaybefore anything spawns."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend as B+ import inspectsrc = "".join(inspect.getsource(B.load_model).split())# allow_manual_cpu is what distinguishes this eligibility check from thediff --git a/studio/backend/tests/test_load_subdirs_stay_offline.py b/studio/backend/tests/test_load_subdirs_stay_offline.pyindex 2549233aec0..3ca62168273 100644--- a/studio/backend/tests/test_load_subdirs_stay_offline.py+++ b/studio/backend/tests/test_load_subdirs_stay_offline.py@@ -27,10 +27,10 @@ _PLAIN = "unsloth/Llama-3.2-1B-Instruct"@pytest.fixturedef detector_spy(monkeypatch):"""Record how detect_audio_type is called, without touching the network."""- calls = []-import utils.models.model_config as model_config+ calls = []+def fake_detect(model_name,hf_token = None,diff --git a/studio/backend/tests/test_local_model_format.py b/studio/backend/tests/test_local_model_format.pyindex 5cdb5508e3e..4ebf1c7d8fb 100644--- a/studio/backend/tests/test_local_model_format.py+++ b/studio/backend/tests/test_local_model_format.py@@ -530,6 +530,7 @@ def test_scan_models_dir_root_weights_do_not_hide_child_models(tmp_path):# ── Images picker task tag for local (non-GGUF) diffusers models ──────────────from models.models import LocalModelInfo # noqa: E402+from hub.services.models import catalog_classification as classificationdef _local(@@ -578,8 +579,6 @@ def test_windows_cloud_recall_attributes_are_not_local():def test_local_gguf_task_reads_present_header(tmp_path, monkeypatch):"""Fully present files retain architecture-based task detection."""- from hub.services.models import catalog_classification as classification-gguf = _touch(tmp_path / "generic-Q4_K_M.gguf")reads = []monkeypatch.setattr(classification, "file_contents_available_locally", lambda _path: True)@@ -602,7 +601,6 @@ def test_local_gguf_task_reads_present_header(tmp_path, monkeypatch):def test_local_gguf_task_skips_online_only_contents(tmp_path, monkeypatch):"""Cloud placeholders stay discoverable by name without opening their data."""- from hub.services.models import catalog_classification as classificationdef forbidden(*_args, **_kwargs):raise AssertionError("local GGUF listing touched placeholder contents")@@ -628,7 +626,6 @@ def test_local_classification_never_opens_an_online_only_gguf(tmp_path, monkeypacomes back None, which for a placeholder is every time, and that probe reads anarchitecture of its own. Asserting on ``_local_model_task`` alone leaves the listinghydrating exactly the files it stopped classifying, a folder row once per sibling."""- from hub.services.models import catalog_classification as classificationfrom utils.models import gguf_metadatasingle = _touch(tmp_path / "single" / "generic-Q4_K_M.gguf")@@ -653,8 +650,6 @@ def test_local_classification_never_opens_an_online_only_gguf(tmp_path, monkeypadef test_local_classification_probes_the_hf_cache_snapshot(tmp_path):- from hub.services.models import catalog_classification as classification-repo = tmp_path / "models--unsloth--csm-1b"snapshot = repo / "snapshots" / "abc"snapshot.mkdir(parents = True)@@ -703,7 +698,6 @@ def test_an_unhydrated_denoiser_keeps_the_picker_that_would_hydrate_it(tmp_path,"""Images and Video filter On Device rows on an exact task, so an unclassified denoiseris not reachable from the one page whose pick would pull it down, and lists in Chatinstead. The filename carries the family, and it is read without opening the file."""- from hub.services.models import catalog_classification as classificationdef forbidden(*_args, **_kwargs):raise AssertionError("placeholder contents were read to classify it")@@ -731,7 +725,6 @@ def test_an_ancestor_directory_does_not_name_an_unhydrated_gguf(tmp_path, monkeysegment of it. With an architecture that mismatch only picks the wrong family; for aplaceholder the name is the entire case, so a shelf named after a family would file everychat GGUF stored under it as an image or video model."""- from hub.services.models import catalog_classification as classificationdef forbidden(*_args, **_kwargs):raise AssertionError("placeholder contents were read to classify it")@@ -941,7 +934,6 @@ def test_a_single_file_video_repo_is_flagged_diffusers(monkeypatch):from types import SimpleNamespacefrom core.inference.video_families import detect_video_family- from hub.services.models import catalog_classification as classificationrepo_id = "Lightricks/LTX-Video"assert detect_video_family(repo_id) is not None, "fixture assumes a known video family"diff --git a/studio/backend/tests/test_logging_middleware.py b/studio/backend/tests/test_logging_middleware.pyindex f62962fdddf..64f2370fe72 100644--- a/studio/backend/tests/test_logging_middleware.py+++ b/studio/backend/tests/test_logging_middleware.py@@ -15,6 +15,15 @@ from loggers import handlers as hmodfrom loggers.handlers import LoggingMiddleware+async def app(scope, receive, send):+ await send({"type": "http.response.start", "status": 200, "headers": []})+ await send({"type": "http.response.body", "body": b"ok"})+++async def send(message):+ pass++class _LogCapture:def __init__(self):self.events = []@@ -68,13 +77,6 @@ def test_success_logs_status_and_forwards_chunks(logs):def test_excluded_asset_success_skips_log(logs):- async def app(scope, receive, send):- await send({"type": "http.response.start", "status": 200, "headers": []})- await send({"type": "http.response.body", "body": b"ok"})-- async def send(message):- pass-for path in ("/assets/index.css", "/icon.svg", "/font.woff2"):_run(LoggingMiddleware(app)(_http_scope(path), _noop_receive, send))@@ -86,9 +88,6 @@ def test_exception_logs_real_status_and_reraises(logs):await send({"type": "http.response.start", "status": 418, "headers": []})raise RuntimeError("stream failed")- async def send(message):- pass-with pytest.raises(RuntimeError, match = "stream failed"):_run(LoggingMiddleware(app)(_http_scope("/api/health"), _noop_receive, send))@@ -102,9 +101,6 @@ def test_cancelled_error_propagates_without_error_log(logs):async def app(scope, receive, send):raise asyncio.CancelledError()- async def send(message):- pass-with pytest.raises(asyncio.CancelledError):_run(LoggingMiddleware(app)(_http_scope("/api/health"), _noop_receive, send))@@ -117,9 +113,6 @@ def test_non_http_scope_passes_through(logs):async def app(scope, receive, send):seen.append(scope["type"])- async def send(message):- pass-_run(LoggingMiddleware(app)({"type": "websocket", "path": "/ws"}, _noop_receive, send))assert seen == ["websocket"]@@ -129,13 +122,6 @@ def test_non_http_scope_passes_through(logs):def test_duplicate_get_within_window_deduped(logs, monkeypatch):monkeypatch.setattr(hmod, "_ACCESS_LOG_DEDUP_MS", 1000)- async def app(scope, receive, send):- await send({"type": "http.response.start", "status": 200, "headers": []})- await send({"type": "http.response.body", "body": b"ok"})-- async def send(message):- pass-mw = LoggingMiddleware(app)for _ in range(3):_run(mw(_http_scope("/api/models/browse-folders"), _noop_receive, send))@@ -156,9 +142,6 @@ def test_mutations_and_errors_are_never_deduped(logs, monkeypatch):await send({"type": "http.response.start", "status": 404, "headers": []})await send({"type": "http.response.body", "body": b""})- async def send(message):- pass-mw = LoggingMiddleware(post_ok)for _ in range(2):_run(mw(_http_scope("/api/chat/threads", method = "POST"), _noop_receive, send))@@ -175,13 +158,6 @@ def test_quiet_poll_paths_use_longer_heartbeat_window(logs, monkeypatch):monkeypatch.setattr(hmod, "_ACCESS_LOG_DEDUP_MS", 0)monkeypatch.setattr(hmod, "_QUIET_POLL_DEDUP_MS", 1000)- async def app(scope, receive, send):- await send({"type": "http.response.start", "status": 200, "headers": []})- await send({"type": "http.response.body", "body": b"ok"})-- async def send(message):- pass-mw = LoggingMiddleware(app)for _ in range(3):_run(mw(_http_scope("/api/inference/monitor"), _noop_receive, send)) # quiet@@ -200,13 +176,6 @@ def test_liveness_probe_heartbeats(logs, monkeypatch):monkeypatch.setattr(hmod, "_QUIET_POLL_DEDUP_MS", 1000)monkeypatch.setattr(hmod, "_WATCHDOG_POLL_DEDUP_MS", 1000)- async def app(scope, receive, send):- await send({"type": "http.response.start", "status": 200, "headers": []})- await send({"type": "http.response.body", "body": b"ok"})-- async def send(message):- pass-mw = LoggingMiddleware(app)for _ in range(4):_run(mw(_http_scope("/api/liveness"), _noop_receive, send))@@ -260,9 +229,6 @@ def test_liveness_probe_errors_still_log(logs, monkeypatch):await send({"type": "http.response.start", "status": 503, "headers": []})await send({"type": "http.response.body", "body": b"down"})- async def send(message):- pass-mw = LoggingMiddleware(app)for _ in range(3):_run(mw(_http_scope("/api/liveness"), _noop_receive, send))@@ -277,13 +243,6 @@ def test_verbose_keeps_every_watchdog_probe(logs, monkeypatch):monkeypatch.setattr(hmod, "_QUIET_POLL_DEDUP_MS", 0)monkeypatch.setattr(hmod, "_WATCHDOG_POLL_DEDUP_MS", 60000)- async def app(scope, receive, send):- await send({"type": "http.response.start", "status": 200, "headers": []})- await send({"type": "http.response.body", "body": b"ok"})-- async def send(message):- pass-mw = LoggingMiddleware(app)for _ in range(3):_run(mw(_http_scope("/api/liveness"), _noop_receive, send))@@ -295,13 +254,6 @@ def test_verbose_keeps_every_watchdog_probe(logs, monkeypatch):def test_distinct_query_strings_are_not_deduped(logs, monkeypatch):monkeypatch.setattr(hmod, "_ACCESS_LOG_DEDUP_MS", 1000)- async def app(scope, receive, send):- await send({"type": "http.response.start", "status": 200, "headers": []})- await send({"type": "http.response.body", "body": b"ok"})-- async def send(message):- pass-def scope(query):return {"type": "http",diff --git a/studio/backend/tests/test_login_rate_limit.py b/studio/backend/tests/test_login_rate_limit.pyindex 6f9635e41e3..5ccd99af1c8 100644--- a/studio/backend/tests/test_login_rate_limit.py+++ b/studio/backend/tests/test_login_rate_limit.py@@ -126,10 +126,9 @@ class TestClientIp:assert _client_ip(req) == "2001:db8::1"def test_forwarded_isolates_first_element(self, env_trust_proxy):- from routes.auth import _client_ip-# Pick the first Forwarded element only, else suffix variations create# attacker-controlled buckets.+ from routes.auth import _client_ipreq = _FakeRequest("127.0.0.1",{"forwarded": "for=198.51.100.42, for=10.0.0.1;proto=https"},@@ -137,9 +136,8 @@ class TestClientIp:assert _client_ip(req) == "198.51.100.42"def test_xff_invalid_ip_falls_back_to_client_host(self, env_trust_proxy):- from routes.auth import _client_ip-# A garbage XFF must not propagate into the bucket key.+ from routes.auth import _client_ipreq = _FakeRequest("127.0.0.1", {"x-forwarded-for": "not-an-ip"})assert _client_ip(req) == "127.0.0.1"diff --git a/studio/backend/tests/test_mcp_servers.py b/studio/backend/tests/test_mcp_servers.pyindex dbaa8a08dc0..7df8a2c0208 100644--- a/studio/backend/tests/test_mcp_servers.py+++ b/studio/backend/tests/test_mcp_servers.py@@ -81,10 +81,10 @@ def test_validate_url_accepts_http_and_https():def test_validate_url_stdio_keeps_posix_outer_normalization(monkeypatch):- import sys-import routes.mcp_servers as routes_mcp+ import sys+monkeypatch.setattr(sys, "platform", "linux")monkeypatch.setattr(routes_mcp, "stdio_mcp_enabled", lambda: True)assert routes_mcp._validate_url("\n python --flag \u2003") == "python --flag"@@ -99,8 +99,8 @@ def test_validate_url_rejects_bad(bad):def test_stdio_command_codec_roundtrip_preserves_order_empty_and_url_argument(monkeypatch):- import routes.mcp_servers as routes_mcpfrom models.mcp_servers import McpStdioCommand, McpStdioDecodeRequest+ import routes.mcp_servers as routes_mcpmonkeypatch.setattr(routes_mcp, "stdio_mcp_enabled", lambda: True)payload = McpStdioCommand(@@ -118,8 +118,8 @@ def test_stdio_command_codec_roundtrip_preserves_order_empty_and_url_argument(modef test_stdio_command_codec_strips_only_executable_outer_padding(monkeypatch):- import routes.mcp_servers as routes_mcpfrom models.mcp_servers import McpStdioCommand, McpStdioDecodeRequest+ import routes.mcp_servers as routes_mcpmonkeypatch.setattr(routes_mcp, "stdio_mcp_enabled", lambda: True)arguments = [" keep outer spaces ", "\nkeep control whitespace\n", ""]@@ -148,14 +148,17 @@ def test_stdio_command_codec_strips_only_executable_outer_padding(monkeypatch):def test_stdio_command_codec_windows_preserves_final_whitespace_argument_end_to_end(tmp_path, monkeypatch, final_argument):+ from core.inference import mcp_client+ from models.mcp_servers import McpServerUpdate+ from models.mcp_servers import McpStdioCommand, McpStdioDecodeRequestimport asyncio+ import routes.mcp_servers as routes_mcp+import sysimport fastmcpfrom fastmcp.client import transports- from core.inference import mcp_client- import routes.mcp_servers as routes_mcpfrom models.mcp_servers import (McpServerCreate,McpServerTestRequest,@@ -244,8 +247,8 @@ def test_stdio_command_codec_windows_preserves_final_whitespace_argument_end_to_def test_stdio_command_codec_is_stateless_and_never_probes(monkeypatch):- import routes.mcp_servers as routes_mcpfrom models.mcp_servers import McpStdioCommand, McpStdioDecodeRequest+ import routes.mcp_servers as routes_mcpmonkeypatch.setattr(routes_mcp, "stdio_mcp_enabled", lambda: True)@@ -266,8 +269,8 @@ def test_stdio_command_codec_is_stateless_and_never_probes(monkeypatch):def test_stdio_command_codec_remains_available_when_execution_is_disabled(monkeypatch):- import routes.mcp_servers as routes_mcpfrom models.mcp_servers import McpStdioCommand, McpStdioDecodeRequest+ import routes.mcp_servers as routes_mcpmonkeypatch.setattr(routes_mcp, "stdio_mcp_enabled", lambda: False)encoded = routes_mcp.encode_stdio_command(@@ -292,8 +295,8 @@ def test_stdio_command_codec_remains_available_when_execution_is_disabled(monkey],)def test_stdio_command_codec_rejects_invalid_input_before_probe(monkeypatch, operation, detail):- import routes.mcp_servers as routes_mcpfrom models.mcp_servers import McpStdioCommand, McpStdioDecodeRequest+ import routes.mcp_servers as routes_mcpmonkeypatch.setattr(routes_mcp, "stdio_mcp_enabled", lambda: True)@@ -324,6 +327,8 @@ def test_stdio_command_codec_rejects_invalid_input_before_probe(monkeypatch, ope@pytest.mark.parametrize("bad", [1, True, None, {"value": "x"}])def test_stdio_command_codec_rejects_non_string_arguments(bad):+ from models.mcp_servers import McpStdioCommand, McpStdioDecodeRequest+from models.mcp_servers import McpStdioCommandwith pytest.raises(ValidationError):McpStdioCommand.model_validate({"command": "python", "arguments": [bad]})@@ -337,7 +342,9 @@ def test_stdio_command_codec_rejects_non_string_arguments(bad):],)def test_stdio_command_codec_rejects_json_nul(payload, monkeypatch):+ from models.mcp_servers import McpStdioCommand, McpStdioDecodeRequestimport routes.mcp_servers as routes_mcp+from models.mcp_servers import McpStdioCommandmonkeypatch.setattr(routes_mcp, "stdio_mcp_enabled", lambda: True)@@ -351,9 +358,11 @@ def test_stdio_command_codec_rejects_json_nul(payload, monkeypatch):def test_stdio_raw_nul_is_rejected_before_route_side_effects(tmp_path, monkeypatch):+ from models.mcp_servers import McpServerUpdate+ from models.mcp_servers import McpStdioCommand, McpStdioDecodeRequestimport asyncio-import routes.mcp_servers as routes_mcp+from models.mcp_servers import (McpServerCreate,McpServerImportRequest,@@ -435,9 +444,10 @@ def test_normalize_headers():def test_invalid_headers_and_environment_are_rejected_before_side_effects(headers, tmp_path, monkeypatch):+ from models.mcp_servers import McpServerUpdateimport asyncio-import routes.mcp_servers as routes_mcp+from models.mcp_servers import (McpServerCreate,McpServerImportRequest,@@ -499,10 +509,10 @@ def test_invalid_headers_and_environment_are_rejected_before_side_effects(def test_changes_from_payload_tristate_headers():- from routes.mcp_servers import _changes_from_payload+ # omitted → key absentfrom models.mcp_servers import McpServerUpdate+ from routes.mcp_servers import _changes_from_payload- # omitted → key absentassert "headers_json" not in _changes_from_payload(McpServerUpdate(display_name = "x"))# null → stored as None (clear all headers)assert _changes_from_payload(McpServerUpdate(headers = None))["headers_json"] is None@@ -536,8 +546,8 @@ def test_execute_tool_malformed_mcp_name():def test_execute_tool_unknown_server(tmp_path, monkeypatch):- _reset_db(tmp_path, monkeypatch)from core.inference.tools import execute_tool+ _reset_db(tmp_path, monkeypatch)assert (execute_tool("mcp__missing__do_thing", {})== "Error: MCP server for tool 'do_thing' not found"@@ -545,6 +555,8 @@ def test_execute_tool_unknown_server(tmp_path, monkeypatch):def test_execute_tool_disabled_server(tmp_path, monkeypatch):+ from core.inference.tools import execute_tool+_reset_db(tmp_path, monkeypatch)mcp_servers_db.create_server(id = "srv1",@@ -552,7 +564,6 @@ def test_execute_tool_disabled_server(tmp_path, monkeypatch):url = "https://a/m",is_enabled = False,)- from core.inference.tools import execute_toolassert execute_tool("mcp__srv1__do_thing", {}) == "Error: MCP server 'A' is disabled"@@ -651,10 +662,10 @@ def test_mcp_specs_keep_the_tool_arguments(schema_key):def test_mcp_specs_match_the_installed_sdk_dump():"""Whichever spelling the pinned SDK emits, the arguments must survive."""- from mcp.types import Tool-from core.inference.tools import _mcp_specs_for_server+ from mcp.types import Tool+schema = {"type": "object", "properties": {"q": {"type": "string"}}}dumped = Tool(name = "search", description = "d", inputSchema = schema).model_dump(exclude_none = True)specs = _mcp_specs_for_server({"id": "srv", "display_name": "S"}, [dumped])@@ -673,8 +684,8 @@ def test_mcp_specs_drops_duplicate_names():def test_call_tool_sync_respects_pre_set_cancel_event(monkeypatch):"""Pre-set cancel_event -> immediate cancellation, no network round-trip."""- import threadingfrom core.inference import mcp_client+ import threading# Stub _client so the test doesn't need a real MCP server.class _StubClient:@@ -711,10 +722,10 @@ def test_call_tool_sync_respects_pre_set_cancel_event(monkeypatch):def test_clear_oauth_tokens_async_no_op_safe(tmp_path, monkeypatch):"""clear_oauth_tokens_async on a URL with no stored token must not raise;the delete + update handlers call it best-effort regardless of state."""+ from core.inference import mcp_clientimport asynciomonkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path))- from core.inference import mcp_clientmonkeypatch.setattr(mcp_client, "_oauth_token_store", None)asyncio.run(mcp_client.clear_oauth_tokens_async("https://example.com/mcp"))@@ -723,10 +734,11 @@ def test_clear_oauth_tokens_async_no_op_safe(tmp_path, monkeypatch):def test_delete_server_calls_oauth_cleanup_when_oauth_was_on(tmp_path, monkeypatch):"""delete_mcp_server route helper must call clear_oauth_tokens_asyncwhen the deleted row had use_oauth=true."""+ from core.inference import mcp_clientimport asyncio+ import routes.mcp_servers as routes_mcp_reset_db(tmp_path, monkeypatch)- from core.inference import mcp_clientmonkeypatch.setattr(mcp_client, "_oauth_token_store", None)mcp_servers_db.create_server(@@ -744,7 +756,6 @@ def test_delete_server_calls_oauth_cleanup_when_oauth_was_on(tmp_path, monkeypatmonkeypatch.setattr(mcp_client, "clear_oauth_tokens_async", fake_clear)# Patch the route's module binding too so it's seen.- import routes.mcp_servers as routes_mcpmonkeypatch.setattr(routes_mcp, "clear_oauth_tokens_async", fake_clear)asyncio.run(routes_mcp.delete_mcp_server("oauth1", current_subject = "u"))@@ -754,11 +765,11 @@ def test_delete_server_calls_oauth_cleanup_when_oauth_was_on(tmp_path, monkeypatdef test_delete_server_skips_oauth_cleanup_when_oauth_off(tmp_path, monkeypatch):"""No OAuth token cleanup when the deleted server never had OAuth."""+ from core.inference import mcp_clientimport asyncio+ import routes.mcp_servers as routes_mcp_reset_db(tmp_path, monkeypatch)- from core.inference import mcp_client- import routes.mcp_servers as routes_mcpmonkeypatch.setattr(mcp_client, "_oauth_token_store", None)mcp_servers_db.create_server(@@ -781,13 +792,13 @@ def test_delete_server_skips_oauth_cleanup_when_oauth_off(tmp_path, monkeypatch)def test_update_server_clears_oauth_on_url_change(tmp_path, monkeypatch):"""Changing the URL on an OAuth server must drop the old URL's tokensso the new URL doesn't inherit credentials."""- import asyncio-- _reset_db(tmp_path, monkeypatch)from core.inference import mcp_clientfrom models.mcp_servers import McpServerUpdate+ import asyncioimport routes.mcp_servers as routes_mcp+ _reset_db(tmp_path, monkeypatch)+monkeypatch.setattr(mcp_client, "_oauth_token_store", None)mcp_servers_db.create_server(id = "s1",@@ -816,13 +827,13 @@ def test_update_server_clears_oauth_on_url_change(tmp_path, monkeypatch):def test_update_server_clears_oauth_when_oauth_disabled(tmp_path, monkeypatch):"""Flipping use_oauth false must drop the old URL's tokens."""- import asyncio-- _reset_db(tmp_path, monkeypatch)from core.inference import mcp_clientfrom models.mcp_servers import McpServerUpdate+ import asyncioimport routes.mcp_servers as routes_mcp+ _reset_db(tmp_path, monkeypatch)+monkeypatch.setattr(mcp_client, "_oauth_token_store", None)mcp_servers_db.create_server(id = "s1",@@ -849,8 +860,8 @@ def test_update_server_clears_oauth_when_oauth_disabled(tmp_path, monkeypatch):def test_changes_from_payload_rejects_null_is_enabled():"""Explicit null for is_enabled used to hit int(None) -> TypeError 500."""- from routes.mcp_servers import _changes_from_payloadfrom models.mcp_servers import McpServerUpdate+ from routes.mcp_servers import _changes_from_payloadwith pytest.raises(HTTPException) as exc:_changes_from_payload(McpServerUpdate(is_enabled = None))@@ -859,8 +870,8 @@ def test_changes_from_payload_rejects_null_is_enabled():def test_changes_from_payload_rejects_null_use_oauth():"""Explicit null for use_oauth used to hit int(None) -> TypeError 500."""- from routes.mcp_servers import _changes_from_payloadfrom models.mcp_servers import McpServerUpdate+ from routes.mcp_servers import _changes_from_payloadwith pytest.raises(HTTPException) as exc:_changes_from_payload(McpServerUpdate(use_oauth = None))@@ -889,9 +900,10 @@ def test_test_endpoint_surfaces_url_validation_as_400(tmp_path, monkeypatch):def test_tool_xml_parser_handles_hyphenated_parameter_names():"""Hyphenated property names like `issue-number` must round-trip through theXML parser (the old `<parameter=\\w+>` regex dropped them)."""- from core.inference.tool_call_parser import parse_tool_calls_from_textimport json as _json+ from core.inference.tool_call_parser import parse_tool_calls_from_text+calls = parse_tool_calls_from_text("<function=mcp__srv__create-issue>""<parameter=issue-title>Bug report</parameter>"@@ -929,9 +941,10 @@ def test_tool_healing_strip_handles_gemma_close_only_marker():def test_tool_healing_parser_handles_gemma_native_windows_path():- from core.tool_healing import parse_tool_calls_from_textimport json as _json+ from core.tool_healing import parse_tool_calls_from_text+calls = parse_tool_calls_from_text(r'<|tool_call>call:ls{path:<|"|>C:\Users\wasim\repo<|"|>}<tool_call|>')@@ -941,9 +954,10 @@ def test_tool_healing_parser_handles_gemma_native_windows_path():def test_tool_healing_json_parser_preserves_literal_gemma_quote_token():- from core.tool_healing import parse_tool_calls_from_textimport json as _json+ from core.tool_healing import parse_tool_calls_from_text+text = ("<tool_call>"+ _json.dumps({"name": "python", "arguments": {"code": "print('<|\"|>')"}})@@ -1000,6 +1014,7 @@ def test_call_tool_sync_short_circuits_on_pre_set_cancel(monkeypatch):"""Pre-set cancel_event -> no HTTP request (task used to open a transportbefore the cancel check)."""from core.inference import mcp_client+ import threadingopened: list[str] = []@@ -1021,8 +1036,6 @@ def test_call_tool_sync_short_circuits_on_pre_set_cancel(monkeypatch):monkeypatch.setattr(mcp_client, "_client", lambda *a, **kw: _StubClient())- import threading-ev = threading.Event()ev.set()out = mcp_client.call_tool_sync(@@ -1041,8 +1054,8 @@ def test_call_tool_sync_short_circuits_on_pre_set_cancel(monkeypatch):def test_clear_oauth_tokens_swallows_constructor_errors(tmp_path, monkeypatch):"""clear_oauth_tokens_async is best-effort; an OAuth constructor failuremust not bubble into a 500 from the delete/update routes."""- import asynciofrom core.inference import mcp_client+ import asynciomonkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path))monkeypatch.setattr(mcp_client, "_oauth_token_store", None)@@ -1064,6 +1077,8 @@ def test_clear_oauth_tokens_swallows_constructor_errors(tmp_path, monkeypatch):def test_tool_xml_parser_handles_hyphenated_function_names():"""Hyphenated tool names like `mcp__srv__list-issues` must parse, else themodel can call the tool but Unsloth can't dispatch."""+ import json as _json+from core.inference.tool_call_parser import parse_tool_calls_from_textcalls = parse_tool_calls_from_text(@@ -1071,7 +1086,6 @@ def test_tool_xml_parser_handles_hyphenated_function_names():)assert len(calls) == 1assert calls[0]["function"]["name"] == "mcp__srv__list-issues"- import json as _jsonargs = _json.loads(calls[0]["function"]["arguments"])assert args == {"repo": "octocat/hello"}@@ -1105,6 +1119,7 @@ def test_safetensors_agentic_empty_allowlist_still_means_allow_all():constraint". The MCP-only-no-discovery fix lives at the route level ininference.py, which refuses use_tools when the resolved list is empty."""import threading+from core.inference.safetensors_agentic import run_safetensors_tool_loopcalls: list[str] = []@@ -1149,11 +1164,11 @@ def _one_tool(name = "echo"):def test_get_enabled_mcp_tools_caches_discovery(tmp_path, monkeypatch):"""A second send must serve tools from cache instead of re-probing."""+ from core.inference import mcp_client+ from core.inference import tools as tools_modimport asyncio_reset_db(tmp_path, monkeypatch)- from core.inference import mcp_client- from core.inference import tools as tools_modmonkeypatch.setattr(mcp_client, "_tool_cache", {})mcp_servers_db.create_server(id = "s1", display_name = "A", url = "https://x/mcp", is_enabled = True)@@ -1181,11 +1196,11 @@ def test_get_enabled_mcp_tools_caches_discovery(tmp_path, monkeypatch):def test_get_enabled_mcp_tools_does_not_cache_failures(tmp_path, monkeypatch):"""A failed probe isn't cached: once the cool-off elapses, it's retried."""+ from core.inference import mcp_client+ from core.inference import tools as tools_modimport asyncio_reset_db(tmp_path, monkeypatch)- from core.inference import mcp_client- from core.inference import tools as tools_modmonkeypatch.setattr(mcp_client, "_tool_cache", {})monkeypatch.setattr(mcp_client, "_probe_cooloff_until", {})@@ -1216,13 +1231,13 @@ def test_get_enabled_mcp_tools_does_not_cache_failures(tmp_path, monkeypatch):def test_refresh_warms_tool_cache(tmp_path, monkeypatch):"""Clicking Refresh must populate the cache the chat path reads."""- import asyncio-- _reset_db(tmp_path, monkeypatch)from core.inference import mcp_clientfrom core.inference import tools as tools_mod+ import asyncioimport routes.mcp_servers as routes_mcp+ _reset_db(tmp_path, monkeypatch)+monkeypatch.setattr(mcp_client, "_tool_cache", {})mcp_servers_db.create_server(id = "s1", display_name = "A", url = "https://x/mcp", is_enabled = True)@@ -1248,13 +1263,13 @@ def test_refresh_warms_tool_cache(tmp_path, monkeypatch):def test_update_url_evicts_tool_cache(tmp_path, monkeypatch):"""Re-pointing the URL must drop the old endpoint's cached tools."""- import asyncio-- _reset_db(tmp_path, monkeypatch)from core.inference import mcp_clientfrom models.mcp_servers import McpServerUpdate+ import asyncioimport routes.mcp_servers as routes_mcp+ _reset_db(tmp_path, monkeypatch)+monkeypatch.setattr(mcp_client, "_tool_cache", {"s1": _one_tool("stale")})mcp_servers_db.create_server(id = "s1", display_name = "A", url = "https://old/mcp", is_enabled = True)@@ -1268,13 +1283,13 @@ def test_update_url_evicts_tool_cache(tmp_path, monkeypatch):def test_update_display_name_keeps_tool_cache(tmp_path, monkeypatch):"""A rename touches no endpoint, so the cache must survive it."""- import asyncio-- _reset_db(tmp_path, monkeypatch)from core.inference import mcp_clientfrom models.mcp_servers import McpServerUpdate+ import asyncioimport routes.mcp_servers as routes_mcp+ _reset_db(tmp_path, monkeypatch)+cached = _one_tool()monkeypatch.setattr(mcp_client, "_tool_cache", {"s1": cached})mcp_servers_db.create_server(id = "s1", display_name = "A", url = "https://x/mcp", is_enabled = True)@@ -1289,12 +1304,13 @@ def test_update_rename_keeps_stdio_session(tmp_path, monkeypatch):"""The edit dialog resends url/headers/oauth unchanged on a rename, so gatingthe close on field presence would drop the live stdio session. Only a realendpoint/auth change may close it."""+ from models.mcp_servers import McpServerUpdateimport asyncio+ import routes.mcp_servers as routes_mcp+import json_reset_db(tmp_path, monkeypatch)- from models.mcp_servers import McpServerUpdate- import routes.mcp_servers as routes_mcpclosed: list = []monkeypatch.setattr(routes_mcp, "stdio_mcp_enabled", lambda: True)@@ -1324,11 +1340,11 @@ def test_update_rename_keeps_stdio_session(tmp_path, monkeypatch):def test_update_stdio_command_change_closes_session(tmp_path, monkeypatch):"""A real command change must still close the old stdio session."""+ from models.mcp_servers import McpServerUpdateimport asyncio+ import routes.mcp_servers as routes_mcp_reset_db(tmp_path, monkeypatch)- from models.mcp_servers import McpServerUpdate- import routes.mcp_servers as routes_mcpclosed: list = []monkeypatch.setattr(routes_mcp, "stdio_mcp_enabled", lambda: True)@@ -1350,12 +1366,15 @@ def test_update_stdio_command_change_closes_session(tmp_path, monkeypatch):def test_stdio_arguments_update_preserves_untouched_bytes_then_invalidates_once(tmp_path, monkeypatch):+ from core.inference import mcp_client+ from models.mcp_servers import McpServerUpdate+ from models.mcp_servers import McpStdioCommand, McpStdioDecodeRequestimport asyncio+ import routes.mcp_servers as routes_mcp+import json- from core.inference import mcp_clientfrom models.mcp_servers import McpServerUpdate, McpStdioCommand- import routes.mcp_servers as routes_mcp_reset_db(tmp_path, monkeypatch)monkeypatch.setattr(routes_mcp, "stdio_mcp_enabled", lambda: True)@@ -1409,13 +1428,13 @@ def test_stdio_arguments_update_preserves_untouched_bytes_then_invalidates_once(def test_update_disable_evicts_tool_cache(tmp_path, monkeypatch):"""Disabling a server must drop its cached tools, not leave them unread."""- import asyncio-- _reset_db(tmp_path, monkeypatch)from core.inference import mcp_clientfrom models.mcp_servers import McpServerUpdate+ import asyncioimport routes.mcp_servers as routes_mcp+ _reset_db(tmp_path, monkeypatch)+monkeypatch.setattr(mcp_client, "_tool_cache", {"s1": _one_tool()})mcp_servers_db.create_server(id = "s1", display_name = "A", url = "https://x/mcp", is_enabled = True)@@ -1427,11 +1446,11 @@ def test_update_disable_evicts_tool_cache(tmp_path, monkeypatch):def test_delete_evicts_tool_cache(tmp_path, monkeypatch):"""Deleting a server must not leave its tools cached."""+ from core.inference import mcp_clientimport asyncio+ import routes.mcp_servers as routes_mcp_reset_db(tmp_path, monkeypatch)- from core.inference import mcp_client- import routes.mcp_servers as routes_mcpmonkeypatch.setattr(mcp_client, "_tool_cache", {"s1": _one_tool()})mcp_servers_db.create_server(id = "s1", display_name = "A", url = "https://x/mcp", is_enabled = True)@@ -1450,11 +1469,11 @@ def test_invalidate_tool_cache_clears_all(monkeypatch):def test_get_enabled_mcp_tools_probes_only_uncached(tmp_path, monkeypatch):"""An already-cached server must not be re-probed alongside a cold one."""+ from core.inference import mcp_client+ from core.inference import tools as tools_modimport asyncio_reset_db(tmp_path, monkeypatch)- from core.inference import mcp_client- from core.inference import tools as tools_modmonkeypatch.setattr(mcp_client, "_tool_cache", {"s1": _one_tool("cached")})mcp_servers_db.create_server(id = "s1", display_name = "A", url = "https://a/mcp", is_enabled = True)@@ -1480,11 +1499,11 @@ def test_get_enabled_mcp_tools_probes_only_uncached(tmp_path, monkeypatch):def test_get_enabled_mcp_tools_partial_failure_caches_healthy(tmp_path, monkeypatch):"""One server failing must not stop the others from being cached/served."""+ from core.inference import mcp_client+ from core.inference import tools as tools_modimport asyncio_reset_db(tmp_path, monkeypatch)- from core.inference import mcp_client- from core.inference import tools as tools_modmonkeypatch.setattr(mcp_client, "_tool_cache", {})monkeypatch.setattr(mcp_client, "_probe_cooloff_until", {})@@ -1511,11 +1530,11 @@ def test_get_enabled_mcp_tools_partial_failure_caches_healthy(tmp_path, monkeypadef test_get_enabled_mcp_tools_caches_empty_tool_list(tmp_path, monkeypatch):"""A server exposing zero tools is cached as [] (a hit), not re-probed."""+ from core.inference import mcp_client+ from core.inference import tools as tools_modimport asyncio_reset_db(tmp_path, monkeypatch)- from core.inference import mcp_client- from core.inference import tools as tools_modmonkeypatch.setattr(mcp_client, "_tool_cache", {})mcp_servers_db.create_server(id = "s1", display_name = "A", url = "https://x/mcp", is_enabled = True)@@ -1541,13 +1560,13 @@ def test_get_enabled_mcp_tools_caches_empty_tool_list(tmp_path, monkeypatch):def test_update_headers_evicts_tool_cache(tmp_path, monkeypatch):"""Changing auth headers must drop tools discovered under the old headers."""- import asyncio-- _reset_db(tmp_path, monkeypatch)from core.inference import mcp_clientfrom models.mcp_servers import McpServerUpdate+ import asyncioimport routes.mcp_servers as routes_mcp+ _reset_db(tmp_path, monkeypatch)+monkeypatch.setattr(mcp_client, "_tool_cache", {"s1": _one_tool()})mcp_servers_db.create_server(id = "s1", display_name = "A", url = "https://x/mcp", is_enabled = True)@@ -1564,11 +1583,11 @@ def test_update_headers_evicts_tool_cache(tmp_path, monkeypatch):def test_get_enabled_mcp_tools_skips_cache_when_config_changes_mid_probe(tmp_path, monkeypatch):"""A config edit landing during an in-flight probe must not be clobberedby the now-stale probe result (TOCTOU on the cache write)."""+ from core.inference import mcp_client+ from core.inference import tools as tools_modimport asyncio_reset_db(tmp_path, monkeypatch)- from core.inference import mcp_client- from core.inference import tools as tools_modmonkeypatch.setattr(mcp_client, "_tool_cache", {})mcp_servers_db.create_server(id = "s1", display_name = "A", url = "https://old/mcp", is_enabled = True)@@ -1596,11 +1615,11 @@ def test_get_enabled_mcp_tools_no_cooloff_when_config_changes_mid_failed_probe("""An edit landing while a probe of the OLD config is failing must not parka cool-off on the now-fresh config -- else the re-pointed server the userjust fixed is needlessly skipped for the whole cool-off window."""+ from core.inference import mcp_client+ from core.inference import tools as tools_modimport asyncio_reset_db(tmp_path, monkeypatch)- from core.inference import mcp_client- from core.inference import tools as tools_modmonkeypatch.setattr(mcp_client, "_tool_cache", {})monkeypatch.setattr(mcp_client, "_probe_cooloff_until", {})@@ -1628,11 +1647,11 @@ def test_get_enabled_mcp_tools_no_cooloff_when_server_deleted_mid_failed_probe():"""A delete landing while a probe fails must not leave an orphan cool-offentry keyed by the since-removed server id."""+ from core.inference import mcp_client+ from core.inference import tools as tools_modimport asyncio_reset_db(tmp_path, monkeypatch)- from core.inference import mcp_client- from core.inference import tools as tools_modmonkeypatch.setattr(mcp_client, "_tool_cache", {})monkeypatch.setattr(mcp_client, "_probe_cooloff_until", {})@@ -1656,11 +1675,11 @@ def test_get_enabled_mcp_tools_no_cooloff_when_server_deleted_mid_failed_probe(def test_get_enabled_mcp_tools_skips_failed_server_during_cooloff(tmp_path, monkeypatch):"""A down server is probed once, then skipped during the cool-off insteadof being re-probed (and re-hung) on every send."""+ from core.inference import mcp_client+ from core.inference import tools as tools_modimport asyncio_reset_db(tmp_path, monkeypatch)- from core.inference import mcp_client- from core.inference import tools as tools_modmonkeypatch.setattr(mcp_client, "_tool_cache", {})monkeypatch.setattr(mcp_client, "_probe_cooloff_until", {})@@ -1724,11 +1743,11 @@ def test_invalidate_clears_failure_cooloff(monkeypatch):def test_refresh_failure_records_cooloff(tmp_path, monkeypatch):"""A failed manual refresh starts the cool-off so the next chat send doesnot immediately hang on the down server."""+ from core.inference import mcp_clientimport asyncio+ import routes.mcp_servers as routes_mcp_reset_db(tmp_path, monkeypatch)- from core.inference import mcp_client- import routes.mcp_servers as routes_mcpmonkeypatch.setattr(mcp_client, "_tool_cache", {})monkeypatch.setattr(mcp_client, "_probe_cooloff_until", {})@@ -1751,11 +1770,11 @@ def test_refresh_failure_records_cooloff(tmp_path, monkeypatch):def test_refresh_drops_result_when_config_changes_mid_probe(tmp_path, monkeypatch):"""A manual refresh must not warm the chat cache with tools discoveredunder an old config if the server is edited while the probe is in flight."""+ from core.inference import mcp_clientimport asyncio+ import routes.mcp_servers as routes_mcp_reset_db(tmp_path, monkeypatch)- from core.inference import mcp_client- import routes.mcp_servers as routes_mcpmonkeypatch.setattr(mcp_client, "_tool_cache", {})mcp_servers_db.create_server(id = "s1", display_name = "A", url = "https://old/mcp", is_enabled = True)@@ -1778,11 +1797,11 @@ def test_refresh_drops_result_when_config_changes_mid_probe(tmp_path, monkeypatcdef test_refresh_failure_no_cooloff_when_config_changes_mid_probe(tmp_path, monkeypatch):"""A manual refresh failure for an old config must not cool off the freshlyedited server."""+ from core.inference import mcp_clientimport asyncio+ import routes.mcp_servers as routes_mcp_reset_db(tmp_path, monkeypatch)- from core.inference import mcp_client- import routes.mcp_servers as routes_mcpmonkeypatch.setattr(mcp_client, "_tool_cache", {})monkeypatch.setattr(mcp_client, "_probe_cooloff_until", {})@@ -1807,11 +1826,11 @@ def test_get_enabled_mcp_tools_drops_result_when_server_deleted_mid_probe(tmp_pa"""A delete landing while a probe is in flight must drop the now-orphanresult -- the `fresh is None` arm of the mid-probe TOCTOU guard. Theresult is neither served nor cached under the since-removed id."""+ from core.inference import mcp_client+ from core.inference import tools as tools_modimport asyncio_reset_db(tmp_path, monkeypatch)- from core.inference import mcp_client- from core.inference import tools as tools_modmonkeypatch.setattr(mcp_client, "_tool_cache", {})monkeypatch.setattr(mcp_client, "_probe_cooloff_until", {})@@ -1838,12 +1857,13 @@ def test_oauth_probe_failure_in_chat_path_uses_long_cooloff(tmp_path, monkeypatc"""When an OAuth server fails discovery during a send, the chat path mustrecord the OAuth (long) cool-off, not the plain one -- otherwise itsmulti-minute browser hang recurs every minute."""+ from core.inference import mcp_client+ from core.inference import tools as tools_modimport asyncio+import time_reset_db(tmp_path, monkeypatch)- from core.inference import mcp_client- from core.inference import tools as tools_modmonkeypatch.setattr(mcp_client, "_tool_cache", {})monkeypatch.setattr(mcp_client, "_probe_cooloff_until", {})@@ -1932,13 +1952,14 @@ def test_provisional_tool_provenance_stamps_mcp_display_name(tmp_path, monkeypatdef test_display_names_never_reach_the_callable_tool_name(tmp_path, monkeypatch):"""Routing, approvals and replay all key off mcp__<serverId>__<tool>, so thedisplay name must stay presentation only."""+ from core.inference.tools import _mcp_specs_for_server+_reset_db(tmp_path, monkeypatch)mcp_servers_db.create_server(id = "srv1", display_name = "GitHub", url = "https://a/m")from core.inference.tool_loop_controller import (mcp_display_parts,provisional_tool_provenance,)- from core.inference.tools import _mcp_specs_for_serverspecs = _mcp_specs_for_server({"id": "srv1", "display_name": "GitHub"}, [{"name": "create_issue"}]@@ -1952,10 +1973,11 @@ def test_display_names_never_reach_the_callable_tool_name(tmp_path, monkeypatch)def test_two_servers_sharing_a_display_name_stay_separately_routable(tmp_path, monkeypatch):"""Duplicate display names must not collapse two servers into one route."""+ from core.inference.tools import _mcp_specs_for_server+_reset_db(tmp_path, monkeypatch)for sid in ("srv1", "srv2"):mcp_servers_db.create_server(id = sid, display_name = "GitHub", url = f"https://{sid}/m")- from core.inference.tools import _mcp_specs_for_servernames = {_mcp_specs_for_server({"id": sid, "display_name": "GitHub"}, [{"name": "run"}])[0][diff --git a/studio/backend/tests/test_mcp_stdio_api_key_gate.py b/studio/backend/tests/test_mcp_stdio_api_key_gate.pyindex d5ac9e39652..65b6a85e415 100644--- a/studio/backend/tests/test_mcp_stdio_api_key_gate.py+++ b/studio/backend/tests/test_mcp_stdio_api_key_gate.py@@ -67,6 +67,7 @@ def test_stdio_command_codec_refuses_api_key_before_work(monkeypatch, stdio_on, no_probe, operation):import routes.mcp_servers as routes_mcp+from models.mcp_servers import McpStdioCommand, McpStdioDecodeRequestdef _never(*args, **kwargs):@@ -94,6 +95,7 @@ def test_stdio_command_codec_refuses_api_key_before_work(def test_test_endpoint_refuses_stdio_from_api_key(tmp_path, monkeypatch, stdio_on, no_probe):import routes.mcp_servers as routes_mcp+from models.mcp_servers import McpServerTestRequest_reset_db(tmp_path, monkeypatch)@@ -110,6 +112,7 @@ def test_test_endpoint_refuses_stdio_from_api_key(tmp_path, monkeypatch, stdio_odef test_test_endpoint_allows_http_from_api_key(tmp_path, monkeypatch, stdio_on):import routes.mcp_servers as routes_mcp+from models.mcp_servers import McpServerTestRequest_reset_db(tmp_path, monkeypatch)@@ -136,6 +139,7 @@ def test_test_endpoint_allows_http_from_api_key(tmp_path, monkeypatch, stdio_on)def test_create_refuses_stdio_from_api_key_and_writes_nothing(tmp_path, monkeypatch, stdio_on):import routes.mcp_servers as routes_mcp+from models.mcp_servers import McpServerCreate_reset_db(tmp_path, monkeypatch)@@ -153,6 +157,7 @@ def test_create_refuses_stdio_from_api_key_and_writes_nothing(tmp_path, monkeypadef test_create_allows_http_from_api_key(tmp_path, monkeypatch, stdio_on):import routes.mcp_servers as routes_mcp+from models.mcp_servers import McpServerCreate_reset_db(tmp_path, monkeypatch)@@ -167,8 +172,8 @@ def test_create_allows_http_from_api_key(tmp_path, monkeypatch, stdio_on):def test_update_refuses_http_to_stdio_conversion_from_api_key(tmp_path, monkeypatch, stdio_on):- import routes.mcp_servers as routes_mcpfrom models.mcp_servers import McpServerUpdate+ import routes.mcp_servers as routes_mcp_reset_db(tmp_path, monkeypatch)mcp_servers_db.create_server(id = "s1", display_name = "A", url = "https://a/mcp")@@ -198,8 +203,8 @@ def test_update_refuses_any_edit_of_a_stdio_row_from_api_key():"""Not just the address: the env vars, the name and the enabled flag allchange what runs or how, so an API key may not touch a stdio row at all."""- import routes.mcp_servers as routes_mcpfrom models.mcp_servers import McpServerUpdate+ import routes.mcp_servers as routes_mcp_reset_db(tmp_path, monkeypatch)mcp_servers_db.create_server(id = "s1", display_name = "Local", url = STDIO_CMD)@@ -220,8 +225,8 @@ def test_update_regates_after_the_oauth_clear_await(tmp_path, monkeypatch, stdio"""clear_oauth_tokens_async awaits, handing the loop to other requests. Ifthe owner converts the row to stdio in that window, the write that followsmust not land the API key's headers as the command's env."""- import routes.mcp_servers as routes_mcpfrom models.mcp_servers import McpServerUpdate+ import routes.mcp_servers as routes_mcp_reset_db(tmp_path, monkeypatch)mcp_servers_db.create_server(@@ -249,8 +254,8 @@ def test_update_regates_after_the_oauth_clear_await(tmp_path, monkeypatch, stdiodef test_update_allows_http_row_but_redacts_saved_headers_from_keyless(tmp_path, monkeypatch, stdio_on):- import routes.mcp_servers as routes_mcpfrom models.mcp_servers import McpServerUpdate+ import routes.mcp_servers as routes_mcp_reset_db(tmp_path, monkeypatch)mcp_servers_db.create_server(@@ -318,6 +323,7 @@ _MIXED_CONFIG = {def test_import_from_api_key_keeps_http_and_reports_stdio(tmp_path, monkeypatch, stdio_on):import routes.mcp_servers as routes_mcp+from models.mcp_servers import McpServerImportRequest_reset_db(tmp_path, monkeypatch)@@ -351,7 +357,9 @@ def test_import_from_api_key_keeps_http_and_reports_stdio(tmp_path, monkeypatch,def test_ui_session_still_creates_and_imports_stdio(tmp_path, monkeypatch, stdio_on):+ from models.mcp_servers import McpServerUpdateimport routes.mcp_servers as routes_mcp+from models.mcp_servers import McpServerCreate, McpServerImportRequest, McpServerUpdate_reset_db(tmp_path, monkeypatch)@@ -390,10 +398,10 @@ def test_default_is_ui_session_so_direct_calls_are_unaffected():"""The dependency is Annotated with a plain False default; a bare`= Depends(...)` default would be a truthy object and 403 every directcall (the existing suites call these handlers directly)."""- import inspect-import routes.mcp_servers as routes_mcp+ import inspect+for name in ("create_mcp_server","update_mcp_server",diff --git a/studio/backend/tests/test_mcp_stdio_improvements.py b/studio/backend/tests/test_mcp_stdio_improvements.pyindex 0bcd109fb2b..06f3c8fc861 100644--- a/studio/backend/tests/test_mcp_stdio_improvements.py+++ b/studio/backend/tests/test_mcp_stdio_improvements.py@@ -47,10 +47,11 @@ def test_client_builds_stdio_when_enabled_without_spawning(monkeypatch):def test_client_builds_stdio_with_encoded_arguments_without_shell(monkeypatch):+ import routes.mcp_servers as routes_mcp+import fastmcpfrom fastmcp.client import transportsfrom models.mcp_servers import McpStdioCommand- import routes.mcp_servers as routes_mcp_enable(monkeypatch)captured = {}@@ -89,6 +90,7 @@ def test_client_http_unaffected_by_gate(monkeypatch):def test_create_forces_oauth_off_for_stdio(tmp_path, monkeypatch):import routes.mcp_servers as routes_mcp+from models.mcp_servers import McpServerCreate_reset_db(tmp_path, monkeypatch)@@ -105,6 +107,7 @@ def test_create_forces_oauth_off_for_stdio(tmp_path, monkeypatch):def test_create_keeps_oauth_for_http(tmp_path, monkeypatch):import routes.mcp_servers as routes_mcp+from models.mcp_servers import McpServerCreate_reset_db(tmp_path, monkeypatch)@@ -120,6 +123,7 @@ def test_create_keeps_oauth_for_http(tmp_path, monkeypatch):def test_connection_test_forces_oauth_off_for_stdio(monkeypatch):import routes.mcp_servers as routes_mcp+from models.mcp_servers import McpServerTestRequest_enable(monkeypatch)@@ -143,8 +147,8 @@ def test_connection_test_forces_oauth_off_for_stdio(monkeypatch):def test_update_url_to_stdio_clears_oauth(tmp_path, monkeypatch):- import routes.mcp_servers as routes_mcpfrom models.mcp_servers import McpServerUpdate+ import routes.mcp_servers as routes_mcp_reset_db(tmp_path, monkeypatch)_enable(monkeypatch)@@ -163,8 +167,8 @@ def test_update_url_to_stdio_clears_oauth(tmp_path, monkeypatch):def test_switch_stdio_to_http_drops_env(tmp_path, monkeypatch):- import routes.mcp_servers as routes_mcpfrom models.mcp_servers import McpServerUpdate+ import routes.mcp_servers as routes_mcp_reset_db(tmp_path, monkeypatch)_enable(monkeypatch)@@ -185,8 +189,8 @@ def test_switch_stdio_to_http_drops_env(tmp_path, monkeypatch):def test_switch_keeps_explicitly_supplied_headers(tmp_path, monkeypatch):- import routes.mcp_servers as routes_mcpfrom models.mcp_servers import McpServerUpdate+ import routes.mcp_servers as routes_mcp_reset_db(tmp_path, monkeypatch)_enable(monkeypatch)@@ -207,8 +211,8 @@ def test_switch_keeps_explicitly_supplied_headers(tmp_path, monkeypatch):def test_same_transport_edit_keeps_headers(tmp_path, monkeypatch):- import routes.mcp_servers as routes_mcpfrom models.mcp_servers import McpServerUpdate+ import routes.mcp_servers as routes_mcp_reset_db(tmp_path, monkeypatch)_enable(monkeypatch)diff --git a/studio/backend/tests/test_mcp_stdio_pr5863.py b/studio/backend/tests/test_mcp_stdio_pr5863.pyindex 80322139aa7..cda68c9c4fa 100644--- a/studio/backend/tests/test_mcp_stdio_pr5863.py+++ b/studio/backend/tests/test_mcp_stdio_pr5863.py@@ -40,6 +40,7 @@ def _isolate_stdio_env():# apply_stdio_mcp_loopback_default() mutates os.environ and a module flag that# monkeypatch can't roll back, and stdio_mcp_enabled() reads the process tool# policy; snapshot/restore all three so nothing leaks between tests or files.+from state import tool_policysaved = os.environ.get("UNSLOTH_STUDIO_ALLOW_STDIO_MCP")@@ -350,6 +351,7 @@ def test_cleared_env_after_auto_default_falls_back_to_host_default(monkeypatch):def test_disable_tools_overrides_loopback_default(monkeypatch):# When stdio is on only via the loopback auto-default, --disable-tools (the# only way tool policy is False on a loopback bind) turns it back off.+from state import tool_policy_disable(monkeypatch)@@ -363,6 +365,7 @@ def test_explicit_env_opt_in_survives_external_default_policy(monkeypatch):# `UNSLOTH_STUDIO_ALLOW_STDIO_MCP=1 unsloth studio run -H 0.0.0.0` with no# --enable-tools: tool policy is False by the external-host default, not by# --disable-tools, so the explicit env opt-in must still win.+from state import tool_policymonkeypatch.setenv("UNSLOTH_STUDIO_ALLOW_STDIO_MCP", "1")@@ -374,6 +377,7 @@ def test_explicit_env_opt_in_survives_external_default_policy(monkeypatch):def test_explicit_env_opt_in_beats_disable_tools_on_loopback(monkeypatch):# An operator who hand-sets =1 before launch outranks --disable-tools even on# loopback: apply_ leaves the auto-default inactive, so the veto doesn't apply.+from state import tool_policymonkeypatch.setenv("UNSLOTH_STUDIO_ALLOW_STDIO_MCP", "1")@@ -386,6 +390,7 @@ def test_explicit_env_opt_in_beats_disable_tools_on_loopback(monkeypatch):def test_non_false_tool_policy_defers_to_env(monkeypatch, policy):# Only an explicit --disable-tools (False) gates stdio; None/True fall through# to the env var so the gate keeps its normal meaning.+from state import tool_policytool_policy.set_tool_policy(policy)@@ -410,9 +415,10 @@ def test_probe_timeout_matrix():def test_validate_url_gate_off_rejects_stdio(monkeypatch):- _disable(monkeypatch)from routes.mcp_servers import _validate_url+ _disable(monkeypatch)+assert _validate_url("https://example.com/mcp") == "https://example.com/mcp"# urlparse reads "localhost:8000" scheme as "localhost", so it lands here too.for bad in [@@ -431,9 +437,10 @@ def test_validate_url_gate_off_rejects_stdio(monkeypatch):def test_validate_url_gate_off_message_depends_on_whitespace(monkeypatch):# The message names a command only when the value has whitespace, and# never says "desktop app only" (self-hosted can opt in via the env var).- _disable(monkeypatch)from routes.mcp_servers import _validate_url+ _disable(monkeypatch)+with pytest.raises(HTTPException) as exc:_validate_url("npx -y @modelcontextprotocol/server-filesystem /tmp")cmd = exc.value.detail.lower()@@ -449,9 +456,10 @@ def test_validate_url_gate_off_message_depends_on_whitespace(monkeypatch):def test_validate_url_gate_on_accepts_stdio(monkeypatch):- _enable(monkeypatch)from routes.mcp_servers import _validate_url+ _enable(monkeypatch)+assert _validate_url("npx -y server /tmp") == "npx -y server /tmp"# http still works when stdio is onassert _validate_url("https://x/mcp") == "https://x/mcp"@@ -473,9 +481,9 @@ def test_validate_url_gate_on_accepts_stdio(monkeypatch):def test_create_route_gate(tmp_path, monkeypatch, transport):import asyncio+ import routes.mcp_servers as routes_mcpfrom models.mcp_servers import McpServerCreate- import routes.mcp_servers as routes_mcp_reset_db(tmp_path, monkeypatch)payload = McpServerCreate(display_name = "FS", url = "npx -y server /tmp")@@ -492,9 +500,9 @@ def test_create_route_gate(tmp_path, monkeypatch, transport):def test_update_http_to_stdio_blocked_when_off(tmp_path, monkeypatch):import asyncio+ import routes.mcp_servers as routes_mcpfrom models.mcp_servers import McpServerUpdate- import routes.mcp_servers as routes_mcp_reset_db(tmp_path, monkeypatch)_disable(monkeypatch)@@ -511,9 +519,9 @@ def test_update_http_to_stdio_blocked_when_off(tmp_path, monkeypatch):def test_test_route_gate(tmp_path, monkeypatch, transport):import asyncio+ import routes.mcp_servers as routes_mcpfrom models.mcp_servers import McpServerTestRequest- import routes.mcp_servers as routes_mcp_reset_db(tmp_path, monkeypatch)req = McpServerTestRequest(url = "npx -y server /tmp")@@ -532,7 +540,6 @@ def test_test_route_gate(tmp_path, monkeypatch, transport):def test_refresh_route_gate(tmp_path, monkeypatch, transport):import asyncio-import routes.mcp_servers as routes_mcp_reset_db(tmp_path, monkeypatch)diff --git a/studio/backend/tests/test_media_auto_switch.py b/studio/backend/tests/test_media_auto_switch.pyindex 7bc68780faf..91a2a1cab2b 100644--- a/studio/backend/tests/test_media_auto_switch.py+++ b/studio/backend/tests/test_media_auto_switch.py@@ -33,6 +33,12 @@ import utils.openai_auto_switch_settings as settingsfrom auth.authentication import get_current_subjectfrom core.inference.openai_auto_download import preferred_quantfrom utils.api_errors import install_api_error_handlers+from routes.video import router as video_router+import core.inference.diffusion as diffusion_module+import core.inference.diffusion_families as families+import core.inference.llama_keepwarm as chat+import core.inference.media_keepwarm as keepwarm+import core.inference.video as video_moduledef _a_real_video_family(name = "wan2.2-ti2v-5b"):@@ -93,6 +99,26 @@ def _info()+def _gguf_info(+ *args,+ model_format = "gguf",+ **kwargs,+):+ """_info for a GGUF model entry."""+ return _info(*args, model_format = model_format, **kwargs)+++def _gguf_image_info(+ *args,+ model_format = "gguf",+ source = "hf_cache",+ task = mas.IMAGE_TASK,+ **kwargs,+):+ """_info for a cached GGUF image model."""+ return _info(*args, model_format = model_format, source = source, task = task, **kwargs)++def _hf_cache_repo(root, repo_id, *, files):"""A minimal HF cache repo: ``models--org--name/snapshots/<sha>/<file> -> ../../blobs/<sha>``.@@ -187,15 +213,7 @@ def test_a_cached_gguf_repo_resolves_to_its_repo_id(catalog, tmp_path):repo_dir, _snapshot = _hf_cache_repo(tmp_path, "unsloth/Z-Image-Turbo-GGUF", files = ["z-image-turbo-Q4_K_S.gguf"])- catalog.append(- _info(- "unsloth/Z-Image-Turbo-GGUF",- repo_dir,- task = mas.IMAGE_TASK,- model_format = "gguf",- source = "hf_cache",- )- )+ catalog.append(_gguf_image_info("unsloth/Z-Image-Turbo-GGUF", repo_dir))pick = mas.resolve_local_media_model("unsloth/Z-Image-Turbo-GGUF", task = mas.IMAGE_TASK)@@ -365,15 +383,7 @@ def flux(catalog, tmp_path):def cached_gguf(catalog, tmp_path):"""A GGUF repo in the HF cache, whose snapshot entries are symlinks into blobs/."""repo, _snapshot = _hf_cache_repo(tmp_path, "city96/FLUX.1-dev-gguf", files = ["f-Q4_K_M.gguf"])- catalog.append(- _info(- "city96/FLUX.1-dev-gguf",- repo,- task = mas.IMAGE_TASK,- model_format = "gguf",- source = "hf_cache",- )- )+ catalog.append(_gguf_image_info("city96/FLUX.1-dev-gguf", repo))return repo@@ -383,11 +393,10 @@ def two_bpw(catalog, tmp_path):for bpw in ("3.53", "3.97"):(tmp_path / f"z-IQ4_XS-{bpw}bpw.gguf").write_bytes(b"")catalog.append(- _info(+ _gguf_info(f"z-IQ4_XS-{bpw}bpw",tmp_path / f"z-IQ4_XS-{bpw}bpw.gguf",task = mas.IMAGE_TASK,- model_format = "gguf",))return tmp_path@@ -563,9 +572,6 @@ def test_a_companion_base_repo_does_not_count_as_serving(def test_the_video_route_switches_before_it_touches_the_backend(monkeypatch):- import core.inference.video as video_module- from routes.video import router as video_router-calls: list = []async def _switch_stub(@@ -606,11 +612,10 @@ def test_a_sibling_loose_gguf_is_not_treated_as_already_serving(for quant in ("Q4_K_M", "Q8_0"):(tmp_path / f"z-image-{quant}.gguf").write_bytes(b"")catalog.append(- _info(+ _gguf_info(f"z-image-{quant}",tmp_path / f"z-image-{quant}.gguf",task = mas.IMAGE_TASK,- model_format = "gguf",))backend.repo_id = str(tmp_path)@@ -791,15 +796,7 @@ def test_an_unverifiable_download_plan_refuses_rather_than_loading(# The video planner reports zero bytes when its own metadata call failed, because its normal# caller falls back to an inline pull. Zero there is "unknown", not "nothing to fetch".repo, _snapshot = _hf_cache_repo(tmp_path, "unsloth/Wan2.2-GGUF", files = ["wan-Q4_K_M.gguf"])- catalog.append(- _info(- "unsloth/Wan2.2-GGUF",- repo,- task = mas.VIDEO_TASK,- model_format = "gguf",- source = "hf_cache",- )- )+ catalog.append(_gguf_image_info("unsloth/Wan2.2-GGUF", repo, task = mas.VIDEO_TASK))monkeypatch.setattr(backend, "download_plan", lambda model_path, **kw: {"total_bytes": 0, "plan_failed": True})@@ -949,15 +946,7 @@ def test_a_cache_tree_inside_a_scan_folder_still_loads_by_repo_id(catalog, tmp_prepo, _snapshot = _hf_cache_repo(tmp_path, "unsloth/Z-Image-Turbo-GGUF", files = ["z-Q4_K_S.gguf"])- catalog.append(- _info(- "unsloth/Z-Image-Turbo-GGUF",- repo,- task = mas.IMAGE_TASK,- model_format = "gguf",- source = "custom",- )- )+ catalog.append(_gguf_image_info("unsloth/Z-Image-Turbo-GGUF", repo, source = "custom"))pick = mas.resolve_local_media_model("unsloth/Z-Image-Turbo-GGUF", task = mas.IMAGE_TASK)@@ -1047,7 +1036,6 @@ def test_setup_keeps_the_gate_and_lock_after_the_caller_gives_up():# Shielding alone let the caller unwind both contexts while setup was still before# begin_load, so a newly admitted generation could be cut short by the orphaned switch.- import core.inference.media_keepwarm as keepwarmstarted = asyncio.Event()release = asyncio.Event()@@ -1143,11 +1131,10 @@ def test_a_ref2va_checkpoint_expects_its_own_partition(catalog, enabled, tmp_pat# keyframe default rejected the checkpoint that had just loaded.(tmp_path / "minimax_h3_ref2va-Q4_K_M.gguf").write_bytes(b"")catalog.append(- _info(+ _gguf_info("minimax_h3_ref2va-Q4_K_M",tmp_path / "minimax_h3_ref2va-Q4_K_M.gguf",task = mas.VIDEO_TASK,- model_format = "gguf",))backend.repo_id = str(tmp_path)@@ -1204,8 +1191,6 @@ def test_every_backend_call_the_video_route_makes_exists_on_the_backend():def test_the_video_load_route_records_provenance_without_raising(monkeypatch):# The provenance call is made positionally from both load routes, so a signature that drifts# from them 500s every load after the background work has already been accepted.- import core.inference.video as video_module- from routes.video import router as video_routerbackend = _video_load_backend(validate_load_request = lambda *a, **k: _a_real_video_family(),@@ -1318,7 +1303,6 @@ def test_a_stalled_gate_does_not_pin_the_switch_lock():# A gate held elsewhere must not keep the setup task, and with it the switch lock, alive# past the budget: acquisition happens before the non-cancellable phase.- import core.inference.media_keepwarm as keepwarmmonkeypatch.setattr(mas, "_SWITCH_BUDGET_S", 0.4)keepwarm._TRACKERS[arb.VIDEO].gate.acquire()@@ -1353,7 +1337,6 @@ def test_two_switches_on_different_backends_do_not_refuse_each_other(def test_the_ltx23_extras_check_names_the_exact_companions(tmp_path, monkeypatch):# The extras repo also holds checkpoints, so any-weight-file evidence proves nothing about# the three variant-specific companions the assembly reads.- import core.inference.diffusion_families as familiesimport core.inference.video_ltx2 as ltx2import core.inference.video_families as video_families@@ -1380,7 +1363,6 @@ def test_the_ltx23_extras_check_names_the_exact_companions(tmp_path, monkeypatchdef test_the_chat_probe_counts_a_parked_switcher_once(monkeypatch):# A waiter is marked inside its own switch, so counting both discounted it twice and an# active chat stream read as idle, which the final gated drain no longer re-checks.- import core.inference.llama_keepwarm as chatmonkeypatch.setattr(chat, "other_inference_request_count", lambda **kw: 2)with mas.note_switcher(arb.DIFFUSION), mas.note_switcher(arb.VIDEO):with mas.note_waiter(arb.VIDEO):@@ -1390,7 +1372,6 @@ def test_the_chat_probe_counts_a_parked_switcher_once(monkeypatch):def test_a_sharded_encoder_without_its_index_is_incomplete(monkeypatch):# One shard and no index is an interrupted pull of a repo that is always sharded, so# from_pretrained would fetch the index and the rest.- import core.inference.diffusion_families as familiesmonkeypatch.setattr(families, "_upstream_is_cached", lambda *a, **k: True)monkeypatch.setattr(locality, "_cached_snapshot_file", lambda repo, name: None)@@ -1437,7 +1418,6 @@ def test_a_model_unloaded_during_resolution_is_not_reported_as_resident(def test_the_images_route_switches_before_it_checks_what_is_loaded(monkeypatch):- import core.inference.diffusion as diffusion_moduleimport core.inference.image_gallery as gallery_modulefrom routes.inference import router@@ -1592,8 +1572,6 @@ def test_the_video_route_refuses_what_the_target_cannot_serve():# begin_generate would say the same thing, but only after the switch had evicted the# resident model and spent minutes loading a target that was never going to serve this.- import core.inference.video as video_module- from routes.video import router as video_routergenerated: list = []path, filename = pick@@ -1627,7 +1605,6 @@ def test_the_video_route_refuses_what_the_target_cannot_serve(def test_an_encoder_missing_its_config_is_incomplete(monkeypatch):# Every shard on disk and no config.json still reaches the Hub: the pipeline builds the# encoder with from_pretrained on the whole repository, not on the weights alone.- import core.inference.diffusion_families as familiesmonkeypatch.setattr(families, "_upstream_is_cached", lambda *a, **k: True)monkeypatch.setattr(locality, "_cached_snapshot_file", lambda repo, name: None)@@ -1645,7 +1622,6 @@ def test_a_generically_named_ltx23_checkpoint_is_still_header_checked():# Neither the repo nor the filename carries a family token, so only the loader's# general.architecture fallback resolves LTX and reaches the 2.3 extras check.- import core.inference.video as video_moduleimport core.inference.video_ltx2 as ltx2from core.inference.video_families import detect_video_family@@ -1695,15 +1671,7 @@ def test_a_gguf_the_loader_cannot_open_is_not_advertised(catalog, tmp_path):_repo_dir, snapshot = _hf_cache_repo(tmp_path, "unsloth/Z-Image-Turbo-GGUF", files = ["z-image-turbo-Q4_K_S.gguf"])- catalog.append(- _info(- "unsloth/Z-Image-Turbo-GGUF",- snapshot,- task = mas.IMAGE_TASK,- model_format = "gguf",- source = "hf_cache",- )- )+ catalog.append(_gguf_image_info("unsloth/Z-Image-Turbo-GGUF", snapshot))assert mas.resolve_local_media_model("unsloth/Z-Image-Turbo-GGUF", task = mas.IMAGE_TASK) is Noneassert mas.available_media_model_ids(mas.IMAGE_TASK) == []@@ -1715,7 +1683,6 @@ def test_a_cpu_switch_does_not_hold_the_gates_it_cannot_evict_behind(# A CPU load releases GPU ownership instead of taking it, so waiting on chat's lifecycle gate# let an unrelated teardown time the switch out, and holding it blocked new chat requests for# as long as the re-plan and the load registration took.- import core.inference.llama_keepwarm as chatmonkeypatch.setattr(backends, "load_takes_the_gpu", lambda: False)monkeypatch.setattr(mas, "load_takes_the_gpu", lambda: False)@@ -1736,8 +1703,6 @@ def test_a_cpu_switch_does_not_hold_the_gates_it_cannot_evict_behind(def test_the_video_route_refuses_a_flow_control_the_target_does_not_expose(monkeypatch):# LTX-2 exposes no audio_flow_shift, and _resolve_flow_shifts would only say so after the# switch had evicted the resident pipeline and loaded the target.- import core.inference.video as video_module- from routes.video import router as video_routergenerated: list = []@@ -1846,8 +1811,6 @@ def test_a_sharded_component_missing_a_shard_is_refused(catalog, enabled, tmp_padef test_a_native_h3_target_refuses_an_audio_shift_sd_cpp_cannot_apply(monkeypatch):# A MiniMax-H3 GGUF always loads through sd.cpp, which derives the audio schedule against a# fixed shift, so the engine rule is knowable before the switch rather than only after it.- import core.inference.video as video_module- from routes.video import router as video_routergenerated: list = []@@ -2007,8 +1970,6 @@ def test_a_single_file_hidream_still_checks_its_encoder(def test_a_native_h3_target_refuses_max_reference_sizing(monkeypatch):# stable-diffusion.cpp scales every reference to the generation's pixel area, so 'max' needs# the Diffusers engine, and an H3 GGUF is always native.- import core.inference.video as video_module- from routes.video import router as video_routergenerated: list = []@@ -2080,7 +2041,6 @@ def test_a_hosted_modular_component_is_checked_against_the_cache(# load_components pulls each repository the modular index names, and the video planner omits# its base manifest whenever the local path exists, so a hosted component that is not on# disk would be downloaded after the resident pipeline had already gone.- import core.inference.diffusion as diffusion_modulecache = tmp_path / "hub"component = cache / "models--unsloth--MiniMax-H3" / "snapshots" / ("a" * 40) / "vae"@@ -2157,7 +2117,6 @@ def test_a_stalled_load_probe_still_answers_inside_the_budget(flux, enabled, bacdef test_setup_that_never_registers_gives_the_gates_back(flux, enabled, backend, monkeypatch):# A first-run native install runs for minutes before begin_load, and holding both media# admission gates and chat's that long blocks every unrelated request.- import core.inference.media_keepwarm as keepwarmmonkeypatch.setattr(mas, "_SWITCH_BUDGET_S", 0.3)monkeypatch.setattr(mas, "_SETUP_GRACE_S", 0.5)@@ -2198,7 +2157,6 @@ def test_a_superseded_snapshot_does_not_vouch_for_the_active_one():# refs/main names the revision from_pretrained resolves to, so a complete component in an# older snapshot must not answer for the partial one the load will actually read.- import core.inference.diffusion as diffusion_modulecache = tmp_path / "hub"repo = cache / "models--unsloth--MiniMax-H3"@@ -2231,7 +2189,6 @@ def test_a_root_level_hosted_component_needs_more_than_one_shard():# A component named as a whole repo went through _upstream_is_cached, whose no-manifest# branch is satisfied by a single weight file an interrupted sharded pull leaves behind.- import core.inference.diffusion as diffusion_modulecache = tmp_path / "hub"snapshot = cache / "models--unsloth--MiniMax-H3-VAE" / "snapshots" / ("a" * 40)@@ -2264,11 +2221,10 @@ def test_a_split_gguf_missing_a_shard_is_not_advertised(catalog, tmp_path):# present, so half a split set would evict the resident model and then fail at startup.(tmp_path / "z-image-Q4_K_M-00001-of-00002.gguf").write_bytes(b"")catalog.append(- _info(+ _gguf_info("z-image",tmp_path / "z-image-Q4_K_M-00001-of-00002.gguf",task = mas.IMAGE_TASK,- model_format = "gguf",))@@ -2285,7 +2241,6 @@ def test_a_pinned_component_revision_is_the_one_checked():# ComponentSpec.load is handed the whole spec, so a pinned revision is what gets fetched;# a complete default snapshot must not answer for it.- import core.inference.diffusion as diffusion_modulecache = tmp_path / "hub"repo = cache / "models--unsloth--MiniMax-H3"@@ -2318,7 +2273,6 @@ def test_a_pinned_component_variant_needs_its_own_weights():# from_pretrained asks for the named variant's files rather than falling back to the# default ones, so a component holding only the default weights is not complete for it.- import core.inference.diffusion as diffusion_modulecache = tmp_path / "hub"component = cache / "models--unsloth--MiniMax-H3" / "snapshots" / ("a" * 40) / "vae"@@ -2395,7 +2349,6 @@ def test_a_shard_index_declaring_nothing_is_not_proof(catalog, enabled, tmp_pathdef test_a_cached_split_gguf_missing_a_shard_is_not_advertised(catalog, tmp_path, monkeypatch):# An active cache is loaded by repo id, which skipped the split check entirely, while the# planners look only at the selected first shard and report nothing missing.- import core.inference.diffusion as diffusion_modulerepo_dir, snapshot = _hf_cache_repo(tmp_path,@@ -2403,15 +2356,7 @@ def test_a_cached_split_gguf_missing_a_shard_is_not_advertised(catalog, tmp_pathfiles = ["z-image-Q4_K_M-00001-of-00002.gguf"],)monkeypatch.setattr(diffusion_module, "hub_cache_dir", lambda: str(tmp_path))- catalog.append(- _info(- "unsloth/Z-Image-Turbo-GGUF",- repo_dir,- task = mas.IMAGE_TASK,- model_format = "gguf",- source = "hf_cache",- )- )+ catalog.append(_gguf_image_info("unsloth/Z-Image-Turbo-GGUF", repo_dir))assert mas.resolve_local_media_model("unsloth/Z-Image-Turbo-GGUF", task = mas.IMAGE_TASK) is None@@ -2428,7 +2373,6 @@ def test_chat_admitted_before_the_gate_still_stops_the_switch():# A chat request that passed the lifecycle gate after the outer drain's last probe is# already running when the switch takes that gate, and the GPU handoff would terminate it.- import core.inference.llama_keepwarm as chatmonkeypatch.setattr(mas, "_DRAIN_WAIT_S", 0.3)outer = {"done": False}@@ -2478,12 +2422,7 @@ def test_two_h3_partitions_of_one_quant_are_told_apart(catalog, enabled, tmp_patname = f"minimax_h3_{partition}-Q4_K_M.gguf"(tmp_path / name).write_bytes(b"")catalog.append(- _info(- f"minimax_h3_{partition}-Q4_K_M",- tmp_path / name,- task = mas.VIDEO_TASK,- model_format = "gguf",- )+ _gguf_info(f"minimax_h3_{partition}-Q4_K_M", tmp_path / name, task = mas.VIDEO_TASK))backend.repo_id = str(tmp_path)backend.gguf_variant = "Q4_K_M"@@ -2516,8 +2455,6 @@ def _capture_begin_load(monkeypatch, route):monkeypatch.setattr(router_module, "select_and_activate_engine", lambda *a, **k: _Backend())monkeypatch.setattr(router_module, "get_active_diffusion_engine", lambda: _Backend())else:- import core.inference.video as video_module-backend = _video_load_backend(validate_load_request = lambda *a, **k: _a_real_video_family(),begin_load = _begin_load,@@ -2564,14 +2501,7 @@ def test_a_non_h3_sibling_stays_in_the_ambiguity_group(catalog, enabled, tmp_pat# for the non-H3 one, so neither may be treated as already serving the other.for name in ("minimax_h3_fl2va-Q4_K_M.gguf", "wan-Q4_K_M.gguf"):(tmp_path / name).write_bytes(b"")- catalog.append(- _info(- name.removesuffix(".gguf"),- tmp_path / name,- task = mas.VIDEO_TASK,- model_format = "gguf",- )- )+ catalog.append(_gguf_info(name.removesuffix(".gguf"), tmp_path / name, task = mas.VIDEO_TASK))backend.repo_id = str(tmp_path)backend.gguf_variant = "Q4_K_M"backend.model_kind = "gguf"@@ -2598,7 +2528,6 @@ def test_a_media_request_parked_on_a_gate_is_not_read_as_running_chat_work(monke# gate: the in-gate drain discounted it on the media side but chat_busy(count_pending=False)# read the same blocked request as running chat work, so an otherwise idle switch answered# 409 and loaded nothing. Driven with the real gates and the real middleware.- import core.inference.llama_keepwarm as chatclass _Backend:def status(self):diff --git a/studio/backend/tests/test_media_family_assembler_offline.py b/studio/backend/tests/test_media_family_assembler_offline.pyindex 1ec9d716a5c..6ec1ae03c7a 100644--- a/studio/backend/tests/test_media_family_assembler_offline.py+++ b/studio/backend/tests/test_media_family_assembler_offline.py@@ -220,10 +220,10 @@ def _drive_krea(with_transformer = True,):"""Assemble a Krea pipeline against fakes, recording what each component was asked for."""- from core.inference.diffusion_krea2 import load_krea2_pipeline-import huggingface_hub+ from core.inference.diffusion_krea2 import load_krea2_pipeline+index = tmp_path / "model_index.json"index.write_text(json.dumps({"patch_size": 2}), encoding = "utf-8")seen: dict = {}diff --git a/studio/backend/tests/test_media_keepwarm.py b/studio/backend/tests/test_media_keepwarm.pyindex 2f39b576625..2257c5629be 100644--- a/studio/backend/tests/test_media_keepwarm.py+++ b/studio/backend/tests/test_media_keepwarm.py@@ -19,6 +19,7 @@ import pytestimport core.inference.gpu_arbiter as arbimport core.inference.media_keepwarm as mkimport utils.openai_auto_switch_settings as settings+from core.inference.llama_keepwarm import LlamaKeepWarmMiddlewareclass _FakeEngine:@@ -620,7 +621,6 @@ def test_a_load_that_has_not_registered_yet_is_not_unloaded(media, monkeypatch):# and the page silently rolls the pick back. The window has to be closed, not narrowed,# so the tick is pinned to the exact moment the route has started and the backend still# reports nothing loading.- from core.inference.llama_keepwarm import LlamaKeepWarmMiddlewaremonkeypatch.setattr(settings, "get_media_auto_unload_idle_seconds", lambda: 60)_step()@@ -649,8 +649,6 @@ def test_a_load_that_has_not_registered_yet_is_not_unloaded(media, monkeypatch):def test_the_middleware_counts_a_generation_against_its_backend(media, monkeypatch):- from core.inference.llama_keepwarm import LlamaKeepWarmMiddleware-monkeypatch.setattr(settings, "get_media_auto_unload_idle_seconds", lambda: 60)_step()_idle(arb.DIFFUSION)@@ -679,7 +677,6 @@ def test_a_cancelled_wait_on_the_media_gate_leaves_no_chat_request_behind(media,# process-wide chat count positive for good: chat idle unload would never fire again and# every training start would go on being told an inference request was running.import core.inference.llama_keepwarm as lk- from core.inference.llama_keepwarm import LlamaKeepWarmMiddlewaremonkeypatch.setattr(lk, "_inflight", 0)monkeypatch.setattr(lk, "_pending", 0)@@ -715,8 +712,6 @@ def test_a_cancelled_wait_on_the_media_gate_leaves_no_chat_request_behind(media,def test_an_unauthenticated_probe_does_not_keep_the_pipeline_warm(media, monkeypatch):- from core.inference.llama_keepwarm import LlamaKeepWarmMiddleware-monkeypatch.setattr(settings, "get_media_auto_unload_idle_seconds", lambda: 60)_step()_idle(arb.DIFFUSION)@@ -743,8 +738,6 @@ def _stalled_media_request(path, headers):dependency runs, so nothing downstream ever produces a status for it."""async def _run():- from core.inference.llama_keepwarm import LlamaKeepWarmMiddleware-started = asyncio.Event()async def _app(scope, receive, send):diff --git a/studio/backend/tests/test_memory_estimate.py b/studio/backend/tests/test_memory_estimate.pyindex 17c758b52c1..1dc5090b618 100644--- a/studio/backend/tests/test_memory_estimate.py+++ b/studio/backend/tests/test_memory_estimate.py@@ -961,7 +961,6 @@ class TestEstimateMemoryRoute:# function the interpreter can switch out of: a concurrent pop makes it raise# KeyError, a concurrent insert RuntimeError, and neither is caught between# there and the worker, so it surfaces as a 500 on a slider drag.- import inspectfor source in (inspect.getsource(ri._gguf_resident_file_gb),inspect.getsource(ri._cached_estimate_config),@@ -987,7 +986,6 @@ class TestEstimateMemoryRoute:# so 50 estimates were 250 /proc scans and 250 retained atexit handlers at# 120 ms each. Pricing a load must not be able to kill a server.import atexit- import inspectfrom core.inference.llama_cpp import LlamaCppBackend@@ -1026,7 +1024,6 @@ class TestEstimateMemoryRoute:def test_the_inert_probe_mode_is_opt_in(self):# Default True, so every existing caller -- above all the real backend that# owns the llama-server child -- keeps exactly the behaviour it has today.- import inspectfrom core.inference.llama_cpp import LlamaCppBackenddiff --git a/studio/backend/tests/test_metal_paravirtual_guard.py b/studio/backend/tests/test_metal_paravirtual_guard.pyindex 7cb0f4dd25d..4b7e13089cd 100644--- a/studio/backend/tests/test_metal_paravirtual_guard.py+++ b/studio/backend/tests/test_metal_paravirtual_guard.py@@ -381,8 +381,7 @@ def test_the_drafter_pin_falls_back_before_it_gives_up(caps, expected):def test_a_failed_probe_does_not_cost_the_user_their_drafter():"""The drop half of the same decision: an unanswered probe must not drop."""- drafter, _extras, _warnings = _drafter_gate(- paravirtual = True,+ drafter, _extras, _warnings = _paravirtual_gate(caps = {"spec_draft_ngl_flag": None, "mtp_probe_inconclusive": True},drafter = "/m/mtp-model.gguf",extra_args = None,@@ -714,6 +713,17 @@ def _drafter_gate(return scope["launch_mtp_draft_path"], scope["extra_args"], log.warnings+def _paravirtual_gate(+ *args,+ caps = {},+ drafter = None,+ paravirtual = True,+ **kwargs,+):+ """_drafter_gate on a paravirtual host with no drafter and no probed caps."""+ return _drafter_gate(*args, caps = caps, drafter = drafter, paravirtual = paravirtual, **kwargs)++@pytest.fixture(autouse = True)def _no_inherited_draft_env(monkeypatch):"""The drafter parsers fall back to os.environ, so a stray var on the host must not@@ -744,17 +754,13 @@ def test_a_drafter_that_cannot_be_pinned_is_dropped(monkeypatch):# the caller's own speculative tuning for nothing.monkeypatch.setenv("LLAMA_ARG_SPEC_DRAFT_MODEL", "/models/env.gguf")tuning = ["--spec-draft-n-max", "6"]- drafter, extras, warnings = _drafter_gate(- paravirtual = True, caps = {}, drafter = None, extra_args = tuning- )+ drafter, extras, warnings = _paravirtual_gate(extra_args = tuning)assert warnings == []assert extras == tuning# But an env drafter the extras DO keep alive still drops: their --spec-type is what# stops the scrub.owned = ["--spec-type", "draft-simple"]- drafter, _extras, warnings = _drafter_gate(- paravirtual = True, caps = {}, drafter = None, extra_args = owned- )+ drafter, _extras, warnings = _paravirtual_gate(extra_args = owned)assert any("draft-layer flag" in w for w in warnings), warnings@@ -762,9 +768,7 @@ def test_the_drop_takes_a_user_owned_drafter_with_it():"""A user --spec-type makes _build_speculative_flags emit nothing, so clearing onlyUnsloth's resolved path would leave their --model-draft on the device."""extras = ["--spec-type", "draft-simple", "--model-draft", "/models/d.gguf", "--top-k", "40"]- drafter, out, warnings = _drafter_gate(- paravirtual = True, caps = {}, drafter = None, extra_args = extras- )+ drafter, out, warnings = _paravirtual_gate(extra_args = extras)assert drafter is Noneassert warningsassert llama_cpp._extra_args_mtp_draft_path(out, {}) is None@@ -833,12 +837,7 @@ def test_an_inherited_drafter_env_is_not_exempted_by_a_drafter_free_mode(monkeyp"""Same through the env the child reads directly: the drafter loads whatever--spec-type says, so it cannot ride out the drop on the mode alone."""monkeypatch.setenv("LLAMA_ARG_SPEC_DRAFT_MODEL", "/models/env.gguf")- drafter, _out, warnings = _drafter_gate(- paravirtual = True,- caps = {},- drafter = None,- extra_args = ["--spec-type", "ngram-mod"],- )+ drafter, _out, warnings = _paravirtual_gate(extra_args = ["--spec-type", "ngram-mod"])assert drafter is Noneassert warnings@@ -1019,9 +1018,7 @@ def test_a_load_with_no_separate_drafter_is_unaffected():--gpu-layers 0."""for caps in ({}, {"spec_draft_ngl_flag": "--spec-draft-ngl"}):extras = ["--spec-type", "draft-mtp", "--spec-draft-n-max", "2"]- drafter, out, warnings = _drafter_gate(- paravirtual = True, caps = caps, drafter = None, extra_args = extras- )+ drafter, out, warnings = _paravirtual_gate(caps = caps, extra_args = extras)assert drafter is Noneassert out == extrasassert warnings == []@@ -1322,6 +1319,23 @@ def _target_state(backend, gguf, **overrides):return backend.adopt_load_intent_if_matched(llama_cpp.GgufLoadIntent(**kwargs))+def _target_state_auto(+ *args,+ gpu_layers = -1,+ gpu_memory_mode = "auto",+ speculative_type = "auto",+ **kwargs,+):+ """_target_state for a full-offload auto load, the shape every case here starts from."""+ return _target_state(+ *args,+ gpu_layers = gpu_layers,+ gpu_memory_mode = gpu_memory_mode,+ speculative_type = speculative_type,+ **kwargs,+ )++def _gpu_pin_recorders():"""Every `if` in load_model that writes self._gpu_ids, in source order. load_modelrecords the pin more than once, so judging one site would miss a later overwrite."""@@ -1829,12 +1843,9 @@ def test_a_repeat_auto_request_with_extras_matches_the_cpu_server_it_left(monkey)assert _route_matches(request, backend) is Trueassert (- _target_state(+ _target_state_auto(backend,gguf,- speculative_type = "auto",- gpu_memory_mode = "auto",- gpu_layers = -1,tensor_parallel = True,n_cpu_moe = 8,extra_args = ["-ngl", "99", "--top-k", "40"],@@ -1850,17 +1861,7 @@ def test_the_same_pair_still_mismatches_on_a_real_mac(monkeypatch, tmp_path):backend, gguf = _cpu_server(monkeypatch, tmp_path, launched_extras = ["--top-k", "40"])request = _load_request(gguf, llama_extra_args = ["--top-k", "40"])assert _route_matches(request, backend) is False- assert (- _target_state(- backend,- gguf,- speculative_type = "auto",- gpu_memory_mode = "auto",- gpu_layers = -1,- extra_args = ["--top-k", "40"],- )- is False- )+ assert _target_state_auto(backend, gguf, extra_args = ["--top-k", "40"]) is Falsedef test_a_genuinely_different_extras_box_still_reloads(monkeypatch, tmp_path):@@ -1870,17 +1871,7 @@ def test_a_genuinely_different_extras_box_still_reloads(monkeypatch, tmp_path):backend, gguf = _cpu_server(monkeypatch, tmp_path, launched_extras = ["--top-k", "40"])request = _load_request(gguf, llama_extra_args = ["--top-k", "20"])assert _route_matches(request, backend) is False- assert (- _target_state(- backend,- gguf,- speculative_type = "auto",- gpu_memory_mode = "auto",- gpu_layers = -1,- extra_args = ["--top-k", "20"],- )- is False- )+ assert _target_state_auto(backend, gguf, extra_args = ["--top-k", "20"]) is Falsedef test_a_tensor_split_mode_in_extras_does_not_reload_a_cpu_server(monkeypatch, tmp_path):@@ -1892,15 +1883,7 @@ def test_a_tensor_split_mode_in_extras_does_not_reload_a_cpu_server(monkeypatch,request = _load_request(gguf, llama_extra_args = ["-sm", "tensor"], tensor_parallel = True)assert _route_matches(request, backend) is Trueassert (- _target_state(- backend,- gguf,- speculative_type = "auto",- gpu_memory_mode = "auto",- gpu_layers = -1,- tensor_parallel = True,- extra_args = ["-sm", "tensor"],- )+ _target_state_auto(backend, gguf, tensor_parallel = True, extra_args = ["-sm", "tensor"])is True)@@ -1916,17 +1899,7 @@ def test_a_dropped_drafter_does_not_reload_over_the_extras_it_rewrote(monkeypatc)request = _load_request(gguf, llama_extra_args = list(asked))assert _route_matches(request, backend) is True- assert (- _target_state(- backend,- gguf,- speculative_type = "auto",- gpu_memory_mode = "auto",- gpu_layers = -1,- extra_args = list(asked),- )- is True- )+ assert _target_state_auto(backend, gguf, extra_args = list(asked)) is Truedef test_an_apply_that_inherits_the_extras_does_not_reload_the_rewritten_server(@@ -1964,15 +1937,7 @@ def test_an_edited_spec_flag_still_reloads_after_a_dropped_drafter(monkeypatch,request = _load_request(gguf, llama_extra_args = ["--draft-max", "4", "--top-k", "40"])assert _route_matches(request, backend) is Falseassert (- _target_state(- backend,- gguf,- speculative_type = "auto",- gpu_memory_mode = "auto",- gpu_layers = -1,- extra_args = ["--draft-max", "4", "--top-k", "40"],- )- is False+ _target_state_auto(backend, gguf, extra_args = ["--draft-max", "4", "--top-k", "40"]) is False)diff --git a/studio/backend/tests/test_mla_kv_cache_symmetry.py b/studio/backend/tests/test_mla_kv_cache_symmetry.pyindex 0829da55f60..d914cb5965e 100644--- a/studio/backend/tests/test_mla_kv_cache_symmetry.py+++ b/studio/backend/tests/test_mla_kv_cache_symmetry.py@@ -46,6 +46,7 @@ if not hasattr(sys.modules["structlog"], "get_logger"):sys.modules["structlog"].get_logger = _structlog_stub.get_loggerfrom core.inference.llama_cpp import LlamaCppBackend # noqa: E402+import inspectQUANT_KV = ["llama-server",@@ -189,8 +190,6 @@ class TestTheSignalStudioAlreadyHas:"""The target and the drafter are separate models with separate contexts,so every reset site gates each side on its own metadata rather thanreusing the target's answer for both."""- import inspect-src = inspect.getsource(LlamaCppBackend.load_model)assert src.count("mla = self._target_kv_symmetry()") >= 2assert src.count("draft_mla = self._draft_kv_symmetry(") == src.count(@@ -367,8 +366,6 @@ class TestTheDraftSignalComesFromTheLaunchCommand:def test_every_call_site_passes_a_command(self):"""A bare _draft_kv_symmetry() would silently read no drafter at all."""- import inspect-src = inspect.getsource(LlamaCppBackend.load_model)assert "self._draft_kv_symmetry()" not in srcassert src.count("self._draft_kv_symmetry(") == src.count("self._target_kv_symmetry()")@@ -385,7 +382,6 @@ class TestLoadModelNeverReadsEnvBeforeItExists:def test_env_is_never_loaded_before_it_is_assigned(self):import ast- import inspectimport textwrapfn = ast.parse(textwrap.dedent(inspect.getsource(LlamaCppBackend.load_model)))diff --git a/studio/backend/tests/test_mlx_inference_backend.py b/studio/backend/tests/test_mlx_inference_backend.pyindex 001a5411308..d0642c0b190 100644--- a/studio/backend/tests/test_mlx_inference_backend.py+++ b/studio/backend/tests/test_mlx_inference_backend.py@@ -15,6 +15,12 @@ from types import SimpleNamespaceimport pytest+class _Resp:+ def __init__(self, text, tok):+ self.text = text+ self.token = tok++class _DummyMetal:@staticmethoddef is_available():@@ -97,9 +103,10 @@ class _AdapterTree:def test_temporary_mlx_adapter_state_bypasses_and_restores_wrappers(monkeypatch):- _install_fake_mlx(monkeypatch)from core.inference.mlx_inference import _temporary_mlx_adapter_state+ _install_fake_mlx(monkeypatch)+base = object()wrapper = SimpleNamespace(lora_a = object(), lora_b = object(), linear = base, m = object())model = _AdapterTree({"model.layers.0.proj": wrapper})@@ -137,14 +144,14 @@ def test_temporary_mlx_adapter_state_validates_requests():def test_temporary_mlx_adapter_state_uses_real_mlx_module_tree():+ from core.inference.mlx_inference import _temporary_mlx_adapter_state+nn = pytest.importorskip("mlx.nn")pytest.importorskip("mlx_lm")from mlx_lm.models.switch_layers import SwitchLinearfrom mlx_lm.tuner.dora import DoRALinearfrom mlx_lm.tuner.lora import LoRAEmbedding, LoRALinear, LoRASwitchLinear- from core.inference.mlx_inference import _temporary_mlx_adapter_state-class _Layer(nn.Module):def __init__(self):super().__init__()@@ -179,12 +186,12 @@ def test_temporary_mlx_adapter_state_uses_real_mlx_module_tree():def test_mlx_inference_text_load_forwards_studio_settings(monkeypatch):+ from core.inference.mlx_inference import MLXInferenceBackend+_install_fake_mlx(monkeypatch)calls = []_install_fake_fast_mlx(monkeypatch, calls)- from core.inference.mlx_inference import MLXInferenceBackend-backend = MLXInferenceBackend()config = SimpleNamespace(identifier = "fake/text", is_vision = False, is_lora = False)@@ -219,12 +226,12 @@ def test_mlx_inference_text_load_forwards_studio_settings(monkeypatch):def test_mlx_text_lora_record_keeps_base_model_for_native_template(monkeypatch):# A LoRA adapter's own tokenizer often ships no chat template; the native tool-calling template# lives on the base model.+ from core.inference.mlx_inference import MLXInferenceBackend+_install_fake_mlx(monkeypatch)calls = []_install_fake_fast_mlx(monkeypatch, calls)- from core.inference.mlx_inference import MLXInferenceBackend-backend = MLXInferenceBackend()config = SimpleNamespace(identifier = "fake/text-adapter",@@ -243,6 +250,8 @@ def test_mlx_text_lora_record_keeps_base_model_for_native_template(monkeypatch):def test_mlx_inference_vlm_lora_uses_unsloth_loader_without_native_adapter_rewrite(monkeypatch, tmp_path):+ from core.inference.mlx_inference import MLXInferenceBackend+_install_fake_mlx(monkeypatch)calls = []_install_fake_fast_mlx(monkeypatch, calls)@@ -260,8 +269,6 @@ def test_mlx_inference_vlm_lora_uses_unsloth_loader_without_native_adapter_rewrioriginal_cfg = '{"base_model_name_or_path": "fake/base", "rank": 8}\n'cfg_path.write_text(original_cfg)- from core.inference.mlx_inference import MLXInferenceBackend-backend = MLXInferenceBackend()config = SimpleNamespace(identifier = str(adapter_dir),@@ -298,10 +305,11 @@ def test_mlx_inference_vlm_lora_uses_unsloth_loader_without_native_adapter_rewridef test_mlx_inference_distributed_vlm_forwards_group_to_fast_mlx(monkeypatch):+ from core.inference.mlx_inference import MLXInferenceBackend+_install_fake_mlx(monkeypatch)calls = []_install_fake_fast_mlx(monkeypatch, calls)- from core.inference.mlx_inference import MLXInferenceBackendgroup = SimpleNamespace(size = lambda: 2, rank = lambda: 0)config = SimpleNamespace(identifier = "fake/vlm", is_vision = True, is_lora = False)@@ -553,8 +561,8 @@ def test_worker_share_object_oversize_notifies_peers(monkeypatch):def test_mlx_generate_chat_response_accepts_template_kwargs():- import inspectfrom core.inference.mlx_inference import MLXInferenceBackend+ import inspectsig = inspect.signature(MLXInferenceBackend.generate_chat_response)params = sig.parameters@@ -575,6 +583,7 @@ def test_mlx_vlm_reemits_think_prefill_inside_adapter_context(monkeypatch):the UI renders the thinking block during prefill and a pre-first-tokencancel does not drop it. Mirrors _generate_text."""from core.inference import mlx_inference+ from core.inference.mlx_inference import MLXInferenceBackendMLXInferenceBackend = mlx_inference.MLXInferenceBackend@@ -631,6 +640,7 @@ def test_mlx_vlm_reemits_think_prefill_inside_adapter_context(monkeypatch):def test_mlx_vlm_generation_selects_renderer_by_capability(monkeypatch):from core.inference import mlx_inference+ from core.inference.mlx_inference import MLXInferenceBackendMLXInferenceBackend = mlx_inference.MLXInferenceBackend@@ -721,9 +731,10 @@ def test_mlx_vlm_generation_selects_renderer_by_capability(monkeypatch):def test_mlx_vlm_image_injection_reuses_media_aliases(monkeypatch):# Moved to chat_template_helpers so the transformers vision path shares it (#10092).- from core.inference.chat_template_helpers import prompt_serializes_structured_mediafrom core.inference.mlx_inference import MLXInferenceBackend+ from core.inference.chat_template_helpers import prompt_serializes_structured_media+media = [{"type": "image"}]quoted = [{"role": "user", "content": media}, {"role": "user", "content": f"Explain {media}"}]assert prompt_serializes_structured_media(f"<image>\n{media[0]}", quoted[:1])@@ -770,8 +781,11 @@ def test_mlx_vlm_model_config_prefers_config_with_model_type():def test_mlx_generate_text_forwards_kwargs_into_template_helper(monkeypatch):"""Mac text path must route through apply_chat_template_for_generation soreasoning / tool kwargs reach the tokenizer."""- _install_fake_mlx(monkeypatch)from core.inference import mlx_inference+ from core.inference.mlx_inference import MLXInferenceBackend+ import types as _types++ _install_fake_mlx(monkeypatch)MLXInferenceBackend = mlx_inference.MLXInferenceBackendreal_adapter_state = mlx_inference._temporary_mlx_adapter_state@@ -792,7 +806,6 @@ def test_mlx_generate_text_forwards_kwargs_into_template_helper(monkeypatch):# mlx_lm.stream_generate yields response objects with .token; use a# one-token generator so _generate_text returns without the real stack.- import types as _typesmlx_lm_pkg = _types.ModuleType("mlx_lm")mlx_lm_sample = _types.ModuleType("mlx_lm.sample_utils")@@ -899,9 +912,10 @@ def test_mlx_generate_text_forwards_kwargs_into_template_helper(monkeypatch):def test_mlx_text_normalizes_native_reasoning_and_close_releases_lock(monkeypatch):- _install_fake_mlx(monkeypatch)from core.inference.mlx_inference import MLXInferenceBackend+ _install_fake_mlx(monkeypatch)+monkeypatch.setattr("core.inference.chat_template_helpers.apply_chat_template_for_generation",lambda *_args, **_kwargs: "prompt",@@ -921,11 +935,6 @@ def test_mlx_text_normalizes_native_reasoning_and_close_releases_lock(monkeypatcmlx_lm_sample.make_sampler = lambda **_kw: object()mlx_lm_sample.make_logits_processors = lambda **_kw: None- class _Resp:- def __init__(self, text, tok):- self.text = text- self.token = tok-def _stream_generate(_model, _tokenizer, **_kw):yield _Resp("<|channel>thought\n", 10)yield _Resp("r", 11)@@ -965,9 +974,10 @@ def test_mlx_text_post_tool_prompt_opens_reasoning_channel(monkeypatch):outside reasoning would leak the post-tool reasoning and a raw ``<channel|>``into the visible answer."""- _install_fake_mlx(monkeypatch)from core.inference.mlx_inference import MLXInferenceBackend+ _install_fake_mlx(monkeypatch)+post_tool_prompt = ("<|turn>model\n<|tool_response>response:web_search{}<tool_response|><|channel>thought\n")@@ -992,11 +1002,6 @@ def test_mlx_text_post_tool_prompt_opens_reasoning_channel(monkeypatch):mlx_lm_sample.make_sampler = lambda **_kw: object()mlx_lm_sample.make_logits_processors = lambda **_kw: None- class _Resp:- def __init__(self, text, tok):- self.text = text- self.token = tok-def _stream_generate(_model, _tokenizer, **_kw):yield _Resp("The search says 18C.", 10)yield _Resp("<channel|>", 11)@@ -1045,9 +1050,10 @@ def test_mlx_text_post_tool_prompt_opens_reasoning_channel(monkeypatch):def test_mlx_text_native_metadata_preserves_prefilled_think_snapshots(monkeypatch):- _install_fake_mlx(monkeypatch)from core.inference.mlx_inference import MLXInferenceBackend+ _install_fake_mlx(monkeypatch)+monkeypatch.setattr("core.inference.chat_template_helpers.apply_chat_template_for_generation",lambda *_args, **_kwargs: "prompt<think>\n",@@ -1067,11 +1073,6 @@ def test_mlx_text_native_metadata_preserves_prefilled_think_snapshots(monkeypatcmlx_lm_sample.make_sampler = lambda **_kw: object()mlx_lm_sample.make_logits_processors = lambda **_kw: None- class _Resp:- def __init__(self, text, tok):- self.text = text- self.token = tok-def _stream_generate(_model, _tokenizer, **_kw):yield _Resp("reason", 10)yield _Resp("</think>", 11)@@ -1102,9 +1103,10 @@ def test_mlx_text_native_metadata_preserves_prefilled_think_snapshots(monkeypatcdef test_mlx_vlm_normalizes_native_reasoning_channels(monkeypatch):- _install_fake_mlx(monkeypatch)from core.inference.mlx_inference import MLXInferenceBackend+ _install_fake_mlx(monkeypatch)+monkeypatch.setattr("core.inference.chat_template_helpers.apply_chat_template_for_generation",lambda *_args, **_kwargs: "prompt",@@ -1113,11 +1115,6 @@ def test_mlx_vlm_normalizes_native_reasoning_channels(monkeypatch):mlx_vlm_pkg = types.ModuleType("mlx_vlm")- class _Resp:- def __init__(self, text, tok):- self.text = text- self.token = tok-def _stream_generate(_model, _processor, _prompt, _images, **_kw):yield _Resp("<|channel>thought\n", 10)yield _Resp("vision", 11)@@ -1152,9 +1149,10 @@ def test_mlx_vlm_normalizes_native_reasoning_channels(monkeypatch):def test_mlx_vlm_post_tool_prompt_opens_reasoning_channel(monkeypatch):"""The VLM snapshot path must derive channel state from its rendered prompt too."""- _install_fake_mlx(monkeypatch)from core.inference.mlx_inference import MLXInferenceBackend+ _install_fake_mlx(monkeypatch)+post_tool_prompt = "<|tool_response>response:web_search{}<tool_response|><|channel>thought\n"monkeypatch.setattr("core.inference.chat_template_helpers.apply_chat_template_for_generation",@@ -1164,11 +1162,6 @@ def test_mlx_vlm_post_tool_prompt_opens_reasoning_channel(monkeypatch):mlx_vlm_pkg = types.ModuleType("mlx_vlm")- class _Resp:- def __init__(self, text, tok):- self.text = text- self.token = tok-def _stream_generate(_model, _processor, _prompt, _images, **_kw):yield _Resp("looking at it", 10)yield _Resp("<channel|>", 11)@@ -1290,9 +1283,10 @@ def test_mlx_prompt_cache_max_bytes_budget(monkeypatch):def test_mlx_prompt_cache_never_returns_empty_remainder(monkeypatch):- _install_fake_prompt_cache_api(monkeypatch)from core.inference.mlx_inference import _MLXPromptCacheHistory+ _install_fake_prompt_cache_api(monkeypatch)+history = _MLXPromptCacheHistory(6, 1 << 30)tokens = list(range(10))cache, rest = history.fetch(object(), "key", tokens)@@ -1317,9 +1311,10 @@ def test_mlx_prompt_cache_never_returns_empty_remainder(monkeypatch):def test_mlx_prompt_cache_key_isolates_adapter_state(monkeypatch):+ from core.inference.mlx_inference import MLXInferenceBackend+_install_fake_prompt_cache_api(monkeypatch)_install_fake_mlx(monkeypatch)- from core.inference.mlx_inference import MLXInferenceBackendclass _Tok:bos_token = None@@ -1354,9 +1349,9 @@ def _install_fake_text_stack(captured,markers = None,):- import types as _types-from core.inference import mlx_inference+ from core.inference.mlx_inference import MLXInferenceBackend+ import types as _types_install_fake_mlx(monkeypatch)monkeypatch.setattr(@@ -1426,8 +1421,6 @@ def _install_fake_text_stack():return "".join(str(i) for i in ids)- from core.inference.mlx_inference import MLXInferenceBackend-backend = MLXInferenceBackend()backend._model = object()backend._tokenizer = _Tok()@@ -1511,10 +1504,11 @@ def test_mlx_presence_penalty_latches_the_first_decode_step():def test_mlx_prompt_cache_survives_reset_but_not_unload(monkeypatch):+ from core.inference.mlx_inference import MLXInferenceBackend+_install_fake_prompt_cache_api(monkeypatch)_install_fake_mlx(monkeypatch)sys.modules["mlx.core"].clear_cache = lambda: None- from core.inference.mlx_inference import MLXInferenceBackendbackend = MLXInferenceBackend()backend.active_model_name = "model-a"@@ -1529,9 +1523,10 @@ def test_mlx_prompt_cache_survives_reset_but_not_unload(monkeypatch):def test_mlx_prompt_cache_skips_entries_over_budget(monkeypatch):- _install_fake_prompt_cache_api(monkeypatch)from core.inference.mlx_inference import _MLXPromptCacheHistory+ _install_fake_prompt_cache_api(monkeypatch)+history = _MLXPromptCacheHistory(6, 1000)history.insert("key", [1, 2, 3], [_FakeCacheEntry(offset = 3, nbytes = 400)])assert len(history._lru.entries.get("key", {})) == 1@@ -1543,9 +1538,10 @@ def test_mlx_prompt_cache_skips_entries_over_budget(monkeypatch):def test_mlx_prompt_cache_keys_on_what_the_kv_covers(monkeypatch):- _install_fake_prompt_cache_api(monkeypatch)from core.inference.mlx_inference import _MLXPromptCacheHistory+ _install_fake_prompt_cache_api(monkeypatch)+class _Entry:def __init__(self,@@ -1566,6 +1562,8 @@ def test_mlx_prompt_cache_keys_on_what_the_kv_covers(monkeypatch):def test_mlx_prompt_cache_only_stores_verifiable_prefix_coverage(monkeypatch):+ from core.inference.mlx_inference import _MLXPromptCacheHistory+mx = pytest.importorskip("mlx.core")from mlx_lm.models.cache import CacheList, ChunkedKVCache, KVCache, RotatingKVCache@@ -1608,6 +1606,8 @@ def test_mlx_prompt_cache_only_stores_verifiable_prefix_coverage(monkeypatch):def test_mlx_prompt_cache_covers_hybrid_recurrent_layouts(monkeypatch):+ from core.inference.mlx_inference import _MLXPromptCacheHistory+mx = pytest.importorskip("mlx.core")from mlx_lm.models.cache import (ArraysCache,@@ -1904,11 +1904,12 @@ def test_mlx_registered_renderer_accepts_published_nemotron_model_type_case():"""The official checkpoint capitalizes its model type while mlx-vlm'sregistry uses lowercase. Unsloth must reach the registered renderer ratherthan rejecting the checkpoint before the loader's normalization can run."""+ from core.inference.mlx_inference import _render_registered_vlm_prompt+# Real mlx-vlm and Zoo, like the renderer contract test above. Bare# backend CI ships neither, so skip rather than error.pytest.importorskip("mlx_vlm.prompt_utils")loader = pytest.importorskip("unsloth_zoo.mlx.loader")- from core.inference.mlx_inference import _render_registered_vlm_promptloader._ensure_vlm_prompt_utils_patched()published = "NemotronH_Nano_Omni_Reasoning_V3"@@ -2188,9 +2189,8 @@ def test_mlx_audio_input_honors_adapter_selection(monkeypatch):def test_worker_forwards_use_adapter_on_the_audio_command():"""The wire carries the selection only when the caller set it."""- import inspect-from core.inference import worker+ import inspectsrc = inspect.getsource(worker._handle_generate_audio_input)assert 'cmd.get("use_adapter")' in src@@ -2309,9 +2309,10 @@ def test_kv_quant_probe_reports_what_the_runtime_would_really_do(monkeypatch):Static proxies were wrong both ways: a declared head_dim a model ignores,and windows spelled differently from `max_size`."""+ from core.inference import mlx_inference+pytest.importorskip("mlx_lm")from mlx_lm.models import cache as lm_cache- from core.inference import mlx_inferencedef elig(factory, dim = 128):lm = _tiny_lm(factory, dim)@@ -2446,6 +2447,7 @@ def test_chat_template_override_crosses_both_ipc_hops():"""The backend runs in a subprocess; the parent rebuilds its entry from anenumerated key set at each hop, and /status and the reload check read theparent's copy."""+ from core.inference import workerimport inspectfrom core.inference import orchestrator, worker@@ -2856,6 +2858,8 @@ def test_a_successful_override_does_not_pin_the_tokenizer_past_load(monkeypatch)Nothing reads them once the audio and image checks have run, and the workeroutlives the model, so holding them defeats part of what unload releases."""+ from core.inference.mlx_inference import MLXInferenceBackend+_install_fake_mlx(monkeypatch)_install_fake_fast_mlx(monkeypatch, [])# A text model with no template of its own is the one case an override may create.@@ -2864,7 +2868,6 @@ def test_a_successful_override_does_not_pin_the_tokenizer_past_load(monkeypatch)"core.inference.chat_template_helpers.apply_chat_template_for_generation",lambda target, messages, **kwargs: "rendered",)- from core.inference.mlx_inference import MLXInferenceBackendbackend = MLXInferenceBackend()config = SimpleNamespace(identifier = "fake/text", is_vision = False, is_lora = False)@@ -2971,6 +2974,8 @@ def test_a_vision_override_is_checked_even_when_the_native_render_needs_recoveryunplaced with no error, and recovery is unavailable anyway once tools orreasoning controls are set."""+ from core.inference import mlx_inference+_install_fake_mlx(monkeypatch)_install_fake_fast_mlx(monkeypatch, [])monkeypatch.setattr(_DummyProcessor, "chat_template", "{{ native }}", raising = False)@@ -2991,7 +2996,6 @@ def test_a_vision_override_is_checked_even_when_the_native_render_needs_recoverymonkeypatch.setattr("core.inference.chat_template_helpers.apply_chat_template_for_generation", render)- from core.inference import mlx_inference# The native template fails the check, which used to skip the override's.assert mlx_inference._image_marker_survives(_DummyTokenizer(), _DummyProcessor(), None) is False@@ -3088,9 +3092,10 @@ def test_mlx_processors_penalize_in_range_ids_and_route_strays_away(factory_name, factory_args, vocab, sequence, expected):# MLX does no bounds checking, so a stray id is undefined behaviour.- mx = pytest.importorskip("mlx.core")from core.inference import mlx_inference+ mx = pytest.importorskip("mlx.core")+proc = getattr(mlx_inference, factory_name)(*factory_args)proc(mx.array([10, 11]), mx.zeros((1, vocab))) # first call latches prompt_lenout = proc(mx.array(sequence), mx.zeros((1, vocab)))@@ -3215,6 +3220,8 @@ def test_rng_capture_reinterprets_signed_words(monkeypatch, words, expected):finally and replace the probe's own outcome, the failure shape #9478 set outto remove."""+ from core.inference import mlx_inference+mlx_inference, seeded = _fake_rng_state(monkeypatch, words)captured = mlx_inference._mlx_rng_key_words()@@ -3236,6 +3243,8 @@ def test_rng_capture_declines_words_that_are_not_32_bit(monkeypatch, words):unprobed run. Declining is the outcome the caller already handles, and it isthe only one that says so out loud."""+ from core.inference import mlx_inference+mlx_inference, seeded = _fake_rng_state(monkeypatch, words)warnings = _capture_rng_warnings(monkeypatch, mlx_inference)@@ -3253,6 +3262,8 @@ def test_rng_capture_declines_words_that_are_not_32_bit(monkeypatch, words):def _capture_rng_warnings(monkeypatch, mlx_inference):"""Collect this module's warnings. It logs through structlog, which caplogdoes not see."""+ from core.inference import mlx_inference+warnings = []monkeypatch.setattr(mlx_inference.logger,@@ -3383,6 +3394,7 @@ def _recording_mx(monkeypatch, synchronize = None):def test_a_stream_that_cannot_be_drained_does_not_stop_the_caller(monkeypatch):"""A plain mx.new_stream raises when synchronized off its creating thread."""+ from core.inference import mlx_inferencedef synchronize(stream = None):if stream == "foreign":@@ -3398,6 +3410,8 @@ def test_a_stream_that_cannot_be_drained_does_not_stop_the_caller(monkeypatch):def test_a_module_getattr_that_raises_does_not_stop_the_caller(monkeypatch):+ from core.inference import mlx_inference+mlx_inference, mx, events = _recording_mx(monkeypatch)module = types.ModuleType("mlx_vlm.generate")module.__getattr__ = lambda name: (_ for _ in ()).throw(RuntimeError(name))@@ -3410,6 +3424,8 @@ def test_a_module_getattr_that_raises_does_not_stop_the_caller(monkeypatch):def test_one_stream_shared_by_several_modules_is_drained_once(monkeypatch):"""0.6.x defines the stream once and re-exports it from every candidate name."""+ from core.inference import mlx_inference+mlx_inference, mx, events = _recording_mx(monkeypatch)for name in ("mlx_vlm.generate", "mlx_vlm.generate.dispatch", "mlx_vlm.generate.ar"):_fake_stream_module(monkeypatch, name, "shared")@@ -3420,6 +3436,8 @@ def test_one_stream_shared_by_several_modules_is_drained_once(monkeypatch):def test_the_speculative_decoding_stream_is_drained_too(monkeypatch):+ from core.inference import mlx_inference+mlx_inference, mx, events = _recording_mx(monkeypatch)_fake_stream_module(monkeypatch, "mlx_vlm.speculative.common", "speculative")@@ -3504,10 +3522,11 @@ def _count_route(**fields,):"""Drive the endpoint against `backend`, classifying from a real template."""+ from routes import inference as route+backend_dir = str(Path(__file__).resolve().parent.parent)if backend_dir not in sys.path:sys.path.insert(0, backend_dir)- from routes import inference as routebackend.models = models or _mirror(template)monkeypatch.setattr(route, "get_inference_backend", lambda: backend)@@ -3522,6 +3541,15 @@ def _count_route()+def _count_hi(+ *args,+ messages = [{"role": "user", "content": "hi"}],+ **kwargs,+):+ """_count_route over the one-message chat every case counts."""+ return _count_route(*args, messages = messages, **kwargs)++def test_an_mlx_count_is_served_where_llama_cpp_would_have_refused(monkeypatch):"""llama.cpp not being loaded used to be the whole answer, for vision models too."""from fastapi import HTTPException@@ -3588,6 +3616,8 @@ def test_an_mlx_count_prices_the_tools_the_completion_would_render(def test_an_mlx_count_prices_the_relay_the_tool_loop_did_not_claim(monkeypatch):"""A declined request carrying tool history goes to the relay, which keeps thestructured tool_calls the extraction flattens away."""+ from routes import inference as route+fn = {"name": "web_search", "arguments": '{"q": "x"}'}call = {"id": "c1", "type": "function", "function": fn}history = [@@ -3682,9 +3712,10 @@ def test_the_load_policy_bounds_a_pin_only_where_the_bound_can_be_enforced(monke"""mlx-lm cannot quantize a rotating cache (to_quantized raises, from the first token),so exactly one applies. A pin is an explicit memory instruction; a self-chosen windowis not. An unenforceable pin buys nothing, so it does not spend the quantization."""- pytest.importorskip("mlx_lm.models.cache")from core.inference import mlx_inferencefrom core.inference.mlx_inference import MLXInferenceBackend++ pytest.importorskip("mlx_lm.models.cache")from mlx_lm.models.cache import KVCache# About the pin/enforceability combination, not the eligibility probe.@@ -3745,8 +3776,9 @@ def test_the_bound_is_checked_on_a_real_cache_at_the_size_that_was_asked_for():"""make_cache ignores max_kv_size, and those caches range from constant-state tounbounded, so the argument does not say whether it applied. Nor does anarchitecture-chosen cap serve a narrower request."""- pytest.importorskip("mlx_lm.models.cache")from core.inference.mlx_inference import _kv_window_enforced++ pytest.importorskip("mlx_lm.models.cache")from mlx_lm.models.cache import ArraysCache, ChunkedKVCache, KVCache, RotatingKVCachehonours = SimpleNamespace(layers = [object(), object()])@@ -3824,9 +3856,9 @@ def test_the_probe_reads_a_cache_shape_without_needing_the_mlx_wheels(monkeypatcdef test_the_window_reaches_the_runtime_on_every_generation_route(monkeypatch):"""The runtimes read max_kv_size only when no prompt_cache is passed, so each routebuilding its own cache carries the bound. The audio route once did not."""- import types as _types-from core.inference.mlx_inference import MLXInferenceBackend+ from core.inference.mlx_inference import _MLXPromptCacheHistory+ import types as _typeskwargs = MLXInferenceBackend._kv_window_generate_kwargsassert kwargs(SimpleNamespace(_kv_cache_window = None)) == {}@@ -3835,7 +3867,6 @@ def test_the_window_reaches_the_runtime_on_every_generation_route(monkeypatch):# The prompt-cache history builds its own cache; it has to carry the window too.pytest.importorskip("mlx_lm.models.cache")- from core.inference.mlx_inference import _MLXPromptCacheHistorymodel = SimpleNamespace(layers = [object(), object()])bounded = _MLXPromptCacheHistory(4, 1 << 20, 512).fetch(model, "k", [1, 2])[0]@@ -3954,13 +3985,7 @@ def test_an_mlx_count_prices_the_current_date_the_completion_prepends(monkeypatc# No API key, which is what makes the prompt Studio's to compose.interactive = SimpleNamespace(headers = Headers({}), query_params = {}, cookies = {})backend = _RenderRecordingBackend()- _count_route(- monkeypatch,- backend,- template = _PLAIN_TEMPLATE,- request = interactive,- messages = [{"role": "user", "content": "hi"}],- )+ _count_hi(monkeypatch, backend, template = _PLAIN_TEMPLATE, request = interactive)from routes.inference import current_date_prompt_lineline = current_date_prompt_line(request = interactive)@@ -3979,11 +4004,10 @@ def test_an_mlx_count_prices_the_archive_tool_and_its_compaction_nudge(monkeypatmonkeypatch.setattr(tool_policy, "_tool_policy_default", True)monkeypatch.setattr(route, "_thread_has_conversation_archive", lambda tid: bool(tid))backend = _RenderRecordingBackend()- _count_route(+ _count_hi(monkeypatch,backend,template = _TOOL_TEMPLATE,- messages = [{"role": "user", "content": "hi"}],enabled_tools = ["web_search"],thread_id = "thread-with-an-archive",)@@ -4002,9 +4026,10 @@ def test_an_mlx_count_prices_the_archive_tool_and_its_compaction_nudge(monkeypatdef test_an_mlx_count_prices_the_date_an_api_key_tool_loop_still_gets(monkeypatch):"""`_wants_current_date` is false for an API-key request, but the tool-loop completionreapplies it with include_api_key, so a count that did not would be short that line."""- from starlette.datastructures import Headersfrom state import tool_policy+ from starlette.datastructures import Headers+monkeypatch.setattr(tool_policy, "_tool_policy_default", True)keyed = SimpleNamespace(headers = Headers({"authorization": "Bearer sk-unsloth-test"}),@@ -4012,13 +4037,12 @@ def test_an_mlx_count_prices_the_date_an_api_key_tool_loop_still_gets(monkeypatccookies = {},)backend = _RenderRecordingBackend()- _count_route(+ _count_hi(monkeypatch,backend,template = _TOOL_TEMPLATE,request = keyed,enabled_tools = ["web_search"],- messages = [{"role": "user", "content": "hi"}],)from routes.inference import current_date_prompt_line@@ -4044,7 +4068,6 @@ def test_an_mlx_count_is_dropped_when_a_same_model_reload_lands_under_it(monkeyp"""The active name cannot see a same-ID reload, which is how a template override lands.A count routed from the old entry must not be published."""from fastapi import HTTPException- from routes import inference as routebackend = _RenderRecordingBackend()backend.load_generation = 7@@ -4067,17 +4090,11 @@ def test_an_mlx_count_yields_to_a_generation_that_started_while_it_prepared(monk"""Everything between admission and the tokenizer awaits, so a chat starting in the gapwould wait behind this count for the orchestrator lock. GGUF re-checks; this must too."""from fastapi import HTTPException- from routes import inference as routebackend = _RenderRecordingBackend()counts = iter([0, 1]) # admitted at the entry check, busy by the last checkpointwith pytest.raises(HTTPException) as excinfo:- _count_route(- monkeypatch,- backend,- generations = lambda: next(counts, 1),- messages = [{"role": "user", "content": "hi"}],- )+ _count_hi(monkeypatch, backend, generations = lambda: next(counts, 1))assert excinfo.value.status_code == 503assert "generation is in progress" in str(excinfo.value.detail)@@ -4085,9 +4102,8 @@ def test_an_mlx_count_yields_to_a_generation_that_started_while_it_prepared(monkdef test_the_mlx_mcp_snapshot_is_taken_under_the_same_guard_the_gguf_count_uses():"""The MCP handlers hold this guard across the row change and the schema-cacheinvalidation, so a snapshot outside it can pair a new row with a stale schema."""- import inspect-from routes import inference as route+ import inspectbody = inspect.getsource(route._mlx_count_chat_tokens)assert "mcp_server_snapshot_guard" in body, "the MLX snapshot is unguarded"diff --git a/studio/backend/tests/test_mlx_repair.py b/studio/backend/tests/test_mlx_repair.pyindex ae9e782114e..8018d8f3fa5 100644--- a/studio/backend/tests/test_mlx_repair.py+++ b/studio/backend/tests/test_mlx_repair.py@@ -22,6 +22,11 @@ if str(_BACKEND) not in sys.path:import utils.mlx_repair as mr # noqa: E402+class _Result:+ returncode = 0+ stdout = ""++@pytest.fixture(autouse = True)def _reset_attempt_guard(monkeypatch):monkeypatch.setattr(mr, "_attempted", False)@@ -120,10 +125,6 @@ def test_repair_install_pins_transformers_and_cleans_up(monkeypatch):monkeypatch.setattr(mr, "_transformers_constraint_args", _spy_args)monkeypatch.setattr(mr, "_uv_executable", lambda: "/usr/bin/uv")- class _Result:- returncode = 0- stdout = ""-def _fake_run(cmd, **kwargs):captured["cmd"] = cmdcaptured["env"] = kwargs.get("env")@@ -160,10 +161,6 @@ def test_install_requires_prebuilt_wheels(monkeypatch):pytest.importorskip("transformers")captured = {}- class _Result:- returncode = 0- stdout = ""-monkeypatch.setattr(mr, "_uv_executable", lambda: "/usr/bin/uv")monkeypatch.setattr(mr.subprocess, "run", lambda cmd, **k: captured.update(cmd = cmd) or _Result()@@ -216,10 +213,6 @@ def test_install_env_drops_secrets_and_source_redirects(monkeypatch):def test_repair_rejects_inadequate_stack(monkeypatch):# A successful uv run that still leaves an old/missing mlx-vlm must NOT clear# chat-only: attempt_mlx_repair returns False so Train/Export stay disabled.- class _Result:- returncode = 0- stdout = ""-monkeypatch.setattr(mr.subprocess, "run", lambda *a, **k: _Result())monkeypatch.setattr(mr, "mlx_stack_available", lambda: False)assert mr.attempt_mlx_repair() is False@@ -229,10 +222,6 @@ def test_inadequate_stack_warning_names_the_floors_not_the_install_pins(monkeypa# The gate this message reports on is mlx_stack_available(), which tests the# floors. Quoting the install pins instead would tell an operator running a# perfectly usable mlx 0.33 that they need exactly 0.32.1.- class _Result:- returncode = 0- stdout = ""-warnings = []# Pin both, or this test measures the host. attempt_mlx_repair returns early# when _uv_executable() finds nothing, long before the message under test, so@@ -254,10 +243,6 @@ def test_inadequate_stack_warning_names_the_floors_not_the_install_pins(monkeypadef test_repair_invalidates_import_caches_before_stack_check(monkeypatch):events = []- class _Result:- returncode = 0- stdout = ""-def _stack_available():events.append("check")assert events == ["invalidate", "check"]@@ -354,6 +339,8 @@ def test_disable_env_skips(monkeypatch):def test_apple_silicon_missing_mlx_starts_repair_and_redetects(monkeypatch):+ import utils.hardware.hardware as hw+import threadingmonkeypatch.setattr(mr, "is_apple_silicon", lambda: True)@@ -371,8 +358,6 @@ def test_apple_silicon_missing_mlx_starts_repair_and_redetects(monkeypatch):# and capture that re-detection is invoked on success.monkeypatch.setattr(mr, "attempt_mlx_repair", _fake_repair)- import utils.hardware.hardware as hw-monkeypatch.setattr(hw, "detect_hardware", lambda: redetected.__setitem__("called", True))started = mr.start_mlx_autorepair_if_needed()@@ -581,6 +566,7 @@ def _join_the_repair_worker():def test_a_stack_that_measures_usable_overturns_the_verdict(monkeypatch):# The #9120 shape: chat-only cached from a race the warm has since finished importing.+import utils.hardware.hardware as hwmonkeypatch.setattr(mr, "is_apple_silicon", lambda: True)diff --git a/studio/backend/tests/test_mlx_stack_blockers.py b/studio/backend/tests/test_mlx_stack_blockers.pyindex aa32a08ad55..531890da2cc 100644--- a/studio/backend/tests/test_mlx_stack_blockers.py+++ b/studio/backend/tests/test_mlx_stack_blockers.py@@ -290,10 +290,10 @@ def test_a_malformed_installed_version_is_bounded_too(monkeypatch):# it can name a package the install has since put there.def _fake_hardware(monkeypatch, calls: list[str]):"""Stand the real hardware module's re-detection down, keeping the module identity."""- from contextlib import nullcontext-from utils.hardware import hardware as hw+ from contextlib import nullcontext+monkeypatch.setattr(hw, "detect_hardware", lambda: calls.append("detect"))monkeypatch.setattr(hw, "owning_detection_epoch", lambda epoch: nullcontext())monkeypatch.setattr(hw, "current_detection_epoch", lambda: None)diff --git a/studio/backend/tests/test_mmproj_placement_policy.py b/studio/backend/tests/test_mmproj_placement_policy.pyindex 51295f4ac55..dcb7bba2c48 100644--- a/studio/backend/tests/test_mmproj_placement_policy.py+++ b/studio/backend/tests/test_mmproj_placement_policy.py@@ -28,6 +28,7 @@ from core.inference.llama_cpp import (_AUTO_OFFLOAD_CTX,_resolved_mmproj_offload,)+import utils.models.gguf_metadata as _metafrom models.inference import InferenceStatusResponse, LoadResponsefrom routes.inference import (_estimate_gguf_required_gb,@@ -453,8 +454,6 @@ def test_the_vision_switch_does_not_take_audio_only_projectors_away(off there and must leave it alone. A projector serving both modalities isstill suppressed, because llama.cpp cannot load one modality without theother, and the switch is the user asking for the VRAM back."""- import utils.models.gguf_metadata as _meta-backend, gguf = _backend(tmp_path, memory = [(0, 7_600, 8_192)])with patch.object(_meta, "mmproj_capabilities", lambda _p: (has_audio, accepts_image)):cmd = _launch(backend, gguf, disable_vision = True)["cmd"]@@ -539,8 +538,6 @@ def test_a_remembered_mmproj_auto_does_not_survive_the_vision_switch(tmp_path):def test_an_audio_only_projector_is_not_taken_away_by_the_auto_override(tmp_path):"""The override exists to stop a projector coming back. An audio-only one is kepton purpose, so --no-mmproj-auto must not follow it out the door."""- import utils.models.gguf_metadata as _meta-backend, gguf = _backend(tmp_path, memory = [(0, 7_600, 8_192)])with patch.object(_meta, "mmproj_capabilities", lambda _p: (True, False)):cmd = _launch(backend, gguf, disable_vision = True, extra_args = ["--mmproj-auto"])["cmd"]@@ -553,8 +550,6 @@ def test_an_audio_only_projector_does_not_blame_the_switch_for_images(tmp_path):"""vision_disabled_by_user drives the composer's "you turned it off" message, soon a model with no image encoder it would promise a capability that turning theswitch back on cannot deliver."""- import utils.models.gguf_metadata as _meta-backend, gguf = _backend(tmp_path, memory = [(0, 7_600, 8_192)])with patch.object(_meta, "mmproj_capabilities", lambda _p: (True, False)):_launch(backend, gguf, disable_vision = True)@@ -569,8 +564,6 @@ def test_the_training_guard_still_charges_an_audio_only_projector(tmp_path):let the guard admit a chat load the running training job cannot afford, whichis the direction that costs someone else's job rather than merely annoyingthis user."""- import utils.models.gguf_metadata as _meta-model = tmp_path / "model.gguf"model.write_bytes(b"\x00" * (4 * MIB))mmproj = tmp_path / "mmproj-F16.gguf"@@ -769,8 +762,6 @@ def test_the_vision_switch_does_not_record_an_inherited_audio_encoder(tmp_path,cannot load half of it, so switching vision off takes the audio with it."""backend, gguf = _ambient_mmproj(tmp_path, monkeypatch)- import utils.models.gguf_metadata as _meta-with patch.object(_meta, "mmproj_capabilities", lambda _p: (True, True)):result = _launch(backend, gguf, disable_vision = True)@@ -787,8 +778,6 @@ def test_the_vision_switch_keeps_an_inherited_audio_only_encoder(tmp_path, monke"""backend, gguf = _ambient_mmproj(tmp_path, monkeypatch)- import utils.models.gguf_metadata as _meta-with patch.object(_meta, "mmproj_capabilities", lambda _p: (True, False)):result = _launch(backend, gguf, disable_vision = True)@@ -807,8 +796,6 @@ def test_an_inherited_projector_that_reads_images_still_goes(tmp_path, monkeypatAn image-capable one is exactly what the switch is for."""backend, gguf = _ambient_mmproj(tmp_path, monkeypatch)- import utils.models.gguf_metadata as _meta-with patch.object(_meta, "mmproj_capabilities", lambda _p: (False, True)):result = _launch(backend, gguf, disable_vision = True)@@ -1321,8 +1308,6 @@ def test_the_guard_charges_a_projector_only_the_environment_names(tmp_path, monkbare = _estimate_gguf_required_gb(config, disable_vision = True)monkeypatch.setenv("LLAMA_ARG_MMPROJ", str(ambient))- import utils.models.gguf_metadata as _meta-with patch.object(_meta, "mmproj_capabilities", lambda _p: (True, False)):charged = _estimate_gguf_required_gb(config, disable_vision = True)# An image-capable one is scrubbed out of the child, so it must stay uncharged.@@ -1368,8 +1353,6 @@ def test_a_suppressed_image_projector_hands_the_budget_to_the_inherited_one(tmp_ambient = tmp_path / "ambient-mmproj.gguf"ambient.write_bytes(b"\x00" * (2 * MIB))- import utils.models.gguf_metadata as _meta-def _caps(path):# Configured: images, so the switch drops it. Inherited: audio only, so it stays.return (False, True) if str(path) == str(configured) else (True, False)@@ -1465,8 +1448,6 @@ def test_a_virtualised_metal_device_does_not_keep_the_inherited_projector(tmp_pabackend, gguf = _backend(tmp_path, memory = [])backend._resolve_launch_mmproj_path = lambda **_kw: None- import utils.models.gguf_metadata as _meta-with patch.object(_meta, "mmproj_capabilities", lambda _p: (True, False)):result = _launch(backend, gguf, disable_vision = True, extra_args = ["--mmproj-auto"])@@ -1486,8 +1467,6 @@ def test_dropping_an_inherited_image_projector_points_at_the_switch(tmp_path, mobackend, gguf = _backend(tmp_path, memory = [(0, 7_600, 8_192)])backend._resolve_launch_mmproj_path = lambda **_kw: None- import utils.models.gguf_metadata as _meta-with patch.object(_meta, "mmproj_capabilities", lambda _p: (False, True)):_launch(backend, gguf, disable_vision = True)diff --git a/studio/backend/tests/test_model_cache_snapshot.py b/studio/backend/tests/test_model_cache_snapshot.pyindex 0e9692368c4..3583f9656a8 100644--- a/studio/backend/tests/test_model_cache_snapshot.py+++ b/studio/backend/tests/test_model_cache_snapshot.py@@ -236,6 +236,7 @@ def test_refs_directory_symlink_cannot_escape_cache(tmp_path):def test_training_pin_prefers_a_snapshot_that_has_weights(tmp_path):# refs/main can point at a metadata-only revision while a complete snapshot sits beside it.+from core.training.training import _resolve_model_snapshotrepo_root = _model_repo(tmp_path, "Org/Model")@@ -253,6 +254,7 @@ def test_training_pin_prefers_a_snapshot_that_has_weights(tmp_path):def test_training_pin_still_falls_back_to_metadata_only_snapshots(tmp_path):# With no weights anywhere the pin is unchanged, so the worker's Hub retry still runs.+from core.training.training import _resolve_model_snapshotrepo_root = _model_repo(tmp_path, "Org/Model")@@ -265,6 +267,7 @@ def test_training_pin_skips_a_weights_only_snapshot_without_metadata(tmp_path):# A newer weights-only fetch (interrupted download, or an allow_patterns pull that never took# config.json) must not displace an older complete sibling: the start route rejects a snapshot# with no loader metadata, so picking it 400s a run that used to work.+from core.training.training import _resolve_model_snapshotrepo_root = _model_repo(tmp_path, "Org/Model")@@ -282,6 +285,7 @@ def test_training_pin_skips_a_weights_only_snapshot_without_metadata(tmp_path):def test_training_pin_ignores_weight_names_the_start_route_rejects(tmp_path):# consolidated.safetensors has no transformers loader path and is not in _MODEL_WEIGHT_CANDIDATES,# so treating it as "has weights" selects a snapshot the start route then rejects.+from core.training.training import _resolve_model_snapshotrepo_root = _model_repo(tmp_path, "Org/Model")diff --git a/studio/backend/tests/test_model_ids.py b/studio/backend/tests/test_model_ids.pyindex a4f84d92b03..47d6eb2f0dd 100644--- a/studio/backend/tests/test_model_ids.py+++ b/studio/backend/tests/test_model_ids.py@@ -9,6 +9,7 @@ if str(_BACKEND) not in sys.path:sys.path.insert(0, str(_BACKEND))from core.inference.model_ids import model_id_matches, public_model_id # noqa: E402+from core.inference.model_ids import display_model_namedef test_local_gguf_path_becomes_clean_stem():@@ -80,8 +81,6 @@ def test_matches_clean_and_legacy():def test_display_model_name_uses_the_repo_leaf_not_the_snapshot_sha():- from core.inference.model_ids import display_model_name-posix = ("/home/u/.cache/huggingface/hub/models--unsloth--DeepSeek-V4-Flash-0731-GGUF""/snapshots/57326b941c4603e24d1a5e71c22520c66e086eb8"@@ -98,8 +97,6 @@ def test_display_model_name_uses_the_repo_leaf_not_the_snapshot_sha():def test_display_model_name_leaves_ordinary_ids_alone():- from core.inference.model_ids import display_model_name-assert display_model_name("unsloth/Qwen3-30B-A3B-GGUF") == "Qwen3-30B-A3B-GGUF"assert display_model_name("Qwen3-30B-A3B") == "Qwen3-30B-A3B"assert display_model_name("/srv/models/Qwen3-30B-A3B-Q4_K_M.gguf") == ("Qwen3-30B-A3B-Q4_K_M")@@ -108,8 +105,6 @@ def test_display_model_name_leaves_ordinary_ids_alone():def test_display_model_name_keeps_gguf_on_hub_repo_ids():- from core.inference.model_ids import display_model_name-# A real Hub repo: the suffix is part of the leaf, not an extension to strip.assert display_model_name("lex-au/Orpheus-3b-FT-Q8_0.gguf") == "Orpheus-3b-FT-Q8_0.gguf"# A file inside a repo (>= 2 slashes) is still a file reference.diff --git a/studio/backend/tests/test_model_memory_settings.py b/studio/backend/tests/test_model_memory_settings.pyindex fd42bc5bb27..aaa915664d3 100644--- a/studio/backend/tests/test_model_memory_settings.py+++ b/studio/backend/tests/test_model_memory_settings.py@@ -210,7 +210,6 @@ class TestMemlockLimit:def test_unlimited_reports_none(self, monkeypatch):import resource-import utils.model_memory_settings as mmmonkeypatch.setattr(@@ -223,7 +222,6 @@ class TestMemlockLimit:@pytest.mark.parametrize("soft", [0, 64 * 1024, 8 * 1024 * 1024])def test_finite_limits_are_reported(self, monkeypatch, soft):import resource-import utils.model_memory_settings as mmmonkeypatch.setattr(resource, "getrlimit", lambda _w: (soft, soft))@@ -231,7 +229,6 @@ class TestMemlockLimit:def test_negative_is_treated_as_unlimited(self, monkeypatch):import resource-import utils.model_memory_settings as mmmonkeypatch.setattr(resource, "getrlimit", lambda _w: (-1, -1))@@ -240,7 +237,6 @@ class TestMemlockLimit:@pytest.mark.parametrize("exc", [ValueError, OSError, AttributeError])def test_probe_failure_never_raises(self, monkeypatch, exc):import resource-import utils.model_memory_settings as mmdef boom(_w):@@ -537,10 +533,10 @@ class TestCapabilityProbeFallback:def test_load_mode_flag_survives_a_failed_probe(self, monkeypatch):"""A timed-out or broken --help probe must fall back conservatively,not raise UnboundLocalError and block the load."""- import subprocess-from core.inference.llama_cpp import LlamaCppBackend+ import subprocess+LlamaCppBackend._capability_cache.clear()def boom(*_a, **_k):@@ -574,10 +570,10 @@ class TestCacheInvalidationRace:rest of the TTL, and a load could launch flags contradicting it."""def test_stale_fill_is_dropped(self, monkeypatch):- import threading-import utils.model_memory_settings as mm+ import threading+mm._cache.clear()store = {mm.KEEP_RESIDENT_SETTING_KEY: False}gate = threading.Event()@@ -681,10 +677,10 @@ class TestRacingReadReturnsTheNewValue:write must not hand back the pre-write setting."""def test_a_read_invalidated_mid_flight_is_retried(self, monkeypatch):- import threading-import utils.model_memory_settings as mm+ import threading+mm._cache.clear()store = {mm.KEEP_RESIDENT_SETTING_KEY: False}gate = threading.Event()@@ -883,11 +879,11 @@ class TestFullOffloadDetection:def test_the_manual_branch_really_does_not_set_fully_gpu_offloaded(self):"""Pins the premise. If a later change starts setting it there, thistest fails and the derived check can be simplified away."""+ from core.inference.llama_cpp import LlamaCppBackendimport astimport inspect- import textwrap- from core.inference.llama_cpp import LlamaCppBackend+ import textwrapsource = textwrap.dedent(inspect.getsource(LlamaCppBackend.load_model))tree = ast.parse(source)@@ -1073,9 +1069,10 @@ class TestHostMemoryGate:vulkan_igpu = False,**kwargs,):- import utils.hardwarefrom core.inference.llama_cpp import LlamaCppBackend+ import utils.hardware+monkeypatch.setattr(utils.hardware, "is_apple_silicon", lambda: apple)monkeypatch.setattr(LlamaCppBackend, "_amd_apu_wants_unified_memory", staticmethod(lambda idx = None: amd)@@ -1535,7 +1532,6 @@ class TestTheRetryCanReadTheGate:@staticmethoddef _load_model_ast():import ast- from pathlib import Pathsrc = Path(__file__).resolve().parent.parent / "core" / "inference" / "llama_cpp.py"for node in ast.walk(ast.parse(src.read_text(encoding = "utf-8"))):@@ -1654,11 +1650,11 @@ class TestCpuMoeCountIsParsed:assert _args_place_tensors_on_cpu(extras) is expecteddef test_it_is_the_same_predicate_the_pipeline_check_uses(self):+ import astimport inspectfrom core.inference.llama_cpp import _pipeline_parallel_disabled_by_args- import astimport textwraptree = ast.parse(textwrap.dedent(inspect.getsource(_pipeline_parallel_disabled_by_args)))@@ -1861,7 +1857,6 @@ class TestPairedWritesAreInvalidatedTogether:def test_one_acquisition_covers_every_key(self):import ast- from pathlib import Pathsrc = Path(__file__).resolve().parent.parent / "utils" / "model_memory_settings.py"tree = ast.parse(src.read_text(encoding = "utf-8"))@@ -1889,9 +1884,9 @@ class TestThePolicyReadsOneSnapshot:saved --mlock in the extras."""def test_a_save_between_the_two_reads_is_not_observable(self, monkeypatch):+ # Start at (keep_resident=True, no_ram_reserve=False).import utils.model_memory_settings as mm- # Start at (keep_resident=True, no_ram_reserve=False).store = {mm.KEEP_RESIDENT_SETTING_KEY: True,mm.NO_RAM_RESERVE_SETTING_KEY: False,@@ -1926,7 +1921,6 @@ class TestThePolicyReadsOneSnapshot:def test_the_policy_derives_both_from_one_call(self):import ast- from pathlib import Pathsrc = Path(__file__).resolve().parent.parent / "core" / "inference" / "llama_server_args.py"tree = ast.parse(src.read_text(encoding = "utf-8"))@@ -1987,6 +1981,7 @@ class TestResidencyDoesNotBlockReload:@pytest.fixturedef idle_env(self, monkeypatch):"""Standalone UNSLOTH_MODEL_IDLE_TTL with auto-switch off."""+ import utils.model_memory_settings as mmimport utils.openai_auto_switch_settings as ausmonkeypatch.setattr(aus, "_stored_idle_seconds", lambda: None)@@ -1994,7 +1989,6 @@ class TestResidencyDoesNotBlockReload:monkeypatch.setattr(aus, "get_openai_auto_switch_enabled", lambda: False)def residency(on):- import utils.model_memory_settings as mmmonkeypatch.setattr(mm, "get_keep_resident", lambda: on)return residency@@ -2025,10 +2019,11 @@ class TestResidencyDoesNotBlockReload:def test_turning_idle_unload_off_still_disables_the_reload_path(self, monkeypatch):"""The converse: no TTL and no auto-switch means no automatic load, withor without residency, so this is not just always-true."""- import routes.inference as riimport utils.model_memory_settings as mmimport utils.openai_auto_switch_settings as aus+ import routes.inference as ri+monkeypatch.setattr(aus, "_stored_idle_seconds", lambda: None)monkeypatch.setattr(aus, "_env_idle_seconds", lambda: None)monkeypatch.setattr(aus, "get_openai_auto_switch_enabled", lambda: False)@@ -2203,10 +2198,10 @@ class TestAGpuIdsPinOverridesADeviceFlag:def test_the_launch_really_sanitizes_before_classifying(self):"""Source check: the gate call must receive the stripped extras and env,so this cannot regress into reading the raw ones again."""+ from core.inference.llama_cpp import LlamaCppBackendimport inspect- import re- from core.inference.llama_cpp import LlamaCppBackend+ import resrc = inspect.getsource(LlamaCppBackend.load_model)strip = src.find("_mem_extra_args = self._strip_device_extra_args(extra_args)")@@ -2274,9 +2269,8 @@ class TestFitOffRetryDropsTheLock:def test_the_launch_really_reclassifies_the_fit_off_retry(self):"""Source check: the branch must re-ask the gate and clear thebookkeeping, so it cannot drift back to reusing the fitted verdict."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend+ import inspectsrc = inspect.getsource(LlamaCppBackend.load_model)branch = src.find('run_cmd = [*run_cmd, "--fit", "off"]')@@ -2320,9 +2314,8 @@ class TestFitOffRetryClearsPolicyActivity:def test_the_launch_recomputes_activity_without_the_managed_flag(self):"""Source check: the retry must reuse the non-managed half of the launchexpression, not leave the first attempt's verdict standing."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend+ import inspectsrc = inspect.getsource(LlamaCppBackend.load_model)assert (@@ -2342,7 +2335,6 @@ class TestNoDeadMemoryBookkeeping:def test_the_launch_records_nothing_unread(self):import ast- from pathlib import Pathbackend = Path(__file__).resolve().parent.parenttarget = backend / "core" / "inference" / "llama_cpp.py"@@ -2445,9 +2437,8 @@ class TestTheEffectiveFitterState:def test_the_launch_asks_over_the_whole_command(self):"""Source check: Unsloth emits its own --fit into cmd, so reading theextras alone would miss it."""- import inspect-from core.inference.llama_cpp import LlamaCppBackend+ import inspectsrc = inspect.getsource(LlamaCppBackend.load_model)assert "fit_active = fit_is_effectively_on(" in srcdiff --git a/studio/backend/tests/test_mtp_drafter_companion.py b/studio/backend/tests/test_mtp_drafter_companion.pyindex 60087855f24..aa7dd03c529 100644--- a/studio/backend/tests/test_mtp_drafter_companion.py+++ b/studio/backend/tests/test_mtp_drafter_companion.py@@ -701,6 +701,7 @@ def test_variant_plans_new_scheme_uses_root_drafter():def test_download_mtp_prefers_root_over_new_scheme_copies(monkeypatch):# _pick_mtp is nested; capture it via the companion-download seam.+from core.inference.llama_cpp import LlamaCppBackendmonkeypatch.delenv("HF_HUB_OFFLINE", raising = False) # online: skip reuse probe@@ -735,8 +736,8 @@ def test_download_mtp_prefers_root_over_new_scheme_copies(monkeypatch):def test_companion_downloads_forward_the_load_cancel_event(monkeypatch):- import core.inference.llama_cpp as llama_cpp_modulefrom core.inference.llama_cpp import LlamaCppBackend+ import core.inference.llama_cpp as llama_cpp_modulemonkeypatch.delenv("HF_HUB_OFFLINE", raising = False)monkeypatch.setattr(@@ -814,8 +815,8 @@ def _capture_companion_download(backend):def test_download_mtp_refetches_when_the_cache_holds_only_a_shared_head(tmp_path, monkeypatch):- import utils.models.gguf_metadata as gmfrom core.inference.llama_cpp import LlamaCppBackend+ import utils.models.gguf_metadata as gmmonkeypatch.delenv("HF_HUB_OFFLINE", raising = False)monkeypatch.setattr(gm, "read_gguf_nextn_predict_layers", lambda p: 0)@@ -834,8 +835,8 @@ def test_download_mtp_refetches_when_the_cache_holds_only_a_shared_head(tmp_pathdef test_download_mtp_still_reuses_a_cached_self_contained_head(tmp_path, monkeypatch):- import utils.models.gguf_metadata as gmfrom core.inference.llama_cpp import LlamaCppBackend+ import utils.models.gguf_metadata as gmmonkeypatch.delenv("HF_HUB_OFFLINE", raising = False)monkeypatch.setattr(gm, "read_gguf_nextn_predict_layers", lambda p: 0)@@ -858,8 +859,8 @@ def test_download_mtp_still_reuses_a_cached_self_contained_head(tmp_path, monkeydef test_download_mtp_keeps_a_lone_shared_head_offline(tmp_path, monkeypatch):- import utils.models.gguf_metadata as gmfrom core.inference.llama_cpp import LlamaCppBackend+ import utils.models.gguf_metadata as gmmonkeypatch.setenv("HF_HUB_OFFLINE", "1")monkeypatch.setattr(gm, "read_gguf_nextn_predict_layers", lambda p: 0)@@ -884,9 +885,8 @@ def _stub_hub(listing_fails = False,):"""The live repo without a network, stubbed below the helper's own snapshot lookup."""- import huggingface_hub-import core.inference.llama_cpp as llama_cpp_module+ import huggingface_hubdef _list(repo, token = None):if listing_fails:@@ -905,8 +905,8 @@ def _stub_hub(def test_download_mtp_lists_the_repo_past_the_helpers_own_snapshot_reuse(tmp_path, monkeypatch):"""The helper repeats _companion_snapshot_sibling before listing, so the caller'sfall-through returned the same borrowing head (unsloth#10322). Uses the real helper."""- import utils.models.gguf_metadata as gmfrom core.inference.llama_cpp import LlamaCppBackend+ import utils.models.gguf_metadata as gmmonkeypatch.delenv("HF_HUB_OFFLINE", raising = False)monkeypatch.setattr(gm, "read_gguf_nextn_predict_layers", lambda p: 0)@@ -936,8 +936,8 @@ def test_download_mtp_lists_the_repo_past_the_helpers_own_snapshot_reuse(tmp_patdef test_download_mtp_keeps_the_borrowing_head_when_the_listing_never_answers(tmp_path, monkeypatch):- import utils.models.gguf_metadata as gmfrom core.inference.llama_cpp import LlamaCppBackend+ import utils.models.gguf_metadata as gmmonkeypatch.delenv("HF_HUB_OFFLINE", raising = False)monkeypatch.setattr(gm, "read_gguf_nextn_predict_layers", lambda p: 0)@@ -955,8 +955,8 @@ def test_download_mtp_keeps_the_borrowing_head_when_the_listing_never_answers(def test_download_mtp_reuses_cached_root_drafter_offline(tmp_path, monkeypatch):- import utils.models.model_config as mcfrom core.inference.llama_cpp import LlamaCppBackend+ import utils.models.model_config as mcmonkeypatch.setenv("HF_HUB_OFFLINE", "1")snap = _seed_snapshot(@@ -976,8 +976,9 @@ def test_download_mtp_reuses_cached_root_drafter_offline(tmp_path, monkeypatch):def test_download_mtp_reuses_cached_subdir_copy_when_no_root_offline(tmp_path, monkeypatch):# Pre-fix build may have fetched only the MTP/ copy; reuse it offline.- import utils.models.model_config as mc+from core.inference.llama_cpp import LlamaCppBackend+ import utils.models.model_config as mcmonkeypatch.setenv("HF_HUB_OFFLINE", "1")snap = _seed_snapshot(@@ -996,8 +997,9 @@ def test_download_mtp_reuses_cached_subdir_copy_when_no_root_offline(tmp_path, mdef test_download_mtp_prefers_root_across_snapshots_offline(tmp_path, monkeypatch):# A newer partial snapshot holds only the MTP/ copy; an older one has the# root. Must still return the small root, not the large subdir copy.- import utils.models.model_config as mc+from core.inference.llama_cpp import LlamaCppBackend+ import utils.models.model_config as mcmonkeypatch.setenv("HF_HUB_OFFLINE", "1")snap_partial = _seed_snapshot(tmp_path / "new", ["MTP/mtp-gemma-4-E4B-it-BF16.gguf"])@@ -1011,8 +1013,9 @@ def test_download_mtp_prefers_root_across_snapshots_offline(tmp_path, monkeypatcdef test_download_mtp_reuse_follows_snapshot_order_offline(tmp_path, monkeypatch):# Two snapshots both hold a root drafter; newest-first order must win so a# fresh main GGUF is not paired with a stale drafter revision.- import utils.models.model_config as mc+from core.inference.llama_cpp import LlamaCppBackend+ import utils.models.model_config as mcmonkeypatch.setenv("HF_HUB_OFFLINE", "1")newest = _seed_snapshot(tmp_path / "newest", ["mtp-gemma-4-E4B-it.gguf"])@@ -1024,8 +1027,8 @@ def test_download_mtp_reuse_follows_snapshot_order_offline(tmp_path, monkeypatchdef test_download_mtp_prefers_main_snapshot_offline(tmp_path, monkeypatch):- import utils.models.model_config as mcfrom core.inference.llama_cpp import LlamaCppBackend+ import utils.models.model_config as mcmonkeypatch.setenv("HF_HUB_OFFLINE", "1")snapshots = tmp_path / "models--unsloth--gemma" / "snapshots"@@ -1075,8 +1078,9 @@ def test_download_mtp_skips_discovery_for_embedded_head(tmp_path, monkeypatch):def test_download_mtp_online_skips_cache_reuse(tmp_path, monkeypatch):# Online, do not reuse a cached copy: go to the download path so a changed# drafter is refetched (hf_hub_download checks the current revision).- import utils.models.model_config as mc+from core.inference.llama_cpp import LlamaCppBackend+ import utils.models.model_config as mcmonkeypatch.delenv("HF_HUB_OFFLINE", raising = False)snap = _seed_snapshot(tmp_path, ["mtp-gemma-4-E4B-it.gguf"])@@ -1115,8 +1119,8 @@ def _dspark_download_probe("""Run _download_dspark against a stubbed capability probe and an optionallycached sidecar; report whether the ~11 GB fetch (and even the repo listing)was reached."""- import core.inference.llama_cpp as llama_cpp_modulefrom core.inference.llama_cpp import LlamaCppBackend+ import core.inference.llama_cpp as llama_cpp_modulemonkeypatch.delenv("HF_HUB_OFFLINE", raising = False)monkeypatch.setattr(@@ -1172,8 +1176,8 @@ def test_download_dspark_fetches_when_the_binary_supports_it(monkeypatch):def test_download_dspark_records_whether_the_repo_publishes_a_sidecar(monkeypatch):"""The reuse check retries a failed fetch but must never retry a repo thatships none, so the two "returned None" cases have to stay distinguishable."""- import core.inference.llama_cpp as llama_cpp_modulefrom core.inference.llama_cpp import LlamaCppBackend+ import core.inference.llama_cpp as llama_cpp_modulemonkeypatch.delenv("HF_HUB_OFFLINE", raising = False)monkeypatch.setattr(@@ -1213,8 +1217,8 @@ def test_an_unreachable_hub_is_not_recorded_as_a_missing_sidecar(monkeypatch, tm"""A listing that never completed says nothing about the repo. Recording it asa definitive absence would suppress the reuse check's retry, so DSpark wouldnever be fetched once connectivity returned."""- import core.inference.llama_cpp as llama_cpp_modulefrom core.inference.llama_cpp import LlamaCppBackend+ import core.inference.llama_cpp as llama_cpp_modulemonkeypatch.delenv("HF_HUB_OFFLINE", raising = False)monkeypatch.setattr(@@ -2341,8 +2345,8 @@ def _dflash_download_probe(supports_dflash,cached = None,):- import core.inference.llama_cpp as llama_cpp_modulefrom core.inference.llama_cpp import LlamaCppBackend+ import core.inference.llama_cpp as llama_cpp_modulemonkeypatch.delenv("HF_HUB_OFFLINE", raising = False)monkeypatch.setattr(@@ -2422,8 +2426,8 @@ def test_download_dflash_still_reports_a_cached_sidecar_it_cannot_run(tmp_path,def test_download_dflash_records_whether_the_repo_publishes_a_sidecar(monkeypatch):"""Most repos publish none, and retrying that on every Apply would relaunchan identical server forever; a failed fetch must still be retried."""- import core.inference.llama_cpp as llama_cpp_modulefrom core.inference.llama_cpp import LlamaCppBackend+ import core.inference.llama_cpp as llama_cpp_modulemonkeypatch.delenv("HF_HUB_OFFLINE", raising = False)monkeypatch.setattr(@@ -2500,8 +2504,8 @@ _MULTI_FAMILY_LISTING = [def _dflash_download_pick(monkeypatch, *, listing, near_path):"""The sidecar _download_dflash's picker chooses out of a repo listing."""- import core.inference.llama_cpp as llama_cpp_modulefrom core.inference.llama_cpp import LlamaCppBackend+ import core.inference.llama_cpp as llama_cpp_modulemonkeypatch.delenv("HF_HUB_OFFLINE", raising = False)monkeypatch.setattr(@@ -2655,6 +2659,7 @@ def test_dflash_stays_unreclaimable_even_though_auto_now_launches_it(tmp_path):an order of magnitude under the ~11 GB DSpark case the rule was written for.The positive control below shows the reclaim itself still works."""from hub.services.models.deletion import _delete_gguf_variant_from_repos+from hub.utils.gguf import is_reclaimable_drafter_pathassert is_reclaimable_drafter_path("dflash-kquant.gguf") is False@@ -2740,7 +2745,6 @@ def test_detect_dflash_file_validates_a_candidate_before_reading_its_header(tmp_rejection takes a read back, so the order is: resolve, ask accept, then read."""import os-import utils.models.model_config as mcleased = tmp_path / "leased"@@ -2796,8 +2800,8 @@ def _dflash_repo_download(sibling = None,):"""Drive _download_dflash over a repo listing whose files exist in tmp_path."""- import core.inference.llama_cpp as llama_cpp_modulefrom core.inference.llama_cpp import LlamaCppBackend+ import core.inference.llama_cpp as llama_cpp_modulemonkeypatch.delenv("HF_HUB_OFFLINE", raising = False)monkeypatch.setattr(@@ -2959,7 +2963,9 @@ def _dflash_fetch_during_auto_load(monkeypatch, *, supports_dspark, supports_dflDrives the real load path: the suppression lives inline in load_model'sdownload phase, so nothing short of running it can pin the interaction."""+ from core.inference.llama_cpp import LlamaCppBackendimport core.inference.llama_cpp as llama_cpp_module+from core.inference.llama_cpp import GgufLoadIntent, LlamaCppBackendmonkeypatch.setattr(@@ -3271,8 +3277,8 @@ def test_cached_dflash_lookup_ignores_a_nested_dflash_named_weight(tmp_path, mondef _split_companion_download(tmp_path, monkeypatch, listing):- import core.inference.llama_cpp as llama_cpp_modulefrom core.inference.llama_cpp import LlamaCppBackend+ import core.inference.llama_cpp as llama_cpp_modulemonkeypatch.delenv("HF_HUB_OFFLINE", raising = False)monkeypatch.setattr("huggingface_hub.list_repo_files", lambda repo, token = None: list(listing))@@ -3359,8 +3365,8 @@ def test_companion_snapshot_reuse_skips_an_incomplete_split_sidecar(tmp_path):def test_offline_companion_cache_hit_skips_an_incomplete_split(tmp_path, monkeypatch):"""The offline cache lookup is the third way a shard can reach --model-draft,and offline there is no fetch left to complete the set."""- import core.inference.llama_cpp as llama_cpp_modulefrom core.inference.llama_cpp import LlamaCppBackend+ import core.inference.llama_cpp as llama_cpp_modulemonkeypatch.setenv("HF_HUB_OFFLINE", "1")monkeypatch.setattr(llama_cpp_module, "_hub_download_in_flight", lambda hf_repo: False)@@ -3451,8 +3457,8 @@ def test_cached_dflash_lookup_falls_through_from_a_half_split_to_a_whole_one(tmpdef _dflash_hub_download(tmp_path, monkeypatch, *, listing, fetch):"""Drive _download_dflash through the real _download_companion_gguf, with onlythe two Hub calls stubbed out."""- import core.inference.llama_cpp as llama_cpp_modulefrom core.inference.llama_cpp import LlamaCppBackend+ import core.inference.llama_cpp as llama_cpp_modulemonkeypatch.delenv("HF_HUB_OFFLINE", raising = False)monkeypatch.setattr(@@ -3719,8 +3725,9 @@ def test_download_dflash_skips_a_root_weight_too_big_to_be_a_drafter(monkeypatchdef test_download_companion_refuses_a_listing_missing_part_of_a_split_set(monkeypatch):"""The snapshot and cache paths both refuse half a split companion; the downloadpath returned shard 1 and handed llama-server a set it cannot open."""- import core.inference.llama_cpp as llama_cpp_modulefrom core.inference.llama_cpp import LlamaCppBackend+ import core.inference.llama_cpp as llama_cpp_module+ import huggingface_hubmonkeypatch.delenv("HF_HUB_OFFLINE", raising = False)monkeypatch.setattr(@@ -3728,7 +3735,6 @@ def test_download_companion_refuses_a_listing_missing_part_of_a_split_set(monkey)# Patched at the source: _download_companion_gguf imports list_repo_files inside# its own body, so a module attribute on llama_cpp is never consulted.- import huggingface_hubmonkeypatch.setattr(huggingface_hub,@@ -3815,9 +3821,9 @@ def test_download_dflash_sums_a_split_family_before_the_size_bound(monkeypatch,def test_download_companion_records_an_incomplete_listing_as_settled():"""The completeness rejection lands after outcome["listed"] was set true, sowithout this the caller reads a settled answer as one worth retrying forever."""+ from core.inference.llama_cpp import LlamaCppBackendimport core.inference.llama_cpp as llama_cpp_moduleimport huggingface_hub- from core.inference.llama_cpp import LlamaCppBackendimport pytest as _pytestdiff --git a/studio/backend/tests/test_multimodal_document.py b/studio/backend/tests/test_multimodal_document.pyindex b347c4aef82..2a3d233d378 100644--- a/studio/backend/tests/test_multimodal_document.py+++ b/studio/backend/tests/test_multimodal_document.py@@ -471,6 +471,7 @@ def test_build_external_messages_passes_input_document_for_anthropic_and_openai(# Both providers' stream helpers translate input_document (Anthropic -># {type:"document"}, OpenAI Responses -> {type:"input_file"}), so the# part round-trips through the builder unchanged on those routes.+from models.inference import ChatMessagefrom routes.inference import _build_external_messages@@ -507,6 +508,7 @@ def test_build_external_messages_strips_input_document_for_unmapped_providers():# `messages` verbatim, so an `input_document` part fails the upstream# validator. The builder must strip it for any provider whose stream# helper doesn't translate it.+from models.inference import ChatMessagefrom routes.inference import _build_external_messages@@ -538,6 +540,7 @@ def test_build_external_messages_strips_input_document_for_unmapped_providers():def test_build_external_messages_strips_input_document_when_provider_type_unknown():# Defensive: legacy callers without provider_type must not leak the# part to an unknown destination.+from models.inference import ChatMessagefrom routes.inference import _build_external_messagesdiff --git a/studio/backend/tests/test_native_tls.py b/studio/backend/tests/test_native_tls.pyindex c053dc39929..ee580afbbf6 100644--- a/studio/backend/tests/test_native_tls.py+++ b/studio/backend/tests/test_native_tls.py@@ -21,12 +21,11 @@ if _BACKEND_DIR not in sys.path:sys.path.insert(0, _BACKEND_DIR)from utils import native_tls+import os@pytest.fixture(autouse = True)def _reset_activation(monkeypatch):- import os-monkeypatch.setattr(native_tls, "_activated", False)for key in ("UNSLOTH_STUDIO_NATIVE_TLS",@@ -82,8 +81,6 @@ def test_activate_injects_once(monkeypatch):def test_activate_exports_uv_native_tls(monkeypatch):- import os-monkeypatch.setattr(sys, "platform", "darwin")_fake_truststore(monkeypatch)@@ -93,8 +90,6 @@ def test_activate_exports_uv_native_tls(monkeypatch):def test_activate_keeps_explicit_uv_override(monkeypatch):- import os-monkeypatch.setattr(sys, "platform", "darwin")monkeypatch.setenv("UV_SYSTEM_CERTS", "0")_fake_truststore(monkeypatch)@@ -106,8 +101,6 @@ def test_activate_keeps_explicit_uv_override(monkeypatch):def test_activate_mirrors_legacy_uv_override(monkeypatch):- import os-monkeypatch.setattr(sys, "platform", "darwin")monkeypatch.setenv("UV_NATIVE_TLS", "0")_fake_truststore(monkeypatch)@@ -118,8 +111,6 @@ def test_activate_mirrors_legacy_uv_override(monkeypatch):def test_disabled_does_not_touch_uv_env(monkeypatch):- import os-monkeypatch.setattr(sys, "platform", "linux")_fake_truststore(monkeypatch)diff --git a/studio/backend/tests/test_no_progress_tool_results.py b/studio/backend/tests/test_no_progress_tool_results.pyindex 95d0c8cb117..986d6117e2c 100644--- a/studio/backend/tests/test_no_progress_tool_results.py+++ b/studio/backend/tests/test_no_progress_tool_results.py@@ -32,6 +32,7 @@ if _BACKEND_DIR not in sys.path:import core.inference.llama_cpp as llama_cpp_modulefrom core.inference.llama_cpp import _MAX_IDENTICAL_TOOL_RESULTS, LlamaCppBackend+import core.inference.llama_cpp as _lc_TRUNCATION_NOTICE = "(truncated to 0 chars for the model; showing lines 1-11 of 63.)"@@ -315,7 +316,6 @@ def test_distinct_calls_answered_with_the_same_acknowledgement_are_left_alone(modef _starve_the_budget(monkeypatch):"""Force every result budget under _MIN_USEFUL_RESULT_TOKENS, as a tight window does."""- import core.inference.llama_cpp as _lc # noqa: PLC0415monkeypatch.setattr(_lc, "tool_result_budget", lambda *_a, **_k: 0)@@ -451,7 +451,6 @@ def test_a_resumed_turn_prices_its_tool_result_by_what_is_left(monkeypatch):"""caps: list[object] = []- import core.inference.llama_cpp as _lc # noqa: PLC0415real_budget = _lc.tool_result_budget@@ -495,7 +494,6 @@ def test_a_resumed_turn_sizes_its_recall_by_what_is_left(monkeypatch):"""caps: list[object] = []- import core.inference.llama_cpp as _lc # noqa: PLC0415real_budget = _lc._retrieval_budgetdiff --git a/studio/backend/tests/test_offline_embedding_minimal.py b/studio/backend/tests/test_offline_embedding_minimal.pyindex d79a137988b..c0434d2d816 100644--- a/studio/backend/tests/test_offline_embedding_minimal.py+++ b/studio/backend/tests/test_offline_embedding_minimal.py@@ -287,17 +287,19 @@ def test_snapshot_is_loadable_with_a_complete_indexed_weight_family(hf_cache):def test_cancel_marker_keeps_a_snapshot_pending(monkeypatch, hf_cache):- _make_cache(hf_cache, "org/cancelled", {"config.json": "{}", "model.safetensors": "x"})from hub.utils import download_manifest+ _make_cache(hf_cache, "org/cancelled", {"config.json": "{}", "model.safetensors": "x"})+monkeypatch.setattr(download_manifest, "has_cancel_marker", lambda *a, **k: True)assert hf_cache_snapshot_is_loadable("org/cancelled") is Falsedef test_snapshot_manifest_requires_every_expected_file(monkeypatch, hf_cache):- _make_cache(hf_cache, "org/manifest-partial", {"config.json": "{}", "model.safetensors": "x"})from hub.utils import download_manifest+ _make_cache(hf_cache, "org/manifest-partial", {"config.json": "{}", "model.safetensors": "x"})+manifest = download_manifest.Manifest(repo_type = "model",repo_id = "org/manifest-partial",@@ -315,12 +317,13 @@ def test_snapshot_manifest_requires_every_expected_file(monkeypatch, hf_cache):def test_verified_snapshot_manifest_ignores_unrelated_incomplete_blob(monkeypatch, hf_cache):+ from hub.utils import download_manifest+_make_cache(hf_cache,"org/manifest-complete",{"config.json": "{}", "model.safetensors": "weights"},)- from hub.utils import download_manifestmanifest = download_manifest.Manifest(repo_type = "model",@@ -781,11 +784,11 @@ def test_gate_blocks_on_index_path_traversal(hf_cache):def test_gate_allows_symlinked_sharded_safetensors(tmp_path, monkeypatch):# Real HF caches store snapshot files as symlinks into blobs/. A resolve()-based containment# check would escape the snapshot and false-block every sharded model; the lexical gate must not.+ from huggingface_hub.file_download import repo_folder_name+import hashlibimport os- from huggingface_hub.file_download import repo_folder_name-root = tmp_path / "hub"root.mkdir()monkeypatch.setenv("HF_HOME", str(tmp_path))@@ -1132,6 +1135,8 @@ def test_an_uncached_model_online_still_loads_by_repo_id(monkeypatch):def test_a_module_declared_but_absent_makes_the_snapshot_incomplete(monkeypatch, tmp_path):"""Only module roots already carrying weights were validated, so a snapshotmissing 0_Transformer entirely passed on a complete 2_Dense."""+ import utils.utils as utils+from utils import utilssnapshot = tmp_path / "snap"diff --git a/studio/backend/tests/test_offline_gguf_cache_fallback.py b/studio/backend/tests/test_offline_gguf_cache_fallback.pyindex 1af54eb9fb5..c4f40543f5c 100644--- a/studio/backend/tests/test_offline_gguf_cache_fallback.py+++ b/studio/backend/tests/test_offline_gguf_cache_fallback.py@@ -113,6 +113,18 @@ from utils.models.model_config import (import utils.models.model_config as mc+def fail_download(*_args, **_kwargs):+ raise AssertionError("should reuse the cached GGUF instead of downloading")+++def boom(*a, **k):+ raise OSError("network down")+++def boom_list(*a, **k):+ raise OSError("offline")++# ---------------------------------------------------------------------------# Fixtures# ---------------------------------------------------------------------------@@ -228,6 +240,8 @@ class TestGgufVariantFileResolution:assert _gguf_files_for_variant(files, "") == ["model-Q4_K_M.gguf"]def test_remote_listing_skips_big_endian_quant_sibling(self, monkeypatch, clean_offline_env):+ from utils.models.model_config import list_gguf_variants+siblings = [_types.SimpleNamespace(rfilename = "model-Q4_K_M-be.gguf", size = 100),_types.SimpleNamespace(rfilename = "model-Q4_K_M.gguf", size = 10),@@ -317,9 +331,6 @@ class TestGgufVariantFileResolution:):return [_types.SimpleNamespace(path = path, size = 4) for path in paths if path]- def fail_download(*_args, **_kwargs):- raise AssertionError("should reuse the cached GGUF instead of downloading")-with (patch("huggingface_hub.list_repo_files",@@ -377,9 +388,6 @@ class TestGgufVariantFileResolution:seen_repos.append(repo_id)return str(snap / filename) if repo_id == canonical_repo else None- def fail_download(*_args, **_kwargs):- raise AssertionError("should reuse the cached GGUF instead of downloading")-with (patch("huggingface_hub.list_repo_files", fake_list_repo_files),patch("huggingface_hub.get_paths_info", fake_get_paths_info),@@ -432,9 +440,6 @@ class TestGgufVariantFileResolution:):return [_types.SimpleNamespace(path = p, size = 4) for p in paths if p]- def fail_download(*_args, **_kwargs):- raise AssertionError("should reuse the cached GGUF instead of downloading")-with (patch("huggingface_hub.list_repo_files", lambda *_a, **_k: ["model-UD-Q4_K_XL.gguf"]),patch("huggingface_hub.get_paths_info", fake_get_paths_info),@@ -762,6 +767,8 @@ class TestResolveRepoIdCasing:class TestListGgufVariantsOffline:def test_offline_env_short_circuits_api(self, hf_cache, clean_offline_env, monkeypatch):+ from utils.models.model_config import list_gguf_variants+_build_cache(hf_cache, "unsloth/a", {"a-UD-Q4_K_XL.gguf": 1})monkeypatch.setenv("HF_HUB_OFFLINE", "1")@@ -777,6 +784,8 @@ class TestListGgufVariantsOffline:def test_offline_cache_miss_does_not_call_api(self, hf_cache, clean_offline_env, monkeypatch, offline_variable):+ from utils.models.model_config import list_gguf_variants+monkeypatch.setenv(offline_variable, "yes")def boom(*_args, **_kwargs):@@ -789,10 +798,9 @@ class TestListGgufVariantsOffline:assert has_vision is Falsedef test_api_exception_falls_back_to_cache(self, hf_cache, clean_offline_env):- _build_cache(hf_cache, "unsloth/a", {"a-Q4_K_M.gguf": 1})+ from utils.models.model_config import list_gguf_variants- def boom(*a, **k):- raise OSError("network down")+ _build_cache(hf_cache, "unsloth/a", {"a-Q4_K_M.gguf": 1})with patch("huggingface_hub.model_info", boom):variants, _has = list_gguf_variants("unsloth/a")@@ -800,15 +808,15 @@ class TestListGgufVariantsOffline:assert variants[0].quant == "Q4_K_M"def test_api_exception_with_no_cache_reraises(self, hf_cache, clean_offline_env):- def boom(*a, **k):- raise OSError("network down")-+ from utils.models.model_config import list_gguf_variantswith patch("huggingface_hub.model_info", boom):with pytest.raises(OSError, match = "network down"):list_gguf_variants("unsloth/never-cached")def test_online_path_unaffected(self, hf_cache, clean_offline_env):# When the API succeeds, cache is not consulted.+ from utils.models.model_config import list_gguf_variants+api_payload = _siblings({"a-UD-Q4_K_XL.gguf": 5, "a-Q2_K.gguf": 3})def hf_info(*a, **k):@@ -1066,6 +1074,8 @@ class TestSharedHubModelInfo:def test_variant_listing_shares_its_sized_read(self, hub):"""A GGUF listing asks for sizes, and the probes after it read the same response."""+ from utils.models.model_config import list_gguf_variants+hub.siblings = ("a-Q4_K_M.gguf",)with shared_hub_model_info():@@ -1129,10 +1139,11 @@ class TestSharedHubModelInfo:def test_the_config_route_opens_the_shared_read_and_the_pin(self, hub, monkeypatch):"""The endpoint is what establishes both request scopes, not the helpers below it."""+ import utils.utils as uu+import asyncioimport routes.models as models_route- import utils.utils as uupinned_inside: list = []@@ -1445,6 +1456,8 @@ class TestAllProxyIsHonoured:monkeypatch.setenv("HF_ENDPOINT", "https://huggingface.co")def test_all_proxy_alone_resolves(self, monkeypatch):+ from utils.utils import hf_proxy_for_endpoint+from utils.utils import hf_proxy_configured, hf_proxy_for_endpointmonkeypatch.setenv("ALL_PROXY", "http://proxy.internal:3128")@@ -1492,9 +1505,9 @@ class TestAllProxyIsHonoured:def test_probe_opens_through_all_proxy(self, monkeypatch):"""The HEAD probe must be issued through the proxy, not attempted directly."""- import urllib.request-from utils.transformers_version import hf_endpoint_unreachable+ import urllib.error+ import urllib.requestmonkeypatch.setenv("ALL_PROXY", "http://proxy.internal:3128")seen = {}@@ -1525,9 +1538,9 @@ class TestAllProxyIsHonoured:assert seen["proxies"] == {"https": "http://proxy.internal:3128"}def test_probe_forces_direct_opener_when_no_proxy_bypasses(self, monkeypatch):- import urllib.request-from utils.transformers_version import hf_endpoint_unreachable+ import urllib.error+ import urllib.requestmonkeypatch.setenv("HF_ENDPOINT", "https://10.23.4.5")monkeypatch.setenv("HTTPS_PROXY", "http://proxy.internal:3128")@@ -1617,15 +1630,17 @@ class TestSlowLinkIsNotOffline:passdef _probe_raising(self, monkeypatch, exc):+ from utils.transformers_version import hf_endpoint_unreachable+ import urllib.errorimport urllib.requestmonkeypatch.setattr(urllib.request, "urlopen", lambda *a, **k: (_ for _ in ()).throw(exc))- from utils.transformers_version import hf_endpoint_unreachablereturn hf_endpoint_unreachabledef test_timeout_with_tcp_egress_is_reachable(self, monkeypatch):import urllib.error+ import urllib.requestprobe = self._probe_raising(monkeypatch, urllib.error.URLError(TimeoutError("slow")))monkeypatch.setattr("utils.utils.hf_tcp_reachable", lambda *a, **k: True)@@ -1633,6 +1648,7 @@ class TestSlowLinkIsNotOffline:def test_timeout_without_tcp_egress_is_offline(self, monkeypatch):import urllib.error+ import urllib.requestprobe = self._probe_raising(monkeypatch, urllib.error.URLError(TimeoutError("dead")))monkeypatch.setattr("utils.utils.hf_tcp_reachable", lambda *a, **k: False)@@ -1664,9 +1680,8 @@ class TestConcurrentGuardsHoldTheirOwnReference:def test_second_guard_engages_and_offline_survives_first_exit(self, monkeypatch, clean_offline_env):- import threading-import core.inference.llama_cpp as lc+ import threadingmonkeypatch.setattr(lc, "_hf_unreachable", lambda: True)@@ -1714,9 +1729,8 @@ class TestConcurrentGuardsHoldTheirOwnReference:self, monkeypatch, clean_offline_env):"""A guard that starts just before another sets the env must retain its window."""- import threading-import core.inference.llama_cpp as lc+ import threadingimport utils.utils as uumonkeypatch.setattr(lc, "_hf_unreachable", lambda: True)@@ -1780,9 +1794,10 @@ class TestSpawnEnvironmentDoesNotInheritScopedOffline:def test_explicit_subprocess_environment_uses_pre_guard_values(self, monkeypatch, clean_offline_env, tmp_path):- from utils.hf_cache_settings import HuggingFaceCachePathsfrom utils.utils import force_hf_offline+ from utils.hf_cache_settings import HuggingFaceCachePaths+paths = HuggingFaceCachePaths(tmp_path, tmp_path / "hub", tmp_path / "xet", "studio")with force_hf_offline():child_env = paths.child_env()@@ -1818,6 +1833,8 @@ class TestSpawnWindowKeepsTheParentOffline:def test_env_offline_still_true_inside_the_spawn_window(self, monkeypatch, clean_offline_env):from utils.hf_cache_settings import child_environment_for_spawn+ from utils.utils import force_hf_offline+from utils.transformers_version import _env_offlinefrom utils.utils import force_hf_offline, hf_env_offline@@ -1836,10 +1853,10 @@ class TestSpawnWindowKeepsTheParentOffline:):"""Guard on one thread, spawn window on a second, raw metadata read on a third.No request may leave the process."""- import threading-from utils.hf_cache_settings import child_environment_for_spawnfrom utils.utils import force_hf_offline+ import threading+ import utils.transformers_version as tvfrom utils import transformers_version as tv@@ -1891,6 +1908,7 @@ class TestProxyTimeoutIsNotExcused:passdef _probe_timing_out(self, monkeypatch):+ from utils.transformers_version import hf_endpoint_unreachableimport urllib.errorimport urllib.request@@ -1899,7 +1917,6 @@ class TestProxyTimeoutIsNotExcused:"urlopen",lambda *a, **k: (_ for _ in ()).throw(urllib.error.URLError(TimeoutError("slow"))),)- from utils.transformers_version import hf_endpoint_unreachablereturn hf_endpoint_unreachable@@ -1939,18 +1956,18 @@ class TestEndpointNormalisation:assert hf_endpoint_url() == "https://huggingface.co"def test_probe_uses_the_same_normalised_url(self, monkeypatch):- seen: list = []+ from utils.transformers_version import hf_endpoint_unreachable+ import urllib.errorimport urllib.request+ seen: list = []+def _capture(req, *a, **k):seen.append(req.full_url)raise urllib.error.URLError("stop")- import urllib.error-monkeypatch.setenv("HF_ENDPOINT", " ")monkeypatch.setattr(urllib.request, "urlopen", _capture)- from utils.transformers_version import hf_endpoint_unreachablehf_endpoint_unreachable(timeout = 1)assert seen and seen[0].startswith("https://huggingface.co"), seen@@ -1966,13 +1983,14 @@ class TestIpv6Endpoint:# Mock the resolver rather than trusting the runner's: an ISP or captive# portal that hijacks NXDOMAIN resolves .invalid and would fail this test on# a perfectly good build. Also saves a real 2s lookup per run.++ from utils.utils import dns_host_deadimport socket as _socketdef _nxdomain(*a, **k):raise _socket.gaierror(-2, "Name or service not known")monkeypatch.setattr(_socket, "getaddrinfo", _nxdomain)- from utils.utils import dns_host_deadassert dns_host_dead("no-such-host.invalid", timeout = 2.0) is True@@ -1982,9 +2000,8 @@ class TestCallWithDeadline:multiplies any nominal cap by the address count."""def test_a_stalled_call_raises_rather_than_waiting(self):- import time as _time-from utils.utils import call_with_deadline+ import time as _timestarted = _time.monotonic()with pytest.raises(TimeoutError):@@ -1998,9 +2015,8 @@ class TestCallWithDeadline:def test_slow_but_finished_work_is_not_cut_off(self):"""The bound is the deadline given, not a shorter internal one: a first download ona slow link must not be killed for being slow."""- import time as _time-from utils.utils import call_with_deadline+ import time as _timedef _slow():_time.sleep(1.4)@@ -2013,10 +2029,10 @@ class TestCallWithDeadline:def test_the_caller_s_log_context_follows_the_work(self):"""Context is per-thread: without the copy, logging inside the call loses therequest fields it carries when the same code runs inline."""- import contextvars-from utils.utils import call_with_deadline+ import contextvars+request_id = contextvars.ContextVar("request_id")request_id.set("abc-123")assert call_with_deadline(lambda: request_id.get("missing"), 5.0) == "abc-123"@@ -2071,19 +2087,21 @@ class TestGatewayErrorsAreNotConnectionFailures:passdef _probe_with(self, monkeypatch, exc):+ from utils.transformers_version import hf_endpoint_unreachable+ import urllib.errorimport urllib.requestdef _urlopen(*a, **k):raise excmonkeypatch.setattr(urllib.request, "urlopen", _urlopen)- from utils.transformers_version import hf_endpoint_unreachablereturn hf_endpoint_unreachable@pytest.mark.parametrize("code", [502, 503, 504])def test_strict_mode_treats_gateway_error_as_reachable(self, monkeypatch, code):import urllib.error+ import urllib.requestexc = urllib.error.HTTPError("u", code, "err", {}, None)probe = self._probe_with(monkeypatch, exc)@@ -2094,6 +2112,7 @@ class TestGatewayErrorsAreNotConnectionFailures:@pytest.mark.parametrize("code", [401, 403, 404, 429])def test_other_http_errors_always_reachable(self, monkeypatch, code):import urllib.error+ import urllib.requestexc = urllib.error.HTTPError("u", code, "err", {}, None)probe = self._probe_with(monkeypatch, exc)@@ -2171,6 +2190,9 @@ class TestHfUnreachableProbe:assert hf_unreachable() is Falsedef test_reset_forces_reprobe(self, monkeypatch, clean_offline_env):+ from utils.utils import hf_unreachable+ from utils.utils import reset_hf_reachability_cache+from utils.utils import hf_unreachable, reset_hf_reachability_cachecalls: list = []@@ -2187,10 +2209,9 @@ class TestHfUnreachableProbe:assert uu._HF_REACHABILITY_TTL_S <= 10.0def test_verdict_expires_so_a_disconnect_is_noticed(self, monkeypatch, clean_offline_env):+ from utils.utils import hf_unreachableimport time as _time-import utils.utils as uu- from utils.utils import hf_unreachablemonkeypatch.setattr(uu, "_HF_REACHABILITY_TTL_S", 0.2)verdict = {"value": False}@@ -2239,9 +2260,6 @@ class TestDownloadMmprojOfflineCacheFallback:)backend = LlamaCppBackend()- def boom_list(*a, **k):- raise OSError("offline")-def fake_download(repo_id,filename,@@ -2276,9 +2294,6 @@ class TestDownloadMmprojOfflineCacheFallback:)backend = LlamaCppBackend()- def boom_list(*a, **k):- raise OSError("offline")-captured = {}def fake_download(@@ -2311,9 +2326,6 @@ class TestDownloadMmprojOfflineCacheFallback:)backend = LlamaCppBackend()- def boom_list(*a, **k):- raise OSError("offline")-with patch("huggingface_hub.list_repo_files", boom_list):out = backend._download_mmproj(hf_repo = "unsloth/text-only-GGUF",@@ -2404,6 +2416,8 @@ class TestListLocalGgufVariantsSubdir:assert Path(out).name == "model-BF16-00001-of-00002.gguf"def test_lone_split_symlink_uses_colocated_target_shards(self, tmp_path):+ from utils.models.model_config import _find_local_gguf_by_variant+from utils.models.model_config import _find_local_gguf_by_variant, detect_gguf_modeltarget_dir = tmp_path / "external" / "BF16"@@ -2507,9 +2521,6 @@ class TestListGgufVariantsPermanentErrors:_build_cache(hf_cache, "u/transient-gguf", {"foo-Q4_K_M.gguf": 1})- def boom(*a, **k):- raise OSError("network down")-with patch("huggingface_hub.model_info", boom):variants, _ = list_gguf_variants("u/transient-gguf")assert any(v.quant == "Q4_K_M" for v in variants)@@ -2550,6 +2561,7 @@ class TestProbeDnsDeadNoGlobalTimeoutMutation:def test_default_timeout_unchanged_when_dns_up(self, monkeypatch):import socket as _socket+from core.inference.llama_cpp import _probe_dns_deadprev = _socket.getdefaulttimeout()@@ -2591,12 +2603,13 @@ class TestProbeDnsDeadNoGlobalTimeoutMutation:probe hanging on the same lookup."""import socket as _socket+ import threading+from core.inference.llama_cpp import _probe_dns_dead# Patch getaddrinfo, which is what the probe calls; patching gethostbyname made# this pass vacuously off the real NXDOMAIN for .invalid.def wedged(*a, **k):- import threadingthreading.Event().wait()monkeypatch.setattr(_socket, "getaddrinfo", wedged)@@ -2604,9 +2617,9 @@ class TestProbeDnsDeadNoGlobalTimeoutMutation:def test_slow_but_resolving_dns_is_not_dead(self, monkeypatch):"""The case the deadline used to misread: an answer that arrives after it."""+ from utils.utils import dns_host_deadimport socket as _socketimport time as _time- from utils.utils import dns_host_deaddef slow(*a, **k):_time.sleep(0.3)@@ -2617,8 +2630,8 @@ class TestProbeDnsDeadNoGlobalTimeoutMutation:def test_nxdomain_is_still_dead(self, monkeypatch):"""The reported bug's shortcut must keep working: a real resolver error."""- import socket as _socketfrom utils.utils import dns_host_dead+ import socket as _socketdef nxdomain(*a, **k):raise _socket.gaierror("Name or service not known")@@ -2734,32 +2747,36 @@ class TestProxyDetectionWithoutRequests:monkeypatch.setattr(builtins, "__import__", _fail)def test_env_proxy_still_found_without_requests(self, monkeypatch):+ from utils.utils import hf_proxy_for_endpoint+monkeypatch.setenv("HTTPS_PROXY", "http://proxy.internal:3128")self._without_requests(monkeypatch)- from utils.utils import hf_proxy_for_endpointassert hf_proxy_for_endpoint("https://huggingface.co") == "http://proxy.internal:3128"def test_all_proxy_still_found_without_requests(self, monkeypatch):+ from utils.utils import hf_proxy_for_endpoint+monkeypatch.setenv("ALL_PROXY", "http://proxy.internal:3128")self._without_requests(monkeypatch)- from utils.utils import hf_proxy_for_endpointassert hf_proxy_for_endpoint("https://huggingface.co") == "http://proxy.internal:3128"def test_no_proxy_still_bypasses_without_requests(self, monkeypatch):+ from utils.utils import hf_proxy_for_endpoint+monkeypatch.setenv("HTTPS_PROXY", "http://proxy.internal:3128")monkeypatch.setenv("NO_PROXY", "huggingface.co")self._without_requests(monkeypatch)- from utils.utils import hf_proxy_for_endpointassert hf_proxy_for_endpoint("https://huggingface.co") is Nonedef test_dns_shortcut_stands_down_without_requests(self, monkeypatch):+ from utils.utils import hf_dns_dead+monkeypatch.setenv("ALL_PROXY", "http://proxy.internal:3128")monkeypatch.setenv("HF_ENDPOINT", "https://does-not-resolve.invalid")self._without_requests(monkeypatch)- from utils.utils import hf_dns_deadassert hf_dns_dead(timeout = 1.0) is False@@ -2782,6 +2799,8 @@ class TestSocksProxyIsNotEgressEvidence:assert hf_proxy_usable_by_urllib(None) is Truedef test_socks_proxy_reports_reachable(self, monkeypatch):+ from utils.transformers_version import hf_endpoint_unreachable+ import urllib.errorimport urllib.requestmonkeypatch.setenv("ALL_PROXY", "socks5://127.0.0.1:1080")@@ -2791,7 +2810,6 @@ class TestSocksProxyIsNotEgressEvidence:raise AssertionError("must not attempt urlopen through a socks proxy")monkeypatch.setattr(urllib.request, "urlopen", _boom)- from utils.transformers_version import hf_endpoint_unreachableassert hf_endpoint_unreachable(1) is False@@ -2806,6 +2824,8 @@ class TestProbeFailsOpenOnNonNetworkErrors:@pytest.mark.parametrize("exc", [ValueError("bad url"), TypeError("bad port"), MemoryError()])def test_non_socket_exception_is_inconclusive(self, monkeypatch, exc):+ from utils.transformers_version import hf_endpoint_unreachable+ import urllib.errorimport urllib.requestmonkeypatch.setenv("HF_ENDPOINT", "https://huggingface.co")@@ -2814,13 +2834,13 @@ class TestProbeFailsOpenOnNonNetworkErrors:"urlopen",lambda *a, **k: (_ for _ in ()).throw(exc),)- from utils.transformers_version import hf_endpoint_unreachableassert hf_endpoint_unreachable(1) is Falsedef test_client_side_urlerror_is_inconclusive(self, monkeypatch):"""urllib reports client-side problems as URLError with a plain string reason("no host given", "unknown url type"). That is not an answer about egress."""+ from utils.transformers_version import hf_endpoint_unreachableimport urllib.errorimport urllib.request@@ -2830,15 +2850,16 @@ class TestProbeFailsOpenOnNonNetworkErrors:"urlopen",lambda *a, **k: (_ for _ in ()).throw(urllib.error.URLError("no host given")),)- from utils.transformers_version import hf_endpoint_unreachableassert hf_endpoint_unreachable(1) is Falsedef test_socket_reason_urlerror_is_still_offline(self, monkeypatch):- import socket+ from utils.transformers_version import hf_endpoint_unreachableimport urllib.errorimport urllib.request+ import socket+monkeypatch.setenv("HF_ENDPOINT", "https://huggingface.co")monkeypatch.setattr(urllib.request,@@ -2847,11 +2868,12 @@ class TestProbeFailsOpenOnNonNetworkErrors:urllib.error.URLError(socket.gaierror(-2, "Name or service not known"))),)- from utils.transformers_version import hf_endpoint_unreachableassert hf_endpoint_unreachable(1) is Truedef test_socket_error_is_still_offline(self, monkeypatch):+ from utils.transformers_version import hf_endpoint_unreachable+ import urllib.errorimport urllib.requestmonkeypatch.setenv("HF_ENDPOINT", "https://huggingface.co")@@ -2860,7 +2882,6 @@ class TestProbeFailsOpenOnNonNetworkErrors:"urlopen",lambda *a, **k: (_ for _ in ()).throw(OSError("network unreachable")),)- from utils.transformers_version import hf_endpoint_unreachableassert hf_endpoint_unreachable(1) is True@@ -2876,6 +2897,7 @@ class TestExplicitBaseEnvIsAlsoScrubbed:def test_explicit_base_loses_scoped_offline(self, monkeypatch, tmp_path):from utils.utils import force_hf_offline+from utils.hf_cache_settings import get_hf_cache_pathsfrom utils.native_path_leases import child_env_without_native_path_secret@@ -2890,6 +2912,7 @@ class TestExplicitBaseEnvIsAlsoScrubbed:def test_user_set_offline_still_reaches_the_child(self, monkeypatch):from utils.utils import force_hf_offline+from utils.hf_cache_settings import get_hf_cache_pathsmonkeypatch.setenv("HF_HUB_OFFLINE", "1")@@ -2906,13 +2929,13 @@ class TestHttpsProxyDefaultPort:passdef test_https_proxy_defaults_to_443(self, monkeypatch):- monkeypatch.setenv("HTTPS_PROXY", "https://proxy.internal")from utils.utils import hf_connect_target+ monkeypatch.setenv("HTTPS_PROXY", "https://proxy.internal")assert list(hf_connect_target("https://huggingface.co")) == ["proxy.internal", 443]def test_http_proxy_defaults_to_80(self, monkeypatch):- monkeypatch.setenv("HTTPS_PROXY", "http://proxy.internal")from utils.utils import hf_connect_target+ monkeypatch.setenv("HTTPS_PROXY", "http://proxy.internal")assert list(hf_connect_target("https://huggingface.co")) == ["proxy.internal", 80]@@ -2968,8 +2991,8 @@ class TestMetadataReadsUseTheHubProxy:srv.shutdown()def _clear_caches(self, monkeypatch):+ import urllib.errorimport urllib.request-import utils.transformers_version as tvmonkeypatch.setattr(tv, "_config_json_cache", {})@@ -2980,6 +3003,7 @@ class TestMetadataReadsUseTheHubProxy:def test_all_proxy_is_ignored_by_the_default_opener(self, monkeypatch):"""Guards the premise: urllib alone would never use this proxy."""+ import urllib.errorimport urllib.requestmonkeypatch.setenv("ALL_PROXY", "http://127.0.0.1:3128")@@ -3082,22 +3106,23 @@ class TestSlowProxyDoesNotForceOffline:reset_hf_reachability_cache()def test_shared_guard_passes_both_ambiguity_flags_off(self, monkeypatch):+ from utils.utils import hf_unreachable+ import utils.transformers_version as tv+seen = {}def _probe(timeout, **kwargs):seen.update(kwargs)return False- import utils.transformers_version as tv-monkeypatch.setattr(tv, "hf_endpoint_unreachable", _probe)- from utils.utils import hf_unreachableassert hf_unreachable(timeout = 1) is Falseassert seen == {"gateway_errors_offline": False, "proxy_timeouts_offline": False}def test_slow_proxy_reads_reachable_not_offline(self, monkeypatch):"""End to end through the real classifier: a clean timeout behind a proxy."""+ from utils.utils import hf_unreachableimport utils.transformers_version as tvmonkeypatch.setenv("HF_ENDPOINT", "https://hub.example.test")@@ -3116,8 +3141,6 @@ class TestSlowProxyDoesNotForceOffline:# Sanity: the same input IS offline with the flag on, so the flag is what moves it.assert tv.hf_endpoint_unreachable(1, proxy_timeouts_offline = True) is True- from utils.utils import hf_unreachable-assert hf_unreachable(timeout = 1) is False@@ -3273,6 +3296,7 @@ class TestModelConfigPredicateHonoursTheWindow:is back to the user's values, so an env-only predicate would fire that 15s request."""def test_predicate_is_true_inside_the_spawn_window(self, monkeypatch):+ from utils.utils import force_hf_offlineimport threadingfrom utils.models.model_config import _env_offline@@ -3309,9 +3333,9 @@ class TestModelConfigPredicateHonoursTheWindow:"""The stall this prevents: _detect_audio_from_tokenizer's own requests.get is thecall the gate protects, so drive that function rather than a caller that couldreturn earlier for unrelated reasons."""+ from utils.utils import force_hf_offlineimport threading- import utils.models.model_config as mcfrom utils.utils import force_hf_offline, hf_environment_restored_for_spawnfor key in ("HF_HUB_OFFLINE", "TRANSFORMERS_OFFLINE"):@@ -3361,7 +3385,6 @@ class TestLocalModelWithRemoteBaseIsGuarded:base can still be a hub repo, and resolving the config dereferences that base."""def test_helper_engages_for_a_remote_target(self, monkeypatch):- import utils.models.model_config as mcimport core.inference.llama_cpp as llama_cppopened = []@@ -3380,8 +3403,6 @@ class TestLocalModelWithRemoteBaseIsGuarded:"""Worker contexts may not be able to import the route-side guard."""import builtins- import utils.models.model_config as mc-real_import = builtins.__import__def _boom(name, *a, **k):@@ -3624,7 +3645,6 @@ class TestHungProbeHonoursFailOpenBehindAProxy:def test_it_stays_bounded(self, monkeypatch, clean_proxy_env):import time as _time-import utils.transformers_version as tvmonkeypatch.setenv("HF_ENDPOINT", "https://hub.example.test")@@ -3650,11 +3670,11 @@ class TestGuardsShareOneDnsLookup:"""The case that accumulates: DNS answers past its deadline, so the shortcut isinconclusive and the probe decides. That probe memoises, so the siblings reuse it."""from core.inference.llama_cpp import _hf_unreachable+ import utils.transformers_version as tvdns = []probe = []monkeypatch.setattr("utils.utils.hf_dns_dead", lambda *a, **k: (dns.append(1), False)[1])- import utils.transformers_version as tvmonkeypatch.setattr(tv, "hf_endpoint_unreachable", lambda *a, **k: (probe.append(1), True)[1]@@ -3668,11 +3688,11 @@ class TestGuardsShareOneDnsLookup:def test_a_reachable_verdict_is_reused_too(self, monkeypatch, clean_offline_env):from core.inference.llama_cpp import _hf_unreachable+ import utils.transformers_version as tvdns = []probe = []monkeypatch.setattr("utils.utils.hf_dns_dead", lambda *a, **k: (dns.append(1), False)[1])- import utils.transformers_version as tvmonkeypatch.setattr(tv, "hf_endpoint_unreachable", lambda *a, **k: (probe.append(1), False)[1]@@ -3688,10 +3708,10 @@ class TestGuardsShareOneDnsLookup:):"""A dead lookup fails fast, so caching it would only delay recovery by the TTL."""from core.inference.llama_cpp import _hf_unreachable+ import utils.transformers_version as tvstate = {"dead": True}monkeypatch.setattr("utils.utils.hf_dns_dead", lambda *a, **k: state["dead"])- import utils.transformers_version as tvmonkeypatch.setattr(tv, "hf_endpoint_unreachable", lambda *a, **k: False)@@ -3741,8 +3761,8 @@ class TestPinnedReachability:"""UNSLOTH_OFFLINE_PROBE turns off the TCP probe, not DNS, so the shortcut is theonly detector left and the pin must not answer for it. A link that drops after theguard admitting the request has to be seen by the guards that follow."""- import utils.utils as uufrom core.inference.llama_cpp import _hf_unreachable+ import utils.utils as uumonkeypatch.setenv("UNSLOTH_OFFLINE_PROBE", "0")lookups = []@@ -3792,9 +3812,10 @@ class TestPinnedReachability:def test_the_guard_the_route_opens_reads_the_pinned_verdict(self, probe, monkeypatch):"""The wrapper the handler actually goes through, not only ``hf_unreachable``: itanswers from the memo directly, so that path has to pin like every other."""- import core.inference.llama_cpp as llama_cppimport utils.utils as uu+ import core.inference.llama_cpp as llama_cpp+monkeypatch.setattr(uu, "_HF_REACHABILITY_TTL_S", 60.0)monkeypatch.setattr(uu, "hf_dns_dead", lambda *_a, **_k: False, raising = False)assert uu.hf_unreachable() is False@@ -3834,7 +3855,6 @@ class TestPinnedReachability:Module-global state would hand A request B's pin, and A would report B's verdict."""import threading-import utils.transformers_version as tvimport utils.utils as uudiff --git a/studio/backend/tests/test_offline_inference_parent.py b/studio/backend/tests/test_offline_inference_parent.pyindex 05aed804bea..ccf3e6c20d9 100644--- a/studio/backend/tests/test_offline_inference_parent.py+++ b/studio/backend/tests/test_offline_inference_parent.py@@ -86,6 +86,9 @@ from utils.transformers_version import (_check_tokenizer_config_needs_v5,_env_offline as _env_offline_tv,)+import importlib.util+import json+import pathlib@pytest.fixture@@ -269,8 +272,6 @@ class TestInferenceWorkerProbesForItself:into the retry paths the parent already ruled out."""def _block(self):- import pathlib-backend_root = pathlib.Path(__file__).resolve().parent.parentsrc = (backend_root / "core" / "inference" / "worker.py").read_text(encoding = "utf-8",@@ -281,8 +282,6 @@ class TestInferenceWorkerProbesForItself:return src[start : src.index("\n import warnings", start)]def test_the_probe_exists_and_runs_before_activation(self):- import pathlib-backend_root = pathlib.Path(__file__).resolve().parent.parentsrc = (backend_root / "core" / "inference" / "worker.py").read_text(encoding = "utf-8",@@ -322,9 +321,6 @@ class TestWorkerProbesOnlyWhenTheHubIsNeeded:was DNS-only on main for training, and absent entirely for inference."""def _load(self, relpath, name):- import importlib.util- import pathlib-backend_root = pathlib.Path(__file__).resolve().parent.parentspec = importlib.util.spec_from_file_location(name, backend_root / relpath)mod = importlib.util.module_from_spec(spec)@@ -358,8 +354,6 @@ class TestWorkerProbesOnlyWhenTheHubIsNeeded:def test_inference_gate_reads_a_local_adapter_base_from_disk(self, tmp_path):"""A local adapter pointing at a REMOTE base still needs the probe, and the baseis readable without touching the network."""- import json-w = self._load("core/inference/worker.py", "inference_worker_gate_adapter")(tmp_path / "adapter_config.json").write_text(json.dumps({"base_model_name_or_path": "org/base"}),@@ -375,8 +369,6 @@ class TestWorkerProbesOnlyWhenTheHubIsNeeded:assert w._recorded_local_base("org/model") == (None, False)def test_both_probes_sit_behind_the_gate(self):- import pathlib-backend_root = pathlib.Path(__file__).resolve().parent.parentinf = (backend_root / "core" / "inference" / "worker.py").read_text(encoding = "utf-8",@@ -396,9 +388,6 @@ class TestLocalLoraTrainingJobStillProbes:training and security code fetches, so the job is not filesystem-only."""def _worker(self):- import importlib.util- import pathlib-backend_root = pathlib.Path(__file__).resolve().parent.parentspec = importlib.util.spec_from_file_location("training_worker_lora_gate",@@ -409,8 +398,6 @@ class TestLocalLoraTrainingJobStillProbes:return moddef test_local_adapter_with_a_remote_base_is_not_local(self, tmp_path):- import json-w = self._worker()(tmp_path / "adapter_config.json").write_text(json.dumps({"base_model_name_or_path": "org/base"}),@@ -419,8 +406,6 @@ class TestLocalLoraTrainingJobStillProbes:assert w._training_job_is_local({"model_name": str(tmp_path)}) is Falsedef test_local_adapter_with_a_local_base_is_local(self, tmp_path):- import json-w = self._worker()base = tmp_path / "base"base.mkdir()@@ -437,8 +422,6 @@ class TestLocalLoraTrainingJobStillProbes:def test_a_null_recorded_base_still_probes(self, tmp_path):"""An explicit null reads the same as a missing key: no base on disk, so theresolver falls through to get_base_model_from_lora, which is a Hub call."""- import json-w = self._worker()(tmp_path / "adapter_config.json").write_text(json.dumps({"base_model_name_or_path": None}),@@ -448,10 +431,6 @@ class TestLocalLoraTrainingJobStillProbes:def test_both_workers_agree(self, tmp_path):"""The two gates must classify the same adapter the same way."""- import importlib.util- import json- import pathlib-backend_root = pathlib.Path(__file__).resolve().parent.parent(tmp_path / "adapter_config.json").write_text(json.dumps({"base_model_name_or_path": "org/base"}),@@ -476,9 +455,6 @@ class TestFullCheckpointBaseKeepsTheProbe:job is not filesystem-only even though every path on disk is local."""def _module(self, relative_path, name):- import importlib.util- import pathlib-backend_root = pathlib.Path(__file__).resolve().parent.parentspec = importlib.util.spec_from_file_location(name, backend_root / relative_path)mod = importlib.util.module_from_spec(spec)@@ -486,7 +462,6 @@ class TestFullCheckpointBaseKeepsTheProbe:return moddef _checkpoint(self, tmp_path, config_json):- import json(tmp_path / "config.json").write_text(json.dumps(config_json), encoding = "utf-8")return str(tmp_path)@@ -517,8 +492,6 @@ class TestFullCheckpointBaseKeepsTheProbe:def test_an_adapter_base_still_wins_over_config_json(self, tmp_path):"""Ordering matches the resolver: the adapter's base, not the config.json one."""- import json-target = self._checkpoint(tmp_path, {"model_name": "org/from-config"})(tmp_path / "adapter_config.json").write_text(json.dumps({"base_model_name_or_path": "org/from-adapter"}),@@ -530,8 +503,6 @@ class TestFullCheckpointBaseKeepsTheProbe:def test_a_baseless_adapter_needs_the_hub(self, tmp_path):"""With no base on disk the resolver falls through to get_base_model_from_lora,which is a Hub call, so the gate must fail closed."""- import json-(tmp_path / "adapter_config.json").write_text(json.dumps({}), encoding = "utf-8")inf = self._module("core/inference/worker.py", "inference_worker_baseless_gate")trn = self._module("core/training/worker.py", "training_worker_baseless_gate")@@ -542,7 +513,6 @@ class TestFullCheckpointBaseKeepsTheProbe:def test_the_gate_agrees_with_the_resolver(self, tmp_path):"""Anti-drift: this bug was the gate reading less than _resolve_base_model does.For every on-disk shape the two must name the same base."""- import jsonimport sysbackend_root = str(__import__("pathlib").Path(__file__).resolve().parent.parent)@@ -602,7 +572,6 @@ class TestLoadRouteResolvesConfigOffTheLoop:def test_the_guard_and_config_resolution_run_in_a_thread(self):import ast- import pathlibbackend_root = pathlib.Path(__file__).resolve().parent.parentsrc = (backend_root / "routes" / "inference.py").read_text(encoding = "utf-8")diff --git a/studio/backend/tests/test_offload_planner_seam.py b/studio/backend/tests/test_offload_planner_seam.pyindex f79bb40037f..eed5ada47eb 100644--- a/studio/backend/tests/test_offload_planner_seam.py+++ b/studio/backend/tests/test_offload_planner_seam.py@@ -20,6 +20,18 @@ import pytestfrom core.inference.llama_cpp import LlamaCppBackendfrom core.inference.offload_layout import LM_HEAD_PATTERN, BlockLayout, ModelLayoutfrom core.inference.offload_planner import Plan, plan_placement, smart_offload_enabled+from core.inference.llama_cpp import _extra_args_tensor_split+from core.inference.llama_cpp import _linux_math_core_count+import core.inference.llama_cpp as llama_mod+import inspect+import sys+++class _SmtHost:+ @staticmethod+ def cpu_count(logical = True):+ return 16 if logical else 8+# The planner's decision table is covered in test_offload_planner.py. HERE is the# seam: whether the launch path declines when it should, emits the tokens@@ -205,8 +217,6 @@ def test_the_seam_reads_no_attribute_the_real_backend_lacks():constant is module-level, so every planned load 500'd with AttributeError (seen ona 67.6 GB GGUF, Colab T4 high-RAM). The suite stayed green only because the doubledeclared it."""- import inspect-for name in ("_HOST_RAM_HEADROOM_MIB",):assert not hasattr(_Stub, name), (f"the double declares {name}, so it can hide a missing attribute on the "@@ -489,7 +499,6 @@ def test_repeated_ot_flags_rather_than_a_joined_value():def _load_model_source() -> str:- import inspectreturn inspect.getsource(LlamaCppBackend.load_model)@@ -552,8 +561,6 @@ def test_a_spill_plan_startup_failure_can_revoke_the_plan():straight to the terminal fallbacks instead of retrying the --fit on placementit replaced. Reachability only: the revocation must be reachable from thecrash path, not just from the `label` guard at the top of the spawn."""- import inspect-body = inspect.getsource(LlamaCppBackend.load_model)body = body[body.index("def _spawn_and_wait") :]assert body.count("_drop_tensor_spill") >= 2@@ -563,8 +570,6 @@ def test_the_revocation_runs_only_on_retries():"""Gated on `label`, which is empty only on the first spawn. A retry addedlater is covered without anyone remembering to strip -- the failure mode thatproduced most of the review traffic on the load-mode work."""- import inspect-src = inspect.getsource(LlamaCppBackend.load_model)idx = src.index("def _spawn_and_wait")head = src[idx : idx + 1600]@@ -606,7 +611,6 @@ def test_a_unified_memory_apu_is_declared_to_the_planner():assert got_discrete.spills_anything, "the discrete host is free to plan"# And the seam passes the real predicate rather than leaving the default.- import inspectcompact = "".join(inspect.getsource(LlamaCppBackend._planned_tensor_spill).split())assert "unified_memory=self._amd_apu_wants_unified_memory(" in compact@@ -713,8 +717,6 @@ def test_a_slash_delimited_tensor_split_is_the_same_override(extra_args, env):"""llama.cpp splits -ts on ``[,/]+`` (common/arg.cpp:2791-2804), so ``3/1`` IS``3,1`` to the child. Failing to parse it returned None, which the callercannot tell from "no override": it planned 1:1 while the child ran 3:1."""- from core.inference.llama_cpp import _extra_args_tensor_split-assert _extra_args_tensor_split(extra_args, env or {}) == [3.0, 1.0]# And it reaches the planner as the row weights: the 3:1 plan, not free-VRAM.@@ -1118,8 +1120,6 @@ def test_an_unsized_inherited_projector_abstains():def test_the_seam_hands_the_planner_the_inherited_projector():"""The planner can only charge what the launch path snapshots for it, and theload-mode fit already computes exactly these two values."""- import inspect-compact = "".join(inspect.getsource(LlamaCppBackend.load_model).split())assert '_spill_inputs["env_mmproj_unsized"]=_fit_env_mmproj_unsized' in compactassert '_spill_inputs["env_mmproj_bytes"]=' in compact@@ -1274,8 +1274,6 @@ def test_the_flat_mtp_reserve_reaches_the_spill_budget():reduced budget, or the plan spends 5% of VRAM the fit deliberately kept freeand then pins it with --fit off."""- import inspect-compact = "".join(inspect.getsource(LlamaCppBackend.load_model).split())assert '"gpu_usable_mib":{_idx:max(0.0,_gpu_usable((_idx,_free),_pin_fraction))' in compact@@ -1385,8 +1383,6 @@ def test_swa_weights_follow_the_effective_cache_geometry():def test_the_seam_passes_the_launch_geometry_to_the_kv_vector():"""Reachability: the knobs must travel from the launch path, or the two fixesabove only hold in a unit test."""- import inspect-src = inspect.getsource(LlamaCppBackend.load_model)call = src[src.index('"kv_layer_weights"') :][:400]compact = "".join(call.split())@@ -1426,8 +1422,6 @@ def test_a_reconciliation_still_strips_a_non_layer_split_mode_env():"""Reachability anchor for the decline above: if load_model ever stopsscrubbing the inherited mode, the env form becomes plannable again and this isthe test that should fail and say so."""- import inspect-src = inspect.getsource(LlamaCppBackend.load_model)compact = "".join(src.split())assert 'if_inherited_smand_inherited_sm!="layer":' in compact@@ -1479,8 +1473,6 @@ def test_a_non_finite_tensor_split_declines_instead_of_raising(value):reaches int(round(p * scale)), which raises ValueError / OverflowError with notry around the _planned_tensor_spill call, aborting the load -- whilellama.cpp merely degenerates on the same value."""- from core.inference.llama_cpp import _extra_args_tensor_split-assert _extra_args_tensor_split(["-ts", value], {}) is Nonetwo_cards = [(0, 14 * 1024), (1, 14 * 1024)]# Declines (unparseable -ts is not "no -ts"), and above all does not raise.@@ -1492,8 +1484,6 @@ def test_a_non_finite_tensor_split_declines_instead_of_raising(value):def test_a_cumulative_float32_tensor_split_overflow_declines_instead_of_raising():- from core.inference.llama_cpp import _extra_args_tensor_split-value = "3e38,3e38"assert _extra_args_tensor_split(["-ts", value], {}) is Nonetwo_cards = [(0, 14 * 1024), (1, 14 * 1024)]@@ -1609,7 +1599,6 @@ def test_the_seam_passes_the_flash_state_and_cache_type_to_the_kv_vector():fail on a reflow, neither of which says what the seam passes."""import ast- import inspectimport textwraptree = ast.parse(textwrap.dedent(inspect.getsource(LlamaCppBackend.load_model)))@@ -1685,8 +1674,6 @@ def test_the_cost_model_is_told_physical_cores_not_hyperthreads(monkeypatch):desktop had 12, and the generation penalty came out roughly half of what aspill really costs there."""- import core.inference.llama_cpp as llama_mod-monkeypatch.setattr(llama_mod, "_linux_math_core_count", lambda: None)assert llama_mod._spilled_decode_threads() >= 1@@ -1695,8 +1682,6 @@ def test_the_cost_model_is_told_physical_cores_not_hyperthreads(monkeypatch):def cpu_count(logical = True):return 12 if logical else 6- import sys-saved = sys.modules.get("psutil")sys.modules["psutil"] = _FakePsutilmonkeypatch.setattr(@@ -1734,8 +1719,6 @@ def test_the_cost_model_is_told_physical_cores_not_hyperthreads(monkeypatch):def test_thread_overrides_reach_the_cost_model(monkeypatch):- import core.inference.llama_cpp as llama_mod-seen = []def _threads(n_threads = None, extra_args = None):@@ -1757,8 +1740,6 @@ def test_thread_overrides_reach_the_cost_model(monkeypatch):def test_thread_override_precedence_matches_the_launched_command(monkeypatch):- import core.inference.llama_cpp as llama_mod-monkeypatch.setattr(llama_mod, "_linux_math_core_count", lambda: None)class _FakePsutil:@@ -1766,8 +1747,6 @@ def test_thread_override_precedence_matches_the_launched_command(monkeypatch):def cpu_count(logical = True):return 12 if logical else 6- import sys-monkeypatch.setitem(sys.modules, "psutil", _FakePsutil)monkeypatch.setattr(llama_mod.os, "sched_getaffinity", lambda _pid: set(range(12)), raising = False@@ -1779,17 +1758,8 @@ def test_thread_override_precedence_matches_the_launched_command(monkeypatch):def test_smt_workers_do_not_multiply_math_core_capacity(monkeypatch):- import core.inference.llama_cpp as llama_mod-monkeypatch.setattr(llama_mod, "_linux_math_core_count", lambda: None)- class _SmtHost:- @staticmethod- def cpu_count(logical = True):- return 16 if logical else 8-- import sys-monkeypatch.setitem(sys.modules, "psutil", _SmtHost)monkeypatch.setattr(llama_mod.os, "sched_getaffinity", lambda _pid: set(range(16)), raising = False@@ -1811,15 +1781,6 @@ def test_smt_workers_do_not_multiply_math_core_capacity(monkeypatch):def test_default_thread_override_uses_native_logical_count(monkeypatch):- import core.inference.llama_cpp as llama_mod-- class _SmtHost:- @staticmethod- def cpu_count(logical = True):- return 16 if logical else 8-- import sys-monkeypatch.setattr(llama_mod, "_linux_math_core_count", lambda: 8)monkeypatch.setattr(llama_mod.os, "cpu_count", lambda: 2)monkeypatch.setitem(sys.modules, "psutil", _SmtHost)@@ -1827,15 +1788,6 @@ def test_default_thread_override_uses_native_logical_count(monkeypatch):def test_inherited_linux_cpu_affinity_declines_spill_pricing(monkeypatch):- import core.inference.llama_cpp as llama_mod-- class _SmtHost:- @staticmethod- def cpu_count(logical = True):- return 16 if logical else 8-- import sys-monkeypatch.setattr(llama_mod, "_linux_math_core_count", lambda: 8)monkeypatch.setattr(llama_mod.sys, "platform", "linux")monkeypatch.setattr(@@ -1853,15 +1805,11 @@ def test_inherited_linux_cpu_affinity_declines_spill_pricing(monkeypatch):def test_inherited_linux_arm_cpu_affinity_declines_spill_pricing(monkeypatch):- import core.inference.llama_cpp as llama_mod-class _ArmHost:@staticmethoddef cpu_count(logical = True):return 72 if logical else 36- import sys-monkeypatch.setattr(llama_mod.sys, "platform", "linux")monkeypatch.setattr(llama_mod.os,@@ -1877,8 +1825,6 @@ def test_inherited_linux_arm_cpu_affinity_declines_spill_pricing(monkeypatch):def test_oversubscribed_decode_threads_decline_spill_planning(monkeypatch):- import core.inference.llama_cpp as llama_mod-monkeypatch.setattr(llama_mod, "_linux_math_core_count", lambda: 8)monkeypatch.setattr(llama_mod.sys, "platform", "linux")monkeypatch.setattr(@@ -1908,14 +1854,11 @@ def test_oversubscribed_decode_threads_decline_spill_planning(monkeypatch):],)def test_affinity_constrained_decode_declines_spill_planning(monkeypatch, extra_args):- import core.inference.llama_cpp as llama_modmonkeypatch.setattr(llama_mod, "_linux_math_core_count", lambda: 8)assert _plan(_Stub(), free_mib = 14 * 1024, extra_args = extra_args) is Nonedef test_linux_hybrid_math_cores_exclude_efficiency_cores(tmp_path):- from core.inference.llama_cpp import _linux_math_core_count-sibling_sets = [f"{core},{core + 8}" for core in range(8)]sibling_sets.extend(f"{core},{core + 8}" for core in range(8))sibling_sets.extend(str(cpu) for cpu in range(16, 32))@@ -1930,8 +1873,6 @@ def test_linux_hybrid_math_cores_exclude_efficiency_cores(tmp_path):def test_linux_hybrid_pmu_excludes_efficiency_cores_without_capacity(tmp_path):- from core.inference.llama_cpp import _linux_math_core_count-cpu_root = tmp_path / "cpu"event_root = tmp_path / "events"sibling_sets = [f"{core},{core + 4}" for core in range(4)]@@ -1958,8 +1899,6 @@ def test_linux_hybrid_pmu_excludes_efficiency_cores_without_capacity(tmp_path):@pytest.mark.parametrize("source", ["pmu", "capacity"])def test_linux_no_smt_hybrid_matches_llama_cpu_loop(tmp_path, source):- from core.inference.llama_cpp import _linux_math_core_count-cpu_root = tmp_path / "cpu"event_root = tmp_path / "events"for cpu in range(8):@@ -1985,8 +1924,6 @@ def test_linux_no_smt_hybrid_matches_llama_cpu_loop(tmp_path, source):def test_linux_sparse_online_hybrid_matches_llama_physical_fallback(tmp_path):- from core.inference.llama_cpp import _linux_math_core_count-cpu_root = tmp_path / "cpu"event_root = tmp_path / "events"sibling_sets = [f"{cpu},{cpu + 8}" for cpu in range(8)]@@ -2013,8 +1950,6 @@ def test_linux_sparse_online_hybrid_matches_llama_physical_fallback(tmp_path):def test_linux_smt_disabled_hybrid_skips_offline_siblings(tmp_path):- from core.inference.llama_cpp import _linux_math_core_count-cpu_root = tmp_path / "cpu"event_root = tmp_path / "events"sibling_sets = [f"{cpu // 2 * 2},{cpu // 2 * 2 + 1}" for cpu in range(16)]@@ -2040,8 +1975,6 @@ def test_linux_smt_disabled_hybrid_skips_offline_siblings(tmp_path):def test_linux_hybrid_unpinnable_cpu_matches_llama_physical_fallback(tmp_path):- from core.inference.llama_cpp import _linux_math_core_count-cpu_root = tmp_path / "cpu"event_root = tmp_path / "events"sibling_sets = [f"{cpu // 2 * 2},{cpu // 2 * 2 + 1}" for cpu in range(16)]@@ -2068,8 +2001,6 @@ def test_linux_hybrid_unpinnable_cpu_matches_llama_physical_fallback(tmp_path):def test_linux_hybrid_affinity_probe_failure_matches_physical_fallback(tmp_path, monkeypatch):- import core.inference.llama_cpp as llama_mod-cpu_root = tmp_path / "cpu"event_root = tmp_path / "events"for cpu in range(8):@@ -2098,8 +2029,6 @@ def test_linux_hybrid_affinity_probe_failure_matches_physical_fallback(tmp_path,def test_linux_hybrid_affinity_restore_failure_stays_in_worker(tmp_path, monkeypatch):- import core.inference.llama_cpp as llama_mod-cpu_root = tmp_path / "cpu"event_root = tmp_path / "events"for cpu in range(8):@@ -2136,8 +2065,6 @@ def test_linux_hybrid_affinity_restore_failure_stays_in_worker(tmp_path, monkeypdef test_linux_hybrid_with_unreadable_core_mask_is_conservative(tmp_path):- from core.inference.llama_cpp import _linux_math_core_count-cpu_root = tmp_path / "cpu"event_root = tmp_path / "events"for cpu in range(4):@@ -2158,8 +2085,6 @@ def test_linux_hybrid_with_unreadable_core_mask_is_conservative(tmp_path):def test_linux_hybrid_with_only_efficiency_cores_matches_llama_fallback(tmp_path):- from core.inference.llama_cpp import _linux_math_core_count-cpu_root = tmp_path / "cpu"event_root = tmp_path / "events"for cpu in range(4):@@ -2182,8 +2107,6 @@ def test_linux_hybrid_with_only_efficiency_cores_matches_llama_fallback(tmp_pathdef test_linux_topology_ignores_python_cpu_count_override(tmp_path, monkeypatch):- import core.inference.llama_cpp as llama_mod-for cpu in range(4):cpu_path = tmp_path / f"cpu{cpu}" / "topology"cpu_path.mkdir(parents = True)@@ -2194,8 +2117,6 @@ def test_linux_topology_ignores_python_cpu_count_override(tmp_path, monkeypatch)def test_linux_non_hybrid_topology_excludes_offline_cores(tmp_path):- from core.inference.llama_cpp import _linux_math_core_count-sibling_sets = ["0,4", "1,5", "2,6", "3,7", "0,4", "1,5", "2,6", "3,7"]for cpu, sibling_set in enumerate(sibling_sets):cpu_path = tmp_path / f"cpu{cpu}" / "topology"@@ -2207,7 +2128,6 @@ def test_linux_non_hybrid_topology_excludes_offline_cores(tmp_path):def test_linux_amd_capacity_classes_keep_all_physical_cores(tmp_path):- from core.inference.llama_cpp import _linux_math_core_countfor cpu in range(24):cpu_path = tmp_path / f"cpu{cpu}"(cpu_path / "topology").mkdir(parents = True)@@ -2225,7 +2145,6 @@ def test_linux_amd_capacity_classes_keep_all_physical_cores(tmp_path):],)def test_linux_smt_and_non_smt_core_counts(tmp_path, sibling_sets, expected):- from core.inference.llama_cpp import _linux_math_core_countfor cpu, sibling_set in enumerate(sibling_sets):cpu_path = tmp_path / f"cpu{cpu}"(cpu_path / "topology").mkdir(parents = True)@@ -2235,15 +2154,11 @@ def test_linux_smt_and_non_smt_core_counts(tmp_path, sibling_sets, expected):def test_invalid_linux_topology_falls_back_to_psutil(monkeypatch):- import core.inference.llama_cpp as llama_mod-class _PhysicalHost:@staticmethoddef cpu_count(logical = True):return 24 if logical else 12- import sys-monkeypatch.setattr(llama_mod, "_linux_math_core_count", lambda: None)monkeypatch.setattr(llama_mod.os, "sched_getaffinity", lambda _pid: set(range(24)), raising = Falsediff --git a/studio/backend/tests/test_ollama_manifest_load_resolution.py b/studio/backend/tests/test_ollama_manifest_load_resolution.pyindex a46e0b030e6..bd7a996f020 100644--- a/studio/backend/tests/test_ollama_manifest_load_resolution.py+++ b/studio/backend/tests/test_ollama_manifest_load_resolution.py@@ -89,6 +89,7 @@ def test_custom_folder_scan_preserves_ollama_rows(tmp_path):def test_registered_custom_ollama_ref_can_be_materialized(tmp_path, monkeypatch):from hub.services.models import ollama+from hub.storage import scan_foldersroot = tmp_path / "registered-ollama"@@ -131,10 +132,10 @@ def test_public_identity_keeps_the_manifest_ref():def test_retagged_manifest_replaces_one_hardlink_and_invalidates_loaded_identity(tmp_path, monkeypatch):+ from hub.services.models import ollamafrom types import SimpleNamespacefrom core.inference.llama_cpp import GgufLoadIntent, LlamaCppBackend- from hub.services.models import ollamaroot = tmp_path / "ollama-retagged"tag_file = _write_ollama_store(root)@@ -308,10 +309,10 @@ def test_failed_main_retag_restores_the_previous_projector(tmp_path, monkeypatchdef test_materialization_lease_blocks_a_concurrent_retag(tmp_path, monkeypatch):- import threading-from hub.services.models import ollama+ import threading+root = tmp_path / "ollama-materialization-lease"tag_file = _write_ollama_store(root)monkeypatch.setattr(ollama, "ollama_model_dirs", lambda: [root])@@ -347,12 +348,13 @@ def test_materialization_lease_blocks_a_concurrent_retag(tmp_path, monkeypatch):def test_waiting_route_lease_does_not_starve_the_default_executor(tmp_path, monkeypatch):+ from hub.services.models import ollama+import asyncioimport threadingfrom concurrent.futures import ThreadPoolExecutorfrom contextlib import ExitStack- from hub.services.models import ollamafrom models.inference import ValidateModelRequestfrom routes import inference@@ -430,10 +432,10 @@ def test_projector_removal_deletes_the_stale_link(tmp_path, monkeypatch):def test_projector_only_retag_invalidates_loaded_identity(tmp_path, monkeypatch):+ from hub.services.models import ollamafrom types import SimpleNamespacefrom core.inference.llama_cpp import GgufLoadIntent, LlamaCppBackend- from hub.services.models import ollamaroot = tmp_path / "ollama-projector-retagged"tag_file = _write_ollama_store(@@ -472,9 +474,10 @@ def test_projector_only_retag_invalidates_loaded_identity(tmp_path, monkeypatch)def test_ollama_intent_loads_the_link_but_keeps_the_manifest_identity(tmp_path, monkeypatch):+ from models.inference import LoadRequest+ from routes.inference import _resolve_model_identifier_for_requestfrom types import SimpleNamespace- from models.inference import LoadRequestfrom routes.inference import (_active_gguf_intent,_LoadPlacement,@@ -613,6 +616,7 @@ def test_an_ignorable_layer_does_not_hide_the_primary_model(tmp_path, monkeypatc# Neither layer stops llama.cpp loading the model layer beside it, so neither is a# reason to withhold the row: a draft model only accelerates speculative decoding,# and ollama has ignored the embed layer since 0.1.2.+from hub.services.models import ollamaroot = tmp_path / f"ollama-{ignorable.rpartition('.')[2]}"@@ -630,6 +634,7 @@ def test_one_unsupported_layer_still_withholds_beside_the_metadata():# Pins each unsupported type on its own. The withholding test above carries both at# once, so admitting just one of them back into the loadable set would still pass it.+from hub.services.models import ollamaroot = tmp_path / f"ollama-{unsupported.rpartition('.')[2]}"diff --git a/studio/backend/tests/test_openai_audio_speech_route.py b/studio/backend/tests/test_openai_audio_speech_route.pyindex 4264a2d68de..9a8ad15e18e 100644--- a/studio/backend/tests/test_openai_audio_speech_route.py+++ b/studio/backend/tests/test_openai_audio_speech_route.py@@ -23,6 +23,16 @@ import routes.inference as routes_modulefrom auth.authentication import get_current_subjectfrom routes.inference import routerfrom utils.api_errors import install_api_error_handlers+from core.inference.external_provider import ExternalProviderClient+from models.inference import AudioSpeechRequest+import core.inference.audio_gallery as gallery+import core.inference.external_provider as provider_module+import threading+++async def _boom(text):+ raise HTTPException(status_code = 400, detail = "No model loaded.")+_WAV = b"RIFF\x24\x00\x00\x00WAVEfmt fake-payload"@@ -261,8 +271,6 @@ def test_the_budget_is_rechecked_after_an_idle_model_is_restored():def test_the_gallery_is_bounded_so_an_api_client_cannot_fill_the_disk(monkeypatch, tmp_path):- import core.inference.audio_gallery as gallery-monkeypatch.setattr(gallery, "gallery_dir", lambda: tmp_path)monkeypatch.setenv("UNSLOTH_AUDIO_GALLERY_MAX_CLIPS", "3")meta = {@@ -284,8 +292,6 @@ def test_the_gallery_is_bounded_so_an_api_client_cannot_fill_the_disk(monkeypatcdef test_the_gallery_is_bounded_by_bytes_not_only_by_count(monkeypatch, tmp_path):"""A count alone does not bound the disk: 2000 clips of maximum-length speech is tensof gigabytes, and stopping /v1/audio/speech filling the disk is what the cap is for."""- import core.inference.audio_gallery as gallery-monkeypatch.setattr(gallery, "gallery_dir", lambda: tmp_path)monkeypatch.setenv("UNSLOTH_AUDIO_GALLERY_MAX_CLIPS", "1000")monkeypatch.setenv("UNSLOTH_AUDIO_GALLERY_MAX_BYTES", str(4 * 1024))@@ -307,8 +313,6 @@ def test_the_gallery_is_bounded_by_bytes_not_only_by_count(monkeypatch, tmp_pathdef test_one_oversized_clip_is_still_returned_rather_than_pruned_immediately(monkeypatch, tmp_path):"""The newest clip is the one the caller just generated. Pruning it because it aloneexceeds the quota would read as a silent failure."""- import core.inference.audio_gallery as gallery-monkeypatch.setattr(gallery, "gallery_dir", lambda: tmp_path)monkeypatch.setenv("UNSLOTH_AUDIO_GALLERY_MAX_BYTES", "64")meta = {@@ -514,10 +518,6 @@ def test_external_upstream_error_is_502(monkeypatch):def test_external_disconnect_cancels_the_upstream_request(monkeypatch):- import asyncio-- from models.inference import AudioSpeechRequest-_install_external(monkeypatch)upstream_cancelled = asyncio.Event()@@ -603,7 +603,6 @@ def test_external_rejects_a_legacy_key_snapshotted_for_another_base_url(monkeypadef test_external_tts_drops_the_local_keepwarm_count_before_proxy(monkeypatch):from core.inference import llama_keepwarm- from models.inference import AudioSpeechRequestmonkeypatch.setattr(llama_keepwarm, "_inflight", 1)monkeypatch.setattr(llama_keepwarm, "_pending", 0)@@ -634,8 +633,6 @@ def test_external_tts_drops_the_local_keepwarm_count_before_proxy(monkeypatch):})- import asyncio-asyncio.run(routes_module.openai_audio_speech(AudioSpeechRequest(@@ -652,11 +649,6 @@ def test_external_tts_drops_the_local_keepwarm_count_before_proxy(monkeypatch):def test_provider_client_appends_speech_path_before_the_base_query(monkeypatch):- import asyncio-- from core.inference.external_provider import ExternalProviderClient- import core.inference.external_provider as provider_module-sent = {}class _Response:@@ -684,14 +676,10 @@ def test_provider_client_appends_speech_path_before_the_base_query(monkeypatch):def test_provider_client_merges_concatenated_wav_segments(monkeypatch):- import asyncioimport ioimport structimport wave- from core.inference.external_provider import ExternalProviderClient- import core.inference.external_provider as provider_module-def _wav(frames, rate = 24_000):output = io.BytesIO()with wave.open(output, "wb") as writer:@@ -755,12 +743,6 @@ def test_provider_client_merges_concatenated_wav_segments(monkeypatch):def test_provider_client_merges_wav_off_the_event_loop(monkeypatch):- import asyncio- import threading-- from core.inference.external_provider import ExternalProviderClient- import core.inference.external_provider as provider_module-merge_started = threading.Event()release_merge = threading.Event()@@ -798,12 +780,6 @@ def test_provider_client_merges_wav_off_the_event_loop(monkeypatch):def test_cancelling_provider_speech_stops_the_wav_worker(monkeypatch):- import asyncio- import threading-- from core.inference.external_provider import ExternalProviderClient- import core.inference.external_provider as provider_module-merge_started = threading.Event()merge_cancel_seen = threading.Event()merge_stopped = threading.Event()@@ -850,11 +826,6 @@ def test_cancelling_provider_speech_stops_the_wav_worker(monkeypatch):def test_external_provider_reads_do_not_block_the_event_loop(monkeypatch):- import asyncio- import threading-- from models.inference import AudioSpeechRequest-_install_external(monkeypatch)original_get_provider = routes_module.providers_db.get_providerread_started = threading.Event()@@ -904,10 +875,6 @@ def test_external_provider_reads_do_not_block_the_event_loop(monkeypatch):def test_external_rejects_a_cross_process_provider_edit_after_resolving_its_key(monkeypatch):- import asyncio-- from models.inference import AudioSpeechRequest-old_config = {"provider_type": "custom","display_name": "Old TTS",@@ -965,9 +932,6 @@ def test_speech_opens_a_monitor_row(monkeypatch):def test_tts_failure_records_an_error_row(monkeypatch):- async def _boom(text):- raise HTTPException(status_code = 400, detail = "No model loaded.")-cli, calls, saved = _make_client(monkeypatch, generate = _boom)api_monitor.clear()assert cli.post("/v1/audio/speech", json = {"input": "hi"}).status_code == 400@@ -1018,9 +982,6 @@ def test_a_failure_before_the_relabel_does_not_leak_the_requested_path(overlay polls and serves. Windows and UNC forms are covered because redacting a hostpath is the whole point."""- async def _boom(text):- raise HTTPException(status_code = 400, detail = "No model loaded.")-cli, calls, saved = _make_client(monkeypatch, generate = _boom)api_monitor.clear()resp = cli.post("/v1/audio/speech", json = {"input": "hi", "model": requested})@@ -1033,9 +994,6 @@ def test_a_failure_before_the_relabel_does_not_leak_the_requested_path(def test_an_ordinary_model_id_is_still_recorded_verbatim(monkeypatch):# The redaction must not rewrite the ids clients actually send.- async def _boom(text):- raise HTTPException(status_code = 400, detail = "No model loaded.")-cli, calls, saved = _make_client(monkeypatch, generate = _boom)for requested in ("tts-1", "gpt-4o-mini-tts", "unsloth/orpheus-3b-0.1-ft"):api_monitor.clear()diff --git a/studio/backend/tests/test_openai_auto_download.py b/studio/backend/tests/test_openai_auto_download.pyindex a4fa872ebf5..0e4bce536f7 100644--- a/studio/backend/tests/test_openai_auto_download.py+++ b/studio/backend/tests/test_openai_auto_download.py@@ -19,6 +19,10 @@ from core.inference import llama_keepwarmfrom core.inference import openai_auto_download as auto_dlfrom core.inference.local_model_resolver import warm_index_soon as _real_warm_index_soonfrom utils import openai_auto_switch_settings as settings+from core.inference import local_model_resolver as resolver+from core.inference.api_monitor import api_monitor+from hub.services.models import downloads+import threadingclass _Sibling:@@ -139,7 +143,6 @@ def test_the_hub_error_helper_carries_a_status_on_both_majors():def hub(monkeypatch):"""Wire the whole remote surface to fakes and record what was dispatched."""import huggingface_hub- from hub.services.models import downloadsstate = {"info": _gguf_repo_info(),@@ -525,7 +528,6 @@ def test_repeat_request_reports_progress_without_reprobing(hub, monkeypatch):def test_progress_is_scaled_to_a_percentage(monkeypatch):# The hub service reports a 0-1 fraction; a raw 0.492 would render as "0%".- from hub.services.models import downloadsasync def _fraction(repo_id,@@ -588,7 +590,6 @@ def test_an_adoptable_dispatch_still_tracks_the_existing_job(hub):def test_a_failed_status_probe_does_not_end_the_watch(hub, monkeypatch):# A probe that raised says nothing: reading it as "idle" freed the slot mid-download.- from hub.services.models import downloadsasync def _boom(repo_id, gguf_variant = ""):raise RuntimeError("registry unavailable")@@ -614,7 +615,6 @@ def test_a_hanging_code_probe_does_not_pin_the_slot(hub, monkeypatch):# hf_hub_download and auth_check take no timeout and run while the provisional slot# is held, so an unresponsive Hub stalled the request and reported every other model# busy. Unchecked is not cleared, so the bounded probe refuses instead of admitting.- import threadingentered, release = threading.Event(), threading.Event()@@ -646,7 +646,6 @@ def test_a_hanging_code_probe_does_not_pin_the_slot(hub, monkeypatch):def test_a_hanging_auth_check_falls_through_to_the_download(hub, monkeypatch):# Inconclusive, not denied: the download's own auth is the real gate, so a slow# gated-repo check must not turn into a refusal.- import threadinghub["info"].gated = Truerelease = threading.Event()@@ -772,8 +771,6 @@ def test_setting_off_does_nothing_at_all(hub):def test_hook_raises_the_openai_envelope_with_retry_after(hub):- from fastapi import HTTPException-with pytest.raises(HTTPException) as excinfo:_hook("unsloth/x-GGUF:UD-Q5_K_XL", _Req(), enabled = True)exc = excinfo.value@@ -785,8 +782,6 @@ def test_hook_raises_the_openai_envelope_with_retry_after(hub):def test_hook_uses_the_anthropic_envelope_on_messages(hub):- from fastapi import HTTPException-with pytest.raises(HTTPException) as excinfo:_hook("unsloth/x-GGUF", _Req(path = "/v1/messages"), enabled = True)detail = excinfo.value.detail@@ -808,7 +803,6 @@ def test_hook_swallows_unexpected_failures(hub, monkeypatch):def _download_rows():- from core.inference.api_monitor import api_monitorreturn [e for e in api_monitor.snapshot() if e["event"] == "download"]@@ -817,9 +811,6 @@ def test_a_ui_session_download_is_not_marked_as_api_traffic(hub):"someone is serving other clients" from "someone is using Unsloth". Unsloth'sown chat hits these same /v1 endpoints with a session JWT, so hardcoding theflag on the download row popped the panel open mid-chat."""- from fastapi import HTTPException- from core.inference.api_monitor import api_monitor-api_monitor.clear()with pytest.raises(HTTPException):# No Authorization header: the UI's session-JWT path.@@ -831,9 +822,7 @@ def test_a_ui_session_download_is_not_marked_as_api_traffic(hub):def test_an_api_key_download_keeps_the_attribution_and_names_its_caller(hub):"""The row is shared, so it needs the subject as well: without one theattribution is reported to every logged-in browser instead of the caller."""- from fastapi import HTTPExceptionfrom auth.authentication import API_KEY_PREFIX- from core.inference.api_monitor import api_monitorapi_monitor.clear()with pytest.raises(HTTPException):@@ -857,9 +846,7 @@ def test_an_api_key_caller_waiting_on_someone_elses_download_gets_a_row(hub):handler's own api_monitor.start. Without a row of its own that call is invisible:the only row is the session's via_api_key=False download, so the overlay staysshut and the monitor presents API traffic as Unsloth's own."""- from fastapi import HTTPExceptionfrom auth.authentication import API_KEY_PREFIX- from core.inference.api_monitor import api_monitorapi_monitor.clear()with pytest.raises(HTTPException):@@ -889,9 +876,7 @@ def test_an_api_key_caller_waiting_on_someone_elses_download_gets_a_row(hub):def test_hook_prefers_the_hub_header_token(hub):- from fastapi import HTTPExceptionfrom hub.dependencies import HUB_HF_TOKEN_HEADER-with pytest.raises(HTTPException):_hook("unsloth/x-GGUF",@@ -1409,7 +1394,6 @@ def test_a_resolver_alias_for_the_resident_model_is_not_refused(monkeypatch):def test_the_request_path_never_triggers_a_model_index_rescan(monkeypatch):# The scan takes seconds under a lock, so this hook must answer from the last built index.- from core.inference import local_model_resolver as resolverscans = []warmed = []@@ -1440,7 +1424,6 @@ def test_the_request_path_never_triggers_a_model_index_rescan(monkeypatch):def test_a_cold_index_is_scanned_rather_than_read_as_nothing_here(monkeypatch):# With no cached evidence yet, reading that as "not downloaded" answers a named# local model with the resident one. Pay the scan once, off the loop.- from core.inference import local_model_resolver as resolverentry = resolver._LocalGgufEntry("org/other", "/srv/models/org--other", ("Q4_K_M",))scans = []@@ -1477,9 +1460,6 @@ def test_a_cold_index_is_scanned_rather_than_read_as_nothing_here(monkeypatch):def test_a_cold_scan_that_never_finishes_says_so_instead_of_guessing(monkeypatch):# The scan is bounded, but an unfinished one knows nothing about the name, and# falling through would put the resident model behind it: answer "not yet".- import threading-- from core.inference import local_model_resolver as resolvermonkeypatch.setattr(resolver, "_scan", (0.0, {}))monkeypatch.setattr(inference_route, "_COLD_INDEX_WAIT_S", 0.05)@@ -1538,11 +1518,8 @@ def test_a_refusal_is_never_swallowed_by_the_cannot_verify_handler(monkeypatch):def test_warming_the_index_never_waits_on_the_scan_lock(monkeypatch):# _lock is held for the whole scan, so contending for it would park every later request.- import threadingimport time as _time- from core.inference import local_model_resolver as resolver-monkeypatch.setattr(resolver, "_scan", (0.0, {}))released = threading.Event()monkeypatch.setattr(resolver, "_build_index", lambda: (released.wait(5), {})[1])@@ -1563,11 +1540,8 @@ def test_warming_the_index_never_waits_on_the_scan_lock(monkeypatch):def test_invalidation_during_a_warm_preserves_a_second_scan(monkeypatch):- import threadingimport time as _time- from core.inference import local_model_resolver as resolver-first_scan_started = threading.Event()release_first_scan = threading.Event()invalidation_finished = threading.Event()@@ -1624,7 +1598,6 @@ def test_invalidation_during_a_warm_preserves_a_second_scan(monkeypatch):def test_a_stale_index_is_refreshed_so_a_hub_download_becomes_visible(monkeypatch):# Only the auto-download watcher calls invalidate_index, so a Hub UI download is seen# only if the warm can run again.- from core.inference import local_model_resolver as resolverscans = []monkeypatch.setattr(resolver, "_build_index", lambda: scans.append(1) or {})@@ -1641,7 +1614,6 @@ def test_a_stale_index_is_refreshed_so_a_hub_download_becomes_visible(monkeypatcdef test_an_id_v1_models_advertised_is_refused_before_the_resolver_warms(monkeypatch):# /v1/models can advertise an unloaded local GGUF while the resolver index is cold. A bare# id has no quant to refuse on, so without that evidence the resident model would answer.- from core.inference import local_model_resolver as resolvermonkeypatch.setattr(resolver, "_scan", (0.0, {}))# Stub the walk: a real multi-root scan inside the cold-wait budget makes this@@ -1671,7 +1643,6 @@ def test_an_id_v1_models_advertised_is_refused_before_the_resolver_warms(monkeypdef test_an_advertised_alias_for_the_resident_weights_is_still_served(monkeypatch):# The flip side: the catalog can list the resident weights under an alias, which is not# evidence of a different model.- from core.inference import local_model_resolver as resolvermonkeypatch.setattr(resolver, "_scan", (0.0, {}))# Stub the walk: a real multi-root scan inside the cold-wait budget makes this@@ -1750,7 +1721,6 @@ def test_a_quant_request_is_not_satisfied_by_transformers_weights(monkeypatch):# A Transformers model active from a directory that also holds GGUF exports resolves# to that directory, so the path match let admission answer an explicit quant with# the safetensors weights. Only llama.cpp has a quant identity.- from core.inference import local_model_resolver as resolverentry = resolver._LocalGgufEntry("alias", "/srv/models/tuned", ("Q4_K_M",))monkeypatch.setattr(resolver, "_scan", (time.monotonic(), {"alias": entry}))@@ -1823,7 +1793,6 @@ def test_a_timed_out_download_stops_holding_the_slot_once_unprobeable(monkeypatcdef test_a_sibling_quant_in_the_same_directory_is_not_the_resident_one(monkeypatch):# Quants of one repo share a directory, so the path match alone cannot tell them# apart, and an explicit :Q8_0 was answered by a resident Q4_K_M.- from core.inference import local_model_resolver as resolverentry = resolver._LocalGgufEntry("org/model", "/hf/org--model/snap", ("Q4_K_M", "Q8_0"))monkeypatch.setattr(resolver, "_scan", (time.monotonic(), {"org/model": entry}))@@ -1903,7 +1872,6 @@ def test_windows_style_paths_still_match_their_own_directory(monkeypatch):def test_a_bare_request_for_a_just_downloaded_model_is_refused(monkeypatch):# End of the same chain: the note has to reach admission, or a bare request between# the download landing and the scan is served by the resident model.- from core.inference import local_model_resolver as resolvermonkeypatch.setattr(resolver, "_scan", (time.monotonic(), {}))monkeypatch.setattr(resolver, "_just_downloaded", {"org/fresh"})@@ -1925,7 +1893,6 @@ def test_a_non_quant_tag_does_not_tear_down_a_serving_quant(monkeypatch):# _already_serving split on ":" rather than on whether the suffix names a quant, so# org/model:latest against a serving Q8_0 counted as a mismatch and swapped in the# preferred Q4_K_M, for a request either one satisfies.- from core.inference import local_model_resolver as resolverentry = resolver._LocalGgufEntry("org/model", "/hf/org--model/snap", ("Q4_K_M", "Q8_0"))monkeypatch.setattr(resolver, "_scan", (time.monotonic(), {"org/model": entry}))diff --git a/studio/backend/tests/test_openai_auto_switch.py b/studio/backend/tests/test_openai_auto_switch.pyindex 13b0a4b5f99..64eff7ea9a8 100644--- a/studio/backend/tests/test_openai_auto_switch.py+++ b/studio/backend/tests/test_openai_auto_switch.py@@ -21,6 +21,87 @@ import routes.inference as inference_routefrom models.inference import LoadRequestfrom core.inference import local_model_resolver as resolverfrom utils import openai_auto_switch_settings as settings+from core.inference import llama_cpp as llama_cpp_mod+from core.inference import llama_keepwarm as kw+from core.inference.llama_cpp import LlamaCppBackend+from core.inference.llama_keepwarm import _is_inference_path+from models.inference import ChatCompletionRequest+from models.inference import ResponsesRequest+from pathlib import Path+from storage import studio_db+from types import SimpleNamespace+from utils import hf_cache_settings+from utils.hardware import hardware as hw+import base64 as _b64+import inspect+import io+import routes.models as models_route+import routes.settings as settings_route+import storage.studio_db as db+import utils.paths as paths+from core.inference.tools import cached_mcp_tools+from hub.services import download_lifecycle+from models.inference import ChatMessage, ImageContentPart, ImageUrl+import logging+import routes.models as model_routes+import state.tool_policy as _tp+++async def _boom(*a, **k):+ raise _Reached()+++def _reset_keepwarm():+ """Clear the keep-warm counters and mark the model long idle."""+ kw._inflight = 0+ kw._pending = 0+ kw._last_active = time.monotonic() - 3600+ kw._last_unloaded_model = None+ kw._kv_resume = None+++def _vision_gguf_cache_repo(tmp_path):+ """An HF cache repo whose older snapshot holds the weights and newer one the companions."""+ repo = tmp_path / "models--org--Vision-GGUF"+ old = repo / "snapshots" / "weights-revision"+ old.mkdir(parents = True)+ (old / "vision-model-Q4_K_M.gguf").write_bytes(b"GGUF weights")+ newer = repo / "snapshots" / "companion-revision"+ newer.mkdir(parents = True)+ return repo, old, newer+++class _Reached(Exception):+ pass+++class _CountBackend(LlamaCppBackend):+ is_loaded = True+ base_url = "http://127.0.0.1:1"+ _auth_headers = None++ def __init__(self):+ pass+++async def _usable(ids, index_kind = "physical"):+ return True+++class _Proc:+ stderr = None++ def wait(self):+ return 0+++async def receive():+ return {"type": "http.request", "body": b"", "more_body": False}+++async def send(_m):+ pass+# captured before the autouse fixture below pins it, so its own test can reach the real one._REAL_HOST_HAS_NON_GGUF_BACKEND = resolver._host_has_a_non_gguf_backend@@ -34,8 +115,6 @@ def _host_serves_non_gguf(monkeypatch):torch or MLX installed, and an unpinned MLX verdict made the whole file pass or failby platform. Each gate is covered by its own test below."""- from utils.hardware import hardware as hw-monkeypatch.setattr(resolver, "_host_has_a_non_gguf_backend", lambda: True)# the device itself, not the helper, so a test setting DEVICE for itself still wins.monkeypatch.setattr(hw, "DEVICE", hw.DeviceType.CUDA, raising = False)@@ -119,7 +198,6 @@ class _LoadRecorder:self.anonymous_hf_access = anonymous_hf_accessself.speech_codec_path = speech_codec_pathif self.fail:- from fastapi import HTTPExceptionraise HTTPException(status_code = 503, detail = "load failed")self.backend.model_identifier = request.model_pathself.backend.hf_variant = getattr(request, "gguf_variant", None)@@ -132,7 +210,6 @@ class _LoadRecorder:self.backend._openai_gguf_companion_state = resolver.local_gguf_companion_state(tuple(request._gguf_companion_roots))- from core.inference import llama_keepwarm as kwkw.note_model_loaded(self.backend)return None@@ -153,6 +230,29 @@ def _wire(monkeypatch, *, enabled, resolves_to, backend, recorder):monkeypatch.setattr(inference_route, "_preflight_speech_codec_for_switch", lambda *_a: None)+def _wire_on(+ *args,+ enabled = True,+ **kwargs,+):+ """_wire with auto-switch enabled."""+ return _wire(*args, enabled = enabled, **kwargs)+++def _wired(+ monkeypatch,+ backend,+ resolves_to,+ *,+ enabled = True,+ fail = False,+):+ """Load recorder + _wire around a backend: the setup nearly every test below repeats."""+ rec = _LoadRecorder(backend, fail = fail)+ _wire(monkeypatch, enabled = enabled, resolves_to = resolves_to, backend = backend, recorder = rec)+ return backend, rec++async def _noop_reject(*_args, **_kwargs):return None@@ -162,14 +262,11 @@ def _run_hook(model = "some/model"):def test_flag_off_never_loads(monkeypatch):- backend = _FakeBackend("unsloth/A-GGUF")- rec = _LoadRecorder(backend)- _wire(+ backend, rec = _wired(monkeypatch,+ _FakeBackend("unsloth/A-GGUF"),+ ("unsloth/B-GGUF", None, "unsloth/B-GGUF"),enabled = False,- resolves_to = ("unsloth/B-GGUF", None, "unsloth/B-GGUF"),- backend = backend,- recorder = rec,)# Off means no load, but A must not answer as B either: say why instead.with pytest.raises(HTTPException) as excinfo:@@ -180,37 +277,23 @@ def test_flag_off_never_loads(monkeypatch):def test_unknown_model_falls_through(monkeypatch):- backend = _FakeBackend("unsloth/A-GGUF")- rec = _LoadRecorder(backend)- _wire(monkeypatch, enabled = True, resolves_to = None, backend = backend, recorder = rec)+ backend, rec = _wired(monkeypatch, _FakeBackend("unsloth/A-GGUF"), None)_run_hook("gpt-4o-mini")assert rec.calls == []def test_already_loaded_does_not_reload(monkeypatch):- backend = _FakeBackend("unsloth/A-GGUF")- rec = _LoadRecorder(backend)# Case-insensitive match against the loaded identifier.- _wire(- monkeypatch,- enabled = True,- resolves_to = ("unsloth/a-gguf", None, "unsloth/a-gguf"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend("unsloth/A-GGUF"), ("unsloth/a-gguf", None, "unsloth/a-gguf"))_run_hook("unsloth/A-GGUF")assert rec.calls == []def test_known_unloaded_model_switches_once(monkeypatch):- backend = _FakeBackend("unsloth/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend("unsloth/A-GGUF"), ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"))_run_hook("unsloth/B-GGUF:Q4_K_M")assert len(rec.calls) == 1@@ -238,9 +321,8 @@ def test_a_late_cancel_keeps_the_completed_switch_metadata(monkeypatch):backend.is_loaded = Truecancel_event.set()- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = ("/models/B.gguf", "Q4_K_M", "unsloth/B-GGUF"),backend = backend,recorder = _load,@@ -267,9 +349,9 @@ def test_a_late_cancel_keeps_the_completed_switch_metadata(monkeypatch):def test_resident_model_skips_the_filesystem_resolver(monkeypatch):- backend = _FakeBackend("unsloth/Muse-Glimmer-30B-GGUF", "UD-Q4_K_XL")- rec = _LoadRecorder(backend)- _wire(monkeypatch, enabled = True, resolves_to = None, backend = backend, recorder = rec)+ backend, rec = _wired(+ monkeypatch, _FakeBackend("unsloth/Muse-Glimmer-30B-GGUF", "UD-Q4_K_XL"), None+ )warmed = []monkeypatch.setattr(resolver, "warm_index_soon", lambda: warmed.append(1))@@ -284,9 +366,7 @@ def test_resident_model_skips_the_filesystem_resolver(monkeypatch):def test_auto_switch_reads_an_additions_only_snapshot_without_rebuilding_it(monkeypatch):real_resolve = resolver.resolve_local_gguf- backend = _FakeBackend("unsloth/A-GGUF", "Q4_K_M")- rec = _LoadRecorder(backend)- _wire(monkeypatch, enabled = True, resolves_to = None, backend = backend, recorder = rec)+ backend, rec = _wired(monkeypatch, _FakeBackend("unsloth/A-GGUF", "Q4_K_M"), None)calls = []warmed = []@@ -317,12 +397,10 @@ def test_auto_switch_reads_an_additions_only_snapshot_without_rebuilding_it(monkdef test_non_additive_invalidation_keeps_unservable_check_cold(monkeypatch):- from types import SimpleNamespace-real_resolve = resolver.resolve_local_gguf- backend = _FakeBackend("unsloth/A-GGUF", "Q4_K_M")- rec = _LoadRecorder(backend)- _wire(monkeypatch, enabled = False, resolves_to = None, backend = backend, recorder = rec)+ backend, rec = _wired(+ monkeypatch, _FakeBackend("unsloth/A-GGUF", "Q4_K_M"), None, enabled = False+ )monkeypatch.setattr(resolver, "resolve_local_gguf", real_resolve)monkeypatch.setattr(inference_route,@@ -351,9 +429,7 @@ def test_non_additive_invalidation_keeps_unservable_check_cold(monkeypatch):def test_an_expired_positive_hit_refreshes_before_switching(monkeypatch):real_resolve = resolver.resolve_local_gguf- backend = _FakeBackend("unsloth/A-GGUF", "Q4_K_M")- rec = _LoadRecorder(backend)- _wire(monkeypatch, enabled = True, resolves_to = None, backend = backend, recorder = rec)+ backend, rec = _wired(monkeypatch, _FakeBackend("unsloth/A-GGUF", "Q4_K_M"), None)removed = resolver._LocalGgufEntry("unsloth/B-GGUF", "/removed-root/unsloth/B-GGUF", ("Q4_K_M",))@@ -391,9 +467,7 @@ def test_an_expired_positive_hit_refreshes_before_switching(monkeypatch):def test_a_stale_miss_refreshes_before_the_resident_model_can_answer(monkeypatch):real_resolve = resolver.resolve_local_gguf- backend = _FakeBackend("unsloth/A-GGUF", "Q4_K_M")- rec = _LoadRecorder(backend)- _wire(monkeypatch, enabled = True, resolves_to = None, backend = backend, recorder = rec)+ backend, rec = _wired(monkeypatch, _FakeBackend("unsloth/A-GGUF", "Q4_K_M"), None)old = resolver._LocalGgufEntry("unsloth/A-GGUF", "/models/unsloth/A-GGUF", ("Q4_K_M",))added = resolver._LocalGgufEntry("unsloth/B-GGUF", "/models/unsloth/B-GGUF", ("Q4_K_M",))monkeypatch.setattr(resolver, "_scan", (1.0, {"unsloth/a-gguf": old}))@@ -425,14 +499,8 @@ def test_a_stale_miss_refreshes_before_the_resident_model_can_answer(monkeypatchdef test_concurrent_same_target_loads_once(monkeypatch):- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("unsloth/B-GGUF", None, "unsloth/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend(None), ("unsloth/B-GGUF", None, "unsloth/B-GGUF"))async def _race():@@ -446,16 +514,11 @@ def test_concurrent_same_target_loads_once(monkeypatch):def test_load_failure_propagates(monkeypatch):- from fastapi import HTTPException-- backend = _FakeBackend("unsloth/A-GGUF")- rec = _LoadRecorder(backend, fail = True)- _wire(+ backend, rec = _wired(monkeypatch,- enabled = True,- resolves_to = ("unsloth/B-GGUF", None, "unsloth/B-GGUF"),- backend = backend,- recorder = rec,+ _FakeBackend("unsloth/A-GGUF"),+ ("unsloth/B-GGUF", None, "unsloth/B-GGUF"),+ fail = True,)with pytest.raises(HTTPException):_run_hook("unsloth/B-GGUF")@@ -463,14 +526,10 @@ def test_load_failure_propagates(monkeypatch):def test_same_repo_different_variant_switches(monkeypatch):# Q4_K_M loaded, Q8_0 requested: a different quant must trigger a reload.- backend = _FakeBackend("unsloth/B-GGUF", hf_variant = "Q4_K_M")- rec = _LoadRecorder(backend)- _wire(+ backend, rec = _wired(monkeypatch,- enabled = True,- resolves_to = ("unsloth/B-GGUF", "Q8_0", "unsloth/B-GGUF"),- backend = backend,- recorder = rec,+ _FakeBackend("unsloth/B-GGUF", hf_variant = "Q4_K_M"),+ ("unsloth/B-GGUF", "Q8_0", "unsloth/B-GGUF"),)_run_hook("unsloth/B-GGUF:Q8_0")assert len(rec.calls) == 1@@ -480,9 +539,8 @@ def test_same_repo_different_variant_switches(monkeypatch):def test_same_repo_same_variant_does_not_reload(monkeypatch):backend = _FakeBackend("unsloth/B-GGUF", hf_variant = "Q4_K_M")rec = _LoadRecorder(backend)- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = ("unsloth/B-GGUF", "q4_k_m", "unsloth/B-GGUF"), # case-insensitivebackend = backend,recorder = rec,@@ -493,7 +551,6 @@ def test_same_repo_same_variant_does_not_reload(monkeypatch):def test_responses_endpoint_wires_auto_switch_before_streaming_dispatch():# streaming bypasses chat, so its switch must run before dispatch.- import inspectsrc = inspect.getsource(inference_route.openai_responses)assert "_maybe_auto_switch_model" in src@@ -503,8 +560,6 @@ def test_responses_endpoint_wires_auto_switch_before_streaming_dispatch():def test_nonstreaming_responses_passes_subject_to_chat(monkeypatch):- from models.inference import ResponsesRequest-seen = {}async def _capture(_payload, _request, current_subject):@@ -532,7 +587,6 @@ def test_nonstreaming_responses_passes_subject_to_chat(monkeypatch):def test_embeddings_endpoint_wires_auto_switch_before_loaded_check():# /v1/embeddings is model-bearing too, so it must auto-switch before the# loaded-state gate. Asserted on the source for order-independence.- import inspectsrc = inspect.getsource(inference_route.openai_embeddings)assert "_auto_switch_from_request_body" in src@@ -541,7 +595,6 @@ def test_embeddings_endpoint_wires_auto_switch_before_loaded_check():def test_count_tokens_endpoint_wires_auto_switch_before_loaded_check():# The Anthropic token-count endpoint must count with the requested model.- import inspectsrc = inspect.getsource(inference_route.anthropic_count_tokens)assert "_maybe_auto_switch_model" in src@@ -588,8 +641,6 @@ def test_openai_compat_routes_bound_to_handlers_with_auth():def test_local_gguf_entry_filters_non_gguf_and_recurses(tmp_path):- from types import SimpleNamespace-# Transformers/safetensors folder: not a GGUF, must be rejected.tf = tmp_path / "tf-model"tf.mkdir()@@ -615,7 +666,6 @@ def test_local_gguf_entry_filters_non_gguf_and_recurses(tmp_path):def test_local_gguf_entry_labels_a_quantless_filename_as_the_picker_does(tmp_path):# No quant token in the name, so the picker and every saved setting key this variant by# the stem. Labelling it "v2" here left an API load reading a key nothing wrote (#10227).- from types import SimpleNamespacefrom utils.openai_auto_switch_settings import override_lookup_candidates@@ -635,7 +685,6 @@ def test_local_gguf_entry_labels_a_quantless_filename_as_the_picker_does(tmp_patdef test_local_gguf_entry_skips_a_quant_whose_blob_is_gone(tmp_path):# An evicted blob leaves a dangling link, and advertising it offers a quant no load can# open. The twins repo is the other direction: one quant, two checkpoints, dead one first.- from types import SimpleNamespaceevicted = tmp_path / "models--org--evicted" / "snapshots" / "abc"evicted.mkdir(parents = True)@@ -663,8 +712,6 @@ def test_local_gguf_entry_skips_a_quant_whose_blob_is_gone(tmp_path):def _one_gguf_repo(tmp_path, name, filenames):"""An HF-cache repo dir holding *filenames*, and the entry the index builds for it."""- from types import SimpleNamespace-snapshot = tmp_path / f"models--org--{name}" / "snapshots" / "abc"snapshot.mkdir(parents = True)for index, filename in enumerate(filenames):@@ -735,7 +782,6 @@ def test_a_bundle_repos_mirror_is_not_emptied_by_the_h3_filter(tmp_path):# A cache dir name only ever extends a repo id by more of that id, so these contain the# bundle marker while being ordinary chat repos. Matched as a substring, the denoiser filter# left them with no quants, which withholds the entry and 404s a downloaded model.- from types import SimpleNamespacefor name in ("MiniMax-H3-GGUF-mirror", "minimax-h3-gguf-i1", "MiniMax-H3-GGUF-BF16"):snapshot = tmp_path / f"models--unsloth--{name}" / "snapshots" / "abc"snapshot.mkdir(parents = True)@@ -785,7 +831,6 @@ def test_local_gguf_entry_rejects_standalone_companions(tmp_path, monkeypatch):# bare mmproj projector (it only filters mmproj inside directory scans). A# projector is not a servable model, so the resolver must reject it or# /v1/models advertises it and a switch could load it over the real weights.- from types import SimpleNamespaceproj = tmp_path / "mmproj-F16.gguf"proj.write_text("x")@@ -920,8 +965,6 @@ def test_setting_coercion():def test_idle_loop_does_not_unload_freshly_loaded_model(monkeypatch):# Server idle far longer than the TTL, then a model is loaded: the load# transition stamps activity so the next poll must not unload it.- import time- from core.inference import llama_keepwarm as kwmonkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: 1)monkeypatch.setattr(settings, "idle_unload_is_configured", lambda: 1 > 0)@@ -950,8 +993,6 @@ def test_idle_loop_unloads_after_ttl_and_stashes_for_reload(monkeypatch):# The headline behavior (the other idle tests only cover the negative paths):# with nothing in flight and the TTL elapsed, the loop frees the GGUF exactly# once and records its identity so a later alias request can reload that variant.- import time- from core.inference import llama_keepwarm as kwmonkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: 0.005)monkeypatch.setattr(settings, "idle_unload_is_configured", lambda: 0.005 > 0)@@ -998,8 +1039,6 @@ def test_idle_loop_unloads_after_ttl_and_stashes_for_reload(monkeypatch):def test_a_request_landing_during_the_pin_read_is_not_unloaded_out_from_under(monkeypatch):# A chat may register _pending during the off-loop pin read, invalidating prior idleness.- import time- from core.inference import llama_keepwarm as kwmonkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: 0.005)monkeypatch.setattr(settings, "idle_unload_is_configured", lambda: True)@@ -1051,17 +1090,10 @@ def test_a_request_landing_during_the_pin_read_is_not_unloaded_out_from_under(modef test_idle_loop_deletes_saved_kv_when_unload_fails(monkeypatch, tmp_path):- import time- from core.inference import llama_keepwarm as kw-monkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: 0.005)monkeypatch.setattr(settings, "idle_unload_is_configured", lambda: 0.005 > 0)monkeypatch.setattr(settings, "get_auto_unload_keep_kv", lambda: True)- kw._inflight = 0- kw._pending = 0- kw._last_active = time.monotonic() - 3600- kw._last_unloaded_model = None- kw._kv_resume = None+ _reset_keepwarm()saved = tmp_path / "resume-abc-slot0.bin"backend = _FakeBackend("unsloth/Idle-GGUF")@@ -1103,8 +1135,6 @@ def test_idle_loop_deletes_saved_kv_when_unload_fails(monkeypatch, tmp_path):def test_disabling_idle_unload_purges_saved_kv(monkeypatch, tmp_path):# PUT leaves keep-KV on but makes idle unload inactive: saved KV must go too.- import routes.settings as settings_route- from core.inference import llama_keepwarm as kwsaved = tmp_path / "resume-abc-slot0.bin"saved.write_bytes(b"kv")@@ -1129,8 +1159,6 @@ def test_disabling_idle_unload_purges_saved_kv(monkeypatch, tmp_path):def test_residency_does_not_purge_saved_kv(monkeypatch, tmp_path):# Residency zeroes the effective TTL, but the user never turned idle unload# off, so saving anything else must leave their saved KV alone.- import routes.settings as settings_route- from core.inference import llama_keepwarm as kwsaved = tmp_path / "resume-abc-slot0.bin"saved.write_bytes(b"kv")@@ -1176,7 +1204,6 @@ def test_idle_unload_is_configured_ignores_the_residency_veto(monkeypatch):def test_audio_generate_is_tracked_as_inference_path():# Direct GGUF TTS uses the llama backend and can outlive the idle TTL, so# the keep-warm middleware must count it as in-flight inference.- from core.inference.llama_keepwarm import _is_inference_pathassert _is_inference_path("/api/inference/audio/generate") is Trueassert _is_inference_path("/v1/chat/completions") is True@@ -1186,8 +1213,6 @@ def test_audio_generate_is_tracked_as_inference_path():def test_idle_loop_does_not_unload_while_request_inflight(monkeypatch):# An in-flight request (inflight > 0) must protect the model from unload# even when it has been idle by wall-clock past the TTL.- import time- from core.inference import llama_keepwarm as kwmonkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: 0.01)monkeypatch.setattr(settings, "idle_unload_is_configured", lambda: 0.01 > 0)@@ -1217,14 +1242,8 @@ def test_idle_loop_does_not_unload_while_request_inflight(monkeypatch):def test_auto_switch_applies_model_override(monkeypatch):# A configured model loads with its saved launch flags, not bare defaults.- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend(None), ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"))monkeypatch.setattr(settings,@@ -1243,14 +1262,8 @@ def test_auto_switch_applies_model_override(monkeypatch):def test_auto_switch_applies_partial_override(monkeypatch):# Only llama_extra_args is configured: it is applied, max_seq_length stays default.- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend(None), ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"))monkeypatch.setattr(settings, "get_model_override", lambda model_id: {"llama_extra_args": ["--flash-attn"]}@@ -1264,8 +1277,6 @@ def test_auto_switch_applies_partial_override(monkeypatch):def _mock_override_store(monkeypatch):"""Back the override read + atomic-merge write with an in-memory dict."""- import storage.studio_db as db-store = {}def _merge_entry(@@ -1311,7 +1322,6 @@ def override_store(monkeypatch):def _put(model_id, **fields):"""One override PUT through the route, spelled the way the UI sends it."""- import routes.settings as settings_routereturn settings_route.update_openai_auto_switch_override(settings_route.ModelOverridePayload(model_id = model_id, **fields),"tester",@@ -1335,9 +1345,6 @@ def test_model_override_roundtrip(monkeypatch):def test_override_route_rejects_managed_flag_and_removes(monkeypatch):- import routes.settings as settings_route- from fastapi import HTTPException-_mock_override_store(monkeypatch)# A managed/denylisted llama-server flag is rejected with 400, not 500.@@ -1405,8 +1412,6 @@ def test_model_override_rejects_zero_max_seq_length():# 0 is not a valid sequence length and the setter drops a falsy value, so the# payload must reject it at the boundary instead of accepting then discarding it.import pydantic- import routes.settings as settings_route-with pytest.raises(pydantic.ValidationError):settings_route.ModelOverridePayload(model_id = "x", max_seq_length = 0)assert settings_route.ModelOverridePayload(model_id = "x", max_seq_length = 1).max_seq_length == 1@@ -1415,8 +1420,6 @@ def test_model_override_rejects_zero_max_seq_length():def test_update_openai_auto_switch_writes_both_keys_in_one_transaction(monkeypatch):# The PUT must persist enabled + idle in a single upsert so a settings write can't# leave one key updated and the other stale.- import routes.settings as settings_route- import storage.studio_db as dbfrom utils.openai_auto_switch_settings import (AUTO_UNLOAD_IDLE_SETTING_KEY,OPENAI_AUTO_SWITCH_SETTING_KEY,@@ -1444,7 +1447,6 @@ def test_settings_report_idle_unload_active_when_env_backed(monkeypatch):# Codex P2: with UNSLOTH_MODEL_IDLE_TTL driving idle-unload while the toggle is# off, the settings response must report idle_unload_active so the UI shows the# feature as active via env rather than "needs enable".- import routes.settings as settings_routemonkeypatch.setattr(settings_route, "get_openai_auto_switch_enabled", lambda: False)monkeypatch.setattr(settings_route, "get_stored_auto_unload_idle_seconds", lambda: 600)@@ -1466,7 +1468,6 @@ def test_v1_models_retrieve_is_case_insensitive(monkeypatch):# from a catalog id must still hit (200), not 404. Guards the .lower() compare# in openai_retrieve_model against a silent revert. (The full local catalog is# main's #6519; only the loaded fast-path is exact, the catalog loop is lenient.)- from fastapi import HTTPExceptionmonkeypatch.setattr(inference_route, "_openai_model_objects", lambda: []) # nothing loaded@@ -1493,8 +1494,6 @@ def test_v1_models_retrieve_is_case_insensitive(monkeypatch):def test_index_excludes_hidden_models(tmp_path, monkeypatch):# The llama.cpp validation probe and RAG embedding weights are hidden from# Unsloth's pickers; they must never become auto-switch targets.- from types import SimpleNamespace- import routes.models as models_routenormal = tmp_path / "normal-Q4_K_M.gguf"normal.write_bytes(b"x" * 32)@@ -1556,7 +1555,6 @@ def test_idle_disabled_when_auto_switch_off(monkeypatch):def test_count_tokens_is_tracked_as_inference_path():# count_tokens counts via the loaded tokenizer, so idle-unload must not pull# the model out from under it; it has to be a tracked in-flight path.- from core.inference.llama_keepwarm import _is_inference_pathassert _is_inference_path("/v1/messages/count_tokens") is Trueassert _is_inference_path("/api/inference/messages/count_tokens") is True@@ -1570,26 +1568,15 @@ def test_count_tokens_is_tracked_as_inference_path():def test_bare_id_tolerates_any_loaded_variant(monkeypatch):# Repo already loaded as Q4_K_M; a BARE request for the same repo (resolver# picks the largest local quant, Q8_0) must NOT reload a different quant.- backend = _FakeBackend("unsloth/B-GGUF", hf_variant = "Q4_K_M")- rec = _LoadRecorder(backend)- _wire(+ backend, rec = _wired(monkeypatch,- enabled = True,- resolves_to = ("unsloth/B-GGUF", "Q8_0", "unsloth/B-GGUF"),- backend = backend,- recorder = rec,+ _FakeBackend("unsloth/B-GGUF", hf_variant = "Q4_K_M"),+ ("unsloth/B-GGUF", "Q8_0", "unsloth/B-GGUF"),)_run_hook("unsloth/B-GGUF") # bare, no :VARIANTassert rec.calls == []# An explicit :VARIANT request still honors the quant (reloads to Q8_0).- rec2 = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("unsloth/B-GGUF", "Q8_0", "unsloth/B-GGUF"),- backend = backend,- recorder = rec2,- )+ _, rec2 = _wired(monkeypatch, backend, ("unsloth/B-GGUF", "Q8_0", "unsloth/B-GGUF"))_run_hook("unsloth/B-GGUF:Q8_0")assert len(rec2.calls) == 1@@ -1597,7 +1584,6 @@ def test_bare_id_tolerates_any_loaded_variant(monkeypatch):def test_responses_hook_runs_after_input_validation():# A request that 400s on empty input must not have triggered a model load,# so the auto-switch hook must come after the input-validation guard.- import inspectsrc = inspect.getsource(inference_route.openai_responses)assert "No input provided" in src@@ -1608,8 +1594,6 @@ def test_responses_system_only_rejected_before_switch(monkeypatch):# Codex P2: instructions-only input normalises to a lone system message, which# passes the empty-input check; it must 400 before the switch so an invalid# Responses request can't evict the resident model.- from fastapi import HTTPException- from models.inference import ResponsesRequestasync def _boom(*a, **k):raise AssertionError("must not switch a system-only Responses request")@@ -1624,7 +1608,6 @@ def test_responses_system_only_rejected_before_switch(monkeypatch):def test_keepwarm_tracks_inflight_when_enabled_even_if_idle_zero(monkeypatch):# In-flight must be counted whenever auto-switch is on, even with idle TTL 0,# so enabling idle mid-stream cannot unload an in-flight request.- from core.inference import llama_keepwarm as kwmonkeypatch.setattr(settings, "get_openai_auto_switch_enabled", lambda: True)kw._inflight = 0@@ -1636,12 +1619,6 @@ def test_keepwarm_tracks_inflight_when_enabled_even_if_idle_zero(monkeypatch):await send({"type": "http.response.body", "body": b"ok", "more_body": False})async def drive():- async def receive():- return {"type": "http.request", "body": b"", "more_body": False}-- async def send(_m):- pass-scope = {"type": "http", "path": "/v1/chat/completions", "method": "POST", "headers": []}await kw.LlamaKeepWarmMiddleware(app)(scope, receive, send)@@ -1665,32 +1642,15 @@ def _bad_body_request():def test_completions_malformed_body_503_not_500_when_unloaded(monkeypatch):# OFF + nothing loaded + unparseable body must still 503 (pre-feature# behavior), not 500 from the early body read.- from fastapi import HTTPException- backend = _FakeBackend(None)- _wire(- monkeypatch,- enabled = False,- resolves_to = None,- backend = backend,- recorder = _LoadRecorder(backend),- )+ backend, rec = _wired(monkeypatch, _FakeBackend(None), None, enabled = False)with pytest.raises(HTTPException) as exc:asyncio.run(inference_route.openai_completions(_bad_body_request(), "tester"))assert exc.value.status_code == 503def test_embeddings_malformed_body_503_not_500_when_unloaded(monkeypatch):- from fastapi import HTTPException-- backend = _FakeBackend(None)- _wire(- monkeypatch,- enabled = False,- resolves_to = None,- backend = backend,- recorder = _LoadRecorder(backend),- )+ backend, rec = _wired(monkeypatch, _FakeBackend(None), None, enabled = False)with pytest.raises(HTTPException) as exc:asyncio.run(inference_route.openai_embeddings(_bad_body_request(), "tester"))assert exc.value.status_code == 503@@ -1700,11 +1660,8 @@ def test_non_string_model_falls_through_without_error(monkeypatch):# A non-string model (e.g. {"model": 123} on a raw-body endpoint) must be# treated as absent, never raising in the membership checks, even when a stash# exists from idle-unload.- from core.inference import llama_keepwarm as kw- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(monkeypatch, enabled = True, resolves_to = None, backend = backend, recorder = rec)+ backend, rec = _wired(monkeypatch, _FakeBackend(None), None)monkeypatch.setattr(kw, "_last_unloaded_model", ("unsloth/A-GGUF", None))asyncio.run(inference_route._maybe_auto_switch_model(123, object(), "tester"))assert rec.calls == [] # no load, no TypeError@@ -1713,7 +1670,6 @@ def test_non_string_model_falls_through_without_error(monkeypatch):def test_anthropic_validates_max_tokens_before_auto_switch():# An Anthropic request missing max_tokens must 400 before the hook runs, so an# invalid request never triggers a model load. Asserted on the source order.- import inspectsrc = inspect.getsource(inference_route.anthropic_messages)assert "_maybe_auto_switch_model" in src@@ -1723,7 +1679,6 @@ def test_anthropic_validates_max_tokens_before_auto_switch():def test_alias_reloads_model_freed_by_idle_unload_with_quant(monkeypatch):# After idle-unload frees the model, an unknown/alias name (resolves to None)# reloads what was freed, including the exact quant, instead of 503-ing.- from core.inference import llama_keepwarm as kwbackend = _FakeBackend(None) # idle-unload emptied the backendrec = _LoadRecorder(backend)@@ -1739,11 +1694,8 @@ def test_alias_reloads_model_freed_by_idle_unload_with_quant(monkeypatch):def test_alias_does_not_reload_when_model_already_loaded(monkeypatch):# The reload only triggers on an empty backend; with something loaded, an# unknown name still falls through (drop-in) without resurrecting the stash.- from core.inference import llama_keepwarm as kw- backend = _FakeBackend("unsloth/B-GGUF")- rec = _LoadRecorder(backend)- _wire(monkeypatch, enabled = True, resolves_to = None, backend = backend, recorder = rec)+ backend, rec = _wired(monkeypatch, _FakeBackend("unsloth/B-GGUF"), None)monkeypatch.setattr(kw, "_last_unloaded_model", ("unsloth/A-GGUF", None))_run_hook("gpt-4o-mini")assert rec.calls == []@@ -1752,7 +1704,6 @@ def test_alias_does_not_reload_when_model_already_loaded(monkeypatch):def test_idle_loop_does_not_unload_while_request_pending(monkeypatch):# A request that has marked itself pending (waiting on the unload gate) but not# yet started must keep the idle loop from unloading the model.- from core.inference import llama_keepwarm as kwmonkeypatch.setattr(kw, "_inflight", 0)monkeypatch.setattr(kw, "_pending", 0)@@ -1768,7 +1719,6 @@ def test_idle_loop_does_not_unload_while_request_pending(monkeypatch):def test_keepwarm_tracks_inflight_even_when_auto_switch_off(monkeypatch):# A stream that starts while the feature is OFF must still be counted, so# enabling idle-unload mid-stream cannot unload it.- from core.inference import llama_keepwarm as kwmonkeypatch.setattr(settings, "get_openai_auto_switch_enabled", lambda: False)monkeypatch.setattr(kw, "_inflight", 0)@@ -1780,12 +1730,6 @@ def test_keepwarm_tracks_inflight_even_when_auto_switch_off(monkeypatch):await send({"type": "http.response.body", "body": b"ok", "more_body": False})async def drive():- async def receive():- return {"type": "http.request", "body": b"", "more_body": False}-- async def send(_m):- pass-scope = {"type": "http", "path": "/v1/chat/completions", "method": "POST", "headers": []}await kw.LlamaKeepWarmMiddleware(app)(scope, receive, send)@@ -1797,10 +1741,7 @@ def test_keepwarm_tracks_inflight_even_when_auto_switch_off(monkeypatch):def test_build_index_covers_legacy_default_lmstudio_and_custom_roots(monkeypatch, tmp_path):# _build_index must scan the same roots the model picker lists, else a model# the UI shows is silently served as the loaded one. Verify each is consulted.- from pathlib import Path- import routes.models as models_routefrom utils import paths as upaths- from utils import hf_cache_settingsimport storage.studio_db as studio_dbscanned = []@@ -1905,7 +1846,6 @@ def _json_body_request(payload):def test_completions_list_body_is_400_not_500(monkeypatch):# A valid JSON non-dict body (e.g. a list) on a loaded backend is a clean 400,# not a 500 from body.get(...).- from fastapi import HTTPExceptionbackend = _FakeBackend("unsloth/A-GGUF") # loaded_wire(@@ -1921,16 +1861,7 @@ def test_completions_list_body_is_400_not_500(monkeypatch):def test_embeddings_list_body_is_400_not_500(monkeypatch):- from fastapi import HTTPException-- backend = _FakeBackend("unsloth/A-GGUF")- _wire(- monkeypatch,- enabled = False,- resolves_to = None,- backend = backend,- recorder = _LoadRecorder(backend),- )+ backend, rec = _wired(monkeypatch, _FakeBackend("unsloth/A-GGUF"), None, enabled = False)with pytest.raises(HTTPException) as exc:asyncio.run(inference_route.openai_embeddings(_json_body_request([]), "tester"))assert exc.value.status_code == 400@@ -1938,7 +1869,6 @@ def test_embeddings_list_body_is_400_not_500(monkeypatch):def test_middleware_ignores_non_post(monkeypatch):# CORS preflight (OPTIONS) on an inference path must not be tracked as in-flight.- from core.inference import llama_keepwarm as kwmonkeypatch.setattr(kw, "_inflight", 0)seen = {}@@ -1949,12 +1879,6 @@ def test_middleware_ignores_non_post(monkeypatch):await send({"type": "http.response.body", "body": b"", "more_body": False})async def drive():- async def receive():- return {"type": "http.request", "body": b"", "more_body": False}-- async def send(_m):- pass-scope = {"type": "http", "path": "/v1/chat/completions", "method": "OPTIONS", "headers": []}await kw.LlamaKeepWarmMiddleware(app)(scope, receive, send)@@ -1969,16 +1893,9 @@ def test_middleware_ignores_non_post(monkeypatch):def test_auto_switch_waits_for_another_inference_to_finish(monkeypatch):# A cross-model swap queues while another request is generating, then loads# after that request drains. The requesting call itself is excluded.- from core.inference import llama_keepwarm as kw- backend = _FakeBackend("org/A-GGUF", hf_variant = "Q4_K_M")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/p/B", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend("org/A-GGUF", hf_variant = "Q4_K_M"), ("/p/B", "Q8_0", "org/B-GGUF"))monkeypatch.setattr(kw, "_inflight", 2) # this request + another active onemonkeypatch.setattr(kw, "_pending", 0)@@ -1998,17 +1915,8 @@ def test_auto_switch_waits_for_another_inference_to_finish(monkeypatch):def test_auto_switch_swaps_when_only_caller_is_active(monkeypatch):# Only the caller is in flight: nothing else to protect, so the swap proceeds.- from core.inference import llama_keepwarm as kw- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/p/B", None, "org/B-GGUF"),- backend = backend,- recorder = rec,- )+ backend, rec = _wired(monkeypatch, _FakeBackend("org/A-GGUF"), ("/p/B", None, "org/B-GGUF"))monkeypatch.setattr(kw, "_inflight", 1)monkeypatch.setattr(kw, "_pending", 0)_run_hook("org/B-GGUF")@@ -2019,8 +1927,6 @@ def test_auto_switch_swaps_when_only_caller_is_active(monkeypatch):def test_idle_loop_resets_timer_for_same_repo_different_variant(monkeypatch):# Same repo, different quant counts as a fresh model: the idle timer resets, so# the new variant is not unloaded before one TTL of its own.- import time- from core.inference import llama_keepwarm as kwmonkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: 0.05)monkeypatch.setattr(settings, "idle_unload_is_configured", lambda: 0.05 > 0)@@ -2050,16 +1956,12 @@ def test_idle_loop_resets_timer_for_same_repo_different_variant(monkeypatch):def test_generate_stream_is_tracked_as_inference_path():- from core.inference.llama_keepwarm import _is_inference_path-assert _is_inference_path("/api/inference/generate/stream") is Trueassert _is_inference_path("/api/inference/audio/generate") is Trueassert _is_inference_path("/v1/responses") is Truedef test_successful_manual_load_clears_last_unloaded_stash():- from core.inference import llama_keepwarm as kw-kw._set_last_unloaded(("org/A-GGUF", "Q4_K_M"))assert kw.get_last_unloaded_model() == ("org/A-GGUF", "Q4_K_M")kw.note_model_loaded()@@ -2069,7 +1971,6 @@ def test_successful_manual_load_clears_last_unloaded_stash():def test_hf_cache_entry_loads_from_local_snapshot_path(tmp_path):# An HF-cache repo resolves to its on-disk snapshot dir, so /load takes the# local branch (no repo-id download). loader_id stays the repo id.- from types import SimpleNamespacerepo = tmp_path / "models--org--Repo"snap = repo / "snapshots" / "abc123"@@ -2088,10 +1989,6 @@ def test_hf_cache_entry_loads_from_local_snapshot_path(tmp_path):def test_hf_cache_entry_skips_newer_companion_only_snapshot(tmp_path):"""A companion fetched at a newer revision must not hide older complete weights."""- import os- from pathlib import Path- from types import SimpleNamespace-repo = tmp_path / "models--unsloth--Qwen3.8-Flash-Next-GGUF"old = repo / "snapshots" / "weights-revision"quant_dir = old / "UD-Q4_K_XL"@@ -2133,9 +2030,6 @@ def test_hf_cache_entry_skips_newer_companion_only_snapshot(tmp_path):def test_snapshot_selection_preserves_legacy_alias_companion_scope(tmp_path, repo_level, filename, legacy, canonical):- import os- from types import SimpleNamespace-repo = tmp_path / "models--org--alias-scope"old = repo / "snapshots" / "weights"newer = repo / "snapshots" / "companions"@@ -2171,20 +2065,13 @@ def test_snapshot_selection_preserves_legacy_alias_companion_scope(def test_hf_cache_entry_keeps_newer_companions_for_auto_switch(tmp_path, monkeypatch):"""Selected weights stay pinned while request and load probes see companions."""- from pathlib import Path- from types import SimpleNamespacefrom utils.models import model_config as model_config_modulefrom utils.models.drafters import dflash as dflash_moduleModelConfig = model_config_module.ModelConfigdetect_mmproj_file = model_config_module.detect_mmproj_file- repo = tmp_path / "models--org--Vision-GGUF"- old = repo / "snapshots" / "weights-revision"- old.mkdir(parents = True)- (old / "vision-model-Q4_K_M.gguf").write_bytes(b"GGUF weights")- newer = repo / "snapshots" / "companion-revision"- newer.mkdir(parents = True)+ repo, old, newer = _vision_gguf_cache_repo(tmp_path)mmproj = newer / "mmproj-vision-model-F16.gguf"mmproj.write_bytes(b"GGUF companion")mtp = newer / "mtp-vision-model-Q4_0.gguf"@@ -2240,16 +2127,9 @@ def test_hf_cache_entry_keeps_newer_companions_for_auto_switch(tmp_path, monkeypdef test_hf_cache_entry_skips_unreadable_sibling_for_mmproj(tmp_path, monkeypatch):- from pathlib import Path- from types import SimpleNamespacefrom utils.models import model_config as model_config_module- repo = tmp_path / "models--org--Vision-GGUF"- old = repo / "snapshots" / "weights-revision"- old.mkdir(parents = True)- (old / "vision-model-Q4_K_M.gguf").write_bytes(b"GGUF weights")- newer = repo / "snapshots" / "companion-revision"- newer.mkdir(parents = True)+ repo, old, newer = _vision_gguf_cache_repo(tmp_path)(newer / "mmproj-vision-model-F16.gguf").write_bytes(b"GGUF companion")unreadable = repo / "snapshots" / "unreadable-revision"unreadable.mkdir(parents = True)@@ -2309,8 +2189,6 @@ def test_image_preflight_uses_the_projector_selected_for_load(tmp_path, monkeypadef test_companion_roots_skip_only_the_unreadable_sibling(tmp_path, monkeypatch):- from pathlib import Path-repo = tmp_path / "models--org--Vision-GGUF"selected = repo / "snapshots" / "weights-revision"readable = repo / "snapshots" / "companion-revision"@@ -2333,7 +2211,6 @@ def test_companion_roots_skip_only_the_unreadable_sibling(tmp_path, monkeypatch)def test_snapshot_selector_skips_only_the_unreadable_child(tmp_path, monkeypatch):- from pathlib import Pathfrom hub.utils.gguf import select_gguf_cache_snapshot_for_repo_dirrepo = tmp_path / "models--org--Vision-GGUF"@@ -2378,26 +2255,15 @@ def test_disjoint_companion_roots_preserve_selected_snapshot_ancestor_walk(tmp_pdef test_auto_switch_carries_hf_cache_companion_roots_into_load(tmp_path, monkeypatch):- from pathlib import Path-- repo = tmp_path / "models--org--Vision-GGUF"- old = repo / "snapshots" / "weights-revision"- old.mkdir(parents = True)- (old / "vision-model-Q4_K_M.gguf").write_bytes(b"GGUF weights")- newer = repo / "snapshots" / "companion-revision"- newer.mkdir(parents = True)+ repo, old, newer = _vision_gguf_cache_repo(tmp_path)(newer / "mmproj-vision-model-F16.gguf").write_bytes(b"GGUF companion")os.utime(old, (1_000, 1_000))os.utime(newer, (2_000, 2_000))- backend = _FakeBackend("org/Other-GGUF", "Q4_K_M")- recorder = _LoadRecorder(backend)- _wire(+ backend, recorder = _wired(monkeypatch,- enabled = True,- resolves_to = (str(old), "Q4_K_M", "org/Vision-GGUF"),- backend = backend,- recorder = recorder,+ _FakeBackend("org/Other-GGUF", "Q4_K_M"),+ (str(old), "Q4_K_M", "org/Vision-GGUF"),)asyncio.run(@@ -2422,24 +2288,13 @@ def test_auto_switch_carries_hf_cache_companion_roots_into_load(tmp_path, monkeydef test_auto_switch_display_alias_keeps_repo_level_companion_scope(tmp_path, monkeypatch):- from pathlib import Path-- repo = tmp_path / "models--org--Vision-GGUF"- old = repo / "snapshots" / "weights-revision"- old.mkdir(parents = True)- (old / "vision-model-Q4_K_M.gguf").write_bytes(b"GGUF weights")- newer = repo / "snapshots" / "companion-revision"- newer.mkdir(parents = True)+ repo, old, newer = _vision_gguf_cache_repo(tmp_path)(newer / "mmproj-vision-model-F16.gguf").write_bytes(b"GGUF companion")- backend = _FakeBackend("org/Other-GGUF", "Q4_K_M")- recorder = _LoadRecorder(backend)- _wire(+ backend, recorder = _wired(monkeypatch,- enabled = True,- resolves_to = (str(old), "Q4_K_M", "org/Vision-GGUF", True),- backend = backend,- recorder = recorder,+ _FakeBackend("org/Other-GGUF", "Q4_K_M"),+ (str(old), "Q4_K_M", "org/Vision-GGUF", True),)asyncio.run(@@ -2458,28 +2313,14 @@ def test_auto_switch_display_alias_keeps_repo_level_companion_scope(tmp_path, modef test_repo_level_request_reloads_resident_snapshot_without_companion_roots(tmp_path, monkeypatch, advertised):- from pathlib import Path-- repo = tmp_path / "models--org--Vision-GGUF"- old = repo / "snapshots" / "weights-revision"- old.mkdir(parents = True)- (old / "vision-model-Q4_K_M.gguf").write_bytes(b"GGUF weights")- newer = repo / "snapshots" / "companion-revision"- newer.mkdir(parents = True)+ repo, old, newer = _vision_gguf_cache_repo(tmp_path)(newer / "mmproj-vision-model-F16.gguf").write_bytes(b"GGUF companion")backend = _FakeBackend(str(old), "Q4_K_M")if advertised:backend._openai_advertised_id = "org/Vision-GGUF"backend._openai_gguf_companion_roots = (str(old),)- recorder = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = (str(old), "Q4_K_M", "org/Vision-GGUF", True),- backend = backend,- recorder = recorder,- )+ _, recorder = _wired(monkeypatch, backend, (str(old), "Q4_K_M", "org/Vision-GGUF", True))asyncio.run(inference_route._maybe_auto_switch_model(@@ -2506,14 +2347,10 @@ def test_resident_repo_reloads_when_companion_finishes_in_existing_snapshot(if precreated:(companion / "mmproj-vision-model-F16.gguf").write_bytes(b"")(selected / "vision-model-Q4_K_M.gguf").write_bytes(b"GGUF weights")- backend = _FakeBackend("org/Other-GGUF", "Q4_K_M")- recorder = _LoadRecorder(backend)- _wire(+ backend, recorder = _wired(monkeypatch,- enabled = True,- resolves_to = (str(selected), "Q4_K_M", "org/Vision-GGUF", True),- backend = backend,- recorder = recorder,+ _FakeBackend("org/Other-GGUF", "Q4_K_M"),+ (str(selected), "Q4_K_M", "org/Vision-GGUF", True),)async def run():@@ -2529,24 +2366,13 @@ def test_resident_repo_reloads_when_companion_finishes_in_existing_snapshot(def test_auto_switch_exact_revision_does_not_widen_companion_roots(tmp_path, monkeypatch):- from pathlib import Path-- repo = tmp_path / "models--org--Vision-GGUF"- old = repo / "snapshots" / "weights-revision"- old.mkdir(parents = True)- (old / "vision-model-Q4_K_M.gguf").write_bytes(b"GGUF weights")- newer = repo / "snapshots" / "companion-revision"- newer.mkdir(parents = True)+ repo, old, newer = _vision_gguf_cache_repo(tmp_path)(newer / "mmproj-other-model-F16.gguf").write_bytes(b"GGUF companion")- backend = _FakeBackend("org/Other-GGUF", "Q4_K_M")- recorder = _LoadRecorder(backend)- _wire(+ backend, recorder = _wired(monkeypatch,- enabled = True,- resolves_to = (str(old), "Q4_K_M", "org/Vision-GGUF"),- backend = backend,- recorder = recorder,+ _FakeBackend("org/Other-GGUF", "Q4_K_M"),+ (str(old), "Q4_K_M", "org/Vision-GGUF"),)asyncio.run(@@ -2567,15 +2393,7 @@ def test_auto_switch_exact_revision_does_not_widen_companion_roots(tmp_path, mondef test_idle_stash_reload_carries_hf_cache_companion_roots(tmp_path, monkeypatch):- from pathlib import Path- from core.inference import llama_keepwarm as kw-- repo = tmp_path / "models--org--Vision-GGUF"- old = repo / "snapshots" / "weights-revision"- old.mkdir(parents = True)- (old / "vision-model-Q4_K_M.gguf").write_bytes(b"GGUF weights")- newer = repo / "snapshots" / "companion-revision"- newer.mkdir(parents = True)+ repo, old, newer = _vision_gguf_cache_repo(tmp_path)(newer / "mmproj-vision-model-F16.gguf").write_bytes(b"GGUF companion")os.utime(old, (1_000, 1_000))os.utime(newer, (2_000, 2_000))@@ -2604,8 +2422,6 @@ def test_idle_stash_reload_carries_hf_cache_companion_roots(tmp_path, monkeypatcdef test_loaded_identity_stashes_only_roots_used_by_the_load():- from core.inference import llama_keepwarm as kw-backend = _FakeBackend("cache/snapshots/weights-revision", "Q4_K_M")backend._openai_advertised_id = "org/Vision-GGUF"backend._openai_gguf_companion_roots = ("weights-revision", "companion-revision")@@ -2625,14 +2441,7 @@ def test_loaded_identity_stashes_only_roots_used_by_the_load():def test_idle_stash_reload_of_manual_snapshot_does_not_add_sibling_roots(tmp_path, monkeypatch):- from core.inference import llama_keepwarm as kw-- repo = tmp_path / "models--org--Vision-GGUF"- old = repo / "snapshots" / "weights-revision"- old.mkdir(parents = True)- (old / "vision-model-Q4_K_M.gguf").write_bytes(b"GGUF weights")- newer = repo / "snapshots" / "companion-revision"- newer.mkdir(parents = True)+ repo, old, newer = _vision_gguf_cache_repo(tmp_path)(newer / "mmproj-other-model-F16.gguf").write_bytes(b"GGUF companion")backend = _FakeBackend(None)@@ -2658,20 +2467,14 @@ def test_idle_stash_reload_of_manual_snapshot_does_not_add_sibling_roots(tmp_patdef test_companion_root_scan_does_not_block_the_event_loop(tmp_path, monkeypatch):- import time-old = tmp_path / "models--org--Vision-GGUF" / "snapshots" / "weights-revision"old.mkdir(parents = True)(old / "vision-model-Q4_K_M.gguf").write_bytes(b"GGUF weights")- backend = _FakeBackend("org/Other-GGUF", "Q4_K_M")- recorder = _LoadRecorder(backend)- _wire(+ backend, recorder = _wired(monkeypatch,- enabled = True,- resolves_to = (str(old), "Q4_K_M", "org/Vision-GGUF"),- backend = backend,- recorder = recorder,+ _FakeBackend("org/Other-GGUF", "Q4_K_M"),+ (str(old), "Q4_K_M", "org/Vision-GGUF"),)entered = threading.Event()release = threading.Event()@@ -2705,10 +2508,6 @@ def test_companion_root_scan_does_not_block_the_event_loop(tmp_path, monkeypatchdef test_inactive_hf_cache_entry_skips_newer_companion_only_snapshot(tmp_path):"""Inactive cache rows point at a snapshot but still select complete weights."""- import os- from pathlib import Path- import routes.models as models_route-repo = tmp_path / "models--org--Repo"old = repo / "snapshots" / "weights-revision"old.mkdir(parents = True)@@ -2736,10 +2535,6 @@ def test_inactive_hf_cache_entry_skips_newer_companion_only_snapshot(tmp_path):def test_hf_cache_entry_stays_within_the_scanned_case_variant(tmp_path):"""A cache row must load from its exact repo directory, not a case-folded peer."""- import os- from pathlib import Path- from types import SimpleNamespace-scanned_repo = tmp_path / "models--Org--Repo"scanned_snapshot = scanned_repo / "snapshots" / "scanned-revision"scanned_snapshot.mkdir(parents = True)@@ -2764,8 +2559,6 @@ def test_hf_cache_entry_stays_within_the_scanned_case_variant(tmp_path):def test_hf_cache_entry_excludes_torn_quant_from_selected_snapshot(tmp_path):"""Only complete quants from the selected snapshot may be advertised as loadable."""- from types import SimpleNamespace-repo = tmp_path / "models--org--Repo"snapshot = repo / "snapshots" / "revision"snapshot.mkdir(parents = True)@@ -2783,8 +2576,6 @@ def test_hf_cache_entry_excludes_torn_quant_from_selected_snapshot(tmp_path):def test_hf_cache_entry_keeps_partial_only_fallback(tmp_path):"""A repo with no complete quant keeps the pre-existing fallback listing."""- from types import SimpleNamespace-repo = tmp_path / "models--org--Repo"snapshot = repo / "snapshots" / "revision"snapshot.mkdir(parents = True)@@ -2801,8 +2592,6 @@ def test_hf_cache_entry_keeps_partial_only_fallback(tmp_path):def test_selected_snapshot_preserves_local_variant_labels(tmp_path):"""Selected snapshots use picker identities and still accept legacy API pins."""- from types import SimpleNamespace-repo = tmp_path / "models--org--Repo"snapshot = repo / "snapshots" / "revision"snapshot.mkdir(parents = True)@@ -2824,8 +2613,6 @@ def test_selected_snapshot_preserves_local_variant_labels(tmp_path):def test_model_dir_with_snapshots_subdir_keeps_root_gguf(tmp_path):"""A regular model dir is not an HF cache just because snapshots/ exists."""- from types import SimpleNamespace-nested = tmp_path / "snapshots" / "revision"nested.mkdir(parents = True)(nested / "mmproj-model-F16.gguf").write_bytes(b"GGUF companion")@@ -2843,9 +2630,6 @@ def test_model_dir_with_snapshots_subdir_keeps_root_gguf(tmp_path):def test_hf_cache_entries_do_not_rescan_the_cache_root(monkeypatch, tmp_path):"""Each scanned row already owns an exact repo directory under the cache root."""- from pathlib import Path- from types import SimpleNamespace-infos = []for index in range(3):repo = tmp_path / f"models--org--Repo-{index}"@@ -2927,16 +2711,11 @@ def test_already_loaded_by_repo_id_is_not_reswapped(monkeypatch):# A model loaded normally has model_identifier == repo id, but the resolver# returns the concrete load path. A request for that repo must count as already# serving (no reload, no 409) even with another inference active.- from core.inference import llama_keepwarm as kw- backend = _FakeBackend("org/Repo-GGUF", hf_variant = "Q4_K_M")- rec = _LoadRecorder(backend)- _wire(+ backend, rec = _wired(monkeypatch,- enabled = True,- resolves_to = ("/cache/models--org--Repo-GGUF/snapshots/abc", "Q4_K_M", "org/Repo-GGUF"),- backend = backend,- recorder = rec,+ _FakeBackend("org/Repo-GGUF", hf_variant = "Q4_K_M"),+ ("/cache/models--org--Repo-GGUF/snapshots/abc", "Q4_K_M", "org/Repo-GGUF"),)monkeypatch.setattr(kw, "_inflight", 2)monkeypatch.setattr(kw, "_pending", 0)@@ -2948,14 +2727,8 @@ def test_already_loaded_by_repo_id_is_not_reswapped(monkeypatch):def test_auto_switch_advertises_repo_id_after_load(monkeypatch):# After a load-by-path, the backend advertises the repo id (override key), not# the concrete path, so /v1/models and the idle stash stay name-based.- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/p/B-snapshot", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend("org/A-GGUF"), ("/p/B-snapshot", "Q8_0", "org/B-GGUF"))_run_hook("org/B-GGUF:Q8_0")assert rec.calls[0].model_path == "/p/B-snapshot" # loaded by concrete path@@ -2970,9 +2743,8 @@ def test_already_serving_by_path_records_advertised_alias(monkeypatch):path = "/cache/models--org--Repo-GGUF/snapshots/abc"backend = _FakeBackend(path, hf_variant = "Q4_K_M") # loaded by path, no advertised idrec = _LoadRecorder(backend)- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = (path, "Q4_K_M", "org/Repo-GGUF"),backend = backend,recorder = rec,@@ -2991,9 +2763,8 @@ def test_already_serving_requested_by_path_records_advertised_alias(monkeypatch)path = "/models/lmstudio/TheBloke/weights-file-01.gguf"backend = _FakeBackend(path) # loaded by path, no advertised idrec = _LoadRecorder(backend)- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = (path, None, "Qwen3-4B-Instruct-GGUF"),backend = backend,recorder = rec,@@ -3016,7 +2787,6 @@ def test_streaming_responses_uses_advertised_id_helper():# the snapshot path while the repo id lives in _openai_advertised_id, so the raw# form would stream a snapshot basename while /v1/models, chat, and non-streaming# responses report the repo id.- import inspectsrc = inspect.getsource(inference_route._responses_stream)assert "_clean_model = _llama_public_model_id(llama_backend" in src@@ -3027,17 +2797,8 @@ def test_concurrent_same_target_requests_load_once(monkeypatch):# Two concurrent requests for the same unloaded model must load once, not each# 409 the other. Simulate the second request already waiting (registered) while# the first runs the hook with _inflight counting both.- from core.inference import llama_keepwarm as kw- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/p/B", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,- )+ backend, rec = _wired(monkeypatch, _FakeBackend("org/A-GGUF"), ("/p/B", "Q8_0", "org/B-GGUF"))monkeypatch.setattr(kw, "_inflight", 2) # both same-target requests countedmonkeypatch.setattr(kw, "_pending", 0)inference_route._note_switch_waiter(inference_route._switch_key("org/B-GGUF", "Q8_0"), 1)@@ -3048,17 +2809,8 @@ def test_concurrent_same_target_requests_load_once(monkeypatch):def test_queued_different_target_does_not_deadlock_current_swap(monkeypatch):# A concurrent request already queued for another target is not generating,# so it must not prevent the current serialized swap from proceeding.- from core.inference import llama_keepwarm as kw- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/p/B", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,- )+ backend, rec = _wired(monkeypatch, _FakeBackend("org/A-GGUF"), ("/p/B", "Q8_0", "org/B-GGUF"))monkeypatch.setattr(kw, "_inflight", 2)monkeypatch.setattr(kw, "_pending", 0)inference_route._note_switch_waiter(inference_route._switch_key("org/C-GGUF", "Q4_K_M"), 1)@@ -3068,7 +2820,6 @@ def test_queued_different_target_does_not_deadlock_current_swap(monkeypatch):def test_v1_models_advertises_repo_id_not_load_path(monkeypatch):# /v1/models must report the advertised repo id, never the host load path.- from types import SimpleNamespacellama = _FakeBackend("/cache/models--org--Repo/snapshots/abc")llama._openai_advertised_id = "org/Repo-GGUF"@@ -3084,7 +2835,6 @@ def test_idle_alias_reload_preserves_override_via_advertised_id(monkeypatch):# The idle stash carries (load_path, quant, advertised_id). An alias reload must# look up the override by the advertised repo id, not the concrete load path,# so the user's saved launch flags survive the unload/reload.- from core.inference import llama_keepwarm as kwbackend = _FakeBackend(None) # idle-unload emptied the slotrec = _LoadRecorder(backend)@@ -3103,7 +2853,6 @@ def test_load_route_holds_lifecycle_gate(monkeypatch):# Lock the manual /load gate against silent revert: the route must wrap the# load in inference_lifecycle_gate so idle-unload can't fire mid-load.# Asserted on the gated coroutine: the route only adds the padded response.- import inspectsrc = inspect.getsource(inference_route.load_model_gated)assert "inference_lifecycle_gate" in src@@ -3114,7 +2863,6 @@ def test_model_replacements_recheck_sidecar_swap_before_either_backend_is_unload# Both replacement directions drain, then recheck whether a sidecar install reserved the# gate meanwhile. That recheck is the last thing that can reject the load, so the# destructive cancel must follow it. Exact-model reuse exits earlier and never waits.- import inspectsrc = inspect.getsource(inference_route._load_model_impl)already_loaded = src.index('status = "already_loaded"')@@ -3141,7 +2889,6 @@ def test_switch_waiter_deregisters_before_swap_gate_release():# A waiter left registered after the swap gate is released would let a swap on# another event loop count the finished request as still queued, pass the drain# early, and unload the model that request is about to generate against.- import inspectsrc = inspect.getsource(inference_route._maybe_auto_switch_model)deregister = src.index("_note_switch_waiter(key, -1)")@@ -3161,7 +2908,6 @@ def _anthropic_payload(max_tokens = None):def test_anthropic_503_when_unloaded_and_auto_switch_off(monkeypatch):# Default-off parity: unloaded backend + auto-switch off 503s before the# max_tokens 400, exactly as the pre-feature endpoint did.- from fastapi import HTTPExceptionbackend = _FakeBackend(None)monkeypatch.setattr(inference_route, "get_llama_cpp_backend", lambda: backend)@@ -3174,7 +2920,6 @@ def test_anthropic_503_when_unloaded_and_auto_switch_off(monkeypatch):def test_anthropic_400_when_auto_switch_on_and_max_tokens_missing(monkeypatch):# With auto-switch on, request-shape validation runs first: a missing# max_tokens still 400s before any load is attempted.- from fastapi import HTTPExceptionbackend = _FakeBackend(None)monkeypatch.setattr(inference_route, "get_llama_cpp_backend", lambda: backend)@@ -3190,17 +2935,8 @@ def test_anthropic_400_when_auto_switch_on_and_max_tokens_missing(monkeypatch):def test_pending_same_target_request_does_not_block_swap(monkeypatch):# A second same-target request blocked in the middleware (pending, not yet# generating) must not block the first request: pending is excluded.- from core.inference import llama_keepwarm as kw- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/p/B", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,- )+ backend, rec = _wired(monkeypatch, _FakeBackend("org/A-GGUF"), ("/p/B", "Q8_0", "org/B-GGUF"))monkeypatch.setattr(kw, "_inflight", 1) # just the callermonkeypatch.setattr(kw, "_pending", 1) # second request blocked in middleware_run_hook("org/B-GGUF:Q8_0")@@ -3211,17 +2947,8 @@ def test_swap_waits_until_concurrent_request_finishes_resolving(monkeypatch):# The real middleware counts a concurrent same-model request as in-flight# before it resolves and registers a target waiter. Treat it as active until# its target is known, then recognize it as another queued switch request.- from core.inference import llama_keepwarm as kw- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/p/B", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,- )+ backend, rec = _wired(monkeypatch, _FakeBackend("org/A-GGUF"), ("/p/B", "Q8_0", "org/B-GGUF"))monkeypatch.setattr(kw, "_inflight", 2) # caller + a still-resolving twinmonkeypatch.setattr(kw, "_pending", 0)# The twin is still resolving, so it is counted in-flight but has not joined@@ -3241,8 +2968,6 @@ def test_swap_waits_until_concurrent_request_finishes_resolving(monkeypatch):def test_external_untrack_decrements_inflight_and_is_idempotent():- from core.inference import llama_keepwarm as kw-kw._inflight = 2scope = {"type": "http"}kw.untrack_current_request(scope)@@ -3256,7 +2981,6 @@ def test_external_untrack_decrements_inflight_and_is_idempotent():def test_manual_unload_interrupts_even_while_inference_active(monkeypatch):# A manual /unload is a deliberate action: it tears down immediately even with# a request in flight (only the automatic idle loop defers). No 409.- from core.inference import llama_keepwarm as kwfrom models.inference import UnloadRequestbackend = _FakeBackend("org/A-GGUF")@@ -3276,13 +3000,11 @@ def test_manual_unload_interrupts_even_while_inference_active(monkeypatch):def test_auto_switch_waits_when_unsloth_stream_active(monkeypatch):# The GGUF slot is empty but an Unsloth model is streaming (counted in-flight).# The replacement waits for it just as it does for a GGUF generation.- from core.inference import llama_keepwarm as kwbackend = _FakeBackend(None) # no GGUF loadedrec = _LoadRecorder(backend)- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = ("/p/B", "Q8_0", "org/B-GGUF"),backend = backend,recorder = rec,@@ -3323,7 +3045,6 @@ def test_public_model_id_prefers_advertised_over_path():def test_chat_validates_non_system_message_before_auto_switch():# A system-only chat must be rejected before the hook so an invalid request# never swaps the resident model. Asserted on source order.- import inspectsrc = inspect.getsource(inference_route.produce_openai_chat_completions)assert src.index("At least one non-system message is required.") < src.index("_maybe_auto_switch_model"@@ -3333,7 +3054,6 @@ def test_chat_validates_non_system_message_before_auto_switch():def test_chat_untracks_external_provider_before_proxy():# The external-provider branch must untrack the request before proxying so its# stream can't block a concurrent local auto-switch.- import inspectsrc = inspect.getsource(inference_route.produce_openai_chat_completions)assert src.index("untrack_current_request") < src.index("_proxy_to_external_provider")@@ -3361,8 +3081,6 @@ def _training_request():def test_api_training_refused_while_inference_active(monkeypatch):# API-key caller: training is refused with 409 while a request streams, so it# can't free VRAM by unloading the chat model out from under the stream.- from fastapi import HTTPException- from core.inference import llama_keepwarm as kwimport routes.training as training_routemonkeypatch.setattr(kw, "_inflight", 1)@@ -3379,8 +3097,6 @@ def test_api_training_refused_while_inference_active(monkeypatch):def test_ui_training_not_blocked_by_active_inference(monkeypatch):# UI (session auth) caller: the API guard is skipped, so training proceeds past# it even with inference active (here it hits the normal already-active path).- from types import SimpleNamespace- from core.inference import llama_keepwarm as kwimport routes.training as training_routemonkeypatch.setattr(kw, "_inflight", 1)@@ -3434,7 +3150,6 @@ def test_env_idle_standalone_reloads_freed_model_with_auto_switch_off(monkeypatc# C3: a standalone UNSLOTH_MODEL_IDLE_TTL (auto-switch OFF) freed the model on# idle; the next request must restore exactly what was freed even though the# resolver never runs while auto-switch is off.- from core.inference import llama_keepwarm as kwbackend = _FakeBackend(None) # idle-unload emptied the slotrec = _LoadRecorder(backend)@@ -3463,11 +3178,8 @@ def test_env_idle_standalone_reloads_freed_model_with_auto_switch_off(monkeypatcdef test_no_stash_reload_when_idle_off_and_auto_switch_off(monkeypatch):# C3 guard: with both auto-switch and idle-unload off the hook is a pure no-op# and must not resurrect a stashed model (that path only serves the idle feature).- from core.inference import llama_keepwarm as kw- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(monkeypatch, enabled = False, resolves_to = None, backend = backend, recorder = rec)+ backend, rec = _wired(monkeypatch, _FakeBackend(None), None, enabled = False)monkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: 0)monkeypatch.setattr(settings, "idle_unload_is_configured", lambda: 0 > 0)monkeypatch.setattr(kw, "_inflight", 0)@@ -3480,8 +3192,6 @@ def test_stash_reload_skipped_while_unsloth_model_active(monkeypatch):# An Unsloth/Transformers model loaded after an idle-unload leaves the GGUF slot# empty but is the live model; an unknown /v1 name must NOT resurrect the stale# GGUF stash (that reload would tear the active Unsloth model down).- from types import SimpleNamespace- from core.inference import llama_keepwarm as kwbackend = _FakeBackend(None) # GGUF slot emptyrec = _LoadRecorder(backend)@@ -3506,7 +3216,6 @@ def test_is_abs_path_id_distinguishes_path_from_repo_id():def test_advertised_loader_id_prefers_alias_over_abs_path():# C1: the ./models and LM Studio scanners report the on-disk path as info.id.- from types import SimpleNamespacef = resolver._advertised_loader_id# An absolute-path id falls back to the first non-path alias.@@ -3532,10 +3241,6 @@ def test_advertised_loader_id_prefers_alias_over_abs_path():def test_index_advertises_alias_not_filesystem_path(tmp_path, monkeypatch):# C1 end-to-end: a scanner that reports the path as the id must not advertise the# host path in /v1/models, yet the model stays resolvable by that path too.- from types import SimpleNamespace- import routes.models as models_route- from storage import studio_db- import utils.paths as pathsgguf = tmp_path / "model-Q4_K_M.gguf"gguf.write_bytes(b"x" * 32)@@ -3564,13 +3269,6 @@ def test_index_advertises_alias_not_filesystem_path(tmp_path, monkeypatch):def test_inactive_cache_revision_aliases_keep_exact_companion_scope(tmp_path, monkeypatch):- from types import SimpleNamespace-- import routes.models as models_route- from storage import studio_db- from utils import hf_cache_settings- import utils.paths as paths-repo = tmp_path / "models--org--Vision-GGUF"selected = repo / "snapshots" / "weights-revision"selected.mkdir(parents = True)@@ -3620,9 +3318,6 @@ def test_inactive_cache_revision_aliases_keep_exact_companion_scope(tmp_path, modef test_build_index_survives_a_failing_scanner(tmp_path, monkeypatch):# gemini: one bad scanner (e.g. a permission error on ./models) must drop only# that source, not abort the whole index and lose what the others found.- from types import SimpleNamespace- import routes.models as models_route- import utils.paths as pathsdef _boom(*a, **k):raise OSError("permission denied")@@ -3650,11 +3345,6 @@ def test_build_index_survives_a_failing_scanner(tmp_path, monkeypatch):def test_build_index_groups_overlapping_custom_gguf_roots(tmp_path, monkeypatch):- import routes.models as models_route- from storage import studio_db- from utils import hf_cache_settings- import utils.paths as paths-root = tmp_path / "custom"model = root / "publisher"model.mkdir(parents = True)@@ -3696,13 +3386,6 @@ def test_build_index_groups_overlapping_custom_gguf_roots(tmp_path, monkeypatch)def test_non_gguf_entries_skip_gguf_sibling_revision_scans(tmp_path, monkeypatch):- from types import SimpleNamespace-- import routes.models as models_route- from storage import studio_db- from utils import hf_cache_settings- import utils.paths as paths-snapshot = tmp_path / "models--org--Repo" / "snapshots" / "rev-new"info = SimpleNamespace(id = str(snapshot),@@ -3742,7 +3425,6 @@ def test_local_servable_model_reads_files_not_model_format(tmp_path):# Codex: HF-cache GGUF snapshots leave model_format unset, so /v1/models must# decide the format from the on-disk files. A checkpoint dir needs a root# config.json to prove it is a model and not a bare adapter.- from types import SimpleNamespacegguf = tmp_path / "model-Q4_K_M.gguf"gguf.write_bytes(b"x" * 32)@@ -3767,7 +3449,6 @@ def test_local_servable_model_reads_files_not_model_format(tmp_path):def test_local_servable_model_excludes_ollama_links(tmp_path):# Codex P2: Ollama entries come from a scanner _build_index skips, so their# advertised ids never resolve; the catalog must not report them as servable.- from types import SimpleNamespacelinks = tmp_path / ".studio_links"links.mkdir()@@ -3801,13 +3482,11 @@ def test_embeddings_input_present_helper():def test_embeddings_rejects_missing_input_before_switch(monkeypatch):# C2: with auto-switch on, an embeddings request carrying no input must 400# before the hook, so an invalid request never swaps the resident model.- from fastapi import HTTPExceptionbackend = _FakeBackend("org/A-GGUF") # loadedrec = _LoadRecorder(backend)- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = ("/p/B", "Q8_0", "org/B-GGUF"),backend = backend,recorder = rec,@@ -3823,7 +3502,6 @@ def test_embeddings_rejects_missing_input_before_switch(monkeypatch):def test_retrieve_model_tolerates_non_string_id(monkeypatch):# G2: a model object with a non-string id (defensive) must be skipped rather# than crashing the .lower() compare; a valid id is still found, unknown 404s.- from fastapi import HTTPExceptionasync def _objs():return [{"id": 123, "object": "model"}, {"id": "org/B-GGUF", "object": "model"}]@@ -3842,7 +3520,6 @@ def test_retrieve_model_resolves_raw_path_to_advertised_id(monkeypatch):# a loaded auto-switch model. Its /v1/models entry is keyed by the advertised# repo id (identifier = snapshot path), so the raw-path fallback must map the raw# id to that advertised id, not public_model_id(path), or a loaded model 404s.- from types import SimpleNamespaceraw_path = "/cache/models--org--B-GGUF/snapshots/abc/model.gguf"llama = SimpleNamespace(@@ -3869,17 +3546,8 @@ def test_chat_streaming_n_gt_1_rejected_before_switch(monkeypatch):# Codex P2: only the non-streaming GGUF path returns multiple choices, so# stream=true + n>1 is invalid on every local serving path. Both fields are# known pre-switch, so it must 400 before the switch rather than loading model B.- from fastapi import HTTPException- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/p/B", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,- )+ backend, rec = _wired(monkeypatch, _FakeBackend("org/A-GGUF"), ("/p/B", "Q8_0", "org/B-GGUF"))payload = _chat_request(model = "org/B-GGUF", stream = True, n = 2)with pytest.raises(HTTPException) as exc:asyncio.run(inference_route.openai_chat_completions(payload, object(), "tester"))@@ -3949,8 +3617,6 @@ def test_keepwarm_does_not_stamp_activity_on_401(monkeypatch):def _stash(monkeypatch, *, idle = 600):"""Common setup for the standalone-idle reload paths: feature off, idle TTL on,an idle-freed model in the stash, nothing loaded, no in-flight requests."""- from core.inference import llama_keepwarm as kw-monkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: idle)monkeypatch.setattr(settings, "idle_unload_is_configured", lambda: idle > 0)monkeypatch.setattr(kw, "_inflight", 0)@@ -3969,17 +3635,8 @@ def test_completions_prompt_present_helper():def test_completions_rejects_missing_prompt_before_switch(monkeypatch):# #1: /v1/completions had no prompt pre-check, so a malformed request naming a# different downloaded GGUF loaded it before failing. Now it 400s first.- from fastapi import HTTPException- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/p/B", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,- )+ backend, rec = _wired(monkeypatch, _FakeBackend("org/A-GGUF"), ("/p/B", "Q8_0", "org/B-GGUF"))with pytest.raises(HTTPException) as exc:asyncio.run(inference_route.openai_completions(@@ -3993,12 +3650,8 @@ def test_completions_rejects_missing_prompt_before_switch(monkeypatch):def test_chat_system_only_rejected_before_idle_reload(monkeypatch):# #4: the chat pre-load guard only checked auto-switch; a standalone idle TTL# could still reload a system-only chat before the 400. Now it 400s first.- from fastapi import HTTPException- from models.inference import ChatCompletionRequest- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(monkeypatch, enabled = False, resolves_to = None, backend = backend, recorder = rec)+ backend, rec = _wired(monkeypatch, _FakeBackend(None), None, enabled = False)_stash(monkeypatch)payload = ChatCompletionRequest(model = "x", messages = [{"role": "system", "content": "sys"}])with pytest.raises(HTTPException) as exc:@@ -4010,11 +3663,8 @@ def test_chat_system_only_rejected_before_idle_reload(monkeypatch):def test_embeddings_missing_input_rejected_before_idle_reload(monkeypatch):# #5: same gap on /v1/embeddings; the missing-input 400 must fire under a# standalone idle TTL too, not only when auto-switch is on.- from fastapi import HTTPException- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(monkeypatch, enabled = False, resolves_to = None, backend = backend, recorder = rec)+ backend, rec = _wired(monkeypatch, _FakeBackend(None), None, enabled = False)_stash(monkeypatch)with pytest.raises(HTTPException) as exc:asyncio.run(inference_route.openai_embeddings(_json_body_request({"model": "x"}), "tester"))@@ -4026,9 +3676,7 @@ def test_messages_does_not_503_before_reload_hook_when_idle_on(monkeypatch):# #3: /v1/messages 503'd before the reload hook when auto-switch was off, so a# standalone idle TTL could never restore the freed model. The early 503 now# defers to any automatic-load trigger, so the reload hook runs.- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(monkeypatch, enabled = False, resolves_to = None, backend = backend, recorder = rec)+ backend, rec = _wired(monkeypatch, _FakeBackend(None), None, enabled = False)_stash(monkeypatch)# The handler proceeds past the hook to real generation (no llama-server here),# so tolerate the downstream failure; the reload having run is the assertion.@@ -4046,7 +3694,6 @@ def test_messages_does_not_503_before_reload_hook_when_idle_on(monkeypatch):def test_messages_503_gated_on_automatic_load_predicate():# Lock the #3 fix at the source: the early 503 must check the shared predicate.- import inspectsrc = inspect.getsource(inference_route.anthropic_messages)assert "_automatic_model_load_may_run" in src@@ -4055,9 +3702,7 @@ def test_raw_body_without_model_reloads_freed_model(monkeypatch):# #6: a raw completions/embeddings body that omits `model` passed None, which# skipped the idle-stash reload and 503'd. A non-empty sentinel now lets the# reload run while still resolving as unknown.- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(monkeypatch, enabled = False, resolves_to = None, backend = backend, recorder = rec)+ backend, rec = _wired(monkeypatch, _FakeBackend(None), None, enabled = False)_stash(monkeypatch)body = asyncio.run(inference_route._auto_switch_from_request_body(@@ -4073,11 +3718,8 @@ def test_raw_body_without_model_reloads_freed_model(monkeypatch):def test_audio_generate_reloads_idle_freed_model(monkeypatch):# #2: /audio/generate is keep-warm-tracked but had no reload hook, so an# idle-freed audio GGUF stayed unloaded. The hook now restores it.- from models.inference import ChatCompletionRequest- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(monkeypatch, enabled = False, resolves_to = None, backend = backend, recorder = rec)+ backend, rec = _wired(monkeypatch, _FakeBackend(None), None, enabled = False)_stash(monkeypatch)payload = ChatCompletionRequest(model = "x", messages = [{"role": "user", "content": "say hi"}])# Falls through to the non-audio backend path (no real model) after the reload;@@ -4093,12 +3735,8 @@ def test_audio_generate_reloads_idle_freed_model(monkeypatch):def test_audio_generate_does_not_reload_on_invalid_request(monkeypatch):# The audio reload hook must run after message validation, so an empty request# never triggers a reload.- from fastapi import HTTPException- from models.inference import ChatCompletionRequest- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(monkeypatch, enabled = False, resolves_to = None, backend = backend, recorder = rec)+ backend, rec = _wired(monkeypatch, _FakeBackend(None), None, enabled = False)_stash(monkeypatch)payload = ChatCompletionRequest(model = "x", messages = [])with pytest.raises(HTTPException) as exc:@@ -4111,15 +3749,7 @@ def test_preview_scope_disables_auto_switch(monkeypatch):# #7: the public preview route delegates to the chat handler; a caller-supplied# model must not switch away from the pinned checkpoint. The scope opt-out flag# makes the hook a no-op.- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/p/B", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,- )+ backend, rec = _wired(monkeypatch, _FakeBackend("org/A-GGUF"), ("/p/B", "Q8_0", "org/B-GGUF"))class _Req:def __init__(self):@@ -4139,7 +3769,6 @@ def test_preview_scope_disables_auto_switch(monkeypatch):def test_preview_chat_is_tracked_as_inference_path():# #8: long preview streams use the same backend; the keep-warm middleware must# count them so the idle loop can't unload mid-response.- from core.inference.llama_keepwarm import _is_inference_pathassert _is_inference_path("/p/my-run/v1/chat/completions") is Trueassert _is_inference_path("/p/my-run/ckpt-100/v1/chat/completions") is True@@ -4149,8 +3778,6 @@ def test_preview_chat_is_tracked_as_inference_path():def test_untrack_does_not_reset_idle_timer():# #9: external-provider traffic was keeping the local GGUF warm forever because# untrack stamped _last_active. It must decrement in-flight without restamping.- import time- from core.inference import llama_keepwarm as kwkw._inflight = 1kw._last_active = time.monotonic() - 3600@@ -4165,8 +3792,6 @@ def test_untrack_does_not_reset_idle_timer():def test_note_start_does_not_reset_idle_timer():# The start stamp was removed so an external request that is later untracked# cannot reset the timer at start either; in-flight count still protects it.- import time- from core.inference import llama_keepwarm as kwkw._inflight = 0kw._pending = 0@@ -4190,9 +3815,8 @@ def test_omitted_model_does_not_resolve_to_a_named_gguf(monkeypatch):# resolver here would switch to B if it ran; it must not.backend = _FakeBackend("org/A-GGUF") # a model is already loadedrec = _LoadRecorder(backend)- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = ("/p/B", "Q8_0", "org/B-GGUF"),backend = backend,recorder = rec,@@ -4209,7 +3833,6 @@ def test_omitted_model_does_not_resolve_to_a_named_gguf(monkeypatch):def test_omitted_model_still_reloads_idle_freed_model(monkeypatch):# The reload-only sentinel must still restore an idle-freed model (the round-9# behavior), it just never runs the resolver.- from core.inference import llama_keepwarm as kwbackend = _FakeBackend(None) # idle-unload emptied the slotrec = _LoadRecorder(backend)@@ -4240,17 +3863,8 @@ def _anthropic_payload_with_tools(tools, max_tokens = 16):def test_anthropic_invalid_tool_rejected_before_switch(monkeypatch):# Codex P2: a malformed client tool (no input_schema, no server-tool type) must# 400 before the auto-switch hook, so an invalid request never evicts the model.- from fastapi import HTTPException- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/p/B", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,- )+ backend, rec = _wired(monkeypatch, _FakeBackend("org/A-GGUF"), ("/p/B", "Q8_0", "org/B-GGUF"))payload = _anthropic_payload_with_tools([{"name": "broken"}]) # missing input_schemawith pytest.raises(HTTPException) as exc:asyncio.run(inference_route.anthropic_messages(payload, object(), "tester"))@@ -4261,7 +3875,6 @@ def test_anthropic_invalid_tool_rejected_before_switch(monkeypatch):def test_anthropic_validates_tools_before_auto_switch():# Lock the order at the source: tool-shape validation precedes the hook, for# both /messages and /messages/count_tokens (shared helper).- import inspectfor fn in (inference_route.anthropic_messages, inference_route.anthropic_count_tokens):src = inspect.getsource(fn)assert src.index("_validate_anthropic_client_tools") < src.index("_maybe_auto_switch_model")@@ -4271,17 +3884,8 @@ def test_anthropic_mixed_tools_rejected_before_switch(monkeypatch):# Codex P2: combining an Anthropic server tool (type) with a custom client tool# (input_schema) is unsupported and must 400 before the switch, so the request# can't evict the loaded model only to be rejected after the load.- from fastapi import HTTPException- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/p/B", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,- )+ backend, rec = _wired(monkeypatch, _FakeBackend("org/A-GGUF"), ("/p/B", "Q8_0", "org/B-GGUF"))payload = _anthropic_payload_with_tools([{"type": "web_search_20250305"}, # server tool@@ -4308,8 +3912,6 @@ def _responses_payload(set_model = True,stream = None,):- from models.inference import ResponsesRequest-kwargs = dict(input = "hi")if set_model:kwargs["model"] = "org/B-GGUF"@@ -4323,7 +3925,6 @@ def _responses_payload(def test_switch_model_for_payload_only_switches_when_explicit():# Codex P2: an omitted `model` (pydantic fills "default") must be reload-only;# an explicitly set model -- including a literal "default" -- is honored.- from models.inference import ChatCompletionRequestomitted = ChatCompletionRequest(messages = [_chat_msg()])assert inference_route._switch_model_for_payload(omitted) == inference_route._RELOAD_ONLY_MODEL@@ -4336,16 +3937,9 @@ def test_switch_model_for_payload_only_switches_when_explicit():def test_omitted_schema_model_skips_resolver(monkeypatch):# End to end: a schema request omitting `model` must not run the resolver, so a# GGUF named "default" is never swapped to; an explicit model still switches.- from models.inference import ChatCompletionRequest- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("org/B-GGUF", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend("org/A-GGUF"), ("org/B-GGUF", "Q8_0", "org/B-GGUF"))omitted = ChatCompletionRequest(messages = [_chat_msg()])asyncio.run(@@ -4377,16 +3971,9 @@ def test_build_chat_request_propagates_omitted_model():def test_responses_invalid_function_tool_rejected_before_switch(monkeypatch):# Codex P2: a malformed function tool (no name) must 400 before the hook, so an# invalid /v1/responses request never switches or evicts the loaded model.- from fastapi import HTTPException- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("org/B-GGUF", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend("org/A-GGUF"), ("org/B-GGUF", "Q8_0", "org/B-GGUF"))payload = _responses_payload(tools = [{"type": "function", "parameters": {}}])with pytest.raises(HTTPException) as exc:@@ -4398,12 +3985,6 @@ def test_responses_invalid_function_tool_rejected_before_switch(monkeypatch):def test_responses_valid_and_builtin_tools_pass_validation(monkeypatch):# A well-formed function tool and a built-in (non-function) tool must pass the# pre-switch check. Stub the hook so the test stops right after validation.- class _Reached(Exception):- pass-- async def _boom(*a, **k):- raise _Reached()-monkeypatch.setattr(inference_route, "_maybe_auto_switch_model", _boom)payload = _responses_payload(tools = [{"type": "function", "name": "ok", "parameters": {}}, {"type": "web_search"}]@@ -4414,7 +3995,6 @@ def test_responses_valid_and_builtin_tools_pass_validation(monkeypatch):def test_responses_validates_tools_before_auto_switch():# Lock the order at the source: tool validation precedes the switch hook.- import inspectsrc = inspect.getsource(inference_route.openai_responses)assert src.index("each function tool must have a 'name'") < src.index("_maybe_auto_switch_model"@@ -4425,8 +4005,6 @@ def test_responses_forcing_tool_choice_without_name_rejected_before_switch(monke# Codex P2: a forcing-function tool_choice with no name (Responses shape# {"type": "function"}) must 400 before the switch, so the streaming path can't# forward a bad choice and an invalid request can't evict the model.- from fastapi import HTTPException- from models.inference import ResponsesRequestasync def _boom(*a, **k):raise AssertionError("must not switch on an invalid tool_choice")@@ -4450,7 +4028,6 @@ def test_responses_forcing_tool_choice_without_name_rejected_before_switch(monkedef test_swap_acquires_process_gate_before_load():# Lock in the structure: the process-wide gate is acquired before the load and# always released, so a cross-loop swap can't reach _load_model_impl unguarded.- import inspectsrc = inspect.getsource(inference_route._maybe_auto_switch_model)assert src.index("_acquire_swap_gate") < src.index("_load_model_impl")@@ -4466,23 +4043,23 @@ def _chat_request(**kw):return ChatCompletionRequest(**kw)+def _chat_request_b(+ *args,+ model = "org/B-GGUF",+ **kwargs,+):+ """_chat_request for the org/B-GGUF model these cases switch to."""+ return _chat_request(*args, model = model, **kwargs)++def test_chat_confirm_without_stream_rejected_before_switch(monkeypatch):# Codex P2: confirm_tool_calls=true + stream=false + local tools is an invalid# shape; it must 400 before the switch hook so it can't evict the resident model.- from fastapi import HTTPException- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("org/B-GGUF", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,- )- payload = _chat_request(- model = "org/B-GGUF", enable_tools = True, confirm_tool_calls = True, stream = False+ backend, rec = _wired(+ monkeypatch, _FakeBackend("org/A-GGUF"), ("org/B-GGUF", "Q8_0", "org/B-GGUF"))+ payload = _chat_request_b(enable_tools = True, confirm_tool_calls = True, stream = False)with pytest.raises(HTTPException) as exc:asyncio.run(inference_route.openai_chat_completions(payload, object(), "tester"))assert exc.value.status_code == 400@@ -4492,16 +4069,9 @@ def test_chat_confirm_without_stream_rejected_before_switch(monkeypatch):def test_chat_confirm_with_bypass_permissions_reaches_hook(monkeypatch):# bypass_permissions suppresses the confirm gate, so the pre-check must not fire;# the request should reach the switch hook (stubbed here to a sentinel).- class _Reached(Exception):- pass-- async def _boom(*a, **k):- raise _Reached()-monkeypatch.setattr(settings, "get_openai_auto_switch_enabled", lambda: True)monkeypatch.setattr(inference_route, "_maybe_auto_switch_model", _boom)- payload = _chat_request(- model = "org/B-GGUF",+ payload = _chat_request_b(enable_tools = True,confirm_tool_calls = True,stream = False,@@ -4519,10 +4089,6 @@ def test_chat_audio_input_guards_target_before_switch(monkeypatch):# the projector alone: an audio model's projector carries no vision tower, so# requiring one would refuse the very models that serve the request. A# safetensors or MLX checkpoint declares audio apart, so that flag rides along.- from models.inference import ChatMessage, ImageContentPart, ImageUrl-- class _Reached(Exception):- passcaptured = {}@@ -4570,8 +4136,7 @@ def test_chat_audio_input_guards_target_before_switch(monkeypatch):# An image in the same request does need the vision tower.img = ImageContentPart(type = "image_url", image_url = ImageUrl(url = "data:image/png;base64,AAAA"))- payload = _chat_request(- model = "org/B-GGUF",+ payload = _chat_request_b(audio_base64 = "AAAA",messages = [ChatMessage(role = "user", content = [img])],)@@ -4606,17 +4171,8 @@ def test_completions_rejects_object_prompt_before_switch(monkeypatch):# Codex P2: an object prompt like {"prompt": {}} is a deterministic client error# (only a string or array is valid). It must 400 before the switch so a bad shape# can't load the named GGUF only to be rejected by llama-server after eviction.- from fastapi import HTTPException- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/p/B", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,- )+ backend, rec = _wired(monkeypatch, _FakeBackend("org/A-GGUF"), ("/p/B", "Q8_0", "org/B-GGUF"))with pytest.raises(HTTPException) as exc:asyncio.run(inference_route.openai_completions(@@ -4630,17 +4186,8 @@ def test_completions_rejects_object_prompt_before_switch(monkeypatch):def test_embeddings_rejects_object_input_before_switch(monkeypatch):# Codex P2: an object input like {"input": {}} is a deterministic client error# (only a string or array is valid); reject before the switch, like completions.- from fastapi import HTTPException- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/p/B", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,- )+ backend, rec = _wired(monkeypatch, _FakeBackend("org/A-GGUF"), ("/p/B", "Q8_0", "org/B-GGUF"))with pytest.raises(HTTPException) as exc:asyncio.run(inference_route.openai_embeddings(@@ -4654,17 +4201,8 @@ def test_embeddings_rejects_object_input_before_switch(monkeypatch):def test_chat_oversized_audio_rejected_before_switch(monkeypatch):# Codex P2: the audio size cap is a cheap, target-independent length check, so an# oversized upload must 413 before the switch rather than loading a GGUF first.- from fastapi import HTTPException- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/p/B", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,- )+ backend, rec = _wired(monkeypatch, _FakeBackend("org/A-GGUF"), ("/p/B", "Q8_0", "org/B-GGUF"))big = "A" * (inference_route._MAX_AUDIO_B64_CHARS + 1)payload = _chat_request(model = "org/B-GGUF", audio_base64 = big)with pytest.raises(HTTPException) as exc:@@ -4677,22 +4215,12 @@ def test_chat_confirm_without_stream_mcp_rejected_before_switch(monkeypatch):# Codex P2: mcp_enabled opens the local tool loop on its own, so confirm+no-stream# +mcp is the same invalid shape as confirm+no-stream+tools and must 400 before# the switch. The old guard only checked explicit tool fields and missed it.- import state.tool_policy as _tp- from fastapi import HTTPExceptionmonkeypatch.setattr(_tp, "get_tool_policy", lambda: None) # no CLI --disable-tools- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("org/B-GGUF", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,- )- payload = _chat_request(- model = "org/B-GGUF", mcp_enabled = True, confirm_tool_calls = True, stream = False+ backend, rec = _wired(+ monkeypatch, _FakeBackend("org/A-GGUF"), ("org/B-GGUF", "Q8_0", "org/B-GGUF"))+ payload = _chat_request_b(mcp_enabled = True, confirm_tool_calls = True, stream = False)with pytest.raises(HTTPException) as exc:asyncio.run(inference_route.openai_chat_completions(payload, object(), "tester"))assert exc.value.status_code == 400@@ -4702,16 +4230,9 @@ def test_chat_confirm_without_stream_mcp_rejected_before_switch(monkeypatch):def test_require_vision_rejects_text_target_before_switch(monkeypatch):# Codex P2: an image request naming a different text-only GGUF must 400 before# the swap, so the resident vision model is not evicted for a rejected request.- from fastapi import HTTPException- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/local/B.gguf", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend("org/A-GGUF"), ("/local/B.gguf", "Q8_0", "org/B-GGUF"))monkeypatch.setattr(inference_route, "_target_is_vision", lambda _p, _v = None, _i = True: False)with pytest.raises(HTTPException) as exc:@@ -4725,14 +4246,8 @@ def test_require_vision_rejects_text_target_before_switch(monkeypatch):def test_require_vision_allows_vision_target(monkeypatch):- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/local/B.gguf", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend("org/A-GGUF"), ("/local/B.gguf", "Q8_0", "org/B-GGUF"))monkeypatch.setattr(inference_route, "_target_is_vision", lambda _p, _v = None, _i = True: True)asyncio.run(@@ -4756,14 +4271,8 @@ def test_an_audio_only_target_still_switches_for_an_audio_request(monkeypatch, t+ struct.pack("<I", 7)+ struct.pack("<?", True))- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = (str(tmp_path), "Q4_K_M", "org/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend("org/A-GGUF"), (str(tmp_path), "Q4_K_M", "org/B-GGUF"))asyncio.run(inference_route._maybe_auto_switch_model(@@ -4776,14 +4285,8 @@ def test_an_audio_only_target_still_switches_for_an_audio_request(monkeypatch, tdef test_require_vision_probes_the_quant_the_load_will_open(monkeypatch):# The resolver hands the gate a directory plus the quant to load, so the probe must# see the same pair the load does (#8772).- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/cache/snap", "UD-Q4_K_XL", "org/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend("org/A-GGUF"), ("/cache/snap", "UD-Q4_K_XL", "org/B-GGUF"))probed: list[tuple] = []monkeypatch.setattr(@@ -4833,11 +4336,8 @@ def test_target_is_vision_reads_a_subdir_quants_projector(tmp_path):def test_require_vision_ignores_reload_stash(monkeypatch):# The reload-stash path restores the model the request was already using; the# modality check applies only to an explicit resolver target, not a restore.- from core.inference import llama_keepwarm as kw- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(monkeypatch, enabled = False, resolves_to = None, backend = backend, recorder = rec)+ backend, rec = _wired(monkeypatch, _FakeBackend(None), None, enabled = False)monkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: 600)monkeypatch.setattr(settings, "idle_unload_is_configured", lambda: 600 > 0)monkeypatch.setattr(kw, "_inflight", 0)@@ -4859,7 +4359,6 @@ def test_require_vision_ignores_reload_stash(monkeypatch):def test_chat_validates_confirm_and_modality_before_switch():# Lock the order at the source: confirm-shape rejection precedes the hook, and# the hook rejects a non-vision target before the load.- import inspectsrc = inspect.getsource(inference_route.produce_openai_chat_completions)assert src.index("confirm_tool_calls requires stream=true") < src.index(@@ -4885,8 +4384,6 @@ def test_messages_have_image_helper():def test_anthropic_request_has_image_helper():- from types import SimpleNamespace-f = inference_route._anthropic_request_has_imagetext = SimpleNamespace(messages = [SimpleNamespace(content = "hi")])assert f(text) is False@@ -4904,7 +4401,6 @@ def test_responses_and_anthropic_wire_require_vision_from_images():# P2: the modality guard must fire on /v1/responses and /v1/messages too, so an# image request can't evict a vision model for a text-only target. Lock the wiring# at the source: each hook derives require_vision from the request's images.- import inspectresponses_src = inspect.getsource(inference_route.openai_responses)assert "_responses_has_image = _messages_have_image(" in responses_src@@ -4924,17 +4420,8 @@ def test_responses_and_anthropic_wire_require_vision_from_images():def test_count_tokens_rejects_malformed_tool_before_switch(monkeypatch):# Codex P2: /v1/messages/count_tokens must reject a malformed tool before the# switch, like /messages, so a count request can't evict the loaded model.- from fastapi import HTTPException- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/p/B", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,- )+ backend, rec = _wired(monkeypatch, _FakeBackend("org/A-GGUF"), ("/p/B", "Q8_0", "org/B-GGUF"))payload = _anthropic_payload_with_tools([{"name": "broken"}]) # no input_schema/typewith pytest.raises(HTTPException) as exc:asyncio.run(inference_route.anthropic_count_tokens(payload, object(), "tester"))@@ -4946,9 +4433,6 @@ def test_count_tokens_forwards_vision_guard_to_switch(monkeypatch):# Codex P2: an image /v1/messages/count_tokens naming a text-only GGUF must# carry the same require_vision guard as /messages, so it can't evict a loaded# vision model for a swap that can't serve the request.- class _Reached(Exception):- pass-captured = {}async def _capture(@@ -4990,8 +4474,6 @@ def _count_tokens_backend():"""A loaded GGUF backend wired into the count endpoint, as ``(switched, counted)``: auto-switchattempts, and the messages/system/tools/template kwargs the route hands to the tokenizer."""- from core.inference.llama_cpp import LlamaCppBackend-backend = _FakeBackend(loaded_id)backend.supports_tools = supports_tools# The real kwargs builder, so this pins the route against the completion path's mapping.@@ -5225,8 +4707,6 @@ def test_chat_count_tokens_prices_the_route_the_completion_takes(Applying the process tool policy without first asking which route the request takes prices abuilt-in catalog plus the action nudge, while the completion forwards verbatim and sends neither."""- import state.tool_policy as _tp-_switched, counted = _count_tokens_backend(monkeypatch, count = 99, supports_tools = True)async def _select(payload, *, tools_on, mcp_allowed):@@ -5442,8 +4922,6 @@ MCP_TOOL_PAYLOAD = [{"name": "lookup", "description": "d", "inputSchema": {"typedef test_cached_mcp_tools_reads_the_cache_without_probing(tmp_path, monkeypatch):- from core.inference.tools import cached_mcp_tools-_enabled_mcp_server(tmp_path, monkeypatch, cached = MCP_TOOL_PAYLOAD)specs, complete = cached_mcp_tools()assert complete is True@@ -5451,8 +4929,6 @@ def test_cached_mcp_tools_reads_the_cache_without_probing(tmp_path, monkeypatch)def test_cached_mcp_tools_reports_an_undiscovered_server_as_incomplete(tmp_path, monkeypatch):- from core.inference.tools import cached_mcp_tools-_enabled_mcp_server(tmp_path, monkeypatch)specs, complete = cached_mcp_tools()assert specs == []@@ -5465,7 +4941,6 @@ def test_cached_mcp_tools_reports_an_undiscovered_server_as_incomplete(tmp_path,def test_cached_mcp_tools_counts_a_cooloff_server_as_complete(tmp_path, monkeypatch):# The completion renders nothing for a cool-off server either, so skipping it is exact rather# than short. Declining here would blank the bar over an agreement.- from core.inference.tools import cached_mcp_tools_enabled_mcp_server(tmp_path, monkeypatch, cooloff = True)specs, complete = cached_mcp_tools()@@ -5540,8 +5015,6 @@ def test_a_count_admitted_while_idle_stands_down_if_a_run_starts(monkeypatch):counted.update(messages = messages)return 1234- from core.inference.llama_cpp import LlamaCppBackend-backend = inference_route.get_llama_cpp_backend()monkeypatch.setattr(backend, "count_chat_tokens", _count)assert LlamaCppBackend is not None@@ -5590,7 +5063,6 @@ def test_count_chat_tokens_stands_down_before_tokenizing(monkeypatch, abort, exp"""The abort has to escape the template except-block. Swallowed, it would setapply_template_failed and the text fallback would tokenize anyway, which is the workbeing declined. The control shows the poll alone does not stop an idle count."""- from core.inference import llama_cpp as llama_cpp_modfrom core.inference.llama_cpp import CountAborted, LlamaCppBackendposted: list = []@@ -5624,14 +5096,6 @@ def test_count_chat_tokens_stands_down_before_tokenizing(monkeypatch, abort, expreturn _FakeResponse({"prompt": "user hi"})return _FakeResponse({"tokens": [1, 2]})- class _CountBackend(LlamaCppBackend):- is_loaded = True- base_url = "http://127.0.0.1:1"- _auth_headers = None-- def __init__(self):- pass-monkeypatch.setattr(llama_cpp_mod.httpx, "Client", _FakeClient)call = lambda: _CountBackend().count_chat_tokens([{"role": "user", "content": "hi"}],@@ -5723,8 +5187,6 @@ def test_chat_count_tokens_counts_an_empty_chat_the_cli_policy_fills(monkeypatchaction nudge it injects are real occupancy, and refusing them would blank a bar that has anumber to show."""- import state.tool_policy as _tp-_switched, counted = _count_tokens_backend(monkeypatch, count = 850, supports_tools = True)async def _select(payload, *, tools_on, mcp_allowed):@@ -5906,8 +5368,6 @@ def test_count_chat_tokens_renders_with_the_requested_template_kwargs():"""The kwargs have to reach llama-server itself: /apply-template runs the same parseras /v1/chat/completions, so the rendered prompt only moves when they are in the body."""- from core.inference import llama_cpp as llama_cpp_mod- from core.inference.llama_cpp import LlamaCppBackendclass _FakeResponse:status_code = 200@@ -5941,14 +5401,6 @@ def test_count_chat_tokens_renders_with_the_requested_template_kwargs(prefill = "" if kwargs.get("enable_thinking", True) else " <think> </think>"return _FakeResponse({"prompt": "user hi assistant" + prefill})- class _CountBackend(LlamaCppBackend):- is_loaded = True- base_url = "http://127.0.0.1:1"- _auth_headers = None-- def __init__(self):- pass-monkeypatch.setattr(llama_cpp_mod.httpx, "Client", _FakeClient)assert (_CountBackend().count_chat_tokens(@@ -5973,8 +5425,6 @@ def test_strict_count_refuses_a_text_only_template_fallback(monkeypatch, failure"""/apply-template failing on a TEXT-ONLY prompt used to fall through to concatenating messagetext, dropping every role marker, special token and tool schema (~30% of a six-turn two-toolprompt). Strict callers publish what they get, so it must be an error, not an estimate."""- from core.inference import llama_cpp as llama_cpp_mod- from core.inference.llama_cpp import LlamaCppBackendclass _FakeResponse:def __init__(@@ -6010,14 +5460,6 @@ def test_strict_count_refuses_a_text_only_template_fallback(monkeypatch, failurereturn _FakeResponse({"error": "template error"}, status_code = 500)return _FakeResponse({"tokens": str(body.get("content", "")).split()})- class _CountBackend(LlamaCppBackend):- is_loaded = True- base_url = "http://127.0.0.1:1"- _auth_headers = None-- def __init__(self):- pass-monkeypatch.setattr(llama_cpp_mod.httpx, "Client", _FakeClient)messages = [{"role": "user", "content": "hi there"}]tools = [{"type": "function", "function": {"name": "web_search"}}]@@ -6038,9 +5480,6 @@ def test_an_empty_chat_sends_the_empty_list_unchanged(monkeypatch):mistral-7b-instruct-v0.3 driven through llama-server with --jinja: all five render.Injecting a placeholder system turn would add a system block to the count for Qwen3(+30 chars) and Phi-4 (+38), overcounting the empty chat the bar exists to show."""- from core.inference import llama_cpp as llama_cpp_mod- from core.inference.llama_cpp import LlamaCppBackend-seen = {}class _FakeResponse:@@ -6077,14 +5516,6 @@ def test_an_empty_chat_sends_the_empty_list_unchanged(monkeypatch):return _FakeResponse({"prompt": "<start_of_turn>model\n"})return _FakeResponse({"tokens": str(body.get("content", "")).split()})- class _CountBackend(LlamaCppBackend):- is_loaded = True- base_url = "http://127.0.0.1:1"- _auth_headers = None-- def __init__(self):- pass-monkeypatch.setattr(llama_cpp_mod.httpx, "Client", _FakeClient)count = _CountBackend().count_chat_tokens([], None, None, strict = True)assert seen["messages"] == [], "the count must not invent a turn the caller never sent"@@ -6128,9 +5559,6 @@ def test_a_count_never_spawns_mcp_servers():def _capture_audio_switch(monkeypatch):- class _Reached(Exception):- pass-captured = {}async def _capture(model, request, subject, **kwargs):@@ -6144,8 +5572,6 @@ def _capture_audio_switch(monkeypatch):@pytest.mark.parametrize("model", [None, "", "org/B-GGUF"])def test_audio_generate_model_selection(monkeypatch, model):- from models.inference import ChatCompletionRequest-reached, captured = _capture_audio_switch(monkeypatch)payload = ChatCompletionRequest(messages = [{"role": "user", "content": "say hi"}],@@ -6173,7 +5599,6 @@ def test_note_model_unloaded_clears_reload_stash(monkeypatch):def test_unload_route_clears_reload_stash(monkeypatch):# The /unload route must clear the stash on both the GGUF and non-GGUF branches.# Asserted on the impl: the route only adds the padded response.- import inspectsrc = inspect.getsource(inference_route._unload_model_impl)assert src.count("note_model_unloaded()") >= 2@@ -6181,7 +5606,6 @@ def test_unload_route_clears_reload_stash(monkeypatch):def test_non_gguf_load_clears_reload_stash():# A non-GGUF (Transformers/Unsloth) load must clear the stash like the GGUF# branch, so it never lingers until the idle poll (or forever, idle-unload off).- import inspectsrc = inspect.getsource(inference_route._load_model_impl)assert src.count("note_model_loaded()") >= 1 # non-GGUF branch@@ -6190,16 +5614,9 @@ def test_non_gguf_load_clears_reload_stash():def test_chat_rejects_malformed_tool_choice_before_switch(monkeypatch):# Codex P2: a forcing object with no function name must 400 before the switch.- from fastapi import HTTPException- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("org/B-GGUF", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend("org/A-GGUF"), ("org/B-GGUF", "Q8_0", "org/B-GGUF"))payload = _chat_request(model = "org/B-GGUF", tool_choice = {"type": "function", "function": {}})with pytest.raises(HTTPException) as exc:@@ -6210,17 +5627,9 @@ def test_chat_rejects_malformed_tool_choice_before_switch(monkeypatch):def test_chat_valid_tool_choice_reaches_hook(monkeypatch):# A well-formed forcing object must pass the pre-check and reach the hook.- class _Reached(Exception):- pass-- async def _boom(*a, **k):- raise _Reached()-monkeypatch.setattr(settings, "get_openai_auto_switch_enabled", lambda: True)monkeypatch.setattr(inference_route, "_maybe_auto_switch_model", _boom)- payload = _chat_request(- model = "org/B-GGUF", tool_choice = {"type": "function", "function": {"name": "ok"}}- )+ payload = _chat_request_b(tool_choice = {"type": "function", "function": {"name": "ok"}})with pytest.raises(_Reached):asyncio.run(inference_route.openai_chat_completions(payload, object(), "tester"))@@ -6228,8 +5637,6 @@ def test_chat_valid_tool_choice_reaches_hook(monkeypatch):def test_lifecycle_gate_serializes_across_loops():# Codex P2: the lifecycle gate must be process-wide so a swap on one loop blocks# inference starting on another. Two loops must never hold the gate at once.- import threading- from core.inference import llama_keepwarm as kwstate = {"cur": 0, "max": 0}slock = threading.Lock()@@ -6261,7 +5668,6 @@ def test_auto_switch_serializes_across_event_loops(monkeypatch):# Codex P2: the per-loop asyncio lock can't serialize two swaps on different# event loops in one process. The process-wide gate must, so the two slow loads# never overlap on the single model slot.- import threadingbackend = _FakeBackend("org/A-GGUF")state = {"cur": 0, "max": 0}@@ -6360,7 +5766,6 @@ def _run_responses_stream_no_model(resolves_to = None,):# Drive _responses_stream's GGUF-not-loaded guard. Returns (status, detail).- from fastapi import HTTPExceptionfrom models.inference import ResponsesRequest, ChatMessagemonkeypatch.setattr(settings, "get_openai_auto_switch_enabled", lambda: enabled)@@ -6430,7 +5835,6 @@ def _wire_unloaded_chat(def _chat_error(payload):- from fastapi import HTTPExceptionwith pytest.raises(HTTPException) as exc:asyncio.run(inference_route.openai_chat_completions(payload, object(), "tester"))return exc.value.status_code, exc.value.detail@@ -6438,11 +5842,8 @@ def _chat_error(payload):def test_chat_mistyped_gguf_repo_404s_before_vision_guard(monkeypatch):# #8376: image request with a mistyped GGUF id must 404, not 400 on the loaded model.- from fastapi import HTTPException- backend = _FakeBackend("unsloth/text-only-GGUF", "UD-Q4_K_XL")- rec = _LoadRecorder(backend)- _wire(monkeypatch, enabled = True, resolves_to = None, backend = backend, recorder = rec)+ backend, rec = _wired(monkeypatch, _FakeBackend("unsloth/text-only-GGUF", "UD-Q4_K_XL"), None)monkeypatch.setattr("utils.openai_auto_switch_settings.get_openai_auto_download_enabled", lambda: False)@@ -6451,10 +5852,7 @@ def test_chat_mistyped_gguf_repo_404s_before_vision_guard(monkeypatch):"describe_local_miss",lambda _m: (resolver.MISS_MODEL_NOT_FOUND, ()),)- payload = _chat_request(- model = "unsloth/typo-vision-GGUF",- image_base64 = "aGVsbG8=",- )+ payload = _chat_request_b(model = "unsloth/typo-vision-GGUF", image_base64 = "aGVsbG8=")request = type("_R", (), {"url": type("_U", (), {"path": "/v1/chat/completions"})()})()with pytest.raises(HTTPException) as exc:asyncio.run(inference_route.openai_chat_completions(payload, request, "tester"))@@ -6539,7 +5937,6 @@ def test_chat_available_id_list_is_capped(monkeypatch):def test_anthropic_undownloaded_model_uses_the_anthropic_envelope(monkeypatch):# Shared with /v1/messages, so the 404 must not leak an OpenAI-shaped body.- from fastapi import HTTPExceptionasync def _noop_switch(*a, **k):return None@@ -6560,7 +5957,6 @@ def test_anthropic_undownloaded_model_uses_the_anthropic_envelope(monkeypatch):def test_chat_undownloaded_model_uses_the_openai_envelope(monkeypatch):# The OpenAI surface carries param/code so SDK clients can branch on it.- from fastapi import HTTPException_wire_unloaded_chat(monkeypatch, enabled = True)request = type("_R", (), {"url": type("_U", (), {"path": "/v1/chat/completions"})()})()@@ -6596,7 +5992,6 @@ def test_gguf_only_paths_keep_the_generic_error_for_the_resident_non_gguf_model(def test_completions_keeps_the_generic_error_for_the_resident_non_gguf_model(monkeypatch):# Same contradiction on the raw-body surface, via _auto_switch_from_request_body.- from fastapi import HTTPExceptionresident = "unsloth/Llama-3.2-1B-Instruct"_wire_unloaded_chat(monkeypatch, enabled = True, catalog = (resident,))@@ -6688,17 +6083,10 @@ def _drive_idle_loop(def test_idle_unload_saves_slots_before_unload_and_stashes_manifest(monkeypatch, tmp_path):- import time- from core.inference import llama_keepwarm as kw-monkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: 0.005)monkeypatch.setattr(settings, "idle_unload_is_configured", lambda: 0.005 > 0)monkeypatch.setattr(settings, "get_auto_unload_keep_kv", lambda: True)- kw._inflight = 0- kw._pending = 0- kw._last_active = time.monotonic() - 3600- kw._last_unloaded_model = None- kw._kv_resume = None+ _reset_keepwarm()events = []backend = _FakeBackend("unsloth/Idle-GGUF", hf_variant = "Q4_K_M")@@ -6732,17 +6120,10 @@ def test_idle_unload_saves_slots_before_unload_and_stashes_manifest(monkeypatch,def test_idle_save_failure_still_unloads_plain(monkeypatch):- import time- from core.inference import llama_keepwarm as kw-monkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: 0.005)monkeypatch.setattr(settings, "idle_unload_is_configured", lambda: 0.005 > 0)monkeypatch.setattr(settings, "get_auto_unload_keep_kv", lambda: True)- kw._inflight = 0- kw._pending = 0- kw._last_active = time.monotonic() - 3600- kw._last_unloaded_model = None- kw._kv_resume = None+ _reset_keepwarm()unloads = []backend = _FakeBackend("unsloth/Idle-GGUF", hf_variant = "Q4_K_M")@@ -6765,17 +6146,10 @@ def test_idle_save_failure_still_unloads_plain(monkeypatch):def test_keep_kv_setting_off_skips_save(monkeypatch):- import time- from core.inference import llama_keepwarm as kw-monkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: 0.005)monkeypatch.setattr(settings, "idle_unload_is_configured", lambda: 0.005 > 0)monkeypatch.setattr(settings, "get_auto_unload_keep_kv", lambda: False)- kw._inflight = 0- kw._pending = 0- kw._last_active = time.monotonic() - 3600- kw._last_unloaded_model = None- kw._kv_resume = None+ _reset_keepwarm()saves, unloads = [], []backend = _FakeBackend("unsloth/Idle-GGUF")@@ -6795,18 +6169,11 @@ def test_keep_kv_setting_off_skips_save(monkeypatch):def test_keep_kv_disabled_mid_save_discards_manifest(monkeypatch, tmp_path):- import time- from core.inference import llama_keepwarm as kw-keep = {"on": True}monkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: 0.005)monkeypatch.setattr(settings, "idle_unload_is_configured", lambda: 0.005 > 0)monkeypatch.setattr(settings, "get_auto_unload_keep_kv", lambda: keep["on"])- kw._inflight = 0- kw._pending = 0- kw._last_active = time.monotonic() - 3600- kw._last_unloaded_model = None- kw._kv_resume = None+ _reset_keepwarm()unloads = []backend = _FakeBackend("unsloth/Idle-GGUF", hf_variant = "Q4_K_M")@@ -6837,18 +6204,11 @@ def test_keep_kv_disabled_mid_save_discards_manifest(monkeypatch, tmp_path):def test_idle_ttl_disabled_mid_save_skips_unload(monkeypatch, tmp_path):- import time- from core.inference import llama_keepwarm as kw-ttl = {"v": 0.005}monkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: ttl["v"])monkeypatch.setattr(settings, "idle_unload_is_configured", lambda: ttl["v"] > 0)monkeypatch.setattr(settings, "get_auto_unload_keep_kv", lambda: True)- kw._inflight = 0- kw._pending = 0- kw._last_active = time.monotonic() - 3600- kw._last_unloaded_model = None- kw._kv_resume = None+ _reset_keepwarm()unloads = []backend = _FakeBackend("unsloth/Idle-GGUF", hf_variant = "Q4_K_M")@@ -6876,8 +6236,6 @@ def test_idle_ttl_disabled_mid_save_skips_unload(monkeypatch, tmp_path):def test_alias_reload_restores_slots_and_deletes_files(monkeypatch, tmp_path):- from core.inference import llama_keepwarm as kw-backend = _FakeBackend(None) # idle-unload emptied the backendbackend._slot_save_binary = ("/bin/llama-server", 111)restored = []@@ -6898,16 +6256,13 @@ def test_alias_reload_restores_slots_and_deletes_files(monkeypatch, tmp_path):def test_no_restore_when_different_model_loads(monkeypatch, tmp_path):- from core.inference import llama_keepwarm as kw-backend = _FakeBackend(None)backend._slot_save_binary = ("/bin/llama-server", 111)restored = []backend.restore_slots_for_resume = lambda manifest: restored.append(manifest)rec = _LoadRecorder(backend)- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = ("unsloth/B-GGUF", None, "unsloth/B-GGUF"),backend = backend,recorder = rec,@@ -6924,8 +6279,6 @@ def test_no_restore_when_different_model_loads(monkeypatch, tmp_path):def test_restore_skipped_when_binary_changed(monkeypatch, tmp_path):- from core.inference import llama_keepwarm as kw-state_file, manifest = _seed_kv_manifest(tmp_path)backend = _FakeBackend("unsloth/A-GGUF", hf_variant = "Q4_K_M")backend._gguf_path = manifest["gguf"]@@ -6939,8 +6292,6 @@ def test_restore_skipped_when_binary_changed(monkeypatch, tmp_path):def test_restore_skipped_when_launch_config_changed(tmp_path):- from core.inference import llama_keepwarm as kw-state_file, manifest = _seed_kv_manifest(tmp_path)backend = _FakeBackend("unsloth/A-GGUF", hf_variant = "Q4_K_M")backend._gguf_path = manifest["gguf"]@@ -6955,8 +6306,6 @@ def test_restore_skipped_when_launch_config_changed(tmp_path):def test_restore_skipped_when_gguf_rewritten_in_place(tmp_path):- from core.inference import llama_keepwarm as kw-state_file, manifest = _seed_kv_manifest(tmp_path)with open(manifest["gguf"], "wb") as fh:fh.write(b"different weights") # same path, new content@@ -6972,8 +6321,6 @@ def test_restore_skipped_when_gguf_rewritten_in_place(tmp_path):def test_note_model_unloaded_purges_manifest_and_files(tmp_path):- from core.inference import llama_keepwarm as kw-state_file, manifest = _seed_kv_manifest(tmp_path)kw._set_last_unloaded(("org/A-GGUF", "Q4_K_M"))kw._set_kv_resume(manifest)@@ -6984,8 +6331,6 @@ def test_note_model_unloaded_purges_manifest_and_files(tmp_path):def test_note_model_loaded_purges_manifest_and_files(tmp_path):- from core.inference import llama_keepwarm as kw-state_file, manifest = _seed_kv_manifest(tmp_path)kw._set_last_unloaded(("org/A-GGUF", "Q4_K_M"))kw._set_kv_resume(manifest)@@ -6996,8 +6341,6 @@ def test_note_model_loaded_purges_manifest_and_files(tmp_path):def test_new_idle_save_purges_previous_manifest_files(tmp_path):- from core.inference import llama_keepwarm as kw-old_file, old_manifest = _seed_kv_manifest(tmp_path)kw._set_kv_resume(old_manifest)new_file = tmp_path / "resume-def-slot0.bin"@@ -7016,7 +6359,6 @@ def test_new_idle_save_purges_previous_manifest_files(tmp_path):def test_sweep_slot_save_dir_removes_only_resume_files(monkeypatch, tmp_path):- from core.inference import llama_keepwarm as kwfrom utils.paths import storage_rootsmonkeypatch.setattr(storage_roots, "llama_slot_cache_root", lambda: tmp_path)@@ -7030,8 +6372,6 @@ def test_sweep_slot_save_dir_removes_only_resume_files(monkeypatch, tmp_path):def test_keep_kv_setting_roundtrip_and_default(monkeypatch):- import storage.studio_db as db-store = {}monkeypatch.setattr(db, "upsert_app_settings", lambda m: store.update(m))monkeypatch.setattr(settings, "_cached_setting", lambda k, d = None: store.get(k, d))@@ -7049,8 +6389,6 @@ def test_keep_kv_setting_roundtrip_and_default(monkeypatch):def test_stale_stash_cleanup_waits_for_lifecycle_gate(monkeypatch, tmp_path):# The loop's stale-stash purge must wait on the gate a mid-reload holds.- import time- from core.inference import llama_keepwarm as kwmonkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: 3600)monkeypatch.setattr(settings, "idle_unload_is_configured", lambda: 3600 > 0)@@ -7077,10 +6415,6 @@ def test_stale_stash_cleanup_waits_for_lifecycle_gate(monkeypatch, tmp_path):def test_put_route_disabling_keep_kv_purges_saved_state(monkeypatch, tmp_path):- import routes.settings as settings_route- import storage.studio_db as db- from core.inference import llama_keepwarm as kw-store = {}monkeypatch.setattr(db, "upsert_app_settings", lambda m: store.update(m))monkeypatch.setattr(settings, "_cached_setting", lambda k, d = None: store.get(k, d))@@ -7096,8 +6430,6 @@ def test_put_route_disabling_keep_kv_purges_saved_state(monkeypatch, tmp_path):def test_keep_kv_only_update_leaves_env_idle_ttl_active(monkeypatch):# A keep-KV-only update must not materialize the env TTL as a stored value.- import routes.settings as settings_route- import storage.studio_db as dbstore = {}monkeypatch.setattr(db, "upsert_app_settings", lambda m: store.update(m))@@ -7131,14 +6463,11 @@ def test_keep_kv_only_update_leaves_env_idle_ttl_active(monkeypatch):def test_load_impl_notes_loaded_with_backend_off_loop():- import inspectsrc = inspect.getsource(inference_route._load_model_impl)assert "to_thread(note_model_loaded, llama_backend)" in srcdef test_restore_matches_gguf_realpath_across_naming(tmp_path):- from core.inference import llama_keepwarm as kw-blob = tmp_path / "blob.gguf"blob.write_bytes(b"gguf")link = tmp_path / "snapshot.gguf"@@ -7162,8 +6491,6 @@ def test_restore_matches_gguf_realpath_across_naming(tmp_path):def test_setter_rejects_idle_below_floor(monkeypatch):- import storage.studio_db as db-writes = []monkeypatch.setattr(db, "upsert_app_settings", lambda m: writes.append(dict(m)))settings._cache.clear()@@ -7180,7 +6507,6 @@ def test_setter_rejects_idle_below_floor(monkeypatch):def test_media_idle_setting_roundtrip_and_default(monkeypatch):# The image/video TTL is persisted on its own key, in the same write as the rest# of the section, and starts off: a chat TTL alone must not evict a pipeline.- import storage.studio_db as dbstore = {}monkeypatch.setattr(db, "upsert_app_settings", lambda m: store.update(m))@@ -7205,8 +6531,6 @@ def test_media_idle_setting_roundtrip_and_default(monkeypatch):def test_settings_route_reports_the_media_idle_ttl(monkeypatch):- import routes.settings as settings_route-monkeypatch.setattr(settings_route, "get_stored_media_auto_unload_idle_seconds", lambda: 600)monkeypatch.setattr(settings_route, "get_media_auto_unload_idle_seconds", lambda: 600)resp = settings_route.get_openai_auto_switch("tester")@@ -7221,9 +6545,6 @@ def test_settings_route_reports_the_media_idle_ttl(monkeypatch):def test_put_route_rejects_media_idle_below_floor():- import routes.settings as settings_route- from fastapi import HTTPException-payload = settings_route.OpenAIAutoSwitchPayload(enabled = True, media_auto_unload_idle_seconds = 30)@@ -7233,9 +6554,6 @@ def test_put_route_rejects_media_idle_below_floor():def test_put_route_rejects_idle_below_floor():- import routes.settings as settings_route- from fastapi import HTTPException-payload = settings_route.OpenAIAutoSwitchPayload(enabled = True, auto_unload_idle_seconds = 30)with pytest.raises(HTTPException) as excinfo:settings_route.update_openai_auto_switch(payload, "tester")@@ -7392,14 +6710,8 @@ def test_a_carried_ctx_flag_cannot_outrank_a_freshly_saved_context(monkeypatch):assert entry["max_seq_length"] == 32768assert entry["llama_extra_args"] == ["--ctx-size", "8192", "--top-k", "40"]- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend(None), ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"))_run_hook("unsloth/B-GGUF")@@ -7420,14 +6732,8 @@ def test_a_matching_explicit_ctx_flag_survives_auto_switch(monkeypatch):spec_draft_n_max = 3,)- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend(None), ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"))_run_hook("unsloth/B-GGUF")@@ -7523,14 +6829,8 @@ def test_load_kwargs_strip_only_the_shadow_groups_the_override_supplies():def test_saved_parallel_slots_reach_an_api_load(monkeypatch):# Parallel decode slots are a per-model setting the picker sends on every GGUF load.- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend(None), ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"))monkeypatch.setattr(settings, "get_model_override", lambda mid: {"n_parallel": 8})@@ -7577,14 +6877,8 @@ def test_eviction_cleanup_clears_mirrored_fields_but_keeps_launch_flags(overridedef test_auto_switch_prefers_variant_qualified_override(monkeypatch):# Settings are per quant; the bare repo id is only the fallback.- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend(None), ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"))stored = {"unsloth/B-GGUF": {"max_seq_length": 1024},@@ -7599,14 +6893,8 @@ def test_auto_switch_prefers_variant_qualified_override(monkeypatch):def test_auto_switch_falls_back_to_bare_repo_override(monkeypatch):- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend(None), ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"))stored = {"unsloth/B-GGUF": {"max_seq_length": 1024}}monkeypatch.setattr(settings, "get_model_override", lambda mid: stored.get(mid, {}))@@ -7630,14 +6918,10 @@ def test_override_route_preserves_launch_flags_across_a_settings_only_update(ovedef test_override_found_under_a_concrete_path_with_variant(monkeypatch):# A local folder resolves to repo id + path; settings saved against the path must be found.- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(+ backend, rec = _wired(monkeypatch,- enabled = True,- resolves_to = ("/models/local/Qwen3-8B-Q4_K_M.gguf", "Q4_K_M", "unsloth/Qwen3-8B-GGUF"),- backend = backend,- recorder = rec,+ _FakeBackend(None),+ ("/models/local/Qwen3-8B-Q4_K_M.gguf", "Q4_K_M", "unsloth/Qwen3-8B-GGUF"),)stored = {"/models/local/Qwen3-8B-Q4_K_M.gguf:Q4_K_M": {"max_seq_length": 8192}}monkeypatch.setattr(settings, "get_model_override", lambda mid: stored.get(mid, {}))@@ -7649,14 +6933,8 @@ def test_override_found_under_a_concrete_path_with_variant(monkeypatch):def test_path_qualified_override_beats_repo_qualified(monkeypatch):# Most specific first: the settings page keys a local row by the path being loaded,# while the repo id is only the advertised alias. The row the user edited wins.- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/models/local/x.gguf", "Q4_K_M", "unsloth/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend(None), ("/models/local/x.gguf", "Q4_K_M", "unsloth/B-GGUF"))stored = {"unsloth/B-GGUF:Q4_K_M": {"max_seq_length": 8192},@@ -7714,14 +6992,8 @@ def test_a_repo_save_retires_the_legacy_snapshot_path_entry(monkeypatch):assert f"{_LEGACY_SNAPSHOT}:Q4_K_M" not in resp.overrides# End to end: the load the request triggers carries the saved value, not the retired one.- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = (_LEGACY_SNAPSHOT, "Q4_K_M", "unsloth/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend(None), (_LEGACY_SNAPSHOT, "Q4_K_M", "unsloth/B-GGUF"))_run_hook("unsloth/B-GGUF:Q4_K_M")@@ -7894,14 +7166,8 @@ def test_a_fill_never_creates_a_snapshot_path_key_over_a_repo_id_entry(override_def test_stale_gpu_ids_are_dropped_not_fatal(monkeypatch):# A two-GPU pin on a one-GPU box used to 400 the load; one dead field degrades to defaults.- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend(None), ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"))monkeypatch.setattr(settings,@@ -7922,20 +7188,11 @@ def test_stale_gpu_ids_are_dropped_not_fatal(monkeypatch):def test_usable_gpu_ids_are_kept(monkeypatch):- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend(None), ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"))monkeypatch.setattr(settings, "get_model_override", lambda mid: {"gpu_ids": [0, 1]})- async def _usable(ids, index_kind = "physical"):- return True-monkeypatch.setattr(inference_route, "_override_gpu_ids_still_resolve", _usable)_run_hook("unsloth/B-GGUF")@@ -7955,7 +7212,6 @@ def test_override_gpu_ids_probe_never_raises(monkeypatch):def test_vulkan_ordinal_absent_from_the_probe_is_unusable(monkeypatch):# resolve_requested_gpu_ids only rejects malformed ordinals; presence needs the ggml probe.- from core.inference.llama_cpp import LlamaCppBackendmonkeypatch.setattr(LlamaCppBackend, "_is_vulkan_backend", staticmethod(lambda: True))monkeypatch.setattr(@@ -7971,7 +7227,6 @@ def test_vulkan_ordinal_absent_from_the_probe_is_unusable(monkeypatch):def test_vulkan_probe_without_a_binary_does_not_block_the_load(monkeypatch):# Nothing to probe with, and refusing would drop a valid pin on every load.- from core.inference.llama_cpp import LlamaCppBackendmonkeypatch.setattr(LlamaCppBackend, "_is_vulkan_backend", staticmethod(lambda: True))monkeypatch.setattr(LlamaCppBackend, "_find_llama_server_binary", staticmethod(lambda: None))@@ -7998,7 +7253,6 @@ def _pin_resolves_on_a_host_with_device_0(monkeypatch):def test_a_pin_written_in_the_other_index_space_is_unusable(monkeypatch):# What the availability checks cannot see: ordinal 0 and physical device 0 both exist,# so a pin carried across a backend change passes every presence test.- from core.inference.llama_cpp import LlamaCppBackend_pin_resolves_on_a_host_with_device_0(monkeypatch)monkeypatch.setattr(@@ -8020,8 +7274,6 @@ def test_a_pin_written_in_the_other_index_space_is_unusable(monkeypatch):def test_a_rocm_pin_survives_while_the_backend_is_still_rocm(monkeypatch):- from core.inference.llama_cpp import LlamaCppBackend-_pin_resolves_on_a_host_with_device_0(monkeypatch)monkeypatch.setattr(LlamaCppBackend, "_is_vulkan_backend", staticmethod(lambda: False))assert asyncio.run(inference_route._override_gpu_ids_still_resolve([0], "physical")) is True@@ -8218,24 +7470,14 @@ def test_real_quant_suffix_on_a_path_still_carries_flags_over(override_store):def test_load_retries_without_gpu_ids_when_the_loader_rejects_the_pin(monkeypatch):# The pre-flight check can't mirror every loader rule, and a stale pin must not block a load.- from fastapi import HTTPException- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend(None), ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"))monkeypatch.setattr(settings, "get_model_override", lambda mid: {"gpu_ids": [0], "max_seq_length": 4096})- async def _usable(ids, index_kind = "physical"):- return True-monkeypatch.setattr(inference_route, "_override_gpu_ids_still_resolve", _usable)calls = {"n": 0}@@ -8259,22 +7501,11 @@ def test_load_retries_without_gpu_ids_when_the_loader_rejects_the_pin(monkeypatcdef test_a_non_gpu_load_failure_is_not_retried(monkeypatch):- from fastapi import HTTPException-- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend(None), ("unsloth/B-GGUF", "Q4_K_M", "unsloth/B-GGUF"))monkeypatch.setattr(settings, "get_model_override", lambda mid: {"gpu_ids": [0]})- async def _usable(ids, index_kind = "physical"):- return True-monkeypatch.setattr(inference_route, "_override_gpu_ids_still_resolve", _usable)calls = {"n": 0}@@ -8398,8 +7629,6 @@ def test_a_tag_that_names_no_quant_resolves_to_the_repo(monkeypatch):# still miss, or a swap would serve the wrong weights under the right name.from core.inference.local_model_resolver import _LocalGgufEntry- import time-entry = _LocalGgufEntry("org/model", "/srv/models/org--model", ("Q4_K_M",))# Fresh stamp so _index serves this instead of rescanning over it.monkeypatch.setattr(resolver, "_scan", (time.monotonic(), {"org/model": entry}))@@ -8421,15 +7650,6 @@ def test_any_finished_download_drops_the_resolver_cache(monkeypatch):# Only the API auto-download watcher invalidated, so a GGUF fetched in the Hub UI# stayed absent to the cache-only request path and the resident model answered.# Every worker exits through here.- import logging-- from hub.services import download_lifecycle-- class _Proc:- stderr = None-- def wait(self):- return 0class _Registry:def cancel_requested(self, key):@@ -8501,10 +7721,6 @@ def test_trusted_cache_rechecks_snapshot_after_freshness(monkeypatch):def test_async_scan_folder_routes_offload_storage_and_invalidation(monkeypatch):- from types import SimpleNamespace-- import routes.models as model_routes-event_loop_thread = threading.get_ident()calls = []@@ -8540,7 +7756,6 @@ def test_async_scan_folder_routes_offload_storage_and_invalidation(monkeypatch):def test_scan_folder_removal_revokes_additions_only_cache_trust(monkeypatch):- import routes.models as model_routesfrom hub.services.models import local_inventoryentry = resolver._LocalGgufEntry("org/old", "/custom/org--old", ("Q4_K_M",))@@ -8572,10 +7787,7 @@ def test_scan_folder_removal_revokes_additions_only_cache_trust(monkeypatch):def test_scan_folder_storage_removals_report_if_a_row_changed(monkeypatch):- from types import SimpleNamespace-from hub.storage import scan_folders- from storage import studio_dbclass _Connection:def __init__(self, rowcount):@@ -8604,7 +7816,6 @@ def test_scan_folder_storage_removals_report_if_a_row_changed(monkeypatch):def test_noop_scan_folder_removals_do_not_invalidate_the_index(monkeypatch):- import routes.models as model_routesfrom hub.services.models import local_inventoryinvalidated = []@@ -8648,15 +7859,6 @@ def test_local_and_remote_agree_on_the_preferred_quant():def test_a_just_downloaded_model_is_evidence_before_the_scan_indexes_it(monkeypatch):# The retained index covers what was known, but nothing covers the model that just# landed until the next scan: a bare request for it was answered by the resident one.- import logging-- from hub.services import download_lifecycle-- class _Proc:- stderr = None-- def wait(self):- return 0class _Registry:def cancel_requested(self, key):@@ -8697,16 +7899,6 @@ def test_a_finished_dataset_is_not_recorded_as_a_local_model(monkeypatch):# finalize_worker_exit is shared with dataset downloads. Noting one as a local model# would refuse a bare /v1 request naming that id instead of letting a foreign id# fall through, and would kick off a multi-directory scan for nothing.- import logging- import time-- from hub.services import download_lifecycle-- class _Proc:- stderr = None-- def wait(self):- return 0class _Registry:def cancel_requested(self, key):@@ -8742,7 +7934,6 @@ def test_two_local_paths_differing_only_in_case_are_not_the_same_model(monkeypat# _loaded_satisfies lowercased the request and every backend identifier, so on a# case-sensitive filesystem /srv/models/foo.gguf read as satisfied by a resident# /srv/models/Foo.gguf. A repo alias must still stay case-insensitive.- import osloaded = _FakeBackend(loaded_id = "/srv/models/Foo.gguf")monkeypatch.setattr(inference_route, "get_llama_cpp_backend", lambda: loaded)@@ -8766,8 +7957,6 @@ def test_abs_path_ids_are_recognised_in_either_platform_spelling():then reached /v1/models as a published host path. Ids outlive the machinethat wrote them (settings sync, WSL, a copied config), and the model-overrideidentity already folds both spellings."""- from types import SimpleNamespace-for spelling in ("/home/me/models/x.gguf", "C:\\models\\x.gguf", "//host/share/x.gguf"):assert resolver._is_abs_path_id(spelling) is True, spelling# An alias wins, and with none the path is stripped to a public id.@@ -8793,8 +7982,6 @@ def test_fill_absent_fields_put_never_replaces_a_newer_server_value(override_stowrites each model in turn, so a save by another tab during that pass wasoverwritten by this browser's older copy. fill_absent_fields writes only whatthe entry lacks, so every value already on the server wins."""- import routes.settings as settings_route-# The other tab's save lands first.newer = settings_route.ModelOverridePayload(model_id = "unsloth/B-GGUF", max_seq_length = 8192)settings_route.update_openai_auto_switch_override(newer, "tester")@@ -8819,8 +8006,6 @@ def test_fill_absent_fields_carries_the_browser_only_settings_into_a_legacy_entrllama_extra_args and max_seq_length. An upgraded install holds such an entry whilelocalStorage holds the context, KV cache, speculative and GPU settings, and anentry-level skip would strand exactly what the migration exists to carry."""- import routes.settings as settings_route-store = _mock_override_store(monkeypatch)legacy = settings_route.ModelOverridePayload(@@ -8865,8 +8050,6 @@ def test_fill_absent_fields_carries_the_browser_only_settings_into_a_legacy_entrdef test_fill_absent_fields_matches_a_legacy_casing_and_never_deletes(override_store):"""The stored key can carry the casing an older install typed, and it must notbe duplicated or emptied by a fill for the folded spelling."""- import routes.settings as settings_route-stored = settings_route.ModelOverridePayload(model_id = "Unsloth/B-GGUF:Q4_K_M", max_seq_length = 8192)@@ -8896,8 +8079,6 @@ def test_fill_absent_fields_does_not_break_the_empty_payload_removal(override_st"""fill_absent_fields is a write mode, not a saved field: leaving it in the dumpedpayload would make every request look non-empty and silently retire the legacy"a payload carrying only model_id forgets this model" contract."""- import routes.settings as settings_route-stored = settings_route.ModelOverridePayload(model_id = "unsloth/B-GGUF", max_seq_length = 4096)settings_route.update_openai_auto_switch_override(stored, "tester")empty = settings_route.ModelOverridePayload(model_id = "unsloth/B-GGUF")@@ -8908,8 +8089,6 @@ def test_fill_absent_fields_does_not_break_the_empty_payload_removal(override_stdef test_map_entry_fill_reads_and_writes_in_one_transaction(tmp_path, monkeypatch):"""The real store, not the in-memory stand-in: the read has to share the write'stransaction, or a concurrent writer still slips between them."""- import storage.studio_db as db-monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path))monkeypatch.setattr(db, "_schema_ready", False)@@ -8950,8 +8129,6 @@ def test_a_fill_never_relabels_a_stored_gpu_pin_with_this_browser_s_index_space(by field the ids would stay and the qualifier would land, and the row wouldthen name devices in a space it was never written in."""- import storage.studio_db as db-monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path))monkeypatch.setattr(db, "_schema_ready", False)@@ -8980,8 +8157,6 @@ def test_gpu_ids_dedupe_is_not_a_scan_of_the_list_being_built():de-duplicates it. Testing membership against the growing list walks up toMAX_GPU_ID entries per element; a set keeps the pass linear. Order, bounds andthe bool rejection all have to survive the change."""- import time-from utils.openai_auto_switch_settings import MAX_GPU_ID, normalize_model_overrideassert normalize_model_override({"gpu_ids": [3, 1, 3, 0, 1, 2]})["gpu_ids"] == [3, 1, 0, 2]@@ -9003,7 +8178,6 @@ def test_gpu_ids_payload_is_bounded():but work, so it is rejected at the boundary. A real device list is tiny."""import pydantic- import routes.settings as settings_routefrom utils.openai_auto_switch_settings import MAX_GPU_IDassert settings_route.MAX_GPU_IDS == MAX_GPU_ID + 1@@ -9024,9 +8198,7 @@ def test_gpu_ids_payload_is_bounded():def _switch_with_overrides(monkeypatch, resolves_to, stored, requested):"""Run the auto-switch hook against a real override map and return the load."""- backend = _FakeBackend(None)- rec = _LoadRecorder(backend)- _wire(monkeypatch, enabled = True, resolves_to = resolves_to, backend = backend, recorder = rec)+ backend, rec = _wired(monkeypatch, _FakeBackend(None), resolves_to)_mock_override_store(monkeypatch)for key, max_seq_length in stored.items():settings.set_model_override(key, max_seq_length = max_seq_length)@@ -9127,7 +8299,6 @@ def test_a_fill_does_not_replay_a_stored_flag_through_validation(monkeypatch):flag that has been denylisted since it was saved would 400 the one-timemigration, which then retries on every start. A fill keeps the stored flagswithout sending them back."""- import routes.settings as settings_routefrom core.inference import llama_server_argsstore = _mock_override_store(monkeypatch)@@ -9210,9 +8381,6 @@ def test_two_spellings_of_one_cached_quant_do_not_delete_each_others_save(monkeyby the snapshot path an upgraded install still holds, can both write before eithercleanup runs and then retire each other's row. Both calls return 200 and nothing isstored. Whichever runs second must retire the first instead."""- import threading- import routes.settings as settings_route-_mock_override_store(monkeypatch)repo = "unsloth/Qwen3-8B-GGUF:Q4_K_M"snapshot = "/mnt/old-cache/models--unsloth--Qwen3-8B-GGUF/snapshots/abc123:Q4_K_M"@@ -9287,13 +8455,7 @@ def test_mlx_kv_bits_survives_the_whole_override_projection():def _idle_backend(kw, monkeypatch, *, user_loaded):"""A loaded, long-idle GGUF backend wired into the idle loop."""- import time-- kw._inflight = 0- kw._pending = 0- kw._last_active = time.monotonic() - 3600- kw._last_unloaded_model = None- kw._kv_resume = None+ _reset_keepwarm()backend = _FakeBackend("unsloth/Idle-GGUF", hf_variant = "Q4_K_M")backend._loaded_by_user_action = user_loaded@@ -9309,8 +8471,6 @@ def _idle_backend(kw, monkeypatch, *, user_loaded):def test_api_only_setting_roundtrip_and_default(monkeypatch):- import storage.studio_db as db-store = {}monkeypatch.setattr(db, "upsert_app_settings", lambda m: store.update(m))monkeypatch.setattr(settings, "_cached_setting", lambda k, d = None: store.get(k, d))@@ -9328,7 +8488,6 @@ def test_api_only_setting_roundtrip_and_default(monkeypatch):def test_idle_unload_still_frees_a_user_loaded_model_by_default(monkeypatch):# Backwards compatibility: with the toggle off, provenance changes nothing.- from core.inference import llama_keepwarm as kwbackend = _idle_backend(kw, monkeypatch, user_loaded = True)monkeypatch.setattr(settings, "get_auto_unload_api_only", lambda: False)@@ -9338,8 +8497,6 @@ def test_idle_unload_still_frees_a_user_loaded_model_by_default(monkeypatch):def test_api_only_spares_a_user_loaded_model_but_not_an_api_one(monkeypatch):- from core.inference import llama_keepwarm as kw-monkeypatch.setattr(settings, "get_auto_unload_api_only", lambda: True)pinned = _idle_backend(kw, monkeypatch, user_loaded = True)@@ -9355,16 +8512,14 @@ def test_api_only_spares_a_user_loaded_model_but_not_an_api_one(monkeypatch):def test_an_idle_restored_model_is_api_provenance(monkeypatch):# The restore runs through the auto-switch path, so the model it brings back# is unloadable again on the next idle rather than pinned forever.- from core.inference import llama_keepwarm as kwmonkeypatch.setattr(settings, "get_auto_unload_api_only", lambda: True)backend = _idle_backend(kw, monkeypatch, user_loaded = True)backend.is_loaded = Falsebackend.model_identifier = Nonerec = _LoadRecorder(backend)- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = ("unsloth/B-GGUF", None, "unsloth/B-GGUF"),backend = backend,recorder = rec,@@ -9379,8 +8534,6 @@ def test_an_idle_restored_model_is_api_provenance(monkeypatch):def test_unload_clears_the_user_load_flag():# Otherwise the next API load inherits the pin and never frees its VRAM.- import inspect- from core.inference.llama_cpp import LlamaCppBackendbackend = LlamaCppBackend()assert backend._loaded_by_user_action is False # off until a UI load says otherwise@@ -9393,8 +8546,6 @@ def test_unload_clears_the_user_load_flag():def test_the_load_route_pins_and_other_load_surfaces_do_not(monkeypatch):- import inspect-backend = _FakeBackend("unsloth/A-GGUF")backend._loaded_by_user_action = Falsemonkeypatch.setattr(inference_route, "get_llama_cpp_backend", lambda: backend)@@ -9420,7 +8571,6 @@ def test_the_load_route_pins_and_other_load_surfaces_do_not(monkeypatch):def test_api_only_turned_on_mid_save_still_spares_the_model(monkeypatch, tmp_path):# A KV save can take seconds; the loop re-reads the TTL and keep-KV after it# for exactly that reason, so provenance has to be re-read there too.- from core.inference import llama_keepwarm as kwon = {"now": False}monkeypatch.setattr(settings, "get_auto_unload_api_only", lambda: on["now"])@@ -9722,14 +8872,8 @@ def test_normalize_keeps_an_explicit_empty_list_only_when_asked(monkeypatch):def _wire_refusing_switch(monkeypatch):- backend = _FakeBackend("org/A-GGUF")- rec = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("/local/B.gguf", "Q8_0", "org/B-GGUF"),- backend = backend,- recorder = rec,+ backend, rec = _wired(+ monkeypatch, _FakeBackend("org/A-GGUF"), ("/local/B.gguf", "Q8_0", "org/B-GGUF"))monkeypatch.setattr(inference_route, "_target_is_vision", lambda _p, _v = None, _i = True: False)return rec@@ -9770,9 +8914,6 @@ def test_a_video_request_labels_the_switch_refusal_video(monkeypatch):"""End to end through the handler: video joins require_vision, so the labelhas to follow or the user who attached a clip is told about image or audio."""- class _Reached(Exception):- pass-captured = {}async def _capture(@@ -9870,14 +9011,8 @@ def test_a_video_request_never_switches_to_a_non_gguf_target():def test_count_tokens_switch_marks_new_model_preview_owned(monkeypatch):- backend = _FakeBackend("org/A-GGUF")- recorder = _LoadRecorder(backend)- _wire(- monkeypatch,- enabled = True,- resolves_to = ("org/B-GGUF", None, "org/B-GGUF"),- backend = backend,- recorder = recorder,+ backend, recorder = _wired(+ monkeypatch, _FakeBackend("org/A-GGUF"), ("org/B-GGUF", None, "org/B-GGUF"))inference_route._set_preview_resident("org/A-GGUF")asyncio.run(@@ -10029,7 +9164,6 @@ def test_a_downloaded_spark_repository_resolves_to_its_llm_checkpoint(tmp_path,def test_a_diffusers_pipeline_is_not_a_servable_chat_model(tmp_path):# The Images and Video backends own these; /v1/chat/completions cannot serve them.- from types import SimpleNamespacepipeline = _local_checkpoint(tmp_path, "SomeDiffusionPipeline")(pipeline / "model_index.json").write_text("{}")@@ -10039,7 +9173,6 @@ def test_a_diffusers_pipeline_is_not_a_servable_chat_model(tmp_path):def test_a_partial_download_is_not_a_servable_chat_model(tmp_path):# Advertising an incomplete snapshot hands out an id whose load must fail.- from types import SimpleNamespacepath = _local_checkpoint(tmp_path)info = SimpleNamespace(id = str(path), path = str(path), partial = True)@@ -10048,7 +9181,6 @@ def test_a_partial_download_is_not_a_servable_chat_model(tmp_path):def test_an_adapter_only_directory_is_not_a_servable_chat_model(tmp_path):# A bare LoRA adapter has no config.json and cannot be served on its own.- from types import SimpleNamespacepath = tmp_path / "adapter"path.mkdir()@@ -10061,11 +9193,8 @@ def test_an_adapter_only_directory_is_not_a_servable_chat_model(tmp_path):def test_an_installed_mlx_model_is_indexed_and_resolves(tmp_path, monkeypatch):# Issue #8748: an unloaded MLX model was invisible to the resolver, so a request# naming it 404'd as not downloaded.- import routes.models as models_routefrom utils import paths- from utils.hardware import hardware as hw-path = _local_checkpoint(tmp_path)(path / "config.json").write_text('{"architectures": ["Qwen3ForCausalLM"], "model_type": "qwen3", "quantization": {"group_size": 64, "bits": 4}}'@@ -10098,8 +9227,6 @@ def test_a_family_mlx_lm_remaps_is_listed_in_the_catalog(tmp_path, monkeypatch,# #8748 for Mistral, Devstral, Ministral, Magistral, Codestral and Kimi K2: the# loader applies MODEL_REMAPPING first, so predicting the import from model_type# withheld every quant these ship. The catalog advertises what is on disk.- from types import SimpleNamespace- from utils.hardware import hardware as hwmonkeypatch.setattr(hw, "DEVICE", hw.DeviceType.MLX)path = _local_checkpoint(tmp_path, f"{model_type}-4bit")@@ -10152,9 +9279,8 @@ def test_auto_switch_loads_an_unloaded_mlx_model(monkeypatch):orchestrator.active_model_name = request.model_pathreturn None- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = ("/srv/models/Qwen3-MLX", None, "unsloth/Qwen3-MLX"),backend = llama,recorder = _load,@@ -10190,9 +9316,8 @@ def test_auto_switch_does_not_reload_a_resident_mlx_model(monkeypatch):calls.append(request)return None- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = ("/srv/models/Qwen3-MLX", None, "unsloth/Qwen3-MLX"),backend = llama,recorder = _load,@@ -10237,9 +9362,8 @@ def test_a_gguf_only_endpoint_refuses_a_non_gguf_target_before_loading(monkeypatcalls.append(request)return None- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = ("/srv/models/Qwen3-MLX", None, "unsloth/Qwen3-MLX"),backend = llama,recorder = _load,@@ -10282,9 +9406,8 @@ def test_two_scan_roots_sharing_a_basename_do_not_answer_for_each_other(monkeypaorchestrator.active_model_name = "/root2/model"return None- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = ("/root2/model", None, "model"),backend = llama,recorder = _load,@@ -10317,9 +9440,8 @@ def test_the_resident_path_still_short_circuits_its_own_request(monkeypatch):calls.append(request)return None- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = ("/root1/model", None, "model"),backend = llama,recorder = _load,@@ -10351,9 +9473,8 @@ def test_a_repo_id_resident_still_matches_a_path_request_through_the_alias(monkecalls.append(request)return None- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = ("/cache/snapshots/abc", None, "mlx-community/Qwen3-8B-4bit"),backend = llama,recorder = _load,@@ -10398,7 +9519,6 @@ def test_a_custom_code_checkpoint_is_not_switchable(tmp_path):# Codex P2: an auto_map repo needs trust_remote_code plus the subject's approval# fingerprint, and a switch carries neither, so advertising it guarantees a load# that fails after evicting the resident model.- from types import SimpleNamespacepath = _local_checkpoint(tmp_path, "CustomCode")(path / "config.json").write_text(@@ -10412,7 +9532,6 @@ def test_an_embedding_checkpoint_is_not_switchable(tmp_path):# Codex P2: only the generative chat path consumes non-GGUF entries, and# /v1/embeddings is GGUF-only, so a SentenceTransformer would be loaded as a# language model and fail after the swap evicted the resident model.- from types import SimpleNamespacepath = _local_checkpoint(tmp_path, "bge-small")info = SimpleNamespace(id = str(path), path = str(path))@@ -10450,7 +9569,6 @@ def test_streaming_responses_refuses_a_non_gguf_swap(monkeypatch):def test_a_pickle_checkpoint_is_not_switchable(tmp_path):# Codex P2: .bin weights are pickle-backed, so an API request must not be able to# load one without an explicit load action.- from types import SimpleNamespacepath = _local_checkpoint(tmp_path, "PickleOnly")(path / "model.safetensors").unlink()@@ -10462,7 +9580,6 @@ def test_a_pickle_checkpoint_is_not_switchable(tmp_path):def test_auto_map_in_a_processor_config_is_not_switchable(tmp_path):# Codex P2: trust_remote_code runs auto_map from any of the scanner's config# files, not just config.json, and a switch carries no approval fingerprint.- from types import SimpleNamespacefrom utils.security.remote_code_scan import REMOTE_CODE_CONFIG_FILESfor name in REMOTE_CODE_CONFIG_FILES:path = _local_checkpoint(tmp_path, f"custom-{name}")@@ -10476,7 +9593,6 @@ def test_auto_map_in_a_processor_config_is_not_switchable(tmp_path):def test_a_non_generative_checkpoint_is_not_switchable(tmp_path):# Codex P2: chat generation is the only route these entries feed, so an# encoder-only or classifier checkpoint would fail in the language-model load.- from types import SimpleNamespacepath = _local_checkpoint(tmp_path, "bert-base")(path / "config.json").write_text('{"architectures": ["BertForSequenceClassification"]}')@@ -10487,7 +9603,6 @@ def test_a_non_generative_checkpoint_is_not_switchable(tmp_path):def test_a_lora_directory_with_a_copied_config_is_not_switchable(tmp_path):# Codex P2: ModelConfig resolves an adapter's base_model_name_or_path, so the# switch could fetch weights this resolver promises never to download.- from types import SimpleNamespacepath = _local_checkpoint(tmp_path, "SomeLoRA")info = SimpleNamespace(id = str(path), path = str(path))@@ -10527,7 +9642,6 @@ def test_a_nested_non_gguf_row_is_not_marked_resident(monkeypatch):def test_a_text_seq2seq_checkpoint_is_not_switchable(tmp_path):# Codex P2: ForConditionalGeneration is overloaded. T5 and BART wear it, and the# serving path has no seq2seq branch, so only a multimodal sub-config qualifies.- from types import SimpleNamespacepath = _local_checkpoint(tmp_path, "t5-base")info = SimpleNamespace(id = str(path), path = str(path))@@ -10546,8 +9660,6 @@ def test_a_whisper_checkpoint_is_switchable(tmp_path):the audio model rather than wearing one, so requiring that key filtered it out with T5.The orchestrator loads it through WhisperForConditionalGeneration and chat routes therequest to generate_whisper_response, so it is a target this server actually serves."""- from types import SimpleNamespace-path = _local_checkpoint(tmp_path, "whisper-large-v3")info = SimpleNamespace(id = str(path), path = str(path))(path / "config.json").write_text(@@ -10570,8 +9682,6 @@ def test_a_whisper_checkpoint_is_switchable(tmp_path):def test_unsupported_conditional_audio_checkpoints_are_not_switchable(tmp_path, architecture, model_type):- from types import SimpleNamespace-path = _local_checkpoint(tmp_path, model_type)info = SimpleNamespace(id = str(path), path = str(path))(path / "config.json").write_text(@@ -10584,8 +9694,6 @@ def test_an_mlx_host_does_not_advertise_an_asr_checkpoint(tmp_path, monkeypatch)"""The MLX worker refuses ASR outright at worker.py:823 and TTS at 1103, so a Whispercheckpoint on Apple Silicon can only be loaded to fail. Same host-capability rule as_host_has_a_non_gguf_backend, not a prediction about the load."""- from types import SimpleNamespace-path = _local_checkpoint(tmp_path, "whisper-large-v3")info = SimpleNamespace(id = str(path), path = str(path))(path / "config.json").write_text(@@ -10606,8 +9714,6 @@ def test_an_empty_auto_map_is_not_remote_code(tmp_path):utils/security/consent.py), so an empty or null mapping names no implementation andexecutes nothing. Rejecting on the key's presence alone hid a loadable checkpointwhile adding no approval boundary."""- from types import SimpleNamespace-path = _local_checkpoint(tmp_path, "EmptyAutoMap")info = SimpleNamespace(id = str(path), path = str(path))base = '{"architectures": ["Qwen3ForCausalLM"], "model_type": "qwen3", "auto_map": %s}'@@ -10622,7 +9728,6 @@ def test_an_empty_auto_map_is_not_remote_code(tmp_path):def test_a_host_without_a_non_gguf_backend_advertises_none(tmp_path, monkeypatch):# Codex P2: with neither torch nor MLX the worker has nothing to load safetensors# with, and _load_model_impl unloads the resident GGUF before that fails.- from types import SimpleNamespacepath = _local_checkpoint(tmp_path, "NoBackend")info = SimpleNamespace(id = str(path), path = str(path))@@ -10648,7 +9753,6 @@ def test_a_nested_row_is_not_resident_against_a_loaded_gguf_directory(monkeypatcdef test_a_non_canonical_safetensors_name_is_not_switchable(tmp_path):# Codex P2: the loader receives no variant, so model.fp16.safetensors or a stray# optimizer.safetensors is not weights it can open.- from types import SimpleNamespacepath = _local_checkpoint(tmp_path, "VariantOnly")(path / "model.safetensors").rename(path / "model.fp16.safetensors")@@ -10658,7 +9762,6 @@ def test_a_non_canonical_safetensors_name_is_not_switchable(tmp_path):def test_a_causal_model_without_the_suffix_stays_switchable(tmp_path):# the loader selects GPT2LMHeadModel too, so requiring ForCausalLM alone hid working models.- from types import SimpleNamespacepath = _local_checkpoint(tmp_path, "gpt2")info = SimpleNamespace(id = str(path), path = str(path))@@ -10670,7 +9773,6 @@ def test_a_causal_model_without_the_suffix_stays_switchable(tmp_path):def test_a_model_type_without_architectures_is_not_switchable(tmp_path):# the causal mapping holds bert and bart, so model_type alone stays withheld either way.- from types import SimpleNamespacepath = _local_checkpoint(tmp_path, "no-arch")info = SimpleNamespace(id = str(path), path = str(path))@@ -10689,7 +9791,6 @@ def test_the_advertised_alias_is_cleared_before_a_replacement_load(tmp_path):# Codex P1: load_model publishes active_model_name for the new weights, so an alias# cleared afterwards leaves a window where a request for the old model matches it# and is answered by the new ones. The clear must precede the load call.- import inspectsrc = inspect.getsource(inference_route._load_model_impl)# Anchored on the line start: llama_backend.load_model appears earlier and would@@ -10739,7 +9840,6 @@ def test_a_failed_replacement_keeps_the_surviving_model_advertised():def test_both_failed_load_exits_restore_the_alias():# the raise and the falsy-success exit are separate paths; a fix on one alone leaks.- import inspectsrc = inspect.getsource(inference_route._load_model_impl)assert src.count("_restore_alias_if_failed_load_left_the_prior_model(") == 2@@ -10766,12 +9866,9 @@ def test_a_stale_alias_after_unload_is_not_a_resident_identity(monkeypatch):def test_the_audio_preflight_only_binds_a_non_gguf_target(monkeypatch):- import base64 as _b64- import ioimport wave# _prepare_audio_for_llama takes a data: URI and a continuation.- from fastapi import HTTPExceptionmonkeypatch.setattr(inference_route,@@ -10816,9 +9913,8 @@ def test_a_prior_turn_image_does_not_block_a_non_gguf_audio_switch(monkeypatch):calls.append(request)orchestrator.active_model_name = request.model_path- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = ("/srv/models/Whisper", None, "org/Whisper"),backend = llama,recorder = _load,@@ -10866,8 +9962,6 @@ def test_a_prior_turn_image_does_not_block_a_non_gguf_audio_switch(monkeypatch):)def test_invalid_non_gguf_images_are_rejected_before_switch(monkeypatch, image_preflight):if image_preflight == "truncated image":- import base64 as _b64- import iofrom PIL import Imageencoded = io.BytesIO()@@ -10889,9 +9983,8 @@ def test_invalid_non_gguf_images_are_rejected_before_switch(monkeypatch, image_pcalls.append(request)orchestrator.active_model_name = request.model_path- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = ("/srv/models/Vision", None, "org/Vision"),backend = llama,recorder = _load,@@ -10920,9 +10013,8 @@ def test_invalid_non_gguf_images_are_rejected_before_switch(monkeypatch, image_pdef test_a_malformed_gguf_image_is_rejected_before_switch(monkeypatch):llama = _FakeBackend("org/A-GGUF")recorder = _LoadRecorder(llama)- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = ("/srv/models/B.gguf", "Q4_K_M", "org/B-GGUF"),backend = llama,recorder = recorder,@@ -10952,8 +10044,6 @@ def test_a_malformed_gguf_image_is_rejected_before_switch(monkeypatch):def test_gguf_image_preflight_allows_multiple_valid_images():- import base64 as _b64- import iofrom PIL import Imageencoded = io.BytesIO()@@ -10969,7 +10059,6 @@ def test_gguf_image_preflight_allows_multiple_valid_images():def test_the_image_preflight_collects_every_local_request_image():- from models.inference import ChatMessage, ImageContentPart, ImageUrlpayload = _chat_request(image_base64 = "legacy",messages = [@@ -11004,9 +10093,8 @@ def _wire_image_switch_target(monkeypatch, *, target_is_gguf):backend = _FakeBackend("org/A-GGUF")recorder = _LoadRecorder(backend)target_path = "/srv/models/B.gguf" if target_is_gguf else "/srv/models/B"- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = (target_path, None, "org/B-GGUF"),backend = backend,recorder = recorder,@@ -11032,8 +10120,6 @@ def _wire_image_switch_target(monkeypatch, *, target_is_gguf):ids = ["empty data url", "remote url"],)def test_chat_rejects_unsupported_openai_images_before_non_gguf_switch(monkeypatch, url, detail):- from models.inference import ChatMessage, ImageContentPart, ImageUrl-backend, recorder = _wire_image_switch_target(monkeypatch, target_is_gguf = False)payload = _chat_request(model = "org/B-GGUF",@@ -11060,7 +10146,6 @@ def test_chat_rejects_unsupported_openai_images_before_non_gguf_switch(monkeypatdef _responses_image_payload(*images, stream):- from models.inference import ResponsesRequestreturn ResponsesRequest(model = "org/B-GGUF",stream = stream,@@ -11203,9 +10288,8 @@ def test_mixed_audio_and_image_is_rejected_before_a_non_gguf_switch(monkeypatch)models: dict = {}recorder = _LoadRecorder(llama)- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = ("/srv/models/Audio-VLM", None, "org/Audio-VLM"),backend = llama,recorder = recorder,@@ -11244,12 +10328,8 @@ def test_the_gguf_audio_preflight_takes_the_base64_llama_cpp_takes():That helper decodes with the lenient default, which drops whitespace, so avalidate = True preflight 400'd MIME-wrapped and newline-padded uploads the sameserver served before the preflight existed."""- import base64 as _b64- import ioimport wave- from fastapi import HTTPException-wav = io.BytesIO()with wave.open(wav, "wb") as audio:audio.setnchannels(1)@@ -11285,13 +10365,10 @@ def test_the_gguf_audio_preflight_takes_the_base64_llama_cpp_takes():def test_non_audio_bytes_are_rejected_before_a_gguf_switch(monkeypatch):- import base64 as _b64-llama = _FakeBackend("org/A-GGUF")recorder = _LoadRecorder(llama)- _wire(+ _wire_on(monkeypatch,- enabled = True,resolves_to = ("/srv/models/B.gguf", "Q4_K_M", "org/B-GGUF"),backend = llama,recorder = recorder,@@ -11320,7 +10397,6 @@ def test_non_audio_bytes_are_rejected_before_a_gguf_switch(monkeypatch):def test_a_non_gguf_audio_target_is_refused_without_a_decoder(monkeypatch):# on a torch-free host the swap used to unload the resident model, then fail importing torchaudio.- from fastapi import HTTPExceptionmonkeypatch.setattr(inference_route, "_audio_decoder_is_available", lambda: False)with pytest.raises(HTTPException) as exc:@@ -11335,7 +10411,6 @@ def test_a_non_gguf_audio_target_is_refused_without_a_decoder(monkeypatch):def test_non_audio_bytes_are_rejected_before_a_non_gguf_switch(monkeypatch):# non-audio bytes are a deterministic 400, and the decoded array is reused downstream.- from fastapi import HTTPExceptionmonkeypatch.setattr(inference_route, "_audio_decoder_is_available", lambda: True)@@ -11383,7 +10458,6 @@ def test_a_gguf_only_host_does_not_need_torchaudio_to_accept_audio(monkeypatch):def test_a_host_with_no_accelerator_serves_gguf_only(monkeypatch):# codex P2: unsloth's get_device_type raises without a GPU, so a CPU wheel proves nothing.- from utils.hardware import hardware as hwmonkeypatch.setattr(resolver, "_host_serves_mlx", lambda: False)monkeypatch.setattr(hw, "DEVICE", hw.DeviceType.CPU, raising = False)@@ -11394,7 +10468,6 @@ def test_a_host_with_no_accelerator_serves_gguf_only(monkeypatch):def test_a_serving_hf_cache_row_is_reported_loaded(tmp_path, monkeypatch):# the scanner lists the models--* dir but the orchestrator records the snapshot it loaded.- from types import SimpleNamespacerepo = tmp_path / "models--org--Chat"snapshot = repo / "snapshots" / "abc"diff --git a/studio/backend/tests/test_openai_catalog.py b/studio/backend/tests/test_openai_catalog.pyindex 3574302a378..dbd9c3948ea 100644--- a/studio/backend/tests/test_openai_catalog.py+++ b/studio/backend/tests/test_openai_catalog.py@@ -14,6 +14,7 @@ if str(_BACKEND) not in sys.path:import routes.inference as inf # noqa: E402from core.inference import local_model_resolver as resolver # noqa: E402+import routes.models as models_modclass _Info:@@ -202,7 +203,6 @@ def test_empty_and_errored_scans_are_cached(monkeypatch):# Cache validity is keyed on the timestamp, not list contents, so an empty# (fresh install / no local models) or errored scan is still cached for the# TTL instead of rescanning the filesystem on every /v1/models poll.- import routes.models as models_modfor outcome in ("empty", "error"):calls = {"n": 0}@@ -227,7 +227,6 @@ def test_catalog_ttl_starts_after_scan_completes(monkeypatch):# The cache timestamp must be taken AFTER the scan, not before it. A scan that# outlives the TTL would otherwise leave the cache born-expired, so the next# caller rescans instead of reusing the just-computed catalog.- import routes.models as models_modclock = {"t": 1000.0}monkeypatch.setattr(inf.time, "monotonic", lambda: clock["t"])@@ -277,8 +276,6 @@ def test_cached_local_catalog_offloads_and_caches(monkeypatch):calls["scan"] += 1return [_Info("/data/models/A.gguf", "A")]- import routes.models as models_mod-monkeypatch.setattr(models_mod, "collect_local_models", _fake_collect)real_to_thread = inf.asyncio.to_threaddiff --git a/studio/backend/tests/test_openai_code_execution.py b/studio/backend/tests/test_openai_code_execution.pyindex 63e94613ed1..387176aa36f 100644--- a/studio/backend/tests/test_openai_code_execution.py+++ b/studio/backend/tests/test_openai_code_execution.py@@ -19,6 +19,23 @@ from core.inference import external_provider as ep_modfrom core.inference.external_provider import ExternalProviderClient+async def run():+ client = _make_client()+ return await _collect(+ client._stream_openai_responses(+ messages = [{"role": "user", "content": "hi"}],+ model = "gpt-5.5",+ temperature = 0.7,+ top_p = 0.95,+ max_tokens = 4096,+ enable_thinking = None,+ reasoning_effort = None,+ enabled_tools = ["code_execution"],+ openai_code_exec_container_id = "cntr_stale_999",+ )+ )++def _drive(coro):return asyncio.new_event_loop().run_until_complete(coro)@@ -350,22 +367,6 @@ def test_stale_container_emits_invalidated(monkeypatch):_mock_http_client(monkeypatch, handler)- async def run():- client = _make_client()- return await _collect(- client._stream_openai_responses(- messages = [{"role": "user", "content": "hi"}],- model = "gpt-5.5",- temperature = 0.7,- top_p = 0.95,- max_tokens = 4096,- enable_thinking = None,- reasoning_effort = None,- enabled_tools = ["code_execution"],- openai_code_exec_container_id = "cntr_stale_999",- )- )-lines = _drive(run())events = _tool_events(lines)invalidated = [e for e in events if e["type"] == "container_invalidated"]@@ -420,22 +421,6 @@ def test_expired_container_triggers_transparent_retry(monkeypatch):_mock_http_client(monkeypatch, handler)- async def run():- client = _make_client()- return await _collect(- client._stream_openai_responses(- messages = [{"role": "user", "content": "hi"}],- model = "gpt-5.5",- temperature = 0.7,- top_p = 0.95,- max_tokens = 4096,- enable_thinking = None,- reasoning_effort = None,- enabled_tools = ["code_execution"],- openai_code_exec_container_id = "cntr_stale_999",- )- )-lines = _drive(run())events = _tool_events(lines)@@ -488,22 +473,6 @@ def test_expired_container_retries_only_once(monkeypatch):_mock_http_client(monkeypatch, handler)- async def run():- client = _make_client()- return await _collect(- client._stream_openai_responses(- messages = [{"role": "user", "content": "hi"}],- model = "gpt-5.5",- temperature = 0.7,- top_p = 0.95,- max_tokens = 4096,- enable_thinking = None,- reasoning_effort = None,- enabled_tools = ["code_execution"],- openai_code_exec_container_id = "cntr_stale_999",- )- )-lines = _drive(run())# Exactly two calls (first + one retry); a third would be a loop.diff --git a/studio/backend/tests/test_openai_codex_subscription.py b/studio/backend/tests/test_openai_codex_subscription.pyindex 6cbee8717af..df29d836acb 100644--- a/studio/backend/tests/test_openai_codex_subscription.py+++ b/studio/backend/tests/test_openai_codex_subscription.py@@ -50,6 +50,23 @@ from core.inference.openai_responses_shared import normalize_function_schemafrom core.inference.providers import get_provider_info, list_available_providers+async def _is_disconnected():+ return False+++class AlwaysRejecting:+ async def get(+ self,+ _url,+ headers = None,+ params = None,+ ):+ return httpx.Response(401, json = {"detail": "expired"})++ async def aclose(self):+ return None++def _jwt(payload: dict) -> str:encoded = base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip("=")return f"header.{encoded}.signature"@@ -796,7 +813,6 @@ def test_bare_detail_upstream_error_reaches_the_user():def _models_response(payload, status = 200):- import httpxclass FakeClient:def __init__(self):self.calls = []@@ -1529,9 +1545,6 @@ def _codex_chat_gate(monkeypatch.setattr(codex_auth, "resolve_access", resolve or _refuse)- async def _is_disconnected():- return False-request = SimpleNamespace(headers = {},state = SimpleNamespace(skip_api_monitor = True),@@ -1599,9 +1612,6 @@ def test_codex_chat_receives_the_current_date(monkeypatch):lambda **_kwargs: "The current date is 2026-08-15.",)- async def _is_disconnected():- return False-request = SimpleNamespace(headers = {},state = SimpleNamespace(skip_api_monitor = True),@@ -1698,8 +1708,6 @@ def test_chat_refetches_the_plan_catalog_after_a_restart(monkeypatch):def test_chat_refuses_when_the_catalog_cannot_be_refreshed(monkeypatch):"""An unreachable catalog refuses the model; it does not declare the connection bad."""- import httpx-forget_subscription_models("codex-1")async def _resolve(_provider_id):@@ -1767,9 +1775,6 @@ def test_chat_reads_vision_support_from_the_plan_catalog(monkeypatch):monkeypatch.setattr(codex_auth, "resolve_access", _refuse)- async def _is_disconnected():- return False-def call():request = SimpleNamespace(headers = {},@@ -1888,9 +1893,6 @@ def test_chat_reports_reconnection_when_an_image_needs_the_catalog(monkeypatch):monkeypatch.setattr(codex_auth, "resolve_access", _needs_reauth)- async def _is_disconnected():- return False-payload = ChatCompletionRequest(messages = [{@@ -2058,8 +2060,6 @@ def test_a_superseded_catalog_read_does_not_commit(monkeypatch):Committing late would also clear the mark that says the saved models are unproven,turning a self-correcting state into a sticky one."""- import httpx-forget_subscription_models("provider-9")class Rebinding:@@ -2136,8 +2136,6 @@ def test_a_catalog_401_spends_one_forced_refresh(monkeypatch):The responses transport already spends one forced refresh on that, so the editorshould not be the only path that gives up and demands a reconnect."""- import httpx-forget_subscription_models("provider-11")calls = []@@ -2179,22 +2177,8 @@ def test_a_catalog_401_spends_one_forced_refresh(monkeypatch):def test_a_second_catalog_401_asks_for_reconnection(monkeypatch):"""A refresh that does not help is a real reauthorization, not an endless retry."""- import httpx-forget_subscription_models("provider-12")- class AlwaysRejecting:- async def get(- self,- _url,- headers = None,- params = None,- ):- return httpx.Response(401, json = {"detail": "expired"})-- async def aclose(self):- return None-async def _resolve(_provider_id,force_refresh = False,@@ -2218,8 +2202,6 @@ def test_a_refresh_that_cannot_be_reached_stays_retryable(monkeypatch):rejected, so anything else has to stay transient or the user is sent to reconnect aconnection whose credentials are fine."""- import httpx-forget_subscription_models("provider-13")class Rejecting:@@ -2253,8 +2235,6 @@ def test_a_refresh_that_cannot_be_reached_stays_retryable(monkeypatch):def test_a_rejected_refresh_credential_is_a_reauthorization(monkeypatch):"""The permanent variant still means reconnect."""- import httpx-forget_subscription_models("provider-14")class Rejecting:@@ -2291,8 +2271,6 @@ def test_a_catalog_is_not_committed_for_an_account_another_worker_replaced(monkeA read this worker started is not retired by another worker's rebind, so the storedbundle is the only thing that can say the answer is for the wrong account."""- import httpx-forget_subscription_models("provider-15")class Slow:@@ -2355,8 +2333,6 @@ def test_an_overtaken_read_still_answers_its_own_caller(monkeypatch):Reporting nothing listed would let a manual reload overlapping a chat refuse a modelthe chat's own lookup had just seen."""- import httpx-forget_subscription_models("provider-17")class Overtaken:@@ -2394,8 +2370,6 @@ def test_an_overtaken_read_still_answers_its_own_caller(monkeypatch):def test_an_overtaken_read_is_dropped_when_the_account_moved(monkeypatch):"""If a rebind is what overtook it, its models belong to the previous account."""- import httpx-forget_subscription_models("provider-18")class Overtaken:@@ -2432,8 +2406,6 @@ def test_a_cold_worker_does_not_trust_a_row_it_cannot_vouch_for(monkeypatch):saved = "gpt-5.7-nova"forget_subscription_models("codex-1")- import httpx-calls = []async def _resolve(@@ -2598,23 +2570,9 @@ def test_recording_the_proof_never_overwrites_newer_credentials(monkeypatch):def test_a_second_catalog_401_is_recorded_on_the_connection(monkeypatch):"""Raising alone leaves auth_status saying connected, so nothing offers Reconnect."""- import httpx-forget_subscription_models("provider-22")marked = []- class AlwaysRejecting:- async def get(- self,- _url,- headers = None,- params = None,- ):- return httpx.Response(401, json = {"detail": "expired"})-- async def aclose(self):- return None-async def _resolve(_provider_id,force_refresh = False,@@ -2697,23 +2655,9 @@ def test_the_reauthorization_marker_is_written_under_the_guard(monkeypatch):The streaming error path already takes this guard; the catalog path is the same kindof write and needs the same protection."""- import httpx-forget_subscription_models("provider-24")order = []- class AlwaysRejecting:- async def get(- self,- _url,- headers = None,- params = None,- ):- return httpx.Response(401, json = {"detail": "expired"})-- async def aclose(self):- return None-async def _resolve(_provider_id,force_refresh = False,@@ -2829,7 +2773,6 @@ def test_a_boolean_context_window_is_not_reported_as_a_length(monkeypatch):def _gated_models_client(gate, slug):"""A models endpoint whose response the test releases, not the network."""- import httpxclass Gated:async def get(@@ -2941,8 +2884,6 @@ def test_a_read_started_after_a_release_cannot_be_matched_by_the_older_one(monkedef test_quota_metadata_marks_a_terminal_refusal():"""A 429 is both "wait a moment" and "your plan is spent"; only the flag tells them apart."""- from core.inference import openai_codex_client as codex_client-response = httpx.Response(429,headers = {"retry-after": "30"},@@ -2959,8 +2900,6 @@ def test_quota_metadata_marks_a_terminal_refusal():def test_quota_metadata_falls_back_to_retry_after_ms():"""The client's own backoff already reads retry-after-ms; dropping it here left the delayhonoured on this side of the proxy and guessed at on the other."""- from core.inference import openai_codex_client as codex_client-request = httpx.Request("POST", "https://chatgpt.com/backend-api/codex/responses")ms_only = httpx.Response(429, headers = {"retry-after-ms": "30000"}, request = request)assert codex_client._quota_metadata(ms_only) == {"retry_after": "30.0"}@@ -2976,8 +2915,6 @@ def test_quota_metadata_falls_back_to_retry_after_ms():def _quota_error_for(monkeypatch, body):"""Drive the real send/classify loop against one 429 and return the CodexQuotaError."""- from core.inference import openai_codex_client as codex_client-request = httpx.Request("POST", "https://chatgpt.com/backend-api/codex/responses")async def _no_pause(delay, cancel_event):@@ -3033,7 +2970,6 @@ def test_upstream_error_code_survives_a_body_that_cannot_be_read():"""Parsing a body whose read failed raises StreamError, which is not an HTTPError. Theclassification runs after a failed read, so an uncaught one would replace the quota errorthe caller is supposed to see."""- from core.inference import openai_codex_client as codex_clientclass _FailingStream(httpx.AsyncByteStream):async def __aiter__(self):@@ -3054,8 +2990,6 @@ def test_upstream_error_code_survives_a_body_that_cannot_be_read():def test_upstream_error_code_reads_code_then_type():- from core.inference import openai_codex_client as codex_client-request = httpx.Request("POST", "https://chatgpt.com/backend-api/codex/responses")def _code_of(body):@@ -3072,6 +3006,5 @@ def test_upstream_error_code_reads_code_then_type():def test_terminal_quota_detail_is_recognised():- from core.inference import openai_codex_client as codex_clientassert codex_client._is_terminal_quota("You exceeded your current quota (insufficient_quota)")assert not codex_client._is_terminal_quota("Rate limit reached, try again in 30s")diff --git a/studio/backend/tests/test_openai_compaction.py b/studio/backend/tests/test_openai_compaction.pyindex 6fad2c5eaff..7099517c906 100644--- a/studio/backend/tests/test_openai_compaction.py+++ b/studio/backend/tests/test_openai_compaction.py@@ -71,15 +71,20 @@ def _capture(monkeypatch, *, base_url: str, threshold) -> dict:return captured+def _capture_at_threshold(+ *args,+ threshold = 200_000,+ **kwargs,+):+ """_capture at the shared 200k compaction threshold."""+ return _capture(*args, threshold = threshold, **kwargs)++# ── cloud OpenAI carries the compaction field verbatim ──────────────def test_cloud_openai_sets_compaction_block(monkeypatch):- captured = _capture(- monkeypatch,- base_url = "https://api.openai.com/v1",- threshold = 200_000,- )+ captured = _capture_at_threshold(monkeypatch, base_url = "https://api.openai.com/v1")assert captured["body"].get("context_management") == [{"type": "compaction", "compact_threshold": 200_000}]@@ -105,11 +110,7 @@ def test_non_cloud_base_silently_drops_compaction(monkeypatch):# ollama / llama.cpp / "custom" presets collapse to provider="openai"# but lack context_management. Sending the field would 400 them, so it# must NOT appear on the wire.- captured = _capture(- monkeypatch,- base_url = "http://127.0.0.1:11434/v1",- threshold = 200_000,- )+ captured = _capture_at_threshold(monkeypatch, base_url = "http://127.0.0.1:11434/v1")assert "context_management" not in captured["body"]@@ -121,10 +122,9 @@ def test_azure_openai_base_url_carries_compaction_block(monkeypatch):# (context_management, prompt_cache_retention, container shell) under# a *.openai.azure.com base URL. Treat it as cloud so the compaction# field reaches the API.- captured = _capture(+ captured = _capture_at_threshold(monkeypatch,base_url = "https://my-resource.openai.azure.com/openai/v1",- threshold = 200_000,)assert captured["body"].get("context_management") == [{"type": "compaction", "compact_threshold": 200_000}@@ -160,11 +160,7 @@ def test_cloud_gate_uses_hostname_not_substring(monkeypatch):"https://attacker.com/.openai.azure.com/v1","https://my-resource.openai.azure.com.attacker.com/openai/v1",]:- captured = _capture(- monkeypatch,- base_url = evil,- threshold = 200_000,- )+ captured = _capture_at_threshold(monkeypatch, base_url = evil)assert "context_management" not in captured["body"], evilassert "prompt_cache_retention" not in captured["body"], evildiff --git a/studio/backend/tests/test_openai_container_crud.py b/studio/backend/tests/test_openai_container_crud.pyindex a6997e18f79..e95f36471f9 100644--- a/studio/backend/tests/test_openai_container_crud.py+++ b/studio/backend/tests/test_openai_container_crud.py@@ -26,6 +26,10 @@ from core.inference import external_provider as ep_modfrom core.inference.external_provider import ExternalProviderClient+class ResolverReached(Exception):+ pass++def _drive(coro):return asyncio.new_event_loop().run_until_complete(coro)@@ -160,11 +164,8 @@ def test_delete_propagates_openai_4xx(monkeypatch):def test_external_chat_route_resolves_saved_provider_key(monkeypatch):- from routes import inference as inf_modfrom models.inference import ChatCompletionRequest-- class ResolverReached(Exception):- pass+ from routes import inference as inf_modmonkeypatch.setattr(inf_mod.providers_db,@@ -193,11 +194,8 @@ def test_external_chat_route_resolves_saved_provider_key(monkeypatch):def test_external_chat_api_keys_cannot_use_saved_provider_key(monkeypatch):- from routes import inference as inf_modfrom models.inference import ChatCompletionRequest-- class ResolverReached(Exception):- pass+ from routes import inference as inf_modmonkeypatch.setattr(inf_mod.providers_db,@@ -231,8 +229,8 @@ def test_external_chat_api_keys_cannot_use_saved_provider_key(monkeypatch):def test_external_chat_explicit_key_honors_edited_target(monkeypatch):- from routes import inference as inf_modfrom models.inference import ChatCompletionRequest+ from routes import inference as inf_modclass ClientReached(Exception):pass@@ -274,8 +272,8 @@ def test_external_chat_explicit_key_honors_edited_target(monkeypatch):def test_container_client_explicit_key_honors_request_target(monkeypatch):- from routes import inference as inf_modfrom models.inference import OpenAIContainerRequest+ from routes import inference as inf_modmonkeypatch.setattr(inf_mod.providers_db,@@ -303,8 +301,8 @@ def test_container_client_explicit_key_honors_request_target(monkeypatch):def test_container_client_uses_saved_provider_key(monkeypatch):- from routes import inference as inf_modfrom models.inference import OpenAIContainerRequest+ from routes import inference as inf_modcalls: list[tuple[str | None, str | None, bool]] = []@@ -345,11 +343,8 @@ def test_container_client_uses_saved_provider_key(monkeypatch):def test_container_route_blocks_saved_keys_for_internal_api_key(monkeypatch):- from routes import inference as inf_modfrom models.inference import OpenAIContainerRequest-- class ResolverReached(Exception):- pass+ from routes import inference as inf_moddef fake_resolve(_body, *, allow_saved_key):raise ResolverReached(allow_saved_key)@@ -372,8 +367,8 @@ def test_container_route_blocks_saved_keys_for_internal_api_key(monkeypatch):def test_container_client_rejects_openai_lookalike_host(monkeypatch):- from routes import inference as inf_modfrom models.inference import OpenAIContainerRequest+ from routes import inference as inf_modmonkeypatch.setattr(inf_mod.providers_db,@@ -398,8 +393,8 @@ def test_list_route_filters_expired_containers(monkeypatch):"""OpenAI keeps containers in /v1/containers with status="expired"after their idle TTL passes — unusable but still listed. The listroute must drop them so the picker shows only usable containers."""- from routes import inference as inf_modfrom models.inference import OpenAIContainerRequest+ from routes import inference as inf_moddef handler(request: httpx.Request) -> httpx.Response:return httpx.Response(diff --git a/studio/backend/tests/test_openai_models_media.py b/studio/backend/tests/test_openai_models_media.pyindex d5073c52486..fa160f46928 100644--- a/studio/backend/tests/test_openai_models_media.py+++ b/studio/backend/tests/test_openai_models_media.py@@ -25,6 +25,7 @@ from core.inference import local_model_resolver as resolver # noqa: E402from core.inference import media_model_index as mmi # noqa: E402from core.inference.media_model_index import MediaModelPick # noqa: E402from utils.api_errors import install_api_error_handlers # noqa: E402+from core.inference import media_localityclass _Info:@@ -64,8 +65,6 @@ class _FakeUnsloth:def _media_index(monkeypatch, picks_by_task):"""Stand in for the media index the generation routes resolve against."""- from core.inference import media_locality-inf._MEDIA_PICK_CACHE.update(at = None, picks = {})monkeypatch.setattr(media_locality, "missing_download_bytes", lambda owner, pick: 0)monkeypatch.setattr(@@ -266,8 +265,6 @@ def test_edit_only_checkpoints_are_not_offered_for_text_to_image(monkeypatch):"""The catalog tags an instruction-editing checkpoint text-to-image, but it ships notxt2img workflow: the switch refuses it with a 400 and a resident one is refused by/v1/images/generations too."""- from core.inference import media_locality-edit = MediaModelPick("org/qwen-image-edit", "/hf/edit")plain = MediaModelPick("org/plain-image", "/hf/plain")monkeypatch.setattr(@@ -356,8 +353,6 @@ def test_only_ids_the_media_resolver_accepts_are_listed(monkeypatch):def test_media_model_with_missing_companions_is_not_advertised(monkeypatch):- from core.inference import media_locality-image = _PICKS["text-to-image"][0]_catalog(monkeypatch, _INFOS, picks = {"text-to-image": [image]})monkeypatch.setattr(diff --git a/studio/backend/tests/test_openai_models_servable_cache_edges.py b/studio/backend/tests/test_openai_models_servable_cache_edges.pyindex c31efd9e085..c5c0041ac6b 100644--- a/studio/backend/tests/test_openai_models_servable_cache_edges.py+++ b/studio/backend/tests/test_openai_models_servable_cache_edges.py@@ -24,6 +24,7 @@ if _BACKEND_DIR not in sys.path:sys.path.insert(0, _BACKEND_DIR)import routes.inference as inf # noqa: E402+from core.inference import local_model_resolver as resolver@pytest.fixture(autouse = True)@@ -238,8 +239,6 @@ def test_a_deleted_model_leaves_the_listing_within_the_catalog_ttl(monkeypatch):cache can stay standing for the rest of its 30s TTL. Keying on the resolvergeneration is what stops the removed model being advertised for that window, whichthe per-request scan used to drop at once."""- from core.inference import local_model_resolver as resolver-catalog = _catalog(2, "d")gone: set[str] = set()@@ -260,8 +259,6 @@ def test_a_deleted_model_leaves_the_listing_within_the_catalog_ttl(monkeypatch):def test_an_additions_only_invalidation_also_refreshes_the_scan(monkeypatch):"""A finished download invalidates additions-only, and a new quant must appearwithout waiting out the catalog TTL."""- from core.inference import local_model_resolver as resolver-catalog = _catalog(1, "q")quants = {"/models/q0": ("Q4_K_M",)}@@ -281,8 +278,6 @@ def test_an_additions_only_invalidation_also_refreshes_the_scan(monkeypatch):def test_an_invalidation_during_a_scan_is_not_stamped_in(monkeypatch):"""The generation is read before the scan, so an invalidation that lands while thescan runs makes the stored entry stale rather than being cached as already seen."""- from core.inference import local_model_resolver as resolver-catalog = _catalog(1, "r")state = {"racing": True}@@ -305,8 +300,6 @@ def test_an_invalidation_during_a_scan_is_not_stamped_in(monkeypatch):def test_the_generation_only_moves_on_invalidation(monkeypatch):"""A quiet process must still get the cache: if the generation drifted on its own,every request would rescan and the fix would undo the performance work."""- from core.inference import local_model_resolver as resolver-before = resolver.index_generation()catalog = _catalog(3, "s")monkeypatch.setattr(@@ -422,8 +415,6 @@ def test_a_generation_bump_while_waiting_for_the_lock_is_not_accepted(monkeypatcdelete lands during that scan. The scanner stamps its entry with the generation itSTARTED with, which is correct, so a waiter comparing against the value it capturedbefore queueing would be handed rows examined before the delete."""- from core.inference import local_model_resolver as resolver-catalog = _catalog(1, "w")scanned = {"n": 0}@@ -485,8 +476,6 @@ def test_the_invalidation_helper_stays_off_the_event_loop():def test_a_hit_is_rejected_when_the_generation_moves_mid_read(monkeypatch):"""Seqlock: generation, entry, generation. A delete completing between the first tworeads was accepted anyway, and the lock-free path is where most requests land."""- from core.inference import local_model_resolver as resolver-catalog = _catalog(1, "s")scanned = {"n": 0}diff --git a/studio/backend/tests/test_openai_responses_translation.py b/studio/backend/tests/test_openai_responses_translation.pyindex f18257c2e90..fbde1c630e2 100644--- a/studio/backend/tests/test_openai_responses_translation.py+++ b/studio/backend/tests/test_openai_responses_translation.py@@ -26,6 +26,23 @@ from core.inference import external_provider as ep_modfrom core.inference.external_provider import ExternalProviderClient+async def run():+ client = _make_client()+ lines = await _collect(+ client._stream_openai_responses(+ messages = [{"role": "user", "content": "hi"}],+ model = "gpt-5.5",+ temperature = 0.7,+ top_p = 0.95,+ max_tokens = None,+ enable_thinking = None,+ reasoning_effort = None,+ )+ )+ await client.close()+ return lines++def _drive(coro):return asyncio.new_event_loop().run_until_complete(coro)@@ -214,22 +231,6 @@ def test_responses_failed_without_details_has_actionable_fallback(monkeypatch):_mock_http_client(monkeypatch, handler)- async def run():- client = _make_client()- lines = await _collect(- client._stream_openai_responses(- messages = [{"role": "user", "content": "hi"}],- model = "gpt-5.5",- temperature = 0.7,- top_p = 0.95,- max_tokens = None,- enable_thinking = None,- reasoning_effort = None,- )- )- await client.close()- return lines-lines = _drive(run())error_line = next(line for line in lines if '"error"' in line)error = json.loads(error_line[len("data:") :].strip())["error"]@@ -300,22 +301,6 @@ def test_responses_sse_translates_to_chat_completions_chunks(monkeypatch):_mock_http_client(monkeypatch, handler)- async def run():- client = _make_client()- lines = await _collect(- client._stream_openai_responses(- messages = [{"role": "user", "content": "hi"}],- model = "gpt-5.5",- temperature = 0.7,- top_p = 0.95,- max_tokens = None,- enable_thinking = None,- reasoning_effort = None,- )- )- await client.close()- return lines-lines = _drive(run())# Keep only data lines for assertion clarity.@@ -840,22 +825,6 @@ def test_responses_reasoning_summary_wrapped_in_think_tags(monkeypatch):_mock_http_client(monkeypatch, handler)- async def run():- client = _make_client()- lines = await _collect(- client._stream_openai_responses(- messages = [{"role": "user", "content": "hi"}],- model = "gpt-5.5",- temperature = 0.7,- top_p = 0.95,- max_tokens = None,- enable_thinking = None,- reasoning_effort = None,- )- )- await client.close()- return lines-lines = _drive(run())data_lines = [line[len("data:") :].strip()diff --git a/studio/backend/tests/test_openai_tool_passthrough.py b/studio/backend/tests/test_openai_tool_passthrough.pyindex 2343008f6ef..d79c8640dc1 100644--- a/studio/backend/tests/test_openai_tool_passthrough.py+++ b/studio/backend/tests/test_openai_tool_passthrough.py@@ -195,6 +195,8 @@ class TestFriendlyUpstreamError:generations drain the shared KV cache. Nothing about the request was toolong, so rewording it as an oversized prompt sends the client compacting avalid conversation instead of retrying."""+ from routes.inference import _anthropic_upstream_error+from routes.inference import _anthropic_upstream_error, _classify_llama_generation_errorfrom core.inference.stream_errors import KV_STARVATION_MESSAGE@@ -262,6 +264,8 @@ class TestFriendlyUpstreamError:"""An exceed_context_size_error body carries n_prompt_tokens/n_ctx even when itsmessage spells out no numbers. These paths are handed the whole body, so thetotals are right there; dropping them sends the client a count-less refusal."""+ from routes.inference import _anthropic_upstream_error+from routes.inference import _anthropic_upstream_error, _oversize_countsbody = (@@ -275,9 +279,10 @@ class TestFriendlyUpstreamError:def test_the_remedy_comes_from_the_fit_not_a_flat_shorten_the_conversation(self):"""When the latest turn or the irreducible floor is what does not fit, compactingthe history cannot make it fit, and a client told to compact just retries."""- from core.inference import context_refusalfrom routes.inference import _anthropic_upstream_error+ from core.inference import context_refusal+body = "the request (214331 tokens) exceeds the available context size (131072 tokens)"try:context_refusal.record_fit(@@ -636,11 +641,12 @@ class TestChatCompletionRequestToolFields:# stream=False and return application/json, never text/event-stream.# Mounts the real router to catch middleware/aliasing regressions;# backends are bypassed via provider_type + a stubbed proxy.+ import routes.inference as inference_route+from fastapi import FastAPIfrom fastapi.responses import JSONResponsefrom fastapi.testclient import TestClient- import routes.inference as inference_routefrom auth.authentication import get_current_subjectcaptured = {}@@ -675,10 +681,11 @@ class TestChatCompletionRequestToolFields:llama_backend,inference_backend = None,):+ import routes.inference as inference_route+from fastapi import FastAPIfrom fastapi.testclient import TestClient- import routes.inference as inference_routefrom auth.authentication import get_current_subjectfrom utils.api_errors import install_api_error_handlers@@ -708,9 +715,6 @@ class TestChatCompletionRequestToolFields:self._make(n = 129)def test_n_rejected_for_external_provider_path(self, monkeypatch):- class _UnusedBackend:- is_loaded = False-client = self._v1_client(monkeypatch, _UnusedBackend())resp = client.post("/v1/chat/completions",@@ -723,9 +727,6 @@ class TestChatCompletionRequestToolFields:self._assert_unsupported_n(resp)def test_confirm_tool_calls_rejected_for_provider_tools(self, monkeypatch):- class _UnusedBackend:- is_loaded = False-client = self._v1_client(monkeypatch, _UnusedBackend())resp = client.post("/v1/chat/completions",@@ -745,10 +746,9 @@ class TestChatCompletionRequestToolFields:assert "only supported for local streaming tools" in body["error"]["message"]def test_confirm_tool_calls_allowed_for_codex_studio_tools(self, monkeypatch):- from routes import inference as inference_route+ import routes.inference as inference_route- class _UnusedBackend:- is_loaded = False+ from routes import inference as inference_routeclient = self._v1_client(monkeypatch, _UnusedBackend())@@ -774,9 +774,6 @@ class TestChatCompletionRequestToolFields:assert resp.json() == {"ok": True}def test_logprobs_rejected_until_supported(self, monkeypatch):- class _UnusedBackend:- is_loaded = False-client = self._v1_client(monkeypatch, _UnusedBackend())resp = client.post("/v1/chat/completions",@@ -789,9 +786,6 @@ class TestChatCompletionRequestToolFields:self._assert_unsupported_param(resp, "logprobs")def test_top_logprobs_rejected_until_supported(self, monkeypatch):- class _UnusedBackend:- is_loaded = False-client = self._v1_client(monkeypatch, _UnusedBackend())resp = client.post("/v1/chat/completions",@@ -841,15 +835,7 @@ class TestChatCompletionRequestToolFields:"/v1/chat/completions",json = {"messages": [{"role": "user", "content": "hi"}],- "tools": [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object"},- },- }- ],+ "tools": _lookup_tools(),"n": 2,},)@@ -933,15 +919,7 @@ class TestChatCompletionRequestToolFields:"/v1/chat/completions",json = {"messages": [{"role": "user", "content": "hi"}],- "tools": [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object"},- },- }- ],+ "tools": _lookup_tools(),},)@@ -958,12 +936,6 @@ class TestChatCompletionRequestToolFields:generating long after the request has been answered with a 400."""import routes.inference as inference_route- class _LlamaOff:- is_loaded = False- supports_tools = False- is_vision = False- context_length = None-class _VisionBackend:active_model_name = "vision-sf"models = {@@ -1020,12 +992,6 @@ class TestChatCompletionRequestToolFields:"""A loaded safetensors backend on the standard path, recording generation."""import routes.inference as inference_route- class _LlamaOff:- is_loaded = False- supports_tools = False- is_vision = False- context_length = None-calls = []class _Backend:@@ -1189,14 +1155,9 @@ class TestChatCompletionRequestToolFields:"""The TTS auto-route returns before the standard image path and speaks thenewest user text, so an attached image is discarded. It is also why thetext-only rejection never sees such a request."""+ import numpy as npimport routes.inference as inference_route- class _LlamaOff:- is_loaded = False- supports_tools = False- is_vision = False- context_length = None-spoken = []class _TtsBackend:@@ -1216,7 +1177,6 @@ class TestChatCompletionRequestToolFields:def generate_audio_response(self, **kwargs):spoken.append(kwargs)- import numpy as npreturn np.zeros(16000, dtype = "float32"), 16000def reset_generation_state(self, cancel_event = None):@@ -1349,6 +1309,7 @@ class TestChatCompletionRequestToolFields:"""The GGUF audio branch returns before every image check, exactly as thesafetensors audio branch does, and speaks the newest user TEXT. An attachedimage went unread and unmentioned."""+ import numpy as npimport routes.inference as inference_routespoken = []@@ -1364,7 +1325,6 @@ class TestChatCompletionRequestToolFields:def generate_audio_response(self, **kwargs):spoken.append(kwargs)- import numpy as npreturn np.zeros(16000, dtype = "float32"), 16000monitor = ApiMonitor(max_entries = 3)@@ -1445,20 +1405,7 @@ class TestChatCompletionRequestToolFields:client, backend = self._standard_vision_client(monkeypatch, monitor, is_vision = False)resp = client.post("/v1/chat/completions",- json = {- "messages": [- {- "role": "user",- "content": [- {"type": "text", "text": "what is this?"},- {- "type": "image_url",- "image_url": {"url": "data:image/png;base64,AAAA"},- },- ],- }- ]- },+ json = {"messages": _image_question_messages()},)assert resp.status_code == 400@@ -1477,15 +1424,8 @@ class TestChatCompletionRequestToolFields:only the flattened messages plus the audio, so an attached image isdiscarded. Any count: one is dropped here as silently as two."""import numpy as np-import routes.inference as inference_route- class _LlamaOff:- is_loaded = False- supports_tools = False- is_vision = False- context_length = None-calls = []class _OmniBackend:@@ -1554,37 +1494,11 @@ class TestChatCompletionRequestToolFields:extension, with no image parts anywhere, means that image for THISrequest. The audio branch cannot forward it, so refuse rather than drop."""import numpy as np-import routes.inference as inference_route- class _LlamaOff:- is_loaded = False- supports_tools = False- is_vision = False- context_length = None-calls = []- class _OmniBackend:- active_model_name = "omni-sf"- models = {- "omni-sf": {- "is_vision": True,- "has_audio_input": True,- "chat_template_info": {"template": "chatml"},- "context_length": 4096,- }- }-- def resize_image(self, image):- return image-- def generate_audio_input_response(self, **kwargs):- calls.append(kwargs)- yield "answered from the audio"-- def reset_generation_state(self, cancel_event = None):- pass+ omni = _omni_backend(calls)monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inference_route, "api_monitor", monitor)@@ -1598,7 +1512,7 @@ class TestChatCompletionRequestToolFields:"_decode_audio_base64",lambda *a, **k: np.zeros(16000, dtype = "float32"),)- client = self._v1_client(monkeypatch, _LlamaOff(), _OmniBackend())+ client = self._v1_client(monkeypatch, _LlamaOff(), omni)resp = client.post("/v1/chat/completions",@@ -1619,37 +1533,11 @@ class TestChatCompletionRequestToolFields:per-turn signal. When an earlier turn carries the image the parts decide,and the voice follow-up must still be answered."""import numpy as np-import routes.inference as inference_route- class _LlamaOff:- is_loaded = False- supports_tools = False- is_vision = False- context_length = None-calls = []- class _OmniBackend:- active_model_name = "omni-sf"- models = {- "omni-sf": {- "is_vision": True,- "has_audio_input": True,- "chat_template_info": {"template": "chatml"},- "context_length": 4096,- }- }-- def resize_image(self, image):- return image-- def generate_audio_input_response(self, **kwargs):- calls.append(kwargs)- yield "answered from the audio"-- def reset_generation_state(self, cancel_event = None):- pass+ omni = _omni_backend(calls)monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inference_route, "api_monitor", monitor)@@ -1663,7 +1551,7 @@ class TestChatCompletionRequestToolFields:"_decode_audio_base64",lambda *a, **k: np.zeros(16000, dtype = "float32"),)- client = self._v1_client(monkeypatch, _LlamaOff(), _OmniBackend())+ client = self._v1_client(monkeypatch, _LlamaOff(), omni)image = {"type": "image_url", "image_url": {"url": "data:image/png;base64,AAAA"}}resp = client.post(@@ -1691,37 +1579,11 @@ class TestChatCompletionRequestToolFields:newest turn stays text-only. Studio's own field is copied out of the threadand byte-matches a part; one matching nothing came with this request."""import numpy as np-import routes.inference as inference_route- class _LlamaOff:- is_loaded = False- supports_tools = False- is_vision = False- context_length = None-calls = []- class _OmniBackend:- active_model_name = "omni-sf"- models = {- "omni-sf": {- "is_vision": True,- "has_audio_input": True,- "chat_template_info": {"template": "chatml"},- "context_length": 4096,- }- }-- def resize_image(self, image):- return image-- def generate_audio_input_response(self, **kwargs):- calls.append(kwargs)- yield "answered from the audio"-- def reset_generation_state(self, cancel_event = None):- pass+ omni = _omni_backend(calls)monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inference_route, "api_monitor", monitor)@@ -1735,7 +1597,7 @@ class TestChatCompletionRequestToolFields:"_decode_audio_base64",lambda *a, **k: np.zeros(16000, dtype = "float32"),)- client = self._v1_client(monkeypatch, _LlamaOff(), _OmniBackend())+ client = self._v1_client(monkeypatch, _LlamaOff(), omni)old = {"type": "image_url", "image_url": {"url": "data:image/png;base64,T0xE"}}resp = client.post(@@ -1761,37 +1623,11 @@ class TestChatCompletionRequestToolFields:so an image on an assistant turn is not something it could be an echo of.Matching any role let an explicitly attached image be discarded here."""import numpy as np-import routes.inference as inference_route- class _LlamaOff:- is_loaded = False- supports_tools = False- is_vision = False- context_length = None-calls = []- class _OmniBackend:- active_model_name = "omni-sf"- models = {- "omni-sf": {- "is_vision": True,- "has_audio_input": True,- "chat_template_info": {"template": "chatml"},- "context_length": 4096,- }- }-- def resize_image(self, image):- return image-- def generate_audio_input_response(self, **kwargs):- calls.append(kwargs)- yield "answered from the audio"-- def reset_generation_state(self, cancel_event = None):- pass+ omni = _omni_backend(calls)monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inference_route, "api_monitor", monitor)@@ -1805,7 +1641,7 @@ class TestChatCompletionRequestToolFields:"_decode_audio_base64",lambda *a, **k: np.zeros(16000, dtype = "float32"),)- client = self._v1_client(monkeypatch, _LlamaOff(), _OmniBackend())+ client = self._v1_client(monkeypatch, _LlamaOff(), omni)generated = {"type": "image_url","image_url": {"url": f"data:image/png;base64,{_BLUE_PNG_B64}"},@@ -1833,37 +1669,11 @@ class TestChatCompletionRequestToolFields:"""The count is scoped to the newest user turn, so asking by voice about apicture attached on an earlier turn keeps working."""import numpy as np-import routes.inference as inference_route- class _LlamaOff:- is_loaded = False- supports_tools = False- is_vision = False- context_length = None-calls = []- class _OmniBackend:- active_model_name = "omni-sf"- models = {- "omni-sf": {- "is_vision": True,- "has_audio_input": True,- "chat_template_info": {"template": "chatml"},- "context_length": 4096,- }- }-- def resize_image(self, image):- return image-- def generate_audio_input_response(self, **kwargs):- calls.append(kwargs)- yield "answered from the audio"-- def reset_generation_state(self, cancel_event = None):- pass+ omni = _omni_backend(calls)monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inference_route, "api_monitor", monitor)@@ -1877,7 +1687,7 @@ class TestChatCompletionRequestToolFields:"_decode_audio_base64",lambda *a, **k: np.zeros(16000, dtype = "float32"),)- client = self._v1_client(monkeypatch, _LlamaOff(), _OmniBackend())+ client = self._v1_client(monkeypatch, _LlamaOff(), omni)image = {"type": "image_url", "image_url": {"url": "data:image/png;base64,AAAA"}}resp = client.post(@@ -1906,20 +1716,7 @@ class TestChatCompletionRequestToolFields:client, backend = self._standard_vision_client(monkeypatch, monitor)resp = client.post("/v1/chat/completions",- json = {- "messages": [- {- "role": "user",- "content": [- {"type": "text", "text": "what is this?"},- {- "type": "image_url",- "image_url": {"url": "data:image/png;base64,AAAA"},- },- ],- }- ]- },+ json = {"messages": _image_question_messages()},)assert resp.status_code == 400@@ -1973,15 +1770,7 @@ class TestChatCompletionRequestToolFields:"/v1/chat/completions",json = {"messages": [{"role": "user", "content": "use client tool"}],- "tools": [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object"},- },- }- ],+ "tools": _lookup_tools(),},)@@ -2001,6 +1790,7 @@ class TestChatCompletionRequestToolFields:# confirm_tool_calls=True still forces the local-confirm rejection.# The pre-switch guard only runs when an automatic load may run, so force# that predicate on to exercise it against a resident passthrough backend.+import routes.inference as inference_routeclass _GGUFBackend:@@ -2024,12 +1814,7 @@ class TestChatCompletionRequestToolFields:inference_route.api_monitor.finish(kwargs.get("monitor_id"))return inference_route.JSONResponse({"ok": True, "model": model_name})- client_tools = [- {- "type": "function",- "function": {"name": "lookup", "parameters": {"type": "object"}},- }- ]+ client_tools = _lookup_tools()def _setup(policy = None):reset_tool_policy()@@ -2102,6 +1887,7 @@ class TestChatCompletionRequestToolFields:# prompt on, so it must 400 at the pre-switch guard -- before# _maybe_auto_switch_model runs -- rather than evicting the resident model# and 400ing only at the per-backend check.+import routes.inference as inference_routeclass _GGUFBackend:@@ -2150,6 +1936,7 @@ class TestChatCompletionRequestToolFields:# DiffusionGemma forces supports_tools off while passthrough stays# available (#6851): enable_tools=True must not steal client tools# from the passthrough into an Unsloth tool loop that cannot run.+import routes.inference as inference_routecaptured = {}@@ -2193,15 +1980,7 @@ class TestChatCompletionRequestToolFields:json = {"messages": [{"role": "user", "content": "use client tool"}],"enable_tools": True,- "tools": [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object"},- },- }- ],+ "tools": _lookup_tools(),},)@@ -2234,15 +2013,7 @@ class TestChatCompletionRequestToolFields:"/v1/chat/completions",json = {"messages": [{"role": "user", "content": "hi"}],- "tools": [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object"},- },- }- ],+ "tools": _lookup_tools(),"tool_choice": "none",},)@@ -3128,12 +2899,8 @@ class TestOpenAICompatibilityHelpers:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monitor_id = monitor.start(- endpoint = "/v1/completions",- method = "POST",- model = "m",- prompt = "hi",- context_length = 100,+ monitor_id = _monitor_entry(+ monitor, endpoint = "/v1/completions", model = "m", context_length = 100)event = (b'data: {"id":"chatcmpl-test","choices":[{"text":"done","finish_reason":"stop"}],'@@ -3254,20 +3021,7 @@ class TestOpenAICompatibilityHelpers:assert image_b64 == "BBBB"def test_single_image_still_returned(self):- payload = ChatCompletionRequest(- messages = [- {- "role": "user",- "content": [- {"type": "text", "text": "what is this?"},- {- "type": "image_url",- "image_url": {"url": "data:image/png;base64,AAAA"},- },- ],- }- ]- )+ payload = ChatCompletionRequest(messages = _image_question_messages())_, _, image_b64 = _extract_content_parts(payload.messages)@@ -3503,11 +3257,204 @@ from routes.inference import ( # noqa: E402)-class TestDropEmptyAssistantSentinels:- def test_drops_empty_assistant_between_real_turns(self):- msgs = [- {"role": "user", "content": "hi"},- {"role": "assistant", "content": ""},+def _image_question_messages():+ """One user turn holding a question and a tiny png."""+ return [+ {+ "role": "user",+ "content": [+ {"type": "text", "text": "what is this?"},+ {+ "type": "image_url",+ "image_url": {"url": "data:image/png;base64,AAAA"},+ },+ ],+ }+ ]+++def _stop_metadata():+ """The terminal usage frame the gguf generators yield."""+ return {+ "type": "metadata",+ "usage": {"prompt_tokens": 3, "completion_tokens": 2, "total_tokens": 5},+ "finish_reason": "stop",+ }+++def _lookup_tools():+ """The one-entry lookup tool list the passthrough tests advertise."""+ return [+ {+ "type": "function",+ "function": {+ "name": "lookup",+ "parameters": {"type": "object"},+ },+ }+ ]+++def _omni_backend(calls):+ """The audio-capable multimodal backend the audio-input tests drive, recording into `calls`."""++ class _OmniBackend:+ active_model_name = "omni-sf"+ models = {+ "omni-sf": {+ "is_vision": True,+ "has_audio_input": True,+ "chat_template_info": {"template": "chatml"},+ "context_length": 4096,+ }+ }++ def resize_image(self, image):+ return image++ def generate_audio_input_response(self, **kwargs):+ calls.append(kwargs)+ yield "answered from the audio"++ def reset_generation_state(self, cancel_event = None):+ pass++ return _OmniBackend()+++def _pin_loaded_backend(monkeypatch, **overrides):+ """Pin get_llama_cpp_backend to a loaded gguf backend at the test base_url."""+ import routes.inference as inf_mod++ fields = {+ "is_loaded": True,+ "base_url": "http://llama.test",+ "context_length": 4096,+ "model_identifier": "gguf",+ }+ fields.update(overrides)+ monkeypatch.setattr(inf_mod, "get_llama_cpp_backend", lambda: SimpleNamespace(**fields))+++def _hi_stream_payload(**overrides):+ """The one-user-message streaming request the passthrough tests send."""+ fields = {+ "model": "default",+ "messages": [ChatMessage(role = "user", content = "hi")],+ "stream": True,+ }+ fields.update(overrides)+ return ChatCompletionRequest(**fields)+++def _drive_passthrough(backend, payload, monitor_id):+ """_openai_passthrough_stream with the connected request and fixed completion id."""+ return _openai_passthrough_stream(+ _ConnectedRequest(),+ threading.Event(),+ backend,+ payload,+ "chatcmpl-test",+ "chatcmpl-test",+ monitor_id = monitor_id,+ )+++class _UnusedBackend:+ is_loaded = False+++class _LlamaOff:+ is_loaded = False+ supports_tools = False+ is_vision = False+ context_length = None+++async def fake_select_tools(*_args, **_kwargs):+ return [+ {+ "type": "function",+ "function": {+ "name": "lookup",+ "parameters": {"type": "object", "properties": {}},+ },+ }+ ]+++async def fake_send(*_args, **_kwargs):+ return httpx.Response(200, content = b"")+++class HangingCancelableClient:+ def __init__(self):+ self.started = asyncio.Event()+ self.closed = asyncio.Event()++ async def post(self, *_args, **_kwargs):+ self.started.set()+ await self.closed.wait()+ raise httpx.ReadError("client closed")++ async def aclose(self):+ self.closed.set()+++class _ConnectedRequest:+ """A request that never reports a disconnect, redefined inline by many tests below."""++ async def is_disconnected(self):+ return False+++_LOOKUP_TOOL = {+ "type": "function",+ "function": {+ "name": "lookup",+ "parameters": {"type": "object", "properties": {}},+ },+}+++def _install_monitor(monkeypatch, max_entries = 3):+ """Fresh ApiMonitor pinned onto the route module, plus one started entry."""+ import routes.inference as inf_mod++ monitor = ApiMonitor(max_entries = max_entries)+ monitor_id = _monitor_entry(monitor)+ monkeypatch.setattr(inf_mod, "api_monitor", monitor)+ return monitor, monitor_id+++def _passthrough_backend(**overrides):+ """The llama backend stand-in _openai_passthrough_stream is driven with."""+ fields = {+ "base_url": "http://llama.test",+ "context_length": 4096,+ "_request_reasoning_kwargs": lambda *_args, **_kwargs: None,+ }+ fields.update(overrides)+ return SimpleNamespace(**fields)+++def _monitor_entry(monitor, **overrides):+ """monitor.start with the chat-completions defaults the tests below repeat verbatim."""+ kwargs = {+ "endpoint": "/v1/chat/completions",+ "method": "POST",+ "model": "gguf",+ "prompt": "hi",+ }+ kwargs.update(overrides)+ return monitor.start(**kwargs)+++class TestDropEmptyAssistantSentinels:+ def test_drops_empty_assistant_between_real_turns(self):+ msgs = [+ {"role": "user", "content": "hi"},+ {"role": "assistant", "content": ""},{"role": "user", "content": "again"},]out = _drop_empty_assistant_sentinels(msgs)@@ -4213,11 +4160,7 @@ class TestGgufVisionToolRouting:with pytest.raises(HTTPException) as exc:self._drive(- openai_chat_completions(- payload,- request = self._Request(),- current_subject = "test",- )+ openai_chat_completions(payload, request = self._Request(), current_subject = "test"))assert exc.value.status_code == 400@@ -4266,11 +4209,7 @@ class TestGgufVisionToolRouting:with pytest.raises(HTTPException) as exc:self._drive(- openai_chat_completions(- payload,- request = self._Request(),- current_subject = "test",- )+ openai_chat_completions(payload, request = self._Request(), current_subject = "test"))assert exc.value.status_code == 400assert "requires stream=true" in exc.value.detail["error"]["message"]@@ -4288,6 +4227,7 @@ class TestGgufVisionToolRouting:def test_streaming_confirm_gate_refuses_a_caller_that_hid_the_frames(self, monkeypatch):# A stream without X-Unsloth-Events has nowhere to be asked, so the loop would park# in wait_tool_decision for the full timeout (_confirm_gate_has_no_channel).+import routes.inference as inf_modreset_tool_policy()@@ -4331,11 +4271,7 @@ class TestGgufVisionToolRouting:# Same request with the frames on runs the loop: the guard refuses no one else.def _tools(**_kwargs):yield {"type": "content", "text": "done"}- yield {- "type": "metadata",- "usage": {"prompt_tokens": 3, "completion_tokens": 2, "total_tokens": 5},- "finish_reason": "stop",- }+ yield _stop_metadata()result = self._run_gguf_case(monkeypatch,@@ -4355,6 +4291,7 @@ class TestGgufVisionToolRouting:# mcp_enabled arms _confirm_gate_needs_stream on intent, but discovery finds no MCP# tool here, so the selection is empty and the loop is skipped. Refusing on intent# would 400 a request that answers fine without ever prompting.+import routes.inference as inf_modreset_tool_policy()@@ -4390,11 +4327,7 @@ class TestGgufVisionToolRouting:yield "<think>plan"yield "<think>plan</think>vis"yield "<think>plan</think>visible"- yield {- "type": "metadata",- "usage": {"prompt_tokens": 3, "completion_tokens": 2, "total_tokens": 5},- "finish_reason": "stop",- }+ yield _stop_metadata()result = self._run_gguf_case(monkeypatch,@@ -4413,9 +4346,8 @@ class TestGgufVisionToolRouting:def test_standard_gguf_stream_queued_request_sends_keepalive_before_generation(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class Request(self._Request):app = SimpleNamespace(state = SimpleNamespace(llama_parallel_slots = 1))@@ -4477,9 +4409,8 @@ class TestGgufVisionToolRouting:asyncio.run(_run())def test_standard_gguf_stream_close_after_first_chunk_cleans_tracker(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-cancel_id = "standard-stream-close-cleanup"def _generate(**_kwargs):@@ -4528,9 +4459,8 @@ class TestGgufVisionToolRouting:def test_standard_gguf_stream_task_cancel_after_first_chunk_finalizes_monitor(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-started = threading.Event()released = threading.Event()@@ -4588,23 +4518,11 @@ class TestGgufVisionToolRouting:asyncio.run(_run())def test_gguf_tool_stream_queued_request_sends_keepalive_before_generation(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class Request(self._Request):app = SimpleNamespace(state = SimpleNamespace(llama_parallel_slots = 1))- async def fake_select_tools(*_args, **_kwargs):- return [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object", "properties": {}},- },- }- ]-def _generate(**_kwargs):raise AssertionError("GGUF tool loop must not start while queued")@@ -4666,9 +4584,8 @@ class TestGgufVisionToolRouting:asyncio.run(_run())def test_gguf_gated_tool_start_gets_a_separate_prompt_flush(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-async def fake_select_tools(*_args, **_kwargs):return [{@@ -4750,20 +4667,8 @@ class TestGgufVisionToolRouting:asyncio.run(_run())def test_gguf_tool_stream_task_cancel_after_first_chunk_finalizes_monitor(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-- async def fake_select_tools(*_args, **_kwargs):- return [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object", "properties": {}},- },- }- ]-started = threading.Event()released = threading.Event()@@ -4873,11 +4778,7 @@ class TestGgufVisionToolRouting:response_format = {"type": "json_object"},)response = self._drive(- openai_chat_completions(- payload,- request = self._Request(),- current_subject = "test",- )+ openai_chat_completions(payload, request = self._Request(), current_subject = "test"))assert json.loads(response.body)["ok"] is True@@ -4946,11 +4847,7 @@ class TestGgufVisionToolRouting:tools = client_tools,)response = self._drive(- openai_chat_completions(- payload,- request = self._Request(),- current_subject = "test",- )+ openai_chat_completions(payload, request = self._Request(), current_subject = "test"))assert json.loads(response.body)["ok"] is True@@ -5006,11 +4903,7 @@ class TestGgufVisionToolRouting:tool_choice = "none",)response = self._drive(- openai_chat_completions(- payload,- request = self._Request(),- current_subject = "test",- )+ openai_chat_completions(payload, request = self._Request(), current_subject = "test"))assert json.loads(response.body)["choices"][0]["message"]["content"] == "plain response"@@ -5068,11 +4961,7 @@ class TestGgufVisionToolRouting:response_format = {"type": "json_object"},)response = self._drive(- openai_chat_completions(- payload,- request = self._Request(),- current_subject = "test",- )+ openai_chat_completions(payload, request = self._Request(), current_subject = "test"))assert json.loads(response.body)["ok"] is True@@ -5132,11 +5021,7 @@ class TestGgufVisionToolRouting:tools = client_tools,)response = self._drive(- openai_chat_completions(- payload,- request = self._Request(),- current_subject = "test",- )+ openai_chat_completions(payload, request = self._Request(), current_subject = "test"))assert json.loads(response.body)["ok"] is True@@ -5146,11 +5031,7 @@ class TestGgufVisionToolRouting:def test_reasoning_capable_gguf_stream_splits_reasoning_by_default(self, monkeypatch):def _generate(**_kwargs):yield "<think>plan</think>visible"- yield {- "type": "metadata",- "usage": {"prompt_tokens": 3, "completion_tokens": 2, "total_tokens": 5},- "finish_reason": "stop",- }+ yield _stop_metadata()result = self._run_gguf_case(monkeypatch,@@ -5168,11 +5049,7 @@ class TestGgufVisionToolRouting:def test_reasoning_capable_gguf_stream_sanitizes_think_tags_when_disabled(self, monkeypatch):def _generate(**_kwargs):yield "<think>leaked</think>visible"- yield {- "type": "metadata",- "usage": {"prompt_tokens": 3, "completion_tokens": 2, "total_tokens": 5},- "finish_reason": "stop",- }+ yield _stop_metadata()result = self._run_gguf_case(monkeypatch,@@ -5194,11 +5071,7 @@ class TestGgufVisionToolRouting:"type": "content","text": '<think>plan</think>visible <|tool_call>call:terminal{command:"ls"}<tool_call|>',}- yield {- "type": "metadata",- "usage": {"prompt_tokens": 3, "completion_tokens": 2, "total_tokens": 5},- "finish_reason": "stop",- }+ yield _stop_metadata()result = self._run_gguf_case(monkeypatch,@@ -5225,11 +5098,7 @@ class TestGgufVisionToolRouting:def _tools(**_kwargs):yield {"type": "content", "text": "answer <"}yield {"type": "status", "text": ""}- yield {- "type": "metadata",- "usage": {"prompt_tokens": 3, "completion_tokens": 2, "total_tokens": 5},- "finish_reason": "stop",- }+ yield _stop_metadata()result = self._run_gguf_case(monkeypatch,@@ -5253,11 +5122,7 @@ class TestGgufVisionToolRouting:def test_non_streaming_gguf_splits_reasoning_content(self, monkeypatch):def _generate(**_kwargs):yield "<think>plan</think>visible"- yield {- "type": "metadata",- "usage": {"prompt_tokens": 3, "completion_tokens": 2, "total_tokens": 5},- "finish_reason": "stop",- }+ yield _stop_metadata()result = self._run_gguf_case(monkeypatch, generate = _generate)body = result.body@@ -5269,9 +5134,8 @@ class TestGgufVisionToolRouting:assert entry["reply"] == "visible"def test_standard_gguf_non_streaming_admission_timeout_before_generation(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class Request(self._Request):app = SimpleNamespace(state = SimpleNamespace(llama_parallel_slots = 1))@@ -5322,9 +5186,8 @@ class TestGgufVisionToolRouting:def test_standard_gguf_non_streaming_cancel_id_stops_queued_request_before_generation(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class Request(self._Request):app = SimpleNamespace(state = SimpleNamespace(llama_parallel_slots = 1))@@ -5393,9 +5256,8 @@ class TestGgufVisionToolRouting:def test_standard_gguf_non_streaming_admission_task_cancel_cleans_tracker_and_slot(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-cancel_id = "standard-nonstream-task-cancel"async def fake_wait(*_args, **_kwargs):@@ -5442,23 +5304,11 @@ class TestGgufVisionToolRouting:asyncio.run(_run())def test_gguf_tool_non_streaming_admission_timeout_before_generation(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class Request(self._Request):app = SimpleNamespace(state = SimpleNamespace(llama_parallel_slots = 1))- async def fake_select_tools(*_args, **_kwargs):- return [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object", "properties": {}},- },- }- ]-def _generate(**_kwargs):raise AssertionError("GGUF tool loop must not start while queued")@@ -5507,20 +5357,8 @@ class TestGgufVisionToolRouting:asyncio.run(_run())def test_gguf_tool_non_streaming_cancel_drains_worker_before_releasing_slot(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-- async def fake_select_tools(*_args, **_kwargs):- return [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object", "properties": {}},- },- }- ]-started = threading.Event()released = threading.Event()@@ -5555,11 +5393,7 @@ class TestGgufVisionToolRouting:enable_tools = True,)task = asyncio.create_task(- openai_chat_completions(- payload,- request = self._Request(),- current_subject = "test",- )+ openai_chat_completions(payload, request = self._Request(), current_subject = "test"))# Generous budgets. What this test asserts is that cancelling the# request drains the worker, and none of the numbers below are part@@ -5636,11 +5470,7 @@ class TestGgufVisionToolRouting:)response = self._drive(- openai_chat_completions(- payload,- request = self._Request(),- current_subject = "test",- )+ openai_chat_completions(payload, request = self._Request(), current_subject = "test"))body = json.loads(response.body)@@ -5699,9 +5529,8 @@ class TestGgufVisionToolRouting:_DRAIN_BUDGET_S = 30.0def test_non_streaming_gguf_cancel_drains_worker(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-started = threading.Event()released = threading.Event()@@ -5731,11 +5560,7 @@ class TestGgufVisionToolRouting:messages = [{"role": "user", "content": "hi"}],)task = asyncio.create_task(- openai_chat_completions(- payload,- request = self._Request(),- current_subject = "test",- )+ openai_chat_completions(payload, request = self._Request(), current_subject = "test"))assert await asyncio.to_thread(started.wait, 1.0)@@ -5883,10 +5708,6 @@ class TestApiMonitorProviderAndCompletionStreams:):import routes.inference as inf_mod- class Request:- async def is_disconnected(self):- return False-upstream_bodies = []async def fake_send(_client, built_request, *_args, **_kwargs):@@ -5901,36 +5722,19 @@ class TestApiMonitorProviderAndCompletionStreams:monkeypatch.setattr(inf_mod, "api_monitor", monitor)monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)monkeypatch.setattr(inf_mod, "_aiter_llama_stream_items", fake_items)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)payload = ChatCompletionRequest(model = "default",messages = [ChatMessage(role = "user", content = "hi")],stream = True,stream_options = stream_options,- tools = [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object", "properties": {}},- },- }- ],+ tools = [_LOOKUP_TOOL],)response = await _openai_passthrough_stream(- Request(),+ _ConnectedRequest(),threading.Event(),- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),+ _passthrough_backend(),payload,"gguf","chatcmpl-test",@@ -6010,49 +5814,21 @@ class TestApiMonitorProviderAndCompletionStreams:return framesdef test_passthrough_stream_preheader_dispatched_with_timeout(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-gate = asyncio.Event()async def fake_send(*_args, **_kwargs):await gate.wait()return httpx.Response(200, content = b"")- class Request:- async def is_disconnected(self):- return False-- monitor = ApiMonitor(max_entries = 3)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )- monkeypatch.setattr(inf_mod, "api_monitor", monitor)+ monitor, monitor_id = _install_monitor(monkeypatch)monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)- payload = ChatCompletionRequest(- model = "default",- messages = [ChatMessage(role = "user", content = "hi")],- stream = True,- )+ payload = _hi_stream_payload()response = await asyncio.wait_for(- _openai_passthrough_stream(- Request(),- threading.Event(),- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),- payload,- "chatcmpl-test",- "chatcmpl-test",- monitor_id = monitor_id,- ),+ _drive_passthrough(_passthrough_backend(), payload, monitor_id),timeout = 5.0,)assert isinstance(response, _SameTaskStreamingResponse)@@ -6067,52 +5843,27 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_stream_forwards_backend_auth_headers(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-captured_headers = {}async def fake_send(_client, req, *_args, **_kwargs):captured_headers.update(dict(req.headers))return httpx.Response(200, content = b"")- class Request:- async def is_disconnected(self):- return False-- monitor = ApiMonitor(max_entries = 3)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )- monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)+ monitor, monitor_id = _install_monitor(monkeypatch)+ monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)payload = ChatCompletionRequest(model = "default",messages = [ChatMessage(role = "user", content = "hi")],stream = True,- tools = [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object", "properties": {}},- },- }- ],+ tools = [_LOOKUP_TOOL],)response = await _openai_passthrough_stream(- Request(),+ _ConnectedRequest(),threading.Event(),- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- _auth_headers = {"Authorization": "Bearer secret"},- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),+ _passthrough_backend(_auth_headers = {"Authorization": "Bearer secret"}),payload,"chatcmpl-test","chatcmpl-test",@@ -6130,27 +5881,15 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_stream_keepalive_while_upstream_headers_are_pending(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-gate = asyncio.Event()async def fake_send(*_args, **_kwargs):await gate.wait()return httpx.Response(200, content = b"")- class Request:- async def is_disconnected(self):- return False-- monitor = ApiMonitor(max_entries = 3)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )- monkeypatch.setattr(inf_mod, "api_monitor", monitor)+ monitor, monitor_id = _install_monitor(monkeypatch)monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)monkeypatch.setattr(inf_mod,@@ -6158,26 +5897,10 @@ class TestApiMonitorProviderAndCompletionStreams:0.01,)- payload = ChatCompletionRequest(- model = "default",- messages = [ChatMessage(role = "user", content = "hi")],- stream = True,- )+ payload = _hi_stream_payload()response = await asyncio.wait_for(- _openai_passthrough_stream(- Request(),- threading.Event(),- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),- payload,- "chatcmpl-test",- "chatcmpl-test",- monitor_id = monitor_id,- ),+ _drive_passthrough(_passthrough_backend(), payload, monitor_id),timeout = 5.0,)@@ -6195,40 +5918,20 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_stream_preheader_non_200_in_window(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-async def fake_send(*_args, **_kwargs):return httpx.Response(400, content = b'{"error":"bad"}')- class Request:- async def is_disconnected(self):- return False-- monitor = ApiMonitor(max_entries = 3)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )- monkeypatch.setattr(inf_mod, "api_monitor", monitor)+ monitor, monitor_id = _install_monitor(monkeypatch)monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)- payload = ChatCompletionRequest(- model = "default",- messages = [ChatMessage(role = "user", content = "hi")],- stream = True,- )+ payload = _hi_stream_payload()with pytest.raises(HTTPException) as exc:await _openai_passthrough_stream(- Request(),+ _ConnectedRequest(),threading.Event(),- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),+ _passthrough_backend(),payload,"chatcmpl-test","chatcmpl-test",@@ -6239,40 +5942,20 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_stream_preheader_request_error_in_window(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-async def fake_send(*_args, **_kwargs):raise httpx.ConnectError("connectivity issue")- class Request:- async def is_disconnected(self):- return False-- monitor = ApiMonitor(max_entries = 3)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )- monkeypatch.setattr(inf_mod, "api_monitor", monitor)+ monitor, monitor_id = _install_monitor(monkeypatch)monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)- payload = ChatCompletionRequest(- model = "default",- messages = [ChatMessage(role = "user", content = "hi")],- stream = True,- )+ payload = _hi_stream_payload()with pytest.raises(HTTPException) as exc:await _openai_passthrough_stream(- Request(),+ _ConnectedRequest(),threading.Event(),- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),+ _passthrough_backend(),payload,"chatcmpl-test","chatcmpl-test",@@ -6283,48 +5966,20 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_stream_preheader_delayed_non_200_returns_sse_error(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-gate = asyncio.Event()async def fake_send(*_args, **_kwargs):await gate.wait()return httpx.Response(400, content = b'{"error":"bad"}')- class Request:- async def is_disconnected(self):- return False-- monitor = ApiMonitor(max_entries = 3)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )- monkeypatch.setattr(inf_mod, "api_monitor", monitor)+ monitor, monitor_id = _install_monitor(monkeypatch)monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)- payload = ChatCompletionRequest(- model = "default",- messages = [ChatMessage(role = "user", content = "hi")],- stream = True,- )+ payload = _hi_stream_payload()response = await asyncio.wait_for(- _openai_passthrough_stream(- Request(),- threading.Event(),- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),- payload,- "chatcmpl-test",- "chatcmpl-test",- monitor_id = monitor_id,- ),+ _drive_passthrough(_passthrough_backend(), payload, monitor_id),timeout = 5.0,)assert isinstance(response, _SameTaskStreamingResponse)@@ -6346,9 +6001,8 @@ class TestApiMonitorProviderAndCompletionStreams:def test_passthrough_stream_preheader_delayed_context_error_keeps_error_envelope(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-gate = asyncio.Event()ctx_msg = "request (4096 tokens) exceeds the available context size (2048 tokens)"@@ -6356,28 +6010,13 @@ class TestApiMonitorProviderAndCompletionStreams:await gate.wait()return httpx.Response(400, content = ctx_msg.encode("utf-8"))- class Request:- async def is_disconnected(self):- return False-- monitor = ApiMonitor(max_entries = 3)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )- monkeypatch.setattr(inf_mod, "api_monitor", monitor)+ monitor, monitor_id = _install_monitor(monkeypatch)monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)- payload = ChatCompletionRequest(- model = "default",- messages = [ChatMessage(role = "user", content = "hi")],- stream = True,- )+ payload = _hi_stream_payload()response = await asyncio.wait_for(_openai_passthrough_stream(- Request(),+ _ConnectedRequest(),threading.Event(),SimpleNamespace(base_url = "http://llama.test",@@ -6415,9 +6054,8 @@ class TestApiMonitorProviderAndCompletionStreams:def test_passthrough_stream_preheader_delayed_context_error_retries_truncation(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-gate = asyncio.Event()calls = []err_body = json.dumps(@@ -6437,18 +6075,7 @@ class TestApiMonitorProviderAndCompletionStreams:return httpx.Response(400, content = err_body)return httpx.Response(200, content = b"")- class Request:- async def is_disconnected(self):- return False-- monitor = ApiMonitor(max_entries = 3)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )- monkeypatch.setattr(inf_mod, "api_monitor", monitor)+ monitor, monitor_id = _install_monitor(monkeypatch)monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)messages = [@@ -6466,7 +6093,7 @@ class TestApiMonitorProviderAndCompletionStreams:)response = await asyncio.wait_for(_openai_passthrough_stream(- Request(),+ _ConnectedRequest(),threading.Event(),SimpleNamespace(base_url = "http://llama.test",@@ -6498,9 +6125,8 @@ class TestApiMonitorProviderAndCompletionStreams:def test_passthrough_stream_preheader_immediate_context_retry_adopts_delayed_response(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-gate = asyncio.Event()calls = []err_body = json.dumps(@@ -6532,18 +6158,7 @@ class TestApiMonitorProviderAndCompletionStreams:for line in ok_lines:yield line- class Request:- async def is_disconnected(self):- return False-- monitor = ApiMonitor(max_entries = 3)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )- monkeypatch.setattr(inf_mod, "api_monitor", monitor)+ monitor, monitor_id = _install_monitor(monkeypatch)monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)monkeypatch.setattr(inf_mod, "_aiter_llama_stream_items", fake_items)@@ -6562,7 +6177,7 @@ class TestApiMonitorProviderAndCompletionStreams:)response = await asyncio.wait_for(_openai_passthrough_stream(- Request(),+ _ConnectedRequest(),threading.Event(),SimpleNamespace(base_url = "http://llama.test",@@ -6595,9 +6210,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_stream_preheader_delayed_request_error_cleans_up(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-gate = asyncio.Event()cancel_id = "delayed-request-error-cancel"@@ -6605,18 +6219,7 @@ class TestApiMonitorProviderAndCompletionStreams:await gate.wait()raise httpx.ConnectError("delayed connectivity issue")- class Request:- async def is_disconnected(self):- return False-- monitor = ApiMonitor(max_entries = 3)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )- monkeypatch.setattr(inf_mod, "api_monitor", monitor)+ monitor, monitor_id = _install_monitor(monkeypatch)monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)payload = ChatCompletionRequest(@@ -6626,19 +6229,7 @@ class TestApiMonitorProviderAndCompletionStreams:cancel_id = cancel_id,)response = await asyncio.wait_for(- _openai_passthrough_stream(- Request(),- threading.Event(),- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),- payload,- "chatcmpl-test",- "chatcmpl-test",- monitor_id = monitor_id,- ),+ _drive_passthrough(_passthrough_backend(), payload, monitor_id),timeout = 5.0,)assert isinstance(response, _SameTaskStreamingResponse)@@ -6660,9 +6251,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_stream_preheader_cancel_cleans_pending_send(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-entered = asyncio.Event()cancelled = asyncio.Event()cancel_id = "preheader-cancel-cleanup"@@ -6675,18 +6265,7 @@ class TestApiMonitorProviderAndCompletionStreams:cancelled.set()raise- class Request:- async def is_disconnected(self):- return False-- monitor = ApiMonitor(max_entries = 3)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )- monkeypatch.setattr(inf_mod, "api_monitor", monitor)+ monitor, monitor_id = _install_monitor(monkeypatch)monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)payload = ChatCompletionRequest(@@ -6696,19 +6275,7 @@ class TestApiMonitorProviderAndCompletionStreams:cancel_id = cancel_id,)task = asyncio.create_task(- _openai_passthrough_stream(- Request(),- threading.Event(),- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),- payload,- "chatcmpl-test",- "chatcmpl-test",- monitor_id = monitor_id,- )+ _drive_passthrough(_passthrough_backend(), payload, monitor_id))await asyncio.wait_for(entered.wait(), timeout = 5.0)assert cancel_id in inf_mod._CANCEL_REGISTRY@@ -6722,9 +6289,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_stream_unstarted_cleanup_closes_completed_send_response(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-gate = asyncio.Event()returned = asyncio.Event()cancel_id = "unstarted-completed-send-cleanup"@@ -6742,18 +6308,7 @@ class TestApiMonitorProviderAndCompletionStreams:returned.set()return upstream_response- class Request:- async def is_disconnected(self):- return False-- monitor = ApiMonitor(max_entries = 3)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )- monkeypatch.setattr(inf_mod, "api_monitor", monitor)+ monitor, monitor_id = _install_monitor(monkeypatch)monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)payload = ChatCompletionRequest(@@ -6763,19 +6318,7 @@ class TestApiMonitorProviderAndCompletionStreams:cancel_id = cancel_id,)response = await asyncio.wait_for(- _openai_passthrough_stream(- Request(),- threading.Event(),- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),- payload,- "chatcmpl-test",- "chatcmpl-test",- monitor_id = monitor_id,- ),+ _drive_passthrough(_passthrough_backend(), payload, monitor_id),timeout = 5.0,)assert isinstance(response, _SameTaskStreamingResponse)@@ -6791,9 +6334,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_external_non_streaming_json_updates_monitor(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class DummyExternalClient:def __init__(self, **_kwargs):pass@@ -6841,9 +6383,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_external_stream_clean_eof_flushes_pending_tool_monitor(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-lines = ['data: {"choices":[{"index":0,"delta":{"tool_calls":[''{"index":0,"id":"call-eof","function":{"name":"lookup",'@@ -6886,9 +6427,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_external_stream_cancel_finalizes_monitor(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class DummyExternalClient:def __init__(self, **_kwargs):pass@@ -6931,9 +6471,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_completions_preheader_cancel_finalizes_monitor(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class Request:state = SimpleNamespace()url = SimpleNamespace(path = "/v1/completions")@@ -6950,16 +6489,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monkeypatch.setattr(- inf_mod,- "get_llama_cpp_backend",- lambda: SimpleNamespace(- is_loaded = True,- base_url = "http://llama.test",- context_length = 4096,- model_identifier = "gguf",- ),- )+ _pin_loaded_backend(monkeypatch)monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)response = await openai_completions(Request(), current_subject = "test")@@ -6975,9 +6505,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_completions_stream_cancel_finalizes_monitor(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class Request:state = SimpleNamespace()url = SimpleNamespace(path = "/v1/completions")@@ -6989,25 +6518,13 @@ class TestApiMonitorProviderAndCompletionStreams:async def is_disconnected(self):return False- async def fake_send(*_args, **_kwargs):- return httpx.Response(200, content = b"")-async def fake_items(*_args, **_kwargs):yield b'data: {"choices":[{"text":"hello"}]}\n\n'await asyncio.sleep(3600)monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monkeypatch.setattr(- inf_mod,- "get_llama_cpp_backend",- lambda: SimpleNamespace(- is_loaded = True,- base_url = "http://llama.test",- context_length = 4096,- model_identifier = "gguf",- ),- )+ _pin_loaded_backend(monkeypatch)monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)monkeypatch.setattr(inf_mod, "_aiter_llama_stream_items", fake_items)@@ -7030,9 +6547,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_completions_stream_requests_usage_only_for_monitor(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class Request:state = SimpleNamespace()url = SimpleNamespace(path = "/v1/completions")@@ -7060,16 +6576,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monkeypatch.setattr(- inf_mod,- "get_llama_cpp_backend",- lambda: SimpleNamespace(- is_loaded = True,- base_url = "http://llama.test",- context_length = 4096,- model_identifier = "gguf",- ),- )+ _pin_loaded_backend(monkeypatch)monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)monkeypatch.setattr(inf_mod, "_aiter_llama_stream_items", fake_items)@@ -7086,9 +6593,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_completions_non_streaming_post_error_finalizes_monitor(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class Request:state = SimpleNamespace()url = SimpleNamespace(path = "/v1/completions")@@ -7121,16 +6627,7 @@ class TestApiMonitorProviderAndCompletionStreams:"_cancelable_nonstreaming_client",lambda: FailingAsyncClient(),)- monkeypatch.setattr(- inf_mod,- "get_llama_cpp_backend",- lambda: SimpleNamespace(- is_loaded = True,- base_url = "http://llama.test",- context_length = 4096,- model_identifier = "gguf",- ),- )+ _pin_loaded_backend(monkeypatch)with pytest.raises(httpx.ConnectError):await openai_completions(Request(), current_subject = "test")@@ -7145,9 +6642,8 @@ class TestApiMonitorProviderAndCompletionStreams:def test_completions_omitted_max_tokens_falls_back_to_context(self, monkeypatch):# With no env knobs set, an omitted max_tokens must forward the# backend's context length, exactly as on main.+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class Request:state = SimpleNamespace()url = SimpleNamespace(path = "/v1/completions")@@ -7185,16 +6681,7 @@ class TestApiMonitorProviderAndCompletionStreams:monkeypatch.setattr(inf_mod, "_cancelable_nonstreaming_client", lambda: CapturingClient())- monkeypatch.setattr(- inf_mod,- "get_llama_cpp_backend",- lambda: SimpleNamespace(- is_loaded = True,- base_url = "http://llama.test",- context_length = 4096,- model_identifier = "gguf",- ),- )+ _pin_loaded_backend(monkeypatch)await openai_completions(Request(), current_subject = "test")@@ -7204,9 +6691,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_completions_forwards_spec_valid_zero_max_tokens(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class Request:state = SimpleNamespace()url = SimpleNamespace(path = "/v1/completions")@@ -7244,16 +6730,7 @@ class TestApiMonitorProviderAndCompletionStreams:monkeypatch.setattr(inf_mod, "_cancelable_nonstreaming_client", lambda: CapturingClient())- monkeypatch.setattr(- inf_mod,- "get_llama_cpp_backend",- lambda: SimpleNamespace(- is_loaded = True,- base_url = "http://llama.test",- context_length = 4096,- model_identifier = "gguf",- ),- )+ _pin_loaded_backend(monkeypatch)await openai_completions(Request(), current_subject = "test")@@ -7263,9 +6740,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_completions_rejects_non_integer_max_tokens_before_forwarding(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class Request:state = SimpleNamespace()url = SimpleNamespace(path = "/v1/completions")@@ -7282,16 +6758,7 @@ class TestApiMonitorProviderAndCompletionStreams:monkeypatch.setattr(inf_mod, "api_monitor", monitor)monkeypatch.setattr(inf_mod, "nonstreaming_client", lambda: UnusedClient())monkeypatch.setattr(inf_mod, "_cancelable_nonstreaming_client", lambda: UnusedClient())- monkeypatch.setattr(- inf_mod,- "get_llama_cpp_backend",- lambda: SimpleNamespace(- is_loaded = True,- base_url = "http://llama.test",- context_length = 4096,- model_identifier = "gguf",- ),- )+ _pin_loaded_backend(monkeypatch)with pytest.raises(HTTPException) as exc:await openai_completions(Request(), current_subject = "test")@@ -7308,12 +6775,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monitor_id = monitor.start(- endpoint = "/v1/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor, endpoint = "/v1/completions")_monitor_openai_chunk(monitor_id,@@ -7342,12 +6804,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)_monitor_openai_chunk(monitor_id,@@ -7395,12 +6852,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)for tool_call in [{"index": 0,@@ -7428,12 +6880,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)for name, arguments in [("web", None), ("web_search", "{}")]:_monitor_openai_chunk(monitor_id,@@ -7467,12 +6914,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)_monitor_openai_chunk(monitor_id,{@@ -7510,12 +6952,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)for function_call in [{"name": "look", "arguments": '{"query":'},{"name": "up", "arguments": '"weather"}'},@@ -7538,12 +6975,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)deltas = [{"index": 0, "id": "call-a", "function": {"name": "alpha", "arguments": '{"a":'}},{"index": 1, "id": "call-b", "function": {"name": "beta", "arguments": '{"b":'}},@@ -7571,12 +7003,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)deltas = [{"index": 0, "function": {"name": "alpha", "arguments": '{"a":'}},{"index": 1, "function": {"name": "beta", "arguments": '{"b":'}},@@ -7605,12 +7032,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)chunks = [(0, {"index": 0, "id": "shared-id", "function": {"name": "alpha", "arguments": "{"}}),(1, {"index": 0, "id": "shared-id", "function": {"name": "beta", "arguments": "{"}}),@@ -7654,12 +7076,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)deltas = [{"index": 0, "id": "call-a", "function": {"name": "alpha", "arguments": '{"a":'}},{"index": 0, "id": "call-b", "function": {"name": "beta", "arguments": '{"b":'}},@@ -7687,12 +7104,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)for tool_call in [{"index": 0,@@ -7726,12 +7138,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- first_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "first",- )+ first_id = _monitor_entry(monitor, prompt = "first")_monitor_openai_chunk(first_id,{@@ -7757,12 +7164,7 @@ class TestApiMonitorProviderAndCompletionStreams:else:monitor.finish(first_id, "cancelled" if terminal == "cancelled" else "completed")- second_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "second",- )+ second_id = _monitor_entry(monitor, prompt = "second")_monitor_openai_chunk(second_id,{@@ -7791,12 +7193,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)for content in ["hel", "lo"]:_monitor_openai_chunk(monitor_id,@@ -7825,12 +7222,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)text = "Done." if tool_first else "Checking:"text_chunk = {"choices": [{"index": 0, "delta": {"content": text}}]}tool_chunk = {@@ -7864,12 +7256,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)_monitor_openai_chunk(monitor_id,{@@ -7915,12 +7302,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)_monitor_openai_chunk(monitor_id,{@@ -7961,12 +7343,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)chunks = [{"choices": [@@ -8015,12 +7392,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)chunks = [{"choices": [@@ -8055,12 +7427,7 @@ class TestApiMonitorProviderAndCompletionStreams:monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)for index in range(70):_monitor_openai_chunk(monitor_id,@@ -8103,9 +7470,8 @@ class TestApiMonitorProviderAndCompletionStreams:assert entry.openai_stream_last_tool_indexes == {}def test_embeddings_request_is_counted_active_and_completed(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class Request:state = SimpleNamespace()url = SimpleNamespace(path = "/v1/embeddings")@@ -8145,16 +7511,7 @@ class TestApiMonitorProviderAndCompletionStreams:"_cancelable_nonstreaming_client",lambda: FakeAsyncClient(),)- monkeypatch.setattr(- inf_mod,- "get_llama_cpp_backend",- lambda: SimpleNamespace(- is_loaded = True,- base_url = "http://llama.test",- context_length = 4096,- model_identifier = "gguf",- ),- )+ _pin_loaded_backend(monkeypatch)response = await openai_embeddings(Request(), current_subject = "test")@@ -8170,16 +7527,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_stream_task_cancel_finalizes_monitor(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-- class Request:- async def is_disconnected(self):- return False-- async def fake_send(*_args, **_kwargs):- return httpx.Response(200, content = b"")-async def fake_items(*_args, **_kwargs):yield 'data: {"choices":[{"delta":{"content":"hello"}}]}'await asyncio.sleep(3600)@@ -8190,37 +7539,19 @@ class TestApiMonitorProviderAndCompletionStreams:monkeypatch.setattr(inf_mod, "api_monitor", monitor)monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)monkeypatch.setattr(inf_mod, "_aiter_llama_stream_items", fake_items)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)payload = ChatCompletionRequest(model = "default",messages = [ChatMessage(role = "user", content = "hi")],stream = True,cancel_id = cancel_id,- tools = [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object", "properties": {}},- },- }- ],+ tools = [_LOOKUP_TOOL],)response = await _openai_passthrough_stream(- Request(),+ _ConnectedRequest(),threading.Event(),- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- _auth_headers = {"Authorization": "Bearer secret"},- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),+ _passthrough_backend(_auth_headers = {"Authorization": "Bearer secret"}),payload,"gguf","chatcmpl-test",@@ -8249,9 +7580,8 @@ class TestApiMonitorProviderAndCompletionStreams:def test_passthrough_stream_immediate_task_cancel_releases_admission_and_tracker(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-async def fake_cancel_check(*_args, **_kwargs):raise asyncio.CancelledError()@@ -8275,12 +7605,7 @@ class TestApiMonitorProviderAndCompletionStreams:effective_parallel_slots = 1,_request_reasoning_kwargs = lambda *_args, **_kwargs: None,)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)with pytest.raises(asyncio.CancelledError):await _openai_passthrough_stream(@@ -8304,9 +7629,8 @@ class TestApiMonitorProviderAndCompletionStreams:def test_passthrough_stream_queued_cancel_before_inner_first_chunk_runs_cleanup(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class Request(self._Request):app = SimpleNamespace(state = SimpleNamespace(llama_parallel_slots = 1))@@ -8365,12 +7689,7 @@ class TestApiMonitorProviderAndCompletionStreams:response = await _openai_passthrough_stream(Request(),threading.Event(),- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- effective_parallel_slots = 1,- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),+ _passthrough_backend(effective_parallel_slots = 1),payload,"gguf","chatcmpl-test",@@ -8414,9 +7733,8 @@ class TestApiMonitorProviderAndCompletionStreams:def test_passthrough_stream_queued_cancel_after_inner_first_chunk_finalizes_monitor(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class Request(self._Request):app = SimpleNamespace(state = SimpleNamespace(llama_parallel_slots = 1))@@ -8455,12 +7773,7 @@ class TestApiMonitorProviderAndCompletionStreams:"_openai_passthrough_stream_admitted",fake_admitted,)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)queue = get_llama_admission_queue("http://llama.test")blocker = queue.reserve(capacity = 1, config = LlamaAdmissionConfig()).lease_nowait()@@ -8476,12 +7789,7 @@ class TestApiMonitorProviderAndCompletionStreams:response = await _openai_passthrough_stream(Request(),threading.Event(),- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- effective_parallel_slots = 1,- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),+ _passthrough_backend(effective_parallel_slots = 1),payload,"gguf","chatcmpl-test",@@ -8644,9 +7952,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_stream_queued_request_sends_keepalive_before_upstream(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class Request:app = SimpleNamespace(state = SimpleNamespace(llama_parallel_slots = 1))url = SimpleNamespace(path = "/v1/chat/completions")@@ -8661,22 +7968,13 @@ class TestApiMonitorProviderAndCompletionStreams:monkeypatch.setenv(ADMISSION_KEEPALIVE_INTERVAL_ENV, "0.01")monkeypatch.setattr(inf_mod, "api_monitor", monitor)monkeypatch.setattr(inf_mod, "_openai_passthrough_stream_admitted", fail_admitted)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)queue = get_llama_admission_queue("http://llama.test")blocker = queue.reserve(capacity = 1, config = LlamaAdmissionConfig()).lease_nowait()assert blocker is not None- payload = ChatCompletionRequest(- model = "default",- messages = [ChatMessage(role = "user", content = "hi")],- stream = True,- )+ payload = _hi_stream_payload()response = await _openai_passthrough_stream(Request(),threading.Event(),@@ -8714,9 +8012,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_non_streaming_admission_timeout_before_upstream(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class Request:app = SimpleNamespace(state = SimpleNamespace(llama_parallel_slots = 1))url = SimpleNamespace(path = "/v1/chat/completions")@@ -8767,9 +8064,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_non_streaming_admission_queue_full_before_upstream(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class Request:app = SimpleNamespace(state = SimpleNamespace(llama_parallel_slots = 1))url = SimpleNamespace(path = "/v1/chat/completions")@@ -8826,9 +8122,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_non_streaming_immediate_cancel_stops_before_upstream(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-async def fail_upstream(*_args, **_kwargs):raise AssertionError("upstream must not start after client cancellation")@@ -8842,12 +8137,7 @@ class TestApiMonitorProviderAndCompletionStreams:cancel_event.set()monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)payload = ChatCompletionRequest(model = "default",messages = [ChatMessage(role = "user", content = "hi")],@@ -8876,9 +8166,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_non_streaming_admission_task_cancel_finalizes_monitor(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-async def fake_wait(*_args, **_kwargs):raise asyncio.CancelledError()@@ -8897,12 +8186,7 @@ class TestApiMonitorProviderAndCompletionStreams:"_openai_passthrough_non_streaming_upstream",fail_upstream,)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)payload = ChatCompletionRequest(model = "default",messages = [ChatMessage(role = "user", content = "hi")],@@ -8930,9 +8214,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_non_streaming_cancel_finalizes_monitor(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class CancellingAsyncClient:async def __aenter__(self):return self@@ -8950,33 +8233,16 @@ class TestApiMonitorProviderAndCompletionStreams:"nonstreaming_client",lambda: CancellingAsyncClient(),)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)payload = ChatCompletionRequest(model = "default",messages = [ChatMessage(role = "user", content = "hi")],- tools = [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object", "properties": {}},- },- }- ],+ tools = [_LOOKUP_TOOL],)with pytest.raises(asyncio.CancelledError):await _openai_passthrough_non_streaming(- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),+ _passthrough_backend(),payload,"gguf",monitor_id = monitor_id,@@ -8989,26 +8255,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_non_streaming_cancel_closes_blocked_upstream_post(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-- class HangingCancelableClient:- def __init__(self):- self.started = asyncio.Event()- self.closed = asyncio.Event()-- async def post(self, *_args, **_kwargs):- self.started.set()- await self.closed.wait()- raise httpx.ReadError("client closed")-- async def aclose(self):- self.closed.set()-- class Request:- async def is_disconnected(self):- return False-client = HangingCancelableClient()monitor = ApiMonitor(max_entries = 3)monkeypatch.setattr(inf_mod, "api_monitor", monitor)@@ -9017,38 +8265,21 @@ class TestApiMonitorProviderAndCompletionStreams:"_cancelable_nonstreaming_client",lambda: client,)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)cancel_event = threading.Event()payload = ChatCompletionRequest(model = "default",messages = [ChatMessage(role = "user", content = "hi")],- tools = [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object", "properties": {}},- },- }- ],+ tools = [_LOOKUP_TOOL],)task = asyncio.create_task(_openai_passthrough_non_streaming(- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),+ _passthrough_backend(),payload,"gguf",monitor_id = monitor_id,- request = Request(),+ request = _ConnectedRequest(),cancel_event = cancel_event,))@@ -9066,22 +8297,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_non_streaming_route_registers_cancel_id(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-- class HangingCancelableClient:- def __init__(self):- self.started = asyncio.Event()- self.closed = asyncio.Event()-- async def post(self, *_args, **_kwargs):- self.started.set()- await self.closed.wait()- raise httpx.ReadError("client closed")-- async def aclose(self):- self.closed.set()-class Request:state = SimpleNamespace()url = SimpleNamespace(path = "/v1/chat/completions")@@ -9119,15 +8336,7 @@ class TestApiMonitorProviderAndCompletionStreams:model = "default",messages = [ChatMessage(role = "user", content = "hi")],cancel_id = cancel_id,- tools = [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object", "properties": {}},- },- }- ],+ tools = [_LOOKUP_TOOL],)task = asyncio.create_task(@@ -9153,22 +8362,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_non_streaming_disconnect_closes_blocked_upstream_post(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-- class HangingCancelableClient:- def __init__(self):- self.started = asyncio.Event()- self.closed = asyncio.Event()-- async def post(self, *_args, **_kwargs):- self.started.set()- await self.closed.wait()- raise httpx.ReadError("client closed")-- async def aclose(self):- self.closed.set()-class Request:def __init__(self):self.disconnected = False@@ -9185,34 +8380,17 @@ class TestApiMonitorProviderAndCompletionStreams:"_cancelable_nonstreaming_client",lambda: client,)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)cancel_event = threading.Event()payload = ChatCompletionRequest(model = "default",messages = [ChatMessage(role = "user", content = "hi")],- tools = [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object", "properties": {}},- },- }- ],+ tools = [_LOOKUP_TOOL],)task = asyncio.create_task(_openai_passthrough_non_streaming(- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),+ _passthrough_backend(),payload,"gguf",monitor_id = monitor_id,@@ -9235,9 +8413,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_non_streaming_forwards_backend_auth_headers(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-captured = {}class FakeNonStreamingClient:@@ -9260,14 +8437,7 @@ class TestApiMonitorProviderAndCompletionStreams:},)- monitor = ApiMonitor(max_entries = 3)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )- monkeypatch.setattr(inf_mod, "api_monitor", monitor)+ monitor, monitor_id = _install_monitor(monkeypatch)monkeypatch.setattr(inf_mod,"nonstreaming_client",@@ -9276,24 +8446,11 @@ class TestApiMonitorProviderAndCompletionStreams:payload = ChatCompletionRequest(model = "default",messages = [ChatMessage(role = "user", content = "hi")],- tools = [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object", "properties": {}},- },- }- ],+ tools = [_LOOKUP_TOOL],)response = await _openai_passthrough_non_streaming(- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- _auth_headers = {"Authorization": "Bearer secret"},- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),+ _passthrough_backend(_auth_headers = {"Authorization": "Bearer secret"}),payload,"gguf",monitor_id = monitor_id,@@ -9306,9 +8463,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_non_streaming_forces_upstream_stream_false(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-captured = {}class FakeNonStreamingClient:@@ -9349,34 +8505,17 @@ class TestApiMonitorProviderAndCompletionStreams:"nonstreaming_client",lambda: FakeNonStreamingClient(),)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)payload = ChatCompletionRequest(model = "default",messages = [ChatMessage(role = "user", content = "hi")],stream = True,stream_options = {"include_usage": True},- tools = [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object", "properties": {}},- },- }- ],+ tools = [_LOOKUP_TOOL],)await _openai_passthrough_non_streaming(- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),+ _passthrough_backend(),payload,"gguf",monitor_id = monitor_id,@@ -9412,16 +8551,8 @@ class TestApiMonitorProviderAndCompletionStreams:# Some llama-server builds emit the finish chunk and then hold the HTTP# stream open without sending [DONE]; the terminal classifier must end# the client stream promptly instead of hanging on the open socket.+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-- class Request:- async def is_disconnected(self):- return False-- async def fake_send(*_args, **_kwargs):- return httpx.Response(200, content = b"")-async def fake_items(*_args,post_first_item_read_timeout_s = None,@@ -9437,35 +8568,18 @@ class TestApiMonitorProviderAndCompletionStreams:monkeypatch.setattr(inf_mod, "api_monitor", monitor)monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)monkeypatch.setattr(inf_mod, "_aiter_llama_stream_items", fake_items)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)payload = ChatCompletionRequest(model = "default",messages = [ChatMessage(role = "user", content = "hi")],stream = True,- tools = [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object", "properties": {}},- },- }- ],+ tools = [_LOOKUP_TOOL],)response = await _openai_passthrough_stream(- Request(),+ _ConnectedRequest(),threading.Event(),- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),+ _passthrough_backend(),payload,"gguf","chatcmpl-test",@@ -9490,16 +8604,8 @@ class TestApiMonitorProviderAndCompletionStreams:# include_usage keeps the stream open past the finish chunk waiting for# the usage chunk; if that never arrives, the post-terminal grace path# must close with a clean [DONE], not an in-band error.+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-- class Request:- async def is_disconnected(self):- return False-- async def fake_send(*_args, **_kwargs):- return httpx.Response(200, content = b"")-async def fake_items(*_args, **_kwargs):yield 'data: {"choices":[{"index":0,"delta":{"content":"hi"},"finish_reason":null}]}'yield 'data: {"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}'@@ -9509,36 +8615,19 @@ class TestApiMonitorProviderAndCompletionStreams:monkeypatch.setattr(inf_mod, "api_monitor", monitor)monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)monkeypatch.setattr(inf_mod, "_aiter_llama_stream_items", fake_items)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)payload = ChatCompletionRequest(model = "default",messages = [ChatMessage(role = "user", content = "hi")],stream = True,stream_options = {"include_usage": True},- tools = [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object", "properties": {}},- },- }- ],+ tools = [_LOOKUP_TOOL],)response = await _openai_passthrough_stream(- Request(),+ _ConnectedRequest(),threading.Event(),- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),+ _passthrough_backend(),payload,"gguf","chatcmpl-test",@@ -9556,16 +8645,8 @@ class TestApiMonitorProviderAndCompletionStreams:asyncio.run(_run())def test_passthrough_stream_stall_after_data_emits_error(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-- class Request:- async def is_disconnected(self):- return False-- async def fake_send(*_args, **_kwargs):- return httpx.Response(200, content = b"")-async def fake_items(*_args, **_kwargs):yield 'data: {"choices":[{"delta":{"content":"hello"}}]}'raise httpx.ReadTimeout("upstream went silent")@@ -9574,35 +8655,18 @@ class TestApiMonitorProviderAndCompletionStreams:monkeypatch.setattr(inf_mod, "api_monitor", monitor)monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fake_send)monkeypatch.setattr(inf_mod, "_aiter_llama_stream_items", fake_items)- monitor_id = monitor.start(- endpoint = "/v1/chat/completions",- method = "POST",- model = "gguf",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor)payload = ChatCompletionRequest(model = "default",messages = [ChatMessage(role = "user", content = "hi")],stream = True,- tools = [- {- "type": "function",- "function": {- "name": "lookup",- "parameters": {"type": "object", "properties": {}},- },- }- ],+ tools = [_LOOKUP_TOOL],)response = await _openai_passthrough_stream(- Request(),+ _ConnectedRequest(),threading.Event(),- SimpleNamespace(- base_url = "http://llama.test",- context_length = 4096,- _request_reasoning_kwargs = lambda *_args, **_kwargs: None,- ),+ _passthrough_backend(),payload,"gguf","chatcmpl-test",@@ -9632,9 +8696,8 @@ class TestApiMonitorSafetensorsUsage:method = "POST"def test_non_streaming_safetensors_keeps_event_loop_responsive(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-generation_started = threading.Event()generation_finished = threading.Event()generation_threads = []@@ -9701,9 +8764,8 @@ class TestApiMonitorSafetensorsUsage:asyncio.run(_run())def test_non_streaming_safetensors_records_usage(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class DummyBackend:active_model_name = "safe-model"models = {"safe-model": {"context_length": 2048}}@@ -9763,9 +8825,8 @@ class TestApiMonitorSafetensorsUsage:asyncio.run(_run())def test_non_streaming_safetensors_tool_cancel_records_cancelled(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-reset_tool_policy()class DummyBackend:@@ -9834,9 +8895,8 @@ class TestApiMonitorSafetensorsUsage:asyncio.run(_run())def test_non_streaming_safetensors_tool_task_cancel_finalizes_monitor(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-reset_tool_policy()reset_called = False@@ -9944,6 +9004,7 @@ class TestApiMonitorAudioInput:return inf_moddef test_audio_input_non_streaming_records_active_monitor(self, monkeypatch):+ import routes.inference as inf_modasync def _run():inf_mod = self._patch_audio_backend(monkeypatch, ["hello", " world"])monitor = ApiMonitor(max_entries = 3)@@ -9976,6 +9037,7 @@ class TestApiMonitorAudioInput:asyncio.run(_run())def test_audio_input_non_streaming_keeps_event_loop_responsive(self, monkeypatch):+ import routes.inference as inf_modasync def _run():generation_started = threading.Event()generation_finished = threading.Event()@@ -10027,6 +9089,7 @@ class TestApiMonitorAudioInput:asyncio.run(_run())def test_audio_input_streaming_records_monitor_reply(self, monkeypatch):+ import routes.inference as inf_modasync def _run():inf_mod = self._patch_audio_backend(monkeypatch, ["hello", " world"])monitor = ApiMonitor(max_entries = 3)@@ -10089,9 +9152,8 @@ class TestApiMonitorAudioInput:asyncio.run(_run())def test_non_gguf_tts_auto_route_records_monitor(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class DummyTtsBackend:active_model_name = "tts-model"models = {@@ -10156,9 +9218,8 @@ class TestApiMonitorAudioInput:asyncio.run(_run())def test_non_gguf_tts_cancel_finalizes_monitor(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-class DummyTtsBackend:active_model_name = "tts-model"models = {@@ -10210,9 +9271,8 @@ class TestApiMonitorAudioInput:asyncio.run(_run())def test_gguf_tts_auto_route_records_monitor(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-async def fake_generate_audio(_payload,_request,@@ -10338,9 +9398,8 @@ class TestResponsesChatTemplateKwargs:assert chat_req.enable_thinking is Nonedef test_responses_stream_queued_request_sends_keepalive_before_upstream(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-async def fail_send(*_args, **_kwargs):raise AssertionError("responses upstream must not start while queued")@@ -10361,12 +9420,7 @@ class TestResponsesChatTemplateKwargs:queue = get_llama_admission_queue("http://llama.responses.test")blocker = queue.reserve(capacity = 1, config = LlamaAdmissionConfig()).lease_nowait()assert blocker is not None- monitor_id = monitor.start(- endpoint = "/v1/responses",- method = "POST",- model = "qwen-local",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor, endpoint = "/v1/responses", model = "qwen-local")payload = ResponsesRequest(model = "qwen-local", input = "hi", stream = True)response = await _responses_stream(@@ -10398,9 +9452,8 @@ class TestResponsesChatTemplateKwargs:asyncio.run(_run())def test_responses_stream_cancel_after_created_finalizes_monitor_and_slot(self, monkeypatch):+ import routes.inference as inf_modasync def _run():- import routes.inference as inf_mod-async def fail_send(*_args, **_kwargs):raise AssertionError("responses upstream must not start after created cancel")@@ -10416,12 +9469,7 @@ class TestResponsesChatTemplateKwargs:monkeypatch.setattr(inf_mod, "api_monitor", monitor)monkeypatch.setattr(inf_mod, "get_llama_cpp_backend", lambda: backend)monkeypatch.setattr(inf_mod, "_send_stream_with_preheader_cancel", fail_send)- monitor_id = monitor.start(- endpoint = "/v1/responses",- method = "POST",- model = "qwen-local",- prompt = "hi",- )+ monitor_id = _monitor_entry(monitor, endpoint = "/v1/responses", model = "qwen-local")payload = ResponsesRequest(model = "qwen-local", input = "hi", stream = True)response = await _responses_stream(@@ -10675,9 +9723,8 @@ def test_the_two_seed_helpers_agree_on_which_seeds_are_random():class TestPassthroughImageNormalization:@staticmethoddef _data_url(fmt: str) -> str:- from io import BytesIO-from PIL import Image+ from io import BytesIObuf = BytesIO()Image.new("RGB", (2, 2), (0, 128, 255)).save(buf, format = fmt)@@ -10747,9 +9794,8 @@ class TestPassthroughImageNormalization:@staticmethoddef _gray16_ramp_png(width: int = 256) -> bytes:- from io import BytesIO-from PIL import Image+ from io import BytesIOimg = Image.new("I;16", (width, 2))img.putdata([(x % width) * 257 for _ in range(2) for x in range(width)])@@ -10761,9 +9807,9 @@ class TestPassthroughImageNormalization:# convert("RGB") reads a 16-bit source as 8-bit and clips: the whole ramp# above 255 collapses to white. llama-server reading the same PNG itself# scales instead, so re-encoding must not throw the picture away.- from io import BytesIOfrom PIL import Image+ from io import BytesIOraw = self._gray16_ramp_png()out = base64.b64decode(_image_bytes_to_png_b64(raw))@@ -10777,9 +9823,9 @@ class TestPassthroughImageNormalization:# I;16B rejects point() outright, and the caller turns any exception into# a 400, so scaling without normalising the mode is worse than clipping.# PNG cannot carry the tagged modes; a 16-bit TIFF reopens as I;16B.- from io import BytesIOfrom PIL import Image+ from io import BytesIOimg = Image.new("I;16B", (256, 2))img.putdata([(x % 256) * 257 for _ in range(2) for x in range(256)])@@ -10795,9 +9841,9 @@ class TestPassthroughImageNormalization:# "I" and "F" declare no range. A 32-bit TIFF whose samples already sit# in 0..255 must keep them: scaling it by 1/257 would black the picture# out, which is worse than the clipping the scaling was added to fix.- from io import BytesIOfrom PIL import Image+ from io import BytesIOimg = Image.new("I", (256, 2))img.putdata([x % 256 for _ in range(2) for x in range(256)])@@ -10810,9 +9856,8 @@ class TestPassthroughImageNormalization:assert row == list(range(256)), f"0..255 ramp altered: max={max(row)}"def test_eight_bit_images_are_unchanged_by_the_scaling_branch(self):- from io import BytesIO-from PIL import Image+ from io import BytesIOfor mode, colour in (("RGB", (10, 20, 30)), ("RGBA", (10, 20, 30, 255)), ("L", 77)):buf = BytesIO()Image.new(mode, (4, 4), colour).save(buf, format = "PNG")@@ -10853,9 +9898,8 @@ class TestPassthroughImageNormalization:assert exc.value.status_code == 400def test_echoed_legacy_image_is_not_spliced_twice(self):- from io import BytesIO-from PIL import Image+ from io import BytesIObuf = BytesIO()Image.new("RGB", (2, 2), (1, 2, 3)).save(buf, format = "WEBP")diff --git a/studio/backend/tests/test_openai_videos_route.py b/studio/backend/tests/test_openai_videos_route.pyindex fb35ef98d19..fbd3b4aec74 100644--- a/studio/backend/tests/test_openai_videos_route.py+++ b/studio/backend/tests/test_openai_videos_route.py@@ -32,6 +32,8 @@ if str(_TESTS_DIR) not in sys.path:sys.path.insert(0, str(_TESTS_DIR))from test_video_routes import _FakeBackend # noqa: E402+from core.inference import media_auto_switch+import types@pytest.mark.parametrize(@@ -797,10 +799,6 @@ def test_an_unservable_duration_is_refused_before_the_model_switch(client, backeA duration no family can serve was therefore only refused inside begin_generate --after the resident pipeline had been evicted and the target model fully loaded."""- import types-- from core.inference import media_auto_switch-switched = {"completed": False}async def _fake_switch(@@ -826,10 +824,6 @@ def test_an_unservable_duration_is_refused_before_the_model_switch(client, backedef test_a_reference_only_checkpoint_is_refused_before_the_model_switch(client, backend, monkeypatch):- import types-- from core.inference import media_auto_switch-switched = {"completed": False}async def _fake_switch(@@ -857,9 +851,6 @@ def test_a_reference_only_checkpoint_is_refused_before_the_model_switch(@pytest.mark.parametrize("reference_size", [(8, 8), (5000, 1000)])def test_ref2va_routes_the_input_image_as_a_reference(client, backend, monkeypatch, reference_size):- import types-- from core.inference import media_auto_switchfrom core.inference.video_minimax_h3 import H3_TASK_REFERENCESswitched = {"completed": False}@@ -916,8 +907,6 @@ def test_ref2va_routes_the_input_image_as_a_reference(client, backend, monkeypatdef test_reference_conditioning_follows_the_state_reserved_by_begin_generate(client, backend, monkeypatch):- import types-from core.inference.video_families import detect_video_familyfrom core.inference.video_minimax_h3 import H3_TASK_REFERENCES@@ -1170,10 +1159,6 @@ def test_an_undecodable_reference_is_refused_before_the_model_switch(client, bac_resolve_keyframes only decodes inside begin_generate, which runs after the autoswitch, so bad bytes used to evict the resident pipeline and load the target first."""- import types-- from core.inference import media_auto_switch-switched = {"completed": False}async def _fake_switch(@@ -1286,9 +1271,6 @@ def test_the_job_describes_the_run_the_backend_reserved(client, backend, monkeypdef test_requested_model_cannot_change_before_the_generation_reservation(client, backend, monkeypatch, swap_before_snapshot):- import types-- from core.inference import media_auto_switchfrom utils import openai_auto_switch_settingsrequested = backend.status()["repo_id"]diff --git a/studio/backend/tests/test_orchestrator_unload_cancel.py b/studio/backend/tests/test_orchestrator_unload_cancel.pyindex 966b5e06a94..d3da92e74a6 100644--- a/studio/backend/tests/test_orchestrator_unload_cancel.py+++ b/studio/backend/tests/test_orchestrator_unload_cancel.py@@ -15,6 +15,17 @@ from core.inference import orchestrator as orch_modfrom core.inference.orchestrator import InferenceOrchestrator+class _Llama:+ is_active = False+ is_loaded = False+ model_identifier = None+++class _Unsloth:+ def get_loading_model(self):+ return None # no Unsloth load in flight -> Unsloth fast path skipped++def _bare_orchestrator():"""An orchestrator without the real __init__ subprocess/network."""o = InferenceOrchestrator.__new__(InferenceOrchestrator)@@ -325,6 +336,7 @@ def test_worker_drain_skip_emits_cancelled_gen_done_when_draining():# while a generate is still queued would be lost when it is dequeued. drain_event# is the durable signal: while it is set the worker skips the generate (emitting an# immediate gen_done so the stream/mailbox drains) instead of running it.+import queue as _queuefrom core.inference.worker import _drain_skip_generate@@ -368,6 +380,7 @@ def test_worker_generate_rechecks_drain_after_clearing_cancel():# parent sets drain+cancel for an unload, then the worker clears cancel_event# (erasing that cancel). A second drain check *after* the clear catches it and# skips the generate instead of running the outgoing model to completion.+import queue as _queuefrom core.inference.worker import _drain_skip_generate@@ -573,9 +586,9 @@ def test_load_does_not_accumulate_stale_models_defeating_the_unload_guard(monkey# Otherwise switching A -> B leaves 'A' in self.models, so a later unload('A')# passes the "not in self.models" guard and the worker's absent-name fallback# unloads the *active* model B.- import typesfrom utils import transformers_version as _tv+ import typeso = _bare_orchestrator()o.active_model_name = None@@ -618,17 +631,14 @@ def test_load_does_not_accumulate_stale_models_defeating_the_unload_guard(monkeydef test_unload_route_serializes_with_loads_via_lifecycle_gate(monkeypatch):# Item #5: /unload must hold the same lifecycle gate as /load so a concurrent load# can't swap the backend subprocess/queues mid-unload.- import asyncio+ from models.inference import LoadRequest, UnloadRequest+ import asyncioimport routes.inference as inference_route+from core.inference import llama_keepwarm as kwfrom models.inference import UnloadRequest- class _Llama:- is_active = False- is_loaded = False- model_identifier = None-monkeypatch.setattr(inference_route, "get_llama_cpp_backend", lambda: _Llama())monkeypatch.setattr(inference_route, "is_registered_native_path_label", lambda *a: False)@@ -720,17 +730,14 @@ def test_unload_route_cancels_in_flight_load_without_waiting_on_gate(monkeypatch# The regression: /unload wrapped its whole body in the lifecycle gate, so the# Stop-loading button (cancelLoading -> /unload) could not interrupt a safetensors# load that holds the gate for its full duration. The cancel must run off-gate.- import asyncio+ from models.inference import LoadRequest, UnloadRequest+ import asyncioimport routes.inference as inference_route+from core.inference import llama_keepwarm as kwfrom models.inference import UnloadRequest- class _Llama:- is_active = False- is_loaded = False- model_identifier = None-monkeypatch.setattr(inference_route, "get_llama_cpp_backend", lambda: _Llama())monkeypatch.setattr(inference_route, "is_registered_native_path_label", lambda *a: False)@@ -767,10 +774,9 @@ def test_unload_route_cancels_in_flight_load_without_waiting_on_gate(monkeypatchdef test_scoped_unload_cancels_only_its_running_standard_load(monkeypatch):+ from models.inference import LoadRequest, UnloadRequestimport asyncio-import routes.inference as inference_route- from models.inference import LoadRequest, UnloadRequestcancelled = []@@ -782,11 +788,6 @@ def test_scoped_unload_cancels_only_its_running_standard_load(monkeypatch):cancelled.append(name)return True- class _Llama:- is_active = False- is_loaded = False- model_identifier = None-monkeypatch.setattr(inference_route, "get_inference_backend", lambda: _Backend())monkeypatch.setattr(inference_route, "get_llama_cpp_backend", lambda: _Llama())attempt = inference_route._begin_load_attempt(@@ -811,10 +812,9 @@ def test_scoped_unload_cancels_only_its_running_standard_load(monkeypatch):def test_scoped_unload_cannot_cancel_a_newer_same_model_load(monkeypatch):+ from models.inference import LoadRequest, UnloadRequestimport asyncio-import routes.inference as inference_route- from models.inference import LoadRequest, UnloadRequestdef _unexpected_backend():pytest.fail("a stale scoped cancel must not inspect or mutate any backend")@@ -842,11 +842,10 @@ def test_scoped_unload_cannot_cancel_a_newer_same_model_load(monkeypatch):def test_scoped_cancel_before_load_registration_is_consumed(monkeypatch):- import asyncio-- import routes.inference as inference_routefrom fastapi import HTTPExceptionfrom models.inference import LoadRequest, UnloadRequest+ import asyncio+ import routes.inference as inference_routedef _unexpected_backend():pytest.fail("cancel-first cleanup must not inspect a backend")@@ -881,12 +880,11 @@ def test_scoped_cancel_before_load_registration_is_consumed(monkeypatch):def test_scoped_cancel_holds_load_ownership_until_backend_cancel_finishes(monkeypatch):- import asyncio-- import routes.inference as inference_routefrom core.inference import llama_keepwarmfrom fastapi import HTTPExceptionfrom models.inference import LoadRequest, UnloadRequest+ import asyncio+ import routes.inference as inference_routebackend_cancel_entered = threading.Event()release_backend_cancel = threading.Event()@@ -895,11 +893,6 @@ def test_scoped_cancel_holds_load_ownership_until_backend_cancel_finishes(monkeydef get_loading_model(self):return None- class _Llama:- is_active = False- is_loaded = False- model_identifier = None-def _get_backend():backend_cancel_entered.set()assert release_backend_cancel.wait(timeout = 5)@@ -973,11 +966,10 @@ def test_scoped_cancel_holds_load_ownership_until_backend_cancel_finishes(monkeydef test_scoped_unload_of_queued_attempt_never_unloads_or_deadlocks(monkeypatch):- import asyncio-- import routes.inference as inference_routefrom fastapi import HTTPExceptionfrom models.inference import LoadRequest, UnloadRequest+ import asyncio+ import routes.inference as inference_routedef _unexpected_backend():pytest.fail("cancel-only cleanup must never unload a resident model")@@ -1019,10 +1011,9 @@ def test_scoped_unload_of_queued_attempt_never_unloads_or_deadlocks(monkeypatch)def test_scoped_unload_cancels_only_its_running_gguf_load(monkeypatch):+ from models.inference import LoadRequest, UnloadRequestimport asyncio-import routes.inference as inference_route- from models.inference import LoadRequest, UnloadRequestclass _Backend:def get_loading_model(self):@@ -1081,8 +1072,8 @@ def test_standard_load_honors_scoped_cancel_before_worker_start():def test_gguf_load_cancellation_includes_the_backend_unload_event():import asyncio-import routes.inference as inference_route+from core.inference.llama_cpp import GgufDownloadCancelledclass _Llama:@@ -1127,7 +1118,6 @@ def test_cancelled_gguf_download_uses_the_typed_signal():def test_gguf_load_attempt_does_not_hide_a_real_resolver_failure():import asyncio-import routes.inference as inference_routeclass _Llama:@@ -1149,8 +1139,8 @@ def test_gguf_load_attempt_does_not_hide_a_real_resolver_failure():def test_stale_unload_does_not_hide_an_update_refusal():import asyncio-import routes.inference as inference_route+from core.inference.llama_cpp import GgufLoadIntent, LlamaCppBackendllama = LlamaCppBackend()@@ -1317,6 +1307,8 @@ def test_load_model_aborts_when_cancelled_before_spawn(monkeypatch):# Stop-loading during GPU placement discards the loading marker (cancel_load) with# no child yet to kill. load_model must observe the removal and not spawn a worker# that loads the model after /unload already reported it unloaded.+ from utils import transformers_version as tv+o = _bare_orchestrator()o.active_model_name = Noneo.models = {}@@ -1353,9 +1345,9 @@ def test_load_model_aborts_when_old_worker_survives_shutdown(monkeypatch):# A wedged worker that outlives terminate/kill makes _shutdown_subprocess return# False. load_model must not spawn a second worker over it (double GPU allocation +# the survivor's handle is lost); it aborts so the load can retry once it exits.- import typesfrom utils import transformers_version as tv+ import typeso = _bare_orchestrator()o.active_model_name = "old"@@ -1380,6 +1372,8 @@ def test_load_model_aborts_when_old_worker_survives_shutdown(monkeypatch):def test_load_model_proceeds_when_not_cancelled(monkeypatch):# Guard against a false abort: an uncancelled load keeps its marker and spawns.+ from utils import transformers_version as tv+o = _bare_orchestrator()o.active_model_name = Noneo.models = {}@@ -1414,9 +1408,9 @@ def test_load_model_proceeds_when_not_cancelled(monkeypatch):def test_load_model_reaps_worker_after_inactivity_timeout(monkeypatch):# A quiet install can trip the inactivity timeout; the failure path must# still tear its worker down (#9398).- import typesfrom utils import transformers_version as tv+ import typeso = _bare_orchestrator()o.active_model_name = None@@ -1451,9 +1445,8 @@ def test_load_model_reaps_worker_after_inactivity_timeout(monkeypatch):def test_worker_reported_load_failure_reaps_worker(monkeypatch):- import types-from utils import transformers_version as tv+ import typeso = _bare_orchestrator()o.active_model_name = None@@ -1485,9 +1478,9 @@ def test_worker_reported_load_failure_reaps_worker(monkeypatch):def test_failed_load_keeps_timeout_after_cancel_teardown(monkeypatch):# Both paths may request teardown; the serialized second call is harmless.- import typesfrom utils import transformers_version as tv+ import typeso = _bare_orchestrator()o.active_model_name = None@@ -1548,9 +1541,9 @@ def test_failed_load_keeps_timeout_after_cancel_teardown(monkeypatch):def test_failed_load_keeps_the_timeout_when_teardown_raises(monkeypatch):# A teardown failure must stay a warning, not replace the load error.- import typesfrom utils import transformers_version as tv+ import typeso = _bare_orchestrator()o.active_model_name = None@@ -1592,9 +1585,9 @@ def test_load_model_aborts_when_cancelled_during_spawn(monkeypatch):# recheck the marker once the child exists and tear the orphaned worker down,# instead of waiting for "loaded" and publishing a model /unload already# reported as unloaded (a live subprocess nothing later reaps).- import typesfrom utils import transformers_version as tv+ import typeso = _bare_orchestrator()o.active_model_name = None@@ -1640,10 +1633,10 @@ def test_unload_cancels_loading_gguf_off_gate(monkeypatch):# A still-loading GGUF (is_active, not is_loaded) must be cancelled off the gate:# /load holds the lifecycle gate for the whole load, so a gated unload would wait# it out. Assert the gate is never entered and unload_model() runs.- import asyncio as _asyncio- import routes.inference as rifrom core.inference import llama_keepwarm+ import asyncio as _asyncio+ import routes.inference as rigate_entered = {"v": False}@@ -1668,10 +1661,6 @@ def test_unload_cancels_loading_gguf_off_gate(monkeypatch):llama = _LlamaBackend()- class _Unsloth:- def get_loading_model(self):- return None # no Unsloth load in flight -> Unsloth fast path skipped-monkeypatch.setattr(ri, "get_llama_cpp_backend", lambda: llama)monkeypatch.setattr(ri, "get_inference_backend", lambda: _Unsloth())monkeypatch.setattr(llama_keepwarm, "inference_lifecycle_gate", lambda: _Gate())@@ -1688,10 +1677,10 @@ def test_unload_cancels_loading_gguf_off_gate(monkeypatch):def test_unload_loaded_gguf_still_uses_gate(monkeypatch):# Guard: an already-loaded GGUF (is_loaded True) is NOT caught by the off-gate# fast path; it goes through the gate as before.- import asyncio as _asyncio- import routes.inference as rifrom core.inference import llama_keepwarm+ import asyncio as _asyncio+ import routes.inference as rigate_entered = {"v": False}@@ -1716,10 +1705,6 @@ def test_unload_loaded_gguf_still_uses_gate(monkeypatch):llama = _LlamaBackend()- class _Unsloth:- def get_loading_model(self):- return None-monkeypatch.setattr(ri, "get_llama_cpp_backend", lambda: llama)monkeypatch.setattr(ri, "get_inference_backend", lambda: _Unsloth())monkeypatch.setattr(ri, "is_registered_native_path_label", lambda a, b: False)@@ -1742,10 +1727,10 @@ def test_unload_of_mismatched_loading_gguf_skips_off_gate_fast_path(monkeypatch)# load (e.g. a second tab unloading Y kills the load of X). A mismatched target must# fall through to the lifecycle gate (where, in production, it waits out X's /load and# then no-ops) instead of taking the off-gate teardown.- import asyncio as _asyncio- import routes.inference as rifrom core.inference import llama_keepwarm+ import asyncio as _asyncio+ import routes.inference as rigate_entered = {"v": False}@@ -1770,10 +1755,6 @@ def test_unload_of_mismatched_loading_gguf_skips_off_gate_fast_path(monkeypatch)llama = _LlamaBackend()- class _Unsloth:- def get_loading_model(self):- return None # no Unsloth load in flight -> Unsloth fast path skipped-monkeypatch.setattr(ri, "get_llama_cpp_backend", lambda: llama)monkeypatch.setattr(ri, "get_inference_backend", lambda: _Unsloth())monkeypatch.setattr(ri, "is_registered_native_path_label", lambda a, b: False)@@ -1841,9 +1822,9 @@ def test_cancel_load_reclears_state_when_racing_load_repopulates_during_teardown# mirrors, so without a second clear /unload reports success while the backend keeps# advertising a model whose worker was just killed. cancel_load must re-clear after the# teardown so no phantom loaded model survives.- import typesfrom utils import transformers_version as _tv+ import typeso = _bare_orchestrator()o.loading_models = {"m"}@@ -2046,9 +2027,9 @@ def test_load_model_aborts_publish_when_cancelled_after_wait_response(monkeypatc# advertises active_model_name/models for a model /unload already reported cancelled,# over a subprocess cancel_load just killed. The recheck must observe the discarded# marker and abort the publish.- import typesfrom utils import transformers_version as _tv+ import typeso = _bare_orchestrator()o.loading_models = {"m"}@@ -2195,6 +2176,7 @@ def test_start_dispatcher_refuses_while_unload_pending():# Direct unit guard: with an unload in progress (_unload_pending set under the# lifecycle lock by unload_model), _start_dispatcher must refuse and spawn nothing,# even though no dispatcher is currently running.+import queue as _queueo = _bare_orchestrator()@@ -2216,6 +2198,7 @@ def test_start_dispatcher_resumes_after_unload_clears():# Guard the other direction: once the unload finishes and clears _unload_pending, a# later compare request must be able to start the dispatcher again (the gate must not# wedge). Proves the refusal above is scoped to the unload, not permanent.+import queue as _queueo = _bare_orchestrator()@@ -2243,6 +2226,7 @@ def test_queued_start_behind_unload_stop_spawns_no_dispatcher():# it on the same lock. When the stop releases the lock the queued start must observe# _unload_pending (set under the lock ahead of the stop) and refuse: no fresh# dispatcher may be left running to steal the "unloaded" reply.+import queue as _queueo = _bare_orchestrator()@@ -2326,6 +2310,7 @@ def test_worker_ownership_follows_the_worker_not_the_consumer():# The subprocess runs one generation at a time and can start B while A's consumer has yet to# drain its mailbox. A must stop owning the worker the moment its gen_done is routed, else# a late Stop for A cancels B.+import queue as _queueo = _bare_orchestrator()@@ -2358,6 +2343,7 @@ def test_worker_ownership_follows_the_worker_not_the_consumer():def test_status_responses_do_not_transfer_worker_ownership():# Status lines are not an answer to any request; the dispatcher drops them before routing.+import queue as _queueo = _bare_orchestrator()@@ -2397,6 +2383,7 @@ def test_a_stale_mailbox_read_does_not_cancel_the_running_generation():# A dispatched consumer can still be draining tokens after the dispatcher retired its request# and started the next one. Stopping it then must tear down only its own stream: signalling# the shared worker event would end its successor.+import queue as _queueo = _bare_orchestrator()@@ -2451,7 +2438,6 @@ def test_a_dispatcher_started_mid_stream_still_reaches_the_direct_reader():# A compare request can start the dispatcher while an ordinary chat is streaming. The# dispatcher then owns resp_queue, and without a mailbox for the direct reader it dropped# that chat's tokens and its gen_done as unaddressed, hanging it.- import queue as _queueo = _bare_orchestrator()o._mailbox_lock = threading.Lock()@@ -2483,6 +2469,7 @@ def test_the_direct_reader_hands_back_a_compare_response_it_took():# The mirror race: this reader is already blocked on resp_queue when a compare request's# dispatcher starts, so it can take that request's response first. Consuming it would# corrupt this chat and hang the compare pane.+import queue as _queueo = _bare_orchestrator()@@ -2524,6 +2511,7 @@ def test_replacing_the_subprocess_clears_worker_scoped_state():# Ownership is keyed only by cancel-event identity, so a consumer still blocked on its# mailbox when the worker was replaced stayed recorded as the executor. A generation on# the fresh worker then failed _owns_worker and could not be stopped.+import queue as _queueo = _bare_orchestrator()@@ -2617,10 +2605,10 @@ def test_a_scoped_load_cancel_that_never_reports_back_releases_the_load():two leaves nobody to set it. An unbounded wait parks the load underinference_lifecycle_gate for the process lifetime, and asyncio.to_thread's executorthreads are non-daemon, so it also blocks exit."""+ from models.inference import LoadRequest, UnloadRequestimport asyncioimport routes.inference as inf- from models.inference import LoadRequest, UnloadRequestrequest = LoadRequest(model_path = "org/a", load_request_id = "handshake-drop")original_impl = inf._load_model_impldiff --git a/studio/backend/tests/test_orphaned_children.py b/studio/backend/tests/test_orphaned_children.pyindex 0f50c4d9224..ec66d735bc0 100644--- a/studio/backend/tests/test_orphaned_children.py+++ b/studio/backend/tests/test_orphaned_children.py@@ -194,7 +194,6 @@ def test_macos_style_orphans_are_recorded_and_reaped(tmp_path, monkeypatch):# decides, not the pid: a dead pid is recycled fast on a busy machine# (macOS especially), and an owner whose start time no longer matches is# a different process, so its recorded children are orphans.- import jsonpayload = json.loads(record.read_text())payload["owner_identity"] = "a-previous-studio-that-is-gone"@@ -241,7 +240,6 @@ def test_a_second_studio_does_not_erase_the_first_record(tmp_path, monkeypatch):try:pl.adopt_pid(child.pid)# A sibling Unsloth's record, written under its own pid.- import jsonother = records / "424242.json"other.write_text(@@ -347,8 +345,6 @@ def test_a_record_written_with_a_process_name_still_matches():def test_a_group_outliving_its_leader_is_still_reaped(tmp_path, monkeypatch):"""The shim can crash while the visual server holds the GPU; the record'spid is then gone and the group is the only handle left."""- import json-from utils import process_lifetime as plif pl._is_windows():@@ -442,11 +438,10 @@ def test_a_surviving_child_stays_in_the_record(tmp_path, monkeypatch):def test_concurrent_adopts_all_survive(tmp_path, monkeypatch):"""Two threads adopting at once must not lose either pid."""- import json- import threading-from utils import process_lifetime as pl+ import threading+directory = tmp_path / "children"monkeypatch.setenv("UNSLOTH_STUDIO_CHILD_RECORD", str(directory))pl._tracked_pids.clear()@@ -490,7 +485,6 @@ def test_a_survivor_keeps_its_record_through_a_clean_shutdown(tmp_path, monkeypapl.clear_breadcrumb()record = directory / f"{os.getpid()}.json"assert record.is_file(), "the only handle on the survivor was deleted"- import jsonassert [e["pid"] for e in json.loads(record.read_text())["children"]] == [stubborn.pid]finally:@@ -515,8 +509,6 @@ def test_a_confirmed_shutdown_still_clears_the_record(tmp_path, monkeypatch):def test_a_live_owner_survives_an_unreadable_identity(tmp_path, monkeypatch):"""A `ps` that failed for a moment must not cost a running Unsloth itssidecars."""- import json-from utils import process_lifetime as pldirectory = tmp_path / "children"@@ -583,10 +575,10 @@ def test_the_status_is_not_claimed_when_prctl_is_blocked(monkeypatch):def test_the_probe_does_not_arm_anything(monkeypatch):"""PR_GET_PDEATHSIG is read-only, so probing must leave our own setting be."""- import ctypes-from utils import process_lifetime as pl+ import ctypes+if not pl._is_linux():pytest.skip("Linux only")libc = ctypes.CDLL("libc.so.6", use_errno = True)@@ -600,11 +592,10 @@ def test_the_probe_does_not_arm_anything(monkeypatch):def test_the_sweep_snapshot_is_taken_under_the_lock():"""Writes hold _record_lock, so the read has to as well."""+ from utils import process_lifetime as plimport astimport inspect- from utils import process_lifetime as pl-tree = ast.parse(inspect.getsource(pl.terminate_all))body = ast.dump(tree)assert "_record_lock" in body@@ -616,10 +607,10 @@ def test_the_sweep_snapshot_is_taken_under_the_lock():def test_the_probe_fails_when_only_the_read_is_permitted(monkeypatch):"""seccomp filters prctl on its first argument, so a working GET saysnothing about SET."""- import ctypes-from utils import process_lifetime as pl+ import ctypes+class _Libc:def prctl(self, op, *rest):return 0 if op == 2 else -1 # GET ok, SET rejected@@ -700,8 +691,6 @@ def test_a_windows_tool_tree_dies_with_its_job(monkeypatch):def test_the_windows_breadcrumb_fallback_takes_the_whole_tree(tmp_path, monkeypatch):"""Killing the leader alone strands its workers, and the record naming themis deleted straight after."""- import json-from utils import process_lifetime as plmonkeypatch.setattr(pl, "_is_windows", lambda: True)@@ -869,11 +858,10 @@ def test_the_windows_backstop_takes_the_whole_tree(tmp_path, monkeypatch):def test_the_windows_identity_probe_prototypes_every_handle_call():"""Without argtypes ctypes marshals a 64-bit HANDLE as c_int, so the handleis truncated and the probe leaks one per call."""+ from utils import process_lifetime as plimport astimport inspect- from utils import process_lifetime as pl-source = inspect.getsource(pl._pid_identity)tree = ast.parse(source)prototyped = {@@ -919,8 +907,6 @@ def test_a_retained_child_keeps_its_group(tmp_path, monkeypatch):def test_a_worker_record_is_revisited_after_its_owner_dies(tmp_path, monkeypatch):"""A worker's record is skipped while its owner lives, and that owner can beterminated later in the same sweep by the backend's own record."""- import json-from utils import process_lifetime as plmonkeypatch.setenv("UNSLOTH_STUDIO_CHILD_RECORD", str(tmp_path / "children"))@@ -985,10 +971,10 @@ def test_a_group_that_survives_sigkill_keeps_its_record(monkeypatch):def test_a_failed_taskkill_falls_through_to_the_leader(monkeypatch):"""check=False does not raise, so the status is the only signal."""- import subprocess as sp-from utils import process_lifetime as pl+ import subprocess as sp+monkeypatch.setattr(pl.os, "kill", lambda pid, sig: fallbacks.append(pid))fallbacks = []@@ -1011,8 +997,6 @@ def test_a_failed_taskkill_falls_through_to_the_leader(monkeypatch):def test_a_zombie_owner_does_not_shield_its_children(tmp_path, monkeypatch):"""os.kill(pid, 0) succeeds for a zombie, and the sweep runs once, so itssidecars would survive until another restart."""- import json-from utils import process_lifetime as plmonkeypatch.setenv("UNSLOTH_STUDIO_CHILD_RECORD", str(tmp_path / "children"))@@ -1045,8 +1029,6 @@ def test_a_zombie_owner_does_not_shield_its_children(tmp_path, monkeypatch):def test_a_group_that_cannot_be_taken_keeps_its_record(tmp_path, monkeypatch):"""_reap_orphaned_group returning False has to leave the breadcrumb, or thenext startup has no handle on the survivors."""- import json-from utils import process_lifetime as plmonkeypatch.setenv("UNSLOTH_STUDIO_CHILD_RECORD", str(tmp_path / "children"))@@ -1106,12 +1088,12 @@ def test_every_child_starting_path_checks_the_rearm():def test_a_tool_subprocess_is_recorded_while_it_runs(tmp_path, monkeypatch):"""A force quit mid-call on macOS would otherwise leave a session-leadingtool with nothing able to find it."""- monkeypatch.setenv("UNSLOTH_STUDIO_CHILD_RECORD", str(tmp_path / "children"))- monkeypatch.setenv("UNSLOTH_STUDIO_SANDBOX_HOME", str(tmp_path / "sb"))-from core.inference import toolsfrom utils import process_lifetime as pl+ monkeypatch.setenv("UNSLOTH_STUDIO_CHILD_RECORD", str(tmp_path / "children"))+ monkeypatch.setenv("UNSLOTH_STUDIO_SANDBOX_HOME", str(tmp_path / "sb"))+pl._tracked_pids.clear()pl._tracked_pgids.clear()tools._workdirs.clear()@@ -1132,8 +1114,6 @@ def test_a_tool_subprocess_is_recorded_while_it_runs(tmp_path, monkeypatch):def test_a_terminated_leader_leaving_a_group_keeps_its_record(tmp_path, monkeypatch):"""The dead-leader branch covered only a leader that was already gone."""- import json-from utils import process_lifetime as plmonkeypatch.setenv("UNSLOTH_STUDIO_CHILD_RECORD", str(tmp_path / "children"))@@ -1207,8 +1187,6 @@ def test_a_group_of_only_zombies_is_not_alive():def test_a_zombie_child_is_not_signalled_or_waited_on(tmp_path, monkeypatch):"""Under a non-reaping PID 1 a zombie answers every probe, so terminating itburns the full grace period per record and reaps nothing."""- import json-from utils import process_lifetime as plmonkeypatch.setenv("UNSLOTH_STUDIO_CHILD_RECORD", str(tmp_path / "children"))@@ -1244,11 +1222,10 @@ def test_a_zombie_child_is_not_signalled_or_waited_on(tmp_path, monkeypatch):def test_the_component_installer_is_recorded_while_it_runs():"""It rewrites files in place, so one surviving a crash overlaps the nextlaunch."""+ from utils.prebuilt import update_flowimport astimport inspect- from utils.prebuilt import update_flow-tree = ast.parse(inspect.getsource(update_flow.stream_installer))body = ast.dump(tree)assert "adopt_pid" in body, "the installer is never recorded"@@ -1331,9 +1308,8 @@ def test_the_component_installer_stays_in_the_backend_group():"""The desktop stop path force-kills this backend's group, so a session ofits own would let the installer keep rewriting files after the app reportsthe backend stopped."""- import inspect-from utils.prebuilt import update_flow+ import inspectsource = inspect.getsource(update_flow.stream_installer)assert "start_new_session = " not in source@@ -1385,8 +1361,6 @@ def test_a_fork_child_does_not_claim_the_parents_children(tmp_path, monkeypatch)def test_a_missing_child_identity_is_filled_in_later(tmp_path, monkeypatch):"""None is permanent otherwise: nothing signals an entry it cannot verify,so that child outlives every shutdown."""- import json-from utils import process_lifetime as plmonkeypatch.setenv("UNSLOTH_STUDIO_CHILD_RECORD", str(tmp_path / "children"))@@ -1435,10 +1409,10 @@ def test_adoption_installs_the_fork_reset(monkeypatch):def test_a_group_of_nothing_but_a_zombie_costs_no_grace_period(monkeypatch):"""killpg(pgid, 0) answers for a zombie, and where pid 1 does not reap itstays that way, so every stale record would wait out the timeout."""- import time as _time-from utils import process_lifetime as pl+ import time as _time+monkeypatch.setattr(pl, "_is_windows", lambda: False)monkeypatch.setattr(os, "killpg", lambda pgid, sig: None)monkeypatch.setattr(pl, "_group_has_members", lambda pgid: False)@@ -1451,10 +1425,10 @@ def test_a_group_of_nothing_but_a_zombie_costs_no_grace_period(monkeypatch):def test_an_unanswerable_group_query_is_not_an_empty_group(monkeypatch):"""Reporting empty for a failed ps lets forget_pid drop the only record ofa live descendant."""- import subprocess as _subprocess-from utils import process_lifetime as pl+ import subprocess as _subprocess+monkeypatch.setattr(pl, "_is_linux", lambda: False)monkeypatch.setattr(pl, "_is_windows", lambda: False)monkeypatch.setattr(sys, "platform", "darwin")@@ -2248,9 +2222,8 @@ def test_announced_children_survive_two_threads_draining_at_once():timer does not stop a callback that already began. A bare`while announced: announced.pop()` raises KeyError out of whichever threadloses that race, replacing the installer error the caller should see."""- import inspect-from utils.prebuilt import update_flow+ import inspectannounced = update_flow.AnnouncedChildren()assert announced.take() is None, "an empty drain must end the loop, not raise"