Fix #4603: stage fake install bundles in a temp package root, lock wheel builds

Assisted-by: AI coding agent (author: wanjinhao1)
(cherry picked from commit 60140356c0)
This commit is contained in:
wanjinhao1 authored and safishamsi committed 2026-11-05 12:12:04 +00:00
1 parent 4c16a79817
commit 63cf60dd22
2 files changed
+116 -64

No files matched your search

+75 -58
View File
@@ -5,17 +5,14 @@ claude and its reuse twins (antigravity, kimi) install progressively: a lean
SKILL.md plus a references/ sidecar. Every other host whose bundle has not
shipped yet still installs today's byte-identical monolith.
The plumbing tests below stage a hand-made fake bundle in claude's slot so the
dir-copy, version-stamp, reinstall, and uninstall flow can be exercised with
fixed, asserted content. The fixture backs up the real committed bundle and
restores it on teardown, so the working tree is never disturbed.
The plumbing tests below stage a hand-made fake bundle in a throwaway package
root (not in the real package), so the working tree is never disturbed.
"""
from __future__ import annotations
import os
import shutil
import sys
import tempfile
from pathlib import Path
from unittest.mock import patch
@@ -28,40 +25,77 @@ import graphify.__main__ as mainmod
PKG_DIR = Path(graphify.__file__).parent
class _FakeBundle(str):
"""The platform name, carrying the staged fake package root.
A str subclass so existing ``platform = fake_bundle`` call sites keep
working while tests that need the staged bundle path (the read-only-package
test) can read ``fake_bundle.pkg_root``.
"""
pkg_root: Path
def _stage_fake_package(root: Path, *, references: "tuple[str, str] | None" = ("# extraction spec fragment\n", "# query fragment\n")) -> Path:
"""Materialise a throwaway mini graphify package for the install tests.
The installer resolves every packaged asset relative to
``graphify.install.__file__`` (skill bodies, ``skills/<bundle>/references/``,
``always_on/*.md``, ``command-kilo.md``). Pointing that ``__file__`` at a
fake package root under ``tmp_path`` lets the tests stage their fake bundle
there instead of moving the real committed bundle out of the on-disk
package: a killed run (SIGTERM/SIGKILL runs no ``finally`` blocks) used to
leave the fake bundle in the package, dirtying the checkout, and a second
pytest on the same checkout read a package whose bundle was moved away
(#4603). With the fake root nothing under the real package is ever touched,
so nothing can be left behind.
``references=None`` stages the malformed-bundle layout instead: a bundle dir
present with no ``references/`` subdir (the hard-fail case).
"""
root.mkdir(parents=True, exist_ok=True)
# Monolith bodies and the kilo command file, so monolith-platform installs
# through the fake root keep working.
for pattern in ("skill*.md", "command-kilo.md"):
for packaged in PKG_DIR.glob(pattern):
shutil.copy2(packaged, root / packaged.name)
shutil.copytree(PKG_DIR / "always_on", root / "always_on")
shutil.copytree(PKG_DIR / "skills", root / "skills")
bundle_dir = root / "skills" / "claude"
if references is None:
shutil.rmtree(bundle_dir)
bundle_dir.mkdir(parents=True)
# Bundle dir present, but no references/ subdir inside it.
(bundle_dir / "SKILL.md").write_text("body\n", encoding="utf-8")
else:
refs_dir = bundle_dir / "references"
shutil.rmtree(refs_dir)
refs_dir.mkdir(parents=True)
(refs_dir / "extraction-spec.md").write_text(references[0], encoding="utf-8")
(refs_dir / "query.md").write_text(references[1], encoding="utf-8")
return root
def _fake_package_file(root: Path):
"""Point ``graphify.install``'s asset resolution at the staged fake root."""
return patch("graphify.install.__file__", str(root / "install.py"))
@pytest.fixture()
def fake_bundle():
"""Stage a fake references/ bundle in claude's slot, then restore the real one.
def fake_bundle(tmp_path):
"""Stage a fake references/ bundle in a throwaway package root.
Yields the platform name. claude is used because its skill_refs bundle is
"claude" and it has no extra plugin wiring. The real committed bundle is
moved aside for the duration and put back afterward, so the on-disk package
is left exactly as found.
"claude" and it has no extra plugin wiring. The fake bundle lives in a
private package root under ``tmp_path`` and the installer is pointed at it
via ``graphify.install.__file__``, so the real on-disk package (and the
checkout) is never modified (#4603).
"""
platform = "claude"
bundle = mainmod._PLATFORM_CONFIG[platform]["skill_refs"]
skills_root = PKG_DIR / "skills"
bundle_dir = skills_root / bundle
refs_dir = bundle_dir / "references"
created_root = not skills_root.exists()
backup_dir = None
if bundle_dir.exists():
backup_dir = Path(tempfile.mkdtemp()) / "bundle_backup"
shutil.move(str(bundle_dir), str(backup_dir))
refs_dir.mkdir(parents=True, exist_ok=True)
(refs_dir / "extraction-spec.md").write_text("# extraction spec fragment\n", encoding="utf-8")
(refs_dir / "query.md").write_text("# query fragment\n", encoding="utf-8")
try:
platform = _FakeBundle("claude")
platform.pkg_root = _stage_fake_package(tmp_path / "fakepkg")
with _fake_package_file(platform.pkg_root):
yield platform
finally:
if bundle_dir.exists():
shutil.rmtree(bundle_dir, ignore_errors=True)
if backup_dir is not None:
shutil.move(str(backup_dir), str(bundle_dir))
shutil.rmtree(backup_dir.parent, ignore_errors=True)
elif created_root:
shutil.rmtree(skills_root, ignore_errors=True)
def _install(tmp_path, platform):
@@ -184,34 +218,18 @@ def test_hard_fail_when_bundle_dir_present_but_references_missing(tmp_path, monk
A wholly-absent bundle dir is the opposite case (a host whose wave has not
shipped) and falls back to monolith, covered by
``test_unbuilt_bundle_host_falls_back_to_monolith``.
"""
platform = "claude"
bundle = mainmod._PLATFORM_CONFIG[platform]["skill_refs"]
skills_root = PKG_DIR / "skills"
bundle_dir = skills_root / bundle
created_root = not skills_root.exists()
backup_dir = None
if bundle_dir.exists():
backup_dir = Path(tempfile.mkdtemp()) / "bundle_backup"
shutil.move(str(bundle_dir), str(backup_dir))
# Bundle dir present, but no references/ subdir inside it.
bundle_dir.mkdir(parents=True, exist_ok=True)
(bundle_dir / "SKILL.md").write_text("body\n", encoding="utf-8")
try:
The malformed bundle is staged in a throwaway package root
(``_stage_fake_package(..., references=None)``), so the real package is
never modified (#4603).
"""
root = _stage_fake_package(tmp_path / "malformedpkg", references=None)
with _fake_package_file(root):
with pytest.raises(SystemExit) as exc:
with patch("graphify.__main__.Path.home", return_value=tmp_path):
monkeypatch.chdir(tmp_path)
mainmod._copy_skill_file("claude")
assert exc.value.code == 1
finally:
if bundle_dir.exists():
shutil.rmtree(bundle_dir, ignore_errors=True)
if backup_dir is not None:
shutil.move(str(backup_dir), str(bundle_dir))
shutil.rmtree(backup_dir.parent, ignore_errors=True)
elif created_root:
shutil.rmtree(skills_root, ignore_errors=True)
def _first_unbuilt_progressive_host():
@@ -526,8 +544,7 @@ def test_install_from_read_only_package_dir(tmp_path, fake_bundle):
staged copy is made writable first.
"""
platform = fake_bundle
bundle = mainmod._PLATFORM_CONFIG[platform]["skill_refs"]
refs_src = PKG_DIR / "skills" / bundle / "references"
refs_src = platform.pkg_root / "skills" / "claude" / "references"
modes = {p: p.stat().st_mode for p in (refs_src, *refs_src.rglob("*"))}
for path in modes:
+41 -6
View File
@@ -7,9 +7,12 @@ This builds the wheel once and asserts every committed skill artifact ships in i
"""
from __future__ import annotations
import hashlib
import subprocess
import sys
import tempfile
import zipfile
from contextlib import contextmanager
from pathlib import Path
import pytest
@@ -52,18 +55,50 @@ def _expected_artifacts() -> list[Path]:
return bodies + refs + always
@contextmanager
def _wheel_build_lock():
"""Serialise wheel builds that share one checkout's ``build/`` dir (#4603).
``python -m build --no-isolation`` runs setuptools in the repo, whose
default ``build/`` dir is shared by every process on that checkout: two
concurrent pytest runs deleted each other's staged files and one run then
skipped all dependent tests. The lock file lives in the system temp dir,
keyed by the repo path, so it is shared across processes on the same
checkout without adding anything to the checkout itself. fcntl locks are
released by the OS even when the holder is killed, so a dead run never
wedges later ones. Platforms without fcntl fall back to no locking.
"""
try:
import fcntl
except ImportError:
yield
return
key = hashlib.sha256(str(REPO).encode()).hexdigest()[:16]
lock_path = Path(tempfile.gettempdir()) / f"graphify-wheel-build-{key}.lock"
with open(lock_path, "a+") as fh:
fcntl.flock(fh.fileno(), fcntl.LOCK_EX)
try:
yield
finally:
fcntl.flock(fh.fileno(), fcntl.LOCK_UN)
@pytest.fixture(scope="module")
def wheel_namelist(tmp_path_factory) -> set[str]:
if not _has_build():
pytest.skip("`python -m build` unavailable (dev extra not installed)")
out = tmp_path_factory.mktemp("wheel")
proc = subprocess.run(
[sys.executable, "-m", "build", "--wheel", "--no-isolation",
"--outdir", str(out), str(REPO)],
capture_output=True, text=True,
)
with _wheel_build_lock():
proc = subprocess.run(
[sys.executable, "-m", "build", "--wheel", "--no-isolation",
"--outdir", str(out), str(REPO)],
capture_output=True, text=True,
)
if proc.returncode != 0:
pytest.skip(f"wheel build failed in this env:\n{proc.stderr[-800:]}")
# A wheel build that fails here fails for a packaging reason the guards
# below exist to catch (or a genuinely broken checkout); skipping hid
# exactly that, reporting 133 green skips instead (#4603).
pytest.fail(f"wheel build failed in this env:\n{proc.stderr[-800:]}")
wheels = list(out.glob("graphifyy-*.whl"))
assert wheels, "no wheel produced"
with zipfile.ZipFile(max(wheels, key=lambda p: p.stat().st_mtime)) as z: