2026-08-05 18:38:22 +00:00
// Package npcregistry normalizes merged D&D non-player character records.
package npcregistry
2026-07-21 02:36:59 +00:00
import (
"context"
"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"
2026-08-09 16:38:43 +00:00
"gitea.maximumdirect.net/eric/notarius/internal/framework/semanticreconcile"
2026-07-21 02:36:59 +00:00
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
2026-11-22 00:27:34 +00:00
registrycodec "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/codec/npcregistry"
2026-07-21 02:36:59 +00:00
"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-11-22 00:27:34 +00:00
"go.maximumdirect.net/promptkit"
2026-07-21 02:36:59 +00:00
)
const (
2026-11-22 00:27:34 +00:00
Key = "dnd/npc-registry"
PromptID = "notarius/dnd.npc_registry.normalize"
CorrectionPromptID = "notarius/dnd.npc_registry.normalize.repair"
2026-07-26 01:36:32 +00:00
ReasonCodeNPCFieldsNormalized = "npc_fields_normalized"
ReasonCodeNPCIDRecomputed = "npc_id_recomputed"
ReasonCodeSourceReferencesNormalized = "source_references_normalized"
ReasonCodeDuplicateNPCCollapsed = "duplicate_npc_collapsed"
ReasonCodeNPCSemanticReconciliationExhausted = "npc_semantic_reconciliation_exhausted"
2026-07-21 02:36:59 +00:00
)
var requiredCapabilities = [ ] string { "merged" }
var providedCapabilities = [ ] string { "normalized" }
2026-08-05 18:25:04 +00:00
var _ contracts . Normalizer [ dnd . NPCRegistry ] = ( * Normalizer ) ( nil )
2026-07-21 02:36:59 +00:00
type Options struct { }
2026-07-26 01:36:32 +00:00
type Normalizer struct {
2026-11-22 00:27:34 +00:00
engine * promptkit . Engine
2026-07-26 01:36:32 +00:00
}
2026-11-22 00:27:34 +00:00
func New ( engine * promptkit . Engine , _ Options ) ( * Normalizer , error ) {
if engine == nil {
return nil , normalizerErrorf ( "native engine must not be nil" )
2026-07-26 01:36:32 +00:00
}
2026-08-09 16:38:43 +00:00
return & Normalizer { engine : engine } , nil
2026-07-26 01:36:32 +00:00
}
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 }
2026-08-05 18:25:04 +00:00
func ( n * Normalizer ) Normalize ( ctx context . Context , req contracts . TypedNormalizeRequest [ dnd . NPCRegistry ] ) ( contracts . TypedNormalizeResult [ dnd . NPCRegistry ] , error ) {
2026-07-21 02:36:59 +00:00
if n == nil {
2026-08-05 18:25:04 +00:00
return contracts . TypedNormalizeResult [ dnd . NPCRegistry ] { } , normalizerErrorf ( "normalizer must not be nil" )
2026-07-21 02:36:59 +00:00
}
2026-08-09 16:38:43 +00:00
if n . engine == nil {
2026-11-22 00:27:34 +00:00
return contracts . TypedNormalizeResult [ dnd . NPCRegistry ] { } , normalizerErrorf ( "native engine must not be nil" )
2026-07-26 01:36:32 +00:00
}
2026-07-21 02:36:59 +00:00
if ctx == nil {
2026-08-05 18:25:04 +00:00
return contracts . TypedNormalizeResult [ dnd . NPCRegistry ] { } , normalizerErrorf ( "context must not be nil" )
2026-07-21 02:36:59 +00:00
}
if err := ctx . Err ( ) ; err != nil {
2026-08-05 18:25:04 +00:00
return contracts . TypedNormalizeResult [ dnd . NPCRegistry ] { } , normalizerErrorf ( "context error before normalize: %w" , err )
2026-07-21 02:36:59 +00:00
}
2026-08-09 18:30:32 +00:00
if req . Source == nil {
return contracts . TypedNormalizeResult [ dnd . NPCRegistry ] { } , normalizerErrorf ( "source document must not be nil" )
}
2026-07-26 01:36:32 +00:00
2026-11-22 00:27:34 +00:00
// Keep the already available merged artifact separate from preprocessed
// originals. A failed native response must not recover an earlier proposal.
merged := cloneRegistry ( req . MergeOutput . Value )
2026-07-24 14:20:10 +00:00
order := shared . NewSourceRefOrder ( req . Source )
2026-11-22 00:27:34 +00:00
records , baseFindings := preprocessRecords ( merged , order )
2026-07-26 01:36:32 +00:00
deterministic := recordList ( records )
2026-11-22 03:19:43 +00:00
if err := req . Operation . Execution . RecordEvidence ( "normalization/originals.json" , deterministic ) ; err != nil {
return contracts . TypedNormalizeResult [ dnd . NPCRegistry ] { } , err
}
2026-11-22 00:27:34 +00:00
findings := append ( [ ] diagnostics . Finding ( nil ) , baseFindings ... )
discardedGroups := - 1
var omissionDetails [ ] string
fallback := func ( _ context . Context , final * dnd . NPCRegistry ) ( dnd . NPCRegistry , error ) {
value := cloneRegistry ( merged )
if final != nil {
// Each interpreted candidate was built afresh from fixed original records,
// using only the final proposal's compatible, non-overlapping safe groups.
value = cloneRegistry ( * final )
} else {
findings = nil
discardedGroups = - 1
omissionDetails = nil
}
if err := validateFallback ( value , req . Source ) ; err != nil {
return dnd . NPCRegistry { } , err
}
return value , nil
}
finish := func ( value dnd . NPCRegistry , outcome contracts . ProducerOutcome , err error ) ( contracts . TypedNormalizeResult [ dnd . NPCRegistry ] , error ) {
if err != nil {
return contracts . TypedNormalizeResult [ dnd . NPCRegistry ] { } , normalizerErrorf ( "normalize NPCs: %w" , err )
}
result , err := normalizationResult ( value , findings )
if err != nil {
return contracts . TypedNormalizeResult [ dnd . NPCRegistry ] { } , err
}
result . Outcome = outcome
if outcome . Disposition == contracts . ProducerFallback {
details := append ( [ ] string ( nil ) , omissionDetails ... )
details = append ( details , outcome . FallbackReason )
groups , err := diagnostics . Collect ( [ ] diagnostics . Finding { semanticFallbackFinding ( discardedGroups , details ... ) } , contracts . DiagnosticDispositionWarning , contracts . DiagnosticCategoryFallback )
if err != nil {
return contracts . TypedNormalizeResult [ dnd . NPCRegistry ] { } , normalizerErrorf ( "collect fallback diagnostic: %w" , err )
}
result . Diagnostics = append ( result . Diagnostics , groups ... )
}
return result , nil
}
noModel := func ( ) ( contracts . TypedNormalizeResult [ dnd . NPCRegistry ] , error ) {
value , outcome , err := pipeline . AssessProducer ( ctx , req . Operation , deterministic , func ( ctx context . Context ) ( dnd . NPCRegistry , error ) { return fallback ( ctx , nil ) } )
return finish ( value , outcome , err )
}
2026-08-09 18:30:32 +00:00
if len ( records ) < 2 {
2026-11-22 00:27:34 +00:00
return noModel ( )
2026-07-26 01:36:32 +00:00
}
2026-08-09 16:38:43 +00:00
candidates , envelopes , err := reconciliationInputs ( records )
if err != nil {
return contracts . TypedNormalizeResult [ dnd . NPCRegistry ] { } , normalizerErrorf ( "prepare semantic reconciliation inputs: %w" , err )
}
2026-11-22 00:27:34 +00:00
preparation , err := semanticreconcile . Prepare ( req . Source , candidates , semanticreconcile . DefaultLimits ( ) )
2026-08-09 16:38:43 +00:00
if err != nil {
2026-11-22 00:27:34 +00:00
return contracts . TypedNormalizeResult [ dnd . NPCRegistry ] { } , normalizerErrorf ( "prepare semantic reconciliation: %w" , err )
2026-08-09 16:38:43 +00:00
}
2026-11-22 03:19:43 +00:00
if err := req . Operation . Execution . RecordEvidence ( "normalization/preparation.json" , map [ string ] any {
"disposition" : preparation . Disposition ( ) , "candidate_number_range" : preparation . CandidateNumberRange ( ) ,
"candidate_mappings" : preparation . CandidateMappings ( ) , "candidates" : candidates ,
} ) ; err != nil {
return contracts . TypedNormalizeResult [ dnd . NPCRegistry ] { } , err
}
2026-11-22 00:27:34 +00:00
switch preparation . Disposition ( ) {
case semanticreconcile . InsufficientCandidates :
return noModel ( )
case semanticreconcile . LimitExceeded :
value , outcome , err := pipeline . FinishProducerFailure ( ctx , req . Operation . Controls . Mode , normalizerErrorf ( "candidate set exceeded semantic reconciliation preparation limits" ) , func ( ctx context . Context ) ( dnd . NPCRegistry , error ) { return fallback ( ctx , nil ) } )
return finish ( value , outcome , err )
2026-07-26 01:36:32 +00:00
}
2026-11-22 00:27:34 +00:00
initial , err := semanticreconcile . InitialRequest ( preparation , PromptID , req . LLMProfile , req . SessionID )
2026-08-09 16:38:43 +00:00
if err != nil {
2026-11-22 00:27:34 +00:00
return contracts . TypedNormalizeResult [ dnd . NPCRegistry ] { } , normalizerErrorf ( "build native request: %w" , err )
2026-07-26 01:36:32 +00:00
}
2026-11-22 00:27:34 +00:00
value , outcome , err := pipeline . RunProducer ( ctx , n . engine , req . Operation , initial ,
func ( _ context . Context , result * promptkit . RunResult ) ( * dnd . NPCRegistry , [ ] string , error ) {
response , err := semanticreconcile . DecodeProposal ( result . RawOutput )
if err != nil {
return nil , nil , err
}
assessment := preparation . Assess ( response )
2026-11-22 03:19:43 +00:00
if err := req . Operation . Execution . RecordEvidence ( "normalization/proposals/" + result . RunID + ".json" , map [ string ] any { "proposal" : response , "issues" : assessment . Issues ( ) , "discarded_group_count" : assessment . DiscardedGroupCount ( ) } ) ; err != nil {
return nil , nil , err
}
2026-11-22 00:27:34 +00:00
applied , semanticFindings , err := applyReconciliationPlan ( assessment . Plan ( ) , records , envelopes , order )
if err != nil {
return nil , nil , normalizerErrorf ( "apply semantic reconciliation plan: %w" , err )
}
findings = append ( append ( [ ] diagnostics . Finding ( nil ) , baseFindings ... ) , semanticFindings ... )
discardedGroups = assessment . DiscardedGroupCount ( )
omissionDetails = semanticreconcile . IssueDetails ( assessment . Issues ( ) )
guidance , err := semanticreconcile . CorrectionDetails ( assessment . Issues ( ) , preparation . CandidateNumberRange ( ) )
if err != nil {
return nil , nil , err
}
candidate := recordList ( applied )
return & candidate , guidance , nil
} ,
func ( _ context . Context , guidance [ ] string ) ( promptkit . RunRequest , error ) {
return semanticreconcile . CorrectionRequest ( CorrectionPromptID , req . LLMProfile , req . SessionID , guidance ) , nil
} , fallback )
return finish ( value , outcome , err )
2026-07-26 01:36:32 +00:00
}
2026-11-22 00:27:34 +00:00
// validateFallback checks the durable artifact and the identity/source invariants
// used by registry consumers, not the configured ordinary validator chain.
func validateFallback ( value dnd . NPCRegistry , document * source . SourceDocument ) error {
if _ , err := registrycodec . New ( ) . Encode ( value ) ; err != nil {
return normalizerErrorf ( "construct NPCs fallback: %w" , err )
}
if issues := identity . ValidateList ( value ) ; len ( issues ) > 0 {
return normalizerErrorf ( "construct NPCs fallback: registry identity is not usable downstream" )
}
index := source . NewDocumentIndex ( document )
for _ , npc := range value . NPCs {
for _ , reference := range npc . SourceRefs {
if err := index . ValidateRef ( reference ) ; err != nil {
return normalizerErrorf ( "construct NPCs fallback: %w" , err )
}
}
}
return nil
2026-07-26 01:36:32 +00:00
}
2026-11-22 00:27:34 +00:00
func cloneRegistry ( value dnd . NPCRegistry ) dnd . NPCRegistry {
if value . NPCs == nil {
return dnd . NPCRegistry { }
2026-08-28 18:25:00 +00:00
}
2026-11-22 00:27:34 +00:00
result := dnd . NPCRegistry { NPCs : make ( [ ] dnd . NPC , len ( value . NPCs ) ) }
for i , npc := range value . NPCs {
result . NPCs [ i ] = cloneNPC ( npc )
2026-08-28 18:25:00 +00:00
}
2026-11-22 00:27:34 +00:00
return result
2026-07-26 01:36:32 +00:00
}
2026-11-22 00:27:34 +00:00
func semanticFallbackFinding ( discarded int , details ... string ) diagnostics . Finding {
message := "semantic reconciliation did not obtain ordinary acceptance; using supported fallback output"
if discarded > 0 {
message = fmt . Sprintf ( "%d proposal group(s) omitted from the final semantic proposal; all affected candidates were preserved separately" , discarded )
2026-08-28 18:25:00 +00:00
}
if len ( details ) > 0 {
message = diagnostics . Aggregate ( message , details )
2026-07-26 01:36:32 +00:00
}
2026-08-27 16:40:17 +00:00
return diagnostics . Finding { Scope : "npcs" , ReasonCode : ReasonCodeNPCSemanticReconciliationExhausted , Message : message }
2026-07-26 01:36:32 +00:00
}
2026-11-22 00:27:34 +00:00
func normalizationResult ( value dnd . NPCRegistry , findings [ ] diagnostics . Finding ) ( contracts . TypedNormalizeResult [ dnd . NPCRegistry ] , error ) {
groups , err := diagnostics . Collect ( findings , contracts . DiagnosticDispositionObservation , contracts . DiagnosticCategoryNormalization )
2026-08-27 15:57:29 +00:00
if err != nil {
return contracts . TypedNormalizeResult [ dnd . NPCRegistry ] { } , normalizerErrorf ( "collect normalization diagnostics: %w" , err )
}
2026-11-22 00:27:34 +00:00
return contracts . TypedNormalizeResult [ dnd . NPCRegistry ] { Value : value , Diagnostics : groups } , nil
2026-08-09 16:38:43 +00:00
}
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-08-27 16:40:17 +00:00
func preprocessRecords ( input dnd . NPCRegistry , order shared . SourceRefOrder ) ( [ ] normalizedRecord , [ ] diagnostics . Finding ) {
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 ) )
2026-08-27 18:41:59 +00:00
findings := make ( [ ] diagnostics . Finding , 0 )
2026-07-21 02:36:59 +00:00
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-08-27 18:41:59 +00:00
findings = append ( findings , diagnostics . Finding { 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-08-27 18:41:59 +00:00
findings = append ( findings , diagnostics . Finding { 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-08-27 18:41:59 +00:00
findings = append ( findings , diagnostics . Finding { 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-08-27 18:41:59 +00:00
findings = append ( findings , diagnostics . Finding { 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-08-27 18:41:59 +00:00
findings = append ( findings , duplicateFinding ( retainedIndex , memberInputIndexes ( records , members [ 1 : ] ) ) )
2026-07-21 02:36:59 +00:00
}
}
2026-08-27 18:41:59 +00:00
return output , findings
2026-07-21 02:36:59 +00:00
}
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
}
2026-08-05 18:25:04 +00:00
func recordList ( records [ ] normalizedRecord ) dnd . NPCRegistry {
2026-07-26 01:36:32 +00:00
if records == nil {
2026-08-05 18:25:04 +00:00
return dnd . NPCRegistry { }
2026-07-26 01:36:32 +00:00
}
2026-08-05 18:25:04 +00:00
return dnd . NPCRegistry { 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
}
2026-08-27 18:41:59 +00:00
func duplicateFinding ( retainedIndex int , removed [ ] int ) diagnostics . Finding {
2026-07-21 02:36:59 +00:00
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-08-27 16:40:17 +00:00
return diagnostics . Finding { 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-11-22 00:27:34 +00:00
return pipeline . ModuleSpec { Key : Key , Stage : pipeline . StageNormalize , ExecutionClass : contracts . ExecutionClassLLMBacked , Prompts : [ ] string { PromptID , CorrectionPromptID } , Requires : append ( [ ] string ( nil ) , requiredCapabilities ... ) , Provides : append ( [ ] string ( nil ) , providedCapabilities ... ) , ArtifactKind : dnd . NPCRegistryKind }
2026-07-21 02:36:59 +00:00
}
func Register ( registry * pipeline . NormalizerRegistry ) error {
2026-08-05 18:25:04 +00:00
return pipeline . RegisterNormalizerBuilder ( registry , ModuleSpec ( ) , validateOptions , func ( request pipeline . BuildRequest ) ( contracts . Normalizer [ dnd . NPCRegistry ] , error ) {
2026-07-21 02:36:59 +00:00
options , err := DecodeOptions ( request . Options )
if err != nil {
return nil , err
}
2026-11-22 00:27:34 +00:00
return New ( request . Dependencies . Engine , 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 {
2026-08-05 18:38:22 +00:00
return fmt . Errorf ( "dnd npc registry normalizer: " + format , args ... )
2026-07-21 02:36:59 +00:00
}