Secure, Fast, and Extensible Sandbox runtime for AI agents.
refactor(sdks): simplify download metadata parsing and tests
wenmou9ac2d337381b
diff --git a/sdks/sandbox/csharp/src/OpenSandbox/Adapters/FilesystemAdapter.cs b/sdks/sandbox/csharp/src/OpenSandbox/Adapters/FilesystemAdapter.csindex 2541b3d65..b9cb41c04 100644--- a/sdks/sandbox/csharp/src/OpenSandbox/Adapters/FilesystemAdapter.cs+++ b/sdks/sandbox/csharp/src/OpenSandbox/Adapters/FilesystemAdapter.cs@@ -16,7 +16,6 @@ using System.Net.Http.Headers;using System.Runtime.CompilerServices;using System.Text;using System.Text.Json;-using System.Text.RegularExpressions;using OpenSandbox.Core;using OpenSandbox.Internal;using OpenSandbox.Models;@@ -34,9 +33,6 @@ internal sealed class FilesystemAdapter : IIdentitySandboxFiles, IDownloadSandboprivate readonly string _baseUrl;private readonly string _identityBaseUrl;private readonly IReadOnlyDictionary<string, string> _headers;- private static readonly Regex ContentRangeRegex = new(- @"^bytes\s+(\d+)-(\d+)/(\d+|\*)$",- RegexOptions.Compiled | RegexOptions.IgnoreCase);private static readonly JsonSerializerOptions JsonOptions = new(){@@ -302,25 +298,12 @@ internal sealed class FilesystemAdapter : IIdentitySandboxFiles, IDownloadSandboreturn null;}- var invalid = new ByteRange(-1, -1, -1, raw!);- var match = ContentRangeRegex.Match(raw!);- if (!match.Success ||- !long.TryParse(match.Groups[1].Value, out var start) ||- !long.TryParse(match.Groups[2].Value, out var end))+ if (ContentRangeHeaderValue.TryParse(raw, out var range) &&+ string.Equals(range.Unit, "bytes", StringComparison.OrdinalIgnoreCase) && range.HasRange){- return invalid;+ return new ByteRange(range.From!.Value, range.To!.Value, range.Length ?? -1, raw!);}-- var total = -1L;- if (match.Groups[3].Value != "*" && !long.TryParse(match.Groups[3].Value, out total))- {- return invalid;- }- if (end < start || (total != -1 && total <= end))- {- return invalid;- }- return new ByteRange(start, end, total, raw!);+ return new ByteRange(-1, -1, -1, raw!);}private static string? GetContentHeader(HttpResponseMessage response, string name)diff --git a/sdks/sandbox/csharp/tests/OpenSandbox.Tests/FilesystemAdapterTests.cs b/sdks/sandbox/csharp/tests/OpenSandbox.Tests/FilesystemAdapterTests.csindex 0127abd29..f8822adff 100644--- a/sdks/sandbox/csharp/tests/OpenSandbox.Tests/FilesystemAdapterTests.cs+++ b/sdks/sandbox/csharp/tests/OpenSandbox.Tests/FilesystemAdapterTests.cs@@ -248,39 +248,25 @@ public class FilesystemAdapterTestsresponse.TotalSize.Should().Be(10);}- [Fact]- public async Task ReadBytesDetailedAsync_ShouldIdentifyPartialResponseWithoutContentRange()+ [Theory]+ [InlineData(null, -1, -1)]+ [InlineData("garbage", -1, -1)]+ [InlineData("bytes 5-9/*", 5, 9)]+ [InlineData("bytes 0-4/4", -1, -1)]+ public async Task ReadBytesDetailedAsync_ShouldReportUnknownTotalSize(string? raw, long start, long end){var handler = new DownloadHandler();- handler.Enqueue(DownloadResponse(HttpStatusCode.PartialContent, "hello"));+ handler.Enqueue(DownloadResponse(HttpStatusCode.PartialContent, "hello", raw));using var client = new HttpClient(handler);var adapter = CreateAdapter(client);var response = await adapter.ReadBytesDetailedAsync("/data.bin");response.IsPartial.Should().BeTrue();- response.ContentRange.Should().BeNull();+ response.ContentRange.Should().Be(raw == null ? null : new ByteRange(start, end, -1, raw));response.TotalSize.Should().Be(-1);}- [Fact]- public async Task ReadBytesDetailedAsync_ShouldPreserveInvalidAndUnknownRanges()- {- var handler = new DownloadHandler();- handler.Enqueue(DownloadResponse(HttpStatusCode.PartialContent, "hello", "garbage"));- handler.Enqueue(DownloadResponse(HttpStatusCode.PartialContent, "hello", "bytes 5-9/*"));- using var client = new HttpClient(handler);- var adapter = CreateAdapter(client);-- var invalid = await adapter.ReadBytesDetailedAsync("/data.bin");- var unknown = await adapter.ReadBytesDetailedAsync("/data.bin");-- invalid.ContentRange.Should().Be(new ByteRange(-1, -1, -1, "garbage"));- invalid.TotalSize.Should().Be(-1);- unknown.ContentRange.Should().Be(new ByteRange(5, 9, -1, "bytes 5-9/*"));- unknown.TotalSize.Should().Be(-1);- }-[Fact]public async Task DetailedAndLegacyReads_ShouldReturnResponseBodies(){diff --git a/sdks/sandbox/go/opensandbox_test.go b/sdks/sandbox/go/opensandbox_test.goindex 95e25ae2f..3a9168436 100644--- a/sdks/sandbox/go/opensandbox_test.go+++ b/sdks/sandbox/go/opensandbox_test.go@@ -62,22 +62,6 @@ func jsonResponse(w http.ResponseWriter, status int, v any) {json.NewEncoder(w).Encode(v)}-type roundTripFunc func(*http.Request) (*http.Response, error)--func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {- return f(req)-}--type closeTrackingBody struct {- io.Reader- closed bool-}--func (b *closeTrackingBody) Close() error {- b.closed = true- return nil-}-func TestCreateSandbox(t *testing.T) {now := time.Now().UTC().Truncate(time.Second)want := SandboxInfo{@@ -1897,95 +1881,30 @@ func TestDownloadFileResponse_Range(t *testing.T) {require.Equal(t, ByteRange{Start: 0, End: 4, Total: 10, Raw: "bytes 0-4/10"}, *resp.ContentRange)}-func TestDownloadFileResponse_PartialWithoutContentRange(t *testing.T) {- _, client := newExecdServer(t, func(w http.ResponseWriter, r *http.Request) {- w.WriteHeader(http.StatusPartialContent)- _, _ = w.Write([]byte("hello"))- })-- resp, err := client.DownloadFileResponse(context.Background(), "/sandbox/big.bin", "bytes=0-4")- require.NoErrorf(t, err, "DownloadFileResponse partial without Content-Range")- defer resp.Body.Close()-- require.Equal(t, true, resp.IsPartial())- if resp.ContentRange != nil {- assert.Fail(t, fmt.Sprintf("ContentRange = %+v, want nil", resp.ContentRange))+func TestDownloadFileResponse_UnknownTotal(t *testing.T) {+ for _, tc := range []struct {+ name string+ raw string+ want *ByteRange+ }{+ {name: "missing"},+ {name: "malformed", raw: "garbage", want: &ByteRange{Start: -1, End: -1, Total: -1, Raw: "garbage"}},+ {name: "unknown", raw: "bytes 5-9/*", want: &ByteRange{Start: 5, End: 9, Total: -1, Raw: "bytes 5-9/*"}},+ } {+ t.Run(tc.name, func(t *testing.T) {+ _, client := newExecdServer(t, func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Range", tc.raw)+ w.WriteHeader(http.StatusPartialContent)+ _, _ = w.Write([]byte("hello"))+ })+ resp, err := client.DownloadFileResponse(context.Background(), "/sandbox/big.bin", "bytes=5-")+ require.NoError(t, err)+ defer resp.Body.Close()+ require.True(t, resp.IsPartial())+ require.Equal(t, tc.want, resp.ContentRange)+ require.Equal(t, int64(-1), resp.TotalSize)+ })}- require.Equal(t, int64(-1), resp.TotalSize)-}--func TestDownloadFileResponse_InvalidContentRange(t *testing.T) {- _, client := newExecdServer(t, func(w http.ResponseWriter, r *http.Request) {- w.Header().Set("Content-Range", "garbage")- w.WriteHeader(http.StatusPartialContent)- _, _ = w.Write([]byte("hello"))- })-- resp, err := client.DownloadFileResponse(context.Background(), "/sandbox/big.bin", "bytes=0-4")- require.NoErrorf(t, err, "DownloadFileResponse invalid Content-Range")- defer resp.Body.Close()-- require.NotNil(t, resp.ContentRange)- require.Equal(t, ByteRange{Start: -1, End: -1, Total: -1, Raw: "garbage"}, *resp.ContentRange)- require.Equal(t, int64(-1), resp.TotalSize)-}--func TestParseContentRange_UnknownTotal(t *testing.T) {- got := parseContentRange("bytes 1024-2047/*")- require.NotNil(t, got)- require.Equal(t, ByteRange{Start: 1024, End: 2047, Total: -1, Raw: "bytes 1024-2047/*"}, *got)-}--func TestDownloadFileResponse_ClosesErrorBodies(t *testing.T) {- t.Run("error", func(t *testing.T) {- body := &closeTrackingBody{Reader: strings.NewReader(`{"code":"NOT_FOUND","message":"missing"}`)}- client := NewExecdClient("http://execd.test", "token", WithHTTPClient(&http.Client{- Transport: roundTripFunc(func(*http.Request) (*http.Response, error) {- return &http.Response{- StatusCode: http.StatusNotFound,- Header: make(http.Header),- Body: body,- }, nil- }),- }))-- _, err := client.DownloadFileResponse(context.Background(), "/missing", "")- require.Error(t, err)- require.Equal(t, true, body.closed, "error response body should be closed")- })-- t.Run("retry", func(t *testing.T) {- errorBody := &closeTrackingBody{Reader: strings.NewReader(`{"code":"UNAVAILABLE","message":"retry"}`)}- successBody := &closeTrackingBody{Reader: strings.NewReader("hello")}- attempts := 0- client := NewExecdClient("http://execd.test", "token",- WithHTTPClient(&http.Client{Transport: roundTripFunc(func(*http.Request) (*http.Response, error) {- attempts++- if attempts == 1 {- return &http.Response{- StatusCode: http.StatusServiceUnavailable,- Header: make(http.Header),- Body: errorBody,- }, nil- }- return &http.Response{- StatusCode: http.StatusOK,- Header: make(http.Header),- Body: successBody,- ContentLength: 5,- }, nil- })}),- WithRetry(RetryConfig{MaxRetries: 1, Multiplier: 1}),- )-- resp, err := client.DownloadFileResponse(context.Background(), "/data", "")- require.NoErrorf(t, err, "DownloadFileResponse retry")- require.Equal(t, 2, attempts)- require.Equal(t, true, errorBody.closed, "failed attempt body should be closed")- require.Equal(t, false, successBody.closed, "successful response body should remain open")- require.NoErrorf(t, resp.Body.Close(), "close successful response body")- require.Equal(t, true, successBody.closed, "caller should close successful response body")- })}func TestDownloadFile_WithCustomHeaders(t *testing.T) {diff --git a/sdks/sandbox/javascript/src/adapters/downloadResponse.ts b/sdks/sandbox/javascript/src/adapters/downloadResponse.tsindex 7fd281c7a..895a9f951 100644--- a/sdks/sandbox/javascript/src/adapters/downloadResponse.ts+++ b/sdks/sandbox/javascript/src/adapters/downloadResponse.ts@@ -59,7 +59,7 @@ export function createReadBytesResponse<TBody>(contentLength,totalSize: isPartial? (contentRange?.total ?? -1)- : lineBased ? -1 : contentLength,+ : (lineBased ? -1 : contentLength),contentRange,isPartial,};diff --git a/sdks/sandbox/javascript/tests/filesystem.download-response.test.mjs b/sdks/sandbox/javascript/tests/filesystem.download-response.test.mjsindex 324c3ba81..d28f788fd 100644--- a/sdks/sandbox/javascript/tests/filesystem.download-response.test.mjs+++ b/sdks/sandbox/javascript/tests/filesystem.download-response.test.mjs@@ -83,47 +83,24 @@ test("readBytesDetailed identifies an ignored Range request", async () => {assert.equal(response.totalSize, 10);});-test("readBytesDetailed identifies a partial response without Content-Range", async () => {- const adapter = createAdapter(async () =>- new Response("hello", {- status: 206,- }),- );-- const response = await adapter.readBytesDetailed("/data.bin");-- assert.equal(response.isPartial, true);- assert.equal(response.contentRange, undefined);- assert.equal(response.totalSize, -1);-});--test("readBytesDetailed preserves malformed and unknown Content-Range values", async () => {- const ranges = ["garbage", "bytes 5-9/*"];- const adapter = createAdapter(async () =>- new Response("hello", {- status: 206,- headers: { "Content-Range": ranges.shift() },- }),- );-- const malformed = await adapter.readBytesDetailed("/data.bin");- assert.deepEqual(malformed.contentRange, {- start: -1,- end: -1,- total: -1,- raw: "garbage",- });- assert.equal(malformed.totalSize, -1);-- const unknown = await adapter.readBytesDetailed("/data.bin");- assert.deepEqual(unknown.contentRange, {- start: 5,- end: 9,- total: -1,- raw: "bytes 5-9/*",+for (const [raw, expected] of [+ [undefined, undefined],+ ["garbage", { start: -1, end: -1, total: -1, raw: "garbage" }],+ ["bytes 5-9/*", { start: 5, end: 9, total: -1, raw: "bytes 5-9/*" }],+]) {+ test(`readBytesDetailed reports unknown total size: ${raw}`, async () => {+ const adapter = createAdapter(async () =>+ new Response("hello", {+ status: 206,+ headers: raw ? { "Content-Range": raw } : {},+ }),+ );+ const response = await adapter.readBytesDetailed("/data.bin");+ assert.equal(response.isPartial, true);+ assert.deepEqual(response.contentRange, expected);+ assert.equal(response.totalSize, -1);});- assert.equal(unknown.totalSize, -1);-});+}test("detailed and legacy streaming reads return the response body", async () => {const adapter = createAdapter(async () =>diff --git a/sdks/sandbox/kotlin/sandbox/src/test/kotlin/com/alibaba/opensandbox/sandbox/infrastructure/adapters/service/FilesystemAdapterTest.kt b/sdks/sandbox/kotlin/sandbox/src/test/kotlin/com/alibaba/opensandbox/sandbox/infrastructure/adapters/service/FilesystemAdapterTest.ktindex 8a859bec6..8ff06287f 100644--- a/sdks/sandbox/kotlin/sandbox/src/test/kotlin/com/alibaba/opensandbox/sandbox/infrastructure/adapters/service/FilesystemAdapterTest.kt+++ b/sdks/sandbox/kotlin/sandbox/src/test/kotlin/com/alibaba/opensandbox/sandbox/infrastructure/adapters/service/FilesystemAdapterTest.kt@@ -233,42 +233,22 @@ class FilesystemAdapterTest {}@Test- fun `readByteArrayDetailed identifies a partial response without Content-Range`() {- mockWebServer.enqueue(- MockResponse()- .setResponseCode(206)- .setBody("hello"),- )-- val response = filesystemAdapter.readByteArrayDetailed("/data.bin")-- assertTrue(response.isPartial)- assertNull(response.contentRange)- assertEquals(-1, response.totalSize)- }-- @Test- fun `readByteArrayDetailed preserves invalid and unknown Content-Range values`() {- mockWebServer.enqueue(- MockResponse()- .setResponseCode(206)- .setHeader("Content-Range", "garbage")- .setBody("hello"),- )- mockWebServer.enqueue(- MockResponse()- .setResponseCode(206)- .setHeader("Content-Range", "bytes 5-9/*")- .setBody("hello"),- )-- val invalid = filesystemAdapter.readByteArrayDetailed("/data.bin")- val unknown = filesystemAdapter.readByteArrayDetailed("/data.bin")-- assertEquals(ByteRange(-1, -1, -1, "garbage"), invalid.contentRange)- assertEquals(-1, invalid.totalSize)- assertEquals(ByteRange(5, 9, -1, "bytes 5-9/*"), unknown.contentRange)- assertEquals(-1, unknown.totalSize)+ fun `readByteArrayDetailed reports unknown total size`() {+ for ((raw, expected) in listOf(+ null to null,+ "garbage" to ByteRange(-1, -1, -1, "garbage"),+ "bytes 5-9/*" to ByteRange(5, 9, -1, "bytes 5-9/*"),+ )) {+ val response = MockResponse().setResponseCode(206).setBody("hello")+ if (raw != null) response.setHeader("Content-Range", raw)+ mockWebServer.enqueue(response)++ val download = filesystemAdapter.readByteArrayDetailed("/data.bin")++ assertTrue(download.isPartial)+ assertEquals(expected, download.contentRange)+ assertEquals(-1, download.totalSize)+ }}@Test