test_mwp1_generation_authority.py
python
sha256:383b9b15b1a7c18348bf797dbec252b8842199567b4b04371578fedd5f7891d4
merge: integrate push-ingest commit verification into dev
Opus 5.5
minor
⚠ breaking
11 days ago
| 1 | """MWP-1 Phase 0 (RED) — commit-graph generation authority. |
| 2 | |
| 3 | Sub-ticket: musehub#106 — https://staging.musehub.ai/gabriel/musehub/issues/106 |
| 4 | Master: muse#58 — https://staging.musehub.ai/gabriel/muse/issues/58 |
| 5 | |
| 6 | Reproduces the clone-after-push staleness root cause (RC-1): both generation |
| 7 | computers fall back to ``generation 0`` when a commit's parent generation cannot |
| 8 | be resolved — |
| 9 | |
| 10 | musehub_wire_push.py:926 (wire_push_unpack_mpack, step 9b) |
| 11 | musehub_wire_push.py:1188 (_build_commit_graph_from_raw) |
| 12 | |
| 13 | both: ``gen = (max(parent_gens) + 1) if parent_gens else 0`` |
| 14 | |
| 15 | That conflates a genuine root commit (parent_ids == [], gen 0 correct) with a |
| 16 | commit whose parents exist but were not resolved (must be parent_gen + 1). A |
| 17 | gen-0 tip then poisons the generation-bounded range scan in ``_walk_commit_delta`` |
| 18 | (``generation <= max_want_gen``), truncating the fetch walk and shipping a clone |
| 19 | that is missing the newest commits. |
| 20 | |
| 21 | Expected Phase 0 status: |
| 22 | - MWP1_01 RED (currently yields generation 0 instead of 3) |
| 23 | - MWP1_02 GREEN (guard — genuine root must stay 0 through every later phase) |
| 24 | - MWP1_03 RED (push path yields generation 0 for the new tip) |
| 25 | |
| 26 | The fix (Phases 1–2) makes generations authoritative by backfilling missing |
| 27 | parent generations from ``musehub_commits`` (the source-of-truth DAG). |
| 28 | """ |
| 29 | from __future__ import annotations |
| 30 | |
| 31 | import datetime |
| 32 | import logging |
| 33 | from unittest.mock import AsyncMock, MagicMock, patch |
| 34 | |
| 35 | import pytest |
| 36 | from sqlalchemy import delete as _sa_delete, select |
| 37 | from sqlalchemy.ext.asyncio import AsyncSession |
| 38 | |
| 39 | from muse.core.ids import hash_commit, hash_snapshot |
| 40 | from muse.core.mpack import build_wire_mpack, parse_wire_mpack |
| 41 | from muse.core.types import blob_id |
| 42 | from musehub.core.genesis import compute_branch_id, compute_identity_id |
| 43 | from musehub.db.musehub_repo_models import ( |
| 44 | MusehubBranch, |
| 45 | MusehubCommit, |
| 46 | MusehubCommitGraph, |
| 47 | MusehubCommitRef, |
| 48 | ) |
| 49 | from musehub.services.musehub_repository import create_repo |
| 50 | from musehub.services.musehub_wire_fetch import wire_fetch_mpack |
| 51 | from musehub.services.musehub_wire_push import ( |
| 52 | _build_commit_graph_from_raw, |
| 53 | _resolve_generation_with_backfill, |
| 54 | check_commit_graph_invariant, |
| 55 | repair_corrupt_commit_generations, |
| 56 | wire_push_unpack_mpack, |
| 57 | ) |
| 58 | |
| 59 | _OWNER = "gabriel" |
| 60 | _IDENTITY_ID = compute_identity_id(b"gabriel") |
| 61 | |
| 62 | |
| 63 | # --------------------------------------------------------------------------- |
| 64 | # Helpers |
| 65 | # --------------------------------------------------------------------------- |
| 66 | |
| 67 | |
| 68 | def _cid(seed: str) -> str: |
| 69 | """Deterministic sha256: commit id from a seed string, for rows seeded directly.""" |
| 70 | return blob_id(f"mwp1-commit-{seed}".encode()) |
| 71 | |
| 72 | |
| 73 | _SEEDS: dict[str, str] = {} |
| 74 | |
| 75 | |
| 76 | def _pushed_cid(seed: str, parent: str | None, snap_id: str) -> str: |
| 77 | """The id of the commit _raw_commit builds for *seed*; push ingest verifies it.""" |
| 78 | cid = hash_commit(parent_ids=[parent] if parent else [], snapshot_id=snap_id, message=f"commit {seed}", |
| 79 | committed_at_iso=_now().isoformat(), author=_OWNER, signer_public_key="") |
| 80 | _SEEDS[cid] = seed |
| 81 | return cid |
| 82 | |
| 83 | |
| 84 | def _now() -> datetime.datetime: |
| 85 | return datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc) |
| 86 | |
| 87 | |
| 88 | def _raw_commit( |
| 89 | cid: str, |
| 90 | parent: str | None, |
| 91 | snap_id: str, |
| 92 | *, |
| 93 | branch: str = "main", |
| 94 | ) -> dict: |
| 95 | """A raw commit dict in the shape both generation computers consume.""" |
| 96 | return { |
| 97 | "commit_id": cid, |
| 98 | "branch": branch, |
| 99 | "message": f"commit {_SEEDS.get(cid, cid[:12])}", |
| 100 | "author": _OWNER, |
| 101 | "committed_at": _now().isoformat(), |
| 102 | "parent_commit_id": parent, |
| 103 | "parent2_commit_id": None, |
| 104 | "snapshot_id": snap_id, |
| 105 | "agent_id": "", |
| 106 | "model_id": "", |
| 107 | "toolchain_id": "", |
| 108 | "sem_ver_bump": "none", |
| 109 | "breaking_changes": [], |
| 110 | "signature": "", |
| 111 | "signer_key_id": "", |
| 112 | "signer_public_key": "", |
| 113 | "prompt_hash": "", |
| 114 | } |
| 115 | |
| 116 | |
| 117 | async def _seed_commit_row( |
| 118 | session: AsyncSession, |
| 119 | cid: str, |
| 120 | parent_ids: list[str], |
| 121 | snap_id: str, |
| 122 | ) -> None: |
| 123 | """Insert a musehub_commits row (source-of-truth DAG) without a graph row.""" |
| 124 | session.add(MusehubCommit( |
| 125 | commit_id=cid, |
| 126 | branch="main", |
| 127 | message=f"commit {cid[:12]}", |
| 128 | author=_OWNER, |
| 129 | timestamp=_now(), |
| 130 | parent_ids=parent_ids, |
| 131 | snapshot_id=snap_id, |
| 132 | )) |
| 133 | await session.flush() |
| 134 | |
| 135 | |
| 136 | async def _seed_graph_row( |
| 137 | session: AsyncSession, |
| 138 | cid: str, |
| 139 | parent_ids: list[str], |
| 140 | generation: int, |
| 141 | snap_id: str, |
| 142 | ) -> None: |
| 143 | session.add(MusehubCommitGraph( |
| 144 | commit_id=cid, |
| 145 | parent_ids=parent_ids, |
| 146 | generation=generation, |
| 147 | snapshot_id=snap_id, |
| 148 | created_at=_now(), |
| 149 | )) |
| 150 | await session.flush() |
| 151 | |
| 152 | |
| 153 | async def _graph_gen(session: AsyncSession, cid: str) -> int | None: |
| 154 | return (await session.execute( |
| 155 | select(MusehubCommitGraph.generation).where(MusehubCommitGraph.commit_id == cid) |
| 156 | )).scalar_one_or_none() |
| 157 | |
| 158 | |
| 159 | def _mock_backend(mpack_bytes: bytes) -> MagicMock: |
| 160 | backend = MagicMock() |
| 161 | backend.get_mpack = AsyncMock(return_value=mpack_bytes) |
| 162 | backend.put = AsyncMock(return_value=None) |
| 163 | backend.put_mpack = AsyncMock(return_value=None) |
| 164 | backend.quarantine_mpack = AsyncMock(return_value=None) |
| 165 | backend.presign_get = AsyncMock(return_value="") |
| 166 | backend.presign_mpack_get = AsyncMock(return_value="") |
| 167 | return backend |
| 168 | |
| 169 | |
| 170 | class _InMemBackend: |
| 171 | """In-memory backend for end-to-end push→fetch tests. |
| 172 | |
| 173 | Pre-seed push mpacks with ``backend._mpacks[mpack_key] = mpack_bytes`` |
| 174 | BEFORE calling ``wire_push_unpack_mpack``. The fetch path writes its |
| 175 | assembled mpack via ``put_mpack`` and the test reads it back directly. |
| 176 | """ |
| 177 | |
| 178 | def __init__(self) -> None: |
| 179 | self._mpacks: dict[str, bytes] = {} |
| 180 | |
| 181 | async def put_mpack(self, mpack_id: str, data: bytes) -> None: |
| 182 | self._mpacks[mpack_id] = data |
| 183 | |
| 184 | async def get_mpack(self, mpack_id: str) -> bytes | None: |
| 185 | return self._mpacks.get(mpack_id) |
| 186 | |
| 187 | async def presign_mpack_get(self, mpack_id: str, ttl: int = 3600) -> str: |
| 188 | return f"inmem://{mpack_id}" |
| 189 | |
| 190 | async def put(self, oid: str, data: bytes) -> str: |
| 191 | return oid |
| 192 | |
| 193 | async def get(self, oid: str) -> bytes | None: |
| 194 | return None |
| 195 | |
| 196 | async def presign_get(self, oid: str, ttl: int = 3600) -> str: |
| 197 | return f"inmem://{oid}" |
| 198 | |
| 199 | async def quarantine_mpack(self, *args: object, **kwargs: object) -> None: |
| 200 | pass |
| 201 | |
| 202 | |
| 203 | # --------------------------------------------------------------------------- |
| 204 | # MWP1_01 — unresolved parent must get parent_gen + 1, never 0 |
| 205 | # --------------------------------------------------------------------------- |
| 206 | |
| 207 | |
| 208 | @pytest.mark.asyncio |
| 209 | async def test_mwp1_01_unresolved_parent_gets_correct_generation( |
| 210 | db_session: AsyncSession, |
| 211 | ) -> None: |
| 212 | """RED: a child whose parent is absent from both the mpack and the commit |
| 213 | graph must receive ``parent_generation + 1`` — not 0. |
| 214 | |
| 215 | Precondition: C1<-C2<-C3 exist in musehub_commits (the authoritative DAG) |
| 216 | but NONE of them have a musehub_commit_graph row. We then compute the graph |
| 217 | for [C4] alone (C4's parent C3 is the external, ungraphed parent). |
| 218 | |
| 219 | True generations: C1=0, C2=1, C3=2, therefore C4 must be 3. |
| 220 | Buggy behaviour: db_parent_gens is empty -> C4 gets generation 0. |
| 221 | """ |
| 222 | c1, c2, c3, c4 = _cid("01-c1"), _cid("01-c2"), _cid("01-c3"), _cid("01-c4") |
| 223 | s1, s2, s3, s4 = (hash_snapshot({"f.txt": blob_id(f"01-{n}".encode())}) |
| 224 | for n in ("s1", "s2", "s3", "s4")) |
| 225 | |
| 226 | await _seed_commit_row(db_session, c1, [], s1) |
| 227 | await _seed_commit_row(db_session, c2, [c1], s2) |
| 228 | await _seed_commit_row(db_session, c3, [c2], s3) |
| 229 | |
| 230 | await _build_commit_graph_from_raw(db_session, [_raw_commit(c4, c3, s4)]) |
| 231 | await db_session.flush() |
| 232 | |
| 233 | gen_c4 = await _graph_gen(db_session, c4) |
| 234 | assert gen_c4 == 3, ( |
| 235 | f"C4 must inherit parent_generation + 1 = 3 (C1=0,C2=1,C3=2,C4=3); " |
| 236 | f"got {gen_c4}. A gen-0 tip poisons the fetch range scan and ships a " |
| 237 | f"stale clone." |
| 238 | ) |
| 239 | |
| 240 | |
| 241 | # --------------------------------------------------------------------------- |
| 242 | # MWP1_02 — genuine root commit stays generation 0 (guard, GREEN) |
| 243 | # --------------------------------------------------------------------------- |
| 244 | |
| 245 | |
| 246 | @pytest.mark.asyncio |
| 247 | async def test_mwp1_02_root_commit_generation_zero( |
| 248 | db_session: AsyncSession, |
| 249 | ) -> None: |
| 250 | """GREEN guard: a commit with no parents is a genuine root — generation 0. |
| 251 | |
| 252 | Later phases must not over-correct the gen-0 fallback into breaking the |
| 253 | legitimate root case. |
| 254 | """ |
| 255 | root = _cid("02-root") |
| 256 | s_root = hash_snapshot({"f.txt": blob_id(b"02-root")}) |
| 257 | |
| 258 | await _build_commit_graph_from_raw(db_session, [_raw_commit(root, None, s_root)]) |
| 259 | await db_session.flush() |
| 260 | |
| 261 | gen_root = await _graph_gen(db_session, root) |
| 262 | assert gen_root == 0, f"root commit (no parents) must be generation 0; got {gen_root}" |
| 263 | |
| 264 | |
| 265 | # --------------------------------------------------------------------------- |
| 266 | # MWP1_03 — push of a child of an ungraphed remote tip (push-path site) |
| 267 | # --------------------------------------------------------------------------- |
| 268 | |
| 269 | |
| 270 | @pytest.mark.asyncio |
| 271 | async def test_mwp1_03_push_child_of_ungraphed_remote_tip( |
| 272 | db_session: AsyncSession, |
| 273 | ) -> None: |
| 274 | """RED: pushing C4 (child of remote tip C3) when C3 has no graph row must |
| 275 | backfill C3's generation and assign C4 = C3 + 1. |
| 276 | |
| 277 | This exercises the OTHER generation site — wire_push_unpack_mpack step 9b. |
| 278 | |
| 279 | Precondition mirrors a real backfill gap: |
| 280 | - musehub_commits: C1<-C2<-C3 (authoritative DAG) |
| 281 | - musehub_commit_graph: C1=0, C2=1 only (C3 deliberately absent) |
| 282 | - branch main -> C3 (remote tip) |
| 283 | Push C4 (parent C3, in the 'have' set so excluded from the mpack). |
| 284 | |
| 285 | True result: C3 backfilled to 2, C4 = 3. |
| 286 | Buggy result: external parent C3 not found in graph -> C4 gets generation 0. |
| 287 | |
| 288 | force=True isolates step-9 generation logic from the FF/ancestor check, which |
| 289 | itself reads the commit graph we are deliberately leaving incomplete. |
| 290 | """ |
| 291 | repo = await create_repo( |
| 292 | db_session, |
| 293 | name="mwp1-03", |
| 294 | owner=_OWNER, |
| 295 | owner_user_id=_IDENTITY_ID, |
| 296 | visibility="public", |
| 297 | initialize=False, |
| 298 | ) |
| 299 | await db_session.commit() |
| 300 | |
| 301 | c1, c2, c3, c4 = _cid("03-c1"), _cid("03-c2"), _cid("03-c3"), _cid("03-c4") |
| 302 | s1 = hash_snapshot({"f1.txt": blob_id(b"03-1")}) |
| 303 | s2 = hash_snapshot({"f2.txt": blob_id(b"03-2")}) |
| 304 | s3 = hash_snapshot({"f3.txt": blob_id(b"03-3")}) |
| 305 | |
| 306 | # Authoritative DAG: all three commits exist in musehub_commits. |
| 307 | await _seed_commit_row(db_session, c1, [], s1) |
| 308 | await _seed_commit_row(db_session, c2, [c1], s2) |
| 309 | await _seed_commit_row(db_session, c3, [c2], s3) |
| 310 | # Graph has C1, C2 — but NOT C3 (the backfill gap). |
| 311 | await _seed_graph_row(db_session, c1, [], 0, s1) |
| 312 | await _seed_graph_row(db_session, c2, [c1], 1, s2) |
| 313 | # Branch tip is C3. |
| 314 | db_session.add(MusehubBranch( |
| 315 | branch_id=compute_branch_id(repo.repo_id, "main"), |
| 316 | repo_id=repo.repo_id, |
| 317 | name="main", |
| 318 | head_commit_id=c3, |
| 319 | )) |
| 320 | await db_session.commit() |
| 321 | |
| 322 | # Build a MUSE-binary mpack carrying ONLY C4 (its parent C3 is external). |
| 323 | oid4 = blob_id(b"03-c4-content") |
| 324 | s4 = hash_snapshot({"f4.txt": oid4}) |
| 325 | c4 = _pushed_cid("03-c4", c3, s4) |
| 326 | mpack_bytes = build_wire_mpack({ |
| 327 | "commits": [_raw_commit(c4, c3, s4)], |
| 328 | "snapshots": [{ |
| 329 | "snapshot_id": s4, |
| 330 | "parent_snapshot_id": None, |
| 331 | "delta_upsert": {"f4.txt": oid4}, |
| 332 | "delta_remove": [], |
| 333 | "directories": [], |
| 334 | }], |
| 335 | "blobs": [{"object_id": oid4, "content": b"03-c4-content"}], |
| 336 | "tags": [], |
| 337 | }) |
| 338 | mpack_key = blob_id(mpack_bytes) |
| 339 | backend = _mock_backend(mpack_bytes) |
| 340 | |
| 341 | with patch("musehub.services.musehub_wire.get_backend", return_value=backend), \ |
| 342 | patch("musehub.services.musehub_wire_push.get_backend", return_value=backend), \ |
| 343 | patch("musehub.storage.backends.get_backend", return_value=backend): |
| 344 | await wire_push_unpack_mpack( |
| 345 | db_session, |
| 346 | repo.repo_id, |
| 347 | mpack_key, |
| 348 | pusher_id=_OWNER, |
| 349 | branch="main", |
| 350 | head_commit_id=c4, |
| 351 | commits_count=1, |
| 352 | blobs_count=1, |
| 353 | force=True, |
| 354 | ) |
| 355 | await db_session.flush() |
| 356 | |
| 357 | gen_c3 = await _graph_gen(db_session, c3) |
| 358 | gen_c4 = await _graph_gen(db_session, c4) |
| 359 | assert gen_c3 == 2, f"C3 must be backfilled to generation 2; got {gen_c3}" |
| 360 | assert gen_c4 == 3, ( |
| 361 | f"C4 (child of remote tip C3) must be generation 3; got {gen_c4}. " |
| 362 | f"A gen-0 new tip is the clone-after-push staleness bug." |
| 363 | ) |
| 364 | |
| 365 | |
| 366 | # --------------------------------------------------------------------------- |
| 367 | # MWP1_04 — Phase 1: seam test for _build_commit_graph_from_raw |
| 368 | # --------------------------------------------------------------------------- |
| 369 | |
| 370 | _WIRE_SHARED_LOGGER = "musehub.services.musehub_wire_shared" |
| 371 | _UNRESOLVED_MARKER = "[MWP1] unresolved parent generations" |
| 372 | |
| 373 | |
| 374 | @pytest.mark.asyncio |
| 375 | async def test_mwp1_04_seam_root_vs_unresolved_build_commit_graph( |
| 376 | db_session: AsyncSession, |
| 377 | caplog: pytest.LogCaptureFixture, |
| 378 | ) -> None: |
| 379 | """GREEN: _build_commit_graph_from_raw separates 'genuine root' from |
| 380 | 'unresolved parent' at the generation seam, then backfills via Phase 2. |
| 381 | |
| 382 | - Root commit (parent_ids == []) → generation 0, NO unresolved warning. |
| 383 | - Child of a parent in musehub_commits but NOT in the graph → fires the |
| 384 | unresolved branch, logs a warning, then backfills correctly. |
| 385 | """ |
| 386 | root = _cid("04-root") |
| 387 | child = _cid("04-child") |
| 388 | ungraphed_parent = _cid("04-ungraphed") |
| 389 | s_root = hash_snapshot({"r.txt": blob_id(b"04-root")}) |
| 390 | s_ungraphed = hash_snapshot({"u.txt": blob_id(b"04-ungraphed")}) |
| 391 | s_child = hash_snapshot({"c.txt": blob_id(b"04-child")}) |
| 392 | |
| 393 | # Seed ungraphed_parent in musehub_commits (authoritative DAG) but NOT in |
| 394 | # musehub_commit_graph — this is the realistic backfill-gap scenario. |
| 395 | await _seed_commit_row(db_session, ungraphed_parent, [], s_ungraphed) |
| 396 | await db_session.flush() |
| 397 | |
| 398 | with caplog.at_level(logging.WARNING, logger=_WIRE_SHARED_LOGGER): |
| 399 | # Root case — must NOT emit unresolved warning. |
| 400 | await _build_commit_graph_from_raw(db_session, [_raw_commit(root, None, s_root)]) |
| 401 | await db_session.flush() |
| 402 | |
| 403 | root_warnings = [ |
| 404 | r.message for r in caplog.records |
| 405 | if _UNRESOLVED_MARKER in r.message |
| 406 | ] |
| 407 | assert root_warnings == [], ( |
| 408 | f"Root commit must not trigger the unresolved-parent warning; got: {root_warnings}" |
| 409 | ) |
| 410 | |
| 411 | caplog.clear() |
| 412 | |
| 413 | with caplog.at_level(logging.WARNING, logger=_WIRE_SHARED_LOGGER): |
| 414 | # Unresolved case — ungraphed_parent is in musehub_commits but NOT in graph. |
| 415 | # The seam must fire the unresolved branch, log a warning, then backfill. |
| 416 | await _build_commit_graph_from_raw( |
| 417 | db_session, |
| 418 | [_raw_commit(child, ungraphed_parent, s_child)], |
| 419 | ) |
| 420 | await db_session.flush() |
| 421 | |
| 422 | unresolved_warnings = [ |
| 423 | r.message for r in caplog.records |
| 424 | if _UNRESOLVED_MARKER in r.message |
| 425 | ] |
| 426 | assert unresolved_warnings, ( |
| 427 | "Expected an unresolved-parent warning from _build_commit_graph_from_raw " |
| 428 | "when the parent has no graph row — none was emitted. " |
| 429 | "The Phase 1 seam is not wired correctly at site 2." |
| 430 | ) |
| 431 | warning_text = unresolved_warnings[0] |
| 432 | assert "_build_commit_graph_from_raw" in warning_text, ( |
| 433 | f"Warning should identify the site (_build_commit_graph_from_raw); got: {warning_text!r}" |
| 434 | ) |
| 435 | assert ungraphed_parent in warning_text or child in warning_text, ( |
| 436 | f"Warning should mention the commit or unresolved parent; got: {warning_text!r}" |
| 437 | ) |
| 438 | |
| 439 | |
| 440 | # --------------------------------------------------------------------------- |
| 441 | # MWP1_05 — Phase 1: seam test for wire_push_unpack_mpack (step 9b) |
| 442 | # --------------------------------------------------------------------------- |
| 443 | |
| 444 | |
| 445 | @pytest.mark.asyncio |
| 446 | async def test_mwp1_05_seam_push_path_logs_unresolved_warning( |
| 447 | db_session: AsyncSession, |
| 448 | caplog: pytest.LogCaptureFixture, |
| 449 | ) -> None: |
| 450 | """GREEN: the wire_push_unpack_mpack generation site logs the unresolved |
| 451 | warning when a pushed commit's parent has no graph row. |
| 452 | |
| 453 | This is the MWP1_03 scenario restated as a spy/log assertion (Phase 1 seam |
| 454 | for the push-path site). The generation value is still wrong (0) until |
| 455 | Phase 2; but the seam is wired and observable. |
| 456 | """ |
| 457 | repo = await create_repo( |
| 458 | db_session, |
| 459 | name="mwp1-05", |
| 460 | owner=_OWNER, |
| 461 | owner_user_id=_IDENTITY_ID, |
| 462 | visibility="public", |
| 463 | initialize=False, |
| 464 | ) |
| 465 | await db_session.commit() |
| 466 | |
| 467 | c1, c2, c3, c4 = _cid("05-c1"), _cid("05-c2"), _cid("05-c3"), _cid("05-c4") |
| 468 | s1 = hash_snapshot({"f1.txt": blob_id(b"05-1")}) |
| 469 | s2 = hash_snapshot({"f2.txt": blob_id(b"05-2")}) |
| 470 | s3 = hash_snapshot({"f3.txt": blob_id(b"05-3")}) |
| 471 | |
| 472 | await _seed_commit_row(db_session, c1, [], s1) |
| 473 | await _seed_commit_row(db_session, c2, [c1], s2) |
| 474 | await _seed_commit_row(db_session, c3, [c2], s3) |
| 475 | await _seed_graph_row(db_session, c1, [], 0, s1) |
| 476 | await _seed_graph_row(db_session, c2, [c1], 1, s2) |
| 477 | # C3 deliberately absent from musehub_commit_graph. |
| 478 | db_session.add(MusehubBranch( |
| 479 | branch_id=compute_branch_id(repo.repo_id, "main"), |
| 480 | repo_id=repo.repo_id, |
| 481 | name="main", |
| 482 | head_commit_id=c3, |
| 483 | )) |
| 484 | await db_session.commit() |
| 485 | |
| 486 | oid4 = blob_id(b"05-c4-content") |
| 487 | s4 = hash_snapshot({"f4.txt": oid4}) |
| 488 | c4 = _pushed_cid("05-c4", c3, s4) |
| 489 | mpack_bytes = build_wire_mpack({ |
| 490 | "commits": [_raw_commit(c4, c3, s4)], |
| 491 | "snapshots": [{ |
| 492 | "snapshot_id": s4, |
| 493 | "parent_snapshot_id": None, |
| 494 | "delta_upsert": {"f4.txt": oid4}, |
| 495 | "delta_remove": [], |
| 496 | "directories": [], |
| 497 | }], |
| 498 | "blobs": [{"object_id": oid4, "content": b"05-c4-content"}], |
| 499 | "tags": [], |
| 500 | }) |
| 501 | mpack_key = blob_id(mpack_bytes) |
| 502 | backend = _mock_backend(mpack_bytes) |
| 503 | |
| 504 | with caplog.at_level(logging.WARNING, logger=_WIRE_SHARED_LOGGER), \ |
| 505 | patch("musehub.services.musehub_wire.get_backend", return_value=backend), \ |
| 506 | patch("musehub.services.musehub_wire_push.get_backend", return_value=backend), \ |
| 507 | patch("musehub.storage.backends.get_backend", return_value=backend): |
| 508 | await wire_push_unpack_mpack( |
| 509 | db_session, |
| 510 | repo.repo_id, |
| 511 | mpack_key, |
| 512 | pusher_id=_OWNER, |
| 513 | branch="main", |
| 514 | head_commit_id=c4, |
| 515 | commits_count=1, |
| 516 | blobs_count=1, |
| 517 | force=True, |
| 518 | ) |
| 519 | await db_session.flush() |
| 520 | |
| 521 | unresolved_warnings = [ |
| 522 | r.message for r in caplog.records |
| 523 | if _UNRESOLVED_MARKER in r.message |
| 524 | ] |
| 525 | assert unresolved_warnings, ( |
| 526 | "Expected the unresolved-parent warning from wire_push_unpack_mpack " |
| 527 | "when C4's parent C3 has no graph row — none was emitted. " |
| 528 | "The Phase 1 seam is not wired at site 1." |
| 529 | ) |
| 530 | warning_text = unresolved_warnings[0] |
| 531 | assert "wire_push_unpack_mpack" in warning_text, ( |
| 532 | f"Warning should identify the site (wire_push_unpack_mpack); got: {warning_text!r}" |
| 533 | ) |
| 534 | |
| 535 | |
| 536 | # --------------------------------------------------------------------------- |
| 537 | # MWP1_06/MWP1_07 — Phase 2: backfill resolves full missing-parent chain |
| 538 | # --------------------------------------------------------------------------- |
| 539 | |
| 540 | |
| 541 | @pytest.mark.asyncio |
| 542 | async def test_mwp1_07_backfill_resolves_chain_of_depth_n( |
| 543 | db_session: AsyncSession, |
| 544 | ) -> None: |
| 545 | """GREEN: _build_commit_graph_from_raw now calls _resolve_generation_with_backfill, |
| 546 | which walks musehub_commits.parent_ids to compute generations for all missing |
| 547 | ancestors. |
| 548 | |
| 549 | Scenario: C1<-C2<-C3<-C4 exist in musehub_commits but NONE in the graph. |
| 550 | Push [C5] (parent C4). True generations: C1=0 C2=1 C3=2 C4=3 C5=4. |
| 551 | After Phase 2 the backfill must upsert C1..C4 and compute C5=4. |
| 552 | """ |
| 553 | c1, c2, c3, c4, c5 = (_cid(f"07-c{i}") for i in range(1, 6)) |
| 554 | snaps = { |
| 555 | c1: hash_snapshot({"a.txt": blob_id(b"07-1")}), |
| 556 | c2: hash_snapshot({"b.txt": blob_id(b"07-2")}), |
| 557 | c3: hash_snapshot({"c.txt": blob_id(b"07-3")}), |
| 558 | c4: hash_snapshot({"d.txt": blob_id(b"07-4")}), |
| 559 | c5: hash_snapshot({"e.txt": blob_id(b"07-5")}), |
| 560 | } |
| 561 | |
| 562 | # Seed the authoritative DAG — no graph rows. |
| 563 | for cid, parent in [(c1, None), (c2, c1), (c3, c2), (c4, c3)]: |
| 564 | await _seed_commit_row(db_session, cid, [parent] if parent else [], snaps[cid]) |
| 565 | |
| 566 | await _build_commit_graph_from_raw(db_session, [_raw_commit(c5, c4, snaps[c5])]) |
| 567 | await db_session.flush() |
| 568 | |
| 569 | expected = {c1: 0, c2: 1, c3: 2, c4: 3, c5: 4} |
| 570 | for cid, exp_gen in expected.items(): |
| 571 | actual = await _graph_gen(db_session, cid) |
| 572 | assert actual == exp_gen, ( |
| 573 | f"commit {cid[:16]} expected generation {exp_gen}; got {actual}. " |
| 574 | f"Backfill must compute bottom-up from the root." |
| 575 | ) |
| 576 | |
| 577 | |
| 578 | # --------------------------------------------------------------------------- |
| 579 | # MWP1_08 — Phase 2: missing-from-musehub_commits raises integrity error |
| 580 | # --------------------------------------------------------------------------- |
| 581 | |
| 582 | |
| 583 | @pytest.mark.asyncio |
| 584 | async def test_mwp1_08_absent_parent_raises_integrity_error( |
| 585 | db_session: AsyncSession, |
| 586 | ) -> None: |
| 587 | """GREEN: pushing a commit whose parent is absent from musehub_commits raises |
| 588 | ValueError with the [MWP1] integrity error tag. |
| 589 | |
| 590 | C2 claims C1 as its parent, but C1 is NOT in musehub_commits. The fast-forward |
| 591 | push invariant requires every referenced commit to already be on the server. |
| 592 | """ |
| 593 | phantom = _cid("08-phantom") # not inserted into musehub_commits |
| 594 | c2 = _cid("08-c2") |
| 595 | s2 = hash_snapshot({"f.txt": blob_id(b"08-2")}) |
| 596 | |
| 597 | with pytest.raises(ValueError, match=r"\[MWP1\] integrity error"): |
| 598 | await _resolve_generation_with_backfill( |
| 599 | db_session, [phantom], |
| 600 | inline_gen={}, |
| 601 | db_gen={}, |
| 602 | ) |
| 603 | |
| 604 | # Also verify via the full build path (C2 -> phantom, phantom absent). |
| 605 | with pytest.raises(ValueError, match=r"\[MWP1\] integrity error"): |
| 606 | await _build_commit_graph_from_raw( |
| 607 | db_session, [_raw_commit(c2, phantom, s2)] |
| 608 | ) |
| 609 | |
| 610 | |
| 611 | # --------------------------------------------------------------------------- |
| 612 | # MWP1_09 — Phase 2 stress: 1 000-commit chain backfills correctly and bounded |
| 613 | # --------------------------------------------------------------------------- |
| 614 | |
| 615 | |
| 616 | @pytest.mark.asyncio |
| 617 | async def test_mwp1_09_stress_1000_commit_chain( |
| 618 | db_session: AsyncSession, |
| 619 | ) -> None: |
| 620 | """GREEN: backfill over a 1,000-commit missing chain completes with correct |
| 621 | generations and stops at the first anchor (a commit already in the graph). |
| 622 | |
| 623 | Scenario: |
| 624 | - C0 in musehub_commit_graph with generation=0 (anchor). |
| 625 | - C1..C999 in musehub_commits only — no graph rows. |
| 626 | - Call _resolve_generation_with_backfill([C999]). |
| 627 | |
| 628 | Expected: C999=999. The walk must stop at C0 (the anchor) without walking |
| 629 | its parents (C0 has no parents, but the point is the graph lookup short-circuits). |
| 630 | The chain must be fully backfilled: C1=1, C500=500, C999=999. |
| 631 | """ |
| 632 | n = 1000 |
| 633 | # Build commit IDs: C0 is the root anchor; C1..C999 are ungraphed. |
| 634 | chain = [_cid(f"09-c{i:04d}") for i in range(n)] |
| 635 | snap = hash_snapshot({"f.txt": blob_id(b"09-root")}) |
| 636 | |
| 637 | # Seed musehub_commits for ALL n commits. |
| 638 | for i, cid in enumerate(chain): |
| 639 | parent = [chain[i - 1]] if i > 0 else [] |
| 640 | await _seed_commit_row(db_session, cid, parent, snap) |
| 641 | |
| 642 | # Seed musehub_commit_graph ONLY for C0 (the anchor). |
| 643 | await _seed_graph_row(db_session, chain[0], [], 0, snap) |
| 644 | await db_session.flush() |
| 645 | |
| 646 | # Backfill from C999 upward. |
| 647 | result = await _resolve_generation_with_backfill( |
| 648 | db_session, [chain[n - 1]], |
| 649 | inline_gen={}, |
| 650 | db_gen={}, |
| 651 | ) |
| 652 | await db_session.flush() |
| 653 | |
| 654 | assert result == n - 1, ( |
| 655 | f"_resolve_generation_with_backfill([C{n-1}]) must return {n - 1}; got {result}" |
| 656 | ) |
| 657 | |
| 658 | # Spot-check a few points in the chain. |
| 659 | for i in [1, 100, 500, 999]: |
| 660 | actual = await _graph_gen(db_session, chain[i]) |
| 661 | assert actual == i, ( |
| 662 | f"C{i} must have generation {i} after backfill; got {actual}" |
| 663 | ) |
| 664 | |
| 665 | # Anchor C0 must not have been modified (it was already gen=0). |
| 666 | assert await _graph_gen(db_session, chain[0]) == 0, ( |
| 667 | "Anchor commit C0 must remain at generation 0 after backfill." |
| 668 | ) |
| 669 | |
| 670 | |
| 671 | # --------------------------------------------------------------------------- |
| 672 | # MWP1_10 — Phase 3: repair routine corrects pre-existing corrupt rows |
| 673 | # --------------------------------------------------------------------------- |
| 674 | |
| 675 | |
| 676 | @pytest.mark.asyncio |
| 677 | async def test_mwp1_10_repair_corrects_corrupt_rows( |
| 678 | db_session: AsyncSession, |
| 679 | ) -> None: |
| 680 | """GREEN: repair_corrupt_commit_generations fixes graph rows that were written |
| 681 | with generation=0 but have non-empty parent_ids (the RC-1 bug artefacts). |
| 682 | |
| 683 | Scenario mirrors a database state left behind by the pre-Phase-2 push path: |
| 684 | - C1: graph row gen=0, parent_ids=[] (true root — must NOT be touched) |
| 685 | - C2: graph row gen=0, parent_ids=[C1] (corrupt — parent is a true root) |
| 686 | - C3: graph row gen=0, parent_ids=[C2] (corrupt — parent is also corrupt) |
| 687 | - C4: graph row gen=0, parent_ids=[C3] (corrupt — deepest leaf) |
| 688 | |
| 689 | After repair: C1=0 (unchanged), C2=1, C3=2, C4=3. |
| 690 | """ |
| 691 | c1, c2, c3, c4 = (_cid(f"10-c{i}") for i in range(1, 5)) |
| 692 | snap = hash_snapshot({"f.txt": blob_id(b"10-root")}) |
| 693 | |
| 694 | # Pre-seed corrupt graph rows (simulating the old buggy push output). |
| 695 | # ALL four rows have generation=0 — C1 is a true root, C2/C3/C4 are corrupt. |
| 696 | await _seed_graph_row(db_session, c1, [], 0, snap) # true root — correct |
| 697 | await _seed_graph_row(db_session, c2, [c1], 0, snap) # corrupt |
| 698 | await _seed_graph_row(db_session, c3, [c2], 0, snap) # corrupt |
| 699 | await _seed_graph_row(db_session, c4, [c3], 0, snap) # corrupt |
| 700 | await db_session.flush() |
| 701 | |
| 702 | result = await repair_corrupt_commit_generations(db_session) |
| 703 | await db_session.flush() |
| 704 | |
| 705 | assert result["total_corrupt"] == 3, ( |
| 706 | f"Repair must identify exactly 3 corrupt rows (C2,C3,C4); got {result}" |
| 707 | ) |
| 708 | assert result["repaired"] == 3, ( |
| 709 | f"Repair must correct all 3 corrupt rows; got {result}" |
| 710 | ) |
| 711 | |
| 712 | # Verify correct generations after repair. |
| 713 | expected = {c1: 0, c2: 1, c3: 2, c4: 3} |
| 714 | for cid, exp_gen in expected.items(): |
| 715 | actual = await _graph_gen(db_session, cid) |
| 716 | assert actual == exp_gen, ( |
| 717 | f"After repair: commit {cid[:16]} expected gen={exp_gen}; got {actual}" |
| 718 | ) |
| 719 | |
| 720 | |
| 721 | # --------------------------------------------------------------------------- |
| 722 | # MWP1_11 — Phase 3: re-running the repair is a no-op (idempotent) |
| 723 | # --------------------------------------------------------------------------- |
| 724 | |
| 725 | |
| 726 | @pytest.mark.asyncio |
| 727 | async def test_mwp1_11_repair_is_idempotent( |
| 728 | db_session: AsyncSession, |
| 729 | ) -> None: |
| 730 | """GREEN: calling repair_corrupt_commit_generations twice is a no-op on the |
| 731 | second call — it finds zero corrupt rows and returns immediately. |
| 732 | |
| 733 | Uses the same scenario as MWP1_10 to confirm that after the first repair pass |
| 734 | all previously-corrupt rows now have correct non-zero generations, so the |
| 735 | WHERE generation=0 AND cardinality(parent_ids)>0 predicate matches nothing. |
| 736 | """ |
| 737 | c1, c2, c3 = (_cid(f"11-c{i}") for i in range(1, 4)) |
| 738 | snap = hash_snapshot({"f.txt": blob_id(b"11-root")}) |
| 739 | |
| 740 | await _seed_graph_row(db_session, c1, [], 0, snap) # true root |
| 741 | await _seed_graph_row(db_session, c2, [c1], 0, snap) # corrupt |
| 742 | await _seed_graph_row(db_session, c3, [c2], 0, snap) # corrupt |
| 743 | await db_session.flush() |
| 744 | |
| 745 | # First call — repairs C2 and C3. |
| 746 | first = await repair_corrupt_commit_generations(db_session) |
| 747 | await db_session.flush() |
| 748 | |
| 749 | assert first["total_corrupt"] == 2 |
| 750 | assert first["repaired"] == 2 |
| 751 | |
| 752 | # Second call — must find nothing to repair. |
| 753 | second = await repair_corrupt_commit_generations(db_session) |
| 754 | await db_session.flush() |
| 755 | |
| 756 | assert second["total_corrupt"] == 0, ( |
| 757 | f"Second repair call must find 0 corrupt rows; got {second}" |
| 758 | ) |
| 759 | assert second["repaired"] == 0, ( |
| 760 | f"Second repair call must repair 0 rows; got {second}" |
| 761 | ) |
| 762 | |
| 763 | # Generations must still be correct after both passes. |
| 764 | assert await _graph_gen(db_session, c1) == 0 |
| 765 | assert await _graph_gen(db_session, c2) == 1 |
| 766 | assert await _graph_gen(db_session, c3) == 2 |
| 767 | |
| 768 | |
| 769 | # --------------------------------------------------------------------------- |
| 770 | # MWP1_12 — Phase 4: data-integrity invariant |
| 771 | # --------------------------------------------------------------------------- |
| 772 | |
| 773 | |
| 774 | @pytest.mark.asyncio |
| 775 | async def test_mwp1_12_commit_graph_invariant( |
| 776 | db_session: AsyncSession, |
| 777 | ) -> None: |
| 778 | """GREEN: check_commit_graph_invariant detects corrupt rows (gen=0 with parents) |
| 779 | and reports clean after repair_corrupt_commit_generations fixes them. |
| 780 | |
| 781 | This is the assertable invariant for CI / verify: no musehub_commit_graph |
| 782 | row may have generation==0 AND non-empty parent_ids. |
| 783 | """ |
| 784 | c1, c2, c3 = (_cid(f"12-c{i}") for i in range(1, 4)) |
| 785 | snap = hash_snapshot({"f.txt": blob_id(b"12-root")}) |
| 786 | |
| 787 | # Seed: C1 is a true root (gen=0, no parents — valid). |
| 788 | # C2 and C3 are corrupt (gen=0 with parents). |
| 789 | await _seed_graph_row(db_session, c1, [], 0, snap) # valid root |
| 790 | await _seed_graph_row(db_session, c2, [c1], 0, snap) # corrupt |
| 791 | await _seed_graph_row(db_session, c3, [c2], 0, snap) # corrupt |
| 792 | await db_session.flush() |
| 793 | |
| 794 | # Invariant must report violations before repair. |
| 795 | pre = await check_commit_graph_invariant(db_session) |
| 796 | assert pre["valid"] is False, ( |
| 797 | f"Invariant must detect corrupt rows before repair; got {pre}" |
| 798 | ) |
| 799 | assert pre["violations"] == 2, ( |
| 800 | f"Invariant must count exactly 2 violations (C2, C3); got {pre}" |
| 801 | ) |
| 802 | |
| 803 | # Repair, then re-check. |
| 804 | await repair_corrupt_commit_generations(db_session) |
| 805 | await db_session.flush() |
| 806 | |
| 807 | post = await check_commit_graph_invariant(db_session) |
| 808 | assert post["valid"] is True, ( |
| 809 | f"Invariant must pass after repair; got {post}" |
| 810 | ) |
| 811 | assert post["violations"] == 0, ( |
| 812 | f"Invariant must report 0 violations after repair; got {post}" |
| 813 | ) |
| 814 | |
| 815 | |
| 816 | # --------------------------------------------------------------------------- |
| 817 | # MWP1_13 — Phase 4: fetch-side guard fires on snapshot mismatch |
| 818 | # --------------------------------------------------------------------------- |
| 819 | |
| 820 | |
| 821 | @pytest.mark.asyncio |
| 822 | async def test_mwp1_13_fetch_side_guard_fires_on_snapshot_mismatch( |
| 823 | db_session: AsyncSession, |
| 824 | ) -> None: |
| 825 | """GREEN: wire_fetch_mpack calls repair_corrupt_commit_generations when the |
| 826 | CommitGraph's max-gen snapshot disagrees with the authoritative MusehubCommit |
| 827 | snapshot (the BLOB-DEBUG mismatch condition). |
| 828 | |
| 829 | Setup: |
| 830 | - tip in CommitGraph: generation=0, snapshot_id=OLD_SNAP (stale — RC-1 artefact) |
| 831 | - tip in MusehubCommit: snapshot_id=NEW_SNAP (authoritative) |
| 832 | |
| 833 | The guard detects want_tip_snap_id (OLD_SNAP) ∉ want_snap_ids_from_commit_rows |
| 834 | ({NEW_SNAP}) and calls repair_corrupt_commit_generations before assembling. |
| 835 | """ |
| 836 | repo = await create_repo( |
| 837 | db_session, |
| 838 | name="mwp1-13", |
| 839 | owner=_OWNER, |
| 840 | owner_user_id=_IDENTITY_ID, |
| 841 | visibility="public", |
| 842 | initialize=False, |
| 843 | ) |
| 844 | await db_session.commit() |
| 845 | |
| 846 | tip = _cid("13-tip") |
| 847 | old_snap = hash_snapshot({"old.txt": blob_id(b"13-old")}) |
| 848 | new_snap = hash_snapshot({"new.txt": blob_id(b"13-new")}) |
| 849 | |
| 850 | # Authoritative DAG: tip has new_snap (what the client should receive). |
| 851 | await _seed_commit_row(db_session, tip, [], new_snap) |
| 852 | db_session.add(MusehubCommitRef(repo_id=repo.repo_id, commit_id=tip)) |
| 853 | # CommitGraph: tip has gen=0 with OLD snapshot (the stale RC-1 state). |
| 854 | await _seed_graph_row(db_session, tip, [], 0, old_snap) |
| 855 | await db_session.flush() |
| 856 | |
| 857 | backend = _mock_backend(b"") |
| 858 | |
| 859 | with patch( |
| 860 | "musehub.services.musehub_wire_fetch.repair_corrupt_commit_generations", |
| 861 | new_callable=AsyncMock, |
| 862 | ) as mock_repair, \ |
| 863 | patch("musehub.services.musehub_wire.get_backend", return_value=backend), \ |
| 864 | patch("musehub.services.musehub_wire_fetch.get_backend", return_value=backend), \ |
| 865 | patch("musehub.storage.backends.get_backend", return_value=backend): |
| 866 | |
| 867 | mock_repair.return_value = {"total_corrupt": 0, "repaired": 0} |
| 868 | |
| 869 | try: |
| 870 | await wire_fetch_mpack( |
| 871 | db_session, |
| 872 | repo.repo_id, |
| 873 | want=[tip], |
| 874 | have=[], |
| 875 | force_build=True, |
| 876 | ) |
| 877 | except Exception: |
| 878 | pass # function may fail on missing blobs — only the guard call matters |
| 879 | |
| 880 | mock_repair.assert_called_once_with(db_session) |
| 881 | |
| 882 | |
| 883 | # --------------------------------------------------------------------------- |
| 884 | # MWP1_14 — Phase 5: end-to-end regression proof |
| 885 | # --------------------------------------------------------------------------- |
| 886 | |
| 887 | |
| 888 | @pytest.mark.asyncio |
| 889 | async def test_mwp1_14_end_to_end_push_clone_push_clone( |
| 890 | db_session: AsyncSession, |
| 891 | ) -> None: |
| 892 | """GREEN: push C1→C2→C3, clone, push C4, clone again → the second clone |
| 893 | contains all 4 commits (C1 through C4) and C4's blob. |
| 894 | |
| 895 | This is the acceptance gate for MWP-1. It directly reproduces the |
| 896 | clone-after-push staleness (RC-1 bug) and proves the fix holds: |
| 897 | |
| 898 | 1. Push C1/C2/C3 individually. |
| 899 | 2. Corrupt C3's CommitGraph row (delete it) — simulates the backfill gap |
| 900 | that the RC-1 bug created: a parent that is on the server in |
| 901 | musehub_commits but absent from musehub_commit_graph. |
| 902 | 3. Push C4 (parent C3). |
| 903 | - WITHOUT the fix: C3 not in graph → parent_gens empty → C4 gets gen=0. |
| 904 | The range scan (gen ≤ max_want_gen=0) returns only C4 (and possibly C1) |
| 905 | so the clone is missing C2 and C3. |
| 906 | - WITH the fix: _resolve_generation_with_backfill restores C3=2, so |
| 907 | C4 gets gen=3 and the full history is returned. |
| 908 | 4. Clone with want=[C4], have=[] → assert all 4 commits and C4's blob. |
| 909 | """ |
| 910 | repo = await create_repo( |
| 911 | db_session, |
| 912 | name="mwp1-14", |
| 913 | owner=_OWNER, |
| 914 | owner_user_id=_IDENTITY_ID, |
| 915 | visibility="public", |
| 916 | initialize=False, |
| 917 | ) |
| 918 | await db_session.commit() |
| 919 | |
| 920 | c1, c2, c3, c4 = (_cid(f"14-c{i}") for i in range(1, 5)) |
| 921 | oid1 = blob_id(b"14-c1-blob") |
| 922 | oid2 = blob_id(b"14-c2-blob") |
| 923 | oid3 = blob_id(b"14-c3-blob") |
| 924 | oid4 = blob_id(b"14-c4-blob") |
| 925 | s1 = hash_snapshot({"f1.txt": oid1}) |
| 926 | s2 = hash_snapshot({"f2.txt": oid2}) |
| 927 | s3 = hash_snapshot({"f3.txt": oid3}) |
| 928 | s4 = hash_snapshot({"f4.txt": oid4}) |
| 929 | c1 = _pushed_cid("14-c1", None, s1) |
| 930 | c2 = _pushed_cid("14-c2", c1, s2) |
| 931 | c3 = _pushed_cid("14-c3", c2, s3) |
| 932 | c4 = _pushed_cid("14-c4", c3, s4) |
| 933 | |
| 934 | backend = _InMemBackend() |
| 935 | |
| 936 | def _single_commit_mpack( |
| 937 | cid: str, parent: str | None, snap_id: str, |
| 938 | fpath: str, oid: str, data: bytes, |
| 939 | ) -> tuple[bytes, str]: |
| 940 | mpack_bytes = build_wire_mpack({ |
| 941 | "commits": [_raw_commit(cid, parent, snap_id)], |
| 942 | "snapshots": [{ |
| 943 | "snapshot_id": snap_id, |
| 944 | "parent_snapshot_id": None, |
| 945 | "delta_upsert": {fpath: oid}, |
| 946 | "delta_remove": [], |
| 947 | "directories": [], |
| 948 | }], |
| 949 | "blobs": [{"object_id": oid, "content": data}], |
| 950 | "tags": [], |
| 951 | }) |
| 952 | mpack_key = blob_id(mpack_bytes) |
| 953 | backend._mpacks[mpack_key] = mpack_bytes |
| 954 | return mpack_bytes, mpack_key |
| 955 | |
| 956 | async def _push( |
| 957 | cid: str, parent: str | None, snap_id: str, |
| 958 | fpath: str, oid: str, data: bytes, |
| 959 | ) -> None: |
| 960 | _, mpack_key = _single_commit_mpack(cid, parent, snap_id, fpath, oid, data) |
| 961 | with patch("musehub.services.musehub_wire.get_backend", return_value=backend), \ |
| 962 | patch("musehub.services.musehub_wire_push.get_backend", return_value=backend), \ |
| 963 | patch("musehub.storage.backends.get_backend", return_value=backend): |
| 964 | await wire_push_unpack_mpack( |
| 965 | db_session, |
| 966 | repo.repo_id, |
| 967 | mpack_key, |
| 968 | pusher_id=_OWNER, |
| 969 | branch="main", |
| 970 | head_commit_id=cid, |
| 971 | commits_count=1, |
| 972 | blobs_count=1, |
| 973 | force=True, |
| 974 | ) |
| 975 | await db_session.flush() |
| 976 | |
| 977 | # Step 1: push C1, C2, C3 in sequence. |
| 978 | await _push(c1, None, s1, "f1.txt", oid1, b"14-c1-blob") |
| 979 | await _push(c2, c1, s2, "f2.txt", oid2, b"14-c2-blob") |
| 980 | await _push(c3, c2, s3, "f3.txt", oid3, b"14-c3-blob") |
| 981 | |
| 982 | # Verify correct generations after initial pushes. |
| 983 | assert await _graph_gen(db_session, c1) == 0 |
| 984 | assert await _graph_gen(db_session, c2) == 1 |
| 985 | assert await _graph_gen(db_session, c3) == 2 |
| 986 | |
| 987 | # Step 2: simulate the RC-1 backfill gap — delete C3 from CommitGraph. |
| 988 | # This is the state that the pre-fix push path produced when a parent's |
| 989 | # graph row was missing: the next push would compute gen=0 for C4. |
| 990 | await db_session.execute( |
| 991 | _sa_delete(MusehubCommitGraph).where(MusehubCommitGraph.commit_id == c3) |
| 992 | ) |
| 993 | await db_session.flush() |
| 994 | assert await _graph_gen(db_session, c3) is None, "C3 must be absent from graph after deletion" |
| 995 | |
| 996 | # Step 3: push C4 (parent C3). Phase 2 fix backfills C3=2, so C4 gets gen=3. |
| 997 | await _push(c4, c3, s4, "f4.txt", oid4, b"14-c4-blob") |
| 998 | |
| 999 | gen_c3_after = await _graph_gen(db_session, c3) |
| 1000 | gen_c4 = await _graph_gen(db_session, c4) |
| 1001 | assert gen_c3_after == 2, ( |
| 1002 | f"C3 must be backfilled to generation 2 after pushing C4; got {gen_c3_after}. " |
| 1003 | f"Without the fix C3 stays absent and C4 gets gen=0." |
| 1004 | ) |
| 1005 | assert gen_c4 == 3, ( |
| 1006 | f"C4 must receive generation 3 (C3+1); got {gen_c4}. " |
| 1007 | f"A gen-0 tip causes the range scan to miss C1/C2/C3 in the second clone." |
| 1008 | ) |
| 1009 | |
| 1010 | # Step 4: clone with want=[C4], have=[] — the second clone must include all history. |
| 1011 | fetch_result: dict | None = None |
| 1012 | with patch("musehub.services.musehub_wire.get_backend", return_value=backend), \ |
| 1013 | patch("musehub.services.musehub_wire_fetch.get_backend", return_value=backend), \ |
| 1014 | patch("musehub.storage.backends.get_backend", return_value=backend): |
| 1015 | fetch_result = await wire_fetch_mpack( |
| 1016 | db_session, |
| 1017 | repo.repo_id, |
| 1018 | want=[c4], |
| 1019 | have=[], |
| 1020 | force_build=True, |
| 1021 | ) |
| 1022 | |
| 1023 | assert fetch_result is not None |
| 1024 | assert fetch_result["mpack_id"] is not None, "Fetch must return a mpack_id" |
| 1025 | |
| 1026 | assembled_mpack_id = fetch_result["mpack_id"] |
| 1027 | assert assembled_mpack_id in backend._mpacks, ( |
| 1028 | "Assembled mpack must be stored in the backend" |
| 1029 | ) |
| 1030 | |
| 1031 | assembled_bytes = backend._mpacks[assembled_mpack_id] |
| 1032 | parsed = parse_wire_mpack(assembled_bytes) |
| 1033 | |
| 1034 | cloned_commit_ids = {c["commit_id"] for c in parsed.get("commits", [])} |
| 1035 | cloned_blob_oids = {b["object_id"] for b in parsed.get("blobs", [])} |
| 1036 | |
| 1037 | # Primary acceptance gate: C4 and its blob must be in the second clone. |
| 1038 | assert c4 in cloned_commit_ids, ( |
| 1039 | f"C4 (the just-pushed commit) must be in the second clone; " |
| 1040 | f"commits found: {[cid[:16] for cid in cloned_commit_ids]}" |
| 1041 | ) |
| 1042 | assert oid4 in cloned_blob_oids, ( |
| 1043 | f"C4's blob (oid4) must be in the second clone's blobs; " |
| 1044 | f"blobs found: {[o[:16] for o in cloned_blob_oids]}" |
| 1045 | ) |
| 1046 | |
| 1047 | # Full-history gate: a fresh clone must include the complete ancestor chain. |
| 1048 | # WITHOUT the fix: C4 gets gen=0, max_want_gen=0, range scan misses C2/C3 |
| 1049 | # (gen=1,2 are > max_want_gen=0), so they are absent from the clone. |
| 1050 | assert c1 in cloned_commit_ids, ( |
| 1051 | f"C1 must be in the second clone (full history); " |
| 1052 | f"WITHOUT the fix this fails because C4 gets gen=0 and the range scan " |
| 1053 | f"(gen <= 0) misses C1's ancestors. commits: {[cid[:16] for cid in cloned_commit_ids]}" |
| 1054 | ) |
| 1055 | assert c2 in cloned_commit_ids, ( |
| 1056 | f"C2 must be in the second clone (full history); got {[cid[:16] for cid in cloned_commit_ids]}" |
| 1057 | ) |
| 1058 | assert c3 in cloned_commit_ids, ( |
| 1059 | f"C3 must be in the second clone (full history); got {[cid[:16] for cid in cloned_commit_ids]}" |
| 1060 | ) |
File History
10 commits
sha256:383b9b15b1a7c18348bf797dbec252b8842199567b4b04371578fedd5f7891d4
merge: integrate push-ingest commit verification into dev
Opus 5.5
minor
⚠
11 days ago
sha256:39b90cebf3ab4c72ef67a29fe9d451ba1394084edf4053dff98878651f5d2378
test: build reproducible commit ids in push fixtures
Opus 5.5
11 days ago
sha256:4defb0407ee7fe26b1addb8df5cf3d31dae4ebf1ab92eeca1c05348b9d4c8458
merge: integrate R1 into the publishable #114 line (private…
Opus 5.5
minor
⚠
12 days ago
sha256:83227647a82e1733a20b0b6d225df3d153769630d3c710d255e0c1b8aab8ae61
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Opus 5.5
minor
⚠
13 days ago
sha256:1aca297eaa71ac4ab5bd77dcedeb4bf3bd1a1cb1a13fb058ade9fc3233d090a8
test(mwp-1): Phase 5 — end-to-end regression proof (MWP1_14)
Sonnet 4.6
141 days ago
sha256:2e0d3cd2563e7f2f301a1183c5a000a2f2c4088c83beb12155af20c10f16e862
feat(mwp-1): Phase 4 — data-integrity invariant and fetch-s…
Sonnet 4.6
patch
141 days ago
sha256:667089b438359d91efd6337e44c0bcb653deaf6ad3937861c5d2707457ff0aae
feat(mwp-1): Phase 3 — repair_corrupt_commit_generations fo…
Sonnet 4.6
patch
141 days ago
sha256:e7097592983d62d4acf97d03e05f44d6f96541aecd65b141fe7f525d0cb088cc
feat(mwp-1): Phase 2 — authoritative generation backfill vi…
Sonnet 4.6
patch
141 days ago
sha256:9fa9f5bd7edf5160c2425cf7c3b6ab9dd677d41a9593565c7e8e94900a0cef41
feat(mwp-1): Phase 1 — distinguish root from unresolved at …
Sonnet 4.6
patch
141 days ago
sha256:bb4fa850f693e22ecdf6b4f3d12a4996f55972d50184da54b8561d5a52dc2fba
test(mwp-1): Phase 0 — red reproduction of commit-graph gen…
Sonnet 4.6
141 days ago