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.

20.5 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-Present the Unsloth team. See /studio/LICENSE.AGPL-3.0

"""Do not stamp the commit on a failed copy, or the refresh treats the notebook as synced."""

from __future__ import annotations

import hashlib
import os
import shutil
import subprocess
from pathlib import Path

import pytest

REPO_ROOT = Path(__file__).resolve().parents[2]
SYNC = REPO_ROOT / "docker" / "unsloth_sync_notebooks.sh"

TEMPLATE_COMMIT = "a" * 40

behavioural = pytest.mark.skipif(
    any(shutil.which(tool) is None for tool in ("bash", "sha256sum")),
    reason = "needs bash and sha256sum",
)


def _sha256(path: Path) -> str:
    return hashlib.sha256(path.read_bytes()).hexdigest()


def _template(tmp_path: Path) -> Path:
    template = tmp_path / "template"
    (template / "sub").mkdir(parents = True)
    (template / "a.ipynb").write_text("A", encoding = "utf-8")
    (template / "sub" / "b.ipynb").write_text("B", encoding = "utf-8")
    (template / ".unsloth_template_commit").write_text(TEMPLATE_COMMIT + "\n", encoding = "utf-8")
    return template


def _run(tmp_path: Path, template: Path, dest: Path) -> subprocess.CompletedProcess:
    return subprocess.run(
        ["bash", str(SYNC)],
        capture_output = True,
        text = True,
        timeout = 300,
        env = dict(
            os.environ,
            UNSLOTH_NOTEBOOKS_TEMPLATE = str(template),
            UNSLOTH_NOTEBOOKS_DIR = str(dest),
            UNSLOTH_SKIP_NOTEBOOK_REFRESH = "1",
            UNSLOTH_SKIP_NOTEBOOK_VIEW = "1",
            UNSLOTH_KEEP_COLAB_INTRO = "1",
        ),
    )


def _state(dest: Path) -> dict:
    path = dest / ".unsloth_sync_state"
    if not path.is_file():
        return {}
    out = {}
    for line in path.read_text(encoding = "utf-8").splitlines():
        parts = line.split("  ", 1)
        if len(parts) == 2:
            out[parts[1]] = parts[0]
    return out


@behavioural
def test_a_failed_copy_leaves_the_commit_unstamped_and_retries_next_start(tmp_path: Path):
    template = _template(tmp_path)
    dest = tmp_path / "dest"
    dest.mkdir()
    # A regular file, so the copy fails with ENOTDIR even as root.
    (dest / "sub").write_text("blocked", encoding = "utf-8")

    first = _run(tmp_path, template, dest)
    assert first.returncode == 0, first.stdout + first.stderr

    assert (dest / "a.ipynb").read_text(encoding = "utf-8") == "A"
    assert "a.ipynb" in _state(dest)
    assert "sub/b.ipynb" not in _state(dest), "a copy that failed must not be recorded"
    assert not (dest / ".unsloth_sync_commit").exists(), (
        "stamping the template commit after an incomplete populate makes the "
        "phase 2 refresh exit early, so the missing notebook never comes back"
    )
    assert (dest / ".unsloth_sync_partial").exists()

    (dest / "sub").unlink()

    second = _run(tmp_path, template, dest)
    assert second.returncode == 0, second.stdout + second.stderr

    assert (dest / "sub" / "b.ipynb").read_text(encoding = "utf-8") == "B"
    assert _state(dest)["sub/b.ipynb"] == _sha256(dest / "sub" / "b.ipynb")
    assert _state(dest)["a.ipynb"] == _sha256(dest / "a.ipynb")
    assert (dest / ".unsloth_sync_commit").read_text(encoding = "utf-8").strip() == (TEMPLATE_COMMIT)
    assert not (dest / ".unsloth_sync_partial").exists()


@behavioural
def test_a_clean_populate_stamps_the_commit_and_does_not_re_run(tmp_path: Path):
    template = _template(tmp_path)
    dest = tmp_path / "dest"
    dest.mkdir()

    assert _run(tmp_path, template, dest).returncode == 0
    assert (dest / ".unsloth_sync_commit").read_text(encoding = "utf-8").strip() == (TEMPLATE_COMMIT)
    assert not (dest / ".unsloth_sync_partial").exists()
    before = _state(dest)
    assert set(before) == {"a.ipynb", "sub/b.ipynb"}

    # Re-running phase 1 would drop this file's record, so identical state proves a skip.
    (dest / "a.ipynb").write_text("edited", encoding = "utf-8")

    assert _run(tmp_path, template, dest).returncode == 0
    assert _state(dest) == before
    assert (dest / "a.ipynb").read_text(encoding = "utf-8") == "edited"


@behavioural
def test_the_partial_marker_is_not_recorded_as_a_notebook(tmp_path: Path):
    # record_state() walks every file, so an unskipped dotfile would sync as a notebook.
    template = _template(tmp_path)
    dest = tmp_path / "dest"
    dest.mkdir()
    (dest / "sub").write_text("blocked", encoding = "utf-8")

    assert _run(tmp_path, template, dest).returncode == 0
    assert (dest / ".unsloth_sync_partial").exists()
    assert ".unsloth_sync_partial" not in _state(dest)

    source = SYNC.read_text(encoding = "utf-8")
    skip = source[source.index("record_state() {") :]
    skip = skip[: skip.index("printf")]
    assert ".unsloth_sync_partial" in skip


@behavioural
def test_the_retry_keeps_records_the_refresh_added(tmp_path: Path):
    """A retry rebuilds the state from the TEMPLATE alone, so anything the refresh did
    in between is thrown away: a template file whose bytes upstream changed now differs
    from the template and hits the "kept existing user file" branch, and a notebook that
    exists only upstream is never visited at all. Both become user-owned for good, while
    the commit marker is stamped anyway so it looks converged."""
    template = _template(tmp_path)
    dest = tmp_path / "dest"
    dest.mkdir()
    (dest / "sub").write_text("blocked", encoding = "utf-8")

    assert _run(tmp_path, template, dest).returncode == 0
    assert (dest / ".unsloth_sync_partial").exists()

    (dest / "a.ipynb").write_text("A-v2-from-upstream", encoding = "utf-8")
    (dest / "remote_only.ipynb").write_text("R", encoding = "utf-8")
    (dest / ".unsloth_sync_state").write_text(
        f"{_sha256(dest / 'a.ipynb')}  a.ipynb\n"
        f"{_sha256(dest / 'remote_only.ipynb')}  remote_only.ipynb\n",
        encoding = "utf-8",
    )

    (dest / "sub").unlink()
    second = _run(tmp_path, template, dest)
    assert second.returncode == 0, second.stdout + second.stderr

    state = _state(dest)
    assert state.get("a.ipynb") == _sha256(
        dest / "a.ipynb"
    ), "the refreshed copy is ours, not the user's; dropping its record freezes it"
    assert state.get("remote_only.ipynb") == _sha256(
        dest / "remote_only.ipynb"
    ), "a notebook that exists only upstream is never walked by the populate loop"
    assert state.get("sub/b.ipynb") == _sha256(dest / "sub" / "b.ipynb")
    assert (dest / "a.ipynb").read_text(
        encoding = "utf-8"
    ) == "A-v2-from-upstream", "the retry must not overwrite the newer copy with the baked template"
    assert len(state) == 3, state


@behavioural
def test_a_first_boot_records_only_what_it_populated(tmp_path: Path):
    """Non-vacuity for the merge above: with no prior state there is nothing to carry
    forward, so a stray file in DEST must not acquire a record."""
    template = _template(tmp_path)
    dest = tmp_path / "dest"
    dest.mkdir()
    (dest / "not_ours.ipynb").write_text("N", encoding = "utf-8")

    assert _run(tmp_path, template, dest).returncode == 0
    assert set(_state(dest)) == {"a.ipynb", "sub/b.ipynb"}


# Restoring from the baked template can move a refreshed notebook backwards, and phase 2
# exits on `remote == last`, so the sync marker must be cleared.

UPSTREAM_COMMIT = "b" * 40


def _restored_run(tmp_path: Path, template: Path, dest: Path, recorded: str, body: str | None):
    """State says `recorded` for a.ipynb; `body` is what is on disk, None to delete it."""
    (dest / ".unsloth_sync_state").write_text(
        f"{recorded}  a.ipynb\n{_sha256(template / 'sub' / 'b.ipynb')}  sub/b.ipynb\n",
        encoding = "utf-8",
    )
    (dest / ".unsloth_sync_commit").write_text(UPSTREAM_COMMIT + "\n", encoding = "utf-8")
    if body is None:
        (dest / "a.ipynb").unlink()
    else:
        (dest / "a.ipynb").write_text(body, encoding = "utf-8")
    return _run(tmp_path, template, dest)


@behavioural
def test_a_notebook_restored_backwards_drops_the_sync_marker(tmp_path: Path):
    template, dest = _template(tmp_path), tmp_path / "dest"
    assert _run(tmp_path, template, dest).returncode == 0

    run = _restored_run(tmp_path, template, dest, "c" * 64, None)
    assert run.returncode == 0, run.stdout + run.stderr

    assert (dest / "a.ipynb").read_text(encoding = "utf-8") == "A", "it was restored"
    assert not (dest / ".unsloth_sync_commit").is_file(), (
        "the marker survived a downgrade, so the refresh exits on remote == last and "
        "the notebook stays on the image's older copy"
    )
    assert _state(dest)["a.ipynb"] == _sha256(template / "a.ipynb")
    assert "1 needing a refresh" in run.stdout, run.stdout


@behavioural
def test_a_restore_that_changes_nothing_keeps_the_marker(tmp_path: Path):
    """Non-vacuity and the cost control: an ordinary delete of a notebook that never
    moved past the image must not force a full clone on the next start."""
    template, dest = _template(tmp_path), tmp_path / "dest"
    assert _run(tmp_path, template, dest).returncode == 0

    run = _restored_run(tmp_path, template, dest, _sha256(template / "a.ipynb"), None)
    assert run.returncode == 0, run.stdout + run.stderr

    assert (dest / "a.ipynb").read_text(encoding = "utf-8") == "A"
    assert (dest / ".unsloth_sync_commit").read_text(encoding = "utf-8").strip() == UPSTREAM_COMMIT
    assert "0 needing a refresh" in run.stdout, run.stdout


@behavioural
def test_a_notebook_still_on_disk_is_not_touched(tmp_path: Path):
    """The restore is only for files that are GONE; a user's edit stays and keeps the
    marker, because nothing was walked backwards."""
    template, dest = _template(tmp_path), tmp_path / "dest"
    assert _run(tmp_path, template, dest).returncode == 0

    run = _restored_run(tmp_path, template, dest, "c" * 64, "MY OWN WORK")
    assert run.returncode == 0, run.stdout + run.stderr

    assert (dest / "a.ipynb").read_text(encoding = "utf-8") == "MY OWN WORK"
    assert (dest / ".unsloth_sync_commit").read_text(encoding = "utf-8").strip() == UPSTREAM_COMMIT


@behavioural
def test_a_stale_unwritable_state_temp_is_not_published_as_our_state(tmp_path: Path):
    template = _template(tmp_path)
    dest = tmp_path / "dest"
    dest.mkdir()
    stale = dest / ".unsloth_sync_state.tmp"
    stale.write_text("deadbeef  stale.ipynb\n", encoding = "utf-8")
    stale.chmod(0o444)

    run = _run(tmp_path, template, dest)
    assert run.returncode == 0, run.stdout + run.stderr

    state = _state(dest)
    assert (
        "stale.ipynb" not in state
    ), "a leftover from an interrupted run was adopted as the state of this one"
    assert state == {
        "a.ipynb": _sha256(dest / "a.ipynb"),
        "sub/b.ipynb": _sha256(dest / "sub" / "b.ipynb"),
    }, state
    assert (dest / ".unsloth_sync_commit").read_text(encoding = "utf-8").strip() == TEMPLATE_COMMIT
    assert not (dest / ".unsloth_sync_partial").exists()


@behavioural
def test_an_unstageable_state_leaves_the_marker_off_instead_of_copying(tmp_path: Path):
    """Fail closed when the staging path cannot be cleared: unrecorded notebooks read as kept user
    edits, so the marker would be stamped over them; a directory there cannot be unlinked or
    truncated even by root."""
    template = _template(tmp_path)
    dest = tmp_path / "dest"
    dest.mkdir()
    (dest / ".unsloth_sync_state.tmp").mkdir()

    run = _run(tmp_path, template, dest)
    assert run.returncode == 0, run.stdout + run.stderr

    assert not (dest / ".unsloth_sync_state").exists(), run.stdout + run.stderr
    assert not (
        dest / ".unsloth_sync_commit"
    ).exists(), "stamping the commit here strands every notebook this run copied"
    assert (dest / ".unsloth_sync_partial").exists()
    assert not (dest / "a.ipynb").exists(), "nothing may be published without a record"
    assert "could not be staged" in run.stdout, run.stdout


# A failed state append after copy leaves an unrecorded file; RLIMIT_FSIZE simulates ENOSPC.
def _run_capped(template: Path, dest: Path, max_bytes: int):
    import resource
    import signal

    def _cap():
        # SIGXFSZ would kill the script; the shell must see the write error.
        signal.signal(signal.SIGXFSZ, signal.SIG_IGN)
        resource.setrlimit(resource.RLIMIT_FSIZE, (max_bytes, max_bytes))

    return subprocess.run(
        ["bash", str(SYNC)],
        capture_output = True,
        text = True,
        timeout = 300,
        preexec_fn = _cap,
        env = dict(
            os.environ,
            UNSLOTH_NOTEBOOKS_TEMPLATE = str(template),
            UNSLOTH_NOTEBOOKS_DIR = str(dest),
            UNSLOTH_SKIP_NOTEBOOK_REFRESH = "1",
            UNSLOTH_SKIP_NOTEBOOK_VIEW = "1",
            UNSLOTH_KEEP_COLAB_INTRO = "1",
        ),
    )


def _many_template(tmp_path: Path, count: int) -> Path:
    template = tmp_path / "template"
    template.mkdir(parents = True)
    for i in range(count):
        (template / f"n{i}.ipynb").write_text(f"N{i}", encoding = "utf-8")
    (template / ".unsloth_template_commit").write_text(TEMPLATE_COMMIT + "\n", encoding = "utf-8")
    return template


@behavioural
def test_a_failed_state_append_holds_back_the_commit_marker(tmp_path: Path):
    """A record lost after a successful copy must count as a population failure."""
    template = _many_template(tmp_path, 15)
    dest = tmp_path / "dest"
    dest.mkdir()

    run = _run_capped(template, dest, 512)
    assert run.returncode == 0, run.stdout + run.stderr

    copied = sorted(p.name for p in dest.glob("*.ipynb"))
    recorded = _state(dest)
    assert len(copied) > len(
        recorded
    ), "the cap did not bite; this test proves nothing unless some append failed"
    assert not (
        dest / ".unsloth_sync_commit"
    ).exists(), "notebooks copied but not recorded are user-owned for good once this is stamped"
    assert (dest / ".unsloth_sync_partial").exists(), run.stdout


@behavioural
def test_the_retry_after_a_failed_append_converges(tmp_path: Path):
    """And the retry must actually finish the job, not just defer forever."""
    template = _many_template(tmp_path, 15)
    dest = tmp_path / "dest"
    dest.mkdir()

    _run_capped(template, dest, 512)
    assert not (dest / ".unsloth_sync_commit").exists()

    run = _run(tmp_path, template, dest)
    assert run.returncode == 0, run.stdout + run.stderr
    assert len(_state(dest)) == 15, _state(dest)
    assert (dest / ".unsloth_sync_commit").exists(), run.stdout
    assert not (dest / ".unsloth_sync_partial").exists()


# Class guard: every append to a staged state file must handle or count its failure.
_STAGED_STATE_TARGETS = ('>> "$STATE.tmp"', '>> "$TMPSTATE"', '>> "$RS_TMP"')

_ACCOUNTED = (
    "|| populate_failed=",
    "|| failed=",
    "|| rs_ok=0",
    "|| unrecorded ",
    "|| drop_unrecordable ",
    "populate_failed=$((populate_failed + 1))",
    "failed=$((failed + 1))",
    "rs_ok=0",
)


def _exempt_lines(lines):
    """record_state() is dead code kept dead by a companion test, while record_tmpstate() is the
    shared append helper whose callers must handle the status it reports."""
    out = set()
    for name in ("record_state() {", "record_tmpstate() {"):
        start = next(i for i, l in enumerate(lines) if l.startswith(name))
        end = next(i for i in range(start, len(lines)) if lines[i] == "}")
        out.update(range(start, end + 1))
    return out


def test_every_staged_state_append_is_checked():
    lines = SYNC.read_text(encoding = "utf-8").splitlines()
    exempt = _exempt_lines(lines)
    unchecked = []
    for i, line in enumerate(lines):
        if i in exempt:
            continue
        if not any(t in line for t in _STAGED_STATE_TARGETS):
            continue
        window = " ".join(lines[i : i + 3])
        if not any(marker in window for marker in _ACCOUNTED):
            unchecked.append((i + 1, line.strip()))
    assert not unchecked, (
        "these appends to a staged state file neither handle their own failure nor "
        "have one counted immediately after, so a lost record would still publish a "
        "truncated state and advance the commit marker:\n"
        + "\n".join(f"  line {n}: {t}" for n, t in unchecked)
    )


def test_every_record_tmpstate_call_handles_a_failed_write():
    """Every record_tmpstate call must handle a failed write: roll the file back or retry the write."""
    lines = SYNC.read_text(encoding = "utf-8").splitlines()
    exempt = _exempt_lines(lines)
    bare = []
    for i, line in enumerate(lines):
        if i in exempt or "record_tmpstate " not in line:
            continue
        stripped = line.strip()
        if stripped.startswith("#"):
            continue
        if "||" in stripped or stripped.startswith("if record_tmpstate"):
            continue
        bare.append((i + 1, stripped))
    assert not bare, (
        "these record_tmpstate calls ignore a failed write, so the notebook stays on "
        "disk with no record and the next refresh treats it as a user edit:\n"
        + "\n".join(f"  line {n}: {t}" for n, t in bare)
    )


def test_a_failed_record_can_roll_the_notebook_back():
    """drop_unrecordable must only ever remove OUR copy, never a user edit."""
    body = SYNC.read_text(encoding = "utf-8")
    assert "drop_unrecordable() {" in body
    fn = body[body.index("drop_unrecordable() {") :]
    fn = fn[: fn.index("\n}")]
    assert (
        'hash_of "$_d"' in fn and "${LAST[$1]:-}" in fn
    ), "the rollback must be gated on the file still matching the last record"


def test_the_marker_advance_is_gated_on_the_failure_counters():
    """The guard above is only worth anything while the counters still hold the
    marker back."""
    body = SYNC.read_text(encoding = "utf-8")
    assert '[ "$failed" -eq 0 ] && [ "$published" -eq 1 ]' in body
    assert '[ "$populate_failed" -eq 0 ]' in body


def test_record_state_is_still_uncalled():
    """Any call site would make the unchecked append live, and its loop is a pipeline, so a failure
    counter set inside would be lost in the subshell; the exemption depends on this."""
    body = SYNC.read_text(encoding = "utf-8")
    calls = [
        ln.strip()
        for ln in body.splitlines()
        if "record_state" in ln
        and not ln.startswith("record_state() {")
        and not ln.lstrip().startswith("#")
    ]
    assert not calls, f"record_state gained a caller; the exemption is now unsound: {calls}"


# State must be staged beside $STATE (same fs, readable by later --user boots).
def _state_path(dest: Path) -> Path:
    return dest / ".unsloth_sync_state"


@behavioural
def test_an_unreadable_state_is_never_replaced_by_an_empty_one(tmp_path: Path):
    template = _template(tmp_path)
    dest = tmp_path / "dest"
    dest.mkdir()
    assert _run(tmp_path, template, dest).returncode == 0
    before = _state(dest)
    assert before, "nothing to protect; the populate did not record anything"

    _state_path(dest).chmod(0o000)
    try:
        run = _run(tmp_path, template, dest)
    finally:
        _state_path(dest).chmod(0o644)

    assert run.returncode == 0, run.stdout + run.stderr
    assert _state(dest) == before, (
        "the state was replaced while unreadable; every notebook it described is now "
        "read as a user edit and never updated again\n" + run.stdout + run.stderr
    )
    assert "unreadable" in run.stdout, run.stdout


@behavioural
def test_the_restore_loop_does_not_downgrade_the_state_file(tmp_path: Path):
    """It runs on every boot, so a mode or owner change here compounds."""
    template = _template(tmp_path)
    dest = tmp_path / "dest"
    dest.mkdir()
    _run(tmp_path, template, dest)
    first = _state_path(dest).stat().st_mode & 0o777

    _run(tmp_path, template, dest)
    second = _state_path(dest).stat().st_mode & 0o777

    assert second == first, f"the state mode changed across boots: {oct(first)} -> {oct(second)}"
    assert second & 0o044, (
        f"the state is not group/world readable ({oct(second)}); a root boot leaves it "
        f"unreadable to a later --user boot, which is the input above"
    )


def test_every_state_writer_stages_beside_the_state_file():
    """mktemp lands in /tmp, a different filesystem from $DEST under the documented
    bind mount, so the publish stops being an atomic same-directory rename."""
    body = SYNC.read_text(encoding = "utf-8")
    assert 'RS_TMP="$STATE.tmp"' in body, "the restore loop must stage beside $STATE"
    assert 'RS_TMP="$(mktemp)"' not in body


def test_the_restore_publish_failure_is_not_silent():
    """Its sibling branch three lines down is loud and drops the marker; this one used
    to swallow the failure while the 'restored N' success message still printed."""
    body = SYNC.read_text(encoding = "utf-8")
    block = body[body.index('mv "$RS_TMP" "$STATE"') :][:600]
    assert "could not be published" in block, block[:200]
    assert 'rm -f "$SYNCED"' in block, block[:200]