eredu-runtime 0.2.0

Backend-neutral model execution runtime for Eredu
Documentation
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
//! Mandatory family-blind coordination of one complete realtime frame.
//!
//! This module joins portable ingress, schedule interpretation, ordered model
//! decisions, delayed payload history, and exact completion attachment. Model
//! equations and native tensor operations remain behind narrow injected traits.

use std::{cell::RefCell, sync::Arc};

use eredu_core::{
    scheduler::{DistributedTransitionOutput, TransitionOutput, WorkDescriptor},
    Completion, RealtimeInputFrame,
};

use crate::{
    complete_realtime_frame, prepare_realtime_frame, CompletedRealtimeFrame,
    MaterializedRealtimeInput, RealtimeCompletionAttachmentError, RealtimeFrameInterpretationError,
    RealtimeFrameTensorMechanisms, RealtimeGenerationBranch, RealtimeHostTokenMaterializer,
    RealtimeIngressContract, RealtimeIngressError, RealtimePayloadBranch, RealtimePayloadContract,
    RealtimePayloadContractError, RealtimePayloadHistory, Sampler, SamplingBackend,
    SequentialDecisionDriver, SequentialDecisionError, SequentialDecisionPlan,
    SequentialDecisionPlanError,
};

/// Architecture-owned execution of one already interpreted realtime frame.
///
/// Implementations receive only canonical temporal tensors and the existing
/// ordered decision driver. They do not validate host frames, advance the
/// portable schedule, resolve delayed coordinates, or publish state.
pub trait PreparedRealtimeFrameExecutor<B, S, M>
where
    B: SamplingBackend,
    S: Sampler<B>,
{
    /// Architecture execution failure.
    type Error;
    /// Architecture execution resources retained until exact completion exists.
    type Retained;

    /// Mutates only the unpublished model-state branch and resolves every
    /// decision through `driver` in architecture order.
    fn execute(
        &mut self,
        model_state: &mut M,
        temporal: &[B::Token],
        driver: &mut SequentialDecisionDriver<B, S>,
        context: &B::Context,
    ) -> Result<Self::Retained, Self::Error>;
}

/// Narrow exact-completion mechanism for a fully interpreted frame.
///
/// The mechanism may retain output, history, and model-state native resources,
/// but it cannot inspect portable frame source/target semantics.
pub trait RealtimeFrameCompletionMechanism<T, M, R> {
    /// Exact completion object attached to the generation branch.
    type Completion;
    /// Native submission or retention failure.
    type Error;

    /// Submits or records all work required by the completed unpublished frame.
    fn complete(
        &mut self,
        input: MaterializedRealtimeInput<T>,
        output: &CompletedRealtimeFrame<T, T>,
        model_state: &M,
        payload_history: &RealtimePayloadHistory<T>,
        execution: Option<R>,
    ) -> Result<Self::Completion, RealtimeCompletionCreationError<Self::Completion, Self::Error>>;

    /// Reports the exact number of resources explicitly owned by a completion.
    fn retained_resources(&self, _completion: &Self::Completion) -> usize {
        0
    }
}

/// Failure before completion creation or after native work already began.
pub enum RealtimeCompletionCreationError<C, E> {
    /// No native work escaped the failed completion operation.
    BeforeSubmission(E),
    /// Native work began and its exact quarantine completion must be retained.
    AfterSubmission {
        /// Original native submission failure.
        error: E,
        /// Exact completion retaining every possibly live resource.
        completion: C,
    },
}

impl<C, E> RealtimeCompletionCreationError<C, E> {
    /// Reports a failure proven to precede native submission.
    pub const fn before_submission(error: E) -> Self {
        Self::BeforeSubmission(error)
    }

    /// Reports a failure after submission while preserving quarantine ownership.
    pub const fn after_submission(error: E, completion: C) -> Self {
        Self::AfterSubmission { error, completion }
    }
}

/// One completed native frame paired with the exact completion tracked by the scheduler.
pub struct SubmittedRealtimeFrame<T, C> {
    frame: CompletedRealtimeFrame<T, T>,
    completion: C,
    retained_resources: usize,
}

impl<T, C> SubmittedRealtimeFrame<T, C> {
    /// Returns native output values before portable host observation.
    pub const fn frame(&self) -> &CompletedRealtimeFrame<T, T> {
        &self.frame
    }

    /// Returns the exact completion handle shared with the unpublished state branch.
    pub const fn completion(&self) -> &C {
        &self.completion
    }

    /// Consumes the scheduler output into native frame values and exact completion.
    pub fn into_parts(self) -> (CompletedRealtimeFrame<T, T>, C) {
        (self.frame, self.completion)
    }
}

impl<T, C> TransitionOutput for SubmittedRealtimeFrame<T, C>
where
    C: Completion,
{
    type Error = C::Error;

    fn is_complete(&self) -> Result<bool, Self::Error> {
        self.completion.is_complete()
    }

    fn retained_resources(&self) -> usize {
        self.retained_resources
    }
}

/// Family-blind conversion of one completed native frame into its host representation.
///
/// The observer is invoked only after the exact native completion reports ready and
/// successfully validates through [`Completion::wait`]. Implementations may perform
/// tensor-to-host conversion and portable output validation, but cannot publish session state.
pub trait RealtimeFrameHostObserver<T> {
    /// Portable or otherwise host-owned output cached before session publication.
    type Output;
    /// Host conversion or output validation failure.
    type Error: std::error::Error + 'static;

    /// Observes one exactly completed native frame.
    fn observe(
        &mut self,
        frame: &CompletedRealtimeFrame<T, T>,
    ) -> Result<Self::Output, Self::Error>;
}

enum HostObservationState<H: RealtimeFrameHostObserver<T>, T> {
    Pending(H),
    Ready(H::Output),
    Failed(Arc<H::Error>),
}

/// Scheduler transition which gates publication on exact completion and host observation.
///
/// Core schedulers call [`TransitionOutput::is_complete`] before committing the associated
/// semantic-state branch. This wrapper returns `true` only after the native completion has
/// validated and the family-blind observer has produced and cached the host output. Therefore an
/// observation failure follows the scheduler's ordinary failed-transition path and cannot publish
/// the branch. The cached output is consumed only from the transition returned after commit.
pub struct PrepublicationRealtimeFrame<T, C, H>
where
    H: RealtimeFrameHostObserver<T>,
{
    submitted: SubmittedRealtimeFrame<T, C>,
    observation: RefCell<HostObservationState<H, T>>,
}

impl<T, C, H> PrepublicationRealtimeFrame<T, C, H>
where
    H: RealtimeFrameHostObserver<T>,
{
    /// Binds one submitted native frame to its mandatory prepublication observer.
    pub fn new(submitted: SubmittedRealtimeFrame<T, C>, observer: H) -> Self {
        Self {
            submitted,
            observation: RefCell::new(HostObservationState::Pending(observer)),
        }
    }

    /// Consumes the cached host output after the scheduler returns this committed transition.
    ///
    /// Calling this before [`TransitionOutput::is_complete`] succeeds reports that observation is
    /// still pending. A failed observation retains its original error for stable diagnostics.
    pub fn into_host_output(self) -> Result<H::Output, RealtimeHostOutputUnavailable<H::Error>> {
        match self.observation.into_inner() {
            HostObservationState::Pending(_) => Err(RealtimeHostOutputUnavailable::Pending),
            HostObservationState::Ready(output) => Ok(output),
            HostObservationState::Failed(error) => {
                Err(RealtimeHostOutputUnavailable::Observation(error))
            }
        }
    }
}

impl<T, C, H> TransitionOutput for PrepublicationRealtimeFrame<T, C, H>
where
    C: Completion,
    C::Error: 'static,
    H: RealtimeFrameHostObserver<T>,
{
    type Error = RealtimePrepublicationError<C::Error, H::Error>;

    fn is_complete(&self) -> Result<bool, Self::Error> {
        {
            let observation = self.observation.borrow();
            match &*observation {
                HostObservationState::Ready(_) => return Ok(true),
                HostObservationState::Failed(error) => {
                    return Err(RealtimePrepublicationError::Observation(Arc::clone(error)))
                }
                HostObservationState::Pending(_) => {}
            }
        }

        if !self
            .submitted
            .completion()
            .is_complete()
            .map_err(RealtimePrepublicationError::Completion)?
        {
            return Ok(false);
        }
        self.submitted
            .completion()
            .wait()
            .map_err(RealtimePrepublicationError::Completion)?;

        let mut observation = self.observation.borrow_mut();
        let HostObservationState::Pending(observer) = &mut *observation else {
            unreachable!("prepublication observation state was checked before completion wait")
        };
        match observer.observe(self.submitted.frame()) {
            Ok(output) => {
                *observation = HostObservationState::Ready(output);
                Ok(true)
            }
            Err(error) => {
                let error = Arc::new(error);
                *observation = HostObservationState::Failed(Arc::clone(&error));
                Err(RealtimePrepublicationError::Observation(error))
            }
        }
    }

    fn retained_resources(&self) -> usize {
        self.submitted.retained_resources()
    }
}

impl<T, C, H> DistributedTransitionOutput for PrepublicationRealtimeFrame<T, C, H>
where
    C: Completion,
    C::Error: 'static,
    H: RealtimeFrameHostObserver<T>,
    H::Output: WorkDescriptor,
{
    fn encode_distributed_output(&self, output: &mut Vec<u32>) -> Result<(), String> {
        match &*self.observation.borrow() {
            HostObservationState::Ready(observed) => observed
                .encode_descriptor(output)
                .map_err(|error| error.to_string()),
            HostObservationState::Pending(_) => {
                Err("realtime output observation is still pending".into())
            }
            HostObservationState::Failed(error) => {
                Err(format!("realtime output observation failed: {error}"))
            }
        }
    }
}

/// Exact-completion or host-observation failure before session publication.
#[derive(Debug)]
pub enum RealtimePrepublicationError<C, O> {
    /// Native completion readiness or validation failed.
    Completion(C),
    /// Host conversion or output validation failed and was cached exactly once.
    Observation(Arc<O>),
}

impl<C, O> std::fmt::Display for RealtimePrepublicationError<C, O>
where
    C: std::error::Error,
    O: std::error::Error,
{
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Completion(error) => {
                write!(formatter, "realtime exact completion failed: {error}")
            }
            Self::Observation(error) => {
                write!(formatter, "realtime host observation failed: {error}")
            }
        }
    }
}

impl<C, O> std::error::Error for RealtimePrepublicationError<C, O>
where
    C: std::error::Error + 'static,
    O: std::error::Error + 'static,
{
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Completion(error) => Some(error),
            Self::Observation(error) => Some(error.as_ref()),
        }
    }
}

/// A prepublication frame was consumed before a successful host observation was available.
#[derive(Debug)]
pub enum RealtimeHostOutputUnavailable<O> {
    /// Exact completion and host observation have not succeeded yet.
    Pending,
    /// Host observation failed; the original cached failure is retained.
    Observation(Arc<O>),
}

impl<O> std::fmt::Display for RealtimeHostOutputUnavailable<O>
where
    O: std::error::Error,
{
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Pending => formatter.write_str("realtime host output is not observed yet"),
            Self::Observation(error) => {
                write!(formatter, "realtime host observation failed: {error}")
            }
        }
    }
}

impl<O> std::error::Error for RealtimeHostOutputUnavailable<O>
where
    O: std::error::Error + 'static,
{
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Pending => None,
            Self::Observation(error) => Some(error.as_ref()),
        }
    }
}

/// Portable text-then-depth decision controls for one realtime executor.
#[derive(Debug, Clone, PartialEq)]
pub struct RealtimeDecisionExecution {
    allow_fully_forced_tail_skip: bool,
}

impl RealtimeDecisionExecution {
    /// Selects whether an architecture-proven fully forced tail may be omitted.
    pub const fn new(allow_fully_forced_tail_skip: bool) -> Self {
        Self {
            allow_fully_forced_tail_skip,
        }
    }

    /// Whether an architecture-proven fully forced tail may omit model units.
    pub const fn allows_fully_forced_tail_skip(&self) -> bool {
        self.allow_fully_forced_tail_skip
    }
}

/// Coordinates one complete realtime frame on an already unpublished branch.
///
/// Host validation precedes opaque materialization. Schedule and payload-history
/// changes are staged locally until preparation, model decisions, output
/// completion, and exact native completion creation all succeed. Model state is
/// necessarily mutated through its caller-owned transaction branch. Therefore,
/// on any returned error the caller **must discard the entire branch**; the fair
/// scheduler's submission path already enforces that rule.
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
pub fn execute_realtime_frame<B, S, M, T, C, H, F, E, K>(
    contract: &RealtimeIngressContract,
    payload_contract: &RealtimePayloadContract,
    frame: &RealtimeInputFrame,
    branch: &mut RealtimeGenerationBranch<RealtimePayloadBranch<M, T>, S, B::RandomState, C>,
    decisions: &RealtimeDecisionExecution,
    host_materializer: &mut H,
    tensor_mechanisms: &mut F,
    model_executor: &mut E,
    completion_mechanism: &mut K,
    context: &B::Context,
) -> Result<
    SubmittedRealtimeFrame<T, C>,
    RealtimeFrameCoordinatorError<H::Error, F::Error, E::Error, B::Error, K::Error>,
>
where
    B: SamplingBackend<Token = T, Logits = T>,
    B::RandomState: Clone,
    C: Completion + Clone,
    S: Sampler<B> + Clone,
    T: Clone,
    H: RealtimeHostTokenMaterializer<Tensor = T>,
    F: RealtimeFrameTensorMechanisms<Tensor = T>,
    E: PreparedRealtimeFrameExecutor<B, S, M>,
    K: RealtimeFrameCompletionMechanism<T, M, E::Retained, Completion = C>,
{
    if branch.has_submission_completion() {
        return Err(RealtimeFrameCoordinatorError::CompletionAttachment(
            RealtimeCompletionAttachmentError::AlreadyAttached,
        ));
    }
    branch
        .schedule_state()
        .validate_schedule(contract.schedule())
        .map_err(|error| {
            RealtimeFrameCoordinatorError::Interpretation(
                RealtimeFrameInterpretationError::Schedule(error),
            )
        })?;
    if payload_contract.schedule() != contract.schedule() {
        return Err(RealtimeFrameCoordinatorError::PayloadContract(
            RealtimePayloadContractError::ScheduleMismatch,
        ));
    }
    if payload_contract.batch().get() != frame.batch() {
        return Err(RealtimeFrameCoordinatorError::PayloadContract(
            RealtimePayloadContractError::BatchMismatch,
        ));
    }
    if payload_contract.text_domain() != contract.text_domain() {
        return Err(RealtimeFrameCoordinatorError::PayloadContract(
            RealtimePayloadContractError::TextDomainMismatch,
        ));
    }
    if payload_contract.audio_domain() != contract.audio_domain() {
        return Err(RealtimeFrameCoordinatorError::PayloadContract(
            RealtimePayloadContractError::AudioDomainMismatch,
        ));
    }
    let mut payload_history = branch.model_state().payload_history().clone();
    payload_history
        .bind_or_validate_contract(payload_contract)
        .map_err(|error| {
            RealtimeFrameCoordinatorError::Interpretation(
                RealtimeFrameInterpretationError::History(error),
            )
        })?;
    let validated = contract
        .validate(frame)
        .map_err(RealtimeFrameCoordinatorError::Ingress)?;
    let input = validated
        .materialize(host_materializer)
        .map_err(RealtimeFrameCoordinatorError::Materialization)?;

    let schedule = contract.schedule();
    let mut schedule_state = branch.schedule_state().clone();
    let prepared = prepare_realtime_frame(
        schedule,
        &mut schedule_state,
        &mut payload_history,
        &input,
        tensor_mechanisms,
    )
    .map_err(RealtimeFrameCoordinatorError::Interpretation)?;

    let (completed, execution_retained) = if prepared.transition().model_call_required() {
        let plan = SequentialDecisionPlan::new(
            prepared.directives().iter().cloned(),
            prepared.retains_diagnostics(),
            decisions.allow_fully_forced_tail_skip,
        )
        .map_err(RealtimeFrameCoordinatorError::DecisionPlan)?;
        let sampling = branch.sampling();
        let temperatures = std::iter::once(sampling.text_temperature())
            .chain(std::iter::repeat_n(
                sampling.audio_temperature(),
                schedule.depth_audio_codebooks(),
            ))
            .collect();
        let mut driver = branch
            .decision_driver::<B>(plan, temperatures)
            .map_err(RealtimeFrameCoordinatorError::DecisionPlan)?;
        let execution_retained = model_executor
            .execute(
                branch.model_state_mut().model_state_mut(),
                prepared.temporal(),
                &mut driver,
                context,
            )
            .map_err(RealtimeFrameCoordinatorError::Model)?;
        driver
            .finish()
            .map_err(RealtimeFrameCoordinatorError::Decision)?;
        let resolved = driver
            .decisions()
            .iter()
            .map(|decision| decision.token().clone())
            .collect::<Vec<_>>();
        let diagnostics = driver
            .diagnostics()
            .iter()
            .map(|diagnostic| diagnostic.logits().clone())
            .collect::<Vec<_>>();
        let completed = complete_realtime_frame(
            schedule,
            &mut payload_history,
            prepared,
            resolved,
            diagnostics,
            tensor_mechanisms,
        )
        .map_err(RealtimeFrameCoordinatorError::Interpretation)?;
        branch
            .adopt_decision_driver(driver)
            .map_err(RealtimeFrameCoordinatorError::Decision)?;
        (completed, Some(execution_retained))
    } else {
        (
            complete_realtime_frame(
                schedule,
                &mut payload_history,
                prepared,
                Vec::new(),
                Vec::new(),
                tensor_mechanisms,
            )
            .map_err(RealtimeFrameCoordinatorError::Interpretation)?,
            None,
        )
    };

    let completion = match completion_mechanism.complete(
        input,
        &completed,
        branch.model_state_mut().model_state(),
        &payload_history,
        execution_retained,
    ) {
        Ok(completion) => completion,
        Err(RealtimeCompletionCreationError::BeforeSubmission(error)) => {
            return Err(RealtimeFrameCoordinatorError::Completion(error));
        }
        Err(RealtimeCompletionCreationError::AfterSubmission { error, completion }) => {
            branch
                .attach_submission_completion(completion)
                .map_err(RealtimeFrameCoordinatorError::CompletionAttachment)?;
            return Err(RealtimeFrameCoordinatorError::CompletionAfterSubmission(
                error,
            ));
        }
    };
    let retained_resources = completion_mechanism.retained_resources(&completion);

    *branch.schedule_state_mut() = schedule_state;
    *branch.model_state_mut().payload_history_mut() = payload_history;
    branch
        .attach_submission_completion(completion.clone())
        .map_err(RealtimeFrameCoordinatorError::CompletionAttachment)?;
    Ok(SubmittedRealtimeFrame {
        frame: completed,
        completion,
        retained_resources,
    })
}

/// Failure while coordinating an unpublished complete realtime frame.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum RealtimeFrameCoordinatorError<H, F, E, B, K> {
    /// Session payload identity did not match ingress or accepted batch.
    #[error(transparent)]
    PayloadContract(RealtimePayloadContractError),
    /// Portable host validation failed before opaque materialization.
    #[error(transparent)]
    Ingress(RealtimeIngressError),
    /// Validated host payload materialization failed.
    #[error("realtime host payload materialization failed")]
    Materialization(H),
    /// Schedule, history, or tensor interpretation failed.
    #[error("realtime frame interpretation failed")]
    Interpretation(RealtimeFrameInterpretationError<F>),
    /// Decision policy or branch sampling-state cardinality was invalid.
    #[error(transparent)]
    DecisionPlan(SequentialDecisionPlanError),
    /// Ordered sampling or decision completion failed.
    #[error("realtime ordered decision failed")]
    Decision(SequentialDecisionError<B>),
    /// Typed architecture execution failed.
    #[error("realtime prepared model execution failed")]
    Model(E),
    /// Exact completion creation or resource retention failed.
    #[error("realtime exact completion creation failed")]
    Completion(K),
    /// Native completion creation failed after work began; the branch owns quarantine evidence.
    #[error("realtime exact completion creation failed after native submission")]
    CompletionAfterSubmission(K),
    /// The caller supplied a branch which already represented a submission.
    #[error(transparent)]
    CompletionAttachment(RealtimeCompletionAttachmentError),
}

#[cfg(test)]
mod tests {
    use std::{cell::Cell, convert::Infallible, rc::Rc, time::Instant};

    use eredu_core::{
        scheduler::{RequestId, Scheduler, SchedulerLimits, SemanticStateTransaction},
        Completion, RealtimeFrameConvention, RealtimeSpeechConfig, TokenFilter,
    };

    use super::*;
    use crate::{
        PenaltyConfig, RealtimeGenerationState, RealtimePayloadGeneration,
        RealtimePayloadOwnerIdentity, RealtimePayloadState, TokenDomain,
    };

    #[derive(Debug, Clone, Eq, PartialEq)]
    struct Matrix {
        values: Vec<i32>,
        shape: [usize; 2],
    }

    #[derive(Debug, Clone, Copy, thiserror::Error)]
    #[error("test mechanism failed")]
    struct MechanismError;

    #[derive(Default)]
    struct Mechanisms {
        calls: usize,
    }

    impl RealtimeHostTokenMaterializer for Mechanisms {
        type Tensor = Matrix;
        type Error = MechanismError;

        fn materialize_i32(
            &mut self,
            values: &[i32],
            shape: [usize; 2],
        ) -> Result<Self::Tensor, Self::Error> {
            self.calls += 1;
            Ok(Matrix {
                values: values.to_vec(),
                shape,
            })
        }
    }

    impl RealtimeFrameTensorMechanisms for Mechanisms {
        type Tensor = Matrix;
        type Error = MechanismError;

        fn column(
            &mut self,
            matrix: &Self::Tensor,
            column: usize,
        ) -> Result<Self::Tensor, Self::Error> {
            self.calls += 1;
            let columns = matrix.shape[1];
            Ok(Matrix {
                values: (0..matrix.shape[0])
                    .map(|row| matrix.values[row * columns + column])
                    .collect(),
                shape: [matrix.shape[0], 1],
            })
        }

        fn filled_column(&mut self, token: i32, batch: usize) -> Result<Self::Tensor, Self::Error> {
            self.calls += 1;
            Ok(Matrix {
                values: vec![token; batch],
                shape: [batch, 1],
            })
        }

        fn stack_columns(
            &mut self,
            columns: &[Self::Tensor],
            batch: usize,
        ) -> Result<Self::Tensor, Self::Error> {
            self.calls += 1;
            let values = (0..batch)
                .flat_map(|row| columns.iter().map(move |column| column.values[row]))
                .collect();
            Ok(Matrix {
                values,
                shape: [batch, columns.len()],
            })
        }
    }

    struct TestBackend;

    impl SamplingBackend for TestBackend {
        type Logits = Matrix;
        type Token = Matrix;
        type RandomState = usize;
        type Context = ();
        type Error = MechanismError;

        fn error(_message: String) -> Self::Error {
            MechanismError
        }

        fn validate_token(
            token: &Self::Token,
            domain: TokenDomain,
            _context: &Self::Context,
        ) -> Result<Self::Token, Self::Error> {
            token
                .values
                .iter()
                .all(|value| {
                    usize::try_from(*value).is_ok_and(|value| value < domain.cardinality())
                })
                .then(|| token.clone())
                .ok_or(MechanismError)
        }

        fn scale_temperature(
            logits: &Self::Logits,
            _temperature: f32,
            _context: &Self::Context,
        ) -> Result<Self::Logits, Self::Error> {
            Ok(logits.clone())
        }

        fn apply_penalties(
            logits: &Self::Logits,
            _history: &[u32],
            _penalties: PenaltyConfig,
            _context: &Self::Context,
        ) -> Result<Self::Logits, Self::Error> {
            Ok(logits.clone())
        }

        fn apply_top_k(
            logits: Self::Logits,
            _top_k: i32,
            _context: &Self::Context,
        ) -> Result<Self::Logits, Self::Error> {
            Ok(logits)
        }

        fn apply_top_p(
            logits: Self::Logits,
            _top_p: f32,
            _context: &Self::Context,
        ) -> Result<Self::Logits, Self::Error> {
            Ok(logits)
        }

        fn apply_min_p(
            logits: Self::Logits,
            _min_p: f32,
            _context: &Self::Context,
        ) -> Result<Self::Logits, Self::Error> {
            Ok(logits)
        }

        fn apply_token_filter(
            logits: &Self::Logits,
            _filter: &TokenFilter,
            _context: &Self::Context,
        ) -> Result<Self::Logits, Self::Error> {
            Ok(logits.clone())
        }

        fn apply_mirostat(
            logits: &Self::Logits,
            _history: &[u32],
            _penalties: PenaltyConfig,
            _tau: f32,
            _eta: f32,
            _context: &Self::Context,
        ) -> Result<Self::Logits, Self::Error> {
            Ok(logits.clone())
        }

        fn sample_raw(
            logits: &Self::Logits,
            _temperature: f32,
            random: Option<&mut Self::RandomState>,
            _context: &Self::Context,
        ) -> Result<Self::Token, Self::Error> {
            if let Some(random) = random {
                *random += 1;
            }
            Ok(logits.clone())
        }

        fn sample_processed(
            logits: &Self::Logits,
            temperature: f32,
            random: Option<&mut Self::RandomState>,
            context: &Self::Context,
        ) -> Result<Self::Token, Self::Error> {
            Self::sample_raw(logits, temperature, random, context)
        }

        fn token_id(token: &Self::Token, _context: &Self::Context) -> Result<u32, Self::Error> {
            token
                .values
                .first()
                .copied()
                .and_then(|value| u32::try_from(value).ok())
                .ok_or(MechanismError)
        }

        fn token_probability(
            _logits: &Self::Logits,
            _token: u32,
            _context: &Self::Context,
        ) -> Result<f32, Self::Error> {
            Ok(1.0)
        }
    }

    #[derive(Debug, Clone, Eq, PartialEq)]
    struct TestSampler;

    impl Sampler<TestBackend> for TestSampler {
        fn sample(
            &mut self,
            logits: &Matrix,
            temperature: f32,
            random: Option<&mut usize>,
            context: &(),
        ) -> Result<Matrix, MechanismError> {
            TestBackend::sample_raw(logits, temperature, random, context)
        }
    }

    #[derive(Debug, Clone)]
    struct ModelState {
        executions: usize,
        discards: Rc<Cell<usize>>,
    }

    #[derive(Debug, Clone, Copy, thiserror::Error)]
    #[error("test model-state transaction failed")]
    struct ModelStateError;

    impl SemanticStateTransaction for ModelState {
        type Branch = Self;
        type Error = ModelStateError;

        fn branch(&self) -> Result<Self::Branch, Self::Error> {
            Ok(self.clone())
        }

        fn commit_branch(&mut self, branch: Self::Branch) -> Result<(), Self::Error> {
            *self = branch;
            Ok(())
        }

        fn discard_branch(branch: Self::Branch) -> Result<(), Self::Error> {
            branch.discards.set(branch.discards.get() + 1);
            Ok(())
        }
    }

    #[derive(Debug, Clone, Default)]
    struct TestCompletion {
        waits: Rc<Cell<usize>>,
    }

    #[derive(Debug, Clone, Copy, thiserror::Error)]
    #[error("test completion failed")]
    struct CompletionError;

    impl Completion for TestCompletion {
        type Error = CompletionError;

        fn is_complete(&self) -> Result<bool, Self::Error> {
            Ok(true)
        }

        fn wait(&self) -> Result<(), Self::Error> {
            self.waits.set(self.waits.get() + 1);
            Ok(())
        }
    }

    struct Executor {
        fail: bool,
        calls: usize,
    }

    #[derive(Debug, Clone, Copy, thiserror::Error)]
    #[error("test model execution failed")]
    struct ExecutionError;

    impl PreparedRealtimeFrameExecutor<TestBackend, TestSampler, ModelState> for Executor {
        type Error = ExecutionError;
        type Retained = Matrix;

        fn execute(
            &mut self,
            model_state: &mut ModelState,
            _temporal: &[Matrix],
            driver: &mut SequentialDecisionDriver<TestBackend, TestSampler>,
            _context: &(),
        ) -> Result<Self::Retained, Self::Error> {
            self.calls += 1;
            model_state.executions += 1;
            if self.fail {
                return Err(ExecutionError);
            }
            for prediction in 0..driver.plan().len() {
                let value = if prediction == 0 { 2 } else { 6 };
                let domain = TokenDomain::new(if prediction == 0 { 10 } else { 9 });
                driver
                    .resolve(
                        prediction,
                        &Matrix {
                            values: vec![value],
                            shape: [1, 1],
                        },
                        domain,
                        &(),
                    )
                    .map_err(|_| ExecutionError)?;
            }
            Ok(Matrix {
                values: vec![7],
                shape: [1, 1],
            })
        }
    }

    #[derive(Default)]
    struct CompletionMechanism {
        calls: usize,
        retained_calls: usize,
        fail_after_submission: bool,
        waits: Rc<Cell<usize>>,
    }

    impl RealtimeFrameCompletionMechanism<Matrix, ModelState, Matrix> for CompletionMechanism {
        type Completion = TestCompletion;
        type Error = CompletionError;

        fn complete(
            &mut self,
            _input: MaterializedRealtimeInput<Matrix>,
            _output: &CompletedRealtimeFrame<Matrix, Matrix>,
            _model_state: &ModelState,
            _payload_history: &RealtimePayloadHistory<Matrix>,
            execution: Option<Matrix>,
        ) -> Result<Self::Completion, RealtimeCompletionCreationError<Self::Completion, Self::Error>>
        {
            self.calls += 1;
            if let Some(execution) = execution {
                self.retained_calls += 1;
                assert_eq!(execution.values, vec![7]);
            }
            let completion = TestCompletion {
                waits: self.waits.clone(),
            };
            if self.fail_after_submission {
                return Err(RealtimeCompletionCreationError::after_submission(
                    CompletionError,
                    completion,
                ));
            }
            Ok(completion)
        }
    }

    #[derive(Clone)]
    struct ControlledCompletion {
        ready: Rc<Cell<bool>>,
        fail_wait: bool,
        waits: Rc<Cell<usize>>,
    }

    impl Completion for ControlledCompletion {
        type Error = CompletionError;

        fn is_complete(&self) -> Result<bool, Self::Error> {
            Ok(self.ready.get())
        }

        fn wait(&self) -> Result<(), Self::Error> {
            self.waits.set(self.waits.get() + 1);
            if self.fail_wait {
                Err(CompletionError)
            } else {
                Ok(())
            }
        }
    }

    #[derive(Debug, Clone, Copy, thiserror::Error)]
    #[error("test host observation failed")]
    struct ObservationError;

    struct Observer {
        calls: Rc<Cell<usize>>,
        fail: bool,
    }

    impl RealtimeFrameHostObserver<Matrix> for Observer {
        type Output = Vec<i32>;
        type Error = ObservationError;

        fn observe(
            &mut self,
            frame: &CompletedRealtimeFrame<Matrix, Matrix>,
        ) -> Result<Self::Output, Self::Error> {
            self.calls.set(self.calls.get() + 1);
            if self.fail {
                Err(ObservationError)
            } else {
                Ok(frame.text().values.clone())
            }
        }
    }

    #[derive(Clone)]
    struct PublicationState {
        value: usize,
        published: Rc<Cell<usize>>,
        discards: Rc<Cell<usize>>,
    }

    impl SemanticStateTransaction for PublicationState {
        type Branch = Self;
        type Error = Infallible;

        fn branch(&self) -> Result<Self::Branch, Self::Error> {
            Ok(self.clone())
        }

        fn commit_branch(&mut self, branch: Self::Branch) -> Result<(), Self::Error> {
            branch.published.set(branch.value);
            *self = branch;
            Ok(())
        }

        fn discard_branch(branch: Self::Branch) -> Result<(), Self::Error> {
            branch.discards.set(branch.discards.get() + 1);
            Ok(())
        }
    }

    type Generation = RealtimeGenerationState<
        RealtimePayloadState<ModelState, Matrix>,
        TestSampler,
        usize,
        TestCompletion,
    >;

    fn schedule(convention: RealtimeFrameConvention) -> RealtimeSpeechConfig {
        RealtimeSpeechConfig::new(2, 1, 1, 1, 9, 8, convention, vec![0, 0, 1]).unwrap()
    }

    fn state(schedule: &RealtimeSpeechConfig, discards: Rc<Cell<usize>>) -> Generation {
        let payload = RealtimePayloadState::new(
            ModelState {
                executions: 0,
                discards,
            },
            RealtimePayloadHistory::new(schedule.clone()),
            schedule,
        )
        .unwrap();
        Generation::new(
            payload,
            schedule.clone(),
            eredu_core::RealtimeSampling::greedy(),
            vec![TestSampler, TestSampler],
            Some(0),
        )
        .unwrap()
    }

    fn contract(schedule: &RealtimeSpeechConfig) -> RealtimeIngressContract {
        RealtimeIngressContract::new(schedule.clone(), TokenDomain::new(10), TokenDomain::new(9))
            .unwrap()
    }

    fn payload_contract(schedule: &RealtimeSpeechConfig) -> RealtimePayloadContract {
        RealtimePayloadContract::new(
            schedule.clone(),
            1,
            TokenDomain::new(10),
            TokenDomain::new(9),
            RealtimePayloadGeneration::new(1).unwrap(),
            RealtimePayloadOwnerIdentity::new(1).unwrap(),
        )
        .unwrap()
    }

    fn frame() -> RealtimeInputFrame {
        RealtimeInputFrame::new(1, vec![4])
            .with_forced_text(vec![3])
            .with_partially_forced_generated_audio(vec![5], vec![false])
    }

    fn submitted_test_frame<C>(completion: C) -> SubmittedRealtimeFrame<Matrix, C> {
        let schedule = schedule(RealtimeFrameConvention::FeedbackAlignedHistory);
        let state = state(&schedule, Rc::new(Cell::new(0)));
        let mut branch = state.branch().unwrap();
        let mut host = Mechanisms::default();
        let mut tensors = Mechanisms::default();
        let mut executor = Executor {
            fail: false,
            calls: 0,
        };
        let mut completion_mechanism = CompletionMechanism::default();
        let submitted = execute_realtime_frame::<TestBackend, _, _, _, _, _, _, _, _>(
            &contract(&schedule),
            &payload_contract(&schedule),
            &frame(),
            &mut branch,
            &RealtimeDecisionExecution::new(true),
            &mut host,
            &mut tensors,
            &mut executor,
            &mut completion_mechanism,
            &(),
        )
        .unwrap();
        let SubmittedRealtimeFrame {
            frame,
            retained_resources,
            ..
        } = submitted;
        SubmittedRealtimeFrame {
            frame,
            completion,
            retained_resources,
        }
    }

    #[test]
    fn complete_frame_changes_only_the_unpublished_branch_until_commit() {
        let schedule = schedule(RealtimeFrameConvention::FeedbackAlignedHistory);
        let discards = Rc::new(Cell::new(0));
        let mut state = state(&schedule, discards);
        let mut branch = state.branch().unwrap();
        let mut host = Mechanisms::default();
        let mut tensors = Mechanisms::default();
        let mut executor = Executor {
            fail: false,
            calls: 0,
        };
        let mut completion = CompletionMechanism::default();

        let output = execute_realtime_frame::<TestBackend, _, _, _, _, _, _, _, _>(
            &contract(&schedule),
            &payload_contract(&schedule),
            &frame(),
            &mut branch,
            &RealtimeDecisionExecution::new(true),
            &mut host,
            &mut tensors,
            &mut executor,
            &mut completion,
            &(),
        )
        .unwrap();

        assert_eq!(output.frame().text().values, vec![3]);
        assert_eq!(output.frame().sampled_audio().values, vec![6]);
        assert_eq!(executor.calls, 1);
        assert_eq!(completion.calls, 1);
        assert_eq!(completion.retained_calls, 1);
        assert_eq!(state.schedule_state().frontier(), 0);
        assert_eq!(state.model_state().model_state().executions, 0);
        assert!(state.model_state().payload_history().is_empty());

        state.commit_branch(branch).unwrap();
        assert_eq!(state.schedule_state().frontier(), 1);
        assert_eq!(state.model_state().model_state().executions, 1);
        assert!(!state.model_state().payload_history().is_empty());
        assert_eq!(state.random_state(), Some(&1));
    }

    #[test]
    fn model_failure_requires_discard_and_never_changes_canonical_state() {
        let schedule = schedule(RealtimeFrameConvention::FeedbackAlignedHistory);
        let discards = Rc::new(Cell::new(0));
        let state = state(&schedule, discards.clone());
        let mut branch = state.branch().unwrap();
        let mut host = Mechanisms::default();
        let mut tensors = Mechanisms::default();
        let mut executor = Executor {
            fail: true,
            calls: 0,
        };
        let mut completion = CompletionMechanism::default();

        assert!(matches!(
            execute_realtime_frame::<TestBackend, _, _, _, _, _, _, _, _>(
                &contract(&schedule),
                &payload_contract(&schedule),
                &frame(),
                &mut branch,
                &RealtimeDecisionExecution::new(true),
                &mut host,
                &mut tensors,
                &mut executor,
                &mut completion,
                &(),
            ),
            Err(RealtimeFrameCoordinatorError::Model(ExecutionError))
        ));
        assert_eq!(state.schedule_state().frontier(), 0);
        assert_eq!(state.model_state().model_state().executions, 0);
        assert!(state.model_state().payload_history().is_empty());
        assert_eq!(completion.calls, 0);

        Generation::discard_branch(branch).unwrap();
        assert_eq!(discards.get(), 1);
    }

    #[test]
    fn post_submission_completion_failure_is_quarantined_until_discard_waits() {
        let schedule = schedule(RealtimeFrameConvention::FeedbackAlignedHistory);
        let discards = Rc::new(Cell::new(0));
        let state = state(&schedule, discards.clone());
        let mut branch = state.branch().unwrap();
        let mut host = Mechanisms::default();
        let mut tensors = Mechanisms::default();
        let mut executor = Executor {
            fail: false,
            calls: 0,
        };
        let waits = Rc::new(Cell::new(0));
        let mut completion = CompletionMechanism {
            fail_after_submission: true,
            waits: waits.clone(),
            ..CompletionMechanism::default()
        };

        assert!(matches!(
            execute_realtime_frame::<TestBackend, _, _, _, _, _, _, _, _>(
                &contract(&schedule),
                &payload_contract(&schedule),
                &frame(),
                &mut branch,
                &RealtimeDecisionExecution::new(true),
                &mut host,
                &mut tensors,
                &mut executor,
                &mut completion,
                &(),
            ),
            Err(RealtimeFrameCoordinatorError::CompletionAfterSubmission(
                CompletionError
            ))
        ));
        assert!(branch.has_submission_completion());
        assert_eq!(waits.get(), 0);
        assert_eq!(state.schedule_state().frontier(), 0);
        assert_eq!(state.model_state().model_state().executions, 0);

        Generation::discard_branch(branch).unwrap();
        assert_eq!(waits.get(), 1);
        assert_eq!(discards.get(), 1);
    }

    #[test]
    fn initialization_bypasses_the_nonempty_decision_plan_and_model() {
        let schedule = schedule(RealtimeFrameConvention::AbsoluteDelayedSlots);
        let discards = Rc::new(Cell::new(0));
        let state = state(&schedule, discards);
        let mut branch = state.branch().unwrap();
        let mut host = Mechanisms::default();
        let mut tensors = Mechanisms::default();
        let mut executor = Executor {
            fail: true,
            calls: 0,
        };
        let mut completion = CompletionMechanism::default();

        let output = execute_realtime_frame::<TestBackend, _, _, _, _, _, _, _, _>(
            &contract(&schedule),
            &payload_contract(&schedule),
            &frame(),
            &mut branch,
            &RealtimeDecisionExecution::new(true),
            &mut host,
            &mut tensors,
            &mut executor,
            &mut completion,
            &(),
        )
        .unwrap();

        assert_eq!(executor.calls, 0);
        assert_eq!(completion.calls, 1);
        assert_eq!(completion.retained_calls, 0);
        assert_eq!(output.frame().text().values, vec![9]);
        assert_eq!(branch.schedule_state().frontier(), 1);
    }

    #[test]
    fn branch_and_schedule_preflight_precede_every_native_mechanism() {
        let active_schedule = schedule(RealtimeFrameConvention::FeedbackAlignedHistory);
        let state = state(&active_schedule, Rc::new(Cell::new(0)));
        let mut branch = state.branch().unwrap();
        branch
            .attach_submission_completion(TestCompletion::default())
            .unwrap();
        let mut host = Mechanisms::default();
        let mut tensors = Mechanisms::default();
        let mut executor = Executor {
            fail: false,
            calls: 0,
        };
        let mut completion = CompletionMechanism::default();

        assert!(matches!(
            execute_realtime_frame::<TestBackend, _, _, _, _, _, _, _, _>(
                &contract(&active_schedule),
                &payload_contract(&active_schedule),
                &frame(),
                &mut branch,
                &RealtimeDecisionExecution::new(true),
                &mut host,
                &mut tensors,
                &mut executor,
                &mut completion,
                &(),
            ),
            Err(RealtimeFrameCoordinatorError::CompletionAttachment(
                RealtimeCompletionAttachmentError::AlreadyAttached
            ))
        ));
        assert_eq!(host.calls, 0);
        assert_eq!(tensors.calls, 0);
        assert_eq!(executor.calls, 0);
        assert_eq!(completion.calls, 0);

        let mut branch = state.branch().unwrap();
        let mismatched = schedule(RealtimeFrameConvention::AbsoluteDelayedSlots);
        assert!(matches!(
            execute_realtime_frame::<TestBackend, _, _, _, _, _, _, _, _>(
                &contract(&mismatched),
                &payload_contract(&mismatched),
                &frame(),
                &mut branch,
                &RealtimeDecisionExecution::new(true),
                &mut host,
                &mut tensors,
                &mut executor,
                &mut completion,
                &(),
            ),
            Err(RealtimeFrameCoordinatorError::Interpretation(
                RealtimeFrameInterpretationError::Schedule(_)
            ))
        ));
        assert_eq!(host.calls, 0);
        assert_eq!(tensors.calls, 0);
        assert_eq!(executor.calls, 0);
        assert_eq!(completion.calls, 0);
    }

    #[test]
    fn prepublication_observation_waits_for_exact_completion_and_is_cached_once() {
        let ready = Rc::new(Cell::new(false));
        let waits = Rc::new(Cell::new(0));
        let observer_calls = Rc::new(Cell::new(0));
        let transition = PrepublicationRealtimeFrame::new(
            submitted_test_frame(ControlledCompletion {
                ready: ready.clone(),
                fail_wait: false,
                waits: waits.clone(),
            }),
            Observer {
                calls: observer_calls.clone(),
                fail: false,
            },
        );

        assert!(!transition.is_complete().unwrap());
        assert_eq!(waits.get(), 0);
        assert_eq!(observer_calls.get(), 0);

        ready.set(true);
        assert!(transition.is_complete().unwrap());
        assert!(transition.is_complete().unwrap());
        assert_eq!(waits.get(), 1);
        assert_eq!(observer_calls.get(), 1);
        assert_eq!(transition.into_host_output().unwrap(), vec![3]);
    }

    #[test]
    fn completion_and_observation_failures_never_expose_host_output() {
        let observer_calls = Rc::new(Cell::new(0));
        let completion_failure = PrepublicationRealtimeFrame::new(
            submitted_test_frame(ControlledCompletion {
                ready: Rc::new(Cell::new(true)),
                fail_wait: true,
                waits: Rc::new(Cell::new(0)),
            }),
            Observer {
                calls: observer_calls.clone(),
                fail: false,
            },
        );
        assert!(matches!(
            completion_failure.is_complete(),
            Err(RealtimePrepublicationError::Completion(CompletionError))
        ));
        assert_eq!(observer_calls.get(), 0);
        assert!(matches!(
            completion_failure.into_host_output(),
            Err(RealtimeHostOutputUnavailable::Pending)
        ));

        let observer_calls = Rc::new(Cell::new(0));
        let observation_failure = PrepublicationRealtimeFrame::new(
            submitted_test_frame(ControlledCompletion {
                ready: Rc::new(Cell::new(true)),
                fail_wait: false,
                waits: Rc::new(Cell::new(0)),
            }),
            Observer {
                calls: observer_calls.clone(),
                fail: true,
            },
        );
        assert!(matches!(
            observation_failure.is_complete(),
            Err(RealtimePrepublicationError::Observation(_))
        ));
        assert!(matches!(
            observation_failure.is_complete(),
            Err(RealtimePrepublicationError::Observation(_))
        ));
        assert_eq!(observer_calls.get(), 1);
        assert!(matches!(
            observation_failure.into_host_output(),
            Err(RealtimeHostOutputUnavailable::Observation(_))
        ));
    }

    #[test]
    fn scheduler_commits_only_after_host_observation_succeeds() {
        let limits = SchedulerLimits::new(1, 1).unwrap();
        let request = RequestId::new(7);
        let published = Rc::new(Cell::new(0));
        let discards = Rc::new(Cell::new(0));
        let mut scheduler = Scheduler::new(limits).unwrap();
        scheduler
            .register(
                request,
                PublicationState {
                    value: 0,
                    published: published.clone(),
                    discards: discards.clone(),
                },
            )
            .unwrap();
        scheduler.enqueue(request, frame()).unwrap();
        let observer_calls = Rc::new(Cell::new(0));
        let progress = scheduler
            .run_local_turn(Instant::now(), |_, _, branch| {
                branch.value += 1;
                Ok::<_, Infallible>(PrepublicationRealtimeFrame::new(
                    submitted_test_frame(ControlledCompletion {
                        ready: Rc::new(Cell::new(true)),
                        fail_wait: false,
                        waits: Rc::new(Cell::new(0)),
                    }),
                    Observer {
                        calls: observer_calls.clone(),
                        fail: true,
                    },
                ))
            })
            .unwrap();
        assert!(progress.committed.is_empty());
        assert_eq!(progress.failed.len(), 1);
        assert_eq!(published.get(), 0);
        assert_eq!(discards.get(), 1);
        assert_eq!(observer_calls.get(), 1);

        let request = RequestId::new(8);
        let published = Rc::new(Cell::new(0));
        let discards = Rc::new(Cell::new(0));
        let mut scheduler = Scheduler::new(limits).unwrap();
        scheduler
            .register(
                request,
                PublicationState {
                    value: 0,
                    published: published.clone(),
                    discards: discards.clone(),
                },
            )
            .unwrap();
        scheduler.enqueue(request, frame()).unwrap();
        let observer_calls = Rc::new(Cell::new(0));
        let mut progress = scheduler
            .run_local_turn(Instant::now(), |_, _, branch| {
                branch.value += 1;
                Ok::<_, Infallible>(PrepublicationRealtimeFrame::new(
                    submitted_test_frame(ControlledCompletion {
                        ready: Rc::new(Cell::new(true)),
                        fail_wait: false,
                        waits: Rc::new(Cell::new(0)),
                    }),
                    Observer {
                        calls: observer_calls.clone(),
                        fail: false,
                    },
                ))
            })
            .unwrap();
        assert!(progress.failed.is_empty());
        assert_eq!(progress.committed.len(), 1);
        assert_eq!(published.get(), 1);
        assert_eq!(discards.get(), 0);
        assert_eq!(observer_calls.get(), 1);
        let (_, _, transition) = progress.committed.pop().unwrap();
        assert_eq!(transition.into_host_output().unwrap(), vec![3]);
    }
}