gabriel / musehub public
test_wire_push_fetch_contract.py python
279 lines 13.4 KB
Raw
sha256:89c7a8268be75aeb33144acf863ad3ebefe7fd3bdfd679faa768199fd789b376 merge: REST wire wave — #296 #297 #298 #299 (Tier A review … Opus 5.5 minor ⚠ breaking 1 day ago
1 """Wire push → fetch contract with the current Muse client (musehub#296, #297, #298).
2
3 The MPacks are built by the Muse client library under test (``muse.core.mpack.build_mpack`` /
4 ``build_wire_mpack``, exactly what ``muse push`` sends), pushed over HTTP to ``push/unpack-mpack``,
5 indexed, and fetched back through ``wire_fetch_mpack``: a normal push and fetch must keep working
6 while the hub enforces the signed-commit setting, keeps the commit fields it used to drop and bounds
7 every fetch request.
8 """
9 from __future__ import annotations
10
11 import datetime
12 import pathlib
13
14 import msgpack
15 import pytest
16 import pytest_asyncio
17 from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
18 from httpx import ASGITransport, AsyncClient
19 from sqlalchemy import select
20 from sqlalchemy.ext.asyncio import AsyncSession
21
22 from muse.core.commits import CommitRecord, write_commit
23 from muse.core.mpack import build_mpack, build_wire_mpack, parse_wire_mpack
24 from muse.core.object_store import write_object
25 from muse.core.paths import muse_dir
26 from muse.core.provenance import provenance_payload, sign_commit_ed25519
27 from muse.core.refs import write_branch_ref
28 from muse.core.snapshot import compute_commit_id, compute_snapshot_id
29 from muse.core.snapshots import SnapshotRecord, write_snapshot
30 from muse.core.types import blob_id, encode_pubkey, fake_id
31 from musehub.auth.request_signing import MSignContext, optional_signed_request, require_signed_request
32 from musehub.db.database import get_db
33 from musehub.db.musehub_repo_models import MusehubCommit
34 from musehub.main import app
35
36 pytestmark = pytest.mark.wire
37
38 _AUTH = MSignContext(handle="gabriel", identity_id="sha256:" + "0" * 64, is_agent=False, is_admin=True)
39 _KEY = Ed25519PrivateKey.from_private_bytes(bytes([7]) * 32) # test vector, never a real key
40
41
42 @pytest_asyncio.fixture()
43 async def client(db_session: AsyncSession): # noqa: ANN201
44 async def _db(): # noqa: ANN202
45 yield db_session
46 app.dependency_overrides[get_db] = _db
47 app.dependency_overrides[require_signed_request] = lambda: _AUTH
48 app.dependency_overrides[optional_signed_request] = lambda: _AUTH
49 async with AsyncClient(transport=ASGITransport(app=app), base_url="https://localhost:1337") as c:
50 yield c
51 app.dependency_overrides.clear()
52
53
54 @pytest_asyncio.fixture()
55 async def repo(client: AsyncClient): # noqa: ANN201
56 r = await client.post("/api/repos", json={"owner": "gabriel", "name": "wire-rest-contract", "visibility": "public",
57 "initialize": False})
58 assert r.status_code in (200, 201), r.text
59 yield r.json()
60
61
62 def _local(tmp: pathlib.Path) -> pathlib.Path:
63 dot = muse_dir(tmp)
64 dot.mkdir(parents=True)
65 (dot / "repo.json").write_text('{"repo_id":"wire-rest","owner":"gabriel"}')
66 for d in ("commits", "snapshots", "objects"):
67 (dot / d).mkdir()
68 (dot / "refs" / "heads").mkdir(parents=True)
69 (dot / "HEAD").write_text("ref: refs/heads/main\n")
70 (dot / "config.toml").write_text("")
71 return tmp
72
73
74 def _populate(root: pathlib.Path, n: int, *, signed: bool = False, seed: str = "w", **extras: object) -> list[str]:
75 """``n`` linear commits, each changing one file; signed as ``muse commit --sign`` signs when asked."""
76 pub = encode_pubkey("ed25519", _KEY.public_key().public_bytes_raw()) if signed else ""
77 parent: str | None = None
78 ts = datetime.datetime(2026, 11, 14, tzinfo=datetime.timezone.utc)
79 ids: list[str] = []
80 for i in range(n):
81 data = f"{seed}-{i}".encode() * 8
82 oid = blob_id(data)
83 write_object(root, oid, data)
84 manifest = {"README.md": oid}
85 sid = compute_snapshot_id(manifest)
86 write_snapshot(root, SnapshotRecord(snapshot_id=sid, manifest=manifest))
87 msg, at = f"{seed} commit {i}", ts.isoformat()
88 cid = compute_commit_id(parent_ids=[parent] if parent else [], snapshot_id=sid, message=msg,
89 committed_at_iso=at, author="gabriel", signer_public_key=pub)
90 sig = sign_commit_ed25519(provenance_payload(cid, author="gabriel", committed_at=at), _KEY) if signed else ""
91 write_commit(root, CommitRecord(commit_id=cid, branch="main", snapshot_id=sid, message=msg, committed_at=ts,
92 parent_commit_id=parent, author="gabriel", signature=sig, signer_public_key=pub,
93 **extras)) # type: ignore[arg-type]
94 parent = cid
95 ids.append(cid)
96 ts += datetime.timedelta(minutes=1)
97 write_branch_ref(root, "main", ids[-1])
98 return ids
99
100
101 async def _push(client: AsyncClient, slug: str, root: pathlib.Path, tip: str) -> tuple[int, str, int]:
102 import musehub.storage.backends as backends
103
104 pack = build_mpack(root, [tip], have=[])
105 wire = build_wire_mpack(pack)
106 key = blob_id(wire)
107 await backends.get_backend().put_mpack(key, wire)
108 r = await client.post(f"/gabriel/{slug}/push/unpack-mpack", content=msgpack.packb({
109 "mpack_key": key, "branch": "main", "head": tip,
110 "commits_count": len(pack.get("commits") or []), "blobs_count": len(pack.get("blobs") or []),
111 }, use_bin_type=True), headers={"Content-Type": "application/x-msgpack"})
112 return r.status_code, key, len(pack.get("blobs") or [])
113
114
115 async def _index(db_session: AsyncSession, repo_id: str, key: str, tip: str, blobs: int) -> None:
116 from musehub.core.genesis import compute_job_id
117 from musehub.db.musehub_jobs_models import MusehubBackgroundJob
118 from musehub.services.musehub_wire import process_mpack_index_job
119
120 now = datetime.datetime.now(tz=datetime.timezone.utc)
121 job_id = compute_job_id(repo_id, "mpack.index", now.isoformat())
122 db_session.add(MusehubBackgroundJob(job_id=job_id, repo_id=repo_id, job_type="mpack.index", status="pending", payload={
123 "mpack_key": key, "pusher_id": "sha256:" + "0" * 64, "branch": "main", "head": tip, "force": False,
124 "declared_objects_count": blobs}))
125 await db_session.flush()
126 await process_mpack_index_job(db_session, job_id)
127 await db_session.commit()
128
129
130 async def _fetched_commits(db_session: AsyncSession, repo_id: str, tip: str) -> list[dict]:
131 import musehub.storage.backends as backends
132 from musehub.services.musehub_wire import wire_fetch_mpack
133
134 result = await wire_fetch_mpack(db_session, repo_id, want=[tip], have=[], ttl_seconds=60, force_build=True)
135 raw = await backends.get_backend().get_mpack(result["mpack_id"])
136 assert raw is not None and blob_id(raw) == result["mpack_id"]
137 return list(parse_wire_mpack(raw)["commits"])
138
139
140 async def _stored(db_session: AsyncSession, ids: list[str]) -> set[str]:
141 return set((await db_session.execute(select(MusehubCommit.commit_id).where(MusehubCommit.commit_id.in_(ids)))).scalars())
142
143
144 # ── a normal push and fetch keep working ────────────────────────────────────
145
146
147 @pytest.mark.asyncio
148 @pytest.mark.parametrize("signed", [False, True])
149 async def test_a_normal_push_then_fetch_round_trips(
150 client: AsyncClient, repo: dict, db_session: AsyncSession, tmp_path: pathlib.Path, signed: bool
151 ) -> None:
152 root = _local(tmp_path / "r")
153 ids = _populate(root, 3, signed=signed)
154 code, key, blobs = await _push(client, repo["slug"], root, ids[-1])
155 assert code == 200
156 await _index(db_session, repo["repoId"], key, ids[-1], blobs)
157 commits = await _fetched_commits(db_session, repo["repoId"], ids[-1])
158 assert {c["commit_id"] for c in commits} == set(ids)
159 assert all(bool(c["signature"]) is signed for c in commits)
160 refs = await client.get(f"/gabriel/{repo['slug']}/refs")
161 assert refs.status_code == 200 and refs.json()["branch_heads"]["main"] == ids[-1]
162
163
164 @pytest.mark.asyncio
165 async def test_the_fetch_mpack_route_accepts_the_clients_request(client: AsyncClient, repo: dict) -> None:
166 """The exact body muse 0.2.2rc4 sends ({want, have, ttl_seconds}) is not refused by the new bounds."""
167 body = msgpack.packb({"want": [fake_id("w")], "have": [fake_id("h1"), fake_id("h2")], "ttl_seconds": 3600},
168 use_bin_type=True)
169 r = await client.post(f"/gabriel/{repo['slug']}/fetch/mpack", content=body, headers={"Content-Type": "application/x-msgpack"})
170 assert r.status_code != 422, r.text
171
172
173 # ── musehub#296: require_signed_commits ─────────────────────────────────────
174
175
176 @pytest.mark.asyncio
177 async def test_require_signed_commits_refuses_an_unsigned_push(
178 client: AsyncClient, repo: dict, db_session: AsyncSession, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
179 ) -> None:
180 import musehub.config as config
181 import musehub.storage.backends as backends
182
183 monkeypatch.setattr(config.settings, "require_signed_commits", True)
184 root = _local(tmp_path / "r")
185 ids = _populate(root, 2)
186 code, key, _ = await _push(client, repo["slug"], root, ids[-1])
187 assert code == 422
188 assert await _stored(db_session, ids) == set() # nothing written
189 assert await backends.get_backend().get_mpack(key) is None # quarantined
190
191
192 @pytest.mark.asyncio
193 async def test_require_signed_commits_accepts_a_signed_push(
194 client: AsyncClient, repo: dict, db_session: AsyncSession, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
195 ) -> None:
196 import musehub.config as config
197
198 monkeypatch.setattr(config.settings, "require_signed_commits", True)
199 root = _local(tmp_path / "r")
200 ids = _populate(root, 2, signed=True)
201 code, _, _ = await _push(client, repo["slug"], root, ids[-1])
202 assert code == 200
203 assert await _stored(db_session, ids) == set(ids)
204
205
206 @pytest.mark.asyncio
207 async def test_unsigned_pushes_are_accepted_when_the_setting_is_off(
208 client: AsyncClient, repo: dict, db_session: AsyncSession, tmp_path: pathlib.Path
209 ) -> None:
210 import musehub.config as config
211
212 assert config.settings.require_signed_commits is False
213 root = _local(tmp_path / "r")
214 ids = _populate(root, 2)
215 code, _, _ = await _push(client, repo["slug"], root, ids[-1])
216 assert code == 200 and await _stored(db_session, ids) == set(ids)
217
218
219 # ── musehub#297: commit fields survive push → fetch ─────────────────────────
220
221
222 @pytest.mark.asyncio
223 async def test_reviewed_by_test_runs_and_format_version_survive_a_round_trip(
224 client: AsyncClient, repo: dict, db_session: AsyncSession, tmp_path: pathlib.Path
225 ) -> None:
226 from musehub.models.wire import WIRE_COMMIT_FORMAT_VERSION, WireCommit
227
228 root = _local(tmp_path / "r")
229 ids = _populate(root, 2, reviewed_by=["alice", "bob"], test_runs=5)
230 code, key, blobs = await _push(client, repo["slug"], root, ids[-1])
231 assert code == 200
232 await _index(db_session, repo["repoId"], key, ids[-1], blobs)
233 commits = await _fetched_commits(db_session, repo["repoId"], ids[-1])
234 assert commits and all(c["reviewed_by"] == ["alice", "bob"] and c["test_runs"] == 5 for c in commits)
235 assert all(c["format_version"] == WIRE_COMMIT_FORMAT_VERSION == 8 for c in commits)
236 assert WireCommit(commit_id=ids[0]).format_version == 8 # one value: model default and serve path
237
238
239 @pytest.mark.asyncio
240 async def test_commit_metadata_survives_a_round_trip(
241 client: AsyncClient, repo: dict, db_session: AsyncSession, tmp_path: pathlib.Path
242 ) -> None:
243 root = _local(tmp_path / "r")
244 ids = _populate(root, 1, metadata={"k": "v"})
245 code, key, blobs = await _push(client, repo["slug"], root, ids[-1])
246 assert code == 200
247 await _index(db_session, repo["repoId"], key, ids[-1], blobs)
248 commits = await _fetched_commits(db_session, repo["repoId"], ids[-1])
249 assert commits[0]["metadata"] == {"k": "v"}
250
251
252 # ── musehub#298: fetch request bounds ───────────────────────────────────────
253
254
255 @pytest.mark.asyncio
256 @pytest.mark.parametrize("route", ["fetch", "fetch/mpack", "fetch/presign"])
257 async def test_every_fetch_route_refuses_more_than_10000_wants(client: AsyncClient, repo: dict, route: str) -> None:
258 body = {"want": [fake_id(f"w-{i}") for i in range(10_001)], "have": []}
259 r = await client.post(f"/gabriel/{repo['slug']}/{route}", content=msgpack.packb(body, use_bin_type=True),
260 headers={"Content-Type": "application/x-msgpack"})
261 assert r.status_code == 422, (route, r.status_code)
262
263
264 @pytest.mark.asyncio
265 @pytest.mark.parametrize("route", ["fetch", "fetch/mpack", "fetch/presign"])
266 async def test_a_large_have_is_trimmed_not_refused(client: AsyncClient, repo: dict, route: str) -> None:
267 """Review F1: the client sends every local and remote-tracking tip as have; a repo with >1,000 branches must still fetch."""
268 body = {"want": [fake_id("w")], "have": [fake_id(f"h-{i}") for i in range(2_500)]}
269 r = await client.post(f"/gabriel/{repo['slug']}/{route}", content=msgpack.packb(body, use_bin_type=True),
270 headers={"Content-Type": "application/x-msgpack"})
271 assert r.status_code != 422, (route, r.status_code, r.text[:200])
272
273
274 @pytest.mark.asyncio
275 @pytest.mark.parametrize("route", ["fetch", "fetch/mpack", "fetch/presign"])
276 async def test_depth_is_refused_rather_than_ignored(client: AsyncClient, repo: dict, route: str) -> None:
277 body = msgpack.packb({"want": [fake_id("w")], "have": [], "depth": 1}, use_bin_type=True)
278 r = await client.post(f"/gabriel/{repo['slug']}/{route}", content=body, headers={"Content-Type": "application/x-msgpack"})
279 assert r.status_code == 422 and "depth" in r.text
File History 4 commits
sha256:89c7a8268be75aeb33144acf863ad3ebefe7fd3bdfd679faa768199fd789b376 merge: REST wire wave — #296 #297 #298 #299 (Tier A review … Opus 5.5 minor ⚠ 1 day ago
sha256:0425c7ed0698f5efc5b053148f82fd8764d6895fa27e9dd6e331d55786914085 fix(wire): review of #296-#299 — trim have instead of refus… Opus 5.5 patch 1 day ago
sha256:bcf1d3c0486f7022d47cd24eed38fad1b1c50879b58cfe173231d76f0c9a1432 feat(wire): keep each pushed commit's metadata — commit_met… Opus 5.5 minor ⚠ 1 day ago
sha256:104a1e95e9b0515cbec3bc4fd4b7ad1f89b7312ea0c1bd1b3ba16fe014af07a8 fix(wire): enforce require_signed_commits, keep reviewed_by… Opus 5.5 minor ⚠ 2 days ago