asupersync 0.3.6

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
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
//! Content-defined chunking and deduplication for ATP-C6.
//!
//! This module implements content-defined chunking (CDC) algorithms and deduplication
//! infrastructure for efficient cross-transfer chunk reuse. Provides rolling hash
//! boundary detection, chunk identity management, and secure cache lookup that doesn't
//! leak unauthorized object graph membership.

use super::ChunkingProfileError;
use sha2::{Digest, Sha256};
use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque};

/// Parameters for content-defined chunking.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CdcParameters {
    pub window_size: usize,
    pub min_chunk_size: u64,
    pub max_chunk_size: u64,
    pub normalization_constant: u64,
}

/// Criteria for chunk reuse in deduplication.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChunkReuseCriteria {
    pub max_age_seconds: u64,
    pub min_proof_strength: crate::atp::manifest::ProofStrength,
    pub require_same_algorithm: bool,
}

/// Verification data for chunk integrity.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ChunkVerification {
    pub algorithm: String,
    pub proof_strength: crate::atp::manifest::ProofStrength,
}

/// Chunk data result from CDC boundary computation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CdcChunkData {
    pub byte_offset: u64,
    pub size_bytes: u64,
    pub content_hash: [u8; 32],
}

/// Conservative lower bound for incremental re-sync bytes-on-wire.
#[derive(Debug, Clone, PartialEq)]
pub struct DeltaFloorEstimate {
    pub receiver_unique_chunk_count: usize,
    pub sender_unique_chunk_count: usize,
    pub shared_chunk_count: usize,
    pub sender_missing_chunk_count: usize,
    pub receiver_stale_chunk_count: usize,
    pub sender_missing_bytes: u64,
    pub receiver_stale_bytes: u64,
    pub symmetric_difference_bytes: u64,
    pub estimated_floor_bytes_on_wire: u64,
    pub observed_gap_to_floor: Option<f64>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct ChunkSetKey {
    content_hash: [u8; 32],
    size_bytes: u64,
}

impl From<&CdcChunkData> for ChunkSetKey {
    fn from(chunk: &CdcChunkData) -> Self {
        Self {
            content_hash: chunk.content_hash,
            size_bytes: chunk.size_bytes,
        }
    }
}

/// Content-defined chunking engine with rolling hash boundary detection.
pub struct CdcEngine;

impl CdcEngine {
    /// Create a new CDC engine.
    pub fn new() -> Self {
        Self
    }

    /// Compute content-defined chunk boundaries using FastCDC-style gear hash.
    pub fn compute_cdc_boundaries(
        &mut self,
        data: &[u8],
        params: &CdcParameters,
    ) -> Result<Vec<CdcChunkData>, ChunkingProfileError> {
        Self::validate_params(params)?;

        if data.is_empty() {
            return Ok(Vec::new());
        }

        let data_len = Self::usize_to_u64(data.len(), "CDC input length")?;
        let mut chunks = Vec::new();
        let mut rolling_hash = RollingHash::new(params.window_size);
        let mut last_boundary = 0u64;

        // FastCDC uses a stricter mask before the target size and a looser mask
        // after it, which keeps average chunks near the requested range without
        // forcing every chunk to the maximum size.
        let mask_bits = Self::compute_mask_bits_from_constant(params.normalization_constant);
        let normal_mask = Self::boundary_mask(mask_bits);
        let small_mask = Self::boundary_mask(mask_bits.saturating_add(1).min(63));
        let large_mask = Self::boundary_mask(mask_bits.saturating_sub(1).max(1));
        let target_chunk_size =
            params.min_chunk_size + (params.max_chunk_size - params.min_chunk_size) / 2;

        for (i, &byte) in data.iter().enumerate() {
            rolling_hash.update(byte);

            let current_pos = Self::usize_to_u64(i, "CDC boundary index")?
                .checked_add(1)
                .ok_or_else(|| {
                    ChunkingProfileError::InvalidChunkParameters(format!(
                        "CDC boundary position overflow at index {i}"
                    ))
                })?;
            let chunk_size = current_pos - last_boundary;

            if Self::should_cut_chunk(
                data,
                i + 1,
                chunk_size,
                rolling_hash.hash(),
                params,
                target_chunk_size,
                small_mask,
                normal_mask,
                large_mask,
            ) {
                Self::push_chunk(&mut chunks, data, last_boundary, current_pos)?;
                last_boundary = current_pos;
                rolling_hash.reset();
            }
        }

        // Add final chunk if needed
        if last_boundary < data_len {
            let final_size = data_len - last_boundary;
            if final_size < params.min_chunk_size && !chunks.is_empty() {
                let previous_start = chunks.last().map_or(0, |chunk| chunk.byte_offset);
                if data_len - previous_start <= params.max_chunk_size {
                    chunks.pop();
                    last_boundary = previous_start;
                }
            }
            Self::push_chunk(&mut chunks, data, last_boundary, data_len)?;
        }

        Ok(chunks)
    }

    /// Estimate the absolute information floor for a re-sync where the receiver
    /// already has `receiver_existing` chunks and the sender wants
    /// `sender_target`. The floor is the unique target chunk bytes absent from
    /// the receiver's content-addressed set; any real protocol can only exceed
    /// it with manifest/reconciliation/FEC/auth overhead.
    pub fn estimate_delta_floor(
        receiver_existing: &[CdcChunkData],
        sender_target: &[CdcChunkData],
        observed_bytes_on_wire: Option<u64>,
    ) -> DeltaFloorEstimate {
        let receiver_set: BTreeSet<ChunkSetKey> =
            receiver_existing.iter().map(ChunkSetKey::from).collect();
        let sender_set: BTreeSet<ChunkSetKey> =
            sender_target.iter().map(ChunkSetKey::from).collect();

        let shared_chunk_count = sender_set.intersection(&receiver_set).count();
        let sender_missing_chunk_count = sender_set.difference(&receiver_set).count();
        let receiver_stale_chunk_count = receiver_set.difference(&sender_set).count();
        let sender_missing_bytes = sender_set
            .difference(&receiver_set)
            .fold(0u64, |sum, chunk| sum.saturating_add(chunk.size_bytes));
        let receiver_stale_bytes = receiver_set
            .difference(&sender_set)
            .fold(0u64, |sum, chunk| sum.saturating_add(chunk.size_bytes));
        let symmetric_difference_bytes = sender_missing_bytes.saturating_add(receiver_stale_bytes);
        let observed_gap_to_floor = observed_bytes_on_wire.and_then(|observed| {
            if sender_missing_bytes == 0 {
                None
            } else {
                Some(observed as f64 / sender_missing_bytes as f64)
            }
        });

        DeltaFloorEstimate {
            receiver_unique_chunk_count: receiver_set.len(),
            sender_unique_chunk_count: sender_set.len(),
            shared_chunk_count,
            sender_missing_chunk_count,
            receiver_stale_chunk_count,
            sender_missing_bytes,
            receiver_stale_bytes,
            symmetric_difference_bytes,
            estimated_floor_bytes_on_wire: sender_missing_bytes,
            observed_gap_to_floor,
        }
    }

    fn validate_params(params: &CdcParameters) -> Result<(), ChunkingProfileError> {
        if params.window_size == 0 {
            return Err(ChunkingProfileError::InvalidChunkParameters(
                "CDC window size must be greater than zero".to_string(),
            ));
        }
        if params.min_chunk_size == 0 {
            return Err(ChunkingProfileError::InvalidChunkParameters(
                "CDC min chunk size must be greater than zero".to_string(),
            ));
        }
        if params.max_chunk_size < params.min_chunk_size {
            return Err(ChunkingProfileError::InvalidChunkParameters(
                "CDC max chunk size must be greater than or equal to min chunk size".to_string(),
            ));
        }

        Ok(())
    }

    #[allow(clippy::too_many_arguments)]
    fn should_cut_chunk(
        data: &[u8],
        current_pos: usize,
        chunk_size: u64,
        hash: u64,
        params: &CdcParameters,
        target_chunk_size: u64,
        small_mask: u64,
        normal_mask: u64,
        large_mask: u64,
    ) -> bool {
        if chunk_size < params.min_chunk_size {
            return false;
        }
        if chunk_size >= params.max_chunk_size {
            return true;
        }
        if Self::is_structural_boundary(data, current_pos) {
            return true;
        }

        let mask = match chunk_size.cmp(&target_chunk_size) {
            std::cmp::Ordering::Less => small_mask,
            std::cmp::Ordering::Equal => normal_mask,
            std::cmp::Ordering::Greater => large_mask,
        };

        (hash & mask) == 0
    }

    fn is_structural_boundary(data: &[u8], current_pos: usize) -> bool {
        current_pos > 0
            && current_pos <= data.len()
            && matches!(data[current_pos - 1], b'\n' | b'\r')
    }

    /// Compute mask bits from normalization constant.
    fn compute_mask_bits_from_constant(constant: u64) -> u32 {
        // Use hash-based mapping to ensure deterministic chunking
        // Each unique constant maps to a unique mask bit value
        let mut hasher = Sha256::new();
        hasher.update(constant.to_be_bytes());
        let hash = hasher.finalize();

        // Extract first 4 bytes as u32 and map to range 8-23
        let hash_u32 = u32::from_be_bytes([hash[0], hash[1], hash[2], hash[3]]);
        let bits = (hash_u32 % 16) + 8; // Range: 8-23 bits (256B to 8MB average)
        bits
    }

    fn boundary_mask(bits: u32) -> u64 {
        if bits >= 64 {
            u64::MAX
        } else {
            (1u64 << bits) - 1
        }
    }

    fn push_chunk(
        chunks: &mut Vec<CdcChunkData>,
        data: &[u8],
        start: u64,
        end: u64,
    ) -> Result<(), ChunkingProfileError> {
        let start_index = Self::u64_to_usize(start, "CDC chunk start")?;
        let end_index = Self::u64_to_usize(end, "CDC chunk end")?;
        let chunk_data = &data[start_index..end_index];
        let content_hash = Self::compute_content_hash(chunk_data);

        chunks.push(CdcChunkData {
            byte_offset: start,
            size_bytes: end - start,
            content_hash,
        });

        Ok(())
    }

    fn usize_to_u64(value: usize, label: &str) -> Result<u64, ChunkingProfileError> {
        u64::try_from(value).map_err(|_| {
            ChunkingProfileError::InvalidChunkParameters(format!(
                "{label} {value} exceeds u64::MAX"
            ))
        })
    }

    fn u64_to_usize(value: u64, label: &str) -> Result<usize, ChunkingProfileError> {
        usize::try_from(value).map_err(|_| {
            ChunkingProfileError::InvalidChunkParameters(format!(
                "{label} {value} exceeds usize::MAX"
            ))
        })
    }

    /// Compute SHA-256 hash of chunk data.
    fn compute_content_hash(data: &[u8]) -> [u8; 32] {
        let mut hasher = Sha256::new();
        hasher.update(data);
        hasher.finalize().into()
    }
}

const GEAR_TABLE: [u64; 256] = build_gear_table();

const fn build_gear_table() -> [u64; 256] {
    let mut table = [0u64; 256];
    let mut index = 0usize;
    while index < 256 {
        table[index] = splitmix64((index as u64).wrapping_mul(0x9e37_79b9_7f4a_7c15));
        index += 1;
    }
    table
}

const fn splitmix64(mut value: u64) -> u64 {
    value = value.wrapping_add(0x9e37_79b9_7f4a_7c15);
    let mut mixed = value;
    mixed = (mixed ^ (mixed >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9);
    mixed = (mixed ^ (mixed >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb);
    mixed ^ (mixed >> 31)
}

/// Gear hash for FastCDC content-defined chunking.
pub struct RollingHash {
    hash: u64,
}

impl RollingHash {
    /// Create new gear hash. The window size is retained in the API because
    /// callers provide it as part of the CDC profile, but FastCDC gear hashing
    /// advances one byte at a time and does not need to subtract an old byte.
    pub fn new(_window_size: usize) -> Self {
        Self { hash: 0 }
    }

    /// Add byte to the gear hash.
    pub fn update(&mut self, byte: u8) {
        self.hash = (self.hash << 1).wrapping_add(GEAR_TABLE[usize::from(byte)]);
    }

    /// Advance the gear hash. The old byte is intentionally ignored.
    pub fn roll(&mut self, _old_byte: u8, new_byte: u8) {
        self.update(new_byte);
    }

    /// Get current hash value.
    pub fn hash(&self) -> u64 {
        self.hash
    }

    /// Reset the gear hash at a chunk boundary.
    pub fn reset(&mut self) {
        self.hash = 0;
    }
}

/// Chunk identity for deduplication.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ChunkIdentity {
    /// SHA-256 hash of chunk content.
    pub content_hash: [u8; 32],
    /// Chunk size in bytes.
    pub size_bytes: u64,
    /// Capability scope for authorized access.
    pub capability_scope: String,
    /// Chunk verification data.
    pub verification: ChunkVerification,
}

impl ChunkIdentity {
    /// Create chunk identity directly from data.
    pub fn from_data(
        data: &[u8],
        capability_scope: &str,
        proof_strength: crate::atp::manifest::ProofStrength,
    ) -> Self {
        let content_hash = Self::compute_content_hash(data);
        let size_bytes = data.len() as u64;
        Self {
            content_hash,
            size_bytes,
            capability_scope: capability_scope.to_string(),
            verification: ChunkVerification {
                algorithm: "sha256".to_string(),
                proof_strength,
            },
        }
    }

    /// Compute SHA-256 hash of data.
    fn compute_content_hash(data: &[u8]) -> [u8; 32] {
        let mut hasher = Sha256::new();
        hasher.update(data);
        hasher.finalize().into()
    }

    /// Get identity string for deduplication keys.
    pub fn identity_string(&self) -> String {
        let hash_hex = hex_hash(&self.content_hash);
        format!("{}:{}:{}", hash_hex, self.size_bytes, self.capability_scope)
    }
}

/// Chunk cache for cross-transfer reuse.
pub struct ChunkCache {
    /// Mapping from chunk identity to cached chunk data.
    chunks: HashMap<ChunkIdentity, CachedChunk>,
    /// Index by content hash for fast lookup.
    content_hash_index: HashMap<[u8; 32], BTreeSet<ChunkIdentity>>,
    /// Current cache size in bytes.
    current_size: u64,
    /// Maximum cache size in bytes.
    max_size: u64,
    /// Cache hit count.
    cache_hits: u64,
    /// Cache miss count.
    cache_misses: u64,
}

/// Cached chunk data with metadata.
#[derive(Debug, Clone)]
pub struct CachedChunk {
    /// Chunk data.
    pub data: Vec<u8>,
    /// When this chunk was last accessed.
    pub last_accessed: std::time::SystemTime,
    /// How many times this chunk has been reused.
    pub reuse_count: u32,
    /// Original source object (for debugging/tracing).
    pub source_object: Option<String>,
}

impl ChunkCache {
    /// Create new chunk cache with size limit.
    pub fn new(max_size: u64) -> Self {
        Self {
            chunks: HashMap::new(),
            content_hash_index: HashMap::new(),
            current_size: 0,
            max_size,
            cache_hits: 0,
            cache_misses: 0,
        }
    }

    /// Store chunk in cache.
    pub fn store_chunk(
        &mut self,
        identity: &ChunkIdentity,
        data: &[u8],
    ) -> Result<(), ChunkingProfileError> {
        let data_len = u64::try_from(data.len()).map_err(|_| {
            ChunkingProfileError::InvalidChunkParameters(
                "chunk data length exceeds supported size".to_string(),
            )
        })?;

        // Validate chunk data matches identity
        if data_len != identity.size_bytes {
            return Err(ChunkingProfileError::InvalidChunkParameters(
                "chunk data size doesn't match identity".to_string(),
            ));
        }
        if data_len > self.max_size {
            return Err(ChunkingProfileError::InvalidChunkParameters(
                "chunk data exceeds cache size limit".to_string(),
            ));
        }

        let computed_hash = ChunkIdentity::compute_content_hash(data);
        if computed_hash != identity.content_hash {
            return Err(ChunkingProfileError::InvalidChunkParameters(
                "chunk data hash doesn't match identity".to_string(),
            ));
        }

        // Replacement must not double-count the same identity.
        self.remove_chunk(identity);

        // Make space if needed
        let target_size = self.max_size.saturating_sub(data_len);
        while self.current_size > target_size && !self.chunks.is_empty() {
            self.evict_least_recently_used();
        }

        // Store chunk
        let cached_chunk = CachedChunk {
            data: data.to_vec(),
            last_accessed: std::time::SystemTime::now(),
            reuse_count: 0,
            source_object: None,
        };

        self.current_size += data_len;

        // Update content hash index
        self.content_hash_index
            .entry(identity.content_hash)
            .or_default()
            .insert(identity.clone());

        self.chunks.insert(identity.clone(), cached_chunk);

        Ok(())
    }

    /// Lookup chunk by identity.
    pub fn lookup_chunk(&mut self, identity: &ChunkIdentity) -> Option<Vec<u8>> {
        if let Some(chunk) = self.chunks.get_mut(identity) {
            chunk.last_accessed = std::time::SystemTime::now();
            chunk.reuse_count += 1;
            self.cache_hits += 1;
            Some(chunk.data.clone())
        } else {
            self.cache_misses += 1;
            None
        }
    }

    /// Retrieve chunk by identity.
    pub fn retrieve_chunk(
        &mut self,
        identity: &ChunkIdentity,
    ) -> Result<Option<Vec<u8>>, ChunkingProfileError> {
        Ok(self.lookup_chunk(identity))
    }

    /// Find chunks with same content hash but different context.
    pub fn find_similar_chunks(&self, content_hash: [u8; 32]) -> Vec<&ChunkIdentity> {
        self.content_hash_index
            .get(&content_hash)
            .map(|identities| identities.iter().collect())
            .unwrap_or_default()
    }

    /// Check if chunk can be reused given capability scope.
    pub fn can_reuse_chunk(&self, chunk_identity: &ChunkIdentity, requesting_scope: &str) -> bool {
        // Empty scopes are explicit globally reusable cache entries. Non-empty
        // scopes must match the requester's registered dedupe context.
        chunk_identity.capability_scope.is_empty()
            || chunk_identity.capability_scope == requesting_scope
    }

    fn meets_reuse_criteria(
        &self,
        chunk_identity: &ChunkIdentity,
        criteria: &ChunkReuseCriteria,
    ) -> bool {
        if chunk_identity.verification.proof_strength < criteria.min_proof_strength {
            return false;
        }

        if criteria.require_same_algorithm && chunk_identity.verification.algorithm != "sha256" {
            return false;
        }

        let Some(cached_chunk) = self.chunks.get(chunk_identity) else {
            return false;
        };

        let Ok(age) = std::time::SystemTime::now().duration_since(cached_chunk.last_accessed)
        else {
            return false;
        };

        age.as_secs() <= criteria.max_age_seconds
    }

    /// Evict least recently used chunk.
    fn evict_least_recently_used(&mut self) {
        let oldest_identity = self
            .chunks
            .iter()
            .min_by_key(|(_, chunk)| chunk.last_accessed)
            .map(|(identity, _)| identity.clone());

        if let Some(identity) = oldest_identity {
            self.remove_chunk(&identity);
        }
    }

    /// Remove chunk from cache.
    fn remove_chunk(&mut self, identity: &ChunkIdentity) {
        if self.chunks.remove(identity).is_some() {
            self.current_size = self.current_size.saturating_sub(identity.size_bytes);

            // Update content hash index
            if let Some(identities) = self.content_hash_index.get_mut(&identity.content_hash) {
                identities.remove(identity);
                if identities.is_empty() {
                    self.content_hash_index.remove(&identity.content_hash);
                }
            }
        }
    }

    /// Get cache statistics (alias for backward compatibility).
    pub fn stats(&self) -> ChunkCacheStats {
        self.get_statistics()
    }

    /// Get cache statistics.
    pub fn get_statistics(&self) -> ChunkCacheStats {
        let total_reuse_count: u32 = self.chunks.values().map(|c| c.reuse_count).sum();

        ChunkCacheStats {
            total_chunks: self.chunks.len(),
            current_size: self.current_size,
            max_size: self.max_size,
            total_reuse_count,
            utilization: if self.max_size == 0 {
                0.0
            } else {
                self.current_size as f64 / self.max_size as f64
            },
            cache_hits: self.cache_hits,
            cache_misses: self.cache_misses,
        }
    }
}

/// Chunk cache statistics.
#[derive(Debug, Clone)]
pub struct ChunkCacheStats {
    /// Total number of cached chunks.
    pub total_chunks: usize,
    /// Current cache size in bytes.
    pub current_size: u64,
    /// Maximum cache size in bytes.
    pub max_size: u64,
    /// Total number of chunk reuses.
    pub total_reuse_count: u32,
    /// Cache utilization (0.0 to 1.0).
    pub utilization: f64,
    /// Number of cache hits.
    pub cache_hits: u64,
    /// Number of cache misses.
    pub cache_misses: u64,
}

/// br-asupersync-7tcipb item 4: default upper bound on the number of distinct
/// transfers whose per-transfer chunk/stat state [`ChunkReuseManager`] retains.
/// `transfer_chunks` / `transfer_stats` are keyed by `transfer_id`, which can be
/// attacker-influenced, so without a bound a peer that issues a flood of unique
/// transfer ids would grow these maps without limit (memory DoS). The cache
/// itself is already LRU-capped; this bounds the per-transfer key space.
const DEFAULT_MAX_TRACKED_TRANSFERS: usize = 4096;

/// Cross-transfer chunk reuse manager.
pub struct ChunkReuseManager {
    /// Chunk cache.
    cache: ChunkCache,
    /// Registered transfer chunks.
    transfer_chunks: BTreeMap<String, Vec<ChunkIdentity>>,
    /// Reuse statistics per transfer.
    transfer_stats: BTreeMap<String, TransferReuseStats>,
    /// br-asupersync-7tcipb item 4: FIFO insertion order of distinct tracked
    /// transfer ids (each id appears at most once). Drives oldest-first eviction
    /// once the tracked-transfer count would exceed `max_tracked_transfers`.
    transfer_order: VecDeque<String>,
    /// br-asupersync-7tcipb item 4: upper bound on distinct tracked transfers.
    max_tracked_transfers: usize,
}

/// Reuse statistics for a transfer.
#[derive(Debug, Clone)]
pub struct TransferReuseStats {
    pub total_chunks_reused: u64,
    pub bytes_saved: u64,
    pub deduplication_ratio: f64,
}

impl ChunkReuseManager {
    /// Create new chunk reuse manager.
    pub fn new() -> Self {
        Self::with_max_tracked_transfers(DEFAULT_MAX_TRACKED_TRANSFERS)
    }

    /// br-asupersync-7tcipb item 4: create a manager that retains per-transfer
    /// state for at most `max_tracked_transfers` distinct transfers. Once that
    /// bound would be exceeded, the oldest-registered transfer is evicted from
    /// both `transfer_chunks` and `transfer_stats` (FIFO), so a peer flooding
    /// the manager with unique transfer ids cannot grow them without bound.
    /// `max_tracked_transfers` is clamped to at least 1.
    #[must_use]
    pub fn with_max_tracked_transfers(max_tracked_transfers: usize) -> Self {
        Self {
            cache: ChunkCache::new(100 * 1024 * 1024), // 100MB default cache
            transfer_chunks: BTreeMap::new(),
            transfer_stats: BTreeMap::new(),
            transfer_order: VecDeque::new(),
            max_tracked_transfers: max_tracked_transfers.max(1),
        }
    }

    /// br-asupersync-7tcipb item 4: number of distinct transfers currently
    /// tracked, always `<= max_tracked_transfers`. Exposed for observability so
    /// operators can confirm the dedupe manager is not growing without bound.
    #[must_use]
    pub fn tracked_transfer_count(&self) -> usize {
        self.transfer_order.len()
    }

    /// br-asupersync-7tcipb item 4: record a (possibly new) transfer id in FIFO
    /// order, evicting the oldest transfer(s) from both per-transfer maps if
    /// tracking a new id would exceed `max_tracked_transfers`.
    ///
    /// MUST be called BEFORE inserting `transfer_id` into either map: "absent
    /// from both maps" is how a first-seen id is detected, which keeps each id
    /// enqueued exactly once and prevents an id present in only one map from
    /// being double-counted.
    fn note_transfer(&mut self, transfer_id: &str) {
        let already_tracked = self.transfer_chunks.contains_key(transfer_id)
            || self.transfer_stats.contains_key(transfer_id);
        if already_tracked {
            return;
        }
        while self.transfer_order.len() >= self.max_tracked_transfers {
            let Some(oldest) = self.transfer_order.pop_front() else {
                break;
            };
            self.transfer_chunks.remove(&oldest);
            self.transfer_stats.remove(&oldest);
        }
        self.transfer_order.push_back(transfer_id.to_string());
    }

    /// Register a chunk for a transfer.
    pub fn register_transfer_chunk(
        &mut self,
        transfer_id: &str,
        identity: &ChunkIdentity,
    ) -> Result<(), ChunkingProfileError> {
        // br-asupersync-7tcipb item 4: bound tracked transfers before inserting.
        self.note_transfer(transfer_id);
        self.transfer_chunks
            .entry(transfer_id.to_string())
            .or_default()
            .push(identity.clone());
        Ok(())
    }

    /// Lookup the dedupe capability scope for a transfer.
    fn capability_scope_for_transfer(&self, transfer_id: &str) -> Option<String> {
        let Some(identities) = self.transfer_chunks.get(transfer_id) else {
            return Some(transfer_scope(transfer_id));
        };

        let mut registered_scope = None;
        for identity in identities {
            if identity.capability_scope.is_empty() {
                continue;
            }

            match &registered_scope {
                Some(scope) if scope != &identity.capability_scope => return None,
                Some(_) => {}
                None => registered_scope = Some(identity.capability_scope.clone()),
            }
        }

        registered_scope.or_else(|| Some(String::new()))
    }

    /// Find reusable chunks for a transfer.
    pub fn find_reusable_chunks(
        &self,
        transfer_id: &str,
        content_hashes: &[[u8; 32]],
        criteria: &ChunkReuseCriteria,
    ) -> Vec<ChunkIdentity> {
        let mut reusable: Vec<ChunkIdentity> = Vec::new();
        let mut seen_requested_hashes = BTreeSet::new();
        let mut reusable_content_indices: BTreeMap<([u8; 32], u64), usize> = BTreeMap::new();

        let requesting_scope = self
            .capability_scope_for_transfer(transfer_id)
            .unwrap_or_default();

        for &hash in content_hashes {
            if !seen_requested_hashes.insert(hash) {
                continue;
            }

            let similar = self.cache.find_similar_chunks(hash);
            for chunk in similar {
                if self.cache.can_reuse_chunk(chunk, &requesting_scope)
                    && self.cache.meets_reuse_criteria(chunk, criteria)
                {
                    let content_key = (chunk.content_hash, chunk.size_bytes);
                    if let Some(&index) = reusable_content_indices.get(&content_key) {
                        if chunk.verification.proof_strength
                            > reusable[index].verification.proof_strength
                        {
                            reusable[index] = chunk.clone();
                        }
                    } else {
                        reusable_content_indices.insert(content_key, reusable.len());
                        reusable.push(chunk.clone());
                    }
                }
            }
        }

        reusable
    }

    /// Register chunk reuse for a transfer.
    pub fn register_chunk_reuse(
        &mut self,
        transfer_id: &str,
        identity: &ChunkIdentity,
        _source_transfer_id: &str,
    ) -> Result<(), ChunkingProfileError> {
        // br-asupersync-7tcipb item 4: bound tracked transfers before inserting.
        self.note_transfer(transfer_id);
        let stats = self
            .transfer_stats
            .entry(transfer_id.to_string())
            .or_insert_with(|| TransferReuseStats {
                total_chunks_reused: 0,
                bytes_saved: 0,
                deduplication_ratio: 0.0,
            });

        stats.total_chunks_reused += 1;
        stats.bytes_saved += identity.size_bytes;

        // Update deduplication ratio (simple approximation)
        stats.deduplication_ratio =
            stats.bytes_saved as f64 / (stats.bytes_saved as f64 + 1_000_000.0);

        Ok(())
    }

    /// Get reuse statistics for a transfer.
    pub fn get_reuse_statistics(&self, transfer_id: &str) -> Option<TransferReuseStats> {
        self.transfer_stats.get(transfer_id).cloned()
    }

    /// Store chunk for future reuse (kept for backward compatibility).
    pub fn store_chunk_for_reuse(
        &mut self,
        chunk_data: &[u8],
        transfer_id: &str,
    ) -> Result<ChunkIdentity, ChunkingProfileError> {
        let identity = ChunkIdentity::from_data(
            chunk_data,
            &transfer_scope(transfer_id),
            crate::atp::manifest::ProofStrength::Basic,
        );

        self.cache.store_chunk(&identity, chunk_data)?;
        self.register_transfer_chunk(transfer_id, &identity)?;

        Ok(identity)
    }
}

/// Convert hash to hex string.
fn hex_hash(hash: &[u8; 32]) -> String {
    hash.iter().map(|b| format!("{:02x}", b)).collect()
}

fn transfer_scope(transfer_id: &str) -> String {
    format!("transfer-{transfer_id}")
}

#[cfg(test)]
mod active_tests {
    use super::*;
    use crate::atp::manifest::ProofStrength;

    fn criteria() -> ChunkReuseCriteria {
        ChunkReuseCriteria {
            max_age_seconds: 3600,
            min_proof_strength: ProofStrength::Basic,
            require_same_algorithm: true,
        }
    }

    fn fastcdc_params() -> CdcParameters {
        CdcParameters {
            window_size: 64,
            min_chunk_size: 512,
            max_chunk_size: 4096,
            normalization_constant: 0x1021,
        }
    }

    fn fastcdc_fixture(record_count: usize) -> Vec<u8> {
        let mut data = Vec::new();
        for record_index in 0..record_count {
            data.extend_from_slice(format!("record-{record_index:04}:").as_bytes());
            for payload_index in 0..80 {
                data.push(b'a' + ((record_index + payload_index) % 26) as u8);
            }
            data.push(b'\n');
        }
        data
    }

    fn chunk_hash_set(chunks: &[CdcChunkData]) -> std::collections::BTreeSet<[u8; 32]> {
        chunks.iter().map(|chunk| chunk.content_hash).collect()
    }

    fn test_cdc_chunk(byte_offset: u64, data: &[u8]) -> CdcChunkData {
        CdcChunkData {
            byte_offset,
            size_bytes: data.len() as u64,
            content_hash: CdcEngine::compute_content_hash(data),
        }
    }

    #[test]
    fn gear_hash_matches_known_vector() {
        let mut hash = RollingHash::new(64);
        for &byte in b"asupersync-fastcdc" {
            hash.update(byte);
        }

        assert_eq!(hash.hash(), 0x5240_b854_273d_098e);
    }

    #[test]
    fn fastcdc_chunks_cover_input_byte_exactly() {
        let mut engine = CdcEngine::new();
        let params = fastcdc_params();
        let data = fastcdc_fixture(220);

        let chunks = engine.compute_cdc_boundaries(&data, &params).unwrap();

        assert!(!chunks.is_empty());
        let mut reconstructed = Vec::new();
        let mut expected_offset = 0u64;
        for chunk in &chunks {
            assert_eq!(chunk.byte_offset, expected_offset);
            assert!(chunk.size_bytes <= params.max_chunk_size);

            let start = usize::try_from(chunk.byte_offset).unwrap();
            let end = usize::try_from(chunk.byte_offset + chunk.size_bytes).unwrap();
            reconstructed.extend_from_slice(&data[start..end]);
            expected_offset += chunk.size_bytes;
        }

        assert_eq!(expected_offset, data.len() as u64);
        assert_eq!(reconstructed, data);
    }

    #[test]
    fn fastcdc_resynchronizes_after_prefix_and_insert() {
        let mut engine = CdcEngine::new();
        let params = fastcdc_params();
        let original = fastcdc_fixture(420);

        let mut prefixed = b"new-header-line\n".to_vec();
        prefixed.extend_from_slice(&original);

        let insert_at = original.len() / 2;
        let mut inserted = original[..insert_at].to_vec();
        inserted.extend_from_slice(b"inserted-record:payload payload payload\n");
        inserted.extend_from_slice(&original[insert_at..]);

        let original_chunks = engine.compute_cdc_boundaries(&original, &params).unwrap();
        let prefixed_chunks = engine.compute_cdc_boundaries(&prefixed, &params).unwrap();
        let inserted_chunks = engine.compute_cdc_boundaries(&inserted, &params).unwrap();

        let original_hashes = chunk_hash_set(&original_chunks);
        let prefixed_hashes = chunk_hash_set(&prefixed_chunks);
        let inserted_hashes = chunk_hash_set(&inserted_chunks);
        let prefix_common = original_hashes.intersection(&prefixed_hashes).count();
        let insert_common = original_hashes.intersection(&inserted_hashes).count();

        assert!(prefix_common * 3 >= original_hashes.len() * 2);
        assert!(insert_common * 2 >= original_hashes.len());
    }

    #[test]
    fn delta_floor_zero_when_receiver_already_has_target_chunks() {
        let chunks = vec![
            test_cdc_chunk(0, b"alpha"),
            test_cdc_chunk(5, b"beta"),
            test_cdc_chunk(9, b"gamma"),
        ];

        let estimate = CdcEngine::estimate_delta_floor(&chunks, &chunks, Some(0));

        assert_eq!(estimate.receiver_unique_chunk_count, 3);
        assert_eq!(estimate.sender_unique_chunk_count, 3);
        assert_eq!(estimate.shared_chunk_count, 3);
        assert_eq!(estimate.sender_missing_chunk_count, 0);
        assert_eq!(estimate.estimated_floor_bytes_on_wire, 0);
        assert_eq!(estimate.observed_gap_to_floor, None);
    }

    #[test]
    fn delta_floor_counts_unique_missing_target_chunks_once() {
        let old_a = test_cdc_chunk(0, b"alpha");
        let old_b = test_cdc_chunk(5, b"beta");
        let new_c = test_cdc_chunk(5, b"carrot");
        let duplicate_new_c = test_cdc_chunk(11, b"carrot");

        let estimate = CdcEngine::estimate_delta_floor(
            &[old_a.clone(), old_b],
            &[old_a, new_c, duplicate_new_c],
            Some(18),
        );

        assert_eq!(estimate.shared_chunk_count, 1);
        assert_eq!(estimate.sender_missing_chunk_count, 1);
        assert_eq!(estimate.receiver_stale_chunk_count, 1);
        assert_eq!(estimate.sender_missing_bytes, 6);
        assert_eq!(estimate.receiver_stale_bytes, 4);
        assert_eq!(estimate.symmetric_difference_bytes, 10);
        assert_eq!(estimate.estimated_floor_bytes_on_wire, 6);
        assert_eq!(estimate.observed_gap_to_floor, Some(3.0));
    }

    #[test]
    fn same_transfer_reuses_own_scoped_chunk() {
        let mut manager = ChunkReuseManager::new();
        let identity = manager
            .store_chunk_for_reuse(b"chunk-data", "transfer-a")
            .unwrap();

        let reusable =
            manager.find_reusable_chunks("transfer-a", &[identity.content_hash], &criteria());

        assert_eq!(reusable, vec![identity]);
    }

    #[test]
    fn different_transfer_cannot_reuse_private_scope() {
        let mut manager = ChunkReuseManager::new();
        let identity = manager
            .store_chunk_for_reuse(b"chunk-data", "transfer-a")
            .unwrap();

        let reusable =
            manager.find_reusable_chunks("transfer-b", &[identity.content_hash], &criteria());

        assert!(reusable.is_empty());
    }

    #[test]
    fn conflicting_registered_scopes_fail_closed_to_global_only_reuse() {
        let mut manager = ChunkReuseManager::new();
        let private_a = ChunkIdentity::from_data(b"aaa", "scope-a", ProofStrength::Basic);
        let private_b = ChunkIdentity::from_data(b"bbb", "scope-b", ProofStrength::Basic);
        let global = ChunkIdentity::from_data(b"ccc", "", ProofStrength::Basic);

        manager
            .register_transfer_chunk("mixed", &private_a)
            .unwrap();
        manager
            .register_transfer_chunk("mixed", &private_b)
            .unwrap();
        manager.cache.store_chunk(&private_a, b"aaa").unwrap();
        manager.cache.store_chunk(&private_b, b"bbb").unwrap();
        manager.cache.store_chunk(&global, b"ccc").unwrap();

        let reusable = manager.find_reusable_chunks(
            "mixed",
            &[
                private_a.content_hash,
                private_b.content_hash,
                global.content_hash,
            ],
            &criteria(),
        );

        assert_eq!(reusable, vec![global]);
    }

    #[test]
    fn replacing_same_identity_does_not_inflate_cache_size() {
        let data = b"repeat";
        let identity = ChunkIdentity::from_data(data, "scope-a", ProofStrength::Basic);
        let mut cache = ChunkCache::new(1024);

        cache.store_chunk(&identity, data).unwrap();
        cache.store_chunk(&identity, data).unwrap();

        let stats = cache.get_statistics();
        assert_eq!(stats.total_chunks, 1);
        assert_eq!(stats.current_size, data.len() as u64);
    }

    #[test]
    fn oversized_chunk_is_rejected_without_cache_growth() {
        let data = b"too-large";
        let identity = ChunkIdentity::from_data(data, "scope-a", ProofStrength::Basic);
        let mut cache = ChunkCache::new(1);

        let err = cache.store_chunk(&identity, data).unwrap_err();

        assert!(matches!(
            err,
            ChunkingProfileError::InvalidChunkParameters(_)
        ));
        assert_eq!(cache.get_statistics().current_size, 0);
    }

    #[test]
    fn zero_sized_cache_reports_zero_utilization() {
        let cache = ChunkCache::new(0);

        assert_eq!(cache.get_statistics().utilization, 0.0);
    }

    #[test]
    fn reuse_criteria_rejects_low_proof_chunks() {
        let mut manager = ChunkReuseManager::new();
        let identity = manager
            .store_chunk_for_reuse(b"chunk-data", "transfer-a")
            .unwrap();
        let strict_criteria = ChunkReuseCriteria {
            min_proof_strength: ProofStrength::Enhanced,
            ..criteria()
        };

        let reusable =
            manager.find_reusable_chunks("transfer-a", &[identity.content_hash], &strict_criteria);

        assert!(reusable.is_empty());
    }

    #[test]
    fn reuse_criteria_rejects_stale_chunks() {
        let mut manager = ChunkReuseManager::new();
        let identity = manager
            .store_chunk_for_reuse(b"chunk-data", "transfer-a")
            .unwrap();
        manager
            .cache
            .chunks
            .get_mut(&identity)
            .unwrap()
            .last_accessed = std::time::SystemTime::now()
            .checked_sub(std::time::Duration::from_secs(10))
            .unwrap();
        let stale_rejecting_criteria = ChunkReuseCriteria {
            max_age_seconds: 1,
            ..criteria()
        };

        let reusable = manager.find_reusable_chunks(
            "transfer-a",
            &[identity.content_hash],
            &stale_rejecting_criteria,
        );

        assert!(reusable.is_empty());
    }

    #[test]
    fn reuse_criteria_rejects_non_canonical_algorithm_when_required() {
        let mut manager = ChunkReuseManager::new();
        let mut identity =
            ChunkIdentity::from_data(b"chunk-data", "transfer-transfer-a", ProofStrength::Basic);
        identity.verification.algorithm = "custom-hash".to_string();
        manager.cache.store_chunk(&identity, b"chunk-data").unwrap();
        manager
            .register_transfer_chunk("transfer-a", &identity)
            .unwrap();

        let reusable =
            manager.find_reusable_chunks("transfer-a", &[identity.content_hash], &criteria());

        assert!(reusable.is_empty());
    }

    #[test]
    fn same_content_in_multiple_authorized_scopes_returns_one_candidate() {
        let mut manager = ChunkReuseManager::new();
        let data = b"shared-content";
        let scoped =
            ChunkIdentity::from_data(data, "transfer-transfer-a", ProofStrength::Cryptographic);
        let global = ChunkIdentity::from_data(data, "", ProofStrength::Basic);

        manager.cache.store_chunk(&scoped, data).unwrap();
        manager.cache.store_chunk(&global, data).unwrap();
        manager
            .register_transfer_chunk("transfer-a", &scoped)
            .unwrap();

        let reusable =
            manager.find_reusable_chunks("transfer-a", &[scoped.content_hash], &criteria());

        assert_eq!(reusable.len(), 1);
        assert_eq!(reusable[0].content_hash, scoped.content_hash);
        assert_eq!(reusable[0].size_bytes, scoped.size_bytes);
        assert_eq!(
            reusable[0].verification.proof_strength,
            ProofStrength::Cryptographic
        );
    }
}