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
//! ATP Compatibility Adapters for alternative transports.
//!
//! Provides ATP-over-WebTransport, MASQUE/CONNECT-UDP, and TCP/TLS 443 fallback
//! adapters while preserving ATP's core guarantees. Native QUIC remains the
//! foundation; these are compatibility adapters for constrained environments.

pub mod integration_tests;
pub mod masque;
pub mod tcptls;
pub mod webtransport;

use crate::atp::object::ObjectId;
use crate::error::{Error, ErrorKind, Result};
use crate::types::TraceId;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::time::{Duration, SystemTime};

/// ATP adapter types for alternative transport protocols.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AdapterType {
    /// Native ATP over native QUIC (baseline)
    #[serde(rename = "native-quic")]
    NativeQuic,
    /// ATP over HTTP/3 with WebTransport streams/datagrams
    #[serde(rename = "webtransport")]
    WebTransport,
    /// ATP over MASQUE/CONNECT-UDP for proxy traversal
    #[serde(rename = "masque-connect-udp")]
    MasqueConnectUdp,
    /// ATP over TCP/TLS 443 fallback for hostile networks
    #[serde(rename = "tcp-tls-443")]
    TcpTlsFallback,
}

/// Adapter feature compatibility matrix showing supported/downgraded capabilities.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdapterParity {
    /// Object verification and manifest support
    pub object_support: FeatureSupport,
    /// Stream protocol support
    pub stream_support: FeatureSupport,
    /// Proof generation and verification
    pub proof_support: FeatureSupport,
    /// Path establishment and selection
    pub path_support: FeatureSupport,
    /// RaptorQ repair capabilities
    pub repair_support: FeatureSupport,
    /// Datagram transmission support
    pub datagram_support: FeatureSupport,
    /// Mailbox and inbox support
    pub mailbox_support: FeatureSupport,
    /// Swarm coordination support
    pub swarm_support: FeatureSupport,
    /// Diagnostic and telemetry support
    pub diagnostic_support: FeatureSupport,
}

/// Feature support levels for adapter capabilities.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum FeatureSupport {
    /// Full feature support with no limitations
    Full,
    /// Partial support with documented limitations
    Partial,
    /// Feature is downgraded with fallback behavior
    Downgraded,
    /// Feature is not supported
    Unsupported,
}

/// Adapter configuration for transport selection and policy.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdapterConfig {
    /// Preferred adapter types in order of preference
    pub preferred_adapters: Vec<AdapterType>,
    /// Downgrade policy when preferred adapters fail
    pub downgrade_policy: DowngradePolicy,
    /// Feature requirements that must be satisfied
    pub required_features: Vec<RequiredFeature>,
    /// Adapter-specific configurations
    pub adapter_configs: HashMap<AdapterType, AdapterSpecificConfig>,
    /// Performance caveat reporting configuration
    pub caveat_reporting: CaveatReporting,
}

/// Policy for adapter downgrading when preferred options fail.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DowngradePolicy {
    /// Fail if preferred adapter cannot be used
    Strict,
    /// Allow downgrade to any compatible adapter
    AllowDowngrade,
    /// Allow downgrade only to specified adapters
    AllowSpecific(Vec<AdapterType>),
    /// Use fallback adapter as last resort
    FallbackOnly(AdapterType),
}

/// Required feature that must be supported by chosen adapter.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RequiredFeature {
    /// Object verification must be supported
    ObjectVerification,
    /// Stream protocol must be supported
    StreamProtocol,
    /// Proof generation must be supported
    ProofGeneration,
    /// Repair capabilities must be supported
    RepairCapabilities,
    /// Swarm coordination must be supported
    SwarmCoordination,
}

/// Adapter-specific configuration parameters.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdapterSpecificConfig {
    /// Connection timeout for this adapter
    pub connection_timeout: Duration,
    /// Maximum concurrent streams/connections
    pub max_concurrent: usize,
    /// Adapter-specific feature flags
    pub feature_flags: HashMap<String, bool>,
    /// Performance tuning parameters
    pub performance_config: PerformanceConfig,
}

/// Performance configuration and caveat reporting.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceConfig {
    /// Buffer sizes for this adapter
    pub buffer_sizes: BufferSizes,
    /// Retry policy configuration
    pub retry_policy: RetryPolicy,
    /// Keep-alive settings
    pub keep_alive: KeepAliveConfig,
}

/// Buffer size configuration for adapter.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BufferSizes {
    /// Send buffer size in bytes
    pub send_buffer: usize,
    /// Receive buffer size in bytes
    pub recv_buffer: usize,
    /// Maximum frame size
    pub max_frame_size: usize,
}

/// Retry policy for adapter connections.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetryPolicy {
    /// Maximum retry attempts
    pub max_attempts: usize,
    /// Base retry delay
    pub base_delay: Duration,
    /// Maximum retry delay
    pub max_delay: Duration,
    /// Exponential backoff factor
    pub backoff_factor: f64,
}

/// Keep-alive configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeepAliveConfig {
    /// Keep-alive interval
    pub interval: Duration,
    /// Keep-alive timeout
    pub timeout: Duration,
    /// Enable keep-alive
    pub enabled: bool,
}

/// Caveat reporting configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CaveatReporting {
    /// Report nested transport performance issues
    pub report_performance: bool,
    /// Report head-of-line blocking issues
    pub report_hol_blocking: bool,
    /// Report diagnostic limitations
    pub report_diagnostic_limits: bool,
    /// Include detailed timing information
    pub include_timing: bool,
}

/// Adapter negotiation result with selected transport and capabilities.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdapterNegotiation {
    /// Selected adapter type
    pub selected_adapter: AdapterType,
    /// Negotiated feature parity
    pub feature_parity: AdapterParity,
    /// Downgrade reasons if applicable
    pub downgrade_reasons: Vec<DowngradeReason>,
    /// Performance caveats for this adapter
    pub performance_caveats: Vec<PerformanceCaveat>,
    /// Adapter-specific metadata
    pub adapter_metadata: AdapterMetadata,
    /// Negotiation timestamp
    pub negotiated_at: SystemTime,
}

/// Reason for adapter downgrading.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum DowngradeReason {
    /// Preferred adapter not available
    AdapterUnavailable,
    /// Feature requirements not met
    FeatureRequirementsNotMet(Vec<RequiredFeature>),
    /// Connection failed
    ConnectionFailed(String),
    /// Performance below threshold
    PerformanceBelowThreshold,
    /// Policy-enforced downgrade
    PolicyEnforced,
    /// Network conditions require fallback
    NetworkConditions,
}

/// Performance caveat for adapter selection.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PerformanceCaveat {
    /// Head-of-line blocking may occur
    HeadOfLineBlocking,
    /// Increased latency expected
    IncreasedLatency(Duration),
    /// Reduced throughput expected
    ReducedThroughput(f64),
    /// No multiplexing support
    NoMultiplexing,
    /// Limited concurrent streams
    LimitedConcurrency(usize),
    /// Nested transport overhead
    NestedTransportOverhead,
    /// Reliability concerns
    ReliabilityConcerns(String),
}

/// Adapter-specific metadata and configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdapterMetadata {
    /// Adapter version
    pub version: String,
    /// Transport path information
    pub path_info: TransportPath,
    /// Relay/proxy information if applicable
    pub relay_info: Option<RelayInfo>,
    /// Security parameters
    pub security_params: SecurityParams,
    /// Replay pointer for deterministic testing
    pub replay_pointer: Option<String>,
}

/// Transport path information.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransportPath {
    /// Local endpoint
    pub local_endpoint: String,
    /// Remote endpoint
    pub remote_endpoint: String,
    /// Intermediate hops if known
    pub intermediate_hops: Vec<String>,
    /// Path MTU if available
    pub path_mtu: Option<usize>,
}

/// Relay/proxy information.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RelayInfo {
    /// Relay/proxy address
    pub relay_address: String,
    /// Relay type (MASQUE, CONNECT, etc.)
    pub relay_type: String,
    /// Relay capabilities
    pub relay_capabilities: Vec<String>,
    /// Relay path ID for diagnostics
    pub relay_path_id: String,
}

/// Security parameters for adapter.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityParams {
    /// TLS version if applicable
    pub tls_version: Option<String>,
    /// Cipher suite
    pub cipher_suite: Option<String>,
    /// Certificate validation mode
    pub cert_validation: CertValidationMode,
    /// Additional security flags
    pub security_flags: Vec<String>,
}

/// Certificate validation mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CertValidationMode {
    /// Full certificate validation
    Full,
    /// Relaxed validation for testing
    Relaxed,
    /// Custom validation logic
    Custom,
}

/// ATP compatibility adapter manager.
#[derive(Debug)]
pub struct AdapterManager {
    /// Adapter configuration
    config: AdapterConfig,
    /// Adapter parity matrix
    parity_matrix: HashMap<AdapterType, AdapterParity>,
    /// Active adapter sessions
    active_sessions: HashMap<String, AdapterSession>,
    /// Adapter metrics
    metrics: AdapterMetrics,
}

/// Active adapter session.
#[derive(Debug, Clone)]
pub struct AdapterSession {
    /// Session identifier
    pub session_id: String,
    /// Negotiated adapter
    pub negotiation: AdapterNegotiation,
    /// Session start time
    pub started_at: SystemTime,
    /// Last activity time
    pub last_activity: SystemTime,
    /// Session statistics
    pub stats: SessionStats,
}

/// Session statistics for adapter.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionStats {
    /// Bytes sent
    pub bytes_sent: u64,
    /// Bytes received
    pub bytes_received: u64,
    /// Objects transferred
    pub objects_transferred: u64,
    /// Streams created
    pub streams_created: u64,
    /// Connection errors
    pub connection_errors: u64,
    /// Average latency
    pub avg_latency: Duration,
}

/// Adapter metrics for monitoring.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdapterMetrics {
    /// Sessions per adapter type
    pub sessions_by_adapter: HashMap<AdapterType, u64>,
    /// Downgrade frequency
    pub downgrade_frequency: HashMap<DowngradeReason, u64>,
    /// Performance caveat frequency
    pub caveat_frequency: HashMap<String, u64>,
    /// Average session duration by adapter
    pub avg_session_duration: HashMap<AdapterType, Duration>,
    /// Success rates by adapter
    pub success_rates: HashMap<AdapterType, f64>,
    /// Last updated timestamp
    pub last_updated: SystemTime,
}

impl AdapterManager {
    /// Create a new adapter manager with configuration.
    pub fn new(config: AdapterConfig) -> Self {
        let parity_matrix = Self::build_parity_matrix();
        Self {
            config,
            parity_matrix,
            active_sessions: HashMap::new(),
            metrics: AdapterMetrics::new(),
        }
    }

    /// Negotiate adapter for new connection based on requirements and policy.
    pub async fn negotiate_adapter(
        &mut self,
        requirements: &[RequiredFeature],
        trace_id: TraceId,
    ) -> Result<AdapterNegotiation> {
        // Try preferred adapters in order
        for &adapter_type in &self.config.preferred_adapters {
            if let Ok(negotiation) = self.try_adapter(adapter_type, requirements, trace_id).await {
                self.record_successful_negotiation(&negotiation);
                return Ok(negotiation);
            }
        }

        // Apply downgrade policy
        match &self.config.downgrade_policy {
            DowngradePolicy::Strict => Err(Error::new(ErrorKind::ConnectionRefused)),
            DowngradePolicy::AllowDowngrade => {
                self.try_fallback_adapters(requirements, trace_id).await
            }
            DowngradePolicy::AllowSpecific(allowed) => {
                self.try_specific_adapters(allowed, requirements, trace_id)
                    .await
            }
            DowngradePolicy::FallbackOnly(fallback) => {
                self.try_adapter(*fallback, requirements, trace_id).await
            }
        }
    }

    /// Start adapter session with negotiated parameters.
    pub async fn start_session(
        &mut self,
        negotiation: AdapterNegotiation,
        _object_id: ObjectId,
    ) -> Result<String> {
        let session_id = format!(
            "adapter-{:?}-{}",
            negotiation.selected_adapter,
            SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_millis()
        );

        let session = AdapterSession {
            session_id: session_id.clone(),
            negotiation,
            started_at: SystemTime::now(),
            last_activity: SystemTime::now(),
            stats: SessionStats::new(),
        };

        self.active_sessions.insert(session_id.clone(), session);
        self.update_metrics();

        Ok(session_id)
    }

    /// Get adapter feature parity for specific adapter type.
    pub fn get_adapter_parity(&self, adapter_type: AdapterType) -> Option<&AdapterParity> {
        self.parity_matrix.get(&adapter_type)
    }

    /// Get current adapter metrics.
    pub fn metrics(&self) -> &AdapterMetrics {
        &self.metrics
    }

    /// Build feature parity matrix for all adapter types.
    fn build_parity_matrix() -> HashMap<AdapterType, AdapterParity> {
        let mut matrix = HashMap::new();

        // Native QUIC - full support baseline
        matrix.insert(
            AdapterType::NativeQuic,
            AdapterParity {
                object_support: FeatureSupport::Full,
                stream_support: FeatureSupport::Full,
                proof_support: FeatureSupport::Full,
                path_support: FeatureSupport::Full,
                repair_support: FeatureSupport::Full,
                datagram_support: FeatureSupport::Full,
                mailbox_support: FeatureSupport::Full,
                swarm_support: FeatureSupport::Full,
                diagnostic_support: FeatureSupport::Full,
            },
        );

        // WebTransport - good support with some limitations
        matrix.insert(
            AdapterType::WebTransport,
            AdapterParity {
                object_support: FeatureSupport::Full,
                stream_support: FeatureSupport::Full,
                proof_support: FeatureSupport::Partial, // Limited by browser sandbox
                path_support: FeatureSupport::Partial,  // Limited path control
                repair_support: FeatureSupport::Partial, // Limited repair strategies
                datagram_support: FeatureSupport::Full,
                mailbox_support: FeatureSupport::Full,
                swarm_support: FeatureSupport::Partial, // Limited coordination
                diagnostic_support: FeatureSupport::Partial, // Limited diagnostics
            },
        );

        // MASQUE/CONNECT-UDP - proxy limitations
        matrix.insert(
            AdapterType::MasqueConnectUdp,
            AdapterParity {
                object_support: FeatureSupport::Full,
                stream_support: FeatureSupport::Downgraded, // Through UDP simulation
                proof_support: FeatureSupport::Full,
                path_support: FeatureSupport::Downgraded, // Via proxy path
                repair_support: FeatureSupport::Partial,  // Limited by proxy
                datagram_support: FeatureSupport::Full,
                mailbox_support: FeatureSupport::Full,
                swarm_support: FeatureSupport::Downgraded, // Proxy coordination limits
                diagnostic_support: FeatureSupport::Partial, // Proxy visibility limits
            },
        );

        // TCP/TLS 443 - significant limitations
        matrix.insert(
            AdapterType::TcpTlsFallback,
            AdapterParity {
                object_support: FeatureSupport::Full,
                stream_support: FeatureSupport::Downgraded, // Single stream, HOL blocking
                proof_support: FeatureSupport::Full,
                path_support: FeatureSupport::Downgraded, // Limited path establishment
                repair_support: FeatureSupport::Downgraded, // Serialized repair only
                datagram_support: FeatureSupport::Unsupported, // TCP doesn't support datagrams
                mailbox_support: FeatureSupport::Downgraded, // Polling-based only
                swarm_support: FeatureSupport::Downgraded, // Limited swarm coordination
                diagnostic_support: FeatureSupport::Partial, // TCP-level diagnostics only
            },
        );

        matrix
    }

    /// Try to negotiate specific adapter type.
    async fn try_adapter(
        &self,
        adapter_type: AdapterType,
        requirements: &[RequiredFeature],
        trace_id: TraceId,
    ) -> Result<AdapterNegotiation> {
        let parity = self
            .parity_matrix
            .get(&adapter_type)
            .ok_or_else(|| Error::new(ErrorKind::ConfigError))?;

        // Check if adapter meets requirements
        let mut downgrade_reasons = Vec::new();
        let mut performance_caveats = Vec::new();

        for requirement in requirements {
            if !self.check_feature_requirement(parity, requirement) {
                downgrade_reasons.push(DowngradeReason::FeatureRequirementsNotMet(vec![
                    requirement.clone(),
                ]));
            }
        }

        if !downgrade_reasons.is_empty() {
            return Err(Error::new(ErrorKind::ConnectionRefused).with_message(format!(
                "{adapter_type} does not satisfy required ATP adapter features: {downgrade_reasons:?}"
            )));
        }

        // Add adapter-specific performance caveats
        self.add_adapter_caveats(adapter_type, &mut performance_caveats);

        let negotiation = AdapterNegotiation {
            selected_adapter: adapter_type,
            feature_parity: parity.clone(),
            downgrade_reasons,
            performance_caveats,
            adapter_metadata: self.build_adapter_metadata(adapter_type, trace_id),
            negotiated_at: SystemTime::now(),
        };

        Ok(negotiation)
    }

    /// Try fallback adapters when preferred options fail.
    async fn try_fallback_adapters(
        &self,
        requirements: &[RequiredFeature],
        trace_id: TraceId,
    ) -> Result<AdapterNegotiation> {
        // Try adapters in capability order: WebTransport, MASQUE, TCP/TLS
        let fallback_order = [
            AdapterType::WebTransport,
            AdapterType::MasqueConnectUdp,
            AdapterType::TcpTlsFallback,
        ];

        for &adapter_type in &fallback_order {
            if let Ok(negotiation) = self.try_adapter(adapter_type, requirements, trace_id).await {
                return Ok(negotiation);
            }
        }

        Err(Error::new(ErrorKind::ConnectionRefused))
    }

    /// Try specific allowed adapters.
    async fn try_specific_adapters(
        &self,
        allowed: &[AdapterType],
        requirements: &[RequiredFeature],
        trace_id: TraceId,
    ) -> Result<AdapterNegotiation> {
        for &adapter_type in allowed {
            if let Ok(negotiation) = self.try_adapter(adapter_type, requirements, trace_id).await {
                return Ok(negotiation);
            }
        }

        Err(Error::new(ErrorKind::ConnectionRefused))
    }

    /// Check if adapter feature parity meets requirement.
    fn check_feature_requirement(
        &self,
        parity: &AdapterParity,
        requirement: &RequiredFeature,
    ) -> bool {
        match requirement {
            RequiredFeature::ObjectVerification => matches!(
                parity.object_support,
                FeatureSupport::Full | FeatureSupport::Partial
            ),
            RequiredFeature::StreamProtocol => matches!(
                parity.stream_support,
                FeatureSupport::Full | FeatureSupport::Partial | FeatureSupport::Downgraded
            ),
            RequiredFeature::ProofGeneration => matches!(
                parity.proof_support,
                FeatureSupport::Full | FeatureSupport::Partial
            ),
            RequiredFeature::RepairCapabilities => matches!(
                parity.repair_support,
                FeatureSupport::Full | FeatureSupport::Partial | FeatureSupport::Downgraded
            ),
            RequiredFeature::SwarmCoordination => matches!(
                parity.swarm_support,
                FeatureSupport::Full | FeatureSupport::Partial | FeatureSupport::Downgraded
            ),
        }
    }

    /// Add adapter-specific performance caveats.
    fn add_adapter_caveats(&self, adapter_type: AdapterType, caveats: &mut Vec<PerformanceCaveat>) {
        match adapter_type {
            AdapterType::NativeQuic => {
                // No caveats for native implementation
            }
            AdapterType::WebTransport => {
                caveats.push(PerformanceCaveat::NestedTransportOverhead);
            }
            AdapterType::MasqueConnectUdp => {
                caveats.push(PerformanceCaveat::NestedTransportOverhead);
                caveats.push(PerformanceCaveat::IncreasedLatency(Duration::from_millis(
                    50,
                )));
            }
            AdapterType::TcpTlsFallback => {
                caveats.push(PerformanceCaveat::HeadOfLineBlocking);
                caveats.push(PerformanceCaveat::NoMultiplexing);
                caveats.push(PerformanceCaveat::IncreasedLatency(Duration::from_millis(
                    100,
                )));
                caveats.push(PerformanceCaveat::ReducedThroughput(0.7)); // 30% throughput reduction
            }
        }
    }

    /// Build adapter metadata.
    fn build_adapter_metadata(
        &self,
        _adapter_type: AdapterType,
        trace_id: TraceId,
    ) -> AdapterMetadata {
        AdapterMetadata {
            version: "1.0.0".to_string(),
            path_info: TransportPath {
                local_endpoint: "0.0.0.0:0".to_string(),
                remote_endpoint: "example.com:443".to_string(),
                intermediate_hops: Vec::new(),
                path_mtu: Some(1500),
            },
            relay_info: None, // Would be populated for MASQUE adapters
            security_params: SecurityParams {
                tls_version: Some("TLS 1.3".to_string()),
                cipher_suite: Some("TLS_AES_128_GCM_SHA256".to_string()),
                cert_validation: CertValidationMode::Full,
                security_flags: vec!["ALPN".to_string()],
            },
            replay_pointer: Some(format!("trace-{}", trace_id.as_u128())),
        }
    }

    /// Record successful negotiation for metrics.
    fn record_successful_negotiation(&mut self, negotiation: &AdapterNegotiation) {
        *self
            .metrics
            .sessions_by_adapter
            .entry(negotiation.selected_adapter)
            .or_insert(0) += 1;
        self.metrics.last_updated = SystemTime::now();
    }

    /// Update adapter metrics.
    fn update_metrics(&mut self) {
        self.metrics.last_updated = SystemTime::now();
    }
}

impl Default for AdapterConfig {
    fn default() -> Self {
        Self {
            preferred_adapters: vec![
                AdapterType::NativeQuic,
                AdapterType::WebTransport,
                AdapterType::MasqueConnectUdp,
                AdapterType::TcpTlsFallback,
            ],
            downgrade_policy: DowngradePolicy::AllowDowngrade,
            required_features: vec![RequiredFeature::ObjectVerification],
            adapter_configs: HashMap::new(),
            caveat_reporting: CaveatReporting {
                report_performance: true,
                report_hol_blocking: true,
                report_diagnostic_limits: true,
                include_timing: true,
            },
        }
    }
}

impl SessionStats {
    fn new() -> Self {
        Self {
            bytes_sent: 0,
            bytes_received: 0,
            objects_transferred: 0,
            streams_created: 0,
            connection_errors: 0,
            avg_latency: Duration::from_millis(0),
        }
    }
}

impl AdapterMetrics {
    fn new() -> Self {
        Self {
            sessions_by_adapter: HashMap::new(),
            downgrade_frequency: HashMap::new(),
            caveat_frequency: HashMap::new(),
            avg_session_duration: HashMap::new(),
            success_rates: HashMap::new(),
            last_updated: SystemTime::now(),
        }
    }
}

impl fmt::Display for AdapterType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            AdapterType::NativeQuic => write!(f, "native-quic"),
            AdapterType::WebTransport => write!(f, "webtransport"),
            AdapterType::MasqueConnectUdp => write!(f, "masque-connect-udp"),
            AdapterType::TcpTlsFallback => write!(f, "tcp-tls-443"),
        }
    }
}

impl fmt::Display for FeatureSupport {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            FeatureSupport::Full => write!(f, "full"),
            FeatureSupport::Partial => write!(f, "partial"),
            FeatureSupport::Downgraded => write!(f, "downgraded"),
            FeatureSupport::Unsupported => write!(f, "unsupported"),
        }
    }
}

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

    #[test]
    fn test_adapter_parity_matrix() {
        let manager = AdapterManager::new(AdapterConfig::default());

        // Native QUIC should have full support
        let native_parity = manager.get_adapter_parity(AdapterType::NativeQuic).unwrap();
        assert_eq!(native_parity.object_support, FeatureSupport::Full);
        assert_eq!(native_parity.stream_support, FeatureSupport::Full);

        // TCP fallback should have limited datagram support
        let tcp_parity = manager
            .get_adapter_parity(AdapterType::TcpTlsFallback)
            .unwrap();
        assert_eq!(tcp_parity.datagram_support, FeatureSupport::Unsupported);
        assert_eq!(tcp_parity.stream_support, FeatureSupport::Downgraded);
    }

    #[test]
    fn test_feature_requirements() {
        let manager = AdapterManager::new(AdapterConfig::default());
        let tcp_parity = manager
            .get_adapter_parity(AdapterType::TcpTlsFallback)
            .unwrap();

        // Object verification should be supported
        assert!(
            manager.check_feature_requirement(tcp_parity, &RequiredFeature::ObjectVerification)
        );

        // Stream protocol should be supported (even if downgraded)
        assert!(manager.check_feature_requirement(tcp_parity, &RequiredFeature::StreamProtocol));
    }

    #[test]
    fn test_adapter_negotiation() {
        block_on(async {
            let mut manager = AdapterManager::new(AdapterConfig::default());
            let requirements = vec![RequiredFeature::ObjectVerification];
            let trace_id = TraceId::from_parts(1, 1);

            let negotiation = manager
                .negotiate_adapter(&requirements, trace_id)
                .await
                .unwrap();
            assert_eq!(negotiation.selected_adapter, AdapterType::NativeQuic);
        });
    }

    #[test]
    fn adapter_negotiation_skips_unmet_feature_requirements() {
        block_on(async {
            let mut manager = AdapterManager::new(AdapterConfig::default());
            manager.config.preferred_adapters =
                vec![AdapterType::NativeQuic, AdapterType::WebTransport];
            manager
                .parity_matrix
                .get_mut(&AdapterType::NativeQuic)
                .expect("native parity")
                .object_support = FeatureSupport::Unsupported;

            let negotiation = manager
                .negotiate_adapter(
                    &[RequiredFeature::ObjectVerification],
                    TraceId::from_parts(1, 1),
                )
                .await
                .expect("compatible fallback should be selected");

            assert_eq!(negotiation.selected_adapter, AdapterType::WebTransport);
            assert!(
                negotiation.downgrade_reasons.is_empty(),
                "selected adapter should satisfy the requested features"
            );
        });
    }

    #[test]
    fn test_adapter_display() {
        assert_eq!(format!("{}", AdapterType::NativeQuic), "native-quic");
        assert_eq!(format!("{}", AdapterType::WebTransport), "webtransport");
        assert_eq!(
            format!("{}", AdapterType::MasqueConnectUdp),
            "masque-connect-udp"
        );
        assert_eq!(format!("{}", AdapterType::TcpTlsFallback), "tcp-tls-443");
    }

    #[test]
    fn test_feature_support_display() {
        assert_eq!(format!("{}", FeatureSupport::Full), "full");
        assert_eq!(format!("{}", FeatureSupport::Partial), "partial");
        assert_eq!(format!("{}", FeatureSupport::Downgraded), "downgraded");
        assert_eq!(format!("{}", FeatureSupport::Unsupported), "unsupported");
    }
}