@coasys/ad4m 0.13.0-test-interpretation-1 → 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 (279) hide show
  1. package/lib/index.cjs +2409 -625
  2. package/lib/index.js +2406 -626
  3. package/lib/src/Literal.js +8 -0
  4. package/lib/src/ai/AIClient.js +6 -3
  5. package/lib/src/apiClient.d.ts +40 -4
  6. package/lib/src/apiClient.js +149 -9
  7. package/lib/src/generated/api/RunInterpretationRequest.d.ts +16 -0
  8. package/lib/src/index.d.ts +3 -1
  9. package/lib/src/index.js +2 -0
  10. package/lib/src/model/Ad4mModel.d.ts +34 -16
  11. package/lib/src/model/Ad4mModel.js +302 -63
  12. package/lib/src/model/Ad4mModel.test.js +480 -735
  13. package/lib/src/model/ModelQueryBuilder.js +2 -2
  14. package/lib/src/model/decorators.d.ts +199 -12
  15. package/lib/src/model/decorators.js +88 -32
  16. package/lib/src/model/json-schema.js +7 -21
  17. package/lib/src/model/model-manifest.d.ts +41 -0
  18. package/lib/src/model/model-manifest.js +11 -0
  19. package/lib/src/model/query-common.d.ts +2 -2
  20. package/lib/src/model/query-sparql.d.ts +62 -0
  21. package/lib/src/model/query-sparql.js +78 -15
  22. package/lib/src/model/query-sparql.test.js +92 -10
  23. package/lib/src/model/query-utils.d.ts +7 -2
  24. package/lib/src/model/query-utils.js +21 -11
  25. package/lib/src/model/shacl-gen.js +77 -15
  26. package/lib/src/model/types.d.ts +32 -8
  27. package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +33 -33
  28. package/lib/src/perspectives/AutoProcessor.d.ts +198 -2
  29. package/lib/src/perspectives/FlowInstance.d.ts +187 -0
  30. package/lib/src/perspectives/FlowInstance.js +336 -0
  31. package/lib/src/perspectives/FlowModels.d.ts +94 -0
  32. package/lib/src/perspectives/FlowModels.js +182 -0
  33. package/lib/src/perspectives/InterpretationModels.d.ts +41 -3
  34. package/lib/src/perspectives/InterpretationModels.js +60 -26
  35. package/lib/src/perspectives/PerspectiveClient.d.ts +88 -7
  36. package/lib/src/perspectives/PerspectiveClient.js +138 -11
  37. package/lib/src/perspectives/PerspectiveProxy.d.ts +150 -57
  38. package/lib/src/perspectives/PerspectiveProxy.js +411 -287
  39. package/lib/src/perspectives/SparqlBindings.d.ts +24 -3
  40. package/lib/src/perspectives/SparqlBindings.js +57 -6
  41. package/lib/src/shacl/SHACLFlow.d.ts +273 -47
  42. package/lib/src/shacl/SHACLFlow.js +397 -88
  43. package/lib/src/shacl/SHACLShape.d.ts +19 -3
  44. package/lib/src/shacl/SHACLShape.js +30 -1
  45. package/lib/src/shacl/SHACLShape.test.js +82 -0
  46. package/lib/src/shacl/index.d.ts +1 -1
  47. package/lib/src/shacl/index.js +1 -1
  48. package/package.json +3 -3
  49. package/src/Literal.ts +7 -0
  50. package/src/ai/AIClient.ts +6 -3
  51. package/src/apiClient.test.ts +227 -0
  52. package/src/apiClient.ts +185 -9
  53. package/src/generated/api/RunInterpretationRequest.ts +19 -3
  54. package/src/index.ts +3 -1
  55. package/src/model/Ad4mModel.test.ts +729 -22
  56. package/src/model/Ad4mModel.ts +305 -66
  57. package/src/model/ModelQueryBuilder.ts +2 -2
  58. package/src/model/decorators.ts +278 -44
  59. package/src/model/json-schema.ts +10 -25
  60. package/src/model/query-common.ts +2 -2
  61. package/src/model/query-sparql.test.ts +114 -13
  62. package/src/model/query-sparql.ts +83 -14
  63. package/src/model/query-utils.ts +22 -12
  64. package/src/model/relation-filtering.test.ts +70 -20
  65. package/src/model/shacl-gen.ts +78 -13
  66. package/src/model/shacl-roundtrip.test.ts +134 -2
  67. package/src/model/types.ts +30 -8
  68. package/src/perspectives/AutoProcessor.ts +204 -2
  69. package/src/perspectives/FlowInstance.ts +369 -0
  70. package/src/perspectives/FlowModels.ts +157 -0
  71. package/src/perspectives/InterpretationModels.ts +91 -21
  72. package/src/perspectives/PerspectiveClient.test.ts +67 -6
  73. package/src/perspectives/PerspectiveClient.ts +190 -14
  74. package/src/perspectives/PerspectiveProxy.test.ts +406 -0
  75. package/src/perspectives/PerspectiveProxy.ts +494 -308
  76. package/src/perspectives/SparqlBindings.test.ts +134 -0
  77. package/src/perspectives/SparqlBindings.ts +57 -6
  78. package/src/perspectives/shacl-rpc.test.ts +258 -0
  79. package/src/shacl/SHACLFlow.test.ts +762 -35
  80. package/src/shacl/SHACLFlow.ts +680 -112
  81. package/src/shacl/SHACLShape.test.ts +30 -4
  82. package/src/shacl/SHACLShape.ts +59 -6
  83. package/src/shacl/index.ts +1 -1
  84. package/lib/src/generated/rest/AIModelLoadingStatus.d.ts +0 -7
  85. package/lib/src/generated/rest/AIModelLoadingStatus.js +0 -2
  86. package/lib/src/generated/rest/AddAgentInfosRequest.d.ts +0 -3
  87. package/lib/src/generated/rest/AddAgentInfosRequest.js +0 -2
  88. package/lib/src/generated/rest/AddLinkExpressionRequest.d.ts +0 -6
  89. package/lib/src/generated/rest/AddLinkExpressionRequest.js +0 -1
  90. package/lib/src/generated/rest/AddLinkRequest.d.ts +0 -6
  91. package/lib/src/generated/rest/AddLinkRequest.js +0 -1
  92. package/lib/src/generated/rest/AddLinksBulkRequest.d.ts +0 -6
  93. package/lib/src/generated/rest/AddLinksBulkRequest.js +0 -1
  94. package/lib/src/generated/rest/AddSdnaRequest.d.ts +0 -6
  95. package/lib/src/generated/rest/AddSdnaRequest.js +0 -2
  96. package/lib/src/generated/rest/ApplyTemplateRequest.d.ts +0 -4
  97. package/lib/src/generated/rest/ApplyTemplateRequest.js +0 -2
  98. package/lib/src/generated/rest/AuthInfoInput.d.ts +0 -10
  99. package/lib/src/generated/rest/AuthInfoInput.js +0 -1
  100. package/lib/src/generated/rest/BroadcastRequest.d.ts +0 -6
  101. package/lib/src/generated/rest/BroadcastRequest.js +0 -1
  102. package/lib/src/generated/rest/CapabilityInput.d.ts +0 -5
  103. package/lib/src/generated/rest/CapabilityInput.js +0 -1
  104. package/lib/src/generated/rest/CommitBatchRequest.d.ts +0 -3
  105. package/lib/src/generated/rest/CommitBatchRequest.js +0 -2
  106. package/lib/src/generated/rest/ComputeLogEntry.d.ts +0 -9
  107. package/lib/src/generated/rest/ComputeLogEntry.js +0 -2
  108. package/lib/src/generated/rest/CreateExpressionRequest.d.ts +0 -4
  109. package/lib/src/generated/rest/CreateExpressionRequest.js +0 -2
  110. package/lib/src/generated/rest/CreatePerspectiveRequest.d.ts +0 -3
  111. package/lib/src/generated/rest/CreatePerspectiveRequest.js +0 -2
  112. package/lib/src/generated/rest/CreateSubjectRequest.d.ts +0 -6
  113. package/lib/src/generated/rest/CreateSubjectRequest.js +0 -2
  114. package/lib/src/generated/rest/CreateUserRequest.d.ts +0 -4
  115. package/lib/src/generated/rest/CreateUserRequest.js +0 -2
  116. package/lib/src/generated/rest/CreateUserResponse.d.ts +0 -5
  117. package/lib/src/generated/rest/CreateUserResponse.js +0 -2
  118. package/lib/src/generated/rest/DecoratedExpressionProof.d.ts +0 -6
  119. package/lib/src/generated/rest/DecoratedExpressionProof.js +0 -2
  120. package/lib/src/generated/rest/DecoratedLinkExpression.d.ts +0 -10
  121. package/lib/src/generated/rest/DecoratedLinkExpression.js +0 -1
  122. package/lib/src/generated/rest/DisposeQueryRequest.d.ts +0 -3
  123. package/lib/src/generated/rest/DisposeQueryRequest.js +0 -2
  124. package/lib/src/generated/rest/EmailTestRequest.d.ts +0 -6
  125. package/lib/src/generated/rest/EmailTestRequest.js +0 -2
  126. package/lib/src/generated/rest/EmbedRequest.d.ts +0 -4
  127. package/lib/src/generated/rest/EmbedRequest.js +0 -2
  128. package/lib/src/generated/rest/EntanglementProofInput.d.ts +0 -6
  129. package/lib/src/generated/rest/EntanglementProofInput.js +0 -2
  130. package/lib/src/generated/rest/EntanglementProofPreflightRequest.d.ts +0 -4
  131. package/lib/src/generated/rest/EntanglementProofPreflightRequest.js +0 -2
  132. package/lib/src/generated/rest/ExecuteCommandsRequest.d.ts +0 -4
  133. package/lib/src/generated/rest/ExecuteCommandsRequest.js +0 -2
  134. package/lib/src/generated/rest/ExportRequest.d.ts +0 -5
  135. package/lib/src/generated/rest/ExportRequest.js +0 -2
  136. package/lib/src/generated/rest/ExpressionManyRequest.d.ts +0 -3
  137. package/lib/src/generated/rest/ExpressionManyRequest.js +0 -2
  138. package/lib/src/generated/rest/ExpressionProof.d.ts +0 -4
  139. package/lib/src/generated/rest/ExpressionProof.js +0 -2
  140. package/lib/src/generated/rest/ExpressionProofInput.d.ts +0 -6
  141. package/lib/src/generated/rest/ExpressionProofInput.js +0 -2
  142. package/lib/src/generated/rest/FriendSendMessageRequest.d.ts +0 -3
  143. package/lib/src/generated/rest/FriendSendMessageRequest.js +0 -2
  144. package/lib/src/generated/rest/FriendsListRequest.d.ts +0 -3
  145. package/lib/src/generated/rest/FriendsListRequest.js +0 -2
  146. package/lib/src/generated/rest/GenerateAgentRequest.d.ts +0 -3
  147. package/lib/src/generated/rest/GenerateAgentRequest.js +0 -2
  148. package/lib/src/generated/rest/GenerateJwtRequest.d.ts +0 -4
  149. package/lib/src/generated/rest/GenerateJwtRequest.js +0 -2
  150. package/lib/src/generated/rest/GetSubjectDataRequest.d.ts +0 -4
  151. package/lib/src/generated/rest/GetSubjectDataRequest.js +0 -2
  152. package/lib/src/generated/rest/HostingInfoResponse.d.ts +0 -5
  153. package/lib/src/generated/rest/HostingInfoResponse.js +0 -2
  154. package/lib/src/generated/rest/HostingWalletResponse.d.ts +0 -4
  155. package/lib/src/generated/rest/HostingWalletResponse.js +0 -2
  156. package/lib/src/generated/rest/ImportAgentRequest.d.ts +0 -6
  157. package/lib/src/generated/rest/ImportAgentRequest.js +0 -2
  158. package/lib/src/generated/rest/ImportRequest.d.ts +0 -4
  159. package/lib/src/generated/rest/ImportRequest.js +0 -2
  160. package/lib/src/generated/rest/JoinNeighbourhoodRequest.d.ts +0 -3
  161. package/lib/src/generated/rest/JoinNeighbourhoodRequest.js +0 -2
  162. package/lib/src/generated/rest/KeepAliveQueryRequest.d.ts +0 -3
  163. package/lib/src/generated/rest/KeepAliveQueryRequest.js +0 -2
  164. package/lib/src/generated/rest/LanguageMetaInput.d.ts +0 -6
  165. package/lib/src/generated/rest/LanguageMetaInput.js +0 -2
  166. package/lib/src/generated/rest/Link.d.ts +0 -5
  167. package/lib/src/generated/rest/Link.js +0 -2
  168. package/lib/src/generated/rest/LinkExpression.d.ts +0 -10
  169. package/lib/src/generated/rest/LinkExpression.js +0 -1
  170. package/lib/src/generated/rest/LinkExpressionInput.d.ts +0 -10
  171. package/lib/src/generated/rest/LinkExpressionInput.js +0 -1
  172. package/lib/src/generated/rest/LinkInput.d.ts +0 -5
  173. package/lib/src/generated/rest/LinkInput.js +0 -2
  174. package/lib/src/generated/rest/LinkLanguageTemplatesRequest.d.ts +0 -3
  175. package/lib/src/generated/rest/LinkLanguageTemplatesRequest.js +0 -2
  176. package/lib/src/generated/rest/LinkMutationRequest.d.ts +0 -12
  177. package/lib/src/generated/rest/LinkMutationRequest.js +0 -1
  178. package/lib/src/generated/rest/LinkMutationResponse.d.ts +0 -9
  179. package/lib/src/generated/rest/LinkMutationResponse.js +0 -1
  180. package/lib/src/generated/rest/LinkMutations.d.ts +0 -6
  181. package/lib/src/generated/rest/LinkMutations.js +0 -1
  182. package/lib/src/generated/rest/LinkMutationsRequest.d.ts +0 -5
  183. package/lib/src/generated/rest/LinkMutationsRequest.js +0 -1
  184. package/lib/src/generated/rest/LinkStatus.d.ts +0 -1
  185. package/lib/src/generated/rest/LinkStatus.js +0 -2
  186. package/lib/src/generated/rest/LinkUpdateInput.d.ts +0 -5
  187. package/lib/src/generated/rest/LinkUpdateInput.js +0 -1
  188. package/lib/src/generated/rest/LockAgentRequest.d.ts +0 -3
  189. package/lib/src/generated/rest/LockAgentRequest.js +0 -2
  190. package/lib/src/generated/rest/LoginUserRequest.d.ts +0 -5
  191. package/lib/src/generated/rest/LoginUserRequest.js +0 -2
  192. package/lib/src/generated/rest/ModelType.d.ts +0 -1
  193. package/lib/src/generated/rest/ModelType.js +0 -2
  194. package/lib/src/generated/rest/NotificationGrantRequest.d.ts +0 -3
  195. package/lib/src/generated/rest/NotificationGrantRequest.js +0 -2
  196. package/lib/src/generated/rest/NotificationInput.d.ts +0 -10
  197. package/lib/src/generated/rest/NotificationInput.js +0 -2
  198. package/lib/src/generated/rest/OpenLinkRequest.d.ts +0 -3
  199. package/lib/src/generated/rest/OpenLinkRequest.js +0 -2
  200. package/lib/src/generated/rest/PermitCapabilityRequest.d.ts +0 -3
  201. package/lib/src/generated/rest/PermitCapabilityRequest.js +0 -2
  202. package/lib/src/generated/rest/PerspectiveInput.d.ts +0 -4
  203. package/lib/src/generated/rest/PerspectiveInput.js +0 -1
  204. package/lib/src/generated/rest/PromptRequest.d.ts +0 -4
  205. package/lib/src/generated/rest/PromptRequest.js +0 -2
  206. package/lib/src/generated/rest/PublicPerspectiveInput.d.ts +0 -4
  207. package/lib/src/generated/rest/PublicPerspectiveInput.js +0 -1
  208. package/lib/src/generated/rest/PublishLanguageRequest.d.ts +0 -5
  209. package/lib/src/generated/rest/PublishLanguageRequest.js +0 -1
  210. package/lib/src/generated/rest/PublishNeighbourhoodRequest.d.ts +0 -6
  211. package/lib/src/generated/rest/PublishNeighbourhoodRequest.js +0 -1
  212. package/lib/src/generated/rest/QueryRequest.d.ts +0 -7
  213. package/lib/src/generated/rest/QueryRequest.js +0 -2
  214. package/lib/src/generated/rest/RemoveLinkRequest.d.ts +0 -5
  215. package/lib/src/generated/rest/RemoveLinkRequest.js +0 -1
  216. package/lib/src/generated/rest/RemoveLinksBulkRequest.d.ts +0 -5
  217. package/lib/src/generated/rest/RemoveLinksBulkRequest.js +0 -1
  218. package/lib/src/generated/rest/RequestCapabilityRequest.d.ts +0 -4
  219. package/lib/src/generated/rest/RequestCapabilityRequest.js +0 -1
  220. package/lib/src/generated/rest/RequestPaymentRequest.d.ts +0 -3
  221. package/lib/src/generated/rest/RequestPaymentRequest.js +0 -2
  222. package/lib/src/generated/rest/RequestVerificationRequest.d.ts +0 -4
  223. package/lib/src/generated/rest/RequestVerificationRequest.js +0 -2
  224. package/lib/src/generated/rest/ResourceInput.d.ts +0 -4
  225. package/lib/src/generated/rest/ResourceInput.js +0 -2
  226. package/lib/src/generated/rest/SetDefaultModelRequest.d.ts +0 -4
  227. package/lib/src/generated/rest/SetDefaultModelRequest.js +0 -1
  228. package/lib/src/generated/rest/SetFreeHostingEnabledRequest.d.ts +0 -3
  229. package/lib/src/generated/rest/SetFreeHostingEnabledRequest.js +0 -2
  230. package/lib/src/generated/rest/SetHotWalletAddressRequest.d.ts +0 -3
  231. package/lib/src/generated/rest/SetHotWalletAddressRequest.js +0 -2
  232. package/lib/src/generated/rest/SetMultiUserRequest.d.ts +0 -3
  233. package/lib/src/generated/rest/SetMultiUserRequest.js +0 -2
  234. package/lib/src/generated/rest/SetOnlineStatusRequest.d.ts +0 -5
  235. package/lib/src/generated/rest/SetOnlineStatusRequest.js +0 -1
  236. package/lib/src/generated/rest/SetStatusRequest.d.ts +0 -3
  237. package/lib/src/generated/rest/SetStatusRequest.js +0 -2
  238. package/lib/src/generated/rest/SetUserFreeAccessRequest.d.ts +0 -4
  239. package/lib/src/generated/rest/SetUserFreeAccessRequest.js +0 -2
  240. package/lib/src/generated/rest/SignMessageRequest.d.ts +0 -3
  241. package/lib/src/generated/rest/SignMessageRequest.js +0 -2
  242. package/lib/src/generated/rest/SignalRequest.d.ts +0 -6
  243. package/lib/src/generated/rest/SignalRequest.js +0 -1
  244. package/lib/src/generated/rest/SubscribeQueryRequest.d.ts +0 -3
  245. package/lib/src/generated/rest/SubscribeQueryRequest.js +0 -2
  246. package/lib/src/generated/rest/SubscribeQueryResponse.d.ts +0 -4
  247. package/lib/src/generated/rest/SubscribeQueryResponse.js +0 -2
  248. package/lib/src/generated/rest/TrustedAgentsRequest.d.ts +0 -3
  249. package/lib/src/generated/rest/TrustedAgentsRequest.js +0 -2
  250. package/lib/src/generated/rest/UnlockAgentRequest.d.ts +0 -4
  251. package/lib/src/generated/rest/UnlockAgentRequest.js +0 -2
  252. package/lib/src/generated/rest/UpdateLinkRequest.d.ts +0 -7
  253. package/lib/src/generated/rest/UpdateLinkRequest.js +0 -1
  254. package/lib/src/generated/rest/UpdatePerspectiveRequest.d.ts +0 -3
  255. package/lib/src/generated/rest/UpdatePerspectiveRequest.js +0 -2
  256. package/lib/src/generated/rest/UpdateProfileRequest.d.ts +0 -5
  257. package/lib/src/generated/rest/UpdateProfileRequest.js +0 -1
  258. package/lib/src/generated/rest/UserStatistics.d.ts +0 -9
  259. package/lib/src/generated/rest/UserStatistics.js +0 -2
  260. package/lib/src/generated/rest/VerificationRequestResult.d.ts +0 -6
  261. package/lib/src/generated/rest/VerificationRequestResult.js +0 -2
  262. package/lib/src/generated/rest/VerifyEmailRequest.d.ts +0 -6
  263. package/lib/src/generated/rest/VerifyEmailRequest.js +0 -2
  264. package/lib/src/generated/rest/VerifySignatureRequest.d.ts +0 -5
  265. package/lib/src/generated/rest/VerifySignatureRequest.js +0 -2
  266. package/lib/src/generated/rest/WriteSettingsRequest.d.ts +0 -3
  267. package/lib/src/generated/rest/WriteSettingsRequest.js +0 -2
  268. package/lib/src/generated/rest/index.d.ts +0 -73
  269. package/lib/src/generated/rest/index.js +0 -8
  270. package/lib/src/generated/rest/routes.d.ts +0 -587
  271. package/lib/src/generated/rest/routes.js +0 -3
  272. package/lib/src/model/query-surreal.d.ts +0 -44
  273. package/lib/src/model/query-surreal.js +0 -249
  274. package/lib/src/model/surreal-utils.d.ts +0 -42
  275. package/lib/src/model/surreal-utils.js +0 -128
  276. package/lib/src/perspectives/PerspectiveProxy.test.d.ts +0 -1
  277. package/lib/src/perspectives/PerspectiveProxy.test.js +0 -34
  278. package/lib/src/restClient.d.ts +0 -52
  279. package/lib/src/restClient.js +0 -227
@@ -0,0 +1,369 @@
1
+ /**
2
+ * `FlowInstance` — object-oriented wrapper for a running flow.
3
+ *
4
+ * Design authority: `docs/flow-interpretation-hints-design.md` §4.3.
5
+ *
6
+ * Wraps two on-graph objects:
7
+ *
8
+ * - `FlowInstanceRecord` — the raw @Model row minted by
9
+ * {@link FlowInstance.start}; carries `flow`, `subject`, `currentState`,
10
+ * and Ad4mModel's synthesised `createdAt`.
11
+ * - `SHACLFlow` — the flow definition the record is bound to. Held so
12
+ * `availableTransitions` / `currentState` object lookups don't require
13
+ * a per-call round-trip.
14
+ *
15
+ * Construct via the class factories — {@link FlowInstance.start} for the
16
+ * mint path and {@link FlowInstance.findAll} for the read path — rather
17
+ * than the `wrap` escape hatch, which exists for tests that already own
18
+ * a matched (record, shape) pair.
19
+ *
20
+ * # What's here vs. what's coming in §7 / slice 10.6
21
+ *
22
+ * Read surface (implemented today, PR #929):
23
+ * - `uri`, `subject`, `flowName`, `currentStateName`, `startedAtMillis`
24
+ * accessors.
25
+ * - `currentState` — resolves the `FlowState` on the shape whose name
26
+ * matches `record.currentState`.
27
+ * - `availableTransitions` — filters `shape.transitions` by `fromState`.
28
+ * - `proposals()` — queries `FlowTransitionProposal` records that
29
+ * target this instance's URI.
30
+ *
31
+ * Mutations + subscriptions (`proposeTransition`, `accept`, `reject`,
32
+ * `fireAction`, `onStateChange`, `onProposalAdded`, `onProposalResolved`)
33
+ * land with the consensus-engine slice (§4.3 / slice 10.6). Rather than
34
+ * ship them as `throw new Error("not yet")` stubs — which is a type-level
35
+ * lie the caller only discovers at runtime — they are simply absent from
36
+ * this class until the engine is wired.
37
+ */
38
+
39
+ import { PerspectiveProxy } from "./PerspectiveProxy";
40
+ import { Ad4mModel } from "../model/Ad4mModel";
41
+ import { FlowInstanceRecord, FlowTransitionProposal } from "./FlowModels";
42
+ import { SHACLFlow, FlowState, FlowTransition } from "../shacl/SHACLFlow";
43
+
44
+ /**
45
+ * Extract the flow's human-readable name from its canonical URI.
46
+ *
47
+ * `SHACLFlow.flowUri` is `${namespace}${name}Flow` (e.g.
48
+ * `coasys://DeliveryFlow`). This function strips the `Flow` suffix and
49
+ * everything before the last URI-segment separator (`/` or `#`), leaving
50
+ * the bare name. Returns `undefined` for URIs that don't match the
51
+ * `…{name}Flow` shape — a stale FlowInstanceRecord whose flow URI came
52
+ * from an older writer, for instance.
53
+ */
54
+ function flowNameFromUri(flowUri: string): string | undefined {
55
+ if (!flowUri.endsWith("Flow")) return undefined;
56
+ const withoutSuffix = flowUri.slice(0, -"Flow".length);
57
+ const sepIdx = Math.max(
58
+ withoutSuffix.lastIndexOf("/"),
59
+ withoutSuffix.lastIndexOf("#"),
60
+ );
61
+ if (sepIdx < 0) return withoutSuffix || undefined;
62
+ const name = withoutSuffix.slice(sepIdx + 1);
63
+ return name || undefined;
64
+ }
65
+
66
+ export class FlowInstance {
67
+ /**
68
+ * Private constructor — construct via {@link FlowInstance.start} or
69
+ * {@link FlowInstance.findAll}. {@link FlowInstance.wrap} is the
70
+ * escape hatch for tests / direct construction cases.
71
+ */
72
+ private constructor(
73
+ private readonly perspective: PerspectiveProxy,
74
+ /** The flow definition this instance was minted against. */
75
+ public readonly shape: SHACLFlow,
76
+ /**
77
+ * The on-graph record — carries `flow`, `subject`, `currentState`
78
+ * and Ad4mModel's synthesised `createdAt`. Callers can reach into it
79
+ * for anything the wrapper doesn't expose directly, but should prefer
80
+ * the wrapper accessors so future refactors of the underlying shape
81
+ * don't ripple through their code.
82
+ */
83
+ public readonly record: FlowInstanceRecord,
84
+ ) {}
85
+
86
+ /**
87
+ * Escape hatch used by {@link FlowInstance.start} and
88
+ * {@link FlowInstance.findAll} — not meant for general use, but public
89
+ * so tests that already own a `FlowInstanceRecord + SHACLFlow` pair
90
+ * (e.g. from pre-populated fixtures) can build a wrapper without
91
+ * going through the mint path.
92
+ */
93
+ static wrap(
94
+ perspective: PerspectiveProxy,
95
+ shape: SHACLFlow,
96
+ record: FlowInstanceRecord,
97
+ ): FlowInstance {
98
+ return new FlowInstance(perspective, shape, record);
99
+ }
100
+
101
+ /**
102
+ * Mint a new `FlowInstance` on the given perspective (design doc §4.3).
103
+ *
104
+ * Idempotently registers the hardwired `FlowInstanceRecord` +
105
+ * `FlowTransitionProposal` @Model classes on first call — the on-graph
106
+ * shape matches the Rust-side hardwired SDNA (parity-locked in
107
+ * `flow-instance.test.ts` / `flow-transition-proposal.test.ts`).
108
+ *
109
+ * The returned wrapper carries the parsed `SHACLFlow` alongside the
110
+ * on-graph record, so `currentState` / `availableTransitions` /
111
+ * `proposals` accessors work without further round-trips. The consensus
112
+ * engine (slice 10.6) will read the record's `currentState` when it
113
+ * fires transitions.
114
+ *
115
+ * @param perspective - The perspective the flow instance lives on
116
+ * @param flowName - Name of a `SHACLFlow` already registered on the perspective
117
+ * @param baseExpression - URI of the subject expression the flow runs on
118
+ * @throws When the flow is unknown or has zero declared states (zero-state
119
+ * flows fire via the forthcoming atomic-action path, §6.3).
120
+ */
121
+ static async start(
122
+ perspective: PerspectiveProxy,
123
+ flowName: string,
124
+ baseExpression: string,
125
+ ): Promise<FlowInstance> {
126
+ const flow = await perspective.getFlow(flowName);
127
+ if (!flow) throw `Flow "${flowName}" not found`;
128
+ if (flow.states.length === 0) {
129
+ throw `Flow "${flowName}" has no states — FlowInstance.start is for stateful flows only; zero-state flows fire via the forthcoming atomic-action path (§6.3)`;
130
+ }
131
+ // Register the hardwired runtime classes if this is the first flow
132
+ // instance on the perspective. registerAll is a single batched RPC and
133
+ // no-ops when both classes are already present.
134
+ await Ad4mModel.registerAll(perspective, [FlowInstanceRecord, FlowTransitionProposal]);
135
+ // Property keys must be the FlowInstanceRecord @Model field names —
136
+ // `subject` (not `baseExpression`, which collides with Ad4mModel's
137
+ // synthetic hydration field and would be silently shadowed on read).
138
+ // No explicit start-time field: Ad4mModel synthesises `createdAt` on
139
+ // hydration from the earliest link timestamp on the instance's URI.
140
+ // Convention: `SHACLFlow.states` is stored sorted ascending by `value`
141
+ // (enforced by `fromLinks`), so `states[0]` is the initial state. A
142
+ // flow author who wants a specific state as the entry point must give
143
+ // it the lowest `value` in the set.
144
+ //
145
+ // Store the flow's URI, not the bare name — see FlowInstanceRecord's
146
+ // docstring for the collision-across-modules argument (James PR #929 R5).
147
+ const record = await FlowInstanceRecord.create(perspective, {
148
+ flowUri: flow.flowUri,
149
+ subject: baseExpression,
150
+ currentState: flow.states[0].name,
151
+ });
152
+ return FlowInstance.wrap(perspective, flow, record);
153
+ }
154
+
155
+ /**
156
+ * Return every live `FlowInstance` on the perspective (design doc §4.3).
157
+ *
158
+ * Read-only path: never registers classes. On a perspective that has
159
+ * never minted a flow instance, the `FlowInstanceRecord` SHACL shape
160
+ * isn't installed yet — this returns `[]` in that case rather than
161
+ * mutating the perspective. Registration is the responsibility of
162
+ * {@link FlowInstance.start}, which is the write path. (Registering on
163
+ * a read would sync a write to every peer in the neighbourhood —
164
+ * exactly what a query must not do — James PR #929 R7.)
165
+ *
166
+ * Filter surface (all optional, all combinable):
167
+ * - **`flowName`** — narrows by flow-name discriminator (e.g. "Delivery")
168
+ * - **`subject`** — narrows by base-expression URI, i.e. "give me every
169
+ * flow running on THIS expression"
170
+ *
171
+ * Both filters translate to a single SHACL `where`-filter round-trip
172
+ * (no client-side filtering); combining them AND-joins server-side.
173
+ * The string-arg shape (`FlowInstance.findAll(p, "Delivery")`) is
174
+ * shorthand for `{ flowName: "Delivery" }`.
175
+ *
176
+ * Records whose `flow` value has no matching `SHACLFlow` on the
177
+ * perspective (e.g. the flow was unregistered) are silently skipped —
178
+ * the wrapper can't answer `currentState` / `availableTransitions`
179
+ * without the shape, and callers routinely iterate the returned array
180
+ * without null-checks.
181
+ *
182
+ * @example
183
+ * ```typescript
184
+ * const all = await FlowInstance.findAll(perspective);
185
+ * const deliveries = await FlowInstance.findAll(perspective, "Delivery");
186
+ * const onThisTask = await FlowInstance.findAll(perspective, {
187
+ * subject: "ad4m://task/1",
188
+ * });
189
+ * const deliveriesOnTask = await FlowInstance.findAll(perspective, {
190
+ * flowName: "Delivery",
191
+ * subject: "ad4m://task/1",
192
+ * });
193
+ * ```
194
+ */
195
+ static async findAll(
196
+ perspective: PerspectiveProxy,
197
+ filter?: string | { flowName?: string; subject?: string },
198
+ ): Promise<FlowInstance[]> {
199
+ const { flowName, subject } =
200
+ typeof filter === "string"
201
+ ? { flowName: filter, subject: undefined }
202
+ : { flowName: filter?.flowName, subject: filter?.subject };
203
+
204
+ // Filter surface accepts a flow *name* for ergonomics — resolve it to
205
+ // the canonical URI before the SHACL query, since the record stores
206
+ // the URI (James PR #929 R5). Unknown name → no matches (short-circuit
207
+ // with an empty result rather than issuing a bare-name query that
208
+ // would silently return nothing anyway).
209
+ const where: Record<string, string> = {};
210
+ if (flowName !== undefined) {
211
+ const flow = await perspective.getFlow(flowName);
212
+ if (!flow) return [];
213
+ where.flowUri = flow.flowUri;
214
+ }
215
+ if (subject !== undefined) where.subject = subject;
216
+
217
+ // "Shape not found" on a perspective that has never minted a flow
218
+ // instance is a no-op read, not an error — the executor throws for a
219
+ // missing SHACL shape and there is no side-effect-free way to ask
220
+ // "does this class exist". Return `[]` on that specific case; rethrow
221
+ // anything else so real infra failures don't get swallowed.
222
+ //
223
+ // Regex covers every message the executor + client stack raises for
224
+ // an unregistered class:
225
+ // "No SHACL shape stored for class 'FlowInstance'." (executor RPC)
226
+ // "Shape not found" (older Rust path)
227
+ // "class not registered" / "not registered" (TS Ad4mModel guard)
228
+ let records: FlowInstanceRecord[];
229
+ try {
230
+ records =
231
+ Object.keys(where).length > 0
232
+ ? await FlowInstanceRecord.findAll(perspective, { where })
233
+ : await FlowInstanceRecord.findAll(perspective);
234
+ } catch (e) {
235
+ const msg = e instanceof Error ? e.message : String(e);
236
+ if (
237
+ /no shacl shape stored|shape not found|class not registered|not registered/i.test(
238
+ msg,
239
+ )
240
+ ) {
241
+ return [];
242
+ }
243
+ throw e;
244
+ }
245
+
246
+ // Pair each record with its parsed SHACLFlow. Cache lookups by
247
+ // flow-URI so `getFlow` fires at most once per distinct flow —
248
+ // matters when a perspective has hundreds of instances against
249
+ // one shape.
250
+ //
251
+ // The URI stored on the record is `${namespace}${name}Flow`
252
+ // (see SHACLFlow.flowUri) — recovering the name for `getFlow`
253
+ // means stripping the namespace + the `Flow` suffix. Records that
254
+ // don't parse this way are treated as stale and skipped.
255
+ const shapesByUri = new Map<string, SHACLFlow>();
256
+ const wrappers: FlowInstance[] = [];
257
+ for (const record of records) {
258
+ let shape = shapesByUri.get(record.flowUri);
259
+ if (!shape) {
260
+ const name = flowNameFromUri(record.flowUri);
261
+ if (!name) continue;
262
+ const loaded = await perspective.getFlow(name);
263
+ if (!loaded || loaded.flowUri !== record.flowUri) {
264
+ // Stale record — the flow it references was unregistered,
265
+ // or a different flow now owns that name.
266
+ continue;
267
+ }
268
+ shape = loaded;
269
+ shapesByUri.set(record.flowUri, shape);
270
+ }
271
+ wrappers.push(FlowInstance.wrap(perspective, shape, record));
272
+ }
273
+ return wrappers;
274
+ }
275
+
276
+ // ── Read accessors ────────────────────────────────────────────────────
277
+
278
+ /**
279
+ * URI of the on-graph `FlowInstance` node — `ad4m://flow/instance/{id}`.
280
+ *
281
+ * Sourced from `Ad4mModel`'s synthesised `baseExpression`, which for
282
+ * `FlowInstanceRecord` *is* the instance's own URI (the value
283
+ * `FlowTransitionProposal.flowInstance` references).
284
+ */
285
+ get uri(): string {
286
+ return (this.record as any).baseExpression as string;
287
+ }
288
+
289
+ /**
290
+ * URI of the base expression this flow runs on — matches the
291
+ * `SHACLFlow.inputTypes` that {@link PerspectiveProxy.availableFlows}
292
+ * would greenlight for this expression.
293
+ */
294
+ get subject(): string {
295
+ return this.record.subject;
296
+ }
297
+
298
+ /**
299
+ * Human-readable flow name — the display label. Sourced from the paired
300
+ * `SHACLFlow.name`, not from the record (which stores the URI as its
301
+ * canonical identity — see FlowInstanceRecord's docstring for why).
302
+ */
303
+ get flowName(): string {
304
+ return this.shape.name;
305
+ }
306
+
307
+ /** Flow URI — the on-graph discriminator, collision-free across modules. */
308
+ get flowUri(): string {
309
+ return this.record.flowUri;
310
+ }
311
+
312
+ /** Name of the state this instance is currently in. */
313
+ get currentStateName(): string {
314
+ return this.record.currentState;
315
+ }
316
+
317
+ /**
318
+ * ISO-8601 (epoch millis after Ad4mModel hydration) start time.
319
+ * Returns `undefined` when hydration didn't produce a `createdAt`
320
+ * — rare, but not fatal.
321
+ */
322
+ get startedAtMillis(): number | undefined {
323
+ const v = (this.record as any).createdAt;
324
+ return typeof v === "number" ? v : undefined;
325
+ }
326
+
327
+ /**
328
+ * The `FlowState` object on the shape matching `currentStateName`.
329
+ * Throws when the record's state name is not declared on the flow —
330
+ * that's a "stale FlowInstance whose flow was edited under it" bug,
331
+ * not a silent-fail case.
332
+ */
333
+ get currentState(): FlowState {
334
+ const s = this.shape.states.find((x) => x.name === this.currentStateName);
335
+ if (!s) {
336
+ throw new Error(
337
+ `FlowInstance ${this.uri}: currentState "${this.currentStateName}" is not declared on flow "${this.flowName}". ` +
338
+ `Known states: ${this.shape.states.map((x) => x.name).join(", ") || "(none)"}`,
339
+ );
340
+ }
341
+ return s;
342
+ }
343
+
344
+ /**
345
+ * Every `FlowTransition` on the shape whose `fromState` matches
346
+ * `currentStateName`. Order preserved from the shape.
347
+ *
348
+ * Empty when the current state is terminal (no outgoing transitions).
349
+ */
350
+ get availableTransitions(): FlowTransition[] {
351
+ return this.shape.transitions.filter((t) => t.fromState === this.currentStateName);
352
+ }
353
+
354
+ /**
355
+ * All `FlowTransitionProposal` records targeting this instance's URI.
356
+ *
357
+ * Uses the SDNA identity discriminator (`ad4m://flow/instance` predicate)
358
+ * for the where-filter — one SPARQL round-trip, no client-side
359
+ * filtering.
360
+ *
361
+ * Note: the consensus engine (slice 10.6) will start writing these; today
362
+ * they only exist when a client wrote one directly (or a test seeded one).
363
+ */
364
+ async proposals(): Promise<FlowTransitionProposal[]> {
365
+ return FlowTransitionProposal.findAll(this.perspective, {
366
+ where: { flowInstance: this.uri },
367
+ });
368
+ }
369
+ }
@@ -0,0 +1,157 @@
1
+ /**
2
+ * TypeScript @Model classes mirroring the Rust-side hardwired SDNA for the
3
+ * flow runtime. Apps can import these to query the perspective graph for
4
+ * pending flow-transition proposals without knowing the raw link predicates.
5
+ *
6
+ * Registration:
7
+ * await FlowTransitionProposal.register(perspective);
8
+ *
9
+ * Querying:
10
+ * const pending = await FlowTransitionProposal.findAll(perspective);
11
+ *
12
+ * Slice 5/7 of the flow-implementation arc (design doc §4.2). The Rust
13
+ * engine does not yet mint these — that lands with the consensus firing
14
+ * work in a later PR arc. This class defines the on-graph shape so:
15
+ * - clients can build UI over proposals immediately (Deliberation demo);
16
+ * - the SDNA-parity test locks TS/Rust drift the moment the JSON exists
17
+ * (`rust-executor/src/perspectives/hardwired_sdna/flow_transition_proposal.json`);
18
+ * - the engine can start writing into this shape when consensus lands
19
+ * without touching the client-side surface.
20
+ */
21
+
22
+ import { Ad4mModel } from "../model/Ad4mModel";
23
+ import { HasMany, Optional, Property } from "../model/decorators";
24
+ import { Model } from "../model/decorators";
25
+
26
+ // ── FlowTransitionProposal ──────────────────────────────────────────────────
27
+ // Mirrors the Rust hardwired `flow_transition_proposal.json` SDNA (once wired
28
+ // on the engine side; see file header). One node per proposed transition; the
29
+ // URI is `ad4m://flow/proposal/<uuid>` when the engine mints it, but the class
30
+ // carries no URI-format constraint — apps constructing proposals directly may
31
+ // choose any URI they can defend as unique.
32
+
33
+ @Model({ name: "FlowTransitionProposal" })
34
+ export class FlowTransitionProposal extends Ad4mModel {
35
+ /**
36
+ * URI of the running `FlowInstance` this proposal targets. First-declared
37
+ * property so `buildSHACL` derives the shape namespace from its `through`
38
+ * prefix — `ad4m://flow/instance` → `ad4m://` → target_class
39
+ * `ad4m://FlowTransitionProposal`. Also the identity predicate: the
40
+ * discriminator `findAll` uses to isolate proposal nodes.
41
+ */
42
+ @Property({ through: "ad4m://flow/instance", required: true, identity: true })
43
+ flowInstance: string = "";
44
+
45
+ /** Name of the state the flow instance is currently in. */
46
+ @Property({ through: "ad4m://flow/from_state", required: true })
47
+ fromState: string = "";
48
+
49
+ /** Name of the state the proposal wants to transition to. */
50
+ @Property({ through: "ad4m://flow/to_state", required: true })
51
+ toState: string = "";
52
+
53
+ /** DID of the agent that issued the proposal. */
54
+ @Property({ through: "ad4m://flow/proposer", required: true })
55
+ proposer: string = "";
56
+
57
+ /**
58
+ * URIs of instances offered as evidence that `toState.requires` is now
59
+ * satisfied. Multiple links, one per cited instance; ordering is not
60
+ * significant.
61
+ */
62
+ @HasMany({ through: "ad4m://flow/evidence", datatype: "xsd:string" })
63
+ evidence: string[] = [];
64
+
65
+ /**
66
+ * JSON blob: `{ [uri]: sha256_hex }` — one entry per evidence URI, hashing
67
+ * the canonicalized graph-visible properties (sorted by property URI,
68
+ * multi-values sorted lexicographically) at proposal time. Downstream
69
+ * verifiers use it to detect edits to cited instances after the proposal
70
+ * was written. Stored as an opaque string because SHACL properties carry
71
+ * no rich-object type; the engine parses it back into a map.
72
+ */
73
+ @Property({ through: "ad4m://flow/evidence_hashes", required: true })
74
+ evidenceHashes: string = "";
75
+
76
+ /**
77
+ * URI of the `InterpretationRun` node that produced this proposal, when
78
+ * the proposal came from an LLM extraction pass. Absent when a human
79
+ * clicked "propose" in the UI or an external tool wrote the proposal
80
+ * directly.
81
+ */
82
+ @Optional({ through: "ad4m://flow/run_uri" })
83
+ runUri?: string;
84
+
85
+ /**
86
+ * Free-form human-readable justification. Optional — the LLM includes it
87
+ * when it wants to explain *why* the cited evidence satisfies the state's
88
+ * `requires`; the UI shows it to reviewers.
89
+ */
90
+ @Optional({ through: "ad4m://flow/rationale" })
91
+ rationale?: string;
92
+
93
+ // "When was this proposal written?" is answered by `Ad4mModel`'s built-in
94
+ // `createdAt`, synthesized on hydration from the earliest link timestamp
95
+ // of the proposal's own links (all written together during create_subject).
96
+ }
97
+
98
+ // ── FlowInstanceRecord ──────────────────────────────────────────────────────
99
+ // On-graph node minted by the engine when a flow is started on a specific
100
+ // base expression. Its URI is the value that `FlowTransitionProposal.flowInstance`
101
+ // references. Mirrors `rust-executor/src/perspectives/hardwired_sdna/flow_instance.json`.
102
+ //
103
+ // Design authority: `docs/flow-interpretation-hints-design.md` §4.3.
104
+ //
105
+ // This class is the raw on-graph record (@Model over the SDNA shape). The
106
+ // object-oriented handle callers work with is `FlowInstance` in
107
+ // `./FlowInstance.ts` — it wraps `FlowInstanceRecord + SHACLFlow definition`
108
+ // and exposes `currentState`, `availableTransitions`, `proposals`, etc.
109
+ // Renamed from `FlowInstance` → `FlowInstanceRecord` on 2026-08-27 per
110
+ // Nico's PR #929 review R2 (the `FlowInstance` name is reserved for the
111
+ // wrapper the design has always specified in §4.3).
112
+ //
113
+ // The SDNA name (`@Model({ name: "FlowInstance" })`) stays unchanged — that
114
+ // keys the on-graph class, not the TS export. Renaming the TS class only
115
+ // changes the JS import surface; the graph shape is identical, and Rust
116
+ // hardwired_sdna/flow_instance.json + all the parity tests still lock down
117
+ // the same `ad4m://FlowInstance` target class.
118
+
119
+ @Model({ name: "FlowInstance" })
120
+ export class FlowInstanceRecord extends Ad4mModel {
121
+ /**
122
+ * URI of the `SHACLFlow` this instance runs — the flow's canonical
123
+ * identity, collision-free across social-DNA modules from different
124
+ * communities (bare names WILL collide once shared flows are adopted
125
+ * cross-team — James PR #929 R5). Sourced from `SHACLFlow.flowUri`
126
+ * (which is `${namespace}${name}Flow`, e.g. `coasys://DeliveryFlow`).
127
+ *
128
+ * First-declared property so `buildSHACL` derives the shape namespace
129
+ * from its `through` prefix — `ad4m://flow/flow_uri` → `ad4m://` →
130
+ * target_class `ad4m://FlowInstance`. Also the discriminator predicate
131
+ * `findAll` uses to isolate instance nodes.
132
+ *
133
+ * The human-readable flow name is still available on the paired
134
+ * `SHACLFlow` (via `perspective.getFlow(record.flowUri.split('/').pop()!.replace(/Flow$/,''))`,
135
+ * or more commonly via the `FlowInstance` wrapper's `flowName` getter).
136
+ */
137
+ @Property({ through: "ad4m://flow/flow_uri", required: true, identity: true })
138
+ flowUri: string = "";
139
+
140
+ /**
141
+ * URI of the subject expression this flow runs on.
142
+ * Named `subject` (not `baseExpression`) because `Ad4mModel` reserves
143
+ * `baseExpression` as a synthetic hydration field always set to the
144
+ * instance's own URI — a subclass property with that name is shadowed
145
+ * on read.
146
+ */
147
+ @Property({ through: "ad4m://flow/base", required: true })
148
+ subject: string = "";
149
+
150
+ /** Name of the state the flow is currently in. */
151
+ @Property({ through: "ad4m://flow/current_state", required: true })
152
+ currentState: string = "";
153
+
154
+ // "When was this flow started?" is answered by `Ad4mModel`'s built-in
155
+ // `createdAt`, synthesized on hydration from the earliest link timestamp
156
+ // of the instance's own links (all written together during `mint_flow_instance`).
157
+ }