From babf5f98218cb81ffe29e0080ec682f199d720df Mon Sep 17 00:00:00 2001 From: dingo Date: Tue, 14 Apr 2026 06:22:59 -0400 Subject: [PATCH] fix(ai): prevent greedy engine from freezing after a high-value ability activation When an ability activation raised the score baseline significantly (e.g. bouncing a unit, dealing damage, healing), no subsequent move or attack could beat that new baseline, so the AI ended its turn after just one action leaving all other units idle. Two-part fix: 1. Relax the selection threshold by NoRegressTolerance (1.0 pts) so that moves/attacks that are near-neutral still qualify as candidates. 2. Track serialised-state hashes for every state seen during the turn; candidates that would revisit a prior state are skipped, preventing the tolerance from enabling intra-turn cycles. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- .../Heuristics/HeuristicDecisionEngine.cs | 41 +++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/Server/src/SOR.Ai/Heuristics/HeuristicDecisionEngine.cs b/Server/src/SOR.Ai/Heuristics/HeuristicDecisionEngine.cs index 06dcd4f..61bca85 100644 --- a/Server/src/SOR.Ai/Heuristics/HeuristicDecisionEngine.cs +++ b/Server/src/SOR.Ai/Heuristics/HeuristicDecisionEngine.cs @@ -52,6 +52,18 @@ internal sealed class HeuristicDecisionEngine : IAiDecisionEngine const int maxActions = 50; int iterations = 0; + // Track serialised-state hashes seen so far this turn to prevent intra-turn + // cycles. Using full JSON hash is cheap enough given we already serialise for + // every candidate evaluation. + var seenStateHashes = new HashSet { HashState(workingState) }; + + // Allow actions that don't strictly beat the current baseline but are no + // worse than this tolerance. This prevents the AI from freezing after a + // single high-value action (e.g. an ability activation) raises the baseline + // above what any subsequent move/attack can achieve, leaving other units idle. + // Cycle detection (seenStateHashes) ensures the tolerance cannot cause loops. + const float NoRegressTolerance = 1.0f; + while (iterations++ < maxActions) { var workingCtx = new AiTurnContext(workingState, ctx.AiAccountId); @@ -61,17 +73,20 @@ internal sealed class HeuristicDecisionEngine : IAiDecisionEngine if (candidates.Count == 0) break; - AiAction? bestAction = null; - float bestScore = baseScore; + AiAction? bestAction = null; + float bestScore = baseScore - NoRegressTolerance; // threshold: must not regress beyond tolerance + int bestNewHash = 0; foreach (var candidate in candidates) { var simState = CloneState(workingState); float score; + int newHash; try { ApplyAction(simState, ctx.AiAccountId, candidate); - score = ScoringFunctions.Evaluate(simState, ctx.AiAccountId, _profile); + newHash = HashState(simState); + score = ScoringFunctions.Evaluate(simState, ctx.AiAccountId, _profile); } catch { @@ -79,19 +94,26 @@ internal sealed class HeuristicDecisionEngine : IAiDecisionEngine continue; } + // Skip actions that would revisit a state already seen this turn. + if (seenStateHashes.Contains(newHash)) continue; + if (score > bestScore) { - bestScore = score; - bestAction = candidate; + bestScore = score; + bestAction = candidate; + bestNewHash = newHash; } } if (bestAction is null) - break; // no improvement available + break; // no improvement or non-regressive action available // ── Mistake injection ────────────────────────────────────────────── if (_profile.MistakeRate > 0 && Random.Shared.NextSingle() < _profile.MistakeRate) - bestAction = candidates[Random.Shared.Next(candidates.Count)]; + { + bestAction = candidates[Random.Shared.Next(candidates.Count)]; + bestNewHash = 0; // hash unknown for random pick; recomputed below + } plan.Add(bestAction); @@ -99,6 +121,8 @@ internal sealed class HeuristicDecisionEngine : IAiDecisionEngine try { ApplyAction(workingState, ctx.AiAccountId, bestAction); } catch { break; /* action became invalid — stop the loop */ } + seenStateHashes.Add(bestNewHash != 0 ? bestNewHash : HashState(workingState)); + baseScore = ScoringFunctions.Evaluate(workingState, ctx.AiAccountId, _profile); } @@ -111,6 +135,9 @@ internal sealed class HeuristicDecisionEngine : IAiDecisionEngine private static GameState CloneState(GameState state) => GameEngine.Deserialize(GameEngine.Serialize(state)); + private static int HashState(GameState state) + => GameEngine.Serialize(state).GetHashCode(); + /// /// Apply one AiAction directly to a GameState (for simulation purposes). /// Throws GameException if the action is invalid — callers must handle this.