haagenti 0.1.0

Next-generation compression library with LZ4, Zstd, Brotli, and Deflate support
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
//! Incremental HCT file writer with append-only semantics.
//!
//! Writes compressed tensors incrementally, allowing resumption after
//! interruption. The header is written at the end when all tensors are done.

use std::fs::{File, OpenOptions};
use std::io::{BufWriter, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::{Error, Result};

/// Magic bytes for incremental HCT files.
const INCREMENTAL_MAGIC: &[u8; 4] = b"IHCT";

/// Version of the incremental format.
const INCREMENTAL_VERSION: u32 = 1;

/// Reserved space for the final safetensors header (in bytes).
/// We allocate 16MB which should be enough for ~50K tensors.
const RESERVED_HEADER_SIZE: u64 = 16 * 1024 * 1024;

/// Index entry for a compressed tensor.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TensorIndexEntry {
    /// Tensor name.
    pub name: String,
    /// Offset in the data section (after header).
    pub offset: u64,
    /// Compressed size in bytes.
    pub compressed_size: u64,
    /// Original shape.
    pub shape: Vec<usize>,
    /// Original data type.
    pub dtype: String,
    /// XXH3-64 checksum of compressed data.
    pub checksum: u64,
}

/// Metadata written at the end of the file.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IncrementalMeta {
    /// Magic bytes (for validation).
    pub magic: String,
    /// Format version.
    pub version: u32,
    /// Total tensors written.
    pub tensor_count: usize,
    /// Total compressed data size.
    pub data_size: u64,
    /// Index of all tensors.
    pub index: Vec<TensorIndexEntry>,
}

/// Incremental HCT file writer.
///
/// Writes tensors one at a time, appending to the file. The final
/// safetensors-compatible header is written when `finalize()` is called.
pub struct IncrementalHctWriter {
    /// Output file handle.
    file: BufWriter<File>,
    /// Path to the output file.
    path: PathBuf,
    /// Current write position (after reserved header).
    position: u64,
    /// Index of written tensors.
    index: Vec<TensorIndexEntry>,
    /// Whether the file is finalized.
    finalized: bool,
}

impl IncrementalHctWriter {
    /// Creates a new incremental writer.
    ///
    /// Reserves space at the beginning for the header, then writes
    /// tensor data sequentially. Call `finalize()` to write the header.
    pub fn create(path: impl AsRef<Path>) -> Result<Self> {
        let path = path.as_ref();

        let file = File::create(path)
            .map_err(|e| Error::io(format!("failed to create output file: {}", e)))?;

        let mut writer = BufWriter::new(file);

        // Write placeholder header with magic and version
        writer
            .write_all(INCREMENTAL_MAGIC)
            .map_err(|e| Error::io(format!("failed to write magic: {}", e)))?;
        writer
            .write_all(&INCREMENTAL_VERSION.to_le_bytes())
            .map_err(|e| Error::io(format!("failed to write version: {}", e)))?;

        // Reserve space for the final header
        let reserved = vec![0u8; (RESERVED_HEADER_SIZE - 8) as usize];
        writer
            .write_all(&reserved)
            .map_err(|e| Error::io(format!("failed to reserve header space: {}", e)))?;

        writer
            .flush()
            .map_err(|e| Error::io(format!("failed to flush: {}", e)))?;

        Ok(Self {
            file: writer,
            path: path.to_path_buf(),
            position: RESERVED_HEADER_SIZE,
            index: Vec::new(),
            finalized: false,
        })
    }

    /// Resumes writing to an existing incremental file.
    ///
    /// Reads the existing index from the end of the file and continues
    /// appending new tensors.
    pub fn resume(path: impl AsRef<Path>) -> Result<Self> {
        let path = path.as_ref();

        // Open for reading to get current state
        let mut file =
            File::open(path).map_err(|e| Error::io(format!("failed to open for resume: {}", e)))?;

        // Verify magic
        let mut magic = [0u8; 4];
        std::io::Read::read_exact(&mut file, &mut magic)
            .map_err(|e| Error::io(format!("failed to read magic: {}", e)))?;

        if &magic != INCREMENTAL_MAGIC {
            return Err(Error::corrupted("invalid incremental file magic"));
        }

        // Try to read existing index from the end
        let file_len = file
            .metadata()
            .map_err(|e| Error::io(format!("failed to get file size: {}", e)))?
            .len();

        let (index, position) = if file_len > RESERVED_HEADER_SIZE {
            // Try to find and parse the index footer
            Self::read_index_footer(&mut file, file_len)?
        } else {
            (Vec::new(), RESERVED_HEADER_SIZE)
        };

        // Reopen for append
        drop(file);
        let file = OpenOptions::new()
            .write(true)
            .open(path)
            .map_err(|e| Error::io(format!("failed to open for append: {}", e)))?;

        let mut writer = BufWriter::new(file);

        // Seek to append position
        writer
            .seek(SeekFrom::Start(position))
            .map_err(|e| Error::io(format!("failed to seek to append position: {}", e)))?;

        Ok(Self {
            file: writer,
            path: path.to_path_buf(),
            position,
            index,
            finalized: false,
        })
    }

    /// Reads the index footer from an existing file.
    fn read_index_footer(file: &mut File, file_len: u64) -> Result<(Vec<TensorIndexEntry>, u64)> {
        // The index footer is stored as JSON at the very end with a length prefix
        // Format: [data...][json_index][json_length: u64][FOOTER_MAGIC: 4 bytes]
        const FOOTER_MAGIC: &[u8; 4] = b"INDX";

        if file_len < RESERVED_HEADER_SIZE + 12 {
            // Too small to have footer
            return Ok((Vec::new(), RESERVED_HEADER_SIZE));
        }

        // Read footer magic
        file.seek(SeekFrom::End(-4))
            .map_err(|e| Error::io(format!("failed to seek to footer: {}", e)))?;

        let mut footer_magic = [0u8; 4];
        std::io::Read::read_exact(file, &mut footer_magic)
            .map_err(|e| Error::io(format!("failed to read footer magic: {}", e)))?;

        if &footer_magic != FOOTER_MAGIC {
            // No valid footer, start fresh after reserved header
            return Ok((Vec::new(), RESERVED_HEADER_SIZE));
        }

        // Read JSON length
        file.seek(SeekFrom::End(-12))
            .map_err(|e| Error::io(format!("failed to seek to length: {}", e)))?;

        let mut len_bytes = [0u8; 8];
        std::io::Read::read_exact(file, &mut len_bytes)
            .map_err(|e| Error::io(format!("failed to read index length: {}", e)))?;
        let json_len = u64::from_le_bytes(len_bytes);

        if json_len > file_len - RESERVED_HEADER_SIZE - 12 {
            return Err(Error::corrupted("invalid index length"));
        }

        // Read JSON index
        file.seek(SeekFrom::End(-(12 + json_len as i64)))
            .map_err(|e| Error::io(format!("failed to seek to index: {}", e)))?;

        let mut json_bytes = vec![0u8; json_len as usize];
        std::io::Read::read_exact(file, &mut json_bytes)
            .map_err(|e| Error::io(format!("failed to read index: {}", e)))?;

        let index: Vec<TensorIndexEntry> = serde_json::from_slice(&json_bytes)
            .map_err(|e| Error::corrupted(format!("failed to parse index: {}", e)))?;

        // Calculate data end position (before index)
        let data_end = file_len - 12 - json_len;

        Ok((index, data_end))
    }

    /// Writes a compressed tensor to the file.
    ///
    /// Returns the offset where the tensor was written.
    pub fn write_tensor(
        &mut self,
        name: impl Into<String>,
        data: &[u8],
        shape: Vec<usize>,
        dtype: impl Into<String>,
    ) -> Result<u64> {
        if self.finalized {
            return Err(Error::io("cannot write to finalized file"));
        }

        let name = name.into();
        let dtype = dtype.into();
        let offset = self.position;

        // Compute checksum
        let checksum = xxhash_rust::xxh3::xxh3_64(data);

        // Write data
        self.file
            .write_all(data)
            .map_err(|e| Error::io(format!("failed to write tensor data: {}", e)))?;

        // Update position
        self.position += data.len() as u64;

        // Add to index
        self.index.push(TensorIndexEntry {
            name,
            offset,
            compressed_size: data.len() as u64,
            shape,
            dtype,
            checksum,
        });

        Ok(offset)
    }

    /// Flushes pending writes and updates the index footer.
    ///
    /// This allows resumption without losing recent progress.
    pub fn checkpoint(&mut self) -> Result<()> {
        const FOOTER_MAGIC: &[u8; 4] = b"INDX";

        self.file
            .flush()
            .map_err(|e| Error::io(format!("failed to flush: {}", e)))?;

        // Write index as JSON
        let json = serde_json::to_vec(&self.index)
            .map_err(|e| Error::io(format!("failed to serialize index: {}", e)))?;

        self.file
            .write_all(&json)
            .map_err(|e| Error::io(format!("failed to write index: {}", e)))?;

        // Write length and magic
        self.file
            .write_all(&(json.len() as u64).to_le_bytes())
            .map_err(|e| Error::io(format!("failed to write index length: {}", e)))?;
        self.file
            .write_all(FOOTER_MAGIC)
            .map_err(|e| Error::io(format!("failed to write footer magic: {}", e)))?;

        self.file
            .flush()
            .map_err(|e| Error::io(format!("failed to flush checkpoint: {}", e)))?;

        // Truncate to current position + footer
        // (This removes any old footer that's now outdated)
        let current_pos = self.position + json.len() as u64 + 12;
        self.file
            .get_mut()
            .set_len(current_pos)
            .map_err(|e| Error::io(format!("failed to truncate: {}", e)))?;

        // Seek back to data append position
        self.file
            .seek(SeekFrom::Start(self.position))
            .map_err(|e| Error::io(format!("failed to seek after checkpoint: {}", e)))?;

        Ok(())
    }

    /// Finalizes the file by writing the safetensors-compatible header.
    ///
    /// After this, the file can be loaded as a standard safetensors file.
    pub fn finalize(mut self) -> Result<PathBuf> {
        if self.finalized {
            return Err(Error::io("file already finalized"));
        }

        self.file
            .flush()
            .map_err(|e| Error::io(format!("failed to flush before finalize: {}", e)))?;

        // Build safetensors header JSON
        let mut header_obj = serde_json::Map::new();

        for entry in &self.index {
            let tensor_info = serde_json::json!({
                "dtype": entry.dtype,
                "shape": entry.shape,
                "data_offsets": [
                    entry.offset - RESERVED_HEADER_SIZE,
                    entry.offset - RESERVED_HEADER_SIZE + entry.compressed_size
                ]
            });
            header_obj.insert(entry.name.clone(), tensor_info);
        }

        let header_json = serde_json::to_vec(&header_obj)
            .map_err(|e| Error::io(format!("failed to serialize header: {}", e)))?;

        // Pad to 8-byte alignment
        let padded_len = (header_json.len() + 7) & !7;
        let padding = padded_len - header_json.len();

        // Check if header fits in reserved space
        let header_total = 8 + padded_len; // length prefix + json
        if header_total > RESERVED_HEADER_SIZE as usize {
            return Err(Error::io(format!(
                "header too large: {} bytes (max {})",
                header_total, RESERVED_HEADER_SIZE
            )));
        }

        // Seek to beginning and write final header
        self.file
            .seek(SeekFrom::Start(0))
            .map_err(|e| Error::io(format!("failed to seek to start: {}", e)))?;

        // Write header length
        self.file
            .write_all(&(padded_len as u64).to_le_bytes())
            .map_err(|e| Error::io(format!("failed to write header length: {}", e)))?;

        // Write header JSON
        self.file
            .write_all(&header_json)
            .map_err(|e| Error::io(format!("failed to write header JSON: {}", e)))?;

        // Write padding
        if padding > 0 {
            self.file
                .write_all(&vec![b' '; padding])
                .map_err(|e| Error::io(format!("failed to write padding: {}", e)))?;
        }

        // Fill remaining reserved space with zeros
        let remaining = RESERVED_HEADER_SIZE as usize - header_total;
        if remaining > 0 {
            // We need to shift the data section
            // For simplicity, we'll require the header to fit in the reserved space
            // and just zero out the unused portion
            // A production implementation would compact the file
        }

        self.file
            .flush()
            .map_err(|e| Error::io(format!("failed to flush final: {}", e)))?;

        self.finalized = true;

        Ok(self.path)
    }

    /// Returns the number of tensors written.
    #[must_use]
    pub fn tensor_count(&self) -> usize {
        self.index.len()
    }

    /// Returns the current data size (excluding header).
    #[must_use]
    pub fn data_size(&self) -> u64 {
        self.position - RESERVED_HEADER_SIZE
    }

    /// Returns the output file path.
    #[must_use]
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Returns the tensor index.
    #[must_use]
    pub fn index(&self) -> &[TensorIndexEntry] {
        &self.index
    }
}

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

    #[test]
    fn test_incremental_writer_create() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.safetensors");

        let writer = IncrementalHctWriter::create(&path).unwrap();
        assert_eq!(writer.tensor_count(), 0);
        assert!(path.exists());
    }

    #[test]
    fn test_incremental_writer_write_tensor() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.safetensors");

        let mut writer = IncrementalHctWriter::create(&path).unwrap();

        let data = vec![1u8, 2, 3, 4, 5, 6, 7, 8];
        let offset = writer
            .write_tensor("tensor.0", &data, vec![2, 4], "F16")
            .unwrap();

        assert_eq!(offset, RESERVED_HEADER_SIZE);
        assert_eq!(writer.tensor_count(), 1);
        assert_eq!(writer.data_size(), 8);
    }

    #[test]
    fn test_incremental_writer_checkpoint() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.safetensors");

        {
            let mut writer = IncrementalHctWriter::create(&path).unwrap();
            writer
                .write_tensor("tensor.0", &[1, 2, 3, 4], vec![4], "F32")
                .unwrap();
            writer.checkpoint().unwrap();
        }

        // Resume and verify index was preserved
        let writer = IncrementalHctWriter::resume(&path).unwrap();
        assert_eq!(writer.tensor_count(), 1);
        assert_eq!(writer.index()[0].name, "tensor.0");
    }

    #[test]
    fn test_incremental_writer_finalize() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.safetensors");

        let mut writer = IncrementalHctWriter::create(&path).unwrap();

        // Write some tensors
        let data1 = vec![0u8; 32];
        let data2 = vec![0u8; 64];
        writer
            .write_tensor("weight.0", &data1, vec![4, 8], "F16")
            .unwrap();
        writer
            .write_tensor("weight.1", &data2, vec![8, 8], "F16")
            .unwrap();

        let final_path = writer.finalize().unwrap();
        assert_eq!(final_path, path);

        // Verify the file starts with a valid safetensors header
        let file = std::fs::read(&path).unwrap();
        let header_len = u64::from_le_bytes(file[0..8].try_into().unwrap());
        assert!(header_len > 0);
        assert!(header_len < RESERVED_HEADER_SIZE);
    }
}