gabriel / musehub public
test_build_context.py python
677 lines 37.8 KB
Raw
sha256:b4b999434d042d74edc7032f1f2cfd20fda5b3580fe85966351db5b8eef0abb3 feat(intel): every grammar on the hub, and every intel job … Opus 5.5 minor ⚠ breaking 1 day ago
1 """Deployment-image reproducibility (musehub#114 Phase 6D, Phase F).
2
3 The image must contain only committed, intended inputs, and say which revisions they came from.
4 Context assembly is tested against *real* throw-away Muse repositories in a temp directory (HOME is
5 redirected, nothing canonical is touched). The Dockerfile allowlist is tested statically against the
6 Dockerfile's own COPY lines and, when a Docker daemon is available, with a real BuildKit probe build.
7 No live image is built or pushed.
8 """
9
10 from __future__ import annotations
11
12 import hashlib
13 import json
14 import os
15 import pathlib
16 import io
17 import re
18 import shutil
19 import subprocess
20 import sys
21 import tarfile
22
23 import pytest
24 from packaging.requirements import Requirement
25 from packaging.utils import canonicalize_name
26
27 from deploy import build_context
28 from deploy.build_context import FRONTEND_ASSETS, MIGRATIONS, REQUIRED, STATIC_DIR, BuildContextError, assemble, context_tar, main
29
30 ROOT = pathlib.Path(__file__).resolve().parents[1]
31 DOCKERFILE = ROOT / "Dockerfile"
32 DOCKERIGNORE = ROOT / "Dockerfile.dockerignore"
33 REQUIREMENTS = ROOT / "requirements.txt"
34 CONSTRAINTS = ROOT / "constraints.txt"
35 PUSH_SH = ROOT / "deploy" / "push.sh"
36 DEPLOY_SH = ROOT / "deploy" / "deploy.sh"
37
38
39 # ── npm test double ───────────────────────────────────────────────────────────
40
41 #: Logs "<cwd> <args>", installs nothing, and "builds" each asset from its committed source, so a test can tell a
42 #: built asset from a stray working-tree file. FAKE_NPM_FAIL=<script> fails that script; FAKE_NPM_EMPTY=<asset>
43 #: writes that asset empty.
44 FAKE_NPM = r"""#!/bin/sh
45 printf '%s %s\n' "$PWD" "$*" >> "$FAKE_NPM_LOG"
46 case "$1" in
47 ci) mkdir -p node_modules/.fake; exit 0 ;;
48 run) ;;
49 *) exit 2 ;;
50 esac
51 if [ "$2" = "--silent" ]; then script="$3"; else script="$2"; fi
52 if [ "$script" = "$FAKE_NPM_FAIL" ]; then echo "boom: $script" >&2; exit 1; fi
53 out=musehub/templates/musehub/static
54 mkdir -p "$out"
55 case "$script" in
56 build:js) name=app.js; src=src/ts/app.ts ;;
57 build:css) name=app.css; src=src/scss/app.scss ;;
58 build:embed) name=embed.css; src=src/scss/embed.scss ;;
59 *) echo "unknown script $script" >&2; exit 1 ;;
60 esac
61 if [ "$name" = "$FAKE_NPM_EMPTY" ]; then : > "$out/$name"; else { printf 'built:'; cat "$src"; } > "$out/$name"; fi
62 """
63
64 #: compiled asset -> the committed source the fake npm builds it from
65 ASSET_SOURCES = {"app.js": "src/ts/app.ts", "app.css": "src/scss/app.scss", "embed.css": "src/scss/embed.scss"}
66
67
68 @pytest.fixture(autouse=True)
69 def fake_npm(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path:
70 bindir = tmp_path / "fake-bin"
71 bindir.mkdir()
72 (bindir / "npm").write_text(FAKE_NPM)
73 (bindir / "npm").chmod(0o755)
74 log = tmp_path / "npm.log"
75 monkeypatch.setenv("FAKE_NPM_LOG", str(log))
76 monkeypatch.setenv("PATH", f"{bindir}{os.pathsep}{os.environ['PATH']}")
77 return log
78
79
80 # ── throw-away Muse repositories ──────────────────────────────────────────────
81
82
83 @pytest.fixture
84 def home(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path:
85 h = tmp_path / "home"
86 h.mkdir()
87 monkeypatch.setenv("HOME", str(h))
88 return h
89
90
91 def muse(repo: pathlib.Path, *args: str) -> str:
92 proc = subprocess.run(["muse", "-C", str(repo), *args], capture_output=True, text=True, timeout=120)
93 assert proc.returncode == 0, f"muse {args}: {proc.stderr or proc.stdout}"
94 return proc.stdout
95
96
97 def init_repo(repo: pathlib.Path, files: dict[str, str]) -> None:
98 repo.mkdir(parents=True)
99 subprocess.run(["muse", "init", "--domain", "code", str(repo)], capture_output=True, check=True, timeout=60)
100 for rel, text in files.items():
101 (repo / rel).parent.mkdir(parents=True, exist_ok=True)
102 (repo / rel).write_text(text)
103 muse(repo, "code", "add", *files)
104 muse(repo, "commit", "-m", "fixture")
105
106
107 def tracked(base: dict[str, str] | None = None) -> dict[str, dict[str, str]]:
108 """The committed content of both fixture repositories: every REQUIRED input plus migrations."""
109 hub = {
110 "Dockerfile": "FROM scratch\n", "Dockerfile.dockerignore": "**\n", "requirements.txt": "fastapi\n", "constraints.txt": "fastapi==0.1\n", "grammar-requirements.txt": "tree-sitter-java==0.23.5\n",
111 "pyproject.toml": "[project]\n",
112 "alembic.ini": "[alembic]\n", "entrypoint.sh": "#!/bin/sh\n", "musehub/__init__.py": "", "alembic/env.py": "",
113 "alembic/versions/0001_init.py": "# 1\n", "alembic/versions/0002_more.py": "# 2\n", "deploy/deploy.sh": "#!/bin/sh\n", "docs/guide.md": "# guide\n",
114 "package.json": '{"name": "fixture"}\n', "package-lock.json": '{"lockfileVersion": 3}\n', "tsconfig.json": "{}\n",
115 "src/ts/app.ts": "export const app = 1;\n", "src/scss/app.scss": "body { color: red; }\n", "src/scss/embed.scss": ".player { color: blue; }\n",
116 }
117 lib = {"pyproject.toml": "[project]\n", "README.md": "# muse\n", "muse/__init__.py": ""}
118 return {"musehub": {**hub, **(base or {})}, "muse": lib}
119
120
121 @pytest.fixture
122 def eco(tmp_path: pathlib.Path, home: pathlib.Path) -> pathlib.Path:
123 root = tmp_path / "ecosystem"
124 root.mkdir()
125 spec = tracked()
126 for name, files in spec.items():
127 init_repo(root / name, files)
128 return root
129
130
131 def out_dir(tmp_path: pathlib.Path) -> pathlib.Path:
132 return tmp_path / "ctx"
133
134
135 def listing(root: pathlib.Path) -> list[str]:
136 return sorted(str(p.relative_to(root)) for p in root.rglob("*") if p.is_file())
137
138
139 # ── exclusion ─────────────────────────────────────────────────────────────────
140
141
142 class TestUntrackedFilesNeverEnter:
143 def test_untracked_handoff_document_is_excluded_and_not_deleted(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
144 handoff = eco / "musehub" / "docs" / "work" / "session-handoff.md"
145 handoff.parent.mkdir(parents=True)
146 handoff.write_text("local notes\n")
147 info = assemble(eco, out_dir(tmp_path))
148 assert not (out_dir(tmp_path) / "musehub/docs/work").exists()
149 assert "docs/work/session-handoff.md" in info["repositories"]["musehub"]["untracked_excluded"]
150 assert handoff.read_text() == "local notes\n", "the source file is left exactly where it was"
151
152 def test_any_unexpected_local_file_is_excluded(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
153 for rel in ("musehub/stray.txt", "musehub/musehub/scratch.py", "musehub/deploy/local-notes.sh", "muse/muse/wip.py", "muse/dist/muse-9.tar.gz", "musehub/.env"):
154 (eco / rel).parent.mkdir(parents=True, exist_ok=True)
155 (eco / rel).write_text("local\n")
156 (eco / "agentception").mkdir()
157 (eco / "agentception" / "secret.txt").write_text("not ours\n")
158 assemble(eco, out_dir(tmp_path))
159 names = listing(out_dir(tmp_path))
160 for leaked in ("stray.txt", "scratch.py", "local-notes.sh", "wip.py", "muse-9.tar.gz", ".env", "secret.txt"):
161 assert not any(n.endswith(leaked) for n in names), leaked
162 assert not (out_dir(tmp_path) / "agentception").exists()
163
164 def test_muse_internals_are_never_exported(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
165 assemble(eco, out_dir(tmp_path))
166 assert not any(".muse/" in n or n.startswith(".muse") for n in listing(out_dir(tmp_path)))
167
168 def test_the_context_is_exactly_the_committed_files_plus_build_info_and_compiled_assets(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
169 assemble(eco, out_dir(tmp_path))
170 want = sorted(
171 [f"musehub/{p}" for p in tracked()["musehub"]] + [f"muse/{p}" for p in tracked()["muse"]] + ["build-info.json"]
172 + [f"{STATIC_DIR}/{name}" for name in FRONTEND_ASSETS]
173 )
174 assert listing(out_dir(tmp_path)) == want
175
176 def test_two_assemblies_are_identical(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
177 a, b = out_dir(tmp_path), tmp_path / "ctx2"
178 assemble(eco, a)
179 assemble(eco, b)
180 assert listing(a) == listing(b)
181 assert all((a / n).read_bytes() == (b / n).read_bytes() for n in listing(a))
182
183
184 # ── inclusion ─────────────────────────────────────────────────────────────────
185
186
187 class TestRequiredInputs:
188 def test_application_files_and_every_migration_are_present(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
189 assemble(eco, out_dir(tmp_path))
190 for required in REQUIRED:
191 assert (out_dir(tmp_path) / required).exists(), required
192 assert sorted(p.name for p in (out_dir(tmp_path) / MIGRATIONS).glob("*.py")) == ["0001_init.py", "0002_more.py"]
193
194 @pytest.mark.parametrize("missing", ["entrypoint.sh", "requirements.txt", "constraints.txt", "grammar-requirements.txt", "alembic.ini", "musehub/__init__.py", "Dockerfile.dockerignore",
195 "deploy/deploy.sh", "package.json", "package-lock.json"])
196 def test_build_fails_when_a_required_application_input_is_not_committed(self, tmp_path: pathlib.Path, home: pathlib.Path, missing: str) -> None:
197 eco = tmp_path / "e"
198 eco.mkdir()
199 spec = tracked()
200 del spec["musehub"][missing]
201 for name, files in spec.items():
202 init_repo(eco / name, files)
203 with pytest.raises(BuildContextError, match="missing from the exported tree"):
204 assemble(eco, out_dir(tmp_path))
205 assert not out_dir(tmp_path).exists(), "no half-assembled context is left behind"
206
207 def test_build_fails_when_the_muse_engine_inputs_are_missing(self, tmp_path: pathlib.Path, home: pathlib.Path) -> None:
208 eco = tmp_path / "e"
209 eco.mkdir()
210 spec = tracked()
211 del spec["muse"]["README.md"]
212 for name, files in spec.items():
213 init_repo(eco / name, files)
214 with pytest.raises(BuildContextError, match="muse/README.md"):
215 assemble(eco, out_dir(tmp_path))
216
217 def test_build_fails_when_no_migrations_are_committed(self, tmp_path: pathlib.Path, home: pathlib.Path) -> None:
218 eco = tmp_path / "e"
219 eco.mkdir()
220 spec = tracked()
221 spec["musehub"] = {k: v for k, v in spec["musehub"].items() if "versions" not in k}
222 for name, files in spec.items():
223 init_repo(eco / name, files)
224 with pytest.raises(BuildContextError, match="no migrations"):
225 assemble(eco, out_dir(tmp_path))
226
227 def test_a_migration_missing_from_the_export_is_detected(self, eco: pathlib.Path, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None:
228 real = build_context._export
229
230 def truncating(repo: pathlib.Path, revision: str, out: pathlib.Path, scratch: pathlib.Path, runner: object) -> list[str]:
231 names = real(repo, revision, out, scratch, runner) # type: ignore[arg-type]
232 if repo.name == "musehub":
233 (out / MIGRATIONS / "0002_more.py").unlink()
234 return names
235
236 monkeypatch.setattr(build_context, "_export", truncating)
237 with pytest.raises(BuildContextError, match="exported migrations differ"):
238 assemble(eco, out_dir(tmp_path))
239 assert not out_dir(tmp_path).exists()
240
241
242 # ── compiled UI assets ────────────────────────────────────────────────────────
243
244
245 class TestCompiledUiAssets:
246 """musehub#239. The app serves app.js/app.css/embed.css from /static. They are build outputs (.museignore), so an export of
247 committed files never contains them; the context builds them from the committed sources."""
248
249 def test_every_asset_is_in_the_context_built_from_the_committed_sources(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None: # SA_01
250 assemble(eco, out_dir(tmp_path))
251 assert sorted(FRONTEND_ASSETS) == sorted(ASSET_SOURCES)
252 for name, src in ASSET_SOURCES.items():
253 assert (out_dir(tmp_path) / STATIC_DIR / name).read_bytes() == b"built:" + (eco / "musehub" / src).read_bytes(), name
254
255 def test_build_info_records_each_asset_digest(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None: # SA_02
256 info = assemble(eco, out_dir(tmp_path))
257 for name in FRONTEND_ASSETS:
258 data = (out_dir(tmp_path) / STATIC_DIR / name).read_bytes()
259 assert info["frontend_assets"][name] == {"sha256": hashlib.sha256(data).hexdigest(), "bytes": len(data)}
260
261 def test_local_build_products_and_node_modules_are_never_used(self, eco: pathlib.Path, tmp_path: pathlib.Path, fake_npm: pathlib.Path) -> None: # SA_03
262 stale = eco / STATIC_DIR / "app.css"
263 stale.parent.mkdir(parents=True, exist_ok=True)
264 stale.write_text("stale local build\n")
265 (eco / "musehub" / "node_modules" / ".bin").mkdir(parents=True)
266 assemble(eco, out_dir(tmp_path))
267 assert (out_dir(tmp_path) / STATIC_DIR / "app.css").read_bytes().startswith(b"built:")
268 cwds = {line.split(" ", 1)[0] for line in fake_npm.read_text().splitlines()}
269 assert cwds and not [c for c in cwds if c.startswith((str(eco), str(out_dir(tmp_path))))], cwds
270 assert stale.read_text() == "stale local build\n", "the working tree is left exactly as it was"
271
272 def test_npm_installs_the_lockfile_without_lifecycle_scripts_and_builds_each_asset(self, eco: pathlib.Path, tmp_path: pathlib.Path, fake_npm: pathlib.Path) -> None: # SA_04
273 assemble(eco, out_dir(tmp_path))
274 calls = [line.split(" ", 1)[1] for line in fake_npm.read_text().splitlines()]
275 assert calls[0].split()[:2] == ["ci", "--ignore-scripts"]
276 assert sorted(calls[1:]) == sorted(f"run --silent {script}" for script in FRONTEND_ASSETS.values())
277 assert not [n for n in listing(out_dir(tmp_path)) if "node_modules" in n]
278
279 @pytest.mark.parametrize("env", [{"FAKE_NPM_FAIL": "build:css"}, {"FAKE_NPM_EMPTY": "app.js"}])
280 def test_a_failed_or_empty_asset_refuses_and_leaves_no_context(self, eco: pathlib.Path, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch, env: dict[str, str]) -> None: # SA_05
281 for key, value in env.items():
282 monkeypatch.setenv(key, value)
283 with pytest.raises(BuildContextError, match="frontend"):
284 assemble(eco, out_dir(tmp_path))
285 assert not out_dir(tmp_path).exists()
286
287 def test_no_npm_refuses_the_build(self, eco: pathlib.Path, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None: # SA_06
288 monkeypatch.setattr(build_context, "_npm", lambda: None)
289 with pytest.raises(BuildContextError, match="npm"):
290 assemble(eco, out_dir(tmp_path))
291 assert not out_dir(tmp_path).exists()
292
293
294 # ── provenance and refusal ────────────────────────────────────────────────────
295
296
297 class TestRevisionIdentity:
298 def test_build_info_records_both_repositories_muse_revisions(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
299 info = assemble(eco, out_dir(tmp_path))
300 for name in ("musehub", "muse"):
301 head = json.loads(muse(eco / name, "rev-parse", "HEAD", "--json"))["commit_id"]
302 assert info["repositories"][name]["revision"] == head
303 assert json.loads((out_dir(tmp_path) / "build-info.json").read_text()) == info
304
305 def test_tracked_modifications_refuse_the_build(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
306 (eco / "musehub" / "entrypoint.sh").write_text("#!/bin/sh\necho changed\n")
307 with pytest.raises(BuildContextError, match="tracked changes"):
308 assemble(eco, out_dir(tmp_path))
309 assert not out_dir(tmp_path).exists()
310
311 def test_a_tracked_deletion_refuses_the_build(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
312 (eco / "muse" / "README.md").unlink()
313 with pytest.raises(BuildContextError, match="tracked changes"):
314 assemble(eco, out_dir(tmp_path))
315
316 def test_changes_in_the_engine_repo_refuse_too(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
317 (eco / "muse" / "muse" / "__init__.py").write_text("x = 1\n")
318 with pytest.raises(BuildContextError, match="muse has tracked changes"):
319 assemble(eco, out_dir(tmp_path))
320
321 def test_a_directory_that_is_not_a_muse_repo_is_refused(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
322 shutil.rmtree(eco / "muse" / ".muse")
323 with pytest.raises(BuildContextError, match="cannot determine the revision with Muse"):
324 assemble(eco, out_dir(tmp_path))
325
326 def test_a_missing_repository_is_refused(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
327 shutil.rmtree(eco / "muse")
328 with pytest.raises(BuildContextError, match="not found"):
329 assemble(eco, out_dir(tmp_path))
330
331 def test_muse_unavailable_is_refused(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
332 def broken(*_a: object, **_k: object) -> subprocess.CompletedProcess[str]:
333 raise FileNotFoundError("muse")
334
335 with pytest.raises(BuildContextError, match="cannot determine the revision"):
336 assemble(eco, out_dir(tmp_path), runner=broken)
337
338 def test_an_existing_output_directory_is_never_reused(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
339 out_dir(tmp_path).mkdir()
340 (out_dir(tmp_path) / "old.txt").write_text("stale")
341 with pytest.raises(BuildContextError, match="never reused"):
342 assemble(eco, out_dir(tmp_path))
343 assert (out_dir(tmp_path) / "old.txt").read_text() == "stale"
344
345 def test_cli_prints_the_info_and_refuses_with_status_1(self, eco: pathlib.Path, tmp_path: pathlib.Path, capsys: pytest.CaptureFixture[str]) -> None:
346 assert main(["--ecosystem", str(eco), "--out", str(out_dir(tmp_path))]) == 0
347 assert json.loads(capsys.readouterr().out)["schema"] == 1
348 assert main(["--ecosystem", str(eco), "--out", str(out_dir(tmp_path))]) == 1
349 assert "BUILD CONTEXT REFUSED" in capsys.readouterr().err
350
351
352 # ── Dockerfile and ignore file ────────────────────────────────────────────────
353
354
355 def copy_sources() -> list[str]:
356 sources = []
357 for line in DOCKERFILE.read_text().splitlines():
358 m = re.match(r"\s*COPY\s+(?P<flags>(?:--\S+\s+)*)(?P<rest>.+)$", line)
359 if not m or "--from" in m["flags"]:
360 continue
361 parts = m["rest"].split()
362 sources += [p.rstrip("/") for p in parts[:-1]]
363 return sources
364
365
366 def allowed() -> set[str]:
367 return {line[1:].strip().rstrip("/") for line in DOCKERIGNORE.read_text().splitlines() if line.startswith("!")}
368
369
370 class TestDockerfileAllowlist:
371 def test_every_copy_source_is_allowlisted(self) -> None:
372 sources = copy_sources()
373 assert sources, "no COPY lines parsed"
374 wl = allowed()
375 for src in sources:
376 bases = {a.removesuffix("/**") for a in wl}
377 narrowed = any(b.startswith(src + "/") for b in bases) # a directory COPY the allowlist narrows to named children
378 inside = any(src == b or src.startswith(b + "/") for b in bases)
379 assert inside or narrowed, f"{src} is COPYed but not allowlisted"
380
381 def test_every_allowlisted_path_is_something_the_dockerfile_copies_or_a_child_of_it(self) -> None:
382 sources = copy_sources()
383 for entry in allowed():
384 base = entry.removesuffix("/**")
385 assert any(base == s or base.startswith(s + "/") for s in sources), f"{entry} is allowlisted but the Dockerfile never copies it"
386
387 def test_default_is_deny_and_work_notes_are_denied_last(self) -> None:
388 lines = [line for line in DOCKERIGNORE.read_text().splitlines() if line and not line.startswith("#")]
389 assert lines[0] == "**"
390 assert lines.index("musehub/docs/work/**") > max(i for i, line in enumerate(lines) if line.startswith("!")), "denials must follow the allowlist"
391
392 def test_the_phase_6d_working_note_is_denied_by_its_exact_path_only(self) -> None: # SA_07
393 lines = [line for line in DOCKERIGNORE.read_text().splitlines() if line and not line.startswith("#")]
394 last_allow = max(i for i, line in enumerate(lines) if line.startswith("!"))
395 assert lines.index("musehub/docs/phase-6d-readiness.md") > last_allow
396 assert {line for line in lines[last_allow + 1:] if "docs" in line} == {
397 "musehub/docs/work", "musehub/docs/work/**", "musehub/docs/phase-6d-readiness.md",
398 }, "no other documentation is excluded"
399 assert "!musehub/docs/**" in DOCKERIGNORE.read_text().splitlines(), "the documentation allowlist is unchanged"
400
401 def test_the_image_records_both_revisions_as_labels(self) -> None:
402 src = DOCKERFILE.read_text()
403 assert 'ARG MUSEHUB_REVISION=""' in src and 'ARG MUSE_REVISION=""' in src
404 assert 'org.opencontainers.image.revision="${MUSEHUB_REVISION}"' in src
405 assert 'ai.musehub.muse-engine-revision="${MUSE_REVISION}"' in src
406
407 @pytest.mark.host_tools # needs the docker CLI and a daemon on the machine running pytest; opt in with -m host_tools
408 @pytest.mark.parametrize("transport", ["directory", "stream"])
409 def test_buildkit_applies_the_real_allowlist_to_a_context_full_of_decoys(self, tmp_path: pathlib.Path, transport: str) -> None:
410 """The allowlist holds for a directory context (BuildKit applies the file) and for the stream push.sh uses (the stream applies it:
411 BuildKit does not read Dockerfile.dockerignore from a streamed context)."""
412 probe = subprocess.run(["docker", "info", "--format", "{{.ServerVersion}}"], capture_output=True, text=True, timeout=30)
413 assert probe.returncode == 0, "host_tools: a docker daemon is required"
414 ctx = tmp_path / "ctx"
415 files = {
416 # intended inputs
417 "musehub/requirements.txt": "a", "musehub/constraints.txt": "a", "musehub/grammar-requirements.txt": "a", "musehub/pyproject.toml": "a", "musehub/alembic.ini": "a",
418 "musehub/entrypoint.sh": "a",
419 "musehub/musehub/app.py": "a", "musehub/alembic/env.py": "a", "musehub/alembic/versions/0001_init.py": "a",
420 "musehub/docs/guide.md": "a", "musehub/deploy/deploy.sh": "a", "muse/pyproject.toml": "a", "muse/README.md": "a", "muse/muse/__init__.py": "a",
421 "musehub/docs/release-runbook.md": "a", "musehub/musehub/templates/musehub/static/app.css": "a",
422 # decoys that must never be in the image
423 "musehub/docs/work/session-handoff.md": "x", "musehub/docs/phase-6d-readiness.md": "x",
424 "musehub/stray.txt": "x", "musehub/.env": "x", "musehub/musehub.db": "x",
425 "musehub/node_modules/pkg/index.js": "x", "musehub/musehub/__pycache__/app.cpython-314.pyc": "x", "musehub/tests/test_x.py": "x",
426 "musehub/src/ts/app.ts": "x", "muse/.venv/bin/python": "x", "muse/dist/muse-1.tar.gz": "x", "muse/tests/test_y.py": "x",
427 "agentception/secret.txt": "x", "stray-root.txt": "x", "musehub/.git/config": "x", "musehub/.muse/repo.json": "x",
428 "musehub/musehub/local.db": "x", "musehub/.env.production": "x",
429 }
430 for rel, text in files.items():
431 (ctx / rel).parent.mkdir(parents=True, exist_ok=True)
432 (ctx / rel).write_text(text)
433 (ctx / "musehub" / "Dockerfile").write_text("FROM scratch\nCOPY . /ctx/\n")
434 shutil.copyfile(DOCKERIGNORE, ctx / "musehub" / "Dockerfile.dockerignore")
435 dest = tmp_path / "out"
436 if transport == "stream":
437 proc = stream_build(ctx, "musehub/Dockerfile", dest, "--no-cache")
438 assert proc.returncode == 0, proc.stderr.decode()[-1500:]
439 else:
440 proc = subprocess.run(
441 ["docker", "build", "--no-cache", "-f", str(ctx / "musehub" / "Dockerfile"), "--output", f"type=local,dest={dest}", str(ctx)],
442 capture_output=True, text=True, timeout=300, env={**os.environ, "DOCKER_BUILDKIT": "1"},
443 )
444 assert proc.returncode == 0, proc.stderr[-1500:]
445 got = {str(p.relative_to(dest / "ctx")) for p in (dest / "ctx").rglob("*") if p.is_file()}
446 intended = {k for k in files if k.startswith((
447 "musehub/requirements", "musehub/constraints", "musehub/pyproject", "musehub/alembic", "musehub/entrypoint", "musehub/musehub/app",
448 "musehub/docs/guide",
449 "musehub/docs/release-runbook", "musehub/musehub/templates/musehub/static/", "musehub/deploy", "muse/pyproject", "muse/README", "muse/muse/",
450 ))}
451 assert intended <= got, f"intended inputs missing: {sorted(intended - got)}"
452 assert got - intended - {"musehub/Dockerfile", "musehub/Dockerfile.dockerignore"} == set(), f"unexpected files in the image context: {sorted(got - intended)}"
453
454
455 def stream_build(ctx: pathlib.Path, dockerfile: str, dest: pathlib.Path, *flags: str) -> "subprocess.CompletedProcess[bytes]":
456 """``docker build`` of *ctx* exactly as push.sh does it: ``deploy.build_context --stream`` piped to the build on stdin."""
457 producer = subprocess.run([sys.executable, "-m", "deploy.build_context", "--stream", str(ctx)], capture_output=True, timeout=120, cwd=ROOT)
458 assert producer.returncode == 0, producer.stderr.decode()[-800:]
459 return subprocess.run(["docker", "build", *flags, "-f", dockerfile, "--output", f"type=local,dest={dest}", "-"], input=producer.stdout,
460 capture_output=True, timeout=300, env={**os.environ, "DOCKER_BUILDKIT": "1"})
461
462
463 def docker_available() -> bool:
464 if shutil.which("docker") is None:
465 return False
466 return subprocess.run(["docker", "info", "--format", "{{.ServerVersion}}"], capture_output=True, timeout=30).returncode == 0
467
468
469 # ── the context reaches the builder as a fresh stream (equal-size edits are never lost) ──
470
471
472 class TestContextStream:
473 """BuildKit transfers a *directory* context incrementally and treats a file as unchanged when size, mtime and mode match an earlier
474 transfer. Every context file has mtime 0, so an edit that keeps the byte length (a version bump, a pin) was silently not transferred
475 and the build used the previous content. A tar stream carries no such state."""
476
477 def test_the_stream_is_a_tar_of_every_file_with_normalised_ownership_and_exec_bits(self, tmp_path: pathlib.Path) -> None:
478 ctx = tmp_path / "ctx"
479 (ctx / "musehub").mkdir(parents=True)
480 (ctx / "musehub" / "entrypoint.sh").write_text("#!/bin/sh\n")
481 (ctx / "musehub" / "entrypoint.sh").chmod(0o755)
482 (ctx / "musehub" / "a.txt").write_text("a")
483 buf = io.BytesIO()
484 context_tar(ctx, buf)
485 with tarfile.open(fileobj=io.BytesIO(buf.getvalue())) as tf:
486 members = {m.name: m for m in tf.getmembers()}
487 assert members["musehub/entrypoint.sh"].mode & 0o111 == 0o111 and members["musehub/a.txt"].mode & 0o111 == 0
488 assert {(m.uid, m.gid, m.uname, m.gname) for m in members.values()} == {(0, 0, "", "")}
489 assert [m for m in members if m.startswith("./")] == []
490
491 def test_the_same_context_always_produces_the_same_bytes(self, tmp_path: pathlib.Path) -> None:
492 ctx = tmp_path / "ctx"
493 for name in ("z.txt", "a.txt", "m/b.txt"):
494 (ctx / name).parent.mkdir(parents=True, exist_ok=True)
495 (ctx / name).write_text(name)
496 first, second = io.BytesIO(), io.BytesIO()
497 context_tar(ctx, first)
498 os.utime(ctx / "a.txt", (12345, 12345))
499 context_tar(ctx, second)
500 assert first.getvalue() == second.getvalue()
501
502 @pytest.mark.parametrize("name", ["._env.py", ".DS_Store", "sub/._pyproject.toml", "sub/.DS_Store"])
503 def test_macos_metadata_can_never_enter_the_stream(self, tmp_path: pathlib.Path, name: str) -> None:
504 ctx = tmp_path / "ctx"
505 (ctx / name).parent.mkdir(parents=True, exist_ok=True)
506 (ctx / name).write_text("junk")
507 with pytest.raises(BuildContextError, match="macOS metadata"):
508 context_tar(ctx, io.BytesIO())
509
510 @pytest.mark.parametrize("name", ["musehub/._env.py"]) # muse ignores .DS_Store, so it cannot be tracked; the stream test covers it
511 def test_a_tracked_macos_metadata_file_refuses_the_context(self, eco: pathlib.Path, tmp_path: pathlib.Path, name: str) -> None:
512 (eco / name).write_text("junk")
513 muse(eco / "musehub", "code", "add", name.removeprefix("musehub/"))
514 muse(eco / "musehub", "commit", "-m", "metadata")
515 with pytest.raises(BuildContextError, match="macOS metadata"):
516 assemble(eco, out_dir(tmp_path))
517 assert not out_dir(tmp_path).exists()
518
519 @pytest.mark.host_tools # needs a docker daemon on the machine running pytest; opt in with -m host_tools
520 def test_an_equal_size_equal_mtime_edit_reaches_the_image(self, tmp_path: pathlib.Path) -> None:
521 ctx = tmp_path / "ctx"
522 (ctx / "musehub").mkdir(parents=True)
523 (ctx / "musehub" / "Dockerfile").write_text("FROM scratch\nCOPY musehub/pin.txt /pin.txt\n")
524 pin = ctx / "musehub" / "pin.txt"
525 for old, new in (("fastapi>=0.136.1", "fastapi>=0.138.0"), ("rc4", "rc5")): # same length, as the c17 and rc4->rc5 bumps were
526 assert len(old) == len(new)
527 outputs = []
528 for text in (old, new):
529 pin.write_text(text)
530 for path in (pin, ctx / "musehub" / "Dockerfile"):
531 os.utime(path, (0, 0)) # what muse archive and build_context leave on every file
532 dest = tmp_path / f"out-{len(outputs)}-{old[-3:]}"
533 proc = stream_build(ctx, "musehub/Dockerfile", dest, "--no-cache-filter=nonexistent")
534 assert proc.returncode == 0, proc.stderr.decode()[-1500:]
535 outputs.append((dest / "pin.txt").read_text())
536 assert outputs == [old, new]
537
538 @pytest.mark.host_tools # needs a docker daemon on the machine running pytest; opt in with -m host_tools
539 def test_the_image_holds_no_macos_metadata_even_when_the_host_tar_would_add_it(self, tmp_path: pathlib.Path) -> None:
540 ctx = tmp_path / "ctx"
541 (ctx / "musehub").mkdir(parents=True)
542 (ctx / "musehub" / "Dockerfile").write_text("FROM scratch\nCOPY musehub/ /app/\n")
543 (ctx / "musehub" / "env.py").write_text("x")
544 dest = tmp_path / "out"
545 proc = stream_build(ctx, "musehub/Dockerfile", dest)
546 assert proc.returncode == 0, proc.stderr.decode()[-1500:]
547 assert sorted(p.name for p in (dest / "app").iterdir()) == ["Dockerfile", "env.py"]
548
549
550 # ── dependency resolution ─────────────────────────────────────────────────────
551
552
553 def constraint_pins() -> dict[str, str]:
554 pins: dict[str, str] = {}
555 for line in CONSTRAINTS.read_text().splitlines():
556 line = line.split("#", 1)[0].strip()
557 if not line:
558 continue
559 m = re.fullmatch(r"([A-Za-z0-9][A-Za-z0-9._-]*)==([^=\s;]+)", line)
560 assert m, f"constraints.txt must hold exact pins only, got {line!r}"
561 name = canonicalize_name(m[1])
562 assert name not in pins, f"{name} is pinned twice"
563 pins[name] = m[2]
564 return pins
565
566
567 class TestDependencyConstraints:
568 """A rebuild installs exactly the verified versions, not whatever PyPI offers that day.
569
570 The runtime install resolves transitive dependencies from PyPI on every build (its stage is never
571 cached), so a new upstream release changed the image's packages between two builds of one revision.
572 """
573
574 def test_every_requirement_has_an_exact_pin_that_satisfies_it(self) -> None:
575 pins = constraint_pins()
576 for line in REQUIREMENTS.read_text().splitlines():
577 line = line.split("#", 1)[0].strip()
578 if not line:
579 continue
580 req = Requirement(line)
581 name = canonicalize_name(req.name)
582 assert name in pins, f"{req.name} is in requirements.txt but not pinned in constraints.txt"
583 assert req.specifier.contains(pins[name], prereleases=True), f"{req.name}=={pins[name]} does not satisfy {req}"
584
585 def test_the_engine_built_from_source_is_not_pinned(self) -> None:
586 assert "muse" not in constraint_pins(), "muse is built from the pinned engine revision, not resolved"
587
588 def test_both_resolution_steps_use_the_constraints(self) -> None:
589 src = DOCKERFILE.read_text()
590 builder, runtime = src.split("AS runtime", 1)
591 assert "COPY musehub/constraints.txt ." in builder
592 assert "pip wheel --no-cache-dir --no-deps --wheel-dir /app/wheels -c constraints.txt -r /dev/stdin" in builder, \
593 "the builder's requirements wheel build must be constrained"
594 assert builder.index("COPY musehub/constraints.txt .") > builder.index("cryptography==48.0.0"), \
595 "copy the constraints after the source builds so they keep their cache"
596 install = runtime[runtime.index("RUN --mount=type=bind,from=builder,source=/app/constraints.txt,target=/tmp/constraints.txt"):]
597 assert install.split("\n", 2)[1].strip().startswith("pip install --no-cache-dir -c /tmp/constraints.txt /wheels/*"), \
598 "the runtime install fetches transitive dependencies and must be constrained"
599
600
601 # ── push.sh / deploy.sh ───────────────────────────────────────────────────────
602
603
604 def code(path: pathlib.Path) -> str:
605 return "\n".join(line for line in path.read_text().splitlines() if not line.strip().startswith("#"))
606
607
608 class TestPushScript:
609 src = code(PUSH_SH)
610
611 def test_builds_from_the_assembled_context_not_the_working_tree(self) -> None:
612 assert "deploy.build_context" in self.src
613 build = self.src[self.src.index("deploy.build_context --stream"):]
614 assert '--stream "$CONTEXT_DIR"' in build.split("log \"Build complete.\"")[0]
615 assert '"$ECOSYSTEM_DIR"\n' not in build.split("log \"Build complete.\"")[0], "the working tree must not be the build context"
616 assert self.src.index("deploy.build_context") < self.src.index("docker build")
617
618 def test_the_context_is_streamed_never_handed_over_as_a_directory(self) -> None:
619 """A directory context loses same-size edits (TestContextStream); the build must read a tar from stdin."""
620 build = self.src[self.src.index("deploy.build_context --stream"):self.src.index('log "Build complete."')]
621 assert '--stream "$CONTEXT_DIR"' in build and "| docker build" in build
622 assert build.rstrip().endswith("-f musehub/Dockerfile -"), "the build context argument is stdin"
623 assert '"$CONTEXT_DIR"\n' not in build.split("docker build")[1], "no directory may follow docker build"
624 assert "pipefail" in self.src
625
626 def test_a_context_failure_stops_before_any_build(self) -> None:
627 assert "build context refused" in self.src and "nothing was built" in self.src
628
629 def test_revisions_are_passed_to_the_build_and_verified_on_the_image(self) -> None:
630 assert '--build-arg "MUSEHUB_REVISION=${MUSEHUB_REVISION}"' in self.src and '--build-arg "MUSE_REVISION=${MUSE_REVISION}"' in self.src
631 verify = self.src.index("docker image inspect")
632 assert self.src.index("docker build") < verify < self.src.index("docker save")
633 assert "org.opencontainers.image.revision" in self.src and "not pushing" in self.src
634
635 def test_there_is_no_latest_tag_and_no_local_fallback_revision(self) -> None:
636 assert ":latest" not in self.src and "latest" not in re.sub(r"There is no :latest", "", self.src)
637 assert 'echo "local"' not in self.src and "refusing to build an unidentifiable image" in self.src
638
639 def test_the_pushed_digest_is_reported(self) -> None:
640 assert 'crane digest "${image}:${IMAGE_TAG}"' in self.src and "@${digest}" in self.src
641
642 def test_deploy_sh_has_no_latest_default(self) -> None:
643 deploy = code(DEPLOY_SH)
644 assert 'IMAGE_TAG="${IMAGE_TAG:-latest}"' not in deploy and 'IMAGE_TAG="${IMAGE_TAG:-}"' in deploy
645 assert '[ -n "${IMAGE_TAG:-}" ] || die' in deploy
646
647
648 class TestDeployStaticCheck:
649 """musehub#239. /healthz is JSON-only: a release without its compiled UI assets passed it. The new slot must serve every asset
650 the build context produces before nginx sends it traffic."""
651
652 src = code(DEPLOY_SH)
653
654 def test_every_compiled_asset_is_checked_on_the_new_slot_before_traffic_moves(self) -> None: # SA_08
655 assets = re.search(r"^STATIC_ASSETS=\((?P<names>[^)]*)\)$", self.src, re.M)
656 assert assets and sorted(assets["names"].split()) == sorted(FRONTEND_ASSETS)
657 health = self.src.index('health_check "$HEALTH_URL" "$NEW_SLOT"')
658 check = self.src.index('static_check "http://127.0.0.1:${NEW_PORT}" "$NEW_SLOT"')
659 assert health < check < self.src.index('nginx_point_to "$NEW_SLOT"')
660
661 @pytest.mark.parametrize(("curl_out", "curl_exit", "passes"), [
662 ("200 1234", 0, True), ("404 22", 0, False), ("200 0", 0, False), ("000 0", 7, False),
663 ])
664 def test_a_missing_empty_or_unreachable_asset_stops_the_deploy(self, tmp_path: pathlib.Path, curl_out: str, curl_exit: int, passes: bool) -> None: # SA_09
665 text = DEPLOY_SH.read_text()
666 assets = re.search(r"^STATIC_ASSETS=\(.*?\)$", text, re.M)
667 func = re.search(r"^static_check\(\) \{.*?^\}", text, re.M | re.S)
668 assert assets and func
669 bindir = tmp_path / "bin"
670 bindir.mkdir()
671 (bindir / "curl").write_text(f"#!/bin/sh\nprintf '%s' '{curl_out}'\nexit {curl_exit}\n")
672 (bindir / "curl").chmod(0o755)
673 script = f'set -euo pipefail\nlog() {{ :; }}\ndie() {{ echo "$*" >&2; exit 1; }}\n{assets[0]}\n{func[0]}\nstatic_check http://127.0.0.1:1338 green\n'
674 proc = subprocess.run(["bash", "-c", script], capture_output=True, text=True, timeout=30, env={**os.environ, "PATH": f"{bindir}:/usr/bin:/bin"})
675 assert (proc.returncode == 0) is passes, proc.stderr
676 if not passes:
677 assert "not switching traffic" in proc.stderr
File History 7 commits
sha256:b4b999434d042d74edc7032f1f2cfd20fda5b3580fe85966351db5b8eef0abb3 feat(intel): every grammar on the hub, and every intel job … Opus 5.5 minor ⚠ 1 day 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:414893924229cfb6dd5a5154d9526c1a9b57a2721289daba6ad89a6c98c64c09 fix(deploy): stream the Docker build context so same-size e… Sonnet 5.5 patch 5 days ago
sha256:70e2deebbf676f002c5122f381222eb21fe65b51c04380538ea9acb3fe62dfc5 build: install the verified dependency versions instead of … Opus 5.5 minor ⚠ 11 days ago
sha256:6b75088fadf04c065f8c92b3b71f8c7ffe315d0e41ab71e4dc7b0b9e99f29426 fix(deploy): build the compiled UI assets into the pinned b… Opus 5.5 minor ⚠ 12 days ago
sha256:3c6d2e2751e9222a0872ba699f16baa457d9990c1998c20956badb9186ee69ee fix(deploy): image built from committed files only, with re… Opus 5.5 minor ⚠ 12 days ago