Preserve context and original causes in HTTP failures
This commit is contained in:
97
context_identity_contract_test.go
Normal file
97
context_identity_contract_test.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package promptkit_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
"time"
|
||||
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
type contextIdentityResponseBody struct {
|
||||
ctx context.Context
|
||||
cause error
|
||||
}
|
||||
|
||||
func (b contextIdentityResponseBody) Read([]byte) (int, error) { <-b.ctx.Done(); return 0, b.cause }
|
||||
func (contextIdentityResponseBody) Close() error { return nil }
|
||||
|
||||
func TestExecutionContextAndOriginalCauseIdentities(t *testing.T) {
|
||||
for _, split := range []bool{false, true} {
|
||||
for _, kind := range []string{"caller cancel", "caller deadline", "body operation deadline"} {
|
||||
t.Run(map[bool]string{false: "run/", true: "prepared/"}[split]+kind, func(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
cause := errors.New("private original cause")
|
||||
ctx, cancel := context.WithCancelCause(context.Background())
|
||||
defer cancel(nil)
|
||||
want := error(context.Canceled)
|
||||
failure := promptkit.ExecutionFailureCanceled
|
||||
if kind == "caller deadline" {
|
||||
var stop context.CancelFunc
|
||||
ctx, stop = context.WithTimeoutCause(context.Background(), time.Second, cause)
|
||||
defer stop()
|
||||
}
|
||||
if kind != "caller cancel" {
|
||||
want = context.DeadlineExceeded
|
||||
failure = promptkit.ExecutionFailureDeadline
|
||||
}
|
||||
cfg := simpleConfig("message")
|
||||
cfg.HTTPClient = &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
||||
if kind == "body operation deadline" {
|
||||
return &http.Response{StatusCode: http.StatusOK, Body: contextIdentityResponseBody{ctx: r.Context(), cause: cause}}, nil
|
||||
}
|
||||
if kind == "caller cancel" {
|
||||
cancel(cause)
|
||||
} else {
|
||||
<-ctx.Done()
|
||||
}
|
||||
return nil, context.Cause(ctx)
|
||||
})}
|
||||
engine, err := promptkit.NewEngine(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var events []promptkit.ExecutionEvent
|
||||
req := simpleGenerationRequest()
|
||||
req.OperationTimeout = time.Minute
|
||||
if kind == "body operation deadline" {
|
||||
req.OperationTimeout = time.Second
|
||||
}
|
||||
req.Observer = func(e promptkit.ExecutionEvent) { events = append(events, e) }
|
||||
var result *promptkit.RunResult
|
||||
if split {
|
||||
handle, prepareErr := engine.PrepareExecution(ctx, req)
|
||||
if prepareErr != nil {
|
||||
t.Fatal(prepareErr)
|
||||
}
|
||||
result, err = engine.RunPrepared(ctx, handle)
|
||||
} else {
|
||||
result, err = engine.Run(ctx, req)
|
||||
}
|
||||
for _, identity := range []error{want, cause, promptkit.ErrLLMGenerate, promptkit.ErrTransport} {
|
||||
if !errors.Is(err, identity) {
|
||||
t.Errorf("error=%v, want identity %v", err, identity)
|
||||
}
|
||||
}
|
||||
if kind != "body operation deadline" {
|
||||
var transportErr *url.Error
|
||||
if !errors.As(err, &transportErr) {
|
||||
t.Errorf("lost HTTP transport error: %v", err)
|
||||
}
|
||||
}
|
||||
if result != nil || errors.Is(err, promptkit.ErrOperationTimeout) != (kind == "body operation deadline") || strings.Contains(err.Error(), cause.Error()) {
|
||||
t.Fatalf("incorrect outcome or disclosure: result=%v error=%v", result, err)
|
||||
}
|
||||
if len(events) != 3 || events[1].Failure != failure || events[2].Kind != promptkit.ExecutionFinished || events[2].Failure != failure {
|
||||
t.Fatalf("attempt/terminal classification = %+v", events)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -159,6 +159,13 @@ caller deadlines, generation deadlines, and whole-request client timeouts
|
||||
retain `context.DeadlineExceeded`. The runner adds its generation category
|
||||
without discarding those identities or depending on HTTP status mapping.
|
||||
|
||||
When a transport failure carries a custom context cancellation cause, the
|
||||
client also retains the standard context identity. It correlates that cause
|
||||
with the returned failure rather than labeling unrelated transport errors from
|
||||
context state alone. Cancellation while reading a successful response body
|
||||
retains the original read failure alongside the context identity within the
|
||||
redacting wrapper.
|
||||
|
||||
## Test Ownership
|
||||
|
||||
The
|
||||
|
||||
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