@mmstack/primitives 21.7.0 → 21.8.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.
@@ -4052,7 +4052,7 @@ function isOpaque(value) {
4052
4052
  function isWritableSignal(value) {
4053
4053
  return isWritableSignal$2(value);
4054
4054
  }
4055
- function isRecord(value) {
4055
+ function isRecord$1(value) {
4056
4056
  if (value === null || typeof value !== 'object' || isOpaque(value))
4057
4057
  return false;
4058
4058
  const proto = Object.getPrototypeOf(value);
@@ -4068,7 +4068,7 @@ function isLeafValue(value, vivifyEnabled) {
4068
4068
  return !vivifyEnabled;
4069
4069
  if (isOpaque(value))
4070
4070
  return true; // opaque always wins — even arrays
4071
- return !Array.isArray(value) && !isRecord(value);
4071
+ return !Array.isArray(value) && !isRecord$1(value);
4072
4072
  }
4073
4073
  /**
4074
4074
  * @internal
@@ -4081,7 +4081,7 @@ function resolveVivify(sample, option) {
4081
4081
  return false;
4082
4082
  if (Array.isArray(sample))
4083
4083
  return 'array';
4084
- if (isRecord(sample))
4084
+ if (isRecord$1(sample))
4085
4085
  return 'object';
4086
4086
  return 'auto';
4087
4087
  }
@@ -4106,7 +4106,7 @@ function createFallbackOnChange(target, prop, vivifyFn, isMutableSource) {
4106
4106
  ? container
4107
4107
  : Array.isArray(container)
4108
4108
  ? container.slice()
4109
- : isRecord(container)
4109
+ : isRecord$1(container)
4110
4110
  ? { ...container }
4111
4111
  : container; // non-plain leaf (Date/class instance): legacy in-place attempt
4112
4112
  try {
@@ -4243,7 +4243,7 @@ const isPlainArray$1 = (v) => Array.isArray(v) && !isOpaque(v);
4243
4243
  function diffNode(prev, next, path, ops) {
4244
4244
  if (Object.is(prev, next))
4245
4245
  return;
4246
- if (isRecord(prev) && isRecord(next)) {
4246
+ if (isRecord$1(prev) && isRecord$1(next)) {
4247
4247
  for (const key of Object.keys(prev)) {
4248
4248
  if (!Object.hasOwn(next, key))
4249
4249
  ops.push({ kind: 'delete', path: [...path, key], prev: prev[key] });
@@ -4276,9 +4276,11 @@ function diffNode(prev, next, path, ops) {
4276
4276
  /** Immutably applies one op along its path, vivifying missing containers `'auto'`-style. */
4277
4277
  function applyAt(container, path, idx, op) {
4278
4278
  const seg = path[idx];
4279
+ if (seg === '__proto__')
4280
+ return container;
4279
4281
  const base = isPlainArray$1(container)
4280
4282
  ? container.slice()
4281
- : isRecord(container)
4283
+ : isRecord$1(container)
4282
4284
  ? { ...container }
4283
4285
  : typeof seg === 'number'
4284
4286
  ? []
@@ -4307,6 +4309,8 @@ function applyOps(root, ops) {
4307
4309
  const list = Array.isArray(ops) ? ops : ops.ops;
4308
4310
  let next = root;
4309
4311
  for (const op of list) {
4312
+ if (op.kind === 'clear')
4313
+ continue; // register retirement, never a value change
4310
4314
  if (op.path.length === 0) {
4311
4315
  if (op.kind === 'set')
4312
4316
  next = op.next;
@@ -4320,7 +4324,8 @@ function applyOps(root, ops) {
4320
4324
  * Pure reference-pruned structural diff of two roots into minimal ops (the emission core of
4321
4325
  * {@link opLog}, exported so code outside a log can produce a batch — e.g. diffing a scratch
4322
4326
  * draft against a replica's current value to route a write to its owner). Trusts the
4323
- * copy-on-write contract: an untouched subtree that kept its reference is skipped.
4327
+ * copy-on-write contract: an untouched subtree that kept its reference is skipped. Emits only
4328
+ * `set` and `delete`; `clear` is an emission-layer intent, never a diff product.
4324
4329
  */
4325
4330
  function diffOps(prev, next) {
4326
4331
  const ops = [];
@@ -4331,13 +4336,17 @@ function diffOps(prev, next) {
4331
4336
  * Inverts a batch for undo: reversed order, `set`↔its own inverse (an add — a `set` with no
4332
4337
  * `prev` — inverts to a `delete`; a `delete` inverts to a `set` restoring `prev`). Feed the
4333
4338
  * result to {@link OpLog.apply}. Requires the ops' `prev`s, which in-memory batches always
4334
- * carry a wire-serialized batch that stripped them is not invertible.
4339
+ * carry (a wire-serialized batch that stripped them is not invertible). A `clear` is skipped:
4340
+ * it never changed a value, so it has no independent inverse (the accompanying subtree `set`'s
4341
+ * `prev` subsumes restoration).
4335
4342
  */
4336
4343
  function invertBatch(batch) {
4337
4344
  const ops = Array.isArray(batch) ? batch : batch.ops;
4338
4345
  const inverted = [];
4339
4346
  for (let i = ops.length - 1; i >= 0; i--) {
4340
4347
  const op = ops[i];
4348
+ if (op.kind === 'clear')
4349
+ continue;
4341
4350
  if (op.kind === 'delete') {
4342
4351
  inverted.push({
4343
4352
  kind: 'set',
@@ -4497,7 +4506,7 @@ function buildChildNode(target, prop, isMutableSource, options) {
4497
4506
  const value = untracked(target);
4498
4507
  const nodeVivify = resolveVivify(value, options.vivify);
4499
4508
  const vivifyFn = createVivify(nodeVivify);
4500
- const equalFn = isMutableSource && (isRecord(value) || Array.isArray(value))
4509
+ const equalFn = isMutableSource && (isRecord$1(value) || Array.isArray(value))
4501
4510
  ? mutableChildEqual
4502
4511
  : undefined;
4503
4512
  const computation = derived(target, {
@@ -4546,7 +4555,7 @@ function toStore(source, { injector, vivify = false, noUnionLeaves = false, ...r
4546
4555
  const v = source();
4547
4556
  if (Array.isArray(v) && !isOpaque(v))
4548
4557
  return 'array';
4549
- if (isRecord(v))
4558
+ if (isRecord$1(v))
4550
4559
  return 'record';
4551
4560
  return 'primitive';
4552
4561
  }, ...(ngDevMode ? [{ debugName: "kind" }] : /* istanbul ignore next */ []));
@@ -4594,7 +4603,7 @@ function toStore(source, { injector, vivify = false, noUnionLeaves = false, ...r
4594
4603
  arr[len] = 'length';
4595
4604
  return arr;
4596
4605
  }
4597
- if (!isRecord(v))
4606
+ if (!isRecord$1(v))
4598
4607
  return [];
4599
4608
  return Reflect.ownKeys(v);
4600
4609
  },
@@ -4612,7 +4621,7 @@ function toStore(source, { injector, vivify = false, noUnionLeaves = false, ...r
4612
4621
  return { enumerable: true, configurable: true };
4613
4622
  return;
4614
4623
  }
4615
- if (!isRecord(v) || !(prop in v))
4624
+ if (!isRecord$1(v) || !(prop in v))
4616
4625
  return;
4617
4626
  return { enumerable: true, configurable: true };
4618
4627
  },
@@ -4951,23 +4960,110 @@ function createHlcClock(now = Date.now) {
4951
4960
  };
4952
4961
  }
4953
4962
 
4954
- const OP_PROTO_VERSION = 1;
4963
+ /**
4964
+ * Wire protocol version. Version 2 ops carry `cites` + `epoch` (the dot-citation register);
4965
+ * envelopes from other versions are dropped loudly: an op without citations cannot be merged
4966
+ * soundly (it would supersede nothing and its siblings would accumulate forever), so versions
4967
+ * are never silently mixed.
4968
+ */
4969
+ const OP_PROTO_VERSION = 2;
4955
4970
  const CONFLICT_BRAND = '~mmstackConflict';
4956
4971
  function isConflicted(value) {
4957
4972
  return typeof value === 'object' && value !== null && CONFLICT_BRAND in value;
4958
4973
  }
4974
+ const hasControlChar = (s) => {
4975
+ for (let i = 0; i < s.length; i++)
4976
+ if (s.charCodeAt(i) < 0x20)
4977
+ return true;
4978
+ return false;
4979
+ };
4980
+ const isCleanId = (v) => typeof v === 'string' && v.length > 0 && !hasControlChar(v);
4981
+ const isFiniteHlc = (h) => !!h &&
4982
+ typeof h === 'object' &&
4983
+ Number.isFinite(h.p) &&
4984
+ Number.isFinite(h.l);
4985
+ /**
4986
+ * Deterministic, total well-formedness check for a received envelope. Returns a short reason
4987
+ * string when the envelope must be rejected WHOLE, or `null` when it is well-formed. It reads only
4988
+ * the envelope (no clock, no local state), so every replica accepts or rejects a given envelope
4989
+ * identically. This validates SHAPE, not authority: it closes malformed input (control characters
4990
+ * in an id or path segment that could forge a path-key separator, a non-integer version, an unknown
4991
+ * op kind, a negative epoch, forged cites, a root delete, two ops racing on one path). Authority and
4992
+ * access control stay at the relay; direct peer-to-peer rooms are trust-full for authority, so this
4993
+ * shape check is a peer's only line against a malformed neighbor.
4994
+ */
4995
+ function validateEnvelope(env) {
4996
+ if (!env || typeof env !== 'object')
4997
+ return 'envelope';
4998
+ if (!isCleanId(env.origin))
4999
+ return 'origin';
5000
+ if (!isCleanId(env.writer))
5001
+ return 'writer';
5002
+ if (!isFiniteHlc(env.hlc))
5003
+ return 'hlc';
5004
+ if (!Number.isInteger(env.version) || env.version <= 0)
5005
+ return 'version';
5006
+ if (!Array.isArray(env.ops))
5007
+ return 'ops';
5008
+ const seenPaths = new Set();
5009
+ for (const op of env.ops) {
5010
+ if (!op || typeof op !== 'object')
5011
+ return 'op';
5012
+ if (op.kind !== 'set' && op.kind !== 'delete' && op.kind !== 'clear')
5013
+ return 'kind';
5014
+ if (!Array.isArray(op.path))
5015
+ return 'path';
5016
+ for (const seg of op.path) {
5017
+ if (typeof seg === 'string' && hasControlChar(seg))
5018
+ return 'path-control';
5019
+ if (seg === '__proto__')
5020
+ return 'path-proto';
5021
+ }
5022
+ if (op.path.length === 0 && op.kind !== 'set')
5023
+ return 'root-op';
5024
+ const epoch = op.epoch;
5025
+ if (typeof epoch !== 'number' || !Number.isFinite(epoch) || epoch < 0)
5026
+ return 'epoch';
5027
+ const cites = op.cites;
5028
+ if (!Array.isArray(cites))
5029
+ return 'cites';
5030
+ for (const c of cites) {
5031
+ if (!c ||
5032
+ typeof c !== 'object' ||
5033
+ !isCleanId(c.origin) ||
5034
+ !isFiniteHlc(c.hlc)) {
5035
+ return 'cites';
5036
+ }
5037
+ }
5038
+ // one op per path per envelope: a dot is (origin, hlc), so two ops on one path in one envelope
5039
+ // would share a dot and break the register's per-origin bookkeeping. Segments with control
5040
+ // characters are already rejected above, so this join is unambiguous.
5041
+ const key = op.path.map(String).join(String.fromCharCode(0x1f));
5042
+ if (seenPaths.has(key))
5043
+ return 'dup-path';
5044
+ seenPaths.add(key);
5045
+ }
5046
+ return null;
5047
+ }
4959
5048
  const lww = (_ancestor, mine) => mine;
4960
5049
  const mergeThree = (ancestor, mine, theirs) => merge3(ancestor, mine, theirs);
4961
- const preserve = (ancestor, mine, theirs) => ({ [CONFLICT_BRAND]: true, mine, theirs, ancestor });
5050
+ const preserve = (ancestor, mine, theirs) => ({
5051
+ [CONFLICT_BRAND]: true,
5052
+ siblings: [mine, theirs],
5053
+ mine,
5054
+ theirs,
5055
+ ancestor,
5056
+ });
4962
5057
  /**
4963
5058
  * Identity-aware array merge: reconciles two concurrent versions of
4964
5059
  * an array item-wise by a user-provided identity, instead of last-writer-wins on the whole
4965
5060
  * array. Items are matched by key; per-item fields merge via `merge3` against the ancestor
4966
5061
  * item; items added on either side survive; an item removed on either side and unedited on
4967
5062
  * the other stays removed. Item ORDER follows `mine` (the total-order winner), with `theirs`-
4968
- * only additions appended positional merging is out of scope (fractional indexing is the
4969
- * known upgrade if dogfooding demands it). Arrays still TRAVEL as whole-value sets; identity
4970
- * only shapes conflict resolution, so the wire format is untouched.
5063
+ * only additions appended, and arrays still TRAVEL as whole-value sets. For a list whose elements
5064
+ * move and edit concurrently, model it as a keyed container (a record of elements ordered by
5065
+ * `posBetween`) instead: `insertElement`/`moveElement`/`removeElement` write per element, so a
5066
+ * reorder and a concurrent edit both survive and elements travel one at a time.
4971
5067
  */
4972
5068
  function keyedArray(identity, opt) {
4973
5069
  const mergeItem = opt?.item ?? mergeThree;
@@ -5015,19 +5111,23 @@ function compilePolicies(entries) {
5015
5111
  merge: e.merge,
5016
5112
  }));
5017
5113
  }
5114
+ function matchSegments(segments, path) {
5115
+ if (segments.length !== path.length)
5116
+ return false;
5117
+ for (let i = 0; i < path.length; i++) {
5118
+ if (segments[i] !== '*' && segments[i] !== String(path[i]))
5119
+ return false;
5120
+ }
5121
+ return true;
5122
+ }
5018
5123
  function policyFor(policies, path) {
5019
- outer: for (const p of policies) {
5020
- if (p.segments.length !== path.length)
5021
- continue;
5022
- for (let i = 0; i < path.length; i++) {
5023
- if (p.segments[i] !== '*' && p.segments[i] !== String(path[i]))
5024
- continue outer;
5025
- }
5026
- return p.merge;
5124
+ for (const p of policies) {
5125
+ if (matchSegments(p.segments, path))
5126
+ return p.merge;
5027
5127
  }
5028
5128
  return lww;
5029
5129
  }
5030
- const SEP = '';
5130
+ const SEP = ''; // unit separator: keeps joined path keys prefix-unambiguous
5031
5131
  const keyOf$1 = (path) => path.map(String).join(SEP);
5032
5132
  function structuralEq(a, b) {
5033
5133
  if (Object.is(a, b))
@@ -5052,108 +5152,539 @@ function structuralEq(a, b) {
5052
5152
  }
5053
5153
  return true;
5054
5154
  }
5055
- // total order (hlc, writer, origin): two origins can share a writer AND a stamp
5056
- // (independent clocks, same ms), so only origin makes the order strict
5057
- const compareStamp = (a, b) => {
5155
+ const kindClass = (k) => (k === 'clear' ? 0 : 1);
5156
+ /**
5157
+ * The register's total order: max by `(epoch, kind-class, hlc, writer, origin)`, where `set`
5158
+ * and `delete` outrank `clear` at equal epoch. Epoch first makes an authority bump decisive
5159
+ * regardless of clocks (and closes stale-value resurrection); the kind-class tier makes a
5160
+ * concurrent edit's survival of a subtree replace categorical rather than a clock race; origin
5161
+ * last keeps the order strict when two replicas share a writer and a stamp.
5162
+ */
5163
+ function compareSiblings(a, b) {
5164
+ if (a.epoch !== b.epoch)
5165
+ return a.epoch - b.epoch;
5166
+ const kc = kindClass(a.kind) - kindClass(b.kind);
5167
+ if (kc !== 0)
5168
+ return kc;
5058
5169
  const byTotal = compareTotal(a.hlc, a.writer, b.hlc, b.writer);
5059
5170
  if (byTotal !== 0)
5060
5171
  return byTotal;
5061
5172
  return a.origin < b.origin ? -1 : a.origin > b.origin ? 1 : 0;
5173
+ }
5174
+ const maxSibling = (siblings) => siblings.reduce((a, b) => (compareSiblings(a, b) >= 0 ? a : b));
5175
+ /** Last-writer-wins over the live sibling set: the {@link compareSiblings} maximum, as-is. */
5176
+ const defaultFold = (siblings) => {
5177
+ const winner = maxSibling(siblings);
5178
+ return winner.kind === 'set'
5179
+ ? { kind: 'set', value: winner.value }
5180
+ : { kind: winner.kind };
5181
+ };
5182
+ // preserve on the register seam: every top-precedence live sibling survives as data. A delete
5183
+ // competes as a value (it may surface inside the conflict as `undefined`); lower-epoch siblings
5184
+ // never surface (the epoch gate stays outermost).
5185
+ const preserveFold = (siblings) => {
5186
+ const winner = maxSibling(siblings);
5187
+ if (winner.kind === 'clear')
5188
+ return { kind: 'clear' };
5189
+ const top = siblings.filter((s) => s.epoch === winner.epoch && s.kind !== 'clear');
5190
+ if (top.length === 1) {
5191
+ return top[0].kind === 'set'
5192
+ ? { kind: 'set', value: top[0].value }
5193
+ : { kind: 'delete' };
5194
+ }
5195
+ const ordered = [...top].sort((a, b) => compareSiblings(b, a));
5196
+ const values = ordered.map((s) => (s.kind === 'set' ? s.value : undefined));
5197
+ const conflicted = {
5198
+ [CONFLICT_BRAND]: true,
5199
+ siblings: values,
5200
+ mine: values[0],
5201
+ theirs: values[1],
5202
+ ancestor: ordered[1].prev,
5203
+ };
5204
+ return { kind: 'set', value: conflicted };
5062
5205
  };
5063
- const beats = (a, b) => compareStamp(a, b) > 0;
5206
+ // A two-sided MergeFn generalized to N siblings: reduce over the canonically-ordered
5207
+ // top-precedence set, winner first, each step merging the next sibling against its own `prev`
5208
+ // as the ancestor. The iteration order is a pure function of the set, so the result converges
5209
+ // even for merges that are not associative (the reason pairwise-at-arrival diverged).
5210
+ const mergeFold = (merge) => {
5211
+ return (siblings, ctx) => {
5212
+ const ordered = [...siblings].sort((a, b) => compareSiblings(b, a));
5213
+ const winner = ordered[0];
5214
+ if (winner.kind !== 'set')
5215
+ return { kind: winner.kind };
5216
+ let acc = winner.value;
5217
+ for (let i = 1; i < ordered.length; i++) {
5218
+ const s = ordered[i];
5219
+ if (s.kind !== 'set' || s.epoch !== winner.epoch)
5220
+ continue;
5221
+ acc = merge(s.prev, acc, s.value, ctx);
5222
+ }
5223
+ return { kind: 'set', value: acc };
5224
+ };
5225
+ };
5226
+ const isContainer = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
5064
5227
  /**
5065
- * The unsequenced-topology convergence core: a per-path last-writer-wins
5066
- * register map over the total order (hlc, writer), with subtree dominance. Order-independent:
5067
- * any arrival order of the same envelope set yields the same state.
5228
+ * The unsequenced-topology convergence core: a dot-citation multi-value register per path.
5229
+ * An op supersedes exactly the sibling dots it cites; uncited concurrent writes stay live; a
5230
+ * pluggable fold resolves the live set at read. Both the live set and any pure fold over it
5231
+ * are functions of the delivered op SET, so any arrival order of the same envelopes (split,
5232
+ * duplicated, cites-before-ops) yields the same state.
5068
5233
  */
5069
5234
  function createConvergingApply(opt) {
5070
5235
  const registers = new Map();
5236
+ // per-path floor of this replica's own emitted epochs: monotone, survives reset() so a
5237
+ // rehydrated replica can never re-emit below an epoch it already exposed
5238
+ const floors = new Map();
5239
+ // monotone ingest counter + the seq at which each live sibling arrived (keyed pathKey → origin).
5240
+ // captureFrontier() reads the counter in O(1); a frontier-scoped stamp cites only siblings at or
5241
+ // below the captured seq. Side-mapped so the public sibling/checkpoint shapes stay unchanged.
5242
+ let ingestSeq = 0;
5243
+ const seqs = new Map();
5244
+ const setSeq = (key, origin, seq) => {
5245
+ let sm = seqs.get(key);
5246
+ if (!sm)
5247
+ seqs.set(key, (sm = new Map()));
5248
+ sm.set(origin, seq);
5249
+ };
5071
5250
  const policies = compilePolicies(opt?.policies ?? []);
5072
- const resolveConcurrent = (winner, loser, path) => {
5251
+ const customFolds = (opt?.folds ?? []).map((e) => ({
5252
+ segments: typeof e.path === 'string' ? e.path.split('.') : e.path.map(String),
5253
+ fold: e.fold,
5254
+ }));
5255
+ const foldFor = (path) => {
5256
+ for (const f of customFolds) {
5257
+ if (matchSegments(f.segments, path))
5258
+ return f.fold;
5259
+ }
5073
5260
  const merge = policyFor(policies, path);
5074
- if (merge === lww || winner.kind === 'delete' || loser.kind === 'delete') {
5075
- return winner;
5261
+ if (merge === lww)
5262
+ return defaultFold;
5263
+ if (merge === preserve)
5264
+ return preserveFold;
5265
+ return mergeFold(merge);
5266
+ };
5267
+ const regAt = (path) => {
5268
+ const key = keyOf$1(path);
5269
+ let reg = registers.get(key);
5270
+ if (!reg) {
5271
+ reg = { path, siblings: new Map(), water: new Map(), sig: '' };
5272
+ registers.set(key, reg);
5273
+ }
5274
+ return reg;
5275
+ };
5276
+ const liveOf = (reg) => {
5277
+ const out = [];
5278
+ for (const [o, s] of reg.siblings) {
5279
+ const w = reg.water.get(o);
5280
+ if (!w || compareHlc(s.hlc, w) > 0)
5281
+ out.push(s);
5076
5282
  }
5077
- const resolved = merge(loser.prev, winner.next, loser.next, { path });
5078
- if (Object.is(resolved, winner.next))
5079
- return winner;
5080
- return { kind: 'set', path, next: resolved, prev: winner.next };
5283
+ return out.sort((a, b) => a.origin < b.origin ? -1 : a.origin > b.origin ? 1 : 0);
5284
+ };
5285
+ // The live siblings a frontier had observed: those that arrived at or below its captured seq.
5286
+ // Used by a fork commit so it supersedes only what it saw when it forked, not later writes.
5287
+ const liveObserved = (reg, frontier) => {
5288
+ const live = liveOf(reg);
5289
+ if (!frontier)
5290
+ return live;
5291
+ const sm = seqs.get(keyOf$1(reg.path));
5292
+ return live.filter((s) => (sm?.get(s.origin) ?? 0) <= frontier.seq);
5081
5293
  };
5082
- const concurrentWith = (incoming, registered) => {
5083
- if (incoming.kind === 'delete' || registered.kind === 'delete')
5294
+ // JSON of a tuple array, not a separator-joined string: `origin` is a caller-supplied value on a
5295
+ // P2P peer, so a naive `origin@p.l#epoch` join lets a crafted origin collide the signatures of two
5296
+ // distinct live sets. A collision makes refresh() skip a fold update, and since that skip is
5297
+ // arrival-order-sensitive it breaks convergence. JSON.stringify escapes the strings and the array
5298
+ // structure is unambiguous, so the signature is injective in the live set.
5299
+ const sigOf = (live) => JSON.stringify(live.map((s) => [s.origin, s.hlc.p, s.hlc.l, s.epoch, s.kind]));
5300
+ /** Recompute the fold cache; true iff the materialized result meaningfully changed. */
5301
+ const refresh = (reg) => {
5302
+ const live = liveOf(reg);
5303
+ const sig = sigOf(live);
5304
+ if (sig === reg.sig)
5084
5305
  return false;
5085
- if (!Object.hasOwn(incoming, 'prev'))
5086
- return true;
5087
- return !structuralEq(incoming.prev, registered.next);
5306
+ reg.sig = sig;
5307
+ const next = live.length
5308
+ ? foldFor(reg.path)(live, { path: reg.path })
5309
+ : undefined;
5310
+ const prev = reg.result;
5311
+ const same = prev === next ||
5312
+ (!!prev &&
5313
+ !!next &&
5314
+ prev.kind === next.kind &&
5315
+ (prev.kind !== 'set' ||
5316
+ next.kind !== 'set' ||
5317
+ Object.is(prev.value, next.value) ||
5318
+ structuralEq(prev.value, next.value)));
5319
+ if (same)
5320
+ return false; // keep the previous result object: reference identity is the contract
5321
+ reg.result = next;
5322
+ return true;
5323
+ };
5324
+ const descendantsOf = (key) => {
5325
+ const out = [];
5326
+ for (const [k, r] of registers) {
5327
+ if (k === key)
5328
+ continue;
5329
+ if (key === '' ? k !== '' : k.startsWith(key + SEP))
5330
+ out.push(r);
5331
+ }
5332
+ return out.sort((a, b) => a.path.length - b.path.length ||
5333
+ (keyOf$1(a.path) < keyOf$1(b.path) ? -1 : 1));
5334
+ };
5335
+ /** Does `value` still hold a key at `rel` (present, not merely undefined)? */
5336
+ const holdsKey = (value, rel) => {
5337
+ let cur = value;
5338
+ for (const seg of rel) {
5339
+ if (cur === null ||
5340
+ typeof cur !== 'object' ||
5341
+ !Object.hasOwn(cur, String(seg))) {
5342
+ return false;
5343
+ }
5344
+ cur = cur[String(seg)];
5345
+ }
5346
+ return true;
5347
+ };
5348
+ // A lone tombstone is droppable only if nothing else still materializes its key: no live
5349
+ // descendant register would resurface, and no live ancestor `set` value still holds it. Mirrors
5350
+ // the relay's retention twin so a client that prunes converges with a joiner seeded from the relay.
5351
+ const tombstoneDroppable = (key, reg) => {
5352
+ for (const [k, other] of registers) {
5353
+ if (k === key)
5354
+ continue;
5355
+ if (k.startsWith(key + SEP)) {
5356
+ if (liveOf(other).length > 0)
5357
+ return false;
5358
+ }
5359
+ else if (key.startsWith(k === '' ? '' : k + SEP)) {
5360
+ const rel = reg.path.slice(other.path.length);
5361
+ for (const s of liveOf(other)) {
5362
+ if (s.kind === 'set' && holdsKey(s.value, rel))
5363
+ return false;
5364
+ }
5365
+ }
5366
+ }
5367
+ return true;
5368
+ };
5369
+ /** Nearest ancestor register that contributes a value or a deletion (clears abstain). */
5370
+ const nearestContributing = (path) => {
5371
+ for (let len = path.length - 1; len >= 0; len--) {
5372
+ const reg = registers.get(keyOf$1(path.slice(0, len)));
5373
+ if (reg?.result && reg.result.kind !== 'clear')
5374
+ return reg;
5375
+ }
5376
+ return undefined;
5377
+ };
5378
+ // graft with the deterministic type-change rule: a graft whose parent location is not a plain
5379
+ // record is DROPPED (the register stays intact and resurfaces if the container is restored)
5380
+ const graft = (tree, rel, res) => {
5381
+ if (!isContainer(tree))
5382
+ return tree;
5383
+ const head = String(rel[0]);
5384
+ if (rel.length === 1) {
5385
+ if (res.kind === 'delete') {
5386
+ if (!Object.hasOwn(tree, head))
5387
+ return tree;
5388
+ const copy = { ...tree };
5389
+ delete copy[head];
5390
+ return copy;
5391
+ }
5392
+ return { ...tree, [head]: res.value };
5393
+ }
5394
+ if (!Object.hasOwn(tree, head)) {
5395
+ // vivify an absent middle container so a checkpoint-seeded materialization matches a peer that
5396
+ // applied the ops incrementally (incremental apply creates missing parents). A numeric next
5397
+ // segment vivifies an array, else an object, mirroring the incremental apply path.
5398
+ const vivified = typeof rel[1] === 'number' ? [] : {};
5399
+ return { ...tree, [head]: graft(vivified, rel.slice(1), res) };
5400
+ }
5401
+ const child = graft(tree[head], rel.slice(1), res);
5402
+ return child === tree[head] ? tree : { ...tree, [head]: child };
5403
+ };
5404
+ /** Would a value at `rel` under `value` materialize, per the graft rules? */
5405
+ const graftable = (value, rel) => {
5406
+ let cur = value;
5407
+ for (let i = 0; i < rel.length - 1; i++) {
5408
+ if (!isContainer(cur) || !Object.hasOwn(cur, String(rel[i])))
5409
+ return false;
5410
+ cur = cur[String(rel[i])];
5411
+ }
5412
+ return isContainer(cur);
5413
+ };
5414
+ /**
5415
+ * Whether a value at `path` materializes: every contributing ancestor register down the
5416
+ * chain must be a `set` whose value composes containers to the next one. The drop rule is
5417
+ * checked against the WHOLE chain, since a graft fine under its nearest ancestor can still drop
5418
+ * at a scalar further up.
5419
+ */
5420
+ const shows = (path) => {
5421
+ let holder;
5422
+ for (let len = 0; len < path.length; len++) {
5423
+ const reg = registers.get(keyOf$1(path.slice(0, len)));
5424
+ if (!reg?.result || reg.result.kind === 'clear')
5425
+ continue;
5426
+ if (holder) {
5427
+ const hres = holder.result;
5428
+ if (!hres || hres.kind !== 'set')
5429
+ return false;
5430
+ if (!graftable(hres.value, reg.path.slice(holder.path.length))) {
5431
+ return false;
5432
+ }
5433
+ }
5434
+ holder = reg;
5435
+ }
5436
+ if (!holder)
5437
+ return true; // nothing above constrains → vivify semantics
5438
+ const hres = holder.result;
5439
+ if (!hres || hres.kind !== 'set')
5440
+ return false;
5441
+ return graftable(hres.value, path.slice(holder.path.length));
5442
+ };
5443
+ /** Deepest-live-wins subtree value: the register's fold value with every live descendant fold grafted on. */
5444
+ const materializeAt = (base) => {
5445
+ const res = base.result;
5446
+ let tree = res && res.kind === 'set' ? res.value : undefined;
5447
+ for (const d of descendantsOf(keyOf$1(base.path))) {
5448
+ const r = d.result;
5449
+ if (!r || r.kind === 'clear')
5450
+ continue;
5451
+ if (!shows(d.path))
5452
+ continue; // dropped under a deleted/scalar ancestor (matches applied deltas)
5453
+ tree = graft(tree, d.path.slice(base.path.length), r);
5454
+ }
5455
+ return tree;
5456
+ };
5457
+ const deltas = (changed) => {
5458
+ changed.sort((a, b) => a.reg.path.length - b.reg.path.length ||
5459
+ (keyOf$1(a.reg.path) < keyOf$1(b.reg.path) ? -1 : 1));
5460
+ const out = [];
5461
+ const regions = [];
5462
+ const covered = (key) => regions.some((r) => key === r || (r === '' ? true : key.startsWith(r + SEP)));
5463
+ for (const { reg, before } of changed) {
5464
+ const key = keyOf$1(reg.path);
5465
+ if (covered(key))
5466
+ continue;
5467
+ const res = reg.result;
5468
+ if (!res || res.kind === 'clear') {
5469
+ // the register now abstains: re-materialize the nearest ancestor region it cleared out of
5470
+ if (!reg.path.length)
5471
+ continue;
5472
+ const anc = nearestContributing(reg.path);
5473
+ const ares = anc?.result;
5474
+ if (!anc || !ares || ares.kind !== 'set' || !shows(anc.path))
5475
+ continue;
5476
+ out.push({ kind: 'set', path: anc.path, next: materializeAt(anc) });
5477
+ regions.push(keyOf$1(anc.path));
5478
+ continue;
5479
+ }
5480
+ if (!shows(reg.path))
5481
+ continue; // dropped by the type-change rule or a deleted parent
5482
+ if (res.kind === 'delete') {
5483
+ if (!reg.path.length)
5484
+ continue; // a root delete is meaningless
5485
+ out.push({
5486
+ kind: 'delete',
5487
+ path: reg.path,
5488
+ prev: before?.kind === 'set' ? before.value : undefined,
5489
+ });
5490
+ }
5491
+ else {
5492
+ out.push({ kind: 'set', path: reg.path, next: materializeAt(reg) });
5493
+ }
5494
+ regions.push(key);
5495
+ }
5496
+ return out;
5088
5497
  };
5089
5498
  return {
5090
5499
  ingest: (env, o) => {
5091
- const stamp = {
5092
- hlc: env.hlc,
5093
- writer: env.writer,
5094
- origin: env.origin,
5095
- };
5096
- const out = [];
5500
+ const touched = new Map();
5501
+ const seq = ++ingestSeq;
5097
5502
  for (const op of env.ops) {
5503
+ if (o?.frontier && compareHlc(env.hlc, o.frontier) <= 0)
5504
+ continue; // below the pruned horizon
5505
+ // a delete or clear at the root has no parent register to abstain to; it can only blank the
5506
+ // whole document, and materialize would then disagree with the delta path, so drop it
5507
+ if (!op.path.length && op.kind !== 'set')
5508
+ continue;
5509
+ const reg = regAt(op.path);
5098
5510
  const key = keyOf$1(op.path);
5099
- let dominated = false;
5100
- let exact;
5101
- for (let len = 0; len <= op.path.length; len++) {
5102
- const reg = registers.get(keyOf$1(op.path.slice(0, len)));
5103
- if (!reg)
5511
+ if (!touched.has(key))
5512
+ touched.set(key, { reg, before: reg.result });
5513
+ const sop = op;
5514
+ for (const c of sop.cites ?? []) {
5515
+ // a self-citation (the op citing its own dot) would born-dead the write; ignore it
5516
+ if (c.origin === env.origin && compareHlc(c.hlc, env.hlc) === 0)
5104
5517
  continue;
5105
- if (len === op.path.length)
5106
- exact = reg;
5107
- else if (beats(reg, stamp)) {
5108
- dominated = true;
5109
- break;
5110
- }
5518
+ const cur = reg.water.get(c.origin);
5519
+ if (!cur || compareHlc(c.hlc, cur) > 0)
5520
+ reg.water.set(c.origin, c.hlc);
5111
5521
  }
5112
- if (dominated)
5113
- continue;
5114
- if (exact && beats(exact, stamp)) {
5115
- if (concurrentWith(op, exact.op)) {
5116
- const resolved = resolveConcurrent(exact.op, op, op.path);
5117
- if (resolved !== exact.op) {
5118
- exact.op = resolved;
5119
- if (!o?.local)
5120
- out.push(resolved);
5121
- }
5522
+ const best = reg.siblings.get(env.origin);
5523
+ if (!best || compareHlc(env.hlc, best.hlc) > 0) {
5524
+ const sib = {
5525
+ kind: op.kind,
5526
+ writer: env.writer,
5527
+ origin: env.origin,
5528
+ hlc: env.hlc,
5529
+ epoch: sop.epoch ?? 0,
5530
+ };
5531
+ if (op.kind === 'set')
5532
+ sib.value = op.next;
5533
+ if (op.kind !== 'clear' && Object.hasOwn(op, 'prev')) {
5534
+ sib.prev = op.prev;
5122
5535
  }
5123
- continue;
5536
+ reg.siblings.set(env.origin, sib);
5537
+ setSeq(key, env.origin, seq);
5124
5538
  }
5125
- let accepted = op;
5126
- if (exact && concurrentWith(op, exact.op)) {
5127
- accepted = resolveConcurrent(op, exact.op, op.path);
5539
+ if (o?.local && (sop.epoch ?? 0) > 0) {
5540
+ floors.set(key, Math.max(floors.get(key) ?? 0, sop.epoch));
5128
5541
  }
5129
- const isDescendant = key === ''
5130
- ? (k) => k !== ''
5131
- : (k) => k.startsWith(key + SEP);
5132
- const replays = [];
5133
- for (const [k, reg] of registers) {
5134
- if (!isDescendant(k))
5542
+ }
5543
+ const changed = [];
5544
+ for (const c of touched.values()) {
5545
+ if (refresh(c.reg))
5546
+ changed.push(c);
5547
+ }
5548
+ if ((o?.local && !o?.reconcile) || !changed.length)
5549
+ return [];
5550
+ return deltas(changed);
5551
+ },
5552
+ stamp: (ops, o) => {
5553
+ const out = [];
5554
+ const bump = o?.bump ? 1 : 0;
5555
+ const frontier = o?.frontier;
5556
+ const epochFor = (key, live) => {
5557
+ let e = floors.get(key) ?? 0;
5558
+ for (const s of live)
5559
+ if (s.epoch > e)
5560
+ e = s.epoch;
5561
+ return e + bump;
5562
+ };
5563
+ for (const op of ops) {
5564
+ const key = keyOf$1(op.path);
5565
+ const reg = registers.get(key);
5566
+ const live = reg ? liveObserved(reg, frontier) : [];
5567
+ out.push({
5568
+ ...op,
5569
+ cites: live.map((s) => ({ origin: s.origin, hlc: s.hlc })),
5570
+ epoch: epochFor(key, live),
5571
+ });
5572
+ if (op.kind === 'clear')
5573
+ continue;
5574
+ for (const d of descendantsOf(key)) {
5575
+ if (d.result?.kind === 'clear')
5576
+ continue; // already abstaining
5577
+ const dlive = liveObserved(d, frontier);
5578
+ if (!dlive.length)
5135
5579
  continue;
5136
- if (beats(stamp, reg))
5137
- registers.delete(k);
5138
- else
5139
- replays.push(reg);
5580
+ out.push({
5581
+ kind: 'clear',
5582
+ path: d.path,
5583
+ cites: dlive.map((s) => ({ origin: s.origin, hlc: s.hlc })),
5584
+ epoch: epochFor(keyOf$1(d.path), dlive),
5585
+ });
5140
5586
  }
5141
- replays.sort(compareStamp);
5142
- registers.set(key, {
5143
- hlc: env.hlc,
5144
- writer: env.writer,
5145
- origin: env.origin,
5146
- op: accepted,
5587
+ }
5588
+ return out;
5589
+ },
5590
+ captureFrontier: () => ({ seq: ingestSeq }),
5591
+ liveAt: (path) => {
5592
+ const reg = registers.get(keyOf$1(path));
5593
+ return reg ? liveOf(reg) : [];
5594
+ },
5595
+ materialize: () => {
5596
+ const root = registers.get('');
5597
+ const res = root?.result;
5598
+ let tree = res && res.kind === 'set' ? res.value : undefined;
5599
+ for (const d of descendantsOf('')) {
5600
+ const r = d.result;
5601
+ if (!r || r.kind === 'clear')
5602
+ continue;
5603
+ if (!shows(d.path))
5604
+ continue; // dropped under a deleted/scalar ancestor (matches applied deltas)
5605
+ if (tree === undefined)
5606
+ tree = {}; // vivify: deeper registers materialize without a root write
5607
+ tree = graft(tree, d.path, r);
5608
+ }
5609
+ return tree;
5610
+ },
5611
+ checkpoint: () => {
5612
+ const out = [];
5613
+ for (const reg of registers.values()) {
5614
+ out.push({
5615
+ path: reg.path,
5616
+ siblings: [...reg.siblings.values()],
5617
+ water: Object.fromEntries(reg.water),
5147
5618
  });
5148
- if (!o?.local) {
5149
- out.push(accepted);
5150
- for (const r of replays)
5151
- out.push(r.op);
5152
- }
5153
5619
  }
5154
5620
  return out;
5155
5621
  },
5156
- reset: () => registers.clear(),
5622
+ load: (regs) => {
5623
+ const seq = ++ingestSeq;
5624
+ for (const r of regs) {
5625
+ const reg = regAt(r.path);
5626
+ const key = keyOf$1(r.path);
5627
+ for (const s of r.siblings) {
5628
+ const cur = reg.siblings.get(s.origin);
5629
+ if (!cur || compareHlc(s.hlc, cur.hlc) > 0) {
5630
+ reg.siblings.set(s.origin, s);
5631
+ setSeq(key, s.origin, seq);
5632
+ }
5633
+ }
5634
+ for (const [o, h] of Object.entries(r.water)) {
5635
+ const cur = reg.water.get(o);
5636
+ if (!cur || compareHlc(h, cur) > 0)
5637
+ reg.water.set(o, h);
5638
+ }
5639
+ if (opt?.origin) {
5640
+ const own = reg.siblings.get(opt.origin);
5641
+ if (own) {
5642
+ floors.set(key, Math.max(floors.get(key) ?? 0, own.epoch));
5643
+ }
5644
+ }
5645
+ refresh(reg);
5646
+ }
5647
+ },
5648
+ prune: (frontier) => {
5649
+ for (const [key, reg] of [...registers]) {
5650
+ const sm = seqs.get(key);
5651
+ for (const [o, s] of [...reg.siblings]) {
5652
+ const w = reg.water.get(o);
5653
+ if (compareHlc(s.hlc, frontier) <= 0 &&
5654
+ w &&
5655
+ compareHlc(s.hlc, w) <= 0) {
5656
+ reg.siblings.delete(o);
5657
+ sm?.delete(o);
5658
+ }
5659
+ }
5660
+ for (const [o, h] of [...reg.water]) {
5661
+ if (compareHlc(h, frontier) <= 0)
5662
+ reg.water.delete(o);
5663
+ }
5664
+ if (reg.siblings.size === 0 && reg.water.size === 0) {
5665
+ registers.delete(key);
5666
+ seqs.delete(key);
5667
+ floors.delete(key);
5668
+ }
5669
+ }
5670
+ const byDepth = [...registers.entries()].sort((a, b) => b[1].path.length - a[1].path.length);
5671
+ for (const [key, reg] of byDepth) {
5672
+ const live = liveOf(reg);
5673
+ if (live.length === 1 &&
5674
+ live[0].kind === 'delete' &&
5675
+ reg.siblings.size === 1 &&
5676
+ compareHlc(live[0].hlc, frontier) <= 0 &&
5677
+ tombstoneDroppable(key, reg)) {
5678
+ registers.delete(key);
5679
+ seqs.delete(key);
5680
+ floors.delete(key);
5681
+ }
5682
+ }
5683
+ },
5684
+ reset: () => {
5685
+ registers.clear();
5686
+ seqs.clear();
5687
+ },
5157
5688
  };
5158
5689
  }
5159
5690
  function getAtPath(root, path) {
@@ -5181,6 +5712,10 @@ function rebaseOps(root, pending, remote, policies) {
5181
5712
  for (const batch of pending) {
5182
5713
  const next = [];
5183
5714
  for (const op of batch) {
5715
+ if (op.kind === 'clear') {
5716
+ next.push(op); // a register intent, not a value change: passes through untouched
5717
+ continue;
5718
+ }
5184
5719
  const cur = getAtPath(base, op.path);
5185
5720
  if (op.kind === 'delete') {
5186
5721
  next.push({ kind: 'delete', path: op.path, prev: cur });
@@ -5219,20 +5754,30 @@ function generateOrigin() {
5219
5754
  }
5220
5755
  /**
5221
5756
  * Wires a copy-on-write signal (a `store` root) to the op protocol: local writes emit
5222
- * stamped envelopes, received envelopes fold in through the converging apply. The
5223
- * unsequenced-topology client core that `tabSync(store)` and P2P transports build on.
5757
+ * stamped envelopes (citing the sibling dots they observed), received envelopes fold in
5758
+ * through the converging register. The unsequenced-topology client core that
5759
+ * `tabSync(store)` and P2P transports build on.
5224
5760
  */
5225
5761
  const RECENT_LOCAL_CAP = 64;
5226
5762
  function opSync(source, opt) {
5227
5763
  const origin = opt.origin ?? generateOrigin();
5228
5764
  const clock = opt.clock ?? createHlcClock();
5229
- const conv = createConvergingApply({ policies: opt.policies });
5765
+ const conv = createConvergingApply({
5766
+ policies: opt.policies,
5767
+ folds: opt.folds,
5768
+ origin,
5769
+ });
5230
5770
  const subscribers = new Set();
5231
5771
  // per-origin high-watermark; `versions.get(origin)` IS the local emit counter, so a hydrate/restore
5232
5772
  // that raises our own watermark also advances the next mint — no separate counter to drift out of
5233
5773
  // sync and collide with a version acked before a reboot but dropped from a debounced outbox.
5234
5774
  const versions = new Map();
5235
5775
  const recentLocal = [];
5776
+ // highest stability frontier this peer has pruned to. A remote envelope at or below it is a settled
5777
+ // straggler (its state is compacted away); re-admitting one could resurrect a value below the
5778
+ // frontier, and per-origin version dedup cannot catch a FIRST-CONTACT straggler (no prior entry),
5779
+ // so the frontier is the admission gate that closes that hole on the receive path.
5780
+ let prunedFrontier;
5236
5781
  const resolvedInjector = opt.driver
5237
5782
  ? null
5238
5783
  : (opt.injector ?? inject(Injector));
@@ -5253,6 +5798,9 @@ function opSync(source, opt) {
5253
5798
  const canDefer = !opt.driver;
5254
5799
  const outbox = [];
5255
5800
  let receiving = false;
5801
+ let bumping = false;
5802
+ // set while a synced fork's commit is emitting: freezes emission cites to what the fork observed
5803
+ let scopeFrontier;
5256
5804
  // a signal the drain reaction tracks; bumping it schedules an outbox drain for the next tick
5257
5805
  const drainTick = signal(0, ...(ngDevMode ? [{ debugName: "drainTick" }] : /* istanbul ignore next */ []));
5258
5806
  const scheduleDrain = () => drainTick.update((v) => v + 1);
@@ -5265,6 +5813,8 @@ function opSync(source, opt) {
5265
5813
  notify(env);
5266
5814
  };
5267
5815
  const emitLocal = (ops) => {
5816
+ const frontier = scopeFrontier;
5817
+ const stamped = conv.stamp(ops, { bump: bumping, frontier });
5268
5818
  const nextVersion = (versions.get(origin) ?? 0) + 1;
5269
5819
  const env = {
5270
5820
  proto: OP_PROTO_VERSION,
@@ -5273,10 +5823,19 @@ function opSync(source, opt) {
5273
5823
  version: nextVersion,
5274
5824
  hlc: clock.next(),
5275
5825
  policyVersion: opt.policyVersion ?? 0,
5276
- ops,
5826
+ ops: stamped,
5277
5827
  };
5278
5828
  versions.set(origin, nextVersion);
5279
- conv.ingest(env, { local: true });
5829
+ if (frontier) {
5830
+ // a fork commit: its ops are concurrent siblings (they cite only the fork-time frontier), so
5831
+ // move the store to the fold winner rather than leaving the raw committed value in place
5832
+ const reconciled = conv.ingest(env, { local: true, reconcile: true });
5833
+ if (reconciled.length)
5834
+ log.apply(reconciled);
5835
+ }
5836
+ else {
5837
+ conv.ingest(env, { local: true });
5838
+ }
5280
5839
  recentLocal.push(env);
5281
5840
  if (recentLocal.length > RECENT_LOCAL_CAP)
5282
5841
  recentLocal.shift();
@@ -5308,10 +5867,25 @@ function opSync(source, opt) {
5308
5867
  return;
5309
5868
  if (env.proto !== OP_PROTO_VERSION) {
5310
5869
  if (isDevMode()) {
5311
- console.warn(`[@mmstack/primitives] dropped envelope with proto ${env.proto} (expected ${OP_PROTO_VERSION})`);
5870
+ console.warn(`[@mmstack/primitives] dropped envelope with proto ${env.proto} (expected ${OP_PROTO_VERSION}: ops must carry cites + epoch; emitters from another protocol version are rejected rather than silently mixed)`);
5871
+ }
5872
+ return;
5873
+ }
5874
+ const reason = validateEnvelope(env);
5875
+ if (reason !== null) {
5876
+ if (isDevMode()) {
5877
+ console.warn(`[@mmstack/primitives] dropped malformed envelope (${reason}) from origin ${String(env.origin)}`);
5312
5878
  }
5879
+ opt.onReject?.(env, reason);
5313
5880
  return;
5314
5881
  }
5882
+ // a settled straggler at or below the pruned stability frontier: reject it (its state is
5883
+ // compacted, re-admitting could resurrect a below-frontier value). All ops in an envelope share
5884
+ // its stamp, so the envelope hlc is the dot for every op. The live relay path never delivers a
5885
+ // below-frontier op (a lagging client gets a snapshot, not a delta), so this only fires on a
5886
+ // stray re-broadcast, e.g. over a P2P/multi-path topology.
5887
+ if (prunedFrontier && compareHlc(env.hlc, prunedFrontier) <= 0)
5888
+ return;
5315
5889
  const known = versions.get(env.origin);
5316
5890
  if (known !== undefined && env.version <= known)
5317
5891
  return; // duplicate/covered — idempotent
@@ -5321,14 +5895,9 @@ function opSync(source, opt) {
5321
5895
  versions.set(env.origin, env.version);
5322
5896
  receiving = true;
5323
5897
  try {
5324
- // Freeze pending local FIRST — stamped by a clock that has NOT yet observed this remote, so
5325
- // the local write keeps its causally-independent (original) stamp rather than being lifted
5326
- // above the remote and always winning. Emission is deferred to a tick via the outbox; this
5327
- // only registers + stamps.
5328
5898
  log.flush();
5329
5899
  clock.observe(env.hlc);
5330
5900
  const ops = conv.ingest(env);
5331
- // apply the converged result — a local write that LOST its path rolls back visibly here
5332
5901
  if (ops.length)
5333
5902
  log.apply(ops);
5334
5903
  }
@@ -5340,45 +5909,88 @@ function opSync(source, opt) {
5340
5909
  drainOutbox();
5341
5910
  log.flush();
5342
5911
  },
5912
+ override: (fn) => {
5913
+ log.flush(); // earlier pending writes emit un-bumped
5914
+ bumping = true;
5915
+ try {
5916
+ fn();
5917
+ log.flush();
5918
+ }
5919
+ finally {
5920
+ bumping = false;
5921
+ }
5922
+ },
5923
+ captureFrontier: () => {
5924
+ log.flush(); // fold pending base writes in first, so they count as observed
5925
+ return conv.captureFrontier();
5926
+ },
5927
+ commitScope: (frontier, fn) => {
5928
+ log.flush(); // earlier pending writes emit against the live frontier, not this one
5929
+ scopeFrontier = frontier;
5930
+ try {
5931
+ fn();
5932
+ log.flush(); // stamp + register the scoped writes now, while the frontier is frozen
5933
+ }
5934
+ finally {
5935
+ scopeFrontier = undefined;
5936
+ }
5937
+ },
5343
5938
  watermark: () => Object.fromEntries(versions),
5939
+ prune: (frontier) => {
5940
+ if (!prunedFrontier || compareHlc(frontier, prunedFrontier) > 0) {
5941
+ prunedFrontier = frontier;
5942
+ }
5943
+ conv.prune(frontier);
5944
+ },
5344
5945
  snapshot: () => {
5345
5946
  log.flush();
5346
- return { root: untracked(source), wm: Object.fromEntries(versions) };
5947
+ return {
5948
+ root: untracked(source),
5949
+ registers: conv.checkpoint(),
5950
+ wm: Object.fromEntries(versions),
5951
+ };
5347
5952
  },
5348
5953
  seed: () => {
5349
5954
  log.flush();
5350
5955
  emitLocal([{ kind: 'set', path: [], next: untracked(source) }]);
5351
5956
  },
5352
- hydrate: (root, wm) => {
5957
+ hydrate: (state, pending) => {
5353
5958
  log.flush();
5354
- const covered = wm?.[origin] ?? 0;
5355
- const pending = recentLocal.filter((e) => e.version > covered);
5959
+ // rebase this origin's uncovered local writes on top. A caller that keeps a durable outbox
5960
+ // (meshSync, the worker replica) passes its full unacked set, so a long offline burst larger
5961
+ // than the in-memory `recentLocal` cap is never dropped from the rebase; without it, fall back
5962
+ // to the recent-local ring.
5963
+ const source = pending ?? recentLocal;
5964
+ const toReplay = source.filter((e) => e.version > (state.wm?.[e.origin] ?? 0));
5356
5965
  conv.reset();
5357
- let next = root;
5358
- for (const e of pending)
5359
- next = applyOps(next, e.ops);
5360
- log.apply([{ kind: 'set', path: [], next }]);
5361
- for (const [o, v] of Object.entries(wm ?? {})) {
5966
+ conv.load(state.registers ?? []);
5967
+ const deltas = [];
5968
+ for (const e of toReplay) {
5969
+ deltas.push(...conv.ingest(e, { local: true, reconcile: true }));
5970
+ }
5971
+ log.apply([
5972
+ { kind: 'set', path: [], next: applyOps(state.root, deltas) },
5973
+ ]);
5974
+ for (const [o, v] of Object.entries(state.wm ?? {})) {
5362
5975
  versions.set(o, Math.max(versions.get(o) ?? 0, v));
5363
5976
  }
5364
- for (const e of pending)
5365
- conv.ingest(e, { local: true });
5366
5977
  },
5367
5978
  restore: (envs, highWater) => {
5368
- let maxV = versions.get(origin) ?? 0;
5979
+ let tailOrigin;
5369
5980
  for (const env of envs) {
5370
- if (env.origin !== origin)
5371
- continue; // only this origin's own durable outbox
5372
5981
  clock.observe(env.hlc); // keep the clock ≥ restored stamps before any future mint
5373
5982
  log.apply(env.ops); // reflect the offline edit in the store, echo-free
5374
5983
  conv.ingest(env, { local: true }); // register as a local winner (survives a reconnect merge)
5375
5984
  recentLocal.push(env);
5376
5985
  if (recentLocal.length > RECENT_LOCAL_CAP)
5377
5986
  recentLocal.shift();
5378
- maxV = Math.max(maxV, env.version);
5987
+ versions.set(env.origin, Math.max(versions.get(env.origin) ?? 0, env.version));
5988
+ tailOrigin = env.origin;
5379
5989
  notify(env); // hand to the transport to resend the unacknowledged tail
5380
5990
  }
5381
- versions.set(origin, Math.max(maxV, highWater ?? 0));
5991
+ if (highWater != null && tailOrigin != null) {
5992
+ versions.set(tailOrigin, Math.max(versions.get(tailOrigin) ?? 0, highWater));
5993
+ }
5382
5994
  },
5383
5995
  destroy: () => {
5384
5996
  drainOutbox(); // don't silently drop frozen-but-unsent local writes
@@ -5389,6 +6001,196 @@ function opSync(source, opt) {
5389
6001
  },
5390
6002
  };
5391
6003
  }
6004
+ /**
6005
+ * Fork a synced store for isolated edits (an agent branch, a staged review), keeping the correct
6006
+ * emission semantics on commit. The fork observes the base as it was when this call ran; committing
6007
+ * emits its diff citing only those observed dots, so an edit that landed on the base mid-flight
6008
+ * stays a concurrent sibling and the configured fold decides between them, rather than the commit
6009
+ * overwriting a write it never saw. `rebase()` re-observes the base (a following commit then
6010
+ * supersedes what is visible now, the reviewed-and-apply step). Pass the same `store` and `sync`
6011
+ * that are wired together; the fork is a plain {@link Fork} otherwise, so `forkStore` itself stays
6012
+ * sync-agnostic.
6013
+ */
6014
+ function syncedFork(sync, store, opt) {
6015
+ let frontier = sync.captureFrontier();
6016
+ const f = forkStore(store, opt);
6017
+ return {
6018
+ store: f.store,
6019
+ ops: f.ops,
6020
+ commit: () => sync.commitScope(frontier, () => f.commit()),
6021
+ discard: () => {
6022
+ f.discard();
6023
+ frontier = sync.captureFrontier();
6024
+ },
6025
+ rebase: () => {
6026
+ frontier = sync.captureFrontier();
6027
+ },
6028
+ };
6029
+ }
6030
+
6031
+ /**
6032
+ * Reserved key holding an element's fractional position inside a keyed container. It lives INSIDE
6033
+ * the element (at `[container, elementKey, '~pos']`), so a reorder is a one-field write that never
6034
+ * collides with a concurrent edit to the element's data. It stays visible on the materialized
6035
+ * element value; do not read, write, or strip it by hand, use the helpers in this file.
6036
+ */
6037
+ const POS_SEGMENT = '~pos';
6038
+ // Order-preserving fractional-index digits. The alphabet is ASCII-ascending, so a plain string
6039
+ // comparison of two positions matches their fractional order with no decoding.
6040
+ const DIGITS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
6041
+ const BASE = DIGITS.length;
6042
+ const digitOf = (c) => DIGITS.indexOf(c);
6043
+ // Repeated inserts into the SAME gap grow a position one digit at a time. Healthy positions stay a
6044
+ // few characters; a long one signals a hot insertion point that should be rebalanced.
6045
+ const POS_WARN_LENGTH = 48;
6046
+ let posWarned = false;
6047
+ const warnIfLong = (pos) => {
6048
+ if (isDevMode() && !posWarned && pos.length >= POS_WARN_LENGTH) {
6049
+ posWarned = true;
6050
+ console.warn(`[@mmstack/primitives] a keyed-container position grew to ${pos.length} characters from ` +
6051
+ `repeated inserts into one gap. Call rebalanceContainer(...) to reclaim precision.`);
6052
+ }
6053
+ return pos;
6054
+ };
6055
+ /**
6056
+ * A compact position string strictly between `before` and `after`, ordered by plain string
6057
+ * comparison. Pass `undefined` for an open end: `posBetween()` seeds the first element,
6058
+ * `posBetween(last)` appends, `posBetween(undefined, first)` prepends. Repeated inserts into the
6059
+ * same gap grow the string one digit at a time rather than colliding, and the result is never equal
6060
+ * to either neighbor. `before` must sort before `after`.
6061
+ */
6062
+ function posBetween(before, after) {
6063
+ // Neighbors can tie (concurrent inserts into the same gap leave two equal positions, ordered only
6064
+ // by key). There is no position strictly between equal bounds, so open the upper end: the new
6065
+ // position sorts just after them and stays deterministic instead of looping.
6066
+ const upper = before != null && after != null && before >= after ? undefined : after;
6067
+ let i = 0;
6068
+ let out = '';
6069
+ // The upper bound only opens to BASE once we pass `after`'s last constraining digit: an adjacent
6070
+ // pair (gap of 1) leaves no room here, so we commit the lower digit and everything deeper is free.
6071
+ let upperOpen = upper == null;
6072
+ for (;;) {
6073
+ const lo = before != null && i < before.length ? digitOf(before[i]) : 0;
6074
+ const hi = upperOpen || upper == null ? BASE : i < upper.length ? digitOf(upper[i]) : 0;
6075
+ if (hi - lo >= 2)
6076
+ return warnIfLong(out + DIGITS[lo + ((hi - lo) >> 1)]);
6077
+ if (hi === lo) {
6078
+ // digits equal: no room yet, but `after` still constrains deeper digits, keep following it
6079
+ out += DIGITS[lo];
6080
+ i++;
6081
+ continue;
6082
+ }
6083
+ // gap of 1: commit the lower digit and open the upper bound (deeper digits only exceed `before`)
6084
+ out += DIGITS[lo];
6085
+ i++;
6086
+ upperOpen = true;
6087
+ }
6088
+ }
6089
+ const isRecord = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
6090
+ /**
6091
+ * A keyed container's elements in reading order. Order is a pure function of the materialized
6092
+ * value: elements sort by their `~pos` string, ties broken by key. An element whose `~pos` is
6093
+ * missing or not a string is ordered as if its position were the empty string (it sorts first,
6094
+ * key breaking the tie), so a peer that dropped the position field still lands somewhere
6095
+ * deterministic on every replica.
6096
+ */
6097
+ function orderedEntries(container) {
6098
+ const entries = [];
6099
+ for (const key of Object.keys(container)) {
6100
+ const value = container[key];
6101
+ const raw = isRecord(value) ? value[POS_SEGMENT] : undefined;
6102
+ entries.push({ key, pos: typeof raw === 'string' ? raw : '', value });
6103
+ }
6104
+ entries.sort((a, b) => a.pos < b.pos ? -1 : a.pos > b.pos ? 1 : a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
6105
+ return entries;
6106
+ }
6107
+ const devError = (msg) => {
6108
+ if (typeof ngDevMode !== 'undefined' && ngDevMode)
6109
+ console.error(`[keyed-container] ${msg}`);
6110
+ };
6111
+ const neighborPositions = (entries, index) => {
6112
+ const clamped = Math.max(0, Math.min(index, entries.length));
6113
+ return [entries[clamped - 1]?.pos || undefined, entries[clamped]?.pos || undefined];
6114
+ };
6115
+ /**
6116
+ * Insert `value` under `key` at `index` in reading order (default: append). The position is
6117
+ * computed from the neighbors at that index, so the element lands where asked without renumbering
6118
+ * any sibling. A keyed container is a RECORD, never an array, so this is a per-key write the sync
6119
+ * layer diffs on its own. Returns the assigned position. Re-inserting an existing key overwrites it.
6120
+ */
6121
+ function insertElement(container, key, value, index) {
6122
+ if (POS_SEGMENT in value)
6123
+ devError(`insertElement: '${POS_SEGMENT}' is managed, drop it from the value`);
6124
+ const entries = orderedEntries(container()).filter((e) => e.key !== key);
6125
+ const [before, after] = neighborPositions(entries, index ?? entries.length);
6126
+ const pos = posBetween(before, after);
6127
+ container.update((c) => ({ ...c, [key]: { ...value, [POS_SEGMENT]: pos } }));
6128
+ return pos;
6129
+ }
6130
+ /**
6131
+ * Move the element at `key` to `index` in reading order. This writes ONLY the element's `~pos`
6132
+ * field, so it never conflicts with a concurrent edit to the same element's data (they land on
6133
+ * different paths and both survive). Returns the new position, or `undefined` if `key` is absent.
6134
+ */
6135
+ function moveElement(container, key, index) {
6136
+ const current = container()[key];
6137
+ if (current == null)
6138
+ return undefined;
6139
+ const entries = orderedEntries(container()).filter((e) => e.key !== key);
6140
+ const [before, after] = neighborPositions(entries, index);
6141
+ const pos = posBetween(before, after);
6142
+ container.update((c) => ({ ...c, [key]: { ...c[key], [POS_SEGMENT]: pos } }));
6143
+ return pos;
6144
+ }
6145
+ /** Remove the element at `key`. Deletes the whole element (a per-key delete the sync layer folds). */
6146
+ function removeElement(container, key) {
6147
+ container.update((c) => {
6148
+ if (!(key in c))
6149
+ return c;
6150
+ const next = { ...c };
6151
+ delete next[key];
6152
+ return next;
6153
+ });
6154
+ }
6155
+ /**
6156
+ * Reassign every element's position to a fresh, evenly spaced sequence, as an authority write:
6157
+ * each `~pos` set is epoch-bumped so it wins the merge against any concurrent move, while leaving
6158
+ * concurrent edits to element DATA untouched (only the `~pos` fields are written). Use this to
6159
+ * reclaim precision after many same-gap inserts. Existing reading order is preserved.
6160
+ */
6161
+ function rebalanceContainer(sync, container) {
6162
+ const order = orderedEntries(container());
6163
+ const positions = evenPositions(order.length);
6164
+ sync.override(() => {
6165
+ container.update((c) => {
6166
+ const next = { ...c };
6167
+ order.forEach(({ key }, i) => {
6168
+ next[key] = { ...c[key], [POS_SEGMENT]: positions[i] };
6169
+ });
6170
+ return next;
6171
+ });
6172
+ });
6173
+ }
6174
+ // `n` evenly spaced, order-preserving positions in (0, 1): fraction (i+1)/(n+1) encoded to enough
6175
+ // base-62 digits that consecutive fractions never collide. Compact, so precision is reclaimed.
6176
+ function evenPositions(n) {
6177
+ if (n === 0)
6178
+ return [];
6179
+ const digits = Math.floor(Math.log(n + 1) / Math.log(BASE)) + 2;
6180
+ const out = [];
6181
+ for (let i = 0; i < n; i++) {
6182
+ let f = (i + 1) / (n + 1);
6183
+ let s = '';
6184
+ for (let k = 0; k < digits; k++) {
6185
+ f *= BASE;
6186
+ const d = Math.min(BASE - 1, Math.floor(f));
6187
+ s += DIGITS[d];
6188
+ f -= d;
6189
+ }
6190
+ out.push(s);
6191
+ }
6192
+ return out;
6193
+ }
5392
6194
 
5393
6195
  /**
5394
6196
  * Undo/redo for a copy-on-write store, built on the op-log: each tracked change is stored as
@@ -5659,7 +6461,7 @@ const isPlainArray = (v) => Array.isArray(v) && !isOpaque(v);
5659
6461
  function keyOf(item, key) {
5660
6462
  if (typeof key === 'function')
5661
6463
  return key(item);
5662
- return isRecord(item) ? item[key] : item;
6464
+ return isRecord$1(item) ? item[key] : item;
5663
6465
  }
5664
6466
  /**
5665
6467
  * Produces a value equal to `next` but sharing as much of `prev`'s reference structure as possible:
@@ -5688,7 +6490,7 @@ function reconcileValue(prev, next, key) {
5688
6490
  });
5689
6491
  return changed ? out : prev;
5690
6492
  }
5691
- if (isRecord(prev) && isRecord(next)) {
6493
+ if (isRecord$1(prev) && isRecord$1(next)) {
5692
6494
  const nextKeys = Object.keys(next);
5693
6495
  let changed = Object.keys(prev).length !== nextKeys.length;
5694
6496
  const out = {};
@@ -5974,7 +6776,7 @@ function storeTabSync(sig, opt, bus, injector) {
5974
6776
  const covered = Object.entries(snap.wm).every(([origin, v]) => (msg.wm[origin] ?? 0) >= v);
5975
6777
  post(covered
5976
6778
  ? { t: 'uptodate', to: msg.from }
5977
- : { t: 'state', to: msg.from, root: snap.root, wm: snap.wm });
6779
+ : { t: 'state', to: msg.from, state: snap });
5978
6780
  }, Math.random() * jitterMs);
5979
6781
  responseTimers.set(msg.from, timer);
5980
6782
  return;
@@ -5989,7 +6791,7 @@ function storeTabSync(sig, opt, bus, injector) {
5989
6791
  if (msg.to !== sync.origin || phase !== 'joining')
5990
6792
  return;
5991
6793
  if (msg.t === 'state')
5992
- sync.hydrate(msg.root, msg.wm);
6794
+ sync.hydrate(msg.state);
5993
6795
  goLive();
5994
6796
  return;
5995
6797
  }
@@ -6363,5 +7165,5 @@ function withHistory(sourceOrValue, opt) {
6363
7165
  * Generated bundle index. Do not edit.
6364
7166
  */
6365
7167
 
6366
- export { CONCURRENCY_INSTRUMENTATION, MmActivity, MmTransition, MmViewTransitionName, OP_PROTO_VERSION, PAUSABLE_OPTIONS, PERSISTED_STORE_OPTIONS, SuspenseBoundary, SuspenseBoundaryBase, UnscopedSuspenseBoundary, activeTransaction, applyOps, batteryStatus, bridgeScopeToPendingTasks, chunked, clipboard, combineWith, compareHlc, compareTotal, createAttributedPending, createConvergingApply, createForwardingScope, createHlcClock, createStoreContext, createTransaction, createTransitionScope, debounce, debounced, deferredValue, derived, diffOps, distinct, elementSize, elementVisibility, extendStore, filter, filterWith, focusWithin, forkStore, geolocation, getTransitionScope, holdUntilReady, idle, indexArray, injectPaused, injectRegisterResource, injectStartTransaction, injectStartTransition, injectTransitionScope, invertBatch, isConflicted, isDerivation, isLeaf, isMutable, isOpaque, isStore, keepPrevious, keyArray, keyedArray, latest, lww, map, mapArray, mapObject, mediaQuery, merge3, mergeThree, mousePosition, mutable, mutableStore, nestedEffect, networkStatus, opLog, opSync, opaque, orientation, pageVisibility, pairwise, pausableComputed, pausableEffect, pausableSignal, perfCustomTracks, persist, persistedStore, pipeable, piped, pointerDrag, policyStrategy, pooled, pooledArray, pooledMap, pooledSet, prefersDarkMode, prefersReducedMotion, preserve, projection, provideConcurrencyInstrumentation, provideForwardingTransitionScope, providePausableOptions, providePaused, providePersistedStoreOptions, provideTransitionScope, rebaseOps, reconcile, registerResource, resolvePause, scan, scrollPosition, select, sensor, sensors, signalFromEvent, startWith, store, storeHistory, stored, tabSync, tap, throttle, throttled, toFakeDerivation, toFakeSignalDerivation, toStore, toWritable, until, use, windowSize, withHistory };
7168
+ export { CONCURRENCY_INSTRUMENTATION, MmActivity, MmTransition, MmViewTransitionName, OP_PROTO_VERSION, PAUSABLE_OPTIONS, PERSISTED_STORE_OPTIONS, POS_SEGMENT, SuspenseBoundary, SuspenseBoundaryBase, UnscopedSuspenseBoundary, activeTransaction, applyOps, batteryStatus, bridgeScopeToPendingTasks, chunked, clipboard, combineWith, compareHlc, compareSiblings, compareTotal, createAttributedPending, createConvergingApply, createForwardingScope, createHlcClock, createStoreContext, createTransaction, createTransitionScope, debounce, debounced, defaultFold, deferredValue, derived, diffOps, distinct, elementSize, elementVisibility, extendStore, filter, filterWith, focusWithin, forkStore, geolocation, getTransitionScope, holdUntilReady, idle, indexArray, injectPaused, injectRegisterResource, injectStartTransaction, injectStartTransition, injectTransitionScope, insertElement, invertBatch, isConflicted, isDerivation, isLeaf, isMutable, isOpaque, isStore, keepPrevious, keyArray, keyedArray, latest, lww, map, mapArray, mapObject, mediaQuery, merge3, mergeThree, mousePosition, moveElement, mutable, mutableStore, nestedEffect, networkStatus, opLog, opSync, opaque, orderedEntries, orientation, pageVisibility, pairwise, pausableComputed, pausableEffect, pausableSignal, perfCustomTracks, persist, persistedStore, pipeable, piped, pointerDrag, policyStrategy, pooled, pooledArray, pooledMap, pooledSet, posBetween, prefersDarkMode, prefersReducedMotion, preserve, projection, provideConcurrencyInstrumentation, provideForwardingTransitionScope, providePausableOptions, providePaused, providePersistedStoreOptions, provideTransitionScope, rebalanceContainer, rebaseOps, reconcile, registerResource, removeElement, resolvePause, scan, scrollPosition, select, sensor, sensors, signalFromEvent, startWith, store, storeHistory, stored, syncedFork, tabSync, tap, throttle, throttled, toFakeDerivation, toFakeSignalDerivation, toStore, toWritable, until, use, validateEnvelope, windowSize, withHistory };
6367
7169
  //# sourceMappingURL=mmstack-primitives.mjs.map