@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.
- package/README.md +133 -0
- package/core/deterministic-math.d.ts +75 -1
- package/core/native/instance_ledger.hpp +1095 -0
- package/core/native/legacy_checkpoint_ids.hpp +107 -0
- package/core/native/physics/abi.cpp +39 -211
- package/core/native/physics/abi.hpp +2 -6
- package/core/native/physics/articulation.cpp +1434 -0
- package/core/native/physics/articulation.hpp +279 -0
- package/core/native/physics/buoyancy.cpp +1588 -0
- package/core/native/physics/buoyancy.hpp +254 -0
- package/core/native/physics/capsule_triangle.cpp +168 -0
- package/core/native/physics/capsule_triangle.hpp +40 -0
- package/core/native/physics/capsule_triangle_native.inc +291 -0
- package/core/native/physics/certified_sum.hpp +218 -0
- package/core/native/physics/character.cpp +638 -0
- package/core/native/physics/character.hpp +169 -0
- package/core/native/physics/checkpoint_schema.hpp +379 -0
- package/core/native/physics/cloth.cpp +1401 -0
- package/core/native/physics/cloth.hpp +357 -0
- package/core/native/physics/constraint_rows.cpp +363 -2
- package/core/native/physics/constraint_rows.hpp +72 -1
- package/core/native/physics/deformable_query.cpp +1630 -0
- package/core/native/physics/deformable_query.hpp +578 -0
- package/core/native/physics/distance.cpp +1166 -0
- package/core/native/physics/distance.hpp +151 -0
- package/core/native/physics/exact/exact-applied.cpp +101 -0
- package/core/native/physics/exact/exact-applied.hpp +112 -0
- package/core/native/physics/exact/exact-dyadic.cpp +691 -0
- package/core/native/physics/exact/exact-dyadic.hpp +58 -0
- package/core/native/physics/exact/exact-operation.hpp +51 -0
- package/core/native/physics/exact/word-core.hpp +262 -0
- package/core/native/physics/extended_abi.cpp +1242 -0
- package/core/native/physics/extended_abi.hpp +355 -0
- package/core/native/physics/extended_abi_characters.cpp +261 -0
- package/core/native/physics/extended_abi_characters.hpp +434 -0
- package/core/native/physics/extended_abi_core_features.cpp +713 -0
- package/core/native/physics/extended_abi_core_features.hpp +226 -0
- package/core/native/physics/extended_abi_deformables.cpp +448 -0
- package/core/native/physics/extended_abi_deformables.hpp +867 -0
- package/core/native/physics/extended_abi_vehicles.cpp +411 -0
- package/core/native/physics/extended_abi_vehicles.hpp +222 -0
- package/core/native/physics/fracture.cpp +1981 -0
- package/core/native/physics/fracture.hpp +460 -0
- package/core/native/physics/gear.cpp +563 -0
- package/core/native/physics/gear.hpp +158 -0
- package/core/native/physics/geometry_memory.hpp +476 -0
- package/core/native/physics/geometry_store.cpp +547 -0
- package/core/native/physics/geometry_store.hpp +134 -0
- package/core/native/physics/lag_compensation.cpp +1626 -0
- package/core/native/physics/lag_compensation.hpp +405 -0
- package/core/native/physics/mass_properties.cpp +449 -0
- package/core/native/physics/mass_properties.hpp +23 -0
- package/core/native/physics/material_pose.hpp +988 -0
- package/core/native/physics/particles.cpp +1065 -0
- package/core/native/physics/particles.hpp +261 -0
- package/core/native/physics/path_joint.cpp +781 -0
- package/core/native/physics/path_joint.hpp +154 -0
- package/core/native/physics/physics.cpp +13043 -1065
- package/core/native/physics/physics.hpp +184 -8
- package/core/native/physics/planar_deformable.cpp +2433 -0
- package/core/native/physics/planar_deformable.hpp +889 -0
- package/core/native/physics/platformer_2d.cpp +1353 -0
- package/core/native/physics/platformer_2d.hpp +340 -0
- package/core/native/physics/powered_ragdoll.cpp +1100 -0
- package/core/native/physics/powered_ragdoll.hpp +279 -0
- package/core/native/physics/powertrain.cpp +586 -0
- package/core/native/physics/powertrain.hpp +180 -0
- package/core/native/physics/pulley.cpp +311 -0
- package/core/native/physics/pulley.hpp +121 -0
- package/core/native/physics/ragdoll.cpp +586 -0
- package/core/native/physics/ragdoll.hpp +221 -0
- package/core/native/physics/rope.cpp +692 -0
- package/core/native/physics/rope.hpp +215 -0
- package/core/native/physics/rounded_convex.cpp +1229 -0
- package/core/native/physics/rounded_convex.hpp +287 -0
- package/core/native/physics/sdf_geometry.cpp +1821 -0
- package/core/native/physics/sdf_geometry.hpp +474 -0
- package/core/native/physics/soft_body.cpp +1449 -0
- package/core/native/physics/soft_body.hpp +328 -0
- package/core/native/physics/soft_collision.cpp +1862 -0
- package/core/native/physics/soft_collision.hpp +561 -0
- package/core/native/physics/surface_velocity.cpp +366 -0
- package/core/native/physics/surface_velocity.hpp +243 -0
- package/core/native/physics/topology.cpp +357 -39
- package/core/native/physics/topology.hpp +97 -6
- package/core/native/physics/tracks.cpp +1179 -0
- package/core/native/physics/tracks.hpp +404 -0
- package/core/native/physics/vehicle.cpp +629 -0
- package/core/native/physics/vehicle.hpp +191 -0
- package/core/native/physics/voxel_cooking.cpp +1929 -0
- package/core/native/physics/voxel_cooking.hpp +395 -0
- package/core/native/physics/world.cpp +5290 -854
- package/core/native/physics/world.hpp +796 -24
- package/core/native/physics/world_abi.cpp +3840 -306
- package/core/native/physics/world_abi.hpp +530 -10
- package/core/native/physics/world_digest_visitor.hpp +277 -0
- package/core/native/physics/world_strong_digest.hpp +557 -0
- package/core/native/physics2d/ccd.cpp +1302 -0
- package/core/native/physics2d/ccd.hpp +141 -0
- package/core/native/physics2d/checkpoint_memory.hpp +349 -0
- package/core/native/physics2d/clock_primitives.hpp +138 -0
- package/core/native/physics2d/commands.cpp +174 -28
- package/core/native/physics2d/commands.hpp +3 -1
- package/core/native/physics2d/compound.cpp +581 -0
- package/core/native/physics2d/compound.hpp +248 -0
- package/core/native/physics2d/contour.cpp +325 -0
- package/core/native/physics2d/contour.hpp +143 -0
- package/core/native/physics2d/frame_scope.hpp +124 -0
- package/core/native/physics2d/joint_phases.hpp +93 -0
- package/core/native/physics2d/joints.cpp +587 -150
- package/core/native/physics2d/joints.hpp +5 -0
- package/core/native/physics2d/memory_private.hpp +125 -0
- package/core/native/physics2d/owned_abi.cpp +1354 -879
- package/core/native/physics2d/owned_abi.hpp +190 -52
- package/core/native/physics2d/phase_test_hooks.hpp +17 -0
- package/core/native/physics2d/physics2d.cpp +2073 -120
- package/core/native/physics2d/physics2d.hpp +154 -2
- package/core/native/physics2d/queries.cpp +1055 -79
- package/core/native/physics2d/queries.hpp +71 -5
- package/core/native/physics2d/world.cpp +1313 -353
- package/core/native/physics2d/world.hpp +124 -13
- package/core/native/sha256/sha256_compress_unsigned.hpp +68 -0
- package/core/native/sha256/sha256_fips_constants.hpp +24 -0
- package/core/native/sha256/sha256_length.hpp +56 -0
- package/core/native/sha256/sha256_memo.hpp +48 -0
- package/core/native/sha256/sha256_sink.hpp +234 -0
- package/core/physics2d-owned.d.ts +466 -126
- package/core/runtime.d.ts +192 -19
- package/core/scripts/build-native.mjs +34 -0
- package/core/scripts/build-wasm-physics.mjs +272 -23
- package/core/scripts/extended-physics-profile.mjs +634 -0
- package/core/scripts/generate-physics2d-vectors.mjs +59 -0
- package/core/src/kinetix-deterministic-math.d.ts +2 -0
- package/core/src/kinetix-deterministic-math.mjs +513 -27
- package/core/src/kinetix-owned-runtime-adapter.mjs +788 -0
- package/core/src/kinetix-runtime-adapter-utils.mjs +54 -6
- package/core/src/kinetix-runtime-contract.mjs +55 -9
- package/core/src/kinetix-runtime-identity.mjs +12 -1
- package/core/src/kinetix-session-config.mjs +7 -2
- package/core/src/kinetix-state-report.mjs +36 -0
- package/core/src/physics/abi-constants.mjs +5 -4
- package/core/src/physics/articulation.d.mts +194 -0
- package/core/src/physics/articulation.mjs +1141 -0
- package/core/src/physics/buoyancy.d.mts +143 -0
- package/core/src/physics/buoyancy.mjs +268 -0
- package/core/src/physics/cloth.d.mts +314 -0
- package/core/src/physics/cloth.mjs +1156 -0
- package/core/src/physics/deformable-query.d.mts +217 -0
- package/core/src/physics/deformable-query.mjs +1412 -0
- package/core/src/physics/distance.d.mts +160 -0
- package/core/src/physics/distance.mjs +175 -0
- package/core/src/physics/extended-wasm-instance.d.mts +5 -0
- package/core/src/physics/extended-wasm-instance.mjs +48 -0
- package/core/src/physics/feature-api.d.mts +74 -0
- package/core/src/physics/feature-api.mjs +98 -0
- package/core/src/physics/fracture.d.mts +133 -0
- package/core/src/physics/fracture.mjs +1324 -0
- package/core/src/physics/gear.d.mts +585 -0
- package/core/src/physics/gear.mjs +955 -0
- package/core/src/physics/lag-compensation.d.mts +101 -0
- package/core/src/physics/lag-compensation.mjs +1100 -0
- package/core/src/physics/mass-properties.d.mts +114 -0
- package/core/src/physics/mass-properties.mjs +137 -468
- package/core/src/physics/particles.d.mts +177 -0
- package/core/src/physics/particles.mjs +758 -0
- package/core/src/physics/path-joint.d.mts +171 -0
- package/core/src/physics/path-joint.mjs +1231 -0
- package/core/src/physics/physics-extended-wasm-module.generated.mjs +13 -0
- package/core/src/physics/physics-profile-wasm-module.generated.mjs +48 -0
- package/core/src/physics/physics-wasm-module.generated.mjs +34 -5
- package/core/src/physics/powered-ragdoll.d.mts +188 -0
- package/core/src/physics/powered-ragdoll.mjs +1043 -0
- package/core/src/physics/powertrain.d.mts +260 -0
- package/core/src/physics/powertrain.mjs +621 -0
- package/core/src/physics/pulley.d.mts +98 -0
- package/core/src/physics/pulley.mjs +686 -0
- package/core/src/physics/ragdoll.d.mts +289 -0
- package/core/src/physics/ragdoll.mjs +1323 -0
- package/core/src/physics/rope.d.mts +241 -0
- package/core/src/physics/rope.mjs +319 -0
- package/core/src/physics/rounded-convex.d.mts +328 -0
- package/core/src/physics/rounded-convex.mjs +1404 -0
- package/core/src/physics/sdf-geometry.d.mts +117 -0
- package/core/src/physics/sdf-geometry.mjs +1204 -0
- package/core/src/physics/soft-body.d.mts +144 -0
- package/core/src/physics/soft-body.mjs +1285 -0
- package/core/src/physics/soft-collision.d.mts +651 -0
- package/core/src/physics/soft-collision.mjs +1883 -0
- package/core/src/physics/surface-velocity.d.mts +156 -0
- package/core/src/physics/surface-velocity.mjs +722 -0
- package/core/src/physics/tracks.d.mts +411 -0
- package/core/src/physics/tracks.mjs +1074 -0
- package/core/src/physics/vehicle.d.mts +3 -0
- package/core/src/physics/vehicle.mjs +172 -0
- package/core/src/physics/voxel-cooking.d.mts +219 -0
- package/core/src/physics/voxel-cooking.mjs +1499 -0
- package/core/src/physics/voxel-swept-quad.mjs +110 -0
- package/core/src/physics/wasm-instance.mjs +160 -23
- package/core/src/physics/world.d.mts +62 -0
- package/core/src/physics/world.mjs +1947 -152
- package/core/src/physics2d/body-state.mjs +218 -15
- package/core/src/physics2d/collision.d.mts +52 -0
- package/core/src/physics2d/collision.mjs +234 -0
- package/core/src/physics2d/compound.mjs +454 -0
- package/core/src/physics2d/contour-state.mjs +771 -0
- package/core/src/physics2d/feature-api.d.mts +211 -0
- package/core/src/physics2d/feature-api.mjs +3 -0
- package/core/src/physics2d/fluid-state.mjs +145 -0
- package/core/src/physics2d/joint-state.mjs +1 -1
- package/core/src/physics2d/owned-queries.mjs +3 -1
- package/core/src/physics2d/owned-transport.mjs +268 -122
- package/core/src/physics2d/owned-world.mjs +353 -230
- package/core/src/physics2d/particle-fluid.d.mts +152 -0
- package/core/src/physics2d/particle-fluid.mjs +233 -0
- package/core/src/physics2d/physics2d-wasm-module.generated.mjs +27 -10
- package/core/src/physics2d/query-codec.mjs +61 -13
- package/core/src/physics2d/submerged-geometry.d.mts +51 -0
- package/core/src/physics2d/submerged-geometry.mjs +186 -0
- package/core/src/runtime.mjs +23 -0
- package/core/src/shared/bytes.d.mts +2 -0
- package/core/src/shared/bytes.mjs +33 -0
- package/core/src/shared/sha256.d.mts +3 -0
- package/core/src/shared/sha256.mjs +72 -22
- package/core/units.d.ts +50 -0
- package/core/world.d.ts +253 -5
- package/dist/authority-room.d.ts +9 -0
- package/dist/authority-room.js +45 -16
- package/dist/browser.d.ts +6 -5
- package/dist/browser.js +4 -3
- package/dist/cadence.d.ts +7 -0
- package/dist/cadence.js +19 -0
- package/dist/canonical.d.ts +1 -0
- package/dist/canonical.js +3 -0
- package/dist/checksum-policy.d.ts +10 -0
- package/dist/checksum-policy.js +14 -0
- package/dist/collider-cooking-hull.d.ts +18 -0
- package/dist/collider-cooking-hull.js +248 -0
- package/dist/collider-cooking.d.ts +140 -0
- package/dist/collider-cooking.js +794 -0
- package/dist/compat/v5.d.ts +121 -0
- package/dist/compat/v5.js +479 -0
- package/dist/creator.d.ts +7 -6
- package/dist/creator.js +4 -3
- package/dist/game-byte-section.d.ts +36 -0
- package/dist/game-byte-section.js +350 -0
- package/dist/index.d.ts +13 -6
- package/dist/index.js +9 -4
- package/dist/instant-replay.d.ts +27 -2
- package/dist/instant-replay.js +91 -16
- package/dist/lag-compensation3d.js +11 -10
- package/dist/match-log.d.ts +21 -4
- package/dist/match-log.js +92 -9
- package/dist/math.d.ts +2 -0
- package/dist/math.js +1 -0
- package/dist/modules/character-3d.d.ts +16 -1
- package/dist/modules/character-3d.js +42 -5
- package/dist/modules/combat.js +6 -5
- package/dist/movement3d-revision1.d.ts +2 -0
- package/dist/movement3d-revision1.js +973 -0
- package/dist/movement3d.d.ts +53 -3
- package/dist/movement3d.js +917 -247
- package/dist/networked-client.d.ts +15 -0
- package/dist/networked-client.js +378 -105
- package/dist/node.d.ts +3 -2
- package/dist/node.js +2 -1
- package/dist/physics/2d.d.ts +38 -4
- package/dist/physics/2d.js +57 -36
- package/dist/physics/3d.d.ts +755 -111
- package/dist/physics/3d.js +2355 -158
- package/dist/physics/edit-result-graph.d.ts +36 -0
- package/dist/physics/edit-result-graph.js +141 -0
- package/dist/physics/game-codec3d.d.ts +41 -0
- package/dist/physics/game-codec3d.js +544 -0
- package/dist/physics/installed-checkpoint3d.d.ts +13 -0
- package/dist/physics/installed-checkpoint3d.js +120 -0
- package/dist/physics/installed-runtime3d.d.ts +51 -0
- package/dist/physics/installed-runtime3d.js +372 -0
- package/dist/physics/installed-state-commitment3d.d.ts +2 -0
- package/dist/physics/installed-state-commitment3d.js +24 -0
- package/dist/physics/units.d.ts +38 -0
- package/dist/physics/units.js +56 -0
- package/dist/physics/vehicle-state3d.d.ts +93 -0
- package/dist/physics/vehicle-state3d.js +1191 -0
- package/dist/physics/voxel-body-state.d.ts +61 -0
- package/dist/physics/voxel-body-state.js +410 -0
- package/dist/r3f-render-adapter.d.ts +59 -1
- package/dist/r3f-render-adapter.js +137 -2
- package/dist/replay.d.ts +13 -0
- package/dist/replay.js +95 -70
- package/dist/run-room-transport.js +5 -1
- package/dist/runner.d.ts +17 -2
- package/dist/runner.js +472 -84
- package/dist/runtime-cleanup.d.ts +54 -0
- package/dist/runtime-cleanup.js +266 -0
- package/dist/runtime-lease.d.ts +11 -1
- package/dist/runtime-lease.js +23 -10
- package/dist/runtime-room-metadata.d.ts +2 -8
- package/dist/runtime-room-metadata.js +11 -14
- package/dist/runtime-session.d.ts +8 -0
- package/dist/runtime-session.js +222 -58
- package/dist/schema-codegen.js +3 -0
- package/dist/schema-dsl-binary.d.ts +10 -4
- package/dist/schema-dsl-binary.js +375 -76
- package/dist/scratch-pool.d.ts +32 -0
- package/dist/scratch-pool.js +60 -0
- package/dist/sdk-session.d.ts +1 -0
- package/dist/sdk-session.js +4 -0
- package/dist/secret-config.js +2 -60
- package/dist/secret-crypto.js +70 -36
- package/dist/session-snapshot.d.ts +15 -4
- package/dist/session-snapshot.js +35 -33
- package/dist/session-wire.js +14 -22
- package/dist/sparse-set.d.ts +1 -0
- package/dist/sparse-set.js +22 -0
- package/dist/spatial/spatial-grid-3d.d.ts +41 -0
- package/dist/spatial/spatial-grid-3d.js +115 -0
- package/dist/synctest.d.ts +9 -0
- package/dist/synctest.js +106 -17
- package/dist/testing-network.d.ts +2 -0
- package/dist/testing-network.js +122 -18
- package/dist/testing.d.ts +27 -6
- package/dist/testing.js +297 -56
- package/dist/tools/index.d.ts +3 -2
- package/dist/tools/index.js +3 -2
- package/dist/tools/module-certification.d.ts +6 -0
- package/dist/tools/module-certification.js +26 -2
- package/package.json +18 -5
- package/core/scripts/run-box3d-benchmark.mjs +0 -350
- package/core/src/physics/index.mjs +0 -3169
package/dist/networked-client.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { canonicalStringify, cloneCanonical, encodeUtf8 } from './canonical.js';
|
|
2
|
+
import { sha256Hex } from '@series-inc/rundot-syncplay/core/runtime';
|
|
2
3
|
import { SyncplayError } from './errors.js';
|
|
4
|
+
import { parseSyncplayChecksumPolicy } from './checksum-policy.js';
|
|
3
5
|
import { createApiDiagnostics } from './api-diagnostics.js';
|
|
4
6
|
import { createDeterministicLocalAuthoritySnapshotChunkEnvelope, createDeterministicLocalAuthoritySnapshotCompleteEnvelope, createDeterministicLocalAuthoritySnapshotOfferEnvelope, decodeDeterministicLocalAuthorityWireEnvelope, } from './local-authority-wire.js';
|
|
5
7
|
import { base64ToBytes, bytesToBase64, createSnapshotTransferCollector, createSnapshotTransferFromBytes, decodeDeterministicStateSnapshot, deterministicRoomDescriptorHash, encodeDeterministicStateSnapshot, } from './session-snapshot.js';
|
|
@@ -181,6 +183,15 @@ export async function prepareNetworkedSyncplayTransport(transport, prepare, opti
|
|
|
181
183
|
}
|
|
182
184
|
}
|
|
183
185
|
export function createNetworkedSyncplayClient(options) {
|
|
186
|
+
const checksumPolicy = parseSyncplayChecksumPolicy(options);
|
|
187
|
+
const minimumSnapshotWitnesses = options.minimumSnapshotWitnesses ?? 1;
|
|
188
|
+
if (!Number.isSafeInteger(minimumSnapshotWitnesses) || minimumSnapshotWitnesses < 1) {
|
|
189
|
+
throw new Error('SYNCPLAY_SNAPSHOT_WITNESS_POLICY_INVALID');
|
|
190
|
+
}
|
|
191
|
+
if (options.maxCatchUpFrames !== undefined
|
|
192
|
+
&& (!Number.isSafeInteger(options.maxCatchUpFrames) || options.maxCatchUpFrames < 1)) {
|
|
193
|
+
throw new Error('SYNCPLAY_RUNTIME_SESSION_INVALID');
|
|
194
|
+
}
|
|
184
195
|
const sessionMsgType = options.sessionMsgType ?? DEFAULT_MSG_TYPE;
|
|
185
196
|
const maxPredictionTicks = options.maxPredictionTicks ?? DEFAULT_MAX_PREDICTION;
|
|
186
197
|
if (options.maxSnapshotBytes !== undefined && (!Number.isInteger(options.maxSnapshotBytes) || options.maxSnapshotBytes < 1)) {
|
|
@@ -217,6 +228,9 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
217
228
|
let appliedThrough = -1;
|
|
218
229
|
let rollbacks = 0;
|
|
219
230
|
let maxRollbackDepth = 0;
|
|
231
|
+
let lastResimulationDurationMs = 0;
|
|
232
|
+
let accumulatedResimulationDurationMs = 0;
|
|
233
|
+
const rollbackDepthHistogram = [0, 0, 0, 0, 0];
|
|
220
234
|
let hardToleranceTicks = 0;
|
|
221
235
|
let latestAuthorityTick = -1;
|
|
222
236
|
let caughtUp = false;
|
|
@@ -282,7 +296,7 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
282
296
|
let confirmationQueue;
|
|
283
297
|
let secretFailure;
|
|
284
298
|
const predictionMode = options.predictionMode ?? 'always';
|
|
285
|
-
let sessionGeneration
|
|
299
|
+
let sessionGeneration;
|
|
286
300
|
let runtimeEpoch = 0;
|
|
287
301
|
let commandSequence = 0;
|
|
288
302
|
let droppedConfirmedPresentationFrames = 0;
|
|
@@ -298,6 +312,23 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
298
312
|
let swapGeneration;
|
|
299
313
|
const swapBuffer = [];
|
|
300
314
|
let disposePromise;
|
|
315
|
+
let disposeError;
|
|
316
|
+
let commitBarrier;
|
|
317
|
+
const cleanupJobs = new Set();
|
|
318
|
+
const pendingAcquisitions = new Set();
|
|
319
|
+
const cleanupErrors = [];
|
|
320
|
+
function cleanupFailure(errors) {
|
|
321
|
+
return Object.assign(new AggregateError([...new Set(errors)], 'SYNCPLAY_CLIENT_CLEANUP_FAILED'), { code: 'SYNCPLAY_CLIENT_CLEANUP_FAILED' });
|
|
322
|
+
}
|
|
323
|
+
function trackCleanup(job) {
|
|
324
|
+
cleanupJobs.add(job);
|
|
325
|
+
void job.then(() => cleanupJobs.delete(job), (error) => {
|
|
326
|
+
cleanupJobs.delete(job);
|
|
327
|
+
cleanupErrors.push(error);
|
|
328
|
+
secretFailure = cleanupFailure(cleanupErrors);
|
|
329
|
+
});
|
|
330
|
+
return job;
|
|
331
|
+
}
|
|
301
332
|
let secrets;
|
|
302
333
|
// M5.5 snapshot late-join state: identities from session-start (descriptor)
|
|
303
334
|
// and the session config (schema), the donor upload queue, and the joiner's
|
|
@@ -305,23 +336,36 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
305
336
|
let descriptorHash = '';
|
|
306
337
|
let schemaHash = '';
|
|
307
338
|
const reportedTicks = [];
|
|
339
|
+
const pendingConfirmedChecksums = new Map();
|
|
308
340
|
let lastAutoReportedTick = -1;
|
|
309
341
|
let transferCollector = createSnapshotTransferCollector(snapshotCollectorOptions);
|
|
342
|
+
let snapshotWitnesses;
|
|
310
343
|
const outgoingTransferQueue = [];
|
|
311
344
|
let donorTransferCounter = 0;
|
|
312
345
|
let readyResolve;
|
|
313
346
|
let readyReject;
|
|
314
|
-
|
|
347
|
+
let readyPromise = new Promise((resolve, reject) => {
|
|
315
348
|
readyResolve = resolve;
|
|
316
349
|
readyReject = reject;
|
|
317
350
|
});
|
|
318
351
|
void readyPromise.catch(() => undefined);
|
|
352
|
+
function renewReadiness() {
|
|
353
|
+
if (readyResolve !== undefined)
|
|
354
|
+
return;
|
|
355
|
+
readyPromise = new Promise((resolve, reject) => {
|
|
356
|
+
readyResolve = resolve;
|
|
357
|
+
readyReject = reject;
|
|
358
|
+
});
|
|
359
|
+
void readyPromise.catch(() => undefined);
|
|
360
|
+
}
|
|
319
361
|
function assertActiveClient() {
|
|
320
362
|
if (disposed)
|
|
321
363
|
throw new Error('SYNCPLAY_CLIENT_DISPOSED');
|
|
322
364
|
}
|
|
323
365
|
function requireSession() {
|
|
324
366
|
assertActiveClient();
|
|
367
|
+
if (secretFailure !== undefined)
|
|
368
|
+
throw secretFailure;
|
|
325
369
|
if (session === undefined)
|
|
326
370
|
throw new Error('SYNCPLAY_CLIENT_NOT_READY');
|
|
327
371
|
return session;
|
|
@@ -329,18 +373,62 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
329
373
|
function releaseLease(lease) {
|
|
330
374
|
if (lease === undefined || !lease.owned)
|
|
331
375
|
return undefined;
|
|
332
|
-
const released = lease.release()
|
|
333
|
-
|
|
334
|
-
|
|
376
|
+
const released = lease.release();
|
|
377
|
+
const outcome = lease.releaseOutcome;
|
|
378
|
+
if (outcome.status === 'released')
|
|
379
|
+
return undefined;
|
|
380
|
+
if (outcome.status === 'failed') {
|
|
381
|
+
void released.catch(() => undefined);
|
|
382
|
+
cleanupErrors.push(outcome.error);
|
|
383
|
+
secretFailure = cleanupFailure(cleanupErrors);
|
|
384
|
+
throw outcome.error;
|
|
385
|
+
}
|
|
386
|
+
return trackCleanup(released);
|
|
387
|
+
}
|
|
388
|
+
function cleanupCandidate(lease, candidate) {
|
|
389
|
+
const errors = [];
|
|
390
|
+
try {
|
|
391
|
+
candidate?.dispose();
|
|
392
|
+
}
|
|
393
|
+
catch (error) {
|
|
394
|
+
errors.push(error);
|
|
395
|
+
}
|
|
396
|
+
let pending;
|
|
397
|
+
try {
|
|
398
|
+
pending = releaseLease(lease);
|
|
399
|
+
}
|
|
400
|
+
catch (error) {
|
|
401
|
+
errors.push(error);
|
|
402
|
+
}
|
|
403
|
+
if (pending !== undefined)
|
|
404
|
+
return pending.then(() => {
|
|
405
|
+
if (errors.length > 0)
|
|
406
|
+
throw cleanupFailure(errors);
|
|
407
|
+
}, (error) => { throw cleanupFailure([...errors, error]); });
|
|
408
|
+
if (errors.length > 0)
|
|
409
|
+
throw cleanupFailure(errors);
|
|
410
|
+
return undefined;
|
|
411
|
+
}
|
|
412
|
+
function deactivateGeneration(caught) {
|
|
413
|
+
runtimeEpoch += 1;
|
|
414
|
+
pendingConfirmedChecksums.clear();
|
|
415
|
+
const failure = caught instanceof Error ? caught : new Error(String(caught), { cause: caught });
|
|
416
|
+
secretFailure = session === undefined ? failure
|
|
417
|
+
: new SyncplayError(`SYNCPLAY_RUNTIME_GENERATION_FAILED: ${failure.message}`, {
|
|
418
|
+
code: 'client.generation-failed', cause: caught,
|
|
419
|
+
});
|
|
420
|
+
return secretFailure;
|
|
335
421
|
}
|
|
336
422
|
function failSwap(controller, caught) {
|
|
423
|
+
if (caught instanceof AggregateError && 'code' in caught && caught.code === 'SYNCPLAY_CLIENT_CLEANUP_FAILED') {
|
|
424
|
+
cleanupErrors.push(caught);
|
|
425
|
+
}
|
|
337
426
|
if (swapAbort !== controller)
|
|
338
427
|
return;
|
|
339
428
|
swapAbort = undefined;
|
|
340
429
|
swapGeneration = undefined;
|
|
341
430
|
swapBuffer.length = 0;
|
|
342
|
-
const failure =
|
|
343
|
-
secretFailure = failure;
|
|
431
|
+
const failure = deactivateGeneration(caught);
|
|
344
432
|
readyReject?.(failure);
|
|
345
433
|
readyResolve = undefined;
|
|
346
434
|
readyReject = undefined;
|
|
@@ -381,12 +469,18 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
381
469
|
swapBuffer.push(text);
|
|
382
470
|
return;
|
|
383
471
|
}
|
|
472
|
+
if (secretFailure !== undefined) {
|
|
473
|
+
const decoded = decodeDeterministicSessionMessage(text);
|
|
474
|
+
if (!decoded.ok || decoded.message.kind !== 'session-start')
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
384
477
|
try {
|
|
385
478
|
handleSessionText(text);
|
|
386
479
|
}
|
|
387
480
|
catch (caught) {
|
|
388
|
-
const failure = caught instanceof Error ? caught : new Error(String(caught));
|
|
389
|
-
|
|
481
|
+
const failure = caught instanceof Error ? caught : new Error(String(caught), { cause: caught });
|
|
482
|
+
if (session === undefined)
|
|
483
|
+
secretFailure = failure;
|
|
390
484
|
readyReject?.(failure);
|
|
391
485
|
readyResolve = undefined;
|
|
392
486
|
readyReject = undefined;
|
|
@@ -411,7 +505,7 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
411
505
|
const nextGeneration = message.generation ?? 0;
|
|
412
506
|
// A generation change on a LIVE session is a full session swap: the game
|
|
413
507
|
// must re-prepare its runtime against the new descriptor before rebuild.
|
|
414
|
-
if (
|
|
508
|
+
if (sessionGeneration !== undefined && nextGeneration !== sessionGeneration) {
|
|
415
509
|
beginSessionSwap(message, nextGeneration);
|
|
416
510
|
return;
|
|
417
511
|
}
|
|
@@ -430,10 +524,23 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
430
524
|
}
|
|
431
525
|
}
|
|
432
526
|
function beginSessionSwap(message, nextGeneration) {
|
|
527
|
+
renewReadiness();
|
|
433
528
|
const controller = new AbortController();
|
|
434
529
|
swapAbort = controller;
|
|
435
530
|
swapGeneration = nextGeneration;
|
|
436
|
-
|
|
531
|
+
if (options.prepareSession === undefined) {
|
|
532
|
+
startSessionRuntime(message, nextGeneration, controller);
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
let preparation;
|
|
536
|
+
try {
|
|
537
|
+
preparation = options.prepareSession(immutableDescriptor(message), controller.signal);
|
|
538
|
+
}
|
|
539
|
+
catch (error) {
|
|
540
|
+
failSwap(controller, error);
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
void Promise.resolve(preparation)
|
|
437
544
|
.then(() => {
|
|
438
545
|
if (disposed || swapAbort !== controller)
|
|
439
546
|
return;
|
|
@@ -445,86 +552,147 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
445
552
|
startSessionRuntime(message, message.generation ?? 0);
|
|
446
553
|
}
|
|
447
554
|
function startSessionRuntime(message, nextGeneration, preparedController) {
|
|
555
|
+
renewReadiness();
|
|
448
556
|
const controller = preparedController ?? new AbortController();
|
|
449
|
-
|
|
557
|
+
swapAbort = controller;
|
|
558
|
+
swapGeneration = nextGeneration;
|
|
559
|
+
const finish = () => {
|
|
560
|
+
if (disposed || swapAbort !== controller || controller.signal.aborted)
|
|
561
|
+
return;
|
|
562
|
+
swapAbort = undefined;
|
|
563
|
+
swapGeneration = undefined;
|
|
564
|
+
replaySwapBuffer();
|
|
565
|
+
};
|
|
566
|
+
const accept = (lease) => {
|
|
567
|
+
if (disposed || swapAbort !== controller || controller.signal.aborted) {
|
|
568
|
+
try {
|
|
569
|
+
const pending = cleanupCandidate(lease);
|
|
570
|
+
if (pending !== undefined)
|
|
571
|
+
trackCleanup(pending);
|
|
572
|
+
}
|
|
573
|
+
catch (error) {
|
|
574
|
+
trackCleanup(Promise.reject(error));
|
|
575
|
+
}
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
const pending = installSessionRuntime(message, nextGeneration, lease, controller);
|
|
579
|
+
if (pending === undefined)
|
|
580
|
+
finish();
|
|
581
|
+
else
|
|
582
|
+
void pending.then(finish, (caught) => failSwap(controller, caught));
|
|
583
|
+
};
|
|
450
584
|
try {
|
|
451
|
-
result = invokeRuntimeFactory(options.runtimeFactory, message.runtimeIdentity, message.sessionConfigBytes.slice(), { signal: controller.signal });
|
|
585
|
+
const result = invokeRuntimeFactory(options.runtimeFactory, message.runtimeIdentity, message.sessionConfigBytes.slice(), { signal: controller.signal });
|
|
586
|
+
if (!isRuntimeFactoryPromise(result))
|
|
587
|
+
accept(acquireRuntimeResult(result));
|
|
588
|
+
else {
|
|
589
|
+
const acquisition = acquireRuntime(result).then(accept).catch((caught) => failSwap(controller, caught));
|
|
590
|
+
pendingAcquisitions.add(acquisition);
|
|
591
|
+
void acquisition.then(() => pendingAcquisitions.delete(acquisition));
|
|
592
|
+
}
|
|
452
593
|
}
|
|
453
594
|
catch (caught) {
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
else
|
|
457
|
-
throw caught;
|
|
458
|
-
return;
|
|
595
|
+
failSwap(controller, caught);
|
|
596
|
+
throw caught;
|
|
459
597
|
}
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
598
|
+
}
|
|
599
|
+
function installSessionRuntime(message, nextGeneration, lease, controller) {
|
|
600
|
+
const runtime = lease.runtime;
|
|
601
|
+
if (runtime === activeRuntime)
|
|
602
|
+
throw new Error('SYNCPLAY_RUNTIME_ALREADY_ACTIVE');
|
|
603
|
+
let nextSession;
|
|
604
|
+
try {
|
|
605
|
+
if (canonicalStringify(runtime.identity)
|
|
606
|
+
!== canonicalStringify(message.runtimeIdentity)) {
|
|
607
|
+
throw new SyncplayError('Runtime factory substituted the Kinetix runtime identity', {
|
|
608
|
+
code: 'client.runtime-identity-mismatch',
|
|
609
|
+
});
|
|
464
610
|
}
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
611
|
+
if (!equalBytes(runtime.sessionConfigBytes(), message.sessionConfigBytes)
|
|
612
|
+
|| runtime.sessionConfigDigest !== message.sessionConfigDigest) {
|
|
613
|
+
throw new SyncplayError('Runtime factory substituted the canonical session config', {
|
|
614
|
+
code: 'client.runtime-config-mismatch',
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
nextSession = createSyncplaySession(runtime, {
|
|
618
|
+
playerCount: message.playerCount,
|
|
619
|
+
defaultInput: options.defaultInput,
|
|
620
|
+
...(options.requires === undefined ? {} : { requires: options.requires }),
|
|
621
|
+
...(options.snapshotBufferSize === undefined ? {} : { snapshotBufferSize: options.snapshotBufferSize }),
|
|
622
|
+
...checksumPolicy,
|
|
623
|
+
...(options.maxCatchUpFrames === undefined ? {} : { maxCatchUpFrames: options.maxCatchUpFrames }),
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
catch (error) {
|
|
627
|
+
if (swapAbort === controller)
|
|
628
|
+
deactivateGeneration(error);
|
|
629
|
+
try {
|
|
630
|
+
const pending = cleanupCandidate(lease);
|
|
631
|
+
if (pending !== undefined)
|
|
632
|
+
return trackCleanup(pending).then(() => { throw error; }, (cleanupError) => { throw cleanupFailure([error, cleanupError]); });
|
|
472
633
|
}
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
swapGeneration = undefined;
|
|
476
|
-
replaySwapBuffer();
|
|
634
|
+
catch (cleanupError) {
|
|
635
|
+
throw cleanupFailure([error, cleanupError]);
|
|
477
636
|
}
|
|
478
|
-
|
|
637
|
+
throw error;
|
|
479
638
|
}
|
|
480
|
-
|
|
481
|
-
swapGeneration = nextGeneration;
|
|
482
|
-
void acquireRuntime(result)
|
|
483
|
-
.then((lease) => {
|
|
639
|
+
const activate = () => {
|
|
484
640
|
if (disposed || swapAbort !== controller || controller.signal.aborted) {
|
|
485
|
-
|
|
486
|
-
return;
|
|
641
|
+
return cleanupCandidate(lease, nextSession);
|
|
487
642
|
}
|
|
643
|
+
activateSessionRuntime(message, nextGeneration, lease, nextSession);
|
|
644
|
+
};
|
|
645
|
+
const rejectCommit = (error) => {
|
|
646
|
+
secretFailure = cleanupFailure([error]);
|
|
488
647
|
try {
|
|
489
|
-
|
|
648
|
+
const pending = cleanupCandidate(lease, nextSession);
|
|
649
|
+
if (pending !== undefined)
|
|
650
|
+
return pending.then(() => { throw cleanupFailure([error]); }, (cleanupError) => { throw cleanupFailure([error, cleanupError]); });
|
|
490
651
|
}
|
|
491
|
-
catch (
|
|
492
|
-
|
|
493
|
-
failSwap(controller, caught);
|
|
494
|
-
return;
|
|
652
|
+
catch (cleanupError) {
|
|
653
|
+
throw cleanupFailure([error, cleanupError]);
|
|
495
654
|
}
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
655
|
+
throw cleanupFailure([error]);
|
|
656
|
+
};
|
|
657
|
+
let pending = commitBarrier;
|
|
658
|
+
if (pending === undefined) {
|
|
659
|
+
const previousSession = session;
|
|
660
|
+
const previousLease = activeLease;
|
|
661
|
+
session = undefined;
|
|
662
|
+
activeLease = undefined;
|
|
663
|
+
activeRuntime = undefined;
|
|
664
|
+
runtimeEpoch += 1;
|
|
665
|
+
const errors = [];
|
|
666
|
+
try {
|
|
667
|
+
previousSession?.dispose();
|
|
668
|
+
}
|
|
669
|
+
catch (error) {
|
|
670
|
+
errors.push(error);
|
|
671
|
+
}
|
|
672
|
+
try {
|
|
673
|
+
pending = releaseLease(previousLease);
|
|
674
|
+
}
|
|
675
|
+
catch (error) {
|
|
676
|
+
errors.push(error);
|
|
677
|
+
}
|
|
678
|
+
if (errors.length > 0) {
|
|
679
|
+
if (pending !== undefined)
|
|
680
|
+
pending = pending.then(() => { throw cleanupFailure(errors); }, (error) => { throw cleanupFailure([...errors, error]); });
|
|
681
|
+
else
|
|
682
|
+
return rejectCommit(cleanupFailure(errors));
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
if (pending === undefined)
|
|
686
|
+
return activate();
|
|
687
|
+
const commit = trackCleanup(pending.then(activate, rejectCommit));
|
|
688
|
+
commitBarrier = commit;
|
|
689
|
+
void commit.then(() => { if (commitBarrier === commit)
|
|
690
|
+
commitBarrier = undefined; }, () => { if (commitBarrier === commit)
|
|
691
|
+
commitBarrier = undefined; });
|
|
692
|
+
return commit;
|
|
501
693
|
}
|
|
502
|
-
function
|
|
694
|
+
function activateSessionRuntime(message, nextGeneration, lease, nextSession) {
|
|
503
695
|
const runtime = lease.runtime;
|
|
504
|
-
if (canonicalStringify(runtime.identity)
|
|
505
|
-
!== canonicalStringify(message.runtimeIdentity)) {
|
|
506
|
-
throw new SyncplayError('Runtime factory substituted the Kinetix runtime identity', {
|
|
507
|
-
code: 'client.runtime-identity-mismatch',
|
|
508
|
-
});
|
|
509
|
-
}
|
|
510
|
-
if (!equalBytes(runtime.sessionConfigBytes(), message.sessionConfigBytes)
|
|
511
|
-
|| runtime.sessionConfigDigest !== message.sessionConfigDigest) {
|
|
512
|
-
throw new SyncplayError('Runtime factory substituted the canonical session config', {
|
|
513
|
-
code: 'client.runtime-config-mismatch',
|
|
514
|
-
});
|
|
515
|
-
}
|
|
516
|
-
const nextSession = createSyncplaySession(runtime, {
|
|
517
|
-
playerCount: message.playerCount,
|
|
518
|
-
defaultInput: options.defaultInput,
|
|
519
|
-
...(options.requires === undefined ? {} : { requires: options.requires }),
|
|
520
|
-
...(options.snapshotBufferSize === undefined ? {} : { snapshotBufferSize: options.snapshotBufferSize }),
|
|
521
|
-
...(options.checksumIntervalFrames === undefined ? {} : { checksumIntervalFrames: options.checksumIntervalFrames }),
|
|
522
|
-
});
|
|
523
|
-
try {
|
|
524
|
-
session?.dispose();
|
|
525
|
-
}
|
|
526
|
-
catch { /* Cleanup must continue. */ }
|
|
527
|
-
releaseLease(activeLease);
|
|
528
696
|
activeLease = lease;
|
|
529
697
|
activeRuntime = runtime;
|
|
530
698
|
session = nextSession;
|
|
@@ -548,9 +716,11 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
548
716
|
ownInputs.clear();
|
|
549
717
|
confirmed.clear();
|
|
550
718
|
reportedTicks.length = 0;
|
|
719
|
+
pendingConfirmedChecksums.clear();
|
|
551
720
|
lastAutoReportedTick = -1;
|
|
552
721
|
ownInputSubstitutions = 0;
|
|
553
722
|
transferCollector = createSnapshotTransferCollector(snapshotCollectorOptions);
|
|
723
|
+
snapshotWitnesses = undefined;
|
|
554
724
|
outgoingTransferQueue.length = 0;
|
|
555
725
|
tickRateHz = message.tickRateHz;
|
|
556
726
|
frameMs = tickRateHz > 0 ? 1000 / tickRateHz : 0;
|
|
@@ -984,9 +1154,12 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
984
1154
|
const prior = confirmationQueue ?? Promise.resolve();
|
|
985
1155
|
const selectedRuntimeEpoch = runtimeEpoch;
|
|
986
1156
|
const next = prior.then(async () => {
|
|
987
|
-
for (const frame of frames)
|
|
1157
|
+
for (const frame of frames) {
|
|
1158
|
+
if (disposed || secretFailure !== undefined || selectedRuntimeEpoch !== runtimeEpoch)
|
|
1159
|
+
return;
|
|
988
1160
|
await secrets.acceptConfirmedCommands(frame.commands ?? []);
|
|
989
|
-
|
|
1161
|
+
}
|
|
1162
|
+
if (disposed || secretFailure !== undefined || selectedRuntimeEpoch !== runtimeEpoch)
|
|
990
1163
|
return;
|
|
991
1164
|
applyConfirmedFrames(frames, authorityTick);
|
|
992
1165
|
});
|
|
@@ -999,6 +1172,9 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
999
1172
|
});
|
|
1000
1173
|
}
|
|
1001
1174
|
function applyConfirmedFrames(frames, authorityTick) {
|
|
1175
|
+
if (disposed || secretFailure !== undefined)
|
|
1176
|
+
return;
|
|
1177
|
+
const confirmationRuntimeEpoch = runtimeEpoch;
|
|
1002
1178
|
const firstNewTick = frames.find((frame) => frame.tick > appliedThrough)?.tick;
|
|
1003
1179
|
const presentationWasLive = caughtUp;
|
|
1004
1180
|
const freshRoomFirstFrame = appliedThrough === -1 && firstNewTick === 0 && authorityTick === 0;
|
|
@@ -1013,6 +1189,7 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
1013
1189
|
});
|
|
1014
1190
|
}
|
|
1015
1191
|
}
|
|
1192
|
+
let batchResimulatedThrough = -1;
|
|
1016
1193
|
while (confirmed.has(appliedThrough + 1)) {
|
|
1017
1194
|
const tick = appliedThrough + 1;
|
|
1018
1195
|
const confFrame = confirmed.get(tick);
|
|
@@ -1022,15 +1199,38 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
1022
1199
|
if (predFrame === undefined || canonicalStringify(simulationFrame) !== canonicalStringify(predFrame)) {
|
|
1023
1200
|
timeline[tick] = { inputs: confInputs.slice(), commands: confFrame.commands };
|
|
1024
1201
|
if (predictedThrough > tick) {
|
|
1202
|
+
const depth = predictedThrough - tick;
|
|
1025
1203
|
rollbacks += 1;
|
|
1026
|
-
maxRollbackDepth = Math.max(maxRollbackDepth,
|
|
1204
|
+
maxRollbackDepth = Math.max(maxRollbackDepth, depth);
|
|
1205
|
+
if (depth <= 2) {
|
|
1206
|
+
rollbackDepthHistogram[1] += 1;
|
|
1207
|
+
}
|
|
1208
|
+
else if (depth <= 4) {
|
|
1209
|
+
rollbackDepthHistogram[2] += 1;
|
|
1210
|
+
}
|
|
1211
|
+
else if (depth <= 8) {
|
|
1212
|
+
rollbackDepthHistogram[3] += 1;
|
|
1213
|
+
}
|
|
1214
|
+
else {
|
|
1215
|
+
rollbackDepthHistogram[4] += 1;
|
|
1216
|
+
}
|
|
1217
|
+
batchResimulatedThrough = Math.max(batchResimulatedThrough, predictedThrough);
|
|
1218
|
+
const start = wallClock();
|
|
1027
1219
|
simulate(tick);
|
|
1220
|
+
const duration = Math.max(0, wallClock() - start);
|
|
1221
|
+
lastResimulationDurationMs = duration;
|
|
1222
|
+
accumulatedResimulationDurationMs += duration;
|
|
1028
1223
|
}
|
|
1029
1224
|
else {
|
|
1030
1225
|
predictedThrough = tick + 1;
|
|
1031
1226
|
simulate(tick);
|
|
1032
1227
|
}
|
|
1033
1228
|
}
|
|
1229
|
+
else if (tick >= batchResimulatedThrough) {
|
|
1230
|
+
// Confirmed frame matched prediction without rollback (rollback depth 0)
|
|
1231
|
+
// and was not already rewritten by a previous resimulation in this batch.
|
|
1232
|
+
rollbackDepthHistogram[0] += 1;
|
|
1233
|
+
}
|
|
1034
1234
|
for (let playerSlot = 0; playerSlot < playerCount; playerSlot += 1) {
|
|
1035
1235
|
lastConfirmedBySlot[playerSlot] = confInputs[playerSlot];
|
|
1036
1236
|
}
|
|
@@ -1063,6 +1263,9 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
1063
1263
|
projection: activeRuntime.project(selected.checkpoint, previous?.checkpoint),
|
|
1064
1264
|
}));
|
|
1065
1265
|
}
|
|
1266
|
+
retainConfirmedChecksumAt(tick);
|
|
1267
|
+
if (disposed || secretFailure !== undefined || runtimeEpoch !== confirmationRuntimeEpoch)
|
|
1268
|
+
return;
|
|
1066
1269
|
}
|
|
1067
1270
|
// A joiner that caught up from replayed history still has its own-input
|
|
1068
1271
|
// cursor at tick 0 while `appliedThrough` has jumped to the authority's
|
|
@@ -1105,13 +1308,16 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
1105
1308
|
}
|
|
1106
1309
|
// ── M5.5: cadence-aligned checksum reports (quorum food + desync telemetry) ──
|
|
1107
1310
|
function autoReportCrossedCadences() {
|
|
1311
|
+
const reportRuntimeEpoch = runtimeEpoch;
|
|
1108
1312
|
let next = (Math.floor(lastAutoReportedTick / checksumCadenceTicks) + 1) * checksumCadenceTicks;
|
|
1109
1313
|
let reported = false;
|
|
1110
1314
|
while (next <= appliedThrough) {
|
|
1111
|
-
sendChecksumReportAt(next);
|
|
1112
1315
|
lastAutoReportedTick = next;
|
|
1316
|
+
sendChecksumReportAt(next);
|
|
1317
|
+
if (disposed || secretFailure !== undefined || runtimeEpoch !== reportRuntimeEpoch)
|
|
1318
|
+
return;
|
|
1113
1319
|
reported = true;
|
|
1114
|
-
next
|
|
1320
|
+
next = (Math.floor(lastAutoReportedTick / checksumCadenceTicks) + 1) * checksumCadenceTicks;
|
|
1115
1321
|
}
|
|
1116
1322
|
// Netcode telemetry rides the same cadence (G8) — only once RTT exists,
|
|
1117
1323
|
// so step-counted tests (which never drive pumpPaced) stay silent.
|
|
@@ -1128,38 +1334,47 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
1128
1334
|
}));
|
|
1129
1335
|
}
|
|
1130
1336
|
}
|
|
1337
|
+
function retainConfirmedChecksumAt(tick) {
|
|
1338
|
+
if (role === 'spectator' || tick <= lastAutoReportedTick || tick % checksumCadenceTicks !== 0)
|
|
1339
|
+
return;
|
|
1340
|
+
pendingConfirmedChecksums.set(tick, requireSession().getWireStateChecksum(tick + 1));
|
|
1341
|
+
if (pendingConfirmedChecksums.size >= REPORTED_TICKS_RETAINED)
|
|
1342
|
+
autoReportCrossedCadences();
|
|
1343
|
+
}
|
|
1131
1344
|
/**
|
|
1132
|
-
*
|
|
1133
|
-
*
|
|
1134
|
-
* session out from under prediction. Quorum consistency comes from the wire
|
|
1135
|
-
* checksum's full-state serialize/deserialize round-trip; synctest's codec
|
|
1136
|
-
* phase verifies that round-trip is canonical on every retained frame.
|
|
1137
|
-
* Ticks that fell out of the buffer are skipped (the client simply is not
|
|
1138
|
-
* donor-eligible for them).
|
|
1345
|
+
* Keep confirmed cadence hashes before history pruning. Reports do not restore
|
|
1346
|
+
* the live session. Snapshot donation still requires a retained checkpoint.
|
|
1139
1347
|
*/
|
|
1140
1348
|
function sendChecksumReportAt(tick) {
|
|
1349
|
+
if (disposed || secretFailure !== undefined)
|
|
1350
|
+
return;
|
|
1141
1351
|
const active = session;
|
|
1142
1352
|
// Spectators hold no slot — the authority would drop their reports anyway.
|
|
1143
1353
|
if (active === undefined || tick < 0 || role === 'spectator') {
|
|
1144
1354
|
return;
|
|
1145
1355
|
}
|
|
1146
|
-
|
|
1147
|
-
if (
|
|
1148
|
-
|
|
1356
|
+
let checksum = pendingConfirmedChecksums.get(tick);
|
|
1357
|
+
if (checksum === undefined) {
|
|
1358
|
+
const snapshot = active.peekSnapshot(tick + 1);
|
|
1359
|
+
if (snapshot === undefined)
|
|
1360
|
+
return;
|
|
1361
|
+
checksum = active.getWireStateChecksum(snapshot.frame);
|
|
1149
1362
|
}
|
|
1363
|
+
pendingConfirmedChecksums.delete(tick);
|
|
1364
|
+
reportedTicks.push(tick);
|
|
1365
|
+
if (reportedTicks.length > REPORTED_TICKS_RETAINED)
|
|
1366
|
+
reportedTicks.shift();
|
|
1150
1367
|
options.transport.send(encodeDeterministicSessionMessage({
|
|
1151
1368
|
kind: 'checksum-report',
|
|
1152
1369
|
slot,
|
|
1153
1370
|
tick,
|
|
1154
|
-
checksum
|
|
1371
|
+
checksum,
|
|
1155
1372
|
}));
|
|
1156
|
-
reportedTicks.push(tick);
|
|
1157
|
-
if (reportedTicks.length > REPORTED_TICKS_RETAINED) {
|
|
1158
|
-
reportedTicks.shift();
|
|
1159
|
-
}
|
|
1160
1373
|
}
|
|
1161
1374
|
// ── M5.5: snapshot late-join wire handling (donor + joiner roles) ──
|
|
1162
1375
|
function handleSnapshotWireMessage(text) {
|
|
1376
|
+
if (disposed || secretFailure !== undefined)
|
|
1377
|
+
return;
|
|
1163
1378
|
let envelope;
|
|
1164
1379
|
try {
|
|
1165
1380
|
envelope = decodeDeterministicLocalAuthorityWireEnvelope(text);
|
|
@@ -1188,6 +1403,8 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
1188
1403
|
}
|
|
1189
1404
|
/** Donor role: answer with the smallest self-reported tick the buffer can still export. */
|
|
1190
1405
|
function handleSnapshotRequest(payload) {
|
|
1406
|
+
if (disposed || secretFailure !== undefined)
|
|
1407
|
+
return;
|
|
1191
1408
|
const active = session;
|
|
1192
1409
|
if (active === undefined) {
|
|
1193
1410
|
return;
|
|
@@ -1210,16 +1427,19 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
1210
1427
|
if (snapshot === undefined || confirmedAtTick === undefined) {
|
|
1211
1428
|
continue;
|
|
1212
1429
|
}
|
|
1213
|
-
const
|
|
1214
|
-
|
|
1430
|
+
const runtimeBytes = active.getSnapshotBytes(snapshot.frame);
|
|
1431
|
+
const fields = {
|
|
1215
1432
|
tick,
|
|
1216
1433
|
sessionFrame: tick + 1,
|
|
1217
|
-
runtimeBytes: bytesToBase64(
|
|
1434
|
+
runtimeBytes: bytesToBase64(runtimeBytes),
|
|
1218
1435
|
runtimeIdentity: active.schemaIdentity,
|
|
1219
1436
|
sessionConfigDigest: schemaHash,
|
|
1220
1437
|
stateChecksum: active.getWireStateChecksum(snapshot.frame),
|
|
1221
1438
|
confirmedInputs: confirmedAtTick.map((input) => cloneCanonical(input)),
|
|
1222
|
-
}
|
|
1439
|
+
};
|
|
1440
|
+
const bytes = encodeDeterministicStateSnapshot(active.schemaIdentity.stateDigestDomain === 'syncplay.state-report/2'
|
|
1441
|
+
? { ...fields, v: 3, runtimePayloadChecksum: sha256Hex(runtimeBytes), witnesses: 1 }
|
|
1442
|
+
: { ...fields, v: 2 });
|
|
1223
1443
|
donorTransferCounter += 1;
|
|
1224
1444
|
const transfer = createSnapshotTransferFromBytes(bytes, {
|
|
1225
1445
|
identity: { descriptorHash, schemaHash },
|
|
@@ -1239,6 +1459,8 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
1239
1459
|
/** Paced donor upload: a burst of large chunks must not trip host flood limits. */
|
|
1240
1460
|
function flushOutgoingTransfer() {
|
|
1241
1461
|
for (let i = 0; i < TRANSFER_SENDS_PER_PUMP && outgoingTransferQueue.length > 0; i += 1) {
|
|
1462
|
+
if (disposed || secretFailure !== undefined)
|
|
1463
|
+
return;
|
|
1242
1464
|
const text = outgoingTransferQueue.shift();
|
|
1243
1465
|
if (text !== undefined) {
|
|
1244
1466
|
options.transport.send(text);
|
|
@@ -1290,6 +1512,11 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
1290
1512
|
frame: snapshot.sessionFrame,
|
|
1291
1513
|
});
|
|
1292
1514
|
}
|
|
1515
|
+
if ((snapshot.v === 3 ? snapshot.witnesses : 1) < minimumSnapshotWitnesses) {
|
|
1516
|
+
throw new SyncplayError('Snapshot has too few checksum witnesses', {
|
|
1517
|
+
code: 'client.snapshot-insufficient-witnesses', frame: snapshot.sessionFrame,
|
|
1518
|
+
});
|
|
1519
|
+
}
|
|
1293
1520
|
try {
|
|
1294
1521
|
active.restoreSnapshot({
|
|
1295
1522
|
frame: snapshot.sessionFrame,
|
|
@@ -1301,12 +1528,20 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
1301
1528
|
});
|
|
1302
1529
|
}
|
|
1303
1530
|
catch (error) {
|
|
1531
|
+
const failureCode = error instanceof Error && 'code' in error ? String(error.code)
|
|
1532
|
+
: error instanceof Error ? error.message : '';
|
|
1533
|
+
if (failureCode.includes('CLEANUP_FAILED') || failureCode.includes('RUNTIME_FAILED')) {
|
|
1534
|
+
secretFailure = error instanceof Error ? error : new Error(String(error));
|
|
1535
|
+
throw secretFailure;
|
|
1536
|
+
}
|
|
1304
1537
|
throw new SyncplayError('Hydrated snapshot bytes or checksum do not match the Kinetix runtime', {
|
|
1305
1538
|
code: 'client.snapshot-state-mismatch',
|
|
1306
1539
|
frame: snapshot.sessionFrame,
|
|
1540
|
+
cause: error,
|
|
1307
1541
|
});
|
|
1308
1542
|
}
|
|
1309
1543
|
appliedThrough = snapshot.tick;
|
|
1544
|
+
snapshotWitnesses = snapshot.v === 3 ? snapshot.witnesses : undefined;
|
|
1310
1545
|
confirmedPlayerPresence = unknownPresence();
|
|
1311
1546
|
predictedThrough = snapshot.tick + 1;
|
|
1312
1547
|
// Never send input for ticks at/below the hydration point — they are
|
|
@@ -1336,7 +1571,7 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
1336
1571
|
return role;
|
|
1337
1572
|
},
|
|
1338
1573
|
get ready() {
|
|
1339
|
-
return session !== undefined;
|
|
1574
|
+
return session !== undefined && secretFailure === undefined;
|
|
1340
1575
|
},
|
|
1341
1576
|
get predictedThrough() {
|
|
1342
1577
|
return predictedThrough;
|
|
@@ -1347,6 +1582,7 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
1347
1582
|
get rollbacks() {
|
|
1348
1583
|
return rollbacks;
|
|
1349
1584
|
},
|
|
1585
|
+
get snapshotWitnesses() { return snapshotWitnesses; },
|
|
1350
1586
|
get confirmedPlayerPresence() {
|
|
1351
1587
|
return confirmedPlayerPresence;
|
|
1352
1588
|
},
|
|
@@ -1357,13 +1593,22 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
1357
1593
|
inputDelayTicks,
|
|
1358
1594
|
rollbacks,
|
|
1359
1595
|
maxRollbackDepth,
|
|
1596
|
+
lastResimulationDurationMs,
|
|
1597
|
+
accumulatedResimulationDurationMs,
|
|
1598
|
+
rollbackDepthHistogram: [
|
|
1599
|
+
rollbackDepthHistogram[0],
|
|
1600
|
+
rollbackDepthHistogram[1],
|
|
1601
|
+
rollbackDepthHistogram[2],
|
|
1602
|
+
rollbackDepthHistogram[3],
|
|
1603
|
+
rollbackDepthHistogram[4],
|
|
1604
|
+
],
|
|
1360
1605
|
droppedConfirmedPresentationFrames,
|
|
1361
1606
|
rejectedCommands,
|
|
1362
1607
|
ownInputSubstitutions,
|
|
1363
1608
|
};
|
|
1364
1609
|
},
|
|
1365
1610
|
get generation() {
|
|
1366
|
-
return sessionGeneration;
|
|
1611
|
+
return sessionGeneration ?? 0;
|
|
1367
1612
|
},
|
|
1368
1613
|
submitCommand(kind, payload) {
|
|
1369
1614
|
assertActiveClient();
|
|
@@ -1380,7 +1625,7 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
1380
1625
|
options.transport.send(encodeDeterministicSessionMessage({
|
|
1381
1626
|
kind: 'command-request',
|
|
1382
1627
|
slot,
|
|
1383
|
-
generation: sessionGeneration,
|
|
1628
|
+
generation: sessionGeneration ?? 0,
|
|
1384
1629
|
sequence: commandSequence,
|
|
1385
1630
|
command: { kind, payload },
|
|
1386
1631
|
}));
|
|
@@ -1445,22 +1690,44 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
1445
1690
|
ownScheduleCursor = Math.max(ownScheduleCursor, resendThrough + 1);
|
|
1446
1691
|
},
|
|
1447
1692
|
dispose() {
|
|
1448
|
-
if (disposed)
|
|
1693
|
+
if (disposed) {
|
|
1694
|
+
if (disposeError !== undefined)
|
|
1695
|
+
throw disposeError;
|
|
1449
1696
|
return disposePromise;
|
|
1697
|
+
}
|
|
1450
1698
|
disposed = true;
|
|
1451
1699
|
runtimeEpoch += 1;
|
|
1700
|
+
pendingConfirmedChecksums.clear();
|
|
1452
1701
|
swapAbort?.abort();
|
|
1453
1702
|
swapAbort = undefined;
|
|
1454
1703
|
swapGeneration = undefined;
|
|
1455
1704
|
swapBuffer.length = 0;
|
|
1456
1705
|
ephemeralHandlers.clear();
|
|
1457
1706
|
pendingEphemeral = undefined;
|
|
1707
|
+
const errors = [];
|
|
1458
1708
|
try {
|
|
1459
1709
|
session?.dispose();
|
|
1460
1710
|
}
|
|
1461
|
-
catch {
|
|
1711
|
+
catch (error) {
|
|
1712
|
+
errors.push(error);
|
|
1713
|
+
}
|
|
1462
1714
|
session = undefined;
|
|
1463
|
-
|
|
1715
|
+
try {
|
|
1716
|
+
releaseLease(activeLease);
|
|
1717
|
+
}
|
|
1718
|
+
catch (error) {
|
|
1719
|
+
errors.push(error);
|
|
1720
|
+
}
|
|
1721
|
+
if (pendingAcquisitions.size > 0 || cleanupJobs.size > 0) {
|
|
1722
|
+
disposePromise = (async () => {
|
|
1723
|
+
while (pendingAcquisitions.size > 0 || cleanupJobs.size > 0) {
|
|
1724
|
+
await Promise.allSettled([...pendingAcquisitions, ...cleanupJobs]);
|
|
1725
|
+
}
|
|
1726
|
+
if (errors.length > 0 || cleanupErrors.length > 0)
|
|
1727
|
+
throw cleanupFailure([...errors, ...cleanupErrors]);
|
|
1728
|
+
})();
|
|
1729
|
+
void disposePromise.catch(() => undefined);
|
|
1730
|
+
}
|
|
1464
1731
|
activeLease = undefined;
|
|
1465
1732
|
activeRuntime = undefined;
|
|
1466
1733
|
caughtUp = false;
|
|
@@ -1475,6 +1742,10 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
1475
1742
|
readyReject?.(error);
|
|
1476
1743
|
readyResolve = undefined;
|
|
1477
1744
|
readyReject = undefined;
|
|
1745
|
+
if (disposePromise === undefined && (errors.length > 0 || cleanupErrors.length > 0)) {
|
|
1746
|
+
disposeError = cleanupFailure([...errors, ...cleanupErrors]);
|
|
1747
|
+
throw disposeError;
|
|
1748
|
+
}
|
|
1478
1749
|
return disposePromise;
|
|
1479
1750
|
},
|
|
1480
1751
|
whenReady() {
|
|
@@ -1485,6 +1756,8 @@ export function createNetworkedSyncplayClient(options) {
|
|
|
1485
1756
|
pumpPaced,
|
|
1486
1757
|
reportChecksum() {
|
|
1487
1758
|
assertActiveClient();
|
|
1759
|
+
if (secretFailure !== undefined)
|
|
1760
|
+
throw secretFailure;
|
|
1488
1761
|
if (session === undefined || appliedThrough < 0) {
|
|
1489
1762
|
return;
|
|
1490
1763
|
}
|