File size: 3,904 Bytes
0e03c98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a8a270
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e03c98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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