Improve D&D validation reliability
This commit is contained in:
@@ -15,7 +15,7 @@ import (
|
||||
const (
|
||||
Key = "extract/dnd/scene-descriptions/shape"
|
||||
ReasonCode = "invalid_scene_description_shape"
|
||||
policy = "dnd.scene_descriptions.validator.shape.v1"
|
||||
policy = "dnd.scene_descriptions.validator.shape.v2"
|
||||
)
|
||||
|
||||
type Options struct{}
|
||||
@@ -34,8 +34,14 @@ func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
}
|
||||
|
||||
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(), CorrectionGuidance: "Return a complete scene-description list with the required number of scenes, supported scene kinds, nonblank titles and summaries, and valid source references."}, nil
|
||||
issues, corrections := assess(req.Value, req.Stage == string(pipeline.StageExtract))
|
||||
if len(issues) != 0 {
|
||||
return contracts.ValidationResult{
|
||||
Approved: false,
|
||||
ReasonCode: ReasonCode,
|
||||
Message: diagnostics.Aggregate("invalid scene description shape", issues),
|
||||
CorrectionGuidance: corrections.Guidance("Correct the rejected scene description and return the complete replacement response"),
|
||||
}, nil
|
||||
}
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
@@ -47,38 +53,53 @@ func ValidateForStage(value dnd.SceneDescriptionList, stage string) error {
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
issues, _ := assess(value, exactlyOne)
|
||||
if len(issues) == 0 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%s", diagnostics.Aggregate("invalid scene description shape", issues))
|
||||
}
|
||||
|
||||
func assess(value dnd.SceneDescriptionList, exactlyOne bool) ([]string, diagnostics.Corrections) {
|
||||
issues := make([]string, 0)
|
||||
var corrections diagnostics.Corrections
|
||||
if value.Scenes == nil {
|
||||
issues = append(issues, "scenes must be present")
|
||||
corrections.Add("scene", "Return one scene description for the supplied extraction chunk.", "")
|
||||
} else if len(value.Scenes) == 0 {
|
||||
issues = append(issues, "scenes must not be empty")
|
||||
corrections.Add("scene", "Return one scene description for the supplied extraction chunk.", "")
|
||||
} else if exactlyOne && len(value.Scenes) != 1 {
|
||||
issues = append(issues, "extraction must contain exactly one scene")
|
||||
corrections.Add("scene", "Return exactly one scene description for the supplied extraction chunk.", "")
|
||||
}
|
||||
for index, scene := range value.Scenes {
|
||||
prefix := fmt.Sprintf("scenes[%d]", index)
|
||||
record := fmt.Sprintf("Affected scene titled %s with kind %s, citing %s.", diagnostics.Quote(strings.TrimSpace(scene.Title)), diagnostics.Quote(string(scene.Kind)), diagnostics.SourceRefRange(scene.SourceRef))
|
||||
if strings.TrimSpace(scene.ID) == "" || scene.ID != strings.TrimSpace(scene.ID) {
|
||||
issues = append(issues, prefix+".id must be non-empty and trimmed")
|
||||
corrections.Add("scene", "Return exactly one scene description for the supplied extraction chunk.", record)
|
||||
}
|
||||
if !ValidKind(scene.Kind) {
|
||||
issues = append(issues, prefix+".kind is unsupported: "+diagnostics.Quote(string(scene.Kind)))
|
||||
corrections.Add("kind", "Set `kind` to exactly one of `combat`, `narrative`, `recap`, or `meta`.", record)
|
||||
}
|
||||
if strings.TrimSpace(scene.Title) == "" || scene.Title != strings.TrimSpace(scene.Title) {
|
||||
issues = append(issues, prefix+".title must be non-empty and trimmed")
|
||||
corrections.Add("title", "Provide a concise, nonblank, trimmed title grounded in the supplied scene transcript.", record)
|
||||
}
|
||||
if strings.TrimSpace(scene.Summary) == "" || scene.Summary != strings.TrimSpace(scene.Summary) {
|
||||
issues = append(issues, prefix+".summary must be non-empty and trimmed")
|
||||
corrections.Add("summary", "Provide a concise, nonblank, trimmed summary grounded in the supplied scene transcript.", record)
|
||||
}
|
||||
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")
|
||||
corrections.Add("scene", "Return exactly one scene description for the supplied extraction chunk.", record)
|
||||
}
|
||||
}
|
||||
return issues, corrections
|
||||
}
|
||||
|
||||
func ValidKind(value dnd.SceneKind) bool {
|
||||
switch value {
|
||||
case dnd.SceneKindCombat, dnd.SceneKindNarrative, dnd.SceneKindRecap, dnd.SceneKindMeta:
|
||||
|
||||
Reference in New Issue
Block a user