diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4f193b7..086bfef 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,6 +1,12 @@ name: CI on: + workflow_dispatch: + inputs: + disable_caches: + description: Build helper without restoring or saving dependency/model caches + type: boolean + default: false push: branches: - "**" @@ -21,12 +27,39 @@ env: SKIP_BUNDLED_SEPARATOR_HELPER: "1" jobs: + changes: + name: Select Helper Validation + runs-on: macos-26 + outputs: + helper_required: ${{ steps.changes.outputs.helper_required }} + steps: + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + - name: Select checks + id: changes + run: python3 scripts/separator_ci_changes.py + python-tests: name: Separator Helper Tests runs-on: macos-26 steps: - name: Checkout - uses: actions/checkout@v5 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Set up pinned Python + uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + with: + python-version-file: JammLabSeparatorHelper/.python-version + architecture: arm64 + check-latest: false + + - name: Validate dependency inputs + run: python3 scripts/check_separator_dependencies.py --check-platform + + - name: Test dependency and CI checks + run: python3 -m unittest discover -s scripts -p 'test_separator_dependencies.py' - name: Run Python helper tests run: python3 -m unittest JammLabSeparatorHelper/test_runner.py @@ -39,7 +72,7 @@ jobs: runs-on: macos-26 steps: - name: Checkout - uses: actions/checkout@v5 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Run Swift tests run: > @@ -85,7 +118,7 @@ jobs: - Release steps: - name: Checkout - uses: actions/checkout@v5 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Build unsigned app run: > @@ -103,12 +136,14 @@ jobs: ARCHS=arm64 tag-artifacts: - name: Tag Build Artifacts + name: Packaged Helper and Release Artifacts runs-on: macos-26 needs: - python-tests - swift-tests - if: startsWith(github.ref, 'refs/tags/v') + - changes + if: needs.changes.outputs.helper_required == 'true' + timeout-minutes: 120 outputs: release_version: ${{ steps.release_metadata.outputs.RELEASE_VERSION }} app_version: ${{ steps.release_metadata.outputs.APP_VERSION }} @@ -117,38 +152,76 @@ jobs: is_prerelease: ${{ steps.release_metadata.outputs.IS_PRERELEASE }} env: SKIP_BUNDLED_SEPARATOR_HELPER: "0" + PIP_CACHE_DIR: ${{ github.workspace }}/build/JammLabSeparatorHelper/pip-cache steps: - name: Checkout - uses: actions/checkout@v5 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 - - name: Derive release metadata from tag + - name: Set up pinned Python + uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + with: + python-version-file: JammLabSeparatorHelper/.python-version + architecture: arm64 + check-latest: false + + - name: Record toolchain + run: | + mkdir -p build/JammLabSeparatorHelper/reports + { + python3 --version + sw_vers + uname -m + xcodebuild -version + } > build/JammLabSeparatorHelper/reports/toolchain.txt + + - name: Derive release metadata id: release_metadata run: | set -euo pipefail - metadata="$(scripts/derive_release_metadata.sh "$GITHUB_REF_NAME")" + if [[ "$GITHUB_REF" == refs/tags/v* ]]; then + build_tag="$GITHUB_REF_NAME" + else + build_tag="v0.0.0-dev.${GITHUB_RUN_NUMBER}" + fi + metadata="$(scripts/derive_release_metadata.sh "$build_tag")" printf '%s\n' "$metadata" printf '%s\n' "$metadata" >> "$GITHUB_ENV" printf '%s\n' "$metadata" >> "$GITHUB_OUTPUT" - - name: Cache bundled separator helper dependencies - uses: actions/cache@v4 + - name: Cache Python downloads + if: ${{ !inputs.disable_caches }} + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: build/JammLabSeparatorHelper/pip-cache + key: separator-pip-v1-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('JammLabSeparatorHelper/.python-version', 'JammLabSeparatorHelper/requirements*.txt', 'JammLabSeparatorHelper/*.lock', 'JammLabSeparatorHelper/build-constraints.txt') }} + + - name: Cache separator models + if: ${{ !inputs.disable_caches }} + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: - path: | - build/JammLabSeparatorHelper/venv - build/JammLabSeparatorHelper/model-cache - build/JammLabSeparatorHelper/pyinstaller-config - key: separator-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('JammLabSeparatorHelper/requirements.txt', 'JammLabSeparatorHelper/requirements-build.txt', 'JammLabSeparatorHelper/runner.py', 'JammLabSeparatorHelper/JammLabSeparatorHelper.spec', 'scripts/build_separator_helper.sh') }} - restore-keys: | - separator-${{ runner.os }}-${{ runner.arch }}- + path: build/JammLabSeparatorHelper/model-cache + key: separator-models-v1-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('JammLabSeparatorHelper/helper-manifest.json') }} - name: Build bundled separator helper - run: scripts/build_separator_helper.sh + run: | + set -euo pipefail + scripts/build_separator_helper.sh 2>&1 | tee build/JammLabSeparatorHelper/reports/build.log + + - name: Validate packaged helper offline + run: > + build/JammLabSeparatorHelper/venv/bin/python scripts/smoke_separator_helper.py + build/JammLabSeparatorHelper/dist/JammLabSeparatorHelper/JammLabSeparatorHelper + --report-dir build/JammLabSeparatorHelper/reports/standalone + + - name: Test smoke rejection cases + run: build/JammLabSeparatorHelper/venv/bin/python -m unittest discover -s scripts -p 'test_separator_smoke.py' - name: Build unsigned release app run: | set -euo pipefail + # Bash 3.2 needs guarded expansion of empty arrays with nounset enabled. update_check_build_setting=() if [[ "$RELEASE_CHANNEL" == "stable" ]]; then update_check_build_setting+=("SWIFT_ACTIVE_COMPILATION_CONDITIONS=JAMMLAB_STABLE_RELEASE") @@ -166,7 +239,7 @@ jobs: ONLY_ACTIVE_ARCH=YES \ ARCHS=arm64 \ -only-testing:JammLabTests/AppVersionTests/testCompiledReleasePolicyMatchesBuildFlags \ - "${update_check_build_setting[@]}" + ${update_check_build_setting[@]+"${update_check_build_setting[@]}"} xcodebuild build \ -project "$XCODE_PROJECT" \ @@ -182,7 +255,7 @@ jobs: CURRENT_PROJECT_VERSION="${{ github.run_number }}" \ ONLY_ACTIVE_ARCH=YES \ ARCHS=arm64 \ - "${update_check_build_setting[@]}" + ${update_check_build_setting[@]+"${update_check_build_setting[@]}"} app_path="$DERIVED_DATA_PATH/Build/Products/Release/JammLab.app" built_version="$(plutil -extract CFBundleShortVersionString raw -o - "$app_path/Contents/Info.plist")" @@ -200,38 +273,110 @@ jobs: exit 1 fi + - name: Validate app-embedded helper offline + run: > + build/JammLabSeparatorHelper/venv/bin/python scripts/smoke_separator_helper.py + build-ci/Build/Products/Release/JammLab.app/Contents/Resources/JammLabSeparatorHelper/JammLabSeparatorHelper + --report-dir build/JammLabSeparatorHelper/reports/app + - name: Package unsigned DMG run: > scripts/package_dmg.sh "$DERIVED_DATA_PATH/Build/Products/Release/JammLab.app" "dist/JammLab-$RELEASE_VERSION-unsigned.dmg" + - name: Verify DMG + run: hdiutil verify "dist/JammLab-$RELEASE_VERSION-unsigned.dmg" + - name: Package source archive run: | set -euo pipefail mkdir -p dist - git archive --format=zip --output "dist/JammLab-$RELEASE_VERSION-source.zip" "$GITHUB_REF_NAME" + git archive --format=zip --output "dist/JammLab-$RELEASE_VERSION-source.zip" "HEAD" - name: Upload unsigned DMG - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: JammLab-${{ steps.release_metadata.outputs.RELEASE_VERSION }}-unsigned-dmg path: dist/JammLab-${{ steps.release_metadata.outputs.RELEASE_VERSION }}-unsigned.dmg if-no-files-found: error - name: Upload source archive - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: JammLab-${{ steps.release_metadata.outputs.RELEASE_VERSION }}-source path: dist/JammLab-${{ steps.release_metadata.outputs.RELEASE_VERSION }}-source.zip if-no-files-found: error + - name: Record artifact checksums + working-directory: dist + run: shasum -a 256 "JammLab-$RELEASE_VERSION-unsigned.dmg" "JammLab-$RELEASE_VERSION-source.zip" > SHA256SUMS + + - name: Upload checksums + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: JammLab-${{ steps.release_metadata.outputs.RELEASE_VERSION }}-checksums + path: dist/SHA256SUMS + if-no-files-found: error + + - name: Upload helper diagnostics + if: always() + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: separator-diagnostics + path: build/JammLabSeparatorHelper/reports + if-no-files-found: warn + + verify-artifacts: + name: Verify Downloaded Artifacts + runs-on: ubuntu-latest + needs: tag-artifacts + steps: + - name: Download build artifacts + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 + with: + pattern: JammLab-${{ needs.tag-artifacts.outputs.release_version }}-* + merge-multiple: true + path: dist + - name: Verify names, contents and checksums + working-directory: dist + env: + RELEASE_VERSION: ${{ needs.tag-artifacts.outputs.release_version }} + run: | + test -f "JammLab-$RELEASE_VERSION-unsigned.dmg" + test -f "JammLab-$RELEASE_VERSION-source.zip" + sha256sum --check SHA256SUMS + unzip -t "JammLab-$RELEASE_VERSION-source.zip" + + helper-validation: + name: Helper Validation + runs-on: ubuntu-latest + if: always() + needs: [changes, tag-artifacts, verify-artifacts] + steps: + - name: Require selected validation to succeed + env: + CHANGES_RESULT: ${{ needs.changes.result }} + REQUIRED: ${{ needs.changes.outputs.helper_required }} + BUILD_RESULT: ${{ needs.tag-artifacts.result }} + ARTIFACT_RESULT: ${{ needs.verify-artifacts.result }} + run: | + test "$CHANGES_RESULT" = success + if [ "$REQUIRED" = true ]; then + test "$BUILD_RESULT" = success + test "$ARTIFACT_RESULT" = success + else + test "$BUILD_RESULT" = skipped + test "$ARTIFACT_RESULT" = skipped + fi + publish-release: name: Publish GitHub Release runs-on: ubuntu-latest needs: - tag-artifacts - if: startsWith(github.ref, 'refs/tags/v') && needs.tag-artifacts.outputs.is_github_release == 'true' + - helper-validation + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') && needs.tag-artifacts.outputs.is_github_release == 'true' permissions: contents: write env: @@ -239,13 +384,13 @@ jobs: IS_PRERELEASE: ${{ needs.tag-artifacts.outputs.is_prerelease }} steps: - name: Download unsigned DMG - uses: actions/download-artifact@v4 + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 with: name: JammLab-${{ needs.tag-artifacts.outputs.release_version }}-unsigned-dmg path: dist - name: Download source archive - uses: actions/download-artifact@v4 + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 with: name: JammLab-${{ needs.tag-artifacts.outputs.release_version }}-source path: dist diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e2f69f..eb1121a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ development artifact builds use `vMAJOR.MINOR.PATCH-dev.N`. ## Unreleased +- Fixed development and beta release builds failing in CI with the macOS system Bash. +- Made bundled Stem helper builds repeatable with pinned Python dependencies and added offline separation and release-artifact checks to CI. + ## 1.2.0 ### New Features diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 938281a..8364bce 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -58,9 +58,66 @@ Build the helper from the repository root before building the app target: scripts/build_separator_helper.sh ``` -The first helper build installs Python dependencies and prefetches separator -models, so it requires network access. Later runs reuse project-local build -state under `build/JammLabSeparatorHelper/`. +Use the exact arm64 CPython version in `JammLabSeparatorHelper/.python-version` +(currently 3.14.5), setting `PYTHON_BIN` if necessary. The build rejects other +Python versions and platforms before changing build output. CI uses +`actions/setup-python` with the same version file. + +Each helper build creates a fresh venv and installs all 69 pinned runtime and +packaging dependencies from `requirements-macos-arm64.lock`. It runs `pip check` +and checks the complete installed inventory against the lock. No dependency is +automatically upgraded. `build-constraints.txt` also pins tools used in isolated +source builds. The first build needs network access to obtain packages and +models. Later builds cache package downloads/wheels and model files, never the +venv or PyInstaller build state. This fixes package versions, not the bytes of +the final signed binary or DMG; macOS/Xcode runner images can still change. + +### Updating the dependency lock + +1. Start a dedicated branch. Use the pinned Python and create an isolated + candidate venv under `build/`; do not freeze a general-purpose environment. +2. Install the current lock there first. Make intentional edits to the direct + requirements and, if needed, the Python version/build constraints. Resolve + those edits in the candidate environment with pip, explicitly selecting + intended upgrades. Keep `setuptools<82` compatibility until separately tested. +3. Run `pip check` in the candidate environment and inspect the full dependency + diff. Export `python -m pip freeze --all` from that environment to + `JammLabSeparatorHelper/requirements-macos-arm64.lock`. Keep exact pins for + pip, setuptools, wheel, Cython and packaging hooks. `onnx-weekly` is required + by audio-separator 0.44.2 and is pinned to its existing development release; + do not replace it with `onnx` as part of a routine lock refresh. +4. Align `build-constraints.txt` with the candidate versions, then run + `python3 scripts/check_separator_dependencies.py --refresh-input-digest`. + The digest records all direct inputs and detects stale locks; it does not + certify dependency compatibility. +5. Build from scratch with `scripts/build_separator_helper.sh`, run the offline + smoke tests below, and update THIRD_PARTY_NOTICES.md from the resulting + environment. CI must pass both a cold and a warm run before merging. + +### Packaging validation + +```sh +python3 scripts/check_separator_dependencies.py --check-platform +python3 -m unittest discover -s scripts -p 'test_separator_dependencies.py' +build/JammLabSeparatorHelper/venv/bin/python scripts/smoke_separator_helper.py \ + build/JammLabSeparatorHelper/dist/JammLabSeparatorHelper/JammLabSeparatorHelper \ + --report-dir build/JammLabSeparatorHelper/reports/standalone +``` + +The smoke test uses macOS `sandbox-exec` to deny networking, a fresh home/temp +directory and a PATH containing no external Python or FFmpeg. It verifies the +sandbox restriction, helper capabilities and environment, then separates a +deterministic ten-second WAV with every manifest model on CPU. It checks stem +names/counts, readable finite samples and duration within 100 ms. This checks +packaging, not musical quality. CI repeats it against the helper inside the +Release app, verifies the DMG, and verifies SHA-256 checksums after downloading +the DMG and source ZIP in a separate job. Logs and timings are uploaded even on +failure. Run only on a macOS host that permits the sandbox to be applied. + +PRs touching the helper, related scripts or workflows run full packaging checks; +UI-only PRs keep the fast checks. Tags and manual runs always validate packaging. +Use the manual `disable_caches` option for cold verification, then rerun with +caches enabled. Manual validation never publishes a release, even on a tag. Useful paths and settings: diff --git a/JammLabSeparatorHelper/.python-version b/JammLabSeparatorHelper/.python-version new file mode 100644 index 0000000..a6d9ada --- /dev/null +++ b/JammLabSeparatorHelper/.python-version @@ -0,0 +1 @@ +3.14.5 diff --git a/JammLabSeparatorHelper/build-constraints.txt b/JammLabSeparatorHelper/build-constraints.txt new file mode 100644 index 0000000..230132b --- /dev/null +++ b/JammLabSeparatorHelper/build-constraints.txt @@ -0,0 +1,6 @@ +# Tools used in isolated source builds. Keep aligned with the platform lock. +setuptools==81.0.0 +wheel==0.47.0 +packaging==26.2 +cython==3.2.5 +numpy==2.4.6 diff --git a/JammLabSeparatorHelper/requirements-macos-arm64.lock b/JammLabSeparatorHelper/requirements-macos-arm64.lock new file mode 100644 index 0000000..678212e --- /dev/null +++ b/JammLabSeparatorHelper/requirements-macos-arm64.lock @@ -0,0 +1,72 @@ +# inputs-sha256: bfd1e87c7239b4c56d11c14013ea2681a47b4441c6de048d569cee5074efae03 +# macOS arm64, CPython 3.14.5. See CONTRIBUTING.md for regeneration. +# Runtime, packaging and explicit source-build tools; no automatic upgrades. +absl-py==2.4.0 +altgraph==0.17.5 +audio-separator==0.44.2 +audioop-lts==0.2.2 +audioread==3.1.0 +beartype==0.18.5 +certifi==2026.5.20 +cffi==2.0.0 +charset-normalizer==3.4.7 +cython==3.2.5 +decorator==5.3.1 +diffq==0.2.4 +einops==0.8.2 +filelock==3.29.0 +flatbuffers==25.12.19 +fsspec==2026.4.0 +idna==3.17 +imageio-ffmpeg==0.6.0 +jinja2==3.1.6 +joblib==1.5.3 +julius==0.2.7 +lazy-loader==0.5 +librosa==0.11.0 +llvmlite==0.47.0 +macholib==1.16.4 +markupsafe==3.0.3 +ml-collections==1.1.0 +ml-dtypes==0.5.4 +mpmath==1.3.0 +msgpack==1.1.2 +narwhals==2.22.0 +networkx==3.6.1 +numba==0.65.1 +numpy==2.4.6 +onnx-weekly==1.22.0.dev20260519 +onnx2torch-py313==1.6.0 +onnxruntime==1.26.0 +packaging==26.2 +pillow==12.2.0 +pip==26.1.2 +platformdirs==4.10.0 +pooch==1.9.0 +protobuf==7.35.0 +pycparser==3.0 +pydub==0.25.1 +pyinstaller==6.20.0 +pyinstaller-hooks-contrib==2026.5 +pyyaml==6.0.3 +requests==2.34.2 +resampy==0.4.3 +rotary-embedding-torch==0.6.5 +samplerate==0.1.0 +scikit-learn==1.9.0 +scipy==1.17.1 +setuptools==81.0.0 +six==1.17.0 +soundfile==0.13.1 +soxr==1.1.0 +standard-aifc==3.13.0 +standard-chunk==3.13.0 +standard-sunau==3.13.0 +sympy==1.14.0 +threadpoolctl==3.6.0 +torch==2.12.0 +torchvision==0.27.0 +tqdm==4.67.3 +typing-extensions==4.15.0 +urllib3==2.7.0 +wheel==0.47.0 diff --git a/README.md b/README.md index 1bade48..3a47627 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,9 @@ not need the packaged helper may set `SKIP_BUNDLED_SEPARATOR_HELPER=1`. GitHub CI uses these modes: - feature branches and pull requests to `main`: Python helper tests and Swift tests; +- helper, dependency, packaging-script or workflow changes: a clean pinned + helper build, offline separation checks, Release app/DMG packaging and + cross-job artifact checksum verification; - `main` pushes: tests plus unsigned Debug/Release build smoke; - stable release tags `vMAJOR.MINOR.PATCH`: tests, bundled separator build, unsigned Release app build, DMG packaging, source archive upload, and a @@ -56,6 +59,13 @@ GitHub CI uses these modes: - development tags `vMAJOR.MINOR.PATCH-dev.N`: the same artifact build, uploaded only as workflow artifacts. Dev tags do not create GitHub Releases. +The CI workflow can also be run manually, with `disable_caches` enabled for a +cold validation run. Manual runs never publish releases. `Helper Validation` +is the stable completion check, including when expensive checks are skipped. +The helper uses the exact Python version in +`JammLabSeparatorHelper/.python-version` and a macOS arm64 dependency lock; +see CONTRIBUTING.md for the update procedure. + Release versions are derived from Git tags. Tags such as `v1.0.0`, `v1.0.0-beta`, and `v1.0.0-dev.1` all build the app with `MARKETING_VERSION=1.0.0`; the standard macOS About panel reads that base app diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index a0e1524..eeb0d7f 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -8,6 +8,12 @@ The bundled Python separator inventory was generated from the local PyInstaller helper. Review this file whenever `JammLabSeparatorHelper` dependencies, model files, or binary packaging change. +The inventory is now pinned in +`JammLabSeparatorHelper/requirements-macos-arm64.lock` for arm64 CPython 3.14.5. +Clean installs verify the complete package inventory and dependency consistency. +This change preserves the package versions listed below; it does not upgrade +the separator, inference libraries or model weights. + Apple system frameworks used by the native macOS app are not listed here. ## Release Review Notes diff --git a/scripts/build_separator_helper.sh b/scripts/build_separator_helper.sh index f001a1a..b716b63 100755 --- a/scripts/build_separator_helper.sh +++ b/scripts/build_separator_helper.sh @@ -11,15 +11,26 @@ HELPER_MANIFEST="$HELPER_DIR/helper-manifest.json" PYINSTALLER_CONFIG_DIR="$BUILD_DIR/pyinstaller-config" PYTHON_BIN="${PYTHON_BIN:-python3}" +# Check before changing any existing build output. +"$PYTHON_BIN" "$ROOT_DIR/scripts/check_separator_dependencies.py" --check-platform + mkdir -p "$BUILD_DIR" "$PYINSTALLER_CONFIG_DIR" export PYINSTALLER_CONFIG_DIR -if [[ ! -x "$VENV_DIR/bin/python" ]]; then - "$PYTHON_BIN" -m venv "$VENV_DIR" -fi - -"$VENV_DIR/bin/python" -m pip install --upgrade pip "setuptools<82" wheel -"$VENV_DIR/bin/python" -m pip install -r "$HELPER_DIR/requirements-build.txt" +# Never reuse an environment from another Python installation or dependency set. +"$PYTHON_BIN" -m venv --clear "$VENV_DIR" +export PIP_CACHE_DIR="${PIP_CACHE_DIR:-$BUILD_DIR/pip-cache}" +export PIP_DISABLE_PIP_VERSION_CHECK=1 +export PYTHONNOUSERSITE=1 +LOCK_FILE="$HELPER_DIR/requirements-macos-arm64.lock" +PIP_PIN="$(sed -n '/^pip==/p' "$LOCK_FILE")" +"$VENV_DIR/bin/python" -m pip install --no-deps "$PIP_PIN" +"$VENV_DIR/bin/python" -m pip install --no-deps \ + --build-constraint "$HELPER_DIR/build-constraints.txt" -r "$LOCK_FILE" +"$VENV_DIR/bin/python" -m pip check +"$VENV_DIR/bin/python" "$ROOT_DIR/scripts/check_separator_dependencies.py" --check-environment +mkdir -p "$BUILD_DIR/reports" +"$VENV_DIR/bin/python" -m pip freeze --all > "$BUILD_DIR/reports/installed-packages.txt" mkdir -p "$MODEL_CACHE_DIR" SEPARATOR_MODELS="$("$PYTHON_BIN" -c 'import json,sys; print(" ".join(model["name"] for model in json.load(open(sys.argv[1]))["models"]))' "$HELPER_MANIFEST")" @@ -32,7 +43,8 @@ for model in $SEPARATOR_MODELS; do --model_file_dir "$MODEL_CACHE_DIR" done -rm -rf "$DIST_DIR" "$BUILD_DIR/work" +rm -rf "$DIST_DIR" "$BUILD_DIR/work" "$PYINSTALLER_CONFIG_DIR" +mkdir -p "$PYINSTALLER_CONFIG_DIR" ( cd "$HELPER_DIR" "$VENV_DIR/bin/python" -m PyInstaller \ diff --git a/scripts/check_separator_dependencies.py b/scripts/check_separator_dependencies.py new file mode 100644 index 0000000..bb16922 --- /dev/null +++ b/scripts/check_separator_dependencies.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python3 +"""Validate the helper's version lock without importing its ML dependencies.""" + +import argparse +import hashlib +import importlib.metadata +import platform +import re +from pathlib import Path + +HELPER = Path(__file__).resolve().parents[1] / "JammLabSeparatorHelper" +LOCK_NAME = "requirements-macos-arm64.lock" +INPUTS = (".python-version", "requirements.txt", "requirements-build.txt", "build-constraints.txt") +DIGEST_PREFIX = "# inputs-sha256: " + + +def canonical_name(name): + return re.sub(r"[-_.]+", "-", name).lower() + + +def read_pins(path): + pins = {} + for line in path.read_text().splitlines(): + line = line.strip() + if not line or line.startswith("#") or line == "-r requirements.txt": + continue + match = re.fullmatch(r"([\w.-]+)(?:\[[\w,.-]+\])?==([\w.+!-]+)", line) + if not match: + raise ValueError(f"{path.name}: expected an exact package==version pin: {line}") + name, version = canonical_name(match[1]), match[2] + if name in pins: + raise ValueError(f"{path.name}: duplicate package: {name}") + pins[name] = version + return pins + + +def input_digest(helper): + digest = hashlib.sha256() + for name in INPUTS: + digest.update(name.encode() + b"\0" + (helper / name).read_bytes() + b"\0") + return digest.hexdigest() + + +def validate_lock(helper): + lock = helper / LOCK_NAME + expected_header = DIGEST_PREFIX + input_digest(helper) + if expected_header not in lock.read_text().splitlines(): + raise ValueError("Dependency inputs changed. Regenerate and validate the helper lock (CONTRIBUTING.md).") + pins = read_pins(lock) + for name in INPUTS[1:]: + for package, version in read_pins(helper / name).items(): + if pins.get(package) != version: + raise ValueError(f"{name}: {package}=={version} disagrees with {LOCK_NAME}") + for package in ("pip", "setuptools", "wheel", "pyinstaller-hooks-contrib"): + if package not in pins: + raise ValueError(f"Missing build dependency: {package}") + return pins + + +def validate_platform(helper, system=None, machine=None, version=None): + actual = (system or platform.system(), machine or platform.machine(), version or platform.python_version()) + expected = ("Darwin", "arm64", (helper / ".python-version").read_text().strip()) + if actual != expected: + raise ValueError(f"Helper build requires {expected}; found {actual}. Set PYTHON_BIN to the pinned arm64 Python.") + + +def validate_environment(pins, installed=None): + if installed is None: + installed = {canonical_name(d.metadata["Name"]): d.version for d in importlib.metadata.distributions()} + differences = [f"{name}: expected {pins.get(name, 'absent')}, found {installed.get(name, 'absent')}" + for name in sorted(pins.keys() | installed.keys()) if pins.get(name) != installed.get(name)] + if differences: + raise ValueError("Environment does not match lock:\n" + "\n".join(differences)) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--check-platform", action="store_true") + parser.add_argument("--check-environment", action="store_true") + parser.add_argument("--refresh-input-digest", action="store_true", help="Only when intentionally regenerating the lock") + args = parser.parse_args() + if args.refresh_input_digest: + lock = HELPER / LOCK_NAME + lines = [line for line in lock.read_text().splitlines() if not line.startswith(DIGEST_PREFIX)] + lock.write_text(DIGEST_PREFIX + input_digest(HELPER) + "\n" + "\n".join(lines) + "\n") + try: + pins = validate_lock(HELPER) + if args.check_platform: + validate_platform(HELPER) + if args.check_environment: + validate_environment(pins) + except ValueError as error: + parser.exit(1, f"error: {error}\n") + print(f"Validated {len(pins)} pinned helper dependencies") + + +if __name__ == "__main__": + main() diff --git a/scripts/separator_ci_changes.py b/scripts/separator_ci_changes.py new file mode 100644 index 0000000..a80428f --- /dev/null +++ b/scripts/separator_ci_changes.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +"""Select expensive packaging checks while keeping ordinary CI checks visible.""" + +import json +import os +import subprocess +from pathlib import Path + + +def affects_helper(path): + return (path.startswith(("JammLabSeparatorHelper/", "JammLabStemHelper/", ".github/workflows/", "Configurations/")) + or path in {"scripts/package_dmg.sh", "scripts/derive_release_metadata.sh", + "JammLab.xcodeproj/project.pbxproj", "JammLab/JammLab.entitlements", + "JammLab/JammLab.Release.entitlements"} + or (path.startswith("scripts/") and "separator" in path)) + + +def needs_full_check(event_name, ref, paths): + return event_name == "workflow_dispatch" or ref.startswith("refs/tags/v") or any(map(affects_helper, paths)) + + +def changed_paths(event_name, event): + if event_name == "pull_request": + base, head = event["pull_request"]["base"]["sha"], event["pull_request"]["head"]["sha"] + revision = f"{base}...{head}" + else: + base, head = event.get("before", ""), event.get("after", "HEAD") + if not base or set(base) == {"0"}: + # A new branch has no before commit: conservatively validate packaging. + return [".github/workflows/ci.yml"] + revision = f"{base}..{head}" + result = subprocess.run(["git", "diff", "--no-renames", "--name-only", "-z", revision], check=True, capture_output=True) + return result.stdout.decode().rstrip("\0").split("\0") + + +def main(): + event_name, ref = os.environ["GITHUB_EVENT_NAME"], os.environ["GITHUB_REF"] + event = json.loads(Path(os.environ["GITHUB_EVENT_PATH"]).read_text()) + paths = [] if event_name == "workflow_dispatch" or ref.startswith("refs/tags/v") else changed_paths(event_name, event) + required = needs_full_check(event_name, ref, paths) + with open(os.environ["GITHUB_OUTPUT"], "a") as output: + output.write(f"helper_required={str(required).lower()}\n") + print(f"Full helper validation: {required}") + + +if __name__ == "__main__": + main() diff --git a/scripts/smoke_separator_helper.py b/scripts/smoke_separator_helper.py new file mode 100644 index 0000000..91d3c69 --- /dev/null +++ b/scripts/smoke_separator_helper.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python3 +"""Exercise the packaged helper, including its seeded cache, without network access.""" + +import argparse +import hashlib +import json +import os +from pathlib import Path +import subprocess +import sys +import tempfile +import time + +import numpy as np +import soundfile as sf + +ROOT = Path(__file__).resolve().parents[1] +EXPECTED_STEMS = { + "htdemucs.yaml": {"vocals", "drums", "bass", "other"}, + "htdemucs_6s.yaml": {"vocals", "drums", "bass", "other", "guitar", "piano"}, + "UVR-MDX-NET-Inst_HQ_5.onnx": {"vocals", "instrumental"}, +} +SANDBOX = '(version 1) (allow default) (deny network*)' + + +def validate_outputs(directory, expected, duration=10.0): + files = sorted(directory.glob("*.wav")) + if len(files) != len(expected): + raise ValueError(f"Expected {len(expected)} WAV stems, found {len(files)} in {directory}") + for stem in expected: + if not any(f"({stem})" in path.stem.lower() for path in files): + raise ValueError(f"Missing stem {stem}: {[path.name for path in files]}") + for path in files: + audio, rate = sf.read(path, always_2d=True) + if not len(audio) or not np.isfinite(audio).all(): + raise ValueError(f"Empty or non-finite audio: {path}") + if abs(len(audio) / rate - duration) > 0.1: + raise ValueError(f"Wrong stem duration: {path}: {len(audio) / rate}") + return [path.name for path in files] + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("helper", type=Path) + parser.add_argument("--report-dir", required=True, type=Path) + args = parser.parse_args() + helper = args.helper.resolve(strict=True) + report = args.report_dir.resolve() + report.mkdir(parents=True, exist_ok=True) + manifest_path = ROOT / "JammLabSeparatorHelper/helper-manifest.json" + manifest = json.loads(manifest_path.read_text()) + if set(EXPECTED_STEMS) != {model["name"] for model in manifest["models"]}: + raise ValueError("Update smoke-test expectations for the changed model manifest") + + results = [] + with tempfile.TemporaryDirectory(prefix="jammlab-offline-smoke-") as temporary: + work = Path(temporary) + # HOME is set only in the child environment, never repurposed in the shell. + for name in ("home", "tmp", "empty-path"): + (work / name).mkdir() + env = {"HOME": str(work / "home"), "TMPDIR": str(work / "tmp"), + "PATH": str(work / "empty-path"), "PYTHONNOUSERSITE": "1", + "LANG": "en_US.UTF-8", "OMP_NUM_THREADS": "2"} + + def run(arguments, name, timeout=1800): + command = ["/usr/bin/sandbox-exec", "-p", SANDBOX, *map(str, arguments)] + start = time.monotonic() + with (report / f"{name}.log").open("w") as log: + subprocess.run(command, env=env, cwd=work, stdout=log, stderr=subprocess.STDOUT, + check=True, timeout=timeout) + return time.monotonic() - start + + # Fail closed if the host cannot enforce the network restriction. + run([sys.executable, "-c", "import errno,socket; s=socket.socket(); " + "assert s.connect_ex(('127.0.0.1',9)) in (errno.EPERM,errno.EACCES)"], "network-denial", 30) + run([helper, "--capabilities_json"], "capabilities", 60) + capabilities = json.loads((report / "capabilities.log").read_text()) + for field in ("protocolVersion", "separatorVersion", "supportedComputeModes"): + if capabilities[field] != manifest[field]: + raise ValueError(f"Incorrect helper capability: {field}") + if capabilities["manifestSHA256"] != hashlib.sha256(manifest_path.read_bytes()).hexdigest(): + raise ValueError("Packaged helper manifest does not match the source manifest") + if set(capabilities["supportedModels"]) != set(EXPECTED_STEMS): + raise ValueError("Packaged helper does not support the expected models") + run([helper, "--env_info"], "environment", 120) + environment = (report / "environment.log").read_text() + if any(word in environment for word in ("not-installed", "not-bundled", "unavailable:")): + raise ValueError("Packaged helper reports missing runtime dependencies") + + rate = 44100 + t = np.arange(10 * rate) / rate + envelope = 0.5 + 0.5 * np.sin(2 * np.pi * 2 * t) + signal = envelope * sum(0.1 * np.sin(2 * np.pi * frequency * t) for frequency in (110, 220, 440)) + source = work / "fixture.wav" + sf.write(source, np.column_stack((signal, signal * 0.8)), rate, subtype="PCM_16") + for index, model in enumerate(manifest["models"]): + name = model["name"] + output = work / f"output-{index}" + cache = work / f"empty-model-cache-{index}" + elapsed = run([helper, source, "--output_dir", output, "--model_file_dir", cache, + "--model_filename", name, "--output_format", "WAV", "--compute_device", "cpu"], + f"model-{index}") + run([helper, "--validate_model_cache", name, "--model_file_dir", cache], f"cache-{index}", 60) + files = validate_outputs(output, EXPECTED_STEMS[name]) + results.append({"model": name, "seconds": elapsed, "stems": files}) + (report / "results.json").write_text(json.dumps(results, indent=2) + "\n") + print(f"PASS {name}: {elapsed:.1f}s, {len(files)} stems", flush=True) + + +if __name__ == "__main__": + main() diff --git a/scripts/test_separator_dependencies.py b/scripts/test_separator_dependencies.py new file mode 100644 index 0000000..c32fb38 --- /dev/null +++ b/scripts/test_separator_dependencies.py @@ -0,0 +1,122 @@ +import tempfile +import unittest +import os +import subprocess +from pathlib import Path +from unittest.mock import patch + +import check_separator_dependencies as dependencies +import separator_ci_changes as changes + + +class DependencyLockTests(unittest.TestCase): + def test_checked_in_lock_matches_inputs_and_platform(self): + pins = dependencies.validate_lock(dependencies.HELPER) + self.assertEqual(pins["audio-separator"], "0.44.2") + dependencies.validate_platform(dependencies.HELPER, "Darwin", "arm64", "3.14.5") + + def test_wrong_python_or_architecture_fails(self): + for platform in [("Darwin", "arm64", "3.13.0"), ("Darwin", "x86_64", "3.14.5")]: + with self.subTest(platform=platform), self.assertRaisesRegex(ValueError, "PYTHON_BIN"): + dependencies.validate_platform(dependencies.HELPER, *platform) + + def test_input_edit_requires_lock_refresh(self): + with tempfile.TemporaryDirectory() as root: + helper = Path(root) + for name in (*dependencies.INPUTS, dependencies.LOCK_NAME): + (helper / name).write_bytes((dependencies.HELPER / name).read_bytes()) + with (helper / "requirements.txt").open("a") as output: + output.write("new-package==1.0\n") + with self.assertRaisesRegex(ValueError, "inputs changed"): + dependencies.validate_lock(helper) + + def test_exact_environment_rejects_missing_extra_and_wrong_versions(self): + expected = {"package": "1.0"} + dependencies.validate_environment(expected, expected) + for installed in [{}, {"package": "2.0"}, {"package": "1.0", "extra": "1.0"}]: + with self.subTest(installed=installed), self.assertRaisesRegex(ValueError, "does not match"): + dependencies.validate_environment(expected, installed) + + def test_unpinned_duplicate_and_url_requirements_fail(self): + for content in ["package>=1.0", "package @ https://example.com/package.whl", "package==1\nPackage==1"]: + with tempfile.TemporaryDirectory() as root: + path = Path(root) / "lock" + path.write_text(content) + with self.assertRaises(ValueError): + dependencies.read_pins(path) + + def test_failed_install_stops_before_packaging(self): + with tempfile.TemporaryDirectory() as root: + root = Path(root) + (root / "scripts").mkdir() + (root / "JammLabSeparatorHelper").mkdir() + build_script = root / "scripts/build_separator_helper.sh" + build_script.write_bytes((dependencies.HELPER.parent / "scripts/build_separator_helper.sh").read_bytes()) + (root / "JammLabSeparatorHelper" / dependencies.LOCK_NAME).write_text("pip==26.1.2\n") + fake_python = root / "fake-python" + fake_python.write_text('''#!/bin/bash +if [[ "$1" == *check_separator_dependencies.py ]]; then exit 0; fi +if [[ "$1" == -m && "$2" == venv ]]; then + mkdir -p "$4/bin" + printf '#!/bin/bash\\nexit 23\\n' > "$4/bin/python" + chmod +x "$4/bin/python" + exit 0 +fi +exit 99 +''') + fake_python.chmod(0o755) + result = subprocess.run(["bash", str(build_script)], env={**os.environ, "PYTHON_BIN": str(fake_python)}, + capture_output=True, text=True) + self.assertEqual(result.returncode, 23, result.stderr) + self.assertFalse((root / "build/JammLabSeparatorHelper/dist").exists()) + + +class ChangeSelectionTests(unittest.TestCase): + def test_helper_and_workflow_changes_require_packaging(self): + for path in ["JammLabSeparatorHelper/.python-version", "JammLabSeparatorHelper/runner.py", + ".github/workflows/ci.yml", "scripts/build_separator_helper.sh", + "scripts/test_separator_dependencies.py", "scripts/package_dmg.sh", + "JammLab.xcodeproj/project.pbxproj", "JammLabStemHelper/main.swift", + "Configurations/CI.xcconfig", "JammLab/JammLab.Release.entitlements"]: + self.assertTrue(changes.needs_full_check("pull_request", "refs/pull/1/merge", [path]), path) + + def test_ui_only_change_skips_packaging(self): + self.assertFalse(changes.needs_full_check("pull_request", "refs/pull/1/merge", ["JammLab/Views/ContentView.swift"])) + + def test_tag_and_manual_runs_always_validate(self): + self.assertTrue(changes.needs_full_check("push", "refs/tags/v1.2.3", [])) + self.assertTrue(changes.needs_full_check("workflow_dispatch", "refs/heads/main", [])) + + def test_diff_failure_is_not_treated_as_no_changes(self): + import subprocess + with patch.object(changes.subprocess, "run", side_effect=subprocess.CalledProcessError(1, "git")): + with self.assertRaises(subprocess.CalledProcessError): + changes.changed_paths("push", {"before": "a" * 40, "after": "b" * 40}) + + def test_rename_out_of_helper_directory_still_requires_check(self): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + def git(*arguments): + return subprocess.check_output(["git", "-C", str(root), *arguments], text=True).strip() + git("init", "-q") + git("config", "user.name", "Test") + git("config", "user.email", "test@example.invalid") + (root / "JammLabSeparatorHelper").mkdir() + (root / "JammLabSeparatorHelper/input.txt").write_text("packaging input\n") + git("add", ".") + git("-c", "core.hooksPath=/dev/null", "commit", "-qm", "initial") + before = git("rev-parse", "HEAD") + git("mv", "JammLabSeparatorHelper/input.txt", "elsewhere.txt") + git("-c", "core.hooksPath=/dev/null", "commit", "-qm", "rename") + after = git("rev-parse", "HEAD") + previous = Path.cwd() + try: + os.chdir(root) + paths = changes.changed_paths("push", {"before": before, "after": after}) + finally: + os.chdir(previous) + self.assertTrue(changes.needs_full_check("push", "refs/heads/test", paths)) + + +if __name__ == "__main__": + unittest.main() diff --git a/scripts/test_separator_smoke.py b/scripts/test_separator_smoke.py new file mode 100644 index 0000000..3378157 --- /dev/null +++ b/scripts/test_separator_smoke.py @@ -0,0 +1,38 @@ +"""Run in the locked helper venv; no ML models or network are needed.""" +import tempfile +import unittest +from pathlib import Path + +import numpy as np +import soundfile as sf + +from smoke_separator_helper import validate_outputs + + +class SmokeOutputTests(unittest.TestCase): + def test_valid_stems(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) + for stem in ("vocals", "instrumental"): + sf.write(path / f"fixture_({stem}).wav", np.ones(1000), 100, subtype="FLOAT") + self.assertEqual(len(validate_outputs(path, {"vocals", "instrumental"})), 2) + + def test_rejects_missing_wrong_empty_nonfinite_and_wrong_duration(self): + cases = [ + (None, None, "Expected"), + ("wrong", np.ones(1000), "Missing stem"), + ("vocals", np.zeros(0), "Empty or non-finite"), + ("vocals", np.full(1000, np.nan), "Empty or non-finite"), + ("vocals", np.ones(980), "Wrong stem duration"), + ] + for name, audio, message in cases: + with self.subTest(message=message), tempfile.TemporaryDirectory() as directory: + path = Path(directory) + if name is not None: + sf.write(path / f"fixture_({name}).wav", audio, 100, subtype="FLOAT") + with self.assertRaisesRegex(ValueError, message): + validate_outputs(path, {"vocals"}) + + +if __name__ == "__main__": + unittest.main()