aft-store 0.68.1

On-disk index formats for Agent Foundational Toolkit
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
use std::path::{Path, PathBuf};
use std::sync::Arc;

use super::*;
use crate::cache::BlockCache;
use crate::ids::{Digest128, FamilyId, SegmentId, WriterIncarnation};
use crate::test_rng::TestRng;

const KIND_A: SectionKind = SectionKind::new(1);
const KIND_B: SectionKind = SectionKind::new(2);

fn family(name: &str) -> FamilyId {
    FamilyId::from_digest(Digest128::of(name.as_bytes()))
}

fn meta() -> SegmentMeta {
    SegmentMeta {
        family: family("repo"),
        segment: SegmentId::new(9),
        incarnation: WriterIncarnation::new(3),
        created_unix_ms: 1_793_700_000_000,
    }
}

fn spec(kind: SectionKind, key_space: u16, alignment: Alignment) -> SectionSpec {
    SectionSpec {
        id: SectionId::new(kind, KeySpace::new(key_space)),
        version: 1,
        alignment,
        requirement: Requirement::Required,
    }
}

fn known() -> Vec<KnownSection> {
    vec![
        KnownSection {
            kind: KIND_A,
            version: 1,
        },
        KnownSection {
            kind: KIND_B,
            version: 1,
        },
    ]
}

fn cache() -> Arc<BlockCache> {
    Arc::new(BlockCache::new(8 * BLOCK_LEN))
}

/// Section lengths around block boundaries, each filled with distinct bytes.
fn sample_sections() -> Vec<(SectionSpec, Vec<u8>)> {
    let mut rng = TestRng::new(1);
    [
        0,
        1,
        BLOCK_LEN - 1,
        BLOCK_LEN,
        BLOCK_LEN + 1,
        3 * BLOCK_LEN + 17,
    ]
    .into_iter()
    .enumerate()
    .map(|(index, len)| {
        let alignment = if index % 2 == 0 {
            Alignment::Word
        } else {
            Alignment::Vector
        };
        let kind = if index % 3 == 0 { KIND_B } else { KIND_A };
        let key_space = u16::try_from(index).unwrap();
        (spec(kind, key_space, alignment), rng.bytes(len))
    })
    .collect()
}

fn write_segment(dir: &Path, sections: &[(SectionSpec, Vec<u8>)]) -> PathBuf {
    let path = dir.join("1.seg");
    let mut writer = SegmentWriter::create(&path, meta()).unwrap();
    for (spec, bytes) in sections {
        writer.begin_section(*spec).unwrap();
        // Written in uneven pieces, so block boundaries fall inside writes.
        for piece in bytes.chunks(10_007) {
            writer.write(piece).unwrap();
        }
        writer.end_section(bytes.len() as u64 / 3).unwrap();
    }
    let summary = writer.finish().unwrap();
    assert_eq!(summary.sections, sections.len());
    assert_eq!(summary.len, std::fs::metadata(&path).unwrap().len());
    path
}

#[test]
fn every_byte_reads_back_through_the_cache() {
    let dir = tempfile::tempdir().unwrap();
    let sections = sample_sections();
    let path = write_segment(dir.path(), &sections);
    let segment = Segment::open(&path, family("repo"), &known(), cache()).unwrap();
    assert_eq!(segment.id(), SegmentId::new(9));
    assert_eq!(segment.incarnation(), WriterIncarnation::new(3));
    segment.verify().unwrap();

    let mut rng = TestRng::new(2);
    for (spec, bytes) in &sections {
        let handle = segment.section(spec.id).unwrap();
        let info = segment.info(handle);
        assert_eq!(info.len, bytes.len() as u64);
        assert_eq!(info.item_count, bytes.len() as u64 / 3);
        assert_eq!(segment.read_range(handle, 0..info.len).unwrap(), *bytes);
        for _ in 0..50 {
            if bytes.is_empty() {
                break;
            }
            let start = rng.below_usize(bytes.len());
            let end = start + rng.below_usize(bytes.len() - start + 1);
            let read = segment
                .read_range(handle, start as u64..end as u64)
                .unwrap();
            assert_eq!(
                read,
                bytes[start..end],
                "range {start}..{end} of {}",
                spec.id
            );
        }
    }
}

#[test]
fn sections_start_on_their_alignment() {
    let dir = tempfile::tempdir().unwrap();
    let sections = sample_sections();
    let path = write_segment(dir.path(), &sections);
    let file = std::fs::read(&path).unwrap();
    let segment = Segment::open(&path, family("repo"), &known(), cache()).unwrap();
    // The reader checked alignment at open; this confirms the bytes sit
    // where the table says by locating each non-empty section in the file.
    for (spec, bytes) in sections.iter().filter(|(_, bytes)| bytes.len() > 64) {
        let handle = segment.section(spec.id).unwrap();
        let first = segment.read_range(handle, 0..64).unwrap();
        let offset = file
            .windows(64)
            .position(|window| window == first.as_slice())
            .unwrap();
        assert_eq!(offset as u64 % spec.alignment.bytes(), 0, "{}", spec.id);
        assert_eq!(&file[offset..offset + bytes.len()], bytes.as_slice());
    }
}

#[test]
fn reads_past_a_section_end_are_refused() {
    let dir = tempfile::tempdir().unwrap();
    let sections = sample_sections();
    let path = write_segment(dir.path(), &sections);
    let segment = Segment::open(&path, family("repo"), &known(), cache()).unwrap();
    let handle = segment.section(sections[1].0.id).unwrap();
    let error = segment.read_range(handle, 0..2).unwrap_err();
    assert!(
        matches!(error.kind(), ErrorKind::OutOfRange { len: 1, .. }),
        "{error}"
    );
}

#[test]
fn a_changed_block_fails_only_reads_that_touch_it() {
    let dir = tempfile::tempdir().unwrap();
    let sections = sample_sections();
    let path = write_segment(dir.path(), &sections);
    let (spec, bytes) = &sections[5];
    let target = {
        let segment = Segment::open(&path, family("repo"), &known(), cache()).unwrap();
        let handle = segment.section(spec.id).unwrap();
        let block_one = segment
            .read_range(handle, BLOCK_LEN as u64..BLOCK_LEN as u64 + 32)
            .unwrap();
        let file = std::fs::read(&path).unwrap();
        file.windows(32)
            .position(|window| window == block_one.as_slice())
            .unwrap()
    };
    let mut file = std::fs::read(&path).unwrap();
    file[target + 5] ^= 0x40;
    std::fs::write(&path, &file).unwrap();

    let segment = Segment::open(&path, family("repo"), &known(), cache()).unwrap();
    let handle = segment.section(spec.id).unwrap();
    assert_eq!(
        segment.read_range(handle, 0..100).unwrap(),
        bytes[..100],
        "block 0 is intact"
    );
    let error = segment
        .read_range(handle, BLOCK_LEN as u64..BLOCK_LEN as u64 + 10)
        .unwrap_err();
    assert!(error.is_corruption());
    assert!(
        matches!(
            error.kind(),
            ErrorKind::Corrupt(Region::Block { block: 1, .. })
        ),
        "{error}"
    );
    assert!(segment.verify().unwrap_err().is_corruption());
}

#[test]
fn a_segment_of_another_family_is_refused() {
    let dir = tempfile::tempdir().unwrap();
    let path = write_segment(dir.path(), &sample_sections());
    let error = Segment::open(&path, family("other"), &known(), cache()).unwrap_err();
    assert!(
        matches!(error.kind(), ErrorKind::FamilyMismatch { .. }),
        "{error}"
    );
}

#[test]
fn unknown_required_sections_refuse_and_unknown_optional_ones_are_skipped() {
    let dir = tempfile::tempdir().unwrap();
    let mut sections = sample_sections();
    sections.push((
        SectionSpec {
            id: SectionId::new(SectionKind::new(77), KeySpace::NONE),
            version: 1,
            alignment: Alignment::Word,
            requirement: Requirement::Optional,
        },
        b"later plane".to_vec(),
    ));
    let path = write_segment(dir.path(), &sections);
    let segment = Segment::open(&path, family("repo"), &known(), cache()).unwrap();
    assert_eq!(segment.sections().count(), sections.len() - 1);
    assert!(
        segment
            .section(SectionId::new(SectionKind::new(77), KeySpace::NONE))
            .is_none()
    );

    // A reader that knows kind A only at version 2 cannot use version 1.
    let only_newer = [KnownSection {
        kind: KIND_A,
        version: 2,
    }];
    let error = Segment::open(&path, family("repo"), &only_newer, cache()).unwrap_err();
    assert!(
        matches!(
            error.kind(),
            ErrorKind::UnknownRequiredSection { version: 1, .. }
        ),
        "{error}"
    );
}

#[test]
fn an_unfinished_segment_is_not_a_segment() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("torn.seg");
    let mut writer = SegmentWriter::create(&path, meta()).unwrap();
    writer
        .write_section(spec(KIND_A, 0, Alignment::Word), &[7; 5000], 1)
        .unwrap();
    // The writer is dropped without finishing, as a crash would leave it.
    drop(writer);
    let error = Segment::open(&path, family("repo"), &known(), cache()).unwrap_err();
    assert!(matches!(error.kind(), ErrorKind::NotASegment), "{error}");
}

#[test]
fn writers_refuse_misuse() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("misuse.seg");
    std::fs::write(&path, b"existing").unwrap();
    assert!(matches!(
        SegmentWriter::create(&path, meta()).unwrap_err().kind(),
        ErrorKind::Io(error) if error.kind() == std::io::ErrorKind::AlreadyExists
    ));

    let path = dir.path().join("fresh.seg");
    let mut writer = SegmentWriter::create(&path, meta()).unwrap();
    assert!(writer.write(b"loose").is_err(), "bytes outside a section");
    let a = spec(KIND_A, 0, Alignment::Word);
    writer.write_section(a, b"one", 1).unwrap();
    assert!(matches!(
        writer.write_section(a, b"two", 1).unwrap_err().kind(),
        ErrorKind::DuplicateSection(_)
    ));
    writer
        .begin_section(spec(KIND_B, 0, Alignment::Word))
        .unwrap();
    assert!(matches!(
        writer.finish().unwrap_err().kind(),
        ErrorKind::UnfinishedSection(_)
    ));
}

#[test]
fn closing_a_segment_releases_its_cached_blocks() {
    let dir = tempfile::tempdir().unwrap();
    let sections = sample_sections();
    let path = write_segment(dir.path(), &sections);
    let shared = cache();
    let segment = Segment::open(&path, family("repo"), &known(), Arc::clone(&shared)).unwrap();
    let handle = segment.section(sections[5].0.id).unwrap();
    segment
        .read_range(handle, 0..segment.info(handle).len)
        .unwrap();
    assert_eq!(shared.stats().blocks, 4);
    drop(segment);
    assert_eq!(shared.stats().blocks, 0);
    assert_eq!(shared.stats().resident_bytes, 0);
}

/// Writes segment `n` of the family at `dir/n.seg`, with one section of
/// `bytes`.
fn write_numbered(dir: &Path, n: u64, bytes: &[u8]) -> (PathBuf, SectionId) {
    let path = dir.join(format!("{n}.seg"));
    let section = spec(KIND_A, 0, Alignment::Word);
    let mut writer = SegmentWriter::create(
        &path,
        SegmentMeta {
            segment: SegmentId::new(n),
            ..meta()
        },
    )
    .unwrap();
    writer.begin_section(section).unwrap();
    writer.write(bytes).unwrap();
    writer.end_section(1).unwrap();
    writer.finish().unwrap();
    (path, section.id)
}

/// A process holds only so many segment files open. With room for three,
/// six segments read back every byte as written, closing and opening their
/// files again in turn, and never more than three are open. A file
/// replaced while its segment's file was closed fails the next read as
/// damage, never answering with the other file's bytes; one removed fails
/// it as missing.
#[test]
fn past_the_cap_on_open_files_segments_reopen_their_own_file() {
    let dir = tempfile::tempdir().unwrap();
    let files = Arc::new(open_files::OpenFiles::new(3));
    let mut rng = TestRng::new(7);
    let written = (1..=6)
        .map(|n| {
            let bytes = rng.bytes(2 * BLOCK_LEN + 5);
            let (path, id) = write_numbered(dir.path(), n, &bytes);
            (path, id, bytes)
        })
        .collect::<Vec<_>>();
    // A cache of one block, so reads keep going to the files.
    let cache = Arc::new(BlockCache::new(BLOCK_LEN));
    let segments = written
        .iter()
        .map(|(path, _, _)| {
            Segment::open_in(path, family("repo"), &known(), Arc::clone(&cache), &files).unwrap()
        })
        .collect::<Vec<_>>();
    assert_eq!(files.open_count(), 3);
    for round in 0..3 {
        for (segment, (_, id, bytes)) in segments.iter().zip(&written) {
            let handle = segment.section(*id).unwrap();
            assert_eq!(
                segment.read_range(handle, 0..bytes.len() as u64).unwrap(),
                *bytes,
                "round {round}, segment {}",
                segment.id().get()
            );
            assert!(files.open_count() <= 3, "{}", files.open_count());
        }
    }
    segments[5].verify().unwrap();
    assert_eq!(files.open_count(), 3);

    // Segments 4 to 6 were read last, so the files of 1 to 3 are closed.
    // Another segment's file now stands at 1's path, and 2's is gone.
    std::fs::copy(&written[3].0, &written[0].0).unwrap();
    std::fs::remove_file(&written[1].0).unwrap();
    let first = segments[0].section(written[0].1).unwrap();
    let error = segments[0].read_range(first, 0..10).unwrap_err();
    assert!(error.is_corruption(), "{error}");
    let second = segments[1].section(written[1].1).unwrap();
    let error = segments[1].read_range(second, 0..10).unwrap_err();
    assert!(
        matches!(error.kind(), ErrorKind::Io(io) if io.kind() == std::io::ErrorKind::NotFound),
        "{error}"
    );
    // A file of another length is refused too.
    std::fs::write(&written[2].0, b"short").unwrap();
    let third = segments[2].section(written[2].1).unwrap();
    assert!(
        segments[2]
            .read_range(third, 0..10)
            .unwrap_err()
            .is_corruption()
    );
    // The files still open read on, and none of the failures opened one.
    assert_eq!(files.open_count(), 3);
    let (_, id, bytes) = &written[4];
    let handle = segments[4].section(*id).unwrap();
    assert_eq!(
        segments[4]
            .read_range(handle, 0..bytes.len() as u64)
            .unwrap(),
        *bytes
    );
    // A dropped segment closes its file.
    drop(segments);
    assert_eq!(files.open_count(), 0);
}

/// Damage anywhere in a segment must never panic, and must never let a read
/// return bytes other than the ones written: either the read fails or the
/// damage was in bytes no read uses, such as alignment padding.
#[test]
fn damaged_files_fail_cleanly_or_read_the_written_bytes() {
    let dir = tempfile::tempdir().unwrap();
    let sections = sample_sections();
    let path = write_segment(dir.path(), &sections);
    let original = std::fs::read(&path).unwrap();
    let mut rng = TestRng::new(3);
    let (mut opened, mut refused_reads) = (0, 0);
    for round in 0..300 {
        let mut damaged = original.clone();
        match round % 3 {
            0 => {
                let at = rng.below_usize(damaged.len());
                damaged[at] ^= 1 << rng.below(8);
            }
            1 => damaged.truncate(rng.below_usize(damaged.len())),
            _ => {
                let at = rng.below_usize(damaged.len());
                let len = rng.below_usize(64).min(damaged.len() - at);
                damaged[at..at + len].fill(0);
            }
        }
        let damaged_path = dir.path().join(format!("damaged-{round}.seg"));
        std::fs::write(&damaged_path, &damaged).unwrap();
        let Ok(segment) = Segment::open(&damaged_path, family("repo"), &known(), cache()) else {
            continue;
        };
        opened += 1;
        for (spec, bytes) in &sections {
            let Some(handle) = segment.section(spec.id) else {
                continue;
            };
            match segment.read_range(handle, 0..bytes.len() as u64) {
                Ok(read) => {
                    assert_eq!(read, *bytes, "round {round}: a read returned changed bytes");
                }
                Err(error) => {
                    assert!(error.is_corruption(), "round {round}: {error}");
                    refused_reads += 1;
                }
            }
        }
    }
    // Damage inside section bytes leaves the tables intact, so some damaged
    // files open and only their reads fail; both paths must have run.
    assert!(
        opened > 0 && refused_reads > 0,
        "opened {opened}, refused reads {refused_reads}"
    );
}

/// The segment format version and the required sections it was pinned with
/// last; see [`FORMAT_VERSION`]. Each entry is a section kind and its
/// version, from the text, content, call-graph and vector planes.
const PINNED_FORMAT_VERSION: u16 = 1;
const PINNED_REQUIRED_SECTIONS: [(u16, u16); 22] = [
    // Text.
    (0x0100, 1),
    (0x0101, 1),
    (0x0102, 1),
    (0x0103, 1),
    // Content.
    (0x0200, 1),
    (0x0201, 1),
    (0x0202, 1),
    (0x0203, 1),
    // Call graph.
    (0x0300, 4),
    (0x0301, 4),
    (0x0302, 4),
    (0x0303, 4),
    (0x0304, 4),
    // Vectors.
    (0x0400, 1),
    (0x0401, 1),
    (0x0402, 1),
    (0x0403, 1),
    (0x0404, 1),
    (0x0405, 1),
    (0x0406, 1),
    (0x0407, 1),
    (0x0408, 1),
];

/// Every section kind the planes read, with its version. Each plane writes
/// all of its sections as required, so a build that does not know one of
/// them cannot open a segment holding it.
fn required_sections() -> std::collections::BTreeSet<(u16, u16)> {
    crate::text::KNOWN
        .iter()
        .chain(&crate::content::KNOWN)
        .chain(&crate::graph::KNOWN)
        .chain(&crate::vector::KNOWN)
        .map(|known| (known.kind.get(), known.version))
        .collect()
}

/// An older build must meet a newer segment format version, which it
/// refuses, rather than a required section it does not know, which makes
/// the segment unusable, so it is built over and reclaimed. So the
/// required sections may only change together with [`FORMAT_VERSION`].
#[test]
fn the_segment_format_version_moves_with_the_required_sections() {
    let sections = required_sections();
    let pinned = PINNED_REQUIRED_SECTIONS
        .into_iter()
        .collect::<std::collections::BTreeSet<_>>();
    assert!(
        FORMAT_VERSION != PINNED_FORMAT_VERSION || sections == pinned,
        "the required sections changed (added {:?}, removed {:?}) while the segment format \
         version stayed {FORMAT_VERSION}: bump FORMAT_VERSION in segment/format.rs, so an older \
         build refuses these segments by their version instead of meeting a required section \
         it does not know and rebuilding over them. Then pin the new version and sections \
         here. Only an optional section, which an older build skips, needs no bump.",
        sections.difference(&pinned).collect::<Vec<_>>(),
        pinned.difference(&sections).collect::<Vec<_>>()
    );
    assert!(
        FORMAT_VERSION == PINNED_FORMAT_VERSION && sections == pinned,
        "the segment format version is {FORMAT_VERSION} but this guard pins version \
         {PINNED_FORMAT_VERSION}: pin the new version here together with the required sections \
         it holds, {sections:?}"
    );
}