2026-08-05 19:00:55 +00:00
// Package invariants validates normalized D&D NPC occurrence artifacts.
2026-07-23 13:35:15 +00:00
package invariants
import (
"context"
"fmt"
"sort"
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
2026-08-06 13:40:42 +00:00
occurrencemodel "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcoccurrences"
2026-07-23 13:35:15 +00:00
npcregistry "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcs/registry"
2026-07-24 14:20:10 +00:00
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared"
2026-07-23 13:35:15 +00:00
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics"
2026-08-05 19:00:55 +00:00
occurrenceshape "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/npcoccurrences/shape"
2026-07-23 13:35:15 +00:00
)
const (
2026-08-05 19:00:55 +00:00
Key = "normalize/dnd/npc-occurrences/invariants"
2026-08-05 20:17:20 +00:00
ReasonCode = "invalid_npc_occurrence_normalization"
2026-08-05 19:00:55 +00:00
policy = "dnd.npc_occurrences.validator.normalized.v1"
2026-07-23 13:35:15 +00:00
)
type Options struct { }
type Validator struct {
npcResolver * npcregistry . Resolver
}
2026-08-05 18:55:58 +00:00
var _ contracts . TypedValidator [ dnd . NPCOccurrenceList ] = ( * Validator ) ( nil )
2026-07-23 13:35:15 +00:00
var _ contracts . ManifestMetadataProvider = ( * Validator ) ( nil )
var _ pipeline . CheckpointFingerprintProvider = ( * Validator ) ( nil )
func New ( _ Options , references ... contracts . ReferenceSet ) ( * Validator , error ) {
if len ( references ) > 1 {
2026-08-05 19:00:55 +00:00
return nil , fmt . Errorf ( "NPC occurrence invariants validator accepts at most one reference set" )
2026-07-23 13:35:15 +00:00
}
var referenceSet contracts . ReferenceSet
if len ( references ) == 1 {
referenceSet = references [ 0 ]
}
resolver , err := npcregistry . NewResolver ( referenceSet )
if err != nil {
return nil , fmt . Errorf ( "prepare NPC registry: %w" , err )
}
return & Validator { npcResolver : resolver } , nil
}
func ( v * Validator ) Name ( ) string { return Key }
func ( v * Validator ) ExecutionClass ( ) contracts . ExecutionClass {
return contracts . ExecutionClassDeterministic
}
func ( v * Validator ) ManifestMetadata ( ) map [ string ] any {
if v == nil || v . npcResolver == nil {
return nil
}
metadata := map [ string ] any { "policy" : policy }
seeded := v . npcResolver . Seeded ( )
if seeded . Bound ( ) {
metadata [ "npc_registry_digest" ] = seeded . Digest ( )
metadata [ "npc_count" ] = seeded . Count ( )
}
return metadata
}
func ( v * Validator ) CheckpointFingerprints ( ) [ ] pipeline . CheckpointFingerprint {
if v == nil || v . npcResolver == nil {
return nil
}
return [ ] pipeline . CheckpointFingerprint {
{ Name : "policy" , Value : policy } ,
2026-08-08 14:37:54 +00:00
{ Name : "npc_registry" , Value : v . npcResolver . Seeded ( ) . IdentityDigest ( ) } ,
2026-07-23 13:35:15 +00:00
}
}
2026-08-05 18:55:58 +00:00
func ( v * Validator ) Validate ( _ context . Context , req contracts . TypedValidationRequest [ dnd . NPCOccurrenceList ] ) ( contracts . ValidationResult , error ) {
2026-08-05 19:00:55 +00:00
if occurrenceshape . Validate ( req . Value ) != nil {
2026-07-25 12:57:31 +00:00
return contracts . ValidationResult { Approved : true } , nil
}
index := source . NewDocumentIndex ( req . Source )
if ! allSourceRefsValid ( index , req . Value ) {
2026-07-23 13:35:15 +00:00
return contracts . ValidationResult { Approved : true } , nil
}
if v == nil || v . npcResolver == nil {
2026-08-05 19:00:55 +00:00
return contracts . ValidationResult { } , fmt . Errorf ( "NPC occurrence invariants validator must not be nil" )
2026-07-23 13:35:15 +00:00
}
npcRegistry , err := v . npcResolver . Resolve ( req . References )
if err != nil {
return contracts . ValidationResult { } , fmt . Errorf ( "resolve NPC registry: %w" , err )
}
if ! npcRegistry . Bound ( ) {
2026-08-27 11:50:37 +00:00
return contracts . ValidationResult { Approved : false , ReasonCode : ReasonCode , Message : "invalid NPC occurrence normalization: NPC registry reference is required" , CorrectionGuidance : "Return NPC occurrences using contextual NPC names that match the supplied NPC registry." } , nil
2026-07-23 13:35:15 +00:00
}
2026-07-25 13:30:23 +00:00
issues := issuesFor ( shared . NewSourceRefOrderFromIndex ( index ) , req . Value , npcRegistry )
2026-07-23 13:35:15 +00:00
if len ( issues ) == 0 {
return contracts . ValidationResult { Approved : true } , nil
}
return contracts . ValidationResult {
2026-08-27 11:50:37 +00:00
Approved : false ,
ReasonCode : ReasonCode ,
Message : diagnostics . Aggregate ( "invalid NPC occurrence normalization" , issues ) ,
CorrectionGuidance : "Return NPC occurrences in canonical transcript order, without duplicates, using contextual NPC names that match the supplied registry and supported interaction kinds." ,
2026-07-23 13:35:15 +00:00
} , nil
}
2026-08-05 18:55:58 +00:00
func allSourceRefsValid ( index source . DocumentIndex , value dnd . NPCOccurrenceList ) bool {
for _ , occurrence := range value . Occurrences {
2026-08-06 13:40:42 +00:00
if ! occurrencemodel . ValidSourceRefs ( index , occurrence . SourceRefs ) {
2026-07-25 13:01:31 +00:00
return false
2026-07-23 13:35:15 +00:00
}
}
return true
}
2026-08-05 18:55:58 +00:00
func issuesFor ( order shared . SourceRefOrder , value dnd . NPCOccurrenceList , npcRegistry * npcregistry . Registry ) [ ] string {
2026-07-23 13:35:15 +00:00
issues := make ( [ ] string , 0 )
2026-08-05 18:55:58 +00:00
for index , occurrence := range value . Occurrences {
prefix := fmt . Sprintf ( "occurrences[%d]" , index )
canonical , ok := npcRegistry . LookupID ( occurrence . NPCID )
if ! ok {
issues = append ( issues , prefix + ".npc_id is not in the NPC registry: " + diagnostics . Quote ( occurrence . NPCID ) )
} else if occurrence . Name != canonical . Name {
issues = append ( issues , prefix + ".name does not match npc_id: " + diagnostics . Quote ( occurrence . Name ) )
2026-07-23 13:35:15 +00:00
}
2026-08-05 18:55:58 +00:00
for refIndex := 1 ; refIndex < len ( occurrence . SourceRefs ) ; refIndex ++ {
previous := occurrence . SourceRefs [ refIndex - 1 ]
current := occurrence . SourceRefs [ refIndex ]
2026-07-24 14:20:10 +00:00
if order . Less ( current , previous ) {
2026-07-23 13:35:15 +00:00
issues = append ( issues , fmt . Sprintf ( "%s.source_refs are not in canonical order at index %d" , prefix , refIndex ) )
} else if current == previous {
issues = append ( issues , fmt . Sprintf ( "%s.source_refs[%d] duplicates the previous reference" , prefix , refIndex ) )
}
}
}
2026-08-05 18:55:58 +00:00
if ! sort . SliceIsSorted ( value . Occurrences , func ( left , right int ) bool {
2026-08-06 13:40:42 +00:00
return occurrencemodel . Less ( order , value . Occurrences [ left ] , value . Occurrences [ right ] )
2026-07-23 13:35:15 +00:00
} ) {
2026-08-05 18:55:58 +00:00
issues = append ( issues , "occurrences are not in canonical order" )
2026-07-23 13:35:15 +00:00
}
seen := make ( map [ string ] int )
2026-08-05 18:55:58 +00:00
for index , occurrence := range value . Occurrences {
2026-08-06 13:40:42 +00:00
key := occurrencemodel . ExactIdentity ( occurrence )
2026-07-23 13:35:15 +00:00
if previous , ok := seen [ key ] ; ok {
2026-08-05 18:55:58 +00:00
issues = append ( issues , fmt . Sprintf ( "occurrences[%d] duplicates occurrence %d" , index , previous ) )
2026-07-23 13:35:15 +00:00
continue
}
seen [ key ] = index
}
return issues
}
func Spec ( ) pipeline . ValidatorSpec {
return pipeline . ValidatorSpec { Key : Key , ExecutionClass : contracts . ExecutionClassDeterministic }
}
func Register ( registry * pipeline . ValidatorRegistry ) error {
2026-08-05 18:55:58 +00:00
return pipeline . RegisterTypedValidatorBuilder ( registry , dnd . NPCOccurrenceListKind , Spec ( ) , validateOptions , func ( request pipeline . BuildRequest ) ( contracts . TypedValidator [ dnd . NPCOccurrenceList ] , error ) {
2026-07-23 13:35:15 +00:00
options , err := DecodeOptions ( request . Options )
if err != nil {
return nil , err
}
return New ( options , request . References )
} )
}
func DecodeOptions ( options map [ string ] any ) ( Options , error ) {
if err := pipeline . RejectUnknownOptions ( options ) ; err != nil {
return Options { } , err
}
return Options { } , nil
}
func validateOptions ( options map [ string ] any ) error { _ , err := DecodeOptions ( options ) ; return err }