package config import ( "bytes" "encoding/json" "fmt" "sort" "strings" ) // effectivePipelineProjection is the single safe, schema-specific view used // for inspection, comparison, and identity. Runtime metadata and unresolved // family declarations have no slot in this type. type effectivePipelineProjection struct { Workspace WorkspaceConfig `yaml:"workspace" json:"workspace"` Campaigns CampaignsConfig `yaml:"campaigns" json:"campaigns"` Storage effectiveStorageConfig `yaml:"storage" json:"storage"` Spool SpoolConfig `yaml:"spool" json:"spool"` Cache CacheConfig `yaml:"cache" json:"cache"` Publish *PublishConfig `yaml:"publish" json:"publish"` WhisperX WhisperXConfig `yaml:"whisperx" json:"whisperx"` Seriatim SeriatimConfig `yaml:"seriatim" json:"seriatim"` Audita effectiveAuditaConfig `yaml:"audita" json:"audita"` Normalize *NormalizeConfig `yaml:"normalize" json:"normalize"` Trim *TrimConfig `yaml:"trim" json:"trim"` Render *RenderConfig `yaml:"render" json:"render"` Scriptorium *effectiveScriptoriumConfig `yaml:"scriptorium" json:"scriptorium"` Notarius *effectiveNotariusConfig `yaml:"notarius" json:"notarius"` Notification NotificationConfig `yaml:"notification" json:"notification"` } type effectiveStorageConfig struct { Backend string `yaml:"backend" json:"backend"` S3 *effectiveStorageS3Config `yaml:"s3" json:"s3"` } type effectiveStorageS3Config struct { Bucket string `yaml:"bucket" json:"bucket"` RootPrefix string `yaml:"root_prefix" json:"root_prefix"` Region string `yaml:"region" json:"region"` Endpoint string `yaml:"endpoint" json:"endpoint"` ForcePathStyle bool `yaml:"force_path_style" json:"force_path_style"` AccessKeyID *string `yaml:"access_key_id,omitempty" json:"access_key_id,omitempty"` SecretAccessKey *string `yaml:"secret_access_key,omitempty" json:"secret_access_key,omitempty"` } type effectiveAuditaConfig struct { Binary string `yaml:"binary" json:"binary"` Timeout string `yaml:"timeout" json:"timeout"` Credentials []string `yaml:"credentials" json:"credentials"` Modules []string `yaml:"modules" json:"modules"` Overlay string `yaml:"overlay" json:"overlay"` ProposalProfile string `yaml:"proposal_profile" json:"proposal_profile"` ValidationProfile string `yaml:"validation_profile" json:"validation_profile"` TranscriptDescription string `yaml:"transcript_description" json:"transcript_description"` ConfigPath string `yaml:"config_path" json:"config_path"` OutputSchema string `yaml:"output_schema" json:"output_schema"` } type effectiveNotariusConfig struct { Enabled bool `yaml:"enabled" json:"enabled"` Binary string `yaml:"binary" json:"binary"` Credentials []string `yaml:"credentials" json:"credentials"` ConfigOverlay *string `yaml:"config_overlay,omitempty" json:"config_overlay,omitempty"` Resume bool `yaml:"resume,omitempty" json:"resume,omitempty"` ChunkCache string `yaml:"chunk_cache,omitempty" json:"chunk_cache,omitempty"` ConfigPath string `yaml:"config_path" json:"config_path"` PipelineID string `yaml:"pipeline_id" json:"pipeline_id"` Timeout string `yaml:"timeout" json:"timeout"` WorkingDirectory string `yaml:"working_directory" json:"working_directory"` References map[string]string `yaml:"references" json:"references"` Outputs map[string]NotariusOutputConfig `yaml:"outputs" json:"outputs"` } type effectiveScriptoriumConfig struct { Credentials []string `yaml:"credentials" json:"credentials"` Binary string `yaml:"binary" json:"binary"` ConfigPath string `yaml:"config_path" json:"config_path"` Timeout string `yaml:"timeout" json:"timeout"` RenderDebug bool `yaml:"render_debug" json:"render_debug"` Artifacts map[string]ScriptoriumArtifactConfig `yaml:"artifacts" json:"artifacts"` } type projectedPipelineValue struct { record EffectivePipelineValueRecord segments []string } func projectEffectivePipeline(cfg *PipelineConfig) effectivePipelineProjection { projection := effectivePipelineProjection{ Workspace: cfg.Workspace, Campaigns: cfg.Campaigns, Storage: effectiveStorageConfig{Backend: cfg.Storage.Backend}, Spool: cfg.Spool, Cache: cfg.Cache, Publish: cfg.Publish, WhisperX: cfg.WhisperX, Seriatim: cfg.Seriatim, Normalize: cfg.Normalize, Trim: cfg.Trim, Render: cfg.Render, Notification: cfg.Notification, } if s3 := cfg.Storage.S3; s3 != nil { projection.Storage.S3 = &effectiveStorageS3Config{Bucket: s3.Bucket, RootPrefix: s3.RootPrefix, Region: s3.Region, Endpoint: s3.Endpoint, ForcePathStyle: s3.ForcePathStyle, AccessKeyID: secretReferenceName(s3.AccessKeyID), SecretAccessKey: secretReferenceName(s3.SecretAccessKey)} } audita := cfg.Audita projection.Audita = effectiveAuditaConfig{Binary: audita.Binary, Timeout: audita.Timeout, Credentials: secretReferenceNames(audita.Credentials), Modules: audita.Modules, Overlay: audita.Overlay, ProposalProfile: audita.ProposalProfile, ValidationProfile: audita.ValidationProfile, TranscriptDescription: audita.TranscriptDescription, ConfigPath: audita.ConfigPath, OutputSchema: audita.OutputSchema} if n := cfg.Notarius; n != nil { projection.Notarius = &effectiveNotariusConfig{Enabled: n.Enabled, Binary: n.Binary, Credentials: secretReferenceNames(n.Credentials), ConfigOverlay: n.ConfigOverlay, Resume: n.Resume, ChunkCache: n.ChunkCache, ConfigPath: n.ConfigPath, PipelineID: n.PipelineID, Timeout: n.Timeout, WorkingDirectory: n.WorkingDirectory, References: n.References, Outputs: n.Outputs} } if cfg.Scriptorium != nil { projection.Scriptorium = &effectiveScriptoriumConfig{ Credentials: secretReferenceNames(cfg.Scriptorium.Credentials), Binary: cfg.Scriptorium.Binary, ConfigPath: cfg.Scriptorium.ConfigPath, Timeout: cfg.Scriptorium.Timeout, RenderDebug: cfg.Scriptorium.RenderDebug, Artifacts: cfg.Scriptorium.Artifacts, } } return projection } func projectionJSON(projection effectivePipelineProjection) ([]byte, error) { data, err := json.Marshal(projection) if err != nil { return nil, fmt.Errorf("encode effective pipeline projection: %w", err) } return data, nil } func projectionValueTree(value any) (any, error) { data, err := json.Marshal(value) if err != nil { return nil, fmt.Errorf("encode typed pipeline projection: %w", err) } decoder := json.NewDecoder(bytes.NewReader(data)) decoder.UseNumber() var root any if err := decoder.Decode(&root); err != nil { return nil, fmt.Errorf("decode effective pipeline projection: %w", err) } return root, nil } func projectionValueRecords(projection effectivePipelineProjection) ([]EffectivePipelineValueRecord, error) { detailed, err := projectionDetailedRecords(projection) if err != nil { return nil, err } records := make([]EffectivePipelineValueRecord, 0, len(detailed)) for _, value := range detailed { records = append(records, value.record) } return records, nil } func projectionDetailedRecords(value any) ([]projectedPipelineValue, error) { decoded, err := projectionValueTree(value) if err != nil { return nil, err } root, ok := decoded.(map[string]any) if !ok { return nil, fmt.Errorf("effective pipeline projection must be a mapping") } var records []projectedPipelineValue appendProjectionRecords(&records, nil, root) sort.Slice(records, func(i, j int) bool { return records[i].record.Path < records[j].record.Path }) return records, nil } func appendProjectionRecords(records *[]projectedPipelineValue, segments []string, value any) { if mapping, ok := value.(map[string]any); ok && len(mapping) > 0 { keys := make([]string, 0, len(mapping)) for key := range mapping { keys = append(keys, key) } sort.Strings(keys) for _, key := range keys { child := append(append([]string(nil), segments...), key) appendProjectionRecords(records, child, mapping[key]) } return } encoded, _ := json.Marshal(value) path := strings.Join(segments, ".") *records = append(*records, projectedPipelineValue{ record: EffectivePipelineValueRecord{Path: path, Value: string(encoded)}, segments: append([]string(nil), segments...), }) }