Secure, Fast, and Extensible Sandbox runtime for AI agents.
fix(sdk-go): do not wake-replay a request that followed a redirect
JasonBuildAIac13fb0bb265
diff --git a/sdks/sandbox/go/http.go b/sdks/sandbox/go/http.goindex 992d59144..da262513e 100644--- a/sdks/sandbox/go/http.go+++ b/sdks/sandbox/go/http.go@@ -508,12 +508,21 @@ func (c *Client) doStreamRequest(ctx context.Context, method, path string, body// gateway-owned OSEP-0024 wake marker for the wake retry contract.func handleError(resp *http.Response) error {retryAfter, retryAfterPresent := parseRetryAfterValue(resp.Header.Get("Retry-After"))+ wakeExpired := resp.StatusCode == http.StatusServiceUnavailable &&+ resp.Header.Get(wakeMarkerHeader) == wakeMarkerExpired+ // The marker only proves that the final hop never forwarded the request.+ // When the client followed a redirect first, an earlier hop already+ // received the request (and may have processed it), so replaying the+ // original request is no longer provably free of a second execution.+ if wakeExpired && resp.Request != nil && resp.Request.Response != nil {+ wakeExpired = false+ }apiErr := &APIError{StatusCode: resp.StatusCode,RequestID: resp.Header.Get("X-Request-Id"),RetryAfter: retryAfter,RetryAfterPresent: retryAfterPresent,- WakeExpired: resp.StatusCode == http.StatusServiceUnavailable && resp.Header.Get(wakeMarkerHeader) == wakeMarkerExpired,+ WakeExpired: wakeExpired,}data, readErr := io.ReadAll(resp.Body)if readErr != nil {diff --git a/sdks/sandbox/go/stream_wake_retry_test.go b/sdks/sandbox/go/stream_wake_retry_test.goindex 6dea47500..652d982f7 100644--- a/sdks/sandbox/go/stream_wake_retry_test.go+++ b/sdks/sandbox/go/stream_wake_retry_test.go@@ -135,3 +135,34 @@ func TestStreamWakeRetry_DisabledReturnsMarked503(t *testing.T) {require.True(t, apiErr.WakeExpired, "the marked error must reach the caller")require.Equal(t, 1, int(attempts.Load()), "stream bootstrap attempts")}++// A marker received after a redirect chain is not proof about the original+// request: an earlier hop already received it and may have processed it. The+// bootstrap must not be replayed, so the original request is sent exactly once.+func TestStreamWakeRetry_NotRetriedAfterRedirect(t *testing.T) {+ var bootstraps, wakeAnswers atomic.Int32+ mux := http.NewServeMux()+ mux.HandleFunc("/command", func(w http.ResponseWriter, r *http.Request) {+ bootstraps.Add(1)+ http.Redirect(w, r, "/wake", http.StatusTemporaryRedirect)+ })+ mux.HandleFunc("/wake", func(w http.ResponseWriter, r *http.Request) {+ wakeAnswers.Add(1)+ writeStreamWakeTimeout(w)+ })+ srv := httptest.NewServer(mux)+ defer srv.Close()++ client := NewExecdClient(srv.URL, "tok", WithRetry(wakeConfig(5, time.Minute)))++ err := client.RunCommand(context.Background(), RunCommandRequest{Command: "echo hello"}, func(StreamEvent) error {+ return nil+ })+ require.Error(t, err, "a marked 503 behind a redirect must not be replayed")+ require.Equal(t, 1, int(bootstraps.Load()), "original requests")+ require.Equal(t, 1, int(wakeAnswers.Load()), "redirected requests")+ var apiErr *APIError+ require.ErrorAs(t, err, &apiErr)+ require.Equal(t, http.StatusServiceUnavailable, apiErr.StatusCode, "status code")+ require.True(t, !apiErr.WakeExpired, "a redirected answer must not claim wake provenance")+}