@dxos/echo 0.16.0 → 0.17.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.
Files changed (176) hide show
  1. package/dist/lib/Annotation.mjs +2 -2
  2. package/dist/lib/Database.mjs +1 -1
  3. package/dist/lib/Error.mjs +1 -1
  4. package/dist/lib/Feed.mjs +1 -1
  5. package/dist/lib/Format.mjs +2 -2
  6. package/dist/lib/Migration.mjs +1 -1
  7. package/dist/lib/chunk-Annotation.mjs +14 -11
  8. package/dist/lib/chunk-Annotation.mjs.map +1 -1
  9. package/dist/lib/chunk-Blob.mjs +1 -1
  10. package/dist/lib/chunk-Collection.mjs +1 -1
  11. package/dist/lib/chunk-Database.mjs +93 -7
  12. package/dist/lib/chunk-Database.mjs.map +1 -1
  13. package/dist/lib/chunk-Dataset.mjs +1 -1
  14. package/dist/lib/chunk-Entity.mjs +2 -2
  15. package/dist/lib/chunk-Entity.mjs.map +1 -1
  16. package/dist/lib/chunk-Error.mjs +13 -1
  17. package/dist/lib/chunk-Error.mjs.map +1 -1
  18. package/dist/lib/chunk-Feed.mjs +311 -17
  19. package/dist/lib/chunk-Feed.mjs.map +1 -1
  20. package/dist/lib/chunk-Filter.mjs +4 -3
  21. package/dist/lib/chunk-Filter.mjs.map +1 -1
  22. package/dist/lib/chunk-Format.mjs +2 -1
  23. package/dist/lib/chunk-Hypergraph.mjs +1 -1
  24. package/dist/lib/chunk-Hypergraph.mjs.map +1 -1
  25. package/dist/lib/chunk-Migration.mjs +155 -5
  26. package/dist/lib/chunk-Migration.mjs.map +1 -1
  27. package/dist/lib/chunk-Obj.mjs +12 -5
  28. package/dist/lib/chunk-Obj.mjs.map +1 -1
  29. package/dist/lib/chunk-Query.mjs +2 -2
  30. package/dist/lib/chunk-QueryResult.mjs.map +1 -1
  31. package/dist/lib/chunk-Ref2.mjs +2 -2
  32. package/dist/lib/chunk-Registry.mjs +1 -1
  33. package/dist/lib/chunk-Registry.mjs.map +1 -1
  34. package/dist/lib/chunk-Relation.mjs +5 -5
  35. package/dist/lib/chunk-Relation.mjs.map +1 -1
  36. package/dist/lib/chunk-Scope.mjs +16 -2
  37. package/dist/lib/chunk-Scope.mjs.map +1 -1
  38. package/dist/lib/chunk-Tag.mjs +4 -4
  39. package/dist/lib/chunk-Tag.mjs.map +1 -1
  40. package/dist/lib/chunk-Text.mjs +2 -2
  41. package/dist/lib/chunk-Type.mjs +26 -7
  42. package/dist/lib/chunk-Type.mjs.map +1 -1
  43. package/dist/lib/chunk-View.mjs +1 -1
  44. package/dist/lib/chunk-atom-snapshot.mjs +29 -1
  45. package/dist/lib/chunk-atom-snapshot.mjs.map +1 -1
  46. package/dist/lib/chunk-entity-dictionary.mjs +28 -19
  47. package/dist/lib/chunk-entity-dictionary.mjs.map +1 -1
  48. package/dist/lib/chunk-internal.mjs +231 -79
  49. package/dist/lib/chunk-internal.mjs.map +1 -1
  50. package/dist/lib/chunk-json-schema.mjs +1 -1
  51. package/dist/lib/chunk-ref.mjs +38 -34
  52. package/dist/lib/chunk-ref.mjs.map +1 -1
  53. package/dist/lib/chunk-select.mjs +7 -3
  54. package/dist/lib/chunk-select.mjs.map +1 -1
  55. package/dist/lib/index.mjs +2 -2
  56. package/dist/lib/internal.mjs +6 -6
  57. package/dist/lib/testing.mjs +2 -2
  58. package/dist/lib/testing.mjs.map +1 -1
  59. package/dist/types/src/Annotation.d.ts +1 -1
  60. package/dist/types/src/Annotation.d.ts.map +1 -1
  61. package/dist/types/src/Database.d.ts +30 -13
  62. package/dist/types/src/Database.d.ts.map +1 -1
  63. package/dist/types/src/Entity.d.ts +3 -4
  64. package/dist/types/src/Entity.d.ts.map +1 -1
  65. package/dist/types/src/Error.d.ts +36 -0
  66. package/dist/types/src/Error.d.ts.map +1 -1
  67. package/dist/types/src/Feed.d.ts +261 -18
  68. package/dist/types/src/Feed.d.ts.map +1 -1
  69. package/dist/types/src/Filter.d.ts +7 -1
  70. package/dist/types/src/Filter.d.ts.map +1 -1
  71. package/dist/types/src/Hypergraph.d.ts +1 -1
  72. package/dist/types/src/Migration.d.ts +213 -2
  73. package/dist/types/src/Migration.d.ts.map +1 -1
  74. package/dist/types/src/Migration.test.d.ts +2 -0
  75. package/dist/types/src/Migration.test.d.ts.map +1 -0
  76. package/dist/types/src/Obj.d.ts +4 -4
  77. package/dist/types/src/Obj.d.ts.map +1 -1
  78. package/dist/types/src/QueryResult.d.ts +16 -0
  79. package/dist/types/src/QueryResult.d.ts.map +1 -1
  80. package/dist/types/src/Relation.d.ts +3 -4
  81. package/dist/types/src/Relation.d.ts.map +1 -1
  82. package/dist/types/src/Scope.d.ts +32 -1
  83. package/dist/types/src/Scope.d.ts.map +1 -1
  84. package/dist/types/src/Tag.d.ts.map +1 -1
  85. package/dist/types/src/Type.d.ts +27 -2
  86. package/dist/types/src/Type.d.ts.map +1 -1
  87. package/dist/types/src/internal/Annotation/annotations.d.ts +35 -1
  88. package/dist/types/src/internal/Annotation/annotations.d.ts.map +1 -1
  89. package/dist/types/src/internal/Annotation/atoms.d.ts +2 -2
  90. package/dist/types/src/internal/Annotation/atoms.d.ts.map +1 -1
  91. package/dist/types/src/internal/Entity/api.d.ts +11 -0
  92. package/dist/types/src/internal/Entity/api.d.ts.map +1 -1
  93. package/dist/types/src/internal/Entity/model.d.ts +3 -2
  94. package/dist/types/src/internal/Entity/model.d.ts.map +1 -1
  95. package/dist/types/src/internal/Format/string.d.ts +5 -1
  96. package/dist/types/src/internal/Format/string.d.ts.map +1 -1
  97. package/dist/types/src/internal/Obj/atoms.d.ts +1 -1
  98. package/dist/types/src/internal/Obj/atoms.d.ts.map +1 -1
  99. package/dist/types/src/internal/Obj/json-serializer.d.ts.map +1 -1
  100. package/dist/types/src/internal/Query/pretty.d.ts.map +1 -1
  101. package/dist/types/src/internal/Ref/ref.d.ts +5 -1
  102. package/dist/types/src/internal/Ref/ref.d.ts.map +1 -1
  103. package/dist/types/src/internal/Ref/strong-deps.d.ts +3 -1
  104. package/dist/types/src/internal/Ref/strong-deps.d.ts.map +1 -1
  105. package/dist/types/src/internal/common/api/meta.d.ts +4 -5
  106. package/dist/types/src/internal/common/api/meta.d.ts.map +1 -1
  107. package/dist/types/src/internal/common/atom-snapshot.d.ts +10 -0
  108. package/dist/types/src/internal/common/atom-snapshot.d.ts.map +1 -1
  109. package/dist/types/src/internal/common/proxy/change-context.d.ts +4 -0
  110. package/dist/types/src/internal/common/proxy/change-context.d.ts.map +1 -1
  111. package/dist/types/src/internal/common/proxy/ownership.d.ts +5 -0
  112. package/dist/types/src/internal/common/proxy/ownership.d.ts.map +1 -1
  113. package/dist/types/src/internal/common/proxy/proxy-utils.d.ts.map +1 -1
  114. package/dist/types/src/internal/common/proxy/schema-validator.d.ts +17 -2
  115. package/dist/types/src/internal/common/proxy/schema-validator.d.ts.map +1 -1
  116. package/dist/types/src/internal/common/proxy/symbols.d.ts +10 -0
  117. package/dist/types/src/internal/common/proxy/symbols.d.ts.map +1 -1
  118. package/dist/types/src/internal/common/proxy/typed-handler.d.ts +0 -1
  119. package/dist/types/src/internal/common/proxy/typed-handler.d.ts.map +1 -1
  120. package/dist/types/src/internal/common/types/meta.d.ts +8 -4
  121. package/dist/types/src/internal/common/types/meta.d.ts.map +1 -1
  122. package/dist/types/src/internal/common/types/model-symbols.d.ts +6 -0
  123. package/dist/types/src/internal/common/types/model-symbols.d.ts.map +1 -1
  124. package/dist/types/src/internal/index.d.ts.map +1 -1
  125. package/dist/types/tsconfig.tsbuildinfo +1 -1
  126. package/package.json +19 -15
  127. package/src/Annotation.test.ts +34 -0
  128. package/src/Annotation.ts +3 -0
  129. package/src/Database.ts +46 -15
  130. package/src/Entity.ts +3 -4
  131. package/src/Error.ts +9 -0
  132. package/src/Feed.test.ts +160 -0
  133. package/src/Feed.ts +412 -29
  134. package/src/Filter.ts +8 -1
  135. package/src/Hypergraph.ts +1 -1
  136. package/src/Migration.test.ts +137 -0
  137. package/src/Migration.ts +376 -4
  138. package/src/Obj.test.ts +44 -4
  139. package/src/Obj.ts +19 -5
  140. package/src/QueryResult.ts +14 -0
  141. package/src/Relation.ts +3 -4
  142. package/src/Scope.ts +43 -1
  143. package/src/Tag.md +5 -6
  144. package/src/Tag.ts +3 -2
  145. package/src/Type.test.ts +52 -0
  146. package/src/Type.ts +75 -22
  147. package/src/Type.tst.ts +155 -0
  148. package/src/internal/Annotation/annotations.test.ts +19 -0
  149. package/src/internal/Annotation/annotations.ts +67 -1
  150. package/src/internal/Annotation/atoms.ts +10 -9
  151. package/src/internal/Entity/api.ts +20 -1
  152. package/src/internal/Entity/model.ts +3 -0
  153. package/src/internal/Filter/match.ts +2 -2
  154. package/src/internal/Format/string.ts +7 -2
  155. package/src/internal/JsonSchema/json-schema.test.ts +2 -2
  156. package/src/internal/Obj/atoms.ts +9 -5
  157. package/src/internal/Obj/json-serializer.test.ts +34 -7
  158. package/src/internal/Obj/json-serializer.ts +18 -3
  159. package/src/internal/Query/pretty.ts +3 -2
  160. package/src/internal/Ref/ref.test.ts +27 -2
  161. package/src/internal/Ref/ref.ts +18 -2
  162. package/src/internal/Ref/strong-deps.ts +21 -2
  163. package/src/internal/common/api/meta.ts +7 -8
  164. package/src/internal/common/atom-snapshot.ts +61 -0
  165. package/src/internal/common/proxy/change-context.ts +5 -0
  166. package/src/internal/common/proxy/change.test.ts +62 -31
  167. package/src/internal/common/proxy/json-serializer.ts +3 -2
  168. package/src/internal/common/proxy/ownership.ts +14 -1
  169. package/src/internal/common/proxy/proxy-utils.ts +7 -1
  170. package/src/internal/common/proxy/schema-validator.ts +116 -27
  171. package/src/internal/common/proxy/symbols.ts +12 -0
  172. package/src/internal/common/proxy/typed-handler.test.ts +43 -0
  173. package/src/internal/common/proxy/typed-handler.ts +105 -33
  174. package/src/internal/common/types/meta.ts +25 -20
  175. package/src/internal/common/types/model-symbols.ts +7 -0
  176. package/src/internal/index.ts +1 -2
@@ -34,6 +34,7 @@ import {
34
34
  getEchoRoot,
35
35
  getOwner,
36
36
  getRawTarget,
37
+ hasEntityElements,
37
38
  hasForeignOwner,
38
39
  notifyOwnerChain,
39
40
  setOwnerRecursive,
@@ -49,8 +50,8 @@ import {
49
50
  symbolReactivePrototype,
50
51
  } from './proxy-utils.ts';
51
52
  import { ReactiveArray } from './reactive-array.ts';
52
- import { SchemaValidator, assertsWithDetail } from './schema-validator.ts';
53
- import { ChangeId, ChangeKeyId, EventId } from './symbols.ts';
53
+ import { SchemaValidator, assertsWithDetail, isEntityArrayAst } from './schema-validator.ts';
54
+ import { ChangeId, ChangeKeyId, EntityElementsId, EventId } from './symbols.ts';
54
55
 
55
56
  // Re-export for external consumers.
56
57
  export { getEchoRoot, setMetaOwner } from './ownership.ts';
@@ -105,6 +106,14 @@ const deepCopy = <T>(value: T, visited = new Map<object, object>()): T => {
105
106
  return visited.get(actualValue) as T;
106
107
  }
107
108
 
109
+ // Entities held by identity are shared, not copied.
110
+ if (hasEntityElements(actualValue)) {
111
+ const copy = toEntityArray(actualValue);
112
+ visited.set(actualValue, copy);
113
+ copyHiddenProperties(actualValue, copy);
114
+ return copy as T;
115
+ }
116
+
108
117
  // Handle ReactiveArray specially to preserve reactivity.
109
118
  if (actualValue instanceof ReactiveArray) {
110
119
  const copy = new ReactiveArray<any>();
@@ -157,6 +166,16 @@ const setDeepOnRawTargets = (target: object, path: readonly (string | number)[],
157
166
  parent[path[path.length - 1]] = value;
158
167
  };
159
168
 
169
+ /** A fresh reactive array of the given entities, marked so they stay held by identity. */
170
+ const toEntityArray = (items: readonly unknown[]): ReactiveArray<unknown> => {
171
+ const array = new ReactiveArray<unknown>();
172
+ for (const item of items) {
173
+ array[array.length] = item;
174
+ }
175
+ defineHiddenProperty(array, EntityElementsId, true);
176
+ return array;
177
+ };
178
+
160
179
  /**
161
180
  * Copy hidden properties (SchemaId, SchemaAstId, TypeId) from source to target.
162
181
  */
@@ -172,6 +191,20 @@ const copyHiddenProperties = (source: any, target: any): void => {
172
191
  }
173
192
  };
174
193
 
194
+ /**
195
+ * Node inspector for typed targets. Forces `showHidden: false` so a target's hidden metadata, including its owner and
196
+ * through it unrelated fields of the root, never prints.
197
+ */
198
+ function inspectTypedTarget(
199
+ this: ProxyTarget,
200
+ _depth: number,
201
+ options: InspectOptionsStylized,
202
+ inspectFn: (value: any, options?: InspectOptionsStylized) => string,
203
+ ): string {
204
+ const inspected = inspectFn(getRawTarget(this), { ...options, showHidden: false, customInspect: false });
205
+ return `Typed ${inspected}`;
206
+ }
207
+
175
208
  //
176
209
  // Object layering for an in-memory typed reactive object (`TypedReactiveHandler`).
177
210
  //
@@ -183,7 +216,7 @@ const copyHiddenProperties = (source: any, target: any): void => {
183
216
  // │ set/has/... traps run here; there is no `get` trap, so reads land on the target
184
217
  // ▼
185
218
  // target the user's object. OWN enumerable props = user data ({ name, age, ... });
186
- // │ [[Prototype]] OWN symbol props are removed by `compactMetadataToInstanceState` after init.
219
+ // │ [[Prototype]] a root's OWN symbol props are removed by `compactMetadataToInstanceState` after init.
187
220
  // ▼
188
221
  // instanceState `Object.create(TypedObjectPrototype)`. Holds the relocated symbol-keyed
189
222
  // │ [[Prototype]] metadata as hidden props: [EventId] (root only), [ObjectDeletedId], [SchemaId],
@@ -200,7 +233,9 @@ const copyHiddenProperties = (source: any, target: any): void => {
200
233
  //
201
234
  // Root vs nested: a root object owns an `[EventId]`; nested records share their root's reactivity
202
235
  // and have none. That presence is the "is this an initialized root" signal (it replaced an earlier
203
- // `[ChangeId] === true` marker).
236
+ // `[ChangeId] === true` marker). Only a root gets an instanceState: a nested record never changes
237
+ // handler, so it keeps its metadata as own properties and points straight at TypedObjectPrototype
238
+ // (`shareTypedObjectPrototype`).
204
239
  //
205
240
  // `this` in the accessors below: the get trap calls `Reflect.get(target, prop, receiver)`, so
206
241
  // `this` is the PROXY (receiver) on the common path, or the raw target when read directly.
@@ -208,11 +243,12 @@ const copyHiddenProperties = (source: any, target: any): void => {
208
243
  // metadata chain both hang off the raw target, and `executeChange` keys the change context by it.
209
244
  //
210
245
  /**
211
- * Behaviour prototype for typed reactive records. A record's per-object metadata is moved onto an
246
+ * Behaviour prototype for typed reactive records. A root's per-object metadata is moved onto an
212
247
  * intermediate instance-state object whose prototype is this, so that swapping the handler that
213
248
  * backs an object (e.g. an in-memory object becoming database-backed) is a prototype re-point that
214
- * doesn't perturb the target's own-property shape. Marked as a reactive prototype so the "plain
215
- * object" gates (`isValidProxyTarget` / `deepCopy`) still treat such targets as data records.
249
+ * doesn't perturb the target's own-property shape. A nested record never swaps handler, so it points
250
+ * here directly and keeps its metadata as own properties. Marked as a reactive prototype so the
251
+ * "plain object" gates (`isValidProxyTarget` / `deepCopy`) still treat such targets as data records.
216
252
  */
217
253
  const TypedObjectPrototype: object = Object.create(Object.prototype);
218
254
  defineHiddenProperty(TypedObjectPrototype, symbolReactivePrototype, true);
@@ -256,6 +292,8 @@ Object.defineProperties(TypedObjectPrototype, {
256
292
  : (that: unknown) => getRawTarget(that) === target;
257
293
  },
258
294
  },
295
+ // One shared inspector rather than a bound function per record: a nested JSON Schema alone is dozens of records.
296
+ [inspectCustom]: { value: inspectTypedTarget },
259
297
  [StaticTypeSchemaSlot]: {
260
298
  // Lazily rebuild the source Effect Schema from `jsonSchema` and cache it as an own (hidden)
261
299
  // property so subsequent reads short-circuit this accessor; the set-trap deletes the cache when
@@ -305,6 +343,22 @@ const compactMetadataToInstanceState = (target: ProxyTarget): void => {
305
343
  Object.setPrototypeOf(target, state);
306
344
  };
307
345
 
346
+ /**
347
+ * Point a nested record straight at {@link TypedObjectPrototype}, keeping its metadata as own properties.
348
+ *
349
+ * Unlike {@link compactMetadataToInstanceState} this keeps the record in V8's fast-property mode: deleting the moved
350
+ * properties would turn the target into a dictionary, and so would making the per-record state a prototype. Both
351
+ * cost ~0.5 KiB a record, which for an object holding JSON Schema (one record per schema node) is most of its heap.
352
+ */
353
+ const shareTypedObjectPrototype = (target: ProxyTarget): void => {
354
+ // `isValidProxyTarget` admits only records rooted at `Object.prototype` or at a prototype carrying
355
+ // `symbolReactivePrototype`, which only `TypedObjectPrototype` sets, so a record skipped here already inherits it.
356
+ if (Array.isArray(target) || Object.getPrototypeOf(target) !== Object.prototype) {
357
+ return;
358
+ }
359
+ Object.setPrototypeOf(target, TypedObjectPrototype);
360
+ };
361
+
308
362
  /**
309
363
  * Typed in-memory reactive store (with Schema).
310
364
  * Reactivity is based on Event subscriptions, not signals.
@@ -328,8 +382,8 @@ export class TypedReactiveHandler implements ReactiveHandler<ProxyTarget> {
328
382
  }
329
383
 
330
384
  // Default to not-deleted, but preserve a deletion marker already stamped on the target (e.g. by
331
- // `objectFromJSON` when hydrating a tombstone snapshot).
332
- if (!(ObjectDeletedId in target)) {
385
+ // `objectFromJSON` when hydrating a tombstone snapshot). Only entities are deleted, never a nested record.
386
+ if (!hasOwner && !(ObjectDeletedId in target)) {
333
387
  defineHiddenProperty(target, ObjectDeletedId, false);
334
388
  }
335
389
 
@@ -358,16 +412,17 @@ export class TypedReactiveHandler implements ReactiveHandler<ProxyTarget> {
358
412
  }
359
413
  }
360
414
 
361
- // Maybe have been set by `create`.
362
- Object.defineProperty(target, inspectCustom, {
363
- enumerable: false,
364
- configurable: true,
365
- value: this._inspect.bind(target),
366
- });
367
-
368
- // Relocate per-object metadata onto an instance-state prototype, leaving only user data as own
369
- // properties on the target. Done last so all metadata set above (and by `prepareTypedTarget`) moves.
370
- compactMetadataToInstanceState(target);
415
+ // Relocate a root's metadata onto an instance-state prototype, leaving only user data as own properties on the
416
+ // target. Done last so all metadata set above (and by `prepareTypedTarget`) moves. A nested record never changes
417
+ // handler, so it needs no state of its own: see `shareTypedObjectPrototype`.
418
+ if (Array.isArray(target)) {
419
+ // Arrays keep their `ReactiveArray` prototype, so they carry the shared inspector themselves.
420
+ defineHiddenProperty(target, inspectCustom, inspectTypedTarget);
421
+ } else if (hasOwner) {
422
+ shareTypedObjectPrototype(target);
423
+ } else {
424
+ compactMetadataToInstanceState(target);
425
+ }
371
426
 
372
427
  // After compaction and ownership, so each child is stamped and owned before it gets a proxy of its
373
428
  // own. Recursive: wrapping a child runs its `init`, which wraps that child's children.
@@ -519,6 +574,17 @@ export class TypedReactiveHandler implements ReactiveHandler<ProxyTarget> {
519
574
  return { echoRoot, preparedValue: value };
520
575
  }
521
576
 
577
+ // An element of an entity array is a reference to another root: validated, never owned or copied.
578
+ if (hasEntityElements(target)) {
579
+ return { echoRoot, preparedValue: this._validateValue(target, prop, value) };
580
+ }
581
+ if (Array.isArray(value) && isEntityArrayAst(SchemaValidator.getTargetPropertyAst(target, prop))) {
582
+ const entities = toEntityArray(value);
583
+ const validated = this._validateValue(target, prop, entities);
584
+ setOwnerRecursive(validated, echoRoot);
585
+ return { echoRoot, preparedValue: createProxy(validated, this) };
586
+ }
587
+
522
588
  // Check for cycles before assignment.
523
589
  if (isValidProxyTarget(value) || isProxy(value)) {
524
590
  if (wouldCreateCycle(echoRoot, value)) {
@@ -587,20 +653,6 @@ export class TypedReactiveHandler implements ReactiveHandler<ProxyTarget> {
587
653
 
588
654
  return value;
589
655
  }
590
-
591
- private _inspect(
592
- _: number,
593
- options: InspectOptionsStylized,
594
- inspectFn: (value: any, options?: InspectOptionsStylized) => string,
595
- ): string {
596
- const inspected = inspectFn(this, {
597
- ...options,
598
- showHidden: false,
599
- customInspect: false,
600
- });
601
-
602
- return `Typed ${inspected}`;
603
- }
604
656
  }
605
657
 
606
658
  /**
@@ -668,6 +720,9 @@ const setNestedSchema = (obj: any, ast: SchemaAST.AST) => {
668
720
  };
669
721
 
670
722
  const setChildSchemas = (obj: any, ast: SchemaAST.AST) => {
723
+ if (hasEntityElements(obj)) {
724
+ return;
725
+ }
671
726
  if (Array.isArray(obj)) {
672
727
  for (let index = 0; index < obj.length; index++) {
673
728
  if (isValidProxyTarget(obj[index])) {
@@ -722,9 +777,23 @@ export const validateAndReactifyTarget = <T>(target: T, schema: Schema.Schema<T>
722
777
  assertsWithDetail(schema, target);
723
778
  SchemaValidator.assertExactProperties(schema.ast, target, (path) => getDeep(target, path));
724
779
  }
780
+ markEntityArrays(target, schema.ast);
725
781
  makeArraysReactive(target);
726
782
  };
727
783
 
784
+ /** Marks the target's top-level entity-array properties (e.g. `meta.tags`) as held by identity. */
785
+ const markEntityArrays = (target: any, ast: SchemaAST.AST) => {
786
+ if (!SchemaAST.isObjects(ast)) {
787
+ return;
788
+ }
789
+ for (const property of ast.propertySignatures) {
790
+ const value = target[property.name];
791
+ if (Array.isArray(value) && !hasEntityElements(value) && isEntityArrayAst(property.type)) {
792
+ target[property.name] = toEntityArray(value);
793
+ }
794
+ }
795
+ };
796
+
728
797
  /**
729
798
  * Like {@link prepareTypedTarget}, for a target whose own `SchemaId`/`TypeId` are already stamped
730
799
  * and locked `configurable: false` — e.g. an object decoded from JSON via `setSchema`/`setTypename`
@@ -750,6 +819,9 @@ const makeArraysReactive = (target: any) => {
750
819
  if (value == null || typeof value !== 'object' || isProxy(value)) {
751
820
  continue;
752
821
  }
822
+ if (hasEntityElements(value)) {
823
+ continue;
824
+ }
753
825
  if (Array.isArray(value) && !(value instanceof ReactiveArray)) {
754
826
  target[key] = ReactiveArray.from(value);
755
827
  }
@@ -4,18 +4,18 @@
4
4
 
5
5
  import * as Schema from 'effect/Schema';
6
6
 
7
- import { type EncodedReference, ForeignKey } from '@dxos/echo-protocol';
7
+ import { ForeignKey } from '@dxos/echo-protocol';
8
8
  import { invariant } from '@dxos/invariant';
9
9
  import { DXN } from '@dxos/keys';
10
10
  import { type Comparator, intersection } from '@dxos/util';
11
11
 
12
12
  import type * as Tag from '../../../Tag.ts';
13
13
  import { Dictionary } from '../../Annotation/dictionary.ts';
14
- // `meta` is no longer re-exported from the `common/types` barrel (see ./index.ts), so importing the
15
- // Ref schema builder here no longer forms an eval-order cycle with `Annotation`/`Database`.
16
- import { type Ref, createEchoReferenceSchema } from '../../Ref/ref.ts';
14
+ import { EntityValueAnnotationId } from '../proxy/symbols.ts';
17
15
  import { type AnyProperties } from './base.ts';
16
+ import { KindId } from './entity.ts';
18
17
  import { MetaId } from './model-symbols.ts';
18
+ import { TypeId } from './typename.ts';
19
19
  import { TagTypeDXN } from './well-known-types.ts';
20
20
 
21
21
  /**
@@ -28,29 +28,34 @@ export const ATTR_META = '@meta';
28
28
  //
29
29
 
30
30
  /**
31
- * Schema for references to {@link Tag} objects stored in {@link EntityMetaSchema.tags}.
32
- *
33
- * Built from the shared {@link createEchoReferenceSchema} (the same builder `Ref.Ref` uses) via
34
- * `Schema.suspend`, so it reuses the canonical ref codec rather than duplicating it. The Tag type
35
- * identity comes from the shared {@link TagTypeDXN} constant; `suspend` defers construction until
36
- * first use, and `Tag` is referenced type-only, so no `Tag` value import is needed.
31
+ * Whether the value is a {@link Tag} entity. Read off the entity's own kind and type stamps, so
32
+ * neither `Tag` nor the annotation helpers are imported here (both would form an eval-order cycle).
37
33
  */
38
- const TagRefSchema = Schema.suspend(
39
- (): Schema.Codec<Ref<Tag.Tag>, EncodedReference> =>
40
- // The factory yields a loosely-typed `Ref<any>` schema; narrow it to the Tag-typed ref.
41
- createEchoReferenceSchema(undefined, DXN.getName(TagTypeDXN), DXN.getVersion(TagTypeDXN)) as Schema.Codec<
42
- Ref<Tag.Tag>,
43
- EncodedReference
44
- >,
45
- );
34
+ export const isTagEntity = (value: unknown): value is Tag.Tag => {
35
+ if (value == null || typeof value !== 'object' || Reflect.get(value, KindId) === undefined) {
36
+ return false;
37
+ }
38
+ const type = Reflect.get(value, TypeId);
39
+ const dxn = DXN.isDXN(type) ? DXN.tryMake(type) : undefined;
40
+ return dxn != null && DXN.getName(dxn) === DXN.getName(TagTypeDXN);
41
+ };
42
+
43
+ /**
44
+ * Schema for the {@link Tag} entities in {@link EntityMetaSchema.tags}: a strong dependency, held by
45
+ * identity like a relation endpoint, and persisted as a reference.
46
+ */
47
+ const TagEntitySchema = Schema.declare(isTagEntity, {
48
+ identifier: 'Tag',
49
+ [EntityValueAnnotationId]: true,
50
+ });
46
51
 
47
52
  export const EntityMetaSchema = Schema.Struct({
48
53
  keys: Schema.Array(ForeignKey),
49
54
 
50
55
  /**
51
- * Tags applied to this entity, as references to {@link Tag} objects.
56
+ * Tags applied to this entity: {@link Tag} objects, loaded with the entity as strong dependencies.
52
57
  */
53
- tags: Schema.Array(TagRefSchema),
58
+ tags: Schema.Array(TagEntitySchema),
54
59
 
55
60
  /**
56
61
  * Fully-qualified registry key for the object (FQN format, e.g. `com.example.type.foo`).
@@ -50,6 +50,13 @@ export const ObjectVersionId: unique symbol = Symbol.for('@dxos/echo/Version');
50
50
  */
51
51
  export const ObjectDatabaseId = Symbol.for('@dxos/echo/Database');
52
52
 
53
+ /**
54
+ * Hidden, non-reactive feed block keys of a live feed object this client wrote: what a read of its
55
+ * latest block would stamp in `@meta.keys`, set when the store confirms the write. Read via the `Feed`
56
+ * block getters, which prefer it to `@meta.keys`.
57
+ */
58
+ export const FeedBlockKeysId = Symbol.for('@dxos/echo/FeedBlockKeys');
59
+
53
60
  /**
54
61
  * Branch accessor symbol. Resolves to the branch name this object instance is bound to (`'main'` for
55
62
  * the canonical object, or the branch of a `db.branch()` independent instance). Read via `Obj.getBranch`.
@@ -15,6 +15,5 @@ export * from './Ref/index.ts';
15
15
  export * from './Type/index.ts';
16
16
 
17
17
  // Re-exported here (not from the low-level `common/types` barrel) so consumers keep importing meta
18
- // from `@dxos/echo/internal`, while that barrel stays free of the Ref/Annotation eval cycle. Placed
19
- // after `./Ref` so the ref schema `meta` depends on is initialized first.
18
+ // from `@dxos/echo/internal`, while that barrel stays free of the Annotation eval cycle.
20
19
  export * from './common/types/meta.ts';