"""Content hash of the packaged python tree. The remote plugin stages a bundle under this hash, and a detached kernel and its bridge announce it, so the daemon can tell when a kernel runs older code than the daemon's own tree. """ from __future__ import annotations import os import hashlib from pathlib import Path ROOT = Path(__file__).resolve().parent def digest(root: Path = ROOT) -> str: """Hash Python sources and the kernel requirements lock in path order.""" hasher = hashlib.sha256() files: list[Path] = [] for directory, dirs, names in os.walk(root): dirs[:] = [d for d in dirs if d != ".venv"] base = Path(directory) files.extend( base / n for n in names if n.endswith(".py") or (base == root and n in {"pyproject.toml", "uv.lock"}) ) for path in sorted(files): hasher.update(str(path.relative_to(root)).encode()) with path.open("rb") as source: # Keep the path/contents digest stable while bounding each read. while chunk := source.read(64 * 1024): hasher.update(chunk) return hasher.hexdigest()