Preserve context and original causes in HTTP failures
This commit is contained in:
50
internal/llm/context_identity_test.go
Normal file
50
internal/llm/context_identity_test.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.maximumdirect.net/promptkit/internal/domain"
|
||||
)
|
||||
|
||||
func TestTransportDoesNotRelabelIndependentFailureAfterCancellation(t *testing.T) {
|
||||
ctx, cancel := context.WithCancelCause(context.Background())
|
||||
defer cancel(nil)
|
||||
independent := errors.New("independent transport failure")
|
||||
client := NewOpenAICompatibleClient(OpenAICompatibleConfig{HTTPClient: &http.Client{Transport: roundTripFunc(func(*http.Request) (*http.Response, error) {
|
||||
cancel(errors.New("unrelated caller cause"))
|
||||
return nil, independent
|
||||
})}})
|
||||
_, err := client.Generate(ctx, contextIdentityRequest())
|
||||
if !errors.Is(err, independent) || errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("unrelated failure relabeled: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
type contextIdentityBody struct {
|
||||
cancel context.CancelFunc
|
||||
cause error
|
||||
}
|
||||
|
||||
func (b contextIdentityBody) Read([]byte) (int, error) { b.cancel(); return 0, b.cause }
|
||||
func (contextIdentityBody) Close() error { return nil }
|
||||
|
||||
func TestResponseReadCancellationRetainsOriginalCause(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
cause := errors.New("private body failure")
|
||||
client := NewOpenAICompatibleClient(OpenAICompatibleConfig{HTTPClient: &http.Client{Transport: roundTripFunc(func(*http.Request) (*http.Response, error) {
|
||||
return &http.Response{StatusCode: http.StatusOK, Body: contextIdentityBody{cancel: cancel, cause: cause}}, nil
|
||||
})}})
|
||||
_, err := client.Generate(ctx, contextIdentityRequest())
|
||||
if !errors.Is(err, context.Canceled) || !errors.Is(err, cause) || !errors.Is(err, ErrTransport) || strings.Contains(err.Error(), cause.Error()) {
|
||||
t.Fatalf("lost body cause or redaction: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func contextIdentityRequest() domain.GenerateRequest {
|
||||
return domain.GenerateRequest{Target: domain.ExecutionTarget{Endpoint: "http://example.test/v1", Model: "test"}}
|
||||
}
|
||||
@@ -220,6 +220,13 @@ func (p *PreparedGeneration) execute(ctx context.Context, captureEvidence bool)
|
||||
outcome.StatusCode = httpResp.StatusCode
|
||||
}
|
||||
if err != nil {
|
||||
// net/http can return the context's custom cause instead of ctx.Err().
|
||||
// Add the standard identity only when the transport failure carries that
|
||||
// cause; an unrelated failure must not acquire a later cancellation.
|
||||
if contextErr := requestContext.Err(); contextErr != nil &&
|
||||
!errors.Is(err, contextErr) && errors.Is(err, context.Cause(requestContext)) {
|
||||
err = errors.Join(contextErr, err)
|
||||
}
|
||||
outcome.Err = &requestFailedError{cause: err}
|
||||
return outcome
|
||||
}
|
||||
@@ -234,7 +241,9 @@ func (p *PreparedGeneration) execute(ctx context.Context, captureEvidence bool)
|
||||
httpResp.ContentLength,
|
||||
httpResp.Body,
|
||||
)
|
||||
if err := observedContextError(requestContext, ctx, outcome.Err); err != nil {
|
||||
// The provider status is already captured separately. Context failure
|
||||
// supersedes it, rather than nesting a provider-status error as transport.
|
||||
if err := observedContextError(requestContext, ctx, nil); err != nil {
|
||||
outcome.Err = err
|
||||
}
|
||||
return outcome
|
||||
@@ -300,10 +309,10 @@ func (p *PreparedGeneration) execute(ctx context.Context, captureEvidence bool)
|
||||
|
||||
func observedContextError(attempt, caller context.Context, cause error) error {
|
||||
if err := caller.Err(); err != nil {
|
||||
return &requestFailedError{cause: err}
|
||||
return &requestFailedError{cause: errors.Join(err, cause)}
|
||||
}
|
||||
if err := attempt.Err(); err != nil {
|
||||
return &requestFailedError{cause: err}
|
||||
return &requestFailedError{cause: errors.Join(err, cause)}
|
||||
}
|
||||
if errors.Is(cause, context.Canceled) || errors.Is(cause, context.DeadlineExceeded) {
|
||||
return &requestFailedError{cause: cause}
|
||||
|
||||
Reference in New Issue
Block a user