Files
alan 72aecd8b1f Remove resource pools, leases and wait queues
Drop the resource allocation feature end to end: store tables and migration, runner scheduling and cleanup, protocol types and commands, CLI commands, TUI screens, tests and docs. Existing databases open cleanly and definitions carrying a retired resources block still validate.
2026-10-19 22:27:27 +01:00

101 lines
3.0 KiB
Go
Raw Permalink 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 {
// A resources block from a definition created before the feature was removed
// is ignored: it no longer declares scheduling or validation.
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 validateControl(s Stage, stages map[string]bool) error {
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
}