nooise 2.5.0

Ambient music generator for the terminal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
//! Ordered interaction-effect execution: the adapter-side consequences of a
//! kernel transition.
//!
//! Effects run in order and stop at the first failure. Control and automation
//! mutations call *down* into `edit.rs`; this module must never call into
//! `ui.rs`.

use std::error::Error;
use std::fmt;

use super::interaction::{InteractionEffect, Page, PaletteStagedEdit};
use super::song::SongCodeError;
use super::*;
use rand::{Rng, SeedableRng, rngs::StdRng};

/// Why one effect in a transition's ordered list did not run.
///
/// Execution stops at the first failure, so a failure is also the reason the
/// effects behind it never ran.
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum EffectFailure {
    /// The effect named a control id no registry spec claims.
    UnknownControl(&'static str),
    /// The song state could not be encoded, so there was nothing to copy.
    SongEncode(SongCodeError),
    /// The clipboard refused the write.
    Clipboard(ClipboardError),
    /// The effect needs a piece of live context the frame did not carry.
    MissingContext(&'static str),
    /// The selected control already carries the maximum lanes of this family.
    AutomationLaneLimit,
    /// The kernel emitted an effect this executor does not implement. New
    /// effects are rejected explicitly rather than silently dropped.
    UnsupportedInteraction(InteractionEffect),
}

impl fmt::Display for EffectFailure {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnknownControl(id) => write!(f, "no control named {id}"),
            Self::SongEncode(error) => write!(f, "{error}"),
            Self::Clipboard(error) => write!(f, "{error}"),
            Self::MissingContext(what) => write!(f, "no {what} in this frame"),
            Self::AutomationLaneLimit => {
                write!(f, "this slider already has four lanes of that kind")
            }
            Self::UnsupportedInteraction(effect) => {
                write!(f, "unsupported interaction effect {effect:?}")
            }
        }
    }
}

impl Error for EffectFailure {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::SongEncode(error) => Some(error),
            Self::Clipboard(error) => Some(error),
            _ => None,
        }
    }
}

/// Why a clipboard write did not land. The detail string is the system
/// backend's own message; the variant names which step it came from.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum ClipboardError {
    /// No system clipboard could be opened at all.
    Unavailable(String),
    /// The clipboard opened but rejected the text.
    WriteRejected(String),
}

impl fmt::Display for ClipboardError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Unavailable(detail) => write!(f, "clipboard unavailable: {detail}"),
            Self::WriteRejected(detail) => write!(f, "clipboard write rejected: {detail}"),
        }
    }
}

impl Error for ClipboardError {}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum EffectAcknowledgement {
    Published {
        generation: u64,
    },
    Staged {
        count: usize,
    },
    Message(String),
    ControlSelected {
        tab: Tab,
        index: usize,
        id: &'static str,
    },
    PerformanceEdited {
        tab: Tab,
        index: usize,
        id: &'static str,
        generation: u64,
    },
    PageSelected(Page),
    QuitRequested,
    NoChange,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum ControlEdit {
    Delta(f32),
    Value(f32),
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum LiveEffect {
    EditControl {
        id: &'static str,
        edit: ControlEdit,
    },
    SelectControl {
        tab: Tab,
        index: usize,
        id: &'static str,
    },
    StageForBar {
        target_beat: f64,
        edits: Vec<StagedEdit>,
    },
    CommitPending {
        beat: f64,
    },
    CopySong,
    ShowMessage(String),
}

#[derive(Default)]
pub(crate) struct InteractionExecutionContext {
    pub(crate) selected_control: Option<&'static str>,
    pub(crate) beat: f64,
}

/// The control an effect targets, or the failure every selection-scoped
/// effect reports when nothing is selected.
fn selected_control(selected_control: Option<&'static str>) -> Result<&'static str, EffectFailure> {
    selected_control.ok_or(EffectFailure::MissingContext("selected control"))
}

/// Palette edits cross the kernel boundary as value bits; this is where they
/// become session edits.
fn staged_edits(edits: Vec<PaletteStagedEdit>) -> Vec<StagedEdit> {
    edits
        .into_iter()
        .map(|edit| StagedEdit {
            id: edit.id,
            value: f32::from_bits(edit.value_bits),
        })
        .collect()
}

pub(crate) struct ProductionInteractionContext<'a> {
    pub(crate) selected_control: Option<&'static str>,
    pub(crate) visible_control_ids: &'a [&'static str],
    pub(crate) randomizes_automation: bool,
    pub(crate) tab: Tab,
    pub(crate) selected: usize,
    pub(crate) automation_selected: usize,
    pub(crate) beat: f64,
    pub(crate) flipped: &'a mut FlippedUnits,
}

/// The one seam that reaches the system clipboard, so replay can substitute a
/// deterministic one.
pub(crate) trait Clipboard {
    fn set_text(&mut self, text: String) -> Result<(), ClipboardError>;
}

pub(crate) struct SystemClipboard;

impl Clipboard for SystemClipboard {
    fn set_text(&mut self, text: String) -> Result<(), ClipboardError> {
        let mut clipboard = arboard::Clipboard::new()
            .map_err(|error| ClipboardError::Unavailable(error.to_string()))?;
        clipboard
            .set_text(text)
            .map_err(|error| ClipboardError::WriteRejected(error.to_string()))
    }
}

/// Stateful boundary for executing ordered interaction effects. All
/// user-audible edits delegate to one `LiveSession` transaction; UI-only
/// consequences are acknowledged only after publication succeeds.
pub(crate) struct EffectExecutor {
    session: LiveSession,
    auto: AutoControls,
    recent: RecentControls,
    pending: Option<(f64, Vec<StagedEdit>)>,
    message: Option<(String, Instant)>,
    /// The one source of randomness an effect may draw on (`r` rolls a
    /// control). Seeded from entropy in production and from a fixed seed in
    /// replay, so a replayed trace rolls the same values twice.
    rng: StdRng,
    /// What play mode has played lately, for `LeadCapture`. Live-only.
    phrase: LeadPhraseBuffer,
}

impl EffectExecutor {
    pub(crate) fn new(session: LiveSession, auto: AutoControls) -> Self {
        Self::seeded(session, auto, rand::random())
    }

    pub(crate) fn seeded(session: LiveSession, auto: AutoControls, seed: u64) -> Self {
        Self {
            session,
            auto,
            recent: RecentControls::default(),
            pending: None,
            message: None,
            rng: StdRng::seed_from_u64(seed),
            phrase: LeadPhraseBuffer::default(),
        }
    }

    pub(crate) fn session(&self) -> &LiveSession {
        &self.session
    }

    pub(crate) fn auto_position(&self, beat: f64) -> Option<MorphPosition> {
        self.auto.position_at(beat)
    }

    pub(crate) fn toggle_auto(&mut self, beat: f64) {
        if self.auto.is_running() {
            self.auto.exit();
            self.session.update(|_| {});
        } else {
            let current = self.session.load();
            self.auto
                .toggle(current.controls.clone(), current.automation.clone(), beat);
            self.session.update(|_| {});
        }
    }

    pub(crate) fn recent(&self) -> &RecentControls {
        &self.recent
    }

    pub(crate) fn pending(&self) -> Option<&(f64, Vec<StagedEdit>)> {
        self.pending.as_ref()
    }

    pub(crate) fn message(&self) -> Option<&str> {
        self.message.as_ref().map(|(message, _)| message.as_str())
    }

    pub(crate) fn expire_message(&mut self, ttl: std::time::Duration) {
        if self
            .message
            .as_ref()
            .is_some_and(|(_, shown_at)| shown_at.elapsed() >= ttl)
        {
            self.message = None;
        }
    }

    pub(crate) fn execute(
        &mut self,
        effect: LiveEffect,
    ) -> Result<EffectAcknowledgement, EffectFailure> {
        let mut clipboard = SystemClipboard;
        self.execute_with_clipboard(effect, &mut clipboard)
    }

    pub(crate) fn execute_with_clipboard(
        &mut self,
        effect: LiveEffect,
        clipboard: &mut dyn Clipboard,
    ) -> Result<EffectAcknowledgement, EffectFailure> {
        match effect {
            LiveEffect::EditControl { id, edit } => {
                let spec = spec_by_id(id).ok_or(EffectFailure::UnknownControl(id))?;
                let snapshot = self.edit_session(Some(id), |snapshot| match edit {
                    ControlEdit::Delta(delta) => spec.apply_delta(delta, &mut snapshot.controls),
                    ControlEdit::Value(value) => spec.apply_value(value, &mut snapshot.controls),
                });
                Ok(EffectAcknowledgement::Published {
                    generation: snapshot.generation,
                })
            }
            LiveEffect::SelectControl { tab, index, id } => {
                if spec_by_id(id).is_none() {
                    return Err(EffectFailure::UnknownControl(id));
                }
                self.recent.touch(id);
                Ok(EffectAcknowledgement::ControlSelected { tab, index, id })
            }
            LiveEffect::StageForBar { target_beat, edits } => {
                if let Some(unknown) = edits.iter().find(|edit| spec_by_id(edit.id).is_none()) {
                    return Err(EffectFailure::UnknownControl(unknown.id));
                }
                let count = edits.len();
                self.pending = Some((target_beat, edits));
                Ok(EffectAcknowledgement::Staged { count })
            }
            LiveEffect::CommitPending { beat } => {
                let Some((target, edits)) = self.pending.as_ref() else {
                    return Ok(EffectAcknowledgement::NoChange);
                };
                if beat < *target {
                    return Ok(EffectAcknowledgement::NoChange);
                }
                let edits = edits.clone();
                if let Some(unknown) = edits.iter().find(|edit| spec_by_id(edit.id).is_none()) {
                    return Err(EffectFailure::UnknownControl(unknown.id));
                }
                self.auto.exit();
                let snapshot = self.session.update(|snapshot| {
                    for edit in &edits {
                        spec_by_id(edit.id)
                            .expect("validated staged control")
                            .apply_value(edit.value, &mut snapshot.controls);
                    }
                });
                self.pending = None;
                self.recent.touch_edits(&edits);
                let plural = if edits.len() == 1 { "" } else { "s" };
                let message = format!("{} edit{plural} applied", edits.len());
                self.message = Some((message, Instant::now()));
                Ok(EffectAcknowledgement::Published {
                    generation: snapshot.generation,
                })
            }
            LiveEffect::CopySong => {
                let snapshot = self.session.load();
                let now_seconds = self.session.audio_seconds();
                let code = encode_song_code(&SongState {
                    controls: snapshot.controls.clone(),
                    automation: snapshot.automation.clone(),
                    tonal_sequence: Some(snapshot.tonal_sequence.clone()),
                    muted: snapshot.muted,
                    gestures: snapshot.gestures.snapshot_at(now_seconds),
                })
                .map_err(EffectFailure::SongEncode)?;
                clipboard.set_text(code).map_err(EffectFailure::Clipboard)?;
                let message = "song code copied to clipboard".to_string();
                self.message = Some((message.clone(), Instant::now()));
                Ok(EffectAcknowledgement::Message(message))
            }
            LiveEffect::ShowMessage(message) => {
                self.message = Some((message.clone(), Instant::now()));
                Ok(EffectAcknowledgement::Message(message))
            }
        }
    }

    /// Ordered generic bridge. Production drives effects through
    /// `execute_interaction_with_clipboard`; this shape exists for the
    /// adapter tests that assert stop-at-first-failure ordering.
    #[cfg(test)]
    pub(crate) fn execute_ordered(
        &mut self,
        effects: impl IntoIterator<Item = LiveEffect>,
    ) -> Vec<Result<EffectAcknowledgement, EffectFailure>> {
        run_ordered(effects, |effect| self.execute(effect))
    }

    /// Apply an edit to the live session.
    ///
    pub(crate) fn edit_session(
        &mut self,
        recent_id: Option<&'static str>,
        mut edit: impl FnMut(&mut LiveSessionSnapshot),
    ) -> Arc<LiveSessionSnapshot> {
        self.auto.exit();
        let snapshot = self.session.update(|snapshot| edit(snapshot));
        if let Some(id) = recent_id {
            self.recent.touch(id);
        }
        snapshot
    }

    /// Acknowledge the generation the session currently holds. Every effect
    /// that publishes reports the generation its own edit produced, so this
    /// is only ever read after the edit.
    fn published(&self) -> EffectAcknowledgement {
        EffectAcknowledgement::Published {
            generation: self.session.load().generation,
        }
    }

    /// Run an automation edit against a detached copy of the automation the
    /// frame started from, then acknowledge what it published. The copy is
    /// what lets the edit read the route it is about to replace while the
    /// executor holds the session mutably.
    fn with_automation(
        &mut self,
        edit: impl FnOnce(&mut Self, &AutomationState),
    ) -> Result<EffectAcknowledgement, EffectFailure> {
        let automation = self.session.load().automation.clone();
        edit(self, &automation);
        Ok(self.published())
    }

    pub(crate) fn edit_navigation_automation(
        &mut self,
        mut edit: impl FnMut(&mut AutomationState),
    ) {
        self.session
            .update(|snapshot| edit(&mut snapshot.automation));
    }

    /// Resolve a palette module row. The chain already holding this module
    /// means the user wants the one that is there, not a second copy, so this
    /// jumps instead of adding. A full chain fails loudly with the count
    /// rather than silently doing nothing.
    fn place_module(
        &mut self,
        tab: Tab,
        catalog_index: usize,
    ) -> Result<EffectAcknowledgement, EffectFailure> {
        let kind = MODULE_CATALOG
            .get(catalog_index)
            .ok_or(EffectFailure::MissingContext("catalog module"))?;
        if !module_available_on(*kind, tab) {
            return Err(EffectFailure::MissingContext(
                "module is unavailable on this layer",
            ));
        }
        let existing = self
            .session
            .load()
            .controls
            .modules
            .for_tab(tab)
            .and_then(|slots| chain_amount_slot(slots, kind.id));
        let slot = match existing {
            Some(slot) => slot,
            None => {
                let placed = self.edit_session(None, |snapshot| {
                    if let Some(slots) = snapshot.controls.modules.for_tab_mut(tab)
                        && let Some(free) = slots.iter().position(ModuleSlot::is_empty)
                    {
                        // Added modules always start inert, whatever a
                        // pre-loaded slot's factory amount happens to be, so
                        // adding one is audibly free and needs no confirming.
                        slots[free] = preset_slot(kind.id, 0.0);
                    }
                });
                let Some(slot) = placed
                    .controls
                    .modules
                    .for_tab(tab)
                    .and_then(|slots| chain_amount_slot(slots, kind.id))
                else {
                    return Ok(EffectAcknowledgement::Message(format!(
                        "{} chain full ({}/{})",
                        tab.name(),
                        MODULE_SLOTS,
                        MODULE_SLOTS
                    )));
                };
                slot
            }
        };
        let id = module_slot_collapsed_id(tab, slot, &self.session.load().controls)
            .ok_or(EffectFailure::MissingContext("module slot control"))?;
        let index = spec_index(tab, id).ok_or(EffectFailure::UnknownControl(id))?;
        self.recent.touch(id);
        self.execute(LiveEffect::SelectControl { tab, index, id })
    }

    /// Mute is a session overlay, so automation and auto morph keep driving
    /// the authored level while the engine gates the final layer output.
    pub(crate) fn toggle_mute(&mut self, tab: Tab) {
        let Some(id) = tab.level_id() else { return };
        self.recent.touch(id);
        self.session
            .update(|snapshot| snapshot.muted[tab as usize] = !snapshot.muted[tab as usize]);
    }

    /// Typed bridge from the pure interaction kernel to effect execution.
    /// Effects needing adapter-owned data must receive it explicitly through
    /// `context`; unsupported staged performance effects fail visibly.
    #[cfg(test)]
    pub(crate) fn execute_interaction(
        &mut self,
        effect: InteractionEffect,
        context: &InteractionExecutionContext,
    ) -> Result<EffectAcknowledgement, EffectFailure> {
        let mut clipboard = SystemClipboard;
        self.execute_interaction_with_clipboard(effect, context, &mut clipboard)
    }

    pub(crate) fn execute_interaction_with_clipboard(
        &mut self,
        effect: InteractionEffect,
        context: &InteractionExecutionContext,
        clipboard: &mut dyn Clipboard,
    ) -> Result<EffectAcknowledgement, EffectFailure> {
        match effect {
            InteractionEffect::AdjustSelected(delta) => {
                let id = selected_control(context.selected_control)?;
                self.execute(LiveEffect::EditControl {
                    id,
                    edit: ControlEdit::Delta(f32::from(delta)),
                })
            }
            InteractionEffect::CommitNumeric(value) => {
                let id = selected_control(context.selected_control)?;
                self.execute(LiveEffect::EditControl {
                    id,
                    edit: ControlEdit::Value(value),
                })
            }
            InteractionEffect::PaletteJump { tab, index, id } => {
                self.execute(LiveEffect::SelectControl { tab, index, id })
            }
            // Needs only the session, so the generic bridge can resolve it;
            // the production path adds closing the open editor first.
            InteractionEffect::PaletteModule { tab, catalog_index } => {
                self.place_module(tab, catalog_index)
            }
            InteractionEffect::PaletteCommit(edits) => {
                let edits = staged_edits(edits);
                if edits.is_empty() {
                    return Ok(EffectAcknowledgement::NoChange);
                }
                self.execute(LiveEffect::StageForBar {
                    target_beat: context.beat,
                    edits,
                })?;
                self.execute(LiveEffect::CommitPending { beat: context.beat })
            }
            InteractionEffect::SelectPage(page) => Ok(EffectAcknowledgement::PageSelected(page)),
            InteractionEffect::Save => self.execute_with_clipboard(LiveEffect::CopySong, clipboard),
            InteractionEffect::Quit => Ok(EffectAcknowledgement::QuitRequested),
            unsupported @ (InteractionEffect::AutomationConfirm(_)
            | InteractionEffect::AddAutomation(_)
            | InteractionEffect::ResetSelected
            | InteractionEffect::ToggleAuto
            | InteractionEffect::ToggleUnits
            | InteractionEffect::ToggleMute { .. }
            | InteractionEffect::RemoveAutomation
            | InteractionEffect::ReseedAutomation
            | InteractionEffect::RandomizeSelected
            | InteractionEffect::RandomizeScope
            | InteractionEffect::CloseAutomationAll
            | InteractionEffect::TouchSelected
            | InteractionEffect::PaletteCommitAtBar(_)
            | InteractionEffect::PerformanceInstrument(_)
            | InteractionEffect::HoldPerformanceSelector(_)
            | InteractionEffect::ReleaseHeldSelector(_)
            | InteractionEffect::PerformanceEdit { .. }
            | InteractionEffect::LeadTone { .. }
            | InteractionEffect::LeadRelease
            | InteractionEffect::LeadNudge { .. }
            | InteractionEffect::LeadPattern
            | InteractionEffect::LeadCapture
            | InteractionEffect::GesturePress { .. }
            | InteractionEffect::GestureRelease(_)
            | InteractionEffect::GestureReleaseAll) => {
                Err(EffectFailure::UnsupportedInteraction(unsupported))
            }
        }
    }

    /// Execute one kernel transition's ordered effects and stop at the first
    /// failure. Later effects are never attempted. The production path has its
    /// own bridge below, so this generic one exists only for the tests that
    /// drive ordering without adapter-owned context.
    #[cfg(test)]
    pub(crate) fn execute_interactions_ordered_with_clipboard(
        &mut self,
        effects: impl IntoIterator<Item = InteractionEffect>,
        context: &InteractionExecutionContext,
        clipboard: &mut dyn Clipboard,
    ) -> Vec<Result<EffectAcknowledgement, EffectFailure>> {
        run_ordered(effects, |effect| {
            self.execute_interaction_with_clipboard(effect, context, clipboard)
        })
    }

    pub(crate) fn execute_production_interactions_with_clipboard(
        &mut self,
        effects: impl IntoIterator<Item = InteractionEffect>,
        context: &mut ProductionInteractionContext<'_>,
        clipboard: &mut dyn Clipboard,
    ) -> Vec<Result<EffectAcknowledgement, EffectFailure>> {
        run_ordered(effects, |effect| {
            self.execute_production_interaction(effect, context, clipboard)
        })
    }

    fn execute_production_interaction(
        &mut self,
        effect: InteractionEffect,
        context: &mut ProductionInteractionContext<'_>,
        clipboard: &mut dyn Clipboard,
    ) -> Result<EffectAcknowledgement, EffectFailure> {
        match effect {
            InteractionEffect::AdjustSelected(delta) => {
                self.with_automation(|executor, automation| {
                    adjust_lfo_or_control(
                        executor,
                        automation,
                        context.automation_selected,
                        context.tab,
                        context.selected,
                        f32::from(delta),
                        context.beat,
                        context.flipped,
                    );
                })
            }
            InteractionEffect::CommitNumeric(value) => {
                self.with_automation(|executor, automation| {
                    set_modulator_or_control(
                        executor,
                        automation,
                        context.automation_selected,
                        context.tab,
                        context.selected,
                        value,
                        context.beat,
                        context.flipped,
                    );
                })
            }
            InteractionEffect::AutomationConfirm(kind) => {
                let id = selected_control(context.selected_control)?;
                let kind = ModKind::from(kind);
                let mut selected = context.automation_selected;
                open_modulator_effect_for_id(self, id, kind, &mut selected);
                Ok(self.published())
            }
            InteractionEffect::AddAutomation(kind) => {
                let id = selected_control(context.selected_control)?;
                let kind = ModKind::from(kind);
                if !add_modulator_effect_for_id(self, id, kind) {
                    return Err(EffectFailure::AutomationLaneLimit);
                }
                Ok(self.published())
            }
            InteractionEffect::ResetSelected => self.with_automation(|executor, automation| {
                reset_lfo_or_control(
                    executor,
                    automation,
                    context.automation_selected,
                    context.tab,
                    context.selected,
                    context.beat,
                );
            }),
            InteractionEffect::ToggleAuto => {
                self.toggle_auto(context.beat);
                Ok(self.published())
            }
            InteractionEffect::ToggleUnits => self.with_automation(|executor, automation| {
                toggle_units_effect(
                    executor,
                    automation,
                    context.flipped,
                    context.automation_selected,
                    context.tab,
                    context.selected,
                    context.beat,
                );
            }),
            InteractionEffect::ToggleMute { master } => {
                self.toggle_mute(if master { Tab::Master } else { context.tab });
                Ok(self.published())
            }
            InteractionEffect::RemoveAutomation => self.with_automation(|executor, automation| {
                remove_automation_effect(
                    executor,
                    automation,
                    context.selected_control,
                    context.automation_selected,
                );
            }),
            InteractionEffect::ReseedAutomation => self.with_automation(reseed_automation_effect),
            InteractionEffect::RandomizeSelected => {
                let id = selected_control(context.selected_control)?;
                let spec = spec_by_id(id).ok_or(EffectFailure::MissingContext("control"))?;
                let ratio = self.rng.r#gen::<f32>();
                let snapshot = self.edit_session(Some(spec.id), |snapshot| {
                    spec.apply_ratio(ratio, &mut snapshot.controls);
                });
                Ok(EffectAcknowledgement::Published {
                    generation: snapshot.generation,
                })
            }
            InteractionEffect::RandomizeScope => {
                let mut rng = self.rng.clone();
                let snapshot = self.edit_session(None, |snapshot| {
                    if context.randomizes_automation {
                        match snapshot.automation.active_kind() {
                            Some(ModKind::Lfo) => {
                                if let Some(address) = snapshot.automation.active_address()
                                    && let Some(route) = snapshot.automation.route_mut(address)
                                {
                                    route.randomize(&mut rng, context.beat);
                                }
                            }
                            Some(ModKind::Envelope) => {
                                if let Some(address) = snapshot.automation.active_address()
                                    && let Some(route) = snapshot.automation.envelope_mut(address)
                                {
                                    route.randomize(&mut rng);
                                }
                            }
                            None => {}
                        }
                    } else {
                        for id in context.visible_control_ids {
                            if let Some(spec) = spec_by_id(id) {
                                spec.apply_ratio(rng.r#gen(), &mut snapshot.controls);
                            }
                        }
                    }
                });
                self.rng = rng;
                Ok(EffectAcknowledgement::Published {
                    generation: snapshot.generation,
                })
            }
            InteractionEffect::CloseAutomationAll => {
                self.edit_navigation_automation(AutomationState::close_editor);
                Ok(EffectAcknowledgement::NoChange)
            }
            InteractionEffect::TouchSelected => {
                let id = selected_control(context.selected_control)?;
                let tab = tab_owning_control(id).unwrap_or(context.tab);
                let index = spec_index(tab, id).unwrap_or(context.selected);
                self.execute(LiveEffect::SelectControl { tab, index, id })
            }
            InteractionEffect::PaletteCommitAtBar(edits) => {
                let edits = staged_edits(edits);
                self.execute(LiveEffect::StageForBar {
                    target_beat: next_bar_beat(context.beat),
                    edits,
                })
            }
            InteractionEffect::PaletteJump { tab, index, id } => {
                self.edit_navigation_automation(AutomationState::close_editor);
                self.execute(LiveEffect::SelectControl { tab, index, id })
            }
            InteractionEffect::PerformanceInstrument(_)
            | InteractionEffect::HoldPerformanceSelector(_)
            | InteractionEffect::ReleaseHeldSelector(_) => Ok(EffectAcknowledgement::NoChange),
            // A played note is a gesture over the song, not an edit of it:
            // it publishes through the session so the audio thread sees it,
            // but never exits auto — soloing over a morph is the point. The
            // phrase buffer remembers it so `r` can keep it afterwards.
            InteractionEffect::LeadTone { tone, hold } => {
                self.phrase.push(LeadPress {
                    beat: context.beat,
                    tone,
                });
                let snapshot = self.session.update(|snapshot| {
                    snapshot.lead_play.presses = snapshot.lead_play.presses.wrapping_add(1);
                    snapshot.lead_play.tone = tone;
                    snapshot.lead_play.held = hold;
                });
                Ok(EffectAcknowledgement::Published {
                    generation: snapshot.generation,
                })
            }
            InteractionEffect::LeadRelease => {
                let snapshot = self
                    .session
                    .update(|snapshot| snapshot.lead_play.held = false);
                Ok(EffectAcknowledgement::Published {
                    generation: snapshot.generation,
                })
            }
            InteractionEffect::LeadNudge { id, delta } => {
                let spec = spec_by_id(id).ok_or(EffectFailure::MissingContext(id))?;
                let snapshot = self.edit_session(Some(spec.id), |snapshot| {
                    spec.apply_delta(f32::from(delta), &mut snapshot.controls);
                });
                Ok(EffectAcknowledgement::Published {
                    generation: snapshot.generation,
                })
            }
            InteractionEffect::LeadPattern => {
                let spec = spec_by_id(LEAD_PATTERN_ID)
                    .ok_or(EffectFailure::MissingContext(LEAD_PATTERN_ID))?;
                let snapshot = self.edit_session(Some(spec.id), |snapshot| {
                    let next = LeadPattern::from_value(snapshot.controls.lead.pattern).toggled();
                    spec.apply_value(next.value(), &mut snapshot.controls);
                });
                Ok(EffectAcknowledgement::Published {
                    generation: snapshot.generation,
                })
            }
            InteractionEffect::LeadCapture => {
                let lead = &self.session.load().controls.lead;
                let Some(capture) = lead_capture(
                    self.phrase.presses(),
                    context.beat,
                    lead.rate_beats,
                    lead.offset_beats,
                ) else {
                    self.message = Some(("nothing played yet".to_string(), Instant::now()));
                    return Ok(EffectAcknowledgement::NoChange);
                };
                let steps = spec_by_id(LEAD_STEPS_ID)
                    .ok_or(EffectFailure::MissingContext(LEAD_STEPS_ID))?;
                let pattern = spec_by_id(LEAD_PATTERN_ID)
                    .ok_or(EffectFailure::MissingContext(LEAD_PATTERN_ID))?;
                let snapshot = self.edit_session(Some(steps.id), |snapshot| {
                    snapshot.controls.lead.steps = capture.steps;
                    steps.apply_value(capture.count as f32, &mut snapshot.controls);
                    pattern.apply_value(LeadPattern::Play.value(), &mut snapshot.controls);
                });
                self.message = Some((format!("kept {} steps", capture.count), Instant::now()));
                Ok(EffectAcknowledgement::Published {
                    generation: snapshot.generation,
                })
            }
            InteractionEffect::GesturePress { kind, tab } => {
                let now_seconds = self.session.audio_seconds();
                let snapshot = self
                    .session
                    .update(|snapshot| snapshot.gestures.press(kind, tab, now_seconds));
                Ok(EffectAcknowledgement::Published {
                    generation: snapshot.generation,
                })
            }
            InteractionEffect::GestureRelease(kind) => {
                let now_seconds = self.session.audio_seconds();
                let snapshot = self
                    .session
                    .update(|snapshot| snapshot.gestures.release(kind, now_seconds));
                Ok(EffectAcknowledgement::Published {
                    generation: snapshot.generation,
                })
            }
            InteractionEffect::GestureReleaseAll => {
                if !self.session.load().gestures.has_held() {
                    return Ok(EffectAcknowledgement::NoChange);
                }
                let now_seconds = self.session.audio_seconds();
                let snapshot = self
                    .session
                    .update(|snapshot| snapshot.gestures.release_all(now_seconds));
                Ok(EffectAcknowledgement::Published {
                    generation: snapshot.generation,
                })
            }
            InteractionEffect::PaletteModule { tab, catalog_index } => {
                self.edit_navigation_automation(AutomationState::close_editor);
                self.place_module(tab, catalog_index)
            }
            InteractionEffect::PerformanceEdit {
                targets,
                focus,
                action,
            } => {
                let edits = targets
                    .iter()
                    .map(|instrument| {
                        performance_target(instrument, action)
                            .ok_or(EffectFailure::MissingContext("performance target"))
                    })
                    .collect::<Result<Vec<_>, _>>()?;
                let (tab, index, focus_spec, _) = performance_target(focus, action)
                    .ok_or(EffectFailure::MissingContext("performance focus"))?;
                let snapshot = self.edit_session(None, |snapshot| {
                    for (_, _, spec, direction) in &edits {
                        spec.apply_delta(*direction, &mut snapshot.controls);
                    }
                });
                for (_, _, spec, _) in &edits {
                    self.recent.touch(spec.id);
                }
                self.recent.touch(focus_spec.id);
                Ok(EffectAcknowledgement::PerformanceEdited {
                    tab,
                    index,
                    id: focus_spec.id,
                    generation: snapshot.generation,
                })
            }
            other => self.execute_interaction_with_clipboard(
                other,
                &InteractionExecutionContext {
                    selected_control: context.selected_control,
                    beat: context.beat,
                },
                clipboard,
            ),
        }
    }
}

/// Run an ordered effect list and stop at the first failure: every bridge
/// executes its transition's effects in order, and none of them may attempt
/// an effect after one has failed.
fn run_ordered<T>(
    effects: impl IntoIterator<Item = T>,
    mut execute: impl FnMut(T) -> Result<EffectAcknowledgement, EffectFailure>,
) -> Vec<Result<EffectAcknowledgement, EffectFailure>> {
    let mut results = Vec::new();
    for effect in effects {
        let result = execute(effect);
        let failed = result.is_err();
        results.push(result);
        if failed {
            break;
        }
    }
    results
}

#[derive(Default)]
pub(crate) struct RecentControls {
    ids: Vec<&'static str>,
}

impl RecentControls {
    const CAPACITY: usize = 10;

    pub(crate) fn ids(&self) -> &[&'static str] {
        &self.ids
    }

    pub(crate) fn touch(&mut self, id: &'static str) {
        self.ids.retain(|&known| known != id);
        self.ids.insert(0, id);
        self.ids.truncate(Self::CAPACITY);
    }

    pub(crate) fn touch_edits(&mut self, edits: &[StagedEdit]) {
        for edit in edits.iter().rev() {
            self.touch(edit.id);
        }
    }
}

#[cfg(test)]
#[cfg(test)]
mod tests {
    use super::*;
    use crate::fluid::interaction::PerformanceInstrument;

    #[derive(Default)]
    struct FakeClipboard {
        value: Option<String>,
        failure: Option<ClipboardError>,
    }

    impl Clipboard for FakeClipboard {
        fn set_text(&mut self, text: String) -> Result<(), ClipboardError> {
            if let Some(error) = self.failure.take() {
                Err(error)
            } else {
                self.value = Some(text);
                Ok(())
            }
        }
    }

    fn executor() -> EffectExecutor {
        executor_with(FluidControls::default())
    }

    fn executor_with(controls: FluidControls) -> EffectExecutor {
        let session = LiveSession::new(LiveSessionSnapshot::from_controls(controls));
        EffectExecutor::new(
            session,
            AutoControls::new(no_morph(), decode_auto_states(), DEFAULT_AUTO_BARS),
        )
    }

    #[test]
    fn toggle_mute_preserves_the_authored_level_and_toggles_the_session_gate() {
        let mut controls = FluidControls::default();
        controls.perc.level = 0.65;
        let mut executor = executor_with(controls);

        executor.toggle_mute(Tab::Perc);
        assert_eq!(executor.session().load().controls.perc.level, 0.65);
        assert!(executor.session().load().muted[Tab::Perc as usize]);

        executor.toggle_mute(Tab::Perc);
        assert_eq!(executor.session().load().controls.perc.level, 0.65);
        assert!(!executor.session().load().muted[Tab::Perc as usize]);
    }

    #[test]
    fn toggle_mute_on_master_is_independent_of_track_mute() {
        let mut controls = FluidControls::default();
        controls.master.level = 0.8;
        controls.bass.level = 0.5;
        let mut executor = executor_with(controls);

        executor.toggle_mute(Tab::Master);
        assert!(executor.session().load().muted[Tab::Master as usize]);
        assert_eq!(executor.session().load().controls.bass.level, 0.5);

        executor.toggle_mute(Tab::Bass);
        assert!(executor.session().load().muted[Tab::Bass as usize]);
        // Master stays muted; muting bass didn't disturb it or restore it early.
        assert!(executor.session().load().muted[Tab::Master as usize]);

        executor.toggle_mute(Tab::Master);
        assert!(!executor.session().load().muted[Tab::Master as usize]);
        assert_eq!(executor.session().load().controls.master.level, 0.8);
        assert!(executor.session().load().muted[Tab::Bass as usize]);
    }

    /// Muting used to stand auto mode down, because `edit_session` exited auto
    /// unconditionally. The morph then stopped mid-write and stranded whatever
    /// automation it had just installed, which kept rendering as lanes on
    /// controls that never had routes.
    #[test]
    fn muting_never_stands_auto_mode_down() {
        let mut executor = executor_with(FluidControls::default());
        executor.toggle_auto(0.0);
        assert!(
            executor.auto_position(0.0).is_some(),
            "auto should be running"
        );

        executor.toggle_mute(Tab::Master);
        assert!(
            executor.auto_position(0.0).is_some(),
            "mute must not end the morph"
        );
        executor.toggle_mute(Tab::Master);
        assert!(
            executor.auto_position(0.0).is_some(),
            "unmute must not end the morph either"
        );
    }

    /// The other half of the contract: a deliberate edit *does* take over, or
    /// the morph would immediately overwrite what the user just set.
    #[test]
    fn editing_a_control_stands_auto_mode_down() {
        let mut executor = executor_with(FluidControls::default());
        executor.toggle_auto(0.0);
        assert!(executor.auto_position(0.0).is_some());

        executor
            .execute(LiveEffect::EditControl {
                id: "bass.level",
                edit: ControlEdit::Delta(1.0),
            })
            .expect("editing a real control succeeds");
        assert!(
            executor.auto_position(0.0).is_none(),
            "a deliberate edit takes the controls over"
        );
    }

    /// Typing a module name adds it to the current layer, inert, and lands
    /// the cursor on the row it just created.
    #[test]
    fn confirming_a_module_row_adds_it_inert_and_selects_it() {
        let mut executor = executor_with(FluidControls::default());
        let delay = module_catalog_index("delay");

        let ack = executor
            .execute_interaction(
                InteractionEffect::PaletteModule {
                    tab: Tab::Bass,
                    catalog_index: delay,
                },
                &InteractionExecutionContext::default(),
            )
            .expect("adding to a layer with free slots succeeds");

        let slots = executor.session().load().controls.modules.bass;
        let placed = chain_amount_slot(&slots, "delay").expect("delay was placed");
        // Added modules start silent whatever the factory preset does, so
        // adding one can never change what is playing.
        assert_eq!(slots[placed].amount, 0.0);
        assert!(matches!(
            ack,
            EffectAcknowledgement::ControlSelected { tab: Tab::Bass, .. }
        ));
    }

    /// The rule that stops three compressors piling up: a layer that already
    /// holds the module gets a jump, never a second copy.
    #[test]
    fn confirming_a_module_already_on_the_layer_jumps_instead_of_duplicating() {
        let mut executor = executor_with(FluidControls::default());
        let drive = module_catalog_index("drive");

        // Kick ships with Drive pre-loaded at 0.2.
        let before = executor.session().load().controls.modules.kick;
        executor
            .execute_interaction(
                InteractionEffect::PaletteModule {
                    tab: Tab::Kick,
                    catalog_index: drive,
                },
                &InteractionExecutionContext::default(),
            )
            .expect("jumping to an existing module succeeds");

        let after = executor.session().load().controls.modules.kick;
        assert_eq!(
            before, after,
            "no second Drive, and the amount is untouched"
        );
    }

    /// A full chain says so with the count rather than silently doing nothing.
    #[test]
    fn a_full_chain_refuses_loudly() {
        let mut controls = FluidControls::default();
        for slot in controls.modules.pad.iter_mut() {
            *slot = preset_slot("room", 0.0);
        }
        let mut executor = executor_with(controls);
        let delay = module_catalog_index("delay");

        let ack = executor
            .execute_interaction(
                InteractionEffect::PaletteModule {
                    tab: Tab::Chords,
                    catalog_index: delay,
                },
                &InteractionExecutionContext::default(),
            )
            .expect("a full chain is a message, not a failure");
        match ack {
            EffectAcknowledgement::Message(text) => {
                assert!(text.contains("full"), "unhelpful message: {text}");
                assert!(text.contains(&MODULE_SLOTS.to_string()), "no count: {text}");
            }
            other => panic!("expected a full-chain message, got {other:?}"),
        }
    }

    #[test]
    fn ordered_effects_publish_then_apply_ui_consequences() {
        let mut executor = executor();
        let results = executor.execute_ordered([
            LiveEffect::EditControl {
                id: "master.bpm",
                edit: ControlEdit::Value(91.0),
            },
            LiveEffect::ShowMessage("done".into()),
        ]);

        assert!(matches!(
            results[0],
            Ok(EffectAcknowledgement::Published { generation: 1 })
        ));
        assert_eq!(executor.session.load().controls.master.bpm, 91.0);
        assert_eq!(executor.recent.ids(), &["master.bpm"]);
        assert_eq!(executor.message(), Some("done"));
    }

    #[test]
    fn ordered_effects_stop_at_first_failure() {
        let mut executor = executor();
        let results = executor.execute_ordered([
            LiveEffect::EditControl {
                id: "missing.control",
                edit: ControlEdit::Value(1.0),
            },
            LiveEffect::ShowMessage("must not run".into()),
        ]);

        assert_eq!(
            results,
            vec![Err(EffectFailure::UnknownControl("missing.control"))]
        );
        assert_eq!(executor.message(), None);
        assert_eq!(executor.session.load().generation, 0);
    }

    #[test]
    fn failed_clipboard_is_reported_without_false_success_message() {
        let mut executor = executor();
        let mut clipboard = FakeClipboard {
            failure: Some(ClipboardError::WriteRejected("denied".into())),
            ..FakeClipboard::default()
        };
        assert_eq!(
            executor.execute_with_clipboard(LiveEffect::CopySong, &mut clipboard),
            Err(EffectFailure::Clipboard(ClipboardError::WriteRejected(
                "denied".into()
            )))
        );
        assert_eq!(executor.message(), None);
    }

    /// A Delay time row steps through the registry: `contextual` hands the
    /// loaded clock's grid to `apply_delta`, so Sync moves on the beat grid
    /// and Free by 10 ms with no Delay-specific edit path in between.
    #[test]
    fn delay_time_rows_step_on_the_loaded_clocks_grid() {
        let mut controls = FluidControls::default();
        controls.modules.kick[2] = preset_slot("delay", 0.5);
        controls.modules.kick[2].time = 0.5;
        let mut executor = executor_with(controls);
        let selected = tab_specs(Tab::Kick)
            .iter()
            .position(|spec| spec.id == "kick.slot3.time")
            .expect("kick slot 3 has a time row");
        let mut flipped = FlippedUnits::default();
        let mut context = ProductionInteractionContext {
            selected_control: Some("kick.slot3.time"),
            visible_control_ids: &[],
            randomizes_automation: false,
            tab: Tab::Kick,
            selected,
            automation_selected: 0,
            beat: 0.0,
            flipped: &mut flipped,
        };
        let mut clipboard = FakeClipboard::default();

        executor.execute_production_interactions_with_clipboard(
            [InteractionEffect::AdjustSelected(1)],
            &mut context,
            &mut clipboard,
        );
        assert_eq!(
            executor.session().load().controls.modules.kick[2].time,
            0.75
        );

        executor.edit_session(None, |snapshot| {
            switch_delay_clock(&mut snapshot.controls.modules.kick[2], false, 120.0);
        });
        // Sync -> Free keeps the audible length, snapped to the 10 ms grid.
        assert_eq!(
            executor.session().load().controls.modules.kick[2].time,
            380.0
        );
        executor.execute_production_interactions_with_clipboard(
            [InteractionEffect::AdjustSelected(1)],
            &mut context,
            &mut clipboard,
        );
        assert_eq!(
            executor.session().load().controls.modules.kick[2].time,
            390.0
        );
    }

    #[test]
    fn interaction_effects_require_context_and_never_silently_drop() {
        let mut executor = executor();
        assert_eq!(
            executor.execute_interaction(
                InteractionEffect::AdjustSelected(1),
                &InteractionExecutionContext::default(),
            ),
            Err(EffectFailure::MissingContext("selected control"))
        );
        assert_eq!(
            executor.execute_interaction(
                InteractionEffect::PerformanceInstrument(PerformanceInstrument::Kick),
                &InteractionExecutionContext::default(),
            ),
            Err(EffectFailure::UnsupportedInteraction(
                InteractionEffect::PerformanceInstrument(PerformanceInstrument::Kick)
            ))
        );
    }

    #[test]
    fn ordered_interaction_failure_prevents_later_clipboard_side_effect() {
        let mut executor = executor();
        let mut clipboard = FakeClipboard::default();
        let results = executor.execute_interactions_ordered_with_clipboard(
            [
                InteractionEffect::SelectPage(Page::Bass),
                InteractionEffect::PerformanceInstrument(PerformanceInstrument::Bass),
                InteractionEffect::Save,
            ],
            &InteractionExecutionContext::default(),
            &mut clipboard,
        );
        assert_eq!(results.len(), 2);
        assert_eq!(
            results[0],
            Ok(EffectAcknowledgement::PageSelected(Page::Bass))
        );
        assert_eq!(
            results[1],
            Err(EffectFailure::UnsupportedInteraction(
                InteractionEffect::PerformanceInstrument(PerformanceInstrument::Bass)
            ))
        );
        assert_eq!(clipboard.value, None);
    }

    #[test]
    fn typed_palette_jump_is_acknowledged_without_session_mutation() {
        let mut executor = executor();
        let acknowledgement = executor
            .execute_interaction(
                InteractionEffect::PaletteJump {
                    tab: Tab::Master,
                    index: 1,
                    id: "master.bpm",
                },
                &InteractionExecutionContext::default(),
            )
            .unwrap();

        assert_eq!(
            acknowledgement,
            EffectAcknowledgement::ControlSelected {
                tab: Tab::Master,
                index: 1,
                id: "master.bpm",
            }
        );
        assert_eq!(executor.recent.ids(), &["master.bpm"]);
        assert_eq!(executor.session.load().generation, 0);
    }

    #[test]
    fn typed_palette_commit_applies_payload_atomically() {
        let mut executor = executor();
        let acknowledgement = executor
            .execute_interaction(
                InteractionEffect::PaletteCommit(vec![
                    PaletteStagedEdit {
                        id: "master.bpm",
                        value_bits: 99.0f32.to_bits(),
                    },
                    PaletteStagedEdit {
                        id: "pad.level",
                        value_bits: 25.0f32.to_bits(),
                    },
                ]),
                &InteractionExecutionContext {
                    beat: 12.0,
                    ..InteractionExecutionContext::default()
                },
            )
            .unwrap();

        assert_eq!(
            acknowledgement,
            EffectAcknowledgement::Published { generation: 1 }
        );
        let session = executor.session.load();
        assert_eq!(session.controls.master.bpm, 99.0);
        assert_eq!(session.controls.pad.level, 0.25);
        assert_eq!(executor.recent.ids(), &["master.bpm", "pad.level"]);
    }

    /// Play mode never arms anything: presses only publish play state and
    /// feed the phrase buffer. `r` then lifts the phrase into the lane as
    /// one auto-exiting edit and sets the lane playing; with nothing played
    /// it is an explicit no-change with a notice.
    #[test]
    fn lead_capture_keeps_the_played_phrase_as_the_lane() {
        let mut executor = executor();
        let mut flipped = FlippedUnits::default();
        let mut clipboard = FakeClipboard::default();
        let mut run = |executor: &mut EffectExecutor, effect, beat| {
            let mut context = ProductionInteractionContext {
                selected_control: None,
                visible_control_ids: &[],
                randomizes_automation: false,
                tab: Tab::Lead,
                selected: 0,
                automation_selected: 0,
                beat,
                flipped: &mut flipped,
            };
            executor
                .execute_production_interactions_with_clipboard(
                    [effect],
                    &mut context,
                    &mut clipboard,
                )
                .pop()
                .unwrap()
                .unwrap()
        };
        let press = |tone| InteractionEffect::LeadTone { tone, hold: false };

        assert_eq!(
            run(&mut executor, InteractionEffect::LeadCapture, 0.0),
            EffectAcknowledgement::NoChange
        );
        assert_eq!(executor.message(), Some("nothing played yet"));

        run(&mut executor, InteractionEffect::LeadPattern, 0.0);
        assert_eq!(
            LeadPattern::from_value(executor.session.load().controls.lead.pattern),
            LeadPattern::Off
        );
        // Rate 0.5: beats 8, 8.5, 9 are steps 16, 17, 18 -> lane 0, 1, 2.
        run(&mut executor, press(1), 8.0);
        run(&mut executor, press(3), 8.5);
        run(&mut executor, press(5), 9.0);
        assert_eq!(
            executor.session.load().controls.lead.steps,
            DEFAULT_LEAD_STEPS,
            "playing edits nothing"
        );

        run(&mut executor, InteractionEffect::LeadCapture, 9.4);
        let session = executor.session.load();
        assert_eq!(session.controls.lead.step_count, 4.0);
        assert_eq!(&session.controls.lead.steps[..4], &[1.0, 3.0, 5.0, 0.0]);
        assert_eq!(
            LeadPattern::from_value(session.controls.lead.pattern),
            LeadPattern::Play,
            "keeping a phrase sets the lane playing"
        );
        assert!(!executor.auto.is_running());
        assert_eq!(executor.message(), Some("kept 4 steps"));
        assert_eq!(executor.recent.ids()[0], LEAD_STEPS_ID);
    }

    #[test]
    fn empty_typed_palette_commit_is_an_explicit_no_change() {
        let mut executor = executor();
        assert_eq!(
            executor.execute_interaction(
                InteractionEffect::PaletteCommit(Vec::new()),
                &InteractionExecutionContext {
                    beat: 12.0,
                    ..InteractionExecutionContext::default()
                },
            ),
            Ok(EffectAcknowledgement::NoChange)
        );
        assert_eq!(executor.session.load().generation, 0);
        assert!(executor.pending().is_none());
        assert!(executor.recent.ids().is_empty());
    }

    #[test]
    fn selection_consequence_updates_mru_and_returns_navigation_target() {
        let mut executor = executor();
        let result = executor
            .execute(LiveEffect::SelectControl {
                tab: Tab::Master,
                index: 1,
                id: "master.bpm",
            })
            .unwrap();

        assert_eq!(
            result,
            EffectAcknowledgement::ControlSelected {
                tab: Tab::Master,
                index: 1,
                id: "master.bpm",
            }
        );
        assert_eq!(executor.recent.ids(), &["master.bpm"]);
        assert_eq!(executor.session.load().generation, 0);
    }

    #[test]
    fn staged_commit_is_atomic_and_updates_mru_at_due_beat() {
        let mut executor = executor();
        let edits = vec![
            StagedEdit {
                id: "master.bpm",
                value: 99.0,
            },
            StagedEdit {
                id: "pad.level",
                value: 25.0,
            },
        ];
        executor
            .execute(LiveEffect::StageForBar {
                target_beat: 8.0,
                edits,
            })
            .unwrap();
        executor
            .execute(LiveEffect::CommitPending { beat: 7.99 })
            .unwrap();
        assert_eq!(executor.session.load().generation, 0);
        executor
            .execute(LiveEffect::CommitPending { beat: 8.0 })
            .unwrap();
        let snapshot = executor.session.load();
        assert_eq!(snapshot.generation, 1);
        assert_eq!(snapshot.controls.master.bpm, 99.0);
        assert_eq!(executor.recent.ids(), &["master.bpm", "pad.level"]);
        assert!(executor.pending().is_none());
    }

    #[test]
    fn invalid_staged_edit_fails_without_publication_or_pending_state() {
        let mut executor = executor();
        let result = executor.execute(LiveEffect::StageForBar {
            target_beat: 4.0,
            edits: vec![StagedEdit {
                id: "missing.control",
                value: 1.0,
            }],
        });

        assert_eq!(
            result,
            Err(EffectFailure::UnknownControl("missing.control"))
        );
        assert_eq!(executor.session.load().generation, 0);
        assert!(executor.pending().is_none());
        assert!(executor.recent.ids().is_empty());
    }

    #[test]
    fn aggregate_edit_exits_auto_before_publishing() {
        let controls = FluidControls::default();
        let session = LiveSession::new(LiveSessionSnapshot::from_controls(controls.clone()));
        let morph = Arc::new(ArcSwap::from_pointee(Some(MorphState::from_live(
            controls,
            AutomationState::default(),
            decode_auto_states(),
            DEFAULT_AUTO_BARS,
            0.0,
        ))));
        let auto = AutoControls::new(Arc::clone(&morph), decode_auto_states(), DEFAULT_AUTO_BARS);
        let mut executor = EffectExecutor::new(session, auto);

        executor
            .execute(LiveEffect::EditControl {
                id: "master.bpm",
                edit: ControlEdit::Value(88.0),
            })
            .unwrap();

        assert!(morph.load().is_none());
        assert_eq!(executor.session.load().controls.master.bpm, 88.0);
        assert_eq!(executor.recent.ids(), &["master.bpm"]);
        let stale_publish = executor.session.transact(|snapshot| {
            if morph.load().is_none() {
                return Err("auto stopped");
            }
            snapshot.controls.master.bpm = 140.0;
            Ok(())
        });
        assert!(matches!(stale_publish, Err("auto stopped")));
        assert_eq!(executor.session.load().controls.master.bpm, 88.0);
    }

    #[test]
    fn standalone_toggle_off_fences_stale_auto_publication() {
        use std::sync::Barrier;
        use std::sync::atomic::{AtomicUsize, Ordering};
        use std::thread;

        let controls = FluidControls::default();
        let session = LiveSession::new(LiveSessionSnapshot::from_controls(controls.clone()));
        let morph = Arc::new(ArcSwap::from_pointee(Some(MorphState::from_live(
            controls,
            AutomationState::default(),
            decode_auto_states(),
            DEFAULT_AUTO_BARS,
            0.0,
        ))));
        let auto = AutoControls::new(Arc::clone(&morph), decode_auto_states(), DEFAULT_AUTO_BARS);
        let mut executor = EffectExecutor::new(session, auto);
        let stale_source = morph.load_full();
        let entered = Arc::new(Barrier::new(2));
        let release = Arc::new(Barrier::new(2));
        let attempts = Arc::new(AtomicUsize::new(0));
        let writer_session = executor.session.clone();
        let writer_morph = Arc::clone(&morph);
        let writer_entered = Arc::clone(&entered);
        let writer_release = Arc::clone(&release);
        let writer_attempts = Arc::clone(&attempts);
        let writer = thread::spawn(move || {
            writer_session.transact(|snapshot| {
                let source_is_current = Arc::ptr_eq(&writer_morph.load_full(), &stale_source);
                if writer_attempts.fetch_add(1, Ordering::SeqCst) == 0 {
                    assert!(stale_source.is_some());
                    assert!(source_is_current);
                    writer_entered.wait();
                    writer_release.wait();
                }
                if !source_is_current {
                    return Err("auto stopped");
                }
                snapshot.controls.master.bpm = 140.0;
                Ok(())
            })
        });

        entered.wait();
        executor.toggle_auto(1.0);
        release.wait();
        let stale_publish = writer.join().unwrap();

        assert!(morph.load().is_none());
        assert_eq!(executor.session.load().generation, 1);
        assert!(matches!(stale_publish, Err("auto stopped")));
        assert_eq!(attempts.load(Ordering::SeqCst), 2);
        assert_eq!(
            executor.session.load().controls.master.bpm,
            FluidControls::default().master.bpm
        );
    }
}