import argparse import sys from pathlib import Path, PurePosixPath import yaml def safe_relative_path(value): if not isinstance(value, str) or not value: return False path = PurePosixPath(value) return not path.is_absolute() and ".." not in path.parts def validate_mod_host_chain(routing, source_by_id): errors = [] chains = routing.get("mod_host_chains") if not isinstance(chains, dict): return ["routing.mod_host_chains must be a mapping"] if set(chains) != {"guitar"}: errors.append("routing.mod_host_chains must declare the guitar chain exactly once") chain = chains.get("guitar") if not isinstance(chain, dict): return errors + ["routing.mod_host_chains.guitar must be a mapping"] guitar = source_by_id.get("guitar") source_port = guitar.get("capture_port") if isinstance(guitar, dict) else None if chain.get("source_port") != source_port: errors.append("routing.mod_host_chains.guitar.source_port must match the guitar capture port") bypass = chain.get("bypass_link") mix_input = chain.get("mix_input") if not isinstance(bypass, dict): errors.append("routing.mod_host_chains.guitar.bypass_link must be a mapping") elif bypass != {"from": source_port, "to": mix_input}: errors.append("routing.mod_host_chains.guitar.bypass_link must identify the direct capture-to-mix edge") if mix_input != "rigforge.direct-mix.input:playback_AUX0": errors.append("routing.mod_host_chains.guitar.mix_input must be the guitar mixer's AUX0 input") processors = chain.get("processors") if not isinstance(processors, list) or len(processors) != 2: return errors + ["routing.mod_host_chains.guitar.processors must contain NAM followed by one cabinet IR"] expected_processors = ( { "id": "amp-head", "instance": 0, "plugin_uri": "http://github.com/mikeoliphant/neural-amp-modeler-lv2", "bundle_path": ".lv2/neural_amp_modeler.lv2", "file_property": "http://github.com/mikeoliphant/neural-amp-modeler-lv2#model", "asset_path": "amp_emul/amphead/VOX_AC15/Vox_AC15CH_Crunch_TB.nam", "input_port": "effect_0:input", "output_port": "effect_0:output", }, { "id": "cabinet-ir", "instance": 1, "plugin_uri": "http://lsp-plug.in/plugins/lv2/impulse_responses_mono", "bundle_path": ".lv2/lsp-plugins.lv2", "file_property": "http://lsp-plug.in/plugins/lv2/impulse_responses_mono/ports#ifn", "asset_path": "amp_emul/cabinet/VOX_AC30_1964/VOX_AC30_1964_celestion.wav", "input_port": "effect_1:in", "output_port": "effect_1:out", }, ) for index, (processor, expected) in enumerate(zip(processors, expected_processors)): location = f"routing.mod_host_chains.guitar.processors[{index}]" if not isinstance(processor, dict): errors.append(f"{location} must be a mapping") continue for key, value in expected.items(): if processor.get(key) != value: errors.append(f"{location}.{key} must be {value}") for key in ("bundle_path", "asset_path"): if not safe_relative_path(processor.get(key)): errors.append(f"{location}.{key} must be a safe relative path") if isinstance(processors[0], dict): parameters = processors[0].get("parameters") if not isinstance(parameters, dict): errors.append("routing.mod_host_chains.guitar.processors[0].parameters must be a mapping") else: output_level = parameters.get("output_level") if ( not isinstance(output_level, (int, float)) or isinstance(output_level, bool) or not -20.0 <= output_level <= 20.0 ): errors.append("NAM output_level must be between -20 and 20 dB") links = routing.get("managed_links") if isinstance(links, list) and len(processors) == 2: actual_edges = { (link.get("from"), link.get("to")) for link in links if ( isinstance(link, dict) and isinstance(link.get("from"), str) and isinstance(link.get("to"), str) ) } if isinstance(processors[0], dict) and isinstance(processors[1], dict): expected_candidates = ( (source_port, processors[0].get("input_port")), (processors[0].get("output_port"), processors[1].get("input_port")), (processors[1].get("output_port"), mix_input), ) for edge in expected_candidates: if not all(isinstance(endpoint, str) for endpoint in edge): continue if edge not in actual_edges: errors.append(f"mod-host guitar chain link must be declared: {edge[0]} -> {edge[1]}") if isinstance(bypass, dict): bypass_edge = (bypass.get("from"), bypass.get("to")) if all(isinstance(endpoint, str) for endpoint in bypass_edge) and bypass_edge in actual_edges: errors.append("the guitar capture-to-mix bypass must not be a managed link") managed_unlinks = routing.get("managed_unlinks") unlink_edges = { (item.get("from"), item.get("to")) for item in managed_unlinks if isinstance(item, dict) } if isinstance(managed_unlinks, list) else set() if ( all(isinstance(endpoint, str) for endpoint in bypass_edge) and bypass_edge not in unlink_edges ): errors.append("the guitar capture-to-mix bypass must be declared in routing.managed_unlinks") return errors 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") errors.extend(validate_mod_host_chain(routing, source_by_id)) 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") for key, expected in ( ("binary_source", "artifacts/mod-host/arm64/current/mod-host"), ("service_source", "config/systemd/user/rigforge-mod-host.service.in"), ("service_destination", ".config/systemd/user/rigforge-mod-host.service"), ("user_service", "rigforge-mod-host.service"), ("configure_script", "scripts/configure_mod_host.py"), ("pw_jack_path", "/usr/bin/pw-jack"), ("python_path", "/usr/bin/python3"), ): if mod_host.get(key) != expected: errors.append(f"deployment.mod_host.{key} must be {expected}") socket_port = mod_host.get("socket_port") if ( not isinstance(socket_port, int) or isinstance(socket_port, bool) or not 1 <= socket_port <= 65535 ): errors.append("deployment.mod_host.socket_port must be a valid TCP port") 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())