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.

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

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
. "$SCRIPT_DIR/_harness.sh"
# shellcheck source=tests/sh/_ps1_source.sh
. "$SCRIPT_DIR/_ps1_source.sh"
INSTALL_SH="$SCRIPT_DIR/../../install.sh"
INSTALL_PS1="$SCRIPT_DIR/../../install.ps1"
ROLLBACK_BLOCK=$(sed -n '/^_VENV_ROLLBACK_DIR=""/,/^trap '\''_on_install_signal 143'\'' TERM$/p' "$INSTALL_SH")
# Splicing without these helpers makes cases exit 127, which satisfies any "marker unchanged"
# expectation. printf, not $'\n': the workflow runs this file with `sh`.
MARKER_HELPER=$(awk '
    /^(_restore_uv_cache_marker|_record_uv_cache_choice|_absolutize_uv_cache_dir)\(\) \{/ { grab = 1 }
    grab { print }
    grab && /^}/ { grab = 0 }
' "$INSTALL_SH")
if ! printf '%s\n' "$MARKER_HELPER" | grep -q '^_restore_uv_cache_marker() {' \
   || ! printf '%s\n' "$MARKER_HELPER" | grep -q '^_record_uv_cache_choice() {' \
   || ! printf '%s\n' "$MARKER_HELPER" | grep -q '^_absolutize_uv_cache_dir() {'; then
    echo "  FAIL: could not extract the uv cache marker helpers from install.sh"
    exit 1
fi
ROLLBACK_BLOCK=$(printf '%s\n%s\n' "$MARKER_HELPER" "$ROLLBACK_BLOCK")
if ! printf '%s\n' "$ROLLBACK_BLOCK" | grep -q '^_on_install_signal() {'; then
    echo "  FAIL: could not extract rollback lifecycle block from install.sh"
    exit 1
fi

WORK=$(mktemp -d)
trap 'rm -rf "$WORK"' EXIT

run_signal_case() {
    _signal="$1"
    _expected_status="$2"
    _case_dir="$WORK/signal-$_signal"
    mkdir -p "$_case_dir/unsloth_studio"
    printf 'old\n' > "$_case_dir/unsloth_studio/generation"
    _harness="$_case_dir/harness.sh"
    {
        printf '%s\n' 'set -e'
        printf '%s\n' 'substep() { :; }'
        printf '%s\n' 'rollback_substep() { substep "$@"; }'
        printf '%s\n' 'C_WARN=""'
        printf "STUDIO_HOME='%s'\n" "$_case_dir"
        printf "VENV_DIR='%s/unsloth_studio'\n" "$_case_dir"
        printf '%s\n' "$ROLLBACK_BLOCK"
        printf '%s\n' '_start_studio_venv_replacement "$VENV_DIR"'
        printf '%s\n' 'mkdir -p "$VENV_DIR"'
        printf '%s\n' 'printf "partial\n" > "$VENV_DIR/generation"'
        printf 'kill -%s $$\n' "$_signal"
        printf '%s\n' 'exit 99'
    } > "$_harness"

    set +e
    dash "$_harness" >/dev/null 2>&1
    _status=$?
    set -e
    if [ "$_status" = "$_expected_status" ]; then
        ok "dash $_signal exits with $_expected_status"
    else
        bad "dash $_signal exits with $_expected_status (got $_status)"
    fi
    if [ "$(cat "$_case_dir/unsloth_studio/generation" 2>/dev/null)" = "old" ]; then
        ok "dash $_signal restores the previous environment"
    else
        bad "dash $_signal did not restore the previous environment"
    fi
    if ! find "$_case_dir" -maxdepth 1 -name 'unsloth_studio.rollback.*' -print -quit | grep -q .; then
        ok "dash $_signal leaves no rollback copy"
    else
        bad "dash $_signal left a rollback copy"
    fi
}

echo "=== install.sh signal rollback ==="
run_signal_case INT 130
run_signal_case TERM 143
run_signal_case HUP 129

echo "=== install.sh transition boundaries ==="
START_BOUNDARY_DIR="$WORK/start-boundary"
mkdir -p "$START_BOUNDARY_DIR/unsloth_studio"
printf 'old\n' > "$START_BOUNDARY_DIR/unsloth_studio/generation"
START_BOUNDARY_HARNESS="$START_BOUNDARY_DIR/harness.sh"
{
    printf '%s\n' 'set -e'
    printf '%s\n' 'substep() { :; }'
    printf '%s\n' 'rollback_substep() { substep "$@"; }'
    printf '%s\n' 'C_WARN=""'
    printf "STUDIO_HOME='%s'\n" "$START_BOUNDARY_DIR"
    printf "VENV_DIR='%s/unsloth_studio'\n" "$START_BOUNDARY_DIR"
    printf '%s\n' "$ROLLBACK_BLOCK"
    printf '%s\n' 'mv() { command mv "$@"; kill -TERM $$; }'
    printf '%s\n' '_start_studio_venv_replacement "$VENV_DIR"'
} > "$START_BOUNDARY_HARNESS"
set +e
dash "$START_BOUNDARY_HARNESS" >/dev/null 2>&1
_start_boundary_status=$?
set -e
if [ "$_start_boundary_status" -eq 143 ] \
   && [ "$(cat "$START_BOUNDARY_DIR/unsloth_studio/generation" 2>/dev/null)" = "old" ]; then
    ok "signal immediately after rollback rename restores the old environment"
else
    bad "rollback state was not published before rename"
fi

COMMIT_BOUNDARY_DIR="$WORK/commit-boundary"
mkdir -p "$COMMIT_BOUNDARY_DIR/unsloth_studio"
printf 'old\n' > "$COMMIT_BOUNDARY_DIR/unsloth_studio/generation"
COMMIT_BOUNDARY_HARNESS="$COMMIT_BOUNDARY_DIR/harness.sh"
{
    printf '%s\n' 'set -e'
    printf '%s\n' 'substep() { :; }'
    printf '%s\n' 'rollback_substep() { substep "$@"; }'
    printf '%s\n' 'C_WARN=""'
    printf "STUDIO_HOME='%s'\n" "$COMMIT_BOUNDARY_DIR"
    printf "VENV_DIR='%s/unsloth_studio'\n" "$COMMIT_BOUNDARY_DIR"
    printf '%s\n' "$ROLLBACK_BLOCK"
    printf '%s\n' '_start_studio_venv_replacement "$VENV_DIR"'
    printf '%s\n' 'mkdir -p "$VENV_DIR"'
    printf '%s\n' 'printf "new\n" > "$VENV_DIR/generation"'
    printf '%s\n' 'rm() { kill -TERM $$; }'
    printf '%s\n' '_commit_studio_venv_replacement'
} > "$COMMIT_BOUNDARY_HARNESS"
set +e
dash "$COMMIT_BOUNDARY_HARNESS" >/dev/null 2>&1
_commit_boundary_status=$?
set -e
if [ "$_commit_boundary_status" -eq 143 ] \
   && [ "$(cat "$COMMIT_BOUNDARY_DIR/unsloth_studio/generation" 2>/dev/null)" = "new" ]; then
    ok "signal during committed-backup deletion keeps the new environment"
else
    bad "signal during committed-backup deletion restored a partial backup"
fi

echo "=== install.sh successful cleanup ==="
PRUNE_DIR="$WORK/prune"
mkdir -p "$PRUNE_DIR/unsloth_studio"
printf 'old\n' > "$PRUNE_DIR/unsloth_studio/generation"
PRUNE_HARNESS="$PRUNE_DIR/harness.sh"
{
    printf '%s\n' 'set -e'
    printf '%s\n' 'substep() { :; }'
    printf '%s\n' 'rollback_substep() { substep "$@"; }'
    printf '%s\n' 'C_WARN=""'
    printf "STUDIO_HOME='%s'\n" "$PRUNE_DIR"
    printf "VENV_DIR='%s/unsloth_studio'\n" "$PRUNE_DIR"
    printf '%s\n' "$ROLLBACK_BLOCK"
    printf '%s\n' '_start_studio_venv_replacement "$VENV_DIR"'
    printf '%s\n' 'mkdir -p "$VENV_DIR"'
    printf '%s\n' 'printf "new\n" > "$VENV_DIR/generation"'
    printf '%s\n' 'mkdir "$STUDIO_HOME/unsloth_studio.rollback.20000101000000.999999999"'
    printf '%s\n' 'mkdir "$STUDIO_HOME/unsloth_studio.rollback.20000101000001.$$"'
    printf '%s\n' 'mkdir "$STUDIO_HOME/unsloth_studio.rollback.user-data"'
    printf '%s\n' 'mkdir "$STUDIO_HOME/outside"'
    printf '%s\n' 'ln -s "$STUDIO_HOME/outside" "$STUDIO_HOME/unsloth_studio.rollback.20000101000002.999999998"'
    printf '%s\n' '_commit_studio_venv_replacement'
} > "$PRUNE_HARNESS"

sh "$PRUNE_HARNESS" >/dev/null 2>&1
if [ "$(cat "$PRUNE_DIR/unsloth_studio/generation" 2>/dev/null)" = "new" ]; then
    ok "successful replacement keeps the new environment"
else
    bad "successful replacement lost the new environment"
fi
if [ ! -d "$PRUNE_DIR/unsloth_studio.rollback.20000101000000.999999999" ]; then
    ok "successful install removes an orphan from a dead PID"
else
    bad "successful install left an orphan from a dead PID"
fi
_active_count=$(find "$PRUNE_DIR" -maxdepth 1 -type d -name 'unsloth_studio.rollback.20000101000001.*' | wc -l)
if [ "$_active_count" -eq 1 ]; then
    ok "successful install preserves a concurrent installer's rollback"
else
    bad "successful install removed a concurrent installer's rollback"
fi
if [ -d "$PRUNE_DIR/unsloth_studio.rollback.user-data" ]; then
    ok "stale cleanup preserves names outside the generated format"
else
    bad "stale cleanup removed a non-generated rollback name"
fi
if [ -L "$PRUNE_DIR/unsloth_studio.rollback.20000101000002.999999998" ] \
   && [ -d "$PRUNE_DIR/outside" ]; then
    ok "stale cleanup does not follow rollback symlinks"
else
    bad "stale cleanup mutated a rollback symlink target"
fi

echo "=== install.sh commits before the post-setup tail ==="
# Nothing after studio setup returns may reach the exit trap that restores the old environment.
_commit_calls=$(grep -c '^[[:space:]]*_commit_studio_venv_replacement$' "$INSTALL_SH")
_commit_at=$(grep -n '^[[:space:]]*_commit_studio_venv_replacement$' "$INSTALL_SH" | head -1 | cut -d: -f1)
_setup_gate_at=$(grep -n '^if \[ "\$_SETUP_EXIT" -eq 0 \]; then$' "$INSTALL_SH" | head -1 | cut -d: -f1)
_shim_at=$(grep -n '^mkdir -p "\$_LOCAL_BIN"$' "$INSTALL_SH" | head -1 | cut -d: -f1)
if [ "$_commit_calls" -eq 1 ] && [ -n "$_commit_at" ] && [ -n "$_setup_gate_at" ] && [ -n "$_shim_at" ] \
   && [ "$_setup_gate_at" -lt "$_commit_at" ] && [ "$_commit_at" -lt "$_shim_at" ]; then
    ok "the replacement is committed inside the setup-succeeded gate, before anything is wired"
else
    bad "the replacement is committed too late (gate=$_setup_gate_at commit=$_commit_at shim=$_shim_at calls=$_commit_calls)"
fi
_gate_first=$(sed -n "$((_setup_gate_at + 1)),\$p" "$INSTALL_SH" \
    | grep -vE '^[[:space:]]*(#|$)' | head -1 | sed 's/^[[:space:]]*//')
if [ "$_gate_first" = "_commit_studio_venv_replacement" ]; then
    ok "nothing runs between studio setup succeeding and the commit"
else
    bad "the setup-succeeded gate runs something before the commit ($_gate_first)"
fi

# Both anchors are checked: without the closing one sed would run to EOF.
_tail_ends=$(grep -c '^if \[ "\$_SETUP_EXIT" -ne 0 \]; then$' "$INSTALL_SH")
TAIL_BLOCK=$(sed -n '/^if \[ "\$_SETUP_EXIT" -eq 0 \]; then$/,/^if \[ "\$_SETUP_EXIT" -ne 0 \]; then$/p' "$INSTALL_SH" \
    | sed '$d')
if [ "$_tail_ends" -ne 1 ] \
   || ! printf '%s\n' "$TAIL_BLOCK" | grep -q '^_persist_login_path_dir() {'; then
    echo "  FAIL: could not extract the post-setup tail from install.sh"
    exit 1
fi

write_tail_harness() {  # case dir, login shell, "no-exe" to leave the shim's target absent
    {
        printf '%s\n' 'set -e'
        printf '%s\n' 'substep() { printf "%s\n" "$1" >> "$STUDIO_HOME/steps.log"; }'
        printf '%s\n' 'rollback_substep() { substep "$@"; }'
        printf '%s\n' 'step() { printf "%s\n" "$2" >> "$STUDIO_HOME/steps.log"; }'
        printf '%s\n' 'tauri_clear_install_error() { :; }'
        printf '%s\n' 'create_studio_shortcuts() { return 0; }'
        printf '%s\n' 'TAURI_MODE=false'
        printf '%s\n' 'OS=linux'
        printf '%s\n' 'C_WARN=""'
        printf "STUDIO_HOME='%s'\n" "$1"
        printf "VENV_DIR='%s/unsloth_studio'\n" "$1"
        printf '%s\n' "$ROLLBACK_BLOCK"
        printf '%s\n' '_start_studio_venv_replacement "$VENV_DIR"'
        printf '%s\n' 'mkdir -p "$VENV_DIR/bin"'
        if [ "${3:-}" != no-exe ]; then
            printf '%s\n' 'printf "#!/bin/sh\\n" > "$VENV_DIR/bin/unsloth"'
            printf '%s\n' 'chmod +x "$VENV_DIR/bin/unsloth"'
        fi
        printf '%s\n' 'VENV_ABS_BIN="$VENV_DIR/bin"'
        printf '%s\n' 'printf "new\n" > "$VENV_DIR/generation"'
        printf '%s\n' '_SETUP_EXIT=0'
        printf "HOME='%s/home'\n" "$1"
        printf '%s\n' 'export HOME'
        printf "SHELL='%s'\n" "$2"
        printf '%s\n' 'export SHELL'
        # An exported ZDOTDIR would send the fixture's write to the developer's own ~/.zshrc.
        printf '%s\n' 'unset ZDOTDIR ZSH_VERSION UV_NO_MODIFY_PATH UV_UNMANAGED_INSTALL'
        printf '%s\n' '_LOCAL_BIN="$HOME/.local/bin"'
        printf '%s\n' '_STUDIO_HOME_REDIRECT=default'
        printf '%s\n' '_UNSLOTH_LOGIN_PATH="/usr/bin:/bin"'
        printf '%s\n' '_UNSLOTH_UV_BIN_DIR="$HOME/.local/uvbin"'
        printf '%s\n' "$TAIL_BLOCK"
    } > "$1/harness.sh"
}

# An unwritable profile costs the PATH entry it would have added, not the install.
run_readonly_profile_case() {
    _case="$1"
    _case_dir="$WORK/readonly-$_case"
    _case_home="$_case_dir/home"
    mkdir -p "$_case_dir/unsloth_studio" "$_case_home/.local/bin" \
        "$_case_home/$(dirname "$3")"
    printf 'old\n' > "$_case_dir/unsloth_studio/generation"
    printf '# unwritable\n' > "$_case_home/$3"
    chmod 444 "$_case_home/$3"
    if true 2>/dev/null >> "$_case_home/$3"; then
        bad "unwritable $_case profile could not be set up (this user appends to it regardless)"
        return 0
    fi
    write_tail_harness "$_case_dir" "$2"

    set +e
    sh "$_case_dir/harness.sh" >/dev/null 2>&1
    _status=$?
    set -e
    if [ "$_status" -eq 0 ]; then
        ok "an unwritable $_case profile does not fail the install"
    else
        bad "an unwritable $_case profile failed the install (exit $_status)"
    fi
    if [ "$(cat "$_case_home/$3")" = "# unwritable" ]; then
        ok "an unwritable $_case profile is left as it was"
    else
        bad "an unwritable $_case profile was modified"
    fi
    if grep -q "could not write $_case_home/$3; add ~/.local/bin" \
        "$_case_dir/steps.log" 2>/dev/null; then
        ok "an unwritable $_case profile is reported to the user"
    else
        bad "an unwritable $_case profile is silently skipped"
    fi
    if grep -q '/.local/uvbin' "$_case_home/.profile" 2>/dev/null; then
        ok "the profiles that can be written still get their PATH entry ($_case)"
    else
        bad "one unwritable $_case profile stopped the remaining profiles from being written"
    fi
}

run_readonly_profile_case zsh /bin/zsh .zshrc
run_readonly_profile_case fish /usr/bin/fish .config/fish/conf.d/unsloth.fish

# A real directory at the shim path is user data; refusing must cost the shim, not the env.
REFUSE_DIR="$WORK/tail-refusal"
mkdir -p "$REFUSE_DIR/unsloth_studio" "$REFUSE_DIR/home/.local/bin/unsloth"
printf 'old\n' > "$REFUSE_DIR/unsloth_studio/generation"
write_tail_harness "$REFUSE_DIR" /bin/bash
set +e
sh "$REFUSE_DIR/harness.sh" >/dev/null 2>&1
_refusal_status=$?
set -e
if [ "$_refusal_status" -eq 1 ]; then
    ok "a directory at the shim path still refuses the install"
else
    bad "a directory at the shim path no longer refuses the install (exit $_refusal_status)"
fi
if [ "$(cat "$REFUSE_DIR/unsloth_studio/generation" 2>/dev/null)" = "new" ]; then
    ok "a refused shim keeps the environment just installed"
else
    bad "a refused shim rolled back the environment just installed"
fi
if ! find "$REFUSE_DIR" -maxdepth 1 -name 'unsloth_studio.rollback.*' -print -quit | grep -q .; then
    ok "a refused shim leaves no rollback copy"
else
    bad "a refused shim left a rollback copy"
fi

# An unwritable bin dir only fails the install when its entry is not this run's shim.
run_readonly_bin_case() {  # name, what the existing entry points at, expected status, [no-exe]
    _bin_dir="$WORK/readonly-bin-$1"
    _bin_home="$_bin_dir/home"
    mkdir -p "$_bin_dir/unsloth_studio" "$_bin_home/.local/bin"
    printf 'old\n' > "$_bin_dir/unsloth_studio/generation"
    ln -sfn "$2" "$_bin_home/.local/bin/unsloth"
    chmod 555 "$_bin_home/.local/bin"
    if true 2>/dev/null > "$_bin_home/.local/bin/probe"; then
        rm -f "$_bin_home/.local/bin/probe"
        chmod 755 "$_bin_home/.local/bin"
        bad "unwritable bin directory holding $1 could not be set up (this user writes it anyway)"
        return 0
    fi
    write_tail_harness "$_bin_dir" /bin/bash "${4:-}"
    set +e
    sh "$_bin_dir/harness.sh" >/dev/null 2>"$_bin_dir/stderr"
    _bin_status=$?
    set -e
    chmod 755 "$_bin_home/.local/bin"
    if [ "$_bin_status" -eq "$3" ]; then
        ok "an unwritable bin directory holding $1 exits $3"
    else
        bad "an unwritable bin directory holding $1 exits $3 (got $_bin_status)"
    fi
    if [ "$3" -eq 0 ]; then
        if grep -q "kept the existing shim" "$_bin_dir/steps.log" 2>/dev/null; then
            ok "keeping the existing shim is reported rather than passed over in silence"
        else
            bad "keeping the existing shim is not reported"
        fi
    elif grep -qF "run '$_bin_dir/unsloth_studio/bin/unsloth' directly" "$_bin_dir/stderr"; then
        ok "refusing $1 says how to start Unsloth without the shim"
    else
        bad "refusing $1 does not say how to start Unsloth without the shim"
    fi
}

run_readonly_bin_case absolute-shim "$WORK/readonly-bin-absolute-shim/unsloth_studio/bin/unsloth" 0
run_readonly_bin_case relative-shim ../../../unsloth_studio/bin/unsloth 0
run_readonly_bin_case another-command /bin/false 1
run_readonly_bin_case dangling-shim \
    "$WORK/readonly-bin-dangling-shim/unsloth_studio/bin/unsloth" 1 no-exe

echo "=== install.ps1 rollback wiring ==="
# install.ps1 emits a launcher via a here-string with its own functions and `} finally {`.
INSTALL_PS1_CODE=$(ps1_code "$INSTALL_PS1")
if printf '%s\n' "$INSTALL_PS1_CODE" | grep -q '^    function Remove-StaleStudioVenvRollbacks {' \
   && printf '%s\n' "$INSTALL_PS1_CODE" | grep -q '^    Remove-StaleStudioVenvRollbacks$'; then
    ok "Windows installer prunes stale rollbacks after success"
else
    bad "Windows installer does not wire stale rollback cleanup"
fi
_finally_hits=$(printf '%s\n' "$INSTALL_PS1_CODE" | grep -c '^    } finally {$' || true)
_finally_guarded=$(printf '%s\n' "$INSTALL_PS1_CODE" |
    grep -A3 '^    } finally {$' | grep -c 'Restore-StudioVenvRollback' || true)
if [ "$_finally_hits" -gt 0 ] && [ "$_finally_guarded" -gt 0 ]; then
    ok "Windows replacement is protected by finally"
else
    bad "Windows replacement lacks finally rollback"
fi
if printf '%s\n' "$INSTALL_PS1_CODE" \
   | grep -A18 '^    function Remove-StudioVenvTreeWithRetry {' \
   | grep -q 'ErrorAction Stop'; then
    ok "Windows rollback deletion failures are observable and retried"
else
    bad "Windows rollback deletion still hides failures"
fi

# The marker must not outlive its attempt even when no venv replacement was in flight.
marker_case() {  # label, pre-existing marker value or empty, expect, [commit]
    _label="$1"; _pre="$2"; _expect="$3"; _commit="${4:-}"
    _dir="$WORK/marker-$_label"
    mkdir -p "$_dir/cache"
    [ -n "$_pre" ] && printf '%s\n' "$_pre" > "$_dir/cache/uv-cache-dir"
    _h="$_dir/harness.sh"
    {
        printf '%s\n' 'set -e'
        printf '%s\n' 'substep() { :; }'
        printf '%s\n' 'rollback_substep() { substep "$@"; }'
        printf '%s\n' 'C_WARN=""'
        printf "STUDIO_HOME='%s'\n" "$_dir"
        printf "VENV_DIR='%s/unsloth_studio'\n" "$_dir"
        printf '%s\n' "$ROLLBACK_BLOCK"
        printf '%s\n' 'UV_CACHE_DIR="/tmp/this-attempt-cache"'
        printf '%s\n' '_record_uv_cache_choice'
        [ -n "$_commit" ] && printf '%s\n' '_commit_studio_venv_replacement'
        printf '%s\n' 'exit 1'
    } > "$_h"
    ( cd "$_dir" && sh "$_h" >/dev/null 2>"$_dir/err" ) || _rc=$?
    if [ "${_rc:-0}" = 127 ] || [ -s "$_dir/err" ]; then
        bad "$_label (harness did not run: $(cat "$_dir/err"))"
        return 0
    fi
    if [ -f "$_dir/cache/uv-cache-dir" ]; then _got=$(cat "$_dir/cache/uv-cache-dir"); else _got="<gone>"; fi
    if [ "$_got" = "$_expect" ]; then
        ok "$_label"
    else
        bad "$_label (expected [$_expect], got [$_got])"
    fi
}

# A signal can land between any two statements, so the commit sets one flag both restores consult.
commit_flag_case() {
    _dir="$WORK/marker-commit-window"
    mkdir -p "$_dir/cache"
    printf '%s\n' "/previous/install/cache" > "$_dir/cache/uv-cache-dir"
    _h="$_dir/harness.sh"
    {
        printf '%s\n' 'set -e'
        printf '%s\n' 'substep() { :; }'
        printf '%s\n' 'rollback_substep() { substep "$@"; }'
        printf '%s\n' 'C_WARN=""'
        printf "STUDIO_HOME='%s'\n" "$_dir"
        printf "VENV_DIR='%s/unsloth_studio'\n" "$_dir"
        printf '%s\n' "$ROLLBACK_BLOCK"
        printf '%s\n' 'UV_CACHE_DIR="/tmp/this-attempt-cache"'
        printf '%s\n' '_record_uv_cache_choice'
        printf '%s\n' 'mkdir -p "$VENV_DIR" "$STUDIO_HOME/backup"'
        printf '%s\n' 'printf "new\n" > "$VENV_DIR/generation"'
        printf '%s\n' 'printf "old\n" > "$STUDIO_HOME/backup/generation"'
        printf '%s\n' '_STUDIO_INSTALL_COMMITTED=true'
        printf '%s\n' '_VENV_ROLLBACK_ACTIVE=true'
        printf '%s\n' '_VENV_ROLLBACK_DIR="$STUDIO_HOME/backup"'
        printf '%s\n' '_VENV_ROLLBACK_TARGET="$VENV_DIR"'
        printf '%s\n' '_restore_studio_venv_replacement'
        printf '%s\n' '_restore_uv_cache_marker'
    } > "$_h"
    ( cd "$_dir" && sh "$_h" >/dev/null 2>"$_dir/err" ) || _rc=$?
    if [ "${_rc:-0}" = 127 ] || [ -s "$_dir/err" ]; then
        bad "a committed install is not half rolled back (harness: $(cat "$_dir/err"))"
        return 0
    fi
    _got=$(cat "$_dir/cache/uv-cache-dir" 2>/dev/null)
    _gen=$(cat "$_dir/unsloth_studio/generation" 2>/dev/null)
    if [ "$_got" = "/tmp/this-attempt-cache" ] && [ "$_gen" = new ]; then
        ok "a committed install is not half rolled back by a signal mid-commit"
    else
        bad "a signal mid-commit undid half the commit (marker [$_got], venv [$_gen])"
    fi
}

echo "=== uv cache marker survives only a successful install ==="
marker_case "a failed install with no venv replacement restores the previous marker" \
    "/previous/install/cache" "/previous/install/cache"
marker_case "a failed first install leaves no marker behind" "" "<gone>"
# A first install takes no rollback branch and must still commit the marker.
marker_case "a committed first install keeps its marker when a later step fails" \
    "/previous/install/cache" "/tmp/this-attempt-cache" commit
commit_flag_case

echo "=== --no-rollback discards the old environment instead of keeping a copy (#11313) ==="
# uv creates only into an absent or empty path, so the rename always happens first.
no_rollback_case() {  # label  flag_line  expect_copy(yes|no)
    _nr_label="$1"
    _nr_flag="$2"
    _nr_expect="$3"
    _nr_dir="$WORK/no-rollback-$(printf '%s' "$_nr_label" | tr -c 'a-zA-Z0-9' '-')"
    mkdir -p "$_nr_dir/unsloth_studio"
    printf 'old\n' > "$_nr_dir/unsloth_studio/generation"
    _nr_harness="$_nr_dir/harness.sh"
    {
        printf '%s\n' 'set -e'
        printf '%s\n' 'substep() { printf "SUBSTEP %s\n" "$*"; }'
        printf '%s\n' 'rollback_substep() { substep "$@"; }'
        printf '%s\n' 'C_WARN=""'
        printf "STUDIO_HOME='%s'\n" "$_nr_dir"
        printf "VENV_DIR='%s/unsloth_studio'\n" "$_nr_dir"
        printf '%s\n' "$_nr_flag"
        printf '%s\n' "$ROLLBACK_BLOCK"
        printf '%s\n' '_start_studio_venv_replacement "$VENV_DIR"'
    } > "$_nr_harness"
    set +e
    _nr_out=$(dash "$_nr_harness" 2>&1)
    _nr_status=$?
    set -e
    if [ "$_nr_status" -ne 0 ]; then
        bad "$_nr_label: harness exited $_nr_status"
        return
    fi
    if find "$_nr_dir" -maxdepth 1 -name 'unsloth_studio.rollback.*' -print -quit | grep -q .; then
        _nr_got=yes
    else
        _nr_got=no
    fi
    if [ "$_nr_got" = "$_nr_expect" ]; then
        ok "$_nr_label"
    else
        bad "$_nr_label (rollback copy present: $_nr_got, expected $_nr_expect)"
    fi
    if [ ! -e "$_nr_dir/unsloth_studio" ]; then
        ok "$_nr_label: the old environment was moved aside"
    else
        bad "$_nr_label: the old environment is still in place"
    fi
    case "$_nr_expect" in
        no)
            if printf '%s\n' "$_nr_out" | grep -q 'discarded (--no-rollback)'; then
                ok "$_nr_label: says the old environment was discarded"
            else
                bad "$_nr_label: said nothing about discarding it"
            fi
            ;;
        yes)
            if printf '%s\n' "$_nr_out" | grep -q 'preserved for rollback'; then
                ok "$_nr_label: says the old environment was preserved"
            else
                bad "$_nr_label: said nothing about preserving it"
            fi
            ;;
    esac
}
no_rollback_case "default keeps the rollback copy" "_NO_ROLLBACK=false" yes
no_rollback_case "flag drops the rollback copy" "_NO_ROLLBACK=true" no
# Unset must read as "keep", never as an error under set -e.
no_rollback_case "an unset flag still keeps the copy" "# no flag set" yes

echo "=== --no-rollback clears the restore state, so a later signal cannot resurrect it ==="
NR_SIGNAL_DIR="$WORK/no-rollback-signal"
mkdir -p "$NR_SIGNAL_DIR/unsloth_studio"
printf 'old\n' > "$NR_SIGNAL_DIR/unsloth_studio/generation"
{
    printf '%s\n' 'set -e'
    printf '%s\n' 'substep() { :; }'
    printf '%s\n' 'rollback_substep() { substep "$@"; }'
    printf '%s\n' 'C_WARN=""'
    printf "STUDIO_HOME='%s'\n" "$NR_SIGNAL_DIR"
    printf "VENV_DIR='%s/unsloth_studio'\n" "$NR_SIGNAL_DIR"
    printf '%s\n' '_NO_ROLLBACK=true'
    printf '%s\n' "$ROLLBACK_BLOCK"
    printf '%s\n' '_start_studio_venv_replacement "$VENV_DIR"'
    printf '%s\n' 'mkdir -p "$VENV_DIR"'
    printf '%s\n' 'printf "partial\n" > "$VENV_DIR/generation"'
    printf '%s\n' 'kill -TERM $$'
    printf '%s\n' 'exit 99'
} > "$NR_SIGNAL_DIR/harness.sh"
set +e
dash "$NR_SIGNAL_DIR/harness.sh" >/dev/null 2>&1
_nr_signal_status=$?
set -e
if [ "$_nr_signal_status" -eq 143 ]; then
    ok "a signal after --no-rollback still exits 143"
else
    bad "a signal after --no-rollback exited $_nr_signal_status"
fi
if [ ! -e "$NR_SIGNAL_DIR/unsloth_studio" ] \
   || [ "$(cat "$NR_SIGNAL_DIR/unsloth_studio/generation" 2>/dev/null)" != "old" ]; then
    ok "--no-rollback does not resurrect the discarded environment"
else
    bad "--no-rollback restored an environment it had deleted"
fi

echo "=== a discard that could not delete says so instead of reporting success (#11313) ==="
# rm -rf only exempts missing paths; shadowing rm reaches a real unlink failure portably.
DISCARD_FAIL_DIR="$WORK/no-rollback-undeletable"
mkdir -p "$DISCARD_FAIL_DIR/unsloth_studio"
printf 'old\n' > "$DISCARD_FAIL_DIR/unsloth_studio/generation"
{
    printf '%s\n' 'set -e'
    printf '%s\n' 'substep() { printf "SUBSTEP %s\n" "$1"; }'
    printf '%s\n' 'rollback_substep() { substep "$@"; }'
    printf '%s\n' 'C_WARN=""'
    printf "STUDIO_HOME='%s'\n" "$DISCARD_FAIL_DIR"
    printf "VENV_DIR='%s/unsloth_studio'\n" "$DISCARD_FAIL_DIR"
    printf '%s\n' '_NO_ROLLBACK=true'
    printf '%s\n' "$ROLLBACK_BLOCK"
    printf '%s\n' 'rm() { return 1; }'
    printf '%s\n' '_start_studio_venv_replacement "$VENV_DIR"'
    printf '%s\n' 'echo INSTALL_CONTINUED'
} > "$DISCARD_FAIL_DIR/harness.sh"
set +e
DISCARD_FAIL_OUT=$(dash "$DISCARD_FAIL_DIR/harness.sh" 2>&1)
DISCARD_FAIL_STATUS=$?
set -e
if [ "$DISCARD_FAIL_STATUS" -eq 0 ] && printf '%s\n' "$DISCARD_FAIL_OUT" | grep -q INSTALL_CONTINUED; then
    ok "a failed discard never aborts the install"
else
    bad "a failed discard exited $DISCARD_FAIL_STATUS"
fi
if printf '%s\n' "$DISCARD_FAIL_OUT" | grep -q 'could not discard the previous environment'; then
    ok "a failed discard says the environment is still there"
else
    bad "a failed discard said nothing about the tree it could not remove"
fi
if printf '%s\n' "$DISCARD_FAIL_OUT" | grep -q 'discarded (--no-rollback)'; then
    bad "a failed discard still claimed the environment was discarded"
else
    ok "a failed discard does not claim success"
fi
if printf '%s\n' "$DISCARD_FAIL_OUT" | grep -q "$DISCARD_FAIL_DIR/unsloth_studio.rollback."; then
    ok "a failed discard names the path left on disk"
else
    bad "a failed discard did not name the path left on disk"
fi

echo "=== a disk that fills before studio setup is still diagnosed (#11313) ==="
# The venv and torch installs exit long before studio setup, so disk diagnosis hangs off the exit trap.
DISK_DIR="$WORK/diskfull"
mkdir -p "$DISK_DIR"
DISK_OUT=$(
    {
        printf '%s\n' 'set -e'
        printf '%s\n' 'substep() { :; }'
        printf '%s\n' 'step() { :; }'
        printf '%s\n' 'C_WARN=""'
        printf '%s\n' 'TAURI_MODE=true'
        printf '%s\n' '_restore_studio_venv_replacement() { :; }'
        printf '%s\n' '_restore_uv_cache_marker() { :; }'
        printf '%s\n' '_cleanup_install_temporaries() { :; }'
        printf "STUDIO_HOME='%s'\n" "$DISK_DIR"
        printf '%s\n' "$ROLLBACK_BLOCK"
        printf '%s\n' '_free_space_kb() { echo 1024; }'
        # install.sh defines tauri_log above the extracted slice.
        printf '%s\n' 'tauri_log() { echo "[TAURI:$1] $2"; }'
        printf '%s\n' 'exit 3'
    } | dash 2>&1
) || true
if printf '%s\n' "$DISK_OUT" | grep -q "the disk is full"; then
    ok "an early failure names the full disk instead of a bare exit code"
else
    bad "an early failure still reports only its exit code: $DISK_OUT"
fi
if printf '%s\n' "$DISK_OUT" | grep -q "ERROR_DEFAULT"; then
    ok "and the diagnosis reaches the Tauri marker the desktop UI reads"
else
    bad "the diagnosis never reached the Tauri marker"
fi
DISK_OK_OUT=$(
    {
        printf '%s\n' 'set -e'
        printf '%s\n' 'substep() { :; }'
        printf '%s\n' 'step() { :; }'
        printf '%s\n' 'C_WARN=""'
        printf '%s\n' 'TAURI_MODE=true'
        printf '%s\n' '_restore_studio_venv_replacement() { :; }'
        printf '%s\n' '_restore_uv_cache_marker() { :; }'
        printf '%s\n' '_cleanup_install_temporaries() { :; }'
        printf "STUDIO_HOME='%s'\n" "$DISK_DIR"
        printf '%s\n' "$ROLLBACK_BLOCK"
        printf '%s\n' '_free_space_kb() { echo 104857600; }'
        printf '%s\n' 'tauri_log() { echo "[TAURI:$1] $2"; }'
        printf '%s\n' 'exit 3'
    } | dash 2>&1
) || true
if printf '%s\n' "$DISK_OK_OUT" | grep -q "the disk is full"; then
    bad "a failure with plenty of room blamed the disk: $DISK_OK_OUT"
else
    ok "a failure with room to spare does not blame the disk"
fi

echo "=== a diagnostic write that fails must not cost the rollback (#11313) ==="
# The trap runs under set -e: the restore must happen first and writes must be best-effort.
TRAP_DIR="$WORK/trapsafe"
mkdir -p "$TRAP_DIR"
TRAP_OUT=$(
    {
        printf '%s\n' 'set -e'
        printf '%s\n' 'substep() { :; }'
        printf '%s\n' 'step() { :; }'
        printf '%s\n' 'C_WARN=""'
        printf '%s\n' 'TAURI_MODE=true'
        printf "STUDIO_HOME='%s'\n" "$TRAP_DIR"
        printf '%s\n' "$ROLLBACK_BLOCK"
        # After the extraction, so these shadow the real ones.
        printf "_restore_studio_venv_replacement() { : > '%s/restored'; }\n" "$TRAP_DIR"
        printf "_restore_uv_cache_marker() { : > '%s/marker'; }\n" "$TRAP_DIR"
        printf '%s\n' '_cleanup_install_temporaries() { :; }'
        printf '%s\n' '_free_space_kb() { echo 1024; }'
        printf '%s\n' 'tauri_log() { echo "[TAURI:$1] $2"; }'
        # Both streams closed, so every diagnostic write fails like a closed Tauri pipe.
        printf '%s\n' 'exec 1>&- 2>&-'
        printf '%s\n' 'exit 3'
    } | dash 2>&1
) || true
if [ -f "$TRAP_DIR/restored" ]; then
    ok "the environment is restored even when the diagnostic cannot be written"
else
    bad "a failed diagnostic write aborted the trap before the restore"
fi
if [ -f "$TRAP_DIR/marker" ]; then
    ok "and so is the uv cache marker"
else
    bad "a failed diagnostic write aborted the trap before the marker restore"
fi

echo "=== the restore frees space, so the measurement has to precede it (#11313) ==="
# The restore frees the half-built tree, so free space must be measured before it.
FREED_DIR="$WORK/freed"
mkdir -p "$FREED_DIR"
FREED_OUT=$(
    {
        printf '%s\n' 'set -e'
        printf '%s\n' 'substep() { :; }'
        printf '%s\n' 'step() { :; }'
        printf '%s\n' 'C_WARN=""'
        printf '%s\n' 'TAURI_MODE=true'
        printf "STUDIO_HOME='%s'\n" "$FREED_DIR"
        printf '%s\n' "$ROLLBACK_BLOCK"
        printf "_restore_studio_venv_replacement() { : > '%s/restored'; }\n" "$FREED_DIR"
        printf '%s\n' '_restore_uv_cache_marker() { :; }'
        printf '%s\n' '_cleanup_install_temporaries() { :; }'
        printf "_free_space_kb() { if [ -f '%s/restored' ]; then echo 104857600; else echo 1024; fi; }\n" "$FREED_DIR"
        printf '%s\n' 'tauri_log() { echo "[TAURI:$1] $2"; }'
        printf '%s\n' 'exit 3'
    } | dash 2>&1
) || true
if printf '%s\n' "$FREED_OUT" | grep -q "the disk is full"; then
    ok "the full disk is still named after the restore has freed the space"
else
    bad "the restore hid the full disk from the diagnosis: $FREED_OUT"
fi

echo "=== the disk-full remedy describes what happened, not what was asked for (#11313) ==="
# A failed discard left a tree on disk; deleting it is likely what makes the retry fit.
remedy_case() {  # label  state_lines  expected_grep  unexpected_grep
    _rc_label="$1"; _rc_state="$2"; _rc_want="$3"; _rc_not="$4"
    _rc_out=$(
        {
            printf '%s\n' 'substep() { :; }'
            printf '%s\n' 'step() { :; }'
            printf '%s\n' 'C_WARN=""'
            printf "STUDIO_HOME='%s'\n" "$WORK"
            printf '%s\n' "$ROLLBACK_BLOCK"
            printf '%s\n' '_free_space_kb() { echo 1024; }'
            printf '%s\n' "$_rc_state"
            printf '%s\n' '_set_disk_full_suffix'
            printf '%s\n' 'printf "%s\n" "$_DISK_FULL_REMEDY"'
        } | dash 2>&1
    )
    if printf '%s\n' "$_rc_out" | grep -q "$_rc_want"; then
        ok "$_rc_label names the right remedy"
    else
        bad "$_rc_label said: $_rc_out"
    fi
    if [ -n "$_rc_not" ]; then
        if printf '%s\n' "$_rc_out" | grep -q "$_rc_not"; then
            bad "$_rc_label still says the wrong thing: $_rc_out"
        else
            ok "$_rc_label does not say the wrong thing"
        fi
    fi
}
remedy_case "a discard that left a tree behind" \
    '_NO_ROLLBACK=true; _VENV_DISCARD_LEFTOVER="/tmp/left-behind"' \
    "still at /tmp/left-behind" "nothing further"
remedy_case "a discard that really happened" \
    '_NO_ROLLBACK=true; _VENV_DISCARDED=true' \
    "nothing further of its own to reclaim" ""
remedy_case "the flag with nothing to discard" \
    '_NO_ROLLBACK=true' \
    "Free some space and re-run." "already discarded"
remedy_case "an install that never passed the flag" \
    '_NO_ROLLBACK=false' \
    "drops the previous environment" "already discarded"

echo ""
echo "  PASS: $PASS"
echo "  FAIL: $FAIL"
[ "$FAIL" -eq 0 ] || exit 1
echo "ALL PASSED"