| 1 | # Braid format v1 |
| 2 | |
| 3 | **Status: frozen.** Sections 1 to 5 (ids, the codec, trees, changesets and |
| 4 | names) are implemented in `crates/format` and frozen: any change to hashed bytes |
| 5 | needs a new format version. Sections 6 and 7 describe the git mapping and the |
| 6 | on-disk storage, which are not hashed and can still change. Section 8 describes |
| 7 | derived data, whose encodings each have a version of their own. |
| 8 | |
| 9 | Every golden vector below is pinned by the Rust tests and recomputed by |
| 10 | [`check_vectors.py`](check_vectors.py), a second implementation written from |
| 11 | this document alone. It needs the reference `b3sum` tool: |
| 12 | |
| 13 | ```bash |
| 14 | python3 docs/spec/check_vectors.py |
| 15 | ``` |
| 16 | |
| 17 | This document covers what is **hashed**: ids, trees, changesets and names. How |
| 18 | the store keeps objects on disk (records, indexes, compression, deltas, chunks) |
| 19 | is not part of any id and has its own, separately versioned, section at the end. |
| 20 | |
| 21 | ## 1. Conventions |
| 22 | |
| 23 | - Integers are little-endian. |
| 24 | - `uv` is an unsigned LEB128 integer of at most 64 bits. Encoders write the |
| 25 | shortest form; decoders reject overlong forms (such as `80 00`) and values |
| 26 | that do not fit in 64 bits. |
| 27 | - `sv` is a signed integer, zigzag-mapped and then written as `uv`. |
| 28 | - `bytes` is a `uv` length followed by that many raw bytes. |
| 29 | - `[32]` is exactly 32 raw bytes: an object id. |
| 30 | - A decoder rejects any bytes left over after the last field. |
| 31 | - Names and paths are byte strings, not text. A path joins names with `/`. |
| 32 | |
| 33 | ## 2. Object ids (implemented) |
| 34 | |
| 35 | An object id is BLAKE3 in key-derivation mode, with one context string per kind: |
| 36 | |
| 37 | | Kind | Code | Context string | |
| 38 | |---|---|---| |
| 39 | | blob | 1 | `elffuss-braid 2026 blob v1` | |
| 40 | | tree | 2 | `elffuss-braid 2026 tree v1` | |
| 41 | | changeset | 3 | `elffuss-braid 2026 changeset v1` | |
| 42 | | chunklist | 4 | `elffuss-braid 2026 chunklist v1` | |
| 43 | | derived | 5 | `elffuss-braid 2026 derived v1` | |
| 44 | |
| 45 | ``` |
| 46 | id = BLAKE3-derive-key(context(kind), payload) 32 bytes |
| 47 | ``` |
| 48 | |
| 49 | - The context separates kinds, so the payload carries no type prefix and can be |
| 50 | hashed while it streams in. |
| 51 | - A **blob id is always the hash of the complete file content**, however the |
| 52 | store keeps it (whole, as a delta, or in chunks). |
| 53 | - The text form is 64 lowercase hex digits; the short form is the first 12. |
| 54 | Parsers accept lowercase only, so equal ids always have equal text. |
| 55 | |
| 56 | Golden vectors (pinned in `crates/format/src/id.rs`): |
| 57 | |
| 58 | | Kind | Payload | Id | |
| 59 | |---|---|---| |
| 60 | | blob | empty | `ff9c212a7ef64fa38f0c411b3a68d523a20fd197ee3ba03fa4a44b7c5e99c958` | |
| 61 | | blob | `hello\n` | `6acd5cdf880a325d2df716699943aae586ad77d54fb2ed0c5a3f53c76fa56f89` | |
| 62 | | tree | empty | `46f1ab000b3145d2e28ff69f7570eb88065d76ef86905bb268699adb9c860c08` | |
| 63 | | changeset | empty | `1be995db81f244ac5ea57f1ba45d9f9da94ad4edd6dbfa0979c16101a34d04ef` | |
| 64 | | chunklist | empty | `f14231be43c0cc73e7c4850ec9d980e6952b56c57f7125d0c39d16874be2ab04` | |
| 65 | | derived | empty | `6f101e23c531e6e3959878774beefb475944b2f0200e52eafb37fd80f97df172` | |
| 66 | |
| 67 | Anyone can check them with the reference [`b3sum`](https://github.com/BLAKE3-team/BLAKE3/tree/master/b3sum) tool: |
| 68 | |
| 69 | ```bash |
| 70 | printf 'hello\n' | b3sum --derive-key "elffuss-braid 2026 blob v1" |
| 71 | ``` |
| 72 | |
| 73 | ## 3. Trees |
| 74 | |
| 75 | A tree describes one directory. Small directories are one flat node. Large ones |
| 76 | are split into a balanced tree of nodes whose boundaries depend only on the |
| 77 | entry names, so the same set of entries always produces the same root id, no |
| 78 | matter in which order it was edited. |
| 79 | |
| 80 | ``` |
| 81 | u8 format = 1 |
| 82 | u8 node_type 0 flat, 1 leaf, 2 internal |
| 83 | |
| 84 | flat | leaf: |
| 85 | uv n |
| 86 | n × entry strictly ascending by name bytes |
| 87 | |
| 88 | entry: |
| 89 | bytes name |
| 90 | u8 kind 0 file, 1 executable, 2 symlink, 3 tree, 4 gitlink |
| 91 | (5 is reserved for conflict entries in a later version) |
| 92 | kind 0–2: [32] blob id, uv size in bytes |
| 93 | kind 3: [32] tree id |
| 94 | kind 4: u8 algorithm (1 = SHA-1, 2 = SHA-256), raw git id (20 or 32 bytes) |
| 95 | |
| 96 | internal: |
| 97 | u8 level 1 for the nodes just above the leaves |
| 98 | uv n |
| 99 | n × (bytes last_name, [32] child id, uv entry_count) |
| 100 | ``` |
| 101 | |
| 102 | - A symlink's target is stored as a blob. |
| 103 | - File sizes live in the tree, so a listing or a virtual file system can report |
| 104 | sizes without fetching any content. |
| 105 | - An empty directory is the tree with zero entries. It can be represented, but |
| 106 | checkouts never create one from scratch. |
| 107 | |
| 108 | **Splitting rule.** A directory with **1,024 entries or fewer must be one flat |
| 109 | node.** A larger one must be split: |
| 110 | |
| 111 | 1. Walk the sorted entries left to right, filling a leaf. After adding entry |
| 112 | `e` to a leaf that now holds `c` entries, end the leaf if |
| 113 | `c ≥ 32` and `u32le(H(0, e.name)[0..4]) < 2³² / 128`, or if `c = 512`. |
| 114 | 2. Build the next level the same way over the children, using each child's |
| 115 | `last_name`, with level numbers 1, 2, …, until one node remains: the root. |
| 116 | |
| 117 | `H(level, name) = BLAKE3-derive-key("elffuss-braid 2026 tree-split v1", u8 level ‖ name)`. |
| 118 | |
| 119 | Boundaries depend only on names, so changing a file's content never moves them, |
| 120 | and an edit never rewrites the leaves before it. Any way of updating a directory |
| 121 | must produce exactly the nodes of building it from scratch. |
| 122 | |
| 123 | A reader checks a split directory by collecting its entries and building them |
| 124 | again: the root must come out identical. A flat node may hold at most 1,024 |
| 125 | entries, a leaf or internal node at least 1 and at most 512, and an internal |
| 126 | node's children must be one level below it. |
| 127 | |
| 128 | Golden vectors: |
| 129 | |
| 130 | | Tree | Id | |
| 131 | |---|---| |
| 132 | | empty (`01 00 00`) | `3136b0f18562ee73b840ea348c54ae3c7e97e4cc3e547ea9dd03fa1cd8b384cf` | |
| 133 | | small: `README.md` (file, `hello\n`), `latest` (symlink to `README.md`), `run.sh` (executable, `#!/bin/sh\n`), `src` (the empty tree), `vendor` (SHA-1 gitlink, twenty `11` bytes) | `a6c6d54bb7663d576e8f7a702e0670927e22098d6c15c10475e06b51dc3d8c1b` | |
| 134 | | large: 5,000 files `f00000` … `f04999`, each the one-byte blob `x`; 30 nodes | `2c88a412664c1fd862c3487e1313a045a20fe4f7bdeaf1bb71e08564edc7dcce` | |
| 135 | |
| 136 | ## 4. Changesets |
| 137 | |
| 138 | A changeset is one change to the repository: its parents, the root tree it |
| 139 | produces, and the list of files that changed relative to its **first** parent. |
| 140 | |
| 141 | ``` |
| 142 | u8 format = 1 |
| 143 | uv parent_count |
| 144 | parent_count × [32] changeset id parent[0] is the base of the change list |
| 145 | [32] root tree id |
| 146 | u8 changes_mode 0 inline, 1 external |
| 147 | mode 0: uv n, n × change |
| 148 | mode 1: [32] blob id of the encoded change list, uv n |
| 149 | author: bytes name, bytes email, sv seconds since the Unix epoch, sv offset in minutes |
| 150 | committer: same layout |
| 151 | bytes message raw bytes, no normalisation |
| 152 | uv extra_count |
| 153 | extra_count × (bytes key, bytes value) order and duplicates preserved |
| 154 | u8 git_origin 0 none; 1 followed by [32] blob id of the raw git commit bytes |
| 155 | |
| 156 | change: |
| 157 | bytes path |
| 158 | u8 op 0 delete, 1 file, 2 executable, 3 symlink, 4 gitlink |
| 159 | op 1–3: [32] blob id, uv size |
| 160 | op 4: u8 algorithm, raw git id |
| 161 | ``` |
| 162 | |
| 163 | Rules: |
| 164 | |
| 165 | - The change list is **exactly** the set of paths whose entry differs from the |
| 166 | first parent's tree (different kind or different id), sorted by path bytes, |
| 167 | without duplicates and without no-op entries. A changeset with no parents is |
| 168 | compared against the empty tree. |
| 169 | - Changes are per file. A directory replaced by a file is a delete for every |
| 170 | file below it plus one set for the new file. |
| 171 | - A symlink cannot take a name that must not be a symlink (section 5). |
| 172 | - The list is stored inline when its encoding (`uv n, n × change`) is at most |
| 173 | 64 KiB, and otherwise as a blob holding exactly that encoding, so a huge |
| 174 | import does not make every history walk read a huge object. A decoder rejects |
| 175 | an inline list over the limit; the store rejects an external list that would |
| 176 | have fit inline. |
| 177 | - **Admission check.** A store accepts a changeset only if applying its change |
| 178 | list to `parent[0]`'s root tree yields exactly `root_tree`, every referenced |
| 179 | object is present, and every parent was already accepted. The check costs |
| 180 | time proportional to the change, not to the repository. |
| 181 | - A merge has two or more parents; its change list is still relative to |
| 182 | `parent[0]`. |
| 183 | - `git_origin` points at the original commit bytes for changesets imported from |
| 184 | git, so export can reproduce the commit bit for bit. The reference is part of |
| 185 | the hash. |
| 186 | |
| 187 | Golden vector: no parents; root = the empty tree; two changes, `README.md` |
| 188 | set to the file `hello\n` (6 bytes) and `src/lib.rs` set to the empty file; |
| 189 | author and committer `Ada Lovelace <ada@example.org>` at second 1,700,000,000 |
| 190 | with offset +60; message `Initial import\n`; no extras; no git origin. |
| 191 | |
| 192 | ``` |
| 193 | 01 00 3136b0f18562ee73b840ea348c54ae3c7e97e4cc3e547ea9dd03fa1cd8b384cf |
| 194 | 00 02 09 524541444d452e6d64 01 6acd5cdf880a325d2df716699943aae586ad77d54fb2ed0c5a3f53c76fa56f89 06 |
| 195 | 0a 7372632f6c69622e7273 01 ff9c212a7ef64fa38f0c411b3a68d523a20fd197ee3ba03fa4a44b7c5e99c958 00 |
| 196 | 0c 416461204c6f76656c616365 0f 616461406578616d706c652e6f7267 80c49fd50c 78 |
| 197 | 0c 416461204c6f76656c616365 0f 616461406578616d706c652e6f7267 80c49fd50c 78 |
| 198 | 0f 496e697469616c20696d706f72740a |
| 199 | 00 |
| 200 | 00 |
| 201 | ``` |
| 202 | |
| 203 | Id: `4f8d49034a95a2938d30d13bee5508100211d04e1c125ef567a0c41c27f777d1`. |
| 204 | |
| 205 | ## 5. Names |
| 206 | |
| 207 | The store is permissive, close to what `git fsck` accepts; checkouts are |
| 208 | strict, per file system. |
| 209 | |
| 210 | A name in a tree is rejected if it: |
| 211 | |
| 212 | - is empty, longer than 4,096 bytes, or contains `/` or NUL; |
| 213 | - is `.` or `..`; |
| 214 | - aliases `.braid` or `.git` once these are removed or folded: the code points |
| 215 | that HFS+ ignores (U+200C–U+200F, U+202A–U+202E, U+206A–U+206F, U+FEFF), |
| 216 | trailing dots and spaces (NTFS), letter case, and Unicode normalisation (NFC); |
| 217 | this includes the 8.3 short forms `BRAID~1` and `GIT~1`; |
| 218 | - is `.gitmodules`, `.gitignore`, `.gitattributes` or `.braidignore` with kind |
| 219 | symlink. |
| 220 | |
| 221 | When writing a working copy, braid also refuses names that would collide on |
| 222 | that file system (letter case, Unicode normalisation), never follows a symlink |
| 223 | while walking or writing, and never runs anything found in repository content. |
| 224 | |
| 225 | ## 6. Git mapping |
| 226 | |
| 227 | - Every imported git object has a two-way mapping between its git id and its |
| 228 | braid id, stored as derived records. |
| 229 | - Imported commits and tags keep their raw bytes (`git_origin`), which preserves |
| 230 | header order, signatures, encodings, unknown headers and odd author lines. |
| 231 | - A git tree that cannot be regenerated exactly from the braid tree (unsorted or |
| 232 | duplicate entries, non-canonical modes such as `100664`) keeps its raw bytes as |
| 233 | well. |
| 234 | - Git sorts a directory entry as if its name ended in `/`; braid sorts by raw |
| 235 | name bytes. Export reorders. |
| 236 | - During import, every commit is exported again and its git id compared with |
| 237 | the original. Any difference stops the import. |
| 238 | |
| 239 | These rules decide the braid ids of imported objects, so two importers that |
| 240 | follow them give the same ids. They are implemented in `crates/git`. |
| 241 | |
| 242 | **Trees.** An entry's kind comes from the file type bits of its octal mode: |
| 243 | `100000` is a file, executable when the owner may execute it (so `100664` is a |
| 244 | file and `100775` an executable); `120000` a symlink, whose blob is the target; |
| 245 | `040000` a directory; `160000` a SHA-1 gitlink. Any other type stops the |
| 246 | import, and so does a name section 5 refuses, checked before anything the tree |
| 247 | names is read. Of entries with the same name, the braid tree keeps the last |
| 248 | written, which is the one a git checkout uses. Braid trees never hold an empty |
| 249 | subdirectory (only a root can be empty), so an entry for an empty directory, or |
| 250 | for a directory holding only empty ones, is left out; changes, being per file, |
| 251 | never mention one. |
| 252 | |
| 253 | The *canonical* git tree of a braid tree lists its entries in git's order with |
| 254 | the modes `100644`, `100755`, `120000`, `40000` and `160000`, naming each |
| 255 | subdirectory by its own canonical git tree. A git tree equal to the canonical |
| 256 | git tree of its braid tree needs nothing more. Any other, including one that |
| 257 | lost duplicate names or empty directories, keeps its raw bytes in a blob, |
| 258 | recorded against its git id, since several git trees can share one braid tree; |
| 259 | a tree that names such a tree is not canonical either. The empty tree is read |
| 260 | as present even when git did not store it. |
| 261 | |
| 262 | **Commits.** The changeset of a commit has: as parents, the changesets of the |
| 263 | `parent` headers that follow the first header, `tree`, in their order; as root, |
| 264 | the braid tree of `tree`; the change list against the first parent; the |
| 265 | message, which is every byte after the first empty line; no extras; and as git |
| 266 | origin the blob with the commit's bytes. Author and committer are the first |
| 267 | `author` and `committer` headers, read as `Name <email> seconds ±HHMM`: |
| 268 | |
| 269 | - the name is everything before the first `<`, without surrounding ASCII |
| 270 | whitespace, and the email everything between it and the next `>`; |
| 271 | - after the `>`, the first word is the time in seconds and the second the |
| 272 | offset, `±HHMM` read as a decimal number and turned into minutes; a missing |
| 273 | or unreadable value is 0; |
| 274 | - a line without a `<` followed by a `>` is all name, with no email and time 0; |
| 275 | a missing header gives an empty signature. |
| 276 | |
| 277 | Annotated tags keep their raw bytes in a blob and record the object they point |
| 278 | to. Replacement objects and grafts are ignored, and objects are read raw: |
| 279 | no filters, no line-ending conversion, no LFS. Shallow and SHA-256 |
| 280 | repositories are refused for now. |
| 281 | |
| 282 | **Commits imported from git.** A changeset with a git origin is exported as |
| 283 | those bytes, after checking that the commit names the changeset's root and its |
| 284 | parents, in order. The tree it names must be the one the map links to the root |
| 285 | or, where the map does not know that tree, the canonical tree of the root; each |
| 286 | parent must be the commit the map links to the parent changeset or, where the |
| 287 | map does not know it, the commit that changeset exports to. So a repository |
| 288 | that received the changesets without the map that imported them, such as a |
| 289 | server, exports them to their original commits too, except where a git tree is |
| 290 | not canonical: only the importing repository's map holds its raw bytes, and |
| 291 | such a commit cannot be exported elsewhere. |
| 292 | |
| 293 | **Commits made in braid.** A changeset without a git origin is exported as |
| 294 | `tree`, one `parent` per parent, `author` and `committer` written as |
| 295 | `Name <email> seconds ±HHMM`, then one header per extra in order, a value with |
| 296 | newlines continuing on lines that start with a space, an empty line and the |
| 297 | message unchanged. A signature holding `<`, `>`, a newline or NUL, an offset |
| 298 | beyond ±99 hours, or an extra that cannot be a header has no git form and is |
| 299 | refused. |
| 300 | |
| 301 | An export records what it generated in the map, in the same tables as an |
| 302 | import: the commit of each changeset, each canonical tree and each blob. An |
| 303 | import that meets one of those commits again, from wherever it went, takes the |
| 304 | changeset it was exported from instead of making a new one with a git origin. |
| 305 | The map stays one to one: a pairing whose commit or changeset (tree, blob) is |
| 306 | already mapped is not recorded, and a braid tree keeps the canonical git tree |
| 307 | it has. Since exports are deterministic, the pairings of any changeset can be |
| 308 | recomputed at any time and recorded again. |
| 309 | |
| 310 | **Map record.** The mapping is one derived record, versioned on its own: |
| 311 | |
| 312 | ``` |
| 313 | "braid-git-map" u8 version = 1 |
| 314 | uv n, n × ([20] commit, [32] changeset) |
| 315 | uv n, n × ([20] tree, [32] braid tree) |
| 316 | uv n, n × ([20] tree, [32] blob with its raw bytes) trees that are not canonical |
| 317 | uv n, n × ([20] blob, [32] braid blob, uv size) |
| 318 | uv n, n × ([20] tag, [32] blob with its raw bytes, [20] target, u8 kind) |
| 319 | uv n, n × (bytes ref name, [20] tag) tag names |
| 320 | ``` |
| 321 | |
| 322 | Each table is sorted by key without duplicates; target kinds are 1 commit, |
| 323 | 2 tree, 3 blob and 4 tag. The reverse mappings are rebuilt on load. |
| 324 | |
| 325 | ## 7. Storage (not hashed, versioned on its own) |
| 326 | |
| 327 | This is the on-disk layout of the local store as implemented in |
| 328 | `crates/store`. It is not hashed: it can change without changing any id. |
| 329 | Fixed-size integers are little-endian; `uv`, `sv` and `bytes` are the codec of |
| 330 | section 1. |
| 331 | |
| 332 | ``` |
| 333 | .braid/store/format object-format=blake3-dk-v1, then storage=<version> |
| 334 | .braid/store/format.lock empty; locked exclusively to raise the storage version |
| 335 | .braid/store/seg/<name>.seg object records, append-only |
| 336 | .braid/store/seg/<name>.jrn journal of a segment that is still open |
| 337 | .braid/store/seg/<name>.idx sorted index of a sealed segment |
| 338 | .braid/store/seg/<name>.midx merged index of several sealed segments |
| 339 | .braid/store/seg/merge.lock empty; locked exclusively while indexes are merged |
| 340 | .braid/store/oplog/lock empty; locked exclusively by transactions, shared by readers |
| 341 | .braid/store/oplog/log operation records, append-only |
| 342 | .braid/store/oplog/ckpt.<seq> every ref as of operation <seq> |
| 343 | .braid/store/derived/index where the records of derived values are, by key |
| 344 | ``` |
| 345 | |
| 346 | Segment and merged index names are 32 random lowercase hex digits. Files whose |
| 347 | names start with `tmp-` are still being written, or were left by a writer that |
| 348 | died; they are never read, and are removed once they are a minute old and |
| 349 | unlocked. |
| 350 | |
| 351 | **Storage version.** The `format` file has two lines: the object ids |
| 352 | (`object-format=blake3-dk-v1`), and the storage version, what a reader must |
| 353 | understand to use the store (`storage=3`). A store without the second line has |
| 354 | storage version 1: records stored whole (codecs 0 and 1), which every version |
| 355 | of braid reads. Version 2 adds deltas (codec 3) and chunked blobs (codec 4). |
| 356 | Version 3 adds indexes of version 2, whose every part carries its own |
| 357 | checksum, and merged indexes, which take the place of the segment indexes they |
| 358 | merge, so that a sealed segment may have no index of its own. A version of |
| 359 | braid refuses a store of a later storage version, and a `format` file with any |
| 360 | line it does not know; versions from before storage versions compare the whole |
| 361 | file with the first line, so they refuse version 2 and later too. |
| 362 | |
| 363 | New stores start at the current version. A store is raised only when a writer |
| 364 | is about to write the first record or index that needs a later version (a |
| 365 | delta or a chunked blob needs 2, an index of version 2 needs 3), or when a |
| 366 | handle finds records that need it already there. The writer takes an |
| 367 | exclusive lock on `format.lock`, reads `format` again, and if it is still |
| 368 | older publishes the new one through a temporary file and a rename: the version |
| 369 | never goes down, and it reaches the disk before anything that needs it. |
| 370 | |
| 371 | A journal entry or an index whose checksum holds but which names an object |
| 372 | kind or a codec the reader does not know, sets a reserved field, or has a later |
| 373 | index version, was written by a newer version of braid: it is not torn or |
| 374 | damaged. The reader refuses the store rather than skip the entry, scan around |
| 375 | the index, or append after them, so nothing is ever written over records it |
| 376 | does not understand. |
| 377 | |
| 378 | **Sharing.** A writer appends only to a segment it holds an exclusive, |
| 379 | non-blocking lock on: an unlocked open segment if there is one, else a new one |
| 380 | made with an exclusive create. The kernel releases the lock when the writer |
| 381 | dies, and the next writer resumes after what the segment's journal lists. |
| 382 | Segments are never truncated, since others may be reading them. A segment is |
| 383 | sealed once it holds 256 MiB, or once its journal lists 8,192 records: given a |
| 384 | sorted index through a temporary file, flushed together with the segment, and |
| 385 | a rename, and its journal removed; it is never written again. A segment is |
| 386 | open while it has a journal and no index of its own; a writer takes over only |
| 387 | such a segment, and never makes a journal for a segment that has none. |
| 388 | |
| 389 | **Lookups.** A reader keeps a map of where objects are only for the open |
| 390 | segments, from their journals, which the record limit keeps short. It looks up |
| 391 | sealed segments in their indexes, binary searching the fan-out bucket of the |
| 392 | id's first byte, and reads an index as lookups need it: the head on first |
| 393 | use, then one bucket at a time. Opening a store reads the open journals and |
| 394 | the heads of the merged indexes, whatever the store holds. On a miss, a reader |
| 395 | reads the directory again once. Of several copies of an object, a lookup takes |
| 396 | the first that serves its purpose; a delta's base must be a copy stored whole, |
| 397 | and a chunk a copy that is not chunked. |
| 398 | |
| 399 | **Merged indexes.** Once 8 segment indexes are there, a writer merges indexes |
| 400 | at its next sync, or at the end of its next transaction, under an exclusive, |
| 401 | non-blocking lock on `seg/merge.lock`, so that one process merges at a time |
| 402 | and the others go on. A merge takes every segment index that no merged index |
| 403 | covers, then the merged indexes that hold at most twice as many entries as it |
| 404 | took so far, smallest first, so that each merged index left holds more than |
| 405 | twice what the new one does, and an entry is written again a number of times |
| 406 | that grows with the logarithm of the store's size. It reads them whole, checking every entry (one found damaged is made |
| 407 | again from its segments' records), and writes one copy of each object, the |
| 408 | best ranked, as a new merged index through a temporary file, a flush and a |
| 409 | rename, then flushes the directory. Only then does it remove what it merged: |
| 410 | first the journals that sealers which died left next to indexes it took, |
| 411 | flushing the directory, so that a merged segment never looks open, then the |
| 412 | indexes, with any that an earlier merge cut short left behind. A merge cut |
| 413 | short at any point leaves a store that reads the same: a segment covered by |
| 414 | several indexes is served by a merged index before its own, and by the merged |
| 415 | index that covers more segments, the newer, before one that covers fewer. |
| 416 | |
| 417 | **Publish order.** Objects are written and flushed before the op log records |
| 418 | an operation that refers to them, so the op log never points at an object that |
| 419 | is not durable. An index is flushed in one batch with the segment it points |
| 420 | into, before its rename, and a file published through a rename never reaches |
| 421 | the disk before its contents. |
| 422 | |
| 423 | **Flushes.** A store is opened in one of two modes. `ordered`, the local |
| 424 | default, may lose the last operations on power loss but never leaves a store |
| 425 | inconsistent; `full`, the server default, makes what a transaction |
| 426 | acknowledges durable before it returns. Flushes come in batches: a sync flushes |
| 427 | every segment and journal it covers together, and a transaction flushes its |
| 428 | objects in one batch, then appends its record and flushes the log. On Apple |
| 429 | systems, where `fsync` keeps no order and `F_FULLFSYNC` has the drive write |
| 430 | out its whole cache, every file of a batch but the last is moved to the drive |
| 431 | with `fsync`, and the last is flushed with `F_BARRIERFSYNC` (`ordered`), a |
| 432 | barrier: the writes issued before it reach the medium before any issued after |
| 433 | it; or with `F_FULLFSYNC` (`full`), which covers the whole batch. Other systems |
| 434 | have no barrier to ask for, and each file of a batch is flushed on its own: |
| 435 | with `fdatasync` (`ordered`) or `fsync` (`full`) on Linux. |
| 436 | |
| 437 | **Object record**, starting at a multiple of 8 bytes: |
| 438 | |
| 439 | ``` |
| 440 | u32 sync = 0xB7A1D5E9 | u8 kind | u8 codec | u16 flags = 0 |
| 441 | u32 stored_len | u64 raw_len | [32] id |
| 442 | [32] dictionary or base id, codecs 2 and 3 only |
| 443 | payload, stored_len bytes |
| 444 | u32 crc32c of everything from the sync word to the end of the payload |
| 445 | zero padding to a multiple of 8 bytes |
| 446 | ``` |
| 447 | |
| 448 | `raw_len` is always the size of the object itself, whatever the codec, so the |
| 449 | kind and size of any object come from its header without reading its payload. |
| 450 | After a crash, the next writer appends at the next multiple of 8 after the end |
| 451 | of the file, so the remains of a cut append stay in place and scans step over |
| 452 | them. |
| 453 | |
| 454 | | Codec | Payload | Used for | |
| 455 | |---|---|---| |
| 456 | | 0 raw | the object as it is (`stored_len == raw_len`) | payloads up to 64 bytes, and larger ones zstd does not make smaller | |
| 457 | | 1 zstd | one zstd frame, level 3 | other objects stored whole | |
| 458 | | 2 | reserved: zstd with a dictionary | not written yet | |
| 459 | | 3 delta | one zstd frame against the raw payload of the base the header names | blobs under 4 MiB, trees and derived objects, given a similar object | |
| 460 | | 4 chunked | the chunk list | blobs only, of 4 MiB or more | |
| 461 | |
| 462 | **Deltas (codec 3).** A writer may say which object a new one is probably |
| 463 | close to. Braid names, for each new tree node and file, the one at the same |
| 464 | path in the parent (for a merged file, the branch's version); for an imported |
| 465 | commit's raw bytes, the first parent's; for the git map, the map it grew from. |
| 466 | A blob under 4 MiB, a tree or a derived object is then compressed with zstd at |
| 467 | level 3, with the raw payload of the base as a reference prefix |
| 468 | (`ZSTD_CCtx_refPrefix`), a window that holds the base and the object together |
| 469 | (at most 2²⁷ bytes, so that any decoder accepts it), and long-distance |
| 470 | matching. Changesets are never deltas: against their first parent, imported |
| 471 | histories gave none that met the cut below. The frame is kept only when it and the 32 |
| 472 | bytes of the base id take at most a quarter of the bytes of the object stored |
| 473 | on its own (codec 0 or 1); otherwise the object is stored on its own. |
| 474 | |
| 475 | The fraction decides how often a version is stored whole again, since a delta |
| 476 | holds every change since its base. Imports of the histories of four public |
| 477 | repositories took, as a ratio to the same histories packed by `git gc |
| 478 | --aggressive`, 2.9 to 3.7 keeping deltas up to a half, 2.6 to 3.3 up to a |
| 479 | third, 2.5 to 3.1 up to a quarter, and 2.6 to 3.1 up to a sixth. |
| 480 | |
| 481 | Depth is always one. A base is stored whole, with codec 0 or 1, and when the |
| 482 | object named as similar is a delta, the new one is made against that delta's |
| 483 | own base. Reading a delta reads its base and applies one delta, never a chain. |
| 484 | A reader that knows several copies of one object, as racing writers can leave, |
| 485 | uses a copy stored whole before a delta, and a delta before a chunked blob, so |
| 486 | that a base always resolves to a copy stored whole when there is one. |
| 487 | |
| 488 | **Chunked blobs (codec 4).** A blob of 4 MiB or more is cut with FastCDC, the |
| 489 | 2020 algorithm with normalisation level 1 and the standard gear table, into |
| 490 | chunks whose boundaries depend only on the content: |
| 491 | |
| 492 | | Blob size | Smallest chunk | Average | Largest | |
| 493 | |---|---|---|---| |
| 494 | | 4 MiB to 256 MiB | 16 KiB | 64 KiB | 256 KiB | |
| 495 | | 256 MiB or more | 256 KiB | 1 MiB | 4 MiB | |
| 496 | |
| 497 | The last chunk may be smaller than the smallest size. A writer that streams a |
| 498 | blob in cuts it with the sizes for the size it expects, such as a file's |
| 499 | length; how a blob is cut never changes its id. Each chunk is a blob of its |
| 500 | own, whose id is the hash of its bytes, so a chunk shared by several files or |
| 501 | versions is stored once. A chunk is stored whole: raw when its first 64 KiB do |
| 502 | not compress below 0.9 of their size, else compressed when that is smaller. A |
| 503 | chunk list never names a chunked blob: a chunk is written whole even when a |
| 504 | chunked copy of the same bytes exists. The blob's own record holds its chunk |
| 505 | list: |
| 506 | |
| 507 | ``` |
| 508 | uv n n ≥ 1 |
| 509 | n × ([32] chunk id | uv chunk size) in order; sizes at least 1 |
| 510 | ``` |
| 511 | |
| 512 | The sizes add up to the record's `raw_len`, and the chunks, in order, make up |
| 513 | the blob. Its id is still the hash of its full content: readers fetch the |
| 514 | chunks one at a time, each checked against its own id and size, and check the |
| 515 | whole against the blob's id, so a blob of any size can be read and written as a |
| 516 | stream. |
| 517 | |
| 518 | **What depends on what.** A delta needs its base, and a chunked blob its |
| 519 | chunks. A writer that finds a copy of an object instead of writing it again |
| 520 | relies on it only together with what it needs, and flushes, at its next sync, |
| 521 | the open segments of other writers that hold any of them, as it does for the |
| 522 | copy itself. There is no garbage collection yet (`braid gc` is planned): no |
| 523 | object is ever removed. Garbage collection must never drop a base while a delta |
| 524 | needs it, nor a chunk while a chunk list names it. |
| 525 | |
| 526 | **Journal entry**, 64 bytes at a multiple of 64, written after its record: |
| 527 | |
| 528 | ``` |
| 529 | [32] id | u64 offset | u32 stored_len | u8 kind | u8 codec | u16 0 |
| 530 | u64 raw_len | u32 record crc | u32 crc32c of the 60 bytes before it |
| 531 | ``` |
| 532 | |
| 533 | Entries that fail their CRC are skipped, and a new lease holder writes after the |
| 534 | last valid one. |
| 535 | |
| 536 | **Index**, version 2, written once when a segment is sealed (`<name>.idx`, |
| 537 | one segment) or when indexes are merged (`<name>.midx`, several): |
| 538 | |
| 539 | ``` |
| 540 | [8] "braididx" | u32 version = 2 | u32 0 | u64 count | u32 segments |
| 541 | u32 crc32c of the 28 bytes before it |
| 542 | segments × ([16] segment name | u64 segment length) |
| 543 | 256 × u32 fan-out: entry b counts the ids whose first byte is at most b |
| 544 | u32 crc32c of the segment table and the fan-out |
| 545 | count × 64-byte entries, ids strictly ascending: |
| 546 | [32] id | u64 offset | u32 stored_len | u8 kind | u8 codec | u16 segment |
| 547 | u64 raw_len | u32 record crc | u32 crc32c of the 60 bytes before it |
| 548 | ``` |
| 549 | |
| 550 | An entry is a journal entry whose reserved field holds the number of its |
| 551 | record's segment in the table: 0 in the index of one segment, and at most |
| 552 | 65,536 segments per index. A segment name is its 32 hex digits as 16 bytes, |
| 553 | and its length the size it had when it was sealed; no entry points past it. |
| 554 | The head (everything up to the second checksum) and every entry carry their |
| 555 | own checksums, so that a reader checks what it reads and nothing more. The |
| 556 | first 32 bytes of every later index version carry their own checksum too, so |
| 557 | that a reader can tell a later version from damage without knowing its |
| 558 | layout. Only entries whose record checks out go in. Of several copies of an |
| 559 | object, a copy stored whole wins over a delta, a delta over a chunked blob, |
| 560 | and the first of equals wins; a merged index keeps one copy of each object. |
| 561 | |
| 562 | Stores of storage versions 1 and 2 hold indexes of version 1, which readers of |
| 563 | version 3 still read, whole, and merge like the others: |
| 564 | |
| 565 | ``` |
| 566 | [8] "braididx" | u32 version = 1 | u32 0 | u64 count | u64 segment length |
| 567 | 256 × u32 fan-out: entry b counts the ids whose first byte is at most b |
| 568 | count × [32] ids, strictly ascending |
| 569 | count × (u64 offset | u32 stored_len | u8 kind | u8 codec | u16 0 |
| 570 | u64 raw_len | u32 record crc | u32 0), in id order |
| 571 | [32] BLAKE3 hash of everything before it |
| 572 | ``` |
| 573 | |
| 574 | A damaged index, of either version, is worked around by scanning the segments |
| 575 | it covers; fsck reports it. A sealed segment that no readable index covers, |
| 576 | when the head of a merged index is damaged, is scanned the same way. |
| 577 | |
| 578 | **Operation record**: |
| 579 | |
| 580 | ``` |
| 581 | u32 magic = 0xB7A10910 | u32 body_len | body | u32 body_len + 16 |
| 582 | u32 crc32c of everything before it |
| 583 | |
| 584 | body: u8 version = 1 | uv seq | sv milliseconds since the Unix epoch |
| 585 | bytes agent | bytes description | uv seq undone, 0 for none |
| 586 | uv n | n × (bytes ref name | u8 which | [32] old | [32] new) |
| 587 | ``` |
| 588 | |
| 589 | `which` has bit 0 set when `old` is present and bit 1 when `new` is; absent |
| 590 | values take no bytes. The length at the end lets readers walk the log |
| 591 | backwards. Sequence numbers start at 1 and have no gaps. A record cut short by a |
| 592 | crash is skipped by readers and cut off by the next transaction, but only when |
| 593 | no whole record follows it. |
| 594 | |
| 595 | **Checkpoint**, written through a temporary file and a rename every |
| 596 | `checkpoint_interval` operations; the newest two are kept: |
| 597 | |
| 598 | ``` |
| 599 | [8] "braidckp" | u32 version = 1 | u32 0 | u64 seq | u64 where record seq ends |
| 600 | uv n | n × (bytes ref name | [32] id), names strictly ascending |
| 601 | [32] BLAKE3 hash of everything before it |
| 602 | ``` |
| 603 | |
| 604 | **Derived index entry**, 72 bytes at a multiple of 72: |
| 605 | |
| 606 | ``` |
| 607 | [32] key | [32] record id | u32 0 | u32 crc32c of the 68 bytes before it |
| 608 | ``` |
| 609 | |
| 610 | The derived index is a cache of where the records of section 8 are. A writer |
| 611 | locks the file exclusively, appends its entries at the first multiple of 72 at |
| 612 | or after the end, and unlocks; the records the entries name are durable |
| 613 | first. Readers take no lock, skip entries that fail their CRC, and let the last |
| 614 | entry of a key count. The file needs no sync: losing entries only means |
| 615 | computing values again. |
| 616 | |
| 617 | **Where the room goes.** Imported, the histories of four public repositories |
| 618 | take 2.5 to 3.1 times what `git gc --aggressive` packs them into, and 6.1 to |
| 619 | 8.3 times without deltas. As a ratio to git's packed size: |
| 620 | |
| 621 | | Part | Share | |
| 622 | |---|---| |
| 623 | | Blobs: file contents, and the raw bytes of imported commits | 1.46 to 1.74 | |
| 624 | | Record headers, checksums and padding | 0.21 to 0.35 | |
| 625 | | Journal and index entries | 0.18 to 0.33 | |
| 626 | | Changesets | 0.14 to 0.35 | |
| 627 | | Trees | 0.25 to 0.31 | |
| 628 | | The git map | 0.13 to 0.22 | |
| 629 | |
| 630 | **Planned, not implemented yet:** |
| 631 | |
| 632 | - *Storage version 3, compact records and indexes.* A record header that |
| 633 | leaves out the id its index entry already holds and writes lengths as `uv`, |
| 634 | and index entries of about half their size. The overhead per object, headers |
| 635 | and entries together, is 0.4 to 0.7 of git's size above. |
| 636 | - *Skip-deltas.* Deltas one level deep hold every change since their base, so |
| 637 | blobs take several times what git's delta chains do. A delta could be made |
| 638 | against a recent version, with a bounded number of deltas to apply on any |
| 639 | read (logarithmic in the number of versions) rather than exactly one. |
| 640 | - *Per-repository dictionaries (codec 2)*, for the small objects deltas do not |
| 641 | help: trees, changesets and raw commits. |
| 642 | - A binary fuse filter in indexes for fast misses. |
| 643 | - Garbage collection (`braid gc`), which must keep every base and chunk that |
| 644 | something still needs. |
| 645 | |
| 646 | ## 8. Derived data (not frozen, versioned per function) |
| 647 | |
| 648 | Values computed from changesets are stored as derived objects (kind 5), so |
| 649 | their ids follow section 2, but their payloads are not part of format v1: |
| 650 | each function versions its encoding, and a new encoding is a new version, so |
| 651 | new keys. They are implemented in `crates/engine`. |
| 652 | |
| 653 | **Key** of the value of `function` at `version`: |
| 654 | |
| 655 | ``` |
| 656 | BLAKE3-derive-key("elffuss-braid 2026 derived v1", |
| 657 | bytes function | uv version | uv n | n × [32] input id | bytes argument) |
| 658 | ``` |
| 659 | |
| 660 | **Record**: a derived object whose payload is `[32] key` followed by the |
| 661 | value. A reader uses a record only when it starts with the key and the value |
| 662 | decodes; anything else is computed again. |
| 663 | |
| 664 | **`graph`, version 1**, of a changeset (the input is its id): |
| 665 | |
| 666 | ``` |
| 667 | uv n, n × [32] parents, in order |
| 668 | uv generation 1 without parents, else 1 + the largest of the parents' |
| 669 | uv height 0 without parents, else 1 + the first parent's |
| 670 | [32] skip present when height > 0: the first-parent ancestor at |
| 671 | skip_height(height) |
| 672 | ``` |
| 673 | |
| 674 | `skip_height(h)` is 0 for `h < 2`; for an even `h`, `h` with its lowest set |
| 675 | bit cleared; for an odd `h`, 1 + (`h - 1` with its two lowest set bits |
| 676 | cleared). |
| 677 | |
| 678 | **`changed-paths`, version 1**, of a changeset (the input is its id): |
| 679 | |
| 680 | ``` |
| 681 | u8 1, then [32] the first parent; or 0 without parents |
| 682 | uv n the paths the change list names and all their parent |
| 683 | directories, each once |
| 684 | filter ceil(10 n / 8) bytes when 0 < n ≤ 1024, else nothing |
| 685 | ``` |
| 686 | |
| 687 | The bits of an entry come from `h = BLAKE3-derive-key("elffuss-braid 2026 |
| 688 | changed-paths v1", path)`: with `a` and `b` its first two little-endian 64-bit |
| 689 | words and `m` the number of bits of the filter, its bits are `(a + i·b) mod m` |
| 690 | for `i` from 0 to 6, computed modulo 2⁶⁴; bit `j` is bit `j mod 8` of byte |
| 691 | `j / 8`. With `n = 0` no path matches; with `n > 1024` every path may. |
| 692 | |
| 693 | **`blame`, version 1**, of a changeset and a path (the input is the |
| 694 | changeset's id, the argument the path), for a changeset whose change list sets |
| 695 | the path to a file, an executable or a symlink: |
| 696 | |
| 697 | ``` |
| 698 | uv k, k × [32] changesets, in the order their first lines come |
| 699 | uv r, r × (uv index of the changeset, uv lines) runs, in line order |
| 700 | ``` |
| 701 | |
| 702 | Runs are not empty, two runs in a row never have the same changeset, and every |
| 703 | changeset listed has lines. A line ends after `\n`; a last line without one |
| 704 | is a line too. |