"""Offline built-wheel import and real stdio acceptance, not plugin certification.""" import asyncio import hashlib import json import os import shutil import subprocess import sys import tarfile import uuid import zipfile from collections.abc import Iterator from email.parser import Parser from pathlib import Path import anyio import mido # type: ignore[import-untyped] import pytest from jsonschema import Draft202012Validator from mcp.shared.version import SUPPORTED_PROTOCOL_VERSIONS from test_cc_stdio import exercise_control_stdio from test_hybrid_stdio import exercise_hybrid_stdio from test_native_service import TEMPLATE, job, validate_saved from test_phrase_stdio import exercise_phrase_stdio from test_stdio import TIMEOUT, RawStdio from test_text_feedback import exercise_text_feedback from riff_groove_builder.schemas import ( GenerationError, GenerationResult, ProfileListResult, ToolName, tool_input_schema, tool_output_schema, ) ROOT = Path(__file__).resolve().parents[2] EVIDENCE = ROOT / ".test-artifacts" / "step16" / "installed-wheel" pytestmark = pytest.mark.integration def _distribution_directory(workspace: Path) -> Path: configured = os.environ.get("RIFF_RELEASE_DIST") if configured is None: return workspace / "dist" directory = Path(configured).resolve(strict=True) assert directory.is_dir() and directory.is_relative_to(ROOT / "dist") assert len(list(directory.glob("*.whl"))) == len(list(directory.glob("*.tar.gz"))) == 1 return directory def _run(command: list[str], *, cwd: Path, env: dict[str, str]) -> str: result = subprocess.run( command, cwd=cwd, env=env, capture_output=True, text=True, timeout=180, check=False, ) assert result.returncode == 0, ( f"Command failed: {command!r}\nstdout:\n{result.stdout}\nstderr:\n{result.stderr}" ) return result.stdout @pytest.fixture(scope="module") def distribution_workspace() -> Iterator[tuple[Path, dict[str, str]]]: workspace = ROOT / ".test-artifacts" / f"distribution-{uuid.uuid4().hex}" workspace.mkdir(parents=True) scratch = workspace / "scratch" scratch.mkdir() env = os.environ.copy() for name in ("PYTHONPATH", "PYTHONHOME"): env.pop(name, None) env.update( { "TEMP": str(scratch), "TMP": str(scratch), "TMPDIR": str(scratch), "PIP_NO_INDEX": "1", "PIP_NO_CACHE_DIR": "1", "PIP_DISABLE_PIP_VERSION_CHECK": "1", "PYTHONDONTWRITEBYTECODE": "1", } ) try: if os.environ.get("RIFF_RELEASE_DIST") is None: # Default build makes the wheel from the sdist, exercising both layouts. _run( [ sys.executable, "-I", "-B", "-m", "build", "--no-isolation", "--outdir", str(workspace / "dist"), str(ROOT), ], cwd=ROOT, env=env, ) _distribution_directory(workspace) yield workspace, env finally: shutil.rmtree(workspace) def test_build_distributions( distribution_workspace: tuple[Path, dict[str, str]], ) -> None: workspace, _ = distribution_workspace directory = _distribution_directory(workspace) wheels = list(directory.glob("*.whl")) sdists = list(directory.glob("*.tar.gz")) assert len(wheels) == len(sdists) == 1 with zipfile.ZipFile(wheels[0]) as wheel: assert "riff_groove_builder/__init__.py" in wheel.namelist() package_files = { path.relative_to(ROOT / "src").as_posix(): path for path in (ROOT / "src" / "riff_groove_builder").rglob("*") if path.is_file() and path.suffix in (".py", ".json") } assert { name for name in wheel.namelist() if name.startswith("riff_groove_builder/") } == set(package_files) for name, path in package_files.items(): assert wheel.read(name) == path.read_bytes() metadata_names = [name for name in wheel.namelist() if ".dist-info/" in name] assert sorted(name.rsplit("/", 1)[1] for name in metadata_names) == [ "METADATA", "RECORD", "WHEEL", "top_level.txt", ] assert len(wheel.namelist()) == len(package_files) + len(metadata_names) metadata = Parser().parsestr( wheel.read(next(name for name in metadata_names if name.endswith("/METADATA"))).decode( "utf-8" ) ) assert metadata["Version"] == "0.2.0" assert str(metadata.get_payload()).replace("\r\n", "\n") == (ROOT / "README.md").read_text( "utf-8" ) with tarfile.open(sdists[0]) as sdist: names = sdist.getnames() assert any(name.endswith("/src/riff_groove_builder/__init__.py") for name in names) assert any(name.endswith("/pyproject.toml") for name in names) assert not any("/.venv/" in name or "/.test-artifacts/" in name for name in names) files = { "/".join(member.name.split("/")[1:]): member for member in sdist.getmembers() if member.isfile() } for name, path in package_files.items(): saved = sdist.extractfile(files["src/" + name]) assert saved is not None and saved.read() == path.read_bytes() for name in ("README.md", "pyproject.toml"): saved = sdist.extractfile(files[name]) assert saved is not None and saved.read() == (ROOT / name).read_bytes() assert all( name in ("README.md", "pyproject.toml", "setup.cfg", "PKG-INFO") or name.removeprefix("src/") in package_files or name.startswith("src/gaj3t_musicgen_riff_groove_builder.egg-info/") for name in files ) def test_wheel_import_outside_checkout( distribution_workspace: tuple[Path, dict[str, str]], ) -> None: workspace, env = distribution_workspace wheel = next(_distribution_directory(workspace).glob("*.whl")) environment = workspace / "wheel-venv" outside_checkout = ROOT.parent assert not outside_checkout.is_relative_to(ROOT) _run( [sys.executable, "-I", "-B", "-m", "venv", str(environment)], cwd=outside_checkout, env=env, ) python = environment / ("Scripts/python.exe" if os.name == "nt" else "bin/python") _run( [ str(python), "-I", "-B", "-m", "pip", "install", "--no-index", "--no-deps", "--no-cache-dir", str(wheel), ], cwd=outside_checkout, env=env, ) probe = """ import contextlib import importlib.metadata import io import json import pathlib import sys stdout, stderr = io.StringIO(), io.StringIO() with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr): import riff_groove_builder as package assert stdout.getvalue() == stderr.getvalue() == "" assert "mcp" not in sys.modules assert "riff_groove_builder.server" not in sys.modules distribution = importlib.metadata.distribution("gaj3t-musicgen-riff-groove-builder") module_path = pathlib.Path(package.__file__).resolve() installed_file = pathlib.Path(distribution.locate_file("riff_groove_builder/__init__.py")).resolve() assert package.__version__ == distribution.version == "0.2.0" assert module_path == installed_file assert module_path.is_relative_to(pathlib.Path(sys.prefix).resolve()) print(json.dumps({ "version": package.__version__, "metadata_version": distribution.version, "module_path": str(module_path), "prefix": sys.prefix, "cwd": str(pathlib.Path.cwd()), "isolated": sys.flags.isolated, })) """ output = _run( [str(python), "-I", "-B", "-c", probe], cwd=outside_checkout, env=env, ) evidence = json.loads(output) assert evidence["isolated"] == 1 assert Path(evidence["cwd"]).resolve() == outside_checkout assert Path(evidence["module_path"]).is_relative_to(environment) assert not Path(evidence["module_path"]).is_relative_to(ROOT / "src") print(f"Installed-wheel evidence: {output.strip()}") _WHEEL_BOOTSTRAP = """ import hashlib import importlib import importlib.metadata import importlib.resources import json from pathlib import Path import runpy import sys target = Path(sys.argv[1]).resolve() sys.path.insert(0, str(target)) names = ( "riff_groove_builder", "riff_groove_builder.server", "riff_groove_builder.service", "riff_groove_builder.schemas", "riff_groove_builder.errors", "riff_groove_builder.description", "riff_groove_builder.hybrid", "riff_groove_builder.pitch", "riff_groove_builder.compose", "riff_groove_builder.profiles", "riff_groove_builder.articulations", "riff_groove_builder.midi", "riff_groove_builder.formats", "riff_groove_builder.formats.agp", "riff_groove_builder.formats.sdp", "riff_groove_builder.data", "riff_groove_builder.data.profiles", ) locations = {} for name in names: module = importlib.import_module(name) path = Path(module.__file__).resolve() assert path.is_relative_to(target), (name, path) locations[name] = str(path) main_path = Path(importlib.util.find_spec("riff_groove_builder.__main__").origin).resolve() assert main_path.is_relative_to(target) locations["riff_groove_builder.__main__"] = str(main_path) distribution = importlib.metadata.distribution("gaj3t-musicgen-riff-groove-builder") assert Path(distribution.locate_file("")).resolve() == target assert distribution.version == "0.2.0" assert not json.loads(distribution.read_text("direct_url.json")).get("dir_info", {}).get("editable") profiles = importlib.import_module("riff_groove_builder.data.profiles") resources = importlib.resources.files(profiles) resource_names = sorted(path.name for path in resources.iterdir() if path.name.endswith(".json")) assert resource_names == sorted(profiles.PROFILE_RESOURCES) resource_evidence = {} for name in resource_names: resource = resources.joinpath(name) assert Path(str(resource)).resolve().is_relative_to(target) assert json.loads(resource.read_text(encoding="utf-8"))["schema_version"] == "1" resource_evidence[name] = { "path": str(resource), "sha256": hashlib.sha256(resource.read_bytes()).hexdigest() } assert sys.flags.isolated == 1 if sys.argv[2] == "probe": from riff_groove_builder.articulations import compile_score from riff_groove_builder.formats.agp import write_riffer, validate_riffer_file from riff_groove_builder.profiles import load_profile_registry from riff_groove_builder.schemas import Score, ProfileSelection, ControlChangeEvent native_evidence = [] output = target.parent / "installed-native-readback" output.mkdir() for part, identifier, pitch, extension in ( ("lead", "ample.ame.4.0.1.riffer.v1", 36, "griff"), ("bass", "ample.amr.4.1.0.riffer.v1", 35, "briff"), ): profile = load_profile_registry().select( [part], ProfileSelection(**{part: identifier}) )[part] score = Score.model_validate({ "intent": { "description": "Original installed-wheel native verification", "parts": [part], "tempo_bpm": 120, "bars": 1, "time_signature": {"numerator": 4, "denominator": 4}, "key": "C minor", "seed": 0, }, "start": {"numerator": 0}, "end": {"numerator": 4}, "tracks": [{"part": part, "notes": [{ "kind": "pitched", "part": part, "pitch": pitch, "start": {"numerator": 0}, "duration": {"numerator": 1}, "intensity": 0.5, "articulation": "palm_mute", }]}], }) plan = compile_score(score, {part: profile}) plan.tracks[0].events.append(ControlChangeEvent( part=part, channel=profile.channel, tick=480, priority=10, sequence=100, controller=1, value=120, )) path = output / ("original." + extension) write_riffer(path, plan, part, profile) saved = validate_riffer_file(path, plan, part, profile) assert len(saved.notes) == 1 and saved.notes[0].pitch == pitch assert saved.notes[0].articulation == ("PM" if part == "lead" else "PMP") assert saved.cc1 == ((240, 120),) native_evidence.append({ "extension": extension, "notes": len(saved.notes), "cc1": saved.cc1, "size_bytes": path.stat().st_size, "path": str(path), }) from riff_groove_builder.formats.sdp import write_sdp, validate_sdp_file template = output / "original-template.sd3p" lines = [ "PlugVersion 3.4.4", "SAMPLER {", "HQ {", 'soundlib "SL-DFH"', "}", ] for index, (pad, pitch) in enumerate((("kickR", 36), ("snareR", 38), ("hatsTP", 42))): lines.extend([ f"instbox {index} {{", "xpad 0 SL-DFH {", "pads {", f"pad {pad} {{", "}", f"alias {pad} {pitch}", "}", "}", "}", ]) lines.extend([ "}", "ClearTracksFirst", "SongTrack 0 {", "ClearTrackFirst", "ClearSongParts", "TrackLength 4", "Tempo 120 0 0", "TimeSig 4 4 0", "}", "SetCurrentSongTrack 0", "MidiInChannel -1", ]) template.write_bytes(("\\n".join(lines) + "\\n").encode()) profile = load_profile_registry().select( ["drums"], ProfileSelection(drums="toontrack.sd3.3.4.4.core.v1") )["drums"] score = Score.model_validate({ "intent": { "description": "Original installed-wheel SD3 verification", "parts": ["drums"], "tempo_bpm": 137.5, "bars": 1, "time_signature": {"numerator": 4, "denominator": 4}, "key": "E minor", "seed": 19, }, "start": {"numerator": 0}, "end": {"numerator": 4}, "tracks": [{"part": "drums", "notes": [{ "kind": "drum", "part": "drums", "voice": "snare", "start": {"numerator": 0}, "duration": {"numerator": 1, "denominator": 8}, "intensity": 1.0, "articulation": "normal", }]}], }) plan = compile_score(score, {"drums": profile}) path = output / "original.sd3p" write_sdp(path, plan, "drums", profile, template=template) saved = validate_sdp_file(path, plan, "drums", profile, template=template) assert len(saved.notes) == 1 assert (saved.notes[0].channel, saved.notes[0].pitch, saved.notes[0].velocity) == (9, 38, 127) native_evidence.append({ "extension": "sd3p", "notes": 1, "size_bytes": path.stat().st_size, "path": str(path), }) print(json.dumps({ "locations": locations, "resources": resource_names, "target": str(target), "resource_evidence": resource_evidence, "module_sha256": { name: hashlib.sha256(Path(path).read_bytes()).hexdigest() for name, path in locations.items() }, "cwd": str(Path.cwd()), "isolated": sys.flags.isolated, "dependency_mcp": importlib.import_module("mcp").__file__, "version": distribution.version, "native_readback": native_evidence, })) else: sys.argv = ["riff_groove_builder", "--output-dir", sys.argv[2], *sys.argv[3:]] runpy.run_module("riff_groove_builder", run_name="__main__") """ @pytest.fixture def anyio_backend() -> str: return "asyncio" async def _call( client: RawStdio, name: ToolName, arguments: dict[str, object] ) -> dict[str, object]: Draft202012Validator(tool_input_schema(name)).validate(arguments) response = await client.request(name, "tools/call", {"name": name, "arguments": arguments}) result = response["result"] Draft202012Validator(tool_output_schema(name)).validate(result["structuredContent"]) assert len(result["content"]) == 1 assert result["content"][0]["type"] == "text" assert json.loads(result["content"][0]["text"]) == result["structuredContent"] assert isinstance(result, dict) return result def _read_published_midi( result: GenerationResult, outputs: Path, basename: str ) -> dict[str, object]: assert result.resolved.parts == ["lead", "bass", "drums"] assert result.resolved.tempo_bpm == 137.5 and result.resolved.bars == 2 assert result.resolved.time_signature.model_dump() == {"numerator": 7, "denominator": 8} assert result.resolved.key == "A# minor" and result.resolved.seed == 42 assert result.resolved.profile_ids.selected() == { "lead": "standard.lead.v1", "bass": "standard.bass.v1", "drums": "standard.gm-drums.v1", } assert result.resolved.musical_start_tick == 0 assert result.resolved.musical_end_tick == 6720 and result.resolved.ticks_per_quarter == 960 assert result.resolved.assumptions == [ "template: omitted; using rock.", "technique: omitted; using normal.", ] assert result.resolved.overrides == [] assert len(result.artifacts) == 1 artifact = result.artifacts[0] path = Path(artifact.path) assert artifact.format == "mid" and artifact.parts == result.resolved.parts assert artifact.profile_ids == ["standard.lead.v1", "standard.bass.v1", "standard.gm-drums.v1"] assert path.is_absolute() and path == outputs / basename / f"{basename}.mid" assert list(path.parent.iterdir()) == [path] data = path.read_bytes() assert data.startswith(b"MThd") and len(data) == artifact.size_bytes == path.stat().st_size midi = mido.MidiFile(path) assert midi.type == 1 and midi.ticks_per_beat == 960 assert [track.name for track in midi.tracks] == ["Conductor", "Lead", "Bass", "Drums"] conductor = midi.tracks[0] assert [message.type for message in conductor] == [ "track_name", "set_tempo", "time_signature", "end_of_track", ] assert conductor[1].tempo == 436364 and conductor[1].time == 0 assert (conductor[2].numerator, conductor[2].denominator, conductor[2].time) == (7, 8, 0) counts = [] for index, track in enumerate(midi.tracks): tick = 0 active: dict[tuple[int, int], int] = {} onsets = [] for message in track: assert isinstance(message.time, int) and message.time >= 0 tick += message.time if message.is_meta: assert message.type in ( ("track_name", "set_tempo", "time_signature", "end_of_track") if index == 0 else ("track_name", "end_of_track") ) continue assert index > 0 and message.type in ("note_on", "note_off") assert message.channel == (0, 1, 9)[index - 1] assert 0 <= message.note <= 127 and 0 <= message.velocity <= 127 key = message.channel, message.note if message.type == "note_on": assert message.velocity > 0 and key not in active and tick < 6720 active[key] = tick onsets.append(tick) if index == 3: assert message.note in (36, 38, 42) else: assert key in active and tick - active.pop(key) == (240 if index == 3 else 960) assert not active and tick == 6720 assert track[-1].type == "end_of_track" assert sum(message.type == "end_of_track" for message in track) == 1 assert onsets == ( [] if index == 0 else [ tick for tick in (0, 960, 1920, 3360, 4320, 5280) for _ in range(2 if index == 3 else 1) ] ) counts.append(len(onsets)) return { "path": str(path), "size": len(data), "sha256": hashlib.sha256(data).hexdigest(), "note_counts": counts, } @pytest.mark.anyio async def test_built_wheel_real_stdio_generation_outside_checkout( distribution_workspace: tuple[Path, dict[str, str]], ) -> None: workspace, env = distribution_workspace wheel = next(_distribution_directory(workspace).glob("*.whl")) wheel_sha256 = hashlib.sha256(wheel.read_bytes()).hexdigest() target = workspace / "installed-server" outputs = workspace / "server-outputs" outputs.mkdir() outside = ROOT.parent assert not outside.is_relative_to(ROOT) _run( [ sys.executable, "-I", "-B", "-m", "pip", "install", "--no-index", "--no-deps", "--no-cache-dir", "--target", str(target), str(wheel), ], cwd=outside, env=env, ) with zipfile.ZipFile(wheel) as archive: resources = [name for name in archive.namelist() if name.endswith(".json")] assert len(resources) == 6 assert all(name.startswith("riff_groove_builder/data/profiles/") for name in resources) for name in resources: assert target.joinpath(*name.split("/")).read_bytes() == archive.read(name) command = [sys.executable, "-I", "-B", "-c", _WHEEL_BOOTSTRAP, str(target)] evidence = json.loads(_run([*command, "probe"], cwd=outside, env=env)) assert Path(evidence["cwd"]) == outside and evidence["isolated"] == 1 assert Path(evidence["dependency_mcp"]).is_relative_to(Path(sys.prefix)) assert len(evidence["locations"]) == 18 assert [item["extension"] for item in evidence["native_readback"]] == ["griff", "briff", "sd3p"] assert all( item["notes"] == 1 and item["size_bytes"] > 0 and Path(item["path"]).is_relative_to(workspace) for item in evidence["native_readback"] ) assert all(Path(path).is_relative_to(target) for path in evidence["locations"].values()) assert all( not Path(path).is_relative_to(ROOT / "src") for path in evidence["locations"].values() ) for name, path in evidence["locations"].items(): source = ROOT / "src" / Path(path).relative_to(target) assert hashlib.sha256(source.read_bytes()).hexdigest() == evidence["module_sha256"][name] for name, resource in evidence["resource_evidence"].items(): source = ROOT / "src" / "riff_groove_builder" / "data" / "profiles" / name assert hashlib.sha256(source.read_bytes()).hexdigest() == resource["sha256"] disk_evidence = [] for initialized in (True, False): process = await asyncio.wait_for( asyncio.create_subprocess_exec( *command, str(outputs), cwd=outside, env=env, stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, ), TIMEOUT, ) client = RawStdio(process) try: if not initialized: await client.close() assert process.returncode == 0 assert client.lines == [] and client.diagnostics == b"" evidence["uninitialized_eof_exit_code"] = process.returncode continue with anyio.fail_after(60): handshake = await client.initialize() assert handshake["result"]["protocolVersion"] in SUPPORTED_PROTOCOL_VERSIONS assert handshake["result"]["serverInfo"]["name"] == "riff-groove-builder" tools = (await client.request(1, "tools/list"))["result"]["tools"] names: tuple[ToolName, ...] = ("list_instrument_profiles", "generate_music") assert [tool["name"] for tool in tools] == list(names) for name, tool in zip(names, tools, strict=True): assert tool["inputSchema"] == tool_input_schema(name) assert tool["outputSchema"] == tool_output_schema(name) listed = await _call(client, "list_instrument_profiles", {}) assert listed["isError"] is False profiles = ProfileListResult.model_validate(listed["structuredContent"]).profiles assert [profile.id for profile in profiles] == [ "ample.ame.4.0.1.riffer.v1", "ample.amr.4.1.0.riffer.v1", "standard.bass.v1", "standard.gm-drums.v1", "standard.lead.v1", "toontrack.sd3.3.4.4.core.v1", ] assert all( profile.mapping_kind == "standard_midi" and profile.midi_available and not profile.native_available and profile.supported_articulations == ["normal"] and profile.limitations for profile in profiles if profile.id.startswith("standard.") ) assert [p.native_available for p in profiles] == [ True, True, False, False, False, False, ] evidence["text_only_recovery"] = await exercise_text_feedback( client, outputs, label="installed-wheel" ) evidence["handshake"] = handshake["result"] evidence["tool_names"] = [tool["name"] for tool in tools] arguments: dict[str, object] = { "description": "lead bass drums 137.5 BPM 2 bars 7/8 Bb minor quarter rests", "seed": 42, "profiles": { "lead": "standard.lead.v1", "bass": "standard.bass.v1", "drums": "standard.gm-drums.v1", }, } for basename, formats in ( ("explicit", ["mid"]), ("default", None), ("native", ["agp", "sdp"]), ): extra = {} if formats is None else {"formats": formats} response = await _call( client, "generate_music", {**arguments, "basename": basename, **extra} ) assert response["isError"] is False result = GenerationResult.model_validate(response["structuredContent"]) fallback = [] if basename == "explicit" else ["lead", "bass", "drums"] assert result.status == ("midi_fallback" if fallback else "complete") assert result.artifacts[0].fallback_for == fallback assert [(warning.code, warning.part) for warning in result.warnings] == [ ("STANDARD_MIDI_MAPPING", "lead"), ("STANDARD_MIDI_MAPPING", "bass"), ("STANDARD_MIDI_MAPPING", "drums"), *(("NATIVE_FORMAT_UNAVAILABLE", part) for part in fallback), ] for warning in result.warnings[3:]: assert ( "sdp_profile_not_supported" if warning.part == "drums" else "riffer_profile_not_supported" ) in warning.message disk_evidence.append(_read_published_midi(result, outputs, basename)) assert len({item["sha256"] for item in disk_evidence}) == 1 target_request = job(basename="target-no-template") response = await _call( client, "generate_music", target_request.model_dump(mode="json", exclude_none=True), ) target_result = GenerationResult.model_validate(response["structuredContent"]) assert response["isError"] is False and target_result.status == "midi_fallback" assert [a.format for a in target_result.artifacts] == ["mid", "griff", "briff"] assert target_result.artifacts[0].fallback_for == ["drums"] assert [ (w.part, w.message) for w in target_result.warnings if w.code == "NATIVE_FORMAT_UNAVAILABLE" ] == [ ( "drums", "sd3p: sdp_template_required; " "the combined Standard MIDI substitutes for this native target.", ) ] validate_saved(target_result, target_request, None) before = sorted(outputs.iterdir()) response = await _call( client, "generate_music", job(basename="target-disabled", allow_midi_fallback=False).model_dump( mode="json", exclude_none=True ), ) assert response["isError"] is True error = GenerationError.model_validate(response["structuredContent"]) assert error.code == "NATIVE_FORMAT_UNAVAILABLE" assert error.details == { "format": "sd3p", "part": "drums", "reason": "sdp_template_required", } assert sorted(outputs.iterdir()) == before failures: list[tuple[str, str, dict[str, object], str]] = [ ( "disabled", "drums only", {"allow_midi_fallback": False}, "NATIVE_FORMAT_UNAVAILABLE", ), ( "unsupported", "lead only palm mute", {"formats": ["mid"]}, "UNSUPPORTED_ARTICULATION", ), ( "disabled_lead", "lead only", {"allow_midi_fallback": False}, "NATIVE_FORMAT_UNAVAILABLE", ), ("unicode", "drums only café", {"formats": ["mid"]}, "UNSUPPORTED_DESCRIPTION"), ("explicit", "drums only", {"formats": ["mid"]}, "OUTPUT_EXISTS"), ] for basename, description, error_options, code in failures: before = sorted(outputs.iterdir()) response = await _call( client, "generate_music", { "description": description, "basename": basename, "profiles": ( {"drums": "standard.gm-drums.v1"} if description.startswith("drums") else {"lead": "standard.lead.v1"} ), **error_options, }, ) assert response["isError"] is True error = GenerationError.model_validate(response["structuredContent"]) assert error.code == code assert sorted(outputs.iterdir()) == before if basename == "disabled": assert error.details == { "format": "sd3p", "part": "drums", "reason": "sdp_profile_not_supported", } if basename == "disabled_lead": assert error.details == { "format": "griff", "part": "lead", "reason": "riffer_profile_not_supported", } assert "café" not in json.dumps(response, ensure_ascii=False) assert not list(outputs.glob(".riff-stage-*")) await client.send('{"private-description": "秘密 café", invalid JSON') notification = await client.receive() assert notification["method"] == "notifications/message" assert notification["params"]["data"] == "Internal Server Error" assert (await client.request("after-malformed", "ping"))["result"] == {} await client.assert_quiet() finally: with anyio.CancelScope(shield=True): await client.close() assert process.returncode == 0 if initialized: assert len(client.lines) == 20 assert b"SDK rejected malformed protocol input" in client.diagnostics assert "秘密" not in client.diagnostics.decode("utf-8") assert b"private-description" not in client.diagnostics evidence["server_pid"] = process.pid evidence["stdout_lines"] = len(client.lines) evidence["exit_code"] = process.returncode else: assert client.lines == [] and client.diagnostics == b"" original_template = target.parent / "controls-original-template.sd3p" shutil.copyfile(TEMPLATE, original_template) original_bytes = original_template.read_bytes() original_info = original_template.stat() process = await asyncio.wait_for( asyncio.create_subprocess_exec( *command, str(outputs), "--sd3-template", str(original_template), cwd=outside, env=env, stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, ), TIMEOUT, ) native_client = RawStdio(process) native_evidence = [] try: await native_client.initialize() listed = await _call(native_client, "list_instrument_profiles", {}) summaries = ProfileListResult.model_validate(listed["structuredContent"]).profiles assert all(p.native_available for p in summaries if p.mapping_kind == "instrument") for basename, formats in ( ("native-default", None), ("native-alias", ["agp", "sdp"]), ("native-actual", ["griff", "briff", "sd3p"]), ("native-midi", ["mid"]), ): request = job(basename=basename, formats=formats, allow_midi_fallback=False) response = await _call( native_client, "generate_music", request.model_dump(mode="json", exclude_none=True) ) assert response["isError"] is False result = GenerationResult.model_validate(response["structuredContent"]) assert result.status == "complete" assert [a.format for a in result.artifacts] == ( ["mid", "griff", "briff", "sd3p"] if formats is None else ["mid"] if formats == ["mid"] else ["griff", "briff", "sd3p"] ) native_evidence.append(validate_saved(result, request, original_template)) before_collision = { str(path): path.read_bytes() for path in outputs.rglob("*") if path.is_file() } response = await _call( native_client, "generate_music", job(basename="native-default").model_dump(mode="json", exclude_none=True), ) assert response["isError"] is True assert GenerationError.model_validate(response["structuredContent"]).code == "OUTPUT_EXISTS" assert { str(path): path.read_bytes() for path in outputs.rglob("*") if path.is_file() } == before_collision assert not list(outputs.glob(".riff-stage-*")) control_outputs = await exercise_control_stdio( native_client, outputs, original_template, prefix="wheel-cc" ) await exercise_phrase_stdio(native_client, outputs, label="installed-wheel") legacy_lines = len(native_client.lines) finally: with anyio.CancelScope(shield=True): await native_client.close() assert native_client.diagnostics == b"" and process.returncode == 0 assert legacy_lines == len(native_client.lines) == 23 process = await asyncio.wait_for( asyncio.create_subprocess_exec( *command, str(outputs), cwd=outside, env=env, stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, ), TIMEOUT, ) hybrid_client = RawStdio(process) try: handshake = await hybrid_client.initialize() assert handshake["result"]["serverInfo"]["version"] == "0.2.0" hybrid_outputs = await exercise_hybrid_stdio( hybrid_client, outputs, label="installed-wheel" ) finally: await hybrid_client.close() assert process.returncode == 0 and hybrid_client.diagnostics == b"" evidence["hybrid_transport"] = { "handshake": handshake["result"], "stdout_lines": len(hybrid_client.lines), "exit_code": process.returncode, "stderr": hybrid_client.diagnostics.decode("utf-8"), "sd3_template": None, } assert original_template.read_bytes() == original_bytes assert (original_template.stat().st_ino, original_template.stat().st_mtime_ns) == ( original_info.st_ino, original_info.st_mtime_ns, ) assert sorted(path.name for path in outputs.iterdir()) == sorted( [ "default", "explicit", "native", "native-alias", "native-actual", "native-default", "native-midi", "target-no-template", "text-recovered", "phrase-original", "phrase-nearby", *control_outputs, *hybrid_outputs, ] ) assert not list(outputs.rglob("*.agp")) and not list(outputs.rglob("*.sdp")) print(f"Installed-wheel server evidence: {json.dumps(evidence, sort_keys=True)}") print(f"Installed-wheel disk evidence: {json.dumps(disk_evidence, sort_keys=True)}") print(f"Installed-wheel native service evidence: {json.dumps(native_evidence, sort_keys=True)}") assert hashlib.sha256(wheel.read_bytes()).hexdigest() == wheel_sha256 evidence.update( { "wheel_path": str(wheel), "wheel_sha256": wheel_sha256, "native_stdout_lines": len(native_client.lines), "native_exit_code": process.returncode, "native_stderr": native_client.diagnostics.decode("utf-8"), "disk": disk_evidence, "native": native_evidence, } ) EVIDENCE.mkdir(parents=True, exist_ok=True) (EVIDENCE / "distribution-acceptance.json").write_text( json.dumps(evidence, indent=2), encoding="utf-8" )