Skip to content

fix(repo): Keep the parent entries when Tree::add splits a subtree - #351

Open
TomPlanche wants to merge 1 commit into
atrium-rs:mainfrom
TomPlanche:fix/repo-mst-split-lost-entries
Open

TomPlanche wants to merge 1 commit into
atrium-rs:mainfrom
TomPlanche:fix/repo-mst-split-lost-entries

Conversation

@TomPlanche

@TomPlanche TomPlanche commented Nov 17, 2026 •

Copy link
Copy Markdown

edit: Add mermaid diagrams

Fixes #343.

Problem

mst::Tree::add can remove keys that are already in the tree. After that, Tree::get returns None for those keys, and Tree::keys does not list them. On main, the reproduction from the issue loses a key in 72 of 300 key sequences.

Cause

When add puts a key in a layer above a subtree, split_subtree splits that subtree into a left part and a right part. split_subtree first splits the lowest node on the path of the key. Then it walks back up the path and splits each parent.

A parent kept its entries on one side only if the child also had entries on that side. If all the entries of the child were on one side of the key, the old code dropped the entries of the parent on the other side.

The smallest case has 3 keys, with D < F < G:

  1. Add D (layer 1) and G (layer 0). The root is [D, subtree(G)].
  2. Add F (layer 2). split_subtree splits the root around F.
  3. The node [G] goes completely to the right of F, so the left part of this node is None.
  4. The old code then made no left part for the root, and D was lost.

The diagram shows the tree before the insertion of F. Then it shows the result on main and the result with the fix.

flowchart LR
  subgraph s1["Before"]
    direction TB
    a1["Root, layer 1<br/>key: D"]
    a2["Layer 0<br/>key: G"]
    a1 -->|"keys after D"| a2
  end

  subgraph s2["On main: D is lost"]
    direction TB
    b1["Root, layer 2<br/>key: F"]
    b2["Layer 1, not written<br/>key: D"]
    b3["Layer 1<br/>no key"]
    b4["Layer 0<br/>key: G"]
    b1 -.->|"keys before F: no link"| b2
    b1 -->|"keys after F"| b3
    b3 --> b4
  end

  subgraph s3["With the fix: D stays"]
    direction TB
    c1["Root, layer 2<br/>key: F"]
    c2["Layer 1<br/>key: D"]
    c3["Layer 1<br/>no key"]
    c4["Layer 0<br/>key: G"]
    c1 -->|"keys before F"| c2
    c1 -->|"keys after F"| c3
    c3 --> c4
  end

  s1 ~~~ s2 ~~~ s3

  classDef added fill:#dafbe1,stroke:#1a7f37,color:#1f2328
  classDef lost fill:#ffebe9,stroke:#cf222e,color:#cf222e,stroke-dasharray:5 5
  class b1,c1 added
  class b2 lost
  linkStyle 1 stroke:#cf222e
Loading

The mirror case loses a key on the right side. Add E (layer 0) and I (layer 1), then add F (layer 2), with E < F < I. The node [E] goes completely to the left of F. The old code then made no right part for the root, and I was lost.

flowchart LR
  subgraph s1["Before"]
    direction TB
    a1["Root, layer 1<br/>key: I"]
    a2["Layer 0<br/>key: E"]
    a1 -->|"keys before I"| a2
  end

  subgraph s2["On main: I is lost"]
    direction TB
    b1["Root, layer 2<br/>key: F"]
    b2["Layer 1<br/>no key"]
    b3["Layer 1, not written<br/>key: I"]
    b4["Layer 0<br/>key: E"]
    b1 -->|"keys before F"| b2
    b1 -.->|"keys after F: no link"| b3
    b2 --> b4
  end

  subgraph s3["With the fix: I stays"]
    direction TB
    c1["Root, layer 2<br/>key: F"]
    c2["Layer 1<br/>no key"]
    c3["Layer 1<br/>key: I"]
    c4["Layer 0<br/>key: E"]
    c1 -->|"keys before F"| c2
    c1 -->|"keys after F"| c3
    c2 --> c4
  end

  s1 ~~~ s2 ~~~ s3

  classDef added fill:#dafbe1,stroke:#1a7f37,color:#1f2328
  classDef lost fill:#ffebe9,stroke:#cf222e,color:#cf222e,stroke-dasharray:5 5
  class b1,c1 added
  class b3 lost
  linkStyle 2 stroke:#cf222e
Loading

Keys that arrive in sorted order, such as TIDs, do not cause this bug. The new key is always the largest key, so each parent has no entries on the right side. This is why the existing tests did not find the bug. Keys that arrive in a random order, such as CIDs, cause the bug after a small number of insertions.

Fix

Each side of a parent now always keeps the entries of that parent. split_subtree adds the link to the child part only if that part exists. A side becomes None only if it has no entries at all.

The change is in the loop of split_subtree that walks back up the path. The public API does not change.

Tests

  • mst_split_keeps_left_entries and mst_split_keeps_right_entries are the two cases with 3 keys, one for each side. Each test also makes sure that a different insertion order gives the same root CID.
  • mst_add_unordered_keys adds 20 CID-shaped keys for each of 50 salts. These are the same keys as in the reproduction from the issue. After each add, the test reads all the earlier keys. Then the test compares the root CID with the root CID of a tree made from the sorted keys.
  • On main, the 3 new tests fail. mst_add_unordered_keys fails on salt-2 after 18 insertions, which is the case from the issue.
  • cargo test -p atrium-repo: 23 unit tests and 1 doc test pass.
  • cargo clippy -p atrium-repo --all-targets --no-deps -- -D warnings and cargo fmt-no-gen --check are clean.
  • I also ran a larger check that is not in the test suite: 30 sequences of 400 keys, with insertions and then deletions. All keys stayed readable, and the root CID did not depend on the insertion order or the deletion order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

atrium-repo: mst::Tree silently loses previously-inserted entries under ordinary sequential use

1 participant