loro icon indicating copy to clipboard operation
loro copied to clipboard

loro-repo: tombstone clear updates can be dropped when no prior tombstoneState entry

Open lodystage opened this issue 10 months ago • 0 comments

Summary

In apps consuming loro-repo (tested with [email protected], @loro-dev/[email protected]), we sometimes observe that tombstone clears (i.e. a ts/<docId> entry being removed / becoming undefined) do not produce any observable update via the LoroRepo watcher/event surface. Downstream UIs can stay in a stale "deleted/offline" state until a full refresh/rescan.

This is especially visible in scenarios like: a client has a local cache marking a doc as tombstoned, reconnects, and the server snapshot indicates the tombstone is already cleared.

Expected

When the effective tombstone state for a doc changes from "tombstoned" -> "not tombstoned", consumers should be able to observe an update (e.g. via repo.watch(...) / soft-delete events) without requiring a full refresh/rescan.

Actual

A tombstone clear can be ignored if MetadataManager has no prior tombstone entry for that doc. That means consumers that maintain their own derived cache (or have stale derived state after a reload) won't get a signal to clear it.

Suspected Root Cause

In MetadataManager.emitSoftDeleted, there is a guard that returns early when clearing a tombstone but the internal tombstoneState has no previous entry:

// pseudo-excerpt
if (!hadPrev && deletedAtMs === undefined) return;

This seems to be reachable during hydration / snapshot application where the system learns "not deleted" without having ever recorded a prior "deleted" entry in this session.

Minimal Repro (high-level)

  1. Client A sets ts/<docId> to a finite number (soft-delete).
  2. Client B (offline or with stale derived state) records that doc as tombstoned in its own cache.
  3. Client A clears the tombstone (ts/<docId> removed / undefined).
  4. Client B reconnects and hydrates from a snapshot that reflects the cleared tombstone.
  5. Client B never observes a tombstone-clear update from the LoroRepo event surface (unless it performs a full refresh/rescan).

Impact (real app symptom)

Downstream UI state derived from doc meta can remain stale until refresh. Example: an "agent options" list for a newly activated machine does not appear until page refresh, even though other meta-room updates are realtime.

Suggested Fix Directions

  • Consider emitting a tombstone-clear signal even when hadPrev === false (i.e. treat the clear event as meaningful by itself).
  • Alternatively, ensure tombstoneState is deterministically initialized from the current ts/* snapshot before any live events are applied, so hadPrev accurately reflects reality.
  • Or, provide a lower-level dedicated event for ts/<docId> key changes so consumers can reliably maintain derived caches.

If there is a better canonical way for consumers to subscribe to tombstone changes (including clears) without subscribing directly to the underlying Flock keyspace, please advise.

lodystage avatar Jan 15 '26 10:01 lodystage