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

@@ -12,7 +12,6 @@ const (
MaxDiagnosticScopeBytes = 512
MaxDiagnosticMessageBytes = 4 * 1024
MaxDiagnosticSamples = 3
MaxProducerDiagnosticGroups = 64
)
// DiagnosticDisposition identifies the operator significance of a producer
@@ -97,9 +96,7 @@ type DiagnosticGroup struct {
// and presentation boundaries. Global aggregation policy is applied by the
// framework before it reaches those boundaries.
type DiagnosticCollection struct {
Groups []DiagnosticGroup `json:"groups"`
Truncated bool `json:"truncated"`
UnrepresentedOccurrenceCount int `json:"unrepresented_occurrence_count"`
Groups []DiagnosticGroup `json:"groups"`
}
// DiagnosticProjection is the validated warning/non-warning view used by
@@ -129,9 +126,6 @@ func (diagnostic ProducerDiagnostic) Validate() error {
// ValidateProducerDiagnostics validates the complete set returned by one
// producer or validator result.
func ValidateProducerDiagnostics(diagnostics []ProducerDiagnostic) error {
if len(diagnostics) > MaxProducerDiagnosticGroups {
return errors.New("producer diagnostics exceed maximum group count")
}
for index, diagnostic := range diagnostics {
if err := diagnostic.Validate(); err != nil {
return fmt.Errorf("producer diagnostic %d: %w", index, err)
@@ -179,15 +173,8 @@ func (group DiagnosticGroup) Validate() error {
)
}
// Validate checks the collection shape without imposing later global
// aggregation limits.
// Validate checks the collection shape and group identities.
func (collection DiagnosticCollection) Validate() error {
if collection.UnrepresentedOccurrenceCount < 0 {
return errors.New("diagnostic collection unrepresented occurrence count must not be negative")
}
if !collection.Truncated && collection.UnrepresentedOccurrenceCount != 0 {
return errors.New("diagnostic collection has unrepresented occurrences without truncation")
}
seen := make(map[diagnosticGroupKey]struct{}, len(collection.Groups))
for index, group := range collection.Groups {
if err := group.Validate(); err != nil {
@@ -203,7 +190,7 @@ func (collection DiagnosticCollection) Validate() error {
}
// ProjectDiagnosticCollection validates, partitions, and totals one finalized
// collection. Unrepresented occurrences belong to the non-warning projection.
// collection.
func ProjectDiagnosticCollection(collection DiagnosticCollection) (DiagnosticProjection, error) {
if err := collection.Validate(); err != nil {
return DiagnosticProjection{}, err
@@ -229,11 +216,6 @@ func ProjectDiagnosticCollection(collection DiagnosticCollection) (DiagnosticPro
projection.DiagnosticOccurrenceCount = count
projection.Diagnostics = append(projection.Diagnostics, cloneDiagnosticGroup(group))
}
count, err := addDiagnosticOccurrences(projection.DiagnosticOccurrenceCount, collection.UnrepresentedOccurrenceCount)
if err != nil {
return DiagnosticProjection{}, fmt.Errorf("diagnostic occurrences: %w", err)
}
projection.DiagnosticOccurrenceCount = count
return projection, nil
}

View File

@@ -1,6 +1,7 @@
package contracts
import (
"fmt"
"strings"
"testing"
)
@@ -75,19 +76,15 @@ func TestProducerDiagnosticValidationRejectsInvalidFieldsAndCounts(t *testing.T)
}
}
func TestValidateProducerDiagnosticsEnforcesLocalGroupBound(t *testing.T) {
diagnostics := make([]ProducerDiagnostic, MaxProducerDiagnosticGroups)
func TestValidateProducerDiagnosticsAcceptsManyGroups(t *testing.T) {
diagnostics := make([]ProducerDiagnostic, 400)
for index := range diagnostics {
diagnostics[index] = validProducerDiagnostic()
diagnostics[index].ReasonCode = "reason-" + string(rune('a'+index))
diagnostics[index].ReasonCode = fmt.Sprintf("reason-%d", index)
}
if err := ValidateProducerDiagnostics(diagnostics); err != nil {
t.Fatalf("ValidateProducerDiagnostics() error = %v", err)
}
diagnostics = append(diagnostics, validProducerDiagnostic())
if err := ValidateProducerDiagnostics(diagnostics); err == nil {
t.Fatal("ValidateProducerDiagnostics() error = nil, want excessive-group error")
}
}
func TestDiagnosticGroupValidationPreservesChunkIndexZero(t *testing.T) {
@@ -149,16 +146,12 @@ func TestCloneDiagnosticCollectionOwnsGroupsAndChunkIndex(t *testing.T) {
func TestProjectDiagnosticCollectionPartitionsAndChecksTotals(t *testing.T) {
warning := validDiagnosticGroup(DiagnosticDispositionWarning, DiagnosticCategoryFallback, "fallback", 2)
diagnostic := validDiagnosticGroup(DiagnosticDispositionAdvisory, DiagnosticCategoryDataQuality, "quality", 3)
collection := DiagnosticCollection{
Groups: []DiagnosticGroup{warning, diagnostic},
Truncated: true,
UnrepresentedOccurrenceCount: 4,
}
collection := DiagnosticCollection{Groups: []DiagnosticGroup{warning, diagnostic}}
projection, err := ProjectDiagnosticCollection(collection)
if err != nil {
t.Fatal(err)
}
if len(projection.Warnings) != 1 || len(projection.Diagnostics) != 1 || projection.WarningOccurrenceCount != 2 || projection.DiagnosticOccurrenceCount != 7 {
if len(projection.Warnings) != 1 || len(projection.Diagnostics) != 1 || projection.WarningOccurrenceCount != 2 || projection.DiagnosticOccurrenceCount != 3 {
t.Fatalf("projection = %#v, want partitioned exact totals", projection)
}
collection.Groups[0].Samples[0].Message = "mutated"

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,

View File

@@ -1,6 +1,8 @@
package pipeline
import (
"context"
"fmt"
"testing"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
@@ -17,6 +19,23 @@ func TestPromoteProducerDiagnosticsPreservesValidatorOrigin(t *testing.T) {
}
}
func TestRunnerRetainsManyDiagnosticGroups(t *testing.T) {
diagnostics := make([]contracts.ProducerDiagnostic, 0, 600)
for i := 0; i < 300; i++ {
warning := producerDiagnostic("scope", "warning")
warning.Disposition = contracts.DiagnosticDispositionWarning
warning.Category = contracts.DiagnosticCategoryDegradation
warning.ReasonCode = fmt.Sprintf("warning-%d", i)
advisory := producerDiagnostic("scope", "advisory")
advisory.ReasonCode = fmt.Sprintf("advisory-%d", i)
diagnostics = append(diagnostics, warning, advisory)
}
output, err := New().Run(context.Background(), RunInput{Prepared: preparedConcurrentPipeline(t, 1), RawInput: []byte("input"), Diagnostics: diagnostics})
if err != nil || len(output.Diagnostics.Groups) != len(diagnostics) {
t.Fatalf("Run() error = %v, groups = %d, want %d", err, len(output.Diagnostics.Groups), len(diagnostics))
}
}
func TestAppendDiagnosticGroupsRetainsOnlyStructuredDiagnostics(t *testing.T) {
output := RunOutput{}
appendDiagnosticGroups(&output, []contracts.DiagnosticGroup{{