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.

Studio: skip the audio gallery prune's sidecar pass under both caps and list prepared inputs once per sweep (#15874)

Daniel Handf42bc03b6bf
diff --git a/studio/backend/core/inference/audio_gallery.py b/studio/backend/core/inference/audio_gallery.py
index 1333f72cc58..ab734781660 100644
--- a/studio/backend/core/inference/audio_gallery.py
+++ b/studio/backend/core/inference/audio_gallery.py
@@ -148,6 +148,28 @@ def _clip_bytes(audio_id: str, directory: Path) -> int:
return total
+def _under_caps(directory: Path, cap: int, byte_cap: int) -> bool:
+ """All clip WAVs (a superset of what the prune ranks) fit both caps, decided without reading sidecars."""
+ try:
+ paths = _clip_wavs(directory)
+ except OSError:
+ return False
+ if cap > 0 and len(paths) > cap:
+ return False
+ if byte_cap <= 0:
+ return True
+ total = 0
+ for path in paths:
+ for counted in (path, directory / f"{path.stem}.source.wav"):
+ try:
+ total += counted.stat().st_size
+ except OSError:
+ pass
+ if total > byte_cap:
+ return False
+ return True
+
+
def _prune_to_cap() -> int:
"""Best-effort; only Unsloth-owned pairs are pruned, archived and pinned clips are exempt, and an unreadable flag store skips the prune"""
cap = _max_clips()
@@ -159,6 +181,8 @@ def _prune_to_cap() -> int:
try:
# Select AND delete under one lock, as clear() does; choosing victims from a snapshot lets an archive land and be deleted anyway.
with gallery_flags.exclusive(directory, require_file_lock = True):
+ if _under_caps(directory, cap, byte_cap):
+ return 0
# By age, not display order: a clip dragged down is not older. Pinned clips are exempt.
entries = [
e for e in _list_audio_entries(directory = directory) if not e[0].get("pinned")
diff --git a/studio/backend/core/inference/audio_inputs.py b/studio/backend/core/inference/audio_inputs.py
index d912a6b1865..27cb0cd0464 100644
--- a/studio/backend/core/inference/audio_inputs.py
+++ b/studio/backend/core/inference/audio_inputs.py
@@ -493,6 +493,31 @@ def _derived(directory: Path, stem: str) -> list[Path]:
return []
+def _derived_index(directory: Path) -> Optional[dict[str, list[Path]]]:
+ """``_derived`` for every id from one listing (an id has no dot); None where pathlib's case folding cannot be matched exactly."""
+ fold = os.path.normcase
+ insensitive = fold("A") != "A"
+ try:
+ names = os.listdir(directory)
+ except OSError:
+ return None
+ index: dict[str, list[Path]] = {}
+ for name in names:
+ if insensitive and not name.isascii():
+ return None
+ folded = fold(name)
+ key, dot, _rest = folded.partition(".")
+ if dot and folded.endswith(".wav") and len(folded) >= len(key) + 5:
+ index.setdefault(key, []).append(directory / name)
+ return index
+
+
+def _derived_from(index: Optional[dict[str, list[Path]]], directory: Path, stem: str) -> list[Path]:
+ if index is None or not _valid_id(stem):
+ return _derived(directory, stem)
+ return index.get(os.path.normcase(stem), [])
+
+
def _remove(directory: Path, input_id: str) -> bool:
wav = directory / f"{input_id}.wav"
existed = wav.is_file()
@@ -538,6 +563,7 @@ def sweep(
now = _now()
removed = 0
live: list[tuple[float, str, int]] = []
+ derived = _derived_index(directory)
for sidecar in directory.glob("*.json"):
input_id = sidecar.stem
meta = _read_sidecar(sidecar)
@@ -547,7 +573,7 @@ def sweep(
removed += _remove(directory, input_id)
continue
size = _size(directory / f"{input_id}.wav") + sum(
- _size(p) for p in _derived(directory, input_id)
+ _size(p) for p in _derived_from(derived, directory, input_id)
)
live.append((_touched(meta), input_id, size))
live.sort(reverse = True)
diff --git a/studio/backend/tests/test_audio_gallery_prune_caps.py b/studio/backend/tests/test_audio_gallery_prune_caps.py
new file mode 100644
index 00000000000..a5f045ea62b
--- /dev/null
+++ b/studio/backend/tests/test_audio_gallery_prune_caps.py
@@ -0,0 +1,66 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+from __future__ import annotations
+
+import os
+
+import pytest
+
+import core.inference.audio_gallery as gallery
+
+
[email protected](autouse = True)
+def _tmp_gallery(monkeypatch, tmp_path):
+ monkeypatch.setattr(gallery, "studio_root", lambda: tmp_path)
+
+
+def _wav():
+ return b"RIFF\x24\x00\x00\x00WAVEfmt "
+
+
+def _meta(**over):
+ base = {
+ "prompt": "hello from a sloth",
+ "model": "unsloth/orpheus-3b-0.1-ft",
+ "audio_type": "snac",
+ "sample_rate": 24000,
+ "duration_s": 1.5,
+ "created_at": "2026-08-06T00:00:00Z",
+ }
+ base.update(over)
+ return base
+
+
+def _save_with_mtime(prompt: str, t: float) -> dict:
+ record = gallery.save(_wav(), _meta(prompt = prompt))
+ os.utime(gallery.gallery_dir() / f"{record['id']}.wav", (t, t))
+ return record
+
+
+def test_prune_under_both_caps_reads_no_sidecar(monkeypatch):
+ for i in range(5):
+ gallery.save(_wav(), _meta(prompt = f"clip {i}"))
+ monkeypatch.setenv("UNSLOTH_AUDIO_GALLERY_MAX_CLIPS", "5")
+ reads = []
+ monkeypatch.setattr(gallery, "_read_meta", lambda path: reads.append(path))
+ assert gallery._prune_to_cap() == 0
+ assert reads == []
+
+
[email protected]("over", ["count", "bytes", "source_bytes"])
+def test_prune_just_over_a_cap_still_prunes_the_oldest(monkeypatch, over):
+ first = _save_with_mtime("first", 100.0)
+ if over == "source_bytes":
+ (gallery.gallery_dir() / f"{first['id']}.source.wav").write_bytes(b"s" * 64)
+ second = _save_with_mtime("second", 200.0)
+ size = (gallery.gallery_dir() / f"{second['id']}.wav").stat().st_size
+ if over == "count":
+ monkeypatch.setenv("UNSLOTH_AUDIO_GALLERY_MAX_CLIPS", "1")
+ else:
+ monkeypatch.setenv("UNSLOTH_AUDIO_GALLERY_MAX_CLIPS", "0")
+ slack = 63 if over == "source_bytes" else -1
+ monkeypatch.setenv("UNSLOTH_AUDIO_GALLERY_MAX_BYTES", str(2 * size + slack))
+ assert gallery._prune_to_cap() == 1
+ assert gallery.audio_path(first["id"]) is None
+ assert gallery.audio_path(second["id"]) is not None
diff --git a/studio/backend/tests/test_audio_inputs.py b/studio/backend/tests/test_audio_inputs.py
index 5672accb6b5..9778d3e0950 100644
--- a/studio/backend/tests/test_audio_inputs.py
+++ b/studio/backend/tests/test_audio_inputs.py
@@ -381,6 +381,62 @@ def test_the_sweep_reaps_a_prepared_upload_a_crash_left_behind():
assert not stale.exists() and waiting.exists()
+def _sidecar_for(directory, input_id, touched):
+ import json
+ meta = {
+ "name": "a.wav",
+ "duration_s": 1,
+ "sample_rate": 16000,
+ "channels": 1,
+ "sha256": input_id,
+ "created_at": "x",
+ "touched_at": touched,
+ }
+ (directory / f"{input_id}.json").write_text(json.dumps(meta), encoding = "utf-8")
+
+
+def test_the_sweep_lists_prepared_copies_once_for_every_live_input(monkeypatch):
+ directory = audio_inputs.inputs_dir()
+ now = time.time()
+ for i in range(6):
+ input_id = f"{i:032x}"
+ (directory / f"{input_id}.wav").write_bytes(b"w" * 10)
+ (directory / f"{input_id}.24000.mono.wav").write_bytes(b"d" * 10)
+ _sidecar_for(directory, input_id, now)
+ globs = []
+ real = audio_inputs._derived
+ monkeypatch.setattr(audio_inputs, "_derived", lambda d, s: globs.append(s) or real(d, s))
+ assert audio_inputs.sweep() == 0
+ assert globs == []
+
+
+def test_the_sweep_sizes_prepared_copies_like_the_per_input_glob(monkeypatch):
+ directory = audio_inputs.inputs_dir()
+ now = time.time()
+ names = {
+ "aaaa": ["aaaa.24000.mono.wav", "aaaa..wav", "aaaa.x.y.wav", "aaaa.1.wav.tmp"],
+ "aaaab": ["aaaab.1.wav"],
+ "bbbb": ["bbbb.16000.stereo.m30.wav"],
+ "cc-dd_1": ["cc-dd_1.24000.mono.wav", "cc-dd_1x.1.wav"],
+ }
+ for age, (input_id, derived) in enumerate(names.items()):
+ (directory / f"{input_id}.wav").write_bytes(b"w" * 100)
+ for name in derived:
+ (directory / name).write_bytes(b"d" * 1000)
+ _sidecar_for(directory, input_id, now - age)
+ (directory / "zz.notes.wav").write_bytes(b"q")
+ index = audio_inputs._derived_index(directory)
+ for input_id in names:
+ assert sorted(audio_inputs._derived_from(index, directory, input_id)) == sorted(
+ audio_inputs._derived(directory, input_id)
+ )
+ # aaaa = 100 + 3 * 1000 (the .tmp is not a WAV), aaaab = 1100, bbbb = 1100, cc-dd_1 = 1100.
+ assert audio_inputs.sweep(byte_cap = 3100 + 1100 + 1100) == 1
+ assert not (directory / "cc-dd_1.wav").exists()
+ assert (directory / "cc-dd_1x.1.wav").exists()
+ assert (directory / "bbbb.wav").exists()
+
+
@pytest.mark.parametrize(
"fmt, codec, rate, layout, start_s, end_s",
[