Files
pipelines/internal/stage/stage.go
T
alan 217e11da1d Merge branch 'main' into turn-integration
# Conflicts:
#	internal/server/runner.go
#	internal/stage/model_prompt.go
#	internal/stage/model_run.go
#	internal/stage/model_test.go
2026-10-19 22:32:13 +01:00

110 lines
3.1 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
// TurnPaths is the runtime-provided file layout for one model visit. Input and
// memory are read by the model; Output is written by the server from the model's
// returned markdown. Private belongs to this visit; Public is shared by every
// stage in the run.
type TurnPaths struct {
Dir string
Input string
Memory string
Output string
Private string
Public string
}
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
Turn TurnPaths
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)
}