Files
pipelines/internal/stage/stage.go
T
alan 538143554c Split stage memory into capped essential memory.md and full history.md
Every stage used to be told to read memory.md, the full output of every
earlier turn (19 KB average, 92 KB max), and paid for it on each model call.
memory.md is now essential memory, hard-capped at 4 KB and inlined in the
prompt: the task, the previous stage's # handoff section (or its truncated
output head), a one-line-per-stage outcome ledger and the server-captured
changed files. history.md keeps the old full memory on disk; read-capable
stages are pointed at it and input.md for on-demand detail, and it is never
inlined. The prompt asks each stage for a short # handoff section.
2026-11-04 13:00:48 +00:00

113 lines
3.3 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. Memory is
// the short essential memory the prompt inlines; Input and History are the full
// previous output and run history, read only on demand (History is empty for
// callers that predate it). 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
History 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)
}