gabriel / musehub public
test_wire_push_root_snapshot_integrity.py python
216 lines 8.0 KB
Raw
sha256:383b9b15b1a7c18348bf797dbec252b8842199567b4b04371578fedd5f7891d4 merge: integrate push-ingest commit verification into dev Opus 5.5 minor ⚠ breaking 11 days ago
1 """TDD — musehub#93: root-of-push snapshots were never hash-verified before storage.
2
3 Root cause, in ``musehub_wire_push.py``'s snapshot-processing loop::
4
5 if _parent_sid and hash_snapshot(_base, _snap_dirs or None) != _sid:
6 raise ValueError(...)
7
8 This integrity check only ran when ``_parent_sid`` was truthy. Every snapshot
9 that is the *root* of a push batch (``parent_snapshot_id=None`` — the first
10 commit sent, or a genuinely new repo's first commit) has ``_parent_sid`` falsy
11 and was therefore never checked against its own declared ID at all. A wire/
12 transport bug that dropped or mangled ``directories`` (or, in principle, the
13 manifest itself) for exactly this kind of entry would sail through unverified
14 and be persisted permanently -- undetectable until a client later re-hashed it
15 on clone and rejected the mismatch (the exact `entries=1062 dirs=0` corruption
16 found live on `gabriel/muse`@staging that prompted this issue).
17
18 RED before the fix: a root snapshot with directories that don't reproduce its
19 own declared snapshot_id is silently stored with dirs=0.
20 GREEN after: the push is rejected outright, matching the existing (and
21 unaffected) behavior for a snapshot with a bad/phantom parent.
22 """
23 from __future__ import annotations
24
25 import datetime
26 from unittest.mock import AsyncMock, MagicMock, patch
27
28 import msgpack
29 import pytest
30 from sqlalchemy.ext.asyncio import AsyncSession
31
32 from muse.core.ids import hash_snapshot
33 from muse.core.mpack import build_wire_mpack
34 from muse.core.types import blob_id
35 from musehub.core.genesis import compute_identity_id
36 from tests.commit_fixtures import with_reproducible_id
37 from musehub.db.musehub_repo_models import MusehubSnapshot
38 from musehub.services.musehub_repository import create_repo
39 from musehub.services.musehub_wire_push import wire_push_unpack_mpack
40
41 _OWNER = "gabriel"
42 _IDENTITY_ID = compute_identity_id(b"gabriel")
43
44
45 def _cid(seed: str) -> str:
46 return blob_id(f"root-integrity-commit-{seed}".encode())
47
48
49 def _now() -> datetime.datetime:
50 return datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc)
51
52
53 def _raw_commit(label: str, snap_id: str) -> dict:
54 return with_reproducible_id({
55 "commit_id": "",
56 "branch": "feat",
57 "message": f"commit {label[:12]}",
58 "author": _OWNER,
59 "committed_at": _now().isoformat(),
60 "parent_commit_id": None,
61 "parent2_commit_id": None,
62 "snapshot_id": snap_id,
63 "agent_id": "",
64 "model_id": "",
65 "toolchain_id": "",
66 "sem_ver_bump": "none",
67 "breaking_changes": [],
68 "signature": "",
69 "signer_key_id": "",
70 "signer_public_key": "",
71 "prompt_hash": "",
72 })
73
74
75 def _mock_backend(mpack_bytes: bytes) -> MagicMock:
76 backend = MagicMock()
77 backend.get_mpack = AsyncMock(return_value=mpack_bytes)
78 backend.put = AsyncMock(return_value=None)
79 backend.put_mpack = AsyncMock(return_value=None)
80 backend.quarantine_mpack = AsyncMock(return_value=None)
81 backend.presign_get = AsyncMock(return_value="")
82 backend.presign_mpack_get = AsyncMock(return_value="")
83 return backend
84
85
86 @pytest.mark.asyncio
87 async def test_root_snapshot_with_wrong_directories_is_rejected_not_silently_stored(
88 db_session: AsyncSession,
89 ) -> None:
90 """RED: a root (parent_snapshot_id=None) snapshot's declared directories
91 don't reproduce its own snapshot_id. Must be rejected outright -- exactly
92 like the existing phantom-parent case -- not persisted with the wrong
93 (or dropped) directories."""
94 repo = await create_repo(
95 db_session,
96 name="root-integrity-repro",
97 owner=_OWNER,
98 owner_user_id=_IDENTITY_ID,
99 visibility="public",
100 initialize=False,
101 )
102 await db_session.commit()
103
104 oid1 = blob_id(b"content-f1")
105 manifest = {"tracks/f1.txt": oid1}
106 real_directories = ["tracks", "docs"]
107 # The snapshot_id was genuinely computed WITH these directories locally --
108 # this is what a correct client push looks like.
109 snap_id = hash_snapshot(manifest, real_directories)
110
111 a_record = _raw_commit(_cid("a"), snap_id)
112 a_commit = a_record["commit_id"]
113 mpack_bytes = build_wire_mpack({
114 "commits": [a_record],
115 "snapshots": [{
116 "snapshot_id": snap_id,
117 "parent_snapshot_id": None,
118 "delta_upsert": manifest,
119 "delta_remove": [],
120 # Wire/transport bug simulation: directories arrived wrong (here:
121 # dropped entirely) relative to what snap_id was actually hashed
122 # with. This is exactly the shape of musehub#93's live corruption.
123 "directories": [],
124 }],
125 "blobs": [{"object_id": oid1, "content": b"content-f1"}],
126 "tags": [],
127 })
128 mpack_key = blob_id(mpack_bytes)
129 backend = _mock_backend(mpack_bytes)
130
131 with patch("musehub.services.musehub_wire.get_backend", return_value=backend), \
132 patch("musehub.services.musehub_wire_push.get_backend", return_value=backend), \
133 patch("musehub.storage.backends.get_backend", return_value=backend):
134 with pytest.raises(ValueError, match="does not reproduce its own declared ID"):
135 await wire_push_unpack_mpack(
136 db_session,
137 repo.repo_id,
138 mpack_key,
139 pusher_id=_OWNER,
140 branch="feat",
141 head_commit_id=a_commit,
142 commits_count=1,
143 blobs_count=1,
144 force=True,
145 )
146
147 stored = await db_session.get(MusehubSnapshot, snap_id)
148 assert stored is None, (
149 "a root snapshot whose directories don't reproduce its own declared ID "
150 "must never be persisted -- this is exactly the musehub#93 corruption "
151 "(dirs silently dropped, entry_count correct) sailing through unverified"
152 )
153
154
155 @pytest.mark.asyncio
156 async def test_root_snapshot_with_correct_directories_is_stored_intact(
157 db_session: AsyncSession,
158 ) -> None:
159 """GREEN control: a correctly-hashed root snapshot with real directories
160 must still push successfully and be stored with those directories intact --
161 the new unconditional check must not reject legitimate pushes."""
162 repo = await create_repo(
163 db_session,
164 name="root-integrity-happy-path",
165 owner=_OWNER,
166 owner_user_id=_IDENTITY_ID,
167 visibility="public",
168 initialize=False,
169 )
170 await db_session.commit()
171
172 oid1 = blob_id(b"content-f1-happy")
173 manifest = {"tracks/f1.txt": oid1}
174 real_directories = ["tracks", "docs", "empty_dir"]
175 snap_id = hash_snapshot(manifest, real_directories)
176
177 a_record = _raw_commit(_cid("happy-a"), snap_id)
178 a_commit = a_record["commit_id"]
179 mpack_bytes = build_wire_mpack({
180 "commits": [a_record],
181 "snapshots": [{
182 "snapshot_id": snap_id,
183 "parent_snapshot_id": None,
184 "delta_upsert": manifest,
185 "delta_remove": [],
186 "directories": real_directories,
187 }],
188 "blobs": [{"object_id": oid1, "content": b"content-f1-happy"}],
189 "tags": [],
190 })
191 mpack_key = blob_id(mpack_bytes)
192 backend = _mock_backend(mpack_bytes)
193
194 with patch("musehub.services.musehub_wire.get_backend", return_value=backend), \
195 patch("musehub.services.musehub_wire_push.get_backend", return_value=backend), \
196 patch("musehub.storage.backends.get_backend", return_value=backend):
197 await wire_push_unpack_mpack(
198 db_session,
199 repo.repo_id,
200 mpack_key,
201 pusher_id=_OWNER,
202 branch="feat",
203 head_commit_id=a_commit,
204 commits_count=1,
205 blobs_count=1,
206 force=True,
207 )
208 await db_session.flush()
209
210 stored = await db_session.get(MusehubSnapshot, snap_id)
211 assert stored is not None, "a correctly-hashed root snapshot must be stored"
212 assert sorted(stored.directories or []) == sorted(real_directories), (
213 f"stored directories don't match what was pushed: "
214 f"stored={stored.directories!r} pushed={real_directories!r}"
215 )
216 assert hash_snapshot(manifest, list(stored.directories or [])) == snap_id
File History 3 commits
sha256:383b9b15b1a7c18348bf797dbec252b8842199567b4b04371578fedd5f7891d4 merge: integrate push-ingest commit verification into dev Opus 5.5 minor ⚠ 11 days ago
sha256:a4d6b38221ff81ce17b43d47240f441c45140d63c575f98273c66c0df132c388 fix(push): verify commit records at ingest and reconcile du… Opus 5.5 minor ⚠ 11 days ago
sha256:c3910cc561368d2b40576c1fbb0841b5d3abefd0a65c96c85114a7238222c77c fix: root-of-push snapshots were never hash-verified before… Sonnet 5 patch 59 days ago