2026-08-05 20:00:20 +00:00
// Package sourcerefs validates D&D item-occurrence transcript evidence.
2026-07-25 21:52:59 +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 20:00:20 +00:00
itemoccurrenceshape "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/itemoccurrences/shape"
2026-07-25 21:52:59 +00:00
)
const (
2026-08-05 20:00:20 +00:00
Key = "extract/dnd/item-occurrences/source_refs"
ReasonCode = "invalid_item_occurrence_source_references"
policy = "dnd.item_occurrences.source_refs.v1"
2026-07-25 21:52:59 +00:00
)
type Options struct { }
type Validator struct { }
2026-08-05 19:55:34 +00:00
var _ contracts . TypedValidator [ dnd . ItemOccurrenceList ] = ( * Validator ) ( nil )
2026-07-25 21:52:59 +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 19:55:34 +00:00
func ( v * Validator ) Validate ( _ context . Context , req contracts . TypedValidationRequest [ dnd . ItemOccurrenceList ] ) ( contracts . ValidationResult , error ) {
2026-07-25 21:52:59 +00:00
if req . Stage == string ( pipeline . StageExtract ) && req . Chunk == nil {
2026-08-05 19:55:34 +00:00
return contracts . ValidationResult { } , fmt . Errorf ( "item occurrence source-reference validator requires the current extraction chunk" )
2026-07-25 21:52:59 +00:00
}
2026-08-05 20:00:20 +00:00
if itemoccurrenceshape . Validate ( req . Value ) != nil {
2026-07-25 21:52:59 +00:00
return contracts . ValidationResult { Approved : true } , nil
}
index := source . NewDocumentIndex ( req . Source )
2026-07-25 23:59:56 +00:00
var coverage * chunkCoverage
if req . Stage == string ( pipeline . StageExtract ) {
coverage = newChunkCoverage ( req . Chunk )
}
2026-07-25 21:52:59 +00:00
issues := make ( [ ] string , 0 )
2026-08-05 20:00:20 +00:00
for occurrenceIndex , occurrence := range req . Value . Occurrences {
for refIndex , ref := range occurrence . SourceRefs {
2026-07-25 21:52:59 +00:00
if err := index . ValidateRef ( ref ) ; err != nil {
2026-08-05 20:00:20 +00:00
issues = append ( issues , fmt . Sprintf ( "occurrences[%d].source_refs[%d]: %s" , occurrenceIndex , refIndex , diagnostics . Truncate ( err . Error ( ) ) ) )
2026-07-25 21:52:59 +00:00
continue
}
2026-07-25 23:59:56 +00:00
if coverage != nil && ! coverage . contains ( req . Source , ref ) {
2026-08-05 20:00:20 +00:00
issues = append ( issues , fmt . Sprintf ( "occurrences[%d].source_refs[%d]: source reference is outside the current extraction chunk" , occurrenceIndex , refIndex ) )
2026-07-25 21:52:59 +00:00
}
}
}
if len ( issues ) == 0 {
return contracts . ValidationResult { Approved : true } , nil
}
2026-08-05 19:55:34 +00:00
return contracts . ValidationResult { Approved : false , ReasonCode : ReasonCode , Message : diagnostics . Aggregate ( "invalid item occurrence source references" , issues ) } , nil
2026-07-25 21:52:59 +00:00
}
2026-07-25 23:59:56 +00:00
type chunkCoverage struct {
sourceID string
unitIDs map [ int ] struct { }
}
func newChunkCoverage ( chunk * source . Chunk ) * chunkCoverage {
coverage := & chunkCoverage {
sourceID : chunk . SourceID ,
unitIDs : make ( map [ int ] struct { } , len ( chunk . Units ) ) ,
2026-07-25 21:52:59 +00:00
}
2026-07-25 23:59:56 +00:00
for _ , unit := range chunk . Units {
coverage . unitIDs [ unit . ID ] = struct { } { }
}
return coverage
2026-07-25 21:52:59 +00:00
}
2026-07-25 23:59:56 +00:00
func ( coverage * chunkCoverage ) contains ( doc * source . SourceDocument , ref source . SourceRef ) bool {
if coverage == nil || doc == nil || ref . SourceID != coverage . sourceID {
2026-07-25 21:52:59 +00:00
return false
}
2026-07-25 22:00:32 +00:00
start , startOK := source . UnitIndex ( doc , ref . StartUnitID )
end , endOK := source . UnitIndex ( doc , ref . EndUnitID )
if ! startOK || ! endOK || start > end {
return false
}
for position := start ; position <= end ; position ++ {
2026-07-25 23:59:56 +00:00
if _ , found := coverage . unitIDs [ doc . Units [ position ] . ID ] ; ! found {
2026-07-25 22:00:32 +00:00
return false
}
2026-07-25 21:52:59 +00:00
}
2026-07-25 22:00:32 +00:00
return true
2026-07-25 21:52:59 +00:00
}
func Spec ( ) pipeline . ValidatorSpec {
return pipeline . ValidatorSpec { Key : Key , ExecutionClass : contracts . ExecutionClassDeterministic }
}
func Register ( registry * pipeline . ValidatorRegistry ) error {
2026-08-05 19:55:34 +00:00
return pipeline . RegisterTypedValidatorBuilder ( registry , dnd . ItemOccurrenceListKind , Spec ( ) , validateOptions , func ( request pipeline . BuildRequest ) ( contracts . TypedValidator [ dnd . ItemOccurrenceList ] , error ) {
2026-07-25 21:52:59 +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 }