Reuse duplicate grouping for combat turns and spells
This commit is contained in:
@@ -116,9 +116,11 @@ and advisory responsibilities; [Configuration](../config.md#production-validator
|
||||
owns their order and replacement rules. Extraction containment requires the
|
||||
entire cited span inside the current chunk after ordinary reference validation.
|
||||
Scene descriptions instead require the exact one-scene identity/range.
|
||||
NPC and location occurrence normalizers share [duplicate-position bookkeeping](../../internal/modules/dnd/shared/duplicates.go);
|
||||
each family supplies exact identity and source-reference eligibility, clones its
|
||||
retained records, and maps positions back to original-input diagnostics.
|
||||
NPC and location occurrences, combat turns, and spell casts share
|
||||
[duplicate-position bookkeeping](../../internal/modules/dnd/shared/duplicates.go).
|
||||
Each family supplies identity and source-reference eligibility and owns its output
|
||||
and diagnostic mapping. Spells retain the original list when no duplicate is
|
||||
removed; item occurrences keep their bucket-plus-exact-equality check separately.
|
||||
|
||||
Guidance describes all currently detectable model-controlled defects using
|
||||
contextual names, response fields, and source ranges. It does not copy operator
|
||||
|
||||
@@ -487,10 +487,11 @@ or move every family's checks without a demonstrated cross-stage need.
|
||||
|
||||
### CAT-021 — Exact-Duplicate Group Bookkeeping Is Reimplemented
|
||||
|
||||
**Progress:** NPC and location occurrence normalizers now share pure position
|
||||
grouping while retaining their own identity, eligibility, cloning, and original
|
||||
index diagnostics. Interleaved, invalid-reference, distinct-evidence, and
|
||||
permutation tests pass. Combat and the conditional spell review remain pending.
|
||||
**Status:** Resolved. NPC/location occurrences, combat turns, and spell casts
|
||||
share pure position grouping while retaining family-owned identity, eligibility,
|
||||
output, and original-index diagnostics. Spell no-change and empty representations
|
||||
remain local; item occurrences deliberately retain their bucket-plus-exact-equality
|
||||
implementation. Independent normalizer and grouping tests pass.
|
||||
|
||||
**Assessment:** Pure shared-helper opportunity; original medium.
|
||||
|
||||
|
||||
@@ -802,6 +802,12 @@ Comment family-owned eligibility/mapping.
|
||||
|
||||
## Stage 29 — Reuse Grouping For Combat And Review Spells
|
||||
|
||||
**Completed.** Combat and spell duplicate grouping use the shared bookkeeping
|
||||
with family-owned keys and eligibility. Spell empty/no-change cases retain the
|
||||
original list; item occurrences deliberately keep bucket-plus-exact equality.
|
||||
Combat evidence/index ownership and spell no-change storage have regressions;
|
||||
the specified combat, spell, and item normalizer suites pass.
|
||||
|
||||
**Coverage:** CAT-021 completion. **Prerequisites:** Stage 28.
|
||||
|
||||
Migrate combat turns without identity/eligibility changes. Migrate spells only if
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package combatturns
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||
)
|
||||
|
||||
func TestCollapseDuplicatesKeepsEvidenceAndOriginalIndices(t *testing.T) {
|
||||
doc := &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 10}, {ID: 20}}}
|
||||
ref := func(id int) source.SourceRef {
|
||||
return source.SourceRef{SourceID: doc.ID, StartUnitID: id, EndUnitID: id}
|
||||
}
|
||||
a, b, invalid := validTurn("Aria", ref(10)), validTurn("Aria", ref(20)), validTurn("Aria", ref(999))
|
||||
records := []normalizedRecord{{turn: a, inputIndex: 10}, {turn: b, inputIndex: 20}, {turn: invalid, inputIndex: 30}, {turn: b, inputIndex: 40}, {turn: invalid, inputIndex: 50}, {turn: a, inputIndex: 60}}
|
||||
output, findings := collapseDuplicates(records, source.NewDocumentIndex(doc))
|
||||
if want := []dnd.CombatTurn{a, b, invalid, invalid}; !reflect.DeepEqual(output, want) {
|
||||
t.Fatalf("output = %#v, want %#v", output, want)
|
||||
}
|
||||
if len(findings) != 2 || !strings.Contains(findings[0].Message, "retained input index 10") || !strings.Contains(findings[0].Message, "removed input index 60") || !strings.Contains(findings[1].Message, "retained input index 20") || !strings.Contains(findings[1].Message, "removed input index 40") {
|
||||
t.Fatalf("findings = %#v", findings)
|
||||
}
|
||||
output[0].SourceRefs[0].StartUnitID = 20
|
||||
if records[0].turn.SourceRefs[0].StartUnitID != 10 {
|
||||
t.Fatal("output aliases input evidence")
|
||||
}
|
||||
}
|
||||
@@ -200,48 +200,25 @@ func sourceRefsEqual(left, right []source.SourceRef) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
type duplicateGroup struct {
|
||||
retainedIndex int
|
||||
removed []int
|
||||
}
|
||||
|
||||
func collapseDuplicates(records []normalizedRecord, documentIndex source.DocumentIndex) ([]dnd.CombatTurn, []diagnostics.Finding) {
|
||||
// Combat owns its identity and evidence eligibility; positions map back to inputs.
|
||||
retained, groups := shared.GroupDuplicates(len(records), func(index int) (string, bool) {
|
||||
return duplicateKey(records[index].turn, documentIndex)
|
||||
})
|
||||
output := make([]dnd.CombatTurn, 0, len(retained))
|
||||
for _, index := range retained {
|
||||
output = append(output, cloneCombatTurn(records[index].turn))
|
||||
}
|
||||
if len(records) == 0 {
|
||||
return make([]dnd.CombatTurn, 0), nil
|
||||
return output, nil
|
||||
}
|
||||
|
||||
keep := make([]bool, len(records))
|
||||
groups := make([]duplicateGroup, 0)
|
||||
groupByKey := make(map[string]int)
|
||||
for index, record := range records {
|
||||
key, eligible := duplicateKey(record.turn, documentIndex)
|
||||
if !eligible {
|
||||
keep[index] = true
|
||||
continue
|
||||
}
|
||||
groupIndex, exists := groupByKey[key]
|
||||
if !exists {
|
||||
groupByKey[key] = len(groups)
|
||||
groups = append(groups, duplicateGroup{retainedIndex: record.inputIndex})
|
||||
keep[index] = true
|
||||
continue
|
||||
}
|
||||
groups[groupIndex].removed = append(groups[groupIndex].removed, record.inputIndex)
|
||||
}
|
||||
|
||||
output := make([]dnd.CombatTurn, 0, len(records))
|
||||
for index, record := range records {
|
||||
if keep[index] {
|
||||
output = append(output, cloneCombatTurn(record.turn))
|
||||
}
|
||||
}
|
||||
|
||||
findings := make([]diagnostics.Finding, 0)
|
||||
findings := make([]diagnostics.Finding, 0, len(groups))
|
||||
for _, group := range groups {
|
||||
if len(group.removed) == 0 {
|
||||
continue
|
||||
removed := make([]int, len(group.Removed))
|
||||
for index, position := range group.Removed {
|
||||
removed[index] = records[position].inputIndex
|
||||
}
|
||||
findings = append(findings, duplicateFinding(group.retainedIndex, group.removed))
|
||||
findings = append(findings, duplicateFinding(records[group.Representative].inputIndex, removed))
|
||||
}
|
||||
return output, findings
|
||||
}
|
||||
|
||||
@@ -150,59 +150,25 @@ func canonicalizeSourceRefs(order shared.SourceRefOrder, input []source.SourceRe
|
||||
return canonical, orderChanged, len(input) - len(canonical)
|
||||
}
|
||||
|
||||
type duplicateGroup struct {
|
||||
retainedIndex int
|
||||
removed []int
|
||||
}
|
||||
|
||||
func collapseDuplicateSpellCasts(input dnd.SpellList, documentIndex source.DocumentIndex, catalog spellcatalog.EffectiveCatalog) (dnd.SpellList, []diagnostics.Finding) {
|
||||
if len(input.SpellCasts) == 0 {
|
||||
return input, nil
|
||||
}
|
||||
|
||||
keep := make([]bool, len(input.SpellCasts))
|
||||
groups := make([]duplicateGroup, 0)
|
||||
groupByKey := make(map[string]int)
|
||||
for index, cast := range input.SpellCasts {
|
||||
key, eligible := duplicateKey(cast, documentIndex, catalog)
|
||||
if !eligible {
|
||||
keep[index] = true
|
||||
continue
|
||||
}
|
||||
groupIndex, exists := groupByKey[key]
|
||||
if !exists {
|
||||
groupByKey[key] = len(groups)
|
||||
groups = append(groups, duplicateGroup{retainedIndex: index})
|
||||
keep[index] = true
|
||||
continue
|
||||
}
|
||||
groups[groupIndex].removed = append(groups[groupIndex].removed, index)
|
||||
}
|
||||
|
||||
removedAny := false
|
||||
for _, group := range groups {
|
||||
if len(group.removed) > 0 {
|
||||
removedAny = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !removedAny {
|
||||
// Spell lookup and evidence eligibility remain local to this family.
|
||||
retained, groups := shared.GroupDuplicates(len(input.SpellCasts), func(index int) (string, bool) {
|
||||
return duplicateKey(input.SpellCasts[index], documentIndex, catalog)
|
||||
})
|
||||
// Keep the original list and its nil/empty behavior when nothing collapses.
|
||||
if len(groups) == 0 {
|
||||
return input, nil
|
||||
}
|
||||
|
||||
output := dnd.SpellList{SpellCasts: make([]dnd.SpellCast, 0, len(input.SpellCasts))}
|
||||
for index, cast := range input.SpellCasts {
|
||||
if keep[index] {
|
||||
output.SpellCasts = append(output.SpellCasts, cast)
|
||||
}
|
||||
for _, index := range retained {
|
||||
output.SpellCasts = append(output.SpellCasts, input.SpellCasts[index])
|
||||
}
|
||||
|
||||
findings := make([]diagnostics.Finding, 0)
|
||||
findings := make([]diagnostics.Finding, 0, len(groups))
|
||||
for _, group := range groups {
|
||||
if len(group.removed) == 0 {
|
||||
continue
|
||||
}
|
||||
findings = append(findings, duplicateFinding(group.retainedIndex, group.removed))
|
||||
findings = append(findings, duplicateFinding(group.Representative, group.Removed))
|
||||
}
|
||||
return output, findings
|
||||
}
|
||||
|
||||
@@ -357,6 +357,20 @@ func TestNormalizeKeepsDistinctAndIneligibleCastsSeparate(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDuplicateGroupingPreservesUnchangedSpellListStorage(t *testing.T) {
|
||||
normalizer := newNormalizer(t)
|
||||
index := source.NewDocumentIndex(sourceDocument(3))
|
||||
for _, input := range []dnd.SpellList{{}, {SpellCasts: []dnd.SpellCast{}}, {SpellCasts: []dnd.SpellCast{{Spell: "Cure Wounds", Caster: "Aria", SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 1, EndUnitID: 1}}}}}} {
|
||||
output, findings := collapseDuplicateSpellCasts(input, index, normalizer.effectiveCatalog)
|
||||
if !reflect.DeepEqual(output, input) || findings != nil {
|
||||
t.Fatalf("unchanged = %#v, %#v, want %#v", output, findings, input)
|
||||
}
|
||||
if len(input.SpellCasts) > 0 && &output.SpellCasts[0] != &input.SpellCasts[0] {
|
||||
t.Fatal("unchanged list replaced its storage")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeDoesNotCollapseAdjacentOrOverlappingEvidence(t *testing.T) {
|
||||
doc := sourceDocument(8)
|
||||
input := dnd.SpellList{SpellCasts: []dnd.SpellCast{
|
||||
|
||||
Reference in New Issue
Block a user