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.

7.9 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

"""A resume is judged on the commit the ref resolved to, since a ref string can move between runs."""

from __future__ import annotations

import json
import sys
import types
from pathlib import Path

import pytest

sys.path.insert(0, str(Path(__file__).resolve().parents[3]))

import studiobench.__main__ as sb  # noqa: E402
from studiobench import pacer as pacer_mod  # noqa: E402
from studiobench.runtime import browser as browser_mod  # noqa: E402
from studiobench.runtime import bundle_guard, lifecycle  # noqa: E402
from studiobench.runtime import session as session_mod  # noqa: E402
from studiobench.runtime.lifecycle import StudioAuth, StudioInstall  # noqa: E402
from studiobench.runtime.types import Paths  # noqa: E402


class _Verdict:
    production = True
    bundle_type = 0
    reason = "production build"

    def as_dict(self) -> dict:
        return {"production": True, "reason": self.reason}


class _Bundle:
    # Resolve the engine like browser.launch, or a fixed stub looks like an engine change off Linux/macOS.
    engine = browser_mod.default_engine()[0]
    engine_note = "stubbed for this test"
    browser = context = page = cdp = None

    def close(self) -> None:
        pass


class _Pacer:
    base_url = "http://127.0.0.1:65535"

    def __init__(self) -> None:
        self.state = types.SimpleNamespace(model_ids = [])

    def start(self):
        return self

    def stop(self) -> None:
        pass


class _Runner:
    def __init__(self, **kwargs) -> None:
        self.kwargs = kwargs

    def run(self, cell, plan) -> dict:
        return {
            "cell_id": cell.cell_id,
            "completed": True,
            "actions": [{"action": "keystroke", "ran": True}],
        }


@pytest.fixture
def studio(monkeypatch, tmp_path):
    """Every seam `run()` reaches outside this process. `commits` is the knob: which commit each
    ref resolves to on THIS invocation, which is what moving a branch means here."""

    state = {"commits": {}, "stopped": [], "out": tmp_path / "out"}

    def fake_install(ref, home, *args, **kwargs):
        install = StudioInstall(home = Path(home), repo = Path(home).parent / "repo", branch = ref)
        # setattr so the fixture builds against a StudioInstall without a commit field.
        install.commit = state["commits"].get(ref, f"c-{ref}-1")
        return install

    def fake_launch(install, port, log_path, *args, **kwargs):
        install.port = port
        install.pid = 90_000 + port
        install.bootstrap_password = "secret"
        return install

    monkeypatch.setattr(lifecycle, "install_studio", fake_install)
    monkeypatch.setattr(lifecycle, "launch_studio", fake_launch)
    monkeypatch.setattr(lifecycle, "stop_studio", lambda install: state["stopped"].append(install))
    monkeypatch.setattr(lifecycle, "wait_for_healthz", lambda *a, **k: True)
    monkeypatch.setattr(
        lifecycle,
        "authenticate",
        lambda base_url, username, password: StudioAuth(
            access_token = "t",
            refresh_token = "r",
            base_url = base_url,
            username = username,
            password = password,
        ),
    )
    monkeypatch.setattr(lifecycle, "register_provider", lambda *a, **k: "provider-1")
    monkeypatch.setattr(bundle_guard, "check_bundle", lambda url: _Verdict())
    monkeypatch.setattr(browser_mod, "launch", lambda *a, **k: _Bundle())
    monkeypatch.setattr(
        browser_mod,
        "install_wall_clock_watchdog",
        lambda *a, **k: types.SimpleNamespace(cancel = lambda: None),
    )
    monkeypatch.setattr(pacer_mod, "Pacer", _Pacer)
    monkeypatch.setattr(session_mod, "CellRunner", _Runner)
    return state


def _args(state, *extra):
    return sb.parse_args(
        ["--tier", "quick", "--rungs", "1K", "--out", str(state["out"])] + list(extra)
    )


def _rows(state):
    path = Paths.under(state["out"]).payload_jsonl
    return [json.loads(line) for line in path.read_text(encoding = "utf-8").splitlines() if line]


def test_a_run_records_the_commit_its_ref_resolved_to(studio):
    assert sb.run(_args(studio, "--branch", "main")) == 0

    meta = [r for r in _rows(studio) if r.get("row_type") == "run_meta"][0]
    assert meta["studio_ref"] == "main"
    assert meta["studio_commit"] == "c-main-1"


def test_an_attached_studio_records_no_commit(studio):
    """The build behind a URL is not visible from here, and a blank is the honest answer."""

    assert sb.run(_args(studio, "--attach", "http://127.0.0.1:5310")) == 0

    meta = [r for r in _rows(studio) if r.get("row_type") == "run_meta"][0]
    assert meta["studio_commit"] == ""


def test_a_resume_after_the_branch_moved_is_refused(studio):
    assert sb.run(_args(studio, "--branch", "main")) == 0

    studio["commits"]["main"] = "c-main-2"
    with pytest.raises(SystemExit) as excinfo:
        sb.run(_args(studio, "--branch", "main", "--resume"))

    message = str(excinfo.value)
    assert "studio_commit" in message
    assert "c-main-1" in message and "c-main-2" in message


def test_the_studios_that_refusal_launched_are_released(studio):
    """The refusal happens inside the setup guard, so it may not leak the servers it needed."""

    assert sb.run(_args(studio, "--branch", "main")) == 0
    studio["stopped"].clear()

    studio["commits"]["main"] = "c-main-2"
    with pytest.raises(SystemExit):
        sb.run(_args(studio, "--branch", "main", "--resume"))

    assert [i.branch for i in studio["stopped"]] == ["main"]


def test_a_resume_after_the_treatment_moved_is_refused(studio):
    """The second side gets the same rule, out of `ab_plan` rather than `run_meta`."""

    assert sb.run(_args(studio, "--branch", "main", "--ab", "fix"), ab_ref = "fix") == 0

    studio["commits"]["fix"] = "c-fix-2"
    with pytest.raises(SystemExit) as excinfo:
        sb.run(_args(studio, "--branch", "main", "--ab", "fix", "--resume"), ab_ref = "fix")

    message = str(excinfo.value)
    assert "treatment_commit" in message
    assert "c-fix-1" in message and "c-fix-2" in message


def test_the_same_commit_still_resumes(studio):
    """Resuming the same build must still skip its cells and exit 0 with --resume."""

    assert sb.run(_args(studio, "--branch", "main")) == 0
    assert sb.run(_args(studio, "--branch", "main", "--resume")) == 0


def test_a_payload_recorded_before_commits_were_written_still_resumes(studio):
    """The back-compatibility control, and the same rule `recorded_identities` already applies:
    an axis a payload never declared cannot be a difference."""

    assert sb.run(_args(studio, "--branch", "main")) == 0

    path = Paths.under(studio["out"]).payload_jsonl
    kept = []
    for row in _rows(studio):
        row.pop("studio_commit", None)
        kept.append(json.dumps(row))
    path.write_text("\n".join(kept) + "\n", encoding = "utf-8")

    studio["commits"]["main"] = "c-main-2"
    assert sb.run(_args(studio, "--branch", "main", "--resume")) == 0


def test_an_attached_resume_is_not_refused_for_having_no_commit(studio):
    """An attached resume is not refused for having no commit to offer; it still returns 0."""

    assert sb.run(_args(studio, "--attach", "http://127.0.0.1:5310")) == 0
    assert sb.run(_args(studio, "--attach", "http://127.0.0.1:5310", "--resume")) == 0


def test_a_fresh_run_onto_a_moved_branch_is_not_refused(studio):
    """And the last one: this is a `--resume` rule. Without it the payload is archived and a new
    one is started, which was always the right answer for a different build."""

    assert sb.run(_args(studio, "--branch", "main")) == 0

    studio["commits"]["main"] = "c-main-2"
    assert sb.run(_args(studio, "--branch", "main")) == 0

    meta = [r for r in _rows(studio) if r.get("row_type") == "run_meta"]
    assert [m["studio_commit"] for m in meta] == ["c-main-2"]


if __name__ == "__main__":
    raise SystemExit(pytest.main([__file__, "-q"]))