#!/usr/bin/env python3 """Diagnose heretic2 boot Failure B. When PLE_ST_DEBUG_GETTENSOR=1, logs every safetensors tensor read to /tmp/stdbg-.log so the last line before an UntypedStorage ValueError names the tensor that fails. Installed via PYTHONPATH as sitecustomize. """ import os import sys if os.environ.get("PLE_ST_DEBUG_GETTENSOR") == "1": try: from safetensors import safe_open _orig = safe_open.get_tensor _path = f"/stdbg/stdbg-{os.getpid()}.log" _fh = open(_path, "a", buffering=1 << 16) def _patched(self, name): try: t = _orig(self, name) except Exception as exc: _fh.write(f"RAISE {name} {type(exc).__name__} {exc}\n") _fh.flush() raise _fh.write(f"OK {name} {tuple(t.shape)} {t.dtype}\n") return t safe_open.get_tensor = _patched except Exception as exc: # pragma: no cover sys.stderr.write(f"STDBG patch failed {exc}\n")