Improve D&D validation reliability
This commit is contained in:
@@ -14,9 +14,10 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
Key = "normalize/dnd/location-registry/identity"
|
||||
ReasonCode = "invalid_location_identity"
|
||||
policy = domainidentity.Policy
|
||||
Key = "normalize/dnd/location-registry/identity"
|
||||
ReasonCode = "invalid_location_identity"
|
||||
policy = domainidentity.Policy
|
||||
correctionPolicy = "dnd.location_registry.validator.identity.v2"
|
||||
)
|
||||
|
||||
type Options struct{}
|
||||
@@ -31,7 +32,7 @@ func (v *Validator) ExecutionClass() contracts.ExecutionClass {
|
||||
return contracts.ExecutionClassDeterministic
|
||||
}
|
||||
func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
return []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}
|
||||
return []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}, {Name: "correction_policy", Value: correctionPolicy}}
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.LocationRegistry]) (contracts.ValidationResult, error) {
|
||||
@@ -43,17 +44,38 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
issues := make([]string, len(identityIssues))
|
||||
var corrections diagnostics.Corrections
|
||||
for index, issue := range identityIssues {
|
||||
issues[index] = fmt.Sprintf("locations[%d] %s: %s", issue.RecordIndex, issue.Code, diagnostics.Quote(issue.Value))
|
||||
if issue.RecordIndex < 0 || issue.RecordIndex >= len(req.Value.Locations) {
|
||||
continue
|
||||
}
|
||||
location := req.Value.Locations[issue.RecordIndex]
|
||||
corrections.Add(string(issue.Code), locationIdentityCorrection(issue.Code), fmt.Sprintf("Affected location %s %s.", diagnostics.Quote(location.Name), diagnostics.SourceRange(location.SourceRefs)))
|
||||
}
|
||||
return contracts.ValidationResult{
|
||||
Approved: false,
|
||||
ReasonCode: ReasonCode,
|
||||
Message: diagnostics.Aggregate("invalid location identity", issues),
|
||||
CorrectionGuidance: "Return one canonical registry entry per distinct proper location name, combining duplicate mentions of the same location.",
|
||||
CorrectionGuidance: corrections.Guidance("Correct the duplicate-group proposals and return the complete replacement proposal response"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func locationIdentityCorrection(code domainidentity.IssueCode) string {
|
||||
switch code {
|
||||
case domainidentity.IssueEmptyCanonicalName:
|
||||
return "Select a canonical proposal member with a nonblank, transcript-supported proper location name."
|
||||
case domainidentity.IssueMissingEvidence:
|
||||
return "Select a canonical proposal member that has direct transcript evidence; do not discard all supported evidence for a location."
|
||||
case domainidentity.IssueInvalidID, domainidentity.IssueIDMismatch:
|
||||
return "Revise the proposal so its canonical location member has a valid name and evidence anchor; Notarius derives durable identity without model input."
|
||||
case domainidentity.IssueDuplicateID:
|
||||
return "Do not use proposals that collapse distinct locations or evidence anchors into one canonical identity; group only records that describe the same location."
|
||||
default:
|
||||
return "Revise the duplicate-group proposal so every canonical location is transcript-supported and distinct."
|
||||
}
|
||||
}
|
||||
|
||||
func Spec() pipeline.ValidatorSpec {
|
||||
return pipeline.ValidatorSpec{Key: Key, ExecutionClass: contracts.ExecutionClassDeterministic}
|
||||
}
|
||||
|
||||
@@ -47,10 +47,13 @@ func TestValidatorDefersShapeAndRejectsDerivationAndDuplicateID(t *testing.T) {
|
||||
t.Fatalf("message %q missing %q", result.Message, want)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(result.CorrectionGuidance, "duplicate-group proposals") || !strings.Contains(result.CorrectionGuidance, "Gate") || strings.Contains(result.CorrectionGuidance, "not-an-id") || strings.Contains(result.CorrectionGuidance, "locations[") {
|
||||
t.Fatalf("CorrectionGuidance = %q, want contextual proposal guidance without IDs or operator paths", result.CorrectionGuidance)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatorRegistersIdentityPolicy(t *testing.T) {
|
||||
if got := New(Options{}).CheckpointFingerprints(); len(got) != 1 || got[0].Value != domainidentity.Policy {
|
||||
if got := New(Options{}).CheckpointFingerprints(); len(got) != 2 || got[0].Value != domainidentity.Policy || got[1].Name != "correction_policy" || got[1].Value != correctionPolicy {
|
||||
t.Fatalf("fingerprints = %#v", got)
|
||||
}
|
||||
registry := pipeline.NewValidatorRegistry()
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
const (
|
||||
Key = "extract/dnd/location-registry/shape"
|
||||
ReasonCode = "invalid_location_shape"
|
||||
policy = "dnd.location_registry.validator.shape.v1"
|
||||
policy = "dnd.location_registry.validator.shape.v2"
|
||||
)
|
||||
|
||||
type Options struct{}
|
||||
@@ -34,39 +34,58 @@ func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.LocationRegistry]) (contracts.ValidationResult, error) {
|
||||
issues := issuesFor(req.Value)
|
||||
normalization := req.Stage == string(pipeline.StageNormalize)
|
||||
issues, corrections := assess(req.Value, normalization)
|
||||
if len(issues) > 0 {
|
||||
return rejection(diagnostics.Aggregate("invalid location shape", issues)), nil
|
||||
prefix := "Correct every rejected location and return the complete replacement registry"
|
||||
if normalization {
|
||||
prefix = "Correct the duplicate-group proposals and return the complete replacement proposal response"
|
||||
}
|
||||
return rejection(diagnostics.Aggregate("invalid location shape", issues), corrections.Guidance(prefix)), nil
|
||||
}
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
|
||||
func Validate(value dnd.LocationRegistry) error {
|
||||
issues := issuesFor(value)
|
||||
issues, _ := assess(value, false)
|
||||
if len(issues) == 0 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%s", diagnostics.Aggregate("invalid location shape", issues))
|
||||
}
|
||||
|
||||
func issuesFor(value dnd.LocationRegistry) []string {
|
||||
func assess(value dnd.LocationRegistry, normalization bool) ([]string, diagnostics.Corrections) {
|
||||
var corrections diagnostics.Corrections
|
||||
nameRule := "Provide one nonblank, transcript-supported proper location name for every registry entry."
|
||||
refRule := "Provide at least one transcript source range that directly supports every named location."
|
||||
listRule := "Return a `locations` array; use an empty array when the transcript establishes no named locations."
|
||||
if normalization {
|
||||
nameRule = "Revise the duplicate-group proposals so every selected canonical location has a nonblank, transcript-supported proper name."
|
||||
refRule = "Revise the duplicate-group proposals so every selected canonical location preserves direct transcript evidence."
|
||||
listRule = "Revise the duplicate-group proposals so normalization retains the complete location candidate registry."
|
||||
}
|
||||
if value.Locations == nil {
|
||||
return []string{"locations must be present"}
|
||||
corrections.Add("list", listRule, "")
|
||||
return []string{"locations must be present"}, corrections
|
||||
}
|
||||
issues := make([]string, 0)
|
||||
for index, location := range value.Locations {
|
||||
prefix := fmt.Sprintf("locations[%d]", index)
|
||||
record := "Affected location " + diagnostics.Quote(strings.TrimSpace(location.Name)) + " " + diagnostics.SourceRange(location.SourceRefs) + "."
|
||||
if strings.TrimSpace(location.ID) == "" {
|
||||
issues = append(issues, prefix+".id must not be empty")
|
||||
corrections.Add("name", nameRule, record)
|
||||
}
|
||||
if strings.TrimSpace(location.Name) == "" {
|
||||
issues = append(issues, prefix+".name must not be empty")
|
||||
corrections.Add("name", nameRule, record)
|
||||
}
|
||||
if len(location.SourceRefs) == 0 {
|
||||
issues = append(issues, prefix+".source_refs must not be empty")
|
||||
corrections.Add("source-refs", refRule, record)
|
||||
}
|
||||
}
|
||||
return issues
|
||||
return issues, corrections
|
||||
}
|
||||
|
||||
func Spec() pipeline.ValidatorSpec {
|
||||
@@ -92,6 +111,6 @@ func DecodeOptions(options map[string]any) (Options, error) {
|
||||
|
||||
func validateOptions(options map[string]any) error { _, err := DecodeOptions(options); return err }
|
||||
|
||||
func rejection(message string) contracts.ValidationResult {
|
||||
return contracts.ValidationResult{Approved: false, ReasonCode: ReasonCode, Message: message, CorrectionGuidance: "Return a complete location registry containing only properly named locations with valid source references."}
|
||||
func rejection(message, guidance string) contracts.ValidationResult {
|
||||
return contracts.ValidationResult{Approved: false, ReasonCode: ReasonCode, Message: message, CorrectionGuidance: guidance}
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
locationshape "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/locationregistry/shape"
|
||||
)
|
||||
@@ -16,7 +17,7 @@ import (
|
||||
const (
|
||||
Key = "extract/dnd/location-registry/source_refs"
|
||||
ReasonCode = "invalid_location_source_refs"
|
||||
policy = "dnd.location_registry.validator.source_refs.v2"
|
||||
policy = "dnd.location_registry.validator.source_refs.v3"
|
||||
)
|
||||
|
||||
type Options struct{}
|
||||
@@ -42,35 +43,37 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
index := source.NewDocumentIndex(req.Source)
|
||||
coverage := shared.NewChunkCoverage(req.Chunk)
|
||||
issues := make([]string, 0)
|
||||
var corrections diagnostics.Corrections
|
||||
normalization := req.Stage == string(pipeline.StageNormalize)
|
||||
validRangeRule := "Use positive source range endpoints that occur in the supplied transcript, with the earlier unit first."
|
||||
guidancePrefix := "Correct every rejected location citation and return the complete replacement location registry"
|
||||
if normalization {
|
||||
validRangeRule = "Revise the duplicate-group proposals so every selected canonical location preserves valid transcript evidence; do not reproduce application IDs."
|
||||
guidancePrefix = "Correct the duplicate-group proposals and return the complete replacement proposal response"
|
||||
}
|
||||
for locationIndex, location := range req.Value.Locations {
|
||||
for refIndex, ref := range location.SourceRefs {
|
||||
record := fmt.Sprintf("Affected location %s, citing %s.", diagnostics.Quote(location.Name), diagnostics.SourceRefRange(ref))
|
||||
if err := index.ValidateRef(ref); err != nil {
|
||||
issues = append(issues, fmt.Sprintf("locations[%d].source_refs[%d]: %s", locationIndex, refIndex, diagnostics.Truncate(err.Error())))
|
||||
corrections.Add("valid-range", validRangeRule, record)
|
||||
continue
|
||||
}
|
||||
if req.Stage == string(pipeline.StageExtract) && !chunkContainsRef(req.Chunk, ref) {
|
||||
if req.Stage == string(pipeline.StageExtract) && !coverage.Contains(index, req.Source, ref) {
|
||||
issues = append(issues, fmt.Sprintf("locations[%d].source_refs[%d]: source reference is outside the current extraction chunk", locationIndex, refIndex))
|
||||
corrections.Add("chunk-range", "Use only source ranges wholly contained in the supplied extraction chunk.", record)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(issues) == 0 {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
return rejection(diagnostics.Aggregate("invalid location source references", issues)), nil
|
||||
}
|
||||
|
||||
func chunkContainsRef(chunk *source.Chunk, ref source.SourceRef) bool {
|
||||
if chunk == nil || ref.SourceID != chunk.SourceID {
|
||||
return false
|
||||
}
|
||||
startFound := false
|
||||
endFound := false
|
||||
for _, unit := range chunk.Units {
|
||||
startFound = startFound || unit.ID == ref.StartUnitID
|
||||
endFound = endFound || unit.ID == ref.EndUnitID
|
||||
}
|
||||
return startFound && endFound
|
||||
return rejection(
|
||||
diagnostics.Aggregate("invalid location source references", issues),
|
||||
corrections.Guidance(guidancePrefix),
|
||||
), nil
|
||||
}
|
||||
|
||||
func Spec() pipeline.ValidatorSpec {
|
||||
@@ -96,6 +99,6 @@ func DecodeOptions(options map[string]any) (Options, error) {
|
||||
|
||||
func validateOptions(options map[string]any) error { _, err := DecodeOptions(options); return err }
|
||||
|
||||
func rejection(message string) contracts.ValidationResult {
|
||||
return contracts.ValidationResult{Approved: false, ReasonCode: ReasonCode, Message: message, CorrectionGuidance: "Return registry locations whose source references identify valid transcript ranges within the supplied extraction chunk and directly support each proper location name."}
|
||||
func rejection(message, guidance string) contracts.ValidationResult {
|
||||
return contracts.ValidationResult{Approved: false, ReasonCode: ReasonCode, Message: message, CorrectionGuidance: guidance}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user