Preserve native validation failure details
This commit is contained in:
@@ -204,6 +204,8 @@ This stage is one prompt for contracts and sketches, not executor implementation
|
|||||||
|
|
||||||
## Stage 2 — Correct And Share Native Result Usability Handling
|
## Stage 2 — Correct And Share Native Result Usability Handling
|
||||||
|
|
||||||
|
**Status:** Complete.
|
||||||
|
|
||||||
**Goal:** Resolve CAT-007 independently of the broader lifecycle rewrite.
|
**Goal:** Resolve CAT-007 independently of the broader lifecycle rewrite.
|
||||||
|
|
||||||
**Inspect:** `pipeline/producer.go`, combat semantic `validator.go` and tests,
|
**Inspect:** `pipeline/producer.go`, combat semantic `validator.go` and tests,
|
||||||
|
|||||||
40
internal/framework/pipeline/native_result.go
Normal file
40
internal/framework/pipeline/native_result.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package pipeline
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"go.maximumdirect.net/promptkit"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NativeValidationError describes a completed native content check that did
|
||||||
|
// not pass. It is distinct from a native operational error such as failure to
|
||||||
|
// prepare or run validation.
|
||||||
|
type NativeValidationError struct {
|
||||||
|
Validation promptkit.ValidationResult
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *NativeValidationError) Error() string {
|
||||||
|
if len(e.Validation.Errors) == 0 {
|
||||||
|
return "native output did not satisfy its contract"
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("native output did not satisfy its contract: %s", strings.Join(e.Validation.Errors, "; "))
|
||||||
|
}
|
||||||
|
|
||||||
|
// CheckNativeResult preserves an actual native error and rejects an absent or
|
||||||
|
// structurally unusable result before any domain interpretation takes place.
|
||||||
|
func CheckNativeResult(result *promptkit.RunResult, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if result == nil {
|
||||||
|
return errors.New("native execution returned no result")
|
||||||
|
}
|
||||||
|
if result.Validation.Status == promptkit.ValidationFailed || !result.Validation.IsValid {
|
||||||
|
validation := result.Validation
|
||||||
|
validation.Errors = append([]string(nil), validation.Errors...)
|
||||||
|
return &NativeValidationError{Validation: validation}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
31
internal/framework/pipeline/native_result_test.go
Normal file
31
internal/framework/pipeline/native_result_test.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package pipeline
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"go.maximumdirect.net/promptkit"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCheckNativeResultDistinguishesNativeFailureAndContentFailure(t *testing.T) {
|
||||||
|
nativeErr := errors.New("native execution failed")
|
||||||
|
if err := CheckNativeResult(nil, nativeErr); !errors.Is(err, nativeErr) {
|
||||||
|
t.Fatalf("native error = %v", err)
|
||||||
|
}
|
||||||
|
if err := CheckNativeResult(nil, nil); err == nil {
|
||||||
|
t.Fatal("absent native result was usable")
|
||||||
|
}
|
||||||
|
result := &promptkit.RunResult{Validation: promptkit.ValidationResult{Status: promptkit.ValidationFailed, Errors: []string{"missing field"}}}
|
||||||
|
err := CheckNativeResult(result, nil)
|
||||||
|
var failure *NativeValidationError
|
||||||
|
if !errors.As(err, &failure) || errors.Is(err, promptkit.ErrValidation) || len(failure.Validation.Errors) != 1 || failure.Validation.Errors[0] != "missing field" {
|
||||||
|
t.Fatalf("content failure = %v", err)
|
||||||
|
}
|
||||||
|
result.Validation.Errors[0] = "changed"
|
||||||
|
if failure.Validation.Errors[0] != "missing field" {
|
||||||
|
t.Fatal("validation detail was not retained")
|
||||||
|
}
|
||||||
|
if err := CheckNativeResult(&promptkit.RunResult{Validation: promptkit.ValidationResult{Status: promptkit.ValidationPassed, IsValid: true}}, nil); err != nil {
|
||||||
|
t.Fatalf("valid result = %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -64,8 +64,7 @@ func RunProducer[T any](
|
|||||||
failure = canceled
|
failure = canceled
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if result == nil || result.Validation.Status == promptkit.ValidationFailed || !result.Validation.IsValid {
|
if failure = CheckNativeResult(result, nil); failure != nil {
|
||||||
failure = fmt.Errorf("%w: producer output did not satisfy its native contract", promptkit.ErrValidation)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
candidate, guidance, failure = interpret(ctx, result)
|
candidate, guidance, failure = interpret(ctx, result)
|
||||||
@@ -181,7 +180,7 @@ func RunProducer[T any](
|
|||||||
return zero, contracts.ProducerOutcome{}, canceled
|
return zero, contracts.ProducerOutcome{}, canceled
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
failure = err
|
failure = CheckNativeResult(result, err)
|
||||||
} else {
|
} else {
|
||||||
evaluate(result)
|
evaluate(result)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -201,6 +201,26 @@ func TestNativeProducerDoesNotInterpretFailedNativeValidation(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNativeProducerReportsCompletedValidationFailure(t *testing.T) {
|
||||||
|
engine := nativeProducerEngine(t, producerClientFunc(func(context.Context, promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
||||||
|
return &promptkit.GenerateResponse{Content: "not JSON"}, nil
|
||||||
|
}))
|
||||||
|
interpreted := false
|
||||||
|
operation := contracts.ProducerOperation[int]{Controls: contracts.ProducerControls{Mode: contracts.ModeStrict}, Assess: rejectedProducerAssessment}
|
||||||
|
_, _, err := RunProducer(context.Background(), engine, operation, nativeProducerInitial(),
|
||||||
|
func(context.Context, *promptkit.RunResult) (*int, []string, error) {
|
||||||
|
interpreted = true
|
||||||
|
return nil, nil, nil
|
||||||
|
}, nil, nil)
|
||||||
|
var invalid *NativeValidationError
|
||||||
|
if !errors.As(err, &invalid) || errors.Is(err, promptkit.ErrValidation) || len(invalid.Validation.Errors) == 0 || interpreted {
|
||||||
|
t.Fatalf("completed validation failure = %v, interpreted = %t", err, interpreted)
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), invalid.Validation.Errors[0]) {
|
||||||
|
t.Fatalf("native validation detail lost: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestNativeProducerFailureDoesNotRecoverEarlierCandidate(t *testing.T) {
|
func TestNativeProducerFailureDoesNotRecoverEarlierCandidate(t *testing.T) {
|
||||||
failure := errors.New("provider unavailable")
|
failure := errors.New("provider unavailable")
|
||||||
for _, failCorrection := range []bool{false, true} {
|
for _, failCorrection := range []bool{false, true} {
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package semanticreconcile
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"io/fs"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -116,12 +118,20 @@ func TestNativeStructuralFailureIsNotAReconciliationRetry(t *testing.T) {
|
|||||||
// A test-selected native budget gives a terminal result without depending on
|
// A test-selected native budget gives a terminal result without depending on
|
||||||
// the upstream default number of structural repair calls.
|
// the upstream default number of structural repair calls.
|
||||||
zero := 0
|
zero := 0
|
||||||
|
schemaBytes, err := fs.ReadFile(assets.FS(), "generic/normalize/deduplication/schemas/semantic_reconciliation_llm.v1.json")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var schema any
|
||||||
|
if err := json.Unmarshal(schemaBytes, &schema); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
request.Validation = &promptkit.OutputContractInput{
|
request.Validation = &promptkit.OutputContractInput{
|
||||||
Format: promptkit.FormatJSON, ValidationMode: promptkit.ValidationJSONSchema,
|
Format: promptkit.FormatJSON, ValidationMode: promptkit.ValidationJSONSchema,
|
||||||
SchemaPath: "generic/normalize/deduplication/schemas/semantic_reconciliation_llm.v1.json", RepairAttempts: &zero,
|
Schema: schema, RepairAttempts: &zero,
|
||||||
}
|
}
|
||||||
result, err := engine.Run(context.Background(), request)
|
result, err := engine.Run(context.Background(), request)
|
||||||
if err == nil && (result == nil || result.Validation.Status != promptkit.ValidationFailed || result.Validation.IsValid) {
|
if err != nil || result == nil || result.Validation.Status != promptkit.ValidationFailed || result.Validation.IsValid || len(result.Validation.Errors) == 0 {
|
||||||
t.Fatalf("native structural failure lost: %#v %v", result, err)
|
t.Fatalf("native structural failure lost: %#v %v", result, err)
|
||||||
}
|
}
|
||||||
if len(client.requests) != 1 {
|
if len(client.requests) != 1 {
|
||||||
|
|||||||
@@ -70,9 +70,13 @@ func TestNilErrorFailedNativeValidationCannotBecomeApproval(t *testing.T) {
|
|||||||
}
|
}
|
||||||
result, err := validator.Validate(context.Background(), request)
|
result, err := validator.Validate(context.Background(), request)
|
||||||
var unavailable *contracts.ValidationUnavailableError
|
var unavailable *contracts.ValidationUnavailableError
|
||||||
if !errors.As(err, &unavailable) || !errors.Is(err, promptkit.ErrValidation) || result.Approved || result.CorrectionGuidance != "" {
|
var invalid *pipeline.NativeValidationError
|
||||||
|
if !errors.As(err, &unavailable) || !errors.As(err, &invalid) || errors.Is(err, promptkit.ErrValidation) || len(invalid.Validation.Errors) == 0 || result.Approved || result.CorrectionGuidance != "" {
|
||||||
t.Fatalf("failed native contract accepted/interpreted: %#v %v", result, err)
|
t.Fatalf("failed native contract accepted/interpreted: %#v %v", result, err)
|
||||||
}
|
}
|
||||||
|
if !strings.Contains(err.Error(), invalid.Validation.Errors[0]) {
|
||||||
|
t.Fatalf("native validation detail lost: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNestedValidatorUsesTheReleasedNativeBackendAdmission(t *testing.T) {
|
func TestNestedValidatorUsesTheReleasedNativeBackendAdmission(t *testing.T) {
|
||||||
|
|||||||
@@ -67,11 +67,8 @@ func (v *Validator) Validate(ctx context.Context, req contracts.TypedValidationR
|
|||||||
if canceled := ctx.Err(); canceled != nil {
|
if canceled := ctx.Err(); canceled != nil {
|
||||||
return contracts.ValidationResult{}, canceled
|
return contracts.ValidationResult{}, canceled
|
||||||
}
|
}
|
||||||
if err != nil {
|
if nativeErr := pipeline.CheckNativeResult(result, err); nativeErr != nil {
|
||||||
return contracts.ValidationResult{}, unavailable(err)
|
return contracts.ValidationResult{}, unavailable(nativeErr)
|
||||||
}
|
|
||||||
if result == nil || result.Validation.Status == promptkit.ValidationFailed || !result.Validation.IsValid {
|
|
||||||
return contracts.ValidationResult{}, unavailable(validatorErrorf("native output contract failed: %w", promptkit.ErrValidation))
|
|
||||||
}
|
}
|
||||||
var response classificationResponse
|
var response classificationResponse
|
||||||
if err := json.Unmarshal([]byte(result.RawOutput), &response); err != nil {
|
if err := json.Unmarshal([]byte(result.RawOutput), &response); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user