Elffuss BraidHub

utopia-ia/elffuss-braid public

Docs: derived data, history queries, and blame's differences with git

The design describes derived records and the store's derived index, the
three derived functions behind ancestry, log -- <path> and blame, and the
two ways blame differs from git's on purpose: renames are not followed,
and when a change has several shortest line diffs, git and braid can keep
different copies of repeated lines. The format spec gains the derived
index entry in section 7, and a section 8 with the key and the encodings
of graph, changed-paths and blame, each versioned on its own. The README
lists log -- <path>, blame and derive.
Changesete02541b063365e321a7f0e3afb2be5ba3e0a9617ac842e646c78d03c3cb0a44f
AuthorKikoCis <KikoCis@users.noreply.github.com> · 2026-10-16 01:27 +02:00
CommitterKikoCis <KikoCis@users.noreply.github.com> · 2026-10-16 01:27 +02:00
Parents42de601adb21

Browse the files at this changeset

3 files changed

modified README.md +8 −0
@@ -104,6 +104,8 @@
104104 cd /tmp/demo
105105 braid git import /path/to/any/git/repository # every branch and tag
106106 braid log main -n 5
107+braid log main -- src/lib.rs # only the changes to a path
108+braid blame main src/lib.rs # who last changed each line
107109 braid show main --json
108110 braid fsck
109111 ```
@@ -172,6 +174,12 @@
172174 - `braid show` and `braid diff <from> <to>` list the files a change adds,
173175 modifies, deletes or turns into another type (`A`, `M`, `D`, `T`), as
174176 `git diff --name-status` does.
177+- `braid log [rev] -- <path>...` lists the changesets that touched a path,
178+ along first parents, and `braid blame <rev> <path>` names the changeset,
179+ the agent and the date of each line. Both read derived data kept in the
180+ store: generation numbers and skip pointers for ancestry, changed-path
181+ filters, and blame records, computed the first time they are needed.
182+ `braid derive --all` computes them ahead of time, after a large import.
175183
176184 `braid status --help`, `braid land --help` and `braid update --help` describe
177185 every JSON field.
modified docs/design.md +105 −5
@@ -84,6 +84,37 @@
8484 filters, blame, git mappings) is stored as records keyed by their inputs, and
8585 can always be recomputed.
8686
87+### Derived data
88+
89+Everything that is not in a changeset is derived data: computed from
90+changesets, stored, and always recomputable. A derived value is what a named
91+function, at some version, gives for some inputs, and its key is
92+`BLAKE3-derive-key("elffuss-braid 2026 derived v1", function ‖ version ‖ input
93+ids ‖ argument)`. The encodings are in section 8 of the format spec.
94+
95+- **Records.** A value is stored as an ordinary derived object whose payload
96+ is its key followed by its encoding, which each function versions on its
97+ own: a new encoding is a new version, so new keys. The object's id depends
98+ on the value, so the store keeps a *derived index* next to its segments,
99+ `derived/index`: an append-only file of fixed-size entries, a key and a
100+ record id, each with a checksum.
101+- **A cache, checked on use.** The index is never trusted: a record is used
102+ only when its payload starts with the key and its value decodes, and a
103+ value whose record is missing, damaged or unindexed is computed again.
104+ Losing the index, or part of it, costs only time.
105+- **Many processes, no database.** Values are deterministic functions of their
106+ inputs, so two processes that compute the same value store the same bytes
107+ under the same id; storing an object twice does nothing, and two entries
108+ for a key name the same record. Writers append to the index under an
109+ exclusive lock, at a multiple of the entry size, so a crash leaves at worst
110+ an entry that fails its checksum, and readers take no lock. Records are
111+ made durable before an entry names them.
112+- **Lazy.** A value is computed the first time a command needs it, with the
113+ values it depends on, and recorded when the command finishes. `braid
114+ derive` computes them ahead of time, after a large import for example.
115+- The git map is a derived record too, but a single one, found through the
116+ ref `braid/git-map` rather than the index.
117+
87118 ## Landing on a branch
88119
89120 `land` takes a changeset `C` built on base `B` and moves a branch to include it.
@@ -116,6 +147,66 @@
116147 contained it. An acknowledged landing is durable. Readers only see durable
117148 heads.
118149
150+## History
151+
152+Three derived functions answer history queries without walking all of
153+history every time. The engine's `History` reads them through the derived
154+index, computes what is missing, and remembers what it read.
155+
156+- **Commit graph data** (`graph`, per changeset): the parents, the
157+ generation (1 without parents, otherwise 1 more than the largest of the
158+ parents'), the height along first parents, and a skip pointer to the
159+ first-parent ancestor at the height that Bitcoin's block index picks for
160+ its skip pointers. Following skip pointers reaches any first-parent
161+ ancestor in a number of steps that grows with the logarithm of the
162+ distance. With them, `is_ancestor` and `first_parent_chain_contains` find a
163+ first-parent ancestor in logarithmic time, and every other walk stops
164+ below the generation it looks for, since an ancestor always has a smaller
165+ generation. `merge_bases` walks down from both sides in order of
166+ generation, marking what each side reaches; a changeset reached from both
167+ is a merge base unless it is below one already found, and the walk stops
168+ when everything left to visit is. The answers are those of the walking
169+ functions, which landing, merging, workspaces and git sync still use,
170+ since they are given only objects; property tests compare the two on
171+ random histories with octopus and criss-cross merges.
172+- **Changed-path filters** (`changed-paths`, per changeset): the first parent
173+ and a Bloom filter, with git's parameters (10 bits per entry, 7 hashes), of
174+ the paths the change list names and their parent directories. `braid log
175+ [rev] -- <path>...` lists, newest first, the changesets on the first-parent
176+ chain whose change list touches a path or anything below it, as `git log
177+ --first-parent -- <path>` does, without following renames. It reads one
178+ small record per changeset walked, and a change list only where the filter
179+ lets the changeset through: the ones that touched the path, and about one
180+ in a hundred of the others.
181+- **Blame** (`blame`, per changeset and path, where the changeset sets the
182+ file): the changeset that last changed each line, as runs. It is computed
183+ from the blame of the previous version of the file on the first-parent
184+ chain and a line diff between the two versions, imara-diff's Myers
185+ algorithm with its indentation heuristic, which is what git's blame uses by
186+ default; a merge on the first-parent chain owns the lines it brought in.
187+ Blaming a file computes the records of the versions that lack one, back to
188+ the newest that has one, so blaming it again, at the same revision or a
189+ later one, costs one diff per new version. `braid blame <rev> <path>`
190+ prints the short id, the agent or the author, the date, the line number
191+ and the line; `--json` gives the same per line.
192+
193+Blame differs from git's on purpose in two ways, and the differential tests
194+check that it differs in no other:
195+
196+- **Renames are not followed.** Git follows a file across whole-file renames,
197+ and has no switch to turn that off. Braid's history of a path starts where
198+ the path last appeared: a line that git traces back across a rename is the
199+ changeset's that created the path.
200+- **Several shortest diffs.** When a change can be written as more than one
201+ shortest line diff, git's diff and imara-diff's may pick different ones.
202+ Both delete and insert as many lines; they keep different copies of lines
203+ that repeat, such as closing braces and blank lines, and blame gives those
204+ copies to different changesets. On this repository's own history, fewer
205+ than one line in a thousand differs this way. The tests replay git's own
206+ diffs along the same versions, which must give git's blame line for line,
207+ and check that at every version braid's diff inserts as many lines as
208+ git's.
209+
119210 ## Workspaces
120211
121212 - One workspace per agent, all sharing one store. No staging area: tracked
@@ -267,10 +358,16 @@
267358 file system shim that drops unsynced writes.
268359 - **Properties.** Encodings round-trip canonically; incremental tree updates
269360 equal building from scratch; disjoint landings commute; a changeset's change
270- list equals the diff of its trees.
361+ list equals the diff of its trees; ancestry with derived data answers what
362+ the walks answer on random histories; changed-path filters never miss a
363+ path they hold; derived records do not depend on the order they are
364+ computed in.
271365 - **Fuzzing** of every decoder, the git importer, path validation and merges.
272366 - **Differential tests with git as the oracle** on real histories: tree diffs,
273- path history, merge bases and three-way merges. Braid merging cleanly where
367+ path history (`git log --first-parent -- <path>`), merge bases (`git
368+ merge-base --all`), blame (`git blame --first-parent`, with the two
369+ differences above checked as such) and three-way merges, on fixtures, on
370+ random histories and on this repository's own. Braid merging cleanly where
274371 git reports a conflict is treated as a critical bug.
275372 - **Round trips** of real public repositories through import and export, bit
276373 for bit.
@@ -282,9 +379,12 @@
282379 them.
283380
284381 - Commit and land: proportional to the changed paths times the tree depth.
285-- `log -- path`: proportional to the changes that touched the path.
286-- Ancestry and merge base: logarithmic, using skip pointers and generation
287- numbers.
382+- `log -- path`: proportional to the changes that touched the path, plus one
383+ filter check per changeset walked.
384+- Ancestry and merge base: logarithmic for first-parent ancestors, using skip
385+ pointers; otherwise proportional to the changesets above the generation
386+ looked for. Derived data is computed once, the first time.
387+- Blame: one line diff per version of the file not blamed before.
288388 - A lazy sparse clone: proportional to the objects under the requested paths.
289389 - Store size: at most twice a garbage-collected git repository of the same
290390 history.
modified docs/spec/format-v1.md +76 −1
@@ -3,7 +3,8 @@
33 **Status: frozen.** Sections 1 to 5 (ids, the codec, trees, changesets and
44 names) are implemented in `crates/format` and frozen: any change to hashed bytes
55 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.
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.
78
89 Every golden vector below is pinned by the Rust tests and recomputed by
910 [`check_vectors.py`](check_vectors.py), a second implementation written from
@@ -325,6 +326,7 @@
325326 .braid/store/oplog/lock empty; locked exclusively by transactions, shared by readers
326327 .braid/store/oplog/log operation records, append-only
327328 .braid/store/oplog/ckpt.<seq> every ref as of operation <seq>
329+.braid/store/derived/index where the records of derived values are, by key
328330 ```
329331
330332 Segment names are 32 random lowercase hex digits. Files whose names start with
@@ -411,7 +413,80 @@
411413 [32] BLAKE3 hash of everything before it
412414 ```
413415
416+**Derived index entry**, 72 bytes at a multiple of 72:
417+
418+```
419+[32] key | [32] record id | u32 0 | u32 crc32c of the 68 bytes before it
420+```
421+
422+The derived index is a cache of where the records of section 8 are. A writer
423+locks the file exclusively, appends its entries at the first multiple of 72 at
424+or after the end, and unlocks; the records the entries name are durable
425+first. Readers take no lock, skip entries that fail their CRC, and let the last
426+entry of a key count. The file needs no sync: losing entries only means
427+computing values again.
428+
414429 **Planned, not implemented yet:** a binary fuse filter in indexes for fast
415430 misses, merged indexes, one-level zstd deltas against the same path in the
416431 first parent (codec 3), per-repository dictionaries (codec 2), and FastCDC
417432 chunking of blobs of 4 MiB or more.
433+
434+## 8. Derived data (not frozen, versioned per function)
435+
436+Values computed from changesets are stored as derived objects (kind 5), so
437+their ids follow section 2, but their payloads are not part of format v1:
438+each function versions its encoding, and a new encoding is a new version, so
439+new keys. They are implemented in `crates/engine`.
440+
441+**Key** of the value of `function` at `version`:
442+
443+```
444+BLAKE3-derive-key("elffuss-braid 2026 derived v1",
445+ bytes function | uv version | uv n | n × [32] input id | bytes argument)
446+```
447+
448+**Record**: a derived object whose payload is `[32] key` followed by the
449+value. A reader uses a record only when it starts with the key and the value
450+decodes; anything else is computed again.
451+
452+**`graph`, version 1**, of a changeset (the input is its id):
453+
454+```
455+uv n, n × [32] parents, in order
456+uv generation 1 without parents, else 1 + the largest of the parents'
457+uv height 0 without parents, else 1 + the first parent's
458+[32] skip present when height > 0: the first-parent ancestor at
459+ skip_height(height)
460+```
461+
462+`skip_height(h)` is 0 for `h < 2`; for an even `h`, `h` with its lowest set
463+bit cleared; for an odd `h`, 1 + (`h - 1` with its two lowest set bits
464+cleared).
465+
466+**`changed-paths`, version 1**, of a changeset (the input is its id):
467+
468+```
469+u8 1, then [32] the first parent; or 0 without parents
470+uv n the paths the change list names and all their parent
471+ directories, each once
472+ filter ceil(10 n / 8) bytes when 0 < n ≤ 1024, else nothing
473+```
474+
475+The bits of an entry come from `h = BLAKE3-derive-key("elffuss-braid 2026
476+changed-paths v1", path)`: with `a` and `b` its first two little-endian 64-bit
477+words and `m` the number of bits of the filter, its bits are `(a + i·b) mod m`
478+for `i` from 0 to 6, computed modulo 2⁶⁴; bit `j` is bit `j mod 8` of byte
479+`j / 8`. With `n = 0` no path matches; with `n > 1024` every path may.
480+
481+**`blame`, version 1**, of a changeset and a path (the input is the
482+changeset's id, the argument the path), for a changeset whose change list sets
483+the path to a file, an executable or a symlink:
484+
485+```
486+uv k, k × [32] changesets, in the order their first lines come
487+uv r, r × (uv index of the changeset, uv lines) runs, in line order
488+```
489+
490+Runs are not empty, two runs in a row never have the same changeset, and every
491+changeset listed has lines. A line ends after `\n`; a last line without one
492+is a line too.