From 2713a786e4c929a635dd9ed5351a590fa99a9100 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Mon, 19 Oct 2026 13:12:20 -0400 Subject: [PATCH] fix(arena): clamp early-timer catch-up so the tick loop holds 20Hz Node timers can fire a sub-millisecond early relative to performance.now(); Math.floor((now - scheduledFor) / TICK_MS) then yields -1, and (1 + extra) shaved a whole tick off the next schedule. The resulting delay-0 re-fire doubled the sim rate (~33-40Hz observed on the live arena during integration testing; local repro confirmed it is the scheduler, not the host). Clamp extra to [0, TICK_CATCHUP_CAP] and add a wall-clock regression test that pins driftReport ticksRun to ~20/s (fails at the doubled rate). --- src/arena/games/bomperator.ts | 6 ++++- tests/int/arena-bomperator.int.spec.ts | 32 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/arena/games/bomperator.ts b/src/arena/games/bomperator.ts index 7beecf06..c96f824e 100644 --- a/src/arena/games/bomperator.ts +++ b/src/arena/games/bomperator.ts @@ -241,7 +241,11 @@ export class BomperatorGameModule implements RealtimeGameModule { record.ticksRun += 1; if (!record.timer) return; // step completed or disposed the room - const extra = Math.min(Math.floor((now - scheduledFor) / TICK_MS), TICK_CATCHUP_CAP); + // Clamp BOTH sides: an early fire (Node timers can land a sub-millisecond + // ahead of performance.now()) floors to -1, and an unclamped negative + // "extra" shaves a whole tick off the next schedule, producing an instant + // catch-up re-fire that doubles the sim rate (~33Hz observed in prod). + const extra = Math.max(0, Math.min(Math.floor((now - scheduledFor) / TICK_MS), TICK_CATCHUP_CAP)); for (let i = 0; i < extra; i += 1) { this.step(record); record.ticksRun += 1; diff --git a/tests/int/arena-bomperator.int.spec.ts b/tests/int/arena-bomperator.int.spec.ts index 27a2b32a..c9e50722 100644 --- a/tests/int/arena-bomperator.int.spec.ts +++ b/tests/int/arena-bomperator.int.spec.ts @@ -54,6 +54,38 @@ beforeEach(() => { }); afterEach(() => ctx.mod.clear()); +describe("arena bomperator module: scheduler rate", () => { + it( + "wall-clock loop advances the sim at ~20Hz, not faster (early-timer regression)", + async () => { + // Prod bug this pins: Node timers may fire a sub-millisecond early vs + // performance.now(); an unclamped negative catch-up `extra` shaved a + // tick off the next schedule and doubled the rate (~33-40Hz observed). + const mod = new BomperatorGameModule({ + deliver: () => {}, + onResults: () => {}, + autoTick: true, + }); + try { + const matchId = mod.createRoom(8001, "HzHost"); + mod.join(matchId, 8002, "HzGuest"); + mod.start(matchId, 8001); + mod.driftReport(); // reset counters + await new Promise((resolve) => setTimeout(resolve, 1000)); + const report = mod.driftReport()[0]; + expect(report).toBeDefined(); + // 20Hz over 1s = ~20 ticks; generous bounds for slow CI, but firmly + // below the doubled rate and above a starved one. + expect(report!.ticksRun).toBeGreaterThanOrEqual(14); + expect(report!.ticksRun).toBeLessThanOrEqual(26); + } finally { + mod.clear(); + } + }, + 15_000, + ); +}); + describe("arena bomperator module: lobby", () => { it("seats the host and reports a waiting match", () => { const matchId = ctx.mod.createRoom(9001, "Host");