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

zat.dev/zds

atproto pds · mirror of https://tangled.org/zat.dev/zds

0
0
342 lines · 15.5 KB Raw History

incident: the 7-byte block that took down writes

2026-08-18 — every com.atproto.repo.createRecord on pds.zat.dev returned 500 for two accounts, and com.atproto.sync.getRepo returned 404 for the same two. The cause was a single missing 7-byte block, planted six days earlier by a bug that had already been fixed upstream before it ever ran here.

Two defects compose. Neither is dangerous alone.

defectintroducedfixed
Adelete leaves an emptied subtree node in the tree instead of pruning the pointerzat v0.3.10zat v0.3.19 (f7d0816)
BcollectBlocks never emits an empty node's block, so it is referenced but never storedalwayszat v0.4.3

A created the bad node. B hid it, then turned it into an outage.


symptoms

error failed to serve /xrpc/com.atproto.repo.createRecord: PartialTree
  • createRecord → 500 for any collection, including unknown ones
  • getRepo → 404 RepoNotFound
  • listRepos, getLatestCommit, describeRepo → fine

That split is the first clue. The working endpoints read the records and commits tables. The failing ones walk the MST. The repo metadata was intact; the tree was not.


the tree

birds.place, commit 8730 → 8732, deleting place.birds.sighting/3msvg56t43skv — the sole occupant of the subtree hanging off the speciesPhoto entry.

        BEFORE (8730)                          AFTER (8732, zat v0.3.10)
        root, layer 1                          root, layer 1
        ├── h1 app.bsky.actor.profile/self     ├── h1 app.bsky.actor.profile/self
        │     └── node (4 entries)             │     └── node (4 entries)
        ├── h1 …/place.birds.speciesPhoto      ├── h1 …/place.birds.speciesPhoto
        │     └── node (1 entry)               │     └── {"e":[],"l":null}   ← empty node,
        │          └── place.birds.sighting/…  │                               left behind
        ├── h1 sh.tangled.actor.profile/self   ├── h1 sh.tangled.actor.profile/self
        │     └── node (1 entry)               │     └── node (1 entry)
        └── h1 sh.tangled.repo/birds.place     └── h1 sh.tangled.repo/birds.place

                                               CANONICAL: the pointer should be null,
                                               and the node should not exist at all.

The MST is content-addressed, so a node that should not exist changes every ancestor CID. From 8732 onward the repo served a root no other implementation would compute for that keyset.


defect A — the missing prune

zat v0.3.10, deleteFromNode, the descent path. The entry is removed from the child, the parent is marked dirty, and nothing drops the now-empty child:

// v0.3.10 src/internal/repo/mst.zig:389-393
const child_ref = childAtIndex(node, entryLowerBound(node.entries.items, key));
if (try self.ensureChildNode(child_ref)) |sub| {
    const prev = try self.deleteFromNode(sub, layer - 1, key);
    if (prev != null) node.dirty = true;      // ← and that's all
    return prev;
}

Current zat adds one line, plus a comment that states the stakes exactly:

// HEAD src/internal/repo/mst.zig:382-386
const prev = try self.deleteFromNode(sub, layer - 1, key);
if (prev != null) {
    node.dirty = true;
    pruneIfEmpty(child_ref);                  // ← f7d0816
}

Drop a subtree pointer whose node no longer holds anything. MST nodes are content-addressed, so an emptied node left in place serializes as a real block and changes every ancestor CID — the tree would no longer match the one that never contained the deleted key, breaking commit-proof inversion.

— pruneIfEmpty doc comment, mst.zig:391-394

the subtlety worth keeping

pruneIfEmpty does not prune every childless node:

// mst.zig:395-398
fn pruneIfEmpty(child_ref: *?*Node) void {
    const child = child_ref.* orelse return;
    if (child.entries.items.len == 0 and child.left == null) child_ref.* = null;
}

The and child.left == null matters. A node with no entries but a surviving left child is a legitimate pass-through that bridges a height gap; dropping it would promote the leaf a level and produce a non-canonical root. zat tests both directions — that the emptied subtree is pruned (mst.zig:1992) and that the height-gap pass-through is kept (mst.zig:2079).

So the fully-empty encoding {"e":[],"l":null} — the 7 bytes A2 6165 80 616C F6, CID bafyreie5737gdxlw5i64vzichcalba3z2v5n6icifvx5xytvske7mr3hpm — is canonical in exactly one position: the root of an empty repo. Anywhere else it is corruption.


defect B — the block that never gets written

zds derives the root CID and then collects blocks:

// zds src/storage/store.zig:2140-2141
const data_cid = try tree.rootCid();
try writeMstBlocks(&tree, &mst_blocks);

rootCid() serializes the root, which recurses through nodeCid on every child. nodeCid caches the encoding, clears dirty, and does not emit a block (mst.zig:492-516). That is fine for ordinary nodes: collectNodeBlocks visits them afterwards and emits from the cached encoding. The cache exists precisely to make that safe.

An empty node is the one shape that never reaches the emit path:

// mst.zig:518-519
fn collectNodeBlocks(self: *Mst, node: *Node, …) MstError!void {
    if (isUnloadedStub(node)) return;        // ← bails here

// mst.zig:786-788
fn isUnloadedStub(node: *const Node) bool {
    return !node.dirty and node.cid != null and node.left == null and node.entries.items.len == 0;
}

Once nodeCid has cleaned it, an empty node satisfies every clause: not dirty, has a CID, no left, no entries. It is now structurally indistinguishable from an unloaded stub — and skipping stubs is correct, because a stub's block is already in the store. This one's never was.

zat names the ambiguity in a test title of its own — "An unloaded stub is indistinguishable from an empty node by inspection … emptiness may only be judged after loading" (mst.zig:2102-2108). The delete path was hardened against it. collectNodeBlocks was not.

  nodeCid()          →  empty node: dirty=false, cid set, encoded cached, NO BLOCK EMITTED
  collectNodeBlocks() →  isUnloadedStub() == true  →  return
  repo_blocks         →  block absent
  next tree walk      →  reader.get(cid) == null   →  error.PartialTree

PartialTree propagates out of applyWrites through the else => return err arm of createRecord (src/atproto/repo.zig:49) as a 500 — which is why the collection in the request was irrelevant. The tree could not be loaded at all.


timeline

2026-07-02  zat v0.3.10 released                        ← no pruneIfEmpty
2026-07-25  f7d0816 "mst: prune subtree nodes emptied by delete"
            → shipped in v0.3.19; zds still pinned v0.3.10
2026-08-12  15:13:07  commit 8730  + place.birds.sighting/3msvg56t43skv
            15:13:37  commit 8732  − same key  → EMPTY NODE CREATED (defect A)
2026-08-13  04:54     zds 4cb15aa bumps zat v0.3.10 → v0.3.29
                      defect A fixed — 13h41m too late. the bad node persists.
2026-08-18  05:14 / 05:16  last successful commits on both repos
            ~05:37  first `PartialTree` — a walk finally reaches the empty node
                    → all writes down on zat.dev and birds.place
            <05:59  mitigation: the 7-byte block inserted for both DIDs; writes resume
                    (birds.place commits again at 05:59:39, pruning its own node)
            06:33   zat v0.4.3 deployed (defect B fixed)
            later   zat.dev's three stranded nodes cleared by forward create/delete
                    commits; PDS-wide sweep clean

Dated entries are for birds.place, whose history was replayed commit by commit. zat.dev separately acquired three such nodes under io.atcr.manifest/… by the same mechanism; its history was not replayed, so their dates are not established.

The gap is the interesting part. The bad node sat inert for six days because nothing had to traverse that particular branch. Defect B guarantees the block is missing; it just takes a walk that reaches it to convert that into an outage.


reproduction

Feeding the exact 10-key set and the single delete into zat v0.3.10 reproduces the production commit bit-for-bit:

8732 stored (production)  830b8e14f23565d4029bb079a46be67188d81d1264a4e695301f03b963c99207
8732 rebuilt on v0.3.10   830b8e14f23565d4029bb079a46be67188d81d1264a4e695301f03b963c99207  ✓
canonical (fresh build)   6b68cca937ce9cf58e6047b4fa2d4ad8b831f444e5ebb157f5980bcac027bd5d

The same input on current zat produces canonical. The base tree at 8730 was verified canonical first, so the delete is the sole cause.

A replay through the full zds write path — real SQLite, loadLazy, RepoBlockReader, applyWrites — on current code produces zero missing blocks and zero empty nodes. Defect A is genuinely fixed here.


mitigation

The empty node is a repo-independent constant, so restoring availability needed no commit rewrite, no re-signing, and no firehose replay:

INSERT INTO repo_blocks (did, cid, data, repo_rev)
VALUES (?, 'bafyreie5737gdxlw5i64vzichcalba3z2v5n6icifvx5xytvske7mr3hpm',
        x'A2616580616CF6', NULL);

Applied to both DIDs. Both repos served again immediately.

birds.place then healed itself: its bot's normal create/delete cycle on place.birds.sighting/* lands in exactly that subtree, and the delete now runs through the fixed pruneIfEmpty. Its served root returned to a canonical fresh build without intervention.

zat.dev had no traffic landing in the right place, so it kept its stranded nodes and served a non-canonical root until they were cleared deliberately — see repair below.

sweep at mitigation time

All nine repos, walked from their served roots. Note the empty=1 counts undercount: this sweep deduped by CID, and every empty node shares one CID (zat.dev actually held three).

did:plc:b64lsctzqnzpv6vd4ry3qktw   records=54   missing=0  empty=0
did:plc:siv7zbedip4vcet4v67piibr   records=84   missing=0  empty=0
did:plc:mkqt76xvfgxuemlwlx6ruc3w   records=384  missing=0  empty=1  ← zat.dev, non-canonical
did:plc:zbq37dlgttwafiqadqikhjgk   records=2    missing=0  empty=0
did:plc:whuetkjivofavwkqtmry5vpm   records=1    missing=0  empty=0
did:plc:w4p4bumx22vgnjmctoqjj7sy   records=24   missing=0  empty=0  ← birds.place, healed
did:plc:lm5j7vhcqvmmalcosgujgkrh   records=1    missing=0  empty=0
did:plc:n3qjsl7p5xfgvirr5t7tnvdm   records=0    missing=0  empty=1  ← empty repo, canonical
did:plc:lho2vo4lk3pqey4vl7zlvvvh   records=0    missing=0  empty=1  ← empty repo, canonical

The two records=0 repos have the empty node at their root, which is the one canonical position for it. collectBlocksInto emits it there through a dedicated self.root == null branch (mst.zig:466-479) — which is why those two DIDs had the block stored all along, and why the same 7 bytes were present under other DIDs while missing under the broken ones.


repair: the corrective forward commit

Restoring the block fixes availability but not shape: the empty node is still there, still invalid, still changing every ancestor CID. Clearing it needs a forward commit — not a history rewrite. Past commits are untouched; the repo simply moves to a new commit whose data root is the correctly-shaped tree, which is ordinary appending under the spec's only requirement that rev increase monotonically.

A stranded empty node cannot be pruned by an ordinary delete — pruneIfEmpty only fires after a delete that descends through the pointer and finds something, and there is nothing inside an empty node to find. So it takes a create that lands inside it, then a delete:

create io.atcr.manifest/<rkey>   → the empty node now holds one entry
delete io.atcr.manifest/<rkey>   → node empties, pruneIfEmpty drops the pointer

Two commits, never one batch, because of the applyWrites ordering defect below.

The trigger key must satisfy both MST placement rules: keyHeight(key) must equal the layer of the empty node, and it must sort strictly inside the gap the node occupies. Both bounds share the io.atcr.manifest collection prefix, so the key has to live in that collection — there is no scratch-namespace option. For a node that is its parent's last entry, appending a suffix to that entry's rkey yields the smallest key greater than it, which is always inside the gap.

birds.place never needed this: its bot's own create/delete cycle on place.birds.sighting/* happened to land in the right gap and pruned its node as a side effect.

counting them is harder than it looks

zat.dev held three stranded nodes, not one. Every empty node is the same 7 bytes and therefore the same CID, so a tree walk that memoises visited CIDs reports only the first one and silently hides the rest. The first repair looked like it had moved the node rather than removed it. Count by path, never by CID.


citations

Everything below was read at the versions named, not recalled.

zat (~/tangled.org/zat.dev/zat)

  • f7d08169b9dd5afd34dbb8c6016d42ebafad763e — "mst: prune subtree nodes emptied by delete", 2026-07-25 01:56:38 -0500
  • first tag containing it: v0.3.19. git merge-base --is-ancestor f7d0816 v0.3.10 → NO
  • git show v0.3.10:src/internal/repo/mst.zig | grep -c pruneIfEmpty → 0
  • HEAD src/internal/repo/mst.zig: pruneIfEmpty call 385, definition 395-398; nodeCid 492; collectNodeBlocks 518-519; isUnloadedStub 786-788; collectBlocksInto 466-479
  • tests: empty-root reference CID 1564; subtree prune 1992; height-gap pass-through 2079; stub-vs-empty ambiguity 2102

zds (~/tangled.org/zat.dev/zds)

  • src/storage/store.zig: loadLazy 2111, deleteReturn 2134, rootCid 2140, writeMstBlocks 2141, repoBlockDataFrom (DID-scoped reader) 485
  • src/atproto/repo.zig:48 — the else => return err that surfaces PartialTree as 500
  • build.zig.zon pinned zat v0.3.10 from 29fd5e2 (2026-07-02) until 4cb15aa (2026-08-12 23:54:39 -0500 = 2026-08-13 04:54 UTC), which bumped it to v0.3.29

atproto spec (https://atproto.com/specs/repository, read 2026-08-18)

  • "The top of the tree must not be a an empty node which only points to a sub-tree. Empty intermediate nodes are allowed, as long as they point to a sub-tree which does contain entries." — so {"e":[],"l":null} as a subtree pointer is invalid
  • "An empty repository with no records is represented as a single MST node with an empty array of entries." — the one position where it is valid
  • "The overall structure and shape of the MST is deterministic based on the current key/value content, regardless of the history of insertions and deletions that lead to the current contents."
  • rev "Must increase monotonically" — the repair is an append, not a history rewrite

other implementations

  • reference PDS bluesky-social/atproto @ 02f6e227b: packages/pds/src/actor-store/repo/transactor.ts:193-213
  • tranquil tranquil.farm/tranquil-pds @ 1dc0c40: crates/tranquil-api/src/repo/record/batch.rs:180-190

production (zds-pds, fly.io, /data/zds.sqlite3)

  • birds.place did:plc:w4p4bumx22vgnjmctoqjj7sy, commits 8730 / 8732, revs 3msvg56t43sku / 3msvg63musrkw
  • stored roots bafyreibaq3sxp4vuib5g7xzt63lua7b3okpy53hu4ds6znbfa2wpjhhd6a (8730) and bafyreiedbohbj4rvmxkafg5qpgsgxztrrdmb2eteuttjkma7ao4whsmsa4 (8732)
  • the missing block, verbatim from repo_blocks: A2616580616CF6, 7 bytes