validate_desired_state.py 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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_additional_looper_routes(routing):
  234. errors = []
  235. links = routing.get("managed_links")
  236. if not isinstance(links, list):
  237. return ["routing.managed_links must be a list"]
  238. second_loop_inputs = {
  239. f"sooperlooper:loop1_in_{channel}" for channel in range(1, 5)
  240. }
  241. input_routes = [
  242. link
  243. for link in links
  244. if (
  245. isinstance(link, dict)
  246. and isinstance(link.get("to"), str)
  247. and link["to"] in second_loop_inputs
  248. )
  249. ]
  250. if input_routes:
  251. errors.append("SooperLooper loop1 inputs must remain unconnected")
  252. second_loop_output_links = [
  253. link
  254. for link in links
  255. if isinstance(link, dict)
  256. and isinstance(link.get("from"), str)
  257. and link["from"].startswith("sooperlooper:loop1_out_")
  258. ]
  259. expected_edge = (
  260. "sooperlooper:loop1_out_1",
  261. "rigforge.direct-mix.input:playback_AUX8",
  262. )
  263. declared_edges = [
  264. (link.get("from"), link.get("to"))
  265. for link in second_loop_output_links
  266. ]
  267. if declared_edges != [expected_edge]:
  268. errors.append(
  269. "SooperLooper loop1 must route only output 1 to direct-mix AUX8"
  270. )
  271. return errors
  272. def validate_state(state):
  273. errors = []
  274. if not isinstance(state, dict):
  275. return ["document root must be a mapping"]
  276. # Establish the declared source map before validating looper references.
  277. sources = state.get("sources")
  278. if not isinstance(sources, list):
  279. return ["sources must be a list"]
  280. source_by_id = {}
  281. physical_inputs = set()
  282. capture_ports = set()
  283. for index, source in enumerate(sources):
  284. location = f"sources[{index}]"
  285. if not isinstance(source, dict):
  286. errors.append(f"{location} must be a mapping")
  287. continue
  288. source_id = source.get("id")
  289. if not isinstance(source_id, str) or not source_id:
  290. errors.append(f"{location}.id must be a non-empty string")
  291. elif source_id in source_by_id:
  292. errors.append(f"duplicate source id: {source_id}")
  293. else:
  294. source_by_id[source_id] = source
  295. physical_input = source.get("physical_input")
  296. if not isinstance(physical_input, int) or isinstance(physical_input, bool):
  297. errors.append(f"{location}.physical_input must be an integer")
  298. elif physical_input in physical_inputs:
  299. errors.append(f"duplicate physical input: {physical_input}")
  300. else:
  301. physical_inputs.add(physical_input)
  302. capture_port = source.get("capture_port")
  303. if not isinstance(capture_port, str) or not capture_port:
  304. errors.append(f"{location}.capture_port must be a non-empty string")
  305. elif capture_port in capture_ports:
  306. errors.append(f"duplicate capture port: {capture_port}")
  307. else:
  308. capture_ports.add(capture_port)
  309. # Keep the first four-channel milestone tied to its four named instruments.
  310. expected_source_ids = {"guitar", "bass", "voice", "synth"}
  311. if set(source_by_id) != expected_source_ids:
  312. errors.append("source ids must be guitar, bass, voice, and synth exactly once")
  313. if len(sources) != 4:
  314. errors.append(f"exactly four sources are required, got {len(sources)}")
  315. if physical_inputs != {1, 2, 3, 4}:
  316. errors.append("physical inputs must cover 1 through 4 exactly once")
  317. # One SooperLooper instance hosts two four-channel loops.
  318. system = state.get("system")
  319. components = system.get("components") if isinstance(system, dict) else None
  320. if not isinstance(components, list):
  321. errors.append("system.components must be a list")
  322. components = []
  323. loopers = [item for item in components if isinstance(item, dict) and item.get("id") == "looper"]
  324. if len(loopers) != 1:
  325. errors.append(f"exactly one looper component is required, got {len(loopers)}")
  326. elif loopers[0].get("instance_count") != 1:
  327. errors.append("looper.instance_count must be 1")
  328. elif loopers[0].get("loop_count") != 2:
  329. errors.append("looper.loop_count must be 2")
  330. elif loopers[0].get("channels_per_loop") != 4:
  331. errors.append("looper.channels_per_loop must be 4")
  332. routing = state.get("routing")
  333. if not isinstance(routing, dict):
  334. errors.append("routing must be a mapping")
  335. return errors
  336. looper_inputs = routing.get("looper_inputs")
  337. if not isinstance(looper_inputs, list):
  338. errors.append("routing.looper_inputs must be a list")
  339. looper_inputs = []
  340. # Check that each source is assigned exactly once across all loop channels.
  341. input_channels = set()
  342. input_sources = set()
  343. for index, loop_input in enumerate(looper_inputs):
  344. location = f"routing.looper_inputs[{index}]"
  345. if not isinstance(loop_input, dict):
  346. errors.append(f"{location} must be a mapping")
  347. continue
  348. source_id = loop_input.get("source")
  349. channel = loop_input.get("channel")
  350. if source_id not in source_by_id:
  351. errors.append(f"{location}.source references unknown source: {source_id}")
  352. if source_id in input_sources:
  353. errors.append(f"source is assigned to multiple looper inputs: {source_id}")
  354. input_sources.add(source_id)
  355. if not isinstance(channel, int) or isinstance(channel, bool) or channel not in range(1, 5):
  356. errors.append(f"{location}.channel must be an integer from 1 through 4")
  357. elif channel in input_channels:
  358. errors.append(f"duplicate looper input channel: {channel}")
  359. else:
  360. input_channels.add(channel)
  361. if len(looper_inputs) != 4 or input_channels != {1, 2, 3, 4}:
  362. errors.append("looper inputs must cover channels 1 through 4 exactly once")
  363. if input_sources != set(source_by_id):
  364. errors.append("each declared source must map to exactly one looper input")
  365. mix = routing.get("mix")
  366. if not isinstance(mix, dict):
  367. errors.append("routing.mix must be a mapping")
  368. else:
  369. output_port = mix.get("output_port")
  370. if not isinstance(output_port, str) or not output_port:
  371. errors.append("routing.mix.output_port must be a non-empty string")
  372. mix_id = mix.get("id")
  373. if not isinstance(mix_id, str) or not mix_id:
  374. errors.append("routing.mix.id must be a non-empty string")
  375. includes = mix.get("includes")
  376. if not isinstance(includes, list) or "looper_returns" not in includes:
  377. errors.append("routing.mix.includes must include looper_returns")
  378. # Loop returns join the output mix but must never feed the loop inputs.
  379. returns = routing.get("looper_returns")
  380. if not isinstance(returns, dict):
  381. errors.append("routing.looper_returns must be a mapping")
  382. elif returns.get("feed_looper_inputs") is not False:
  383. errors.append("routing.looper_returns.feed_looper_inputs must be false")
  384. errors.extend(validate_mod_host_chain(routing, source_by_id))
  385. errors.extend(validate_mod_host_channel_routes(state, source_by_id))
  386. errors.extend(validate_additional_looper_routes(routing))
  387. parameters = state.get("parameters")
  388. if not isinstance(parameters, dict):
  389. errors.append("parameters must be a mapping")
  390. else:
  391. for key in ("sample_rate_hz", "quantum_frames"):
  392. value = parameters.get(key)
  393. if (
  394. not isinstance(value, int)
  395. or isinstance(value, bool)
  396. or value <= 0
  397. ):
  398. errors.append(f"parameters.{key} must be a positive integer")
  399. controls = state.get("controls")
  400. if not isinstance(controls, dict):
  401. errors.append("controls must be a mapping")
  402. else:
  403. midi_bridge = controls.get("midi_bridge")
  404. if not isinstance(midi_bridge, dict):
  405. errors.append("controls.midi_bridge must be a mapping")
  406. else:
  407. if midi_bridge.get("bindings_file") != "config/midi-bindings.yaml":
  408. errors.append("controls.midi_bridge.bindings_file must be config/midi-bindings.yaml")
  409. if (
  410. not isinstance(midi_bridge.get("input_port_name_contains"), str)
  411. or not midi_bridge["input_port_name_contains"].strip()
  412. ):
  413. errors.append("controls.midi_bridge.input_port_name_contains must be non-empty")
  414. mod_ui = midi_bridge.get("mod_ui")
  415. if not isinstance(mod_ui, dict):
  416. errors.append("controls.midi_bridge.mod_ui must be a mapping")
  417. else:
  418. if mod_ui.get("host") != "127.0.0.1":
  419. errors.append("controls.midi_bridge.mod_ui.host must be 127.0.0.1")
  420. port = mod_ui.get("port")
  421. if (
  422. not isinstance(port, int)
  423. or isinstance(port, bool)
  424. or not 1 <= port <= 65535
  425. ):
  426. errors.append("controls.midi_bridge.mod_ui.port must be a valid TCP port")
  427. elif isinstance(deployment := state.get("deployment"), dict):
  428. ui_deployment = deployment.get("mod_ui")
  429. if (
  430. isinstance(ui_deployment, dict)
  431. and ui_deployment.get("websocket_port") != port
  432. ):
  433. errors.append(
  434. "controls.midi_bridge.mod_ui.port must match deployment.mod_ui.websocket_port"
  435. )
  436. websocket_path = mod_ui.get("websocket_path")
  437. if websocket_path != "/websocket":
  438. errors.append("controls.midi_bridge.mod_ui.websocket_path must be /websocket")
  439. connections = state.get("connections", [])
  440. if not isinstance(connections, list):
  441. errors.append("connections must be a list")
  442. connections = []
  443. # An explicitly-forbidden connection documents policy; any other status is actionable.
  444. for index, connection in enumerate(connections):
  445. if not isinstance(connection, dict):
  446. errors.append(f"connections[{index}] must be a mapping")
  447. continue
  448. if (
  449. connection.get("from") == "looper_returns"
  450. and connection.get("to") in {
  451. "looper_inputs",
  452. "looper_input_1",
  453. "looper_input_2",
  454. "looper_input_3",
  455. "looper_input_4",
  456. }
  457. and connection.get("status") != "explicitly-forbidden"
  458. ):
  459. errors.append(f"connections[{index}] routes looper returns into looper inputs")
  460. deployment = state.get("deployment")
  461. if not isinstance(deployment, dict):
  462. errors.append("deployment must be a mapping")
  463. else:
  464. for component, repository, asset, archive_format, bundle_paths in (
  465. (
  466. "lsp",
  467. "lsp-plugins/lsp-plugins",
  468. "lsp-plugins-{tag}-Linux-aarch64.7z",
  469. "7z",
  470. {"lsp-plugins.lv2": "lsp-plugins-{tag}-Linux-aarch64/LV2/lsp-plugins.lv2"},
  471. ),
  472. (
  473. "dragonfly_reverb",
  474. "michaelwillis/dragonfly-reverb",
  475. "dragonfly-reverb-{tag}-linux-arm64.tar.xz",
  476. "tar.xz",
  477. {
  478. "DragonflyEarlyReflections.lv2": "dragonfly-reverb-{tag}/DragonflyEarlyReflections.lv2",
  479. "DragonflyHallReverb.lv2": "dragonfly-reverb-{tag}/DragonflyHallReverb.lv2",
  480. "DragonflyPlateReverb.lv2": "dragonfly-reverb-{tag}/DragonflyPlateReverb.lv2",
  481. "DragonflyRoomReverb.lv2": "dragonfly-reverb-{tag}/DragonflyRoomReverb.lv2",
  482. },
  483. ),
  484. ):
  485. plugin = deployment.get(component)
  486. if not isinstance(plugin, dict):
  487. errors.append(f"deployment.{component} must be a mapping")
  488. continue
  489. tag = plugin.get("tag")
  490. if not isinstance(tag, str) or not tag or any(
  491. character not in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-"
  492. for character in tag
  493. ):
  494. errors.append(f"deployment.{component}.tag must be a simple version tag")
  495. expected_url = f"https://github.com/{repository}/releases/download/{tag}/{asset.format(tag=tag)}"
  496. if plugin.get("archive_url") != expected_url:
  497. errors.append(f"deployment.{component}.archive_url must match its tag and approved release asset")
  498. if plugin.get("archive_format") != archive_format:
  499. errors.append(f"deployment.{component}.archive_format must be {archive_format}")
  500. expected_paths = {
  501. name: path.format(tag=tag) for name, path in bundle_paths.items()
  502. }
  503. if plugin.get("bundle_paths") != expected_paths:
  504. errors.append(f"deployment.{component}.bundle_paths are invalid")
  505. for bundle_name, bundle_path in expected_paths.items():
  506. if bundle_name not in bundle_path:
  507. errors.append(f"deployment.{component}.bundle_paths keys must name their bundle directories")
  508. namlv2 = deployment.get("namlv2")
  509. if not isinstance(namlv2, dict):
  510. errors.append("deployment.namlv2 must be a mapping")
  511. else:
  512. if namlv2.get("source_url") != "https://github.com/mikeoliphant/neural-amp-modeler-lv2":
  513. errors.append(
  514. "deployment.namlv2.source_url must match the approved GitHub repository"
  515. )
  516. tag = namlv2.get("tag")
  517. allowed_tag_characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-"
  518. if not isinstance(tag, str) or not tag or any(
  519. character not in allowed_tag_characters for character in tag
  520. ):
  521. errors.append("deployment.namlv2.tag must be a simple version tag")
  522. archive_url = namlv2.get("archive_url")
  523. expected_url = (
  524. f"https://github.com/mikeoliphant/neural-amp-modeler-lv2/releases/download/{tag}/"
  525. "neural_amp_modeler_lv2_rpi5.tgz"
  526. )
  527. if archive_url != expected_url:
  528. errors.append("deployment.namlv2.archive_url must match its tag and approved RPi 5 release asset")
  529. if namlv2.get("bundle_directory") != "neural_amp_modeler.lv2":
  530. errors.append("deployment.namlv2.bundle_directory must be neural_amp_modeler.lv2")
  531. if namlv2.get("artifact_directory") != "artifacts/nam-lv2/arm64/current":
  532. errors.append(
  533. "deployment.namlv2.artifact_directory must be artifacts/nam-lv2/arm64/current"
  534. )
  535. if namlv2.get("backup_directory") != ".local/state/rigforge/namlv2-backups":
  536. errors.append(
  537. "deployment.namlv2.backup_directory must be "
  538. ".local/state/rigforge/namlv2-backups"
  539. )
  540. sooperlooper = deployment.get("sooperlooper")
  541. if not isinstance(sooperlooper, dict):
  542. errors.append("deployment.sooperlooper must be a mapping")
  543. else:
  544. source_url = sooperlooper.get("source_url")
  545. if source_url != "https://github.com/jojolb/sooperlooper-mods":
  546. errors.append("deployment.sooperlooper.source_url must match the approved GitHub repository")
  547. tag = sooperlooper.get("tag")
  548. if not isinstance(tag, str) or not tag or any(
  549. character not in allowed_tag_characters for character in tag
  550. ):
  551. errors.append("deployment.sooperlooper.tag must be a simple version tag")
  552. for key, expected in (
  553. ("binary_source", "artifacts/sooperlooper/arm64/current/sooperlooper"),
  554. (
  555. "service_source",
  556. "config/systemd/user/rigforge-sooperlooper.service.in",
  557. ),
  558. (
  559. "service_destination",
  560. ".config/systemd/user/rigforge-sooperlooper.service",
  561. ),
  562. ("user_service", "rigforge-sooperlooper.service"),
  563. ("binding_file", "config/sooperlooper/fcb1010.slb"),
  564. ("pw_jack_path", "/usr/bin/pw-jack"),
  565. ("oscsend_path", "/usr/bin/oscsend"),
  566. ):
  567. if sooperlooper.get(key) != expected:
  568. errors.append(f"deployment.sooperlooper.{key} must be {expected}")
  569. timeout_seconds = sooperlooper.get("timeout_seconds")
  570. if (
  571. not isinstance(timeout_seconds, int)
  572. or isinstance(timeout_seconds, bool)
  573. or not 1 <= timeout_seconds <= 300
  574. ):
  575. errors.append("deployment.sooperlooper.timeout_seconds must be between 1 and 300")
  576. osc = sooperlooper.get("osc")
  577. if not isinstance(osc, dict):
  578. errors.append("deployment.sooperlooper.osc must be a mapping")
  579. else:
  580. if osc.get("host") != "127.0.0.1":
  581. errors.append("deployment.sooperlooper.osc.host must be 127.0.0.1")
  582. port = osc.get("port")
  583. if (
  584. not isinstance(port, int)
  585. or isinstance(port, bool)
  586. or not 1 <= port <= 65535
  587. ):
  588. errors.append("deployment.sooperlooper.osc.port must be a valid UDP port")
  589. delay = osc.get("post_start_delay_seconds")
  590. if (
  591. not isinstance(delay, int)
  592. or isinstance(delay, bool)
  593. or not 0 <= delay <= 30
  594. ):
  595. errors.append(
  596. "deployment.sooperlooper.osc.post_start_delay_seconds must be between 0 and 30"
  597. )
  598. prefader = osc.get("discrete_prefader")
  599. if (
  600. not isinstance(prefader, int)
  601. or isinstance(prefader, bool)
  602. or prefader not in (0, 1)
  603. ):
  604. errors.append(
  605. "deployment.sooperlooper.osc.discrete_prefader must be 0 or 1"
  606. )
  607. slweb = deployment.get("slweb")
  608. if not isinstance(slweb, dict):
  609. errors.append("deployment.slweb must be a mapping")
  610. else:
  611. for key, expected in (
  612. ("source_directory", "tools"),
  613. ("install_directory", ".local/share/rigforge/sooperlooper-slweb"),
  614. ("service_source", "config/systemd/user/rigforge-slweb.service.in"),
  615. ("service_destination", ".config/systemd/user/rigforge-slweb.service"),
  616. ("user_service", "rigforge-slweb.service"),
  617. ("python_path", "/usr/bin/python3"),
  618. ("http_port", 8080),
  619. ("files_directory", "/home/jojo/loops"),
  620. ):
  621. if slweb.get(key) != expected:
  622. errors.append(f"deployment.slweb.{key} must be {expected}")
  623. if slweb.get("http_host") not in ("127.0.0.1", "0.0.0.0"):
  624. errors.append(
  625. "deployment.slweb.http_host must be 127.0.0.1 or 0.0.0.0"
  626. )
  627. mod_host = deployment.get("mod_host")
  628. if not isinstance(mod_host, dict):
  629. errors.append("deployment.mod_host must be a mapping")
  630. else:
  631. source_url = mod_host.get("source_url")
  632. if source_url != "https://github.com/mod-audio/mod-host":
  633. errors.append("deployment.mod_host.source_url must match the approved GitHub repository")
  634. tag = mod_host.get("tag")
  635. if not isinstance(tag, str) or not tag or any(
  636. character not in allowed_tag_characters for character in tag
  637. ):
  638. errors.append("deployment.mod_host.tag must be a simple version tag")
  639. for key, expected in (
  640. ("binary_source", "artifacts/mod-host/arm64/current/mod-host"),
  641. ("service_source", "config/systemd/user/rigforge-mod-host.service.in"),
  642. ("service_destination", ".config/systemd/user/rigforge-mod-host.service"),
  643. ("user_service", "rigforge-mod-host.service"),
  644. ("readiness_script", "scripts/wait_mod_host_ports.py"),
  645. ("pw_jack_path", "/usr/bin/pw-jack"),
  646. ("python_path", "/usr/bin/python3"),
  647. ):
  648. if mod_host.get(key) != expected:
  649. errors.append(f"deployment.mod_host.{key} must be {expected}")
  650. socket_port = mod_host.get("socket_port")
  651. if (
  652. not isinstance(socket_port, int)
  653. or isinstance(socket_port, bool)
  654. or not 1 <= socket_port <= 65535
  655. ):
  656. errors.append("deployment.mod_host.socket_port must be a valid TCP port")
  657. feedback_port = mod_host.get("feedback_port")
  658. if (
  659. not isinstance(feedback_port, int)
  660. or isinstance(feedback_port, bool)
  661. or not 1 <= feedback_port <= 65535
  662. ):
  663. errors.append("deployment.mod_host.feedback_port must be a valid TCP port")
  664. elif feedback_port == socket_port:
  665. errors.append("deployment.mod_host.feedback_port must differ from socket_port")
  666. port_wait_timeout = mod_host.get("port_wait_timeout_seconds")
  667. if (
  668. not isinstance(port_wait_timeout, int)
  669. or isinstance(port_wait_timeout, bool)
  670. or not 5 <= port_wait_timeout <= 300
  671. ):
  672. errors.append(
  673. "deployment.mod_host.port_wait_timeout_seconds must be between 5 and 300"
  674. )
  675. mod_ui = deployment.get("mod_ui")
  676. if not isinstance(mod_ui, dict):
  677. errors.append("deployment.mod_ui must be a mapping")
  678. else:
  679. if mod_ui.get("source_url") != "https://github.com/mod-audio/mod-ui":
  680. errors.append("deployment.mod_ui.source_url must match the approved GitHub repository")
  681. tag = mod_ui.get("tag")
  682. if not isinstance(tag, str) or not tag or any(
  683. character not in allowed_tag_characters for character in tag
  684. ):
  685. errors.append("deployment.mod_ui.tag must be a simple version tag")
  686. if mod_ui.get("artifact_directory") != "artifacts/mod-ui/arm64/current":
  687. errors.append(
  688. "deployment.mod_ui.artifact_directory must be artifacts/mod-ui/arm64/current"
  689. )
  690. for key, expected in (
  691. ("launcher", "artifacts/mod-ui/arm64/current/run-mod-ui"),
  692. ("runner_script", "scripts/run_mod_ui_service.py"),
  693. ("service_source", "config/systemd/user/rigforge-mod-ui.service.in"),
  694. ("service_destination", ".config/systemd/user/rigforge-mod-ui.service"),
  695. ("user_service", "rigforge-mod-ui.service"),
  696. ("pw_jack_path", "/usr/bin/pw-jack"),
  697. ("python_path", "/usr/bin/python3"),
  698. ("websocket_host", "127.0.0.1"),
  699. ("reference_directory", ".local/share/rigforge/mod-ui/references"),
  700. ("pedalboards_directory", ".local/share/rigforge/mod-ui/pedalboards"),
  701. ("user_files_directory", ".local/share/rigforge/mod-ui/user-files"),
  702. ("last_state_file", ".local/share/rigforge/mod-ui/last-rigforge.json"),
  703. ):
  704. if mod_ui.get(key) != expected:
  705. errors.append(f"deployment.mod_ui.{key} must be {expected}")
  706. expected_input_ports = [
  707. f"rigforge.mod-host-input.capture:capture_AUX{index}"
  708. for index in range(4)
  709. ]
  710. expected_output_ports = [
  711. f"rigforge.mod-host-output.playback:playback_AUX{index}"
  712. for index in range(4)
  713. ]
  714. if mod_ui.get("hardware_input_ports") != expected_input_ports:
  715. errors.append(
  716. "deployment.mod_ui.hardware_input_ports must declare capture_AUX0 through capture_AUX3"
  717. )
  718. if mod_ui.get("hardware_output_ports") != expected_output_ports:
  719. errors.append(
  720. "deployment.mod_ui.hardware_output_ports must declare playback_AUX0 through playback_AUX3"
  721. )
  722. ui_port = mod_ui.get("websocket_port")
  723. if (
  724. not isinstance(ui_port, int)
  725. or isinstance(ui_port, bool)
  726. or not 1 <= ui_port <= 65535
  727. ):
  728. errors.append("deployment.mod_ui.websocket_port must be a valid TCP port")
  729. elif isinstance(mod_host, dict) and (
  730. ui_port == mod_host.get("socket_port")
  731. or ui_port == mod_host.get("feedback_port")
  732. ):
  733. errors.append("deployment.mod_ui.websocket_port must differ from mod-host TCP ports")
  734. mod_host_ports = deployment.get("mod_host_ports")
  735. if not isinstance(mod_host_ports, dict):
  736. errors.append("deployment.mod_host_ports must be a mapping")
  737. else:
  738. for key, expected in (
  739. ("binary_path", "/usr/bin/pw-loopback"),
  740. ("runner_script", "scripts/run_mod_host_ports.py"),
  741. ("service_source", "config/systemd/user/rigforge-mod-host-ports.service.in"),
  742. ("service_destination", ".config/systemd/user/rigforge-mod-host-ports.service"),
  743. ("user_service", "rigforge-mod-host-ports.service"),
  744. ("python_path", "/usr/bin/python3"),
  745. ("input_sink_name", "rigforge.mod-host-input"),
  746. ("input_source_name", "rigforge.mod-host-input.capture"),
  747. ("output_sink_name", "rigforge.mod-host-output.playback"),
  748. ("output_source_name", "rigforge.mod-host-output"),
  749. ):
  750. if mod_host_ports.get(key) != expected:
  751. errors.append(f"deployment.mod_host_ports.{key} must be {expected}")
  752. if mod_host_ports.get("channel_map") != ["AUX0", "AUX1", "AUX2", "AUX3"]:
  753. errors.append(
  754. "deployment.mod_host_ports.channel_map must contain AUX0 through AUX3"
  755. )
  756. reconcile = deployment.get("reconcile")
  757. if not isinstance(reconcile, dict):
  758. errors.append("deployment.reconcile must be a mapping")
  759. else:
  760. for key, expected in (
  761. ("observer_script", "scripts/observe_pipewire.py"),
  762. (
  763. "service_source",
  764. "config/systemd/user/rigforge-pipewire-reconcile.service.in",
  765. ),
  766. (
  767. "service_destination",
  768. ".config/systemd/user/rigforge-pipewire-reconcile.service",
  769. ),
  770. ("user_service", "rigforge-pipewire-reconcile.service"),
  771. (
  772. "timer_source",
  773. "config/systemd/user/rigforge-pipewire-reconcile.timer.in",
  774. ),
  775. (
  776. "timer_destination",
  777. ".config/systemd/user/rigforge-pipewire-reconcile.timer",
  778. ),
  779. ("user_timer", "rigforge-pipewire-reconcile.timer"),
  780. ("python_path", "/usr/bin/python3"),
  781. ):
  782. if reconcile.get(key) != expected:
  783. errors.append(f"deployment.reconcile.{key} must be {expected}")
  784. interval = reconcile.get("interval_seconds")
  785. if (
  786. not isinstance(interval, int)
  787. or isinstance(interval, bool)
  788. or not 5 <= interval <= 300
  789. ):
  790. errors.append(
  791. "deployment.reconcile.interval_seconds must be between 5 and 300"
  792. )
  793. midi_bridge = deployment.get("midi_bridge")
  794. if not isinstance(midi_bridge, dict):
  795. errors.append("deployment.midi_bridge must be a mapping")
  796. else:
  797. for key, expected in (
  798. ("bridge_script", "scripts/midi_action_bridge.py"),
  799. (
  800. "service_source",
  801. "config/systemd/user/rigforge-midi-bridge.service.in",
  802. ),
  803. (
  804. "service_destination",
  805. ".config/systemd/user/rigforge-midi-bridge.service",
  806. ),
  807. ("user_service", "rigforge-midi-bridge.service"),
  808. ("python_path", "/usr/bin/python3"),
  809. ):
  810. if midi_bridge.get(key) != expected:
  811. errors.append(f"deployment.midi_bridge.{key} must be {expected}")
  812. pipewire = deployment.get("pipewire")
  813. if not isinstance(pipewire, dict):
  814. errors.append("deployment.pipewire must be a mapping")
  815. else:
  816. expected_fragment_source = "config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf"
  817. if pipewire.get("fragment_source") != expected_fragment_source:
  818. errors.append("deployment.pipewire.fragment_source must name the managed repository fragment")
  819. expected_fragment_destination = ".config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf"
  820. if pipewire.get("fragment_destination") != expected_fragment_destination:
  821. errors.append("deployment.pipewire.fragment_destination must be the user filter-chain fragment path")
  822. if pipewire.get("user_service") != "filter-chain.service":
  823. errors.append("deployment.pipewire.user_service must be filter-chain.service")
  824. return errors
  825. def main(argv=None):
  826. parser = argparse.ArgumentParser()
  827. parser.add_argument(
  828. "state_file",
  829. nargs="?",
  830. type=Path,
  831. default=Path(__file__).resolve().parents[1] / "config" / "desired-state.yaml",
  832. )
  833. args = parser.parse_args(argv)
  834. try:
  835. with args.state_file.open(encoding="utf-8") as state_stream:
  836. state = yaml.safe_load(state_stream)
  837. except (OSError, yaml.YAMLError) as error:
  838. print(f"Unable to load {args.state_file}: {error}", file=sys.stderr)
  839. return 2
  840. errors = validate_state(state)
  841. if errors:
  842. for error in errors:
  843. print(f"ERROR: {error}", file=sys.stderr)
  844. return 1
  845. print(f"Valid desired state: {args.state_file}")
  846. return 0
  847. if __name__ == "__main__":
  848. raise SystemExit(main())