import json, time, hashlib from typing import Any def _hash(s: str) -> str: return hashlib.sha256(s.encode("utf-8", errors="ignore")).hexdigest() def log_event(event_type: str, user_id: str | None, meta: dict[str, Any]): # Do NOT store content. Use hashes and sizes only. rec = { "ts": time.strftime("%Y-%m-%dT%H:%M:%S%z"), "event": event_type, "user": user_id or "unknown", "meta": meta, } try: with open("audit.jsonl", "a", encoding="utf-8") as f: f.write(json.dumps(rec) + "\n") except Exception: pass def hash_summary(label: str, text: str) -> dict: return { f"{label}_sha256": _hash(text), f"{label}_chars": len(text or ""), }