Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·8723 lines (8272 loc) · 406 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·8723 lines (8272 loc) · 406 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# Unsloth Studio Installer. Usage, supported options and the web one-liner live in the README under "Unsloth Studio (web UI)" and are deliberately not repeated here: this file ships inside the Linux desktop bundle, where a header rehearsing download-and-run command lines is the first thing a generic script classifier reads. A piped install takes options as environment variables after the pipe (UNSLOTH_NO_TORCH, UNSLOTH_SKIP_AUTOSTART, UNSLOTH_INSTALL_SYSTEMD, UNSLOTH_SYSTEMD_HOST, UNSLOTH_SYSTEMD_PORT, UNSLOTH_ISOLATE_UV_CACHE, UNSLOTH_INSTALL_NO_ROLLBACK, UNSLOTH_PYTHON, UNSLOTH_STUDIO_HOME), because a bare `--no-torch` after the pipe would be read as an option to sh itself; a local run takes the equivalent flags (--no-torch, --isolated-uv-cache, --no-rollback, --python, --local). Install dir priority: UNSLOTH_STUDIO_HOME > STUDIO_HOME > UNSLOTH_HOME/studio > $HOME/.unsloth/studio
# 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
# ── Why the installer lives in a function ──
# Body is wrapped in a function so a piped sh reads the whole file before running; an early
# exit otherwise breaks curl's pipe. Do not add `exec < /dev/null` (closes the piped source).
_unsloth_main() {
# ── Output style (aligned with studio/setup.sh) ──
RULE=""
_rule_i=0
while [ "$_rule_i" -lt 52 ]; do
RULE="${RULE}─"
_rule_i=$((_rule_i + 1))
done
if [ -n "${NO_COLOR:-}" ]; then
C_TITLE= C_DIM= C_OK= C_WARN= C_ERR= C_RST=
elif [ -t 1 ] || [ -n "${FORCE_COLOR:-}" ]; then
_ESC="$(printf '\033')"
C_TITLE="${_ESC}[38;5;150m"
C_DIM="${_ESC}[38;5;245m"
C_OK="${_ESC}[38;5;108m"
C_WARN="${_ESC}[38;5;136m"
C_ERR="${_ESC}[91m"
C_RST="${_ESC}[0m"
else
C_TITLE= C_DIM= C_OK= C_WARN= C_ERR= C_RST=
fi
step() { printf " ${C_DIM}%-15.15s${C_RST}${3:-$C_OK}%s${C_RST}\n" "$1" "$2"; }
substep() { printf " ${C_DIM}%-15s${2:-$C_DIM}%s${C_RST}\n" "" "$1"; }
# ── Parse flags ──
STUDIO_LOCAL_INSTALL=false
PACKAGE_NAME="unsloth"
TAURI_MODE=false
_USER_PYTHON=""
_NO_TORCH_FLAG=false
_SKIP_AUTOSTART=false
_ISOLATE_UV_CACHE=false
_NO_ROLLBACK=false
_VENV_DISCARDED=false
_VENV_DISCARD_LEFTOVER=""
_INSTALL_SYSTEMD=false
_SYSTEMD_STARTED=false
_VERBOSE=false
_SHORTCUTS_ONLY=false
_next_is_package=false
_next_is_python=false
_next_is_llama_cpp_dir=false
_WITH_LLAMA_CPP_DIR="${UNSLOTH_LOCAL_LLAMA_CPP_DIR:-}"
for arg in "$@"; do
if [ "$_next_is_package" = true ]; then
PACKAGE_NAME="$arg"
_next_is_package=false
continue
fi
if [ "$_next_is_python" = true ]; then
_USER_PYTHON="$arg"
_next_is_python=false
continue
fi
if [ "$_next_is_llama_cpp_dir" = true ]; then
_WITH_LLAMA_CPP_DIR="$arg"
_next_is_llama_cpp_dir=false
continue
fi
case "$arg" in
--local) STUDIO_LOCAL_INSTALL=true ;;
--package) _next_is_package=true ;;
--tauri) TAURI_MODE=true ;;
--python) _next_is_python=true ;;
--no-torch) _NO_TORCH_FLAG=true ;;
--isolated-uv-cache) _ISOLATE_UV_CACHE=true ;;
--no-rollback) _NO_ROLLBACK=true ;;
--verbose|-v) _VERBOSE=true ;;
--shortcuts-only) _SHORTCUTS_ONLY=true ;;
--with-llama-cpp-dir) _next_is_llama_cpp_dir=true ;;
esac
done
case "${UNSLOTH_NO_TORCH:-}" in 1|true|TRUE|yes|YES|on|ON) _NO_TORCH_FLAG=true ;; esac
case "${UNSLOTH_SKIP_AUTOSTART:-}" in 1|true|TRUE|yes|YES|on|ON) _SKIP_AUTOSTART=true ;; esac
case "${UNSLOTH_ISOLATE_UV_CACHE:-}" in 1|true|TRUE|yes|YES|on|ON) _ISOLATE_UV_CACHE=true ;; esac
case "${UNSLOTH_INSTALL_NO_ROLLBACK:-}" in 1|true|TRUE|yes|YES|on|ON) _NO_ROLLBACK=true ;; esac
case "${UNSLOTH_INSTALL_SYSTEMD:-}" in 1|true|TRUE|yes|YES|on|ON) _INSTALL_SYSTEMD=true ;; esac
[ -z "$_USER_PYTHON" ] && [ -n "${UNSLOTH_PYTHON:-}" ] && _USER_PYTHON="$UNSLOTH_PYTHON"
if [ "$_VERBOSE" = true ]; then
export UNSLOTH_VERBOSE=1
fi
if [ "$TAURI_MODE" = true ]; then
_tauri_canonical_root() (
_tcr_path=$1
case "$_tcr_path" in /*) ;; *) _tcr_path="$PWD/$_tcr_path" ;; esac
_tcr_tail=""
# Resolve the existing ancestor without creating a fresh install root.
while [ ! -d "$_tcr_path" ]; do
_tcr_tail="/${_tcr_path##*/}$_tcr_tail"
_tcr_path=${_tcr_path%/*}
[ -n "$_tcr_path" ] || _tcr_path=/
done
_tcr_path=$(CDPATH= cd -P -- "$_tcr_path" 2>/dev/null && pwd -P) || return 1
printf '%s\n' "$_tcr_path$_tcr_tail" | awk -F/ '{
for (i = 1; i <= NF; i++) {
if ($i == "" || $i == ".") continue
if ($i == "..") { if (n > 0) n-- }
else parts[++n] = $i
}
for (i = 1; i <= n; i++) printf "/%s", parts[i]
if (n == 0) printf "/"
printf "\n"
}'
)
_tauri_override_var=""
_tauri_override=$(printf '%s' "${UNSLOTH_STUDIO_HOME:-}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -n "$_tauri_override" ]; then
_tauri_override_var="UNSLOTH_STUDIO_HOME"
else
_tauri_override=$(printf '%s' "${STUDIO_HOME:-}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
[ -n "$_tauri_override" ] && _tauri_override_var="STUDIO_HOME"
fi
if [ -z "$_tauri_override" ]; then
_tauri_override=$(printf '%s' "${UNSLOTH_HOME:-}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -n "$_tauri_override" ]; then
_tauri_override_var="UNSLOTH_HOME (studio child)"
_tauri_override="${_tauri_override%/}/studio"
fi
fi
if [ -n "$_tauri_override" ]; then
case "$_tauri_override" in
"~") _tauri_override="$HOME" ;;
"~/"*) _tauri_override="$HOME/${_tauri_override#'~/'}" ;;
esac
_tauri_override_abs=$(_tauri_canonical_root "$_tauri_override") \
|| _tauri_override_abs="$_tauri_override"
_tauri_legacy_root=$(_tauri_canonical_root "$HOME/.unsloth/studio") \
|| _tauri_legacy_root="$HOME/.unsloth/studio"
if [ "$_tauri_override_abs" != "$_tauri_legacy_root" ]; then
echo "ERROR: $_tauri_override_var is not supported with --tauri." >&2
echo " The desktop app still uses the legacy ~/.unsloth/studio root." >&2
echo " Run install.sh without --tauri for custom-root shell installs," >&2
echo " or unset the env var for default desktop installs." >&2
exit 1
fi
fi
fi
_is_verbose() {
[ "${UNSLOTH_VERBOSE:-0}" = "1" ]
}
run_maybe_quiet() {
if _is_verbose; then
"$@"
else
"$@" > /dev/null 2>&1
fi
}
_trim_index_path_slashes() {
_tips_v="$1"
case "$_tips_v" in
*[?#]*)
_tips_head="${_tips_v%%[?#]*}"
_tips_tail="${_tips_v#"$_tips_head"}"
;;
*)
_tips_head="$_tips_v"
_tips_tail=""
;;
esac
while [ -n "$_tips_head" ] && [ "${_tips_head%/}" != "$_tips_head" ]; do
_tips_head="${_tips_head%/}"
done
printf '%s%s' "$_tips_head" "$_tips_tail"
}
_redact_install_output() {
sed -E \
-e 's#(https?://)[^/@[:space:]`]+@#\1<redacted>@#g' \
-e 's#([?&][^=[:space:]&`]+)=[^&#[:space:]`]+#\1=<redacted>#g' \
-e 's|(https?://[^[:space:]`#]+)#[^[:space:]`]+|\1#<redacted>|g' \
"$@"
}
: "${UNSLOTH_DL_MARKER_MIN_BYTES:=52428800}"
# Markers go to stderr: the verbose path's sed redactor block-buffers and would delay them.
_uv_download_markers() {
# Minimal images lack awk; without this the pipe closes and the child dies of SIGPIPE.
if ! command -v awk >/dev/null 2>&1; then
if [ -n "$1" ]; then cat >> "$1"; else cat; fi
return
fi
awk -v logf="$1" -v minb="$2" -v tauri="${TAURI_MODE:-false}" -v err=/dev/stderr '
{ if (logf == "") print; else print >> logf }
tauri != "true" { next }
# Field-relative so a leading status glyph cannot shift the match.
/(^| )Downloading [^ ]+ \([0-9.]+[KMG]iB\)$/ {
size = $NF
gsub(/[()]/, "", size)
n = size; sub(/[KMG]iB$/, "", n)
u = size; sub(/^[0-9.]+/, "", u)
mult = (u == "GiB") ? 1073741824 : (u == "MiB") ? 1048576 : 1024
if (n * mult >= minb) {
announced[$(NF - 1)] = 1
print "[TAURI:DL] " $(NF - 1) " " size > err
fflush(err)
}
next
}
# Only close what was opened: uv also reports completion for unannounced packages.
/(^| )Downloaded [^ ]+$/ && ($NF in announced) {
delete announced[$NF]
print "[TAURI:DL_DONE] " $NF > err
fflush(err)
}
'
}
run_install_cmd() {
if [ -z "${_UNSLOTH_MIRROR_SPARE:-}" ]; then
_run_install_cmd_once "$@"
return
fi
_run_install_cmd_once "$@" || _mirror_retry_install "$?" "$@"
}
_mirror_retry_install() {
[ -n "${_UNSLOTH_MIRROR_SPARE:-}" ] || return "$1"
_mri_rc=$1
_mri_label=$2
shift 2
case " $* " in
*" https://download.pytorch.org/whl"*) _mri_ran=torch ;;
*" --index-url "*|*" --default-index "*|*" --find-links "*|*" --no-index "*|*"://"*|*" --torch-backend"*) _mri_ran="" ;;
*" uv venv "*|*" uv python install "*) _mri_ran=python ;;
*) _mri_ran=pypi ;;
esac
_mri_host=$(_mirror_failed_host "${_ric_log:-}" "$_mri_ran") || _mri_host=""
rm -f "${_ric_log:-}"
case "$_mri_host $* " in
"pypi "*" --index-url "*|"pypi "*" --default-index "*) return "$_mri_rc" ;;
"unsynced "*" --index-url "*|"unsynced "*" --default-index "*|"unsynced "*" --no-index "*) return "$_mri_rc" ;;
"torch "*https://download.pytorch.org/whl*|"pypi "*|"python "*|"unsynced "*) ;;
*) return "$_mri_rc" ;;
esac
_mirror_take "$_mri_host" || return "$_mri_rc"
(
for _mri_pair in $_MT_PAIRS; do export "$_mri_pair"; done
[ "$_mri_host" != torch ] || _ric_torch_mirror=$UNSLOTH_PYTORCH_MIRROR
_run_install_cmd_once "$_mri_label" "$@" || { _mri_rc=$?; rm -f "${_ric_log:-}"; exit "$_mri_rc"; }
) || return
for _mri_pair in $_MT_PAIRS; do export "$_mri_pair"; done
if [ "$_mri_host" = torch ]; then
_ric_torch_mirror=$UNSLOTH_PYTORCH_MIRROR
case "${TORCH_INDEX_URL:-}" in
https://download.pytorch.org/whl*) TORCH_INDEX_URL=$_ric_torch_mirror${TORCH_INDEX_URL#https://download.pytorch.org/whl} ;;
esac
fi
}
_ric_run() {
if "$@" 2>&1; then
_cmd_rc=0
else
_cmd_rc=$?
fi
printf '%s' "$_cmd_rc" > "$_rcf"
}
_ric_tee() {
if command -v tee >/dev/null 2>&1; then tee "$1"; else cat; fi
}
_run_install_cmd_once() {
_label="$1"
shift
[ -z "${_ric_log:-}" ] || rm -f "$_ric_log"
_ric_log=""
if [ -n "${_ric_torch_mirror:-}" ]; then
_ric_n=$#
for _ric_arg in "$@"; do
case "$_ric_arg" in
https://download.pytorch.org/whl*) _ric_arg=$_ric_torch_mirror${_ric_arg#https://download.pytorch.org/whl} ;;
esac
set -- "$@" "$_ric_arg"
done
shift "$_ric_n"
fi
# Clear inherited uv index vars so a uv.toml cannot outrank the --default-index pin.
case " $* " in
*" --default-index "*) set -- env -u UV_DEFAULT_INDEX -u UV_INDEX_URL -u UV_INDEX -u UV_EXTRA_INDEX_URL -u UV_TORCH_BACKEND -u UV_FIND_LINKS -u UV_CONFIG_FILE UV_NO_CONFIG=1 "$@" ;;
esac
if _is_verbose; then
_rcf=$(mktemp)
tauri_stream_log stdout "OUTPUT_CLEAR" "$_label"
_log=$(mktemp)
_ric_run "$@" | _ric_tee "$_log" | _uv_download_markers "" "$UNSLOTH_DL_MARKER_MIN_BYTES" | _redact_install_output
_rc=$(cat "$_rcf" 2>/dev/null || echo 1)
rm -f "$_rcf"
_rc=${_rc:-1}
if [ "$_rc" -eq 0 ] 2>/dev/null; then
rm -f "$_log"
tauri_clear_install_error "$_label recovered"
return 0
fi
_ric_damaged=$(_uv_damaged_wheels "$_log")
if [ -n "${_UNSLOTH_MIRROR_SPARE:-}" ]; then _ric_log=$_log; else rm -f "$_log"; fi
tauri_stream_log stdout "ERROR_OUTPUT" "$_label failed (exit code $_rc)"
step "error" "$_label failed (exit code $_rc)" "$C_ERR" >&2
return "$_rc"
fi
_log=$(mktemp)
_rcf=$(mktemp)
tauri_stream_log stderr "OUTPUT_CLEAR" "$_label"
{
if "$@" 2>&1; then
_cmd_rc=0
else
_cmd_rc=$?
fi
printf '%s' "$_cmd_rc" > "$_rcf"
} | _uv_download_markers "$_log" "$UNSLOTH_DL_MARKER_MIN_BYTES"
_rc=$(cat "$_rcf" 2>/dev/null || echo 1)
rm -f "$_rcf"
_rc=${_rc:-1}
if [ "$_rc" -eq 0 ] 2>/dev/null; then
rm -f "$_log"
tauri_clear_install_error "$_label recovered"
return 0
fi
step "error" "$_label failed (exit code $_rc)" "$C_ERR" >&2
_redact_install_output "$_log" >&2
tauri_stream_log stderr "ERROR_OUTPUT" "$_label failed (exit code $_rc)"
_ric_damaged=$(_uv_damaged_wheels "$_log")
if [ -n "${_UNSLOTH_MIRROR_SPARE:-}" ]; then _ric_log=$_log; else rm -f "$_log"; fi
return $_rc
}
# uv reuses a damaged cached wheel on every run, --refresh included (#17375).
_uv_damaged_wheels() {
[ -f "$1" ] || return 0
awk '
p != "" && /^[ \t]*(cause|Caused by): The wheel is invalid/ { print tolower(p) }
{ p = "" }
/Failed to install: [^ ]+ \(/ {
p = $0; sub(/.*Failed to install: [^ ]+ \(/, "", p); sub(/(==| @ ).*/, "", p)
if (p !~ /^[A-Za-z0-9][A-Za-z0-9._-]*$/) p = ""
}' "$1"
}
_repair_uv_damaged_wheels() {
_rudw_new=""
for _rudw_n in ${_ric_damaged:-}; do
case "$_ricr_cleaned" in *" $_rudw_n "*) continue ;; esac
[ "$_ricr_cleaned_n" -lt 50 ] || break
_ricr_cleaned="$_ricr_cleaned$_rudw_n "
_ricr_cleaned_n=$((_ricr_cleaned_n + 1))
_rudw_new="$_rudw_new $_rudw_n"
done
[ -n "$_rudw_new" ] || return 1
substep "clearing damaged uv cache entries for$_rudw_new..." "$C_WARN"
# Another uv process may hold the cache lock.
# shellcheck disable=SC2086
UV_LOCK_TIMEOUT=${UV_LOCK_TIMEOUT:-60} uv cache clean $_rudw_new >/dev/null 2>&1
}
: "${UNSLOTH_INSTALL_RETRIES:=3}"
: "${UNSLOTH_INSTALL_RETRY_DELAY:=3}"
run_install_cmd_retry() {
_ricr_label="$1"
# Bounds 1..100 retries and 0..3600s delay; 0?* rejects octal delays.
case "$UNSLOTH_INSTALL_RETRIES" in
''|*[!0-9]*|0) _ricr_max=3 ;;
*) if [ "${#UNSLOTH_INSTALL_RETRIES}" -le 3 ] && [ "$UNSLOTH_INSTALL_RETRIES" -ge 1 ] 2>/dev/null && [ "$UNSLOTH_INSTALL_RETRIES" -le 100 ] 2>/dev/null; then _ricr_max=$UNSLOTH_INSTALL_RETRIES; else _ricr_max=3; fi ;;
esac
case "$UNSLOTH_INSTALL_RETRY_DELAY" in
''|*[!0-9]*|0?*) _ricr_delay=3 ;;
*) if [ "${#UNSLOTH_INSTALL_RETRY_DELAY}" -le 4 ] && [ "$UNSLOTH_INSTALL_RETRY_DELAY" -ge 0 ] 2>/dev/null && [ "$UNSLOTH_INSTALL_RETRY_DELAY" -le 3600 ] 2>/dev/null; then _ricr_delay=$UNSLOTH_INSTALL_RETRY_DELAY; else _ricr_delay=3; fi ;;
esac
_ricr_attempt=1
_ricr_cleaned=" "
_ricr_cleaned_n=0
while :; do
_ric_damaged=""
# AND-OR (not `if`) preserves the real failure code for the rollback path.
_run_install_cmd_once "$@" && return 0
_ricr_rc=$?
# Not an attempt: uv names one damaged package per run.
_repair_uv_damaged_wheels && continue
if [ "$_ricr_attempt" -ge "$_ricr_max" ]; then
_mirror_retry_install "$_ricr_rc" "$@" && return 0
return $?
fi
substep "retrying \"$_ricr_label\" after transient failure (attempt $((_ricr_attempt + 1))/$_ricr_max, waiting ${_ricr_delay}s)..." "$C_WARN"
sleep "$_ricr_delay" || true
_ricr_attempt=$((_ricr_attempt + 1))
_ricr_delay=$((_ricr_delay * 2))
done
}
# gfx906: the prebuilt AMD bnb wheel has no gfx906 kernels and would clobber a source-built bnb.
_is_gfx906_bnb_skip() {
[ "${_gfx906_target:-false}" = true ] && return 0
_bnb_gfx_env=$(printf '%s' "${UNSLOTH_ROCM_GFX_ARCH:-}" | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]')
_bnb_gfx_env=${_bnb_gfx_env%%:*}
[ "$_bnb_gfx_env" = "gfx906" ] && return 0
# A pinned index skips the reroute that sets _gfx906_target, so probe here (sole-arch rule).
if [ -z "$_bnb_gfx_env" ] && [ "${_torch_index_pinned:-false}" = true ]; then
_bnb_gfx_probe=$(_probe_amd_gfx_arch | awk 'NF && !seen[$0]++')
[ "$_bnb_gfx_probe" = "gfx906" ] && return 0
fi
return 1
}
# pip install unsloth pulls a generic bnb; drop a freshly pulled wheel but keep a source build.
_gfx906_bnb_installed() {
"$_VENV_PY" -c "import importlib.util as u, sys; sys.exit(0 if u.find_spec('bitsandbytes') else 1)" >/dev/null 2>&1
}
_gfx906_bnb_snapshot() {
_gfx906_bnb_absent_before=false
_is_gfx906_bnb_skip || return 0
_gfx906_bnb_installed || _gfx906_bnb_absent_before=true
}
_gfx906_bnb_prune() {
_is_gfx906_bnb_skip || return 0
[ "${_gfx906_bnb_absent_before:-false}" = true ] || return 0
_gfx906_bnb_installed || return 0
substep "gfx906: removing generic bitsandbytes pulled in as a dependency (no gfx906 kernels; build from source for 4-bit QLoRA)" "$C_WARN"
uv pip uninstall --python "$_VENV_PY" bitsandbytes >/dev/null 2>&1 \
|| "$_VENV_PY" -m pip uninstall -y bitsandbytes >/dev/null 2>&1 || true
}
# bnb <= 0.49.2 NaNs at 4-bit decode on AMD (bnb #1887). Keep this floor in step with the amd extra
# in pyproject.toml and studio/install_python_stack.py.
_BNB_ROCM_PYPI_FALLBACK="bitsandbytes>=0.50.0"
# Intel XPU: 0.50.0 is the first with xpu libs; studio/setup.ps1 uses the same floor.
_BNB_XPU_SPEC="bitsandbytes>=0.50.0"
# bnb aarch64 wheels carry no ROCm binary at any version, so messages must not claim 4-bit.
_bnb_rocm_arch_has_binary() {
case "$_ARCH" in
aarch64|arm64) return 1 ;;
*) return 0 ;;
esac
}
_warn_bnb_no_rocm_binary() {
_bnb_rocm_arch_has_binary && return 0
substep "[WARN] aarch64: bitsandbytes ships no ROCm kernels on this arch; 4-bit QLoRA needs a source build -- https://unsloth.ai/docs/get-started/install/amd" "$C_WARN"
}
_install_bnb_rocm() {
_label="$1"
_venv_py="$2"
case "$_ARCH" in
x86_64|amd64)
_bnb_whl_url="https://github.com/bitsandbytes-foundation/bitsandbytes/releases/download/continuous-release_main/bitsandbytes-1.33.7.preview-py3-none-manylinux_2_24_x86_64.whl"
;;
aarch64|arm64)
_bnb_whl_url="https://github.com/bitsandbytes-foundation/bitsandbytes/releases/download/continuous-release_main/bitsandbytes-1.33.7.preview-py3-none-manylinux_2_24_aarch64.whl"
;;
*)
_bnb_whl_url=""
;;
esac
# uv rejects this pre-release wheel (filename/metadata version mismatch); pip accepts it.
if ! "$_venv_py" -m pip --version >/dev/null 2>&1; then
if ! run_maybe_quiet "$_venv_py" -m ensurepip --upgrade; then
run_maybe_quiet uv pip install --python "$_venv_py" pip || \
substep "[WARN] could not bootstrap pip; bitsandbytes install will likely fail" "$C_WARN"
fi
fi
if [ -n "$_bnb_whl_url" ]; then
substep "installing bitsandbytes for AMD ROCm (pre-release, PR #1887)..."
_bnb_log=$(mktemp)
if "$_venv_py" -m pip install \
--disable-pip-version-check \
--force-reinstall --no-cache-dir --no-deps \
--retries 8 --timeout 90 \
"$_bnb_whl_url" >"$_bnb_log" 2>&1; then
rm -f "$_bnb_log"
_warn_bnb_no_rocm_binary
return 0
fi
_bnb_rc=$?
if _is_verbose; then
_redact_install_output "$_bnb_log" >&2
fi
rm -f "$_bnb_log"
step "warning" "$_label (pre-release) failed (exit code $_bnb_rc)" "$C_WARN" >&2
if _bnb_rocm_arch_has_binary; then
substep "[WARN] bnb pre-release install failed; falling back to PyPI $_BNB_ROCM_PYPI_FALLBACK, which carries the ROCm 4-bit fix" "$C_WARN"
else
substep "[WARN] bnb pre-release install failed; falling back to PyPI $_BNB_ROCM_PYPI_FALLBACK" "$C_WARN"
fi
fi
run_install_cmd "$_label (pypi fallback)" "$_venv_py" -m pip install \
--force-reinstall --no-cache-dir --no-deps "$_BNB_ROCM_PYPI_FALLBACK"
_bnb_pypi_rc=$?
_warn_bnb_no_rocm_binary
return $_bnb_pypi_rc
}
if [ "$_next_is_package" = true ]; then
echo "❌ ERROR: --package requires an argument." >&2
exit 1
fi
if [ "$_next_is_python" = true ]; then
echo "❌ ERROR: --python requires a version argument (e.g. --python 3.12)." >&2
exit 1
fi
if [ "$_next_is_llama_cpp_dir" = true ]; then
echo "❌ ERROR: --with-llama-cpp-dir requires a path argument." >&2
exit 1
fi
case "$PACKAGE_NAME" in
[!a-zA-Z0-9]*)
echo "❌ ERROR: --package name must start with a letter or digit." >&2
exit 1 ;;
*[!a-zA-Z0-9._-]*)
echo "❌ ERROR: --package name contains invalid characters (allowed: a-z A-Z 0-9 . _ -)" >&2
exit 1 ;;
esac
# ── Tauri structured output ──
tauri_log() {
if [ "$TAURI_MODE" = true ]; then
echo "[TAURI:$1] $2"
fi
}
tauri_stream_log() {
_tsl_stream="$1"
_tsl_tag="$2"
shift 2
if [ "$TAURI_MODE" = true ]; then
if [ "$_tsl_stream" = stderr ]; then
printf '[TAURI:%s] %s\n' "$_tsl_tag" "$*" >&2
else
printf '[TAURI:%s] %s\n' "$_tsl_tag" "$*"
fi
fi
}
rollback_substep() {
if [ "$TAURI_MODE" = true ]; then
tauri_log "PROGRESS" "$1"
else
substep "$@"
fi
}
tauri_clear_install_error() {
if [ "$TAURI_MODE" = true ]; then
tauri_log "ERROR_CLEAR" "$1"
printf '[TAURI:ERROR_CLEAR] %s\n' "$1" >&2
fi
}
tauri_diag_marker() {
_diag_gpu_branch="${1:-unknown}"
_diag_torch_index_family="${2:-none}"
tauri_log "DIAG" "diag_schema=1 platform=${OS:-unknown} arch=${_ARCH:-unknown} python_version=${PYTHON_VERSION:-unknown} skip_torch=${SKIP_TORCH:-false} mac_intel=${MAC_INTEL:-false} gpu_branch=${_diag_gpu_branch} torch_index_family=${_diag_torch_index_family}"
}
_tauri_torch_index_family() {
if [ "${SKIP_TORCH:-false}" = true ]; then
echo "none"
return
fi
_diag_url="${1:-}"
_diag_url="${_diag_url%%\?*}"
_diag_url="${_diag_url%%#*}"
_diag_url="${_diag_url%/}"
case "$_diag_url" in
*/cu118) echo "cu118" ;;
*/cu124) echo "cu124" ;;
*/cu126) echo "cu126" ;;
*/cu128) echo "cu128" ;;
*/cu130) echo "cu130" ;;
*/cpu) echo "cpu" ;;
*/xpu) echo "xpu" ;;
*/rocm[0-9]*.[0-9]*)
_diag_family=${_diag_url##*/}
case "$_diag_family" in
rocm[0-9]*.[0-9]*) echo "$_diag_family" ;;
*) echo "auto" ;;
esac ;;
*repo.amd.com/rocm/whl/gfx*|*rocm/whl/gfx*) echo "rocm7.13" ;;
"") echo "none" ;;
*) echo "auto" ;;
esac
}
_tauri_gpu_branch() {
_diag_family="${1:-unknown}"
_diag_radeon="${2:-false}"
if [ "${SKIP_TORCH:-false}" = true ]; then
echo "no_torch"
return
fi
if [ "${OS:-}" = "macos" ]; then
echo "mac"
return
fi
case "$_diag_family" in
cu[0-9]*) echo "cuda" ;;
rocm*)
if [ "$_diag_radeon" = true ]; then
echo "rocm_radeon"
else
echo "rocm"
fi ;;
radeon) echo "rocm_radeon" ;;
xpu) echo "xpu" ;;
cpu) echo "cpu" ;;
none) echo "no_torch" ;;
*) echo "unknown" ;;
esac
}
PYTHON_VERSION=""
_resolve_studio_destinations() {
_override_var=""
_override=$(printf '%s' "${UNSLOTH_STUDIO_HOME:-}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -n "$_override" ]; then
_override_var="UNSLOTH_STUDIO_HOME"
else
_override=$(printf '%s' "${STUDIO_HOME:-}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
[ -n "$_override" ] && _override_var="STUDIO_HOME"
fi
if [ -z "$_override" ]; then
_override=$(printf '%s' "${UNSLOTH_HOME:-}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -n "$_override" ]; then
_override_var="UNSLOTH_HOME (studio child)"
_override="${_override%/}/studio"
fi
fi
# Tilde expansion: quoted env vars aren't subject to it on assignment.
case "$_override" in
"~") _override="$HOME" ;;
"~/"*) _override="$HOME/${_override#'~/'}" ;;
esac
if [ -n "$_override" ]; then
mkdir -p -- "$_override" 2>/dev/null || { echo "ERROR: $_override_var=$_override cannot be created." >&2; exit 1; }
[ -w "$_override" ] || { echo "ERROR: $_override_var=$_override is not writable." >&2; exit 1; }
STUDIO_HOME="$(CDPATH= cd -P -- "$_override" && pwd -P)" || exit 1
DATA_DIR="$STUDIO_HOME/share"
_LOCAL_BIN="$STUDIO_HOME/bin"
_STUDIO_HOME_REDIRECT=env
substep "custom $_override_var=$STUDIO_HOME"
return 0
fi
_default_home=""
if command -v getent >/dev/null 2>&1; then
_default_home=$(getent passwd "${USER:-$(whoami)}" 2>/dev/null | cut -d: -f6)
elif [ "$(uname)" = "Darwin" ] && command -v dscl >/dev/null 2>&1; then
_default_home=$(dscl . -read "/Users/${USER:-$(whoami)}" NFSHomeDirectory 2>/dev/null | awk '{print $2}')
fi
_home_canon="$HOME"
if [ -d "$_home_canon" ]; then
_home_canon=$(CDPATH= cd -P -- "$_home_canon" 2>/dev/null && pwd -P) || _home_canon="$HOME"
fi
_default_home_canon="$_default_home"
if [ -n "$_default_home_canon" ] && [ -d "$_default_home_canon" ]; then
_default_home_canon=$(CDPATH= cd -P -- "$_default_home_canon" 2>/dev/null && pwd -P) || _default_home_canon="$_default_home"
fi
if [ -n "$_default_home_canon" ] && [ "$_home_canon" != "$_default_home_canon" ]; then
STUDIO_HOME="$HOME/.unsloth/studio"
DATA_DIR="$HOME/.local/share/unsloth"
_LOCAL_BIN="$HOME/.local/bin"
_STUDIO_HOME_REDIRECT=home
substep "HOME redirected ($HOME); install follows \$HOME"
return 0
fi
STUDIO_HOME="$HOME/.unsloth/studio"
DATA_DIR="$HOME/.local/share/unsloth"
_LOCAL_BIN="$HOME/.local/bin"
_STUDIO_HOME_REDIRECT=default
}
# uv --no-cache neither reads nor writes a cache, so nothing is recorded. Values are clap's
# BoolishValueParser set. Mirrors _uv_no_cache_requested() in unsloth_cli/commands/studio.py.
_uv_no_cache_requested() {
case "$(printf '%s' "${UV_NO_CACHE:-}" | tr '[:upper:]' '[:lower:]')" in
1|y|yes|t|true|on) return 0 ;;
esac
return 1
}
_uv_is_bucket_name() {
case "$1" in
*-v[0-9]*) ;;
*) return 1 ;;
esac
# '' too: `##*-v` on `archive-v1-v` leaves an empty suffix; Test-StudioUvBucketName rejects it.
case "${1##*-v}" in
''|*[!0-9]*) return 1 ;;
esac
# Every CacheBucket in uv 0.13.5 (UV_PINNED_VERSION) plus built-wheels; re-read
# uv-cache/src/lib.rs on a pin bump, or a missing kind goes unprobed and uv fails on it.
case "${1%-v*}" in
archive|binaries|builds|built-wheels|environments|files|flat-index) ;;
git|interpreter|osv|python|sdists|simple|wheels) ;;
*) return 1 ;;
esac
return 0
}
_absolutize_uv_cache_dir() {
_uv_cache_path="${1-$UV_CACHE_DIR}"
case "$_uv_cache_path" in
/*) ;;
*)
_uv_cache_base="${UV_WORKING_DIR:-$PWD}"
case "$_uv_cache_base" in
/*) ;;
*) _uv_cache_base="$PWD/$_uv_cache_base" ;;
esac
_uv_cache_path="$_uv_cache_base/$_uv_cache_path"
;;
esac
# Strip trailing slashes: the studio-vs-shared comparison below is a string compare.
while [ "$_uv_cache_path" != / ]; do
case "$_uv_cache_path" in
*/) _uv_cache_path="${_uv_cache_path%/}" ;;
*) break ;;
esac
done
printf '%s\n' "$_uv_cache_path"
}
# Can we create AND fill this directory? A real create, since mkdir -p exits 0 on an existing
# unwritable one and -w reads the mode, not the filesystem. mktemp, since a predictable name
# in another account's directory can be pre-created as a symlink to follow.
_uv_cache_root_is_writable() {
mkdir -p "$1" 2>/dev/null || return 1
_uv_root_probe=$(mktemp "$1/.unsloth-write-probe.XXXXXX" 2>/dev/null) || return 1
# Retry the unlink and trust whether the file is gone: a held handle can make rm fail once.
_uv_root_tries=0
while :; do
rm -f "$_uv_root_probe" 2>/dev/null || true
[ -e "$_uv_root_probe" ] || break
_uv_root_tries=$((_uv_root_tries + 1))
if [ "$_uv_root_tries" -ge 3 ]; then
unset _uv_root_probe _uv_root_tries
return 1
fi
sleep 1
done
unset _uv_root_probe _uv_root_tries
return 0
}
# Also check every bucket: a root-owned bucket from an elevated run makes uv abort.
_uv_cache_is_writable() {
_uv_cache_root_is_writable "$1" || return 1
_uv_w_bad=0
# Case folding is filesystem-dependent (APFS folds, ext4 does not), so measure it.
_uv_w_fold=0
_uv_w_probe="$1/.unsloth-case-probe.$$-A"
if mkdir "$_uv_w_probe" 2>/dev/null; then
[ -d "$1/.unsloth-case-probe.$$-a" ] && _uv_w_fold=1
rmdir "$_uv_w_probe" 2>/dev/null || true
fi
unset _uv_w_probe
_uv_w_glob=on
case $- in *f*) _uv_w_glob=off ;; esac
set +f
for _uv_w_dir in "$1"/*; do
_uv_w_name="${_uv_w_dir##*/}"
if ! _uv_is_bucket_name "$_uv_w_name"; then
[ "$_uv_w_fold" = 1 ] || continue
case "$_uv_w_name" in *[[:upper:]]*) ;; *) continue ;; esac
_uv_w_lower=$(printf '%s' "$_uv_w_name" | tr '[:upper:]' '[:lower:]')
_uv_is_bucket_name "$_uv_w_lower" || continue
fi
if [ ! -d "$_uv_w_dir" ]; then
if [ -e "$_uv_w_dir" ] || [ -L "$_uv_w_dir" ]; then
_uv_w_bad=1
fi
continue
fi
_uv_cache_root_is_writable "$_uv_w_dir" || _uv_w_bad=1
done
if [ "$_uv_w_glob" = off ]; then set -f; fi
unset _uv_w_dir _uv_w_glob _uv_w_name _uv_w_lower _uv_w_fold
if [ "$_uv_w_bad" -ne 0 ]; then
unset _uv_w_bad
return 1
fi
unset _uv_w_bad
return 0
}
# Records which cache this install used so an update reuses it. UV_CACHE_DIR is made absolute
# because setup.sh changes directory.
_record_uv_cache_choice() {
UV_CACHE_DIR=$(_absolutize_uv_cache_dir)
_uv_marker_dir="$STUDIO_HOME/cache"
_uv_marker_file="$_uv_marker_dir/uv-cache-dir"
_uv_marker_value="$UV_CACHE_DIR"
if [ "$_UV_MARKER_SAVED" != true ]; then
if [ -e "$_uv_marker_file" ] || [ -L "$_uv_marker_file" ]; then
_UV_MARKER_PREVIOUS=$(cat "$_uv_marker_file" 2>/dev/null) || return 0
_UV_MARKER_EXISTED=true
else
_UV_MARKER_PREVIOUS=""
_UV_MARKER_EXISTED=false
fi
_UV_MARKER_SAVED=true
fi
(
mkdir -p "$_uv_marker_dir" 2>/dev/null &&
# Unlink first: a redirection follows a symlink and truncates its target.
rm -f "$_uv_marker_file" 2>/dev/null &&
# And only once gone: rm can fail on a link in an undeletable directory.
! { [ -e "$_uv_marker_file" ] || [ -L "$_uv_marker_file" ]; } &&
printf '%s\n' "$_uv_marker_value" > "$_uv_marker_file" 2>/dev/null
) || true
}
_restore_uv_cache_marker() {
[ "${_STUDIO_INSTALL_COMMITTED:-false}" = true ] && return 0
[ "$_UV_MARKER_SAVED" = true ] || return 0
_uv_marker_file="$STUDIO_HOME/cache/uv-cache-dir"
rm -f "$_uv_marker_file" 2>/dev/null || true
if [ "$_UV_MARKER_EXISTED" = true ] \
&& ! { [ -e "$_uv_marker_file" ] || [ -L "$_uv_marker_file" ]; }; then
printf '%s\n' "$_UV_MARKER_PREVIOUS" > "$_uv_marker_file" 2>/dev/null || true
fi
_UV_MARKER_SAVED=false
}
_configure_uv_cache() {
_uv_studio_cache="$STUDIO_HOME/cache/uv"
# Only a caller's UV_CACHE_DIR outranks selection; the flag tells it apart from our default.
if [ "${_UV_CACHE_DIR_INSTALLER_DEFAULT:-false}" != true ]; then
case "${UV_CACHE_DIR-}" in
*[![:space:]]*)
_UV_CACHE_MODE=custom
export UV_CACHE_DIR
if _uv_no_cache_requested; then
step "uv cache" "preserving custom UV_CACHE_DIR ($UV_CACHE_DIR); uv caching is off (UV_NO_CACHE), so nothing is recorded"
else
_record_uv_cache_choice
step "uv cache" "preserving custom UV_CACHE_DIR ($UV_CACHE_DIR)"
fi
return 0
;;
esac
fi
if [ "$_ISOLATE_UV_CACHE" = true ] || { [ "$_STUDIO_KEEPS_TOOLS" = true ] && _uv_cache_is_writable "$_uv_studio_cache"; }; then
UV_CACHE_DIR="$_uv_studio_cache"
_UV_CACHE_MODE=isolated
export UV_CACHE_DIR
_uv_no_cache_requested || _record_uv_cache_choice
step "uv cache" "forced Unsloth Studio cache isolation ($UV_CACHE_DIR); already-cached packages may download again" "$C_WARN"
return 0
fi
if _uv_no_cache_requested; then
UV_CACHE_DIR="$_uv_studio_cache"
_UV_CACHE_MODE=studio
export UV_CACHE_DIR
step "uv cache" "uv caching is off (UV_NO_CACHE); nothing to select or record"
return 0
fi
# Ask uv so uv.toml / UV_CONFIG_FILE count; take the last line so a notice is not the path.
_uv_default_cache=$(env -u UV_CACHE_DIR uv cache dir 2>/dev/null \
| sed -e 's/[[:space:]]*$//' -e '/^$/d' | tail -n 1) || _uv_default_cache=""
if [ -z "$_uv_default_cache" ]; then
if [ -n "${XDG_CACHE_HOME:-}" ]; then
_uv_default_cache="${XDG_CACHE_HOME}/uv"
elif [ -n "${HOME:-}" ]; then
_uv_default_cache="${HOME}/.cache/uv"
fi
fi
if [ -n "$_uv_default_cache" ]; then
_uv_default_cache=$(_absolutize_uv_cache_dir "$_uv_default_cache")
fi
# The recorded cache outranks uv's default while warm (same as studio.py _with_studio_uv_cache).
# Strip BOM and trailing CR: install.ps1 writes it with PowerShell 5.1 utf8 under WSL.
_uv_recorded=$(cat "$STUDIO_HOME/cache/uv-cache-dir" 2>/dev/null) || _uv_recorded=""
_uv_cr=$(printf '\r')
_uv_recorded="${_uv_recorded%"$_uv_cr"}"
unset _uv_cr
_uv_bom=$(printf '\357\273\277')
_uv_recorded="${_uv_recorded#"$_uv_bom"}"
unset _uv_bom
case "$_uv_recorded" in
*[![:space:]]*) _uv_recorded=$(_absolutize_uv_cache_dir "$_uv_recorded") ;;
*) _uv_recorded="" ;;
esac
# A pre-marker $STUDIO_HOME/cache/uv goes LAST, behind uv's default: its content may be
# leftover backend wheels while Torch and CUDA live in the default.
_uv_unmarked_studio=""
if [ -z "$_uv_recorded" ] || [ ! -d "$_uv_recorded" ]; then
_uv_unmarked_studio="$_uv_studio_cache"
fi
# Root and buckets must be writable; nested entries are not probed (too many files).
_uv_scan_blocked=false
_uv_blocked_cache=""
_uv_warn_cache=""
_uv_chosen_cache=""
for _uv_candidate in "$_uv_recorded" "$_uv_default_cache" "$_uv_unmarked_studio"; do
{ [ -n "$_uv_candidate" ] && [ -z "$_uv_chosen_cache" ]; } || continue
_uv_cand_populated=false
_uv_cand_writable=true
if [ -d "$_uv_candidate" ] && [ -r "$_uv_candidate" ]; then
# The globs below are the scan, so a caller's set -f would read every cache as empty.
_uv_glob=on
case $- in *f*) _uv_glob=off ;; esac
set +f
_uv_cache_is_writable "$_uv_candidate" || _uv_cand_writable=false
# Warm means package bytes: wheels-* holds metadata only. -L to match Get-ChildItem.
for _uv_bucket in \
"$_uv_candidate"/archive-* \
"$_uv_candidate"/builds-* \
"$_uv_candidate"/built-wheels-* \
"$_uv_candidate"/wheels-* \
"$_uv_candidate"/sdists-*; do
[ -d "$_uv_bucket" ] || continue
# Stricter than the probe: only kinds uv fills count (`archive-v0.backup` does not).
_uv_bucket_base="${_uv_bucket##*/}"
_uv_is_bucket_name "$_uv_bucket_base" || continue
case "${_uv_bucket_base%-v*}" in
archive|builds|built-wheels|wheels|sdists) ;;
*) continue ;;
esac
if [ ! -r "$_uv_bucket" ] || [ ! -x "$_uv_bucket" ]; then
_uv_scan_blocked=true
[ -n "$_uv_blocked_cache" ] || _uv_blocked_cache="$_uv_candidate"
continue
fi
# `|| true`: head closes the pipe and find's SIGPIPE under pipefail would drop the path.
_uv_artifact=$(find -L "$_uv_bucket" -type f \
! -name CACHEDIR.TAG ! -name .git ! -name .gitignore \
! -name '.unsloth-write-probe.*' \
! -name '*.lock' ! -name '*.msgpack' ! -name '*.http' ! -name '*.rev' \
-print 2>/dev/null | head -n 1) || true
if [ -n "$_uv_artifact" ]; then
_uv_cand_populated=true
break
fi
done
if [ "$_uv_glob" = off ]; then set -f; fi
fi
if [ "$_uv_cand_populated" = true ] && [ "$_uv_cand_writable" = true ]; then
_uv_chosen_cache="$_uv_candidate"
elif [ "$_uv_cand_populated" = true ] && [ -z "$_uv_warn_cache" ]; then
_uv_warn_cache="$_uv_candidate"
fi
done
unset _uv_candidate _uv_cand_populated _uv_cand_writable _uv_unmarked_studio
if [ -n "$_uv_chosen_cache" ]; then
UV_CACHE_DIR="$_uv_chosen_cache"
if [ "$_uv_chosen_cache" = "$_uv_studio_cache" ]; then
_UV_CACHE_MODE=studio
else
_UV_CACHE_MODE=shared
fi
else
UV_CACHE_DIR="$_uv_studio_cache"
_UV_CACHE_MODE=studio
# An unwritable Studio cache is no fallback: prefer the warm suspect cache, then uv's default.
if ! _uv_cache_is_writable "$_uv_studio_cache"; then
if [ -n "$_uv_warn_cache" ]; then
UV_CACHE_DIR="$_uv_warn_cache"
if [ "$_uv_warn_cache" != "$_uv_studio_cache" ]; then
_UV_CACHE_MODE=shared
fi
elif [ -n "$_uv_default_cache" ] \
&& [ "$_uv_default_cache" != "$_uv_studio_cache" ] \
&& _uv_cache_is_writable "$_uv_default_cache"; then
UV_CACHE_DIR="$_uv_default_cache"
_UV_CACHE_MODE=shared
_uv_studio_unusable=true
fi
fi
fi
export UV_CACHE_DIR
_record_uv_cache_choice
case "$_UV_CACHE_MODE" in
shared)