package config import ( "fmt" "strings" "time" "gitea.maximumdirect.net/eric/audita/v3/internal/core/modulecatalog" "gitea.maximumdirect.net/eric/audita/v3/internal/core/outputschema" ck "go.maximumdirect.net/configkit" ) const ( DefaultTranscriptDescriptionMaxChars = 500 DefaultValidationMaxPromptTokens = 2048 ) // Config contains application settings. Invocation paths are not settings. type Config struct { Version int `yaml:"version"` Glossaries []string `yaml:"glossaries"` ProtectedTerms []string `yaml:"protected_terms"` GlossaryCollections map[string]GlossaryCollection `yaml:"glossary_collections"` LLM LLM `yaml:"llm"` Pipeline Pipeline `yaml:"pipeline"` Output Output `yaml:"output"` Validation Validation `yaml:"validation"` Chunking Chunking `yaml:"chunking"` Normalization Normalization `yaml:"normalization"` Context Context `yaml:"context"` Reporting Reporting `yaml:"reporting"` Diagnostics Diagnostics `yaml:"diagnostics"` } type GlossaryCollection struct { Entries []GlossaryEntry `yaml:"entries"` } type GlossaryEntry struct { ID string `yaml:"id"` Name string `yaml:"name"` Category string `yaml:"category"` Description string `yaml:"description"` Aliases []string `yaml:"aliases"` Plural *string `yaml:"plural"` Extra map[string]any `yaml:",inline"` } type LLM struct { ProposalProfile *string `yaml:"proposal_profile"` ValidationProfile *string `yaml:"validation_profile"` CorrectionRepairProfile *string `yaml:"correction_repair_profile"` CorrectionRepairAttempts int `yaml:"correction_repair_attempts"` TransientRetries int `yaml:"transient_retries"` RepairAttempts int `yaml:"repair_attempts"` OperationTimeout time.Duration `yaml:"operation_timeout"` } type Pipeline struct { Stages []Stage `yaml:"stages"` } type Stage struct { Module string `yaml:"module"` ID *string `yaml:"id"` Glossaries []string `yaml:"glossaries"` ProtectedTerms []string `yaml:"protected_terms"` LLM LLM `yaml:"llm"` Validation Validation `yaml:"validation"` Context Context `yaml:"context"` } type Output struct { Schema string `yaml:"schema"` } type Validation struct { MaxPromptTokens int `yaml:"max_prompt_tokens"` } type Chunking struct { TargetSections *int `yaml:"target_sections"` MaxSectionTokens int `yaml:"max_section_tokens"` MinSectionTokens int `yaml:"min_section_tokens"` } type Normalization struct { MaxSegmentGap float64 `yaml:"max_segment_gap"` EllipsisGap float64 `yaml:"ellipsis_gap"` MaxSegmentDuration float64 `yaml:"max_segment_duration"` MaxSegmentTokens int `yaml:"max_segment_tokens"` } type Context struct { Description string `yaml:"description"` } type Diagnostics struct { WorkDir string `yaml:"work_dir"` } type Reporting struct { Enabled bool `yaml:"enabled"` StdoutDir string `yaml:"stdout_dir"` } func defaults() Config { return Config{ Version: 3, LLM: LLM{TransientRetries: 2, RepairAttempts: 2, CorrectionRepairAttempts: 1, OperationTimeout: 15 * time.Minute}, Pipeline: Pipeline{Stages: []Stage{{Module: "glossary"}, {Module: "homophones"}, {Module: "glossary"}, {Module: "spoken_word"}, {Module: "grammar"}}}, Output: Output{Schema: "bare-segments"}, Validation: Validation{MaxPromptTokens: DefaultValidationMaxPromptTokens}, Chunking: Chunking{MaxSectionTokens: 8192, MinSectionTokens: 2048}, Normalization: Normalization{MaxSegmentGap: 4, EllipsisGap: 3.5, MaxSegmentDuration: 60, MaxSegmentTokens: 2048}, Reporting: Reporting{Enabled: true, StdoutDir: "."}, Diagnostics: Diagnostics{WorkDir: "/tmp/audita"}, } } const glossaryImports = `config: imports: - path: glossary.yml required: false destination: [glossary_collections] - dir: glossary required: false recursive: true pattern: ["*.yml", "*.yaml"] destination: [glossary_collections] ` // Load resolves the selected main source and preserves native source evidence. func Load(input ck.Input, overlay string, overrides ...ck.Override) (*ck.Result[Config], error) { input.Imports = true return ck.LoadLayered(input, ck.LayeredOptions[Config]{ Overlay: overlay, Overrides: overrides, Underlays: []ck.Underlay{{Inputs: []ck.Input{{Source: ck.Bytes("Audita glossary imports", []byte(glossaryImports)), Imports: true, MainRelative: ck.MainRelative()}}}}, Definition: ck.Definition[Config]{ Defaults: defaults, SequenceIdentities: []ck.SequenceIdentity{ck.SequenceIdentityMatching(ck.SequenceSelector{ck.SequenceLiteralKey("glossary_collections"), ck.SequenceAnyMappingKey(), ck.SequenceLiteralKey("entries")}, "id")}, Version: &ck.VersionRule{Path: ck.Keys("version"), Check: func(version ck.Version, v *ck.Validation) error { if version.Present && (version.Null || version.Text != "3") { v.Error(ck.Keys("version"), "use application configuration version 3; see docs/migrations/v3.md") } return nil }}, Resolve: resolveSettings, Validate: validate, }, }) } func validate(c *Config, v *ck.Validation) { check := func(ok bool, path ck.Path, message string) { if !ok { v.Error(path, message) } } for name, collection := range c.GlossaryCollections { for i, entry := range collection.Entries { path := ck.Keys("glossary_collections", name, "entries").Index(i) check(entry.ID != "", path.Key("id"), "entry id must not be empty") check(entry.Name != "", path.Key("name"), "entry name must not be empty") check(entry.Category != "", path.Key("category"), "entry category must not be empty") for index, alias := range entry.Aliases { check(alias != "", path.Key("aliases").Index(index), "alias must not be empty") } check(entry.Plural == nil || *entry.Plural != "", path.Key("plural"), "plural must not be empty") } } validateRuntimeSettings(check, ck.Keys(), c.LLM, c.Validation, c.Context) validateSelections := func(values []string, path ck.Path) { for i, name := range values { _, exists := c.GlossaryCollections[name] check(exists, path.Index(i), "unknown glossary collection") } } validateSelections(c.Glossaries, ck.Keys("glossaries")) validateSelections(c.ProtectedTerms, ck.Keys("protected_terms")) ids := make(map[string]bool) for i, item := range c.Pipeline.Stages { path := ck.Keys("pipeline", "stages").Index(i) check(modulecatalog.IsSupported(item.Module), path.Key("module"), "unsupported module key") if item.ID != nil { check(*item.ID != "", path.Key("id"), "id must not be empty") check(!ids[*item.ID], path.Key("id"), "id must be unique") ids[*item.ID] = true } validateSelections(item.Glossaries, path.Key("glossaries")) validateSelections(item.ProtectedTerms, path.Key("protected_terms")) validateRuntimeSettings(check, path, item.LLM, item.Validation, item.Context) } check(outputschema.IsSupported(c.Output.Schema), ck.Keys("output", "schema"), "unsupported output schema") check(c.Chunking.MaxSectionTokens > 0, ck.Keys("chunking", "max_section_tokens"), "maximum section tokens must be positive") check(c.Chunking.MinSectionTokens > 0 && c.Chunking.MinSectionTokens <= c.Chunking.MaxSectionTokens, ck.Keys("chunking", "min_section_tokens"), "minimum section tokens must be positive and no greater than maximum") check(c.Chunking.TargetSections == nil || *c.Chunking.TargetSections > 0, ck.Keys("chunking", "target_sections"), "target sections must be positive when set") check(c.Normalization.MaxSegmentGap >= 0, ck.Keys("normalization", "max_segment_gap"), "maximum segment gap must be non-negative") check(c.Normalization.EllipsisGap >= 0, ck.Keys("normalization", "ellipsis_gap"), "ellipsis gap must be non-negative") check(c.Normalization.MaxSegmentDuration > 0, ck.Keys("normalization", "max_segment_duration"), "maximum segment duration must be positive") check(c.Normalization.MaxSegmentTokens > 0, ck.Keys("normalization", "max_segment_tokens"), "maximum segment tokens must be positive") } func validateRuntimeSettings(check func(bool, ck.Path, string), base ck.Path, settings LLM, validation Validation, context Context) { llm := base.Key("llm") check(settings.TransientRetries >= 0, llm.Key("transient_retries"), "retry budget must be non-negative") check(settings.RepairAttempts >= 0 && settings.RepairAttempts <= 3, llm.Key("repair_attempts"), "repair attempts must be between zero and three") check(settings.CorrectionRepairAttempts >= 0, llm.Key("correction_repair_attempts"), "correction repair attempts must be non-negative") check(settings.CorrectionRepairProfile == nil || *settings.CorrectionRepairProfile != "", llm.Key("correction_repair_profile"), "correction repair profile must not be empty; use null to clear it") check(settings.OperationTimeout > 0, llm.Key("operation_timeout"), "operation timeout must be positive") check(settings.ValidationProfile == nil || *settings.ValidationProfile != "", llm.Key("validation_profile"), "validation profile must not be empty; use null to clear it") check(validation.MaxPromptTokens > 0, base.Key("validation").Key("max_prompt_tokens"), "validation prompt budget must be positive") check(len(strings.TrimSpace(context.Description)) <= DefaultTranscriptDescriptionMaxChars, base.Key("context").Key("description"), fmt.Sprintf("description must be at most %d bytes after trimming", DefaultTranscriptDescriptionMaxChars)) }