test_push_ingest_commit_integrity.py
python
sha256:383b9b15b1a7c18348bf797dbec252b8842199567b4b04371578fedd5f7891d4
merge: integrate push-ingest commit verification into dev
Opus 5.5
minor
⚠ breaking
11 days ago
| 1 | """Push ingest verifies commit records and reconciles duplicates. |
| 2 | |
| 3 | Contract (PIC_01..PIC_17): |
| 4 | |
| 5 | * Every pushed commit must reproduce its commit id exactly as the serve path |
| 6 | rebuilds it from the stored row, raw committed_at included. No exception. |
| 7 | * A new commit whose id binds a signer must carry a signature that verifies; |
| 8 | an unsigned commit must carry no signature. Otherwise the push is refused |
| 9 | before anything is written and the mpack is quarantined. |
| 10 | * A pushed record for a commit that is already stored is compared with the |
| 11 | stored row under a row lock. The row changes only where cryptography |
| 12 | decides: a stored identity that does not reproduce, or a stored signature |
| 13 | that does not verify, is replaced by the verified pushed values. Fields no |
| 14 | id or signature covers (structured_delta, sem_ver_bump, breaking_changes, |
| 15 | signer_key_id, branch) are never rewritten. Every difference is reported in |
| 16 | the push result's ``divergent_commits``. |
| 17 | * A heal invalidates the fetch cache of every repository that references the |
| 18 | commit. |
| 19 | |
| 20 | Real server push (``wire_push_unpack_mpack``), real server fetch |
| 21 | (``wire_fetch_mpack``) and the real client ``apply_mpack``. Synthetic keys, |
| 22 | in-memory blob store, private test database. |
| 23 | """ |
| 24 | from __future__ import annotations |
| 25 | |
| 26 | import asyncio |
| 27 | import datetime |
| 28 | import json |
| 29 | import pathlib |
| 30 | |
| 31 | import pytest |
| 32 | from cryptography.exceptions import InvalidSignature |
| 33 | from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey, Ed25519PublicKey |
| 34 | from sqlalchemy import func, select, text |
| 35 | from sqlalchemy.ext.asyncio import AsyncSession |
| 36 | |
| 37 | from muse.core.ids import hash_commit, hash_snapshot |
| 38 | from muse.core.mpack import apply_mpack, build_wire_mpack, parse_wire_mpack |
| 39 | from muse.core.paths import init_repo_dirs |
| 40 | from muse.core.provenance import encode_public_key, provenance_payload, sign_commit_ed25519 |
| 41 | from muse.core.types import blob_id, decode_pubkey, decode_sig, public_key_fingerprint |
| 42 | from musehub.core.genesis import compute_identity_id |
| 43 | from musehub.db.musehub_jobs_models import MusehubBackgroundJob |
| 44 | from musehub.db.musehub_repo_models import MusehubCommit, MusehubFetchMPackCache |
| 45 | from musehub.services.musehub_repository import create_repo |
| 46 | from musehub.services.musehub_wire_fetch import wire_fetch_mpack |
| 47 | from musehub.services.musehub_wire_push import wire_push_unpack_mpack |
| 48 | from musehub.services.musehub_wire_shared import MPackValidationError |
| 49 | from musehub.storage import backends as _backends |
| 50 | from musehub.types.json_types import IntDict, JSONObject |
| 51 | from tests.conftest import _TEST_ENGINE |
| 52 | |
| 53 | AT = datetime.datetime(2026, 5, 21, 3, 27, 48, 41574, tzinfo=datetime.timezone.utc).isoformat() |
| 54 | AT_NON_UTC = "2026-03-13T10:00:00.123456-07:00" |
| 55 | TABLES = ("musehub_commits", "musehub_snapshots", "musehub_objects", "musehub_object_refs", |
| 56 | "musehub_commit_refs", "musehub_snapshot_refs", "musehub_mpack_index", |
| 57 | "musehub_commit_graph", "musehub_background_jobs") |
| 58 | |
| 59 | |
| 60 | def _delta(tag: str) -> JSONObject: |
| 61 | return {"domain": "code", "ops": [{"op": "insert", "address": f"src/{tag}.py"}], "summary": "1 added"} |
| 62 | |
| 63 | |
| 64 | def _sign(rec: JSONObject, key: Ed25519PrivateKey) -> str: |
| 65 | f = lambda k: str(rec.get(k) or "") |
| 66 | payload = provenance_payload( |
| 67 | f("commit_id"), author=f("author"), agent_id=f("agent_id"), model_id=f("model_id"), |
| 68 | toolchain_id=f("toolchain_id"), prompt_hash=f("prompt_hash"), committed_at=f("committed_at"), |
| 69 | ) |
| 70 | return sign_commit_ed25519(payload, key) |
| 71 | |
| 72 | |
| 73 | _Item = tuple[JSONObject, JSONObject, JSONObject] |
| 74 | |
| 75 | |
| 76 | def _record(tag: str, key: Ed25519PrivateKey | None, *, committed_at: str = AT) -> _Item: |
| 77 | """A root commit (signed by *key*, or unsigned) with its snapshot and blob.""" |
| 78 | content = f"pic-{tag}".encode() |
| 79 | oid = blob_id(content) |
| 80 | manifest, dirs = {f"src/{tag}.py": oid}, ["src"] |
| 81 | snap = hash_snapshot(manifest, dirs) |
| 82 | pub = encode_public_key(key)[1] if key else "" |
| 83 | rec: JSONObject = { |
| 84 | "branch": "dev", "message": f"pic {tag}", "author": "owner", "committed_at": committed_at, |
| 85 | "parent_commit_id": None, "parent2_commit_id": None, "snapshot_id": snap, |
| 86 | "agent_id": "agent", "model_id": "model", "toolchain_id": "", "prompt_hash": "", |
| 87 | "sem_ver_bump": "minor", "breaking_changes": [], "signer_public_key": pub, |
| 88 | "signer_key_id": public_key_fingerprint(encode_public_key(key)[0]) if key else "", |
| 89 | "structured_delta": _delta(tag), |
| 90 | } |
| 91 | rec["commit_id"] = hash_commit(parent_ids=[], snapshot_id=snap, message=rec["message"], |
| 92 | committed_at_iso=committed_at, author="owner", signer_public_key=pub) |
| 93 | rec["signature"] = _sign(rec, key) if key else "" |
| 94 | snapshot = {"snapshot_id": snap, "parent_snapshot_id": None, "delta_upsert": manifest, |
| 95 | "delta_remove": [], "directories": dirs} |
| 96 | return rec, snapshot, {"object_id": oid, "content": content} |
| 97 | |
| 98 | |
| 99 | def _bad_signature(rec: JSONObject) -> str: |
| 100 | return sign_commit_ed25519("not this payload", Ed25519PrivateKey.generate()) |
| 101 | |
| 102 | |
| 103 | def _verifies(rec: JSONObject) -> bool: |
| 104 | if not rec.get("signer_public_key"): |
| 105 | return not rec.get("signature") |
| 106 | payload = provenance_payload( |
| 107 | str(rec["commit_id"]), author=str(rec.get("author") or ""), agent_id=rec.get("agent_id") or "", |
| 108 | model_id=rec.get("model_id") or "", toolchain_id=rec.get("toolchain_id") or "", |
| 109 | prompt_hash=rec.get("prompt_hash") or "", committed_at=rec.get("committed_at") or "", |
| 110 | ) |
| 111 | try: |
| 112 | Ed25519PublicKey.from_public_bytes(decode_pubkey(str(rec["signer_public_key"]))[1]).verify( |
| 113 | decode_sig(str(rec["signature"]))[1], payload.encode()) |
| 114 | return True |
| 115 | except (InvalidSignature, ValueError): |
| 116 | return False |
| 117 | |
| 118 | |
| 119 | async def _repo(session: AsyncSession, owner: str, name: str) -> str: |
| 120 | repo = await create_repo(session, name=name, owner=owner, owner_user_id=compute_identity_id(owner.encode()), |
| 121 | visibility="public", initialize=False) |
| 122 | await session.commit() |
| 123 | return repo.repo_id |
| 124 | |
| 125 | |
| 126 | async def _push(session: AsyncSession, repo_id: str, owner: str, *items: _Item) -> JSONObject: |
| 127 | mpack = build_wire_mpack({"commits": [i[0] for i in items], "snapshots": [i[1] for i in items], |
| 128 | "blobs": [i[2] for i in items], "tags": []}) |
| 129 | key = blob_id(mpack) |
| 130 | await _backends.get_backend().put_mpack(key, mpack) |
| 131 | try: |
| 132 | return await wire_push_unpack_mpack(session, repo_id, key, pusher_id=owner, branch="dev", |
| 133 | head_commit_id=items[-1][0]["commit_id"], commits_count=len(items), |
| 134 | blobs_count=len(items), force=True) |
| 135 | except Exception: |
| 136 | await session.rollback() |
| 137 | raise |
| 138 | |
| 139 | |
| 140 | async def _served(session: AsyncSession, repo_id: str, commit_id: str) -> tuple[JSONObject | None, bytes]: |
| 141 | res = await wire_fetch_mpack(session, repo_id, want=[commit_id], have=[], force_build=True) |
| 142 | raw = await _backends.get_backend().get_mpack(str(res["mpack_id"])) |
| 143 | assert raw |
| 144 | recs = [dict(c) for c in parse_wire_mpack(raw).get("commits") or [] if c.get("commit_id") == commit_id] |
| 145 | return (recs[0] if recs else None), raw |
| 146 | |
| 147 | |
| 148 | def _client_store(root: pathlib.Path, raw: bytes, commit_id: str) -> JSONObject | None: |
| 149 | """What the real client keeps after applying a fetched mpack.""" |
| 150 | init_repo_dirs(root) |
| 151 | apply_mpack(root, parse_wire_mpack(raw)) |
| 152 | h = commit_id.split(":")[1] |
| 153 | p = root / ".muse" / "objects" / "sha256" / h[:2] / h[2:] |
| 154 | return json.loads(p.read_bytes().partition(b"\0")[2]) if p.exists() else None |
| 155 | |
| 156 | |
| 157 | async def _counts(session: AsyncSession) -> IntDict: |
| 158 | return {t: (await session.execute(text(f"SELECT count(*) FROM {t}"))).scalar_one() for t in TABLES} |
| 159 | |
| 160 | |
| 161 | _UNSET = "\x00unset" |
| 162 | |
| 163 | |
| 164 | async def _degrade( |
| 165 | session: AsyncSession, commit_id: str, *, |
| 166 | signature: str = _UNSET, message: str = _UNSET, clear_delta: bool = False, |
| 167 | ) -> None: |
| 168 | """Simulate a row stored before push verification existed.""" |
| 169 | row = await session.get(MusehubCommit, commit_id) |
| 170 | assert row is not None |
| 171 | if signature != _UNSET: |
| 172 | row.signature = signature |
| 173 | if message != _UNSET: |
| 174 | row.message = message |
| 175 | if clear_delta: |
| 176 | row.structured_delta = None |
| 177 | await session.commit() |
| 178 | session.expire_all() |
| 179 | |
| 180 | |
| 181 | async def _row(session: AsyncSession, commit_id: str) -> MusehubCommit: |
| 182 | session.expire_all() |
| 183 | row = await session.get(MusehubCommit, commit_id) |
| 184 | assert row is not None |
| 185 | return row |
| 186 | |
| 187 | |
| 188 | # ── new commits ────────────────────────────────────────────────────────────── |
| 189 | |
| 190 | @pytest.mark.asyncio |
| 191 | async def test_pic_01_valid_signed_push_round_trips(db_session: AsyncSession, tmp_path: pathlib.Path) -> None: |
| 192 | rec, snap, blob = _record("p1", Ed25519PrivateKey.generate()) |
| 193 | repo = await _repo(db_session, "owner", "pic-1") |
| 194 | result = await _push(db_session, repo, "owner", (rec, snap, blob)) |
| 195 | assert result["commits_written"] == 1 and result["divergent_commits"] == [] |
| 196 | served, raw = await _served(db_session, repo, rec["commit_id"]) |
| 197 | assert served and _verifies(served) and served["structured_delta"] == rec["structured_delta"] |
| 198 | stored = _client_store(tmp_path / "clone", raw, rec["commit_id"]) |
| 199 | assert stored and _verifies(stored) |
| 200 | |
| 201 | |
| 202 | @pytest.mark.asyncio |
| 203 | async def test_pic_02_unsigned_commits_keep_their_behaviour(db_session: AsyncSession) -> None: |
| 204 | rec, snap, blob = _record("p2", None) |
| 205 | repo = await _repo(db_session, "owner", "pic-2") |
| 206 | assert (await _push(db_session, repo, "owner", (rec, snap, blob)))["commits_written"] == 1 |
| 207 | stray, snap2, blob2 = _record("p2b", None) |
| 208 | stray["signature"] = _bad_signature(stray) |
| 209 | with pytest.raises(MPackValidationError, match="signature"): |
| 210 | await _push(db_session, repo, "owner", (stray, snap2, blob2)) |
| 211 | assert await db_session.get(MusehubCommit, stray["commit_id"]) is None |
| 212 | |
| 213 | |
| 214 | @pytest.mark.asyncio |
| 215 | @pytest.mark.parametrize("case", ["unverifiable-signature", "identity-mismatch", "malformed"]) |
| 216 | async def test_pic_03_05_refused_push_leaves_no_partial_state(db_session: AsyncSession, case: str) -> None: |
| 217 | rec, snap, blob = _record(f"p3-{case}", Ed25519PrivateKey.generate()) |
| 218 | if case == "unverifiable-signature": |
| 219 | rec["signature"] = _bad_signature(rec) |
| 220 | elif case == "identity-mismatch": |
| 221 | rec["message"] = "fields that do not hash to this commit id" |
| 222 | else: |
| 223 | rec["committed_at"] = "not a timestamp" |
| 224 | repo = await _repo(db_session, "owner", f"pic-3-{case}") |
| 225 | before = await _counts(db_session) |
| 226 | mpack = build_wire_mpack({"commits": [rec], "snapshots": [snap], "blobs": [blob], "tags": []}) |
| 227 | key = blob_id(mpack) |
| 228 | await _backends.get_backend().put_mpack(key, mpack) |
| 229 | with pytest.raises(MPackValidationError): |
| 230 | await wire_push_unpack_mpack(db_session, repo, key, pusher_id="owner", branch="dev", |
| 231 | head_commit_id=rec["commit_id"], commits_count=1, blobs_count=1, force=True) |
| 232 | await db_session.rollback() |
| 233 | assert await _counts(db_session) == before, "a refused push wrote rows" |
| 234 | assert await _backends.get_backend().get_mpack(key) is None, "a refused mpack must be quarantined" |
| 235 | |
| 236 | |
| 237 | @pytest.mark.asyncio |
| 238 | async def test_pic_06_non_utc_committed_at_is_kept_raw(db_session: AsyncSession, tmp_path: pathlib.Path) -> None: |
| 239 | rec, snap, blob = _record("p6", Ed25519PrivateKey.generate(), committed_at=AT_NON_UTC) |
| 240 | repo = await _repo(db_session, "owner", "pic-6") |
| 241 | assert (await _push(db_session, repo, "owner", (rec, snap, blob)))["commits_written"] == 1 |
| 242 | served, raw = await _served(db_session, repo, rec["commit_id"]) |
| 243 | assert served and served["committed_at"] == AT_NON_UTC and _verifies(served) |
| 244 | assert _client_store(tmp_path / "clone", raw, rec["commit_id"]) is not None |
| 245 | |
| 246 | |
| 247 | @pytest.mark.asyncio |
| 248 | async def test_pic_17_two_different_records_for_one_id_in_one_push(db_session: AsyncSession) -> None: |
| 249 | rec, snap, blob = _record("p17", Ed25519PrivateKey.generate()) |
| 250 | other = {**rec, "sem_ver_bump": "major"} |
| 251 | repo = await _repo(db_session, "owner", "pic-17") |
| 252 | with pytest.raises(MPackValidationError, match="two different records"): |
| 253 | await _push(db_session, repo, "owner", (rec, snap, blob), (other, snap, blob)) |
| 254 | |
| 255 | |
| 256 | # ── a second repository pushes the same commit first ──────────────────────── |
| 257 | |
| 258 | @pytest.mark.asyncio |
| 259 | async def test_pic_07_unverifiable_first_copy_from_another_repo_is_refused( |
| 260 | db_session: AsyncSession, tmp_path: pathlib.Path, |
| 261 | ) -> None: |
| 262 | rec, snap, blob = _record("p7", Ed25519PrivateKey.generate()) |
| 263 | degraded = {**rec, "signature": _bad_signature(rec), "structured_delta": None} |
| 264 | other = await _repo(db_session, "other", "pic-7-a") |
| 265 | owner = await _repo(db_session, "owner", "pic-7-b") |
| 266 | with pytest.raises(MPackValidationError, match="does not verify"): |
| 267 | await _push(db_session, other, "other", (degraded, snap, blob)) |
| 268 | assert (await _push(db_session, owner, "owner", (rec, snap, blob)))["commits_written"] == 1 |
| 269 | served, raw = await _served(db_session, owner, rec["commit_id"]) |
| 270 | assert served and _verifies(served) and served["structured_delta"] == rec["structured_delta"] |
| 271 | assert _client_store(tmp_path / "clone", raw, rec["commit_id"]) is not None |
| 272 | |
| 273 | |
| 274 | @pytest.mark.asyncio |
| 275 | async def test_pic_08_identity_mismatch_first_copy_from_another_repo_is_refused( |
| 276 | db_session: AsyncSession, tmp_path: pathlib.Path, |
| 277 | ) -> None: |
| 278 | rec, snap, blob = _record("p8", Ed25519PrivateKey.generate()) |
| 279 | other = await _repo(db_session, "other", "pic-8-a") |
| 280 | owner = await _repo(db_session, "owner", "pic-8-b") |
| 281 | with pytest.raises(MPackValidationError, match="does not reproduce"): |
| 282 | await _push(db_session, other, "other", ({**rec, "message": "different message"}, snap, blob)) |
| 283 | await _push(db_session, owner, "owner", (rec, snap, blob)) |
| 284 | _, raw = await _served(db_session, owner, rec["commit_id"]) |
| 285 | assert _client_store(tmp_path / "clone", raw, rec["commit_id"]) is not None |
| 286 | |
| 287 | |
| 288 | @pytest.mark.asyncio |
| 289 | async def test_pic_12_non_empty_delta_is_never_overwritten(db_session: AsyncSession) -> None: |
| 290 | rec, snap, blob = _record("p12", Ed25519PrivateKey.generate()) |
| 291 | first = {**rec, "structured_delta": _delta("first-copy")} |
| 292 | other = await _repo(db_session, "other", "pic-12-a") |
| 293 | owner = await _repo(db_session, "owner", "pic-12-b") |
| 294 | await _push(db_session, other, "other", (first, snap, blob)) |
| 295 | result = await _push(db_session, owner, "owner", (rec, snap, blob)) |
| 296 | [entry] = result["divergent_commits"] |
| 297 | assert entry["action"] == "kept-stored" and entry["fields"] == ["structured_delta"] |
| 298 | assert (await _row(db_session, rec["commit_id"])).structured_delta == _delta("first-copy") |
| 299 | |
| 300 | |
| 301 | # ── rows stored before verification existed ───────────────────────────────── |
| 302 | |
| 303 | @pytest.mark.asyncio |
| 304 | async def test_pic_09_stored_unverifiable_signature_is_healed_but_delta_is_not_filled( |
| 305 | db_session: AsyncSession, tmp_path: pathlib.Path, |
| 306 | ) -> None: |
| 307 | rec, snap, blob = _record("p9", Ed25519PrivateKey.generate()) |
| 308 | repo = await _repo(db_session, "owner", "pic-9") |
| 309 | await _push(db_session, repo, "owner", (rec, snap, blob)) |
| 310 | await _degrade(db_session, rec["commit_id"], signature=_bad_signature(rec), clear_delta=True) |
| 311 | result = await _push(db_session, repo, "owner", (rec, snap, blob)) |
| 312 | [entry] = result["divergent_commits"] |
| 313 | assert entry["action"] == "healed-signature" and entry["fields"] == ["structured_delta"] |
| 314 | row = await _row(db_session, rec["commit_id"]) |
| 315 | assert row.signature == rec["signature"] and row.structured_delta is None |
| 316 | served, raw = await _served(db_session, repo, rec["commit_id"]) |
| 317 | assert served and _verifies(served) |
| 318 | stored = _client_store(tmp_path / "clone", raw, rec["commit_id"]) |
| 319 | assert stored and _verifies(stored) |
| 320 | |
| 321 | |
| 322 | @pytest.mark.asyncio |
| 323 | async def test_pic_10_stored_identity_that_does_not_reproduce_is_healed( |
| 324 | db_session: AsyncSession, tmp_path: pathlib.Path, |
| 325 | ) -> None: |
| 326 | rec, snap, blob = _record("p10", Ed25519PrivateKey.generate()) |
| 327 | repo = await _repo(db_session, "owner", "pic-10") |
| 328 | await _push(db_session, repo, "owner", (rec, snap, blob)) |
| 329 | await _degrade(db_session, rec["commit_id"], message="stored message that does not hash to the id") |
| 330 | _, raw = await _served(db_session, repo, rec["commit_id"]) |
| 331 | assert _client_store(tmp_path / "before", raw, rec["commit_id"]) is None |
| 332 | [entry] = (await _push(db_session, repo, "owner", (rec, snap, blob)))["divergent_commits"] |
| 333 | assert entry["action"] == "healed-identity" and entry["fields"] == [] |
| 334 | _, raw = await _served(db_session, repo, rec["commit_id"]) |
| 335 | assert _client_store(tmp_path / "after", raw, rec["commit_id"]) is not None |
| 336 | |
| 337 | |
| 338 | @pytest.mark.asyncio |
| 339 | async def test_pic_11_valid_stored_record_cannot_be_degraded(db_session: AsyncSession) -> None: |
| 340 | rec, snap, blob = _record("p11", Ed25519PrivateKey.generate()) |
| 341 | owner = await _repo(db_session, "owner", "pic-11-a") |
| 342 | other = await _repo(db_session, "other", "pic-11-b") |
| 343 | await _push(db_session, owner, "owner", (rec, snap, blob)) |
| 344 | degraded = {**rec, "signature": _bad_signature(rec), "agent_id": "other-agent", "structured_delta": None} |
| 345 | result = await _push(db_session, other, "other", (degraded, snap, blob)) |
| 346 | [entry] = result["divergent_commits"] |
| 347 | assert entry["action"] == "kept-stored" and "does not verify" in entry["reason"] |
| 348 | row = await _row(db_session, rec["commit_id"]) |
| 349 | assert (row.signature, row.agent_id, row.structured_delta) == (rec["signature"], "agent", rec["structured_delta"]) |
| 350 | |
| 351 | |
| 352 | @pytest.mark.asyncio |
| 353 | async def test_pic_13_identical_duplicate_is_a_quiet_no_op(db_session: AsyncSession) -> None: |
| 354 | rec, snap, blob = _record("p13", Ed25519PrivateKey.generate()) |
| 355 | a, b = await _repo(db_session, "owner", "pic-13-a"), await _repo(db_session, "other", "pic-13-b") |
| 356 | await _push(db_session, a, "owner", (rec, snap, blob)) |
| 357 | for repo, who in ((a, "owner"), (b, "other")): |
| 358 | result = await _push(db_session, repo, who, (rec, snap, blob)) |
| 359 | assert result["commits_written"] == 0 and result["divergent_commits"] == [] |
| 360 | |
| 361 | |
| 362 | @pytest.mark.asyncio |
| 363 | async def test_pic_16_mirroring_an_existing_unverifiable_record_writes_nothing_and_reports( |
| 364 | db_session: AsyncSession, |
| 365 | ) -> None: |
| 366 | rec, snap, blob = _record("p16", Ed25519PrivateKey.generate()) |
| 367 | origin = await _repo(db_session, "owner", "pic-16-a") |
| 368 | await _push(db_session, origin, "owner", (rec, snap, blob)) |
| 369 | legacy_signature = _bad_signature(rec) |
| 370 | await _degrade(db_session, rec["commit_id"], signature=legacy_signature) |
| 371 | mirror = await _repo(db_session, "other", "pic-16-b") |
| 372 | result = await _push(db_session, mirror, "other", ({**rec, "signature": legacy_signature}, snap, blob)) |
| 373 | [entry] = result["divergent_commits"] |
| 374 | assert entry["action"] == "kept-stored" and entry["fields"] == [] and "does not verify" in entry["reason"] |
| 375 | assert (await _row(db_session, rec["commit_id"])).signature == legacy_signature |
| 376 | |
| 377 | |
| 378 | @pytest.mark.asyncio |
| 379 | async def test_pic_14_heal_invalidates_every_referencing_repos_cache(db_session: AsyncSession) -> None: |
| 380 | rec, snap, blob = _record("p14", Ed25519PrivateKey.generate()) |
| 381 | a, b = await _repo(db_session, "owner", "pic-14-a"), await _repo(db_session, "other", "pic-14-b") |
| 382 | await _push(db_session, a, "owner", (rec, snap, blob)) |
| 383 | await _push(db_session, b, "other", (rec, snap, blob)) |
| 384 | await _degrade(db_session, rec["commit_id"], signature=_bad_signature(rec)) |
| 385 | for repo in (a, b): |
| 386 | db_session.add(MusehubFetchMPackCache(cache_id=f"cache-{repo}", repo_id=repo, tip_commit_id=rec["commit_id"], |
| 387 | mpack_id=f"mpack-{repo}")) |
| 388 | await db_session.commit() |
| 389 | await _push(db_session, a, "owner", (rec, snap, blob)) |
| 390 | for repo in (a, b): |
| 391 | caches = (await db_session.execute(select(func.count()).select_from(MusehubFetchMPackCache) |
| 392 | .where(MusehubFetchMPackCache.repo_id == repo))).scalar_one() |
| 393 | jobs = (await db_session.execute(select(func.count()).select_from(MusehubBackgroundJob).where( |
| 394 | MusehubBackgroundJob.repo_id == repo, MusehubBackgroundJob.job_type == "fetch.mpack.prebuild"))).scalar_one() |
| 395 | assert caches == 0 and jobs == 1, f"repo {repo[:20]} kept a stale cache or got no prebuild" |
| 396 | |
| 397 | |
| 398 | # ── concurrency ────────────────────────────────────────────────────────────── |
| 399 | |
| 400 | @pytest.mark.asyncio |
| 401 | @pytest.mark.parametrize("other_outcome", ["commit", "rollback"]) |
| 402 | async def test_pic_15a_push_waiting_on_a_concurrent_first_write(db_session: AsyncSession, other_outcome: str) -> None: |
| 403 | """A push whose commit is being inserted by another, uncommitted transaction |
| 404 | waits for it, then reconciles (other committed) or inserts (other rolled back).""" |
| 405 | rec, snap, blob = _record(f"p15-{other_outcome}", Ed25519PrivateKey.generate()) |
| 406 | repo = await _repo(db_session, "owner", f"pic-15-{other_outcome}") |
| 407 | async with _TEST_ENGINE.connect() as other: |
| 408 | tx = await other.begin() |
| 409 | await other.execute(text( |
| 410 | "INSERT INTO musehub_commits (commit_id, branch, message, author, timestamp, committed_at_raw, parent_ids, " |
| 411 | "snapshot_id, signature, signer_public_key, signer_key_id, agent_id, model_id, toolchain_id, prompt_hash) " |
| 412 | "VALUES (:c, 'dev', :m, 'owner', :t, :raw, '{}', :s, :sig, :pub, '', 'agent', 'model', '', '')" |
| 413 | ), {"c": rec["commit_id"], "m": rec["message"], "t": datetime.datetime.fromisoformat(AT), "raw": AT, |
| 414 | "s": rec["snapshot_id"], "sig": _bad_signature(rec), "pub": rec["signer_public_key"]}) |
| 415 | async with AsyncSession(_TEST_ENGINE) as pusher: |
| 416 | task = asyncio.create_task(_push(pusher, repo, "owner", (rec, snap, blob))) |
| 417 | await asyncio.sleep(0.5) |
| 418 | assert not task.done(), "the push should be waiting on the other transaction's row" |
| 419 | await (tx.commit() if other_outcome == "commit" else tx.rollback()) |
| 420 | result = await asyncio.wait_for(task, 10) |
| 421 | row = await _row(db_session, rec["commit_id"]) |
| 422 | assert row.signature == rec["signature"], "the stored row must end up with the verified signature" |
| 423 | if other_outcome == "commit": |
| 424 | assert [e["action"] for e in result["divergent_commits"]] == ["healed-signature"] |
| 425 | else: |
| 426 | assert result["commits_written"] == 1 and result["divergent_commits"] == [] |
| 427 | |
| 428 | |
| 429 | @pytest.mark.asyncio |
| 430 | async def test_pic_15b_concurrent_pushes_of_one_commit_stay_consistent(db_session: AsyncSession) -> None: |
| 431 | """Free-running concurrent pushes of one commit: two valid variants (they |
| 432 | differ only in structured_delta) and one with an unverifiable signature. |
| 433 | |
| 434 | A push may fail on the object-row insert race that exists independently of |
| 435 | commit verification (UniqueViolation on musehub_objects); such a push rolls |
| 436 | back whole and is retried. Whatever the interleaving: one row is inserted, |
| 437 | it carries the verified signature, the losing valid variant reports the |
| 438 | divergence, and the unverifiable record is never stored. |
| 439 | """ |
| 440 | from sqlalchemy.exc import IntegrityError |
| 441 | |
| 442 | rec, snap, blob = _record("p15b", Ed25519PrivateKey.generate()) |
| 443 | variant = {**rec, "structured_delta": _delta("variant")} |
| 444 | bad = {**rec, "signature": _bad_signature(rec)} |
| 445 | repos = [await _repo(db_session, f"user{i}", f"pic-15b-{i}") for i in range(3)] |
| 446 | pushes = [(repos[0], "user0", rec), (repos[1], "user1", variant), (repos[2], "user2", bad)] |
| 447 | async with AsyncSession(_TEST_ENGINE) as s0, AsyncSession(_TEST_ENGINE) as s1, AsyncSession(_TEST_ENGINE) as s2: |
| 448 | results = list(await asyncio.gather( |
| 449 | *(_push(s, repo, who, (r, snap, blob)) for s, (repo, who, r) in zip((s0, s1, s2), pushes)), |
| 450 | return_exceptions=True, |
| 451 | )) |
| 452 | for i, res in enumerate(results): |
| 453 | if isinstance(res, IntegrityError): |
| 454 | assert "musehub_objects" in str(res) or "musehub_snapshots" in str(res), res |
| 455 | async with AsyncSession(_TEST_ENGINE) as retry: |
| 456 | repo, who, r = pushes[i] |
| 457 | try: |
| 458 | results[i] = await _push(retry, repo, who, (r, snap, blob)) |
| 459 | except MPackValidationError as exc: |
| 460 | results[i] = exc |
| 461 | valid = [r for r in results[:2] if isinstance(r, dict)] |
| 462 | assert len(valid) == 2, f"valid pushes must succeed (after a retry at most): {results[:2]}" |
| 463 | assert sum(r["commits_written"] for r in valid) == 1 |
| 464 | assert sum(len(r["divergent_commits"]) for r in valid) == 1, "the losing valid push must report the divergence" |
| 465 | row = await _row(db_session, rec["commit_id"]) |
| 466 | assert row.signature == rec["signature"] |
| 467 | assert row.structured_delta in (rec["structured_delta"], variant["structured_delta"]) |
| 468 | third = results[2] |
| 469 | assert isinstance(third, MPackValidationError) or ( |
| 470 | isinstance(third, dict) and third["commits_written"] == 0 |
| 471 | and [e["action"] for e in third["divergent_commits"]] == ["kept-stored"] |
| 472 | ), f"an unverifiable record must never be stored: {third!r}" |
File History
2 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