Share occurrence duplicate grouping bookkeeping

This commit is contained in:
2026-11-26 02:53:03 +00:00
parent 6275c62d25
commit f9dd53f4c2
9 changed files with 180 additions and 66 deletions

View File

@@ -116,6 +116,9 @@ and advisory responsibilities; [Configuration](../config.md#production-validator
owns their order and replacement rules. Extraction containment requires the owns their order and replacement rules. Extraction containment requires the
entire cited span inside the current chunk after ordinary reference validation. entire cited span inside the current chunk after ordinary reference validation.
Scene descriptions instead require the exact one-scene identity/range. 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.
Guidance describes all currently detectable model-controlled defects using Guidance describes all currently detectable model-controlled defects using
contextual names, response fields, and source ranges. It does not copy operator contextual names, response fields, and source ranges. It does not copy operator

View File

@@ -487,6 +487,11 @@ or move every family's checks without a demonstrated cross-stage need.
### CAT-021 — Exact-Duplicate Group Bookkeeping Is Reimplemented ### 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.
**Assessment:** Pure shared-helper opportunity; original medium. **Assessment:** Pure shared-helper opportunity; original medium.
**Source:** [EQ-002](10-equivalence.md#eq-002--exact-duplicate-collapse-repeats-stable-group-bookkeeping). **Source:** [EQ-002](10-equivalence.md#eq-002--exact-duplicate-collapse-repeats-stable-group-bookkeeping).

View File

@@ -781,6 +781,12 @@ Update D&D family guidance.
## Stage 28 — Share NPC And Location Duplicate Bookkeeping ## Stage 28 — Share NPC And Location Duplicate Bookkeeping
**Completed.** Shared grouping returns retained positions and duplicate groups
in representative order; NPC and location families retain eligibility, identity,
cloning, and original-index diagnostics. Independent tests cover interleaving,
invalid references, distinct evidence, permutations, and ownership. The shared
and both normalizer suites pass. Combat and spell review follow next.
**Coverage:** CAT-021 first callers. **Prerequisites:** preceding stage order. **Coverage:** CAT-021 first callers. **Prerequisites:** preceding stage order.
Implement selected grouping helper and migrate NPC/location occurrences. Families Implement selected grouping helper and migrate NPC/location occurrences. Families

View File

@@ -0,0 +1,36 @@
package locationoccurrences
import (
"reflect"
"strings"
"testing"
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
)
func TestCollapseDuplicatesPreservesIndependentEvidenceAndOriginalIndices(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 := dnd.LocationOccurrence{LocationID: "place", Name: "Inn", Kind: dnd.LocationOccurrenceKindMentioned, SourceRefs: []source.SourceRef{ref(10)}}
b := dnd.LocationOccurrence{LocationID: "place", Name: "Inn", Kind: dnd.LocationOccurrenceKindMentioned, SourceRefs: []source.SourceRef{ref(20)}}
invalid := dnd.LocationOccurrence{LocationID: "place", Name: "Inn", Kind: dnd.LocationOccurrenceKindMentioned, SourceRefs: []source.SourceRef{ref(999)}}
records := []normalizedRecord{{a, 10}, {b, 20}, {invalid, 30}, {b, 40}, {invalid, 50}, {a, 60}}
output, findings := collapseDuplicates(records, source.NewDocumentIndex(doc))
if want := []dnd.LocationOccurrence{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)
}
_, reversed := collapseDuplicates([]normalizedRecord{{b, 20}, {a, 10}, {a, 60}, {b, 40}}, source.NewDocumentIndex(doc))
if len(reversed) != 2 || !strings.Contains(reversed[0].Message, "retained input index 20") || !strings.Contains(reversed[1].Message, "retained input index 10") {
t.Fatalf("permuted findings = %#v", reversed)
}
output[0].SourceRefs[0].StartUnitID = 20
if records[0].occurrence.SourceRefs[0].StartUnitID != 10 {
t.Fatal("output aliases input evidence")
}
}

View File

@@ -164,44 +164,29 @@ func sourceRefsEqual(left, right []source.SourceRef) bool {
return true return true
} }
type duplicateGroup struct {
retainedIndex int
removed []int
}
func collapseDuplicates(records []normalizedRecord, documentIndex source.DocumentIndex) ([]dnd.LocationOccurrence, []diagnostics.Finding) { func collapseDuplicates(records []normalizedRecord, documentIndex source.DocumentIndex) ([]dnd.LocationOccurrence, []diagnostics.Finding) {
if len(records) == 0 { // This family owns eligibility and maps shared positions to original indices.
return make([]dnd.LocationOccurrence, 0), nil retained, groups := shared.GroupDuplicates(len(records), func(index int) (string, bool) {
} occurrence := records[index].occurrence
keep := make([]bool, len(records)) if !validSourceRefs(documentIndex, occurrence.SourceRefs) {
groups := make([]duplicateGroup, 0) return "", false
groupByKey := make(map[string]int)
for index, record := range records {
if !validSourceRefs(documentIndex, record.occurrence.SourceRefs) {
keep[index] = true
continue
} }
key := exactIdentity(record.occurrence) return exactIdentity(occurrence), true
groupIndex, exists := groupByKey[key] })
if !exists { output := make([]dnd.LocationOccurrence, 0, len(retained))
groupByKey[key] = len(groups) for _, index := range retained {
groups = append(groups, duplicateGroup{retainedIndex: record.inputIndex}) output = append(output, cloneOccurrence(records[index].occurrence))
keep[index] = true
continue
}
groups[groupIndex].removed = append(groups[groupIndex].removed, record.inputIndex)
} }
output := make([]dnd.LocationOccurrence, 0, len(records)) findings := make([]diagnostics.Finding, 0, len(groups))
for index, record := range records {
if keep[index] {
output = append(output, cloneOccurrence(record.occurrence))
}
}
findings := make([]diagnostics.Finding, 0)
for _, group := range groups { for _, group := range groups {
if len(group.removed) > 0 { removed := make([]int, len(group.Removed))
findings = append(findings, duplicateFinding(group.retainedIndex, group.removed)) for index, position := range group.Removed {
removed[index] = records[position].inputIndex
} }
findings = append(findings, duplicateFinding(records[group.Representative].inputIndex, removed))
}
if len(records) == 0 {
return output, nil
} }
return output, findings return output, findings
} }

View File

@@ -0,0 +1,36 @@
package npcoccurrences
import (
"reflect"
"strings"
"testing"
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
)
func TestCollapseDuplicatesPreservesIndependentEvidenceAndOriginalIndices(t *testing.T) {
doc := testDocument()
ref := func(id int) source.SourceRef {
return source.SourceRef{SourceID: doc.ID, StartUnitID: id, EndUnitID: id}
}
a := occurrence("Ária", dnd.NPCOccurrenceKindDialogue, ref(10))
b := occurrence("Ária", dnd.NPCOccurrenceKindDialogue, ref(20))
invalid := occurrence("Ária", dnd.NPCOccurrenceKindDialogue, ref(999))
records := []normalizedRecord{{a, 10}, {b, 20}, {invalid, 30}, {b, 40}, {invalid, 50}, {a, 60}}
output, findings := collapseDuplicates(records, source.NewDocumentIndex(doc))
if want := []dnd.NPCOccurrence{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)
}
_, reversed := collapseDuplicates([]normalizedRecord{{b, 20}, {a, 10}, {a, 60}, {b, 40}}, source.NewDocumentIndex(doc))
if len(reversed) != 2 || !strings.Contains(reversed[0].Message, "retained input index 20") || !strings.Contains(reversed[1].Message, "retained input index 10") {
t.Fatalf("permuted findings = %#v", reversed)
}
output[0].SourceRefs[0].StartUnitID = 30
if records[0].occurrence.SourceRefs[0].StartUnitID != 10 {
t.Fatal("output aliases input evidence")
}
}

View File

@@ -160,44 +160,29 @@ func cloneOccurrence(input dnd.NPCOccurrence) dnd.NPCOccurrence {
return output return output
} }
type duplicateGroup struct {
retainedIndex int
removed []int
}
func collapseDuplicates(records []normalizedRecord, documentIndex source.DocumentIndex) ([]dnd.NPCOccurrence, []diagnostics.Finding) { func collapseDuplicates(records []normalizedRecord, documentIndex source.DocumentIndex) ([]dnd.NPCOccurrence, []diagnostics.Finding) {
if len(records) == 0 { // This family owns eligibility and maps shared positions to original indices.
return make([]dnd.NPCOccurrence, 0), nil retained, groups := shared.GroupDuplicates(len(records), func(index int) (string, bool) {
} occurrence := records[index].occurrence
keep := make([]bool, len(records)) if !occurrencemodel.ValidSourceRefs(documentIndex, occurrence.SourceRefs) {
groups := make([]duplicateGroup, 0) return "", false
groupByKey := make(map[string]int)
for index, record := range records {
if !occurrencemodel.ValidSourceRefs(documentIndex, record.occurrence.SourceRefs) {
keep[index] = true
continue
} }
key := occurrencemodel.ExactIdentity(record.occurrence) return occurrencemodel.ExactIdentity(occurrence), true
groupIndex, exists := groupByKey[key] })
if !exists { output := make([]dnd.NPCOccurrence, 0, len(retained))
groupByKey[key] = len(groups) for _, index := range retained {
groups = append(groups, duplicateGroup{retainedIndex: record.inputIndex}) output = append(output, cloneOccurrence(records[index].occurrence))
keep[index] = true
continue
}
groups[groupIndex].removed = append(groups[groupIndex].removed, record.inputIndex)
} }
output := make([]dnd.NPCOccurrence, 0, len(records)) findings := make([]diagnostics.Finding, 0, len(groups))
for index, record := range records {
if keep[index] {
output = append(output, cloneOccurrence(record.occurrence))
}
}
findings := make([]diagnostics.Finding, 0)
for _, group := range groups { for _, group := range groups {
if len(group.removed) != 0 { removed := make([]int, len(group.Removed))
findings = append(findings, duplicateFinding(group.retainedIndex, group.removed)) for index, position := range group.Removed {
removed[index] = records[position].inputIndex
} }
findings = append(findings, duplicateFinding(records[group.Representative].inputIndex, removed))
}
if len(records) == 0 {
return output, nil
} }
return output, findings return output, findings
} }

View File

@@ -0,0 +1,37 @@
package shared
// DuplicatePositions identifies a retained representative and later duplicates
// by their positions in the caller's input sequence.
type DuplicatePositions struct {
Representative int
Removed []int
}
// GroupDuplicates retains the first eligible record per key; ineligible records
// remain independent. Duplicate groups follow representative encounter order.
func GroupDuplicates[K comparable](count int, keyFor func(int) (K, bool)) ([]int, []DuplicatePositions) {
retained := make([]int, 0, count)
groups := make([]DuplicatePositions, 0)
byKey := make(map[K]int)
for index := 0; index < count; index++ {
key, eligible := keyFor(index)
if !eligible {
retained = append(retained, index)
continue
}
if group, exists := byKey[key]; exists {
groups[group].Removed = append(groups[group].Removed, index)
continue
}
byKey[key] = len(groups)
groups = append(groups, DuplicatePositions{Representative: index})
retained = append(retained, index)
}
var duplicates []DuplicatePositions
for _, group := range groups {
if len(group.Removed) > 0 {
duplicates = append(duplicates, group)
}
}
return retained, duplicates
}

View File

@@ -0,0 +1,21 @@
package shared
import (
"reflect"
"testing"
)
func TestGroupDuplicatesKeepsIneligibleAndRepresentativeOrder(t *testing.T) {
keys := []string{"a", "b", "invalid", "b", "invalid", "a", "c"}
retained, groups := GroupDuplicates(len(keys), func(index int) (string, bool) { return keys[index], keys[index] != "invalid" })
if want := []int{0, 1, 2, 4, 6}; !reflect.DeepEqual(retained, want) {
t.Fatalf("retained = %v, want %v", retained, want)
}
if want := []DuplicatePositions{{Representative: 0, Removed: []int{5}}, {Representative: 1, Removed: []int{3}}}; !reflect.DeepEqual(groups, want) {
t.Fatalf("groups = %#v, want %#v", groups, want)
}
retained, groups = GroupDuplicates(0, func(int) (string, bool) { t.Fatal("unexpected callback"); return "", false })
if retained == nil || len(retained) != 0 || len(groups) != 0 {
t.Fatalf("empty result = %#v, %#v", retained, groups)
}
}