File size: 13,799 Bytes
21ff762 | 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | """
test_corpus_cache.py -- Tests for the Phase 2 on-disk embedding cache.
Focus areas:
- subject_id validation (rejects traversal, whitespace, empty)
- backend key slugging (isolates backends with unsafe name chars)
- get/put round-trip with content-hash keyed filenames
- content-change invalidation (stale entries miss silently)
- orphan sweep: put on new content removes the old vector file
- invalidate removes manifest row + vector file
- load_all / subjects / size
- backend isolation: two caches in the same root do not cross-pollute
- concurrent writers (ThreadPoolExecutor)
- dtype coercion (non-float32 input is coerced, output is float32)
- 1-D enforcement
- manifest JSON corruption is recovered by dropping the cache
"""
from __future__ import annotations
import json
import sys
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
import numpy as np
import pytest
SRC_DIR = Path(__file__).resolve().parents[1]
if str(SRC_DIR) not in sys.path:
sys.path.insert(0, str(SRC_DIR))
import corpus_cache as cc # noqa: E402
def _vec(n: int, seed: int) -> np.ndarray:
rng = np.random.default_rng(seed)
v = rng.normal(size=n).astype(np.float32)
return v / (np.linalg.norm(v) or 1.0)
# ────────────────────────────────────────────────────────────────────
# Backend key slugging
# ────────────────────────────────────────────────────────────────────
@pytest.mark.parametrize(
"name,expected",
[
("sentence-transformers:all-MiniLM-L6-v2", "sentence-transformers_all-MiniLM-L6-v2"),
("ollama:nomic-embed-text", "ollama_nomic-embed-text"),
("simple-name", "simple-name"),
],
)
def test_slug_backend_key_replaces_unsafe_chars(name: str, expected: str) -> None:
assert cc._slug_backend_key(name) == expected
@pytest.mark.parametrize("bad", ["", "::::", "///", " "])
def test_slug_backend_key_rejects_empty_or_unsafe(bad: str) -> None:
with pytest.raises(ValueError):
cc._slug_backend_key(bad)
def test_slug_backend_key_caps_length() -> None:
very_long = "x" * 500
assert len(cc._slug_backend_key(very_long)) == 64
# ────────────────────────────────────────────────────────────────────
# subject_id validation
# ────────────────────────────────────────────────────────────────────
@pytest.mark.parametrize(
"bad_id",
[
"",
"../escape",
"has/slash",
"has\\backslash",
"has space",
"-leading-dash",
".leading-dot",
"a" * 200,
],
)
def test_get_rejects_invalid_subject_id(bad_id: str, tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
with pytest.raises(ValueError, match="invalid subject_id"):
cache.get(bad_id, "content")
@pytest.mark.parametrize(
"bad_id",
["", "../escape", "has/slash", "has\\backslash"],
)
def test_put_rejects_invalid_subject_id(bad_id: str, tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
with pytest.raises(ValueError, match="invalid subject_id"):
cache.put(bad_id, "content", _vec(8, 0))
# ────────────────────────────────────────────────────────────────────
# Round-trip
# ────────────────────────────────────────────────────────────────────
def test_put_then_get_returns_same_vector(tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
v = _vec(8, 1)
cache.put("python-patterns", "body", v)
got = cache.get("python-patterns", "body")
assert got is not None
np.testing.assert_array_equal(got, v)
assert got.dtype == np.float32
def test_get_missing_subject_returns_none(tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
assert cache.get("nobody", "anything") is None
def test_get_with_changed_content_returns_none(tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
cache.put("skill", "original", _vec(8, 2))
assert cache.get("skill", "modified") is None
def test_put_replaces_vector_on_new_content(tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
v1, v2 = _vec(8, 3), _vec(8, 4)
cache.put("skill", "first", v1)
cache.put("skill", "second", v2)
# Old content hash should not be retrievable.
assert cache.get("skill", "first") is None
got = cache.get("skill", "second")
assert got is not None
np.testing.assert_array_equal(got, v2)
def test_put_orphan_sweep_removes_old_file(tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
cache.put("skill", "first", _vec(8, 5))
# Exactly one .npy exists.
npys = list(cache.backend_dir.glob("skill__*.npy"))
assert len(npys) == 1
cache.put("skill", "second", _vec(8, 6))
npys = list(cache.backend_dir.glob("skill__*.npy"))
assert len(npys) == 1 # old one deleted
# ────────────────────────────────────────────────────────────────────
# Metadata / manifest helpers
# ────────────────────────────────────────────────────────────────────
def test_entry_returns_metadata(tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
cache.put("skill", "body", _vec(16, 7))
e = cache.entry("skill")
assert e is not None
assert e.dim == 16
assert len(e.content_sha256) == 64
def test_entry_missing_returns_none(tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
assert cache.entry("nobody") is None
def test_subjects_yields_sorted(tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
for sid in ["charlie", "alpha", "bravo"]:
cache.put(sid, "b", _vec(4, hash(sid) & 0xFFFF))
assert list(cache.subjects()) == ["alpha", "bravo", "charlie"]
def test_size_counts_entries(tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
assert cache.size() == 0
cache.put("a", "x", _vec(4, 0))
cache.put("b", "x", _vec(4, 1))
assert cache.size() == 2
def test_load_all_returns_every_vector(tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
vectors = {f"s{i}": _vec(8, i) for i in range(5)}
for sid, v in vectors.items():
cache.put(sid, "body-" + sid, v)
loaded = cache.load_all()
assert set(loaded.keys()) == set(vectors.keys())
for sid, v in vectors.items():
np.testing.assert_array_equal(loaded[sid], v)
def test_load_all_skips_missing_files(tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
cache.put("a", "body", _vec(4, 0))
cache.put("b", "body", _vec(4, 1))
# Manually delete b's .npy to simulate corruption.
list(cache.backend_dir.glob("b__*.npy"))[0].unlink()
loaded = cache.load_all()
assert set(loaded.keys()) == {"a"}
# ────────────────────────────────────────────────────────────────────
# Invalidation
# ────────────────────────────────────────────────────────────────────
def test_invalidate_removes_entry(tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
cache.put("s", "body", _vec(4, 0))
assert cache.invalidate("s") is True
assert cache.get("s", "body") is None
assert cache.size() == 0
# Vector file gone too.
assert list(cache.backend_dir.glob("s__*.npy")) == []
def test_invalidate_missing_returns_false(tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
assert cache.invalidate("nothing-here") is False
# ────────────────────────────────────────────────────────────────────
# Backend isolation
# ────────────────────────────────────────────────────────────────────
def test_two_backends_do_not_share_entries(tmp_path: Path) -> None:
st_cache = cc.CorpusCache("sentence-transformers:all-MiniLM-L6-v2", root=tmp_path)
ollama_cache = cc.CorpusCache("ollama:nomic-embed-text", root=tmp_path)
st_cache.put("shared-id", "body", _vec(8, 10))
assert ollama_cache.get("shared-id", "body") is None
assert st_cache.get("shared-id", "body") is not None
ollama_cache.put("shared-id", "body", _vec(16, 11))
# Both now have entries, but sizes are independent.
assert st_cache.size() == 1
assert ollama_cache.size() == 1
assert st_cache.backend_dir != ollama_cache.backend_dir
# ────────────────────────────────────────────────────────────────────
# Concurrency
# ────────────────────────────────────────────────────────────────────
def test_concurrent_puts_all_land(tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
N = 32
def worker(i: int) -> None:
cache.put(f"s{i:03d}", f"body-{i}", _vec(8, i))
with ThreadPoolExecutor(max_workers=8) as pool:
list(pool.map(worker, range(N)))
assert cache.size() == N
loaded = cache.load_all()
assert len(loaded) == N
for i in range(N):
np.testing.assert_array_equal(loaded[f"s{i:03d}"], _vec(8, i))
# ────────────────────────────────────────────────────────────────────
# Input validation
# ────────────────────────────────────────────────────────────────────
def test_put_rejects_2d_vector(tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
with pytest.raises(ValueError, match="1-D"):
cache.put("s", "body", np.zeros((2, 4), dtype=np.float32))
def test_put_coerces_non_float32_input(tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
v = np.arange(8, dtype=np.float64)
cache.put("s", "body", v)
got = cache.get("s", "body")
assert got is not None
assert got.dtype == np.float32
np.testing.assert_array_equal(got, v.astype(np.float32))
# ────────────────────────────────────────────────────────────────────
# Corruption recovery
# ────────────────────────────────────────────────────────────────────
def test_corrupt_manifest_drops_cache_silently(tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
cache.put("s", "body", _vec(4, 0))
# Clobber the manifest with garbage.
cache.manifest_path.write_text("{{{ not json", encoding="utf-8")
assert cache.size() == 0
# size() reports 0 but the raw .npy is still there; a fresh put
# must succeed without exploding.
cache.put("s", "body-new", _vec(4, 1))
got = cache.get("s", "body-new")
assert got is not None
def test_manifest_non_dict_is_dropped(tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
cache._ensure_dir()
cache.manifest_path.write_text(json.dumps(["not", "a", "dict"]), encoding="utf-8")
assert cache.size() == 0
# ────────────────────────────────────────────────────────────────────
# clear()
# ────────────────────────────────────────────────────────────────────
def test_clear_wipes_backend_dir(tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
cache.put("s", "body", _vec(4, 0))
assert cache.backend_dir.exists()
cache.clear()
assert not cache.backend_dir.exists()
assert cache.size() == 0
def test_clear_on_empty_cache_is_ok(tmp_path: Path) -> None:
cache = cc.CorpusCache("st", root=tmp_path)
cache.clear() # no error
|