From bcdd6fe06cbe61d77b64be5c8c7f0a3c80066af0 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Thu, 19 Nov 2026 13:44:42 +0000 Subject: [PATCH] Preserve operation timeout identity during HTTP generation --- execution_timeout_contract_test.go | 67 +++++++++++++++++++++++++++++ internal/usecase/operation_scope.go | 5 ++- 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/execution_timeout_contract_test.go b/execution_timeout_contract_test.go index a64c296..979c8e0 100644 --- a/execution_timeout_contract_test.go +++ b/execution_timeout_contract_test.go @@ -3,6 +3,9 @@ package promptkit_test import ( "context" "errors" + "net/http" + "net/http/httptest" + "net/url" "testing" "testing/synctest" "time" @@ -10,6 +13,70 @@ import ( "go.maximumdirect.net/promptkit" ) +func TestBuiltInGenerationOperationTimeout(t *testing.T) { + for _, split := range []bool{false, true} { + t.Run(map[bool]string{false: "run", true: "prepared"}[split], func(t *testing.T) { + started := make(chan struct{}, 1) + release := make(chan struct{}) + provider := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { + select { + case started <- struct{}{}: + default: + } + <-release + })) + defer provider.Close() + defer close(release) + + config := simpleConfig("message") + zero := 0 + config.TransientRetries = &zero + config.HTTPClient = provider.Client() + engine, err := promptkit.NewEngine(config) + if err != nil { + t.Fatal(err) + } + var terminal promptkit.ExecutionEvent + req := promptkit.RunRequest{PromptID: "prompt", OperationTimeout: time.Second, + Execution: &promptkit.ExecutionTargetOverride{Endpoint: provider.URL + "/v1", TimeoutSeconds: &zero}, + Observer: func(event promptkit.ExecutionEvent) { + if event.Kind == promptkit.ExecutionFinished { + terminal = event + } + }, + } + parent := context.Background() + var result *promptkit.RunResult + if split { + handle, prepareErr := engine.PrepareExecution(parent, req) + if prepareErr != nil { + t.Fatal(prepareErr) + } + result, err = engine.RunPrepared(parent, handle) + } else { + result, err = engine.Run(parent, req) + } + select { + case <-started: + default: + t.Fatal("operation expired before provider received the request") + } + for _, target := range []error{promptkit.ErrOperationTimeout, context.DeadlineExceeded, promptkit.ErrLLMGenerate, promptkit.ErrTransport} { + if !errors.Is(err, target) { + t.Errorf("error=%v, want identity %v", err, target) + } + } + var transportErr *url.Error + if !errors.As(err, &transportErr) { + t.Errorf("error=%v, want retained HTTP transport cause", err) + } + if result != nil || parent.Err() != nil || terminal.Failure != promptkit.ExecutionFailureDeadline { + t.Fatalf("result=%v parent=%v terminal=%+v", result, parent.Err(), terminal) + } + }) + } +} + func TestRunOperationTimeoutAttribution(t *testing.T) { synctest.Test(t, func(t *testing.T) { engine := observationEngine(t, observationClientFunc(func(ctx context.Context, _ promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) { diff --git a/internal/usecase/operation_scope.go b/internal/usecase/operation_scope.go index c1aca0b..a9c2cab 100644 --- a/internal/usecase/operation_scope.go +++ b/internal/usecase/operation_scope.go @@ -3,6 +3,7 @@ package usecase import ( "context" "errors" + "fmt" "time" ) @@ -17,7 +18,9 @@ type OperationScope struct { func NewOperationScope(parent context.Context, duration time.Duration) *OperationScope { scope := &OperationScope{Context: parent, cancel: func() {}} if duration > 0 { - scope.cause = errors.New("operation deadline") + // Transports may return context.Cause rather than ctx.Err(). Retain + // deadline identity while keeping this cause unique to the invocation. + scope.cause = fmt.Errorf("operation deadline: %w", context.DeadlineExceeded) scope.Context, scope.cancel = context.WithTimeoutCause(parent, duration, scope.cause) } return scope