Skip to content

fix(rust_brain): preserve HLC on snapshot restore and gossip replay - #71

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

fix(rust_brain): preserve HLC on snapshot restore and gossip replay#71
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-investigation-ed71

Conversation

@cursor

@cursor cursor Bot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Bug and impact

v0.6.0 added Hybrid Logical Clock (HLC) enforcement on RustBrain.remember(), but three code paths did not round-trip HLC timestamps:

  • restore_from_file() — restored nodes got fresh HLC values instead of snapshot values
  • bulk_write() — ignored hlc in row payloads
  • gossip.receive() — applied gossip events without HLC, causing silent rejection or stale overwrites

Concrete trigger: After disaster-recovery snapshot restore, a valid causal successor write (e.g. hlc=(5000, 11, 'nodeA') after restored (5000, 10, 'nodeA')) raised TimestampRegression because restore assigned a fresh wall-clock HLC. Gossiped updates with correct causal ordering were similarly rejected or could overwrite newer local state.

Impact: Silent data loss after backup restore and broken cross-node memory sync in distributed deployments.

Root cause

MemoryNode.to_dict() serialises HLC, but restore/gossip/bulk paths never read it back. The global _hlc clock was not advanced during restore, so monotonic enforcement compared against wrong timestamps.

Fix

  • Add _parse_hlc() for snapshot/wire normalisation with legacy fallback
  • restore_from_file: restore hlc, update global clock via _hlc.update(), hold lock during restore
  • bulk_write: pass hlc from row payloads
  • gossip.receive: apply hlc, reject stale overwrites, skip missing-hlc updates on existing keys

Validation

  • Reproduced bug before fix: restore assigned fresh HLC; successor write rejected
  • pytest tests/test_hlc_snapshot_gossip.py tests/test_enterprise_backup.py — 13 passed
Open in Web View Automation 

v0.6.0 added HybridLogicalClock enforcement but restore_from_file(),
bulk_write(), and gossip.receive() did not round-trip HLC timestamps.
After disaster-recovery restore, nodes received fresh HLC values, causing
TimestampRegression on valid causal successor writes and silent rejection
of gossiped updates.

- Add _parse_hlc() for snapshot/wire normalisation with legacy fallback
- restore_from_file: restore hlc, update global clock, hold lock
- bulk_write: pass hlc from row payloads
- gossip.receive: apply hlc, reject stale overwrites, skip missing-hlc updates
- Add regression tests for restore round-trip and gossip ordering

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