2026-07-21 02:36:59 +00:00
// Package npcs normalizes merged D&D non-player character records.
package npcs
import (
"context"
2026-07-26 01:36:32 +00:00
"errors"
2026-07-21 02:36:59 +00:00
"fmt"
"reflect"
2026-07-26 01:36:32 +00:00
"sort"
2026-07-21 02:36:59 +00:00
"strconv"
"strings"
"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/npcs/identity"
2026-07-24 14:20:10 +00:00
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared"
2026-07-21 04:34:56 +00:00
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics"
2026-07-21 02:36:59 +00:00
)
const (
2026-07-26 01:36:32 +00:00
Key = "dnd/npcs"
normalizationPolicy = "dnd.npcs.normalize.v3"
semanticContextPolicy = "dnd.npcs.semantic_context.v1"
NormalizationPolicy = normalizationPolicy
ReasonCodeNPCFieldsNormalized = "npc_fields_normalized"
ReasonCodeNPCIDRecomputed = "npc_id_recomputed"
ReasonCodeSourceReferencesNormalized = "source_references_normalized"
ReasonCodeDuplicateNPCCollapsed = "duplicate_npc_collapsed"
ReasonCodeNPCSemanticProposalInvalid = "npc_semantic_proposal_invalid"
ReasonCodeNPCSemanticReconciliationExhausted = "npc_semantic_reconciliation_exhausted"
ReasonCodeNPCNormalizationWarningsOmitted = "npc_normalization_warnings_omitted"
2026-07-21 02:36:59 +00:00
)
var requiredCapabilities = [ ] string { "merged" }
var providedCapabilities = [ ] string { "normalized" }
var _ contracts . Normalizer [ dnd . NPCList ] = ( * Normalizer ) ( nil )
var _ contracts . ManifestMetadataProvider = ( * Normalizer ) ( nil )
var _ pipeline . CheckpointFingerprintProvider = ( * Normalizer ) ( nil )
type Options struct { }
2026-07-26 01:36:32 +00:00
type Normalizer struct {
llm contracts . StructuredLLMClient
promptSHA string
responseSchemaSHA string
}
func New ( llmClient contracts . StructuredLLMClient , _ Options ) ( * Normalizer , error ) {
if llmClient == nil {
return nil , normalizerErrorf ( "LLM client must not be nil" )
}
2026-07-28 16:42:41 +00:00
promptSHA , err := promptAssetMetadata ( )
2026-07-26 01:36:32 +00:00
if err != nil {
return nil , normalizerErrorf ( "load prompt metadata: %w" , err )
}
responseSchema , err := loadResponseSchema ( )
if err != nil {
return nil , normalizerErrorf ( "load response schema: %w" , err )
}
return & Normalizer { llm : llmClient , promptSHA : promptSHA , responseSchemaSHA : responseSchema . SHA256 } , nil
}
2026-07-22 18:51:26 +00:00
func ( n * Normalizer ) Key ( ) string { return Key }
2026-07-21 02:36:59 +00:00
func ( n * Normalizer ) ReferenceSlots ( ) [ ] contracts . ReferenceSlot { return nil }
func ( n * Normalizer ) ManifestMetadata ( ) map [ string ] any {
if n == nil {
return nil
}
2026-07-26 01:36:32 +00:00
return map [ string ] any {
"prompt_id" : PromptID ,
"prompt_version" : SchemaVersion ,
"prompt_sha256" : n . promptSHA ,
"response_schema_key" : string ( ResponseSchemaKey ) ,
"response_schema_id" : ResponseSchemaID ,
"response_schema_name" : ResponseSchemaName ,
"response_schema_version" : SchemaVersion ,
"response_schema_sha256" : n . responseSchemaSHA ,
"identity_policy" : identity . Policy ,
"normalization_policy" : normalizationPolicy ,
"semantic_context_policy" : semanticContextPolicy ,
"semantic_context_radius" : semanticContextRadius ,
}
2026-07-21 02:36:59 +00:00
}
func ( n * Normalizer ) CheckpointFingerprints ( ) [ ] pipeline . CheckpointFingerprint {
if n == nil {
return nil
}
return [ ] pipeline . CheckpointFingerprint {
2026-07-26 01:36:32 +00:00
{ Name : "prompt" , Value : n . promptSHA } ,
{ Name : "response_schema" , Value : n . responseSchemaSHA } ,
2026-07-21 02:36:59 +00:00
{ Name : "identity_policy" , Value : identity . Policy } ,
2026-07-21 14:58:26 +00:00
{ Name : "normalization_policy" , Value : normalizationPolicy } ,
2026-07-26 01:36:32 +00:00
{ Name : "semantic_context_policy" , Value : fmt . Sprintf ( "%s:%d" , semanticContextPolicy , semanticContextRadius ) } ,
2026-07-21 02:36:59 +00:00
}
}
func ( n * Normalizer ) Normalize ( ctx context . Context , req contracts . TypedNormalizeRequest [ dnd . NPCList ] ) ( contracts . TypedNormalizeResult [ dnd . NPCList ] , error ) {
if n == nil {
return contracts . TypedNormalizeResult [ dnd . NPCList ] { } , normalizerErrorf ( "normalizer must not be nil" )
}
2026-07-26 01:36:32 +00:00
if n . llm == nil {
return contracts . TypedNormalizeResult [ dnd . NPCList ] { } , normalizerErrorf ( "LLM client must not be nil" )
}
2026-07-21 02:36:59 +00:00
if ctx == nil {
return contracts . TypedNormalizeResult [ dnd . NPCList ] { } , normalizerErrorf ( "context must not be nil" )
}
if err := ctx . Err ( ) ; err != nil {
return contracts . TypedNormalizeResult [ dnd . NPCList ] { } , normalizerErrorf ( "context error before normalize: %w" , err )
}
2026-07-26 01:36:32 +00:00
2026-07-24 14:20:10 +00:00
order := shared . NewSourceRefOrder ( req . Source )
2026-07-26 01:36:32 +00:00
records , warnings := preprocessRecords ( req . MergeOutput . Value , order )
deterministic := recordList ( records )
materials , ready , err := buildDefaultNormalizeContextMaterials ( req . Source , recordValues ( records ) )
if err != nil {
return contracts . TypedNormalizeResult [ dnd . NPCList ] { } , normalizerErrorf ( "build semantic context: %w" , err )
}
if ! ready {
return contracts . TypedNormalizeResult [ dnd . NPCList ] { Value : deterministic , Warnings : limitWarnings ( warnings ) } , nil
}
var response normalizeProposalResponse
if _ , err := n . llm . CompleteStructured ( ctx , contracts . StructuredCompletionRequest {
StageName : Key , PromptID : PromptID , PromptVersion : SchemaVersion ,
ProfileID : req . LLMProfile , SessionID : req . SessionID ,
Inputs : contracts . LLMInputSet { "candidates" : materials . Candidates , "transcript" : materials . Transcript } ,
} , & response ) ; err != nil {
if errors . Is ( err , contracts . ErrInvalidStructuredOutput ) {
return n . invalidStructuredResult ( deterministic , warnings ) , nil
}
return contracts . TypedNormalizeResult [ dnd . NPCList ] { } , normalizerErrorf ( "complete structured output: %w" , err )
}
assessment := assessProposal ( response , records , materials . candidatePositions )
applied , semanticWarnings := applySafeGroups ( records , assessment . safeGroups , order )
warnings = append ( warnings , semanticWarnings ... )
if assessment . discardedGroups == 0 {
return contracts . TypedNormalizeResult [ dnd . NPCList ] { Value : recordList ( applied ) , Warnings : limitWarnings ( warnings ) } , nil
}
return retryResult ( recordList ( applied ) , warnings , assessment ) , nil
}
func ( n * Normalizer ) invalidStructuredResult ( value dnd . NPCList , warnings [ ] contracts . Warning ) contracts . TypedNormalizeResult [ dnd . NPCList ] {
return contracts . TypedNormalizeResult [ dnd . NPCList ] {
Value : value ,
Warnings : limitWarningsForRetry ( warnings ) ,
Retry : & contracts . NormalizeRetry {
ReasonCode : ReasonCodeNPCSemanticProposalInvalid ,
Message : "semantic proposal requires retry: invalid structured output" ,
FallbackWarnings : [ ] contracts . Warning { semanticFallbackWarning ( - 1 ) } ,
} ,
}
}
func retryResult ( value dnd . NPCList , warnings [ ] contracts . Warning , assessment proposalAssessment ) contracts . TypedNormalizeResult [ dnd . NPCList ] {
return contracts . TypedNormalizeResult [ dnd . NPCList ] {
Value : value ,
Warnings : limitWarningsForRetry ( warnings ) ,
Retry : & contracts . NormalizeRetry {
ReasonCode : ReasonCodeNPCSemanticProposalInvalid ,
Message : diagnostics . Aggregate ( "semantic proposal requires retry" , assessment . issues ) ,
FallbackWarnings : [ ] contracts . Warning { semanticFallbackWarning ( assessment . discardedGroups ) } ,
} ,
}
}
func semanticFallbackWarning ( discardedGroups int ) contracts . Warning {
message := "semantic proposal could not be applied"
if discardedGroups >= 0 {
message = fmt . Sprintf ( "%d proposal group(s) omitted after semantic proposal retry exhaustion" , discardedGroups )
}
return contracts . Warning { Scope : "npcs" , ReasonCode : ReasonCodeNPCSemanticReconciliationExhausted , Message : message }
}
func limitWarnings ( warnings [ ] contracts . Warning ) [ ] contracts . Warning {
return diagnostics . LimitWarnings ( warnings , "npcs" , ReasonCodeNPCNormalizationWarningsOmitted )
}
func limitWarningsForRetry ( warnings [ ] contracts . Warning ) [ ] contracts . Warning {
if warnings == nil {
return nil
}
if len ( warnings ) < diagnostics . MaxWarnings {
return append ( [ ] contracts . Warning ( nil ) , warnings ... )
}
displayed := diagnostics . MaxWarnings - 2
bounded := append ( [ ] contracts . Warning ( nil ) , warnings [ : displayed ] ... )
return append ( bounded , contracts . Warning {
Scope : "npcs" , ReasonCode : ReasonCodeNPCNormalizationWarningsOmitted ,
Message : fmt . Sprintf ( "%d additional warning(s) omitted" , len ( warnings ) - displayed ) ,
} )
2026-07-21 02:36:59 +00:00
}
type normalizedRecord struct {
2026-07-26 01:36:32 +00:00
npc dnd . NPC
inputIndexes [ ] int
earliest int
2026-07-21 02:36:59 +00:00
}
2026-07-26 01:36:32 +00:00
func preprocessRecords ( input dnd . NPCList , order shared . SourceRefOrder ) ( [ ] normalizedRecord , [ ] contracts . Warning ) {
2026-07-21 02:36:59 +00:00
if input . NPCs == nil {
2026-07-26 01:36:32 +00:00
return nil , nil
2026-07-21 02:36:59 +00:00
}
records := make ( [ ] normalizedRecord , len ( input . NPCs ) )
warnings := make ( [ ] contracts . Warning , 0 )
for index , inputNPC := range input . NPCs {
2026-07-24 14:20:10 +00:00
npc , fieldsChanged , referencesChanged := normalizeRecord ( inputNPC , order )
2026-07-26 01:36:32 +00:00
records [ index ] = normalizedRecord { npc : npc , inputIndexes : [ ] int { index } , earliest : index }
2026-07-21 02:36:59 +00:00
if fieldsChanged {
2026-07-22 18:51:26 +00:00
warnings = append ( warnings , contracts . Warning { Scope : npcScope ( index ) , ReasonCode : ReasonCodeNPCFieldsNormalized , Message : fmt . Sprintf ( "input index %d: NPC name normalized for %s" , index , diagnostics . Quote ( inputNPC . Name ) ) } )
2026-07-21 02:36:59 +00:00
}
if referencesChanged {
2026-07-22 18:51:26 +00:00
warnings = append ( warnings , contracts . Warning { Scope : npcScope ( index ) , ReasonCode : ReasonCodeSourceReferencesNormalized , Message : fmt . Sprintf ( "input index %d: source references normalized (original count %d, final count %d)" , index , len ( inputNPC . SourceRefs ) , len ( npc . SourceRefs ) ) } )
2026-07-21 02:36:59 +00:00
}
if inputNPC . ID != npc . ID {
2026-07-22 18:51:26 +00:00
warnings = append ( warnings , contracts . Warning { Scope : npcScope ( index ) , ReasonCode : ReasonCodeNPCIDRecomputed , Message : fmt . Sprintf ( "input index %d: NPC ID recomputed from %s" , index , diagnostics . Quote ( npc . Name ) ) } )
2026-07-21 02:36:59 +00:00
}
}
2026-07-22 18:51:26 +00:00
groups := canonicalNameGroups ( records )
2026-07-26 01:36:32 +00:00
output := make ( [ ] normalizedRecord , 0 , len ( groups ) )
2026-07-22 18:51:26 +00:00
for _ , members := range groups {
2026-07-24 14:20:10 +00:00
consolidated , referencesChanged := consolidate ( records , members , order )
2026-07-26 01:36:32 +00:00
output = append ( output , consolidated )
retainedIndex := consolidated . earliest
2026-07-22 18:51:26 +00:00
if referencesChanged {
2026-07-26 01:36:32 +00:00
warnings = append ( warnings , contracts . Warning { Scope : npcScope ( retainedIndex ) , ReasonCode : ReasonCodeSourceReferencesNormalized , Message : fmt . Sprintf ( "input index %d: source references normalized during identity consolidation (final count %d)" , retainedIndex , len ( consolidated . npc . SourceRefs ) ) } )
2026-07-21 02:36:59 +00:00
}
if len ( members ) > 1 {
2026-07-26 01:36:32 +00:00
warnings = append ( warnings , duplicateWarning ( retainedIndex , memberInputIndexes ( records , members [ 1 : ] ) ) )
2026-07-21 02:36:59 +00:00
}
}
return output , warnings
}
2026-07-24 14:20:10 +00:00
func normalizeRecord ( input dnd . NPC , order shared . SourceRefOrder ) ( dnd . NPC , bool , bool ) {
2026-07-21 02:36:59 +00:00
output := cloneNPC ( input )
output . Name = identity . NormalizeDisplay ( input . Name )
2026-07-25 13:01:31 +00:00
output . SourceRefs = order . Canonicalize ( input . SourceRefs )
2026-07-21 02:36:59 +00:00
output . ID = identity . DeriveID ( output . Name )
2026-07-22 18:51:26 +00:00
return output , input . Name != output . Name , ! reflect . DeepEqual ( input . SourceRefs , output . SourceRefs )
2026-07-21 02:36:59 +00:00
}
func cloneNPC ( input dnd . NPC ) dnd . NPC {
2026-07-22 18:51:26 +00:00
input . SourceRefs = cloneSourceRefs ( input . SourceRefs )
return input
}
func canonicalNameGroups ( records [ ] normalizedRecord ) [ ] [ ] int {
groups := make ( [ ] [ ] int , 0 , len ( records ) )
ownerByKey := make ( map [ string ] int , len ( records ) )
for index , record := range records {
key := identity . ComparisonKey ( record . npc . Name )
if key != "" {
if groupIndex , ok := ownerByKey [ key ] ; ok {
groups [ groupIndex ] = append ( groups [ groupIndex ] , index )
continue
2026-07-21 02:36:59 +00:00
}
2026-07-22 18:51:26 +00:00
ownerByKey [ key ] = len ( groups )
2026-07-21 02:36:59 +00:00
}
2026-07-22 18:51:26 +00:00
groups = append ( groups , [ ] int { index } )
2026-07-21 02:36:59 +00:00
}
2026-07-22 18:51:26 +00:00
return groups
2026-07-21 02:36:59 +00:00
}
2026-07-26 01:36:32 +00:00
func consolidate ( records [ ] normalizedRecord , members [ ] int , order shared . SourceRefOrder ) ( normalizedRecord , bool ) {
output := cloneRecord ( records [ members [ 0 ] ] )
originalRefs := cloneSourceRefs ( output . npc . SourceRefs )
2026-07-21 02:36:59 +00:00
for _ , member := range members [ 1 : ] {
2026-07-26 01:36:32 +00:00
output . npc . SourceRefs = append ( output . npc . SourceRefs , records [ member ] . npc . SourceRefs ... )
output . inputIndexes = append ( output . inputIndexes , records [ member ] . inputIndexes ... )
if records [ member ] . earliest < output . earliest {
output . earliest = records [ member ] . earliest
}
2026-07-21 02:36:59 +00:00
}
2026-07-26 01:36:32 +00:00
output . inputIndexes = sortedUniqueIndexes ( output . inputIndexes )
output . npc . SourceRefs = order . Canonicalize ( output . npc . SourceRefs )
output . npc . ID = identity . DeriveID ( output . npc . Name )
return output , ! reflect . DeepEqual ( originalRefs , output . npc . SourceRefs )
}
func cloneRecord ( input normalizedRecord ) normalizedRecord {
input . npc = cloneNPC ( input . npc )
input . inputIndexes = append ( [ ] int ( nil ) , input . inputIndexes ... )
return input
}
func memberInputIndexes ( records [ ] normalizedRecord , members [ ] int ) [ ] int {
indexes := make ( [ ] int , 0 , len ( members ) )
for _ , member := range members {
indexes = append ( indexes , records [ member ] . inputIndexes ... )
}
return sortedUniqueIndexes ( indexes )
}
func sortedUniqueIndexes ( indexes [ ] int ) [ ] int {
if len ( indexes ) == 0 {
return nil
}
out := append ( [ ] int ( nil ) , indexes ... )
sort . Ints ( out )
write := 1
for _ , index := range out [ 1 : ] {
if index != out [ write - 1 ] {
out [ write ] = index
write ++
}
}
return out [ : write ]
}
func recordValues ( records [ ] normalizedRecord ) [ ] dnd . NPC {
if records == nil {
return nil
}
values := make ( [ ] dnd . NPC , len ( records ) )
for index , record := range records {
values [ index ] = cloneNPC ( record . npc )
}
return values
}
func recordList ( records [ ] normalizedRecord ) dnd . NPCList {
if records == nil {
return dnd . NPCList { }
}
return dnd . NPCList { NPCs : recordValues ( records ) }
2026-07-21 02:36:59 +00:00
}
2026-07-22 18:51:26 +00:00
func cloneSourceRefs ( input [ ] source . SourceRef ) [ ] source . SourceRef {
2026-07-21 02:36:59 +00:00
if input == nil {
return nil
}
2026-07-22 18:51:26 +00:00
return append ( [ ] source . SourceRef ( nil ) , input ... )
2026-07-21 02:36:59 +00:00
}
func duplicateWarning ( retainedIndex int , removed [ ] int ) contracts . Warning {
const maxDisplayedIndices = 20
displayed := removed
if len ( displayed ) > maxDisplayedIndices {
displayed = displayed [ : maxDisplayedIndices ]
}
indices := make ( [ ] string , len ( displayed ) )
for index , removedIndex := range displayed {
indices [ index ] = strconv . Itoa ( removedIndex )
}
message := fmt . Sprintf ( "retained input index %d; removed input indices [%s]" , retainedIndex , strings . Join ( indices , ", " ) )
if omitted := len ( removed ) - len ( displayed ) ; omitted > 0 {
message += fmt . Sprintf ( "; %d additional removed input indices omitted" , omitted )
}
2026-07-22 18:51:26 +00:00
return contracts . Warning { Scope : npcScope ( retainedIndex ) , ReasonCode : ReasonCodeDuplicateNPCCollapsed , Message : message }
2026-07-21 02:36:59 +00:00
}
func npcScope ( index int ) string { return fmt . Sprintf ( "npcs[%d]" , index ) }
func ModuleSpec ( ) pipeline . ModuleSpec {
2026-08-03 16:49:51 +00:00
return pipeline . ModuleSpec { Key : Key , Stage : pipeline . StageNormalize , ExecutionClass : contracts . ExecutionClassLLMBacked , Requires : append ( [ ] string ( nil ) , requiredCapabilities ... ) , Provides : append ( [ ] string ( nil ) , providedCapabilities ... ) , ArtifactKind : dnd . NPCListKind }
2026-07-21 02:36:59 +00:00
}
func Register ( registry * pipeline . NormalizerRegistry ) error {
return pipeline . RegisterNormalizerBuilder ( registry , ModuleSpec ( ) , validateOptions , func ( request pipeline . BuildRequest ) ( contracts . Normalizer [ dnd . NPCList ] , error ) {
options , err := DecodeOptions ( request . Options )
if err != nil {
return nil , err
}
2026-07-26 01:36:32 +00:00
return New ( request . Dependencies . LLM , options )
2026-07-21 02:36:59 +00:00
} )
}
2026-07-22 18:51:26 +00:00
func validateOptions ( options map [ string ] any ) error { _ , err := DecodeOptions ( options ) ; return err }
2026-07-21 02:36:59 +00:00
func DecodeOptions ( options map [ string ] any ) ( Options , error ) {
if err := pipeline . RejectUnknownOptions ( options ) ; err != nil {
return Options { } , normalizerErrorf ( "%w" , err )
}
return Options { } , nil
}
func normalizerErrorf ( format string , args ... any ) error {
return fmt . Errorf ( "dnd npcs normalizer: " + format , args ... )
}