Add D&D scene description validation
This commit is contained in:
176
internal/modules/dnd/normalize/scenedescriptions/normalizer.go
Normal file
176
internal/modules/dnd/normalize/scenedescriptions/normalizer.go
Normal file
@@ -0,0 +1,176 @@
|
||||
// Package scenedescriptions normalizes merged D&D scene descriptions.
|
||||
package scenedescriptions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"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"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/scenedescriptions/shape"
|
||||
)
|
||||
|
||||
const (
|
||||
Key = "dnd/scene-descriptions"
|
||||
normalizerPolicy = "dnd.scene_descriptions.normalizer.v1"
|
||||
)
|
||||
|
||||
var requiredCapabilities = []string{"merged"}
|
||||
var providedCapabilities = []string{"normalized"}
|
||||
|
||||
var _ contracts.Normalizer[dnd.SceneDescriptionList] = (*Normalizer)(nil)
|
||||
var _ contracts.ManifestMetadataProvider = (*Normalizer)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Normalizer)(nil)
|
||||
|
||||
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{"normalizer_policy": normalizerPolicy}
|
||||
}
|
||||
|
||||
func (n *Normalizer) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
if n == nil {
|
||||
return nil
|
||||
}
|
||||
return []pipeline.CheckpointFingerprint{{Name: "normalizer_policy", Value: normalizerPolicy}}
|
||||
}
|
||||
|
||||
func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalizeRequest[dnd.SceneDescriptionList]) (contracts.TypedNormalizeResult[dnd.SceneDescriptionList], error) {
|
||||
if n == nil {
|
||||
return contracts.TypedNormalizeResult[dnd.SceneDescriptionList]{}, normalizerErrorf("normalizer must not be nil")
|
||||
}
|
||||
if ctx == nil {
|
||||
return contracts.TypedNormalizeResult[dnd.SceneDescriptionList]{}, normalizerErrorf("context must not be nil")
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return contracts.TypedNormalizeResult[dnd.SceneDescriptionList]{}, normalizerErrorf("context error before normalize: %w", err)
|
||||
}
|
||||
value, err := normalizeList(req.MergeOutput.Value, req.Source)
|
||||
if err != nil {
|
||||
return contracts.TypedNormalizeResult[dnd.SceneDescriptionList]{}, normalizerErrorf("normalize scenes: %w", err)
|
||||
}
|
||||
return contracts.TypedNormalizeResult[dnd.SceneDescriptionList]{Value: value}, nil
|
||||
}
|
||||
|
||||
func normalizeList(input dnd.SceneDescriptionList, doc *source.SourceDocument) (dnd.SceneDescriptionList, error) {
|
||||
if doc == nil {
|
||||
return dnd.SceneDescriptionList{}, fmt.Errorf("source document must not be nil")
|
||||
}
|
||||
if input.Scenes == nil {
|
||||
return dnd.SceneDescriptionList{}, fmt.Errorf("scenes must be present")
|
||||
}
|
||||
if len(input.Scenes) == 0 {
|
||||
return dnd.SceneDescriptionList{}, fmt.Errorf("scenes must not be empty")
|
||||
}
|
||||
|
||||
output := dnd.SceneDescriptionList{Scenes: make([]dnd.SceneDescription, len(input.Scenes))}
|
||||
for index, scene := range input.Scenes {
|
||||
scene.Title = strings.TrimSpace(scene.Title)
|
||||
scene.Summary = strings.TrimSpace(scene.Summary)
|
||||
if err := shape.Validate(dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{scene}}); err != nil {
|
||||
return dnd.SceneDescriptionList{}, fmt.Errorf("scenes[%d]: %w", index, err)
|
||||
}
|
||||
if err := source.ValidateRef(doc, scene.SourceRef); err != nil {
|
||||
return dnd.SceneDescriptionList{}, fmt.Errorf("scenes[%d].source_ref: %s", index, diagnostics.Truncate(err.Error()))
|
||||
}
|
||||
output.Scenes[index] = scene
|
||||
}
|
||||
|
||||
sort.SliceStable(output.Scenes, func(left, right int) bool {
|
||||
leftStart, _ := source.UnitIndex(doc, output.Scenes[left].SourceRef.StartUnitID)
|
||||
rightStart, _ := source.UnitIndex(doc, output.Scenes[right].SourceRef.StartUnitID)
|
||||
if leftStart != rightStart {
|
||||
return leftStart < rightStart
|
||||
}
|
||||
return output.Scenes[left].ID < output.Scenes[right].ID
|
||||
})
|
||||
|
||||
unique := make([]dnd.SceneDescription, 0, len(output.Scenes))
|
||||
byID := make(map[string]dnd.SceneDescription, len(output.Scenes))
|
||||
byRange := make(map[source.SourceRef]dnd.SceneDescription, len(output.Scenes))
|
||||
for _, scene := range output.Scenes {
|
||||
if previous, ok := byID[scene.ID]; ok && !identical(previous, scene) {
|
||||
return dnd.SceneDescriptionList{}, fmt.Errorf("scene ID %s has conflicting records", diagnostics.Quote(scene.ID))
|
||||
}
|
||||
if previous, ok := byRange[scene.SourceRef]; ok && !sameModelContent(previous, scene) {
|
||||
return dnd.SceneDescriptionList{}, fmt.Errorf("source range %s has conflicting records", sourceRefLabel(scene.SourceRef))
|
||||
}
|
||||
if containsIdentical(unique, scene) {
|
||||
continue
|
||||
}
|
||||
byID[scene.ID] = scene
|
||||
byRange[scene.SourceRef] = scene
|
||||
unique = append(unique, scene)
|
||||
}
|
||||
output.Scenes = unique
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func identical(left, right dnd.SceneDescription) bool {
|
||||
return left == right
|
||||
}
|
||||
|
||||
func sameModelContent(left, right dnd.SceneDescription) bool {
|
||||
return left.Kind == right.Kind && left.Title == right.Title && left.Summary == right.Summary
|
||||
}
|
||||
|
||||
func containsIdentical(scenes []dnd.SceneDescription, target dnd.SceneDescription) bool {
|
||||
for _, scene := range scenes {
|
||||
if identical(scene, target) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func sourceRefLabel(ref source.SourceRef) string {
|
||||
return fmt.Sprintf("%s:%d-%d", diagnostics.Quote(ref.SourceID), ref.StartUnitID, ref.EndUnitID)
|
||||
}
|
||||
|
||||
func ModuleSpec() pipeline.ModuleSpec {
|
||||
return pipeline.ModuleSpec{
|
||||
Key: Key,
|
||||
Stage: pipeline.StageNormalize,
|
||||
Requires: append([]string(nil), requiredCapabilities...),
|
||||
Provides: append([]string(nil), providedCapabilities...),
|
||||
ArtifactKind: dnd.SceneDescriptionListKind,
|
||||
}
|
||||
}
|
||||
|
||||
func Register(registry *pipeline.NormalizerRegistry) error {
|
||||
return pipeline.RegisterNormalizerBuilder(registry, ModuleSpec(), validateOptions, func(request pipeline.BuildRequest) (contracts.Normalizer[dnd.SceneDescriptionList], 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 scene descriptions normalizer: "+format, args...)
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package scenedescriptions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"strings"
|
||||
"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"
|
||||
)
|
||||
|
||||
func TestNormalizeTrimsOrdersDeduplicatesAndOwnsOutput(t *testing.T) {
|
||||
doc := testDocument()
|
||||
input := dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{
|
||||
scene("later", 3, 3, dnd.SceneKindNarrative, " Return ", " The party returns. "),
|
||||
scene("first-b", 1, 1, dnd.SceneKindMeta, " Rules ", " The table checks rules. "),
|
||||
scene("first-a", 1, 1, dnd.SceneKindMeta, " Rules ", " The table checks rules. "),
|
||||
scene("later", 3, 3, dnd.SceneKindNarrative, " Return ", " The party returns. "),
|
||||
}}
|
||||
before := cloneList(input)
|
||||
result, err := New(Options{}).Normalize(context.Background(), normalizeRequest(input, doc))
|
||||
if err != nil {
|
||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||
}
|
||||
want := []dnd.SceneDescription{
|
||||
scene("first-a", 1, 1, dnd.SceneKindMeta, "Rules", "The table checks rules."),
|
||||
scene("first-b", 1, 1, dnd.SceneKindMeta, "Rules", "The table checks rules."),
|
||||
scene("later", 3, 3, dnd.SceneKindNarrative, "Return", "The party returns."),
|
||||
}
|
||||
if !reflect.DeepEqual(result.Value.Scenes, want) {
|
||||
t.Fatalf("scenes = %#v, want %#v", result.Value.Scenes, want)
|
||||
}
|
||||
if !reflect.DeepEqual(input, before) {
|
||||
t.Fatalf("Normalize() mutated input: %#v", input)
|
||||
}
|
||||
result.Value.Scenes[0].Title = "changed"
|
||||
if input.Scenes[0].Title == "changed" {
|
||||
t.Fatal("normalized output aliases input storage")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeRejectsInvalidCandidatesAndConflicts(t *testing.T) {
|
||||
doc := testDocument()
|
||||
valid := scene("one", 1, 1, dnd.SceneKindNarrative, "Arrival", "The party arrives.")
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
input dnd.SceneDescriptionList
|
||||
}{
|
||||
{name: "nil list", input: dnd.SceneDescriptionList{}},
|
||||
{name: "empty list", input: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{}}},
|
||||
{name: "blank ID", input: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{{Kind: dnd.SceneKindNarrative, Title: "Arrival", Summary: "The party arrives.", SourceRef: valid.SourceRef}}}},
|
||||
{name: "invalid kind", input: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{{ID: "one", Kind: "other", Title: "Arrival", Summary: "The party arrives.", SourceRef: valid.SourceRef}}}},
|
||||
{name: "invalid range", input: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{{ID: "one", Kind: dnd.SceneKindNarrative, Title: "Arrival", Summary: "The party arrives.", SourceRef: source.SourceRef{SourceID: doc.ID, StartUnitID: 9, EndUnitID: 9}}}}},
|
||||
{name: "same ID conflict", input: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{valid, scene("one", 2, 2, dnd.SceneKindNarrative, "Departure", "The party leaves.")}}},
|
||||
{name: "same range conflict", input: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{valid, scene("two", 1, 1, dnd.SceneKindCombat, "Ambush", "Bandits strike.")}}},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if _, err := New(Options{}).Normalize(context.Background(), normalizeRequest(test.input, doc)); err == nil {
|
||||
t.Fatal("Normalize() error = nil, want rejection")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizerContractAndCancellation(t *testing.T) {
|
||||
if _, err := DecodeOptions(nil); err != nil {
|
||||
t.Fatalf("DecodeOptions(nil) error = %v", err)
|
||||
}
|
||||
if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil {
|
||||
t.Fatal("DecodeOptions() accepted unknown options")
|
||||
}
|
||||
want := pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageNormalize, Requires: []string{"merged"}, Provides: []string{"normalized"}, ArtifactKind: dnd.SceneDescriptionListKind}
|
||||
if got := ModuleSpec(); !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("ModuleSpec() = %#v, want %#v", got, want)
|
||||
}
|
||||
registry := pipeline.NewNormalizerRegistry()
|
||||
if err := Register(registry); err != nil {
|
||||
t.Fatalf("Register() error = %v", err)
|
||||
}
|
||||
normalizer := New(Options{})
|
||||
if metadata := normalizer.ManifestMetadata(); metadata["normalizer_policy"] != normalizerPolicy {
|
||||
t.Fatalf("metadata = %#v", metadata)
|
||||
}
|
||||
if got, want := normalizer.CheckpointFingerprints(), []pipeline.CheckpointFingerprint{{Name: "normalizer_policy", Value: normalizerPolicy}}; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("fingerprints = %#v, want %#v", got, want)
|
||||
}
|
||||
canceled, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if _, err := normalizer.Normalize(canceled, normalizeRequest(dnd.SceneDescriptionList{}, testDocument())); err == nil || !strings.Contains(err.Error(), "context") {
|
||||
t.Fatalf("canceled Normalize() error = %v", err)
|
||||
}
|
||||
if _, err := normalizer.Normalize(context.Background(), normalizeRequest(dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{scene("one", 1, 1, dnd.SceneKindNarrative, "Arrival", "The party arrives.")}}, nil)); err == nil || !strings.Contains(err.Error(), "source document") {
|
||||
t.Fatalf("nil source Normalize() error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeRequest(value dnd.SceneDescriptionList, doc *source.SourceDocument) contracts.TypedNormalizeRequest[dnd.SceneDescriptionList] {
|
||||
return contracts.TypedNormalizeRequest[dnd.SceneDescriptionList]{Source: doc, MergeOutput: contracts.MergeArtifact[dnd.SceneDescriptionList]{Value: value}}
|
||||
}
|
||||
|
||||
func testDocument() *source.SourceDocument {
|
||||
return &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 1, Text: "Rules are discussed."}, {ID: 2, Text: "The party travels."}, {ID: 3, Text: "The party returns."}}}
|
||||
}
|
||||
|
||||
func scene(id string, start, end int, kind dnd.SceneKind, title, summary string) dnd.SceneDescription {
|
||||
return dnd.SceneDescription{ID: id, SourceRef: source.SourceRef{SourceID: "session", StartUnitID: start, EndUnitID: end}, Kind: kind, Title: title, Summary: summary}
|
||||
}
|
||||
|
||||
func cloneList(value dnd.SceneDescriptionList) dnd.SceneDescriptionList {
|
||||
if value.Scenes != nil {
|
||||
value.Scenes = append([]dnd.SceneDescription(nil), value.Scenes...)
|
||||
}
|
||||
return value
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
// Package invariants validates normalized D&D scene description 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"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/scenedescriptions/shape"
|
||||
)
|
||||
|
||||
const (
|
||||
Key = "normalize/dnd/scene-descriptions/invariants"
|
||||
ReasonCode = "invalid_scene_description_normalization"
|
||||
policy = "dnd.scene_descriptions.validator.invariants.v1"
|
||||
)
|
||||
|
||||
type Options struct{}
|
||||
type Validator struct{}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.SceneDescriptionList] = (*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.SceneDescriptionList]) (contracts.ValidationResult, error) {
|
||||
if shape.Validate(req.Value) != nil || !sourceRefsValid(req.Source, req.Value) {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
issues := issuesFor(req.Source, req.Value)
|
||||
if len(issues) == 0 {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
return contracts.ValidationResult{
|
||||
Approved: false, ReasonCode: ReasonCode,
|
||||
Message: diagnostics.Aggregate("invalid scene description normalization", issues),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func sourceRefsValid(doc *source.SourceDocument, value dnd.SceneDescriptionList) bool {
|
||||
for _, scene := range value.Scenes {
|
||||
if source.ValidateRef(doc, scene.SourceRef) != nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func issuesFor(doc *source.SourceDocument, value dnd.SceneDescriptionList) []string {
|
||||
issues := make([]string, 0)
|
||||
if !sort.SliceIsSorted(value.Scenes, func(left, right int) bool {
|
||||
leftStart, _ := source.UnitIndex(doc, value.Scenes[left].SourceRef.StartUnitID)
|
||||
rightStart, _ := source.UnitIndex(doc, value.Scenes[right].SourceRef.StartUnitID)
|
||||
if leftStart != rightStart {
|
||||
return leftStart < rightStart
|
||||
}
|
||||
return value.Scenes[left].ID < value.Scenes[right].ID
|
||||
}) {
|
||||
issues = append(issues, "scenes are not in canonical source order")
|
||||
}
|
||||
|
||||
byID := make(map[string]int, len(value.Scenes))
|
||||
byRange := make(map[source.SourceRef]int, len(value.Scenes))
|
||||
for index, scene := range value.Scenes {
|
||||
if index > 0 && value.Scenes[index-1] == scene {
|
||||
issues = append(issues, fmt.Sprintf("scenes[%d] duplicates scenes[%d]", index, index-1))
|
||||
}
|
||||
if previous, ok := byID[scene.ID]; ok && value.Scenes[previous] != scene {
|
||||
issues = append(issues, fmt.Sprintf("scenes[%d] conflicts with scenes[%d] for scene ID", index, previous))
|
||||
} else if !ok {
|
||||
byID[scene.ID] = index
|
||||
}
|
||||
if previous, ok := byRange[scene.SourceRef]; ok && !sameModelContent(value.Scenes[previous], scene) {
|
||||
issues = append(issues, fmt.Sprintf("scenes[%d] conflicts with scenes[%d] for source range", index, previous))
|
||||
} else if !ok {
|
||||
byRange[scene.SourceRef] = index
|
||||
}
|
||||
}
|
||||
return issues
|
||||
}
|
||||
|
||||
func sameModelContent(left, right dnd.SceneDescription) bool {
|
||||
return left.Kind == right.Kind && left.Title == right.Title && left.Summary == right.Summary
|
||||
}
|
||||
|
||||
func Spec() pipeline.ValidatorSpec {
|
||||
return pipeline.ValidatorSpec{Key: Key, ExecutionClass: contracts.ExecutionClassDeterministic}
|
||||
}
|
||||
|
||||
func Register(registry *pipeline.ValidatorRegistry) error {
|
||||
return pipeline.RegisterTypedValidatorBuilder(registry, dnd.SceneDescriptionListKind, Spec(), validateOptions, func(request pipeline.BuildRequest) (contracts.TypedValidator[dnd.SceneDescriptionList], 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 }
|
||||
@@ -0,0 +1,69 @@
|
||||
package invariants
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
func TestValidatorEnforcesOrderDuplicatesAndConflicts(t *testing.T) {
|
||||
doc := document()
|
||||
first := scene("first", 1, dnd.SceneKindNarrative, "Arrival", "The party arrives.")
|
||||
second := scene("second", 2, dnd.SceneKindCombat, "Ambush", "Bandits attack.")
|
||||
validator := New(Options{})
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
value dnd.SceneDescriptionList
|
||||
approved bool
|
||||
}{
|
||||
{name: "normalized", value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{first, second}}, approved: true},
|
||||
{name: "out of order", value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{second, first}}},
|
||||
{name: "exact duplicate", value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{first, first}}},
|
||||
{name: "same ID conflict", value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{first, scene("first", 2, dnd.SceneKindCombat, "Ambush", "Bandits attack.")}}},
|
||||
{name: "same range conflict", value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{first, scene("other", 1, dnd.SceneKindMeta, "Rules", "The table checks rules.")}}},
|
||||
{name: "same range same content different ID", value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{scene("a", 1, dnd.SceneKindNarrative, "Arrival", "The party arrives."), scene("b", 1, dnd.SceneKindNarrative, "Arrival", "The party arrives.")}}, approved: true},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
result, err := validator.Validate(context.Background(), contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Source: doc, Value: test.value})
|
||||
if err != nil || result.Approved != test.approved {
|
||||
t.Fatalf("Validate() = %#v, %v; want approved=%t", result, err, test.approved)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatorDefersOwnedFailuresAndRegistersPolicy(t *testing.T) {
|
||||
doc := document()
|
||||
result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Source: doc, Value: dnd.SceneDescriptionList{}})
|
||||
if err != nil || !result.Approved {
|
||||
t.Fatalf("Validate() = %#v, %v; want shape deferral", result, err)
|
||||
}
|
||||
invalidRef := dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{scene("one", 9, dnd.SceneKindNarrative, "Arrival", "The party arrives.")}}
|
||||
result, err = New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Source: doc, Value: invalidRef})
|
||||
if err != nil || !result.Approved {
|
||||
t.Fatalf("Validate() = %#v, %v; want source-reference deferral", result, err)
|
||||
}
|
||||
registry := pipeline.NewValidatorRegistry()
|
||||
if err := Register(registry); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := registry.Spec(Key); !ok {
|
||||
t.Fatalf("registry missing %q", Key)
|
||||
}
|
||||
if got, want := New(Options{}).CheckpointFingerprints(), []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("fingerprints = %#v, want %#v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func document() *source.SourceDocument {
|
||||
return &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 1, Text: "The party arrives."}, {ID: 2, Text: "Bandits attack."}}}
|
||||
}
|
||||
|
||||
func scene(id string, unit int, kind dnd.SceneKind, title, summary string) dnd.SceneDescription {
|
||||
return dnd.SceneDescription{ID: id, SourceRef: source.SourceRef{SourceID: "session", StartUnitID: unit, EndUnitID: unit}, Kind: kind, Title: title, Summary: summary}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
// Package shape validates required D&D scene description fields.
|
||||
package shape
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
const (
|
||||
Key = "extract/dnd/scene-descriptions/shape"
|
||||
ReasonCode = "invalid_scene_description_shape"
|
||||
policy = "dnd.scene_descriptions.validator.shape.v1"
|
||||
)
|
||||
|
||||
type Options struct{}
|
||||
type Validator struct{}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.SceneDescriptionList] = (*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.SceneDescriptionList]) (contracts.ValidationResult, error) {
|
||||
if err := ValidateForStage(req.Value, req.Stage); err != nil {
|
||||
return contracts.ValidationResult{Approved: false, ReasonCode: ReasonCode, Message: err.Error()}, nil
|
||||
}
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
|
||||
func Validate(value dnd.SceneDescriptionList) error { return validate(value, false) }
|
||||
|
||||
func ValidateForStage(value dnd.SceneDescriptionList, stage string) error {
|
||||
return validate(value, stage == string(pipeline.StageExtract))
|
||||
}
|
||||
|
||||
func validate(value dnd.SceneDescriptionList, exactlyOne bool) error {
|
||||
issues := make([]string, 0)
|
||||
if value.Scenes == nil {
|
||||
issues = append(issues, "scenes must be present")
|
||||
} else if len(value.Scenes) == 0 {
|
||||
issues = append(issues, "scenes must not be empty")
|
||||
} else if exactlyOne && len(value.Scenes) != 1 {
|
||||
issues = append(issues, "extraction must contain exactly one scene")
|
||||
}
|
||||
for index, scene := range value.Scenes {
|
||||
prefix := fmt.Sprintf("scenes[%d]", index)
|
||||
if strings.TrimSpace(scene.ID) == "" || scene.ID != strings.TrimSpace(scene.ID) {
|
||||
issues = append(issues, prefix+".id must be non-empty and trimmed")
|
||||
}
|
||||
if !ValidKind(scene.Kind) {
|
||||
issues = append(issues, prefix+".kind is unsupported: "+diagnostics.Quote(string(scene.Kind)))
|
||||
}
|
||||
if strings.TrimSpace(scene.Title) == "" || scene.Title != strings.TrimSpace(scene.Title) {
|
||||
issues = append(issues, prefix+".title must be non-empty and trimmed")
|
||||
}
|
||||
if strings.TrimSpace(scene.Summary) == "" || scene.Summary != strings.TrimSpace(scene.Summary) {
|
||||
issues = append(issues, prefix+".summary must be non-empty and trimmed")
|
||||
}
|
||||
if strings.TrimSpace(scene.SourceRef.SourceID) == "" || scene.SourceRef.SourceID != strings.TrimSpace(scene.SourceRef.SourceID) || scene.SourceRef.StartUnitID <= 0 || scene.SourceRef.EndUnitID <= 0 {
|
||||
issues = append(issues, prefix+".source_ref must have a trimmed source ID and positive unit IDs")
|
||||
}
|
||||
}
|
||||
if len(issues) == 0 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%s", diagnostics.Aggregate("invalid scene description shape", issues))
|
||||
}
|
||||
|
||||
func ValidKind(value dnd.SceneKind) bool {
|
||||
switch value {
|
||||
case dnd.SceneKindCombat, dnd.SceneKindNarrative, dnd.SceneKindRecap, dnd.SceneKindMeta:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func Spec() pipeline.ValidatorSpec {
|
||||
return pipeline.ValidatorSpec{Key: Key, ExecutionClass: contracts.ExecutionClassDeterministic}
|
||||
}
|
||||
|
||||
func Register(registry *pipeline.ValidatorRegistry) error {
|
||||
return pipeline.RegisterTypedValidatorBuilder(registry, dnd.SceneDescriptionListKind, Spec(), validateOptions, func(request pipeline.BuildRequest) (contracts.TypedValidator[dnd.SceneDescriptionList], 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 }
|
||||
@@ -0,0 +1,65 @@
|
||||
package shape
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
func TestValidatorRequiresSceneShapeAndExactlyOneExtractionRecord(t *testing.T) {
|
||||
valid := list(scene("one", dnd.SceneKindNarrative, "Arrival", "The party arrives.", 1, 1))
|
||||
validator := New(Options{})
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
value dnd.SceneDescriptionList
|
||||
stage string
|
||||
approved bool
|
||||
}{
|
||||
{name: "valid normalization", value: valid, approved: true},
|
||||
{name: "valid extraction", value: valid, stage: string(pipeline.StageExtract), approved: true},
|
||||
{name: "multiple extraction records", value: list(valid.Scenes[0], scene("two", dnd.SceneKindMeta, "Rules", "The table checks rules.", 2, 2)), stage: string(pipeline.StageExtract)},
|
||||
{name: "nil list", value: dnd.SceneDescriptionList{}},
|
||||
{name: "empty list", value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{}}},
|
||||
{name: "blank ID", value: list(scene(" ", dnd.SceneKindNarrative, "Arrival", "The party arrives.", 1, 1))},
|
||||
{name: "unsupported kind", value: list(scene("one", "other", "Arrival", "The party arrives.", 1, 1))},
|
||||
{name: "untrimmed prose", value: list(scene("one", dnd.SceneKindNarrative, " Arrival ", "The party arrives.", 1, 1))},
|
||||
{name: "empty source reference", value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{{ID: "one", Kind: dnd.SceneKindNarrative, Title: "Arrival", Summary: "The party arrives."}}}},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
result, err := validator.Validate(context.Background(), contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Stage: test.stage, Value: test.value})
|
||||
if err != nil || result.Approved != test.approved {
|
||||
t.Fatalf("Validate() = %#v, %v; want approved=%t", result, err, test.approved)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatorContractAndRegistration(t *testing.T) {
|
||||
registry := pipeline.NewValidatorRegistry()
|
||||
if err := Register(registry); err != nil {
|
||||
t.Fatalf("Register() error = %v", err)
|
||||
}
|
||||
if _, ok := registry.Spec(Key); !ok {
|
||||
t.Fatalf("registry has no spec for %q", Key)
|
||||
}
|
||||
if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil {
|
||||
t.Fatal("DecodeOptions() accepted unknown options")
|
||||
}
|
||||
validator := New(Options{})
|
||||
if got, want := validator.CheckpointFingerprints(), []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("fingerprints = %#v, want %#v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func list(scenes ...dnd.SceneDescription) dnd.SceneDescriptionList {
|
||||
return dnd.SceneDescriptionList{Scenes: scenes}
|
||||
}
|
||||
|
||||
func scene(id string, kind dnd.SceneKind, title, summary string, start, end int) dnd.SceneDescription {
|
||||
return dnd.SceneDescription{ID: id, Kind: kind, Title: title, Summary: summary, SourceRef: source.SourceRef{SourceID: "session", StartUnitID: start, EndUnitID: end}}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
// Package sourcerefs validates D&D scene-description source identity.
|
||||
package sourcerefs
|
||||
|
||||
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"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/scenedescriptions/shape"
|
||||
)
|
||||
|
||||
const (
|
||||
Key = "extract/dnd/scene-descriptions/source_refs"
|
||||
ReasonCode = "invalid_scene_description_source_refs"
|
||||
policy = "dnd.scene_descriptions.validator.source_refs.v1"
|
||||
)
|
||||
|
||||
type Options struct{}
|
||||
type Validator struct{}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.SceneDescriptionList] = (*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.SceneDescriptionList]) (contracts.ValidationResult, error) {
|
||||
if shape.ValidateForStage(req.Value, req.Stage) != nil {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
issues := make([]string, 0)
|
||||
if req.Stage == string(pipeline.StageExtract) && req.Chunk == nil {
|
||||
issues = append(issues, "current extraction chunk must not be nil")
|
||||
}
|
||||
for index, scene := range req.Value.Scenes {
|
||||
prefix := fmt.Sprintf("scenes[%d]", index)
|
||||
if err := source.ValidateRef(req.Source, scene.SourceRef); err != nil {
|
||||
issues = append(issues, prefix+".source_ref: "+diagnostics.Truncate(err.Error()))
|
||||
continue
|
||||
}
|
||||
if req.Stage == string(pipeline.StageExtract) && req.Chunk != nil {
|
||||
if scene.ID != req.Chunk.ID {
|
||||
issues = append(issues, prefix+".id must equal the current extraction chunk ID")
|
||||
}
|
||||
if scene.SourceRef != req.Chunk.Ref {
|
||||
issues = append(issues, prefix+".source_ref must equal the current extraction chunk range")
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(issues) == 0 {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
return contracts.ValidationResult{
|
||||
Approved: false, ReasonCode: ReasonCode,
|
||||
Message: diagnostics.Aggregate("invalid scene description source references", issues),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func Spec() pipeline.ValidatorSpec {
|
||||
return pipeline.ValidatorSpec{Key: Key, ExecutionClass: contracts.ExecutionClassDeterministic}
|
||||
}
|
||||
|
||||
func Register(registry *pipeline.ValidatorRegistry) error {
|
||||
return pipeline.RegisterTypedValidatorBuilder(registry, dnd.SceneDescriptionListKind, Spec(), validateOptions, func(request pipeline.BuildRequest) (contracts.TypedValidator[dnd.SceneDescriptionList], 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 }
|
||||
@@ -0,0 +1,59 @@
|
||||
package sourcerefs
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
func TestValidatorRequiresExactExtractionChunkIdentity(t *testing.T) {
|
||||
doc := document()
|
||||
chunk := &source.Chunk{ID: "chunk-1", SourceID: doc.ID, Ref: source.SourceRef{SourceID: doc.ID, StartUnitID: 1, EndUnitID: 2}}
|
||||
valid := dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{{ID: chunk.ID, SourceRef: chunk.Ref, Kind: dnd.SceneKindNarrative, Title: "Arrival", Summary: "The party arrives."}}}
|
||||
validator := New(Options{})
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
request contracts.TypedValidationRequest[dnd.SceneDescriptionList]
|
||||
approved bool
|
||||
}{
|
||||
{name: "exact extraction identity", request: contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Stage: string(pipeline.StageExtract), Source: doc, Chunk: chunk, Value: valid}, approved: true},
|
||||
{name: "wrong ID", request: contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Stage: string(pipeline.StageExtract), Source: doc, Chunk: chunk, Value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{{ID: "other", SourceRef: chunk.Ref, Kind: dnd.SceneKindNarrative, Title: "Arrival", Summary: "The party arrives."}}}}},
|
||||
{name: "contained but unequal range", request: contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Stage: string(pipeline.StageExtract), Source: doc, Chunk: chunk, Value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{{ID: chunk.ID, SourceRef: source.SourceRef{SourceID: doc.ID, StartUnitID: 1, EndUnitID: 1}, Kind: dnd.SceneKindNarrative, Title: "Arrival", Summary: "The party arrives."}}}}},
|
||||
{name: "missing chunk", request: contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Stage: string(pipeline.StageExtract), Source: doc, Value: valid}},
|
||||
{name: "normalization needs no chunk", request: contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Stage: string(pipeline.StageNormalize), Source: doc, Value: valid}, approved: true},
|
||||
{name: "invalid current source", request: contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Source: doc, Value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{{ID: "chunk", SourceRef: source.SourceRef{SourceID: doc.ID, StartUnitID: 7, EndUnitID: 7}, Kind: dnd.SceneKindNarrative, Title: "Arrival", Summary: "The party arrives."}}}}},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
result, err := validator.Validate(context.Background(), test.request)
|
||||
if err != nil || result.Approved != test.approved {
|
||||
t.Fatalf("Validate() = %#v, %v; want approved=%t", result, err, test.approved)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatorDefersMalformedShapeAndRegistersPolicy(t *testing.T) {
|
||||
result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Value: dnd.SceneDescriptionList{}})
|
||||
if err != nil || !result.Approved {
|
||||
t.Fatalf("Validate() = %#v, %v; want malformed shape deferred", result, err)
|
||||
}
|
||||
registry := pipeline.NewValidatorRegistry()
|
||||
if err := Register(registry); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := registry.Spec(Key); !ok {
|
||||
t.Fatalf("registry missing %q", Key)
|
||||
}
|
||||
if got, want := New(Options{}).CheckpointFingerprints(), []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("fingerprints = %#v, want %#v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func document() *source.SourceDocument {
|
||||
return &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 1, Text: "The party arrives."}, {ID: 2, Text: "They investigate."}}}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
// Package sourcerelatedness warns when scene prose has no lexical grounding.
|
||||
package sourcerelatedness
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"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"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/scenedescriptions/shape"
|
||||
)
|
||||
|
||||
const (
|
||||
Key = "extract/dnd/scene-descriptions/source_relatedness"
|
||||
WarningReasonCode = "scene_description_not_near_source"
|
||||
OmittedReasonCode = "scene_description_relatedness_warnings_omitted"
|
||||
policy = "dnd.scene_descriptions.validator.source_relatedness.v1"
|
||||
)
|
||||
|
||||
var stopwords = map[string]struct{}{
|
||||
"a": {}, "an": {}, "and": {}, "are": {}, "as": {}, "at": {}, "be": {}, "but": {}, "by": {}, "for": {}, "from": {},
|
||||
"had": {}, "has": {}, "have": {}, "he": {}, "her": {}, "him": {}, "his": {}, "in": {}, "into": {}, "is": {}, "it": {}, "its": {},
|
||||
"of": {}, "on": {}, "or": {}, "she": {}, "that": {}, "the": {}, "their": {}, "them": {}, "they": {}, "this": {}, "to": {},
|
||||
"was": {}, "were": {}, "with": {},
|
||||
}
|
||||
|
||||
type Options struct{}
|
||||
type Validator struct{}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.SceneDescriptionList] = (*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.SceneDescriptionList]) (contracts.ValidationResult, error) {
|
||||
if shape.Validate(req.Value) != nil || !sourceRefsValid(req.Source, req.Value) {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
warnings := make([]contracts.Warning, 0)
|
||||
for index, scene := range req.Value.Scenes {
|
||||
citedText, err := shared.CitedText(req.Source, []source.SourceRef{scene.SourceRef})
|
||||
if err != nil {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
citedTokens := tokenSet(citedText)
|
||||
if !hasGroundedToken(citedTokens, scene.Title) {
|
||||
warnings = append(warnings, warning(index, "title"))
|
||||
}
|
||||
if !hasGroundedToken(citedTokens, scene.Summary) {
|
||||
warnings = append(warnings, warning(index, "summary"))
|
||||
}
|
||||
}
|
||||
return contracts.ValidationResult{
|
||||
Approved: true,
|
||||
Warnings: diagnostics.LimitWarnings(warnings, "scenes", OmittedReasonCode),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func sourceRefsValid(doc *source.SourceDocument, value dnd.SceneDescriptionList) bool {
|
||||
for _, scene := range value.Scenes {
|
||||
if source.ValidateRef(doc, scene.SourceRef) != nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func tokenSet(value string) map[string]struct{} {
|
||||
tokens := make(map[string]struct{})
|
||||
for _, token := range shared.NormalizedTokens(value) {
|
||||
if significant(token) {
|
||||
tokens[token] = struct{}{}
|
||||
}
|
||||
}
|
||||
return tokens
|
||||
}
|
||||
|
||||
func hasGroundedToken(cited map[string]struct{}, value string) bool {
|
||||
for _, token := range shared.NormalizedTokens(value) {
|
||||
if !significant(token) {
|
||||
continue
|
||||
}
|
||||
if _, ok := cited[token]; ok {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func significant(token string) bool {
|
||||
if utf8.RuneCountInString(token) < 3 {
|
||||
return false
|
||||
}
|
||||
_, ignored := stopwords[token]
|
||||
return !ignored
|
||||
}
|
||||
|
||||
func warning(index int, field string) contracts.Warning {
|
||||
return contracts.Warning{
|
||||
Scope: fmt.Sprintf("scenes[%d].%s", index, field),
|
||||
ReasonCode: WarningReasonCode,
|
||||
Message: "scene description " + field + " has no significant token in cited source text",
|
||||
}
|
||||
}
|
||||
|
||||
func Spec() pipeline.ValidatorSpec {
|
||||
return pipeline.ValidatorSpec{Key: Key, ExecutionClass: contracts.ExecutionClassDeterministic}
|
||||
}
|
||||
|
||||
func Register(registry *pipeline.ValidatorRegistry) error {
|
||||
return pipeline.RegisterTypedValidatorBuilder(registry, dnd.SceneDescriptionListKind, Spec(), validateOptions, func(request pipeline.BuildRequest) (contracts.TypedValidator[dnd.SceneDescriptionList], 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 }
|
||||
@@ -0,0 +1,78 @@
|
||||
package sourcerelatedness
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"strings"
|
||||
"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"
|
||||
)
|
||||
|
||||
func TestValidatorWarnsIndependentlyForUngroundedTitleAndSummary(t *testing.T) {
|
||||
doc := document()
|
||||
value := dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{
|
||||
scene("one", "Watchtower arrival", "The group enters the ruined watchtower."),
|
||||
scene("two", "Moonlit council", "Whispers linger."),
|
||||
scene("three", "The and to", "An or the"),
|
||||
}}
|
||||
result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Source: doc, Value: value})
|
||||
if err != nil || !result.Approved {
|
||||
t.Fatalf("Validate() = %#v, %v", result, err)
|
||||
}
|
||||
wantScopes := []string{"scenes[1].title", "scenes[1].summary", "scenes[2].title", "scenes[2].summary"}
|
||||
if len(result.Warnings) != len(wantScopes) {
|
||||
t.Fatalf("warnings = %#v, want %d", result.Warnings, len(wantScopes))
|
||||
}
|
||||
for index, scope := range wantScopes {
|
||||
item := result.Warnings[index]
|
||||
if item.Scope != scope || item.ReasonCode != WarningReasonCode {
|
||||
t.Fatalf("warning[%d] = %#v, want scope %q", index, item, scope)
|
||||
}
|
||||
if len(item.Message) > 4096 || strings.Contains(item.Message, doc.Units[0].Text) {
|
||||
t.Fatalf("warning[%d] is not safely bounded: %#v", index, item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatorUsesTranscriptOnlyAndDefersInvalidInputs(t *testing.T) {
|
||||
doc := document()
|
||||
value := dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{scene("one", "Greencloak", "Greencloak arrives.")}}
|
||||
result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.SceneDescriptionList]{
|
||||
Source: doc, Value: value,
|
||||
References: contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
||||
"glossary": {Items: []contracts.ReferenceItem{{Content: []byte("Greencloak: title")}}},
|
||||
}},
|
||||
})
|
||||
if err != nil || len(result.Warnings) != 2 {
|
||||
t.Fatalf("Validate() = %#v, %v; want transcript-only warnings", result, err)
|
||||
}
|
||||
malformed, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Source: doc, Value: dnd.SceneDescriptionList{}})
|
||||
if err != nil || !malformed.Approved || len(malformed.Warnings) != 0 {
|
||||
t.Fatalf("malformed Validate() = %#v, %v; want deferral", malformed, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatorRegistrationAndPolicyFingerprint(t *testing.T) {
|
||||
registry := pipeline.NewValidatorRegistry()
|
||||
if err := Register(registry); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := registry.Spec(Key); !ok {
|
||||
t.Fatalf("registry missing %q", Key)
|
||||
}
|
||||
if got, want := New(Options{}).CheckpointFingerprints(), []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("fingerprints = %#v, want %#v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func document() *source.SourceDocument {
|
||||
return &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 1, Text: "The group enters the ruined watchtower."}}}
|
||||
}
|
||||
|
||||
func scene(id, title, summary string) dnd.SceneDescription {
|
||||
return dnd.SceneDescription{ID: id, SourceRef: source.SourceRef{SourceID: "session", StartUnitID: 1, EndUnitID: 1}, Kind: dnd.SceneKindNarrative, Title: title, Summary: summary}
|
||||
}
|
||||
Reference in New Issue
Block a user