97 lines
3.3 KiB
Go
97 lines
3.3 KiB
Go
// Package preflight prepares provider-independent processing inputs.
|
|
package preflight
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"slices"
|
|
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/core/config"
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/core/modulecatalog"
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/contracts"
|
|
)
|
|
|
|
// SelectionError indicates that resolved operator settings cannot be used for processing.
|
|
type SelectionError struct{ Cause error }
|
|
|
|
func (e *SelectionError) Error() string { return e.Cause.Error() }
|
|
func (e *SelectionError) Unwrap() error { return e.Cause }
|
|
|
|
// ProcessingPlan checks all resolved selections before acquiring outputs.
|
|
func ProcessingPlan(ctx context.Context, cfg *config.Config, inspectProfile func(context.Context, string) error) ([]contracts.ModuleRunSpec, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
if cfg == nil {
|
|
return nil, fmt.Errorf("application configuration is required")
|
|
}
|
|
label := func(index int, item config.Stage) string {
|
|
if item.ID != nil {
|
|
return fmt.Sprintf("stage %d (%s, id %q)", index+1, item.Module, *item.ID)
|
|
}
|
|
return fmt.Sprintf("stage %d (%s)", index+1, item.Module)
|
|
}
|
|
for index, item := range cfg.Pipeline.Stages {
|
|
if item.Module != modulecatalog.KeyGlossary {
|
|
continue
|
|
}
|
|
hasEntries := slices.ContainsFunc(item.Glossaries, func(name string) bool {
|
|
return len(cfg.GlossaryCollections[name].Entries) > 0
|
|
})
|
|
if !hasEntries {
|
|
return nil, &SelectionError{Cause: fmt.Errorf("%s has no glossary entries in selected collections %q", label(index, item), item.Glossaries)}
|
|
}
|
|
}
|
|
checkedProfiles := make(map[string]struct{})
|
|
for index, item := range cfg.Pipeline.Stages {
|
|
selections := []struct {
|
|
role string
|
|
profile *string
|
|
}{{"proposal", item.LLM.ProposalProfile}, {"validation", item.LLM.ValidationProfile}}
|
|
if item.LLM.CorrectionRepairAttempts > 0 {
|
|
selections = append(selections, struct {
|
|
role string
|
|
profile *string
|
|
}{"correction repair", item.LLM.CorrectionRepairProfile})
|
|
}
|
|
for _, selection := range selections {
|
|
if selection.profile == nil || *selection.profile == "" {
|
|
return nil, &SelectionError{Cause: fmt.Errorf("%s requires a %s profile", label(index, item), selection.role)}
|
|
}
|
|
if inspectProfile == nil {
|
|
return nil, fmt.Errorf("profile inspection is required")
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
id := *selection.profile
|
|
if _, ok := checkedProfiles[id]; ok {
|
|
continue
|
|
}
|
|
if err := inspectProfile(ctx, id); err != nil {
|
|
return nil, fmt.Errorf("%s %s profile %q: %w", label(index, item), selection.role, id, err)
|
|
}
|
|
checkedProfiles[id] = struct{}{}
|
|
}
|
|
}
|
|
specs := contracts.ResolveModuleRunSpecs(cfg)
|
|
glossaryViews := make(map[contracts.GlossarySelectionKey]int)
|
|
for i := range specs {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
spec := &specs[i]
|
|
key := contracts.GlossarySelection(spec.Settings)
|
|
if prior, ok := glossaryViews[key]; ok {
|
|
spec.GlossaryVariables = specs[prior].GlossaryVariables
|
|
spec.GlossaryView = specs[prior].GlossaryView
|
|
continue
|
|
}
|
|
if err := spec.PrepareGlossary(); err != nil {
|
|
return nil, fmt.Errorf("%s glossary preparation: %w", label(i, spec.Settings), err)
|
|
}
|
|
glossaryViews[key] = i
|
|
}
|
|
return specs, ctx.Err()
|
|
}
|