2026-08-05 19:39:06 +00:00
package itemregistry
import (
"context"
"encoding/json"
2026-08-09 16:46:43 +00:00
"fmt"
2026-08-05 19:39:06 +00:00
"reflect"
2026-08-08 15:05:35 +00:00
"strconv"
2026-08-05 19:39:06 +00:00
"strings"
"testing"
2026-11-22 00:17:58 +00:00
"gitea.maximumdirect.net/eric/notarius/assets"
2026-08-05 19:39:06 +00:00
"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:46:43 +00:00
"gitea.maximumdirect.net/eric/notarius/internal/framework/semanticreconcile"
2026-08-05 19:39:06 +00:00
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/items/identity"
2026-08-06 13:28:12 +00:00
identityvalidator "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/itemregistry/identity"
2026-11-22 00:17:58 +00:00
"go.maximumdirect.net/configkit"
"go.maximumdirect.net/promptkit"
2026-08-05 19:39:06 +00:00
)
2026-11-22 00:17:58 +00:00
func TestModuleContractAndNativeDependencies ( t * testing . T ) {
want := pipeline . ModuleSpec { Key : Key , Stage : pipeline . StageNormalize , ExecutionClass : contracts . ExecutionClassLLMBacked , Prompts : [ ] string { PromptID , CorrectionPromptID } , Requires : [ ] string { "merged" } , Provides : [ ] string { "normalized" } , ArtifactKind : dnd . ItemRegistryKind }
2026-08-05 19:39:06 +00:00
if got := ModuleSpec ( ) ; ! reflect . DeepEqual ( got , want ) {
t . Fatalf ( "ModuleSpec() = %#v, want %#v" , got , want )
}
registry := pipeline . NewNormalizerRegistry ( )
if err := Register ( registry ) ; err != nil {
t . Fatal ( err )
}
if _ , err := New ( nil , Options { } ) ; err == nil {
2026-11-22 00:17:58 +00:00
t . Fatal ( "New() accepted nil native engine" )
2026-08-05 19:39:06 +00:00
}
2026-11-22 00:17:58 +00:00
got := ModuleSpec ( )
got . Prompts [ 0 ] = "caller mutation"
if ! reflect . DeepEqual ( ModuleSpec ( ) , want ) {
t . Fatal ( "static dependency declaration is not owned" )
2026-08-09 16:46:43 +00:00
}
2026-08-05 19:39:06 +00:00
}
2026-08-09 18:30:32 +00:00
func TestNormalizeRejectsNilSourceDocument ( t * testing . T ) {
_ , err := newNormalizer ( t , & recordingNormalizerClient { } ) . Normalize (
context . Background ( ) ,
contracts . TypedNormalizeRequest [ dnd . ItemRegistry ] { } ,
)
if err == nil || ! strings . Contains ( err . Error ( ) , "source document must not be nil" ) {
t . Fatalf ( "Normalize() error = %v, want nil source rejection" , err )
}
}
2026-08-06 13:28:12 +00:00
func TestNormalizeConsolidatesEqualNamesAcrossEvidenceWithoutMutation ( t * testing . T ) {
doc := & source . SourceDocument { ID : "session" , Units : [ ] source . SourceUnit {
{ ID : 1 , Text : "The rope is secured." } ,
{ ID : 2 , Text : "The party takes the rope." } ,
{ ID : 3 , Text : "The rope is packed away." } ,
{ ID : 4 , Text : "A lantern lights the path." } ,
} }
2026-08-05 19:39:06 +00:00
input := dnd . ItemRegistry { Items : [ ] dnd . Item {
2026-08-06 13:28:12 +00:00
{ Name : " Rope " , SourceRefs : [ ] source . SourceRef { { SourceID : "session" , StartUnitID : 3 , EndUnitID : 3 } , { SourceID : "session" , StartUnitID : 1 , EndUnitID : 1 } } } ,
{ Name : "rope" , SourceRefs : [ ] source . SourceRef { { SourceID : "session" , StartUnitID : 2 , EndUnitID : 2 } , { SourceID : "session" , StartUnitID : 3 , EndUnitID : 3 } } } ,
{ Name : "Lantern" , SourceRefs : [ ] source . SourceRef { { SourceID : "session" , StartUnitID : 4 , EndUnitID : 4 } } } ,
} }
before := dnd . ItemRegistry { Items : [ ] dnd . Item {
{ Name : " Rope " , SourceRefs : [ ] source . SourceRef { { SourceID : "session" , StartUnitID : 3 , EndUnitID : 3 } , { SourceID : "session" , StartUnitID : 1 , EndUnitID : 1 } } } ,
{ Name : "rope" , SourceRefs : [ ] source . SourceRef { { SourceID : "session" , StartUnitID : 2 , EndUnitID : 2 } , { SourceID : "session" , StartUnitID : 3 , EndUnitID : 3 } } } ,
{ Name : "Lantern" , SourceRefs : [ ] source . SourceRef { { SourceID : "session" , StartUnitID : 4 , EndUnitID : 4 } } } ,
2026-08-05 19:39:06 +00:00
} }
2026-08-06 13:28:12 +00:00
client := & recordingNormalizerClient { }
result , err := newNormalizer ( t , client ) . Normalize ( context . Background ( ) , normalizeRequestWithSource ( input , doc ) )
if err != nil || len ( result . Value . Items ) != 2 || ! reflect . DeepEqual ( input , before ) || len ( client . requests ) != 1 {
t . Fatalf ( "Normalize() = %#v, %v; want deterministic non-mutating consolidation" , result , err )
}
rope := result . Value . Items [ 0 ]
wantRefs := [ ] source . SourceRef { { SourceID : "session" , StartUnitID : 1 , EndUnitID : 1 } , { SourceID : "session" , StartUnitID : 2 , EndUnitID : 2 } , { SourceID : "session" , StartUnitID : 3 , EndUnitID : 3 } }
2026-08-27 15:57:29 +00:00
if rope . Name != "Rope" || rope . ID != identity . DeriveID ( "Rope" ) || ! reflect . DeepEqual ( rope . SourceRefs , wantRefs ) || result . Value . Items [ 1 ] . Name != "Lantern" || ! hasDiagnostic ( result . Diagnostics , ReasonCodeDuplicateItemCollapsed , contracts . DiagnosticDispositionObservation ) {
t . Fatalf ( "items = %#v, diagnostics = %#v; want earliest display name, canonical evidence union, and stable placement" , result . Value . Items , result . Diagnostics )
2026-08-06 13:28:12 +00:00
}
validation , validationErr := identityvalidator . New ( identityvalidator . Options { } ) . Validate ( context . Background ( ) , contracts . TypedValidationRequest [ dnd . ItemRegistry ] { Value : result . Value } )
if validationErr != nil || ! validation . Approved {
t . Fatalf ( "default item-registry identity validation = %#v, %v; want normalized cross-chunk candidates accepted" , validation , validationErr )
}
var candidates struct {
Candidates [ ] struct {
2026-08-09 16:46:43 +00:00
Label string ` json:"label" `
2026-08-06 13:28:12 +00:00
} ` json:"candidates" `
}
2026-11-22 00:17:58 +00:00
if err := json . Unmarshal ( renderedInput ( t , client . requests [ 0 ] , "candidates" ) , & candidates ) ; err != nil || len ( candidates . Candidates ) != 2 || candidates . Candidates [ 0 ] . Label != "Rope" || candidates . Candidates [ 1 ] . Label != "Lantern" {
2026-08-06 13:28:12 +00:00
t . Fatalf ( "semantic candidates = %#v, %v; want one candidate per comparison name" , candidates , err )
2026-08-05 19:39:06 +00:00
}
2026-08-06 13:28:12 +00:00
repeated , repeatErr := newNormalizer ( t , & recordingNormalizerClient { } ) . Normalize ( context . Background ( ) , normalizeRequestWithSource ( result . Value , doc ) )
if repeatErr != nil || ! reflect . DeepEqual ( repeated . Value , result . Value ) {
t . Fatalf ( "repeated normalization = %#v, %v; want stable output" , repeated , repeatErr )
2026-08-05 19:39:06 +00:00
}
}
2026-08-09 02:26:49 +00:00
func BenchmarkComparisonNameGroupsManyDistinct ( b * testing . B ) {
records := make ( [ ] normalizedRecord , 1_000 )
for index := range records {
records [ index ] = normalizedRecord { item : dnd . Item { Name : "Item " + strconv . Itoa ( index ) } }
}
b . ReportAllocs ( )
b . ResetTimer ( )
for iteration := 0 ; iteration < b . N ; iteration ++ {
if groups := comparisonNameGroups ( records ) ; len ( groups ) != len ( records ) {
b . Fatalf ( "group count = %d, want %d" , len ( groups ) , len ( records ) )
}
}
}
2026-08-06 13:31:15 +00:00
func TestNormalizeAppliesSafeAliasProposal ( t * testing . T ) {
2026-08-05 19:39:06 +00:00
doc := semanticDocument ( )
input := dnd . ItemRegistry { Items : [ ] dnd . Item {
{ Name : "Star Compass" , SourceRefs : [ ] source . SourceRef { { SourceID : doc . ID , StartUnitID : 10 , EndUnitID : 10 } } } ,
{ Name : "Compass of the Stars" , SourceRefs : [ ] source . SourceRef { { SourceID : doc . ID , StartUnitID : 20 , EndUnitID : 20 } } } ,
{ Name : "Gold Pieces" , SourceRefs : [ ] source . SourceRef { { SourceID : doc . ID , StartUnitID : 30 , EndUnitID : 30 } } } ,
} }
2026-08-29 03:16:24 +00:00
client := & recordingNormalizerClient { response : ` { "duplicate_groups":[ { "candidate_numbers":[1,2],"canonical_candidate_number":2}]} ` }
2026-08-05 19:39:06 +00:00
result , err := newNormalizer ( t , client ) . Normalize ( context . Background ( ) , normalizeRequestWithSource ( input , doc ) )
2026-11-22 00:17:58 +00:00
if err != nil || result . Outcome . Disposition != contracts . ProducerAccepted || len ( result . Value . Items ) != 2 {
2026-08-05 19:39:06 +00:00
t . Fatalf ( "Normalize() = %#v, %v" , result , err )
}
merged := result . Value . Items [ 0 ]
2026-08-27 15:57:29 +00:00
if merged . Name != "Compass of the Stars" || merged . ID != identity . DeriveID ( merged . Name ) || len ( merged . SourceRefs ) != 2 || ! hasDiagnostic ( result . Diagnostics , ReasonCodeDuplicateItemCollapsed , contracts . DiagnosticDispositionObservation ) {
t . Fatalf ( "merged item = %#v, diagnostics = %#v" , merged , result . Diagnostics )
2026-08-05 19:39:06 +00:00
}
2026-11-22 00:17:58 +00:00
encoded := string ( renderedInput ( t , client . requests [ 0 ] , "candidates" ) ) + string ( renderedInput ( t , client . requests [ 0 ] , "transcript" ) )
2026-08-08 15:05:35 +00:00
if strings . Contains ( encoded , doc . ID ) || strings . Contains ( encoded , "candidate-" ) || strings . Contains ( encoded , merged . ID ) || ! strings . Contains ( encoded , ` "source_refs" ` ) {
2026-08-05 19:39:06 +00:00
t . Fatalf ( "private inputs = %s" , encoded )
}
2026-08-06 13:31:15 +00:00
}
func TestNormalizeAppliesCurrencyReconciliationSafely ( t * testing . T ) {
doc := semanticDocument ( )
ref := func ( unitID int ) [ ] source . SourceRef {
return [ ] source . SourceRef { { SourceID : doc . ID , StartUnitID : unitID , EndUnitID : unitID } }
}
for _ , test := range [ ] struct {
name string
items [ ] dnd . Item
response string
wantNames [ ] string
wantRefCounts [ ] int
2026-11-22 00:17:58 +00:00
wantFallback bool
2026-08-27 15:57:29 +00:00
reasonCode string
disposition contracts . DiagnosticDisposition
2026-08-06 13:31:15 +00:00
} {
{
name : "same denomination aliases" ,
items : [ ] dnd . Item {
{ Name : "GP" , SourceRefs : ref ( 10 ) } ,
{ Name : "Gold Piece" , SourceRefs : ref ( 20 ) } ,
{ Name : "Gold Pieces" , SourceRefs : ref ( 30 ) } ,
} ,
2026-08-29 03:16:24 +00:00
response : ` { "duplicate_groups":[ { "candidate_numbers":[1,2,3],"canonical_candidate_number":2}]} ` ,
2026-08-06 13:31:15 +00:00
wantNames : [ ] string { "Gold Piece" } ,
wantRefCounts : [ ] int { 3 } ,
2026-08-27 15:57:29 +00:00
reasonCode : ReasonCodeDuplicateItemCollapsed ,
disposition : contracts . DiagnosticDispositionObservation ,
2026-08-06 13:31:15 +00:00
} ,
{
name : "different denominations" ,
items : [ ] dnd . Item {
{ Name : "Gold Pieces" , SourceRefs : ref ( 10 ) } ,
{ Name : "Silver Pieces" , SourceRefs : ref ( 20 ) } ,
} ,
2026-08-29 03:16:24 +00:00
response : ` { "duplicate_groups":[ { "candidate_numbers":[1,2],"canonical_candidate_number":1}]} ` ,
2026-08-06 13:31:15 +00:00
wantNames : [ ] string { "Gold Pieces" , "Silver Pieces" } ,
wantRefCounts : [ ] int { 1 , 1 } ,
2026-11-22 00:17:58 +00:00
wantFallback : true ,
2026-08-27 15:57:29 +00:00
reasonCode : ReasonCodeItemSemanticProposalInvalid ,
disposition : contracts . DiagnosticDispositionAdvisory ,
2026-08-06 13:31:15 +00:00
} ,
{
name : "currency plus ordinary item" ,
items : [ ] dnd . Item {
{ Name : "Gold Pieces" , SourceRefs : ref ( 10 ) } ,
{ Name : "Longsword" , SourceRefs : ref ( 20 ) } ,
} ,
2026-08-29 03:16:24 +00:00
response : ` { "duplicate_groups":[ { "candidate_numbers":[1,2],"canonical_candidate_number":1}]} ` ,
2026-08-06 13:31:15 +00:00
wantNames : [ ] string { "Gold Pieces" , "Longsword" } ,
wantRefCounts : [ ] int { 1 , 1 } ,
2026-11-22 00:17:58 +00:00
wantFallback : true ,
2026-08-27 15:57:29 +00:00
reasonCode : ReasonCodeItemSemanticProposalInvalid ,
disposition : contracts . DiagnosticDispositionAdvisory ,
2026-08-06 13:31:15 +00:00
} ,
{
name : "ordinary items" ,
items : [ ] dnd . Item {
{ Name : "Star Compass" , SourceRefs : ref ( 10 ) } ,
{ Name : "Compass of the Stars" , SourceRefs : ref ( 20 ) } ,
} ,
2026-08-29 03:16:24 +00:00
response : ` { "duplicate_groups":[ { "candidate_numbers":[1,2],"canonical_candidate_number":2}]} ` ,
2026-08-06 13:31:15 +00:00
wantNames : [ ] string { "Compass of the Stars" } ,
wantRefCounts : [ ] int { 2 } ,
2026-08-27 15:57:29 +00:00
reasonCode : ReasonCodeDuplicateItemCollapsed ,
disposition : contracts . DiagnosticDispositionObservation ,
2026-08-06 13:31:15 +00:00
} ,
{
name : "currency plus ordinary canonical item" ,
items : [ ] dnd . Item {
{ Name : "Gold Pieces" , SourceRefs : ref ( 10 ) } ,
{ Name : "Longsword" , SourceRefs : ref ( 20 ) } ,
} ,
2026-08-29 03:16:24 +00:00
response : ` { "duplicate_groups":[ { "candidate_numbers":[1,2],"canonical_candidate_number":2}]} ` ,
2026-08-06 13:31:15 +00:00
wantNames : [ ] string { "Gold Pieces" , "Longsword" } ,
wantRefCounts : [ ] int { 1 , 1 } ,
2026-11-22 00:17:58 +00:00
wantFallback : true ,
2026-08-27 15:57:29 +00:00
reasonCode : ReasonCodeItemSemanticProposalInvalid ,
disposition : contracts . DiagnosticDispositionAdvisory ,
2026-08-06 13:31:15 +00:00
} ,
} {
t . Run ( test . name , func ( t * testing . T ) {
input := dnd . ItemRegistry { Items : make ( [ ] dnd . Item , len ( test . items ) ) }
for index , item := range test . items {
input . Items [ index ] = cloneItem ( item )
}
before := dnd . ItemRegistry { Items : make ( [ ] dnd . Item , len ( input . Items ) ) }
for index , item := range input . Items {
before . Items [ index ] = cloneItem ( item )
}
2026-11-22 00:17:58 +00:00
result , err := newNormalizer ( t , & recordingNormalizerClient { response : test . response } ) . Normalize ( context . Background ( ) , permissiveRequestWithSource ( input , doc ) )
if err != nil || ( result . Outcome . Disposition == contracts . ProducerFallback ) != test . wantFallback || ! reflect . DeepEqual ( input , before ) || ! hasDiagnostic ( result . Diagnostics , test . reasonCode , test . disposition ) {
2026-08-06 13:31:15 +00:00
t . Fatalf ( "Normalize() = %#v, %v" , result , err )
}
if len ( result . Value . Items ) != len ( test . wantNames ) {
t . Fatalf ( "items = %#v, want names %#v" , result . Value . Items , test . wantNames )
}
for index , item := range result . Value . Items {
if item . Name != test . wantNames [ index ] || len ( item . SourceRefs ) != test . wantRefCounts [ index ] {
t . Fatalf ( "item %d = %#v, want name %q with %d source refs" , index , item , test . wantNames [ index ] , test . wantRefCounts [ index ] )
}
}
2026-11-22 00:17:58 +00:00
if test . wantFallback && ( result . Outcome . ReuseEligible || ! hasDiagnostic ( result . Diagnostics , ReasonCodeItemSemanticReconciliationExhausted , contracts . DiagnosticDispositionWarning ) ) {
t . Fatalf ( "fallback lacks qualification: %#v" , result )
2026-08-06 13:31:15 +00:00
}
} )
2026-08-05 19:39:06 +00:00
}
}
func TestNormalizePreservesCandidatesForUnsafeProposalGroups ( t * testing . T ) {
doc := semanticDocument ( )
input := dnd . ItemRegistry { Items : [ ] dnd . Item {
{ Name : "Star Compass" , SourceRefs : [ ] source . SourceRef { { SourceID : doc . ID , StartUnitID : 10 , EndUnitID : 10 } } } ,
{ Name : "Compass" , SourceRefs : [ ] source . SourceRef { { SourceID : doc . ID , StartUnitID : 20 , EndUnitID : 20 } } } ,
{ Name : "Rope" , SourceRefs : [ ] source . SourceRef { { SourceID : doc . ID , StartUnitID : 30 , EndUnitID : 30 } } } ,
} }
2026-08-29 03:16:24 +00:00
client := & recordingNormalizerClient { response : ` { "duplicate_groups":[ { "candidate_numbers":[1,2],"canonical_candidate_number":1}, { "candidate_numbers":[2,3],"canonical_candidate_number":3}, { "candidate_numbers":[1,3],"canonical_candidate_number":99}]} ` }
2026-11-22 00:17:58 +00:00
result , err := newNormalizer ( t , client ) . Normalize ( context . Background ( ) , permissiveRequestWithSource ( input , doc ) )
if err != nil || result . Outcome . Disposition != contracts . ProducerFallback || result . Outcome . ReuseEligible || len ( result . Value . Items ) != 3 || ! hasDiagnostic ( result . Diagnostics , ReasonCodeItemSemanticReconciliationExhausted , contracts . DiagnosticDispositionWarning ) {
t . Fatalf ( "Normalize() = %#v, %v; want qualified safe fallback" , result , err )
2026-08-05 19:39:06 +00:00
}
}
2026-08-09 16:46:43 +00:00
func TestNormalizeAppliesIndependentGroupAndCountsAllOmissions ( t * testing . T ) {
doc := semanticDocument ( )
ref := func ( unitID int ) [ ] source . SourceRef {
return [ ] source . SourceRef { { SourceID : doc . ID , StartUnitID : unitID , EndUnitID : unitID } }
}
input := dnd . ItemRegistry { Items : [ ] dnd . Item {
{ Name : "Star Compass" , SourceRefs : ref ( 10 ) } ,
{ Name : "Compass of the Stars" , SourceRefs : ref ( 20 ) } ,
{ Name : "Gold Pieces" , SourceRefs : ref ( 30 ) } ,
{ Name : "Silver Pieces" , SourceRefs : ref ( 30 ) } ,
{ Name : "Rope" , SourceRefs : ref ( 10 ) } ,
} }
2026-08-29 03:16:24 +00:00
client := & recordingNormalizerClient { response : ` { "duplicate_groups":[ { "candidate_numbers":[1,2],"canonical_candidate_number":2}, { "candidate_numbers":[3,4],"canonical_candidate_number":3}, { "candidate_numbers":[5,99],"canonical_candidate_number":5}]} ` }
2026-11-22 00:17:58 +00:00
request := permissiveRequestWithSource ( input , doc )
request . Operation . Controls . MaxAdditionalTurns = 1
result , err := newNormalizer ( t , client ) . Normalize ( context . Background ( ) , request )
if err != nil || result . Outcome . Disposition != contracts . ProducerFallback {
t . Fatalf ( "Normalize() = %#v, %v; want qualified safe partial output" , result , err )
2026-08-09 16:46:43 +00:00
}
wantNames := [ ] string { "Compass of the Stars" , "Gold Pieces" , "Silver Pieces" , "Rope" }
if len ( result . Value . Items ) != len ( wantNames ) {
t . Fatalf ( "items = %#v, want %v" , result . Value . Items , wantNames )
}
for index , name := range wantNames {
if result . Value . Items [ index ] . Name != name {
t . Fatalf ( "item %d = %#v, want %q" , index , result . Value . Items [ index ] , name )
}
}
2026-08-27 15:57:29 +00:00
if ! hasDiagnostic ( result . Diagnostics , ReasonCodeDuplicateItemCollapsed , contracts . DiagnosticDispositionObservation ) || ! hasDiagnostic ( result . Diagnostics , ReasonCodeItemSemanticProposalInvalid , contracts . DiagnosticDispositionAdvisory ) {
t . Fatalf ( "diagnostics = %#v, want accepted and guarded-group diagnostics" , result . Diagnostics )
2026-08-09 16:46:43 +00:00
}
2026-11-22 00:17:58 +00:00
if len ( client . requests ) != 2 {
t . Fatalf ( "native calls = %d, want initial and correction" , len ( client . requests ) )
}
messages := client . requests [ 1 ] . Prompt . Messages
guidance := messages [ len ( messages ) - 1 ] . Content
for _ , expected := range [ ] string { "aliases of the same denomination" , "do not combine currency with non-currency items" , "candidate-number range 1 through 5" , "source positions" } {
if ! strings . Contains ( guidance , expected ) {
t . Fatalf ( "missing domain/shared guidance %q" , expected )
}
}
if strings . Contains ( guidance , "member_unknown" ) || strings . Contains ( guidance , ReasonCodeItemSemanticProposalInvalid ) {
t . Fatal ( "internal diagnostics copied into correction" )
}
var warning string
for _ , group := range result . Diagnostics {
if group . ReasonCode == ReasonCodeItemSemanticReconciliationExhausted && len ( group . Samples ) > 0 {
warning = group . Samples [ 0 ] . Message
}
2026-08-28 18:25:00 +00:00
}
2026-11-22 00:17:58 +00:00
if ! strings . Contains ( warning , "2 proposal group(s)" ) || ! strings . Contains ( warning , "preserved separately" ) || ! strings . Contains ( warning , "same denomination" ) {
t . Fatalf ( "fallback warning = %q" , warning )
2026-08-09 16:46:43 +00:00
}
}
2026-11-22 00:17:58 +00:00
func TestPreparationLimitsFollowResolvedModeWithoutModelOrAssessment ( t * testing . T ) {
2026-08-09 16:46:43 +00:00
doc := & source . SourceDocument { ID : "session" , Units : [ ] source . SourceUnit { { ID : 1 , Kind : "narration" , Text : "A crowded storeroom" } } }
limit := semanticreconcile . DefaultLimits ( ) . MaximumCandidates
input := dnd . ItemRegistry { Items : make ( [ ] dnd . Item , limit + 1 ) }
2026-11-22 00:17:58 +00:00
for i := range input . Items {
name := fmt . Sprintf ( "Item %d" , i )
input . Items [ i ] = dnd . Item { ID : identity . DeriveID ( name ) , Name : name , SourceRefs : [ ] source . SourceRef { { SourceID : doc . ID , StartUnitID : 1 , EndUnitID : 1 } } }
}
for _ , mode := range [ ] contracts . ExecutionMode { contracts . ModeStrict , contracts . ModePermissive } {
t . Run ( string ( mode ) , func ( t * testing . T ) {
client := & recordingNormalizerClient { }
request := normalizeRequestWithSource ( input , doc )
request . Operation . Controls . Mode = mode
assessed := 0
request . Operation . Assess = func ( context . Context , dnd . ItemRegistry ) ( contracts . CandidateAssessment , error ) {
assessed ++
return contracts . CandidateAssessment { } , nil
}
result , err := newNormalizer ( t , client ) . Normalize ( context . Background ( ) , request )
if len ( client . requests ) != 0 || assessed != 0 {
t . Fatal ( "unready preparation caused model execution or ordinary validation" )
}
if mode == contracts . ModeStrict {
if err == nil {
t . Fatal ( "strict preparation-limit failure succeeded" )
}
} else if err != nil || result . Outcome . Disposition != contracts . ProducerFallback || result . Outcome . ReuseEligible || ! reflect . DeepEqual ( result . Value , input ) {
t . Fatalf ( "permissive preparation limit: %#v %v" , result , err )
}
} )
2026-08-09 16:46:43 +00:00
}
}
2026-11-22 00:17:58 +00:00
func TestNormalizationRemainsIdempotent ( t * testing . T ) {
2026-08-05 19:39:06 +00:00
doc := semanticDocument ( )
input := dnd . ItemRegistry { Items : [ ] dnd . Item { { Name : "Star Compass" , SourceRefs : [ ] source . SourceRef { { SourceID : doc . ID , StartUnitID : 10 , EndUnitID : 10 } } } , { Name : "Compass" , SourceRefs : [ ] source . SourceRef { { SourceID : doc . ID , StartUnitID : 20 , EndUnitID : 20 } } } } }
first , err := newNormalizer ( t , & recordingNormalizerClient { } ) . Normalize ( context . Background ( ) , normalizeRequestWithSource ( input , doc ) )
if err != nil {
t . Fatal ( err )
}
2026-11-22 00:17:58 +00:00
second , err := newNormalizer ( t , & recordingNormalizerClient { } ) . Normalize ( context . Background ( ) , normalizeRequestWithSource ( first . Value , doc ) )
if err != nil || ! reflect . DeepEqual ( first . Value , second . Value ) {
t . Fatalf ( "repeated normalization changed output: %#v %v" , second , err )
2026-08-05 19:39:06 +00:00
}
2026-11-22 00:17:58 +00:00
}
func TestNativeNormalizationPromptPairPreparesWithSameContract ( t * testing . T ) {
client := & recordingNormalizerClient { }
normalizer := newNormalizer ( t , client )
var initial promptkit . PreparedRun
for _ , id := range [ ] string { PromptID , CorrectionPromptID } {
inputs := map [ string ] promptkit . ArtifactRef { "guidance" : promptkit . Inline ( "Keep currency denominations separate." ) }
if id == PromptID {
inputs = map [ string ] promptkit . ArtifactRef { "candidates" : promptkit . Inline ( ` { "candidate_number_range": { "first":1,"last":2},"candidates":[]} ` ) , "transcript" : promptkit . Inline ( ` { "windows":[]} ` ) }
}
handle , err := normalizer . engine . PrepareExecution ( context . Background ( ) , promptkit . RunRequest { PromptID : id , APIKey : "test-only" , Inputs : inputs } )
if err != nil {
t . Fatal ( err )
}
prepared := handle . Details ( )
handle . Discard ( )
if prepared . SelectedProfileID != "notarius/dnd-extraction" || prepared . StructuredOutput == nil {
t . Fatal ( "missing native defaults/schema" )
}
if id == PromptID {
initial = prepared
var system string
for _ , message := range prepared . Messages {
if message . Role == "system" {
system += message . Content
}
}
for _ , rule := range [ ] string { "candidate_number_range" , "start_unit_id" , "currency denominations" , "materially different item" , "canonical_candidate_number" } {
if ! strings . Contains ( system , rule ) {
t . Fatalf ( "missing enduring rule %q" , rule )
}
}
} else if ! reflect . DeepEqual ( initial . OutputContract , prepared . OutputContract ) || len ( prepared . Messages ) != 1 || prepared . Messages [ 0 ] . Role != "user" || ! strings . Contains ( prepared . Messages [ 0 ] . Content , "Keep currency denominations separate." ) {
t . Fatal ( "correction did not preserve the output contract or supply only findings/assignment" )
}
}
if len ( client . requests ) != 0 {
t . Fatal ( "offline preparation invoked generation" )
2026-08-09 16:46:43 +00:00
}
2026-08-05 19:39:06 +00:00
}
type recordingNormalizerClient struct {
2026-11-22 00:17:58 +00:00
response string
responses [ ] string
err error
errors [ ] error
requests [ ] promptkit . GenerateRequest
onGenerate func ( context . Context , promptkit . GenerateRequest )
2026-08-05 19:39:06 +00:00
}
2026-11-22 00:17:58 +00:00
func ( c * recordingNormalizerClient ) Generate ( ctx context . Context , request promptkit . GenerateRequest ) ( * promptkit . GenerateResponse , error ) {
index := len ( c . requests )
c . requests = append ( c . requests , request )
if c . onGenerate != nil {
c . onGenerate ( ctx , request )
2026-08-26 23:24:20 +00:00
}
2026-08-05 19:39:06 +00:00
if c . err != nil {
2026-11-22 00:17:58 +00:00
return nil , c . err
}
if index < len ( c . errors ) && c . errors [ index ] != nil {
return nil , c . errors [ index ]
2026-08-05 19:39:06 +00:00
}
response := c . response
2026-11-22 00:17:58 +00:00
if index < len ( c . responses ) {
response = c . responses [ index ]
}
2026-08-05 19:39:06 +00:00
if response == "" {
response = ` { "duplicate_groups":[]} `
}
2026-11-22 00:17:58 +00:00
return & promptkit . GenerateResponse { Content : response } , nil
2026-08-08 15:05:35 +00:00
}
2026-11-22 00:17:58 +00:00
func newNormalizer ( t * testing . T , client promptkit . LLMClient ) * Normalizer {
2026-08-05 19:39:06 +00:00
t . Helper ( )
2026-11-22 00:17:58 +00:00
type settings struct {
PromptKit promptkit . Settings ` yaml:"promptkit" `
}
definition := promptkit . Configure ( configkit . Definition [ settings ] { } , func ( value * settings ) * promptkit . Settings { return & value . PromptKit } )
filesystem := configkit . NewFilesystem ( "Notarius assets" , assets . FS ( ) )
result , err := configkit . LoadLayered ( configkit . Input { Source : configkit . Bytes ( "normalizer fixture" , [ ] byte ( ` promptkit :
profiles :
- id : item - normalize - test
endpoint : https : //example.invalid/v1
model : normalization - test - model
api_key_required : false
` ) ) } , configkit . LayeredOptions [ settings ] { Definition : definition , Underlays : [ ] configkit . Underlay {
promptkit . BundledOpenRouterUnderlay ( ) ,
{ Inputs : [ ] configkit . Input { { Source : filesystem . File ( "notarius.yml" ) , Imports : true , Filesystem : & configkit . FilesystemContext { FS : & filesystem , Root : "." } } } } ,
} } )
if err != nil {
t . Fatal ( err )
}
engine , err := promptkit . NewFromConfig ( result , promptkit . RuntimeConfig { } , promptkit . WithLLMClient ( client ) )
if err != nil {
t . Fatal ( err )
}
normalizer , err := New ( engine , Options { } )
2026-08-05 19:39:06 +00:00
if err != nil {
2026-11-22 00:17:58 +00:00
t . Fatal ( err )
2026-08-05 19:39:06 +00:00
}
return normalizer
}
2026-11-22 00:17:58 +00:00
func renderedInput ( t * testing . T , request promptkit . GenerateRequest , name string ) [ ] byte {
t . Helper ( )
key := "candidate_number_range"
if name == "transcript" {
key = "windows"
}
for _ , message := range request . Prompt . Messages {
start := strings . Index ( message . Content , "{" )
if start < 0 {
continue
}
content := [ ] byte ( message . Content [ start : ] )
var fields map [ string ] json . RawMessage
if json . Unmarshal ( content , & fields ) == nil && fields [ key ] != nil {
return content
}
}
t . Fatalf ( "missing rendered %s input" , name )
return nil
}
2026-08-05 19:39:06 +00:00
func normalizeRequest ( value dnd . ItemRegistry ) contracts . TypedNormalizeRequest [ dnd . ItemRegistry ] {
2026-08-09 18:30:32 +00:00
return contracts . TypedNormalizeRequest [ dnd . ItemRegistry ] {
2026-11-22 00:17:58 +00:00
Source : & source . SourceDocument { } ,
LLMProfile : "item-normalize-test" ,
Operation : contracts . ProducerOperation [ dnd . ItemRegistry ] { Controls : contracts . ProducerControls { Mode : contracts . ModeStrict , Cache : contracts . CacheBypass } , ReuseEligible : true , Assess : func ( ctx context . Context , value dnd . ItemRegistry ) ( contracts . CandidateAssessment , error ) {
result , err := identityvalidator . New ( identityvalidator . Options { } ) . Validate ( ctx , contracts . TypedValidationRequest [ dnd . ItemRegistry ] { Value : value , Stage : string ( pipeline . StageNormalize ) } )
if err != nil {
return contracts . CandidateAssessment { } , err
}
decision := contracts . AssessmentApproved
if ! result . Approved {
decision = contracts . AssessmentRejected
}
return contracts . CandidateAssessment { Validators : [ ] contracts . ValidatorAssessment { { ValidatorName : identityvalidator . Key , Decision : decision , Result : result } } } , nil
} } ,
2026-08-09 18:30:32 +00:00
MergeOutput : contracts . MergeArtifact [ dnd . ItemRegistry ] { Value : value } ,
}
2026-08-05 19:39:06 +00:00
}
func normalizeRequestWithSource ( value dnd . ItemRegistry , doc * source . SourceDocument ) contracts . TypedNormalizeRequest [ dnd . ItemRegistry ] {
request := normalizeRequest ( value )
request . Source = doc
return request
}
func semanticDocument ( ) * source . SourceDocument {
return & source . SourceDocument { ID : "item-session" , Units : [ ] source . SourceUnit { { ID : 10 , Text : "The Star Compass points north." } , { ID : 20 , Text : "The compass of the stars glows." } , { ID : 30 , Text : "The chest holds gold pieces." } } }
}
2026-08-27 15:57:29 +00:00
func hasDiagnostic ( diagnostics [ ] contracts . ProducerDiagnostic , reason string , disposition contracts . DiagnosticDisposition ) bool {
for _ , diagnostic := range diagnostics {
if diagnostic . ReasonCode == reason && diagnostic . Disposition == disposition {
2026-08-05 19:39:06 +00:00
return true
}
}
return false
}
2026-08-09 16:46:43 +00:00
2026-11-22 00:17:58 +00:00
func permissiveRequestWithSource ( value dnd . ItemRegistry , doc * source . SourceDocument ) contracts . TypedNormalizeRequest [ dnd . ItemRegistry ] {
request := normalizeRequestWithSource ( value , doc )
request . Operation . Controls . Mode = contracts . ModePermissive
return request
2026-08-09 16:46:43 +00:00
}