gabriel / musehub public
test_wire_push_object_completeness.py python
827 lines 39.8 KB
Raw
sha256:c62240d36e803460a80726fd25ffe9b5c51d9e0193c1f77753e157dc8d5d1d39 fix(wire): bind every pushed commit's snapshot and the bran… Opus 5.5 minor ⚠ breaking 3 days ago
1 """TDD (Tier A) — push admission must not publish an incomplete object graph.
2
3 Demonstrated defect (gabriel/musehub, 2026-11-13): ``wire_push_unpack_mpack``
4 resolves every incoming snapshot's manifest and hash-verifies the *snapshot*,
5 and hash-verifies every blob the pack *does* carry, but never checks that the
6 blobs the snapshots reference are obtainable. A client that omits blobs (the
7 Muse client does so for "promised" objects, ``build_mpack``) therefore gets
8 commits + snapshots + a moved branch pointer published for a history that can
9 never be cloned, fetched or analysed (two such blobs broke ``detect_refactor``).
10
11 Invariant enforced here, at the receiving server (the authority):
12
13 Every object a newly admitted snapshot *introduces* (its ``delta_upsert``
14 values — snapshots are flat path→object manifests; there are no nested
15 manifests) must be either carried by the incoming pack (hash-verified) or
16 already stored AND linked to *this* repository (``musehub_object_refs`` joined
17 to ``musehub_objects``, the same relation ``fetch/objects`` serves from).
18 Otherwise the whole push is rejected before any row is committed and the
19 branch pointer does not move.
20
21 Entries a snapshot merely *inherits* from its parent are not re-checked (they
22 were admitted under this rule, or pre-date it) so a repository with unrelated
23 historical gaps can still accept new, complete pushes.
24
25 Tests marked RED fail on the unfixed server; tests marked GUARD pass before and
26 after and pin behaviour the fix must not break.
27 """
28 from __future__ import annotations
29
30 import asyncio
31 import datetime
32 from unittest.mock import AsyncMock, MagicMock, patch
33
34 import msgpack
35 import pytest
36 from sqlalchemy import select, text
37 from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
38
39 from muse.core.ids import hash_snapshot
40 from muse.core.mpack import build_wire_mpack
41 from muse.core.types import blob_id
42 from musehub.core.genesis import compute_identity_id
43 from musehub.db.musehub_repo_models import (
44 MusehubBranch,
45 MusehubCommit,
46 MusehubObject,
47 MusehubCommitRef,
48 MusehubObjectRef,
49 MusehubSnapshot,
50 MusehubSnapshotRef,
51 )
52 from musehub.services.musehub_repository import create_repo
53 from musehub.services.musehub_wire_push import wire_push_unpack_mpack
54 from tests.commit_fixtures import with_reproducible_id
55
56 pytestmark = pytest.mark.wire
57
58 _OWNER = "gabriel"
59 _IDENTITY_ID = compute_identity_id(b"gabriel")
60 _REJECTION = "incomplete object graph"
61
62
63 def _now() -> str:
64 return datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc).isoformat()
65
66
67 def _raw_commit(label: str, snap_id: str, parent: str | None = None, branch: str = "feat") -> dict:
68 return with_reproducible_id({
69 "commit_id": "", "branch": branch, "message": f"commit {label}", "author": _OWNER,
70 "committed_at": _now(), "parent_commit_id": parent, "parent2_commit_id": None,
71 "snapshot_id": snap_id, "agent_id": "", "model_id": "", "toolchain_id": "",
72 "sem_ver_bump": "none", "breaking_changes": [], "signature": "", "signer_key_id": "",
73 "signer_public_key": "", "prompt_hash": "",
74 })
75
76
77 def _snap(manifest: dict[str, str], parent_sid: str | None = None,
78 parent_manifest: dict[str, str] | None = None) -> tuple[str, dict]:
79 sid = hash_snapshot(manifest, None)
80 base = parent_manifest or {}
81 upsert = {p: o for p, o in manifest.items() if base.get(p) != o}
82 remove = [p for p in base if p not in manifest]
83 return sid, {"snapshot_id": sid, "parent_snapshot_id": parent_sid,
84 "delta_upsert": upsert, "delta_remove": remove, "directories": []}
85
86
87 def _mock_backend(mpack_bytes: bytes) -> MagicMock:
88 b = MagicMock()
89 b.get_mpack = AsyncMock(return_value=mpack_bytes)
90 b.put = AsyncMock(return_value=None)
91 b.put_mpack = AsyncMock(return_value=None)
92 b.quarantine_mpack = AsyncMock(return_value=None)
93 b.presign_get = AsyncMock(return_value="")
94 b.presign_mpack_get = AsyncMock(return_value="")
95 return b
96
97
98 async def _push(session: AsyncSession, repo_id: str, mpack: dict, head: str, *, blobs_count: int | None = None):
99 data = build_wire_mpack(mpack)
100 backend = _mock_backend(data)
101 with patch("musehub.services.musehub_wire.get_backend", return_value=backend), \
102 patch("musehub.services.musehub_wire_push.get_backend", return_value=backend), \
103 patch("musehub.storage.backends.get_backend", return_value=backend):
104 return await wire_push_unpack_mpack(
105 session, repo_id, blob_id(data), pusher_id=_OWNER, branch="feat", head_commit_id=head,
106 commits_count=len(mpack["commits"]),
107 blobs_count=len(mpack["blobs"]) if blobs_count is None else blobs_count, force=True,
108 )
109
110
111 async def _repo(session: AsyncSession, name: str):
112 repo = await create_repo(session, name=name, owner=_OWNER, owner_user_id=_IDENTITY_ID,
113 visibility="public", initialize=False)
114 await session.commit()
115 return repo
116
117
118 async def _published_state(factory: async_sessionmaker, repo_id: str, snap_ids: list[str], commit_ids: list[str]):
119 """What a *fresh* connection can see — proves nothing leaked past a rejection."""
120 async with factory() as s:
121 branch = (await s.execute(select(MusehubBranch).where(
122 MusehubBranch.repo_id == repo_id, MusehubBranch.name == "feat"))).scalar_one_or_none()
123 snaps = (await s.execute(select(MusehubSnapshot.snapshot_id).where(
124 MusehubSnapshot.snapshot_id.in_(snap_ids)))).scalars().all()
125 commits = (await s.execute(select(MusehubCommit.commit_id).where(
126 MusehubCommit.commit_id.in_(commit_ids)))).scalars().all()
127 refs = (await s.execute(select(MusehubObjectRef.object_id).where(
128 MusehubObjectRef.repo_id == repo_id))).scalars().all()
129 return branch, list(snaps), list(commits), list(refs)
130
131
132 async def _seed_object(session: AsyncSession, repo_id: str, data: bytes, uri: str, *, link: bool = True) -> str:
133 oid = blob_id(data)
134 session.add(MusehubObject(object_id=oid, path="", size_bytes=len(data), storage_uri=uri, content_cache=None))
135 await session.flush()
136 if link:
137 session.add(MusehubObjectRef(repo_id=repo_id, object_id=oid))
138 await session.commit()
139 return oid
140
141
142 # ── RED: the demonstrated defect ─────────────────────────────────────────────
143
144 @pytest.mark.asyncio
145 async def test_push_omitting_a_required_blob_is_rejected_and_publishes_nothing(
146 db_session: AsyncSession, session_factory: async_sessionmaker,
147 ) -> None:
148 """RED (case 1 + 11): commit + snapshot arrive, the blob does not, and the
149 request header claims a blob count (a lying/outdated client). Pre-fix this
150 publishes the commit, the snapshot and moves the branch."""
151 repo = await _repo(db_session, "completeness-omit")
152 oid = blob_id(b"never-sent")
153 sid, snap = _snap({"a.txt": oid})
154 c = _raw_commit("omit", sid)
155 mp = {"commits": [c], "snapshots": [snap], "blobs": [], "tags": []}
156
157 with pytest.raises(ValueError, match=_REJECTION) as ei:
158 await _push(db_session, repo.repo_id, mp, c["commit_id"], blobs_count=7)
159 assert oid in str(ei.value)
160
161 branch, snaps, commits, refs = await _published_state(session_factory, repo.repo_id, [sid], [c["commit_id"]])
162 assert branch is None and snaps == [] and commits == [] and refs == []
163
164
165 @pytest.mark.asyncio
166 async def test_manifest_with_exactly_one_missing_blob_names_only_that_blob(
167 db_session: AsyncSession, session_factory: async_sessionmaker,
168 ) -> None:
169 """RED (case 2): four referenced blobs, three carried."""
170 repo = await _repo(db_session, "completeness-one-of-four")
171 datas = [f"blob-{i}".encode() for i in range(4)]
172 oids = [blob_id(d) for d in datas]
173 sid, snap = _snap({f"f{i}.txt": o for i, o in enumerate(oids)})
174 c = _raw_commit("one-of-four", sid)
175 mp = {"commits": [c], "snapshots": [snap], "tags": [],
176 "blobs": [{"object_id": o, "content": d} for o, d in zip(oids[:3], datas[:3])]}
177
178 with pytest.raises(ValueError, match=_REJECTION) as ei:
179 await _push(db_session, repo.repo_id, mp, c["commit_id"])
180 msg = str(ei.value)
181 assert oids[3] in msg and not any(o in msg for o in oids[:3])
182
183 branch, snaps, commits, refs = await _published_state(session_factory, repo.repo_id, [sid], [c["commit_id"]])
184 # all-or-nothing: the three good blobs must not have been linked to the repo either
185 assert branch is None and snaps == [] and commits == [] and refs == []
186
187
188 @pytest.mark.asyncio
189 async def test_missing_blob_in_a_descendant_snapshot_rejects_the_whole_chain(
190 db_session: AsyncSession, session_factory: async_sessionmaker,
191 ) -> None:
192 """RED (case 6): snapshots are flat manifests, so 'nested' means a snapshot
193 chain inside one push. The parent is complete, the child's delta adds an
194 absent blob — neither commit may be published."""
195 repo = await _repo(db_session, "completeness-chain")
196 d1, d2 = b"parent-file", b"child-file-missing"
197 o1, o2 = blob_id(d1), blob_id(d2)
198 m1 = {"a.txt": o1}
199 m2 = {"a.txt": o1, "b.txt": o2}
200 s1, snap1 = _snap(m1)
201 s2, snap2 = _snap(m2, s1, m1)
202 c1 = _raw_commit("p", s1)
203 c2 = _raw_commit("c", s2, parent=c1["commit_id"])
204 mp = {"commits": [c1, c2], "snapshots": [snap1, snap2], "tags": [],
205 "blobs": [{"object_id": o1, "content": d1}]}
206
207 with pytest.raises(ValueError, match=_REJECTION) as ei:
208 await _push(db_session, repo.repo_id, mp, c2["commit_id"])
209 assert o2 in str(ei.value)
210
211 branch, snaps, commits, refs = await _published_state(
212 session_factory, repo.repo_id, [s1, s2], [c1["commit_id"], c2["commit_id"]])
213 assert branch is None and snaps == [] and commits == [] and refs == []
214
215
216 @pytest.mark.asyncio
217 async def test_decoy_blob_under_another_id_does_not_satisfy_the_reference(
218 db_session: AsyncSession,
219 ) -> None:
220 """RED (case 8/11): the pack carries a perfectly valid blob Y; the snapshot
221 references X. Shipping *some* verified blob must not count as shipping X."""
222 repo = await _repo(db_session, "completeness-decoy")
223 x, ydata = blob_id(b"wanted"), b"decoy"
224 sid, snap = _snap({"a.txt": x})
225 c = _raw_commit("decoy", sid)
226 mp = {"commits": [c], "snapshots": [snap], "tags": [],
227 "blobs": [{"object_id": blob_id(ydata), "content": ydata}]}
228 with pytest.raises(ValueError, match=_REJECTION):
229 await _push(db_session, repo.repo_id, mp, c["commit_id"])
230
231
232 @pytest.mark.asyncio
233 async def test_object_stored_for_another_repo_is_not_available_to_this_one(
234 db_session: AsyncSession,
235 ) -> None:
236 """RED (design decision, flagged for review): an object that exists on the
237 server only because *another* repository pushed it is not obtainable through
238 this repository (``fetch/objects`` serves only objects linked to the repo) and
239 referencing it without sending it would grant a reference without possession."""
240 other = await _repo(db_session, "completeness-other")
241 repo = await _repo(db_session, "completeness-cross")
242 data = b"belongs-to-other-repo"
243 oid = await _seed_object(db_session, other.repo_id, data, "s3://bucket/x")
244 sid, snap = _snap({"a.txt": oid})
245 c = _raw_commit("cross", sid)
246 mp = {"commits": [c], "snapshots": [snap], "blobs": [], "tags": []}
247 with pytest.raises(ValueError, match=_REJECTION):
248 await _push(db_session, repo.repo_id, mp, c["commit_id"])
249
250
251 @pytest.mark.asyncio
252 async def test_rejected_push_can_be_retried_with_the_blob_and_then_succeeds(
253 db_session: AsyncSession, session_factory: async_sessionmaker,
254 ) -> None:
255 """RED (case 9, interrupted/retried): a rejection leaves no residue that would
256 make the corrected retry conflict or half-apply."""
257 repo = await _repo(db_session, "completeness-retry")
258 data = b"retry-file"
259 oid = blob_id(data)
260 sid, snap = _snap({"a.txt": oid})
261 c = _raw_commit("retry", sid)
262 bad = {"commits": [c], "snapshots": [snap], "blobs": [], "tags": []}
263 with pytest.raises(ValueError, match=_REJECTION):
264 await _push(db_session, repo.repo_id, bad, c["commit_id"])
265
266 good = {**bad, "blobs": [{"object_id": oid, "content": data}]}
267 result = await _push(db_session, repo.repo_id, good, c["commit_id"])
268 assert result["head"] == c["commit_id"] and result["blobs_written"] == 1
269 branch, snaps, commits, refs = await _published_state(session_factory, repo.repo_id, [sid], [c["commit_id"]])
270 assert branch is not None and branch.head_commit_id == c["commit_id"]
271 assert snaps == [sid] and commits == [c["commit_id"]] and refs == [oid]
272
273
274 @pytest.mark.asyncio
275 async def test_concurrent_pushes_never_admit_a_snapshot_whose_objects_are_absent(
276 db_session: AsyncSession, session_factory: async_sessionmaker,
277 ) -> None:
278 """GUARD (case 7; passes pre-fix because B happens to reuse A's object): push A carries blob X; push B (different commit) references X
279 without carrying it, racing A. Either order is acceptable, but any snapshot
280 that ends up stored must have all of its objects linked to the repo."""
281 repo = await _repo(db_session, "completeness-race")
282 data = b"shared-object"
283 oid = blob_id(data)
284 sa, snap_a = _snap({"a.txt": oid})
285 sb, snap_b = _snap({"b.txt": oid})
286 ca, cb = _raw_commit("a", sa, branch="a"), _raw_commit("b", sb, branch="b")
287 mp_a = {"commits": [ca], "snapshots": [snap_a], "tags": [], "blobs": [{"object_id": oid, "content": data}]}
288 mp_b = {"commits": [cb], "snapshots": [snap_b], "tags": [], "blobs": []}
289
290 async def go(mp: dict, head: str):
291 async with session_factory() as s:
292 try:
293 return await _push(s, repo.repo_id, mp, head)
294 except ValueError as exc:
295 return exc
296
297 await asyncio.gather(go(mp_a, ca["commit_id"]), go(mp_b, cb["commit_id"]))
298
299 async with session_factory() as s:
300 stored = (await s.execute(select(MusehubSnapshot.snapshot_id).where(
301 MusehubSnapshot.snapshot_id.in_([sa, sb])))).scalars().all()
302 linked = set((await s.execute(select(MusehubObjectRef.object_id).where(
303 MusehubObjectRef.repo_id == repo.repo_id))).scalars())
304 assert sa in stored, "push A carried its blob and must be admitted"
305 for sid in stored:
306 assert oid in linked, f"snapshot {sid[:20]} admitted but its object is not linked to the repo"
307
308
309 # ── GUARD: legitimate pushes must keep working ───────────────────────────────
310
311 @pytest.mark.asyncio
312 async def test_blob_supplied_in_the_incoming_pack_is_admitted(db_session: AsyncSession) -> None:
313 """GUARD (case 5)."""
314 repo = await _repo(db_session, "completeness-supplied")
315 data = b"supplied"
316 oid = blob_id(data)
317 sid, snap = _snap({"a.txt": oid})
318 c = _raw_commit("supplied", sid)
319 mp = {"commits": [c], "snapshots": [snap], "tags": [], "blobs": [{"object_id": oid, "content": data}]}
320 result = await _push(db_session, repo.repo_id, mp, c["commit_id"])
321 assert result["head"] == c["commit_id"] and result["snapshots_written"] == 1
322
323
324 @pytest.mark.asyncio
325 @pytest.mark.parametrize("uri", ["s3://bucket/loose-object", "mpack://sha256:" + "ab" * 32])
326 async def test_blob_already_stored_for_this_repo_is_admitted_without_resending(
327 db_session: AsyncSession, uri: str,
328 ) -> None:
329 """GUARD (cases 3 and 4): a previously stored loose (s3://) or packed (mpack://)
330 object linked to this repo satisfies a later snapshot that re-uses it."""
331 repo = await _repo(db_session, f"completeness-existing-{uri[:4]}")
332 oid = await _seed_object(db_session, repo.repo_id, b"already-here-" + uri.encode(), uri)
333 sid, snap = _snap({"moved/a.txt": oid})
334 c = _raw_commit("existing", sid)
335 mp = {"commits": [c], "snapshots": [snap], "blobs": [], "tags": []}
336 result = await _push(db_session, repo.repo_id, mp, c["commit_id"])
337 assert result["head"] == c["commit_id"]
338
339
340 @pytest.mark.asyncio
341 async def test_mismatched_blob_content_is_still_rejected(db_session: AsyncSession) -> None:
342 """GUARD (case 8): canonical hash verification of carried blobs is unchanged."""
343 repo = await _repo(db_session, "completeness-hash")
344 oid = blob_id(b"claimed")
345 sid, snap = _snap({"a.txt": oid})
346 c = _raw_commit("hash", sid)
347 mp = {"commits": [c], "snapshots": [snap], "tags": [],
348 "blobs": [{"object_id": oid, "content": b"different bytes"}]}
349 with pytest.raises(ValueError, match="hash"):
350 await _push(db_session, repo.repo_id, mp, c["commit_id"])
351
352
353 def _full_snap(manifest: dict[str, str]) -> tuple[str, dict]:
354 """The shape the real client sends for the first snapshot of a push
355 (``_build_snapshot_deltas``): no parent, the FULL manifest as delta_upsert —
356 even when the destination already holds the parent commit."""
357 return _snap(manifest)
358
359
360 async def _push_legacy_then_lose_blob(db_session: AsyncSession, repo_id: str):
361 """Land a complete commit L, then delete one of its blobs' rows to reproduce the
362 live gabriel/musehub condition (a stored snapshot whose object is gone)."""
363 keep, ghost_data = b"kept-file", b"ghost-file"
364 ok, og = blob_id(keep), blob_id(ghost_data)
365 m = {"keep.txt": ok, "old.txt": og}
366 sid, snap = _full_snap(m)
367 c = _raw_commit("legacy", sid)
368 await _push(db_session, repo_id, {"commits": [c], "snapshots": [snap], "tags": [], "blobs": [
369 {"object_id": ok, "content": keep}, {"object_id": og, "content": ghost_data}]}, c["commit_id"])
370 await db_session.execute(text("DELETE FROM musehub_object_refs WHERE object_id = :o"), {"o": og})
371 await db_session.execute(text("DELETE FROM musehub_objects WHERE object_id = :o"), {"o": og})
372 await db_session.commit()
373 return c, m, og
374
375
376 @pytest.mark.asyncio
377 async def test_unrelated_historical_gap_does_not_block_a_complete_new_push(
378 db_session: AsyncSession,
379 ) -> None:
380 """RED against the naive fix / GUARD against over-strictness (case 10), in the
381 REAL client shape: the repo holds commit L whose blob is gone. A child commit
382 ships its first snapshot as a full manifest (parent None) that merely inherits
383 the gap entry and adds one fully supplied blob. Only entries the push
384 *introduces* relative to the repository's own parent snapshot are checked."""
385 repo = await _repo(db_session, "completeness-legacy-gap")
386 parent, pm, ghost = await _push_legacy_then_lose_blob(db_session, repo.repo_id)
387
388 new = b"brand-new-file"
389 onew = blob_id(new)
390 sid, snap = _full_snap({**pm, "new.txt": onew})
391 c = _raw_commit("after-gap", sid, parent=parent["commit_id"])
392 mp = {"commits": [c], "snapshots": [snap], "tags": [], "blobs": [{"object_id": onew, "content": new}]}
393 result = await _push(db_session, repo.repo_id, mp, c["commit_id"])
394 assert result["head"] == c["commit_id"] and result["snapshots_written"] == 1
395
396
397 @pytest.mark.asyncio
398 async def test_gap_inheritance_does_not_excuse_a_different_missing_blob(
399 db_session: AsyncSession,
400 ) -> None:
401 """RED (case 10, negative): same repository gap, but the child *changes* the gap
402 entry to a different absent object. That is introduced, not inherited."""
403 repo = await _repo(db_session, "completeness-legacy-gap-neg")
404 parent, pm, ghost = await _push_legacy_then_lose_blob(db_session, repo.repo_id)
405
406 other_ghost = blob_id(b"a-different-absent-blob")
407 sid, snap = _full_snap({**pm, "old.txt": other_ghost})
408 c = _raw_commit("changes-gap", sid, parent=parent["commit_id"])
409 mp = {"commits": [c], "snapshots": [snap], "tags": [], "blobs": []}
410 with pytest.raises(ValueError, match=_REJECTION) as ei:
411 await _push(db_session, repo.repo_id, mp, c["commit_id"])
412 assert other_ghost in str(ei.value) and ghost not in str(ei.value)
413
414
415 @pytest.mark.asyncio
416 async def test_claiming_an_unrelated_commit_as_parent_does_not_launder_a_missing_blob(
417 db_session: AsyncSession,
418 ) -> None:
419 """RED (case 11): a hostile client names the legacy commit as parent but its
420 manifest introduces an absent blob on a *new* path. Inheritance only excuses an
421 entry the parent snapshot really holds at that path."""
422 repo = await _repo(db_session, "completeness-launder")
423 parent, pm, ghost = await _push_legacy_then_lose_blob(db_session, repo.repo_id)
424 sid, snap = _full_snap({**pm, "moved/old.txt": ghost}) # same oid, new path: not inherited
425 c = _raw_commit("launder", sid, parent=parent["commit_id"])
426 mp = {"commits": [c], "snapshots": [snap], "tags": [], "blobs": []}
427 with pytest.raises(ValueError, match=_REJECTION):
428 await _push(db_session, repo.repo_id, mp, c["commit_id"])
429
430
431 # ── Cross-repository snapshot membership (Tier A review B1/B2) ────────────────
432 # Promoted from the independent reviewer's probes (review/test_review_probe_tierA.py).
433 # Object availability is repository-scoped, so snapshot "already admitted" and
434 # "inheritance source" must be repository-scoped too: a snapshot or parent that
435 # exists on the server only because ANOTHER repository pushed it exempts nothing.
436
437
438 async def _repo_state(factory: async_sessionmaker, repo_id: str) -> tuple:
439 """Everything a fresh connection can see for one repository."""
440 async with factory() as s:
441 heads = (await s.execute(select(MusehubBranch.name, MusehubBranch.head_commit_id).where(
442 MusehubBranch.repo_id == repo_id))).all()
443 snaps = (await s.execute(select(MusehubSnapshotRef.snapshot_id).where(
444 MusehubSnapshotRef.repo_id == repo_id))).scalars().all()
445 commits = (await s.execute(select(MusehubCommitRef.commit_id).where(
446 MusehubCommitRef.repo_id == repo_id))).scalars().all()
447 objs = (await s.execute(select(MusehubObjectRef.object_id).where(
448 MusehubObjectRef.repo_id == repo_id))).scalars().all()
449 return sorted(heads), sorted(snaps), sorted(commits), sorted(objs)
450
451
452 async def _push_to_a(session: AsyncSession, repo_id: str, manifest: dict[str, str],
453 blobs: dict[str, bytes]) -> tuple[str, dict, dict]:
454 sid, snap = _snap(manifest)
455 ca = _raw_commit("a", sid)
456 await _push(session, repo_id, {"commits": [ca], "snapshots": [snap], "tags": [],
457 "blobs": [{"object_id": o, "content": d} for o, d in blobs.items()]},
458 ca["commit_id"])
459 await session.commit()
460 return sid, snap, ca
461
462
463 @pytest.mark.asyncio
464 async def test_b1_snapshot_stored_only_for_another_repo_does_not_exempt_its_objects(
465 db_session: AsyncSession, session_factory: async_sessionmaker,
466 ) -> None:
467 """RED (review B1): repo A legitimately pushes snapshot S with its blob. Repo B
468 then receives a commit pointing at S with ZERO blobs. Pre-fix the global
469 ``musehub_snapshots`` dedup counted S as already admitted, so nothing was checked
470 and B's branch moved to a history B cannot serve."""
471 a = await _repo(db_session, "b1-a")
472 b = await _repo(db_session, "b1-b")
473 data = b"owned-by-repo-a-only"
474 oid = blob_id(data)
475 sid, snap, ca = await _push_to_a(db_session, a.repo_id, {"secret.txt": oid}, {oid: data})
476 a_before = await _repo_state(session_factory, a.repo_id)
477
478 cb = _raw_commit("b", sid)
479 with pytest.raises(ValueError, match=_REJECTION) as ei:
480 await _push(db_session, b.repo_id, {"commits": [cb], "snapshots": [snap], "tags": [], "blobs": []},
481 cb["commit_id"])
482 assert oid in str(ei.value) # actionable: names the object B must send
483
484 assert await _repo_state(session_factory, b.repo_id) == ([], [], [], [])
485 _, _, commits, _ = await _published_state(session_factory, b.repo_id, [], [cb["commit_id"]])
486 assert commits == [] # no commit row leaked globally either
487 assert await _repo_state(session_factory, a.repo_id) == a_before
488
489
490 @pytest.mark.asyncio
491 async def test_b2_foreign_parent_snapshot_does_not_exempt_inherited_objects(
492 db_session: AsyncSession, session_factory: async_sessionmaker,
493 ) -> None:
494 """RED (review B2): repo B receives a NEW snapshot whose ``parent_snapshot_id``
495 names repo A's snapshot and whose delta adds one blob it does carry. Pre-fix
496 only the delta was checked, so B inherited A's object without possessing it."""
497 a = await _repo(db_session, "b2-a")
498 b = await _repo(db_session, "b2-b")
499 data = b"owned-by-repo-a-only-2"
500 oid = blob_id(data)
501 m1 = {"secret.txt": oid}
502 s1, _, _ = await _push_to_a(db_session, a.repo_id, m1, {oid: data})
503 a_before = await _repo_state(session_factory, a.repo_id)
504
505 new = b"b-own-file"
506 m2 = {"secret.txt": oid, "mine.txt": blob_id(new)}
507 s2, snap2 = _snap(m2, s1, m1)
508 assert snap2["delta_upsert"] == {"mine.txt": blob_id(new)} # the shape that bypassed the check
509 cb = _raw_commit("b", s2)
510 with pytest.raises(ValueError, match=_REJECTION) as ei:
511 await _push(db_session, b.repo_id, {"commits": [cb], "snapshots": [snap2], "tags": [],
512 "blobs": [{"object_id": blob_id(new), "content": new}]},
513 cb["commit_id"])
514 assert oid in str(ei.value) and blob_id(new) not in str(ei.value)
515
516 assert await _repo_state(session_factory, b.repo_id) == ([], [], [], [])
517 _, snaps, commits, _ = await _published_state(session_factory, b.repo_id, [s2], [cb["commit_id"]])
518 assert snaps == [] and commits == []
519 assert await _repo_state(session_factory, a.repo_id) == a_before
520
521
522 @pytest.mark.asyncio
523 async def test_identical_history_pushed_complete_to_a_second_repo_is_admitted(
524 db_session: AsyncSession, session_factory: async_sessionmaker,
525 ) -> None:
526 """GUARD (B1 counterpart): the same content in two repositories is legitimate
527 (forks, mirrors). With the blob supplied, B's push of A's snapshot succeeds."""
528 a = await _repo(db_session, "b1-guard-a")
529 b = await _repo(db_session, "b1-guard-b")
530 data = b"shared-content"
531 oid = blob_id(data)
532 sid, snap, _ = await _push_to_a(db_session, a.repo_id, {"f.txt": oid}, {oid: data})
533
534 cb = _raw_commit("b", sid)
535 result = await _push(db_session, b.repo_id, {"commits": [cb], "snapshots": [snap], "tags": [],
536 "blobs": [{"object_id": oid, "content": data}]},
537 cb["commit_id"])
538 await db_session.commit()
539 assert result["head"] == cb["commit_id"]
540 heads, _, commits, objs = await _repo_state(session_factory, b.repo_id)
541 assert heads == [("feat", cb["commit_id"])] and commits == [cb["commit_id"]] and objs == [oid]
542
543
544 @pytest.mark.asyncio
545 async def test_foreign_parent_snapshot_with_every_object_supplied_is_admitted(
546 db_session: AsyncSession, session_factory: async_sessionmaker,
547 ) -> None:
548 """GUARD (B2 counterpart): a foreign parent is not itself forbidden; it just
549 exempts nothing. When the push carries every object of the child's full
550 manifest, the push is admitted."""
551 a = await _repo(db_session, "b2-guard-a")
552 b = await _repo(db_session, "b2-guard-b")
553 data = b"shared-content-2"
554 oid = blob_id(data)
555 m1 = {"secret.txt": oid}
556 s1, _, _ = await _push_to_a(db_session, a.repo_id, m1, {oid: data})
557
558 new = b"b-own-file-2"
559 s2, snap2 = _snap({**m1, "mine.txt": blob_id(new)}, s1, m1)
560 cb = _raw_commit("b", s2)
561 result = await _push(db_session, b.repo_id, {"commits": [cb], "snapshots": [snap2], "tags": [],
562 "blobs": [{"object_id": oid, "content": data},
563 {"object_id": blob_id(new), "content": new}]},
564 cb["commit_id"])
565 await db_session.commit()
566 assert result["head"] == cb["commit_id"]
567 heads, snaps, _, objs = await _repo_state(session_factory, b.repo_id)
568 assert heads == [("feat", cb["commit_id"])] and snaps == [s2] and objs == sorted([oid, blob_id(new)])
569
570
571 @pytest.mark.asyncio
572 async def test_parent_snapshot_linked_to_this_repo_still_exempts_inherited_entries(
573 db_session: AsyncSession,
574 ) -> None:
575 """GUARD: inheritance from this repository's OWN snapshot is unchanged; a child
576 sending only its delta (and only the delta's blob) is admitted."""
577 repo = await _repo(db_session, "own-parent-guard")
578 data = b"base-file"
579 oid = blob_id(data)
580 m1 = {"base.txt": oid}
581 s1, _, c1 = await _push_to_a(db_session, repo.repo_id, m1, {oid: data})
582
583 new = b"child-file"
584 s2, snap2 = _snap({**m1, "child.txt": blob_id(new)}, s1, m1)
585 c2 = _raw_commit("child", s2, parent=c1["commit_id"])
586 result = await _push(db_session, repo.repo_id, {"commits": [c2], "snapshots": [snap2], "tags": [],
587 "blobs": [{"object_id": blob_id(new), "content": new}]},
588 c2["commit_id"])
589 assert result["head"] == c2["commit_id"] and result["snapshots_written"] == 1
590
591
592 @pytest.mark.asyncio
593 async def test_snapshot_already_linked_to_this_repo_is_admitted_without_resending(
594 db_session: AsyncSession,
595 ) -> None:
596 """GUARD: a new commit reusing a snapshot this repository already holds (e.g. a
597 revert to an earlier tree) needs no blobs."""
598 repo = await _repo(db_session, "own-snapshot-guard")
599 data = b"kept-file"
600 oid = blob_id(data)
601 sid, snap, c1 = await _push_to_a(db_session, repo.repo_id, {"k.txt": oid}, {oid: data})
602
603 c2 = _raw_commit("same-tree", sid, parent=c1["commit_id"])
604 result = await _push(db_session, repo.repo_id, {"commits": [c2], "snapshots": [snap], "tags": [], "blobs": []},
605 c2["commit_id"])
606 assert result["head"] == c2["commit_id"]
607
608
609 # ── B1′: every incoming commit and the branch tip must belong to this push or repo ──
610 #
611 # The snapshot admission check above only sees snapshots carried in the pack. A
612 # commit names its snapshot by id, and the branch tip names a commit by id, so both
613 # must also be bound: a commit's snapshot is either in this push (and so checked
614 # above) or already part of this repository; the tip is either a commit of this
615 # push or a commit already linked to this repository. "Part of this repository" is
616 # the same membership the fetch path serves from: a ``musehub_snapshot_refs`` row,
617 # or the snapshot of a commit linked to the repository (historical pushes linked
618 # only globally new snapshots, so most repository snapshots have no snapshot ref).
619
620 _FOREIGN = "not part of this push or this repository"
621
622
623 async def _drop_snapshot_ref(session: AsyncSession, repo_id: str, sid: str) -> None:
624 """Reproduce the historical shape: commit linked to the repo, snapshot ref absent."""
625 await session.execute(text(
626 "DELETE FROM musehub_snapshot_refs WHERE repo_id = :r AND snapshot_id = :s"), {"r": repo_id, "s": sid})
627 await session.commit()
628
629
630 @pytest.mark.asyncio
631 async def test_b1p_commit_naming_foreign_snapshot_absent_from_pack_is_rejected(
632 db_session: AsyncSession, session_factory: async_sessionmaker,
633 ) -> None:
634 """RED (re-review B1′, the reviewer's probe): repo A legitimately pushes snapshot S
635 with its blob. Repo B receives a pack with ONE commit naming S and no snapshot
636 record and no blob. Pre-fix nothing examined the commit's snapshot: B's branch
637 moved to a tree B never possessed."""
638 a = await _repo(db_session, "b1p-a")
639 b = await _repo(db_session, "b1p-b")
640 data = b"owned-by-repo-a-only-b1p"
641 oid = blob_id(data)
642 sid, _snap, _ca = await _push_to_a(db_session, a.repo_id, {"secret.txt": oid}, {oid: data})
643 a_before = await _repo_state(session_factory, a.repo_id)
644
645 cb = _raw_commit("b", sid)
646 with pytest.raises(ValueError, match=_FOREIGN) as ei:
647 await _push(db_session, b.repo_id, {"commits": [cb], "snapshots": [], "tags": [], "blobs": []},
648 cb["commit_id"])
649 assert sid in str(ei.value)
650
651 assert await _repo_state(session_factory, b.repo_id) == ([], [], [], [])
652 _, _, commits, _ = await _published_state(session_factory, b.repo_id, [], [cb["commit_id"]])
653 assert commits == [] # no commit row leaked globally
654 assert await _repo_state(session_factory, a.repo_id) == a_before
655
656
657 @pytest.mark.asyncio
658 async def test_b1p_commit_naming_a_snapshot_that_exists_nowhere_is_rejected(
659 db_session: AsyncSession, session_factory: async_sessionmaker,
660 ) -> None:
661 """RED: the same hole with a snapshot id that is stored nowhere at all."""
662 b = await _repo(db_session, "b1p-nowhere")
663 ghost = hash_snapshot({"ghost.txt": blob_id(b"never-pushed")}, None)
664 cb = _raw_commit("ghost", ghost)
665 with pytest.raises(ValueError, match=_FOREIGN):
666 await _push(db_session, b.repo_id, {"commits": [cb], "snapshots": [], "tags": [], "blobs": []},
667 cb["commit_id"])
668 assert await _repo_state(session_factory, b.repo_id) == ([], [], [], [])
669
670
671 @pytest.mark.asyncio
672 async def test_b1p_one_foreign_commit_rejects_the_whole_push_and_publishes_nothing(
673 db_session: AsyncSession, session_factory: async_sessionmaker,
674 ) -> None:
675 """RED (atomicity): a pack whose first commit is complete and whose second commit
676 names a foreign snapshot is rejected as a whole; neither the valid commit, its
677 snapshot, its object association nor the ref is published."""
678 a = await _repo(db_session, "b1p-mix-a")
679 b = await _repo(db_session, "b1p-mix-b")
680 foreign = b"owned-by-a-mix"
681 foid = blob_id(foreign)
682 fsid, _, _ = await _push_to_a(db_session, a.repo_id, {"secret.txt": foid}, {foid: foreign})
683
684 own = b"b-own-complete"
685 m1 = {"own.txt": blob_id(own)}
686 s1, snap1 = _snap(m1)
687 c1 = _raw_commit("b1", s1)
688 c2 = _raw_commit("b2", fsid, parent=c1["commit_id"])
689 with pytest.raises(ValueError, match=_FOREIGN):
690 await _push(db_session, b.repo_id, {"commits": [c1, c2], "snapshots": [snap1], "tags": [],
691 "blobs": [{"object_id": blob_id(own), "content": own}]},
692 c2["commit_id"])
693 assert await _repo_state(session_factory, b.repo_id) == ([], [], [], [])
694 _, snaps, commits, _ = await _published_state(session_factory, b.repo_id, [s1], [c1["commit_id"], c2["commit_id"]])
695 assert snaps == [] and commits == []
696
697
698 @pytest.mark.asyncio
699 async def test_b1p_branch_tip_naming_a_foreign_commit_absent_from_the_pack_is_rejected(
700 db_session: AsyncSession, session_factory: async_sessionmaker,
701 ) -> None:
702 """RED (tip variant): a pack carrying no commits whose head_commit_id is repo A's
703 commit. Pre-fix step 10 set B's new branch to a commit B never received (a new
704 branch, or --force, skips the fast-forward walk)."""
705 a = await _repo(db_session, "b1p-tip-a")
706 b = await _repo(db_session, "b1p-tip-b")
707 data = b"owned-by-a-tip"
708 oid = blob_id(data)
709 _sid, _snap, ca = await _push_to_a(db_session, a.repo_id, {"secret.txt": oid}, {oid: data})
710
711 with pytest.raises(ValueError, match=_FOREIGN) as ei:
712 await _push(db_session, b.repo_id, {"commits": [], "snapshots": [], "tags": [], "blobs": []},
713 ca["commit_id"])
714 assert ca["commit_id"] in str(ei.value)
715 assert await _repo_state(session_factory, b.repo_id) == ([], [], [], [])
716
717
718 @pytest.mark.asyncio
719 async def test_b1p_branch_tip_naming_a_commit_that_exists_nowhere_is_rejected(
720 db_session: AsyncSession, session_factory: async_sessionmaker,
721 ) -> None:
722 """RED: a branch must never be set to a commit id that is stored nowhere."""
723 b = await _repo(db_session, "b1p-tip-nowhere")
724 with pytest.raises(ValueError, match=_FOREIGN):
725 await _push(db_session, b.repo_id, {"commits": [], "snapshots": [], "tags": [], "blobs": []},
726 "sha256:" + "cd" * 32)
727 assert await _repo_state(session_factory, b.repo_id) == ([], [], [], [])
728
729
730 @pytest.mark.asyncio
731 async def test_b1p_snapshot_admitted_by_full_check_is_linked_and_reusable(
732 db_session: AsyncSession, session_factory: async_sessionmaker,
733 ) -> None:
734 """RED (link every admitted snapshot): B pushes A's snapshot S complete (full
735 check, S already stored globally). S must now be linked to B, so a later honest
736 commit reusing S without resending it is admitted."""
737 a = await _repo(db_session, "b1p-link-a")
738 b = await _repo(db_session, "b1p-link-b")
739 data = b"shared-content-link"
740 oid = blob_id(data)
741 sid, snap, _ = await _push_to_a(db_session, a.repo_id, {"f.txt": oid}, {oid: data})
742
743 cb1 = _raw_commit("b1", sid)
744 await _push(db_session, b.repo_id, {"commits": [cb1], "snapshots": [snap], "tags": [],
745 "blobs": [{"object_id": oid, "content": data}]}, cb1["commit_id"])
746 await db_session.commit()
747 _, snaps, _, _ = await _repo_state(session_factory, b.repo_id)
748 assert snaps == [sid]
749
750 cb2 = _raw_commit("b2-same-tree", sid, parent=cb1["commit_id"])
751 result = await _push(db_session, b.repo_id, {"commits": [cb2], "snapshots": [], "tags": [], "blobs": []},
752 cb2["commit_id"])
753 await db_session.commit()
754 assert result["head"] == cb2["commit_id"]
755
756
757 @pytest.mark.asyncio
758 async def test_commit_reusing_a_linked_snapshot_omitted_from_the_pack_is_admitted(
759 db_session: AsyncSession,
760 ) -> None:
761 """GUARD: a re-commit of an existing tree of this repository, snapshot record not
762 resent, is admitted."""
763 repo = await _repo(db_session, "b1p-own-omit")
764 data = b"kept-file-omit"
765 oid = blob_id(data)
766 sid, _snap, c1 = await _push_to_a(db_session, repo.repo_id, {"k.txt": oid}, {oid: data})
767 c2 = _raw_commit("same-tree", sid, parent=c1["commit_id"])
768 result = await _push(db_session, repo.repo_id, {"commits": [c2], "snapshots": [], "tags": [], "blobs": []},
769 c2["commit_id"])
770 assert result["head"] == c2["commit_id"]
771
772
773 @pytest.mark.asyncio
774 async def test_snapshot_linked_only_through_a_repo_commit_counts_as_this_repos(
775 db_session: AsyncSession,
776 ) -> None:
777 """GUARD (historical shape, ~99% of localhost gabriel/musehub snapshots): the
778 repository's commit is linked but its snapshot has no snapshot ref. A new commit
779 reusing that tree without resending it, and a resend of the same snapshot record,
780 are both admitted."""
781 repo = await _repo(db_session, "b1p-hist")
782 data = b"historic-file"
783 oid = blob_id(data)
784 sid, snap, c1 = await _push_to_a(db_session, repo.repo_id, {"h.txt": oid}, {oid: data})
785 await _drop_snapshot_ref(db_session, repo.repo_id, sid)
786
787 c2 = _raw_commit("reuse", sid, parent=c1["commit_id"])
788 r2 = await _push(db_session, repo.repo_id, {"commits": [c2], "snapshots": [], "tags": [], "blobs": []},
789 c2["commit_id"])
790 await db_session.commit()
791 assert r2["head"] == c2["commit_id"]
792 c3 = _raw_commit("resend", sid, parent=c2["commit_id"])
793 r3 = await _push(db_session, repo.repo_id, {"commits": [c3], "snapshots": [snap], "tags": [], "blobs": []},
794 c3["commit_id"])
795 assert r3["head"] == c3["commit_id"]
796
797
798 @pytest.mark.asyncio
799 async def test_branch_tip_on_a_commit_already_in_this_repo_is_admitted(
800 db_session: AsyncSession,
801 ) -> None:
802 """GUARD: pointing a branch at a commit this repository already holds (e.g. a new
803 branch at an existing commit) needs no commits in the pack."""
804 repo = await _repo(db_session, "b1p-tip-own")
805 data = b"tip-own"
806 oid = blob_id(data)
807 _sid, _snap, c1 = await _push_to_a(db_session, repo.repo_id, {"t.txt": oid}, {oid: data})
808 result = await _push(db_session, repo.repo_id, {"commits": [], "snapshots": [], "tags": [], "blobs": []},
809 c1["commit_id"])
810 assert result["head"] == c1["commit_id"]
811
812
813 @pytest.mark.asyncio
814 async def test_every_admission_rejection_is_logged_with_repo_and_reason(
815 db_session: AsyncSession, caplog: pytest.LogCaptureFixture,
816 ) -> None:
817 """Observability: a rejected push leaves one ``[push-admission] REJECT`` line naming
818 the repository and the reason (no object or snapshot ids), for alerting."""
819 b = await _repo(db_session, "b1p-log")
820 ghost = hash_snapshot({"ghost.txt": blob_id(b"never-pushed-log")}, None)
821 cb = _raw_commit("ghost", ghost)
822 with caplog.at_level("WARNING"), pytest.raises(ValueError, match=_FOREIGN):
823 await _push(db_session, b.repo_id, {"commits": [cb], "snapshots": [], "tags": [], "blobs": []},
824 cb["commit_id"])
825 lines = [r.getMessage() for r in caplog.records if "[push-admission] REJECT" in r.getMessage()]
826 assert lines == [f"[push-admission] REJECT repo={b.repo_id} reason=foreign_snapshot count=1"]
827 assert ghost not in lines[0]
File History 3 commits
sha256:c62240d36e803460a80726fd25ffe9b5c51d9e0193c1f77753e157dc8d5d1d39 fix(wire): bind every pushed commit's snapshot and the bran… Opus 5.5 minor ⚠ 3 days ago
sha256:f09118b15e036b2d250982426f3f4ee89ed2d0f56c026966050faf79c3dea2a3 fix(wire): scope push admission's snapshot exemptions to th… Opus 5.5 patch 3 days ago
sha256:e36ccd951b07d22ec8c5baa36af1b3c52be7c2ca6cc767e05f0a84b789df9aeb fix(wire): refuse a push whose newly introduced snapshot ob… Sonnet 5.5 minor ⚠ 3 days ago