172 lines
5.5 KiB
Go
172 lines
5.5 KiB
Go
// Package preflight checks resolved processing inputs and prepares output resources.
|
|
package preflight
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/core/config"
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/core/diagnostics"
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/core/modulecatalog"
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/core/schema"
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/contracts"
|
|
)
|
|
|
|
// Input contains already acquired application and transcript inputs.
|
|
type Input struct {
|
|
InspectProfile func(context.Context, string) error
|
|
Config *config.Config
|
|
Transcript *schema.SourceTranscript
|
|
Output diagnostics.OutputSelection
|
|
InitialReports diagnostics.ReportBundle
|
|
FailedReports diagnostics.ReportRenderer
|
|
Debug *DebugInput
|
|
}
|
|
|
|
type DebugInput struct {
|
|
WorkDir string
|
|
RunID string
|
|
Metadata diagnostics.InvocationMetadata
|
|
EffectiveConfig []byte
|
|
}
|
|
|
|
type Prepared struct {
|
|
Specs []contracts.ModuleRunSpec
|
|
Output *diagnostics.OutputSession
|
|
Debug *diagnostics.RunDirectory
|
|
Warnings []error
|
|
}
|
|
|
|
// Prepare validates invocation inputs and collisions before acquiring outputs.
|
|
func Prepare(ctx context.Context, input Input) (*Prepared, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
if input.Config == nil {
|
|
return nil, fmt.Errorf("application configuration is required")
|
|
}
|
|
if input.Transcript == nil {
|
|
return nil, fmt.Errorf("transcript is required")
|
|
}
|
|
specs, err := ProcessingPlan(ctx, input.Config, input.InspectProfile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
session, err := diagnostics.SelectOutput(input.Output)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := session.Prepare(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, errors.Join(err, session.Close())
|
|
}
|
|
if err := session.Initialize(ctx, input.InitialReports, input.FailedReports); err != nil {
|
|
return nil, errors.Join(err, session.Close())
|
|
}
|
|
cancelOutput := func(err error) error {
|
|
return session.Finish(ctx, err, nil, input.FailedReports)
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, cancelOutput(err)
|
|
}
|
|
result := &Prepared{Specs: specs, Output: session}
|
|
if input.Debug != nil {
|
|
result.Debug, result.Warnings = diagnostics.PrepareDebug(input.Debug.WorkDir, input.Debug.RunID, input.Debug.Metadata, input.Debug.EffectiveConfig)
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, cancelOutput(err)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// 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()
|
|
}
|