```python:inference/core/cache/air_gapped.py
import hashlib
import os
import re

from inference.core.env import MODEL_CACHE_DIR, USE_INFERENCE_MODELS

# Precompile regexes to avoid repeated compilation overhead.
_NON_ALLOWED_RE = re.compile(r"[^A-Za-z0-9_-]+")
_CONSEC_RE = re.compile(r"[_-]{2,}")


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 = _NON_ALLOWED_RE.sub("-", model_id)
    slug = _CONSEC_RE.sub("-", 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}"


def _has_non_hidden_children(path: str) -> bool:
    """Return True if *path* contains at least one non-hidden entry.

    Hidden files (names starting with ``"."``) are excluded because they may be
    stale lock-file leftovers that do not represent usable model artifacts.
    """
    try:
        # Use scandir to avoid building a full list of entries and to reduce
        # syscall overhead compared to listdir + subsequent stats.
        with os.scandir(path) as it:
            for entry in it:
                if not entry.name.startswith("."):
                    return True
        return False
    except OSError:
        return False


def is_model_cached(model_id: str) -> bool:
    """Best-effort check whether *model_id* has cached artifacts.

    Checks both the traditional and ``inference-models`` cache layouts,
    respecting the ``USE_INFERENCE_MODELS`` flag to avoid false positives
    from one layout when the runtime uses the other.

    .. note::

       This is intentionally optimistic — a directory with non-hidden files
       is assumed to contain a usable model.  Full integrity verification
       (hash checks, registry validation) happens at model-load time inside
       ``inference-models``.  Treat the result as *"there is a chance the
       model is cached"* rather than a guarantee.
    """
    if not USE_INFERENCE_MODELS:
        # Only check the traditional layout when inference-models is disabled.
        traditional_path = os.path.join(MODEL_CACHE_DIR, model_id)
        return _has_non_hidden_children(traditional_path)

    # When inference-models is enabled, check both layouts — models cached
    # before the migration still sit in the traditional tree.

    # When inference-models is enabled, check both layouts — models cached
    # before the migration still sit in the traditional tree.
    traditional_path = os.path.join(MODEL_CACHE_DIR, model_id)
    if _has_non_hidden_children(traditional_path):
        return True

    slug = _slugify_model_id(model_id)
    models_cache_path = os.path.join(MODEL_CACHE_DIR, "models-cache", slug)
    if _has_non_hidden_children(models_cache_path):
        return True

    return False
```