Prepare occurrence chronology keys before sorting

This commit is contained in:
2026-11-26 02:56:43 +00:00
parent 26da344f27
commit 7077cee35e
9 changed files with 109 additions and 14 deletions

View File

@@ -17,7 +17,7 @@ func TestCollapseDuplicatesPreservesIndependentEvidenceAndOriginalIndices(t *tes
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}}
records := []normalizedRecord{{occurrence: a, inputIndex: 10}, {occurrence: b, inputIndex: 20}, {occurrence: invalid, inputIndex: 30}, {occurrence: b, inputIndex: 40}, {occurrence: invalid, inputIndex: 50}, {occurrence: a, inputIndex: 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)
@@ -25,7 +25,7 @@ func TestCollapseDuplicatesPreservesIndependentEvidenceAndOriginalIndices(t *tes
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))
_, reversed := collapseDuplicates([]normalizedRecord{{occurrence: b, inputIndex: 20}, {occurrence: a, inputIndex: 10}, {occurrence: a, inputIndex: 60}, {occurrence: b, inputIndex: 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)
}

View File

@@ -93,8 +93,10 @@ func (n *Normalizer) PrepareNormalize(ctx context.Context, req contracts.Normali
}
type normalizedRecord struct {
occurrence dnd.NPCOccurrence
inputIndex int
occurrence dnd.NPCOccurrence
inputIndex int
earliest int
hasEvidence bool
}
func normalizeList(input dnd.NPCOccurrenceList, documentIndex source.DocumentIndex, order shared.SourceRefOrder, registry *npcregistry.Registry) (dnd.NPCOccurrenceList, []diagnostics.Finding, error) {
@@ -109,7 +111,8 @@ func normalizeList(input dnd.NPCOccurrenceList, documentIndex source.DocumentInd
if err != nil {
return dnd.NPCOccurrenceList{}, nil, fmt.Errorf("occurrences[%d]: %w", index, err)
}
records[index] = normalizedRecord{occurrence: occurrence, inputIndex: index}
earliest, hasEvidence := order.EarliestValid(occurrence.SourceRefs)
records[index] = normalizedRecord{occurrence: occurrence, inputIndex: index, earliest: earliest, hasEvidence: hasEvidence}
if refsChanged {
findings = append(findings, diagnostics.Finding{
Scope: occurrenceScope(index),
@@ -121,7 +124,14 @@ func normalizeList(input dnd.NPCOccurrenceList, documentIndex source.DocumentInd
}
sort.SliceStable(records, func(left, right int) bool {
return occurrencemodel.Less(order, records[left].occurrence, records[right].occurrence)
l, r := records[left], records[right]
if l.hasEvidence != r.hasEvidence {
return l.hasEvidence
}
if l.hasEvidence && l.earliest != r.earliest {
return l.earliest < r.earliest
}
return occurrencemodel.LessAfterEvidence(order, l.occurrence, r.occurrence)
})
for position, record := range records {
if position == record.inputIndex {

View File

@@ -117,6 +117,33 @@ func TestNormalizeOrdersAndCollapsesExactDuplicatesOnly(t *testing.T) {
}
}
func TestNormalizeOrdersTiedEvidenceAndRetainsInvalidOccurrences(t *testing.T) {
doc := &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 50}, {ID: 10}, {ID: 90}, {ID: 30}, {ID: 70}, {ID: 20}, {ID: 60}, {ID: 40}}}
ref := func(id int) source.SourceRef {
return source.SourceRef{SourceID: doc.ID, StartUnitID: id, EndUnitID: id}
}
earlier := occurrence("Ária", dnd.NPCOccurrenceKindDialogue, ref(50))
earlier.SourceRefs = append(earlier.SourceRefs, ref(10), ref(90), ref(30), ref(70), ref(20), ref(60))
later := occurrence("Ária", dnd.NPCOccurrenceKindDialogue, ref(50))
later.SourceRefs = append(later.SourceRefs, ref(10), ref(90), ref(30), ref(70), ref(20), ref(40))
invalid := occurrence("Ária", dnd.NPCOccurrenceKindDialogue, ref(999))
input := dnd.NPCOccurrenceList{Occurrences: []dnd.NPCOccurrence{later, invalid, earlier, later, invalid}}
normalizer, err := New(Options{}, npcReferences(t))
if err != nil {
t.Fatal(err)
}
result, err := runNormalize(normalizer, context.Background(), contracts.NormalizeInput[dnd.NPCOccurrenceList]{Source: doc, MergeOutput: contracts.MergeArtifact[dnd.NPCOccurrenceList]{Value: input}})
if err != nil {
t.Fatal(err)
}
if want := []dnd.NPCOccurrence{earlier, later, invalid, invalid}; !reflect.DeepEqual(result.Value.Occurrences, want) {
t.Fatalf("ordered = %#v, want %#v", result.Value.Occurrences, want)
}
if !hasDiagnostic(result.Diagnostics, ReasonCodeOccurrencesReordered) || !hasDiagnostic(result.Diagnostics, ReasonCodeDuplicateCollapsed) {
t.Fatalf("diagnostics = %#v", result.Diagnostics)
}
}
func TestNormalizerContract(t *testing.T) {
_, err := New(Options{}, npcReferences(t))
if err != nil {