Skip to content

fix(rust_brain): preserve HLC on restore and gossip receive#21

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-investigation-9db6
Draft

fix(rust_brain): preserve HLC on restore and gossip receive#21
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-investigation-9db6

Conversation

@cursor

@cursor cursor Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Bug and impact

Critical data loss after snapshot restore. v0.6.0 switched monotonic write enforcement from ts_ns to Hybrid Logical Clock (HLC), but restore_from_file() did not round-trip HLC values from snapshots. After restart, restored nodes received fresh local HLC timestamps. Any legitimate post-crash write or gossip replay carrying the pre-crash HLC was rejected with TimestampRegression, causing silent data loss.

Stale gossip overwrites. gossip.receive() ignored remote HLC timestamps and called remember() without hlc=, assigning a fresh local clock. Late-arriving gossip events could overwrite newer local data on the same key.

Root cause

Two call paths were missed when HLC enforcement landed in v0.6.0 (07a1c75):

  1. restore_from_file() constructed MemoryNode without restoring hlc or advancing the process clock via _hlc.update().
  2. gossip.receive() did not pass hlc, ts_ns, or edges through to remember().

Fix

  • Add _parse_hlc() helper with legacy ts_ns fallback for pre-v0.6.0 snapshots.
  • restore_from_file(): restore HLC per node and call _hlc.update() so the process clock stays ahead.
  • bulk_write(): pass hlc through when present in row dicts.
  • gossip.receive(): apply remote HLC via update_hlc(), reject stale writes, skip missing-HLC updates on existing keys.

Validation

  • Reproduced both bugs before fix (HLC not preserved on restore; stale gossip overwrite accepted).
  • tests/test_enterprise_backup.py::test_restore_preserves_hlc_for_replication
  • tests/test_gossip.py: 4 new HLC ordering tests
  • Full suite: 192 passed, 8 skipped
Open in Web View Automation 

v0.6.0 switched monotonic enforcement from ts_ns to HLC but two call
paths were not updated:

- restore_from_file() dropped HLC from snapshots, assigning fresh clocks
  that blocked legitimate post-restore replication and broke causal order.
- gossip.receive() omitted HLC, so late-arriving events got a fresh local
  timestamp and could overwrite newer data on the same key.

Restore now round-trips HLC (with ts_ns fallback for legacy snapshots) and
advances the process clock. Gossip applies remote HLCs via update_hlc(),
rejects stale writes, and skips unordered updates to existing keys when
no HLC is present.

Regression tests added for both paths.

Co-authored-by: Daniel <DJLougen@users.noreply.github.com>
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.

1 participant