Files
notarius/internal/framework/pipeline/validation_policy.go

118 lines
3.2 KiB
Go
Raw Normal View History

2026-08-26 23:34:06 +00:00
package pipeline
import (
"fmt"
"strings"
2026-08-26 23:34:06 +00:00
)
type ExecutionMode string
2026-08-26 23:34:06 +00:00
const (
ModeStrict ExecutionMode = "strict"
ModePermissive ExecutionMode = "permissive"
2026-08-26 23:34:06 +00:00
)
func ParseExecutionMode(value string) (ExecutionMode, error) {
mode := ExecutionMode(strings.TrimSpace(value))
if mode != ModeStrict && mode != ModePermissive {
return "", fmt.Errorf("mode must be strict or permissive")
}
return mode, nil
}
type CacheMode string
2026-08-26 23:34:06 +00:00
const (
CacheAuto CacheMode = "auto"
CacheBypass CacheMode = "bypass"
CacheRefresh CacheMode = "refresh"
2026-08-26 23:34:06 +00:00
)
func ParseCacheMode(value string) (CacheMode, error) {
mode := CacheMode(strings.TrimSpace(value))
switch mode {
case CacheAuto, CacheBypass, CacheRefresh:
return mode, nil
}
return "", fmt.Errorf("cache must be auto, bypass, or refresh")
2026-08-26 23:34:06 +00:00
}
// ProducerControls contains final operation controls; execution does not inherit settings.
type ProducerControls struct {
Mode ExecutionMode `json:"mode"`
MaxAdditionalTurns int `json:"max_additional_turns"`
Cache CacheMode `json:"cache"`
2026-08-26 23:34:06 +00:00
}
func ResolveProducerControls(binding ModuleBinding, profile PipelineProfile, invocationCache *CacheMode) (ProducerControls, error) {
result := ProducerControls{Mode: ModeStrict, MaxAdditionalTurns: 2, Cache: CacheAuto}
for _, mode := range []*ExecutionMode{profile.Mode, binding.Mode} {
if mode != nil {
parsed, err := ParseExecutionMode(string(*mode))
if err != nil {
return ProducerControls{}, err
}
result.Mode = parsed
2026-08-26 23:34:06 +00:00
}
}
for _, turns := range []*int{profile.MaxAdditionalTurns, binding.MaxAdditionalTurns} {
if turns != nil {
if *turns < 0 {
return ProducerControls{}, fmt.Errorf("max_additional_turns must not be negative")
}
result.MaxAdditionalTurns = *turns
2026-08-26 23:34:06 +00:00
}
}
for _, cache := range []*CacheMode{profile.Cache, binding.Cache, invocationCache} {
if cache != nil {
parsed, err := ParseCacheMode(string(*cache))
if err != nil {
return ProducerControls{}, err
}
result.Cache = parsed
2026-08-26 23:34:06 +00:00
}
}
return result, nil
2026-08-26 23:34:06 +00:00
}
func cloneControl[T any](value *T) *T {
if value == nil {
2026-08-26 23:34:06 +00:00
return nil
}
copy := *value
return &copy
2026-08-26 23:34:06 +00:00
}
func applyEffectiveProducerControls(resolved *ResolvedPipeline, profile PipelineProfile, invocationCache *CacheMode) error {
apply := func(binding ModuleBinding, scope string) (ProducerControls, error) {
controls, err := ResolveProducerControls(binding, profile, invocationCache)
if err != nil {
return ProducerControls{}, fmt.Errorf("pipeline %q %s: %w", resolved.ID, scope, err)
}
return controls, nil
2026-08-26 23:34:06 +00:00
}
var err error
resolved.ChunkControls, err = apply(resolved.Chunk, "chunk")
if err != nil {
return err
2026-08-26 23:34:06 +00:00
}
for stepIndex := range resolved.Steps {
for laneIndex := range resolved.Steps[stepIndex].ArtifactLanes {
lane := &resolved.Steps[stepIndex].ArtifactLanes[laneIndex]
lane.ExtractControls, err = apply(lane.Extract, fmt.Sprintf("lane %q extract", lane.ID))
if err != nil {
return err
}
lane.MergeControls, err = apply(lane.Merge, fmt.Sprintf("lane %q merge", lane.ID))
if err != nil {
return err
}
lane.NormalizeControls, err = apply(lane.Normalize, fmt.Sprintf("lane %q normalize", lane.ID))
if err != nil {
return err
}
}
2026-08-26 23:34:06 +00:00
}
return nil
2026-08-26 23:34:06 +00:00
}