741 lines
30 KiB
Python
741 lines
30 KiB
Python
"""Real native service transactions, saved content and operator-template acceptance."""
|
|
|
|
import asyncio
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import shutil
|
|
import sys
|
|
import uuid
|
|
import xml.etree.ElementTree as ET
|
|
from collections.abc import Callable, Iterator
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from pathlib import Path
|
|
from threading import Barrier
|
|
from unittest.mock import patch
|
|
|
|
import anyio
|
|
import mido # type: ignore[import-untyped]
|
|
import pytest
|
|
from jsonschema import Draft202012Validator
|
|
from test_stdio import TIMEOUT, RawStdio, _environment
|
|
|
|
from riff_groove_builder.articulations import apply_scheduled_controls, compile_score
|
|
from riff_groove_builder.compose import compose_score
|
|
from riff_groove_builder.description import resolve_description
|
|
from riff_groove_builder.errors import (
|
|
DomainError,
|
|
NativeFormatUnavailableError,
|
|
OutputExistsError,
|
|
OutputIOError,
|
|
SerializationError,
|
|
UnsupportedArticulationError,
|
|
)
|
|
from riff_groove_builder.formats.agp import validate_riffer_file
|
|
from riff_groove_builder.formats.sdp import parse_sdp_project, validate_sdp_file, write_sdp
|
|
from riff_groove_builder.midi import validate_midi_file
|
|
from riff_groove_builder.profiles import InstrumentProfile, load_profile_registry
|
|
from riff_groove_builder.schemas import (
|
|
EventPlan,
|
|
GenerateMusicRequest,
|
|
GenerationResult,
|
|
Part,
|
|
ProfileSelection,
|
|
tool_output_schema,
|
|
)
|
|
from riff_groove_builder.service import GenerationService, _Stage
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
TEMPLATE = ROOT / "tests" / "fixtures" / "sd3" / "original-template.sd3p"
|
|
TARGETS = {
|
|
"lead": "ample.ame.4.0.1.riffer.v1",
|
|
"bass": "ample.amr.4.1.0.riffer.v1",
|
|
"drums": "toontrack.sd3.3.4.4.core.v1",
|
|
}
|
|
pytestmark = pytest.mark.integration
|
|
|
|
|
|
@pytest.fixture
|
|
def workspace() -> Iterator[Path]:
|
|
path = ROOT / ".test-artifacts" / f"native-service-{uuid.uuid4().hex}"
|
|
path.mkdir(parents=True)
|
|
try:
|
|
yield path
|
|
finally:
|
|
shutil.rmtree(path)
|
|
|
|
|
|
def job(**updates: object) -> GenerateMusicRequest:
|
|
return GenerateMusicRequest.model_validate(
|
|
{
|
|
"description": "lead bass drums palm mute 137.5 BPM 2 bars 7/8 Bb minor quarter rests",
|
|
"basename": "original",
|
|
"seed": 42,
|
|
**updates,
|
|
}
|
|
)
|
|
|
|
|
|
def plan_for(request: GenerateMusicRequest) -> tuple[EventPlan, dict[Part, InstrumentProfile]]:
|
|
intent = resolve_description(request).intent
|
|
profiles = load_profile_registry().select(intent.parts, request.profiles or ProfileSelection())
|
|
return apply_scheduled_controls(
|
|
compile_score(compose_score(intent, profiles), profiles), request.cc_events, profiles
|
|
), profiles
|
|
|
|
|
|
def validate_saved(
|
|
result: GenerationResult, request: GenerateMusicRequest, template: Path | None
|
|
) -> dict[str, object]:
|
|
"""Read actual files with validators plus independent raw MIDI/XML/triple checks."""
|
|
Draft202012Validator(tool_output_schema("generate_music")).validate(
|
|
result.model_dump(mode="json")
|
|
)
|
|
plan, profiles = plan_for(request)
|
|
assert result.resolved.profile_ids.selected() == {
|
|
part: profiles[part].id for part in plan.score.intent.parts
|
|
}
|
|
assert result.resolved.tempo_bpm == plan.score.intent.tempo_bpm
|
|
assert result.resolved.time_signature == plan.score.intent.time_signature
|
|
assert result.resolved.key == plan.score.intent.key
|
|
assert result.resolved.seed == request.seed
|
|
evidence: dict[str, object] = {}
|
|
for artifact in result.artifacts:
|
|
path = Path(artifact.path)
|
|
raw = path.read_bytes()
|
|
assert path.suffix == f".{artifact.format}" and len(raw) == artifact.size_bytes > 0
|
|
assert artifact.profile_ids == [profiles[part].id for part in artifact.parts]
|
|
details: dict[str, object] = {
|
|
"parts": artifact.parts,
|
|
"profiles": artifact.profile_ids,
|
|
"size": len(raw),
|
|
"sha256": hashlib.sha256(raw).hexdigest(),
|
|
}
|
|
if artifact.format == "mid":
|
|
validate_midi_file(path, plan)
|
|
midi = mido.MidiFile(path)
|
|
assert raw.startswith(b"MThd")
|
|
assert midi.type == 1 and midi.ticks_per_beat == 960
|
|
assert [sum(message.time for message in track) for track in midi.tracks] == [
|
|
plan.end_tick
|
|
] * (len(result.resolved.parts) + 1)
|
|
conductor = midi.tracks[0]
|
|
assert next(m.tempo for m in conductor if m.type == "set_tempo") == round(
|
|
60_000_000 / result.resolved.tempo_bpm
|
|
)
|
|
sig = next(m for m in conductor if m.type == "time_signature")
|
|
assert (sig.numerator, sig.denominator) == (
|
|
result.resolved.time_signature.numerator,
|
|
result.resolved.time_signature.denominator,
|
|
)
|
|
counts = {}
|
|
for part, track in zip(result.resolved.parts, midi.tracks[1:], strict=True):
|
|
notes = [m for m in track if m.type == "note_on"]
|
|
assert {m.channel for m in notes} == {profiles[part].channel}
|
|
controls = [m for m in notes if part != "drums" and m.note in (24, 26)]
|
|
musical = [m for m in notes if m not in controls]
|
|
assert len(musical) == len(
|
|
next(t for t in plan.score.tracks if t.part == part).notes
|
|
)
|
|
if request.description.find("palm mute") >= 0 and part != "drums":
|
|
assert [m.note for m in controls] == [26 if part == "lead" else 24]
|
|
assert {m.velocity for m in controls} == {100}
|
|
assert (
|
|
all(32 <= m.velocity <= 63 for m in musical)
|
|
if part == "bass"
|
|
else all(1 <= m.velocity <= 127 for m in musical)
|
|
)
|
|
if part == "drums":
|
|
assert {m.note for m in musical} <= {36, 38, 42}
|
|
counts[part] = len(musical)
|
|
details["musical_note_counts"] = counts
|
|
elif artifact.format in ("griff", "briff"):
|
|
part = artifact.parts[0]
|
|
saved = validate_riffer_file(path, plan, part, profiles[part])
|
|
assert raw.startswith(b"<?xml") and not raw.startswith(b"MThd")
|
|
xml = ET.fromstring(raw)
|
|
assert xml.tag == "Riff" and xml.attrib["PPQBase"] == "480"
|
|
wanted = "PM" if part == "lead" else "PMP"
|
|
if "palm mute" in request.description:
|
|
assert {n.articulation for n in saved.notes} == {wanted}
|
|
details["notes"] = len(saved.notes)
|
|
details["techniques"] = sorted({n.articulation for n in saved.notes})
|
|
details["velocity_range"] = [
|
|
min(n.velocity for n in saved.notes),
|
|
max(n.velocity for n in saved.notes),
|
|
]
|
|
else:
|
|
assert template is not None
|
|
saved_drums = validate_sdp_file(
|
|
path, plan, "drums", profiles["drums"], template=template
|
|
)
|
|
project = parse_sdp_project(raw)
|
|
assert saved_drums.notes == project.source_notes
|
|
assert project.non_song_bytes == parse_sdp_project(template.read_bytes()).non_song_bytes
|
|
assert {n.channel for n in saved_drums.notes} == {9}
|
|
assert {n.pitch for n in saved_drums.notes} <= {36, 38, 42}
|
|
assert saved_drums.tempo_bpm == result.resolved.tempo_bpm
|
|
assert saved_drums.end == plan.score.intent.duration_beats
|
|
details["notes"] = len(saved_drums.notes)
|
|
details["non_song_sha256"] = saved_drums.non_song_sha256
|
|
details["velocity_range"] = [
|
|
min(n.velocity for n in saved_drums.notes),
|
|
max(n.velocity for n in saved_drums.notes),
|
|
]
|
|
evidence[artifact.format] = details
|
|
assert {p.name for p in Path(result.artifacts[0].path).parent.iterdir()} == {
|
|
Path(a.path).name for a in result.artifacts
|
|
}
|
|
return evidence
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"formats",
|
|
[
|
|
None,
|
|
["agp", "sdp"],
|
|
["griff", "briff", "sd3p"],
|
|
["mid"],
|
|
["agp", "griff", "briff", "sdp", "sd3p", "mid"],
|
|
],
|
|
)
|
|
@pytest.mark.parametrize(
|
|
"description",
|
|
[
|
|
"lead bass drums palm mute 137.5 BPM 2 bars 7/8 Bb minor quarter rests",
|
|
"lead bass drums 123 BPM 1 bar 3/4 F# major sixteenth metal",
|
|
],
|
|
)
|
|
def test_all_parts_native_defaults_aliases_and_midi(
|
|
workspace: Path, formats: list[str] | None, description: str
|
|
) -> None:
|
|
before = TEMPLATE.stat(), TEMPLATE.read_bytes()
|
|
request = job(formats=formats, description=description)
|
|
result = GenerationService(workspace, sd3_template=TEMPLATE).generate_music(request)
|
|
expected = (
|
|
["mid"]
|
|
if formats == ["mid"]
|
|
else ["griff", "briff", "sd3p"]
|
|
if formats in (["agp", "sdp"], ["griff", "briff", "sd3p"])
|
|
else ["mid", "griff", "briff", "sd3p"]
|
|
)
|
|
assert result.status == "complete"
|
|
assert [a.format for a in result.artifacts] == expected
|
|
assert [w.code for w in result.warnings] == ["VERIFIED_SUBSET_LIMITS"] * 3
|
|
if "palm mute" in description:
|
|
assert any(
|
|
"drums retain normal" in assumption for assumption in result.resolved.assumptions
|
|
)
|
|
validate_saved(result, request, TEMPLATE)
|
|
after = TEMPLATE.stat()
|
|
assert before[1] == TEMPLATE.read_bytes()
|
|
assert (before[0].st_ino, before[0].st_mtime_ns) == (after.st_ino, after.st_mtime_ns)
|
|
assert list(workspace.iterdir()) == [workspace / "original"]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("part", "alias", "actual"),
|
|
[("lead", "agp", "griff"), ("bass", "agp", "briff"), ("drums", "sdp", "sd3p")],
|
|
)
|
|
def test_single_part_alias_and_actual_are_identical(
|
|
workspace: Path, part: Part, alias: str, actual: str
|
|
) -> None:
|
|
service = GenerationService(workspace, sd3_template=TEMPLATE)
|
|
outputs = []
|
|
for name, extension in (("alias", alias), ("actual", actual)):
|
|
request = job(basename=name, description=f"{part} only 1 bar quarter", formats=[extension])
|
|
result = service.generate_music(request)
|
|
assert len(result.artifacts) == 1 and result.artifacts[0].format == actual
|
|
assert result.artifacts[0].parts == [part]
|
|
validate_saved(result, request, TEMPLATE)
|
|
outputs.append(Path(result.artifacts[0].path).read_bytes())
|
|
assert outputs[0] == outputs[1]
|
|
|
|
|
|
def test_missing_template_keeps_real_riffer_and_fallback_only_drums(workspace: Path) -> None:
|
|
request = job()
|
|
result = GenerationService(workspace).generate_music(request)
|
|
assert result.status == "midi_fallback"
|
|
assert [a.format for a in result.artifacts] == ["mid", "griff", "briff"]
|
|
assert result.artifacts[0].fallback_for == ["drums"]
|
|
assert [(w.part, w.code) for w in result.warnings if w.code == "NATIVE_FORMAT_UNAVAILABLE"] == [
|
|
("drums", "NATIVE_FORMAT_UNAVAILABLE")
|
|
]
|
|
assert "sd3p: sdp_template_required" in result.warnings[-1].message
|
|
validate_saved(result, request, None)
|
|
|
|
|
|
def test_missing_template_fallback_disabled_never_stages(workspace: Path) -> None:
|
|
service = GenerationService(workspace)
|
|
with (
|
|
patch.object(service, "_new_stage", side_effect=AssertionError),
|
|
pytest.raises(NativeFormatUnavailableError) as caught,
|
|
):
|
|
service.generate_music(job(allow_midi_fallback=False))
|
|
assert caught.value.details == {
|
|
"format": "sd3p",
|
|
"part": "drums",
|
|
"reason": "sdp_template_required",
|
|
}
|
|
assert not list(workspace.iterdir())
|
|
|
|
|
|
def test_default_target_midi_only_needs_no_template(workspace: Path) -> None:
|
|
request = job(formats=["mid"], allow_midi_fallback=False)
|
|
result = GenerationService(workspace).generate_music(request)
|
|
assert result.status == "complete" and result.artifacts[0].fallback_for == []
|
|
assert result.resolved.profile_ids.selected() == TARGETS
|
|
validate_saved(result, request, None)
|
|
|
|
|
|
@pytest.mark.parametrize("allow", [False, True])
|
|
def test_known_native_decimal_limit_is_preflighted(workspace: Path, allow: bool) -> None:
|
|
service = GenerationService(workspace, sd3_template=TEMPLATE)
|
|
request = job(tempo_bpm=137.1234567, allow_midi_fallback=allow)
|
|
if not allow:
|
|
with (
|
|
patch.object(service, "_new_stage", side_effect=AssertionError),
|
|
pytest.raises(NativeFormatUnavailableError) as caught,
|
|
):
|
|
service.generate_music(request)
|
|
assert caught.value.details["reason"] == "sd3_timing_not_representable"
|
|
assert not list(workspace.iterdir())
|
|
else:
|
|
result = service.generate_music(request)
|
|
assert result.status == "midi_fallback"
|
|
assert [a.format for a in result.artifacts] == ["mid", "griff", "briff"]
|
|
assert result.artifacts[0].fallback_for == ["drums"]
|
|
validate_saved(result, request, None)
|
|
|
|
|
|
@pytest.mark.parametrize("allow", [False, True])
|
|
def test_common_preroll_is_never_silently_removed(workspace: Path, allow: bool) -> None:
|
|
request = job(allow_midi_fallback=allow)
|
|
plan, _ = plan_for(request)
|
|
plan.musical_start_tick += 120
|
|
plan.musical_end_tick += 120
|
|
plan.end_tick += 120
|
|
for track in plan.tracks:
|
|
for event in track.events:
|
|
event.tick += 120
|
|
with patch("riff_groove_builder.service.compile_score", return_value=plan):
|
|
if allow:
|
|
result = GenerationService(workspace, sd3_template=TEMPLATE).generate_music(request)
|
|
assert [a.format for a in result.artifacts] == ["mid"]
|
|
assert result.artifacts[0].fallback_for == ["lead", "bass", "drums"]
|
|
assert result.resolved.musical_start_tick == 120
|
|
validate_midi_file(Path(result.artifacts[0].path), plan)
|
|
else:
|
|
with pytest.raises(NativeFormatUnavailableError):
|
|
GenerationService(workspace, sd3_template=TEMPLATE).generate_music(request)
|
|
assert not list(workspace.iterdir())
|
|
|
|
|
|
def test_plan_mismatch_is_not_capability_fallback(workspace: Path) -> None:
|
|
request = job()
|
|
plan, _ = plan_for(request)
|
|
event = next(
|
|
e for e in plan.tracks[0].events if e.kind == "note_on" and e.role == "musical_note"
|
|
)
|
|
assert event.kind == "note_on"
|
|
event.velocity -= 1
|
|
with (
|
|
patch("riff_groove_builder.service.compile_score", return_value=plan),
|
|
pytest.raises(SerializationError) as caught,
|
|
):
|
|
GenerationService(workspace, sd3_template=TEMPLATE).generate_music(request)
|
|
assert caught.value.details["reason"] == "score_event_plan_mismatch"
|
|
assert not list(workspace.iterdir())
|
|
|
|
|
|
@pytest.mark.parametrize("kind", ["corrupt", "version", "missing", "library"])
|
|
def test_explicit_bad_template_is_visible_not_fallback(workspace: Path, kind: str) -> None:
|
|
template = workspace / "operator.sd3p"
|
|
raw = TEMPLATE.read_bytes()
|
|
if kind == "corrupt":
|
|
template.write_bytes(b"private malformed project {")
|
|
elif kind == "version":
|
|
template.write_bytes(raw.replace(b"3.4.4", b"3.4.5"))
|
|
elif kind == "library":
|
|
template.write_bytes(raw.replace(b"SL-DFH", b"SL-OTHER"))
|
|
with pytest.raises(OutputIOError if kind == "missing" else SerializationError):
|
|
GenerationService(workspace, sd3_template=template).generate_music(job())
|
|
assert not list(workspace.glob(".riff-stage-*")) and not (workspace / "original").exists()
|
|
|
|
|
|
def test_runtime_summaries_reflect_template_and_subset_without_mutating_registry(
|
|
workspace: Path,
|
|
) -> None:
|
|
registry = load_profile_registry()
|
|
for template, ready in ((None, False), (TEMPLATE, True)):
|
|
listed = GenerationService(
|
|
workspace, registry=registry, sd3_template=template
|
|
).list_instrument_profiles()
|
|
for summary in listed.profiles:
|
|
assert (
|
|
summary.native_format
|
|
== {"lead": "griff", "bass": "briff", "drums": "sd3p"}[summary.part]
|
|
)
|
|
if summary.id.startswith("standard."):
|
|
assert not summary.native_available
|
|
else:
|
|
assert summary.native_available == (summary.part != "drums" or ready)
|
|
assert "Default profile" in summary.limitations[-1]
|
|
assert "certification" in summary.limitations[-1]
|
|
if summary.part == "drums":
|
|
assert summary.product_version == "3.4.4"
|
|
assert (
|
|
"sdp_template_required" in summary.limitations[-1]
|
|
if not ready
|
|
else ("sd3_3_4_4_template_song" in summary.limitations[-1])
|
|
)
|
|
assert all(s.native_available for s in registry.summaries() if not s.id.startswith("standard."))
|
|
assert not list(workspace.iterdir())
|
|
|
|
|
|
def test_standard_profiles_stay_explicit_and_do_not_approximate_palm_mute(workspace: Path) -> None:
|
|
with pytest.raises(UnsupportedArticulationError):
|
|
GenerationService(workspace).generate_music(
|
|
job(description="lead only palm mute", profiles={"lead": "standard.lead.v1"})
|
|
)
|
|
with pytest.raises(UnsupportedArticulationError) as caught:
|
|
GenerationService(workspace).generate_music(job(description="drums only palm mute"))
|
|
assert caught.value.details["reason"] == "pitched_parts_required"
|
|
assert not list(workspace.iterdir())
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"operation",
|
|
[
|
|
"write_riffer",
|
|
"write_sdp",
|
|
"validate_riffer_file",
|
|
"validate_sdp_file",
|
|
],
|
|
)
|
|
@pytest.mark.parametrize("error_type", [SerializationError, OutputIOError])
|
|
def test_native_faults_never_fallback_or_publish(
|
|
workspace: Path, operation: str, error_type: type[DomainError]
|
|
) -> None:
|
|
sentinel = workspace / "sentinel"
|
|
sentinel.write_bytes(b"operator sentinel")
|
|
before = sentinel.stat()
|
|
with (
|
|
patch(
|
|
f"riff_groove_builder.service.{operation}",
|
|
side_effect=error_type(details={"reason": "injected_failure"}),
|
|
),
|
|
pytest.raises(error_type),
|
|
):
|
|
GenerationService(workspace, sd3_template=TEMPLATE).generate_music(job())
|
|
assert list(workspace.iterdir()) == [sentinel]
|
|
assert (sentinel.stat().st_ino, sentinel.stat().st_mtime_ns) == (
|
|
before.st_ino,
|
|
before.st_mtime_ns,
|
|
)
|
|
|
|
|
|
def test_last_native_partial_write_cleans_all_owned_files(workspace: Path) -> None:
|
|
def fail(
|
|
path: Path,
|
|
plan: EventPlan,
|
|
part: Part,
|
|
profile: InstrumentProfile,
|
|
*,
|
|
template: Path | None,
|
|
on_created: Callable[[os.stat_result], None],
|
|
) -> Path:
|
|
with path.open("xb") as stream:
|
|
on_created(os.fstat(stream.fileno()))
|
|
stream.write(b"partial native bytes")
|
|
assert {p.suffix for p in path.parent.iterdir()} == {".mid", ".griff", ".briff", ".sd3p"}
|
|
raise OutputIOError(details={"reason": "last_native_short_write"})
|
|
|
|
with (
|
|
patch("riff_groove_builder.service.write_sdp", side_effect=fail),
|
|
pytest.raises(OutputIOError) as caught,
|
|
):
|
|
GenerationService(workspace, sd3_template=TEMPLATE).generate_music(job())
|
|
assert caught.value.details["reason"] == "last_native_short_write"
|
|
assert not list(workspace.iterdir())
|
|
|
|
|
|
def test_actual_saved_sd3_corruption_fails_and_cleans_complete_set(workspace: Path) -> None:
|
|
def corrupt(
|
|
path: Path,
|
|
plan: EventPlan,
|
|
part: Part,
|
|
profile: InstrumentProfile,
|
|
*,
|
|
template: Path | None,
|
|
on_created: Callable[[os.stat_result], None],
|
|
) -> Path:
|
|
write_sdp(path, plan, part, profile, template=template, on_created=on_created)
|
|
raw = path.read_bytes()
|
|
assert b"TrackLength 7" in raw
|
|
path.write_bytes(raw.replace(b"TrackLength 7", b"TrackLength 8"))
|
|
return path
|
|
|
|
with (
|
|
patch("riff_groove_builder.service.write_sdp", side_effect=corrupt),
|
|
pytest.raises(SerializationError),
|
|
):
|
|
GenerationService(workspace, sd3_template=TEMPLATE).generate_music(job())
|
|
assert not list(workspace.iterdir())
|
|
|
|
|
|
@pytest.mark.parametrize("cleanup_denied", [False, True])
|
|
def test_multi_artifact_publication_and_cleanup_failure(
|
|
workspace: Path, cleanup_denied: bool
|
|
) -> None:
|
|
sentinel = workspace / "user-sentinel"
|
|
sentinel.write_bytes(b"preserved")
|
|
with (
|
|
patch.object(Path, "rename", side_effect=PermissionError),
|
|
pytest.raises(OutputIOError) as caught,
|
|
):
|
|
if cleanup_denied:
|
|
with patch.object(Path, "unlink", side_effect=PermissionError):
|
|
GenerationService(workspace, sd3_template=TEMPLATE).generate_music(job())
|
|
else:
|
|
GenerationService(workspace, sd3_template=TEMPLATE).generate_music(job())
|
|
assert caught.value.details["reason"] == (
|
|
"stage_cleanup_failed" if cleanup_denied else "publication_failed"
|
|
)
|
|
assert sentinel.read_bytes() == b"preserved" and not (workspace / "original").exists()
|
|
stages = list(workspace.glob(".riff-stage-*"))
|
|
assert len(stages) == (1 if cleanup_denied else 0)
|
|
if cleanup_denied:
|
|
assert {p.suffix for p in stages[0].iterdir()} == {".mid", ".griff", ".briff", ".sd3p"}
|
|
|
|
|
|
@pytest.mark.parametrize("injection", ["unexpected", "same-name-replacement", "late-corruption"])
|
|
def test_multi_file_ownership_and_last_writer_mutation(workspace: Path, injection: str) -> None:
|
|
def inject(
|
|
path: Path,
|
|
plan: EventPlan,
|
|
part: Part,
|
|
profile: InstrumentProfile,
|
|
*,
|
|
template: Path | None,
|
|
on_created: Callable[[os.stat_result], None],
|
|
) -> Path:
|
|
write_sdp(path, plan, part, profile, template=template, on_created=on_created)
|
|
lead = path.parent / "original_lead.griff"
|
|
if injection == "unexpected":
|
|
(path.parent / "user-sentinel").write_bytes(b"unowned")
|
|
elif injection == "same-name-replacement":
|
|
# Keep the old inode allocated, so the new inode cannot reuse it.
|
|
lead.rename(workspace / "original-owned.griff")
|
|
lead.write_bytes(b"unowned replacement")
|
|
else:
|
|
before = lead.stat()
|
|
raw = lead.read_bytes()
|
|
lead.write_bytes(raw.replace(b"PM", b"XX"))
|
|
os.utime(lead, ns=(before.st_atime_ns, before.st_mtime_ns))
|
|
return path
|
|
|
|
with (
|
|
patch("riff_groove_builder.service.write_sdp", side_effect=inject),
|
|
pytest.raises(OutputIOError) as caught,
|
|
):
|
|
GenerationService(workspace, sd3_template=TEMPLATE).generate_music(job())
|
|
assert not (workspace / "original").exists()
|
|
if injection == "late-corruption":
|
|
assert caught.value.details["reason"] == "staged_content_changed"
|
|
assert not list(workspace.iterdir())
|
|
else:
|
|
assert caught.value.details["reason"] == "stage_cleanup_failed"
|
|
stages = list(workspace.glob(".riff-stage-*"))
|
|
assert len(stages) == 1
|
|
preserved = stages[0] / (
|
|
"user-sentinel" if injection == "unexpected" else "original_lead.griff"
|
|
)
|
|
assert preserved.read_bytes().startswith(b"unowned")
|
|
|
|
|
|
def test_template_changed_after_configuration_is_error_before_staging(workspace: Path) -> None:
|
|
template = workspace / "operator.sd3p"
|
|
template.write_bytes(TEMPLATE.read_bytes())
|
|
service = GenerationService(workspace, sd3_template=template)
|
|
template.write_bytes(template.read_bytes() + b"\n")
|
|
with pytest.raises(SerializationError) as caught:
|
|
service.generate_music(job())
|
|
assert caught.value.details["reason"] == "sd3_template_changed"
|
|
assert list(workspace.iterdir()) == [template]
|
|
|
|
|
|
def test_multifile_concurrent_publication_has_one_winner(workspace: Path) -> None:
|
|
barrier = Barrier(2)
|
|
publish = GenerationService._publish
|
|
|
|
def synchronized(self: GenerationService, stage: _Stage, destination: Path) -> None:
|
|
barrier.wait(timeout=30)
|
|
publish(self, stage, destination)
|
|
|
|
def run(service: GenerationService) -> GenerationResult | DomainError:
|
|
try:
|
|
return service.generate_music(job())
|
|
except DomainError as error:
|
|
return error
|
|
|
|
with patch.object(GenerationService, "_publish", synchronized), ThreadPoolExecutor(2) as pool:
|
|
results = list(
|
|
pool.map(
|
|
run,
|
|
[
|
|
GenerationService(workspace, sd3_template=TEMPLATE),
|
|
GenerationService(workspace, sd3_template=TEMPLATE),
|
|
],
|
|
)
|
|
)
|
|
winners = [r for r in results if isinstance(r, GenerationResult)]
|
|
assert len(winners) == 1 and sum(isinstance(r, OutputExistsError) for r in results) == 1
|
|
validate_saved(winners[0], job(), TEMPLATE)
|
|
assert list(workspace.iterdir()) == [workspace / "original"]
|
|
|
|
|
|
def test_genuine_local_template_readonly_acceptance_when_configured(workspace: Path) -> None:
|
|
"""Ordinary portable runs need no private template; an explicit local run records evidence."""
|
|
configured = os.environ.get("RIFF_SD3_TEMPLATE")
|
|
if configured is None:
|
|
return
|
|
template = Path(configured)
|
|
before = template.stat(), template.read_bytes()
|
|
request = job(basename="genuine-original")
|
|
if os.environ.get("RIFF_NATIVE_EVIDENCE") == "1":
|
|
workspace = ROOT / ".test-artifacts" / f"step9-genuine-{uuid.uuid4().hex}"
|
|
workspace.mkdir()
|
|
result = GenerationService(workspace, sd3_template=template).generate_music(request)
|
|
evidence = validate_saved(result, request, template)
|
|
after = template.stat()
|
|
assert template.read_bytes() == before[1]
|
|
assert (before[0].st_ino, before[0].st_size, before[0].st_mtime_ns) == (
|
|
after.st_ino,
|
|
after.st_size,
|
|
after.st_mtime_ns,
|
|
)
|
|
record = {
|
|
"source_sha256": hashlib.sha256(before[1]).hexdigest(),
|
|
"source_inode": before[0].st_ino,
|
|
"source_size": before[0].st_size,
|
|
"source_mtime_ns": before[0].st_mtime_ns,
|
|
"result": result.model_dump(mode="json"),
|
|
"artifacts": evidence,
|
|
}
|
|
if os.environ.get("RIFF_NATIVE_EVIDENCE") == "1":
|
|
(workspace / "evidence.json").write_text(json.dumps(record, indent=2), encoding="utf-8")
|
|
print(json.dumps(record, sort_keys=True))
|
|
|
|
|
|
@pytest.fixture
|
|
def anyio_backend() -> str:
|
|
return "asyncio"
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_real_stdio_all_native_default_alias_actual_and_midi(workspace: Path) -> None:
|
|
if os.environ.get("RIFF_NATIVE_EVIDENCE") == "1":
|
|
workspace = ROOT / ".test-artifacts" / f"step9-stdio-{uuid.uuid4().hex}"
|
|
workspace.mkdir()
|
|
outputs, cwd = workspace / "outputs", workspace / "other-cwd"
|
|
outputs.mkdir()
|
|
cwd.mkdir()
|
|
template = workspace / "original-template.sd3p"
|
|
template.write_bytes(TEMPLATE.read_bytes())
|
|
before = template.stat(), template.read_bytes()
|
|
process = await asyncio.wait_for(
|
|
asyncio.create_subprocess_exec(
|
|
sys.executable,
|
|
"-I",
|
|
"-B",
|
|
"-m",
|
|
"riff_groove_builder",
|
|
"--output-dir",
|
|
str(outputs),
|
|
"--sd3-template",
|
|
str(template),
|
|
cwd=cwd,
|
|
env=_environment(),
|
|
stdin=asyncio.subprocess.PIPE,
|
|
stdout=asyncio.subprocess.PIPE,
|
|
stderr=asyncio.subprocess.PIPE,
|
|
),
|
|
TIMEOUT,
|
|
)
|
|
client = RawStdio(process)
|
|
records = []
|
|
try:
|
|
with anyio.fail_after(60):
|
|
await client.initialize()
|
|
tools = (await client.request(1, "tools/list"))["result"]["tools"]
|
|
assert len(tools) == 2
|
|
assert "--sd3-template" not in json.dumps(tools)
|
|
for tool in tools:
|
|
Draft202012Validator.check_schema(tool["inputSchema"])
|
|
Draft202012Validator.check_schema(tool["outputSchema"])
|
|
listed = await client.request(
|
|
2, "tools/call", {"name": "list_instrument_profiles", "arguments": {}}
|
|
)
|
|
summaries = listed["result"]["structuredContent"]["profiles"]
|
|
assert all(
|
|
s["native_available"] for s in summaries if s["mapping_kind"] == "instrument"
|
|
)
|
|
for name, formats in (
|
|
("default", None),
|
|
("aliases", ["agp", "sdp"]),
|
|
("actual", ["griff", "briff", "sd3p"]),
|
|
("midi", ["mid"]),
|
|
):
|
|
request = job(basename=name, formats=formats, allow_midi_fallback=False)
|
|
arguments = request.model_dump(mode="json", exclude_none=True)
|
|
response = await client.request(
|
|
name, "tools/call", {"name": "generate_music", "arguments": arguments}
|
|
)
|
|
assert response["result"]["isError"] is False
|
|
result = GenerationResult.model_validate(response["result"]["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"]
|
|
)
|
|
records.append(
|
|
{
|
|
"request": arguments,
|
|
"result": result.model_dump(mode="json"),
|
|
"saved": validate_saved(result, request, template),
|
|
}
|
|
)
|
|
response = await client.request(
|
|
"untrusted-template",
|
|
"tools/call",
|
|
{
|
|
"name": "generate_music",
|
|
"arguments": {
|
|
"description": "music",
|
|
"basename": "bad",
|
|
"sd3_template": "private-user-path",
|
|
},
|
|
},
|
|
)
|
|
assert response["result"]["isError"]
|
|
assert response["result"]["structuredContent"]["code"] == "INVALID_REQUEST"
|
|
assert "private-user-path" not in json.dumps(response)
|
|
finally:
|
|
with anyio.CancelScope(shield=True):
|
|
await client.close()
|
|
assert process.returncode == 0 and client.diagnostics == b""
|
|
assert template.read_bytes() == before[1]
|
|
assert (template.stat().st_ino, template.stat().st_mtime_ns) == (
|
|
before[0].st_ino,
|
|
before[0].st_mtime_ns,
|
|
)
|
|
assert not list(outputs.glob(".riff-stage-*"))
|
|
assert len(client.lines) == 8
|
|
if os.environ.get("RIFF_NATIVE_EVIDENCE") == "1":
|
|
(workspace / "evidence.json").write_text(json.dumps(records, indent=2), encoding="utf-8")
|
|
print(f"Native stdio evidence: {json.dumps(records, sort_keys=True)}")
|