utils/dep_freshness/cache.py
Krzysztof kuhy Rudnicki 2a2f18ca56 Add the shared dependency-freshness gate
"Are we on the newest version?" was not mechanically answerable across these
repos: constraint styles had drifted per-ecosystem and a caret range is
"newest-compatible" by definition, which gives a checker nothing to compare.
This gate makes it answerable and then enforces the answer.

Checks two things at once -- that every dependency carries an exact pin, and
that the pin equals its registry's latest STABLE release -- across pub.dev,
PyPI, npm, crates.io, the Go proxy, kuhyx/utils git tags, and the Flutter /
Node / Python toolchains.

Three deliberate design points:

* Pre-releases are rejected per-ecosystem, because every ecosystem leaks them
  differently and each leak fails GREEN. npm lets a maintainer point
  `dist-tags.latest` at an rc; crates.io's `max_version` includes them; PyPI's
  legacy `info.version` includes yanked releases. Unit-tested with fixtures,
  since an end-to-end run cannot surface a wrong "up to date".

* Offline, the gate serves an expired cache entry and PASSES with a DEGRADED
  banner (exit 3 only under --strict, which is CI). `--no-verify` is banned
  here, so a hook that hard-failed offline would leave no way to commit at all.

* The allowlist has two entry classes. `blocked_by: transitive:<pkg>@<ver>` is
  a predicate that clears itself when upstream moves and must NOT carry a
  date; everything else is discretionary and requires `expires`, capped at 90
  days. Exact pins plus latest-everywhere is provably unsatisfiable when a
  dependency internally constrains a shared transitive, and expiring the
  predicate class on a calendar would turn the gate red for a reason no commit
  could fix. An entry with nothing left to excuse is itself an error.

One hook id, one shim, one shared implementation -- always delegating, so it
cannot fork the way the 250-line cap forked into four ids, two of which were
independent reimplementations.

256 tests, 100% branch coverage on every module of the gate. Also adds the
workflow that runs them, which incidentally gives md_naming's 71 tests their
first CI run.

Not yet installed in any repo: a repo that gets the gate before its deps are
bumped is instantly and permanently red, so the gate and the upgrade land
together, per repo.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CRykabMwPgDzFHcARoqpaK
2026-08-28 13:00:35 +02:00

113 lines
3.4 KiB
Python

"""On-disk snapshot of registry answers, shared across all 78 manifests.
Overlapping dependencies are fetched once per TTL rather than once per repo,
and — the load-bearing part — an *expired* entry is still served when the
network is down. A pre-commit hook that hard-fails offline would corner the
user, because `--no-verify` is banned.
"""
from __future__ import annotations
from dataclasses import dataclass
import json
import os
from pathlib import Path
import tempfile
import time
from dep_freshness._tables import (
CACHE_FILE,
CACHE_PATH_ENV,
DEFAULT_CACHE_DIR,
GITTAG,
TTL_GITTAG_SECONDS,
TTL_SECONDS,
)
def cache_dir() -> Path:
"""Where the snapshot lives; overridable for tests via the env var."""
raw = os.environ.get(CACHE_PATH_ENV) or DEFAULT_CACHE_DIR
return Path(raw).expanduser()
def ttl_for(ecosystem: str) -> int:
"""Git tags move rarely and cost a subprocess, so they cache longer."""
return TTL_GITTAG_SECONDS if ecosystem == GITTAG else TTL_SECONDS
@dataclass
class Entry:
"""One cached lookup, plus how old it is."""
version: str | None
fetched_at: float
def age(self, now: float | None = None) -> float:
return (time.time() if now is None else now) - self.fetched_at
def fresh(self, ecosystem: str, now: float | None = None) -> bool:
return self.age(now) < ttl_for(ecosystem)
class Cache:
"""A JSON file keyed `ecosystem/package` -> {version, fetched_at}."""
def __init__(self, path: Path | None = None) -> None:
self.path = path or (cache_dir() / CACHE_FILE)
self._data: dict[str, dict] = {}
self._dirty = False
self._load()
def _load(self) -> None:
try:
raw = self.path.read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError):
return
try:
parsed = json.loads(raw)
except ValueError:
return # a corrupt cache is a cold cache, never an error
if isinstance(parsed, dict):
self._data = {k: v for k, v in parsed.items() if isinstance(v, dict)}
@staticmethod
def key(ecosystem: str, package: str) -> str:
return f"{ecosystem}/{package}"
def get(self, ecosystem: str, package: str) -> Entry | None:
record = self._data.get(self.key(ecosystem, package))
if record is None:
return None
try:
return Entry(record.get("version"), float(record["fetched_at"]))
except (KeyError, TypeError, ValueError):
return None
def put(self, ecosystem: str, package: str, version: str | None) -> None:
self._data[self.key(ecosystem, package)] = {
"version": version,
"fetched_at": time.time(),
}
self._dirty = True
def clear(self) -> None:
self._data = {}
self._dirty = True
def save(self) -> None:
"""Atomic write; a half-written cache would poison every later run."""
if not self._dirty:
return
try:
self.path.parent.mkdir(parents=True, exist_ok=True)
with tempfile.NamedTemporaryFile(
"w", dir=self.path.parent, delete=False, encoding="utf-8"
) as handle:
json.dump(self._data, handle)
temp = Path(handle.name)
temp.replace(self.path)
self._dirty = False
except OSError:
pass # an unwritable cache slows the next run; it must not fail it