Files
pipelines/internal/stage/stage.go
T

93 lines
2.5 KiB
Go

// Package stage owns type-specific validation and execution. Pipeline parsing,
// graph routing, persistence, and presentation belong to their respective layers.
package stage
import (
"context"
"fmt"
)
// Values are runtime data, not the prose output contract in a definition.
type Values map[string]any
type Definition struct {
ReportFileArtifacts bool
Key string
Type string
Blocks map[string]string
Configs map[string]string
Edges map[string]string
}
// Result selects one declared outcome. The future graph runner owns resolving
// its destination and routing execution errors through the mandatory error edge.
type Result struct {
Outcome string `json:"outcome"`
Output Values `json:"output"`
}
// Stage implementations must be safe to share between concurrent runs. Definitions
// and input maps are caller-owned: validation and execution must not mutate them.
// Run returns an error for execution failures, not a fabricated success result.
type Stage interface {
Validate(Definition) error
Run(context.Context, Definition, Values) (Result, error)
}
// Registry is immutable after construction. Adding a type requires an implementation
// here, not another branch in the Markdown parser or graph validator.
type Registry struct {
types map[string]Stage
}
func NewRegistry(types map[string]Stage) *Registry {
copy := make(map[string]Stage, len(types))
for name, implementation := range types {
copy[name] = implementation
}
return &Registry{types: copy}
}
func DefaultRegistry() *Registry {
return NewRegistry(map[string]Stage{ModelType: ModelStage{}, NtfyType: NtfyStage{}})
}
func EffectiveType(name string) string {
if name == "" {
return ModelType
}
return name
}
func (r *Registry) Resolve(name string) (Stage, error) {
name = EffectiveType(name)
if r != nil {
if implementation := r.types[name]; implementation != nil {
return implementation, nil
}
}
return nil, fmt.Errorf("unsupported stage type %q", name)
}
func (r *Registry) Validate(def Definition) error {
implementation, err := r.Resolve(def.Type)
if err != nil {
return err
}
return implementation.Validate(def)
}
func (r *Registry) Run(ctx context.Context, def Definition, input Values) (Result, error) {
if err := ctx.Err(); err != nil {
return Result{}, err
}
implementation, err := r.Resolve(def.Type)
if err != nil {
return Result{}, err
}
if err := implementation.Validate(def); err != nil {
return Result{}, err
}
return implementation.Run(ctx, def, input)
}