"""Pull verb atomic tests against localhost. Builds a source repo with real muse commits, pushes to local hub, then exercises clone + incremental pull roundtrips. Tests: PL0: clone-equivalent — pull 100 commits into an empty local repo PL1: +1 commit incremental PL2: +10 commits incremental PL3: +100 commits incremental Usage: python3 scripts/pull_test.py """ from __future__ import annotations import json import os import shutil import subprocess import sys import time MUSE = "muse" LOCAL_HUB = "https://localhost:1337" TIMING_REPO_URL = f"{LOCAL_HUB}/gabriel/timing-test" def run(cmd: list[str], cwd: str | None = None, check: bool = True) -> subprocess.CompletedProcess: return subprocess.run(cmd, cwd=cwd, capture_output=True, text=True, check=check) def m(args: list[str], cwd: str, check: bool = True) -> subprocess.CompletedProcess: return run([MUSE] + args, cwd=cwd, check=check) def mj(args: list[str], cwd: str) -> dict: r = m(args + ["--json"], cwd=cwd) return json.loads(r.stdout) def fresh_pull_target(path: str) -> str: if os.path.exists(path): shutil.rmtree(path) os.makedirs(path) m(["init"], cwd=path) # Use remote add first; fall back to set-url if it already exists. r = run([MUSE, "remote", "add", "local", TIMING_REPO_URL], cwd=path, check=False) if r.returncode != 0: run([MUSE, "remote", "set-url", "local", TIMING_REPO_URL], cwd=path, check=False) return path def local_commit_count(repo: str) -> int: try: return len(mj(["log"], cwd=repo).get("commits", [])) except Exception: return -1 def timed_pull(repo: str, branch: str = "main") -> dict: t0 = time.perf_counter() r = run([MUSE, "pull", "local", branch], cwd=repo, check=False) elapsed = round((time.perf_counter() - t0) * 1000, 1) ok = r.returncode == 0 err = None if not ok: output = r.stdout + r.stderr for line in output.splitlines(): if "❌" in line or "Error" in line or "failed" in line.lower(): err = line.strip() break if not err: err = (r.stdout + r.stderr).strip()[:200] return {"ok": ok, "t_total": elapsed, "error": err} def fmt(label: str, r: dict, extra: str = "") -> str: ok = "✅" if r["ok"] else "❌" err = f" ERROR: {r['error']}" if r.get("error") else "" return ( f" {label:42s}" f" total={str(r['t_total']):>8}ms" f" {ok}{extra}{err}" ) def _add_commits(src: str, n: int) -> bool: existing = local_commit_count(src) for i in range(n): idx = existing + i obj_file = os.path.join(src, f"obj_{idx:04d}.dat") with open(obj_file, "wb") as f: f.write(os.urandom(512)) m(["code", "add", f"obj_{idx:04d}.dat"], cwd=src) m(["commit", "-m", f"pull test commit {idx}", "--agent-id", "wire-test", "--model-id", "n/a"], cwd=src) r = run([MUSE, "push", "local", "main", "--force"], cwd=src, check=False) return r.returncode == 0 def build_source_repo(path: str, n_commits: int) -> bool: if os.path.exists(path): shutil.rmtree(path) os.makedirs(path) m(["init"], cwd=path) r = run([MUSE, "remote", "add", "local", TIMING_REPO_URL], cwd=path, check=False) if r.returncode != 0: run([MUSE, "remote", "set-url", "local", TIMING_REPO_URL], cwd=path, check=False) for i in range(n_commits): obj_file = os.path.join(path, f"obj_{i:04d}.dat") with open(obj_file, "wb") as f: f.write(os.urandom(512)) m(["code", "add", f"obj_{i:04d}.dat"], cwd=path) m(["commit", "-m", f"pull test commit {i}", "--agent-id", "wire-test", "--model-id", "n/a"], cwd=path) r = run([MUSE, "push", "local", "main", "--force"], cwd=path, check=False) return r.returncode == 0 def ensure_hub_repo() -> None: tmp = "/tmp/pull-hub-probe" if os.path.exists(tmp): shutil.rmtree(tmp) os.makedirs(tmp) m(["init"], cwd=tmp) run([MUSE, "-C", tmp, "hub", "repo", "create", "--name", "timing-test", "--visibility", "public", "--json"], check=False) def main() -> None: print(f"\n── Pull atomic tests → {LOCAL_HUB} ──") print(f" {'label':42s} {'total':>12} status") print(" " + "-" * 85) ensure_hub_repo() src = "/tmp/pull-local-src" print(f" building source repo (100 commits)… ", end="", flush=True) ok_build = build_source_repo(src, 100) print("done" if ok_build else "FAILED") if not ok_build: print("❌ source repo push failed — stopping.") return target = "/tmp/pull-local-target" fresh_pull_target(target) r0 = timed_pull(target) n0 = local_commit_count(target) print(fmt("PL0: empty→100c+100o (M clone)", r0, f" [{n0}c local]")) sys.stdout.flush() if not r0["ok"]: print(f"\n❌ PL0 failed — stopping.\n {r0['error']}") return _add_commits(src, 1) r1 = timed_pull(target) print(fmt("PL1: +1c+1o incremental (XS)", r1, f" [{local_commit_count(target)}c local]")) sys.stdout.flush() _add_commits(src, 10) r2 = timed_pull(target) print(fmt("PL2: +10c+10o incremental (XS→S)", r2, f" [{local_commit_count(target)}c local]")) sys.stdout.flush() _add_commits(src, 100) r3 = timed_pull(target) print(fmt("PL3: +100c+100o incremental (M)", r3, f" [{local_commit_count(target)}c local]")) sys.stdout.flush() print() if __name__ == "__main__": main()