Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

fix(sdk-go): make write retry safety provable

JasonBuildAI2477622256e6
diff --git a/sdks/sandbox/go/http.go b/sdks/sandbox/go/http.go
index 094f935d8..dbf179924 100644
--- a/sdks/sandbox/go/http.go
+++ b/sdks/sandbox/go/http.go
@@ -17,6 +17,7 @@ package opensandbox
import (
"bytes"
"context"
+ "crypto/tls"
"encoding/json"
"errors"
"fmt"
@@ -27,6 +28,7 @@ import (
"net/url"
"strings"
"sync"
+ "sync/atomic"
"time"
)
@@ -362,13 +364,27 @@ func (c *Client) doRequestOnce(ctx context.Context, method, path string, body an
bodyReader = bytes.NewReader(buf)
}
+ var sent atomic.Bool
+ var tlsHandshakeFailed atomic.Bool
+ trace := &httptrace.ClientTrace{
+ WroteHeaders: func() {
+ sent.Store(true)
+ },
+ GotFirstResponseByte: func() {
+ sent.Store(true)
+ },
+ TLSHandshakeDone: func(_ tls.ConnectionState, err error) {
+ if err != nil {
+ tlsHandshakeFailed.Store(true)
+ }
+ },
+ }
if reused != nil {
- ctx = httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{
- GotConn: func(info httptrace.GotConnInfo) {
- *reused = info.Reused
- },
- })
+ trace.GotConn = func(info httptrace.GotConnInfo) {
+ *reused = info.Reused
+ }
}
+ ctx = httptrace.WithClientTrace(ctx, trace)
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, bodyReader)
if err != nil {
@@ -389,6 +405,11 @@ func (c *Client) doRequestOnce(ctx context.Context, method, path string, body an
resp, err := c.httpClient.Do(req)
if err != nil {
+ if sent.Load() {
+ err = &requestSentError{err: err}
+ } else if tlsHandshakeFailed.Load() {
+ err = &preSendError{err: err}
+ }
return fmt.Errorf("opensandbox: do request: %w", err)
}
defer resp.Body.Close()
@@ -415,13 +436,14 @@ func (c *Client) doRequestOnce(ctx context.Context, method, path string, body an
}
// doStreamRequest builds an HTTP request, executes it, and streams SSE events
-// through handler. Connection setup follows the method-aware retry policy; once
-// streaming begins, errors are not retried (partial data may have been delivered
-// to the handler).
+// through handler. SSE bootstraps deliberately bypass the generic retry loop:
+// the stream may already have delivered partial data or submitted a command,
+// and OSEP-0017 requires the bootstrap to stay unwrapped. Once streaming begins,
+// errors are not retried.
func (c *Client) doStreamRequest(ctx context.Context, method, path string, body any, handler EventHandler) error {
var resp *http.Response
- connectErr := c.withRetryWake(ctx, false, method, func() error {
+ connectErr := func() error {
var bodyReader io.Reader
if body != nil {
buf, err := json.Marshal(body)
@@ -463,7 +485,7 @@ func (c *Client) doStreamRequest(ctx context.Context, method, path string, body
resp = r
return nil
- })
+ }()
if connectErr != nil {
return connectErr
}
diff --git a/sdks/sandbox/go/retry.go b/sdks/sandbox/go/retry.go
index ff8c25caf..62a55c68f 100644
--- a/sdks/sandbox/go/retry.go
+++ b/sdks/sandbox/go/retry.go
@@ -148,7 +148,7 @@ func DefaultRetryConfig() RetryConfig {
// errors and network failures. Idempotent requests retry 429/502/503/504 by
// default; override RetryConfig.RetryableStatusCodes to customize. POST and
// PATCH retry only transport failures provably raised before any request bytes
-// are sent (currently dial failures), unless
+// are sent (dial, proxy-connect, and TLS-handshake failures), unless
// RetryConfig.RetryableStatusCodesNonIdempotent opts into specific status
// codes.
func WithRetry(cfg RetryConfig) Option {
@@ -210,13 +210,37 @@ func (r *RetryConfig) isRetryableStatus(method string, code int) bool {
return false
}
+// requestSentError marks a transport failure after request bytes were written
+// or a response was observed, including failures on a redirect hop. It keeps a
+// later dial failure from being mistaken for a pre-send failure of the whole
+// call.
+type requestSentError struct{ err error }
+
+func (e *requestSentError) Error() string { return e.err.Error() }
+func (e *requestSentError) Unwrap() error { return e.err }
+
+// preSendError marks a transport failure proven to have happened before any
+// request bytes were written, such as a TLS handshake failure.
+type preSendError struct{ err error }
+
+func (e *preSendError) Error() string { return e.err.Error() }
+func (e *preSendError) Unwrap() error { return e.err }
+
// isPreSendError reports whether err proves that no request bytes reached the
// server. Go surfaces dial failures (including DNS and TCP connect failures)
-// as *net.OpError with Op == "dial"; OSEP-0017 requires classifying those as
-// safe to retry for any method.
+// and proxy CONNECT failures as *net.OpError; OSEP-0017 requires classifying
+// those as safe to retry for any method.
func isPreSendError(err error) bool {
+ var sentErr *requestSentError
+ if errors.As(err, &sentErr) {
+ return false
+ }
+ var preSendErr *preSendError
+ if errors.As(err, &preSendErr) {
+ return true
+ }
var opErr *net.OpError
- return errors.As(err, &opErr) && opErr.Op == "dial"
+ return errors.As(err, &opErr) && (opErr.Op == "dial" || opErr.Op == "proxyconnect")
}
// isTransientErrorForMethod checks whether err should trigger a generic retry
@@ -337,17 +361,13 @@ func (c *Client) withRetry(ctx context.Context, fn func() error) error {
// withRetryMethod executes fn with the generic retry policy tailored to method.
func (c *Client) withRetryMethod(ctx context.Context, method string, fn func() error) error {
- return c.withRetryWake(ctx, true, method, fn)
+ return c.withRetryWake(ctx, method, fn)
}
-// withRetryWake is withRetry with control over the wake contract. Streaming
-// connects pass wakeEnabled=false: an established stream is never re-executed,
-// and a waking sandbox is not parked for up to the wake budget — the response
-// falls back to the generic policy's bare-503 rules (retried only when the
-// method/status qualify), matching the other official SDKs, which leave their
-// streaming transports unwrapped.
-func (c *Client) withRetryWake(ctx context.Context, wakeEnabled bool, method string, fn func() error) error {
- if c.retry == nil || (c.retry.MaxRetries == 0 && !(wakeEnabled && c.retry.wakeEnabled())) {
+// withRetryWake applies the generic policy together with the OSEP-0024 wake
+// contract. Streaming transports intentionally call neither layer.
+func (c *Client) withRetryWake(ctx context.Context, method string, fn func() error) error {
+ if c.retry == nil || (c.retry.MaxRetries == 0 && !c.retry.wakeEnabled()) {
return fn()
}
@@ -376,7 +396,7 @@ func (c *Client) withRetryWake(ctx context.Context, wakeEnabled bool, method str
}
var apiErr *APIError
- if wakeEnabled && c.retry.wakeEnabled() && errors.As(lastErr, &apiErr) && apiErr.WakeExpired {
+ if c.retry.wakeEnabled() && errors.As(lastErr, &apiErr) && apiErr.WakeExpired {
wakeAttempts++
elapsed := time.Since(started)
// An explicit Retry-After of 0 means retry immediately; only a
diff --git a/sdks/sandbox/go/retry_non_idempotent_test.go b/sdks/sandbox/go/retry_non_idempotent_test.go
index 8cf8016cc..3be8533f1 100644
--- a/sdks/sandbox/go/retry_non_idempotent_test.go
+++ b/sdks/sandbox/go/retry_non_idempotent_test.go
@@ -119,6 +119,57 @@ func TestRetry_NonIdempotentTransportFailures(t *testing.T) {
}
}
+func TestRetry_NonIdempotentRedirectDialNotRetried(t *testing.T) {
+ targetListener, err := net.Listen("tcp", "127.0.0.1:0")
+ require.NoError(t, err)
+ target := "http://" + targetListener.Addr().String() + "/next"
+ require.NoError(t, targetListener.Close())
+
+ var attempts atomic.Int32
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ attempts.Add(1)
+ http.Redirect(w, r, target, http.StatusTemporaryRedirect)
+ }))
+ defer srv.Close()
+
+ client := NewExecdClient(srv.URL, "token", WithRetry(RetryConfig{
+ MaxRetries: 1,
+ InitialBackoff: time.Millisecond,
+ }))
+
+ _, err = client.CreateSession(context.Background())
+ require.Error(t, err)
+ require.Equal(t, int32(1), attempts.Load(), "a redirected POST must not replay its first hop")
+}
+
+func TestRetry_NonIdempotentTLSHandshakeFailureRetried(t *testing.T) {
+ ln, err := net.Listen("tcp", "127.0.0.1:0")
+ require.NoError(t, err)
+ defer ln.Close()
+
+ var accepts atomic.Int32
+ done := make(chan struct{})
+ go func() {
+ defer close(done)
+ for {
+ conn, err := ln.Accept()
+ if err != nil {
+ return
+ }
+ accepts.Add(1)
+ _ = conn.Close()
+ }
+ }()
+
+ client := NewClient("https://"+ln.Addr().String(), "token", "OPEN-SANDBOX-API-KEY",
+ WithHTTPClient(&http.Client{Transport: &http.Transport{TLSHandshakeTimeout: time.Second}}),
+ WithRetry(RetryConfig{MaxRetries: 1, InitialBackoff: time.Millisecond}),
+ )
+ err = client.doRequest(context.Background(), http.MethodPost, "/", map[string]string{"a": "b"}, nil)
+ require.Error(t, err)
+ require.Equal(t, int32(2), accepts.Load(), "a failed TLS handshake is a pre-send failure")
+}
+
type retryRoundTripFunc func(*http.Request) (*http.Response, error)
func (f retryRoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
diff --git a/sdks/sandbox/go/retry_test.go b/sdks/sandbox/go/retry_test.go
index 08c80526c..050c800e5 100644
--- a/sdks/sandbox/go/retry_test.go
+++ b/sdks/sandbox/go/retry_test.go
@@ -291,10 +291,11 @@ func TestRetry_StreamingConnectionNotRetried(t *testing.T) {
defer srv.Close()
client := NewExecdClient(srv.URL, "tok", WithRetry(RetryConfig{
- MaxRetries: 2,
- InitialBackoff: 10 * time.Millisecond,
- MaxBackoff: 100 * time.Millisecond,
- Multiplier: 2.0,
+ MaxRetries: 2,
+ InitialBackoff: 10 * time.Millisecond,
+ MaxBackoff: 100 * time.Millisecond,
+ Multiplier: 2.0,
+ RetryableStatusCodesNonIdempotent: []int{http.StatusServiceUnavailable},
}))
var events []StreamEvent
@@ -637,6 +638,27 @@ func TestIsTransientError(t *testing.T) {
nil,
false,
},
+ {
+ "redirect dial after send POST",
+ http.MethodPost,
+ &requestSentError{err: &net.OpError{Op: "dial", Net: "tcp", Err: fmt.Errorf("connection refused")}},
+ nil,
+ false,
+ },
+ {
+ "TLS handshake POST",
+ http.MethodPost,
+ &preSendError{err: fmt.Errorf("tls handshake failed")},
+ nil,
+ true,
+ },
+ {
+ "proxy connect POST",
+ http.MethodPost,
+ &net.OpError{Op: "proxyconnect", Net: "tcp", Err: fmt.Errorf("connection refused")},
+ nil,
+ true,
+ },
{
"read failure GET",
http.MethodGet,