@hadoku/game-server 0.1.175 → 0.1.176

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.
@@ -450,13 +450,14 @@ export type JvmHostFactory<L extends {
450
450
  attach(record: MatchRecord, events: HostEvents<L, C>): MatchHost<C>;
451
451
  handOverAll(): string[];
452
452
  reapOnExit(): ReapResult;
453
- /** JVMs this pool holds, with their matches: for diagnostics and tests. */
453
+ /** JVMs this pool holds, with their matches and the restores waiting in them for an old copy to stop: for placement, diagnostics and tests. */
454
454
  jvms(): {
455
455
  hostId: string;
456
456
  warm: boolean;
457
457
  draining: boolean;
458
458
  adopted: boolean;
459
459
  matches: string[];
460
+ waiting: string[];
460
461
  }[];
461
462
  /** Place nothing more into this JVM, and retire it once its matches end (a heap past `recycleWanted`). */
462
463
  drain(hostId: string, why: string): void;
package/dist/play/host.js CHANGED
@@ -256,7 +256,9 @@ export function jvmHostFactory(config, options = {}) {
256
256
  listener();
257
257
  };
258
258
  const liveJvms = () => [...jvms].filter((j) => !j.exited && !j.retiring && !j.released);
259
- const hasRoom = (jvm) => jvm.matches.size < perJvm && !jvm.draining;
259
+ /** The matches a JVM holds, counting the restores waiting in it for their old copy to stop. */
260
+ const load = (jvm) => jvm.matches.size + jvm.waiting.size;
261
+ const hasRoom = (jvm) => load(jvm) < perJvm && !jvm.draining;
260
262
  /* ── The record and the streams ── */
261
263
  function writeRecord(jvm) {
262
264
  if (jvm.released || jvm.exited)
@@ -384,6 +386,7 @@ export function jvmHostFactory(config, options = {}) {
384
386
  adopted: from.adopted ?? false,
385
387
  released: false,
386
388
  matches: new Map(),
389
+ waiting: new Map(),
387
390
  lastHeard: Date.now(),
388
391
  lastCheck: Date.now(),
389
392
  livenessArmed: !from.warm,
@@ -531,10 +534,19 @@ export function jvmHostFactory(config, options = {}) {
531
534
  if (h.stopTimer !== null)
532
535
  clearTimeout(h.stopTimer);
533
536
  closeStream(h);
534
- pending.delete(h.id);
537
+ if (pending.get(h.id) === h)
538
+ pending.delete(h.id);
535
539
  const jvm = h.jvm;
536
- jvm.matches.delete(h.id);
537
- writeRecord(jvm);
540
+ // By identity: a restore of this id may be waiting beside it (`held`), or be it.
541
+ if (jvm.matches.get(h.id) === h)
542
+ jvm.matches.delete(h.id);
543
+ if (jvm.waiting.get(h.id) === h)
544
+ jvm.waiting.delete(h.id);
545
+ const next = h.held === null ? jvm.waiting.get(h.id) : undefined;
546
+ if (next !== undefined)
547
+ startHeld(next);
548
+ else
549
+ writeRecord(jvm);
538
550
  if (h.events !== null && !h.handed) {
539
551
  h.events.exit(h.stopping
540
552
  ? 'stopped'
@@ -544,7 +556,7 @@ export function jvmHostFactory(config, options = {}) {
544
556
  ? PLAYED_OUT
545
557
  : 'Forge ended the match without being asked to');
546
558
  }
547
- if (jvm.matches.size === 0)
559
+ if (load(jvm) === 0)
548
560
  idle(jvm);
549
561
  notifyRoom();
550
562
  replenish();
@@ -559,17 +571,19 @@ export function jvmHostFactory(config, options = {}) {
559
571
  if (jvm.idle !== null)
560
572
  clearTimeout(jvm.idle);
561
573
  const why = jvm.unresponsive ?? reason;
562
- const lost = [...jvm.matches.values()].filter((h) => !h.ended);
574
+ const lost = [...jvm.matches.values(), ...jvm.waiting.values()].filter((h) => !h.ended);
563
575
  for (const h of lost) {
564
576
  h.ended = true;
565
577
  if (h.stopTimer !== null)
566
578
  clearTimeout(h.stopTimer);
567
579
  closeStream(h);
568
- pending.delete(h.id);
580
+ if (pending.get(h.id) === h)
581
+ pending.delete(h.id);
569
582
  if (!reaping && h.events !== null && !h.handed)
570
583
  h.events.exit(h.stopping ? 'stopped' : why);
571
584
  }
572
585
  jvm.matches.clear();
586
+ jvm.waiting.clear();
573
587
  if (reaping)
574
588
  return;
575
589
  if (lost.some((h) => !h.stopping)) {
@@ -606,7 +620,7 @@ export function jvmHostFactory(config, options = {}) {
606
620
  }
607
621
  /** The JVM has no matches: retire it now, or after `idleMs` unless it is the warm one then. */
608
622
  function idle(jvm) {
609
- if (jvm.matches.size > 0 || jvm.exited || jvm.retiring || jvm.released)
623
+ if (load(jvm) > 0 || jvm.exited || jvm.retiring || jvm.released)
610
624
  return;
611
625
  if (closed || !warmWanted || jvm.draining) {
612
626
  retire(jvm, closed ? 'closing' : jvm.draining ? 'drained' : 'no match left, and no JVM is kept warm');
@@ -616,7 +630,7 @@ export function jvmHostFactory(config, options = {}) {
616
630
  clearTimeout(jvm.idle);
617
631
  jvm.idle = setTimeout(() => {
618
632
  jvm.idle = null;
619
- if (jvm.matches.size > 0)
633
+ if (load(jvm) > 0)
620
634
  return;
621
635
  if (liveJvms().some((other) => other !== jvm && hasRoom(other)))
622
636
  retire(jvm, `no match for ${idleMs} ms`);
@@ -662,10 +676,38 @@ export function jvmHostFactory(config, options = {}) {
662
676
  }
663
677
  }
664
678
  /* ── Matches ── */
679
+ /** Send a match's `start` to its JVM: at once, or (`held`) once the old copy of its id there has stopped. */
680
+ function sendStart(h) {
681
+ h.jvm.transport.send(JSON.stringify({ t: 'start', config: bridgeConfig(h.id, h.entry.spec, config.aiFlatMc ?? 'off'), match: h.jvm.hostId }));
682
+ }
683
+ /**
684
+ * The old copy of a restored match has stopped in its JVM: the restore takes its place there, its
685
+ * `start` first and then every command it was sent meanwhile, in order. The bridge has let go of the
686
+ * id by then (`PlayBridge.startHosted` removes it before it says `stopped`).
687
+ */
688
+ function startHeld(h) {
689
+ const held = h.held ?? [];
690
+ const jvm = h.jvm;
691
+ h.held = null;
692
+ jvm.waiting.delete(h.id);
693
+ jvm.matches.set(h.id, h);
694
+ writeRecord(jvm);
695
+ (h.events?.log ?? poolLog)({ evt: 'match-start-released', matchId: h.id, hostId: jvm.hostId, waitedMs: Date.now() - Date.parse(h.entry.startedAt), commands: held.length });
696
+ sendStart(h);
697
+ for (const command of held) {
698
+ persist(h, JSON.stringify({ c: command }));
699
+ jvm.transport.send(JSON.stringify({ ...command, match: h.id }));
700
+ }
701
+ }
665
702
  function stopMatch(h) {
666
703
  if (h.stopping || h.ended)
667
704
  return;
668
705
  h.stopping = true;
706
+ // Never started: a `stop` of its id would reach the old copy it waits behind, which is already stopping.
707
+ if (h.held !== null) {
708
+ matchEnded(h);
709
+ return;
710
+ }
669
711
  // Out of the record now, not at its `stopped`: a JVM killed before that (remote, unconfirmed)
670
712
  // would otherwise offer the match to the next server.
671
713
  writeRecord(h.jvm);
@@ -685,6 +727,10 @@ export function jvmHostFactory(config, options = {}) {
685
727
  }
686
728
  if (h.ended || h.jvm.exited)
687
729
  return;
730
+ if (h.held !== null) {
731
+ h.held.push(command);
732
+ return;
733
+ }
688
734
  // Kept in the same stream, in the same order, as the lines that came back: a replay
689
735
  // has to know which asks the old server had already answered.
690
736
  persist(h, JSON.stringify({ c: command }));
@@ -716,7 +762,8 @@ export function jvmHostFactory(config, options = {}) {
716
762
  });
717
763
  return false;
718
764
  }
719
- if (h.ended || h.stopping || h.jvm.exited || h.jvm.released || h.events === null)
765
+ // Not started yet: Forge has no game of it for a later server to take back.
766
+ if (h.ended || h.stopping || h.held !== null || h.jvm.exited || h.jvm.released || h.events === null)
720
767
  return false;
721
768
  // A bot game is not kept: nobody can watch it, and the room plays it again (`play/botgames.ts`).
722
769
  if (h.entry.spec.headless !== undefined)
@@ -726,26 +773,59 @@ export function jvmHostFactory(config, options = {}) {
726
773
  },
727
774
  };
728
775
  }
729
- /** Where a new match goes: the fullest warm JVM with room, else one booting, else none. */
730
776
  /**
731
- * A JVM with room for `matchId`. Never one still holding a match of that id: a restored match
732
- * keeps its id (`MatchRegistry.restore`), and a JVM routes its lines by id, so the new game must
733
- * not land beside the old one in the moment before the old one is gone.
777
+ * Where a new match goes: the fullest warm JVM with room, else one booting, else none. Never one
778
+ * still holding a match of `matchId`: a restored match keeps its id (`MatchRegistry.restore`), and
779
+ * a JVM routes its lines by id, so the new game must not run beside the old one there.
734
780
  */
735
781
  function placeFor(matchId) {
736
- const open = liveJvms().filter((j) => hasRoom(j) && (matchId === undefined || !j.matches.has(matchId)));
737
- const warm = open.filter((j) => j.warm).sort((a, b) => b.matches.size - a.matches.size);
782
+ const open = liveJvms().filter((j) => hasRoom(j) && (matchId === undefined || (!j.matches.has(matchId) && !j.waiting.has(matchId))));
783
+ const warm = open.filter((j) => j.warm).sort((a, b) => load(b) - load(a));
738
784
  return warm[0] ?? open[0] ?? null;
739
785
  }
740
786
  /**
741
- * Throws when the match cannot be given a JVM (every JVM full at `maxJvms`, or a launch that
742
- * throws), and leaves nothing behind. The caller fails the match (`Match.start`).
787
+ * The JVM where `matchId`'s old copy is being stopped, when the restore can take its place there
788
+ * once it goes: a claw runs one JVM, so without this a Restart turn found no room (2026-11-29).
789
+ */
790
+ function behindOldCopy(matchId) {
791
+ return (liveJvms().find((j) => {
792
+ const old = j.matches.get(matchId);
793
+ return old !== undefined && old.stopping && !old.ended && !j.draining && !j.waiting.has(matchId) && load(j) <= perJvm;
794
+ }) ?? null);
795
+ }
796
+ /** Why no JVM of this pool takes `matchId`, JVM by JVM: "full" only when every one is. */
797
+ function noRoom(matchId) {
798
+ const why = (j) => {
799
+ if (j.retiring)
800
+ return 'is retiring';
801
+ if (j.draining)
802
+ return 'is draining';
803
+ const old = j.matches.get(matchId);
804
+ if (old !== undefined && !old.stopping)
805
+ return 'still runs its old copy, which was not asked to stop';
806
+ if (j.waiting.has(matchId))
807
+ return 'already holds a restore of it, waiting for its old copy to stop';
808
+ return load(j) >= perJvm ? `is full (${load(j)} of ${perJvm})` : 'has room';
809
+ };
810
+ const all = [...jvms];
811
+ if (all.every((j) => why(j).startsWith('is full')))
812
+ return `every Forge JVM is full (${jvms.size} of ${maxJvms()}, ${perJvm} matches each)`;
813
+ return `no Forge JVM here can take ${matchId}: ${all.map((j) => `${j.hostId} ${why(j)}`).join('; ')}; ${jvms.size} of ${maxJvms()} JVMs running`;
814
+ }
815
+ /**
816
+ * Throws when the match cannot be given a JVM (none can take it at `maxJvms`, or a launch that
817
+ * throws), and leaves nothing behind. The caller fails the match (`Match.start`). A restore whose
818
+ * old copy is being stopped in a JVM waits there for it (`held`) rather than in a JVM still booting
819
+ * or a launch: a match's stop takes far less than a boot.
743
820
  */
744
821
  const factory = ((matchId, spec, events) => {
745
- let jvm = placeFor(matchId);
822
+ const open = placeFor(matchId);
823
+ const after = open?.warm === true ? null : behindOldCopy(matchId);
824
+ const behind = after !== null;
825
+ let jvm = after ?? open;
746
826
  if (jvm === null) {
747
827
  if (jvms.size >= maxJvms())
748
- throw new Error(`every Forge JVM is full (${jvms.size} of ${maxJvms()}, ${perJvm} matches each)`);
828
+ throw new Error(noRoom(matchId));
749
829
  jvm = launch();
750
830
  }
751
831
  if (jvm.idle !== null)
@@ -764,11 +844,25 @@ export function jvmHostFactory(config, options = {}) {
764
844
  fatal: null,
765
845
  stopTimer: null,
766
846
  stream: null,
847
+ held: behind ? [] : null,
767
848
  };
768
- jvm.matches.set(matchId, h);
769
- writeRecord(jvm);
770
- events.log({ evt: 'match-placed', matchId, hostId: jvm.hostId, warm: jvm.warm, matches: jvm.matches.size, aiFlatMc: config.aiFlatMc ?? 'off' });
771
- jvm.transport.send(JSON.stringify({ t: 'start', config: bridgeConfig(matchId, spec, config.aiFlatMc ?? 'off'), match: jvm.hostId }));
849
+ events.log({
850
+ evt: 'match-placed',
851
+ matchId,
852
+ hostId: jvm.hostId,
853
+ warm: jvm.warm,
854
+ matches: load(jvm) + 1,
855
+ aiFlatMc: config.aiFlatMc ?? 'off',
856
+ ...(behind ? { behind: 'its old copy, being stopped' } : {}),
857
+ });
858
+ if (behind) {
859
+ jvm.waiting.set(matchId, h);
860
+ }
861
+ else {
862
+ jvm.matches.set(matchId, h);
863
+ writeRecord(jvm);
864
+ sendStart(h);
865
+ }
772
866
  notifyRoom();
773
867
  replenish();
774
868
  return hostFace(h);
@@ -781,7 +875,7 @@ export function jvmHostFactory(config, options = {}) {
781
875
  factory.watchRoom = (listener) => {
782
876
  roomWatchers.push(listener);
783
877
  };
784
- factory.jvms = () => [...jvms].map((j) => ({ hostId: j.hostId, warm: j.warm, draining: j.draining, adopted: j.adopted, matches: [...j.matches.keys()] }));
878
+ factory.jvms = () => [...jvms].map((j) => ({ hostId: j.hostId, warm: j.warm, draining: j.draining, adopted: j.adopted, matches: [...j.matches.keys()], waiting: [...j.waiting.keys()] }));
785
879
  factory.drain = (hostId, why) => {
786
880
  const jvm = [...jvms].find((j) => j.hostId === hostId);
787
881
  if (jvm === undefined || jvm.draining || jvm.exited || jvm.released)
@@ -839,6 +933,7 @@ export function jvmHostFactory(config, options = {}) {
839
933
  fatal: null,
840
934
  stopTimer: null,
841
935
  stream: null,
936
+ held: null,
842
937
  };
843
938
  jvm.matches.set(h.id, h);
844
939
  if (existsSync(streamFile(dir, h.id))) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hadoku/game-server",
3
- "version": "0.1.175",
4
- "gitHead": "9b2dacac47c9445b2b3059b3a9272269a370607f",
3
+ "version": "0.1.176",
4
+ "gitHead": "496d4e086f48a863ada4fae6b255f3bd3b63d144",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "description": "The server parts any hosted game shares, above the kernel: the app config and edge auth, the room core (identity, seats, sessions, membership, the room list, the generic room routes), match hosting (the host contract, local and remote JVMs, placement, the start queue, recordings, the in-process and relay runtimes), streams, erase, telemetry and usage. Knows no game; Magic plugs in from apps/server. MIT; imports only node built-ins, express, socket.io, @wolffm/* and the platform packages.",
@@ -17,9 +17,9 @@
17
17
  "play/remote/"
18
18
  ],
19
19
  "dependencies": {
20
- "@wolffm/host-wire": "npm:@hadoku/host-wire@0.1.175",
21
- "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.175",
22
- "@wolffm/service-kernel": "npm:@hadoku/service-kernel@0.1.208",
20
+ "@wolffm/host-wire": "npm:@hadoku/host-wire@0.1.176",
21
+ "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.176",
22
+ "@wolffm/service-kernel": "npm:@hadoku/service-kernel@0.1.209",
23
23
  "@wolffm/prefs-client": "npm:@hadoku/prefs-client@^1.2.2",
24
24
  "@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@^0.45.145",
25
25
  "@wolffm/rooms-server": "npm:@hadoku/rooms-server@^0.45.145",