Honor cancellation after recording native evaluation evidence
This commit is contained in:
@@ -191,6 +191,9 @@ func executeNative[T any](ctx context.Context, execution ExecutionContext[T], op
|
||||
} else {
|
||||
evaluate(result)
|
||||
}
|
||||
if canceled := ctx.Err(); canceled != nil {
|
||||
return empty, errors.Join(canceled, fatal)
|
||||
}
|
||||
if fatal != nil {
|
||||
return empty, fatal
|
||||
}
|
||||
|
||||
@@ -253,3 +253,56 @@ func TestNativeProducerOperationCancellationStopsDelivery(t *testing.T) {
|
||||
t.Fatalf("result=%#v err=%v calls=%d", result, err, calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNativeProducerOperationCancellationDuringEvaluationCapture(t *testing.T) {
|
||||
for _, failCapture := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("capture_failure=%t", failCapture), func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
captureErr := errors.New("evaluation capture failed")
|
||||
evidence := &nativeEvidence{fail: func(name string) error {
|
||||
if strings.HasPrefix(name, "interpretations/") {
|
||||
cancel()
|
||||
if failCapture {
|
||||
return captureErr
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}}
|
||||
calls, assessments := 0, 0
|
||||
engine := nativeProducerEngine(t, producerClientFunc(func(context.Context, promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
||||
calls++
|
||||
return &promptkit.GenerateResponse{Content: `{"value":1}`}, nil
|
||||
}))
|
||||
result, err := ExecuteOperation(ctx, ExecutionContext[int]{
|
||||
Engine: engine,
|
||||
// No cache write can provide an incidental cancellation check.
|
||||
Controls: contracts.ProducerControls{Mode: contracts.ModePermissive, Cache: contracts.CacheBypass, MaxAdditionalTurns: 1},
|
||||
Execution: contracts.ExecutionOptions{Capture: evidence},
|
||||
Assess: func(ctx context.Context, value int) (contracts.CandidateAssessment, error) {
|
||||
assessments++
|
||||
return acceptedProducerAssessment(ctx, value)
|
||||
},
|
||||
}, func(context.Context) (contracts.PreparedOperation[int], error) {
|
||||
return contracts.NativeOperation[int]{Initial: nativeProducerInitial(), Interpret: interpretNumberEvaluation,
|
||||
Correction: func(context.Context, []string) (promptkit.RunRequest, error) {
|
||||
t.Fatal("cancellation invoked correction")
|
||||
return promptkit.RunRequest{}, nil
|
||||
},
|
||||
Fallback: func(context.Context, contracts.Evaluation[int]) (int, error) {
|
||||
t.Fatal("cancellation invoked fallback")
|
||||
return 0, nil
|
||||
}}, nil
|
||||
})
|
||||
if !errors.Is(err, context.Canceled) || result.Outcome.Disposition != "" || result.Evaluation != nil || result.Value != 0 || calls != 1 || assessments != 1 {
|
||||
t.Fatalf("result=%#v err=%v calls=%d assessments=%d", result, err, calls, assessments)
|
||||
}
|
||||
if failCapture {
|
||||
var captured *contracts.CaptureError
|
||||
if !errors.Is(err, captureErr) || !errors.As(err, &captured) {
|
||||
t.Fatalf("capture failure was not retained: %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user