@series-inc/rundot-syncplay 6.0.0-rc.2 → 6.0.0-rc.21

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 (329) hide show
  1. package/README.md +133 -0
  2. package/core/deterministic-math.d.ts +75 -1
  3. package/core/native/instance_ledger.hpp +1095 -0
  4. package/core/native/legacy_checkpoint_ids.hpp +107 -0
  5. package/core/native/physics/abi.cpp +39 -211
  6. package/core/native/physics/abi.hpp +2 -6
  7. package/core/native/physics/articulation.cpp +1434 -0
  8. package/core/native/physics/articulation.hpp +279 -0
  9. package/core/native/physics/buoyancy.cpp +1588 -0
  10. package/core/native/physics/buoyancy.hpp +254 -0
  11. package/core/native/physics/capsule_triangle.cpp +168 -0
  12. package/core/native/physics/capsule_triangle.hpp +40 -0
  13. package/core/native/physics/capsule_triangle_native.inc +291 -0
  14. package/core/native/physics/certified_sum.hpp +218 -0
  15. package/core/native/physics/character.cpp +638 -0
  16. package/core/native/physics/character.hpp +169 -0
  17. package/core/native/physics/checkpoint_schema.hpp +379 -0
  18. package/core/native/physics/cloth.cpp +1401 -0
  19. package/core/native/physics/cloth.hpp +357 -0
  20. package/core/native/physics/constraint_rows.cpp +363 -2
  21. package/core/native/physics/constraint_rows.hpp +72 -1
  22. package/core/native/physics/deformable_query.cpp +1630 -0
  23. package/core/native/physics/deformable_query.hpp +578 -0
  24. package/core/native/physics/distance.cpp +1166 -0
  25. package/core/native/physics/distance.hpp +151 -0
  26. package/core/native/physics/exact/exact-applied.cpp +101 -0
  27. package/core/native/physics/exact/exact-applied.hpp +112 -0
  28. package/core/native/physics/exact/exact-dyadic.cpp +691 -0
  29. package/core/native/physics/exact/exact-dyadic.hpp +58 -0
  30. package/core/native/physics/exact/exact-operation.hpp +51 -0
  31. package/core/native/physics/exact/word-core.hpp +262 -0
  32. package/core/native/physics/extended_abi.cpp +1242 -0
  33. package/core/native/physics/extended_abi.hpp +355 -0
  34. package/core/native/physics/extended_abi_characters.cpp +261 -0
  35. package/core/native/physics/extended_abi_characters.hpp +434 -0
  36. package/core/native/physics/extended_abi_core_features.cpp +713 -0
  37. package/core/native/physics/extended_abi_core_features.hpp +226 -0
  38. package/core/native/physics/extended_abi_deformables.cpp +448 -0
  39. package/core/native/physics/extended_abi_deformables.hpp +867 -0
  40. package/core/native/physics/extended_abi_vehicles.cpp +411 -0
  41. package/core/native/physics/extended_abi_vehicles.hpp +222 -0
  42. package/core/native/physics/fracture.cpp +1981 -0
  43. package/core/native/physics/fracture.hpp +460 -0
  44. package/core/native/physics/gear.cpp +563 -0
  45. package/core/native/physics/gear.hpp +158 -0
  46. package/core/native/physics/geometry_memory.hpp +476 -0
  47. package/core/native/physics/geometry_store.cpp +547 -0
  48. package/core/native/physics/geometry_store.hpp +134 -0
  49. package/core/native/physics/lag_compensation.cpp +1626 -0
  50. package/core/native/physics/lag_compensation.hpp +405 -0
  51. package/core/native/physics/mass_properties.cpp +449 -0
  52. package/core/native/physics/mass_properties.hpp +23 -0
  53. package/core/native/physics/material_pose.hpp +988 -0
  54. package/core/native/physics/particles.cpp +1065 -0
  55. package/core/native/physics/particles.hpp +261 -0
  56. package/core/native/physics/path_joint.cpp +781 -0
  57. package/core/native/physics/path_joint.hpp +154 -0
  58. package/core/native/physics/physics.cpp +13043 -1065
  59. package/core/native/physics/physics.hpp +184 -8
  60. package/core/native/physics/planar_deformable.cpp +2433 -0
  61. package/core/native/physics/planar_deformable.hpp +889 -0
  62. package/core/native/physics/platformer_2d.cpp +1353 -0
  63. package/core/native/physics/platformer_2d.hpp +340 -0
  64. package/core/native/physics/powered_ragdoll.cpp +1100 -0
  65. package/core/native/physics/powered_ragdoll.hpp +279 -0
  66. package/core/native/physics/powertrain.cpp +586 -0
  67. package/core/native/physics/powertrain.hpp +180 -0
  68. package/core/native/physics/pulley.cpp +311 -0
  69. package/core/native/physics/pulley.hpp +121 -0
  70. package/core/native/physics/ragdoll.cpp +586 -0
  71. package/core/native/physics/ragdoll.hpp +221 -0
  72. package/core/native/physics/rope.cpp +692 -0
  73. package/core/native/physics/rope.hpp +215 -0
  74. package/core/native/physics/rounded_convex.cpp +1229 -0
  75. package/core/native/physics/rounded_convex.hpp +287 -0
  76. package/core/native/physics/sdf_geometry.cpp +1821 -0
  77. package/core/native/physics/sdf_geometry.hpp +474 -0
  78. package/core/native/physics/soft_body.cpp +1449 -0
  79. package/core/native/physics/soft_body.hpp +328 -0
  80. package/core/native/physics/soft_collision.cpp +1862 -0
  81. package/core/native/physics/soft_collision.hpp +561 -0
  82. package/core/native/physics/surface_velocity.cpp +366 -0
  83. package/core/native/physics/surface_velocity.hpp +243 -0
  84. package/core/native/physics/topology.cpp +357 -39
  85. package/core/native/physics/topology.hpp +97 -6
  86. package/core/native/physics/tracks.cpp +1179 -0
  87. package/core/native/physics/tracks.hpp +404 -0
  88. package/core/native/physics/vehicle.cpp +629 -0
  89. package/core/native/physics/vehicle.hpp +191 -0
  90. package/core/native/physics/voxel_cooking.cpp +1929 -0
  91. package/core/native/physics/voxel_cooking.hpp +395 -0
  92. package/core/native/physics/world.cpp +5290 -854
  93. package/core/native/physics/world.hpp +796 -24
  94. package/core/native/physics/world_abi.cpp +3840 -306
  95. package/core/native/physics/world_abi.hpp +530 -10
  96. package/core/native/physics/world_digest_visitor.hpp +277 -0
  97. package/core/native/physics/world_strong_digest.hpp +557 -0
  98. package/core/native/physics2d/ccd.cpp +1302 -0
  99. package/core/native/physics2d/ccd.hpp +141 -0
  100. package/core/native/physics2d/checkpoint_memory.hpp +349 -0
  101. package/core/native/physics2d/clock_primitives.hpp +138 -0
  102. package/core/native/physics2d/commands.cpp +174 -28
  103. package/core/native/physics2d/commands.hpp +3 -1
  104. package/core/native/physics2d/compound.cpp +581 -0
  105. package/core/native/physics2d/compound.hpp +248 -0
  106. package/core/native/physics2d/contour.cpp +325 -0
  107. package/core/native/physics2d/contour.hpp +143 -0
  108. package/core/native/physics2d/frame_scope.hpp +124 -0
  109. package/core/native/physics2d/joint_phases.hpp +93 -0
  110. package/core/native/physics2d/joints.cpp +587 -150
  111. package/core/native/physics2d/joints.hpp +5 -0
  112. package/core/native/physics2d/memory_private.hpp +125 -0
  113. package/core/native/physics2d/owned_abi.cpp +1354 -879
  114. package/core/native/physics2d/owned_abi.hpp +190 -52
  115. package/core/native/physics2d/phase_test_hooks.hpp +17 -0
  116. package/core/native/physics2d/physics2d.cpp +2073 -120
  117. package/core/native/physics2d/physics2d.hpp +154 -2
  118. package/core/native/physics2d/queries.cpp +1055 -79
  119. package/core/native/physics2d/queries.hpp +71 -5
  120. package/core/native/physics2d/world.cpp +1313 -353
  121. package/core/native/physics2d/world.hpp +124 -13
  122. package/core/native/sha256/sha256_compress_unsigned.hpp +68 -0
  123. package/core/native/sha256/sha256_fips_constants.hpp +24 -0
  124. package/core/native/sha256/sha256_length.hpp +56 -0
  125. package/core/native/sha256/sha256_memo.hpp +48 -0
  126. package/core/native/sha256/sha256_sink.hpp +234 -0
  127. package/core/physics2d-owned.d.ts +466 -126
  128. package/core/runtime.d.ts +192 -19
  129. package/core/scripts/build-native.mjs +34 -0
  130. package/core/scripts/build-wasm-physics.mjs +272 -23
  131. package/core/scripts/extended-physics-profile.mjs +634 -0
  132. package/core/scripts/generate-physics2d-vectors.mjs +59 -0
  133. package/core/src/kinetix-deterministic-math.d.ts +2 -0
  134. package/core/src/kinetix-deterministic-math.mjs +513 -27
  135. package/core/src/kinetix-owned-runtime-adapter.mjs +788 -0
  136. package/core/src/kinetix-runtime-adapter-utils.mjs +54 -6
  137. package/core/src/kinetix-runtime-contract.mjs +55 -9
  138. package/core/src/kinetix-runtime-identity.mjs +12 -1
  139. package/core/src/kinetix-session-config.mjs +7 -2
  140. package/core/src/kinetix-state-report.mjs +36 -0
  141. package/core/src/physics/abi-constants.mjs +5 -4
  142. package/core/src/physics/articulation.d.mts +194 -0
  143. package/core/src/physics/articulation.mjs +1141 -0
  144. package/core/src/physics/buoyancy.d.mts +143 -0
  145. package/core/src/physics/buoyancy.mjs +268 -0
  146. package/core/src/physics/cloth.d.mts +314 -0
  147. package/core/src/physics/cloth.mjs +1156 -0
  148. package/core/src/physics/deformable-query.d.mts +217 -0
  149. package/core/src/physics/deformable-query.mjs +1412 -0
  150. package/core/src/physics/distance.d.mts +160 -0
  151. package/core/src/physics/distance.mjs +175 -0
  152. package/core/src/physics/extended-wasm-instance.d.mts +5 -0
  153. package/core/src/physics/extended-wasm-instance.mjs +48 -0
  154. package/core/src/physics/feature-api.d.mts +74 -0
  155. package/core/src/physics/feature-api.mjs +98 -0
  156. package/core/src/physics/fracture.d.mts +133 -0
  157. package/core/src/physics/fracture.mjs +1324 -0
  158. package/core/src/physics/gear.d.mts +585 -0
  159. package/core/src/physics/gear.mjs +955 -0
  160. package/core/src/physics/lag-compensation.d.mts +101 -0
  161. package/core/src/physics/lag-compensation.mjs +1100 -0
  162. package/core/src/physics/mass-properties.d.mts +114 -0
  163. package/core/src/physics/mass-properties.mjs +137 -468
  164. package/core/src/physics/particles.d.mts +177 -0
  165. package/core/src/physics/particles.mjs +758 -0
  166. package/core/src/physics/path-joint.d.mts +171 -0
  167. package/core/src/physics/path-joint.mjs +1231 -0
  168. package/core/src/physics/physics-extended-wasm-module.generated.mjs +13 -0
  169. package/core/src/physics/physics-profile-wasm-module.generated.mjs +48 -0
  170. package/core/src/physics/physics-wasm-module.generated.mjs +34 -5
  171. package/core/src/physics/powered-ragdoll.d.mts +188 -0
  172. package/core/src/physics/powered-ragdoll.mjs +1043 -0
  173. package/core/src/physics/powertrain.d.mts +260 -0
  174. package/core/src/physics/powertrain.mjs +621 -0
  175. package/core/src/physics/pulley.d.mts +98 -0
  176. package/core/src/physics/pulley.mjs +686 -0
  177. package/core/src/physics/ragdoll.d.mts +289 -0
  178. package/core/src/physics/ragdoll.mjs +1323 -0
  179. package/core/src/physics/rope.d.mts +241 -0
  180. package/core/src/physics/rope.mjs +319 -0
  181. package/core/src/physics/rounded-convex.d.mts +328 -0
  182. package/core/src/physics/rounded-convex.mjs +1404 -0
  183. package/core/src/physics/sdf-geometry.d.mts +117 -0
  184. package/core/src/physics/sdf-geometry.mjs +1204 -0
  185. package/core/src/physics/soft-body.d.mts +144 -0
  186. package/core/src/physics/soft-body.mjs +1285 -0
  187. package/core/src/physics/soft-collision.d.mts +651 -0
  188. package/core/src/physics/soft-collision.mjs +1883 -0
  189. package/core/src/physics/surface-velocity.d.mts +156 -0
  190. package/core/src/physics/surface-velocity.mjs +722 -0
  191. package/core/src/physics/tracks.d.mts +411 -0
  192. package/core/src/physics/tracks.mjs +1074 -0
  193. package/core/src/physics/vehicle.d.mts +3 -0
  194. package/core/src/physics/vehicle.mjs +172 -0
  195. package/core/src/physics/voxel-cooking.d.mts +219 -0
  196. package/core/src/physics/voxel-cooking.mjs +1499 -0
  197. package/core/src/physics/voxel-swept-quad.mjs +110 -0
  198. package/core/src/physics/wasm-instance.mjs +160 -23
  199. package/core/src/physics/world.d.mts +62 -0
  200. package/core/src/physics/world.mjs +1947 -152
  201. package/core/src/physics2d/body-state.mjs +218 -15
  202. package/core/src/physics2d/collision.d.mts +52 -0
  203. package/core/src/physics2d/collision.mjs +234 -0
  204. package/core/src/physics2d/compound.mjs +454 -0
  205. package/core/src/physics2d/contour-state.mjs +771 -0
  206. package/core/src/physics2d/feature-api.d.mts +211 -0
  207. package/core/src/physics2d/feature-api.mjs +3 -0
  208. package/core/src/physics2d/fluid-state.mjs +145 -0
  209. package/core/src/physics2d/joint-state.mjs +1 -1
  210. package/core/src/physics2d/owned-queries.mjs +3 -1
  211. package/core/src/physics2d/owned-transport.mjs +268 -122
  212. package/core/src/physics2d/owned-world.mjs +353 -230
  213. package/core/src/physics2d/particle-fluid.d.mts +152 -0
  214. package/core/src/physics2d/particle-fluid.mjs +233 -0
  215. package/core/src/physics2d/physics2d-wasm-module.generated.mjs +27 -10
  216. package/core/src/physics2d/query-codec.mjs +61 -13
  217. package/core/src/physics2d/submerged-geometry.d.mts +51 -0
  218. package/core/src/physics2d/submerged-geometry.mjs +186 -0
  219. package/core/src/runtime.mjs +23 -0
  220. package/core/src/shared/bytes.d.mts +2 -0
  221. package/core/src/shared/bytes.mjs +33 -0
  222. package/core/src/shared/sha256.d.mts +3 -0
  223. package/core/src/shared/sha256.mjs +72 -22
  224. package/core/units.d.ts +50 -0
  225. package/core/world.d.ts +253 -5
  226. package/dist/authority-room.d.ts +9 -0
  227. package/dist/authority-room.js +45 -16
  228. package/dist/browser.d.ts +6 -5
  229. package/dist/browser.js +4 -3
  230. package/dist/cadence.d.ts +7 -0
  231. package/dist/cadence.js +19 -0
  232. package/dist/canonical.d.ts +1 -0
  233. package/dist/canonical.js +3 -0
  234. package/dist/checksum-policy.d.ts +10 -0
  235. package/dist/checksum-policy.js +14 -0
  236. package/dist/collider-cooking-hull.d.ts +18 -0
  237. package/dist/collider-cooking-hull.js +248 -0
  238. package/dist/collider-cooking.d.ts +140 -0
  239. package/dist/collider-cooking.js +794 -0
  240. package/dist/compat/v5.d.ts +121 -0
  241. package/dist/compat/v5.js +479 -0
  242. package/dist/creator.d.ts +7 -6
  243. package/dist/creator.js +4 -3
  244. package/dist/game-byte-section.d.ts +36 -0
  245. package/dist/game-byte-section.js +350 -0
  246. package/dist/index.d.ts +13 -6
  247. package/dist/index.js +9 -4
  248. package/dist/instant-replay.d.ts +27 -2
  249. package/dist/instant-replay.js +91 -16
  250. package/dist/lag-compensation3d.js +11 -10
  251. package/dist/match-log.d.ts +21 -4
  252. package/dist/match-log.js +92 -9
  253. package/dist/math.d.ts +2 -0
  254. package/dist/math.js +1 -0
  255. package/dist/modules/character-3d.d.ts +16 -1
  256. package/dist/modules/character-3d.js +42 -5
  257. package/dist/modules/combat.js +6 -5
  258. package/dist/movement3d-revision1.d.ts +2 -0
  259. package/dist/movement3d-revision1.js +973 -0
  260. package/dist/movement3d.d.ts +53 -3
  261. package/dist/movement3d.js +917 -247
  262. package/dist/networked-client.d.ts +15 -0
  263. package/dist/networked-client.js +378 -105
  264. package/dist/node.d.ts +3 -2
  265. package/dist/node.js +2 -1
  266. package/dist/physics/2d.d.ts +38 -4
  267. package/dist/physics/2d.js +57 -36
  268. package/dist/physics/3d.d.ts +755 -111
  269. package/dist/physics/3d.js +2355 -158
  270. package/dist/physics/edit-result-graph.d.ts +36 -0
  271. package/dist/physics/edit-result-graph.js +141 -0
  272. package/dist/physics/game-codec3d.d.ts +41 -0
  273. package/dist/physics/game-codec3d.js +544 -0
  274. package/dist/physics/installed-checkpoint3d.d.ts +13 -0
  275. package/dist/physics/installed-checkpoint3d.js +120 -0
  276. package/dist/physics/installed-runtime3d.d.ts +51 -0
  277. package/dist/physics/installed-runtime3d.js +372 -0
  278. package/dist/physics/installed-state-commitment3d.d.ts +2 -0
  279. package/dist/physics/installed-state-commitment3d.js +24 -0
  280. package/dist/physics/units.d.ts +38 -0
  281. package/dist/physics/units.js +56 -0
  282. package/dist/physics/vehicle-state3d.d.ts +93 -0
  283. package/dist/physics/vehicle-state3d.js +1191 -0
  284. package/dist/physics/voxel-body-state.d.ts +61 -0
  285. package/dist/physics/voxel-body-state.js +410 -0
  286. package/dist/r3f-render-adapter.d.ts +59 -1
  287. package/dist/r3f-render-adapter.js +137 -2
  288. package/dist/replay.d.ts +13 -0
  289. package/dist/replay.js +95 -70
  290. package/dist/run-room-transport.js +5 -1
  291. package/dist/runner.d.ts +17 -2
  292. package/dist/runner.js +472 -84
  293. package/dist/runtime-cleanup.d.ts +54 -0
  294. package/dist/runtime-cleanup.js +266 -0
  295. package/dist/runtime-lease.d.ts +11 -1
  296. package/dist/runtime-lease.js +23 -10
  297. package/dist/runtime-room-metadata.d.ts +2 -8
  298. package/dist/runtime-room-metadata.js +11 -14
  299. package/dist/runtime-session.d.ts +8 -0
  300. package/dist/runtime-session.js +222 -58
  301. package/dist/schema-codegen.js +3 -0
  302. package/dist/schema-dsl-binary.d.ts +10 -4
  303. package/dist/schema-dsl-binary.js +375 -76
  304. package/dist/scratch-pool.d.ts +32 -0
  305. package/dist/scratch-pool.js +60 -0
  306. package/dist/sdk-session.d.ts +1 -0
  307. package/dist/sdk-session.js +4 -0
  308. package/dist/secret-config.js +2 -60
  309. package/dist/secret-crypto.js +70 -36
  310. package/dist/session-snapshot.d.ts +15 -4
  311. package/dist/session-snapshot.js +35 -33
  312. package/dist/session-wire.js +14 -22
  313. package/dist/sparse-set.d.ts +1 -0
  314. package/dist/sparse-set.js +22 -0
  315. package/dist/spatial/spatial-grid-3d.d.ts +41 -0
  316. package/dist/spatial/spatial-grid-3d.js +115 -0
  317. package/dist/synctest.d.ts +9 -0
  318. package/dist/synctest.js +106 -17
  319. package/dist/testing-network.d.ts +2 -0
  320. package/dist/testing-network.js +122 -18
  321. package/dist/testing.d.ts +27 -6
  322. package/dist/testing.js +297 -56
  323. package/dist/tools/index.d.ts +3 -2
  324. package/dist/tools/index.js +3 -2
  325. package/dist/tools/module-certification.d.ts +6 -0
  326. package/dist/tools/module-certification.js +26 -2
  327. package/package.json +18 -5
  328. package/core/scripts/run-box3d-benchmark.mjs +0 -350
  329. package/core/src/physics/index.mjs +0 -3169
@@ -0,0 +1,788 @@
1
+ import { assertKinetixRuntimeIdentity, kinetixStateReportDomain } from './kinetix-runtime-contract.mjs';
2
+ import { kinetixRuntimeIdentityDigest } from './kinetix-runtime-identity.mjs';
3
+ import { createKinetixStateReporter } from './kinetix-state-report.mjs';
4
+ import { kinetixSessionConfigDigest } from './kinetix-session-config.mjs';
5
+ import { sha256Hex } from './shared/sha256.mjs';
6
+ import { copyUint8Array, uint8ArrayByteLength } from './shared/bytes.mjs';
7
+
8
+ function fail(code, cause) {
9
+ throw new Error(code, arguments.length < 2 ? undefined : { cause });
10
+ }
11
+
12
+ function isPlainObject(value) {
13
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
14
+ && Object.getPrototypeOf(value) === Object.prototype;
15
+ }
16
+
17
+ function isUint8Array(value) {
18
+ return uint8ArrayByteLength(value) !== undefined;
19
+ }
20
+
21
+ function equalBytes(left, right) {
22
+ const count = uint8ArrayByteLength(left);
23
+ if (count === undefined || count !== uint8ArrayByteLength(right)) return false;
24
+ for (let index = 0; index < count; index += 1) if (left[index] !== right[index]) return false;
25
+ return true;
26
+ }
27
+
28
+ function isCheckpointFailure(error) {
29
+ return error instanceof Error
30
+ && error.message.startsWith('KINETIX_RUNTIME_CHECKPOINT_INVALID');
31
+ }
32
+
33
+ function createCell(captured, frame, revision) {
34
+ return {
35
+ captured,
36
+ frame,
37
+ revision,
38
+ refs: 0,
39
+ released: false,
40
+ serialized: null,
41
+ checksumHex: null,
42
+ wireChecksumHex: null,
43
+ };
44
+ }
45
+
46
+ export function createOwnedInstalledRuntimeAdapter(options) {
47
+ if (!isPlainObject(options)) fail('KINETIX_RUNTIME_ADAPTER_INVALID');
48
+
49
+ const {
50
+ identity,
51
+ sessionConfigBytes: rawSessionConfigBytes,
52
+ step,
53
+ decodeCommand,
54
+ stateStorage,
55
+ } = options;
56
+
57
+ assertKinetixRuntimeIdentity(identity);
58
+
59
+ if (!isUint8Array(rawSessionConfigBytes)) {
60
+ fail('KINETIX_RUNTIME_ADAPTER_INVALID: sessionConfigBytes must be a Uint8Array');
61
+ }
62
+ const sessionConfigBytes = copyUint8Array(rawSessionConfigBytes);
63
+ if (typeof step !== 'function') {
64
+ fail('KINETIX_RUNTIME_ADAPTER_INVALID: step must be a function');
65
+ }
66
+ if (decodeCommand !== undefined && typeof decodeCommand !== 'function') {
67
+ fail('KINETIX_RUNTIME_ADAPTER_INVALID: decodeCommand must be a function');
68
+ }
69
+ if (!isPlainObject(stateStorage) || stateStorage.kind !== 'owned') {
70
+ fail('KINETIX_RUNTIME_ADAPTER_INVALID: stateStorage must be kind owned');
71
+ }
72
+ const strongReport = identity.stateDigestDomain === kinetixStateReportDomain;
73
+ if (strongReport ? typeof stateStorage.stateCommitment !== 'function' : stateStorage.stateCommitment !== undefined) {
74
+ fail('KINETIX_RUNTIME_IDENTITY_DOMAIN_MISMATCH');
75
+ }
76
+
77
+ const { checkpointBudget } = stateStorage;
78
+ if (
79
+ !isPlainObject(checkpointBudget)
80
+ || !Number.isSafeInteger(checkpointBudget.maxLiveCheckpoints) || checkpointBudget.maxLiveCheckpoints <= 0
81
+ || !Number.isSafeInteger(checkpointBudget.maxAdvanceBatch) || checkpointBudget.maxAdvanceBatch <= 0
82
+ || !Number.isSafeInteger(checkpointBudget.importScratch) || checkpointBudget.importScratch <= 0
83
+ ) {
84
+ fail('KINETIX_RUNTIME_ADAPTER_INVALID: invalid checkpointBudget');
85
+ }
86
+
87
+ const requiredHooks = [
88
+ 'captureState',
89
+ 'restoreState',
90
+ 'releaseState',
91
+ 'frameOf',
92
+ 'inspectState',
93
+ 'serializeState',
94
+ 'hydrateState',
95
+ 'checksumStateBytes',
96
+ 'readLiveRevision',
97
+ ];
98
+ for (const hook of requiredHooks) {
99
+ if (typeof stateStorage[hook] !== 'function') {
100
+ fail(`KINETIX_RUNTIME_ADAPTER_INVALID: missing required hook ${hook} on stateStorage`);
101
+ }
102
+ }
103
+
104
+ if (stateStorage.projectState !== undefined && typeof stateStorage.projectState !== 'function') {
105
+ fail('KINETIX_RUNTIME_ADAPTER_INVALID: projectState must be a function');
106
+ }
107
+ if (stateStorage.preflightAdvance !== undefined && typeof stateStorage.preflightAdvance !== 'function') {
108
+ fail('KINETIX_RUNTIME_ADAPTER_INVALID: preflightAdvance must be a function');
109
+ }
110
+ if (stateStorage.runReadOnly !== undefined && typeof stateStorage.runReadOnly !== 'function') {
111
+ fail('KINETIX_RUNTIME_ADAPTER_INVALID: runReadOnly must be a function');
112
+ }
113
+ if (stateStorage.validateSnapshot !== undefined && typeof stateStorage.validateSnapshot !== 'function') {
114
+ fail('KINETIX_RUNTIME_ADAPTER_INVALID: validateSnapshot must be a function');
115
+ }
116
+ const maxSnapshotBytes = stateStorage.maxSnapshotBytes;
117
+ if (maxSnapshotBytes !== undefined
118
+ && (!Number.isSafeInteger(maxSnapshotBytes) || maxSnapshotBytes < 1)) {
119
+ fail('KINETIX_RUNTIME_ADAPTER_INVALID: invalid maxSnapshotBytes');
120
+ }
121
+
122
+ const budget = Object.freeze({
123
+ maxLiveCheckpoints: checkpointBudget.maxLiveCheckpoints,
124
+ maxAdvanceBatch: checkpointBudget.maxAdvanceBatch,
125
+ importScratch: checkpointBudget.importScratch,
126
+ });
127
+ const hooks = Object.freeze({
128
+ captureState: stateStorage.captureState.bind(stateStorage),
129
+ restoreState: stateStorage.restoreState.bind(stateStorage),
130
+ releaseState: stateStorage.releaseState.bind(stateStorage),
131
+ frameOf: stateStorage.frameOf.bind(stateStorage),
132
+ inspectState: stateStorage.inspectState.bind(stateStorage),
133
+ serializeState: stateStorage.serializeState.bind(stateStorage),
134
+ hydrateState: stateStorage.hydrateState.bind(stateStorage),
135
+ checksumStateBytes: stateStorage.checksumStateBytes.bind(stateStorage),
136
+ stateCommitment: stateStorage.stateCommitment?.bind(stateStorage),
137
+ readLiveRevision: stateStorage.readLiveRevision.bind(stateStorage),
138
+ preflightAdvance: stateStorage.preflightAdvance?.bind(stateStorage),
139
+ validateSnapshot: stateStorage.validateSnapshot?.bind(stateStorage),
140
+ runReadOnly: stateStorage.runReadOnly === undefined ? action => action()
141
+ : stateStorage.runReadOnly.bind(stateStorage),
142
+ });
143
+ const projectState = stateStorage.projectState === undefined
144
+ ? undefined
145
+ : stateStorage.projectState.bind(stateStorage);
146
+
147
+ let disposed = false;
148
+ let runtimeFailed = false;
149
+ let disposePromise = null;
150
+ let operation;
151
+
152
+ function withOperation(name, work) {
153
+ if (operation !== undefined) fail(`KINETIX_RUNTIME_REENTRANT: ${name} during ${operation}`);
154
+ operation = name;
155
+ try { return work(); }
156
+ finally { operation = undefined; }
157
+ }
158
+
159
+ function assertNotDisposed() {
160
+ if (disposed) fail('KINETIX_RUNTIME_DISPOSED');
161
+ }
162
+
163
+ function assertNotFailed() {
164
+ if (runtimeFailed) fail('KINETIX_RUNTIME_FAILED');
165
+ }
166
+
167
+ const handles = new Map();
168
+ const activeCells = new Set();
169
+
170
+ function pin(cell) {
171
+ if (cell.released) {
172
+ fail('KINETIX_RUNTIME_CHECKPOINT_INVALID: attempt to pin released cell');
173
+ }
174
+ cell.refs += 1;
175
+ }
176
+
177
+ function unpin(cell) {
178
+ if (cell.refs <= 0) {
179
+ fail('KINETIX_RUNTIME_CHECKPOINT_INVALID: ref count underflow');
180
+ }
181
+ cell.refs -= 1;
182
+ if (cell.refs === 0 && !cell.released) {
183
+ cell.released = true;
184
+ activeCells.delete(cell);
185
+ hooks.releaseState(cell.captured);
186
+ }
187
+ }
188
+
189
+ function safeUnpin(cell, errors = []) {
190
+ try {
191
+ unpin(cell);
192
+ } catch (err) {
193
+ errors.push(err);
194
+ }
195
+ }
196
+
197
+ function dropUndeliveredHandles(entries, errors) {
198
+ for (const { handle, cell } of entries) {
199
+ if (handles.delete(handle)) {
200
+ safeUnpin(cell, errors);
201
+ }
202
+ }
203
+ }
204
+
205
+ function throwCleanupFailed(errors) {
206
+ const aggregate = new AggregateError(errors, 'KINETIX_RUNTIME_CLEANUP_FAILED');
207
+ aggregate.code = 'KINETIX_RUNTIME_CLEANUP_FAILED';
208
+ aggregate.cause = errors[0];
209
+ throw aggregate;
210
+ }
211
+
212
+ function createHandle(cell) {
213
+ const handle = Object.freeze({ frame: cell.frame });
214
+ pin(cell);
215
+ handles.set(handle, cell);
216
+ return handle;
217
+ }
218
+
219
+ const runtimeIdentity = Object.freeze(structuredClone(identity));
220
+ const sessionConfigDigest = kinetixSessionConfigDigest(sessionConfigBytes);
221
+ const reportState = strongReport
222
+ ? createKinetixStateReporter(kinetixRuntimeIdentityDigest(runtimeIdentity), sessionConfigDigest) : undefined;
223
+
224
+ function strongWireChecksum(cell) {
225
+ const commitment = hooks.runReadOnly(() => hooks.stateCommitment(cell.captured));
226
+ return reportState(cell.frame, commitment);
227
+ }
228
+
229
+ // Initial capture for frame 0 / starting state
230
+ let initialCaptured;
231
+ try {
232
+ initialCaptured = hooks.captureState();
233
+ } catch (cause) {
234
+ if (cause instanceof Error) throw cause;
235
+ fail('KINETIX_RUNTIME_INIT_FAILED', cause);
236
+ }
237
+ let currentCell;
238
+ try {
239
+ const initialFrame = hooks.frameOf(initialCaptured);
240
+ if (!Number.isSafeInteger(initialFrame) || initialFrame < 0) {
241
+ fail('KINETIX_RUNTIME_FRAME_MISMATCH');
242
+ }
243
+ const initialRev = hooks.readLiveRevision();
244
+ if (typeof initialRev !== 'bigint') {
245
+ fail('KINETIX_RUNTIME_ADAPTER_INVALID: readLiveRevision must return bigint');
246
+ }
247
+ currentCell = createCell(initialCaptured, initialFrame, initialRev);
248
+ pin(currentCell); // internal current pin
249
+ activeCells.add(currentCell);
250
+ } catch (initErr) {
251
+ const releaseErrors = [];
252
+ try {
253
+ hooks.releaseState(initialCaptured);
254
+ } catch (relErr) {
255
+ releaseErrors.push(relErr);
256
+ }
257
+ if (releaseErrors.length > 0) {
258
+ const agg = new AggregateError(
259
+ [initErr, ...releaseErrors],
260
+ 'KINETIX_RUNTIME_INIT_FAILED',
261
+ );
262
+ agg.code = 'KINETIX_RUNTIME_INIT_FAILED';
263
+ agg.cause = initErr;
264
+ throw agg;
265
+ }
266
+ if (initErr instanceof Error) throw initErr;
267
+ fail('KINETIX_RUNTIME_INIT_FAILED', initErr);
268
+ }
269
+
270
+ function adopt(newCell) {
271
+ pin(newCell);
272
+ const oldCell = currentCell;
273
+ currentCell = newCell;
274
+ if (oldCell !== null) {
275
+ unpin(oldCell);
276
+ }
277
+ }
278
+
279
+ function lookupCell(checkpoint) {
280
+ assertNotDisposed();
281
+ assertNotFailed();
282
+ const cell = isPlainObject(checkpoint) ? handles.get(checkpoint) : undefined;
283
+ if (!cell) fail('KINETIX_RUNTIME_CHECKPOINT_INVALID');
284
+ return cell;
285
+ }
286
+
287
+ const capabilities = Object.freeze({
288
+ commands: decodeCommand !== undefined,
289
+ ownership: 'lease',
290
+ });
291
+
292
+ const runtime = {
293
+ identity: runtimeIdentity,
294
+ capabilities,
295
+ checkpointBudget: budget,
296
+ sessionConfigDigest,
297
+ get currentFrame() {
298
+ assertNotDisposed();
299
+ assertNotFailed();
300
+ return currentCell.frame;
301
+ },
302
+ sessionConfigBytes() {
303
+ assertNotDisposed();
304
+ assertNotFailed();
305
+ return copyUint8Array(sessionConfigBytes);
306
+ },
307
+ capture() {
308
+ assertNotDisposed();
309
+ assertNotFailed();
310
+ const liveRev = hooks.readLiveRevision();
311
+ if (typeof liveRev !== 'bigint') {
312
+ fail('KINETIX_RUNTIME_ADAPTER_INVALID: readLiveRevision must return bigint');
313
+ }
314
+ if (liveRev !== currentCell.revision) {
315
+ fail('KINETIX_RUNTIME_LIVE_MUTATION');
316
+ }
317
+ return createHandle(currentCell);
318
+ },
319
+ advance(requests) {
320
+ assertNotDisposed();
321
+ assertNotFailed();
322
+ const decodedRequests = hooks.runReadOnly(() => {
323
+ if (!Array.isArray(requests)) {
324
+ fail('KINETIX_RUNTIME_BATCH_INVALID');
325
+ }
326
+ if (requests.length > budget.maxAdvanceBatch) {
327
+ fail(`KINETIX_RUNTIME_BATCH_INVALID: batch size ${requests.length} exceeds maxAdvanceBatch ${budget.maxAdvanceBatch}`);
328
+ }
329
+ if (activeCells.size + requests.length > budget.maxLiveCheckpoints) {
330
+ fail(`KINETIX_RUNTIME_CAPACITY_INSUFFICIENT: batch size ${requests.length} exceeds remaining capacity (${activeCells.size}/${budget.maxLiveCheckpoints})`);
331
+ }
332
+ const liveRev = hooks.readLiveRevision();
333
+ if (typeof liveRev !== 'bigint') {
334
+ fail('KINETIX_RUNTIME_ADAPTER_INVALID: readLiveRevision must return bigint');
335
+ }
336
+ if (liveRev !== currentCell.revision) {
337
+ fail('KINETIX_RUNTIME_LIVE_MUTATION');
338
+ }
339
+
340
+ let expected = currentCell.frame + 1;
341
+ return requests.map((request) => {
342
+ if (!isPlainObject(request)) fail('KINETIX_RUNTIME_FRAME_MISMATCH');
343
+ const { frame, inputs, commands: rawCommands, checksum } = request;
344
+ if (
345
+ frame !== expected
346
+ || !Array.isArray(inputs)
347
+ || !Array.isArray(rawCommands)
348
+ || typeof checksum !== 'boolean'
349
+ ) {
350
+ fail('KINETIX_RUNTIME_FRAME_MISMATCH');
351
+ }
352
+ if (decodeCommand === undefined && rawCommands.length !== 0) {
353
+ fail('KINETIX_RUNTIME_COMMAND_INVALID');
354
+ }
355
+ expected += 1;
356
+ let commands = [];
357
+ if (decodeCommand !== undefined) {
358
+ try {
359
+ commands = rawCommands.map((command) => decodeCommand(command));
360
+ } catch (cause) {
361
+ fail('KINETIX_RUNTIME_COMMAND_INVALID', cause);
362
+ }
363
+ }
364
+ return { request: { frame, inputs, commands: rawCommands, checksum }, commands };
365
+ });
366
+ });
367
+
368
+ if (decodedRequests.length === 0) return [];
369
+ hooks.preflightAdvance?.(decodedRequests.length);
370
+
371
+ const startingCell = currentCell;
372
+ pin(startingCell); // batch rollback pin
373
+
374
+ const provisionalCandidates = [];
375
+ const createdHandles = [];
376
+ const results = [];
377
+
378
+ try {
379
+ for (const { request, commands } of decodedRequests) {
380
+ step(request.inputs, commands);
381
+ const captured = hooks.captureState();
382
+
383
+ // Immediately register candidate before any fallible validation
384
+ const candidate = createCell(captured, request.frame, null);
385
+ pin(candidate); // pinned provisional candidate
386
+ provisionalCandidates.push(candidate);
387
+ activeCells.add(candidate);
388
+
389
+ const rev = hooks.readLiveRevision();
390
+ if (typeof rev !== 'bigint') {
391
+ fail('KINETIX_RUNTIME_ADAPTER_INVALID: readLiveRevision must return bigint');
392
+ }
393
+ candidate.revision = rev;
394
+
395
+ const frame = hooks.frameOf(candidate.captured);
396
+ if (frame !== request.frame) {
397
+ fail('KINETIX_RUNTIME_FRAME_MISMATCH');
398
+ }
399
+ candidate.frame = frame;
400
+
401
+ adopt(candidate);
402
+
403
+ let checksumStr;
404
+ if (request.checksum) {
405
+ let csBytes;
406
+ try {
407
+ csBytes = hooks.checksumStateBytes(candidate.captured);
408
+ } catch (cause) {
409
+ fail('KINETIX_RUNTIME_CHECKPOINT_INVALID', cause);
410
+ }
411
+ if (!isUint8Array(csBytes)) fail('KINETIX_RUNTIME_CHECKPOINT_INVALID');
412
+ candidate.checksumHex = sha256Hex(csBytes);
413
+ checksumStr = candidate.checksumHex;
414
+ }
415
+
416
+ const handle = createHandle(candidate);
417
+ createdHandles.push({ handle, cell: candidate });
418
+ results.push({
419
+ frame: request.frame,
420
+ checkpoint: handle,
421
+ ...(checksumStr !== undefined ? { checksum: checksumStr } : {}),
422
+ });
423
+ }
424
+ } catch (advanceError) {
425
+ const cleanupErrors = [];
426
+
427
+ dropUndeliveredHandles(createdHandles, cleanupErrors);
428
+
429
+ const rollbackErrors = [];
430
+ try {
431
+ if (startingCell.released) {
432
+ fail('KINETIX_RUNTIME_CHECKPOINT_INVALID: attempt to restore released cell');
433
+ }
434
+ hooks.restoreState(startingCell.captured);
435
+ const rbRev = hooks.readLiveRevision();
436
+ if (typeof rbRev !== 'bigint') {
437
+ fail('KINETIX_RUNTIME_ADAPTER_INVALID: readLiveRevision must return bigint');
438
+ }
439
+ startingCell.revision = rbRev;
440
+ const rbFrame = hooks.frameOf(startingCell.captured);
441
+ if (rbFrame !== startingCell.frame) {
442
+ fail('KINETIX_RUNTIME_FRAME_MISMATCH');
443
+ }
444
+ adopt(startingCell);
445
+ } catch (rbErr) {
446
+ rollbackErrors.push(rbErr);
447
+ runtimeFailed = true;
448
+ }
449
+
450
+ // Clean every candidate registered during this batch
451
+ for (const candidate of provisionalCandidates) {
452
+ safeUnpin(candidate, cleanupErrors);
453
+ }
454
+
455
+ // Release batch rollback pin on startingCell
456
+ safeUnpin(startingCell, cleanupErrors);
457
+
458
+ if (rollbackErrors.length > 0) {
459
+ const aggregate = new AggregateError(
460
+ [advanceError, ...rollbackErrors, ...cleanupErrors],
461
+ 'KINETIX_RUNTIME_ROLLBACK_FAILED',
462
+ );
463
+ aggregate.code = 'KINETIX_RUNTIME_ROLLBACK_FAILED';
464
+ aggregate.cause = advanceError;
465
+ aggregate.rollbackError = rollbackErrors[0];
466
+ throw aggregate;
467
+ }
468
+
469
+ if (cleanupErrors.length > 0) {
470
+ const aggregate = new AggregateError(
471
+ [advanceError, ...cleanupErrors],
472
+ 'KINETIX_RUNTIME_CLEANUP_FAILED',
473
+ );
474
+ aggregate.code = 'KINETIX_RUNTIME_CLEANUP_FAILED';
475
+ aggregate.cause = advanceError;
476
+ throw aggregate;
477
+ }
478
+
479
+ throw advanceError;
480
+ }
481
+
482
+ // The batch is committed when the try block ends. Later cleanup must not restore.
483
+ const finishErrors = [];
484
+ for (const candidate of provisionalCandidates) {
485
+ safeUnpin(candidate, finishErrors);
486
+ }
487
+ safeUnpin(startingCell, finishErrors);
488
+ if (finishErrors.length > 0) {
489
+ dropUndeliveredHandles(createdHandles, finishErrors);
490
+ if (currentCell === null || currentCell.released) {
491
+ runtimeFailed = true;
492
+ }
493
+ throwCleanupFailed(finishErrors);
494
+ }
495
+
496
+ return results;
497
+ },
498
+ restore(checkpoint) {
499
+ assertNotDisposed();
500
+ assertNotFailed();
501
+ const cell = lookupCell(checkpoint);
502
+ if (cell === currentCell) {
503
+ const liveRev = hooks.readLiveRevision();
504
+ if (typeof liveRev !== 'bigint') {
505
+ fail('KINETIX_RUNTIME_ADAPTER_INVALID: readLiveRevision must return bigint');
506
+ }
507
+ if (liveRev !== currentCell.revision) {
508
+ fail('KINETIX_RUNTIME_LIVE_MUTATION');
509
+ }
510
+ return;
511
+ }
512
+
513
+ try {
514
+ hooks.restoreState(cell.captured);
515
+ const liveRev = hooks.readLiveRevision();
516
+ if (typeof liveRev !== 'bigint') {
517
+ fail('KINETIX_RUNTIME_ADAPTER_INVALID: readLiveRevision must return bigint');
518
+ }
519
+ cell.revision = liveRev;
520
+ const restoredFrame = hooks.frameOf(cell.captured);
521
+ if (restoredFrame !== cell.frame) {
522
+ fail('KINETIX_RUNTIME_FRAME_MISMATCH');
523
+ }
524
+ adopt(cell);
525
+ } catch (err) {
526
+ runtimeFailed = true;
527
+ throw err;
528
+ }
529
+ },
530
+ verifyCurrentState(checkpoint) {
531
+ assertNotDisposed();
532
+ assertNotFailed();
533
+ const cell = isPlainObject(checkpoint) ? handles.get(checkpoint) : undefined;
534
+ if (!cell || cell !== currentCell) {
535
+ fail('KINETIX_RUNTIME_CHECKPOINT_NOT_CURRENT');
536
+ }
537
+ const liveRev = hooks.readLiveRevision();
538
+ if (typeof liveRev !== 'bigint') {
539
+ fail('KINETIX_RUNTIME_ADAPTER_INVALID: readLiveRevision must return bigint');
540
+ }
541
+ if (liveRev !== currentCell.revision) {
542
+ fail('KINETIX_RUNTIME_LIVE_MUTATION');
543
+ }
544
+ },
545
+ inspect(checkpoint) {
546
+ assertNotDisposed();
547
+ assertNotFailed();
548
+ const cell = lookupCell(checkpoint);
549
+ return hooks.inspectState(cell.captured);
550
+ },
551
+ serializeCheckpoint(checkpoint) {
552
+ assertNotDisposed();
553
+ assertNotFailed();
554
+ const cell = lookupCell(checkpoint);
555
+ if (cell.serialized === null) {
556
+ let rawBytes;
557
+ try {
558
+ rawBytes = hooks.serializeState(cell.captured);
559
+ } catch (cause) {
560
+ fail('KINETIX_RUNTIME_CHECKPOINT_INVALID', cause);
561
+ }
562
+ if (!isUint8Array(rawBytes)) fail('KINETIX_RUNTIME_CHECKPOINT_INVALID');
563
+ cell.serialized = copyUint8Array(rawBytes);
564
+ }
565
+ return copyUint8Array(cell.serialized);
566
+ },
567
+ hydrateCheckpoint(frame, bytes) {
568
+ assertNotDisposed();
569
+ assertNotFailed();
570
+ if (!isUint8Array(bytes)) fail('KINETIX_RUNTIME_CHECKPOINT_INVALID');
571
+ if (!Number.isSafeInteger(frame) || frame < 0) {
572
+ fail('KINETIX_RUNTIME_FRAME_MISMATCH');
573
+ }
574
+ if (maxSnapshotBytes !== undefined && uint8ArrayByteLength(bytes) > maxSnapshotBytes) {
575
+ fail('KINETIX_RUNTIME_CHECKPOINT_INVALID: snapshot byte limit');
576
+ }
577
+ try { hooks.runReadOnly(() => hooks.validateSnapshot?.(frame, bytes)); }
578
+ catch (cause) { fail('KINETIX_RUNTIME_CHECKPOINT_INVALID', cause); }
579
+
580
+ // Preflight scratch and total capacity BEFORE calling hydrateState
581
+ if (budget.importScratch < 1 || activeCells.size + 1 > budget.maxLiveCheckpoints) {
582
+ fail('KINETIX_RUNTIME_CAPACITY_INSUFFICIENT');
583
+ }
584
+
585
+ const normalized = copyUint8Array(bytes);
586
+ let candidate;
587
+ try {
588
+ candidate = hooks.hydrateState(frame, normalized);
589
+ } catch (error) {
590
+ fail('KINETIX_RUNTIME_CHECKPOINT_INVALID', error);
591
+ }
592
+
593
+ const cleanupReleaseErrors = [];
594
+ let released = false;
595
+ const releaseCandidate = () => {
596
+ if (!released) {
597
+ released = true;
598
+ try {
599
+ hooks.releaseState(candidate);
600
+ } catch (relErr) {
601
+ cleanupReleaseErrors.push(relErr);
602
+ }
603
+ }
604
+ };
605
+
606
+ try {
607
+ let canonicalBytes;
608
+ try {
609
+ canonicalBytes = hooks.serializeState(candidate);
610
+ } catch (serErr) {
611
+ releaseCandidate();
612
+ throw serErr;
613
+ }
614
+ if (!isUint8Array(canonicalBytes) || !equalBytes(canonicalBytes, normalized)) {
615
+ releaseCandidate();
616
+ fail('KINETIX_RUNTIME_CHECKPOINT_INVALID');
617
+ }
618
+
619
+ let candidateFrame;
620
+ try {
621
+ candidateFrame = hooks.frameOf(candidate);
622
+ } catch (frErr) {
623
+ releaseCandidate();
624
+ throw frErr;
625
+ }
626
+ if (candidateFrame !== frame) {
627
+ releaseCandidate();
628
+ fail('KINETIX_RUNTIME_CHECKPOINT_INVALID');
629
+ }
630
+
631
+ const liveRev = hooks.readLiveRevision();
632
+ if (typeof liveRev !== 'bigint') {
633
+ releaseCandidate();
634
+ fail('KINETIX_RUNTIME_ADAPTER_INVALID: readLiveRevision must return bigint');
635
+ }
636
+
637
+ const cell = createCell(candidate, frame, liveRev);
638
+ cell.serialized = copyUint8Array(canonicalBytes);
639
+ cell.wireChecksumHex = strongReport ? strongWireChecksum(cell) : sha256Hex(cell.serialized);
640
+ activeCells.add(cell);
641
+ return createHandle(cell);
642
+ } catch (err) {
643
+ releaseCandidate();
644
+ if (cleanupReleaseErrors.length > 0) {
645
+ const aggregate = new AggregateError(
646
+ [err, ...cleanupReleaseErrors],
647
+ 'KINETIX_RUNTIME_CLEANUP_FAILED',
648
+ );
649
+ aggregate.code = 'KINETIX_RUNTIME_CLEANUP_FAILED';
650
+ aggregate.cause = err;
651
+ throw aggregate;
652
+ }
653
+ if (isCheckpointFailure(err) || err?.message?.startsWith('KINETIX_RUNTIME_FRAME_MISMATCH')) {
654
+ throw err;
655
+ }
656
+ fail('KINETIX_RUNTIME_CHECKPOINT_INVALID', err);
657
+ }
658
+ },
659
+ checksum(checkpoint) {
660
+ assertNotDisposed();
661
+ assertNotFailed();
662
+ const cell = lookupCell(checkpoint);
663
+ if (cell.checksumHex === null) {
664
+ let csBytes;
665
+ try {
666
+ csBytes = hooks.checksumStateBytes(cell.captured);
667
+ } catch (cause) {
668
+ fail('KINETIX_RUNTIME_CHECKPOINT_INVALID', cause);
669
+ }
670
+ if (!isUint8Array(csBytes)) fail('KINETIX_RUNTIME_CHECKPOINT_INVALID');
671
+ cell.checksumHex = sha256Hex(csBytes);
672
+ }
673
+ return cell.checksumHex;
674
+ },
675
+ wireChecksum(checkpoint) {
676
+ assertNotDisposed();
677
+ assertNotFailed();
678
+ const cell = lookupCell(checkpoint);
679
+ if (cell.wireChecksumHex === null) {
680
+ if (strongReport) {
681
+ cell.wireChecksumHex = strongWireChecksum(cell);
682
+ return cell.wireChecksumHex;
683
+ }
684
+ if (cell.serialized === null) {
685
+ let rawBytes;
686
+ try {
687
+ rawBytes = hooks.serializeState(cell.captured);
688
+ } catch (cause) {
689
+ fail('KINETIX_RUNTIME_CHECKPOINT_INVALID', cause);
690
+ }
691
+ if (!isUint8Array(rawBytes)) fail('KINETIX_RUNTIME_CHECKPOINT_INVALID');
692
+ cell.serialized = copyUint8Array(rawBytes);
693
+ }
694
+ cell.wireChecksumHex = sha256Hex(cell.serialized);
695
+ }
696
+ return cell.wireChecksumHex;
697
+ },
698
+ release(checkpoint) {
699
+ if (disposed) return;
700
+ const cell = isPlainObject(checkpoint) ? handles.get(checkpoint) : undefined;
701
+ if (cell !== undefined) {
702
+ handles.delete(checkpoint);
703
+ unpin(cell);
704
+ }
705
+ },
706
+ };
707
+
708
+ if (projectState !== undefined) {
709
+ runtime.project = (checkpoint, previous) => {
710
+ assertNotDisposed();
711
+ assertNotFailed();
712
+ const cell = lookupCell(checkpoint);
713
+ let previousCaptured = null;
714
+ if (previous !== undefined && previous !== checkpoint) {
715
+ const prevCell = lookupCell(previous);
716
+ previousCaptured = prevCell.captured;
717
+ }
718
+ try {
719
+ return projectState(cell.captured, previousCaptured);
720
+ } catch (cause) {
721
+ fail('KINETIX_RUNTIME_PROJECTION_INVALID', cause);
722
+ }
723
+ };
724
+ }
725
+
726
+ for (const [name, descriptor] of Object.entries(Object.getOwnPropertyDescriptors(runtime))) {
727
+ if (typeof descriptor.value !== 'function') continue;
728
+ const work = descriptor.value;
729
+ runtime[name] = (...args) => {
730
+ try {
731
+ return withOperation(name, () => work(...args));
732
+ } catch (cause) {
733
+ if (cause instanceof Error) throw cause;
734
+ fail(`KINETIX_RUNTIME_OPERATION_FAILED: ${name}`, cause);
735
+ }
736
+ };
737
+ }
738
+
739
+ function disposeOwnedState() {
740
+ if (disposePromise !== null) return disposePromise;
741
+ disposePromise = (async () => {
742
+ if (disposed) return;
743
+ disposed = true;
744
+ const errors = [];
745
+
746
+ for (const [handle, cell] of handles.entries()) {
747
+ handles.delete(handle);
748
+ safeUnpin(cell, errors);
749
+ }
750
+
751
+ if (currentCell !== null) {
752
+ const cell = currentCell;
753
+ currentCell = null;
754
+ safeUnpin(cell, errors);
755
+ }
756
+
757
+ for (const cell of Array.from(activeCells)) {
758
+ if (!cell.released) {
759
+ cell.released = true;
760
+ activeCells.delete(cell);
761
+ try {
762
+ hooks.releaseState(cell.captured);
763
+ } catch (err) {
764
+ errors.push(err);
765
+ }
766
+ }
767
+ }
768
+
769
+ if (errors.length > 0) {
770
+ if (errors.length === 1) throw errors[0];
771
+ const aggregate = new AggregateError(errors, 'KINETIX_RUNTIME_DISPOSE_FAILED');
772
+ aggregate.code = 'KINETIX_RUNTIME_DISPOSE_FAILED';
773
+ throw aggregate;
774
+ }
775
+ })();
776
+ return disposePromise;
777
+ }
778
+
779
+ function dispose() {
780
+ if (disposePromise !== null) return disposePromise;
781
+ return withOperation('dispose', disposeOwnedState);
782
+ }
783
+
784
+ return Object.freeze({
785
+ runtime: Object.freeze(runtime),
786
+ dispose,
787
+ });
788
+ }