Files
pipelines/internal/stage/stage.go
T

122 lines
3.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"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
type resourceTargetContextKey struct{}
// WithResourceTarget attaches the server-granted concrete target to a stage's
// runtime context. It is deliberately separate from model input and contains
// no capability or credential material.
func WithResourceTarget(ctx context.Context, target *protocol.ResourceTarget) context.Context {
if target == nil {
return ctx
}
copy := &protocol.ResourceTarget{Member: target.Member}
if target.Metadata != nil {
copy.Metadata = map[string]string{}
for key, value := range target.Metadata {
copy.Metadata[key] = value
}
}
return context.WithValue(ctx, resourceTargetContextKey{}, copy)
}
func ResourceTarget(ctx context.Context) (*protocol.ResourceTarget, bool) {
target, ok := ctx.Value(resourceTargetContextKey{}).(*protocol.ResourceTarget)
return target, ok
}
// 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
// ArtifactsDir is the per-run/step directory the runtime created for this
// visit; empty when the runner did not provide one (for example in tests).
ArtifactsDir 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{}, HumanType: HumanStage{}})
}
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)
}