@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.
@@ -3396,6 +3396,7 @@ var AbracadabraProvider = class AbracadabraProvider extends AbracadabraBaseProvi
3396
3396
  return childProvider;
3397
3397
  }
3398
3398
  unloadChild(childId) {
3399
+ if (this.pinnedChildren.has(childId)) return;
3399
3400
  const provider = this.childProviders.get(childId);
3400
3401
  if (provider) {
3401
3402
  provider.destroy();
@@ -3411,6 +3412,7 @@ var AbracadabraProvider = class AbracadabraProvider extends AbracadabraBaseProvi
3411
3412
  */
3412
3413
  pinChild(childId) {
3413
3414
  this.pinnedChildren.add(childId);
3415
+ this.transientChildren.delete(childId);
3414
3416
  }
3415
3417
  /**
3416
3418
  * Unpin a child, allowing LRU eviction to reclaim it when capacity is exceeded.
@@ -19080,13 +19082,6 @@ function projectTreeEntry(entry, expectedType) {
19080
19082
  //#endregion
19081
19083
  //#region packages/provider/src/TreeManager.ts
19082
19084
  /**
19083
- * TreeManager — typed ORM layer over the root Y.Doc's "doc-tree" / "doc-trash"
19084
- * Y.Maps.
19085
- *
19086
- * Extracted from `mcp/tools/tree.ts` and `cli/commands/documents.ts` logic.
19087
- * All tree CRUD operations go through this class.
19088
- */
19089
- /**
19090
19085
  * Stable total order over tree siblings: `order` ascending, then `id`
19091
19086
  * ascending as a deterministic tiebreak. The legacy scan sorted by
19092
19087
  * `order` alone and left ties to insertion/iteration order — a superset
@@ -19099,6 +19094,55 @@ function cmpKey(oa, ia, ob, ib) {
19099
19094
  function cmpEntry(a, b) {
19100
19095
  return cmpKey(a.order ?? 0, a.id, b.order ?? 0, b.id);
19101
19096
  }
19097
+ /**
19098
+ * Deterministic, convergent cycle break for the parent-pointer forest.
19099
+ *
19100
+ * Concurrent offline reparents converge (Yjs LWW on each entry's *own*
19101
+ * `parentId` key) to a state that can contain a directed cycle — e.g. peer A
19102
+ * moves X under Y while peer B moves Y under X yields `X.parent=Y` AND
19103
+ * `Y.parent=X`. Both writes touch different keys so both survive, and the
19104
+ * whole subtree then floats unreachable from root (the "orphaned subtree"
19105
+ * corruption — §3b of OFFLINE-FIRST-TREE-AUDIT). Yjs guarantees every replica
19106
+ * converges to the SAME cyclic map, so any repair that is a pure function of
19107
+ * that map is itself convergent: every replica picks the identical break and
19108
+ * agrees on the result with no coordinator (a lightweight stand-in for the
19109
+ * full Kleppmann fold — it loses one move's intent, but never corrupts).
19110
+ *
19111
+ * Rule: in each cycle, the node with the lexicographically-greatest id is
19112
+ * logically reparented to root. Returns the set of such break-node ids — empty
19113
+ * when the forest is already acyclic (the overwhelmingly common case, so the
19114
+ * hot path adds one O(n) walk and changes no behavior).
19115
+ *
19116
+ * `parentOf` returns the effective *normalized* parent (null = root); a parent
19117
+ * id absent from `ids` is a strand, not a cycle (Tier-0 does not auto-rescue
19118
+ * strands). Self-loops (`parentId === id`) are a degenerate one-node cycle and
19119
+ * are handled. The Rust provider mirrors this exactly (`cycle_break_ids`).
19120
+ */
19121
+ function cycleBreakIds(ids, parentOf) {
19122
+ const idSet = new Set(ids);
19123
+ const done = /* @__PURE__ */ new Set();
19124
+ const breaks = /* @__PURE__ */ new Set();
19125
+ for (const start of idSet) {
19126
+ if (done.has(start)) continue;
19127
+ const path = [];
19128
+ const onPath = /* @__PURE__ */ new Map();
19129
+ let cur = start;
19130
+ while (cur !== null && idSet.has(cur) && !done.has(cur)) {
19131
+ const seenAt = onPath.get(cur);
19132
+ if (seenAt !== void 0) {
19133
+ let maxId = path[seenAt];
19134
+ for (let i = seenAt + 1; i < path.length; i++) if (path[i] > maxId) maxId = path[i];
19135
+ breaks.add(maxId);
19136
+ break;
19137
+ }
19138
+ onPath.set(cur, path.length);
19139
+ path.push(cur);
19140
+ cur = parentOf(cur);
19141
+ }
19142
+ for (const id of path) done.add(id);
19143
+ }
19144
+ return breaks;
19145
+ }
19102
19146
  /** Opaque, dependency-free cursor over the (order,id) sibling order. */
19103
19147
  function encodeCursor(order, id) {
19104
19148
  return encodeURIComponent(JSON.stringify([order ?? 0, id]));
@@ -19158,11 +19202,10 @@ var TreeManager = class {
19158
19202
  rebuildIndex(treeMap) {
19159
19203
  const root = this.dm.rootDocId;
19160
19204
  const byId = /* @__PURE__ */ new Map();
19161
- const childrenByParent = /* @__PURE__ */ new Map();
19162
19205
  treeMap.forEach((raw, id) => {
19163
19206
  const value = toPlain(raw);
19164
19207
  if (typeof value !== "object" || value === null) return;
19165
- const entry = {
19208
+ byId.set(id, {
19166
19209
  id,
19167
19210
  label: value.label || "Untitled",
19168
19211
  parentId: normalizeRootId(value.parentId ?? null, root),
@@ -19171,15 +19214,22 @@ var TreeManager = class {
19171
19214
  meta: value.meta,
19172
19215
  createdAt: value.createdAt,
19173
19216
  updatedAt: value.updatedAt
19174
- };
19175
- byId.set(id, entry);
19217
+ });
19218
+ });
19219
+ const breaks = cycleBreakIds(byId.keys(), (id) => byId.get(id)?.parentId ?? null);
19220
+ for (const id of breaks) {
19221
+ const e = byId.get(id);
19222
+ if (e) e.parentId = null;
19223
+ }
19224
+ const childrenByParent = /* @__PURE__ */ new Map();
19225
+ for (const entry of byId.values()) {
19176
19226
  let bucket = childrenByParent.get(entry.parentId);
19177
19227
  if (!bucket) {
19178
19228
  bucket = [];
19179
19229
  childrenByParent.set(entry.parentId, bucket);
19180
19230
  }
19181
19231
  bucket.push(entry);
19182
- });
19232
+ }
19183
19233
  for (const bucket of childrenByParent.values()) bucket.sort(cmpEntry);
19184
19234
  this._byId = byId;
19185
19235
  this._childrenByParent = childrenByParent;
@@ -19228,10 +19278,17 @@ var TreeManager = class {
19228
19278
  normalizedEntries() {
19229
19279
  if (this.ensureIndex()) return Array.from(this._byId.values());
19230
19280
  const root = this.dm.rootDocId;
19231
- return this.readEntries().map((e) => ({
19281
+ const entries = this.readEntries().map((e) => ({
19232
19282
  ...e,
19233
19283
  parentId: normalizeRootId(e.parentId, root)
19234
19284
  }));
19285
+ const byId = new Map(entries.map((e) => [e.id, e]));
19286
+ const breaks = cycleBreakIds(byId.keys(), (id) => byId.get(id)?.parentId ?? null);
19287
+ for (const id of breaks) {
19288
+ const e = byId.get(id);
19289
+ if (e) e.parentId = null;
19290
+ }
19291
+ return entries;
19235
19292
  }
19236
19293
  /** Get immediate children of a parent (sorted by order). */
19237
19294
  childrenOf(parentId) {
@@ -19479,6 +19536,40 @@ var TreeManager = class {
19479
19536
  });
19480
19537
  });
19481
19538
  }
19539
+ /**
19540
+ * Heal converged move-cycles in the stored tree itself.
19541
+ *
19542
+ * The read paths already present a cycle-free, fully-reachable tree
19543
+ * without writing (see {@link cycleBreakIds}); this *persists* that repair
19544
+ * into the CRDT so the stored structure is acyclic too — useful as a
19545
+ * one-shot on load, or behind a "repair tree" action. Deterministic and
19546
+ * convergent: every replica computes the identical break set, so even
19547
+ * concurrent calls from multiple peers write the same `parentId: null` and
19548
+ * converge. Returns the number of nodes reparented to root (0 when already
19549
+ * acyclic — the common case, where no transaction is opened). Optional —
19550
+ * the always-on read guard already makes the corruption invisible.
19551
+ */
19552
+ repairTree() {
19553
+ const treeMap = this.dm.getTreeMap();
19554
+ const rootDoc = this.dm.rootDocument;
19555
+ if (!treeMap || !rootDoc) throw new Error("Not connected");
19556
+ const root = this.dm.rootDocId;
19557
+ const parentById = /* @__PURE__ */ new Map();
19558
+ treeMap.forEach((raw, id) => {
19559
+ const value = toPlain(raw);
19560
+ if (typeof value !== "object" || value === null) return;
19561
+ parentById.set(id, normalizeRootId(value.parentId ?? null, root));
19562
+ });
19563
+ const breaks = cycleBreakIds(parentById.keys(), (id) => parentById.get(id) ?? null);
19564
+ if (!breaks.size) return 0;
19565
+ rootDoc.transact(() => {
19566
+ for (const id of breaks) if (treeMap.get(id)) patchEntry(treeMap, id, {
19567
+ parentId: null,
19568
+ updatedAt: Date.now()
19569
+ });
19570
+ });
19571
+ return breaks.size;
19572
+ }
19482
19573
  /** Change the page type of a document. */
19483
19574
  changeType(docId, type) {
19484
19575
  const treeMap = this.dm.getTreeMap();