import argparse import sys from pathlib import Path import yaml def validate_state(state): errors = [] if not isinstance(state, dict): return ["document root must be a mapping"] # Establish the declared source map before validating looper references. sources = state.get("sources") if not isinstance(sources, list): return ["sources must be a list"] source_by_id = {} physical_inputs = set() capture_ports = set() for index, source in enumerate(sources): location = f"sources[{index}]" if not isinstance(source, dict): errors.append(f"{location} must be a mapping") continue source_id = source.get("id") if not isinstance(source_id, str) or not source_id: errors.append(f"{location}.id must be a non-empty string") elif source_id in source_by_id: errors.append(f"duplicate source id: {source_id}") else: source_by_id[source_id] = source physical_input = source.get("physical_input") if not isinstance(physical_input, int) or isinstance(physical_input, bool): errors.append(f"{location}.physical_input must be an integer") elif physical_input in physical_inputs: errors.append(f"duplicate physical input: {physical_input}") else: physical_inputs.add(physical_input) capture_port = source.get("capture_port") if not isinstance(capture_port, str) or not capture_port: errors.append(f"{location}.capture_port must be a non-empty string") elif capture_port in capture_ports: errors.append(f"duplicate capture port: {capture_port}") else: capture_ports.add(capture_port) # Keep the first four-channel milestone tied to its four named instruments. expected_source_ids = {"guitar", "bass", "voice", "synth"} if set(source_by_id) != expected_source_ids: errors.append("source ids must be guitar, bass, voice, and synth exactly once") if len(sources) != 4: errors.append(f"exactly four sources are required, got {len(sources)}") if physical_inputs != {1, 2, 3, 4}: errors.append("physical inputs must cover 1 through 4 exactly once") # The required looper is one instance containing one four-channel loop. system = state.get("system") components = system.get("components") if isinstance(system, dict) else None if not isinstance(components, list): errors.append("system.components must be a list") components = [] loopers = [item for item in components if isinstance(item, dict) and item.get("id") == "looper"] if len(loopers) != 1: errors.append(f"exactly one looper component is required, got {len(loopers)}") elif loopers[0].get("instance_count") != 1: errors.append("looper.instance_count must be 1") elif loopers[0].get("loop_count") != 1: errors.append("looper.loop_count must be 1") elif loopers[0].get("channels_per_loop") != 4: errors.append("looper.channels_per_loop must be 4") routing = state.get("routing") if not isinstance(routing, dict): errors.append("routing must be a mapping") return errors looper_inputs = routing.get("looper_inputs") if not isinstance(looper_inputs, list): errors.append("routing.looper_inputs must be a list") looper_inputs = [] # Check that each source is assigned exactly once across all loop channels. input_channels = set() input_sources = set() for index, loop_input in enumerate(looper_inputs): location = f"routing.looper_inputs[{index}]" if not isinstance(loop_input, dict): errors.append(f"{location} must be a mapping") continue source_id = loop_input.get("source") channel = loop_input.get("channel") if source_id not in source_by_id: errors.append(f"{location}.source references unknown source: {source_id}") if source_id in input_sources: errors.append(f"source is assigned to multiple looper inputs: {source_id}") input_sources.add(source_id) if not isinstance(channel, int) or isinstance(channel, bool) or channel not in range(1, 5): errors.append(f"{location}.channel must be an integer from 1 through 4") elif channel in input_channels: errors.append(f"duplicate looper input channel: {channel}") else: input_channels.add(channel) if len(looper_inputs) != 4 or input_channels != {1, 2, 3, 4}: errors.append("looper inputs must cover channels 1 through 4 exactly once") if input_sources != set(source_by_id): errors.append("each declared source must map to exactly one looper input") mix = routing.get("mix") if not isinstance(mix, dict): errors.append("routing.mix must be a mapping") else: output_port = mix.get("output_port") if not isinstance(output_port, str) or not output_port: errors.append("routing.mix.output_port must be a non-empty string") mix_id = mix.get("id") if not isinstance(mix_id, str) or not mix_id: errors.append("routing.mix.id must be a non-empty string") includes = mix.get("includes") if not isinstance(includes, list) or "looper_returns" not in includes: errors.append("routing.mix.includes must include looper_returns") # Loop returns join the output mix but must never feed the loop inputs. returns = routing.get("looper_returns") if not isinstance(returns, dict): errors.append("routing.looper_returns must be a mapping") elif returns.get("feed_looper_inputs") is not False: errors.append("routing.looper_returns.feed_looper_inputs must be false") connections = state.get("connections", []) if not isinstance(connections, list): errors.append("connections must be a list") connections = [] # An explicitly-forbidden connection documents policy; any other status is actionable. for index, connection in enumerate(connections): if not isinstance(connection, dict): errors.append(f"connections[{index}] must be a mapping") continue if ( connection.get("from") == "looper_returns" and connection.get("to") in { "looper_inputs", "looper_input_1", "looper_input_2", "looper_input_3", "looper_input_4", } and connection.get("status") != "explicitly-forbidden" ): errors.append(f"connections[{index}] routes looper returns into looper inputs") deployment = state.get("deployment") if not isinstance(deployment, dict): errors.append("deployment must be a mapping") else: for component, repository, asset, archive_format, bundle_paths in ( ( "lsp", "lsp-plugins/lsp-plugins", "lsp-plugins-{tag}-Linux-aarch64.7z", "7z", {"lsp-plugins.lv2": "lsp-plugins-{tag}-Linux-aarch64/LV2/lsp-plugins.lv2"}, ), ( "dragonfly_reverb", "michaelwillis/dragonfly-reverb", "dragonfly-reverb-{tag}-linux-arm64.tar.xz", "tar.xz", { "DragonflyEarlyReflections.lv2": "dragonfly-reverb-{tag}/DragonflyEarlyReflections.lv2", "DragonflyHallReverb.lv2": "dragonfly-reverb-{tag}/DragonflyHallReverb.lv2", "DragonflyPlateReverb.lv2": "dragonfly-reverb-{tag}/DragonflyPlateReverb.lv2", "DragonflyRoomReverb.lv2": "dragonfly-reverb-{tag}/DragonflyRoomReverb.lv2", }, ), ): plugin = deployment.get(component) if not isinstance(plugin, dict): errors.append(f"deployment.{component} must be a mapping") continue tag = plugin.get("tag") if not isinstance(tag, str) or not tag or any( character not in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-" for character in tag ): errors.append(f"deployment.{component}.tag must be a simple version tag") expected_url = f"https://github.com/{repository}/releases/download/{tag}/{asset.format(tag=tag)}" if plugin.get("archive_url") != expected_url: errors.append(f"deployment.{component}.archive_url must match its tag and approved release asset") if plugin.get("archive_format") != archive_format: errors.append(f"deployment.{component}.archive_format must be {archive_format}") expected_paths = { name: path.format(tag=tag) for name, path in bundle_paths.items() } if plugin.get("bundle_paths") != expected_paths: errors.append(f"deployment.{component}.bundle_paths are invalid") for bundle_name, bundle_path in expected_paths.items(): if bundle_name not in bundle_path: errors.append(f"deployment.{component}.bundle_paths keys must name their bundle directories") namlv2 = deployment.get("namlv2") if not isinstance(namlv2, dict): errors.append("deployment.namlv2 must be a mapping") else: tag = namlv2.get("tag") allowed_tag_characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-" if not isinstance(tag, str) or not tag or any( character not in allowed_tag_characters for character in tag ): errors.append("deployment.namlv2.tag must be a simple version tag") archive_url = namlv2.get("archive_url") expected_url = ( f"https://github.com/mikeoliphant/neural-amp-modeler-lv2/releases/download/{tag}/" "neural_amp_modeler_lv2_rpi5.tgz" ) if archive_url != expected_url: errors.append("deployment.namlv2.archive_url must match its tag and approved RPi 5 release asset") if namlv2.get("bundle_directory") != "neural_amp_modeler.lv2": errors.append("deployment.namlv2.bundle_directory must be neural_amp_modeler.lv2") sooperlooper = deployment.get("sooperlooper") if not isinstance(sooperlooper, dict): errors.append("deployment.sooperlooper must be a mapping") else: source_url = sooperlooper.get("source_url") if source_url != "https://github.com/abarker/sooperlooper-mods": errors.append("deployment.sooperlooper.source_url must match the approved GitHub repository") tag = sooperlooper.get("tag") if not isinstance(tag, str) or not tag or any( character not in allowed_tag_characters for character in tag ): errors.append("deployment.sooperlooper.tag must be a simple version tag") mod_host = deployment.get("mod_host") if not isinstance(mod_host, dict): errors.append("deployment.mod_host must be a mapping") else: source_url = mod_host.get("source_url") if source_url != "https://github.com/mod-audio/mod-host": errors.append("deployment.mod_host.source_url must match the approved GitHub repository") tag = mod_host.get("tag") if not isinstance(tag, str) or not tag or any( character not in allowed_tag_characters for character in tag ): errors.append("deployment.mod_host.tag must be a simple version tag") pipewire = deployment.get("pipewire") if not isinstance(pipewire, dict): errors.append("deployment.pipewire must be a mapping") else: expected_fragment_source = "config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf" if pipewire.get("fragment_source") != expected_fragment_source: errors.append("deployment.pipewire.fragment_source must name the managed repository fragment") expected_fragment_destination = ".config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf" if pipewire.get("fragment_destination") != expected_fragment_destination: errors.append("deployment.pipewire.fragment_destination must be the user filter-chain fragment path") if pipewire.get("user_service") != "filter-chain.service": errors.append("deployment.pipewire.user_service must be filter-chain.service") return errors def main(argv=None): parser = argparse.ArgumentParser() parser.add_argument( "state_file", nargs="?", type=Path, default=Path(__file__).resolve().parents[1] / "config" / "desired-state.yaml", ) args = parser.parse_args(argv) try: with args.state_file.open(encoding="utf-8") as state_stream: state = yaml.safe_load(state_stream) except (OSError, yaml.YAMLError) as error: print(f"Unable to load {args.state_file}: {error}", file=sys.stderr) return 2 errors = validate_state(state) if errors: for error in errors: print(f"ERROR: {error}", file=sys.stderr) return 1 print(f"Valid desired state: {args.state_file}") return 0 if __name__ == "__main__": raise SystemExit(main())