"""Store validated metadata in memory and on disk with expiry and atomic writes."""
from __future__ import annotations
import hashlib
import json
import os
import re
import shutil
import stat
import sys
import time
import warnings
from collections.abc import Callable
from contextlib import suppress
from pathlib import Path
from typing import Any
from platformdirs import user_cache_path
from . import _common
from . import upstream as _upstream
__all__ = [
"DEFAULT_CACHE_TTL_SECONDS",
"_MEMORY_CACHE",
"MetadataCacheWarning",
"_cache_dir",
"_cache_enabled",
"_cache_key",
"_cache_path",
"_cache_status",
"_cache_ttl_seconds",
"_cached_text",
"_clear_cache",
"_disk_cache_get",
"_disk_cache_set",
"_memory_cache_get",
"_memory_cache_set",
"_remove_readonly_cache_entry",
"_remove_readonly_cache_entry_legacy",
"_rmtree",
]
DEFAULT_CACHE_TTL_SECONDS = 24 * 60 * 60
_MEMORY_CACHE: dict[str, tuple[float, str]] = {}
_CACHE_NAMESPACES = {
"capabilities": "xml",
"valid_reff": "xml",
"scaife_library": "json",
}
[docs]
def _cache_enabled() -> bool:
"""Return whether the environment permits metadata caching."""
value = _common._normalize_space(
os.environ.get("PERSEUS_MCP_DISABLE_CACHE")
).casefold()
return value not in {"1", "true", "yes", "on"}
[docs]
def _cache_ttl_seconds() -> int:
"""Read the cache lifetime in seconds; zero disables expiry, not caching."""
value = _common._normalize_space(os.environ.get("PERSEUS_MCP_CACHE_TTL_SECONDS"))
if not value:
return DEFAULT_CACHE_TTL_SECONDS
try:
ttl = int(value)
except ValueError as exc:
raise ValueError("PERSEUS_MCP_CACHE_TTL_SECONDS must be an integer") from exc
return max(0, ttl)
[docs]
def _cache_dir() -> Path:
"""Resolve an override or the platform's shared per-user metadata cache."""
configured = os.environ.get("PERSEUS_MCP_CACHE_DIR", "").strip()
if configured:
return Path(configured).expanduser().absolute()
default = user_cache_path("perseus-mcp", appauthor=False)
# XDG cache roots should be absolute. Ignore a relative inherited root
# rather than silently making the default depend on the launching CWD.
return default if default.is_absolute() else Path.home() / ".cache" / "perseus-mcp"
[docs]
def _cache_key(parts: dict[str, Any]) -> str:
"""Hash a canonical JSON representation of request parameters for a disk-safe key."""
payload = json.dumps(parts, sort_keys=True, ensure_ascii=True, default=str)
return hashlib.sha256(payload.encode("utf-8")).hexdigest()
[docs]
def _cache_path(namespace: str, key: str, extension: str = "txt") -> Path:
"""Locate an entry within its cache namespace without creating directories."""
return _cache_dir() / namespace / f"{key}.{extension}"
[docs]
def _memory_cache_get(name: str) -> str | None:
"""Return an unexpired, previously validated memory entry or None."""
if not _cache_enabled():
return None
cached = _MEMORY_CACHE.get(name)
if cached is None:
return None
timestamp, value = cached
ttl = _cache_ttl_seconds()
if ttl and time.time() - timestamp > ttl:
_MEMORY_CACHE.pop(name, None)
return None
return value
[docs]
def _memory_cache_set(name: str, value: str) -> None:
"""Store validated text with its insertion time when caching is enabled."""
if _cache_enabled():
_MEMORY_CACHE[name] = (time.time(), value)
[docs]
def _disk_cache_get(path: Path) -> str | None:
"""Read unexpired UTF-8 text; return None on a miss or unreadable entry."""
if not _cache_enabled():
return None
try:
if not path.exists():
return None
ttl = _cache_ttl_seconds()
if ttl and time.time() - path.stat().st_mtime > ttl:
return None
return path.read_text(encoding="utf-8")
except (OSError, UnicodeError):
# Another process may clear or replace the optional cache between the
# existence check, stat, and read. Treat an unavailable entry as a
# cache miss so the caller can fetch a fresh upstream response.
return None
[docs]
def _disk_cache_set(path: Path, value: str) -> None:
"""Write a cache entry atomically.
PERSEUS_MCP_CACHE_DIR is documented as safe to share across multiple
local processes (an MCP server plus one or more notebook kernels). A
plain ``path.write_text(...)`` is not atomic, so two processes writing
the same cache key around the same time could interleave and leave a
corrupted file that fails to parse on the next read. Writing to a
process-unique temporary file in the same directory and then using
``os.replace`` (atomic on POSIX and Windows when source/destination are
on the same volume) avoids that.
"""
if not _cache_enabled():
return
path.parent.mkdir(parents=True, exist_ok=True)
tmp_path = path.with_name(f"{path.name}.tmp-{os.getpid()}-{time.monotonic_ns()}")
try:
tmp_path.write_text(value, encoding="utf-8")
os.replace(tmp_path, path)
except (OSError, UnicodeError):
with suppress(OSError):
tmp_path.unlink(missing_ok=True)
raise
[docs]
async def _cached_text(
namespace: str,
key_parts: dict[str, Any],
fetcher,
*,
extension: str = "xml",
refresh: bool = False,
disk_validator: Callable[[str], None] = _upstream.validate_text,
) -> str:
"""Cache validated fetch results; validate untrusted disk entries on load.
disk_validator runs only on disk loads, not memory hits or fresh fetches.
Fetchers must validate before returning (the production fetchers use _get).
Memory entries have already passed upstream or disk validation.
"""
key = _cache_key(key_parts)
memory_key = f"{namespace}:{key}"
path = _cache_path(namespace, key, extension)
if not refresh:
cached = _memory_cache_get(memory_key)
if cached is not None:
return cached
cached = _disk_cache_get(path)
if cached is not None:
try:
disk_validator(cached)
except _upstream.UpstreamFailure as exc:
_MEMORY_CACHE.pop(memory_key, None)
with suppress(OSError):
path.unlink(missing_ok=True)
warnings.warn(
f"Ignoring invalid {namespace} cache entry ({exc.code}).",
MetadataCacheWarning,
stacklevel=2,
)
else:
_memory_cache_set(memory_key, cached)
return cached
value = await fetcher()
_memory_cache_set(memory_key, value)
try:
_disk_cache_set(path, value)
except (OSError, UnicodeError) as exc:
warnings.warn(
f"Could not update optional metadata cache at {path}: {exc}. "
"Returning the upstream response without disk caching.",
MetadataCacheWarning,
stacklevel=2,
)
return value
[docs]
def _cache_status() -> str:
"""Serialize cache configuration, entry counts, and disk usage as JSON."""
cache_dir = _cache_dir()
sizes = []
for directory, extension in _cache_namespaces(cache_dir):
for path in _cache_files(directory, extension):
with suppress(FileNotFoundError):
sizes.append(path.stat().st_size)
return json.dumps(
{
"enabled": _cache_enabled(),
"cache_dir": str(cache_dir),
"ttl_seconds": _cache_ttl_seconds(),
"memory_entries": len(_MEMORY_CACHE),
"disk_files": len(sizes),
"disk_bytes": sum(sizes),
},
ensure_ascii=False,
indent=2,
)
[docs]
def _remove_readonly_cache_entry(function, path: str, error: BaseException) -> None:
"""Make a protected cache entry writable and retry its removal.
On Unix, a protected parent directory can prevent even ``chmod`` from
reaching the failed entry. Restore access to the parent first, then make
the entry writable and retry the failed removal operation.
"""
if not isinstance(error, PermissionError):
raise error
parent = os.path.dirname(path)
if parent and parent != path:
parent_mode = stat.S_IMODE(os.stat(parent, follow_symlinks=False).st_mode)
os.chmod(
parent,
parent_mode | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR,
)
path_mode = os.stat(path, follow_symlinks=False).st_mode
if stat.S_ISDIR(path_mode):
required_mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
else:
required_mode = stat.S_IRUSR | stat.S_IWUSR
os.chmod(path, stat.S_IMODE(path_mode) | required_mode)
if function in (os.open, os.scandir):
_rmtree(path)
return
function(path)
[docs]
def _remove_readonly_cache_entry_legacy(function, path: str, exc_info) -> None:
"""Adapt the Python 3.11 ``onerror`` callback to the ``onexc`` handler."""
_remove_readonly_cache_entry(function, path, exc_info[1])
[docs]
def _rmtree(path: str | os.PathLike[str]) -> None:
"""Remove a tree with the supported error callback API."""
if sys.version_info >= (3, 12):
shutil.rmtree(path, onexc=_remove_readonly_cache_entry)
else:
shutil.rmtree(path, onerror=_remove_readonly_cache_entry_legacy)
def _is_cache_link(path: Path) -> bool:
"""Identify symlinks and Windows reparse points without following them."""
info = path.lstat()
return stat.S_ISLNK(info.st_mode) or bool(
getattr(info, "st_file_attributes", 0)
& getattr(stat, "FILE_ATTRIBUTE_REPARSE_POINT", 0)
)
def _cache_namespaces(cache_dir: Path):
"""Yield existing metadata namespaces without traversing filesystem links."""
try:
if not cache_dir.exists() or _is_cache_link(cache_dir):
return
except FileNotFoundError:
return
for name, extension in _CACHE_NAMESPACES.items():
directory = cache_dir / name
with suppress(FileNotFoundError):
if directory.is_dir() and not _is_cache_link(directory):
yield directory, extension
def _cache_files(directory: Path, extension: str):
"""Yield only generated cache entries and temporary files in one namespace."""
pattern = rf"[0-9a-f]{{64}}\.{extension}(?:\.tmp-[0-9]+-[0-9]+)?"
with suppress(FileNotFoundError):
for path in directory.iterdir():
if re.fullmatch(pattern, path.name):
with suppress(FileNotFoundError):
if not _is_cache_link(path) and path.is_file():
yield path
[docs]
def _clear_cache() -> str:
"""Clear generated metadata entries without recursively deleting user directories."""
_MEMORY_CACHE.clear()
cache_dir = _cache_dir()
existed = cache_dir.exists()
files_removed = 0
for directory, extension in _cache_namespaces(cache_dir):
# Restore access only on a verified metadata namespace, never on its
# configured parent or on links to another directory.
try:
directory.chmod(
stat.S_IMODE(directory.stat().st_mode)
| stat.S_IRUSR
| stat.S_IWUSR
| stat.S_IXUSR
)
except FileNotFoundError:
continue
for path in _cache_files(directory, extension):
try:
path.unlink()
except FileNotFoundError:
continue
except PermissionError as exc:
_remove_readonly_cache_entry(os.unlink, str(path), exc)
files_removed += 1
with suppress(OSError):
directory.rmdir() # Only empty directories can be removed.
if files_removed:
with suppress(OSError):
cache_dir.rmdir()
return json.dumps(
{
"cache_dir": str(cache_dir),
"memory_entries": 0,
"disk_cache_removed": existed and not cache_dir.exists(),
"disk_files_removed": files_removed,
},
ensure_ascii=False,
indent=2,
)