Prepare normalized processing plans and fixed chunks
This commit is contained in:
75
internal/framework/preflight/plan.go
Normal file
75
internal/framework/preflight/plan.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package preflight
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/chunking"
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/config"
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/normalization"
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/schema"
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/contracts"
|
||||
)
|
||||
|
||||
// Plan holds the local processing decisions for one invocation.
|
||||
type Plan struct {
|
||||
Config *config.Config
|
||||
Specs []contracts.ModuleRunSpec
|
||||
Transcript *schema.Transcript
|
||||
Summary *normalization.NormalizationSummary
|
||||
Sections []chunking.Section
|
||||
Profiles []string
|
||||
}
|
||||
|
||||
// BuildPlan resolves processing selections, normalizes the input, and fixes its chunks.
|
||||
func BuildPlan(ctx context.Context, cfg *config.Config, source *schema.SourceTranscript, inspect func(context.Context, string) error) (*Plan, error) {
|
||||
if cfg == nil {
|
||||
return nil, fmt.Errorf("application configuration is required")
|
||||
}
|
||||
if source == nil {
|
||||
return nil, fmt.Errorf("transcript is required")
|
||||
}
|
||||
var profiles []string
|
||||
var inspectAndCollect func(context.Context, string) error
|
||||
if inspect != nil {
|
||||
inspectAndCollect = func(ctx context.Context, id string) error {
|
||||
if err := inspect(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
profiles = append(profiles, id)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
specs, err := ProcessingPlan(ctx, cfg, inspectAndCollect)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
normalizer := normalization.NewNormalizer(normalization.NormalizationConfig{
|
||||
MaxSegmentGap: cfg.Normalization.MaxSegmentGap, EllipsisGap: cfg.Normalization.EllipsisGap,
|
||||
MaxSegmentDuration: cfg.Normalization.MaxSegmentDuration, MaxSegmentTokens: cfg.Normalization.MaxSegmentTokens,
|
||||
})
|
||||
transcript, summary := normalizer.Normalize(source.Canonical())
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, fmt.Errorf("normalize transcript: %w", err)
|
||||
}
|
||||
chunker := chunking.NewChunker(chunking.ChunkingConfig{
|
||||
MaxSectionTokens: cfg.Chunking.MaxSectionTokens, MinSectionTokens: cfg.Chunking.MinSectionTokens,
|
||||
TargetSections: cfg.Chunking.TargetSections,
|
||||
})
|
||||
sections, err := chunker.ChunkTranscript(transcript)
|
||||
if err != nil {
|
||||
var infeasible *chunking.InfeasibleTargetError
|
||||
if errors.As(err, &infeasible) {
|
||||
return nil, &SelectionError{Cause: fmt.Errorf("chunk normalized transcript: %w", err)}
|
||||
}
|
||||
return nil, fmt.Errorf("chunk normalized transcript: %w", err)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, fmt.Errorf("chunk normalized transcript: %w", err)
|
||||
}
|
||||
return &Plan{Config: cfg, Specs: specs, Transcript: transcript, Summary: summary, Sections: sections, Profiles: profiles}, nil
|
||||
}
|
||||
73
internal/framework/preflight/plan_test.go
Normal file
73
internal/framework/preflight/plan_test.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package preflight
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/chunking"
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/config"
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/schema"
|
||||
)
|
||||
|
||||
func TestBuildPlanResolvesProfilesAndChunks(t *testing.T) {
|
||||
proposal, validation, repair, unused := "proposal", "validation", "repair", "unused"
|
||||
cfg := &config.Config{
|
||||
Chunking: config.Chunking{MaxSectionTokens: 100},
|
||||
Pipeline: config.Pipeline{Stages: []config.Stage{
|
||||
{Module: "grammar", LLM: config.LLM{ProposalProfile: &proposal, ValidationProfile: &validation, CorrectionRepairProfile: &unused}},
|
||||
{Module: "grammar", LLM: config.LLM{ProposalProfile: &validation, ValidationProfile: &proposal, CorrectionRepairProfile: &repair, CorrectionRepairAttempts: 1}},
|
||||
}},
|
||||
}
|
||||
source := &schema.SourceTranscript{Segments: []schema.SourceSegment{{Speaker: "A", Start: 0, End: 1, Text: "hello"}, {Speaker: "B", Start: 2, End: 3, Text: "world"}}}
|
||||
var inspected []string
|
||||
plan, err := BuildPlan(context.Background(), cfg, source, func(_ context.Context, id string) error { inspected = append(inspected, id); return nil })
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !slices.Equal(plan.Profiles, []string{proposal, validation, repair}) || !slices.Equal(plan.Profiles, inspected) {
|
||||
t.Fatalf("profiles %v, inspected %v", plan.Profiles, inspected)
|
||||
}
|
||||
if len(plan.Specs) != 2 || len(plan.Transcript.Segments) != 2 || plan.Summary.InputSegmentCount != 2 || len(plan.Sections) != 1 || plan.Sections[0].StartSegmentID != 1 || plan.Sections[0].EndSegmentID != 2 {
|
||||
t.Fatalf("unexpected plan: %+v", plan)
|
||||
}
|
||||
count := 3
|
||||
cfg.Chunking.TargetSections = &count
|
||||
_, err = BuildPlan(context.Background(), cfg, source, func(context.Context, string) error { return nil })
|
||||
var selection *SelectionError
|
||||
var infeasible *chunking.InfeasibleTargetError
|
||||
if !errors.As(err, &selection) || !errors.As(err, &infeasible) {
|
||||
t.Fatalf("expected input selection failure, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildPlanEmptyPipeline(t *testing.T) {
|
||||
cfg := &config.Config{Chunking: config.Chunking{MaxSectionTokens: 100}}
|
||||
plan, err := BuildPlan(context.Background(), cfg, &schema.SourceTranscript{Segments: []schema.SourceSegment{{Speaker: "A", Text: "hello"}}}, nil)
|
||||
if err != nil || len(plan.Profiles) != 0 || len(plan.Specs) != 0 || len(plan.Sections) != 1 {
|
||||
t.Fatalf("plan %+v, error %v", plan, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildPlanRetainsNormalizedContentAndGuidance(t *testing.T) {
|
||||
profile := "selected"
|
||||
cfg := &config.Config{
|
||||
Chunking: config.Chunking{MaxSectionTokens: 100},
|
||||
Normalization: config.Normalization{MaxSegmentGap: 2, EllipsisGap: 2, MaxSegmentDuration: 10, MaxSegmentTokens: 100},
|
||||
GlossaryCollections: map[string]config.GlossaryCollection{"terms": {Instruction: "Use the shared vocabulary", Entries: []config.GlossaryEntry{{ID: "one", Name: "Example"}}}},
|
||||
Pipeline: config.Pipeline{Stages: []config.Stage{{Module: "glossary", Glossaries: []string{"terms"}, LLM: config.LLM{ProposalProfile: &profile, ValidationProfile: &profile}}}},
|
||||
}
|
||||
source := &schema.SourceTranscript{Segments: []schema.SourceSegment{{Speaker: "A", Start: 0, End: 1, Text: "hello"}, {Speaker: "A", Start: 1.1, End: 2, Text: "world"}}}
|
||||
plan, err := BuildPlan(context.Background(), cfg, source, func(context.Context, string) error { return nil })
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(plan.Transcript.Segments) != 1 || plan.Transcript.Segments[0].Text != "hello world" || plan.Summary.MergesPerformed != 1 || len(plan.Sections) != 1 || plan.Sections[0].Segments[0].Text != "hello world" {
|
||||
t.Fatalf("normalization or chunks: %+v", plan)
|
||||
}
|
||||
if !strings.Contains(plan.Specs[0].GlossaryVariables.InstructionsBlock, "Use the shared vocabulary") {
|
||||
t.Fatalf("guidance missing: %+v", plan.Specs[0])
|
||||
}
|
||||
}
|
||||
@@ -1,92 +1,16 @@
|
||||
// Package preflight checks resolved processing inputs and prepares output resources.
|
||||
// Package preflight prepares provider-independent processing inputs.
|
||||
package preflight
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/config"
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/diagnostics"
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/modulecatalog"
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/schema"
|
||||
"gitea.maximumdirect.net/eric/audita/v4/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
|
||||
}
|
||||
checked, err := diagnostics.CheckOutput(input.Output)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
session := diagnostics.NewOutputSession(checked)
|
||||
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 }
|
||||
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
package preflight
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/config"
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/diagnostics"
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/schema"
|
||||
)
|
||||
|
||||
func TestReadinessAndCollisionPrecedeOutputPreparation(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
output := filepath.Join(dir, "output.json")
|
||||
if _, err := Prepare(context.Background(), Input{Config: &config.Config{}, Output: diagnostics.OutputSelection{TranscriptPath: output}}); err == nil {
|
||||
t.Fatal("missing transcript accepted")
|
||||
}
|
||||
if _, err := os.Stat(output); !os.IsNotExist(err) {
|
||||
t.Fatalf("output created: %v", err)
|
||||
}
|
||||
if _, err := Prepare(context.Background(), Input{Config: &config.Config{}, Transcript: &schema.SourceTranscript{}, Output: diagnostics.OutputSelection{TranscriptPath: output, ProtectedPaths: []string{output}}}); err == nil {
|
||||
t.Fatal("collision accepted")
|
||||
}
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil || len(entries) != 0 {
|
||||
t.Fatalf("prepared before check: %v %v", entries, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOptionalDebugFailureDoesNotPreventPreparation(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
blocked := filepath.Join(dir, "file")
|
||||
if err := os.WriteFile(blocked, []byte("x"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := Prepare(context.Background(), Input{Config: &config.Config{}, Transcript: &schema.SourceTranscript{}, Output: diagnostics.OutputSelection{TranscriptPath: filepath.Join(dir, "output")}, Debug: &DebugInput{WorkDir: blocked, RunID: "run"}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(result.Warnings) == 0 || result.Debug != nil {
|
||||
t.Fatalf("debug=%v warnings=%v", result.Debug, result.Warnings)
|
||||
}
|
||||
if err := result.Output.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -4,35 +4,28 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/config"
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/diagnostics"
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/schema"
|
||||
)
|
||||
|
||||
func TestLaterEmptyGlossaryFailsBeforeOutputPreparation(t *testing.T) {
|
||||
func TestLaterEmptyGlossaryFailsBeforeProfileInspection(t *testing.T) {
|
||||
profile, id := "selected", "spells"
|
||||
cfg := &config.Config{Pipeline: config.Pipeline{Stages: []config.Stage{
|
||||
{Module: "grammar", LLM: config.LLM{ProposalProfile: &profile, ValidationProfile: &profile}},
|
||||
{Module: "glossary", ID: &id, Glossaries: []string{"empty"}, LLM: config.LLM{ProposalProfile: &profile, ValidationProfile: &profile}},
|
||||
}}}
|
||||
output := filepath.Join(t.TempDir(), "transcript.json")
|
||||
calls := 0
|
||||
_, err := Prepare(context.Background(), Input{Config: cfg, Transcript: &schema.SourceTranscript{}, Output: diagnostics.OutputSelection{TranscriptPath: output}, InspectProfile: func(context.Context, string) error { calls++; return nil }})
|
||||
_, err := BuildPlan(context.Background(), cfg, &schema.SourceTranscript{}, func(context.Context, string) error { calls++; return nil })
|
||||
if err == nil || !strings.Contains(err.Error(), "stage 2") || !strings.Contains(err.Error(), "spells") || !strings.Contains(err.Error(), "empty") {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if calls != 0 {
|
||||
t.Fatal("inspected profiles before checking all glossary selections")
|
||||
}
|
||||
if _, err := os.Stat(output); !os.IsNotExist(err) {
|
||||
t.Fatalf("output exists: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyPipelineNeedsNoProfile(t *testing.T) {
|
||||
@@ -54,14 +47,10 @@ func TestCorrectionRepairReadinessDependsOnBudget(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg.Pipeline.Stages[0].LLM.CorrectionRepairAttempts = 1
|
||||
output := filepath.Join(t.TempDir(), "transcript.json")
|
||||
_, err := Prepare(context.Background(), Input{Config: cfg, Transcript: &schema.SourceTranscript{}, Output: diagnostics.OutputSelection{TranscriptPath: output}, InspectProfile: inspect})
|
||||
_, err := BuildPlan(context.Background(), cfg, &schema.SourceTranscript{}, inspect)
|
||||
if err == nil || !strings.Contains(err.Error(), "correction repair") {
|
||||
t.Fatalf("readiness error: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(output); !os.IsNotExist(err) {
|
||||
t.Fatalf("output exists: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessingPlanReusesOnlyMatchingGlossaryViews(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user