report: MST delete left empty subtree nodes in place, breaking commit-proof inversion
Status: reproduced and fixed on this branch. Found downstream in zlay via the atmoq relay-conformance corpus; root-caused and fixed here in zat.
Symptom
verifyCommitDiff returned error.PrevDataMismatch for a valid second commit
produced by @atproto/repo. The signature verified, the CAR parsed, op inversion
raised no error — the recomputed root simply did not equal prevData.
Downstream effect in zlay: since 0b85d4b made Sync 1.1 proofs enforcing, a
commit that fails inversion is dropped. Production showed
relay_validation_failed{reason="sync_1_1"} = 1,522,716, with
commit_integrity at 1,522,787 — i.e. essentially all integrity drops were
inversion/prevData. Those counters cannot distinguish an intended
missing-prevData drop from this false rejection, which is why it read as
healthy. Quantifying how much of that 1.5M was this bug still needs doing; see
"open follow-ups".
Root cause
Mst.deleteFromNode recursed into a child subtree, marked the parent dirty, and
returned — but never dropped the child when the delete emptied it. The emptied
node stayed pointed-to, so rootCid() serialized it as a real block and every
ancestor CID changed. The tree therefore did not equal the tree that never
contained the key, which is exactly the equality inversion depends on.
deleteReturn had this trim logic for the root only:
while (self.root) |root| {
if (root.entries.items.len == 0) { ... }
}
Nothing equivalent existed one level down. The TS reference prunes in the recursive case:
const subtree = await prev.deleteRecurse(key)
if ((await subtree.getEntries()).length === 0) {
return this.removeEntry(index - 1) // drop the emptied subtree
}
Fix: pruneIfEmpty after the recursive delete, clearing the child pointer when
the node holds no entries and no left subtree. In TS a node whose only content
is a left subtree has entries.length === 1, so length === 0 there is
equivalent to entries.len == 0 and left == null in our representation — a node
holding only a left subtree is still kept, and the existing root-level trim
handles collapsing it.
Why the existing tests missed it
Three separate blind spots, all of which now have coverage:
-
deleteReturn mutates through lazy stubs and matches eager rootcompares the lazy path against the eager path. Both sharedeleteFromNode, so a wrong root that is consistently wrong passes. It asserts agreement, not correctness. -
interop: mst commit proofs(firehose/commit-proof-fixtures.json) does exercisedels, but every deleted key in those six fixtures is a high key at or above the root layer — handled by theheight >= layerbranch and itsmergeSubtreespath. No fixture deletes a low key that is the sole occupant of its subtree, which is the only shape that triggers this. -
verifyCommitDiff: build tree, serialize partial CAR, verify inversionand zlay'sSync 1.1 accepts commit diff with correct prevDataboth build the fixture with our own encoder, invert an update rather than a create, and ship the whole tree rather than a partial proof. Real firehose traffic is the opposite on all three counts.
The shape that breaks is ordinary: a repo with two records whose keys have
different heights. app.bsky.feed.post/3lsync00001zz has height 1,
...00002zz height 0, so the second lives alone in a subtree hanging off the
first. Inverting its creation must prune that subtree.
Reproduction
Two tests, both of which fail without the fix (verified by reverting the prune):
-
src/internal/repo/mst.zig—deleting the only key in a subtree prunes the empty node. Pure MST, no CAR or signatures: add a low key to a tree, delete it, assert the root returns to its previous CID. Asserts against an independently built tree rather than against another code path. -
src/internal/repo/repo.zig—verifyCommitDiff: interop — real @atproto second commit inverts to prevData. Golden fixture insrc/internal/repo/testdata/atproto-commit-proof.json, generated by@atproto/repo0.8.x: CAR isnewBlocks + relevantBlocks(a genuine partial proof), onecreateop,prevData= the preceding commit's MST root.
Full suite: 482 tests, zig fmt --check src/ build.zig clean. (zig fmt --check .
reports a vendored file under the gitignored zig-pkg/; pre-existing.)
Open follow-ups
- Release + downstream bump. zlay pins zat v0.3.10 by url+hash and needs a released version carrying this fix before its conformance row flips. Not done here: releasing pushes tags.
- Split zlay's counters.
failed_commit_integrity/failed_sync_1_1are bumped by bothvalidatePrevDataPresence(intended drop) and inversion failure (this bug). They should be distinguishable before anyone reads a prevData drop rate as intended behavior. - Upstream the fixture. The atproto interop fixture set has no low-key-alone-in-subtree delete case. Worth contributing, since any implementation with this bug passes the current suite.
mergeSubtreeslooks safe but is unproven.pruneIfEmptyruns after the recursive call regardless of which branch the child took, so a child emptied by the delete-at-own-level path is pruned too.mergeSubtreesitself only constructs a node when both sides are non-null, and non-empty inputs cannot merge into an empty node — but that rests on the non-empty invariant this fix establishes rather than on a test.