Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(lint): retire the single-slot base-counts cache

mateo-berri526f6d793ec9
diff --git a/scripts/type_check_gate.py b/scripts/type_check_gate.py
index 2fab7f8363a..c9f774c6113 100644
--- a/scripts/type_check_gate.py
+++ b/scripts/type_check_gate.py
@@ -63,7 +63,7 @@ import sys
import tempfile
import zipfile
from collections import Counter
-from collections.abc import Callable, Iterator, Mapping
+from collections.abc import Callable, Iterator, Mapping, Sequence
from pathlib import Path
from typing import Final, NamedTuple
@@ -73,6 +73,7 @@ PYRIGHT_CONFIG = REPO_ROOT / "pyrightconfig.json"
UV_LOCK = REPO_ROOT / "uv.lock"
DEFAULT_BASE = "origin/litellm_internal_staging"
CACHE_FILE_PREFIX = "basedpyright-base-"
+CACHE_KEEP_ENTRIES = 8
ARTIFACT_NAME_PREFIX = "basedpyright-counts-"
GH_TIMEOUT_SECONDS = 10
@@ -367,16 +368,30 @@ def counts_payload(base_point: str, counts: Mapping[str, int]) -> str:
)
+def entry_recency(path: Path) -> float:
+ try:
+ return path.stat().st_mtime
+ except OSError:
+ return 0.0
+
+
+def evicted_beyond_cap(entries: Sequence[Path], keep: int) -> tuple[Path, ...]:
+ newest_first: Final = sorted(entries, key=entry_recency, reverse=True)
+ return tuple(newest_first[keep:])
+
+
def store_counts(
directory: Path, path: Path, base_point: str, counts: Mapping[str, int]
) -> None:
directory.mkdir(parents=True, exist_ok=True)
- for stale in directory.glob(f"{CACHE_FILE_PREFIX}*.json"):
- if stale != path:
- stale.unlink(missing_ok=True)
scratch = scratch_path(path)
scratch.write_text(counts_payload(base_point, counts))
scratch.replace(path)
+ siblings: Final = tuple(
+ entry for entry in directory.glob(f"{CACHE_FILE_PREFIX}*.json") if entry != path
+ )
+ for stale in evicted_beyond_cap(siblings, CACHE_KEEP_ENTRIES - 1):
+ stale.unlink(missing_ok=True)
def parse_origin_slug(url: str) -> str | None:
diff --git a/tests/test_litellm/test_type_check_gate.py b/tests/test_litellm/test_type_check_gate.py
index 6cf21707629..d104e4ca0c8 100644
--- a/tests/test_litellm/test_type_check_gate.py
+++ b/tests/test_litellm/test_type_check_gate.py
@@ -1,6 +1,7 @@
import hashlib
import importlib.util
import json
+import os
import subprocess
from pathlib import Path
@@ -387,15 +388,45 @@ def test_store_prune_spares_a_concurrent_runs_in_flight_scratch(tmp_path):
assert gate.load_cached_counts(mine) == {"reportAny": 1}
-def test_store_prunes_entries_for_other_branch_points(tmp_path):
+def test_store_keeps_a_concurrent_worktrees_entry_for_another_branch_point(tmp_path):
old = gate.cache_path(tmp_path, "old", ("f",))
gate.store_counts(tmp_path, old, "old", {"reportAny": 1})
new = gate.cache_path(tmp_path, "new", ("f",))
gate.store_counts(tmp_path, new, "new", {"reportAny": 2})
- assert not old.exists()
+ assert gate.load_cached_counts(old) == {"reportAny": 1}
assert gate.load_cached_counts(new) == {"reportAny": 2}
+def test_store_evicts_only_the_oldest_entries_beyond_the_cap(tmp_path):
+ aged = [
+ gate.cache_path(tmp_path, f"base{i}", ("f",))
+ for i in range(gate.CACHE_KEEP_ENTRIES)
+ ]
+ for age, path in enumerate(aged):
+ gate.store_counts(tmp_path, path, f"base{age}", {"reportAny": age})
+ os.utime(path, (age, age))
+ newest = gate.cache_path(tmp_path, "newest", ("f",))
+ gate.store_counts(tmp_path, newest, "newest", {"reportAny": 99})
+ assert not aged[0].exists()
+ assert all(path.exists() for path in aged[1:])
+ assert gate.load_cached_counts(newest) == {"reportAny": 99}
+
+
+def test_store_never_evicts_the_entry_it_just_wrote_even_on_mtime_ties(tmp_path):
+ others = [
+ gate.cache_path(tmp_path, f"base{i}", ("f",))
+ for i in range(gate.CACHE_KEEP_ENTRIES + 2)
+ ]
+ for path in others:
+ gate.store_counts(tmp_path, path, path.name, {"reportAny": 1})
+ os.utime(path, (9_999_999_999, 9_999_999_999))
+ mine = gate.cache_path(tmp_path, "mine", ("f",))
+ gate.store_counts(tmp_path, mine, "mine", {"reportAny": 2})
+ assert gate.load_cached_counts(mine) == {"reportAny": 2}
+ survivors = list(tmp_path.glob(f"{gate.CACHE_FILE_PREFIX}*.json"))
+ assert len(survivors) == gate.CACHE_KEEP_ENTRIES
+
+
def _no_fetch(ref):
return None