Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
improvement(sandbox): exempt caller-consumed streams from the output retention budget#6353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+229
−29
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ad81141
fix(sandbox): exempt caller-consumed streams from the output retentio…
icecrasher321 6a91592
test(sandbox): drop explicit any from the new conformance stream mocks
icecrasher321 922b5fa
fix(sandbox): cut Daytona's retained tail to the same bound as E2B
icecrasher321 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -13,12 +13,14 @@ import { | ||
| } from '@/lib/core/utils/stream-limits' | ||
| import { CodeLanguage } from '@/lib/execution/languages' | ||
| import { | ||
| appendStreamedSandboxOutput, | ||
| isSandboxOutputLimitError, | ||
| MAX_SANDBOX_OUTPUT_BYTES, | ||
| MAX_SANDBOX_PROCESS_OUTPUT_BYTES, | ||
| SandboxOutputFileError, | ||
| SandboxOutputLimitError, | ||
| SandboxProcessOutputBudget, | ||
| tailStreamedSandboxOutput, | ||
| } from '@/lib/execution/remote-sandbox/output-limits' | ||
| import type { | ||
| CreateSandboxOptions, | ||
| @@ -246,6 +248,18 @@ class DaytonaSandboxHandle implements SandboxHandle { | ||
| const outputBudget = new SandboxProcessOutputBudget( | ||
| options.maxOutputBytes ?? MAX_SANDBOX_PROCESS_OUTPUT_BYTES | ||
| ) | ||
| // Matches the E2B adapter: the budget bounds what Sim retains, so a stream the caller consumes | ||
| // itself is exempt and only a diagnostic tail is kept. Per stream, so a caller that streams | ||
| // stdout but not stderr still has stderr fully bounded. The failover must not change behavior. | ||
| const retainStdout = options.onStdout === undefined | ||
| const retainStderr = options.onStderr === undefined | ||
| // The appender keeps the accumulator under twice the tail so it is not re-cut on every chunk, | ||
| // which leaves it anywhere in that band when the stream ends. E2B tails the value it returns, | ||
| // so the final cut has to happen here too or a stream finishing between one and two tails comes | ||
| // back longer on Daytona than on E2B — a failover divergence, which is what this adapter pair | ||
| // must never have. | ||
| const finalStdout = () => (retainStdout ? stdout : tailStreamedSandboxOutput(stdout)) | ||
| const finalStderr = () => (retainStderr ? stderr : tailStreamedSandboxOutput(stderr)) | ||
| try { | ||
| await this.sandbox.process.createSession(sessionId) | ||
| sessionCreated = true | ||
| @@ -282,14 +296,17 @@ class DaytonaSandboxHandle implements SandboxHandle { | ||
| const appendOutput = ( | ||
| chunk: string, | ||
| append: (value: string) => void, | ||
| retain: boolean, | ||
| callback?: (value: string) => void | ||
| ) => { | ||
| try { | ||
| outputBudget.add(chunk) | ||
| } catch { | ||
| void this.kill().catch(() => {}) | ||
| resolveOutputLimit() | ||
| return | ||
| if (retain) { | ||
| try { | ||
| outputBudget.add(chunk) | ||
| } catch { | ||
| void this.kill().catch(() => {}) | ||
| resolveOutputLimit() | ||
| return | ||
| } | ||
| } | ||
| append(chunk) | ||
| callback?.(chunk) | ||
| @@ -302,17 +319,19 @@ class DaytonaSandboxHandle implements SandboxHandle { | ||
| appendOutput( | ||
| chunk, | ||
| (value) => { | ||
| stdout += value | ||
| stdout = retainStdout ? stdout + value : appendStreamedSandboxOutput(stdout, value) | ||
| }, | ||
| retainStdout, | ||
| options.onStdout | ||
| ) | ||
| }, | ||
| (chunk: string) => { | ||
| appendOutput( | ||
| chunk, | ||
| (value) => { | ||
| stderr += value | ||
| stderr = retainStderr ? stderr + value : appendStreamedSandboxOutput(stderr, value) | ||
| }, | ||
| retainStderr, | ||
icecrasher321 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| options.onStderr | ||
| ) | ||
| } | ||
| @@ -355,8 +374,8 @@ class DaytonaSandboxHandle implements SandboxHandle { | ||
| if (outcome === 'output-limit' || outputBudget.error) throw outputBudget.error | ||
| if (outcome === 'timeout') { | ||
| return { | ||
| stdout, | ||
| stderr: stderr || `Command timed out after ${options.timeoutMs}ms`, | ||
| stdout: finalStdout(), | ||
| stderr: finalStderr() || `Command timed out after ${options.timeoutMs}ms`, | ||
| exitCode: 124, | ||
| timedOut: true, | ||
| } | ||
| @@ -365,12 +384,17 @@ class DaytonaSandboxHandle implements SandboxHandle { | ||
| if (outputBudget.error) throw outputBudget.error | ||
| const timedOut = isDaytonaExecutionTimeout(streamError) | ||
| if (!timedOut) throw streamError | ||
| return { stdout, stderr: stderr || getErrorMessage(streamError), exitCode: 124, timedOut } | ||
| return { | ||
| stdout: finalStdout(), | ||
| stderr: finalStderr() || getErrorMessage(streamError), | ||
| exitCode: 124, | ||
| timedOut, | ||
| } | ||
| } | ||
| const finished = await this.sandbox.process.getSessionCommand(sessionId, commandId) | ||
| const exitCode = finished.exitCode ?? 0 | ||
| return { stdout, stderr, exitCode } | ||
| return { stdout: finalStdout(), stderr: finalStderr(), exitCode } | ||
| } catch (error) { | ||
| if (isSandboxOutputLimitError(error)) { | ||
| void this.kill().catch(() => {}) | ||
| @@ -382,10 +406,15 @@ class DaytonaSandboxHandle implements SandboxHandle { | ||
| : new DOMException('Execution cancelled', 'AbortError') | ||
| } | ||
| if (isDaytonaExecutionTimeout(error)) { | ||
| return { stdout, stderr: stderr || getErrorMessage(error), exitCode: 124, timedOut: true } | ||
| return { | ||
| stdout: finalStdout(), | ||
| stderr: finalStderr() || getErrorMessage(error), | ||
| exitCode: 124, | ||
| timedOut: true, | ||
| } | ||
| } | ||
| if (operation === 'code') throw error | ||
| return { stdout, stderr: stderr || getErrorMessage(error), exitCode: 1 } | ||
| return { stdout: finalStdout(), stderr: finalStderr() || getErrorMessage(error), exitCode: 1 } | ||
| } finally { | ||
| if (sessionCreated) { | ||
| try { | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.