validate_desired_state.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. import argparse
  2. import sys
  3. from pathlib import Path, PurePosixPath
  4. import yaml
  5. def safe_relative_path(value):
  6. if not isinstance(value, str) or not value:
  7. return False
  8. path = PurePosixPath(value)
  9. return not path.is_absolute() and ".." not in path.parts
  10. def validate_mod_host_chain(routing, source_by_id):
  11. errors = []
  12. chains = routing.get("mod_host_chains")
  13. if not isinstance(chains, dict):
  14. return ["routing.mod_host_chains must be a mapping"]
  15. if set(chains) != {"guitar"}:
  16. errors.append("routing.mod_host_chains must declare the guitar chain exactly once")
  17. chain = chains.get("guitar")
  18. if not isinstance(chain, dict):
  19. return errors + ["routing.mod_host_chains.guitar must be a mapping"]
  20. guitar = source_by_id.get("guitar")
  21. physical_input = guitar.get("capture_port") if isinstance(guitar, dict) else None
  22. if chain.get("physical_input_port") != physical_input:
  23. errors.append(
  24. "routing.mod_host_chains.guitar.physical_input_port must match the guitar capture port"
  25. )
  26. if chain.get("source_port") != "rigforge.mod-host-input.capture:capture_AUX0":
  27. errors.append(
  28. "routing.mod_host_chains.guitar.source_port must be the mod-ui-visible input boundary"
  29. )
  30. if chain.get("pipewire_input_port") != "rigforge.mod-host-input:playback_AUX0":
  31. errors.append(
  32. "routing.mod_host_chains.guitar.pipewire_input_port must be the Rigforge input sink"
  33. )
  34. if chain.get("pipewire_output_port") != "rigforge.mod-host-output:capture_AUX0":
  35. errors.append(
  36. "routing.mod_host_chains.guitar.pipewire_output_port must be the Rigforge output source"
  37. )
  38. if chain.get("mod_ui_output_port") != "rigforge.mod-host-output.playback:playback_AUX0":
  39. errors.append(
  40. "routing.mod_host_chains.guitar.mod_ui_output_port must be the mod-ui-visible output boundary"
  41. )
  42. bypass = chain.get("bypass_link")
  43. mix_input = chain.get("mix_input")
  44. if not isinstance(bypass, dict):
  45. errors.append("routing.mod_host_chains.guitar.bypass_link must be a mapping")
  46. elif bypass != {"from": physical_input, "to": mix_input}:
  47. errors.append("routing.mod_host_chains.guitar.bypass_link must identify the direct capture-to-mix edge")
  48. if mix_input != "rigforge.direct-mix.input:playback_AUX0":
  49. errors.append("routing.mod_host_chains.guitar.mix_input must be the guitar mixer's AUX0 input")
  50. processors = chain.get("processors")
  51. if not isinstance(processors, list) or len(processors) != 2:
  52. return errors + ["routing.mod_host_chains.guitar.processors must contain NAM followed by one cabinet IR"]
  53. expected_processors = (
  54. {
  55. "id": "amp-head",
  56. "instance": 0,
  57. "plugin_uri": "http://github.com/mikeoliphant/neural-amp-modeler-lv2",
  58. "mod_ui_instance": "/graph/rigforge_nam",
  59. "bundle_path": ".lv2/neural_amp_modeler.lv2",
  60. "file_property": "http://github.com/mikeoliphant/neural-amp-modeler-lv2#model",
  61. "asset_path": "amp_emul/amphead/VOX_AC15/Vox_AC15CH_Crystal_Clean_TB.nam",
  62. "input_port": "effect_0:input",
  63. "output_port": "effect_0:output",
  64. },
  65. {
  66. "id": "cabinet-ir",
  67. "instance": 1,
  68. "plugin_uri": "http://lsp-plug.in/plugins/lv2/impulse_responses_mono",
  69. "mod_ui_instance": "/graph/rigforge_ir",
  70. "bundle_path": ".lv2/lsp-plugins.lv2",
  71. "file_property": "http://lsp-plug.in/plugins/lv2/impulse_responses_mono/ports#ifn",
  72. "asset_path": "amp_emul/cabinet/VOX_AC30_1964/VOX_AC30_1964_celestion.wav",
  73. "input_port": "effect_1:in",
  74. "output_port": "effect_1:out",
  75. },
  76. )
  77. for index, (processor, expected) in enumerate(zip(processors, expected_processors)):
  78. location = f"routing.mod_host_chains.guitar.processors[{index}]"
  79. if not isinstance(processor, dict):
  80. errors.append(f"{location} must be a mapping")
  81. continue
  82. for key, value in expected.items():
  83. if processor.get(key) != value:
  84. errors.append(f"{location}.{key} must be {value}")
  85. for key in ("bundle_path", "asset_path"):
  86. if not safe_relative_path(processor.get(key)):
  87. errors.append(f"{location}.{key} must be a safe relative path")
  88. if index == 0:
  89. expected_models = {
  90. "crystal_clean_tb": "amp_emul/amphead/VOX_AC15/Vox_AC15CH_Crystal_Clean_TB.nam",
  91. "edge_of_breakup_tb": "amp_emul/amphead/VOX_AC15/Vox_AC15CH_Edge_of_Breakup_TB.nam",
  92. "crunch_tb": "amp_emul/amphead/VOX_AC15/Vox_AC15CH_Crunch_TB.nam",
  93. "overdriven_tb": "amp_emul/amphead/VOX_AC15/Vox_AC15CH_Overdriven_TB.nam",
  94. }
  95. models = processor.get("available_models")
  96. if not isinstance(models, dict) or models != expected_models:
  97. errors.append(f"{location}.available_models must declare the four approved Vox TB models")
  98. elif any(not safe_relative_path(path) for path in models.values()):
  99. errors.append(f"{location}.available_models paths must be safe relative paths")
  100. elif processor.get("asset_path") not in models.values():
  101. errors.append(f"{location}.asset_path must be one of the declared available_models")
  102. if isinstance(processors[0], dict):
  103. parameters = processors[0].get("parameters")
  104. if not isinstance(parameters, dict):
  105. errors.append("routing.mod_host_chains.guitar.processors[0].parameters must be a mapping")
  106. else:
  107. output_level = parameters.get("output_level")
  108. if (
  109. not isinstance(output_level, (int, float))
  110. or isinstance(output_level, bool)
  111. or not -20.0 <= output_level <= 20.0
  112. ):
  113. errors.append("NAM output_level must be between -20 and 20 dB")
  114. links = routing.get("managed_links")
  115. if isinstance(links, list) and len(processors) == 2:
  116. actual_edges = {
  117. (link.get("from"), link.get("to"))
  118. for link in links
  119. if (
  120. isinstance(link, dict)
  121. and isinstance(link.get("from"), str)
  122. and isinstance(link.get("to"), str)
  123. )
  124. }
  125. if isinstance(processors[0], dict) and isinstance(processors[1], dict):
  126. expected_candidates = (
  127. (physical_input, chain.get("pipewire_input_port")),
  128. (chain.get("pipewire_output_port"), mix_input),
  129. (chain.get("pipewire_output_port"), "sooperlooper:loop0_in_1"),
  130. )
  131. for edge in expected_candidates:
  132. if not all(isinstance(endpoint, str) for endpoint in edge):
  133. continue
  134. if edge not in actual_edges:
  135. errors.append(f"mod-host guitar boundary link must be declared: {edge[0]} -> {edge[1]}")
  136. internal_edge = (
  137. processors[0].get("output_port"),
  138. processors[1].get("input_port"),
  139. )
  140. if internal_edge in actual_edges:
  141. errors.append(
  142. "the NAM-to-IR link is owned by mod-ui and must not be a Rigforge managed link"
  143. )
  144. if isinstance(bypass, dict):
  145. bypass_edge = (bypass.get("from"), bypass.get("to"))
  146. if all(isinstance(endpoint, str) for endpoint in bypass_edge) and bypass_edge in actual_edges:
  147. errors.append("the guitar capture-to-mix bypass must not be a managed link")
  148. managed_unlinks = routing.get("managed_unlinks")
  149. unlink_edges = {
  150. (item.get("from"), item.get("to"))
  151. for item in managed_unlinks
  152. if isinstance(item, dict)
  153. } if isinstance(managed_unlinks, list) else set()
  154. if (
  155. all(isinstance(endpoint, str) for endpoint in bypass_edge)
  156. and bypass_edge not in unlink_edges
  157. ):
  158. errors.append("the guitar capture-to-mix bypass must be declared in routing.managed_unlinks")
  159. return errors
  160. def validate_mod_host_channel_routes(state, source_by_id):
  161. errors = []
  162. deployment = state.get("deployment")
  163. ports = deployment.get("mod_host_ports") if isinstance(deployment, dict) else None
  164. if not isinstance(ports, dict):
  165. return ["deployment.mod_host_ports must declare four-channel boundaries"]
  166. channel_map = ports.get("channel_map")
  167. if channel_map != ["AUX0", "AUX1", "AUX2", "AUX3"]:
  168. return ["deployment.mod_host_ports.channel_map must contain AUX0 through AUX3"]
  169. required_names = (
  170. "input_sink_name",
  171. "input_source_name",
  172. "output_sink_name",
  173. "output_source_name",
  174. )
  175. if any(not isinstance(ports.get(name), str) or not ports[name] for name in required_names):
  176. return ["deployment.mod_host_ports must declare all four boundary node names"]
  177. routing = state.get("routing")
  178. if not isinstance(routing, dict):
  179. return errors
  180. links = routing.get("managed_links")
  181. if not isinstance(links, list):
  182. return ["routing.managed_links must be a list"]
  183. edges = {
  184. (link.get("from"), link.get("to"))
  185. for link in links
  186. if isinstance(link, dict)
  187. }
  188. for channel, source_id in enumerate(("guitar", "bass", "voice", "synth")):
  189. source = source_by_id.get(source_id)
  190. if not isinstance(source, dict):
  191. continue
  192. position = channel_map[channel]
  193. expected_capture = f"alsa_input.hw_U192k_0:capture_AUX{channel}"
  194. if source.get("physical_input") != channel + 1:
  195. errors.append(
  196. f"{source_id} must use physical input {channel + 1} for mod-ui channel {channel + 1}"
  197. )
  198. if source.get("capture_port") != expected_capture:
  199. errors.append(
  200. f"{source_id} capture_port must be {expected_capture} for mod-ui channel {channel + 1}"
  201. )
  202. expected = (
  203. (
  204. source.get("capture_port"),
  205. f"{ports['input_sink_name']}:playback_{position}",
  206. ),
  207. (
  208. f"{ports['output_source_name']}:capture_{position}",
  209. f"rigforge.direct-mix.input:playback_AUX{channel}",
  210. ),
  211. (
  212. f"{ports['output_source_name']}:capture_{position}",
  213. f"sooperlooper:loop0_in_{channel + 1}",
  214. ),
  215. )
  216. for edge in expected:
  217. if edge not in edges:
  218. errors.append(
  219. f"{source_id} mod-host boundary route must be declared: "
  220. f"{edge[0]} -> {edge[1]}"
  221. )
  222. bypass_edges = {
  223. (source.get("capture_port"), f"rigforge.direct-mix.input:playback_AUX{channel}"),
  224. (source.get("capture_port"), f"sooperlooper:loop0_in_{channel + 1}"),
  225. }
  226. stale = bypass_edges & edges
  227. for source_port, destination_port in sorted(stale):
  228. errors.append(
  229. f"{source_id} capture must not bypass its mod-ui channel: "
  230. f"{source_port} -> {destination_port}"
  231. )
  232. return errors
  233. def validate_state(state):
  234. errors = []
  235. if not isinstance(state, dict):
  236. return ["document root must be a mapping"]
  237. # Establish the declared source map before validating looper references.
  238. sources = state.get("sources")
  239. if not isinstance(sources, list):
  240. return ["sources must be a list"]
  241. source_by_id = {}
  242. physical_inputs = set()
  243. capture_ports = set()
  244. for index, source in enumerate(sources):
  245. location = f"sources[{index}]"
  246. if not isinstance(source, dict):
  247. errors.append(f"{location} must be a mapping")
  248. continue
  249. source_id = source.get("id")
  250. if not isinstance(source_id, str) or not source_id:
  251. errors.append(f"{location}.id must be a non-empty string")
  252. elif source_id in source_by_id:
  253. errors.append(f"duplicate source id: {source_id}")
  254. else:
  255. source_by_id[source_id] = source
  256. physical_input = source.get("physical_input")
  257. if not isinstance(physical_input, int) or isinstance(physical_input, bool):
  258. errors.append(f"{location}.physical_input must be an integer")
  259. elif physical_input in physical_inputs:
  260. errors.append(f"duplicate physical input: {physical_input}")
  261. else:
  262. physical_inputs.add(physical_input)
  263. capture_port = source.get("capture_port")
  264. if not isinstance(capture_port, str) or not capture_port:
  265. errors.append(f"{location}.capture_port must be a non-empty string")
  266. elif capture_port in capture_ports:
  267. errors.append(f"duplicate capture port: {capture_port}")
  268. else:
  269. capture_ports.add(capture_port)
  270. # Keep the first four-channel milestone tied to its four named instruments.
  271. expected_source_ids = {"guitar", "bass", "voice", "synth"}
  272. if set(source_by_id) != expected_source_ids:
  273. errors.append("source ids must be guitar, bass, voice, and synth exactly once")
  274. if len(sources) != 4:
  275. errors.append(f"exactly four sources are required, got {len(sources)}")
  276. if physical_inputs != {1, 2, 3, 4}:
  277. errors.append("physical inputs must cover 1 through 4 exactly once")
  278. # The required looper is one instance containing one four-channel loop.
  279. system = state.get("system")
  280. components = system.get("components") if isinstance(system, dict) else None
  281. if not isinstance(components, list):
  282. errors.append("system.components must be a list")
  283. components = []
  284. loopers = [item for item in components if isinstance(item, dict) and item.get("id") == "looper"]
  285. if len(loopers) != 1:
  286. errors.append(f"exactly one looper component is required, got {len(loopers)}")
  287. elif loopers[0].get("instance_count") != 1:
  288. errors.append("looper.instance_count must be 1")
  289. elif loopers[0].get("loop_count") != 1:
  290. errors.append("looper.loop_count must be 1")
  291. elif loopers[0].get("channels_per_loop") != 4:
  292. errors.append("looper.channels_per_loop must be 4")
  293. routing = state.get("routing")
  294. if not isinstance(routing, dict):
  295. errors.append("routing must be a mapping")
  296. return errors
  297. looper_inputs = routing.get("looper_inputs")
  298. if not isinstance(looper_inputs, list):
  299. errors.append("routing.looper_inputs must be a list")
  300. looper_inputs = []
  301. # Check that each source is assigned exactly once across all loop channels.
  302. input_channels = set()
  303. input_sources = set()
  304. for index, loop_input in enumerate(looper_inputs):
  305. location = f"routing.looper_inputs[{index}]"
  306. if not isinstance(loop_input, dict):
  307. errors.append(f"{location} must be a mapping")
  308. continue
  309. source_id = loop_input.get("source")
  310. channel = loop_input.get("channel")
  311. if source_id not in source_by_id:
  312. errors.append(f"{location}.source references unknown source: {source_id}")
  313. if source_id in input_sources:
  314. errors.append(f"source is assigned to multiple looper inputs: {source_id}")
  315. input_sources.add(source_id)
  316. if not isinstance(channel, int) or isinstance(channel, bool) or channel not in range(1, 5):
  317. errors.append(f"{location}.channel must be an integer from 1 through 4")
  318. elif channel in input_channels:
  319. errors.append(f"duplicate looper input channel: {channel}")
  320. else:
  321. input_channels.add(channel)
  322. if len(looper_inputs) != 4 or input_channels != {1, 2, 3, 4}:
  323. errors.append("looper inputs must cover channels 1 through 4 exactly once")
  324. if input_sources != set(source_by_id):
  325. errors.append("each declared source must map to exactly one looper input")
  326. mix = routing.get("mix")
  327. if not isinstance(mix, dict):
  328. errors.append("routing.mix must be a mapping")
  329. else:
  330. output_port = mix.get("output_port")
  331. if not isinstance(output_port, str) or not output_port:
  332. errors.append("routing.mix.output_port must be a non-empty string")
  333. mix_id = mix.get("id")
  334. if not isinstance(mix_id, str) or not mix_id:
  335. errors.append("routing.mix.id must be a non-empty string")
  336. includes = mix.get("includes")
  337. if not isinstance(includes, list) or "looper_returns" not in includes:
  338. errors.append("routing.mix.includes must include looper_returns")
  339. # Loop returns join the output mix but must never feed the loop inputs.
  340. returns = routing.get("looper_returns")
  341. if not isinstance(returns, dict):
  342. errors.append("routing.looper_returns must be a mapping")
  343. elif returns.get("feed_looper_inputs") is not False:
  344. errors.append("routing.looper_returns.feed_looper_inputs must be false")
  345. errors.extend(validate_mod_host_chain(routing, source_by_id))
  346. errors.extend(validate_mod_host_channel_routes(state, source_by_id))
  347. parameters = state.get("parameters")
  348. if not isinstance(parameters, dict):
  349. errors.append("parameters must be a mapping")
  350. else:
  351. for key in ("sample_rate_hz", "quantum_frames"):
  352. value = parameters.get(key)
  353. if (
  354. not isinstance(value, int)
  355. or isinstance(value, bool)
  356. or value <= 0
  357. ):
  358. errors.append(f"parameters.{key} must be a positive integer")
  359. controls = state.get("controls")
  360. if not isinstance(controls, dict):
  361. errors.append("controls must be a mapping")
  362. else:
  363. midi_bridge = controls.get("midi_bridge")
  364. if not isinstance(midi_bridge, dict):
  365. errors.append("controls.midi_bridge must be a mapping")
  366. else:
  367. if midi_bridge.get("bindings_file") != "config/midi-bindings.yaml":
  368. errors.append("controls.midi_bridge.bindings_file must be config/midi-bindings.yaml")
  369. if (
  370. not isinstance(midi_bridge.get("input_port_name_contains"), str)
  371. or not midi_bridge["input_port_name_contains"].strip()
  372. ):
  373. errors.append("controls.midi_bridge.input_port_name_contains must be non-empty")
  374. mod_ui = midi_bridge.get("mod_ui")
  375. if not isinstance(mod_ui, dict):
  376. errors.append("controls.midi_bridge.mod_ui must be a mapping")
  377. else:
  378. if mod_ui.get("host") != "127.0.0.1":
  379. errors.append("controls.midi_bridge.mod_ui.host must be 127.0.0.1")
  380. port = mod_ui.get("port")
  381. if (
  382. not isinstance(port, int)
  383. or isinstance(port, bool)
  384. or not 1 <= port <= 65535
  385. ):
  386. errors.append("controls.midi_bridge.mod_ui.port must be a valid TCP port")
  387. elif isinstance(deployment := state.get("deployment"), dict):
  388. ui_deployment = deployment.get("mod_ui")
  389. if (
  390. isinstance(ui_deployment, dict)
  391. and ui_deployment.get("websocket_port") != port
  392. ):
  393. errors.append(
  394. "controls.midi_bridge.mod_ui.port must match deployment.mod_ui.websocket_port"
  395. )
  396. websocket_path = mod_ui.get("websocket_path")
  397. if websocket_path != "/websocket":
  398. errors.append("controls.midi_bridge.mod_ui.websocket_path must be /websocket")
  399. connections = state.get("connections", [])
  400. if not isinstance(connections, list):
  401. errors.append("connections must be a list")
  402. connections = []
  403. # An explicitly-forbidden connection documents policy; any other status is actionable.
  404. for index, connection in enumerate(connections):
  405. if not isinstance(connection, dict):
  406. errors.append(f"connections[{index}] must be a mapping")
  407. continue
  408. if (
  409. connection.get("from") == "looper_returns"
  410. and connection.get("to") in {
  411. "looper_inputs",
  412. "looper_input_1",
  413. "looper_input_2",
  414. "looper_input_3",
  415. "looper_input_4",
  416. }
  417. and connection.get("status") != "explicitly-forbidden"
  418. ):
  419. errors.append(f"connections[{index}] routes looper returns into looper inputs")
  420. deployment = state.get("deployment")
  421. if not isinstance(deployment, dict):
  422. errors.append("deployment must be a mapping")
  423. else:
  424. for component, repository, asset, archive_format, bundle_paths in (
  425. (
  426. "lsp",
  427. "lsp-plugins/lsp-plugins",
  428. "lsp-plugins-{tag}-Linux-aarch64.7z",
  429. "7z",
  430. {"lsp-plugins.lv2": "lsp-plugins-{tag}-Linux-aarch64/LV2/lsp-plugins.lv2"},
  431. ),
  432. (
  433. "dragonfly_reverb",
  434. "michaelwillis/dragonfly-reverb",
  435. "dragonfly-reverb-{tag}-linux-arm64.tar.xz",
  436. "tar.xz",
  437. {
  438. "DragonflyEarlyReflections.lv2": "dragonfly-reverb-{tag}/DragonflyEarlyReflections.lv2",
  439. "DragonflyHallReverb.lv2": "dragonfly-reverb-{tag}/DragonflyHallReverb.lv2",
  440. "DragonflyPlateReverb.lv2": "dragonfly-reverb-{tag}/DragonflyPlateReverb.lv2",
  441. "DragonflyRoomReverb.lv2": "dragonfly-reverb-{tag}/DragonflyRoomReverb.lv2",
  442. },
  443. ),
  444. ):
  445. plugin = deployment.get(component)
  446. if not isinstance(plugin, dict):
  447. errors.append(f"deployment.{component} must be a mapping")
  448. continue
  449. tag = plugin.get("tag")
  450. if not isinstance(tag, str) or not tag or any(
  451. character not in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-"
  452. for character in tag
  453. ):
  454. errors.append(f"deployment.{component}.tag must be a simple version tag")
  455. expected_url = f"https://github.com/{repository}/releases/download/{tag}/{asset.format(tag=tag)}"
  456. if plugin.get("archive_url") != expected_url:
  457. errors.append(f"deployment.{component}.archive_url must match its tag and approved release asset")
  458. if plugin.get("archive_format") != archive_format:
  459. errors.append(f"deployment.{component}.archive_format must be {archive_format}")
  460. expected_paths = {
  461. name: path.format(tag=tag) for name, path in bundle_paths.items()
  462. }
  463. if plugin.get("bundle_paths") != expected_paths:
  464. errors.append(f"deployment.{component}.bundle_paths are invalid")
  465. for bundle_name, bundle_path in expected_paths.items():
  466. if bundle_name not in bundle_path:
  467. errors.append(f"deployment.{component}.bundle_paths keys must name their bundle directories")
  468. namlv2 = deployment.get("namlv2")
  469. if not isinstance(namlv2, dict):
  470. errors.append("deployment.namlv2 must be a mapping")
  471. else:
  472. if namlv2.get("source_url") != "https://github.com/mikeoliphant/neural-amp-modeler-lv2":
  473. errors.append(
  474. "deployment.namlv2.source_url must match the approved GitHub repository"
  475. )
  476. tag = namlv2.get("tag")
  477. allowed_tag_characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-"
  478. if not isinstance(tag, str) or not tag or any(
  479. character not in allowed_tag_characters for character in tag
  480. ):
  481. errors.append("deployment.namlv2.tag must be a simple version tag")
  482. archive_url = namlv2.get("archive_url")
  483. expected_url = (
  484. f"https://github.com/mikeoliphant/neural-amp-modeler-lv2/releases/download/{tag}/"
  485. "neural_amp_modeler_lv2_rpi5.tgz"
  486. )
  487. if archive_url != expected_url:
  488. errors.append("deployment.namlv2.archive_url must match its tag and approved RPi 5 release asset")
  489. if namlv2.get("bundle_directory") != "neural_amp_modeler.lv2":
  490. errors.append("deployment.namlv2.bundle_directory must be neural_amp_modeler.lv2")
  491. if namlv2.get("artifact_directory") != "artifacts/nam-lv2/arm64/current":
  492. errors.append(
  493. "deployment.namlv2.artifact_directory must be artifacts/nam-lv2/arm64/current"
  494. )
  495. if namlv2.get("backup_directory") != ".local/state/rigforge/namlv2-backups":
  496. errors.append(
  497. "deployment.namlv2.backup_directory must be "
  498. ".local/state/rigforge/namlv2-backups"
  499. )
  500. sooperlooper = deployment.get("sooperlooper")
  501. if not isinstance(sooperlooper, dict):
  502. errors.append("deployment.sooperlooper must be a mapping")
  503. else:
  504. source_url = sooperlooper.get("source_url")
  505. if source_url != "https://github.com/abarker/sooperlooper-mods":
  506. errors.append("deployment.sooperlooper.source_url must match the approved GitHub repository")
  507. tag = sooperlooper.get("tag")
  508. if not isinstance(tag, str) or not tag or any(
  509. character not in allowed_tag_characters for character in tag
  510. ):
  511. errors.append("deployment.sooperlooper.tag must be a simple version tag")
  512. for key, expected in (
  513. ("binary_source", "artifacts/sooperlooper/arm64/current/sooperlooper"),
  514. (
  515. "service_source",
  516. "config/systemd/user/rigforge-sooperlooper.service.in",
  517. ),
  518. (
  519. "service_destination",
  520. ".config/systemd/user/rigforge-sooperlooper.service",
  521. ),
  522. ("user_service", "rigforge-sooperlooper.service"),
  523. ("binding_file", "config/sooperlooper/fcb1010.slb"),
  524. ("pw_jack_path", "/usr/bin/pw-jack"),
  525. ("oscsend_path", "/usr/bin/oscsend"),
  526. ):
  527. if sooperlooper.get(key) != expected:
  528. errors.append(f"deployment.sooperlooper.{key} must be {expected}")
  529. timeout_seconds = sooperlooper.get("timeout_seconds")
  530. if (
  531. not isinstance(timeout_seconds, int)
  532. or isinstance(timeout_seconds, bool)
  533. or not 1 <= timeout_seconds <= 300
  534. ):
  535. errors.append("deployment.sooperlooper.timeout_seconds must be between 1 and 300")
  536. osc = sooperlooper.get("osc")
  537. if not isinstance(osc, dict):
  538. errors.append("deployment.sooperlooper.osc must be a mapping")
  539. else:
  540. if osc.get("host") != "127.0.0.1":
  541. errors.append("deployment.sooperlooper.osc.host must be 127.0.0.1")
  542. port = osc.get("port")
  543. if (
  544. not isinstance(port, int)
  545. or isinstance(port, bool)
  546. or not 1 <= port <= 65535
  547. ):
  548. errors.append("deployment.sooperlooper.osc.port must be a valid UDP port")
  549. delay = osc.get("post_start_delay_seconds")
  550. if (
  551. not isinstance(delay, int)
  552. or isinstance(delay, bool)
  553. or not 0 <= delay <= 30
  554. ):
  555. errors.append(
  556. "deployment.sooperlooper.osc.post_start_delay_seconds must be between 0 and 30"
  557. )
  558. prefader = osc.get("discrete_prefader")
  559. if (
  560. not isinstance(prefader, int)
  561. or isinstance(prefader, bool)
  562. or prefader not in (0, 1)
  563. ):
  564. errors.append(
  565. "deployment.sooperlooper.osc.discrete_prefader must be 0 or 1"
  566. )
  567. mod_host = deployment.get("mod_host")
  568. if not isinstance(mod_host, dict):
  569. errors.append("deployment.mod_host must be a mapping")
  570. else:
  571. source_url = mod_host.get("source_url")
  572. if source_url != "https://github.com/mod-audio/mod-host":
  573. errors.append("deployment.mod_host.source_url must match the approved GitHub repository")
  574. tag = mod_host.get("tag")
  575. if not isinstance(tag, str) or not tag or any(
  576. character not in allowed_tag_characters for character in tag
  577. ):
  578. errors.append("deployment.mod_host.tag must be a simple version tag")
  579. for key, expected in (
  580. ("binary_source", "artifacts/mod-host/arm64/current/mod-host"),
  581. ("service_source", "config/systemd/user/rigforge-mod-host.service.in"),
  582. ("service_destination", ".config/systemd/user/rigforge-mod-host.service"),
  583. ("user_service", "rigforge-mod-host.service"),
  584. ("readiness_script", "scripts/wait_mod_host_ports.py"),
  585. ("pw_jack_path", "/usr/bin/pw-jack"),
  586. ("python_path", "/usr/bin/python3"),
  587. ):
  588. if mod_host.get(key) != expected:
  589. errors.append(f"deployment.mod_host.{key} must be {expected}")
  590. socket_port = mod_host.get("socket_port")
  591. if (
  592. not isinstance(socket_port, int)
  593. or isinstance(socket_port, bool)
  594. or not 1 <= socket_port <= 65535
  595. ):
  596. errors.append("deployment.mod_host.socket_port must be a valid TCP port")
  597. feedback_port = mod_host.get("feedback_port")
  598. if (
  599. not isinstance(feedback_port, int)
  600. or isinstance(feedback_port, bool)
  601. or not 1 <= feedback_port <= 65535
  602. ):
  603. errors.append("deployment.mod_host.feedback_port must be a valid TCP port")
  604. elif feedback_port == socket_port:
  605. errors.append("deployment.mod_host.feedback_port must differ from socket_port")
  606. port_wait_timeout = mod_host.get("port_wait_timeout_seconds")
  607. if (
  608. not isinstance(port_wait_timeout, int)
  609. or isinstance(port_wait_timeout, bool)
  610. or not 5 <= port_wait_timeout <= 300
  611. ):
  612. errors.append(
  613. "deployment.mod_host.port_wait_timeout_seconds must be between 5 and 300"
  614. )
  615. mod_ui = deployment.get("mod_ui")
  616. if not isinstance(mod_ui, dict):
  617. errors.append("deployment.mod_ui must be a mapping")
  618. else:
  619. if mod_ui.get("source_url") != "https://github.com/mod-audio/mod-ui":
  620. errors.append("deployment.mod_ui.source_url must match the approved GitHub repository")
  621. tag = mod_ui.get("tag")
  622. if not isinstance(tag, str) or not tag or any(
  623. character not in allowed_tag_characters for character in tag
  624. ):
  625. errors.append("deployment.mod_ui.tag must be a simple version tag")
  626. if mod_ui.get("artifact_directory") != "artifacts/mod-ui/arm64/current":
  627. errors.append(
  628. "deployment.mod_ui.artifact_directory must be artifacts/mod-ui/arm64/current"
  629. )
  630. for key, expected in (
  631. ("launcher", "artifacts/mod-ui/arm64/current/run-mod-ui"),
  632. ("runner_script", "scripts/run_mod_ui_service.py"),
  633. ("service_source", "config/systemd/user/rigforge-mod-ui.service.in"),
  634. ("service_destination", ".config/systemd/user/rigforge-mod-ui.service"),
  635. ("user_service", "rigforge-mod-ui.service"),
  636. ("pw_jack_path", "/usr/bin/pw-jack"),
  637. ("python_path", "/usr/bin/python3"),
  638. ("websocket_host", "127.0.0.1"),
  639. ("reference_directory", ".local/share/rigforge/mod-ui/references"),
  640. ("pedalboards_directory", ".local/share/rigforge/mod-ui/pedalboards"),
  641. ("user_files_directory", ".local/share/rigforge/mod-ui/user-files"),
  642. ("last_state_file", ".local/share/rigforge/mod-ui/last-rigforge.json"),
  643. ):
  644. if mod_ui.get(key) != expected:
  645. errors.append(f"deployment.mod_ui.{key} must be {expected}")
  646. expected_input_ports = [
  647. f"rigforge.mod-host-input.capture:capture_AUX{index}"
  648. for index in range(4)
  649. ]
  650. expected_output_ports = [
  651. f"rigforge.mod-host-output.playback:playback_AUX{index}"
  652. for index in range(4)
  653. ]
  654. if mod_ui.get("hardware_input_ports") != expected_input_ports:
  655. errors.append(
  656. "deployment.mod_ui.hardware_input_ports must declare capture_AUX0 through capture_AUX3"
  657. )
  658. if mod_ui.get("hardware_output_ports") != expected_output_ports:
  659. errors.append(
  660. "deployment.mod_ui.hardware_output_ports must declare playback_AUX0 through playback_AUX3"
  661. )
  662. ui_port = mod_ui.get("websocket_port")
  663. if (
  664. not isinstance(ui_port, int)
  665. or isinstance(ui_port, bool)
  666. or not 1 <= ui_port <= 65535
  667. ):
  668. errors.append("deployment.mod_ui.websocket_port must be a valid TCP port")
  669. elif isinstance(mod_host, dict) and (
  670. ui_port == mod_host.get("socket_port")
  671. or ui_port == mod_host.get("feedback_port")
  672. ):
  673. errors.append("deployment.mod_ui.websocket_port must differ from mod-host TCP ports")
  674. mod_host_ports = deployment.get("mod_host_ports")
  675. if not isinstance(mod_host_ports, dict):
  676. errors.append("deployment.mod_host_ports must be a mapping")
  677. else:
  678. for key, expected in (
  679. ("binary_path", "/usr/bin/pw-loopback"),
  680. ("runner_script", "scripts/run_mod_host_ports.py"),
  681. ("service_source", "config/systemd/user/rigforge-mod-host-ports.service.in"),
  682. ("service_destination", ".config/systemd/user/rigforge-mod-host-ports.service"),
  683. ("user_service", "rigforge-mod-host-ports.service"),
  684. ("python_path", "/usr/bin/python3"),
  685. ("input_sink_name", "rigforge.mod-host-input"),
  686. ("input_source_name", "rigforge.mod-host-input.capture"),
  687. ("output_sink_name", "rigforge.mod-host-output.playback"),
  688. ("output_source_name", "rigforge.mod-host-output"),
  689. ):
  690. if mod_host_ports.get(key) != expected:
  691. errors.append(f"deployment.mod_host_ports.{key} must be {expected}")
  692. if mod_host_ports.get("channel_map") != ["AUX0", "AUX1", "AUX2", "AUX3"]:
  693. errors.append(
  694. "deployment.mod_host_ports.channel_map must contain AUX0 through AUX3"
  695. )
  696. reconcile = deployment.get("reconcile")
  697. if not isinstance(reconcile, dict):
  698. errors.append("deployment.reconcile must be a mapping")
  699. else:
  700. for key, expected in (
  701. ("observer_script", "scripts/observe_pipewire.py"),
  702. (
  703. "service_source",
  704. "config/systemd/user/rigforge-pipewire-reconcile.service.in",
  705. ),
  706. (
  707. "service_destination",
  708. ".config/systemd/user/rigforge-pipewire-reconcile.service",
  709. ),
  710. ("user_service", "rigforge-pipewire-reconcile.service"),
  711. (
  712. "timer_source",
  713. "config/systemd/user/rigforge-pipewire-reconcile.timer.in",
  714. ),
  715. (
  716. "timer_destination",
  717. ".config/systemd/user/rigforge-pipewire-reconcile.timer",
  718. ),
  719. ("user_timer", "rigforge-pipewire-reconcile.timer"),
  720. ("python_path", "/usr/bin/python3"),
  721. ):
  722. if reconcile.get(key) != expected:
  723. errors.append(f"deployment.reconcile.{key} must be {expected}")
  724. interval = reconcile.get("interval_seconds")
  725. if (
  726. not isinstance(interval, int)
  727. or isinstance(interval, bool)
  728. or not 5 <= interval <= 300
  729. ):
  730. errors.append(
  731. "deployment.reconcile.interval_seconds must be between 5 and 300"
  732. )
  733. midi_bridge = deployment.get("midi_bridge")
  734. if not isinstance(midi_bridge, dict):
  735. errors.append("deployment.midi_bridge must be a mapping")
  736. else:
  737. for key, expected in (
  738. ("bridge_script", "scripts/midi_action_bridge.py"),
  739. (
  740. "service_source",
  741. "config/systemd/user/rigforge-midi-bridge.service.in",
  742. ),
  743. (
  744. "service_destination",
  745. ".config/systemd/user/rigforge-midi-bridge.service",
  746. ),
  747. ("user_service", "rigforge-midi-bridge.service"),
  748. ("python_path", "/usr/bin/python3"),
  749. ):
  750. if midi_bridge.get(key) != expected:
  751. errors.append(f"deployment.midi_bridge.{key} must be {expected}")
  752. pipewire = deployment.get("pipewire")
  753. if not isinstance(pipewire, dict):
  754. errors.append("deployment.pipewire must be a mapping")
  755. else:
  756. expected_fragment_source = "config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf"
  757. if pipewire.get("fragment_source") != expected_fragment_source:
  758. errors.append("deployment.pipewire.fragment_source must name the managed repository fragment")
  759. expected_fragment_destination = ".config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf"
  760. if pipewire.get("fragment_destination") != expected_fragment_destination:
  761. errors.append("deployment.pipewire.fragment_destination must be the user filter-chain fragment path")
  762. if pipewire.get("user_service") != "filter-chain.service":
  763. errors.append("deployment.pipewire.user_service must be filter-chain.service")
  764. return errors
  765. def main(argv=None):
  766. parser = argparse.ArgumentParser()
  767. parser.add_argument(
  768. "state_file",
  769. nargs="?",
  770. type=Path,
  771. default=Path(__file__).resolve().parents[1] / "config" / "desired-state.yaml",
  772. )
  773. args = parser.parse_args(argv)
  774. try:
  775. with args.state_file.open(encoding="utf-8") as state_stream:
  776. state = yaml.safe_load(state_stream)
  777. except (OSError, yaml.YAMLError) as error:
  778. print(f"Unable to load {args.state_file}: {error}", file=sys.stderr)
  779. return 2
  780. errors = validate_state(state)
  781. if errors:
  782. for error in errors:
  783. print(f"ERROR: {error}", file=sys.stderr)
  784. return 1
  785. print(f"Valid desired state: {args.state_file}")
  786. return 0
  787. if __name__ == "__main__":
  788. raise SystemExit(main())