Files
notarius/internal/preflight/preflight.go

136 lines
5.8 KiB
Go

// Package preflight checks statically known native resources for an already
// resolved pipeline. Acquisition, construction, and presentation belong to callers.
package preflight
import (
"context"
"errors"
"fmt"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
"go.maximumdirect.net/promptkit"
)
// SelectionError identifies an absent configured native resource. Callers can
// distinguish this invalid selection from unavailable inspection or cancellation
// without inspecting diagnostic text or the kit's private lookup causes.
type SelectionError struct{ Cause error }
func (err *SelectionError) Error() string { return err.Cause.Error() }
func (err *SelectionError) Unwrap() error { return err.Cause }
func resourceFinding(err error) error {
if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) && (errors.Is(err, promptkit.ErrPromptNotFound) || errors.Is(err, promptkit.ErrProfileNotFound)) {
return &SelectionError{Cause: err}
}
return err
}
// RequiresEngine reports selected model work, including nested validators.
// Deterministic pipelines need no engine merely to validate their composition.
func RequiresEngine(resolved pipeline.ResolvedPipeline) bool {
if resolved.InputExecutionClass == contracts.ExecutionClassLLMBacked || resolved.ChunkExecutionClass == contracts.ExecutionClassLLMBacked || resolved.OutputExecutionClass == contracts.ExecutionClassLLMBacked {
return true
}
for _, step := range resolved.Steps {
for _, lane := range step.ArtifactLanes {
if lane.ExtractExecutionClass == contracts.ExecutionClassLLMBacked || lane.MergeExecutionClass == contracts.ExecutionClassLLMBacked || lane.NormalizeExecutionClass == contracts.ExecutionClassLLMBacked {
return true
}
}
}
for _, chain := range resolved.ValidatorChains {
for _, validator := range chain.Validators {
if validator.ExecutionClass == contracts.ExecutionClassLLMBacked {
return true
}
}
}
return false
}
// Validate checks the same declarations and resource selections used by native
// request builders. It does not render, prepare, read input/reference files,
// resolve credential values, or call providers. Dynamic selections remain runtime
// checks. Callers first use their ordinary application resolver for module,
// options, type, validator-chain, and ordered-reference compatibility.
func Validate(ctx context.Context, resolved pipeline.ResolvedPipeline, catalog pipeline.ModuleCatalog, engine *promptkit.Engine) error {
if ctx == nil {
return errors.New("preflight context must not be nil")
}
if err := ctx.Err(); err != nil {
return err
}
var findings []error
check := func(stage pipeline.ModuleStage, step, lane, module, validator string, class contracts.ExecutionClass, profile string, prompts []string) {
if class != contracts.ExecutionClassLLMBacked || ctx.Err() != nil {
return
}
scope := fmt.Sprintf("pipeline %q", resolved.ID)
if step != "" {
scope += fmt.Sprintf(" step %q", step)
}
if lane != "" {
scope += fmt.Sprintf(" lane %q", lane)
}
scope += fmt.Sprintf(" %s module %q", stage, module)
if validator != "" {
scope += fmt.Sprintf(" validator %q", validator)
}
if engine == nil {
findings = append(findings, fmt.Errorf("%s: %w: native engine is required", scope, promptkit.ErrInvalidConfig))
return
}
if profile != "" {
if _, err := engine.InspectProfile(ctx, profile); err != nil {
findings = append(findings, resourceFinding(fmt.Errorf("%s profile %q: %w", scope, profile, err)))
}
}
for _, id := range prompts {
if ctx.Err() != nil {
return
}
prompt, err := engine.InspectPrompt(ctx, id)
if err != nil {
findings = append(findings, resourceFinding(fmt.Errorf("%s prompt %q: %w", scope, id, err)))
continue
}
if profile != "" {
continue
}
if prompt.DefaultProfileID == nil || *prompt.DefaultProfileID == "" {
// A module may select a profile dynamically when constructing its
// actual request. No declared selection is not a missing resource.
continue
}
if _, err := engine.InspectProfile(ctx, *prompt.DefaultProfileID); err != nil {
findings = append(findings, resourceFinding(fmt.Errorf("%s prompt %q default profile %q: %w", scope, id, *prompt.DefaultProfileID, err)))
}
}
}
input, _ := catalog.Inputs.Spec(resolved.Input.Module)
check(pipeline.StageInput, "", "", resolved.Input.Module, "", resolved.InputExecutionClass, resolved.Input.LLMProfile, input.Prompts)
check(pipeline.StageChunk, "", "", resolved.Chunk.Module, "", resolved.ChunkExecutionClass, resolved.Chunk.LLMProfile, resolved.ChunkPrompts)
steps := make(map[string]string)
for _, step := range resolved.Steps {
for _, lane := range step.ArtifactLanes {
steps[lane.ID] = step.ID
check(pipeline.StageExtract, step.ID, lane.ID, lane.Extract.Module, "", lane.ExtractExecutionClass, lane.Extract.LLMProfile, lane.ExtractPrompts)
check(pipeline.StageMerge, step.ID, lane.ID, lane.Merge.Module, "", lane.MergeExecutionClass, lane.Merge.LLMProfile, lane.MergePrompts)
check(pipeline.StageNormalize, step.ID, lane.ID, lane.Normalize.Module, "", lane.NormalizeExecutionClass, lane.Normalize.LLMProfile, lane.NormalizePrompts)
}
}
for _, chain := range resolved.ValidatorChains {
for _, validator := range chain.Validators {
check(chain.Stage, steps[chain.LaneID], chain.LaneID, chain.ModuleKey, validator.Binding.Module, validator.ExecutionClass, validator.Binding.LLMProfile, validator.Prompts)
}
}
output, _ := catalog.Outputs.Spec(resolved.Output.Module)
check(pipeline.StageOutput, "", "", resolved.Output.Module, "", resolved.OutputExecutionClass, resolved.Output.LLMProfile, output.Prompts)
if err := ctx.Err(); err != nil {
findings = append(findings, err)
}
return errors.Join(findings...)
}