gabriel / musehub public
snapshot.py python
35 lines 1.0 KB
cd448303 Initial extraction of MuseHub from maestro monorepo. Gabriel Cardona <gabriel@tellurstori.com> 7d ago
1 """Pure snapshot hashing utilities retained for MuseHub tests.
2
3 The full snapshot module was extracted to cgcardona/muse.
4 Only compute_snapshot_id and compute_commit_id are retained here
5 because MuseHub test fixtures use them to generate deterministic IDs.
6
7 TODO(musehub-extraction): remove when MuseHub is extracted.
8 """
9 from __future__ import annotations
10
11 import hashlib
12
13
14 def compute_snapshot_id(manifest: dict[str, str]) -> str:
15 """Return sha256 of the sorted ``path:object_id`` pairs."""
16 parts = sorted(f"{path}:{oid}" for path, oid in manifest.items())
17 payload = "|".join(parts).encode()
18 return hashlib.sha256(payload).hexdigest()
19
20
21 def compute_commit_id(
22 parent_ids: list[str],
23 snapshot_id: str,
24 message: str,
25 committed_at_iso: str,
26 ) -> str:
27 """Return sha256 of the commit's canonical inputs."""
28 parts = [
29 "|".join(sorted(parent_ids)),
30 snapshot_id,
31 message,
32 committed_at_iso,
33 ]
34 payload = "|".join(parts).encode()
35 return hashlib.sha256(payload).hexdigest()