Retain all diagnostic groups without capacity caps

This commit is contained in:
2026-11-26 01:59:34 +00:00
parent 6133902b9b
commit b0dc84a479
16 changed files with 146 additions and 116 deletions

View File

@@ -7,26 +7,16 @@ import (
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
)
const (
MaxWarningGroups = 128
MaxNonWarningGroups = 256
)
// Aggregator merges origin-enriched diagnostics in caller-supplied canonical
// order. Its zero value is ready for use.
type Aggregator struct {
groups []contracts.DiagnosticGroup
indices map[groupKey]int
warningGroups int
nonWarningGroups int
warningOccurrences int
nonWarningOccurrences int
unrepresentedOccurrences int
groups []contracts.DiagnosticGroup
indices map[groupKey]int
warningOccurrences int
nonWarningOccurrences int
}
// Add validates and incorporates one final diagnostic group. Actionable
// warnings cannot overflow; later non-warning groups are represented by exact
// unrepresented-occurrence metadata once their fixed bound is reached.
// Add validates and incorporates one final diagnostic group.
func (aggregator *Aggregator) Add(group contracts.DiagnosticGroup) error {
if err := group.Validate(); err != nil {
return fmt.Errorf("diagnostic group: %w", err)
@@ -45,25 +35,9 @@ func (aggregator *Aggregator) Add(group contracts.DiagnosticGroup) error {
aggregator.addOccurrenceTotal(group.Disposition, group.OccurrenceCount)
return nil
}
if group.Disposition == contracts.DiagnosticDispositionWarning {
if aggregator.warningGroups >= MaxWarningGroups {
return errors.New("diagnostic warning groups exceed maximum count")
}
} else if aggregator.nonWarningGroups >= MaxNonWarningGroups {
if err := aggregator.checkOccurrenceTotal(group.Disposition, group.OccurrenceCount); err != nil {
return err
}
aggregator.addOccurrenceTotal(group.Disposition, group.OccurrenceCount)
return aggregator.addUnrepresented(group.OccurrenceCount)
}
if err := aggregator.checkOccurrenceTotal(group.Disposition, group.OccurrenceCount); err != nil {
return err
}
if group.Disposition == contracts.DiagnosticDispositionWarning {
aggregator.warningGroups++
} else {
aggregator.nonWarningGroups++
}
aggregator.addOccurrenceTotal(group.Disposition, group.OccurrenceCount)
aggregator.indices[key] = len(aggregator.groups)
aggregator.groups = append(aggregator.groups, contracts.CloneDiagnosticCollection(contracts.DiagnosticCollection{Groups: []contracts.DiagnosticGroup{group}}).Groups[0])
@@ -96,9 +70,7 @@ func (aggregator *Aggregator) Collection() contracts.DiagnosticCollection {
return contracts.DiagnosticCollection{}
}
return contracts.CloneDiagnosticCollection(contracts.DiagnosticCollection{
Groups: aggregator.groups,
Truncated: aggregator.unrepresentedOccurrences > 0,
UnrepresentedOccurrenceCount: aggregator.unrepresentedOccurrences,
Groups: aggregator.groups,
})
}
@@ -118,14 +90,6 @@ func (aggregator *Aggregator) merge(index int, incoming contracts.DiagnosticGrou
return nil
}
func (aggregator *Aggregator) addUnrepresented(count int) error {
if count > maximumInt()-aggregator.unrepresentedOccurrences {
return errors.New("diagnostic unrepresented occurrence count overflow")
}
aggregator.unrepresentedOccurrences += count
return nil
}
func containsGroupSample(samples []contracts.DiagnosticSample, candidate contracts.DiagnosticSample) bool {
for _, sample := range samples {
if sample.Scope != candidate.Scope || sample.Message != candidate.Message || sample.ChunkID != candidate.ChunkID {

View File

@@ -1,6 +1,7 @@
package diagnostics
import (
"fmt"
"testing"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
@@ -32,30 +33,39 @@ func TestAggregatorMergesByOriginAndPreservesFirstOccurrenceOrder(t *testing.T)
}
}
func TestAggregatorEnforcesWarningBoundAndTruncatesOnlyNonWarnings(t *testing.T) {
warnings := Aggregator{}
for index := 0; index < MaxWarningGroups; index++ {
group := groupForAggregation("scope", "message", contracts.DiagnosticDispositionWarning, contracts.DiagnosticCategoryFallback, "module")
group.ReasonCode = "warning-" + string(rune('a'+index))
if err := warnings.Add(group); err != nil {
t.Fatalf("warning Add(%d) error = %v", index, err)
func TestAggregatorRetainsManyWarningAndNonWarningGroups(t *testing.T) {
aggregator := Aggregator{}
for index := 0; index < 400; index++ {
for _, kind := range []struct {
disposition contracts.DiagnosticDisposition
category contracts.DiagnosticCategory
}{
{contracts.DiagnosticDispositionWarning, contracts.DiagnosticCategoryFallback},
{contracts.DiagnosticDispositionAdvisory, contracts.DiagnosticCategoryDataQuality},
} {
group := groupForAggregation("scope", "message", kind.disposition, kind.category, "module")
group.ReasonCode = fmt.Sprintf("reason-%d", index)
if err := aggregator.Add(group); err != nil {
t.Fatalf("Add(%d) error = %v", index, err)
}
}
}
if err := warnings.Add(groupForAggregation("overflow", "overflow", contracts.DiagnosticDispositionWarning, contracts.DiagnosticCategoryFallback, "overflow")); err == nil {
t.Fatal("warning overflow error = nil, want error")
if got := len(aggregator.Collection().Groups); got != 800 {
t.Fatalf("group count = %d, want 800", got)
}
}
nonWarnings := Aggregator{}
for index := 0; index < MaxNonWarningGroups+3; index++ {
group := groupForAggregation("scope", "message", contracts.DiagnosticDispositionAdvisory, contracts.DiagnosticCategoryDataQuality, "module")
group.ReasonCode = "advisory-" + string(rune('a'+index))
if err := nonWarnings.Add(group); err != nil {
t.Fatalf("non-warning Add(%d) error = %v", index, err)
func TestAggregatorSamplesRepeatedOccurrences(t *testing.T) {
aggregator := Aggregator{}
for i := 0; i < 5; i++ {
group := groupForAggregation(fmt.Sprintf("scope-%d", i), "message", contracts.DiagnosticDispositionAdvisory, contracts.DiagnosticCategoryDataQuality, "module")
if err := aggregator.Add(group); err != nil {
t.Fatal(err)
}
}
collection := nonWarnings.Collection()
if len(collection.Groups) != MaxNonWarningGroups || !collection.Truncated || collection.UnrepresentedOccurrenceCount != 3 {
t.Fatalf("collection = %#v, want bounded non-warning groups and three unrepresented occurrences", collection)
groups := aggregator.Collection().Groups
if len(groups) != 1 || groups[0].OccurrenceCount != 5 || len(groups[0].Samples) != 3 || groups[0].OmittedSampleCount != 2 {
t.Fatalf("groups = %#v, want five occurrences and three samples", groups)
}
}

View File

@@ -1,4 +1,4 @@
// Package diagnostics provides bounded local grouping for producer and
// Package diagnostics provides local grouping for producer and
// validator diagnostic results.
package diagnostics
@@ -33,9 +33,6 @@ func (collector *Collector) Add(diagnostic contracts.ProducerDiagnostic) error {
diagnosticKey := key{disposition: diagnostic.Disposition, category: diagnostic.Category, reasonCode: diagnostic.ReasonCode}
index, exists := collector.indices[diagnosticKey]
if !exists {
if len(collector.diagnostics) >= contracts.MaxProducerDiagnosticGroups {
return errors.New("producer diagnostics exceed maximum group count")
}
collector.indices[diagnosticKey] = len(collector.diagnostics)
collector.diagnostics = append(collector.diagnostics, contracts.CloneProducerDiagnostics([]contracts.ProducerDiagnostic{diagnostic})[0])
return nil

View File

@@ -1,6 +1,7 @@
package diagnostics
import (
"fmt"
"testing"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
@@ -33,7 +34,7 @@ func TestCollectorCountsOccurrencesAndRetainsDistinctSamplesInOrder(t *testing.T
}
}
func TestCollectorSeparatesGroupsAndRejectsInvalidOrExcessiveGroups(t *testing.T) {
func TestCollectorSeparatesGroupsAndRejectsInvalidGroups(t *testing.T) {
collector := NewCollector()
if err := collector.Add(advisory("one", "first")); err != nil {
t.Fatalf("Add() error = %v", err)
@@ -54,16 +55,16 @@ func TestCollectorSeparatesGroupsAndRejectsInvalidOrExcessiveGroups(t *testing.T
t.Fatal("Add() error = nil, want invalid diagnostic error")
}
limited := NewCollector()
for index := 0; index < contracts.MaxProducerDiagnosticGroups; index++ {
many := NewCollector()
for index := 0; index < 400; index++ {
diagnostic := advisory("scope", "message")
diagnostic.ReasonCode = "reason-" + string(rune('a'+index))
if err := limited.Add(diagnostic); err != nil {
diagnostic.ReasonCode = fmt.Sprintf("reason-%d", index)
if err := many.Add(diagnostic); err != nil {
t.Fatalf("Add(%d) error = %v", index, err)
}
}
if err := limited.Add(advisory("overflow", "overflow")); err == nil {
t.Fatal("Add() error = nil, want local group limit error")
if got := len(many.Diagnostics()); got != 400 {
t.Fatalf("group count = %d, want 400", got)
}
}
@@ -80,6 +81,22 @@ func TestCollectorReturnsIndependentSnapshots(t *testing.T) {
}
}
func TestCollectorRejectsOccurrenceOverflow(t *testing.T) {
collector := NewCollector()
first := advisory("scope", "message")
first.OccurrenceCount = maximumInt()
first.OmittedSampleCount = maximumInt() - 1
if err := collector.Add(first); err != nil {
t.Fatal(err)
}
if err := collector.Add(advisory("other", "message")); err == nil {
t.Fatal("Add() accepted occurrence count overflow")
}
if got := collector.Diagnostics(); len(got) != 1 || got[0].OccurrenceCount != maximumInt() {
t.Fatalf("collector changed after overflow: %#v", got)
}
}
func advisory(scope string, message string) contracts.ProducerDiagnostic {
return contracts.ProducerDiagnostic{
Disposition: contracts.DiagnosticDispositionAdvisory,