2026-08-05 19:00:55 +00:00
// Package sourcerefs validates D&D NPC occurrence transcript evidence.
2026-07-23 13:35:15 +00:00
package sourcerefs
import (
"context"
"fmt"
"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"
"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 = "extract/dnd/npc-occurrences/source_refs"
2026-08-05 20:17:20 +00:00
ReasonCode = "invalid_npc_occurrence_source_refs"
2026-08-05 19:00:55 +00:00
policy = "dnd.npc_occurrences.validator.source_refs.v2"
2026-07-23 13:35:15 +00:00
)
type Options struct { }
type Validator struct { }
2026-08-05 18:55:58 +00:00
var _ contracts . TypedValidator [ dnd . NPCOccurrenceList ] = ( * Validator ) ( nil )
2026-07-23 13:35:15 +00:00
var _ pipeline . CheckpointFingerprintProvider = ( * Validator ) ( nil )
func New ( Options ) * Validator { return & Validator { } }
func ( v * Validator ) Name ( ) string { return Key }
func ( v * Validator ) ExecutionClass ( ) contracts . ExecutionClass {
return contracts . ExecutionClassDeterministic
}
func ( v * Validator ) CheckpointFingerprints ( ) [ ] pipeline . CheckpointFingerprint {
return [ ] pipeline . CheckpointFingerprint { { Name : "policy" , Value : policy } }
}
2026-08-05 18:55:58 +00:00
func ( v * Validator ) Validate ( _ context . Context , req contracts . TypedValidationRequest [ dnd . NPCOccurrenceList ] ) ( contracts . ValidationResult , error ) {
2026-07-23 14:25:32 +00:00
if req . Stage == string ( pipeline . StageExtract ) && req . Chunk == nil {
2026-08-05 19:00:55 +00:00
return contracts . ValidationResult { } , fmt . Errorf ( "NPC occurrence source-reference validator requires the current extraction chunk" )
2026-07-23 14:25:32 +00:00
}
2026-08-05 19:00:55 +00:00
if occurrenceshape . Validate ( req . Value ) != nil {
2026-07-23 13:35:15 +00:00
return contracts . ValidationResult { Approved : true } , nil
}
2026-07-25 12:57:31 +00:00
index := source . NewDocumentIndex ( req . Source )
2026-07-23 13:35:15 +00:00
issues := make ( [ ] string , 0 )
2026-08-05 18:55:58 +00:00
for occurrenceIndex , occurrence := range req . Value . Occurrences {
for refIndex , ref := range occurrence . SourceRefs {
2026-07-25 12:57:31 +00:00
if err := index . ValidateRef ( ref ) ; err != nil {
2026-08-05 18:55:58 +00:00
issues = append ( issues , fmt . Sprintf ( "occurrences[%d].source_refs[%d]: %s" , occurrenceIndex , refIndex , diagnostics . Truncate ( err . Error ( ) ) ) )
2026-07-23 14:25:32 +00:00
continue
}
if req . Stage == string ( pipeline . StageExtract ) && ! chunkContainsRef ( req . Chunk , ref ) {
issues = append ( issues , fmt . Sprintf (
2026-08-05 18:55:58 +00:00
"occurrences[%d].source_refs[%d]: source reference is outside the current extraction chunk" ,
occurrenceIndex , refIndex ,
2026-07-23 14:25:32 +00:00
) )
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 source references" , issues ) ,
CorrectionGuidance : "Return NPC occurrences whose source references identify valid transcript ranges within the supplied extraction chunk and directly support each occurrence." ,
2026-07-23 13:35:15 +00:00
} , nil
}
2026-07-23 14:25:32 +00:00
func chunkContainsRef ( chunk * source . Chunk , ref source . SourceRef ) bool {
if chunk == nil || ref . SourceID != chunk . SourceID {
return false
}
startFound := false
endFound := false
for _ , unit := range chunk . Units {
startFound = startFound || unit . ID == ref . StartUnitID
endFound = endFound || unit . ID == ref . EndUnitID
}
return startFound && endFound
}
2026-07-23 13:35:15 +00:00
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 ) , nil
} )
}
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 }