Harden diagnostic handling and warning presentation

This commit is contained in:
2026-08-27 18:41:59 +00:00
parent 1025001f20
commit 079d5af337
67 changed files with 518 additions and 318 deletions

View File

@@ -108,11 +108,11 @@ func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalize
}
index := source.NewDocumentIndex(req.Source)
order := shared.NewSourceRefOrderFromIndex(index)
value, warnings, err := normalizeList(req.MergeOutput.Value, index, order, registry)
value, findings, err := normalizeList(req.MergeOutput.Value, index, order, registry)
if err != nil {
return contracts.TypedNormalizeResult[dnd.NPCOccurrenceList]{}, normalizerErrorf("validate NPC registry pairs: %w", err)
}
diagnosticGroups, err := diagnostics.NormalizationDiagnostics(warnings)
diagnosticGroups, err := diagnostics.NormalizationDiagnostics(findings)
if err != nil {
return contracts.TypedNormalizeResult[dnd.NPCOccurrenceList]{}, normalizerErrorf("collect diagnostics: %w", err)
}
@@ -130,7 +130,7 @@ func normalizeList(input dnd.NPCOccurrenceList, documentIndex source.DocumentInd
}
records := make([]normalizedRecord, len(input.Occurrences))
warnings := make([]diagnostics.Finding, 0)
findings := make([]diagnostics.Finding, 0)
for index, inputOccurrence := range input.Occurrences {
occurrence, refsChanged, err := normalizeOccurrence(inputOccurrence, order, registry)
if err != nil {
@@ -138,7 +138,7 @@ func normalizeList(input dnd.NPCOccurrenceList, documentIndex source.DocumentInd
}
records[index] = normalizedRecord{occurrence: occurrence, inputIndex: index}
if refsChanged {
warnings = append(warnings, diagnostics.Finding{
findings = append(findings, diagnostics.Finding{
Scope: occurrenceScope(index),
ReasonCode: ReasonCodeSourceRefsNormalized,
Message: fmt.Sprintf("input index %d: source references normalized (original count %d, final count %d)",
@@ -154,16 +154,16 @@ func normalizeList(input dnd.NPCOccurrenceList, documentIndex source.DocumentInd
if position == record.inputIndex {
continue
}
warnings = append(warnings, diagnostics.Finding{
findings = append(findings, diagnostics.Finding{
Scope: occurrenceScope(record.inputIndex),
ReasonCode: ReasonCodeOccurrencesReordered,
Message: fmt.Sprintf("input index %d moved to normalized position %d by source chronology", record.inputIndex, position),
})
}
output, duplicateWarnings := collapseDuplicates(records, documentIndex)
warnings = append(warnings, duplicateWarnings...)
return dnd.NPCOccurrenceList{Occurrences: output}, warnings, nil
output, duplicateFindings := collapseDuplicates(records, documentIndex)
findings = append(findings, duplicateFindings...)
return dnd.NPCOccurrenceList{Occurrences: output}, findings, nil
}
func normalizeOccurrence(input dnd.NPCOccurrence, order shared.SourceRefOrder, registry *npcregistry.Registry) (dnd.NPCOccurrence, bool, error) {
@@ -220,16 +220,16 @@ func collapseDuplicates(records []normalizedRecord, documentIndex source.Documen
output = append(output, cloneOccurrence(record.occurrence))
}
}
warnings := make([]diagnostics.Finding, 0)
findings := make([]diagnostics.Finding, 0)
for _, group := range groups {
if len(group.removed) != 0 {
warnings = append(warnings, duplicateWarning(group.retainedIndex, group.removed))
findings = append(findings, duplicateFinding(group.retainedIndex, group.removed))
}
}
return output, warnings
return output, findings
}
func duplicateWarning(retainedIndex int, removed []int) diagnostics.Finding {
func duplicateFinding(retainedIndex int, removed []int) diagnostics.Finding {
issues := make([]string, len(removed))
for index, removedIndex := range removed {
issues[index] = fmt.Sprintf("removed input index %d", removedIndex)

View File

@@ -40,7 +40,7 @@ func TestNormalizeValidatesPairsAndClones(t *testing.T) {
}
second, err := normalizer.Normalize(context.Background(), contracts.TypedNormalizeRequest[dnd.NPCOccurrenceList]{Source: doc, MergeOutput: contracts.MergeArtifact[dnd.NPCOccurrenceList]{Value: result.Value}})
if err != nil || !reflect.DeepEqual(second.Value, result.Value) || len(second.Diagnostics) != 0 {
t.Fatalf("second normalization = %#v, %v; want idempotent output without warnings", second, err)
t.Fatalf("second normalization = %#v, %v; want idempotent output without findings", second, err)
}
result.Value.Occurrences[0].SourceRefs[0].StartUnitID = 999
if input.Occurrences[0].SourceRefs[0].StartUnitID == 999 {
@@ -120,7 +120,7 @@ func TestNormalizeOrdersAndCollapsesExactDuplicatesOnly(t *testing.T) {
}
}
func TestNormalizerContractAndDeterministicWarnings(t *testing.T) {
func TestNormalizerContractAndDeterministicFindings(t *testing.T) {
normalizer, err := New(Options{}, npcReferences(t))
if err != nil {
t.Fatal(err)
@@ -139,7 +139,7 @@ func TestNormalizerContractAndDeterministicWarnings(t *testing.T) {
}
}
func TestNormalizeBoundsWarnings(t *testing.T) {
func TestNormalizeBoundsFindings(t *testing.T) {
count := contracts.MaxDiagnosticSamples + 5
doc := &source.SourceDocument{ID: "session", Units: make([]source.SourceUnit, count)}
input := dnd.NPCOccurrenceList{Occurrences: make([]dnd.NPCOccurrence, count)}