heddle-wire 0.6.0

An AI-native version control system
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
// SPDX-License-Identifier: Apache-2.0
use std::{
    fs::{self, File, OpenOptions},
    io::{Read, Write},
    path::{Path, PathBuf},
    time::{SystemTime, UNIX_EPOCH},
};

use objects::store::{
    CompressionConfig, ObjectStore,
    pack::{ObjectType as PackObjectType, PackBuilder, PackObjectId, StreamingPackBuilder},
};

use crate::{
    ObjectData, ObjectId, ObjectInfo, ObjectType, ProtocolError, Result, load_object_data,
};

/// Maximum hosted native-pack body accepted by the receive primitive.
///
/// Native sync packs are produced from bounded state-closure wants and
/// each decoded pack object is separately capped at 1 GiB in the pack
/// reader. A 2 GiB compressed pack is materially above normal hosted
/// sync use while still preventing an untrusted server from growing the
/// in-memory receive buffer without limit. The receive path can now move
/// to temp-file spooling plus `install_pack_streaming` — that install API
/// reports the installed ids the receiver needs, so only the spooling of
/// the receive buffer itself remains.
pub const MAX_RECEIVED_PACK_SIZE: u64 = 2 * 1024 * 1024 * 1024;

/// Maximum hosted native-pack index accepted by the receive primitive.
///
/// Pack indexes are proportional to object count, not object payload
/// size. 256 MiB leaves room for millions of entries while bounding the
/// second in-memory buffer controlled by the remote sender.
pub const MAX_RECEIVED_PACK_INDEX_SIZE: u64 = 256 * 1024 * 1024;

/// Maximum hosted Git pack accepted by the Git-lane transfer primitive.
///
/// Git-overlay sync sends Git-shaped data as raw Git packs. The sender and
/// receiver still stream those bytes in bounded chunks, but the declared pack
/// size is untrusted wire input and needs a hard ceiling before buffering or
/// spooling work begins.
pub const MAX_RECEIVED_GIT_PACK_SIZE: u64 = 2 * 1024 * 1024 * 1024;

#[derive(Debug, Clone)]
pub struct NativePackBundle {
    pub pack_data: Vec<u8>,
    pub index_data: Vec<u8>,
}

#[derive(Debug)]
pub struct NativePackFileBundle {
    dir: PathBuf,
    pub pack_path: PathBuf,
    pub index_path: PathBuf,
    pub pack_len: u64,
    pub index_len: u64,
}

impl Drop for NativePackFileBundle {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.dir);
    }
}

#[derive(Debug)]
pub struct PackFileChunkReader {
    file: File,
    total_len: u64,
    chunk_size: usize,
    offset: u64,
    chunk_index: u32,
}

pub type NativePackFileChunk = (u64, u32, Vec<u8>, bool);

impl PackFileChunkReader {
    pub fn open(path: &Path, chunk_size: usize) -> Result<Self> {
        let file = File::open(path)?;
        let total_len = file.metadata()?.len();
        Ok(Self {
            file,
            total_len,
            chunk_size: chunk_size.max(1),
            offset: 0,
            chunk_index: 0,
        })
    }

    pub fn next_chunk(&mut self) -> Result<Option<NativePackFileChunk>> {
        if self.offset >= self.total_len {
            return Ok(None);
        }
        let remaining = self.total_len - self.offset;
        let len = remaining.min(self.chunk_size as u64);
        let len = usize::try_from(len).map_err(|_| {
            ProtocolError::InvalidState("native pack file chunk length exceeds usize".to_string())
        })?;
        let mut data = vec![0u8; len];
        self.file.read_exact(&mut data)?;

        let offset = self.offset;
        let chunk_index = self.chunk_index;
        self.offset = self.offset.checked_add(len as u64).ok_or_else(|| {
            ProtocolError::InvalidState("native pack file chunk offset overflow".to_string())
        })?;
        self.chunk_index = self.chunk_index.checked_add(1).ok_or_else(|| {
            ProtocolError::InvalidState("native pack file chunk index overflow".to_string())
        })?;
        Ok(Some((
            offset,
            chunk_index,
            data,
            self.offset == self.total_len,
        )))
    }
}

#[derive(Debug)]
pub struct GrowingPackChunkReader {
    file: File,
    chunk_size: usize,
    offset: u64,
    chunk_index: u32,
}

impl GrowingPackChunkReader {
    pub fn open(path: &Path, chunk_size: usize) -> Result<Self> {
        Ok(Self {
            file: File::open(path)?,
            chunk_size: chunk_size.max(1),
            offset: 0,
            chunk_index: 0,
        })
    }

    pub fn next_available_chunk(
        &mut self,
        final_stream: bool,
    ) -> Result<Option<NativePackFileChunk>> {
        let total_len = self.file.metadata()?.len();
        if self.offset >= total_len {
            return Ok(None);
        }
        let available = total_len - self.offset;
        if !final_stream && available < self.chunk_size as u64 {
            return Ok(None);
        }

        let len = available.min(self.chunk_size as u64);
        let len = usize::try_from(len).map_err(|_| {
            ProtocolError::InvalidState(
                "growing native pack chunk length exceeds usize".to_string(),
            )
        })?;
        let mut data = vec![0u8; len];
        self.file.read_exact(&mut data)?;

        let offset = self.offset;
        let chunk_index = self.chunk_index;
        self.offset = self.offset.checked_add(len as u64).ok_or_else(|| {
            ProtocolError::InvalidState("growing native pack chunk offset overflow".to_string())
        })?;
        self.chunk_index = self.chunk_index.checked_add(1).ok_or_else(|| {
            ProtocolError::InvalidState("growing native pack chunk index overflow".to_string())
        })?;
        Ok(Some((
            offset,
            chunk_index,
            data,
            final_stream && self.offset == total_len,
        )))
    }
}

pub struct NativePackStreamingWriter {
    dir: Option<PathBuf>,
    pack_path: PathBuf,
    index_path: PathBuf,
    builder: Option<StreamingPackBuilder<File>>,
}

impl NativePackStreamingWriter {
    pub fn new_in(root: &Path, object_count: u64) -> Result<Self> {
        let base = root.join("transfer-spool");
        fs::create_dir_all(&base)?;
        let dir = unique_spool_dir(&base)?;
        let pack_path = dir.join("pack");
        let index_path = dir.join("idx");
        let bucket_dir = dir.join("buckets");
        let pack_file = OpenOptions::new()
            .read(true)
            .write(true)
            .create_new(true)
            .open(&pack_path)?;
        let builder = StreamingPackBuilder::new_with_object_count(
            pack_file,
            index_path.clone(),
            sync_pack_compression(),
            bucket_dir,
            object_count,
        )
        .map_err(ProtocolError::from)?;

        Ok(Self {
            dir: Some(dir),
            pack_path,
            index_path,
            builder: Some(builder),
        })
    }

    pub fn pack_path(&self) -> &Path {
        &self.pack_path
    }

    pub fn index_path(&self) -> &Path {
        &self.index_path
    }

    pub fn add_object_data(&mut self, object: ObjectData) -> Result<()> {
        if !is_native_packable_object_type(object.obj_type) {
            return Err(ProtocolError::InvalidState(format!(
                "{:?} sidecar records cannot be packed into the content-addressed object pack",
                object.obj_type
            )));
        }
        let builder = self.builder.as_mut().ok_or_else(|| {
            ProtocolError::InvalidState("native pack streaming writer is finalized".to_string())
        })?;
        let pack_id = to_pack_object_id(&object.id);
        builder
            .add_id(pack_id, to_pack_object_type(object.obj_type)?, object.data)
            .map_err(ProtocolError::from)
    }

    pub fn flush_pack(&mut self) -> Result<()> {
        let builder = self.builder.as_mut().ok_or_else(|| {
            ProtocolError::InvalidState("native pack streaming writer is finalized".to_string())
        })?;
        builder.flush_pack().map_err(ProtocolError::from)
    }

    pub fn finish(mut self) -> Result<NativePackFileBundle> {
        let builder = self.builder.take().ok_or_else(|| {
            ProtocolError::InvalidState("native pack streaming writer is finalized".to_string())
        })?;
        let (file, _) = builder.finalize().map_err(ProtocolError::from)?;
        file.sync_all()?;
        drop(file);
        let pack_len = fs::metadata(&self.pack_path)?.len();
        let index_len = fs::metadata(&self.index_path)?.len();
        let dir = self.dir.take().ok_or_else(|| {
            ProtocolError::InvalidState("native pack streaming writer lost spool dir".to_string())
        })?;
        Ok(NativePackFileBundle {
            dir,
            pack_path: self.pack_path.clone(),
            index_path: self.index_path.clone(),
            pack_len,
            index_len,
        })
    }
}

impl Drop for NativePackStreamingWriter {
    fn drop(&mut self) {
        if let Some(dir) = self.dir.take() {
            let _ = fs::remove_dir_all(dir);
        }
    }
}

#[derive(Debug, Default, Clone)]
pub struct PackChunkState {
    pub pack_data: Vec<u8>,
    pub index_data: Vec<u8>,
    pack_progress: (u64, u32),
    index_progress: (u64, u32),
    pack_complete: bool,
    index_complete: bool,
}

impl PackChunkState {
    pub fn is_complete(&self) -> bool {
        self.pack_complete && self.index_complete
    }
}

#[derive(Debug, Default, Clone)]
pub struct GitPackChunkState {
    transfer_id: Option<String>,
    pack_size: Option<u64>,
    next_offset: u64,
    next_chunk_index: u32,
    pack_data: Vec<u8>,
}

impl GitPackChunkState {
    pub fn is_idle(&self) -> bool {
        self.transfer_id.is_none()
            && self.pack_size.is_none()
            && self.next_offset == 0
            && self.next_chunk_index == 0
            && self.pack_data.is_empty()
    }

    pub fn ensure_idle(&self) -> Result<()> {
        if self.is_idle() {
            Ok(())
        } else {
            Err(ProtocolError::InvalidState(
                "Git pack transfer ended before final chunk".to_string(),
            ))
        }
    }

    pub fn receive_chunk(
        &mut self,
        transfer_id: &str,
        offset: u64,
        chunk_index: u32,
        is_final_chunk: bool,
        pack_size: u64,
        data: &[u8],
    ) -> Result<Option<Vec<u8>>> {
        if transfer_id.is_empty() {
            return Err(ProtocolError::InvalidState(
                "Git pack transfer_id is required".to_string(),
            ));
        }
        if pack_size > MAX_RECEIVED_GIT_PACK_SIZE {
            return Err(ProtocolError::InvalidState(format!(
                "Git pack exceeds maximum transfer size of {MAX_RECEIVED_GIT_PACK_SIZE} bytes"
            )));
        }
        if data.is_empty() {
            return Err(ProtocolError::InvalidState(
                "Git pack chunk must not be empty".to_string(),
            ));
        }
        match self.transfer_id.as_ref() {
            Some(current) if current != transfer_id => {
                return Err(ProtocolError::InvalidState(format!(
                    "Git pack transfer id changed from {current:?} to {transfer_id:?}"
                )));
            }
            Some(_) => {}
            None => {
                self.transfer_id = Some(transfer_id.to_string());
                self.pack_size = Some(pack_size);
            }
        }
        if self.pack_size != Some(pack_size) {
            return Err(ProtocolError::InvalidState(
                "Git pack size changed during transfer".to_string(),
            ));
        }
        if offset != self.next_offset {
            return Err(ProtocolError::InvalidState(format!(
                "Git pack offset mismatch: expected {}, got {}",
                self.next_offset, offset
            )));
        }
        if chunk_index != self.next_chunk_index {
            return Err(ProtocolError::InvalidState(format!(
                "Git pack chunk index mismatch: expected {}, got {}",
                self.next_chunk_index, chunk_index
            )));
        }
        let chunk_len = u64::try_from(data.len()).map_err(|_| {
            ProtocolError::InvalidState("Git pack chunk length exceeds u64".to_string())
        })?;
        let next_offset = self
            .next_offset
            .checked_add(chunk_len)
            .ok_or_else(|| ProtocolError::InvalidState("Git pack offset overflow".to_string()))?;
        if next_offset > pack_size {
            return Err(ProtocolError::InvalidState(
                "Git pack chunk exceeds declared pack size".to_string(),
            ));
        }
        self.pack_data.extend_from_slice(data);
        self.next_offset = next_offset;
        self.next_chunk_index = self.next_chunk_index.checked_add(1).ok_or_else(|| {
            ProtocolError::InvalidState("Git pack chunk index overflow".to_string())
        })?;
        if is_final_chunk {
            if self.next_offset != pack_size {
                return Err(ProtocolError::InvalidState(format!(
                    "Git pack final size mismatch: declared {}, received {}",
                    pack_size, self.next_offset
                )));
            }
            let pack_data = std::mem::take(&mut self.pack_data);
            self.transfer_id = None;
            self.pack_size = None;
            self.next_offset = 0;
            self.next_chunk_index = 0;
            return Ok(Some(pack_data));
        }
        if self.next_offset == pack_size {
            return Err(ProtocolError::InvalidState(
                "Git pack reached declared size without final chunk marker".to_string(),
            ));
        }
        Ok(None)
    }
}

#[derive(Debug)]
pub struct PackChunkSpool {
    dir: PathBuf,
    pack: PackStreamSpool,
    index: PackStreamSpool,
}

impl PackChunkSpool {
    pub fn new_in(root: &Path) -> Result<Self> {
        let base = root.join("transfer-spool");
        fs::create_dir_all(&base)?;
        let dir = unique_spool_dir(&base)?;
        let pack = PackStreamSpool::new(dir.join("pack"))?;
        let index = PackStreamSpool::new(dir.join("idx"))?;
        Ok(Self { dir, pack, index })
    }

    pub fn is_complete(&self) -> bool {
        self.pack.complete && self.index.complete
    }

    #[allow(clippy::too_many_arguments)]
    pub fn receive_chunk(
        &mut self,
        is_index: bool,
        resume_offset: u64,
        chunk_index: u32,
        is_complete: bool,
        data: &[u8],
        is_final_chunk: bool,
    ) -> Result<()> {
        let max_bytes = if is_index {
            MAX_RECEIVED_PACK_INDEX_SIZE
        } else {
            MAX_RECEIVED_PACK_SIZE
        };
        let stream = if is_index {
            &mut self.index
        } else {
            &mut self.pack
        };
        receive_pack_chunk_to_spool(
            stream,
            is_index,
            resume_offset,
            chunk_index,
            is_complete,
            data,
            is_final_chunk,
            max_bytes,
        )
    }

    pub fn install_into(&mut self, store: &impl ObjectStore) -> Result<Vec<PackObjectId>> {
        if !self.is_complete() {
            return Err(ProtocolError::InvalidState(
                "native pack spool is incomplete".to_string(),
            ));
        }
        self.pack.close()?;
        self.index.close()?;
        store
            .install_pack_streaming(&self.pack.path, &self.index.path)
            .map_err(ProtocolError::from)
    }
}

impl Drop for PackChunkSpool {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.dir);
    }
}

#[derive(Debug)]
struct PackStreamSpool {
    path: PathBuf,
    file: Option<File>,
    progress: (u64, u32),
    complete: bool,
}

impl PackStreamSpool {
    fn new(path: PathBuf) -> Result<Self> {
        let file = File::create(&path)?;
        Ok(Self {
            path,
            file: Some(file),
            progress: (0, 0),
            complete: false,
        })
    }

    fn write_all(&mut self, data: &[u8]) -> Result<()> {
        let Some(file) = self.file.as_mut() else {
            return Err(ProtocolError::InvalidState(
                "native pack spool stream is already closed".to_string(),
            ));
        };
        file.write_all(data)?;
        Ok(())
    }

    fn close(&mut self) -> Result<()> {
        if let Some(mut file) = self.file.take() {
            file.flush()?;
            file.sync_all()?;
        }
        Ok(())
    }
}

pub fn native_pack_excluded_object_types() -> &'static [ObjectType] {
    &[ObjectType::Redaction, ObjectType::StateVisibility]
}

pub fn is_native_packable_object_type(obj_type: ObjectType) -> bool {
    !native_pack_excluded_object_types().contains(&obj_type)
}

pub fn build_native_pack(
    store: &impl ObjectStore,
    objects: &[ObjectInfo],
) -> Result<NativePackBundle> {
    let mut builder = PackBuilder::new(sync_pack_compression());

    for info in objects {
        // Sidecar records (redaction + state-visibility) live outside
        // `.heddle/objects/` so GC cannot touch them, and must not be
        // folded into the content-addressed pack. They ship via the
        // per-object transfer path instead; callers split them out before
        // packing.
        if !is_native_packable_object_type(info.obj_type) {
            continue;
        }
        let object = load_object_data(store, &info.id, info.obj_type)?;
        let pack_id = to_pack_object_id(&object.id);
        builder.add_id(pack_id, to_pack_object_type(object.obj_type)?, object.data);
    }

    let (pack_data, index_data, _) = builder.build()?;
    Ok(NativePackBundle {
        pack_data,
        index_data,
    })
}

fn sync_pack_compression() -> CompressionConfig {
    CompressionConfig {
        level: 1,
        min_size: 1024,
        max_delta_size: 0,
        ..CompressionConfig::default()
    }
}

pub fn install_received_pack(
    store: &impl ObjectStore,
    pack_data: &[u8],
    index_data: &[u8],
) -> Result<Vec<PackObjectId>> {
    store
        .install_pack(pack_data, index_data)
        .map_err(ProtocolError::from)
}

pub fn next_pack_chunk(
    data: &[u8],
    chunk_size: usize,
    chunk_index: usize,
) -> Option<(usize, Vec<u8>, bool)> {
    let (start, len) = crate::chunk_bounds(data.len(), chunk_size.max(1), chunk_index)?;
    let is_final = start + len == data.len();
    Some((start, data[start..start + len].to_vec(), is_final))
}

pub fn receive_pack_chunk(
    state: &mut PackChunkState,
    is_index: bool,
    resume_offset: u64,
    chunk_index: u32,
    is_complete: bool,
    data: &[u8],
    is_final_chunk: bool,
) -> Result<()> {
    let max_bytes = if is_index {
        MAX_RECEIVED_PACK_INDEX_SIZE
    } else {
        MAX_RECEIVED_PACK_SIZE
    };
    receive_pack_chunk_with_limit(
        state,
        is_index,
        resume_offset,
        chunk_index,
        is_complete,
        data,
        is_final_chunk,
        max_bytes,
    )
}

#[allow(clippy::too_many_arguments)]
fn receive_pack_chunk_with_limit(
    state: &mut PackChunkState,
    is_index: bool,
    resume_offset: u64,
    chunk_index: u32,
    is_complete: bool,
    data: &[u8],
    is_final_chunk: bool,
    max_bytes: u64,
) -> Result<()> {
    let (buffer, progress, complete) = if is_index {
        (
            &mut state.index_data,
            &mut state.index_progress,
            &mut state.index_complete,
        )
    } else {
        (
            &mut state.pack_data,
            &mut state.pack_progress,
            &mut state.pack_complete,
        )
    };

    let next_progress = validate_pack_chunk(
        *progress,
        is_index,
        resume_offset,
        chunk_index,
        data,
        max_bytes,
    )?;

    buffer.extend_from_slice(data);
    *progress = next_progress;
    if is_final_chunk || is_complete {
        *complete = true;
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn receive_pack_chunk_to_spool(
    stream: &mut PackStreamSpool,
    is_index: bool,
    resume_offset: u64,
    chunk_index: u32,
    is_complete: bool,
    data: &[u8],
    is_final_chunk: bool,
    max_bytes: u64,
) -> Result<()> {
    let next_progress = validate_pack_chunk(
        stream.progress,
        is_index,
        resume_offset,
        chunk_index,
        data,
        max_bytes,
    )?;
    stream.write_all(data)?;
    stream.progress = next_progress;
    if is_final_chunk || is_complete {
        stream.complete = true;
    }
    Ok(())
}

fn validate_pack_chunk(
    progress: (u64, u32),
    is_index: bool,
    resume_offset: u64,
    chunk_index: u32,
    data: &[u8],
    max_bytes: u64,
) -> Result<(u64, u32)> {
    if resume_offset != progress.0 {
        return Err(ProtocolError::InvalidState(format!(
            "native pack chunk resume offset mismatch: expected {}, got {}",
            progress.0, resume_offset
        )));
    }
    if chunk_index != progress.1 {
        return Err(ProtocolError::InvalidState(format!(
            "native pack chunk index mismatch: expected {}, got {}",
            progress.1, chunk_index
        )));
    }

    let data_len = u64::try_from(data.len()).map_err(|_| {
        ProtocolError::InvalidState("native pack chunk length does not fit in u64".to_string())
    })?;
    let next_offset = progress.0.checked_add(data_len).ok_or_else(|| {
        ProtocolError::InvalidState("native pack chunk offset overflow".to_string())
    })?;
    if next_offset > max_bytes {
        let stream_name = if is_index { "index" } else { "body" };
        return Err(ProtocolError::InvalidState(format!(
            "native pack {stream_name} exceeds receive size limit: {next_offset} bytes (max {max_bytes})"
        )));
    }
    let next_chunk = progress.1.checked_add(1).ok_or_else(|| {
        ProtocolError::InvalidState("native pack chunk index overflow".to_string())
    })?;

    Ok((next_offset, next_chunk))
}

fn unique_spool_dir(base: &Path) -> Result<PathBuf> {
    let stamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(|err| {
            ProtocolError::InvalidState(format!("system clock before UNIX epoch: {err}"))
        })?
        .as_nanos();
    for attempt in 0..100u32 {
        let dir = base.join(format!("pack-{}-{stamp}-{attempt}", std::process::id()));
        match fs::create_dir(&dir) {
            Ok(()) => return Ok(dir),
            Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => continue,
            Err(err) => return Err(ProtocolError::Io(err)),
        }
    }
    Err(ProtocolError::InvalidState(
        "failed to allocate native pack spool directory".to_string(),
    ))
}

fn to_pack_object_id(id: &ObjectId) -> PackObjectId {
    match id {
        ObjectId::Hash(hash) => PackObjectId::Hash(*hash),
        ObjectId::ChangeId(change_id) => PackObjectId::ChangeId(*change_id),
    }
}

fn to_pack_object_type(obj_type: ObjectType) -> Result<PackObjectType> {
    match obj_type {
        ObjectType::Blob => Ok(PackObjectType::Blob),
        ObjectType::Tree => Ok(PackObjectType::Tree),
        ObjectType::State => Ok(PackObjectType::State),
        ObjectType::Action => Ok(PackObjectType::Action),
        ObjectType::Redaction => Err(ProtocolError::InvalidState(
            "Redaction sidecar records cannot be packed into the content-addressed object pack"
                .to_string(),
        )),
        ObjectType::StateVisibility => Err(ProtocolError::InvalidState(
            "StateVisibility sidecar records cannot be packed into the content-addressed object pack"
                .to_string(),
        )),
    }
}

#[cfg(test)]
mod tests {
    use objects::{
        object::Blob,
        store::{FsStore, ObjectStore, pack::PackObjectId},
    };
    use tempfile::TempDir;

    use super::{
        GitPackChunkState, GrowingPackChunkReader, MAX_RECEIVED_PACK_SIZE,
        NativePackStreamingWriter, ObjectData, ObjectId, ObjectInfo, ObjectType, PackChunkSpool,
        PackChunkState, PackFileChunkReader, build_native_pack, install_received_pack,
        next_pack_chunk, receive_pack_chunk, receive_pack_chunk_with_limit,
    };

    fn create_test_store() -> (TempDir, FsStore) {
        let temp = TempDir::new().unwrap();
        let store = FsStore::new(temp.path().join(".heddle"));
        store.init().unwrap();
        (temp, store)
    }

    #[test]
    fn receive_pack_chunk_rejects_cumulative_size_over_limit_before_buffering() {
        let mut state = PackChunkState::default();

        receive_pack_chunk_with_limit(&mut state, false, 0, 0, false, b"abcd", false, 8).unwrap();
        receive_pack_chunk_with_limit(&mut state, false, 4, 1, false, b"efgh", false, 8).unwrap();

        let error = receive_pack_chunk_with_limit(&mut state, false, 8, 2, false, b"i", false, 8)
            .unwrap_err();

        assert_eq!(state.pack_data, b"abcdefgh");
        assert!(
            error
                .to_string()
                .contains("native pack body exceeds receive size limit")
        );
        assert!(error.to_string().contains("9 bytes (max 8)"));
    }

    #[test]
    fn receive_pack_chunk_checks_production_limit_before_extending_buffer() {
        let mut state = PackChunkState {
            pack_progress: (MAX_RECEIVED_PACK_SIZE - 1, 0),
            ..PackChunkState::default()
        };

        let error = receive_pack_chunk(
            &mut state,
            false,
            MAX_RECEIVED_PACK_SIZE - 1,
            0,
            false,
            b"xx",
            false,
        )
        .unwrap_err();

        assert!(state.pack_data.is_empty());
        assert!(
            error
                .to_string()
                .contains("native pack body exceeds receive size limit")
        );
    }

    #[test]
    fn receive_pack_chunk_rejects_resume_offset_mismatch_before_buffering() {
        let mut state = PackChunkState::default();

        let error =
            receive_pack_chunk(&mut state, false, 1, 0, false, b"late chunk", false).unwrap_err();

        assert!(state.pack_data.is_empty());
        assert!(
            error
                .to_string()
                .contains("native pack chunk resume offset mismatch: expected 0, got 1")
        );
    }

    #[test]
    fn receive_pack_chunk_rejects_chunk_index_mismatch_before_buffering() {
        let mut state = PackChunkState::default();

        receive_pack_chunk(&mut state, false, 0, 0, false, b"abc", false).unwrap();
        let error = receive_pack_chunk(&mut state, false, 3, 2, false, b"def", false).unwrap_err();

        assert_eq!(state.pack_data, b"abc");
        assert!(
            error
                .to_string()
                .contains("native pack chunk index mismatch: expected 1, got 2")
        );
    }

    #[test]
    fn git_pack_chunk_state_requires_ordered_chunks_and_final_size() {
        let mut state = GitPackChunkState::default();

        assert!(
            state
                .receive_chunk("git-pack:test", 0, 0, false, 8, b"abcd")
                .unwrap()
                .is_none()
        );
        let error = state
            .receive_chunk("git-pack:test", 4, 2, true, 8, b"efgh")
            .unwrap_err();

        assert!(
            error
                .to_string()
                .contains("Git pack chunk index mismatch: expected 1, got 2")
        );
        assert!(state.ensure_idle().is_err());

        let mut state = GitPackChunkState::default();
        state
            .receive_chunk("git-pack:test", 0, 0, false, 8, b"abcd")
            .unwrap();
        let complete = state
            .receive_chunk("git-pack:test", 4, 1, true, 8, b"efgh")
            .unwrap()
            .unwrap();

        assert_eq!(complete, b"abcdefgh");
        assert!(state.ensure_idle().is_ok());
    }

    #[test]
    fn receive_pack_chunk_accepts_completion_flags_for_pack_and_index() {
        let mut state = PackChunkState::default();

        receive_pack_chunk(&mut state, false, 0, 0, true, b"pack-body", false).unwrap();
        assert!(!state.is_complete());
        receive_pack_chunk(&mut state, true, 0, 0, false, b"pack-index", true).unwrap();

        assert!(state.is_complete());
        assert_eq!(state.pack_data, b"pack-body");
        assert_eq!(state.index_data, b"pack-index");
    }

    #[test]
    fn normal_size_native_pack_receives_and_installs() {
        let (_source_temp, source_store) = create_test_store();
        let (_dest_temp, dest_store) = create_test_store();
        let blob = Blob::from("native pack receive regression");
        let hash = source_store.put_blob(&blob).unwrap();
        let bundle = build_native_pack(
            &source_store,
            &[ObjectInfo {
                id: ObjectId::Hash(hash),
                obj_type: ObjectType::Blob,
                size: blob.size() as u64,
                delta_base: None,
            }],
        )
        .unwrap();

        let mut state = PackChunkState::default();
        let mut chunk_index = 0usize;
        while let Some((start, data, is_final)) = next_pack_chunk(&bundle.pack_data, 7, chunk_index)
        {
            receive_pack_chunk(
                &mut state,
                false,
                start as u64,
                chunk_index as u32,
                is_final,
                &data,
                is_final,
            )
            .unwrap();
            chunk_index += 1;
        }

        let mut index_chunk = 0usize;
        while let Some((start, data, is_final)) =
            next_pack_chunk(&bundle.index_data, 5, index_chunk)
        {
            receive_pack_chunk(
                &mut state,
                true,
                start as u64,
                index_chunk as u32,
                is_final,
                &data,
                is_final,
            )
            .unwrap();
            index_chunk += 1;
        }

        assert!(state.is_complete());
        assert_eq!(state.pack_data, bundle.pack_data);
        assert_eq!(state.index_data, bundle.index_data);

        let installed_ids =
            install_received_pack(&dest_store, &state.pack_data, &state.index_data).unwrap();

        assert_eq!(installed_ids, vec![PackObjectId::Hash(hash)]);
        let installed_blob = dest_store.get_blob(&hash).unwrap().unwrap();
        assert_eq!(installed_blob.content(), blob.content());
    }

    #[test]
    fn normal_size_native_pack_spools_and_installs() {
        let (_source_temp, source_store) = create_test_store();
        let (dest_temp, dest_store) = create_test_store();
        let blob = Blob::from("native pack spooled receive regression");
        let hash = source_store.put_blob(&blob).unwrap();
        let bundle = build_native_pack(
            &source_store,
            &[ObjectInfo {
                id: ObjectId::Hash(hash),
                obj_type: ObjectType::Blob,
                size: blob.size() as u64,
                delta_base: None,
            }],
        )
        .unwrap();

        let mut spool = PackChunkSpool::new_in(dest_temp.path()).unwrap();
        let mut chunk_index = 0usize;
        while let Some((start, data, is_final)) = next_pack_chunk(&bundle.pack_data, 7, chunk_index)
        {
            spool
                .receive_chunk(
                    false,
                    start as u64,
                    chunk_index as u32,
                    is_final,
                    &data,
                    is_final,
                )
                .unwrap();
            chunk_index += 1;
        }

        let mut index_chunk = 0usize;
        while let Some((start, data, is_final)) =
            next_pack_chunk(&bundle.index_data, 5, index_chunk)
        {
            spool
                .receive_chunk(
                    true,
                    start as u64,
                    index_chunk as u32,
                    is_final,
                    &data,
                    is_final,
                )
                .unwrap();
            index_chunk += 1;
        }

        assert!(spool.is_complete());
        let installed_ids = spool.install_into(&dest_store).unwrap();

        assert_eq!(installed_ids, vec![PackObjectId::Hash(hash)]);
        let installed_blob = dest_store.get_blob(&hash).unwrap().unwrap();
        assert_eq!(installed_blob.content(), blob.content());
    }

    #[test]
    fn native_pack_streaming_writer_drains_growing_pack_and_installs() {
        let (source_temp, source_store) = create_test_store();
        let (dest_temp, dest_store) = create_test_store();
        let blob = Blob::from("native pack growing stream regression");
        let hash = source_store.put_blob(&blob).unwrap();
        let large_blob = Blob::from_slice(&vec![b'z'; 4096]);
        let large_hash = source_store.put_blob(&large_blob).unwrap();

        let mut writer = NativePackStreamingWriter::new_in(source_temp.path(), 2).unwrap();
        let mut pack_reader = GrowingPackChunkReader::open(writer.pack_path(), 31).unwrap();
        let mut spool = PackChunkSpool::new_in(dest_temp.path()).unwrap();
        let mut saw_interleaved_pack_chunk = false;

        for (id, obj_type, data) in [
            (
                ObjectId::Hash(hash),
                ObjectType::Blob,
                blob.content().to_vec(),
            ),
            (
                ObjectId::Hash(large_hash),
                ObjectType::Blob,
                large_blob.content().to_vec(),
            ),
        ] {
            writer
                .add_object_data(ObjectData {
                    id,
                    obj_type,
                    data,
                    is_delta: false,
                })
                .unwrap();
            writer.flush_pack().unwrap();
            while let Some((offset, chunk_index, data, is_final)) =
                pack_reader.next_available_chunk(false).unwrap()
            {
                assert!(
                    !is_final,
                    "pre-final growing pack drain must not mark chunks final"
                );
                saw_interleaved_pack_chunk = true;
                spool
                    .receive_chunk(false, offset, chunk_index, false, &data, false)
                    .unwrap();
            }
        }

        let bundle = writer.finish().unwrap();
        let mut saw_final_pack_chunk = false;
        while let Some((offset, chunk_index, data, is_final)) =
            pack_reader.next_available_chunk(true).unwrap()
        {
            saw_final_pack_chunk |= is_final;
            spool
                .receive_chunk(false, offset, chunk_index, is_final, &data, is_final)
                .unwrap();
        }

        let mut index_reader = PackFileChunkReader::open(&bundle.index_path, 17).unwrap();
        while let Some((offset, chunk_index, data, is_final)) = index_reader.next_chunk().unwrap() {
            spool
                .receive_chunk(true, offset, chunk_index, is_final, &data, is_final)
                .unwrap();
        }

        assert!(
            saw_interleaved_pack_chunk,
            "expected at least one pack chunk before finalize"
        );
        assert!(
            saw_final_pack_chunk,
            "expected final pack chunk after finish"
        );
        assert!(spool.is_complete());
        let mut installed_ids = spool.install_into(&dest_store).unwrap();
        let mut expected_ids = vec![PackObjectId::Hash(hash), PackObjectId::Hash(large_hash)];
        installed_ids.sort();
        expected_ids.sort();

        assert_eq!(installed_ids, expected_ids);
        let installed_blob = dest_store.get_blob(&hash).unwrap().unwrap();
        assert_eq!(installed_blob.content(), blob.content());
        let installed_large_blob = dest_store.get_blob(&large_hash).unwrap().unwrap();
        assert_eq!(installed_large_blob.content(), large_blob.content());
    }
}