2026-09-05 00:24:58 +00:00
package config
import (
"fmt"
"sort"
"strings"
"gitea.maximumdirect.net/eric/narratio/internal/artifactmodel"
"gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy"
"gitea.maximumdirect.net/eric/narratio/internal/notariusref"
)
func validateNotarius ( cfg * NotariusConfig , scriptorium * ScriptoriumConfig ) error {
if cfg == nil || ! cfg . Enabled {
return nil
}
2026-09-05 00:26:54 +00:00
2026-09-07 03:32:50 +00:00
if strings . TrimSpace ( cfg . Binary ) == "" {
2026-09-05 00:24:58 +00:00
return fmt . Errorf ( "pipeline.notarius.binary is required when pipeline.notarius.enabled is true" )
}
if strings . TrimSpace ( cfg . ConfigPath ) == "" {
return fmt . Errorf ( "pipeline.notarius.config_path is required when pipeline.notarius.enabled is true" )
}
2026-09-07 03:32:50 +00:00
if strings . TrimSpace ( cfg . PipelineID ) == "" {
2026-09-05 00:24:58 +00:00
return fmt . Errorf ( "pipeline.notarius.pipeline_id is required when pipeline.notarius.enabled is true" )
}
2026-09-07 03:32:50 +00:00
if strings . TrimSpace ( cfg . Timeout ) == "" {
2026-09-05 00:26:54 +00:00
return fmt . Errorf ( "pipeline.notarius.timeout must be a valid duration" )
2026-09-05 00:24:58 +00:00
}
2026-09-07 03:32:50 +00:00
if err := validateDuration ( "pipeline.notarius.timeout" , cfg . Timeout ) ; err != nil {
2026-09-05 00:26:54 +00:00
return err
2026-09-05 00:24:58 +00:00
}
if strings . TrimSpace ( cfg . WorkingDirectory ) == "" {
return fmt . Errorf ( "pipeline.notarius.working_directory is required when pipeline.notarius.enabled is true" )
}
2026-09-05 00:26:54 +00:00
if len ( cfg . Outputs ) == 0 {
return fmt . Errorf ( "pipeline.notarius.outputs must contain at least one output when pipeline.notarius.enabled is true" )
}
2026-09-05 00:24:58 +00:00
if len ( cfg . References ) > MaxNotariusReferenceBindings {
return fmt . Errorf ( "pipeline.notarius.references must contain at most %d bindings" , MaxNotariusReferenceBindings )
}
2026-09-07 03:32:50 +00:00
if err := validateNotariusCanonicalScalars ( cfg ) ; err != nil {
2026-09-05 00:26:54 +00:00
return err
}
2026-09-16 17:42:16 +00:00
if ! validNotariusChunkCacheMode ( cfg . ChunkCache ) {
return fmt . Errorf ( "pipeline.notarius.chunk_cache must be empty, auto, bypass, or refresh" )
}
2026-09-07 03:32:50 +00:00
if err := validateNotariusReferences ( cfg . References ) ; err != nil {
2026-09-05 00:26:54 +00:00
return err
}
2026-09-07 03:32:50 +00:00
return validateNotariusOutputs ( cfg . Outputs , scriptorium )
}
2026-09-05 00:26:54 +00:00
2026-09-07 03:32:50 +00:00
func validateNotariusCanonicalScalars ( cfg * NotariusConfig ) error {
fields := [ ] struct {
name string
got string
want string
} {
{ name : "binary" , got : cfg . Binary , want : strings . TrimSpace ( cfg . Binary ) } ,
2026-09-16 17:42:16 +00:00
{ name : "chunk_cache" , got : cfg . ChunkCache , want : strings . TrimSpace ( cfg . ChunkCache ) } ,
2026-09-07 03:32:50 +00:00
{ name : "config_path" , got : cfg . ConfigPath , want : cleanNotariusPath ( cfg . ConfigPath ) } ,
{ name : "pipeline_id" , got : cfg . PipelineID , want : strings . TrimSpace ( cfg . PipelineID ) } ,
{ name : "timeout" , got : cfg . Timeout , want : strings . TrimSpace ( cfg . Timeout ) } ,
{ name : "working_directory" , got : cfg . WorkingDirectory , want : cleanNotariusPath ( cfg . WorkingDirectory ) } ,
}
for _ , field := range fields {
if field . got != field . want {
return fmt . Errorf ( "pipeline.notarius.%s must be normalized" , field . name )
}
}
2026-09-05 00:26:54 +00:00
return nil
}
2026-09-16 17:42:16 +00:00
func validNotariusChunkCacheMode ( mode string ) bool {
switch mode {
case "" , "auto" , "bypass" , "refresh" :
return true
default :
return false
}
}
2026-09-05 00:26:54 +00:00
func normalizeNotariusReferences ( references map [ string ] string ) ( map [ string ] string , error ) {
selectors := make ( [ ] string , 0 , len ( references ) )
for selector := range references {
selectors = append ( selectors , selector )
2026-09-05 00:24:58 +00:00
}
2026-09-05 00:26:54 +00:00
sort . Strings ( selectors )
var normalized map [ string ] string
if references != nil {
normalized = make ( map [ string ] string , len ( references ) )
2026-09-05 00:24:58 +00:00
}
2026-09-05 00:26:54 +00:00
owners := make ( map [ string ] string , len ( references ) )
for _ , rawSelector := range selectors {
2026-09-05 00:24:58 +00:00
selector , err := notariusref . NormalizeSelector ( rawSelector )
if err != nil {
2026-09-05 00:26:54 +00:00
return nil , fmt . Errorf ( "pipeline.notarius.references selector %q is invalid: %w" , rawSelector , err )
2026-09-05 00:24:58 +00:00
}
2026-09-05 00:26:54 +00:00
if previous , ok := owners [ selector ] ; ok {
return nil , fmt . Errorf ( "pipeline.notarius.references selectors %q and %q normalize to %q" , previous , rawSelector , selector )
2026-09-05 00:24:58 +00:00
}
2026-09-05 00:26:54 +00:00
owners [ selector ] = rawSelector
2026-09-05 00:24:58 +00:00
2026-09-05 00:26:54 +00:00
source := strings . TrimSpace ( references [ rawSelector ] )
normalized [ selector ] = source
2026-09-05 00:24:58 +00:00
}
2026-09-05 00:26:54 +00:00
return normalized , nil
}
2026-09-05 00:24:58 +00:00
2026-09-07 03:32:50 +00:00
func normalizeNotariusOutputs ( outputs map [ string ] NotariusOutputConfig ) ( map [ string ] NotariusOutputConfig , error ) {
2026-09-05 00:26:54 +00:00
keys := make ( [ ] string , 0 , len ( outputs ) )
for key := range outputs {
keys = append ( keys , key )
2026-09-05 00:24:58 +00:00
}
2026-09-05 00:26:54 +00:00
sort . Strings ( keys )
2026-09-05 00:24:58 +00:00
2026-09-05 00:26:54 +00:00
normalized := make ( map [ string ] NotariusOutputConfig , len ( outputs ) )
sourceOwners := make ( map [ string ] string , len ( outputs ) )
for _ , rawKey := range keys {
output := outputs [ rawKey ]
2026-09-05 00:24:58 +00:00
key := strings . TrimSpace ( rawKey )
sourceID := artifactpolicy . ExtractionSourceID ( key )
if previousKey , ok := sourceOwners [ sourceID ] ; ok {
2026-09-05 00:26:54 +00:00
return nil , fmt . Errorf ( "pipeline.notarius.outputs keys %q and %q normalize to duplicate source %q" , previousKey , rawKey , sourceID )
2026-09-05 00:24:58 +00:00
}
sourceOwners [ sourceID ] = rawKey
output . LaneID = strings . TrimSpace ( output . LaneID )
output . MediaType = strings . TrimSpace ( output . MediaType )
output . SchemaID = strings . TrimSpace ( output . SchemaID )
output . SchemaVersion = strings . TrimSpace ( output . SchemaVersion )
output . ModuleKey = strings . TrimSpace ( output . ModuleKey )
2026-09-07 03:32:50 +00:00
normalized [ key ] = output
}
return normalized , nil
}
func validateNotariusReferences ( references map [ string ] string ) error {
selectors := make ( [ ] string , 0 , len ( references ) )
for selector := range references {
selectors = append ( selectors , selector )
}
sort . Strings ( selectors )
for _ , selector := range selectors {
canonical , err := notariusref . NormalizeSelector ( selector )
if err != nil {
return fmt . Errorf ( "pipeline.notarius.references selector %q is invalid: %w" , selector , err )
}
if canonical != selector {
return fmt . Errorf ( "pipeline.notarius.references selector %q must be normalized" , selector )
}
source := references [ selector ]
if strings . TrimSpace ( source ) == "" {
return fmt . Errorf ( "pipeline.notarius.references.%s source is required" , selector )
}
if source != strings . TrimSpace ( source ) {
return fmt . Errorf ( "pipeline.notarius.references.%s source must be normalized" , selector )
}
2026-11-27 16:24:08 +00:00
if source == artifactpolicy . SourceInputGlossary {
return fmt . Errorf ( "pipeline.notarius.references.%s: replace %s with an explicit narratio.input.glossary.<party|campaign|setting|spells> source" , selector , source )
}
2026-09-07 03:32:50 +00:00
if _ , ok := artifactpolicy . DescribePreparedInputSource ( source ) ; ! ok {
return fmt . Errorf ( "pipeline.notarius.references.%s source %q is not a supported prepared input source" , selector , source )
}
}
return nil
}
func validateNotariusOutputs ( outputs map [ string ] NotariusOutputConfig , scriptorium * ScriptoriumConfig ) error {
reservedSources := notariusReservedSources ( scriptorium )
keys := make ( [ ] string , 0 , len ( outputs ) )
for key := range outputs {
keys = append ( keys , key )
}
sort . Strings ( keys )
laneOwners := make ( map [ string ] string , len ( outputs ) )
for _ , key := range keys {
output := outputs [ key ]
if strings . TrimSpace ( key ) != key || ! artifactpolicy . IsConfiguredKey ( key ) {
return fmt . Errorf ( "pipeline.notarius.outputs keys must match ^[a-z][a-z0-9_]*$" )
}
sourceID := artifactpolicy . ExtractionSourceID ( key )
if owner , ok := reservedSources [ sourceID ] ; ok {
return fmt . Errorf ( "pipeline.notarius.outputs.%s source %q collides with %s" , key , sourceID , owner )
}
2026-09-05 00:24:58 +00:00
prefix := "pipeline.notarius.outputs." + key
2026-09-07 03:32:50 +00:00
fields := [ ] struct { name , value string } {
{ name : "lane_id" , value : output . LaneID } ,
{ name : "media_type" , value : output . MediaType } ,
{ name : "schema_id" , value : output . SchemaID } ,
{ name : "schema_version" , value : output . SchemaVersion } ,
{ name : "module_key" , value : output . ModuleKey } ,
}
for _ , field := range fields {
if field . value != strings . TrimSpace ( field . value ) {
return fmt . Errorf ( "%s.%s must be normalized" , prefix , field . name )
}
}
2026-09-05 00:24:58 +00:00
if output . LaneID == "" {
2026-09-07 03:32:50 +00:00
return fmt . Errorf ( "%s.lane_id is required" , prefix )
2026-09-05 00:24:58 +00:00
}
if previousKey , ok := laneOwners [ output . LaneID ] ; ok {
2026-09-07 03:32:50 +00:00
return fmt . Errorf ( "%s.lane_id %q duplicates pipeline.notarius.outputs.%s.lane_id" , prefix , output . LaneID , previousKey )
2026-09-05 00:24:58 +00:00
}
laneOwners [ output . LaneID ] = key
if output . MediaType == "" {
2026-09-07 03:32:50 +00:00
return fmt . Errorf ( "%s.media_type is required" , prefix )
2026-09-05 00:24:58 +00:00
}
if output . SchemaID == "" {
2026-09-07 03:32:50 +00:00
return fmt . Errorf ( "%s.schema_id is required" , prefix )
2026-09-05 00:24:58 +00:00
}
if output . SchemaVersion == "" {
2026-09-07 03:32:50 +00:00
return fmt . Errorf ( "%s.schema_version is required" , prefix )
2026-09-05 00:24:58 +00:00
}
2026-09-05 00:26:54 +00:00
}
2026-09-07 03:32:50 +00:00
return nil
2026-09-05 00:26:54 +00:00
}
func notariusReservedSources ( scriptorium * ScriptoriumConfig ) map [ string ] string {
reserved := make ( map [ string ] string )
for _ , spec := range artifactmodel . RuntimeTranscriptArtifacts ( ) {
reserved [ spec . SourceID ] = "built-in source"
}
reserved [ artifactpolicy . SourceBoundsSession ] = "built-in source"
if scriptorium != nil {
for key := range scriptorium . Artifacts {
normalizedKey := strings . TrimSpace ( key )
reserved [ artifactpolicy . ConfiguredSourceID ( normalizedKey ) ] = "configured Scriptorium source"
reserved [ artifactpolicy . PreviousSessionSourceID ( normalizedKey ) ] = "previous-session source"
}
}
return reserved
2026-09-05 00:24:58 +00:00
}
func notariusOutputKeySet ( notarius * NotariusConfig ) map [ string ] struct { } {
out := map [ string ] struct { } { }
if notarius == nil {
return out
}
for key := range notarius . Outputs {
trimmed := strings . TrimSpace ( key )
if artifactpolicy . IsConfiguredKey ( trimmed ) {
out [ trimmed ] = struct { } { }
}
}
return out
}