From 3bc019ad4e2f7917cb3d36c11c82c92e28324d90 Mon Sep 17 00:00:00 2001 From: Kanat Date: Wed, 25 Nov 2026 09:57:38 -0500 Subject: [PATCH 1/2] [AI-3874] Fix flaky daytona refusal-size test The three oversize-answer tests bounded the bytes the fake server wrote, which includes what the host's socket buffers took before the router closed the connection. Count what the router read from the answer instead. Co-Authored-By: Claude Sonnet 5.5 --- .../internal/sandbox/daytona/daytona_test.go | 92 +++++++++++++------ 1 file changed, 63 insertions(+), 29 deletions(-) diff --git a/acceleration/internal/sandbox/daytona/daytona_test.go b/acceleration/internal/sandbox/daytona/daytona_test.go index 5ee748a792..cd66f0578b 100644 --- a/acceleration/internal/sandbox/daytona/daytona_test.go +++ b/acceleration/internal/sandbox/daytona/daytona_test.go @@ -15,6 +15,7 @@ import ( "os" "strings" "sync" + "sync/atomic" "testing" "time" @@ -369,6 +370,35 @@ func (c *counted) Write(b []byte) (int, error) { return n, err } +// tallied is a transport that counts the bytes read from the bodies of the answers to requests +// whose path ends in suffix. It sits under the router's bound, so it counts what the router took +// off the connection: unlike the bytes the server wrote, it does not depend on how much the +// kernel's socket buffers held, which is the host's setting. +type tallied struct { + next http.RoundTripper + suffix string + read atomic.Int64 +} + +func (t *tallied) RoundTrip(r *http.Request) (*http.Response, error) { + answer, err := t.next.RoundTrip(r) + if err == nil && strings.HasSuffix(r.URL.Path, t.suffix) { + answer.Body = tally{ReadCloser: answer.Body, read: &t.read} + } + return answer, err +} + +type tally struct { + io.ReadCloser + read *atomic.Int64 +} + +func (t tally) Read(p []byte) (int, error) { + n, err := t.ReadCloser.Read(p) + t.read.Add(int64(n)) + return n, err +} + // secret serves Daytona's secret API: create, update and delete. The caller holds mu. func (p *platform) secret(w http.ResponseWriter, r *http.Request) { var body struct { @@ -530,6 +560,22 @@ func (s *DaytonaSuite) SetupTest() { s.T().Cleanup(func() { _ = box.Close() }) } +// tallyingBox is a sandbox on the platform that counts what it reads of the answers to requests +// whose path ends in suffix. +func (s *DaytonaSuite) tallyingBox(suffix string) (*Sandbox, *tallied) { + answers := &tallied{next: http.DefaultTransport, suffix: suffix} + box, err := New(Options{ + APIKey: "key-1", + APIURL: s.platform.server.URL + "/api", + ProxyURL: s.platform.server.URL + "/toolbox", + HTTPClient: &http.Client{Transport: answers}, + Logger: slog.New(slog.DiscardHandler), + }) + s.Require().NoError(err) + s.T().Cleanup(func() { _ = box.Close() }) + return box, answers +} + func (s *DaytonaSuite) TestAKeyIsRequired() { s.T().Setenv(apiKeyEnvVar, "") @@ -833,58 +879,46 @@ func (s *DaytonaSuite) TestAFileComesBackOnlyIfItIsNeitherEmptyNorTooLarge() { func (s *DaytonaSuite) TestAFileFarTooLargeIsRefusedWithoutBeingReadWhole() { s.platform.holdsSized("/tmp/dump.bin", 10*sandbox.MaxFileBytes) + box, answers := s.tallyingBox("/files/bulk-download") - result, err := s.box.Run(s.ctx, "dump()", []string{"/tmp/dump.bin"}) + result, err := box.Run(s.ctx, "dump()", []string{"/tmp/dump.bin"}) s.Require().NoError(err) s.Empty(result.Files) s.Equal([]string{"/tmp/dump.bin"}, result.Missing) - var sent int64 - platform := s.platform - s.Eventually(func() bool { - var ended bool - sent, ended = platform.sent("/tmp/dump.bin") - return ended - }, 10*time.Second, 10*time.Millisecond) - // What passed the limit is only what the connection's buffers took before the router - // stopped reading: a few MiB at most, well short of another MaxFileBytes. - s.Less(sent, int64(2*sandbox.MaxFileBytes), "the router read on past the limit") + s.requireReadUpToTheLimit(answers) } func (s *DaytonaSuite) TestADownloadsErrorFarTooLargeIsNotReadWhole() { s.platform.failing["/tmp/dump.bin"] = 10 * sandbox.MaxFileBytes + box, answers := s.tallyingBox("/files/bulk-download") - result, err := s.box.Run(s.ctx, "dump()", []string{"/tmp/dump.bin"}) + result, err := box.Run(s.ctx, "dump()", []string{"/tmp/dump.bin"}) s.Require().NoError(err) s.Equal([]string{"/tmp/dump.bin"}, result.Missing) - var sent int64 - platform := s.platform - s.Eventually(func() bool { - var ended bool - sent, ended = platform.sent("/tmp/dump.bin") - return ended - }, 10*time.Second, 10*time.Millisecond) // The SDK reads an error part whole; past its bound the router stops reading it. - s.Less(sent, int64(2*sandbox.MaxFileBytes), "the router read on past the limit") + s.requireReadUpToTheLimit(answers) } func (s *DaytonaSuite) TestAnUploadsRefusalFarTooLargeIsNotReadWhole() { s.platform.refuseUpload = true s.platform.refusal = 10 * sandbox.MaxFileBytes + box, answers := s.tallyingBox("/files/upload-v2") - err := s.box.Write(s.ctx, "notes.txt", []byte("hello")) + err := box.Write(s.ctx, "notes.txt", []byte("hello")) s.Require().Error(err) - var sent int64 - platform := s.platform - s.Eventually(func() bool { - var ended bool - sent, ended = platform.sent("upload") - return ended - }, 10*time.Second, 10*time.Millisecond) // The SDK reads a refused upload's answer whole; past its bound the router stops reading it. - s.Less(sent, int64(2*sandbox.MaxFileBytes), "the router read on past the limit") + s.requireReadUpToTheLimit(answers) +} + +// requireReadUpToTheLimit checks that the router read the answer up to the limit and no further: +// well short of another MaxFileBytes. A count of nothing would pass a counter that sees no answer. +func (s *DaytonaSuite) requireReadUpToTheLimit(answers *tallied) { + read := answers.read.Load() + s.Greater(read, int64(sandbox.MaxFileBytes), "the answer was not read as far as the limit") + s.Less(read, int64(2*sandbox.MaxFileBytes), "the router read on past the limit") } func (s *DaytonaSuite) TestACommandsOutputFarOverTheBoundIsCutInItsError() { From 760d4e43869d9c242208b0985b632bb91fa066c2 Mon Sep 17 00:00:00 2001 From: Kanat Date: Wed, 25 Nov 2026 10:06:29 -0500 Subject: [PATCH 2/2] AI-3874: drop the unread upload-refusal byte counter in the daytona test fake --- acceleration/internal/sandbox/daytona/daytona_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/acceleration/internal/sandbox/daytona/daytona_test.go b/acceleration/internal/sandbox/daytona/daytona_test.go index cd66f0578b..aba8e080f2 100644 --- a/acceleration/internal/sandbox/daytona/daytona_test.go +++ b/acceleration/internal/sandbox/daytona/daytona_test.go @@ -51,7 +51,7 @@ type platform struct { // failing are the files whose download answers a part named error of that many bytes of x. failing map[string]int // refuseUpload answers every file written into the sandbox with a 400, and refusal, when set, - // with that many bytes of x after it, counted in served under "upload". + // with that many bytes of x after it. refuseUpload bool refusal int // commands are every shell command run in the sandbox, with how many runs came before, @@ -215,9 +215,7 @@ func (p *platform) serve(w http.ResponseWriter, r *http.Request) { file, _, err := r.FormFile("file") if err != nil || p.refuseUpload { w.WriteHeader(http.StatusBadRequest) - wire := &counted{Writer: w} - _, _ = io.Copy(wire, io.LimitReader(filler{}, int64(p.refusal))) - p.served["upload"] += wire.n + _, _ = io.Copy(w, io.LimitReader(filler{}, int64(p.refusal))) return } if r.Context().Err() != nil {