Skip to main content

gunnar_core/
txlog.rs

1//! **The transaction log** — every mutation of durable state on the primary,
2//! in one strictly increasing sequence, appended before the state changes,
3//! fsynced per commit, torn tail detected and cut on open.
4//!
5//! This is what the twin holds. The owner's ruling (2026-09-14): *"the twin
6//! must hold each transaction sent to the master; if it has missed something
7//! it must ask the master for the missing part before continuing to append."*
8//! The design note is `.nornir/twin-transaction-log.md`; this file is the
9//! storage half of it and knows nothing about the wire.
10//!
11//! # What a transaction is
12//!
13//! One frame: a [`Kind`], a small JSON **meta** (which repository, which ref,
14//! which document — [`Meta`]), and a **blob** that is the content itself —
15//! the pack bytes a client sent, the bytes of a control-store document, an
16//! LFS object. The blob is written from the caller's buffer with one
17//! `write_vectored`, never copied into a frame first. Nothing in the log is a
18//! reference to something the twin would have to fetch elsewhere: a twin
19//! holding entries `1..=n` holds everything the primary was told in that
20//! window, and a fresh primary is rebuilt from the twin alone.
21//!
22//! # On disk
23//!
24//! `<data-root>/txlog/` holds segments named by their first sequence number,
25//! `seg-<first>.log`, each a plain run of frames:
26//!
27//! ```text
28//! magic   "GTX1"      4     header crc32c covers 0..28
29//! seq     u64 LE      8
30//! kind    u16 LE      2
31//! flags   u16 LE      2     reserved, 0
32//! meta_len u32 LE     4
33//! blob_len u64 LE     8
34//! hcrc    u32 LE      4
35//! meta    meta_len
36//! blob    blob_len
37//! pcrc    u32 LE      4     crc32c over meta ‖ blob
38//! ```
39//!
40//! Beside each segment, `seg-<first>.idx` is a run of `u64 LE` byte offsets,
41//! one per frame, so `seq → (segment, offset)` is one seek. The index is a
42//! cache: it is appended **after** the frame's fsync and is not fsynced
43//! itself. On open the last index entry is checked against the data and the
44//! index is rebuilt from the data if it disagrees. The data is the truth.
45//!
46//! # Durability and the torn tail
47//!
48//! `append` = write the frame, `fdatasync` the segment, then return the
49//! sequence number. **Group commit** (perf twin-apply-fsync, 2026-10-06): the
50//! frame is staged under the writer lock and the fdatasync is
51//! [`Log::sync_through`]'s — one leader syncs every frame staged so far, outside
52//! the writer lock, so concurrent appenders share it; a twin writes a whole
53//! fetched group with [`Pending::commit_unsynced`] and makes it durable with ONE
54//! [`Log::sync`]. Either way a frame is visible to readers, indexed and counted in
55//! the head only once it is durable — the head is what a twin acknowledges.
56//! A crash before the fsync returns can leave a partial
57//! frame at the end of the last segment and nowhere else; `open` walks from
58//! the last indexed frame, verifies every frame it finds (header crc, both
59//! lengths against the file, payload crc, expected seq), and truncates the
60//! file at the first bad byte. Anything after a torn frame is unreachable
61//! anyway, and an append after garbage would never be read back.
62//!
63//! # Why not `arrow_log` / `history`
64//!
65//! `arrow_log` has the durability machinery (group commit, torn-tail cut) but
66//! is retired, per-repository, keeps no persisted sequence and no streaming
67//! reader, and its own manifest says it goes when the legacy secret log is
68//! deleted. `history` derives a `push_seq` at read time per repository. The
69//! bus in `gunnar-grpc` has a global `seq` but *"there is no durable log and
70//! this is not one"*. A twin that must hold every transaction needs one
71//! persisted, global, gap-free number, and that is this module's whole job.
72//!
73//! # Sequential by design
74//!
75//! Every write here is an append and every read is a forward scan or one
76//! seek — the twin sits on an HDD-tier volume and the log is laid out for
77//! it. No B-tree, no random rewrite, no compaction that copies. Retention is
78//! whole-segment deletion below a floor ([`Log::truncate_before`]).
79
80/// The tee: the decorators that make every store write a frame first.
81#[path = "txlog_tee.rs"]
82pub mod tee;
83
84use std::fs::{self, File, OpenOptions};
85use std::io::{self, Read, Seek, SeekFrom, Write};
86use std::path::{Path, PathBuf};
87use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
88use std::sync::{Arc, Mutex, Weak};
89
90use serde::{Deserialize, Serialize};
91
92const MAGIC: [u8; 4] = *b"GTX1";
93const HEADER_LEN: usize = 32;
94const TRAILER_LEN: usize = 4;
95const SEG_PREFIX: &str = "seg-";
96const SEG_EXT: &str = "log";
97const IDX_EXT: &str = "idx";
98
99/// Frames are appended to the current segment until it passes this size;
100/// the next append starts a new one. Retention deletes whole segments, so
101/// this is the granularity of "what may be truncated".
102pub const DEFAULT_SEGMENT_BYTES: u64 = 1 << 30;
103
104/// The kinds of transaction, one per mutation seam. The number is the wire
105/// and disk value; **never renumber, only add**.
106#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
107#[repr(u16)]
108pub enum Kind {
109    /// A pack landed in a repository: blob = the pack bytes as the client
110    /// sent them. Meta: [`Meta::Pack`].
111    Pack = 1,
112    /// One or more reference edits in a repository, every ref kind — heads,
113    /// tags, notes, remotes, salvage, superseded, and `HEAD`. No blob.
114    /// Meta: [`Meta::Refs`].
115    Refs = 2,
116    /// A content-addressed blob of the control store (a document shard or a
117    /// document root). Blob = the bytes; meta carries the sha-256 id.
118    ControlBlob = 3,
119    /// A control-store document pointer moved (`commit_doc`): the catalog
120    /// (repositories, visibility, tombstones), the identity table
121    /// (principals, keys, grants, tokens, memberships, gates), PKI, mirrors,
122    /// flow, pulls, policy, entitlements. No blob; the bytes arrived as
123    /// [`Kind::ControlBlob`] entries before it.
124    ControlDoc = 4,
125    /// A sealed secret was written or tombstoned. Blob = the sealed bytes
126    /// exactly as the secret store holds them (sealed with the box's KEK).
127    Secret = 5,
128    /// An LFS object landed. Blob = the object bytes. Meta: [`Meta::Lfs`].
129    LfsPut = 6,
130    /// An LFS object was removed. No blob.
131    LfsDelete = 7,
132    /// A repository's object store was created or purged on disk (the
133    /// catalog row is a `ControlDoc`; this is the store beside it).
134    RepoStore = 8,
135    /// **The box's key-encrypting key** — the first frame of a primary's log,
136    /// so a restore into a fresh box can read every `Secret` frame after it.
137    /// Blob = the 32 KEK bytes exactly as `secrets.kek` holds them. The twin
138    /// holds it: a twin that held every sealed secret and not the key would be
139    /// a backup of nothing.
140    Kek = 9,
141    /// One line appended to an append-only signed log under the data root
142    /// (attestations, transparency heads, forge events, build records,
143    /// membership). Blob = the line without its newline. Meta: [`Meta::Append`].
144    Append = 10,
145    /// A whole document under the data root replaced or removed
146    /// (`replication.json`, `webhooks.json`). Blob = the bytes. Meta: [`Meta::File`].
147    File = 11,
148    /// **Never in a log**: the terminator of a twin SNAPSHOT stream
149    /// (`Mirror.SnapshotFetch`, `Bootstrap.Restore`). Every item before it is an
150    /// entry of the kinds above with `seq` 0; this one says at which sequence the
151    /// state they carry was taken. Meta: [`Meta::SnapshotAt`].
152    SnapshotAt = 12,
153    /// **A frame whose content was ERASED in place** ([`Log::erase`]): it was a
154    /// `Pack`, `Refs`, `LfsPut` or `LfsDelete` of a repository whose account was
155    /// deleted. The meta is the original one (it still names the repository);
156    /// the blob reads as empty and its bytes are gone from the file. Never
157    /// appended — only ever written over a frame that is already there, so the
158    /// sequence, the offsets and the twin's gap rule stay exactly as they were.
159    /// Applying one does nothing: the data it carried is what was deleted.
160    Redacted = 13,
161    /// **An account's data was deleted** (owner ruling g-nonpay, 2026-09-29):
162    /// every frame BEFORE this one of the repositories it names is erased
163    /// ([`Log::erase`]) — on the primary when it appends it, on the twin when it
164    /// stores it, on a box being restored when it replays it. No blob.
165    /// Meta: [`Meta::Erase`].
166    Erase = 14,
167    /// **Last-seen stamps** (owner, 2026-10-21): the principals whose last
168    /// sign-in or last operation moved since the previous flush, ONE frame per
169    /// flush ([`crate::activity`]). Blob = JSON, principal id → stamps. Applied
170    /// on the twin into the same control-database table; a snapshot carries
171    /// the whole table as one item of this kind. Meta: [`Meta::Activity`].
172    Activity = 15,
173}
174
175impl Kind {
176    /// The disk value back to a kind. `None` for a value this build does
177    /// not know — a newer primary's frame; the twin still stores it.
178    pub fn from_u16(v: u16) -> Option<Self> {
179        Some(match v {
180            1 => Kind::Pack,
181            2 => Kind::Refs,
182            3 => Kind::ControlBlob,
183            4 => Kind::ControlDoc,
184            5 => Kind::Secret,
185            6 => Kind::LfsPut,
186            7 => Kind::LfsDelete,
187            8 => Kind::RepoStore,
188            9 => Kind::Kek,
189            10 => Kind::Append,
190            11 => Kind::File,
191            12 => Kind::SnapshotAt,
192            13 => Kind::Redacted,
193            14 => Kind::Erase,
194            15 => Kind::Activity,
195            _ => return None,
196        })
197    }
198}
199
200/// One reference edit as the log records it: name, the value expected before
201/// (`None` = must not exist, `Some("")` = no claim), the value after
202/// (`None` = delete). Oids are hex; a symbolic target is `ref:<name>`.
203#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
204pub struct RefEdit {
205    /// Full ref name, or `HEAD`.
206    pub name: String,
207    /// `None` = no claim about the previous value (a plain `put_refs`);
208    /// `Some(None)` = must not have existed; `Some(Some(oid))` = must have
209    /// been `oid`.
210    #[serde(default, skip_serializing_if = "Option::is_none")]
211    pub old: Option<Option<String>>,
212    /// `None` deletes.
213    pub new: Option<String>,
214    /// Peeled target of an annotated tag, hex.
215    #[serde(default, skip_serializing_if = "Option::is_none")]
216    pub peeled: Option<String>,
217    /// Symbolic target, full ref name, when `new` is a symref.
218    #[serde(default, skip_serializing_if = "Option::is_none")]
219    pub symref: Option<String>,
220}
221
222/// The typed meta of a frame. Serialized as JSON; every variant is additive.
223#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
224#[serde(tag = "k", rename_all = "snake_case")]
225pub enum Meta {
226    /// [`Kind::Pack`].
227    Pack {
228        /// Repository key, `namespace/name`.
229        repo: String,
230        /// Hash kind of the repository, `sha1` or `sha256`.
231        hash: String,
232    },
233    /// [`Kind::Refs`].
234    Refs {
235        /// Repository key.
236        repo: String,
237        /// Whether the batch is all-or-nothing (`put_refs_cas`).
238        atomic: bool,
239        /// The edits, in order.
240        edits: Vec<RefEdit>,
241    },
242    /// [`Kind::ControlBlob`].
243    ControlBlob {
244        /// sha-256 of the blob, hex.
245        id: String,
246    },
247    /// [`Kind::ControlDoc`].
248    ControlDoc {
249        /// Document name, e.g. `refs/gunnar/auth`.
250        name: String,
251        /// Blob id the pointer held before, hex, `None` = absent.
252        expected: Option<String>,
253        /// Blob id it holds now, hex.
254        new: String,
255    },
256    /// [`Kind::Secret`].
257    Secret {
258        /// Secret key.
259        key: String,
260        /// `true` = tombstone (the blob is empty).
261        deleted: bool,
262    },
263    /// [`Kind::LfsPut`] and [`Kind::LfsDelete`].
264    Lfs {
265        /// Repository key.
266        repo: String,
267        /// Object id, hex.
268        oid: String,
269        /// Size in bytes.
270        size: u64,
271    },
272    /// [`Kind::RepoStore`].
273    RepoStore {
274        /// Repository key.
275        repo: String,
276        /// `created` or `purged`.
277        op: String,
278        /// Hash kind.
279        hash: String,
280    },
281    /// [`Kind::Kek`]. No fields: the blob is the key.
282    Kek {},
283    /// [`Kind::Append`].
284    Append {
285        /// The log's path relative to the data root, e.g. `attestations.jsonl`.
286        log: String,
287    },
288    /// [`Kind::File`].
289    File {
290        /// The document's path relative to the data root.
291        path: String,
292        /// `true` = removed (the blob is empty).
293        deleted: bool,
294    },
295    /// [`Kind::Activity`]. No fields: the blob is the batch.
296    Activity {},
297    /// [`Kind::SnapshotAt`].
298    SnapshotAt {
299        /// The state the snapshot's items carry is the log's state through this sequence.
300        at: u64,
301    },
302    /// [`Kind::Erase`].
303    Erase {
304        /// The account whose data went.
305        account: String,
306        /// The repository keys (`namespace/name`) whose frames before this one are erased —
307        /// exactly the stores the purge removed, never a prefix of them.
308        repos: Vec<String>,
309        /// Why, for the audit trail.
310        reason: String,
311    },
312}
313
314impl Meta {
315    /// The repository a data frame's meta names — `Pack`, `Refs`, `Lfs`, `RepoStore`.
316    pub fn repo(&self) -> Option<&str> {
317        match self {
318            Meta::Pack { repo, .. } | Meta::Refs { repo, .. } | Meta::Lfs { repo, .. } | Meta::RepoStore { repo, .. } => Some(repo),
319            _ => None,
320        }
321    }
322}
323
324/// What [`Log::erase`] did.
325#[derive(Debug, Clone, Default, PartialEq, Eq)]
326pub struct Erased {
327    /// Frames rewritten as [`Kind::Redacted`] by this call.
328    pub frames: u64,
329    /// Blob bytes gone from the files.
330    pub bytes: u64,
331}
332
333impl Meta {
334    /// Serialize to the bytes the frame carries.
335    pub fn to_bytes(&self) -> Vec<u8> {
336        serde_json::to_vec(self).expect("Meta serializes")
337    }
338
339    /// Parse a frame's meta.
340    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
341        serde_json::from_slice(bytes).map_err(|e| Error::BadMeta(e.to_string()))
342    }
343}
344
345/// A frame read back.
346#[derive(Debug, Clone, PartialEq, Eq)]
347pub struct Entry {
348    /// Its sequence number.
349    pub seq: u64,
350    /// Kind as stored (a twin keeps kinds it does not know).
351    pub kind: u16,
352    /// Meta bytes (JSON).
353    pub meta: Vec<u8>,
354    /// Blob bytes.
355    pub blob: Vec<u8>,
356}
357
358impl Entry {
359    /// The typed kind, if this build knows it.
360    pub fn kind(&self) -> Option<Kind> {
361        Kind::from_u16(self.kind)
362    }
363    /// The typed meta.
364    pub fn meta(&self) -> Result<Meta> {
365        Meta::from_bytes(&self.meta)
366    }
367}
368
369/// Where a frame's blob lies, for a reader that streams it in chunks rather
370/// than loading it.
371#[derive(Debug, Clone, PartialEq, Eq)]
372pub struct Located {
373    /// Sequence number.
374    pub seq: u64,
375    /// Kind as stored.
376    pub kind: u16,
377    /// Meta bytes.
378    pub meta: Vec<u8>,
379    /// The segment file the blob is in.
380    pub path: PathBuf,
381    /// Byte offset of the blob inside that file.
382    pub blob_offset: u64,
383    /// Blob length.
384    pub blob_len: u64,
385    /// **The frame's payload crc32c (meta ‖ blob)** — `Some` only on the LAST piece (restore-large-
386    /// entry, 2026-10-07): what a sender stamps on the entry's last chunk so a box that put the
387    /// entry together across several streams applies it only if it IS the entry. From
388    /// [`Log::read_pieces`] it is CHECKED: the crc over the bytes read matched the trailer. From
389    /// [`Log::read_pieces_from`] at an offset it is the trailer AS STORED, unchecked here — the
390    /// bytes below the offset were not read — and the receiver, which holds the whole entry,
391    /// checks it. `None` from [`Log::locate_blob`] and on an erased frame (its crc was over bytes
392    /// now gone).
393    pub payload_crc: Option<u32>,
394}
395
396/// Errors of the log.
397#[derive(Debug, thiserror::Error)]
398pub enum Error {
399    /// I/O.
400    #[error("txlog: {0}")]
401    Io(#[from] io::Error),
402    /// A frame's meta did not parse.
403    #[error("txlog: bad meta: {0}")]
404    BadMeta(String),
405    /// The requested sequence is below what the log retains.
406    #[error("txlog: seq {seq} not retained (first retained is {first})")]
407    NotRetained {
408        /// What was asked for.
409        seq: u64,
410        /// The oldest sequence still on disk.
411        first: u64,
412    },
413    /// The requested sequence is above the head.
414    #[error("txlog: seq {seq} is beyond head {head}")]
415    BeyondHead {
416        /// What was asked for.
417        seq: u64,
418        /// The newest committed sequence.
419        head: u64,
420    },
421    /// A frame in the middle of a segment (not the tail) is corrupt.
422    #[error("txlog: corrupt frame at {path}:{offset}: {what}")]
423    Corrupt {
424        /// Segment.
425        path: PathBuf,
426        /// Offset of the frame.
427        offset: u64,
428        /// What was wrong.
429        what: &'static str,
430    },
431    /// The log's files are RELEASED ([`Log::release`]): the data set under it is being grown
432    /// and unmounted, so nothing may write until [`Log::reacquire`].
433    #[error("txlog: released — the files under {0} are given up while the data set is grown; writes resume after the remount")]
434    Released(PathBuf),
435    /// An append was asked to start at a sequence that is not `head + 1`.
436    #[error("txlog: append out of order: got {got}, expected {expected}")]
437    OutOfOrder {
438        /// The sequence offered.
439        got: u64,
440        /// The one the log would accept.
441        expected: u64,
442    },
443    /// This log belongs to a TWIN: it holds the primary's numbers and
444    /// originates none. A local mutation on a twin is refused here, loudly,
445    /// rather than stealing a sequence number from the primary.
446    #[error("txlog: this box is a twin (or restoring); it originates no transactions")]
447    Follower,
448    /// The segment directory holds a file the layout does not describe.
449    #[error("txlog: unexpected file {0}")]
450    Unexpected(PathBuf),
451    /// **The log is POISONED** (stinky rows 3 and 6, 2026-10-10): a frame's `fdatasync` failed,
452    /// or the cut of a half-written frame did. After a failed fsync the kernel may have dropped
453    /// the dirty pages and the NEXT fsync succeeds without them (fsyncgate) — a frame its writer
454    /// was told had failed would then be published by the next leader and applied by the twin.
455    /// After a failed cut the next frame would land behind garbage. So nothing more is written:
456    /// every append refuses with this until the log is reopened, which recovers from the last
457    /// durable frame.
458    #[error("txlog: poisoned under {dir} — {cause}; every write refuses until the log is reopened from its last durable frame")]
459    Poisoned {
460        /// The log's directory.
461        dir: PathBuf,
462        /// What failed.
463        cause: String,
464    },
465}
466
467/// Result alias.
468pub type Result<T> = std::result::Result<T, Error>;
469
470// ── crc32c (Castagnoli), table-driven, no dependency ─────────────────────────
471
472const CRC32C_POLY: u32 = 0x82F6_3B78;
473
474const fn crc32c_table() -> [u32; 256] {
475    let mut table = [0u32; 256];
476    let mut i = 0;
477    while i < 256 {
478        let mut c = i as u32;
479        let mut k = 0;
480        while k < 8 {
481            c = if c & 1 != 0 { CRC32C_POLY ^ (c >> 1) } else { c >> 1 };
482            k += 1;
483        }
484        table[i] = c;
485        i += 1;
486    }
487    table
488}
489
490static CRC32C_TABLE: [u32; 256] = crc32c_table();
491
492/// Streaming crc32c.
493#[derive(Debug, Clone, Copy)]
494pub struct Crc32c(u32);
495
496impl Default for Crc32c {
497    fn default() -> Self {
498        Self::new()
499    }
500}
501
502impl Crc32c {
503    /// Fresh.
504    pub const fn new() -> Self {
505        Crc32c(0xFFFF_FFFF)
506    }
507    /// Feed bytes.
508    pub fn update(&mut self, bytes: &[u8]) {
509        let mut c = self.0;
510        for &b in bytes {
511            c = CRC32C_TABLE[((c ^ b as u32) & 0xFF) as usize] ^ (c >> 8);
512        }
513        self.0 = c;
514    }
515    /// The checksum.
516    pub fn finish(self) -> u32 {
517        self.0 ^ 0xFFFF_FFFF
518    }
519    /// The running state, to be kept with the bytes it covers and [`Crc32c::resume`]d — a
520    /// checksum carried across a restart (restore-after-snapshot: a snapshot item staged over
521    /// several streams).
522    pub fn state(&self) -> u32 {
523        self.0
524    }
525    /// Go on from a kept [`Crc32c::state`].
526    pub const fn resume(state: u32) -> Self {
527        Crc32c(state)
528    }
529}
530
531/// crc32c of one buffer.
532pub fn crc32c(bytes: &[u8]) -> u32 {
533    let mut c = Crc32c::new();
534    c.update(bytes);
535    c.finish()
536}
537
538// ── frame header ─────────────────────────────────────────────────────────────
539
540#[derive(Debug, Clone, Copy, PartialEq, Eq)]
541struct Header {
542    seq: u64,
543    kind: u16,
544    meta_len: u32,
545    blob_len: u64,
546}
547
548impl Header {
549    fn encode(&self) -> [u8; HEADER_LEN] {
550        let mut h = [0u8; HEADER_LEN];
551        h[0..4].copy_from_slice(&MAGIC);
552        h[4..12].copy_from_slice(&self.seq.to_le_bytes());
553        h[12..14].copy_from_slice(&self.kind.to_le_bytes());
554        h[14..16].copy_from_slice(&0u16.to_le_bytes());
555        h[16..20].copy_from_slice(&self.meta_len.to_le_bytes());
556        h[20..28].copy_from_slice(&self.blob_len.to_le_bytes());
557        let crc = crc32c(&h[0..28]);
558        h[28..32].copy_from_slice(&crc.to_le_bytes());
559        h
560    }
561
562    fn decode(h: &[u8]) -> Option<Self> {
563        if h.len() < HEADER_LEN || h[0..4] != MAGIC {
564            return None;
565        }
566        let crc = u32::from_le_bytes(h[28..32].try_into().ok()?);
567        if crc32c(&h[0..28]) != crc {
568            return None;
569        }
570        Some(Header {
571            seq: u64::from_le_bytes(h[4..12].try_into().ok()?),
572            kind: u16::from_le_bytes(h[12..14].try_into().ok()?),
573            meta_len: u32::from_le_bytes(h[16..20].try_into().ok()?),
574            blob_len: u64::from_le_bytes(h[20..28].try_into().ok()?),
575        })
576    }
577
578    fn frame_len(&self) -> u64 {
579        HEADER_LEN as u64 + self.meta_len as u64 + self.blob_len + TRAILER_LEN as u64
580    }
581}
582
583// ── segments ─────────────────────────────────────────────────────────────────
584
585fn seg_path(dir: &Path, first: u64) -> PathBuf {
586    dir.join(format!("{SEG_PREFIX}{first:020}.{SEG_EXT}"))
587}
588
589fn idx_path(dir: &Path, first: u64) -> PathBuf {
590    dir.join(format!("{SEG_PREFIX}{first:020}.{IDX_EXT}"))
591}
592
593fn parse_seg_name(name: &str) -> Option<(u64, bool)> {
594    let rest = name.strip_prefix(SEG_PREFIX)?;
595    let (num, ext) = rest.split_once('.')?;
596    let first: u64 = num.parse().ok()?;
597    match ext {
598        SEG_EXT => Some((first, true)),
599        IDX_EXT => Some((first, false)),
600        _ => None,
601    }
602}
603
604fn sync_dir(dir: &Path) -> io::Result<()> {
605    File::open(dir)?.sync_all()
606}
607
608/// **Make `len` bytes at `at` gone**: a punched hole where the filesystem can (the
609/// space is freed and the range reads as zeros), zeros written over it where it cannot.
610fn zero_range(file: &mut File, at: u64, len: u64) -> io::Result<()> {
611    if len == 0 {
612        return Ok(());
613    }
614    #[cfg(target_os = "linux")]
615    {
616        use rustix::fs::{fallocate, FallocateFlags};
617        if fallocate(&*file, FallocateFlags::PUNCH_HOLE | FallocateFlags::KEEP_SIZE, at, len).is_ok() {
618            return Ok(());
619        }
620    }
621    let zeros = vec![0u8; (1usize << 20).min(len as usize)];
622    file.seek(SeekFrom::Start(at))?;
623    let mut left = len;
624    while left > 0 {
625        let n = left.min(zeros.len() as u64) as usize;
626        file.write_all(&zeros[..n])?;
627        left -= n as u64;
628    }
629    Ok(())
630}
631
632/// One segment as the log knows it: its first sequence, its frame offsets.
633#[derive(Debug)]
634struct Segment {
635    first: u64,
636    /// Byte offset of every frame, in order. `offsets.len()` frames.
637    offsets: Vec<u64>,
638    /// Byte length of the valid prefix.
639    len: u64,
640}
641
642impl Segment {
643    fn last_seq(&self) -> Option<u64> {
644        if self.offsets.is_empty() {
645            None
646        } else {
647            Some(self.first + self.offsets.len() as u64 - 1)
648        }
649    }
650}
651
652/// Verify frames from `from` to the end of `file`, appending offsets.
653/// Returns the length of the valid prefix and whether the tail was torn.
654fn scan_frames(
655    path: &Path,
656    file: &mut File,
657    first: u64,
658    offsets: &mut Vec<u64>,
659    from: u64,
660) -> Result<(u64, bool)> {
661    let total = file.metadata()?.len();
662    let mut pos = from;
663    let mut hdr = [0u8; HEADER_LEN];
664    let mut buf = Vec::new();
665    while pos < total {
666        if total - pos < HEADER_LEN as u64 {
667            return Ok((pos, true));
668        }
669        file.seek(SeekFrom::Start(pos))?;
670        file.read_exact(&mut hdr)?;
671        let Some(h) = Header::decode(&hdr) else {
672            return Ok((pos, true));
673        };
674        let expect = first + offsets.len() as u64;
675        if h.seq != expect {
676            return Ok((pos, true));
677        }
678        let payload = h.meta_len as u64 + h.blob_len;
679        if total - pos < h.frame_len() {
680            return Ok((pos, true));
681        }
682        // An ERASED frame's payload no longer matches its checksum — by design,
683        // its bytes are gone ([`Log::erase`]). Its header crc vouched for it.
684        if h.kind == Kind::Redacted as u16 {
685            offsets.push(pos);
686            pos += h.frame_len();
687            continue;
688        }
689        // Verify the payload crc with a bounded buffer: the blob may be large.
690        let mut crc = Crc32c::new();
691        let mut remaining = payload;
692        buf.resize(1 << 20, 0);
693        while remaining > 0 {
694            let n = remaining.min(buf.len() as u64) as usize;
695            file.read_exact(&mut buf[..n])?;
696            crc.update(&buf[..n]);
697            remaining -= n as u64;
698        }
699        let mut tr = [0u8; TRAILER_LEN];
700        file.read_exact(&mut tr)?;
701        if u32::from_le_bytes(tr) != crc.finish() {
702            return Ok((pos, true));
703        }
704        let _ = path;
705        offsets.push(pos);
706        pos += h.frame_len();
707    }
708    Ok((pos, false))
709}
710
711fn open_segment(dir: &Path, first: u64, repair: bool) -> Result<Segment> {
712    let path = seg_path(dir, first);
713    let mut file = OpenOptions::new().read(true).write(repair).open(&path)?;
714    let total = file.metadata()?.len();
715
716    // Trust the index up to the last entry that verifies against the data.
717    let mut offsets: Vec<u64> = Vec::new();
718    let ipath = idx_path(dir, first);
719    if let Ok(bytes) = fs::read(&ipath) {
720        let n = bytes.len() / 8;
721        offsets.reserve(n);
722        for i in 0..n {
723            let off = u64::from_le_bytes(bytes[i * 8..i * 8 + 8].try_into().expect("8 bytes"));
724            offsets.push(off);
725        }
726        // Offsets must be strictly increasing and inside the file.
727        let monotone = offsets.windows(2).all(|w| w[0] < w[1]);
728        if !monotone || offsets.first().is_some_and(|&o| o != 0) {
729            offsets.clear();
730        }
731        while let Some(&last) = offsets.last() {
732            if last + HEADER_LEN as u64 > total {
733                offsets.pop();
734                continue;
735            }
736            file.seek(SeekFrom::Start(last))?;
737            let mut hdr = [0u8; HEADER_LEN];
738            file.read_exact(&mut hdr)?;
739            match Header::decode(&hdr) {
740                Some(h) if h.seq == first + offsets.len() as u64 - 1 && last + h.frame_len() <= total => {
741                    break;
742                }
743                _ => {
744                    offsets.pop();
745                }
746            }
747        }
748    }
749
750    // Resume AT the last indexed frame, not after it: the index vouched for
751    // its header only, and the torn tail is exactly that frame — a crash
752    // between the payload write and the fsync leaves a header that decodes
753    // over a payload that does not. `scan_frames` re-verifies it in full and
754    // puts it back when it is whole.
755    let trusted = offsets.len();
756    let resume = offsets.pop().unwrap_or(0);
757    let (valid_len, torn) = scan_frames(&path, &mut file, first, &mut offsets, resume)?;
758
759    if torn && repair {
760        tracing::warn!(
761            segment = %path.display(),
762            kept = valid_len,
763            discarded = total - valid_len,
764            "txlog: torn tail cut"
765        );
766        file.set_len(valid_len)?;
767        file.sync_all()?;
768    }
769    if (torn && repair) || offsets.len() != trusted {
770        // Rewrite the index to exactly the verified frames.
771        if repair {
772            let mut bytes = Vec::with_capacity(offsets.len() * 8);
773            for off in &offsets {
774                bytes.extend_from_slice(&off.to_le_bytes());
775            }
776            fs::write(&ipath, bytes)?;
777        }
778    }
779    Ok(Segment {
780        first,
781        offsets,
782        len: valid_len,
783    })
784}
785
786// ── the log ──────────────────────────────────────────────────────────────────
787
788struct Writer {
789    /// The open segment's data file, positioned at its end.
790    file: File,
791    /// Its index file.
792    idx: File,
793    /// The open segment as the WRITER knows it: every frame committed into it, including the
794    /// ones of a group commit not yet made durable ([`Pending::commit_unsynced`]). Readers go
795    /// by [`Log::segments`] and [`Log::head_seq`], which hold only what is durable.
796    seg: Segment,
797    /// Bytes of the open segment already handed to the kernel; `buf` follows them.
798    disk_len: u64,
799    /// **Frame bytes staged in memory** (perf twin-apply-fsync, 2026-10-06): a header, its meta,
800    /// small pieces and the trailer, of one frame or of a whole group, go to the kernel as ONE
801    /// write — the twin used to spend four writes on every frame. Flushed before every fsync,
802    /// roll, release and cut, and whenever it passes [`STAGE_BYTES`].
803    buf: Vec<u8>,
804    /// How many of `seg.offsets` are durable and published (in [`Log::segments`], indexed, at or
805    /// below the head). The rest are a group commit in progress.
806    published: usize,
807    /// Test-only: injected write and truncate failures.
808    #[cfg(test)]
809    faults: WriterFaults,
810}
811
812/// Test-only failures a [`Writer`] can be told to meet.
813#[cfg(test)]
814#[derive(Default)]
815struct WriterFaults {
816    /// Fail the put after this many more succeed.
817    fail_put_after: Option<usize>,
818    /// Fail the next [`Writer::cut_to_committed`] before it touches anything.
819    fail_cut: bool,
820}
821
822/// The most frame bytes [`Writer::buf`] stages before it writes them out.
823const STAGE_BYTES: usize = 1 << 20;
824/// A piece at least this long is written straight from the caller's buffer, never copied into
825/// the stage (a 1 MiB piece of a pack has nothing to gain from being coalesced).
826const DIRECT_BYTES: usize = 64 << 10;
827
828impl Writer {
829    /// Hand every staged byte to the kernel.
830    fn flush_buf(&mut self) -> io::Result<()> {
831        if !self.buf.is_empty() {
832            self.file.write_all(&self.buf)?;
833            self.disk_len += self.buf.len() as u64;
834            self.buf.clear();
835        }
836        Ok(())
837    }
838
839    /// Stage `bytes` at the end of the segment (written straight out when large).
840    fn put(&mut self, bytes: &[u8]) -> io::Result<()> {
841        #[cfg(test)]
842        if let Some(n) = self.faults.fail_put_after {
843            if n == 0 {
844                self.faults.fail_put_after = None;
845                return Err(io::Error::other("txlog test: injected write failure"));
846            }
847            self.faults.fail_put_after = Some(n - 1);
848        }
849        if bytes.len() >= DIRECT_BYTES {
850            self.flush_buf()?;
851            self.file.write_all(bytes)?;
852            self.disk_len += bytes.len() as u64;
853            return Ok(());
854        }
855        self.buf.extend_from_slice(bytes);
856        if self.buf.len() >= STAGE_BYTES {
857            self.flush_buf()?;
858        }
859        Ok(())
860    }
861
862    /// Frames committed into the open segment and not yet durable.
863    fn unpublished(&self) -> usize {
864        self.seg.offsets.len() - self.published
865    }
866
867    /// Drop a partial frame: everything after the last committed frame goes, staged or on disk.
868    fn cut_to_committed(&mut self) -> io::Result<()> {
869        #[cfg(test)]
870        if std::mem::take(&mut self.faults.fail_cut) {
871            return Err(io::Error::other("txlog test: injected truncate failure"));
872        }
873        let keep = self.seg.len;
874        if keep >= self.disk_len {
875            // Everything past `disk_len` is staged: keep the committed part of the stage.
876            self.buf.truncate((keep - self.disk_len) as usize);
877            return Ok(());
878        }
879        // The partial frame reached the file (a large piece goes straight out); nothing is
880        // staged after it, so the stage is all partial frame.
881        self.buf.clear();
882        self.file.set_len(keep)?;
883        self.file.seek(SeekFrom::Start(keep))?;
884        self.disk_len = keep;
885        self.file.sync_data()
886    }
887}
888
889/// **The test hold of a group commit, by environment** (round 5.2, 2026-10-07): set to `n`, a
890/// leader of [`Log::sync_through`] waits until `n` frames are staged before its fsync (at most
891/// [`TEST_GROUP_WAIT`]). A test that counts the fsyncs of `n` concurrent writers (gunnar-server
892/// `tests/audit_read_io.rs`, whose writers run in a child under `strace`) sets it, so the count is
893/// of what the group commit shares, not of scheduling luck. Unset on every box: no wait at all.
894pub const TEST_GROUP_ENV: &str = "GUNNAR_TXLOG_TEST_GROUP";
895
896/// **The test hold, read only by a build with `test-hooks`** (wiring tracer B 2026-10-08): a
897/// shipped box never reads [`TEST_GROUP_ENV`], so no stray variable holds its writes.
898#[cfg(feature = "test-hooks")]
899fn test_group_from_env() -> Option<usize> {
900    std::env::var(TEST_GROUP_ENV).ok().and_then(|v| v.trim().parse::<usize>().ok()).filter(|n| *n > 1)
901}
902
903#[cfg(not(feature = "test-hooks"))]
904fn test_group_from_env() -> Option<usize> {
905    None
906}
907
908/// The longest a [`TEST_GROUP_ENV`] hold waits for its group: the last writers of a test finish
909/// one by one, and a group that will never fill goes on without them.
910pub const TEST_GROUP_WAIT: std::time::Duration = std::time::Duration::from_millis(100);
911
912/// The transaction log: one writer, any number of readers, one directory.
913///
914/// `append` is O(1) in the size of the log: a positioned write at the known
915/// end of the open segment, one fsync, one 8-byte index append.
916pub struct Log {
917    dir: PathBuf,
918    /// Closed segments, oldest first, plus the open one at the end.
919    segments: Mutex<Vec<Segment>>,
920    writer: Mutex<Writer>,
921    head: AtomicU64,
922    first: AtomicU64,
923    segment_bytes: u64,
924    /// Only `append_at` / `begin(Some(_))` may write (a twin, a restore).
925    follower: AtomicBool,
926    /// The mutation gate ([`Log::mutation`] / [`Log::freeze`]).
927    gate: Arc<Gate>,
928    /// [`Log::release`] gave up the writer's files; every write refuses until [`Log::reacquire`].
929    released: AtomicBool,
930    /// [`Error::Poisoned`]: what failed. Set once, never cleared — only a reopen recovers.
931    poisoned: Mutex<Option<String>>,
932    /// **Is a group-commit leader in its fsync?** ([`Log::sync_through`]). A flag under a mutex
933    /// that is NEVER held across I/O, and a condvar the leader wakes everybody on when its fsync
934    /// is published (r5 stall review, 2026-10-07). It used to be a `Mutex<()>` HELD ACROSS the
935    /// `fdatasync`: a waiter whose frame the leader's fsync had already made durable still had to
936    /// ACQUIRE that mutex before it could see so — and an unfair mutex hands it to the next
937    /// leader first, so the waiter sat behind an unrelated fsync, then the next, for as long as
938    /// writes kept arriving, every request thread in `futex_wait`.
939    sync_state: Mutex<bool>,
940    /// Notified (all) whenever a leader ends.
941    synced: std::sync::Condvar,
942    /// Test-only: each leader takes the next gate, if any, and waits on it before its fsync.
943    #[cfg(test)]
944    fsync_gates: Mutex<std::collections::VecDeque<std::sync::mpsc::Receiver<()>>>,
945    /// Test-only: this many of the next frame fsyncs fail.
946    #[cfg(test)]
947    fail_fsyncs: std::sync::atomic::AtomicU32,
948    /// **The test hold of a group commit** ([`TEST_GROUP_ENV`]): a leader waits, at most
949    /// [`TEST_GROUP_WAIT`], until this many frames are staged before its fsync. `None` — every
950    /// box, every ordinary test — does not wait at all.
951    test_group: Option<usize>,
952    /// `fdatasync`s that made frames durable ([`Log::frame_syncs`]).
953    frame_syncs: AtomicU64,
954    /// fsyncs an erase made ([`Log::erase_syncs`]).
955    erase_syncs: AtomicU64,
956    /// One erase at a time ([`Log::erase`]); the writer lock is NOT held for it.
957    erase_lock: Mutex<()>,
958    /// Test-only: an erase takes the next gate, if any, and waits on it after its first header.
959    #[cfg(test)]
960    erase_gates: Mutex<std::collections::VecDeque<std::sync::mpsc::Receiver<()>>>,
961}
962
963/// **The mutation gate**: every framed mutation holds a [`MutationGuard`] from before its frame
964/// is appended until the store has taken it; [`Log::freeze`] stops new ones and waits for the
965/// ones in flight. While frozen, the log's head IS the stores' state — what a consistent
966/// snapshot needs (the log is written BEFORE the store, so without the wait a frame at the head
967/// could still be on its way into the store).
968#[derive(Default)]
969struct Gate {
970    state: Mutex<GateState>,
971    cv: std::sync::Condvar,
972}
973
974#[derive(Default)]
975struct GateState {
976    frozen_by: Option<std::thread::ThreadId>,
977    inflight: u64,
978}
979
980thread_local! {
981    /// Nested guards on one thread count once (a tee write inside another framed write).
982    static MUTATION_DEPTH: std::cell::Cell<u32> = const { std::cell::Cell::new(0) };
983}
984
985/// Held by a framed mutation, frame through store write. See [`Log::mutation`].
986#[must_use = "a mutation guard must be held until the store has taken the write"]
987pub struct MutationGuard {
988    gate: Option<Arc<Gate>>,
989}
990
991impl Drop for MutationGuard {
992    fn drop(&mut self) {
993        MUTATION_DEPTH.with(|d| d.set(d.get().saturating_sub(1)));
994        if let Some(gate) = self.gate.take() {
995            let mut st = gate.state.lock().unwrap_or_else(|p| p.into_inner());
996            st.inflight = st.inflight.saturating_sub(1);
997            gate.cv.notify_all();
998        }
999    }
1000}
1001
1002/// Held while the log is frozen. See [`Log::freeze`].
1003pub struct FreezeGuard {
1004    gate: Arc<Gate>,
1005}
1006
1007impl Drop for FreezeGuard {
1008    fn drop(&mut self) {
1009        let mut st = self.gate.state.lock().unwrap_or_else(|p| p.into_inner());
1010        st.frozen_by = None;
1011        self.gate.cv.notify_all();
1012    }
1013}
1014
1015impl std::fmt::Debug for Log {
1016    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1017        f.debug_struct("Log")
1018            .field("dir", &self.dir)
1019            .field("first", &self.first_seq())
1020            .field("head", &self.head_seq())
1021            .finish()
1022    }
1023}
1024
1025/// **The one entry in the log's directory that is not a segment.**
1026///
1027/// `gunnar-server`'s `txlog_lfs` spools an arriving LFS object beside the log
1028/// — `<txlog>/spool/<oid>.part` — because the object has to be on disk before
1029/// the frame that names it is appended. It removes the FILE when the object
1030/// lands; the DIRECTORY stays, because `create_dir_all` made it and nothing
1031/// unmakes it.
1032///
1033/// It is named HERE and not there for the reason the name is load-bearing:
1034/// this module describes what may live in that directory, and [`Log::open_with`]
1035/// refuses everything else. A writer choosing a name the reader has to guess is
1036/// how one LFS push plus one restart became
1037/// `Kernel panic - not syncing: Attempted to kill init!` on the live appliance,
1038/// MEASURED 2026-09-17.
1039pub const SPOOL_DIR: &str = "spool";
1040
1041impl Log {
1042    /// Open or create the log at `dir`, cutting a torn tail if there is one.
1043    pub fn open(dir: impl AsRef<Path>) -> Result<Self> {
1044        Self::open_with(dir, DEFAULT_SEGMENT_BYTES)
1045    }
1046
1047    /// [`Log::open`] with a segment size (tests roll segments small).
1048    pub fn open_with(dir: impl AsRef<Path>, segment_bytes: u64) -> Result<Self> {
1049        let dir = dir.as_ref().to_path_buf();
1050        fs::create_dir_all(&dir)?;
1051        let mut firsts: Vec<u64> = Vec::new();
1052        for entry in fs::read_dir(&dir)? {
1053            let entry = entry?;
1054            let name = entry.file_name();
1055            let name = name.to_string_lossy();
1056            match parse_seg_name(&name) {
1057                Some((first, true)) => firsts.push(first),
1058                Some((_, false)) => {}
1059                // The LFS spool is part of this directory's layout — see
1060                // [`SPOOL_DIR`]. Only that exact name, and only as a directory:
1061                // a stray FILE called `spool` is still something nobody wrote
1062                // on purpose, and a reader that shrugs at one shrugs at
1063                // corruption.
1064                None if name == SPOOL_DIR && entry.file_type().is_ok_and(|t| t.is_dir()) => {}
1065                None => return Err(Error::Unexpected(entry.path())),
1066            }
1067        }
1068        firsts.sort_unstable();
1069        let mut segments = Vec::with_capacity(firsts.len().max(1));
1070        let n = firsts.len();
1071        for (i, first) in firsts.iter().enumerate() {
1072            let seg = open_segment(&dir, *first, true)?;
1073            // A closed segment must be intact end to end; only the last may
1074            // have been torn.
1075            if i + 1 < n && seg.offsets.is_empty() {
1076                return Err(Error::Corrupt {
1077                    path: seg_path(&dir, *first),
1078                    offset: 0,
1079                    what: "closed segment holds no frame",
1080                });
1081            }
1082            // Adjacent segments must be contiguous.
1083            if let Some(next) = firsts.get(i + 1) {
1084                match seg.last_seq() {
1085                    Some(last) if last + 1 == *next => {}
1086                    _ => {
1087                        return Err(Error::Corrupt {
1088                            path: seg_path(&dir, *first),
1089                            offset: seg.len,
1090                            what: "segment does not run up to the next one",
1091                        })
1092                    }
1093                }
1094            }
1095            segments.push(seg);
1096        }
1097        if segments.is_empty() {
1098            let first = 1;
1099            File::create(seg_path(&dir, first))?;
1100            File::create(idx_path(&dir, first))?;
1101            sync_dir(&dir)?;
1102            segments.push(Segment {
1103                first,
1104                offsets: Vec::new(),
1105                len: 0,
1106            });
1107        }
1108        let open = segments.last().expect("one segment");
1109        let head = open.last_seq().unwrap_or(open.first - 1);
1110        let first = segments[0].first;
1111        let mut file = OpenOptions::new().append(true).open(seg_path(&dir, open.first))?;
1112        file.seek(SeekFrom::Start(open.len))?;
1113        let idx = OpenOptions::new().append(true).open(idx_path(&dir, open.first))?;
1114        let seg = Segment {
1115            first: open.first,
1116            offsets: open.offsets.clone(),
1117            len: open.len,
1118        };
1119        let published = seg.offsets.len();
1120        Ok(Log {
1121            dir,
1122            segments: Mutex::new(segments),
1123            writer: Mutex::new(Writer {
1124                file,
1125                idx,
1126                disk_len: seg.len,
1127                seg,
1128                buf: Vec::new(),
1129                published,
1130                #[cfg(test)]
1131                faults: WriterFaults::default(),
1132            }),
1133            head: AtomicU64::new(head),
1134            first: AtomicU64::new(first),
1135            segment_bytes,
1136            follower: AtomicBool::new(false),
1137            gate: Arc::new(Gate::default()),
1138            released: AtomicBool::new(false),
1139            poisoned: Mutex::new(None),
1140            sync_state: Mutex::new(false),
1141            synced: std::sync::Condvar::new(),
1142            #[cfg(test)]
1143            fsync_gates: Mutex::new(Default::default()),
1144            #[cfg(test)]
1145            fail_fsyncs: std::sync::atomic::AtomicU32::new(0),
1146            test_group: test_group_from_env(),
1147            frame_syncs: AtomicU64::new(0),
1148            erase_syncs: AtomicU64::new(0),
1149            erase_lock: Mutex::new(()),
1150            #[cfg(test)]
1151            erase_gates: Mutex::new(Default::default()),
1152        })
1153    }
1154
1155    /// **Enter a framed mutation.** Hold the guard from before the frame is appended until the
1156    /// store has the write. Waits while another thread holds [`Log::freeze`]; the freezing
1157    /// thread itself passes (a mutation it causes lands after the snapshot's sequence and is
1158    /// replayed, which every apply tolerates). Nested guards on one thread count once.
1159    pub fn mutation(&self) -> MutationGuard {
1160        let depth = MUTATION_DEPTH.with(|d| {
1161            let v = d.get();
1162            d.set(v + 1);
1163            v
1164        });
1165        if depth > 0 {
1166            return MutationGuard { gate: None };
1167        }
1168        let me = std::thread::current().id();
1169        let mut st = self.gate.state.lock().unwrap_or_else(|p| p.into_inner());
1170        while matches!(st.frozen_by, Some(t) if t != me) {
1171            st = self.gate.cv.wait(st).unwrap_or_else(|p| p.into_inner());
1172        }
1173        st.inflight += 1;
1174        MutationGuard { gate: Some(Arc::clone(&self.gate)) }
1175    }
1176
1177    /// **Freeze**: no new framed mutation starts on another thread, and this returns once every
1178    /// one in flight has reached its store. Reads are untouched. One freezer at a time. Must not
1179    /// be called from inside a mutation (it would wait for itself).
1180    pub fn freeze(&self) -> FreezeGuard {
1181        debug_assert_eq!(MUTATION_DEPTH.with(|d| d.get()), 0, "freeze from inside a mutation waits for itself");
1182        let me = std::thread::current().id();
1183        let mut st = self.gate.state.lock().unwrap_or_else(|p| p.into_inner());
1184        while st.frozen_by.is_some_and(|t| t != me) {
1185            st = self.gate.cv.wait(st).unwrap_or_else(|p| p.into_inner());
1186        }
1187        st.frozen_by = Some(me);
1188        while st.inflight > 0 {
1189            st = self.gate.cv.wait(st).unwrap_or_else(|p| p.into_inner());
1190        }
1191        FreezeGuard { gate: Arc::clone(&self.gate) }
1192    }
1193
1194    /// The directory.
1195    pub fn dir(&self) -> &Path {
1196        &self.dir
1197    }
1198
1199    /// Make this log follower-only (a twin; a box being restored): `append`
1200    /// and `begin(None)` refuse with [`Error::Follower`], `append_at` works.
1201    pub fn set_follower(&self, on: bool) {
1202        self.follower.store(on, Ordering::Release);
1203    }
1204
1205    /// See [`Log::set_follower`].
1206    pub fn is_follower(&self) -> bool {
1207        self.follower.load(Ordering::Acquire)
1208    }
1209
1210    /// The test hold of a group commit this log honours ([`TEST_GROUP_ENV`]); `None` on every
1211    /// box.
1212    pub fn group_hold(&self) -> Option<usize> {
1213        self.test_group
1214    }
1215
1216    /// The newest committed sequence; 0 when nothing has been committed.
1217    pub fn head_seq(&self) -> u64 {
1218        self.head.load(Ordering::Acquire)
1219    }
1220
1221    /// The oldest sequence still retained. `head + 1` when the log is empty.
1222    pub fn first_seq(&self) -> u64 {
1223        self.first.load(Ordering::Acquire)
1224    }
1225
1226    /// Append one transaction with the next sequence number. Durable when
1227    /// this returns.
1228    pub fn append(&self, kind: Kind, meta: &Meta, blob: &[u8]) -> Result<u64> {
1229        if self.is_follower() {
1230            return Err(Error::Follower);
1231        }
1232        let meta = meta.to_bytes();
1233        self.append_raw(kind as u16, &meta, blob)
1234    }
1235
1236    /// [`Log::append`], but the caller says which sequence number the entry
1237    /// must get — the twin's path: it holds the primary's numbers, not its
1238    /// own. Refuses anything but `head + 1`.
1239    pub fn append_at(&self, seq: u64, kind: u16, meta: &[u8], blob: &[u8]) -> Result<u64> {
1240        {
1241            let mut w = self.lock_writer()?;
1242            let expected = w.seg.first + w.seg.offsets.len() as u64;
1243            if seq != expected {
1244                return Err(Error::OutOfOrder { got: seq, expected });
1245            }
1246            self.stage_locked(&mut w, kind, meta, blob)?;
1247        }
1248        self.sync_through(seq)?;
1249        Ok(seq)
1250    }
1251
1252    /// Append with raw kind and meta bytes. Durable when this returns.
1253    ///
1254    /// **Group commit for concurrent appenders** (perf twin-apply-fsync, 2026-10-06): the frame
1255    /// is staged under the writer lock, and its fsync is [`Log::sync_through`]'s — taken OUTSIDE
1256    /// the writer lock by one leader for every frame staged so far. Eight request threads that
1257    /// append together pay about one `fdatasync` between them instead of eight; one appender alone
1258    /// pays exactly what it paid before (one write, one fdatasync, one index write).
1259    pub fn append_raw(&self, kind: u16, meta: &[u8], blob: &[u8]) -> Result<u64> {
1260        let seq = {
1261            let mut w = self.lock_writer()?;
1262            self.stage_locked(&mut w, kind, meta, blob)?
1263        };
1264        self.sync_through(seq)?;
1265        Ok(seq)
1266    }
1267
1268    /// **[`Log::append`] without waiting for durability** — for a frame whose mutation is
1269    /// invisible until a LATER frame publishes it (a control document's shard blob, published by
1270    /// the `ControlDoc` frame that moves the pointer): that frame's `fdatasync` covers this one,
1271    /// which is earlier in the same file. Refused on a follower, as `append` is.
1272    pub fn stage(&self, kind: Kind, meta: &Meta, blob: &[u8]) -> Result<u64> {
1273        if self.is_follower() {
1274            return Err(Error::Follower);
1275        }
1276        self.stage_raw(kind as u16, &meta.to_bytes(), blob)
1277    }
1278
1279    /// [`Log::append_raw`] without waiting for durability: the frame is whole in the segment and
1280    /// holds its number; nobody reads it and it is not acknowledged until [`Log::sync_through`]
1281    /// (or any later sync) makes it durable. The caller MUST NOT change any state the frame
1282    /// describes before then — the log is written before the state, always.
1283    pub fn stage_raw(&self, kind: u16, meta: &[u8], blob: &[u8]) -> Result<u64> {
1284        let mut w = self.lock_writer()?;
1285        self.stage_locked(&mut w, kind, meta, blob)
1286    }
1287
1288    fn stage_locked(&self, w: &mut Writer, kind: u16, meta: &[u8], blob: &[u8]) -> Result<u64> {
1289        self.roll_if_full(w)?;
1290        let seq = w.seg.first + w.seg.offsets.len() as u64;
1291        let header = Header {
1292            seq,
1293            kind,
1294            meta_len: meta.len() as u32,
1295            blob_len: blob.len() as u64,
1296        };
1297        let h = header.encode();
1298        let mut crc = Crc32c::new();
1299        crc.update(meta);
1300        crc.update(blob);
1301        let tr = crc.finish().to_le_bytes();
1302        let put = (|| {
1303            w.put(&h)?;
1304            w.put(meta)?;
1305            w.put(blob)?;
1306            w.put(&tr)
1307        })();
1308        if let Err(e) = put {
1309            // Half a frame is never left behind for the next one to land after — and when it
1310            // cannot be cut, nothing more is written behind it.
1311            if let Err(cut) = w.cut_to_committed() {
1312                self.poison(format!("a half-written frame could not be cut off the segment ({cut}) after its write failed ({e})"));
1313            }
1314            return Err(e.into());
1315        }
1316        let off = w.seg.len;
1317        w.seg.offsets.push(off);
1318        w.seg.len += header.frame_len();
1319        Ok(seq)
1320    }
1321
1322    /// **Return once frame `seq` is durable** — making it so if nobody is: a waiter becomes the
1323    /// leader when no leader is in its fsync, writes out the stage, `fdatasync`s a duplicate of
1324    /// the segment's descriptor WITHOUT holding the writer lock (appenders keep staging behind it),
1325    /// then indexes and publishes everything that fsync covered and wakes every waiter. A waiter
1326    /// re-checks the head on EVERY wake-up, so the moment a leader's fsync covers its frame it
1327    /// returns — it never waits for a later leader's fsync, and no lock is held across I/O.
1328    pub fn sync_through(&self, seq: u64) -> Result<()> {
1329        {
1330            let mut leading = self.sync_state.lock().unwrap_or_else(|p| p.into_inner());
1331            loop {
1332                if self.head_seq() >= seq {
1333                    return Ok(());
1334                }
1335                if !*leading {
1336                    *leading = true;
1337                    break;
1338                }
1339                leading = self.synced.wait(leading).unwrap_or_else(|p| p.into_inner());
1340            }
1341        }
1342        // This thread leads until `_lead` drops — on every path out, an error and a panic
1343        // included — and then every waiter is woken to look at the head again.
1344        struct Lead<'a>(&'a Log);
1345        impl Drop for Lead<'_> {
1346            fn drop(&mut self) {
1347                *self.0.sync_state.lock().unwrap_or_else(|p| p.into_inner()) = false;
1348                self.0.synced.notify_all();
1349            }
1350        }
1351        let _lead = Lead(self);
1352        if let Some(n) = self.test_group {
1353            self.hold_for_the_group(n);
1354        }
1355        let snap = (|| -> Result<(File, u64, usize)> {
1356            let mut w = self.lock_writer()?;
1357            w.flush_buf()?;
1358            Ok((w.file.try_clone()?, w.seg.first, w.seg.offsets.len()))
1359        })();
1360        let (file, first, upto) = match snap {
1361            Ok(s) => s,
1362            // A release (the data set being grown) publishes everything first.
1363            Err(_) if self.head_seq() >= seq => return Ok(()),
1364            Err(e) => return Err(e),
1365        };
1366        #[cfg(test)]
1367        {
1368            let gate = self.fsync_gates.lock().unwrap_or_else(|p| p.into_inner()).pop_front();
1369            if let Some(gate) = gate {
1370                let _ = gate.recv();
1371            }
1372        }
1373        if let Err(e) = self.sync_frames(&file) {
1374            let mut w = self.writer.lock().expect("txlog writer");
1375            if w.seg.first == first {
1376                return Err(self.poison_and_cut(&mut w, e));
1377            }
1378            // A roll since then published (and synced) that segment whole — the frames this
1379            // fsync was for are durable.
1380            drop(w);
1381            if self.head_seq() < seq {
1382                return Err(self.poison_and_cut(&mut self.writer.lock().expect("txlog writer"), e));
1383            }
1384            return Ok(());
1385        }
1386        self.frame_syncs.fetch_add(1, Ordering::Relaxed);
1387        let mut w = match self.lock_writer() {
1388            Ok(w) => w,
1389            Err(_) if self.head_seq() >= seq => return Ok(()),
1390            Err(e) => return Err(e),
1391        };
1392        // A roll since then published (and synced) that segment whole.
1393        if w.seg.first == first && upto > w.published {
1394            self.publish_upto(&mut w, upto)?;
1395        }
1396        Ok(())
1397    }
1398
1399    /// **The test hold** ([`TEST_GROUP_ENV`]): the leader waits until `n` frames are staged and
1400    /// not yet durable — or [`TEST_GROUP_WAIT`] passes — so a count of fsyncs measures what the
1401    /// group commit shares when the writers ARE there, not how the scheduler happened to
1402    /// interleave them. The writer lock is taken only to read the count.
1403    fn hold_for_the_group(&self, n: usize) {
1404        let until = std::time::Instant::now() + TEST_GROUP_WAIT;
1405        loop {
1406            let staged = self.writer.lock().map(|w| w.unpublished()).unwrap_or(n);
1407            if staged >= n || std::time::Instant::now() >= until {
1408                return;
1409            }
1410            std::thread::sleep(std::time::Duration::from_micros(200));
1411        }
1412    }
1413
1414    /// The `fdatasync` that makes frames durable (and, in a test, the one an injected failure
1415    /// replaces).
1416    fn sync_frames(&self, file: &File) -> io::Result<()> {
1417        #[cfg(test)]
1418        loop {
1419            let n = self.fail_fsyncs.load(Ordering::SeqCst);
1420            if n == 0 {
1421                break;
1422            }
1423            if self.fail_fsyncs.compare_exchange(n, n - 1, Ordering::SeqCst, Ordering::SeqCst).is_ok() {
1424                return Err(io::Error::other("txlog test: injected fsync failure (EIO)"));
1425            }
1426        }
1427        file.sync_data()
1428    }
1429
1430    /// Test-only: the next `n` frame fsyncs fail.
1431    #[cfg(test)]
1432    fn fail_next_fsyncs(&self, n: u32) {
1433        self.fail_fsyncs.store(n, Ordering::SeqCst);
1434    }
1435
1436    /// Test-only: the next leader waits on `gate` before its fsync.
1437    #[cfg(test)]
1438    fn hold_next_fsync(&self, gate: std::sync::mpsc::Receiver<()>) {
1439        self.fsync_gates.lock().unwrap().push_back(gate);
1440    }
1441
1442    /// **Make a group commit durable and visible** (perf twin-apply-fsync, 2026-10-06): every
1443    /// frame committed with [`Pending::commit_unsynced`] since the last call is written out,
1444    /// `fdatasync`ed ONCE, indexed with ONE write, and published — readers see it and the head
1445    /// moves to the last of them only now. A twin acknowledges its head to the primary, so what it
1446    /// acknowledges is what this made durable. Answers the head. Nothing pending = no I/O.
1447    pub fn sync(&self) -> Result<u64> {
1448        let mut w = self.lock_writer()?;
1449        self.publish(&mut w)?;
1450        Ok(self.head_seq())
1451    }
1452
1453    /// Frames committed and not yet durable ([`Log::sync`] makes them so).
1454    pub fn unsynced(&self) -> u64 {
1455        self.writer.lock().expect("txlog writer").unpublished() as u64
1456    }
1457
1458    /// The body of [`Log::sync`], under the writer lock.
1459    fn publish(&self, w: &mut Writer) -> Result<()> {
1460        w.flush_buf()?;
1461        let n = w.unpublished();
1462        if n == 0 {
1463            return Ok(());
1464        }
1465        if let Some(cause) = self.poisoned_by() {
1466            return Err(Error::Poisoned { dir: self.dir.clone(), cause });
1467        }
1468        if let Err(e) = self.sync_frames(&w.file) {
1469            return Err(self.poison_and_cut(w, e));
1470        }
1471        self.frame_syncs.fetch_add(1, Ordering::Relaxed);
1472        self.publish_upto(w, w.seg.offsets.len())
1473    }
1474
1475    /// What poisoned the log, if anything did ([`Error::Poisoned`]).
1476    pub fn poisoned_by(&self) -> Option<String> {
1477        self.poisoned.lock().unwrap_or_else(|p| p.into_inner()).clone()
1478    }
1479
1480    /// **Poison the log** ([`Error::Poisoned`]): the first cause is kept, said once at ERROR.
1481    fn poison(&self, cause: String) {
1482        let mut p = self.poisoned.lock().unwrap_or_else(|p| p.into_inner());
1483        if p.is_none() {
1484            tracing::error!(
1485                target: crate::observe::TARGET_STORE,
1486                event = "txlog.poisoned",
1487                dir = %self.dir.display(),
1488                cause = %cause,
1489                "the transaction log is poisoned: every write refuses until it is reopened"
1490            );
1491            *p = Some(cause);
1492        }
1493    }
1494
1495    /// **A frame fsync failed**: poison the log, then cut every frame it did not make durable off
1496    /// the segment — staged, written, or in the page cache — so neither a later fsync nor a
1497    /// reopen can publish what its writer was told had failed. Answers the error to return.
1498    fn poison_and_cut(&self, w: &mut Writer, e: io::Error) -> Error {
1499        let mut cause = format!("a frame fsync failed ({e})");
1500        let keep = w.seg.offsets.get(w.published).copied().unwrap_or(w.seg.len);
1501        w.seg.offsets.truncate(w.published);
1502        w.seg.len = keep;
1503        if keep >= w.disk_len {
1504            w.buf.truncate((keep - w.disk_len) as usize);
1505        } else {
1506            w.buf.clear();
1507            let cut = (|| {
1508                w.file.set_len(keep)?;
1509                w.file.seek(SeekFrom::Start(keep))?;
1510                w.disk_len = keep;
1511                w.file.sync_data()
1512            })();
1513            if let Err(c) = cut {
1514                cause.push_str(&format!(", and the frames it did not make durable could not be cut ({c})"));
1515            }
1516        }
1517        self.poison(cause.clone());
1518        Error::Poisoned { dir: self.dir.clone(), cause }
1519    }
1520
1521    /// **How many `fdatasync`s made frames durable** since this log was opened — the leader's in
1522    /// [`Log::sync_through`] and a group's in [`Log::sync`] (a roll, a release and a cut sync too,
1523    /// rarely, and are not counted). What the I/O tests hold to a bound per entry.
1524    pub fn frame_syncs(&self) -> u64 {
1525        self.frame_syncs.load(Ordering::Relaxed)
1526    }
1527
1528    /// Index and publish the open segment's frames up to `upto` — already durable.
1529    fn publish_upto(&self, w: &mut Writer, upto: usize) -> Result<()> {
1530        if upto <= w.published {
1531            return Ok(());
1532        }
1533        let fresh: Vec<u64> = w.seg.offsets[w.published..upto].to_vec();
1534        let mut bytes = Vec::with_capacity(fresh.len() * 8);
1535        for off in &fresh {
1536            bytes.extend_from_slice(&off.to_le_bytes());
1537        }
1538        // The index is a cache appended AFTER the data's fsync, never fsynced itself (the
1539        // module's rule): one write for the whole group.
1540        w.idx.write_all(&bytes)?;
1541        w.published = upto;
1542        let end = match w.seg.offsets.get(upto) {
1543            Some(&next) => next,
1544            None => w.seg.len,
1545        };
1546        {
1547            let mut segs = self.segments.lock().expect("txlog segments");
1548            let open = segs.last_mut().expect("open segment");
1549            debug_assert_eq!(open.first, w.seg.first, "the published open segment is the writer's");
1550            open.offsets.extend_from_slice(&fresh);
1551            open.len = end;
1552        }
1553        let last = w.seg.first + upto as u64 - 1;
1554        self.head.store(last, Ordering::Release);
1555        Ok(())
1556    }
1557
1558    /// Begin a streaming append for a blob whose bytes arrive in chunks —
1559    /// the twin receiving a large pack over the wire without holding it in
1560    /// memory. The writer lock is held until [`Pending::commit`] or
1561    /// [`Pending::abort`]; the frame does not exist until `commit`.
1562    pub fn begin(&self, seq: Option<u64>, kind: u16, meta: &[u8], blob_len: u64) -> Result<Pending<'_>> {
1563        if seq.is_none() && self.is_follower() {
1564            return Err(Error::Follower);
1565        }
1566        let mut w = self.lock_writer()?;
1567        self.roll_if_full(&mut w)?;
1568        let next = w.seg.first + w.seg.offsets.len() as u64;
1569        if let Some(seq) = seq {
1570            if seq != next {
1571                return Err(Error::OutOfOrder {
1572                    got: seq,
1573                    expected: next,
1574                });
1575            }
1576        }
1577        let header = Header {
1578            seq: next,
1579            kind,
1580            meta_len: meta.len() as u32,
1581            blob_len,
1582        };
1583        let h = header.encode();
1584        w.put(&h)?;
1585        w.put(meta)?;
1586        let mut crc = Crc32c::new();
1587        crc.update(meta);
1588        Ok(Pending {
1589            log: self,
1590            w,
1591            header,
1592            crc,
1593            written: 0,
1594            finished: false,
1595        })
1596    }
1597
1598    /// The writer, or [`Error::Released`] while the files are given up.
1599    fn lock_writer(&self) -> Result<std::sync::MutexGuard<'_, Writer>> {
1600        let w = self.writer.lock().expect("txlog writer");
1601        if let Some(cause) = self.poisoned_by() {
1602            return Err(Error::Poisoned { dir: self.dir.clone(), cause });
1603        }
1604        if self.released.load(Ordering::Acquire) {
1605            return Err(Error::Released(self.dir.clone()));
1606        }
1607        Ok(w)
1608    }
1609
1610    /// **Give up the writer's files** so the filesystem under the log can be unmounted — the
1611    /// runtime data-set growth (gunnar-server `grow`), where PID 1 unmounts `/data` itself.
1612    ///
1613    /// MEASURED 2026-09-15 under KVM (growbench R5): the open segment's `.log` and `.idx` were
1614    /// the only descriptors still under `/data` after the stores closed, and `umount` answered
1615    /// EBUSY — so every runtime growth on a box carrying the log aborted.
1616    ///
1617    /// Waits for an in-flight [`Pending`] (it holds the writer lock), syncs both files, and
1618    /// parks the handles on `/dev/null`; readers open per call and are unaffected until the
1619    /// unmount itself. Every write refuses with [`Error::Released`] until [`Log::reacquire`].
1620    /// Idempotent.
1621    pub fn release(&self) -> Result<()> {
1622        let mut w = self.writer.lock().expect("txlog writer");
1623        if self.released.load(Ordering::Acquire) {
1624            return Ok(());
1625        }
1626        self.publish(&mut w)?;
1627        w.file.sync_all()?;
1628        w.idx.sync_all()?;
1629        w.file = OpenOptions::new().append(true).open("/dev/null")?;
1630        w.idx = OpenOptions::new().append(true).open("/dev/null")?;
1631        self.released.store(true, Ordering::Release);
1632        Ok(())
1633    }
1634
1635    /// **Take the files back** after the remount. The open segment must be exactly as long as
1636    /// the writer left it — anything else is a different disk, and is refused by name rather
1637    /// than appended to. Idempotent.
1638    pub fn reacquire(&self) -> Result<()> {
1639        let mut w = self.writer.lock().expect("txlog writer");
1640        if !self.released.load(Ordering::Acquire) {
1641            return Ok(());
1642        }
1643        let path = seg_path(&self.dir, w.seg.first);
1644        let mut file = OpenOptions::new().append(true).open(&path)?;
1645        let on_disk = file.metadata()?.len();
1646        if on_disk != w.seg.len {
1647            return Err(Error::Corrupt {
1648                path,
1649                offset: on_disk,
1650                what: "the open segment changed length while the log was released",
1651            });
1652        }
1653        file.seek(SeekFrom::Start(w.seg.len))?;
1654        let idx = OpenOptions::new().append(true).open(idx_path(&self.dir, w.seg.first))?;
1655        w.file = file;
1656        w.idx = idx;
1657        w.disk_len = w.seg.len;
1658        self.released.store(false, Ordering::Release);
1659        Ok(())
1660    }
1661
1662    /// The files are given up ([`Log::release`]).
1663    pub fn is_released(&self) -> bool {
1664        self.released.load(Ordering::Acquire)
1665    }
1666
1667    fn roll_if_full(&self, w: &mut Writer) -> Result<()> {
1668        if w.seg.len < self.segment_bytes || w.seg.offsets.is_empty() {
1669            return Ok(());
1670        }
1671        let next_first = w.seg.first + w.seg.offsets.len() as u64;
1672        // A group in progress ends with the segment it was written into.
1673        self.publish(w)?;
1674        w.file.sync_all()?;
1675        w.idx.sync_all()?;
1676        let file = File::create(seg_path(&self.dir, next_first))?;
1677        let idx = File::create(idx_path(&self.dir, next_first))?;
1678        sync_dir(&self.dir)?;
1679        w.file = file;
1680        w.idx = idx;
1681        w.disk_len = 0;
1682        w.published = 0;
1683        w.seg = Segment {
1684            first: next_first,
1685            offsets: Vec::new(),
1686            len: 0,
1687        };
1688        self.segments.lock().expect("txlog segments").push(Segment {
1689            first: next_first,
1690            offsets: Vec::new(),
1691            len: 0,
1692        });
1693        Ok(())
1694    }
1695
1696    /// Locate a frame: which segment, which offset, how long.
1697    fn locate(&self, seq: u64) -> Result<(PathBuf, u64)> {
1698        let head = self.head_seq();
1699        if seq > head {
1700            return Err(Error::BeyondHead { seq, head });
1701        }
1702        let first = self.first_seq();
1703        if seq < first {
1704            return Err(Error::NotRetained { seq, first });
1705        }
1706        let segs = self.segments.lock().expect("txlog segments");
1707        let i = match segs.binary_search_by(|s| s.first.cmp(&seq)) {
1708            Ok(i) => i,
1709            Err(0) => return Err(Error::NotRetained { seq, first }),
1710            Err(i) => i - 1,
1711        };
1712        let seg = &segs[i];
1713        let k = (seq - seg.first) as usize;
1714        let off = *seg.offsets.get(k).ok_or(Error::BeyondHead { seq, head })?;
1715        Ok((seg_path(&self.dir, seg.first), off))
1716    }
1717
1718    /// Read one frame whole.
1719    pub fn read(&self, seq: u64) -> Result<Entry> {
1720        let (path, off) = self.locate(seq)?;
1721        let mut file = File::open(&path)?;
1722        file.seek(SeekFrom::Start(off))?;
1723        let mut hdr = [0u8; HEADER_LEN];
1724        file.read_exact(&mut hdr)?;
1725        let h = Header::decode(&hdr).ok_or(Error::Corrupt {
1726            path: path.clone(),
1727            offset: off,
1728            what: "header",
1729        })?;
1730        let mut meta = vec![0u8; h.meta_len as usize];
1731        file.read_exact(&mut meta)?;
1732        if h.kind == Kind::Redacted as u16 {
1733            // Erased: the meta is whole, the blob is gone. Nothing to check it against.
1734            return Ok(Entry { seq: h.seq, kind: h.kind, meta, blob: Vec::new() });
1735        }
1736        let mut blob = vec![0u8; h.blob_len as usize];
1737        file.read_exact(&mut blob)?;
1738        let mut tr = [0u8; TRAILER_LEN];
1739        file.read_exact(&mut tr)?;
1740        let mut crc = Crc32c::new();
1741        crc.update(&meta);
1742        crc.update(&blob);
1743        if crc.finish() != u32::from_le_bytes(tr) {
1744            return Err(Error::Corrupt {
1745                path,
1746                offset: off,
1747                what: "payload crc",
1748            });
1749        }
1750        Ok(Entry {
1751            seq: h.seq,
1752            kind: h.kind,
1753            meta,
1754            blob,
1755        })
1756    }
1757
1758    /// Read a frame's header and meta, and say where its blob is, so a
1759    /// caller can stream the blob in chunks.
1760    pub fn locate_blob(&self, seq: u64) -> Result<Located> {
1761        let (path, off) = self.locate(seq)?;
1762        let mut file = File::open(&path)?;
1763        file.seek(SeekFrom::Start(off))?;
1764        let mut hdr = [0u8; HEADER_LEN];
1765        file.read_exact(&mut hdr)?;
1766        let h = Header::decode(&hdr).ok_or(Error::Corrupt {
1767            path: path.clone(),
1768            offset: off,
1769            what: "header",
1770        })?;
1771        let mut meta = vec![0u8; h.meta_len as usize];
1772        file.read_exact(&mut meta)?;
1773        Ok(Located {
1774            seq: h.seq,
1775            kind: h.kind,
1776            meta,
1777            path,
1778            blob_offset: off + HEADER_LEN as u64 + h.meta_len as u64,
1779            // An erased frame streams nothing: its bytes are gone.
1780            blob_len: if h.kind == Kind::Redacted as u16 { 0 } else { h.blob_len },
1781            payload_crc: None,
1782        })
1783    }
1784
1785    /// **One frame's blob in pieces of at most `piece` bytes, never whole** — the primary's half
1786    /// of a twin's fetch (twin-streaming, 2026-10-06). `each` gets the frame's header and meta
1787    /// ([`Located`]), one piece, and whether it is the LAST.
1788    ///
1789    /// **The last piece is handed over only after the payload crc checked out**, the same check
1790    /// [`Log::read`] makes before it answers. A reader that makes the entry visible on `last` (a
1791    /// twin's commit) therefore never makes a corrupt one visible: a frame that fails its crc
1792    /// ends in [`Error::Corrupt`] with every piece but the last delivered, and the reader drops
1793    /// what it was given. Memory is one piece, whatever the blob's size.
1794    ///
1795    /// An erased frame ([`Kind::Redacted`]) is one empty last piece and is not checked: its bytes
1796    /// are gone and its crc was over them. A frame with an empty blob checks the crc over its
1797    /// meta and is one empty last piece.
1798    pub fn read_pieces<E: From<Error>>(
1799        &self,
1800        seq: u64,
1801        piece: usize,
1802        mut each: impl FnMut(&Located, &[u8], bool) -> std::result::Result<(), E>,
1803    ) -> std::result::Result<Located, E> {
1804        let mut at = self.locate_blob(seq)?;
1805        let frame_at = at.blob_offset - HEADER_LEN as u64 - at.meta.len() as u64;
1806        let corrupt = |path: &Path| Error::Corrupt { path: path.to_path_buf(), offset: frame_at, what: "payload crc" };
1807        if at.kind == Kind::Redacted as u16 {
1808            each(&at, &[], true)?;
1809            return Ok(at);
1810        }
1811        let mut file = File::open(&at.path).map_err(Error::from)?;
1812        file.seek(SeekFrom::Start(at.blob_offset)).map_err(Error::from)?;
1813        let mut crc = Crc32c::new();
1814        crc.update(&at.meta);
1815        let mut buf = vec![0u8; (piece.max(1) as u64).min(at.blob_len) as usize];
1816        let mut left = at.blob_len;
1817        loop {
1818            let n = (buf.len() as u64).min(left) as usize;
1819            file.read_exact(&mut buf[..n]).map_err(Error::from)?;
1820            crc.update(&buf[..n]);
1821            left -= n as u64;
1822            if left > 0 {
1823                each(&at, &buf[..n], false)?;
1824                continue;
1825            }
1826            let mut tr = [0u8; TRAILER_LEN];
1827            file.read_exact(&mut tr).map_err(Error::from)?;
1828            if crc.finish() != u32::from_le_bytes(tr) {
1829                return Err(corrupt(&at.path).into());
1830            }
1831            at.payload_crc = Some(u32::from_le_bytes(tr));
1832            each(&at, &buf[..n], true)?;
1833            return Ok(at);
1834        }
1835    }
1836
1837    /// **[`Log::read_pieces`] from byte `from` of the blob on** (restore-large-entry, 2026-10-07):
1838    /// the twin's half of a restore resuming an entry the box being filled holds part of. 0 is
1839    /// [`Log::read_pieces`] exactly. Otherwise only the blob from `from` is read — a resumed entry
1840    /// of gigabytes costs the twin its REST on every call, never a re-read of what was sent, which
1841    /// on a slow volume would eat the call's budget and stall the restore again near the end —
1842    /// and the crc cannot be checked here (it is over all of it): the last piece carries the
1843    /// frame's stored crc as [`Located::payload_crc`], unchecked, and the receiver checks it over
1844    /// the entry it put together. `from` at the blob's end is one empty last piece; `from` past
1845    /// it, or on an erased frame, is refused: an `InvalidInput` I/O error naming both numbers.
1846    pub fn read_pieces_from<E: From<Error>>(
1847        &self,
1848        seq: u64,
1849        from: u64,
1850        piece: usize,
1851        mut each: impl FnMut(&Located, &[u8], bool) -> std::result::Result<(), E>,
1852    ) -> std::result::Result<Located, E> {
1853        if from == 0 {
1854            return self.read_pieces(seq, piece, each);
1855        }
1856        let mut at = self.locate_blob(seq)?;
1857        if at.kind == Kind::Redacted as u16 || from > at.blob_len {
1858            return Err(Error::Io(io::Error::new(
1859                io::ErrorKind::InvalidInput,
1860                format!("txlog: seq {seq} read from byte {from} of a {}-byte blob", at.blob_len),
1861            ))
1862            .into());
1863        }
1864        let mut file = File::open(&at.path).map_err(Error::from)?;
1865        file.seek(SeekFrom::Start(at.blob_offset + from)).map_err(Error::from)?;
1866        let mut left = at.blob_len - from;
1867        let mut buf = vec![0u8; (piece.max(1) as u64).min(left.max(1)) as usize];
1868        loop {
1869            let n = (buf.len() as u64).min(left) as usize;
1870            file.read_exact(&mut buf[..n]).map_err(Error::from)?;
1871            left -= n as u64;
1872            if left > 0 {
1873                each(&at, &buf[..n], false)?;
1874                continue;
1875            }
1876            let mut tr = [0u8; TRAILER_LEN];
1877            file.read_exact(&mut tr).map_err(Error::from)?;
1878            at.payload_crc = Some(u32::from_le_bytes(tr));
1879            each(&at, &buf[..n], true)?;
1880            return Ok(at);
1881        }
1882    }
1883
1884    /// **Erase every frame before `before` that carries data of one of `repos`** — the
1885    /// `Pack`, `Refs`, `LfsPut` and `LfsDelete` frames whose meta names one of them
1886    /// exactly — in place (owner ruling g-nonpay: an account deleted for non-payment
1887    /// is deleted from the log too, on main and on the twin).
1888    ///
1889    /// Per segment, every matching frame is rewritten as [`Kind::Redacted`]: the HEADERS
1890    /// first, made durable with ONE `fdatasync` (from then on every reader reads those frames
1891    /// as erased, and a crash leaves erased frames, never a frame whose checksum fails), then
1892    /// the blobs' ranges are punched out of the file (zeros where the filesystem cannot punch),
1893    /// then ONE `fsync` — two per segment, where it used to be one per frame (R5.1-B). The sequence, the offsets and the index are untouched: a twin's gap
1894    /// rule, a reader's seek and the retention floor mean exactly what they meant.
1895    ///
1896    /// `RepoStore` frames stay: a replay needs the store's create and purge to land
1897    /// in order. Frames AT or after `before` are never touched — a repository created
1898    /// again under the same name after the erase is somebody's new data. Idempotent:
1899    /// an erased frame is punched again (cheap), so an erase a crash cut between its
1900    /// headers and its punches is finished by the next. **Appends do not wait for it**
1901    /// (R5.1-B, 2026-10-07): the writer lock is taken only to publish what is staged and
1902    /// to read the plan, and an erase only touches frames below `before`, all of them
1903    /// published; one erase runs at a time.
1904    pub fn erase(&self, before: u64, repos: &[String]) -> Result<Erased> {
1905        let _one = self.erase_lock.lock().unwrap_or_else(|p| p.into_inner());
1906        // **The writer lock only to publish and to read the plan** (R5.1-B, 2026-10-07). Every
1907        // frame below `before` is published (durable, indexed) once this block ends, and the erase
1908        // touches nothing else: appends go on at the tail beside it.
1909        let plan: Vec<(u64, Vec<u64>)> = {
1910            let mut w = self.lock_writer()?;
1911            self.publish(&mut w)?;
1912            self.segments
1913                .lock()
1914                .expect("txlog segments")
1915                .iter()
1916                .filter(|s| !s.offsets.is_empty() && s.first < before)
1917                .map(|s| (s.first, s.offsets.clone()))
1918                .collect()
1919        };
1920        let data = [Kind::Pack as u16, Kind::Refs as u16, Kind::LfsPut as u16, Kind::LfsDelete as u16];
1921        let mut out = Erased::default();
1922        let mut hdr = [0u8; HEADER_LEN];
1923        for (first, offsets) in plan {
1924            let path = seg_path(&self.dir, first);
1925            let mut file = match OpenOptions::new().read(true).write(true).open(&path) {
1926                Ok(f) => f,
1927                // Retention dropped the whole segment meanwhile: nothing of it is left to erase.
1928                Err(e) if e.kind() == io::ErrorKind::NotFound => continue,
1929                Err(e) => return Err(e.into()),
1930            };
1931            // Pass 1: every matching frame's header becomes Redacted; ONE fdatasync makes them all
1932            // durable before a single byte goes — from then on each frame IS erased, and a crash
1933            // leaves erased frames, never a frame whose checksum fails.
1934            let mut punch: Vec<(u64, u64)> = Vec::new();
1935            for (i, &off) in offsets.iter().enumerate() {
1936                let seq = first + i as u64;
1937                if seq >= before {
1938                    break;
1939                }
1940                file.seek(SeekFrom::Start(off))?;
1941                file.read_exact(&mut hdr)?;
1942                let h = Header::decode(&hdr).ok_or(Error::Corrupt { path: path.clone(), offset: off, what: "header" })?;
1943                let redacted_already = h.kind == Kind::Redacted as u16;
1944                if !redacted_already && !data.contains(&h.kind) {
1945                    continue;
1946                }
1947                let mut meta = vec![0u8; h.meta_len as usize];
1948                file.read_exact(&mut meta)?;
1949                let named = Meta::from_bytes(&meta).ok().and_then(|m| m.repo().map(str::to_owned));
1950                if !named.is_some_and(|r| repos.contains(&r)) {
1951                    continue;
1952                }
1953                let blob_at = off + HEADER_LEN as u64 + h.meta_len as u64;
1954                if redacted_already {
1955                    // Erased before — punched again (idempotent, cheap), so an erase a crash cut
1956                    // between its headers and its punches is finished by the next one.
1957                    punch.push((blob_at, h.blob_len));
1958                    continue;
1959                }
1960                let redacted = Header { kind: Kind::Redacted as u16, ..h };
1961                file.seek(SeekFrom::Start(off))?;
1962                file.write_all(&redacted.encode())?;
1963                punch.push((blob_at, h.blob_len));
1964                out.frames += 1;
1965                out.bytes += h.blob_len;
1966            }
1967            if punch.is_empty() {
1968                continue;
1969            }
1970            file.sync_data()?;
1971            self.erase_syncs.fetch_add(1, Ordering::Relaxed);
1972            #[cfg(test)]
1973            self.erase_gate();
1974            // Pass 2: the bytes, then ONE fsync.
1975            for (at, len) in punch {
1976                zero_range(&mut file, at, len)?;
1977            }
1978            file.sync_all()?;
1979            self.erase_syncs.fetch_add(1, Ordering::Relaxed);
1980        }
1981        Ok(out)
1982    }
1983
1984    /// **How many fsyncs [`Log::erase`] made** since this log was opened.
1985    pub fn erase_syncs(&self) -> u64 {
1986        self.erase_syncs.load(Ordering::Relaxed)
1987    }
1988
1989    /// Test-only: the next erase waits on `gate` once its first header is down.
1990    #[cfg(test)]
1991    fn hold_next_erase(&self, gate: std::sync::mpsc::Receiver<()>) {
1992        self.erase_gates.lock().unwrap().push_back(gate);
1993    }
1994
1995    #[cfg(test)]
1996    fn erase_gate(&self) {
1997        let gate = self.erase_gates.lock().unwrap_or_else(|p| p.into_inner()).pop_front();
1998        if let Some(gate) = gate {
1999            let _ = gate.recv();
2000        }
2001    }
2002
2003    /// **Walk every retained frame's header and meta, oldest first**, without its blob: one
2004    /// open and one sequential buffered read per SEGMENT, each blob skipped by a relative seek
2005    /// inside the buffer, one header array and one meta buffer reused for every frame (T17).
2006    ///
2007    /// [`Log::locate_blob`] opens the segment file once per frame; a scan that runs on every
2008    /// start (`gunnar_server::twin::rebuild_audit_trail`) cannot pay an `open(2)` per frame of a
2009    /// long log. Bounded by the head at call time; a frame whose header does not decode, or
2010    /// whose sequence is not the one its position says, is [`Error::Corrupt`].
2011    pub fn scan_meta(&self, mut each: impl FnMut(u64, u16, &[u8])) -> Result<()> {
2012        let head = self.head_seq();
2013        let plan: Vec<(u64, u64, u64)> = self
2014            .segments
2015            .lock()
2016            .expect("txlog segments")
2017            .iter()
2018            .filter(|s| !s.offsets.is_empty())
2019            .map(|s| (s.first, s.offsets[0], s.offsets.len() as u64))
2020            .collect();
2021        let mut hdr = [0u8; HEADER_LEN];
2022        let mut meta: Vec<u8> = Vec::with_capacity(256);
2023        for (first, start, n) in plan {
2024            let path = seg_path(&self.dir, first);
2025            let mut r = io::BufReader::with_capacity(1 << 16, File::open(&path)?);
2026            r.seek(SeekFrom::Start(start))?;
2027            let mut offset = start;
2028            for seq in first..first + n {
2029                if seq > head {
2030                    return Ok(());
2031                }
2032                r.read_exact(&mut hdr)?;
2033                let h = match Header::decode(&hdr) {
2034                    Some(h) if h.seq == seq => h,
2035                    Some(_) => return Err(Error::Corrupt { path, offset, what: "sequence out of place" }),
2036                    None => return Err(Error::Corrupt { path, offset, what: "header" }),
2037                };
2038                meta.resize(h.meta_len as usize, 0);
2039                r.read_exact(&mut meta)?;
2040                each(seq, h.kind, &meta);
2041                r.seek_relative((h.blob_len + TRAILER_LEN as u64) as i64)?;
2042                offset += h.frame_len();
2043            }
2044        }
2045        Ok(())
2046    }
2047
2048    /// Read `from..=to` whole (bounded by the head).
2049    pub fn range(&self, from: u64, to: u64) -> Result<Vec<Entry>> {
2050        let to = to.min(self.head_seq());
2051        let mut out = Vec::new();
2052        let mut seq = from;
2053        while seq <= to {
2054            out.push(self.read(seq)?);
2055            seq += 1;
2056        }
2057        Ok(out)
2058    }
2059
2060    /// Drop every whole segment whose last sequence is below `seq`. The
2061    /// open segment is never dropped. Returns the new first sequence.
2062    pub fn truncate_before(&self, seq: u64) -> Result<u64> {
2063        let _w = self.lock_writer()?;
2064        let mut segs = self.segments.lock().expect("txlog segments");
2065        while segs.len() > 1 {
2066            let drop = match segs[0].last_seq() {
2067                Some(last) => last < seq,
2068                None => true,
2069            };
2070            if !drop {
2071                break;
2072            }
2073            let first = segs[0].first;
2074            fs::remove_file(seg_path(&self.dir, first))?;
2075            let _ = fs::remove_file(idx_path(&self.dir, first));
2076            segs.remove(0);
2077        }
2078        sync_dir(&self.dir)?;
2079        let first = segs[0].first;
2080        self.first.store(first, Ordering::Release);
2081        Ok(first)
2082    }
2083
2084    /// **Retain from `seq`**: close the open segment (when it holds anything) so every frame
2085    /// up to the head sits in a closed segment, then drop every whole segment below `seq`.
2086    /// Returns the new first sequence. `seq` past the head retains nothing old: the log keeps
2087    /// its numbers and the next append takes `head + 1`. The operator's retention door — and
2088    /// the drill's way to open a retention hole without writing a gigabyte first.
2089    pub fn retain_from(&self, seq: u64) -> Result<u64> {
2090        {
2091            let mut w = self.writer.lock().expect("txlog writer");
2092            self.publish(&mut w)?;
2093            if !w.seg.offsets.is_empty() {
2094                let next_first = w.seg.first + w.seg.offsets.len() as u64;
2095                w.file.sync_all()?;
2096                w.idx.sync_all()?;
2097                let file = File::create(seg_path(&self.dir, next_first))?;
2098                let idx = File::create(idx_path(&self.dir, next_first))?;
2099                sync_dir(&self.dir)?;
2100                w.file = file;
2101                w.idx = idx;
2102                w.disk_len = 0;
2103                w.published = 0;
2104                w.seg = Segment { first: next_first, offsets: Vec::new(), len: 0 };
2105                self.segments.lock().expect("txlog segments").push(Segment { first: next_first, offsets: Vec::new(), len: 0 });
2106            }
2107        }
2108        self.truncate_before(seq)
2109    }
2110
2111    /// **Start over**: drop every segment and begin again at sequence 1 with
2112    /// nothing in the log. The restore door on a fresh box: its own first
2113    /// frames (its minted KEK) make way for the twin's numbers. Refused when
2114    /// the log holds more than `keep_at_most` frames — a box with history is
2115    /// not fresh, and a reset would be data loss with a verb on it.
2116    pub fn reset(&self, keep_at_most: u64) -> Result<()> {
2117        let head = self.head_seq();
2118        if head > keep_at_most {
2119            return Err(Error::OutOfOrder {
2120                got: head,
2121                expected: keep_at_most,
2122            });
2123        }
2124        self.restart_after(0)
2125    }
2126
2127    /// **Start over after `at`**: drop every segment; the log is empty with its head at `at`
2128    /// and the next append taking `at + 1`. What a twin does once it has installed a snapshot
2129    /// taken at `at` — it holds that state, and the log resumes from the next number.
2130    pub fn restart_after(&self, at: u64) -> Result<()> {
2131        let mut w = self.lock_writer()?;
2132        let mut segs = self.segments.lock().expect("txlog segments");
2133        for s in segs.iter() {
2134            fs::remove_file(seg_path(&self.dir, s.first))?;
2135            let _ = fs::remove_file(idx_path(&self.dir, s.first));
2136        }
2137        segs.clear();
2138        let first = at + 1;
2139        File::create(seg_path(&self.dir, first))?;
2140        File::create(idx_path(&self.dir, first))?;
2141        sync_dir(&self.dir)?;
2142        segs.push(Segment {
2143            first,
2144            offsets: Vec::new(),
2145            len: 0,
2146        });
2147        w.file = OpenOptions::new().append(true).open(seg_path(&self.dir, first))?;
2148        w.idx = OpenOptions::new().append(true).open(idx_path(&self.dir, first))?;
2149        w.seg = Segment {
2150            first,
2151            offsets: Vec::new(),
2152            len: 0,
2153        };
2154        w.disk_len = 0;
2155        w.buf.clear();
2156        w.published = 0;
2157        self.head.store(at, Ordering::Release);
2158        self.first.store(first, Ordering::Release);
2159        Ok(())
2160    }
2161
2162    /// **The segments as retention sees them**, oldest first, the open one last:
2163    /// `(first, last, bytes)` — `last` is `first - 1` for a segment that holds no
2164    /// frame yet. What a byte-bounded retention decides from (2026-10-06, cov-13):
2165    /// only whole segments are ever dropped, so the bytes a `from` keeps are the
2166    /// sum of the segments at and after the one holding it.
2167    pub fn layout(&self) -> Vec<(u64, u64, u64)> {
2168        self.segments
2169            .lock()
2170            .expect("txlog segments")
2171            .iter()
2172            .map(|s| (s.first, s.first + s.offsets.len() as u64 - 1, s.len))
2173            .collect()
2174    }
2175
2176    /// Bytes on disk across all segments.
2177    pub fn bytes(&self) -> u64 {
2178        self.segments
2179            .lock()
2180            .expect("txlog segments")
2181            .iter()
2182            .map(|s| s.len)
2183            .sum()
2184    }
2185}
2186
2187/// A streaming append in progress. See [`Log::begin`].
2188///
2189/// **Dropped without [`Pending::commit`] or [`Pending::abort`], it aborts** (twin-streaming,
2190/// 2026-10-06). The partial frame is already in the segment file, and the writer appends with
2191/// `O_APPEND`: before this, a `?` between `begin` and `commit` (a write that failed, a wire that
2192/// broke under a twin's fetch) left the half frame where it was, the next append landed AFTER
2193/// it while the index recorded the offset BEFORE it, and every read of that next frame was a
2194/// corrupt header — a log broken by one failed transfer. Now the half frame is cut off the
2195/// segment on every path out, and a partial transfer leaves exactly nothing.
2196pub struct Pending<'a> {
2197    log: &'a Log,
2198    w: std::sync::MutexGuard<'a, Writer>,
2199    header: Header,
2200    crc: Crc32c,
2201    written: u64,
2202    /// Committed, or the partial frame already cut: nothing for `Drop` to do.
2203    finished: bool,
2204}
2205
2206impl Pending<'_> {
2207    /// The sequence number this frame will commit as.
2208    pub fn seq(&self) -> u64 {
2209        self.header.seq
2210    }
2211
2212    /// Bytes of the blob still owed.
2213    pub fn remaining(&self) -> u64 {
2214        self.header.blob_len - self.written
2215    }
2216
2217    /// Write a chunk of the blob.
2218    pub fn write(&mut self, chunk: &[u8]) -> Result<()> {
2219        if chunk.len() as u64 > self.remaining() {
2220            return Err(Error::Io(io::Error::new(
2221                io::ErrorKind::InvalidInput,
2222                "txlog: chunk exceeds the declared blob length",
2223            )));
2224        }
2225        self.w.put(chunk)?;
2226        self.crc.update(chunk);
2227        self.written += chunk.len() as u64;
2228        Ok(())
2229    }
2230
2231    /// Finish: trailer, fsync, index. Durable when this returns — and so is every frame of a
2232    /// group committed before it ([`Pending::commit_unsynced`]).
2233    pub fn commit(mut self) -> Result<u64> {
2234        let seq = self.close()?;
2235        self.log.publish(&mut self.w)?;
2236        Ok(seq)
2237    }
2238
2239    /// **Finish WITHOUT making it durable — one frame of a group commit** (perf twin-apply-fsync,
2240    /// 2026-10-06). The frame is whole in the segment (staged or written, trailer and all), it
2241    /// holds its sequence number — the next `begin` takes the one after — but it is not fsynced,
2242    /// not indexed, not visible to readers and not the head until [`Log::sync`] (or any synced
2243    /// append, roll or release) makes the whole group durable with ONE `fdatasync`.
2244    ///
2245    /// A crash before that loses the group's tail, never anything acknowledged: `open` keeps
2246    /// every whole frame that reached the disk (each checks its own crc) and cuts the rest, and a
2247    /// twin re-fetches from its durable head.
2248    pub fn commit_unsynced(mut self) -> Result<u64> {
2249        self.close()
2250    }
2251
2252    /// The trailer and the writer's own bookkeeping: the frame is committed, not yet durable.
2253    fn close(&mut self) -> Result<u64> {
2254        if self.remaining() != 0 {
2255            let owed = self.remaining();
2256            self.abort_inner()?;
2257            return Err(Error::Io(io::Error::new(
2258                io::ErrorKind::UnexpectedEof,
2259                format!("txlog: blob short by {owed} bytes at commit"),
2260            )));
2261        }
2262        let tr = self.crc.finish().to_le_bytes();
2263        self.w.put(&tr)?;
2264        // From here the frame is whole: it is the writer's, never cut by a drop.
2265        self.finished = true;
2266        let off = self.w.seg.len;
2267        self.w.seg.offsets.push(off);
2268        self.w.seg.len += self.header.frame_len();
2269        Ok(self.header.seq)
2270    }
2271
2272    /// Give up: the partial frame is cut off the segment.
2273    pub fn abort(mut self) -> Result<()> {
2274        self.abort_inner()
2275    }
2276
2277    fn abort_inner(&mut self) -> Result<()> {
2278        self.finished = true;
2279        if let Err(e) = self.w.cut_to_committed() {
2280            // The half frame stays where the next append would land after it: nothing more is.
2281            self.log.poison(format!("a partial frame could not be cut off the segment ({e})"));
2282            return Err(e.into());
2283        }
2284        Ok(())
2285    }
2286}
2287
2288impl Drop for Pending<'_> {
2289    fn drop(&mut self) {
2290        if self.finished {
2291            return;
2292        }
2293        // A cut that fails here poisons the log; the half frame is the torn tail `open` cuts at
2294        // the next start (it was never indexed and its trailer never written).
2295        let _ = self.abort_inner();
2296    }
2297}
2298
2299/// **Is this path (relative to a data root) a CACHE** — something rebuilt from packs,
2300/// derived from another store, or scratch, that may be lost and must never exist on the
2301/// twin? The exploded read cache, the start-up disk probe, repack and quarantine scratch,
2302/// the metadata mirror (a derived copy of the identity table), the grow journal, and every
2303/// temporary. The drill scans the twin's disk for these.
2304pub fn is_cache_path(rel: &str) -> bool {
2305    let rel = rel.trim_start_matches("./");
2306    // A box with no /serialdat partition has that tree at `<data-root>/.serialdat`, with
2307    // `crash`, `log`, `gc`, `quarantine` linked to it: judge a path by its place in the tree.
2308    let rel = rel.strip_prefix(".serialdat/").or_else(|| rel.strip_prefix("serialdat/")).unwrap_or(rel);
2309    let first = rel.split('/').next().unwrap_or("");
2310    let name = rel.rsplit('/').next().unwrap_or("");
2311    matches!(first, ".korp-disktest" | "gc" | "quarantine" | "meta" | "serialdat")
2312        || rel.split('/').any(|c| c.starts_with("objects.exploded"))
2313        // The commit-graph memo the engine folds from the packs (C2): derived, and off on a twin.
2314        || name == "objects.graph"
2315        || name.ends_with(".tmp")
2316        || name.starts_with("incoming-")
2317        || name == "grow-journal"
2318}
2319
2320/// **The control plane's audit trail, as the LOG names it** — one canonical relative path,
2321/// whatever the layout config put the `log` directory on.
2322///
2323/// It is a frame like `attestations.jsonl` is a frame, and it is spelled here rather than
2324/// derived from a physical path because the two boxes in a restore need not agree on where
2325/// `log/` physically lives: the frame carries the name, the applying box resolves the name.
2326pub const AUDIT_REL: &str = "log/audit-chain.jsonl";
2327
2328/// **A twin's — or a restoring box's — OWN audit trail.** Per box, never framed, never sent.
2329///
2330/// A twin serves a control plane of its own (`Mirror.*`, browse), and every decision it makes
2331/// is ITS decision, not the primary's. Writing those into [`AUDIT_REL`] would interleave a
2332/// second writer into a hash chain whose whole value is that it has exactly one — the `prev`
2333/// links would not hold and the restored copy would be refused at open. So the twin's own
2334/// decisions go here, this file stays [`is_per_box_path`], and the twin **originates nothing**
2335/// into the trail it is holding for the primary.
2336///
2337/// The consequence is the one worth having: a restored box opens [`AUDIT_REL`], finds the dead
2338/// primary's tip, and appends its own first decision with `prev` = that tip. **The trail is one
2339/// unbroken chain across the disaster.**
2340pub const AUDIT_LOCAL_REL: &str = "log/audit-chain.local.jsonl";
2341
2342/// **The site's WireGuard hub declaration** — `<data root>/wg/hub.conf`, laid there by
2343/// `korp-installer` through the ESP data stage on a fresh install
2344/// (`korp_installer::esp::DATA_STAGE_DIR`), then never written again.
2345///
2346/// It is **public site data**: the hub's public key, endpoint, allowed IPs and the
2347/// per-box `[Interface] Address` gunnar requires (`gunnar_server::appliance`). It is the
2348/// answer to "how is this box reached", and a restored main that does not have it is a box
2349/// nobody can reach — the worst thing to discover on the day the twin is used.
2350///
2351/// Framed since 2026-09-21 (lane T11): the twin holds it and a restore brings it back.
2352pub const HUB_CONF_REL: &str = "wg/hub.conf";
2353
2354/// **The digest of the hub declaration this box last framed**, so a boot that changes
2355/// nothing frames nothing. Per box, never framed — it is a record of what THIS log already
2356/// carries, and it is meaningless on any other box.
2357pub const HUB_CONF_FRAMED_REL: &str = "wg/hub.conf.framed";
2358
2359/// **The machine's own WireGuard identity** — the private key minted on the machine and
2360/// wrapped to its own TPM, and its public half.
2361///
2362/// **Never replicated, and now refused by name.** Two reasons, either sufficient: it is a
2363/// PRIVATE KEY, and a twin that held it would be a second place to steal the site's network
2364/// identity from; and it names THIS box to the hub, so a restored main carrying it would come
2365/// up claiming the dead machine's identity on a tunnel the hub has registered to a key that no
2366/// longer exists anywhere. The same ruling holger makes about its admin token and its role
2367/// record. Before this they were merely un-framed by accident — nothing wrote them through the
2368/// sink — which is not the same as being refused.
2369pub const WG_IDENTITY_KEY_REL: &str = "wg/identity.key";
2370/// See [`WG_IDENTITY_KEY_REL`]. The public half is equally per box: it names this machine.
2371pub const WG_IDENTITY_PUB_REL: &str = "wg/identity.pub";
2372
2373/// **Is this path a record of THIS box** — not cache and not a transaction: its own crash
2374/// records and console log (`crash/`, `log/`), a twin's own `twin.json` and
2375/// `twin.key`, and the restore markers. A twin keeps its own; the primary's are never sent.
2376///
2377/// **[`AUDIT_REL`] is the one exception, since 2026-09-21 (lane T11).** It used to be swept up
2378/// by `log/` and was therefore the one class of state a restore silently destroyed: the record
2379/// of who did what, gone on exactly the day somebody needs it. It is a signed-shaped
2380/// append-only log like the other nine in [`FRAMED_FILES`] and it is replicated like them.
2381/// [`AUDIT_LOCAL_REL`] stays per box — see its docs for why the two must not be one file.
2382pub fn is_per_box_path(rel: &str) -> bool {
2383    let rel = rel.trim_start_matches("./");
2384    // A box with no /serialdat partition has that tree at `<data-root>/.serialdat`, with
2385    // `crash`, `log`, `gc`, `quarantine` linked to it: judge a path by its place in the tree.
2386    let rel = rel.strip_prefix(".serialdat/").or_else(|| rel.strip_prefix("serialdat/")).unwrap_or(rel);
2387    if rel == AUDIT_REL || rel == HUB_CONF_REL {
2388        return false;
2389    }
2390    // The machine's own WireGuard identity and the digest of what this log already carries:
2391    // see [`WG_IDENTITY_KEY_REL`] and [`HUB_CONF_FRAMED_REL`].
2392    if matches!(rel, WG_IDENTITY_KEY_REL | WG_IDENTITY_PUB_REL | HUB_CONF_FRAMED_REL) {
2393        return true;
2394    }
2395    let first = rel.split('/').next().unwrap_or("");
2396    // `txlog-snapshot-base` is the snapshot a store-only twin KEEPS as bytes (ruling 2026-09-22);
2397    // `-dropped` / `-too-small` are its resync's room markers (twin-resync-space, 2026-10-06).
2398    matches!(
2399        first,
2400        "crash" | "log" | "twin.json" | "twin.key" | "txlog-restoring" | "txlog-fresh-docs.json" | "txlog-reseal" | "txlog-snapshot-installing" | "txlog-snapshot-base" | "txlog-snapshot-base.old"
2401            | "txlog-snapshot-dropped" | "txlog-snapshot-too-small"
2402            // The entry a restoring box holds part of (restore-large-entry, 2026-10-07).
2403            | "txlog-restore-partial"
2404            // The twin's base a restoring box holds part of (restore-after-snapshot, 2026-10-07).
2405            | "txlog-restore-snapshot"
2406    )
2407}
2408
2409/// **The documents under a data root that are frames** (`Append` and `File` kinds): the signed
2410/// append-only logs and their tree heads, and the whole documents. A snapshot carries each that
2411/// exists; installing one removes each that the snapshot does not carry. A new sink writer adds
2412/// its file here (the twin test fixture checks every sink-framed path is listed).
2413pub const FRAMED_FILES: &[&str] = &[
2414    "attestations.jsonl",
2415    "transparency-heads.jsonl",
2416    "builds.jsonl",
2417    "build-heads.jsonl",
2418    "forge.jsonl",
2419    "forge-heads.jsonl",
2420    "membership.jsonl",
2421    "membership-heads.jsonl",
2422    "replication.json",
2423    "webhooks.json",
2424    // The control plane's audit trail (lane T11, 2026-09-21). Listed LAST so the ordering of
2425    // the nine above — which several fixtures index positionally — does not move.
2426    AUDIT_REL,
2427    // The site's WireGuard hub declaration (lane T11): a restored main that does not have it
2428    // is a box nobody can reach.
2429    HUB_CONF_REL,
2430];
2431
2432/// **Never a transaction**: cache ([`is_cache_path`]) or per box ([`is_per_box_path`]).
2433/// The sink refuses to frame one and the twin refuses to apply one.
2434pub fn never_framed(rel: &str) -> bool {
2435    is_cache_path(rel) || is_per_box_path(rel)
2436}
2437
2438/// **The sink**: how a writer that holds only a PATH (an append-only signed
2439/// log under the data root, an atomically replaced document) frames its write
2440/// in the log of the data root that path is under — without threading an
2441/// `Arc<Log>` through every constructor that names a file.
2442///
2443/// `ServerStores::open` registers `(data root, log)` on a primary. A path
2444/// under no registered root (a test's scratch file, a box with no log, a
2445/// twin — whose writes are applies, never originations) frames nothing. The
2446/// registry holds `Weak`s, so a dropped store unregisters itself.
2447pub mod sink {
2448    use super::*;
2449
2450    static ROOTS: Mutex<Vec<(PathBuf, Weak<Log>)>> = Mutex::new(Vec::new());
2451
2452    fn refuse_cache(rel: &str) -> Result<()> {
2453        if never_framed(rel) {
2454            return Err(Error::BadMeta(format!("{rel} is a cache or per-box path; it is never a transaction")));
2455        }
2456        Ok(())
2457    }
2458
2459    /// Frame writes under `root` into `log`.
2460    pub fn register(root: &Path, log: &Arc<Log>) {
2461        let mut roots = ROOTS.lock().expect("txlog sink");
2462        roots.retain(|(r, w)| w.strong_count() > 0 && r != root);
2463        roots.push((root.to_path_buf(), Arc::downgrade(log)));
2464    }
2465
2466    /// The log `path` is framed into and `path` relative to its root.
2467    pub fn log_for(path: &Path) -> Option<(Arc<Log>, String)> {
2468        let roots = ROOTS.lock().expect("txlog sink");
2469        roots
2470            .iter()
2471            .filter(|(r, _)| path.starts_with(r))
2472            .max_by_key(|(r, _)| r.as_os_str().len())
2473            .and_then(|(r, w)| {
2474                let log = w.upgrade()?;
2475                let rel = path.strip_prefix(r).ok()?.to_string_lossy().into_owned();
2476                Some((log, rel))
2477            })
2478    }
2479
2480    /// A framed write in progress: hold it until the file has the write (the mutation gate,
2481    /// [`Log::mutation`]). Empty when the path is under no registered root.
2482    #[must_use = "hold the frame until the file has the write"]
2483    pub struct Framed(#[allow(dead_code)] Option<MutationGuard>);
2484
2485    /// Before `line` (no newline) is appended to the file at `path`.
2486    pub fn append_line(path: &Path, line: &str) -> Result<Framed> {
2487        if let Some((log, rel)) = log_for(path) {
2488            refuse_cache(&rel)?;
2489            let guard = log.mutation();
2490            log.append(Kind::Append, &Meta::Append { log: rel }, line.as_bytes())?;
2491            return Ok(Framed(Some(guard)));
2492        }
2493        Ok(Framed(None))
2494    }
2495
2496    /// **A line framed and NOT yet durable** ([`stage_line`]): hold it — it is the mutation
2497    /// guard — until the file has the line, and write the line only after [`StagedLine::durable`].
2498    #[must_use = "wait for the frame to be durable, then write the line, then drop this"]
2499    pub struct StagedLine {
2500        _guard: MutationGuard,
2501        log: Arc<Log>,
2502        seq: u64,
2503    }
2504
2505    impl StagedLine {
2506        /// The frame's sequence number.
2507        pub fn seq(&self) -> u64 {
2508            self.seq
2509        }
2510        /// Return once the frame is durable — group-committed with every frame staged with it
2511        /// ([`Log::sync_through`]).
2512        pub fn durable(&self) -> Result<()> {
2513            self.log.sync_through(self.seq)
2514        }
2515        /// The log the frame is in.
2516        pub fn log(&self) -> &Arc<Log> {
2517            &self.log
2518        }
2519    }
2520
2521    /// **[`append_line`], group-committed** (perf, 2026-10-06): the frame is staged and its
2522    /// fsync is shared with every other frame staged before the leader's ([`Log::sync_through`]).
2523    /// `None` = the path is under no registered root (nothing is framed).
2524    pub fn stage_line(path: &Path, line: &str) -> Result<Option<StagedLine>> {
2525        if let Some((log, rel)) = log_for(path) {
2526            refuse_cache(&rel)?;
2527            if log.is_follower() {
2528                return Err(Error::Follower);
2529            }
2530            let guard = log.mutation();
2531            let seq = log.stage_raw(Kind::Append as u16, &Meta::Append { log: rel }.to_bytes(), line.as_bytes())?;
2532            return Ok(Some(StagedLine { _guard: guard, log, seq }));
2533        }
2534        Ok(None)
2535    }
2536
2537    /// Before the document at `path` is replaced by `bytes`, or removed.
2538    pub fn replace_file(path: &Path, bytes: &[u8], deleted: bool) -> Result<Framed> {
2539        if let Some((log, rel)) = log_for(path) {
2540            refuse_cache(&rel)?;
2541            let guard = log.mutation();
2542            log.append(Kind::File, &Meta::File { path: rel, deleted }, bytes)?;
2543            return Ok(Framed(Some(guard)));
2544        }
2545        Ok(Framed(None))
2546    }
2547}
2548
2549#[cfg(test)]
2550mod tests {
2551    use super::*;
2552
2553    fn meta(repo: &str) -> Meta {
2554        Meta::Pack {
2555            repo: repo.into(),
2556            hash: "sha256".into(),
2557        }
2558    }
2559
2560    /// ★ **A failed fsync poisons the log** (stinky row 3, fsyncgate). The caller of the append
2561    /// whose `fdatasync` failed was told so, but its frame stayed staged; the NEXT leader's
2562    /// fsync — which Linux answers with success once the error has been reported — published
2563    /// it, and a twin applied a transaction main had reported as failed. After a failed fsync
2564    /// nothing more is written or published until the log is reopened from its committed point.
2565    #[test]
2566    fn a_failed_fsync_poisons_the_log_until_it_is_reopened() {
2567        let dir = tempfile::tempdir().unwrap();
2568        let m = meta("g/r").to_bytes();
2569        {
2570            let log = Log::open(dir.path()).unwrap();
2571            assert_eq!(log.append_raw(Kind::Pack as u16, &m, b"one").unwrap(), 1);
2572            log.fail_next_fsyncs(1);
2573            log.append_raw(Kind::Pack as u16, &m, b"two").expect_err("its fsync failed");
2574            let next = log.append_raw(Kind::Pack as u16, &m, b"three");
2575            assert!(next.is_err(), "an append after a failed fsync was taken (and published the failed frame with it): {next:?}");
2576            assert!(log.sync().is_err() || log.head_seq() == 1, "a sync published past the failed fsync");
2577            assert_eq!(log.head_seq(), 1, "the frame whose fsync failed was published");
2578            assert!(log.read(2).is_err(), "the frame whose fsync failed is readable");
2579        }
2580        let log = Log::open(dir.path()).unwrap();
2581        assert_eq!(log.head_seq(), 1, "a reopen resurrected the frame whose fsync failed");
2582        assert_eq!(log.append_raw(Kind::Pack as u16, &m, b"again").unwrap(), 2);
2583        assert_eq!(log.read(2).unwrap().blob, b"again");
2584    }
2585
2586    /// ★ **A failed cut poisons the log** (stinky row 6). A frame whose write failed half way is
2587    /// cut back off the segment; when that truncate ALSO failed it was ignored (`let _ =`), the
2588    /// half frame stayed on disk and the next append landed after it while the index recorded
2589    /// the offset before it — every later read a corrupt header, replication broken from there.
2590    #[test]
2591    fn a_failed_cut_of_a_half_frame_poisons_the_log() {
2592        let dir = tempfile::tempdir().unwrap();
2593        let m = meta("g/r").to_bytes();
2594        {
2595            let log = Log::open(dir.path()).unwrap();
2596            assert_eq!(log.append_raw(Kind::Pack as u16, &m, b"one").unwrap(), 1);
2597            {
2598                let mut w = log.writer.lock().unwrap();
2599                // header, meta and the (directly written) blob go out; the trailer fails.
2600                w.faults.fail_put_after = Some(3);
2601                w.faults.fail_cut = true;
2602            }
2603            let big = vec![7u8; DIRECT_BYTES * 2];
2604            log.append_raw(Kind::Pack as u16, &m, &big).expect_err("its trailer failed");
2605            let next = log.append_raw(Kind::Pack as u16, &m, b"three");
2606            assert!(next.is_err(), "an append after a failed cut landed after the half frame: {next:?}");
2607            assert_eq!(log.head_seq(), 1);
2608        }
2609        let log = Log::open(dir.path()).unwrap();
2610        assert_eq!(log.head_seq(), 1);
2611        assert_eq!(log.append_raw(Kind::Pack as u16, &m, b"three").unwrap(), 2);
2612        assert_eq!(log.read(2).unwrap().blob, b"three");
2613    }
2614
2615    #[test]
2616    fn crc32c_known_answer() {
2617        // RFC 3720 / standard check value for "123456789".
2618        assert_eq!(crc32c(b"123456789"), 0xE306_9283);
2619        // Carried across a restart: the state kept after "1234" goes on to the same answer.
2620        let mut a = Crc32c::new();
2621        a.update(b"1234");
2622        let mut b = Crc32c::resume(a.state());
2623        b.update(b"56789");
2624        assert_eq!(b.finish(), 0xE306_9283);
2625    }
2626
2627    #[test]
2628    fn appends_are_dense_and_read_back() {
2629        let dir = tempfile::tempdir().unwrap();
2630        let log = Log::open(dir.path()).unwrap();
2631        assert_eq!(log.head_seq(), 0);
2632        assert_eq!(log.first_seq(), 1);
2633        for i in 1..=10u64 {
2634            let s = log
2635                .append(Kind::Pack, &meta("a/b"), &[i as u8; 100])
2636                .unwrap();
2637            assert_eq!(s, i);
2638        }
2639        assert_eq!(log.head_seq(), 10);
2640        let e = log.read(7).unwrap();
2641        assert_eq!(e.seq, 7);
2642        assert_eq!(e.kind(), Some(Kind::Pack));
2643        assert_eq!(e.blob, vec![7u8; 100]);
2644        assert_eq!(e.meta().unwrap(), meta("a/b"));
2645        assert!(matches!(log.read(11), Err(Error::BeyondHead { .. })));
2646        assert!(matches!(log.read(0), Err(Error::NotRetained { .. })));
2647    }
2648
2649    #[test]
2650    fn reopen_keeps_head_and_trusts_index() {
2651        let dir = tempfile::tempdir().unwrap();
2652        {
2653            let log = Log::open(dir.path()).unwrap();
2654            for i in 0..5u64 {
2655                log.append(Kind::Refs, &meta("x/y"), &[i as u8]).unwrap();
2656            }
2657        }
2658        let log = Log::open(dir.path()).unwrap();
2659        assert_eq!(log.head_seq(), 5);
2660        assert_eq!(log.read(5).unwrap().blob, vec![4u8]);
2661        assert_eq!(log.append(Kind::Refs, &meta("x/y"), b"6").unwrap(), 6);
2662    }
2663
2664    #[test]
2665    fn torn_tail_is_cut_and_the_log_goes_on() {
2666        let dir = tempfile::tempdir().unwrap();
2667        {
2668            let log = Log::open(dir.path()).unwrap();
2669            log.append(Kind::Pack, &meta("a/b"), &[1u8; 1000]).unwrap();
2670            log.append(Kind::Pack, &meta("a/b"), &[2u8; 1000]).unwrap();
2671        }
2672        let path = seg_path(dir.path(), 1);
2673        let len = fs::metadata(&path).unwrap().len();
2674        // Tear the last frame: cut 100 bytes off it.
2675        let f = OpenOptions::new().write(true).open(&path).unwrap();
2676        f.set_len(len - 100).unwrap();
2677        let log = Log::open(dir.path()).unwrap();
2678        assert_eq!(log.head_seq(), 1, "the torn second frame is gone");
2679        assert_eq!(fs::metadata(&path).unwrap().len(), len - 1000 - 32 - 4 - meta("a/b").to_bytes().len() as u64);
2680        assert_eq!(log.append(Kind::Pack, &meta("a/b"), &[3u8; 10]).unwrap(), 2);
2681        drop(log);
2682        let log = Log::open(dir.path()).unwrap();
2683        assert_eq!(log.read(2).unwrap().blob, vec![3u8; 10]);
2684    }
2685
2686    /// One frame through `begin` → `write` → `commit_unsynced`, at `seq`.
2687    fn unsynced(log: &Log, seq: u64, blob: &[u8]) -> u64 {
2688        let m = meta("g/r").to_bytes();
2689        let mut p = log.begin(Some(seq), Kind::Pack as u16, &m, blob.len() as u64).unwrap();
2690        p.write(blob).unwrap();
2691        p.commit_unsynced().unwrap()
2692    }
2693
2694    /// **A group commit is invisible until `sync`, then durable as one** (perf twin-apply-fsync):
2695    /// the frames hold their numbers (the next `begin` takes the one after), nobody reads them and
2696    /// the head does not move until `sync`, which writes the index once and moves the head to the
2697    /// group's last; a reopen finds them all.
2698    #[test]
2699    fn a_group_is_invisible_until_synced_and_then_durable_as_one() {
2700        let dir = tempfile::tempdir().unwrap();
2701        let log = Log::open(dir.path()).unwrap();
2702        log.set_follower(true);
2703        for seq in 1..=5u64 {
2704            assert_eq!(unsynced(&log, seq, &[seq as u8; 100]), seq);
2705        }
2706        assert_eq!(log.head_seq(), 0, "nothing acknowledged before the sync");
2707        assert_eq!(log.unsynced(), 5);
2708        assert!(matches!(log.read(1), Err(Error::BeyondHead { .. })), "a reader does not see a frame that is not durable");
2709        assert!(matches!(log.begin(Some(5), Kind::Pack as u16, b"{}", 0), Err(Error::OutOfOrder { got: 5, expected: 6 })), "the gap rule counts the group");
2710        assert_eq!(fs::metadata(idx_path(dir.path(), 1)).unwrap().len(), 0, "the index is written after the data's fsync");
2711        assert_eq!(log.sync().unwrap(), 5);
2712        assert_eq!((log.head_seq(), log.unsynced()), (5, 0));
2713        assert_eq!(log.read(3).unwrap().blob, vec![3u8; 100]);
2714        assert_eq!(fs::metadata(idx_path(dir.path(), 1)).unwrap().len(), 5 * 8, "one index entry per frame");
2715        assert_eq!(log.sync().unwrap(), 5, "nothing pending: nothing done");
2716        drop(log);
2717        let log = Log::open(dir.path()).unwrap();
2718        assert_eq!(log.head_seq(), 5);
2719        assert_eq!(log.read(5).unwrap().blob, vec![5u8; 100]);
2720    }
2721
2722    /// **A crash before the sync loses only what was never acknowledged.** A synced group stays;
2723    /// a group still staged in memory is simply gone (it never reached the file); a group whose
2724    /// bytes reached the file but were never fsynced is kept frame by frame where whole (each frame
2725    /// checks its own crc), and a half frame is cut. The log takes the next number either way.
2726    #[test]
2727    fn a_crash_before_the_sync_loses_only_what_was_never_acknowledged() {
2728        let dir = tempfile::tempdir().unwrap();
2729        let log = Log::open(dir.path()).unwrap();
2730        for seq in 1..=3u64 {
2731            unsynced(&log, seq, b"acked");
2732        }
2733        assert_eq!(log.sync().unwrap(), 3);
2734        // Staged, never written: a crash (here: the process forgets the log) loses them.
2735        unsynced(&log, 4, b"staged");
2736        unsynced(&log, 5, b"staged");
2737        drop(log);
2738        let log = Log::open(dir.path()).unwrap();
2739        assert_eq!(log.head_seq(), 3, "the acknowledged group is whole; the staged one never reached the file");
2740        // Written (a large piece goes straight to the file), never fsynced, then a half frame.
2741        let big = vec![7u8; DIRECT_BYTES * 2];
2742        unsynced(&log, 4, &big);
2743        let m = meta("g/r").to_bytes();
2744        let mut half = log.begin(Some(5), Kind::Pack as u16, &m, (DIRECT_BYTES * 4) as u64).unwrap();
2745        half.write(&big).unwrap();
2746        std::mem::forget(half); // the process dies with the frame half-written: no cut, no sync
2747        let path = seg_path(dir.path(), 1);
2748        let on_disk = fs::metadata(&path).unwrap().len();
2749        drop(log);
2750        let log = Log::open(dir.path()).unwrap();
2751        assert_eq!(log.head_seq(), 4, "the whole frame that reached the file is kept, the half one is cut");
2752        assert_eq!(log.read(4).unwrap().blob, big);
2753        assert!(fs::metadata(&path).unwrap().len() < on_disk, "the half frame is gone from the segment");
2754        assert_eq!(log.append(Kind::Pack, &meta("g/r"), b"next").unwrap(), 5);
2755    }
2756
2757    /// **A frame given up inside a group takes only itself** — the frames committed before it in
2758    /// the same group stay, staged or written, and the sync makes them durable.
2759    #[test]
2760    fn an_abort_inside_a_group_keeps_the_frames_committed_before_it() {
2761        let dir = tempfile::tempdir().unwrap();
2762        let log = Log::open(dir.path()).unwrap();
2763        unsynced(&log, 1, b"one");
2764        unsynced(&log, 2, &vec![2u8; DIRECT_BYTES]);
2765        let m = meta("g/r").to_bytes();
2766        {
2767            let mut p = log.begin(Some(3), Kind::Pack as u16, &m, 10).unwrap();
2768            p.write(b"abc").unwrap();
2769            // dropped without commit: cut
2770        }
2771        {
2772            let mut p = log.begin(Some(3), Kind::Pack as u16, &m, (DIRECT_BYTES * 2) as u64).unwrap();
2773            p.write(&vec![9u8; DIRECT_BYTES]).unwrap();
2774            p.abort().unwrap();
2775        }
2776        assert_eq!(log.sync().unwrap(), 2);
2777        assert_eq!(log.read(2).unwrap().blob, vec![2u8; DIRECT_BYTES]);
2778        assert_eq!(unsynced(&log, 3, b"three"), 3);
2779        assert_eq!(log.sync().unwrap(), 3);
2780        drop(log);
2781        let log = Log::open(dir.path()).unwrap();
2782        assert_eq!(log.head_seq(), 3);
2783        assert_eq!(log.read(3).unwrap().blob, b"three".to_vec());
2784    }
2785
2786    /// **A waiter whose frame is already durable returns at once — it never waits for the NEXT
2787    /// leader's fsync** (r5 stall review, 2026-10-07: the UI rig's control writes took 10–36 s
2788    /// with every gunnar-server thread in `futex_wait`).
2789    ///
2790    /// The leader used to hold a `Mutex<()>` across its `fdatasync`, and a waiter could only see
2791    /// that its frame was durable after ACQUIRING that mutex. Here leader A's fsync covers frames
2792    /// 1 and 2; B (frame 3, staged after A took its snapshot) queues for the leader BEFORE X
2793    /// (frame 2). When A ends, the mutex goes to B, which leads frame 3's fsync — held here by a
2794    /// gate the test never opens until the end — and X, whose frame has been durable since A
2795    /// published, stays parked behind it. On a loaded disk that is one unrelated fsync after
2796    /// another for as long as writes keep coming.
2797    ///
2798    /// RED with the mutex (X does not return while B's fsync is held); GREEN with the flag and
2799    /// condvar, where every waiter re-reads the head on every wake-up.
2800    #[test]
2801    fn a_waiter_whose_frame_is_durable_returns_without_waiting_for_the_next_leaders_fsync() {
2802        use std::sync::mpsc::channel;
2803        use std::time::Duration;
2804        let dir = tempfile::tempdir().unwrap();
2805        let log = Arc::new(Log::open(dir.path()).unwrap());
2806        let m = meta("g/r").to_bytes();
2807        assert_eq!(log.stage_raw(Kind::Pack as u16, &m, b"one").unwrap(), 1);
2808        assert_eq!(log.stage_raw(Kind::Pack as u16, &m, b"x").unwrap(), 2);
2809        let (open_a, gate_a) = channel::<()>();
2810        let (open_b, gate_b) = channel::<()>();
2811        log.hold_next_fsync(gate_a);
2812        log.hold_next_fsync(gate_b);
2813        let gates_left = |log: &Log| log.fsync_gates.lock().unwrap().len();
2814
2815        // A leads frames 1..=2 and is held before its fsync.
2816        let a = { let log = Arc::clone(&log); std::thread::spawn(move || log.sync_through(1).unwrap()) };
2817        while gates_left(&log) != 1 {
2818            std::thread::sleep(Duration::from_millis(5));
2819        }
2820        // Frame 3 is staged after A's snapshot; B waits for it first, then X for frame 2.
2821        assert_eq!(log.stage_raw(Kind::Pack as u16, &m, b"three").unwrap(), 3);
2822        let b = { let log = Arc::clone(&log); std::thread::spawn(move || log.sync_through(3).unwrap()) };
2823        std::thread::sleep(Duration::from_millis(200));
2824        let (x_done, x_said) = channel::<()>();
2825        let x = { let log = Arc::clone(&log); std::thread::spawn(move || { log.sync_through(2).unwrap(); let _ = x_done.send(()); }) };
2826        std::thread::sleep(Duration::from_millis(200));
2827
2828        // A's fsync goes through: frames 1 and 2 are durable and published.
2829        open_a.send(()).unwrap();
2830        a.join().unwrap();
2831        assert!(log.head_seq() >= 2, "A published frames 1 and 2");
2832        // B now leads frame 3 and is held before ITS fsync. X's frame is durable: X must be back.
2833        let t = std::time::Instant::now();
2834        while gates_left(&log) != 0 && t.elapsed() < Duration::from_secs(5) {
2835            std::thread::sleep(Duration::from_millis(5));
2836        }
2837        let held = gates_left(&log) == 0;
2838        let back = x_said.recv_timeout(Duration::from_secs(5));
2839        open_b.send(()).unwrap();
2840        b.join().unwrap();
2841        x.join().unwrap();
2842        assert!(held, "B led frame 3's fsync (its gate was taken)");
2843        assert!(back.is_ok(), "X's frame was durable since A published it, yet X waited behind B's fsync of frame 3");
2844        assert_eq!(log.head_seq(), 3);
2845    }
2846
2847    /// **An account's erase costs about two fsyncs per segment, and appends do not wait for it**
2848    /// (R5.1-B, 2026-10-07). `Log::erase` held the writer lock for the whole erase and made an
2849    /// `fdatasync` per erased frame (the redacted header) plus one per segment: an account with
2850    /// 200 frames was ~200 fsyncs with every append on the box — every control write, every
2851    /// audited call — waiting behind them. Per segment, ONE fdatasync makes every redacted header
2852    /// durable before any byte is punched, and ONE fsync follows the punches; the writer lock is
2853    /// taken only to publish and to read the plan.
2854    #[test]
2855    fn an_erase_is_two_fsyncs_per_segment_and_appends_do_not_wait_for_it() {
2856        use std::sync::mpsc::channel;
2857        use std::time::Duration;
2858        let dir = tempfile::tempdir().unwrap();
2859        let log = Arc::new(Log::open_with(dir.path(), 64 << 10).unwrap());
2860        for i in 0..200u64 {
2861            let repo = if i % 2 == 0 { "gone/a" } else { "stays/b" };
2862            log.append(Kind::Pack, &meta(repo), &vec![i as u8; 1500]).unwrap();
2863            log.append(Kind::Pack, &meta("gone/a"), &vec![7u8; 600]).unwrap();
2864        }
2865        let before = log.append(Kind::Refs, &meta("stays/b"), b"marker").unwrap() ;
2866        let segments = log.segments.lock().unwrap().len() as u64;
2867        let syncs0 = log.erase_syncs();
2868
2869        // The erase is held once its first header is down; an append meanwhile must not wait.
2870        let (open, gate) = channel::<()>();
2871        log.hold_next_erase(gate);
2872        let eraser = { let log = Arc::clone(&log); std::thread::spawn(move || log.erase(before, &["gone/a".to_string()]).unwrap()) };
2873        while !log.erase_gates.lock().unwrap().is_empty() {
2874            std::thread::sleep(Duration::from_millis(5));
2875        }
2876        let (done, said) = channel::<u64>();
2877        let appender = { let log = Arc::clone(&log); std::thread::spawn(move || { let s = log.append(Kind::Refs, &meta("stays/b"), b"during").unwrap(); let _ = done.send(s); }) };
2878        let appended = said.recv_timeout(Duration::from_secs(3));
2879        open.send(()).unwrap();
2880        let erased = eraser.join().unwrap();
2881        appender.join().unwrap();
2882        let syncs = log.erase_syncs() - syncs0;
2883        let line = format!("erased {} frames in {segments} segments with {syncs} fsyncs", erased.frames);
2884        eprintln!("{line}");
2885        assert_eq!(erased.frames, 300, "{line}");
2886        assert!(appended.is_ok(), "an append waited for the erase: {line}");
2887        assert!(syncs <= 2 * segments, "about two fsyncs per segment: {line}");
2888        // Erased frames read as erased, the others as they were.
2889        for seq in 1..before {
2890            let e = log.read(seq).unwrap();
2891            let gone = Meta::from_bytes(&e.meta).unwrap().repo() == Some("gone/a");
2892            assert_eq!(e.kind == Kind::Redacted as u16, gone, "seq {seq}");
2893            assert_eq!(e.blob.is_empty(), gone, "seq {seq}");
2894        }
2895    }
2896
2897    /// **A group that crosses a segment roll** is published at the roll (the old segment is synced
2898    /// whole before the new one starts) and the rest at the sync: every frame reads back, across
2899    /// segments, before and after a reopen. A synced `append` after a group makes the group durable
2900    /// first, in order.
2901    #[test]
2902    fn a_group_across_a_segment_roll_and_a_synced_append_after_it() {
2903        let dir = tempfile::tempdir().unwrap();
2904        let log = Log::open_with(dir.path(), 4096).unwrap();
2905        for seq in 1..=40u64 {
2906            unsynced(&log, seq, &vec![seq as u8; 300]);
2907        }
2908        assert!(log.head_seq() > 0, "the rolls published the groups in the closed segments");
2909        assert_eq!(log.sync().unwrap(), 40);
2910        unsynced(&log, 41, b"group");
2911        assert_eq!(log.append(Kind::Pack, &meta("g/r"), b"synced").unwrap(), 42, "the append comes after the group");
2912        assert_eq!(log.head_seq(), 42);
2913        for seq in 1..=40u64 {
2914            assert_eq!(log.read(seq).unwrap().blob, vec![seq as u8; 300], "seq {seq}");
2915        }
2916        drop(log);
2917        let log = Log::open_with(dir.path(), 4096).unwrap();
2918        assert_eq!(log.head_seq(), 42);
2919        assert_eq!(log.read(41).unwrap().blob, b"group".to_vec());
2920        assert_eq!(log.read(42).unwrap().blob, b"synced".to_vec());
2921    }
2922
2923    #[test]
2924    fn torn_header_is_cut_too() {
2925        let dir = tempfile::tempdir().unwrap();
2926        {
2927            let log = Log::open(dir.path()).unwrap();
2928            log.append(Kind::Pack, &meta("a/b"), &[1u8; 10]).unwrap();
2929        }
2930        let path = seg_path(dir.path(), 1);
2931        let mut f = OpenOptions::new().append(true).open(&path).unwrap();
2932        f.write_all(b"GTX1\x05\x00\x00").unwrap(); // seven bytes of a header
2933        let log = Log::open(dir.path()).unwrap();
2934        assert_eq!(log.head_seq(), 1);
2935        assert_eq!(log.append(Kind::Pack, &meta("a/b"), &[2u8; 10]).unwrap(), 2);
2936    }
2937
2938    /// `scan_meta` answers exactly what `locate_blob` answers, frame by frame, across segment
2939    /// boundaries and a retention hole, with blobs of every size skipped (T17).
2940    #[test]
2941    fn scan_meta_agrees_with_locate_blob_across_segments_and_retention() {
2942        let dir = tempfile::tempdir().unwrap();
2943        let log = Log::open_with(dir.path(), 4096).unwrap();
2944        for i in 0..200u64 {
2945            let blob = vec![i as u8; (i as usize * 37) % 9000];
2946            log.append(Kind::Pack, &meta(&format!("r/{i}")), &blob).unwrap();
2947        }
2948        log.retain_from(50).unwrap();
2949        let mut seen = Vec::new();
2950        log.scan_meta(|seq, kind, m| seen.push((seq, kind, m.to_vec()))).unwrap();
2951        let want: Vec<_> = (log.first_seq()..=log.head_seq()).map(|s| { let at = log.locate_blob(s).unwrap(); (at.seq, at.kind, at.meta) }).collect();
2952        assert!(log.first_seq() > 1 && !want.is_empty());
2953        assert_eq!(seen, want);
2954    }
2955
2956    #[test]
2957    fn corrupted_payload_in_tail_is_cut() {
2958        let dir = tempfile::tempdir().unwrap();
2959        let first_len;
2960        {
2961            let log = Log::open(dir.path()).unwrap();
2962            log.append(Kind::Pack, &meta("a/b"), &[1u8; 10]).unwrap();
2963            first_len = log.bytes();
2964            log.append(Kind::Pack, &meta("a/b"), &[2u8; 10]).unwrap();
2965        }
2966        let path = seg_path(dir.path(), 1);
2967        let mut f = OpenOptions::new().write(true).open(&path).unwrap();
2968        // Flip one byte of the second frame's blob.
2969        f.seek(SeekFrom::Start(first_len + 32 + meta("a/b").to_bytes().len() as u64 + 3)).unwrap();
2970        f.write_all(&[0xEE]).unwrap();
2971        let log = Log::open(dir.path()).unwrap();
2972        assert_eq!(log.head_seq(), 1);
2973    }
2974
2975    #[test]
2976    fn stale_index_is_rebuilt_from_data() {
2977        let dir = tempfile::tempdir().unwrap();
2978        {
2979            let log = Log::open(dir.path()).unwrap();
2980            for _ in 0..4 {
2981                log.append(Kind::Pack, &meta("a/b"), &[9u8; 10]).unwrap();
2982            }
2983        }
2984        // Lose the index entirely, and separately make it lie.
2985        fs::remove_file(idx_path(dir.path(), 1)).unwrap();
2986        fs::write(idx_path(dir.path(), 1), []).unwrap();
2987        let log = Log::open(dir.path()).unwrap();
2988        assert_eq!(log.head_seq(), 4);
2989        drop(log);
2990        fs::write(idx_path(dir.path(), 1), [0u8; 8 * 9]).unwrap();
2991        let log = Log::open(dir.path()).unwrap();
2992        assert_eq!(log.head_seq(), 4);
2993        assert_eq!(log.read(3).unwrap().seq, 3);
2994    }
2995
2996    #[test]
2997    fn segments_roll_and_retention_drops_whole_segments() {
2998        let dir = tempfile::tempdir().unwrap();
2999        let log = Log::open_with(dir.path(), 300).unwrap();
3000        for i in 1..=20u64 {
3001            log.append(Kind::Pack, &meta("a/b"), &[i as u8; 100]).unwrap();
3002        }
3003        assert!(log.segments.lock().unwrap().len() > 3);
3004        assert_eq!(log.read(1).unwrap().blob[0], 1);
3005        let first = log.truncate_before(10).unwrap();
3006        assert!(first <= 10 && first > 1, "first={first}");
3007        assert!(matches!(log.read(1), Err(Error::NotRetained { .. })));
3008        assert_eq!(log.read(10).unwrap().blob[0], 10);
3009        assert_eq!(log.read(20).unwrap().blob[0], 20);
3010        drop(log);
3011        let log = Log::open_with(dir.path(), 300).unwrap();
3012        assert_eq!(log.first_seq(), first);
3013        assert_eq!(log.head_seq(), 20);
3014        assert_eq!(log.append(Kind::Pack, &meta("a/b"), &[21u8; 10]).unwrap(), 21);
3015    }
3016
3017    #[test]
3018    fn append_at_refuses_out_of_order() {
3019        let dir = tempfile::tempdir().unwrap();
3020        let log = Log::open(dir.path()).unwrap();
3021        let m = meta("a/b").to_bytes();
3022        assert_eq!(log.append_at(1, 1, &m, b"x").unwrap(), 1);
3023        assert!(matches!(log.append_at(3, 1, &m, b"x"), Err(Error::OutOfOrder { got: 3, expected: 2 })));
3024        assert!(matches!(log.append_at(1, 1, &m, b"x"), Err(Error::OutOfOrder { .. })));
3025        assert_eq!(log.append_at(2, 1, &m, b"x").unwrap(), 2);
3026    }
3027
3028    #[test]
3029    fn streaming_append_commits_and_aborts() {
3030        let dir = tempfile::tempdir().unwrap();
3031        let log = Log::open(dir.path()).unwrap();
3032        let m = meta("a/b").to_bytes();
3033        {
3034            let mut p = log.begin(Some(1), 1, &m, 6).unwrap();
3035            p.write(b"abc").unwrap();
3036            p.write(b"def").unwrap();
3037            assert_eq!(p.commit().unwrap(), 1);
3038        }
3039        {
3040            let mut p = log.begin(None, 1, &m, 6).unwrap();
3041            p.write(b"abc").unwrap();
3042            p.abort().unwrap();
3043        }
3044        assert_eq!(log.head_seq(), 1);
3045        {
3046            let mut p = log.begin(None, 1, &m, 3).unwrap();
3047            p.write(b"xy").unwrap();
3048            assert!(p.commit().is_err(), "short blob is refused");
3049        }
3050        assert_eq!(log.head_seq(), 1);
3051        assert_eq!(log.append_at(2, 1, &m, b"ok").unwrap(), 2);
3052        drop(log);
3053        let log = Log::open(dir.path()).unwrap();
3054        assert_eq!(log.head_seq(), 2);
3055        assert_eq!(log.read(1).unwrap().blob, b"abcdef");
3056        assert_eq!(log.read(2).unwrap().blob, b"ok");
3057        let l = log.locate_blob(1).unwrap();
3058        assert_eq!(l.blob_len, 6);
3059        let mut f = File::open(&l.path).unwrap();
3060        f.seek(SeekFrom::Start(l.blob_offset)).unwrap();
3061        let mut b = vec![0u8; 6];
3062        f.read_exact(&mut b).unwrap();
3063        assert_eq!(b, b"abcdef");
3064    }
3065
3066    /// **A streaming append DROPPED half-way leaves nothing** (twin-streaming, 2026-10-06): the
3067    /// shape of a twin's fetch whose wire broke inside an entry, or any `?` between `begin` and
3068    /// `commit`. Without `Drop` cutting it, the half frame stayed in the segment, the next append
3069    /// went after it (`O_APPEND`) while the index recorded the offset before it, and that next
3070    /// frame read back as a corrupt header — in this process AND after a restart.
3071    #[test]
3072    fn a_streaming_append_dropped_half_way_leaves_nothing_and_the_next_append_reads_back() {
3073        let dir = tempfile::tempdir().unwrap();
3074        let log = Log::open(dir.path()).unwrap();
3075        let m = meta("a/b").to_bytes();
3076        assert_eq!(log.append_at(1, 1, &m, b"first").unwrap(), 1);
3077        let before = log.bytes();
3078        {
3079            let mut p = log.begin(Some(2), 1, &m, 1 << 20).unwrap();
3080            p.write(&[7u8; 4096]).unwrap();
3081            // dropped: no commit, no abort
3082        }
3083        assert_eq!(log.head_seq(), 1, "the dropped frame is not in the log");
3084        let seg = log.locate_blob(1).unwrap().path;
3085        assert_eq!(std::fs::metadata(&seg).unwrap().len(), before, "and not in the segment file either");
3086        assert_eq!(log.append_at(2, 1, &m, b"second").unwrap(), 2);
3087        assert_eq!(log.read(2).unwrap().blob, b"second", "the next append reads back in this process");
3088        drop(log);
3089        let log = Log::open(dir.path()).unwrap();
3090        assert_eq!(log.head_seq(), 2);
3091        assert_eq!(log.read(1).unwrap().blob, b"first");
3092        assert_eq!(log.read(2).unwrap().blob, b"second", "and after a restart");
3093    }
3094
3095    /// **`read_pieces` hands a frame over in pieces and the last one only after the crc** — the
3096    /// primary's half of a twin's streamed fetch: the pieces are the blob, byte for byte; an
3097    /// empty blob and an erased frame are one empty last piece; and a frame whose bytes rotted
3098    /// on disk delivers every piece BUT the last and then refuses, so a reader that commits on
3099    /// `last` never commits it.
3100    #[test]
3101    fn read_pieces_streams_the_blob_and_withholds_the_last_piece_of_a_corrupt_frame() {
3102        let dir = tempfile::tempdir().unwrap();
3103        let log = Log::open(dir.path()).unwrap();
3104        let blob: Vec<u8> = (0..10_000u32).map(|i| (i * 7 % 251) as u8).collect();
3105        let s = log.append(Kind::Pack, &meta("a/b"), &blob).unwrap();
3106        let empty = log.append(Kind::Refs, &meta("a/b"), b"").unwrap();
3107        let rot = log.append(Kind::Pack, &meta("a/c"), &blob).unwrap();
3108        let mut got = Vec::new();
3109        let mut lasts = 0;
3110        // The checked payload crc rides on the LAST piece only (restore-large-entry).
3111        let mut want = Crc32c::new();
3112        want.update(&meta("a/b").to_bytes());
3113        want.update(&blob);
3114        let want = want.finish();
3115        let at = log
3116            .read_pieces::<Error>(s, 3000, |l, p, last| {
3117                assert_eq!(l.seq, s);
3118                assert!(p.len() <= 3000);
3119                assert_eq!(l.payload_crc, last.then_some(want), "the crc is handed over with the last piece and only then");
3120                got.extend_from_slice(p);
3121                lasts += usize::from(last);
3122                Ok(())
3123            })
3124            .unwrap();
3125        assert_eq!((got, lasts, at.blob_len), (blob.clone(), 1, blob.len() as u64));
3126        assert_eq!(at.payload_crc, Some(want));
3127        // From an offset (a resumed restore): only the bytes from it, the stored crc on the last
3128        // piece; at the blob's end one empty last piece; past it, or on nothing, refused.
3129        let mut tail = Vec::new();
3130        let at = log
3131            .read_pieces_from::<Error>(s, 4321, 3000, |l, p, last| {
3132                assert!(p.len() <= 3000);
3133                assert_eq!(l.payload_crc, last.then_some(want));
3134                tail.extend_from_slice(p);
3135                Ok(())
3136            })
3137            .unwrap();
3138        assert_eq!((tail.as_slice(), at.payload_crc), (&blob[4321..], Some(want)));
3139        let mut end = Vec::new();
3140        log.read_pieces_from::<Error>(s, blob.len() as u64, 3000, |l, p, last| {
3141            end.push((p.len(), last, l.payload_crc));
3142            Ok(())
3143        })
3144        .unwrap();
3145        assert_eq!(end, vec![(0, true, Some(want))]);
3146        assert!(log.read_pieces_from::<Error>(s, blob.len() as u64 + 1, 3000, |_, _, _| Ok(())).is_err());
3147        let mut pieces = Vec::new();
3148        log.read_pieces::<Error>(empty, 3000, |_, p, last| {
3149            pieces.push((p.len(), last));
3150            Ok(())
3151        })
3152        .unwrap();
3153        assert_eq!(pieces, vec![(0, true)]);
3154        // One byte in the middle of the third frame's blob flips on disk.
3155        let l = log.locate_blob(rot).unwrap();
3156        let mut f = OpenOptions::new().read(true).write(true).open(&l.path).unwrap();
3157        f.seek(SeekFrom::Start(l.blob_offset + 5000)).unwrap();
3158        let mut b = [0u8; 1];
3159        f.read_exact(&mut b).unwrap();
3160        f.seek(SeekFrom::Start(l.blob_offset + 5000)).unwrap();
3161        f.write_all(&[b[0] ^ 0xFF]).unwrap();
3162        drop(f);
3163        let mut seen = 0usize;
3164        let mut last_seen = false;
3165        let r = log.read_pieces::<Error>(rot, 3000, |_, p, last| {
3166            seen += p.len();
3167            last_seen |= last;
3168            Ok(())
3169        });
3170        assert!(matches!(r, Err(Error::Corrupt { what: "payload crc", .. })), "{r:?}");
3171        assert!(!last_seen, "the last piece of a corrupt frame is never handed over");
3172        assert!(seen < blob.len(), "{seen} of {} bytes went out before the refusal", blob.len());
3173        assert!(matches!(log.read(rot), Err(Error::Corrupt { .. })), "read refuses the same frame");
3174        // An erased frame: one empty last piece, unchecked.
3175        log.erase(rot + 1, &["a/b".to_string()]).unwrap();
3176        let mut erased = Vec::new();
3177        log.read_pieces::<Error>(s, 3000, |l, p, last| {
3178            erased.push((l.kind, p.len(), last));
3179            Ok(())
3180        })
3181        .unwrap();
3182        assert_eq!(erased, vec![(Kind::Redacted as u16, 0, true)]);
3183    }
3184
3185    #[test]
3186    fn unknown_kind_is_stored_and_read_back() {
3187        let dir = tempfile::tempdir().unwrap();
3188        let log = Log::open(dir.path()).unwrap();
3189        log.append_raw(999, b"{}", b"future").unwrap();
3190        let e = log.read(1).unwrap();
3191        assert_eq!(e.kind(), None);
3192        assert_eq!(e.blob, b"future");
3193    }
3194
3195    #[test]
3196    fn reset_starts_over_at_one_and_refuses_a_box_with_history() {
3197        let dir = tempfile::tempdir().unwrap();
3198        let log = Log::open(dir.path()).unwrap();
3199        log.append(Kind::Kek, &Meta::Kek {}, &[7u8; 32]).unwrap();
3200        // A box with more frames than the door allows is not fresh.
3201        assert!(log.reset(0).is_err());
3202        assert_eq!(log.head_seq(), 1);
3203        log.reset(1).unwrap();
3204        assert_eq!(log.head_seq(), 0);
3205        assert_eq!(log.first_seq(), 1);
3206        // The twin's numbers go in from 1, and survive a reopen.
3207        log.append_at(1, Kind::Kek as u16, &Meta::Kek {}.to_bytes(), &[9u8; 32]).unwrap();
3208        drop(log);
3209        let log = Log::open(dir.path()).unwrap();
3210        assert_eq!(log.head_seq(), 1);
3211        assert_eq!(log.read(1).unwrap().blob, vec![9u8; 32]);
3212    }
3213
3214    #[test]
3215    fn kek_append_and_file_kinds_round_trip() {
3216        let dir = tempfile::tempdir().unwrap();
3217        let log = Log::open(dir.path()).unwrap();
3218        let metas = [
3219            (Kind::Kek, Meta::Kek {}),
3220            (Kind::Append, Meta::Append { log: "attestations.jsonl".into() }),
3221            (Kind::File, Meta::File { path: "replication.json".into(), deleted: false }),
3222        ];
3223        for (k, m) in &metas {
3224            log.append(*k, m, b"x").unwrap();
3225        }
3226        for (i, (k, m)) in metas.iter().enumerate() {
3227            let e = log.read(i as u64 + 1).unwrap();
3228            assert_eq!(e.kind(), Some(*k));
3229            assert_eq!(&e.meta().unwrap(), m);
3230        }
3231    }
3232
3233    #[test]
3234    fn a_follower_log_refuses_originations_and_takes_the_primarys_numbers() {
3235        let dir = tempfile::tempdir().unwrap();
3236        let log = Log::open(dir.path()).unwrap();
3237        log.set_follower(true);
3238        assert!(matches!(log.append(Kind::Pack, &meta("a/b"), b"x"), Err(Error::Follower)));
3239        assert!(matches!(log.begin(None, Kind::Pack as u16, b"{}", 1), Err(Error::Follower)));
3240        log.append_at(1, Kind::Pack as u16, &meta("a/b").to_bytes(), b"x").unwrap();
3241        assert_eq!(log.head_seq(), 1);
3242        log.set_follower(false);
3243        assert_eq!(log.append(Kind::Pack, &meta("a/b"), b"y").unwrap(), 2);
3244    }
3245
3246    #[test]
3247    fn cache_and_per_box_paths_are_judged_by_their_place_in_the_serialdat_tree() {
3248        for p in ["gc/x", ".serialdat/gc/x", "serialdat/quarantine/p.pack", ".serialdat/meta/identity.json", "repos/a/b.git/objects.exploded/1", ".korp-disktest/probe", "webhooks.json.tmp"] {
3249            assert!(is_cache_path(p) && never_framed(p), "{p} is cache");
3250        }
3251        for p in ["crash/panic-1", ".serialdat/log/console.txt", "log/audit-chain.local.jsonl", ".serialdat/log/audit-chain.local.jsonl", "twin.key"] {
3252            assert!(!is_cache_path(p) && is_per_box_path(p) && never_framed(p), "{p} is per box");
3253        }
3254        for p in ["attestations.jsonl", "replication.json", "repos/a/b.git/pack", "control.redb"] {
3255            assert!(!never_framed(p), "{p} is a transaction's path");
3256        }
3257    }
3258
3259    /// **The audit trail is a transaction, not a per-box record** (lane T11, 2026-09-21) — the
3260    /// D6 finding: a restore used to bring a box back with no audit history and say nothing.
3261    ///
3262    /// Both spellings the layout can produce, and the twin's OWN trail beside it, which must
3263    /// stay per box or two writers would interleave into one hash chain.
3264    ///
3265    /// **Fails before the fix**: delete the `if rel == AUDIT_REL { return false; }` early
3266    /// return from [`is_per_box_path`] and every assertion in the first two blocks reverses.
3267    #[test]
3268    fn the_audit_trail_is_framed_and_the_twins_own_trail_is_not() {
3269        for p in ["log/audit-chain.jsonl", ".serialdat/log/audit-chain.jsonl", "serialdat/log/audit-chain.jsonl", "./log/audit-chain.jsonl"] {
3270            assert!(!is_per_box_path(p), "{p} is the audit trail and travels with the log");
3271            assert!(!never_framed(p), "{p} must be framable, or a restore loses the audit trail");
3272        }
3273        assert!(FRAMED_FILES.contains(&AUDIT_REL), "a snapshot must carry the audit trail");
3274        // The twin's own decisions are its own: per box, never framed, never sent.
3275        assert!(is_per_box_path(AUDIT_LOCAL_REL) && never_framed(AUDIT_LOCAL_REL));
3276        assert!(!FRAMED_FILES.contains(&AUDIT_LOCAL_REL));
3277    }
3278
3279    /// **The site's hub declaration travels; the machine's own identity never does**
3280    /// (lane T11, 2026-09-21 — the owner's "config must be saved in twin too").
3281    ///
3282    /// `wg/hub.conf` is public site data the installer lays into the data root and that
3283    /// nothing in gunnar writes, so it had no sink call site and the twin never held it. A
3284    /// restored main without it is a box nobody can reach. `wg/identity.key` is the opposite
3285    /// case in every respect: a private key, minted on the machine, naming THIS box to the
3286    /// hub — it must never reach the twin and must never come back on a restore.
3287    ///
3288    /// **Fails before the fix**: drop `|| rel == HUB_CONF_REL` from the early return in
3289    /// [`is_per_box_path`] and the first block reverses; drop the `matches!(rel,
3290    /// WG_IDENTITY_KEY_REL | …)` block and the second does.
3291    #[test]
3292    fn the_hub_declaration_travels_and_the_machines_own_identity_never_does() {
3293        for p in [HUB_CONF_REL, ".serialdat/wg/hub.conf", "./wg/hub.conf"] {
3294            assert!(!never_framed(p), "{p} must travel: a restored main without it is unreachable");
3295        }
3296        assert!(FRAMED_FILES.contains(&HUB_CONF_REL), "a snapshot must carry the hub declaration");
3297        for p in [WG_IDENTITY_KEY_REL, WG_IDENTITY_PUB_REL, HUB_CONF_FRAMED_REL] {
3298            assert!(is_per_box_path(p) && never_framed(p), "{p} is this machine's own and never travels");
3299            assert!(!FRAMED_FILES.contains(&p), "{p} must not be in a snapshot");
3300        }
3301    }
3302
3303    /// **A released log holds no descriptor under its directory** (growbench R5, 2026-09-15:
3304    /// the open segment kept `/data` busy and every runtime growth aborted with EBUSY), refuses
3305    /// every write by name, and after `reacquire` appends where it left off — on the live
3306    /// handle and across a reopen.
3307    #[test]
3308    #[cfg(target_os = "linux")]
3309    fn a_released_log_holds_nothing_under_its_dir_refuses_writes_and_resumes_after_reacquire() {
3310        let root = tempfile::tempdir().unwrap();
3311        let dir = root.path().join("txlog");
3312        let log = Log::open(&dir).unwrap();
3313        let holds = |dir: &Path| -> Vec<PathBuf> {
3314            fs::read_dir("/proc/self/fd").unwrap().filter_map(|e| fs::read_link(e.ok()?.path()).ok()).filter(|p| p.starts_with(dir)).collect()
3315        };
3316        log.append(Kind::Kek, &Meta::Kek {}, b"one").unwrap();
3317        log.append(Kind::Kek, &Meta::Kek {}, b"two").unwrap();
3318        assert!(!holds(&dir).is_empty(), "the writer holds the open segment");
3319        log.release().unwrap();
3320        log.release().unwrap();
3321        assert!(log.is_released());
3322        assert_eq!(holds(&dir), Vec::<PathBuf>::new(), "released: nothing under the dir");
3323        assert!(matches!(log.append(Kind::Kek, &Meta::Kek {}, b"no"), Err(Error::Released(_))));
3324        assert!(matches!(log.begin(None, 1, b"", 1), Err(Error::Released(_))));
3325        assert_eq!(log.read(2).unwrap().blob, b"two", "readers open per call");
3326        log.reacquire().unwrap();
3327        log.reacquire().unwrap();
3328        assert_eq!(log.append(Kind::Kek, &Meta::Kek {}, b"three").unwrap(), 3);
3329        drop(log);
3330        let back = Log::open(&dir).unwrap();
3331        assert_eq!(back.head_seq(), 3);
3332        assert_eq!(back.read(3).unwrap().blob, b"three");
3333    }
3334
3335    /// A segment that moved while released is a different disk: refused, not appended to.
3336    #[test]
3337    fn reacquire_refuses_a_segment_that_changed_length() {
3338        let root = tempfile::tempdir().unwrap();
3339        let dir = root.path().join("txlog");
3340        let log = Log::open(&dir).unwrap();
3341        log.append(Kind::Kek, &Meta::Kek {}, b"one").unwrap();
3342        log.release().unwrap();
3343        OpenOptions::new().append(true).open(seg_path(&dir, 1)).unwrap().write_all(b"junk").unwrap();
3344        assert!(matches!(log.reacquire(), Err(Error::Corrupt { .. })));
3345        assert!(log.is_released(), "still refusing writes");
3346    }
3347
3348    #[test]
3349    fn the_sink_frames_writes_under_a_registered_root_and_nothing_elsewhere() {
3350        let root = tempfile::tempdir().unwrap();
3351        let other = tempfile::tempdir().unwrap();
3352        let log = Arc::new(Log::open(root.path().join("txlog")).unwrap());
3353        sink::register(root.path(), &log);
3354        drop(sink::append_line(&root.path().join("attestations.jsonl"), "{\"a\":1}").unwrap());
3355        drop(sink::replace_file(&root.path().join("webhooks.json"), b"[]", false).unwrap());
3356        drop(sink::append_line(&other.path().join("attestations.jsonl"), "{}").unwrap());
3357        assert_eq!(log.head_seq(), 2);
3358        assert_eq!(log.read(1).unwrap().meta().unwrap(), Meta::Append { log: "attestations.jsonl".into() });
3359        assert_eq!(log.read(2).unwrap().meta().unwrap(), Meta::File { path: "webhooks.json".into(), deleted: false });
3360        drop(log);
3361        // A dropped log unregisters itself: nothing to frame into, no error.
3362        drop(sink::append_line(&root.path().join("attestations.jsonl"), "{}").unwrap());
3363    }
3364
3365    #[test]
3366    fn restart_after_leaves_an_empty_log_whose_next_number_follows_the_snapshot_and_survives_a_reopen() {
3367        let dir = tempfile::tempdir().unwrap();
3368        {
3369            let log = Log::open(dir.path()).unwrap();
3370            log.append(Kind::Pack, &meta("a/b"), b"x").unwrap();
3371            log.restart_after(41).unwrap();
3372            assert_eq!((log.first_seq(), log.head_seq()), (42, 41));
3373            assert!(matches!(log.append_at(43, Kind::Pack as u16, b"{}", b""), Err(Error::OutOfOrder { .. })));
3374            assert_eq!(log.append_at(42, Kind::Pack as u16, &meta("a/b").to_bytes(), b"y").unwrap(), 42);
3375        }
3376        let log = Log::open(dir.path()).unwrap();
3377        assert_eq!((log.first_seq(), log.head_seq()), (42, 42));
3378        assert_eq!(log.read(42).unwrap().blob, b"y");
3379        assert!(matches!(log.read(1), Err(Error::NotRetained { .. })));
3380    }
3381
3382    #[test]
3383    fn a_freeze_waits_for_a_mutation_in_flight_and_holds_new_ones_until_it_ends() {
3384        use std::sync::atomic::AtomicU32;
3385        let dir = tempfile::tempdir().unwrap();
3386        let log = Arc::new(Log::open(dir.path()).unwrap());
3387        let stage = Arc::new(AtomicU32::new(0));
3388        // A mutation in flight on another thread: frame appended, store write not yet done.
3389        let (l, s) = (Arc::clone(&log), Arc::clone(&stage));
3390        let (go_tx, go_rx) = std::sync::mpsc::channel::<()>();
3391        let (in_tx, in_rx) = std::sync::mpsc::channel::<()>();
3392        let writer = std::thread::spawn(move || {
3393            let g = l.mutation();
3394            l.append(Kind::Pack, &meta("a/b"), b"1").unwrap();
3395            in_tx.send(()).unwrap();
3396            go_rx.recv().unwrap();
3397            s.store(1, Ordering::SeqCst); // "the store has the write"
3398            drop(g);
3399        });
3400        in_rx.recv().unwrap();
3401        let (l, s) = (Arc::clone(&log), Arc::clone(&stage));
3402        let (frozen_tx, frozen_rx) = std::sync::mpsc::channel::<()>();
3403        let freezer = std::thread::spawn(move || {
3404            let f = l.freeze();
3405            frozen_tx.send(()).unwrap();
3406            // The freeze returned: the write in flight reached its store first.
3407            assert_eq!(s.load(Ordering::SeqCst), 1, "freeze returned before the mutation in flight ended");
3408            let head = l.head_seq();
3409            std::thread::sleep(std::time::Duration::from_millis(50));
3410            assert_eq!(l.head_seq(), head, "no mutation started while frozen");
3411            s.store(2, Ordering::SeqCst);
3412            drop(f);
3413        });
3414        std::thread::sleep(std::time::Duration::from_millis(30));
3415        go_tx.send(()).unwrap();
3416        writer.join().unwrap();
3417        // A new mutation on this thread, started while frozen, waits for the freeze to end.
3418        frozen_rx.recv().unwrap();
3419        let g = log.mutation();
3420        assert_eq!(stage.load(Ordering::SeqCst), 2, "a mutation started while the log was frozen");
3421        log.append(Kind::Pack, &meta("a/b"), b"2").unwrap();
3422        drop(g);
3423        freezer.join().unwrap();
3424        // Nested guards on one thread count once, and the freezing thread passes its own gate.
3425        let outer = log.mutation();
3426        let inner = log.mutation();
3427        drop(inner);
3428        drop(outer);
3429        let f = log.freeze();
3430        let own = log.mutation();
3431        drop(own);
3432        drop(f);
3433    }
3434}
3435
3436#[cfg(test)]
3437mod spool_tests {
3438    //! **The log's directory holds one thing besides segments, and the log has
3439    //! to know it.**
3440
3441    use super::*;
3442
3443    /// **MEASURED 2026-09-17 on the live appliance, as a kernel panic.**
3444    ///
3445    /// `gunnar-server`'s `txlog_lfs` spools an arriving LFS object beside the
3446    /// log — `<txlog>/spool/<oid>.part` — and removes the FILE when the object
3447    /// lands. Nothing ever removes the DIRECTORY, so `create_dir_all` leaves it
3448    /// there for ever after the first LFS upload the box ever takes.
3449    ///
3450    /// [`Log::open_with`] then refused it: every entry that is not a segment
3451    /// name was `Error::Unexpected`. So one LFS push plus one restart was a
3452    /// box that could never open its transaction log again, and since gunnar is
3453    /// PID 1 the exit came back as
3454    /// `Kernel panic - not syncing: Attempted to kill init!`.
3455    ///
3456    /// It hid for as long as it did because it needs BOTH halves: the appliance
3457    /// had taken LFS objects and simply had not restarted since.
3458    ///
3459    /// The spool is part of this directory's layout, so the layout is what
3460    /// names it — one constant, used by the writer and skipped by the reader,
3461    /// rather than the reader guessing at a name the writer chose.
3462    #[test]
3463    fn the_lfs_spool_directory_is_part_of_the_layout_and_does_not_refuse_the_log() {
3464        let dir = tempfile::tempdir().expect("temp dir");
3465        let log = Log::open(dir.path()).expect("a fresh log opens");
3466        drop(log);
3467        std::fs::create_dir_all(dir.path().join(SPOOL_DIR)).expect("the spool dir");
3468        Log::open(dir.path()).expect("a log whose spool directory exists still opens");
3469    }
3470
3471    /// The skip is for the ONE name the layout describes, and nothing else: a
3472    /// stray file in the log's directory is still a refusal, because the
3473    /// alternative is a reader that shrugs at corruption.
3474    #[test]
3475    fn a_stray_entry_is_still_refused_by_name() {
3476        let dir = tempfile::tempdir().expect("temp dir");
3477        Log::open(dir.path()).expect("a fresh log opens");
3478        std::fs::write(dir.path().join("stray"), b"x").expect("write");
3479        let err = Log::open(dir.path()).expect_err("a stray file is not part of the layout");
3480        assert!(matches!(err, Error::Unexpected(_)), "{err}");
3481    }
3482}