test_wire.py
python
sha256:89c7a8268be75aeb33144acf863ad3ebefe7fd3bdfd679faa768199fd789b376
merge: REST wire wave — #296 #297 #298 #299 (Tier A review …
Opus 5.5
minor
⚠ breaking
1 day ago
| 1 | """DOC_WIRE — /muse/wire contract (musehub#254, QA contract v2). |
| 2 | |
| 3 | Every claim on the page was checked on 2026-11-07 against Muse 0.2.1rc20 (muse ``dev`` 5becda10) in a |
| 4 | disposable sandbox and against the MuseHub source that renders the page. These tests keep the |
| 5 | corrected page honest in both directions: they build a real push MPack exactly as ``muse push`` does, |
| 6 | run the page's own verification and reconstruction snippets against it, run the real ``muse |
| 7 | format-patch`` / ``apply-patch`` in throwaway repositories (offline, a temporary HOME, no identity, no |
| 8 | remotes, no git), and compare the page's endpoint and limit tables with the routes and settings. |
| 9 | Known product defects stay documented as contract plus warning (#241 defect-preservation rule): each |
| 10 | is a strict xfail against its ticket, pinned to the behaviour the contract wants. |
| 11 | |
| 12 | * DOC_WIRE_01/02/03 — CLI parse, anchors, loopback URLs: the shared checkers (``test_doc_inf_*``). |
| 13 | * DOC_WIRE_04 — executable integrity contract: the page's MPack verification, fetch and snapshot |
| 14 | reconstruction snippets run against a real MPack; the MPack layout figure matches it. |
| 15 | * DOC_WIRE_05 — the endpoint table equals the wire router (method, path, auth, rate limit); the limits |
| 16 | table equals the settings defaults. |
| 17 | * DOC_WIRE_06 — MPatch format and ``apply-patch`` behaviour, executed. |
| 18 | * DOC_WIRE_07 — hub verifiers and the fixed defects pinned (#296, #297, #298, #299). muse#208 and muse#209 are fixed. |
| 19 | |
| 20 | Rendering is the shared in-process fixture (no database). No test talks to a network service. |
| 21 | """ |
| 22 | |
| 23 | from __future__ import annotations |
| 24 | |
| 25 | import asyncio |
| 26 | import base64 |
| 27 | import hashlib |
| 28 | import html as html_lib |
| 29 | import inspect |
| 30 | import json |
| 31 | import os |
| 32 | import pathlib |
| 33 | import re |
| 34 | import struct |
| 35 | import subprocess |
| 36 | import sys |
| 37 | import types |
| 38 | from dataclasses import dataclass, field |
| 39 | |
| 40 | import pytest |
| 41 | |
| 42 | from tests.docs_contract import registry_check as rc |
| 43 | |
| 44 | PAGE = rc.phase_path("wire") |
| 45 | MUSE_ISSUES = "https://staging.musehub.ai/gabriel/muse/issues/" |
| 46 | HUB_ISSUES = "https://staging.musehub.ai/gabriel/musehub/issues/" |
| 47 | |
| 48 | # Product tickets the page's warnings link to (filed under #254, assigned to gabriel). |
| 49 | _T_EXIT = "209" # gabriel/muse: format-patch/apply-patch I/O errors crashed (ExitCode.IO_ERROR missing) — fixed; the page still links it |
| 50 | _MUSE_LINKS = {_T_EXIT} |
| 51 | _HUB_LINKS = {"233", "52"} |
| 52 | |
| 53 | XFAIL = dict(strict=True, raises=AssertionError) |
| 54 | |
| 55 | |
| 56 | # ── helpers ─────────────────────────────────────────────────────────────────── |
| 57 | |
| 58 | @pytest.fixture(scope="module") |
| 59 | def html(rendered: dict[str, str]) -> str: |
| 60 | return rendered[PAGE] |
| 61 | |
| 62 | |
| 63 | def _plain(fragment: str) -> str: |
| 64 | return html_lib.unescape(re.sub(r"<[^>]+>", "", fragment)) |
| 65 | |
| 66 | |
| 67 | def _blocks(page: str) -> list[str]: |
| 68 | return [_plain(b) for b in re.findall(r"<pre>(.*?)</pre>", page, re.S)] |
| 69 | |
| 70 | |
| 71 | def _block(page: str, marker: str) -> str: |
| 72 | found = [b for b in _blocks(page) if marker in b] |
| 73 | assert len(found) == 1, (marker, len(found)) |
| 74 | return found[0] |
| 75 | |
| 76 | |
| 77 | def _section(page: str, anchor: str) -> str: |
| 78 | m = re.search(rf'<section class="devdocs-section" id="{anchor}">(.*?)</section>', page, re.S) |
| 79 | assert m, anchor |
| 80 | return m.group(1) |
| 81 | |
| 82 | |
| 83 | def _table_rows(fragment: str) -> list[list[str]]: |
| 84 | body = re.search(r"<tbody>(.*?)</tbody>", fragment, re.S) |
| 85 | assert body |
| 86 | return [[_plain(c).strip() for c in re.findall(r"<td[^>]*>(.*?)</td>", row, re.S)] |
| 87 | for row in re.findall(r"<tr>(.*?)</tr>", body.group(1), re.S)] |
| 88 | |
| 89 | |
| 90 | @dataclass |
| 91 | class R: |
| 92 | rc: int |
| 93 | out: str |
| 94 | err: str |
| 95 | json: dict = field(default_factory=dict) |
| 96 | |
| 97 | |
| 98 | class Sandbox: |
| 99 | def __init__(self, root: pathlib.Path) -> None: |
| 100 | self.root = root |
| 101 | self.home = root / "home" |
| 102 | self.home.mkdir(parents=True) |
| 103 | env = {k: v for k, v in os.environ.items() if not k.startswith(("MUSE_", "GIT_"))} |
| 104 | env.update(HOME=str(self.home), XDG_CONFIG_HOME=str(self.home / ".config"), MUSE_KEYCHAIN_BACKEND="disabled", |
| 105 | PYTHON_KEYRING_BACKEND="keyring.backends.fail.Keyring", NO_COLOR="1", |
| 106 | PYTHONPATH=os.pathsep.join(p for p in sys.path if p)) |
| 107 | self.env = env |
| 108 | |
| 109 | def run(self, repo: pathlib.Path, *args: str, ok: bool = True) -> R: |
| 110 | assert str(repo.resolve()).startswith(str(self.root.resolve())), repo |
| 111 | p = subprocess.run([sys.executable, "-c", "import sys; from muse.cli.app import main; main(sys.argv[1:])", *args], |
| 112 | cwd=repo, env=self.env, capture_output=True, text=True, timeout=120) |
| 113 | if ok: |
| 114 | assert p.returncode == 0, (args, p.stdout, p.stderr) |
| 115 | try: |
| 116 | out = json.loads(p.stdout) |
| 117 | except ValueError: |
| 118 | out = {} |
| 119 | return R(p.returncode, p.stdout, p.stderr, out if isinstance(out, dict) else {}) |
| 120 | |
| 121 | def write(self, repo: pathlib.Path, files: dict[str, str | None]) -> None: |
| 122 | for rel, text in files.items(): |
| 123 | path = repo / rel |
| 124 | if text is None: |
| 125 | path.unlink() |
| 126 | continue |
| 127 | path.parent.mkdir(parents=True, exist_ok=True) |
| 128 | path.write_text(text) |
| 129 | |
| 130 | def repo(self, name: str, base: dict[str, str]) -> pathlib.Path: |
| 131 | repo = self.root / name |
| 132 | repo.mkdir() |
| 133 | self.run(repo, "init") |
| 134 | self.write(repo, base) |
| 135 | self.run(repo, "code", "add", ".") |
| 136 | self.run(repo, "commit", "-m", "base") |
| 137 | return repo |
| 138 | |
| 139 | |
| 140 | _BASE = {"src/core.py": "def core():\n return 1\n", "docs/old.md": "old notes\n", "README.md": "# A\n"} |
| 141 | _CHANGE = {"src/core.py": "def core():\n return 2\n", "docs/old.md": None, "test_new.py": "def test_new():\n assert True\n"} |
| 142 | |
| 143 | |
| 144 | @pytest.fixture(scope="module") |
| 145 | def world(tmp_path_factory: pytest.TempPathFactory) -> dict: |
| 146 | sb = Sandbox(tmp_path_factory.mktemp("wire254")) |
| 147 | repo = sb.repo("a", _BASE) |
| 148 | sb.write(repo, _CHANGE) |
| 149 | sb.run(repo, "code", "add", ".") |
| 150 | sb.run(repo, "commit", "-m", "modify, delete, add") |
| 151 | head = sb.run(repo, "rev-parse", "HEAD").out.strip() |
| 152 | out = sb.root / "patches" |
| 153 | out.mkdir() # must exist (gabriel/muse#209) |
| 154 | sb.run(repo, "format-patch", "HEAD", "--output-dir", str(out)) |
| 155 | (patch,) = sorted(out.glob("*.mpatch")) |
| 156 | return {"sb": sb, "repo": repo, "head": head, "patch": patch} |
| 157 | |
| 158 | |
| 159 | @pytest.fixture(scope="module") |
| 160 | def mpack(world: dict) -> bytes: |
| 161 | """The push MPack, built exactly as ``muse push`` builds it (push.py ``_run_mpack_path``).""" |
| 162 | from muse.core.mpack import build_mpack_from_walk, build_wire_mpack, walk_commits |
| 163 | walk = walk_commits(world["repo"], [world["head"]], have=[]) |
| 164 | return build_wire_mpack(build_mpack_from_walk(world["repo"], walk, compress=True)) |
| 165 | |
| 166 | |
| 167 | # ── DOC_WIRE_04 — executable integrity contract ─────────────────────────────── |
| 168 | |
| 169 | class TestIntegrityContract: |
| 170 | def test_page_verification_snippet_accepts_a_real_mpack(self, html: str, mpack: bytes) -> None: |
| 171 | from muse.core.types import blob_id |
| 172 | snippet = _block(html, "required before apply_mpack()") |
| 173 | exec(compile(snippet, "<page:integrity-model>", "exec"), {"mpack_bytes": mpack, "mpack_id": blob_id(mpack)}) |
| 174 | |
| 175 | def test_page_snippet_rejects_the_old_footer_formula(self, html: str, mpack: bytes) -> None: |
| 176 | snippet = _block(html, "required before apply_mpack()") |
| 177 | with pytest.raises(AssertionError): |
| 178 | exec(compile(snippet, "<page:integrity-model>", "exec"), {"mpack_bytes": mpack, "mpack_id": "sha256:" + mpack[-32:].hex()}) |
| 179 | assert 'footer.hex()' not in html and "SHA-256 of the binary (excluding" not in html |
| 180 | |
| 181 | def test_blob_id_formula_in_the_comment_is_exact(self, html: str, mpack: bytes) -> None: |
| 182 | from muse.core.types import blob_id |
| 183 | line = next(ln for ln in _block(html, "required before apply_mpack()").splitlines() if "hexdigest()" in ln) |
| 184 | expr = line.lstrip("# ").strip() |
| 185 | assert eval(expr, {"hashlib": hashlib, "mpack_bytes": mpack}) == blob_id(mpack) |
| 186 | |
| 187 | def test_client_and_server_use_the_whole_file_blob_id(self, mpack: bytes) -> None: |
| 188 | from muse.core.mpack import build_presign_payload |
| 189 | from muse.core.types import blob_id |
| 190 | key = build_presign_payload(mpack)["mpack_key"] |
| 191 | assert key == blob_id(mpack) and len(key) == 71 |
| 192 | assert key != "sha256:" + mpack[-32:].hex() |
| 193 | src = inspect.getsource(__import__("musehub.services.musehub_wire_push", fromlist=["x"]).wire_push_unpack_mpack) |
| 194 | assert "actual_id = blob_id(wire_bytes)" in src |
| 195 | |
| 196 | def test_fetch_snippet_runs(self, html: str, mpack: bytes) -> None: |
| 197 | from muse.core.mpack import parse_wire_mpack |
| 198 | from muse.core.types import blob_id |
| 199 | block = _block(html, "verify before applying") |
| 200 | code = block.split("# verify before applying", 1)[1] |
| 201 | applied: list = [] |
| 202 | ns = {"response": types.SimpleNamespace(content=mpack), "mpack_id": blob_id(mpack), "blob_id": blob_id, |
| 203 | "parse_wire_mpack": parse_wire_mpack, "apply_mpack": lambda root, pack: applied.append(pack), "repo_root": None} |
| 204 | exec(compile(code, "<page:fetch-step-2>", "exec"), ns) |
| 205 | assert len(applied) == 1 and applied[0]["commits"] |
| 206 | |
| 207 | def test_single_bit_corruption_is_refused(self, mpack: bytes) -> None: |
| 208 | from muse.core.mpack import parse_wire_mpack |
| 209 | bad = bytearray(mpack) |
| 210 | bad[len(bad) // 2] ^= 1 |
| 211 | with pytest.raises(OSError): |
| 212 | parse_wire_mpack(bytes(bad)) |
| 213 | |
| 214 | def test_not_msgpack(self, mpack: bytes) -> None: |
| 215 | import msgpack |
| 216 | with pytest.raises(Exception): |
| 217 | msgpack.unpackb(mpack) |
| 218 | |
| 219 | def test_header_layout_matches_page(self, html: str, mpack: bytes) -> None: |
| 220 | assert mpack[:4] == b"MUSE" and mpack[4] == 1 and mpack[5] == 5 |
| 221 | table = [struct.unpack_from("<BQQ", mpack, 6 + 17 * i) for i in range(5)] |
| 222 | assert [t[0] for t in table] == [1, 2, 3, 4, 5] |
| 223 | assert table[0][1] == 0x5B == 6 + 5 * 17 |
| 224 | assert hashlib.sha256(mpack[:-32]).digest() == mpack[-32:] |
| 225 | layout = _block(html, "section table (5 × 17 = 85 bytes)") |
| 226 | for off, sec in (("0x06", "type 1 (OBJECTS)"), ("0x17", "type 2 (COMMITS)"), ("0x28", "type 3 (SNAPSHOTS)"), |
| 227 | ("0x39", "type 4 (TAGS)"), ("0x4a", "type 5 (META)")): |
| 228 | assert re.search(rf"{off}\s+0\d\s.*{re.escape(sec)}", layout), off |
| 229 | assert "0x5b <OBJECTS bytes>" in layout |
| 230 | |
| 231 | def test_objects_section_is_a_raw_pack_and_meta_is_empty(self, html: str, mpack: bytes) -> None: |
| 232 | from muse.core.mpack import parse_wire_mpack |
| 233 | from muse.core.types import blob_id |
| 234 | table = {t: (o, n) for t, o, n in (struct.unpack_from("<BQQ", mpack, 6 + 17 * i) for i in range(5))} |
| 235 | off, n = table[1] |
| 236 | objects = mpack[off:off + n] |
| 237 | count = struct.unpack_from("<Q", objects, 5)[0] |
| 238 | assert objects[:5] == b"MUSE\x01" and hashlib.sha256(objects[:-32]).digest() == objects[-32:] |
| 239 | assert bytes.fromhex("28b52ffd") not in objects # no zstd frame: objects are stored raw |
| 240 | parsed = parse_wire_mpack(mpack) |
| 241 | assert count == len(parsed["blobs"]) and all(blob_id(b["content"]) == b["object_id"] for b in parsed["blobs"]) |
| 242 | assert table[5][1] == 0 and "meta" not in parsed |
| 243 | rows = {r[1]: r[2] for r in _table_rows(_section(html, "mpack-format"))} |
| 244 | assert "raw (uncompressed)" in rows["OBJECTS"] and "zstd-compressed, keyed" not in html |
| 245 | assert "length 0" in rows["META"] and "base_commits" not in html |
| 246 | |
| 247 | def test_figure_matches_a_rebuilt_mpack(self, html: str, mpack: bytes) -> None: |
| 248 | fig = re.search(r'<figure class="docfig" id="mpack-layout-figure".*?</figure>', html, re.S) |
| 249 | assert fig, "mpack-layout-figure missing" |
| 250 | f = fig.group(0) |
| 251 | assert 'aria-labelledby="mpack-layout-figure-title"' in f |
| 252 | rows = re.findall(r'<li class="mpkfig__row[^"]*">(.*?)</li>', f, re.S) |
| 253 | cells = [(_plain(re.search(r'mpkfig__off">(.*?)<', r).group(1)), _plain(re.search(r'mpkfig__name">(.*?)</span>', r, re.S).group(1)), |
| 254 | _plain(re.search(r'mpkfig__size">(.*?)<', r).group(1)), _plain(re.search(r'mpkfig__cov">(.*?)</span>\s*</span>', r, re.S).group(1))) |
| 255 | for r in rows] |
| 256 | assert [c[:3] for c in cells[:4]] == [("0x00", 'magic b"MUSE"', "4 B"), ("0x04", "version 1", "1 B"), |
| 257 | ("0x05", "section_count 5", "1 B"), ("0x06", "section table, 5 × 17 B", "85 B")] |
| 258 | assert cells[4][0] == "0x5b" and cells[-1][2] == "32 B" |
| 259 | sizes = [int(c[2].split()[0].replace(",", "")) for c in cells] |
| 260 | total = sum(sizes) |
| 261 | offs = [int(c[0], 16) for c in cells] |
| 262 | assert all(offs[i] + sizes[i] == offs[i + 1] for i in range(len(cells) - 1)), "figure offsets do not chain" |
| 263 | assert offs[-1] == total - 32 |
| 264 | eqs = _plain(re.search(r'<dl class="mpkfig__eqs">(.*?)</dl>', f, re.S).group(1)) |
| 265 | assert f'"blob {total}\\0"' in eqs and f"{total:,} bytes" in eqs and f"{total - 32:,} bytes" in eqs |
| 266 | assert f"0x{total - 33:x}" in eqs |
| 267 | assert all("in mpack_id" in c[3] for c in cells) |
| 268 | assert ["not in footer hash" in c[3] for c in cells] == [False] * (len(cells) - 1) + [True] |
| 269 | # The fixed regions match any MPack the client builds; the example sizes are this fixture's shape. |
| 270 | table = [struct.unpack_from("<BQQ", mpack, 6 + 17 * i) for i in range(5)] |
| 271 | assert [t[0] for t in table] == [1, 2, 3, 4, 5] and table[3][2] == 8 and table[4][2] == 0 |
| 272 | assert sizes[7] == 8 and sizes[8] == 0 |
| 273 | |
| 274 | def test_snapshot_reconstruction_snippet_runs(self, html: str, mpack: bytes) -> None: |
| 275 | from muse.core.mpack import parse_wire_mpack |
| 276 | snaps = parse_wire_mpack(mpack)["snapshots"] |
| 277 | assert {frozenset(s) for s in snaps} == {frozenset({"snapshot_id", "parent_snapshot_id", "delta_upsert", "delta_remove", "directories"})} |
| 278 | assert snaps[0]["parent_snapshot_id"] is None and snaps[1]["delta_remove"] == ["docs/old.md"] |
| 279 | ns = {"snapshots": snaps} |
| 280 | exec(compile(_block(html, "hash IS the proof"), "<page:reconstruction>", "exec"), ns) |
| 281 | assert set(ns["resolved"]) == {s["snapshot_id"] for s in snaps} |
| 282 | assert "compute_snapshot_id" not in html and "10–100×" not in html |
| 283 | |
| 284 | def test_push_commit_records_carry_no_format_version(self, html: str, mpack: bytes) -> None: |
| 285 | from muse.core.mpack import parse_wire_mpack |
| 286 | commits = parse_wire_mpack(mpack)["commits"] |
| 287 | assert commits and all("format_version" not in c for c in commits) |
| 288 | assert "currently <code>8</code>" not in html and "does not send it" in html |
| 289 | |
| 290 | |
| 291 | # ── DOC_WIRE_05 — endpoints and limits ──────────────────────────────────────── |
| 292 | |
| 293 | def _routes() -> dict[tuple[str, str], tuple[str, str]]: |
| 294 | import musehub.api.routes.wire as wire |
| 295 | import musehub.rate_limits as limits |
| 296 | src = inspect.getsource(wire) |
| 297 | out = {} |
| 298 | for m in re.finditer(r'@router\.(get|post|delete)\(\s*"([^"]+)".*?@limiter\.limit\((\w+)\)\s*async def \w+\((.*?)\)\s*->', src, re.S): |
| 299 | method, path, limit, params = m.groups() |
| 300 | path = re.sub(r"\{(\w+?)(?:_name)?(?::path)?\}", r"{\1}", path) |
| 301 | auth = "Required" if "require_valid_token" in params else "Optional" |
| 302 | out[(method.upper(), path)] = (auth, getattr(limits, limit).replace("/minute", "/min")) |
| 303 | return out |
| 304 | |
| 305 | |
| 306 | def test_endpoint_table_matches_the_wire_router(html: str) -> None: |
| 307 | import musehub.api.routes.wire as wire |
| 308 | routes = _routes() |
| 309 | mounted = {(m, re.sub(r"\{(\w+?)(?:_name)?(?::path)?\}", r"{\1}", r.path)) for r in wire.router.routes for m in r.methods} |
| 310 | assert set(routes) == mounted and len(routes) == 22 # the source scan saw every mounted route |
| 311 | table = {(r[0], r[1]): (r[3], r[4]) for r in _table_rows(_section(html, "endpoints"))} |
| 312 | assert set(table) == set(routes), (set(routes) - set(table), set(table) - set(routes)) |
| 313 | assert {k: v for k, v in table.items() if v != routes[k]} == {} |
| 314 | |
| 315 | |
| 316 | def test_limits_table_matches_settings_defaults(html: str) -> None: |
| 317 | from musehub.config import Settings |
| 318 | import musehub.rate_limits as limits |
| 319 | d = {k: Settings.model_fields[k].default for k in ("mpack_max_bytes", "mpack_max_commits", "mpack_max_objects", |
| 320 | "mpack_max_decompressed_bytes", "mpack_daily_upload_limit_bytes", |
| 321 | "async_push_blob_threshold")} |
| 322 | assert d == {"mpack_max_bytes": 512 * 1024 ** 2, "mpack_max_commits": 100_000, "mpack_max_objects": 1_000_000, |
| 323 | "mpack_max_decompressed_bytes": 4 * 1024 ** 3, "mpack_daily_upload_limit_bytes": 50 * 1024 ** 3, |
| 324 | "async_push_blob_threshold": 200} |
| 325 | rows = {r[0]: r[1] for r in _table_rows(_section(html, "limits"))} |
| 326 | assert rows["MPack size"].startswith("512 MB") |
| 327 | assert rows["Commits / objects per push"].startswith("100,000 / 1,000,000") |
| 328 | assert rows["Decompressed object bytes per push"].startswith("4 GB") |
| 329 | assert rows["Daily upload bytes per identity"].startswith("50 GB") |
| 330 | assert rows["Synchronous unpack threshold"].startswith("200 blobs") |
| 331 | assert (limits.WIRE_PUSH_LIMIT, limits.WIRE_FETCH_LIMIT, limits.OBJECT_LIMIT, limits.REPAIR_LIMIT) == \ |
| 332 | ("30/minute", "120/minute", "120/minute", "2000/minute") |
| 333 | assert "30 requests / minute / IP" in rows["Push rate limit (unpack-mpack, releases, tags, branch delete, logs)"] |
| 334 | assert "R2_BUCKET" not in html and "BLOB_STORAGE_BUCKET" in html |
| 335 | |
| 336 | |
| 337 | def test_fetch_response_shapes_match_the_handlers(html: str) -> None: |
| 338 | import musehub.services.musehub_wire_fetch as f |
| 339 | src = inspect.getsource(f.wire_fetch_mpack) + inspect.getsource(f.wire_fetch_presign) |
| 340 | for key in ('"blob_count"', '"blob_urls"', '"mpack_id"', '"mpack_url"', '"commit_count"'): |
| 341 | assert key in src |
| 342 | assert '"object_count"' not in src and '"object_urls"' not in src |
| 343 | fetch = _block(html, "POST /{owner}/{slug}/fetch/mpack\nContent-Type") |
| 344 | presign = _block(html, "POST /{owner}/{slug}/fetch/presign\nContent-Type") |
| 345 | assert '"blob_count"' in fetch and '"object_count"' not in fetch |
| 346 | assert '"blob_urls"' in presign and '"object_urls"' not in presign |
| 347 | assert "Alias for" not in html |
| 348 | |
| 349 | |
| 350 | def test_known_issue_links(html: str) -> None: |
| 351 | muse = set(re.findall(re.escape(MUSE_ISSUES) + r"(\d+)", html)) |
| 352 | hub = set(re.findall(re.escape(HUB_ISSUES) + r"(\d+)", html)) |
| 353 | assert muse == _MUSE_LINKS and hub == _HUB_LINKS, (muse, hub) |
| 354 | known = _section(html, "known-issues") |
| 355 | assert "<tbody>" not in known and "No open defects are known for this page." in known |
| 356 | |
| 357 | |
| 358 | # ── DOC_WIRE_06 — MPatch, executed ──────────────────────────────────────────── |
| 359 | |
| 360 | class TestMPatch: |
| 361 | def test_schema_matches_page(self, html: str, world: dict) -> None: |
| 362 | from muse.core.types import blob_id |
| 363 | d = json.loads(world["patch"].read_text()) |
| 364 | documented = set(re.findall(r'"(\w+)":', _block(html, '"patch_id": "sha256:'))) |
| 365 | top = documented - {"requires_snapshot", "independent_dimensions", "conflict_free"} |
| 366 | assert top <= set(d) and set(d) - top == set(), set(d) ^ top |
| 367 | assert d["applicability"]["conflict_free"] is True |
| 368 | ops = {op["op"]: set(op) for op in d["ops"]} |
| 369 | rows = {r[0].strip('"'): {c.strip() for c in r[1].split(",")} for r in _table_rows(html.split("<h3>Op types</h3>", 1)[1])} |
| 370 | for op, keys in ops.items(): |
| 371 | assert keys == rows[op] | {"op", "position", "action_label"}, (op, keys) |
| 372 | for oid, b64 in d["blobs"].items(): |
| 373 | assert blob_id(base64.b64decode(b64, validate=True)) == oid |
| 374 | assert set(d["blobs"]) == set(d["required_objects"]) |
| 375 | assert "base64url" not in _section(html, "mpatch-format") |
| 376 | |
| 377 | def test_apply_patch_flags_and_page_commands(self, html: str, world: dict) -> None: |
| 378 | help_out = world["sb"].run(world["repo"], "apply-patch", "--help").out |
| 379 | flags = set(re.findall(r"\[(--[a-z-]+)\]", help_out.split("\n\n", 1)[0])) |
| 380 | assert flags == {"--dry-run", "--check", "--force", "--json"} |
| 381 | cmds = [ln.strip() for ln in _block(html, "check — report applicability only").splitlines() if ln.strip().startswith("muse apply-patch")] |
| 382 | assert cmds and not any("--commit" in c for c in cmds) |
| 383 | |
| 384 | def test_apply_offline_from_inline_blobs_writes_unstaged(self, world: dict) -> None: |
| 385 | sb = world["sb"] |
| 386 | repo = sb.repo("b", _BASE) |
| 387 | assert sb.run(repo, "apply-patch", str(world["patch"]), "--check", "--json").json["applicable"] is True |
| 388 | dry = sb.run(repo, "apply-patch", str(world["patch"]), "--dry-run", "--json").json |
| 389 | assert dry["dry_run"] is True and (repo / "docs/old.md").exists() |
| 390 | sb.run(repo, "apply-patch", str(world["patch"]), "--json") |
| 391 | assert (repo / "src/core.py").read_text() == _CHANGE["src/core.py"] and not (repo / "docs/old.md").exists() |
| 392 | status = sb.run(repo, "status", "--short").out |
| 393 | assert "?? test_new.py" in status # nothing staged: the new file is untracked |
| 394 | |
| 395 | def test_patch_id_and_snapshot_guards(self, world: dict) -> None: |
| 396 | sb = world["sb"] |
| 397 | d = json.loads(world["patch"].read_text()) |
| 398 | d["summary"] = "tampered" |
| 399 | bad = sb.root / "tampered.mpatch" |
| 400 | bad.write_text(json.dumps(d)) |
| 401 | assert sb.run(world["repo"], "apply-patch", str(bad), ok=False).rc == 1 |
| 402 | r = sb.run(world["repo"], "apply-patch", str(world["patch"]), ok=False) # HEAD is past the base snapshot |
| 403 | assert r.rc == 1 and "requires_snapshot" in r.err |
| 404 | |
| 405 | def test_uncommitted_edit_is_not_overwritten(self, world: dict) -> None: |
| 406 | sb = world["sb"] |
| 407 | repo = sb.repo("c", _BASE) |
| 408 | sb.write(repo, {"src/core.py": "def core():\n return 999 # local work\n"}) |
| 409 | r = sb.run(repo, "apply-patch", str(world["patch"]), ok=False) |
| 410 | assert r.rc != 0 and "999" in (repo / "src/core.py").read_text() |
| 411 | |
| 412 | def test_missing_output_dir_exits_3_without_traceback(self, world: dict) -> None: |
| 413 | r = world["sb"].run(world["repo"], "format-patch", "HEAD", "--output-dir", str(world["sb"].root / "absent"), ok=False) |
| 414 | assert r.rc == 3 and "Traceback" not in r.err |
| 415 | |
| 416 | def test_check_exits_1_when_not_applicable(self, world: dict) -> None: |
| 417 | assert world["sb"].run(world["repo"], "apply-patch", str(world["patch"]), "--check", ok=False).rc == 1 |
| 418 | |
| 419 | def test_dry_run_refuses_exactly_when_a_real_run_would(self, world: dict) -> None: |
| 420 | sb = world["sb"] |
| 421 | repo = sb.repo("d", _BASE) |
| 422 | sb.write(repo, {"src/core.py": "def core():\n return 999 # local work\n"}) |
| 423 | dry = sb.run(repo, "apply-patch", str(world["patch"]), "--dry-run", "--json", ok=False) |
| 424 | real = sb.run(repo, "apply-patch", str(world["patch"]), "--json", ok=False) |
| 425 | assert dry.rc == real.rc == 1 and dry.json["error"] == real.json["error"] == "dirty_workdir" |
| 426 | assert dry.json["files"] == real.json["files"] == ["src/core.py"] |
| 427 | assert "999" in (repo / "src/core.py").read_text() |
| 428 | # --force skips the old-content check: the local edit is discarded |
| 429 | sb.run(repo, "apply-patch", str(world["patch"]), "--force", "--json") |
| 430 | assert (repo / "src/core.py").read_text() == _CHANGE["src/core.py"] |
| 431 | |
| 432 | def test_force_never_bypasses_the_path_rules(self, world: dict) -> None: |
| 433 | from muse.core.patch_record import compute_patch_id, deserialize_patch |
| 434 | sb = world["sb"] |
| 435 | repo = sb.repo("e", _BASE) |
| 436 | d = json.loads(world["patch"].read_text()) |
| 437 | d["to_manifest"] = {".muse/notes.py" if p == "test_new.py" else p: oid for p, oid in d["to_manifest"].items()} |
| 438 | d["patch_id"] = compute_patch_id(deserialize_patch(json.dumps(d).encode())) |
| 439 | bad = sb.root / "unsafe.mpatch" |
| 440 | bad.write_text(json.dumps(d)) |
| 441 | r = sb.run(repo, "apply-patch", str(bad), "--force", "--json", ok=False) |
| 442 | assert r.rc == 1 and r.json["error"] == "unsafe_path" and ".muse/notes.py" in r.json["files"] |
| 443 | assert not (repo / ".muse/notes.py").exists() and (repo / "src/core.py").read_text() == _BASE["src/core.py"] |
| 444 | |
| 445 | def test_page_states_the_apply_patch_contract(self, html: str) -> None: |
| 446 | sec = " ".join(_section(html, "mpatch-format").split()) |
| 447 | assert "Known defect" not in sec and "is meant to verify" not in sec |
| 448 | assert "verifies the working-tree file against its old content" in sec |
| 449 | assert "<code>--force</code> never bypasses the path-safety rules" in sec |
| 450 | |
| 451 | |
| 452 | # ── DOC_WIRE_07 — hub verifiers and known defects ───────────────────────────── |
| 453 | |
| 454 | def _wire_commit(mpack: bytes) -> dict: |
| 455 | from muse.core.mpack import parse_wire_mpack |
| 456 | return parse_wire_mpack(mpack)["commits"][-1] |
| 457 | |
| 458 | |
| 459 | def test_push_verifiers_behave_as_documented(mpack: bytes) -> None: |
| 460 | from muse.core.types import blob_id |
| 461 | from musehub.db.musehub_repo_models import MusehubCommit |
| 462 | from musehub.services.musehub_wire_push import _commit_row_from_wire |
| 463 | from musehub.services.musehub_wire_shared import (MPackValidationError, ObjectHashMismatch, _verify_object_hash, |
| 464 | served_identity_ok, served_signature_problem) |
| 465 | _verify_object_hash(blob_id(b"x"), b"x") |
| 466 | with pytest.raises(ObjectHashMismatch): |
| 467 | _verify_object_hash(blob_id(b"x"), b"y") |
| 468 | with pytest.raises(ObjectHashMismatch): |
| 469 | _verify_object_hash("sha256:" + hashlib.sha256(b"x").hexdigest(), b"x") # untyped hash is not an object ID |
| 470 | assert issubclass(MPackValidationError, ValueError) and issubclass(ObjectHashMismatch, ValueError) # → 422 |
| 471 | c = _wire_commit(mpack) |
| 472 | row = MusehubCommit(**_commit_row_from_wire(c)) |
| 473 | assert served_identity_ok(row) and served_signature_problem(row) is None |
| 474 | assert not served_identity_ok(MusehubCommit(**_commit_row_from_wire({**c, "message": "edited"}))) |
| 475 | lone = MusehubCommit(**_commit_row_from_wire({**c, "signature": "ed25519:" + "A" * 86})) |
| 476 | assert served_signature_problem(lone) == "carries a signature but its id binds no signer" |
| 477 | |
| 478 | |
| 479 | class _Backend: |
| 480 | async def quarantine_mpack(self, key: str) -> None: |
| 481 | return None |
| 482 | |
| 483 | |
| 484 | class _NoStoredCommits: |
| 485 | """A session stub for the database-free check: no pushed commit is stored yet.""" |
| 486 | |
| 487 | async def execute(self, _stmt: object) -> types.SimpleNamespace: |
| 488 | return types.SimpleNamespace(scalars=lambda: []) |
| 489 | |
| 490 | |
| 491 | def test_require_signed_commits_rejects_unsigned(html: str, mpack: bytes, monkeypatch: pytest.MonkeyPatch) -> None: |
| 492 | """musehub#296 (fixed): the server-wide setting refuses a new unsigned commit; off, it is accepted.""" |
| 493 | import musehub.config as config |
| 494 | from musehub.services.musehub_wire_push import _verify_incoming_commits |
| 495 | from musehub.services.musehub_wire_shared import MPackValidationError |
| 496 | commit = _wire_commit(mpack) |
| 497 | rows, _ = asyncio.run(_verify_incoming_commits(_NoStoredCommits(), [commit], "sha256:" + "0" * 64, _Backend())) |
| 498 | assert commit["commit_id"] in rows |
| 499 | monkeypatch.setattr(config.settings, "require_signed_commits", True) |
| 500 | with pytest.raises(MPackValidationError, match="requires signed commits"): |
| 501 | asyncio.run(_verify_incoming_commits(_NoStoredCommits(), [commit], "sha256:" + "0" * 64, _Backend())) |
| 502 | text = " ".join(_plain(_section(html, "integrity")).split()) |
| 503 | assert "server-wide require_signed_commits setting" in text and "no per-repo switch" in text |
| 504 | assert "Not available in the current MuseHub release" not in text |
| 505 | |
| 506 | |
| 507 | def test_push_ingest_keeps_reviewed_by_and_test_runs(mpack: bytes) -> None: |
| 508 | """musehub#297 (fixed part): reviewed_by and test_runs are stored as pushed; one format_version is served.""" |
| 509 | from musehub.db.musehub_repo_models import MusehubCommit |
| 510 | from musehub.models.wire import WIRE_COMMIT_FORMAT_VERSION, WireCommit |
| 511 | from musehub.services.musehub_wire_push import _commit_row_from_wire |
| 512 | from musehub.services.musehub_wire_shared import _to_wire_commit |
| 513 | row = _commit_row_from_wire({**_wire_commit(mpack), "reviewed_by": ["alice"], "test_runs": 5}) |
| 514 | assert row["reviewed_by"] == ["alice"] and row["test_runs"] == 5 |
| 515 | served = _to_wire_commit(MusehubCommit(**row)) |
| 516 | assert (served.reviewed_by, served.test_runs, served.format_version) == (["alice"], 5, WIRE_COMMIT_FORMAT_VERSION) |
| 517 | assert WireCommit.model_fields["format_version"].default == WIRE_COMMIT_FORMAT_VERSION == 8 |
| 518 | |
| 519 | |
| 520 | def test_push_ingest_keeps_commit_metadata(mpack: bytes) -> None: |
| 521 | """musehub#297 (fixed): the commit's ``metadata`` is stored (musehub_commits.commit_metadata) and served back on fetch.""" |
| 522 | from musehub.services.musehub_wire_push import _commit_row_from_wire |
| 523 | row = _commit_row_from_wire({**_wire_commit(mpack), "metadata": {"k": "v"}}) |
| 524 | assert row.get("commit_metadata") == {"k": "v"} |
| 525 | |
| 526 | |
| 527 | def test_fetch_bounds_and_depth_are_applied(html: str) -> None: |
| 528 | """musehub#298 (fixed): every fetch route validates WireFetchRequest (want <= 10,000, have trimmed to 1,000, no depth); the dead push |
| 529 | models are gone.""" |
| 530 | import musehub.api.routes.wire as wire |
| 531 | import musehub.models.wire as models |
| 532 | from pydantic import ValidationError |
| 533 | for handler in (wire.fetch, wire.fetch_mpack, wire.fetch_presign): |
| 534 | assert "_fetch_request(" in inspect.getsource(handler), handler.__name__ |
| 535 | assert (models.MAX_WANT_PER_FETCH, models.MAX_HAVE_PER_FETCH) == (10_000, 1_000) |
| 536 | with pytest.raises(ValidationError): |
| 537 | models.WireFetchRequest.model_validate({"want": ["sha256:" + "a" * 64] * 10_001}) |
| 538 | assert len(models.WireFetchRequest.model_validate({"have": ["sha256:" + "a" * 64] * 5_000}).have) == 1_000 |
| 539 | with pytest.raises(ValidationError): |
| 540 | models.WireFetchRequest.model_validate({"want": [], "depth": 1}) |
| 541 | for gone in ("WireSnapshot", "WireObject", "WireMPack", "MAX_OBJECT_BYTES", "MAX_SNAPSHOTS_PER_PUSH"): |
| 542 | assert not hasattr(models, gone), gone |
| 543 | rows = {r[0]: r[1] for r in _table_rows(_section(html, "limits"))} |
| 544 | assert rows["fetch, fetch/mpack, fetch/presign — want / have IDs"].startswith("want 10,000 (422); have first 1,000 used") |
| 545 | assert '"depth"' not in _section(html, "fetch-presign") and "Declared but not enforced" not in html |
| 546 | |
| 547 | |
| 548 | def test_cdn_integrity_check_uses_the_typed_hash(html: str) -> None: |
| 549 | """musehub#299 (fixed): /o/{id} verifies the typed blob ID and refuses a mismatch; the endpoint row says so.""" |
| 550 | import musehub.api.routes.wire as wire |
| 551 | src = inspect.getsource(wire.get_object) |
| 552 | assert "sha256(raw)" not in src and "blob_id(raw)" in src and "HTTP_500_INTERNAL_SERVER_ERROR" in src |
| 553 | row = next(r for r in _table_rows(_section(html, "endpoints")) if r[1] == "/o/{object_id}") |
| 554 | assert "refused with 500" in row[2] and "never served" in row[2] |
File History
10 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
sha256:22ee032c7cb1c6d27f318812c88dd8c2ba76fc8ee54551be7742fe75a1558202
fix(wire): GET /o/{id} verifies the typed blob ID and refus…
Opus 5.5
patch
2 days ago
sha256:e1629e5f328982dbf6e535d3f253993c78ff225dfee4ebb0e0a15fc1db69046b
merge: rc25 — docs and docs-contract tests match Muse batch…
Opus 5.5
minor
⚠
2 days ago
sha256:a3b15c421d90a641270dc6a91b434b7061520e4434cdb2f6acb11a28b71d5d16
docs(wire): apply-patch verifies old content and enforces p…
Opus 5.5
patch
2 days ago
sha256:369f691c7aaec3ca84a864ad1d099d5409045ddc12d185a922b3c84b9e2a1bad
merge: 100% passing, parallelized MuseHub test suite (task/…
Sonnet 5.5
patch
3 days ago
sha256:8166b51ae53275831023a7ce334228b3ccc8237b55d68a35172389db9ff46ad5
test: 100% passing MuseHub suite, parallelized (xdist, one …
Sonnet 5.5
patch
3 days ago
sha256:af8b93688057d89c35730de054ab1ce0648f4c405e3ebb884729a1a8b23fcd76
docs(wire): #254 Wire Protocol QA contract v2 (replay of c0…
Opus 5.5
minor
⚠
9 days ago