Improve D&D validation reliability
This commit is contained in:
@@ -16,7 +16,7 @@ import (
|
||||
const (
|
||||
Key = "extract/dnd/enemy-events/engagements"
|
||||
ReasonCode = "duplicate_enemy_engagement"
|
||||
policy = "dnd.enemy_events.validator.engagements.v1"
|
||||
policy = "dnd.enemy_events.validator.engagements.v2"
|
||||
)
|
||||
|
||||
type Options struct{}
|
||||
@@ -38,7 +38,9 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq
|
||||
if enemyeventshape.Validate(req.Value) != nil {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
seen := make(map[string]struct{})
|
||||
seen := make(map[string]dnd.EnemyEvent)
|
||||
issues := make([]string, 0)
|
||||
var corrections diagnostics.Corrections
|
||||
for _, event := range req.Value.Events {
|
||||
if event.Kind != dnd.EnemyEventKindEngaged {
|
||||
continue
|
||||
@@ -47,17 +49,24 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq
|
||||
if identity == "" {
|
||||
continue
|
||||
}
|
||||
if _, found := seen[identity]; found {
|
||||
return contracts.ValidationResult{
|
||||
Approved: false,
|
||||
ReasonCode: ReasonCode,
|
||||
Message: diagnostics.Aggregate("duplicate enemy engagement", []string{
|
||||
fmt.Sprintf("subject %s has more than one engagement in one combat scene", diagnostics.Quote(event.Name)),
|
||||
}),
|
||||
CorrectionGuidance: "Return at most one engagement event for each contextual enemy name within the same combat scene.",
|
||||
}, nil
|
||||
if first, found := seen[identity]; found {
|
||||
issues = append(issues, fmt.Sprintf("subject %s has more than one engagement in one combat scene", diagnostics.Quote(event.Name)))
|
||||
corrections.Add(
|
||||
"duplicate-subject",
|
||||
"Return at most one `engaged` event for each contextual enemy name within this combat scene; keep the source ranges together on that one event.",
|
||||
fmt.Sprintf("Affected enemy %s: first engagement %s; additional engagement %s.", diagnostics.Quote(event.Name), diagnostics.SourceRange(first.SourceRefs), diagnostics.SourceRange(event.SourceRefs)),
|
||||
)
|
||||
continue
|
||||
}
|
||||
seen[identity] = struct{}{}
|
||||
seen[identity] = event
|
||||
}
|
||||
if len(issues) != 0 {
|
||||
return contracts.ValidationResult{
|
||||
Approved: false,
|
||||
ReasonCode: ReasonCode,
|
||||
Message: diagnostics.Aggregate("duplicate enemy engagement", issues),
|
||||
CorrectionGuidance: corrections.Guidance("Correct every duplicate enemy engagement and return the complete replacement event list"),
|
||||
}, nil
|
||||
}
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
|
||||
@@ -66,6 +66,27 @@ func TestValidatorRegistersStrictOptionsAndPolicy(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatorReportsEveryDuplicateSubjectWithContext(t *testing.T) {
|
||||
value := events(
|
||||
event("Ashfang", dnd.EnemyEventKindEngaged, 1),
|
||||
event("Ashfang", dnd.EnemyEventKindEngaged, 2),
|
||||
event("Briar", dnd.EnemyEventKindEngaged, 3),
|
||||
event("Briar", dnd.EnemyEventKindEngaged, 4),
|
||||
)
|
||||
result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.EnemyEventList]{Value: value})
|
||||
if err != nil || result.Approved {
|
||||
t.Fatalf("Validate() = %#v, %v; want rejection", result, err)
|
||||
}
|
||||
for _, want := range []string{"Ashfang", "Briar", "source unit 1", "source unit 2", "source unit 3", "source unit 4"} {
|
||||
if !strings.Contains(result.CorrectionGuidance, want) {
|
||||
t.Fatalf("CorrectionGuidance = %q, want %q", result.CorrectionGuidance, want)
|
||||
}
|
||||
}
|
||||
if strings.Contains(result.CorrectionGuidance, "events[") || strings.Contains(result.CorrectionGuidance, ReasonCode) {
|
||||
t.Fatalf("CorrectionGuidance exposed internal diagnostics: %q", result.CorrectionGuidance)
|
||||
}
|
||||
}
|
||||
|
||||
func events(values ...dnd.EnemyEvent) dnd.EnemyEventList { return dnd.EnemyEventList{Events: values} }
|
||||
|
||||
func event(name string, kind dnd.EnemyEventKind, unitID int) dnd.EnemyEvent {
|
||||
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
const (
|
||||
Key = "extract/dnd/enemy-events/shape"
|
||||
ReasonCode = "invalid_enemy_event_shape"
|
||||
policy = "dnd.enemy_events.validator.shape.v1"
|
||||
policy = "dnd.enemy_events.validator.shape.v2"
|
||||
)
|
||||
|
||||
type Options struct{}
|
||||
@@ -35,38 +35,50 @@ func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.EnemyEventList]) (contracts.ValidationResult, error) {
|
||||
if err := Validate(req.Value); err != nil {
|
||||
return contracts.ValidationResult{Approved: false, ReasonCode: ReasonCode, Message: err.Error(), CorrectionGuidance: "Return a complete enemy-event list with every required field present, valid contextual NPC names, supported event values, and valid source references."}, nil
|
||||
issues, corrections := assess(req.Value)
|
||||
if len(issues) != 0 {
|
||||
return contracts.ValidationResult{
|
||||
Approved: false,
|
||||
ReasonCode: ReasonCode,
|
||||
Message: diagnostics.Aggregate("invalid enemy event shape", issues),
|
||||
CorrectionGuidance: corrections.Guidance("Correct every rejected enemy event and return the complete replacement event list"),
|
||||
}, nil
|
||||
}
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
|
||||
func Validate(value dnd.EnemyEventList) error {
|
||||
issues := issuesFor(value)
|
||||
issues, _ := assess(value)
|
||||
if len(issues) == 0 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%s", diagnostics.Aggregate("invalid enemy event shape", issues))
|
||||
}
|
||||
|
||||
func issuesFor(value dnd.EnemyEventList) []string {
|
||||
func assess(value dnd.EnemyEventList) ([]string, diagnostics.Corrections) {
|
||||
var corrections diagnostics.Corrections
|
||||
if value.Events == nil {
|
||||
return []string{"events must be present"}
|
||||
corrections.Add("list", "Return an `events` array; use an empty array when the combat scene establishes no enemy events.", "")
|
||||
return []string{"events must be present"}, corrections
|
||||
}
|
||||
issues := make([]string, 0)
|
||||
for eventIndex, event := range value.Events {
|
||||
prefix := fmt.Sprintf("events[%d]", eventIndex)
|
||||
record := fmt.Sprintf("Affected %s event for enemy %s %s.", diagnostics.Quote(string(event.Kind)), diagnostics.Quote(strings.TrimSpace(event.Name)), diagnostics.SourceRange(event.SourceRefs))
|
||||
if strings.TrimSpace(event.Name) == "" {
|
||||
issues = append(issues, prefix+".name must not be empty: "+diagnostics.Quote(event.Name))
|
||||
corrections.Add("name", "Select a nonblank contextual enemy name from the supplied NPC registry for every event.", record)
|
||||
}
|
||||
if !enemyeventmodel.SupportedKind(event.Kind) {
|
||||
issues = append(issues, prefix+".kind is unsupported: "+diagnostics.Quote(string(event.Kind)))
|
||||
corrections.Add("kind", "Set `kind` to exactly one of `engaged`, `killed`, `fled`, `captured`, or `incapacitated`.", record)
|
||||
}
|
||||
if len(event.SourceRefs) == 0 {
|
||||
issues = append(issues, prefix+".source_refs must contain at least one reference")
|
||||
corrections.Add("source-refs", "Provide at least one transcript source range that directly supports every enemy event.", record)
|
||||
}
|
||||
}
|
||||
return issues
|
||||
return issues, corrections
|
||||
}
|
||||
|
||||
func Spec() pipeline.ValidatorSpec {
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"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"
|
||||
enemyeventshape "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/enemyevents/shape"
|
||||
)
|
||||
@@ -16,7 +17,7 @@ import (
|
||||
const (
|
||||
Key = "extract/dnd/enemy-events/source_refs"
|
||||
ReasonCode = "invalid_enemy_event_source_refs"
|
||||
policy = "dnd.enemy_events.validator.source_refs.v1"
|
||||
policy = "dnd.enemy_events.validator.source_refs.v2"
|
||||
)
|
||||
|
||||
type Options struct{}
|
||||
@@ -42,61 +43,40 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
index := source.NewDocumentIndex(req.Source)
|
||||
var coverage *chunkCoverage
|
||||
var coverage shared.ChunkCoverage
|
||||
if req.Stage == string(pipeline.StageExtract) {
|
||||
coverage = newChunkCoverage(req.Chunk)
|
||||
coverage = shared.NewChunkCoverage(req.Chunk)
|
||||
}
|
||||
issues := sourceRefIssues(index, req.Source, coverage, req.Value)
|
||||
issues, corrections := sourceRefIssues(index, req.Source, coverage, req.Stage == string(pipeline.StageExtract), req.Value)
|
||||
if len(issues) == 0 {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
return contracts.ValidationResult{Approved: false, ReasonCode: ReasonCode, Message: diagnostics.Aggregate("invalid enemy event source references", issues), CorrectionGuidance: "Return enemy events whose source references identify valid transcript ranges within the supplied extraction chunk and directly support each event."}, nil
|
||||
return contracts.ValidationResult{
|
||||
Approved: false,
|
||||
ReasonCode: ReasonCode,
|
||||
Message: diagnostics.Aggregate("invalid enemy event source references", issues),
|
||||
CorrectionGuidance: corrections.Guidance("Correct every rejected enemy-event citation and return the complete replacement event list"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func sourceRefIssues(index source.DocumentIndex, doc *source.SourceDocument, coverage *chunkCoverage, value dnd.EnemyEventList) []string {
|
||||
func sourceRefIssues(index source.DocumentIndex, doc *source.SourceDocument, coverage shared.ChunkCoverage, checkCoverage bool, value dnd.EnemyEventList) ([]string, diagnostics.Corrections) {
|
||||
issues := make([]string, 0)
|
||||
var corrections diagnostics.Corrections
|
||||
for eventIndex, event := range value.Events {
|
||||
for refIndex, ref := range event.SourceRefs {
|
||||
record := fmt.Sprintf("Affected %s event for enemy %s, citing %s.", diagnostics.Quote(string(event.Kind)), diagnostics.Quote(event.Name), diagnostics.SourceRefRange(ref))
|
||||
if err := index.ValidateRef(ref); err != nil {
|
||||
issues = append(issues, fmt.Sprintf("events[%d].source_refs[%d]: %s", eventIndex, refIndex, diagnostics.Truncate(err.Error())))
|
||||
corrections.Add("valid-range", "Use positive source range endpoints that occur in the supplied transcript, with the earlier unit first.", record)
|
||||
continue
|
||||
}
|
||||
if coverage != nil && !coverage.contains(doc, ref) {
|
||||
if checkCoverage && !coverage.Contains(index, doc, ref) {
|
||||
issues = append(issues, fmt.Sprintf("events[%d].source_refs[%d]: source reference is outside the current extraction chunk", eventIndex, refIndex))
|
||||
corrections.Add("chunk-range", "Use only source ranges wholly contained in the supplied extraction chunk.", record)
|
||||
}
|
||||
}
|
||||
}
|
||||
return issues
|
||||
}
|
||||
|
||||
type chunkCoverage struct {
|
||||
sourceID string
|
||||
unitIDs map[int]struct{}
|
||||
}
|
||||
|
||||
func newChunkCoverage(chunk *source.Chunk) *chunkCoverage {
|
||||
coverage := &chunkCoverage{sourceID: chunk.SourceID, unitIDs: make(map[int]struct{}, len(chunk.Units))}
|
||||
for _, unit := range chunk.Units {
|
||||
coverage.unitIDs[unit.ID] = struct{}{}
|
||||
}
|
||||
return coverage
|
||||
}
|
||||
|
||||
func (coverage *chunkCoverage) contains(doc *source.SourceDocument, ref source.SourceRef) bool {
|
||||
if coverage == nil || doc == nil || ref.SourceID != coverage.sourceID {
|
||||
return false
|
||||
}
|
||||
start, startOK := source.UnitIndex(doc, ref.StartUnitID)
|
||||
end, endOK := source.UnitIndex(doc, ref.EndUnitID)
|
||||
if !startOK || !endOK || start > end {
|
||||
return false
|
||||
}
|
||||
for position := start; position <= end; position++ {
|
||||
if _, found := coverage.unitIDs[doc.Units[position].ID]; !found {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
return issues, corrections
|
||||
}
|
||||
|
||||
func Spec() pipeline.ValidatorSpec {
|
||||
|
||||
Reference in New Issue
Block a user