File size: 743 Bytes
386287a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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 ""),
    }