Elffuss BraidHub

utopia-ia/elffuss-braid public

0cba57aa99aa · 10.0 KiBSourceRawPermalink

Braid format v1

Status: draft. Sections marked implemented exist in code and are pinned by golden vectors in the tests. Everything else is the design that milestone 1 implements. The format freezes when milestone 1 closes; after that, any change to hashed bytes needs a new format version.

This document covers what is hashed: ids, trees, changesets and names. How the store keeps objects on disk (records, indexes, compression, deltas, chunks) is not part of any id and has its own, separately versioned, section at the end.

1. Conventions

2. Object ids (implemented)

An object id is BLAKE3 in key-derivation mode, with one context string per kind:

KindCodeContext string
blob1elffuss-braid 2026 blob v1
tree2elffuss-braid 2026 tree v1
changeset3elffuss-braid 2026 changeset v1
chunklist4elffuss-braid 2026 chunklist v1
derived5elffuss-braid 2026 derived v1
id = BLAKE3-derive-key(context(kind), payload)        32 bytes

Golden vectors (pinned in crates/format/src/id.rs):

KindPayloadId
blobemptyff9c212a7ef64fa38f0c411b3a68d523a20fd197ee3ba03fa4a44b7c5e99c958
blobhello\n6acd5cdf880a325d2df716699943aae586ad77d54fb2ed0c5a3f53c76fa56f89
treeempty46f1ab000b3145d2e28ff69f7570eb88065d76ef86905bb268699adb9c860c08
changesetempty1be995db81f244ac5ea57f1ba45d9f9da94ad4edd6dbfa0979c16101a34d04ef
chunklistemptyf14231be43c0cc73e7c4850ec9d980e6952b56c57f7125d0c39d16874be2ab04
derivedempty6f101e23c531e6e3959878774beefb475944b2f0200e52eafb37fd80f97df172

Anyone can check them with the reference b3sum tool:

printf 'hello\n' | b3sum --derive-key "elffuss-braid 2026 blob v1"

3. Trees

A tree describes one directory. Small directories are one flat node. Large ones are split into a balanced tree of nodes whose boundaries depend only on the entry names, so the same set of entries always produces the same root id, no matter in which order it was edited.

u8   format = 1
u8   node_type            0 flat, 1 leaf, 2 internal

flat | leaf:
  uv   n
  n × entry               strictly ascending by name bytes

entry:
  bytes name
  u8    kind              0 file, 1 executable, 2 symlink, 3 tree, 4 gitlink
                          (5 is reserved for conflict entries in a later version)
  kind 0–2:  [32] blob id, uv size in bytes
  kind 3:    [32] tree id
  kind 4:    u8 algorithm (1 = SHA-1, 2 = SHA-256), raw git id (20 or 32 bytes)

internal:
  u8   level              1 for the nodes just above the leaves
  uv   n
  n × (bytes last_name, [32] child id, uv entry_count)

Splitting rule. A directory with 1,024 entries or fewer must be one flat node. A larger one must be split:

  1. Walk the sorted entries left to right, filling a leaf. After adding entry e to a leaf that now holds c entries, end the leaf if c ≥ 32 and u32le(H(0, e.name)[0..4]) < 2³² / 128, or if c = 512.
  2. Build the next level the same way over the children, using each child's last_name, with level numbers 1, 2, …, until one node remains: the root.

H(level, name) = BLAKE3-derive-key("elffuss-braid 2026 tree-split v1", u8 level ‖ name).

Boundaries depend only on names, so changing a file's content never moves them, and an edit only rewrites the nodes on its path. An incremental update must always produce the same bytes as building the directory from scratch; the tests check this for random edit sequences.

4. Changesets

A changeset is one change to the repository: its parents, the root tree it produces, and the list of files that changed relative to its first parent.

u8    format = 1
uv    parent_count
parent_count × [32] changeset id      parent[0] is the base of the change list
[32]  root tree id
u8    changes_mode                    0 inline, 1 external
  mode 0:  uv n, n × change
  mode 1:  [32] blob id of the encoded change list, uv n
author:     bytes name, bytes email, sv seconds since the Unix epoch, sv offset in minutes
committer:  same layout
bytes message                         raw bytes, no normalisation
uv    extra_count
extra_count × (bytes key, bytes value)    order and duplicates preserved
u8    git_origin                      0 none; 1 followed by [32] blob id of the raw git commit bytes

change:
  bytes path
  u8    op                            0 delete, 1 file, 2 executable, 3 symlink, 4 gitlink
  op 1–3:  [32] blob id, uv size
  op 4:    u8 algorithm, raw git id

Rules:

5. Names

The store is permissive, close to what git fsck accepts; checkouts are strict, per file system.

A name in a tree is rejected if it:

When writing a working copy, braid also refuses names that would collide on that file system (letter case, Unicode normalisation), never follows a symlink while walking or writing, and never runs anything found in repository content.

6. Git mapping

7. Storage (not hashed, versioned on its own)

This section describes the planned on-disk layout of the local store. It can change without changing any id.

.braid/store/format                 object-format=blake3-dk-v1
.braid/store/seg/<uuid>.seg         object records, append-only
.braid/store/seg/<uuid>.jrn         live journal of a segment that is still open
.braid/store/seg/<uuid>.idx         sorted index of a sealed segment
.braid/store/midx/<id>.midx         merged index written by compaction
.braid/store/oplog/                 lock, log and checkpoints for refs and operations
.braid/ws/<name>/                   one workspace per agent

Object record (8-byte aligned):

u32 sync = 0xB7A1D5E9 | u8 kind | u8 codec (0 raw, 1 zstd, 2 zstd + dictionary, 3 zstd + reference)
u16 flags | u32 stored_len | u64 raw_len | [32] id
codec 2–3: [32] dictionary or reference object id
payload | u32 crc32c over everything from sync to the end of the payload

Journal entry, 64 bytes, each with its own CRC: [32] id, u64 offset, u32 stored_len, u8 kind, u8 codec, u16 0, u64 raw_len, u32 record crc, u32 entry crc.

Index (.idx, .midx): header, a fan-out table, sorted ids, locations, kinds, a binary fuse filter for fast misses, and a footer with offsets and a BLAKE3 checksum.

Storage policy (tunable at any time):