From 0824f7d31545825cdeac5f0aec227651d306f7c1 Mon Sep 17 00:00:00 2001 From: dingo Date: Mon, 13 Apr 2026 08:55:09 -0400 Subject: [PATCH] =?UTF-8?q?fix:=20AI=20turns=20racing=20across=20pods=20?= =?UTF-8?q?=E2=80=94=20only=20start=20AiPlayerSessionManager=20on=20AI=20p?= =?UTF-8?q?od?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit NatsEventBus.SubscribeAsync uses queueGroup=null, so every pod receives every NATS event. AddAiService was guarded by IsEnabled("game"), meaning ALL game pods also started AiPlayerSessionManager. On each AiTurnStartedEvent, all 3 pods (2 game + 1 AI) would concurrently call ExecuteTurnAsync — causing draw/action conflicts and the AI appearing to stall or do nothing. Fix: remove AddHostedService from AddAiService; add it to Program.cs under IsEnabled("ai") so only the dedicated AI pod (ENABLED_SERVICES=game,ai) starts the session manager. Game pods still register AiPlayerSessionManager as a Singleton (for potential injection) but never start its event loop. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- Server/src/SOR.Ai/ServiceRegistration.cs | 10 +++++++--- Server/src/SOR.Host/Program.cs | 7 ++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Server/src/SOR.Ai/ServiceRegistration.cs b/Server/src/SOR.Ai/ServiceRegistration.cs index b2166db..6635416 100644 --- a/Server/src/SOR.Ai/ServiceRegistration.cs +++ b/Server/src/SOR.Ai/ServiceRegistration.cs @@ -51,10 +51,14 @@ public static class ServiceRegistration services.AddSingleton(); services.AddSingleton(sp => sp.GetRequiredService()); - // ── Session manager (singleton hosted service) ──────────────────────── - // Listens for game-start events and orchestrates AI turns. + // ── Session manager (singleton, NOT started here) ──────────────────── + // Registered as Singleton so it can be resolved by DI, but AddHostedService + // is intentionally NOT called here. SOR.Host/Program.cs calls AddHostedService + // only when ENABLED_SERVICES includes "ai", ensuring that exactly one pod + // (the AI pod) listens for GameStartedEvent / AiTurnStartedEvent. + // If every pod started this service, each would receive every NATS event + // (queueGroup=null) and concurrently execute the AI's turns. services.AddSingleton(); - services.AddHostedService(sp => sp.GetRequiredService()); // ── Lobby scoped services ───────────────────────────────────────────── services.AddScoped(); diff --git a/Server/src/SOR.Host/Program.cs b/Server/src/SOR.Host/Program.cs index 71fb69e..d85df19 100644 --- a/Server/src/SOR.Host/Program.cs +++ b/Server/src/SOR.Host/Program.cs @@ -179,8 +179,13 @@ if (IsEnabled("content")) builder.Services.AddContentService(builder.Configur if (IsEnabled("versioning")) builder.Services.AddVersioningService(builder.Configuration); if (IsEnabled("matchmaking")) builder.Services.AddMatchmakingService(builder.Configuration); if (IsEnabled("game")) builder.Services.AddGameService(builder.Configuration); -// AI is always co-located with game: it invokes game handlers in-process. +// AI handlers and infrastructure are co-located with game (invoke game handlers in-process). if (IsEnabled("game")) builder.Services.AddAiService(builder.Configuration); +// AiPlayerSessionManager listens for NATS events (no queue group = every subscriber receives +// every event). Only start it on the dedicated AI pod so exactly one process drives AI turns. +if (IsEnabled("ai")) + builder.Services.AddHostedService(sp => + sp.GetRequiredService()); if (IsEnabled("support")) builder.Services.AddSupportService(builder.Configuration); // ── BUILD ─────────────────────────────────────────────────────────────────────