Skip to content

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.

3.5 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Repeated GGUF header reads demote their log lines, without changing what is detected."""

from __future__ import annotations

import logging

import pytest

from core.inference import llama_cpp


@pytest.fixture(autouse = True)
def _clear_seen():
    llama_cpp._GGUF_METADATA_LOGGED.clear()
    yield
    llama_cpp._GGUF_METADATA_LOGGED.clear()


def _gguf(
    tmp_path,
    name = "m.gguf",
    body = b"x" * 64,
):
    p = tmp_path / name
    p.write_bytes(body)
    return str(p)


def test_only_the_first_read_of_a_file_speaks_up(tmp_path):
    path = _gguf(tmp_path)
    assert llama_cpp._note_gguf_metadata_read(path) is True
    for _ in range(10):
        assert llama_cpp._note_gguf_metadata_read(path) is False


def test_a_changed_file_is_described_again(tmp_path):
    """Keyed on identity, not name: a rebuilt GGUF at the same path is worth stating."""
    path = _gguf(tmp_path)
    assert llama_cpp._note_gguf_metadata_read(path) is True
    import os
    import time

    time.sleep(0.01)
    with open(path, "wb") as fh:
        fh.write(b"y" * 128)
    os.utime(path, None)
    assert llama_cpp._note_gguf_metadata_read(path) is True


def test_an_unstattable_path_still_logs(tmp_path):
    """Fails open: an unidentifiable read is better said out loud than swallowed."""
    assert llama_cpp._note_gguf_metadata_read(str(tmp_path / "gone.gguf")) is True


def test_the_cache_is_bounded(tmp_path):
    for i in range(llama_cpp._GGUF_METADATA_LOGGED_MAX + 5):
        llama_cpp._note_gguf_metadata_read(_gguf(tmp_path, f"m{i}.gguf"))
    assert len(llama_cpp._GGUF_METADATA_LOGGED) <= llama_cpp._GGUF_METADATA_LOGGED_MAX


TEMPLATE = (
    "{% if enable_thinking %}<think>{% endif %}"
    "{% if preserve_thinking %}keep{% endif %}"
    # The exact literal the tool detector matches.
    "{% if tools %}{{ tools }}{% endif %}"
)


def test_debug_level_moves_the_capability_lines_off_info(monkeypatch):
    """Assert on the logger: structlog bypasses caplog, so a caplog test would miss the level."""
    seen: list[tuple[str, str]] = []
    for level in ("info", "debug"):
        monkeypatch.setattr(
            llama_cpp.logger,
            level,
            lambda msg, *a, _lv = level, **k: seen.append((_lv, str(msg))),
        )

    llama_cpp.detect_reasoning_flags(TEMPLATE, "qwen3.8", log_source = "GGUF metadata")
    first = list(seen)
    seen.clear()
    llama_cpp.detect_reasoning_flags(
        TEMPLATE, "qwen3.8", log_source = "GGUF metadata", log_level = "debug"
    )
    repeat = list(seen)

    assert [lv for lv, _ in first] == ["info"] * len(
        first
    ) and first, "the first read must still describe the model at info"
    assert repeat, "a repeat must still be logged, just not at info"
    assert [lv for lv, _ in repeat] == ["debug"] * len(repeat)
    assert [m for _, m in first] == [m for _, m in repeat]


def test_the_level_does_not_change_what_is_detected():
    """Demoting the line must not drop a detected capability, which would hide a regression."""
    loud = llama_cpp.detect_reasoning_flags(TEMPLATE, "qwen3.8", log_source = "GGUF metadata")
    quiet = llama_cpp.detect_reasoning_flags(
        TEMPLATE, "qwen3.8", log_source = "GGUF metadata", log_level = "debug"
    )
    assert loud == quiet
    assert loud["supports_reasoning"] is True
    assert loud["supports_preserve_thinking"] is True
    assert loud["supports_tools"] is True