Prepare scene extraction and deterministic normalization in core

This commit is contained in:
2026-11-25 15:48:32 +00:00
parent 9d2674481b
commit e3ddc177ab
13 changed files with 135 additions and 101 deletions

View File

@@ -36,7 +36,7 @@ func TestNormalizeAcceptedFixtures(t *testing.T) {
for _, fixture := range fixtures.Cases {
t.Run(fixture.Name, func(t *testing.T) {
result, err := newNormalizer(t).Normalize(context.Background(), normalizeRequestWithSource(fixture.Input, sourceDocument(2)))
result, err := runNormalize(newNormalizer(t), context.Background(), normalizeRequestWithSource(fixture.Input, sourceDocument(2)))
if err != nil {
t.Fatalf("Normalize() error = %v, want nil", err)
}

View File

@@ -63,15 +63,15 @@ func (n *Normalizer) ReferenceSlots() []contracts.ReferenceSlot {
return referenceSlots()
}
func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalizeRequest[dnd.SpellList]) (contracts.TypedNormalizeResult[dnd.SpellList], error) {
func (n *Normalizer) PrepareNormalize(ctx context.Context, req contracts.NormalizeInput[dnd.SpellList]) (contracts.PreparedOperation[dnd.SpellList], error) {
if n == nil {
return contracts.TypedNormalizeResult[dnd.SpellList]{}, normalizerErrorf("normalizer must not be nil")
return nil, normalizerErrorf("normalizer must not be nil")
}
if ctx == nil {
return contracts.TypedNormalizeResult[dnd.SpellList]{}, normalizerErrorf("context must not be nil")
return nil, normalizerErrorf("context must not be nil")
}
if err := ctx.Err(); err != nil {
return contracts.TypedNormalizeResult[dnd.SpellList]{}, normalizerErrorf("context error before normalize: %w", err)
return nil, normalizerErrorf("context error before normalize: %w", err)
}
index := source.NewDocumentIndex(req.Source)
@@ -81,9 +81,9 @@ func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalize
findings = append(findings, duplicateFindings...)
diagnosticGroups, err := diagnostics.NormalizationDiagnostics(findings, ReasonCodeSpellNameUnresolved)
if err != nil {
return contracts.TypedNormalizeResult[dnd.SpellList]{}, normalizerErrorf("collect diagnostics: %w", err)
return nil, normalizerErrorf("collect diagnostics: %w", err)
}
return contracts.TypedNormalizeResult[dnd.SpellList]{Value: value, Diagnostics: diagnosticGroups}, nil
return contracts.DeterministicOperation[dnd.SpellList]{Evaluation: contracts.CandidateEvaluation[dnd.SpellList]{Result: contracts.EvaluationView[dnd.SpellList]{Candidate: &value, Diagnostics: diagnosticGroups}}}, nil
}
func normalizeSpellList(input dnd.SpellList, catalog spellcatalog.EffectiveCatalog, order shared.SourceRefOrder) (dnd.SpellList, []diagnostics.Finding) {

View File

@@ -109,7 +109,7 @@ func TestNormalizeCanonicalizesNamesAndReportsUnresolvedNames(t *testing.T) {
{Spell: "Mystery\nSpell\tName"},
{Spell: "Cure Wounds"},
}}
result, err := normalizer.Normalize(context.Background(), normalizeRequest(input))
result, err := runNormalize(normalizer, context.Background(), normalizeRequest(input))
if err != nil {
t.Fatalf("Normalize() error = %v, want nil", err)
}
@@ -131,7 +131,7 @@ func TestNormalizeLimitsFindingsWithoutChangingSpellValues(t *testing.T) {
for index := range input.SpellCasts {
input.SpellCasts[index].Spell = fmt.Sprintf("Unknown Spell %d", index)
}
result, err := newNormalizer(t).Normalize(context.Background(), normalizeRequest(input))
result, err := runNormalize(newNormalizer(t), context.Background(), normalizeRequest(input))
if err != nil {
t.Fatalf("Normalize() error = %v", err)
}
@@ -150,7 +150,7 @@ func TestNormalizeBoundsUnicodeNamesAndQuotesCanonicalReplacement(t *testing.T)
if err != nil {
t.Fatalf("New() error = %v, want nil", err)
}
result, err := normalizer.Normalize(context.Background(), normalizeRequest(dnd.SpellList{SpellCasts: []dnd.SpellCast{{Spell: "long alias"}}}))
result, err := runNormalize(normalizer, context.Background(), normalizeRequest(dnd.SpellList{SpellCasts: []dnd.SpellCast{{Spell: "long alias"}}}))
if err != nil {
t.Fatalf("Normalize() error = %v, want nil", err)
}
@@ -174,7 +174,7 @@ func TestNormalizeSortsAndDeduplicatesExactSourceReferences(t *testing.T) {
{SourceID: "source-a", StartUnitID: 1, EndUnitID: 4},
}
input := dnd.SpellList{SpellCasts: []dnd.SpellCast{{Spell: "Cure Wounds", SourceRefs: inputRefs}}}
result, err := normalizer.Normalize(context.Background(), normalizeRequest(input))
result, err := runNormalize(normalizer, context.Background(), normalizeRequest(input))
if err != nil {
t.Fatalf("Normalize() error = %v, want nil", err)
}
@@ -202,7 +202,7 @@ func TestNormalizeReportsDuplicateRemovalWithoutOrderChange(t *testing.T) {
SourceRefs: []source.SourceRef{ref1, ref1, ref2},
}}}
result, err := newNormalizer(t).Normalize(context.Background(), normalizeRequest(input))
result, err := runNormalize(newNormalizer(t), context.Background(), normalizeRequest(input))
if err != nil {
t.Fatalf("Normalize() error = %v, want nil", err)
}
@@ -226,7 +226,7 @@ func TestNormalizeOrdersReferencesBySourceDocumentPosition(t *testing.T) {
{SourceID: doc.ID, StartUnitID: 999, EndUnitID: 999},
},
}}}
result, err := newNormalizer(t).Normalize(context.Background(), normalizeRequestWithSource(input, doc))
result, err := runNormalize(newNormalizer(t), context.Background(), normalizeRequestWithSource(input, doc))
if err != nil {
t.Fatalf("Normalize() error = %v, want nil", err)
}
@@ -250,7 +250,7 @@ func TestNormalizePreservesNilEmptyAndAdjacentOrOverlappingReferences(t *testing
before := input
before.SpellCasts = append([]dnd.SpellCast(nil), input.SpellCasts...)
before.SpellCasts[2].SourceRefs = append([]source.SourceRef(nil), input.SpellCasts[2].SourceRefs...)
result, err := normalizer.Normalize(context.Background(), normalizeRequest(input))
result, err := runNormalize(normalizer, context.Background(), normalizeRequest(input))
if err != nil {
t.Fatalf("Normalize() error = %v, want nil", err)
}
@@ -272,16 +272,16 @@ func TestNormalizePreservesNilEmptyAndAdjacentOrOverlappingReferences(t *testing
func TestNormalizeHandlesNilAndCanceledCalls(t *testing.T) {
request := normalizeRequest(dnd.SpellList{})
var normalizer *Normalizer
if _, err := normalizer.Normalize(context.Background(), request); err == nil || !strings.Contains(err.Error(), "normalizer must not be nil") {
if _, err := runNormalize(normalizer, context.Background(), request); err == nil || !strings.Contains(err.Error(), "normalizer must not be nil") {
t.Fatalf("nil receiver error = %v, want nil receiver error", err)
}
normalizer = newNormalizer(t)
if _, err := normalizer.Normalize(nil, request); err == nil || !strings.Contains(err.Error(), "context must not be nil") {
if _, err := runNormalize(normalizer, nil, request); err == nil || !strings.Contains(err.Error(), "context must not be nil") {
t.Fatalf("nil context error = %v, want nil context error", err)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
if _, err := normalizer.Normalize(ctx, request); err == nil || !strings.Contains(err.Error(), "context error before normalize") {
if _, err := runNormalize(normalizer, ctx, request); err == nil || !strings.Contains(err.Error(), "context canceled") {
t.Fatalf("canceled context error = %v, want canceled context error", err)
}
}
@@ -300,7 +300,7 @@ func TestNormalizeCollapsesDuplicateGroupsAfterCanonicalization(t *testing.T) {
{Caster: " kyle ", Spell: "Cure Wounds", SourceRefs: []source.SourceRef{firstEvidence}},
}}
result, err := normalizer.Normalize(context.Background(), normalizeRequestWithSource(input, doc))
result, err := runNormalize(normalizer, context.Background(), normalizeRequestWithSource(input, doc))
if err != nil {
t.Fatalf("Normalize() error = %v, want nil", err)
}
@@ -343,7 +343,7 @@ func TestNormalizeKeepsDistinctAndIneligibleCastsSeparate(t *testing.T) {
base := dnd.SpellCast{Spell: "Cure Wounds", Caster: "Aria", SourceRefs: []source.SourceRef{ref}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := newNormalizer(t).Normalize(context.Background(), normalizeRequestWithSource(dnd.SpellList{SpellCasts: []dnd.SpellCast{base, test.variant}}, doc))
result, err := runNormalize(newNormalizer(t), context.Background(), normalizeRequestWithSource(dnd.SpellList{SpellCasts: []dnd.SpellCast{base, test.variant}}, doc))
if err != nil {
t.Fatalf("Normalize() error = %v, want nil", err)
}
@@ -365,7 +365,7 @@ func TestNormalizeDoesNotCollapseAdjacentOrOverlappingEvidence(t *testing.T) {
{Spell: "Cure Wounds", Caster: "Aria", SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 1, EndUnitID: 3}}},
{Spell: "Cure Wounds", Caster: "aria", SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 2, EndUnitID: 4}}},
}}
result, err := newNormalizer(t).Normalize(context.Background(), normalizeRequestWithSource(input, doc))
result, err := runNormalize(newNormalizer(t), context.Background(), normalizeRequestWithSource(input, doc))
if err != nil {
t.Fatalf("Normalize() error = %v, want nil", err)
}
@@ -384,7 +384,7 @@ func TestNormalizeBoundsDuplicateFindingIndices(t *testing.T) {
for index := range casts {
casts[index] = dnd.SpellCast{Spell: "Cure Wounds", Caster: "Aria", SourceRefs: []source.SourceRef{ref}}
}
result, err := newNormalizer(t).Normalize(context.Background(), normalizeRequestWithSource(dnd.SpellList{SpellCasts: casts}, doc))
result, err := runNormalize(newNormalizer(t), context.Background(), normalizeRequestWithSource(dnd.SpellList{SpellCasts: casts}, doc))
if err != nil {
t.Fatalf("Normalize() error = %v, want nil", err)
}
@@ -406,11 +406,11 @@ func TestNormalizeIsIdempotentForAlreadyNormalizedInput(t *testing.T) {
SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 1, EndUnitID: 2}},
}}}
normalizer := newNormalizer(t)
first, err := normalizer.Normalize(context.Background(), normalizeRequestWithSource(input, doc))
first, err := runNormalize(normalizer, context.Background(), normalizeRequestWithSource(input, doc))
if err != nil {
t.Fatalf("first Normalize() error = %v, want nil", err)
}
second, err := normalizer.Normalize(context.Background(), normalizeRequestWithSource(first.Value, doc))
second, err := runNormalize(normalizer, context.Background(), normalizeRequestWithSource(first.Value, doc))
if err != nil {
t.Fatalf("second Normalize() error = %v, want nil", err)
}
@@ -426,7 +426,7 @@ func TestNormalizeDuplicateOutputDoesNotShareInputSlices(t *testing.T) {
{Spell: "Cure Wounds", Caster: "Aria", SourceRefs: []source.SourceRef{ref}},
{Spell: "Cure Wounds", Caster: "aria", SourceRefs: []source.SourceRef{ref}},
}}
result, err := newNormalizer(t).Normalize(context.Background(), normalizeRequestWithSource(input, doc))
result, err := runNormalize(newNormalizer(t), context.Background(), normalizeRequestWithSource(input, doc))
if err != nil {
t.Fatalf("Normalize() error = %v, want nil", err)
}
@@ -445,8 +445,16 @@ func newNormalizer(t *testing.T) *Normalizer {
return normalizer
}
func normalizeRequest(value dnd.SpellList) contracts.TypedNormalizeRequest[dnd.SpellList] {
return contracts.TypedNormalizeRequest[dnd.SpellList]{
func runNormalize(normalizer *Normalizer, ctx context.Context, req contracts.NormalizeInput[dnd.SpellList]) (pipeline.OperationResult[dnd.SpellList], error) {
return pipeline.ExecuteOperation(ctx, pipeline.ExecutionContext[dnd.SpellList]{Assess: func(context.Context, dnd.SpellList) (contracts.CandidateAssessment, error) {
return contracts.CandidateAssessment{}, nil
}}, func(ctx context.Context) (contracts.PreparedOperation[dnd.SpellList], error) {
return normalizer.PrepareNormalize(ctx, req)
})
}
func normalizeRequest(value dnd.SpellList) contracts.NormalizeInput[dnd.SpellList] {
return contracts.NormalizeInput[dnd.SpellList]{
LaneID: "spells",
MergeOutput: contracts.MergeArtifact[dnd.SpellList]{
LaneID: "spells",
@@ -457,7 +465,7 @@ func normalizeRequest(value dnd.SpellList) contracts.TypedNormalizeRequest[dnd.S
}
}
func normalizeRequestWithSource(value dnd.SpellList, doc *source.SourceDocument) contracts.TypedNormalizeRequest[dnd.SpellList] {
func normalizeRequestWithSource(value dnd.SpellList, doc *source.SourceDocument) contracts.NormalizeInput[dnd.SpellList] {
request := normalizeRequest(value)
request.Source = doc
return request