diff --git a/seaweed-block/core/storage/chunk_record.go b/seaweed-block/core/storage/chunk_record.go index 7ebb042bca..92d7dbc204 100644 --- a/seaweed-block/core/storage/chunk_record.go +++ b/seaweed-block/core/storage/chunk_record.go @@ -7,23 +7,38 @@ import ( "hash/crc32" "os" "path/filepath" + "slices" ) // The persisted per-chunk record: one page per segment of // chunkRecordSegment chunks and one header. A page carries each chunk's state -// and hash and the store position it is valid through; the header carries the -// local-work epoch and G, the position every page is valid through. +// (a state of the chunk kernel's table) and hash, the store position it is +// valid through, the segment's candidate count and the segment name its +// chunks' published marks refer to. The header carries the local-work epoch +// and G, the position every page is valid through. const chunkRecordSegment = 10000 +// chunkRecordKernelTable is the version of the chunk kernel's table whose +// states the pages carry: chunk-kernel/2. +const chunkRecordKernelTable = 2 + var ( - chunkRecordPageMagic = [8]byte{'S', 'W', 'C', 'R', 'P', 'G', '0', '1'} - chunkRecordHeaderMagic = [8]byte{'S', 'W', 'C', 'R', 'H', 'D', '0', '1'} + chunkRecordPageMagic = [8]byte{'S', 'W', 'C', 'R', 'P', 'G', '0', '2'} + chunkRecordHeaderMagic = [8]byte{'S', 'W', 'C', 'R', 'H', 'D', '0', '2'} // errChunkRecordCheck: a page or header failed its check. errChunkRecordCheck = errors.New("storage: chunk record: check failed") + // errChunkRecordNotCurrent: the pages are not valid through exactly the + // store's durable position, so the header is not written. + errChunkRecordNotCurrent = errors.New("storage: chunk record: pages not current with the store") ) -const chunkRecordEntryBytes = 1 + 32 +const ( + chunkRecordEntryBytes = 1 + 32 + chunkRecordMaxSegName = 1024 + chunkRecordPageFixed = 8 + 8 + 8 + 2 + 4 + 2 // magic, segment, valid, table, candidates, name length + chunkRecordHeaderBytes = 8 + 2 + 8 + 8 + 8 + 4 +) type chunkRecordEntry struct { State uint8 @@ -31,9 +46,11 @@ type chunkRecordEntry struct { } type chunkRecordPage struct { - Segment uint64 - ValidThrough uint64 - Entries []chunkRecordEntry + Segment uint64 + ValidThrough uint64 + Candidates uint32 + PublishedSegment string + Entries []chunkRecordEntry } type chunkRecordHeader struct { @@ -52,59 +69,87 @@ func chunkRecordSegments(chunks uint64) uint64 { return (chunks + chunkRecordSegment - 1) / chunkRecordSegment } -func encodeChunkRecordPage(p *chunkRecordPage) []byte { +func chunkRecordSegmentLen(chunks, seg uint64) int { + if rest := chunks - seg*chunkRecordSegment; rest < chunkRecordSegment { + return int(rest) + } + return chunkRecordSegment +} + +func encodeChunkRecordPage(p *chunkRecordPage) ([]byte, error) { + if len(p.PublishedSegment) > chunkRecordMaxSegName { + return nil, fmt.Errorf("storage: chunk record: segment name of %d bytes", len(p.PublishedSegment)) + } le := binary.LittleEndian - b := make([]byte, 8+8+8+4, 8+8+8+4+len(p.Entries)*chunkRecordEntryBytes+4) - copy(b, chunkRecordPageMagic[:]) - le.PutUint64(b[8:], p.Segment) - le.PutUint64(b[16:], p.ValidThrough) - le.PutUint32(b[24:], uint32(len(p.Entries))) + b := make([]byte, 0, chunkRecordPageFixed+len(p.PublishedSegment)+4+len(p.Entries)*chunkRecordEntryBytes+4) + b = append(b, chunkRecordPageMagic[:]...) + b = le.AppendUint64(b, p.Segment) + b = le.AppendUint64(b, p.ValidThrough) + b = le.AppendUint16(b, chunkRecordKernelTable) + b = le.AppendUint32(b, p.Candidates) + b = le.AppendUint16(b, uint16(len(p.PublishedSegment))) + b = append(b, p.PublishedSegment...) + b = le.AppendUint32(b, uint32(len(p.Entries))) for _, e := range p.Entries { b = append(b, e.State) b = append(b, e.Hash[:]...) } - return le.AppendUint32(b, crc32.Checksum(b, crc32cTable)) + return le.AppendUint32(b, crc32.Checksum(b, crc32cTable)), nil } func decodeChunkRecordPage(b []byte, seg uint64) (*chunkRecordPage, error) { le := binary.LittleEndian - if len(b) < 8+8+8+4+4 || [8]byte(b[:8]) != chunkRecordPageMagic { + if len(b) < chunkRecordPageFixed+4+4 || [8]byte(b[:8]) != chunkRecordPageMagic { return nil, errChunkRecordCheck } body := b[:len(b)-4] if crc32.Checksum(body, crc32cTable) != le.Uint32(b[len(b)-4:]) { return nil, errChunkRecordCheck } - n := int(le.Uint32(b[24:])) - if len(body) != 28+n*chunkRecordEntryBytes || le.Uint64(b[8:]) != seg { + if le.Uint64(b[8:]) != seg || le.Uint16(b[24:]) != chunkRecordKernelTable { return nil, errChunkRecordCheck } - p := &chunkRecordPage{Segment: seg, ValidThrough: le.Uint64(b[16:]), Entries: make([]chunkRecordEntry, n)} + p := &chunkRecordPage{Segment: seg, ValidThrough: le.Uint64(b[16:]), Candidates: le.Uint32(b[26:])} + at := chunkRecordPageFixed + nameLen := int(le.Uint16(b[30:])) + if nameLen > chunkRecordMaxSegName || len(body) < at+nameLen+4 { + return nil, errChunkRecordCheck + } + p.PublishedSegment = string(b[at : at+nameLen]) + at += nameLen + n := int(le.Uint32(b[at:])) + at += 4 + if len(body) != at+n*chunkRecordEntryBytes { + return nil, errChunkRecordCheck + } + p.Entries = make([]chunkRecordEntry, n) for i := range p.Entries { - at := 28 + i*chunkRecordEntryBytes p.Entries[i].State = b[at] copy(p.Entries[i].Hash[:], b[at+1:at+chunkRecordEntryBytes]) + at += chunkRecordEntryBytes } return p, nil } func encodeChunkRecordHeader(h chunkRecordHeader) []byte { le := binary.LittleEndian - b := make([]byte, 8+24) - copy(b, chunkRecordHeaderMagic[:]) - le.PutUint64(b[8:], h.Epoch) - le.PutUint64(b[16:], h.Chunks) - le.PutUint64(b[24:], h.G) + b := make([]byte, 0, chunkRecordHeaderBytes) + b = append(b, chunkRecordHeaderMagic[:]...) + b = le.AppendUint16(b, chunkRecordKernelTable) + b = le.AppendUint64(b, h.Epoch) + b = le.AppendUint64(b, h.Chunks) + b = le.AppendUint64(b, h.G) return le.AppendUint32(b, crc32.Checksum(b, crc32cTable)) } func decodeChunkRecordHeader(b []byte) (chunkRecordHeader, error) { le := binary.LittleEndian - if len(b) != 8+24+4 || [8]byte(b[:8]) != chunkRecordHeaderMagic || - crc32.Checksum(b[:32], crc32cTable) != le.Uint32(b[32:]) { + if len(b) != chunkRecordHeaderBytes || [8]byte(b[:8]) != chunkRecordHeaderMagic || + crc32.Checksum(b[:len(b)-4], crc32cTable) != le.Uint32(b[len(b)-4:]) || + le.Uint16(b[8:]) != chunkRecordKernelTable { return chunkRecordHeader{}, errChunkRecordCheck } - return chunkRecordHeader{Epoch: le.Uint64(b[8:]), Chunks: le.Uint64(b[16:]), G: le.Uint64(b[24:])}, nil + return chunkRecordHeader{Epoch: le.Uint64(b[10:]), Chunks: le.Uint64(b[18:]), G: le.Uint64(b[26:])}, nil } // writeChunkRecordFile writes one page or the header as one durable step: @@ -123,31 +168,84 @@ func writeChunkRecordFile(dir, name string, b []byte) error { } func writeChunkRecordPage(dir string, p *chunkRecordPage) error { - return writeChunkRecordFile(dir, chunkRecordPageName(p.Segment), encodeChunkRecordPage(p)) -} - -// completeChunkRecord writes the record's header, which makes the record -// complete. It is written only after the store's fence: both descriptor slots -// carry the chunk-state marker at this epoch, synced and read back. Before -// it, an old version may still open the store from an unmarked slot. -func completeChunkRecord(s *WALStore, dir string, h chunkRecordHeader) error { - if err := s.FenceChunkStateUpgrade(h.Epoch); err != nil { + b, err := encodeChunkRecordPage(p) + if err != nil { return err } + return writeChunkRecordFile(dir, chunkRecordPageName(p.Segment), b) +} + +// readChunkRecordPages reads every page of a record of chunks chunks. A +// missing page or one that fails its check is named in bad. +func readChunkRecordPages(dir string, chunks uint64) (map[uint64]*chunkRecordPage, []uint64, error) { + pages := make(map[uint64]*chunkRecordPage) + var bad []uint64 + for seg := uint64(0); seg < chunkRecordSegments(chunks); seg++ { + b, err := os.ReadFile(filepath.Join(dir, chunkRecordPageName(seg))) + if err != nil && !errors.Is(err, os.ErrNotExist) { + return nil, nil, fmt.Errorf("storage: chunk record: %w", err) + } + var p *chunkRecordPage + if err == nil { + p, err = decodeChunkRecordPage(b, seg) + } + if err == nil && len(p.Entries) != chunkRecordSegmentLen(chunks, seg) { + err = errChunkRecordCheck + } + if err != nil { + bad = append(bad, seg) + continue + } + pages[seg] = p + } + return pages, bad, nil +} + +// completeChunkRecord ends the upgrade: it writes the record's header, which +// makes the record complete. In order: +// +// 1. the store's fence: both descriptor slots carry the chunk-state marker at +// epoch, synced and read back; from here no old version opens the store; +// 2. the store's durable position P, read after the fence; +// 3. every page present, passing its check, and valid through exactly P: a +// page behind P misses writes made before the fence (an old version's +// among them), a page ahead of P claims writes that are not durable; +// 4. the header, with G = P. +// +// Writes after step 2 are this version's: a restart takes each chunk whose +// store position is above its page's as changed. +func completeChunkRecord(s *WALStore, dir string, epoch, chunks uint64) error { + if err := s.FenceChunkStateUpgrade(epoch); err != nil { + return err + } + pos := s.NextLSN() - 1 + pages, bad, err := readChunkRecordPages(dir, chunks) + if err != nil { + return err + } + if len(bad) != 0 { + return fmt.Errorf("%w: page %d missing or damaged", errChunkRecordNotCurrent, bad[0]) + } + for seg := uint64(0); seg < chunkRecordSegments(chunks); seg++ { + if v := pages[seg].ValidThrough; v != pos { + return fmt.Errorf("%w: page %d valid through %d, store at %d", errChunkRecordNotCurrent, seg, v, pos) + } + } + h := chunkRecordHeader{Epoch: epoch, Chunks: chunks, G: pos} return writeChunkRecordFile(dir, chunkRecordHeaderName, encodeChunkRecordHeader(h)) } // chunkRecordLoad is what a restart may take from the record. type chunkRecordLoad struct { - // Complete: the header is valid and its epoch is the store's marked - // epoch. Pages then hold every segment whose page passed its check; - // Damaged names the rest, to be rebuilt from the store's bitmap. + // Complete: the header is valid, its epoch is the store's marked epoch + // and its G is not ahead of the store. Pages then hold every segment + // whose page passed its check and is not ahead of the store; Damaged + // names the rest, to be rebuilt from the store's bitmap. Complete bool Header chunkRecordHeader Pages map[uint64]*chunkRecordPage Damaged []uint64 - // Discarded says why an unfinished record was not used: the upgrade - // builds it again from the legacy presence. + // Discarded says why the record was not used at all. Discarded string } @@ -155,12 +253,20 @@ type chunkRecordLoad struct { // store's descriptor marker; storePos is the store's last durable position // after recovery (its replayed log included). // -// An unfinished record (no valid header) is used only when every page is -// present, passes its check, and is valid through storePos: a page behind -// the store missed writes, for example those an old version acknowledged and -// checkpointed between the pages and the fence, so it is never trusted. A -// store without the marker discards any record: an old version published -// after the pages. +// - A store without the marker discards any record: an old version +// published after the pages, and the upgrade starts again from the legacy +// presence. +// - An unfinished record (no valid header) is used only when every page is +// present, passes its check, and is valid through exactly storePos. A page +// behind missed writes, for example those an old version acknowledged +// between the pages and the fence; a page ahead claims writes that are not +// durable. Either way the upgrade builds the record again. +// - A complete record whose G is ahead of storePos is not used. A page ahead +// of storePos is damaged. A page behind it is taken: it is valid through +// the larger of G and its own position, and only this version can have +// written since (the fence keeps an old version out). The caller takes each +// chunk whose store position is above that as changed; the pages are not +// current by themselves. func loadChunkRecord(dir string, chunks uint64, marked bool, epoch, storePos uint64) (chunkRecordLoad, error) { var out chunkRecordLoad if !marked { @@ -172,38 +278,36 @@ func loadChunkRecord(dir string, chunks uint64, marked bool, epoch, storePos uin return out, hdrErr } complete := hdrErr == nil && h.Epoch == epoch && h.Chunks == chunks - pages := make(map[uint64]*chunkRecordPage) - var damaged []uint64 - for seg := uint64(0); seg < chunkRecordSegments(chunks); seg++ { - b, err := os.ReadFile(filepath.Join(dir, chunkRecordPageName(seg))) - if err != nil && !errors.Is(err, os.ErrNotExist) { - return out, fmt.Errorf("storage: chunk record: %w", err) - } - var p *chunkRecordPage - if err == nil { - p, err = decodeChunkRecordPage(b, seg) - } - if err == nil && len(p.Entries) != chunkRecordSegmentLen(chunks, seg) { - err = errChunkRecordCheck - } - if err != nil { - if !complete { - out.Discarded = fmt.Sprintf("unfinished record: page %d missing or damaged", seg) - return out, nil - } - damaged = append(damaged, seg) - continue - } - if !complete && p.ValidThrough < storePos { - out.Discarded = fmt.Sprintf("unfinished record: page %d valid through %d, store at %d", seg, p.ValidThrough, storePos) + if complete && h.G > storePos { + out.Discarded = fmt.Sprintf("header valid through %d, store at %d", h.G, storePos) + return out, nil + } + pages, bad, err := readChunkRecordPages(dir, chunks) + if err != nil { + return out, err + } + if !complete { + if len(bad) != 0 { + out.Discarded = fmt.Sprintf("unfinished record: page %d missing or damaged", bad[0]) return out, nil } - pages[seg] = p + for seg := uint64(0); seg < chunkRecordSegments(chunks); seg++ { + if v := pages[seg].ValidThrough; v != storePos { + out.Discarded = fmt.Sprintf("unfinished record: page %d valid through %d, store at %d", seg, v, storePos) + return out, nil + } + } + out.Pages = pages + return out, nil } - out.Complete, out.Header, out.Pages, out.Damaged = complete, h, pages, damaged - if !complete { - out.Header = chunkRecordHeader{} + for seg, p := range pages { + if p.ValidThrough > storePos { + delete(pages, seg) + bad = append(bad, seg) + } } + slices.Sort(bad) + out.Complete, out.Header, out.Pages, out.Damaged = true, h, pages, bad return out, nil } @@ -215,13 +319,6 @@ func readChunkRecordHeader(dir string) (chunkRecordHeader, error) { return decodeChunkRecordHeader(b) } -func chunkRecordSegmentLen(chunks, seg uint64) int { - if rest := chunks - seg*chunkRecordSegment; rest < chunkRecordSegment { - return int(rest) - } - return chunkRecordSegment -} - // ChunkStateMarker reports the store's chunk-state upgrade marker: whether its // descriptor carries the format and the epoch of local work. func (s *WALStore) ChunkStateMarker() (marked bool, epoch uint64) { diff --git a/seaweed-block/core/storage/chunk_record_test.go b/seaweed-block/core/storage/chunk_record_test.go index 7ef8f96c2d..c8bdc38c8e 100644 --- a/seaweed-block/core/storage/chunk_record_test.go +++ b/seaweed-block/core/storage/chunk_record_test.go @@ -18,6 +18,7 @@ func recordPagesAt(t *testing.T, s *WALStore, dir string, pos uint64) uint64 { for i := range p.Entries { p.Entries[i] = chunkRecordEntry{State: 2, Hash: [32]byte{byte(i)}} } + p.Candidates, p.PublishedSegment = 3, "seg/v1-0001" if err := writeChunkRecordPage(dir, p); err != nil { t.Fatal(err) } @@ -148,7 +149,7 @@ func TestCurrentPagesCompleteAfterTheFence(t *testing.T) { dir := chunkRecordDir(path) pos := s.NextLSN() - 1 chunks := recordPagesAt(t, s, dir, pos) - if err := completeChunkRecord(s, dir, chunkRecordHeader{Epoch: 7, Chunks: chunks, G: pos}); err != nil { + if err := completeChunkRecord(s, dir, 7, chunks); err != nil { t.Fatal(err) } marked, epoch := s.ChunkStateMarker() @@ -174,7 +175,7 @@ func TestRecordHeaderWaitsForTheFence(t *testing.T) { } return nil } - if err := completeChunkRecord(s, dir, chunkRecordHeader{Epoch: 7, Chunks: chunks}); err == nil { + if err := completeChunkRecord(s, dir, 7, chunks); err == nil { t.Fatal("header written with one slot fenced") } if _, err := os.Stat(filepath.Join(dir, chunkRecordHeaderName)); !errors.Is(err, os.ErrNotExist) { @@ -190,21 +191,30 @@ func TestDamagedPage(t *testing.T) { pos := s.NextLSN() - 1 chunks := recordPagesAt(t, s, dir, pos) page := filepath.Join(dir, chunkRecordPageName(0)) - b, err := os.ReadFile(page) - if err != nil { - t.Fatal(err) - } - b[30] ^= 1 - if err := os.WriteFile(page, b, 0o644); err != nil { - t.Fatal(err) + damage := func() { + t.Helper() + b, err := os.ReadFile(page) + if err != nil { + t.Fatal(err) + } + b[40] ^= 1 + if err := os.WriteFile(page, b, 0o644); err != nil { + t.Fatal(err) + } } + damage() got, err := loadChunkRecord(dir, chunks, true, 7, pos) if err != nil || got.Pages != nil || got.Discarded == "" { t.Fatalf("unfinished record with a damaged page: %+v %v", got, err) } - if err := completeChunkRecord(s, dir, chunkRecordHeader{Epoch: 7, Chunks: chunks, G: pos}); err != nil { + if err := completeChunkRecord(s, dir, 7, chunks); !errors.Is(err, errChunkRecordNotCurrent) { + t.Fatalf("a record with a damaged page was completed: %v", err) + } + recordPagesAt(t, s, dir, pos) + if err := completeChunkRecord(s, dir, 7, chunks); err != nil { t.Fatal(err) } + damage() got, err = loadChunkRecord(dir, chunks, true, 7, pos) if err != nil || !got.Complete || len(got.Damaged) != 1 || got.Damaged[0] != 0 { t.Fatalf("complete record with a damaged page: %+v %v", got, err) @@ -225,7 +235,7 @@ func TestStalePagesAreNeverCompleted(t *testing.T) { crashStore(t, old) next := reopenRecovered(t, path) - if err := completeChunkRecord(next, dir, chunkRecordHeader{Epoch: 7, Chunks: chunks, G: next.NextLSN() - 1}); err == nil { + if err := completeChunkRecord(next, dir, 7, chunks); err == nil { t.Fatal("a header was written over pages that miss an acknowledged write") } if _, err := os.Stat(filepath.Join(dir, chunkRecordHeaderName)); !errors.Is(err, os.ErrNotExist) { @@ -257,7 +267,7 @@ func TestCompleteRecordAheadOfTheStoreIsNotTrusted(t *testing.T) { dir := chunkRecordDir(path) pos := s.NextLSN() - 1 chunks := recordPagesAt(t, s, dir, pos) - if err := completeChunkRecord(s, dir, chunkRecordHeader{Epoch: 7, Chunks: chunks, G: pos}); err != nil { + if err := completeChunkRecord(s, dir, 7, chunks); err != nil { t.Fatal(err) } if err := writeChunkRecordFile(dir, chunkRecordHeaderName, encodeChunkRecordHeader(chunkRecordHeader{Epoch: 7, Chunks: chunks, G: pos + 5})); err != nil { @@ -271,3 +281,36 @@ func TestCompleteRecordAheadOfTheStoreIsNotTrusted(t *testing.T) { t.Fatalf("a header ahead of the store was trusted: %+v", got) } } + +// In a complete record a page ahead of the store is damaged; a page behind G +// is taken (valid through G); the V4 fields round-trip. +func TestCompleteRecordPagePositions(t *testing.T) { + s, path := newMappedCheckpointStoreV3(t) + writeSynced(t, s, 1, 2) + dir := chunkRecordDir(path) + pos := s.NextLSN() - 1 + chunks := recordPagesAt(t, s, dir, pos) + if err := completeChunkRecord(s, dir, 7, chunks); err != nil { + t.Fatal(err) + } + ahead := &chunkRecordPage{Segment: 0, ValidThrough: pos + 1, Entries: make([]chunkRecordEntry, chunkRecordSegmentLen(chunks, 0))} + if err := writeChunkRecordPage(dir, ahead); err != nil { + t.Fatal(err) + } + got, err := loadChunkRecord(dir, chunks, true, 7, pos) + if err != nil || !got.Complete || len(got.Damaged) != 1 || got.Damaged[0] != 0 { + t.Fatalf("page ahead of the store in a complete record: %+v %v", got, err) + } + behind := &chunkRecordPage{Segment: 0, ValidThrough: pos - 1, Candidates: 9, PublishedSegment: "seg/x", Entries: make([]chunkRecordEntry, chunkRecordSegmentLen(chunks, 0))} + if err := writeChunkRecordPage(dir, behind); err != nil { + t.Fatal(err) + } + writeSynced(t, s, 3, 3) + got, err = loadChunkRecord(dir, chunks, true, 7, s.NextLSN()-1) + if err != nil || !got.Complete || len(got.Damaged) != 0 { + t.Fatalf("cold page behind G not taken: %+v %v", got, err) + } + if p := got.Pages[0]; p.Candidates != 9 || p.PublishedSegment != "seg/x" || p.ValidThrough != pos-1 { + t.Fatalf("page fields: %+v", p) + } +} diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index eb32db7b31..a513466252 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -614,7 +614,7 @@ The "Machine / transition" column refers to Part A. Notes under a table record w | `INV-CHUNK-STATE-UPGRADE-FENCE-001` | The chunk-state upgrade fence succeeds only after both descriptor slots are fsynced and read back with the format marker and local-work epoch; uncertain writes or readback latch the store until reopen. | M1 format upgrade (caller not wired) | `core/storage/chunk_state_upgrade.go` `WALStore.FenceChunkStateUpgrade` | `TestChunkStateUpgradeFencesBothSlotsBeforeCompletion`, `TestChunkStateUpgradeUncertainSecondSlotRequiresReopen`, `TestChunkStateUpgradeFailureNeverCompletesUntilReopen`, `TestChunkStateUpgradeOldReaderPublicationRestartsTheFence` (`core/storage/chunk_state_upgrade_test.go`; single-publication and unlatched-readback mutant controls) | `CHUNK-STATE-r3` section 18; `SlotFence` rows 146-149 | STUB | | `INV-WAL-V3-DESCRIPTOR-001` | A v3 descriptor slot is used only if it is valid: a matching CRC32C (Castagnoli), every reserved byte zero, an accepted impl, a nonzero slotGen at most 2^64-2, a nonzero nonce, and geometry consistent under checked arithmetic. | M1 Opened (v3 descriptor selection) | `core/storage/superblock_v3.go` `decodeSlotV3`, `descriptorV3.validate`, `checkedAddV3` | `TestSlotV3VectorsDecodeAsExpected`, `TestSlotV3CounterExhaustion` (`core/storage/superblock_v3_test.go`) | — | STUB | | `INV-WAL-V3-DESCRIPTOR-BASE-001` | A valid v3 descriptor slot has CheckpointLSN ≤ mapCoveredCheckpoint ≤ W, and BASE fields that follow the rules of its BASE state. | M1 Opened (v3 descriptor selection) | `core/storage/superblock_v3.go` `descriptorV3.validate` | `TestSlotV3VectorsDecodeAsExpected`, `TestSlotV3InstalledMayHaveCursorBelowTail` (`core/storage/superblock_v3_test.go`) | — | STUB | -| `INV-CHUNK-RECORD-PAGES-CURRENT-001` | An unfinished chunk record (no valid header) is used only when every page is present, passes its check and is valid through the store's durable position after recovery; an unmarked store discards it; the header follows the store's two-slot fence. | M1 Opened (chunk record load, v1.1 upgrade) | `core/storage/chunk_record.go` `loadChunkRecord`, `completeChunkRecord` | `TestUnfinishedRecordPagesBehindTheStoreAreNotTrusted`, `TestUnfinishedRecordAfterAnOldCheckpointIsDiscarded`, `TestRecordHeaderWaitsForTheFence`, `TestDamagedPage` (`core/storage/chunk_record_test.go`) | SlotFence `PagesCurrent`, `FenceHolds` (the per-chunk state models are not in this repository) | STUB | +| `INV-CHUNK-RECORD-PAGES-CURRENT-001` | An unfinished chunk record is used, and its header written, only when every page passes its check and is valid through exactly the store's durable position after recovery (behind: missed writes; ahead: not durable); an unmarked store discards it; the header follows the two-slot fence. | M1 Opened (chunk record load, v1.1 upgrade) | `core/storage/chunk_record.go` `loadChunkRecord`, `completeChunkRecord` | `TestUnfinishedRecordPagesBehindTheStoreAreNotTrusted`, `TestUnfinishedRecordAfterAnOldCheckpointIsDiscarded`, `TestStalePagesAreNeverCompleted`, `TestUnfinishedPagesAheadOfTheStoreAreNotTrusted`, `TestCompleteRecordAheadOfTheStoreIsNotTrusted`, `TestCompleteRecordPagePositions`, `TestRecordHeaderWaitsForTheFence`, `TestDamagedPage` (`core/storage/chunk_record_test.go`) | SlotFence `PagesCurrent`, `FenceHolds`; pages carry chunk-kernel/2 states (the per-chunk state models are not in this repository) | STUB | | `INV-WAL-V3-DESCRIPTOR-SELECT-001` | Of the two v3 descriptor slots, open selects the valid selectable slot with the higher slotGen, and never a prepared migration slot. | M1 Opened (v3 descriptor selection) | `core/storage/superblock_v3.go` `selectSlotV3` | `TestSlotV3Selection` (`core/storage/superblock_v3_test.go`); `TestWALStoreV3CloseKeepsTheNewestSlotAAndItsW` (`core/storage/walstore_close_v3_test.go`) | — | ACTIVE | | `INV-WAL-V3-DESCRIPTOR-CONFLICT-001` | Open is refused when both v3 descriptor slots are valid but carry the same slotGen with different bytes, or disagree on the UUID or the geometry. | M1 Opened (v3 descriptor selection) | `core/storage/superblock_v3.go` `selectSlotV3`, `sameGeometryV3` | `TestSlotV3Selection` (`core/storage/superblock_v3_test.go`) | — | ACTIVE | | `INV-WAL-V3-RECORD-001` | An impl-3 WAL record has a fixed 62-byte prefix, 70 bytes of overhead in all, a CRC32C (Castagnoli) and an explicit EntrySize. | M1 v3 record framing | `core/storage/wal_entry_v3.go` `encodeWALEntryV3`, `payloadSizeV3` | `TestWALEntryV3_CanonicalBarrierBytes`, `TestWALEntryV3_TypeFootprints` | `WALRecovery` abstract record validity; physical byte rules are code-test obligations | ACTIVE |