kernable-sdk 0.2.2

Write event-sourced business logic in Rust; the kernable kernel does the rest.
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
//! Domains, their event and command schemas, and aggregates.

use std::{
    collections::{HashMap, HashSet},
    panic::{AssertUnwindSafe, catch_unwind},
    sync::{Arc, Mutex, RwLock},
};

use kernable_core::{proto, signing};
use serde::{Serialize, de::DeserializeOwned};
use serde_json::{Map, Value};

use crate::{
    BoxError, Error, Listener, Rejected, RequestContext,
    context::Timestamp,
    index::{Index, KeySubject, Requirement, Unique, UniqueKey, reindex_type},
    messages::{EventType, TypeKey},
    vault::{self, VaultFields},
};

type Upcast = Arc<dyn Fn(Value) -> Value + Send + Sync>;
/// Whose personal data a payload holds: from the payload and the aggregate's ID.
pub(crate) type Subject = Arc<dyn Fn(&Value, &str) -> String + Send + Sync>;

#[derive(Clone, Default)]
pub(crate) struct EventDef {
    pub schema: u32,
    pub steps: HashMap<u32, Upcast>,
    pub vault: VaultFields,
    pub eraser: Option<Subject>,
}

struct DomainInner {
    name: String,
    events: RwLock<HashMap<String, EventDef>>,
    commands: RwLock<HashMap<String, VaultFields>>,
    /// What query types given by name are about: [`Domain::query`].
    queries: RwLock<HashMap<String, Option<QueryTarget>>>,
    /// The aggregates created through it, in order, whatever their state type.
    aggregates: RwLock<Vec<Arc<dyn Runnable>>>,
    /// The read models and reactors created through it, in order.
    listeners: RwLock<Vec<Listener>>,
    /// The business processes defined on it, in order.
    processes: RwLock<Vec<crate::process::Process>>,
    /// The message types whose schemas are declared from their type (see [`crate::Message`]).
    declared: RwLock<HashSet<String>>,
}

/// A domain: a name that scopes its aggregates, commands and events, lowercase by convention
/// (`orders`) like aggregates, while message types are capitalized (`orders.PlaceOrder`). Both
/// cases are valid; names are case-sensitive. Types are unique within a domain; elsewhere they
/// are named `domain.Type`. Cloning gives another handle to the same domain.
///
/// Make it with [`domain`]: `let shop_domain = domain("shop");`. A domain remembers the
/// aggregates, read models and reactors created through it, so that an app runs all of them
/// (see [`crate::Kernable::app`]); they live as long as the domain does.
#[derive(Clone)]
pub struct Domain {
    inner: Arc<DomainInner>,
}

/// A domain named `name`, on which you define its aggregates, read models, reactors and event
/// and command schemas: `let shop_domain = domain("shop");`.
///
/// # Panics
///
/// If `name` does not start with a letter or holds characters other than letters, digits,
/// `-` and `_`.
pub fn domain(name: &str) -> Domain {
    let valid = name.starts_with(|c: char| c.is_ascii_alphabetic())
        && name
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_');
    assert!(valid, "invalid domain name {name:?}");
    Domain {
        inner: Arc::new(DomainInner {
            name: name.to_owned(),
            events: RwLock::default(),
            commands: RwLock::default(),
            queries: RwLock::default(),
            aggregates: RwLock::default(),
            listeners: RwLock::default(),
            processes: RwLock::default(),
            declared: RwLock::default(),
        }),
    }
}

impl Domain {
    /// The domain's name, e.g. `shop`.
    pub fn name(&self) -> &str {
        &self.inner.name
    }

    /// Declares that this domain writes `event_type` events at version `schema`, with an
    /// `upcast` per older version, and which of their fields the vault keeps; see
    /// [`EventSchema`]. The domain's aggregates and listeners use it; other domains' listeners
    /// get it with [`Listener::with_domain`]. Types not declared are at version 1.
    ///
    /// # Panics
    ///
    /// If `event_type` is qualified or `schema` is 0.
    pub fn event(&self, event_type: &str, schema: u32) -> EventSchema {
        assert!(
            unqualified(event_type),
            "event types are unqualified within their domain, not {event_type:?}"
        );
        assert!(schema >= 1, "a schema is a version from 1, not {schema}");
        self.inner.events.write().unwrap().insert(
            event_type.to_owned(),
            EventDef {
                schema,
                ..EventDef::default()
            },
        );
        EventSchema {
            domain: self.clone(),
            event_type: event_type.to_owned(),
        }
    }

    /// Declares which fields of `command_type` commands the vault keeps (`personal`,
    /// `secret`); see [`CommandSchema`]. Clients that submit them need the domain:
    /// `kernable.with_domain(&domain)`.
    ///
    /// # Panics
    ///
    /// If `command_type` is qualified.
    pub fn command(&self, command_type: &str) -> CommandSchema {
        assert!(
            unqualified(command_type),
            "command types are unqualified within their domain, not {command_type:?}"
        );
        self.inner
            .commands
            .write()
            .unwrap()
            .insert(command_type.to_owned(), VaultFields::default());
        CommandSchema {
            domain: self.clone(),
            command_type: command_type.to_owned(),
        }
    }

    /// Declares what `query_type` queries are about (see [`QuerySchema::target`]): returns the
    /// [`QuerySchema`] to chain `target` on. A query type (see [`crate::query`]) declares it
    /// with `target(...)` instead. Clients that ask such queries by name need the domain:
    /// `kernable.with_domain(&domain)`.
    ///
    /// # Panics
    ///
    /// If `query_type` is qualified.
    pub fn query(&self, query_type: &str) -> QuerySchema {
        assert!(
            unqualified(query_type),
            "query types are unqualified within their domain, not {query_type:?}"
        );
        self.inner
            .queries
            .write()
            .unwrap()
            .insert(query_type.to_owned(), None);
        QuerySchema {
            domain: self.clone(),
            query_type: query_type.to_owned(),
        }
    }

    /// A business process of this domain named `name` (`fulfilment`; qualified
    /// `shop.fulfilment`): add its start and steps to the [`crate::process::Process`] it
    /// returns. An app of the domain registers it with the kernel ([`crate::Kernable::app`]).
    /// Example: `shop_domain.process("fulfilment").start::<OrderSubmitted>(Start::key("aggregate_id"))...`.
    ///
    /// # Panics
    ///
    /// If `name` is not letters, digits, `-` and `_`, or the domain has a process named `name`
    /// already.
    pub fn process(&self, name: &str) -> crate::process::Process {
        let valid = !name.is_empty()
            && name
                .chars()
                .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_');
        assert!(valid, "invalid process name {name:?}");
        let process = crate::process::Process::new(self.name(), name);
        let mut processes = self.inner.processes.write().unwrap();
        let qualified = process.name();
        assert!(
            !processes.iter().any(|p| p.name() == qualified),
            "{} has a process {name} already",
            self.inner.name
        );
        processes.push(process.clone());
        process
    }

    /// The business processes defined on this domain, in order.
    pub(crate) fn processes(&self) -> Vec<crate::process::Process> {
        self.inner.processes.read().unwrap().clone()
    }

    /// An aggregate of this domain named `name` (lowercase by convention, like `cart`), whose
    /// state starts as `initial`; see [`Aggregate`]. Example:
    /// `let cart_aggregate = shop_domain.aggregate("cart", Cart::default());`.
    ///
    /// # Panics
    ///
    /// If the domain has an aggregate named `name` already.
    pub fn aggregate<S: State>(&self, name: &str, initial: S) -> Aggregate<S> {
        let taken = self.aggregates().iter().any(|a| a.name() == name);
        assert!(
            !taken,
            "{} has an aggregate {name} already",
            self.inner.name
        );
        let aggregate = Aggregate {
            inner: Arc::new(AggregateInner {
                domain: self.clone(),
                name: name.to_owned(),
                initial,
                settings: Mutex::new((100, "1".to_owned())),
                appliers: RwLock::default(),
                handlers: RwLock::default(),
                keys: RwLock::default(),
                facts: RwLock::default(),
                requires: RwLock::default(),
            }),
        };
        self.inner
            .aggregates
            .write()
            .unwrap()
            .push(Arc::new(aggregate.clone()));
        aggregate
    }

    /// A read model of this domain named `name`: builds a view from events and answers queries
    /// from it; see [`Listener`]. Kept in memory and rebuilt from the first event on every start
    /// (order 0; [`Listener::store`] keeps it in a file instead). Run it with
    /// [`crate::Kernable::listener`] or in an app. Example:
    /// `let carts_readmodel = shop_domain.read_model("carts");`.
    ///
    /// # Panics
    ///
    /// If the domain has a read model or reactor named `name` already.
    pub fn read_model(&self, name: &str) -> Listener {
        self.remember(Listener::new(self, name, 0, Some(0), 0, false))
    }

    /// A reactor of this domain named `name`: triggers side effects and follow-up commands; see
    /// [`Listener`]. It starts at the newest event when it is new, then resumes after the last
    /// acknowledged event (order 10). A settling submit from its handler waits for the
    /// listeners before it (the read models), not for the reactor itself, which handles one
    /// event at a time. Submit follow-ups with `ctx.submit` and a fixed ID, so a redelivered
    /// event does not run the command twice; a rejected one parks the event (see
    /// [`Listener::park_after`]). Run it with [`crate::Kernable::listener`] or in an app.
    ///
    /// # Panics
    ///
    /// If the domain has a read model or reactor named `name` already.
    pub fn reactor(&self, name: &str) -> Listener {
        self.remember(Listener::new(
            self,
            name,
            10,
            None,
            crate::listener::PARK_AFTER,
            true,
        ))
    }

    fn remember(&self, listener: Listener) -> Listener {
        let taken = self.listeners().iter().any(|l| l.name() == listener.name());
        assert!(
            !taken,
            "{} has a listener {} already",
            self.inner.name,
            listener.name()
        );
        self.inner.listeners.write().unwrap().push(listener.clone());
        listener
    }

    /// The aggregates created through this domain, in order.
    pub(crate) fn aggregates(&self) -> Vec<Arc<dyn Runnable>> {
        self.inner.aggregates.read().unwrap().clone()
    }

    /// The read models and reactors created through this domain, in order.
    pub(crate) fn listeners(&self) -> Vec<Listener> {
        self.inner.listeners.read().unwrap().clone()
    }

    pub(crate) fn event_def(&self, event_type: &str) -> Option<EventDef> {
        self.inner.events.read().unwrap().get(event_type).cloned()
    }

    pub(crate) fn event_types(&self) -> Vec<String> {
        self.inner.events.read().unwrap().keys().cloned().collect()
    }

    pub(crate) fn command_vault(&self, command_type: &str) -> Option<VaultFields> {
        self.inner
            .commands
            .read()
            .unwrap()
            .get(command_type)
            .cloned()
    }

    /// The aggregate `query_type` queries are about, as declared ([`Domain::query`]).
    pub(crate) fn query_target(&self, query_type: &str) -> Option<QueryTarget> {
        self.inner
            .queries
            .read()
            .unwrap()
            .get(query_type)
            .cloned()
            .flatten()
    }

    /// Runs a message type's `declare` on this domain once, unless its schema was declared by
    /// name already ([`Domain::event`], [`Domain::command`], [`Domain::query`]).
    pub(crate) fn declare_once(&self, message_type: &str, declare: fn(&Domain)) {
        let by_name = self.event_def(message_type).is_some()
            || self.command_vault(message_type).is_some()
            || self
                .inner
                .queries
                .read()
                .unwrap()
                .contains_key(message_type);
        if self
            .inner
            .declared
            .write()
            .unwrap()
            .insert(message_type.to_owned())
            && !by_name
        {
            declare(self);
        }
    }

    /// The payload version this domain writes `event_type` events at.
    pub fn schema_of(&self, event_type: &str) -> u32 {
        self.event_def(event_type).map_or(1, |d| d.schema)
    }

    /// `data` of an `event_type` event stored at `schema`, upcast to the version this domain
    /// knows.
    pub(crate) fn upcast(
        &self,
        event_type: &str,
        schema: u32,
        data: Value,
    ) -> Result<Value, SchemaError> {
        upcast(Some(self), &self.inner.name, event_type, schema, data)
    }
}

fn unqualified(t: &str) -> bool {
    !t.is_empty() && !t.contains(['.', '*'])
}

/// The payload format of one event type: its current version and the steps that bring older
/// payloads up to it. Events are stored as written and never changed; readers upcast them.
///
/// Made with [`Domain::event`]; its methods return it, so they chain:
/// `shop_domain.event("ItemAdded", 2).upcast(1, |mut d| { d["qty"] = json!(1); d });` or
/// `shop_domain.event("CustomerRegistered", 1).personal(&["email"], |_, id| id.to_owned());`.
/// A subject function gets the payload and the aggregate's ID, e.g.
/// `|d, _| d["customerId"].as_str().unwrap_or_default().to_owned()`.
pub struct EventSchema {
    domain: Domain,
    event_type: String,
}

impl EventSchema {
    fn update(&self, f: impl FnOnce(&mut EventDef)) {
        let mut events = self.domain.inner.events.write().unwrap();
        f(events
            .get_mut(&self.event_type)
            .expect("declared by Domain::event"));
    }

    /// Keeps these top-level fields in the vault, as personal data of the subject `subject`
    /// returns from the payload and the aggregate's ID (see [`EventSchema`] for examples).
    /// Events then hold only references; the kernel hands the values to workers and to
    /// listeners allowed to read them, until an event erases the subject.
    pub fn personal(
        self,
        fields: &[&str],
        subject: impl Fn(&Value, &str) -> String + Send + Sync + 'static,
    ) -> Self {
        let fields = fields.iter().map(|f| (*f).to_owned()).collect();
        self.update(|d| d.vault.personal.push((fields, Arc::new(subject))));
        self
    }

    /// Makes this an erasing event: it deletes every vault value of the subject `subject`
    /// returns, in every stream, and the snapshots that may hold them. Read models should drop
    /// the subject's data when they see it; rebuilt, they read `null`.
    pub fn erases(self, subject: impl Fn(&Value, &str) -> String + Send + Sync + 'static) -> Self {
        self.update(|d| d.eraser = Some(Arc::new(subject)));
        self
    }

    /// Turns a payload of version `from` into one of version `from + 1`: `step` gets the old
    /// payload and returns the new one. Declare one step per older version.
    ///
    /// # Panics
    ///
    /// If `from` is not a version below the current one.
    pub fn upcast(self, from: u32, step: impl Fn(Value) -> Value + Send + Sync + 'static) -> Self {
        self.update(|d| {
            assert!(
                from >= 1 && from < d.schema,
                "upcast {} from a version between 1 and {}, not {from}",
                self.event_type,
                d.schema - 1
            );
            d.steps.insert(from, Arc::new(step));
        });
        self
    }
}

/// How a command's payload is kept: which fields go to the vault. Made with
/// [`Domain::command`]; its methods return it, so they chain:
/// `shop_domain.command("Register").personal(&["email"], |_, id| id.to_owned()).secret(&["password"]);`.
/// Clients that submit these commands need the domain: `kernable.with_domain(&shop_domain)`.
pub struct CommandSchema {
    domain: Domain,
    command_type: String,
}

impl CommandSchema {
    fn update(&self, f: impl FnOnce(&mut VaultFields)) {
        let mut commands = self.domain.inner.commands.write().unwrap();
        f(commands
            .get_mut(&self.command_type)
            .expect("declared by Domain::command"));
    }

    /// Keeps these top-level fields in the vault as personal data of `subject`; see
    /// [`EventSchema::personal`].
    pub fn personal(
        self,
        fields: &[&str],
        subject: impl Fn(&Value, &str) -> String + Send + Sync + 'static,
    ) -> Self {
        let fields = fields.iter().map(|f| (*f).to_owned()).collect();
        self.update(|v| v.personal.push((fields, Arc::new(subject))));
        self
    }

    /// Keeps these top-level fields (passwords, one-time tokens) in the vault only until the
    /// command completed. The handler gets them in clear; the stored command keeps a reference.
    pub fn secret(self, fields: &[&str]) -> Self {
        self.update(|v| v.secret.extend(fields.iter().map(|f| (*f).to_owned())));
        self
    }
}

/// What a query type is about. Made with [`Domain::query`]:
/// `shop_domain.query("GetOrder").target("order", "id");` (a `GetOrder` with `{"id": "o-17"}`
/// is about order `o-17`). Clients that ask these queries by name need the domain:
/// `kernable.with_domain(&shop_domain)`.
pub struct QuerySchema {
    domain: Domain,
    query_type: String,
}

impl QuerySchema {
    /// The query is about one aggregate of `aggregate_type` (`order`, of the query's domain, or
    /// qualified: `shop.order`), whose ID is in the data's field `field` (as on the wire:
    /// `orderId`). The SDK sends it with the query, signed, and a middleware decides on it as
    /// on a command's aggregate ([`crate::Check::target`]). Data without a non-empty string in
    /// `field` sends no target.
    pub fn target(self, aggregate_type: &str, field: &str) -> Self {
        self.domain.inner.queries.write().unwrap().insert(
            self.query_type.clone(),
            Some(QueryTarget {
                aggregate_type: aggregate_type.to_owned(),
                field: field.to_owned(),
            }),
        );
        self
    }
}

/// The aggregate a query is about, as its type declares it: [`QuerySchema::target`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct QueryTarget {
    /// `order` (of the query's domain) or qualified: `shop.order`.
    pub aggregate_type: String,
    /// The field of the query's data that holds the aggregate's ID, as on the wire: `orderId`.
    pub field: String,
}

impl QueryTarget {
    /// The wire target of a `domain` query with `data`: the type qualified, the ID from
    /// `data`; none without a non-empty string ID, so a rule that needs one refuses.
    pub(crate) fn wire(&self, domain: &str, data: &Value) -> Option<proto::Target> {
        let id = data
            .get(&self.field)?
            .as_str()
            .filter(|id| !id.is_empty())?;
        let aggregate_type = if self.aggregate_type.contains('.') {
            self.aggregate_type.clone()
        } else {
            format!("{domain}.{}", self.aggregate_type)
        };
        Some(proto::Target {
            aggregate_type,
            aggregate_id: id.to_owned(),
        })
    }
}

/// An event a process cannot read: its schema is newer than known, or a step is missing.
#[derive(Debug)]
pub(crate) struct SchemaError(pub String);

impl std::fmt::Display for SchemaError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

impl std::error::Error for SchemaError {}

/// The schema of `event_type` that `known` declares: the newest this process reads (1 if it
/// declares none). An event stored at a higher one was written by newer code.
pub(crate) fn known_schema(known: Option<&Domain>, event_type: &str) -> u32 {
    known.map_or(1, |d| d.schema_of(event_type))
}

/// `data` of a `domain.event_type` event stored at `schema`, upcast to the version `known`
/// declares (1 if it declares none).
pub(crate) fn upcast(
    known: Option<&Domain>,
    domain: &str,
    event_type: &str,
    schema: u32,
    mut data: Value,
) -> Result<Value, SchemaError> {
    let def = known.and_then(|d| d.event_def(event_type));
    let current = def.as_ref().map_or(1, |d| d.schema);
    if schema > current {
        return Err(SchemaError(format!(
            "{domain}.{event_type} has schema {schema}, but this process knows it only up to {current}: update it"
        )));
    }
    for v in schema..current {
        let step = def.as_ref().and_then(|d| d.steps.get(&v)).ok_or_else(|| {
            SchemaError(format!(
                "no upcast for {domain}.{event_type} from schema {v}"
            ))
        })?;
        data = step(data);
    }
    Ok(data)
}

/// An event returned by a command handler; `data` is stored as JSON. Make it with
/// [`NewEvent::new`]: `NewEvent::new("ItemAdded", &item)`.
#[derive(Clone, Debug, PartialEq)]
pub struct NewEvent {
    /// The event type within the aggregate's domain, e.g. `ItemAdded`.
    pub event_type: String,
    /// The payload.
    pub data: Value,
}

impl NewEvent {
    /// An `event_type` event with `data`; `()` stands for no data.
    ///
    /// # Panics
    ///
    /// If `data` cannot be represented as JSON (a map with non-string keys).
    pub fn new(event_type: &str, data: impl Serialize) -> NewEvent {
        NewEvent {
            event_type: event_type.to_owned(),
            data: serde_json::to_value(data).expect("event data is JSON"),
        }
    }
}

impl<E: EventType> From<E> for NewEvent {
    /// The event of a message type: `Ok(vec![OrderPlaced { ... }.into()])`.
    fn from(event: E) -> NewEvent {
        NewEvent::new(E::TYPE, event)
    }
}

/// What `apply` gets about an event besides its payload. Only fields known before the kernel
/// stores the event, so a snapshot and a replay compute the same state; put a time into the
/// data (`ctx.now()` in the command handler) if the state needs one.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct EventMeta {
    pub event_type: String,
    /// The aggregate's version after this event: 1 for its first event.
    pub version: u64,
    pub tenant: String,
    pub domain: String,
    pub aggregate_type: String,
    pub aggregate_id: String,
    /// The command that caused the event.
    pub command_id: String,
}

/// What `apply` gets besides state, data and the event's meta. No clock and no IDs: `apply`
/// must compute the same state every time.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ApplyContext {
    /// The request context of the command that caused the event.
    pub request: RequestContext,
}

/// What a command handler gets about the command besides its payload.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CommandMeta {
    /// The command's ID: given with [`crate::Submit::id`], else a new UUID.
    pub command_id: String,
    /// The command type within the domain.
    pub command_type: String,
    pub domain: String,
    pub aggregate_type: String,
    pub aggregate_id: String,
    pub tenant: String,
}

/// What a command handler gets besides state, data and the command's meta: the request
/// context, and the command's time and IDs.
#[derive(Clone, Debug)]
pub struct CommandContext {
    /// Who sends the command: check permissions here.
    pub request: RequestContext,
    command_id: String,
    received_at: Timestamp,
    deadline: Timestamp,
    ids: u64,
    /// For [`Aggregate::handle`]: the aggregate and tenant its `CommandMeta` names.
    target: (String, String),
}

impl CommandContext {
    /// A context for calling a command handler directly, e.g. in a unit test with
    /// [`Aggregate::handle`]; its deadline is 30 s after `received_at`.
    pub fn new(
        command_id: &str,
        request: RequestContext,
        received_at: Timestamp,
    ) -> CommandContext {
        CommandContext {
            command_id: command_id.to_owned(),
            request,
            received_at,
            deadline: Timestamp(received_at.0 + 30_000),
            ids: 0,
            target: (String::new(), String::new()),
        }
    }

    /// For [`Aggregate::handle`]: the aggregate ID and tenant the handler's [`CommandMeta`]
    /// names (default: empty), e.g. for a handler that checks them.
    pub fn on(mut self, aggregate_id: &str, tenant: &str) -> CommandContext {
        self.target = (aggregate_id.to_owned(), tenant.to_owned());
        self
    }

    /// When the kernel received the command: the same every time the command runs. Use it
    /// instead of the clock.
    pub fn now(&self) -> Timestamp {
        self.received_at
    }

    /// A new ID that is the same every time the command runs (1st call, 2nd call, ...). Use it
    /// instead of random IDs.
    pub fn id(&mut self) -> String {
        self.ids += 1;
        signing::derived_id(&self.command_id, self.ids - 1)
    }

    /// When the kernel hands the command to another worker if this one has not answered.
    pub fn deadline(&self) -> Timestamp {
        self.deadline
    }
}

/// An event to fold with [`Aggregate::fold`]: type, payload version and data, and what `apply`
/// learns about it (empty fields if unknown). `FoldEvent::new("ItemAdded", json!({ "qty": 2 }))`
/// is enough for most tests.
#[derive(Clone, Debug, Default)]
pub struct FoldEvent {
    pub event_type: String,
    pub schema: u32,
    pub data: Value,
    pub meta: EventMeta,
    /// The request context of the command that caused the event: `apply` gets it in its
    /// [`ApplyContext`].
    pub request: RequestContext,
}

impl FoldEvent {
    /// An `event_type` event at version 1 of its payload.
    pub fn new(event_type: &str, data: impl Serialize) -> FoldEvent {
        FoldEvent {
            event_type: event_type.to_owned(),
            schema: 1,
            data: serde_json::to_value(data).expect("event data is JSON"),
            meta: EventMeta::default(),
            request: RequestContext::default(),
        }
    }
}

/// What an aggregate's state must be: copied, and stored as JSON in snapshots. Every type that
/// is `Clone + Serialize + DeserializeOwned + Send + Sync + 'static` is one, typically a struct
/// with `#[derive(Clone, Default, Serialize, Deserialize)]`.
pub trait State: Clone + Serialize + DeserializeOwned + Send + Sync + 'static {}

impl<S: Clone + Serialize + DeserializeOwned + Send + Sync + 'static> State for S {}

type Apply<S> =
    Arc<dyn Fn(&mut S, Value, &EventMeta, &ApplyContext) -> Result<(), BoxError> + Send + Sync>;
type Handle<S> = Arc<
    dyn Fn(&S, Value, &CommandMeta, &mut CommandContext) -> Result<Vec<NewEvent>, Failure>
        + Send
        + Sync,
>;

type Fact<S> = Arc<dyn Fn(&S) -> bool + Send + Sync>;
type KeyValues<S> = Arc<dyn Fn(&S) -> Vec<String> + Send + Sync>;
type Requires<S> = Arc<dyn Fn(&S, Value) -> Result<Vec<Requirement>, Failure> + Send + Sync>;

/// A unique key as declared: its values in a state, its scope and whose data the values are.
struct KeyDef<S> {
    name: String,
    values: KeyValues<S>,
    global: bool,
    personal: Option<KeySubject<S>>,
}

impl<S> Clone for KeyDef<S> {
    fn clone(&self) -> Self {
        KeyDef {
            name: self.name.clone(),
            values: Arc::clone(&self.values),
            global: self.global,
            personal: self.personal.clone(),
        }
    }
}

struct AggregateInner<S> {
    domain: Domain,
    name: String,
    initial: S,
    /// Snapshot every this many events, and the state format.
    settings: Mutex<(u64, String)>,
    appliers: RwLock<HashMap<String, Apply<S>>>,
    handlers: RwLock<HashMap<String, Handle<S>>>,
    /// Unique keys and facts, in the order declared.
    keys: RwLock<Vec<KeyDef<S>>>,
    facts: RwLock<Vec<(String, Fact<S>)>>,
    /// Command type -> the facts of other aggregates its commands require.
    requires: RwLock<HashMap<String, Requires<S>>>,
}

/// An aggregate: how events change its state (`apply`) and which events a command produces
/// (`command`). Handlers must be deterministic and free of side effects; they may run again.
/// Create it with `domain(...).aggregate(...)`; cloning gives another handle to the same
/// aggregate. The registering methods return the aggregate, so they chain.
///
/// `apply` handlers change the state in place:
/// `cart_aggregate.apply("ItemAdded", |state, data: Item, _evt, _ctx| state.items += data.qty);`.
/// `command` handlers return the new events or a [`Rejected`]:
/// `cart_aggregate.command("AddItem", |state, data: Item, _cmd, _ctx| { if state.checked_out {
/// return Err(Rejected::new("Cart is checked out")); } Ok(vec![NewEvent::new("ItemAdded", data)]) });`.
/// The type of `data` (here `Item`, any `Deserialize` type, or `serde_json::Value`) says how
/// the payload is read; a payload that does not fit fails the command with `handler_error`.
/// Handlers are synchronous and must not block.
///
/// Unique keys, facts and requirements reach across aggregates; the kernel checks them
/// atomically when a command commits. `unique` declares a key computed from the state, which
/// no two aggregates hold with the same value: `customer_aggregate.unique("email", |s|
/// s.email.clone(), Unique::tenant());`. `fact` declares a condition of the state that other
/// aggregates' commands may require: `vehicle_aggregate.fact("open", |s| !s.wound_up);`.
/// `requires` names the facts a command needs:
/// `account_aggregate.requires("OpenAccount", |_state, data: Open| [Requirement::new("funds",
/// "vehicle", &data.vehicle_id, "open")]);`. After every command, the worker sends the keys and
/// facts of the new state, so an aggregate always holds exactly what its state yields.
///
/// Test without a kernel: `cart_aggregate.fold([FoldEvent::new("ItemAdded", json!({ "qty": 2 }))], None)?`
/// returns the state, `cart_aggregate.handle(&state, "AddItem", json!({ "qty": 1 }), &mut ctx)`
/// runs a command handler with a [`CommandContext::new`], and `cart_aggregate.index(&state)?`
/// and `cart_aggregate.requirements(&state, "AddItem", json!({ "qty": 1 }))?` compute the keys,
/// facts and requirements.
pub struct Aggregate<S> {
    inner: Arc<AggregateInner<S>>,
}

impl<S> Clone for Aggregate<S> {
    fn clone(&self) -> Self {
        Aggregate {
            inner: Arc::clone(&self.inner),
        }
    }
}

/// Why a handler failed: a business rejection, or anything else (`handler_error`).
#[derive(Debug)]
pub(crate) enum Failure {
    Rejected(Rejected),
    Error(String),
    /// This process cannot read the aggregate's events: not the command's fault.
    Schema(String),
}

impl Failure {
    /// As a test helper reports it.
    fn into_error(self) -> Error {
        match self {
            Failure::Rejected(r) => Error::Rejected(r),
            Failure::Error(m) | Failure::Schema(m) => Error::Config(m),
        }
    }
}

impl From<SchemaError> for Failure {
    fn from(e: SchemaError) -> Failure {
        Failure::Schema(e.0)
    }
}

impl<S: State> Aggregate<S> {
    /// The domain the aggregate belongs to.
    pub fn domain(&self) -> &Domain {
        &self.inner.domain
    }

    /// The aggregate's name within its domain, e.g. `cart`.
    pub fn name(&self) -> &str {
        &self.inner.name
    }

    /// The state before the first event.
    pub fn initial(&self) -> &S {
        &self.inner.initial
    }

    /// Sends a snapshot of the state every `events` events (default 100; 0 turns them off).
    pub fn snapshot_every(&self, events: u64) -> Self {
        self.inner.settings.lock().unwrap().0 = events;
        self.clone()
    }

    /// Version of the state format (default "1"). Change it when `apply` produces differently
    /// shaped state; older snapshots are then ignored and the state is rebuilt from the events.
    pub fn schema(&self, schema: &str) -> Self {
        self.inner.settings.lock().unwrap().1 = schema.to_owned();
        self.clone()
    }

    /// How an event of `event_type` changes the state: `f(state, data, evt, ctx)` changes
    /// `state` in place, or replaces it with `*state = …`. `data` is the event's payload as `D`,
    /// upcast to the current version; `evt` is an [`EventMeta`], `ctx` an [`ApplyContext`].
    /// `event_type` is a name, or an event type's constant (see [`crate::event`]), whose data
    /// `D` then is: `order_aggregate.apply(OrderPlaced, |s, d, _evt, _ctx| ...)`. The same holds
    /// for `command` and `requires`. See [`Aggregate`] for an example.
    pub fn apply<D: DeserializeOwned>(
        &self,
        event_type: impl TypeKey<D>,
        f: impl Fn(&mut S, D, &EventMeta, &ApplyContext) + Send + Sync + 'static,
    ) -> Self {
        let event_type = event_type.key().within(&self.inner.domain, "apply");
        let event_type = event_type.as_str();
        let apply: Apply<S> = Arc::new(move |state, data, meta, ctx| {
            let data = serde_json::from_value(data)
                .map_err(|e| format!("{} data: {e}", meta.event_type))?;
            f(state, data, meta, ctx);
            Ok(())
        });
        self.inner
            .appliers
            .write()
            .unwrap()
            .insert(event_type.to_owned(), apply);
        self.clone()
    }

    /// Which events a `command_type` command produces: `f(state, data, cmd, ctx)` returns
    /// them as `Ok(vec![NewEvent::new(…)])`, or `Err(Rejected::new(…))`. `data` is the
    /// command's payload as `D`; `cmd` is a [`CommandMeta`], `ctx` a [`CommandContext`] with
    /// the request context and deterministic `now()` and `id()`. See [`Aggregate`] for an
    /// example.
    ///
    /// # Panics
    ///
    /// If `command_type` starts with `_reindex_`: those are reserved for the built-in command
    /// of aggregates with unique keys or facts (see [`crate::reindex_type`]), and an aggregate
    /// cannot see the other aggregate types of its domain.
    pub fn command<D: DeserializeOwned>(
        &self,
        command_type: impl TypeKey<D>,
        f: impl Fn(&S, D, &CommandMeta, &mut CommandContext) -> Result<Vec<NewEvent>, Rejected>
        + Send
        + Sync
        + 'static,
    ) -> Self {
        let command_type = command_type.key().within(&self.inner.domain, "command");
        let command_type = command_type.as_str();
        assert!(
            !command_type.starts_with("_reindex_"),
            "{command_type}: command types starting with _reindex_ are reserved for the built-in \
             command of aggregates with unique keys or facts; name yours otherwise"
        );
        let name = command_type.to_owned();
        let handle: Handle<S> = Arc::new(move |state, data, cmd, ctx| {
            let data = serde_json::from_value(data)
                .map_err(|e| Failure::Error(format!("{name} data: {e}")))?;
            f(state, data, cmd, ctx).map_err(Failure::Rejected)
        });
        self.inner
            .handlers
            .write()
            .unwrap()
            .insert(command_type.to_owned(), handle);
        self.clone()
    }

    /// Declares a unique key named `name`: `values(state)` returns the values the aggregate
    /// holds in that state, as anything that iterates over `String`s (values are strings by
    /// type; format numbers yourself): `None` (holds nothing),
    /// `Some(value)` (one value) or a `Vec` (several; repeated ones count once). No two
    /// aggregates of this type hold the same value of the key at the same time; a command that
    /// would make its aggregate hold a value another one holds is rejected with `unique_taken`.
    /// The value is compared as is: normalize it in `values` (e.g. lowercase an email); an empty
    /// string is a value like any other. `options` says where it is unique (within the tenant,
    /// or [`Unique::global`]) and whether the value is personal ([`Unique::personal`]). Example:
    /// `customer_aggregate.unique("email", |s| s.email.as_ref().map(|e| e.to_lowercase()),
    /// Unique::tenant());`.
    ///
    /// The worker computes the keys from the state after every command, also one without
    /// events; `values` must be deterministic like `apply`, and one that panics fails the command
    /// with `handler_error`. An aggregate that had events before the key was declared holds
    /// none of its values until it runs a command: [`crate::Kernable::reindex`] runs one.
    ///
    /// # Panics
    ///
    /// If `name` is empty, longer than 128 bytes or holds `.` or `*`, or the aggregate declares
    /// a key named `name` already (a fact may have the same name).
    pub fn unique<I: IntoIterator<Item = String>>(
        &self,
        name: &str,
        values: impl Fn(&S) -> I + Send + Sync + 'static,
        options: Unique<S>,
    ) -> Self {
        let taken = self
            .inner
            .keys
            .read()
            .unwrap()
            .iter()
            .any(|k| k.name == name);
        self.declarable("key", name, taken);
        self.inner.keys.write().unwrap().push(KeyDef {
            name: name.to_owned(),
            values: Arc::new(move |state| values(state).into_iter().collect()),
            global: options.global,
            personal: options.personal,
        });
        self.clone()
    }

    /// Declares a fact named `name`, which the aggregate holds while `holds(state)` is true.
    /// Commands of other aggregates of the tenant may require it (see [`Aggregate::requires`]).
    /// Computed like the keys (see [`Aggregate::unique`]). Example:
    /// `vehicle_aggregate.fact("open", |s| !s.wound_up);`.
    ///
    /// # Panics
    ///
    /// If `name` is empty, longer than 128 bytes or holds `.` or `*`, or the aggregate declares
    /// a fact named `name` already (a key may have the same name).
    pub fn fact(&self, name: &str, holds: impl Fn(&S) -> bool + Send + Sync + 'static) -> Self {
        let taken = self
            .inner
            .facts
            .read()
            .unwrap()
            .iter()
            .any(|(f, _)| f == name);
        self.declarable("fact", name, taken);
        self.inner
            .facts
            .write()
            .unwrap()
            .push((name.to_owned(), Arc::new(holds)));
        self.clone()
    }

    /// Which facts of other aggregates of the tenant a `command_type` command requires:
    /// `f(state, data)` returns [`Requirement`]s (anything that iterates over them), from the
    /// state before the command and its payload as `D`. The command commits only if each
    /// named aggregate holds its fact at that moment, otherwise it is rejected with
    /// `requirement_missing`; nothing is locked in between. Example:
    /// `account_aggregate.requires("OpenAccount", |_state, data: Open| [Requirement::new("funds",
    /// "vehicle", &data.vehicle_id, "open")]);`. A payload that is not a `D`, or an `f` that
    /// panics, fails the command with `handler_error`.
    ///
    /// # Panics
    ///
    /// If the aggregate has no `command_type` handler (declare it with
    /// [`Aggregate::command`] first).
    pub fn requires<D: DeserializeOwned, I: IntoIterator<Item = Requirement>>(
        &self,
        command_type: impl TypeKey<D>,
        f: impl Fn(&S, D) -> I + Send + Sync + 'static,
    ) -> Self {
        let command_type = command_type.key().within(&self.inner.domain, "requires");
        let command_type = command_type.as_str();
        assert!(
            self.handler(command_type).is_some(),
            "{} has no command {command_type} to require facts for: declare it first",
            self.inner.name
        );
        let name = command_type.to_owned();
        let requires: Requires<S> = Arc::new(move |state, data| {
            let data = serde_json::from_value(data)
                .map_err(|e| Failure::Error(format!("{name} data: {e}")))?;
            Ok(f(state, data).into_iter().collect())
        });
        self.inner
            .requires
            .write()
            .unwrap()
            .insert(command_type.to_owned(), requires);
        self.clone()
    }

    /// Panics unless `name` can name a new key or fact (`kind`) of this aggregate; `taken`:
    /// one of that kind has the name already.
    fn declarable(&self, kind: &str, name: &str, taken: bool) {
        let aggregate = format!("{}.{}", self.inner.domain.name(), self.inner.name);
        assert!(
            unqualified(name) && name.len() <= 128,
            "{aggregate}: a {kind} name is 1 to 128 bytes without `.` or `*`, not {name:?}"
        );
        assert!(!taken, "{aggregate} declares a {kind} {name} already");
    }

    /// Whether the aggregate declares unique keys or facts.
    fn indexed(&self) -> bool {
        !self.inner.keys.read().unwrap().is_empty() || !self.inner.facts.read().unwrap().is_empty()
    }

    /// The unique keys and facts the aggregate holds in `state`, as the worker sends them after
    /// a command that leaves it in `state`; for tests without a kernel. [`Error::Config`] if a
    /// key or fact function panics.
    pub fn index(&self, state: &S) -> Result<Index, Error> {
        self.index_of(state).map_err(Failure::into_error)
    }

    /// The requirements of a `command_type` command with `data` on `state` (none if it declares
    /// none, see [`Aggregate::requires`]); for tests without a kernel. [`Error::Config`] if
    /// `data` is not the function's `D` or it panics.
    pub fn requirements(
        &self,
        state: &S,
        command_type: &str,
        data: impl Serialize,
    ) -> Result<Vec<Requirement>, Error> {
        let data = serde_json::to_value(data)?;
        self.requirements_of(state, command_type, data)
            .map_err(Failure::into_error)
    }

    fn index_of(&self, state: &S) -> Result<Index, Failure> {
        let keys = self.inner.keys.read().unwrap().clone();
        let facts = self.inner.facts.read().unwrap().clone();
        caught(|| {
            let mut index = Index::default();
            for key in &keys {
                let subject = match &key.personal {
                    None => None,
                    Some(subject) => match subject(state) {
                        Some(s) if !s.is_empty() => Some(s),
                        _ => continue,
                    },
                };
                let mut values: Vec<String> = Vec::new();
                for value in (key.values)(state) {
                    if !values.contains(&value) {
                        values.push(value);
                    }
                }
                index.keys.extend(values.into_iter().map(|value| UniqueKey {
                    name: key.name.clone(),
                    value,
                    global: key.global,
                    subject: subject.clone(),
                }));
            }
            index.facts = facts
                .iter()
                .filter(|(_, holds)| holds(state))
                .map(|(name, _)| name.clone())
                .collect();
            index
        })
    }

    fn requirements_of(
        &self,
        state: &S,
        command_type: &str,
        data: Value,
    ) -> Result<Vec<Requirement>, Failure> {
        let requires = self
            .inner
            .requires
            .read()
            .unwrap()
            .get(command_type)
            .cloned();
        match requires {
            Some(requires) => caught(|| requires(state, data))?,
            None => Ok(Vec::new()),
        }
    }

    /// State after `history`, starting from `from` (default: the initial state). Events are
    /// upcast first; events without an `apply` are skipped. For tests without a kernel.
    /// [`Error::Config`] if an `apply` fails (also a payload that is not its `D`, or a panic)
    /// or an event's schema is newer than known.
    pub fn fold(
        &self,
        history: impl IntoIterator<Item = FoldEvent>,
        from: Option<S>,
    ) -> Result<S, Error> {
        self.fold_inner(history, from).map_err(|f| match f {
            Failure::Rejected(r) => Error::Rejected(r),
            Failure::Error(m) | Failure::Schema(m) => Error::Config(m),
        })
    }

    fn fold_inner(
        &self,
        history: impl IntoIterator<Item = FoldEvent>,
        from: Option<S>,
    ) -> Result<S, Failure> {
        let mut state = from.unwrap_or_else(|| self.inner.initial.clone());
        let appliers = self.inner.appliers.read().unwrap();
        for e in history {
            let Some(apply) = appliers.get(&e.event_type) else {
                continue;
            };
            let data = self.inner.domain.upcast(&e.event_type, e.schema, e.data)?;
            let meta = EventMeta {
                event_type: e.event_type,
                domain: if e.meta.domain.is_empty() {
                    self.inner.domain.name().to_owned()
                } else {
                    e.meta.domain
                },
                aggregate_type: if e.meta.aggregate_type.is_empty() {
                    self.inner.name.clone()
                } else {
                    e.meta.aggregate_type
                },
                ..e.meta
            };
            let ctx = ApplyContext { request: e.request };
            caught(|| apply(&mut state, data, &meta, &ctx))?
                .map_err(|e| Failure::Error(e.to_string()))?;
        }
        Ok(state)
    }

    /// Runs the `command_type` handler on `state`, e.g. in a unit test. The handler's
    /// [`CommandMeta`] has the command ID of `ctx`, this aggregate's domain and name, and an empty
    /// aggregate ID and tenant. [`Error::Rejected`] if the handler rejects the command,
    /// [`Error::Config`] if there is no handler for `command_type` or it fails otherwise.
    pub fn handle(
        &self,
        state: &S,
        command_type: &str,
        data: impl Serialize,
        ctx: &mut CommandContext,
    ) -> Result<Vec<NewEvent>, Error> {
        let handler = self.handler(command_type).ok_or_else(|| {
            Error::Config(format!(
                "no handler for {command_type} on {}",
                self.inner.name
            ))
        })?;
        let data = serde_json::to_value(data)?;
        let cmd = CommandMeta {
            command_id: ctx.command_id.clone(),
            command_type: command_type.to_owned(),
            domain: self.inner.domain.name().to_owned(),
            aggregate_type: self.inner.name.clone(),
            aggregate_id: ctx.target.0.clone(),
            tenant: ctx.target.1.clone(),
        };
        match caught(|| handler(state, data, &cmd, ctx)).and_then(|r| r) {
            Ok(events) => Ok(events),
            Err(Failure::Rejected(r)) => Err(Error::Rejected(r)),
            Err(Failure::Error(m) | Failure::Schema(m)) => Err(Error::Config(m)),
        }
    }

    fn handler(&self, command_type: &str) -> Option<Handle<S>> {
        self.inner
            .handlers
            .read()
            .unwrap()
            .get(command_type)
            .cloned()
    }

    /// A rejection (`personal_in_clear`) if an event holds, in a field the event type does not
    /// declare personal, a non-null value under the name of a personal field of the command:
    /// it would be stored in the signed log in clear, for good.
    fn personal_in_clear(&self, command_type: &str, events: &[NewEvent]) -> Result<(), Failure> {
        let domain = &self.inner.domain;
        let Some(command) = domain.command_vault(command_type) else {
            return Ok(());
        };
        for e in events {
            let def = domain.event_def(&e.event_type);
            let declared = |field: &String| {
                def.as_ref()
                    .is_some_and(|d| d.vault.personal.iter().any(|(f, _)| f.contains(field)))
            };
            for field in command.personal.iter().flat_map(|(fields, _)| fields) {
                let held = e.data.get(field).is_some_and(|v| !v.is_null());
                if held && !declared(field) {
                    return Err(Failure::Rejected(
                        Rejected::new(format!(
                            "{command_type} declares {field} personal, but {event} does not: it \
                             would be stored in clear (declare it with Domain.event({event:?}, \
                             …).personal)",
                            event = e.event_type,
                        ))
                        .code("personal_in_clear"),
                    ));
                }
            }
        }
        Ok(())
    }
}

/// `f()`, with a panic turned into an error.
fn caught<T>(f: impl FnOnce() -> T) -> Result<T, Failure> {
    catch_unwind(AssertUnwindSafe(f)).map_err(|panic| Failure::Error(panic_text(panic.as_ref())))
}

/// What a panic said, as far as it can be shown.
pub(crate) fn panic_text(panic: &(dyn std::any::Any + Send)) -> String {
    panic
        .downcast_ref::<&str>()
        .map(|s| (*s).to_owned())
        .or_else(|| panic.downcast_ref::<String>().cloned())
        .unwrap_or_else(|| "handler panicked".into())
}

/// What a worker sends back for a command it executed.
pub(crate) struct Executed {
    pub events: Vec<proto::NewEvent>,
    pub vault: Vec<proto::VaultValue>,
    /// Version, state format and state, if the events cross a multiple of `snapshot_every`.
    pub snapshot: Option<(u64, String, Vec<u8>)>,
    /// The unique keys and facts of the state after the events, and the facts the command
    /// requires of other aggregates.
    pub keys: Vec<proto::UniqueKey>,
    pub facts: Vec<String>,
    pub requires: Vec<proto::Requirement>,
}

/// An aggregate as a worker runs it, whatever its state type.
pub(crate) trait Runnable: Send + Sync {
    fn domain(&self) -> &Domain;
    fn name(&self) -> &str;
    fn command_types(&self) -> Vec<String>;
    /// The event types it knows: applied ones and the domain's declared ones.
    fn event_types(&self) -> Vec<String>;
    fn execute(&self, x: &proto::Execute) -> Result<Executed, Failure>;
}

impl<S: State> Runnable for Aggregate<S> {
    fn domain(&self) -> &Domain {
        &self.inner.domain
    }

    fn name(&self) -> &str {
        &self.inner.name
    }

    fn command_types(&self) -> Vec<String> {
        let mut types: Vec<String> = self
            .inner
            .handlers
            .read()
            .unwrap()
            .keys()
            .cloned()
            .collect();
        if self.indexed() {
            types.push(reindex_type(&self.inner.name));
        }
        types
    }

    fn event_types(&self) -> Vec<String> {
        let mut types: Vec<String> = self
            .inner
            .appliers
            .read()
            .unwrap()
            .keys()
            .cloned()
            .collect();
        types.extend(self.inner.domain.event_types());
        types.sort();
        types.dedup();
        types
    }

    fn execute(&self, x: &proto::Execute) -> Result<Executed, Failure> {
        let cmd = x.command.clone().unwrap_or_default();
        let (snapshot_every, schema) = self.inner.settings.lock().unwrap().clone();
        let from = match &x.snapshot {
            Some(s) if s.schema != schema => {
                return Err(Failure::Rejected(
                    Rejected::new(format!("snapshot schema {} is not {schema}", s.schema))
                        .code("snapshot_stale"),
                ));
            }
            Some(s) => Some(
                serde_json::from_slice(&s.state)
                    .map_err(|e| Failure::Error(format!("snapshot of {}: {e}", self.inner.name)))?,
            ),
            None => None,
        };
        let history = x.history.iter().map(|e| FoldEvent {
            event_type: e.event_type.clone(),
            schema: e.schema,
            data: vault::unseal(decode(&e.payload), &x.vault).0,
            meta: meta_of(&e.event_type, e.version, &cmd, &e.command_id),
            request: RequestContext::from_wire(e.context.as_ref()),
        });
        let state = self.fold_inner(history.collect::<Vec<_>>(), from)?;
        let request = RequestContext::from_wire(cmd.context.as_ref());
        let meta = CommandMeta {
            command_id: cmd.command_id.clone(),
            command_type: cmd.command_type.clone(),
            domain: cmd.domain.clone(),
            aggregate_type: cmd.aggregate_type.clone(),
            aggregate_id: cmd.aggregate_id.clone(),
            tenant: cmd.tenant_id.clone(),
        };
        let mut ctx = CommandContext {
            command_id: cmd.command_id.clone(),
            request: request.clone(),
            received_at: Timestamp(x.received_at),
            deadline: Timestamp(x.deadline),
            ids: 0,
            target: (cmd.aggregate_id.clone(), cmd.tenant_id.clone()),
        };
        let indexed = self.indexed();
        let handler = self.handler(&cmd.command_type);
        let data = vault::unseal(decode(&cmd.payload), &x.vault).0;
        let produced = match handler {
            Some(handler) => caught(|| handler(&state, data.clone(), &meta, &mut ctx))??,
            // Produces nothing, so the result carries the keys and facts of the current state.
            None if indexed && cmd.command_type == reindex_type(&self.inner.name) => Vec::new(),
            None => {
                return Err(Failure::Rejected(
                    Rejected::new(format!(
                        "no handler for {} on {}.{}",
                        cmd.command_type, cmd.domain, cmd.aggregate_type
                    ))
                    .code("unknown_command"),
                ));
            }
        };
        self.personal_in_clear(&cmd.command_type, &produced)?;
        let requires = self.requirements_of(&state, &cmd.command_type, data)?;

        let mut events = Vec::with_capacity(produced.len());
        let mut values = Vec::new();
        for e in &produced {
            let def = self.inner.domain.event_def(&e.event_type);
            let sealed = vault::seal(&e.data, def.as_ref().map(|d| &d.vault), &cmd.aggregate_id)
                .map_err(Failure::Error)?;
            values.extend(sealed.values);
            let erases = def
                .as_ref()
                .and_then(|d| d.eraser.as_ref())
                .map(|subject| subject(&e.data, &cmd.aggregate_id))
                .unwrap_or_default();
            events.push(proto::NewEvent {
                event_type: e.event_type.clone(),
                schema: def.as_ref().map_or(1, |d| d.schema),
                payload: sealed.payload,
                vault_refs: sealed.refs,
                erases,
                origin_partition: String::new(),
                origin_position: 0,
                // The kernel sets it from the worker's deployment.
                deployment: String::new(),
            });
        }

        let before = x.expected_version;
        let version = before + produced.len() as u64;
        let snapshot_due = snapshot_every != 0
            && !produced.is_empty()
            && version / snapshot_every != before / snapshot_every;
        let after = if produced.is_empty() || !(snapshot_due || indexed) {
            state
        } else {
            // Apply the events as they will be read back (a JSON round trip), so the snapshot
            // and the keys equal those of a replay.
            let read = produced.iter().enumerate().map(|(i, e)| FoldEvent {
                event_type: e.event_type.clone(),
                schema: self.inner.domain.schema_of(&e.event_type),
                data: decode(&encode(&e.data)),
                meta: meta_of(&e.event_type, before + i as u64 + 1, &cmd, &cmd.command_id),
                request: request.clone(),
            });
            self.fold_inner(read.collect::<Vec<_>>(), Some(state))?
        };
        let snapshot = if snapshot_due {
            let state = serde_json::to_vec(&after).map_err(|e| Failure::Error(e.to_string()))?;
            Some((version, schema, state))
        } else {
            None
        };
        let index = if indexed {
            self.index_of(&after)?
        } else {
            Index::default()
        };
        Ok(Executed {
            events,
            vault: values,
            snapshot,
            keys: index.keys.iter().map(UniqueKey::to_wire).collect(),
            facts: index.facts,
            requires: requires.iter().map(|q| q.to_wire(&cmd.domain)).collect(),
        })
    }
}

/// What `apply` gets about an event of `cmd`'s aggregate, caused by command `command_id`.
fn meta_of(event_type: &str, version: u64, cmd: &proto::Command, command_id: &str) -> EventMeta {
    EventMeta {
        event_type: event_type.to_owned(),
        version,
        tenant: cmd.tenant_id.clone(),
        domain: cmd.domain.clone(),
        aggregate_type: cmd.aggregate_type.clone(),
        aggregate_id: cmd.aggregate_id.clone(),
        command_id: command_id.to_owned(),
    }
}

/// JSON bytes of `data`; no data is `{}`.
pub(crate) fn encode(data: &Value) -> Vec<u8> {
    if data.is_null() {
        return b"{}".to_vec();
    }
    serde_json::to_vec(data).expect("a JSON value encodes")
}

/// The JSON value of `payload`; an empty payload is `{}`.
pub(crate) fn decode(payload: &[u8]) -> Value {
    if payload.is_empty() {
        return Value::Object(Map::new());
    }
    serde_json::from_slice(payload).unwrap_or(Value::Null)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_panic_says_what_it_said_as_far_as_it_can() {
        let text = |f: fn()| panic_text(catch_unwind(f).unwrap_err().as_ref());
        std::panic::set_hook(Box::new(|_| {}));
        assert_eq!(text(|| panic!("plain")), "plain");
        assert_eq!(text(|| panic!("formatted {}", 7)), "formatted 7");
        assert_eq!(text(|| std::panic::panic_any(7)), "handler panicked");
        let _ = std::panic::take_hook();
        assert_eq!(SchemaError("too new".into()).to_string(), "too new");
    }
}