Skip to content

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

fix(sdk-go): apply the OSEP-0024 wake retry to stream starts (follow-up)

JasonBuildAIa76386d1b0f2
diff --git a/sdks/sandbox/go/http.go b/sdks/sandbox/go/http.go
index 5a4a48985..992d59144 100644
--- a/sdks/sandbox/go/http.go
+++ b/sdks/sandbox/go/http.go
@@ -444,16 +444,16 @@ 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. SSE bootstraps 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 by it. The OSEP-0024 wake contract
-// still applies to the bootstrap: a marked wake-timeout 503 proves the gateway
-// never forwarded the request, so replaying it cannot run anything twice.
-// Without that retry a command against an auto-paused sandbox fails instead of
-// waking it, unlike every non-streaming request. Once streaming begins, errors
-// are not retried.
+// requires the bootstrap to stay unwrapped by it. The OSEP-0024 wake retry
+// still applies, as it does to every other request: the marker proves the
+// gateway parked the bootstrap and never forwarded it, so replaying it cannot
+// run anything twice, and older behavior that failed the call fast left the
+// primary execution path unable to wake an auto-paused sandbox. 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.withWakeRetry(ctx, func() error {
+ connectErr := c.withWakeRetry(ctx, method, func() error {
var bodyReader io.Reader
if body != nil {
buf, err := json.Marshal(body)
diff --git a/sdks/sandbox/go/retry.go b/sdks/sandbox/go/retry.go
index cd7729f1c..56e58c2c8 100644
--- a/sdks/sandbox/go/retry.go
+++ b/sdks/sandbox/go/retry.go
@@ -375,8 +375,8 @@ func (c *Client) withRetryWake(ctx context.Context, method string, fn func() err
// never forwarded it, so replaying it cannot execute anything twice and is safe
// even for a streaming POST bootstrap. Every other failure, including a bare
// 503, is returned to the caller unwrapped (OSEP-0017).
-func (c *Client) withWakeRetry(ctx context.Context, fn func() error) error {
- return c.retryLoop(ctx, http.MethodGet, false, fn)
+func (c *Client) withWakeRetry(ctx context.Context, method string, fn func() error) error {
+ return c.retryLoop(ctx, method, false, fn)
}
// retryLoop is the shared retry engine. generic controls whether transient
diff --git a/sdks/sandbox/go/stream_wake_retry_test.go b/sdks/sandbox/go/stream_wake_retry_test.go
index c9c3f6cbd..6dea47500 100644
--- a/sdks/sandbox/go/stream_wake_retry_test.go
+++ b/sdks/sandbox/go/stream_wake_retry_test.go
@@ -16,6 +16,7 @@ package opensandbox
import (
"context"
+ "errors"
"net/http"
"net/http/httptest"
"sync/atomic"
@@ -80,3 +81,57 @@ func TestStreamWakeRetry_BudgetExhaustionReturnsTypedError(t *testing.T) {
require.Equal(t, 3, wakeErr.Attempts, "wake attempts")
require.Equal(t, 3, int(attempts.Load()), "stream bootstrap attempts")
}
+
+// A failure after the stream was established must never re-dispatch it: only the
+// connect attempt is wrapped. The handler aborts after the first event, which is
+// the clearest point at which a replay would execute the command twice.
+func TestStreamWakeRetry_NotRetriedAfterFirstEvent(t *testing.T) {
+ var attempts atomic.Int32
+ stop := errors.New("consumer stopped")
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ attempts.Add(1)
+ w.Header().Set("Content-Type", "text/event-stream")
+ w.WriteHeader(http.StatusOK)
+ _, _ = w.Write([]byte("{\"type\":\"stdout\",\"text\":\"first\",\"timestamp\":1}\n\n" +
+ "{\"type\":\"stdout\",\"text\":\"second\",\"timestamp\":2}\n\n"))
+ }))
+ defer srv.Close()
+
+ client := NewExecdClient(srv.URL, "tok", WithRetry(wakeConfig(5, time.Minute)))
+
+ seen := 0
+ err := client.RunCommand(context.Background(), RunCommandRequest{Command: "echo hello"}, func(StreamEvent) error {
+ seen++
+ return stop
+ })
+ require.ErrorIs(t, err, stop, "handler error must surface unchanged")
+ require.Equal(t, 1, seen, "delivered events")
+ require.Equal(t, 1, int(attempts.Load()), "an established stream is never re-dispatched")
+}
+
+// The wake retry stays opt-out: with it disabled a marked 503 reaches the caller
+// immediately, so callers that relied on the previous fail-fast behavior keep it.
+func TestStreamWakeRetry_DisabledReturnsMarked503(t *testing.T) {
+ var attempts atomic.Int32
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ attempts.Add(1)
+ writeStreamWakeTimeout(w)
+ }))
+ defer srv.Close()
+
+ client := NewExecdClient(srv.URL, "tok", WithRetry(RetryConfig{
+ MaxRetries: 0,
+ WakeRetryEnabled: WakeRetryDisabled(),
+ WakeMaxAttempts: 5,
+ WakeOverallDeadline: time.Minute,
+ }))
+
+ err := client.RunCommand(context.Background(), RunCommandRequest{Command: "echo hello"}, func(StreamEvent) error {
+ return nil
+ })
+ require.Error(t, err, "opted-out wake retry on a stream")
+ var apiErr *APIError
+ require.ErrorAs(t, err, &apiErr)
+ require.True(t, apiErr.WakeExpired, "the marked error must reach the caller")
+ require.Equal(t, 1, int(attempts.Load()), "stream bootstrap attempts")
+}