turnframe-core 0.1.0

Pure types and the deterministic Flow Map workflow projector for Turnframe
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
//! Type erasure and the workflow registry (spec ยง8.3).
//!
//! Domain authors implement the typed [`WorkflowDefinition`] and
//! [`WorkflowExecutor`]. The runtime talks to [`ErasedWorkflow`] and
//! [`ErasedExecutor`], where state, commands and events are `serde_json::Value`.
//! [`TypedWorkflowAdapter`] is the only place where the conversion happens:
//! it deserializes at entry, calls the typed code, and serializes at exit.
//! Because projection is pure, erased methods that need a view take the state
//! and re-project instead of trusting a caller-supplied view.

use std::fmt;
use std::sync::Arc;

use indexmap::IndexMap;
use serde::{Deserialize, Serialize};

use crate::case::{CaseRef, Versioned};
use crate::command::{CommandBatch, CommandPolicy};
use crate::error::{ErasedCallError, ErasureError, ExecutionError, StoreError};
use crate::event::{ArtifactRef, Commit, CommittedEvent, OperationalReceipt, ReceiptEvent};
use crate::flow::{
    ConfirmationSubject, DomainEnumeration, InteractionRequirement, ObligationId, PhaseOwnership,
    StartBehaviour, StartPrecondition, ViewOf, WorkflowDefinition, WorkflowExecutor,
    WorkflowNotice, WorkflowView, WritingStage,
};
use crate::ids::{AccountId, CaseId, WorkflowKey, WorkflowVersion};
use crate::interaction::InteractionSpec;
use crate::locale::Locale;
use crate::operation::{GlossaryTerm, OperationSpec};
use crate::target::ResolvedAct;

/// An obligation in erased form.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ErasedObligation {
    /// Stable identifier (canonical JSON).
    pub id: ObligationId,
    /// Canonical JSON value.
    pub value: serde_json::Value,
    /// The same thing in words, when the workflow says it.
    ///
    /// Filled at projection, the one place holding both the obligation and the
    /// definition โ€” the same seam
    /// [`ErasedWorkflowView::state`](Self::value) is filled at. See
    /// [`WorkflowDefinition::obligation_sentence`].
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sentence: Option<crate::locale::LocalizedText>,
    /// The act that answers it, when the workflow names one.
    /// See [`WorkflowDefinition::obligation_act`].
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub act: Option<super::ObligationAct>,
}

/// A [`WorkflowView`] with phase, obligations and
/// outcome as canonical JSON, plus the phase ownership the definition declared.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ErasedWorkflowView {
    /// The case and the revision it was projected at.
    pub case_ref: CaseRef,
    /// Version of the definition.
    pub workflow_version: WorkflowVersion,
    /// The phase, canonical JSON.
    pub phase: serde_json::Value,
    /// Who must act.
    pub phase_ownership: PhaseOwnership,
    /// Open obligations.
    pub obligations: Vec<ErasedObligation>,
    /// Blocking requirement, if any.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub blocking_interaction: Option<InteractionRequirement>,
    /// Notices.
    #[serde(default)]
    pub notices: Vec<WorkflowNotice>,
    /// Outcome, canonical JSON, present only when complete.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub outcome: Option<serde_json::Value>,
    /// What the case HOLDS, as the workflow says it may be stated.
    ///
    /// The counterpart of [`Self::obligations`], which say what it still needs.
    /// Filled from
    /// [`WorkflowDefinition::narratable_state`]
    /// at projection, because that is the one place holding both the state and
    /// the view. Empty for a workflow that declares none, which is the default.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub state: Vec<crate::flow::StateField>,
}

impl ErasedWorkflowView {
    /// Returns `true` when an outcome is present.
    #[must_use]
    pub fn is_complete(&self) -> bool {
        self.outcome.is_some()
    }

    /// Returns `true` when the phase is user-owned.
    #[must_use]
    pub fn is_user_owned(&self) -> bool {
        self.phase_ownership == PhaseOwnership::User
    }

    /// Identifiers of the open obligations.
    #[must_use]
    pub fn obligation_ids(&self) -> Vec<&ObligationId> {
        self.obligations.iter().map(|o| &o.id).collect()
    }
}

/// Object-safe view of a workflow definition (spec ยง8.3).
///
/// State, commands and events cross this boundary as JSON. Methods that need a
/// view take `(case_ref, state)` and re-project internally.
pub trait ErasedWorkflow: Send + Sync {
    /// Stable key.
    fn key(&self) -> WorkflowKey;

    /// Version.
    fn version(&self) -> WorkflowVersion;

    /// Projects a case.
    fn project(
        &self,
        case_ref: CaseRef,
        state: Option<&serde_json::Value>,
    ) -> Result<ErasedWorkflowView, ErasureError>;

    /// The operations offered for this case, validated and stamped with this workflow.
    fn operations(
        &self,
        case_ref: CaseRef,
        state: Option<&serde_json::Value>,
    ) -> Result<Vec<OperationSpec>, ErasureError>;
    /// One line saying what the workflow is for.
    fn summary(&self) -> Option<String>;
    /// Terms this workflow's users say, and what they mean here.
    fn glossary(&self) -> Vec<GlossaryTerm>;
    /// What one record of this workflow is called. See [`WorkflowDefinition::noun`].
    fn noun(&self) -> Option<crate::locale::LocalizedText> {
        None
    }

    /// Guidance for understanding a turn about this case, as the workflow wrote it,
    /// untruncated: the deployment's [`BriefingBudget`](crate::flow::BriefingBudget)
    /// applies where it is shown.
    fn briefing(
        &self,
        case_ref: CaseRef,
        state: Option<&serde_json::Value>,
    ) -> Result<Option<String>, ErasureError>;

    /// What must be true of another case before this workflow may be started.
    ///
    /// Needs no erasure: a precondition is plain data, so this is the typed
    /// method verbatim. See
    /// [`crate::flow::WorkflowDefinition::start_preconditions`].
    fn start_preconditions(&self) -> Vec<StartPrecondition>;

    /// What starting this workflow means when a case of it is already open.
    ///
    /// Needs no erasure either: the answer is a property of the workflow, not
    /// of any case. See
    /// [`crate::flow::WorkflowDefinition::start_behaviour`].
    fn start_behaviour(&self) -> StartBehaviour;

    /// What a confirmation the policy engine raises is about.
    ///
    /// Takes the state and projects it, the way
    /// [`Self::briefing`] does, because the typed method is a function of both.
    /// See [`crate::flow::WorkflowDefinition::confirmation_subject`].
    fn confirmation_subject(
        &self,
        case_ref: CaseRef,
        state: Option<&serde_json::Value>,
        act: &ResolvedAct,
    ) -> Result<Option<ConfirmationSubject>, ErasureError>;

    /// The documents the case `view` describes has.
    ///
    /// See [`crate::flow::WorkflowDefinition::artifacts`].
    fn artifacts(&self, view: &ErasedWorkflowView) -> Result<Vec<ArtifactRef>, ErasureError>;

    /// Whether a new case may be opened while `open` ones are.
    ///
    /// Erased from views rather than from states, because the caller is the
    /// reducer and the reducer holds views. A refusal travels as
    /// [`ErasedCallError::Rejected`], the way the other two domain decisions at
    /// this boundary do. See
    /// [`crate::flow::WorkflowDefinition::may_open_beside`].
    fn may_open_beside(&self, open: &[ErasedWorkflowView]) -> Result<(), ErasedCallError>;

    /// What this workflow wants said in the phase `view` is in.
    ///
    /// Erased from the view rather than from the state, because the composer
    /// holds views and not states: it speaks about the case as it stands after
    /// the turn committed, which is a projection somebody else already made.
    /// See [`crate::flow::WorkflowDefinition::transition_briefing`].
    fn narration_briefing(
        &self,
        stage: WritingStage,
        view: &ErasedWorkflowView,
    ) -> Result<Option<String>, ErasureError>;

    /// The complete value sets this workflow accepts, for the case in `state`.
    ///
    /// Erased the way [`Self::briefing`] is, and for the same reason: the
    /// declaration is a function of the view, and the view is a function of the
    /// state. See [`crate::flow::WorkflowDefinition::enumerations`].
    fn enumerations(
        &self,
        case_ref: CaseRef,
        state: Option<&serde_json::Value>,
    ) -> Result<Vec<DomainEnumeration>, ErasureError>;

    /// One obligation in words, erased. See
    /// [`WorkflowDefinition::obligation_sentence`].
    fn obligation_sentence(
        &self,
        obligation: &serde_json::Value,
    ) -> Result<Option<crate::locale::LocalizedText>, ErasureError>;

    /// What the user may do next once the case owes nothing, erased. See
    /// [`WorkflowDefinition::next_steps`].
    fn next_steps(
        &self,
        view: &ErasedWorkflowView,
    ) -> Result<Vec<crate::locale::LocalizedText>, ErasureError> {
        let _ = view;
        Ok(Vec::new())
    }

    /// Compiles a resolved act into erased commands.
    fn compile_act(
        &self,
        case_ref: CaseRef,
        state: Option<&serde_json::Value>,
        act: &ResolvedAct,
    ) -> Result<Vec<serde_json::Value>, ErasedCallError>;

    /// Why an act that compiled nothing changed nothing.
    ///
    /// See [`crate::flow::WorkflowDefinition::nothing_changed`]. Defaulted to
    /// `None` so an erasure written before this existed still compiles, and
    /// asked only after [`compile_act`](Self::compile_act) has already answered
    /// on the same state.
    fn nothing_changed(
        &self,
        state: Option<&serde_json::Value>,
        act: &ResolvedAct,
    ) -> Option<crate::locale::LocalizedText> {
        let _ = (state, act);
        None
    }

    /// Policy of an erased command.
    fn command_policy(
        &self,
        state: Option<&serde_json::Value>,
        command: &serde_json::Value,
    ) -> Result<CommandPolicy, ErasureError>;

    /// Validates an erased command.
    fn validate_command(
        &self,
        state: Option<&serde_json::Value>,
        command: &serde_json::Value,
    ) -> Result<(), ErasedCallError>;

    /// Renders receipts from erased ledger events.
    ///
    /// A [`ReceiptEvent::Redacted`] entry carries no payload to deserialize, so
    /// an erased event reaches the domain as an erased event rather than as a
    /// deserialization failure.
    fn receipts(
        &self,
        events: &[ReceiptEvent<serde_json::Value>],
        locale: &Locale,
    ) -> Result<Vec<OperationalReceipt>, ErasureError>;

    /// Builds the interaction spec for a requirement of the projected view.
    ///
    /// The returned spec must belong to `case_ref` and must be answerable, so
    /// neither a card bound to another case nor one nobody could answer reaches
    /// persistence.
    fn build_interaction(
        &self,
        case_ref: CaseRef,
        state: Option<&serde_json::Value>,
        requirement: &InteractionRequirement,
    ) -> Result<InteractionSpec, ErasedCallError>;
}

/// The read-only half of [`ErasedExecutor`]: loading a case, and nothing else.
///
/// This is the erased counterpart of [`CaseLoader`](crate::flow::CaseLoader),
/// and the same reasoning applies: a caller that must be unable to mutate a
/// case is handed a value that has no `execute` method rather than a value it
/// is asked not to use. [`WorkflowRegistry::read_only`] projects a whole
/// registry into definitions plus loaders for exactly this purpose.
///
/// **There is nothing to implement.** Every [`ErasedExecutor`] is an
/// `ErasedCaseLoader` through the blanket implementation below, and
/// [`CaseLoaderHandle`] turns an `Arc<dyn ErasedExecutor>` into an owned
/// loader.
#[async_trait::async_trait]
pub trait ErasedCaseLoader: Send + Sync {
    /// Loads a case for an account as JSON.
    ///
    /// # Errors
    ///
    /// [`StoreError`] when the case could not be read.
    async fn load_case(
        &self,
        account: &AccountId,
        case_id: &CaseId,
    ) -> Result<Versioned<Option<serde_json::Value>>, StoreError>;
}

/// Every erased executor loads.
#[async_trait::async_trait]
impl<E: ErasedExecutor + ?Sized> ErasedCaseLoader for E {
    async fn load_case(
        &self,
        account: &AccountId,
        case_id: &CaseId,
    ) -> Result<Versioned<Option<serde_json::Value>>, StoreError> {
        self.load(account, case_id).await
    }
}

/// The loading half of one executor, as a value of its own.
///
/// `Arc<dyn ErasedExecutor>` cannot be coerced to `Arc<dyn ErasedCaseLoader>`
/// โ€” the two are unrelated trait objects โ€” so this handle is the bridge. It
/// holds the executor and exposes exactly one method, which is what makes it
/// safe to give away: a holder can read a case and has no name for anything
/// else.
#[derive(Clone)]
pub struct CaseLoaderHandle {
    executor: Arc<dyn ErasedExecutor>,
}

impl CaseLoaderHandle {
    /// The loading half of `executor`.
    #[must_use]
    pub fn new(executor: Arc<dyn ErasedExecutor>) -> Self {
        Self { executor }
    }
}

impl fmt::Debug for CaseLoaderHandle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CaseLoaderHandle").finish_non_exhaustive()
    }
}

#[async_trait::async_trait]
impl ErasedCaseLoader for CaseLoaderHandle {
    async fn load_case(
        &self,
        account: &AccountId,
        case_id: &CaseId,
    ) -> Result<Versioned<Option<serde_json::Value>>, StoreError> {
        self.executor.load(account, case_id).await
    }
}

/// Object-safe executor working on erased state, commands and events.
#[async_trait::async_trait]
pub trait ErasedExecutor: Send + Sync {
    /// Loads a case for an account as JSON.
    async fn load(
        &self,
        account: &AccountId,
        case_id: &CaseId,
    ) -> Result<Versioned<Option<serde_json::Value>>, StoreError>;

    /// Executes an erased batch.
    async fn execute(
        &self,
        batch: CommandBatch<serde_json::Value>,
    ) -> Result<Commit<serde_json::Value, serde_json::Value>, ExecutionError>;
}

/// Wraps a typed definition and executor, converting only at the boundary.
pub struct TypedWorkflowAdapter<W, E> {
    definition: W,
    executor: E,
}

impl<W, E> TypedWorkflowAdapter<W, E> {
    /// Pairs a definition with its executor.
    #[must_use]
    pub const fn new(definition: W, executor: E) -> Self {
        Self {
            definition,
            executor,
        }
    }

    /// The typed definition.
    #[must_use]
    pub const fn definition(&self) -> &W {
        &self.definition
    }

    /// The typed executor.
    #[must_use]
    pub const fn executor(&self) -> &E {
        &self.executor
    }
}

impl<W: WorkflowDefinition, E> fmt::Debug for TypedWorkflowAdapter<W, E> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("TypedWorkflowAdapter")
            .field("workflow", &self.definition.key())
            .field("version", &self.definition.version())
            .finish_non_exhaustive()
    }
}

impl<W: WorkflowDefinition, E> TypedWorkflowAdapter<W, E> {
    fn state(&self, value: Option<&serde_json::Value>) -> Result<Option<W::State>, ErasureError> {
        value
            .map(|v| {
                W::State::deserialize(v).map_err(|_| ErasureError::StateDeserialization {
                    workflow: self.definition.key(),
                })
            })
            .transpose()
    }

    fn command(&self, value: &serde_json::Value) -> Result<W::Command, ErasureError> {
        W::Command::deserialize(value).map_err(|_| ErasureError::CommandDeserialization {
            workflow: self.definition.key(),
        })
    }

    fn serialize<T: Serialize>(&self, value: &T) -> Result<serde_json::Value, ErasureError> {
        serde_json::to_value(value).map_err(|_| ErasureError::Serialization {
            workflow: self.definition.key(),
        })
    }

    fn typed_view(&self, case_ref: CaseRef, state: Option<&W::State>) -> ViewOf<W> {
        self.definition.project(case_ref, state)
    }

    /// Reads an erased view back into the typed one that produced it.
    ///
    /// A view that does not round-trip is the workflow's own phase, obligation
    /// or outcome failing to deserialize into itself, which is the same defect
    /// the state and command boundaries report rather than paper over.
    fn typed_view_from(&self, view: &ErasedWorkflowView) -> Result<ViewOf<W>, ErasureError> {
        let mismatch = || ErasureError::StateDeserialization {
            workflow: self.definition.key(),
        };
        let phase: W::Phase = serde_json::from_value(view.phase.clone()).map_err(|_| mismatch())?;
        let mut obligations = Vec::with_capacity(view.obligations.len());
        for obligation in &view.obligations {
            obligations.push(
                serde_json::from_value::<W::Obligation>(obligation.value.clone())
                    .map_err(|_| mismatch())?,
            );
        }
        let outcome = view
            .outcome
            .clone()
            .map(serde_json::from_value::<W::Outcome>)
            .transpose()
            .map_err(|_| mismatch())?;
        Ok(WorkflowView {
            case_ref: view.case_ref.clone(),
            workflow_version: view.workflow_version.clone(),
            phase,
            obligations,
            blocking_interaction: view.blocking_interaction.clone(),
            notices: view.notices.clone(),
            outcome,
        })
    }

    /// The state an erased call re-projects must be the very case the act
    /// resolved to, at the revision it resolved at (I13, spec ยง12.2). Without
    /// this an act aimed at one trip could be compiled against another
    /// trip's state.
    fn check_same_case(&self, projected: &CaseRef, resolved: &CaseRef) -> Result<(), ErasureError> {
        if projected.same_case(resolved)
            && projected.expected_revision == resolved.expected_revision
        {
            Ok(())
        } else {
            Err(ErasureError::CaseMismatch {
                workflow: self.definition.key(),
            })
        }
    }
}

impl<W: WorkflowDefinition, E: Send + Sync> ErasedWorkflow for TypedWorkflowAdapter<W, E> {
    fn key(&self) -> WorkflowKey {
        self.definition.key()
    }

    fn version(&self) -> WorkflowVersion {
        self.definition.version()
    }

    fn project(
        &self,
        case_ref: CaseRef,
        state: Option<&serde_json::Value>,
    ) -> Result<ErasedWorkflowView, ErasureError> {
        let state = self.state(state)?;
        let view = self.typed_view(case_ref, state.as_ref());
        let ownership = self.definition.phase_ownership(&view.phase);
        let mut erased = view
            .erase(ownership)
            .map_err(|_| ErasureError::Serialization {
                workflow: self.definition.key(),
            })?;
        // Here and not on the typed view, because this is the only place that
        // holds both: the view is a projection and a projection drops the
        // values, so what a case HOLDS has to be asked of the state.
        erased.state = self.definition.narratable_state(state.as_ref());
        // The words for each obligation, from the same place and for the same
        // reason: this is the only seam holding the typed obligation and the
        // definition at once.
        for obligation in &mut erased.obligations {
            if let Ok(typed) = serde_json::from_value::<W::Obligation>(obligation.value.clone()) {
                obligation.sentence = self.definition.obligation_sentence(&typed);
                obligation.act = self.definition.obligation_act(state.as_ref(), &typed);
            }
        }
        Ok(erased)
    }

    fn operations(
        &self,
        case_ref: CaseRef,
        state: Option<&serde_json::Value>,
    ) -> Result<Vec<OperationSpec>, ErasureError> {
        let state = self.state(state)?;
        let view = self.typed_view(case_ref, state.as_ref());
        let workflow = self.definition.key();
        let mut operations = self.definition.operations(&view);
        for operation in &mut operations {
            operation.workflow = workflow.clone();
            operation
                .validate()
                .map_err(|error| ErasureError::InvalidOperation {
                    workflow: workflow.clone(),
                    reason: error.to_string(),
                })?;
        }
        Ok(operations)
    }

    fn summary(&self) -> Option<String> {
        self.definition.summary()
    }

    fn glossary(&self) -> Vec<GlossaryTerm> {
        self.definition.glossary()
    }

    fn noun(&self) -> Option<crate::locale::LocalizedText> {
        self.definition.noun()
    }

    fn briefing(
        &self,
        case_ref: CaseRef,
        state: Option<&serde_json::Value>,
    ) -> Result<Option<String>, ErasureError> {
        let state = self.state(state)?;
        let view = self.typed_view(case_ref, state.as_ref());
        Ok(self.definition.briefing(&view))
    }

    fn start_preconditions(&self) -> Vec<StartPrecondition> {
        self.definition.start_preconditions()
    }

    fn start_behaviour(&self) -> StartBehaviour {
        self.definition.start_behaviour()
    }

    fn confirmation_subject(
        &self,
        case_ref: CaseRef,
        state: Option<&serde_json::Value>,
        act: &ResolvedAct,
    ) -> Result<Option<ConfirmationSubject>, ErasureError> {
        let state = self.state(state)?;
        let view = self.typed_view(case_ref, state.as_ref());
        Ok(self
            .definition
            .confirmation_subject(state.as_ref(), &view, act))
    }

    fn artifacts(&self, view: &ErasedWorkflowView) -> Result<Vec<ArtifactRef>, ErasureError> {
        let typed = self.typed_view_from(view)?;
        Ok(self.definition.artifacts(&typed))
    }

    fn may_open_beside(&self, open: &[ErasedWorkflowView]) -> Result<(), ErasedCallError> {
        let typed: Vec<ViewOf<W>> = open
            .iter()
            .map(|view| self.typed_view_from(view))
            .collect::<Result<_, _>>()?;
        self.definition
            .may_open_beside(&typed)
            .map_err(ErasedCallError::from)
    }

    fn narration_briefing(
        &self,
        stage: WritingStage,
        view: &ErasedWorkflowView,
    ) -> Result<Option<String>, ErasureError> {
        let typed = self.typed_view_from(view)?;
        Ok(match stage {
            WritingStage::Transition => self.definition.transition_briefing(&typed),
            WritingStage::Answer => self.definition.answer_briefing(&typed),
        })
    }

    fn enumerations(
        &self,
        case_ref: CaseRef,
        state: Option<&serde_json::Value>,
    ) -> Result<Vec<DomainEnumeration>, ErasureError> {
        let state = self.state(state)?;
        let view = self.typed_view(case_ref, state.as_ref());
        Ok(self.definition.enumerations(&view))
    }

    fn next_steps(
        &self,
        view: &ErasedWorkflowView,
    ) -> Result<Vec<crate::locale::LocalizedText>, ErasureError> {
        let typed = self.typed_view_from(view)?;
        Ok(self.definition.next_steps(&typed))
    }

    fn obligation_sentence(
        &self,
        obligation: &serde_json::Value,
    ) -> Result<Option<crate::locale::LocalizedText>, ErasureError> {
        let typed: W::Obligation = serde_json::from_value(obligation.clone()).map_err(|_| {
            ErasureError::StateDeserialization {
                workflow: self.definition.key(),
            }
        })?;
        Ok(self.definition.obligation_sentence(&typed))
    }

    fn compile_act(
        &self,
        case_ref: CaseRef,
        state: Option<&serde_json::Value>,
        act: &ResolvedAct,
    ) -> Result<Vec<serde_json::Value>, ErasedCallError> {
        self.check_same_case(&case_ref, &act.case_ref)?;
        let state = self.state(state)?;
        let view = self.typed_view(case_ref, state.as_ref());
        let commands = self.definition.compile_act(state.as_ref(), &view, act)?;
        commands
            .iter()
            .map(|c| self.serialize(c).map_err(ErasedCallError::from))
            .collect()
    }

    fn nothing_changed(
        &self,
        state: Option<&serde_json::Value>,
        act: &ResolvedAct,
    ) -> Option<crate::locale::LocalizedText> {
        // A state that does not deserialize answers `None` rather than failing
        // the turn: this is asked only after `compile_act` has succeeded on the
        // same state, so it cannot happen, and the worst a wrong answer here
        // could do is leave the writing stage with the operation's name โ€” which
        // is what it had before this channel existed.
        let state = self.state(state).ok()?;
        self.definition.nothing_changed(state.as_ref(), act)
    }

    fn command_policy(
        &self,
        state: Option<&serde_json::Value>,
        command: &serde_json::Value,
    ) -> Result<CommandPolicy, ErasureError> {
        let state = self.state(state)?;
        let command = self.command(command)?;
        Ok(self.definition.command_policy(state.as_ref(), &command))
    }

    fn validate_command(
        &self,
        state: Option<&serde_json::Value>,
        command: &serde_json::Value,
    ) -> Result<(), ErasedCallError> {
        let state = self.state(state)?;
        let command = self.command(command)?;
        self.definition
            .validate_command(state.as_ref(), &command)
            .map_err(ErasedCallError::from)
    }

    fn receipts(
        &self,
        events: &[ReceiptEvent<serde_json::Value>],
        locale: &Locale,
    ) -> Result<Vec<OperationalReceipt>, ErasureError> {
        // Deserialized from the borrowed JSON: identity and timestamp are kept
        // so the domain can cite the event ids its receipts rest on (I16). An
        // erased payload has nothing to deserialize and passes through as
        // `Redacted`, which is why erasure never looks like a broken domain.
        let events = events
            .iter()
            .map(|event| {
                event.try_map_payload_ref(|payload| {
                    W::Event::deserialize(payload).map_err(|_| ErasureError::EventDeserialization {
                        workflow: self.definition.key(),
                    })
                })
            })
            .collect::<Result<Vec<_>, _>>()?;
        Ok(self.definition.receipts(&events, locale))
    }

    fn build_interaction(
        &self,
        case_ref: CaseRef,
        state: Option<&serde_json::Value>,
        requirement: &InteractionRequirement,
    ) -> Result<InteractionSpec, ErasedCallError> {
        let state = self.state(state)?;
        let view = self.typed_view(case_ref.clone(), state.as_ref());
        let spec = self
            .definition
            .build_interaction(state.as_ref(), &view, requirement)?;
        // The card belongs to the case it was projected for, at that revision:
        // a card bound elsewhere would be validated against the wrong state.
        self.check_same_case(&case_ref, &spec.case_ref)?;
        spec.validate()?;
        Ok(spec)
    }
}

#[async_trait::async_trait]
impl<W, E> ErasedExecutor for TypedWorkflowAdapter<W, E>
where
    W: WorkflowDefinition,
    E: WorkflowExecutor<W>,
{
    async fn load(
        &self,
        account: &AccountId,
        case_id: &CaseId,
    ) -> Result<Versioned<Option<serde_json::Value>>, StoreError> {
        let loaded = self.executor.load(account, case_id).await?;
        let revision = loaded.revision;
        let value = loaded
            .value
            .map(|s| serde_json::to_value(&s).map_err(|_| StoreError::Serialization))
            .transpose()?;
        Ok(Versioned::new(value, revision))
    }

    async fn execute(
        &self,
        batch: CommandBatch<serde_json::Value>,
    ) -> Result<Commit<serde_json::Value, serde_json::Value>, ExecutionError> {
        let typed = batch.try_map(|c| self.command(&c))?;
        let commit = self.executor.execute(typed).await?;
        let state = commit
            .state
            .as_ref()
            .map(|s| self.serialize(s))
            .transpose()?;
        let events = commit
            .events
            .into_iter()
            .map(|e: CommittedEvent<W::Event>| e.try_map_payload(|p| self.serialize(&p)))
            .collect::<Result<Vec<_>, _>>()?;
        Ok(Commit {
            state,
            new_revision: commit.new_revision,
            events,
            idempotency_replay: commit.idempotency_replay,
        })
    }
}

/// A registered workflow: its erased definition and executor.
#[derive(Clone)]
pub struct RegisteredWorkflow {
    /// Stable key.
    pub key: WorkflowKey,
    /// Version.
    pub version: WorkflowVersion,
    /// Erased definition.
    pub definition: Arc<dyn ErasedWorkflow>,
    /// Erased executor.
    pub executor: Arc<dyn ErasedExecutor>,
}

impl fmt::Debug for RegisteredWorkflow {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RegisteredWorkflow")
            .field("key", &self.key)
            .field("version", &self.version)
            .finish_non_exhaustive()
    }
}

impl RegisteredWorkflow {
    /// The loading half of this workflow's executor.
    #[must_use]
    pub fn loader(&self) -> CaseLoaderHandle {
        CaseLoaderHandle::new(Arc::clone(&self.executor))
    }

    /// Checks that a stored record's version matches the registered one.
    ///
    /// # Errors
    ///
    /// [`ErasureError::VersionMismatch`] when the versions differ.
    pub fn check_version(&self, found: &WorkflowVersion) -> Result<(), ErasureError> {
        if &self.version == found {
            Ok(())
        } else {
            Err(ErasureError::VersionMismatch {
                workflow: self.key.clone(),
                registered: self.version.clone(),
                found: found.clone(),
            })
        }
    }
}

/// Heterogeneous set of workflows keyed by [`WorkflowKey`] (spec ยง8.3).
#[derive(Debug, Clone, Default)]
pub struct WorkflowRegistry {
    entries: IndexMap<WorkflowKey, RegisteredWorkflow>,
}

impl WorkflowRegistry {
    /// Starts a builder.
    #[must_use]
    pub fn builder() -> WorkflowRegistryBuilder {
        WorkflowRegistryBuilder::default()
    }

    /// Looks a workflow up.
    #[must_use]
    pub fn get(&self, key: &WorkflowKey) -> Option<&RegisteredWorkflow> {
        self.entries.get(key)
    }

    /// Looks a workflow up, failing when unknown.
    pub fn require(&self, key: &WorkflowKey) -> Result<&RegisteredWorkflow, ErasureError> {
        self.entries
            .get(key)
            .ok_or_else(|| ErasureError::UnknownWorkflow {
                workflow: key.clone(),
            })
    }

    /// Checks that `found` is the registered version of `key`.
    pub fn check_version(
        &self,
        key: &WorkflowKey,
        found: &WorkflowVersion,
    ) -> Result<(), ErasureError> {
        self.require(key)?.check_version(found)
    }

    /// Returns `true` when the key is registered.
    #[must_use]
    pub fn contains(&self, key: &WorkflowKey) -> bool {
        self.entries.contains_key(key)
    }

    /// Iterates registrations in registration order.
    pub fn iter(&self) -> impl Iterator<Item = &RegisteredWorkflow> {
        self.entries.values()
    }

    /// Registered keys in registration order.
    pub fn keys(&self) -> impl Iterator<Item = &WorkflowKey> {
        self.entries.keys()
    }

    /// Number of workflows.
    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Returns `true` when empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// The definitions alone: everything pure, no executor of any kind.
    ///
    /// Projection, the act catalog, act compilation, command policy, command
    /// validation and receipts are all here, and none of them reads or writes
    /// anything. A caller handed this cannot even load a case, which is the
    /// right shape for a path that is given the state instead.
    #[must_use]
    pub fn definitions(&self) -> WorkflowDefinitions {
        WorkflowDefinitions {
            entries: self
                .entries
                .iter()
                .map(|(key, registered)| (key.clone(), Arc::clone(&registered.definition)))
                .collect(),
        }
    }

    /// The registry with the write half of every executor removed: the pure
    /// definitions plus the loading half of each executor.
    ///
    /// This is what the plan-only turn path is built on. Nothing in the
    /// returned value can execute a batch, so a planning run cannot mutate a
    /// case however it is refactored.
    #[must_use]
    pub fn read_only(&self) -> WorkflowReadRegistry {
        WorkflowReadRegistry {
            definitions: self.definitions(),
            loaders: self
                .entries
                .iter()
                .map(|(key, registered)| {
                    let loader: Arc<dyn ErasedCaseLoader> = Arc::new(registered.loader());
                    (key.clone(), loader)
                })
                .collect(),
        }
    }
}

/// The pure half of a set of workflows: definitions only.
///
/// Built by [`WorkflowRegistry::definitions`], or from erased definitions
/// directly when there is no registry โ€” a corpus replayed against a projector
/// has states in hand and no executor at all.
#[derive(Clone, Default)]
pub struct WorkflowDefinitions {
    entries: IndexMap<WorkflowKey, Arc<dyn ErasedWorkflow>>,
}

impl fmt::Debug for WorkflowDefinitions {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("WorkflowDefinitions")
            .field("keys", &self.entries.keys().collect::<Vec<_>>())
            .finish()
    }
}

impl WorkflowDefinitions {
    /// An empty set.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds one definition, keyed by [`ErasedWorkflow::key`]. A repeated key
    /// replaces the previous entry.
    #[must_use]
    pub fn with(mut self, definition: Arc<dyn ErasedWorkflow>) -> Self {
        self.entries.insert(definition.key(), definition);
        self
    }

    /// Looks a definition up.
    #[must_use]
    pub fn get(&self, key: &WorkflowKey) -> Option<&Arc<dyn ErasedWorkflow>> {
        self.entries.get(key)
    }

    /// Looks a definition up, failing when unknown.
    ///
    /// # Errors
    ///
    /// [`ErasureError::UnknownWorkflow`] when the key is not present.
    pub fn require(&self, key: &WorkflowKey) -> Result<&Arc<dyn ErasedWorkflow>, ErasureError> {
        self.entries
            .get(key)
            .ok_or_else(|| ErasureError::UnknownWorkflow {
                workflow: key.clone(),
            })
    }

    /// Returns `true` when the key is present.
    #[must_use]
    pub fn contains(&self, key: &WorkflowKey) -> bool {
        self.entries.contains_key(key)
    }

    /// Keys in insertion order.
    pub fn keys(&self) -> impl Iterator<Item = &WorkflowKey> {
        self.entries.keys()
    }

    /// Definitions in insertion order.
    pub fn iter(&self) -> impl Iterator<Item = &Arc<dyn ErasedWorkflow>> {
        self.entries.values()
    }

    /// Number of definitions.
    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Returns `true` when empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}

impl From<&WorkflowRegistry> for WorkflowDefinitions {
    fn from(registry: &WorkflowRegistry) -> Self {
        registry.definitions()
    }
}

impl From<&Arc<WorkflowRegistry>> for WorkflowDefinitions {
    fn from(registry: &Arc<WorkflowRegistry>) -> Self {
        registry.definitions()
    }
}

impl From<Arc<WorkflowRegistry>> for WorkflowDefinitions {
    fn from(registry: Arc<WorkflowRegistry>) -> Self {
        registry.definitions()
    }
}

/// The read-only projection of a [`WorkflowRegistry`]: definitions, and the
/// loading half of each executor.
///
/// A registry built from [`WorkflowDefinitions`] alone carries no loader, and
/// [`WorkflowReadRegistry::require_loader`] then says so rather than silently
/// reading nothing โ€” which is exactly what a caller that seeds the state wants,
/// because it should never reach a loader in the first place.
#[derive(Clone, Default)]
pub struct WorkflowReadRegistry {
    definitions: WorkflowDefinitions,
    loaders: IndexMap<WorkflowKey, Arc<dyn ErasedCaseLoader>>,
}

impl fmt::Debug for WorkflowReadRegistry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("WorkflowReadRegistry")
            .field("definitions", &self.definitions)
            .field("loaders", &self.loaders.len())
            .finish()
    }
}

impl WorkflowReadRegistry {
    /// Definitions with no loader at all.
    #[must_use]
    pub fn from_definitions(definitions: WorkflowDefinitions) -> Self {
        Self {
            definitions,
            loaders: IndexMap::new(),
        }
    }

    /// The definitions.
    #[must_use]
    pub const fn definitions(&self) -> &WorkflowDefinitions {
        &self.definitions
    }

    /// The loading half of one workflow's executor, when there is one.
    #[must_use]
    pub fn loader(&self, key: &WorkflowKey) -> Option<&Arc<dyn ErasedCaseLoader>> {
        self.loaders.get(key)
    }

    /// The loading half of one workflow's executor.
    ///
    /// # Errors
    ///
    /// [`ErasureError::UnknownWorkflow`] when the workflow is not registered or
    /// this projection carries no loader for it.
    pub fn require_loader(
        &self,
        key: &WorkflowKey,
    ) -> Result<&Arc<dyn ErasedCaseLoader>, ErasureError> {
        self.loaders
            .get(key)
            .ok_or_else(|| ErasureError::UnknownWorkflow {
                workflow: key.clone(),
            })
    }

    /// Returns `true` when no loader is carried at all.
    #[must_use]
    pub fn has_no_loaders(&self) -> bool {
        self.loaders.is_empty()
    }
}

impl From<&WorkflowRegistry> for WorkflowReadRegistry {
    fn from(registry: &WorkflowRegistry) -> Self {
        registry.read_only()
    }
}

/// Builds a [`WorkflowRegistry`].
#[derive(Debug, Default)]
pub struct WorkflowRegistryBuilder {
    entries: Vec<RegisteredWorkflow>,
}

impl WorkflowRegistryBuilder {
    /// Registers a typed definition with its executor.
    #[must_use]
    pub fn register<W, E>(self, definition: W, executor: E) -> Self
    where
        W: WorkflowDefinition,
        E: WorkflowExecutor<W> + 'static,
    {
        let adapter = Arc::new(TypedWorkflowAdapter::new(definition, executor));
        let erased_definition: Arc<dyn ErasedWorkflow> = adapter.clone();
        let erased_executor: Arc<dyn ErasedExecutor> = adapter;
        self.register_erased(erased_definition, erased_executor)
    }

    /// Registers an already erased pair.
    #[must_use]
    pub fn register_erased(
        mut self,
        definition: Arc<dyn ErasedWorkflow>,
        executor: Arc<dyn ErasedExecutor>,
    ) -> Self {
        self.entries.push(RegisteredWorkflow {
            key: definition.key(),
            version: definition.version(),
            definition,
            executor,
        });
        self
    }

    /// Builds the registry, failing on duplicate keys.
    pub fn build(self) -> Result<WorkflowRegistry, ErasureError> {
        let mut entries = IndexMap::with_capacity(self.entries.len());
        for entry in self.entries {
            if entries.contains_key(&entry.key) {
                return Err(ErasureError::DuplicateWorkflow {
                    workflow: entry.key,
                });
            }
            entries.insert(entry.key.clone(), entry);
        }
        Ok(WorkflowRegistry { entries })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::case::Versioned;
    use crate::flow::{PhaseOwnership, ViewOf, WorkflowDefinition, WorkflowView};
    use crate::ids::CaseRevision;
    use crate::locale::Locale;
    use crate::target::ResolvedAct;
    use serde::{Deserialize, Serialize};

    #[derive(Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
    struct Nothing;

    struct Toy;

    impl WorkflowDefinition for Toy {
        type State = Nothing;
        type Phase = Nothing;
        type Obligation = Nothing;
        type Command = Nothing;
        type Event = Nothing;
        type Outcome = Nothing;

        fn key(&self) -> WorkflowKey {
            WorkflowKey::from("toy")
        }

        fn version(&self) -> WorkflowVersion {
            WorkflowVersion::from("1")
        }

        fn phase_ownership(&self, _phase: &Self::Phase) -> PhaseOwnership {
            PhaseOwnership::System
        }

        fn project(&self, case_ref: CaseRef, _state: Option<&Self::State>) -> ViewOf<Self> {
            WorkflowView::new(case_ref, self.version(), Nothing)
        }

        fn operations(&self, _view: &ViewOf<Self>) -> Vec<crate::operation::OperationSpec> {
            Vec::new()
        }

        fn compile_act(
            &self,
            _state: Option<&Self::State>,
            _view: &ViewOf<Self>,
            _act: &ResolvedAct,
        ) -> Result<Vec<Self::Command>, crate::error::DomainRejection> {
            Ok(Vec::new())
        }

        fn command_policy(
            &self,
            _state: Option<&Self::State>,
            _command: &Self::Command,
        ) -> CommandPolicy {
            CommandPolicy::conservative()
        }

        fn validate_command(
            &self,
            _state: Option<&Self::State>,
            _command: &Self::Command,
        ) -> Result<(), crate::error::DomainRejection> {
            Ok(())
        }

        fn receipts(
            &self,
            _events: &[ReceiptEvent<Self::Event>],
            _locale: &Locale,
        ) -> Vec<OperationalReceipt> {
            Vec::new()
        }
    }

    struct Executor;

    #[async_trait::async_trait]
    impl crate::flow::WorkflowExecutor<Toy> for Executor {
        async fn load(
            &self,
            _account: &AccountId,
            _case_id: &CaseId,
        ) -> Result<Versioned<Option<Nothing>>, StoreError> {
            Ok(Versioned::new(Some(Nothing), CaseRevision(7)))
        }

        async fn execute(
            &self,
            _batch: CommandBatch<Nothing>,
        ) -> Result<Commit<Nothing, Nothing>, ExecutionError> {
            panic!("the read-only projection must never reach execution");
        }
    }

    fn registry() -> WorkflowRegistry {
        WorkflowRegistry::builder()
            .register(Toy, Executor)
            .build()
            .expect("one workflow, one key")
    }

    #[tokio::test]
    async fn the_read_only_projection_loads_and_cannot_execute() {
        let registry = registry();
        let reading = registry.read_only();
        let key = WorkflowKey::from("toy");

        assert!(reading.definitions().contains(&key));
        let loaded = reading
            .require_loader(&key)
            .expect("the projection carries a loader")
            .load_case(&AccountId::from("a"), &CaseId::from("c1"))
            .await
            .expect("the loader reads");
        assert_eq!(loaded.revision, CaseRevision(7));
        // `ErasedCaseLoader` has exactly one method, so there is nothing here
        // that could reach the panicking `execute`.
    }

    #[test]
    fn definitions_alone_carry_no_loader() {
        let definitions = registry().definitions();
        let reading = WorkflowReadRegistry::from_definitions(definitions);
        assert!(reading.has_no_loaders());
        assert!(reading.require_loader(&WorkflowKey::from("toy")).is_err());
        assert!(
            reading
                .definitions()
                .require(&WorkflowKey::from("toy"))
                .is_ok(),
            "the pure half is still there"
        );
    }

    #[test]
    fn definitions_can_be_built_without_any_executor_at_all() {
        // The executor parameter of the adapter only has to be `Send + Sync`
        // to make an `ErasedWorkflow`, so `()` is one.
        let definition: Arc<dyn ErasedWorkflow> = Arc::new(TypedWorkflowAdapter::new(Toy, ()));
        let definitions = WorkflowDefinitions::new().with(definition);
        assert_eq!(definitions.len(), 1);
        assert!(definitions.contains(&WorkflowKey::from("toy")));
    }
}