Migrate item normalization to native correction and explicit fallback handling
This commit is contained in:
@@ -155,7 +155,7 @@ func RunProducer[T any](
|
||||
if fallback != nil {
|
||||
construct = func(ctx context.Context) (T, error) { return fallback(ctx, candidate) }
|
||||
}
|
||||
return finishProducerFailure(ctx, operation.Controls.Mode, failure, construct)
|
||||
return FinishProducerFailure(ctx, operation.Controls.Mode, failure, construct)
|
||||
}
|
||||
|
||||
// producerRequest applies native options to a value copy. The invocation
|
||||
|
||||
@@ -8,9 +8,9 @@ import (
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
)
|
||||
|
||||
// finishProducerFailure applies the resolved terminal mode without retrying
|
||||
// FinishProducerFailure applies the resolved terminal mode without retrying
|
||||
// production or running ordinary validators against a supported fallback.
|
||||
func finishProducerFailure[T any](ctx context.Context, mode contracts.ExecutionMode, cause error, fallback func(context.Context) (T, error)) (T, contracts.ProducerOutcome, error) {
|
||||
func FinishProducerFailure[T any](ctx context.Context, mode contracts.ExecutionMode, cause error, fallback func(context.Context) (T, error)) (T, contracts.ProducerOutcome, error) {
|
||||
var zero T
|
||||
if ctx == nil {
|
||||
return zero, contracts.ProducerOutcome{}, errors.New("producer context must not be nil")
|
||||
@@ -40,3 +40,30 @@ func finishProducerFailure[T any](ctx context.Context, mode contracts.ExecutionM
|
||||
return zero, contracts.ProducerOutcome{}, fmt.Errorf("unsupported producer mode %q", mode)
|
||||
}
|
||||
}
|
||||
|
||||
// AssessProducer assesses a no-model result once, including deterministic
|
||||
// short-circuits inside otherwise model-backed modules. It never constructs a
|
||||
// native request or gives deterministic work a correction loop.
|
||||
func AssessProducer[T any](ctx context.Context, operation contracts.ProducerOperation[T], value T, fallback func(context.Context) (T, error)) (T, contracts.ProducerOutcome, error) {
|
||||
var zero T
|
||||
if ctx == nil {
|
||||
return zero, contracts.ProducerOutcome{}, errors.New("producer context must not be nil")
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return zero, contracts.ProducerOutcome{}, err
|
||||
}
|
||||
if operation.Assess == nil {
|
||||
return zero, contracts.ProducerOutcome{}, errors.New("producer assessment must be supplied")
|
||||
}
|
||||
assessment, err := operation.Assess(ctx, value)
|
||||
if err != nil {
|
||||
return zero, contracts.ProducerOutcome{}, err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return zero, contracts.ProducerOutcome{}, err
|
||||
}
|
||||
if !assessment.Accepted() {
|
||||
return FinishProducerFailure(ctx, operation.Controls.Mode, assessment.Err(), fallback)
|
||||
}
|
||||
return value, contracts.ProducerOutcome{Disposition: contracts.ProducerAccepted, ReuseEligible: operation.ReuseEligible}, nil
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ func TestProducerTerminalModeControlsSupportedFallback(t *testing.T) {
|
||||
failure := errors.New("candidate not accepted")
|
||||
for _, mode := range []contracts.ExecutionMode{contracts.ModeStrict, contracts.ModePermissive} {
|
||||
calls := 0
|
||||
value, outcome, err := finishProducerFailure(context.Background(), mode, failure, func(context.Context) (string, error) { calls++; return "supported", nil })
|
||||
value, outcome, err := FinishProducerFailure(context.Background(), mode, failure, func(context.Context) (string, error) { calls++; return "supported", nil })
|
||||
if mode == contracts.ModeStrict {
|
||||
if !errors.Is(err, failure) || calls != 0 || value != "" || outcome.Disposition != "" {
|
||||
t.Fatalf("strict = %q %#v %v, calls %d", value, outcome, err, calls)
|
||||
@@ -25,11 +25,11 @@ func TestProducerTerminalModeControlsSupportedFallback(t *testing.T) {
|
||||
|
||||
func TestProducerFallbackCannotHideConstructionFailure(t *testing.T) {
|
||||
original, construction := errors.New("original failure"), errors.New("no invariant-preserving fallback")
|
||||
_, outcome, err := finishProducerFailure(context.Background(), contracts.ModePermissive, original, func(context.Context) (string, error) { return "", construction })
|
||||
_, outcome, err := FinishProducerFailure(context.Background(), contracts.ModePermissive, original, func(context.Context) (string, error) { return "", construction })
|
||||
if !errors.Is(err, original) || !errors.Is(err, construction) || outcome.Disposition != "" {
|
||||
t.Fatalf("outcome = %#v, error = %v", outcome, err)
|
||||
}
|
||||
if _, _, err := finishProducerFailure[string](context.Background(), contracts.ModePermissive, original, nil); !errors.Is(err, original) {
|
||||
if _, _, err := FinishProducerFailure[string](context.Background(), contracts.ModePermissive, original, nil); !errors.Is(err, original) {
|
||||
t.Fatalf("unsupported fallback = %v", err)
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,7 @@ func TestProducerCancellationNeverDeliversFallback(t *testing.T) {
|
||||
if !cancelDuringFallback {
|
||||
cancel()
|
||||
}
|
||||
value, outcome, err := finishProducerFailure(ctx, contracts.ModePermissive, errors.New("failure"), func(context.Context) (string, error) {
|
||||
value, outcome, err := FinishProducerFailure(ctx, contracts.ModePermissive, errors.New("failure"), func(context.Context) (string, error) {
|
||||
calls++
|
||||
cancel()
|
||||
return "not delivered", nil
|
||||
@@ -57,8 +57,54 @@ func TestProducerCancellationNeverDeliversFallback(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestProducerLocalTimeoutDoesNotBecomeCallerCancellation(t *testing.T) {
|
||||
value, outcome, err := finishProducerFailure(context.Background(), contracts.ModePermissive, context.DeadlineExceeded, func(context.Context) (string, error) { return "supported", nil })
|
||||
value, outcome, err := FinishProducerFailure(context.Background(), contracts.ModePermissive, context.DeadlineExceeded, func(context.Context) (string, error) { return "supported", nil })
|
||||
if err != nil || value != "supported" || outcome.Disposition != contracts.ProducerFallback {
|
||||
t.Fatalf("local timeout = %q %#v %v", value, outcome, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoModelProducerIsAssessedOnceWithoutRevalidatingFallback(t *testing.T) {
|
||||
for _, approve := range []bool{true, false} {
|
||||
for _, mode := range []contracts.ExecutionMode{contracts.ModeStrict, contracts.ModePermissive} {
|
||||
assessed, fallbacks := 0, 0
|
||||
operation := contracts.ProducerOperation[string]{
|
||||
Controls: contracts.ProducerControls{Mode: mode}, ReuseEligible: true,
|
||||
Assess: func(context.Context, string) (contracts.CandidateAssessment, error) {
|
||||
assessed++
|
||||
if approve {
|
||||
return contracts.CandidateAssessment{}, nil
|
||||
}
|
||||
return contracts.CandidateAssessment{Validators: []contracts.ValidatorAssessment{{ValidatorName: "policy", Decision: contracts.AssessmentRejected, Result: contracts.ValidationResult{Message: "not accepted"}}}}, nil
|
||||
},
|
||||
}
|
||||
value, outcome, err := AssessProducer(context.Background(), operation, "candidate", func(context.Context) (string, error) { fallbacks++; return "supported", nil })
|
||||
if assessed != 1 {
|
||||
t.Fatalf("assessed %d times", assessed)
|
||||
}
|
||||
if approve {
|
||||
if err != nil || value != "candidate" || outcome.Disposition != contracts.ProducerAccepted || !outcome.ReuseEligible || fallbacks != 0 {
|
||||
t.Fatalf("acceptance = %q %#v %v", value, outcome, err)
|
||||
}
|
||||
} else if mode == contracts.ModeStrict {
|
||||
if err == nil || value != "" || outcome.Disposition != "" || fallbacks != 0 {
|
||||
t.Fatalf("strict failure = %q %#v %v", value, outcome, err)
|
||||
}
|
||||
} else if err != nil || value != "supported" || outcome.Disposition != contracts.ProducerFallback || outcome.ReuseEligible || fallbacks != 1 {
|
||||
t.Fatalf("fallback = %q %#v %v", value, outcome, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoModelProducerStopsAfterAssessmentCancellation(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
operation := contracts.ProducerOperation[int]{Controls: contracts.ProducerControls{Mode: contracts.ModePermissive},
|
||||
Assess: func(context.Context, int) (contracts.CandidateAssessment, error) {
|
||||
cancel()
|
||||
return contracts.CandidateAssessment{}, nil
|
||||
}}
|
||||
_, outcome, err := AssessProducer(ctx, operation, 1, func(context.Context) (int, error) { t.Fatal("fallback after cancellation"); return 0, nil })
|
||||
if !errors.Is(err, context.Canceled) || outcome.Disposition != "" {
|
||||
t.Fatalf("cancellation = %#v %v", outcome, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,17 +17,7 @@ func completeProducerOperation[T any](ctx context.Context, class contracts.Execu
|
||||
return zero, contracts.ProducerOutcome{}, err
|
||||
}
|
||||
if class == contracts.ExecutionClassDeterministic {
|
||||
assessment, err := operation.Assess(ctx, value)
|
||||
if err != nil {
|
||||
return zero, contracts.ProducerOutcome{}, err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return zero, contracts.ProducerOutcome{}, err
|
||||
}
|
||||
if !assessment.Accepted() {
|
||||
return finishProducerFailure(ctx, operation.Controls.Mode, assessment.Err(), fallback)
|
||||
}
|
||||
outcome = contracts.ProducerOutcome{Disposition: contracts.ProducerAccepted, ReuseEligible: operation.ReuseEligible}
|
||||
return AssessProducer(ctx, operation, value, fallback)
|
||||
} else if class != contracts.ExecutionClassLLMBacked {
|
||||
return zero, contracts.ProducerOutcome{}, fmt.Errorf("unsupported producer execution class %q", class)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user