package pipeline import ( "fmt" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/stage" "regexp" "strings" ) var identifier = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9_-]*$`) type Stage = protocol.Stage type Graph = protocol.Graph func StageNames(text string) ([]string, error) { names := strings.Split(text, ",") seen := map[string]bool{} for i := range names { names[i] = strings.TrimSpace(names[i]) if !identifier.MatchString(names[i]) || seen[names[i]] { return nil, fmt.Errorf("stage names must be unique identifiers (letters, digits, _ or -; start with a letter)") } seen[names[i]] = true } return names, nil } func Template(names []string, linear bool) string { var b strings.Builder for i, name := range names { next := protocol.SuccessEnd if linear && i+1 < len(names) { next = names[i+1] } fmt.Fprintf(&b, "# stage %s\n\n```output\nDescribe the output map.\n```\n\n```prompt\nDescribe the task.\n```\n\n```tools\n```\n\n```edges\nsuccess -> %s\nerror -> %s\n```\n\n```configs\ntype=model\nprovider=opencode-go\nmodel=deepseek-v4.1-flash\nretries=2\ntimeout=1h\n```\n\n", name, next, protocol.FailureEnd) } return b.String() } // Parse validates definitions without executing user code or contacting providers. func Parse(source string) (Graph, error) { return ParseWithRegistry(source, stage.DefaultRegistry()) } // ParseWithRegistry applies the same graph rules to independently validated types. func ParseWithRegistry(source string, registry *stage.Registry) (Graph, error) { if len(source) > 1<<20 { return Graph{}, fmt.Errorf("source exceeds 1 MiB") } var graph Graph var err error source, graph.Settings, err = parseSettings(source) if err != nil { return Graph{}, err } var fields map[string]string active := "" var content []string finish := func() error { if fields == nil { return nil } s := &graph.Stages[len(graph.Stages)-1] if strings.TrimSpace(fields["edges"]) == "" { return fmt.Errorf("stage %s: required block edges is missing or empty", s.Key) } if _, exists := fields["input"]; exists { return fmt.Errorf("stage %s: input blocks are no longer supported; input is supplied automatically", s.Key) } s.Output, s.Prompt = fields["output"], fields["prompt"] if err := parseControl(s, fields); err != nil { return fmt.Errorf("stage %s: %w", s.Key, err) } if text, exists := fields["notify"]; exists { var err error s.Notify, err = parseNotify(text) if err != nil { return fmt.Errorf("stage %s: %w", s.Key, err) } delete(fields, "notify") } s.Configs = map[string]string{} for _, line := range strings.Split(fields["configs"], "\n") { if strings.TrimSpace(line) == "" { continue } k, v, ok := strings.Cut(line, "=") k, v = strings.TrimSpace(k), strings.TrimSpace(v) if !ok || !identifier.MatchString(k) || v == "" { return fmt.Errorf("stage %s: invalid config %q", s.Key, line) } if _, exists := s.Configs[k]; exists { return fmt.Errorf("stage %s: duplicate config %s", s.Key, k) } s.Configs[k] = v } s.Type = stage.EffectiveType(s.Configs["type"]) if !identifier.MatchString(s.Type) { return fmt.Errorf("stage %s: invalid stage type %q", s.Key, s.Type) } delete(s.Configs, "type") for name, content := range fields { switch name { case "output", "prompt", "edges", "configs": default: if s.Blocks == nil { s.Blocks = map[string]string{} } s.Blocks[name] = content } } s.Edges = map[string]string{} for _, line := range strings.Split(fields["edges"], "\n") { if strings.TrimSpace(line) == "" { continue } outcome, target, ok := strings.Cut(line, "->") outcome, target = strings.TrimSpace(outcome), strings.TrimSpace(target) if !ok || !identifier.MatchString(outcome) || (target != "#" && !protocol.IsTerminal(target) && !identifier.MatchString(target)) { return fmt.Errorf("stage %s: invalid edge %q", s.Key, line) } if _, exists := s.Edges[outcome]; exists { return fmt.Errorf("stage %s: duplicate outcome %s", s.Key, outcome) } s.Edges[outcome] = protocol.ResolveTarget(outcome, target) } if _, ok := s.Edges["error"]; !ok { s.Edges["error"] = protocol.FailureEnd s.FallbackError = true } if err := registry.Validate(stage.Definition{Key: s.Key, Type: s.Type, Blocks: fields, Configs: s.Configs, Edges: s.Edges}); err != nil { return fmt.Errorf("stage %s: %w", s.Key, err) } return nil } seen := map[string]bool{} for i, line := range strings.Split(strings.ReplaceAll(source, "\r\n", "\n"), "\n") { trim := strings.TrimSpace(line) if active != "" { if trim == "```" { fields[active] = strings.Join(content, "\n") active = "" content = nil } else { content = append(content, line) } continue } if strings.HasPrefix(trim, "# stage ") { if err := finish(); err != nil { return Graph{}, err } name := strings.TrimSpace(strings.TrimPrefix(trim, "# stage ")) if !identifier.MatchString(name) || seen[name] { return Graph{}, fmt.Errorf("line %d: invalid or duplicate stage %q", i+1, name) } seen[name] = true graph.Stages = append(graph.Stages, Stage{Key: name}) fields = map[string]string{} } else if strings.HasPrefix(trim, "```") { block := strings.TrimPrefix(trim, "```") if fields == nil || !identifier.MatchString(block) { return Graph{}, fmt.Errorf("line %d: unexpected block %q", i+1, block) } if _, exists := fields[block]; exists { return Graph{}, fmt.Errorf("line %d: duplicate block %s", i+1, block) } active = block } else if trim != "" { return Graph{}, fmt.Errorf("line %d: unexpected text outside a block", i+1) } } if active != "" { return Graph{}, fmt.Errorf("unclosed %s block", active) } if err := finish(); err != nil { return Graph{}, err } if len(graph.Stages) == 0 { return Graph{}, fmt.Errorf("at least one stage is required") } for _, s := range graph.Stages { if err := validateControl(s, seen); err != nil { return Graph{}, err } for _, target := range s.Edges { if !protocol.IsTerminal(target) && !seen[target] { return Graph{}, fmt.Errorf("stage %s: unknown edge target %s", s.Key, target) } } } reachable := map[string]bool{} byKey := map[string]Stage{} for _, stage := range graph.Stages { byKey[stage.Key] = stage } var visit func(string) visit = func(k string) { if protocol.IsTerminal(k) || reachable[k] { return } reachable[k] = true for _, target := range byKey[k].Edges { visit(target) } } visit(graph.Stages[0].Key) for _, stage := range graph.Stages { if !reachable[stage.Key] { return Graph{}, fmt.Errorf("stage %s: unreachable from entry stage %s", stage.Key, graph.Stages[0].Key) } } canEnd := map[string]bool{protocol.SuccessEnd: true, protocol.FailureEnd: true} for changed := true; changed; { changed = false for _, stage := range graph.Stages { if canEnd[stage.Key] { continue } for _, target := range stage.Edges { if canEnd[target] { canEnd[stage.Key] = true changed = true break } } } } for _, stage := range graph.Stages { if !canEnd[stage.Key] { return Graph{}, fmt.Errorf("stage %s: no path to termination", stage.Key) } } return graph, nil }