File size: 7,913 Bytes
a2aa13e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using Microsoft.Extensions.Logging;
using SilkroadBot.Domain.Models;
using SilkroadBot.Plugins.SDK.Interfaces;

namespace SilkroadBot.AI.Guardrails;

/// <summary>
/// Command Validation Layer (Sanity Check).
/// Every AI-generated action must pass through this layer before execution.
/// Prevents 'hallucinated' or illogical decisions that could lead to character loss.
/// </summary>
public class CommandValidator
{
    private readonly ILogger<CommandValidator> _logger;
    private readonly List<IValidationRule> _rules = new();

    public CommandValidator(ILogger<CommandValidator> logger)
    {
        _logger = logger;
        RegisterDefaultRules();
    }

    /// <summary>
    /// Register a validation rule.
    /// </summary>
    public void RegisterRule(IValidationRule rule)
    {
        _rules.Add(rule);
        _logger.LogDebug("Registered validation rule: {Name}", rule.Name);
    }

    /// <summary>
    /// Validate an AI decision against the current game state.
    /// Returns a validated result with any modifications or rejections.
    /// </summary>
    public ValidationResult Validate(AIDecision decision, GameStateSnapshot gameState)
    {
        if (!decision.IsValid)
            return ValidationResult.Rejected("AI decision marked as invalid");

        foreach (var rule in _rules.OrderBy(r => r.Priority))
        {
            var result = rule.Validate(decision, gameState);
            if (!result.IsApproved)
            {
                _logger.LogWarning("Action '{Action}' rejected by rule '{Rule}': {Reason}",
                    decision.Action, rule.Name, result.Reason);
                return result;
            }
        }

        _logger.LogDebug("Action '{Action}' passed all validation rules", decision.Action);
        return ValidationResult.Approved();
    }

    private void RegisterDefaultRules()
    {
        RegisterRule(new DeathPreventionRule());
        RegisterRule(new PositionSafetyRule());
        RegisterRule(new HealthThresholdRule());
        RegisterRule(new CombatSafetyRule());
        RegisterRule(new ConfidenceThresholdRule());
        RegisterRule(new ActionExistsRule());
    }
}

/// <summary>
/// Interface for validation rules.
/// </summary>
public interface IValidationRule
{
    string Name { get; }
    int Priority { get; }
    ValidationResult Validate(AIDecision decision, GameStateSnapshot gameState);
}

/// <summary>
/// Result of a validation check.
/// </summary>
public record ValidationResult
{
    public bool IsApproved { get; init; }
    public string Reason { get; init; } = string.Empty;
    public string? SuggestedAction { get; init; }
    
    public static ValidationResult Approved() => new() { IsApproved = true };
    public static ValidationResult Rejected(string reason) => new() { IsApproved = false, Reason = reason };
    public static ValidationResult RedirectTo(string action, string reason) => 
        new() { IsApproved = false, Reason = reason, SuggestedAction = action };
}

// ==================== Default Validation Rules ====================

/// <summary>
/// Prevents actions that would be executed while dead.
/// </summary>
public class DeathPreventionRule : IValidationRule
{
    public string Name => "Death Prevention";
    public int Priority => 0;

    public ValidationResult Validate(AIDecision decision, GameStateSnapshot state)
    {
        if (state.IsDead && decision.Action != "resurrect" && decision.Action != "wait")
        {
            return ValidationResult.RedirectTo("wait", 
                "Character is dead. Only resurrect or wait actions are valid.");
        }
        return ValidationResult.Approved();
    }
}

/// <summary>
/// Ensures HP is above a safe threshold before aggressive actions.
/// </summary>
public class HealthThresholdRule : IValidationRule
{
    public string Name => "Health Threshold";
    public int Priority => 10;
    
    private const double CriticalHPThreshold = 20.0;
    private const double LowHPThreshold = 40.0;

    private static readonly HashSet<string> _aggressiveActions = new()
    {
        "attack", "pull_monster", "engage", "skill_attack", "aoe_attack"
    };

    public ValidationResult Validate(AIDecision decision, GameStateSnapshot state)
    {
        if (state.HPPercentage < CriticalHPThreshold && _aggressiveActions.Contains(decision.Action))
        {
            return ValidationResult.RedirectTo("heal",
                $"HP is critically low ({state.HPPercentage:F1}%). Must heal before attacking.");
        }
        
        if (state.HPPercentage < LowHPThreshold && decision.Action == "pull_monster")
        {
            return ValidationResult.RedirectTo("heal",
                $"HP too low ({state.HPPercentage:F1}%) to pull new monsters.");
        }
        
        return ValidationResult.Approved();
    }
}

/// <summary>
/// Validates position safety - prevents walking into dangerous areas.
/// </summary>
public class PositionSafetyRule : IValidationRule
{
    public string Name => "Position Safety";
    public int Priority => 20;

    public ValidationResult Validate(AIDecision decision, GameStateSnapshot state)
    {
        // If moving, check that destination is reasonable
        if (decision.Action == "move" && decision.Parameters.TryGetValue("distance", out var dist))
        {
            if (dist is double distance && distance > 1000)
            {
                return ValidationResult.Rejected(
                    $"Movement distance ({distance}) exceeds safety limit. Potential hallucinated coordinates.");
            }
        }
        return ValidationResult.Approved();
    }
}

/// <summary>
/// Prevents engagement in combat when conditions are unsafe.
/// </summary>
public class CombatSafetyRule : IValidationRule
{
    public string Name => "Combat Safety";
    public int Priority => 15;

    public ValidationResult Validate(AIDecision decision, GameStateSnapshot state)
    {
        // Don't engage if already in combat with too many entities
        if (state.IsInCombat && decision.Action == "pull_monster" && state.NearbyEntityCount > 5)
        {
            return ValidationResult.Rejected(
                $"Already in combat with {state.NearbyEntityCount} nearby entities. Too dangerous to pull more.");
        }
        return ValidationResult.Approved();
    }
}

/// <summary>
/// Rejects decisions with low confidence scores.
/// </summary>
public class ConfidenceThresholdRule : IValidationRule
{
    public string Name => "Confidence Threshold";
    public int Priority => 5;
    
    private const double MinConfidence = 0.3;

    public ValidationResult Validate(AIDecision decision, GameStateSnapshot state)
    {
        if (decision.Confidence < MinConfidence)
        {
            return ValidationResult.RedirectTo("wait",
                $"AI confidence ({decision.Confidence:F2}) below threshold ({MinConfidence}). Defaulting to safe action.");
        }
        return ValidationResult.Approved();
    }
}

/// <summary>
/// Validates that the action is a known/supported action.
/// </summary>
public class ActionExistsRule : IValidationRule
{
    public string Name => "Action Exists";
    public int Priority => 1;
    
    private static readonly HashSet<string> _validActions = new()
    {
        "attack", "heal", "buff", "move", "wait", "loot",
        "pull_monster", "skill_attack", "aoe_attack", "flee",
        "resurrect", "use_potion", "equip", "sell", "buy",
        "teleport", "party_join", "party_leave", "none"
    };

    public ValidationResult Validate(AIDecision decision, GameStateSnapshot state)
    {
        if (!_validActions.Contains(decision.Action.ToLowerInvariant()))
        {
            return ValidationResult.RedirectTo("wait",
                $"Unknown action '{decision.Action}'. Not in valid action set.");
        }
        return ValidationResult.Approved();
    }
}