asupersync 0.3.4

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
//! Self-hosted ATP rendezvous and relay service configuration.
//!
//! This module is the typed contract behind `atp rendezvous serve` and
//! `atp relay serve`. It keeps operator policy explicit before runtime wiring:
//! identity, TLS posture, quotas, expiry windows, rate limits, access policy,
//! mailbox storage, logging, federation, and restart behavior are all modeled
//! as data instead of ambient daemon assumptions.

use super::relay::{RelayError, RelayServiceConfig};
use super::rendezvous::{Quotas as RendezvousQuotas, ServiceConfig as RendezvousServiceConfig};

const MIB: u64 = 1024 * 1024;

/// ATP service command represented by one self-hosted serve config.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AtpServeCommand {
    /// `atp rendezvous serve`.
    RendezvousServe,
    /// `atp relay serve`.
    RelayServe,
}

impl AtpServeCommand {
    /// Stable CLI words for logs, help text, and proof contracts.
    #[must_use]
    pub const fn words(self) -> &'static [&'static str] {
        match self {
            Self::RendezvousServe => &["atp", "rendezvous", "serve"],
            Self::RelayServe => &["atp", "relay", "serve"],
        }
    }
}

/// Supported self-hosted deployment profiles.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AtpDeploymentMode {
    /// Single operator, loopback or private-hosted service.
    Personal,
    /// Team service with authenticated peers and durable restart state.
    Team,
    /// CI service with small quotas and no restart-state retention.
    Ci,
    /// Public-facing gateway with TLS required and redacted logs by default.
    PublicGateway,
}

/// Service identity visible in operator logs and proof artifacts.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AtpServiceIdentity {
    service_id: String,
    operator_label: String,
}

impl AtpServiceIdentity {
    /// Construct a service identity.
    ///
    /// # Errors
    ///
    /// Returns an error when either identifier is blank.
    pub fn new(
        service_id: impl Into<String>,
        operator_label: impl Into<String>,
    ) -> Result<Self, AtpOpsConfigError> {
        let service_id = service_id.into();
        let operator_label = operator_label.into();
        if service_id.trim().is_empty() {
            return Err(AtpOpsConfigError::EmptyServiceId);
        }
        if operator_label.trim().is_empty() {
            return Err(AtpOpsConfigError::EmptyOperatorLabel);
        }
        Ok(Self {
            service_id,
            operator_label,
        })
    }

    /// Stable service id.
    #[must_use]
    pub fn service_id(&self) -> &str {
        &self.service_id
    }

    /// Human/operator-facing deployment label.
    #[must_use]
    pub fn operator_label(&self) -> &str {
        &self.operator_label
    }
}

/// TLS posture for a self-hosted service.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AtpTlsPolicy {
    /// Plaintext is only acceptable for loopback or explicitly private labs.
    DisabledLoopbackOnly,
    /// TLS is required. Client auth can be required separately for team modes.
    Required {
        /// Whether peers must authenticate at the TLS/session boundary.
        client_auth_required: bool,
    },
}

impl AtpTlsPolicy {
    /// Return true when the service requires TLS on its listener.
    #[must_use]
    pub const fn requires_tls(self) -> bool {
        matches!(self, Self::Required { .. })
    }

    /// Return true when peer/client auth is required at the TLS boundary.
    #[must_use]
    pub const fn requires_client_auth(self) -> bool {
        matches!(
            self,
            Self::Required {
                client_auth_required: true,
            }
        )
    }
}

/// Per-service rate limits exposed to operators.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AtpRateLimitPolicy {
    /// Accepted reservations per minute.
    pub max_reservations_per_minute: u32,
    /// Accepted relay packets per second.
    pub max_packets_per_second: u32,
    /// Accepted mailbox bytes per minute.
    pub max_mailbox_bytes_per_minute: u64,
}

impl AtpRateLimitPolicy {
    /// Validate that all rate limits are non-zero.
    pub const fn validate(self) -> Result<Self, AtpOpsConfigError> {
        if self.max_reservations_per_minute == 0
            || self.max_packets_per_second == 0
            || self.max_mailbox_bytes_per_minute == 0
        {
            return Err(AtpOpsConfigError::InvalidQuota);
        }
        Ok(self)
    }
}

/// Expiry windows for service-scoped state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AtpExpiryPolicy {
    /// Maximum rendezvous candidate lifetime.
    pub candidate_ttl_micros: u64,
    /// Default relay reservation lifetime.
    pub relay_reservation_ttl_micros: u64,
    /// Default mailbox retention lifetime.
    pub mailbox_ttl_secs: u64,
}

impl AtpExpiryPolicy {
    /// Validate that all expiry windows are non-zero.
    pub const fn validate(self) -> Result<Self, AtpOpsConfigError> {
        if self.candidate_ttl_micros == 0
            || self.relay_reservation_ttl_micros == 0
            || self.mailbox_ttl_secs == 0
        {
            return Err(AtpOpsConfigError::InvalidExpiry);
        }
        Ok(self)
    }
}

/// Access policy for peers and groups allowed to use a service.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AtpAccessPolicy {
    allowed_peers: Vec<String>,
    allowed_groups: Vec<String>,
    public_registration: bool,
}

impl AtpAccessPolicy {
    /// Private access policy with no public registration.
    #[must_use]
    pub const fn private() -> Self {
        Self {
            allowed_peers: Vec::new(),
            allowed_groups: Vec::new(),
            public_registration: false,
        }
    }

    /// Public registration policy for an explicitly public gateway.
    #[must_use]
    pub const fn public_registration() -> Self {
        Self {
            allowed_peers: Vec::new(),
            allowed_groups: Vec::new(),
            public_registration: true,
        }
    }

    /// Add one allowed peer id.
    ///
    /// # Errors
    ///
    /// Returns an error when the peer id is blank.
    pub fn with_allowed_peer(
        mut self,
        peer_id: impl Into<String>,
    ) -> Result<Self, AtpOpsConfigError> {
        let peer_id = peer_id.into();
        if peer_id.trim().is_empty() {
            return Err(AtpOpsConfigError::EmptyAccessEntry);
        }
        self.allowed_peers.push(peer_id);
        Ok(self)
    }

    /// Add one allowed group id.
    ///
    /// # Errors
    ///
    /// Returns an error when the group id is blank.
    pub fn with_allowed_group(
        mut self,
        group_id: impl Into<String>,
    ) -> Result<Self, AtpOpsConfigError> {
        let group_id = group_id.into();
        if group_id.trim().is_empty() {
            return Err(AtpOpsConfigError::EmptyAccessEntry);
        }
        self.allowed_groups.push(group_id);
        Ok(self)
    }

    /// Allowed peer ids.
    #[must_use]
    pub fn allowed_peers(&self) -> &[String] {
        &self.allowed_peers
    }

    /// Allowed group ids.
    #[must_use]
    pub fn allowed_groups(&self) -> &[String] {
        &self.allowed_groups
    }

    /// Whether public registration is enabled.
    #[must_use]
    pub const fn public_registration_enabled(&self) -> bool {
        self.public_registration
    }
}

impl Default for AtpAccessPolicy {
    fn default() -> Self {
        Self::private()
    }
}

/// Mailbox storage policy for relay-backed offline delivery.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AtpMailboxStoragePolicy {
    storage_label: String,
    max_bytes: u64,
    max_records: u64,
    encrypted_only: bool,
}

impl AtpMailboxStoragePolicy {
    /// Construct mailbox storage policy.
    ///
    /// # Errors
    ///
    /// Returns an error when the label is blank or quotas are zero.
    pub fn new(
        storage_label: impl Into<String>,
        max_bytes: u64,
        max_records: u64,
        encrypted_only: bool,
    ) -> Result<Self, AtpOpsConfigError> {
        let storage_label = storage_label.into();
        if storage_label.trim().is_empty() {
            return Err(AtpOpsConfigError::EmptyMailboxStorageLabel);
        }
        if max_bytes == 0 || max_records == 0 {
            return Err(AtpOpsConfigError::InvalidQuota);
        }
        Ok(Self {
            storage_label,
            max_bytes,
            max_records,
            encrypted_only,
        })
    }

    /// Storage label for operator diagnostics.
    #[must_use]
    pub fn storage_label(&self) -> &str {
        &self.storage_label
    }

    /// Maximum mailbox storage bytes.
    #[must_use]
    pub const fn max_bytes(&self) -> u64 {
        self.max_bytes
    }

    /// Maximum mailbox record count.
    #[must_use]
    pub const fn max_records(&self) -> u64 {
        self.max_records
    }

    /// Whether mailbox custody requires encrypted bytes.
    #[must_use]
    pub const fn encrypted_only(&self) -> bool {
        self.encrypted_only
    }
}

/// Operator log policy.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AtpOperatorLogPolicy {
    /// Whether logs may include redacted peer id prefixes.
    pub log_peer_ids: bool,
    /// Whether service events must be emitted as structured records.
    pub structured_events: bool,
    /// Whether restart snapshots retain active state.
    pub retain_state_on_restart: bool,
}

/// Optional federation policy.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AtpFederationPolicy {
    trusted_services: Vec<String>,
    trust_assumptions: Vec<String>,
}

impl AtpFederationPolicy {
    /// Disable federation.
    #[must_use]
    pub const fn disabled() -> Self {
        Self {
            trusted_services: Vec::new(),
            trust_assumptions: Vec::new(),
        }
    }

    /// Enable federation with explicit trusted services and trust assumptions.
    ///
    /// # Errors
    ///
    /// Returns an error unless both lists are non-empty and contain no blank
    /// entries.
    pub fn opt_in(
        trusted_services: Vec<String>,
        trust_assumptions: Vec<String>,
    ) -> Result<Self, AtpOpsConfigError> {
        if trusted_services.is_empty() {
            return Err(AtpOpsConfigError::FederationRequiresTrustedService);
        }
        if trust_assumptions.is_empty() {
            return Err(AtpOpsConfigError::FederationRequiresTrustAssumption);
        }
        if trusted_services
            .iter()
            .chain(trust_assumptions.iter())
            .any(|entry| entry.trim().is_empty())
        {
            return Err(AtpOpsConfigError::EmptyFederationEntry);
        }
        Ok(Self {
            trusted_services,
            trust_assumptions,
        })
    }

    /// Whether federation is enabled.
    #[must_use]
    pub fn enabled(&self) -> bool {
        !self.trusted_services.is_empty()
    }

    /// Trusted federated service ids.
    #[must_use]
    pub fn trusted_services(&self) -> &[String] {
        &self.trusted_services
    }

    /// Explicit operator trust assumptions.
    #[must_use]
    pub fn trust_assumptions(&self) -> &[String] {
        &self.trust_assumptions
    }
}

impl Default for AtpFederationPolicy {
    fn default() -> Self {
        Self::disabled()
    }
}

/// Relay plaintext visibility guarantee.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RelayContentVisibility {
    /// Relay sees routing metadata, timing, and ciphertext, never object bytes.
    OpaqueEncryptedOnly,
}

/// Self-hosted `atp rendezvous serve` configuration.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AtpRendezvousServeConfig {
    command: AtpServeCommand,
    mode: AtpDeploymentMode,
    identity: AtpServiceIdentity,
    tls: AtpTlsPolicy,
    quotas: RendezvousQuotas,
    expiry: AtpExpiryPolicy,
    rate_limits: AtpRateLimitPolicy,
    access: AtpAccessPolicy,
    federation: AtpFederationPolicy,
    logs: AtpOperatorLogPolicy,
}

impl AtpRendezvousServeConfig {
    /// Build a mode-specific rendezvous serve config.
    ///
    /// # Errors
    ///
    /// Returns an error for invalid identity or generated policy values.
    pub fn for_mode(
        mode: AtpDeploymentMode,
        service_id: impl Into<String>,
        operator_label: impl Into<String>,
    ) -> Result<Self, AtpOpsConfigError> {
        let config = Self {
            command: AtpServeCommand::RendezvousServe,
            mode,
            identity: AtpServiceIdentity::new(service_id, operator_label)?,
            tls: default_tls_policy(mode),
            quotas: rendezvous_quotas(mode),
            expiry: expiry_policy(mode).validate()?,
            rate_limits: rate_limits(mode).validate()?,
            access: default_access_policy(mode),
            federation: AtpFederationPolicy::disabled(),
            logs: log_policy(mode),
        };
        config.validate()?;
        Ok(config)
    }

    /// Command represented by this config.
    #[must_use]
    pub const fn command(&self) -> AtpServeCommand {
        self.command
    }

    /// Deployment mode.
    #[must_use]
    pub const fn mode(&self) -> AtpDeploymentMode {
        self.mode
    }

    /// Service identity.
    #[must_use]
    pub const fn identity(&self) -> &AtpServiceIdentity {
        &self.identity
    }

    /// TLS policy.
    #[must_use]
    pub const fn tls(&self) -> AtpTlsPolicy {
        self.tls
    }

    /// Rendezvous quotas.
    #[must_use]
    pub const fn quotas(&self) -> RendezvousQuotas {
        self.quotas
    }

    /// Expiry policy.
    #[must_use]
    pub const fn expiry(&self) -> AtpExpiryPolicy {
        self.expiry
    }

    /// Rate-limit policy.
    #[must_use]
    pub const fn rate_limits(&self) -> AtpRateLimitPolicy {
        self.rate_limits
    }

    /// Access policy.
    #[must_use]
    pub const fn access(&self) -> &AtpAccessPolicy {
        &self.access
    }

    /// Federation policy.
    #[must_use]
    pub const fn federation(&self) -> &AtpFederationPolicy {
        &self.federation
    }

    /// Operator log policy.
    #[must_use]
    pub const fn logs(&self) -> AtpOperatorLogPolicy {
        self.logs
    }

    /// Override TLS policy.
    ///
    /// # Errors
    ///
    /// Returns an error when a public gateway would become non-TLS.
    pub fn with_tls_policy(mut self, tls: AtpTlsPolicy) -> Result<Self, AtpOpsConfigError> {
        self.tls = tls;
        self.validate()?;
        Ok(self)
    }

    /// Override access policy.
    #[must_use]
    pub fn with_access_policy(mut self, access: AtpAccessPolicy) -> Self {
        self.access = access;
        self
    }

    /// Override federation policy.
    ///
    /// # Errors
    ///
    /// Returns an error when public gateway TLS requirements would be violated.
    pub fn with_federation_policy(
        mut self,
        federation: AtpFederationPolicy,
    ) -> Result<Self, AtpOpsConfigError> {
        self.federation = federation;
        self.validate()?;
        Ok(self)
    }

    /// Convert to the deterministic rendezvous state-machine config.
    pub fn rendezvous_service_config(
        &self,
    ) -> Result<RendezvousServiceConfig, super::rendezvous::Error> {
        RendezvousServiceConfig::new(self.identity.service_id.clone(), self.quotas).map(|config| {
            config
                .with_log_peer_ids(self.logs.log_peer_ids)
                .with_retain_state_on_restart(self.logs.retain_state_on_restart)
        })
    }

    fn validate(&self) -> Result<(), AtpOpsConfigError> {
        validate_public_tls(self.mode, self.tls)
    }
}

/// Self-hosted `atp relay serve` configuration.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AtpRelayServeConfig {
    command: AtpServeCommand,
    mode: AtpDeploymentMode,
    identity: AtpServiceIdentity,
    tls: AtpTlsPolicy,
    max_active_reservations: usize,
    expiry: AtpExpiryPolicy,
    rate_limits: AtpRateLimitPolicy,
    access: AtpAccessPolicy,
    mailbox_storage: AtpMailboxStoragePolicy,
    federation: AtpFederationPolicy,
    logs: AtpOperatorLogPolicy,
}

impl AtpRelayServeConfig {
    /// Build a mode-specific relay serve config.
    ///
    /// # Errors
    ///
    /// Returns an error for invalid identity or generated policy values.
    pub fn for_mode(
        mode: AtpDeploymentMode,
        relay_id: impl Into<String>,
        operator_label: impl Into<String>,
    ) -> Result<Self, AtpOpsConfigError> {
        let config = Self {
            command: AtpServeCommand::RelayServe,
            mode,
            identity: AtpServiceIdentity::new(relay_id, operator_label)?,
            tls: default_tls_policy(mode),
            max_active_reservations: relay_reservation_limit(mode),
            expiry: expiry_policy(mode).validate()?,
            rate_limits: rate_limits(mode).validate()?,
            access: default_access_policy(mode),
            mailbox_storage: mailbox_storage_policy(mode)?,
            federation: AtpFederationPolicy::disabled(),
            logs: log_policy(mode),
        };
        config.validate()?;
        Ok(config)
    }

    /// Command represented by this config.
    #[must_use]
    pub const fn command(&self) -> AtpServeCommand {
        self.command
    }

    /// Deployment mode.
    #[must_use]
    pub const fn mode(&self) -> AtpDeploymentMode {
        self.mode
    }

    /// Service identity.
    #[must_use]
    pub const fn identity(&self) -> &AtpServiceIdentity {
        &self.identity
    }

    /// TLS policy.
    #[must_use]
    pub const fn tls(&self) -> AtpTlsPolicy {
        self.tls
    }

    /// Maximum active relay reservations.
    #[must_use]
    pub const fn max_active_reservations(&self) -> usize {
        self.max_active_reservations
    }

    /// Expiry policy.
    #[must_use]
    pub const fn expiry(&self) -> AtpExpiryPolicy {
        self.expiry
    }

    /// Rate-limit policy.
    #[must_use]
    pub const fn rate_limits(&self) -> AtpRateLimitPolicy {
        self.rate_limits
    }

    /// Access policy.
    #[must_use]
    pub const fn access(&self) -> &AtpAccessPolicy {
        &self.access
    }

    /// Mailbox storage policy.
    #[must_use]
    pub const fn mailbox_storage(&self) -> &AtpMailboxStoragePolicy {
        &self.mailbox_storage
    }

    /// Federation policy.
    #[must_use]
    pub const fn federation(&self) -> &AtpFederationPolicy {
        &self.federation
    }

    /// Operator log policy.
    #[must_use]
    pub const fn logs(&self) -> AtpOperatorLogPolicy {
        self.logs
    }

    /// Relay content visibility guarantee.
    #[must_use]
    pub const fn content_visibility(&self) -> RelayContentVisibility {
        RelayContentVisibility::OpaqueEncryptedOnly
    }

    /// Override TLS policy.
    ///
    /// # Errors
    ///
    /// Returns an error when a public gateway would become non-TLS.
    pub fn with_tls_policy(mut self, tls: AtpTlsPolicy) -> Result<Self, AtpOpsConfigError> {
        self.tls = tls;
        self.validate()?;
        Ok(self)
    }

    /// Override access policy.
    #[must_use]
    pub fn with_access_policy(mut self, access: AtpAccessPolicy) -> Self {
        self.access = access;
        self
    }

    /// Override federation policy.
    ///
    /// # Errors
    ///
    /// Returns an error when public gateway TLS requirements would be violated.
    pub fn with_federation_policy(
        mut self,
        federation: AtpFederationPolicy,
    ) -> Result<Self, AtpOpsConfigError> {
        self.federation = federation;
        self.validate()?;
        Ok(self)
    }

    /// Convert to the deterministic relay state-machine config.
    pub fn relay_service_config(&self) -> Result<RelayServiceConfig, RelayError> {
        RelayServiceConfig::new(
            self.identity.service_id.clone(),
            self.max_active_reservations,
        )
        .map(|config| {
            config
                .with_log_peer_ids(self.logs.log_peer_ids)
                .with_retain_state_on_restart(self.logs.retain_state_on_restart)
        })
    }

    fn validate(&self) -> Result<(), AtpOpsConfigError> {
        validate_public_tls(self.mode, self.tls)?;
        if self.max_active_reservations == 0 {
            return Err(AtpOpsConfigError::InvalidQuota);
        }
        if !self.mailbox_storage.encrypted_only {
            return Err(AtpOpsConfigError::MailboxMustBeEncrypted);
        }
        Ok(())
    }
}

fn validate_public_tls(
    mode: AtpDeploymentMode,
    tls: AtpTlsPolicy,
) -> Result<(), AtpOpsConfigError> {
    if mode == AtpDeploymentMode::PublicGateway && !tls.requires_tls() {
        return Err(AtpOpsConfigError::PublicGatewayRequiresTls);
    }
    Ok(())
}

const fn default_tls_policy(mode: AtpDeploymentMode) -> AtpTlsPolicy {
    match mode {
        AtpDeploymentMode::Personal | AtpDeploymentMode::Ci => AtpTlsPolicy::DisabledLoopbackOnly,
        AtpDeploymentMode::Team => AtpTlsPolicy::Required {
            client_auth_required: true,
        },
        AtpDeploymentMode::PublicGateway => AtpTlsPolicy::Required {
            client_auth_required: false,
        },
    }
}

const fn rendezvous_quotas(mode: AtpDeploymentMode) -> RendezvousQuotas {
    match mode {
        AtpDeploymentMode::Personal => RendezvousQuotas {
            max_candidates_per_peer: 8,
            max_total_candidates: 32,
            max_observations_per_peer: 4,
            max_total_observations: 32,
            max_attempts_per_peer: 8,
        },
        AtpDeploymentMode::Team => RendezvousQuotas {
            max_candidates_per_peer: 16,
            max_total_candidates: 512,
            max_observations_per_peer: 8,
            max_total_observations: 1024,
            max_attempts_per_peer: 16,
        },
        AtpDeploymentMode::Ci => RendezvousQuotas {
            max_candidates_per_peer: 4,
            max_total_candidates: 16,
            max_observations_per_peer: 2,
            max_total_observations: 16,
            max_attempts_per_peer: 4,
        },
        AtpDeploymentMode::PublicGateway => RendezvousQuotas {
            max_candidates_per_peer: 8,
            max_total_candidates: 4096,
            max_observations_per_peer: 4,
            max_total_observations: 8192,
            max_attempts_per_peer: 8,
        },
    }
}

const fn relay_reservation_limit(mode: AtpDeploymentMode) -> usize {
    match mode {
        AtpDeploymentMode::Personal => 64,
        AtpDeploymentMode::Team => 2048,
        AtpDeploymentMode::Ci => 32,
        AtpDeploymentMode::PublicGateway => 8192,
    }
}

const fn expiry_policy(mode: AtpDeploymentMode) -> AtpExpiryPolicy {
    match mode {
        AtpDeploymentMode::Personal => AtpExpiryPolicy {
            candidate_ttl_micros: 60_000_000,
            relay_reservation_ttl_micros: 300_000_000,
            mailbox_ttl_secs: 86_400,
        },
        AtpDeploymentMode::Team => AtpExpiryPolicy {
            candidate_ttl_micros: 60_000_000,
            relay_reservation_ttl_micros: 600_000_000,
            mailbox_ttl_secs: 604_800,
        },
        AtpDeploymentMode::Ci => AtpExpiryPolicy {
            candidate_ttl_micros: 10_000_000,
            relay_reservation_ttl_micros: 60_000_000,
            mailbox_ttl_secs: 3_600,
        },
        AtpDeploymentMode::PublicGateway => AtpExpiryPolicy {
            candidate_ttl_micros: 30_000_000,
            relay_reservation_ttl_micros: 120_000_000,
            mailbox_ttl_secs: 86_400,
        },
    }
}

const fn rate_limits(mode: AtpDeploymentMode) -> AtpRateLimitPolicy {
    match mode {
        AtpDeploymentMode::Personal => AtpRateLimitPolicy {
            max_reservations_per_minute: 60,
            max_packets_per_second: 2_000,
            max_mailbox_bytes_per_minute: 64 * MIB,
        },
        AtpDeploymentMode::Team => AtpRateLimitPolicy {
            max_reservations_per_minute: 2_000,
            max_packets_per_second: 200_000,
            max_mailbox_bytes_per_minute: 4 * 1024 * MIB,
        },
        AtpDeploymentMode::Ci => AtpRateLimitPolicy {
            max_reservations_per_minute: 30,
            max_packets_per_second: 1_000,
            max_mailbox_bytes_per_minute: 32 * MIB,
        },
        AtpDeploymentMode::PublicGateway => AtpRateLimitPolicy {
            max_reservations_per_minute: 1_000,
            max_packets_per_second: 100_000,
            max_mailbox_bytes_per_minute: 1024 * MIB,
        },
    }
}

fn default_access_policy(mode: AtpDeploymentMode) -> AtpAccessPolicy {
    if mode == AtpDeploymentMode::PublicGateway {
        AtpAccessPolicy::public_registration()
    } else {
        AtpAccessPolicy::private()
    }
}

fn mailbox_storage_policy(
    mode: AtpDeploymentMode,
) -> Result<AtpMailboxStoragePolicy, AtpOpsConfigError> {
    match mode {
        AtpDeploymentMode::Personal => {
            AtpMailboxStoragePolicy::new("personal-mailbox", 512 * MIB, 16_384, true)
        }
        AtpDeploymentMode::Team => {
            AtpMailboxStoragePolicy::new("team-mailbox", 64 * 1024 * MIB, 1_000_000, true)
        }
        AtpDeploymentMode::Ci => AtpMailboxStoragePolicy::new("ci-mailbox", 128 * MIB, 4096, true),
        AtpDeploymentMode::PublicGateway => {
            AtpMailboxStoragePolicy::new("public-gateway-mailbox", 16 * 1024 * MIB, 250_000, true)
        }
    }
}

const fn log_policy(mode: AtpDeploymentMode) -> AtpOperatorLogPolicy {
    match mode {
        AtpDeploymentMode::Personal => AtpOperatorLogPolicy {
            log_peer_ids: false,
            structured_events: true,
            retain_state_on_restart: true,
        },
        AtpDeploymentMode::Team => AtpOperatorLogPolicy {
            log_peer_ids: false,
            structured_events: true,
            retain_state_on_restart: true,
        },
        AtpDeploymentMode::Ci => AtpOperatorLogPolicy {
            log_peer_ids: true,
            structured_events: true,
            retain_state_on_restart: false,
        },
        AtpDeploymentMode::PublicGateway => AtpOperatorLogPolicy {
            log_peer_ids: false,
            structured_events: true,
            retain_state_on_restart: true,
        },
    }
}

/// ATP ops configuration errors.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum AtpOpsConfigError {
    /// Service id was blank.
    #[error("ATP service id is empty")]
    EmptyServiceId,
    /// Operator label was blank.
    #[error("ATP operator label is empty")]
    EmptyOperatorLabel,
    /// Peer or group access entry was blank.
    #[error("ATP access entry is empty")]
    EmptyAccessEntry,
    /// Mailbox storage label was blank.
    #[error("ATP mailbox storage label is empty")]
    EmptyMailboxStorageLabel,
    /// Quota or rate-limit value was zero.
    #[error("ATP service quota is invalid")]
    InvalidQuota,
    /// Expiry value was zero.
    #[error("ATP service expiry is invalid")]
    InvalidExpiry,
    /// Public gateways must require TLS.
    #[error("public ATP gateways require TLS")]
    PublicGatewayRequiresTls,
    /// Federation must name at least one trusted service.
    #[error("ATP federation requires at least one trusted service")]
    FederationRequiresTrustedService,
    /// Federation must document trust assumptions explicitly.
    #[error("ATP federation requires explicit trust assumptions")]
    FederationRequiresTrustAssumption,
    /// Federation trusted-service or assumption entry was blank.
    #[error("ATP federation entry is empty")]
    EmptyFederationEntry,
    /// Relay mailbox custody must require encrypted bytes.
    #[error("ATP relay mailbox storage must be encrypted-only")]
    MailboxMustBeEncrypted,
}

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

    #[test]
    fn serve_modes_map_to_cli_commands_and_state_machine_configs() {
        let rendezvous =
            AtpRendezvousServeConfig::for_mode(AtpDeploymentMode::Team, "rv-team", "team-ops")
                .expect("team rendezvous config");
        assert_eq!(
            rendezvous.command().words(),
            &["atp", "rendezvous", "serve"]
        );
        assert!(rendezvous.tls().requires_client_auth());
        assert_eq!(rendezvous.quotas().max_total_candidates, 512);

        let state_config = rendezvous
            .rendezvous_service_config()
            .expect("rendezvous service config");
        assert_eq!(state_config.service_id(), "rv-team");
        assert_eq!(state_config.default_quotas().max_total_candidates, 512);
        assert!(!state_config.log_peer_ids());
        assert!(state_config.retain_state_on_restart());

        let relay = AtpRelayServeConfig::for_mode(AtpDeploymentMode::Ci, "relay-ci", "ci-ops")
            .expect("ci relay config");
        assert_eq!(relay.command().words(), &["atp", "relay", "serve"]);
        assert_eq!(relay.max_active_reservations(), 32);
        assert!(!relay.logs().retain_state_on_restart);

        let relay_state = relay.relay_service_config().expect("relay state config");
        assert_eq!(relay_state.relay_id(), "relay-ci");
        assert_eq!(relay_state.max_active_reservations(), 32);
        assert!(relay_state.log_peer_ids());
        assert!(!relay_state.retain_state_on_restart());
    }

    #[test]
    fn config_covers_access_logs_mailbox_quotas_expiry_and_rate_limits() {
        let access = AtpAccessPolicy::private()
            .with_allowed_peer("peer:alice")
            .expect("allowed peer")
            .with_allowed_group("team:infra")
            .expect("allowed group");
        let relay =
            AtpRelayServeConfig::for_mode(AtpDeploymentMode::Team, "relay-team", "team-ops")
                .expect("relay config")
                .with_access_policy(access);

        assert_eq!(relay.access().allowed_peers(), &["peer:alice".to_owned()]);
        assert_eq!(relay.access().allowed_groups(), &["team:infra".to_owned()]);
        assert!(!relay.access().public_registration_enabled());
        assert_eq!(relay.mailbox_storage().storage_label(), "team-mailbox");
        assert!(relay.mailbox_storage().encrypted_only());
        assert!(relay.mailbox_storage().max_bytes() >= 64 * 1024 * MIB);
        assert_eq!(relay.expiry().mailbox_ttl_secs, 604_800);
        assert!(relay.rate_limits().max_packets_per_second >= 200_000);
        assert!(relay.logs().structured_events);
    }

    #[test]
    fn federation_is_disabled_by_default_and_opt_in_requires_trust() {
        let rendezvous =
            AtpRendezvousServeConfig::for_mode(AtpDeploymentMode::Personal, "rv-personal", "me")
                .expect("personal rendezvous");
        assert!(!rendezvous.federation().enabled());

        assert_eq!(
            AtpFederationPolicy::opt_in(Vec::new(), vec!["team roots are pinned".to_owned()])
                .expect_err("trusted service required"),
            AtpOpsConfigError::FederationRequiresTrustedService
        );
        assert_eq!(
            AtpFederationPolicy::opt_in(vec!["rv-team".to_owned()], Vec::new())
                .expect_err("trust assumption required"),
            AtpOpsConfigError::FederationRequiresTrustAssumption
        );

        let federated = rendezvous
            .with_federation_policy(
                AtpFederationPolicy::opt_in(
                    vec!["rv-team".to_owned()],
                    vec!["operators pin each service identity out of band".to_owned()],
                )
                .expect("federation policy"),
            )
            .expect("federated rendezvous config");
        assert!(federated.federation().enabled());
        assert_eq!(
            federated.federation().trusted_services(),
            &["rv-team".to_owned()]
        );
    }

    #[test]
    fn public_gateway_requires_tls_and_relay_visibility_stays_opaque() {
        let relay = AtpRelayServeConfig::for_mode(
            AtpDeploymentMode::PublicGateway,
            "relay-public",
            "public-ops",
        )
        .expect("public relay config");
        assert!(relay.tls().requires_tls());
        assert!(!relay.tls().requires_client_auth());
        assert!(relay.access().public_registration_enabled());
        assert_eq!(
            relay.content_visibility(),
            RelayContentVisibility::OpaqueEncryptedOnly
        );

        assert_eq!(
            relay
                .clone()
                .with_tls_policy(AtpTlsPolicy::DisabledLoopbackOnly)
                .expect_err("public gateway must require TLS"),
            AtpOpsConfigError::PublicGatewayRequiresTls
        );
    }
}