Hand checked processing selections to execution

This commit is contained in:
2026-11-16 14:18:38 +00:00
parent b1d1b8302c
commit e98e525b5c
19 changed files with 268 additions and 102 deletions

View File

@@ -11,6 +11,7 @@ import (
"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.
@@ -32,6 +33,7 @@ type DebugInput struct {
}
type Prepared struct {
Specs []contracts.ModuleRunSpec
Output *diagnostics.OutputSession
Debug *diagnostics.RunDirectory
Warnings []error
@@ -48,7 +50,8 @@ func Prepare(ctx context.Context, input Input) (*Prepared, error) {
if input.Transcript == nil {
return nil, fmt.Errorf("transcript is required")
}
if err := CheckReadiness(ctx, input.Config, input.InspectProfile); err != nil {
specs, err := ProcessingPlan(ctx, input.Config, input.InspectProfile)
if err != nil {
return nil, err
}
session, err := diagnostics.SelectOutput(input.Output)
@@ -73,7 +76,7 @@ func Prepare(ctx context.Context, input Input) (*Prepared, error) {
if err := ctx.Err(); err != nil {
return nil, cancelOutput(err)
}
result := &Prepared{Output: session}
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)
}
@@ -83,10 +86,19 @@ func Prepare(ctx context.Context, input Input) (*Prepared, error) {
return result, nil
}
// CheckReadiness checks all resolved processing selections before output preparation.
func CheckReadiness(ctx context.Context, cfg *config.Config, inspectProfile func(context.Context, string) error) error {
// 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 fmt.Errorf("application configuration is required")
return nil, fmt.Errorf("application configuration is required")
}
label := func(index int, item config.Stage) string {
if item.ID != nil {
@@ -102,9 +114,10 @@ func CheckReadiness(ctx context.Context, cfg *config.Config, inspectProfile func
return len(cfg.GlossaryCollections[name].Entries) > 0
})
if !hasEntries {
return fmt.Errorf("%s has no glossary entries in selected collections %q", label(index, item), item.Glossaries)
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
@@ -118,15 +131,49 @@ func CheckReadiness(ctx context.Context, cfg *config.Config, inspectProfile func
}
for _, selection := range selections {
if selection.profile == nil || *selection.profile == "" {
return fmt.Errorf("%s requires a %s profile", label(index, item), selection.role)
return nil, &SelectionError{Cause: fmt.Errorf("%s requires a %s profile", label(index, item), selection.role)}
}
if inspectProfile == nil {
return fmt.Errorf("profile inspection is required")
return nil, fmt.Errorf("profile inspection is required")
}
if err := inspectProfile(ctx, *selection.profile); err != nil {
return fmt.Errorf("%s %s profile %q: %w", label(index, item), selection.role, *selection.profile, err)
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{}{}
}
}
return ctx.Err()
specs := contracts.ResolveModuleRunSpecs(cfg)
type glossarySelection struct {
context, protected string
exempt bool
}
glossaryViews := make(map[glossarySelection]int)
for i := range specs {
if err := ctx.Err(); err != nil {
return nil, err
}
spec := &specs[i]
key := glossarySelection{
context: fmt.Sprintf("%q", spec.Settings.Glossaries),
protected: fmt.Sprintf("%q", spec.Settings.ProtectedTerms),
exempt: spec.ModuleKey == modulecatalog.KeyGlossary,
}
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()
}

View File

@@ -2,9 +2,11 @@ package preflight
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"slices"
"strings"
"testing"
@@ -34,7 +36,7 @@ func TestLaterEmptyGlossaryFailsBeforeOutputPreparation(t *testing.T) {
}
func TestEmptyPipelineNeedsNoProfile(t *testing.T) {
if err := CheckReadiness(context.Background(), &config.Config{}, nil); err != nil {
if _, err := ProcessingPlan(context.Background(), &config.Config{}, nil); err != nil {
t.Fatal(err)
}
}
@@ -48,7 +50,7 @@ func TestCorrectionRepairReadinessDependsOnBudget(t *testing.T) {
}
return nil
}
if err := CheckReadiness(context.Background(), cfg, inspect); err != nil {
if _, err := ProcessingPlan(context.Background(), cfg, inspect); err != nil {
t.Fatal(err)
}
cfg.Pipeline.Stages[0].LLM.CorrectionRepairAttempts = 1
@@ -61,3 +63,55 @@ func TestCorrectionRepairReadinessDependsOnBudget(t *testing.T) {
t.Fatalf("output exists: %v", err)
}
}
func TestProcessingPlanReusesOnlyMatchingGlossaryViews(t *testing.T) {
profile := "selected"
item := config.Stage{Module: "grammar", Glossaries: []string{"a", "b"}, ProtectedTerms: []string{"a"},
LLM: config.LLM{ProposalProfile: &profile, ValidationProfile: &profile}}
reversed := item
reversed.Glossaries = []string{"b", "a"}
exempt := item
exempt.Module = "glossary"
cfg := &config.Config{
GlossaryCollections: map[string]config.GlossaryCollection{
"a": {Entries: []config.GlossaryEntry{{ID: "a", Name: "A"}}},
"b": {Entries: []config.GlossaryEntry{{ID: "b", Name: "B"}}},
},
Pipeline: config.Pipeline{Stages: []config.Stage{item, item, reversed, exempt}},
}
var inspected []string
plan, err := ProcessingPlan(context.Background(), cfg, func(_ context.Context, id string) error {
inspected = append(inspected, id)
return nil
})
if err != nil || len(plan) != 4 || !slices.Equal(inspected, []string{profile}) {
t.Fatalf("plan=%+v inspections=%v error=%v", plan, inspected, err)
}
if plan[0].GlossaryView != plan[1].GlossaryView || plan[0].GlossaryVariables != plan[1].GlossaryVariables ||
plan[0].GlossaryView == plan[2].GlossaryView || plan[0].GlossaryView == plan[3].GlossaryView ||
plan[2].Glossary[0].ID != "b" || plan[3].GlossaryVariables.ProtectionJSON != "[]" {
t.Fatalf("incorrect glossary reuse or exemptions: %+v", plan)
}
}
func TestProcessingPlanPreservesFirstInspectionFailureAndCause(t *testing.T) {
proposal, validation := "proposal", "bad"
cfg := &config.Config{Pipeline: config.Pipeline{Stages: []config.Stage{
{Module: "grammar", LLM: config.LLM{ProposalProfile: &proposal, ValidationProfile: &validation}},
{Module: "grammar"},
}}}
runtimeErr := errors.New("inspection failed")
var checked []string
_, err := ProcessingPlan(context.Background(), cfg, func(_ context.Context, id string) error {
checked = append(checked, id)
if id == validation {
return runtimeErr
}
return nil
})
var selection *SelectionError
if !errors.Is(err, runtimeErr) || errors.As(err, &selection) || !strings.Contains(err.Error(), "stage 1 (grammar) validation") ||
!slices.Equal(checked, []string{proposal, validation}) {
t.Fatalf("inspected %v, error %v", checked, err)
}
}