test_wire_step2a_pack.py
python
sha256:51ce277f663e01a43eaffbe77509b1de7ac2d4251b55d23306304bcdeb92c90d
feat(pack): delta-encode snapshots in MPackBundle wire format
Sonnet 4.6
minor
⚠ breaking
122 days ago
| 1 | """Wire protocol step 2A — Pack performance gate (client-side). |
| 2 | |
| 3 | Ticket #45, Step 2A: BFS walk + bundle serialization must complete in under 5s. |
| 4 | |
| 5 | This test measures the two operations that constitute the client-side pack phase: |
| 6 | |
| 7 | build_mpack(root, [head], have=[]) — BFS walk + collect all objects |
| 8 | msgpack.packb(bundle, use_bin_type) — serialize bundle to wire bytes |
| 9 | |
| 10 | Total < 5s |
| 11 | |
| 12 | Repo size: 100 commits, 600 unique objects, 4 KiB blobs (~2.4 MB raw). |
| 13 | 600 objects sits just above _PRESIGN_OBJECT_THRESHOLD (500) — the size class |
| 14 | that triggers the new bundle upload path instead of N individual PUTs. |
| 15 | |
| 16 | If the assertion fails, step 2A is not done. Do not move to step 2B. |
| 17 | """ |
| 18 | from __future__ import annotations |
| 19 | |
| 20 | import datetime |
| 21 | import pathlib |
| 22 | import time |
| 23 | |
| 24 | import msgpack |
| 25 | import pytest |
| 26 | |
| 27 | from muse.core.object_store import write_object |
| 28 | from muse.core.pack import build_mpack |
| 29 | from muse.core.paths import muse_dir |
| 30 | from muse.core.snapshot import compute_commit_id, compute_snapshot_id |
| 31 | from muse.core.store import ( |
| 32 | CommitRecord, |
| 33 | SnapshotRecord, |
| 34 | write_branch_ref, |
| 35 | write_commit, |
| 36 | write_snapshot, |
| 37 | ) |
| 38 | from muse.core.types import blob_id |
| 39 | |
| 40 | |
| 41 | # --------------------------------------------------------------------------- |
| 42 | # Gate constants |
| 43 | # --------------------------------------------------------------------------- |
| 44 | |
| 45 | _N_COMMITS = 100 |
| 46 | _N_OBJECTS = 600 # just above _PRESIGN_OBJECT_THRESHOLD = 500 |
| 47 | _BLOB_SIZE = 4096 # 4 KiB per object → ~2.4 MB raw total |
| 48 | _TOTAL_GATE_S = 5.0 |
| 49 | |
| 50 | |
| 51 | # --------------------------------------------------------------------------- |
| 52 | # Repo fixture |
| 53 | # --------------------------------------------------------------------------- |
| 54 | |
| 55 | def _make_repo(tmp: pathlib.Path) -> pathlib.Path: |
| 56 | tmp.mkdir(parents=True, exist_ok=True) |
| 57 | dot = muse_dir(tmp) |
| 58 | dot.mkdir() |
| 59 | (dot / "repo.json").write_text('{"repo_id":"step2a","owner":"gabriel"}') |
| 60 | for d in ("commits", "snapshots", "objects"): |
| 61 | (dot / d).mkdir() |
| 62 | (dot / "refs" / "heads").mkdir(parents=True) |
| 63 | (dot / "HEAD").write_text("ref: refs/heads/main\n") |
| 64 | (dot / "config.toml").write_text("") |
| 65 | return tmp |
| 66 | |
| 67 | |
| 68 | def _populate(repo: pathlib.Path) -> str: |
| 69 | """Write _N_OBJECTS blobs + _N_COMMITS chain; return tip commit ID.""" |
| 70 | blobs: dict[str, str] = {} |
| 71 | for i in range(_N_OBJECTS): |
| 72 | data = f"step2a-{i:08d}-".encode() + b"x" * _BLOB_SIZE |
| 73 | oid = blob_id(data) |
| 74 | write_object(repo, oid, data) |
| 75 | blobs[f"file_{i:04d}.py"] = oid |
| 76 | |
| 77 | sid = compute_snapshot_id(blobs) |
| 78 | write_snapshot(repo, SnapshotRecord(snapshot_id=sid, manifest=blobs)) |
| 79 | |
| 80 | parent: str | None = None |
| 81 | tip = "" |
| 82 | for i in range(_N_COMMITS): |
| 83 | ts = datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc) |
| 84 | msg = f"commit-{i:05d}" |
| 85 | cid = compute_commit_id( |
| 86 | parent_ids=[parent] if parent else [], |
| 87 | snapshot_id=sid, |
| 88 | message=msg, |
| 89 | committed_at_iso=ts.isoformat(), |
| 90 | author="gabriel", |
| 91 | ) |
| 92 | rec = CommitRecord( |
| 93 | repo_id="step2a", |
| 94 | commit_id=cid, |
| 95 | branch="main", |
| 96 | snapshot_id=sid, |
| 97 | message=msg, |
| 98 | committed_at=ts, |
| 99 | parent_commit_id=parent, |
| 100 | parent2_commit_id=None, |
| 101 | author="gabriel", |
| 102 | metadata={}, |
| 103 | structured_delta=None, |
| 104 | sem_ver_bump="none", |
| 105 | breaking_changes=[], |
| 106 | agent_id="", |
| 107 | model_id="", |
| 108 | toolchain_id="", |
| 109 | prompt_hash="", |
| 110 | signature="", |
| 111 | signer_key_id="", |
| 112 | ) |
| 113 | write_commit(repo, rec) |
| 114 | parent = cid |
| 115 | tip = cid |
| 116 | |
| 117 | write_branch_ref(repo, "main", tip) |
| 118 | return tip |
| 119 | |
| 120 | |
| 121 | # --------------------------------------------------------------------------- |
| 122 | # THE test |
| 123 | # --------------------------------------------------------------------------- |
| 124 | |
| 125 | def test_pack_step2a_performance_gate(tmp_path: pathlib.Path) -> None: |
| 126 | """Step 2A gate: build_mpack + msgpack.packb must complete in under 5s. |
| 127 | |
| 128 | This is a pass/fail performance gate, not a benchmark. If the assertion |
| 129 | fails, step 2A is not done and step 2B must not be attempted. |
| 130 | """ |
| 131 | repo = _make_repo(tmp_path / "repo") |
| 132 | head = _populate(repo) |
| 133 | |
| 134 | t0 = time.perf_counter() |
| 135 | |
| 136 | bundle = build_mpack(repo, [head], have=[]) |
| 137 | wire_bytes = msgpack.packb(bundle, use_bin_type=True) |
| 138 | |
| 139 | total_s = time.perf_counter() - t0 |
| 140 | |
| 141 | assert isinstance(wire_bytes, bytes), "msgpack.packb returned non-bytes" |
| 142 | assert len(wire_bytes) > 0, "bundle serialized to empty bytes" |
| 143 | |
| 144 | n_commits = len(bundle.get("commits", [])) |
| 145 | n_objects = len(bundle.get("objects", [])) |
| 146 | wire_kb = len(wire_bytes) / 1024 |
| 147 | |
| 148 | assert n_commits == _N_COMMITS, ( |
| 149 | f"expected {_N_COMMITS} commits in bundle, got {n_commits}" |
| 150 | ) |
| 151 | assert n_objects == _N_OBJECTS, ( |
| 152 | f"expected {_N_OBJECTS} objects in bundle, got {n_objects}" |
| 153 | ) |
| 154 | |
| 155 | assert total_s < _TOTAL_GATE_S, ( |
| 156 | f"Step 2A FAIL: build_mpack + packb took {total_s:.2f}s — gate is {_TOTAL_GATE_S}s " |
| 157 | f"({n_commits} commits, {n_objects} objects, {wire_kb:.1f} KiB wire)" |
| 158 | ) |
| 159 | |
| 160 | print( |
| 161 | f"\n Step 2A — Pack (client-side)\n" |
| 162 | f" Commits: {n_commits}\n" |
| 163 | f" Objects: {n_objects}\n" |
| 164 | f" Wire size: {wire_kb:.1f} KiB\n" |
| 165 | f" Total time: {total_s:.3f}s (gate {_TOTAL_GATE_S}s) ✅" |
| 166 | ) |
File History
1 commit
sha256:51ce277f663e01a43eaffbe77509b1de7ac2d4251b55d23306304bcdeb92c90d
feat(pack): delta-encode snapshots in MPackBundle wire format
Sonnet 4.6
minor
⚠
122 days ago