File size: 4,682 Bytes
f4cc109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""Persistent SQLite cache for judge decisions.

The cache key covers everything that can change a judged label:

    case id | gold text | prediction text | provider | model | prompt version | evaluation version

Changing any of them yields a different key, so a re-run after editing a
prediction, swapping the judge model or publishing a new prompt version does not
reuse a stale label.
"""

from __future__ import annotations

import hashlib
import sqlite3
import threading
from pathlib import Path
from typing import Optional

__all__ = ["JudgeCache", "NullCache", "make_cache_key"]

_SCHEMA = """
CREATE TABLE IF NOT EXISTS judgements (
    key            TEXT PRIMARY KEY,
    case_id        TEXT NOT NULL,
    label          TEXT,
    raw_output     TEXT,
    attempts       INTEGER DEFAULT 0,
    error          TEXT,
    judge_provider TEXT,
    judge_model    TEXT,
    prompt_version TEXT,
    evaluation_version TEXT,
    created_at     TEXT DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_judgements_case ON judgements(case_id);
"""


def make_cache_key(
    case_id: str,
    gold_text: str,
    pred_text: str,
    judge_provider: str,
    judge_model: str,
    prompt_version: str,
    evaluation_version: str,
) -> str:
    digest = hashlib.sha256()
    for part in (
        case_id,
        gold_text,
        pred_text,
        judge_provider,
        judge_model,
        prompt_version,
        evaluation_version,
    ):
        digest.update(str(part).encode("utf-8"))
        digest.update(b"\x1f")  # unit separator: prevents field-boundary collisions
    return digest.hexdigest()


class CacheEntry:
    __slots__ = ("label", "raw_output", "attempts", "error")

    def __init__(self, label, raw_output, attempts, error):
        self.label = label
        self.raw_output = raw_output
        self.attempts = attempts
        self.error = error


class JudgeCache:
    """Thread-safe SQLite-backed cache.

    Only *successful* judgements (a valid label) are stored; a failed call is not
    cached so that a re-run retries it, which is what ``--resume`` needs.
    """

    def __init__(self, path: Path | str) -> None:
        self.path = Path(path)
        self.path.parent.mkdir(parents=True, exist_ok=True)
        self._lock = threading.Lock()
        self._conn = sqlite3.connect(str(self.path), check_same_thread=False)
        self._conn.execute("PRAGMA journal_mode=WAL")
        self._conn.executescript(_SCHEMA)
        self._conn.commit()
        self.hits = 0
        self.misses = 0

    def get(self, key: str) -> Optional[CacheEntry]:
        with self._lock:
            row = self._conn.execute(
                "SELECT label, raw_output, attempts, error FROM judgements WHERE key = ?",
                (key,),
            ).fetchone()
        if row is None:
            self.misses += 1
            return None
        self.hits += 1
        return CacheEntry(row[0], row[1], row[2] or 0, row[3])

    def put(
        self,
        key: str,
        case_id: str,
        label: Optional[str],
        raw_output: Optional[str],
        attempts: int,
        error: Optional[str],
        judge_provider: str,
        judge_model: str,
        prompt_version: str,
        evaluation_version: str,
    ) -> None:
        if label is None:
            return  # never cache a failure
        with self._lock:
            self._conn.execute(
                "INSERT OR REPLACE INTO judgements "
                "(key, case_id, label, raw_output, attempts, error, judge_provider, "
                " judge_model, prompt_version, evaluation_version) "
                "VALUES (?,?,?,?,?,?,?,?,?,?)",
                (
                    key,
                    case_id,
                    label,
                    raw_output,
                    attempts,
                    error,
                    judge_provider,
                    judge_model,
                    prompt_version,
                    evaluation_version,
                ),
            )
            self._conn.commit()

    def close(self) -> None:
        with self._lock:
            self._conn.close()

    def __enter__(self) -> "JudgeCache":
        return self

    def __exit__(self, *exc) -> None:
        self.close()


class NullCache:
    """Cache-shaped no-op used when ``cache: false`` or ``--no-cache``."""

    hits = 0
    misses = 0

    def get(self, key: str) -> None:
        return None

    def put(self, *args, **kwargs) -> None:
        return None

    def close(self) -> None:
        return None

    def __enter__(self) -> "NullCache":
        return self

    def __exit__(self, *exc) -> None:
        return None