Skip to content

The agent that grows with you

refactor(gateway): name the media-cache max age and lstat cache files once

kshitijk4poorc67b0a1dc4a0
diff --git a/gateway/platforms/base.py b/gateway/platforms/base.py
index 5e2adf5bd67..edc82e9412d 100644
--- a/gateway/platforms/base.py
+++ b/gateway/platforms/base.py
@@ -589,7 +589,7 @@ async def _read_httpx_body_with_limit(response, *, media_type: str) -> bytes:
def _cache_dir_accessors(kind: str, constant_name: str, new_subpath: str, old_name: str):
"""``(get_<kind>_cache_dir, cleanup_<kind>_cache)`` pair. The getter resolves fresh via
get_hermes_dir (active profile) unless a test monkeypatched the module constant away from
- its import-time default, and creates the directory; ``cleanup(max_age_hours=24)`` deletes
+ its import-time default, and creates the directory; ``cleanup(max_age_hours=MEDIA_CACHE_MAX_AGE_HOURS)`` deletes
older files and returns the count."""
def get_dir() -> Path:
d = get_hermes_dir(new_subpath, old_name)
diff --git a/gateway/run.py b/gateway/run.py
index 2b4f95104d6..016e1d67623 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -4636,6 +4636,7 @@ def _housekeeping_media_caches() -> None:
from tools.environments.local import cleanup_terminal_temp_cache
from tools.bot_mode_dm import cleanup_bot_dm_cache
from tools.bot_relay import cleanup_bot_relay_artifacts
+ from agent.provider_media import MEDIA_CACHE_MAX_AGE_HOURS
for cache_name, cleanup_fn in (
("Image", cleanup_image_cache), ("Document", cleanup_document_cache),
@@ -4644,7 +4645,7 @@ def _housekeeping_media_caches() -> None:
("Terminal temp", cleanup_terminal_temp_cache), ("Bot DM", cleanup_bot_dm_cache),
("Bot relay", cleanup_bot_relay_artifacts)):
def _one(name=cache_name, fn=cleanup_fn):
- removed = fn(max_age_hours=24)
+ removed = fn(max_age_hours=MEDIA_CACHE_MAX_AGE_HOURS)
if removed:
logger.info("%s cache cleanup: removed %d stale file(s)", name, removed)
_housekeeping_chore(f"{cache_name} cache cleanup", _one)
diff --git a/tools/credential_files.py b/tools/credential_files.py
index fedd79f806d..d20990305d7 100644
--- a/tools/credential_files.py
+++ b/tools/credential_files.py
@@ -10,6 +10,7 @@ from __future__ import annotations
import logging
import os
import posixpath
+import stat
import time
from contextvars import ContextVar
from pathlib import Path
@@ -385,23 +386,22 @@ def to_agent_visible_cache_path(host_path: str, container_base: str = "/root/.he
return mapped if mapped is not None else host_path
-def _recent_enough(item: Path, cutoff: Optional[float]) -> bool:
- try:
- return cutoff is None or item.stat().st_mtime >= cutoff
- except OSError:
- return False
-
-
def iter_cache_files(container_base: str = "/root/.hermes") -> List[Dict[str, str]]:
"""Per-file cache entries (Modal upload/resync); skips symlinks. ``cache/generated`` is
never swept, so only its files from the last ``MEDIA_CACHE_MAX_AGE_HOURS`` are synced —
otherwise every remote sync would re-walk and upload the whole generation history."""
generated_cutoff = time.time() - MEDIA_CACHE_MAX_AGE_HOURS * 3600
- base = container_base.rstrip("/")
- return [_mount(item, f"{root}/{item.relative_to(host_dir)}")
- for host_dir, root in _cache_dir_roots(container_base, create_missing=False)
- for item in host_dir.rglob("*") if not item.is_symlink() and item.is_file()
- and _recent_enough(item, generated_cutoff if root == f"{base}/{_GENERATED_CACHE}" else None)]
+ gen_root = f"{container_base.rstrip('/')}/{_GENERATED_CACHE}"
+ entries: List[Dict[str, str]] = []
+ for host_dir, root in _cache_dir_roots(container_base, create_missing=False):
+ for item in host_dir.rglob("*"):
+ try:
+ st = item.lstat()
+ except OSError:
+ continue
+ if stat.S_ISREG(st.st_mode) and (root != gen_root or st.st_mtime >= generated_cutoff):
+ entries.append(_mount(item, f"{root}/{item.relative_to(host_dir)}"))
+ return entries
def clear_credential_files() -> None: