```python:inference/core/cache/air_gapped.py
import os
from functools import lru_cache

from inference.core.cache.model_artifacts import are_all_files_cached
from inference.core.env import MODEL_CACHE_DIR


def _slugify_model_id(model_id: str) -> str:
    """Reproduce the slug used by inference-models for cache directory names.

    Must stay in sync with
    ``inference_models.models.auto_loaders.core.slugify_model_id_to_os_safe_format``.
    """
    slug = re.sub(r"[^A-Za-z0-9_-]+", "-", model_id)
    slug = re.sub(r"[_-]{2,}", "-", slug)
    if not slug:
        slug = "special-char-only-model-id"
    if len(slug) > 48:
        slug = slug[:48]
    digest = hashlib.blake2s(model_id.encode("utf-8"), digest_size=4).hexdigest()
    return f"{slug}-{digest}"


# Cache slugification results to avoid repeated expensive hashing/regex operations
@lru_cache(maxsize=2048)
def _cached_slugify_model_id(model_id: str) -> str:
    return _slugify_model_id(model_id)


def _dir_has_entries(path: str) -> bool:
    """Return True if path is an existing directory and contains at least one entry.

    Uses os.scandir to avoid building a full list like os.listdir would.
    """
    if not os.path.isdir(path):
        return False
    # Use scandir as a lightweight iterator and close it promptly
    try:
        with os.scandir(path) as it:
            return next(it, None) is not None
    except PermissionError:
        # Let PermissionError propagate as listdir would in the same situation
        raise


def is_model_cached(model_id: str) -> bool:
    """Check if *model_id* has cached artifacts in either cache layout.

    Layout 1 (traditional): ``MODEL_CACHE_DIR/{model_id}/`` with files inside.
    Layout 2 (inference-models): ``MODEL_CACHE_DIR/models-cache/{slug}/`` with
    sub-directories containing model files.
    """
    # Traditional layout
    traditional_path = os.path.join(MODEL_CACHE_DIR, model_id)
    if _dir_has_entries(traditional_path):
        return True

    # inference-models layout
    slug = _cached_slugify_model_id(model_id)
    models_cache_path = os.path.join(MODEL_CACHE_DIR, "models-cache", slug)
    if _dir_has_entries(models_cache_path):
        return True

    return False


def is_block_cached(artifacts_spec) -> bool:
    """Check whether a block's required cache artifacts are present.

    Handles both formats returned by ``get_required_cache_artifacts()``:
    - **list of model_id strings** (new): block is cached if ANY variant exists.
    - **dict** with ``model_id`` and ``files`` keys (legacy): block is cached
      if all listed files exist for that model_id.

    Returns ``False`` for unrecognised formats.
    """
    if isinstance(artifacts_spec, list):
        for mid in artifacts_spec:
            if is_model_cached(mid):
                return True
        return False
    if isinstance(artifacts_spec, dict):
        model_id = artifacts_spec.get("model_id")
        required_files = artifacts_spec.get("files", [])
        if not model_id or not required_files:
            return False
        return are_all_files_cached(files=required_files, model_id=model_id)
    return False
```