@abraca/dabra 2.37.0 → 2.39.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3366,6 +3366,7 @@ var AbracadabraProvider = class AbracadabraProvider extends AbracadabraBaseProvi
3366
3366
  return childProvider;
3367
3367
  }
3368
3368
  unloadChild(childId) {
3369
+ if (this.pinnedChildren.has(childId)) return;
3369
3370
  const provider = this.childProviders.get(childId);
3370
3371
  if (provider) {
3371
3372
  provider.destroy();
@@ -3381,6 +3382,7 @@ var AbracadabraProvider = class AbracadabraProvider extends AbracadabraBaseProvi
3381
3382
  */
3382
3383
  pinChild(childId) {
3383
3384
  this.pinnedChildren.add(childId);
3385
+ this.transientChildren.delete(childId);
3384
3386
  }
3385
3387
  /**
3386
3388
  * Unpin a child, allowing LRU eviction to reclaim it when capacity is exceeded.
@@ -19039,6 +19041,55 @@ function cmpKey(oa, ia, ob, ib) {
19039
19041
  function cmpEntry(a, b) {
19040
19042
  return cmpKey(a.order ?? 0, a.id, b.order ?? 0, b.id);
19041
19043
  }
19044
+ /**
19045
+ * Deterministic, convergent cycle break for the parent-pointer forest.
19046
+ *
19047
+ * Concurrent offline reparents converge (Yjs LWW on each entry's *own*
19048
+ * `parentId` key) to a state that can contain a directed cycle — e.g. peer A
19049
+ * moves X under Y while peer B moves Y under X yields `X.parent=Y` AND
19050
+ * `Y.parent=X`. Both writes touch different keys so both survive, and the
19051
+ * whole subtree then floats unreachable from root (the "orphaned subtree"
19052
+ * corruption — §3b of OFFLINE-FIRST-TREE-AUDIT). Yjs guarantees every replica
19053
+ * converges to the SAME cyclic map, so any repair that is a pure function of
19054
+ * that map is itself convergent: every replica picks the identical break and
19055
+ * agrees on the result with no coordinator (a lightweight stand-in for the
19056
+ * full Kleppmann fold — it loses one move's intent, but never corrupts).
19057
+ *
19058
+ * Rule: in each cycle, the node with the lexicographically-greatest id is
19059
+ * logically reparented to root. Returns the set of such break-node ids — empty
19060
+ * when the forest is already acyclic (the overwhelmingly common case, so the
19061
+ * hot path adds one O(n) walk and changes no behavior).
19062
+ *
19063
+ * `parentOf` returns the effective *normalized* parent (null = root); a parent
19064
+ * id absent from `ids` is a strand, not a cycle (Tier-0 does not auto-rescue
19065
+ * strands). Self-loops (`parentId === id`) are a degenerate one-node cycle and
19066
+ * are handled. The Rust provider mirrors this exactly (`cycle_break_ids`).
19067
+ */
19068
+ function cycleBreakIds(ids, parentOf) {
19069
+ const idSet = new Set(ids);
19070
+ const done = /* @__PURE__ */ new Set();
19071
+ const breaks = /* @__PURE__ */ new Set();
19072
+ for (const start of idSet) {
19073
+ if (done.has(start)) continue;
19074
+ const path = [];
19075
+ const onPath = /* @__PURE__ */ new Map();
19076
+ let cur = start;
19077
+ while (cur !== null && idSet.has(cur) && !done.has(cur)) {
19078
+ const seenAt = onPath.get(cur);
19079
+ if (seenAt !== void 0) {
19080
+ let maxId = path[seenAt];
19081
+ for (let i = seenAt + 1; i < path.length; i++) if (path[i] > maxId) maxId = path[i];
19082
+ breaks.add(maxId);
19083
+ break;
19084
+ }
19085
+ onPath.set(cur, path.length);
19086
+ path.push(cur);
19087
+ cur = parentOf(cur);
19088
+ }
19089
+ for (const id of path) done.add(id);
19090
+ }
19091
+ return breaks;
19092
+ }
19042
19093
  /** Opaque, dependency-free cursor over the (order,id) sibling order. */
19043
19094
  function encodeCursor(order, id) {
19044
19095
  return encodeURIComponent(JSON.stringify([order ?? 0, id]));
@@ -19098,11 +19149,10 @@ var TreeManager = class {
19098
19149
  rebuildIndex(treeMap) {
19099
19150
  const root = this.dm.rootDocId;
19100
19151
  const byId = /* @__PURE__ */ new Map();
19101
- const childrenByParent = /* @__PURE__ */ new Map();
19102
19152
  treeMap.forEach((raw, id) => {
19103
19153
  const value = toPlain(raw);
19104
19154
  if (typeof value !== "object" || value === null) return;
19105
- const entry = {
19155
+ byId.set(id, {
19106
19156
  id,
19107
19157
  label: value.label || "Untitled",
19108
19158
  parentId: normalizeRootId(value.parentId ?? null, root),
@@ -19111,15 +19161,22 @@ var TreeManager = class {
19111
19161
  meta: value.meta,
19112
19162
  createdAt: value.createdAt,
19113
19163
  updatedAt: value.updatedAt
19114
- };
19115
- byId.set(id, entry);
19164
+ });
19165
+ });
19166
+ const breaks = cycleBreakIds(byId.keys(), (id) => byId.get(id)?.parentId ?? null);
19167
+ for (const id of breaks) {
19168
+ const e = byId.get(id);
19169
+ if (e) e.parentId = null;
19170
+ }
19171
+ const childrenByParent = /* @__PURE__ */ new Map();
19172
+ for (const entry of byId.values()) {
19116
19173
  let bucket = childrenByParent.get(entry.parentId);
19117
19174
  if (!bucket) {
19118
19175
  bucket = [];
19119
19176
  childrenByParent.set(entry.parentId, bucket);
19120
19177
  }
19121
19178
  bucket.push(entry);
19122
- });
19179
+ }
19123
19180
  for (const bucket of childrenByParent.values()) bucket.sort(cmpEntry);
19124
19181
  this._byId = byId;
19125
19182
  this._childrenByParent = childrenByParent;
@@ -19168,10 +19225,17 @@ var TreeManager = class {
19168
19225
  normalizedEntries() {
19169
19226
  if (this.ensureIndex()) return Array.from(this._byId.values());
19170
19227
  const root = this.dm.rootDocId;
19171
- return this.readEntries().map((e) => ({
19228
+ const entries = this.readEntries().map((e) => ({
19172
19229
  ...e,
19173
19230
  parentId: normalizeRootId(e.parentId, root)
19174
19231
  }));
19232
+ const byId = new Map(entries.map((e) => [e.id, e]));
19233
+ const breaks = cycleBreakIds(byId.keys(), (id) => byId.get(id)?.parentId ?? null);
19234
+ for (const id of breaks) {
19235
+ const e = byId.get(id);
19236
+ if (e) e.parentId = null;
19237
+ }
19238
+ return entries;
19175
19239
  }
19176
19240
  /** Get immediate children of a parent (sorted by order). */
19177
19241
  childrenOf(parentId) {
@@ -19419,6 +19483,40 @@ var TreeManager = class {
19419
19483
  });
19420
19484
  });
19421
19485
  }
19486
+ /**
19487
+ * Heal converged move-cycles in the stored tree itself.
19488
+ *
19489
+ * The read paths already present a cycle-free, fully-reachable tree
19490
+ * without writing (see {@link cycleBreakIds}); this *persists* that repair
19491
+ * into the CRDT so the stored structure is acyclic too — useful as a
19492
+ * one-shot on load, or behind a "repair tree" action. Deterministic and
19493
+ * convergent: every replica computes the identical break set, so even
19494
+ * concurrent calls from multiple peers write the same `parentId: null` and
19495
+ * converge. Returns the number of nodes reparented to root (0 when already
19496
+ * acyclic — the common case, where no transaction is opened). Optional —
19497
+ * the always-on read guard already makes the corruption invisible.
19498
+ */
19499
+ repairTree() {
19500
+ const treeMap = this.dm.getTreeMap();
19501
+ const rootDoc = this.dm.rootDocument;
19502
+ if (!treeMap || !rootDoc) throw new Error("Not connected");
19503
+ const root = this.dm.rootDocId;
19504
+ const parentById = /* @__PURE__ */ new Map();
19505
+ treeMap.forEach((raw, id) => {
19506
+ const value = toPlain(raw);
19507
+ if (typeof value !== "object" || value === null) return;
19508
+ parentById.set(id, normalizeRootId(value.parentId ?? null, root));
19509
+ });
19510
+ const breaks = cycleBreakIds(parentById.keys(), (id) => parentById.get(id) ?? null);
19511
+ if (!breaks.size) return 0;
19512
+ rootDoc.transact(() => {
19513
+ for (const id of breaks) if (treeMap.get(id)) patchEntry(treeMap, id, {
19514
+ parentId: null,
19515
+ updatedAt: Date.now()
19516
+ });
19517
+ });
19518
+ return breaks.size;
19519
+ }
19422
19520
  /** Change the page type of a document. */
19423
19521
  changeType(docId, type) {
19424
19522
  const treeMap = this.dm.getTreeMap();