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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: memoize per-repo lookups inside one model inventory scan (#15578)
Daniel Hanab88ee0cf057
diff --git a/studio/backend/core/inference/local_model_resolver.py b/studio/backend/core/inference/local_model_resolver.pyindex ff402aae0ef..32e4199cf01 100644--- a/studio/backend/core/inference/local_model_resolver.py+++ b/studio/backend/core/inference/local_model_resolver.py@@ -13,6 +13,7 @@ from typing import Optionalfrom core.inference.model_ids import public_model_idfrom loggers import get_logger+from utils.scan_memo import in_scan_memofrom utils.account_context import (AccountContext,account_thread,@@ -771,6 +772,7 @@ def local_load_dir(path: Optional[str]) -> Optional[str]:return path+@in_scan_memodef _build_index() -> dict[str, _LocalGgufEntry]:"""Scans the model picker's roots; Ollama manifests are read only, since load materializes the .gguf."""# Lazy import: routes.models imports core.inference.diff --git a/studio/backend/core/inference/media_model_index.py b/studio/backend/core/inference/media_model_index.pyindex 6f17ba693aa..5f78f391d0b 100644--- a/studio/backend/core/inference/media_model_index.py+++ b/studio/backend/core/inference/media_model_index.py@@ -14,6 +14,7 @@ from pathlib import Pathfrom typing import Any, Optionalfrom loggers import get_logger+from utils.scan_memo import in_scan_memologger = get_logger(__name__)@@ -261,6 +262,7 @@ def _loadable_directory(load_dir: Path) -> bool:return sole is not None and _loader_can_open(str(load_dir), sole)+@in_scan_memodef _build_index(task: str) -> dict[str, MediaModelPick]:"""Map every name a downloaded *task* model answers to onto its load spec."""from routes.models import _local_model_task, collect_local_modelsdiff --git a/studio/backend/core/library.py b/studio/backend/core/library.pyindex 6e621eba847..3773bb92199 100644--- a/studio/backend/core/library.py+++ b/studio/backend/core/library.py@@ -40,6 +40,7 @@ from storage import library_dbfrom utils.paths import relocations, storage_rootsfrom utils.paths.path_utils import is_path_within, same_pathfrom utils.paths.storage_roots import account_path+from utils.scan_memo import in_scan_memologger = get_logger(__name__)@@ -1204,6 +1205,7 @@ _SOURCES = ()+@in_scan_memodef list_items() -> list[dict]:"""Every item with its overlay applied, newest activity first. A failing source is skipped soone broken store cannot empty the whole Library."""diff --git a/studio/backend/hub/services/datasets/cache_inventory.py b/studio/backend/hub/services/datasets/cache_inventory.pyindex aff666b4b97..8ae49d06f40 100644--- a/studio/backend/hub/services/datasets/cache_inventory.py+++ b/studio/backend/hub/services/datasets/cache_inventory.py@@ -14,6 +14,7 @@ from typing import Optionalfrom fastapi import HTTPExceptionfrom loggers import get_logger+from utils.scan_memo import in_scan_memofrom hub.services import resolve_destructive_repo_idsfrom hub.services.datasets import downloads, local_options@@ -598,6 +599,7 @@ def _scan_hf_dataset_caches() -> list[dict]:return sorted(seen_lower.values(), key = lambda c: c["repo_id"])+@in_scan_memoasync def list_cached_datasets_response() -> dict:try:rows = await asyncio.to_thread(_scan_hf_dataset_caches)diff --git a/studio/backend/hub/services/models/cache_inventory.py b/studio/backend/hub/services/models/cache_inventory.pyindex 8a09e54e806..d12875e5292 100644--- a/studio/backend/hub/services/models/cache_inventory.py+++ b/studio/backend/hub/services/models/cache_inventory.py@@ -15,6 +15,7 @@ from pathlib import Pathfrom typing import NamedTuple, Optionalfrom loggers import get_logger+from utils.scan_memo import in_scan_memofrom hub.schemas.inventory import ModelFormatfrom hub.utils import inventory_scan as hf_cache_scan@@ -864,6 +865,7 @@ async def _shared_cached_inventory_scan(name: str, scanner) -> _CachedInventorySreturn _CachedInventoryScan(_last_confirmed_inventory.get(name, []), False)+@in_scan_memoasync def list_cached_gguf_response(hf_token: Optional[str] = None):"""List GGUF repos downloaded to HF cache, legacy Unsloth cache, and HF default cache."""try:@@ -1413,6 +1415,7 @@ def _scan_cached_models(return cached+@in_scan_memoasync def list_cached_models_response(hf_token: Optional[str] = None):"""List non-GGUF model repos downloaded to HF cache, legacy Unsloth cache, and HF default cache."""try:diff --git a/studio/backend/hub/services/models/companion_cleanup.py b/studio/backend/hub/services/models/companion_cleanup.pyindex c94e5fc6acd..2ece91b4cd1 100644--- a/studio/backend/hub/services/models/companion_cleanup.py+++ b/studio/backend/hub/services/models/companion_cleanup.py@@ -12,6 +12,7 @@ from typing import Optionalfrom fastapi import HTTPExceptionfrom loggers import get_logger+from utils.scan_memo import in_scan_memofrom hub.utils import companion_assets, download_registryfrom hub.utils.gguf import gguf_variant_key@@ -416,6 +417,7 @@ def _orphan_companions_blocking() -> dict:}+@in_scan_memoasync def orphan_companions_response() -> dict:"""Cached companion bases that no installed model needs. Listing only; nothing is deleted."""return await asyncio.to_thread(_orphan_companions_blocking)diff --git a/studio/backend/hub/services/models/local_inventory.py b/studio/backend/hub/services/models/local_inventory.pyindex f20af6de783..66e7d191671 100644--- a/studio/backend/hub/services/models/local_inventory.py+++ b/studio/backend/hub/services/models/local_inventory.py@@ -19,6 +19,7 @@ from pathlib import Pathfrom typing import List, NamedTuple, Optionalfrom loggers import get_logger+from utils.scan_memo import in_scan_memofrom hub.schemas.inventory import LocalModelInfo, LocalModelListResponse, ModelFormatfrom hub.storage.scan_folders import (@@ -1728,6 +1729,7 @@ def _classify_unsloth_models(models: List[LocalModelInfo]) -> List[LocalModelInfreturn models+@in_scan_memoasync def list_unsloth_models_response() -> LocalModelListResponse:# Off the loop with the scan: classifying a GGUF reads its header.try:@@ -1746,6 +1748,7 @@ async def _account_local_response(response):return response.model_copy(update = {"models": models})+@in_scan_memoasync def list_local_models_response(models_dir: str = "./models") -> LocalModelListResponse:"""Coalesce overlapping local inventory requests for the same models root."""diff --git a/studio/backend/hub/utils/hf_cache_state.py b/studio/backend/hub/utils/hf_cache_state.pyindex 48fa0ece3d6..dd168db5b5e 100644--- a/studio/backend/hub/utils/hf_cache_state.py+++ b/studio/backend/hub/utils/hf_cache_state.py@@ -12,6 +12,8 @@ import sysfrom pathlib import Path, PureWindowsPathfrom typing import Iterable, Iterator, Optional+from utils.scan_memo import memoized+EXIT_CANCELLED = 130@@ -292,6 +294,12 @@ def snapshot_selection_key(snapshot: Path) -> tuple[float, str]:def latest_snapshot_dir(repo_dir: Path) -> Optional[Path]:"""Newest immediate child of ``repo_dir/snapshots``, or None. mtime is the signal huggingface_hub's from_pretrained resolves to; ties fall to ``snapshot_selection_key`` so every caller names the same directory."""+ return memoized(+ ("latest_snapshot_dir", os.fspath(repo_dir)), lambda: _latest_snapshot_dir(repo_dir)+ )+++def _latest_snapshot_dir(repo_dir: Path) -> Optional[Path]:snapshots_dir = repo_dir / "snapshots"try:if not snapshots_dir.is_dir():diff --git a/studio/backend/hub/utils/inventory_scan.py b/studio/backend/hub/utils/inventory_scan.pyindex b8cec6d9dba..6822ea0f096 100644--- a/studio/backend/hub/utils/inventory_scan.py+++ b/studio/backend/hub/utils/inventory_scan.py@@ -17,6 +17,7 @@ from pathlib import Path, PurePosixPath, PureWindowsPathfrom typing import Awaitable, Callable, Hashable, NamedTuple, Optional, TypeVarfrom loggers import get_logger+from utils.scan_memo import in_scan_memo, invalidate_scan_memoslogger = get_logger(__name__)@@ -98,6 +99,7 @@ def invalidate_hf_cache_scans() -> None:_hf_cache_scans_result = None_hf_cache_scans_cached_at = 0.0_hf_cache_scans_epoch += 1+ invalidate_scan_memos()try:from core.inference.audio_cpp_models import forgetexcept Exception: # noqa: BLE001 - no audio.cpp support, nothing memoized@@ -398,6 +400,7 @@ def _with_repos_dropped_by_scan(return scan+@in_scan_memodef _compute_all_hf_cache_scans() -> list:from huggingface_hub import HFCacheInfo, scan_cache_dirdiff --git a/studio/backend/hub/utils/state_dir.py b/studio/backend/hub/utils/state_dir.pyindex bd4b2d52f67..86def2cd462 100644--- a/studio/backend/hub/utils/state_dir.py+++ b/studio/backend/hub/utils/state_dir.py@@ -13,6 +13,7 @@ from pathlib import Pathfrom typing import Literal, Optional, get_argsfrom loggers import get_logger+from utils.scan_memo import memoizedfrom hub.utils.paths import cache_root@@ -213,6 +214,13 @@ def _entry_key(def normalize_hub_cache(hub_cache: str | Path) -> str:"""Resolves, then applies normcase, so manifest writers and readers derive the same state key."""+ return memoized(+ ("normalize_hub_cache", os.fspath(hub_cache)),+ lambda: _normalize_hub_cache_uncached(hub_cache),+ )+++def _normalize_hub_cache_uncached(hub_cache: str | Path) -> str:try:resolved = str(Path(hub_cache).expanduser().resolve(strict = False))except (OSError, RuntimeError, ValueError):diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex 77f8f5d2020..42dfd621b0e 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -58,6 +58,7 @@ from utils.account_context import ()from utils.security.consent import MANAGED_REMOTE_CODE_REFUSAL, managed_remote_code_refusedfrom loggers import get_logger+from utils.scan_memo import in_scan_memofrom loggers.media_progress import (log_media_generation_progress,log_media_load_progress,@@ -35831,6 +35832,7 @@ async def _openai_catalog_with_aliases(quant_pins: bool = False) -> list[dict]:# compatibility alias for the canonical OpenAI path.@router.get("/models/", include_in_schema = False)@router.get("/models")+@in_scan_memoasync def openai_list_models(output_modalities: Optional[str] = None, current_subject: str = Depends(get_current_subject)):diff --git a/studio/backend/routes/models.py b/studio/backend/routes/models.pyindex 833743f8376..470f4b9fe0f 100644--- a/studio/backend/routes/models.py+++ b/studio/backend/routes/models.py@@ -13,6 +13,7 @@ from fastapi import APIRouter, Body, Depends, Header, HTTPException, Query, Requfrom pydantic import BaseModelfrom typing import List, NamedTuple, Optionalfrom loggers import get_logger+from utils.scan_memo import in_scan_memo# Dependency-light leaf (PEP 562 package init): no llama.cpp / torch import chain.from core.inference.memory_contract import (@@ -912,6 +913,7 @@ def _with_comfy_compat_rows(return existing + found+@in_scan_memodef collect_local_models(models_root: Path,*,@@ -1201,6 +1203,7 @@ def _compat_inventory_path_identity(path: object) -> str:return os.path.normcase(raw)+@in_scan_memoasync def _shared_compat_local_inventory_scan(models_root: Path, sources: Optional[_CompatLocalInventorySources] = None) -> List[LocalModelInfo]:@@ -5061,6 +5064,7 @@ async def list_cached_gguf(return {"cached": []}+@in_scan_memodef cached_gguf_rows(cache_scans = None) -> list[dict]:if cache_scans is None:cache_scans = _all_hf_cache_scans()@@ -5292,6 +5296,7 @@ def _repo_hosts_artifacts(repo_id: str) -> bool:return False+@in_scan_memodef cached_model_rows(cache_scans = None) -> list[dict]:_WEIGHT_EXTENSIONS = _NON_GGUF_WEIGHT_EXTENSIONSif cache_scans is None:diff --git a/studio/backend/routes/training_history.py b/studio/backend/routes/training_history.pyindex 537d1087d09..20d2203d05e 100644--- a/studio/backend/routes/training_history.py+++ b/studio/backend/routes/training_history.py@@ -41,6 +41,7 @@ from utils.models.checkpoints import has_preview_model, preview_reffrom utils.paths import outputs_root, resolve_output_dirfrom utils.preview_sharing_settings import get_preview_sharing_enabledfrom utils.preview_token import sign_preview_ref+from utils.scan_memo import in_scan_memologger = get_logger(__name__)@@ -116,6 +117,7 @@ def _summary_from_row()+@in_scan_memodef _summaries_from_rows(rows: list[dict], sharing_on: bool) -> list[TrainingRunSummary]:resource_cache: dict[str, bool] = {}return [_summary_from_row(row, sharing_on, resource_cache) for row in rows]diff --git a/studio/backend/tests/test_scan_memo.py b/studio/backend/tests/test_scan_memo.pynew file mode 100644index 00000000000..b89e3ab8ca9--- /dev/null+++ b/studio/backend/tests/test_scan_memo.py@@ -0,0 +1,213 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Per-scan memo: lookups repeat inside one inventory scan only, never across scans."""++import asyncio+import os+import sys+import threading+from pathlib import Path++import pytest++_backend = os.path.join(os.path.dirname(__file__), "..")+sys.path.insert(0, _backend)++from utils import hf_cache_settings+from utils.paths import storage_roots+from utils.scan_memo import in_scan_memo, memoized, scan_memo+++def test_memoized_outside_a_scope_always_computes():+ calls = []+ assert memoized("k", lambda: calls.append(1) or len(calls)) == 1+ assert memoized("k", lambda: calls.append(1) or len(calls)) == 2+++def test_memoized_inside_a_scope_computes_once_and_ends_with_it():+ calls = []+ compute = lambda: calls.append(1) or len(calls)+ with scan_memo():+ assert memoized("k", compute) == 1+ assert memoized("k", compute) == 1+ assert memoized("other", compute) == 2+ with scan_memo():+ assert memoized("k", compute) == 1+ assert memoized("k", compute) == 3+++def test_a_raise_is_not_cached():+ calls = []++ def compute():+ calls.append(1)+ if len(calls) == 1:+ raise OSError("transient")+ return "ok"++ with scan_memo():+ with pytest.raises(OSError):+ memoized("k", compute)+ assert memoized("k", compute) == "ok"+ assert memoized("k", compute) == "ok"+ assert len(calls) == 2+++def test_async_scope_reaches_to_thread_workers_and_closes():+ calls = []++ def lookup():+ return memoized("k", lambda: calls.append(threading.get_ident()) or len(calls))++ @in_scan_memo+ async def scan():+ first = await asyncio.to_thread(lookup)+ second = await asyncio.to_thread(lookup)+ return first, second++ assert asyncio.run(scan()) == (1, 1)+ assert asyncio.run(scan()) == (2, 2)+ assert lookup() == 3+++def test_a_thread_that_outlives_the_scope_stops_using_it():+ import contextvars++ calls = []+ compute = lambda: calls.append(1) or len(calls)+ with scan_memo():+ assert memoized("k", compute) == 1+ leaked = contextvars.copy_context()+ # A copied context still holds the dict, as a task or thread started in the scan would.+ assert leaked.run(memoized, "k", compute) == 2+ assert leaked.run(memoized, "k", compute) == 3++ def new_scan():+ with scan_memo():+ return memoized("k", compute), memoized("k", compute)++ # A scan begun from that context gets a fresh memo of its own.+ assert leaked.run(new_scan) == (4, 4)+++def test_sync_decorator_keeps_name_and_result():+ @in_scan_memo+ def scan(value):+ return [memoized("k", lambda: value), memoized("k", lambda: value + 1)]++ assert scan.__name__ == "scan"+ assert scan(1) == [1, 1]+ assert scan(5) == [5, 5]+++def test_studio_root_follows_env_between_scans(tmp_path, monkeypatch):+ first, second = tmp_path / "a", tmp_path / "b"+ first.mkdir()+ second.mkdir()+ monkeypatch.delenv("STUDIO_HOME", raising = False)+ monkeypatch.delenv("UNSLOTH_HOME", raising = False)+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(first))+ with scan_memo():+ assert storage_roots.studio_root() == first.resolve()+ # Keyed on the value, so a change inside a scan is still seen.+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(second))+ assert storage_roots.studio_root() == second.resolve()+ assert storage_roots.studio_root() == second.resolve()+++def test_studio_root_symlink_retarget_is_seen_by_the_next_scan(tmp_path, monkeypatch):+ first, second, link = tmp_path / "a", tmp_path / "b", tmp_path / "link"+ first.mkdir()+ second.mkdir()+ try:+ link.symlink_to(first, target_is_directory = True)+ except (OSError, NotImplementedError):+ pytest.skip(+ reason = "creating a symlink needs privileges this host lacks (Windows without Developer Mode)"+ )+ monkeypatch.delenv("STUDIO_HOME", raising = False)+ monkeypatch.delenv("UNSLOTH_HOME", raising = False)+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(link))+ with scan_memo():+ assert storage_roots.studio_root() == first.resolve()+ link.unlink()+ link.symlink_to(second, target_is_directory = True)+ with scan_memo():+ assert storage_roots.studio_root() == second.resolve()+ assert storage_roots.studio_root() == second.resolve()+++def test_hf_cache_setting_write_is_seen_by_the_next_scan(monkeypatch, tmp_path):+ stored = {"value": None}+ monkeypatch.setattr(hf_cache_settings, "_EXPLICIT_CACHE_ENV", {})+ monkeypatch.setattr(hf_cache_settings, "_portable_cache_home", lambda: None)+ monkeypatch.setattr(+ hf_cache_settings,+ "_stored_cache_home",+ lambda: Path(stored["value"]) if stored["value"] else None,+ )+ reads = []+ real = hf_cache_settings._get_hf_cache_paths_uncached+ monkeypatch.setattr(+ hf_cache_settings,+ "_get_hf_cache_paths_uncached",+ lambda: reads.append(1) or real(),+ )+ with scan_memo():+ default = hf_cache_settings.get_hf_cache_paths()+ assert hf_cache_settings.get_hf_cache_paths() is default+ assert len(reads) == 1+ stored["value"] = str(tmp_path / "custom")+ assert hf_cache_settings.get_hf_cache_paths().cache_home == tmp_path / "custom"+ with scan_memo():+ assert hf_cache_settings.get_hf_cache_paths().cache_home == tmp_path / "custom"+++def test_a_cache_invalidation_mid_scan_is_seen_by_the_same_scan(monkeypatch, tmp_path):+ from hub.utils import inventory_scan++ stored = {"value": None}+ monkeypatch.setattr(hf_cache_settings, "_EXPLICIT_CACHE_ENV", {})+ monkeypatch.setattr(hf_cache_settings, "_portable_cache_home", lambda: None)+ monkeypatch.setattr(+ hf_cache_settings,+ "_stored_cache_home",+ lambda: Path(stored["value"]) if stored["value"] else None,+ )+ with scan_memo():+ before = hf_cache_settings.get_hf_cache_paths()+ stored["value"] = str(tmp_path / "custom")+ assert hf_cache_settings.get_hf_cache_paths() is before+ inventory_scan.invalidate_hf_cache_scans()+ assert hf_cache_settings.get_hf_cache_paths().cache_home == tmp_path / "custom"+++def test_known_hub_caches_returns_a_fresh_list_per_call(monkeypatch, tmp_path):+ monkeypatch.setattr(hf_cache_settings, "_known_hf_hub_caches", lambda: [tmp_path])+ with scan_memo():+ first = hf_cache_settings.known_hf_hub_caches()+ first.append("mutated")+ assert hf_cache_settings.known_hf_hub_caches() == [tmp_path]+++def test_registered_model_root_sees_a_new_scan_folder_on_the_next_scan(tmp_path, monkeypatch):+ from storage import studio_db+ from utils.models import model_config++ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))+ monkeypatch.setattr(studio_db, "_schema_ready", set())+ # macOS tmp_path lives under /private/var, which the scan-folder denylist refuses.+ monkeypatch.setattr(studio_db, "_denied_path_prefixes", lambda: [])+ folder = tmp_path / "models"+ (folder / "m").mkdir(parents = True)+ target = str(folder / "m")+ with scan_memo():+ assert model_config._registered_custom_model_root(target) is None+ studio_db.add_scan_folder(str(folder))+ # Inside one scan the folder list is a snapshot.+ assert model_config._registered_custom_model_root(target) is None+ assert model_config._registered_custom_model_root(target) == Path(os.path.abspath(folder))+ with scan_memo():+ assert model_config._registered_custom_model_root(target) == Path(os.path.abspath(folder))+ studio_db.close_wal_keeper()diff --git a/studio/backend/utils/hf_cache_settings.py b/studio/backend/utils/hf_cache_settings.pyindex dd58f376e2e..1e38b8b0c3e 100644--- a/studio/backend/utils/hf_cache_settings.py+++ b/studio/backend/utils/hf_cache_settings.py@@ -194,7 +194,25 @@ def _portable_cache_home() -> Optional[Path]:return None+def _scan_memoized(name: str, compute):+ from utils.scan_memo import memoized++ environ = os.environ+ key = (+ name,+ environ.get("HOME"),+ environ.get("XDG_CACHE_HOME"),+ environ.get("UNSLOTH_PORTABLE"),+ environ.get("UNSLOTH_HOME"),+ )+ return memoized(key, compute)++def get_hf_cache_paths() -> HuggingFaceCachePaths:+ return _scan_memoized("hf_cache_paths", _get_hf_cache_paths_uncached)+++def _get_hf_cache_paths_uncached() -> HuggingFaceCachePaths:env_paths = _environment_paths()if env_paths is not None:return env_paths@@ -457,6 +475,10 @@ def effective_cache_home() -> Path:def known_hf_cache_homes() -> list[Path]:+ return list(_scan_memoized("known_hf_cache_homes", _known_hf_cache_homes))+++def _known_hf_cache_homes() -> list[Path]:paths = get_hf_cache_paths()stored = _stored_cache_home()candidates: list[Path] = []@@ -483,6 +505,10 @@ def known_hf_cache_homes() -> list[Path]:def known_hf_hub_caches() -> list[Path]:+ return list(_scan_memoized("known_hf_hub_caches", _known_hf_hub_caches))+++def _known_hf_hub_caches() -> list[Path]:active = get_hf_cache_paths()out = [active.hub_cache]seen = {os.path.normcase(str(_canonical(active.hub_cache)))}diff --git a/studio/backend/utils/models/model_config.py b/studio/backend/utils/models/model_config.pyindex a19b709b7fe..4058dd99a94 100644--- a/studio/backend/utils/models/model_config.py+++ b/studio/backend/utils/models/model_config.py@@ -2448,7 +2448,10 @@ def detect_dspark_file(def _registered_custom_model_root(path: str) -> Optional[Path]:try:from storage.studio_db import list_scan_folders- folders = list_scan_folders()+ from utils.paths import studio_db_path+ from utils.scan_memo import memoized++ folders = memoized(("scan_folders", str(studio_db_path())), list_scan_folders)except Exception:return Nonecandidate = Path(os.path.abspath(Path(path).expanduser()))diff --git a/studio/backend/utils/paths/storage_roots.py b/studio/backend/utils/paths/storage_roots.pyindex 958d792e8d3..61785c88028 100644--- a/studio/backend/utils/paths/storage_roots.py+++ b/studio/backend/utils/paths/storage_roots.py@@ -22,12 +22,20 @@ from loggers import get_loggerfrom utils.account_context import current_account, is_owner_contextfrom utils.paths.path_utils import drop_appledouble_metadata, host_normalize_pathfrom utils.paths.private_tmp import private_tmp_dir, private_tmp_path+from utils.scan_memo import memoizedlogger = get_logger(__name__)def _infer_studio_home_from_venv() -> Path | None:"""Needs a share/studio.conf or bin shim sentinel, so a dev venv named unsloth_studio is ignored."""+ return memoized(+ ("studio_home_from_venv", sys.prefix, os.environ.get("UNSLOTH_STUDIO_APP")),+ _infer_studio_home_from_venv_uncached,+ )+++def _infer_studio_home_from_venv_uncached() -> Path | None:try:prefix = Path(sys.prefix).resolve()except (OSError, ValueError):@@ -56,6 +64,10 @@ def _infer_studio_home_from_venv() -> Path | None:def _resolved(value: str) -> Path:+ return memoized(("resolved", value), lambda: _resolved_uncached(value))+++def _resolved_uncached(value: str) -> Path:try:return Path(value).expanduser().resolve()except (OSError, ValueError):diff --git a/studio/backend/utils/scan_memo.py b/studio/backend/utils/scan_memo.pynew file mode 100644index 00000000000..edf8664c107--- /dev/null+++ b/studio/backend/utils/scan_memo.py@@ -0,0 +1,78 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Memo for lookups a model inventory scan repeats per repo (root path resolves, the Hugging Face+cache setting, registered scan folders). Scoped to one scan, never kept across requests, so a+change is seen on the next one; outside a scope every lookup runs as before."""++from __future__ import annotations++import functools+import inspect+from contextlib import contextmanager+from contextvars import ContextVar+from typing import Any, Callable, Hashable, Iterator, Optional, TypeVar++_T = TypeVar("_T")++# asyncio.to_thread copies the context, so worker threads of one scan share its dict.+_scope: ContextVar[Optional[dict]] = ContextVar("studio_scan_memo", default = None)+# Set when the scope ends: a task or thread that copied the context and outlives it stops using it.+_CLOSED = object()+# Bumped by invalidate_hf_cache_scans: an inventory retry in the same scope must not reuse old roots.+_generation = 0+++def invalidate_scan_memos() -> None:+ global _generation+ _generation += 1+++@contextmanager+def scan_memo() -> Iterator[None]:+ """Memoize lookups inside; a nested scope joins the outer one."""+ current = _scope.get()+ if current is not None and _CLOSED not in current:+ yield+ return+ memo: dict = {}+ token = _scope.set(memo)+ try:+ yield+ finally:+ memo[_CLOSED] = True+ _scope.reset(token)+++def memoized(key: Hashable, compute: Callable[[], _T]) -> _T:+ """compute() once per scope for key; a raise is not cached."""+ memo = _scope.get()+ if memo is None or _CLOSED in memo:+ return compute()+ key = (_generation, key)+ try:+ return memo[key]+ except KeyError:+ pass+ value = compute()+ memo[key] = value+ return value+++def in_scan_memo(fn: Callable[..., Any]) -> Callable[..., Any]:+ """Run a sync or async function inside scan_memo()."""+ if inspect.iscoroutinefunction(fn):++ @functools.wraps(fn)+ async def run_async(*args, **kwargs):+ with scan_memo():+ return await fn(*args, **kwargs)++ return run_async++ @functools.wraps(fn)+ def run(*args, **kwargs):+ with scan_memo():+ return fn(*args, **kwargs)++ return run