@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
@@ -1,57 +1,80 @@
1
1
  import { Literal } from "../Literal";
2
+ /**
3
+ * Runtime shape check for a decoded `ModelQuery`. Deserialization paths use
4
+ * this to guard against malformed guard arrays leaking into flow metadata
5
+ * (an untyped `Array.isArray` would happily accept `[null]` or `[{}]`).
6
+ * Only the mandatory `className: string` field is enforced — richer
7
+ * validation stays with the runtime engine that will actually execute the
8
+ * query.
9
+ */
10
+ function isModelQueryShape(v) {
11
+ if (typeof v !== "object" ||
12
+ v === null ||
13
+ typeof v.className !== "string" ||
14
+ v.className.length === 0) {
15
+ return false;
16
+ }
17
+ const or = v.or;
18
+ if (or !== undefined) {
19
+ if (!Array.isArray(or) || !or.every(isModelQueryShape))
20
+ return false;
21
+ }
22
+ return true;
23
+ }
24
+ /**
25
+ * Runtime shape check for a decoded `ConsensusRule`. Enforces the one
26
+ * mandatory field (`n: number` with a sane range) and, when `fromRole`
27
+ * is present, that it's a valid ModelQuery. A malformed literal (e.g.
28
+ * `{ n: "many" }` or `{ n: 1, fromRole: null }`) leaves the field unset
29
+ * rather than becoming corrupt flow metadata that downstream engines
30
+ * would trip over.
31
+ */
32
+ function isConsensusRuleShape(v) {
33
+ if (typeof v !== "object" || v === null)
34
+ return false;
35
+ const n = v.n;
36
+ if (typeof n !== "number" || !Number.isFinite(n) || n < 1 || !Number.isInteger(n)) {
37
+ return false;
38
+ }
39
+ const fromRole = v.fromRole;
40
+ if (fromRole !== undefined && !isModelQueryShape(fromRole))
41
+ return false;
42
+ return true;
43
+ }
2
44
  /**
3
45
  * SHACL Flow - represents a state machine for AD4M expressions
4
46
  *
5
47
  * Flows define:
6
- * - Which expressions can enter the flow (flowable condition)
7
- * - What states exist and how to detect them (via link patterns)
8
- * - How to transition between states (via actions)
48
+ * - Which expressions can enter the flow (via `inputTypes`)
49
+ * - What states exist and their per-state guards (via `requires`)
50
+ * - How to transition between states (via transitions + consensus rules)
51
+ *
52
+ * State lives on the on-graph `FlowInstanceRecord.currentState` and is
53
+ * read/written through the `FlowInstance` wrapper class
54
+ * ({@link FlowInstance.start} / {@link FlowInstance.findAll}) — see design
55
+ * doc §4.3/§5.
9
56
  *
10
57
  * @example
11
58
  * ```typescript
12
59
  * const todoFlow = new SHACLFlow('todo://TODO', 'todo://');
13
60
  *
14
61
  * // Any expression can become a TODO
15
- * todoFlow.flowable = 'any';
16
- *
17
- * // Define states
18
- * todoFlow.addState({
19
- * name: 'ready',
20
- * value: 0,
21
- * stateCheck: { predicate: 'todo://state', target: 'todo://ready' }
22
- * });
23
- * todoFlow.addState({
24
- * name: 'doing',
25
- * value: 0.5,
26
- * stateCheck: { predicate: 'todo://state', target: 'todo://doing' }
27
- * });
28
- * todoFlow.addState({
29
- * name: 'done',
30
- * value: 1,
31
- * stateCheck: { predicate: 'todo://state', target: 'todo://done' }
32
- * });
62
+ * todoFlow.inputTypes = ['any'];
33
63
  *
34
- * // Define start action
35
- * todoFlow.startAction = [{
36
- * action: 'addLink',
37
- * source: 'this',
38
- * predicate: 'todo://state',
39
- * target: 'todo://ready'
40
- * }];
64
+ * todoFlow.addState({ name: 'ready', value: 0 });
65
+ * todoFlow.addState({ name: 'doing', value: 0.5 });
66
+ * todoFlow.addState({ name: 'done', value: 1 });
41
67
  *
42
- * // Define transitions
43
68
  * todoFlow.addTransition({
44
69
  * actionName: 'Start',
45
70
  * fromState: 'ready',
46
71
  * toState: 'doing',
47
- * actions: [
48
- * { action: 'addLink', source: 'this', predicate: 'todo://state', target: 'todo://doing' },
49
- * { action: 'removeLink', source: 'this', predicate: 'todo://state', target: 'todo://ready' }
50
- * ]
72
+ * actions: [],
51
73
  * });
52
74
  *
53
- * // Store in perspective
75
+ * // Register on the perspective and mint an instance on a subject.
54
76
  * await perspective.addFlow('TODO', todoFlow);
77
+ * const instance = await FlowInstance.start(perspective, 'TODO', 'expr://123');
55
78
  * ```
56
79
  */
57
80
  export class SHACLFlow {
@@ -61,11 +84,41 @@ export class SHACLFlow {
61
84
  * @param namespace - Namespace for URIs (e.g., "todo://")
62
85
  */
63
86
  constructor(name, namespace) {
64
- /** Condition for which expressions can start this flow */
65
- this.flowable = "any";
66
- /** Actions to execute when starting the flow */
67
- this.startAction = [];
68
- /** States in this flow */
87
+ /**
88
+ * Subject-class URIs the flow accepts as its base expression.
89
+ *
90
+ * Design doc §4.1: a flow's *typed input*. When an
91
+ * expression whose class matches any URI in this array enters scope,
92
+ * the flow-spawn engine considers it a candidate base. Superseded
93
+ * the older link-level `flowable: LinkPattern` primitive — model-level
94
+ * matching by subject class instead of link matching. Empty array =
95
+ * flow declares no typed input. The `"any"` sentinel is accepted as
96
+ * a wildcard for tests and legacy flows.
97
+ */
98
+ this.inputTypes = [];
99
+ /**
100
+ * Subject-class URIs the flow must produce (linked to the base) for a
101
+ * run to count as complete.
102
+ *
103
+ * Design doc §4.1: a flow's *typed output*. Two jobs:
104
+ * (a) documentation for stateful flows — records what a completed run
105
+ * leaves in the graph; (b) **terminal condition** for zero-state flows —
106
+ * a flow with no `FlowState`s completes when at least one instance of
107
+ * each `outputTypes` entry exists in scope and is linked to the base,
108
+ * and `consensusRule` fires. This is what makes "Like" a first-class
109
+ * flow with no state machine.
110
+ */
111
+ this.outputTypes = [];
112
+ /**
113
+ * States in this flow.
114
+ *
115
+ * **Ordering convention (James PR #929 R9):** the array is stored sorted
116
+ * ascending by `value`. `states[0]` is the initial state — the one
117
+ * {@link FlowInstance.start} mints a fresh instance into. A flow author
118
+ * who wants a specific state as the entry point must give it the lowest
119
+ * `value` in the set. `fromLinks` enforces this sort, so link-order on
120
+ * the graph never dictates the initial state.
121
+ */
69
122
  this._states = [];
70
123
  /** Transitions between states */
71
124
  this._transitions = [];
@@ -133,27 +186,63 @@ export class SHACLFlow {
133
186
  predicate: "ad4m://flowName",
134
187
  target: Literal.from(this.name).toUrl()
135
188
  });
136
- // Flowable condition
137
- if (this.flowable === "any") {
189
+ // Top-level interpretation hint (optional, drives AI state suggestion).
190
+ // Empty strings are treated as "unset" so a round-trip through the graph
191
+ // doesn't materialise an empty-hint predicate that consumers would then
192
+ // read back as a meaningful value.
193
+ if (this.interpretationHint) {
194
+ links.push({
195
+ source: flowUri,
196
+ predicate: "ad4m://interpretationHint",
197
+ target: Literal.from(this.interpretationHint).toUrl()
198
+ });
199
+ }
200
+ // Typed I/O (flow-level, design §4.1). Both arrays are only
201
+ // serialized when non-empty — an empty list is indistinguishable
202
+ // from "unset" for a round-trip, and emitting one would give
203
+ // downstream consumers a meaningless predicate to interpret.
204
+ if (this.inputTypes.length > 0) {
205
+ links.push({
206
+ source: flowUri,
207
+ predicate: "ad4m://inputTypes",
208
+ target: `literal:string:${encodeURIComponent(JSON.stringify(this.inputTypes))}`
209
+ });
210
+ }
211
+ if (this.outputTypes.length > 0) {
138
212
  links.push({
139
213
  source: flowUri,
140
- predicate: "ad4m://flowable",
141
- target: "ad4m://any"
214
+ predicate: "ad4m://outputTypes",
215
+ target: `literal:string:${encodeURIComponent(JSON.stringify(this.outputTypes))}`
142
216
  });
143
217
  }
144
- else {
218
+ // Flow-level `creationHint` — same empty-string-is-unset guard as
219
+ // `interpretationHint`, same reason (round-trip fidelity).
220
+ if (this.creationHint) {
145
221
  links.push({
146
222
  source: flowUri,
147
- predicate: "ad4m://flowable",
148
- target: `literal:string:${encodeURIComponent(JSON.stringify(this.flowable))}`
223
+ predicate: "ad4m://creationHint",
224
+ target: Literal.from(this.creationHint).toUrl()
149
225
  });
150
226
  }
151
- // Start action
152
- if (this.startAction.length > 0) {
227
+ // Flow-level `context` — background queries the LLM sees but not
228
+ // guard evidence. Single JSON literal, consumers parse back.
229
+ if (this.context && this.context.length > 0) {
153
230
  links.push({
154
231
  source: flowUri,
155
- predicate: "ad4m://startAction",
156
- target: `literal:string:${encodeURIComponent(JSON.stringify(this.startAction))}`
232
+ predicate: "ad4m://context",
233
+ target: `literal:string:${encodeURIComponent(JSON.stringify(this.context))}`
234
+ });
235
+ }
236
+ // Flow-level `consensusRule` — default for state transitions when a
237
+ // FlowState omits its own rule; also the terminal-firing rule for
238
+ // zero-state flows. Serialized as one JSON literal (the shape is
239
+ // fixed and cheap to parse; consistent with `context` / `requires`
240
+ // so consumers only need one parser).
241
+ if (this.consensusRule) {
242
+ links.push({
243
+ source: flowUri,
244
+ predicate: "ad4m://consensusRule",
245
+ target: `literal:string:${encodeURIComponent(JSON.stringify(this.consensusRule))}`
157
246
  });
158
247
  }
159
248
  // States
@@ -183,12 +272,45 @@ export class SHACLFlow {
183
272
  predicate: "ad4m://stateValue",
184
273
  target: Literal.from(state.value).toUrl()
185
274
  });
186
- // State check pattern
187
- links.push({
188
- source: stateUri,
189
- predicate: "ad4m://stateCheck",
190
- target: `literal:string:${encodeURIComponent(JSON.stringify(state.stateCheck))}`
191
- });
275
+ // Per-state interpretation hint (optional, drives AI state suggestion).
276
+ // Empty strings treated as unset — same rationale as the flow-level hint.
277
+ if (state.interpretationHint) {
278
+ links.push({
279
+ source: stateUri,
280
+ predicate: "ad4m://interpretationHint",
281
+ target: Literal.from(state.interpretationHint).toUrl()
282
+ });
283
+ }
284
+ // Per-state `requires` guard — array of ModelQueries, AND-combined.
285
+ // Serialized as one JSON string on `ad4m://requires`. Consumers
286
+ // (state-transition engine, `flow.availableActions`) parse back.
287
+ if (state.requires && state.requires.length > 0) {
288
+ links.push({
289
+ source: stateUri,
290
+ predicate: "ad4m://requires",
291
+ target: `literal:string:${encodeURIComponent(JSON.stringify(state.requires))}`
292
+ });
293
+ }
294
+ // Per-state `semanticCheck` hint — optional English confirmation
295
+ // that runs after `requires` matches, for states where structural
296
+ // presence isn't sufficient evidence. Empty string = unset.
297
+ if (state.semanticCheck) {
298
+ links.push({
299
+ source: stateUri,
300
+ predicate: "ad4m://semanticCheck",
301
+ target: Literal.from(state.semanticCheck).toUrl()
302
+ });
303
+ }
304
+ // Per-state `consensusRule` override — one JSON literal on the
305
+ // state, same predicate name as the flow-level rule but scoped by
306
+ // source URI so the reader can tell them apart.
307
+ if (state.consensusRule) {
308
+ links.push({
309
+ source: stateUri,
310
+ predicate: "ad4m://consensusRule",
311
+ target: `literal:string:${encodeURIComponent(JSON.stringify(state.consensusRule))}`
312
+ });
313
+ }
192
314
  }
193
315
  // Transitions
194
316
  for (const transition of this._transitions) {
@@ -253,31 +375,96 @@ export class SHACLFlow {
253
375
  const namespace = withoutSuffix.slice(0, lastSlashOrColon + 1);
254
376
  const name = withoutSuffix.slice(lastSlashOrColon + 1);
255
377
  const flow = new SHACLFlow(name, namespace);
256
- // Find flowable condition
257
- const flowableLink = links.find(l => l.source === flowUri && l.predicate === "ad4m://flowable");
258
- if (flowableLink) {
259
- if (flowableLink.target === "ad4m://any") {
260
- flow.flowable = "any";
378
+ // Find top-level interpretation hint. Only accept a decoded value that
379
+ // is a non-empty string — malformed literals (Literal.get() can return
380
+ // an object, number, etc.) leave the field unset rather than becoming
381
+ // corrupt flow metadata.
382
+ const flowHintLink = links.find(l => l.source === flowUri && l.predicate === "ad4m://interpretationHint");
383
+ if (flowHintLink) {
384
+ try {
385
+ const decoded = Literal.fromUrl(flowHintLink.target).get();
386
+ if (typeof decoded === "string" && decoded.length > 0) {
387
+ flow.interpretationHint = decoded;
388
+ }
261
389
  }
262
- else {
263
- try {
264
- const jsonStr = flowableLink.target.replace(/^literal:\/\/string:|^literal:string:/, '');
265
- flow.flowable = JSON.parse(decodeURIComponent(jsonStr));
390
+ catch {
391
+ // Ignore parse errors — leave unset.
392
+ }
393
+ }
394
+ // Find typed I/O (flow-level, design §4.1). Both arrays validated
395
+ // element-wise — only accept string[] payloads. A malformed literal
396
+ // (`[null]`, `[42]`, non-array) leaves the field as its default
397
+ // empty array rather than corrupting flow metadata.
398
+ const inputTypesLink = links.find(l => l.source === flowUri && l.predicate === "ad4m://inputTypes");
399
+ if (inputTypesLink) {
400
+ try {
401
+ const jsonStr = inputTypesLink.target.replace(/^literal:\/\/string:|^literal:string:/, "");
402
+ const parsed = JSON.parse(decodeURIComponent(jsonStr));
403
+ if (Array.isArray(parsed) && parsed.every(v => typeof v === "string" && v.length > 0)) {
404
+ flow.inputTypes = parsed;
266
405
  }
267
- catch {
268
- flow.flowable = "any";
406
+ }
407
+ catch {
408
+ // Ignore parse errors — leave as default empty array.
409
+ }
410
+ }
411
+ const outputTypesLink = links.find(l => l.source === flowUri && l.predicate === "ad4m://outputTypes");
412
+ if (outputTypesLink) {
413
+ try {
414
+ const jsonStr = outputTypesLink.target.replace(/^literal:\/\/string:|^literal:string:/, "");
415
+ const parsed = JSON.parse(decodeURIComponent(jsonStr));
416
+ if (Array.isArray(parsed) && parsed.every(v => typeof v === "string" && v.length > 0)) {
417
+ flow.outputTypes = parsed;
418
+ }
419
+ }
420
+ catch {
421
+ // Ignore parse errors — leave as default empty array.
422
+ }
423
+ }
424
+ // Find flow-level `creationHint`. Non-empty-string guard — same
425
+ // rationale as `interpretationHint`.
426
+ const creationHintLink = links.find(l => l.source === flowUri && l.predicate === "ad4m://creationHint");
427
+ if (creationHintLink) {
428
+ try {
429
+ const decoded = Literal.fromUrl(creationHintLink.target).get();
430
+ if (typeof decoded === "string" && decoded.length > 0) {
431
+ flow.creationHint = decoded;
432
+ }
433
+ }
434
+ catch {
435
+ // Ignore parse errors — leave unset.
436
+ }
437
+ }
438
+ // Find flow-level `context` — same ModelQuery-shape guard as
439
+ // FlowState.requires (rejects `[null]`, `[{}]`, non-string
440
+ // `className`).
441
+ const contextLink = links.find(l => l.source === flowUri && l.predicate === "ad4m://context");
442
+ if (contextLink) {
443
+ try {
444
+ const jsonStr = contextLink.target.replace(/^literal:\/\/string:|^literal:string:/, "");
445
+ const parsed = JSON.parse(decodeURIComponent(jsonStr));
446
+ if (Array.isArray(parsed) && parsed.every(isModelQueryShape)) {
447
+ flow.context = parsed;
269
448
  }
270
449
  }
450
+ catch {
451
+ // Ignore parse errors — leave unset.
452
+ }
271
453
  }
272
- // Find start action
273
- const startActionLink = links.find(l => l.source === flowUri && l.predicate === "ad4m://startAction");
274
- if (startActionLink) {
454
+ // Find flow-level `consensusRule`. Reject malformed literals
455
+ // (missing `n`, non-integer `n`, invalid `fromRole`) so the runtime
456
+ // engine never has to defensively re-validate a stored rule.
457
+ const consensusRuleLink = links.find(l => l.source === flowUri && l.predicate === "ad4m://consensusRule");
458
+ if (consensusRuleLink) {
275
459
  try {
276
- const jsonStr = startActionLink.target.replace(/^literal:\/\/string:|^literal:string:/, '');
277
- flow.startAction = JSON.parse(decodeURIComponent(jsonStr));
460
+ const jsonStr = consensusRuleLink.target.replace(/^literal:\/\/string:|^literal:string:/, "");
461
+ const parsed = JSON.parse(decodeURIComponent(jsonStr));
462
+ if (isConsensusRuleShape(parsed)) {
463
+ flow.consensusRule = parsed;
464
+ }
278
465
  }
279
466
  catch {
280
- // Ignore parse errors
467
+ // Ignore parse errors — leave unset.
281
468
  }
282
469
  }
283
470
  // Find states
@@ -294,20 +481,85 @@ export class SHACLFlow {
294
481
  // Get state value
295
482
  const valueLink = links.find(l => l.source === stateUri && l.predicate === "ad4m://stateValue");
296
483
  const stateValue = valueLink ? Literal.fromUrl(valueLink.target).get() : 0;
297
- // Get state check
298
- const checkLink = links.find(l => l.source === stateUri && l.predicate === "ad4m://stateCheck");
299
- let stateCheck = { predicate: "", target: "" };
300
- if (checkLink) {
484
+ // Get per-state interpretation hint (optional). Same non-empty-string
485
+ // guard as the flow-level hint.
486
+ const hintLink = links.find(l => l.source === stateUri && l.predicate === "ad4m://interpretationHint");
487
+ let interpretationHint;
488
+ if (hintLink) {
301
489
  try {
302
- const jsonStr = checkLink.target.replace(/^literal:\/\/string:|^literal:string:/, '');
303
- stateCheck = JSON.parse(decodeURIComponent(jsonStr));
490
+ const decoded = Literal.fromUrl(hintLink.target).get();
491
+ if (typeof decoded === "string" && decoded.length > 0) {
492
+ interpretationHint = decoded;
493
+ }
304
494
  }
305
495
  catch {
306
- // Ignore parse errors
496
+ // Ignore parse errors — leave unset.
497
+ }
498
+ }
499
+ // Get per-state `requires` guard (optional) — one link carrying a
500
+ // JSON-encoded ModelQuery[]. Reject arrays whose entries are not
501
+ // ModelQuery-shaped (missing/non-string `className`, `null`, primitives).
502
+ const requiresLink = links.find(l => l.source === stateUri && l.predicate === "ad4m://requires");
503
+ let requires;
504
+ if (requiresLink) {
505
+ try {
506
+ const jsonStr = requiresLink.target.replace(/^literal:\/\/string:|^literal:string:/, "");
507
+ const parsed = JSON.parse(decodeURIComponent(jsonStr));
508
+ if (Array.isArray(parsed) && parsed.every(isModelQueryShape)) {
509
+ requires = parsed;
510
+ }
511
+ }
512
+ catch {
513
+ // Ignore parse errors — leave requires unset rather than crash
514
+ }
515
+ }
516
+ // Get per-state `semanticCheck` hint (optional). Non-empty-string guard.
517
+ const semanticCheckLink = links.find(l => l.source === stateUri && l.predicate === "ad4m://semanticCheck");
518
+ let semanticCheck;
519
+ if (semanticCheckLink) {
520
+ try {
521
+ const decoded = Literal.fromUrl(semanticCheckLink.target).get();
522
+ if (typeof decoded === "string" && decoded.length > 0) {
523
+ semanticCheck = decoded;
524
+ }
525
+ }
526
+ catch {
527
+ // Ignore parse errors — leave unset.
528
+ }
529
+ }
530
+ // Get per-state `consensusRule` override (optional). Same shape
531
+ // guard as the flow-level rule; a malformed literal (missing `n`,
532
+ // non-integer, invalid `fromRole`) leaves the field unset.
533
+ const stateConsensusLink = links.find(l => l.source === stateUri && l.predicate === "ad4m://consensusRule");
534
+ let consensusRule;
535
+ if (stateConsensusLink) {
536
+ try {
537
+ const jsonStr = stateConsensusLink.target.replace(/^literal:\/\/string:|^literal:string:/, "");
538
+ const parsed = JSON.parse(decodeURIComponent(jsonStr));
539
+ if (isConsensusRuleShape(parsed)) {
540
+ consensusRule = parsed;
541
+ }
542
+ }
543
+ catch {
544
+ // Ignore parse errors — leave unset.
307
545
  }
308
546
  }
309
- flow.addState({ name: stateName, value: stateValue, stateCheck });
547
+ flow.addState({
548
+ name: stateName,
549
+ value: stateValue,
550
+ ...(interpretationHint !== undefined ? { interpretationHint } : {}),
551
+ ...(requires !== undefined ? { requires } : {}),
552
+ ...(semanticCheck !== undefined ? { semanticCheck } : {}),
553
+ ...(consensusRule !== undefined ? { consensusRule } : {}),
554
+ });
310
555
  }
556
+ // Perspective link storage does not preserve insertion order — `hasState`
557
+ // links come back in arbitrary order from the graph, so `flow.states[0]`
558
+ // was randomly one of the round-tripped states. Sort by `value` ascending
559
+ // so the "initial state = states[0]" convention (used by
560
+ // `FlowInstance.start`) survives a fromGraph round trip.
561
+ // The `states` getter returns a defensive copy, so mutate `_states` directly.
562
+ flow._states.sort((a, b) => a.value - b.value);
311
563
  // Find transitions
312
564
  const transitionLinks = links.filter(l => l.source === flowUri && l.predicate === "ad4m://hasTransition");
313
565
  for (const transitionLink of transitionLinks) {
@@ -343,13 +595,31 @@ export class SHACLFlow {
343
595
  * Convert to JSON representation
344
596
  */
345
597
  toJSON() {
598
+ // Per-state metadata is optional; strip empty strings / empty arrays so
599
+ // json.states[i] agrees with what toLinks() would materialise (a state
600
+ // constructed with `interpretationHint: ""` produces no predicate, so
601
+ // JSON must not carry the empty field either).
602
+ const sanitizedStates = this._states.map(s => ({
603
+ name: s.name,
604
+ value: s.value,
605
+ ...(s.interpretationHint ? { interpretationHint: s.interpretationHint } : {}),
606
+ ...(s.requires && s.requires.length > 0 ? { requires: s.requires } : {}),
607
+ ...(s.semanticCheck ? { semanticCheck: s.semanticCheck } : {}),
608
+ ...(s.consensusRule ? { consensusRule: s.consensusRule } : {})
609
+ }));
346
610
  return {
347
611
  name: this.name,
348
612
  namespace: this.namespace,
349
- flowable: this.flowable,
350
- startAction: this.startAction,
351
- states: this._states,
352
- transitions: this._transitions
613
+ states: sanitizedStates,
614
+ transitions: this._transitions,
615
+ // Empty strings / empty arrays treated as unset — same semantics
616
+ // as the toLinks path, so JSON <-> links round-trips agree.
617
+ ...(this.interpretationHint ? { interpretationHint: this.interpretationHint } : {}),
618
+ ...(this.inputTypes.length > 0 ? { inputTypes: this.inputTypes } : {}),
619
+ ...(this.outputTypes.length > 0 ? { outputTypes: this.outputTypes } : {}),
620
+ ...(this.creationHint ? { creationHint: this.creationHint } : {}),
621
+ ...(this.context && this.context.length > 0 ? { context: this.context } : {}),
622
+ ...(this.consensusRule ? { consensusRule: this.consensusRule } : {})
353
623
  };
354
624
  }
355
625
  /**
@@ -357,10 +627,49 @@ export class SHACLFlow {
357
627
  */
358
628
  static fromJSON(json) {
359
629
  const flow = new SHACLFlow(json.name, json.namespace);
360
- flow.flowable = json.flowable || "any";
361
- flow.startAction = json.startAction || [];
630
+ // Non-empty-string guard — mirrors the toLinks-side reader.
631
+ if (typeof json.interpretationHint === "string" && json.interpretationHint.length > 0) {
632
+ flow.interpretationHint = json.interpretationHint;
633
+ }
634
+ // Typed I/O (design §4.1). Same string[]/ModelQuery[] shape guards
635
+ // as the fromLinks path — malformed payloads leave defaults untouched.
636
+ if (Array.isArray(json.inputTypes) &&
637
+ json.inputTypes.every((v) => typeof v === "string" && v.length > 0)) {
638
+ flow.inputTypes = json.inputTypes;
639
+ }
640
+ if (Array.isArray(json.outputTypes) &&
641
+ json.outputTypes.every((v) => typeof v === "string" && v.length > 0)) {
642
+ flow.outputTypes = json.outputTypes;
643
+ }
644
+ if (typeof json.creationHint === "string" && json.creationHint.length > 0) {
645
+ flow.creationHint = json.creationHint;
646
+ }
647
+ if (Array.isArray(json.context) && json.context.every(isModelQueryShape)) {
648
+ flow.context = json.context;
649
+ }
650
+ if (isConsensusRuleShape(json.consensusRule)) {
651
+ flow.consensusRule = json.consensusRule;
652
+ }
362
653
  for (const state of json.states || []) {
363
- flow.addState(state);
654
+ // Same validation as the toLinks path: reject malformed optional fields
655
+ // rather than let them poison downstream consumers.
656
+ const sanitized = {
657
+ name: state.name,
658
+ value: state.value,
659
+ };
660
+ if (typeof state.interpretationHint === "string" && state.interpretationHint.length > 0) {
661
+ sanitized.interpretationHint = state.interpretationHint;
662
+ }
663
+ if (Array.isArray(state.requires) && state.requires.every(isModelQueryShape)) {
664
+ sanitized.requires = state.requires;
665
+ }
666
+ if (typeof state.semanticCheck === "string" && state.semanticCheck.length > 0) {
667
+ sanitized.semanticCheck = state.semanticCheck;
668
+ }
669
+ if (isConsensusRuleShape(state.consensusRule)) {
670
+ sanitized.consensusRule = state.consensusRule;
671
+ }
672
+ flow.addState(sanitized);
364
673
  }
365
674
  for (const transition of json.transitions || []) {
366
675
  flow.addTransition(transition);
@@ -33,6 +33,14 @@ export interface SHACLPropertyShape {
33
33
  path: string;
34
34
  /** Expected datatype (e.g., xsd:string, xsd:integer) */
35
35
  datatype?: string;
36
+ /**
37
+ * CRDT ordering strategy for this collection ("linkedList"), when it has one.
38
+ *
39
+ * Declared in the shape rather than passed per query so the executor applies
40
+ * it to every writer — the ORM, MCP agents, raw WS-RPC callers, another app
41
+ * on the same neighbourhood.
42
+ */
43
+ ordering?: string;
36
44
  /** Node kind constraint (IRI, Literal, BlankNode) */
37
45
  nodeKind?: 'IRI' | 'Literal' | 'BlankNode';
38
46
  /** Minimum cardinality (required if >= 1) */
@@ -274,6 +274,13 @@ export class SHACLShape {
274
274
  target: prop.path
275
275
  });
276
276
  // Constraints
277
+ if (prop.ordering) {
278
+ links.push({
279
+ source: propShapeId,
280
+ predicate: "ad4m://ordering",
281
+ target: `literal:string:${prop.ordering}`
282
+ });
283
+ }
277
284
  if (prop.datatype) {
278
285
  links.push({
279
286
  source: propShapeId,
@@ -520,6 +527,10 @@ export class SHACLShape {
520
527
  const datatypeLink = links.find(l => l.source === propShapeId && l.predicate === "sh://datatype");
521
528
  if (datatypeLink)
522
529
  prop.datatype = datatypeLink.target;
530
+ const orderingLink = links.find(l => l.source === propShapeId && l.predicate === "ad4m://ordering");
531
+ if (orderingLink) {
532
+ prop.ordering = orderingLink.target.replace(/^literal:\/\/string:|^literal:string:/, '');
533
+ }
523
534
  const nodeKindLink = links.find(l => l.source === propShapeId && l.predicate === "sh://nodeKind");
524
535
  if (nodeKindLink) {
525
536
  prop.nodeKind = nodeKindLink.target.replace('sh://', '');
@@ -757,6 +768,11 @@ export class SHACLShape {
757
768
  transform: p.transform,
758
769
  interpretation_hint: p.interpretationHint,
759
770
  identity: p.identity,
771
+ // The backend reads the ordering declaration back from the
772
+ // `ad4m://ordering` link that `parse_shacl_to_links` writes from this
773
+ // field. `ensureSubjectClass` ships `toJSON()`, so omitting it here
774
+ // leaves `@HasMany({ ordering })` inert no matter what `toLinks()` does.
775
+ ordering: p.ordering,
760
776
  })),
761
777
  constructor_actions: this.constructor_actions,
762
778
  destructor_actions: this.destructor_actions,
@@ -804,6 +820,7 @@ export class SHACLShape {
804
820
  transform: p.transform,
805
821
  interpretationHint: p.interpretation_hint,
806
822
  identity: p.identity,
823
+ ordering: p.ordering,
807
824
  });
808
825
  }
809
826
  if (json.constructor_actions) {