Centralize validator classification and malformed output handling
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package metadata
|
||||
|
||||
import "gitea.maximumdirect.net/eric/audita/internal/framework/contracts"
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/internal/framework/contracts"
|
||||
)
|
||||
|
||||
type ExecutionClass string
|
||||
|
||||
@@ -9,6 +13,31 @@ const (
|
||||
ExecutionClassLLMBacked ExecutionClass = "llm_backed"
|
||||
)
|
||||
|
||||
const (
|
||||
KeyProposalShape = "proposal_shape"
|
||||
KeyConfidenceThreshold = "confidence_threshold"
|
||||
KeyOriginalTextPresence = "original_text_presence"
|
||||
KeyNonEmptyCorrectedText = "non_empty_corrected_text"
|
||||
KeyNoEffect = "no_effect"
|
||||
KeyProtectedTerms = "protected_terms"
|
||||
|
||||
KeySpokenFormPlausibility = "spoken_form_plausibility"
|
||||
KeyMeaningReversalReview = "meaning_reversal_review"
|
||||
KeyEditorialReview = "editorial_review"
|
||||
)
|
||||
|
||||
var executionClassByKey = map[string]ExecutionClass{
|
||||
KeyProposalShape: ExecutionClassDeterministic,
|
||||
KeyConfidenceThreshold: ExecutionClassDeterministic,
|
||||
KeyOriginalTextPresence: ExecutionClassDeterministic,
|
||||
KeyNonEmptyCorrectedText: ExecutionClassDeterministic,
|
||||
KeyNoEffect: ExecutionClassDeterministic,
|
||||
KeyProtectedTerms: ExecutionClassDeterministic,
|
||||
KeySpokenFormPlausibility: ExecutionClassLLMBacked,
|
||||
KeyMeaningReversalReview: ExecutionClassLLMBacked,
|
||||
KeyEditorialReview: ExecutionClassLLMBacked,
|
||||
}
|
||||
|
||||
type ClassifiedValidator interface {
|
||||
contracts.Validator
|
||||
ExecutionClass() ExecutionClass
|
||||
@@ -18,9 +47,11 @@ func ClassOf(v contracts.Validator) ExecutionClass {
|
||||
if v == nil {
|
||||
return ExecutionClassDeterministic
|
||||
}
|
||||
|
||||
classFromKey := ClassForKey(v.Name())
|
||||
classified, ok := v.(ClassifiedValidator)
|
||||
if !ok {
|
||||
return ExecutionClassDeterministic
|
||||
return classFromKey
|
||||
}
|
||||
switch classified.ExecutionClass() {
|
||||
case ExecutionClassLLMBacked:
|
||||
@@ -28,8 +59,16 @@ func ClassOf(v contracts.Validator) ExecutionClass {
|
||||
case ExecutionClassDeterministic:
|
||||
return ExecutionClassDeterministic
|
||||
default:
|
||||
return classFromKey
|
||||
}
|
||||
}
|
||||
|
||||
func ClassForKey(key string) ExecutionClass {
|
||||
class, ok := executionClassByKey[strings.TrimSpace(key)]
|
||||
if !ok {
|
||||
return ExecutionClassDeterministic
|
||||
}
|
||||
return class
|
||||
}
|
||||
|
||||
func Wrap(v contracts.Validator, class ExecutionClass) contracts.Validator {
|
||||
|
||||
@@ -16,6 +16,16 @@ func (u unclassifiedValidator) Validate(_ context.Context, _ contracts.Validatio
|
||||
return frameworkvalidators.Result{ValidatorName: u.Name(), Decisions: nil}, nil
|
||||
}
|
||||
|
||||
type namedUnclassifiedValidator struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func (n namedUnclassifiedValidator) Name() string { return n.name }
|
||||
|
||||
func (n namedUnclassifiedValidator) Validate(_ context.Context, _ contracts.ValidationRequest) (frameworkvalidators.Result, error) {
|
||||
return frameworkvalidators.Result{ValidatorName: n.Name(), Decisions: nil}, nil
|
||||
}
|
||||
|
||||
func TestClassOfDefaultsToDeterministic(t *testing.T) {
|
||||
if got := ClassOf(unclassifiedValidator{}); got != ExecutionClassDeterministic {
|
||||
t.Fatalf("expected deterministic default class, got %q", got)
|
||||
@@ -28,3 +38,22 @@ func TestWrapExposesExecutionClass(t *testing.T) {
|
||||
t.Fatalf("expected llm_backed class, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassForKey(t *testing.T) {
|
||||
if got := ClassForKey(KeyProposalShape); got != ExecutionClassDeterministic {
|
||||
t.Fatalf("expected deterministic class for %q, got %q", KeyProposalShape, got)
|
||||
}
|
||||
if got := ClassForKey(KeySpokenFormPlausibility); got != ExecutionClassLLMBacked {
|
||||
t.Fatalf("expected llm_backed class for %q, got %q", KeySpokenFormPlausibility, got)
|
||||
}
|
||||
if got := ClassForKey("unknown"); got != ExecutionClassDeterministic {
|
||||
t.Fatalf("expected deterministic fallback for unknown key, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassOfFallsBackToStableValidatorKey(t *testing.T) {
|
||||
v := namedUnclassifiedValidator{name: KeyEditorialReview}
|
||||
if got := ClassOf(v); got != ExecutionClassLLMBacked {
|
||||
t.Fatalf("expected llm_backed fallback by key, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user