@zlink-systems/framework 0.30.0 → 0.30.2

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 (456) hide show
  1. package/LICENSE +150 -68
  2. package/dist/.tsbuildinfo +1 -1
  3. package/dist/contracts/Actors/ZLinkActorClient.d.ts +2 -1
  4. package/dist/contracts/Channels/Calls.d.ts +2 -1
  5. package/dist/contracts/Common/ZLinkEncodedPayload.d.ts +6 -0
  6. package/dist/contracts/Common/ZLinkEncodedPayload.js +18 -3
  7. package/dist/contracts/Common/ZLinkMessageMetadata.js +11 -5
  8. package/dist/contracts/Common/encoded-payload-storage.d.ts +1 -1
  9. package/dist/contracts/Common/encoded-payload-storage.js +5 -2
  10. package/dist/contracts/Configuration/Builders.d.ts +1 -1
  11. package/dist/contracts/Configuration/RegistrationBuilderPolicy.d.ts +2 -1
  12. package/dist/contracts/Configuration/RegistrationBuilderPolicy.js +5 -1
  13. package/dist/contracts/Configuration/RegistrationBuilders.js +2 -2
  14. package/dist/contracts/Configuration/RegistrationTypes.d.ts +1 -0
  15. package/dist/contracts/Dispatch/ZLinkDispatchOptions.d.ts +2 -2
  16. package/dist/contracts/Handlers/Attributes.d.ts +5 -13
  17. package/dist/contracts/Handlers/Attributes.js +7 -11
  18. package/dist/contracts/Handlers/JsonContract.d.ts +2 -46
  19. package/dist/contracts/Handlers/JsonContract.js +4 -153
  20. package/dist/contracts/Locations/Rows.d.ts +2 -0
  21. package/dist/contracts/Spots/Contracts.d.ts +6 -1
  22. package/dist/contracts/Spots/ZLinkSpot.d.ts +3 -3
  23. package/dist/nest-integration.d.ts +1 -1
  24. package/dist/nest-integration.js +2 -1
  25. package/dist/runtime/abort.d.ts +5 -0
  26. package/dist/runtime/abort.js +10 -0
  27. package/dist/runtime/actors/actor-authority-publication.js +3 -2
  28. package/dist/runtime/actors/actor-client.d.ts +1 -2
  29. package/dist/runtime/actors/actor-client.js +52 -57
  30. package/dist/runtime/actors/actor-context.d.ts +2 -0
  31. package/dist/runtime/actors/actor-context.js +46 -9
  32. package/dist/runtime/actors/actor-handoff.d.ts +8 -4
  33. package/dist/runtime/actors/actor-handoff.js +219 -92
  34. package/dist/runtime/actors/actor-join-relocation.d.ts +3 -0
  35. package/dist/runtime/actors/actor-local-native-join.js +13 -12
  36. package/dist/runtime/actors/actor-mailbox.js +27 -1
  37. package/dist/runtime/actors/actor-remote-joiner.js +3 -1
  38. package/dist/runtime/actors/actor-remote-wire.js +15 -0
  39. package/dist/runtime/actors/actor-reply.d.ts +4 -0
  40. package/dist/runtime/actors/actor-reply.js +29 -0
  41. package/dist/runtime/actors/actor-routed-join-transport.d.ts +6 -2
  42. package/dist/runtime/actors/actor-routed-json-request.d.ts +2 -0
  43. package/dist/runtime/actors/actor-runtime-contracts.d.ts +2 -0
  44. package/dist/runtime/actors/actor-runtime-state.js +2 -20
  45. package/dist/runtime/actors/actor-source-transfer.d.ts +1 -17
  46. package/dist/runtime/actors/index.d.ts +8 -3
  47. package/dist/runtime/actors/index.js +55 -27
  48. package/dist/runtime/actors/session-disconnected-wire.d.ts +8 -0
  49. package/dist/runtime/actors/session-disconnected-wire.js +48 -0
  50. package/dist/runtime/actors/spot-actor-dispatch.js +2 -2
  51. package/dist/runtime/admission.d.ts +9 -1
  52. package/dist/runtime/admission.js +56 -23
  53. package/dist/runtime/application-jobs/application-ingress-record-owner.d.ts +6 -8
  54. package/dist/runtime/application-jobs/application-ingress-record-owner.js +39 -22
  55. package/dist/runtime/application-jobs/application-job-queue-scope.d.ts +15 -1
  56. package/dist/runtime/application-jobs/application-job-queue-scope.js +61 -20
  57. package/dist/runtime/application-jobs/contracts.d.ts +12 -1
  58. package/dist/runtime/backend/contracts/index.d.ts +37 -12
  59. package/dist/runtime/backend/mesh-actor-session-node-adapter.js +2 -2
  60. package/dist/runtime/backend/mesh-completion-table.d.ts +9 -1
  61. package/dist/runtime/backend/mesh-completion-table.js +154 -49
  62. package/dist/runtime/backend/mesh-dispatch-pump.d.ts +28 -1
  63. package/dist/runtime/backend/mesh-dispatch-pump.js +208 -101
  64. package/dist/runtime/backend/node/node-backend-adapter-support.d.ts +2 -2
  65. package/dist/runtime/backend/node/node-backend-adapter-support.js +16 -9
  66. package/dist/runtime/backend/node/node-dealer-channel-client-transport.d.ts +1 -1
  67. package/dist/runtime/backend/node/node-dealer-channel-client-transport.js +5 -2
  68. package/dist/runtime/backend/node/node-raw-binding-port.js +124 -72
  69. package/dist/runtime/backend/node/node-raw-mesh-backend.d.ts +46 -13
  70. package/dist/runtime/backend/node/node-raw-mesh-backend.js +181 -164
  71. package/dist/runtime/backend/node/node-socket-backend-adapter.d.ts +1 -1
  72. package/dist/runtime/backend/node/node-socket-backend-adapter.js +11 -11
  73. package/dist/runtime/backend/raw-binding-port.d.ts +2 -2
  74. package/dist/runtime/backend/runtime-message.d.ts +1 -0
  75. package/dist/runtime/backend/runtime-message.js +5 -0
  76. package/dist/runtime/backend/runtime-values.d.ts +1 -0
  77. package/dist/runtime/backend/runtime-values.js +2 -1
  78. package/dist/runtime/channels/channel-clients.d.ts +2 -0
  79. package/dist/runtime/channels/channel-clients.js +75 -23
  80. package/dist/runtime/channels/channel-dispatch-pipeline.d.ts +25 -5
  81. package/dist/runtime/channels/channel-dispatch-pipeline.js +111 -56
  82. package/dist/runtime/channels/channel-dispatch-services.d.ts +1 -0
  83. package/dist/runtime/channels/channel-dispatch-services.js +45 -0
  84. package/dist/runtime/channels/channel-dispatchers.d.ts +4 -3
  85. package/dist/runtime/channels/channel-dispatchers.js +62 -54
  86. package/dist/runtime/channels/channel-envelope-inspection.d.ts +1 -2
  87. package/dist/runtime/channels/channel-envelope-inspection.js +1 -16
  88. package/dist/runtime/channels/channel-envelope.d.ts +2 -0
  89. package/dist/runtime/channels/channel-envelope.js +146 -28
  90. package/dist/runtime/channels/channel-outbound-operations.d.ts +2 -2
  91. package/dist/runtime/channels/channel-outbound-operations.js +8 -8
  92. package/dist/runtime/channels/channel-runtime-lifecycle.d.ts +4 -3
  93. package/dist/runtime/channels/channel-runtime-lifecycle.js +19 -24
  94. package/dist/runtime/channels/channel-runtime-manager.d.ts +9 -8
  95. package/dist/runtime/channels/channel-runtime-manager.js +16 -16
  96. package/dist/runtime/channels/channel-transports.d.ts +18 -16
  97. package/dist/runtime/channels/channel-transports.js +77 -58
  98. package/dist/runtime/channels/client-server-location-runtime.js +5 -4
  99. package/dist/runtime/channels/dispatch-error-reporter.d.ts +2 -1
  100. package/dist/runtime/channels/dispatch-error-reporter.js +2 -1
  101. package/dist/runtime/channels/fanout-location-runtime.d.ts +1 -1
  102. package/dist/runtime/channels/fanout-location-runtime.js +3 -3
  103. package/dist/runtime/channels/local-spot-route-transport.d.ts +1 -1
  104. package/dist/runtime/channels/local-spot-route-transport.js +2 -1
  105. package/dist/runtime/channels/router-socket-spot-route-transport.d.ts +2 -2
  106. package/dist/runtime/channels/router-socket-spot-route-transport.js +4 -4
  107. package/dist/runtime/channels/source-spot-router.d.ts +2 -2
  108. package/dist/runtime/channels/source-spot-router.js +4 -4
  109. package/dist/runtime/channels/spot-node-route-transport.d.ts +2 -2
  110. package/dist/runtime/channels/spot-node-route-transport.js +4 -4
  111. package/dist/runtime/channels/spot-route-bridge-transport.d.ts +2 -2
  112. package/dist/runtime/channels/spot-route-bridge-transport.js +4 -4
  113. package/dist/runtime/channels/spot-route-dispatch-strategy.d.ts +5 -4
  114. package/dist/runtime/channels/spot-route-dispatch-strategy.js +18 -18
  115. package/dist/runtime/diagnostics/flow-context.js +19 -7
  116. package/dist/runtime/diagnostics/index.js +4 -9
  117. package/dist/runtime/diagnostics/message-flow.d.ts +8 -0
  118. package/dist/runtime/diagnostics/message-flow.js +35 -5
  119. package/dist/runtime/diagnostics/runtime-metrics.d.ts +2 -2
  120. package/dist/runtime/diagnostics/runtime-metrics.js +10 -8
  121. package/dist/runtime/execution/index.d.ts +4 -2
  122. package/dist/runtime/execution/index.js +33 -10
  123. package/dist/runtime/execution/serial-execution-queue.d.ts +5 -0
  124. package/dist/runtime/execution/serial-execution-queue.js +22 -2
  125. package/dist/runtime/execution/state-lane.d.ts +2 -0
  126. package/dist/runtime/execution/state-lane.js +5 -0
  127. package/dist/runtime/foundation/actor-join-recovery-codec.js +2 -2
  128. package/dist/runtime/foundation/durable-service-request.d.ts +14 -0
  129. package/dist/runtime/foundation/durable-service-request.js +98 -0
  130. package/dist/runtime/foundation/operation-identity.d.ts +1 -0
  131. package/dist/runtime/foundation/operation-identity.js +5 -1
  132. package/dist/runtime/foundation/operation-registry.d.ts +25 -1
  133. package/dist/runtime/foundation/operation-registry.js +86 -18
  134. package/dist/runtime/foundation/raw-service-mesh-runtime.d.ts +61 -8
  135. package/dist/runtime/foundation/raw-service-mesh-runtime.js +486 -266
  136. package/dist/runtime/foundation/service-liveness-registry.d.ts +12 -1
  137. package/dist/runtime/foundation/service-liveness-registry.js +38 -0
  138. package/dist/runtime/foundation/service-mailbox.d.ts +7 -0
  139. package/dist/runtime/foundation/service-maintenance-runtime.js +1 -6
  140. package/dist/runtime/foundation/service-relocation-object-owner.d.ts +3 -3
  141. package/dist/runtime/foundation/service-relocation-object-owner.js +48 -34
  142. package/dist/runtime/foundation/service-relocation-runtime.d.ts +2 -0
  143. package/dist/runtime/foundation/service-relocation-runtime.js +20 -4
  144. package/dist/runtime/foundation/service-runtime-contracts.d.ts +10 -1
  145. package/dist/runtime/foundation/service-session-binding-ingress-port.d.ts +2 -1
  146. package/dist/runtime/foundation/service-stateful-registry.d.ts +5 -4
  147. package/dist/runtime/foundation/service-stateful-registry.js +24 -26
  148. package/dist/runtime/foundation/service-stateful-runtime.d.ts +75 -9
  149. package/dist/runtime/foundation/service-stateful-runtime.js +486 -218
  150. package/dist/runtime/foundation/service-stateful-wire-codec.d.ts +14 -6
  151. package/dist/runtime/foundation/service-stateful-wire-codec.js +48 -27
  152. package/dist/runtime/foundation/service-topology-registry.d.ts +12 -2
  153. package/dist/runtime/foundation/service-topology-registry.js +61 -27
  154. package/dist/runtime/foundation/service-wire-binary-primitives.d.ts +6 -1
  155. package/dist/runtime/foundation/service-wire-binary-primitives.js +27 -11
  156. package/dist/runtime/foundation/service-wire-constants.generated.d.ts +1 -0
  157. package/dist/runtime/foundation/service-wire-constants.generated.js +1 -0
  158. package/dist/runtime/foundation/service-wire-m6a-codec.d.ts +13 -3
  159. package/dist/runtime/foundation/service-wire-m6a-codec.js +189 -78
  160. package/dist/runtime/handlers/handler-instance-scope.d.ts +8 -0
  161. package/dist/runtime/handlers/handler-instance-scope.js +54 -11
  162. package/dist/runtime/host/actor-packet-relay.d.ts +6 -4
  163. package/dist/runtime/host/actor-packet-relay.js +59 -72
  164. package/dist/runtime/host/actor-placement-coordinator.js +25 -8
  165. package/dist/runtime/host/actor-runtime-options-factory.js +2 -2
  166. package/dist/runtime/host/actor-transfer-runtime.d.ts +7 -31
  167. package/dist/runtime/host/actor-transfer-runtime.js +85 -331
  168. package/dist/runtime/host/application-job-queue.d.ts +40 -7
  169. package/dist/runtime/host/application-job-queue.js +137 -42
  170. package/dist/runtime/host/entry-actor-runtime.d.ts +1 -0
  171. package/dist/runtime/host/entry-actor-runtime.js +3 -0
  172. package/dist/runtime/host/index.d.ts +12 -2
  173. package/dist/runtime/host/index.js +300 -202
  174. package/dist/runtime/host/location-runtime-owner.js +2 -6
  175. package/dist/runtime/host/mesh-router-resolver.d.ts +4 -0
  176. package/dist/runtime/host/mesh-router-resolver.js +14 -6
  177. package/dist/runtime/host/remote-bound-session-relay.d.ts +2 -4
  178. package/dist/runtime/host/remote-bound-session-relay.js +73 -208
  179. package/dist/runtime/host/route-mesh-runtime.d.ts +2 -2
  180. package/dist/runtime/host/route-mesh-runtime.js +6 -4
  181. package/dist/runtime/host/runtime-shutdown.d.ts +1 -0
  182. package/dist/runtime/host/runtime-shutdown.js +3 -2
  183. package/dist/runtime/host/service-relocation-control.d.ts +1 -0
  184. package/dist/runtime/host/service-relocation-control.js +10 -4
  185. package/dist/runtime/host/service-relocation-host-runtime.d.ts +22 -14
  186. package/dist/runtime/host/service-relocation-host-runtime.js +754 -599
  187. package/dist/runtime/host/spot-address-transport.js +3 -2
  188. package/dist/runtime/host/spot-runtime-options-factory.js +16 -7
  189. package/dist/runtime/host/user-spot-creation-coordinator.js +33 -84
  190. package/dist/runtime/locations/actor-location-claims.d.ts +2 -2
  191. package/dist/runtime/locations/actor-location-claims.js +34 -19
  192. package/dist/runtime/locations/auto-connect-loop.d.ts +2 -0
  193. package/dist/runtime/locations/auto-connect-loop.js +22 -14
  194. package/dist/runtime/locations/auto-connect-reconciler.js +0 -6
  195. package/dist/runtime/locations/auto-connect-types.d.ts +0 -1
  196. package/dist/runtime/locations/descriptor-publication.d.ts +4 -0
  197. package/dist/runtime/locations/descriptor-publication.js +11 -0
  198. package/dist/runtime/locations/domain-store-contract.d.ts +1 -0
  199. package/dist/runtime/locations/in-memory-authority-store.d.ts +7 -1
  200. package/dist/runtime/locations/in-memory-authority-store.js +56 -19
  201. package/dist/runtime/locations/in-memory-location-store.js +1 -10
  202. package/dist/runtime/locations/index.d.ts +1 -0
  203. package/dist/runtime/locations/index.js +1 -0
  204. package/dist/runtime/locations/lease-tracker.js +1 -6
  205. package/dist/runtime/locations/lifecycle.d.ts +1 -2
  206. package/dist/runtime/locations/lifecycle.js +2 -5
  207. package/dist/runtime/locations/location-store-repository.d.ts +51 -1
  208. package/dist/runtime/locations/location-store-repository.js +363 -139
  209. package/dist/runtime/locations/relocation-blob.d.ts +18 -1
  210. package/dist/runtime/locations/relocation-blob.js +52 -1
  211. package/dist/runtime/locations/request-source-resolver.d.ts +37 -0
  212. package/dist/runtime/locations/request-source-resolver.js +80 -0
  213. package/dist/runtime/locations/resolvers.js +1 -1
  214. package/dist/runtime/locations/runtime.d.ts +7 -3
  215. package/dist/runtime/locations/runtime.js +61 -53
  216. package/dist/runtime/messaging/framework-json-v1.d.ts +4 -6
  217. package/dist/runtime/messaging/framework-json-v1.js +38 -294
  218. package/dist/runtime/messaging/packet-name.d.ts +1 -1
  219. package/dist/runtime/messaging/packet-name.js +8 -24
  220. package/dist/runtime/messaging/payload-codec.d.ts +7 -1
  221. package/dist/runtime/messaging/payload-codec.js +7 -6
  222. package/dist/runtime/messaging/request-result.d.ts +2 -0
  223. package/dist/runtime/messaging/request-result.js +57 -0
  224. package/dist/runtime/messaging/submission-result.d.ts +1 -0
  225. package/dist/runtime/messaging/submission-result.js +5 -2
  226. package/dist/runtime/protocol/service_wire_codec.generated.d.ts +34 -1
  227. package/dist/runtime/protocol/service_wire_codec.generated.js +229 -0
  228. package/dist/runtime/protocol/service_wire_pilot_codec.generated.d.ts +16 -1
  229. package/dist/runtime/protocol/service_wire_pilot_codec.generated.js +37 -8
  230. package/dist/runtime/spots/formal-remote-actor-admission-registry.d.ts +7 -2
  231. package/dist/runtime/spots/formal-remote-actor-admission-registry.js +36 -21
  232. package/dist/runtime/spots/index.d.ts +21 -11
  233. package/dist/runtime/spots/index.js +243 -178
  234. package/dist/runtime/spots/spot-activation-registry.d.ts +4 -2
  235. package/dist/runtime/spots/spot-activation-registry.js +21 -11
  236. package/dist/runtime/spots/spot-activation-state.d.ts +1 -1
  237. package/dist/runtime/spots/spot-activation-state.js +7 -7
  238. package/dist/runtime/spots/spot-activation.d.ts +7 -3
  239. package/dist/runtime/spots/spot-activation.js +140 -52
  240. package/dist/runtime/spots/spot-actor-admission-coordinator.d.ts +1 -0
  241. package/dist/runtime/spots/spot-actor-admission-coordinator.js +13 -22
  242. package/dist/runtime/spots/spot-actor-membership.d.ts +0 -1
  243. package/dist/runtime/spots/spot-actor-membership.js +0 -8
  244. package/dist/runtime/spots/spot-actor-packet-dispatch.js +33 -8
  245. package/dist/runtime/spots/spot-actor-packet-drain.js +5 -33
  246. package/dist/runtime/spots/spot-entry-activation.d.ts +2 -0
  247. package/dist/runtime/spots/spot-entry-activation.js +20 -11
  248. package/dist/runtime/spots/spot-handler-registry.d.ts +7 -4
  249. package/dist/runtime/spots/spot-handler-registry.js +24 -10
  250. package/dist/runtime/spots/spot-manager-internal-contracts.d.ts +2 -2
  251. package/dist/runtime/spots/spot-manager-public.js +2 -2
  252. package/dist/runtime/spots/spot-node-autoconnect.d.ts +2 -1
  253. package/dist/runtime/spots/spot-node-autoconnect.js +22 -42
  254. package/dist/runtime/spots/spot-node-runtime-manager.d.ts +19 -2
  255. package/dist/runtime/spots/spot-node-runtime-manager.js +115 -50
  256. package/dist/runtime/spots/spot-outbound.d.ts +3 -0
  257. package/dist/runtime/spots/spot-outbound.js +27 -18
  258. package/dist/runtime/spots/spot-remote-codec.js +5 -1
  259. package/dist/runtime/spots/spot-routed-spot-packet-dispatch.d.ts +1 -0
  260. package/dist/runtime/spots/spot-routed-spot-packet-dispatch.js +1 -1
  261. package/dist/runtime/spots/spot-runtime-ports.d.ts +1 -1
  262. package/dist/runtime/spots/spot-serial-executor.d.ts +10 -0
  263. package/dist/runtime/spots/spot-serial-executor.js +61 -8
  264. package/dist/runtime/spots/spot-serial-turn-executor.js +19 -2
  265. package/dist/runtime/spots/spot-subscription-dispatch.js +1 -4
  266. package/dist/runtime/spots/spot-timer.d.ts +57 -5
  267. package/dist/runtime/spots/spot-timer.js +288 -103
  268. package/dist/runtime/streams/actor-session-binding-registry.d.ts +42 -11
  269. package/dist/runtime/streams/actor-session-binding-registry.js +165 -48
  270. package/dist/runtime/streams/actor-session-binding-runtime-owner.d.ts +8 -4
  271. package/dist/runtime/streams/bound-actor-relay-sender.d.ts +2 -1
  272. package/dist/runtime/streams/bound-actor-relay-sender.js +15 -8
  273. package/dist/runtime/streams/bound-session-service.d.ts +1 -1
  274. package/dist/runtime/streams/bound-session-service.js +6 -11
  275. package/dist/runtime/streams/index.d.ts +3 -2
  276. package/dist/runtime/streams/index.js +6 -4
  277. package/dist/runtime/streams/managed-stream.js +3 -2
  278. package/dist/runtime/streams/native-fallback-bound-session.js +8 -73
  279. package/dist/runtime/streams/session-actor-coordinator.d.ts +2 -8
  280. package/dist/runtime/streams/session-actor-coordinator.js +15 -32
  281. package/dist/runtime/streams/stream-binding-runtime-ports.d.ts +3 -10
  282. package/dist/runtime/streams/stream-session-runtime.d.ts +1 -1
  283. package/dist/runtime/streams/stream-session-runtime.js +14 -31
  284. package/dist/runtime/workers/index.js +1 -1
  285. package/package.json +3 -3
  286. package/src/contracts/Actors/ZLinkActorClient.ts +2 -1
  287. package/src/contracts/Channels/Calls.ts +2 -1
  288. package/src/contracts/Common/ZLinkEncodedPayload.ts +24 -4
  289. package/src/contracts/Common/ZLinkMessageMetadata.ts +13 -8
  290. package/src/contracts/Common/encoded-payload-storage.ts +6 -3
  291. package/src/contracts/Configuration/Builders.ts +4 -1
  292. package/src/contracts/Configuration/RegistrationBuilderPolicy.ts +7 -2
  293. package/src/contracts/Configuration/RegistrationBuilders.ts +6 -2
  294. package/src/contracts/Configuration/RegistrationTypes.ts +1 -0
  295. package/src/contracts/Dispatch/ZLinkDispatchOptions.ts +2 -1
  296. package/src/contracts/Handlers/Attributes.ts +14 -21
  297. package/src/contracts/Handlers/JsonContract.ts +8 -216
  298. package/src/contracts/Locations/Rows.ts +2 -0
  299. package/src/contracts/Spots/Contracts.ts +10 -1
  300. package/src/contracts/Spots/ZLinkSpot.ts +3 -3
  301. package/src/nest-integration.ts +1 -0
  302. package/src/runtime/abort.ts +12 -0
  303. package/src/runtime/actors/actor-authority-publication.ts +3 -2
  304. package/src/runtime/actors/actor-client.ts +100 -85
  305. package/src/runtime/actors/actor-context.ts +49 -9
  306. package/src/runtime/actors/actor-handoff.ts +291 -110
  307. package/src/runtime/actors/actor-join-relocation.ts +3 -0
  308. package/src/runtime/actors/actor-local-native-join.ts +21 -26
  309. package/src/runtime/actors/actor-mailbox.ts +40 -2
  310. package/src/runtime/actors/actor-remote-joiner.ts +6 -1
  311. package/src/runtime/actors/actor-remote-wire.ts +17 -0
  312. package/src/runtime/actors/actor-reply.ts +42 -0
  313. package/src/runtime/actors/actor-routed-join-transport.ts +14 -4
  314. package/src/runtime/actors/actor-routed-json-request.ts +10 -2
  315. package/src/runtime/actors/actor-runtime-contracts.ts +2 -0
  316. package/src/runtime/actors/actor-runtime-state.ts +2 -20
  317. package/src/runtime/actors/actor-source-transfer.ts +1 -34
  318. package/src/runtime/actors/index.ts +60 -32
  319. package/src/runtime/actors/session-disconnected-wire.ts +66 -0
  320. package/src/runtime/actors/spot-actor-dispatch.ts +4 -2
  321. package/src/runtime/admission.ts +69 -21
  322. package/src/runtime/application-jobs/application-ingress-record-owner.ts +41 -25
  323. package/src/runtime/application-jobs/application-job-queue-scope.ts +83 -22
  324. package/src/runtime/application-jobs/contracts.ts +16 -1
  325. package/src/runtime/backend/contracts/index.ts +92 -18
  326. package/src/runtime/backend/mesh-actor-session-node-adapter.ts +5 -3
  327. package/src/runtime/backend/mesh-completion-table.ts +160 -57
  328. package/src/runtime/backend/mesh-dispatch-pump.ts +237 -108
  329. package/src/runtime/backend/node/node-backend-adapter-support.ts +17 -8
  330. package/src/runtime/backend/node/node-dealer-channel-client-transport.ts +12 -2
  331. package/src/runtime/backend/node/node-raw-binding-port.ts +148 -85
  332. package/src/runtime/backend/node/node-raw-mesh-backend.ts +315 -201
  333. package/src/runtime/backend/node/node-socket-backend-adapter.ts +26 -11
  334. package/src/runtime/backend/raw-binding-port.ts +7 -2
  335. package/src/runtime/backend/runtime-message.ts +6 -0
  336. package/src/runtime/backend/runtime-values.ts +1 -0
  337. package/src/runtime/channels/channel-clients.ts +132 -58
  338. package/src/runtime/channels/channel-dispatch-pipeline.ts +198 -114
  339. package/src/runtime/channels/channel-dispatch-services.ts +69 -1
  340. package/src/runtime/channels/channel-dispatchers.ts +136 -90
  341. package/src/runtime/channels/channel-envelope-inspection.ts +2 -16
  342. package/src/runtime/channels/channel-envelope.ts +225 -31
  343. package/src/runtime/channels/channel-outbound-operations.ts +22 -8
  344. package/src/runtime/channels/channel-runtime-lifecycle.ts +13 -13
  345. package/src/runtime/channels/channel-runtime-manager.ts +41 -17
  346. package/src/runtime/channels/channel-transports.ts +171 -112
  347. package/src/runtime/channels/client-server-location-runtime.ts +11 -10
  348. package/src/runtime/channels/dispatch-error-reporter.ts +4 -2
  349. package/src/runtime/channels/fanout-location-runtime.ts +17 -9
  350. package/src/runtime/channels/local-spot-route-transport.ts +3 -1
  351. package/src/runtime/channels/router-socket-spot-route-transport.ts +13 -4
  352. package/src/runtime/channels/source-spot-router.ts +8 -4
  353. package/src/runtime/channels/spot-node-route-transport.ts +8 -4
  354. package/src/runtime/channels/spot-route-bridge-transport.ts +6 -4
  355. package/src/runtime/channels/spot-route-dispatch-strategy.ts +47 -18
  356. package/src/runtime/diagnostics/flow-context.ts +20 -7
  357. package/src/runtime/diagnostics/index.ts +1 -7
  358. package/src/runtime/diagnostics/message-flow.ts +47 -9
  359. package/src/runtime/diagnostics/runtime-metrics.ts +12 -10
  360. package/src/runtime/execution/index.ts +40 -14
  361. package/src/runtime/execution/serial-execution-queue.ts +33 -4
  362. package/src/runtime/execution/state-lane.ts +7 -0
  363. package/src/runtime/foundation/actor-join-recovery-codec.ts +2 -2
  364. package/src/runtime/foundation/durable-service-request.ts +137 -0
  365. package/src/runtime/foundation/operation-identity.ts +5 -1
  366. package/src/runtime/foundation/operation-registry.ts +98 -20
  367. package/src/runtime/foundation/raw-service-mesh-runtime.ts +704 -336
  368. package/src/runtime/foundation/service-liveness-registry.ts +39 -0
  369. package/src/runtime/foundation/service-mailbox.ts +8 -0
  370. package/src/runtime/foundation/service-maintenance-runtime.ts +1 -8
  371. package/src/runtime/foundation/service-relocation-object-owner.ts +55 -36
  372. package/src/runtime/foundation/service-relocation-runtime.ts +25 -4
  373. package/src/runtime/foundation/service-runtime-contracts.ts +19 -2
  374. package/src/runtime/foundation/service-session-binding-ingress-port.ts +2 -1
  375. package/src/runtime/foundation/service-stateful-registry.ts +38 -35
  376. package/src/runtime/foundation/service-stateful-runtime.ts +732 -275
  377. package/src/runtime/foundation/service-stateful-wire-codec.ts +49 -16
  378. package/src/runtime/foundation/service-topology-registry.ts +91 -36
  379. package/src/runtime/foundation/service-wire-binary-primitives.ts +35 -12
  380. package/src/runtime/foundation/service-wire-constants.generated.ts +1 -0
  381. package/src/runtime/foundation/service-wire-m6a-codec.ts +231 -76
  382. package/src/runtime/handlers/handler-instance-scope.ts +67 -16
  383. package/src/runtime/host/actor-packet-relay.ts +113 -94
  384. package/src/runtime/host/actor-placement-coordinator.ts +31 -8
  385. package/src/runtime/host/actor-runtime-options-factory.ts +13 -2
  386. package/src/runtime/host/actor-transfer-runtime.ts +104 -430
  387. package/src/runtime/host/application-job-queue.ts +160 -41
  388. package/src/runtime/host/entry-actor-runtime.ts +4 -0
  389. package/src/runtime/host/index.ts +473 -271
  390. package/src/runtime/host/location-runtime-owner.ts +2 -6
  391. package/src/runtime/host/mesh-router-resolver.ts +13 -5
  392. package/src/runtime/host/remote-bound-session-relay.ts +88 -271
  393. package/src/runtime/host/route-mesh-runtime.ts +12 -6
  394. package/src/runtime/host/runtime-shutdown.ts +15 -4
  395. package/src/runtime/host/service-relocation-control.ts +11 -5
  396. package/src/runtime/host/service-relocation-host-runtime.ts +1092 -759
  397. package/src/runtime/host/spot-address-transport.ts +4 -2
  398. package/src/runtime/host/spot-runtime-options-factory.ts +20 -7
  399. package/src/runtime/host/user-spot-creation-coordinator.ts +41 -100
  400. package/src/runtime/locations/actor-location-claims.ts +52 -30
  401. package/src/runtime/locations/auto-connect-loop.ts +22 -14
  402. package/src/runtime/locations/auto-connect-reconciler.ts +0 -6
  403. package/src/runtime/locations/auto-connect-types.ts +0 -1
  404. package/src/runtime/locations/descriptor-publication.ts +7 -0
  405. package/src/runtime/locations/domain-store-contract.ts +5 -0
  406. package/src/runtime/locations/in-memory-authority-store.ts +85 -23
  407. package/src/runtime/locations/in-memory-location-store.ts +1 -12
  408. package/src/runtime/locations/index.ts +1 -0
  409. package/src/runtime/locations/lease-tracker.ts +2 -9
  410. package/src/runtime/locations/lifecycle.ts +8 -26
  411. package/src/runtime/locations/location-store-repository.ts +628 -152
  412. package/src/runtime/locations/relocation-blob.ts +75 -1
  413. package/src/runtime/locations/request-source-resolver.ts +138 -0
  414. package/src/runtime/locations/resolvers.ts +2 -2
  415. package/src/runtime/locations/runtime.ts +98 -64
  416. package/src/runtime/messaging/framework-json-v1.ts +5 -325
  417. package/src/runtime/messaging/packet-name.ts +5 -25
  418. package/src/runtime/messaging/payload-codec.ts +8 -11
  419. package/src/runtime/messaging/request-result.ts +62 -0
  420. package/src/runtime/messaging/submission-result.ts +5 -1
  421. package/src/runtime/protocol/service_wire_codec.generated.ts +218 -5
  422. package/src/runtime/protocol/service_wire_pilot_codec.generated.ts +16 -13
  423. package/src/runtime/spots/formal-remote-actor-admission-registry.ts +46 -25
  424. package/src/runtime/spots/index.ts +389 -283
  425. package/src/runtime/spots/spot-activation-registry.ts +25 -13
  426. package/src/runtime/spots/spot-activation-state.ts +12 -19
  427. package/src/runtime/spots/spot-activation.ts +198 -96
  428. package/src/runtime/spots/spot-actor-admission-coordinator.ts +25 -55
  429. package/src/runtime/spots/spot-actor-membership.ts +0 -15
  430. package/src/runtime/spots/spot-actor-packet-dispatch.ts +56 -11
  431. package/src/runtime/spots/spot-actor-packet-drain.ts +9 -52
  432. package/src/runtime/spots/spot-entry-activation.ts +33 -21
  433. package/src/runtime/spots/spot-handler-registry.ts +56 -12
  434. package/src/runtime/spots/spot-manager-internal-contracts.ts +2 -2
  435. package/src/runtime/spots/spot-manager-public.ts +2 -2
  436. package/src/runtime/spots/spot-node-autoconnect.ts +28 -47
  437. package/src/runtime/spots/spot-node-runtime-manager.ts +197 -67
  438. package/src/runtime/spots/spot-outbound.ts +39 -19
  439. package/src/runtime/spots/spot-remote-codec.ts +9 -2
  440. package/src/runtime/spots/spot-routed-spot-packet-dispatch.ts +2 -1
  441. package/src/runtime/spots/spot-runtime-ports.ts +1 -1
  442. package/src/runtime/spots/spot-serial-executor.ts +97 -25
  443. package/src/runtime/spots/spot-serial-turn-executor.ts +22 -3
  444. package/src/runtime/spots/spot-subscription-dispatch.ts +1 -4
  445. package/src/runtime/spots/spot-timer.ts +418 -158
  446. package/src/runtime/streams/actor-session-binding-registry.ts +236 -64
  447. package/src/runtime/streams/actor-session-binding-runtime-owner.ts +16 -8
  448. package/src/runtime/streams/bound-actor-relay-sender.ts +23 -9
  449. package/src/runtime/streams/bound-session-service.ts +7 -10
  450. package/src/runtime/streams/index.ts +12 -6
  451. package/src/runtime/streams/managed-stream.ts +4 -2
  452. package/src/runtime/streams/native-fallback-bound-session.ts +14 -103
  453. package/src/runtime/streams/session-actor-coordinator.ts +39 -88
  454. package/src/runtime/streams/stream-binding-runtime-ports.ts +3 -9
  455. package/src/runtime/streams/stream-session-runtime.ts +13 -34
  456. package/src/runtime/workers/index.ts +1 -1
@@ -2,6 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ZLinkNodeRawMeshBackend = void 0;
4
4
  exports.requestFailureResult = requestFailureResult;
5
+ const routing_id_1 = require("../../routing-id");
6
+ const durable_service_request_1 = require("../../foundation/durable-service-request");
7
+ const routing_id_2 = require("../../routing-id");
5
8
  const stream_wire_1 = require("@zlink-systems/stream-wire");
6
9
  const zlink_1 = require("@zlink-systems/zlink");
7
10
  const node_crypto_1 = require("node:crypto");
@@ -32,11 +35,13 @@ const DEFAULT_NATIVE_ACTIVE_CAPACITY = 10_000;
32
35
  const UNSTARTED_DIAGNOSTIC_ENDPOINT = 'inproc://not-started';
33
36
  const MULTIPART_PACKET_NAME = service_wire_constants_generated_1.SERVICE_FRAMEWORK_MULTIPART_PACKET_NAME;
34
37
  const MULTIPART_CONTENT_TYPE = service_wire_constants_generated_1.SERVICE_FRAMEWORK_MULTIPART_CONTENT_TYPE;
35
- const FATAL_UTF8 = new TextDecoder('utf-8', { fatal: true });
36
38
  const MAX_DRAIN_RECORDS = 64;
37
- // Preserve the existing route observation/admission/liveness cadence. Only the binding
38
- // readable handler admits receive work; this timer never probes the socket.
39
+ // The maintenance timer runs at the nearest management deadline and never
40
+ // sooner than this. Only the binding readable handler admits receive work;
41
+ // this timer never probes the socket.
39
42
  const MESH_BACKEND_MAINTENANCE_INTERVAL_MS = 1;
43
+ /** The longest single timer delay Node accepts. */
44
+ const MESH_BACKEND_MAINTENANCE_MAX_DELAY_MS = 2 ** 31 - 1;
40
45
  // Spec 08 §4.2 bounds the combined turn. Raw gets one equal share; stateful
41
46
  // can use the remaining turn, including time the raw registry did not consume.
42
47
  const MESH_BACKEND_RAW_MAINTENANCE_SHARE = 1 / 2;
@@ -70,18 +75,19 @@ class ZLinkNodeRawMeshBackend {
70
75
  stateful;
71
76
  readyHandler;
72
77
  maintenanceTimer;
73
- pumping = false;
78
+ maintenanceDueMs = Number.POSITIVE_INFINITY;
79
+ pumpTask;
74
80
  readable = 0;
75
81
  onReadable = (receiveReady, routeReady) => {
76
82
  if (receiveReady)
77
83
  this.readable |= 1;
78
84
  if (routeReady)
79
85
  this.readable |= 2;
80
- if (!this.closed && !this.pumping)
81
- void this.pump();
86
+ this.pump();
82
87
  };
83
88
  onMaintenance = () => {
84
89
  this.maintenanceTimer = undefined;
90
+ this.maintenanceDueMs = Number.POSITIVE_INFINITY;
85
91
  if (this.closed)
86
92
  return;
87
93
  const nowMs = performance.now();
@@ -89,7 +95,7 @@ class ZLinkNodeRawMeshBackend {
89
95
  this.runtime?.expireOperations(nowMs, nowMs + raw_service_mesh_runtime_1.RAW_MESH_RECEIVE_TIME_BUDGET_MS * MESH_BACKEND_RAW_MAINTENANCE_SHARE);
90
96
  this.stateful?.expireOperations(nowMs, turnDeadlineMs);
91
97
  this.scheduleMaintenance();
92
- void this.pump();
98
+ this.pump();
93
99
  };
94
100
  onPendingOperationsChanged = () => this.scheduleMaintenance();
95
101
  nextPeerIntent = 1n;
@@ -104,6 +110,8 @@ class ZLinkNodeRawMeshBackend {
104
110
  protocolError;
105
111
  messageFollowHandler;
106
112
  dispatchErrors;
113
+ sessionOwnerDescriptors;
114
+ requestSourceAcceptance;
107
115
  peerDisconnectedHandlers = new Set();
108
116
  constructor(meshName, routingId, bindingPort, applicationJobQueue, applicationJobReceiveFlowFailureSink, peerAdmissionSealed, infrastructureTaskRunner) {
109
117
  this.meshName = meshName;
@@ -137,6 +145,10 @@ class ZLinkNodeRawMeshBackend {
137
145
  this.dispatchErrors = reporter;
138
146
  this.stateful?.setDispatchErrorReporter(reporter, this.meshName);
139
147
  }
148
+ setSessionOwnerDescriptors(source) {
149
+ this.sessionOwnerDescriptors = source;
150
+ this.stateful?.setSessionOwnerDescriptors(source);
151
+ }
140
152
  setProtocolErrorHandler(handler) {
141
153
  this.protocolError = handler;
142
154
  }
@@ -168,11 +180,11 @@ class ZLinkNodeRawMeshBackend {
168
180
  const result = await this.requireStateful().sendToMissingInstanceSpotFrame(target, encodeMultipartApplicationFrame(parts), deadlineUnixMs, sourceSpotId, metadata === undefined ? undefined : (0, service_metadata_codec_1.encodeServiceMetadataFrame)(metadata));
169
181
  return result;
170
182
  }
171
- requestToMissingInstanceSpot(target, parts, deadlineUnixMs, sourceSpotId, metadata) {
172
- return this.observeStateful(service_runtime_contracts_1.OperationKind.InstanceSpotRequest, this.requireStateful().requestToMissingInstanceSpotFrame(target, encodeMultipartApplicationFrame(parts), deadlineUnixMs, sourceSpotId, metadata === undefined ? undefined : (0, service_metadata_codec_1.encodeServiceMetadataFrame)(metadata)));
183
+ requestToMissingInstanceSpot(target, parts, deadlineUnixMs, sourceSpotId, metadata, onAdmitted) {
184
+ return this.observeStateful(service_runtime_contracts_1.OperationKind.InstanceSpotRequest, this.requireStateful().requestToMissingInstanceSpotFrame(target, encodeMultipartApplicationFrame(parts), deadlineUnixMs, sourceSpotId, metadata === undefined ? undefined : (0, service_metadata_codec_1.encodeServiceMetadataFrame)(metadata), onAdmitted));
173
185
  }
174
186
  setRoutingId(routingId) {
175
- if (String(routingId) !== this.routingId) {
187
+ if (!(0, routing_id_1.routingIdsEqual)((0, routing_id_2.toBackendRoutingId)(routingId), this.routingId)) {
176
188
  throw new Error('MeshNode routing id is immutable after construction.');
177
189
  }
178
190
  }
@@ -221,13 +233,7 @@ class ZLinkNodeRawMeshBackend {
221
233
  }
222
234
  }
223
235
  },
224
- onPeerDisconnected: (nodeRoutingId, endpoint) => {
225
- for (const [intentId, peer] of this.peerIntents) {
226
- if (peer.endpoint === endpoint &&
227
- (peer.nodeRoutingId === undefined || peer.nodeRoutingId === nodeRoutingId)) {
228
- this.peerIntents.delete(intentId);
229
- }
230
- }
236
+ onPeerDisconnected: (_nodeRoutingId, endpoint) => {
231
237
  for (const handler of this.peerDisconnectedHandlers)
232
238
  handler(endpoint);
233
239
  }
@@ -237,16 +243,22 @@ class ZLinkNodeRawMeshBackend {
237
243
  if (this.dispatchErrors !== undefined) {
238
244
  this.stateful.setDispatchErrorReporter(this.dispatchErrors, this.meshName);
239
245
  }
246
+ if (this.sessionOwnerDescriptors !== undefined) {
247
+ this.stateful.setSessionOwnerDescriptors(this.sessionOwnerDescriptors);
248
+ }
240
249
  if (this.mailboxRecordDropped !== undefined) {
241
250
  this.stateful.setMailboxDropHandler(this.mailboxRecordDropped);
242
251
  }
243
252
  this.stateful.setMessageFollowHandler((record) => this.messageFollowHandler?.(record));
253
+ if (this.requestSourceAcceptance !== undefined) {
254
+ this.stateful.setRequestSourceAcceptance(this.requestSourceAcceptance);
255
+ }
244
256
  this.runtime = runtime;
245
257
  try {
246
258
  runtime.setReadableHandler(this.onReadable);
247
259
  }
248
260
  catch (error) {
249
- this.close();
261
+ void this.close().catch((failure) => runtime.reportInfrastructureFailure(failure));
250
262
  throw error;
251
263
  }
252
264
  this.scheduleMaintenance();
@@ -257,26 +269,30 @@ class ZLinkNodeRawMeshBackend {
257
269
  async sendMessageFollowNotification(targetNodeRid, record) {
258
270
  return await this.requireStateful().sendMessageFollowNotification(targetNodeRid, record);
259
271
  }
260
- shutdown(_timeoutMs) {
261
- this.close();
272
+ async shutdown(_timeoutMs) {
273
+ await this.close();
262
274
  return zlink_1.RequestResult.Ok;
263
275
  }
276
+ cancelPendingOperations(reason) {
277
+ this.stateful?.cancelPendingOperations(reason);
278
+ this.runtime?.cancelPendingOperations(reason);
279
+ }
264
280
  async publishDraining() {
265
281
  for (const name of this.channels.keys())
266
282
  this.channels.set(name, 0);
267
283
  await this.requireRuntime().updateLocalDescriptor({ state: 'draining' });
268
284
  }
269
- close() {
270
- if (this.closed)
271
- return;
285
+ async close() {
286
+ this.closed = true;
272
287
  if (this.maintenanceTimer !== undefined)
273
288
  clearTimeout(this.maintenanceTimer);
274
289
  this.maintenanceTimer = undefined;
290
+ if (this.pumpTask !== undefined)
291
+ await this.pumpTask;
275
292
  this.stateful?.close();
276
293
  this.stateful = undefined;
277
294
  this.runtime?.close();
278
295
  this.runtime = undefined;
279
- this.closed = true;
280
296
  }
281
297
  addChannelName(name) {
282
298
  this.requireNotStarted();
@@ -328,6 +344,10 @@ class ZLinkNodeRawMeshBackend {
328
344
  });
329
345
  return intent;
330
346
  }
347
+ setRequestSourceAcceptance(acceptance) {
348
+ this.requestSourceAcceptance = acceptance;
349
+ this.stateful?.setRequestSourceAcceptance(acceptance);
350
+ }
331
351
  expectPeer(peer) {
332
352
  this.requireRuntime().expectPeerByRoutingId(peer.endpoint, peer.nodeRoutingId, peer.securityIdentity, peer.lifecycleGeneration);
333
353
  }
@@ -379,13 +399,31 @@ class ZLinkNodeRawMeshBackend {
379
399
  isObjectClientNodeDirectTarget(targetRid) {
380
400
  return this.requireRuntime().isObjectClientNodeDirectTarget(String(targetRid));
381
401
  }
402
+ waitForPeerRoute(targetRid, lifecycleGeneration, deadlineUnixMs, signal) {
403
+ return this.requireRuntime().waitForPeerRoute(String(targetRid), lifecycleGeneration, deadlineUnixMs, signal);
404
+ }
405
+ observePeerRoute(targetRid) {
406
+ return this.requireRuntime().observePeerRoute(String(targetRid));
407
+ }
408
+ hasPeerRouteOrConnectionIntent(targetRid, endpoint) {
409
+ const rid = String(targetRid);
410
+ if (this.requireRuntime().hasSelectedPeerRoute(rid))
411
+ return true;
412
+ for (const intent of this.peerIntents.values()) {
413
+ if (intent.nodeRoutingId === rid ||
414
+ (intent.nodeRoutingId === undefined && intent.endpoint === endpoint))
415
+ return true;
416
+ }
417
+ return false;
418
+ }
382
419
  isPeerRouteReady(targetRid, lifecycleGeneration) {
383
420
  return this.requireRuntime().isPeerRouteReady(String(targetRid), lifecycleGeneration);
384
421
  }
385
- async sendToNode(targetRid, parts) {
386
- return (await this.requireRuntime().sendToNode(String(targetRid), encodeMultipartApplicationFrame(parts)))
387
- ? zlink_1.SubmitResult.Ok
388
- : zlink_1.SubmitResult.NotConnected;
422
+ sendToNode(targetRid, parts) {
423
+ const submitted = this.requireRuntime().sendToNode(String(targetRid), encodeMultipartApplicationFrame(parts));
424
+ return submitted instanceof Promise
425
+ ? submitted.then(nodeSendResult)
426
+ : nodeSendResult(submitted);
389
427
  }
390
428
  async sendInfrastructureControl(targetRid, record) {
391
429
  return (await this.requireRuntime().sendService(String(targetRid), (Array.isArray(record) ? record : [record]).map((value) => Buffer.from(value))))
@@ -398,18 +436,18 @@ class ZLinkNodeRawMeshBackend {
398
436
  : zlink_1.SubmitResult.NotConnected;
399
437
  }
400
438
  async requestInfrastructureControlFrames(targetRid, frames, options) {
401
- return await this.requireRuntime().requestService(String(targetRid), frames.map((frame) => Buffer.from(frame)), options?.timeoutMs ?? Registration_1.DEFAULT_REQUEST_TIMEOUT_MS);
439
+ return (0, durable_service_request_1.requestDurableService)(this.requireRuntime(), String(targetRid), frames, performance.now() + (options?.timeoutMs ?? Registration_1.DEFAULT_REQUEST_TIMEOUT_MS), 'relocation control', options?.signal, options?.onSubmitted);
402
440
  }
403
441
  requestToNode(targetRid, parts, options) {
404
- const pending = this.requireRuntime().requestToNode(String(targetRid), encodeMultipartApplicationFrame(parts), options?.timeoutMs ?? Registration_1.DEFAULT_REQUEST_TIMEOUT_MS);
405
- return this.observeCompletion(pending.id, service_runtime_contracts_1.OperationKind.NodeRequest, pending.promise);
442
+ const pending = this.requireRuntime().requestToNode(String(targetRid), encodeMultipartApplicationFrame(parts), options?.timeoutMs ?? Registration_1.DEFAULT_REQUEST_TIMEOUT_MS, options?.onAdmitted, this.requestObserver, service_runtime_contracts_1.OperationKind.NodeRequest);
443
+ return this.observedOperation(pending, service_runtime_contracts_1.OperationKind.NodeRequest);
406
444
  }
407
445
  async sendToChannel(channelName, parts) {
408
446
  return this.requireRuntime().sendToChannel(channelName, encodeMultipartApplicationFrame(parts));
409
447
  }
410
448
  requestToChannel(channelName, parts, options) {
411
- const pending = this.requireRuntime().requestToChannel(channelName, encodeMultipartApplicationFrame(parts), options?.timeoutMs ?? Registration_1.DEFAULT_REQUEST_TIMEOUT_MS);
412
- return this.observeCompletion(pending.id, service_runtime_contracts_1.OperationKind.ChannelRequest, pending.promise);
449
+ const pending = this.requireRuntime().requestToChannel(channelName, encodeMultipartApplicationFrame(parts), options?.timeoutMs ?? Registration_1.DEFAULT_REQUEST_TIMEOUT_MS, options?.onAdmitted, this.requestObserver, service_runtime_contracts_1.OperationKind.ChannelRequest);
450
+ return this.observedOperation(pending, service_runtime_contracts_1.OperationKind.ChannelRequest);
413
451
  }
414
452
  status() {
415
453
  const descriptor = this.runtime?.topology.localDescriptor() ?? this.createDescriptor();
@@ -570,7 +608,8 @@ class ZLinkNodeRawMeshBackend {
570
608
  return this.requireStateful().instanceSpotApplicationTarget(spotId);
571
609
  }
572
610
  waitForInstanceApplicationQuiescence(spotId, signal) {
573
- return this.requireStateful().waitForInstanceApplicationQuiescence(spotId, signal);
611
+ // Closing the stateful owner terminalizes its operations and clears these waiters.
612
+ return this.stateful?.waitForInstanceApplicationQuiescence(spotId, signal) ?? Promise.resolve();
574
613
  }
575
614
  restoreUserSpotAuthority(spotId, stableType, generation, authorityOwnerGeneration) {
576
615
  return new RawServiceSpot(this, this.requireStateful().restoreUserSpotAuthority(spotId, stableType, generation, authorityOwnerGeneration));
@@ -598,7 +637,7 @@ class ZLinkNodeRawMeshBackend {
598
637
  nodeRid: String(actor.nodeRid),
599
638
  actorId: actor.actorId,
600
639
  generation: actor.generation
601
- }, String(sessionNodeRid), String(sessionRid), bindingGeneration);
640
+ }, String(sessionNodeRid), (0, routing_id_2.toBackendRoutingId)(sessionRid), bindingGeneration);
602
641
  }
603
642
  registerInstanceIntent(instanceType, route, expectedCurrentRoute) {
604
643
  this.requireStateful().registerInstanceIntent(instanceType, route, expectedCurrentRoute);
@@ -666,8 +705,8 @@ class ZLinkNodeRawMeshBackend {
666
705
  const result = await this.requireStateful().sendToInstanceSpot(route, encodeMultipart(parts), sourceSpotId, metadata === undefined ? undefined : (0, service_metadata_codec_1.encodeServiceMetadataFrame)(metadata), instanceIntent);
667
706
  return result;
668
707
  }
669
- requestInstanceSpot(route, parts, timeoutMs = Registration_1.DEFAULT_REQUEST_TIMEOUT_MS, sourceSpotId, metadata, instanceIntent = false) {
670
- return this.observeStateful(service_runtime_contracts_1.OperationKind.InstanceSpotRequest, this.requireStateful().requestToInstanceSpot(route, encodeMultipart(parts), timeoutMs, sourceSpotId, metadata === undefined ? undefined : (0, service_metadata_codec_1.encodeServiceMetadataFrame)(metadata), instanceIntent));
708
+ requestInstanceSpot(route, parts, timeoutMs = Registration_1.DEFAULT_REQUEST_TIMEOUT_MS, sourceSpotId, metadata, instanceIntent = false, onAdmitted) {
709
+ return this.observeStateful(service_runtime_contracts_1.OperationKind.InstanceSpotRequest, this.requireStateful().requestToInstanceSpot(route, encodeMultipart(parts), timeoutMs, sourceSpotId, metadata === undefined ? undefined : (0, service_metadata_codec_1.encodeServiceMetadataFrame)(metadata), instanceIntent, onAdmitted));
671
710
  }
672
711
  actorLookup(actorId) {
673
712
  const actor = this.requireStateful().actor(actorId);
@@ -703,27 +742,26 @@ class ZLinkNodeRawMeshBackend {
703
742
  const target = String(targetNodeRid);
704
743
  return this.observeStateful(service_runtime_contracts_1.OperationKind.ActorJoin, this.requireStateful().joinActorEntrySpotCanonical(actor, target, request, actorFence, local, timeoutMs));
705
744
  }
706
- async sendToActor(actor, parts) {
707
- return this.requireStateful().sendToActor(actor, this.peerGeneration(String(actor.nodeRid)), actor.generation, encodeMultipart(parts));
745
+ async sendToActor(actor, parts, options) {
746
+ return this.requireStateful().sendToActor(actor, encodeMultipart(parts), undefined, undefined, options?.routeFence);
708
747
  }
709
748
  requestToActor(actor, parts, options) {
710
- return this.observeStateful(service_runtime_contracts_1.OperationKind.ActorRequest, this.requireStateful().requestToActor(actor, this.peerGeneration(String(actor.nodeRid)), actor.generation, encodeMultipart(parts), options?.timeoutMs ?? Registration_1.DEFAULT_REQUEST_TIMEOUT_MS));
749
+ return this.observeStateful(service_runtime_contracts_1.OperationKind.ActorRequest, this.requireStateful().requestToActor(actor, encodeMultipart(parts), options?.timeoutMs ?? Registration_1.DEFAULT_REQUEST_TIMEOUT_MS, undefined, undefined, options?.onAdmitted, options?.routeFence));
750
+ }
751
+ acceptRelocatedActorTerminal(operation, replyRouteId, result) {
752
+ return this.requireStateful().acceptRelocatedActorTerminal(operation, replyRouteId, result);
711
753
  }
712
754
  async actorSendToActor(source, target, parts) {
713
- return (await this.requireStateful().sendToActor(target, this.peerGeneration(String(target.nodeRid)), target.generation, encodeMultipart(parts), source));
755
+ return (await this.requireStateful().sendToActor(target, encodeMultipart(parts), source));
714
756
  }
715
757
  actorRequestToActor(source, target, parts, options) {
716
- return this.observeStateful(service_runtime_contracts_1.OperationKind.ActorRequest, this.requireStateful().requestToActor(target, this.peerGeneration(String(target.nodeRid)), target.generation, encodeMultipart(parts), options?.timeoutMs ?? Registration_1.DEFAULT_REQUEST_TIMEOUT_MS, source));
758
+ return this.observeStateful(service_runtime_contracts_1.OperationKind.ActorRequest, this.requireStateful().requestToActor(target, encodeMultipart(parts), options?.timeoutMs ?? Registration_1.DEFAULT_REQUEST_TIMEOUT_MS, source, undefined, options?.onAdmitted));
717
759
  }
718
760
  async sendActorBoundSession(actor, expectedBindingGeneration, parts, _flags) {
719
761
  return (await this.requireStateful().sendBoundSession(actor, expectedBindingGeneration, encodeMultipart(parts)));
720
762
  }
721
763
  closeActorBoundSession(actor, expectedBindingGeneration, timeoutMs = Registration_1.DEFAULT_REQUEST_TIMEOUT_MS) {
722
- const binding = this.requireStateful().registry.binding(actor);
723
- if (binding === undefined) {
724
- return this.observeImmediateFailure(service_runtime_contracts_1.OperationKind.StreamUnbind, zlink_1.RequestResult.NotFound);
725
- }
726
- return this.observeStateful(service_runtime_contracts_1.OperationKind.StreamUnbind, this.requireStateful().unbindSession(binding.sessionRid, actor, expectedBindingGeneration, timeoutMs));
764
+ return this.observeStateful(service_runtime_contracts_1.OperationKind.StreamUnbind, this.requireStateful().closeBoundSession(actor, expectedBindingGeneration, timeoutMs));
727
765
  }
728
766
  leaveActor(actor, expectedMembershipEpoch, timeoutMs = Registration_1.DEFAULT_REQUEST_TIMEOUT_MS) {
729
767
  return this.observeStateful(service_runtime_contracts_1.OperationKind.ActorLeave, this.requireStateful().leaveActor(actor, expectedMembershipEpoch, timeoutMs));
@@ -734,8 +772,8 @@ class ZLinkNodeRawMeshBackend {
734
772
  async sendFromSpot(source, target, parts) {
735
773
  return (await this.requireStateful().sendToSpot(source.ref.spotId, target, encodeMultipart(parts)));
736
774
  }
737
- requestFromSpot(source, target, parts, timeoutMs) {
738
- return this.observeStateful(service_runtime_contracts_1.OperationKind.SpotRequest, this.requireStateful().requestToSpot(source.ref.spotId, target, encodeMultipart(parts), timeoutMs));
775
+ requestFromSpot(source, target, parts, timeoutMs, onAdmitted) {
776
+ return this.observeStateful(service_runtime_contracts_1.OperationKind.SpotRequest, this.requireStateful().requestToSpot(source.ref.spotId, target, encodeMultipart(parts), timeoutMs, onAdmitted));
739
777
  }
740
778
  closeSpot(state) {
741
779
  return this.requireStateful().registry.closeSpot(state.ref);
@@ -744,6 +782,8 @@ class ZLinkNodeRawMeshBackend {
744
782
  await this.requireStateful().publishLogicalMulticast(channelName, topic, encodeMultipart(parts), state.ref.spotId);
745
783
  }
746
784
  setSpotSubscription(state, channelName, topicFilter) {
785
+ if (!this.channels.has(channelName))
786
+ throw new contracts_1.ZLinkFrameworkException(contracts_1.ZLinkFrameworkErrorKind.InvalidOperation, `Spot does not participate in subscription channel '${channelName}'.`);
747
787
  this.requireStateful().setSubscription(state, channelName, topicFilter);
748
788
  }
749
789
  unsetSpotSubscription(state, channelName, topicFilter) {
@@ -784,10 +824,29 @@ class ZLinkNodeRawMeshBackend {
784
824
  }
785
825
  return result;
786
826
  }
827
+ /**
828
+ * Arms the one maintenance timer for the nearest management deadline
829
+ * (spec 08 §4.2): request expiry, liveness, or a pending peer handshake.
830
+ * An earlier deadline re-arms it; a later one keeps the armed time.
831
+ */
787
832
  scheduleMaintenance() {
788
833
  if (this.closed)
789
834
  return;
790
- this.maintenanceTimer ??= setTimeout(this.onMaintenance, MESH_BACKEND_MAINTENANCE_INTERVAL_MS);
835
+ const nowMs = performance.now();
836
+ const dueMs = Math.max(nowMs + MESH_BACKEND_MAINTENANCE_INTERVAL_MS, Math.min(this.runtime?.nextMaintenanceDeadlineMs(nowMs) ?? Number.POSITIVE_INFINITY, this.stateful?.nextMaintenanceDeadlineMs() ?? Number.POSITIVE_INFINITY));
837
+ if (dueMs === Number.POSITIVE_INFINITY) {
838
+ if (this.maintenanceTimer !== undefined)
839
+ clearTimeout(this.maintenanceTimer);
840
+ this.maintenanceTimer = undefined;
841
+ this.maintenanceDueMs = dueMs;
842
+ return;
843
+ }
844
+ if (this.maintenanceTimer === undefined || dueMs < this.maintenanceDueMs) {
845
+ if (this.maintenanceTimer !== undefined)
846
+ clearTimeout(this.maintenanceTimer);
847
+ this.maintenanceTimer = setTimeout(this.onMaintenance, Math.min(MESH_BACKEND_MAINTENANCE_MAX_DELAY_MS, Math.ceil(dueMs - nowMs)));
848
+ this.maintenanceDueMs = dueMs;
849
+ }
791
850
  const pending = (this.runtime?.pendingOperationCount ?? 0) + (this.stateful?.pendingOperationCount ?? 0) > 0;
792
851
  if (pending === this.maintenanceTimer.hasRef())
793
852
  return;
@@ -798,15 +857,19 @@ class ZLinkNodeRawMeshBackend {
798
857
  this.maintenanceTimer.unref();
799
858
  }
800
859
  }
801
- async pump() {
802
- if (this.isClosed() || this.pumping)
860
+ pump() {
861
+ if (this.closed || this.pumpTask !== undefined)
803
862
  return;
804
- this.pumping = true;
863
+ this.pumpTask = this.runPump(this.requireRuntime());
864
+ }
865
+ async runPump(runtime) {
866
+ // Publish pumpTask before any callback can signal readiness or begin close.
867
+ await Promise.resolve();
805
868
  try {
806
869
  do {
807
870
  const ready = this.readable;
808
871
  this.readable = 0;
809
- const more = await this.requireRuntime().pumpBatch((ready & 1) !== 0, (ready & 2) !== 0);
872
+ const more = await runtime.pumpBatch((ready & 1) !== 0, (ready & 2) !== 0);
810
873
  if (more) {
811
874
  // Yield only after actual progress, retaining readiness across the
812
875
  // existing batch limits until receive reports no data.
@@ -815,14 +878,14 @@ class ZLinkNodeRawMeshBackend {
815
878
  }
816
879
  } while (!this.closed && this.readable !== 0);
817
880
  }
881
+ catch (error) {
882
+ runtime.reportInfrastructureFailure(error);
883
+ }
818
884
  finally {
819
- this.pumping = false;
885
+ this.pumpTask = undefined;
820
886
  this.scheduleMaintenance();
821
887
  }
822
888
  }
823
- isClosed() {
824
- return this.closed;
825
- }
826
889
  notifyReady() {
827
890
  const runtime = this.runtime;
828
891
  let domains = service_runtime_contracts_1.ReadyDomain.None;
@@ -835,6 +898,19 @@ class ZLinkNodeRawMeshBackend {
835
898
  if (domains !== service_runtime_contracts_1.ReadyDomain.None)
836
899
  this.readyHandler?.(domains);
837
900
  }
901
+ /**
902
+ * The raw registry takes a remote request's terminal and hands it here,
903
+ * where the Framework completion owner receives it in the same turn.
904
+ */
905
+ requestObserver = {
906
+ operationCompleted: (id, result, operationKind) => this.deliverCompletion({ high: 1n, low: id }, operationKind ?? 0, result),
907
+ operationFailed: (id, error, operationKind) => this.deliverCompletion({ high: 1n, low: id }, operationKind ?? 0, requestFailureResult(error))
908
+ };
909
+ observedOperation(pending, operationKind) {
910
+ return pending.promise === undefined
911
+ ? { high: 1n, low: pending.id }
912
+ : this.observeCompletion(pending.id, operationKind, pending.promise);
913
+ }
838
914
  observeCompletion(low, operationKind, promise) {
839
915
  const id = { high: 1n, low };
840
916
  void promise.then((result) => this.deliverCompletion(id, operationKind, result), (error) => this.deliverCompletion(id, operationKind, requestFailureResult(error)));
@@ -845,10 +921,6 @@ class ZLinkNodeRawMeshBackend {
845
921
  void pending.promise.then((result) => this.deliverCompletion(id, operationKind, result), (error) => this.deliverCompletion(id, operationKind, requestFailureResult(error)));
846
922
  return id;
847
923
  }
848
- observeImmediateFailure(operationKind, terminalResult) {
849
- const low = this.nextPeerIntent++;
850
- return this.observeCompletion(low, operationKind, Promise.resolve({ terminalResult, failureCode: 0 }));
851
- }
852
924
  deliverCompletion(operationId, operationKind, result) {
853
925
  if (this.completionHandler !== undefined) {
854
926
  this.completionHandler({ operationId, terminalResult: result.terminalResult }, () => completionRecord(operationId, operationKind, result));
@@ -953,7 +1025,7 @@ class RawServiceSpot {
953
1025
  const routeFence = requireDirectSpotRouteFence(options?.routeFence, String(targetNodeRid), String(targetSpotId), targetSpotGeneration, options?.entrySpot === true
954
1026
  ? this.backend.entrySpotRouteFence(String(targetNodeRid), String(targetSpotId))
955
1027
  : undefined);
956
- return this.backend.requestFromSpot(this.state, routeFence, parts, options?.timeoutMs ?? Registration_1.DEFAULT_REQUEST_TIMEOUT_MS);
1028
+ return this.backend.requestFromSpot(this.state, routeFence, parts, options?.timeoutMs ?? Registration_1.DEFAULT_REQUEST_TIMEOUT_MS, options?.onAdmitted);
957
1029
  }
958
1030
  async publish(channelName, topic, parts, options) {
959
1031
  this.requireOpen();
@@ -992,6 +1064,9 @@ class RawServiceSpot {
992
1064
  throw new Error(`Spot '${this.routingId}' is closed.`);
993
1065
  }
994
1066
  }
1067
+ function nodeSendResult(submitted) {
1068
+ return submitted ? zlink_1.SubmitResult.Ok : zlink_1.SubmitResult.NotConnected;
1069
+ }
995
1070
  function requireDirectSpotRouteFence(route, targetNodeRid, targetSpotId, targetSpotGeneration, entryRoute) {
996
1071
  if (route === undefined && entryRoute !== undefined) {
997
1072
  return entryRoute;
@@ -1040,7 +1115,7 @@ class RawStreamSessionService {
1040
1115
  return {
1041
1116
  state: this.state,
1042
1117
  lifecycleGeneration: 1n,
1043
- sessionCount: BigInt(new Set(bindings.map((value) => value.sessionRid)).size),
1118
+ sessionCount: BigInt(new Set(bindings.map((value) => (0, routing_id_2.encodeRoutingIdStorageHex)(value.sessionRid))).size),
1044
1119
  bindingCount: BigInt(bindings.length),
1045
1120
  pendingMessageCount: 0n,
1046
1121
  pendingByteCount: 0n,
@@ -1051,32 +1126,32 @@ class RawStreamSessionService {
1051
1126
  this.requireStarted();
1052
1127
  return this.observe(service_runtime_contracts_1.OperationKind.ActorLookup, this.stateful.lookupRemoteActor(String(targetNodeRid), actorId, timeoutMs));
1053
1128
  }
1054
- bindActor(sessionRid, actor, timeoutMs = Registration_1.DEFAULT_REQUEST_TIMEOUT_MS, onBindingReplaced, actorAuthority) {
1129
+ bindActor(sessionRid, actor, timeoutMs = Registration_1.DEFAULT_REQUEST_TIMEOUT_MS, onBindingReplaced, actorAuthority, close) {
1055
1130
  this.requireStarted();
1056
1131
  const sessionKey = String(sessionRid);
1057
1132
  this.sessionTargets.set(sessionKey, sessionRid);
1058
- return this.observe(service_runtime_contracts_1.OperationKind.StreamBind, this.stateful.bindSession(sessionKey, actor, timeoutMs, (targetSessionRid, payloadFrame) => this.deliver(targetSessionRid, actor.actorId, payloadFrame), onBindingReplaced, actorAuthority));
1133
+ return this.observe(service_runtime_contracts_1.OperationKind.StreamBind, this.stateful.bindSession(sessionRid, actor, timeoutMs, (_targetSessionRid, payloadFrame) => this.deliver(sessionKey, actor.actorId, payloadFrame), onBindingReplaced, actorAuthority, close));
1059
1134
  }
1060
1135
  unbindActor(sessionRid, actor, expectedBindingGeneration, timeoutMs = Registration_1.DEFAULT_REQUEST_TIMEOUT_MS) {
1061
1136
  this.requireStarted();
1062
- return this.observe(service_runtime_contracts_1.OperationKind.StreamUnbind, this.stateful.unbindSession(String(sessionRid), actor, expectedBindingGeneration, timeoutMs));
1137
+ return this.observe(service_runtime_contracts_1.OperationKind.StreamUnbind, this.stateful.unbindSession(sessionRid, actor, expectedBindingGeneration, timeoutMs));
1063
1138
  }
1064
1139
  bindings(sessionRid) {
1065
- return this.stateful.sessionBindings(String(sessionRid));
1140
+ return this.stateful.sessionBindings(sessionRid);
1066
1141
  }
1067
1142
  async sendToActor(sessionRid, actor, parts) {
1068
1143
  this.requireStarted();
1069
- return (await this.stateful.sendSessionToActor(String(sessionRid), actor, encodeMultipart(parts)));
1144
+ return (await this.stateful.sendSessionToActor(sessionRid, actor, encodeMultipart(parts)));
1070
1145
  }
1071
1146
  async deliver(sessionRid, actorId, payload) {
1072
1147
  const target = this.sessionTargets.get(sessionRid);
1073
1148
  if (target === undefined)
1074
1149
  return false;
1075
1150
  const operation = this.stream.send(target);
1076
- const parts = decodeMultipartBuffers((0, service_wire_m6a_codec_1.decodeApplicationPayloadView)(payload).payload);
1151
+ const parts = (0, service_wire_m6a_codec_1.decodeMultipartParts)((0, service_wire_m6a_codec_1.decodeApplicationPayloadView)(payload).payload, borrowMultipartPart);
1077
1152
  if (parts.length === 0)
1078
1153
  return false;
1079
- const actorSlot = await (0, service_session_binding_ingress_port_1.serviceSessionBindingIngressPortIfRegistered)(this)?.actorSlot(actorId, sessionRid);
1154
+ const actorSlot = await (0, service_session_binding_ingress_port_1.serviceSessionBindingIngressPortIfRegistered)(this)?.actorSlot(actorId, target);
1080
1155
  if (actorSlot === undefined)
1081
1156
  return false;
1082
1157
  parts[0] = Buffer.from(streamFrameWithActorSlot(parts[0], actorSlot));
@@ -1097,7 +1172,7 @@ class RawStreamSessionService {
1097
1172
  case zlink_1.SubmitResult.Terminated:
1098
1173
  // Let the socket monitor drive the existing STREAM close/unbind
1099
1174
  // lifecycle instead of maintaining a second session-close state.
1100
- (0, node_socket_backend_adapter_1.disconnectStreamPeer)(this.stream, target);
1175
+ (0, node_socket_backend_adapter_1.disconnectSocketPeer)(this.stream, (0, node_backend_adapter_support_1.toNativeRoutingId)(target));
1101
1176
  throw new runtime_values_1.ZLinkBackendResultError('submit', error.result, error.nativeErrno, {
1102
1177
  cause: error
1103
1178
  });
@@ -1325,7 +1400,7 @@ function decodeMultipartRecord(runtime, record) {
1325
1400
  const header = record.parts[0];
1326
1401
  const command = header[service_wire_binary_primitives_1.SERVICE_WIRE_COMMAND_OFFSET];
1327
1402
  const payloadFrame = record.parts[1];
1328
- const application = decodeApplicationEnvelope(payloadFrame);
1403
+ const parts = (0, service_wire_m6a_codec_1.decodeMultipartApplicationParts)(payloadFrame, runtime_message_1.ZLinkBufferMessage.fromOwned, closeMultipartMessage);
1329
1404
  const channelName = record.owner.startsWith('channel:')
1330
1405
  ? record.owner.slice('channel:'.length)
1331
1406
  : null;
@@ -1344,7 +1419,7 @@ function decodeMultipartRecord(runtime, record) {
1344
1419
  : kind === service_runtime_contracts_1.ReceiveKind.ChannelRequest
1345
1420
  ? service_runtime_contracts_1.OperationKind.ChannelRequest
1346
1421
  : 0;
1347
- const ingressLifecycle = receiveIngressLifecycle(record);
1422
+ const applicationJob = record.applicationJob;
1348
1423
  return {
1349
1424
  kind,
1350
1425
  domain: readyDomain(record.domain),
@@ -1357,22 +1432,30 @@ function decodeMultipartRecord(runtime, record) {
1357
1432
  channelName,
1358
1433
  topic: null,
1359
1434
  applicationMetadata: null,
1360
- packetName: application.packetName,
1361
- contentType: application.contentType,
1435
+ packetName: service_wire_constants_generated_1.SERVICE_FRAMEWORK_MULTIPART_PACKET_NAME,
1436
+ contentType: service_wire_constants_generated_1.SERVICE_FRAMEWORK_MULTIPART_CONTENT_TYPE,
1362
1437
  kindData: null,
1363
1438
  terminalResult: 0,
1364
1439
  failureErrno: 0,
1365
- ...ingressLifecycle,
1366
- parts: decodeMultipart(application.payload),
1440
+ applicationJobPermit: applicationJob,
1441
+ releaseRetainedIngress: applicationJob === undefined ? undefined : releaseRecordApplicationJob,
1442
+ parts,
1367
1443
  reply(parts) {
1368
1444
  if (record.correlation === undefined)
1369
1445
  return zlink_1.SubmitResult.InvalidState;
1370
1446
  runtime.reply(record, encodeMultipartApplicationFrame(parts));
1371
1447
  return zlink_1.SubmitResult.Ok;
1372
1448
  },
1373
- replyActorJoin: () => zlink_1.SubmitResult.NotSupported
1449
+ replyActorJoin: replyActorJoinNotSupported
1374
1450
  };
1375
1451
  }
1452
+ /** Shared by every ordinary record: the record's own permit lease is its retained ingress. */
1453
+ function releaseRecordApplicationJob() {
1454
+ this.applicationJobPermit?.close();
1455
+ }
1456
+ function replyActorJoinNotSupported() {
1457
+ return zlink_1.SubmitResult.NotSupported;
1458
+ }
1376
1459
  function decodeStatefulRecord(record, stateful) {
1377
1460
  const payloadFrame = record.parts.length < 2 ? undefined : record.parts[1];
1378
1461
  const canonicalActorJoin = stateful.kindData?.kind === 'actorControl' &&
@@ -1383,13 +1466,18 @@ function decodeStatefulRecord(record, stateful) {
1383
1466
  ? stateful.canonicalApplicationPayload
1384
1467
  : payloadFrame === undefined
1385
1468
  ? undefined
1386
- : decodeApplicationEnvelope(payloadFrame);
1469
+ : (0, service_wire_m6a_codec_1.decodeMultipartApplicationPayloadView)(payloadFrame);
1387
1470
  const operationId = record.correlation === undefined
1388
1471
  ? { high: 0n, low: 0n }
1389
1472
  : { high: operation_identity_1.ZLINK_NATIVE_CORRELATION_OPERATION_NAMESPACE, low: record.correlation };
1390
1473
  const ingressLifecycle = receiveIngressLifecycle(record);
1391
1474
  return {
1392
1475
  kind: stateful.receiveKind,
1476
+ get requestSource() {
1477
+ return record.requestSource;
1478
+ },
1479
+ prepareDispatch: record.sourcePreparationOwner === undefined ? undefined : prepareStatefulMailboxDispatch,
1480
+ statefulMailboxRecord: record,
1393
1481
  domain: readyDomain(record.domain),
1394
1482
  sourceNodeRid: record.sourceRoutingId ?? null,
1395
1483
  sourceSpotId: stateful.sourceSpotId ?? null,
@@ -1483,6 +1571,13 @@ function receiveIngressLifecycle(record) {
1483
1571
  releaseRetainedIngress: () => applicationJob.close()
1484
1572
  };
1485
1573
  }
1574
+ function prepareStatefulMailboxDispatch(signal) {
1575
+ const mailboxRecord = this.statefulMailboxRecord;
1576
+ const owner = mailboxRecord?.sourcePreparationOwner;
1577
+ return mailboxRecord === undefined || owner === undefined
1578
+ ? true
1579
+ : owner.prepareDispatch(mailboxRecord, signal);
1580
+ }
1486
1581
  function readyOwner(owner, nodeRid, stateful, domain) {
1487
1582
  if (owner.startsWith('spot:')) {
1488
1583
  return {
@@ -1592,92 +1687,14 @@ function encodeMultipartApplicationFrame(parts) {
1592
1687
  throw new TypeError('Multipart payload must contain at least one part.');
1593
1688
  return (0, service_wire_m6a_codec_1.encodeMultipartApplicationPayload)(values.map(messageBytesView), MULTIPART_PACKET_NAME, MULTIPART_CONTENT_TYPE);
1594
1689
  }
1595
- function decodeApplicationEnvelope(frame) {
1596
- const bytes = Buffer.from(frame.buffer, frame.byteOffset, frame.byteLength);
1597
- if (bytes.length < service_wire_m6a_codec_1.APPLICATION_PAYLOAD_MINIMUM_FIELD_BYTES ||
1598
- bytes[0] !== service_wire_m6a_codec_1.APPLICATION_PAYLOAD_VERSION) {
1599
- throw new service_wire_m6a_codec_1.ServiceWireProtocolError('Invalid application envelope.');
1600
- }
1601
- const bodyLength = bytes.readUInt32BE(1);
1602
- if (bodyLength !== bytes.length - 5) {
1603
- throw new service_wire_m6a_codec_1.ServiceWireProtocolError('Invalid application envelope length.');
1604
- }
1605
- let offset = 5;
1606
- const packetLength = bytes[offset++];
1607
- if (packetLength === 0 || bytes.length - offset < packetLength + 1) {
1608
- throw new service_wire_m6a_codec_1.ServiceWireProtocolError('Invalid application envelope packet name.');
1609
- }
1610
- let packetName;
1611
- try {
1612
- packetName = FATAL_UTF8.decode(bytes.subarray(offset, offset + packetLength));
1613
- }
1614
- catch {
1615
- throw new service_wire_m6a_codec_1.ServiceWireProtocolError('Invalid application envelope packet name.');
1616
- }
1617
- offset += packetLength;
1618
- const contentLength = bytes[offset++];
1619
- if (contentLength === 0 || bytes.length - offset < contentLength + 4) {
1620
- throw new service_wire_m6a_codec_1.ServiceWireProtocolError('Invalid application envelope content type.');
1621
- }
1622
- let contentType;
1623
- try {
1624
- contentType = FATAL_UTF8.decode(bytes.subarray(offset, offset + contentLength));
1625
- }
1626
- catch {
1627
- throw new service_wire_m6a_codec_1.ServiceWireProtocolError('Invalid application envelope content type.');
1628
- }
1629
- offset += contentLength;
1630
- const payloadLength = bytes.readUInt32BE(offset);
1631
- offset += 4;
1632
- if (packetName !== MULTIPART_PACKET_NAME ||
1633
- contentType !== MULTIPART_CONTENT_TYPE ||
1634
- payloadLength !== bytes.length - offset) {
1635
- throw new service_wire_m6a_codec_1.ServiceWireProtocolError('Unexpected M6A application payload.');
1636
- }
1637
- return { packetName, contentType, payload: bytes.subarray(offset) };
1638
- }
1639
1690
  function decodeMultipart(payload) {
1640
- const buffers = decodeMultipartBuffers(payload);
1641
- const parts = new Array(buffers.length);
1642
- let materialized = 0;
1643
- try {
1644
- for (; materialized < buffers.length; materialized += 1) {
1645
- parts[materialized] = runtime_message_1.ZLinkBufferMessage.fromOwned(buffers[materialized]);
1646
- }
1647
- return parts;
1648
- }
1649
- catch (error) {
1650
- for (let index = 0; index < materialized; index += 1)
1651
- parts[index].close();
1652
- throw error;
1653
- }
1691
+ return (0, service_wire_m6a_codec_1.decodeMultipartParts)(payload, runtime_message_1.ZLinkBufferMessage.fromOwned, closeMultipartMessage);
1654
1692
  }
1655
- function decodeMultipartBuffers(payload) {
1656
- const bytes = Buffer.from(payload.buffer, payload.byteOffset, payload.byteLength);
1657
- if (bytes.length < 4)
1658
- throw new service_wire_m6a_codec_1.ServiceWireProtocolError('Truncated multipart payload.');
1659
- const count = bytes.readUInt32BE(0);
1660
- if (count === 0 || count > Math.floor((bytes.length - 4) / 4)) {
1661
- throw new service_wire_m6a_codec_1.ServiceWireProtocolError('Invalid multipart part count.');
1662
- }
1663
- const result = new Array(count);
1664
- let offset = 4;
1665
- for (let index = 0; index < count; index++) {
1666
- if (bytes.length - offset < 4) {
1667
- throw new service_wire_m6a_codec_1.ServiceWireProtocolError('Truncated multipart part length.');
1668
- }
1669
- const length = bytes.readUInt32BE(offset);
1670
- offset += 4;
1671
- if (bytes.length - offset < length) {
1672
- throw new service_wire_m6a_codec_1.ServiceWireProtocolError('Truncated multipart part.');
1673
- }
1674
- result[index] = bytes.subarray(offset, offset + length);
1675
- offset += length;
1676
- }
1677
- if (offset !== bytes.length) {
1678
- throw new service_wire_m6a_codec_1.ServiceWireProtocolError('Multipart payload has trailing bytes.');
1679
- }
1680
- return result;
1693
+ function closeMultipartMessage(part) {
1694
+ part.close();
1695
+ }
1696
+ function borrowMultipartPart(part) {
1697
+ return part;
1681
1698
  }
1682
1699
  function messageBytesView(value) {
1683
1700
  if (typeof value === 'object' && 'data' in value) {