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

@@ -15,10 +15,10 @@ type normalizerFixtureSet struct {
}
type normalizerFixtureCase struct {
Name string `json:"name"`
Input dnd.SpellList `json:"input"`
Output dnd.SpellList `json:"output"`
WarningReasonCodes []string `json:"warning_reason_codes"`
Name string `json:"name"`
Input dnd.SpellList `json:"input"`
Output dnd.SpellList `json:"output"`
DiagnosticReasonCodes []string `json:"diagnostic_reason_codes"`
}
func TestNormalizeAcceptedFixtures(t *testing.T) {
@@ -48,8 +48,8 @@ func TestNormalizeAcceptedFixtures(t *testing.T) {
for _, diagnostic := range result.Diagnostics {
gotReasonCodes = append(gotReasonCodes, diagnostic.ReasonCode)
}
if !reflect.DeepEqual(gotReasonCodes, fixture.WarningReasonCodes) {
t.Fatalf("warning reason codes = %#v, want %#v", gotReasonCodes, fixture.WarningReasonCodes)
if !reflect.DeepEqual(gotReasonCodes, fixture.DiagnosticReasonCodes) {
t.Fatalf("diagnostic reason codes = %#v, want %#v", gotReasonCodes, fixture.DiagnosticReasonCodes)
}
})
}

View File

@@ -100,10 +100,10 @@ func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalize
index := source.NewDocumentIndex(req.Source)
order := shared.NewSourceRefOrderFromIndex(index)
value, warnings := normalizeSpellList(req.MergeOutput.Value, n.effectiveCatalog, order)
value, duplicateWarnings := collapseDuplicateSpellCasts(value, index, n.effectiveCatalog)
warnings = append(warnings, duplicateWarnings...)
diagnosticGroups, err := diagnostics.NormalizationDiagnostics(warnings, ReasonCodeSpellNameUnresolved)
value, findings := normalizeSpellList(req.MergeOutput.Value, n.effectiveCatalog, order)
value, duplicateFindings := collapseDuplicateSpellCasts(value, index, n.effectiveCatalog)
findings = append(findings, duplicateFindings...)
diagnosticGroups, err := diagnostics.NormalizationDiagnostics(findings, ReasonCodeSpellNameUnresolved)
if err != nil {
return contracts.TypedNormalizeResult[dnd.SpellList]{}, normalizerErrorf("collect diagnostics: %w", err)
}
@@ -111,7 +111,7 @@ func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalize
}
func normalizeSpellList(input dnd.SpellList, catalog spellcatalog.EffectiveCatalog, order shared.SourceRefOrder) (dnd.SpellList, []diagnostics.Finding) {
var warnings []diagnostics.Finding
var findings []diagnostics.Finding
if input.SpellCasts == nil {
return dnd.SpellList{}, nil
}
@@ -121,7 +121,7 @@ func normalizeSpellList(input dnd.SpellList, catalog spellcatalog.EffectiveCatal
cast := cloneSpellCast(inputCast)
if canonicalName, ok := catalog.Lookup(inputCast.Spell); ok {
if inputCast.Spell != canonicalName {
warnings = append(warnings, diagnostics.Finding{
findings = append(findings, diagnostics.Finding{
Scope: spellCastScope(index),
ReasonCode: ReasonCodeSpellNameCanonicalized,
Message: fmt.Sprintf("input index %d: spell name canonicalized from %q to %q",
@@ -130,7 +130,7 @@ func normalizeSpellList(input dnd.SpellList, catalog spellcatalog.EffectiveCatal
}
cast.Spell = canonicalName
} else {
warnings = append(warnings, diagnostics.Finding{
findings = append(findings, diagnostics.Finding{
Scope: spellCastScope(index),
ReasonCode: ReasonCodeSpellNameUnresolved,
Message: fmt.Sprintf("input index %d: spell name %q could not be resolved in the effective catalog",
@@ -141,7 +141,7 @@ func normalizeSpellList(input dnd.SpellList, catalog spellcatalog.EffectiveCatal
canonicalRefs, orderChanged, duplicateCount := canonicalizeSourceRefs(order, inputCast.SourceRefs)
cast.SourceRefs = canonicalRefs
if orderChanged || duplicateCount > 0 {
warnings = append(warnings, diagnostics.Finding{
findings = append(findings, diagnostics.Finding{
Scope: spellCastScope(index),
ReasonCode: ReasonCodeSourceReferencesNormalized,
Message: fmt.Sprintf("input index %d: source references normalized (original count %d, final count %d, order changed %t, duplicates removed %d)",
@@ -150,7 +150,7 @@ func normalizeSpellList(input dnd.SpellList, catalog spellcatalog.EffectiveCatal
}
output.SpellCasts[index] = cast
}
return output, warnings
return output, findings
}
func cloneSpellCast(input dnd.SpellCast) dnd.SpellCast {
@@ -221,14 +221,14 @@ func collapseDuplicateSpellCasts(input dnd.SpellList, documentIndex source.Docum
}
}
warnings := make([]diagnostics.Finding, 0)
findings := make([]diagnostics.Finding, 0)
for _, group := range groups {
if len(group.removed) == 0 {
continue
}
warnings = append(warnings, duplicateWarning(group.retainedIndex, group.removed))
findings = append(findings, duplicateFinding(group.retainedIndex, group.removed))
}
return output, warnings
return output, findings
}
func duplicateKey(cast dnd.SpellCast, documentIndex source.DocumentIndex, catalog spellcatalog.EffectiveCatalog) (string, bool) {
@@ -264,7 +264,7 @@ func writeKeyInt(builder *strings.Builder, value int) {
builder.WriteByte(';')
}
func duplicateWarning(retainedIndex int, removed []int) diagnostics.Finding {
func duplicateFinding(retainedIndex int, removed []int) diagnostics.Finding {
const maxDisplayedIndices = 20
displayed := removed
if len(displayed) > maxDisplayedIndices {

View File

@@ -155,7 +155,7 @@ func TestNormalizeCanonicalizesNamesAndReportsUnresolvedNames(t *testing.T) {
}
}
func TestNormalizeLimitsWarningsWithoutChangingSpellValues(t *testing.T) {
func TestNormalizeLimitsFindingsWithoutChangingSpellValues(t *testing.T) {
input := dnd.SpellList{SpellCasts: make([]dnd.SpellCast, contracts.MaxDiagnosticSamples+1)}
for index := range input.SpellCasts {
input.SpellCasts[index].Spell = fmt.Sprintf("Unknown Spell %d", index)
@@ -241,7 +241,7 @@ func TestNormalizeReportsDuplicateRemovalWithoutOrderChange(t *testing.T) {
}
message := diagnostic.Samples[0].Message
if !strings.Contains(message, "order changed false") || !strings.Contains(message, "duplicates removed 1") {
t.Fatalf("warning = %q, want duplicate-only repair without order change", message)
t.Fatalf("finding = %q, want duplicate-only repair without order change", message)
}
}
@@ -406,7 +406,7 @@ func TestNormalizeDoesNotCollapseAdjacentOrOverlappingEvidence(t *testing.T) {
}
}
func TestNormalizeBoundsDuplicateWarningIndices(t *testing.T) {
func TestNormalizeBoundsDuplicateFindingIndices(t *testing.T) {
doc := sourceDocument(2)
ref := source.SourceRef{SourceID: "source", StartUnitID: 1, EndUnitID: 1}
casts := make([]dnd.SpellCast, 22)
@@ -423,7 +423,7 @@ func TestNormalizeBoundsDuplicateWarningIndices(t *testing.T) {
}
message := diagnostic.Samples[0].Message
if !strings.Contains(message, "removed input indices [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]") || strings.Contains(message, ", 21]") || !strings.Contains(message, "1 additional removed input indices omitted") {
t.Fatalf("warning message = %q, want 20 displayed indices and exact omitted count", message)
t.Fatalf("finding message = %q, want 20 displayed indices and exact omitted count", message)
}
}

View File

@@ -49,7 +49,7 @@
}
]
},
"warning_reason_codes": [
"diagnostic_reason_codes": [
"spell_name_canonicalized",
"source_references_normalized",
"duplicate_spell_cast_collapsed"
@@ -79,7 +79,7 @@
}
]
},
"warning_reason_codes": []
"diagnostic_reason_codes": []
}
]
}