// Package locationoccurrences normalizes merged D&D location occurrence candidates. package locationoccurrences import ( "context" "fmt" "sort" "strconv" "strings" "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" locationregistry "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/locations/registry" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics" ) const ( Key = "dnd/location-occurrences" normalizationPolicy = "dnd.location_occurrences.normalize.v1" NormalizationPolicy = normalizationPolicy ReasonCodeNameCanonicalized = "location_occurrence_name_canonicalized" ReasonCodeUnknownLocationID = "location_occurrence_unknown_location_id" ReasonCodeSourceRefsNormalized = "source_references_normalized" ReasonCodeOccurrencesReordered = "location_occurrences_reordered" ReasonCodeDuplicateCollapsed = "duplicate_location_occurrence_collapsed" ) const ( LocationRegistryReferenceSlot = locationregistry.ReferenceSlot LocationRegistryMaxBytes = locationregistry.MaxBytes ) var requiredCapabilities = []string{"merged"} var providedCapabilities = []string{"normalized"} var _ contracts.Normalizer[dnd.LocationOccurrenceList] = (*Normalizer)(nil) type Options struct{} type Normalizer struct { locationResolver *locationregistry.Resolver } func New(_ Options, references ...contracts.ReferenceSet) (*Normalizer, error) { if len(references) > 1 { return nil, normalizerErrorf("at most one reference set may be supplied") } var referenceSet contracts.ReferenceSet if len(references) == 1 { referenceSet = references[0] } resolver, err := locationregistry.NewResolver(referenceSet) if err != nil { return nil, normalizerErrorf("prepare location registry: %w", err) } return &Normalizer{locationResolver: resolver}, nil } func (n *Normalizer) Key() string { return Key } func (n *Normalizer) ReferenceSlots() []contracts.ReferenceSlot { return referenceSlots() } func (n *Normalizer) PrepareNormalize(ctx context.Context, req contracts.NormalizeInput[dnd.LocationOccurrenceList]) (contracts.PreparedOperation[dnd.LocationOccurrenceList], error) { if n == nil || n.locationResolver == nil { return nil, normalizerErrorf("normalizer must not be nil") } if ctx == nil { return nil, normalizerErrorf("context must not be nil") } if err := ctx.Err(); err != nil { return nil, normalizerErrorf("context error before normalize: %w", err) } registry, err := n.locationResolver.Resolve(req.References) if err != nil { return nil, normalizerErrorf("resolve location registry: %w", err) } if !registry.Bound() { return nil, normalizerErrorf("location registry reference is required") } index := source.NewDocumentIndex(req.Source) value, findings := normalizeList(req.MergeOutput.Value, index, shared.NewSourceRefOrderFromIndex(index), registry) diagnosticGroups, err := diagnostics.NormalizationDiagnostics(findings, ReasonCodeUnknownLocationID) if err != nil { return nil, normalizerErrorf("collect diagnostics: %w", err) } return contracts.DeterministicOperation[dnd.LocationOccurrenceList]{Evaluation: contracts.CandidateEvaluation[dnd.LocationOccurrenceList]{Result: contracts.EvaluationView[dnd.LocationOccurrenceList]{Candidate: &value, Diagnostics: diagnosticGroups}}}, nil } type normalizedRecord struct { occurrence dnd.LocationOccurrence inputIndex int earliest int hasEvidence bool kindRank int } type nameCanonicalization struct{ from, to string } func normalizeList(input dnd.LocationOccurrenceList, documentIndex source.DocumentIndex, order shared.SourceRefOrder, registry *locationregistry.Registry) (dnd.LocationOccurrenceList, []diagnostics.Finding) { if input.Occurrences == nil { return dnd.LocationOccurrenceList{}, nil } records := make([]normalizedRecord, len(input.Occurrences)) findings := make([]diagnostics.Finding, 0) for index, inputOccurrence := range input.Occurrences { occurrence, change, found, refsChanged := normalizeOccurrence(inputOccurrence, order, registry) earliest, hasEvidence := order.EarliestValid(occurrence.SourceRefs) records[index] = normalizedRecord{occurrence: occurrence, inputIndex: index, earliest: earliest, hasEvidence: hasEvidence, kindRank: kindOrder(occurrence.Kind)} if change != nil { findings = append(findings, diagnostics.Finding{Scope: occurrenceScope(index), ReasonCode: ReasonCodeNameCanonicalized, Message: fmt.Sprintf("input index %d: location name canonicalized from %s to %s", index, diagnostics.Quote(change.from), diagnostics.Quote(change.to))}) } if !found { findings = append(findings, diagnostics.Finding{Scope: occurrenceScope(index), ReasonCode: ReasonCodeUnknownLocationID, Message: fmt.Sprintf("input index %d: location ID %s is not in the supplied registry", index, diagnostics.Quote(inputOccurrence.LocationID))}) } if refsChanged { 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)", index, len(inputOccurrence.SourceRefs), len(occurrence.SourceRefs))}) } } sort.SliceStable(records, func(left, right int) bool { l, r := records[left], records[right] if l.hasEvidence != r.hasEvidence { return l.hasEvidence } if l.hasEvidence && l.earliest != r.earliest { return l.earliest < r.earliest } return lessOccurrenceTies(order, l.occurrence, r.occurrence, l.kindRank, r.kindRank) }) for position, record := range records { if position != record.inputIndex { findings = append(findings, diagnostics.Finding{Scope: occurrenceScope(record.inputIndex), ReasonCode: ReasonCodeOccurrencesReordered, Message: fmt.Sprintf("input index %d moved to normalized position %d by canonical occurrence order", record.inputIndex, position)}) } } output, duplicateFindings := collapseDuplicates(records, documentIndex) findings = append(findings, duplicateFindings...) return dnd.LocationOccurrenceList{Occurrences: output}, findings } func normalizeOccurrence(input dnd.LocationOccurrence, order shared.SourceRefOrder, registry *locationregistry.Registry) (dnd.LocationOccurrence, *nameCanonicalization, bool, bool) { output := cloneOccurrence(input) canonical, found := registry.Lookup(input.LocationID) if found { output.Name = canonical.Name } var change *nameCanonicalization if input.Name != output.Name { change = &nameCanonicalization{from: input.Name, to: output.Name} } output.SourceRefs = order.Canonicalize(input.SourceRefs) return output, change, found, !sourceRefsEqual(input.SourceRefs, output.SourceRefs) } func cloneOccurrence(input dnd.LocationOccurrence) dnd.LocationOccurrence { input.SourceRefs = append([]source.SourceRef(nil), input.SourceRefs...) return input } func sourceRefsEqual(left, right []source.SourceRef) bool { if (left == nil) != (right == nil) || len(left) != len(right) { return false } for index := range left { if left[index] != right[index] { return false } } return true } func collapseDuplicates(records []normalizedRecord, documentIndex source.DocumentIndex) ([]dnd.LocationOccurrence, []diagnostics.Finding) { // This family owns eligibility and maps shared positions to original indices. retained, groups := shared.GroupDuplicates(len(records), func(index int) (string, bool) { occurrence := records[index].occurrence if !validSourceRefs(documentIndex, occurrence.SourceRefs) { return "", false } return exactIdentity(occurrence), true }) output := make([]dnd.LocationOccurrence, 0, len(retained)) for _, index := range retained { output = append(output, cloneOccurrence(records[index].occurrence)) } findings := make([]diagnostics.Finding, 0, len(groups)) for _, group := range groups { removed := make([]int, len(group.Removed)) for index, position := range group.Removed { removed[index] = records[position].inputIndex } findings = append(findings, duplicateFinding(records[group.Representative].inputIndex, removed)) } if len(records) == 0 { return output, nil } return output, findings } func validSourceRefs(index source.DocumentIndex, refs []source.SourceRef) bool { if len(refs) == 0 { return false } for _, ref := range refs { if index.ValidateRef(ref) != nil { return false } } return true } func exactIdentity(occurrence dnd.LocationOccurrence) string { var key strings.Builder writeKeyString(&key, occurrence.LocationID) writeKeyString(&key, occurrence.Name) writeKeyString(&key, string(occurrence.Kind)) for _, ref := range occurrence.SourceRefs { writeKeyString(&key, ref.SourceID) writeKeyInt(&key, ref.StartUnitID) writeKeyInt(&key, ref.EndUnitID) } return key.String() } func writeKeyString(builder *strings.Builder, value string) { builder.WriteString(strconv.Itoa(len(value))) builder.WriteByte(':') builder.WriteString(value) } func writeKeyInt(builder *strings.Builder, value int) { builder.WriteString(strconv.Itoa(value)) builder.WriteByte(';') } func lessOccurrence(order shared.SourceRefOrder, left, right dnd.LocationOccurrence) bool { leftPosition, leftHasEvidence := order.EarliestValid(left.SourceRefs) rightPosition, rightHasEvidence := order.EarliestValid(right.SourceRefs) if leftHasEvidence != rightHasEvidence { return leftHasEvidence } if leftHasEvidence && leftPosition != rightPosition { return leftPosition < rightPosition } return lessOccurrenceTies(order, left, right, kindOrder(left.Kind), kindOrder(right.Kind)) } // The normalizer has already established equal earliest-evidence keys here. func lessOccurrenceTies(order shared.SourceRefOrder, left, right dnd.LocationOccurrence, leftRank, rightRank int) bool { if left.LocationID != right.LocationID { return left.LocationID < right.LocationID } if left.Name != right.Name { return left.Name < right.Name } if leftRank != rightRank { return leftRank < rightRank } if left.Kind != right.Kind { return left.Kind < right.Kind } return sourceRefsLess(order, left.SourceRefs, right.SourceRefs) } func kindOrder(kind dnd.LocationOccurrenceKind) int { switch kind { case dnd.LocationOccurrenceKindVisited: return 0 case dnd.LocationOccurrenceKindPlanned: return 1 case dnd.LocationOccurrenceKindRecalled: return 2 case dnd.LocationOccurrenceKindMentioned: return 3 default: return 4 } } func sourceRefsLess(order shared.SourceRefOrder, left, right []source.SourceRef) bool { for index := 0; index < len(left) && index < len(right); index++ { if left[index] == right[index] { continue } return order.Less(left[index], right[index]) } return len(left) < len(right) } 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) } return diagnostics.Finding{Scope: occurrenceScope(retainedIndex), ReasonCode: ReasonCodeDuplicateCollapsed, Message: diagnostics.Aggregate(fmt.Sprintf("duplicate location occurrence collapsed; retained input index %d", retainedIndex), issues)} } func occurrenceScope(index int) string { return fmt.Sprintf("occurrences[%d]", index) } func referenceSlots() []contracts.ReferenceSlot { return []contracts.ReferenceSlot{{ Name: LocationRegistryReferenceSlot, Description: "Required normalized location registry used only for location identity grounding, never as occurrence evidence.", Required: true, AcceptedMediaTypes: []string{"application/json"}, AcceptedArtifactKinds: []contracts.ArtifactKind{dnd.LocationRegistryKind}, MaxBytes: LocationRegistryMaxBytes, }} } func ModuleSpec() pipeline.ModuleSpec { return pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageNormalize, ExecutionClass: contracts.ExecutionClassDeterministic, Requires: append([]string(nil), requiredCapabilities...), Provides: append([]string(nil), providedCapabilities...), ArtifactKind: dnd.LocationOccurrenceListKind, ReferenceSlots: referenceSlots()} } func Register(registry *pipeline.NormalizerRegistry) error { return pipeline.RegisterNormalizerBuilder(registry, ModuleSpec(), validateOptions, func(request pipeline.BuildRequest) (contracts.Normalizer[dnd.LocationOccurrenceList], error) { options, err := DecodeOptions(request.Options) if err != nil { return nil, err } return New(options, request.References) }) } 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 location occurrences normalizer: "+format, args...) }