Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/whichllm/models/benchmark_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def load_benchmark_cache() -> dict[str, float] | None:
logger.debug("Benchmark cache expired")
return None
return data.get("scores", {})
except (json.JSONDecodeError, KeyError) as e:
except (json.JSONDecodeError, UnicodeDecodeError, KeyError) as e:
logger.debug(f"Benchmark cache corrupted: {e}")
return None

Expand Down
2 changes: 1 addition & 1 deletion src/whichllm/models/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def load_cache() -> list[dict] | None:
logger.debug("Cache expired")
return None
return data.get("models", [])
except (json.JSONDecodeError, KeyError) as e:
except (json.JSONDecodeError, UnicodeDecodeError, KeyError) as e:
logger.debug(f"Cache corrupted: {e}")
return None

Expand Down
37 changes: 37 additions & 0 deletions tests/test_cache_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,43 @@ def test_model_cache_rejects_missing_provenance_schema(monkeypatch):
assert cache_mod.load_cache() is None


def _write_non_utf8_cache(path, payload):
"""Write a cache file the way a pre-0.5.12 Windows install left it.

Those versions wrote with ensure_ascii=False through the locale
codepage, so a cached model id containing non-ASCII lands on disk as
cp1252 bytes that are not valid UTF-8.
"""
path.write_text(json.dumps(payload, ensure_ascii=False), encoding="cp1252")
return path


def test_model_cache_survives_non_utf8_file(monkeypatch, tmp_path):
"""A cache file that is not valid UTF-8 is a cache miss, not a crash."""
cache_file = _write_non_utf8_cache(
tmp_path / "models.json",
{
"schema_version": cache_mod.CACHE_SCHEMA_VERSION,
"cached_at": time.time(),
"models": [{"id": "test/Café-Münster"}],
},
)
monkeypatch.setattr(cache_mod, "CACHE_FILE", cache_file)

assert cache_mod.load_cache() is None


def test_benchmark_cache_survives_non_utf8_file(monkeypatch, tmp_path):
"""Same for the benchmark cache, which has its own loader."""
cache_file = _write_non_utf8_cache(
tmp_path / "benchmarks.json",
{"cached_at": time.time(), "scores": {"test/Café-Münster": 1.0}},
)
monkeypatch.setattr(benchmark_mod, "BENCHMARK_CACHE", cache_file)

assert benchmark_mod.load_benchmark_cache() is None


def test_benchmark_cache_reads_and_writes_utf8(monkeypatch, tmp_path):
reader = _ReadableCacheFile(
{"cached_at": time.time(), "scores": {"test/Omega-Ω": 1.0}}
Expand Down