summaryrefslogtreecommitdiff
path: root/internal/task/validator.go
blob: 003fab94199e44060618beacd22c36d7de77a3a6 (plain)
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
package task

import (
	"fmt"
	"strings"
)

// ValidationError collects multiple validation failures.
type ValidationError struct {
	Errors []string
}

func (e *ValidationError) Error() string {
	return fmt.Sprintf("validation failed: %s", strings.Join(e.Errors, "; "))
}

func (e *ValidationError) Add(msg string) {
	e.Errors = append(e.Errors, msg)
}

func (e *ValidationError) HasErrors() bool {
	return len(e.Errors) > 0
}

// Validate checks a task for required fields and valid values.
func Validate(t *Task) error {
	ve := &ValidationError{}

	if t.Name == "" {
		ve.Add("name is required")
	}
	if t.Agent.Instructions == "" {
		ve.Add("agent.instructions is required")
	}
	if t.Agent.MaxBudgetUSD < 0 {
		ve.Add("agent.max_budget_usd must be non-negative")
	}
	if t.Timeout.Duration < 0 {
		ve.Add("timeout must be non-negative")
	}
	if t.Retry.MaxAttempts < 1 {
		ve.Add("retry.max_attempts must be at least 1")
	}
	if t.Retry.Backoff != "" && t.Retry.Backoff != "linear" && t.Retry.Backoff != "exponential" {
		ve.Add("retry.backoff must be 'linear' or 'exponential'")
	}
	validPriorities := map[Priority]bool{PriorityHigh: true, PriorityNormal: true, PriorityLow: true}
	if t.Priority != "" && !validPriorities[t.Priority] {
		ve.Add(fmt.Sprintf("invalid priority %q; must be high, normal, or low", t.Priority))
	}
	if t.Agent.PermissionMode != "" {
		validModes := map[string]bool{
			"default": true, "acceptEdits": true, "bypassPermissions": true,
			"plan": true, "dontAsk": true, "delegate": true,
		}
		if !validModes[t.Agent.PermissionMode] {
			ve.Add(fmt.Sprintf("invalid permission_mode %q", t.Agent.PermissionMode))
		}
	}

	if ve.HasErrors() {
		return ve
	}
	return nil
}