fix: AI turns racing across pods — only start AiPlayerSessionManager on AI pod

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 <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-04-13 08:55:09 -04:00
co-authored by Claude Happy
parent 23dc7925e2
commit 0824f7d315
2 changed files with 13 additions and 4 deletions
+7 -3
View File
@@ -51,10 +51,14 @@ public static class ServiceRegistration
services.AddSingleton<AiAccountRegistry>();
services.AddSingleton<IAiAccountRegistry>(sp => sp.GetRequiredService<AiAccountRegistry>());
// ── 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<AiPlayerSessionManager>();
services.AddHostedService(sp => sp.GetRequiredService<AiPlayerSessionManager>());
// ── Lobby scoped services ─────────────────────────────────────────────
services.AddScoped<AiSlotAssigner>();
+6 -1
View File
@@ -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<SOR.Ai.Core.AiPlayerSessionManager>());
if (IsEnabled("support")) builder.Services.AddSupportService(builder.Configuration);
// ── BUILD ─────────────────────────────────────────────────────────────────────