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");