gabriel / musehub public
test_build_context.py python
565 lines 30.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 re
17 import shutil
18 import subprocess
19
20 import pytest
21 from packaging.requirements import Requirement
22 from packaging.utils import canonicalize_name
23
24 from deploy import build_context
25 from deploy.build_context import FRONTEND_ASSETS, MIGRATIONS, REQUIRED, STATIC_DIR, BuildContextError, assemble, main
26
27 ROOT = pathlib.Path(__file__).resolve().parents[1]
28 DOCKERFILE = ROOT / "Dockerfile"
29 DOCKERIGNORE = ROOT / "Dockerfile.dockerignore"
30 REQUIREMENTS = ROOT / "requirements.txt"
31 CONSTRAINTS = ROOT / "constraints.txt"
32 PUSH_SH = ROOT / "deploy" / "push.sh"
33 DEPLOY_SH = ROOT / "deploy" / "deploy.sh"
34
35
36 # ── npm test double ───────────────────────────────────────────────────────────
37
38 #: Logs "<cwd> <args>", installs nothing, and "builds" each asset from its committed source, so a test can tell a
39 #: built asset from a stray working-tree file. FAKE_NPM_FAIL=<script> fails that script; FAKE_NPM_EMPTY=<asset>
40 #: writes that asset empty.
41 FAKE_NPM = r"""#!/bin/sh
42 printf '%s %s\n' "$PWD" "$*" >> "$FAKE_NPM_LOG"
43 case "$1" in
44 ci) mkdir -p node_modules/.fake; exit 0 ;;
45 run) ;;
46 *) exit 2 ;;
47 esac
48 if [ "$2" = "--silent" ]; then script="$3"; else script="$2"; fi
49 if [ "$script" = "$FAKE_NPM_FAIL" ]; then echo "boom: $script" >&2; exit 1; fi
50 out=musehub/templates/musehub/static
51 mkdir -p "$out"
52 case "$script" in
53 build:js) name=app.js; src=src/ts/app.ts ;;
54 build:css) name=app.css; src=src/scss/app.scss ;;
55 build:embed) name=embed.css; src=src/scss/embed.scss ;;
56 *) echo "unknown script $script" >&2; exit 1 ;;
57 esac
58 if [ "$name" = "$FAKE_NPM_EMPTY" ]; then : > "$out/$name"; else { printf 'built:'; cat "$src"; } > "$out/$name"; fi
59 """
60
61 #: compiled asset -> the committed source the fake npm builds it from
62 ASSET_SOURCES = {"app.js": "src/ts/app.ts", "app.css": "src/scss/app.scss", "embed.css": "src/scss/embed.scss"}
63
64
65 @pytest.fixture(autouse=True)
66 def fake_npm(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path:
67 bindir = tmp_path / "fake-bin"
68 bindir.mkdir()
69 (bindir / "npm").write_text(FAKE_NPM)
70 (bindir / "npm").chmod(0o755)
71 log = tmp_path / "npm.log"
72 monkeypatch.setenv("FAKE_NPM_LOG", str(log))
73 monkeypatch.setenv("PATH", f"{bindir}{os.pathsep}{os.environ['PATH']}")
74 return log
75
76
77 # ── throw-away Muse repositories ──────────────────────────────────────────────
78
79
80 @pytest.fixture
81 def home(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path:
82 h = tmp_path / "home"
83 h.mkdir()
84 monkeypatch.setenv("HOME", str(h))
85 return h
86
87
88 def muse(repo: pathlib.Path, *args: str) -> str:
89 proc = subprocess.run(["muse", "-C", str(repo), *args], capture_output=True, text=True, timeout=120)
90 assert proc.returncode == 0, f"muse {args}: {proc.stderr or proc.stdout}"
91 return proc.stdout
92
93
94 def init_repo(repo: pathlib.Path, files: dict[str, str]) -> None:
95 repo.mkdir(parents=True)
96 subprocess.run(["muse", "init", "--domain", "code", str(repo)], capture_output=True, check=True, timeout=60)
97 for rel, text in files.items():
98 (repo / rel).parent.mkdir(parents=True, exist_ok=True)
99 (repo / rel).write_text(text)
100 muse(repo, "code", "add", *files)
101 muse(repo, "commit", "-m", "fixture")
102
103
104 def tracked(base: dict[str, str] | None = None) -> dict[str, dict[str, str]]:
105 """The committed content of both fixture repositories: every REQUIRED input plus migrations."""
106 hub = {
107 "Dockerfile": "FROM scratch\n", "Dockerfile.dockerignore": "**\n", "requirements.txt": "fastapi\n", "constraints.txt": "fastapi==0.1\n",
108 "pyproject.toml": "[project]\n",
109 "alembic.ini": "[alembic]\n", "entrypoint.sh": "#!/bin/sh\n", "musehub/__init__.py": "", "alembic/env.py": "",
110 "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",
111 "package.json": '{"name": "fixture"}\n', "package-lock.json": '{"lockfileVersion": 3}\n', "tsconfig.json": "{}\n",
112 "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",
113 }
114 lib = {"pyproject.toml": "[project]\n", "README.md": "# muse\n", "muse/__init__.py": ""}
115 return {"musehub": {**hub, **(base or {})}, "muse": lib}
116
117
118 @pytest.fixture
119 def eco(tmp_path: pathlib.Path, home: pathlib.Path) -> pathlib.Path:
120 root = tmp_path / "ecosystem"
121 root.mkdir()
122 spec = tracked()
123 for name, files in spec.items():
124 init_repo(root / name, files)
125 return root
126
127
128 def out_dir(tmp_path: pathlib.Path) -> pathlib.Path:
129 return tmp_path / "ctx"
130
131
132 def listing(root: pathlib.Path) -> list[str]:
133 return sorted(str(p.relative_to(root)) for p in root.rglob("*") if p.is_file())
134
135
136 # ── exclusion ─────────────────────────────────────────────────────────────────
137
138
139 class TestUntrackedFilesNeverEnter:
140 def test_untracked_handoff_document_is_excluded_and_not_deleted(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
141 handoff = eco / "musehub" / "docs" / "work" / "session-handoff.md"
142 handoff.parent.mkdir(parents=True)
143 handoff.write_text("local notes\n")
144 info = assemble(eco, out_dir(tmp_path))
145 assert not (out_dir(tmp_path) / "musehub/docs/work").exists()
146 assert "docs/work/session-handoff.md" in info["repositories"]["musehub"]["untracked_excluded"]
147 assert handoff.read_text() == "local notes\n", "the source file is left exactly where it was"
148
149 def test_any_unexpected_local_file_is_excluded(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
150 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"):
151 (eco / rel).parent.mkdir(parents=True, exist_ok=True)
152 (eco / rel).write_text("local\n")
153 (eco / "agentception").mkdir()
154 (eco / "agentception" / "secret.txt").write_text("not ours\n")
155 assemble(eco, out_dir(tmp_path))
156 names = listing(out_dir(tmp_path))
157 for leaked in ("stray.txt", "scratch.py", "local-notes.sh", "wip.py", "muse-9.tar.gz", ".env", "secret.txt"):
158 assert not any(n.endswith(leaked) for n in names), leaked
159 assert not (out_dir(tmp_path) / "agentception").exists()
160
161 def test_muse_internals_are_never_exported(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
162 assemble(eco, out_dir(tmp_path))
163 assert not any(".muse/" in n or n.startswith(".muse") for n in listing(out_dir(tmp_path)))
164
165 def test_the_context_is_exactly_the_committed_files_plus_build_info_and_compiled_assets(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
166 assemble(eco, out_dir(tmp_path))
167 want = sorted(
168 [f"musehub/{p}" for p in tracked()["musehub"]] + [f"muse/{p}" for p in tracked()["muse"]] + ["build-info.json"]
169 + [f"{STATIC_DIR}/{name}" for name in FRONTEND_ASSETS]
170 )
171 assert listing(out_dir(tmp_path)) == want
172
173 def test_two_assemblies_are_identical(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
174 a, b = out_dir(tmp_path), tmp_path / "ctx2"
175 assemble(eco, a)
176 assemble(eco, b)
177 assert listing(a) == listing(b)
178 assert all((a / n).read_bytes() == (b / n).read_bytes() for n in listing(a))
179
180
181 # ── inclusion ─────────────────────────────────────────────────────────────────
182
183
184 class TestRequiredInputs:
185 def test_application_files_and_every_migration_are_present(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
186 assemble(eco, out_dir(tmp_path))
187 for required in REQUIRED:
188 assert (out_dir(tmp_path) / required).exists(), required
189 assert sorted(p.name for p in (out_dir(tmp_path) / MIGRATIONS).glob("*.py")) == ["0001_init.py", "0002_more.py"]
190
191 @pytest.mark.parametrize("missing", ["entrypoint.sh", "requirements.txt", "constraints.txt", "alembic.ini", "musehub/__init__.py", "Dockerfile.dockerignore",
192 "deploy/deploy.sh", "package.json", "package-lock.json"])
193 def test_build_fails_when_a_required_application_input_is_not_committed(self, tmp_path: pathlib.Path, home: pathlib.Path, missing: str) -> None:
194 eco = tmp_path / "e"
195 eco.mkdir()
196 spec = tracked()
197 del spec["musehub"][missing]
198 for name, files in spec.items():
199 init_repo(eco / name, files)
200 with pytest.raises(BuildContextError, match="missing from the exported tree"):
201 assemble(eco, out_dir(tmp_path))
202 assert not out_dir(tmp_path).exists(), "no half-assembled context is left behind"
203
204 def test_build_fails_when_the_muse_engine_inputs_are_missing(self, tmp_path: pathlib.Path, home: pathlib.Path) -> None:
205 eco = tmp_path / "e"
206 eco.mkdir()
207 spec = tracked()
208 del spec["muse"]["README.md"]
209 for name, files in spec.items():
210 init_repo(eco / name, files)
211 with pytest.raises(BuildContextError, match="muse/README.md"):
212 assemble(eco, out_dir(tmp_path))
213
214 def test_build_fails_when_no_migrations_are_committed(self, tmp_path: pathlib.Path, home: pathlib.Path) -> None:
215 eco = tmp_path / "e"
216 eco.mkdir()
217 spec = tracked()
218 spec["musehub"] = {k: v for k, v in spec["musehub"].items() if "versions" not in k}
219 for name, files in spec.items():
220 init_repo(eco / name, files)
221 with pytest.raises(BuildContextError, match="no migrations"):
222 assemble(eco, out_dir(tmp_path))
223
224 def test_a_migration_missing_from_the_export_is_detected(self, eco: pathlib.Path, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None:
225 real = build_context._export
226
227 def truncating(repo: pathlib.Path, revision: str, out: pathlib.Path, scratch: pathlib.Path, runner: object) -> list[str]:
228 names = real(repo, revision, out, scratch, runner) # type: ignore[arg-type]
229 if repo.name == "musehub":
230 (out / MIGRATIONS / "0002_more.py").unlink()
231 return names
232
233 monkeypatch.setattr(build_context, "_export", truncating)
234 with pytest.raises(BuildContextError, match="exported migrations differ"):
235 assemble(eco, out_dir(tmp_path))
236 assert not out_dir(tmp_path).exists()
237
238
239 # ── compiled UI assets ────────────────────────────────────────────────────────
240
241
242 class TestCompiledUiAssets:
243 """musehub#239. The app serves app.js/app.css/embed.css from /static. They are build outputs (.museignore), so an export of
244 committed files never contains them; the context builds them from the committed sources."""
245
246 def test_every_asset_is_in_the_context_built_from_the_committed_sources(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None: # SA_01
247 assemble(eco, out_dir(tmp_path))
248 assert sorted(FRONTEND_ASSETS) == sorted(ASSET_SOURCES)
249 for name, src in ASSET_SOURCES.items():
250 assert (out_dir(tmp_path) / STATIC_DIR / name).read_bytes() == b"built:" + (eco / "musehub" / src).read_bytes(), name
251
252 def test_build_info_records_each_asset_digest(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None: # SA_02
253 info = assemble(eco, out_dir(tmp_path))
254 for name in FRONTEND_ASSETS:
255 data = (out_dir(tmp_path) / STATIC_DIR / name).read_bytes()
256 assert info["frontend_assets"][name] == {"sha256": hashlib.sha256(data).hexdigest(), "bytes": len(data)}
257
258 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
259 stale = eco / STATIC_DIR / "app.css"
260 stale.parent.mkdir(parents=True, exist_ok=True)
261 stale.write_text("stale local build\n")
262 (eco / "musehub" / "node_modules" / ".bin").mkdir(parents=True)
263 assemble(eco, out_dir(tmp_path))
264 assert (out_dir(tmp_path) / STATIC_DIR / "app.css").read_bytes().startswith(b"built:")
265 cwds = {line.split(" ", 1)[0] for line in fake_npm.read_text().splitlines()}
266 assert cwds and not [c for c in cwds if c.startswith((str(eco), str(out_dir(tmp_path))))], cwds
267 assert stale.read_text() == "stale local build\n", "the working tree is left exactly as it was"
268
269 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
270 assemble(eco, out_dir(tmp_path))
271 calls = [line.split(" ", 1)[1] for line in fake_npm.read_text().splitlines()]
272 assert calls[0].split()[:2] == ["ci", "--ignore-scripts"]
273 assert sorted(calls[1:]) == sorted(f"run --silent {script}" for script in FRONTEND_ASSETS.values())
274 assert not [n for n in listing(out_dir(tmp_path)) if "node_modules" in n]
275
276 @pytest.mark.parametrize("env", [{"FAKE_NPM_FAIL": "build:css"}, {"FAKE_NPM_EMPTY": "app.js"}])
277 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
278 for key, value in env.items():
279 monkeypatch.setenv(key, value)
280 with pytest.raises(BuildContextError, match="frontend"):
281 assemble(eco, out_dir(tmp_path))
282 assert not out_dir(tmp_path).exists()
283
284 def test_no_npm_refuses_the_build(self, eco: pathlib.Path, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None: # SA_06
285 monkeypatch.setattr(build_context, "_npm", lambda: None)
286 with pytest.raises(BuildContextError, match="npm"):
287 assemble(eco, out_dir(tmp_path))
288 assert not out_dir(tmp_path).exists()
289
290
291 # ── provenance and refusal ────────────────────────────────────────────────────
292
293
294 class TestRevisionIdentity:
295 def test_build_info_records_both_repositories_muse_revisions(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
296 info = assemble(eco, out_dir(tmp_path))
297 for name in ("musehub", "muse"):
298 head = json.loads(muse(eco / name, "rev-parse", "HEAD", "--json"))["commit_id"]
299 assert info["repositories"][name]["revision"] == head
300 assert json.loads((out_dir(tmp_path) / "build-info.json").read_text()) == info
301
302 def test_tracked_modifications_refuse_the_build(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
303 (eco / "musehub" / "entrypoint.sh").write_text("#!/bin/sh\necho changed\n")
304 with pytest.raises(BuildContextError, match="tracked changes"):
305 assemble(eco, out_dir(tmp_path))
306 assert not out_dir(tmp_path).exists()
307
308 def test_a_tracked_deletion_refuses_the_build(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
309 (eco / "muse" / "README.md").unlink()
310 with pytest.raises(BuildContextError, match="tracked changes"):
311 assemble(eco, out_dir(tmp_path))
312
313 def test_changes_in_the_engine_repo_refuse_too(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
314 (eco / "muse" / "muse" / "__init__.py").write_text("x = 1\n")
315 with pytest.raises(BuildContextError, match="muse has tracked changes"):
316 assemble(eco, out_dir(tmp_path))
317
318 def test_a_directory_that_is_not_a_muse_repo_is_refused(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
319 shutil.rmtree(eco / "muse" / ".muse")
320 with pytest.raises(BuildContextError, match="cannot determine the revision with Muse"):
321 assemble(eco, out_dir(tmp_path))
322
323 def test_a_missing_repository_is_refused(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
324 shutil.rmtree(eco / "muse")
325 with pytest.raises(BuildContextError, match="not found"):
326 assemble(eco, out_dir(tmp_path))
327
328 def test_muse_unavailable_is_refused(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
329 def broken(*_a: object, **_k: object) -> subprocess.CompletedProcess[str]:
330 raise FileNotFoundError("muse")
331
332 with pytest.raises(BuildContextError, match="cannot determine the revision"):
333 assemble(eco, out_dir(tmp_path), runner=broken)
334
335 def test_an_existing_output_directory_is_never_reused(self, eco: pathlib.Path, tmp_path: pathlib.Path) -> None:
336 out_dir(tmp_path).mkdir()
337 (out_dir(tmp_path) / "old.txt").write_text("stale")
338 with pytest.raises(BuildContextError, match="never reused"):
339 assemble(eco, out_dir(tmp_path))
340 assert (out_dir(tmp_path) / "old.txt").read_text() == "stale"
341
342 def test_cli_prints_the_info_and_refuses_with_status_1(self, eco: pathlib.Path, tmp_path: pathlib.Path, capsys: pytest.CaptureFixture[str]) -> None:
343 assert main(["--ecosystem", str(eco), "--out", str(out_dir(tmp_path))]) == 0
344 assert json.loads(capsys.readouterr().out)["schema"] == 1
345 assert main(["--ecosystem", str(eco), "--out", str(out_dir(tmp_path))]) == 1
346 assert "BUILD CONTEXT REFUSED" in capsys.readouterr().err
347
348
349 # ── Dockerfile and ignore file ────────────────────────────────────────────────
350
351
352 def copy_sources() -> list[str]:
353 sources = []
354 for line in DOCKERFILE.read_text().splitlines():
355 m = re.match(r"\s*COPY\s+(?P<flags>(?:--\S+\s+)*)(?P<rest>.+)$", line)
356 if not m or "--from" in m["flags"]:
357 continue
358 parts = m["rest"].split()
359 sources += [p.rstrip("/") for p in parts[:-1]]
360 return sources
361
362
363 def allowed() -> set[str]:
364 return {line[1:].strip().rstrip("/") for line in DOCKERIGNORE.read_text().splitlines() if line.startswith("!")}
365
366
367 class TestDockerfileAllowlist:
368 def test_every_copy_source_is_allowlisted(self) -> None:
369 sources = copy_sources()
370 assert sources, "no COPY lines parsed"
371 wl = allowed()
372 for src in sources:
373 bases = {a.removesuffix("/**") for a in wl}
374 narrowed = any(b.startswith(src + "/") for b in bases) # a directory COPY the allowlist narrows to named children
375 inside = any(src == b or src.startswith(b + "/") for b in bases)
376 assert inside or narrowed, f"{src} is COPYed but not allowlisted"
377
378 def test_every_allowlisted_path_is_something_the_dockerfile_copies_or_a_child_of_it(self) -> None:
379 sources = copy_sources()
380 for entry in allowed():
381 base = entry.removesuffix("/**")
382 assert any(base == s or base.startswith(s + "/") for s in sources), f"{entry} is allowlisted but the Dockerfile never copies it"
383
384 def test_default_is_deny_and_work_notes_are_denied_last(self) -> None:
385 lines = [line for line in DOCKERIGNORE.read_text().splitlines() if line and not line.startswith("#")]
386 assert lines[0] == "**"
387 assert lines.index("musehub/docs/work/**") > max(i for i, line in enumerate(lines) if line.startswith("!")), "denials must follow the allowlist"
388
389 def test_the_phase_6d_working_note_is_denied_by_its_exact_path_only(self) -> None: # SA_07
390 lines = [line for line in DOCKERIGNORE.read_text().splitlines() if line and not line.startswith("#")]
391 last_allow = max(i for i, line in enumerate(lines) if line.startswith("!"))
392 assert lines.index("musehub/docs/phase-6d-readiness.md") > last_allow
393 assert {line for line in lines[last_allow + 1:] if "docs" in line} == {
394 "musehub/docs/work", "musehub/docs/work/**", "musehub/docs/phase-6d-readiness.md",
395 }, "no other documentation is excluded"
396 assert "!musehub/docs/**" in DOCKERIGNORE.read_text().splitlines(), "the documentation allowlist is unchanged"
397
398 def test_the_image_records_both_revisions_as_labels(self) -> None:
399 src = DOCKERFILE.read_text()
400 assert 'ARG MUSEHUB_REVISION=""' in src and 'ARG MUSE_REVISION=""' in src
401 assert 'org.opencontainers.image.revision="${MUSEHUB_REVISION}"' in src
402 assert 'ai.musehub.muse-engine-revision="${MUSE_REVISION}"' in src
403
404 @pytest.mark.skipif(shutil.which("docker") is None, reason="no docker CLI")
405 def test_buildkit_applies_the_real_allowlist_to_a_context_full_of_decoys(self, tmp_path: pathlib.Path) -> None:
406 probe = subprocess.run(["docker", "info", "--format", "{{.ServerVersion}}"], capture_output=True, text=True, timeout=30)
407 if probe.returncode != 0:
408 pytest.skip("docker daemon not available")
409 ctx = tmp_path / "ctx"
410 files = {
411 # intended inputs
412 "musehub/requirements.txt": "a", "musehub/constraints.txt": "a", "musehub/pyproject.toml": "a", "musehub/alembic.ini": "a",
413 "musehub/entrypoint.sh": "a",
414 "musehub/musehub/app.py": "a", "musehub/alembic/env.py": "a", "musehub/alembic/versions/0001_init.py": "a",
415 "musehub/docs/guide.md": "a", "musehub/deploy/deploy.sh": "a", "muse/pyproject.toml": "a", "muse/README.md": "a", "muse/muse/__init__.py": "a",
416 "musehub/docs/release-runbook.md": "a", "musehub/musehub/templates/musehub/static/app.css": "a",
417 # decoys that must never be in the image
418 "musehub/docs/work/session-handoff.md": "x", "musehub/docs/phase-6d-readiness.md": "x",
419 "musehub/stray.txt": "x", "musehub/.env": "x", "musehub/musehub.db": "x",
420 "musehub/node_modules/pkg/index.js": "x", "musehub/musehub/__pycache__/app.cpython-314.pyc": "x", "musehub/tests/test_x.py": "x",
421 "musehub/src/ts/app.ts": "x", "muse/.venv/bin/python": "x", "muse/dist/muse-1.tar.gz": "x", "muse/tests/test_y.py": "x",
422 "agentception/secret.txt": "x", "stray-root.txt": "x", "musehub/.git/config": "x", "musehub/.muse/repo.json": "x",
423 "musehub/musehub/local.db": "x", "musehub/.env.production": "x",
424 }
425 for rel, text in files.items():
426 (ctx / rel).parent.mkdir(parents=True, exist_ok=True)
427 (ctx / rel).write_text(text)
428 (ctx / "musehub" / "Dockerfile").write_text("FROM scratch\nCOPY . /ctx/\n")
429 shutil.copyfile(DOCKERIGNORE, ctx / "musehub" / "Dockerfile.dockerignore")
430 dest = tmp_path / "out"
431 proc = subprocess.run(
432 ["docker", "build", "--no-cache", "-f", str(ctx / "musehub" / "Dockerfile"), "--output", f"type=local,dest={dest}", str(ctx)],
433 capture_output=True, text=True, timeout=300, env={**os.environ, "DOCKER_BUILDKIT": "1"},
434 )
435 assert proc.returncode == 0, proc.stderr[-1500:]
436 got = {str(p.relative_to(dest / "ctx")) for p in (dest / "ctx").rglob("*") if p.is_file()}
437 intended = {k for k in files if k.startswith((
438 "musehub/requirements", "musehub/constraints", "musehub/pyproject", "musehub/alembic", "musehub/entrypoint", "musehub/musehub/app",
439 "musehub/docs/guide",
440 "musehub/docs/release-runbook", "musehub/musehub/templates/musehub/static/", "musehub/deploy", "muse/pyproject", "muse/README", "muse/muse/",
441 ))}
442 assert intended <= got, f"intended inputs missing: {sorted(intended - got)}"
443 assert got - intended - {"musehub/Dockerfile", "musehub/Dockerfile.dockerignore"} == set(), f"unexpected files in the image context: {sorted(got - intended)}"
444
445
446 # ── dependency resolution ─────────────────────────────────────────────────────
447
448
449 def constraint_pins() -> dict[str, str]:
450 pins: dict[str, str] = {}
451 for line in CONSTRAINTS.read_text().splitlines():
452 line = line.split("#", 1)[0].strip()
453 if not line:
454 continue
455 m = re.fullmatch(r"([A-Za-z0-9][A-Za-z0-9._-]*)==([^=\s;]+)", line)
456 assert m, f"constraints.txt must hold exact pins only, got {line!r}"
457 name = canonicalize_name(m[1])
458 assert name not in pins, f"{name} is pinned twice"
459 pins[name] = m[2]
460 return pins
461
462
463 class TestDependencyConstraints:
464 """A rebuild installs exactly the verified versions, not whatever PyPI offers that day.
465
466 The runtime install resolves transitive dependencies from PyPI on every build (its stage is never
467 cached), so a new upstream release changed the image's packages between two builds of one revision.
468 """
469
470 def test_every_requirement_has_an_exact_pin_that_satisfies_it(self) -> None:
471 pins = constraint_pins()
472 for line in REQUIREMENTS.read_text().splitlines():
473 line = line.split("#", 1)[0].strip()
474 if not line:
475 continue
476 req = Requirement(line)
477 name = canonicalize_name(req.name)
478 assert name in pins, f"{req.name} is in requirements.txt but not pinned in constraints.txt"
479 assert req.specifier.contains(pins[name], prereleases=True), f"{req.name}=={pins[name]} does not satisfy {req}"
480
481 def test_the_engine_built_from_source_is_not_pinned(self) -> None:
482 assert "muse" not in constraint_pins(), "muse is built from the pinned engine revision, not resolved"
483
484 def test_both_resolution_steps_use_the_constraints(self) -> None:
485 src = DOCKERFILE.read_text()
486 builder, runtime = src.split("AS runtime", 1)
487 assert "COPY musehub/constraints.txt ." in builder
488 assert "pip wheel --no-cache-dir --no-deps --wheel-dir /app/wheels -c constraints.txt -r /dev/stdin" in builder, \
489 "the builder's requirements wheel build must be constrained"
490 assert builder.index("COPY musehub/constraints.txt .") > builder.index("cryptography==48.0.0"), \
491 "copy the constraints after the source builds so they keep their cache"
492 install = runtime[runtime.index("RUN --mount=type=bind,from=builder,source=/app/constraints.txt,target=/tmp/constraints.txt"):]
493 assert install.split("\n", 2)[1].strip().startswith("pip install --no-cache-dir -c /tmp/constraints.txt /wheels/*"), \
494 "the runtime install fetches transitive dependencies and must be constrained"
495
496
497 # ── push.sh / deploy.sh ───────────────────────────────────────────────────────
498
499
500 def code(path: pathlib.Path) -> str:
501 return "\n".join(line for line in path.read_text().splitlines() if not line.strip().startswith("#"))
502
503
504 class TestPushScript:
505 src = code(PUSH_SH)
506
507 def test_builds_from_the_assembled_context_not_the_working_tree(self) -> None:
508 assert "deploy.build_context" in self.src
509 build = self.src[self.src.index("docker build"):]
510 assert '"$CONTEXT_DIR"' in build.split("log \"Build complete.\"")[0]
511 assert '"$ECOSYSTEM_DIR"\n' not in build.split("log \"Build complete.\"")[0], "the working tree must not be the build context"
512 assert self.src.index("deploy.build_context") < self.src.index("docker build")
513
514 def test_a_context_failure_stops_before_any_build(self) -> None:
515 assert "build context refused" in self.src and "nothing was built" in self.src
516
517 def test_revisions_are_passed_to_the_build_and_verified_on_the_image(self) -> None:
518 assert '--build-arg "MUSEHUB_REVISION=${MUSEHUB_REVISION}"' in self.src and '--build-arg "MUSE_REVISION=${MUSE_REVISION}"' in self.src
519 verify = self.src.index("docker image inspect")
520 assert self.src.index("docker build") < verify < self.src.index("docker save")
521 assert "org.opencontainers.image.revision" in self.src and "not pushing" in self.src
522
523 def test_there_is_no_latest_tag_and_no_local_fallback_revision(self) -> None:
524 assert ":latest" not in self.src and "latest" not in re.sub(r"There is no :latest", "", self.src)
525 assert 'echo "local"' not in self.src and "refusing to build an unidentifiable image" in self.src
526
527 def test_the_pushed_digest_is_reported(self) -> None:
528 assert 'crane digest "${image}:${IMAGE_TAG}"' in self.src and "@${digest}" in self.src
529
530 def test_deploy_sh_has_no_latest_default(self) -> None:
531 deploy = code(DEPLOY_SH)
532 assert 'IMAGE_TAG="${IMAGE_TAG:-latest}"' not in deploy and 'IMAGE_TAG="${IMAGE_TAG:-}"' in deploy
533 assert '[ -n "${IMAGE_TAG:-}" ] || die' in deploy
534
535
536 class TestDeployStaticCheck:
537 """musehub#239. /healthz is JSON-only: a release without its compiled UI assets passed it. The new slot must serve every asset
538 the build context produces before nginx sends it traffic."""
539
540 src = code(DEPLOY_SH)
541
542 def test_every_compiled_asset_is_checked_on_the_new_slot_before_traffic_moves(self) -> None: # SA_08
543 assets = re.search(r"^STATIC_ASSETS=\((?P<names>[^)]*)\)$", self.src, re.M)
544 assert assets and sorted(assets["names"].split()) == sorted(FRONTEND_ASSETS)
545 health = self.src.index('health_check "$HEALTH_URL" "$NEW_SLOT"')
546 check = self.src.index('static_check "http://127.0.0.1:${NEW_PORT}" "$NEW_SLOT"')
547 assert health < check < self.src.index('nginx_point_to "$NEW_SLOT"')
548
549 @pytest.mark.parametrize(("curl_out", "curl_exit", "passes"), [
550 ("200 1234", 0, True), ("404 22", 0, False), ("200 0", 0, False), ("000 0", 7, False),
551 ])
552 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
553 text = DEPLOY_SH.read_text()
554 assets = re.search(r"^STATIC_ASSETS=\(.*?\)$", text, re.M)
555 func = re.search(r"^static_check\(\) \{.*?^\}", text, re.M | re.S)
556 assert assets and func
557 bindir = tmp_path / "bin"
558 bindir.mkdir()
559 (bindir / "curl").write_text(f"#!/bin/sh\nprintf '%s' '{curl_out}'\nexit {curl_exit}\n")
560 (bindir / "curl").chmod(0o755)
561 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'
562 proc = subprocess.run(["bash", "-c", script], capture_output=True, text=True, timeout=30, env={**os.environ, "PATH": f"{bindir}:/usr/bin:/bin"})
563 assert (proc.returncode == 0) is passes, proc.stderr
564 if not passes:
565 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 6 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