| | | 1 | | namespace NexusLabs.Needlr.AgentFramework.Iterative; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Configuration for iterative loop stall detection. |
| | | 5 | | /// </summary> |
| | | 6 | | /// <remarks> |
| | | 7 | | /// A "stall" is detected when <see cref="ConsecutiveThreshold"/> consecutive |
| | | 8 | | /// iterations produce total token counts within <see cref="TolerancePercent"/> |
| | | 9 | | /// of each other. This is a strong signal the LLM is repeating the same work |
| | | 10 | | /// because it has no cross-iteration memory of what it already did. |
| | | 11 | | /// </remarks> |
| | | 12 | | public sealed class StallDetectionOptions |
| | | 13 | | { |
| | 4 | 14 | | private int _consecutiveThreshold = 3; |
| | 4 | 15 | | private double _tolerancePercent = 0.10; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// The number of consecutive iterations with similar token counts before |
| | | 19 | | /// terminating. Defaults to <c>3</c>. Minimum <c>2</c>. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <exception cref="ArgumentOutOfRangeException">Value is less than 2.</exception> |
| | | 22 | | public int ConsecutiveThreshold |
| | | 23 | | { |
| | 13 | 24 | | get => _consecutiveThreshold; |
| | | 25 | | set |
| | | 26 | | { |
| | 4 | 27 | | if (value < 2) |
| | 0 | 28 | | throw new ArgumentOutOfRangeException(nameof(value), "ConsecutiveThreshold must be at least 2."); |
| | 4 | 29 | | _consecutiveThreshold = value; |
| | 4 | 30 | | } |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// How close token counts must be (as a fraction of the previous iteration's |
| | | 35 | | /// count) to be considered "similar." Defaults to <c>0.10</c> (10%). |
| | | 36 | | /// </summary> |
| | | 37 | | /// <remarks> |
| | | 38 | | /// A tolerance of 0.10 means iterations with 15,000 and 16,200 tokens are |
| | | 39 | | /// considered similar (8% difference), but 15,000 and 18,000 are not (20%). |
| | | 40 | | /// </remarks> |
| | | 41 | | /// <exception cref="ArgumentOutOfRangeException">Value is negative or ≥ 1.0.</exception> |
| | | 42 | | public double TolerancePercent |
| | | 43 | | { |
| | 17 | 44 | | get => _tolerancePercent; |
| | | 45 | | set |
| | | 46 | | { |
| | 4 | 47 | | if (value is < 0.0 or >= 1.0) |
| | 0 | 48 | | throw new ArgumentOutOfRangeException(nameof(value), "TolerancePercent must be between 0.0 (inclusive) a |
| | 4 | 49 | | _tolerancePercent = value; |
| | 4 | 50 | | } |
| | | 51 | | } |
| | | 52 | | } |