LUNA / cache.py
Malina
Claude Opus 5 (1M context)
Cache pamieta to, co sie nie zmieni, i nie pamieta naszych awarii
1a8a270
Raw History Blame Contribute Delete
3.9 kB
# Per-file MAP cache, keyed by what the file IS rather than where it sits.
#
# CACHE_DIR was declared in config.py and never used: every build re-read and re-mapped
# every file, including a retry of a build that had just failed. Uploads land under a
# fresh temporary path each time, so a path-keyed cache would never hit; a content
# fingerprint does.
#
# The MAP result depends on the prompt as well as the file, so both go into the key.
# Best-effort throughout: any failure just means a miss and the normal path runs.
import hashlib
import json
import os
import time
from config import CACHE_DIR, MAP_MODEL, VLM_MODEL
TTL_SECONDS = int(os.getenv("LUNA_CACHE_TTL", str(6 * 3600)))
_CHUNK = 1 << 20 # 1 MB from each end is plenty to tell files apart
def fingerprint(path: str) -> str:
"""Size plus the first and last megabyte. Hashing a 200 MB upload in full would
cost more than the work being cached."""
h = hashlib.sha1()
st = os.stat(path)
h.update(str(st.st_size).encode())
with open(path, "rb") as f:
h.update(f.read(_CHUNK))
if st.st_size > _CHUNK:
f.seek(-_CHUNK, os.SEEK_END)
h.update(f.read(_CHUNK))
return h.hexdigest()[:16]
def _key(path: str, prompt: str) -> str | None:
try:
fp = fingerprint(path)
except OSError:
return None
ctx = hashlib.sha1(f"{prompt}\x00{MAP_MODEL}\x00{VLM_MODEL}".encode()).hexdigest()[:12]
return f"{fp}-{ctx}"
def _path_for(key: str) -> str:
return os.path.join(CACHE_DIR, key + ".json")
def evict_stale() -> None:
now = time.time()
try:
for name in os.listdir(CACHE_DIR):
p = os.path.join(CACHE_DIR, name)
try:
if now - os.path.getmtime(p) > TTL_SECONDS:
os.remove(p)
except OSError:
pass
except OSError:
pass
# --- prompt-independent layer -------------------------------------------------
#
# What a file SAYS does not depend on what was asked of it. Transcribing a five
# minute video costs Whisper about forty seconds, and the wizard invites people to
# refine their prompt and build again, which changed the key above and paid for the
# same transcript a second time. This layer is keyed by the file alone.
def _raw_key(path: str, kind: str) -> str | None:
try:
return f"{fingerprint(path)}-raw-{kind}"
except OSError:
return None
def load_raw(path: str, kind: str):
key = _raw_key(path, kind)
if not key:
return None
try:
p = _path_for(key)
if time.time() - os.path.getmtime(p) > TTL_SECONDS:
return None
with open(p, encoding="utf-8") as f:
return json.load(f)
except (OSError, ValueError):
return None
def store_raw(path: str, kind: str, value) -> None:
key = _raw_key(path, kind)
if not key:
return
try:
os.makedirs(CACHE_DIR, exist_ok=True)
tmp = _path_for(key) + ".tmp"
with open(tmp, "w", encoding="utf-8") as f:
json.dump(value, f)
os.replace(tmp, _path_for(key))
except OSError:
pass
def load(path: str, prompt: str) -> dict | None:
key = _key(path, prompt)
if not key:
return None
try:
p = _path_for(key)
if time.time() - os.path.getmtime(p) > TTL_SECONDS:
return None
with open(p, encoding="utf-8") as f:
return json.load(f)
except (OSError, ValueError):
return None
def store(path: str, prompt: str, result: dict) -> None:
key = _key(path, prompt)
if not key:
return
try:
os.makedirs(CACHE_DIR, exist_ok=True)
tmp = _path_for(key) + ".tmp"
with open(tmp, "w", encoding="utf-8") as f:
json.dump(result, f)
os.replace(tmp, _path_for(key))
except OSError:
pass