From 5711f8b9e3f1c7c1abba27693daa162bb91baaf1 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Sat, 25 Jul 2026 22:00:32 +0000 Subject: [PATCH] Add D&D item event normalization --- .../dnd/normalize/itemevents/normalizer.go | 254 ++++++++++++++++++ .../normalize/itemevents/normalizer_test.go | 194 +++++++++++++ .../itemevents/invariants/validator.go | 131 +++++++++ .../itemevents/invariants/validator_test.go | 129 +++++++++ .../itemevents/source_refs/validator.go | 30 ++- .../itemevents/source_refs/validator_test.go | 8 + 6 files changed, 735 insertions(+), 11 deletions(-) create mode 100644 internal/modules/dnd/normalize/itemevents/normalizer.go create mode 100644 internal/modules/dnd/normalize/itemevents/normalizer_test.go create mode 100644 internal/modules/dnd/validate/itemevents/invariants/validator.go create mode 100644 internal/modules/dnd/validate/itemevents/invariants/validator_test.go diff --git a/internal/modules/dnd/normalize/itemevents/normalizer.go b/internal/modules/dnd/normalize/itemevents/normalizer.go new file mode 100644 index 0000000..930d324 --- /dev/null +++ b/internal/modules/dnd/normalize/itemevents/normalizer.go @@ -0,0 +1,254 @@ +// Package itemevents normalizes merged D&D item-event candidates. +package itemevents + +import ( + "context" + "fmt" + "sort" + + "gitea.maximumdirect.net/eric/notarius/internal/core/source" + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" + "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" + "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd" + itemeventmodel "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/itemevents" + "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared" + "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics" +) + +const ( + Key = "dnd/item-events" + normalizationPolicy = "dnd.item_events.normalize.v1" + NormalizationPolicy = normalizationPolicy + + ReasonCodeDisplayNormalized = "item_event_display_normalized" + ReasonCodeSourceRefsNormalized = "source_references_normalized" + ReasonCodeEventsReordered = "item_events_reordered" + ReasonCodeDuplicateCollapsed = "duplicate_item_event_collapsed" + ReasonCodeWarningsOmitted = "item_event_normalization_warnings_omitted" +) + +var requiredCapabilities = []string{"merged"} +var providedCapabilities = []string{"normalized"} + +var _ contracts.Normalizer[dnd.ItemEventList] = (*Normalizer)(nil) +var _ contracts.ManifestMetadataProvider = (*Normalizer)(nil) +var _ pipeline.CheckpointFingerprintProvider = (*Normalizer)(nil) + +// Options deliberately has no fields: item-event normalization has no +// generated-reference dependency or configurable semantic behavior. +type Options struct{} + +type Normalizer struct{} + +func New(Options) *Normalizer { return &Normalizer{} } + +func (n *Normalizer) Key() string { return Key } + +func (n *Normalizer) ReferenceSlots() []contracts.ReferenceSlot { return nil } + +func (n *Normalizer) ManifestMetadata() map[string]any { + if n == nil { + return nil + } + return map[string]any{"normalization_policy": normalizationPolicy} +} + +func (n *Normalizer) CheckpointFingerprints() []pipeline.CheckpointFingerprint { + if n == nil { + return nil + } + return []pipeline.CheckpointFingerprint{{Name: "normalization_policy", Value: normalizationPolicy}} +} + +func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalizeRequest[dnd.ItemEventList]) (contracts.TypedNormalizeResult[dnd.ItemEventList], error) { + if n == nil { + return contracts.TypedNormalizeResult[dnd.ItemEventList]{}, normalizerErrorf("normalizer must not be nil") + } + if ctx == nil { + return contracts.TypedNormalizeResult[dnd.ItemEventList]{}, normalizerErrorf("context must not be nil") + } + if err := ctx.Err(); err != nil { + return contracts.TypedNormalizeResult[dnd.ItemEventList]{}, normalizerErrorf("context error before normalize: %w", err) + } + + index := source.NewDocumentIndex(req.Source) + value, warnings := normalizeList(req.MergeOutput.Value, index, shared.NewSourceRefOrderFromIndex(index)) + return contracts.TypedNormalizeResult[dnd.ItemEventList]{Value: value, Warnings: warnings}, nil +} + +type normalizedRecord struct { + event dnd.ItemEvent + inputIndex int +} + +func normalizeList(input dnd.ItemEventList, index source.DocumentIndex, order shared.SourceRefOrder) (dnd.ItemEventList, []contracts.Warning) { + if input.Events == nil { + return dnd.ItemEventList{}, nil + } + + records := make([]normalizedRecord, len(input.Events)) + warnings := make([]contracts.Warning, 0) + for index, inputEvent := range input.Events { + event, changedFields, refsChanged := normalizeEvent(inputEvent, order) + records[index] = normalizedRecord{event: event, inputIndex: index} + if len(changedFields) != 0 { + warnings = append(warnings, contracts.Warning{ + Scope: eventScope(index), + ReasonCode: ReasonCodeDisplayNormalized, + Message: fmt.Sprintf("input index %d: normalized display whitespace in %s", index, + diagnostics.Aggregate("fields", changedFields)), + }) + } + if refsChanged { + warnings = append(warnings, contracts.Warning{ + Scope: eventScope(index), + ReasonCode: ReasonCodeSourceRefsNormalized, + Message: fmt.Sprintf("input index %d: source references normalized (original count %d, final count %d)", + index, len(inputEvent.SourceRefs), len(event.SourceRefs)), + }) + } + } + + sort.SliceStable(records, func(left, right int) bool { + return itemeventmodel.Less(order, records[left].event, records[right].event) + }) + for position, record := range records { + if position == record.inputIndex { + continue + } + warnings = append(warnings, contracts.Warning{ + Scope: eventScope(record.inputIndex), + ReasonCode: ReasonCodeEventsReordered, + Message: fmt.Sprintf("input index %d moved to normalized position %d", record.inputIndex, position), + }) + } + + output, duplicateWarnings := collapseDuplicates(records, index, order) + warnings = append(warnings, duplicateWarnings...) + return dnd.ItemEventList{Events: output}, diagnostics.LimitWarnings(warnings, "item_events", ReasonCodeWarningsOmitted) +} + +func normalizeEvent(input dnd.ItemEvent, order shared.SourceRefOrder) (dnd.ItemEvent, []string, bool) { + output := cloneEvent(input) + changedFields := make([]string, 0, 3) + for _, field := range []struct { + name string + value *string + }{ + {name: "name", value: &output.Name}, + {name: "from", value: &output.From}, + {name: "to", value: &output.To}, + } { + trimmed := itemeventmodel.DisplayValue(*field.value) + if *field.value != trimmed { + *field.value = trimmed + changedFields = append(changedFields, field.name) + } + } + output.SourceRefs = order.Canonicalize(input.SourceRefs) + return output, changedFields, !itemeventmodel.SourceRefsEqual(input.SourceRefs, output.SourceRefs) +} + +func cloneEvent(input dnd.ItemEvent) dnd.ItemEvent { + output := input + if input.Quantity != nil { + quantity := *input.Quantity + output.Quantity = &quantity + } + if input.SourceRefs != nil { + output.SourceRefs = append([]source.SourceRef(nil), input.SourceRefs...) + } + return output +} + +type duplicateGroup struct { + retainedIndex int + removed []int +} + +func collapseDuplicates(records []normalizedRecord, index source.DocumentIndex, order shared.SourceRefOrder) ([]dnd.ItemEvent, []contracts.Warning) { + if len(records) == 0 { + return make([]dnd.ItemEvent, 0), nil + } + + keep := make([]bool, len(records)) + groups := make([]duplicateGroup, 0) + groupByKey := make(map[string]int) + for recordIndex, record := range records { + if !itemeventmodel.ValidSourceRefs(index, record.event.SourceRefs) { + keep[recordIndex] = true + continue + } + key := itemeventmodel.ExactIdentity(order, record.event) + groupIndex, exists := groupByKey[key] + if !exists { + groupByKey[key] = len(groups) + groups = append(groups, duplicateGroup{retainedIndex: record.inputIndex}) + keep[recordIndex] = true + continue + } + groups[groupIndex].removed = append(groups[groupIndex].removed, record.inputIndex) + } + + output := make([]dnd.ItemEvent, 0, len(records)) + for recordIndex, record := range records { + if keep[recordIndex] { + output = append(output, cloneEvent(record.event)) + } + } + warnings := make([]contracts.Warning, 0) + for _, group := range groups { + if len(group.removed) != 0 { + warnings = append(warnings, duplicateWarning(group.retainedIndex, group.removed)) + } + } + return output, warnings +} + +func duplicateWarning(retainedIndex int, removed []int) contracts.Warning { + issues := make([]string, len(removed)) + for index, removedIndex := range removed { + issues[index] = fmt.Sprintf("removed input index %d", removedIndex) + } + return contracts.Warning{ + Scope: eventScope(retainedIndex), + ReasonCode: ReasonCodeDuplicateCollapsed, + Message: diagnostics.Aggregate( + fmt.Sprintf("duplicate item event collapsed; retained input index %d", retainedIndex), issues), + } +} + +func eventScope(index int) string { return fmt.Sprintf("events[%d]", index) } + +func ModuleSpec() pipeline.ModuleSpec { + return pipeline.ModuleSpec{ + Key: Key, + Stage: pipeline.StageNormalize, + Requires: append([]string(nil), requiredCapabilities...), + Provides: append([]string(nil), providedCapabilities...), + ArtifactKind: dnd.ItemEventListKind, + } +} + +func Register(registry *pipeline.NormalizerRegistry) error { + return pipeline.RegisterNormalizerBuilder(registry, ModuleSpec(), validateOptions, func(request pipeline.BuildRequest) (contracts.Normalizer[dnd.ItemEventList], error) { + options, err := DecodeOptions(request.Options) + if err != nil { + return nil, err + } + return New(options), nil + }) +} + +func DecodeOptions(options map[string]any) (Options, error) { + if err := pipeline.RejectUnknownOptions(options); err != nil { + return Options{}, normalizerErrorf("%w", err) + } + return Options{}, nil +} + +func validateOptions(options map[string]any) error { _, err := DecodeOptions(options); return err } + +func normalizerErrorf(format string, args ...any) error { + return fmt.Errorf("dnd item events normalizer: "+format, args...) +} diff --git a/internal/modules/dnd/normalize/itemevents/normalizer_test.go b/internal/modules/dnd/normalize/itemevents/normalizer_test.go new file mode 100644 index 0000000..671a2b7 --- /dev/null +++ b/internal/modules/dnd/normalize/itemevents/normalizer_test.go @@ -0,0 +1,194 @@ +package itemevents + +import ( + "context" + "reflect" + "testing" + + "gitea.maximumdirect.net/eric/notarius/internal/core/source" + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" + "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" + "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd" + "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics" +) + +func TestNormalizeTrimsCanonicalizesAndClones(t *testing.T) { + doc := &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 30}, {ID: 10}}} + quantity := 4 + input := dnd.ItemEventList{Events: []dnd.ItemEvent{{ + Name: " Gold Pieces ", Kind: dnd.ItemEventKindTransferred, Quantity: &quantity, From: " Borin ", To: " Aria ", + SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 10, EndUnitID: 10}, {SourceID: doc.ID, StartUnitID: 30, EndUnitID: 30}, {SourceID: doc.ID, StartUnitID: 30, EndUnitID: 30}}, + }}} + result := normalize(t, doc, input) + got := result.Value.Events[0] + if got.Name != "Gold Pieces" || got.From != "Borin" || got.To != "Aria" || got.Quantity == input.Events[0].Quantity || + !reflect.DeepEqual(got.SourceRefs, []source.SourceRef{{SourceID: doc.ID, StartUnitID: 30, EndUnitID: 30}, {SourceID: doc.ID, StartUnitID: 10, EndUnitID: 10}}) { + t.Fatalf("normalized event = %#v", got) + } + if !hasWarning(result.Warnings, ReasonCodeDisplayNormalized) || !hasWarning(result.Warnings, ReasonCodeSourceRefsNormalized) { + t.Fatalf("warnings = %#v", result.Warnings) + } + second := normalize(t, doc, result.Value) + if !reflect.DeepEqual(second.Value, result.Value) || len(second.Warnings) != 0 { + t.Fatalf("second normalization = %#v; want idempotent output without warnings", second) + } + *got.Quantity = 99 + got.SourceRefs[0].StartUnitID = 999 + if quantity != 4 || input.Events[0].SourceRefs[1].StartUnitID != 30 { + t.Fatalf("Normalize() aliased input: %#v", input) + } +} + +func TestNormalizeOrdersAndCollapsesExactDuplicatesOnly(t *testing.T) { + doc := &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 30}, {ID: 10}, {ID: 20}}} + ref := func(unit int) source.SourceRef { + return source.SourceRef{SourceID: doc.ID, StartUnitID: unit, EndUnitID: unit} + } + quantity := 2 + duplicate := dnd.ItemEvent{Name: " Potion ", Kind: dnd.ItemEventKindAcquired, Quantity: &quantity, To: " Aria ", SourceRefs: []source.SourceRef{ref(30)}} + input := dnd.ItemEventList{Events: []dnd.ItemEvent{ + {Name: "Later", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{ref(20)}}, + duplicate, + {Name: "Potion", Kind: dnd.ItemEventKindAcquired, Quantity: &quantity, To: "Aria", SourceRefs: []source.SourceRef{ref(30)}}, + {Name: "Potion", Kind: dnd.ItemEventKindAcquired, Quantity: &quantity, To: "Borin", SourceRefs: []source.SourceRef{ref(30)}}, + {Name: "Potion", Kind: dnd.ItemEventKindAcquired, Quantity: &quantity, To: "Aria", SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 999, EndUnitID: 999}}}, + }} + result := normalize(t, doc, input) + got := result.Value.Events + if len(got) != 4 || got[0].Name != "Potion" || got[0].To != "Aria" || got[1].To != "Borin" || got[2].Name != "Later" || got[3].SourceRefs[0].StartUnitID != 999 { + t.Fatalf("normalized events = %#v", got) + } + if !hasWarning(result.Warnings, ReasonCodeEventsReordered) || !hasWarning(result.Warnings, ReasonCodeDuplicateCollapsed) { + t.Fatalf("warnings = %#v", result.Warnings) + } +} + +func TestNormalizeUsesEveryCanonicalTieBreaker(t *testing.T) { + doc := testDocument() + ref := func(unit int) source.SourceRef { + return source.SourceRef{SourceID: doc.ID, StartUnitID: unit, EndUnitID: unit} + } + quantity := 1 + tests := []struct { + name string + earlier dnd.ItemEvent + later dnd.ItemEvent + }{ + {"evidence position", dnd.ItemEvent{Name: "Z", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{ref(10)}}, dnd.ItemEvent{Name: "A", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{ref(20)}}}, + {"normalized name", dnd.ItemEvent{Name: "A", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{ref(10)}}, dnd.ItemEvent{Name: "B", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{ref(10)}}}, + {"exact name", dnd.ItemEvent{Name: "A", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{ref(10)}}, dnd.ItemEvent{Name: "a", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{ref(10)}}}, + {"kind", dnd.ItemEvent{Name: "Item", Kind: dnd.ItemEventKindAcquired, To: "Aria", SourceRefs: []source.SourceRef{ref(10)}}, dnd.ItemEvent{Name: "Item", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{ref(10)}}}, + {"from presence", dnd.ItemEvent{Name: "Item", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{ref(10)}}, dnd.ItemEvent{Name: "Item", Kind: dnd.ItemEventKindDiscovered, From: "Aria", SourceRefs: []source.SourceRef{ref(10)}}}, + {"from comparison", dnd.ItemEvent{Name: "Item", Kind: dnd.ItemEventKindTransferred, From: "A", To: "Z", SourceRefs: []source.SourceRef{ref(10)}}, dnd.ItemEvent{Name: "Item", Kind: dnd.ItemEventKindTransferred, From: "B", To: "Z", SourceRefs: []source.SourceRef{ref(10)}}}, + {"exact from", dnd.ItemEvent{Name: "Item", Kind: dnd.ItemEventKindTransferred, From: "A", To: "Z", SourceRefs: []source.SourceRef{ref(10)}}, dnd.ItemEvent{Name: "Item", Kind: dnd.ItemEventKindTransferred, From: "a", To: "Z", SourceRefs: []source.SourceRef{ref(10)}}}, + {"to presence", dnd.ItemEvent{Name: "Item", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{ref(10)}}, dnd.ItemEvent{Name: "Item", Kind: dnd.ItemEventKindDiscovered, To: "Aria", SourceRefs: []source.SourceRef{ref(10)}}}, + {"to comparison", dnd.ItemEvent{Name: "Item", Kind: dnd.ItemEventKindTransferred, From: "A", To: "A", SourceRefs: []source.SourceRef{ref(10)}}, dnd.ItemEvent{Name: "Item", Kind: dnd.ItemEventKindTransferred, From: "A", To: "B", SourceRefs: []source.SourceRef{ref(10)}}}, + {"exact to", dnd.ItemEvent{Name: "Item", Kind: dnd.ItemEventKindTransferred, From: "A", To: "A", SourceRefs: []source.SourceRef{ref(10)}}, dnd.ItemEvent{Name: "Item", Kind: dnd.ItemEventKindTransferred, From: "A", To: "a", SourceRefs: []source.SourceRef{ref(10)}}}, + {"quantity", dnd.ItemEvent{Name: "Item", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{ref(10)}}, dnd.ItemEvent{Name: "Item", Kind: dnd.ItemEventKindDiscovered, Quantity: &quantity, SourceRefs: []source.SourceRef{ref(10)}}}, + {"reference sequence", dnd.ItemEvent{Name: "Item", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{ref(10)}}, dnd.ItemEvent{Name: "Item", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{ref(10), ref(20)}}}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + result := normalize(t, doc, dnd.ItemEventList{Events: []dnd.ItemEvent{test.later, test.earlier}}) + if len(result.Value.Events) != 2 || !reflect.DeepEqual(result.Value.Events[0], test.earlier) || !reflect.DeepEqual(result.Value.Events[1], test.later) { + t.Fatalf("normalized events = %#v; want %#v then %#v", result.Value.Events, test.earlier, test.later) + } + }) + } +} + +func TestNormalizePreservesEmptyAndMalformedCandidates(t *testing.T) { + for _, input := range []dnd.ItemEventList{{}, {Events: []dnd.ItemEvent{}}} { + result := normalize(t, nil, input) + if (result.Value.Events == nil) != (input.Events == nil) { + t.Fatalf("empty representation changed: %#v", result.Value) + } + } + malformed := dnd.ItemEventList{Events: []dnd.ItemEvent{ + {Name: " Coin ", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 99, EndUnitID: 99}}}, + {Name: "Coin", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 99, EndUnitID: 99}}}, + }} + result := normalize(t, testDocument(), malformed) + if len(result.Value.Events) != 2 || result.Value.Events[0].Name != "Coin" || result.Value.Events[1].Name != "Coin" { + t.Fatalf("malformed candidates were not safely retained: %#v", result.Value) + } +} + +func TestNormalizeDoesNotInferOrConvertInventoryMeaning(t *testing.T) { + doc := testDocument() + quantity := 25 + input := dnd.ItemEventList{Events: []dnd.ItemEvent{ + {Name: " gold pieces ", Kind: dnd.ItemEventKindDiscovered, Quantity: &quantity, SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 10, EndUnitID: 10}}}, + {Name: " gp ", Kind: dnd.ItemEventKindAcquired, Quantity: &quantity, To: " party ", SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 20, EndUnitID: 20}}}, + {Name: " potion of healing ", Kind: dnd.ItemEventKindTransferred, Quantity: &quantity, From: " Aria ", To: " Borin ", SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 30, EndUnitID: 30}}}, + }} + result := normalize(t, doc, input) + got := make(map[string]dnd.ItemEvent, len(result.Value.Events)) + for _, event := range result.Value.Events { + got[event.Name] = event + } + if event := got["gold pieces"]; event.Kind != dnd.ItemEventKindDiscovered || event.Quantity == nil || *event.Quantity != 25 { + t.Fatalf("discovery event was inferred or converted: %#v", event) + } + if event := got["gp"]; event.Kind != dnd.ItemEventKindAcquired || event.Quantity == nil || *event.Quantity != 25 || event.To != "party" { + t.Fatalf("acquisition event was inferred or denomination-converted: %#v", event) + } + if event := got["potion of healing"]; event.Kind != dnd.ItemEventKindTransferred || event.From != "Aria" || event.To != "Borin" || event.Quantity == nil || *event.Quantity != 25 { + t.Fatalf("transfer event was inferred, aliased, singularized, or ledger-adjusted: %#v", event) + } +} + +func TestNormalizerContractAndBoundedWarnings(t *testing.T) { + normalizer := New(Options{}) + if spec := ModuleSpec(); spec.Key != Key || spec.Stage != pipeline.StageNormalize || spec.ArtifactKind != dnd.ItemEventListKind || len(spec.ReferenceSlots) != 0 || + !reflect.DeepEqual(spec.Requires, []string{"merged"}) || !reflect.DeepEqual(spec.Provides, []string{"normalized"}) { + t.Fatalf("ModuleSpec() = %#v", spec) + } + if metadata := normalizer.ManifestMetadata(); !reflect.DeepEqual(metadata, map[string]any{"normalization_policy": normalizationPolicy}) { + t.Fatalf("ManifestMetadata() = %#v", metadata) + } + if fingerprints := normalizer.CheckpointFingerprints(); !reflect.DeepEqual(fingerprints, []pipeline.CheckpointFingerprint{{Name: "normalization_policy", Value: normalizationPolicy}}) { + t.Fatalf("CheckpointFingerprints() = %#v", fingerprints) + } + registry := pipeline.NewNormalizerRegistry() + if err := Register(registry); err != nil { + t.Fatalf("Register() error = %v", err) + } + if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil { + t.Fatal("DecodeOptions() accepted unknown option") + } + + count := diagnostics.MaxWarnings + 5 + doc := &source.SourceDocument{ID: "session", Units: make([]source.SourceUnit, count)} + input := dnd.ItemEventList{Events: make([]dnd.ItemEvent, count)} + for index := range input.Events { + doc.Units[index].ID = index + 1 + input.Events[index] = dnd.ItemEvent{Name: " Coin ", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: count - index, EndUnitID: count - index}}} + } + result := normalize(t, doc, input) + if len(result.Warnings) != diagnostics.MaxWarnings || result.Warnings[len(result.Warnings)-1].ReasonCode != ReasonCodeWarningsOmitted { + t.Fatalf("warnings = %#v", result.Warnings) + } +} + +func normalize(t *testing.T, doc *source.SourceDocument, input dnd.ItemEventList) contracts.TypedNormalizeResult[dnd.ItemEventList] { + t.Helper() + result, err := New(Options{}).Normalize(context.Background(), contracts.TypedNormalizeRequest[dnd.ItemEventList]{Source: doc, MergeOutput: contracts.MergeArtifact[dnd.ItemEventList]{Value: input}}) + if err != nil { + t.Fatalf("Normalize() error = %v", err) + } + return result +} + +func testDocument() *source.SourceDocument { + return &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 10}, {ID: 20}, {ID: 30}}} +} + +func hasWarning(warnings []contracts.Warning, reason string) bool { + for _, warning := range warnings { + if warning.ReasonCode == reason { + return true + } + } + return false +} diff --git a/internal/modules/dnd/validate/itemevents/invariants/validator.go b/internal/modules/dnd/validate/itemevents/invariants/validator.go new file mode 100644 index 0000000..f66e759 --- /dev/null +++ b/internal/modules/dnd/validate/itemevents/invariants/validator.go @@ -0,0 +1,131 @@ +// Package invariants validates normalized D&D item-event artifacts. +package invariants + +import ( + "context" + "fmt" + + "gitea.maximumdirect.net/eric/notarius/internal/core/source" + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" + "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" + "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd" + itemeventmodel "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/itemevents" + "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared" + "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics" + itemeventshape "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/itemevents/shape" +) + +const ( + Key = "normalize/dnd/item-events/invariants" + ReasonCode = "invalid_normalized_item_event_invariants" + policy = "dnd.item_events.normalize_invariants.v1" +) + +type Options struct{} +type Validator struct{} + +var _ contracts.TypedValidator[dnd.ItemEventList] = (*Validator)(nil) +var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil) + +func New(Options) *Validator { return &Validator{} } +func (v *Validator) Name() string { return Key } +func (v *Validator) ExecutionClass() contracts.ExecutionClass { + return contracts.ExecutionClassDeterministic +} +func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint { + return []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}} +} + +func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.ItemEventList]) (contracts.ValidationResult, error) { + if err := Validate(req.Source, req.Value); err != nil { + return contracts.ValidationResult{Approved: false, ReasonCode: ReasonCode, Message: err.Error()}, nil + } + return contracts.ValidationResult{Approved: true}, nil +} + +// Validate checks only invariants introduced by item-event normalization. +// Shape and source-reference failures remain owned by their earlier validators. +func Validate(doc *source.SourceDocument, value dnd.ItemEventList) error { + if itemeventshape.Validate(value) != nil { + return nil + } + index := source.NewDocumentIndex(doc) + if !sourceRefsValid(index, value) { + return nil + } + issues := issuesFor(shared.NewSourceRefOrderFromIndex(index), value) + if len(issues) == 0 { + return nil + } + return fmt.Errorf("%s", diagnostics.Aggregate("invalid normalized item event invariants", issues)) +} + +func issuesFor(order shared.SourceRefOrder, value dnd.ItemEventList) []string { + issues := make([]string, 0) + seenIdentity := make(map[string]int) + for eventIndex, event := range value.Events { + prefix := fmt.Sprintf("events[%d]", eventIndex) + if event.Name != itemeventmodel.DisplayValue(event.Name) { + issues = append(issues, prefix+".name is not display-normalized") + } + if event.From != itemeventmodel.DisplayValue(event.From) { + issues = append(issues, prefix+".from is not display-normalized") + } + if event.To != itemeventmodel.DisplayValue(event.To) { + issues = append(issues, prefix+".to is not display-normalized") + } + for refIndex := 1; refIndex < len(event.SourceRefs); refIndex++ { + previous := event.SourceRefs[refIndex-1] + current := event.SourceRefs[refIndex] + if order.Less(current, previous) { + issues = append(issues, fmt.Sprintf("%s.source_refs are not in canonical order at index %d", prefix, refIndex)) + } else if current == previous { + issues = append(issues, fmt.Sprintf("%s.source_refs[%d] duplicates the previous reference", prefix, refIndex)) + } + } + key := itemeventmodel.ExactIdentity(order, event) + if previous, exists := seenIdentity[key]; exists { + issues = append(issues, fmt.Sprintf("%s duplicates item event %d under normalized identity", prefix, previous)) + } else { + seenIdentity[key] = eventIndex + } + } + for eventIndex := 1; eventIndex < len(value.Events); eventIndex++ { + if itemeventmodel.Less(order, value.Events[eventIndex], value.Events[eventIndex-1]) { + issues = append(issues, fmt.Sprintf("events[%d] is out of canonical order", eventIndex)) + } + } + return issues +} + +func sourceRefsValid(index source.DocumentIndex, value dnd.ItemEventList) bool { + for _, event := range value.Events { + if !itemeventmodel.ValidSourceRefs(index, event.SourceRefs) { + return false + } + } + return true +} + +func Spec() pipeline.ValidatorSpec { + return pipeline.ValidatorSpec{Key: Key, ExecutionClass: contracts.ExecutionClassDeterministic} +} + +func Register(registry *pipeline.ValidatorRegistry) error { + return pipeline.RegisterTypedValidatorBuilder(registry, dnd.ItemEventListKind, Spec(), validateOptions, func(request pipeline.BuildRequest) (contracts.TypedValidator[dnd.ItemEventList], error) { + options, err := DecodeOptions(request.Options) + if err != nil { + return nil, err + } + return New(options), nil + }) +} + +func DecodeOptions(options map[string]any) (Options, error) { + if err := pipeline.RejectUnknownOptions(options); err != nil { + return Options{}, err + } + return Options{}, nil +} + +func validateOptions(options map[string]any) error { _, err := DecodeOptions(options); return err } diff --git a/internal/modules/dnd/validate/itemevents/invariants/validator_test.go b/internal/modules/dnd/validate/itemevents/invariants/validator_test.go new file mode 100644 index 0000000..01a9b9f --- /dev/null +++ b/internal/modules/dnd/validate/itemevents/invariants/validator_test.go @@ -0,0 +1,129 @@ +package invariants + +import ( + "context" + "reflect" + "strings" + "testing" + "unicode/utf8" + + "gitea.maximumdirect.net/eric/notarius/internal/core/source" + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" + "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" + "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd" + normalizeitemevents "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/normalize/itemevents" +) + +func TestValidatorApprovesNormalizerOutput(t *testing.T) { + doc := invariantDocument() + input := dnd.ItemEventList{Events: []dnd.ItemEvent{ + {Name: " Coin ", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 20, EndUnitID: 20}}}, + {Name: "Arrow", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 10, EndUnitID: 10}}}, + }} + normalized, err := normalizeitemevents.New(normalizeitemevents.Options{}).Normalize(context.Background(), contracts.TypedNormalizeRequest[dnd.ItemEventList]{Source: doc, MergeOutput: contracts.MergeArtifact[dnd.ItemEventList]{Value: input}}) + if err != nil { + t.Fatal(err) + } + result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.ItemEventList]{Source: doc, Value: normalized.Value}) + if err != nil || !result.Approved { + t.Fatalf("Validate() = %#v, %v; want approval", result, err) + } +} + +func TestValidateRejectsOwnedNormalizedInvariants(t *testing.T) { + tests := []struct { + name string + mutate func(*dnd.ItemEventList) + want string + }{ + {name: "name whitespace", mutate: func(value *dnd.ItemEventList) { value.Events[0].Name = " Coin " }, want: ".name is not display-normalized"}, + {name: "from whitespace", mutate: func(value *dnd.ItemEventList) { + value.Events[0].Kind = dnd.ItemEventKindLost + value.Events[0].From = " Aria " + }, want: ".from is not display-normalized"}, + {name: "to whitespace", mutate: func(value *dnd.ItemEventList) { + value.Events[0].Kind = dnd.ItemEventKindAcquired + value.Events[0].To = " Aria " + }, want: ".to is not display-normalized"}, + {name: "reference order", mutate: func(value *dnd.ItemEventList) { + value.Events[0].SourceRefs = []source.SourceRef{{SourceID: "session", StartUnitID: 20, EndUnitID: 20}, {SourceID: "session", StartUnitID: 10, EndUnitID: 10}} + }, want: "not in canonical order"}, + {name: "duplicate reference", mutate: func(value *dnd.ItemEventList) { + value.Events[0].SourceRefs = append(value.Events[0].SourceRefs, value.Events[0].SourceRefs[0]) + }, want: "duplicates the previous reference"}, + {name: "list order", mutate: func(value *dnd.ItemEventList) { value.Events = []dnd.ItemEvent{value.Events[1], value.Events[0]} }, want: "out of canonical order"}, + {name: "duplicate event", mutate: func(value *dnd.ItemEventList) { value.Events = append(value.Events, value.Events[0]) }, want: "duplicates item event"}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + value := normalizedList() + test.mutate(&value) + err := Validate(invariantDocument(), value) + if err == nil || !strings.Contains(err.Error(), test.want) { + t.Fatalf("Validate() error = %v, want %q", err, test.want) + } + result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.ItemEventList]{Source: invariantDocument(), Value: value}) + if err != nil || result.Approved || result.ReasonCode != ReasonCode { + t.Fatalf("validator result = %#v, %v", result, err) + } + }) + } +} + +func TestValidateUsesSourceDocumentOrderAndDefersEarlierFailures(t *testing.T) { + doc := &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 30}, {ID: 10}}} + value := dnd.ItemEventList{Events: []dnd.ItemEvent{{ + Name: "Coin", Kind: dnd.ItemEventKindDiscovered, + SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 10, EndUnitID: 10}, {SourceID: doc.ID, StartUnitID: 30, EndUnitID: 30}}, + }}} + if err := Validate(doc, value); err == nil || !strings.Contains(err.Error(), "not in canonical order") { + t.Fatalf("Validate() error = %v, want document-order rejection", err) + } + shapeInvalid := normalizedList() + shapeInvalid.Events[0].Name = " " + if err := Validate(invariantDocument(), shapeInvalid); err != nil { + t.Fatalf("shape failure must be deferred, got %v", err) + } + sourceInvalid := normalizedList() + sourceInvalid.Events[0].SourceRefs[0].StartUnitID = 999 + if err := Validate(invariantDocument(), sourceInvalid); err != nil { + t.Fatalf("source-reference failure must be deferred, got %v", err) + } +} + +func TestValidatorContractAndBoundedDiagnostics(t *testing.T) { + value := normalizedList() + value.Events = make([]dnd.ItemEvent, 30) + for index := range value.Events { + value.Events[index] = normalizedList().Events[0] + value.Events[index].Name = strings.Repeat("火", 300) + } + result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.ItemEventList]{Source: invariantDocument(), Value: value}) + if err != nil || result.Approved || !utf8.ValidString(result.Message) || len([]byte(result.Message)) > 4096 || !strings.Contains(result.Message, "additional issue(s) omitted") { + t.Fatalf("bounded result = %#v, %v", result, err) + } + if got := New(Options{}).CheckpointFingerprints(); !reflect.DeepEqual(got, []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}) { + t.Fatalf("CheckpointFingerprints() = %#v", got) + } + if spec := Spec(); spec.Key != Key || spec.ExecutionClass != contracts.ExecutionClassDeterministic { + t.Fatalf("Spec() = %#v", spec) + } + registry := pipeline.NewValidatorRegistry() + if err := Register(registry); err != nil { + t.Fatalf("Register() error = %v", err) + } + if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil { + t.Fatal("DecodeOptions() accepted unknown option") + } +} + +func normalizedList() dnd.ItemEventList { + return dnd.ItemEventList{Events: []dnd.ItemEvent{ + {Name: "Arrow", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 10, EndUnitID: 10}}}, + {Name: "Coin", Kind: dnd.ItemEventKindDiscovered, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 20, EndUnitID: 20}}}, + }} +} + +func invariantDocument() *source.SourceDocument { + return &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 10}, {ID: 20}}} +} diff --git a/internal/modules/dnd/validate/itemevents/source_refs/validator.go b/internal/modules/dnd/validate/itemevents/source_refs/validator.go index b3f427d..2b2903d 100644 --- a/internal/modules/dnd/validate/itemevents/source_refs/validator.go +++ b/internal/modules/dnd/validate/itemevents/source_refs/validator.go @@ -46,7 +46,7 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq issues := make([]string, 0) for eventIndex, event := range req.Value.Events { if itemevents.ValidSourceRefs(index, event.SourceRefs) { - if req.Stage != string(pipeline.StageExtract) || refsFitChunk(req.Chunk, event.SourceRefs) { + if req.Stage != string(pipeline.StageExtract) || refsFitChunk(req.Source, req.Chunk, event.SourceRefs) { continue } } @@ -55,7 +55,7 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq issues = append(issues, fmt.Sprintf("events[%d].source_refs[%d]: %s", eventIndex, refIndex, diagnostics.Truncate(err.Error()))) continue } - if req.Stage == string(pipeline.StageExtract) && !chunkContainsRef(req.Chunk, ref) { + if req.Stage == string(pipeline.StageExtract) && !chunkContainsRef(req.Source, req.Chunk, ref) { issues = append(issues, fmt.Sprintf("events[%d].source_refs[%d]: source reference is outside the current extraction chunk", eventIndex, refIndex)) } } @@ -66,26 +66,34 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq return contracts.ValidationResult{Approved: false, ReasonCode: ReasonCode, Message: diagnostics.Aggregate("invalid item event source references", issues)}, nil } -func refsFitChunk(chunk *source.Chunk, refs []source.SourceRef) bool { +func refsFitChunk(doc *source.SourceDocument, chunk *source.Chunk, refs []source.SourceRef) bool { for _, ref := range refs { - if !chunkContainsRef(chunk, ref) { + if !chunkContainsRef(doc, chunk, ref) { return false } } return true } -func chunkContainsRef(chunk *source.Chunk, ref source.SourceRef) bool { +func chunkContainsRef(doc *source.SourceDocument, chunk *source.Chunk, ref source.SourceRef) bool { if chunk == nil || ref.SourceID != chunk.SourceID { return false } - startFound := false - endFound := false - for _, unit := range chunk.Units { - startFound = startFound || unit.ID == ref.StartUnitID - endFound = endFound || unit.ID == ref.EndUnitID + start, startOK := source.UnitIndex(doc, ref.StartUnitID) + end, endOK := source.UnitIndex(doc, ref.EndUnitID) + if !startOK || !endOK || start > end { + return false } - return startFound && endFound + units := make(map[int]struct{}, len(chunk.Units)) + for _, unit := range chunk.Units { + units[unit.ID] = struct{}{} + } + for position := start; position <= end; position++ { + if _, found := units[doc.Units[position].ID]; !found { + return false + } + } + return true } func Spec() pipeline.ValidatorSpec { diff --git a/internal/modules/dnd/validate/itemevents/source_refs/validator_test.go b/internal/modules/dnd/validate/itemevents/source_refs/validator_test.go index f6439f4..7435b0b 100644 --- a/internal/modules/dnd/validate/itemevents/source_refs/validator_test.go +++ b/internal/modules/dnd/validate/itemevents/source_refs/validator_test.go @@ -51,6 +51,14 @@ func TestValidatorEnforcesChunkOnlyDuringExtraction(t *testing.T) { t.Fatalf("out-of-chunk evidence = %#v, %v", result, err) } + noncontiguousChunk := &source.Chunk{ID: "chunk-1", SourceID: doc.ID, Units: []source.SourceUnit{doc.Units[0], doc.Units[2]}} + spansMissingUnit := validList() + spansMissingUnit.Events[0].SourceRefs = []source.SourceRef{{SourceID: doc.ID, StartUnitID: 1, EndUnitID: 3}} + result, err = New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.ItemEventList]{Stage: string(pipeline.StageExtract), Source: doc, Chunk: noncontiguousChunk, Value: spansMissingUnit}) + if err != nil || result.Approved || !strings.Contains(result.Message, "outside the current extraction chunk") { + t.Fatalf("partially contained evidence = %#v, %v", result, err) + } + multiRange := validList() multiRange.Events[0].SourceRefs = []source.SourceRef{{SourceID: doc.ID, StartUnitID: 1, EndUnitID: 2}, {SourceID: doc.ID, StartUnitID: 3, EndUnitID: 4}} result, err = New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.ItemEventList]{Source: doc, Value: multiRange})