"""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))