axond 0.3.20

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

use std::collections::BTreeMap;
use std::fmt;

use super::canonical::{Canonical, CanonicalValue};
use super::ids::{InvalidId, ProjectId, ResourceId, Slug, TenantId};
use super::record::{
    BodyError, DISPLAY_NAME_FIELD, PROJECT_ID_FIELD, Record, SCHEMA_FIELD, TENANT_ID_FIELD,
};
use super::resource::{
    ResourceBody, ResourceKind, ResourceRef, ResourceScope, ResourceVersion, ResourceVersionNumber,
};
use super::revision::DesiredState;

/// The tenant body schema this build reads and writes.
pub const TENANT_SCHEMA: &str = "axond.tenant.v1";

/// The project body schema this build reads and writes.
pub const PROJECT_SCHEMA: &str = "axond.project.v1";

/// Why a tenancy body, or the tenancy graph it belongs to, was refused.
///
/// Every arm names the resource it is about, so a refusal an operator reads
/// points at one row rather than at "the revision".
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum TenancyError {
    #[error("{reference} is a {} resource, not a {}", found.as_str(), expected.as_str())]
    Kind {
        reference: ResourceRef,
        expected: ResourceKind,
        found: ResourceKind,
    },
    #[error("{reference} is a blob body; a tenancy record is inline")]
    NotInline { reference: ResourceRef },
    #[error("{reference} is not a record")]
    NotARecord { reference: ResourceRef },
    #[error(
        "{reference} declares schema `{found}`, which this build does not read (expected `{expected}`)"
    )]
    Schema {
        reference: ResourceRef,
        expected: &'static str,
        found: String,
    },
    #[error("{reference} has no `{field}`")]
    MissingField {
        reference: ResourceRef,
        field: &'static str,
    },
    #[error("{reference} carries `{field}`, which `{schema}` does not define")]
    UnknownField {
        reference: ResourceRef,
        schema: &'static str,
        field: String,
    },
    #[error("{reference} field `{field}` is not a string")]
    FieldType {
        reference: ResourceRef,
        field: &'static str,
    },
    #[error("{reference} field `{field}` is not an id: {source}")]
    MalformedId {
        reference: ResourceRef,
        field: &'static str,
        #[source]
        source: InvalidId,
    },
    #[error("{reference} field `{field}` is not a display name: {source}")]
    MalformedDisplayName {
        reference: ResourceRef,
        field: &'static str,
        #[source]
        source: InvalidDisplayName,
    },
    #[error("{reference} carries {declared}, but its resource identity is {identity}")]
    IdentityMismatch {
        reference: ResourceRef,
        declared: String,
        identity: ResourceId,
    },
    #[error("{reference} declares owner {declared} but is scoped to {scoped:?}")]
    OwnerMismatch {
        reference: ResourceRef,
        declared: TenantId,
        scoped: Option<TenantId>,
    },
    /// A project whose owning tenant row this revision does not carry.
    ///
    /// Only a *project* is held to this: a typed project body is never written
    /// without its tenant, so its absence is a row that went missing rather than
    /// a revision published before the rule existed. Other tenant-scoped
    /// resources are not, deliberately — see [`Tenancy::of`].
    #[error("{reference} belongs to {tenant}, which this revision does not declare")]
    UnknownTenant {
        reference: ResourceRef,
        tenant: TenantId,
    },
    #[error("{reference} places {project} under {scoped}, but that project belongs to {owner}")]
    ProjectOwnerMismatch {
        reference: ResourceRef,
        project: ProjectId,
        scoped: TenantId,
        owner: TenantId,
    },
}

impl TenancyError {
    /// Whether this refusal means *this build cannot read the body*, rather than
    /// *these rows do not agree with each other*.
    ///
    /// The two call for opposite operator actions, so they must not arrive as one
    /// label. A body whose schema identifier, form, or field set is not the one
    /// this release reads — a revision published by a newer build, or a legacy
    /// row written before tenancy bodies were typed — is a *compatibility*
    /// refusal: storage is intact, and the fix is to run a build that reads it or
    /// to publish a revision this one does.
    ///
    /// Four kinds of refusal are *not* compatibility failures:
    ///
    /// - an identity or ownership contradiction: those rows were readable, this
    ///   build understands both of them, and they disagree;
    /// - a body that is not an inline record, or sits under a kind that does not
    ///   match it. Every build that has ever written a tenancy resource wrote an
    ///   inline record under its own kind, untyped ones included, so no release
    ///   writes a scalar or a blob here; a body replaced with one was rewritten
    ///   underneath the gateway, and a newer field set would still arrive as a
    ///   record and be refused by its identifier;
    /// - a project whose tenant row is absent: a typed project body is only ever
    ///   written alongside its tenant, so no release skew produces one, and the
    ///   only rows *required* to exist are ones this build itself wrote (a
    ///   manifest entry whose row is gone is [`MissingResource`], not this);
    /// - a field missing, mistyped, or unparseable *inside a body whose schema
    ///   this build reads*. Reading takes the schema identifier first, so anything
    ///   past that point declared `axond.tenant.v1` and then failed to be one, and
    ///   a schema identifier is only reused for one field set (see the module
    ///   docs). The likely cause is a body rewritten underneath the gateway, not a
    ///   release skew, so it must not be reported as intact storage. A missing
    ///   `schema` field is the legacy case and stays a compatibility refusal.
    ///
    /// The one exception is a display name, whose *rules* can tighten within one
    /// schema — this build refuses a byte-order mark an earlier one accepted — so
    /// a name this build will not take is a skew rather than damage.
    ///
    /// Either way an operator has real repair work; only the compatibility cases
    /// are told the database is fine.
    ///
    /// [`IntegrityError`](super::revision::IntegrityError) carries the
    /// distinction into hydration, and convergence reports it as its own refusal
    /// reason.
    ///
    /// [`MissingResource`]: super::revision::IntegrityError::MissingResource
    pub fn is_incompatible(&self) -> bool {
        match self {
            Self::Schema { .. } | Self::UnknownField { .. } | Self::MalformedDisplayName { .. } => {
                true
            }
            // Only the schema identifier itself: its absence is a body written
            // before tenancy had one at all.
            Self::MissingField { field, .. } | Self::FieldType { field, .. } => {
                *field == SCHEMA_FIELD
            }
            Self::Kind { .. }
            | Self::NotInline { .. }
            | Self::NotARecord { .. }
            | Self::MalformedId { .. }
            | Self::IdentityMismatch { .. }
            | Self::OwnerMismatch { .. }
            | Self::UnknownTenant { .. }
            | Self::ProjectOwnerMismatch { .. } => false,
        }
    }

    /// The resource this refusal is about.
    ///
    /// Projection reports failures per resource ([`ProjectionError::Body`]), so
    /// the mapping is here rather than repeated at each call site.
    ///
    /// [`ProjectionError::Body`]: crate::convergence::ProjectionError::Body
    pub const fn reference(&self) -> ResourceRef {
        match self {
            Self::Kind { reference, .. }
            | Self::NotInline { reference }
            | Self::NotARecord { reference }
            | Self::Schema { reference, .. }
            | Self::MissingField { reference, .. }
            | Self::UnknownField { reference, .. }
            | Self::FieldType { reference, .. }
            | Self::MalformedId { reference, .. }
            | Self::MalformedDisplayName { reference, .. }
            | Self::IdentityMismatch { reference, .. }
            | Self::OwnerMismatch { reference, .. }
            | Self::UnknownTenant { reference, .. }
            | Self::ProjectOwnerMismatch { reference, .. } => *reference,
        }
    }
}

/// Why a display name was refused.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum InvalidDisplayName {
    #[error("a display name must not be empty")]
    Empty,
    #[error("a display name of {length} characters is over the {max}-character limit")]
    TooLong { length: usize, max: usize },
    #[error("a display name may not contain the control character {codepoint:#06x}")]
    ControlCharacter { codepoint: u32 },
    #[error("a display name may not contain a byte-order mark")]
    ByteOrderMark,
    #[error("a display name may not begin or end with whitespace")]
    Untrimmed,
}

/// An operator-facing name: prose, not identity, and not a [`Slug`].
///
/// Normalized on the way in rather than at every comparison: leading and
/// trailing whitespace are refused instead of trimmed, so a name has one
/// spelling and one checksum. Everything the canonical encoder cannot represent —
/// control characters and byte-order marks alike — is refused here too, so an
/// unencodable body is a validation error at the admin edge rather than a
/// [`CanonicalError`](super::canonical::CanonicalError) at publication time.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DisplayName(String);

impl DisplayName {
    pub const MAX_LEN: usize = 128;

    pub fn parse(input: &str) -> Result<Self, InvalidDisplayName> {
        if input.is_empty() {
            return Err(InvalidDisplayName::Empty);
        }
        if input.trim() != input {
            return Err(InvalidDisplayName::Untrimmed);
        }
        let length = input.chars().count();
        if length > Self::MAX_LEN {
            return Err(InvalidDisplayName::TooLong {
                length,
                max: Self::MAX_LEN,
            });
        }
        for character in input.chars() {
            // Exactly what the canonical encoder refuses, and refused here so an
            // unencodable name fails validation instead of publication.
            if character == '\u{feff}' {
                return Err(InvalidDisplayName::ByteOrderMark);
            }
            if character.is_control() {
                return Err(InvalidDisplayName::ControlCharacter {
                    codepoint: u32::from(character),
                });
            }
        }
        Ok(Self(input.to_owned()))
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

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

/// The strict reader, with tenancy's refusals in it.
///
/// Every check [`Record`] makes is a check tenancy needs; giving it tenancy's
/// error type is all it takes to inherit them, and a second schema inherits the
/// same ones rather than reimplementing them slightly differently.
impl BodyError for TenancyError {
    fn kind(reference: ResourceRef, expected: ResourceKind, found: ResourceKind) -> Self {
        Self::Kind {
            reference,
            expected,
            found,
        }
    }

    fn not_inline(reference: ResourceRef) -> Self {
        Self::NotInline { reference }
    }

    fn not_a_record(reference: ResourceRef) -> Self {
        Self::NotARecord { reference }
    }

    fn schema(reference: ResourceRef, expected: &'static str, found: String) -> Self {
        Self::Schema {
            reference,
            expected,
            found,
        }
    }

    fn missing_field(reference: ResourceRef, field: &'static str) -> Self {
        Self::MissingField { reference, field }
    }

    fn unknown_field(reference: ResourceRef, schema: &'static str, field: String) -> Self {
        Self::UnknownField {
            reference,
            schema,
            field,
        }
    }

    fn field_type(reference: ResourceRef, field: &'static str) -> Self {
        Self::FieldType { reference, field }
    }

    fn malformed_id(reference: ResourceRef, field: &'static str, source: InvalidId) -> Self {
        Self::MalformedId {
            reference,
            field,
            source,
        }
    }

    fn malformed_display_name(
        reference: ResourceRef,
        field: &'static str,
        source: InvalidDisplayName,
    ) -> Self {
        Self::MalformedDisplayName {
            reference,
            field,
            source,
        }
    }

    fn identity_mismatch(reference: ResourceRef, declared: String, identity: ResourceId) -> Self {
        Self::IdentityMismatch {
            reference,
            declared,
            identity,
        }
    }
}

/// A deployment tenant: the durable isolation boundary every other resource
/// hangs off.
///
/// Deployment-scoped by [`ResourceKind::permits`], because the tenant *is* the
/// boundary: a tenant living inside a tenant would be a hierarchy this model
/// does not have.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TenantBody {
    tenant: TenantId,
    display_name: DisplayName,
}

impl TenantBody {
    /// The schema identifier this body encodes and reads.
    pub const SCHEMA: &'static str = TENANT_SCHEMA;

    const KNOWN_FIELDS: &'static [&'static str] = &[TENANT_ID_FIELD, DISPLAY_NAME_FIELD];

    pub const fn new(tenant: TenantId, display_name: DisplayName) -> Self {
        Self {
            tenant,
            display_name,
        }
    }

    pub const fn tenant(&self) -> TenantId {
        self.tenant
    }

    pub const fn display_name(&self) -> &DisplayName {
        &self.display_name
    }

    /// The resource identity a tenant's versions are written under.
    ///
    /// A tenant has one identity, not an id plus a separate row id.
    pub const fn resource_id(&self) -> ResourceId {
        ResourceId::new(self.tenant.uuid())
    }

    /// This body as a resource body.
    pub fn body(&self) -> ResourceBody {
        ResourceBody::Inline(self.canonical())
    }

    /// The first version of this tenant, named `slug`.
    pub fn version(&self, slug: Slug) -> ResourceVersion {
        self.version_at(slug, ResourceVersionNumber::FIRST)
    }

    /// A specific version of this tenant, for a rename or a body change.
    pub fn version_at(&self, slug: Slug, version: ResourceVersionNumber) -> ResourceVersion {
        ResourceVersion::new(
            ResourceRef::new(ResourceKind::Tenant, self.resource_id(), version),
            ResourceScope::Deployment,
            slug,
            self.body(),
        )
    }

    /// Read a tenant resource's body, binding it to its envelope.
    pub fn read(resource: &ResourceVersion) -> Result<Self, TenancyError> {
        let record = Record::<TenancyError>::open(
            resource,
            ResourceKind::Tenant,
            Self::SCHEMA,
            Self::KNOWN_FIELDS,
        )?;
        let tenant = record.tenant()?;
        record.identity(tenant, ResourceId::new(tenant.uuid()))?;
        Ok(Self {
            tenant,
            display_name: record.display_name()?,
        })
    }
}

impl Canonical for TenantBody {
    fn canonical(&self) -> CanonicalValue {
        CanonicalValue::map([
            (SCHEMA_FIELD, CanonicalValue::string(Self::SCHEMA)),
            (
                TENANT_ID_FIELD,
                CanonicalValue::string(self.tenant.to_string()),
            ),
            (
                DISPLAY_NAME_FIELD,
                CanonicalValue::string(self.display_name.as_str()),
            ),
        ])
    }
}

/// A tenant-owned project: the routing and accounting boundary a request is
/// served under.
///
/// A project is what the running gateway calls a *namespace* (ADR 0003): keys
/// bind to it, credentials are pooled per `(namespace, provider)`, budgets are
/// charged against it, and rate limits are held per tenant of it. This slice does
/// not change that boundary — it gives it a durable identity and an owner.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProjectBody {
    project: ProjectId,
    tenant: TenantId,
    display_name: DisplayName,
}

impl ProjectBody {
    /// The schema identifier this body encodes and reads.
    pub const SCHEMA: &'static str = PROJECT_SCHEMA;

    const KNOWN_FIELDS: &'static [&'static str] =
        &[PROJECT_ID_FIELD, TENANT_ID_FIELD, DISPLAY_NAME_FIELD];

    pub const fn new(project: ProjectId, tenant: TenantId, display_name: DisplayName) -> Self {
        Self {
            project,
            tenant,
            display_name,
        }
    }

    pub const fn project(&self) -> ProjectId {
        self.project
    }

    /// The tenant that owns this project. Ownership is durable: a project is
    /// never moved between tenants, because everything charged and authorized
    /// under it was charged and authorized under that tenant.
    pub const fn tenant(&self) -> TenantId {
        self.tenant
    }

    pub const fn display_name(&self) -> &DisplayName {
        &self.display_name
    }

    pub const fn resource_id(&self) -> ResourceId {
        ResourceId::new(self.project.uuid())
    }

    pub fn body(&self) -> ResourceBody {
        ResourceBody::Inline(self.canonical())
    }

    /// The scope a project's versions live at: its owning tenant, and only ever
    /// that one.
    pub const fn scope(&self) -> ResourceScope {
        ResourceScope::Tenant(self.tenant)
    }

    /// The scope a resource *inside* this project lives at.
    pub const fn child_scope(&self) -> ResourceScope {
        ResourceScope::Project {
            tenant: self.tenant,
            project: self.project,
        }
    }

    pub fn version(&self, slug: Slug) -> ResourceVersion {
        self.version_at(slug, ResourceVersionNumber::FIRST)
    }

    pub fn version_at(&self, slug: Slug, version: ResourceVersionNumber) -> ResourceVersion {
        ResourceVersion::new(
            ResourceRef::new(ResourceKind::Project, self.resource_id(), version),
            self.scope(),
            slug,
            self.body(),
        )
    }

    /// Read a project resource's body, binding it to its envelope: identity to
    /// the reference, ownership to the scope.
    pub fn read(resource: &ResourceVersion) -> Result<Self, TenancyError> {
        let record = Record::<TenancyError>::open(
            resource,
            ResourceKind::Project,
            Self::SCHEMA,
            Self::KNOWN_FIELDS,
        )?;
        let project = record.project()?;
        record.identity(project, ResourceId::new(project.uuid()))?;
        let tenant = record.tenant()?;
        if resource.scope != ResourceScope::Tenant(tenant) {
            return Err(TenancyError::OwnerMismatch {
                reference: resource.reference,
                declared: tenant,
                scoped: resource.scope.tenant(),
            });
        }
        Ok(Self {
            project,
            tenant,
            display_name: record.display_name()?,
        })
    }
}

impl Canonical for ProjectBody {
    fn canonical(&self) -> CanonicalValue {
        CanonicalValue::map([
            (SCHEMA_FIELD, CanonicalValue::string(Self::SCHEMA)),
            (
                PROJECT_ID_FIELD,
                CanonicalValue::string(self.project.to_string()),
            ),
            (
                TENANT_ID_FIELD,
                CanonicalValue::string(self.tenant.to_string()),
            ),
            (
                DISPLAY_NAME_FIELD,
                CanonicalValue::string(self.display_name.as_str()),
            ),
        ])
    }
}

/// A tenant as a revision holds it: its envelope, its name, and its body.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Tenant {
    pub reference: ResourceRef,
    pub slug: Slug,
    pub body: TenantBody,
}

/// A project as a revision holds it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Project {
    pub reference: ResourceRef,
    pub slug: Slug,
    pub body: ProjectBody,
}

/// The tenancy graph of one revision, resolved once.
///
/// Built by [`Tenancy::of`], which is the single place tenancy bodies are
/// interpreted: publication, hydration, and projection all reach the same
/// conclusions because they all call it. Ordering is by id throughout, so two
/// replicas iterate the same tenants and projects in the same order.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Tenancy {
    tenants: BTreeMap<TenantId, Tenant>,
    projects: BTreeMap<ProjectId, Project>,
}

impl Tenancy {
    /// Read and resolve the tenancy of a desired state.
    ///
    /// Three things are checked that no envelope-level rule can see:
    ///
    /// 1. every tenancy body is a body of a schema this build reads, bound to its
    ///    own envelope (identity, and a project's owner);
    /// 2. a project's owning tenant is declared by the same revision — a revision
    ///    is whole desired state, so an owner that is merely assumed to exist is
    ///    a dangling owner;
    /// 3. a project-scoped resource whose project this revision *does* declare
    ///    names that project's actual owner, so a project cannot be read under a
    ///    tenant that does not own it.
    ///
    /// What is deliberately *not* checked is the tenant of every other
    /// tenant-scoped resource. Requiring it would read well — "a revision is whole
    /// desired state" — but it is a rule this build would be adding to revisions
    /// already published under the older one, where a credential or an alias could
    /// legitimately carry a tenant no row described. Hydration runs these same
    /// rules, so such a revision would stop loading on upgrade. The tenancy view
    /// is the wrong place to buy that: nothing here needs the tenant of a
    /// credential, and a scope naming a tenant that does not exist is unroutable
    /// at the boundary that routes, not unreadable. A project is held to its owner
    /// because this build never writes one without it, and because the projection
    /// cannot name a namespace without it.
    pub fn of(state: &DesiredState) -> Result<Self, TenancyError> {
        let mut tenancy = Self::default();
        for resource in state.resources() {
            match resource.reference.kind {
                ResourceKind::Tenant => {
                    let body = TenantBody::read(resource)?;
                    tenancy.tenants.insert(
                        body.tenant(),
                        Tenant {
                            reference: resource.reference,
                            slug: resource.slug.clone(),
                            body,
                        },
                    );
                }
                ResourceKind::Project => {
                    let body = ProjectBody::read(resource)?;
                    tenancy.projects.insert(
                        body.project(),
                        Project {
                            reference: resource.reference,
                            slug: resource.slug.clone(),
                            body,
                        },
                    );
                }
                _ => {}
            }
        }

        for project in tenancy.projects.values() {
            if !tenancy.tenants.contains_key(&project.body.tenant()) {
                return Err(TenancyError::UnknownTenant {
                    reference: project.reference,
                    tenant: project.body.tenant(),
                });
            }
        }

        for resource in state.resources() {
            let ResourceScope::Project { tenant, project } = &resource.scope else {
                continue;
            };
            // A project this revision does not declare is not contradicted by
            // anything, so there is nothing to refuse: what the scope names is
            // then simply unroutable, and the boundary that routes says so.
            let Some(owner) = tenancy
                .projects
                .get(project)
                .map(|project| project.body.tenant())
            else {
                continue;
            };
            if owner != *tenant {
                return Err(TenancyError::ProjectOwnerMismatch {
                    reference: resource.reference,
                    project: *project,
                    scoped: *tenant,
                    owner,
                });
            }
        }
        Ok(tenancy)
    }

    /// Every tenant, ordered by [`TenantId`].
    pub fn tenants(&self) -> impl ExactSizeIterator<Item = &Tenant> {
        self.tenants.values()
    }

    /// Every project, ordered by [`ProjectId`].
    pub fn projects(&self) -> impl ExactSizeIterator<Item = &Project> {
        self.projects.values()
    }

    pub fn tenant(&self, id: TenantId) -> Option<&Tenant> {
        self.tenants.get(&id)
    }

    pub fn project(&self, id: ProjectId) -> Option<&Project> {
        self.projects.get(&id)
    }

    /// One tenant's projects, ordered by [`ProjectId`].
    pub fn projects_of(&self, tenant: TenantId) -> impl Iterator<Item = &Project> {
        self.projects
            .values()
            .filter(move |project| project.body.tenant() == tenant)
    }

    /// The tenant-qualified name of a project, or `None` if the project is not
    /// in this view.
    ///
    /// A project slug is unique within its tenant and *only* within it, so this
    /// is the qualified form a global, flat runtime namespace identifier must be
    /// derived from. The separator is `/`, which a [`Slug`] can never contain, so
    /// the qualified form is unambiguous and reversible.
    pub fn qualified_name(&self, project: ProjectId) -> Option<QualifiedProject> {
        let project = self.projects.get(&project)?;
        let tenant = self.tenants.get(&project.body.tenant())?;
        Some(QualifiedProject {
            tenant: tenant.slug.clone(),
            project: project.slug.clone(),
        })
    }
}

/// A project named the way a deployment-wide identifier has to name it: tenant
/// first.
///
/// Two tenants may both have a project called `core`; nothing that is global to a
/// deployment may treat those as one name. This is the type that stops a
/// tenant-unique slug from being flattened into a global string by accident.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct QualifiedProject {
    pub tenant: Slug,
    pub project: Slug,
}

impl QualifiedProject {
    /// The separator between the two slugs: not a legal [`Slug`] character, so
    /// `acme/core` decomposes exactly one way.
    pub const SEPARATOR: char = '/';

    /// Split a qualified name back into its slugs.
    pub fn parse(input: &str) -> Option<Self> {
        let (tenant, project) = input.split_once(Self::SEPARATOR)?;
        Some(Self {
            tenant: Slug::parse(tenant).ok()?,
            project: Slug::parse(project).ok()?,
        })
    }
}

impl fmt::Display for QualifiedProject {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}{}{}", self.tenant, Self::SEPARATOR, self.project)
    }
}

#[cfg(test)]
mod tests {
    use super::super::canonical::{Canonical as _, SerializerVersion};
    use super::super::fixtures::{
        alias, candidate, display_name, project, project_body, project_credential, project_id,
        reference, resource_id, state, tenant, tenant_body, tenant_id,
    };
    use super::super::mutation::ExpectedRevision;
    use super::super::revision::{
        BodySkew, IntegrityError, LoadedRevision, RevisionManifest, ValidationError,
    };
    use super::*;
    use std::time::SystemTime;

    fn tenant_resource() -> ResourceVersion {
        tenant(1, "acme")
    }

    fn project_resource() -> ResourceVersion {
        project(&tenant_id(1), 2, "core")
    }

    /// Rewrite a resource's inline record, which is how a body a caller could
    /// never author — or a newer build's body — is put in front of the reader.
    fn with_fields(
        resource: &ResourceVersion,
        edit: impl FnOnce(&mut Vec<(String, CanonicalValue)>),
    ) -> ResourceVersion {
        let ResourceBody::Inline(CanonicalValue::Map(fields)) = &resource.body else {
            panic!("a tenancy fixture body is an inline record");
        };
        let mut fields = fields.clone();
        edit(&mut fields);
        ResourceVersion {
            body: ResourceBody::Inline(CanonicalValue::Map(fields)),
            ..resource.clone()
        }
    }

    fn set(fields: &mut Vec<(String, CanonicalValue)>, field: &str, value: CanonicalValue) {
        fields.retain(|(name, _)| name != field);
        fields.push((field.to_owned(), value));
    }

    #[test]
    fn a_body_round_trips_through_its_envelope_and_its_canonical_bytes() {
        let body = tenant_body(1, "Acme");
        let resource = tenant_resource();
        assert_eq!(TenantBody::read(&resource).unwrap(), body);
        assert_eq!(resource.reference.id, resource_id(1));
        assert_eq!(resource.slug.as_str(), "acme");

        let project = project_body(2, 1, "Core");
        let resource = project_resource();
        assert_eq!(ProjectBody::read(&resource).unwrap(), project);
        assert_eq!(project.tenant(), body.tenant());
        assert_eq!(project.child_scope().tenant(), Some(body.tenant()));

        // The bytes are the identity of the content, so the same body built twice
        // is the same checksum, and the schema is inside them.
        assert_eq!(
            body.checksum().unwrap(),
            tenant_body(1, "Acme").checksum().unwrap()
        );
        assert_ne!(
            body.checksum().unwrap(),
            tenant_body(1, "Globex").checksum().unwrap()
        );
        let bytes = SerializerVersion::V1.encode(&project.canonical()).unwrap();
        let decoded = SerializerVersion::V1
            .decode(&bytes)
            .expect("a tenancy body is canonical, so storage returns what it took");
        assert_eq!(
            SerializerVersion::V1.encode(&decoded).unwrap(),
            bytes,
            "the decoded body re-encodes to the bytes storage holds"
        );
        assert_eq!(
            ProjectBody::read(&ResourceVersion {
                body: ResourceBody::Inline(decoded),
                ..resource
            })
            .unwrap(),
            project,
            "and reads back as the same body"
        );
        assert!(
            String::from_utf8_lossy(&bytes).contains(PROJECT_SCHEMA),
            "the schema identifier is part of the checksummed body"
        );
    }

    #[test]
    fn a_schema_this_build_does_not_read_is_refused_rather_than_guessed_at() {
        let newer = with_fields(&tenant_resource(), |fields| {
            set(fields, "schema", CanonicalValue::string("axond.tenant.v2"));
        });
        assert_eq!(
            TenantBody::read(&newer),
            Err(TenancyError::Schema {
                reference: newer.reference,
                expected: TENANT_SCHEMA,
                found: "axond.tenant.v2".to_owned()
            })
        );

        // A field a newer schema added is a refusal too: reading the fields this
        // build knows and dropping the rest would serve half a revision.
        let extended = with_fields(&project_resource(), |fields| {
            set(fields, "residency", CanonicalValue::string("eu"));
        });
        assert_eq!(
            ProjectBody::read(&extended),
            Err(TenancyError::UnknownField {
                reference: extended.reference,
                schema: PROJECT_SCHEMA,
                field: "residency".to_owned()
            })
        );
    }

    #[test]
    fn a_malformed_body_is_a_typed_refusal_for_every_way_it_can_be_malformed() {
        let resource = tenant_resource();
        let missing = with_fields(&resource, |fields| {
            fields.retain(|(name, _)| name != DISPLAY_NAME_FIELD);
        });
        assert_eq!(
            TenantBody::read(&missing),
            Err(TenancyError::MissingField {
                reference: resource.reference,
                field: DISPLAY_NAME_FIELD
            })
        );

        let wrong_type = with_fields(&resource, |fields| {
            set(fields, TENANT_ID_FIELD, CanonicalValue::integer(7));
        });
        assert_eq!(
            TenantBody::read(&wrong_type),
            Err(TenancyError::FieldType {
                reference: resource.reference,
                field: TENANT_ID_FIELD
            })
        );

        // A project id where a tenant id belongs: the text form is typed, so this
        // is a parse error and not a lookup under the wrong table.
        let mistyped = with_fields(&resource, |fields| {
            set(
                fields,
                TENANT_ID_FIELD,
                CanonicalValue::string(project_id(1).to_string()),
            );
        });
        assert!(matches!(
            TenantBody::read(&mistyped),
            Err(TenancyError::MalformedId {
                field: TENANT_ID_FIELD,
                ..
            })
        ));

        let untrimmed = with_fields(&resource, |fields| {
            set(fields, DISPLAY_NAME_FIELD, CanonicalValue::string(" Acme"));
        });
        assert!(matches!(
            TenantBody::read(&untrimmed),
            Err(TenancyError::MalformedDisplayName {
                source: InvalidDisplayName::Untrimmed,
                ..
            })
        ));

        // The envelope's kind and body form are part of what a body must be.
        let as_project = ResourceVersion {
            reference: reference(ResourceKind::Project, 1),
            ..resource.clone()
        };
        assert!(matches!(
            TenantBody::read(&as_project),
            Err(TenancyError::Kind {
                expected: ResourceKind::Tenant,
                found: ResourceKind::Project,
                ..
            })
        ));
        let not_a_record = ResourceVersion {
            body: ResourceBody::Inline(CanonicalValue::string("acme")),
            ..resource.clone()
        };
        assert_eq!(
            TenantBody::read(&not_a_record),
            Err(TenancyError::NotARecord {
                reference: resource.reference
            })
        );
    }

    #[test]
    fn a_body_that_claims_another_identity_than_its_row_is_refused() {
        // The row for tenant 1 carrying tenant 9's body: two identities for one
        // durable object, which is what binding the body to the reference stops.
        let mismatched = with_fields(&tenant_resource(), |fields| {
            set(
                fields,
                TENANT_ID_FIELD,
                CanonicalValue::string(tenant_id(9).to_string()),
            );
        });
        assert_eq!(
            TenantBody::read(&mismatched),
            Err(TenancyError::IdentityMismatch {
                reference: mismatched.reference,
                declared: tenant_id(9).to_string(),
                identity: resource_id(1)
            })
        );

        let mismatched = with_fields(&project_resource(), |fields| {
            set(
                fields,
                PROJECT_ID_FIELD,
                CanonicalValue::string(project_id(8).to_string()),
            );
        });
        assert!(matches!(
            ProjectBody::read(&mismatched),
            Err(TenancyError::IdentityMismatch { .. })
        ));
    }

    #[test]
    fn a_project_cannot_be_read_under_a_tenant_that_does_not_own_it() {
        // Storage the domain would never have accepted: the scope column says one
        // tenant, the body says another.
        let moved = ResourceVersion {
            scope: ResourceScope::Tenant(tenant_id(9)),
            ..project_resource()
        };
        assert_eq!(
            ProjectBody::read(&moved),
            Err(TenancyError::OwnerMismatch {
                reference: moved.reference,
                declared: tenant_id(1),
                scoped: Some(tenant_id(9))
            })
        );

        let mut state = state();
        state
            .insert(tenant(9, "globex"))
            .expect("a distinct reference");
        let mut relocated = DesiredState::new();
        for resource in state.resources() {
            let resource = if resource.reference.kind == ResourceKind::Project {
                moved.clone()
            } else {
                resource.clone()
            };
            relocated.insert(resource).expect("distinct references");
        }
        for blob in state.blobs() {
            relocated.declare_blob(*blob);
        }
        assert_eq!(
            relocated.validate(),
            Err(ValidationError::Tenancy(TenancyError::OwnerMismatch {
                reference: moved.reference,
                declared: tenant_id(1),
                scoped: Some(tenant_id(9))
            })),
            "an owner edited underneath a project is refused by the domain"
        );
    }

    #[test]
    fn an_invalid_tenancy_body_is_refused_before_a_manifest_exists() {
        let mut state = DesiredState::new();
        let unreadable = with_fields(&tenant_resource(), |fields| {
            set(fields, "schema", CanonicalValue::string("axond.tenant.v2"));
        });
        state.insert(unreadable.clone()).expect("a fresh state");
        let candidate = candidate(ExpectedRevision::Empty, "unreadable", state);
        assert!(matches!(
            candidate.validated_checksum(),
            Err(ValidationError::Tenancy(TenancyError::Schema { .. }))
        ));
        assert!(
            matches!(
                RevisionManifest::of(
                    super::super::fixtures::revision_id(1),
                    None,
                    SystemTime::UNIX_EPOCH,
                    &candidate
                ),
                Err(ValidationError::Tenancy(TenancyError::Schema { .. }))
            ),
            "a body this build cannot read must not become a published revision"
        );
    }

    #[test]
    fn a_hydrated_revision_re_reads_the_bodies_it_was_published_with() {
        let candidate = candidate(ExpectedRevision::Empty, "hydrate", state());
        let manifest = RevisionManifest::of(
            super::super::fixtures::revision_id(1),
            None,
            SystemTime::UNIX_EPOCH,
            &candidate,
        )
        .expect("the fixture state is publishable");
        let loaded = LoadedRevision::assemble(manifest.clone(), candidate.state.clone())
            .expect("the state the manifest describes");
        let tenancy = Tenancy::of(loaded.state()).expect("the fixture tenancy resolves");
        assert_eq!(tenancy.tenants().len(), 1);
        assert_eq!(tenancy.projects().len(), 1);
        assert_eq!(
            tenancy
                .qualified_name(project_id(2))
                .map(|name| name.to_string()),
            Some("acme/core".to_owned())
        );
        assert_eq!(
            tenancy
                .tenant(tenant_id(1))
                .map(|tenant| tenant.slug.as_str()),
            Some("acme")
        );
        assert_eq!(loaded.state().checksum().unwrap(), manifest.checksum);

        // A tenancy body edited in storage does not hydrate, whatever the row's
        // own checksum says.
        let mut edited = DesiredState::new();
        for resource in candidate.state.resources() {
            let resource = if resource.reference.kind == ResourceKind::Project {
                with_fields(resource, |fields| {
                    set(
                        fields,
                        TENANT_ID_FIELD,
                        CanonicalValue::string(tenant_id(9).to_string()),
                    );
                })
            } else {
                resource.clone()
            };
            edited.insert(resource).expect("distinct references");
        }
        for blob in candidate.state.blobs() {
            edited.declare_blob(*blob);
        }
        let error = LoadedRevision::assemble(manifest, edited)
            .expect_err("an edited tenancy body must not hydrate");
        assert_eq!(
            error,
            IntegrityError::Invalid(ValidationError::Tenancy(TenancyError::OwnerMismatch {
                reference: project_resource().reference,
                declared: tenant_id(9),
                scoped: Some(tenant_id(1))
            })),
            "the domain refuses it before any checksum is compared, and names the row"
        );
    }

    /// The two refusals hydration must not conflate. Both are strict; only one is
    /// a reason to go looking at storage.
    #[test]
    fn a_body_this_build_cannot_read_hydrates_as_an_incompatibility_not_corruption() {
        let candidate = candidate(ExpectedRevision::Empty, "hydrate", state());
        let manifest = RevisionManifest::of(
            super::super::fixtures::revision_id(1),
            None,
            SystemTime::UNIX_EPOCH,
            &candidate,
        )
        .expect("the fixture state is publishable");

        // A revision retained from before tenancy bodies were typed: the row is
        // intact, and this build simply does not read it.
        let mut legacy = DesiredState::new();
        for resource in candidate.state.resources() {
            let resource = if resource.reference.kind == ResourceKind::Tenant {
                super::super::fixtures::legacy_tenant(1, "acme")
            } else {
                resource.clone()
            };
            legacy.insert(resource).expect("distinct references");
        }
        for blob in candidate.state.blobs() {
            legacy.declare_blob(*blob);
        }
        let error = LoadedRevision::assemble(manifest.clone(), legacy)
            .expect_err("an untyped tenancy body must not hydrate");
        assert_eq!(
            error,
            IntegrityError::Incompatible(BodySkew::Tenancy(TenancyError::MissingField {
                reference: tenant_resource().reference,
                field: "schema"
            })),
            "a legacy body is a compatibility refusal, and it names the row"
        );
        assert!(error.is_incompatible());
        assert!(
            !error.to_string().contains("unreadable"),
            "intact storage must not be described as unreadable: {error}"
        );

        // A schema identifier from a future release is the same class of refusal,
        // arriving from the other direction.
        let mut newer = DesiredState::new();
        for resource in candidate.state.resources() {
            let resource = if resource.reference.kind == ResourceKind::Tenant {
                with_fields(resource, |fields| {
                    set(fields, "schema", CanonicalValue::string("axond.tenant.v2"));
                })
            } else {
                resource.clone()
            };
            newer.insert(resource).expect("distinct references");
        }
        for blob in candidate.state.blobs() {
            newer.declare_blob(*blob);
        }
        let error =
            LoadedRevision::assemble(manifest, newer).expect_err("a newer schema must not hydrate");
        assert!(
            matches!(
                error,
                IntegrityError::Incompatible(BodySkew::Tenancy(TenancyError::Schema { .. }))
            ),
            "{error}"
        );

        // An ownership contradiction is not a compatibility refusal: those rows were readable and
        // disagree, which is the case that means storage.
        assert!(
            !TenancyError::OwnerMismatch {
                reference: project_resource().reference,
                declared: tenant_id(9),
                scoped: Some(tenant_id(1)),
            }
            .is_incompatible()
        );

        // Nor is a body that declares a schema this build *does* read and then is
        // not one: the identifier is read first, so past that point the field set
        // is known, and a field that has gone missing or changed type is a rewrite
        // rather than a release skew. Telling that operator the database is fine
        // would point them away from the only place to look.
        let mut damaged = DesiredState::new();
        let losing_a_field = with_fields(&tenant_resource(), |fields| {
            fields.retain(|(name, _)| name != DISPLAY_NAME_FIELD);
        });
        damaged.insert(losing_a_field).expect("a fresh state");
        let error = damaged
            .validate()
            .expect_err("a v1 body without a v1 field is not a v1 body");
        assert_eq!(
            error,
            ValidationError::Tenancy(TenancyError::MissingField {
                reference: tenant_resource().reference,
                field: DISPLAY_NAME_FIELD,
            })
        );
        let ValidationError::Tenancy(tenancy) = &error else {
            panic!("expected a tenancy refusal, got {error:?}");
        };
        assert!(
            !tenancy.is_incompatible(),
            "a field lost from a schema this build reads is damage, not a skew"
        );
        assert!(
            !TenancyError::FieldType {
                reference: tenant_resource().reference,
                field: TENANT_ID_FIELD,
            }
            .is_incompatible(),
            "and so is a field whose type changed underneath the gateway"
        );
        assert!(
            TenancyError::MissingField {
                reference: tenant_resource().reference,
                field: SCHEMA_FIELD,
            }
            .is_incompatible(),
            "only the identifier's own absence is the legacy shape"
        );

        // Nor is a body that is no longer a record at all, or one under a kind that
        // does not match it. No release, typed or not, wrote a tenancy body as a
        // scalar or a blob, so the shape itself is the evidence of a rewrite, and a
        // newer field set would still be a record refused by its identifier.
        let mut scalar = DesiredState::new();
        let not_a_record = ResourceVersion {
            body: ResourceBody::Inline(CanonicalValue::String("acme".to_owned())),
            ..tenant_resource()
        };
        scalar.insert(not_a_record).expect("a fresh state");
        let error = scalar
            .validate()
            .expect_err("a tenancy body is a record or it is nothing");
        let ValidationError::Tenancy(tenancy) = &error else {
            panic!("expected a tenancy refusal, got {error:?}");
        };
        assert!(
            !tenancy.is_incompatible(),
            "a body no build ever wrote is damage, and points at storage"
        );
        assert!(
            !TenancyError::NotInline {
                reference: tenant_resource().reference,
            }
            .is_incompatible(),
            "and so is a tenancy record replaced by a blob reference"
        );
        assert!(
            !TenancyError::Kind {
                reference: tenant_resource().reference,
                expected: ResourceKind::Tenant,
                found: ResourceKind::Project,
            }
            .is_incompatible(),
            "and so is a row whose kind and body disagree"
        );
    }

    #[test]
    fn a_project_needs_a_tenant_this_revision_declares() {
        let mut orphaned = DesiredState::new();
        let project = project(&tenant_id(9), 2, "core");
        orphaned.insert(project.clone()).expect("a fresh state");
        assert_eq!(
            Tenancy::of(&orphaned),
            Err(TenancyError::UnknownTenant {
                reference: project.reference,
                tenant: tenant_id(9)
            })
        );

        // It is the *project* that is held to this, and it is damage rather than a
        // skew: this build never writes a project body without its tenant, so the
        // row went missing rather than never having been required.
        assert!(
            !TenancyError::UnknownTenant {
                reference: project.reference,
                tenant: tenant_id(9),
            }
            .is_incompatible(),
            "a row this build itself wrote and cannot find is not an upgrade"
        );

        // Nothing else is: a revision published before this rule existed could
        // carry a credential or an alias whose tenant no row described, and it
        // still hydrates — the tenancy view needs no tenant for either, and a scope
        // naming a tenant that does not exist is unroutable at the boundary that
        // routes, not unreadable here.
        let mut stray = DesiredState::new();
        let alias = alias(&tenant_id(9), 4, "fast", &[]);
        stray.insert(alias.clone()).expect("a fresh state");
        stray
            .validate()
            .expect("an older revision's tenant-scoped resource is not made unhydratable");
        let tenancy = Tenancy::of(&stray).expect("nothing tenancy reads is missing");
        assert_eq!(tenancy.tenants().len(), 0);
    }

    #[test]
    fn a_project_scoped_resource_names_its_projects_real_owner() {
        let owner = tenant_id(1);
        let other = tenant_id(9);
        let mut state = state();
        state.insert(tenant(9, "globex")).expect("a distinct id");

        // A credential filed under `globex`'s scope but inside `acme`'s project:
        // the pair is inconsistent even though each half exists.
        let leaked = project_credential(&other, &project_id(2), 21, "leaked");
        let mut mixed = state.clone();
        mixed.insert(leaked.clone()).expect("a distinct reference");
        assert_eq!(
            Tenancy::of(&mixed),
            Err(TenancyError::ProjectOwnerMismatch {
                reference: leaked.reference,
                project: project_id(2),
                scoped: other,
                owner
            })
        );

        // A project this revision does not declare contradicts nothing, so there
        // is nothing here to refuse: what the scope names is unroutable, which the
        // boundary that routes says, and an older revision does not become
        // unhydratable for having said it.
        let dangling = project_credential(&owner, &project_id(77), 22, "dangling");
        let mut missing = state.clone();
        missing
            .insert(dangling.clone())
            .expect("a distinct reference");
        missing
            .validate()
            .expect("a scope naming an undeclared project is unroutable, not unreadable");

        // The consistent pair is valid desired state.
        let inside = project_credential(&owner, &project_id(2), 23, "inside");
        let mut consistent = state;
        consistent
            .insert(inside)
            .expect("a distinct reference")
            .validate()
            .expect("a resource inside its own tenant's project is valid");
    }

    #[test]
    fn a_project_slug_is_unique_per_tenant_and_qualified_beyond_it() {
        // Two tenants may both call a project `core`: the slug is scoped, so the
        // envelope-level uniqueness rule permits it.
        let mut state = state();
        state.insert(tenant(9, "globex")).expect("a distinct id");
        state
            .insert(project(&tenant_id(9), 12, "core"))
            .expect("a distinct reference")
            .validate()
            .expect("a project slug is unique within its tenant, not across tenants");

        let tenancy = Tenancy::of(&state).expect("two tenants, two projects");
        assert_eq!(tenancy.projects().len(), 2);
        assert_eq!(tenancy.projects_of(tenant_id(9)).count(), 1);
        let qualified: Vec<String> = tenancy
            .projects()
            .map(|project| {
                tenancy
                    .qualified_name(project.body.project())
                    .expect("a project's tenant is declared")
                    .to_string()
            })
            .collect();
        assert_eq!(qualified, vec!["acme/core", "globex/core"]);
        assert_eq!(
            QualifiedProject::parse("acme/core").map(|name| name.to_string()),
            Some("acme/core".to_owned()),
            "the qualified form decomposes exactly one way"
        );
        assert_eq!(QualifiedProject::parse("acme"), None);

        // The same slug twice within one tenant is not a tenancy question: the
        // envelope answers it, and it answers it the same way it always has.
        let mut clashing = state;
        clashing
            .insert(project(&tenant_id(1), 13, "core"))
            .expect("a distinct reference");
        assert!(matches!(
            clashing.validate(),
            Err(ValidationError::DuplicateSlug { .. })
        ));
    }

    #[test]
    fn a_display_name_is_prose_and_is_normalized_on_the_way_in() {
        assert_eq!(display_name("Acme Corp").as_str(), "Acme Corp");
        assert_eq!(DisplayName::parse(""), Err(InvalidDisplayName::Empty));
        assert_eq!(
            DisplayName::parse("Acme "),
            Err(InvalidDisplayName::Untrimmed)
        );
        assert_eq!(
            DisplayName::parse("Acme\tCorp"),
            Err(InvalidDisplayName::ControlCharacter { codepoint: 0x09 }),
            "a name with no canonical form is refused here, not at publication"
        );
        let long = "a".repeat(DisplayName::MAX_LEN + 1);
        assert_eq!(
            DisplayName::parse(&long),
            Err(InvalidDisplayName::TooLong {
                length: DisplayName::MAX_LEN + 1,
                max: DisplayName::MAX_LEN
            })
        );

        // A tenant body carrying an unencodable name cannot be built, so the
        // canonical form of any body that exists is obtainable.
        assert!(
            tenant_body(1, "Acme")
                .canonical()
                .to_canonical_bytes()
                .is_ok(),
            "a validated body always has canonical bytes"
        );
    }

    /// A byte-order mark is not a control character, so it needs its own rule to
    /// stay in step with the canonical encoder — which refuses it. Without one,
    /// `DisplayName::parse` would accept a name whose body then has no canonical
    /// bytes, turning a validation error into a publication-time encoding failure.
    #[test]
    fn a_display_name_refuses_a_byte_order_mark_exactly_as_the_encoder_does() {
        for name in ["\u{feff}Acme", "Ac\u{feff}me", "Acme\u{feff}"] {
            assert_eq!(
                DisplayName::parse(name),
                Err(InvalidDisplayName::ByteOrderMark),
                "a mark anywhere in `{name}` is refused, not only a leading one"
            );
            // The same string, encoded: what validation is standing in front of.
            assert_eq!(
                CanonicalValue::string(name).to_canonical_bytes(),
                Err(super::super::canonical::CanonicalError::ByteOrderMark),
                "the two layers agree about `{name}`"
            );
        }
        assert!(
            !'\u{feff}'.is_control(),
            "a mark is refused by its own rule, not by the control-character check"
        );
    }

    #[test]
    fn the_view_is_ordered_by_id_so_two_replicas_read_it_the_same_way() {
        let mut state = state();
        state.insert(tenant(9, "globex")).expect("a distinct id");
        state
            .insert(project(&tenant_id(9), 12, "later"))
            .expect("a distinct reference");
        let tenancy = Tenancy::of(&state).expect("valid tenancy");
        let tenants: Vec<TenantId> = tenancy
            .tenants()
            .map(|tenant| tenant.body.tenant())
            .collect();
        let mut sorted = tenants.clone();
        sorted.sort();
        assert_eq!(tenants, sorted);
        let projects: Vec<ProjectId> = tenancy
            .projects()
            .map(|project| project.body.project())
            .collect();
        let mut sorted = projects.clone();
        sorted.sort();
        assert_eq!(projects, sorted);
        assert_eq!(
            tenancy.project(project_id(12)).map(|p| p.slug.as_str()),
            Some("later")
        );
        assert_eq!(tenancy.project(project_id(77)), None);
    }
}