Skip to content

Fix: Keep generated keys unique after removals - #844

Closed
Sonica-B wants to merge 1 commit into
unum-cloud:main-v2from
Sonica-B:fix/auto-key-collisions
Closed

Sonica-B wants to merge 1 commit into
unum-cloud:main-v2from
Sonica-B:fix/auto-key-collisions

Conversation

@Sonica-B

@Sonica-B Sonica-B commented Nov 12, 2026 •

Copy link
Copy Markdown

If you call Index.add(None, vectors) after a remove(), or after adding an explicit key at or above len(index), the new keys collide with keys that are already in the index. A regular index raises RuntimeError: Duplicate keys not allowed in high-level wrappers. A multi=True index raises nothing: it stores the new vector under another vector's key, so later get, count and search results mix up the two. The docstring allows keys=None, and python/scripts/join.py uses it.

This targets main-v2. The rewrite now on main takes explicit keys in insert, so generated keys exist only in the 2.x line, which the current release (2.26.6) ships.

The cause is in _add_to_compiled, which numbers new keys from len(compiled). After a removal, len is smaller than the largest live key, so the range overlaps live keys. This code has been there since 835581a.

import numpy as np
from usearch.index import Index

vectors = np.random.rand(4, 8).astype(np.float32)

index = Index(ndim=8, multi=True)
index.add(None, vectors[:3])  # keys 0, 1, 2
index.remove(0)
print("new key:", index.add(None, vectors[3:]), "count(2):", index.count(2))

index = Index(ndim=8)
index.add(None, vectors[:3])
index.remove(0)
print("new key:", index.add(None, vectors[3:]))

Before (usearch 2.26.6, same index.py as main-v2):

new key: [2] count(2): 2
RuntimeError: Duplicate keys not allowed in high-level wrappers

After:

new key: [3] count(2): 1
new key: [3]
flowchart LR
  subgraph Before
    A1["add(None, v)"] --> B1["keys = arange(len, len + n)"] --> C1["add_many: duplicate error, or a shared key when multi=True"]
  end
  subgraph After
    A2["add(None, v)"] --> B2["keys = arange(len, len + n)"] --> D2{"contains_many(keys).any()?"}
    D2 -- no --> C2["add_many, same keys as before"]
    D2 -- yes --> E2["keys = arange(max key + 1, ...), or ValueError at the uint64 limit"] --> C2
  end
Loading

Changes:

  • python/usearch/index.py: when the len-based range hits a present key, keys continue after the largest key in the index. Append-only use keeps its current keys and pays only one contains_many lookup. If no keys are left below the uint64 limit, a ValueError asks for explicit keys, so the range never wraps around to 0.
  • python/scripts/test_index.py: test_index_generated_keys_are_unique, parametrized over multi and three collision cases (removed key, partly colliding batch, explicit key equal to len), plus the near-uint64 limit case.

Concurrent add(None) calls from several threads can still pick the same keys, both before and after this change. That would need state on the native side, so it is left out here.

Tests, Windows 11, Python 3.11, prebuilt 2.26.6 extension with this index.py:

  • pytest python/scripts/test_index.py python/scripts/test_tooling.py python/scripts/test_sparse.py: 715 passed, 18 skipped. The 8 failures are in test_index_mini_floats_with_numkong and fail the same way without this change (the local numkong has no Tensor).
  • The new test fails 6 of 6 on the base.
  • ruff check python and ruff format --check pass.

`Index.add(None, vectors)` numbered new vectors from `len(index)`.
After a `remove` or after explicit keys at or above `len`, that range
reuses keys already in the index: a regular index raises "Duplicate
keys not allowed in high-level wrappers", and a `multi=True` index
silently files the new vector under another vector's key.

When the default range collides with a present key, keys now continue
past the largest key in the index. The append-only path keeps its
`len`-based keys. A `ValueError` asks for explicit keys when no room
is left below the `uint64` limit.
@Sonica-B
Sonica-B changed the base branch from main-dev to main-v2 November 12, 2026 08:57
@ashvardanian

Copy link
Copy Markdown
Member

@Sonica-B, thanks for the fix! 3.0 has no removal yet, so the 2.x key generation it patches is gone; #846 tracks how removal returns, as an exclusive call that renumbers the survivors, so I'm closing this in its favor.

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.

2 participants