| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "regexp" |
| 5 | "strings" |
| 6 | |
| 7 | "reasonix/internal/provider" |
| 8 | ) |
| 9 | |
| 10 | var exitCodePattern = regexp.MustCompile(`(?i)exit (status|code)[:= ]+(-?\d+)`) |
| 11 | |
| 12 | func errorCategory(toolName, errMsg string) string { |
| 13 | msg := firstLine(errMsg) |
| 14 | if strings.HasPrefix(msg, "argument_validation:") { |
| 15 | return msg |
| 16 | } |
| 17 | if m := exitCodePattern.FindStringSubmatch(msg); len(m) == 3 { |
| 18 | return toolName + ":exit:" + m[2] |
| 19 | } |
| 20 | lower := strings.ToLower(msg) |
| 21 | switch { |
| 22 | case strings.Contains(lower, "timeout") || strings.Contains(lower, "deadline"): |
| 23 | return toolName + ":transient:timeout" |
| 24 | case strings.Contains(lower, "connection"): |
| 25 | return toolName + ":transient:connection" |
| 26 | case strings.Contains(lower, "fail") && strings.Contains(lower, "test"): |
| 27 | return toolName + ":test_failure" |
| 28 | } |
| 29 | return toolName + ":" + msg |
| 30 | } |
| 31 | |
| 32 | func consecutiveNormalizedFailure(calls []provider.ToolCall, outcomes []toolOutcome, loop *turnLoopState) bool { |
| 33 | if loop == nil { |
| 34 | return false |
| 35 | } |
| 36 | categories := map[string]int{} |
| 37 | for i := 0; i < len(calls) && i < len(outcomes); i++ { |
| 38 | if strings.TrimSpace(outcomes[i].errMsg) == "" { |
| 39 | continue |
| 40 | } |
| 41 | name := calls[i].Name |
| 42 | if calls[i].ResolvedName != "" { |
| 43 | name = calls[i].ResolvedName |
| 44 | } |
| 45 | category := errorCategory(name, outcomes[i].errMsg) |
| 46 | if strings.Contains(category, ":exit:") || strings.Contains(category, ":test_failure") { |
| 47 | categories[category]++ |
| 48 | } |
| 49 | } |
| 50 | return loop.advanceErrorCategories(categories) |
| 51 | } |
| 52 |