Files
pipelines/internal/pipeline/control.go
T

116 lines
3.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package pipeline
import (
"encoding/json"
"fmt"
"io"
"strings"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func decodeControl(text string, value any) error {
// Control blocks are flat objects. Reject duplicate keys before decoding.
keys := json.NewDecoder(strings.NewReader(text))
if token, err := keys.Token(); err != nil || token != json.Delim('{') {
return fmt.Errorf("expected object")
}
seen := map[string]bool{}
for keys.More() {
token, err := keys.Token()
if err != nil {
return err
}
key, ok := token.(string)
if !ok || seen[key] {
return fmt.Errorf("duplicate key %v", token)
}
seen[key] = true
var raw json.RawMessage
if err := keys.Decode(&raw); err != nil {
return err
}
}
decoder := json.NewDecoder(strings.NewReader(text))
decoder.DisallowUnknownFields()
if err := decoder.Decode(value); err != nil {
return err
}
var extra any
if err := decoder.Decode(&extra); err != io.EOF {
return fmt.Errorf("expected one JSON value")
}
return nil
}
func parseControl(s *Stage, fields map[string]string) error {
if text, ok := fields["resources"]; ok {
if err := decodeControl(text, &s.Resources); err != nil || s.Resources == nil || !validStageResources(*s.Resources) {
return fmt.Errorf("resources must contain exactly one exclusive or pool key (1–128 ASCII letters, digits, _ or -; start with a letter)")
}
delete(fields, "resources")
}
if text, ok := fields["limits"]; ok {
if err := decodeControl(text, &s.Limits); err != nil || s.Limits == nil {
return fmt.Errorf("limits must be an object with max_visits and optional on outcomes")
}
if s.Limits.MaxVisits < 1 || s.Limits.MaxVisits > 5 {
return fmt.Errorf("max_visits must be from 1 to 5")
}
seen := map[string]bool{}
for _, outcome := range s.Limits.On {
if !identifier.MatchString(outcome) || seen[outcome] {
return fmt.Errorf("invalid/duplicate limit outcome %q", outcome)
}
seen[outcome] = true
}
delete(fields, "limits")
}
if text, ok := fields["memory"]; ok {
if err := decodeControl(text, &s.Memory); err != nil || len(s.Memory) == 0 || len(s.Memory) > 50 {
return fmt.Errorf("memory must map 1–50 input names to input.FIELD or STAGE.FIELD")
}
for alias, reference := range s.Memory {
owner, field, ok := strings.Cut(strings.TrimSuffix(reference, "?"), ".")
if !identifier.MatchString(alias) || !ok || !identifier.MatchString(owner) || !identifier.MatchString(field) {
return fmt.Errorf("invalid memory reference %q: %q", alias, reference)
}
}
delete(fields, "memory")
}
return nil
}
func validStageResources(resources protocol.StageResources) bool {
return (ValidResourceKey(resources.Exclusive) && resources.Pool == "") ||
(resources.Exclusive == "" && ValidResourceKey(resources.Pool))
}
func validateControl(s Stage, stages map[string]bool) error {
if s.Resources != nil && s.Type == "human" {
return fmt.Errorf("stage %s: human stages cannot declare resources", s.Key)
}
if s.Limits != nil {
for _, outcome := range s.Limits.On {
target, exists := s.Edges[outcome]
if !exists || protocol.IsTerminal(target) {
return fmt.Errorf("stage %s: limit outcome %q must name a nonterminal edge", s.Key, outcome)
}
}
}
for _, ref := range s.Memory {
owner, _, _ := strings.Cut(ref, ".")
if owner != "input" && !stages[owner] {
return fmt.Errorf("stage %s: unknown memory source %s", s.Key, owner)
}
if owner == s.Key {
return fmt.Errorf("stage %s: self-memory is not allowed; use another stage's explicit handoff", s.Key)
}
}
return nil
}
func ValidResourceKey(key string) bool {
return len(key) <= 128 && identifier.MatchString(key)
}