9130ae8c7a
Add durable FIFO capacity-one resource leases for executable stages, including cleanup-confirmed release, conservative restart quarantine, queue state events, and CLI/TUI/Android waiting presentation. Add scheduler, cleanup, protocol, client, and Android coverage plus a proposed Android v3 resource draft. Checked packaged templates and authoring skill references: neither needs updating in this item. The per-item docs note records the shared documentation that docs-fold must update; no shared README or packaged reference was edited.
111 lines
3.4 KiB
Go
111 lines
3.4 KiB
Go
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 || !ValidResourceKey(s.Resources.Exclusive) {
|
||
return fmt.Errorf("resources must contain exactly one exclusive 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 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)
|
||
}
|