174 lines
6.3 KiB
Go
174 lines
6.3 KiB
Go
// Package invariants validates normalized D&D NPC interaction artifacts.
|
|
package invariants
|
|
|
|
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"
|
|
interactionmodel "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcinteractions"
|
|
npcregistry "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcs/registry"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics"
|
|
interactionshape "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/npcinteractions/shape"
|
|
)
|
|
|
|
const (
|
|
Key = "normalize/dnd/npc-interactions/invariants"
|
|
ReasonCode = "invalid_npc_interaction_normalization"
|
|
policy = "dnd.npc_interactions.validator.normalized.v1"
|
|
)
|
|
|
|
type Options struct{}
|
|
|
|
type Validator struct {
|
|
npcResolver *npcregistry.Resolver
|
|
}
|
|
|
|
var _ contracts.TypedValidator[dnd.NPCInteractionList] = (*Validator)(nil)
|
|
var _ contracts.ManifestMetadataProvider = (*Validator)(nil)
|
|
var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil)
|
|
|
|
func New(_ Options, references ...contracts.ReferenceSet) (*Validator, error) {
|
|
if len(references) > 1 {
|
|
return nil, fmt.Errorf("NPC interaction invariants validator accepts at most one reference set")
|
|
}
|
|
var referenceSet contracts.ReferenceSet
|
|
if len(references) == 1 {
|
|
referenceSet = references[0]
|
|
}
|
|
resolver, err := npcregistry.NewResolver(referenceSet)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("prepare NPC registry: %w", err)
|
|
}
|
|
return &Validator{npcResolver: resolver}, nil
|
|
}
|
|
|
|
func (v *Validator) Name() string { return Key }
|
|
func (v *Validator) ExecutionClass() contracts.ExecutionClass {
|
|
return contracts.ExecutionClassDeterministic
|
|
}
|
|
|
|
func (v *Validator) ManifestMetadata() map[string]any {
|
|
if v == nil || v.npcResolver == nil {
|
|
return nil
|
|
}
|
|
metadata := map[string]any{"policy": policy}
|
|
seeded := v.npcResolver.Seeded()
|
|
if seeded.Bound() {
|
|
metadata["npc_registry_digest"] = seeded.Digest()
|
|
metadata["npc_count"] = seeded.Count()
|
|
}
|
|
return metadata
|
|
}
|
|
|
|
func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
|
if v == nil || v.npcResolver == nil {
|
|
return nil
|
|
}
|
|
return []pipeline.CheckpointFingerprint{
|
|
{Name: "policy", Value: policy},
|
|
{Name: "npc_registry", Value: v.npcResolver.Seeded().ProjectionDigest()},
|
|
}
|
|
}
|
|
|
|
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.NPCInteractionList]) (contracts.ValidationResult, error) {
|
|
if interactionshape.Validate(req.Value) != nil {
|
|
return contracts.ValidationResult{Approved: true}, nil
|
|
}
|
|
index := source.NewDocumentIndex(req.Source)
|
|
if !allSourceRefsValid(index, req.Value) {
|
|
return contracts.ValidationResult{Approved: true}, nil
|
|
}
|
|
if v == nil || v.npcResolver == nil {
|
|
return contracts.ValidationResult{}, fmt.Errorf("NPC interaction invariants validator must not be nil")
|
|
}
|
|
npcRegistry, err := v.npcResolver.Resolve(req.References)
|
|
if err != nil {
|
|
return contracts.ValidationResult{}, fmt.Errorf("resolve NPC registry: %w", err)
|
|
}
|
|
if !npcRegistry.Bound() {
|
|
return contracts.ValidationResult{Approved: false, ReasonCode: ReasonCode, Message: "invalid NPC interaction normalization: NPC registry reference is required"}, nil
|
|
}
|
|
issues := issuesFor(shared.NewSourceRefOrderFromIndex(index), req.Value, npcRegistry)
|
|
if len(issues) == 0 {
|
|
return contracts.ValidationResult{Approved: true}, nil
|
|
}
|
|
return contracts.ValidationResult{
|
|
Approved: false,
|
|
ReasonCode: ReasonCode,
|
|
Message: diagnostics.Aggregate("invalid NPC interaction normalization", issues),
|
|
}, nil
|
|
}
|
|
|
|
func allSourceRefsValid(index source.DocumentIndex, value dnd.NPCInteractionList) bool {
|
|
for _, interaction := range value.Interactions {
|
|
if !interactionmodel.ValidSourceRefs(index, interaction.SourceRefs) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func issuesFor(order shared.SourceRefOrder, value dnd.NPCInteractionList, npcRegistry *npcregistry.Registry) []string {
|
|
issues := make([]string, 0)
|
|
for index, interaction := range value.Interactions {
|
|
prefix := fmt.Sprintf("interactions[%d]", index)
|
|
if canonical, ok := npcRegistry.Lookup(interaction.Name); ok && interaction.Name != canonical.Name {
|
|
issues = append(issues, prefix+".name is not the canonical NPC display name: "+diagnostics.Quote(interaction.Name))
|
|
}
|
|
for refIndex := 1; refIndex < len(interaction.SourceRefs); refIndex++ {
|
|
previous := interaction.SourceRefs[refIndex-1]
|
|
current := interaction.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))
|
|
}
|
|
}
|
|
}
|
|
|
|
if !sort.SliceIsSorted(value.Interactions, func(left, right int) bool {
|
|
return interactionmodel.Less(order, value.Interactions[left], value.Interactions[right])
|
|
}) {
|
|
issues = append(issues, "interactions are not in canonical order")
|
|
}
|
|
seen := make(map[string]int)
|
|
for index, interaction := range value.Interactions {
|
|
key := interactionmodel.ExactIdentity(interaction)
|
|
if previous, ok := seen[key]; ok {
|
|
issues = append(issues, fmt.Sprintf("interactions[%d] duplicates interaction %d", index, previous))
|
|
continue
|
|
}
|
|
seen[key] = index
|
|
}
|
|
return issues
|
|
}
|
|
|
|
func Spec() pipeline.ValidatorSpec {
|
|
return pipeline.ValidatorSpec{Key: Key, ExecutionClass: contracts.ExecutionClassDeterministic}
|
|
}
|
|
|
|
func Register(registry *pipeline.ValidatorRegistry) error {
|
|
return pipeline.RegisterTypedValidatorBuilder(registry, dnd.NPCInteractionListKind, Spec(), validateOptions, func(request pipeline.BuildRequest) (contracts.TypedValidator[dnd.NPCInteractionList], 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{}, err
|
|
}
|
|
return Options{}, nil
|
|
}
|
|
|
|
func validateOptions(options map[string]any) error { _, err := DecodeOptions(options); return err }
|