@coasys/ad4m 0.13.0-test-interpretation-2 → 0.13.0-test-model-layer

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 (254) hide show
  1. package/lib/index.cjs +1982 -498
  2. package/lib/index.js +1982 -499
  3. package/lib/src/ai/AIClient.js +6 -3
  4. package/lib/src/apiClient.d.ts +40 -4
  5. package/lib/src/apiClient.js +149 -9
  6. package/lib/src/index.d.ts +3 -1
  7. package/lib/src/index.js +2 -0
  8. package/lib/src/model/Ad4mModel.d.ts +6 -5
  9. package/lib/src/model/Ad4mModel.js +246 -29
  10. package/lib/src/model/Ad4mModel.test.js +480 -735
  11. package/lib/src/model/decorators.d.ts +147 -4
  12. package/lib/src/model/decorators.js +70 -24
  13. package/lib/src/model/model-manifest.d.ts +41 -0
  14. package/lib/src/model/model-manifest.js +11 -0
  15. package/lib/src/model/query-sparql.test.js +92 -10
  16. package/lib/src/model/shacl-gen.js +33 -0
  17. package/lib/src/model/types.d.ts +9 -0
  18. package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +33 -33
  19. package/lib/src/perspectives/AutoProcessor.d.ts +90 -24
  20. package/lib/src/perspectives/FlowInstance.d.ts +187 -0
  21. package/lib/src/perspectives/FlowInstance.js +336 -0
  22. package/lib/src/perspectives/FlowModels.d.ts +94 -0
  23. package/lib/src/perspectives/FlowModels.js +182 -0
  24. package/lib/src/perspectives/InterpretationModels.d.ts +12 -22
  25. package/lib/src/perspectives/InterpretationModels.js +2 -6
  26. package/lib/src/perspectives/PerspectiveClient.d.ts +68 -7
  27. package/lib/src/perspectives/PerspectiveClient.js +105 -10
  28. package/lib/src/perspectives/PerspectiveProxy.d.ts +142 -57
  29. package/lib/src/perspectives/PerspectiveProxy.js +388 -274
  30. package/lib/src/shacl/SHACLFlow.d.ts +273 -47
  31. package/lib/src/shacl/SHACLFlow.js +397 -88
  32. package/lib/src/shacl/SHACLShape.d.ts +8 -0
  33. package/lib/src/shacl/SHACLShape.js +17 -0
  34. package/lib/src/shacl/SHACLShape.test.js +82 -0
  35. package/package.json +2 -2
  36. package/src/ai/AIClient.ts +6 -3
  37. package/src/apiClient.test.ts +227 -0
  38. package/src/apiClient.ts +185 -9
  39. package/src/index.ts +3 -1
  40. package/src/model/Ad4mModel.test.ts +645 -0
  41. package/src/model/Ad4mModel.ts +245 -26
  42. package/src/model/decorators.ts +216 -35
  43. package/src/model/relation-filtering.test.ts +70 -20
  44. package/src/model/shacl-gen.ts +36 -0
  45. package/src/model/shacl-roundtrip.test.ts +130 -0
  46. package/src/model/types.ts +7 -0
  47. package/src/perspectives/AutoProcessor.ts +94 -24
  48. package/src/perspectives/FlowInstance.ts +369 -0
  49. package/src/perspectives/FlowModels.ts +157 -0
  50. package/src/perspectives/InterpretationModels.ts +13 -25
  51. package/src/perspectives/PerspectiveClient.test.ts +67 -6
  52. package/src/perspectives/PerspectiveClient.ts +142 -12
  53. package/src/perspectives/PerspectiveProxy.test.ts +406 -0
  54. package/src/perspectives/PerspectiveProxy.ts +459 -289
  55. package/src/perspectives/shacl-rpc.test.ts +258 -0
  56. package/src/shacl/SHACLFlow.test.ts +762 -35
  57. package/src/shacl/SHACLFlow.ts +680 -112
  58. package/src/shacl/SHACLShape.ts +31 -0
  59. package/lib/src/generated/rest/AIModelLoadingStatus.d.ts +0 -7
  60. package/lib/src/generated/rest/AIModelLoadingStatus.js +0 -2
  61. package/lib/src/generated/rest/AddAgentInfosRequest.d.ts +0 -3
  62. package/lib/src/generated/rest/AddAgentInfosRequest.js +0 -2
  63. package/lib/src/generated/rest/AddLinkExpressionRequest.d.ts +0 -6
  64. package/lib/src/generated/rest/AddLinkExpressionRequest.js +0 -1
  65. package/lib/src/generated/rest/AddLinkRequest.d.ts +0 -6
  66. package/lib/src/generated/rest/AddLinkRequest.js +0 -1
  67. package/lib/src/generated/rest/AddLinksBulkRequest.d.ts +0 -6
  68. package/lib/src/generated/rest/AddLinksBulkRequest.js +0 -1
  69. package/lib/src/generated/rest/AddSdnaRequest.d.ts +0 -6
  70. package/lib/src/generated/rest/AddSdnaRequest.js +0 -2
  71. package/lib/src/generated/rest/ApplyTemplateRequest.d.ts +0 -4
  72. package/lib/src/generated/rest/ApplyTemplateRequest.js +0 -2
  73. package/lib/src/generated/rest/AuthInfoInput.d.ts +0 -10
  74. package/lib/src/generated/rest/AuthInfoInput.js +0 -1
  75. package/lib/src/generated/rest/BroadcastRequest.d.ts +0 -6
  76. package/lib/src/generated/rest/BroadcastRequest.js +0 -1
  77. package/lib/src/generated/rest/CapabilityInput.d.ts +0 -5
  78. package/lib/src/generated/rest/CapabilityInput.js +0 -1
  79. package/lib/src/generated/rest/CommitBatchRequest.d.ts +0 -3
  80. package/lib/src/generated/rest/CommitBatchRequest.js +0 -2
  81. package/lib/src/generated/rest/ComputeLogEntry.d.ts +0 -9
  82. package/lib/src/generated/rest/ComputeLogEntry.js +0 -2
  83. package/lib/src/generated/rest/CreateExpressionRequest.d.ts +0 -4
  84. package/lib/src/generated/rest/CreateExpressionRequest.js +0 -2
  85. package/lib/src/generated/rest/CreatePerspectiveRequest.d.ts +0 -3
  86. package/lib/src/generated/rest/CreatePerspectiveRequest.js +0 -2
  87. package/lib/src/generated/rest/CreateSubjectRequest.d.ts +0 -6
  88. package/lib/src/generated/rest/CreateSubjectRequest.js +0 -2
  89. package/lib/src/generated/rest/CreateUserRequest.d.ts +0 -4
  90. package/lib/src/generated/rest/CreateUserRequest.js +0 -2
  91. package/lib/src/generated/rest/CreateUserResponse.d.ts +0 -5
  92. package/lib/src/generated/rest/CreateUserResponse.js +0 -2
  93. package/lib/src/generated/rest/DecoratedExpressionProof.d.ts +0 -6
  94. package/lib/src/generated/rest/DecoratedExpressionProof.js +0 -2
  95. package/lib/src/generated/rest/DecoratedLinkExpression.d.ts +0 -10
  96. package/lib/src/generated/rest/DecoratedLinkExpression.js +0 -1
  97. package/lib/src/generated/rest/DisposeQueryRequest.d.ts +0 -3
  98. package/lib/src/generated/rest/DisposeQueryRequest.js +0 -2
  99. package/lib/src/generated/rest/EmailTestRequest.d.ts +0 -6
  100. package/lib/src/generated/rest/EmailTestRequest.js +0 -2
  101. package/lib/src/generated/rest/EmbedRequest.d.ts +0 -4
  102. package/lib/src/generated/rest/EmbedRequest.js +0 -2
  103. package/lib/src/generated/rest/EntanglementProofInput.d.ts +0 -6
  104. package/lib/src/generated/rest/EntanglementProofInput.js +0 -2
  105. package/lib/src/generated/rest/EntanglementProofPreflightRequest.d.ts +0 -4
  106. package/lib/src/generated/rest/EntanglementProofPreflightRequest.js +0 -2
  107. package/lib/src/generated/rest/ExecuteCommandsRequest.d.ts +0 -4
  108. package/lib/src/generated/rest/ExecuteCommandsRequest.js +0 -2
  109. package/lib/src/generated/rest/ExportRequest.d.ts +0 -5
  110. package/lib/src/generated/rest/ExportRequest.js +0 -2
  111. package/lib/src/generated/rest/ExpressionManyRequest.d.ts +0 -3
  112. package/lib/src/generated/rest/ExpressionManyRequest.js +0 -2
  113. package/lib/src/generated/rest/ExpressionProof.d.ts +0 -4
  114. package/lib/src/generated/rest/ExpressionProof.js +0 -2
  115. package/lib/src/generated/rest/ExpressionProofInput.d.ts +0 -6
  116. package/lib/src/generated/rest/ExpressionProofInput.js +0 -2
  117. package/lib/src/generated/rest/FriendSendMessageRequest.d.ts +0 -3
  118. package/lib/src/generated/rest/FriendSendMessageRequest.js +0 -2
  119. package/lib/src/generated/rest/FriendsListRequest.d.ts +0 -3
  120. package/lib/src/generated/rest/FriendsListRequest.js +0 -2
  121. package/lib/src/generated/rest/GenerateAgentRequest.d.ts +0 -3
  122. package/lib/src/generated/rest/GenerateAgentRequest.js +0 -2
  123. package/lib/src/generated/rest/GenerateJwtRequest.d.ts +0 -4
  124. package/lib/src/generated/rest/GenerateJwtRequest.js +0 -2
  125. package/lib/src/generated/rest/GetSubjectDataRequest.d.ts +0 -4
  126. package/lib/src/generated/rest/GetSubjectDataRequest.js +0 -2
  127. package/lib/src/generated/rest/HostingInfoResponse.d.ts +0 -5
  128. package/lib/src/generated/rest/HostingInfoResponse.js +0 -2
  129. package/lib/src/generated/rest/HostingWalletResponse.d.ts +0 -4
  130. package/lib/src/generated/rest/HostingWalletResponse.js +0 -2
  131. package/lib/src/generated/rest/ImportAgentRequest.d.ts +0 -6
  132. package/lib/src/generated/rest/ImportAgentRequest.js +0 -2
  133. package/lib/src/generated/rest/ImportRequest.d.ts +0 -4
  134. package/lib/src/generated/rest/ImportRequest.js +0 -2
  135. package/lib/src/generated/rest/JoinNeighbourhoodRequest.d.ts +0 -3
  136. package/lib/src/generated/rest/JoinNeighbourhoodRequest.js +0 -2
  137. package/lib/src/generated/rest/KeepAliveQueryRequest.d.ts +0 -3
  138. package/lib/src/generated/rest/KeepAliveQueryRequest.js +0 -2
  139. package/lib/src/generated/rest/LanguageMetaInput.d.ts +0 -6
  140. package/lib/src/generated/rest/LanguageMetaInput.js +0 -2
  141. package/lib/src/generated/rest/Link.d.ts +0 -5
  142. package/lib/src/generated/rest/Link.js +0 -2
  143. package/lib/src/generated/rest/LinkExpression.d.ts +0 -10
  144. package/lib/src/generated/rest/LinkExpression.js +0 -1
  145. package/lib/src/generated/rest/LinkExpressionInput.d.ts +0 -10
  146. package/lib/src/generated/rest/LinkExpressionInput.js +0 -1
  147. package/lib/src/generated/rest/LinkInput.d.ts +0 -5
  148. package/lib/src/generated/rest/LinkInput.js +0 -2
  149. package/lib/src/generated/rest/LinkLanguageTemplatesRequest.d.ts +0 -3
  150. package/lib/src/generated/rest/LinkLanguageTemplatesRequest.js +0 -2
  151. package/lib/src/generated/rest/LinkMutationRequest.d.ts +0 -12
  152. package/lib/src/generated/rest/LinkMutationRequest.js +0 -1
  153. package/lib/src/generated/rest/LinkMutationResponse.d.ts +0 -9
  154. package/lib/src/generated/rest/LinkMutationResponse.js +0 -1
  155. package/lib/src/generated/rest/LinkMutations.d.ts +0 -6
  156. package/lib/src/generated/rest/LinkMutations.js +0 -1
  157. package/lib/src/generated/rest/LinkMutationsRequest.d.ts +0 -5
  158. package/lib/src/generated/rest/LinkMutationsRequest.js +0 -1
  159. package/lib/src/generated/rest/LinkStatus.d.ts +0 -1
  160. package/lib/src/generated/rest/LinkStatus.js +0 -2
  161. package/lib/src/generated/rest/LinkUpdateInput.d.ts +0 -5
  162. package/lib/src/generated/rest/LinkUpdateInput.js +0 -1
  163. package/lib/src/generated/rest/LockAgentRequest.d.ts +0 -3
  164. package/lib/src/generated/rest/LockAgentRequest.js +0 -2
  165. package/lib/src/generated/rest/LoginUserRequest.d.ts +0 -5
  166. package/lib/src/generated/rest/LoginUserRequest.js +0 -2
  167. package/lib/src/generated/rest/ModelType.d.ts +0 -1
  168. package/lib/src/generated/rest/ModelType.js +0 -2
  169. package/lib/src/generated/rest/NotificationGrantRequest.d.ts +0 -3
  170. package/lib/src/generated/rest/NotificationGrantRequest.js +0 -2
  171. package/lib/src/generated/rest/NotificationInput.d.ts +0 -10
  172. package/lib/src/generated/rest/NotificationInput.js +0 -2
  173. package/lib/src/generated/rest/OpenLinkRequest.d.ts +0 -3
  174. package/lib/src/generated/rest/OpenLinkRequest.js +0 -2
  175. package/lib/src/generated/rest/PermitCapabilityRequest.d.ts +0 -3
  176. package/lib/src/generated/rest/PermitCapabilityRequest.js +0 -2
  177. package/lib/src/generated/rest/PerspectiveInput.d.ts +0 -4
  178. package/lib/src/generated/rest/PerspectiveInput.js +0 -1
  179. package/lib/src/generated/rest/PromptRequest.d.ts +0 -4
  180. package/lib/src/generated/rest/PromptRequest.js +0 -2
  181. package/lib/src/generated/rest/PublicPerspectiveInput.d.ts +0 -4
  182. package/lib/src/generated/rest/PublicPerspectiveInput.js +0 -1
  183. package/lib/src/generated/rest/PublishLanguageRequest.d.ts +0 -5
  184. package/lib/src/generated/rest/PublishLanguageRequest.js +0 -1
  185. package/lib/src/generated/rest/PublishNeighbourhoodRequest.d.ts +0 -6
  186. package/lib/src/generated/rest/PublishNeighbourhoodRequest.js +0 -1
  187. package/lib/src/generated/rest/QueryRequest.d.ts +0 -7
  188. package/lib/src/generated/rest/QueryRequest.js +0 -2
  189. package/lib/src/generated/rest/RemoveLinkRequest.d.ts +0 -5
  190. package/lib/src/generated/rest/RemoveLinkRequest.js +0 -1
  191. package/lib/src/generated/rest/RemoveLinksBulkRequest.d.ts +0 -5
  192. package/lib/src/generated/rest/RemoveLinksBulkRequest.js +0 -1
  193. package/lib/src/generated/rest/RequestCapabilityRequest.d.ts +0 -4
  194. package/lib/src/generated/rest/RequestCapabilityRequest.js +0 -1
  195. package/lib/src/generated/rest/RequestPaymentRequest.d.ts +0 -3
  196. package/lib/src/generated/rest/RequestPaymentRequest.js +0 -2
  197. package/lib/src/generated/rest/RequestVerificationRequest.d.ts +0 -4
  198. package/lib/src/generated/rest/RequestVerificationRequest.js +0 -2
  199. package/lib/src/generated/rest/ResourceInput.d.ts +0 -4
  200. package/lib/src/generated/rest/ResourceInput.js +0 -2
  201. package/lib/src/generated/rest/SetDefaultModelRequest.d.ts +0 -4
  202. package/lib/src/generated/rest/SetDefaultModelRequest.js +0 -1
  203. package/lib/src/generated/rest/SetFreeHostingEnabledRequest.d.ts +0 -3
  204. package/lib/src/generated/rest/SetFreeHostingEnabledRequest.js +0 -2
  205. package/lib/src/generated/rest/SetHotWalletAddressRequest.d.ts +0 -3
  206. package/lib/src/generated/rest/SetHotWalletAddressRequest.js +0 -2
  207. package/lib/src/generated/rest/SetMultiUserRequest.d.ts +0 -3
  208. package/lib/src/generated/rest/SetMultiUserRequest.js +0 -2
  209. package/lib/src/generated/rest/SetOnlineStatusRequest.d.ts +0 -5
  210. package/lib/src/generated/rest/SetOnlineStatusRequest.js +0 -1
  211. package/lib/src/generated/rest/SetStatusRequest.d.ts +0 -3
  212. package/lib/src/generated/rest/SetStatusRequest.js +0 -2
  213. package/lib/src/generated/rest/SetUserFreeAccessRequest.d.ts +0 -4
  214. package/lib/src/generated/rest/SetUserFreeAccessRequest.js +0 -2
  215. package/lib/src/generated/rest/SignMessageRequest.d.ts +0 -3
  216. package/lib/src/generated/rest/SignMessageRequest.js +0 -2
  217. package/lib/src/generated/rest/SignalRequest.d.ts +0 -6
  218. package/lib/src/generated/rest/SignalRequest.js +0 -1
  219. package/lib/src/generated/rest/SubscribeQueryRequest.d.ts +0 -3
  220. package/lib/src/generated/rest/SubscribeQueryRequest.js +0 -2
  221. package/lib/src/generated/rest/SubscribeQueryResponse.d.ts +0 -4
  222. package/lib/src/generated/rest/SubscribeQueryResponse.js +0 -2
  223. package/lib/src/generated/rest/TrustedAgentsRequest.d.ts +0 -3
  224. package/lib/src/generated/rest/TrustedAgentsRequest.js +0 -2
  225. package/lib/src/generated/rest/UnlockAgentRequest.d.ts +0 -4
  226. package/lib/src/generated/rest/UnlockAgentRequest.js +0 -2
  227. package/lib/src/generated/rest/UpdateLinkRequest.d.ts +0 -7
  228. package/lib/src/generated/rest/UpdateLinkRequest.js +0 -1
  229. package/lib/src/generated/rest/UpdatePerspectiveRequest.d.ts +0 -3
  230. package/lib/src/generated/rest/UpdatePerspectiveRequest.js +0 -2
  231. package/lib/src/generated/rest/UpdateProfileRequest.d.ts +0 -5
  232. package/lib/src/generated/rest/UpdateProfileRequest.js +0 -1
  233. package/lib/src/generated/rest/UserStatistics.d.ts +0 -9
  234. package/lib/src/generated/rest/UserStatistics.js +0 -2
  235. package/lib/src/generated/rest/VerificationRequestResult.d.ts +0 -6
  236. package/lib/src/generated/rest/VerificationRequestResult.js +0 -2
  237. package/lib/src/generated/rest/VerifyEmailRequest.d.ts +0 -6
  238. package/lib/src/generated/rest/VerifyEmailRequest.js +0 -2
  239. package/lib/src/generated/rest/VerifySignatureRequest.d.ts +0 -5
  240. package/lib/src/generated/rest/VerifySignatureRequest.js +0 -2
  241. package/lib/src/generated/rest/WriteSettingsRequest.d.ts +0 -3
  242. package/lib/src/generated/rest/WriteSettingsRequest.js +0 -2
  243. package/lib/src/generated/rest/index.d.ts +0 -73
  244. package/lib/src/generated/rest/index.js +0 -8
  245. package/lib/src/generated/rest/routes.d.ts +0 -587
  246. package/lib/src/generated/rest/routes.js +0 -3
  247. package/lib/src/model/query-surreal.d.ts +0 -44
  248. package/lib/src/model/query-surreal.js +0 -249
  249. package/lib/src/model/surreal-utils.d.ts +0 -42
  250. package/lib/src/model/surreal-utils.js +0 -128
  251. package/lib/src/perspectives/PerspectiveProxy.test.d.ts +0 -1
  252. package/lib/src/perspectives/PerspectiveProxy.test.js +0 -34
  253. package/lib/src/restClient.d.ts +0 -52
  254. package/lib/src/restClient.js +0 -227
@@ -44,6 +44,23 @@ export interface RelationMetadataEntry {
44
44
  * instances — those pass through byte-for-byte.
45
45
  */
46
46
  datatype?: string;
47
+ /** Hydrate targets as the class each one actually is — see `RelationOptions.polymorphic`. */
48
+ polymorphic?: boolean;
49
+ /** Model classes this relation's polymorphic results can be constructed as. */
50
+ instantiateAs?: () => Ad4mModelLike[];
51
+ /** CRDT ordering config — see `RelationOptions.ordering`. */
52
+ ordering?: {
53
+ strategy: 'linkedList';
54
+ };
55
+ /**
56
+ * Natural-language hint describing what this relation MEANS semantically.
57
+ * Emitted as an `ad4m://interpretation_hint` link on the SHACL property
58
+ * node; read back by the Rust harness and rendered into the
59
+ * `_propose_link_child` tool's `predicate` field description. Mirror of
60
+ * the same field on `RelationOptions` (the decorator arg); this is the
61
+ * memoized registry copy.
62
+ */
63
+ interpretationHint?: string;
47
64
  }
48
65
  /**
49
66
  * Retrieve property metadata for a given class constructor.
@@ -512,15 +529,34 @@ export interface RelationOptions {
512
529
  */
513
530
  through?: string;
514
531
  /** The target model class (use a thunk to avoid circular-dependency issues). Optional for untyped string relations.
515
- * Cannot be combined with `getter`. */
532
+ * Combines with `getter`, where it names the class the traversal's values hydrate into. */
516
533
  target?: () => Ad4mModelLike;
517
534
  /**
518
535
  * Custom getter to resolve the relation values. Use this for custom graph traversals.
519
536
  * The expression can reference 'Base' which will be replaced with the instance's base expression.
520
537
  * Example: "SELECT ?target WHERE { ?target <flux://has_reply> <Base> . }"
521
538
  *
522
- * Mutually exclusive with `through` and `target`. When `getter` is provided the
523
- * relation is read-only (no adder/remover actions are generated).
539
+ * Mutually exclusive with `through` — a getter replaces link-based resolution,
540
+ * so there is no predicate to add to or remove from. When `getter` is provided
541
+ * the relation is read-only (no adder/remover actions are generated).
542
+ *
543
+ * **Combines with `target`**, which names the class the traversal's values
544
+ * hydrate into — without it `include` has no shape to resolve and the relation
545
+ * can only return bare URIs. Also combines with `where`, applied as a
546
+ * post-getter filter against the target class.
547
+ *
548
+ * @example Traversing a reified edge — a connection stored as a record rather
549
+ * than a predicate, so no `through` can express it:
550
+ * ```typescript
551
+ * @HasMany({
552
+ * getter: `SELECT ?target WHERE {
553
+ * ?citation <paper://cites_from> <Base> .
554
+ * ?citation <paper://cites_to> ?target .
555
+ * }`,
556
+ * target: () => Paper,
557
+ * })
558
+ * cited: Paper[] = [];
559
+ * ```
524
560
  */
525
561
  getter?: string;
526
562
  /** Whether the link is stored locally (not shared on the network) */
@@ -541,7 +577,8 @@ export interface RelationOptions {
541
577
  * auto-derived from the target shape (flags + required properties).
542
578
  * Providing `where` overrides this auto-derivation.
543
579
  *
544
- * Mutually exclusive with `getter` and `filter: false`.
580
+ * Mutually exclusive with `filter: false`. Combines with `getter`, where it
581
+ * is applied as a post-getter filter against the target class.
545
582
  *
546
583
  * @example
547
584
  * ```typescript
@@ -567,6 +604,112 @@ export interface RelationOptions {
567
604
  * ```
568
605
  */
569
606
  datatype?: string;
607
+ /**
608
+ * Hydrate each target as the class it actually **is**, rather than as the
609
+ * class this relation declares.
610
+ *
611
+ * A heterogeneous relation has no single right target. Declaring the base
612
+ * class loses every subclass property — the executor hydrates against the
613
+ * shape it is given, so an `ImagePost` read as a `Post` arrives with its own
614
+ * fields simply absent, not merely mislabelled. Declaring nothing leaves
615
+ * `include` with no shape to resolve at all.
616
+ *
617
+ * With this set, the targets are classified, grouped by concrete class, and
618
+ * hydrated against their own shapes — one query per distinct class present,
619
+ * not per instance.
620
+ *
621
+ * Pair with `instantiateAs` so the results become instances of the right
622
+ * model class rather than plain objects.
623
+ *
624
+ * @example
625
+ * ```typescript
626
+ * @HasMany({
627
+ * through: "we://children",
628
+ * polymorphic: true,
629
+ * instantiateAs: () => [TextBlock, ImageBlock],
630
+ * })
631
+ * children: Post[] = [];
632
+ * ```
633
+ */
634
+ polymorphic?: boolean;
635
+ /**
636
+ * The model classes a `polymorphic` result may be constructed as.
637
+ *
638
+ * Classification happens in the executor, structurally — there is no
639
+ * `rdf:type` triple to read, so a target's class is derived from the flags
640
+ * and required properties it carries. What cannot cross the wire is the
641
+ * TypeScript constructor, so this names the classes this side can build.
642
+ * `@Model` already records each class's name on the class itself, so the
643
+ * name → class mapping is derived from the list rather than restated
644
+ * alongside it.
645
+ *
646
+ * **This is advisory, not a constraint.** It never narrows what the query
647
+ * returns and never excludes a target: a class missing from the list arrives
648
+ * as plain JSON carrying its class name, so partial knowledge degrades
649
+ * instead of failing. Declaring what this call site can *construct* is a
650
+ * different statement from declaring what the relation may *contain* — a
651
+ * heterogeneous relation is open by definition, and a list that silently
652
+ * dropped unrecognised members would defeat the point of reading it
653
+ * polymorphically at all. Constraining a read to particular classes belongs
654
+ * in the query, not here.
655
+ *
656
+ * A thunk rather than an array, for the same reason `target` is one: it is
657
+ * evaluated at query time, so a class defined later in a circular import
658
+ * graph still resolves.
659
+ */
660
+ instantiateAs?: () => Ad4mModelLike[];
661
+ /**
662
+ * Give this collection a user-controlled order that survives concurrent edits.
663
+ *
664
+ * A `@HasMany` is a set of links, and hydration sorts them by link timestamp.
665
+ * That is right for an append-only collection — a transcript, a message
666
+ * thread — where timestamp order *is* the order. It cannot express a sequence
667
+ * somebody chose: a kanban column, a playlist, the blocks of a post.
668
+ *
669
+ * **Declaration only, for now.** Setting this emits `ad4m://ordering` on the
670
+ * property shape, where the executor reads it back — but no read or write
671
+ * path acts on it yet, so a relation declaring it still hydrates by link
672
+ * timestamp exactly as one that does not. Wiring save and hydration to the
673
+ * declaration is the next change; nothing about how you write the relation
674
+ * will change when it lands — assign the array in the order you want and
675
+ * save.
676
+ *
677
+ * Ordering is declared in the type system, rather than passed per query, so
678
+ * that *every* writer gets it once it is wired: the ORM, MCP agents, raw
679
+ * WS-RPC callers, another app sharing the neighbourhood. Implemented
680
+ * client-side it would order only what this client wrote.
681
+ *
682
+ * @example
683
+ * ```typescript
684
+ * @HasMany(() => Task, {
685
+ * through: "kanban://has_task",
686
+ * ordering: { strategy: "linkedList" },
687
+ * })
688
+ * tasks: Task[] = [];
689
+ * ```
690
+ */
691
+ ordering?: {
692
+ strategy: 'linkedList';
693
+ };
694
+ /**
695
+ * Natural-language hint describing what this relation MEANS semantically —
696
+ * the sentence-level rationale for when to use it, not just its structural
697
+ * shape. Emitted as an `ad4m://interpretation_hint` link on the SHACL
698
+ * property node; read back by the Rust harness (`ClassProposeShape.
699
+ * relations[N].hint`) and rendered into the `_propose_link_child` tool's
700
+ * `predicate` field description so the LLM knows which relation applies
701
+ * to which situation instead of guessing from the predicate name alone.
702
+ *
703
+ * @example
704
+ * ```typescript
705
+ * @HasMany(() => Belief, {
706
+ * through: "ns://basedOn",
707
+ * interpretationHint: "The prior beliefs this intention derives from.",
708
+ * })
709
+ * basedOn: Belief[] = [];
710
+ * ```
711
+ */
712
+ interpretationHint?: string;
570
713
  }
571
714
  /**
572
715
  * Utility type that describes the auto-generated helper methods for a HasMany
@@ -526,35 +526,57 @@ function resolveRelationArgs(first, second) {
526
526
  const opts = typeof first === 'function'
527
527
  ? { ...(second || {}), target: first }
528
528
  : first;
529
- // getter is mutually exclusive with through, target, and where
529
+ // `where` and `filter: false` contradict each other on every relation,
530
+ // getter-backed ones included — checked before the getter path returns.
531
+ if (opts.where && opts.filter === false) {
532
+ throw new Error('Relation decorator: `where` and `filter: false` are contradictory. ' +
533
+ '`where` adds filtering constraints; `filter: false` disables filtering.');
534
+ }
535
+ // getter is mutually exclusive with through
530
536
  if (opts.getter) {
531
537
  if (opts.through) {
532
538
  throw new Error('Relation decorator: `getter` and `through` are mutually exclusive. ' +
533
539
  'Use `getter` alone for custom read-only relations, or `through` ' +
534
540
  '(with optional `target`) for standard link-based relations.');
535
541
  }
536
- if (opts.target) {
537
- throw new Error('Relation decorator: `getter` and `target` are mutually exclusive. ' +
538
- '`target` auto-generates a conformance getter from the model shape; ' +
539
- 'providing both is contradictory.');
540
- }
541
- if (opts.where) {
542
- throw new Error('Relation decorator: `where` and `getter` are mutually exclusive. ' +
543
- 'Use `where` for DSL-based filtering, or `getter` for raw getter expression.');
544
- }
542
+ // `target` and `where` are NOT mutually exclusive with `getter`.
543
+ //
544
+ // `target` does two separable jobs: it auto-derives a conformance
545
+ // getter, and it names the class the relation's values hydrate into.
546
+ // Only the first conflicts with an explicit getter, and `buildSHACL`
547
+ // already resolves that on its own — an explicit getter wins the getter
548
+ // slot, while `sh:class` / `ad4m:targetClassName` are emitted from
549
+ // `target` independently.
550
+ //
551
+ // Refusing the pair cost the one thing a custom getter is for. A getter
552
+ // expresses a traversal the link-shaped DSL cannot — most usefully
553
+ // through a reified edge, where the connection is a record rather than
554
+ // a predicate:
555
+ //
556
+ // @HasMany({
557
+ // getter: "SELECT ?target WHERE { \
558
+ // ?citation <paper://cites_from> <Base> . \
559
+ // ?citation <paper://cites_to> ?target . }",
560
+ // target: () => Paper,
561
+ // })
562
+ // cited: Paper[] = [];
563
+ //
564
+ // Without `target` the relation has no target class, so `include` on it
565
+ // resolves a shape named "" and fails — leaving a traversal that can
566
+ // only ever return bare URIs. With it, the values hydrate like any other
567
+ // relation, because getters are evaluated before include resolution.
568
+ //
569
+ // `where` is the same story: the executor applies post-getter where
570
+ // filters specifically to getter-backed relations
571
+ // (`apply_where_filter_to_relation`), and emitting `wherePredicates`
572
+ // needs `target` to resolve the target's metadata — so the runtime is
573
+ // built for all three together and only this check said otherwise.
545
574
  return opts;
546
575
  }
547
576
  // Default predicate when not provided
548
577
  if (!opts.through) {
549
578
  opts.through = 'ad4m://has_child';
550
579
  }
551
- // where validation
552
- if (opts.where) {
553
- if (opts.filter === false) {
554
- throw new Error('Relation decorator: `where` and `filter: false` are contradictory. ' +
555
- '`where` adds filtering constraints; `filter: false` disables filtering.');
556
- }
557
- }
558
580
  return opts;
559
581
  }
560
582
  export function HasMany(first, second) {
@@ -574,6 +596,10 @@ export function HasMany(first, second) {
574
596
  ...(opts.filter !== undefined && { filter: opts.filter }),
575
597
  ...(opts.where && { where: opts.where }),
576
598
  ...(opts.datatype && { datatype: opts.datatype }),
599
+ ...(opts.polymorphic && { polymorphic: opts.polymorphic }),
600
+ ...(opts.instantiateAs && { instantiateAs: opts.instantiateAs }),
601
+ ...(opts.ordering && { ordering: opts.ordering }),
602
+ ...(opts.interpretationHint && { interpretationHint: opts.interpretationHint }),
577
603
  };
578
604
  const relKey = key;
579
605
  // Only add mutation methods when a predicate is available
@@ -608,6 +634,10 @@ export function HasOne(first, second) {
608
634
  ...(opts.filter !== undefined && { filter: opts.filter }),
609
635
  ...(opts.where && { where: opts.where }),
610
636
  ...(opts.datatype && { datatype: opts.datatype }),
637
+ ...(opts.polymorphic && { polymorphic: opts.polymorphic }),
638
+ ...(opts.instantiateAs && { instantiateAs: opts.instantiateAs }),
639
+ ...(opts.ordering && { ordering: opts.ordering }),
640
+ ...(opts.interpretationHint && { interpretationHint: opts.interpretationHint }),
611
641
  };
612
642
  const relKey = key;
613
643
  if (opts.through) {
@@ -617,15 +647,23 @@ export function HasOne(first, second) {
617
647
  readOnly: false,
618
648
  local: opts.local,
619
649
  })(target, key);
620
- // Add prototype methods for add/remove/set (mirroring @HasMany)
621
- target[`add${capitalize(relKey)}`] = async function (arg) {
622
- return this.addRelationValue(relKey, arg);
650
+ // Add prototype methods for add/remove/set (mirroring @HasMany).
651
+ //
652
+ // `batchId` is part of that mirroring: without it a to-one link
653
+ // cannot join a write group, so anything that creates a record and
654
+ // then points it at something has to commit twice and every
655
+ // subscriber sees the state in between — a record whose to-one
656
+ // relation is still empty. Anything rendering from a subscription
657
+ // therefore has a frame in which the record exists and points at
658
+ // nothing.
659
+ target[`add${capitalize(relKey)}`] = async function (arg, batchId) {
660
+ return this.addRelationValue(relKey, arg, batchId);
623
661
  };
624
- target[`remove${capitalize(relKey)}`] = async function (arg) {
625
- return this.removeRelationValue(relKey, arg);
662
+ target[`remove${capitalize(relKey)}`] = async function (arg, batchId) {
663
+ return this.removeRelationValue(relKey, arg, batchId);
626
664
  };
627
- target[`set${capitalize(relKey)}`] = async function (arg) {
628
- return this.setRelationValues(relKey, arg);
665
+ target[`set${capitalize(relKey)}`] = async function (arg, batchId) {
666
+ return this.setRelationValues(relKey, arg, batchId);
629
667
  };
630
668
  }
631
669
  else {
@@ -649,6 +687,10 @@ export function BelongsToOne(first, second) {
649
687
  ...(opts.filter !== undefined && { filter: opts.filter }),
650
688
  ...(opts.where && { where: opts.where }),
651
689
  ...(opts.datatype && { datatype: opts.datatype }),
690
+ ...(opts.polymorphic && { polymorphic: opts.polymorphic }),
691
+ ...(opts.instantiateAs && { instantiateAs: opts.instantiateAs }),
692
+ ...(opts.ordering && { ordering: opts.ordering }),
693
+ ...(opts.interpretationHint && { interpretationHint: opts.interpretationHint }),
652
694
  };
653
695
  if (opts.through) {
654
696
  // Read-only property (the owning side manages the link)
@@ -679,6 +721,10 @@ export function BelongsToMany(first, second) {
679
721
  ...(opts.filter !== undefined && { filter: opts.filter }),
680
722
  ...(opts.where && { where: opts.where }),
681
723
  ...(opts.datatype && { datatype: opts.datatype }),
724
+ ...(opts.polymorphic && { polymorphic: opts.polymorphic }),
725
+ ...(opts.instantiateAs && { instantiateAs: opts.instantiateAs }),
726
+ ...(opts.ordering && { ordering: opts.ordering }),
727
+ ...(opts.interpretationHint && { interpretationHint: opts.interpretationHint }),
682
728
  };
683
729
  // @BelongsToMany is the inverse/read-only side — do NOT generate add*/remove*/set*
684
730
  // prototype methods. Mutation must go through the owning side's @HasMany decorator.
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Types for the perspective model manifest — a human-readable, AI-friendly summary
3
+ * of all Ad4mModel-compatible classes defined in a perspective's SHACL shapes.
4
+ *
5
+ * Used by `PerspectiveProxy.getModelManifest()` and consumed by WE's AI context
6
+ * injection (`formatManifestForPrompt`) to describe external perspective models
7
+ * to the schema-generation AI.
8
+ *
9
+ * @module
10
+ */
11
+ /** Normalised description of a single property on a model class. */
12
+ export interface ModelManifestProperty {
13
+ /** Property name as declared in the SHACL shape (e.g. "name", "body", "participants"). */
14
+ name: string;
15
+ /** The exact RDF predicate URI (e.g. "flux://has_name"). */
16
+ predicate: string;
17
+ /** Normalised scalar type; inferred from xsd:/sh: datatype + nodeKind. */
18
+ type: 'string' | 'number' | 'boolean' | 'uri';
19
+ /** `true` when maxCount is absent or > 1 (i.e. returned as an array). */
20
+ isCollection: boolean;
21
+ /** `true` when minCount >= 1. */
22
+ required: boolean;
23
+ writable: boolean;
24
+ /** AD4M language used to resolve the value (e.g. "literal"). */
25
+ resolveLanguage?: string;
26
+ /**
27
+ * Local name of the related model class when this is a typed relation property.
28
+ * Derived from `SHACLPropertyShape.class` by stripping the URI prefix.
29
+ * E.g. `flux://Message` → `"Message"`.
30
+ */
31
+ relatedModel?: string;
32
+ }
33
+ /** Normalised description of a single model class stored in a perspective. */
34
+ export interface ModelManifestEntry {
35
+ /** Class name — usable directly as the `model` field in a `$query` token. */
36
+ name: string;
37
+ /** The SHACL `targetClass` URI (e.g. "flux://Channel"). */
38
+ targetClass: string;
39
+ /** All data properties (flag/hasValue properties are excluded). */
40
+ properties: ModelManifestProperty[];
41
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Types for the perspective model manifest — a human-readable, AI-friendly summary
3
+ * of all Ad4mModel-compatible classes defined in a perspective's SHACL shapes.
4
+ *
5
+ * Used by `PerspectiveProxy.getModelManifest()` and consumed by WE's AI context
6
+ * injection (`formatManifestForPrompt`) to describe external perspective models
7
+ * to the schema-generation AI.
8
+ *
9
+ * @module
10
+ */
11
+ export {};
@@ -53,26 +53,28 @@ describe('hasJsOnlyWhereFilters', () => {
53
53
  expect(hasJsOnlyWhereFilters(meta, relMeta, { parent: 'some://id' })).toBe(true);
54
54
  });
55
55
  });
56
- describe('buildSPARQLQuery — pagination handled in JS, not SPARQL', () => {
56
+ describe('buildSPARQLQuery — SPARQL-level pagination via subquery', () => {
57
57
  const modelClass = {};
58
- it('does NOT include LIMIT/OFFSET in SPARQL even when query has them', () => {
58
+ it('includes LIMIT/OFFSET in a pagination subquery when query has them and no JS-only filters', () => {
59
59
  const query = { limit: 10, offset: 0, where: { category: 'some://uri' } };
60
60
  const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
61
- // SPARQL should NOT contain LIMIT/OFFSET — pagination is done in JS
62
- expect(sparql).not.toContain('LIMIT');
63
- expect(sparql).not.toContain('OFFSET');
61
+ // SPARQL should contain LIMIT inside the pagination subquery
62
+ expect(sparql).toContain('LIMIT 10');
64
63
  // The outer query should still exist
65
64
  expect(sparql).toContain('SELECT ?source ?predicate ?target');
65
+ // Should have a subquery pattern
66
+ expect(sparql).toContain('SELECT DISTINCT ?source');
66
67
  });
67
- it('does NOT include LIMIT/OFFSET even with JS-only where filters', () => {
68
- const query = { limit: 10, offset: 0, where: { name: 'Pasta' } };
68
+ it('does NOT include LIMIT/OFFSET when JS-only where filters exist', () => {
69
+ // author is a JS-only filter
70
+ const query = { limit: 10, offset: 0, where: { author: 'did:key:abc' } };
69
71
  const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
70
72
  expect(sparql).not.toContain('LIMIT');
71
73
  expect(sparql).not.toContain('OFFSET');
72
74
  });
73
75
  });
74
76
  describe('buildSPARQLOrderLimitOffset', () => {
75
- it('always returns empty string (pagination is handled in JS)', () => {
77
+ it('always returns empty string (pagination uses subquery pattern instead)', () => {
76
78
  expect(buildSPARQLOrderLimitOffset(emptyMetadata, {})).toBe('');
77
79
  expect(buildSPARQLOrderLimitOffset(emptyMetadata, { limit: 50 })).toBe('');
78
80
  expect(buildSPARQLOrderLimitOffset(emptyMetadata, { limit: 50, offset: 100 })).toBe('');
@@ -132,8 +134,9 @@ describe('buildSPARQLQuery — structural correctness', () => {
132
134
  };
133
135
  const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
134
136
  expect(sparql).toContain('<flux://channel-123>');
135
- // No SPARQL-level pagination
136
- expect(sparql).not.toContain('LIMIT');
137
+ // SPARQL-level pagination via subquery
138
+ expect(sparql).toContain('LIMIT 50');
139
+ expect(sparql).toContain('SELECT DISTINCT ?source');
137
140
  });
138
141
  it('does not allow injection through where clause IRI values', () => {
139
142
  const query = {
@@ -208,6 +211,67 @@ describe('buildSPARQLQuery — IRI correctness', () => {
208
211
  expect(sparql).not.toContain('fn::parse_literal');
209
212
  });
210
213
  });
214
+ // ──────────────────────────────────────────────────────────
215
+ // SPARQL-level pagination (LIMIT/OFFSET in generated queries)
216
+ // ──────────────────────────────────────────────────────────
217
+ describe('SPARQL-level pagination', () => {
218
+ const modelClass = {};
219
+ it('includes LIMIT in SPARQL when query specifies limit', () => {
220
+ const query = { limit: 30 };
221
+ const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
222
+ expect(sparql).toContain('LIMIT 30');
223
+ expect(sparql).toContain('SELECT DISTINCT ?source');
224
+ });
225
+ it('includes OFFSET in SPARQL when query specifies offset > 0', () => {
226
+ const query = { limit: 20, offset: 40 };
227
+ const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
228
+ expect(sparql).toContain('LIMIT 20');
229
+ expect(sparql).toContain('OFFSET 40');
230
+ });
231
+ it('does NOT include OFFSET when offset is 0', () => {
232
+ const query = { limit: 10, offset: 0 };
233
+ const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
234
+ expect(sparql).toContain('LIMIT 10');
235
+ expect(sparql).not.toContain('OFFSET');
236
+ });
237
+ it('includes ORDER BY in subquery when query.order is specified', () => {
238
+ const query = { limit: 10, order: { name: 'DESC' } };
239
+ const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
240
+ expect(sparql).toContain('ORDER BY DESC(');
241
+ expect(sparql).toContain('LIMIT 10');
242
+ });
243
+ it('does NOT default to ORDER BY timestamp when paginating without explicit order', () => {
244
+ const query = { limit: 30 };
245
+ const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
246
+ expect(sparql).not.toContain('ORDER BY');
247
+ expect(sparql).not.toContain('pg_minTs');
248
+ });
249
+ it('does NOT push pagination to SPARQL when JS-only where filters exist (author)', () => {
250
+ const query = { limit: 10, where: { author: 'did:key:abc' } };
251
+ const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
252
+ expect(sparql).not.toContain('LIMIT');
253
+ expect(sparql).not.toContain('OFFSET');
254
+ });
255
+ it('does NOT push pagination to SPARQL when JS-only where filters exist (gt operator)', () => {
256
+ const meta = {
257
+ properties: {
258
+ rating: { name: 'rating', predicate: 'flux://rating', required: true, resolveLanguage: 'literal' },
259
+ },
260
+ relations: {},
261
+ };
262
+ const query = { limit: 10, where: { rating: { gt: 5 } } };
263
+ const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
264
+ expect(sparql).not.toContain('LIMIT');
265
+ });
266
+ it('wraps pagination in a subquery (outer SELECT fetches all links for the page)', () => {
267
+ const query = { limit: 5, offset: 10 };
268
+ const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
269
+ expect(sparql).toContain('SELECT ?source ?predicate ?target ?author ?timestamp');
270
+ expect(sparql).toContain('SELECT DISTINCT ?source');
271
+ expect(sparql).toContain('LIMIT 5');
272
+ expect(sparql).toContain('OFFSET 10');
273
+ });
274
+ });
211
275
  describe('buildSPARQLQuery — set-difference patterns', () => {
212
276
  const modelClass = {};
213
277
  // Subgroups are grandchildren of channels (channel → conversation → subgroup).
@@ -228,3 +292,21 @@ describe('buildSPARQLQuery — set-difference patterns', () => {
228
292
  expect(sparql).toContain('SELECT ?source ?predicate ?target');
229
293
  });
230
294
  });
295
+ describe('buildSPARQLQuery — RDF 1.2 reifier patterns', () => {
296
+ const modelClass = {};
297
+ it('uses rdf:reifies instead of GRAPH for link metadata', () => {
298
+ const query = { limit: 10 };
299
+ const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
300
+ expect(sparql).toContain('<http://www.w3.org/1999/02/22-rdf-syntax-ns#reifies>');
301
+ expect(sparql).toContain('<http://www.w3.org/1999/02/22-rdf-syntax-ns#reifies> <<(');
302
+ expect(sparql).toContain('?_reifier');
303
+ expect(sparql).not.toContain('GRAPH');
304
+ expect(sparql).not.toContain('?linkGraph');
305
+ });
306
+ it('binds author and timestamp on the reifier node', () => {
307
+ const query = {};
308
+ const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
309
+ expect(sparql).toContain('?_reifier <ad4m://ontology/author> ?author');
310
+ expect(sparql).toContain('?_reifier <ad4m://ontology/timestamp> ?timestamp');
311
+ });
312
+ });
@@ -264,10 +264,24 @@ interpretationHint) {
264
264
  if (relMeta.filter === false) {
265
265
  relShape.filter = false;
266
266
  }
267
+ // Ordering is declared in the type system so the executor can act on it
268
+ // for every writer, not just this client.
269
+ if (relMeta.ordering) {
270
+ relShape.ordering = relMeta.ordering.strategy;
271
+ }
267
272
  // AD4M-specific metadata
268
273
  if (relMeta.local !== undefined) {
269
274
  relShape.local = relMeta.local;
270
275
  }
276
+ // Per-relation interpretation hint — sentence-level meaning that
277
+ // steers the harness LLM's `_propose_link_child` `predicate` field
278
+ // description. Read back on the Rust side by
279
+ // `ShaclProperty.interpretation_hint` and rendered into the tool
280
+ // schema alongside the predicate URI enum (so `basedOn` reads
281
+ // "prior beliefs this intention derives from", not just "some link").
282
+ if (relMeta.interpretationHint) {
283
+ relShape.interpretationHint = relMeta.interpretationHint;
284
+ }
271
285
  // Adder / Remover actions — only meaningful for relations backed
272
286
  // by a real link predicate. Getter-only relations are read-only.
273
287
  if (relMeta.predicate) {
@@ -404,6 +418,25 @@ interpretationHint) {
404
418
  }
405
419
  shape.addProperty(relShape);
406
420
  }
421
+ // ── Ordering links ─────────────────────────────────────────────────
422
+ // Ordering entries are stored on the *parent*, under one shared predicate,
423
+ // so that reconstruction needs no extra query: they arrive with the
424
+ // instance's own links. That only works if the instance query asks for them,
425
+ // and it builds its predicate filter from the shape's declared paths — so a
426
+ // class owning any ordered collection has to declare the predicate, or the
427
+ // very links the ordering depends on are filtered out of the read that needs
428
+ // them.
429
+ //
430
+ // One entry covers every ordered relation on the class; each entry names the
431
+ // relation it belongs to.
432
+ const hasOrderedRelation = Object.values(allRelationsMeta).some((m) => m.ordering);
433
+ if (hasOrderedRelation) {
434
+ shape.addProperty({
435
+ name: '_collectionOrder',
436
+ path: 'ad4m://collection_order',
437
+ nodeKind: 'IRI',
438
+ });
439
+ }
407
440
  // Always set constructor and destructor actions on the shape, even
408
441
  // when empty. An empty array serialises to `literal:string:[]`
409
442
  // which the Rust executor parses as a valid (no-op) command list,
@@ -367,6 +367,15 @@ export interface RelationMetadata {
367
367
  local?: boolean;
368
368
  /** Link direction: 'forward' for HasMany/HasOne, 'reverse' for BelongsToMany/BelongsToOne */
369
369
  direction?: 'forward' | 'reverse';
370
+ /**
371
+ * CRDT ordering config, when this collection has a user-controlled order.
372
+ *
373
+ * Its presence is what makes a reorder count as a change: for an ordered
374
+ * relation the sequence *is* the state, where an unordered one is a set.
375
+ */
376
+ ordering?: {
377
+ strategy: 'linkedList';
378
+ };
370
379
  /** Target model class thunk for hydration and type filtering */
371
380
  target?: () => any;
372
381
  /**