Spaces:
Running
Running
File size: 652 Bytes
fd5dd94 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | """Canonical hashing used for previews, approvals and idempotency."""
from __future__ import annotations
import hashlib
import json
from typing import Any
def canonical_json(value: Any) -> str:
return json.dumps(value, sort_keys=True, separators=(",", ":"), default=str)
def sha256_hex(text: str) -> str:
return hashlib.sha256(text.encode("utf-8")).hexdigest()
def preview_hash(effect: Any) -> str:
"""Hash of a would-be effect. Handlers return this from dry runs; the gateway checks that
the effect at execution time still matches the one a human approved."""
return sha256_hex(canonical_json(effect))
|