~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zat

atproto utils for zig · mirror of https://tangled.org/zat.dev/zat

0
0
174 lines · 8.9 KB Raw History
# outside in

devlog 016 left off at v0.6.0-alpha.4. a month and eleven releases later zat
is at v0.7.0-alpha.1, and most of what changed in between was found or
written by someone other than me: another indexer's decoder, a relay's
production captures, a contributor's first patches, and a spec that got
published.

## a stable 0.6

v0.6.0 shipped on October 27 as the first stable release of the zig 0.17
line. the library was alpha.7 unchanged; the release moved zttp and
websocket off their prereleases. the alphas before it were mostly about the
network edge.

alpha.5 fixed a TLS bug that only shows up in long-lived processes. zttp
validated certificates against a time taken at startup, so a certificate
issued after a client started could be rejected as not yet valid. transports
now refresh validation time before each new connection.

alpha.6 distrusts the compromised eMudhra roots and intermediates by SPKI
digest, and `zig build trust-probe` checks that a handle and a DID behind
them are refused while `atproto.com` still resolves. Bluesky shipped its own
response to the same compromise in its spaces alpha that week.

alpha.7 added a STAR writer, which comes back at the end of this post.

## first outside patches

on October 29 Kai Lau sent seven commits. `Tid` is now stored in 64 bits as
a packed struct, keeping the same wire encoding, and `Tid.str()` returns a
`[13]u8` value instead of a borrowed slice. `Datetime`
parsing and calendar arithmetic got simpler and `toString()` returns a
`[27]u8`. `car.writeEphemeral` writes a CAR from separate block slices
without concatenating them first, which is the shape a streaming writer
has. base32 encodes complete chunks with vector operations. they shipped as
0.6.1.

0.6.1 also broke on Linux, and that one is on me. one of the commits renamed
`datetime.zig` to `Datetime.zig`. on a Mac the old lowercase import kept
resolving, because the filesystem ignores case. on a case-sensitive one it
didn't exist. 0.6.2 fixed the import, and a build check now rejects any
local import whose case doesn't match the file on disk, even on a
filesystem that would forgive it. 0.6.3-alpha.1 then could not be used as a
dependency at all: `build.zig` imported the new check's script, and the
package didn't ship it. alpha.2 shipped it.

a fix to a fix to a fix is not a great look. what I take from it is that a
check is code too, and it has consumers you aren't running.

## labelers

0.6.3 is the one feature of the month. `LabelClient` follows a labeler's
`com.atproto.label.subscribeLabels` stream with the same reconnect,
backoff, idle watchdog and cursor handling the firehose client has.
`FirehoseClient` is now `SubscriptionClient(RepoStream)` and `LabelClient`
the same machinery over labels. `Label` encodes, signs and verifies
`com.atproto.label.defs#label`, and DID documents expose a labeler's
`#atproto_label` key and `#atproto_labeler` endpoint. a recorded frame from
Bluesky's moderation labeler verifies against the key plc.directory
publishes, and `examples/07_follow_a_labeler.zig` verified 50 of 50 live
labels when it was written.

## a stricter reader

0.6.4 is a one-line kind of fix with a long tail. the firehose encoder
dropped a field whenever it held its default: `since` when null, `rebase`
and `tooBig` when false. `subscribeRepos` lists all three as required. ZDS,
the PDS built on zat, sent commits without them, and nothing noticed, because indigo's generated decoder only reads the keys that are
present. then [hydrant](https://tangled.org/ptr.pet/hydrant), the indexer
behind tangled's relaunch, decoded them strictly, treated the missing fields
as a stream error, and reconnected at the same cursor every time. ptr.pet
spotted it from the other side. the encoder always writes all three now, and a test decodes the frame
as raw CBOR and checks every field the lexicon requires, which the
round-trip tests never could: zat's own decoder accepts a missing field and
a null one alike.

## repeated paths

the next bug came from zlay, a relay built on zat. on November 6 it
rejected a commit from the network with `DuplicatePath`, and a second one
turned up in its shadow stream. both deleted a record and then created one
at the same path, in one commit.

`verifyCommitDiff` checks a commit by undoing its operations against the new
tree and comparing the result with the previous root. it sorted the
operations by path first and refused any path that appeared twice. that is
fine for a set of independent changes and wrong for a sequence: undoing
delete-then-create in path order, with the duplicate thrown away, cannot
reproduce the old tree. zlay had a bounded capture of rejected proofs
running, so both frames were on disk with their signatures intact. an
offline replay against zat's real functions showed reverse message order
verifying both, forward order failing, and a corrupted previous root still
caught.

hydrant already walks the operations backwards. zat does now too. the sort
is gone from the verifier (`normalizeOps` keeps its old behavior for other
callers), and tests cover repeated updates, delete/create and create/delete
on one path, wrong intermediate CIDs, wrong prior roots, and the two
captured frames themselves. the sync and repository specs, as of November
6, describe inversion without forbidding a repeated path, so this is a
compatibility fix, not a policy change. it ships in 0.7.0-alpha.1.

## the end byte

STAR is the archive format atproto spaces use for repository exports and
account migration: a magic, a metadata map, then key/value pairs. zat
implemented the draft (microcosm.blue/star at eb0c078), which ran pairs
until the input ran out. the [published spec](https://dasl.ing/star.html),
dated November 3, puts a control byte before every pair and one after the
last:

```
[ magic | len | meta ] [ 1 | len | key | len | val ] * [ 0 ]
```

nothing may follow the `0x00`, and varints may run to 9 bytes again. the
point is the oldest one in file formats: an archive that ends at EOF can't
tell a finished file from a cut-off one. under the draft a truncated repo
export was caught only when the rebuilt MST root failed to match. now it is
a framing error, before any profile looks at the pairs. Bluesky's spaces
alpha moved to the same framing on November 5. this is the breaking change
that makes the release 0.7.0-alpha.1.

two costs. the magic didn't change, so STAR-lite and STAR both start
`*l\x01\x00`, and an old archive fails partway through with
`error.InvalidControlByte` or `error.EndOfStream` rather than saying it's
the old format. and to know nothing follows the end byte, the reader tries
to read one more byte: free on a buffer, a wait for the sender to close on a
socket. cobalt does the same, and every zat consumer reads archives from
memory today.

most of the work was done by Bryan Newbold's
[cobalt](https://tangled.org/bnewbold.net/cobalt) fixtures, which zat
already ran. cobalt updated its vectors to the published spec on November
4. swapping them in (19 valid, 37 invalid) and changing the reader and
writer made all of them pass. the six tests that broke afterwards were
zat's own, building archives by hand in the old framing.

a zen pass before committing turned up one thing worth writing down.
`finish()` used to only flush, so forgetting it cost nothing with an
in-memory writer. now it writes the end byte, and forgetting it produces an
archive every reader rejects. the obvious guard, an assert in `deinit`,
would be wrong: ZDS returns `error.DuplicateRecordPath` from the middle of
its export loop under `defer writer.deinit()`, a correct error path the
assert would turn into a panic. so the contract lives in the doc comment
and the README. the same pass split a name: `error.TrailingBytes` already
meant extra CBOR after the metadata, so bytes after the end marker are
`error.BytesAfterEnd`.

## measuring it

STAR had no benchmark, so atproto-bench now has `just bench-star`. one
100,000-record repository archive is built once and shared. each
implementation times reading it and rewriting its pairs, and the rewrite has
to match the file byte for byte. zat writes plain STAR at roughly 60 to 67
million records a second and reads at roughly 51 to 54 million, cobalt at
32 to 43 million and 25 to 34 million, and @atproto/star, async-iterator
based, under a million on both. zat's numbers before and after the framing
change are within run-to-run noise; the new framing adds 0.6% to that
archive.

the benchmark also found something. zat's `ordered_keys` option, which
enforces the strictly increasing keys the repository profiles require,
costs more than half of zat's write throughput, because the writer copies
every key to compare it with the next one. that is the next thing to fix.

## next

this alpha is zat's half of the STAR change. ZDS embeds an archive the
Bluesky SDK serialized in the old framing, and has to move to the new one
along with the rest of the November spaces changes. tend reads repository
archives too, but pins an older zat and takes Hubble's exports as CAR, so it
moves when it bumps.