| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 8 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 9 | ) |
| 10 | |
| 11 | func infrastructureOnly(err error, r *attemptState) bool { |
| 12 | if joined, ok := err.(interface{ Unwrap() []error }); ok { |
| 13 | for _, cause := range joined.Unwrap() { |
| 14 | if !infrastructureOnly(cause, r) { |
| 15 | return false |
| 16 | } |
| 17 | } |
| 18 | return true |
| 19 | } |
| 20 | if wrapped, ok := err.(interface{ Unwrap() error }); ok { |
| 21 | return infrastructureOnly(wrapped.Unwrap(), r) |
| 22 | } |
| 23 | switch err { |
| 24 | case context.Canceled, context.DeadlineExceeded: |
| 25 | return r.ServiceInterrupted |
| 26 | case ErrTransient: |
| 27 | return true |
| 28 | case provider.ErrClosed: |
| 29 | switch r.Phase { |
| 30 | case "claimed", "creating-workspace", "workspace": |
| 31 | return true |
| 32 | } |
| 33 | } |
| 34 | return false |
| 35 | } |
| 36 | |
| 37 | func attemptFailureReason(err error) string { |
| 38 | var refusal *RefusalError |
| 39 | if errors.As(err, &refusal) { |
| 40 | return refusal.Reason |
| 41 | } |
| 42 | for _, item := range []struct { |
| 43 | err error |
| 44 | reason string |
| 45 | }{ |
| 46 | {provider.ErrAccountMismatch, "account_mismatch"}, |
| 47 | {ErrPromptTooLarge, "runner.prompt_too_large"}, {team.ErrEvidence, "runner.evidence"}, |
| 48 | {team.ErrBudget, "runner.budget"}, {team.ErrQuota, "runner.quota"}, {team.ErrReplayUnsafe, "runner.replay"}, |
| 49 | } { |
| 50 | if errors.Is(err, item.err) { |
| 51 | return item.reason |
| 52 | } |
| 53 | } |
| 54 | if joined, ok := err.(interface{ Unwrap() []error }); ok { |
| 55 | for _, cause := range joined.Unwrap() { |
| 56 | if !errors.Is(cause, context.Canceled) && !errors.Is(cause, context.DeadlineExceeded) { |
| 57 | return attemptFailureReason(cause) |
| 58 | } |
| 59 | } |
| 60 | causes := joined.Unwrap() |
| 61 | if len(causes) > 0 { |
| 62 | return attemptFailureReason(causes[0]) |
| 63 | } |
| 64 | } |
| 65 | reason, _ := Diagnostic(err) |
| 66 | return reason |
| 67 | } |
| 68 | |